blob: 29982c7f6080ae99f4753052dc80ff5310f1134b [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>
Rusty Russell296f96f2007-10-22 11:03:37 +100032
Amerigo Wangd34710e2013-05-09 19:50:51 +000033static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020034module_param(napi_weight, int, 0444);
35
Rusty Russelleb939922011-12-19 14:08:01 +000036static bool csum = true, gso = true;
Rusty Russell34a48572008-02-04 23:50:02 -050037module_param(csum, bool, 0444);
38module_param(gso, bool, 0444);
39
Rusty Russell296f96f2007-10-22 11:03:37 +100040/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080041#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080042#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100043
Johannes Berg5377d7582015-08-19 09:48:40 +020044/* RX packet size EWMA. The average packet size is used to determine the packet
45 * buffer size when refilling RX rings. As the entire RX ring may be refilled
46 * at once, the weight is chosen so that the EWMA will be insensitive to short-
47 * term, transient changes in packet size.
Michael Daltonab7db912014-01-16 22:23:27 -080048 */
Johannes Berg5377d7582015-08-19 09:48:40 +020049DECLARE_EWMA(pkt_len, 1, 64)
Michael Daltonab7db912014-01-16 22:23:27 -080050
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020051/* With mergeable buffers we align buffer address and use the low bits to
52 * encode its true size. Buffer size is up to 1 page so we need to align to
53 * square root of page size to ensure we reserve enough bits to encode the true
54 * size.
55 */
56#define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2)
57
Michael Daltonab7db912014-01-16 22:23:27 -080058/* Minimum alignment for mergeable packet buffers. */
Michael S. Tsirkind0fa28f2017-01-23 21:37:52 +020059#define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \
60 1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT)
Michael Daltonab7db912014-01-16 22:23:27 -080061
Rick Jones66846042011-11-14 14:17:08 +000062#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000063
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000064struct virtnet_stats {
Eric Dumazet83a27052012-06-05 22:35:24 +000065 struct u64_stats_sync tx_syncp;
66 struct u64_stats_sync rx_syncp;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000067 u64 tx_bytes;
68 u64 tx_packets;
69
70 u64 rx_bytes;
71 u64 rx_packets;
72};
73
Jason Wange9d74172012-12-07 07:04:55 +000074/* Internal representation of a send virtqueue */
75struct send_queue {
76 /* Virtqueue associated with this send _queue */
77 struct virtqueue *vq;
78
79 /* TX: fragments + linear part + virtio header */
80 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000081
82 /* Name of the send queue: output.$index */
83 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +000084};
85
86/* Internal representation of a receive virtqueue */
87struct receive_queue {
88 /* Virtqueue associated with this receive_queue */
89 struct virtqueue *vq;
90
Rusty Russell296f96f2007-10-22 11:03:37 +100091 struct napi_struct napi;
92
John Fastabendf600b692016-12-15 12:13:24 -080093 struct bpf_prog __rcu *xdp_prog;
94
Jason Wange9d74172012-12-07 07:04:55 +000095 /* Chain pages by the private ptr. */
96 struct page *pages;
97
Michael Daltonab7db912014-01-16 22:23:27 -080098 /* Average packet length for mergeable receive buffers. */
Johannes Berg5377d7582015-08-19 09:48:40 +020099 struct ewma_pkt_len mrg_avg_pkt_len;
Michael Daltonab7db912014-01-16 22:23:27 -0800100
Michael Daltonfb518792014-01-16 22:23:26 -0800101 /* Page frag for packet buffer allocation. */
102 struct page_frag alloc_frag;
103
Jason Wange9d74172012-12-07 07:04:55 +0000104 /* RX: fragments + linear part + virtio header */
105 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +0000106
107 /* Name of this receive queue: input.$index */
108 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +0000109};
110
111struct virtnet_info {
112 struct virtio_device *vdev;
113 struct virtqueue *cvq;
114 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +0000115 struct send_queue *sq;
116 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +0000117 unsigned int status;
118
Jason Wang986a4f42012-12-07 07:04:56 +0000119 /* Max # of queue pairs supported by the device */
120 u16 max_queue_pairs;
121
122 /* # of queue pairs currently used by the driver */
123 u16 curr_queue_pairs;
124
John Fastabend672aafd2016-12-15 12:13:49 -0800125 /* # of XDP queue pairs currently used by the driver */
126 u16 xdp_queue_pairs;
127
Herbert Xu97402b92008-04-18 11:24:27 +0800128 /* I like... big packets and I cannot lie! */
129 bool big_packets;
130
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800131 /* Host will merge rx buffers for big packets (shake it! shake it!) */
132 bool mergeable_rx_bufs;
133
Jason Wang986a4f42012-12-07 07:04:56 +0000134 /* Has control virtqueue */
135 bool has_cvq;
136
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930137 /* Host can handle any s/g split between our header and packet data */
138 bool any_header_sg;
139
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300140 /* Packet virtio header size */
141 u8 hdr_len;
142
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000143 /* Active statistics */
144 struct virtnet_stats __percpu *stats;
145
Rusty Russell3161e452009-08-26 12:22:32 -0700146 /* Work struct for refilling if we run low on memory. */
147 struct delayed_work refill;
148
Jason Wang586d17c2012-04-11 20:43:52 +0000149 /* Work struct for config space updates */
150 struct work_struct config_work;
151
Jason Wang986a4f42012-12-07 07:04:56 +0000152 /* Does the affinity hint is set for virtqueues? */
153 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000154
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +0200155 /* CPU hotplug instances for online & dead */
156 struct hlist_node node;
157 struct hlist_node node_dead;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200158
159 /* Control VQ buffers: protected by the rtnl lock */
160 struct virtio_net_ctrl_hdr ctrl_hdr;
161 virtio_net_ctrl_ack ctrl_status;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700162 struct virtio_net_ctrl_mq ctrl_mq;
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +0200163 u8 ctrl_promisc;
164 u8 ctrl_allmulti;
Andy Lutomirskia725ee32016-07-18 15:34:49 -0700165 u16 ctrl_vid;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +0100166
167 /* Ethtool settings */
168 u8 duplex;
169 u32 speed;
Rusty Russell296f96f2007-10-22 11:03:37 +1000170};
171
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000172struct padded_vnet_hdr {
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300173 struct virtio_net_hdr_mrg_rxbuf hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000174 /*
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300175 * hdr is in a separate sg buffer, and data sg buffer shares same page
176 * with this header sg. This padding makes next sg 16 byte aligned
177 * after the header.
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000178 */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300179 char padding[4];
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000180};
181
Jason Wang986a4f42012-12-07 07:04:56 +0000182/* Converting between virtqueue no. and kernel tx/rx queue no.
183 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
184 */
185static int vq2txq(struct virtqueue *vq)
186{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000187 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000188}
189
190static int txq2vq(int txq)
191{
192 return txq * 2 + 1;
193}
194
195static int vq2rxq(struct virtqueue *vq)
196{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000197 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000198}
199
200static int rxq2vq(int rxq)
201{
202 return rxq * 2;
203}
204
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300205static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000206{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300207 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000208}
209
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000210/*
211 * private is used to chain pages for big packets, put the whole
212 * most recent used list in the beginning for reuse
213 */
Jason Wange9d74172012-12-07 07:04:55 +0000214static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500215{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000216 struct page *end;
217
Jason Wange9d74172012-12-07 07:04:55 +0000218 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000219 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000220 end->private = (unsigned long)rq->pages;
221 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500222}
223
Jason Wange9d74172012-12-07 07:04:55 +0000224static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500225{
Jason Wange9d74172012-12-07 07:04:55 +0000226 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500227
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000228 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000229 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000230 /* clear private here, it is used to chain pages */
231 p->private = 0;
232 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500233 p = alloc_page(gfp_mask);
234 return p;
235}
236
Jason Wange9d74172012-12-07 07:04:55 +0000237static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000238{
Jason Wange9d74172012-12-07 07:04:55 +0000239 struct virtnet_info *vi = vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000240
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500241 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000242 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000243
Rusty Russell363f1512008-06-08 20:51:55 +1000244 /* We were probably waiting for more output buffers. */
Jason Wang986a4f42012-12-07 07:04:56 +0000245 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000246}
247
Michael Daltonab7db912014-01-16 22:23:27 -0800248static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
249{
250 unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
251 return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
252}
253
254static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
255{
256 return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
257
258}
259
260static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
261{
262 unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
263 return (unsigned long)buf | (size - 1);
264}
265
Mike Waychison34646452012-01-04 12:52:32 +0000266/* Called from bottom half context */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300267static struct sk_buff *page_to_skb(struct virtnet_info *vi,
268 struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700269 struct page *page, unsigned int offset,
270 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000271{
272 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300273 struct virtio_net_hdr_mrg_rxbuf *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700274 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000275 char *p;
276
Michael Dalton2613af02013-10-28 15:44:18 -0700277 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000278
279 /* copy small packet so we can reuse these pages for small data */
Paolo Abenic67f5db2016-03-17 15:44:00 +0100280 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000281 if (unlikely(!skb))
282 return NULL;
283
284 hdr = skb_vnet_hdr(skb);
285
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300286 hdr_len = vi->hdr_len;
287 if (vi->mergeable_rx_bufs)
288 hdr_padded_len = sizeof *hdr;
289 else
Michael Dalton2613af02013-10-28 15:44:18 -0700290 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000291
292 memcpy(hdr, p, hdr_len);
293
294 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700295 offset += hdr_padded_len;
296 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000297
298 copy = len;
299 if (copy > skb_tailroom(skb))
300 copy = skb_tailroom(skb);
301 memcpy(skb_put(skb, copy), p, copy);
302
303 len -= copy;
304 offset += copy;
305
Michael Dalton2613af02013-10-28 15:44:18 -0700306 if (vi->mergeable_rx_bufs) {
307 if (len)
308 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
309 else
310 put_page(page);
311 return skb;
312 }
313
Sasha Levine878d782011-09-28 04:40:54 +0000314 /*
315 * Verify that we can indeed put this data into a skb.
316 * This is here to handle cases when the device erroneously
317 * tries to receive more than is possible. This is usually
318 * the case of a broken device.
319 */
320 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000321 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000322 dev_kfree_skb(skb);
323 return NULL;
324 }
Michael Dalton2613af02013-10-28 15:44:18 -0700325 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000326 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700327 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
328 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
329 frag_size, truesize);
330 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000331 page = (struct page *)page->private;
332 offset = 0;
333 }
334
335 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000336 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000337
338 return skb;
339}
340
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100341static bool virtnet_xdp_xmit(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800342 struct receive_queue *rq,
343 struct send_queue *sq,
Jason Wangbb91acc2016-12-23 22:37:32 +0800344 struct xdp_buff *xdp,
345 void *data)
John Fastabend56434a02016-12-15 12:14:13 -0800346{
John Fastabend56434a02016-12-15 12:14:13 -0800347 struct virtio_net_hdr_mrg_rxbuf *hdr;
348 unsigned int num_sg, len;
349 void *xdp_sent;
350 int err;
351
352 /* Free up any pending old buffers before queueing new ones. */
353 while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Jason Wangbb91acc2016-12-23 22:37:32 +0800354 if (vi->mergeable_rx_bufs) {
355 struct page *sent_page = virt_to_head_page(xdp_sent);
356
357 put_page(sent_page);
358 } else { /* small buffer */
359 struct sk_buff *skb = xdp_sent;
360
361 kfree_skb(skb);
362 }
John Fastabend56434a02016-12-15 12:14:13 -0800363 }
364
Jason Wangbb91acc2016-12-23 22:37:32 +0800365 if (vi->mergeable_rx_bufs) {
366 /* Zero header and leave csum up to XDP layers */
367 hdr = xdp->data;
368 memset(hdr, 0, vi->hdr_len);
John Fastabend56434a02016-12-15 12:14:13 -0800369
Jason Wangbb91acc2016-12-23 22:37:32 +0800370 num_sg = 1;
371 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
372 } else { /* small buffer */
373 struct sk_buff *skb = data;
374
375 /* Zero header and leave csum up to XDP layers */
376 hdr = skb_vnet_hdr(skb);
377 memset(hdr, 0, vi->hdr_len);
378
379 num_sg = 2;
380 sg_init_table(sq->sg, 2);
381 sg_set_buf(sq->sg, hdr, vi->hdr_len);
382 skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
383 }
John Fastabend56434a02016-12-15 12:14:13 -0800384 err = virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
Jason Wangbb91acc2016-12-23 22:37:32 +0800385 data, GFP_ATOMIC);
John Fastabend56434a02016-12-15 12:14:13 -0800386 if (unlikely(err)) {
Jason Wangbb91acc2016-12-23 22:37:32 +0800387 if (vi->mergeable_rx_bufs) {
388 struct page *page = virt_to_head_page(xdp->data);
389
390 put_page(page);
391 } else /* small buffer */
392 kfree_skb(data);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100393 /* On error abort to avoid unnecessary kick */
394 return false;
John Fastabend56434a02016-12-15 12:14:13 -0800395 }
396
397 virtqueue_kick(sq->vq);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100398 return true;
John Fastabend56434a02016-12-15 12:14:13 -0800399}
400
John Fastabendf600b692016-12-15 12:13:24 -0800401static u32 do_xdp_prog(struct virtnet_info *vi,
John Fastabend56434a02016-12-15 12:14:13 -0800402 struct receive_queue *rq,
John Fastabendf600b692016-12-15 12:13:24 -0800403 struct bpf_prog *xdp_prog,
Jason Wangbb91acc2016-12-23 22:37:32 +0800404 void *data, int len)
John Fastabendf600b692016-12-15 12:13:24 -0800405{
406 int hdr_padded_len;
407 struct xdp_buff xdp;
Jason Wangbb91acc2016-12-23 22:37:32 +0800408 void *buf;
John Fastabend56434a02016-12-15 12:14:13 -0800409 unsigned int qp;
John Fastabendf600b692016-12-15 12:13:24 -0800410 u32 act;
John Fastabendf600b692016-12-15 12:13:24 -0800411
Jason Wangbb91acc2016-12-23 22:37:32 +0800412 if (vi->mergeable_rx_bufs) {
John Fastabendf600b692016-12-15 12:13:24 -0800413 hdr_padded_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Jason Wangbb91acc2016-12-23 22:37:32 +0800414 xdp.data = data + hdr_padded_len;
415 xdp.data_end = xdp.data + (len - vi->hdr_len);
416 buf = data;
417 } else { /* small buffers */
418 struct sk_buff *skb = data;
John Fastabendf600b692016-12-15 12:13:24 -0800419
Jason Wangbb91acc2016-12-23 22:37:32 +0800420 xdp.data = skb->data;
421 xdp.data_end = xdp.data + len;
422 buf = skb->data;
423 }
John Fastabendf600b692016-12-15 12:13:24 -0800424
425 act = bpf_prog_run_xdp(xdp_prog, &xdp);
426 switch (act) {
427 case XDP_PASS:
428 return XDP_PASS;
John Fastabend56434a02016-12-15 12:14:13 -0800429 case XDP_TX:
430 qp = vi->curr_queue_pairs -
431 vi->xdp_queue_pairs +
432 smp_processor_id();
Jason Wangbb91acc2016-12-23 22:37:32 +0800433 xdp.data = buf;
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100434 if (unlikely(!virtnet_xdp_xmit(vi, rq, &vi->sq[qp], &xdp,
435 data)))
436 trace_xdp_exception(vi->dev, xdp_prog, act);
John Fastabend56434a02016-12-15 12:14:13 -0800437 return XDP_TX;
John Fastabendf600b692016-12-15 12:13:24 -0800438 default:
439 bpf_warn_invalid_xdp_action(act);
John Fastabendf600b692016-12-15 12:13:24 -0800440 case XDP_ABORTED:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100441 trace_xdp_exception(vi->dev, xdp_prog, act);
John Fastabendf600b692016-12-15 12:13:24 -0800442 case XDP_DROP:
443 return XDP_DROP;
444 }
445}
446
Jason Wangbb91acc2016-12-23 22:37:32 +0800447static struct sk_buff *receive_small(struct net_device *dev,
448 struct virtnet_info *vi,
449 struct receive_queue *rq,
450 void *buf, unsigned int len)
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200451{
452 struct sk_buff * skb = buf;
Jason Wangbb91acc2016-12-23 22:37:32 +0800453 struct bpf_prog *xdp_prog;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200454
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300455 len -= vi->hdr_len;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200456 skb_trim(skb, len);
457
Jason Wangbb91acc2016-12-23 22:37:32 +0800458 rcu_read_lock();
459 xdp_prog = rcu_dereference(rq->xdp_prog);
460 if (xdp_prog) {
461 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
462 u32 act;
463
464 if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
465 goto err_xdp;
466 act = do_xdp_prog(vi, rq, xdp_prog, skb, len);
467 switch (act) {
468 case XDP_PASS:
469 break;
470 case XDP_TX:
471 rcu_read_unlock();
472 goto xdp_xmit;
473 case XDP_DROP:
474 default:
475 goto err_xdp;
476 }
477 }
478 rcu_read_unlock();
479
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200480 return skb;
Jason Wangbb91acc2016-12-23 22:37:32 +0800481
482err_xdp:
483 rcu_read_unlock();
484 dev->stats.rx_dropped++;
485 kfree_skb(skb);
486xdp_xmit:
487 return NULL;
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200488}
489
490static struct sk_buff *receive_big(struct net_device *dev,
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300491 struct virtnet_info *vi,
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200492 struct receive_queue *rq,
493 void *buf,
494 unsigned int len)
495{
496 struct page *page = buf;
Jason Wangc47a43d2016-12-23 22:37:31 +0800497 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200498
499 if (unlikely(!skb))
500 goto err;
501
502 return skb;
503
504err:
505 dev->stats.rx_dropped++;
506 give_pages(rq, page);
507 return NULL;
508}
509
John Fastabend72979a62016-12-15 12:14:36 -0800510/* The conditions to enable XDP should preclude the underlying device from
511 * sending packets across multiple buffers (num_buf > 1). However per spec
512 * it does not appear to be illegal to do so but rather just against convention.
513 * So in order to avoid making a system unresponsive the packets are pushed
514 * into a page and the XDP program is run. This will be extremely slow and we
515 * push a warning to the user to fix this as soon as possible. Fixing this may
516 * require resolving the underlying hardware to determine why multiple buffers
517 * are being received or simply loading the XDP program in the ingress stack
518 * after the skb is built because there is no advantage to running it here
519 * anymore.
520 */
521static struct page *xdp_linearize_page(struct receive_queue *rq,
Jason Wang56a86f82016-12-23 22:37:26 +0800522 u16 *num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800523 struct page *p,
524 int offset,
525 unsigned int *len)
526{
527 struct page *page = alloc_page(GFP_ATOMIC);
528 unsigned int page_off = 0;
529
530 if (!page)
531 return NULL;
532
533 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
534 page_off += *len;
535
Jason Wang56a86f82016-12-23 22:37:26 +0800536 while (--*num_buf) {
John Fastabend72979a62016-12-15 12:14:36 -0800537 unsigned int buflen;
538 unsigned long ctx;
539 void *buf;
540 int off;
541
542 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen);
543 if (unlikely(!ctx))
544 goto err_buf;
545
John Fastabend72979a62016-12-15 12:14:36 -0800546 buf = mergeable_ctx_to_buf_address(ctx);
547 p = virt_to_head_page(buf);
548 off = buf - page_address(p);
549
Jason Wang56a86f82016-12-23 22:37:26 +0800550 /* guard against a misconfigured or uncooperative backend that
551 * is sending packet larger than the MTU.
552 */
553 if ((page_off + buflen) > PAGE_SIZE) {
554 put_page(p);
555 goto err_buf;
556 }
557
John Fastabend72979a62016-12-15 12:14:36 -0800558 memcpy(page_address(page) + page_off,
559 page_address(p) + off, buflen);
560 page_off += buflen;
Jason Wang56a86f82016-12-23 22:37:26 +0800561 put_page(p);
John Fastabend72979a62016-12-15 12:14:36 -0800562 }
563
564 *len = page_off;
565 return page;
566err_buf:
567 __free_pages(page, 0);
568 return NULL;
569}
570
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200571static struct sk_buff *receive_mergeable(struct net_device *dev,
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200572 struct virtnet_info *vi,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200573 struct receive_queue *rq,
Michael Daltonab7db912014-01-16 22:23:27 -0800574 unsigned long ctx,
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200575 unsigned int len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000576{
Michael Daltonab7db912014-01-16 22:23:27 -0800577 void *buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300578 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
579 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200580 struct page *page = virt_to_head_page(buf);
581 int offset = buf - page_address(page);
John Fastabendf600b692016-12-15 12:13:24 -0800582 struct sk_buff *head_skb, *curr_skb;
583 struct bpf_prog *xdp_prog;
584 unsigned int truesize;
Michael Daltonab7db912014-01-16 22:23:27 -0800585
John Fastabend56434a02016-12-15 12:14:13 -0800586 head_skb = NULL;
587
John Fastabendf600b692016-12-15 12:13:24 -0800588 rcu_read_lock();
589 xdp_prog = rcu_dereference(rq->xdp_prog);
590 if (xdp_prog) {
John Fastabend72979a62016-12-15 12:14:36 -0800591 struct page *xdp_page;
John Fastabendf600b692016-12-15 12:13:24 -0800592 u32 act;
593
Jason Wang73b62bd2016-12-23 22:37:24 +0800594 /* This happens when rx buffer size is underestimated */
John Fastabendf600b692016-12-15 12:13:24 -0800595 if (unlikely(num_buf > 1)) {
John Fastabend72979a62016-12-15 12:14:36 -0800596 /* linearize data for XDP */
Jason Wang56a86f82016-12-23 22:37:26 +0800597 xdp_page = xdp_linearize_page(rq, &num_buf,
John Fastabend72979a62016-12-15 12:14:36 -0800598 page, offset, &len);
599 if (!xdp_page)
600 goto err_xdp;
601 offset = 0;
602 } else {
603 xdp_page = page;
John Fastabendf600b692016-12-15 12:13:24 -0800604 }
605
606 /* Transient failure which in theory could occur if
607 * in-flight packets from before XDP was enabled reach
608 * the receive path after XDP is loaded. In practice I
609 * was not able to create this condition.
610 */
Jason Wangb00f70b2016-12-23 22:37:28 +0800611 if (unlikely(hdr->hdr.gso_type))
John Fastabendf600b692016-12-15 12:13:24 -0800612 goto err_xdp;
613
Jason Wangbb91acc2016-12-23 22:37:32 +0800614 act = do_xdp_prog(vi, rq, xdp_prog,
615 page_address(xdp_page) + offset, len);
John Fastabend56434a02016-12-15 12:14:13 -0800616 switch (act) {
617 case XDP_PASS:
Jason Wang1830f892016-12-23 22:37:27 +0800618 /* We can only create skb based on xdp_page. */
619 if (unlikely(xdp_page != page)) {
620 rcu_read_unlock();
621 put_page(page);
622 head_skb = page_to_skb(vi, rq, xdp_page,
623 0, len, PAGE_SIZE);
Jason Wang5c334742016-12-23 22:37:29 +0800624 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
Jason Wang1830f892016-12-23 22:37:27 +0800625 return head_skb;
626 }
John Fastabend56434a02016-12-15 12:14:13 -0800627 break;
628 case XDP_TX:
Jason Wang5c334742016-12-23 22:37:29 +0800629 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabend72979a62016-12-15 12:14:36 -0800630 if (unlikely(xdp_page != page))
631 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800632 rcu_read_unlock();
633 goto xdp_xmit;
634 case XDP_DROP:
635 default:
John Fastabend72979a62016-12-15 12:14:36 -0800636 if (unlikely(xdp_page != page))
637 __free_pages(xdp_page, 0);
Jason Wang5c334742016-12-23 22:37:29 +0800638 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
John Fastabendf600b692016-12-15 12:13:24 -0800639 goto err_xdp;
John Fastabend56434a02016-12-15 12:14:13 -0800640 }
John Fastabendf600b692016-12-15 12:13:24 -0800641 }
642 rcu_read_unlock();
643
644 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
645 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
646 curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000647
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200648 if (unlikely(!curr_skb))
649 goto err_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000650 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200651 int num_skb_frags;
652
Michael Daltonab7db912014-01-16 22:23:27 -0800653 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
654 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200655 pr_debug("%s: rx error: %d buffers out of %d missing\n",
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200656 dev->name, num_buf,
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300657 virtio16_to_cpu(vi->vdev,
658 hdr->num_buffers));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200659 dev->stats.rx_length_errors++;
660 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000661 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200662
Michael Daltonab7db912014-01-16 22:23:27 -0800663 buf = mergeable_ctx_to_buf_address(ctx);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200664 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200665
666 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700667 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
668 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200669
670 if (unlikely(!nskb))
671 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700672 if (curr_skb == head_skb)
673 skb_shinfo(curr_skb)->frag_list = nskb;
674 else
675 curr_skb->next = nskb;
676 curr_skb = nskb;
677 head_skb->truesize += nskb->truesize;
678 num_skb_frags = 0;
679 }
Michael Daltonab7db912014-01-16 22:23:27 -0800680 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
Michael Dalton2613af02013-10-28 15:44:18 -0700681 if (curr_skb != head_skb) {
682 head_skb->data_len += len;
683 head_skb->len += len;
Michael Daltonfb518792014-01-16 22:23:26 -0800684 head_skb->truesize += truesize;
Michael Dalton2613af02013-10-28 15:44:18 -0700685 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200686 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800687 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
688 put_page(page);
689 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Daltonfb518792014-01-16 22:23:26 -0800690 len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800691 } else {
692 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Daltonfb518792014-01-16 22:23:26 -0800693 offset, len, truesize);
Jason Wangba275242013-11-01 14:07:48 +0800694 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200695 }
696
Johannes Berg5377d7582015-08-19 09:48:40 +0200697 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200698 return head_skb;
699
John Fastabendf600b692016-12-15 12:13:24 -0800700err_xdp:
701 rcu_read_unlock();
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200702err_skb:
703 put_page(page);
704 while (--num_buf) {
Michael Daltonab7db912014-01-16 22:23:27 -0800705 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
706 if (unlikely(!ctx)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200707 pr_debug("%s: rx error: %d buffers missing\n",
708 dev->name, num_buf);
709 dev->stats.rx_length_errors++;
710 break;
711 }
Michael Daltonab7db912014-01-16 22:23:27 -0800712 page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200713 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000714 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200715err_buf:
716 dev->stats.rx_dropped++;
717 dev_kfree_skb(head_skb);
John Fastabend56434a02016-12-15 12:14:13 -0800718xdp_xmit:
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200719 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000720}
721
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300722static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
723 void *buf, unsigned int len)
Rusty Russell296f96f2007-10-22 11:03:37 +1000724{
Jason Wange9d74172012-12-07 07:04:55 +0000725 struct net_device *dev = vi->dev;
Eric Dumazet58472a72012-02-13 06:53:41 +0000726 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000727 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300728 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000729
Michael S. Tsirkinbcff3162014-10-24 00:22:11 +0300730 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000731 pr_debug("%s: short packet %i\n", dev->name, len);
732 dev->stats.rx_length_errors++;
Michael Daltonab7db912014-01-16 22:23:27 -0800733 if (vi->mergeable_rx_bufs) {
734 unsigned long ctx = (unsigned long)buf;
735 void *base = mergeable_ctx_to_buf_address(ctx);
736 put_page(virt_to_head_page(base));
737 } else if (vi->big_packets) {
Michael Dalton98bfd232013-12-05 13:14:05 -0800738 give_pages(rq, buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800739 } else {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000740 dev_kfree_skb(buf);
Michael Daltonab7db912014-01-16 22:23:27 -0800741 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000742 return;
Rusty Russell296f96f2007-10-22 11:03:37 +1000743 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000744
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200745 if (vi->mergeable_rx_bufs)
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +0200746 skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200747 else if (vi->big_packets)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300748 skb = receive_big(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200749 else
Jason Wangbb91acc2016-12-23 22:37:32 +0800750 skb = receive_small(dev, vi, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200751
752 if (unlikely(!skb))
753 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800754
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000755 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000756
Eric Dumazet83a27052012-06-05 22:35:24 +0000757 u64_stats_update_begin(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000758 stats->rx_bytes += skb->len;
759 stats->rx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000760 u64_stats_update_end(&stats->rx_syncp);
Rusty Russell296f96f2007-10-22 11:03:37 +1000761
Mike Rapoporte858fae2016-06-08 16:09:21 +0300762 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
Jason Wang10a8d942011-06-10 00:56:17 +0000763 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000764
Mike Rapoporte858fae2016-06-08 16:09:21 +0300765 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
766 virtio_is_little_endian(vi->vdev))) {
767 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
768 dev->name, hdr->hdr.gso_type,
769 hdr->hdr.gso_size);
770 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000771 }
772
Mike Rapoportd1dc06d2016-06-14 08:29:38 +0300773 skb->protocol = eth_type_trans(skb, dev);
774 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
775 ntohs(skb->protocol), skb->len, skb->pkt_type);
776
Eric Dumazet0fbd0502015-07-31 18:25:17 +0200777 napi_gro_receive(&rq->napi, skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000778 return;
779
780frame_err:
781 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000782 dev_kfree_skb(skb);
783}
784
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300785static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
786 gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000787{
788 struct sk_buff *skb;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300789 struct virtio_net_hdr_mrg_rxbuf *hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000790 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000791
Michael Dalton5061de32013-11-14 10:41:04 -0800792 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000793 if (unlikely(!skb))
794 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800795
Michael Dalton5061de32013-11-14 10:41:04 -0800796 skb_put(skb, GOOD_PACKET_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000797
798 hdr = skb_vnet_hdr(skb);
Jason Wang547c8902015-08-27 14:53:06 +0800799 sg_init_table(rq->sg, 2);
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300800 sg_set_buf(rq->sg, hdr, vi->hdr_len);
Jason Wange9d74172012-12-07 07:04:55 +0000801 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000802
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030803 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000804 if (err < 0)
805 dev_kfree_skb(skb);
806
807 return err;
808}
809
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300810static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
811 gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000812{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000813 struct page *first, *list = NULL;
814 char *p;
815 int i, err, offset;
816
Rusty Russella5835442014-09-11 10:17:36 +0930817 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
818
Jason Wange9d74172012-12-07 07:04:55 +0000819 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000820 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000821 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000822 if (!first) {
823 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000824 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000825 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700826 }
Jason Wange9d74172012-12-07 07:04:55 +0000827 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000828
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000829 /* chain new page in list head to match sg */
830 first->private = (unsigned long)list;
831 list = first;
832 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800833
Jason Wange9d74172012-12-07 07:04:55 +0000834 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000835 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000836 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000837 return -ENOMEM;
838 }
839 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800840
Jason Wange9d74172012-12-07 07:04:55 +0000841 /* rq->sg[0], rq->sg[1] share the same page */
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300842 /* a separated rq->sg[0] for header - required in case !any_header_sg */
843 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
Herbert Xu97402b92008-04-18 11:24:27 +0800844
Jason Wange9d74172012-12-07 07:04:55 +0000845 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000846 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000847 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800848
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000849 /* chain first in list head */
850 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030851 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
852 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000853 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000854 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800855
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000856 return err;
857}
Herbert Xu97402b92008-04-18 11:24:27 +0800858
Johannes Berg5377d7582015-08-19 09:48:40 +0200859static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000860{
Michael Daltonab7db912014-01-16 22:23:27 -0800861 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
Michael Daltonfbf28d72014-01-16 22:23:30 -0800862 unsigned int len;
863
Johannes Berg5377d7582015-08-19 09:48:40 +0200864 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
Michael Daltonfbf28d72014-01-16 22:23:30 -0800865 GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
866 return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
867}
868
869static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
870{
Michael Daltonfb518792014-01-16 22:23:26 -0800871 struct page_frag *alloc_frag = &rq->alloc_frag;
872 char *buf;
Michael Daltonab7db912014-01-16 22:23:27 -0800873 unsigned long ctx;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000874 int err;
Michael Daltonfb518792014-01-16 22:23:26 -0800875 unsigned int len, hole;
Rusty Russell296f96f2007-10-22 11:03:37 +1000876
Michael Daltonfbf28d72014-01-16 22:23:30 -0800877 len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
Michael Daltonab7db912014-01-16 22:23:27 -0800878 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000879 return -ENOMEM;
Michael Daltonab7db912014-01-16 22:23:27 -0800880
Michael Daltonfb518792014-01-16 22:23:26 -0800881 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
Michael Daltonab7db912014-01-16 22:23:27 -0800882 ctx = mergeable_buf_to_ctx(buf, len);
Michael Daltonfb518792014-01-16 22:23:26 -0800883 get_page(alloc_frag->page);
Michael Daltonfb518792014-01-16 22:23:26 -0800884 alloc_frag->offset += len;
885 hole = alloc_frag->size - alloc_frag->offset;
Michael Daltonab7db912014-01-16 22:23:27 -0800886 if (hole < len) {
887 /* To avoid internal fragmentation, if there is very likely not
888 * enough space for another buffer, add the remaining space to
889 * the current buffer. This extra space is not included in
890 * the truesize stored in ctx.
891 */
Michael Daltonfb518792014-01-16 22:23:26 -0800892 len += hole;
893 alloc_frag->offset += hole;
894 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000895
Michael Daltonfb518792014-01-16 22:23:26 -0800896 sg_init_one(rq->sg, buf, len);
Michael Daltonab7db912014-01-16 22:23:27 -0800897 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000898 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -0700899 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000900
901 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000902}
903
Rusty Russellb2baed62011-12-29 00:42:38 +0000904/*
905 * Returns false if we couldn't fill entirely (OOM).
906 *
907 * Normally run in the receive path, but can also be run from ndo_open
908 * before we're receiving packets, or from refill_work which is
909 * careful to disable receiving (using napi_disable).
910 */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300911static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
912 gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800913{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800914 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000915 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800916
Michael Daltonfb518792014-01-16 22:23:26 -0800917 gfp |= __GFP_COLD;
Amit Shah0aea51c2009-08-26 14:58:28 +0530918 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000919 if (vi->mergeable_rx_bufs)
Jason Wange9d74172012-12-07 07:04:55 +0000920 err = add_recvbuf_mergeable(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000921 else if (vi->big_packets)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +0300922 err = add_recvbuf_big(vi, rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000923 else
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300924 err = add_recvbuf_small(vi, rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800925
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000926 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030927 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800928 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800929 } while (rq->vq->num_free);
Jason Wang681daee22014-03-26 13:03:00 +0800930 virtqueue_kick(rq->vq);
Rusty Russell3161e452009-08-26 12:22:32 -0700931 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800932}
933
Rusty Russell18445c42008-02-04 23:49:57 -0500934static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000935{
936 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +0000937 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +0000938
Rusty Russell18445c42008-02-04 23:49:57 -0500939 /* Schedule NAPI, Suppress further interrupts if successful. */
Jason Wange9d74172012-12-07 07:04:55 +0000940 if (napi_schedule_prep(&rq->napi)) {
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300941 virtqueue_disable_cb(rvq);
Jason Wange9d74172012-12-07 07:04:55 +0000942 __napi_schedule(&rq->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500943 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000944}
945
Jason Wange9d74172012-12-07 07:04:55 +0000946static void virtnet_napi_enable(struct receive_queue *rq)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800947{
Jason Wange9d74172012-12-07 07:04:55 +0000948 napi_enable(&rq->napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800949
950 /* If all buffers were filled by other side before we napi_enabled, we
951 * won't get another interrupt, so process any outstanding packets
952 * now. virtnet_poll wants re-enable the queue, so we disable here.
953 * We synchronize against interrupts via NAPI_STATE_SCHED */
Jason Wange9d74172012-12-07 07:04:55 +0000954 if (napi_schedule_prep(&rq->napi)) {
955 virtqueue_disable_cb(rq->vq);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300956 local_bh_disable();
Jason Wange9d74172012-12-07 07:04:55 +0000957 __napi_schedule(&rq->napi);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300958 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800959 }
960}
961
Rusty Russell3161e452009-08-26 12:22:32 -0700962static void refill_work(struct work_struct *work)
963{
Jason Wange9d74172012-12-07 07:04:55 +0000964 struct virtnet_info *vi =
965 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -0700966 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +0000967 int i;
Rusty Russell3161e452009-08-26 12:22:32 -0700968
Sasha Levin55257d72013-04-29 12:00:08 +0930969 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +0000970 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700971
Jason Wang986a4f42012-12-07 07:04:56 +0000972 napi_disable(&rq->napi);
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300973 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
Jason Wang986a4f42012-12-07 07:04:56 +0000974 virtnet_napi_enable(rq);
975
976 /* In theory, this can happen: if we don't get any buffers in
977 * we will *never* try to fill again.
978 */
979 if (still_empty)
980 schedule_delayed_work(&vi->refill, HZ/2);
981 }
Rusty Russell3161e452009-08-26 12:22:32 -0700982}
983
Jason Wang2ffa7592014-07-23 16:33:54 +0800984static int virtnet_receive(struct receive_queue *rq, int budget)
Rusty Russell296f96f2007-10-22 11:03:37 +1000985{
Jason Wange9d74172012-12-07 07:04:55 +0000986 struct virtnet_info *vi = rq->vq->vdev->priv;
Jason Wang2ffa7592014-07-23 16:33:54 +0800987 unsigned int len, received = 0;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000988 void *buf;
Rusty Russell296f96f2007-10-22 11:03:37 +1000989
Rusty Russell296f96f2007-10-22 11:03:37 +1000990 while (received < budget &&
Jason Wange9d74172012-12-07 07:04:55 +0000991 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300992 receive_buf(vi, rq, buf, len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000993 received++;
994 }
995
Jason Wangbe121f42014-01-16 14:45:24 +0800996 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Michael S. Tsirkin946fa562014-10-24 00:12:10 +0300997 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700998 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -0700999 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001000
Jason Wang2ffa7592014-07-23 16:33:54 +08001001 return received;
1002}
1003
1004static int virtnet_poll(struct napi_struct *napi, int budget)
1005{
1006 struct receive_queue *rq =
1007 container_of(napi, struct receive_queue, napi);
Li RongQingfaadb052015-03-26 15:39:45 +08001008 unsigned int r, received;
Jason Wang2ffa7592014-07-23 16:33:54 +08001009
Li RongQingfaadb052015-03-26 15:39:45 +08001010 received = virtnet_receive(rq, budget);
Jason Wang2ffa7592014-07-23 16:33:54 +08001011
Rusty Russell8329d982007-11-19 11:20:43 -05001012 /* Out of packets? */
1013 if (received < budget) {
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +03001014 r = virtqueue_enable_cb_prepare(rq->vq);
Eric Dumazet4d6308aa2017-02-04 07:49:21 -08001015 if (napi_complete_done(napi, received)) {
1016 if (unlikely(virtqueue_poll(rq->vq, r)) &&
1017 napi_schedule_prep(napi)) {
1018 virtqueue_disable_cb(rq->vq);
1019 __napi_schedule(napi);
1020 }
Christian Borntraeger4265f162008-03-14 14:17:05 +01001021 }
Rusty Russell296f96f2007-10-22 11:03:37 +10001022 }
1023
1024 return received;
1025}
1026
Jason Wang986a4f42012-12-07 07:04:56 +00001027static int virtnet_open(struct net_device *dev)
1028{
1029 struct virtnet_info *vi = netdev_priv(dev);
1030 int i;
1031
Jason Wange4166622013-05-21 20:03:58 +00001032 for (i = 0; i < vi->max_queue_pairs; i++) {
1033 if (i < vi->curr_queue_pairs)
1034 /* Make sure we have some buffers: if oom use wq. */
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03001035 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wange4166622013-05-21 20:03:58 +00001036 schedule_delayed_work(&vi->refill, 0);
Jason Wang986a4f42012-12-07 07:04:56 +00001037 virtnet_napi_enable(&vi->rq[i]);
1038 }
1039
1040 return 0;
1041}
1042
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001043static void free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +10001044{
1045 struct sk_buff *skb;
Michael S. Tsirkin6ee57bc2012-10-16 23:56:14 +10301046 unsigned int len;
Jason Wange9d74172012-12-07 07:04:55 +00001047 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +00001048 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10001049
Jason Wange9d74172012-12-07 07:04:55 +00001050 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001051 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001052
Eric Dumazet83a27052012-06-05 22:35:24 +00001053 u64_stats_update_begin(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001054 stats->tx_bytes += skb->len;
1055 stats->tx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +00001056 u64_stats_update_end(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001057
Eric Dumazeted79bab2009-10-14 14:36:43 +00001058 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001059 }
1060}
1061
Jason Wange9d74172012-12-07 07:04:55 +00001062static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +10001063{
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001064 struct virtio_net_hdr_mrg_rxbuf *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +10001065 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +00001066 struct virtnet_info *vi = sq->vq->vdev->priv;
Michael S. Tsirkin7bedc7d2012-10-16 23:56:14 +10301067 unsigned num_sg;
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001068 unsigned hdr_len = vi->hdr_len;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301069 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +10001070
Johannes Berge1749612008-10-27 15:59:26 -07001071 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301072
1073 can_push = vi->any_header_sg &&
1074 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1075 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1076 /* Even if we can, don't push here yet as this would skew
1077 * csum_start offset below. */
1078 if (can_push)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001079 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301080 else
1081 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +10001082
Mike Rapoporte858fae2016-06-08 16:09:21 +03001083 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
Jason Wang6391a442017-01-20 14:32:42 +08001084 virtio_is_little_endian(vi->vdev), false))
Mike Rapoporte858fae2016-06-08 16:09:21 +03001085 BUG();
Rusty Russell296f96f2007-10-22 11:03:37 +10001086
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001087 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03001088 hdr->num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001089
Jason Wang547c8902015-08-27 14:53:06 +08001090 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301091 if (can_push) {
1092 __skb_push(skb, hdr_len);
1093 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1094 /* Pull header back to avoid skew in tx bytes calculations. */
1095 __skb_pull(skb, hdr_len);
1096 } else {
1097 sg_set_buf(sq->sg, hdr, hdr_len);
1098 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1099 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +10301100 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +10001101}
1102
Stephen Hemminger424efe92009-08-31 19:50:51 +00001103static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -05001104{
1105 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001106 int qnum = skb_get_queue_mapping(skb);
1107 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301108 int err;
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001109 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1110 bool kick = !skb->xmit_more;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001111
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001112 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +00001113 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -05001114
Jacob Keller074c3582014-06-25 02:37:13 +00001115 /* timestamp packet in software */
1116 skb_tx_timestamp(skb);
1117
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001118 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001119 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -06001120
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301121 /* This should not happen! */
Jason Wang681daee22014-03-26 13:03:00 +08001122 if (unlikely(err)) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +10301123 dev->stats.tx_fifo_errors++;
1124 if (net_ratelimit())
1125 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001126 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001127 dev->stats.tx_dropped++;
Eric W. Biederman85e94522014-03-15 18:43:33 -07001128 dev_kfree_skb_any(skb);
Rusty Russell58eba97d2010-07-02 16:34:01 +00001129 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -05001130 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -07001131
Rusty Russell48925e32009-09-24 09:59:20 -06001132 /* Don't wait up for transmitted skbs to be freed. */
1133 skb_orphan(skb);
1134 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -05001135
Michael S. Tsirkin60302ff2015-04-02 13:05:47 +02001136 /* If running out of space, stop queue to avoid getting packets that we
1137 * are then unable to transmit.
1138 * An alternative would be to force queuing layer to requeue the skb by
1139 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1140 * returned in a normal path of operation: it means that driver is not
1141 * maintaining the TX queue stop/start state properly, and causes
1142 * the stack to do a non-trivial amount of useless work.
1143 * Since most packets only take 1 or 2 ring slots, stopping the queue
1144 * early means 16 slots are typically wasted.
stephen hemmingerd631b942015-03-24 16:22:07 -07001145 */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001146 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001147 netif_stop_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001148 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -06001149 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -08001150 free_old_xmit_skbs(sq);
1151 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +00001152 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +00001153 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -06001154 }
1155 }
Rusty Russell99ffc692008-05-02 21:50:46 -05001156 }
Rusty Russell48925e32009-09-24 09:59:20 -06001157
Michael S. Tsirkin4b7fd2e62014-10-15 16:23:28 +03001158 if (kick || netif_xmit_stopped(txq))
David S. Miller0b725a22014-08-25 15:51:53 -07001159 virtqueue_kick(sq->vq);
1160
Rusty Russell48925e32009-09-24 09:59:20 -06001161 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +10001162}
1163
Amos Kong40cbfc32013-01-21 01:17:21 +00001164/*
1165 * Send command via the control virtqueue and check status. Commands
1166 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -08001167 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +00001168 */
1169static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001170 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +00001171{
Rusty Russellf7bc9592013-03-20 15:44:28 +10301172 struct scatterlist *sgs[4], hdr, stat;
stephen hemmingerd24bae32013-12-09 16:17:40 -08001173 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +00001174
1175 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +10301176 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +00001177
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001178 vi->ctrl_status = ~0;
1179 vi->ctrl_hdr.class = class;
1180 vi->ctrl_hdr.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +10301181 /* Add header */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001182 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
Rusty Russellf7bc9592013-03-20 15:44:28 +10301183 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +00001184
Rusty Russellf7bc9592013-03-20 15:44:28 +10301185 if (out)
1186 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +00001187
Rusty Russellf7bc9592013-03-20 15:44:28 +10301188 /* Add return status. */
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001189 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
stephen hemmingerd24bae32013-12-09 16:17:40 -08001190 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +00001191
stephen hemmingerd24bae32013-12-09 16:17:40 -08001192 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
Rusty Russella7c58142014-03-13 11:23:39 +10301193 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
Amos Kong40cbfc32013-01-21 01:17:21 +00001194
Heinz Graalfs67975902013-10-29 09:40:02 +10301195 if (unlikely(!virtqueue_kick(vi->cvq)))
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001196 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001197
1198 /* Spin for a response, the kick causes an ioport write, trapping
1199 * into the hypervisor, so the request should be handled immediately.
1200 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +10301201 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1202 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +00001203 cpu_relax();
1204
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001205 return vi->ctrl_status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +00001206}
1207
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001208static int virtnet_set_mac_address(struct net_device *dev, void *p)
1209{
1210 struct virtnet_info *vi = netdev_priv(dev);
1211 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001212 int ret;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001213 struct sockaddr *addr;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001214 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001215
Shyam Saini801822d2016-12-24 00:44:58 +05301216 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001217 if (!addr)
1218 return -ENOMEM;
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001219
1220 ret = eth_prepare_mac_addr_change(dev, addr);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001221 if (ret)
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001222 goto out;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001223
Amos Kong7e58d5a2013-01-21 01:17:23 +00001224 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1225 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1226 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001227 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +00001228 dev_warn(&vdev->dev,
1229 "Failed to set mac address by vq command.\n");
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001230 ret = -EINVAL;
1231 goto out;
Amos Kong7e58d5a2013-01-21 01:17:23 +00001232 }
Michael S. Tsirkin7e93a022014-11-26 15:58:28 +02001233 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1234 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Rusty Russell855e0c52013-10-14 18:11:51 +10301235 unsigned int i;
1236
1237 /* Naturally, this has an atomicity problem. */
1238 for (i = 0; i < dev->addr_len; i++)
1239 virtio_cwrite8(vdev,
1240 offsetof(struct virtio_net_config, mac) +
1241 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +00001242 }
1243
1244 eth_commit_mac_addr_change(dev, p);
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001245 ret = 0;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001246
Andy Lutomirskie37e2ff2016-12-05 18:10:58 -08001247out:
1248 kfree(addr);
1249 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001250}
1251
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001252static void virtnet_stats(struct net_device *dev,
1253 struct rtnl_link_stats64 *tot)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001254{
1255 struct virtnet_info *vi = netdev_priv(dev);
1256 int cpu;
1257 unsigned int start;
1258
1259 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +00001260 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001261 u64 tpackets, tbytes, rpackets, rbytes;
1262
1263 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001264 start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001265 tpackets = stats->tx_packets;
1266 tbytes = stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001267 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +00001268
1269 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001270 start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001271 rpackets = stats->rx_packets;
1272 rbytes = stats->rx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001273 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001274
1275 tot->rx_packets += rpackets;
1276 tot->tx_packets += tpackets;
1277 tot->rx_bytes += rbytes;
1278 tot->tx_bytes += tbytes;
1279 }
1280
1281 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +00001282 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001283 tot->rx_dropped = dev->stats.rx_dropped;
1284 tot->rx_length_errors = dev->stats.rx_length_errors;
1285 tot->rx_frame_errors = dev->stats.rx_frame_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001286}
1287
Amit Shahda74e892008-02-29 16:24:50 +05301288#ifdef CONFIG_NET_POLL_CONTROLLER
1289static void virtnet_netpoll(struct net_device *dev)
1290{
1291 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001292 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301293
Jason Wang986a4f42012-12-07 07:04:56 +00001294 for (i = 0; i < vi->curr_queue_pairs; i++)
1295 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301296}
1297#endif
1298
Jason Wang586d17c2012-04-11 20:43:52 +00001299static void virtnet_ack_link_announce(struct virtnet_info *vi)
1300{
1301 rtnl_lock();
1302 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001303 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001304 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1305 rtnl_unlock();
1306}
1307
Jason Wang986a4f42012-12-07 07:04:56 +00001308static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1309{
1310 struct scatterlist sg;
Jason Wang986a4f42012-12-07 07:04:56 +00001311 struct net_device *dev = vi->dev;
1312
1313 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1314 return 0;
1315
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001316 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1317 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
Jason Wang986a4f42012-12-07 07:04:56 +00001318
1319 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001320 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001321 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1322 queue_pairs);
1323 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301324 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001325 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001326 /* virtnet_open() will refill when device is going to up. */
1327 if (dev->flags & IFF_UP)
1328 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301329 }
Jason Wang986a4f42012-12-07 07:04:56 +00001330
1331 return 0;
1332}
1333
Rusty Russell296f96f2007-10-22 11:03:37 +10001334static int virtnet_close(struct net_device *dev)
1335{
1336 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001337 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001338
Rusty Russellb2baed62011-12-29 00:42:38 +00001339 /* Make sure refill_work doesn't re-enable napi! */
1340 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001341
1342 for (i = 0; i < vi->max_queue_pairs; i++)
1343 napi_disable(&vi->rq[i].napi);
Rusty Russell296f96f2007-10-22 11:03:37 +10001344
Rusty Russell296f96f2007-10-22 11:03:37 +10001345 return 0;
1346}
1347
Alex Williamson2af76982009-02-04 09:02:40 +00001348static void virtnet_set_rx_mode(struct net_device *dev)
1349{
1350 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001351 struct scatterlist sg[2];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001352 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001353 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001354 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001355 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001356 void *buf;
1357 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001358
stephen hemminger788a8b62013-12-09 16:18:45 -08001359 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001360 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1361 return;
1362
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001363 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1364 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001365
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001366 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001367
1368 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001369 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001370 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001371 vi->ctrl_promisc ? "en" : "dis");
Alex Williamson2af76982009-02-04 09:02:40 +00001372
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001373 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001374
1375 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001376 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001377 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
Michael S. Tsirkin2ac46032015-11-15 15:11:00 +02001378 vi->ctrl_allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001379
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001380 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001381 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001382 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001383 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1384 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1385 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001386 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001387 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001388
Alex Williamson23e258e2009-05-01 17:27:56 +00001389 sg_init_table(sg, 2);
1390
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001391 /* Store the unicast list and count in the front of the buffer */
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001392 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
Jiri Pirkoccffad252009-05-22 23:22:17 +00001393 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001394 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001395 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001396
1397 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001398 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001399
1400 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001401 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001402
Michael S. Tsirkinfdd819b2014-10-07 16:39:48 +02001403 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
Jiri Pirko567ec872010-02-23 23:17:07 +00001404 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001405 netdev_for_each_mc_addr(ha, dev)
1406 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001407
1408 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001409 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001410
1411 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001412 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001413 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001414
1415 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001416}
1417
Patrick McHardy80d5c362013-04-19 02:04:28 +00001418static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1419 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001420{
1421 struct virtnet_info *vi = netdev_priv(dev);
1422 struct scatterlist sg;
1423
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001424 vi->ctrl_vid = vid;
1425 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001426
1427 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001428 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001429 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001430 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001431}
1432
Patrick McHardy80d5c362013-04-19 02:04:28 +00001433static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1434 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001435{
1436 struct virtnet_info *vi = netdev_priv(dev);
1437 struct scatterlist sg;
1438
Andy Lutomirskia725ee32016-07-18 15:34:49 -07001439 vi->ctrl_vid = vid;
1440 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001441
1442 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001443 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001444 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001445 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001446}
1447
Wanlong Gao8898c212013-01-24 23:51:30 +00001448static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001449{
1450 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001451
1452 if (vi->affinity_hint_set) {
1453 for (i = 0; i < vi->max_queue_pairs; i++) {
1454 virtqueue_set_affinity(vi->rq[i].vq, -1);
1455 virtqueue_set_affinity(vi->sq[i].vq, -1);
1456 }
1457
1458 vi->affinity_hint_set = false;
1459 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001460}
1461
1462static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001463{
1464 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001465 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001466
1467 /* In multiqueue mode, when the number of cpu is equal to the number of
1468 * queue pairs, we let the queue pairs to be private to one cpu by
1469 * setting the affinity hint to eliminate the contention.
1470 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001471 if (vi->curr_queue_pairs == 1 ||
1472 vi->max_queue_pairs != num_online_cpus()) {
1473 virtnet_clean_affinity(vi, -1);
1474 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001475 }
1476
Wanlong Gao8898c212013-01-24 23:51:30 +00001477 i = 0;
1478 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001479 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1480 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001481 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001482 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001483 }
1484
Wanlong Gao8898c212013-01-24 23:51:30 +00001485 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001486}
1487
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001488static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001489{
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001490 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1491 node);
1492 virtnet_set_affinity(vi);
1493 return 0;
1494}
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001495
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001496static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1497{
1498 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1499 node_dead);
1500 virtnet_set_affinity(vi);
1501 return 0;
1502}
Jason Wang3ab098d2013-10-15 11:18:58 +08001503
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02001504static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1505{
1506 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1507 node);
1508
1509 virtnet_clean_affinity(vi, cpu);
1510 return 0;
1511}
1512
1513static enum cpuhp_state virtionet_online;
1514
1515static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1516{
1517 int ret;
1518
1519 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1520 if (ret)
1521 return ret;
1522 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1523 &vi->node_dead);
1524 if (!ret)
1525 return ret;
1526 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1527 return ret;
1528}
1529
1530static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1531{
1532 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1533 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1534 &vi->node_dead);
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001535}
1536
Rick Jones8f9f4662011-10-19 08:10:59 +00001537static void virtnet_get_ringparam(struct net_device *dev,
1538 struct ethtool_ringparam *ring)
1539{
1540 struct virtnet_info *vi = netdev_priv(dev);
1541
Jason Wang986a4f42012-12-07 07:04:56 +00001542 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1543 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001544 ring->rx_pending = ring->rx_max_pending;
1545 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001546}
1547
Rick Jones66846042011-11-14 14:17:08 +00001548
1549static void virtnet_get_drvinfo(struct net_device *dev,
1550 struct ethtool_drvinfo *info)
1551{
1552 struct virtnet_info *vi = netdev_priv(dev);
1553 struct virtio_device *vdev = vi->vdev;
1554
1555 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1556 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1557 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1558
1559}
1560
Jason Wangd73bcd22012-12-07 07:04:57 +00001561/* TODO: Eliminate OOO packets during switching */
1562static int virtnet_set_channels(struct net_device *dev,
1563 struct ethtool_channels *channels)
1564{
1565 struct virtnet_info *vi = netdev_priv(dev);
1566 u16 queue_pairs = channels->combined_count;
1567 int err;
1568
1569 /* We don't support separate rx/tx channels.
1570 * We don't allow setting 'other' channels.
1571 */
1572 if (channels->rx_count || channels->tx_count || channels->other_count)
1573 return -EINVAL;
1574
Amos Kongc18e9cd2014-04-18 13:45:41 +08001575 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
Jason Wangd73bcd22012-12-07 07:04:57 +00001576 return -EINVAL;
1577
John Fastabendf600b692016-12-15 12:13:24 -08001578 /* For now we don't support modifying channels while XDP is loaded
1579 * also when XDP is loaded all RX queues have XDP programs so we only
1580 * need to check a single RX queue.
1581 */
1582 if (vi->rq[0].xdp_prog)
1583 return -EINVAL;
1584
Wanlong Gao47be2472013-01-24 23:51:29 +00001585 get_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001586 err = virtnet_set_queues(vi, queue_pairs);
1587 if (!err) {
1588 netif_set_real_num_tx_queues(dev, queue_pairs);
1589 netif_set_real_num_rx_queues(dev, queue_pairs);
1590
Wanlong Gao8898c212013-01-24 23:51:30 +00001591 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001592 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001593 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001594
1595 return err;
1596}
1597
1598static void virtnet_get_channels(struct net_device *dev,
1599 struct ethtool_channels *channels)
1600{
1601 struct virtnet_info *vi = netdev_priv(dev);
1602
1603 channels->combined_count = vi->curr_queue_pairs;
1604 channels->max_combined = vi->max_queue_pairs;
1605 channels->max_other = 0;
1606 channels->rx_count = 0;
1607 channels->tx_count = 0;
1608 channels->other_count = 0;
1609}
1610
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001611/* Check if the user is trying to change anything besides speed/duplex */
1612static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
1613{
1614 struct ethtool_cmd diff1 = *cmd;
1615 struct ethtool_cmd diff2 = {};
1616
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001617 /* cmd is always set so we need to clear it, validate the port type
1618 * and also without autonegotiation we can ignore advertising
1619 */
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001620 ethtool_cmd_speed_set(&diff1, 0);
Nikolay Aleksandrov0cf3ace2016-02-07 21:52:24 +01001621 diff2.port = PORT_OTHER;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001622 diff1.advertising = 0;
1623 diff1.duplex = 0;
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001624 diff1.cmd = 0;
1625
1626 return !memcmp(&diff1, &diff2, sizeof(diff1));
1627}
1628
1629static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1630{
1631 struct virtnet_info *vi = netdev_priv(dev);
1632 u32 speed;
1633
1634 speed = ethtool_cmd_speed(cmd);
1635 /* don't allow custom speed and duplex */
1636 if (!ethtool_validate_speed(speed) ||
1637 !ethtool_validate_duplex(cmd->duplex) ||
1638 !virtnet_validate_ethtool_cmd(cmd))
1639 return -EINVAL;
1640 vi->speed = speed;
1641 vi->duplex = cmd->duplex;
1642
1643 return 0;
1644}
1645
1646static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1647{
1648 struct virtnet_info *vi = netdev_priv(dev);
1649
1650 ethtool_cmd_speed_set(cmd, vi->speed);
1651 cmd->duplex = vi->duplex;
1652 cmd->port = PORT_OTHER;
1653
1654 return 0;
1655}
1656
1657static void virtnet_init_settings(struct net_device *dev)
1658{
1659 struct virtnet_info *vi = netdev_priv(dev);
1660
1661 vi->speed = SPEED_UNKNOWN;
1662 vi->duplex = DUPLEX_UNKNOWN;
1663}
1664
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001665static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001666 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001667 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001668 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001669 .set_channels = virtnet_set_channels,
1670 .get_channels = virtnet_get_channels,
Jacob Keller074c3582014-06-25 02:37:13 +00001671 .get_ts_info = ethtool_op_get_ts_info,
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01001672 .get_settings = virtnet_get_settings,
1673 .set_settings = virtnet_set_settings,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001674};
1675
John Fastabendf600b692016-12-15 12:13:24 -08001676static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1677{
1678 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1679 struct virtnet_info *vi = netdev_priv(dev);
1680 struct bpf_prog *old_prog;
John Fastabend672aafd2016-12-15 12:13:49 -08001681 u16 xdp_qp = 0, curr_qp;
1682 int i, err;
John Fastabendf600b692016-12-15 12:13:24 -08001683
Jakub Kicinski529ec6a2017-01-25 14:56:36 -08001684 if (prog && prog->xdp_adjust_head) {
1685 netdev_warn(dev, "Does not support bpf_xdp_adjust_head()\n");
1686 return -EOPNOTSUPP;
1687 }
1688
John Fastabendf600b692016-12-15 12:13:24 -08001689 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Jason Wang92502fe2016-12-23 22:37:30 +08001690 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1691 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1692 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
John Fastabendf600b692016-12-15 12:13:24 -08001693 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1694 return -EOPNOTSUPP;
1695 }
1696
1697 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1698 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1699 return -EINVAL;
1700 }
1701
1702 if (dev->mtu > max_sz) {
1703 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1704 return -EINVAL;
1705 }
1706
John Fastabend672aafd2016-12-15 12:13:49 -08001707 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1708 if (prog)
1709 xdp_qp = nr_cpu_ids;
1710
1711 /* XDP requires extra queues for XDP_TX */
1712 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1713 netdev_warn(dev, "request %i queues but max is %i\n",
1714 curr_qp + xdp_qp, vi->max_queue_pairs);
1715 return -ENOMEM;
1716 }
1717
1718 err = virtnet_set_queues(vi, curr_qp + xdp_qp);
1719 if (err) {
1720 dev_warn(&dev->dev, "XDP Device queue allocation failure.\n");
1721 return err;
1722 }
1723
John Fastabendf600b692016-12-15 12:13:24 -08001724 if (prog) {
1725 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
John Fastabend672aafd2016-12-15 12:13:49 -08001726 if (IS_ERR(prog)) {
1727 virtnet_set_queues(vi, curr_qp);
John Fastabendf600b692016-12-15 12:13:24 -08001728 return PTR_ERR(prog);
John Fastabend672aafd2016-12-15 12:13:49 -08001729 }
John Fastabendf600b692016-12-15 12:13:24 -08001730 }
1731
John Fastabend672aafd2016-12-15 12:13:49 -08001732 vi->xdp_queue_pairs = xdp_qp;
1733 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1734
John Fastabendf600b692016-12-15 12:13:24 -08001735 for (i = 0; i < vi->max_queue_pairs; i++) {
1736 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1737 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1738 if (old_prog)
1739 bpf_prog_put(old_prog);
1740 }
1741
1742 return 0;
1743}
1744
1745static bool virtnet_xdp_query(struct net_device *dev)
1746{
1747 struct virtnet_info *vi = netdev_priv(dev);
1748 int i;
1749
1750 for (i = 0; i < vi->max_queue_pairs; i++) {
1751 if (vi->rq[i].xdp_prog)
1752 return true;
1753 }
1754 return false;
1755}
1756
1757static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1758{
1759 switch (xdp->command) {
1760 case XDP_SETUP_PROG:
1761 return virtnet_xdp_set(dev, xdp->prog);
1762 case XDP_QUERY_PROG:
1763 xdp->prog_attached = virtnet_xdp_query(dev);
1764 return 0;
1765 default:
1766 return -EINVAL;
1767 }
1768}
1769
Stephen Hemminger76288b42009-01-06 10:44:22 -08001770static const struct net_device_ops virtnet_netdev = {
1771 .ndo_open = virtnet_open,
1772 .ndo_stop = virtnet_close,
1773 .ndo_start_xmit = start_xmit,
1774 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001775 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001776 .ndo_set_rx_mode = virtnet_set_rx_mode,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001777 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001778 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1779 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001780#ifdef CONFIG_NET_POLL_CONTROLLER
1781 .ndo_poll_controller = virtnet_netpoll,
1782#endif
John Fastabendf600b692016-12-15 12:13:24 -08001783 .ndo_xdp = virtnet_xdp,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001784};
1785
Jason Wang586d17c2012-04-11 20:43:52 +00001786static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001787{
Jason Wang586d17c2012-04-11 20:43:52 +00001788 struct virtnet_info *vi =
1789 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001790 u16 v;
1791
Rusty Russell855e0c52013-10-14 18:11:51 +10301792 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1793 struct virtio_net_config, status, &v) < 0)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301794 return;
Jason Wang586d17c2012-04-11 20:43:52 +00001795
1796 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001797 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001798 virtnet_ack_link_announce(vi);
1799 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001800
1801 /* Ignore unknown (future) status bits */
1802 v &= VIRTIO_NET_S_LINK_UP;
1803
1804 if (vi->status == v)
Michael S. Tsirkin507613b2014-10-15 10:22:30 +10301805 return;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001806
1807 vi->status = v;
1808
1809 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1810 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001811 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001812 } else {
1813 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001814 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001815 }
1816}
1817
1818static void virtnet_config_changed(struct virtio_device *vdev)
1819{
1820 struct virtnet_info *vi = vdev->priv;
1821
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001822 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001823}
1824
Jason Wang986a4f42012-12-07 07:04:56 +00001825static void virtnet_free_queues(struct virtnet_info *vi)
1826{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001827 int i;
1828
Jason Wangab3971b2015-03-12 13:57:44 +08001829 for (i = 0; i < vi->max_queue_pairs; i++) {
1830 napi_hash_del(&vi->rq[i].napi);
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001831 netif_napi_del(&vi->rq[i].napi);
Jason Wangab3971b2015-03-12 13:57:44 +08001832 }
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001833
Eric Dumazet963abe52016-11-15 22:24:12 -08001834 /* We called napi_hash_del() before netif_napi_del(),
1835 * we need to respect an RCU grace period before freeing vi->rq
1836 */
1837 synchronize_net();
1838
Jason Wang986a4f42012-12-07 07:04:56 +00001839 kfree(vi->rq);
1840 kfree(vi->sq);
1841}
1842
1843static void free_receive_bufs(struct virtnet_info *vi)
1844{
John Fastabendf600b692016-12-15 12:13:24 -08001845 struct bpf_prog *old_prog;
Jason Wang986a4f42012-12-07 07:04:56 +00001846 int i;
1847
John Fastabendf600b692016-12-15 12:13:24 -08001848 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00001849 for (i = 0; i < vi->max_queue_pairs; i++) {
1850 while (vi->rq[i].pages)
1851 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
John Fastabendf600b692016-12-15 12:13:24 -08001852
1853 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1854 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1855 if (old_prog)
1856 bpf_prog_put(old_prog);
Jason Wang986a4f42012-12-07 07:04:56 +00001857 }
John Fastabendf600b692016-12-15 12:13:24 -08001858 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001859}
1860
Michael Daltonfb518792014-01-16 22:23:26 -08001861static void free_receive_page_frags(struct virtnet_info *vi)
1862{
1863 int i;
1864 for (i = 0; i < vi->max_queue_pairs; i++)
1865 if (vi->rq[i].alloc_frag.page)
1866 put_page(vi->rq[i].alloc_frag.page);
1867}
1868
John Fastabendb68df012017-01-25 18:22:48 -08001869static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
John Fastabend56434a02016-12-15 12:14:13 -08001870{
John Fastabendb68df012017-01-25 18:22:48 -08001871 /* For small receive mode always use kfree_skb variants */
1872 if (!vi->mergeable_rx_bufs)
1873 return false;
1874
John Fastabend56434a02016-12-15 12:14:13 -08001875 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1876 return false;
1877 else if (q < vi->curr_queue_pairs)
1878 return true;
1879 else
1880 return false;
1881}
1882
Jason Wang986a4f42012-12-07 07:04:56 +00001883static void free_unused_bufs(struct virtnet_info *vi)
1884{
1885 void *buf;
1886 int i;
1887
1888 for (i = 0; i < vi->max_queue_pairs; i++) {
1889 struct virtqueue *vq = vi->sq[i].vq;
John Fastabend56434a02016-12-15 12:14:13 -08001890 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
John Fastabendb68df012017-01-25 18:22:48 -08001891 if (!is_xdp_raw_buffer_queue(vi, i))
John Fastabend56434a02016-12-15 12:14:13 -08001892 dev_kfree_skb(buf);
1893 else
1894 put_page(virt_to_head_page(buf));
1895 }
Jason Wang986a4f42012-12-07 07:04:56 +00001896 }
1897
1898 for (i = 0; i < vi->max_queue_pairs; i++) {
1899 struct virtqueue *vq = vi->rq[i].vq;
1900
1901 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Daltonab7db912014-01-16 22:23:27 -08001902 if (vi->mergeable_rx_bufs) {
1903 unsigned long ctx = (unsigned long)buf;
1904 void *base = mergeable_ctx_to_buf_address(ctx);
1905 put_page(virt_to_head_page(base));
1906 } else if (vi->big_packets) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04001907 give_pages(&vi->rq[i], buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001908 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001909 dev_kfree_skb(buf);
Michael Daltonab7db912014-01-16 22:23:27 -08001910 }
Jason Wang986a4f42012-12-07 07:04:56 +00001911 }
Jason Wang986a4f42012-12-07 07:04:56 +00001912 }
1913}
1914
Jason Wange9d74172012-12-07 07:04:55 +00001915static void virtnet_del_vqs(struct virtnet_info *vi)
1916{
1917 struct virtio_device *vdev = vi->vdev;
1918
Wanlong Gao8898c212013-01-24 23:51:30 +00001919 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00001920
Jason Wange9d74172012-12-07 07:04:55 +00001921 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00001922
1923 virtnet_free_queues(vi);
1924}
1925
1926static int virtnet_find_vqs(struct virtnet_info *vi)
1927{
1928 vq_callback_t **callbacks;
1929 struct virtqueue **vqs;
1930 int ret = -ENOMEM;
1931 int i, total_vqs;
1932 const char **names;
1933
1934 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1935 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1936 * possible control vq.
1937 */
1938 total_vqs = vi->max_queue_pairs * 2 +
1939 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1940
1941 /* Allocate space for find_vqs parameters */
1942 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1943 if (!vqs)
1944 goto err_vq;
1945 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1946 if (!callbacks)
1947 goto err_callback;
1948 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1949 if (!names)
1950 goto err_names;
1951
1952 /* Parameters for control virtqueue, if any */
1953 if (vi->has_cvq) {
1954 callbacks[total_vqs - 1] = NULL;
1955 names[total_vqs - 1] = "control";
1956 }
1957
1958 /* Allocate/initialize parameters for send/receive virtqueues */
1959 for (i = 0; i < vi->max_queue_pairs; i++) {
1960 callbacks[rxq2vq(i)] = skb_recv_done;
1961 callbacks[txq2vq(i)] = skb_xmit_done;
1962 sprintf(vi->rq[i].name, "input.%d", i);
1963 sprintf(vi->sq[i].name, "output.%d", i);
1964 names[rxq2vq(i)] = vi->rq[i].name;
1965 names[txq2vq(i)] = vi->sq[i].name;
1966 }
1967
1968 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1969 names);
1970 if (ret)
1971 goto err_find;
1972
1973 if (vi->has_cvq) {
1974 vi->cvq = vqs[total_vqs - 1];
1975 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00001976 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00001977 }
1978
1979 for (i = 0; i < vi->max_queue_pairs; i++) {
1980 vi->rq[i].vq = vqs[rxq2vq(i)];
1981 vi->sq[i].vq = vqs[txq2vq(i)];
1982 }
1983
1984 kfree(names);
1985 kfree(callbacks);
1986 kfree(vqs);
1987
1988 return 0;
1989
1990err_find:
1991 kfree(names);
1992err_names:
1993 kfree(callbacks);
1994err_callback:
1995 kfree(vqs);
1996err_vq:
1997 return ret;
1998}
1999
2000static int virtnet_alloc_queues(struct virtnet_info *vi)
2001{
2002 int i;
2003
2004 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2005 if (!vi->sq)
2006 goto err_sq;
2007 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00002008 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00002009 goto err_rq;
2010
2011 INIT_DELAYED_WORK(&vi->refill, refill_work);
2012 for (i = 0; i < vi->max_queue_pairs; i++) {
2013 vi->rq[i].pages = NULL;
2014 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2015 napi_weight);
2016
2017 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
Johannes Berg5377d7582015-08-19 09:48:40 +02002018 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
Jason Wang986a4f42012-12-07 07:04:56 +00002019 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2020 }
2021
2022 return 0;
2023
2024err_rq:
2025 kfree(vi->sq);
2026err_sq:
2027 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00002028}
2029
Amit Shah3f9c10b2011-12-22 16:58:31 +05302030static int init_vqs(struct virtnet_info *vi)
2031{
Jason Wang986a4f42012-12-07 07:04:56 +00002032 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302033
Jason Wang986a4f42012-12-07 07:04:56 +00002034 /* Allocate send & receive queues */
2035 ret = virtnet_alloc_queues(vi);
2036 if (ret)
2037 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302038
Jason Wang986a4f42012-12-07 07:04:56 +00002039 ret = virtnet_find_vqs(vi);
2040 if (ret)
2041 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302042
Wanlong Gao47be2472013-01-24 23:51:29 +00002043 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00002044 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00002045 put_online_cpus();
2046
Amit Shah3f9c10b2011-12-22 16:58:31 +05302047 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00002048
2049err_free:
2050 virtnet_free_queues(vi);
2051err:
2052 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05302053}
2054
Michael Daltonfbf28d72014-01-16 22:23:30 -08002055#ifdef CONFIG_SYSFS
2056static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2057 struct rx_queue_attribute *attribute, char *buf)
2058{
2059 struct virtnet_info *vi = netdev_priv(queue->dev);
2060 unsigned int queue_index = get_netdev_rx_queue_index(queue);
Johannes Berg5377d7582015-08-19 09:48:40 +02002061 struct ewma_pkt_len *avg;
Michael Daltonfbf28d72014-01-16 22:23:30 -08002062
2063 BUG_ON(queue_index >= vi->max_queue_pairs);
2064 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2065 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2066}
2067
2068static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2069 __ATTR_RO(mergeable_rx_buffer_size);
2070
2071static struct attribute *virtio_net_mrg_rx_attrs[] = {
2072 &mergeable_rx_buffer_size_attribute.attr,
2073 NULL
2074};
2075
2076static const struct attribute_group virtio_net_mrg_rx_group = {
2077 .name = "virtio_net",
2078 .attrs = virtio_net_mrg_rx_attrs
2079};
2080#endif
2081
Jason Wang892d6eb2014-11-20 17:03:05 +08002082static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2083 unsigned int fbit,
2084 const char *fname, const char *dname)
2085{
2086 if (!virtio_has_feature(vdev, fbit))
2087 return false;
2088
2089 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2090 fname, dname);
2091
2092 return true;
2093}
2094
2095#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2096 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2097
2098static bool virtnet_validate_features(struct virtio_device *vdev)
2099{
2100 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2101 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2102 "VIRTIO_NET_F_CTRL_VQ") ||
2103 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2104 "VIRTIO_NET_F_CTRL_VQ") ||
2105 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2106 "VIRTIO_NET_F_CTRL_VQ") ||
2107 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2108 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2109 "VIRTIO_NET_F_CTRL_VQ"))) {
2110 return false;
2111 }
2112
2113 return true;
2114}
2115
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002116#define MIN_MTU ETH_MIN_MTU
2117#define MAX_MTU ETH_MAX_MTU
2118
Rusty Russell296f96f2007-10-22 11:03:37 +10002119static int virtnet_probe(struct virtio_device *vdev)
2120{
Jason Wang986a4f42012-12-07 07:04:56 +00002121 int i, err;
Rusty Russell296f96f2007-10-22 11:03:37 +10002122 struct net_device *dev;
2123 struct virtnet_info *vi;
Jason Wang986a4f42012-12-07 07:04:56 +00002124 u16 max_queue_pairs;
Aaron Conole14de9d12016-06-03 16:57:12 -04002125 int mtu;
Jason Wang986a4f42012-12-07 07:04:56 +00002126
Michael S. Tsirkin6ba42242015-01-12 16:23:37 +02002127 if (!vdev->config->get) {
2128 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2129 __func__);
2130 return -EINVAL;
2131 }
2132
Jason Wang892d6eb2014-11-20 17:03:05 +08002133 if (!virtnet_validate_features(vdev))
2134 return -EINVAL;
2135
Jason Wang986a4f42012-12-07 07:04:56 +00002136 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10302137 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2138 struct virtio_net_config,
2139 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002140
2141 /* We need at least 2 queue's */
2142 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2143 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2144 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2145 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10002146
2147 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00002148 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10002149 if (!dev)
2150 return -ENOMEM;
2151
2152 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00002153 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08002154 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10002155 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002156
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002157 dev->ethtool_ops = &virtnet_ethtool_ops;
Rusty Russell296f96f2007-10-22 11:03:37 +10002158 SET_NETDEV_DEV(dev, &vdev->dev);
2159
2160 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002161 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10002162 /* This opens up the world of extra features. */
Jason Wang48900cb2015-08-05 10:34:04 +08002163 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002164 if (csum)
Jason Wang48900cb2015-08-05 10:34:04 +08002165 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002166
2167 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002168 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05002169 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2170 }
Rusty Russell5539ae962008-05-02 21:50:46 -05002171 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00002172 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2173 dev->hw_features |= NETIF_F_TSO;
2174 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2175 dev->hw_features |= NETIF_F_TSO6;
2176 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2177 dev->hw_features |= NETIF_F_TSO_ECN;
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002178 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2179 dev->hw_features |= NETIF_F_UFO;
Michał Mirosław98e778c2011-03-31 01:01:35 +00002180
Jason Wang41f2f122014-12-24 11:03:52 +08002181 dev->features |= NETIF_F_GSO_ROBUST;
2182
Michał Mirosław98e778c2011-03-31 01:01:35 +00002183 if (gso)
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002184 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
Michał Mirosław98e778c2011-03-31 01:01:35 +00002185 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10002186 }
Thomas Huth4f491292013-08-27 17:09:02 +02002187 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2188 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10002189
Jason Wang4fda8302013-04-10 23:32:21 +00002190 dev->vlan_features = dev->features;
2191
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002192 /* MTU range: 68 - 65535 */
2193 dev->min_mtu = MIN_MTU;
2194 dev->max_mtu = MAX_MTU;
2195
Rusty Russell296f96f2007-10-22 11:03:37 +10002196 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10302197 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2198 virtio_cread_bytes(vdev,
2199 offsetof(struct virtio_net_config, mac),
2200 dev->dev_addr, dev->addr_len);
2201 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002202 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002203
2204 /* Set up our device-specific information */
2205 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002206 vi->dev = dev;
2207 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01002208 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002209 vi->stats = alloc_percpu(struct virtnet_stats);
2210 err = -ENOMEM;
2211 if (vi->stats == NULL)
2212 goto free;
2213
John Stultz827da442013-10-07 15:51:58 -07002214 for_each_possible_cpu(i) {
2215 struct virtnet_stats *virtnet_stats;
2216 virtnet_stats = per_cpu_ptr(vi->stats, i);
2217 u64_stats_init(&virtnet_stats->tx_syncp);
2218 u64_stats_init(&virtnet_stats->rx_syncp);
2219 }
2220
Jason Wang586d17c2012-04-11 20:43:52 +00002221 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10002222
Herbert Xu97402b92008-04-18 11:24:27 +08002223 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00002224 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2225 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05002226 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2227 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
Herbert Xu97402b92008-04-18 11:24:27 +08002228 vi->big_packets = true;
2229
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08002230 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2231 vi->mergeable_rx_bufs = true;
2232
Michael S. Tsirkind04302b2014-10-24 00:24:03 +03002233 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2234 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002235 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2236 else
2237 vi->hdr_len = sizeof(struct virtio_net_hdr);
2238
Michael S. Tsirkin75993302015-07-15 15:26:19 +03002239 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2240 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302241 vi->any_header_sg = true;
2242
Jason Wang986a4f42012-12-07 07:04:56 +00002243 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2244 vi->has_cvq = true;
2245
Aaron Conole14de9d12016-06-03 16:57:12 -04002246 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2247 mtu = virtio_cread16(vdev,
2248 offsetof(struct virtio_net_config,
2249 mtu));
Aaron Conole93a205e2016-10-25 16:12:12 -04002250 if (mtu < dev->min_mtu) {
Aaron Conole14de9d12016-06-03 16:57:12 -04002251 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
Aaron Conole93a205e2016-10-25 16:12:12 -04002252 } else {
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002253 dev->mtu = mtu;
Aaron Conole93a205e2016-10-25 16:12:12 -04002254 dev->max_mtu = mtu;
2255 }
Aaron Conole14de9d12016-06-03 16:57:12 -04002256 }
2257
Michael S. Tsirkin012873d2014-10-24 16:55:57 +03002258 if (vi->any_header_sg)
2259 dev->needed_headroom = vi->hdr_len;
Zhangjie \(HZ\)6ebbc1a2014-04-29 18:43:22 +08002260
Jason Wang44900012016-11-25 12:37:26 +08002261 /* Enable multiqueue by default */
2262 if (num_online_cpus() >= max_queue_pairs)
2263 vi->curr_queue_pairs = max_queue_pairs;
2264 else
2265 vi->curr_queue_pairs = num_online_cpus();
Jason Wang986a4f42012-12-07 07:04:56 +00002266 vi->max_queue_pairs = max_queue_pairs;
2267
2268 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05302269 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002270 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08002271 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002272
Michael Daltonfbf28d72014-01-16 22:23:30 -08002273#ifdef CONFIG_SYSFS
2274 if (vi->mergeable_rx_bufs)
2275 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2276#endif
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08002277 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2278 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00002279
Nikolay Aleksandrov16032be2016-02-03 04:04:37 +01002280 virtnet_init_settings(dev);
2281
Rusty Russell296f96f2007-10-22 11:03:37 +10002282 err = register_netdev(dev);
2283 if (err) {
2284 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002285 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10002286 }
Rusty Russellb3369c12008-02-04 23:50:02 -05002287
Michael S. Tsirkin4baf1e32014-10-15 10:22:30 +10302288 virtio_device_ready(vdev);
2289
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002290 err = virtnet_cpu_notif_add(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002291 if (err) {
2292 pr_debug("virtio_net: registering cpu notifier failed\n");
wangyunjianf00e35e2016-05-31 11:52:43 +08002293 goto free_unregister_netdev;
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002294 }
2295
Jason Wanga2208712016-12-13 14:23:05 +08002296 rtnl_lock();
2297 virtnet_set_queues(vi, vi->curr_queue_pairs);
2298 rtnl_unlock();
Jason Wang44900012016-11-25 12:37:26 +08002299
Jason Wang167c25e2010-11-10 14:45:41 +00002300 /* Assume link up if device can't report link status,
2301 otherwise get link status from config. */
2302 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2303 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07002304 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00002305 } else {
2306 vi->status = VIRTIO_NET_S_LINK_UP;
2307 netif_carrier_on(dev);
2308 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002309
Jason Wang986a4f42012-12-07 07:04:56 +00002310 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2311 dev->name, max_queue_pairs);
2312
Rusty Russell296f96f2007-10-22 11:03:37 +10002313 return 0;
2314
wangyunjianf00e35e2016-05-31 11:52:43 +08002315free_unregister_netdev:
Michael S. Tsirkin02465552014-10-15 10:22:31 +10302316 vi->vdev->config->reset(vdev);
2317
Rusty Russellb3369c12008-02-04 23:50:02 -05002318 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002319free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00002320 cancel_delayed_work_sync(&vi->refill);
Michael Daltonfb518792014-01-16 22:23:26 -08002321 free_receive_page_frags(vi);
Jason Wange9d74172012-12-07 07:04:55 +00002322 virtnet_del_vqs(vi);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00002323free_stats:
2324 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10002325free:
2326 free_netdev(dev);
2327 return err;
2328}
2329
Amit Shah04486ed2011-12-22 16:58:32 +05302330static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10002331{
Amit Shah04486ed2011-12-22 16:58:32 +05302332 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00002333
2334 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00002335 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002336
Jason Wang986a4f42012-12-07 07:04:56 +00002337 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06002338
Michael Daltonfb518792014-01-16 22:23:26 -08002339 free_receive_page_frags(vi);
2340
Jason Wang986a4f42012-12-07 07:04:56 +00002341 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05302342}
2343
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002344static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05302345{
2346 struct virtnet_info *vi = vdev->priv;
2347
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002348 virtnet_cpu_notif_remove(vi);
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00002349
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302350 /* Make sure no work handler is accessing the device. */
2351 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002352
Amit Shah04486ed2011-12-22 16:58:32 +05302353 unregister_netdev(vi->dev);
2354
2355 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05002356
Krishna Kumar2e66f552011-07-20 03:56:02 +00002357 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05002358 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10002359}
2360
Aaron Lu89107002013-09-17 09:25:23 +09302361#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302362static int virtnet_freeze(struct virtio_device *vdev)
2363{
2364 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00002365 int i;
Amit Shah0741bcb2011-12-22 16:58:33 +05302366
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002367 virtnet_cpu_notif_remove(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002368
Michael S. Tsirkin102a2782014-10-15 10:22:29 +10302369 /* Make sure no work handler is accessing the device */
2370 flush_work(&vi->config_work);
Jason Wang586d17c2012-04-11 20:43:52 +00002371
Amit Shah0741bcb2011-12-22 16:58:33 +05302372 netif_device_detach(vi->dev);
2373 cancel_delayed_work_sync(&vi->refill);
2374
Jason Wang91815632014-07-23 16:33:55 +08002375 if (netif_running(vi->dev)) {
Jason Wangab3971b2015-03-12 13:57:44 +08002376 for (i = 0; i < vi->max_queue_pairs; i++)
Jason Wang986a4f42012-12-07 07:04:56 +00002377 napi_disable(&vi->rq[i].napi);
Jason Wang91815632014-07-23 16:33:55 +08002378 }
Amit Shah0741bcb2011-12-22 16:58:33 +05302379
2380 remove_vq_common(vi);
2381
2382 return 0;
2383}
2384
2385static int virtnet_restore(struct virtio_device *vdev)
2386{
2387 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00002388 int err, i;
Amit Shah0741bcb2011-12-22 16:58:33 +05302389
2390 err = init_vqs(vi);
2391 if (err)
2392 return err;
2393
Michael S. Tsirkine53fbd12014-10-15 10:22:32 +10302394 virtio_device_ready(vdev);
2395
Jason Wang6cd4ce02013-12-30 11:34:40 +08002396 if (netif_running(vi->dev)) {
2397 for (i = 0; i < vi->curr_queue_pairs; i++)
Michael S. Tsirkin946fa562014-10-24 00:12:10 +03002398 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
Jason Wang6cd4ce02013-12-30 11:34:40 +08002399 schedule_delayed_work(&vi->refill, 0);
2400
Jason Wang986a4f42012-12-07 07:04:56 +00002401 for (i = 0; i < vi->max_queue_pairs; i++)
2402 virtnet_napi_enable(&vi->rq[i]);
Jason Wang6cd4ce02013-12-30 11:34:40 +08002403 }
Amit Shah0741bcb2011-12-22 16:58:33 +05302404
2405 netif_device_attach(vi->dev);
2406
Jason Wang35ed1592013-10-15 11:18:59 +08002407 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00002408 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang35ed1592013-10-15 11:18:59 +08002409 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00002410
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002411 err = virtnet_cpu_notif_add(vi);
Jason Wangec9debb2013-10-29 15:11:07 +08002412 if (err)
2413 return err;
2414
Amit Shah0741bcb2011-12-22 16:58:33 +05302415 return 0;
2416}
2417#endif
2418
Rusty Russell296f96f2007-10-22 11:03:37 +10002419static struct virtio_device_id id_table[] = {
2420 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2421 { 0 },
2422};
2423
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002424#define VIRTNET_FEATURES \
2425 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2426 VIRTIO_NET_F_MAC, \
2427 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2428 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2429 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2430 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2431 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2432 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2433 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2434 VIRTIO_NET_F_MTU
2435
Rusty Russellc45a6812008-05-02 21:50:50 -05002436static unsigned int features[] = {
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002437 VIRTNET_FEATURES,
2438};
2439
2440static unsigned int features_legacy[] = {
2441 VIRTNET_FEATURES,
2442 VIRTIO_NET_F_GSO,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09302443 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05002444};
2445
Uwe Kleine-König22402522009-11-05 01:32:44 -08002446static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05002447 .feature_table = features,
2448 .feature_table_size = ARRAY_SIZE(features),
Michael S. Tsirkinf3358502016-11-04 12:55:36 +02002449 .feature_table_legacy = features_legacy,
2450 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
Rusty Russell296f96f2007-10-22 11:03:37 +10002451 .driver.name = KBUILD_MODNAME,
2452 .driver.owner = THIS_MODULE,
2453 .id_table = id_table,
2454 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05002455 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08002456 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09302457#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05302458 .freeze = virtnet_freeze,
2459 .restore = virtnet_restore,
2460#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10002461};
2462
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002463static __init int virtio_net_driver_init(void)
2464{
2465 int ret;
2466
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002467 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002468 virtnet_cpu_online,
2469 virtnet_cpu_down_prep);
2470 if (ret < 0)
2471 goto out;
2472 virtionet_online = ret;
Thomas Gleixner73c1b412016-12-21 20:19:54 +01002473 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
Sebastian Andrzej Siewior8017c272016-08-12 19:49:43 +02002474 NULL, virtnet_cpu_dead);
2475 if (ret)
2476 goto err_dead;
2477
2478 ret = register_virtio_driver(&virtio_net_driver);
2479 if (ret)
2480 goto err_virtio;
2481 return 0;
2482err_virtio:
2483 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2484err_dead:
2485 cpuhp_remove_multi_state(virtionet_online);
2486out:
2487 return ret;
2488}
2489module_init(virtio_net_driver_init);
2490
2491static __exit void virtio_net_driver_exit(void)
2492{
2493 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2494 cpuhp_remove_multi_state(virtionet_online);
2495 unregister_virtio_driver(&virtio_net_driver);
2496}
2497module_exit(virtio_net_driver_exit);
Rusty Russell296f96f2007-10-22 11:03:37 +10002498
2499MODULE_DEVICE_TABLE(virtio, id_table);
2500MODULE_DESCRIPTION("Virtio network driver");
2501MODULE_LICENSE("GPL");