blob: f9bf94887ff159b187cfe547b0601298d4e7f1f0 [file] [log] [blame]
Rusty Russell48925e32009-09-24 09:59:20 -06001/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10002 *
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Rusty Russell296f96f2007-10-22 11:03:37 +100017 */
18//#define DEBUG
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
Herbert Xua9ea3fc2008-04-18 11:21:42 +080021#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100022#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
John Fastabendf600b692016-12-15 12:13:24 -080025#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010026#include <linux/bpf_trace.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100027#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080028#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Wanlong Gao8de4b2f2013-01-24 23:51:31 +000030#include <linux/cpu.h>
Michael Daltonab7db912014-01-16 22:23:27 -080031#include <linux/average.h>
Jason Wang91815632014-07-23 16:33:55 +080032#include <net/busy_poll.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100033
Amerigo Wangd34710e2013-05-09 19:50:51 +000034static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020035module_param(napi_weight, int, 0444);
36
Rusty Russelleb939922011-12-19 14:08:01 +000037static bool csum = true, gso = true;
Rusty Russell34a48572008-02-04 23:50:02 -050038module_param(csum, bool, 0444);
39module_param(gso, bool, 0444);
40
Rusty Russell296f96f2007-10-22 11:03:37 +100041/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080042#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080043#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100044
Johannes Berg5377d7582015-08-19 09:48:40 +020045/* RX packet size EWMA. The average packet size is used to determine the packet
46 * buffer size when refilling RX rings. As the entire RX ring may be refilled
47 * at once, the weight is chosen so that the EWMA will be insensitive to short-
48 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080049 */
Johannes Berg5377d7582015-08-19 09:48:40 +020050DECLARE_EWMA(pkt_len, 1, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080051
52/* Minimum alignment for mergeable packet buffers. */
53#define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, 256)
54
Rick Jones66846042011-11-14 14:17:08 +000055#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000056
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000057struct virtnet_stats {
Eric Dumazet83a27052012-06-05 22:35:24 +000058 struct u64_stats_sync tx_syncp;
59 struct u64_stats_sync rx_syncp;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000060 u64 tx_bytes;
61 u64 tx_packets;
62
63 u64 rx_bytes;
64 u64 rx_packets;
65};
66
Jason Wange9d74172012-12-07 07:04:55 +000067/* Internal representation of a send virtqueue */
68struct send_queue {
69 /* Virtqueue associated with this send _queue */
70 struct virtqueue *vq;
71
72 /* TX: fragments + linear part + virtio header */
73 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000074
75 /* Name of the send queue: output.$index */
76 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +000077};
78
79/* Internal representation of a receive virtqueue */
80struct receive_queue {
81 /* Virtqueue associated with this receive_queue */
82 struct virtqueue *vq;
83
Rusty Russell296f96f2007-10-22 11:03:37 +100084 struct napi_struct napi;
85
John Fastabendf600b692016-12-15 12:13:24 -080086 struct bpf_prog __rcu *xdp_prog;
87
Jason Wange9d74172012-12-07 07:04:55 +000088 /* Chain pages by the private ptr. */
89 struct page *pages;
90
Michael Daltonab7db912014-01-16 22:23:27 -080091 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +020092 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -080093
Michael Daltonfb518792014-01-16 22:23:26 -080094 /* Page frag for packet buffer allocation. */
95 struct page_frag alloc_frag;
96
Jason Wange9d74172012-12-07 07:04:55 +000097 /* RX: fragments + linear part + virtio header */
98 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000099
100 /* Name of this receive queue: input.$index */
101 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +0000102};
103
104struct virtnet_info {
105 struct virtio_device *vdev;
106 struct virtqueue *cvq;
107 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000108 struct send_queue *sq;
109 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000110 unsigned int status;
111
Jason Wang986a4f42012-12-07 07:04:56 +0000112 /* Max # of queue pairs supported by the device */
113 u16 max_queue_pairs;
114
115 /* # of queue pairs currently used by the driver */
116 u16 curr_queue_pairs;
117
John Fastabend672aafd2016-12-15 12:13:49 -0800118 /* # of XDP queue pairs currently used by the driver */
119 u16 xdp_queue_pairs;
120
Herbert Xu97402b92008-04-18 11:24:27 +0800121 /* I like... big packets and I cannot lie! */
122 bool big_packets;
123
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800124 /* Host will merge rx buffers for big packets (shake it! shake it!) */
125 bool mergeable_rx_bufs;
126
Jason Wang986a4f42012-12-07 07:04:56 +0000127 /* Has control virtqueue */
128 bool has_cvq;
129
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930130 /* Host can handle any s/g split between our header and packet data */
131 bool any_header_sg;
132
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300133 /* Packet virtio header size */
134 u8 hdr_len;
135
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000136 /* Active statistics */
137 struct virtnet_stats __percpu *stats;
138
Rusty Russell3161e452009-08-26 12:22:32 -0700139 /* Work struct for refilling if we run low on memory. */
140 struct delayed_work refill;
141
Jason Wang586d17c2012-04-11 20:43:52 +0000142 /* Work struct for config space updates */
143 struct work_struct config_work;
144
Jason Wang986a4f42012-12-07 07:04:56 +0000145 /* Does the affinity hint is set for virtqueues? */
146 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000147
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200148 /* CPU hotplug instances for online & dead */
149 struct hlist_node node;
150 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200151
152 /* Control VQ buffers: protected by the rtnl lock */
153 struct virtio_net_ctrl_hdr ctrl_hdr;
154 virtio_net_ctrl_ack ctrl_status;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700155 struct virtio_net_ctrl_mq ctrl_mq;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200156 u8 ctrl_promisc;
157 u8 ctrl_allmulti;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700158 u16 ctrl_vid;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100159
160 /* Ethtool settings */
161 u8 duplex;
162 u32 speed;
Rusty Russell296f96f2007-10-22 11:03:37 +1000163};
164
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000165struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300166 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000167 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300168 * hdr is in a separate sg buffer, and data sg buffer shares same page
169 * with this header sg. This padding makes next sg 16 byte aligned
170 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000171 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300172 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000173};
174
Jason Wang986a4f42012-12-07 07:04:56 +0000175/* Converting between virtqueue no. and kernel tx/rx queue no.
176 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
177 */
178static int vq2txq(struct virtqueue *vq)
179{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000180 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000181}
182
183static int txq2vq(int txq)
184{
185 return txq * 2 + 1;
186}
187
188static int vq2rxq(struct virtqueue *vq)
189{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000190 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000191}
192
193static int rxq2vq(int rxq)
194{
195 return rxq * 2;
196}
197
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300198static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000199{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300200 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000201}
202
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000203/*
204 * private is used to chain pages for big packets, put the whole
205 * most recent used list in the beginning for reuse
206 */
Jason Wange9d74172012-12-07 07:04:55 +0000207static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500208{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000209 struct page *end;
210
Jason Wange9d74172012-12-07 07:04:55 +0000211 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000212 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000213 end->private = (unsigned long)rq->pages;
214 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500215}
216
Jason Wange9d74172012-12-07 07:04:55 +0000217static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500218{
Jason Wange9d74172012-12-07 07:04:55 +0000219 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500220
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000221 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000222 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000223 /* clear private here, it is used to chain pages */
224 p->private = 0;
225 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500226 p = alloc_page(gfp_mask);
227 return p;
228}
229
Jason Wange9d74172012-12-07 07:04:55 +0000230static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000231{
Jason Wange9d74172012-12-07 07:04:55 +0000232 struct virtnet_info *vi = vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000233
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500234 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000235 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000236
Rusty Russell363f1512008-06-08 20:51:55 +1000237 /* We were probably waiting for more output buffers. */
Jason Wang986a4f42012-12-07 07:04:56 +0000238 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000239}
240
Michael Daltonab7db912014-01-16 22:23:27 -0800241static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
242{
243 unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
244 return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
245}
246
247static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
248{
249 return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
250
251}
252
253static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
254{
255 unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
256 return (unsigned long)buf | (size - 1);
257}
258
Mike Waychison34646452012-01-04 12:52:32 +0000259/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300260static struct sk_buff *page_to_skb(struct virtnet_info *vi,
261 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700262 struct page *page, unsigned int offset,
263 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000264{
265 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300266 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700267 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000268 char *p;
269
Michael Dalton2613af02013-10-28 15:44:18 -0700270 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000271
272 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100273 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000274 if (unlikely(!skb))
275 return NULL;
276
277 hdr = skb_vnet_hdr(skb);
278
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300279 hdr_len = vi->hdr_len;
280 if (vi->mergeable_rx_bufs)
281 hdr_padded_len = sizeof *hdr;
282 else
Michael Dalton2613af02013-10-28 15:44:18 -0700283 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000284
285 memcpy(hdr, p, hdr_len);
286
287 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700288 offset += hdr_padded_len;
289 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000290
291 copy = len;
292 if (copy > skb_tailroom(skb))
293 copy = skb_tailroom(skb);
294 memcpy(skb_put(skb, copy), p, copy);
295
296 len -= copy;
297 offset += copy;
298
Michael Dalton2613af02013-10-28 15:44:18 -0700299 if (vi->mergeable_rx_bufs) {
300 if (len)
301 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
302 else
303 put_page(page);
304 return skb;
305 }
306
Sasha Levine878d782011-09-28 04:40:54 +0000307 /*
308 * Verify that we can indeed put this data into a skb.
309 * This is here to handle cases when the device erroneously
310 * tries to receive more than is possible. This is usually
311 * the case of a broken device.
312 */
313 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000314 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000315 dev_kfree_skb(skb);
316 return NULL;
317 }
Michael Dalton2613af02013-10-28 15:44:18 -0700318 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000319 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700320 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
321 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
322 frag_size, truesize);
323 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000324 page = (struct page *)page->private;
325 offset = 0;
326 }
327
328 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000329 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000330
331 return skb;
332}
333
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100334static bool virtnet_xdp_xmit(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800335 struct receive_queue *rq,
336 struct send_queue *sq,
Jason Wangbb91acc2016-12-23 22:37:32 +0800337 struct xdp_buff *xdp,
338 void *data)
John Fastabend56434a02016-12-15 12:14:13 -0800339{
John Fastabend56434a02016-12-15 12:14:13 -0800340 struct virtio_net_hdr_mrg_rxbuf *hdr;
341 unsigned int num_sg, len;
342 void *xdp_sent;
343 int err;
344
345 /* Free up any pending old buffers before queueing new ones. */
346 while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Jason Wangbb91acc2016-12-23 22:37:32 +0800347 if (vi->mergeable_rx_bufs) {
348 struct page *sent_page = virt_to_head_page(xdp_sent);
349
350 put_page(sent_page);
351 } else { /* small buffer */
352 struct sk_buff *skb = xdp_sent;
353
354 kfree_skb(skb);
355 }
John Fastabend56434a02016-12-15 12:14:13 -0800356 }
357
Jason Wangbb91acc2016-12-23 22:37:32 +0800358 if (vi->mergeable_rx_bufs) {
359 /* Zero header and leave csum up to XDP layers */
360 hdr = xdp->data;
361 memset(hdr, 0, vi->hdr_len);
John Fastabend56434a02016-12-15 12:14:13 -0800362
Jason Wangbb91acc2016-12-23 22:37:32 +0800363 num_sg = 1;
364 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
365 } else { /* small buffer */
366 struct sk_buff *skb = data;
367
368 /* Zero header and leave csum up to XDP layers */
369 hdr = skb_vnet_hdr(skb);
370 memset(hdr, 0, vi->hdr_len);
371
372 num_sg = 2;
373 sg_init_table(sq->sg, 2);
374 sg_set_buf(sq->sg, hdr, vi->hdr_len);
375 skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
376 }
John Fastabend56434a02016-12-15 12:14:13 -0800377 err = virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
Jason Wangbb91acc2016-12-23 22:37:32 +0800378 data, GFP_ATOMIC);
John Fastabend56434a02016-12-15 12:14:13 -0800379 if (unlikely(err)) {
Jason Wangbb91acc2016-12-23 22:37:32 +0800380 if (vi->mergeable_rx_bufs) {
381 struct page *page = virt_to_head_page(xdp->data);
382
383 put_page(page);
384 } else /* small buffer */
385 kfree_skb(data);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100386 /* On error abort to avoid unnecessary kick */
387 return false;
John Fastabend56434a02016-12-15 12:14:13 -0800388 }
389
390 virtqueue_kick(sq->vq);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100391 return true;
John Fastabend56434a02016-12-15 12:14:13 -0800392}
393
John Fastabendf600b692016-12-15 12:13:24 -0800394static u32 do_xdp_prog(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800395 struct receive_queue *rq,
John Fastabendf600b692016-12-15 12:13:24 -0800396 struct bpf_prog *xdp_prog,
Jason Wangbb91acc2016-12-23 22:37:32 +0800397 void *data, int len)
John Fastabendf600b692016-12-15 12:13:24 -0800398{
399 int hdr_padded_len;
400 struct xdp_buff xdp;
Jason Wangbb91acc2016-12-23 22:37:32 +0800401 void *buf;
John Fastabend56434a02016-12-15 12:14:13 -0800402 unsigned int qp;
John Fastabendf600b692016-12-15 12:13:24 -0800403 u32 act;
John Fastabendf600b692016-12-15 12:13:24 -0800404
Jason Wangbb91acc2016-12-23 22:37:32 +0800405 if (vi->mergeable_rx_bufs) {
John Fastabendf600b692016-12-15 12:13:24 -0800406 hdr_padded_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Jason Wangbb91acc2016-12-23 22:37:32 +0800407 xdp.data = data + hdr_padded_len;
408 xdp.data_end = xdp.data + (len - vi->hdr_len);
409 buf = data;
410 } else { /* small buffers */
411 struct sk_buff *skb = data;
John Fastabendf600b692016-12-15 12:13:24 -0800412
Jason Wangbb91acc2016-12-23 22:37:32 +0800413 xdp.data = skb->data;
414 xdp.data_end = xdp.data + len;
415 buf = skb->data;
416 }
John Fastabendf600b692016-12-15 12:13:24 -0800417
418 act = bpf_prog_run_xdp(xdp_prog, &xdp);
419 switch (act) {
420 case XDP_PASS:
421 return XDP_PASS;
John Fastabend56434a02016-12-15 12:14:13 -0800422 case XDP_TX:
423 qp = vi->curr_queue_pairs -
424 vi->xdp_queue_pairs +
425 smp_processor_id();
Jason Wangbb91acc2016-12-23 22:37:32 +0800426 xdp.data = buf;
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100427 if (unlikely(!virtnet_xdp_xmit(vi, rq, &vi->sq[qp], &xdp,
428 data)))
429 trace_xdp_exception(vi->dev, xdp_prog, act);
John Fastabend56434a02016-12-15 12:14:13 -0800430 return XDP_TX;
John Fastabendf600b692016-12-15 12:13:24 -0800431 default:
432 bpf_warn_invalid_xdp_action(act);
John Fastabendf600b692016-12-15 12:13:24 -0800433 case XDP_ABORTED:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100434 trace_xdp_exception(vi->dev, xdp_prog, act);
John Fastabendf600b692016-12-15 12:13:24 -0800435 case XDP_DROP:
436 return XDP_DROP;
437 }
438}
439
Jason Wangbb91acc2016-12-23 22:37:32 +0800440static struct sk_buff *receive_small(struct net_device *dev,
441 struct virtnet_info *vi,
442 struct receive_queue *rq,
443 void *buf, unsigned int len)
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200444{
445 struct sk_buff * skb = buf;
Jason Wangbb91acc2016-12-23 22:37:32 +0800446 struct bpf_prog *xdp_prog;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200447
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300448 len -= vi->hdr_len;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200449 skb_trim(skb, len);
450
Jason Wangbb91acc2016-12-23 22:37:32 +0800451 rcu_read_lock();
452 xdp_prog = rcu_dereference(rq->xdp_prog);
453 if (xdp_prog) {
454 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
455 u32 act;
456
457 if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
458 goto err_xdp;
459 act = do_xdp_prog(vi, rq, xdp_prog, skb, len);
460 switch (act) {
461 case XDP_PASS:
462 break;
463 case XDP_TX:
464 rcu_read_unlock();
465 goto xdp_xmit;
466 case XDP_DROP:
467 default:
468 goto err_xdp;
469 }
470 }
471 rcu_read_unlock();
472
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200473 return skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800474
475err_xdp:
476 rcu_read_unlock();
477 dev->stats.rx_dropped++;
478 kfree_skb(skb);
479xdp_xmit:
480 return NULL;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200481}
482
483static struct sk_buff *receive_big(struct net_device *dev,
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300484 struct virtnet_info *vi,
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200485 struct receive_queue *rq,
486 void *buf,
487 unsigned int len)
488{
489 struct page *page = buf;
Jason Wangc47a43d2016-12-23 22:37:31 +0800490 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200491
492 if (unlikely(!skb))
493 goto err;
494
495 return skb;
496
497err:
498 dev->stats.rx_dropped++;
499 give_pages(rq, page);
500 return NULL;
501}
502
John Fastabend72979a62016-12-15 12:14:36 -0800503/* The conditions to enable XDP should preclude the underlying device from
504 * sending packets across multiple buffers (num_buf > 1). However per spec
505 * it does not appear to be illegal to do so but rather just against convention.
506 * So in order to avoid making a system unresponsive the packets are pushed
507 * into a page and the XDP program is run. This will be extremely slow and we
508 * push a warning to the user to fix this as soon as possible. Fixing this may
509 * require resolving the underlying hardware to determine why multiple buffers
510 * are being received or simply loading the XDP program in the ingress stack
511 * after the skb is built because there is no advantage to running it here
512 * anymore.
513 */
514static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800515 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800516 struct page *p,
517 int offset,
518 unsigned int *len)
519{
520 struct page *page = alloc_page(GFP_ATOMIC);
521 unsigned int page_off = 0;
522
523 if (!page)
524 return NULL;
525
526 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
527 page_off += *len;
528
Jason Wang56a86f82016-12-23 22:37:26 +0800529 while (--*num_buf) {
John Fastabend72979a62016-12-15 12:14:36 -0800530 unsigned int buflen;
531 unsigned long ctx;
532 void *buf;
533 int off;
534
535 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen);
536 if (unlikely(!ctx))
537 goto err_buf;
538
John Fastabend72979a62016-12-15 12:14:36 -0800539 buf = mergeable_ctx_to_buf_address(ctx);
540 p = virt_to_head_page(buf);
541 off = buf - page_address(p);
542
Jason Wang56a86f82016-12-23 22:37:26 +0800543 /* guard against a misconfigured or uncooperative backend that
544 * is sending packet larger than the MTU.
545 */
546 if ((page_off + buflen) > PAGE_SIZE) {
547 put_page(p);
548 goto err_buf;
549 }
550
John Fastabend72979a62016-12-15 12:14:36 -0800551 memcpy(page_address(page) + page_off,
552 page_address(p) + off, buflen);
553 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800554 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800555 }
556
557 *len = page_off;
558 return page;
559err_buf:
560 __free_pages(page, 0);
561 return NULL;
562}
563
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200564static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200565 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200566 struct receive_queue *rq,
Michael Daltonab7db912014-01-16 22:23:27 -0800567 unsigned long ctx,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200568 unsigned int len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000569{
Michael Daltonab7db912014-01-16 22:23:27 -0800570 void *buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300571 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
572 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200573 struct page *page = virt_to_head_page(buf);
574 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800575 struct sk_buff *head_skb, *curr_skb;
576 struct bpf_prog *xdp_prog;
577 unsigned int truesize;
Michael Daltonab7db912014-01-16 22:23:27 -0800578
John Fastabend56434a02016-12-15 12:14:13 -0800579 head_skb = NULL;
580
John Fastabendf600b692016-12-15 12:13:24 -0800581 rcu_read_lock();
582 xdp_prog = rcu_dereference(rq->xdp_prog);
583 if (xdp_prog) {
John Fastabend72979a62016-12-15 12:14:36 -0800584 struct page *xdp_page;
John Fastabendf600b692016-12-15 12:13:24 -0800585 u32 act;
586
Jason Wang73b62bd2016-12-23 22:37:24 +0800587 /* This happens when rx buffer size is underestimated */
John Fastabendf600b692016-12-15 12:13:24 -0800588 if (unlikely(num_buf > 1)) {
John Fastabend72979a62016-12-15 12:14:36 -0800589 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800590 xdp_page = xdp_linearize_page(rq, &num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800591 page, offset, &len);
592 if (!xdp_page)
593 goto err_xdp;
594 offset = 0;
595 } else {
596 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800597 }
598
599 /* Transient failure which in theory could occur if
600 * in-flight packets from before XDP was enabled reach
601 * the receive path after XDP is loaded. In practice I
602 * was not able to create this condition.
603 */
Jason Wangb00f70b2016-12-23 22:37:28 +0800604 if (unlikely(hdr->hdr.gso_type))
John Fastabendf600b692016-12-15 12:13:24 -0800605 goto err_xdp;
606
Jason Wangbb91acc2016-12-23 22:37:32 +0800607 act = do_xdp_prog(vi, rq, xdp_prog,
608 page_address(xdp_page) + offset, len);
John Fastabend56434a02016-12-15 12:14:13 -0800609 switch (act) {
610 case XDP_PASS:
Jason Wang1830f892016-12-23 22:37:27 +0800611 /* We can only create skb based on xdp_page. */
612 if (unlikely(xdp_page != page)) {
613 rcu_read_unlock();
614 put_page(page);
615 head_skb = page_to_skb(vi, rq, xdp_page,
616 0, len, PAGE_SIZE);
Jason Wang5c334742016-12-23 22:37:29 +0800617 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
Jason Wang1830f892016-12-23 22:37:27 +0800618 return head_skb;
619 }
John Fastabend56434a02016-12-15 12:14:13 -0800620 break;
621 case XDP_TX:
Jason Wang5c334742016-12-23 22:37:29 +0800622 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabend72979a62016-12-15 12:14:36 -0800623 if (unlikely(xdp_page != page))
624 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800625 rcu_read_unlock();
626 goto xdp_xmit;
627 case XDP_DROP:
628 default:
John Fastabend72979a62016-12-15 12:14:36 -0800629 if (unlikely(xdp_page != page))
630 __free_pages(xdp_page, 0);
Jason Wang5c334742016-12-23 22:37:29 +0800631 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabendf600b692016-12-15 12:13:24 -0800632 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800633 }
John Fastabendf600b692016-12-15 12:13:24 -0800634 }
635 rcu_read_unlock();
636
637 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
638 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
639 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000640
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200641 if (unlikely(!curr_skb))
642 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000643 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200644 int num_skb_frags;
645
Michael Daltonab7db912014-01-16 22:23:27 -0800646 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
647 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200648 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200649 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300650 virtio16_to_cpu(vi->vdev,
651 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200652 dev->stats.rx_length_errors++;
653 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000654 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200655
Michael Daltonab7db912014-01-16 22:23:27 -0800656 buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200657 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200658
659 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700660 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
661 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200662
663 if (unlikely(!nskb))
664 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700665 if (curr_skb == head_skb)
666 skb_shinfo(curr_skb)->frag_list = nskb;
667 else
668 curr_skb->next = nskb;
669 curr_skb = nskb;
670 head_skb->truesize += nskb->truesize;
671 num_skb_frags = 0;
672 }
Michael Daltonab7db912014-01-16 22:23:27 -0800673 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
Michael Dalton2613af02013-10-28 15:44:18 -0700674 if (curr_skb != head_skb) {
675 head_skb->data_len += len;
676 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800677 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700678 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200679 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800680 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
681 put_page(page);
682 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800683 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800684 } else {
685 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800686 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800687 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200688 }
689
Johannes Berg5377d7582015-08-19 09:48:40 +0200690 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200691 return head_skb;
692
John Fastabendf600b692016-12-15 12:13:24 -0800693err_xdp:
694 rcu_read_unlock();
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200695err_skb:
696 put_page(page);
697 while (--num_buf) {
Michael Daltonab7db912014-01-16 22:23:27 -0800698 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
699 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200700 pr_debug("%s: rx error: %d buffers missing\n",
701 dev->name, num_buf);
702 dev->stats.rx_length_errors++;
703 break;
704 }
Michael Daltonab7db912014-01-16 22:23:27 -0800705 page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200706 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000707 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200708err_buf:
709 dev->stats.rx_dropped++;
710 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800711xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200712 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000713}
714
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300715static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
716 void *buf, unsigned int len)
Rusty Russell296f96f2007-10-22 11:03:37 +1000717{
Jason Wange9d74172012-12-07 07:04:55 +0000718 struct net_device *dev = vi->dev;
Eric Dumazet58472a72012-02-13 06:53:41 +0000719 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000720 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300721 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000722
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +0300723 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000724 pr_debug("%s: short packet %i\n", dev->name, len);
725 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -0800726 if (vi->mergeable_rx_bufs) {
727 unsigned long ctx = (unsigned long)buf;
728 void *base = mergeable_ctx_to_buf_address(ctx);
729 put_page(virt_to_head_page(base));
730 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -0800731 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800732 } else {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000733 dev_kfree_skb(buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800734 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000735 return;
Rusty Russell296f96f2007-10-22 11:03:37 +1000736 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000737
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200738 if (vi->mergeable_rx_bufs)
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200739 skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200740 else if (vi->big_packets)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300741 skb = receive_big(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200742 else
Jason Wangbb91acc2016-12-23 22:37:32 +0800743 skb = receive_small(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200744
745 if (unlikely(!skb))
746 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800747
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000748 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000749
Eric Dumazet83a27052012-06-05 22:35:24 +0000750 u64_stats_update_begin(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000751 stats->rx_bytes += skb->len;
752 stats->rx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000753 u64_stats_update_end(&stats->rx_syncp);
Rusty Russell296f96f2007-10-22 11:03:37 +1000754
Mike Rapoporte858fae2016-06-08 16:09:21 +0300755 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +0000756 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000757
Mike Rapoporte858fae2016-06-08 16:09:21 +0300758 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
759 virtio_is_little_endian(vi->vdev))) {
760 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
761 dev->name, hdr->hdr.gso_type,
762 hdr->hdr.gso_size);
763 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000764 }
765
Mike Rapoportd1dc06d2016-06-14 08:29:38 +0300766 skb->protocol = eth_type_trans(skb, dev);
767 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
768 ntohs(skb->protocol), skb->len, skb->pkt_type);
769
Eric Dumazet0fbd0502015-07-31 18:25:17 +0200770 napi_gro_receive(&rq->napi, skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000771 return;
772
773frame_err:
774 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000775 dev_kfree_skb(skb);
776}
777
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300778static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
779 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000780{
781 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300782 struct virtio_net_hdr_mrg_rxbuf *hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000783 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000784
Michael Dalton5061de32013-11-14 10:41:04 -0800785 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000786 if (unlikely(!skb))
787 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800788
Michael Dalton5061de32013-11-14 10:41:04 -0800789 skb_put(skb, GOOD_PACKET_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000790
791 hdr = skb_vnet_hdr(skb);
Jason Wang547c8902015-08-27 14:53:06 +0800792 sg_init_table(rq->sg, 2);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300793 sg_set_buf(rq->sg, hdr, vi->hdr_len);
Jason Wange9d74172012-12-07 07:04:55 +0000794 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000795
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030796 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000797 if (err < 0)
798 dev_kfree_skb(skb);
799
800 return err;
801}
802
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300803static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
804 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000805{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000806 struct page *first, *list = NULL;
807 char *p;
808 int i, err, offset;
809
Rusty Russella5835442014-09-11 10:17:36 +0930810 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
811
Jason Wange9d74172012-12-07 07:04:55 +0000812 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000813 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000814 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000815 if (!first) {
816 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000817 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000818 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700819 }
Jason Wange9d74172012-12-07 07:04:55 +0000820 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000821
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000822 /* chain new page in list head to match sg */
823 first->private = (unsigned long)list;
824 list = first;
825 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800826
Jason Wange9d74172012-12-07 07:04:55 +0000827 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000828 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000829 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000830 return -ENOMEM;
831 }
832 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800833
Jason Wange9d74172012-12-07 07:04:55 +0000834 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300835 /* a separated rq->sg[0] for header - required in case !any_header_sg */
836 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +0800837
Jason Wange9d74172012-12-07 07:04:55 +0000838 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000839 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000840 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800841
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000842 /* chain first in list head */
843 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030844 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
845 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000846 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000847 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800848
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000849 return err;
850}
Herbert Xu97402b92008-04-18 11:24:27 +0800851
Johannes Berg5377d7582015-08-19 09:48:40 +0200852static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000853{
Michael Daltonab7db912014-01-16 22:23:27 -0800854 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -0800855 unsigned int len;
856
Johannes Berg5377d7582015-08-19 09:48:40 +0200857 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael Daltonfbf28d72014-01-16 22:23:30 -0800858 GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
859 return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
860}
861
862static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
863{
Michael Daltonfb518792014-01-16 22:23:26 -0800864 struct page_frag *alloc_frag = &rq->alloc_frag;
865 char *buf;
Michael Daltonab7db912014-01-16 22:23:27 -0800866 unsigned long ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000867 int err;
Michael Daltonfb518792014-01-16 22:23:26 -0800868 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +1000869
Michael Daltonfbf28d72014-01-16 22:23:30 -0800870 len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
Michael Daltonab7db912014-01-16 22:23:27 -0800871 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000872 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -0800873
Michael Daltonfb518792014-01-16 22:23:26 -0800874 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
Michael Daltonab7db912014-01-16 22:23:27 -0800875 ctx = mergeable_buf_to_ctx(buf, len);
Michael Daltonfb518792014-01-16 22:23:26 -0800876 get_page(alloc_frag->page);
Michael Daltonfb518792014-01-16 22:23:26 -0800877 alloc_frag->offset += len;
878 hole = alloc_frag->size - alloc_frag->offset;
Michael Daltonab7db912014-01-16 22:23:27 -0800879 if (hole < len) {
880 /* To avoid internal fragmentation, if there is very likely not
881 * enough space for another buffer, add the remaining space to
882 * the current buffer. This extra space is not included in
883 * the truesize stored in ctx.
884 */
Michael Daltonfb518792014-01-16 22:23:26 -0800885 len += hole;
886 alloc_frag->offset += hole;
887 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000888
Michael Daltonfb518792014-01-16 22:23:26 -0800889 sg_init_one(rq->sg, buf, len);
Michael Daltonab7db912014-01-16 22:23:27 -0800890 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000891 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -0700892 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000893
894 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000895}
896
Rusty Russellb2baed62011-12-29 00:42:38 +0000897/*
898 * Returns false if we couldn't fill entirely (OOM).
899 *
900 * Normally run in the receive path, but can also be run from ndo_open
901 * before we're receiving packets, or from refill_work which is
902 * careful to disable receiving (using napi_disable).
903 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300904static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
905 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800906{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800907 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000908 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800909
Michael Daltonfb518792014-01-16 22:23:26 -0800910 gfp |= __GFP_COLD;
Amit Shah0aea51c2009-08-26 14:58:28 +0530911 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000912 if (vi->mergeable_rx_bufs)
Jason Wange9d74172012-12-07 07:04:55 +0000913 err = add_recvbuf_mergeable(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000914 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300915 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000916 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300917 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800918
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000919 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030920 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800921 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800922 } while (rq->vq->num_free);
Jason Wang681daee22014-03-26 13:03:00 +0800923 virtqueue_kick(rq->vq);
Rusty Russell3161e452009-08-26 12:22:32 -0700924 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800925}
926
Rusty Russell18445c42008-02-04 23:49:57 -0500927static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000928{
929 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +0000930 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +0000931
Rusty Russell18445c42008-02-04 23:49:57 -0500932 /* Schedule NAPI, Suppress further interrupts if successful. */
Jason Wange9d74172012-12-07 07:04:55 +0000933 if (napi_schedule_prep(&rq->napi)) {
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300934 virtqueue_disable_cb(rvq);
Jason Wange9d74172012-12-07 07:04:55 +0000935 __napi_schedule(&rq->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500936 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000937}
938
Jason Wange9d74172012-12-07 07:04:55 +0000939static void virtnet_napi_enable(struct receive_queue *rq)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800940{
Jason Wange9d74172012-12-07 07:04:55 +0000941 napi_enable(&rq->napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800942
943 /* If all buffers were filled by other side before we napi_enabled, we
944 * won't get another interrupt, so process any outstanding packets
945 * now. virtnet_poll wants re-enable the queue, so we disable here.
946 * We synchronize against interrupts via NAPI_STATE_SCHED */
Jason Wange9d74172012-12-07 07:04:55 +0000947 if (napi_schedule_prep(&rq->napi)) {
948 virtqueue_disable_cb(rq->vq);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300949 local_bh_disable();
Jason Wange9d74172012-12-07 07:04:55 +0000950 __napi_schedule(&rq->napi);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300951 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800952 }
953}
954
Rusty Russell3161e452009-08-26 12:22:32 -0700955static void refill_work(struct work_struct *work)
956{
Jason Wange9d74172012-12-07 07:04:55 +0000957 struct virtnet_info *vi =
958 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -0700959 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +0000960 int i;
Rusty Russell3161e452009-08-26 12:22:32 -0700961
Sasha Levin55257d72013-04-29 12:00:08 +0930962 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +0000963 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700964
Jason Wang986a4f42012-12-07 07:04:56 +0000965 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300966 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +0000967 virtnet_napi_enable(rq);
968
969 /* In theory, this can happen: if we don't get any buffers in
970 * we will *never* try to fill again.
971 */
972 if (still_empty)
973 schedule_delayed_work(&vi->refill, HZ/2);
974 }
Rusty Russell3161e452009-08-26 12:22:32 -0700975}
976
Jason Wang2ffa7592014-07-23 16:33:54 +0800977static int virtnet_receive(struct receive_queue *rq, int budget)
Rusty Russell296f96f2007-10-22 11:03:37 +1000978{
Jason Wange9d74172012-12-07 07:04:55 +0000979 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wang2ffa7592014-07-23 16:33:54 +0800980 unsigned int len, received = 0;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000981 void *buf;
Rusty Russell296f96f2007-10-22 11:03:37 +1000982
Rusty Russell296f96f2007-10-22 11:03:37 +1000983 while (received < budget &&
Jason Wange9d74172012-12-07 07:04:55 +0000984 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300985 receive_buf(vi, rq, buf, len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000986 received++;
987 }
988
Jason Wangbe121f42014-01-16 14:45:24 +0800989 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300990 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700991 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -0700992 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000993
Jason Wang2ffa7592014-07-23 16:33:54 +0800994 return received;
995}
996
997static int virtnet_poll(struct napi_struct *napi, int budget)
998{
999 struct receive_queue *rq =
1000 container_of(napi, struct receive_queue, napi);
Li RongQingfaadb052015-03-26 15:39:45 +08001001 unsigned int r, received;
Jason Wang2ffa7592014-07-23 16:33:54 +08001002
Li RongQingfaadb052015-03-26 15:39:45 +08001003 received = virtnet_receive(rq, budget);
Jason Wang2ffa7592014-07-23 16:33:54 +08001004
Rusty Russell8329d982007-11-19 11:20:43 -05001005 /* Out of packets? */
1006 if (received < budget) {
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +03001007 r = virtqueue_enable_cb_prepare(rq->vq);
Eric Dumazet0fbd0502015-07-31 18:25:17 +02001008 napi_complete_done(napi, received);
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +03001009 if (unlikely(virtqueue_poll(rq->vq, r)) &&
Joe Perches8e95a202009-12-03 07:58:21 +00001010 napi_schedule_prep(napi)) {
Jason Wange9d74172012-12-07 07:04:55 +00001011 virtqueue_disable_cb(rq->vq);
Ben Hutchings288379f2009-01-19 16:43:59 -08001012 __napi_schedule(napi);
Christian Borntraeger4265f162008-03-14 14:17:05 +01001013 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001014 }
1015
1016 return received;
1017}
1018
Jason Wang91815632014-07-23 16:33:55 +08001019#ifdef CONFIG_NET_RX_BUSY_POLL
1020/* must be called with local_bh_disable()d */
1021static int virtnet_busy_poll(struct napi_struct *napi)
1022{
1023 struct receive_queue *rq =
1024 container_of(napi, struct receive_queue, napi);
1025 struct virtnet_info *vi = rq->vq->vdev->priv;
1026 int r, received = 0, budget = 4;
1027
1028 if (!(vi->status & VIRTIO_NET_S_LINK_UP))
1029 return LL_FLUSH_FAILED;
1030
1031 if (!napi_schedule_prep(napi))
1032 return LL_FLUSH_BUSY;
1033
1034 virtqueue_disable_cb(rq->vq);
1035
1036again:
1037 received += virtnet_receive(rq, budget);
1038
1039 r = virtqueue_enable_cb_prepare(rq->vq);
1040 clear_bit(NAPI_STATE_SCHED, &napi->state);
1041 if (unlikely(virtqueue_poll(rq->vq, r)) &&
1042 napi_schedule_prep(napi)) {
1043 virtqueue_disable_cb(rq->vq);
1044 if (received < budget) {
1045 budget -= received;
1046 goto again;
1047 } else {
1048 __napi_schedule(napi);
1049 }
1050 }
1051
1052 return received;
1053}
1054#endif /* CONFIG_NET_RX_BUSY_POLL */
1055
Jason Wang986a4f42012-12-07 07:04:56 +00001056static int virtnet_open(struct net_device *dev)
1057{
1058 struct virtnet_info *vi = netdev_priv(dev);
1059 int i;
1060
Jason Wange4166622013-05-21 20:03:58 +00001061 for (i = 0; i < vi->max_queue_pairs; i++) {
1062 if (i < vi->curr_queue_pairs)
1063 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001064 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001065 schedule_delayed_work(&vi->refill, 0);
Jason Wang986a4f42012-12-07 07:04:56 +00001066 virtnet_napi_enable(&vi->rq[i]);
1067 }
1068
1069 return 0;
1070}
1071
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001072static void free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001073{
1074 struct sk_buff *skb;
Michael S. Tsirkin6ee57bc2012-10-16 23:56:14 +10301075 unsigned int len;
Jason Wange9d74172012-12-07 07:04:55 +00001076 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +00001077 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10001078
Jason Wange9d74172012-12-07 07:04:55 +00001079 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001080 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001081
Eric Dumazet83a27052012-06-05 22:35:24 +00001082 u64_stats_update_begin(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001083 stats->tx_bytes += skb->len;
1084 stats->tx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +00001085 u64_stats_update_end(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001086
Eric Dumazeted79bab2009-10-14 14:36:43 +00001087 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001088 }
1089}
1090
Jason Wange9d74172012-12-07 07:04:55 +00001091static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001092{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001093 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001094 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001095 struct virtnet_info *vi = sq->vq->vdev->priv;
Michael S. Tsirkin7bedc7d2012-10-16 23:56:14 +10301096 unsigned num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001097 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301098 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001099
Johannes Berge1749612008-10-27 15:59:26 -07001100 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301101
1102 can_push = vi->any_header_sg &&
1103 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1104 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1105 /* Even if we can, don't push here yet as this would skew
1106 * csum_start offset below. */
1107 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001108 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301109 else
1110 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001111
Mike Rapoporte858fae2016-06-08 16:09:21 +03001112 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1113 virtio_is_little_endian(vi->vdev)))
1114 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001115
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001116 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001117 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001118
Jason Wang547c8902015-08-27 14:53:06 +08001119 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301120 if (can_push) {
1121 __skb_push(skb, hdr_len);
1122 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1123 /* Pull header back to avoid skew in tx bytes calculations. */
1124 __skb_pull(skb, hdr_len);
1125 } else {
1126 sg_set_buf(sq->sg, hdr, hdr_len);
1127 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1128 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301129 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001130}
1131
Stephen Hemminger424efe92009-08-31 19:50:51 +00001132static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001133{
1134 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001135 int qnum = skb_get_queue_mapping(skb);
1136 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301137 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001138 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1139 bool kick = !skb->xmit_more;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001140
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001141 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001142 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001143
Jacob Keller074c3582014-06-25 02:37:13 +00001144 /* timestamp packet in software */
1145 skb_tx_timestamp(skb);
1146
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001147 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001148 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001149
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301150 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001151 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301152 dev->stats.tx_fifo_errors++;
1153 if (net_ratelimit())
1154 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001155 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001156 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001157 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001158 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001159 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001160
Rusty Russell48925e32009-09-24 09:59:20 -06001161 /* Don't wait up for transmitted skbs to be freed. */
1162 skb_orphan(skb);
1163 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -05001164
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001165 /* If running out of space, stop queue to avoid getting packets that we
1166 * are then unable to transmit.
1167 * An alternative would be to force queuing layer to requeue the skb by
1168 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1169 * returned in a normal path of operation: it means that driver is not
1170 * maintaining the TX queue stop/start state properly, and causes
1171 * the stack to do a non-trivial amount of useless work.
1172 * Since most packets only take 1 or 2 ring slots, stopping the queue
1173 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001174 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001175 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001176 netif_stop_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001177 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001178 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001179 free_old_xmit_skbs(sq);
1180 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001181 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001182 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001183 }
1184 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001185 }
Rusty Russell48925e32009-09-24 09:59:20 -06001186
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001187 if (kick || netif_xmit_stopped(txq))
David S. Miller0b725a22014-08-25 15:51:53 -07001188 virtqueue_kick(sq->vq);
1189
Rusty Russell48925e32009-09-24 09:59:20 -06001190 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001191}
1192
Amos Kong40cbfc32013-01-21 01:17:21 +00001193/*
1194 * Send command via the control virtqueue and check status. Commands
1195 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001196 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001197 */
1198static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001199 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001200{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301201 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001202 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001203
1204 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301205 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001206
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001207 vi->ctrl_status = ~0;
1208 vi->ctrl_hdr.class = class;
1209 vi->ctrl_hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301210 /* Add header */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001211 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301212 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001213
Rusty Russellf7bc9592013-03-20 15:44:28 +10301214 if (out)
1215 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001216
Rusty Russellf7bc9592013-03-20 15:44:28 +10301217 /* Add return status. */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001218 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001219 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001220
stephen hemmingerd24bae32013-12-09 16:17:40 -08001221 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301222 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001223
Heinz Graalfs67975902013-10-29 09:40:02 +10301224 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001225 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001226
1227 /* Spin for a response, the kick causes an ioport write, trapping
1228 * into the hypervisor, so the request should be handled immediately.
1229 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301230 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1231 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001232 cpu_relax();
1233
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001234 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001235}
1236
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001237static int virtnet_set_mac_address(struct net_device *dev, void *p)
1238{
1239 struct virtnet_info *vi = netdev_priv(dev);
1240 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001241 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001242 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001243 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001244
Shyam Saini801822d2016-12-24 00:44:58 +05301245 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001246 if (!addr)
1247 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001248
1249 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001250 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001251 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001252
Amos Kong7e58d5a2013-01-21 01:17:23 +00001253 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1254 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1255 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001256 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001257 dev_warn(&vdev->dev,
1258 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001259 ret = -EINVAL;
1260 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001261 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001262 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1263 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301264 unsigned int i;
1265
1266 /* Naturally, this has an atomicity problem. */
1267 for (i = 0; i < dev->addr_len; i++)
1268 virtio_cwrite8(vdev,
1269 offsetof(struct virtio_net_config, mac) +
1270 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001271 }
1272
1273 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001274 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001275
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001276out:
1277 kfree(addr);
1278 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001279}
1280
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001281static void virtnet_stats(struct net_device *dev,
1282 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001283{
1284 struct virtnet_info *vi = netdev_priv(dev);
1285 int cpu;
1286 unsigned int start;
1287
1288 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +00001289 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001290 u64 tpackets, tbytes, rpackets, rbytes;
1291
1292 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001293 start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001294 tpackets = stats->tx_packets;
1295 tbytes = stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001296 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001297
1298 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001299 start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001300 rpackets = stats->rx_packets;
1301 rbytes = stats->rx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001302 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001303
1304 tot->rx_packets += rpackets;
1305 tot->tx_packets += tpackets;
1306 tot->rx_bytes += rbytes;
1307 tot->tx_bytes += tbytes;
1308 }
1309
1310 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001311 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001312 tot->rx_dropped = dev->stats.rx_dropped;
1313 tot->rx_length_errors = dev->stats.rx_length_errors;
1314 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001315}
1316
Amit Shahda74e892008-02-29 16:24:50 +05301317#ifdef CONFIG_NET_POLL_CONTROLLER
1318static void virtnet_netpoll(struct net_device *dev)
1319{
1320 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001321 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301322
Jason Wang986a4f42012-12-07 07:04:56 +00001323 for (i = 0; i < vi->curr_queue_pairs; i++)
1324 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301325}
1326#endif
1327
Jason Wang586d17c2012-04-11 20:43:52 +00001328static void virtnet_ack_link_announce(struct virtnet_info *vi)
1329{
1330 rtnl_lock();
1331 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001332 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001333 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1334 rtnl_unlock();
1335}
1336
Jason Wang986a4f42012-12-07 07:04:56 +00001337static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1338{
1339 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001340 struct net_device *dev = vi->dev;
1341
1342 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1343 return 0;
1344
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001345 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1346 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001347
1348 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001349 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001350 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1351 queue_pairs);
1352 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301353 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001354 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001355 /* virtnet_open() will refill when device is going to up. */
1356 if (dev->flags & IFF_UP)
1357 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301358 }
Jason Wang986a4f42012-12-07 07:04:56 +00001359
1360 return 0;
1361}
1362
Rusty Russell296f96f2007-10-22 11:03:37 +10001363static int virtnet_close(struct net_device *dev)
1364{
1365 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001366 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001367
Rusty Russellb2baed62011-12-29 00:42:38 +00001368 /* Make sure refill_work doesn't re-enable napi! */
1369 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001370
1371 for (i = 0; i < vi->max_queue_pairs; i++)
1372 napi_disable(&vi->rq[i].napi);
Rusty Russell296f96f2007-10-22 11:03:37 +10001373
Rusty Russell296f96f2007-10-22 11:03:37 +10001374 return 0;
1375}
1376
Alex Williamson2af76982009-02-04 09:02:40 +00001377static void virtnet_set_rx_mode(struct net_device *dev)
1378{
1379 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001380 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001381 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001382 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001383 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001384 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001385 void *buf;
1386 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001387
stephen hemminger788a8b62013-12-09 16:18:45 -08001388 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001389 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1390 return;
1391
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001392 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1393 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001394
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001395 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001396
1397 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001398 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001399 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001400 vi->ctrl_promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001401
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001402 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001403
1404 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001405 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001406 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001407 vi->ctrl_allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001408
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001409 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001410 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001411 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001412 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1413 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1414 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001415 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001416 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001417
Alex Williamson23e258e2009-05-01 17:27:56 +00001418 sg_init_table(sg, 2);
1419
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001420 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001421 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001422 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001423 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001424 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001425
1426 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001427 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001428
1429 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001430 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001431
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001432 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001433 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001434 netdev_for_each_mc_addr(ha, dev)
1435 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001436
1437 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001438 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001439
1440 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001441 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001442 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001443
1444 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001445}
1446
Patrick McHardy80d5c362013-04-19 02:04:28 +00001447static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1448 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001449{
1450 struct virtnet_info *vi = netdev_priv(dev);
1451 struct scatterlist sg;
1452
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001453 vi->ctrl_vid = vid;
1454 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001455
1456 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001457 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001458 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001459 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001460}
1461
Patrick McHardy80d5c362013-04-19 02:04:28 +00001462static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1463 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001464{
1465 struct virtnet_info *vi = netdev_priv(dev);
1466 struct scatterlist sg;
1467
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001468 vi->ctrl_vid = vid;
1469 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001470
1471 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001472 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001473 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001474 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001475}
1476
Wanlong Gao8898c212013-01-24 23:51:30 +00001477static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001478{
1479 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001480
1481 if (vi->affinity_hint_set) {
1482 for (i = 0; i < vi->max_queue_pairs; i++) {
1483 virtqueue_set_affinity(vi->rq[i].vq, -1);
1484 virtqueue_set_affinity(vi->sq[i].vq, -1);
1485 }
1486
1487 vi->affinity_hint_set = false;
1488 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001489}
1490
1491static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001492{
1493 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001494 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001495
1496 /* In multiqueue mode, when the number of cpu is equal to the number of
1497 * queue pairs, we let the queue pairs to be private to one cpu by
1498 * setting the affinity hint to eliminate the contention.
1499 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001500 if (vi->curr_queue_pairs == 1 ||
1501 vi->max_queue_pairs != num_online_cpus()) {
1502 virtnet_clean_affinity(vi, -1);
1503 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001504 }
1505
Wanlong Gao8898c212013-01-24 23:51:30 +00001506 i = 0;
1507 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001508 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1509 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001510 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001511 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001512 }
1513
Wanlong Gao8898c212013-01-24 23:51:30 +00001514 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001515}
1516
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001517static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001518{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001519 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1520 node);
1521 virtnet_set_affinity(vi);
1522 return 0;
1523}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001524
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001525static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1526{
1527 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1528 node_dead);
1529 virtnet_set_affinity(vi);
1530 return 0;
1531}
Jason Wang3ab098d2013-10-15 11:18:58 +08001532
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001533static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1534{
1535 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1536 node);
1537
1538 virtnet_clean_affinity(vi, cpu);
1539 return 0;
1540}
1541
1542static enum cpuhp_state virtionet_online;
1543
1544static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1545{
1546 int ret;
1547
1548 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1549 if (ret)
1550 return ret;
1551 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1552 &vi->node_dead);
1553 if (!ret)
1554 return ret;
1555 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1556 return ret;
1557}
1558
1559static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1560{
1561 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1562 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1563 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001564}
1565
Rick Jones8f9f4662011-10-19 08:10:59 +00001566static void virtnet_get_ringparam(struct net_device *dev,
1567 struct ethtool_ringparam *ring)
1568{
1569 struct virtnet_info *vi = netdev_priv(dev);
1570
Jason Wang986a4f42012-12-07 07:04:56 +00001571 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1572 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001573 ring->rx_pending = ring->rx_max_pending;
1574 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001575}
1576
Rick Jones66846042011-11-14 14:17:08 +00001577
1578static void virtnet_get_drvinfo(struct net_device *dev,
1579 struct ethtool_drvinfo *info)
1580{
1581 struct virtnet_info *vi = netdev_priv(dev);
1582 struct virtio_device *vdev = vi->vdev;
1583
1584 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1585 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1586 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1587
1588}
1589
Jason Wangd73bcd22012-12-07 07:04:57 +00001590/* TODO: Eliminate OOO packets during switching */
1591static int virtnet_set_channels(struct net_device *dev,
1592 struct ethtool_channels *channels)
1593{
1594 struct virtnet_info *vi = netdev_priv(dev);
1595 u16 queue_pairs = channels->combined_count;
1596 int err;
1597
1598 /* We don't support separate rx/tx channels.
1599 * We don't allow setting 'other' channels.
1600 */
1601 if (channels->rx_count || channels->tx_count || channels->other_count)
1602 return -EINVAL;
1603
Amos Kongc18e9cd2014-04-18 13:45:41 +08001604 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00001605 return -EINVAL;
1606
John Fastabendf600b692016-12-15 12:13:24 -08001607 /* For now we don't support modifying channels while XDP is loaded
1608 * also when XDP is loaded all RX queues have XDP programs so we only
1609 * need to check a single RX queue.
1610 */
1611 if (vi->rq[0].xdp_prog)
1612 return -EINVAL;
1613
Wanlong Gao47be2472013-01-24 23:51:29 +00001614 get_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001615 err = virtnet_set_queues(vi, queue_pairs);
1616 if (!err) {
1617 netif_set_real_num_tx_queues(dev, queue_pairs);
1618 netif_set_real_num_rx_queues(dev, queue_pairs);
1619
Wanlong Gao8898c212013-01-24 23:51:30 +00001620 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001621 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001622 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001623
1624 return err;
1625}
1626
1627static void virtnet_get_channels(struct net_device *dev,
1628 struct ethtool_channels *channels)
1629{
1630 struct virtnet_info *vi = netdev_priv(dev);
1631
1632 channels->combined_count = vi->curr_queue_pairs;
1633 channels->max_combined = vi->max_queue_pairs;
1634 channels->max_other = 0;
1635 channels->rx_count = 0;
1636 channels->tx_count = 0;
1637 channels->other_count = 0;
1638}
1639
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001640/* Check if the user is trying to change anything besides speed/duplex */
1641static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
1642{
1643 struct ethtool_cmd diff1 = *cmd;
1644 struct ethtool_cmd diff2 = {};
1645
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001646 /* cmd is always set so we need to clear it, validate the port type
1647 * and also without autonegotiation we can ignore advertising
1648 */
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001649 ethtool_cmd_speed_set(&diff1, 0);
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001650 diff2.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001651 diff1.advertising = 0;
1652 diff1.duplex = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001653 diff1.cmd = 0;
1654
1655 return !memcmp(&diff1, &diff2, sizeof(diff1));
1656}
1657
1658static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1659{
1660 struct virtnet_info *vi = netdev_priv(dev);
1661 u32 speed;
1662
1663 speed = ethtool_cmd_speed(cmd);
1664 /* don't allow custom speed and duplex */
1665 if (!ethtool_validate_speed(speed) ||
1666 !ethtool_validate_duplex(cmd->duplex) ||
1667 !virtnet_validate_ethtool_cmd(cmd))
1668 return -EINVAL;
1669 vi->speed = speed;
1670 vi->duplex = cmd->duplex;
1671
1672 return 0;
1673}
1674
1675static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1676{
1677 struct virtnet_info *vi = netdev_priv(dev);
1678
1679 ethtool_cmd_speed_set(cmd, vi->speed);
1680 cmd->duplex = vi->duplex;
1681 cmd->port = PORT_OTHER;
1682
1683 return 0;
1684}
1685
1686static void virtnet_init_settings(struct net_device *dev)
1687{
1688 struct virtnet_info *vi = netdev_priv(dev);
1689
1690 vi->speed = SPEED_UNKNOWN;
1691 vi->duplex = DUPLEX_UNKNOWN;
1692}
1693
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001694static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001695 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001696 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001697 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001698 .set_channels = virtnet_set_channels,
1699 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00001700 .get_ts_info = ethtool_op_get_ts_info,
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001701 .get_settings = virtnet_get_settings,
1702 .set_settings = virtnet_set_settings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001703};
1704
John Fastabendf600b692016-12-15 12:13:24 -08001705static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1706{
1707 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1708 struct virtnet_info *vi = netdev_priv(dev);
1709 struct bpf_prog *old_prog;
John Fastabend672aafd2016-12-15 12:13:49 -08001710 u16 xdp_qp = 0, curr_qp;
1711 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08001712
1713 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Jason Wang92502fe2016-12-23 22:37:30 +08001714 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1715 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1716 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
John Fastabendf600b692016-12-15 12:13:24 -08001717 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1718 return -EOPNOTSUPP;
1719 }
1720
1721 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1722 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1723 return -EINVAL;
1724 }
1725
1726 if (dev->mtu > max_sz) {
1727 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1728 return -EINVAL;
1729 }
1730
John Fastabend672aafd2016-12-15 12:13:49 -08001731 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1732 if (prog)
1733 xdp_qp = nr_cpu_ids;
1734
1735 /* XDP requires extra queues for XDP_TX */
1736 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1737 netdev_warn(dev, "request %i queues but max is %i\n",
1738 curr_qp + xdp_qp, vi->max_queue_pairs);
1739 return -ENOMEM;
1740 }
1741
1742 err = virtnet_set_queues(vi, curr_qp + xdp_qp);
1743 if (err) {
1744 dev_warn(&dev->dev, "XDP Device queue allocation failure.\n");
1745 return err;
1746 }
1747
John Fastabendf600b692016-12-15 12:13:24 -08001748 if (prog) {
1749 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
John Fastabend672aafd2016-12-15 12:13:49 -08001750 if (IS_ERR(prog)) {
1751 virtnet_set_queues(vi, curr_qp);
John Fastabendf600b692016-12-15 12:13:24 -08001752 return PTR_ERR(prog);
John Fastabend672aafd2016-12-15 12:13:49 -08001753 }
John Fastabendf600b692016-12-15 12:13:24 -08001754 }
1755
John Fastabend672aafd2016-12-15 12:13:49 -08001756 vi->xdp_queue_pairs = xdp_qp;
1757 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1758
John Fastabendf600b692016-12-15 12:13:24 -08001759 for (i = 0; i < vi->max_queue_pairs; i++) {
1760 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1761 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1762 if (old_prog)
1763 bpf_prog_put(old_prog);
1764 }
1765
1766 return 0;
1767}
1768
1769static bool virtnet_xdp_query(struct net_device *dev)
1770{
1771 struct virtnet_info *vi = netdev_priv(dev);
1772 int i;
1773
1774 for (i = 0; i < vi->max_queue_pairs; i++) {
1775 if (vi->rq[i].xdp_prog)
1776 return true;
1777 }
1778 return false;
1779}
1780
1781static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1782{
1783 switch (xdp->command) {
1784 case XDP_SETUP_PROG:
1785 return virtnet_xdp_set(dev, xdp->prog);
1786 case XDP_QUERY_PROG:
1787 xdp->prog_attached = virtnet_xdp_query(dev);
1788 return 0;
1789 default:
1790 return -EINVAL;
1791 }
1792}
1793
Stephen Hemminger76288b42009-01-06 10:44:22 -08001794static const struct net_device_ops virtnet_netdev = {
1795 .ndo_open = virtnet_open,
1796 .ndo_stop = virtnet_close,
1797 .ndo_start_xmit = start_xmit,
1798 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001799 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001800 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001801 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001802 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1803 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001804#ifdef CONFIG_NET_POLL_CONTROLLER
1805 .ndo_poll_controller = virtnet_netpoll,
1806#endif
Jason Wang91815632014-07-23 16:33:55 +08001807#ifdef CONFIG_NET_RX_BUSY_POLL
1808 .ndo_busy_poll = virtnet_busy_poll,
1809#endif
John Fastabendf600b692016-12-15 12:13:24 -08001810 .ndo_xdp = virtnet_xdp,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001811};
1812
Jason Wang586d17c2012-04-11 20:43:52 +00001813static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001814{
Jason Wang586d17c2012-04-11 20:43:52 +00001815 struct virtnet_info *vi =
1816 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001817 u16 v;
1818
Rusty Russell855e0c52013-10-14 18:11:51 +10301819 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1820 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301821 return;
Jason Wang586d17c2012-04-11 20:43:52 +00001822
1823 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001824 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001825 virtnet_ack_link_announce(vi);
1826 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001827
1828 /* Ignore unknown (future) status bits */
1829 v &= VIRTIO_NET_S_LINK_UP;
1830
1831 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301832 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001833
1834 vi->status = v;
1835
1836 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1837 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001838 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001839 } else {
1840 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001841 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001842 }
1843}
1844
1845static void virtnet_config_changed(struct virtio_device *vdev)
1846{
1847 struct virtnet_info *vi = vdev->priv;
1848
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001849 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001850}
1851
Jason Wang986a4f42012-12-07 07:04:56 +00001852static void virtnet_free_queues(struct virtnet_info *vi)
1853{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001854 int i;
1855
Jason Wangab3971b2015-03-12 13:57:44 +08001856 for (i = 0; i < vi->max_queue_pairs; i++) {
1857 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001858 netif_napi_del(&vi->rq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08001859 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001860
Eric Dumazet963abe52016-11-15 22:24:12 -08001861 /* We called napi_hash_del() before netif_napi_del(),
1862 * we need to respect an RCU grace period before freeing vi->rq
1863 */
1864 synchronize_net();
1865
Jason Wang986a4f42012-12-07 07:04:56 +00001866 kfree(vi->rq);
1867 kfree(vi->sq);
1868}
1869
1870static void free_receive_bufs(struct virtnet_info *vi)
1871{
John Fastabendf600b692016-12-15 12:13:24 -08001872 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00001873 int i;
1874
John Fastabendf600b692016-12-15 12:13:24 -08001875 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00001876 for (i = 0; i < vi->max_queue_pairs; i++) {
1877 while (vi->rq[i].pages)
1878 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08001879
1880 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1881 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1882 if (old_prog)
1883 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00001884 }
John Fastabendf600b692016-12-15 12:13:24 -08001885 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001886}
1887
Michael Daltonfb518792014-01-16 22:23:26 -08001888static void free_receive_page_frags(struct virtnet_info *vi)
1889{
1890 int i;
1891 for (i = 0; i < vi->max_queue_pairs; i++)
1892 if (vi->rq[i].alloc_frag.page)
1893 put_page(vi->rq[i].alloc_frag.page);
1894}
1895
John Fastabend56434a02016-12-15 12:14:13 -08001896static bool is_xdp_queue(struct virtnet_info *vi, int q)
1897{
1898 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1899 return false;
1900 else if (q < vi->curr_queue_pairs)
1901 return true;
1902 else
1903 return false;
1904}
1905
Jason Wang986a4f42012-12-07 07:04:56 +00001906static void free_unused_bufs(struct virtnet_info *vi)
1907{
1908 void *buf;
1909 int i;
1910
1911 for (i = 0; i < vi->max_queue_pairs; i++) {
1912 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08001913 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1914 if (!is_xdp_queue(vi, i))
1915 dev_kfree_skb(buf);
1916 else
1917 put_page(virt_to_head_page(buf));
1918 }
Jason Wang986a4f42012-12-07 07:04:56 +00001919 }
1920
1921 for (i = 0; i < vi->max_queue_pairs; i++) {
1922 struct virtqueue *vq = vi->rq[i].vq;
1923
1924 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08001925 if (vi->mergeable_rx_bufs) {
1926 unsigned long ctx = (unsigned long)buf;
1927 void *base = mergeable_ctx_to_buf_address(ctx);
1928 put_page(virt_to_head_page(base));
1929 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04001930 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001931 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001932 dev_kfree_skb(buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001933 }
Jason Wang986a4f42012-12-07 07:04:56 +00001934 }
Jason Wang986a4f42012-12-07 07:04:56 +00001935 }
1936}
1937
Jason Wange9d74172012-12-07 07:04:55 +00001938static void virtnet_del_vqs(struct virtnet_info *vi)
1939{
1940 struct virtio_device *vdev = vi->vdev;
1941
Wanlong Gao8898c212013-01-24 23:51:30 +00001942 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00001943
Jason Wange9d74172012-12-07 07:04:55 +00001944 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00001945
1946 virtnet_free_queues(vi);
1947}
1948
1949static int virtnet_find_vqs(struct virtnet_info *vi)
1950{
1951 vq_callback_t **callbacks;
1952 struct virtqueue **vqs;
1953 int ret = -ENOMEM;
1954 int i, total_vqs;
1955 const char **names;
1956
1957 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1958 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1959 * possible control vq.
1960 */
1961 total_vqs = vi->max_queue_pairs * 2 +
1962 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1963
1964 /* Allocate space for find_vqs parameters */
1965 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1966 if (!vqs)
1967 goto err_vq;
1968 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1969 if (!callbacks)
1970 goto err_callback;
1971 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1972 if (!names)
1973 goto err_names;
1974
1975 /* Parameters for control virtqueue, if any */
1976 if (vi->has_cvq) {
1977 callbacks[total_vqs - 1] = NULL;
1978 names[total_vqs - 1] = "control";
1979 }
1980
1981 /* Allocate/initialize parameters for send/receive virtqueues */
1982 for (i = 0; i < vi->max_queue_pairs; i++) {
1983 callbacks[rxq2vq(i)] = skb_recv_done;
1984 callbacks[txq2vq(i)] = skb_xmit_done;
1985 sprintf(vi->rq[i].name, "input.%d", i);
1986 sprintf(vi->sq[i].name, "output.%d", i);
1987 names[rxq2vq(i)] = vi->rq[i].name;
1988 names[txq2vq(i)] = vi->sq[i].name;
1989 }
1990
1991 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1992 names);
1993 if (ret)
1994 goto err_find;
1995
1996 if (vi->has_cvq) {
1997 vi->cvq = vqs[total_vqs - 1];
1998 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00001999 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00002000 }
2001
2002 for (i = 0; i < vi->max_queue_pairs; i++) {
2003 vi->rq[i].vq = vqs[rxq2vq(i)];
2004 vi->sq[i].vq = vqs[txq2vq(i)];
2005 }
2006
2007 kfree(names);
2008 kfree(callbacks);
2009 kfree(vqs);
2010
2011 return 0;
2012
2013err_find:
2014 kfree(names);
2015err_names:
2016 kfree(callbacks);
2017err_callback:
2018 kfree(vqs);
2019err_vq:
2020 return ret;
2021}
2022
2023static int virtnet_alloc_queues(struct virtnet_info *vi)
2024{
2025 int i;
2026
2027 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2028 if (!vi->sq)
2029 goto err_sq;
2030 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002031 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002032 goto err_rq;
2033
2034 INIT_DELAYED_WORK(&vi->refill, refill_work);
2035 for (i = 0; i < vi->max_queue_pairs; i++) {
2036 vi->rq[i].pages = NULL;
2037 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2038 napi_weight);
2039
2040 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002041 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002042 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2043 }
2044
2045 return 0;
2046
2047err_rq:
2048 kfree(vi->sq);
2049err_sq:
2050 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002051}
2052
Amit Shah3f9c10b2011-12-22 16:58:31 +05302053static int init_vqs(struct virtnet_info *vi)
2054{
Jason Wang986a4f42012-12-07 07:04:56 +00002055 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302056
Jason Wang986a4f42012-12-07 07:04:56 +00002057 /* Allocate send & receive queues */
2058 ret = virtnet_alloc_queues(vi);
2059 if (ret)
2060 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302061
Jason Wang986a4f42012-12-07 07:04:56 +00002062 ret = virtnet_find_vqs(vi);
2063 if (ret)
2064 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302065
Wanlong Gao47be2472013-01-24 23:51:29 +00002066 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002067 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002068 put_online_cpus();
2069
Amit Shah3f9c10b2011-12-22 16:58:31 +05302070 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002071
2072err_free:
2073 virtnet_free_queues(vi);
2074err:
2075 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302076}
2077
Michael Daltonfbf28d72014-01-16 22:23:30 -08002078#ifdef CONFIG_SYSFS
2079static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2080 struct rx_queue_attribute *attribute, char *buf)
2081{
2082 struct virtnet_info *vi = netdev_priv(queue->dev);
2083 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Johannes Berg5377d7582015-08-19 09:48:40 +02002084 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002085
2086 BUG_ON(queue_index >= vi->max_queue_pairs);
2087 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2088 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2089}
2090
2091static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2092 __ATTR_RO(mergeable_rx_buffer_size);
2093
2094static struct attribute *virtio_net_mrg_rx_attrs[] = {
2095 &mergeable_rx_buffer_size_attribute.attr,
2096 NULL
2097};
2098
2099static const struct attribute_group virtio_net_mrg_rx_group = {
2100 .name = "virtio_net",
2101 .attrs = virtio_net_mrg_rx_attrs
2102};
2103#endif
2104
Jason Wang892d6eb2014-11-20 17:03:05 +08002105static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2106 unsigned int fbit,
2107 const char *fname, const char *dname)
2108{
2109 if (!virtio_has_feature(vdev, fbit))
2110 return false;
2111
2112 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2113 fname, dname);
2114
2115 return true;
2116}
2117
2118#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2119 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2120
2121static bool virtnet_validate_features(struct virtio_device *vdev)
2122{
2123 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2124 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2125 "VIRTIO_NET_F_CTRL_VQ") ||
2126 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2127 "VIRTIO_NET_F_CTRL_VQ") ||
2128 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2129 "VIRTIO_NET_F_CTRL_VQ") ||
2130 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2131 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2132 "VIRTIO_NET_F_CTRL_VQ"))) {
2133 return false;
2134 }
2135
2136 return true;
2137}
2138
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002139#define MIN_MTU ETH_MIN_MTU
2140#define MAX_MTU ETH_MAX_MTU
2141
Rusty Russell296f96f2007-10-22 11:03:37 +10002142static int virtnet_probe(struct virtio_device *vdev)
2143{
Jason Wang986a4f42012-12-07 07:04:56 +00002144 int i, err;
Rusty Russell296f96f2007-10-22 11:03:37 +10002145 struct net_device *dev;
2146 struct virtnet_info *vi;
Jason Wang986a4f42012-12-07 07:04:56 +00002147 u16 max_queue_pairs;
Aaron Conole14de9d12016-06-03 16:57:12 -04002148 int mtu;
Jason Wang986a4f42012-12-07 07:04:56 +00002149
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002150 if (!vdev->config->get) {
2151 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2152 __func__);
2153 return -EINVAL;
2154 }
2155
Jason Wang892d6eb2014-11-20 17:03:05 +08002156 if (!virtnet_validate_features(vdev))
2157 return -EINVAL;
2158
Jason Wang986a4f42012-12-07 07:04:56 +00002159 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302160 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2161 struct virtio_net_config,
2162 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002163
2164 /* We need at least 2 queue's */
2165 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2166 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2167 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2168 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002169
2170 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002171 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002172 if (!dev)
2173 return -ENOMEM;
2174
2175 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002176 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002177 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002178 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002179
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002180 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002181 SET_NETDEV_DEV(dev, &vdev->dev);
2182
2183 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002184 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002185 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002186 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002187 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002188 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002189
2190 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002191 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05002192 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2193 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002194 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002195 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2196 dev->hw_features |= NETIF_F_TSO;
2197 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2198 dev->hw_features |= NETIF_F_TSO6;
2199 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2200 dev->hw_features |= NETIF_F_TSO_ECN;
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002201 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2202 dev->hw_features |= NETIF_F_UFO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002203
Jason Wang41f2f122014-12-24 11:03:52 +08002204 dev->features |= NETIF_F_GSO_ROBUST;
2205
Michał Mirosław98e778c2011-03-31 01:01:35 +00002206 if (gso)
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002207 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
Michał Mirosław98e778c2011-03-31 01:01:35 +00002208 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002209 }
Thomas Huth4f491292013-08-27 17:09:02 +02002210 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2211 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002212
Jason Wang4fda8302013-04-10 23:32:21 +00002213 dev->vlan_features = dev->features;
2214
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002215 /* MTU range: 68 - 65535 */
2216 dev->min_mtu = MIN_MTU;
2217 dev->max_mtu = MAX_MTU;
2218
Rusty Russell296f96f2007-10-22 11:03:37 +10002219 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302220 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2221 virtio_cread_bytes(vdev,
2222 offsetof(struct virtio_net_config, mac),
2223 dev->dev_addr, dev->addr_len);
2224 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002225 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002226
2227 /* Set up our device-specific information */
2228 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002229 vi->dev = dev;
2230 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002231 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002232 vi->stats = alloc_percpu(struct virtnet_stats);
2233 err = -ENOMEM;
2234 if (vi->stats == NULL)
2235 goto free;
2236
John Stultz827da442013-10-07 15:51:58 -07002237 for_each_possible_cpu(i) {
2238 struct virtnet_stats *virtnet_stats;
2239 virtnet_stats = per_cpu_ptr(vi->stats, i);
2240 u64_stats_init(&virtnet_stats->tx_syncp);
2241 u64_stats_init(&virtnet_stats->rx_syncp);
2242 }
2243
Jason Wang586d17c2012-04-11 20:43:52 +00002244 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002245
Herbert Xu97402b92008-04-18 11:24:27 +08002246 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002247 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2248 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002249 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2250 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002251 vi->big_packets = true;
2252
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002253 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2254 vi->mergeable_rx_bufs = true;
2255
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002256 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2257 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002258 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2259 else
2260 vi->hdr_len = sizeof(struct virtio_net_hdr);
2261
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002262 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2263 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302264 vi->any_header_sg = true;
2265
Jason Wang986a4f42012-12-07 07:04:56 +00002266 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2267 vi->has_cvq = true;
2268
Aaron Conole14de9d12016-06-03 16:57:12 -04002269 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2270 mtu = virtio_cread16(vdev,
2271 offsetof(struct virtio_net_config,
2272 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002273 if (mtu < dev->min_mtu) {
Aaron Conole14de9d12016-06-03 16:57:12 -04002274 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
Aaron Conole93a205e2016-10-25 16:12:12 -04002275 } else {
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002276 dev->mtu = mtu;
Aaron Conole93a205e2016-10-25 16:12:12 -04002277 dev->max_mtu = mtu;
2278 }
Aaron Conole14de9d12016-06-03 16:57:12 -04002279 }
2280
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002281 if (vi->any_header_sg)
2282 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002283
Jason Wang44900012016-11-25 12:37:26 +08002284 /* Enable multiqueue by default */
2285 if (num_online_cpus() >= max_queue_pairs)
2286 vi->curr_queue_pairs = max_queue_pairs;
2287 else
2288 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002289 vi->max_queue_pairs = max_queue_pairs;
2290
2291 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302292 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002293 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08002294 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002295
Michael Daltonfbf28d72014-01-16 22:23:30 -08002296#ifdef CONFIG_SYSFS
2297 if (vi->mergeable_rx_bufs)
2298 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2299#endif
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08002300 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2301 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002302
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002303 virtnet_init_settings(dev);
2304
Rusty Russell296f96f2007-10-22 11:03:37 +10002305 err = register_netdev(dev);
2306 if (err) {
2307 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002308 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10002309 }
Rusty Russellb3369c12008-02-04 23:50:02 -05002310
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10302311 virtio_device_ready(vdev);
2312
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002313 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002314 if (err) {
2315 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08002316 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002317 }
2318
Jason Wanga2208712016-12-13 14:23:05 +08002319 rtnl_lock();
2320 virtnet_set_queues(vi, vi->curr_queue_pairs);
2321 rtnl_unlock();
Jason Wang44900012016-11-25 12:37:26 +08002322
Jason Wang167c25e2010-11-10 14:45:41 +00002323 /* Assume link up if device can't report link status,
2324 otherwise get link status from config. */
2325 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2326 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002327 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00002328 } else {
2329 vi->status = VIRTIO_NET_S_LINK_UP;
2330 netif_carrier_on(dev);
2331 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002332
Jason Wang986a4f42012-12-07 07:04:56 +00002333 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2334 dev->name, max_queue_pairs);
2335
Rusty Russell296f96f2007-10-22 11:03:37 +10002336 return 0;
2337
wangyunjianf00e35e2016-05-31 11:52:43 +08002338free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10302339 vi->vdev->config->reset(vdev);
2340
Rusty Russellb3369c12008-02-04 23:50:02 -05002341 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002342free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00002343 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08002344 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00002345 virtnet_del_vqs(vi);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002346free_stats:
2347 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10002348free:
2349 free_netdev(dev);
2350 return err;
2351}
2352
Amit Shah04486ed2011-12-22 16:58:32 +05302353static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10002354{
Amit Shah04486ed2011-12-22 16:58:32 +05302355 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00002356
2357 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00002358 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002359
Jason Wang986a4f42012-12-07 07:04:56 +00002360 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002361
Michael Daltonfb518792014-01-16 22:23:26 -08002362 free_receive_page_frags(vi);
2363
Jason Wang986a4f42012-12-07 07:04:56 +00002364 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05302365}
2366
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002367static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05302368{
2369 struct virtnet_info *vi = vdev->priv;
2370
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002371 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002372
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302373 /* Make sure no work handler is accessing the device. */
2374 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002375
Amit Shah04486ed2011-12-22 16:58:32 +05302376 unregister_netdev(vi->dev);
2377
2378 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002379
Krishna Kumar2e66f552011-07-20 03:56:02 +00002380 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05002381 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002382}
2383
Aaron Lu89107002013-09-17 09:25:23 +09302384#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302385static int virtnet_freeze(struct virtio_device *vdev)
2386{
2387 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00002388 int i;
Amit Shah0741bcb2011-12-22 16:58:33 +05302389
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002390 virtnet_cpu_notif_remove(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002391
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302392 /* Make sure no work handler is accessing the device */
2393 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002394
Amit Shah0741bcb2011-12-22 16:58:33 +05302395 netif_device_detach(vi->dev);
2396 cancel_delayed_work_sync(&vi->refill);
2397
Jason Wang91815632014-07-23 16:33:55 +08002398 if (netif_running(vi->dev)) {
Jason Wangab3971b2015-03-12 13:57:44 +08002399 for (i = 0; i < vi->max_queue_pairs; i++)
Jason Wang986a4f42012-12-07 07:04:56 +00002400 napi_disable(&vi->rq[i].napi);
Jason Wang91815632014-07-23 16:33:55 +08002401 }
Amit Shah0741bcb2011-12-22 16:58:33 +05302402
2403 remove_vq_common(vi);
2404
2405 return 0;
2406}
2407
2408static int virtnet_restore(struct virtio_device *vdev)
2409{
2410 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00002411 int err, i;
Amit Shah0741bcb2011-12-22 16:58:33 +05302412
2413 err = init_vqs(vi);
2414 if (err)
2415 return err;
2416
Michael S. Tsirkine53fbd12014-10-15 10:22:32 +10302417 virtio_device_ready(vdev);
2418
Jason Wang6cd4ce02013-12-30 11:34:40 +08002419 if (netif_running(vi->dev)) {
2420 for (i = 0; i < vi->curr_queue_pairs; i++)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03002421 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wang6cd4ce02013-12-30 11:34:40 +08002422 schedule_delayed_work(&vi->refill, 0);
2423
Jason Wang986a4f42012-12-07 07:04:56 +00002424 for (i = 0; i < vi->max_queue_pairs; i++)
2425 virtnet_napi_enable(&vi->rq[i]);
Jason Wang6cd4ce02013-12-30 11:34:40 +08002426 }
Amit Shah0741bcb2011-12-22 16:58:33 +05302427
2428 netif_device_attach(vi->dev);
2429
Jason Wang35ed1592013-10-15 11:18:59 +08002430 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00002431 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang35ed1592013-10-15 11:18:59 +08002432 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002433
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002434 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002435 if (err)
2436 return err;
2437
Amit Shah0741bcb2011-12-22 16:58:33 +05302438 return 0;
2439}
2440#endif
2441
Rusty Russell296f96f2007-10-22 11:03:37 +10002442static struct virtio_device_id id_table[] = {
2443 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2444 { 0 },
2445};
2446
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002447#define VIRTNET_FEATURES \
2448 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2449 VIRTIO_NET_F_MAC, \
2450 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2451 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2452 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2453 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2454 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2455 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2456 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2457 VIRTIO_NET_F_MTU
2458
Rusty Russellc45a6812008-05-02 21:50:50 -05002459static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002460 VIRTNET_FEATURES,
2461};
2462
2463static unsigned int features_legacy[] = {
2464 VIRTNET_FEATURES,
2465 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302466 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05002467};
2468
Uwe Kleine-König22402522009-11-05 01:32:44 -08002469static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05002470 .feature_table = features,
2471 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002472 .feature_table_legacy = features_legacy,
2473 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10002474 .driver.name = KBUILD_MODNAME,
2475 .driver.owner = THIS_MODULE,
2476 .id_table = id_table,
2477 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002478 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002479 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09302480#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302481 .freeze = virtnet_freeze,
2482 .restore = virtnet_restore,
2483#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10002484};
2485
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002486static __init int virtio_net_driver_init(void)
2487{
2488 int ret;
2489
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002490 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002491 virtnet_cpu_online,
2492 virtnet_cpu_down_prep);
2493 if (ret < 0)
2494 goto out;
2495 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002496 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002497 NULL, virtnet_cpu_dead);
2498 if (ret)
2499 goto err_dead;
2500
2501 ret = register_virtio_driver(&virtio_net_driver);
2502 if (ret)
2503 goto err_virtio;
2504 return 0;
2505err_virtio:
2506 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2507err_dead:
2508 cpuhp_remove_multi_state(virtionet_online);
2509out:
2510 return ret;
2511}
2512module_init(virtio_net_driver_init);
2513
2514static __exit void virtio_net_driver_exit(void)
2515{
2516 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2517 cpuhp_remove_multi_state(virtionet_online);
2518 unregister_virtio_driver(&virtio_net_driver);
2519}
2520module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10002521
2522MODULE_DEVICE_TABLE(virtio, id_table);
2523MODULE_DESCRIPTION("Virtio network driver");
2524MODULE_LICENSE("GPL");