2009-09-24 15:59:20 +00:00
|
|
|
/* A network driver using virtio.
|
2007-10-22 01:03:37 +00:00
|
|
|
*
|
|
|
|
* Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2013-12-06 14:28:47 +00:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 01:03:37 +00:00
|
|
|
*/
|
|
|
|
//#define DEBUG
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/etherdevice.h>
|
2008-04-18 03:21:42 +00:00
|
|
|
#include <linux/ethtool.h>
|
2007-10-22 01:03:37 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/virtio.h>
|
|
|
|
#include <linux/virtio_net.h>
|
2016-12-15 20:13:24 +00: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 01:28:18 +00:00
|
|
|
#include <linux/bpf_trace.h>
|
2007-10-22 01:03:37 +00:00
|
|
|
#include <linux/scatterlist.h>
|
2009-01-26 02:06:26 +00: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 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2013-01-24 23:51:31 +00:00
|
|
|
#include <linux/cpu.h>
|
2014-01-17 06:23:27 +00:00
|
|
|
#include <linux/average.h>
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2013-05-09 19:50:51 +00:00
|
|
|
static int napi_weight = NAPI_POLL_WEIGHT;
|
2007-12-16 13:19:43 +00:00
|
|
|
module_param(napi_weight, int, 0444);
|
|
|
|
|
2011-12-19 14:08:01 +00:00
|
|
|
static bool csum = true, gso = true;
|
2008-02-05 04:50:02 +00:00
|
|
|
module_param(csum, bool, 0444);
|
|
|
|
module_param(gso, bool, 0444);
|
|
|
|
|
2007-10-22 01:03:37 +00:00
|
|
|
/* FIXME: MTU in config. */
|
2013-11-14 18:41:04 +00:00
|
|
|
#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
|
2008-11-17 06:41:34 +00:00
|
|
|
#define GOOD_COPY_LEN 128
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2015-08-19 07:48:40 +00: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 06:23:27 +00:00
|
|
|
*/
|
2015-08-19 07:48:40 +00:00
|
|
|
DECLARE_EWMA(pkt_len, 1, 64)
|
2014-01-17 06:23:27 +00:00
|
|
|
|
2017-01-23 19:37:52 +00:00
|
|
|
/* With mergeable buffers we align buffer address and use the low bits to
|
|
|
|
* encode its true size. Buffer size is up to 1 page so we need to align to
|
|
|
|
* square root of page size to ensure we reserve enough bits to encode the true
|
|
|
|
* size.
|
|
|
|
*/
|
|
|
|
#define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2)
|
|
|
|
|
2014-01-17 06:23:27 +00:00
|
|
|
/* Minimum alignment for mergeable packet buffers. */
|
2017-01-23 19:37:52 +00:00
|
|
|
#define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \
|
|
|
|
1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT)
|
2014-01-17 06:23:27 +00:00
|
|
|
|
2011-11-14 14:17:08 +00:00
|
|
|
#define VIRTNET_DRIVER_VERSION "1.0.0"
|
2009-02-04 09:02:34 +00:00
|
|
|
|
2011-06-15 06:36:29 +00:00
|
|
|
struct virtnet_stats {
|
2012-06-05 22:35:24 +00:00
|
|
|
struct u64_stats_sync tx_syncp;
|
|
|
|
struct u64_stats_sync rx_syncp;
|
2011-06-15 06:36:29 +00:00
|
|
|
u64 tx_bytes;
|
|
|
|
u64 tx_packets;
|
|
|
|
|
|
|
|
u64 rx_bytes;
|
|
|
|
u64 rx_packets;
|
|
|
|
};
|
|
|
|
|
2012-12-07 07:04:55 +00: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 07:04:56 +00:00
|
|
|
|
|
|
|
/* Name of the send queue: output.$index */
|
|
|
|
char name[40];
|
2012-12-07 07:04:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Internal representation of a receive virtqueue */
|
|
|
|
struct receive_queue {
|
|
|
|
/* Virtqueue associated with this receive_queue */
|
|
|
|
struct virtqueue *vq;
|
|
|
|
|
2007-10-22 01:03:37 +00:00
|
|
|
struct napi_struct napi;
|
|
|
|
|
2016-12-15 20:13:24 +00:00
|
|
|
struct bpf_prog __rcu *xdp_prog;
|
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
/* Chain pages by the private ptr. */
|
|
|
|
struct page *pages;
|
|
|
|
|
2014-01-17 06:23:27 +00:00
|
|
|
/* Average packet length for mergeable receive buffers. */
|
2015-08-19 07:48:40 +00:00
|
|
|
struct ewma_pkt_len mrg_avg_pkt_len;
|
2014-01-17 06:23:27 +00:00
|
|
|
|
2014-01-17 06:23:26 +00:00
|
|
|
/* Page frag for packet buffer allocation. */
|
|
|
|
struct page_frag alloc_frag;
|
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
/* RX: fragments + linear part + virtio header */
|
|
|
|
struct scatterlist sg[MAX_SKB_FRAGS + 2];
|
2012-12-07 07:04:56 +00:00
|
|
|
|
|
|
|
/* Name of this receive queue: input.$index */
|
|
|
|
char name[40];
|
2012-12-07 07:04:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct virtnet_info {
|
|
|
|
struct virtio_device *vdev;
|
|
|
|
struct virtqueue *cvq;
|
|
|
|
struct net_device *dev;
|
2012-12-07 07:04:56 +00:00
|
|
|
struct send_queue *sq;
|
|
|
|
struct receive_queue *rq;
|
2012-12-07 07:04:55 +00:00
|
|
|
unsigned int status;
|
|
|
|
|
2012-12-07 07:04:56 +00: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-15 20:13:49 +00:00
|
|
|
/* # of XDP queue pairs currently used by the driver */
|
|
|
|
u16 xdp_queue_pairs;
|
|
|
|
|
2008-04-18 03:24:27 +00:00
|
|
|
/* I like... big packets and I cannot lie! */
|
|
|
|
bool big_packets;
|
|
|
|
|
2008-11-17 06:41:34 +00:00
|
|
|
/* Host will merge rx buffers for big packets (shake it! shake it!) */
|
|
|
|
bool mergeable_rx_bufs;
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
/* Has control virtqueue */
|
|
|
|
bool has_cvq;
|
|
|
|
|
2013-07-25 00:50:23 +00:00
|
|
|
/* Host can handle any s/g split between our header and packet data */
|
|
|
|
bool any_header_sg;
|
|
|
|
|
2014-10-24 13:55:57 +00:00
|
|
|
/* Packet virtio header size */
|
|
|
|
u8 hdr_len;
|
|
|
|
|
2011-06-15 06:36:29 +00:00
|
|
|
/* Active statistics */
|
|
|
|
struct virtnet_stats __percpu *stats;
|
|
|
|
|
2009-08-26 19:22:32 +00:00
|
|
|
/* Work struct for refilling if we run low on memory. */
|
|
|
|
struct delayed_work refill;
|
|
|
|
|
2012-04-11 20:43:52 +00:00
|
|
|
/* Work struct for config space updates */
|
|
|
|
struct work_struct config_work;
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
/* Does the affinity hint is set for virtqueues? */
|
|
|
|
bool affinity_hint_set;
|
2013-01-24 23:51:29 +00:00
|
|
|
|
2016-08-12 17:49:43 +00:00
|
|
|
/* CPU hotplug instances for online & dead */
|
|
|
|
struct hlist_node node;
|
|
|
|
struct hlist_node node_dead;
|
2015-11-15 13:11:00 +00:00
|
|
|
|
|
|
|
/* Control VQ buffers: protected by the rtnl lock */
|
|
|
|
struct virtio_net_ctrl_hdr ctrl_hdr;
|
|
|
|
virtio_net_ctrl_ack ctrl_status;
|
2016-07-18 22:34:49 +00:00
|
|
|
struct virtio_net_ctrl_mq ctrl_mq;
|
2015-11-15 13:11:00 +00:00
|
|
|
u8 ctrl_promisc;
|
|
|
|
u8 ctrl_allmulti;
|
2016-07-18 22:34:49 +00:00
|
|
|
u16 ctrl_vid;
|
2016-02-03 03:04:37 +00:00
|
|
|
|
|
|
|
/* Ethtool settings */
|
|
|
|
u8 duplex;
|
|
|
|
u32 speed;
|
2007-10-22 01:03:37 +00:00
|
|
|
};
|
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
struct padded_vnet_hdr {
|
2014-10-24 13:55:57 +00:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf hdr;
|
2010-01-29 03:20:04 +00:00
|
|
|
/*
|
2014-10-24 13:55:57 +00: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 03:20:04 +00:00
|
|
|
*/
|
2014-10-24 13:55:57 +00:00
|
|
|
char padding[4];
|
2010-01-29 03:20:04 +00:00
|
|
|
};
|
|
|
|
|
2012-12-07 07:04:56 +00: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 14:17:34 +00:00
|
|
|
return (vq->index - 1) / 2;
|
2012-12-07 07:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int txq2vq(int txq)
|
|
|
|
{
|
|
|
|
return txq * 2 + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vq2rxq(struct virtqueue *vq)
|
|
|
|
{
|
2013-03-21 14:17:34 +00:00
|
|
|
return vq->index / 2;
|
2012-12-07 07:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rxq2vq(int rxq)
|
|
|
|
{
|
|
|
|
return rxq * 2;
|
|
|
|
}
|
|
|
|
|
2014-10-24 13:55:57 +00:00
|
|
|
static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
|
2007-10-22 01:03:37 +00:00
|
|
|
{
|
2014-10-24 13:55:57 +00:00
|
|
|
return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
|
2007-10-22 01:03:37 +00:00
|
|
|
}
|
|
|
|
|
2010-01-29 03:20:04 +00: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 07:04:55 +00:00
|
|
|
static void give_pages(struct receive_queue *rq, struct page *page)
|
2008-11-17 06:39:18 +00:00
|
|
|
{
|
2010-01-29 03:20:04 +00:00
|
|
|
struct page *end;
|
2008-11-17 06:39:18 +00:00
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
/* Find end of list, sew whole thing into vi->rq.pages. */
|
2010-01-29 03:20:04 +00:00
|
|
|
for (end = page; end->private; end = (struct page *)end->private);
|
2012-12-07 07:04:55 +00:00
|
|
|
end->private = (unsigned long)rq->pages;
|
|
|
|
rq->pages = page;
|
2008-11-17 06:39:18 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
|
2008-07-25 17:06:01 +00:00
|
|
|
{
|
2012-12-07 07:04:55 +00:00
|
|
|
struct page *p = rq->pages;
|
2008-07-25 17:06:01 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
if (p) {
|
2012-12-07 07:04:55 +00:00
|
|
|
rq->pages = (struct page *)p->private;
|
2010-01-29 03:20:04 +00:00
|
|
|
/* clear private here, it is used to chain pages */
|
|
|
|
p->private = 0;
|
|
|
|
} else
|
2008-07-25 17:06:01 +00:00
|
|
|
p = alloc_page(gfp_mask);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
static void skb_xmit_done(struct virtqueue *vq)
|
2007-10-22 01:03:37 +00:00
|
|
|
{
|
2012-12-07 07:04:55 +00:00
|
|
|
struct virtnet_info *vi = vq->vdev->priv;
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2008-02-05 04:50:07 +00:00
|
|
|
/* Suppress further interrupts. */
|
2012-12-07 07:04:55 +00:00
|
|
|
virtqueue_disable_cb(vq);
|
2008-05-26 07:48:13 +00:00
|
|
|
|
2008-06-08 10:51:55 +00:00
|
|
|
/* We were probably waiting for more output buffers. */
|
2012-12-07 07:04:56 +00:00
|
|
|
netif_wake_subqueue(vi->dev, vq2txq(vq));
|
2007-10-22 01:03:37 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 06:23:27 +00:00
|
|
|
static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
|
|
|
|
{
|
|
|
|
unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
|
|
|
|
return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
|
|
|
|
{
|
|
|
|
return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
|
|
|
|
{
|
|
|
|
unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
|
|
|
|
return (unsigned long)buf | (size - 1);
|
|
|
|
}
|
|
|
|
|
2012-01-04 12:52:32 +00:00
|
|
|
/* Called from bottom half context */
|
2014-10-23 21:12:10 +00:00
|
|
|
static struct sk_buff *page_to_skb(struct virtnet_info *vi,
|
|
|
|
struct receive_queue *rq,
|
2013-10-28 22:44:18 +00:00
|
|
|
struct page *page, unsigned int offset,
|
|
|
|
unsigned int len, unsigned int truesize)
|
2010-01-29 03:20:04 +00:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
2014-10-24 13:55:57 +00:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr;
|
2013-10-28 22:44:18 +00:00
|
|
|
unsigned int copy, hdr_len, hdr_padded_len;
|
2010-01-29 03:20:04 +00:00
|
|
|
char *p;
|
2008-07-25 17:06:01 +00:00
|
|
|
|
2013-10-28 22:44:18 +00:00
|
|
|
p = page_address(page) + offset;
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
/* copy small packet so we can reuse these pages for small data */
|
2016-03-17 14:44:00 +00:00
|
|
|
skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
|
2010-01-29 03:20:04 +00:00
|
|
|
if (unlikely(!skb))
|
|
|
|
return NULL;
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
hdr = skb_vnet_hdr(skb);
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2014-10-24 13:55:57 +00:00
|
|
|
hdr_len = vi->hdr_len;
|
|
|
|
if (vi->mergeable_rx_bufs)
|
|
|
|
hdr_padded_len = sizeof *hdr;
|
|
|
|
else
|
2013-10-28 22:44:18 +00:00
|
|
|
hdr_padded_len = sizeof(struct padded_vnet_hdr);
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
memcpy(hdr, p, hdr_len);
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
len -= hdr_len;
|
2013-10-28 22:44:18 +00:00
|
|
|
offset += hdr_padded_len;
|
|
|
|
p += hdr_padded_len;
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
copy = len;
|
|
|
|
if (copy > skb_tailroom(skb))
|
|
|
|
copy = skb_tailroom(skb);
|
|
|
|
memcpy(skb_put(skb, copy), p, copy);
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
len -= copy;
|
|
|
|
offset += copy;
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2013-10-28 22:44:18 +00: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 04:40:54 +00: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-08 17:47:28 +00:00
|
|
|
net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
|
2011-09-28 04:40:54 +00:00
|
|
|
dev_kfree_skb(skb);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-28 22:44:18 +00:00
|
|
|
BUG_ON(offset >= PAGE_SIZE);
|
2010-01-29 03:20:04 +00:00
|
|
|
while (len) {
|
2013-10-28 22:44:18 +00: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 03:20:04 +00:00
|
|
|
page = (struct page *)page->private;
|
|
|
|
offset = 0;
|
|
|
|
}
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
if (page)
|
2012-12-07 07:04:55 +00:00
|
|
|
give_pages(rq, page);
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
return skb;
|
|
|
|
}
|
2008-11-17 06:41:34 +00:00
|
|
|
|
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 01:28:18 +00:00
|
|
|
static bool virtnet_xdp_xmit(struct virtnet_info *vi,
|
2016-12-15 20:14:13 +00:00
|
|
|
struct receive_queue *rq,
|
|
|
|
struct send_queue *sq,
|
2016-12-23 14:37:32 +00:00
|
|
|
struct xdp_buff *xdp,
|
|
|
|
void *data)
|
2016-12-15 20:14:13 +00:00
|
|
|
{
|
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr;
|
|
|
|
unsigned int num_sg, len;
|
|
|
|
void *xdp_sent;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* Free up any pending old buffers before queueing new ones. */
|
|
|
|
while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
|
2016-12-23 14:37:32 +00:00
|
|
|
if (vi->mergeable_rx_bufs) {
|
|
|
|
struct page *sent_page = virt_to_head_page(xdp_sent);
|
|
|
|
|
|
|
|
put_page(sent_page);
|
|
|
|
} else { /* small buffer */
|
|
|
|
struct sk_buff *skb = xdp_sent;
|
2016-12-15 20:14:13 +00:00
|
|
|
|
2016-12-23 14:37:32 +00:00
|
|
|
kfree_skb(skb);
|
|
|
|
}
|
|
|
|
}
|
2016-12-15 20:14:13 +00:00
|
|
|
|
2016-12-23 14:37:32 +00:00
|
|
|
if (vi->mergeable_rx_bufs) {
|
|
|
|
/* Zero header and leave csum up to XDP layers */
|
|
|
|
hdr = xdp->data;
|
|
|
|
memset(hdr, 0, vi->hdr_len);
|
|
|
|
|
|
|
|
num_sg = 1;
|
|
|
|
sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
|
|
|
|
} else { /* small buffer */
|
|
|
|
struct sk_buff *skb = data;
|
|
|
|
|
|
|
|
/* Zero header and leave csum up to XDP layers */
|
|
|
|
hdr = skb_vnet_hdr(skb);
|
|
|
|
memset(hdr, 0, vi->hdr_len);
|
|
|
|
|
|
|
|
num_sg = 2;
|
|
|
|
sg_init_table(sq->sg, 2);
|
|
|
|
sg_set_buf(sq->sg, hdr, vi->hdr_len);
|
|
|
|
skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
|
|
|
|
}
|
2016-12-15 20:14:13 +00:00
|
|
|
err = virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
|
2016-12-23 14:37:32 +00:00
|
|
|
data, GFP_ATOMIC);
|
2016-12-15 20:14:13 +00:00
|
|
|
if (unlikely(err)) {
|
2016-12-23 14:37:32 +00:00
|
|
|
if (vi->mergeable_rx_bufs) {
|
|
|
|
struct page *page = virt_to_head_page(xdp->data);
|
|
|
|
|
|
|
|
put_page(page);
|
|
|
|
} else /* small buffer */
|
|
|
|
kfree_skb(data);
|
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 01:28:18 +00:00
|
|
|
/* On error abort to avoid unnecessary kick */
|
|
|
|
return false;
|
2016-12-15 20:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtqueue_kick(sq->vq);
|
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 01:28:18 +00:00
|
|
|
return true;
|
2016-12-15 20:14:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 14:37:32 +00:00
|
|
|
static struct sk_buff *receive_small(struct net_device *dev,
|
|
|
|
struct virtnet_info *vi,
|
|
|
|
struct receive_queue *rq,
|
|
|
|
void *buf, unsigned int len)
|
2013-11-28 11:30:59 +00:00
|
|
|
{
|
|
|
|
struct sk_buff * skb = buf;
|
2016-12-23 14:37:32 +00:00
|
|
|
struct bpf_prog *xdp_prog;
|
2013-11-28 11:30:59 +00:00
|
|
|
|
2014-10-24 13:55:57 +00:00
|
|
|
len -= vi->hdr_len;
|
2013-11-28 11:30:59 +00:00
|
|
|
skb_trim(skb, len);
|
|
|
|
|
2016-12-23 14:37:32 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
xdp_prog = rcu_dereference(rq->xdp_prog);
|
|
|
|
if (xdp_prog) {
|
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
|
2017-02-03 03:15:01 +00:00
|
|
|
struct xdp_buff xdp;
|
|
|
|
unsigned int qp;
|
2016-12-23 14:37:32 +00:00
|
|
|
u32 act;
|
|
|
|
|
|
|
|
if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
|
|
|
|
goto err_xdp;
|
2017-02-03 03:15:01 +00:00
|
|
|
|
|
|
|
xdp.data = skb->data;
|
|
|
|
xdp.data_end = xdp.data + len;
|
|
|
|
act = bpf_prog_run_xdp(xdp_prog, &xdp);
|
|
|
|
|
2016-12-23 14:37:32 +00:00
|
|
|
switch (act) {
|
|
|
|
case XDP_PASS:
|
|
|
|
break;
|
|
|
|
case XDP_TX:
|
2017-02-03 03:15:01 +00:00
|
|
|
qp = vi->curr_queue_pairs -
|
|
|
|
vi->xdp_queue_pairs +
|
|
|
|
smp_processor_id();
|
|
|
|
if (unlikely(!virtnet_xdp_xmit(vi, rq, &vi->sq[qp],
|
|
|
|
&xdp, skb)))
|
|
|
|
trace_xdp_exception(vi->dev, xdp_prog, act);
|
2016-12-23 14:37:32 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
goto xdp_xmit;
|
|
|
|
default:
|
2017-02-03 03:15:01 +00:00
|
|
|
bpf_warn_invalid_xdp_action(act);
|
|
|
|
case XDP_ABORTED:
|
|
|
|
trace_xdp_exception(vi->dev, xdp_prog, act);
|
|
|
|
case XDP_DROP:
|
2016-12-23 14:37:32 +00:00
|
|
|
goto err_xdp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
2013-11-28 11:30:59 +00:00
|
|
|
return skb;
|
2016-12-23 14:37:32 +00:00
|
|
|
|
|
|
|
err_xdp:
|
|
|
|
rcu_read_unlock();
|
|
|
|
dev->stats.rx_dropped++;
|
|
|
|
kfree_skb(skb);
|
|
|
|
xdp_xmit:
|
|
|
|
return NULL;
|
2013-11-28 11:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct sk_buff *receive_big(struct net_device *dev,
|
2014-10-23 21:12:10 +00:00
|
|
|
struct virtnet_info *vi,
|
2013-11-28 11:30:59 +00:00
|
|
|
struct receive_queue *rq,
|
|
|
|
void *buf,
|
|
|
|
unsigned int len)
|
|
|
|
{
|
|
|
|
struct page *page = buf;
|
2016-12-23 14:37:31 +00:00
|
|
|
struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
|
2016-12-15 20:13:24 +00:00
|
|
|
|
2013-11-28 11:30:59 +00:00
|
|
|
if (unlikely(!skb))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
return skb;
|
|
|
|
|
|
|
|
err:
|
|
|
|
dev->stats.rx_dropped++;
|
|
|
|
give_pages(rq, page);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-15 20:14:36 +00:00
|
|
|
/* The conditions to enable XDP should preclude the underlying device from
|
|
|
|
* sending packets across multiple buffers (num_buf > 1). However per spec
|
|
|
|
* it does not appear to be illegal to do so but rather just against convention.
|
|
|
|
* So in order to avoid making a system unresponsive the packets are pushed
|
|
|
|
* into a page and the XDP program is run. This will be extremely slow and we
|
|
|
|
* push a warning to the user to fix this as soon as possible. Fixing this may
|
|
|
|
* require resolving the underlying hardware to determine why multiple buffers
|
|
|
|
* are being received or simply loading the XDP program in the ingress stack
|
|
|
|
* after the skb is built because there is no advantage to running it here
|
|
|
|
* anymore.
|
|
|
|
*/
|
|
|
|
static struct page *xdp_linearize_page(struct receive_queue *rq,
|
2016-12-23 14:37:26 +00:00
|
|
|
u16 *num_buf,
|
2016-12-15 20:14:36 +00:00
|
|
|
struct page *p,
|
|
|
|
int offset,
|
|
|
|
unsigned int *len)
|
|
|
|
{
|
|
|
|
struct page *page = alloc_page(GFP_ATOMIC);
|
|
|
|
unsigned int page_off = 0;
|
|
|
|
|
|
|
|
if (!page)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
|
|
|
|
page_off += *len;
|
|
|
|
|
2016-12-23 14:37:26 +00:00
|
|
|
while (--*num_buf) {
|
2016-12-15 20:14:36 +00:00
|
|
|
unsigned int buflen;
|
|
|
|
unsigned long ctx;
|
|
|
|
void *buf;
|
|
|
|
int off;
|
|
|
|
|
|
|
|
ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen);
|
|
|
|
if (unlikely(!ctx))
|
|
|
|
goto err_buf;
|
|
|
|
|
2016-12-23 14:37:26 +00:00
|
|
|
buf = mergeable_ctx_to_buf_address(ctx);
|
|
|
|
p = virt_to_head_page(buf);
|
|
|
|
off = buf - page_address(p);
|
|
|
|
|
2016-12-15 20:14:36 +00:00
|
|
|
/* guard against a misconfigured or uncooperative backend that
|
|
|
|
* is sending packet larger than the MTU.
|
|
|
|
*/
|
2016-12-23 14:37:26 +00:00
|
|
|
if ((page_off + buflen) > PAGE_SIZE) {
|
|
|
|
put_page(p);
|
2016-12-15 20:14:36 +00:00
|
|
|
goto err_buf;
|
2016-12-23 14:37:26 +00:00
|
|
|
}
|
2016-12-15 20:14:36 +00:00
|
|
|
|
|
|
|
memcpy(page_address(page) + page_off,
|
|
|
|
page_address(p) + off, buflen);
|
|
|
|
page_off += buflen;
|
2016-12-23 14:37:26 +00:00
|
|
|
put_page(p);
|
2016-12-15 20:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*len = page_off;
|
|
|
|
return page;
|
|
|
|
err_buf:
|
|
|
|
__free_pages(page, 0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-28 11:30:55 +00:00
|
|
|
static struct sk_buff *receive_mergeable(struct net_device *dev,
|
2014-10-07 14:39:48 +00:00
|
|
|
struct virtnet_info *vi,
|
2013-11-28 11:30:55 +00:00
|
|
|
struct receive_queue *rq,
|
2014-01-17 06:23:27 +00:00
|
|
|
unsigned long ctx,
|
2013-11-28 11:30:55 +00:00
|
|
|
unsigned int len)
|
2010-01-29 03:20:04 +00:00
|
|
|
{
|
2014-01-17 06:23:27 +00:00
|
|
|
void *buf = mergeable_ctx_to_buf_address(ctx);
|
2014-10-24 13:55:57 +00:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
|
|
|
|
u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
|
2013-11-28 11:30:55 +00:00
|
|
|
struct page *page = virt_to_head_page(buf);
|
|
|
|
int offset = buf - page_address(page);
|
2016-12-15 20:13:24 +00:00
|
|
|
struct sk_buff *head_skb, *curr_skb;
|
|
|
|
struct bpf_prog *xdp_prog;
|
|
|
|
unsigned int truesize;
|
|
|
|
|
2016-12-15 20:14:13 +00:00
|
|
|
head_skb = NULL;
|
|
|
|
|
2016-12-15 20:13:24 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
xdp_prog = rcu_dereference(rq->xdp_prog);
|
|
|
|
if (xdp_prog) {
|
2016-12-15 20:14:36 +00:00
|
|
|
struct page *xdp_page;
|
2017-02-03 03:15:01 +00:00
|
|
|
struct xdp_buff xdp;
|
|
|
|
unsigned int qp;
|
|
|
|
void *data;
|
2016-12-15 20:13:24 +00:00
|
|
|
u32 act;
|
|
|
|
|
2016-12-23 14:37:24 +00:00
|
|
|
/* This happens when rx buffer size is underestimated */
|
2016-12-15 20:13:24 +00:00
|
|
|
if (unlikely(num_buf > 1)) {
|
2016-12-15 20:14:36 +00:00
|
|
|
/* linearize data for XDP */
|
2016-12-23 14:37:26 +00:00
|
|
|
xdp_page = xdp_linearize_page(rq, &num_buf,
|
2016-12-15 20:14:36 +00:00
|
|
|
page, offset, &len);
|
|
|
|
if (!xdp_page)
|
|
|
|
goto err_xdp;
|
|
|
|
offset = 0;
|
|
|
|
} else {
|
|
|
|
xdp_page = page;
|
2016-12-15 20:13:24 +00: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. In practice I
|
|
|
|
* was not able to create this condition.
|
|
|
|
*/
|
2016-12-23 14:37:28 +00:00
|
|
|
if (unlikely(hdr->hdr.gso_type))
|
2016-12-15 20:13:24 +00:00
|
|
|
goto err_xdp;
|
|
|
|
|
2017-02-03 03:15:01 +00:00
|
|
|
data = page_address(xdp_page) + offset;
|
|
|
|
xdp.data = data + vi->hdr_len;
|
|
|
|
xdp.data_end = xdp.data + (len - vi->hdr_len);
|
|
|
|
act = bpf_prog_run_xdp(xdp_prog, &xdp);
|
|
|
|
|
2016-12-15 20:14:13 +00:00
|
|
|
switch (act) {
|
|
|
|
case XDP_PASS:
|
2016-12-23 14:37:27 +00:00
|
|
|
/* We can only create skb based on xdp_page. */
|
|
|
|
if (unlikely(xdp_page != page)) {
|
|
|
|
rcu_read_unlock();
|
|
|
|
put_page(page);
|
|
|
|
head_skb = page_to_skb(vi, rq, xdp_page,
|
|
|
|
0, len, PAGE_SIZE);
|
2016-12-23 14:37:29 +00:00
|
|
|
ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
|
2016-12-23 14:37:27 +00:00
|
|
|
return head_skb;
|
|
|
|
}
|
2016-12-15 20:14:13 +00:00
|
|
|
break;
|
|
|
|
case XDP_TX:
|
2017-02-03 03:15:01 +00:00
|
|
|
qp = vi->curr_queue_pairs -
|
|
|
|
vi->xdp_queue_pairs +
|
|
|
|
smp_processor_id();
|
|
|
|
if (unlikely(!virtnet_xdp_xmit(vi, rq, &vi->sq[qp],
|
|
|
|
&xdp, data)))
|
|
|
|
trace_xdp_exception(vi->dev, xdp_prog, act);
|
2016-12-23 14:37:29 +00:00
|
|
|
ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
|
2016-12-15 20:14:36 +00:00
|
|
|
if (unlikely(xdp_page != page))
|
|
|
|
goto err_xdp;
|
2016-12-15 20:14:13 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
goto xdp_xmit;
|
|
|
|
default:
|
2017-02-03 03:15:01 +00:00
|
|
|
bpf_warn_invalid_xdp_action(act);
|
|
|
|
case XDP_ABORTED:
|
|
|
|
trace_xdp_exception(vi->dev, xdp_prog, act);
|
|
|
|
case XDP_DROP:
|
2016-12-15 20:14:36 +00:00
|
|
|
if (unlikely(xdp_page != page))
|
|
|
|
__free_pages(xdp_page, 0);
|
2016-12-23 14:37:29 +00:00
|
|
|
ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
|
2016-12-15 20:13:24 +00:00
|
|
|
goto err_xdp;
|
2016-12-15 20:14:13 +00:00
|
|
|
}
|
2016-12-15 20:13:24 +00:00
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
2014-01-17 06:23:27 +00:00
|
|
|
|
2016-12-15 20:13:24 +00:00
|
|
|
truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
|
|
|
|
head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
|
|
|
|
curr_skb = head_skb;
|
2010-01-29 03:20:04 +00:00
|
|
|
|
2013-11-28 11:30:55 +00:00
|
|
|
if (unlikely(!curr_skb))
|
|
|
|
goto err_skb;
|
2010-01-29 03:20:04 +00:00
|
|
|
while (--num_buf) {
|
2013-11-28 11:30:55 +00:00
|
|
|
int num_skb_frags;
|
|
|
|
|
2014-01-17 06:23:27 +00:00
|
|
|
ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
|
|
|
|
if (unlikely(!ctx)) {
|
2013-11-28 11:30:55 +00:00
|
|
|
pr_debug("%s: rx error: %d buffers out of %d missing\n",
|
2014-10-07 14:39:48 +00:00
|
|
|
dev->name, num_buf,
|
2014-10-24 13:55:57 +00:00
|
|
|
virtio16_to_cpu(vi->vdev,
|
|
|
|
hdr->num_buffers));
|
2013-11-28 11:30:55 +00:00
|
|
|
dev->stats.rx_length_errors++;
|
|
|
|
goto err_buf;
|
2008-11-17 06:41:34 +00:00
|
|
|
}
|
2013-11-28 11:30:55 +00:00
|
|
|
|
2014-01-17 06:23:27 +00:00
|
|
|
buf = mergeable_ctx_to_buf_address(ctx);
|
2013-11-28 11:30:55 +00:00
|
|
|
page = virt_to_head_page(buf);
|
|
|
|
|
|
|
|
num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
|
2013-10-28 22:44:18 +00:00
|
|
|
if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
|
|
|
|
struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
|
2013-11-28 11:30:55 +00:00
|
|
|
|
|
|
|
if (unlikely(!nskb))
|
|
|
|
goto err_skb;
|
2013-10-28 22:44:18 +00: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;
|
|
|
|
}
|
2014-01-17 06:23:27 +00:00
|
|
|
truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
|
2013-10-28 22:44:18 +00:00
|
|
|
if (curr_skb != head_skb) {
|
|
|
|
head_skb->data_len += len;
|
|
|
|
head_skb->len += len;
|
2014-01-17 06:23:26 +00:00
|
|
|
head_skb->truesize += truesize;
|
2013-10-28 22:44:18 +00:00
|
|
|
}
|
2013-11-28 11:30:55 +00:00
|
|
|
offset = buf - page_address(page);
|
2013-11-01 06:07:48 +00: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 06:23:26 +00:00
|
|
|
len, truesize);
|
2013-11-01 06:07:48 +00:00
|
|
|
} else {
|
|
|
|
skb_add_rx_frag(curr_skb, num_skb_frags, page,
|
2014-01-17 06:23:26 +00:00
|
|
|
offset, len, truesize);
|
2013-11-01 06:07:48 +00:00
|
|
|
}
|
2013-11-28 11:30:55 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 07:48:40 +00:00
|
|
|
ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
|
2013-11-28 11:30:55 +00:00
|
|
|
return head_skb;
|
|
|
|
|
2016-12-15 20:13:24 +00:00
|
|
|
err_xdp:
|
|
|
|
rcu_read_unlock();
|
2013-11-28 11:30:55 +00:00
|
|
|
err_skb:
|
|
|
|
put_page(page);
|
|
|
|
while (--num_buf) {
|
2014-01-17 06:23:27 +00:00
|
|
|
ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
|
|
|
|
if (unlikely(!ctx)) {
|
2013-11-28 11:30:55 +00:00
|
|
|
pr_debug("%s: rx error: %d buffers missing\n",
|
|
|
|
dev->name, num_buf);
|
|
|
|
dev->stats.rx_length_errors++;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-17 06:23:27 +00:00
|
|
|
page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
|
2013-11-28 11:30:55 +00:00
|
|
|
put_page(page);
|
2010-01-29 03:20:04 +00:00
|
|
|
}
|
2013-11-28 11:30:55 +00:00
|
|
|
err_buf:
|
|
|
|
dev->stats.rx_dropped++;
|
|
|
|
dev_kfree_skb(head_skb);
|
2016-12-15 20:14:13 +00:00
|
|
|
xdp_xmit:
|
2013-11-28 11:30:55 +00:00
|
|
|
return NULL;
|
2010-01-29 03:20:04 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 21:12:10 +00:00
|
|
|
static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
|
|
|
|
void *buf, unsigned int len)
|
2010-01-29 03:20:04 +00:00
|
|
|
{
|
2012-12-07 07:04:55 +00:00
|
|
|
struct net_device *dev = vi->dev;
|
2012-02-13 06:53:41 +00:00
|
|
|
struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
|
2010-01-29 03:20:04 +00:00
|
|
|
struct sk_buff *skb;
|
2014-10-24 13:55:57 +00:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr;
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2014-10-23 21:22:11 +00:00
|
|
|
if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
|
2010-01-29 03:20:04 +00:00
|
|
|
pr_debug("%s: short packet %i\n", dev->name, len);
|
|
|
|
dev->stats.rx_length_errors++;
|
2014-01-17 06:23:27 +00:00
|
|
|
if (vi->mergeable_rx_bufs) {
|
|
|
|
unsigned long ctx = (unsigned long)buf;
|
|
|
|
void *base = mergeable_ctx_to_buf_address(ctx);
|
|
|
|
put_page(virt_to_head_page(base));
|
|
|
|
} else if (vi->big_packets) {
|
2013-12-05 21:14:05 +00:00
|
|
|
give_pages(rq, buf);
|
2014-01-17 06:23:27 +00:00
|
|
|
} else {
|
2010-01-29 03:20:04 +00:00
|
|
|
dev_kfree_skb(buf);
|
2014-01-17 06:23:27 +00:00
|
|
|
}
|
2010-01-29 03:20:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2013-11-28 11:30:59 +00:00
|
|
|
if (vi->mergeable_rx_bufs)
|
2014-10-07 14:39:48 +00:00
|
|
|
skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
|
2013-11-28 11:30:59 +00:00
|
|
|
else if (vi->big_packets)
|
2014-10-23 21:12:10 +00:00
|
|
|
skb = receive_big(dev, vi, rq, buf, len);
|
2013-11-28 11:30:59 +00:00
|
|
|
else
|
2016-12-23 14:37:32 +00:00
|
|
|
skb = receive_small(dev, vi, rq, buf, len);
|
2013-11-28 11:30:59 +00:00
|
|
|
|
|
|
|
if (unlikely(!skb))
|
|
|
|
return;
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
hdr = skb_vnet_hdr(skb);
|
2011-06-15 06:36:29 +00:00
|
|
|
|
2012-06-05 22:35:24 +00:00
|
|
|
u64_stats_update_begin(&stats->rx_syncp);
|
2011-06-15 06:36:29 +00:00
|
|
|
stats->rx_bytes += skb->len;
|
|
|
|
stats->rx_packets++;
|
2012-06-05 22:35:24 +00:00
|
|
|
u64_stats_update_end(&stats->rx_syncp);
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2016-06-08 13:09:21 +00:00
|
|
|
if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
|
2011-06-10 00:56:17 +00:00
|
|
|
skb->ip_summed = CHECKSUM_UNNECESSARY;
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2016-06-08 13:09:21 +00: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 01:03:37 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 05:29:38 +00: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-07-31 16:25:17 +00:00
|
|
|
napi_gro_receive(&rq->napi, skb);
|
2007-10-22 01:03:37 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
frame_err:
|
|
|
|
dev->stats.rx_frame_errors++;
|
|
|
|
dev_kfree_skb(skb);
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:12:10 +00:00
|
|
|
static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
|
|
|
|
gfp_t gfp)
|
2007-10-22 01:03:37 +00:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
2014-10-24 13:55:57 +00:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr;
|
2010-01-29 03:20:04 +00:00
|
|
|
int err;
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2013-11-14 18:41:04 +00:00
|
|
|
skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
|
2010-01-29 03:20:04 +00:00
|
|
|
if (unlikely(!skb))
|
|
|
|
return -ENOMEM;
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2013-11-14 18:41:04 +00:00
|
|
|
skb_put(skb, GOOD_PACKET_LEN);
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
hdr = skb_vnet_hdr(skb);
|
2015-08-27 06:53:06 +00:00
|
|
|
sg_init_table(rq->sg, 2);
|
2014-10-24 13:55:57 +00:00
|
|
|
sg_set_buf(rq->sg, hdr, vi->hdr_len);
|
2012-12-07 07:04:55 +00:00
|
|
|
skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
|
2008-04-18 03:24:27 +00:00
|
|
|
|
2013-03-20 05:14:28 +00:00
|
|
|
err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
|
2010-01-29 03:20:04 +00:00
|
|
|
if (err < 0)
|
|
|
|
dev_kfree_skb(skb);
|
2008-04-18 03:24:27 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
return err;
|
|
|
|
}
|
2008-04-18 03:24:27 +00:00
|
|
|
|
2014-10-24 13:55:57 +00:00
|
|
|
static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
|
|
|
|
gfp_t gfp)
|
2010-01-29 03:20:04 +00:00
|
|
|
{
|
|
|
|
struct page *first, *list = NULL;
|
|
|
|
char *p;
|
|
|
|
int i, err, offset;
|
|
|
|
|
2014-09-11 00:47:36 +00:00
|
|
|
sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
|
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
|
2010-01-29 03:20:04 +00:00
|
|
|
for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
|
2012-12-07 07:04:55 +00:00
|
|
|
first = get_a_page(rq, gfp);
|
2010-01-29 03:20:04 +00:00
|
|
|
if (!first) {
|
|
|
|
if (list)
|
2012-12-07 07:04:55 +00:00
|
|
|
give_pages(rq, list);
|
2010-01-29 03:20:04 +00:00
|
|
|
return -ENOMEM;
|
2008-04-18 03:24:27 +00:00
|
|
|
}
|
2012-12-07 07:04:55 +00:00
|
|
|
sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
|
2008-04-18 03:24:27 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
/* chain new page in list head to match sg */
|
|
|
|
first->private = (unsigned long)list;
|
|
|
|
list = first;
|
|
|
|
}
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
first = get_a_page(rq, gfp);
|
2010-01-29 03:20:04 +00:00
|
|
|
if (!first) {
|
2012-12-07 07:04:55 +00:00
|
|
|
give_pages(rq, list);
|
2010-01-29 03:20:04 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
p = page_address(first);
|
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
/* rq->sg[0], rq->sg[1] share the same page */
|
2014-10-24 13:55:57 +00: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 03:20:04 +00:00
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
/* rq->sg[1] for data packet, from offset */
|
2010-01-29 03:20:04 +00:00
|
|
|
offset = sizeof(struct padded_vnet_hdr);
|
2012-12-07 07:04:55 +00:00
|
|
|
sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
|
2010-01-29 03:20:04 +00:00
|
|
|
|
|
|
|
/* chain first in list head */
|
|
|
|
first->private = (unsigned long)list;
|
2013-03-20 05:14:28 +00:00
|
|
|
err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
|
|
|
|
first, gfp);
|
2010-01-29 03:20:04 +00:00
|
|
|
if (err < 0)
|
2012-12-07 07:04:55 +00:00
|
|
|
give_pages(rq, first);
|
2010-01-29 03:20:04 +00:00
|
|
|
|
|
|
|
return err;
|
2007-10-22 01:03:37 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 07:48:40 +00:00
|
|
|
static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
|
2008-11-17 06:41:34 +00:00
|
|
|
{
|
2014-01-17 06:23:27 +00:00
|
|
|
const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
|
2014-01-17 06:23:30 +00:00
|
|
|
unsigned int len;
|
|
|
|
|
2015-08-19 07:48:40 +00:00
|
|
|
len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
|
2014-01-17 06:23:30 +00:00
|
|
|
GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
|
|
|
|
return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
|
|
|
|
{
|
2014-01-17 06:23:26 +00:00
|
|
|
struct page_frag *alloc_frag = &rq->alloc_frag;
|
|
|
|
char *buf;
|
2014-01-17 06:23:27 +00:00
|
|
|
unsigned long ctx;
|
2008-11-17 06:41:34 +00:00
|
|
|
int err;
|
2014-01-17 06:23:26 +00:00
|
|
|
unsigned int len, hole;
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2014-01-17 06:23:30 +00:00
|
|
|
len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
|
2014-01-17 06:23:27 +00:00
|
|
|
if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
|
2010-01-29 03:20:04 +00:00
|
|
|
return -ENOMEM;
|
2014-01-17 06:23:27 +00:00
|
|
|
|
2014-01-17 06:23:26 +00:00
|
|
|
buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
|
2014-01-17 06:23:27 +00:00
|
|
|
ctx = mergeable_buf_to_ctx(buf, len);
|
2014-01-17 06:23:26 +00:00
|
|
|
get_page(alloc_frag->page);
|
|
|
|
alloc_frag->offset += len;
|
|
|
|
hole = alloc_frag->size - alloc_frag->offset;
|
2014-01-17 06:23:27 +00:00
|
|
|
if (hole < len) {
|
|
|
|
/* To avoid internal fragmentation, if there is very likely not
|
|
|
|
* enough space for another buffer, add the remaining space to
|
|
|
|
* the current buffer. This extra space is not included in
|
|
|
|
* the truesize stored in ctx.
|
|
|
|
*/
|
2014-01-17 06:23:26 +00:00
|
|
|
len += hole;
|
|
|
|
alloc_frag->offset += hole;
|
|
|
|
}
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2014-01-17 06:23:26 +00:00
|
|
|
sg_init_one(rq->sg, buf, len);
|
2014-01-17 06:23:27 +00:00
|
|
|
err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
|
2010-01-29 03:20:04 +00:00
|
|
|
if (err < 0)
|
2013-10-28 22:44:18 +00:00
|
|
|
put_page(virt_to_head_page(buf));
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-01-29 03:20:04 +00:00
|
|
|
return err;
|
|
|
|
}
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2011-12-29 00:42:38 +00: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-23 21:12:10 +00:00
|
|
|
static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
|
|
|
|
gfp_t gfp)
|
2010-01-29 03:20:04 +00:00
|
|
|
{
|
|
|
|
int err;
|
2010-07-02 16:32:55 +00:00
|
|
|
bool oom;
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2014-01-17 06:23:26 +00:00
|
|
|
gfp |= __GFP_COLD;
|
2010-01-29 03:20:04 +00:00
|
|
|
do {
|
|
|
|
if (vi->mergeable_rx_bufs)
|
2012-12-07 07:04:55 +00:00
|
|
|
err = add_recvbuf_mergeable(rq, gfp);
|
2010-01-29 03:20:04 +00:00
|
|
|
else if (vi->big_packets)
|
2014-10-24 13:55:57 +00:00
|
|
|
err = add_recvbuf_big(vi, rq, gfp);
|
2010-01-29 03:20:04 +00:00
|
|
|
else
|
2014-10-23 21:12:10 +00:00
|
|
|
err = add_recvbuf_small(vi, rq, gfp);
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2010-07-02 16:32:55 +00:00
|
|
|
oom = err == -ENOMEM;
|
2012-10-16 13:26:14 +00:00
|
|
|
if (err)
|
2008-11-17 06:41:34 +00:00
|
|
|
break;
|
2012-12-20 16:37:04 +00:00
|
|
|
} while (rq->vq->num_free);
|
2014-03-26 05:03:00 +00:00
|
|
|
virtqueue_kick(rq->vq);
|
2009-08-26 19:22:32 +00:00
|
|
|
return !oom;
|
2008-11-17 06:41:34 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 04:49:57 +00:00
|
|
|
static void skb_recv_done(struct virtqueue *rvq)
|
2007-10-22 01:03:37 +00:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = rvq->vdev->priv;
|
2012-12-07 07:04:56 +00:00
|
|
|
struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
|
2012-12-07 07:04:55 +00:00
|
|
|
|
2008-02-05 04:49:57 +00:00
|
|
|
/* Schedule NAPI, Suppress further interrupts if successful. */
|
2012-12-07 07:04:55 +00:00
|
|
|
if (napi_schedule_prep(&rq->napi)) {
|
2010-04-12 13:19:04 +00:00
|
|
|
virtqueue_disable_cb(rvq);
|
2012-12-07 07:04:55 +00:00
|
|
|
__napi_schedule(&rq->napi);
|
2008-02-05 04:49:57 +00:00
|
|
|
}
|
2007-10-22 01:03:37 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
static void virtnet_napi_enable(struct receive_queue *rq)
|
2011-02-10 19:03:31 +00:00
|
|
|
{
|
2012-12-07 07:04:55 +00:00
|
|
|
napi_enable(&rq->napi);
|
2011-02-10 19:03:31 +00:00
|
|
|
|
|
|
|
/* If all buffers were filled by other side before we napi_enabled, we
|
|
|
|
* won't get another interrupt, so process any outstanding packets
|
|
|
|
* now. virtnet_poll wants re-enable the queue, so we disable here.
|
|
|
|
* We synchronize against interrupts via NAPI_STATE_SCHED */
|
2012-12-07 07:04:55 +00:00
|
|
|
if (napi_schedule_prep(&rq->napi)) {
|
|
|
|
virtqueue_disable_cb(rq->vq);
|
2012-05-16 07:57:12 +00:00
|
|
|
local_bh_disable();
|
2012-12-07 07:04:55 +00:00
|
|
|
__napi_schedule(&rq->napi);
|
2012-05-16 07:57:12 +00:00
|
|
|
local_bh_enable();
|
2011-02-10 19:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-26 19:22:32 +00:00
|
|
|
static void refill_work(struct work_struct *work)
|
|
|
|
{
|
2012-12-07 07:04:55 +00:00
|
|
|
struct virtnet_info *vi =
|
|
|
|
container_of(work, struct virtnet_info, refill.work);
|
2009-08-26 19:22:32 +00:00
|
|
|
bool still_empty;
|
2012-12-07 07:04:56 +00:00
|
|
|
int i;
|
|
|
|
|
2013-04-29 02:30:08 +00:00
|
|
|
for (i = 0; i < vi->curr_queue_pairs; i++) {
|
2012-12-07 07:04:56 +00:00
|
|
|
struct receive_queue *rq = &vi->rq[i];
|
2009-08-26 19:22:32 +00:00
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
napi_disable(&rq->napi);
|
2014-10-23 21:12:10 +00:00
|
|
|
still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
|
2012-12-07 07:04:56 +00:00
|
|
|
virtnet_napi_enable(rq);
|
2009-08-26 19:22:32 +00:00
|
|
|
|
2012-12-07 07:04:56 +00: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-26 19:22:32 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 08:33:54 +00:00
|
|
|
static int virtnet_receive(struct receive_queue *rq, int budget)
|
2007-10-22 01:03:37 +00:00
|
|
|
{
|
2012-12-07 07:04:55 +00:00
|
|
|
struct virtnet_info *vi = rq->vq->vdev->priv;
|
2014-07-23 08:33:54 +00:00
|
|
|
unsigned int len, received = 0;
|
2010-01-29 03:20:04 +00:00
|
|
|
void *buf;
|
2007-10-22 01:03:37 +00:00
|
|
|
|
|
|
|
while (received < budget &&
|
2012-12-07 07:04:55 +00:00
|
|
|
(buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
|
2014-10-23 21:12:10 +00:00
|
|
|
receive_buf(vi, rq, buf, len);
|
2007-10-22 01:03:37 +00:00
|
|
|
received++;
|
|
|
|
}
|
|
|
|
|
2014-01-16 06:45:24 +00:00
|
|
|
if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
|
2014-10-23 21:12:10 +00:00
|
|
|
if (!try_fill_recv(vi, rq, GFP_ATOMIC))
|
2012-08-20 21:51:24 +00:00
|
|
|
schedule_delayed_work(&vi->refill, 0);
|
2009-08-26 19:22:32 +00:00
|
|
|
}
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2014-07-23 08:33:54 +00:00
|
|
|
return received;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_poll(struct napi_struct *napi, int budget)
|
|
|
|
{
|
|
|
|
struct receive_queue *rq =
|
|
|
|
container_of(napi, struct receive_queue, napi);
|
2015-03-26 07:39:45 +00:00
|
|
|
unsigned int r, received;
|
2014-07-23 08:33:54 +00:00
|
|
|
|
2015-03-26 07:39:45 +00:00
|
|
|
received = virtnet_receive(rq, budget);
|
2014-07-23 08:33:54 +00:00
|
|
|
|
2007-11-19 16:20:43 +00:00
|
|
|
/* Out of packets? */
|
|
|
|
if (received < budget) {
|
virtio_net: fix race in RX VQ processing
virtio net called virtqueue_enable_cq on RX path after napi_complete, so
with NAPI_STATE_SCHED clear - outside the implicit napi lock.
This violates the requirement to synchronize virtqueue_enable_cq wrt
virtqueue_add_buf. In particular, used event can move backwards,
causing us to lose interrupts.
In a debug build, this can trigger panic within START_USE.
Jason Wang reports that he can trigger the races artificially,
by adding udelay() in virtqueue_enable_cb() after virtio_mb().
However, we must call napi_complete to clear NAPI_STATE_SCHED before
polling the virtqueue for used buffers, otherwise napi_schedule_prep in
a callback will fail, causing us to lose RX events.
To fix, call virtqueue_enable_cb_prepare with NAPI_STATE_SCHED
set (under napi lock), later call virtqueue_poll with
NAPI_STATE_SCHED clear (outside the lock).
Reported-by: Jason Wang <jasowang@redhat.com>
Tested-by: Jason Wang <jasowang@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2013-07-09 05:13:04 +00:00
|
|
|
r = virtqueue_enable_cb_prepare(rq->vq);
|
2017-02-04 15:49:21 +00:00
|
|
|
if (napi_complete_done(napi, received)) {
|
|
|
|
if (unlikely(virtqueue_poll(rq->vq, r)) &&
|
|
|
|
napi_schedule_prep(napi)) {
|
|
|
|
virtqueue_disable_cb(rq->vq);
|
|
|
|
__napi_schedule(napi);
|
|
|
|
}
|
virtio: fix race in enable_cb
There is a race in virtio_net, dealing with disabling/enabling the callback.
I saw the following oops:
kernel BUG at /space/kvm/drivers/virtio/virtio_ring.c:218!
illegal operation: 0001 [#1] SMP
Modules linked in: sunrpc dm_mod
CPU: 2 Not tainted 2.6.25-rc1zlive-host-10623-gd358142-dirty #99
Process swapper (pid: 0, task: 000000000f85a610, ksp: 000000000f873c60)
Krnl PSW : 0404300180000000 00000000002b81a6 (vring_disable_cb+0x16/0x20)
R:0 T:1 IO:0 EX:0 Key:0 M:1 W:0 P:0 AS:0 CC:3 PM:0 EA:3
Krnl GPRS: 0000000000000001 0000000000000001 0000000010005800 0000000000000001
000000000f3a0900 000000000f85a610 0000000000000000 0000000000000000
0000000000000000 000000000f870000 0000000000000000 0000000000001237
000000000f3a0920 000000000010ff74 00000000002846f6 000000000fa0bcd8
Krnl Code: 00000000002b819a: a7110001 tmll %r1,1
00000000002b819e: a7840004 brc 8,2b81a6
00000000002b81a2: a7f40001 brc 15,2b81a4
>00000000002b81a6: a51b0001 oill %r1,1
00000000002b81aa: 40102000 sth %r1,0(%r2)
00000000002b81ae: 07fe bcr 15,%r14
00000000002b81b0: eb7ff0380024 stmg %r7,%r15,56(%r15)
00000000002b81b6: a7f13e00 tmll %r15,15872
Call Trace:
([<000000000fa0bcd0>] 0xfa0bcd0)
[<00000000002b8350>] vring_interrupt+0x5c/0x6c
[<000000000010ab08>] do_extint+0xb8/0xf0
[<0000000000110716>] ext_no_vtime+0x16/0x1a
[<0000000000107e72>] cpu_idle+0x1c2/0x1e0
The problem can be triggered with a high amount of host->guest traffic.
I think its the following race:
poll says netif_rx_complete
poll calls enable_cb
enable_cb opens the interrupt mask
a new packet comes, an interrupt is triggered----\
enable_cb sees that there is more work |
enable_cb disables the interrupt |
. V
. interrupt is delivered
. skb_recv_done does atomic napi test, ok
some waiting disable_cb is called->check fails->bang!
.
poll would do napi check
poll would do disable_cb
The fix is to let enable_cb not disable the interrupt again, but expect the
caller to do the cleanup if it returns false. In that case, the interrupt is
only disabled, if the napi test_set_bit was successful.
Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> (cleaned up doco)
2008-03-14 13:17:05 +00:00
|
|
|
}
|
2007-10-22 01:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return received;
|
|
|
|
}
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
static int virtnet_open(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
int i;
|
|
|
|
|
2013-05-21 20:03:58 +00: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-23 21:12:10 +00:00
|
|
|
if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
|
2013-05-21 20:03:58 +00:00
|
|
|
schedule_delayed_work(&vi->refill, 0);
|
2012-12-07 07:04:56 +00:00
|
|
|
virtnet_napi_enable(&vi->rq[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-20 16:37:04 +00:00
|
|
|
static void free_old_xmit_skbs(struct send_queue *sq)
|
2007-10-22 01:03:37 +00:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
2012-10-16 13:26:14 +00:00
|
|
|
unsigned int len;
|
2012-12-07 07:04:55 +00:00
|
|
|
struct virtnet_info *vi = sq->vq->vdev->priv;
|
2012-02-13 06:53:41 +00:00
|
|
|
struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
|
2007-10-22 01:03:37 +00:00
|
|
|
pr_debug("Sent skb %p\n", skb);
|
2011-06-15 06:36:29 +00:00
|
|
|
|
2012-06-05 22:35:24 +00:00
|
|
|
u64_stats_update_begin(&stats->tx_syncp);
|
2011-06-15 06:36:29 +00:00
|
|
|
stats->tx_bytes += skb->len;
|
|
|
|
stats->tx_packets++;
|
2012-06-05 22:35:24 +00:00
|
|
|
u64_stats_update_end(&stats->tx_syncp);
|
2011-06-15 06:36:29 +00:00
|
|
|
|
2009-10-14 14:36:43 +00:00
|
|
|
dev_kfree_skb_any(skb);
|
2007-10-22 01:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
|
2007-10-22 01:03:37 +00:00
|
|
|
{
|
2014-10-24 13:55:57 +00:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf *hdr;
|
2007-10-22 01:03:37 +00:00
|
|
|
const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
|
2012-12-07 07:04:55 +00:00
|
|
|
struct virtnet_info *vi = sq->vq->vdev->priv;
|
2012-10-16 13:26:14 +00:00
|
|
|
unsigned num_sg;
|
2014-10-24 13:55:57 +00:00
|
|
|
unsigned hdr_len = vi->hdr_len;
|
2013-07-25 00:50:23 +00:00
|
|
|
bool can_push;
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2008-10-27 22:59:26 +00:00
|
|
|
pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
|
2013-07-25 00:50:23 +00: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 13:55:57 +00:00
|
|
|
hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
|
2013-07-25 00:50:23 +00:00
|
|
|
else
|
|
|
|
hdr = skb_vnet_hdr(skb);
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2016-06-08 13:09:21 +00:00
|
|
|
if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
|
2017-01-20 06:32:42 +00:00
|
|
|
virtio_is_little_endian(vi->vdev), false))
|
2016-06-08 13:09:21 +00:00
|
|
|
BUG();
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2008-11-17 06:41:34 +00:00
|
|
|
if (vi->mergeable_rx_bufs)
|
2014-10-24 13:55:57 +00:00
|
|
|
hdr->num_buffers = 0;
|
2008-11-17 06:41:34 +00:00
|
|
|
|
2015-08-27 06:53:06 +00:00
|
|
|
sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
|
2013-07-25 00:50:23 +00:00
|
|
|
if (can_push) {
|
|
|
|
__skb_push(skb, hdr_len);
|
|
|
|
num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
|
|
|
|
/* Pull header back to avoid skew in tx bytes calculations. */
|
|
|
|
__skb_pull(skb, hdr_len);
|
|
|
|
} else {
|
|
|
|
sg_set_buf(sq->sg, hdr, hdr_len);
|
|
|
|
num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
|
|
|
|
}
|
2013-03-20 05:14:28 +00:00
|
|
|
return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
|
2008-05-26 07:48:13 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 19:50:51 +00:00
|
|
|
static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
|
2008-05-03 02:50:46 +00:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2012-12-07 07:04:56 +00:00
|
|
|
int qnum = skb_get_queue_mapping(skb);
|
|
|
|
struct send_queue *sq = &vi->sq[qnum];
|
2012-10-16 13:26:14 +00:00
|
|
|
int err;
|
2014-10-15 13:23:28 +00:00
|
|
|
struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
|
|
|
|
bool kick = !skb->xmit_more;
|
2008-02-05 04:50:07 +00:00
|
|
|
|
|
|
|
/* Free up any pending old buffers before queueing new ones. */
|
2012-12-07 07:04:55 +00:00
|
|
|
free_old_xmit_skbs(sq);
|
2008-05-03 02:50:46 +00:00
|
|
|
|
2014-06-25 02:37:13 +00:00
|
|
|
/* timestamp packet in software */
|
|
|
|
skb_tx_timestamp(skb);
|
|
|
|
|
2009-10-28 11:03:38 +00:00
|
|
|
/* Try to transmit */
|
2012-12-20 16:37:04 +00:00
|
|
|
err = xmit_skb(sq, skb);
|
2009-09-24 15:59:20 +00:00
|
|
|
|
2012-10-16 13:26:14 +00:00
|
|
|
/* This should not happen! */
|
2014-03-26 05:03:00 +00:00
|
|
|
if (unlikely(err)) {
|
2012-10-16 13:26:14 +00:00
|
|
|
dev->stats.tx_fifo_errors++;
|
|
|
|
if (net_ratelimit())
|
|
|
|
dev_warn(&dev->dev,
|
2012-12-20 16:37:04 +00:00
|
|
|
"Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
|
2010-07-02 16:34:01 +00:00
|
|
|
dev->stats.tx_dropped++;
|
2014-03-16 01:43:33 +00:00
|
|
|
dev_kfree_skb_any(skb);
|
2010-07-02 16:34:01 +00:00
|
|
|
return NETDEV_TX_OK;
|
2007-10-22 01:03:37 +00:00
|
|
|
}
|
2009-10-28 11:03:38 +00:00
|
|
|
|
2009-09-24 15:59:20 +00:00
|
|
|
/* Don't wait up for transmitted skbs to be freed. */
|
|
|
|
skb_orphan(skb);
|
|
|
|
nf_reset(skb);
|
|
|
|
|
2015-04-02 11:05:47 +00: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-24 23:22:07 +00:00
|
|
|
*/
|
2012-12-20 16:37:04 +00:00
|
|
|
if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
|
2012-12-07 07:04:56 +00:00
|
|
|
netif_stop_subqueue(dev, qnum);
|
2012-12-07 07:04:55 +00:00
|
|
|
if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
|
2009-09-24 15:59:20 +00:00
|
|
|
/* More just got used, free them then recheck. */
|
2012-12-20 16:37:04 +00:00
|
|
|
free_old_xmit_skbs(sq);
|
|
|
|
if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
|
2012-12-07 07:04:56 +00:00
|
|
|
netif_start_subqueue(dev, qnum);
|
2012-12-07 07:04:55 +00:00
|
|
|
virtqueue_disable_cb(sq->vq);
|
2009-09-24 15:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-03 02:50:46 +00:00
|
|
|
}
|
2009-09-24 15:59:20 +00:00
|
|
|
|
2014-10-15 13:23:28 +00:00
|
|
|
if (kick || netif_xmit_stopped(txq))
|
2014-08-25 22:51:53 +00:00
|
|
|
virtqueue_kick(sq->vq);
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2014-08-25 22:51:53 +00:00
|
|
|
return NETDEV_TX_OK;
|
2014-08-23 20:18:10 +00:00
|
|
|
}
|
|
|
|
|
2013-01-21 01:17:21 +00:00
|
|
|
/*
|
|
|
|
* Send command via the control virtqueue and check status. Commands
|
|
|
|
* supported by the hypervisor, as indicated by feature bits, should
|
2013-12-10 00:18:45 +00:00
|
|
|
* never fail unless improperly formatted.
|
2013-01-21 01:17:21 +00:00
|
|
|
*/
|
|
|
|
static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
|
2013-12-10 00:17:40 +00:00
|
|
|
struct scatterlist *out)
|
2013-01-21 01:17:21 +00:00
|
|
|
{
|
2013-03-20 05:14:28 +00:00
|
|
|
struct scatterlist *sgs[4], hdr, stat;
|
2013-12-10 00:17:40 +00:00
|
|
|
unsigned out_num = 0, tmp;
|
2013-01-21 01:17:21 +00:00
|
|
|
|
|
|
|
/* Caller should know better */
|
2013-03-20 05:14:28 +00:00
|
|
|
BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
|
2013-01-21 01:17:21 +00:00
|
|
|
|
2015-11-15 13:11:00 +00:00
|
|
|
vi->ctrl_status = ~0;
|
|
|
|
vi->ctrl_hdr.class = class;
|
|
|
|
vi->ctrl_hdr.cmd = cmd;
|
2013-03-20 05:14:28 +00:00
|
|
|
/* Add header */
|
2015-11-15 13:11:00 +00:00
|
|
|
sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
|
2013-03-20 05:14:28 +00:00
|
|
|
sgs[out_num++] = &hdr;
|
2013-01-21 01:17:21 +00:00
|
|
|
|
2013-03-20 05:14:28 +00:00
|
|
|
if (out)
|
|
|
|
sgs[out_num++] = out;
|
2013-01-21 01:17:21 +00:00
|
|
|
|
2013-03-20 05:14:28 +00:00
|
|
|
/* Add return status. */
|
2015-11-15 13:11:00 +00:00
|
|
|
sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
|
2013-12-10 00:17:40 +00:00
|
|
|
sgs[out_num] = &stat;
|
2013-01-21 01:17:21 +00:00
|
|
|
|
2013-12-10 00:17:40 +00:00
|
|
|
BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
|
2014-03-13 00:53:39 +00:00
|
|
|
virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
|
2013-01-21 01:17:21 +00:00
|
|
|
|
2013-10-28 23:10:02 +00:00
|
|
|
if (unlikely(!virtqueue_kick(vi->cvq)))
|
2015-11-15 13:11:00 +00:00
|
|
|
return vi->ctrl_status == VIRTIO_NET_OK;
|
2013-01-21 01:17:21 +00:00
|
|
|
|
|
|
|
/* Spin for a response, the kick causes an ioport write, trapping
|
|
|
|
* into the hypervisor, so the request should be handled immediately.
|
|
|
|
*/
|
2013-10-28 23:10:47 +00:00
|
|
|
while (!virtqueue_get_buf(vi->cvq, &tmp) &&
|
|
|
|
!virtqueue_is_broken(vi->cvq))
|
2013-01-21 01:17:21 +00:00
|
|
|
cpu_relax();
|
|
|
|
|
2015-11-15 13:11:00 +00:00
|
|
|
return vi->ctrl_status == VIRTIO_NET_OK;
|
2013-01-21 01:17:21 +00:00
|
|
|
}
|
|
|
|
|
2009-02-05 00:36:34 +00: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 05:10:06 +00:00
|
|
|
int ret;
|
2016-12-06 02:10:58 +00:00
|
|
|
struct sockaddr *addr;
|
2013-01-21 01:17:23 +00:00
|
|
|
struct scatterlist sg;
|
2009-02-05 00:36:34 +00:00
|
|
|
|
2016-12-23 19:14:58 +00:00
|
|
|
addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
|
2016-12-06 02:10:58 +00:00
|
|
|
if (!addr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ret = eth_prepare_mac_addr_change(dev, addr);
|
2012-06-29 05:10:06 +00:00
|
|
|
if (ret)
|
2016-12-06 02:10:58 +00:00
|
|
|
goto out;
|
2009-02-05 00:36:34 +00:00
|
|
|
|
2013-01-21 01:17:23 +00: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 00:17:40 +00:00
|
|
|
VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
|
2013-01-21 01:17:23 +00:00
|
|
|
dev_warn(&vdev->dev,
|
|
|
|
"Failed to set mac address by vq command.\n");
|
2016-12-06 02:10:58 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2013-01-21 01:17:23 +00:00
|
|
|
}
|
2014-11-26 13:58:28 +00:00
|
|
|
} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
|
|
|
|
!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
|
2013-10-14 07:41:51 +00: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 01:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eth_commit_mac_addr_change(dev, p);
|
2016-12-06 02:10:58 +00:00
|
|
|
ret = 0;
|
2009-02-05 00:36:34 +00:00
|
|
|
|
2016-12-06 02:10:58 +00:00
|
|
|
out:
|
|
|
|
kfree(addr);
|
|
|
|
return ret;
|
2009-02-05 00:36:34 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 03:12:52 +00:00
|
|
|
static void virtnet_stats(struct net_device *dev,
|
|
|
|
struct rtnl_link_stats64 *tot)
|
2011-06-15 06:36:29 +00:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
int cpu;
|
|
|
|
unsigned int start;
|
|
|
|
|
|
|
|
for_each_possible_cpu(cpu) {
|
2012-02-13 06:53:41 +00:00
|
|
|
struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
|
2011-06-15 06:36:29 +00:00
|
|
|
u64 tpackets, tbytes, rpackets, rbytes;
|
|
|
|
|
|
|
|
do {
|
2014-03-14 04:26:42 +00:00
|
|
|
start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
|
2011-06-15 06:36:29 +00:00
|
|
|
tpackets = stats->tx_packets;
|
|
|
|
tbytes = stats->tx_bytes;
|
2014-03-14 04:26:42 +00:00
|
|
|
} while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
|
2012-06-05 22:35:24 +00:00
|
|
|
|
|
|
|
do {
|
2014-03-14 04:26:42 +00:00
|
|
|
start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
|
2011-06-15 06:36:29 +00:00
|
|
|
rpackets = stats->rx_packets;
|
|
|
|
rbytes = stats->rx_bytes;
|
2014-03-14 04:26:42 +00:00
|
|
|
} while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
|
2011-06-15 06:36:29 +00:00
|
|
|
|
|
|
|
tot->rx_packets += rpackets;
|
|
|
|
tot->tx_packets += tpackets;
|
|
|
|
tot->rx_bytes += rbytes;
|
|
|
|
tot->tx_bytes += tbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
tot->tx_dropped = dev->stats.tx_dropped;
|
2011-11-21 09:28:17 +00:00
|
|
|
tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
|
2011-06-15 06:36:29 +00:00
|
|
|
tot->rx_dropped = dev->stats.rx_dropped;
|
|
|
|
tot->rx_length_errors = dev->stats.rx_length_errors;
|
|
|
|
tot->rx_frame_errors = dev->stats.rx_frame_errors;
|
|
|
|
}
|
|
|
|
|
2008-02-29 10:54:50 +00:00
|
|
|
#ifdef CONFIG_NET_POLL_CONTROLLER
|
|
|
|
static void virtnet_netpoll(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2012-12-07 07:04:56 +00:00
|
|
|
int i;
|
2008-02-29 10:54:50 +00:00
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
for (i = 0; i < vi->curr_queue_pairs; i++)
|
|
|
|
napi_schedule(&vi->rq[i].napi);
|
2008-02-29 10:54:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-11 20:43:52 +00: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 00:17:40 +00:00
|
|
|
VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
|
2012-04-11 20:43:52 +00:00
|
|
|
dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
|
|
|
|
rtnl_unlock();
|
|
|
|
}
|
|
|
|
|
2017-02-03 03:14:32 +00:00
|
|
|
static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
|
2012-12-07 07:04:56 +00: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;
|
|
|
|
|
2016-07-18 22:34:49 +00: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 07:04:56 +00:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
|
2013-12-10 00:17:40 +00:00
|
|
|
VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
|
2012-12-07 07:04:56 +00:00
|
|
|
dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
|
|
|
|
queue_pairs);
|
|
|
|
return -EINVAL;
|
2013-04-29 02:30:08 +00:00
|
|
|
} else {
|
2012-12-07 07:04:56 +00:00
|
|
|
vi->curr_queue_pairs = queue_pairs;
|
2013-10-15 03:18:59 +00: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 02:30:08 +00:00
|
|
|
}
|
2012-12-07 07:04:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-03 03:14:32 +00: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 01:03:37 +00:00
|
|
|
static int virtnet_close(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2012-12-07 07:04:56 +00:00
|
|
|
int i;
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2011-12-29 00:42:38 +00:00
|
|
|
/* Make sure refill_work doesn't re-enable napi! */
|
|
|
|
cancel_delayed_work_sync(&vi->refill);
|
2012-12-07 07:04:56 +00:00
|
|
|
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++)
|
|
|
|
napi_disable(&vi->rq[i].napi);
|
2007-10-22 01:03:37 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-04 09:02:40 +00:00
|
|
|
static void virtnet_set_rx_mode(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2009-02-04 09:02:45 +00:00
|
|
|
struct scatterlist sg[2];
|
|
|
|
struct virtio_net_ctrl_mac *mac_data;
|
2009-05-22 23:22:17 +00:00
|
|
|
struct netdev_hw_addr *ha;
|
2010-01-25 21:36:10 +00:00
|
|
|
int uc_count;
|
2010-02-08 04:30:35 +00:00
|
|
|
int mc_count;
|
2009-02-04 09:02:45 +00:00
|
|
|
void *buf;
|
|
|
|
int i;
|
2009-02-04 09:02:40 +00:00
|
|
|
|
2013-12-10 00:18:45 +00:00
|
|
|
/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
|
2009-02-04 09:02:40 +00:00
|
|
|
if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
|
|
|
|
return;
|
|
|
|
|
2015-11-15 13:11:00 +00:00
|
|
|
vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
|
|
|
|
vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
|
2009-02-04 09:02:40 +00:00
|
|
|
|
2015-11-15 13:11:00 +00:00
|
|
|
sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
|
2009-02-04 09:02:40 +00:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
|
2013-12-10 00:17:40 +00:00
|
|
|
VIRTIO_NET_CTRL_RX_PROMISC, sg))
|
2009-02-04 09:02:40 +00:00
|
|
|
dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
|
2015-11-15 13:11:00 +00:00
|
|
|
vi->ctrl_promisc ? "en" : "dis");
|
2009-02-04 09:02:40 +00:00
|
|
|
|
2015-11-15 13:11:00 +00:00
|
|
|
sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
|
2009-02-04 09:02:40 +00:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
|
2013-12-10 00:17:40 +00:00
|
|
|
VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
|
2009-02-04 09:02:40 +00:00
|
|
|
dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
|
2015-11-15 13:11:00 +00:00
|
|
|
vi->ctrl_allmulti ? "en" : "dis");
|
2009-02-04 09:02:45 +00:00
|
|
|
|
2010-01-25 21:36:10 +00:00
|
|
|
uc_count = netdev_uc_count(dev);
|
2010-02-08 04:30:35 +00:00
|
|
|
mc_count = netdev_mc_count(dev);
|
2009-02-04 09:02:45 +00:00
|
|
|
/* MAC filter - use one buffer for both lists */
|
2010-02-08 04:30:35 +00:00
|
|
|
buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
|
|
|
|
(2 * sizeof(mac_data->entries)), GFP_ATOMIC);
|
|
|
|
mac_data = buf;
|
2013-02-03 17:28:15 +00:00
|
|
|
if (!buf)
|
2009-02-04 09:02:45 +00:00
|
|
|
return;
|
|
|
|
|
2009-05-01 17:27:56 +00:00
|
|
|
sg_init_table(sg, 2);
|
|
|
|
|
2009-02-04 09:02:45 +00:00
|
|
|
/* Store the unicast list and count in the front of the buffer */
|
2014-10-07 14:39:48 +00:00
|
|
|
mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
|
2009-05-22 23:22:17 +00:00
|
|
|
i = 0;
|
2010-01-25 21:36:10 +00:00
|
|
|
netdev_for_each_uc_addr(ha, dev)
|
2009-05-22 23:22:17 +00:00
|
|
|
memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
|
2009-02-04 09:02:45 +00:00
|
|
|
|
|
|
|
sg_set_buf(&sg[0], mac_data,
|
2010-01-25 21:36:10 +00:00
|
|
|
sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
|
2009-02-04 09:02:45 +00:00
|
|
|
|
|
|
|
/* multicast list and count fill the end */
|
2010-01-25 21:36:10 +00:00
|
|
|
mac_data = (void *)&mac_data->macs[uc_count][0];
|
2009-02-04 09:02:45 +00:00
|
|
|
|
2014-10-07 14:39:48 +00:00
|
|
|
mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
|
2010-02-23 23:17:07 +00:00
|
|
|
i = 0;
|
2010-04-01 21:22:57 +00:00
|
|
|
netdev_for_each_mc_addr(ha, dev)
|
|
|
|
memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
|
2009-02-04 09:02:45 +00:00
|
|
|
|
|
|
|
sg_set_buf(&sg[1], mac_data,
|
2010-02-08 04:30:35 +00:00
|
|
|
sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
|
2009-02-04 09:02:45 +00:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
|
2013-12-10 00:17:40 +00:00
|
|
|
VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
|
2013-11-29 09:02:19 +00:00
|
|
|
dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
|
2009-02-04 09:02:45 +00:00
|
|
|
|
|
|
|
kfree(buf);
|
2009-02-04 09:02:40 +00:00
|
|
|
}
|
|
|
|
|
2013-04-19 02:04:28 +00:00
|
|
|
static int virtnet_vlan_rx_add_vid(struct net_device *dev,
|
|
|
|
__be16 proto, u16 vid)
|
2009-02-04 09:02:50 +00:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct scatterlist sg;
|
|
|
|
|
2016-07-18 22:34:49 +00:00
|
|
|
vi->ctrl_vid = vid;
|
|
|
|
sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
|
2009-02-04 09:02:50 +00:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
|
2013-12-10 00:17:40 +00:00
|
|
|
VIRTIO_NET_CTRL_VLAN_ADD, &sg))
|
2009-02-04 09:02:50 +00:00
|
|
|
dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
|
2011-12-09 00:52:37 +00:00
|
|
|
return 0;
|
2009-02-04 09:02:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-19 02:04:28 +00:00
|
|
|
static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
|
|
|
|
__be16 proto, u16 vid)
|
2009-02-04 09:02:50 +00:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct scatterlist sg;
|
|
|
|
|
2016-07-18 22:34:49 +00:00
|
|
|
vi->ctrl_vid = vid;
|
|
|
|
sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
|
2009-02-04 09:02:50 +00:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
|
2013-12-10 00:17:40 +00:00
|
|
|
VIRTIO_NET_CTRL_VLAN_DEL, &sg))
|
2009-02-04 09:02:50 +00:00
|
|
|
dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
|
2011-12-09 00:52:37 +00:00
|
|
|
return 0;
|
2009-02-04 09:02:50 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 23:51:30 +00:00
|
|
|
static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
|
2012-12-07 07:04:56 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-01-24 23:51:30 +00:00
|
|
|
if (vi->affinity_hint_set) {
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
2013-01-24 23:51:29 +00:00
|
|
|
virtqueue_set_affinity(vi->rq[i].vq, -1);
|
|
|
|
virtqueue_set_affinity(vi->sq[i].vq, -1);
|
|
|
|
}
|
|
|
|
|
2013-01-24 23:51:30 +00:00
|
|
|
vi->affinity_hint_set = false;
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 23:51:29 +00:00
|
|
|
|
2013-01-24 23:51:30 +00:00
|
|
|
static void virtnet_set_affinity(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int cpu;
|
2012-12-07 07:04:56 +00:00
|
|
|
|
|
|
|
/* In multiqueue mode, when the number of cpu is equal to the number of
|
|
|
|
* queue pairs, we let the queue pairs to be private to one cpu by
|
|
|
|
* setting the affinity hint to eliminate the contention.
|
|
|
|
*/
|
2013-01-24 23:51:30 +00:00
|
|
|
if (vi->curr_queue_pairs == 1 ||
|
|
|
|
vi->max_queue_pairs != num_online_cpus()) {
|
|
|
|
virtnet_clean_affinity(vi, -1);
|
|
|
|
return;
|
2012-12-07 07:04:56 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 23:51:30 +00:00
|
|
|
i = 0;
|
|
|
|
for_each_online_cpu(cpu) {
|
2012-12-07 07:04:56 +00:00
|
|
|
virtqueue_set_affinity(vi->rq[i].vq, cpu);
|
|
|
|
virtqueue_set_affinity(vi->sq[i].vq, cpu);
|
2013-11-05 10:19:45 +00:00
|
|
|
netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
|
2013-01-24 23:51:30 +00:00
|
|
|
i++;
|
2012-12-07 07:04:56 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 23:51:30 +00:00
|
|
|
vi->affinity_hint_set = true;
|
2012-12-07 07:04:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:49:43 +00:00
|
|
|
static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
|
2013-01-24 23:51:31 +00:00
|
|
|
{
|
2016-08-12 17:49:43 +00:00
|
|
|
struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
|
|
|
|
node);
|
|
|
|
virtnet_set_affinity(vi);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-24 23:51:31 +00:00
|
|
|
|
2016-08-12 17:49:43 +00: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 03:18:58 +00:00
|
|
|
|
2016-08-12 17:49:43 +00: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);
|
|
|
|
|
|
|
|
virtnet_clean_affinity(vi, cpu);
|
|
|
|
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 07:04:56 +00:00
|
|
|
}
|
|
|
|
|
2011-10-19 08:10:59 +00:00
|
|
|
static void virtnet_get_ringparam(struct net_device *dev,
|
|
|
|
struct ethtool_ringparam *ring)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
2012-12-07 07:04:56 +00: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 08:10:59 +00:00
|
|
|
ring->rx_pending = ring->rx_max_pending;
|
|
|
|
ring->tx_pending = ring->tx_max_pending;
|
|
|
|
}
|
|
|
|
|
2011-11-14 14:17:08 +00: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 07:04:57 +00: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 05:45:41 +00:00
|
|
|
if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
|
2012-12-07 07:04:57 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2016-12-15 20:13:24 +00: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-24 23:51:29 +00:00
|
|
|
get_online_cpus();
|
2017-02-03 03:14:32 +00:00
|
|
|
err = _virtnet_set_queues(vi, queue_pairs);
|
2012-12-07 07:04:57 +00:00
|
|
|
if (!err) {
|
|
|
|
netif_set_real_num_tx_queues(dev, queue_pairs);
|
|
|
|
netif_set_real_num_rx_queues(dev, queue_pairs);
|
|
|
|
|
2013-01-24 23:51:30 +00:00
|
|
|
virtnet_set_affinity(vi);
|
2012-12-07 07:04:57 +00:00
|
|
|
}
|
2013-01-24 23:51:29 +00:00
|
|
|
put_online_cpus();
|
2012-12-07 07:04:57 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-03 03:04:37 +00:00
|
|
|
/* Check if the user is trying to change anything besides speed/duplex */
|
|
|
|
static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct ethtool_cmd diff1 = *cmd;
|
|
|
|
struct ethtool_cmd diff2 = {};
|
|
|
|
|
2016-02-07 20:52:24 +00:00
|
|
|
/* cmd is always set so we need to clear it, validate the port type
|
|
|
|
* and also without autonegotiation we can ignore advertising
|
|
|
|
*/
|
2016-02-03 03:04:37 +00:00
|
|
|
ethtool_cmd_speed_set(&diff1, 0);
|
2016-02-07 20:52:24 +00:00
|
|
|
diff2.port = PORT_OTHER;
|
2016-02-03 03:04:37 +00:00
|
|
|
diff1.advertising = 0;
|
|
|
|
diff1.duplex = 0;
|
|
|
|
diff1.cmd = 0;
|
|
|
|
|
|
|
|
return !memcmp(&diff1, &diff2, sizeof(diff1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
u32 speed;
|
|
|
|
|
|
|
|
speed = ethtool_cmd_speed(cmd);
|
|
|
|
/* don't allow custom speed and duplex */
|
|
|
|
if (!ethtool_validate_speed(speed) ||
|
|
|
|
!ethtool_validate_duplex(cmd->duplex) ||
|
|
|
|
!virtnet_validate_ethtool_cmd(cmd))
|
|
|
|
return -EINVAL;
|
|
|
|
vi->speed = speed;
|
|
|
|
vi->duplex = cmd->duplex;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
|
|
|
ethtool_cmd_speed_set(cmd, vi->speed);
|
|
|
|
cmd->duplex = vi->duplex;
|
|
|
|
cmd->port = PORT_OTHER;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtnet_init_settings(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
|
|
|
vi->speed = SPEED_UNKNOWN;
|
|
|
|
vi->duplex = DUPLEX_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2009-09-02 08:03:33 +00:00
|
|
|
static const struct ethtool_ops virtnet_ethtool_ops = {
|
2011-11-14 14:17:08 +00:00
|
|
|
.get_drvinfo = virtnet_get_drvinfo,
|
2009-01-20 01:09:49 +00:00
|
|
|
.get_link = ethtool_op_get_link,
|
2011-10-19 08:10:59 +00:00
|
|
|
.get_ringparam = virtnet_get_ringparam,
|
2012-12-07 07:04:57 +00:00
|
|
|
.set_channels = virtnet_set_channels,
|
|
|
|
.get_channels = virtnet_get_channels,
|
2014-06-25 02:37:13 +00:00
|
|
|
.get_ts_info = ethtool_op_get_ts_info,
|
2016-02-03 03:04:37 +00:00
|
|
|
.get_settings = virtnet_get_settings,
|
|
|
|
.set_settings = virtnet_set_settings,
|
2008-04-18 03:21:42 +00:00
|
|
|
};
|
|
|
|
|
2016-12-15 20:13:24 +00:00
|
|
|
static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
|
|
|
|
{
|
|
|
|
unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct bpf_prog *old_prog;
|
2016-12-15 20:13:49 +00:00
|
|
|
u16 xdp_qp = 0, curr_qp;
|
|
|
|
int i, err;
|
2016-12-15 20:13:24 +00:00
|
|
|
|
2017-01-25 22:56:36 +00:00
|
|
|
if (prog && prog->xdp_adjust_head) {
|
|
|
|
netdev_warn(dev, "Does not support bpf_xdp_adjust_head()\n");
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2016-12-15 20:13:24 +00:00
|
|
|
if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
|
2016-12-23 14:37:30 +00:00
|
|
|
virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
|
|
|
|
virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
|
|
|
|
virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
|
2016-12-15 20:13:24 +00:00
|
|
|
netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
|
|
|
|
netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->mtu > max_sz) {
|
|
|
|
netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-12-15 20:13:49 +00: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) {
|
|
|
|
netdev_warn(dev, "request %i queues but max is %i\n",
|
|
|
|
curr_qp + xdp_qp, vi->max_queue_pairs);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-02-03 03:14:32 +00:00
|
|
|
err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
|
2016-12-15 20:13:49 +00:00
|
|
|
if (err) {
|
|
|
|
dev_warn(&dev->dev, "XDP Device queue allocation failure.\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-12-15 20:13:24 +00:00
|
|
|
if (prog) {
|
|
|
|
prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
|
2016-12-15 20:13:49 +00:00
|
|
|
if (IS_ERR(prog)) {
|
2017-02-03 03:14:32 +00:00
|
|
|
_virtnet_set_queues(vi, curr_qp);
|
2016-12-15 20:13:24 +00:00
|
|
|
return PTR_ERR(prog);
|
2016-12-15 20:13:49 +00:00
|
|
|
}
|
2016-12-15 20:13:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 20:13:49 +00:00
|
|
|
vi->xdp_queue_pairs = xdp_qp;
|
|
|
|
netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
|
|
|
|
|
2016-12-15 20:13:24 +00:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
|
|
|
|
rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
|
|
|
|
if (old_prog)
|
|
|
|
bpf_prog_put(old_prog);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool virtnet_xdp_query(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
if (vi->rq[i].xdp_prog)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
|
|
|
|
{
|
|
|
|
switch (xdp->command) {
|
|
|
|
case XDP_SETUP_PROG:
|
|
|
|
return virtnet_xdp_set(dev, xdp->prog);
|
|
|
|
case XDP_QUERY_PROG:
|
|
|
|
xdp->prog_attached = virtnet_xdp_query(dev);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-06 18:44:22 +00: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 00:36:34 +00:00
|
|
|
.ndo_set_mac_address = virtnet_set_mac_address,
|
2009-02-04 09:02:40 +00:00
|
|
|
.ndo_set_rx_mode = virtnet_set_rx_mode,
|
2011-06-15 06:36:29 +00:00
|
|
|
.ndo_get_stats64 = virtnet_stats,
|
2009-05-01 17:31:10 +00:00
|
|
|
.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
|
|
|
|
.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
|
2009-01-06 18:44:22 +00:00
|
|
|
#ifdef CONFIG_NET_POLL_CONTROLLER
|
|
|
|
.ndo_poll_controller = virtnet_netpoll,
|
2014-07-23 08:33:55 +00:00
|
|
|
#endif
|
2016-12-15 20:13:24 +00:00
|
|
|
.ndo_xdp = virtnet_xdp,
|
2009-01-06 18:44:22 +00:00
|
|
|
};
|
|
|
|
|
2012-04-11 20:43:52 +00:00
|
|
|
static void virtnet_config_changed_work(struct work_struct *work)
|
2009-01-20 01:09:49 +00:00
|
|
|
{
|
2012-04-11 20:43:52 +00:00
|
|
|
struct virtnet_info *vi =
|
|
|
|
container_of(work, struct virtnet_info, config_work);
|
2009-01-20 01:09:49 +00:00
|
|
|
u16 v;
|
|
|
|
|
2013-10-14 07:41:51 +00:00
|
|
|
if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
|
|
|
|
struct virtio_net_config, status, &v) < 0)
|
2014-10-14 23:52:30 +00:00
|
|
|
return;
|
2012-04-11 20:43:52 +00:00
|
|
|
|
|
|
|
if (v & VIRTIO_NET_S_ANNOUNCE) {
|
2012-08-09 22:14:56 +00:00
|
|
|
netdev_notify_peers(vi->dev);
|
2012-04-11 20:43:52 +00:00
|
|
|
virtnet_ack_link_announce(vi);
|
|
|
|
}
|
2009-01-20 01:09:49 +00:00
|
|
|
|
|
|
|
/* Ignore unknown (future) status bits */
|
|
|
|
v &= VIRTIO_NET_S_LINK_UP;
|
|
|
|
|
|
|
|
if (vi->status == v)
|
2014-10-14 23:52:30 +00:00
|
|
|
return;
|
2009-01-20 01:09:49 +00:00
|
|
|
|
|
|
|
vi->status = v;
|
|
|
|
|
|
|
|
if (vi->status & VIRTIO_NET_S_LINK_UP) {
|
|
|
|
netif_carrier_on(vi->dev);
|
2012-12-07 07:04:56 +00:00
|
|
|
netif_tx_wake_all_queues(vi->dev);
|
2009-01-20 01:09:49 +00:00
|
|
|
} else {
|
|
|
|
netif_carrier_off(vi->dev);
|
2012-12-07 07:04:56 +00:00
|
|
|
netif_tx_stop_all_queues(vi->dev);
|
2009-01-20 01:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtnet_config_changed(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
|
2012-08-20 21:51:24 +00:00
|
|
|
schedule_work(&vi->config_work);
|
2009-01-20 01:09:49 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
static void virtnet_free_queues(struct virtnet_info *vi)
|
|
|
|
{
|
2013-12-05 14:36:21 +00:00
|
|
|
int i;
|
|
|
|
|
2015-03-12 05:57:44 +00:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
napi_hash_del(&vi->rq[i].napi);
|
2013-12-05 14:36:21 +00:00
|
|
|
netif_napi_del(&vi->rq[i].napi);
|
2015-03-12 05:57:44 +00:00
|
|
|
}
|
2013-12-05 14:36:21 +00:00
|
|
|
|
2016-11-16 06:24:12 +00: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 07:04:56 +00:00
|
|
|
kfree(vi->rq);
|
|
|
|
kfree(vi->sq);
|
|
|
|
}
|
|
|
|
|
2017-02-03 03:14:32 +00:00
|
|
|
static void _free_receive_bufs(struct virtnet_info *vi)
|
2012-12-07 07:04:56 +00:00
|
|
|
{
|
2016-12-15 20:13:24 +00:00
|
|
|
struct bpf_prog *old_prog;
|
2012-12-07 07:04:56 +00: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-15 20:13:24 +00: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 07:04:56 +00:00
|
|
|
}
|
2017-02-03 03:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void free_receive_bufs(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
rtnl_lock();
|
|
|
|
_free_receive_bufs(vi);
|
2016-12-15 20:13:24 +00:00
|
|
|
rtnl_unlock();
|
2012-12-07 07:04:56 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 06:23:26 +00: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);
|
|
|
|
}
|
|
|
|
|
2017-01-26 02:22:48 +00:00
|
|
|
static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
|
2016-12-15 20:14:13 +00:00
|
|
|
{
|
2017-01-26 02:22:48 +00:00
|
|
|
/* For small receive mode always use kfree_skb variants */
|
|
|
|
if (!vi->mergeable_rx_bufs)
|
|
|
|
return false;
|
|
|
|
|
2016-12-15 20:14:13 +00:00
|
|
|
if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
|
|
|
|
return false;
|
|
|
|
else if (q < vi->curr_queue_pairs)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-07 07:04:56 +00: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-15 20:14:13 +00:00
|
|
|
while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
|
2017-01-26 02:22:48 +00:00
|
|
|
if (!is_xdp_raw_buffer_queue(vi, i))
|
2016-12-15 20:14:13 +00:00
|
|
|
dev_kfree_skb(buf);
|
|
|
|
else
|
|
|
|
put_page(virt_to_head_page(buf));
|
|
|
|
}
|
2012-12-07 07:04:56 +00: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 06:23:27 +00:00
|
|
|
if (vi->mergeable_rx_bufs) {
|
|
|
|
unsigned long ctx = (unsigned long)buf;
|
|
|
|
void *base = mergeable_ctx_to_buf_address(ctx);
|
|
|
|
put_page(virt_to_head_page(base));
|
|
|
|
} else if (vi->big_packets) {
|
2013-12-05 14:36:20 +00:00
|
|
|
give_pages(&vi->rq[i], buf);
|
2014-01-17 06:23:27 +00:00
|
|
|
} else {
|
2012-12-07 07:04:56 +00:00
|
|
|
dev_kfree_skb(buf);
|
2014-01-17 06:23:27 +00:00
|
|
|
}
|
2012-12-07 07:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
static void virtnet_del_vqs(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
struct virtio_device *vdev = vi->vdev;
|
|
|
|
|
2013-01-24 23:51:30 +00:00
|
|
|
virtnet_clean_affinity(vi, -1);
|
2012-12-07 07:04:56 +00:00
|
|
|
|
2012-12-07 07:04:55 +00:00
|
|
|
vdev->config->del_vqs(vdev);
|
2012-12-07 07:04:56 +00:00
|
|
|
|
|
|
|
virtnet_free_queues(vi);
|
2012-12-07 07:04:55 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
static int virtnet_find_vqs(struct virtnet_info *vi)
|
2011-12-22 11:28:31 +00:00
|
|
|
{
|
2012-12-07 07:04:56 +00:00
|
|
|
vq_callback_t **callbacks;
|
|
|
|
struct virtqueue **vqs;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
int i, total_vqs;
|
|
|
|
const char **names;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
|
|
|
|
if (!vqs)
|
|
|
|
goto err_vq;
|
|
|
|
callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
|
|
|
|
if (!callbacks)
|
|
|
|
goto err_callback;
|
|
|
|
names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
|
|
|
|
if (!names)
|
|
|
|
goto err_names;
|
|
|
|
|
|
|
|
/* Parameters for control virtqueue, if any */
|
|
|
|
if (vi->has_cvq) {
|
|
|
|
callbacks[total_vqs - 1] = NULL;
|
|
|
|
names[total_vqs - 1] = "control";
|
|
|
|
}
|
2011-12-22 11:28:31 +00:00
|
|
|
|
2012-12-07 07:04:56 +00: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;
|
|
|
|
}
|
2011-12-22 11:28:31 +00:00
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
|
|
|
|
names);
|
|
|
|
if (ret)
|
|
|
|
goto err_find;
|
2011-12-22 11:28:31 +00:00
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
if (vi->has_cvq) {
|
|
|
|
vi->cvq = vqs[total_vqs - 1];
|
2011-12-22 11:28:31 +00:00
|
|
|
if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
|
2013-04-19 02:04:27 +00:00
|
|
|
vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
|
2011-12-22 11:28:31 +00:00
|
|
|
}
|
2012-12-07 07:04:56 +00:00
|
|
|
|
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++) {
|
|
|
|
vi->rq[i].vq = vqs[rxq2vq(i)];
|
|
|
|
vi->sq[i].vq = vqs[txq2vq(i)];
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(names);
|
|
|
|
kfree(callbacks);
|
|
|
|
kfree(vqs);
|
|
|
|
|
2011-12-22 11:28:31 +00:00
|
|
|
return 0;
|
2012-12-07 07:04:56 +00:00
|
|
|
|
|
|
|
err_find:
|
|
|
|
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;
|
|
|
|
|
|
|
|
vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
|
|
|
|
if (!vi->sq)
|
|
|
|
goto err_sq;
|
|
|
|
vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
|
2012-12-10 02:24:08 +00:00
|
|
|
if (!vi->rq)
|
2012-12-07 07:04:56 +00: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);
|
|
|
|
|
|
|
|
sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
|
2015-08-19 07:48:40 +00:00
|
|
|
ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
|
2012-12-07 07:04:56 +00:00
|
|
|
sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_rq:
|
|
|
|
kfree(vi->sq);
|
|
|
|
err_sq:
|
|
|
|
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-24 23:51:29 +00:00
|
|
|
get_online_cpus();
|
2013-01-24 23:51:30 +00:00
|
|
|
virtnet_set_affinity(vi);
|
2013-01-24 23:51:29 +00:00
|
|
|
put_online_cpus();
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_free:
|
|
|
|
virtnet_free_queues(vi);
|
|
|
|
err:
|
|
|
|
return ret;
|
2011-12-22 11:28:31 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 06:23:30 +00:00
|
|
|
#ifdef CONFIG_SYSFS
|
|
|
|
static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
|
|
|
|
struct rx_queue_attribute *attribute, char *buf)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(queue->dev);
|
|
|
|
unsigned int queue_index = get_netdev_rx_queue_index(queue);
|
2015-08-19 07:48:40 +00:00
|
|
|
struct ewma_pkt_len *avg;
|
2014-01-17 06:23:30 +00:00
|
|
|
|
|
|
|
BUG_ON(queue_index >= vi->max_queue_pairs);
|
|
|
|
avg = &vi->rq[queue_index].mrg_avg_pkt_len;
|
|
|
|
return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
|
|
|
|
}
|
|
|
|
|
|
|
|
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 09:03:05 +00: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-20 17:55:21 +00:00
|
|
|
#define MIN_MTU ETH_MIN_MTU
|
|
|
|
#define MAX_MTU ETH_MAX_MTU
|
|
|
|
|
2007-10-22 01:03:37 +00:00
|
|
|
static int virtnet_probe(struct virtio_device *vdev)
|
|
|
|
{
|
2012-12-07 07:04:56 +00:00
|
|
|
int i, err;
|
2007-10-22 01:03:37 +00:00
|
|
|
struct net_device *dev;
|
|
|
|
struct virtnet_info *vi;
|
2012-12-07 07:04:56 +00:00
|
|
|
u16 max_queue_pairs;
|
2016-06-03 20:57:12 +00:00
|
|
|
int mtu;
|
2012-12-07 07:04:56 +00:00
|
|
|
|
2015-01-12 14:23:37 +00:00
|
|
|
if (!vdev->config->get) {
|
|
|
|
dev_err(&vdev->dev, "%s failure: config access disabled\n",
|
|
|
|
__func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-11-20 09:03:05 +00:00
|
|
|
if (!virtnet_validate_features(vdev))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
/* Find if host supports multiqueue virtio_net device */
|
2013-10-14 07:41:51 +00:00
|
|
|
err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
|
|
|
|
struct virtio_net_config,
|
|
|
|
max_virtqueue_pairs, &max_queue_pairs);
|
2012-12-07 07:04:56 +00: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 01:03:37 +00:00
|
|
|
|
|
|
|
/* Allocate ourselves a network device with room for our info */
|
2012-12-07 07:04:56 +00:00
|
|
|
dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
|
2007-10-22 01:03:37 +00:00
|
|
|
if (!dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Set up network device as normal. */
|
2012-06-29 05:10:06 +00:00
|
|
|
dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
|
2009-01-06 18:44:22 +00:00
|
|
|
dev->netdev_ops = &virtnet_netdev;
|
2007-10-22 01:03:37 +00:00
|
|
|
dev->features = NETIF_F_HIGHDMA;
|
2011-06-15 06:36:29 +00:00
|
|
|
|
2014-05-11 00:12:32 +00:00
|
|
|
dev->ethtool_ops = &virtnet_ethtool_ops;
|
2007-10-22 01:03:37 +00:00
|
|
|
SET_NETDEV_DEV(dev, &vdev->dev);
|
|
|
|
|
|
|
|
/* Do we support "hardware" checksums? */
|
2011-03-31 01:01:35 +00:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
|
2007-10-22 01:03:37 +00:00
|
|
|
/* This opens up the world of extra features. */
|
2015-08-05 02:34:04 +00:00
|
|
|
dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
|
2011-03-31 01:01:35 +00:00
|
|
|
if (csum)
|
2015-08-05 02:34:04 +00:00
|
|
|
dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
|
2011-03-31 01:01:35 +00:00
|
|
|
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
|
2015-02-03 21:36:17 +00:00
|
|
|
dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
|
2008-02-05 04:50:02 +00:00
|
|
|
| NETIF_F_TSO_ECN | NETIF_F_TSO6;
|
|
|
|
}
|
2008-05-03 02:50:46 +00:00
|
|
|
/* Individual feature bits: what can host handle? */
|
2011-03-31 01:01:35 +00: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;
|
2015-02-03 21:36:17 +00:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
|
|
|
|
dev->hw_features |= NETIF_F_UFO;
|
2011-03-31 01:01:35 +00:00
|
|
|
|
2014-12-24 03:03:52 +00:00
|
|
|
dev->features |= NETIF_F_GSO_ROBUST;
|
|
|
|
|
2011-03-31 01:01:35 +00:00
|
|
|
if (gso)
|
2015-02-03 21:36:17 +00:00
|
|
|
dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
|
2011-03-31 01:01:35 +00:00
|
|
|
/* (!csum && gso) case will be fixed by register_netdev() */
|
2007-10-22 01:03:37 +00:00
|
|
|
}
|
2013-08-27 15:09:02 +00:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
|
|
|
|
dev->features |= NETIF_F_RXCSUM;
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2013-04-10 23:32:21 +00:00
|
|
|
dev->vlan_features = dev->features;
|
|
|
|
|
2016-10-20 17:55:21 +00:00
|
|
|
/* MTU range: 68 - 65535 */
|
|
|
|
dev->min_mtu = MIN_MTU;
|
|
|
|
dev->max_mtu = MAX_MTU;
|
|
|
|
|
2007-10-22 01:03:37 +00:00
|
|
|
/* Configuration may specify what MAC to use. Otherwise random. */
|
2013-10-14 07:41:51 +00: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 06:45:39 +00:00
|
|
|
eth_hw_addr_random(dev);
|
2007-10-22 01:03:37 +00:00
|
|
|
|
|
|
|
/* Set up our device-specific information */
|
|
|
|
vi = netdev_priv(dev);
|
|
|
|
vi->dev = dev;
|
|
|
|
vi->vdev = vdev;
|
2008-02-18 09:02:51 +00:00
|
|
|
vdev->priv = vi;
|
2011-06-15 06:36:29 +00:00
|
|
|
vi->stats = alloc_percpu(struct virtnet_stats);
|
|
|
|
err = -ENOMEM;
|
|
|
|
if (vi->stats == NULL)
|
|
|
|
goto free;
|
|
|
|
|
2013-10-07 22:51:58 +00:00
|
|
|
for_each_possible_cpu(i) {
|
|
|
|
struct virtnet_stats *virtnet_stats;
|
|
|
|
virtnet_stats = per_cpu_ptr(vi->stats, i);
|
|
|
|
u64_stats_init(&virtnet_stats->tx_syncp);
|
|
|
|
u64_stats_init(&virtnet_stats->rx_syncp);
|
|
|
|
}
|
|
|
|
|
2012-04-11 20:43:52 +00:00
|
|
|
INIT_WORK(&vi->config_work, virtnet_config_changed_work);
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2008-04-18 03:24:27 +00:00
|
|
|
/* If we can receive ANY GSO packets, we must allocate large ones. */
|
2009-12-03 07:58:21 +00:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
|
2015-02-03 21:36:17 +00:00
|
|
|
virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
|
2008-04-18 03:24:27 +00:00
|
|
|
vi->big_packets = true;
|
|
|
|
|
2008-11-17 06:41:34 +00:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
|
|
|
|
vi->mergeable_rx_bufs = true;
|
|
|
|
|
2014-10-23 21:24:03 +00:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
|
2014-10-24 13:55:57 +00:00
|
|
|
vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
|
|
|
|
else
|
|
|
|
vi->hdr_len = sizeof(struct virtio_net_hdr);
|
|
|
|
|
2015-07-15 12:26:19 +00:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
|
2013-07-25 00:50:23 +00:00
|
|
|
vi->any_header_sg = true;
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
|
|
|
|
vi->has_cvq = true;
|
|
|
|
|
2016-06-03 20:57:12 +00:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
|
|
|
|
mtu = virtio_cread16(vdev,
|
|
|
|
offsetof(struct virtio_net_config,
|
|
|
|
mtu));
|
2016-10-25 20:12:12 +00:00
|
|
|
if (mtu < dev->min_mtu) {
|
2016-06-03 20:57:12 +00:00
|
|
|
__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
|
2016-10-25 20:12:12 +00:00
|
|
|
} else {
|
2016-10-20 17:55:21 +00:00
|
|
|
dev->mtu = mtu;
|
2016-10-25 20:12:12 +00:00
|
|
|
dev->max_mtu = mtu;
|
|
|
|
}
|
2016-06-03 20:57:12 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 13:55:57 +00: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 10:43:22 +00:00
|
|
|
|
2016-11-25 04:37:26 +00: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 07:04:56 +00:00
|
|
|
vi->max_queue_pairs = max_queue_pairs;
|
|
|
|
|
|
|
|
/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
|
2011-12-22 11:28:31 +00:00
|
|
|
err = init_vqs(vi);
|
2009-06-13 04:16:36 +00:00
|
|
|
if (err)
|
2013-11-05 10:19:45 +00:00
|
|
|
goto free_stats;
|
2007-10-22 01:03:37 +00:00
|
|
|
|
2014-01-17 06:23:30 +00:00
|
|
|
#ifdef CONFIG_SYSFS
|
|
|
|
if (vi->mergeable_rx_bufs)
|
|
|
|
dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
|
|
|
|
#endif
|
2013-11-18 13:19:27 +00: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 07:04:56 +00:00
|
|
|
|
2016-02-03 03:04:37 +00:00
|
|
|
virtnet_init_settings(dev);
|
|
|
|
|
2007-10-22 01:03:37 +00:00
|
|
|
err = register_netdev(dev);
|
|
|
|
if (err) {
|
|
|
|
pr_debug("virtio_net: registering device failed\n");
|
2009-06-13 04:16:36 +00:00
|
|
|
goto free_vqs;
|
2007-10-22 01:03:37 +00:00
|
|
|
}
|
2008-02-05 04:50:02 +00:00
|
|
|
|
2014-10-14 23:52:30 +00:00
|
|
|
virtio_device_ready(vdev);
|
|
|
|
|
2016-08-12 17:49:43 +00:00
|
|
|
err = virtnet_cpu_notif_add(vi);
|
2013-01-24 23:51:31 +00:00
|
|
|
if (err) {
|
|
|
|
pr_debug("virtio_net: registering cpu notifier failed\n");
|
2016-05-31 03:52:43 +00:00
|
|
|
goto free_unregister_netdev;
|
2013-01-24 23:51:31 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 06:23:05 +00:00
|
|
|
virtnet_set_queues(vi, vi->curr_queue_pairs);
|
2016-11-25 04:37:26 +00:00
|
|
|
|
2010-11-10 14:45:41 +00:00
|
|
|
/* Assume link up if device can't report link status,
|
|
|
|
otherwise get link status from config. */
|
|
|
|
if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
|
|
|
|
netif_carrier_off(dev);
|
2012-08-20 21:51:24 +00:00
|
|
|
schedule_work(&vi->config_work);
|
2010-11-10 14:45:41 +00:00
|
|
|
} else {
|
|
|
|
vi->status = VIRTIO_NET_S_LINK_UP;
|
|
|
|
netif_carrier_on(dev);
|
|
|
|
}
|
2009-01-20 01:09:49 +00:00
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
|
|
|
|
dev->name, max_queue_pairs);
|
|
|
|
|
2007-10-22 01:03:37 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-05-31 03:52:43 +00:00
|
|
|
free_unregister_netdev:
|
2014-10-14 23:52:31 +00:00
|
|
|
vi->vdev->config->reset(vdev);
|
|
|
|
|
2008-02-05 04:50:02 +00:00
|
|
|
unregister_netdev(dev);
|
2009-06-13 04:16:36 +00:00
|
|
|
free_vqs:
|
2012-12-07 07:04:56 +00:00
|
|
|
cancel_delayed_work_sync(&vi->refill);
|
2014-01-17 06:23:26 +00:00
|
|
|
free_receive_page_frags(vi);
|
2012-12-07 07:04:55 +00:00
|
|
|
virtnet_del_vqs(vi);
|
2011-06-15 06:36:29 +00:00
|
|
|
free_stats:
|
|
|
|
free_percpu(vi->stats);
|
2007-10-22 01:03:37 +00:00
|
|
|
free:
|
|
|
|
free_netdev(dev);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-12-22 11:28:32 +00:00
|
|
|
static void remove_vq_common(struct virtnet_info *vi)
|
2007-10-22 01:03:37 +00:00
|
|
|
{
|
2011-12-22 11:28:32 +00:00
|
|
|
vi->vdev->config->reset(vi->vdev);
|
2010-02-08 14:14:42 +00:00
|
|
|
|
|
|
|
/* Free unused buffers in both send and recv, if any. */
|
2010-01-29 03:20:04 +00:00
|
|
|
free_unused_bufs(vi);
|
2008-07-25 17:06:01 +00:00
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
free_receive_bufs(vi);
|
2009-06-13 04:16:36 +00:00
|
|
|
|
2014-01-17 06:23:26 +00:00
|
|
|
free_receive_page_frags(vi);
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
virtnet_del_vqs(vi);
|
2011-12-22 11:28:32 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 14:24:15 +00:00
|
|
|
static void virtnet_remove(struct virtio_device *vdev)
|
2011-12-22 11:28:32 +00:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
|
2016-08-12 17:49:43 +00:00
|
|
|
virtnet_cpu_notif_remove(vi);
|
2013-01-24 23:51:31 +00:00
|
|
|
|
2014-10-14 23:52:29 +00:00
|
|
|
/* Make sure no work handler is accessing the device. */
|
|
|
|
flush_work(&vi->config_work);
|
2012-04-11 20:43:52 +00:00
|
|
|
|
2011-12-22 11:28:32 +00:00
|
|
|
unregister_netdev(vi->dev);
|
|
|
|
|
|
|
|
remove_vq_common(vi);
|
2008-07-25 17:06:01 +00:00
|
|
|
|
2011-07-20 03:56:02 +00:00
|
|
|
free_percpu(vi->stats);
|
2007-11-19 16:20:42 +00:00
|
|
|
free_netdev(vi->dev);
|
2007-10-22 01:03:37 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 23:55:23 +00:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2011-12-22 11:28:33 +00:00
|
|
|
static int virtnet_freeze(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
2012-12-07 07:04:56 +00:00
|
|
|
int i;
|
2011-12-22 11:28:33 +00:00
|
|
|
|
2016-08-12 17:49:43 +00:00
|
|
|
virtnet_cpu_notif_remove(vi);
|
2013-10-29 07:11:07 +00:00
|
|
|
|
2014-10-14 23:52:29 +00:00
|
|
|
/* Make sure no work handler is accessing the device */
|
|
|
|
flush_work(&vi->config_work);
|
2012-04-11 20:43:52 +00:00
|
|
|
|
2011-12-22 11:28:33 +00:00
|
|
|
netif_device_detach(vi->dev);
|
|
|
|
cancel_delayed_work_sync(&vi->refill);
|
|
|
|
|
2014-07-23 08:33:55 +00:00
|
|
|
if (netif_running(vi->dev)) {
|
2015-03-12 05:57:44 +00:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++)
|
2012-12-07 07:04:56 +00:00
|
|
|
napi_disable(&vi->rq[i].napi);
|
2014-07-23 08:33:55 +00:00
|
|
|
}
|
2011-12-22 11:28:33 +00:00
|
|
|
|
|
|
|
remove_vq_common(vi);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_restore(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
2012-12-07 07:04:56 +00:00
|
|
|
int err, i;
|
2011-12-22 11:28:33 +00:00
|
|
|
|
|
|
|
err = init_vqs(vi);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2014-10-14 23:52:32 +00:00
|
|
|
virtio_device_ready(vdev);
|
|
|
|
|
2013-12-30 03:34:40 +00:00
|
|
|
if (netif_running(vi->dev)) {
|
|
|
|
for (i = 0; i < vi->curr_queue_pairs; i++)
|
2014-10-23 21:12:10 +00:00
|
|
|
if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
|
2013-12-30 03:34:40 +00:00
|
|
|
schedule_delayed_work(&vi->refill, 0);
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
for (i = 0; i < vi->max_queue_pairs; i++)
|
|
|
|
virtnet_napi_enable(&vi->rq[i]);
|
2013-12-30 03:34:40 +00:00
|
|
|
}
|
2011-12-22 11:28:33 +00:00
|
|
|
|
|
|
|
netif_device_attach(vi->dev);
|
|
|
|
|
2012-12-07 07:04:56 +00:00
|
|
|
virtnet_set_queues(vi, vi->curr_queue_pairs);
|
|
|
|
|
2016-08-12 17:49:43 +00:00
|
|
|
err = virtnet_cpu_notif_add(vi);
|
2013-10-29 07:11:07 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2011-12-22 11:28:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-22 01:03:37 +00:00
|
|
|
static struct virtio_device_id id_table[] = {
|
|
|
|
{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
|
|
|
|
{ 0 },
|
|
|
|
};
|
|
|
|
|
2016-11-04 10:55:36 +00: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, \
|
|
|
|
VIRTIO_NET_F_MTU
|
|
|
|
|
2008-05-03 02:50:50 +00:00
|
|
|
static unsigned int features[] = {
|
2016-11-04 10:55:36 +00:00
|
|
|
VIRTNET_FEATURES,
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned int features_legacy[] = {
|
|
|
|
VIRTNET_FEATURES,
|
|
|
|
VIRTIO_NET_F_GSO,
|
2013-07-25 00:50:23 +00:00
|
|
|
VIRTIO_F_ANY_LAYOUT,
|
2008-05-03 02:50:50 +00:00
|
|
|
};
|
|
|
|
|
2009-11-05 09:32:44 +00:00
|
|
|
static struct virtio_driver virtio_net_driver = {
|
2008-05-03 02:50:50 +00:00
|
|
|
.feature_table = features,
|
|
|
|
.feature_table_size = ARRAY_SIZE(features),
|
2016-11-04 10:55:36 +00:00
|
|
|
.feature_table_legacy = features_legacy,
|
|
|
|
.feature_table_size_legacy = ARRAY_SIZE(features_legacy),
|
2007-10-22 01:03:37 +00:00
|
|
|
.driver.name = KBUILD_MODNAME,
|
|
|
|
.driver.owner = THIS_MODULE,
|
|
|
|
.id_table = id_table,
|
|
|
|
.probe = virtnet_probe,
|
2012-12-03 14:24:15 +00:00
|
|
|
.remove = virtnet_remove,
|
2009-01-20 01:09:49 +00:00
|
|
|
.config_changed = virtnet_config_changed,
|
2013-09-16 23:55:23 +00:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2011-12-22 11:28:33 +00:00
|
|
|
.freeze = virtnet_freeze,
|
|
|
|
.restore = virtnet_restore,
|
|
|
|
#endif
|
2007-10-22 01:03:37 +00:00
|
|
|
};
|
|
|
|
|
2016-08-12 17:49:43 +00:00
|
|
|
static __init int virtio_net_driver_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-12-21 19:19:54 +00:00
|
|
|
ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
|
2016-08-12 17:49:43 +00:00
|
|
|
virtnet_cpu_online,
|
|
|
|
virtnet_cpu_down_prep);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
virtionet_online = ret;
|
2016-12-21 19:19:54 +00:00
|
|
|
ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
|
2016-08-12 17:49:43 +00: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)
|
|
|
|
{
|
|
|
|
cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
|
|
|
|
cpuhp_remove_multi_state(virtionet_online);
|
|
|
|
unregister_virtio_driver(&virtio_net_driver);
|
|
|
|
}
|
|
|
|
module_exit(virtio_net_driver_exit);
|
2007-10-22 01:03:37 +00:00
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(virtio, id_table);
|
|
|
|
MODULE_DESCRIPTION("Virtio network driver");
|
|
|
|
MODULE_LICENSE("GPL");
|