2009-09-24 23:59:20 +08:00
|
|
|
/* A network driver using virtio.
|
2007-10-22 09:03:37 +08:00
|
|
|
*
|
|
|
|
* Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
//#define DEBUG
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/etherdevice.h>
|
2008-04-18 11:21:42 +08:00
|
|
|
#include <linux/ethtool.h>
|
2007-10-22 09:03:37 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/virtio.h>
|
|
|
|
#include <linux/virtio_net.h>
|
|
|
|
#include <linux/scatterlist.h>
|
2009-01-26 10:06:26 +08:00
|
|
|
#include <linux/if_vlan.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2007-12-16 21:19:43 +08:00
|
|
|
static int napi_weight = 128;
|
|
|
|
module_param(napi_weight, int, 0444);
|
|
|
|
|
2011-12-19 22:08:01 +08:00
|
|
|
static bool csum = true, gso = true;
|
2008-02-05 12:50:02 +08:00
|
|
|
module_param(csum, bool, 0444);
|
|
|
|
module_param(gso, bool, 0444);
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
/* FIXME: MTU in config. */
|
2009-01-26 10:06:26 +08:00
|
|
|
#define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
|
2008-11-17 14:41:34 +08:00
|
|
|
#define GOOD_COPY_LEN 128
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2009-02-04 17:02:45 +08:00
|
|
|
#define VIRTNET_SEND_COMMAND_SG_MAX 2
|
2011-11-14 22:17:08 +08:00
|
|
|
#define VIRTNET_DRIVER_VERSION "1.0.0"
|
2009-02-04 17:02:34 +08:00
|
|
|
|
2011-06-15 14:36:29 +08:00
|
|
|
struct virtnet_stats {
|
2012-06-06 06:35:24 +08:00
|
|
|
struct u64_stats_sync tx_syncp;
|
|
|
|
struct u64_stats_sync rx_syncp;
|
2011-06-15 14:36:29 +08:00
|
|
|
u64 tx_bytes;
|
|
|
|
u64 tx_packets;
|
|
|
|
|
|
|
|
u64 rx_bytes;
|
|
|
|
u64 rx_packets;
|
|
|
|
};
|
|
|
|
|
2010-04-08 12:01:41 +08:00
|
|
|
struct virtnet_info {
|
2007-10-22 09:03:37 +08:00
|
|
|
struct virtio_device *vdev;
|
2009-02-04 17:02:34 +08:00
|
|
|
struct virtqueue *rvq, *svq, *cvq;
|
2007-10-22 09:03:37 +08:00
|
|
|
struct net_device *dev;
|
|
|
|
struct napi_struct napi;
|
2009-01-20 09:09:49 +08:00
|
|
|
unsigned int status;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
|
|
|
/* Number of input buffers, and max we've ever had. */
|
|
|
|
unsigned int num, max;
|
|
|
|
|
2008-04-18 11:24:27 +08:00
|
|
|
/* I like... big packets and I cannot lie! */
|
|
|
|
bool big_packets;
|
|
|
|
|
2008-11-17 14:41:34 +08:00
|
|
|
/* Host will merge rx buffers for big packets (shake it! shake it!) */
|
|
|
|
bool mergeable_rx_bufs;
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
/* enable config space updates */
|
|
|
|
bool config_enable;
|
|
|
|
|
2011-06-15 14:36:29 +08:00
|
|
|
/* Active statistics */
|
|
|
|
struct virtnet_stats __percpu *stats;
|
|
|
|
|
2009-08-27 03:22:32 +08:00
|
|
|
/* Work struct for refilling if we run low on memory. */
|
|
|
|
struct delayed_work refill;
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
/* Work struct for config space updates */
|
|
|
|
struct work_struct config_work;
|
|
|
|
|
|
|
|
/* Lock for config space updates */
|
|
|
|
struct mutex config_lock;
|
|
|
|
|
2008-07-26 01:06:01 +08:00
|
|
|
/* Chain pages by the private ptr. */
|
|
|
|
struct page *pages;
|
2010-04-08 12:01:41 +08:00
|
|
|
|
|
|
|
/* fragments + linear part + virtio header */
|
|
|
|
struct scatterlist rx_sg[MAX_SKB_FRAGS + 2];
|
|
|
|
struct scatterlist tx_sg[MAX_SKB_FRAGS + 2];
|
2007-10-22 09:03:37 +08:00
|
|
|
};
|
|
|
|
|
2009-09-24 23:59:19 +08:00
|
|
|
struct skb_vnet_hdr {
|
|
|
|
union {
|
|
|
|
struct virtio_net_hdr hdr;
|
|
|
|
struct virtio_net_hdr_mrg_rxbuf mhdr;
|
|
|
|
};
|
2009-09-24 23:59:20 +08:00
|
|
|
unsigned int num_sg;
|
2009-09-24 23:59:19 +08:00
|
|
|
};
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
struct padded_vnet_hdr {
|
|
|
|
struct virtio_net_hdr hdr;
|
|
|
|
/*
|
|
|
|
* virtio_net_hdr should be in a separated sg buffer because of a
|
|
|
|
* QEMU bug, and data sg buffer shares same page with this header sg.
|
|
|
|
* This padding makes next sg 16 byte aligned after virtio_net_hdr.
|
|
|
|
*/
|
|
|
|
char padding[6];
|
|
|
|
};
|
|
|
|
|
2009-09-24 23:59:19 +08:00
|
|
|
static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2009-09-24 23:59:19 +08:00
|
|
|
return (struct skb_vnet_hdr *)skb->cb;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
/*
|
|
|
|
* private is used to chain pages for big packets, put the whole
|
|
|
|
* most recent used list in the beginning for reuse
|
|
|
|
*/
|
|
|
|
static void give_pages(struct virtnet_info *vi, struct page *page)
|
2008-11-17 14:39:18 +08:00
|
|
|
{
|
2010-01-29 11:20:04 +08:00
|
|
|
struct page *end;
|
2008-11-17 14:39:18 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
/* Find end of list, sew whole thing into vi->pages. */
|
|
|
|
for (end = page; end->private; end = (struct page *)end->private);
|
|
|
|
end->private = (unsigned long)vi->pages;
|
|
|
|
vi->pages = page;
|
2008-11-17 14:39:18 +08:00
|
|
|
}
|
|
|
|
|
2008-07-26 01:06:01 +08:00
|
|
|
static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
|
|
|
|
{
|
|
|
|
struct page *p = vi->pages;
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
if (p) {
|
2008-07-26 01:06:01 +08:00
|
|
|
vi->pages = (struct page *)p->private;
|
2010-01-29 11:20:04 +08:00
|
|
|
/* clear private here, it is used to chain pages */
|
|
|
|
p->private = 0;
|
|
|
|
} else
|
2008-07-26 01:06:01 +08:00
|
|
|
p = alloc_page(gfp_mask);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-02-05 12:50:07 +08:00
|
|
|
static void skb_xmit_done(struct virtqueue *svq)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2008-02-05 12:50:07 +08:00
|
|
|
struct virtnet_info *vi = svq->vdev->priv;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2008-02-05 12:50:07 +08:00
|
|
|
/* Suppress further interrupts. */
|
2010-04-12 21:19:04 +08:00
|
|
|
virtqueue_disable_cb(svq);
|
2008-05-26 15:48:13 +08:00
|
|
|
|
2008-06-08 18:51:55 +08:00
|
|
|
/* We were probably waiting for more output buffers. */
|
2007-10-22 09:03:37 +08:00
|
|
|
netif_wake_queue(vi->dev);
|
|
|
|
}
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
static void set_skb_frag(struct sk_buff *skb, struct page *page,
|
|
|
|
unsigned int offset, unsigned int *len)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2011-10-20 06:17:27 +08:00
|
|
|
int size = min((unsigned)PAGE_SIZE - offset, *len);
|
2010-01-29 11:20:04 +08:00
|
|
|
int i = skb_shinfo(skb)->nr_frags;
|
|
|
|
|
2011-10-20 06:17:27 +08:00
|
|
|
__skb_fill_page_desc(skb, i, page, offset, size);
|
2010-01-29 11:20:04 +08:00
|
|
|
|
2011-10-20 06:17:27 +08:00
|
|
|
skb->data_len += size;
|
|
|
|
skb->len += size;
|
2011-10-20 07:14:46 +08:00
|
|
|
skb->truesize += PAGE_SIZE;
|
2010-01-29 11:20:04 +08:00
|
|
|
skb_shinfo(skb)->nr_frags++;
|
2011-10-20 06:17:27 +08:00
|
|
|
*len -= size;
|
2010-01-29 11:20:04 +08:00
|
|
|
}
|
2008-06-08 18:49:00 +08:00
|
|
|
|
2012-01-04 20:52:32 +08:00
|
|
|
/* Called from bottom half context */
|
2010-01-29 11:20:04 +08:00
|
|
|
static struct sk_buff *page_to_skb(struct virtnet_info *vi,
|
|
|
|
struct page *page, unsigned int len)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
struct skb_vnet_hdr *hdr;
|
|
|
|
unsigned int copy, hdr_len, offset;
|
|
|
|
char *p;
|
2008-07-26 01:06:01 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
p = page_address(page);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
/* copy small packet so we can reuse these pages for small data */
|
|
|
|
skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
|
|
|
|
if (unlikely(!skb))
|
|
|
|
return NULL;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
hdr = skb_vnet_hdr(skb);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
if (vi->mergeable_rx_bufs) {
|
|
|
|
hdr_len = sizeof hdr->mhdr;
|
|
|
|
offset = hdr_len;
|
|
|
|
} else {
|
|
|
|
hdr_len = sizeof hdr->hdr;
|
|
|
|
offset = sizeof(struct padded_vnet_hdr);
|
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
memcpy(hdr, p, hdr_len);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
len -= hdr_len;
|
|
|
|
p += offset;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
copy = len;
|
|
|
|
if (copy > skb_tailroom(skb))
|
|
|
|
copy = skb_tailroom(skb);
|
|
|
|
memcpy(skb_put(skb, copy), p, copy);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
len -= copy;
|
|
|
|
offset += copy;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2011-09-28 12:40:54 +08:00
|
|
|
/*
|
|
|
|
* Verify that we can indeed put this data into a skb.
|
|
|
|
* This is here to handle cases when the device erroneously
|
|
|
|
* tries to receive more than is possible. This is usually
|
|
|
|
* the case of a broken device.
|
|
|
|
*/
|
|
|
|
if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
|
2012-11-09 01:47:28 +08:00
|
|
|
net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
|
2011-09-28 12:40:54 +08:00
|
|
|
dev_kfree_skb(skb);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
while (len) {
|
|
|
|
set_skb_frag(skb, page, offset, &len);
|
|
|
|
page = (struct page *)page->private;
|
|
|
|
offset = 0;
|
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
if (page)
|
|
|
|
give_pages(vi, page);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
return skb;
|
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
static int receive_mergeable(struct virtnet_info *vi, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
|
|
|
|
struct page *page;
|
|
|
|
int num_buf, i, len;
|
|
|
|
|
|
|
|
num_buf = hdr->mhdr.num_buffers;
|
|
|
|
while (--num_buf) {
|
|
|
|
i = skb_shinfo(skb)->nr_frags;
|
|
|
|
if (i >= MAX_SKB_FRAGS) {
|
|
|
|
pr_debug("%s: packet too long\n", skb->dev->name);
|
|
|
|
skb->dev->stats.rx_length_errors++;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-04-12 21:19:04 +08:00
|
|
|
page = virtqueue_get_buf(vi->rvq, &len);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (!page) {
|
|
|
|
pr_debug("%s: rx error: %d buffers missing\n",
|
|
|
|
skb->dev->name, hdr->mhdr.num_buffers);
|
|
|
|
skb->dev->stats.rx_length_errors++;
|
|
|
|
return -EINVAL;
|
2008-11-17 14:41:34 +08:00
|
|
|
}
|
2011-06-15 14:36:29 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
if (len > PAGE_SIZE)
|
|
|
|
len = PAGE_SIZE;
|
|
|
|
|
|
|
|
set_skb_frag(skb, page, 0, &len);
|
|
|
|
|
|
|
|
--vi->num;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void receive_buf(struct net_device *dev, void *buf, unsigned int len)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2012-02-13 14:53:41 +08:00
|
|
|
struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
|
2010-01-29 11:20:04 +08:00
|
|
|
struct sk_buff *skb;
|
|
|
|
struct page *page;
|
|
|
|
struct skb_vnet_hdr *hdr;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
|
|
|
|
pr_debug("%s: short packet %i\n", dev->name, len);
|
|
|
|
dev->stats.rx_length_errors++;
|
|
|
|
if (vi->mergeable_rx_bufs || vi->big_packets)
|
|
|
|
give_pages(vi, buf);
|
|
|
|
else
|
|
|
|
dev_kfree_skb(buf);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
if (!vi->mergeable_rx_bufs && !vi->big_packets) {
|
|
|
|
skb = buf;
|
|
|
|
len -= sizeof(struct virtio_net_hdr);
|
|
|
|
skb_trim(skb, len);
|
|
|
|
} else {
|
|
|
|
page = buf;
|
|
|
|
skb = page_to_skb(vi, page, len);
|
|
|
|
if (unlikely(!skb)) {
|
2008-11-17 14:41:34 +08:00
|
|
|
dev->stats.rx_dropped++;
|
2010-01-29 11:20:04 +08:00
|
|
|
give_pages(vi, page);
|
|
|
|
return;
|
2008-11-17 14:41:34 +08:00
|
|
|
}
|
2010-01-29 11:20:04 +08:00
|
|
|
if (vi->mergeable_rx_bufs)
|
|
|
|
if (receive_mergeable(vi, skb)) {
|
|
|
|
dev_kfree_skb(skb);
|
|
|
|
return;
|
|
|
|
}
|
2008-04-18 11:24:27 +08:00
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
hdr = skb_vnet_hdr(skb);
|
2011-06-15 14:36:29 +08:00
|
|
|
|
2012-06-06 06:35:24 +08:00
|
|
|
u64_stats_update_begin(&stats->rx_syncp);
|
2011-06-15 14:36:29 +08:00
|
|
|
stats->rx_bytes += skb->len;
|
|
|
|
stats->rx_packets++;
|
2012-06-06 06:35:24 +08:00
|
|
|
u64_stats_update_end(&stats->rx_syncp);
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2009-09-24 23:59:19 +08:00
|
|
|
if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
|
2007-10-22 09:03:37 +08:00
|
|
|
pr_debug("Needs csum!\n");
|
2009-09-24 23:59:19 +08:00
|
|
|
if (!skb_partial_csum_set(skb,
|
|
|
|
hdr->hdr.csum_start,
|
|
|
|
hdr->hdr.csum_offset))
|
2007-10-22 09:03:37 +08:00
|
|
|
goto frame_err;
|
2011-06-10 08:56:17 +08:00
|
|
|
} else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
|
|
|
|
skb->ip_summed = CHECKSUM_UNNECESSARY;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2008-06-08 18:49:00 +08:00
|
|
|
skb->protocol = eth_type_trans(skb, dev);
|
|
|
|
pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
|
|
|
|
ntohs(skb->protocol), skb->len, skb->pkt_type);
|
|
|
|
|
2009-09-24 23:59:19 +08:00
|
|
|
if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
|
2007-10-22 09:03:37 +08:00
|
|
|
pr_debug("GSO!\n");
|
2009-09-24 23:59:19 +08:00
|
|
|
switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
|
2007-10-22 09:03:37 +08:00
|
|
|
case VIRTIO_NET_HDR_GSO_TCPV4:
|
|
|
|
skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
|
|
|
|
break;
|
|
|
|
case VIRTIO_NET_HDR_GSO_UDP:
|
|
|
|
skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
|
|
|
|
break;
|
|
|
|
case VIRTIO_NET_HDR_GSO_TCPV6:
|
|
|
|
skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
|
|
|
|
break;
|
|
|
|
default:
|
2012-11-09 01:47:28 +08:00
|
|
|
net_warn_ratelimited("%s: bad gso type %u.\n",
|
|
|
|
dev->name, hdr->hdr.gso_type);
|
2007-10-22 09:03:37 +08:00
|
|
|
goto frame_err;
|
|
|
|
}
|
|
|
|
|
2009-09-24 23:59:19 +08:00
|
|
|
if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
|
2008-02-05 12:50:02 +08:00
|
|
|
skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
|
|
|
|
|
2009-09-24 23:59:19 +08:00
|
|
|
skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
|
2007-10-22 09:03:37 +08:00
|
|
|
if (skb_shinfo(skb)->gso_size == 0) {
|
2012-11-09 01:47:28 +08:00
|
|
|
net_warn_ratelimited("%s: zero gso size.\n", dev->name);
|
2007-10-22 09:03:37 +08:00
|
|
|
goto frame_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Header must be checked, and gso_segs computed. */
|
|
|
|
skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
|
|
|
|
skb_shinfo(skb)->gso_segs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
netif_receive_skb(skb);
|
|
|
|
return;
|
|
|
|
|
|
|
|
frame_err:
|
|
|
|
dev->stats.rx_frame_errors++;
|
|
|
|
dev_kfree_skb(skb);
|
|
|
|
}
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
static int add_recvbuf_small(struct virtnet_info *vi, gfp_t gfp)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
2010-01-29 11:20:04 +08:00
|
|
|
struct skb_vnet_hdr *hdr;
|
|
|
|
int err;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2012-01-04 20:52:32 +08:00
|
|
|
skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (unlikely(!skb))
|
|
|
|
return -ENOMEM;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
skb_put(skb, MAX_PACKET_LEN);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
hdr = skb_vnet_hdr(skb);
|
2010-04-08 12:01:41 +08:00
|
|
|
sg_set_buf(vi->rx_sg, &hdr->hdr, sizeof hdr->hdr);
|
2008-04-18 11:24:27 +08:00
|
|
|
|
2010-04-08 12:01:41 +08:00
|
|
|
skb_to_sgvec(skb, vi->rx_sg + 1, 0, skb->len);
|
2008-04-18 11:24:27 +08:00
|
|
|
|
2012-01-12 13:14:42 +08:00
|
|
|
err = virtqueue_add_buf(vi->rvq, vi->rx_sg, 0, 2, skb, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (err < 0)
|
|
|
|
dev_kfree_skb(skb);
|
2008-04-18 11:24:27 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
2008-04-18 11:24:27 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
static int add_recvbuf_big(struct virtnet_info *vi, gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct page *first, *list = NULL;
|
|
|
|
char *p;
|
|
|
|
int i, err, offset;
|
|
|
|
|
2010-04-08 12:01:41 +08:00
|
|
|
/* page in vi->rx_sg[MAX_SKB_FRAGS + 1] is list tail */
|
2010-01-29 11:20:04 +08:00
|
|
|
for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
|
|
|
|
first = get_a_page(vi, gfp);
|
|
|
|
if (!first) {
|
|
|
|
if (list)
|
|
|
|
give_pages(vi, list);
|
|
|
|
return -ENOMEM;
|
2008-04-18 11:24:27 +08:00
|
|
|
}
|
2010-04-08 12:01:41 +08:00
|
|
|
sg_set_buf(&vi->rx_sg[i], page_address(first), PAGE_SIZE);
|
2008-04-18 11:24:27 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
/* chain new page in list head to match sg */
|
|
|
|
first->private = (unsigned long)list;
|
|
|
|
list = first;
|
|
|
|
}
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
first = get_a_page(vi, gfp);
|
|
|
|
if (!first) {
|
|
|
|
give_pages(vi, list);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
p = page_address(first);
|
|
|
|
|
2010-04-08 12:01:41 +08:00
|
|
|
/* vi->rx_sg[0], vi->rx_sg[1] share the same page */
|
|
|
|
/* a separated vi->rx_sg[0] for virtio_net_hdr only due to QEMU bug */
|
|
|
|
sg_set_buf(&vi->rx_sg[0], p, sizeof(struct virtio_net_hdr));
|
2010-01-29 11:20:04 +08:00
|
|
|
|
2010-04-08 12:01:41 +08:00
|
|
|
/* vi->rx_sg[1] for data packet, from offset */
|
2010-01-29 11:20:04 +08:00
|
|
|
offset = sizeof(struct padded_vnet_hdr);
|
2010-04-08 12:01:41 +08:00
|
|
|
sg_set_buf(&vi->rx_sg[1], p + offset, PAGE_SIZE - offset);
|
2010-01-29 11:20:04 +08:00
|
|
|
|
|
|
|
/* chain first in list head */
|
|
|
|
first->private = (unsigned long)list;
|
2012-01-12 13:14:42 +08:00
|
|
|
err = virtqueue_add_buf(vi->rvq, vi->rx_sg, 0, MAX_SKB_FRAGS + 2,
|
|
|
|
first, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (err < 0)
|
|
|
|
give_pages(vi, first);
|
|
|
|
|
|
|
|
return err;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
|
2008-11-17 14:41:34 +08:00
|
|
|
{
|
2010-01-29 11:20:04 +08:00
|
|
|
struct page *page;
|
2008-11-17 14:41:34 +08:00
|
|
|
int err;
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
page = get_a_page(vi, gfp);
|
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-04-08 12:01:41 +08:00
|
|
|
sg_init_one(vi->rx_sg, page_address(page), PAGE_SIZE);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2012-01-12 13:14:42 +08:00
|
|
|
err = virtqueue_add_buf(vi->rvq, vi->rx_sg, 0, 1, page, gfp);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (err < 0)
|
|
|
|
give_pages(vi, page);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2011-12-29 08:42:38 +08:00
|
|
|
/*
|
|
|
|
* Returns false if we couldn't fill entirely (OOM).
|
|
|
|
*
|
|
|
|
* Normally run in the receive path, but can also be run from ndo_open
|
|
|
|
* before we're receiving packets, or from refill_work which is
|
|
|
|
* careful to disable receiving (using napi_disable).
|
|
|
|
*/
|
2010-01-29 11:20:04 +08:00
|
|
|
static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
|
|
|
|
{
|
|
|
|
int err;
|
2010-07-03 00:32:55 +08:00
|
|
|
bool oom;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
do {
|
|
|
|
if (vi->mergeable_rx_bufs)
|
|
|
|
err = add_recvbuf_mergeable(vi, gfp);
|
|
|
|
else if (vi->big_packets)
|
|
|
|
err = add_recvbuf_big(vi, gfp);
|
|
|
|
else
|
|
|
|
err = add_recvbuf_small(vi, gfp);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-07-03 00:32:55 +08:00
|
|
|
oom = err == -ENOMEM;
|
|
|
|
if (err < 0)
|
2008-11-17 14:41:34 +08:00
|
|
|
break;
|
2010-01-29 11:20:04 +08:00
|
|
|
++vi->num;
|
2009-08-26 17:28:28 +08:00
|
|
|
} while (err > 0);
|
2008-11-17 14:41:34 +08:00
|
|
|
if (unlikely(vi->num > vi->max))
|
|
|
|
vi->max = vi->num;
|
2010-04-12 21:19:04 +08:00
|
|
|
virtqueue_kick(vi->rvq);
|
2009-08-27 03:22:32 +08:00
|
|
|
return !oom;
|
2008-11-17 14:41:34 +08:00
|
|
|
}
|
|
|
|
|
2008-02-05 12:49:57 +08:00
|
|
|
static void skb_recv_done(struct virtqueue *rvq)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = rvq->vdev->priv;
|
2008-02-05 12:49:57 +08:00
|
|
|
/* Schedule NAPI, Suppress further interrupts if successful. */
|
2009-01-20 08:43:59 +08:00
|
|
|
if (napi_schedule_prep(&vi->napi)) {
|
2010-04-12 21:19:04 +08:00
|
|
|
virtqueue_disable_cb(rvq);
|
2009-01-20 08:43:59 +08:00
|
|
|
__napi_schedule(&vi->napi);
|
2008-02-05 12:49:57 +08:00
|
|
|
}
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2011-02-11 03:03:31 +08:00
|
|
|
static void virtnet_napi_enable(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
napi_enable(&vi->napi);
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
if (napi_schedule_prep(&vi->napi)) {
|
|
|
|
virtqueue_disable_cb(vi->rvq);
|
2012-05-16 15:57:12 +08:00
|
|
|
local_bh_disable();
|
2011-02-11 03:03:31 +08:00
|
|
|
__napi_schedule(&vi->napi);
|
2012-05-16 15:57:12 +08:00
|
|
|
local_bh_enable();
|
2011-02-11 03:03:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-27 03:22:32 +08:00
|
|
|
static void refill_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi;
|
|
|
|
bool still_empty;
|
|
|
|
|
|
|
|
vi = container_of(work, struct virtnet_info, refill.work);
|
|
|
|
napi_disable(&vi->napi);
|
2010-01-26 07:51:01 +08:00
|
|
|
still_empty = !try_fill_recv(vi, GFP_KERNEL);
|
2011-02-11 03:03:31 +08:00
|
|
|
virtnet_napi_enable(vi);
|
2009-08-27 03:22:32 +08:00
|
|
|
|
|
|
|
/* In theory, this can happen: if we don't get any buffers in
|
|
|
|
* we will *never* try to fill again. */
|
|
|
|
if (still_empty)
|
2012-08-21 05:51:24 +08:00
|
|
|
schedule_delayed_work(&vi->refill, HZ/2);
|
2009-08-27 03:22:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
static int virtnet_poll(struct napi_struct *napi, int budget)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
|
2010-01-29 11:20:04 +08:00
|
|
|
void *buf;
|
2007-10-22 09:03:37 +08:00
|
|
|
unsigned int len, received = 0;
|
|
|
|
|
|
|
|
again:
|
|
|
|
while (received < budget &&
|
2010-04-12 21:19:04 +08:00
|
|
|
(buf = virtqueue_get_buf(vi->rvq, &len)) != NULL) {
|
2010-01-29 11:20:04 +08:00
|
|
|
receive_buf(vi->dev, buf, len);
|
|
|
|
--vi->num;
|
2007-10-22 09:03:37 +08:00
|
|
|
received++;
|
|
|
|
}
|
|
|
|
|
2009-08-27 03:22:32 +08:00
|
|
|
if (vi->num < vi->max / 2) {
|
|
|
|
if (!try_fill_recv(vi, GFP_ATOMIC))
|
2012-08-21 05:51:24 +08:00
|
|
|
schedule_delayed_work(&vi->refill, 0);
|
2009-08-27 03:22:32 +08:00
|
|
|
}
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2007-11-20 00:20:43 +08:00
|
|
|
/* Out of packets? */
|
|
|
|
if (received < budget) {
|
2009-01-20 08:43:59 +08:00
|
|
|
napi_complete(napi);
|
2010-04-12 21:19:04 +08:00
|
|
|
if (unlikely(!virtqueue_enable_cb(vi->rvq)) &&
|
2009-12-03 15:58:21 +08:00
|
|
|
napi_schedule_prep(napi)) {
|
2010-04-12 21:19:04 +08:00
|
|
|
virtqueue_disable_cb(vi->rvq);
|
2009-01-20 08:43:59 +08:00
|
|
|
__napi_schedule(napi);
|
2007-10-22 09:03:37 +08:00
|
|
|
goto again;
|
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 21:17:05 +08:00
|
|
|
}
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return received;
|
|
|
|
}
|
|
|
|
|
2009-09-24 23:59:20 +08:00
|
|
|
static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
2009-09-24 23:59:20 +08:00
|
|
|
unsigned int len, tot_sgs = 0;
|
2012-02-13 14:53:41 +08:00
|
|
|
struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2010-04-12 21:19:04 +08:00
|
|
|
while ((skb = virtqueue_get_buf(vi->svq, &len)) != NULL) {
|
2007-10-22 09:03:37 +08:00
|
|
|
pr_debug("Sent skb %p\n", skb);
|
2011-06-15 14:36:29 +08:00
|
|
|
|
2012-06-06 06:35:24 +08:00
|
|
|
u64_stats_update_begin(&stats->tx_syncp);
|
2011-06-15 14:36:29 +08:00
|
|
|
stats->tx_bytes += skb->len;
|
|
|
|
stats->tx_packets++;
|
2012-06-06 06:35:24 +08:00
|
|
|
u64_stats_update_end(&stats->tx_syncp);
|
2011-06-15 14:36:29 +08:00
|
|
|
|
2009-09-24 23:59:20 +08:00
|
|
|
tot_sgs += skb_vnet_hdr(skb)->num_sg;
|
2009-10-14 22:36:43 +08:00
|
|
|
dev_kfree_skb_any(skb);
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
2009-09-24 23:59:20 +08:00
|
|
|
return tot_sgs;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2008-05-03 10:50:46 +08:00
|
|
|
static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2009-09-24 23:59:19 +08:00
|
|
|
struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
|
2007-10-22 09:03:37 +08:00
|
|
|
const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
|
|
|
|
|
2008-10-28 06:59:26 +08:00
|
|
|
pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
|
2007-10-22 09:03:37 +08:00
|
|
|
|
|
|
|
if (skb->ip_summed == CHECKSUM_PARTIAL) {
|
2009-09-24 23:59:19 +08:00
|
|
|
hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
|
2010-12-14 23:24:08 +08:00
|
|
|
hdr->hdr.csum_start = skb_checksum_start_offset(skb);
|
2009-09-24 23:59:19 +08:00
|
|
|
hdr->hdr.csum_offset = skb->csum_offset;
|
2007-10-22 09:03:37 +08:00
|
|
|
} else {
|
2009-09-24 23:59:19 +08:00
|
|
|
hdr->hdr.flags = 0;
|
|
|
|
hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (skb_is_gso(skb)) {
|
2009-09-24 23:59:19 +08:00
|
|
|
hdr->hdr.hdr_len = skb_headlen(skb);
|
|
|
|
hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
|
2008-02-05 12:50:02 +08:00
|
|
|
if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
|
2009-09-24 23:59:19 +08:00
|
|
|
hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
|
2007-10-22 09:03:37 +08:00
|
|
|
else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
|
2009-09-24 23:59:19 +08:00
|
|
|
hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
|
2007-10-22 09:03:37 +08:00
|
|
|
else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
|
2009-09-24 23:59:19 +08:00
|
|
|
hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
|
2007-10-22 09:03:37 +08:00
|
|
|
else
|
|
|
|
BUG();
|
2008-02-05 12:50:02 +08:00
|
|
|
if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
|
2009-09-24 23:59:19 +08:00
|
|
|
hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
|
2007-10-22 09:03:37 +08:00
|
|
|
} else {
|
2009-09-24 23:59:19 +08:00
|
|
|
hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
|
|
|
|
hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2009-09-24 23:59:19 +08:00
|
|
|
hdr->mhdr.num_buffers = 0;
|
2008-11-17 14:41:34 +08:00
|
|
|
|
|
|
|
/* Encode metadata header at front. */
|
|
|
|
if (vi->mergeable_rx_bufs)
|
2010-04-08 12:01:41 +08:00
|
|
|
sg_set_buf(vi->tx_sg, &hdr->mhdr, sizeof hdr->mhdr);
|
2008-11-17 14:41:34 +08:00
|
|
|
else
|
2010-04-08 12:01:41 +08:00
|
|
|
sg_set_buf(vi->tx_sg, &hdr->hdr, sizeof hdr->hdr);
|
2008-11-17 14:41:34 +08:00
|
|
|
|
2010-04-08 12:01:41 +08:00
|
|
|
hdr->num_sg = skb_to_sgvec(skb, vi->tx_sg + 1, 0, skb->len) + 1;
|
2010-05-22 08:22:52 +08:00
|
|
|
return virtqueue_add_buf(vi->svq, vi->tx_sg, hdr->num_sg,
|
2012-01-12 13:14:42 +08:00
|
|
|
0, skb, GFP_ATOMIC);
|
2008-05-26 15:48:13 +08:00
|
|
|
}
|
|
|
|
|
2009-09-01 03:50:51 +08:00
|
|
|
static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
|
2008-05-03 10:50:46 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2009-09-24 23:59:20 +08:00
|
|
|
int capacity;
|
2008-02-05 12:50:07 +08:00
|
|
|
|
|
|
|
/* Free up any pending old buffers before queueing new ones. */
|
|
|
|
free_old_xmit_skbs(vi);
|
2008-05-03 10:50:46 +08:00
|
|
|
|
2009-10-28 19:03:38 +08:00
|
|
|
/* Try to transmit */
|
2009-09-24 23:59:20 +08:00
|
|
|
capacity = xmit_skb(vi, skb);
|
|
|
|
|
|
|
|
/* This can happen with OOM and indirect buffers. */
|
|
|
|
if (unlikely(capacity < 0)) {
|
2012-03-27 15:28:09 +08:00
|
|
|
if (likely(capacity == -ENOMEM)) {
|
2012-04-09 13:14:15 +08:00
|
|
|
if (net_ratelimit())
|
2010-07-03 00:34:01 +08:00
|
|
|
dev_warn(&dev->dev,
|
|
|
|
"TX queue failure: out of memory\n");
|
2012-04-09 13:14:15 +08:00
|
|
|
} else {
|
2012-03-27 15:28:09 +08:00
|
|
|
dev->stats.tx_fifo_errors++;
|
|
|
|
if (net_ratelimit())
|
2010-07-03 00:34:01 +08:00
|
|
|
dev_warn(&dev->dev,
|
|
|
|
"Unexpected TX queue failure: %d\n",
|
|
|
|
capacity);
|
2009-09-24 23:59:20 +08:00
|
|
|
}
|
2010-07-03 00:34:01 +08:00
|
|
|
dev->stats.tx_dropped++;
|
|
|
|
kfree_skb(skb);
|
|
|
|
return NETDEV_TX_OK;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
2010-04-12 21:19:04 +08:00
|
|
|
virtqueue_kick(vi->svq);
|
2009-10-28 19:03:38 +08:00
|
|
|
|
2009-09-24 23:59:20 +08:00
|
|
|
/* Don't wait up for transmitted skbs to be freed. */
|
|
|
|
skb_orphan(skb);
|
|
|
|
nf_reset(skb);
|
|
|
|
|
|
|
|
/* Apparently nice girls don't return TX_BUSY; stop the queue
|
|
|
|
* before it gets out of hand. Naturally, this wastes entries. */
|
|
|
|
if (capacity < 2+MAX_SKB_FRAGS) {
|
|
|
|
netif_stop_queue(dev);
|
2011-05-20 07:11:23 +08:00
|
|
|
if (unlikely(!virtqueue_enable_cb_delayed(vi->svq))) {
|
2009-09-24 23:59:20 +08:00
|
|
|
/* More just got used, free them then recheck. */
|
|
|
|
capacity += free_old_xmit_skbs(vi);
|
|
|
|
if (capacity >= 2+MAX_SKB_FRAGS) {
|
|
|
|
netif_start_queue(dev);
|
2010-04-12 21:19:04 +08:00
|
|
|
virtqueue_disable_cb(vi->svq);
|
2009-09-24 23:59:20 +08:00
|
|
|
}
|
|
|
|
}
|
2008-05-03 10:50:46 +08:00
|
|
|
}
|
2009-09-24 23:59:20 +08:00
|
|
|
|
|
|
|
return NETDEV_TX_OK;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2009-02-05 08:36:34 +08:00
|
|
|
static int virtnet_set_mac_address(struct net_device *dev, void *p)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct virtio_device *vdev = vi->vdev;
|
2012-06-29 13:10:06 +08:00
|
|
|
int ret;
|
2009-02-05 08:36:34 +08:00
|
|
|
|
2012-06-29 13:10:06 +08:00
|
|
|
ret = eth_mac_addr(dev, p);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2009-02-05 08:36:34 +08:00
|
|
|
|
2009-04-05 07:40:19 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
|
|
|
|
vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
|
|
|
|
dev->dev_addr, dev->addr_len);
|
2009-02-05 08:36:34 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-15 14:36:29 +08:00
|
|
|
static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
|
|
|
|
struct rtnl_link_stats64 *tot)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
int cpu;
|
|
|
|
unsigned int start;
|
|
|
|
|
|
|
|
for_each_possible_cpu(cpu) {
|
2012-02-13 14:53:41 +08:00
|
|
|
struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
|
2011-06-15 14:36:29 +08:00
|
|
|
u64 tpackets, tbytes, rpackets, rbytes;
|
|
|
|
|
|
|
|
do {
|
2012-07-21 14:30:50 +08:00
|
|
|
start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
|
2011-06-15 14:36:29 +08:00
|
|
|
tpackets = stats->tx_packets;
|
|
|
|
tbytes = stats->tx_bytes;
|
2012-07-21 14:30:50 +08:00
|
|
|
} while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
|
2012-06-06 06:35:24 +08:00
|
|
|
|
|
|
|
do {
|
2012-07-21 14:30:50 +08:00
|
|
|
start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
|
2011-06-15 14:36:29 +08:00
|
|
|
rpackets = stats->rx_packets;
|
|
|
|
rbytes = stats->rx_bytes;
|
2012-07-21 14:30:50 +08:00
|
|
|
} while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
|
2011-06-15 14:36:29 +08:00
|
|
|
|
|
|
|
tot->rx_packets += rpackets;
|
|
|
|
tot->tx_packets += tpackets;
|
|
|
|
tot->rx_bytes += rbytes;
|
|
|
|
tot->tx_bytes += tbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
tot->tx_dropped = dev->stats.tx_dropped;
|
2011-11-21 17:28:17 +08:00
|
|
|
tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
|
2011-06-15 14:36:29 +08:00
|
|
|
tot->rx_dropped = dev->stats.rx_dropped;
|
|
|
|
tot->rx_length_errors = dev->stats.rx_length_errors;
|
|
|
|
tot->rx_frame_errors = dev->stats.rx_frame_errors;
|
|
|
|
|
|
|
|
return tot;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:54:50 +08:00
|
|
|
#ifdef CONFIG_NET_POLL_CONTROLLER
|
|
|
|
static void virtnet_netpoll(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
|
|
|
napi_schedule(&vi->napi);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
static int virtnet_open(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
2011-12-29 08:42:38 +08:00
|
|
|
/* Make sure we have some buffers: if oom use wq. */
|
|
|
|
if (!try_fill_recv(vi, GFP_KERNEL))
|
2012-08-21 05:51:24 +08:00
|
|
|
schedule_delayed_work(&vi->refill, 0);
|
2011-12-29 08:42:38 +08:00
|
|
|
|
2011-02-11 03:03:31 +08:00
|
|
|
virtnet_napi_enable(vi);
|
2007-10-22 09:03:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-04 17:02:34 +08:00
|
|
|
/*
|
|
|
|
* Send command via the control virtqueue and check status. Commands
|
|
|
|
* supported by the hypervisor, as indicated by feature bits, should
|
|
|
|
* never fail unless improperly formated.
|
|
|
|
*/
|
|
|
|
static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
|
|
|
|
struct scatterlist *data, int out, int in)
|
|
|
|
{
|
2009-05-02 01:27:56 +08:00
|
|
|
struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
|
2009-02-04 17:02:34 +08:00
|
|
|
struct virtio_net_ctrl_hdr ctrl;
|
|
|
|
virtio_net_ctrl_ack status = ~0;
|
|
|
|
unsigned int tmp;
|
2009-05-02 01:27:56 +08:00
|
|
|
int i;
|
2009-02-04 17:02:34 +08:00
|
|
|
|
2009-04-11 22:50:23 +08:00
|
|
|
/* Caller should know better */
|
|
|
|
BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
|
|
|
|
(out + in > VIRTNET_SEND_COMMAND_SG_MAX));
|
2009-02-04 17:02:34 +08:00
|
|
|
|
|
|
|
out++; /* Add header */
|
|
|
|
in++; /* Add return status */
|
|
|
|
|
|
|
|
ctrl.class = class;
|
|
|
|
ctrl.cmd = cmd;
|
|
|
|
|
|
|
|
sg_init_table(sg, out + in);
|
|
|
|
|
|
|
|
sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
|
2009-05-02 01:27:56 +08:00
|
|
|
for_each_sg(data, s, out + in - 2, i)
|
|
|
|
sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
|
2009-02-04 17:02:34 +08:00
|
|
|
sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
|
|
|
|
|
2012-01-12 13:14:42 +08:00
|
|
|
BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi, GFP_ATOMIC) < 0);
|
2009-02-04 17:02:34 +08:00
|
|
|
|
2010-04-12 21:19:04 +08:00
|
|
|
virtqueue_kick(vi->cvq);
|
2009-02-04 17:02:34 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Spin for a response, the kick causes an ioport write, trapping
|
|
|
|
* into the hypervisor, so the request should be handled immediately.
|
|
|
|
*/
|
2010-04-12 21:19:04 +08:00
|
|
|
while (!virtqueue_get_buf(vi->cvq, &tmp))
|
2009-02-04 17:02:34 +08:00
|
|
|
cpu_relax();
|
|
|
|
|
|
|
|
return status == VIRTIO_NET_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
static void virtnet_ack_link_announce(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
rtnl_lock();
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
|
|
|
|
VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL,
|
|
|
|
0, 0))
|
|
|
|
dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
|
|
|
|
rtnl_unlock();
|
|
|
|
}
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
static int virtnet_close(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
2011-12-29 08:42:38 +08:00
|
|
|
/* Make sure refill_work doesn't re-enable napi! */
|
|
|
|
cancel_delayed_work_sync(&vi->refill);
|
2007-10-22 09:03:37 +08:00
|
|
|
napi_disable(&vi->napi);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-04 17:02:40 +08:00
|
|
|
static void virtnet_set_rx_mode(struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
2009-02-04 17:02:45 +08:00
|
|
|
struct scatterlist sg[2];
|
2009-02-04 17:02:40 +08:00
|
|
|
u8 promisc, allmulti;
|
2009-02-04 17:02:45 +08:00
|
|
|
struct virtio_net_ctrl_mac *mac_data;
|
2009-05-23 07:22:17 +08:00
|
|
|
struct netdev_hw_addr *ha;
|
2010-01-26 05:36:10 +08:00
|
|
|
int uc_count;
|
2010-02-08 12:30:35 +08:00
|
|
|
int mc_count;
|
2009-02-04 17:02:45 +08:00
|
|
|
void *buf;
|
|
|
|
int i;
|
2009-02-04 17:02:40 +08:00
|
|
|
|
|
|
|
/* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
|
|
|
|
if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
|
|
|
|
return;
|
|
|
|
|
2009-02-04 17:02:45 +08:00
|
|
|
promisc = ((dev->flags & IFF_PROMISC) != 0);
|
|
|
|
allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
|
2009-02-04 17:02:40 +08:00
|
|
|
|
2009-05-02 01:27:56 +08:00
|
|
|
sg_init_one(sg, &promisc, sizeof(promisc));
|
2009-02-04 17:02:40 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
|
|
|
|
VIRTIO_NET_CTRL_RX_PROMISC,
|
2009-02-04 17:02:45 +08:00
|
|
|
sg, 1, 0))
|
2009-02-04 17:02:40 +08:00
|
|
|
dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
|
|
|
|
promisc ? "en" : "dis");
|
|
|
|
|
2009-05-02 01:27:56 +08:00
|
|
|
sg_init_one(sg, &allmulti, sizeof(allmulti));
|
2009-02-04 17:02:40 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
|
|
|
|
VIRTIO_NET_CTRL_RX_ALLMULTI,
|
2009-02-04 17:02:45 +08:00
|
|
|
sg, 1, 0))
|
2009-02-04 17:02:40 +08:00
|
|
|
dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
|
|
|
|
allmulti ? "en" : "dis");
|
2009-02-04 17:02:45 +08:00
|
|
|
|
2010-01-26 05:36:10 +08:00
|
|
|
uc_count = netdev_uc_count(dev);
|
2010-02-08 12:30:35 +08:00
|
|
|
mc_count = netdev_mc_count(dev);
|
2009-02-04 17:02:45 +08:00
|
|
|
/* MAC filter - use one buffer for both lists */
|
2010-02-08 12:30:35 +08:00
|
|
|
buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
|
|
|
|
(2 * sizeof(mac_data->entries)), GFP_ATOMIC);
|
|
|
|
mac_data = buf;
|
2009-02-04 17:02:45 +08:00
|
|
|
if (!buf) {
|
|
|
|
dev_warn(&dev->dev, "No memory for MAC address buffer\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-02 01:27:56 +08:00
|
|
|
sg_init_table(sg, 2);
|
|
|
|
|
2009-02-04 17:02:45 +08:00
|
|
|
/* Store the unicast list and count in the front of the buffer */
|
2010-01-26 05:36:10 +08:00
|
|
|
mac_data->entries = uc_count;
|
2009-05-23 07:22:17 +08:00
|
|
|
i = 0;
|
2010-01-26 05:36:10 +08:00
|
|
|
netdev_for_each_uc_addr(ha, dev)
|
2009-05-23 07:22:17 +08:00
|
|
|
memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
|
2009-02-04 17:02:45 +08:00
|
|
|
|
|
|
|
sg_set_buf(&sg[0], mac_data,
|
2010-01-26 05:36:10 +08:00
|
|
|
sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
|
2009-02-04 17:02:45 +08:00
|
|
|
|
|
|
|
/* multicast list and count fill the end */
|
2010-01-26 05:36:10 +08:00
|
|
|
mac_data = (void *)&mac_data->macs[uc_count][0];
|
2009-02-04 17:02:45 +08:00
|
|
|
|
2010-02-08 12:30:35 +08:00
|
|
|
mac_data->entries = mc_count;
|
2010-02-24 07:17:07 +08:00
|
|
|
i = 0;
|
2010-04-02 05:22:57 +08:00
|
|
|
netdev_for_each_mc_addr(ha, dev)
|
|
|
|
memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
|
2009-02-04 17:02:45 +08:00
|
|
|
|
|
|
|
sg_set_buf(&sg[1], mac_data,
|
2010-02-08 12:30:35 +08:00
|
|
|
sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
|
2009-02-04 17:02:45 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
|
|
|
|
VIRTIO_NET_CTRL_MAC_TABLE_SET,
|
|
|
|
sg, 2, 0))
|
|
|
|
dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
|
|
|
|
|
|
|
|
kfree(buf);
|
2009-02-04 17:02:40 +08:00
|
|
|
}
|
|
|
|
|
2011-12-09 08:52:37 +08:00
|
|
|
static int virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
|
2009-02-04 17:02:50 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct scatterlist sg;
|
|
|
|
|
2009-05-02 01:27:56 +08:00
|
|
|
sg_init_one(&sg, &vid, sizeof(vid));
|
2009-02-04 17:02:50 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
|
|
|
|
VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
|
|
|
|
dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
|
2011-12-09 08:52:37 +08:00
|
|
|
return 0;
|
2009-02-04 17:02:50 +08:00
|
|
|
}
|
|
|
|
|
2011-12-09 08:52:37 +08:00
|
|
|
static int virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
|
2009-02-04 17:02:50 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct scatterlist sg;
|
|
|
|
|
2009-05-02 01:27:56 +08:00
|
|
|
sg_init_one(&sg, &vid, sizeof(vid));
|
2009-02-04 17:02:50 +08:00
|
|
|
|
|
|
|
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
|
|
|
|
VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
|
|
|
|
dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
|
2011-12-09 08:52:37 +08:00
|
|
|
return 0;
|
2009-02-04 17:02:50 +08:00
|
|
|
}
|
|
|
|
|
2011-10-19 16:10:59 +08:00
|
|
|
static void virtnet_get_ringparam(struct net_device *dev,
|
|
|
|
struct ethtool_ringparam *ring)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
|
|
|
|
ring->rx_max_pending = virtqueue_get_vring_size(vi->rvq);
|
|
|
|
ring->tx_max_pending = virtqueue_get_vring_size(vi->svq);
|
|
|
|
ring->rx_pending = ring->rx_max_pending;
|
|
|
|
ring->tx_pending = ring->tx_max_pending;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-14 22:17:08 +08:00
|
|
|
|
|
|
|
static void virtnet_get_drvinfo(struct net_device *dev,
|
|
|
|
struct ethtool_drvinfo *info)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = netdev_priv(dev);
|
|
|
|
struct virtio_device *vdev = vi->vdev;
|
|
|
|
|
|
|
|
strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
|
|
|
|
strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
|
|
|
|
strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-09-02 16:03:33 +08:00
|
|
|
static const struct ethtool_ops virtnet_ethtool_ops = {
|
2011-11-14 22:17:08 +08:00
|
|
|
.get_drvinfo = virtnet_get_drvinfo,
|
2009-01-20 09:09:49 +08:00
|
|
|
.get_link = ethtool_op_get_link,
|
2011-10-19 16:10:59 +08:00
|
|
|
.get_ringparam = virtnet_get_ringparam,
|
2008-04-18 11:21:42 +08:00
|
|
|
};
|
|
|
|
|
2008-11-26 21:58:11 +08:00
|
|
|
#define MIN_MTU 68
|
|
|
|
#define MAX_MTU 65535
|
|
|
|
|
|
|
|
static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
|
|
|
|
{
|
|
|
|
if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
|
|
|
|
return -EINVAL;
|
|
|
|
dev->mtu = new_mtu;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-07 02:44:22 +08:00
|
|
|
static const struct net_device_ops virtnet_netdev = {
|
|
|
|
.ndo_open = virtnet_open,
|
|
|
|
.ndo_stop = virtnet_close,
|
|
|
|
.ndo_start_xmit = start_xmit,
|
|
|
|
.ndo_validate_addr = eth_validate_addr,
|
2009-02-05 08:36:34 +08:00
|
|
|
.ndo_set_mac_address = virtnet_set_mac_address,
|
2009-02-04 17:02:40 +08:00
|
|
|
.ndo_set_rx_mode = virtnet_set_rx_mode,
|
2009-01-07 02:44:22 +08:00
|
|
|
.ndo_change_mtu = virtnet_change_mtu,
|
2011-06-15 14:36:29 +08:00
|
|
|
.ndo_get_stats64 = virtnet_stats,
|
2009-05-02 01:31:10 +08:00
|
|
|
.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
|
|
|
|
.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
|
2009-01-07 02:44:22 +08:00
|
|
|
#ifdef CONFIG_NET_POLL_CONTROLLER
|
|
|
|
.ndo_poll_controller = virtnet_netpoll,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
static void virtnet_config_changed_work(struct work_struct *work)
|
2009-01-20 09:09:49 +08:00
|
|
|
{
|
2012-04-12 04:43:52 +08:00
|
|
|
struct virtnet_info *vi =
|
|
|
|
container_of(work, struct virtnet_info, config_work);
|
2009-01-20 09:09:49 +08:00
|
|
|
u16 v;
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
mutex_lock(&vi->config_lock);
|
|
|
|
if (!vi->config_enable)
|
|
|
|
goto done;
|
|
|
|
|
2011-08-14 22:52:33 +08:00
|
|
|
if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
|
2009-01-20 09:09:49 +08:00
|
|
|
offsetof(struct virtio_net_config, status),
|
2011-08-14 22:52:33 +08:00
|
|
|
&v) < 0)
|
2012-04-12 04:43:52 +08:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (v & VIRTIO_NET_S_ANNOUNCE) {
|
2012-08-10 06:14:56 +08:00
|
|
|
netdev_notify_peers(vi->dev);
|
2012-04-12 04:43:52 +08:00
|
|
|
virtnet_ack_link_announce(vi);
|
|
|
|
}
|
2009-01-20 09:09:49 +08:00
|
|
|
|
|
|
|
/* Ignore unknown (future) status bits */
|
|
|
|
v &= VIRTIO_NET_S_LINK_UP;
|
|
|
|
|
|
|
|
if (vi->status == v)
|
2012-04-12 04:43:52 +08:00
|
|
|
goto done;
|
2009-01-20 09:09:49 +08:00
|
|
|
|
|
|
|
vi->status = v;
|
|
|
|
|
|
|
|
if (vi->status & VIRTIO_NET_S_LINK_UP) {
|
|
|
|
netif_carrier_on(vi->dev);
|
|
|
|
netif_wake_queue(vi->dev);
|
|
|
|
} else {
|
|
|
|
netif_carrier_off(vi->dev);
|
|
|
|
netif_stop_queue(vi->dev);
|
|
|
|
}
|
2012-04-12 04:43:52 +08:00
|
|
|
done:
|
|
|
|
mutex_unlock(&vi->config_lock);
|
2009-01-20 09:09:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void virtnet_config_changed(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
|
2012-08-21 05:51:24 +08:00
|
|
|
schedule_work(&vi->config_work);
|
2009-01-20 09:09:49 +08:00
|
|
|
}
|
|
|
|
|
2011-12-22 19:28:31 +08:00
|
|
|
static int init_vqs(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
struct virtqueue *vqs[3];
|
|
|
|
vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
|
|
|
|
const char *names[] = { "input", "output", "control" };
|
|
|
|
int nvqs, err;
|
|
|
|
|
|
|
|
/* We expect two virtqueues, receive then send,
|
|
|
|
* and optionally control. */
|
|
|
|
nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
|
|
|
|
|
|
|
|
err = vi->vdev->config->find_vqs(vi->vdev, nvqs, vqs, callbacks, names);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
vi->rvq = vqs[0];
|
|
|
|
vi->svq = vqs[1];
|
|
|
|
|
|
|
|
if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
|
|
|
|
vi->cvq = vqs[2];
|
|
|
|
|
|
|
|
if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
|
|
|
|
vi->dev->features |= NETIF_F_HW_VLAN_FILTER;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
static int virtnet_probe(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct net_device *dev;
|
|
|
|
struct virtnet_info *vi;
|
|
|
|
|
|
|
|
/* Allocate ourselves a network device with room for our info */
|
|
|
|
dev = alloc_etherdev(sizeof(struct virtnet_info));
|
|
|
|
if (!dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Set up network device as normal. */
|
2012-06-29 13:10:06 +08:00
|
|
|
dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
|
2009-01-07 02:44:22 +08:00
|
|
|
dev->netdev_ops = &virtnet_netdev;
|
2007-10-22 09:03:37 +08:00
|
|
|
dev->features = NETIF_F_HIGHDMA;
|
2011-06-15 14:36:29 +08:00
|
|
|
|
2008-04-18 11:21:42 +08:00
|
|
|
SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
|
2007-10-22 09:03:37 +08:00
|
|
|
SET_NETDEV_DEV(dev, &vdev->dev);
|
|
|
|
|
|
|
|
/* Do we support "hardware" checksums? */
|
2011-03-31 09:01:35 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
|
2007-10-22 09:03:37 +08:00
|
|
|
/* This opens up the world of extra features. */
|
2011-03-31 09:01:35 +08:00
|
|
|
dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
|
|
|
|
if (csum)
|
|
|
|
dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
|
|
|
|
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
|
|
|
|
dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
|
2008-02-05 12:50:02 +08:00
|
|
|
| NETIF_F_TSO_ECN | NETIF_F_TSO6;
|
|
|
|
}
|
2008-05-03 10:50:46 +08:00
|
|
|
/* Individual feature bits: what can host handle? */
|
2011-03-31 09:01:35 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
|
|
|
|
dev->hw_features |= NETIF_F_TSO;
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
|
|
|
|
dev->hw_features |= NETIF_F_TSO6;
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
|
|
|
|
dev->hw_features |= NETIF_F_TSO_ECN;
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
|
|
|
|
dev->hw_features |= NETIF_F_UFO;
|
|
|
|
|
|
|
|
if (gso)
|
|
|
|
dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
|
|
|
|
/* (!csum && gso) case will be fixed by register_netdev() */
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Configuration may specify what MAC to use. Otherwise random. */
|
2011-08-14 22:52:33 +08:00
|
|
|
if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
|
2008-02-05 12:49:56 +08:00
|
|
|
offsetof(struct virtio_net_config, mac),
|
2011-08-14 22:52:33 +08:00
|
|
|
dev->dev_addr, dev->addr_len) < 0)
|
2012-02-15 14:45:39 +08:00
|
|
|
eth_hw_addr_random(dev);
|
2007-10-22 09:03:37 +08:00
|
|
|
|
|
|
|
/* Set up our device-specific information */
|
|
|
|
vi = netdev_priv(dev);
|
2007-12-16 21:19:43 +08:00
|
|
|
netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
|
2007-10-22 09:03:37 +08:00
|
|
|
vi->dev = dev;
|
|
|
|
vi->vdev = vdev;
|
2008-02-18 17:02:51 +08:00
|
|
|
vdev->priv = vi;
|
2008-07-26 01:06:01 +08:00
|
|
|
vi->pages = NULL;
|
2011-06-15 14:36:29 +08:00
|
|
|
vi->stats = alloc_percpu(struct virtnet_stats);
|
|
|
|
err = -ENOMEM;
|
|
|
|
if (vi->stats == NULL)
|
|
|
|
goto free;
|
|
|
|
|
2009-08-27 03:22:32 +08:00
|
|
|
INIT_DELAYED_WORK(&vi->refill, refill_work);
|
2012-04-12 04:43:52 +08:00
|
|
|
mutex_init(&vi->config_lock);
|
|
|
|
vi->config_enable = true;
|
|
|
|
INIT_WORK(&vi->config_work, virtnet_config_changed_work);
|
2010-04-08 12:01:41 +08:00
|
|
|
sg_init_table(vi->rx_sg, ARRAY_SIZE(vi->rx_sg));
|
|
|
|
sg_init_table(vi->tx_sg, ARRAY_SIZE(vi->tx_sg));
|
2007-10-22 09:03:37 +08:00
|
|
|
|
2008-04-18 11:24:27 +08:00
|
|
|
/* If we can receive ANY GSO packets, we must allocate large ones. */
|
2009-12-03 15:58:21 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
|
|
|
|
virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
|
2008-04-18 11:24:27 +08:00
|
|
|
vi->big_packets = true;
|
|
|
|
|
2008-11-17 14:41:34 +08:00
|
|
|
if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
|
|
|
|
vi->mergeable_rx_bufs = true;
|
|
|
|
|
2011-12-22 19:28:31 +08:00
|
|
|
err = init_vqs(vi);
|
2009-06-13 12:16:36 +08:00
|
|
|
if (err)
|
2011-06-15 14:36:29 +08:00
|
|
|
goto free_stats;
|
2007-10-22 09:03:37 +08:00
|
|
|
|
|
|
|
err = register_netdev(dev);
|
|
|
|
if (err) {
|
|
|
|
pr_debug("virtio_net: registering device failed\n");
|
2009-06-13 12:16:36 +08:00
|
|
|
goto free_vqs;
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
2008-02-05 12:50:02 +08:00
|
|
|
|
|
|
|
/* Last of all, set up some receive buffers. */
|
2009-08-27 03:22:32 +08:00
|
|
|
try_fill_recv(vi, GFP_KERNEL);
|
2008-02-05 12:50:02 +08:00
|
|
|
|
|
|
|
/* If we didn't even get one input buffer, we're useless. */
|
|
|
|
if (vi->num == 0) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto unregister;
|
|
|
|
}
|
|
|
|
|
2010-11-10 22:45:41 +08: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-21 05:51:24 +08:00
|
|
|
schedule_work(&vi->config_work);
|
2010-11-10 22:45:41 +08:00
|
|
|
} else {
|
|
|
|
vi->status = VIRTIO_NET_S_LINK_UP;
|
|
|
|
netif_carrier_on(dev);
|
|
|
|
}
|
2009-01-20 09:09:49 +08:00
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
pr_debug("virtnet: registered device %s\n", dev->name);
|
|
|
|
return 0;
|
|
|
|
|
2008-02-05 12:50:02 +08:00
|
|
|
unregister:
|
|
|
|
unregister_netdev(dev);
|
2009-06-13 12:16:36 +08:00
|
|
|
free_vqs:
|
|
|
|
vdev->config->del_vqs(vdev);
|
2011-06-15 14:36:29 +08:00
|
|
|
free_stats:
|
|
|
|
free_percpu(vi->stats);
|
2007-10-22 09:03:37 +08:00
|
|
|
free:
|
|
|
|
free_netdev(dev);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-01-29 11:20:04 +08:00
|
|
|
static void free_unused_bufs(struct virtnet_info *vi)
|
|
|
|
{
|
|
|
|
void *buf;
|
2010-02-08 22:14:42 +08:00
|
|
|
while (1) {
|
2010-04-12 21:19:04 +08:00
|
|
|
buf = virtqueue_detach_unused_buf(vi->svq);
|
2010-02-08 22:14:42 +08:00
|
|
|
if (!buf)
|
|
|
|
break;
|
|
|
|
dev_kfree_skb(buf);
|
|
|
|
}
|
2010-01-29 11:20:04 +08:00
|
|
|
while (1) {
|
2010-04-12 21:19:04 +08:00
|
|
|
buf = virtqueue_detach_unused_buf(vi->rvq);
|
2010-01-29 11:20:04 +08:00
|
|
|
if (!buf)
|
|
|
|
break;
|
|
|
|
if (vi->mergeable_rx_bufs || vi->big_packets)
|
|
|
|
give_pages(vi, buf);
|
|
|
|
else
|
|
|
|
dev_kfree_skb(buf);
|
|
|
|
--vi->num;
|
|
|
|
}
|
|
|
|
BUG_ON(vi->num != 0);
|
|
|
|
}
|
|
|
|
|
2011-12-22 19:28:32 +08:00
|
|
|
static void remove_vq_common(struct virtnet_info *vi)
|
2007-10-22 09:03:37 +08:00
|
|
|
{
|
2011-12-22 19:28:32 +08:00
|
|
|
vi->vdev->config->reset(vi->vdev);
|
2010-02-08 22:14:42 +08:00
|
|
|
|
|
|
|
/* Free unused buffers in both send and recv, if any. */
|
2010-01-29 11:20:04 +08:00
|
|
|
free_unused_bufs(vi);
|
2008-07-26 01:06:01 +08:00
|
|
|
|
2011-12-22 19:28:32 +08:00
|
|
|
vi->vdev->config->del_vqs(vi->vdev);
|
2009-06-13 12:16:36 +08:00
|
|
|
|
2008-07-26 01:06:01 +08:00
|
|
|
while (vi->pages)
|
|
|
|
__free_pages(get_a_page(vi, GFP_KERNEL), 0);
|
2011-12-22 19:28:32 +08:00
|
|
|
}
|
|
|
|
|
2012-12-03 22:24:15 +08:00
|
|
|
static void virtnet_remove(struct virtio_device *vdev)
|
2011-12-22 19:28:32 +08:00
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
/* Prevent config work handler from accessing the device. */
|
|
|
|
mutex_lock(&vi->config_lock);
|
|
|
|
vi->config_enable = false;
|
|
|
|
mutex_unlock(&vi->config_lock);
|
|
|
|
|
2011-12-22 19:28:32 +08:00
|
|
|
unregister_netdev(vi->dev);
|
|
|
|
|
|
|
|
remove_vq_common(vi);
|
2008-07-26 01:06:01 +08:00
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
flush_work(&vi->config_work);
|
|
|
|
|
2011-07-20 11:56:02 +08:00
|
|
|
free_percpu(vi->stats);
|
2007-11-20 00:20:42 +08:00
|
|
|
free_netdev(vi->dev);
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2011-12-22 19:28:33 +08:00
|
|
|
#ifdef CONFIG_PM
|
|
|
|
static int virtnet_freeze(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
/* Prevent config work handler from accessing the device */
|
|
|
|
mutex_lock(&vi->config_lock);
|
|
|
|
vi->config_enable = false;
|
|
|
|
mutex_unlock(&vi->config_lock);
|
|
|
|
|
2011-12-22 19:28:33 +08:00
|
|
|
netif_device_detach(vi->dev);
|
|
|
|
cancel_delayed_work_sync(&vi->refill);
|
|
|
|
|
|
|
|
if (netif_running(vi->dev))
|
|
|
|
napi_disable(&vi->napi);
|
|
|
|
|
|
|
|
remove_vq_common(vi);
|
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
flush_work(&vi->config_work);
|
|
|
|
|
2011-12-22 19:28:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtnet_restore(struct virtio_device *vdev)
|
|
|
|
{
|
|
|
|
struct virtnet_info *vi = vdev->priv;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = init_vqs(vi);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (netif_running(vi->dev))
|
|
|
|
virtnet_napi_enable(vi);
|
|
|
|
|
|
|
|
netif_device_attach(vi->dev);
|
|
|
|
|
|
|
|
if (!try_fill_recv(vi, GFP_KERNEL))
|
2012-08-21 05:51:24 +08:00
|
|
|
schedule_delayed_work(&vi->refill, 0);
|
2011-12-22 19:28:33 +08:00
|
|
|
|
2012-04-12 04:43:52 +08:00
|
|
|
mutex_lock(&vi->config_lock);
|
|
|
|
vi->config_enable = true;
|
|
|
|
mutex_unlock(&vi->config_lock);
|
|
|
|
|
2011-12-22 19:28:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-22 09:03:37 +08:00
|
|
|
static struct virtio_device_id id_table[] = {
|
|
|
|
{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
|
|
|
|
{ 0 },
|
|
|
|
};
|
|
|
|
|
2008-05-03 10:50:50 +08:00
|
|
|
static unsigned int features[] = {
|
2008-07-08 15:10:42 +08:00
|
|
|
VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
|
|
|
|
VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
|
2008-05-03 10:50:50 +08:00
|
|
|
VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
|
2008-04-18 11:24:27 +08:00
|
|
|
VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
|
2009-07-14 22:21:02 +08:00
|
|
|
VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
|
2009-02-04 17:02:34 +08:00
|
|
|
VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
|
2009-02-04 17:02:50 +08:00
|
|
|
VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
|
2012-04-12 04:43:52 +08:00
|
|
|
VIRTIO_NET_F_GUEST_ANNOUNCE,
|
2008-05-03 10:50:50 +08:00
|
|
|
};
|
|
|
|
|
2009-11-05 17:32:44 +08:00
|
|
|
static struct virtio_driver virtio_net_driver = {
|
2008-05-03 10:50:50 +08:00
|
|
|
.feature_table = features,
|
|
|
|
.feature_table_size = ARRAY_SIZE(features),
|
2007-10-22 09:03:37 +08:00
|
|
|
.driver.name = KBUILD_MODNAME,
|
|
|
|
.driver.owner = THIS_MODULE,
|
|
|
|
.id_table = id_table,
|
|
|
|
.probe = virtnet_probe,
|
2012-12-03 22:24:15 +08:00
|
|
|
.remove = virtnet_remove,
|
2009-01-20 09:09:49 +08:00
|
|
|
.config_changed = virtnet_config_changed,
|
2011-12-22 19:28:33 +08:00
|
|
|
#ifdef CONFIG_PM
|
|
|
|
.freeze = virtnet_freeze,
|
|
|
|
.restore = virtnet_restore,
|
|
|
|
#endif
|
2007-10-22 09:03:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init init(void)
|
|
|
|
{
|
2009-11-05 17:32:44 +08:00
|
|
|
return register_virtio_driver(&virtio_net_driver);
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit fini(void)
|
|
|
|
{
|
2009-11-05 17:32:44 +08:00
|
|
|
unregister_virtio_driver(&virtio_net_driver);
|
2007-10-22 09:03:37 +08:00
|
|
|
}
|
|
|
|
module_init(init);
|
|
|
|
module_exit(fini);
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(virtio, id_table);
|
|
|
|
MODULE_DESCRIPTION("Virtio network driver");
|
|
|
|
MODULE_LICENSE("GPL");
|