2019-05-19 21:51:43 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-09-24 23:59:20 +08:00
|
|
|
/* A network driver using virtio.
|
2007-10-22 09:03:37 +08:00
|
|
|
*
|
|
|
|
* Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
|
|
|
|
*/
|
|
|
|
//#define DEBUG
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/etherdevice.h>
|
2008-04-18 11:21:42 +08:00
|
|
|
#include <linux/ethtool.h>
|
2007-10-22 09:03:37 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/virtio.h>
|
|
|
|
#include <linux/virtio_net.h>
|
2016-12-16 04:13:24 +08:00
|
|
|
#include <linux/bpf.h>
|
bpf: add initial bpf tracepoints
This work adds a number of tracepoints to paths that are either
considered slow-path or exception-like states, where monitoring or
inspecting them would be desirable.
For bpf(2) syscall, tracepoints have been placed for main commands
when they succeed. In XDP case, tracepoint is for exceptions, that
is, f.e. on abnormal BPF program exit such as unknown or XDP_ABORTED
return code, or when error occurs during XDP_TX action and the packet
could not be forwarded.
Both have been split into separate event headers, and can be further
extended. Worst case, if they unexpectedly should get into our way in
future, they can also removed [1]. Of course, these tracepoints (like
any other) can be analyzed by eBPF itself, etc. Example output:
# ./perf record -a -e bpf:* sleep 10
# ./perf script
sock_example 6197 [005] 283.980322: bpf:bpf_map_create: map type=ARRAY ufd=4 key=4 val=8 max=256 flags=0
sock_example 6197 [005] 283.980721: bpf:bpf_prog_load: prog=a5ea8fa30ea6849c type=SOCKET_FILTER ufd=5
sock_example 6197 [005] 283.988423: bpf:bpf_prog_get_type: prog=a5ea8fa30ea6849c type=SOCKET_FILTER
sock_example 6197 [005] 283.988443: bpf:bpf_map_lookup_elem: map type=ARRAY ufd=4 key=[06 00 00 00] val=[00 00 00 00 00 00 00 00]
[...]
sock_example 6197 [005] 288.990868: bpf:bpf_map_lookup_elem: map type=ARRAY ufd=4 key=[01 00 00 00] val=[14 00 00 00 00 00 00 00]
swapper 0 [005] 289.338243: bpf:bpf_prog_put_rcu: prog=a5ea8fa30ea6849c type=SOCKET_FILTER
[1] https://lwn.net/Articles/705270/
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-01-25 09:28:18 +08:00
|
|
|
#include <linux/bpf_trace.h>
|
2007-10-22 09:03:37 +08:00
|
|
|
#include <linux/scatterlist.h>
|
2009-01-26 10:06:26 +08:00
|
|
|
#include <linux/if_vlan.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2013-01-25 07:51:31 +08:00
|
|
|
#include <linux/cpu.h>
|
2014-01-17 14:23:27 +08:00
|
|
|
#include <linux/average.h>
|
2017-09-19 17:42:43 +08:00
|
|
|
#include <linux/filter.h>
|
2018-08-10 08:28:40 +08:00
|
|
|
#include <linux/kernel.h>
|
2017-03-09 08:21:21 +08:00
|
|
|
#include <net/route.h>
|
2018-01-03 18:26:04 +08:00
|
|
|
#include <net/xdp.h>
|
2018-05-25 00:55:17 +08:00
|
|
|
#include <net/net_failover.h>
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2013-05-10 03:50:51 +08:00
|
|
|
static int napi_weight = NAPI_POLL_WEIGHT;
|
2007-12-16 21:19:43 +08:00
|
|
|
module_param(napi_weight, int, 0444);
|
|
|
|
|
2019-06-14 00:24:57 +08:00
|
|
|
static bool csum = true, gso = true, napi_tx = true;
|
2008-02-05 12:50:02 +08:00
|
|
|
module_param(csum, bool, 0444);
|
|
|
|
module_param(gso, bool, 0444);
|
2017-04-25 01:49:27 +08:00
|
|
|
module_param(napi_tx, bool, 0644);
|
2008-02-05 12:50:02 +08:00
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
/* FIXME: MTU in config. */
|
2013-11-15 02:41:04 +08:00
|
|
|
#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
|
2008-11-17 14:41:34 +08:00
|
|
|
#define GOOD_COPY_LEN 128
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2017-02-21 16:46:28 +08:00
|
|
|
#define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
|
|
|
|
|
2017-02-03 11:16:29 +08:00
|
|
|
/* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
|
|
|
|
#define VIRTIO_XDP_HEADROOM 256
|
|
|
|
|
2018-06-26 23:39:58 +08:00
|
|
|
/* Separating two types of XDP xmit */
|
|
|
|
#define VIRTIO_XDP_TX BIT(0)
|
|
|
|
#define VIRTIO_XDP_REDIR BIT(1)
|
|
|
|
|
2019-01-29 08:45:59 +08:00
|
|
|
#define VIRTIO_XDP_FLAG BIT(0)
|
|
|
|
|
2015-08-19 15:48:40 +08:00
|
|
|
/* RX packet size EWMA. The average packet size is used to determine the packet
|
|
|
|
* buffer size when refilling RX rings. As the entire RX ring may be refilled
|
|
|
|
* at once, the weight is chosen so that the EWMA will be insensitive to short-
|
|
|
|
* term, transient changes in packet size.
|
2014-01-17 14:23:27 +08:00
|
|
|
*/
|
2017-02-15 16:49:26 +08:00
|
|
|
DECLARE_EWMA(pkt_len, 0, 64)
|
2014-01-17 14:23:27 +08:00
|
|
|
|
2011-11-14 22:17:08 +08:00
|
|
|
#define VIRTNET_DRIVER_VERSION "1.0.0"
|
2009-02-04 17:02:34 +08:00
|
|
|
|
2017-08-13 05:45:53 +08:00
|
|
|
static const unsigned long guest_offloads[] = {
|
|
|
|
VIRTIO_NET_F_GUEST_TSO4,
|
|
|
|
VIRTIO_NET_F_GUEST_TSO6,
|
|
|
|
VIRTIO_NET_F_GUEST_ECN,
|
2018-11-22 14:36:30 +08:00
|
|
|
VIRTIO_NET_F_GUEST_UFO,
|
|
|
|
VIRTIO_NET_F_GUEST_CSUM
|
2017-08-13 05:45:53 +08:00
|
|
|
};
|
2017-07-19 16:54:49 +08:00
|
|
|
|
2018-01-17 14:38:25 +08:00
|
|
|
struct virtnet_stat_desc {
|
|
|
|
char desc[ETH_GSTRING_LEN];
|
|
|
|
size_t offset;
|
2011-06-15 14:36:29 +08:00
|
|
|
};
|
|
|
|
|
2018-01-17 14:38:25 +08:00
|
|
|
struct virtnet_sq_stats {
|
|
|
|
struct u64_stats_sync syncp;
|
|
|
|
u64 packets;
|
|
|
|
u64 bytes;
|
2018-07-23 22:36:08 +08:00
|
|
|
u64 xdp_tx;
|
|
|
|
u64 xdp_tx_drops;
|
2018-07-23 22:36:09 +08:00
|
|
|
u64 kicks;
|
2018-01-17 14:38:25 +08:00
|
|
|
};
|
|
|
|
|
2018-07-31 17:43:39 +08:00
|
|
|
struct virtnet_rq_stats {
|
|
|
|
struct u64_stats_sync syncp;
|
2018-01-17 14:38:25 +08:00
|
|
|
u64 packets;
|
|
|
|
u64 bytes;
|
2018-07-23 22:36:06 +08:00
|
|
|
u64 drops;
|
2018-07-23 22:36:08 +08:00
|
|
|
u64 xdp_packets;
|
|
|
|
u64 xdp_tx;
|
|
|
|
u64 xdp_redirects;
|
|
|
|
u64 xdp_drops;
|
2018-07-23 22:36:09 +08:00
|
|
|
u64 kicks;
|
2018-01-17 14:38:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
|
2018-07-31 17:43:39 +08:00
|
|
|
#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
|
2018-01-17 14:38:25 +08:00
|
|
|
|
|
|
|
static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
|
2018-07-23 22:36:08 +08:00
|
|
|
{ "packets", VIRTNET_SQ_STAT(packets) },
|
|
|
|
{ "bytes", VIRTNET_SQ_STAT(bytes) },
|
|
|
|
{ "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
|
|
|
|
{ "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
|
2018-07-23 22:36:09 +08:00
|
|
|
{ "kicks", VIRTNET_SQ_STAT(kicks) },
|
2018-01-17 14:38:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
|
2018-07-23 22:36:08 +08:00
|
|
|
{ "packets", VIRTNET_RQ_STAT(packets) },
|
|
|
|
{ "bytes", VIRTNET_RQ_STAT(bytes) },
|
|
|
|
{ "drops", VIRTNET_RQ_STAT(drops) },
|
|
|
|
{ "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
|
|
|
|
{ "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
|
|
|
|
{ "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
|
|
|
|
{ "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
|
2018-07-23 22:36:09 +08:00
|
|
|
{ "kicks", VIRTNET_RQ_STAT(kicks) },
|
2018-01-17 14:38:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
|
|
|
|
#define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
|
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
/* Internal representation of a send virtqueue */
|
|
|
|
struct send_queue {
|
|
|
|
/* Virtqueue associated with this send _queue */
|
|
|
|
struct virtqueue *vq;
|
|
|
|
|
|
|
|
/* TX: fragments + linear part + virtio header */
|
|
|
|
struct scatterlist sg[MAX_SKB_FRAGS + 2];
|
2012-12-07 15:04:56 +08:00
|
|
|
|
|
|
|
/* Name of the send queue: output.$index */
|
|
|
|
char name[40];
|
2017-04-25 01:49:27 +08:00
|
|
|
|
2018-01-17 14:38:25 +08:00
|
|
|
struct virtnet_sq_stats stats;
|
|
|
|
|
2017-04-25 01:49:27 +08:00
|
|
|
struct napi_struct napi;
|
2012-12-07 15:04:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Internal representation of a receive virtqueue */
|
|
|
|
struct receive_queue {
|
|
|
|
/* Virtqueue associated with this receive_queue */
|
|
|
|
struct virtqueue *vq;
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
struct napi_struct napi;
|
|
|
|
|
2016-12-16 04:13:24 +08:00
|
|
|
struct bpf_prog __rcu *xdp_prog;
|
|
|
|
|
2018-01-17 14:38:25 +08:00
|
|
|
struct virtnet_rq_stats stats;
|
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
/* Chain pages by the private ptr. */
|
|
|
|
struct page *pages;
|
|
|
|
|
2014-01-17 14:23:27 +08:00
|
|
|
/* Average packet length for mergeable receive buffers. */
|
2015-08-19 15:48:40 +08:00
|
|
|
struct ewma_pkt_len mrg_avg_pkt_len;
|
2014-01-17 14:23:27 +08:00
|
|
|
|
2014-01-17 14:23:26 +08:00
|
|
|
/* Page frag for packet buffer allocation. */
|
|
|
|
struct page_frag alloc_frag;
|
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
/* RX: fragments + linear part + virtio header */
|
|
|
|
struct scatterlist sg[MAX_SKB_FRAGS + 2];
|
2012-12-07 15:04:56 +08:00
|
|
|
|
2017-03-09 08:21:21 +08:00
|
|
|
/* Min single buffer size for mergeable buffers case. */
|
|
|
|
unsigned int min_buf_len;
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
/* Name of this receive queue: input.$index */
|
|
|
|
char name[40];
|
2018-01-03 18:26:04 +08:00
|
|
|
|
|
|
|
struct xdp_rxq_info xdp_rxq;
|
2012-12-07 15:04:55 +08:00
|
|
|
};
|
|
|
|
|
2018-04-19 13:30:48 +08:00
|
|
|
/* Control VQ buffers: protected by the rtnl lock */
|
|
|
|
struct control_buf {
|
|
|
|
struct virtio_net_ctrl_hdr hdr;
|
|
|
|
virtio_net_ctrl_ack status;
|
|
|
|
struct virtio_net_ctrl_mq mq;
|
|
|
|
u8 promisc;
|
|
|
|
u8 allmulti;
|
2018-04-19 13:30:49 +08:00
|
|
|
__virtio16 vid;
|
2018-04-19 13:30:50 +08:00
|
|
|
__virtio64 offloads;
|
2018-04-19 13:30:48 +08:00
|
|
|
};
|
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
struct virtnet_info {
|
|
|
|
struct virtio_device *vdev;
|
|
|
|
struct virtqueue *cvq;
|
|
|
|
struct net_device *dev;
|
2012-12-07 15:04:56 +08:00
|
|
|
struct send_queue *sq;
|
|
|
|
struct receive_queue *rq;
|
2012-12-07 15:04:55 +08:00
|
|
|
unsigned int status;
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
/* Max # of queue pairs supported by the device */
|
|
|
|
u16 max_queue_pairs;
|
|
|
|
|
|
|
|
/* # of queue pairs currently used by the driver */
|
|
|
|
u16 curr_queue_pairs;
|
|
|
|
|
2016-12-16 04:13:49 +08:00
|
|
|
/* # of XDP queue pairs currently used by the driver */
|
|
|
|
u16 xdp_queue_pairs;
|
|
|
|
|
2008-04-18 11:24:27 +08:00
|
|
|
/* I like... big packets and I cannot lie! */
|
|
|
|
bool big_packets;
|
|
|
|
|
2008-11-17 14:41:34 +08:00
|
|
|
/* Host will merge rx buffers for big packets (shake it! shake it!) */
|
|
|
|
bool mergeable_rx_bufs;
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
/* Has control virtqueue */
|
|
|
|
bool has_cvq;
|
|
|
|
|
2013-07-25 08:50:23 +08:00
|
|
|
/* Host can handle any s/g split between our header and packet data */
|
|
|
|
bool any_header_sg;
|
|
|
|
|
2014-10-24 21:55:57 +08:00
|
|
|
/* Packet virtio header size */
|
|
|
|
u8 hdr_len;
|
|
|
|
|
2009-08-27 03:22:32 +08:00
|
|
|
/* Work struct for refilling if we run low on memory. */
|
|
|
|
struct delayed_work refill;
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
/* Work struct for config space updates */
|
|
|
|
struct work_struct config_work;
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
/* Does the affinity hint is set for virtqueues? */
|
|
|
|
bool affinity_hint_set;
|
2013-01-25 07:51:29 +08:00
|
|
|
|
2016-08-13 01:49:43 +08:00
|
|
|
/* CPU hotplug instances for online & dead */
|
|
|
|
struct hlist_node node;
|
|
|
|
struct hlist_node node_dead;
|
2015-11-15 21:11:00 +08:00
|
|
|
|
2018-04-19 13:30:48 +08:00
|
|
|
struct control_buf *ctrl;
|
2016-02-03 11:04:37 +08:00
|
|
|
|
|
|
|
/* Ethtool settings */
|
|
|
|
u8 duplex;
|
|
|
|
u32 speed;
|
2017-07-19 16:54:49 +08:00
|
|
|
|
|
|
|
unsigned long guest_offloads;
|
2018-12-21 06:14:54 +08:00
|
|
|
unsigned long guest_offloads_capable;
|
2018-05-25 00:55:17 +08:00
|
|
|
|
|
|
|
/* failover when STANDBY feature enabled */
|
|
|
|
struct failover *failover;
|
2007-10-22 09:03:37 +08:00
|
|
|
};
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
struct padded_vnet_hdr {
|
2014-10-24 21:55:57 +08:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf hdr;
|
2010-01-29 11:20:04 +08:00
|
|
|
/*
|
2014-10-24 21:55:57 +08:00
|
|
|
* hdr is in a separate sg buffer, and data sg buffer shares same page
|
|
|
|
* with this header sg. This padding makes next sg 16 byte aligned
|
|
|
|
* after the header.
|
2010-01-29 11:20:04 +08:00
|
|
|
*/
|
2014-10-24 21:55:57 +08:00
|
|
|
char padding[4];
|
2010-01-29 11:20:04 +08:00
|
|
|
};
|
|
|
|
|
2019-01-29 08:45:59 +08:00
|
|
|
static bool is_xdp_frame(void *ptr)
|
|
|
|
{
|
|
|
|
return (unsigned long)ptr & VIRTIO_XDP_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *xdp_to_ptr(struct xdp_frame *ptr)
|
|
|
|
{
|
|
|
|
return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct xdp_frame *ptr_to_xdp(void *ptr)
|
|
|
|
{
|
|
|
|
return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
|
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
/* Converting between virtqueue no. and kernel tx/rx queue no.
|
|
|
|
* 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
|
|
|
|
*/
|
|
|
|
static int vq2txq(struct virtqueue *vq)
|
|
|
|
{
|
2013-03-21 22:17:34 +08:00
|
|
|
return (vq->index - 1) / 2;
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int txq2vq(int txq)
|
|
|
|
{
|
|
|
|
return txq * 2 + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vq2rxq(struct virtqueue *vq)
|
|
|
|
{
|
2013-03-21 22:17:34 +08:00
|
|
|
return vq->index / 2;
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rxq2vq(int rxq)
|
|
|
|
{
|
|
|
|
return rxq * 2;
|
|
|
|
}
|
|
|
|
|
2014-10-24 21:55:57 +08:00
|
|
|
static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2014-10-24 21:55:57 +08:00
|
|
|
return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
/*
|
|
|
|
* private is used to chain pages for big packets, put the whole
|
|
|
|
* most recent used list in the beginning for reuse
|
|
|
|
*/
|
2012-12-07 15:04:55 +08:00
|
|
|
static void give_pages(struct receive_queue *rq, struct page *page)
|
2008-11-17 14:39:18 +08:00
|
|
|
{
|
2010-01-29 11:20:04 +08:00
|
|
|
struct page *end;
|
2008-11-17 14:39:18 +08:00
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
/* Find end of list, sew whole thing into vi->rq.pages. */
|
2010-01-29 11:20:04 +08:00
|
|
|
for (end = page; end->private; end = (struct page *)end->private);
|
2012-12-07 15:04:55 +08:00
|
|
|
end->private = (unsigned long)rq->pages;
|
|
|
|
rq->pages = page;
|
2008-11-17 14:39:18 +08:00
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
|
2008-07-26 01:06:01 +08:00
|
|
|
{
|
2012-12-07 15:04:55 +08:00
|
|
|
struct page *p = rq->pages;
|
2008-07-26 01:06:01 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
if (p) {
|
2012-12-07 15:04:55 +08:00
|
|
|
rq->pages = (struct page *)p->private;
|
2010-01-29 11:20:04 +08:00
|
|
|
/* clear private here, it is used to chain pages */
|
|
|
|
p->private = 0;
|
|
|
|
} else
|
2008-07-26 01:06:01 +08:00
|
|
|
p = alloc_page(gfp_mask);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-04-25 01:49:26 +08:00
|
|
|
static void virtqueue_napi_schedule(struct napi_struct *napi,
|
|
|
|
struct virtqueue *vq)
|
|
|
|
{
|
|
|
|
if (napi_schedule_prep(napi)) {
|
|
|
|
virtqueue_disable_cb(vq);
|
|
|
|
__napi_schedule(napi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtqueue_napi_complete(struct napi_struct *napi,
|
|
|
|
struct virtqueue *vq, int processed)
|
|
|
|
{
|
|
|
|
int opaque;
|
|
|
|
|
|
|
|
opaque = virtqueue_enable_cb_prepare(vq);
|
2017-12-07 12:15:15 +08:00
|
|
|
if (napi_complete_done(napi, processed)) {
|
|
|
|
if (unlikely(virtqueue_poll(vq, opaque)))
|
|
|
|
virtqueue_napi_schedule(napi, vq);
|
|
|
|
} else {
|
|
|
|
virtqueue_disable_cb(vq);
|
|
|
|
}
|
2017-04-25 01:49:26 +08:00
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
static void skb_xmit_done(struct virtqueue *vq)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2012-12-07 15:04:55 +08:00
|
|
|
struct virtnet_info *vi = vq->vdev->priv;
|
2017-04-25 01:49:27 +08:00
|
|
|
struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2008-02-05 12:50:07 +08:00
|
|
|
/* Suppress further interrupts. */
|
2012-12-07 15:04:55 +08:00
|
|
|
virtqueue_disable_cb(vq);
|
2008-05-26 15:48:13 +08:00
|
|
|
|
2017-04-25 01:49:27 +08:00
|
|
|
if (napi->weight)
|
|
|
|
virtqueue_napi_schedule(napi, vq);
|
|
|
|
else
|
|
|
|
/* We were probably waiting for more output buffers. */
|
|
|
|
netif_wake_subqueue(vi->dev, vq2txq(vq));
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2017-07-19 16:54:46 +08:00
|
|
|
#define MRG_CTX_HEADER_SHIFT 22
|
|
|
|
static void *mergeable_len_to_ctx(unsigned int truesize,
|
|
|
|
unsigned int headroom)
|
|
|
|
{
|
|
|
|
return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
|
|
|
|
{
|
|
|
|
return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
|
|
|
|
{
|
|
|
|
return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
|
|
|
|
}
|
|
|
|
|
2012-01-04 20:52:32 +08:00
|
|
|
/* Called from bottom half context */
|
2014-10-24 05:12:10 +08:00
|
|
|
static struct sk_buff *page_to_skb(struct virtnet_info *vi,
|
|
|
|
struct receive_queue *rq,
|
2013-10-29 06:44:18 +08:00
|
|
|
struct page *page, unsigned int offset,
|
2018-11-29 13:53:16 +08:00
|
|
|
unsigned int len, unsigned int truesize,
|
2020-02-25 11:32:12 +08:00
|
|
|
bool hdr_valid, unsigned int metasize)
|
2010-01-29 11:20:04 +08:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
2014-10-24 21:55:57 +08:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr;
|
2013-10-29 06:44:18 +08:00
|
|
|
unsigned int copy, hdr_len, hdr_padded_len;
|
2010-01-29 11:20:04 +08:00
|
|
|
char *p;
|
2008-07-26 01:06:01 +08:00
|
|
|
|
2013-10-29 06:44:18 +08:00
|
|
|
p = page_address(page) + offset;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
/* copy small packet so we can reuse these pages for small data */
|
2016-03-17 22:44:00 +08:00
|
|
|
skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (unlikely(!skb))
|
|
|
|
return NULL;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
hdr = skb_vnet_hdr(skb);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2014-10-24 21:55:57 +08:00
|
|
|
hdr_len = vi->hdr_len;
|
|
|
|
if (vi->mergeable_rx_bufs)
|
2017-08-16 01:29:17 +08:00
|
|
|
hdr_padded_len = sizeof(*hdr);
|
2014-10-24 21:55:57 +08:00
|
|
|
else
|
2013-10-29 06:44:18 +08:00
|
|
|
hdr_padded_len = sizeof(struct padded_vnet_hdr);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2020-02-25 11:32:12 +08:00
|
|
|
/* hdr_valid means no XDP, so we can copy the vnet header */
|
2018-11-29 13:53:16 +08:00
|
|
|
if (hdr_valid)
|
|
|
|
memcpy(hdr, p, hdr_len);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
len -= hdr_len;
|
2013-10-29 06:44:18 +08:00
|
|
|
offset += hdr_padded_len;
|
|
|
|
p += hdr_padded_len;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
copy = len;
|
|
|
|
if (copy > skb_tailroom(skb))
|
|
|
|
copy = skb_tailroom(skb);
|
networking: introduce and use skb_put_data()
A common pattern with skb_put() is to just want to memcpy()
some data into the new space, introduce skb_put_data() for
this.
An spatch similar to the one for skb_put_zero() converts many
of the places using it:
@@
identifier p, p2;
expression len, skb, data;
type t, t2;
@@
(
-p = skb_put(skb, len);
+p = skb_put_data(skb, data, len);
|
-p = (t)skb_put(skb, len);
+p = skb_put_data(skb, data, len);
)
(
p2 = (t2)p;
-memcpy(p2, data, len);
|
-memcpy(p, data, len);
)
@@
type t, t2;
identifier p, p2;
expression skb, data;
@@
t *p;
...
(
-p = skb_put(skb, sizeof(t));
+p = skb_put_data(skb, data, sizeof(t));
|
-p = (t *)skb_put(skb, sizeof(t));
+p = skb_put_data(skb, data, sizeof(t));
)
(
p2 = (t2)p;
-memcpy(p2, data, sizeof(*p));
|
-memcpy(p, data, sizeof(*p));
)
@@
expression skb, len, data;
@@
-memcpy(skb_put(skb, len), data, len);
+skb_put_data(skb, data, len);
(again, manually post-processed to retain some comments)
Reviewed-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-06-16 20:29:20 +08:00
|
|
|
skb_put_data(skb, p, copy);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2020-02-25 11:32:12 +08:00
|
|
|
if (metasize) {
|
|
|
|
__skb_pull(skb, metasize);
|
|
|
|
skb_metadata_set(skb, metasize);
|
|
|
|
}
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
len -= copy;
|
|
|
|
offset += copy;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2013-10-29 06:44:18 +08:00
|
|
|
if (vi->mergeable_rx_bufs) {
|
|
|
|
if (len)
|
|
|
|
skb_add_rx_frag(skb, 0, page, offset, len, truesize);
|
|
|
|
else
|
|
|
|
put_page(page);
|
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
2011-09-28 12:40:54 +08:00
|
|
|
/*
|
|
|
|
* Verify that we can indeed put this data into a skb.
|
|
|
|
* This is here to handle cases when the device erroneously
|
|
|
|
* tries to receive more than is possible. This is usually
|
|
|
|
* the case of a broken device.
|
|
|
|
*/
|
|
|
|
if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
|
2012-11-09 01:47:28 +08:00
|
|
|
net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
|
2011-09-28 12:40:54 +08:00
|
|
|
dev_kfree_skb(skb);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-29 06:44:18 +08:00
|
|
|
BUG_ON(offset >= PAGE_SIZE);
|
2010-01-29 11:20:04 +08:00
|
|
|
while (len) {
|
2013-10-29 06:44:18 +08:00
|
|
|
unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
|
|
|
|
skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
|
|
|
|
frag_size, truesize);
|
|
|
|
len -= frag_size;
|
2010-01-29 11:20:04 +08:00
|
|
|
page = (struct page *)page->private;
|
|
|
|
offset = 0;
|
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
if (page)
|
2012-12-07 15:04:55 +08:00
|
|
|
give_pages(rq, page);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
return skb;
|
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
xdp: change ndo_xdp_xmit API to support bulking
This patch change the API for ndo_xdp_xmit to support bulking
xdp_frames.
When kernel is compiled with CONFIG_RETPOLINE, XDP sees a huge slowdown.
Most of the slowdown is caused by DMA API indirect function calls, but
also the net_device->ndo_xdp_xmit() call.
Benchmarked patch with CONFIG_RETPOLINE, using xdp_redirect_map with
single flow/core test (CPU E5-1650 v4 @ 3.60GHz), showed
performance improved:
for driver ixgbe: 6,042,682 pps -> 6,853,768 pps = +811,086 pps
for driver i40e : 6,187,169 pps -> 6,724,519 pps = +537,350 pps
With frames avail as a bulk inside the driver ndo_xdp_xmit call,
further optimizations are possible, like bulk DMA-mapping for TX.
Testing without CONFIG_RETPOLINE show the same performance for
physical NIC drivers.
The virtual NIC driver tun sees a huge performance boost, as it can
avoid doing per frame producer locking, but instead amortize the
locking cost over the bulk.
V2: Fix compile errors reported by kbuild test robot <lkp@intel.com>
V4: Isolated ndo, driver changes and callers.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-24 22:46:12 +08:00
|
|
|
static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
|
|
|
|
struct send_queue *sq,
|
|
|
|
struct xdp_frame *xdpf)
|
2016-12-16 04:14:13 +08:00
|
|
|
{
|
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr;
|
|
|
|
int err;
|
|
|
|
|
2018-04-17 22:45:52 +08:00
|
|
|
if (unlikely(xdpf->headroom < vi->hdr_len))
|
|
|
|
return -EOVERFLOW;
|
|
|
|
|
|
|
|
/* Make room for virtqueue hdr (also change xdpf->headroom?) */
|
|
|
|
xdpf->data -= vi->hdr_len;
|
2017-02-21 16:46:28 +08:00
|
|
|
/* Zero header and leave csum up to XDP layers */
|
2018-04-17 22:45:52 +08:00
|
|
|
hdr = xdpf->data;
|
2017-02-21 16:46:28 +08:00
|
|
|
memset(hdr, 0, vi->hdr_len);
|
2018-04-17 22:45:52 +08:00
|
|
|
xdpf->len += vi->hdr_len;
|
2016-12-23 22:37:32 +08:00
|
|
|
|
2018-04-17 22:45:52 +08:00
|
|
|
sg_init_one(sq->sg, xdpf->data, xdpf->len);
|
2016-12-23 22:37:32 +08:00
|
|
|
|
2019-01-29 08:45:59 +08:00
|
|
|
err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
|
|
|
|
GFP_ATOMIC);
|
2018-02-20 21:32:15 +08:00
|
|
|
if (unlikely(err))
|
2018-04-17 22:45:52 +08:00
|
|
|
return -ENOSPC; /* Caller handle free/refcnt */
|
2016-12-16 04:14:13 +08:00
|
|
|
|
2018-04-17 22:45:52 +08:00
|
|
|
return 0;
|
2016-12-16 04:14:13 +08:00
|
|
|
}
|
|
|
|
|
2018-07-23 22:36:07 +08:00
|
|
|
static struct send_queue *virtnet_xdp_sq(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
unsigned int qp;
|
|
|
|
|
|
|
|
qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
|
|
|
|
return &vi->sq[qp];
|
|
|
|
}
|
|
|
|
|
xdp: change ndo_xdp_xmit API to support bulking
This patch change the API for ndo_xdp_xmit to support bulking
xdp_frames.
When kernel is compiled with CONFIG_RETPOLINE, XDP sees a huge slowdown.
Most of the slowdown is caused by DMA API indirect function calls, but
also the net_device->ndo_xdp_xmit() call.
Benchmarked patch with CONFIG_RETPOLINE, using xdp_redirect_map with
single flow/core test (CPU E5-1650 v4 @ 3.60GHz), showed
performance improved:
for driver ixgbe: 6,042,682 pps -> 6,853,768 pps = +811,086 pps
for driver i40e : 6,187,169 pps -> 6,724,519 pps = +537,350 pps
With frames avail as a bulk inside the driver ndo_xdp_xmit call,
further optimizations are possible, like bulk DMA-mapping for TX.
Testing without CONFIG_RETPOLINE show the same performance for
physical NIC drivers.
The virtual NIC driver tun sees a huge performance boost, as it can
avoid doing per frame producer locking, but instead amortize the
locking cost over the bulk.
V2: Fix compile errors reported by kbuild test robot <lkp@intel.com>
V4: Isolated ndo, driver changes and callers.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-24 22:46:12 +08:00
|
|
|
static int virtnet_xdp_xmit(struct net_device *dev,
|
2018-05-31 16:59:47 +08:00
|
|
|
int n, struct xdp_frame **frames, u32 flags)
|
2017-09-19 17:42:43 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2018-02-20 21:32:20 +08:00
|
|
|
struct receive_queue *rq = vi->rq;
|
|
|
|
struct bpf_prog *xdp_prog;
|
xdp: change ndo_xdp_xmit API to support bulking
This patch change the API for ndo_xdp_xmit to support bulking
xdp_frames.
When kernel is compiled with CONFIG_RETPOLINE, XDP sees a huge slowdown.
Most of the slowdown is caused by DMA API indirect function calls, but
also the net_device->ndo_xdp_xmit() call.
Benchmarked patch with CONFIG_RETPOLINE, using xdp_redirect_map with
single flow/core test (CPU E5-1650 v4 @ 3.60GHz), showed
performance improved:
for driver ixgbe: 6,042,682 pps -> 6,853,768 pps = +811,086 pps
for driver i40e : 6,187,169 pps -> 6,724,519 pps = +537,350 pps
With frames avail as a bulk inside the driver ndo_xdp_xmit call,
further optimizations are possible, like bulk DMA-mapping for TX.
Testing without CONFIG_RETPOLINE show the same performance for
physical NIC drivers.
The virtual NIC driver tun sees a huge performance boost, as it can
avoid doing per frame producer locking, but instead amortize the
locking cost over the bulk.
V2: Fix compile errors reported by kbuild test robot <lkp@intel.com>
V4: Isolated ndo, driver changes and callers.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-24 22:46:12 +08:00
|
|
|
struct send_queue *sq;
|
|
|
|
unsigned int len;
|
2019-01-31 19:40:30 +08:00
|
|
|
int packets = 0;
|
|
|
|
int bytes = 0;
|
xdp: change ndo_xdp_xmit API to support bulking
This patch change the API for ndo_xdp_xmit to support bulking
xdp_frames.
When kernel is compiled with CONFIG_RETPOLINE, XDP sees a huge slowdown.
Most of the slowdown is caused by DMA API indirect function calls, but
also the net_device->ndo_xdp_xmit() call.
Benchmarked patch with CONFIG_RETPOLINE, using xdp_redirect_map with
single flow/core test (CPU E5-1650 v4 @ 3.60GHz), showed
performance improved:
for driver ixgbe: 6,042,682 pps -> 6,853,768 pps = +811,086 pps
for driver i40e : 6,187,169 pps -> 6,724,519 pps = +537,350 pps
With frames avail as a bulk inside the driver ndo_xdp_xmit call,
further optimizations are possible, like bulk DMA-mapping for TX.
Testing without CONFIG_RETPOLINE show the same performance for
physical NIC drivers.
The virtual NIC driver tun sees a huge performance boost, as it can
avoid doing per frame producer locking, but instead amortize the
locking cost over the bulk.
V2: Fix compile errors reported by kbuild test robot <lkp@intel.com>
V4: Isolated ndo, driver changes and callers.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-24 22:46:12 +08:00
|
|
|
int drops = 0;
|
2018-07-23 22:36:09 +08:00
|
|
|
int kicks = 0;
|
2018-07-23 22:36:08 +08:00
|
|
|
int ret, err;
|
2019-01-29 08:45:59 +08:00
|
|
|
void *ptr;
|
xdp: change ndo_xdp_xmit API to support bulking
This patch change the API for ndo_xdp_xmit to support bulking
xdp_frames.
When kernel is compiled with CONFIG_RETPOLINE, XDP sees a huge slowdown.
Most of the slowdown is caused by DMA API indirect function calls, but
also the net_device->ndo_xdp_xmit() call.
Benchmarked patch with CONFIG_RETPOLINE, using xdp_redirect_map with
single flow/core test (CPU E5-1650 v4 @ 3.60GHz), showed
performance improved:
for driver ixgbe: 6,042,682 pps -> 6,853,768 pps = +811,086 pps
for driver i40e : 6,187,169 pps -> 6,724,519 pps = +537,350 pps
With frames avail as a bulk inside the driver ndo_xdp_xmit call,
further optimizations are possible, like bulk DMA-mapping for TX.
Testing without CONFIG_RETPOLINE show the same performance for
physical NIC drivers.
The virtual NIC driver tun sees a huge performance boost, as it can
avoid doing per frame producer locking, but instead amortize the
locking cost over the bulk.
V2: Fix compile errors reported by kbuild test robot <lkp@intel.com>
V4: Isolated ndo, driver changes and callers.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-24 22:46:12 +08:00
|
|
|
int i;
|
|
|
|
|
2018-02-20 21:32:20 +08:00
|
|
|
/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
|
|
|
|
* indicate XDP resources have been successfully allocated.
|
|
|
|
*/
|
2020-01-27 08:14:01 +08:00
|
|
|
xdp_prog = rcu_access_pointer(rq->xdp_prog);
|
2019-01-29 08:45:56 +08:00
|
|
|
if (!xdp_prog)
|
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
sq = virtnet_xdp_sq(vi);
|
|
|
|
|
|
|
|
if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
|
|
|
|
ret = -EINVAL;
|
2018-07-23 22:36:08 +08:00
|
|
|
drops = n;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-02-20 21:32:20 +08:00
|
|
|
|
xdp: change ndo_xdp_xmit API to support bulking
This patch change the API for ndo_xdp_xmit to support bulking
xdp_frames.
When kernel is compiled with CONFIG_RETPOLINE, XDP sees a huge slowdown.
Most of the slowdown is caused by DMA API indirect function calls, but
also the net_device->ndo_xdp_xmit() call.
Benchmarked patch with CONFIG_RETPOLINE, using xdp_redirect_map with
single flow/core test (CPU E5-1650 v4 @ 3.60GHz), showed
performance improved:
for driver ixgbe: 6,042,682 pps -> 6,853,768 pps = +811,086 pps
for driver i40e : 6,187,169 pps -> 6,724,519 pps = +537,350 pps
With frames avail as a bulk inside the driver ndo_xdp_xmit call,
further optimizations are possible, like bulk DMA-mapping for TX.
Testing without CONFIG_RETPOLINE show the same performance for
physical NIC drivers.
The virtual NIC driver tun sees a huge performance boost, as it can
avoid doing per frame producer locking, but instead amortize the
locking cost over the bulk.
V2: Fix compile errors reported by kbuild test robot <lkp@intel.com>
V4: Isolated ndo, driver changes and callers.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-24 22:46:12 +08:00
|
|
|
/* Free up any pending old buffers before queueing new ones. */
|
2019-01-29 08:45:59 +08:00
|
|
|
while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
|
2019-01-31 19:40:30 +08:00
|
|
|
if (likely(is_xdp_frame(ptr))) {
|
|
|
|
struct xdp_frame *frame = ptr_to_xdp(ptr);
|
|
|
|
|
|
|
|
bytes += frame->len;
|
|
|
|
xdp_return_frame(frame);
|
|
|
|
} else {
|
|
|
|
struct sk_buff *skb = ptr;
|
|
|
|
|
|
|
|
bytes += skb->len;
|
|
|
|
napi_consume_skb(skb, false);
|
|
|
|
}
|
|
|
|
packets++;
|
2019-01-29 08:45:59 +08:00
|
|
|
}
|
xdp: change ndo_xdp_xmit API to support bulking
This patch change the API for ndo_xdp_xmit to support bulking
xdp_frames.
When kernel is compiled with CONFIG_RETPOLINE, XDP sees a huge slowdown.
Most of the slowdown is caused by DMA API indirect function calls, but
also the net_device->ndo_xdp_xmit() call.
Benchmarked patch with CONFIG_RETPOLINE, using xdp_redirect_map with
single flow/core test (CPU E5-1650 v4 @ 3.60GHz), showed
performance improved:
for driver ixgbe: 6,042,682 pps -> 6,853,768 pps = +811,086 pps
for driver i40e : 6,187,169 pps -> 6,724,519 pps = +537,350 pps
With frames avail as a bulk inside the driver ndo_xdp_xmit call,
further optimizations are possible, like bulk DMA-mapping for TX.
Testing without CONFIG_RETPOLINE show the same performance for
physical NIC drivers.
The virtual NIC driver tun sees a huge performance boost, as it can
avoid doing per frame producer locking, but instead amortize the
locking cost over the bulk.
V2: Fix compile errors reported by kbuild test robot <lkp@intel.com>
V4: Isolated ndo, driver changes and callers.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-24 22:46:12 +08:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
struct xdp_frame *xdpf = frames[i];
|
|
|
|
|
|
|
|
err = __virtnet_xdp_xmit_one(vi, sq, xdpf);
|
|
|
|
if (err) {
|
|
|
|
xdp_return_frame_rx_napi(xdpf);
|
|
|
|
drops++;
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 22:36:08 +08:00
|
|
|
ret = n - drops;
|
2018-05-31 17:00:08 +08:00
|
|
|
|
2018-07-23 22:36:09 +08:00
|
|
|
if (flags & XDP_XMIT_FLUSH) {
|
|
|
|
if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
|
|
|
|
kicks = 1;
|
|
|
|
}
|
2018-07-23 22:36:08 +08:00
|
|
|
out:
|
|
|
|
u64_stats_update_begin(&sq->stats.syncp);
|
2019-01-31 19:40:30 +08:00
|
|
|
sq->stats.bytes += bytes;
|
|
|
|
sq->stats.packets += packets;
|
2018-07-23 22:36:08 +08:00
|
|
|
sq->stats.xdp_tx += n;
|
|
|
|
sq->stats.xdp_tx_drops += drops;
|
2018-07-23 22:36:09 +08:00
|
|
|
sq->stats.kicks += kicks;
|
2018-07-23 22:36:08 +08:00
|
|
|
u64_stats_update_end(&sq->stats.syncp);
|
2018-05-31 17:00:08 +08:00
|
|
|
|
2018-07-23 22:36:08 +08:00
|
|
|
return ret;
|
2017-09-19 17:42:43 +08:00
|
|
|
}
|
|
|
|
|
2017-02-21 16:46:28 +08:00
|
|
|
static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
|
|
|
|
}
|
|
|
|
|
2017-07-19 16:54:48 +08:00
|
|
|
/* We copy the packet for XDP in the following cases:
|
|
|
|
*
|
|
|
|
* 1) Packet is scattered across multiple rx buffers.
|
|
|
|
* 2) Headroom space is insufficient.
|
|
|
|
*
|
|
|
|
* This is inefficient but it's a temporary condition that
|
|
|
|
* we hit right after XDP is enabled and until queue is refilled
|
|
|
|
* with large buffers with sufficient headroom - so it should affect
|
|
|
|
* at most queue size packets.
|
|
|
|
* Afterwards, the conditions to enable
|
|
|
|
* XDP should preclude the underlying device from sending packets
|
|
|
|
* across multiple buffers (num_buf > 1), and we make sure buffers
|
|
|
|
* have enough headroom.
|
|
|
|
*/
|
|
|
|
static struct page *xdp_linearize_page(struct receive_queue *rq,
|
|
|
|
u16 *num_buf,
|
|
|
|
struct page *p,
|
|
|
|
int offset,
|
|
|
|
int page_off,
|
|
|
|
unsigned int *len)
|
|
|
|
{
|
|
|
|
struct page *page = alloc_page(GFP_ATOMIC);
|
|
|
|
|
|
|
|
if (!page)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
|
|
|
|
page_off += *len;
|
|
|
|
|
|
|
|
while (--*num_buf) {
|
2018-03-02 17:29:14 +08:00
|
|
|
int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
|
2017-07-19 16:54:48 +08:00
|
|
|
unsigned int buflen;
|
|
|
|
void *buf;
|
|
|
|
int off;
|
|
|
|
|
|
|
|
buf = virtqueue_get_buf(rq->vq, &buflen);
|
|
|
|
if (unlikely(!buf))
|
|
|
|
goto err_buf;
|
|
|
|
|
|
|
|
p = virt_to_head_page(buf);
|
|
|
|
off = buf - page_address(p);
|
|
|
|
|
|
|
|
/* guard against a misconfigured or uncooperative backend that
|
|
|
|
* is sending packet larger than the MTU.
|
|
|
|
*/
|
2018-03-02 17:29:14 +08:00
|
|
|
if ((page_off + buflen + tailroom) > PAGE_SIZE) {
|
2017-07-19 16:54:48 +08:00
|
|
|
put_page(p);
|
|
|
|
goto err_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(page_address(page) + page_off,
|
|
|
|
page_address(p) + off, buflen);
|
|
|
|
page_off += buflen;
|
|
|
|
put_page(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Headroom does not contribute to packet length */
|
|
|
|
*len = page_off - VIRTIO_XDP_HEADROOM;
|
|
|
|
return page;
|
|
|
|
err_buf:
|
|
|
|
__free_pages(page, 0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-23 22:37:32 +08:00
|
|
|
static struct sk_buff *receive_small(struct net_device *dev,
|
|
|
|
struct virtnet_info *vi,
|
|
|
|
struct receive_queue *rq,
|
2017-07-19 16:54:47 +08:00
|
|
|
void *buf, void *ctx,
|
2017-09-19 17:42:43 +08:00
|
|
|
unsigned int len,
|
2018-07-23 22:36:04 +08:00
|
|
|
unsigned int *xdp_xmit,
|
2018-07-31 17:43:39 +08:00
|
|
|
struct virtnet_rq_stats *stats)
|
2013-11-28 19:30:59 +08:00
|
|
|
{
|
2017-02-21 16:46:28 +08:00
|
|
|
struct sk_buff *skb;
|
2016-12-23 22:37:32 +08:00
|
|
|
struct bpf_prog *xdp_prog;
|
2017-07-19 16:54:48 +08:00
|
|
|
unsigned int xdp_headroom = (unsigned long)ctx;
|
2017-02-21 16:46:28 +08:00
|
|
|
unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
|
|
|
|
unsigned int headroom = vi->hdr_len + header_offset;
|
|
|
|
unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
|
|
|
|
SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
|
2017-07-19 16:54:48 +08:00
|
|
|
struct page *page = virt_to_head_page(buf);
|
2018-02-20 21:32:15 +08:00
|
|
|
unsigned int delta = 0;
|
2017-07-19 16:54:48 +08:00
|
|
|
struct page *xdp_page;
|
2018-02-20 21:32:15 +08:00
|
|
|
int err;
|
2020-02-25 11:32:12 +08:00
|
|
|
unsigned int metasize = 0;
|
2018-02-20 21:32:15 +08:00
|
|
|
|
2014-10-24 21:55:57 +08:00
|
|
|
len -= vi->hdr_len;
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->bytes += len;
|
2013-11-28 19:30:59 +08:00
|
|
|
|
2016-12-23 22:37:32 +08:00
|
|
|
rcu_read_lock();
|
|
|
|
xdp_prog = rcu_dereference(rq->xdp_prog);
|
|
|
|
if (xdp_prog) {
|
2017-02-21 16:46:28 +08:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
|
2018-04-17 22:46:37 +08:00
|
|
|
struct xdp_frame *xdpf;
|
2017-02-03 11:15:01 +08:00
|
|
|
struct xdp_buff xdp;
|
2017-02-21 16:46:28 +08:00
|
|
|
void *orig_data;
|
2016-12-23 22:37:32 +08:00
|
|
|
u32 act;
|
|
|
|
|
2018-02-20 21:32:10 +08:00
|
|
|
if (unlikely(hdr->hdr.gso_type))
|
2016-12-23 22:37:32 +08:00
|
|
|
goto err_xdp;
|
2017-02-03 11:15:01 +08:00
|
|
|
|
2017-07-19 16:54:48 +08:00
|
|
|
if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
|
|
|
|
int offset = buf - page_address(page) + header_offset;
|
|
|
|
unsigned int tlen = len + vi->hdr_len;
|
|
|
|
u16 num_buf = 1;
|
|
|
|
|
|
|
|
xdp_headroom = virtnet_get_headroom(vi);
|
|
|
|
header_offset = VIRTNET_RX_PAD + xdp_headroom;
|
|
|
|
headroom = vi->hdr_len + header_offset;
|
|
|
|
buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
|
|
|
|
SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
|
|
|
|
xdp_page = xdp_linearize_page(rq, &num_buf, page,
|
|
|
|
offset, header_offset,
|
|
|
|
&tlen);
|
|
|
|
if (!xdp_page)
|
|
|
|
goto err_xdp;
|
|
|
|
|
|
|
|
buf = page_address(xdp_page);
|
|
|
|
put_page(page);
|
|
|
|
page = xdp_page;
|
|
|
|
}
|
|
|
|
|
2017-02-21 16:46:28 +08:00
|
|
|
xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
|
|
|
|
xdp.data = xdp.data_hard_start + xdp_headroom;
|
2017-02-03 11:15:01 +08:00
|
|
|
xdp.data_end = xdp.data + len;
|
2020-02-25 11:32:12 +08:00
|
|
|
xdp.data_meta = xdp.data;
|
2018-01-03 18:26:04 +08:00
|
|
|
xdp.rxq = &rq->xdp_rxq;
|
virtio_net: Add XDP frame size in two code paths
The virtio_net driver is running inside the guest-OS. There are two
XDP receive code-paths in virtio_net, namely receive_small() and
receive_mergeable(). The receive_big() function does not support XDP.
In receive_small() the frame size is available in buflen. The buffer
backing these frames are allocated in add_recvbuf_small() with same
size, except for the headroom, but tailroom have reserved room for
skb_shared_info. The headroom is encoded in ctx pointer as a value.
In receive_mergeable() the frame size is more dynamic. There are two
basic cases: (1) buffer size is based on a exponentially weighted
moving average (see DECLARE_EWMA) of packet length. Or (2) in case
virtnet_get_headroom() have any headroom then buffer size is
PAGE_SIZE. The ctx pointer is this time used for encoding two values;
the buffer len "truesize" and headroom. In case (1) if the rx buffer
size is underestimated, the packet will have been split over more
buffers (num_buf info in virtio_net_hdr_mrg_rxbuf placed in top of
buffer area). If that happens the XDP path does a xdp_linearize_page
operation.
V3: Adjust frame_sz in receive_mergeable() case, spotted by Jason Wang.
The code is really hard to follow, so some hints to reviewers.
The receive_mergeable() case gets frames that were allocated in
add_recvbuf_mergeable() which uses headroom=virtnet_get_headroom(),
and 'buf' ptr is advanced this headroom. The headroom can only
be 0 or VIRTIO_XDP_HEADROOM, as virtnet_get_headroom is really
simple:
static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
{
return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
}
As frame_sz is an offset size from xdp.data_hard_start, reviewers
should notice how this is calculated in receive_mergeable():
int offset = buf - page_address(page);
[...]
data = page_address(xdp_page) + offset;
xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
The calculated offset will always be VIRTIO_XDP_HEADROOM when
reaching this code. Thus, xdp.data_hard_start will be page-start
address plus vi->hdr_len. Given this xdp.frame_sz need to be
reduced with vi->hdr_len size.
IMHO a followup patch should cleanup this code to make it easier
to maintain and understand, but it is outside the scope of this
patchset.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Link: https://lore.kernel.org/bpf/158945344436.97035.9445115070189151680.stgit@firesoul
2020-05-14 18:50:44 +08:00
|
|
|
xdp.frame_sz = buflen;
|
2017-02-21 16:46:28 +08:00
|
|
|
orig_data = xdp.data;
|
2017-02-03 11:15:01 +08:00
|
|
|
act = bpf_prog_run_xdp(xdp_prog, &xdp);
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->xdp_packets++;
|
2017-02-03 11:15:01 +08:00
|
|
|
|
2016-12-23 22:37:32 +08:00
|
|
|
switch (act) {
|
|
|
|
case XDP_PASS:
|
2017-02-03 11:16:29 +08:00
|
|
|
/* Recalculate length in case bpf program changed it */
|
2017-02-21 16:46:28 +08:00
|
|
|
delta = orig_data - xdp.data;
|
2018-04-18 12:42:20 +08:00
|
|
|
len = xdp.data_end - xdp.data;
|
2020-02-25 11:32:12 +08:00
|
|
|
metasize = xdp.data - xdp.data_meta;
|
2016-12-23 22:37:32 +08:00
|
|
|
break;
|
|
|
|
case XDP_TX:
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->xdp_tx++;
|
2020-05-29 04:47:29 +08:00
|
|
|
xdpf = xdp_convert_buff_to_frame(&xdp);
|
2018-04-17 22:46:37 +08:00
|
|
|
if (unlikely(!xdpf))
|
|
|
|
goto err_xdp;
|
2018-07-31 17:43:38 +08:00
|
|
|
err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
|
|
|
|
if (unlikely(err < 0)) {
|
2017-02-03 11:15:01 +08:00
|
|
|
trace_xdp_exception(vi->dev, xdp_prog, act);
|
2018-02-20 21:32:15 +08:00
|
|
|
goto err_xdp;
|
|
|
|
}
|
2018-06-26 23:39:58 +08:00
|
|
|
*xdp_xmit |= VIRTIO_XDP_TX;
|
2017-09-19 17:42:43 +08:00
|
|
|
rcu_read_unlock();
|
|
|
|
goto xdp_xmit;
|
|
|
|
case XDP_REDIRECT:
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->xdp_redirects++;
|
2017-09-19 17:42:43 +08:00
|
|
|
err = xdp_do_redirect(dev, &xdp, xdp_prog);
|
2018-02-20 21:32:15 +08:00
|
|
|
if (err)
|
|
|
|
goto err_xdp;
|
2018-06-26 23:39:58 +08:00
|
|
|
*xdp_xmit |= VIRTIO_XDP_REDIR;
|
2016-12-23 22:37:32 +08:00
|
|
|
rcu_read_unlock();
|
|
|
|
goto xdp_xmit;
|
|
|
|
default:
|
2017-02-03 11:15:01 +08:00
|
|
|
bpf_warn_invalid_xdp_action(act);
|
2018-08-05 10:42:05 +08:00
|
|
|
/* fall through */
|
2017-02-03 11:15:01 +08:00
|
|
|
case XDP_ABORTED:
|
|
|
|
trace_xdp_exception(vi->dev, xdp_prog, act);
|
|
|
|
case XDP_DROP:
|
2016-12-23 22:37:32 +08:00
|
|
|
goto err_xdp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
2017-02-21 16:46:28 +08:00
|
|
|
skb = build_skb(buf, buflen);
|
|
|
|
if (!skb) {
|
2017-07-19 16:54:48 +08:00
|
|
|
put_page(page);
|
2017-02-21 16:46:28 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
skb_reserve(skb, headroom - delta);
|
2018-04-18 12:42:20 +08:00
|
|
|
skb_put(skb, len);
|
2020-02-25 11:32:11 +08:00
|
|
|
if (!xdp_prog) {
|
2017-02-21 16:46:28 +08:00
|
|
|
buf += header_offset;
|
|
|
|
memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
|
2020-02-25 11:32:11 +08:00
|
|
|
} /* keep zeroed vnet hdr since XDP is loaded */
|
2017-02-21 16:46:28 +08:00
|
|
|
|
2020-02-25 11:32:12 +08:00
|
|
|
if (metasize)
|
|
|
|
skb_metadata_set(skb, metasize);
|
|
|
|
|
2017-02-21 16:46:28 +08:00
|
|
|
err:
|
2013-11-28 19:30:59 +08:00
|
|
|
return skb;
|
2016-12-23 22:37:32 +08:00
|
|
|
|
|
|
|
err_xdp:
|
|
|
|
rcu_read_unlock();
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->xdp_drops++;
|
|
|
|
stats->drops++;
|
2017-07-19 16:54:48 +08:00
|
|
|
put_page(page);
|
2016-12-23 22:37:32 +08:00
|
|
|
xdp_xmit:
|
|
|
|
return NULL;
|
2013-11-28 19:30:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct sk_buff *receive_big(struct net_device *dev,
|
2014-10-24 05:12:10 +08:00
|
|
|
struct virtnet_info *vi,
|
2013-11-28 19:30:59 +08:00
|
|
|
struct receive_queue *rq,
|
|
|
|
void *buf,
|
2018-07-23 22:36:04 +08:00
|
|
|
unsigned int len,
|
2018-07-31 17:43:39 +08:00
|
|
|
struct virtnet_rq_stats *stats)
|
2013-11-28 19:30:59 +08:00
|
|
|
{
|
|
|
|
struct page *page = buf;
|
2020-02-25 11:32:12 +08:00
|
|
|
struct sk_buff *skb =
|
|
|
|
page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0);
|
2016-12-16 04:13:24 +08:00
|
|
|
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->bytes += len - vi->hdr_len;
|
2013-11-28 19:30:59 +08:00
|
|
|
if (unlikely(!skb))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
return skb;
|
|
|
|
|
|
|
|
err:
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->drops++;
|
2013-11-28 19:30:59 +08:00
|
|
|
give_pages(rq, page);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-28 19:30:55 +08:00
|
|
|
static struct sk_buff *receive_mergeable(struct net_device *dev,
|
2014-10-07 22:39:48 +08:00
|
|
|
struct virtnet_info *vi,
|
2013-11-28 19:30:55 +08:00
|
|
|
struct receive_queue *rq,
|
2017-03-07 03:29:47 +08:00
|
|
|
void *buf,
|
|
|
|
void *ctx,
|
2017-09-19 17:42:43 +08:00
|
|
|
unsigned int len,
|
2018-07-23 22:36:04 +08:00
|
|
|
unsigned int *xdp_xmit,
|
2018-07-31 17:43:39 +08:00
|
|
|
struct virtnet_rq_stats *stats)
|
2010-01-29 11:20:04 +08:00
|
|
|
{
|
2014-10-24 21:55:57 +08:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
|
|
|
|
u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
|
2013-11-28 19:30:55 +08:00
|
|
|
struct page *page = virt_to_head_page(buf);
|
|
|
|
int offset = buf - page_address(page);
|
2016-12-16 04:13:24 +08:00
|
|
|
struct sk_buff *head_skb, *curr_skb;
|
|
|
|
struct bpf_prog *xdp_prog;
|
virtio_net: Add XDP frame size in two code paths
The virtio_net driver is running inside the guest-OS. There are two
XDP receive code-paths in virtio_net, namely receive_small() and
receive_mergeable(). The receive_big() function does not support XDP.
In receive_small() the frame size is available in buflen. The buffer
backing these frames are allocated in add_recvbuf_small() with same
size, except for the headroom, but tailroom have reserved room for
skb_shared_info. The headroom is encoded in ctx pointer as a value.
In receive_mergeable() the frame size is more dynamic. There are two
basic cases: (1) buffer size is based on a exponentially weighted
moving average (see DECLARE_EWMA) of packet length. Or (2) in case
virtnet_get_headroom() have any headroom then buffer size is
PAGE_SIZE. The ctx pointer is this time used for encoding two values;
the buffer len "truesize" and headroom. In case (1) if the rx buffer
size is underestimated, the packet will have been split over more
buffers (num_buf info in virtio_net_hdr_mrg_rxbuf placed in top of
buffer area). If that happens the XDP path does a xdp_linearize_page
operation.
V3: Adjust frame_sz in receive_mergeable() case, spotted by Jason Wang.
The code is really hard to follow, so some hints to reviewers.
The receive_mergeable() case gets frames that were allocated in
add_recvbuf_mergeable() which uses headroom=virtnet_get_headroom(),
and 'buf' ptr is advanced this headroom. The headroom can only
be 0 or VIRTIO_XDP_HEADROOM, as virtnet_get_headroom is really
simple:
static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
{
return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
}
As frame_sz is an offset size from xdp.data_hard_start, reviewers
should notice how this is calculated in receive_mergeable():
int offset = buf - page_address(page);
[...]
data = page_address(xdp_page) + offset;
xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
The calculated offset will always be VIRTIO_XDP_HEADROOM when
reaching this code. Thus, xdp.data_hard_start will be page-start
address plus vi->hdr_len. Given this xdp.frame_sz need to be
reduced with vi->hdr_len size.
IMHO a followup patch should cleanup this code to make it easier
to maintain and understand, but it is outside the scope of this
patchset.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Link: https://lore.kernel.org/bpf/158945344436.97035.9445115070189151680.stgit@firesoul
2020-05-14 18:50:44 +08:00
|
|
|
unsigned int truesize = mergeable_ctx_to_truesize(ctx);
|
2017-07-19 16:54:48 +08:00
|
|
|
unsigned int headroom = mergeable_ctx_to_headroom(ctx);
|
2020-02-25 11:32:12 +08:00
|
|
|
unsigned int metasize = 0;
|
virtio_net: Add XDP frame size in two code paths
The virtio_net driver is running inside the guest-OS. There are two
XDP receive code-paths in virtio_net, namely receive_small() and
receive_mergeable(). The receive_big() function does not support XDP.
In receive_small() the frame size is available in buflen. The buffer
backing these frames are allocated in add_recvbuf_small() with same
size, except for the headroom, but tailroom have reserved room for
skb_shared_info. The headroom is encoded in ctx pointer as a value.
In receive_mergeable() the frame size is more dynamic. There are two
basic cases: (1) buffer size is based on a exponentially weighted
moving average (see DECLARE_EWMA) of packet length. Or (2) in case
virtnet_get_headroom() have any headroom then buffer size is
PAGE_SIZE. The ctx pointer is this time used for encoding two values;
the buffer len "truesize" and headroom. In case (1) if the rx buffer
size is underestimated, the packet will have been split over more
buffers (num_buf info in virtio_net_hdr_mrg_rxbuf placed in top of
buffer area). If that happens the XDP path does a xdp_linearize_page
operation.
V3: Adjust frame_sz in receive_mergeable() case, spotted by Jason Wang.
The code is really hard to follow, so some hints to reviewers.
The receive_mergeable() case gets frames that were allocated in
add_recvbuf_mergeable() which uses headroom=virtnet_get_headroom(),
and 'buf' ptr is advanced this headroom. The headroom can only
be 0 or VIRTIO_XDP_HEADROOM, as virtnet_get_headroom is really
simple:
static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
{
return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
}
As frame_sz is an offset size from xdp.data_hard_start, reviewers
should notice how this is calculated in receive_mergeable():
int offset = buf - page_address(page);
[...]
data = page_address(xdp_page) + offset;
xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
The calculated offset will always be VIRTIO_XDP_HEADROOM when
reaching this code. Thus, xdp.data_hard_start will be page-start
address plus vi->hdr_len. Given this xdp.frame_sz need to be
reduced with vi->hdr_len size.
IMHO a followup patch should cleanup this code to make it easier
to maintain and understand, but it is outside the scope of this
patchset.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Link: https://lore.kernel.org/bpf/158945344436.97035.9445115070189151680.stgit@firesoul
2020-05-14 18:50:44 +08:00
|
|
|
unsigned int frame_sz;
|
|
|
|
int err;
|
2016-12-16 04:13:24 +08:00
|
|
|
|
2016-12-16 04:14:13 +08:00
|
|
|
head_skb = NULL;
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->bytes += len - vi->hdr_len;
|
2016-12-16 04:14:13 +08:00
|
|
|
|
2016-12-16 04:13:24 +08:00
|
|
|
rcu_read_lock();
|
|
|
|
xdp_prog = rcu_dereference(rq->xdp_prog);
|
|
|
|
if (xdp_prog) {
|
2018-04-17 22:46:37 +08:00
|
|
|
struct xdp_frame *xdpf;
|
2016-12-16 04:14:36 +08:00
|
|
|
struct page *xdp_page;
|
2017-02-03 11:15:01 +08:00
|
|
|
struct xdp_buff xdp;
|
|
|
|
void *data;
|
2016-12-16 04:13:24 +08:00
|
|
|
u32 act;
|
|
|
|
|
2018-05-22 11:44:31 +08:00
|
|
|
/* Transient failure which in theory could occur if
|
|
|
|
* in-flight packets from before XDP was enabled reach
|
|
|
|
* the receive path after XDP is loaded.
|
|
|
|
*/
|
|
|
|
if (unlikely(hdr->hdr.gso_type))
|
|
|
|
goto err_xdp;
|
|
|
|
|
virtio_net: Add XDP frame size in two code paths
The virtio_net driver is running inside the guest-OS. There are two
XDP receive code-paths in virtio_net, namely receive_small() and
receive_mergeable(). The receive_big() function does not support XDP.
In receive_small() the frame size is available in buflen. The buffer
backing these frames are allocated in add_recvbuf_small() with same
size, except for the headroom, but tailroom have reserved room for
skb_shared_info. The headroom is encoded in ctx pointer as a value.
In receive_mergeable() the frame size is more dynamic. There are two
basic cases: (1) buffer size is based on a exponentially weighted
moving average (see DECLARE_EWMA) of packet length. Or (2) in case
virtnet_get_headroom() have any headroom then buffer size is
PAGE_SIZE. The ctx pointer is this time used for encoding two values;
the buffer len "truesize" and headroom. In case (1) if the rx buffer
size is underestimated, the packet will have been split over more
buffers (num_buf info in virtio_net_hdr_mrg_rxbuf placed in top of
buffer area). If that happens the XDP path does a xdp_linearize_page
operation.
V3: Adjust frame_sz in receive_mergeable() case, spotted by Jason Wang.
The code is really hard to follow, so some hints to reviewers.
The receive_mergeable() case gets frames that were allocated in
add_recvbuf_mergeable() which uses headroom=virtnet_get_headroom(),
and 'buf' ptr is advanced this headroom. The headroom can only
be 0 or VIRTIO_XDP_HEADROOM, as virtnet_get_headroom is really
simple:
static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
{
return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
}
As frame_sz is an offset size from xdp.data_hard_start, reviewers
should notice how this is calculated in receive_mergeable():
int offset = buf - page_address(page);
[...]
data = page_address(xdp_page) + offset;
xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
The calculated offset will always be VIRTIO_XDP_HEADROOM when
reaching this code. Thus, xdp.data_hard_start will be page-start
address plus vi->hdr_len. Given this xdp.frame_sz need to be
reduced with vi->hdr_len size.
IMHO a followup patch should cleanup this code to make it easier
to maintain and understand, but it is outside the scope of this
patchset.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Link: https://lore.kernel.org/bpf/158945344436.97035.9445115070189151680.stgit@firesoul
2020-05-14 18:50:44 +08:00
|
|
|
/* Buffers with headroom use PAGE_SIZE as alloc size,
|
|
|
|
* see add_recvbuf_mergeable() + get_mergeable_buf_len()
|
|
|
|
*/
|
|
|
|
frame_sz = headroom ? PAGE_SIZE : truesize;
|
|
|
|
|
2018-03-02 17:29:14 +08:00
|
|
|
/* This happens when rx buffer size is underestimated
|
|
|
|
* or headroom is not enough because of the buffer
|
|
|
|
* was refilled before XDP is set. This should only
|
|
|
|
* happen for the first several packets, so we don't
|
|
|
|
* care much about its performance.
|
|
|
|
*/
|
2017-07-19 16:54:48 +08:00
|
|
|
if (unlikely(num_buf > 1 ||
|
|
|
|
headroom < virtnet_get_headroom(vi))) {
|
2016-12-16 04:14:36 +08:00
|
|
|
/* linearize data for XDP */
|
2016-12-23 22:37:26 +08:00
|
|
|
xdp_page = xdp_linearize_page(rq, &num_buf,
|
2017-07-19 16:54:48 +08:00
|
|
|
page, offset,
|
|
|
|
VIRTIO_XDP_HEADROOM,
|
|
|
|
&len);
|
virtio_net: Add XDP frame size in two code paths
The virtio_net driver is running inside the guest-OS. There are two
XDP receive code-paths in virtio_net, namely receive_small() and
receive_mergeable(). The receive_big() function does not support XDP.
In receive_small() the frame size is available in buflen. The buffer
backing these frames are allocated in add_recvbuf_small() with same
size, except for the headroom, but tailroom have reserved room for
skb_shared_info. The headroom is encoded in ctx pointer as a value.
In receive_mergeable() the frame size is more dynamic. There are two
basic cases: (1) buffer size is based on a exponentially weighted
moving average (see DECLARE_EWMA) of packet length. Or (2) in case
virtnet_get_headroom() have any headroom then buffer size is
PAGE_SIZE. The ctx pointer is this time used for encoding two values;
the buffer len "truesize" and headroom. In case (1) if the rx buffer
size is underestimated, the packet will have been split over more
buffers (num_buf info in virtio_net_hdr_mrg_rxbuf placed in top of
buffer area). If that happens the XDP path does a xdp_linearize_page
operation.
V3: Adjust frame_sz in receive_mergeable() case, spotted by Jason Wang.
The code is really hard to follow, so some hints to reviewers.
The receive_mergeable() case gets frames that were allocated in
add_recvbuf_mergeable() which uses headroom=virtnet_get_headroom(),
and 'buf' ptr is advanced this headroom. The headroom can only
be 0 or VIRTIO_XDP_HEADROOM, as virtnet_get_headroom is really
simple:
static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
{
return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
}
As frame_sz is an offset size from xdp.data_hard_start, reviewers
should notice how this is calculated in receive_mergeable():
int offset = buf - page_address(page);
[...]
data = page_address(xdp_page) + offset;
xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
The calculated offset will always be VIRTIO_XDP_HEADROOM when
reaching this code. Thus, xdp.data_hard_start will be page-start
address plus vi->hdr_len. Given this xdp.frame_sz need to be
reduced with vi->hdr_len size.
IMHO a followup patch should cleanup this code to make it easier
to maintain and understand, but it is outside the scope of this
patchset.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Link: https://lore.kernel.org/bpf/158945344436.97035.9445115070189151680.stgit@firesoul
2020-05-14 18:50:44 +08:00
|
|
|
frame_sz = PAGE_SIZE;
|
|
|
|
|
2016-12-16 04:14:36 +08:00
|
|
|
if (!xdp_page)
|
|
|
|
goto err_xdp;
|
2017-02-03 11:16:29 +08:00
|
|
|
offset = VIRTIO_XDP_HEADROOM;
|
2016-12-16 04:14:36 +08:00
|
|
|
} else {
|
|
|
|
xdp_page = page;
|
2016-12-16 04:13:24 +08:00
|
|
|
}
|
|
|
|
|
2017-02-03 11:16:29 +08:00
|
|
|
/* Allow consuming headroom but reserve enough space to push
|
|
|
|
* the descriptor on if we get an XDP_TX return code.
|
|
|
|
*/
|
2017-02-03 11:15:01 +08:00
|
|
|
data = page_address(xdp_page) + offset;
|
2017-02-03 11:16:29 +08:00
|
|
|
xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
|
2017-02-03 11:15:01 +08:00
|
|
|
xdp.data = data + vi->hdr_len;
|
|
|
|
xdp.data_end = xdp.data + (len - vi->hdr_len);
|
2020-02-25 11:32:12 +08:00
|
|
|
xdp.data_meta = xdp.data;
|
2018-01-03 18:26:04 +08:00
|
|
|
xdp.rxq = &rq->xdp_rxq;
|
virtio_net: Add XDP frame size in two code paths
The virtio_net driver is running inside the guest-OS. There are two
XDP receive code-paths in virtio_net, namely receive_small() and
receive_mergeable(). The receive_big() function does not support XDP.
In receive_small() the frame size is available in buflen. The buffer
backing these frames are allocated in add_recvbuf_small() with same
size, except for the headroom, but tailroom have reserved room for
skb_shared_info. The headroom is encoded in ctx pointer as a value.
In receive_mergeable() the frame size is more dynamic. There are two
basic cases: (1) buffer size is based on a exponentially weighted
moving average (see DECLARE_EWMA) of packet length. Or (2) in case
virtnet_get_headroom() have any headroom then buffer size is
PAGE_SIZE. The ctx pointer is this time used for encoding two values;
the buffer len "truesize" and headroom. In case (1) if the rx buffer
size is underestimated, the packet will have been split over more
buffers (num_buf info in virtio_net_hdr_mrg_rxbuf placed in top of
buffer area). If that happens the XDP path does a xdp_linearize_page
operation.
V3: Adjust frame_sz in receive_mergeable() case, spotted by Jason Wang.
The code is really hard to follow, so some hints to reviewers.
The receive_mergeable() case gets frames that were allocated in
add_recvbuf_mergeable() which uses headroom=virtnet_get_headroom(),
and 'buf' ptr is advanced this headroom. The headroom can only
be 0 or VIRTIO_XDP_HEADROOM, as virtnet_get_headroom is really
simple:
static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
{
return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
}
As frame_sz is an offset size from xdp.data_hard_start, reviewers
should notice how this is calculated in receive_mergeable():
int offset = buf - page_address(page);
[...]
data = page_address(xdp_page) + offset;
xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
The calculated offset will always be VIRTIO_XDP_HEADROOM when
reaching this code. Thus, xdp.data_hard_start will be page-start
address plus vi->hdr_len. Given this xdp.frame_sz need to be
reduced with vi->hdr_len size.
IMHO a followup patch should cleanup this code to make it easier
to maintain and understand, but it is outside the scope of this
patchset.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Link: https://lore.kernel.org/bpf/158945344436.97035.9445115070189151680.stgit@firesoul
2020-05-14 18:50:44 +08:00
|
|
|
xdp.frame_sz = frame_sz - vi->hdr_len;
|
2018-01-03 18:26:04 +08:00
|
|
|
|
2017-02-03 11:15:01 +08:00
|
|
|
act = bpf_prog_run_xdp(xdp_prog, &xdp);
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->xdp_packets++;
|
2017-02-03 11:15:01 +08:00
|
|
|
|
2016-12-16 04:14:13 +08:00
|
|
|
switch (act) {
|
|
|
|
case XDP_PASS:
|
2020-02-25 11:32:12 +08:00
|
|
|
metasize = xdp.data - xdp.data_meta;
|
|
|
|
|
2017-02-03 11:16:29 +08:00
|
|
|
/* recalculate offset to account for any header
|
2020-02-25 11:32:12 +08:00
|
|
|
* adjustments and minus the metasize to copy the
|
|
|
|
* metadata in page_to_skb(). Note other cases do not
|
|
|
|
* build an skb and avoid using offset
|
2017-02-03 11:16:29 +08:00
|
|
|
*/
|
2020-02-25 11:32:12 +08:00
|
|
|
offset = xdp.data - page_address(xdp_page) -
|
|
|
|
vi->hdr_len - metasize;
|
2017-02-03 11:16:29 +08:00
|
|
|
|
2020-02-25 11:32:12 +08:00
|
|
|
/* recalculate len if xdp.data, xdp.data_end or
|
|
|
|
* xdp.data_meta were adjusted
|
2018-04-18 12:42:20 +08:00
|
|
|
*/
|
2020-02-25 11:32:12 +08:00
|
|
|
len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
|
2016-12-23 22:37:27 +08:00
|
|
|
/* We can only create skb based on xdp_page. */
|
|
|
|
if (unlikely(xdp_page != page)) {
|
|
|
|
rcu_read_unlock();
|
|
|
|
put_page(page);
|
2020-02-25 11:32:12 +08:00
|
|
|
head_skb = page_to_skb(vi, rq, xdp_page, offset,
|
|
|
|
len, PAGE_SIZE, false,
|
|
|
|
metasize);
|
2016-12-23 22:37:27 +08:00
|
|
|
return head_skb;
|
|
|
|
}
|
2016-12-16 04:14:13 +08:00
|
|
|
break;
|
|
|
|
case XDP_TX:
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->xdp_tx++;
|
2020-05-29 04:47:29 +08:00
|
|
|
xdpf = xdp_convert_buff_to_frame(&xdp);
|
2018-04-17 22:46:37 +08:00
|
|
|
if (unlikely(!xdpf))
|
|
|
|
goto err_xdp;
|
2018-07-31 17:43:38 +08:00
|
|
|
err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
|
|
|
|
if (unlikely(err < 0)) {
|
2017-02-03 11:15:01 +08:00
|
|
|
trace_xdp_exception(vi->dev, xdp_prog, act);
|
2018-02-20 21:32:15 +08:00
|
|
|
if (unlikely(xdp_page != page))
|
|
|
|
put_page(xdp_page);
|
|
|
|
goto err_xdp;
|
|
|
|
}
|
2018-06-26 23:39:58 +08:00
|
|
|
*xdp_xmit |= VIRTIO_XDP_TX;
|
2016-12-16 04:14:36 +08:00
|
|
|
if (unlikely(xdp_page != page))
|
2018-05-22 11:44:29 +08:00
|
|
|
put_page(page);
|
2016-12-16 04:14:13 +08:00
|
|
|
rcu_read_unlock();
|
|
|
|
goto xdp_xmit;
|
2018-03-02 17:29:14 +08:00
|
|
|
case XDP_REDIRECT:
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->xdp_redirects++;
|
2018-03-02 17:29:14 +08:00
|
|
|
err = xdp_do_redirect(dev, &xdp, xdp_prog);
|
|
|
|
if (err) {
|
|
|
|
if (unlikely(xdp_page != page))
|
|
|
|
put_page(xdp_page);
|
|
|
|
goto err_xdp;
|
|
|
|
}
|
2018-06-26 23:39:58 +08:00
|
|
|
*xdp_xmit |= VIRTIO_XDP_REDIR;
|
2018-03-02 17:29:14 +08:00
|
|
|
if (unlikely(xdp_page != page))
|
2018-05-22 11:44:28 +08:00
|
|
|
put_page(page);
|
2018-03-02 17:29:14 +08:00
|
|
|
rcu_read_unlock();
|
|
|
|
goto xdp_xmit;
|
2016-12-16 04:14:13 +08:00
|
|
|
default:
|
2017-02-03 11:15:01 +08:00
|
|
|
bpf_warn_invalid_xdp_action(act);
|
2018-08-05 10:42:05 +08:00
|
|
|
/* fall through */
|
2017-02-03 11:15:01 +08:00
|
|
|
case XDP_ABORTED:
|
|
|
|
trace_xdp_exception(vi->dev, xdp_prog, act);
|
2018-08-05 10:42:05 +08:00
|
|
|
/* fall through */
|
2017-02-03 11:15:01 +08:00
|
|
|
case XDP_DROP:
|
2016-12-16 04:14:36 +08:00
|
|
|
if (unlikely(xdp_page != page))
|
|
|
|
__free_pages(xdp_page, 0);
|
2016-12-16 04:13:24 +08:00
|
|
|
goto err_xdp;
|
2016-12-16 04:14:13 +08:00
|
|
|
}
|
2016-12-16 04:13:24 +08:00
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
2014-01-17 14:23:27 +08:00
|
|
|
|
2017-07-19 16:54:46 +08:00
|
|
|
if (unlikely(len > truesize)) {
|
2017-04-06 17:04:47 +08:00
|
|
|
pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
|
2017-03-07 03:29:47 +08:00
|
|
|
dev->name, len, (unsigned long)ctx);
|
|
|
|
dev->stats.rx_length_errors++;
|
|
|
|
goto err_skb;
|
|
|
|
}
|
2017-07-19 16:54:46 +08:00
|
|
|
|
2020-02-25 11:32:12 +08:00
|
|
|
head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
|
|
|
|
metasize);
|
2016-12-16 04:13:24 +08:00
|
|
|
curr_skb = head_skb;
|
2010-01-29 11:20:04 +08:00
|
|
|
|
2013-11-28 19:30:55 +08:00
|
|
|
if (unlikely(!curr_skb))
|
|
|
|
goto err_skb;
|
2010-01-29 11:20:04 +08:00
|
|
|
while (--num_buf) {
|
2013-11-28 19:30:55 +08:00
|
|
|
int num_skb_frags;
|
|
|
|
|
2017-03-07 03:29:47 +08:00
|
|
|
buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
|
2017-12-04 14:02:19 +08:00
|
|
|
if (unlikely(!buf)) {
|
2013-11-28 19:30:55 +08:00
|
|
|
pr_debug("%s: rx error: %d buffers out of %d missing\n",
|
2014-10-07 22:39:48 +08:00
|
|
|
dev->name, num_buf,
|
2014-10-24 21:55:57 +08:00
|
|
|
virtio16_to_cpu(vi->vdev,
|
|
|
|
hdr->num_buffers));
|
2013-11-28 19:30:55 +08:00
|
|
|
dev->stats.rx_length_errors++;
|
|
|
|
goto err_buf;
|
2008-11-17 14:41:34 +08:00
|
|
|
}
|
2013-11-28 19:30:55 +08:00
|
|
|
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->bytes += len;
|
2013-11-28 19:30:55 +08:00
|
|
|
page = virt_to_head_page(buf);
|
2017-07-19 16:54:46 +08:00
|
|
|
|
|
|
|
truesize = mergeable_ctx_to_truesize(ctx);
|
|
|
|
if (unlikely(len > truesize)) {
|
2017-04-06 17:04:47 +08:00
|
|
|
pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
|
2017-03-07 03:29:47 +08:00
|
|
|
dev->name, len, (unsigned long)ctx);
|
|
|
|
dev->stats.rx_length_errors++;
|
|
|
|
goto err_skb;
|
|
|
|
}
|
2013-11-28 19:30:55 +08:00
|
|
|
|
|
|
|
num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
|
2013-10-29 06:44:18 +08:00
|
|
|
if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
|
|
|
|
struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
|
2013-11-28 19:30:55 +08:00
|
|
|
|
|
|
|
if (unlikely(!nskb))
|
|
|
|
goto err_skb;
|
2013-10-29 06:44:18 +08:00
|
|
|
if (curr_skb == head_skb)
|
|
|
|
skb_shinfo(curr_skb)->frag_list = nskb;
|
|
|
|
else
|
|
|
|
curr_skb->next = nskb;
|
|
|
|
curr_skb = nskb;
|
|
|
|
head_skb->truesize += nskb->truesize;
|
|
|
|
num_skb_frags = 0;
|
|
|
|
}
|
|
|
|
if (curr_skb != head_skb) {
|
|
|
|
head_skb->data_len += len;
|
|
|
|
head_skb->len += len;
|
2014-01-17 14:23:26 +08:00
|
|
|
head_skb->truesize += truesize;
|
2013-10-29 06:44:18 +08:00
|
|
|
}
|
2013-11-28 19:30:55 +08:00
|
|
|
offset = buf - page_address(page);
|
2013-11-01 14:07:48 +08:00
|
|
|
if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
|
|
|
|
put_page(page);
|
|
|
|
skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
|
2014-01-17 14:23:26 +08:00
|
|
|
len, truesize);
|
2013-11-01 14:07:48 +08:00
|
|
|
} else {
|
|
|
|
skb_add_rx_frag(curr_skb, num_skb_frags, page,
|
2014-01-17 14:23:26 +08:00
|
|
|
offset, len, truesize);
|
2013-11-01 14:07:48 +08:00
|
|
|
}
|
2013-11-28 19:30:55 +08:00
|
|
|
}
|
|
|
|
|
2015-08-19 15:48:40 +08:00
|
|
|
ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
|
2013-11-28 19:30:55 +08:00
|
|
|
return head_skb;
|
|
|
|
|
2016-12-16 04:13:24 +08:00
|
|
|
err_xdp:
|
|
|
|
rcu_read_unlock();
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->xdp_drops++;
|
2013-11-28 19:30:55 +08:00
|
|
|
err_skb:
|
|
|
|
put_page(page);
|
2018-05-22 11:44:30 +08:00
|
|
|
while (num_buf-- > 1) {
|
2017-03-07 03:29:47 +08:00
|
|
|
buf = virtqueue_get_buf(rq->vq, &len);
|
|
|
|
if (unlikely(!buf)) {
|
2013-11-28 19:30:55 +08:00
|
|
|
pr_debug("%s: rx error: %d buffers missing\n",
|
|
|
|
dev->name, num_buf);
|
|
|
|
dev->stats.rx_length_errors++;
|
|
|
|
break;
|
|
|
|
}
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->bytes += len;
|
2017-03-07 03:29:47 +08:00
|
|
|
page = virt_to_head_page(buf);
|
2013-11-28 19:30:55 +08:00
|
|
|
put_page(page);
|
2010-01-29 11:20:04 +08:00
|
|
|
}
|
2013-11-28 19:30:55 +08:00
|
|
|
err_buf:
|
2018-07-31 17:43:39 +08:00
|
|
|
stats->drops++;
|
2013-11-28 19:30:55 +08:00
|
|
|
dev_kfree_skb(head_skb);
|
2016-12-16 04:14:13 +08:00
|
|
|
xdp_xmit:
|
2013-11-28 19:30:55 +08:00
|
|
|
return NULL;
|
2010-01-29 11:20:04 +08:00
|
|
|
}
|
|
|
|
|
2018-07-23 22:36:04 +08:00
|
|
|
static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
|
|
|
|
void *buf, unsigned int len, void **ctx,
|
2018-07-23 22:36:05 +08:00
|
|
|
unsigned int *xdp_xmit,
|
2018-07-31 17:43:39 +08:00
|
|
|
struct virtnet_rq_stats *stats)
|
2010-01-29 11:20:04 +08:00
|
|
|
{
|
2012-12-07 15:04:55 +08:00
|
|
|
struct net_device *dev = vi->dev;
|
2010-01-29 11:20:04 +08:00
|
|
|
struct sk_buff *skb;
|
2014-10-24 21:55:57 +08:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2014-10-24 05:22:11 +08:00
|
|
|
if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
|
2010-01-29 11:20:04 +08:00
|
|
|
pr_debug("%s: short packet %i\n", dev->name, len);
|
|
|
|
dev->stats.rx_length_errors++;
|
2014-01-17 14:23:27 +08:00
|
|
|
if (vi->mergeable_rx_bufs) {
|
2017-03-07 03:29:47 +08:00
|
|
|
put_page(virt_to_head_page(buf));
|
2014-01-17 14:23:27 +08:00
|
|
|
} else if (vi->big_packets) {
|
2013-12-06 05:14:05 +08:00
|
|
|
give_pages(rq, buf);
|
2014-01-17 14:23:27 +08:00
|
|
|
} else {
|
2017-02-21 16:46:28 +08:00
|
|
|
put_page(virt_to_head_page(buf));
|
2014-01-17 14:23:27 +08:00
|
|
|
}
|
2018-07-23 22:36:04 +08:00
|
|
|
return;
|
2010-01-29 11:20:04 +08:00
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2013-11-28 19:30:59 +08:00
|
|
|
if (vi->mergeable_rx_bufs)
|
2018-07-23 22:36:04 +08:00
|
|
|
skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
|
2018-07-23 22:36:05 +08:00
|
|
|
stats);
|
2013-11-28 19:30:59 +08:00
|
|
|
else if (vi->big_packets)
|
2018-07-23 22:36:05 +08:00
|
|
|
skb = receive_big(dev, vi, rq, buf, len, stats);
|
2013-11-28 19:30:59 +08:00
|
|
|
else
|
2018-07-23 22:36:05 +08:00
|
|
|
skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
|
2013-11-28 19:30:59 +08:00
|
|
|
|
|
|
|
if (unlikely(!skb))
|
2018-07-23 22:36:04 +08:00
|
|
|
return;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
hdr = skb_vnet_hdr(skb);
|
2011-06-15 14:36:29 +08:00
|
|
|
|
2016-06-08 21:09:21 +08:00
|
|
|
if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
|
2011-06-10 08:56:17 +08:00
|
|
|
skb->ip_summed = CHECKSUM_UNNECESSARY;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2016-06-08 21:09:21 +08:00
|
|
|
if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
|
|
|
|
virtio_is_little_endian(vi->vdev))) {
|
|
|
|
net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
|
|
|
|
dev->name, hdr->hdr.gso_type,
|
|
|
|
hdr->hdr.gso_size);
|
|
|
|
goto frame_err;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2019-01-18 09:08:53 +08:00
|
|
|
skb_record_rx_queue(skb, vq2rxq(rq->vq));
|
2016-06-14 13:29:38 +08:00
|
|
|
skb->protocol = eth_type_trans(skb, dev);
|
|
|
|
pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
|
|
|
|
ntohs(skb->protocol), skb->len, skb->pkt_type);
|
|
|
|
|
2015-08-01 00:25:17 +08:00
|
|
|
napi_gro_receive(&rq->napi, skb);
|
2018-07-23 22:36:04 +08:00
|
|
|
return;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
|
|
|
frame_err:
|
|
|
|
dev->stats.rx_frame_errors++;
|
|
|
|
dev_kfree_skb(skb);
|
|
|
|
}
|
|
|
|
|
2017-07-19 16:54:47 +08:00
|
|
|
/* Unlike mergeable buffers, all buffers are allocated to the
|
|
|
|
* same size, except for the headroom. For this reason we do
|
|
|
|
* not need to use mergeable_len_to_ctx here - it is enough
|
|
|
|
* to store the headroom as the context ignoring the truesize.
|
|
|
|
*/
|
2014-10-24 05:12:10 +08:00
|
|
|
static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
|
|
|
|
gfp_t gfp)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2017-02-21 16:46:28 +08:00
|
|
|
struct page_frag *alloc_frag = &rq->alloc_frag;
|
|
|
|
char *buf;
|
2017-02-03 11:16:29 +08:00
|
|
|
unsigned int xdp_headroom = virtnet_get_headroom(vi);
|
2017-07-19 16:54:47 +08:00
|
|
|
void *ctx = (void *)(unsigned long)xdp_headroom;
|
2017-02-21 16:46:28 +08:00
|
|
|
int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
|
2010-01-29 11:20:04 +08:00
|
|
|
int err;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2017-02-21 16:46:28 +08:00
|
|
|
len = SKB_DATA_ALIGN(len) +
|
|
|
|
SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
|
|
|
|
if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
|
2010-01-29 11:20:04 +08:00
|
|
|
return -ENOMEM;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2017-02-21 16:46:28 +08:00
|
|
|
buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
|
|
|
|
get_page(alloc_frag->page);
|
|
|
|
alloc_frag->offset += len;
|
|
|
|
sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
|
|
|
|
vi->hdr_len + GOOD_PACKET_LEN);
|
2017-07-19 16:54:47 +08:00
|
|
|
err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (err < 0)
|
2017-02-21 16:46:28 +08:00
|
|
|
put_page(virt_to_head_page(buf));
|
2010-01-29 11:20:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
2008-04-18 11:24:27 +08:00
|
|
|
|
2014-10-24 21:55:57 +08:00
|
|
|
static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
|
|
|
|
gfp_t gfp)
|
2010-01-29 11:20:04 +08:00
|
|
|
{
|
|
|
|
struct page *first, *list = NULL;
|
|
|
|
char *p;
|
|
|
|
int i, err, offset;
|
|
|
|
|
2014-09-11 08:47:36 +08:00
|
|
|
sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
|
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
|
2010-01-29 11:20:04 +08:00
|
|
|
for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
|
2012-12-07 15:04:55 +08:00
|
|
|
first = get_a_page(rq, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (!first) {
|
|
|
|
if (list)
|
2012-12-07 15:04:55 +08:00
|
|
|
give_pages(rq, list);
|
2010-01-29 11:20:04 +08:00
|
|
|
return -ENOMEM;
|
2008-04-18 11:24:27 +08:00
|
|
|
}
|
2012-12-07 15:04:55 +08:00
|
|
|
sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
|
2008-04-18 11:24:27 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
/* chain new page in list head to match sg */
|
|
|
|
first->private = (unsigned long)list;
|
|
|
|
list = first;
|
|
|
|
}
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
first = get_a_page(rq, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (!first) {
|
2012-12-07 15:04:55 +08:00
|
|
|
give_pages(rq, list);
|
2010-01-29 11:20:04 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
p = page_address(first);
|
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
/* rq->sg[0], rq->sg[1] share the same page */
|
2014-10-24 21:55:57 +08:00
|
|
|
/* a separated rq->sg[0] for header - required in case !any_header_sg */
|
|
|
|
sg_set_buf(&rq->sg[0], p, vi->hdr_len);
|
2010-01-29 11:20:04 +08:00
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
/* rq->sg[1] for data packet, from offset */
|
2010-01-29 11:20:04 +08:00
|
|
|
offset = sizeof(struct padded_vnet_hdr);
|
2012-12-07 15:04:55 +08:00
|
|
|
sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
|
2010-01-29 11:20:04 +08:00
|
|
|
|
|
|
|
/* chain first in list head */
|
|
|
|
first->private = (unsigned long)list;
|
2013-03-20 13:14:28 +08:00
|
|
|
err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
|
|
|
|
first, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (err < 0)
|
2012-12-07 15:04:55 +08:00
|
|
|
give_pages(rq, first);
|
2010-01-29 11:20:04 +08:00
|
|
|
|
|
|
|
return err;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:21:21 +08:00
|
|
|
static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
|
2018-03-02 17:29:14 +08:00
|
|
|
struct ewma_pkt_len *avg_pkt_len,
|
|
|
|
unsigned int room)
|
2008-11-17 14:41:34 +08:00
|
|
|
{
|
2014-01-17 14:23:27 +08:00
|
|
|
const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
|
2014-01-17 14:23:30 +08:00
|
|
|
unsigned int len;
|
|
|
|
|
2018-03-02 17:29:14 +08:00
|
|
|
if (room)
|
|
|
|
return PAGE_SIZE - room;
|
|
|
|
|
|
|
|
len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
|
2017-06-02 22:54:33 +08:00
|
|
|
rq->min_buf_len, PAGE_SIZE - hdr_len);
|
2018-03-02 17:29:14 +08:00
|
|
|
|
2017-03-07 04:21:35 +08:00
|
|
|
return ALIGN(len, L1_CACHE_BYTES);
|
2014-01-17 14:23:30 +08:00
|
|
|
}
|
|
|
|
|
2017-02-03 11:16:29 +08:00
|
|
|
static int add_recvbuf_mergeable(struct virtnet_info *vi,
|
|
|
|
struct receive_queue *rq, gfp_t gfp)
|
2014-01-17 14:23:30 +08:00
|
|
|
{
|
2014-01-17 14:23:26 +08:00
|
|
|
struct page_frag *alloc_frag = &rq->alloc_frag;
|
2017-02-03 11:16:29 +08:00
|
|
|
unsigned int headroom = virtnet_get_headroom(vi);
|
2018-03-02 17:29:14 +08:00
|
|
|
unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
|
|
|
|
unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
|
2014-01-17 14:23:26 +08:00
|
|
|
char *buf;
|
2017-03-07 03:29:47 +08:00
|
|
|
void *ctx;
|
2008-11-17 14:41:34 +08:00
|
|
|
int err;
|
2014-01-17 14:23:26 +08:00
|
|
|
unsigned int len, hole;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2018-03-02 17:29:14 +08:00
|
|
|
/* Extra tailroom is needed to satisfy XDP's assumption. This
|
|
|
|
* means rx frags coalescing won't work, but consider we've
|
|
|
|
* disabled GSO for XDP, it won't be a big issue.
|
|
|
|
*/
|
|
|
|
len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
|
|
|
|
if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
|
2010-01-29 11:20:04 +08:00
|
|
|
return -ENOMEM;
|
2014-01-17 14:23:27 +08:00
|
|
|
|
2014-01-17 14:23:26 +08:00
|
|
|
buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
|
2017-02-03 11:16:29 +08:00
|
|
|
buf += headroom; /* advance address leaving hole at front of pkt */
|
2014-01-17 14:23:26 +08:00
|
|
|
get_page(alloc_frag->page);
|
2018-03-02 17:29:14 +08:00
|
|
|
alloc_frag->offset += len + room;
|
2014-01-17 14:23:26 +08:00
|
|
|
hole = alloc_frag->size - alloc_frag->offset;
|
2018-03-02 17:29:14 +08:00
|
|
|
if (hole < len + room) {
|
2014-01-17 14:23:27 +08:00
|
|
|
/* To avoid internal fragmentation, if there is very likely not
|
|
|
|
* enough space for another buffer, add the remaining space to
|
2017-08-01 02:49:49 +08:00
|
|
|
* the current buffer.
|
2014-01-17 14:23:27 +08:00
|
|
|
*/
|
2014-01-17 14:23:26 +08:00
|
|
|
len += hole;
|
|
|
|
alloc_frag->offset += hole;
|
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2014-01-17 14:23:26 +08:00
|
|
|
sg_init_one(rq->sg, buf, len);
|
2017-08-02 01:07:50 +08:00
|
|
|
ctx = mergeable_len_to_ctx(len, headroom);
|
2017-03-07 03:29:47 +08:00
|
|
|
err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (err < 0)
|
2013-10-29 06:44:18 +08:00
|
|
|
put_page(virt_to_head_page(buf));
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2011-12-29 08:42:38 +08:00
|
|
|
/*
|
|
|
|
* Returns false if we couldn't fill entirely (OOM).
|
|
|
|
*
|
|
|
|
* Normally run in the receive path, but can also be run from ndo_open
|
|
|
|
* before we're receiving packets, or from refill_work which is
|
|
|
|
* careful to disable receiving (using napi_disable).
|
|
|
|
*/
|
2014-10-24 05:12:10 +08:00
|
|
|
static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
|
|
|
|
gfp_t gfp)
|
2010-01-29 11:20:04 +08:00
|
|
|
{
|
|
|
|
int err;
|
2010-07-03 00:32:55 +08:00
|
|
|
bool oom;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
do {
|
|
|
|
if (vi->mergeable_rx_bufs)
|
2017-02-03 11:16:29 +08:00
|
|
|
err = add_recvbuf_mergeable(vi, rq, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
else if (vi->big_packets)
|
2014-10-24 21:55:57 +08:00
|
|
|
err = add_recvbuf_big(vi, rq, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
else
|
2014-10-24 05:12:10 +08:00
|
|
|
err = add_recvbuf_small(vi, rq, gfp);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-07-03 00:32:55 +08:00
|
|
|
oom = err == -ENOMEM;
|
2012-10-16 21:26:14 +08:00
|
|
|
if (err)
|
2008-11-17 14:41:34 +08:00
|
|
|
break;
|
2012-12-21 00:37:04 +08:00
|
|
|
} while (rq->vq->num_free);
|
2018-07-23 22:36:09 +08:00
|
|
|
if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
|
2020-05-07 15:25:56 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
|
2018-07-31 17:43:39 +08:00
|
|
|
rq->stats.kicks++;
|
2020-05-07 15:25:56 +08:00
|
|
|
u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
|
2018-07-23 22:36:09 +08:00
|
|
|
}
|
|
|
|
|
2009-08-27 03:22:32 +08:00
|
|
|
return !oom;
|
2008-11-17 14:41:34 +08:00
|
|
|
}
|
|
|
|
|
2008-02-05 12:49:57 +08:00
|
|
|
static void skb_recv_done(struct virtqueue *rvq)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = rvq->vdev->priv;
|
2012-12-07 15:04:56 +08:00
|
|
|
struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
|
2012-12-07 15:04:55 +08:00
|
|
|
|
2017-04-25 01:49:26 +08:00
|
|
|
virtqueue_napi_schedule(&rq->napi, rvq);
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2017-04-25 01:49:26 +08:00
|
|
|
static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
|
2011-02-11 03:03:31 +08:00
|
|
|
{
|
2017-04-25 01:49:26 +08:00
|
|
|
napi_enable(napi);
|
2011-02-11 03:03:31 +08:00
|
|
|
|
|
|
|
/* If all buffers were filled by other side before we napi_enabled, we
|
2017-04-25 01:49:26 +08:00
|
|
|
* won't get another interrupt, so process any outstanding packets now.
|
|
|
|
* Call local_bh_enable after to trigger softIRQ processing.
|
|
|
|
*/
|
|
|
|
local_bh_disable();
|
|
|
|
virtqueue_napi_schedule(napi, vq);
|
|
|
|
local_bh_enable();
|
2011-02-11 03:03:31 +08:00
|
|
|
}
|
|
|
|
|
2017-04-25 01:49:27 +08:00
|
|
|
static void virtnet_napi_tx_enable(struct virtnet_info *vi,
|
|
|
|
struct virtqueue *vq,
|
|
|
|
struct napi_struct *napi)
|
|
|
|
{
|
|
|
|
if (!napi->weight)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Tx napi touches cachelines on the cpu handling tx interrupts. Only
|
|
|
|
* enable the feature if this is likely affine with the transmit path.
|
|
|
|
*/
|
|
|
|
if (!vi->affinity_hint_set) {
|
|
|
|
napi->weight = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virtnet_napi_enable(vq, napi);
|
|
|
|
}
|
|
|
|
|
2017-04-26 03:59:17 +08:00
|
|
|
static void virtnet_napi_tx_disable(struct napi_struct *napi)
|
|
|
|
{
|
|
|
|
if (napi->weight)
|
|
|
|
napi_disable(napi);
|
|
|
|
}
|
|
|
|
|
2009-08-27 03:22:32 +08:00
|
|
|
static void refill_work(struct work_struct *work)
|
|
|
|
{
|
2012-12-07 15:04:55 +08:00
|
|
|
struct virtnet_info *vi =
|
|
|
|
container_of(work, struct virtnet_info, refill.work);
|
2009-08-27 03:22:32 +08:00
|
|
|
bool still_empty;
|
2012-12-07 15:04:56 +08:00
|
|
|
int i;
|
|
|
|
|
2013-04-29 10:30:08 +08:00
|
|
|
for (i = 0; i < vi->curr_queue_pairs; i++) {
|
2012-12-07 15:04:56 +08:00
|
|
|
struct receive_queue *rq = &vi->rq[i];
|
2009-08-27 03:22:32 +08:00
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
napi_disable(&rq->napi);
|
2014-10-24 05:12:10 +08:00
|
|
|
still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
|
2017-04-25 01:49:26 +08:00
|
|
|
virtnet_napi_enable(rq->vq, &rq->napi);
|
2009-08-27 03:22:32 +08:00
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
/* In theory, this can happen: if we don't get any buffers in
|
|
|
|
* we will *never* try to fill again.
|
|
|
|
*/
|
|
|
|
if (still_empty)
|
|
|
|
schedule_delayed_work(&vi->refill, HZ/2);
|
|
|
|
}
|
2009-08-27 03:22:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-26 23:39:58 +08:00
|
|
|
static int virtnet_receive(struct receive_queue *rq, int budget,
|
|
|
|
unsigned int *xdp_xmit)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2012-12-07 15:04:55 +08:00
|
|
|
struct virtnet_info *vi = rq->vq->vdev->priv;
|
2018-07-31 17:43:39 +08:00
|
|
|
struct virtnet_rq_stats stats = {};
|
2018-07-23 22:36:05 +08:00
|
|
|
unsigned int len;
|
2010-01-29 11:20:04 +08:00
|
|
|
void *buf;
|
2018-07-23 22:36:05 +08:00
|
|
|
int i;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2017-07-19 16:54:47 +08:00
|
|
|
if (!vi->big_packets || vi->mergeable_rx_bufs) {
|
2017-03-07 03:29:47 +08:00
|
|
|
void *ctx;
|
|
|
|
|
2018-07-31 17:43:39 +08:00
|
|
|
while (stats.packets < budget &&
|
2017-03-07 03:29:47 +08:00
|
|
|
(buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
|
2018-07-23 22:36:05 +08:00
|
|
|
receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
|
2018-07-31 17:43:39 +08:00
|
|
|
stats.packets++;
|
2017-03-07 03:29:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-31 17:43:39 +08:00
|
|
|
while (stats.packets < budget &&
|
2017-03-07 03:29:47 +08:00
|
|
|
(buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
|
2018-07-23 22:36:05 +08:00
|
|
|
receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
|
2018-07-31 17:43:39 +08:00
|
|
|
stats.packets++;
|
2017-03-07 03:29:47 +08:00
|
|
|
}
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2019-08-20 10:51:23 +08:00
|
|
|
if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
|
2014-10-24 05:12:10 +08:00
|
|
|
if (!try_fill_recv(vi, rq, GFP_ATOMIC))
|
2012-08-21 05:51:24 +08:00
|
|
|
schedule_delayed_work(&vi->refill, 0);
|
2009-08-27 03:22:32 +08:00
|
|
|
}
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2018-01-17 14:38:25 +08:00
|
|
|
u64_stats_update_begin(&rq->stats.syncp);
|
2018-07-23 22:36:05 +08:00
|
|
|
for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
|
|
|
|
size_t offset = virtnet_rq_stats_desc[i].offset;
|
|
|
|
u64 *item;
|
|
|
|
|
2018-07-31 17:43:39 +08:00
|
|
|
item = (u64 *)((u8 *)&rq->stats + offset);
|
|
|
|
*item += *(u64 *)((u8 *)&stats + offset);
|
2018-07-23 22:36:05 +08:00
|
|
|
}
|
2018-01-17 14:38:25 +08:00
|
|
|
u64_stats_update_end(&rq->stats.syncp);
|
2017-02-17 11:33:09 +08:00
|
|
|
|
2018-07-31 17:43:39 +08:00
|
|
|
return stats.packets;
|
2014-07-23 16:33:54 +08:00
|
|
|
}
|
|
|
|
|
2019-01-18 12:20:07 +08:00
|
|
|
static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
|
2017-04-25 01:49:28 +08:00
|
|
|
{
|
|
|
|
unsigned int len;
|
|
|
|
unsigned int packets = 0;
|
|
|
|
unsigned int bytes = 0;
|
2019-01-29 08:45:59 +08:00
|
|
|
void *ptr;
|
2017-04-25 01:49:28 +08:00
|
|
|
|
2019-01-29 08:45:59 +08:00
|
|
|
while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
|
|
|
|
if (likely(!is_xdp_frame(ptr))) {
|
|
|
|
struct sk_buff *skb = ptr;
|
2017-04-25 01:49:28 +08:00
|
|
|
|
2019-01-29 08:45:59 +08:00
|
|
|
pr_debug("Sent skb %p\n", skb);
|
2017-04-25 01:49:28 +08:00
|
|
|
|
2019-01-29 08:45:59 +08:00
|
|
|
bytes += skb->len;
|
|
|
|
napi_consume_skb(skb, in_napi);
|
|
|
|
} else {
|
|
|
|
struct xdp_frame *frame = ptr_to_xdp(ptr);
|
2017-04-25 01:49:28 +08:00
|
|
|
|
2019-01-29 08:45:59 +08:00
|
|
|
bytes += frame->len;
|
|
|
|
xdp_return_frame(frame);
|
|
|
|
}
|
|
|
|
packets++;
|
2017-04-25 01:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Avoid overhead when no packets have been processed
|
|
|
|
* happens when called speculatively from start_xmit.
|
|
|
|
*/
|
|
|
|
if (!packets)
|
|
|
|
return;
|
|
|
|
|
2018-01-17 14:38:25 +08:00
|
|
|
u64_stats_update_begin(&sq->stats.syncp);
|
|
|
|
sq->stats.bytes += bytes;
|
|
|
|
sq->stats.packets += packets;
|
|
|
|
u64_stats_update_end(&sq->stats.syncp);
|
2017-04-25 01:49:28 +08:00
|
|
|
}
|
|
|
|
|
virtio_net: Don't call free_old_xmit_skbs for xdp_frames
When napi_tx is enabled, virtnet_poll_cleantx() called
free_old_xmit_skbs() even for xdp send queue.
This is bogus since the queue has xdp_frames, not sk_buffs, thus mangled
device tx bytes counters because skb->len is meaningless value, and even
triggered oops due to general protection fault on freeing them.
Since xdp send queues do not aquire locks, old xdp_frames should be
freed only in virtnet_xdp_xmit(), so just skip free_old_xmit_skbs() for
xdp send queues.
Similarly virtnet_poll_tx() called free_old_xmit_skbs(). This NAPI
handler is called even without calling start_xmit() because cb for tx is
by default enabled. Once the handler is called, it enabled the cb again,
and then the handler would be called again. We don't need this handler
for XDP, so don't enable cb as well as not calling free_old_xmit_skbs().
Also, we need to disable tx NAPI when disabling XDP, so
virtnet_poll_tx() can safely access curr_queue_pairs and
xdp_queue_pairs, which are not atomically updated while disabling XDP.
Fixes: b92f1e6751a6 ("virtio-net: transmit napi")
Fixes: 7b0411ef4aa6 ("virtio-net: clean tx descriptors from rx napi")
Signed-off-by: Toshiaki Makita <makita.toshiaki@lab.ntt.co.jp>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-01-29 08:45:54 +08:00
|
|
|
static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
|
|
|
|
{
|
|
|
|
if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
|
|
|
|
return false;
|
|
|
|
else if (q < vi->curr_queue_pairs)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-25 01:49:29 +08:00
|
|
|
static void virtnet_poll_cleantx(struct receive_queue *rq)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = rq->vq->vdev->priv;
|
|
|
|
unsigned int index = vq2rxq(rq->vq);
|
|
|
|
struct send_queue *sq = &vi->sq[index];
|
|
|
|
struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
|
|
|
|
|
virtio_net: Don't call free_old_xmit_skbs for xdp_frames
When napi_tx is enabled, virtnet_poll_cleantx() called
free_old_xmit_skbs() even for xdp send queue.
This is bogus since the queue has xdp_frames, not sk_buffs, thus mangled
device tx bytes counters because skb->len is meaningless value, and even
triggered oops due to general protection fault on freeing them.
Since xdp send queues do not aquire locks, old xdp_frames should be
freed only in virtnet_xdp_xmit(), so just skip free_old_xmit_skbs() for
xdp send queues.
Similarly virtnet_poll_tx() called free_old_xmit_skbs(). This NAPI
handler is called even without calling start_xmit() because cb for tx is
by default enabled. Once the handler is called, it enabled the cb again,
and then the handler would be called again. We don't need this handler
for XDP, so don't enable cb as well as not calling free_old_xmit_skbs().
Also, we need to disable tx NAPI when disabling XDP, so
virtnet_poll_tx() can safely access curr_queue_pairs and
xdp_queue_pairs, which are not atomically updated while disabling XDP.
Fixes: b92f1e6751a6 ("virtio-net: transmit napi")
Fixes: 7b0411ef4aa6 ("virtio-net: clean tx descriptors from rx napi")
Signed-off-by: Toshiaki Makita <makita.toshiaki@lab.ntt.co.jp>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-01-29 08:45:54 +08:00
|
|
|
if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
|
2017-04-25 01:49:29 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (__netif_tx_trylock(txq)) {
|
2019-01-18 12:20:07 +08:00
|
|
|
free_old_xmit_skbs(sq, true);
|
2017-04-25 01:49:29 +08:00
|
|
|
__netif_tx_unlock(txq);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
|
|
|
|
netif_tx_wake_queue(txq);
|
|
|
|
}
|
|
|
|
|
2014-07-23 16:33:54 +08:00
|
|
|
static int virtnet_poll(struct napi_struct *napi, int budget)
|
|
|
|
{
|
|
|
|
struct receive_queue *rq =
|
|
|
|
container_of(napi, struct receive_queue, napi);
|
2018-04-13 14:58:25 +08:00
|
|
|
struct virtnet_info *vi = rq->vq->vdev->priv;
|
|
|
|
struct send_queue *sq;
|
2018-07-23 22:36:07 +08:00
|
|
|
unsigned int received;
|
2018-06-26 23:39:58 +08:00
|
|
|
unsigned int xdp_xmit = 0;
|
2014-07-23 16:33:54 +08:00
|
|
|
|
2017-04-25 01:49:29 +08:00
|
|
|
virtnet_poll_cleantx(rq);
|
|
|
|
|
2017-09-19 17:42:43 +08:00
|
|
|
received = virtnet_receive(rq, budget, &xdp_xmit);
|
2014-07-23 16:33:54 +08:00
|
|
|
|
2007-11-20 00:20:43 +08:00
|
|
|
/* Out of packets? */
|
2017-04-25 01:49:26 +08:00
|
|
|
if (received < budget)
|
|
|
|
virtqueue_napi_complete(napi, rq->vq, received);
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2018-06-26 23:39:58 +08:00
|
|
|
if (xdp_xmit & VIRTIO_XDP_REDIR)
|
xdp: Use bulking for non-map XDP_REDIRECT and consolidate code paths
Since the bulk queue used by XDP_REDIRECT now lives in struct net_device,
we can re-use the bulking for the non-map version of the bpf_redirect()
helper. This is a simple matter of having xdp_do_redirect_slow() queue the
frame on the bulk queue instead of sending it out with __bpf_tx_xdp().
Unfortunately we can't make the bpf_redirect() helper return an error if
the ifindex doesn't exit (as bpf_redirect_map() does), because we don't
have a reference to the network namespace of the ingress device at the time
the helper is called. So we have to leave it as-is and keep the device
lookup in xdp_do_redirect_slow().
Since this leaves less reason to have the non-map redirect code in a
separate function, so we get rid of the xdp_do_redirect_slow() function
entirely. This does lose us the tracepoint disambiguation, but fortunately
the xdp_redirect and xdp_redirect_map tracepoints use the same tracepoint
entry structures. This means both can contain a map index, so we can just
amend the tracepoint definitions so we always emit the xdp_redirect(_err)
tracepoints, but with the map ID only populated if a map is present. This
means we retire the xdp_redirect_map(_err) tracepoints entirely, but keep
the definitions around in case someone is still listening for them.
With this change, the performance of the xdp_redirect sample program goes
from 5Mpps to 8.4Mpps (a 68% increase).
Since the flush functions are no longer map-specific, rename the flush()
functions to drop _map from their names. One of the renamed functions is
the xdp_do_flush_map() callback used in all the xdp-enabled drivers. To
keep from having to update all drivers, use a #define to keep the old name
working, and only update the virtual drivers in this patch.
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/157918768505.1458396.17518057312953572912.stgit@toke.dk
2020-01-16 23:14:45 +08:00
|
|
|
xdp_do_flush();
|
2018-06-26 23:39:58 +08:00
|
|
|
|
|
|
|
if (xdp_xmit & VIRTIO_XDP_TX) {
|
2018-07-23 22:36:07 +08:00
|
|
|
sq = virtnet_xdp_sq(vi);
|
2018-07-23 22:36:09 +08:00
|
|
|
if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
|
|
|
|
u64_stats_update_begin(&sq->stats.syncp);
|
|
|
|
sq->stats.kicks++;
|
|
|
|
u64_stats_update_end(&sq->stats.syncp);
|
|
|
|
}
|
2018-04-13 14:58:25 +08:00
|
|
|
}
|
2017-09-19 17:42:43 +08:00
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
return received;
|
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
static int virtnet_open(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2018-01-03 18:26:04 +08:00
|
|
|
int i, err;
|
2012-12-07 15:04:56 +08:00
|
|
|
|
2013-05-22 04:03:58 +08:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
if (i < vi->curr_queue_pairs)
|
|
|
|
/* Make sure we have some buffers: if oom use wq. */
|
2014-10-24 05:12:10 +08:00
|
|
|
if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
|
2013-05-22 04:03:58 +08:00
|
|
|
schedule_delayed_work(&vi->refill, 0);
|
2018-01-03 18:26:04 +08:00
|
|
|
|
|
|
|
err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
xdp: rhashtable with allocator ID to pointer mapping
Use the IDA infrastructure for getting a cyclic increasing ID number,
that is used for keeping track of each registered allocator per
RX-queue xdp_rxq_info. Instead of using the IDR infrastructure, which
uses a radix tree, use a dynamic rhashtable, for creating ID to
pointer lookup table, because this is faster.
The problem that is being solved here is that, the xdp_rxq_info
pointer (stored in xdp_buff) cannot be used directly, as the
guaranteed lifetime is too short. The info is needed on a
(potentially) remote CPU during DMA-TX completion time . In an
xdp_frame the xdp_mem_info is stored, when it got converted from an
xdp_buff, which is sufficient for the simple page refcnt based recycle
schemes.
For more advanced allocators there is a need to store a pointer to the
registered allocator. Thus, there is a need to guard the lifetime or
validity of the allocator pointer, which is done through this
rhashtable ID map to pointer. The removal and validity of of the
allocator and helper struct xdp_mem_allocator is guarded by RCU. The
allocator will be created by the driver, and registered with
xdp_rxq_info_reg_mem_model().
It is up-to debate who is responsible for freeing the allocator
pointer or invoking the allocator destructor function. In any case,
this must happen via RCU freeing.
Use the IDA infrastructure for getting a cyclic increasing ID number,
that is used for keeping track of each registered allocator per
RX-queue xdp_rxq_info.
V4: Per req of Jason Wang
- Use xdp_rxq_info_reg_mem_model() in all drivers implementing
XDP_REDIRECT, even-though it's not strictly necessary when
allocator==NULL for type MEM_TYPE_PAGE_SHARED (given it's zero).
V6: Per req of Alex Duyck
- Introduce rhashtable_lookup() call in later patch
V8: Address sparse should be static warnings (from kbuild test robot)
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-04-17 22:46:12 +08:00
|
|
|
err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
|
|
|
|
MEM_TYPE_PAGE_SHARED, NULL);
|
|
|
|
if (err < 0) {
|
|
|
|
xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-25 01:49:26 +08:00
|
|
|
virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
|
2017-04-25 01:49:27 +08:00
|
|
|
virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-25 01:49:27 +08:00
|
|
|
static int virtnet_poll_tx(struct napi_struct *napi, int budget)
|
|
|
|
{
|
|
|
|
struct send_queue *sq = container_of(napi, struct send_queue, napi);
|
|
|
|
struct virtnet_info *vi = sq->vq->vdev->priv;
|
virtio_net: Don't call free_old_xmit_skbs for xdp_frames
When napi_tx is enabled, virtnet_poll_cleantx() called
free_old_xmit_skbs() even for xdp send queue.
This is bogus since the queue has xdp_frames, not sk_buffs, thus mangled
device tx bytes counters because skb->len is meaningless value, and even
triggered oops due to general protection fault on freeing them.
Since xdp send queues do not aquire locks, old xdp_frames should be
freed only in virtnet_xdp_xmit(), so just skip free_old_xmit_skbs() for
xdp send queues.
Similarly virtnet_poll_tx() called free_old_xmit_skbs(). This NAPI
handler is called even without calling start_xmit() because cb for tx is
by default enabled. Once the handler is called, it enabled the cb again,
and then the handler would be called again. We don't need this handler
for XDP, so don't enable cb as well as not calling free_old_xmit_skbs().
Also, we need to disable tx NAPI when disabling XDP, so
virtnet_poll_tx() can safely access curr_queue_pairs and
xdp_queue_pairs, which are not atomically updated while disabling XDP.
Fixes: b92f1e6751a6 ("virtio-net: transmit napi")
Fixes: 7b0411ef4aa6 ("virtio-net: clean tx descriptors from rx napi")
Signed-off-by: Toshiaki Makita <makita.toshiaki@lab.ntt.co.jp>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-01-29 08:45:54 +08:00
|
|
|
unsigned int index = vq2txq(sq->vq);
|
|
|
|
struct netdev_queue *txq;
|
2017-04-25 01:49:27 +08:00
|
|
|
|
virtio_net: Don't call free_old_xmit_skbs for xdp_frames
When napi_tx is enabled, virtnet_poll_cleantx() called
free_old_xmit_skbs() even for xdp send queue.
This is bogus since the queue has xdp_frames, not sk_buffs, thus mangled
device tx bytes counters because skb->len is meaningless value, and even
triggered oops due to general protection fault on freeing them.
Since xdp send queues do not aquire locks, old xdp_frames should be
freed only in virtnet_xdp_xmit(), so just skip free_old_xmit_skbs() for
xdp send queues.
Similarly virtnet_poll_tx() called free_old_xmit_skbs(). This NAPI
handler is called even without calling start_xmit() because cb for tx is
by default enabled. Once the handler is called, it enabled the cb again,
and then the handler would be called again. We don't need this handler
for XDP, so don't enable cb as well as not calling free_old_xmit_skbs().
Also, we need to disable tx NAPI when disabling XDP, so
virtnet_poll_tx() can safely access curr_queue_pairs and
xdp_queue_pairs, which are not atomically updated while disabling XDP.
Fixes: b92f1e6751a6 ("virtio-net: transmit napi")
Fixes: 7b0411ef4aa6 ("virtio-net: clean tx descriptors from rx napi")
Signed-off-by: Toshiaki Makita <makita.toshiaki@lab.ntt.co.jp>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-01-29 08:45:54 +08:00
|
|
|
if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
|
|
|
|
/* We don't need to enable cb for XDP */
|
|
|
|
napi_complete_done(napi, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
txq = netdev_get_tx_queue(vi->dev, index);
|
2017-04-25 01:49:27 +08:00
|
|
|
__netif_tx_lock(txq, raw_smp_processor_id());
|
2019-01-18 12:20:07 +08:00
|
|
|
free_old_xmit_skbs(sq, true);
|
2017-04-25 01:49:27 +08:00
|
|
|
__netif_tx_unlock(txq);
|
|
|
|
|
|
|
|
virtqueue_napi_complete(napi, sq->vq, 0);
|
|
|
|
|
|
|
|
if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
|
|
|
|
netif_tx_wake_queue(txq);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2014-10-24 21:55:57 +08:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr;
|
2007-10-22 09:03:37 +08:00
|
|
|
const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
|
2012-12-07 15:04:55 +08:00
|
|
|
struct virtnet_info *vi = sq->vq->vdev->priv;
|
2017-06-04 10:16:26 +08:00
|
|
|
int num_sg;
|
2014-10-24 21:55:57 +08:00
|
|
|
unsigned hdr_len = vi->hdr_len;
|
2013-07-25 08:50:23 +08:00
|
|
|
bool can_push;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2008-10-28 06:59:26 +08:00
|
|
|
pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
|
2013-07-25 08:50:23 +08:00
|
|
|
|
|
|
|
can_push = vi->any_header_sg &&
|
|
|
|
!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
|
|
|
|
!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
|
|
|
|
/* Even if we can, don't push here yet as this would skew
|
|
|
|
* csum_start offset below. */
|
|
|
|
if (can_push)
|
2014-10-24 21:55:57 +08:00
|
|
|
hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
|
2013-07-25 08:50:23 +08:00
|
|
|
else
|
|
|
|
hdr = skb_vnet_hdr(skb);
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2016-06-08 21:09:21 +08:00
|
|
|
if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
|
2018-06-06 23:23:01 +08:00
|
|
|
virtio_is_little_endian(vi->vdev), false,
|
|
|
|
0))
|
2016-06-08 21:09:21 +08:00
|
|
|
BUG();
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2008-11-17 14:41:34 +08:00
|
|
|
if (vi->mergeable_rx_bufs)
|
2014-10-24 21:55:57 +08:00
|
|
|
hdr->num_buffers = 0;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2015-08-27 14:53:06 +08:00
|
|
|
sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
|
2013-07-25 08:50:23 +08:00
|
|
|
if (can_push) {
|
|
|
|
__skb_push(skb, hdr_len);
|
|
|
|
num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
|
2017-06-04 10:16:26 +08:00
|
|
|
if (unlikely(num_sg < 0))
|
|
|
|
return num_sg;
|
2013-07-25 08:50:23 +08:00
|
|
|
/* Pull header back to avoid skew in tx bytes calculations. */
|
|
|
|
__skb_pull(skb, hdr_len);
|
|
|
|
} else {
|
|
|
|
sg_set_buf(sq->sg, hdr, hdr_len);
|
2017-06-04 10:16:26 +08:00
|
|
|
num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
|
|
|
|
if (unlikely(num_sg < 0))
|
|
|
|
return num_sg;
|
|
|
|
num_sg++;
|
2013-07-25 08:50:23 +08:00
|
|
|
}
|
2013-03-20 13:14:28 +08:00
|
|
|
return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
|
2008-05-26 15:48:13 +08:00
|
|
|
}
|
|
|
|
|
2009-09-01 03:50:51 +08:00
|
|
|
static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
|
2008-05-03 10:50:46 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2012-12-07 15:04:56 +08:00
|
|
|
int qnum = skb_get_queue_mapping(skb);
|
|
|
|
struct send_queue *sq = &vi->sq[qnum];
|
2012-10-16 21:26:14 +08:00
|
|
|
int err;
|
2014-10-15 21:23:28 +08:00
|
|
|
struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
|
2019-04-01 22:42:14 +08:00
|
|
|
bool kick = !netdev_xmit_more();
|
2017-04-25 01:49:27 +08:00
|
|
|
bool use_napi = sq->napi.weight;
|
2008-02-05 12:50:07 +08:00
|
|
|
|
|
|
|
/* Free up any pending old buffers before queueing new ones. */
|
2019-01-18 12:20:07 +08:00
|
|
|
free_old_xmit_skbs(sq, false);
|
2008-05-03 10:50:46 +08:00
|
|
|
|
2017-04-25 01:49:30 +08:00
|
|
|
if (use_napi && kick)
|
|
|
|
virtqueue_enable_cb_delayed(sq->vq);
|
|
|
|
|
2014-06-25 10:37:13 +08:00
|
|
|
/* timestamp packet in software */
|
|
|
|
skb_tx_timestamp(skb);
|
|
|
|
|
2009-10-28 19:03:38 +08:00
|
|
|
/* Try to transmit */
|
2012-12-21 00:37:04 +08:00
|
|
|
err = xmit_skb(sq, skb);
|
2009-09-24 23:59:20 +08:00
|
|
|
|
2012-10-16 21:26:14 +08:00
|
|
|
/* This should not happen! */
|
2014-03-26 13:03:00 +08:00
|
|
|
if (unlikely(err)) {
|
2012-10-16 21:26:14 +08:00
|
|
|
dev->stats.tx_fifo_errors++;
|
|
|
|
if (net_ratelimit())
|
|
|
|
dev_warn(&dev->dev,
|
2019-04-03 17:10:13 +08:00
|
|
|
"Unexpected TXQ (%d) queue failure: %d\n",
|
|
|
|
qnum, err);
|
2010-07-03 00:34:01 +08:00
|
|
|
dev->stats.tx_dropped++;
|
2014-03-16 09:43:33 +08:00
|
|
|
dev_kfree_skb_any(skb);
|
2010-07-03 00:34:01 +08:00
|
|
|
return NETDEV_TX_OK;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
2009-10-28 19:03:38 +08:00
|
|
|
|
2009-09-24 23:59:20 +08:00
|
|
|
/* Don't wait up for transmitted skbs to be freed. */
|
2017-04-25 01:49:27 +08:00
|
|
|
if (!use_napi) {
|
|
|
|
skb_orphan(skb);
|
2019-09-30 02:54:03 +08:00
|
|
|
nf_reset_ct(skb);
|
2017-04-25 01:49:27 +08:00
|
|
|
}
|
2009-09-24 23:59:20 +08:00
|
|
|
|
2015-04-02 19:05:47 +08:00
|
|
|
/* If running out of space, stop queue to avoid getting packets that we
|
|
|
|
* are then unable to transmit.
|
|
|
|
* An alternative would be to force queuing layer to requeue the skb by
|
|
|
|
* returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
|
|
|
|
* returned in a normal path of operation: it means that driver is not
|
|
|
|
* maintaining the TX queue stop/start state properly, and causes
|
|
|
|
* the stack to do a non-trivial amount of useless work.
|
|
|
|
* Since most packets only take 1 or 2 ring slots, stopping the queue
|
|
|
|
* early means 16 slots are typically wasted.
|
2015-03-25 07:22:07 +08:00
|
|
|
*/
|
2012-12-21 00:37:04 +08:00
|
|
|
if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
|
2012-12-07 15:04:56 +08:00
|
|
|
netif_stop_subqueue(dev, qnum);
|
2017-04-25 01:49:27 +08:00
|
|
|
if (!use_napi &&
|
|
|
|
unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
|
2009-09-24 23:59:20 +08:00
|
|
|
/* More just got used, free them then recheck. */
|
2019-01-18 12:20:07 +08:00
|
|
|
free_old_xmit_skbs(sq, false);
|
2012-12-21 00:37:04 +08:00
|
|
|
if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
|
2012-12-07 15:04:56 +08:00
|
|
|
netif_start_subqueue(dev, qnum);
|
2012-12-07 15:04:55 +08:00
|
|
|
virtqueue_disable_cb(sq->vq);
|
2009-09-24 23:59:20 +08:00
|
|
|
}
|
|
|
|
}
|
2008-05-03 10:50:46 +08:00
|
|
|
}
|
2009-09-24 23:59:20 +08:00
|
|
|
|
2018-07-23 22:36:09 +08:00
|
|
|
if (kick || netif_xmit_stopped(txq)) {
|
|
|
|
if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
|
|
|
|
u64_stats_update_begin(&sq->stats.syncp);
|
|
|
|
sq->stats.kicks++;
|
|
|
|
u64_stats_update_end(&sq->stats.syncp);
|
|
|
|
}
|
|
|
|
}
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2014-08-26 06:51:53 +08:00
|
|
|
return NETDEV_TX_OK;
|
2014-08-24 04:18:10 +08:00
|
|
|
}
|
|
|
|
|
2013-01-21 09:17:21 +08:00
|
|
|
/*
|
|
|
|
* Send command via the control virtqueue and check status. Commands
|
|
|
|
* supported by the hypervisor, as indicated by feature bits, should
|
2013-12-10 08:18:45 +08:00
|
|
|
* never fail unless improperly formatted.
|
2013-01-21 09:17:21 +08:00
|
|
|
*/
|
|
|
|
static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
|
2013-12-10 08:17:40 +08:00
|
|
|
struct scatterlist *out)
|
2013-01-21 09:17:21 +08:00
|
|
|
{
|
2013-03-20 13:14:28 +08:00
|
|
|
struct scatterlist *sgs[4], hdr, stat;
|
2013-12-10 08:17:40 +08:00
|
|
|
unsigned out_num = 0, tmp;
|
2013-01-21 09:17:21 +08:00
|
|
|
|
|
|
|
/* Caller should know better */
|
2013-03-20 13:14:28 +08:00
|
|
|
BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
|
2013-01-21 09:17:21 +08:00
|
|
|
|
2018-04-19 13:30:48 +08:00
|
|
|
vi->ctrl->status = ~0;
|
|
|
|
vi->ctrl->hdr.class = class;
|
|
|
|
vi->ctrl->hdr.cmd = cmd;
|
2013-03-20 13:14:28 +08:00
|
|
|
/* Add header */
|
2018-04-19 13:30:48 +08:00
|
|
|
sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
|
2013-03-20 13:14:28 +08:00
|
|
|
sgs[out_num++] = &hdr;
|
2013-01-21 09:17:21 +08:00
|
|
|
|
2013-03-20 13:14:28 +08:00
|
|
|
if (out)
|
|
|
|
sgs[out_num++] = out;
|
2013-01-21 09:17:21 +08:00
|
|
|
|
2013-03-20 13:14:28 +08:00
|
|
|
/* Add return status. */
|
2018-04-19 13:30:48 +08:00
|
|
|
sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
|
2013-12-10 08:17:40 +08:00
|
|
|
sgs[out_num] = &stat;
|
2013-01-21 09:17:21 +08:00
|
|
|
|
2013-12-10 08:17:40 +08:00
|
|
|
BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
|
2014-03-13 08:53:39 +08:00
|
|
|
virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
|
2013-01-21 09:17:21 +08:00
|
|
|
|
2013-10-29 07:10:02 +08:00
|
|
|
if (unlikely(!virtqueue_kick(vi->cvq)))
|
2018-04-19 13:30:48 +08:00
|
|
|
return vi->ctrl->status == VIRTIO_NET_OK;
|
2013-01-21 09:17:21 +08:00
|
|
|
|
|
|
|
/* Spin for a response, the kick causes an ioport write, trapping
|
|
|
|
* into the hypervisor, so the request should be handled immediately.
|
|
|
|
*/
|
2013-10-29 07:10:47 +08:00
|
|
|
while (!virtqueue_get_buf(vi->cvq, &tmp) &&
|
|
|
|
!virtqueue_is_broken(vi->cvq))
|
2013-01-21 09:17:21 +08:00
|
|
|
cpu_relax();
|
|
|
|
|
2018-04-19 13:30:48 +08:00
|
|
|
return vi->ctrl->status == VIRTIO_NET_OK;
|
2013-01-21 09:17:21 +08:00
|
|
|
}
|
|
|
|
|
2009-02-05 08:36:34 +08:00
|
|
|
static int virtnet_set_mac_address(struct net_device *dev, void *p)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct virtio_device *vdev = vi->vdev;
|
2012-06-29 13:10:06 +08:00
|
|
|
int ret;
|
2016-12-06 10:10:58 +08:00
|
|
|
struct sockaddr *addr;
|
2013-01-21 09:17:23 +08:00
|
|
|
struct scatterlist sg;
|
2009-02-05 08:36:34 +08:00
|
|
|
|
2018-05-25 00:55:17 +08:00
|
|
|
if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2016-12-24 03:14:58 +08:00
|
|
|
addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
|
2016-12-06 10:10:58 +08:00
|
|
|
if (!addr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ret = eth_prepare_mac_addr_change(dev, addr);
|
2012-06-29 13:10:06 +08:00
|
|
|
if (ret)
|
2016-12-06 10:10:58 +08:00
|
|
|
goto out;
|
2009-02-05 08:36:34 +08:00
|
|
|
|
2013-01-21 09:17:23 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
|
|
|
|
sg_init_one(&sg, addr->sa_data, dev->addr_len);
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
|
2013-12-10 08:17:40 +08:00
|
|
|
VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
|
2013-01-21 09:17:23 +08:00
|
|
|
dev_warn(&vdev->dev,
|
|
|
|
"Failed to set mac address by vq command.\n");
|
2016-12-06 10:10:58 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2013-01-21 09:17:23 +08:00
|
|
|
}
|
2014-11-26 21:58:28 +08:00
|
|
|
} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
|
|
|
|
!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
|
2013-10-14 15:41:51 +08:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Naturally, this has an atomicity problem. */
|
|
|
|
for (i = 0; i < dev->addr_len; i++)
|
|
|
|
virtio_cwrite8(vdev,
|
|
|
|
offsetof(struct virtio_net_config, mac) +
|
|
|
|
i, addr->sa_data[i]);
|
2013-01-21 09:17:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
eth_commit_mac_addr_change(dev, p);
|
2016-12-06 10:10:58 +08:00
|
|
|
ret = 0;
|
2009-02-05 08:36:34 +08:00
|
|
|
|
2016-12-06 10:10:58 +08:00
|
|
|
out:
|
|
|
|
kfree(addr);
|
|
|
|
return ret;
|
2009-02-05 08:36:34 +08:00
|
|
|
}
|
|
|
|
|
2017-01-07 11:12:52 +08:00
|
|
|
static void virtnet_stats(struct net_device *dev,
|
|
|
|
struct rtnl_link_stats64 *tot)
|
2011-06-15 14:36:29 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
unsigned int start;
|
2018-01-17 14:38:25 +08:00
|
|
|
int i;
|
2011-06-15 14:36:29 +08:00
|
|
|
|
2018-01-17 14:38:25 +08:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
2018-07-23 22:36:06 +08:00
|
|
|
u64 tpackets, tbytes, rpackets, rbytes, rdrops;
|
2018-01-17 14:38:25 +08:00
|
|
|
struct receive_queue *rq = &vi->rq[i];
|
|
|
|
struct send_queue *sq = &vi->sq[i];
|
2011-06-15 14:36:29 +08:00
|
|
|
|
|
|
|
do {
|
2018-01-17 14:38:25 +08:00
|
|
|
start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
|
|
|
|
tpackets = sq->stats.packets;
|
|
|
|
tbytes = sq->stats.bytes;
|
|
|
|
} while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
|
2012-06-06 06:35:24 +08:00
|
|
|
|
|
|
|
do {
|
2018-01-17 14:38:25 +08:00
|
|
|
start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
|
2018-07-31 17:43:39 +08:00
|
|
|
rpackets = rq->stats.packets;
|
|
|
|
rbytes = rq->stats.bytes;
|
|
|
|
rdrops = rq->stats.drops;
|
2018-01-17 14:38:25 +08:00
|
|
|
} while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
|
2011-06-15 14:36:29 +08:00
|
|
|
|
|
|
|
tot->rx_packets += rpackets;
|
|
|
|
tot->tx_packets += tpackets;
|
|
|
|
tot->rx_bytes += rbytes;
|
|
|
|
tot->tx_bytes += tbytes;
|
2018-07-23 22:36:06 +08:00
|
|
|
tot->rx_dropped += rdrops;
|
2011-06-15 14:36:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tot->tx_dropped = dev->stats.tx_dropped;
|
2011-11-21 17:28:17 +08:00
|
|
|
tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
|
2011-06-15 14:36:29 +08:00
|
|
|
tot->rx_length_errors = dev->stats.rx_length_errors;
|
|
|
|
tot->rx_frame_errors = dev->stats.rx_frame_errors;
|
|
|
|
}
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
static void virtnet_ack_link_announce(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
rtnl_lock();
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
|
2013-12-10 08:17:40 +08:00
|
|
|
VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
|
2012-04-12 04:43:52 +08:00
|
|
|
dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
|
|
|
|
rtnl_unlock();
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:14:32 +08:00
|
|
|
static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
|
2012-12-07 15:04:56 +08:00
|
|
|
{
|
|
|
|
struct scatterlist sg;
|
|
|
|
struct net_device *dev = vi->dev;
|
|
|
|
|
|
|
|
if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
|
|
|
|
return 0;
|
|
|
|
|
2018-04-19 13:30:48 +08:00
|
|
|
vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
|
|
|
|
sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
|
2012-12-07 15:04:56 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
|
2013-12-10 08:17:40 +08:00
|
|
|
VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
|
2012-12-07 15:04:56 +08:00
|
|
|
dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
|
|
|
|
queue_pairs);
|
|
|
|
return -EINVAL;
|
2013-04-29 10:30:08 +08:00
|
|
|
} else {
|
2012-12-07 15:04:56 +08:00
|
|
|
vi->curr_queue_pairs = queue_pairs;
|
2013-10-15 11:18:59 +08:00
|
|
|
/* virtnet_open() will refill when device is going to up. */
|
|
|
|
if (dev->flags & IFF_UP)
|
|
|
|
schedule_delayed_work(&vi->refill, 0);
|
2013-04-29 10:30:08 +08:00
|
|
|
}
|
2012-12-07 15:04:56 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:14:32 +08:00
|
|
|
static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
rtnl_lock();
|
|
|
|
err = _virtnet_set_queues(vi, queue_pairs);
|
|
|
|
rtnl_unlock();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
static int virtnet_close(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2012-12-07 15:04:56 +08:00
|
|
|
int i;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2011-12-29 08:42:38 +08:00
|
|
|
/* Make sure refill_work doesn't re-enable napi! */
|
|
|
|
cancel_delayed_work_sync(&vi->refill);
|
2012-12-07 15:04:56 +08:00
|
|
|
|
2017-04-25 01:49:27 +08:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
2018-01-03 18:26:04 +08:00
|
|
|
xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
|
2012-12-07 15:04:56 +08:00
|
|
|
napi_disable(&vi->rq[i].napi);
|
2017-04-26 03:59:17 +08:00
|
|
|
virtnet_napi_tx_disable(&vi->sq[i].napi);
|
2017-04-25 01:49:27 +08:00
|
|
|
}
|
2007-10-22 09:03:37 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-04 17:02:40 +08:00
|
|
|
static void virtnet_set_rx_mode(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2009-02-04 17:02:45 +08:00
|
|
|
struct scatterlist sg[2];
|
|
|
|
struct virtio_net_ctrl_mac *mac_data;
|
2009-05-23 07:22:17 +08:00
|
|
|
struct netdev_hw_addr *ha;
|
2010-01-26 05:36:10 +08:00
|
|
|
int uc_count;
|
2010-02-08 12:30:35 +08:00
|
|
|
int mc_count;
|
2009-02-04 17:02:45 +08:00
|
|
|
void *buf;
|
|
|
|
int i;
|
2009-02-04 17:02:40 +08:00
|
|
|
|
2013-12-10 08:18:45 +08:00
|
|
|
/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
|
2009-02-04 17:02:40 +08:00
|
|
|
if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
|
|
|
|
return;
|
|
|
|
|
2018-04-19 13:30:48 +08:00
|
|
|
vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
|
|
|
|
vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
|
2009-02-04 17:02:40 +08:00
|
|
|
|
2018-04-19 13:30:48 +08:00
|
|
|
sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
|
2009-02-04 17:02:40 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
|
2013-12-10 08:17:40 +08:00
|
|
|
VIRTIO_NET_CTRL_RX_PROMISC, sg))
|
2009-02-04 17:02:40 +08:00
|
|
|
dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
|
2018-04-19 13:30:48 +08:00
|
|
|
vi->ctrl->promisc ? "en" : "dis");
|
2009-02-04 17:02:40 +08:00
|
|
|
|
2018-04-19 13:30:48 +08:00
|
|
|
sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
|
2009-02-04 17:02:40 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
|
2013-12-10 08:17:40 +08:00
|
|
|
VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
|
2009-02-04 17:02:40 +08:00
|
|
|
dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
|
2018-04-19 13:30:48 +08:00
|
|
|
vi->ctrl->allmulti ? "en" : "dis");
|
2009-02-04 17:02:45 +08:00
|
|
|
|
2010-01-26 05:36:10 +08:00
|
|
|
uc_count = netdev_uc_count(dev);
|
2010-02-08 12:30:35 +08:00
|
|
|
mc_count = netdev_mc_count(dev);
|
2009-02-04 17:02:45 +08:00
|
|
|
/* MAC filter - use one buffer for both lists */
|
2010-02-08 12:30:35 +08:00
|
|
|
buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
|
|
|
|
(2 * sizeof(mac_data->entries)), GFP_ATOMIC);
|
|
|
|
mac_data = buf;
|
2013-02-04 01:28:15 +08:00
|
|
|
if (!buf)
|
2009-02-04 17:02:45 +08:00
|
|
|
return;
|
|
|
|
|
2009-05-02 01:27:56 +08:00
|
|
|
sg_init_table(sg, 2);
|
|
|
|
|
2009-02-04 17:02:45 +08:00
|
|
|
/* Store the unicast list and count in the front of the buffer */
|
2014-10-07 22:39:48 +08:00
|
|
|
mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
|
2009-05-23 07:22:17 +08:00
|
|
|
i = 0;
|
2010-01-26 05:36:10 +08:00
|
|
|
netdev_for_each_uc_addr(ha, dev)
|
2009-05-23 07:22:17 +08:00
|
|
|
memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
|
2009-02-04 17:02:45 +08:00
|
|
|
|
|
|
|
sg_set_buf(&sg[0], mac_data,
|
2010-01-26 05:36:10 +08:00
|
|
|
sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
|
2009-02-04 17:02:45 +08:00
|
|
|
|
|
|
|
/* multicast list and count fill the end */
|
2010-01-26 05:36:10 +08:00
|
|
|
mac_data = (void *)&mac_data->macs[uc_count][0];
|
2009-02-04 17:02:45 +08:00
|
|
|
|
2014-10-07 22:39:48 +08:00
|
|
|
mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
|
2010-02-24 07:17:07 +08:00
|
|
|
i = 0;
|
2010-04-02 05:22:57 +08:00
|
|
|
netdev_for_each_mc_addr(ha, dev)
|
|
|
|
memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
|
2009-02-04 17:02:45 +08:00
|
|
|
|
|
|
|
sg_set_buf(&sg[1], mac_data,
|
2010-02-08 12:30:35 +08:00
|
|
|
sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
|
2009-02-04 17:02:45 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
|
2013-12-10 08:17:40 +08:00
|
|
|
VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
|
2013-11-29 17:02:19 +08:00
|
|
|
dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
|
2009-02-04 17:02:45 +08:00
|
|
|
|
|
|
|
kfree(buf);
|
2009-02-04 17:02:40 +08:00
|
|
|
}
|
|
|
|
|
2013-04-19 10:04:28 +08:00
|
|
|
static int virtnet_vlan_rx_add_vid(struct net_device *dev,
|
|
|
|
__be16 proto, u16 vid)
|
2009-02-04 17:02:50 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct scatterlist sg;
|
|
|
|
|
2018-04-19 13:30:49 +08:00
|
|
|
vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
|
2018-04-19 13:30:48 +08:00
|
|
|
sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
|
2009-02-04 17:02:50 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
|
2013-12-10 08:17:40 +08:00
|
|
|
VIRTIO_NET_CTRL_VLAN_ADD, &sg))
|
2009-02-04 17:02:50 +08:00
|
|
|
dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
|
2011-12-09 08:52:37 +08:00
|
|
|
return 0;
|
2009-02-04 17:02:50 +08:00
|
|
|
}
|
|
|
|
|
2013-04-19 10:04:28 +08:00
|
|
|
static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
|
|
|
|
__be16 proto, u16 vid)
|
2009-02-04 17:02:50 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct scatterlist sg;
|
|
|
|
|
2018-04-19 13:30:49 +08:00
|
|
|
vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
|
2018-04-19 13:30:48 +08:00
|
|
|
sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
|
2009-02-04 17:02:50 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
|
2013-12-10 08:17:40 +08:00
|
|
|
VIRTIO_NET_CTRL_VLAN_DEL, &sg))
|
2009-02-04 17:02:50 +08:00
|
|
|
dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
|
2011-12-09 08:52:37 +08:00
|
|
|
return 0;
|
2009-02-04 17:02:50 +08:00
|
|
|
}
|
|
|
|
|
2019-03-18 14:56:06 +08:00
|
|
|
static void virtnet_clean_affinity(struct virtnet_info *vi)
|
2012-12-07 15:04:56 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-01-25 07:51:30 +08:00
|
|
|
if (vi->affinity_hint_set) {
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
2018-08-10 09:18:28 +08:00
|
|
|
virtqueue_set_affinity(vi->rq[i].vq, NULL);
|
|
|
|
virtqueue_set_affinity(vi->sq[i].vq, NULL);
|
2013-01-25 07:51:29 +08:00
|
|
|
}
|
|
|
|
|
2013-01-25 07:51:30 +08:00
|
|
|
vi->affinity_hint_set = false;
|
|
|
|
}
|
|
|
|
}
|
2013-01-25 07:51:29 +08:00
|
|
|
|
2013-01-25 07:51:30 +08:00
|
|
|
static void virtnet_set_affinity(struct virtnet_info *vi)
|
|
|
|
{
|
2018-08-10 08:28:40 +08:00
|
|
|
cpumask_var_t mask;
|
|
|
|
int stragglers;
|
|
|
|
int group_size;
|
|
|
|
int i, j, cpu;
|
|
|
|
int num_cpu;
|
|
|
|
int stride;
|
|
|
|
|
|
|
|
if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
|
2019-03-18 14:56:06 +08:00
|
|
|
virtnet_clean_affinity(vi);
|
2013-01-25 07:51:30 +08:00
|
|
|
return;
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
2018-08-10 08:28:40 +08:00
|
|
|
num_cpu = num_online_cpus();
|
|
|
|
stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
|
|
|
|
stragglers = num_cpu >= vi->curr_queue_pairs ?
|
|
|
|
num_cpu % vi->curr_queue_pairs :
|
|
|
|
0;
|
|
|
|
cpu = cpumask_next(-1, cpu_online_mask);
|
2018-08-09 11:07:35 +08:00
|
|
|
|
2018-08-10 08:28:40 +08:00
|
|
|
for (i = 0; i < vi->curr_queue_pairs; i++) {
|
|
|
|
group_size = stride + (i < stragglers ? 1 : 0);
|
|
|
|
|
|
|
|
for (j = 0; j < group_size; j++) {
|
|
|
|
cpumask_set_cpu(cpu, mask);
|
|
|
|
cpu = cpumask_next_wrap(cpu, cpu_online_mask,
|
|
|
|
nr_cpu_ids, false);
|
|
|
|
}
|
|
|
|
virtqueue_set_affinity(vi->rq[i].vq, mask);
|
|
|
|
virtqueue_set_affinity(vi->sq[i].vq, mask);
|
|
|
|
__netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, false);
|
|
|
|
cpumask_clear(mask);
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
2013-01-25 07:51:30 +08:00
|
|
|
vi->affinity_hint_set = true;
|
2018-08-10 08:28:40 +08:00
|
|
|
free_cpumask_var(mask);
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
2016-08-13 01:49:43 +08:00
|
|
|
static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
|
2013-01-25 07:51:31 +08:00
|
|
|
{
|
2016-08-13 01:49:43 +08:00
|
|
|
struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
|
|
|
|
node);
|
|
|
|
virtnet_set_affinity(vi);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-25 07:51:31 +08:00
|
|
|
|
2016-08-13 01:49:43 +08:00
|
|
|
static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
|
|
|
|
node_dead);
|
|
|
|
virtnet_set_affinity(vi);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-15 11:18:58 +08:00
|
|
|
|
2016-08-13 01:49:43 +08:00
|
|
|
static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
|
|
|
|
node);
|
|
|
|
|
2019-03-18 14:56:06 +08:00
|
|
|
virtnet_clean_affinity(vi);
|
2016-08-13 01:49:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum cpuhp_state virtionet_online;
|
|
|
|
|
|
|
|
static int virtnet_cpu_notif_add(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
|
|
|
|
&vi->node_dead);
|
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
|
|
|
|
cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
|
|
|
|
&vi->node_dead);
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
2011-10-19 16:10:59 +08:00
|
|
|
static void virtnet_get_ringparam(struct net_device *dev,
|
|
|
|
struct ethtool_ringparam *ring)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
|
|
|
|
ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
|
2011-10-19 16:10:59 +08:00
|
|
|
ring->rx_pending = ring->rx_max_pending;
|
|
|
|
ring->tx_pending = ring->tx_max_pending;
|
|
|
|
}
|
|
|
|
|
2011-11-14 22:17:08 +08:00
|
|
|
|
|
|
|
static void virtnet_get_drvinfo(struct net_device *dev,
|
|
|
|
struct ethtool_drvinfo *info)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct virtio_device *vdev = vi->vdev;
|
|
|
|
|
|
|
|
strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
|
|
|
|
strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
|
|
|
|
strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:57 +08:00
|
|
|
/* TODO: Eliminate OOO packets during switching */
|
|
|
|
static int virtnet_set_channels(struct net_device *dev,
|
|
|
|
struct ethtool_channels *channels)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
u16 queue_pairs = channels->combined_count;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* We don't support separate rx/tx channels.
|
|
|
|
* We don't allow setting 'other' channels.
|
|
|
|
*/
|
|
|
|
if (channels->rx_count || channels->tx_count || channels->other_count)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-04-18 13:45:41 +08:00
|
|
|
if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
|
2012-12-07 15:04:57 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2016-12-16 04:13:24 +08:00
|
|
|
/* For now we don't support modifying channels while XDP is loaded
|
|
|
|
* also when XDP is loaded all RX queues have XDP programs so we only
|
|
|
|
* need to check a single RX queue.
|
|
|
|
*/
|
|
|
|
if (vi->rq[0].xdp_prog)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2013-01-25 07:51:29 +08:00
|
|
|
get_online_cpus();
|
2017-02-03 11:14:32 +08:00
|
|
|
err = _virtnet_set_queues(vi, queue_pairs);
|
2012-12-07 15:04:57 +08:00
|
|
|
if (!err) {
|
|
|
|
netif_set_real_num_tx_queues(dev, queue_pairs);
|
|
|
|
netif_set_real_num_rx_queues(dev, queue_pairs);
|
|
|
|
|
2013-01-25 07:51:30 +08:00
|
|
|
virtnet_set_affinity(vi);
|
2012-12-07 15:04:57 +08:00
|
|
|
}
|
2013-01-25 07:51:29 +08:00
|
|
|
put_online_cpus();
|
2012-12-07 15:04:57 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:38:25 +08:00
|
|
|
static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
char *p = (char *)data;
|
|
|
|
unsigned int i, j;
|
|
|
|
|
|
|
|
switch (stringset) {
|
|
|
|
case ETH_SS_STATS:
|
|
|
|
for (i = 0; i < vi->curr_queue_pairs; i++) {
|
|
|
|
for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
|
|
|
|
snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_%s",
|
|
|
|
i, virtnet_rq_stats_desc[j].desc);
|
|
|
|
p += ETH_GSTRING_LEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < vi->curr_queue_pairs; i++) {
|
|
|
|
for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
|
|
|
|
snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_%s",
|
|
|
|
i, virtnet_sq_stats_desc[j].desc);
|
|
|
|
p += ETH_GSTRING_LEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_get_sset_count(struct net_device *dev, int sset)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
|
|
|
switch (sset) {
|
|
|
|
case ETH_SS_STATS:
|
|
|
|
return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
|
|
|
|
VIRTNET_SQ_STATS_LEN);
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtnet_get_ethtool_stats(struct net_device *dev,
|
|
|
|
struct ethtool_stats *stats, u64 *data)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
unsigned int idx = 0, start, i, j;
|
|
|
|
const u8 *stats_base;
|
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
for (i = 0; i < vi->curr_queue_pairs; i++) {
|
|
|
|
struct receive_queue *rq = &vi->rq[i];
|
|
|
|
|
2018-07-31 17:43:39 +08:00
|
|
|
stats_base = (u8 *)&rq->stats;
|
2018-01-17 14:38:25 +08:00
|
|
|
do {
|
|
|
|
start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
|
|
|
|
for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
|
|
|
|
offset = virtnet_rq_stats_desc[j].offset;
|
|
|
|
data[idx + j] = *(u64 *)(stats_base + offset);
|
|
|
|
}
|
|
|
|
} while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
|
|
|
|
idx += VIRTNET_RQ_STATS_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < vi->curr_queue_pairs; i++) {
|
|
|
|
struct send_queue *sq = &vi->sq[i];
|
|
|
|
|
|
|
|
stats_base = (u8 *)&sq->stats;
|
|
|
|
do {
|
|
|
|
start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
|
|
|
|
for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
|
|
|
|
offset = virtnet_sq_stats_desc[j].offset;
|
|
|
|
data[idx + j] = *(u64 *)(stats_base + offset);
|
|
|
|
}
|
|
|
|
} while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
|
|
|
|
idx += VIRTNET_SQ_STATS_LEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:57 +08:00
|
|
|
static void virtnet_get_channels(struct net_device *dev,
|
|
|
|
struct ethtool_channels *channels)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
|
|
|
channels->combined_count = vi->curr_queue_pairs;
|
|
|
|
channels->max_combined = vi->max_queue_pairs;
|
|
|
|
channels->max_other = 0;
|
|
|
|
channels->rx_count = 0;
|
|
|
|
channels->tx_count = 0;
|
|
|
|
channels->other_count = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-22 06:24:24 +08:00
|
|
|
static int virtnet_set_link_ksettings(struct net_device *dev,
|
|
|
|
const struct ethtool_link_ksettings *cmd)
|
2016-02-03 11:04:37 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
2020-02-29 04:12:05 +08:00
|
|
|
return ethtool_virtdev_set_link_ksettings(dev, cmd,
|
|
|
|
&vi->speed, &vi->duplex);
|
2016-02-03 11:04:37 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 06:24:24 +08:00
|
|
|
static int virtnet_get_link_ksettings(struct net_device *dev,
|
|
|
|
struct ethtool_link_ksettings *cmd)
|
2016-02-03 11:04:37 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
2017-03-22 06:24:24 +08:00
|
|
|
cmd->base.speed = vi->speed;
|
|
|
|
cmd->base.duplex = vi->duplex;
|
|
|
|
cmd->base.port = PORT_OTHER;
|
2016-02-03 11:04:37 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:06:26 +08:00
|
|
|
static int virtnet_set_coalesce(struct net_device *dev,
|
|
|
|
struct ethtool_coalesce *ec)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
int i, napi_weight;
|
|
|
|
|
2020-03-05 13:15:42 +08:00
|
|
|
if (ec->tx_max_coalesced_frames > 1 ||
|
|
|
|
ec->rx_max_coalesced_frames != 1)
|
2018-10-09 10:06:26 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
|
|
|
|
if (napi_weight ^ vi->sq[0].napi.weight) {
|
|
|
|
if (dev->flags & IFF_UP)
|
|
|
|
return -EBUSY;
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++)
|
|
|
|
vi->sq[i].napi.weight = napi_weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_get_coalesce(struct net_device *dev,
|
|
|
|
struct ethtool_coalesce *ec)
|
|
|
|
{
|
|
|
|
struct ethtool_coalesce ec_default = {
|
|
|
|
.cmd = ETHTOOL_GCOALESCE,
|
|
|
|
.rx_max_coalesced_frames = 1,
|
|
|
|
};
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
|
|
|
memcpy(ec, &ec_default, sizeof(ec_default));
|
|
|
|
|
|
|
|
if (vi->sq[0].napi.weight)
|
|
|
|
ec->tx_max_coalesced_frames = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-03 11:04:37 +08:00
|
|
|
static void virtnet_init_settings(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
|
|
|
vi->speed = SPEED_UNKNOWN;
|
|
|
|
vi->duplex = DUPLEX_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2018-01-06 06:44:54 +08:00
|
|
|
static void virtnet_update_settings(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
u32 speed;
|
|
|
|
u8 duplex;
|
|
|
|
|
|
|
|
if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
|
|
|
|
return;
|
|
|
|
|
2020-08-05 17:39:36 +08:00
|
|
|
virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
|
|
|
|
|
2018-01-06 06:44:54 +08:00
|
|
|
if (ethtool_validate_speed(speed))
|
|
|
|
vi->speed = speed;
|
2020-08-05 17:39:36 +08:00
|
|
|
|
|
|
|
virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
|
|
|
|
|
2018-01-06 06:44:54 +08:00
|
|
|
if (ethtool_validate_duplex(duplex))
|
|
|
|
vi->duplex = duplex;
|
|
|
|
}
|
|
|
|
|
2009-09-02 16:03:33 +08:00
|
|
|
static const struct ethtool_ops virtnet_ethtool_ops = {
|
2020-03-05 13:15:42 +08:00
|
|
|
.supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
|
2011-11-14 22:17:08 +08:00
|
|
|
.get_drvinfo = virtnet_get_drvinfo,
|
2009-01-20 09:09:49 +08:00
|
|
|
.get_link = ethtool_op_get_link,
|
2011-10-19 16:10:59 +08:00
|
|
|
.get_ringparam = virtnet_get_ringparam,
|
2018-01-17 14:38:25 +08:00
|
|
|
.get_strings = virtnet_get_strings,
|
|
|
|
.get_sset_count = virtnet_get_sset_count,
|
|
|
|
.get_ethtool_stats = virtnet_get_ethtool_stats,
|
2012-12-07 15:04:57 +08:00
|
|
|
.set_channels = virtnet_set_channels,
|
|
|
|
.get_channels = virtnet_get_channels,
|
2014-06-25 10:37:13 +08:00
|
|
|
.get_ts_info = ethtool_op_get_ts_info,
|
2017-03-22 06:24:24 +08:00
|
|
|
.get_link_ksettings = virtnet_get_link_ksettings,
|
|
|
|
.set_link_ksettings = virtnet_set_link_ksettings,
|
2018-10-09 10:06:26 +08:00
|
|
|
.set_coalesce = virtnet_set_coalesce,
|
|
|
|
.get_coalesce = virtnet_get_coalesce,
|
2008-04-18 11:21:42 +08:00
|
|
|
};
|
|
|
|
|
2017-02-03 11:16:01 +08:00
|
|
|
static void virtnet_freeze_down(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Make sure no work handler is accessing the device */
|
|
|
|
flush_work(&vi->config_work);
|
|
|
|
|
2018-10-17 18:44:12 +08:00
|
|
|
netif_tx_lock_bh(vi->dev);
|
2017-02-03 11:16:01 +08:00
|
|
|
netif_device_detach(vi->dev);
|
2018-10-17 18:44:12 +08:00
|
|
|
netif_tx_unlock_bh(vi->dev);
|
2017-02-03 11:16:01 +08:00
|
|
|
cancel_delayed_work_sync(&vi->refill);
|
|
|
|
|
|
|
|
if (netif_running(vi->dev)) {
|
2017-04-25 01:49:27 +08:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
2017-02-03 11:16:01 +08:00
|
|
|
napi_disable(&vi->rq[i].napi);
|
2017-04-26 03:59:17 +08:00
|
|
|
virtnet_napi_tx_disable(&vi->sq[i].napi);
|
2017-04-25 01:49:27 +08:00
|
|
|
}
|
2017-02-03 11:16:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init_vqs(struct virtnet_info *vi);
|
|
|
|
|
|
|
|
static int virtnet_restore_up(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
int err, i;
|
|
|
|
|
|
|
|
err = init_vqs(vi);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
virtio_device_ready(vdev);
|
|
|
|
|
|
|
|
if (netif_running(vi->dev)) {
|
|
|
|
for (i = 0; i < vi->curr_queue_pairs; i++)
|
|
|
|
if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
|
|
|
|
schedule_delayed_work(&vi->refill, 0);
|
|
|
|
|
2017-04-25 01:49:27 +08:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
2017-04-25 01:49:26 +08:00
|
|
|
virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
|
2017-04-25 01:49:27 +08:00
|
|
|
virtnet_napi_tx_enable(vi, vi->sq[i].vq,
|
|
|
|
&vi->sq[i].napi);
|
|
|
|
}
|
2017-02-03 11:16:01 +08:00
|
|
|
}
|
|
|
|
|
2018-10-17 18:44:12 +08:00
|
|
|
netif_tx_lock_bh(vi->dev);
|
2017-02-03 11:16:01 +08:00
|
|
|
netif_device_attach(vi->dev);
|
2018-10-17 18:44:12 +08:00
|
|
|
netif_tx_unlock_bh(vi->dev);
|
2017-02-03 11:16:01 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-07-19 16:54:49 +08:00
|
|
|
static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
|
|
|
|
{
|
|
|
|
struct scatterlist sg;
|
2018-04-19 13:30:48 +08:00
|
|
|
vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
|
2017-07-19 16:54:49 +08:00
|
|
|
|
2018-04-19 13:30:48 +08:00
|
|
|
sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
|
2017-07-19 16:54:49 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
|
|
|
|
VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
|
2019-04-03 17:10:13 +08:00
|
|
|
dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
|
2017-07-19 16:54:49 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
u64 offloads = 0;
|
|
|
|
|
|
|
|
if (!vi->guest_offloads)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return virtnet_set_guest_offloads(vi, offloads);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
u64 offloads = vi->guest_offloads;
|
|
|
|
|
|
|
|
if (!vi->guest_offloads)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return virtnet_set_guest_offloads(vi, offloads);
|
|
|
|
}
|
|
|
|
|
2017-05-01 12:46:48 +08:00
|
|
|
static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
|
|
|
|
struct netlink_ext_ack *extack)
|
2016-12-16 04:13:24 +08:00
|
|
|
{
|
|
|
|
unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct bpf_prog *old_prog;
|
2017-02-20 11:50:20 +08:00
|
|
|
u16 xdp_qp = 0, curr_qp;
|
2016-12-16 04:13:49 +08:00
|
|
|
int i, err;
|
2016-12-16 04:13:24 +08:00
|
|
|
|
2017-07-19 16:54:49 +08:00
|
|
|
if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
|
|
|
|
&& (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
|
|
|
|
virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
|
|
|
|
virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
|
2018-11-22 14:36:31 +08:00
|
|
|
virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
|
|
|
|
virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
|
|
|
|
NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO/CSUM, disable LRO/CSUM first");
|
2016-12-16 04:13:24 +08:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
|
2017-05-03 06:39:17 +08:00
|
|
|
NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
|
2016-12-16 04:13:24 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->mtu > max_sz) {
|
2017-05-03 06:39:17 +08:00
|
|
|
NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
|
2016-12-16 04:13:24 +08:00
|
|
|
netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-12-16 04:13:49 +08:00
|
|
|
curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
|
|
|
|
if (prog)
|
|
|
|
xdp_qp = nr_cpu_ids;
|
|
|
|
|
|
|
|
/* XDP requires extra queues for XDP_TX */
|
|
|
|
if (curr_qp + xdp_qp > vi->max_queue_pairs) {
|
2017-05-03 06:39:17 +08:00
|
|
|
NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available");
|
2016-12-16 04:13:49 +08:00
|
|
|
netdev_warn(dev, "request %i queues but max is %i\n",
|
|
|
|
curr_qp + xdp_qp, vi->max_queue_pairs);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2019-01-29 08:45:57 +08:00
|
|
|
old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
|
|
|
|
if (!prog && !old_prog)
|
|
|
|
return 0;
|
|
|
|
|
2019-11-18 01:28:03 +08:00
|
|
|
if (prog)
|
|
|
|
bpf_prog_add(prog, vi->max_queue_pairs - 1);
|
2017-02-03 11:16:29 +08:00
|
|
|
|
2017-07-19 16:54:48 +08:00
|
|
|
/* Make sure NAPI is not using any XDP TX queues for RX. */
|
virtio_net: Don't call free_old_xmit_skbs for xdp_frames
When napi_tx is enabled, virtnet_poll_cleantx() called
free_old_xmit_skbs() even for xdp send queue.
This is bogus since the queue has xdp_frames, not sk_buffs, thus mangled
device tx bytes counters because skb->len is meaningless value, and even
triggered oops due to general protection fault on freeing them.
Since xdp send queues do not aquire locks, old xdp_frames should be
freed only in virtnet_xdp_xmit(), so just skip free_old_xmit_skbs() for
xdp send queues.
Similarly virtnet_poll_tx() called free_old_xmit_skbs(). This NAPI
handler is called even without calling start_xmit() because cb for tx is
by default enabled. Once the handler is called, it enabled the cb again,
and then the handler would be called again. We don't need this handler
for XDP, so don't enable cb as well as not calling free_old_xmit_skbs().
Also, we need to disable tx NAPI when disabling XDP, so
virtnet_poll_tx() can safely access curr_queue_pairs and
xdp_queue_pairs, which are not atomically updated while disabling XDP.
Fixes: b92f1e6751a6 ("virtio-net: transmit napi")
Fixes: 7b0411ef4aa6 ("virtio-net: clean tx descriptors from rx napi")
Signed-off-by: Toshiaki Makita <makita.toshiaki@lab.ntt.co.jp>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-01-29 08:45:54 +08:00
|
|
|
if (netif_running(dev)) {
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
2018-02-28 18:20:04 +08:00
|
|
|
napi_disable(&vi->rq[i].napi);
|
virtio_net: Don't call free_old_xmit_skbs for xdp_frames
When napi_tx is enabled, virtnet_poll_cleantx() called
free_old_xmit_skbs() even for xdp send queue.
This is bogus since the queue has xdp_frames, not sk_buffs, thus mangled
device tx bytes counters because skb->len is meaningless value, and even
triggered oops due to general protection fault on freeing them.
Since xdp send queues do not aquire locks, old xdp_frames should be
freed only in virtnet_xdp_xmit(), so just skip free_old_xmit_skbs() for
xdp send queues.
Similarly virtnet_poll_tx() called free_old_xmit_skbs(). This NAPI
handler is called even without calling start_xmit() because cb for tx is
by default enabled. Once the handler is called, it enabled the cb again,
and then the handler would be called again. We don't need this handler
for XDP, so don't enable cb as well as not calling free_old_xmit_skbs().
Also, we need to disable tx NAPI when disabling XDP, so
virtnet_poll_tx() can safely access curr_queue_pairs and
xdp_queue_pairs, which are not atomically updated while disabling XDP.
Fixes: b92f1e6751a6 ("virtio-net: transmit napi")
Fixes: 7b0411ef4aa6 ("virtio-net: clean tx descriptors from rx napi")
Signed-off-by: Toshiaki Makita <makita.toshiaki@lab.ntt.co.jp>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-01-29 08:45:54 +08:00
|
|
|
virtnet_napi_tx_disable(&vi->sq[i].napi);
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 04:13:24 +08:00
|
|
|
|
2019-01-29 08:45:57 +08:00
|
|
|
if (!prog) {
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
|
|
|
|
if (i == 0)
|
|
|
|
virtnet_restore_guest_offloads(vi);
|
|
|
|
}
|
|
|
|
synchronize_net();
|
|
|
|
}
|
2016-12-16 04:13:24 +08:00
|
|
|
|
2017-07-19 16:54:48 +08:00
|
|
|
err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
|
|
|
|
if (err)
|
|
|
|
goto err;
|
2019-01-29 08:45:55 +08:00
|
|
|
netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
|
2017-07-19 16:54:48 +08:00
|
|
|
vi->xdp_queue_pairs = xdp_qp;
|
2016-12-16 04:13:49 +08:00
|
|
|
|
2019-01-29 08:45:57 +08:00
|
|
|
if (prog) {
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
|
|
|
|
if (i == 0 && !old_prog)
|
2017-07-19 16:54:49 +08:00
|
|
|
virtnet_clear_guest_offloads(vi);
|
|
|
|
}
|
2019-01-29 08:45:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
2016-12-16 04:13:24 +08:00
|
|
|
if (old_prog)
|
|
|
|
bpf_prog_put(old_prog);
|
virtio_net: Don't call free_old_xmit_skbs for xdp_frames
When napi_tx is enabled, virtnet_poll_cleantx() called
free_old_xmit_skbs() even for xdp send queue.
This is bogus since the queue has xdp_frames, not sk_buffs, thus mangled
device tx bytes counters because skb->len is meaningless value, and even
triggered oops due to general protection fault on freeing them.
Since xdp send queues do not aquire locks, old xdp_frames should be
freed only in virtnet_xdp_xmit(), so just skip free_old_xmit_skbs() for
xdp send queues.
Similarly virtnet_poll_tx() called free_old_xmit_skbs(). This NAPI
handler is called even without calling start_xmit() because cb for tx is
by default enabled. Once the handler is called, it enabled the cb again,
and then the handler would be called again. We don't need this handler
for XDP, so don't enable cb as well as not calling free_old_xmit_skbs().
Also, we need to disable tx NAPI when disabling XDP, so
virtnet_poll_tx() can safely access curr_queue_pairs and
xdp_queue_pairs, which are not atomically updated while disabling XDP.
Fixes: b92f1e6751a6 ("virtio-net: transmit napi")
Fixes: 7b0411ef4aa6 ("virtio-net: clean tx descriptors from rx napi")
Signed-off-by: Toshiaki Makita <makita.toshiaki@lab.ntt.co.jp>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-01-29 08:45:54 +08:00
|
|
|
if (netif_running(dev)) {
|
2018-02-28 18:20:04 +08:00
|
|
|
virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
|
virtio_net: Don't call free_old_xmit_skbs for xdp_frames
When napi_tx is enabled, virtnet_poll_cleantx() called
free_old_xmit_skbs() even for xdp send queue.
This is bogus since the queue has xdp_frames, not sk_buffs, thus mangled
device tx bytes counters because skb->len is meaningless value, and even
triggered oops due to general protection fault on freeing them.
Since xdp send queues do not aquire locks, old xdp_frames should be
freed only in virtnet_xdp_xmit(), so just skip free_old_xmit_skbs() for
xdp send queues.
Similarly virtnet_poll_tx() called free_old_xmit_skbs(). This NAPI
handler is called even without calling start_xmit() because cb for tx is
by default enabled. Once the handler is called, it enabled the cb again,
and then the handler would be called again. We don't need this handler
for XDP, so don't enable cb as well as not calling free_old_xmit_skbs().
Also, we need to disable tx NAPI when disabling XDP, so
virtnet_poll_tx() can safely access curr_queue_pairs and
xdp_queue_pairs, which are not atomically updated while disabling XDP.
Fixes: b92f1e6751a6 ("virtio-net: transmit napi")
Fixes: 7b0411ef4aa6 ("virtio-net: clean tx descriptors from rx napi")
Signed-off-by: Toshiaki Makita <makita.toshiaki@lab.ntt.co.jp>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-01-29 08:45:54 +08:00
|
|
|
virtnet_napi_tx_enable(vi, vi->sq[i].vq,
|
|
|
|
&vi->sq[i].napi);
|
|
|
|
}
|
2016-12-16 04:13:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-02-03 11:16:29 +08:00
|
|
|
|
2017-07-19 16:54:48 +08:00
|
|
|
err:
|
2019-01-29 08:45:57 +08:00
|
|
|
if (!prog) {
|
|
|
|
virtnet_clear_guest_offloads(vi);
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++)
|
|
|
|
rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
|
|
|
|
}
|
|
|
|
|
2019-01-29 08:45:53 +08:00
|
|
|
if (netif_running(dev)) {
|
virtio_net: Don't call free_old_xmit_skbs for xdp_frames
When napi_tx is enabled, virtnet_poll_cleantx() called
free_old_xmit_skbs() even for xdp send queue.
This is bogus since the queue has xdp_frames, not sk_buffs, thus mangled
device tx bytes counters because skb->len is meaningless value, and even
triggered oops due to general protection fault on freeing them.
Since xdp send queues do not aquire locks, old xdp_frames should be
freed only in virtnet_xdp_xmit(), so just skip free_old_xmit_skbs() for
xdp send queues.
Similarly virtnet_poll_tx() called free_old_xmit_skbs(). This NAPI
handler is called even without calling start_xmit() because cb for tx is
by default enabled. Once the handler is called, it enabled the cb again,
and then the handler would be called again. We don't need this handler
for XDP, so don't enable cb as well as not calling free_old_xmit_skbs().
Also, we need to disable tx NAPI when disabling XDP, so
virtnet_poll_tx() can safely access curr_queue_pairs and
xdp_queue_pairs, which are not atomically updated while disabling XDP.
Fixes: b92f1e6751a6 ("virtio-net: transmit napi")
Fixes: 7b0411ef4aa6 ("virtio-net: clean tx descriptors from rx napi")
Signed-off-by: Toshiaki Makita <makita.toshiaki@lab.ntt.co.jp>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-01-29 08:45:54 +08:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
2019-01-29 08:45:53 +08:00
|
|
|
virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
|
virtio_net: Don't call free_old_xmit_skbs for xdp_frames
When napi_tx is enabled, virtnet_poll_cleantx() called
free_old_xmit_skbs() even for xdp send queue.
This is bogus since the queue has xdp_frames, not sk_buffs, thus mangled
device tx bytes counters because skb->len is meaningless value, and even
triggered oops due to general protection fault on freeing them.
Since xdp send queues do not aquire locks, old xdp_frames should be
freed only in virtnet_xdp_xmit(), so just skip free_old_xmit_skbs() for
xdp send queues.
Similarly virtnet_poll_tx() called free_old_xmit_skbs(). This NAPI
handler is called even without calling start_xmit() because cb for tx is
by default enabled. Once the handler is called, it enabled the cb again,
and then the handler would be called again. We don't need this handler
for XDP, so don't enable cb as well as not calling free_old_xmit_skbs().
Also, we need to disable tx NAPI when disabling XDP, so
virtnet_poll_tx() can safely access curr_queue_pairs and
xdp_queue_pairs, which are not atomically updated while disabling XDP.
Fixes: b92f1e6751a6 ("virtio-net: transmit napi")
Fixes: 7b0411ef4aa6 ("virtio-net: clean tx descriptors from rx napi")
Signed-off-by: Toshiaki Makita <makita.toshiaki@lab.ntt.co.jp>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-01-29 08:45:54 +08:00
|
|
|
virtnet_napi_tx_enable(vi, vi->sq[i].vq,
|
|
|
|
&vi->sq[i].napi);
|
|
|
|
}
|
2019-01-29 08:45:53 +08:00
|
|
|
}
|
2017-02-03 11:16:29 +08:00
|
|
|
if (prog)
|
|
|
|
bpf_prog_sub(prog, vi->max_queue_pairs - 1);
|
|
|
|
return err;
|
2016-12-16 04:13:24 +08:00
|
|
|
}
|
|
|
|
|
2017-11-04 04:56:16 +08:00
|
|
|
static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
|
2016-12-16 04:13:24 +08:00
|
|
|
{
|
|
|
|
switch (xdp->command) {
|
|
|
|
case XDP_SETUP_PROG:
|
2017-05-01 12:46:48 +08:00
|
|
|
return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
|
2016-12-16 04:13:24 +08:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 00:55:17 +08:00
|
|
|
static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
ret = snprintf(buf, len, "sby");
|
|
|
|
if (ret >= len)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-21 06:14:54 +08:00
|
|
|
static int virtnet_set_features(struct net_device *dev,
|
|
|
|
netdev_features_t features)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
u64 offloads;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((dev->features ^ features) & NETIF_F_LRO) {
|
|
|
|
if (vi->xdp_queue_pairs)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
if (features & NETIF_F_LRO)
|
|
|
|
offloads = vi->guest_offloads_capable;
|
|
|
|
else
|
|
|
|
offloads = 0;
|
|
|
|
|
|
|
|
err = virtnet_set_guest_offloads(vi, offloads);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
vi->guest_offloads = offloads;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-07 02:44:22 +08:00
|
|
|
static const struct net_device_ops virtnet_netdev = {
|
|
|
|
.ndo_open = virtnet_open,
|
|
|
|
.ndo_stop = virtnet_close,
|
|
|
|
.ndo_start_xmit = start_xmit,
|
|
|
|
.ndo_validate_addr = eth_validate_addr,
|
2009-02-05 08:36:34 +08:00
|
|
|
.ndo_set_mac_address = virtnet_set_mac_address,
|
2009-02-04 17:02:40 +08:00
|
|
|
.ndo_set_rx_mode = virtnet_set_rx_mode,
|
2011-06-15 14:36:29 +08:00
|
|
|
.ndo_get_stats64 = virtnet_stats,
|
2009-05-02 01:31:10 +08:00
|
|
|
.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
|
|
|
|
.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
|
2017-11-04 04:56:16 +08:00
|
|
|
.ndo_bpf = virtnet_xdp,
|
2017-09-19 17:42:43 +08:00
|
|
|
.ndo_xdp_xmit = virtnet_xdp_xmit,
|
2017-05-24 01:38:43 +08:00
|
|
|
.ndo_features_check = passthru_features_check,
|
2018-05-25 00:55:17 +08:00
|
|
|
.ndo_get_phys_port_name = virtnet_get_phys_port_name,
|
2018-12-21 06:14:54 +08:00
|
|
|
.ndo_set_features = virtnet_set_features,
|
2009-01-07 02:44:22 +08:00
|
|
|
};
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
static void virtnet_config_changed_work(struct work_struct *work)
|
2009-01-20 09:09:49 +08:00
|
|
|
{
|
2012-04-12 04:43:52 +08:00
|
|
|
struct virtnet_info *vi =
|
|
|
|
container_of(work, struct virtnet_info, config_work);
|
2009-01-20 09:09:49 +08:00
|
|
|
u16 v;
|
|
|
|
|
2013-10-14 15:41:51 +08:00
|
|
|
if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
|
|
|
|
struct virtio_net_config, status, &v) < 0)
|
2014-10-15 07:52:30 +08:00
|
|
|
return;
|
2012-04-12 04:43:52 +08:00
|
|
|
|
|
|
|
if (v & VIRTIO_NET_S_ANNOUNCE) {
|
2012-08-10 06:14:56 +08:00
|
|
|
netdev_notify_peers(vi->dev);
|
2012-04-12 04:43:52 +08:00
|
|
|
virtnet_ack_link_announce(vi);
|
|
|
|
}
|
2009-01-20 09:09:49 +08:00
|
|
|
|
|
|
|
/* Ignore unknown (future) status bits */
|
|
|
|
v &= VIRTIO_NET_S_LINK_UP;
|
|
|
|
|
|
|
|
if (vi->status == v)
|
2014-10-15 07:52:30 +08:00
|
|
|
return;
|
2009-01-20 09:09:49 +08:00
|
|
|
|
|
|
|
vi->status = v;
|
|
|
|
|
|
|
|
if (vi->status & VIRTIO_NET_S_LINK_UP) {
|
2018-01-06 06:44:54 +08:00
|
|
|
virtnet_update_settings(vi);
|
2009-01-20 09:09:49 +08:00
|
|
|
netif_carrier_on(vi->dev);
|
2012-12-07 15:04:56 +08:00
|
|
|
netif_tx_wake_all_queues(vi->dev);
|
2009-01-20 09:09:49 +08:00
|
|
|
} else {
|
|
|
|
netif_carrier_off(vi->dev);
|
2012-12-07 15:04:56 +08:00
|
|
|
netif_tx_stop_all_queues(vi->dev);
|
2009-01-20 09:09:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtnet_config_changed(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
|
2012-08-21 05:51:24 +08:00
|
|
|
schedule_work(&vi->config_work);
|
2009-01-20 09:09:49 +08:00
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
static void virtnet_free_queues(struct virtnet_info *vi)
|
|
|
|
{
|
2013-12-05 22:36:21 +08:00
|
|
|
int i;
|
|
|
|
|
2015-03-12 13:57:44 +08:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
napi_hash_del(&vi->rq[i].napi);
|
2013-12-05 22:36:21 +08:00
|
|
|
netif_napi_del(&vi->rq[i].napi);
|
2017-04-25 01:49:27 +08:00
|
|
|
netif_napi_del(&vi->sq[i].napi);
|
2015-03-12 13:57:44 +08:00
|
|
|
}
|
2013-12-05 22:36:21 +08:00
|
|
|
|
2016-11-16 14:24:12 +08:00
|
|
|
/* We called napi_hash_del() before netif_napi_del(),
|
|
|
|
* we need to respect an RCU grace period before freeing vi->rq
|
|
|
|
*/
|
|
|
|
synchronize_net();
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
kfree(vi->rq);
|
|
|
|
kfree(vi->sq);
|
2018-04-19 13:30:48 +08:00
|
|
|
kfree(vi->ctrl);
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
2017-02-03 11:14:32 +08:00
|
|
|
static void _free_receive_bufs(struct virtnet_info *vi)
|
2012-12-07 15:04:56 +08:00
|
|
|
{
|
2016-12-16 04:13:24 +08:00
|
|
|
struct bpf_prog *old_prog;
|
2012-12-07 15:04:56 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
while (vi->rq[i].pages)
|
|
|
|
__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
|
2016-12-16 04:13:24 +08:00
|
|
|
|
|
|
|
old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
|
|
|
|
RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
|
|
|
|
if (old_prog)
|
|
|
|
bpf_prog_put(old_prog);
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
2017-02-03 11:14:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void free_receive_bufs(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
rtnl_lock();
|
|
|
|
_free_receive_bufs(vi);
|
2016-12-16 04:13:24 +08:00
|
|
|
rtnl_unlock();
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
2014-01-17 14:23:26 +08:00
|
|
|
static void free_receive_page_frags(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++)
|
|
|
|
if (vi->rq[i].alloc_frag.page)
|
|
|
|
put_page(vi->rq[i].alloc_frag.page);
|
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
static void free_unused_bufs(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
void *buf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
struct virtqueue *vq = vi->sq[i].vq;
|
2016-12-16 04:14:13 +08:00
|
|
|
while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
|
2019-01-29 08:45:59 +08:00
|
|
|
if (!is_xdp_frame(buf))
|
2016-12-16 04:14:13 +08:00
|
|
|
dev_kfree_skb(buf);
|
|
|
|
else
|
2019-01-29 08:45:59 +08:00
|
|
|
xdp_return_frame(ptr_to_xdp(buf));
|
2016-12-16 04:14:13 +08:00
|
|
|
}
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
struct virtqueue *vq = vi->rq[i].vq;
|
|
|
|
|
|
|
|
while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
|
2014-01-17 14:23:27 +08:00
|
|
|
if (vi->mergeable_rx_bufs) {
|
2017-03-07 03:29:47 +08:00
|
|
|
put_page(virt_to_head_page(buf));
|
2014-01-17 14:23:27 +08:00
|
|
|
} else if (vi->big_packets) {
|
2013-12-05 22:36:20 +08:00
|
|
|
give_pages(&vi->rq[i], buf);
|
2014-01-17 14:23:27 +08:00
|
|
|
} else {
|
2017-02-21 16:46:28 +08:00
|
|
|
put_page(virt_to_head_page(buf));
|
2014-01-17 14:23:27 +08:00
|
|
|
}
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
static void virtnet_del_vqs(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
struct virtio_device *vdev = vi->vdev;
|
|
|
|
|
2019-03-18 14:56:06 +08:00
|
|
|
virtnet_clean_affinity(vi);
|
2012-12-07 15:04:56 +08:00
|
|
|
|
2012-12-07 15:04:55 +08:00
|
|
|
vdev->config->del_vqs(vdev);
|
2012-12-07 15:04:56 +08:00
|
|
|
|
|
|
|
virtnet_free_queues(vi);
|
2012-12-07 15:04:55 +08:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:21:21 +08:00
|
|
|
/* How large should a single buffer be so a queue full of these can fit at
|
|
|
|
* least one full packet?
|
|
|
|
* Logic below assumes the mergeable buffer header is used.
|
|
|
|
*/
|
|
|
|
static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
|
|
|
|
{
|
|
|
|
const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
|
|
|
|
unsigned int rq_size = virtqueue_get_vring_size(vq);
|
|
|
|
unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
|
|
|
|
unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
|
|
|
|
unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
|
|
|
|
|
2017-06-02 22:54:33 +08:00
|
|
|
return max(max(min_buf_len, hdr_len) - hdr_len,
|
|
|
|
(unsigned int)GOOD_PACKET_LEN);
|
2017-03-09 08:21:21 +08:00
|
|
|
}
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
static int virtnet_find_vqs(struct virtnet_info *vi)
|
2011-12-22 19:28:31 +08:00
|
|
|
{
|
2012-12-07 15:04:56 +08:00
|
|
|
vq_callback_t **callbacks;
|
|
|
|
struct virtqueue **vqs;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
int i, total_vqs;
|
|
|
|
const char **names;
|
2017-03-07 02:31:21 +08:00
|
|
|
bool *ctx;
|
2012-12-07 15:04:56 +08:00
|
|
|
|
|
|
|
/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
|
|
|
|
* possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
|
|
|
|
* possible control vq.
|
|
|
|
*/
|
|
|
|
total_vqs = vi->max_queue_pairs * 2 +
|
|
|
|
virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
|
|
|
|
|
|
|
|
/* Allocate space for find_vqs parameters */
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
|
2012-12-07 15:04:56 +08:00
|
|
|
if (!vqs)
|
|
|
|
goto err_vq;
|
treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
patch replaces cases of:
kmalloc(a * b, gfp)
with:
kmalloc_array(a * b, gfp)
as well as handling cases of:
kmalloc(a * b * c, gfp)
with:
kmalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kmalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kmalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The tools/ directory was manually excluded, since it has its own
implementation of kmalloc().
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kmalloc
+ kmalloc_array
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kmalloc(sizeof(THING) * C2, ...)
|
kmalloc(sizeof(TYPE) * C2, ...)
|
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(C1 * C2, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * E2
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 04:55:00 +08:00
|
|
|
callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
|
2012-12-07 15:04:56 +08:00
|
|
|
if (!callbacks)
|
|
|
|
goto err_callback;
|
treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
patch replaces cases of:
kmalloc(a * b, gfp)
with:
kmalloc_array(a * b, gfp)
as well as handling cases of:
kmalloc(a * b * c, gfp)
with:
kmalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kmalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kmalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The tools/ directory was manually excluded, since it has its own
implementation of kmalloc().
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kmalloc
+ kmalloc_array
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kmalloc(sizeof(THING) * C2, ...)
|
kmalloc(sizeof(TYPE) * C2, ...)
|
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(C1 * C2, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * E2
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 04:55:00 +08:00
|
|
|
names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
|
2012-12-07 15:04:56 +08:00
|
|
|
if (!names)
|
|
|
|
goto err_names;
|
2017-07-19 16:54:47 +08:00
|
|
|
if (!vi->big_packets || vi->mergeable_rx_bufs) {
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
|
2017-03-07 02:31:21 +08:00
|
|
|
if (!ctx)
|
|
|
|
goto err_ctx;
|
|
|
|
} else {
|
|
|
|
ctx = NULL;
|
|
|
|
}
|
2012-12-07 15:04:56 +08:00
|
|
|
|
|
|
|
/* Parameters for control virtqueue, if any */
|
|
|
|
if (vi->has_cvq) {
|
|
|
|
callbacks[total_vqs - 1] = NULL;
|
|
|
|
names[total_vqs - 1] = "control";
|
|
|
|
}
|
2011-12-22 19:28:31 +08:00
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
/* Allocate/initialize parameters for send/receive virtqueues */
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
callbacks[rxq2vq(i)] = skb_recv_done;
|
|
|
|
callbacks[txq2vq(i)] = skb_xmit_done;
|
|
|
|
sprintf(vi->rq[i].name, "input.%d", i);
|
|
|
|
sprintf(vi->sq[i].name, "output.%d", i);
|
|
|
|
names[rxq2vq(i)] = vi->rq[i].name;
|
|
|
|
names[txq2vq(i)] = vi->sq[i].name;
|
2017-03-07 02:31:21 +08:00
|
|
|
if (ctx)
|
|
|
|
ctx[rxq2vq(i)] = true;
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
2011-12-22 19:28:31 +08:00
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
|
2017-03-07 02:31:21 +08:00
|
|
|
names, ctx, NULL);
|
2012-12-07 15:04:56 +08:00
|
|
|
if (ret)
|
|
|
|
goto err_find;
|
2011-12-22 19:28:31 +08:00
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
if (vi->has_cvq) {
|
|
|
|
vi->cvq = vqs[total_vqs - 1];
|
2011-12-22 19:28:31 +08:00
|
|
|
if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
|
2013-04-19 10:04:27 +08:00
|
|
|
vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
|
2011-12-22 19:28:31 +08:00
|
|
|
}
|
2012-12-07 15:04:56 +08:00
|
|
|
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
vi->rq[i].vq = vqs[rxq2vq(i)];
|
2017-03-09 08:21:21 +08:00
|
|
|
vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
|
2012-12-07 15:04:56 +08:00
|
|
|
vi->sq[i].vq = vqs[txq2vq(i)];
|
|
|
|
}
|
|
|
|
|
2018-05-31 22:16:32 +08:00
|
|
|
/* run here: ret == 0. */
|
2012-12-07 15:04:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
err_find:
|
2017-03-07 02:31:21 +08:00
|
|
|
kfree(ctx);
|
|
|
|
err_ctx:
|
2012-12-07 15:04:56 +08:00
|
|
|
kfree(names);
|
|
|
|
err_names:
|
|
|
|
kfree(callbacks);
|
|
|
|
err_callback:
|
|
|
|
kfree(vqs);
|
|
|
|
err_vq:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_alloc_queues(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2018-04-19 13:30:48 +08:00
|
|
|
vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
|
|
|
|
if (!vi->ctrl)
|
|
|
|
goto err_ctrl;
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
|
2012-12-07 15:04:56 +08:00
|
|
|
if (!vi->sq)
|
|
|
|
goto err_sq;
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
|
2012-12-10 10:24:08 +08:00
|
|
|
if (!vi->rq)
|
2012-12-07 15:04:56 +08:00
|
|
|
goto err_rq;
|
|
|
|
|
|
|
|
INIT_DELAYED_WORK(&vi->refill, refill_work);
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
vi->rq[i].pages = NULL;
|
|
|
|
netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
|
|
|
|
napi_weight);
|
2017-04-28 08:37:58 +08:00
|
|
|
netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
|
|
|
|
napi_tx ? napi_weight : 0);
|
2012-12-07 15:04:56 +08:00
|
|
|
|
|
|
|
sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
|
2015-08-19 15:48:40 +08:00
|
|
|
ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
|
2012-12-07 15:04:56 +08:00
|
|
|
sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
|
2018-01-17 14:38:25 +08:00
|
|
|
|
|
|
|
u64_stats_init(&vi->rq[i].stats.syncp);
|
|
|
|
u64_stats_init(&vi->sq[i].stats.syncp);
|
2012-12-07 15:04:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_rq:
|
|
|
|
kfree(vi->sq);
|
|
|
|
err_sq:
|
2018-04-19 13:30:48 +08:00
|
|
|
kfree(vi->ctrl);
|
|
|
|
err_ctrl:
|
2012-12-07 15:04:56 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init_vqs(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Allocate send & receive queues */
|
|
|
|
ret = virtnet_alloc_queues(vi);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = virtnet_find_vqs(vi);
|
|
|
|
if (ret)
|
|
|
|
goto err_free;
|
|
|
|
|
2013-01-25 07:51:29 +08:00
|
|
|
get_online_cpus();
|
2013-01-25 07:51:30 +08:00
|
|
|
virtnet_set_affinity(vi);
|
2013-01-25 07:51:29 +08:00
|
|
|
put_online_cpus();
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_free:
|
|
|
|
virtnet_free_queues(vi);
|
|
|
|
err:
|
|
|
|
return ret;
|
2011-12-22 19:28:31 +08:00
|
|
|
}
|
|
|
|
|
2014-01-17 14:23:30 +08:00
|
|
|
#ifdef CONFIG_SYSFS
|
|
|
|
static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
|
2017-08-19 04:46:24 +08:00
|
|
|
char *buf)
|
2014-01-17 14:23:30 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(queue->dev);
|
|
|
|
unsigned int queue_index = get_netdev_rx_queue_index(queue);
|
2018-03-02 17:29:14 +08:00
|
|
|
unsigned int headroom = virtnet_get_headroom(vi);
|
|
|
|
unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
|
2015-08-19 15:48:40 +08:00
|
|
|
struct ewma_pkt_len *avg;
|
2014-01-17 14:23:30 +08:00
|
|
|
|
|
|
|
BUG_ON(queue_index >= vi->max_queue_pairs);
|
|
|
|
avg = &vi->rq[queue_index].mrg_avg_pkt_len;
|
2017-03-09 08:21:21 +08:00
|
|
|
return sprintf(buf, "%u\n",
|
2018-03-02 17:29:14 +08:00
|
|
|
get_mergeable_buf_len(&vi->rq[queue_index], avg,
|
|
|
|
SKB_DATA_ALIGN(headroom + tailroom)));
|
2014-01-17 14:23:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
|
|
|
|
__ATTR_RO(mergeable_rx_buffer_size);
|
|
|
|
|
|
|
|
static struct attribute *virtio_net_mrg_rx_attrs[] = {
|
|
|
|
&mergeable_rx_buffer_size_attribute.attr,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct attribute_group virtio_net_mrg_rx_group = {
|
|
|
|
.name = "virtio_net",
|
|
|
|
.attrs = virtio_net_mrg_rx_attrs
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-11-20 17:03:05 +08:00
|
|
|
static bool virtnet_fail_on_feature(struct virtio_device *vdev,
|
|
|
|
unsigned int fbit,
|
|
|
|
const char *fname, const char *dname)
|
|
|
|
{
|
|
|
|
if (!virtio_has_feature(vdev, fbit))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
dev_err(&vdev->dev, "device advertises feature %s but not %s",
|
|
|
|
fname, dname);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
|
|
|
|
virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
|
|
|
|
|
|
|
|
static bool virtnet_validate_features(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
|
|
|
|
(VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
|
|
|
|
"VIRTIO_NET_F_CTRL_VQ") ||
|
|
|
|
VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
|
|
|
|
"VIRTIO_NET_F_CTRL_VQ") ||
|
|
|
|
VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
|
|
|
|
"VIRTIO_NET_F_CTRL_VQ") ||
|
|
|
|
VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
|
|
|
|
VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
|
|
|
|
"VIRTIO_NET_F_CTRL_VQ"))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-21 01:55:21 +08:00
|
|
|
#define MIN_MTU ETH_MIN_MTU
|
|
|
|
#define MAX_MTU ETH_MAX_MTU
|
|
|
|
|
2017-03-30 00:09:14 +08:00
|
|
|
static int virtnet_validate(struct virtio_device *vdev)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2015-01-12 22:23:37 +08:00
|
|
|
if (!vdev->config->get) {
|
|
|
|
dev_err(&vdev->dev, "%s failure: config access disabled\n",
|
|
|
|
__func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-11-20 17:03:05 +08:00
|
|
|
if (!virtnet_validate_features(vdev))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-03-30 00:09:14 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
|
|
|
|
int mtu = virtio_cread16(vdev,
|
|
|
|
offsetof(struct virtio_net_config,
|
|
|
|
mtu));
|
|
|
|
if (mtu < MIN_MTU)
|
|
|
|
__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_probe(struct virtio_device *vdev)
|
|
|
|
{
|
2018-01-17 14:38:25 +08:00
|
|
|
int i, err = -ENOMEM;
|
2017-03-30 00:09:14 +08:00
|
|
|
struct net_device *dev;
|
|
|
|
struct virtnet_info *vi;
|
|
|
|
u16 max_queue_pairs;
|
|
|
|
int mtu;
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
/* Find if host supports multiqueue virtio_net device */
|
2013-10-14 15:41:51 +08:00
|
|
|
err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
|
|
|
|
struct virtio_net_config,
|
|
|
|
max_virtqueue_pairs, &max_queue_pairs);
|
2012-12-07 15:04:56 +08:00
|
|
|
|
|
|
|
/* We need at least 2 queue's */
|
|
|
|
if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
|
|
|
|
max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
|
|
|
|
!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
|
|
|
|
max_queue_pairs = 1;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
|
|
|
/* Allocate ourselves a network device with room for our info */
|
2012-12-07 15:04:56 +08:00
|
|
|
dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
|
2007-10-22 09:03:37 +08:00
|
|
|
if (!dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Set up network device as normal. */
|
2012-06-29 13:10:06 +08:00
|
|
|
dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
|
2009-01-07 02:44:22 +08:00
|
|
|
dev->netdev_ops = &virtnet_netdev;
|
2007-10-22 09:03:37 +08:00
|
|
|
dev->features = NETIF_F_HIGHDMA;
|
2011-06-15 14:36:29 +08:00
|
|
|
|
2014-05-11 08:12:32 +08:00
|
|
|
dev->ethtool_ops = &virtnet_ethtool_ops;
|
2007-10-22 09:03:37 +08:00
|
|
|
SET_NETDEV_DEV(dev, &vdev->dev);
|
|
|
|
|
|
|
|
/* Do we support "hardware" checksums? */
|
2011-03-31 09:01:35 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
|
2007-10-22 09:03:37 +08:00
|
|
|
/* This opens up the world of extra features. */
|
2015-08-05 10:34:04 +08:00
|
|
|
dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
|
2011-03-31 09:01:35 +08:00
|
|
|
if (csum)
|
2015-08-05 10:34:04 +08:00
|
|
|
dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
|
2011-03-31 09:01:35 +08:00
|
|
|
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
|
2017-07-03 21:37:32 +08:00
|
|
|
dev->hw_features |= NETIF_F_TSO
|
2008-02-05 12:50:02 +08:00
|
|
|
| NETIF_F_TSO_ECN | NETIF_F_TSO6;
|
|
|
|
}
|
2008-05-03 10:50:46 +08:00
|
|
|
/* Individual feature bits: what can host handle? */
|
2011-03-31 09:01:35 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
|
|
|
|
dev->hw_features |= NETIF_F_TSO;
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
|
|
|
|
dev->hw_features |= NETIF_F_TSO6;
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
|
|
|
|
dev->hw_features |= NETIF_F_TSO_ECN;
|
|
|
|
|
2014-12-24 11:03:52 +08:00
|
|
|
dev->features |= NETIF_F_GSO_ROBUST;
|
|
|
|
|
2011-03-31 09:01:35 +08:00
|
|
|
if (gso)
|
2017-07-03 21:37:32 +08:00
|
|
|
dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
|
2011-03-31 09:01:35 +08:00
|
|
|
/* (!csum && gso) case will be fixed by register_netdev() */
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
2013-08-27 23:09:02 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
|
|
|
|
dev->features |= NETIF_F_RXCSUM;
|
2018-12-21 06:14:54 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
|
|
|
|
dev->features |= NETIF_F_LRO;
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
|
|
|
|
dev->hw_features |= NETIF_F_LRO;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2013-04-11 07:32:21 +08:00
|
|
|
dev->vlan_features = dev->features;
|
|
|
|
|
2016-10-21 01:55:21 +08:00
|
|
|
/* MTU range: 68 - 65535 */
|
|
|
|
dev->min_mtu = MIN_MTU;
|
|
|
|
dev->max_mtu = MAX_MTU;
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
/* Configuration may specify what MAC to use. Otherwise random. */
|
2013-10-14 15:41:51 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
|
|
|
|
virtio_cread_bytes(vdev,
|
|
|
|
offsetof(struct virtio_net_config, mac),
|
|
|
|
dev->dev_addr, dev->addr_len);
|
|
|
|
else
|
2012-02-15 14:45:39 +08:00
|
|
|
eth_hw_addr_random(dev);
|
2007-10-22 09:03:37 +08:00
|
|
|
|
|
|
|
/* Set up our device-specific information */
|
|
|
|
vi = netdev_priv(dev);
|
|
|
|
vi->dev = dev;
|
|
|
|
vi->vdev = vdev;
|
2008-02-18 17:02:51 +08:00
|
|
|
vdev->priv = vi;
|
2013-10-08 06:51:58 +08:00
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
INIT_WORK(&vi->config_work, virtnet_config_changed_work);
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2008-04-18 11:24:27 +08:00
|
|
|
/* If we can receive ANY GSO packets, we must allocate large ones. */
|
2009-12-03 15:58:21 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
|
2015-02-04 05:36:17 +08:00
|
|
|
virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
|
2008-04-18 11:24:27 +08:00
|
|
|
vi->big_packets = true;
|
|
|
|
|
2008-11-17 14:41:34 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
|
|
|
|
vi->mergeable_rx_bufs = true;
|
|
|
|
|
2014-10-24 05:24:03 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
|
2014-10-24 21:55:57 +08:00
|
|
|
vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
|
|
|
|
else
|
|
|
|
vi->hdr_len = sizeof(struct virtio_net_hdr);
|
|
|
|
|
2015-07-15 20:26:19 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
|
2013-07-25 08:50:23 +08:00
|
|
|
vi->any_header_sg = true;
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
|
|
|
|
vi->has_cvq = true;
|
|
|
|
|
2016-06-04 04:57:12 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
|
|
|
|
mtu = virtio_cread16(vdev,
|
|
|
|
offsetof(struct virtio_net_config,
|
|
|
|
mtu));
|
2016-10-26 04:12:12 +08:00
|
|
|
if (mtu < dev->min_mtu) {
|
2017-03-30 00:09:14 +08:00
|
|
|
/* Should never trigger: MTU was previously validated
|
|
|
|
* in virtnet_validate.
|
|
|
|
*/
|
2019-04-03 17:10:13 +08:00
|
|
|
dev_err(&vdev->dev,
|
|
|
|
"device MTU appears to have changed it is now %d < %d",
|
|
|
|
mtu, dev->min_mtu);
|
2018-01-17 14:38:25 +08:00
|
|
|
goto free;
|
2016-10-26 04:12:12 +08:00
|
|
|
}
|
2017-03-08 08:14:25 +08:00
|
|
|
|
2017-03-30 00:09:14 +08:00
|
|
|
dev->mtu = mtu;
|
|
|
|
dev->max_mtu = mtu;
|
|
|
|
|
2017-03-08 08:14:25 +08:00
|
|
|
/* TODO: size buffers correctly in this case. */
|
|
|
|
if (dev->mtu > ETH_DATA_LEN)
|
|
|
|
vi->big_packets = true;
|
2016-06-04 04:57:12 +08:00
|
|
|
}
|
|
|
|
|
2014-10-24 21:55:57 +08:00
|
|
|
if (vi->any_header_sg)
|
|
|
|
dev->needed_headroom = vi->hdr_len;
|
virtio-net: Set needed_headroom for virtio-net when VIRTIO_F_ANY_LAYOUT is true
This is a small supplement for commit e7428e95a06fb516fac1308bd0e176e27c0b9287
("virtio-net: put virtio-net header inline with data"). TCP packages have
enough room to put virtio-net header in, but UDP packages do not. By
setting dev->needed_headroom for virtio-net device, UDP packages could have
enough room.
For UDP packages, sk_buff is alloced in fun __ip_append_data. The size is
"alloclen + hh_len + 15", and "hh_len = LL_RESERVED_SPACE(rt-dst.dev);".
The Macro is defined as follows:
#define LL_RESERVED_SPACE(dev) \
((((dev)->hard_header_len+(dev)->needed_headroom)\
&~(HH_DATA_MOD - 1)) + HH_DATA_MOD)
By default, for UDP packages, after skb is allocated, only 16 bytes
reserved. And 2 bytes remained after mac header is set. That is not enough
to put virtio-net header in. If we set dev->needed_headroom to 12 or 10
(according to mergeable_rx_bufs is on or off ), more room can be reserved.
Then there is enough room for UDP packages to put the header in.
test result list as below:
guest and host: suse11sp3, netperf, intel 2.4GHz
+-------+---------+---------+---------+---------+
| | old | new |
+-------+---------+---------+---------+---------+
| UDP | Gbit/s | pps | Gbit/s | pps |
| 64 | 0.57 | 692232 | 0.61 | 742420 |
| 256 | 1.60 | 686860 | 1.71 | 733331 |
| 512 | 2.92 | 674576 | 3.07 | 710446 |
| 1024 | 4.99 | 598977 | 5.17 | 620821 |
| 1460 | 5.68 | 483757 | 7.16 | 610519 |
| 4096 | 6.98 | 637468 | 7.21 | 658471 |
+-------+---------+---------+---------+---------+
Signed-off-by: Zhang Jie <zhangjie14@huawei.com>
Acked-by: Rusty Russell <rusty@rustcorp.com.au>
Acked-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-04-29 18:43:22 +08:00
|
|
|
|
2016-11-25 12:37:26 +08:00
|
|
|
/* Enable multiqueue by default */
|
|
|
|
if (num_online_cpus() >= max_queue_pairs)
|
|
|
|
vi->curr_queue_pairs = max_queue_pairs;
|
|
|
|
else
|
|
|
|
vi->curr_queue_pairs = num_online_cpus();
|
2012-12-07 15:04:56 +08:00
|
|
|
vi->max_queue_pairs = max_queue_pairs;
|
|
|
|
|
|
|
|
/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
|
2011-12-22 19:28:31 +08:00
|
|
|
err = init_vqs(vi);
|
2009-06-13 12:16:36 +08:00
|
|
|
if (err)
|
2018-01-17 14:38:25 +08:00
|
|
|
goto free;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2014-01-17 14:23:30 +08:00
|
|
|
#ifdef CONFIG_SYSFS
|
|
|
|
if (vi->mergeable_rx_bufs)
|
|
|
|
dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
|
|
|
|
#endif
|
2013-11-18 21:19:27 +08:00
|
|
|
netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
|
|
|
|
netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
|
2012-12-07 15:04:56 +08:00
|
|
|
|
2016-02-03 11:04:37 +08:00
|
|
|
virtnet_init_settings(dev);
|
|
|
|
|
2018-05-25 00:55:17 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
|
|
|
|
vi->failover = net_failover_create(vi->dev);
|
2018-05-31 10:05:07 +08:00
|
|
|
if (IS_ERR(vi->failover)) {
|
|
|
|
err = PTR_ERR(vi->failover);
|
2018-05-25 00:55:17 +08:00
|
|
|
goto free_vqs;
|
2018-05-31 10:05:07 +08:00
|
|
|
}
|
2018-05-25 00:55:17 +08:00
|
|
|
}
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
err = register_netdev(dev);
|
|
|
|
if (err) {
|
|
|
|
pr_debug("virtio_net: registering device failed\n");
|
2018-05-25 00:55:17 +08:00
|
|
|
goto free_failover;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
2008-02-05 12:50:02 +08:00
|
|
|
|
2014-10-15 07:52:30 +08:00
|
|
|
virtio_device_ready(vdev);
|
|
|
|
|
2016-08-13 01:49:43 +08:00
|
|
|
err = virtnet_cpu_notif_add(vi);
|
2013-01-25 07:51:31 +08:00
|
|
|
if (err) {
|
|
|
|
pr_debug("virtio_net: registering cpu notifier failed\n");
|
2016-05-31 11:52:43 +08:00
|
|
|
goto free_unregister_netdev;
|
2013-01-25 07:51:31 +08:00
|
|
|
}
|
|
|
|
|
2016-12-13 14:23:05 +08:00
|
|
|
virtnet_set_queues(vi, vi->curr_queue_pairs);
|
2016-11-25 12:37:26 +08:00
|
|
|
|
2010-11-10 22:45:41 +08:00
|
|
|
/* Assume link up if device can't report link status,
|
|
|
|
otherwise get link status from config. */
|
2018-03-22 22:42:41 +08:00
|
|
|
netif_carrier_off(dev);
|
2010-11-10 22:45:41 +08:00
|
|
|
if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
|
2012-08-21 05:51:24 +08:00
|
|
|
schedule_work(&vi->config_work);
|
2010-11-10 22:45:41 +08:00
|
|
|
} else {
|
|
|
|
vi->status = VIRTIO_NET_S_LINK_UP;
|
2018-01-06 06:44:54 +08:00
|
|
|
virtnet_update_settings(vi);
|
2010-11-10 22:45:41 +08:00
|
|
|
netif_carrier_on(dev);
|
|
|
|
}
|
2009-01-20 09:09:49 +08:00
|
|
|
|
2017-07-19 16:54:49 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
|
|
|
|
if (virtio_has_feature(vi->vdev, guest_offloads[i]))
|
|
|
|
set_bit(guest_offloads[i], &vi->guest_offloads);
|
2018-12-21 06:14:54 +08:00
|
|
|
vi->guest_offloads_capable = vi->guest_offloads;
|
2017-07-19 16:54:49 +08:00
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
|
|
|
|
dev->name, max_queue_pairs);
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
return 0;
|
|
|
|
|
2016-05-31 11:52:43 +08:00
|
|
|
free_unregister_netdev:
|
2014-10-15 07:52:31 +08:00
|
|
|
vi->vdev->config->reset(vdev);
|
|
|
|
|
2008-02-05 12:50:02 +08:00
|
|
|
unregister_netdev(dev);
|
2018-05-25 00:55:17 +08:00
|
|
|
free_failover:
|
|
|
|
net_failover_destroy(vi->failover);
|
2009-06-13 12:16:36 +08:00
|
|
|
free_vqs:
|
2012-12-07 15:04:56 +08:00
|
|
|
cancel_delayed_work_sync(&vi->refill);
|
2014-01-17 14:23:26 +08:00
|
|
|
free_receive_page_frags(vi);
|
2012-12-07 15:04:55 +08:00
|
|
|
virtnet_del_vqs(vi);
|
2007-10-22 09:03:37 +08:00
|
|
|
free:
|
|
|
|
free_netdev(dev);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-12-22 19:28:32 +08:00
|
|
|
static void remove_vq_common(struct virtnet_info *vi)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2011-12-22 19:28:32 +08:00
|
|
|
vi->vdev->config->reset(vi->vdev);
|
2010-02-08 22:14:42 +08:00
|
|
|
|
|
|
|
/* Free unused buffers in both send and recv, if any. */
|
2010-01-29 11:20:04 +08:00
|
|
|
free_unused_bufs(vi);
|
2008-07-26 01:06:01 +08:00
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
free_receive_bufs(vi);
|
2009-06-13 12:16:36 +08:00
|
|
|
|
2014-01-17 14:23:26 +08:00
|
|
|
free_receive_page_frags(vi);
|
|
|
|
|
2012-12-07 15:04:56 +08:00
|
|
|
virtnet_del_vqs(vi);
|
2011-12-22 19:28:32 +08:00
|
|
|
}
|
|
|
|
|
2012-12-03 22:24:15 +08:00
|
|
|
static void virtnet_remove(struct virtio_device *vdev)
|
2011-12-22 19:28:32 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
|
2016-08-13 01:49:43 +08:00
|
|
|
virtnet_cpu_notif_remove(vi);
|
2013-01-25 07:51:31 +08:00
|
|
|
|
2014-10-15 07:52:29 +08:00
|
|
|
/* Make sure no work handler is accessing the device. */
|
|
|
|
flush_work(&vi->config_work);
|
2012-04-12 04:43:52 +08:00
|
|
|
|
2011-12-22 19:28:32 +08:00
|
|
|
unregister_netdev(vi->dev);
|
|
|
|
|
2018-05-25 00:55:17 +08:00
|
|
|
net_failover_destroy(vi->failover);
|
|
|
|
|
2011-12-22 19:28:32 +08:00
|
|
|
remove_vq_common(vi);
|
2008-07-26 01:06:01 +08:00
|
|
|
|
2007-11-20 00:20:42 +08:00
|
|
|
free_netdev(vi->dev);
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2017-07-25 23:35:50 +08:00
|
|
|
static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
|
2011-12-22 19:28:33 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
|
2016-08-13 01:49:43 +08:00
|
|
|
virtnet_cpu_notif_remove(vi);
|
2017-02-03 11:16:01 +08:00
|
|
|
virtnet_freeze_down(vdev);
|
2011-12-22 19:28:33 +08:00
|
|
|
remove_vq_common(vi);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-25 23:35:50 +08:00
|
|
|
static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
|
2011-12-22 19:28:33 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
2017-02-03 11:16:01 +08:00
|
|
|
int err;
|
2011-12-22 19:28:33 +08:00
|
|
|
|
2017-02-03 11:16:01 +08:00
|
|
|
err = virtnet_restore_up(vdev);
|
2011-12-22 19:28:33 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
2012-12-07 15:04:56 +08:00
|
|
|
virtnet_set_queues(vi, vi->curr_queue_pairs);
|
|
|
|
|
2016-08-13 01:49:43 +08:00
|
|
|
err = virtnet_cpu_notif_add(vi);
|
2013-10-29 15:11:07 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2011-12-22 19:28:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
static struct virtio_device_id id_table[] = {
|
|
|
|
{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
|
|
|
|
{ 0 },
|
|
|
|
};
|
|
|
|
|
2016-11-04 18:55:36 +08:00
|
|
|
#define VIRTNET_FEATURES \
|
|
|
|
VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
|
|
|
|
VIRTIO_NET_F_MAC, \
|
|
|
|
VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
|
|
|
|
VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
|
|
|
|
VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
|
|
|
|
VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
|
|
|
|
VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
|
|
|
|
VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
|
|
|
|
VIRTIO_NET_F_CTRL_MAC_ADDR, \
|
2018-01-06 06:44:54 +08:00
|
|
|
VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
|
2018-05-25 00:55:16 +08:00
|
|
|
VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
|
2016-11-04 18:55:36 +08:00
|
|
|
|
2008-05-03 10:50:50 +08:00
|
|
|
static unsigned int features[] = {
|
2016-11-04 18:55:36 +08:00
|
|
|
VIRTNET_FEATURES,
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned int features_legacy[] = {
|
|
|
|
VIRTNET_FEATURES,
|
|
|
|
VIRTIO_NET_F_GSO,
|
2013-07-25 08:50:23 +08:00
|
|
|
VIRTIO_F_ANY_LAYOUT,
|
2008-05-03 10:50:50 +08:00
|
|
|
};
|
|
|
|
|
2009-11-05 17:32:44 +08:00
|
|
|
static struct virtio_driver virtio_net_driver = {
|
2008-05-03 10:50:50 +08:00
|
|
|
.feature_table = features,
|
|
|
|
.feature_table_size = ARRAY_SIZE(features),
|
2016-11-04 18:55:36 +08:00
|
|
|
.feature_table_legacy = features_legacy,
|
|
|
|
.feature_table_size_legacy = ARRAY_SIZE(features_legacy),
|
2007-10-22 09:03:37 +08:00
|
|
|
.driver.name = KBUILD_MODNAME,
|
|
|
|
.driver.owner = THIS_MODULE,
|
|
|
|
.id_table = id_table,
|
2017-03-30 00:09:14 +08:00
|
|
|
.validate = virtnet_validate,
|
2007-10-22 09:03:37 +08:00
|
|
|
.probe = virtnet_probe,
|
2012-12-03 22:24:15 +08:00
|
|
|
.remove = virtnet_remove,
|
2009-01-20 09:09:49 +08:00
|
|
|
.config_changed = virtnet_config_changed,
|
2013-09-17 07:55:23 +08:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2011-12-22 19:28:33 +08:00
|
|
|
.freeze = virtnet_freeze,
|
|
|
|
.restore = virtnet_restore,
|
|
|
|
#endif
|
2007-10-22 09:03:37 +08:00
|
|
|
};
|
|
|
|
|
2016-08-13 01:49:43 +08:00
|
|
|
static __init int virtio_net_driver_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-12-22 03:19:54 +08:00
|
|
|
ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
|
2016-08-13 01:49:43 +08:00
|
|
|
virtnet_cpu_online,
|
|
|
|
virtnet_cpu_down_prep);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
virtionet_online = ret;
|
2016-12-22 03:19:54 +08:00
|
|
|
ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
|
2016-08-13 01:49:43 +08:00
|
|
|
NULL, virtnet_cpu_dead);
|
|
|
|
if (ret)
|
|
|
|
goto err_dead;
|
|
|
|
|
|
|
|
ret = register_virtio_driver(&virtio_net_driver);
|
|
|
|
if (ret)
|
|
|
|
goto err_virtio;
|
|
|
|
return 0;
|
|
|
|
err_virtio:
|
|
|
|
cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
|
|
|
|
err_dead:
|
|
|
|
cpuhp_remove_multi_state(virtionet_online);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
module_init(virtio_net_driver_init);
|
|
|
|
|
|
|
|
static __exit void virtio_net_driver_exit(void)
|
|
|
|
{
|
2017-07-24 21:38:32 +08:00
|
|
|
unregister_virtio_driver(&virtio_net_driver);
|
2016-08-13 01:49:43 +08:00
|
|
|
cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
|
|
|
|
cpuhp_remove_multi_state(virtionet_online);
|
|
|
|
}
|
|
|
|
module_exit(virtio_net_driver_exit);
|
2007-10-22 09:03:37 +08:00
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(virtio, id_table);
|
|
|
|
MODULE_DESCRIPTION("Virtio network driver");
|
|
|
|
MODULE_LICENSE("GPL");
|