]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/virtio_net.c
Linux 4.11-rc5
[mirror_ubuntu-bionic-kernel.git] / drivers / net / virtio_net.c
CommitLineData
48925e37 1/* A network driver using virtio.
296f96fc
RR
2 *
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
adf8d3ff 16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
296f96fc
RR
17 */
18//#define DEBUG
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
a9ea3fc6 21#include <linux/ethtool.h>
296f96fc
RR
22#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
f600b690 25#include <linux/bpf.h>
a67edbf4 26#include <linux/bpf_trace.h>
296f96fc 27#include <linux/scatterlist.h>
e918085a 28#include <linux/if_vlan.h>
5a0e3ad6 29#include <linux/slab.h>
8de4b2f3 30#include <linux/cpu.h>
ab7db917 31#include <linux/average.h>
296f96fc 32
d34710e3 33static int napi_weight = NAPI_POLL_WEIGHT;
6c0cd7c0
DL
34module_param(napi_weight, int, 0444);
35
eb939922 36static bool csum = true, gso = true;
34a48579
RR
37module_param(csum, bool, 0444);
38module_param(gso, bool, 0444);
39
296f96fc 40/* FIXME: MTU in config. */
5061de36 41#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
3f2c31d9 42#define GOOD_COPY_LEN 128
296f96fc 43
f6b10209
JW
44#define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
45
2de2f7f4
JF
46/* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
47#define VIRTIO_XDP_HEADROOM 256
48
5377d758
JB
49/* RX packet size EWMA. The average packet size is used to determine the packet
50 * buffer size when refilling RX rings. As the entire RX ring may be refilled
51 * at once, the weight is chosen so that the EWMA will be insensitive to short-
52 * term, transient changes in packet size.
ab7db917 53 */
eb1e011a 54DECLARE_EWMA(pkt_len, 0, 64)
ab7db917 55
d0fa28f0
MT
56/* With mergeable buffers we align buffer address and use the low bits to
57 * encode its true size. Buffer size is up to 1 page so we need to align to
58 * square root of page size to ensure we reserve enough bits to encode the true
59 * size.
60 */
61#define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2)
62
ab7db917 63/* Minimum alignment for mergeable packet buffers. */
d0fa28f0
MT
64#define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \
65 1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT)
ab7db917 66
66846048 67#define VIRTNET_DRIVER_VERSION "1.0.0"
2a41f71d 68
3fa2a1df 69struct virtnet_stats {
83a27052
ED
70 struct u64_stats_sync tx_syncp;
71 struct u64_stats_sync rx_syncp;
3fa2a1df 72 u64 tx_bytes;
73 u64 tx_packets;
74
75 u64 rx_bytes;
76 u64 rx_packets;
77};
78
e9d7417b
JW
79/* Internal representation of a send virtqueue */
80struct send_queue {
81 /* Virtqueue associated with this send _queue */
82 struct virtqueue *vq;
83
84 /* TX: fragments + linear part + virtio header */
85 struct scatterlist sg[MAX_SKB_FRAGS + 2];
986a4f4d
JW
86
87 /* Name of the send queue: output.$index */
88 char name[40];
e9d7417b
JW
89};
90
91/* Internal representation of a receive virtqueue */
92struct receive_queue {
93 /* Virtqueue associated with this receive_queue */
94 struct virtqueue *vq;
95
296f96fc
RR
96 struct napi_struct napi;
97
f600b690
JF
98 struct bpf_prog __rcu *xdp_prog;
99
e9d7417b
JW
100 /* Chain pages by the private ptr. */
101 struct page *pages;
102
ab7db917 103 /* Average packet length for mergeable receive buffers. */
5377d758 104 struct ewma_pkt_len mrg_avg_pkt_len;
ab7db917 105
fb51879d
MD
106 /* Page frag for packet buffer allocation. */
107 struct page_frag alloc_frag;
108
e9d7417b
JW
109 /* RX: fragments + linear part + virtio header */
110 struct scatterlist sg[MAX_SKB_FRAGS + 2];
986a4f4d
JW
111
112 /* Name of this receive queue: input.$index */
113 char name[40];
e9d7417b
JW
114};
115
116struct virtnet_info {
117 struct virtio_device *vdev;
118 struct virtqueue *cvq;
119 struct net_device *dev;
986a4f4d
JW
120 struct send_queue *sq;
121 struct receive_queue *rq;
e9d7417b
JW
122 unsigned int status;
123
986a4f4d
JW
124 /* Max # of queue pairs supported by the device */
125 u16 max_queue_pairs;
126
127 /* # of queue pairs currently used by the driver */
128 u16 curr_queue_pairs;
129
672aafd5
JF
130 /* # of XDP queue pairs currently used by the driver */
131 u16 xdp_queue_pairs;
132
97402b96
HX
133 /* I like... big packets and I cannot lie! */
134 bool big_packets;
135
3f2c31d9
MM
136 /* Host will merge rx buffers for big packets (shake it! shake it!) */
137 bool mergeable_rx_bufs;
138
986a4f4d
JW
139 /* Has control virtqueue */
140 bool has_cvq;
141
e7428e95
MT
142 /* Host can handle any s/g split between our header and packet data */
143 bool any_header_sg;
144
012873d0
MT
145 /* Packet virtio header size */
146 u8 hdr_len;
147
3fa2a1df 148 /* Active statistics */
149 struct virtnet_stats __percpu *stats;
150
3161e453
RR
151 /* Work struct for refilling if we run low on memory. */
152 struct delayed_work refill;
153
586d17c5
JW
154 /* Work struct for config space updates */
155 struct work_struct config_work;
156
986a4f4d
JW
157 /* Does the affinity hint is set for virtqueues? */
158 bool affinity_hint_set;
47be2479 159
8017c279
SAS
160 /* CPU hotplug instances for online & dead */
161 struct hlist_node node;
162 struct hlist_node node_dead;
2ac46030
MT
163
164 /* Control VQ buffers: protected by the rtnl lock */
165 struct virtio_net_ctrl_hdr ctrl_hdr;
166 virtio_net_ctrl_ack ctrl_status;
a725ee3e 167 struct virtio_net_ctrl_mq ctrl_mq;
2ac46030
MT
168 u8 ctrl_promisc;
169 u8 ctrl_allmulti;
a725ee3e 170 u16 ctrl_vid;
16032be5
NA
171
172 /* Ethtool settings */
173 u8 duplex;
174 u32 speed;
296f96fc
RR
175};
176
9ab86bbc 177struct padded_vnet_hdr {
012873d0 178 struct virtio_net_hdr_mrg_rxbuf hdr;
9ab86bbc 179 /*
012873d0
MT
180 * hdr is in a separate sg buffer, and data sg buffer shares same page
181 * with this header sg. This padding makes next sg 16 byte aligned
182 * after the header.
9ab86bbc 183 */
012873d0 184 char padding[4];
9ab86bbc
SM
185};
186
986a4f4d
JW
187/* Converting between virtqueue no. and kernel tx/rx queue no.
188 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
189 */
190static int vq2txq(struct virtqueue *vq)
191{
9d0ca6ed 192 return (vq->index - 1) / 2;
986a4f4d
JW
193}
194
195static int txq2vq(int txq)
196{
197 return txq * 2 + 1;
198}
199
200static int vq2rxq(struct virtqueue *vq)
201{
9d0ca6ed 202 return vq->index / 2;
986a4f4d
JW
203}
204
205static int rxq2vq(int rxq)
206{
207 return rxq * 2;
208}
209
012873d0 210static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
296f96fc 211{
012873d0 212 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
296f96fc
RR
213}
214
9ab86bbc
SM
215/*
216 * private is used to chain pages for big packets, put the whole
217 * most recent used list in the beginning for reuse
218 */
e9d7417b 219static void give_pages(struct receive_queue *rq, struct page *page)
0a888fd1 220{
9ab86bbc 221 struct page *end;
0a888fd1 222
e9d7417b 223 /* Find end of list, sew whole thing into vi->rq.pages. */
9ab86bbc 224 for (end = page; end->private; end = (struct page *)end->private);
e9d7417b
JW
225 end->private = (unsigned long)rq->pages;
226 rq->pages = page;
0a888fd1
MM
227}
228
e9d7417b 229static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
fb6813f4 230{
e9d7417b 231 struct page *p = rq->pages;
fb6813f4 232
9ab86bbc 233 if (p) {
e9d7417b 234 rq->pages = (struct page *)p->private;
9ab86bbc
SM
235 /* clear private here, it is used to chain pages */
236 p->private = 0;
237 } else
fb6813f4
RR
238 p = alloc_page(gfp_mask);
239 return p;
240}
241
e9d7417b 242static void skb_xmit_done(struct virtqueue *vq)
296f96fc 243{
e9d7417b 244 struct virtnet_info *vi = vq->vdev->priv;
296f96fc 245
2cb9c6ba 246 /* Suppress further interrupts. */
e9d7417b 247 virtqueue_disable_cb(vq);
11a3a154 248
363f1514 249 /* We were probably waiting for more output buffers. */
986a4f4d 250 netif_wake_subqueue(vi->dev, vq2txq(vq));
296f96fc
RR
251}
252
ab7db917
MD
253static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
254{
255 unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
256 return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
257}
258
259static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
260{
261 return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
262
263}
264
265static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
266{
267 unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
268 return (unsigned long)buf | (size - 1);
269}
270
3464645a 271/* Called from bottom half context */
946fa564
MT
272static struct sk_buff *page_to_skb(struct virtnet_info *vi,
273 struct receive_queue *rq,
2613af0e
MD
274 struct page *page, unsigned int offset,
275 unsigned int len, unsigned int truesize)
9ab86bbc
SM
276{
277 struct sk_buff *skb;
012873d0 278 struct virtio_net_hdr_mrg_rxbuf *hdr;
2613af0e 279 unsigned int copy, hdr_len, hdr_padded_len;
9ab86bbc 280 char *p;
fb6813f4 281
2613af0e 282 p = page_address(page) + offset;
3f2c31d9 283
9ab86bbc 284 /* copy small packet so we can reuse these pages for small data */
c67f5db8 285 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
9ab86bbc
SM
286 if (unlikely(!skb))
287 return NULL;
3f2c31d9 288
9ab86bbc 289 hdr = skb_vnet_hdr(skb);
3f2c31d9 290
012873d0
MT
291 hdr_len = vi->hdr_len;
292 if (vi->mergeable_rx_bufs)
293 hdr_padded_len = sizeof *hdr;
294 else
2613af0e 295 hdr_padded_len = sizeof(struct padded_vnet_hdr);
3f2c31d9 296
9ab86bbc 297 memcpy(hdr, p, hdr_len);
3f2c31d9 298
9ab86bbc 299 len -= hdr_len;
2613af0e
MD
300 offset += hdr_padded_len;
301 p += hdr_padded_len;
3f2c31d9 302
9ab86bbc
SM
303 copy = len;
304 if (copy > skb_tailroom(skb))
305 copy = skb_tailroom(skb);
306 memcpy(skb_put(skb, copy), p, copy);
3f2c31d9 307
9ab86bbc
SM
308 len -= copy;
309 offset += copy;
3f2c31d9 310
2613af0e
MD
311 if (vi->mergeable_rx_bufs) {
312 if (len)
313 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
314 else
315 put_page(page);
316 return skb;
317 }
318
e878d78b
SL
319 /*
320 * Verify that we can indeed put this data into a skb.
321 * This is here to handle cases when the device erroneously
322 * tries to receive more than is possible. This is usually
323 * the case of a broken device.
324 */
325 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
be443899 326 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
e878d78b
SL
327 dev_kfree_skb(skb);
328 return NULL;
329 }
2613af0e 330 BUG_ON(offset >= PAGE_SIZE);
9ab86bbc 331 while (len) {
2613af0e
MD
332 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
333 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
334 frag_size, truesize);
335 len -= frag_size;
9ab86bbc
SM
336 page = (struct page *)page->private;
337 offset = 0;
338 }
3f2c31d9 339
9ab86bbc 340 if (page)
e9d7417b 341 give_pages(rq, page);
3f2c31d9 342
9ab86bbc
SM
343 return skb;
344}
3f2c31d9 345
a67edbf4 346static bool virtnet_xdp_xmit(struct virtnet_info *vi,
56434a01 347 struct receive_queue *rq,
f6b10209 348 struct xdp_buff *xdp)
56434a01 349{
56434a01 350 struct virtio_net_hdr_mrg_rxbuf *hdr;
f6b10209 351 unsigned int len;
722d8283
JF
352 struct send_queue *sq;
353 unsigned int qp;
56434a01
JF
354 void *xdp_sent;
355 int err;
356
722d8283
JF
357 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
358 sq = &vi->sq[qp];
359
56434a01
JF
360 /* Free up any pending old buffers before queueing new ones. */
361 while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
f6b10209 362 struct page *sent_page = virt_to_head_page(xdp_sent);
bb91accf 363
f6b10209 364 put_page(sent_page);
bb91accf 365 }
56434a01 366
f6b10209
JW
367 xdp->data -= vi->hdr_len;
368 /* Zero header and leave csum up to XDP layers */
369 hdr = xdp->data;
370 memset(hdr, 0, vi->hdr_len);
bb91accf 371
f6b10209 372 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
bb91accf 373
f6b10209 374 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC);
56434a01 375 if (unlikely(err)) {
f6b10209 376 struct page *page = virt_to_head_page(xdp->data);
bb91accf 377
f6b10209 378 put_page(page);
a67edbf4 379 return false;
56434a01
JF
380 }
381
382 virtqueue_kick(sq->vq);
a67edbf4 383 return true;
56434a01
JF
384}
385
f6b10209
JW
386static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
387{
388 return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
389}
390
bb91accf
JW
391static struct sk_buff *receive_small(struct net_device *dev,
392 struct virtnet_info *vi,
393 struct receive_queue *rq,
394 void *buf, unsigned int len)
f121159d 395{
f6b10209 396 struct sk_buff *skb;
bb91accf 397 struct bpf_prog *xdp_prog;
f6b10209
JW
398 unsigned int xdp_headroom = virtnet_get_headroom(vi);
399 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
400 unsigned int headroom = vi->hdr_len + header_offset;
401 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
402 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
403 unsigned int delta = 0;
012873d0 404 len -= vi->hdr_len;
f121159d 405
bb91accf
JW
406 rcu_read_lock();
407 xdp_prog = rcu_dereference(rq->xdp_prog);
408 if (xdp_prog) {
f6b10209 409 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
0354e4d1 410 struct xdp_buff xdp;
f6b10209 411 void *orig_data;
bb91accf
JW
412 u32 act;
413
414 if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
415 goto err_xdp;
0354e4d1 416
f6b10209
JW
417 xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
418 xdp.data = xdp.data_hard_start + xdp_headroom;
0354e4d1 419 xdp.data_end = xdp.data + len;
f6b10209 420 orig_data = xdp.data;
0354e4d1
JF
421 act = bpf_prog_run_xdp(xdp_prog, &xdp);
422
bb91accf
JW
423 switch (act) {
424 case XDP_PASS:
2de2f7f4 425 /* Recalculate length in case bpf program changed it */
f6b10209 426 delta = orig_data - xdp.data;
bb91accf
JW
427 break;
428 case XDP_TX:
f6b10209 429 if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
0354e4d1 430 trace_xdp_exception(vi->dev, xdp_prog, act);
bb91accf
JW
431 rcu_read_unlock();
432 goto xdp_xmit;
bb91accf 433 default:
0354e4d1
JF
434 bpf_warn_invalid_xdp_action(act);
435 case XDP_ABORTED:
436 trace_xdp_exception(vi->dev, xdp_prog, act);
437 case XDP_DROP:
bb91accf
JW
438 goto err_xdp;
439 }
440 }
441 rcu_read_unlock();
442
f6b10209
JW
443 skb = build_skb(buf, buflen);
444 if (!skb) {
445 put_page(virt_to_head_page(buf));
446 goto err;
447 }
448 skb_reserve(skb, headroom - delta);
449 skb_put(skb, len + delta);
450 if (!delta) {
451 buf += header_offset;
452 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
453 } /* keep zeroed vnet hdr since packet was changed by bpf */
454
455err:
f121159d 456 return skb;
bb91accf
JW
457
458err_xdp:
459 rcu_read_unlock();
460 dev->stats.rx_dropped++;
f6b10209 461 put_page(virt_to_head_page(buf));
bb91accf
JW
462xdp_xmit:
463 return NULL;
f121159d
MT
464}
465
466static struct sk_buff *receive_big(struct net_device *dev,
946fa564 467 struct virtnet_info *vi,
f121159d
MT
468 struct receive_queue *rq,
469 void *buf,
470 unsigned int len)
471{
472 struct page *page = buf;
c47a43d3 473 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
f600b690 474
f121159d
MT
475 if (unlikely(!skb))
476 goto err;
477
478 return skb;
479
480err:
481 dev->stats.rx_dropped++;
482 give_pages(rq, page);
483 return NULL;
484}
485
72979a6c
JF
486/* The conditions to enable XDP should preclude the underlying device from
487 * sending packets across multiple buffers (num_buf > 1). However per spec
488 * it does not appear to be illegal to do so but rather just against convention.
489 * So in order to avoid making a system unresponsive the packets are pushed
490 * into a page and the XDP program is run. This will be extremely slow and we
491 * push a warning to the user to fix this as soon as possible. Fixing this may
492 * require resolving the underlying hardware to determine why multiple buffers
493 * are being received or simply loading the XDP program in the ingress stack
494 * after the skb is built because there is no advantage to running it here
495 * anymore.
496 */
497static struct page *xdp_linearize_page(struct receive_queue *rq,
56a86f84 498 u16 *num_buf,
72979a6c
JF
499 struct page *p,
500 int offset,
501 unsigned int *len)
502{
503 struct page *page = alloc_page(GFP_ATOMIC);
2de2f7f4 504 unsigned int page_off = VIRTIO_XDP_HEADROOM;
72979a6c
JF
505
506 if (!page)
507 return NULL;
508
509 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
510 page_off += *len;
511
56a86f84 512 while (--*num_buf) {
72979a6c
JF
513 unsigned int buflen;
514 unsigned long ctx;
515 void *buf;
516 int off;
517
518 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen);
519 if (unlikely(!ctx))
520 goto err_buf;
521
56a86f84
JW
522 buf = mergeable_ctx_to_buf_address(ctx);
523 p = virt_to_head_page(buf);
524 off = buf - page_address(p);
525
72979a6c
JF
526 /* guard against a misconfigured or uncooperative backend that
527 * is sending packet larger than the MTU.
528 */
56a86f84
JW
529 if ((page_off + buflen) > PAGE_SIZE) {
530 put_page(p);
72979a6c 531 goto err_buf;
56a86f84 532 }
72979a6c
JF
533
534 memcpy(page_address(page) + page_off,
535 page_address(p) + off, buflen);
536 page_off += buflen;
56a86f84 537 put_page(p);
72979a6c
JF
538 }
539
2de2f7f4
JF
540 /* Headroom does not contribute to packet length */
541 *len = page_off - VIRTIO_XDP_HEADROOM;
72979a6c
JF
542 return page;
543err_buf:
544 __free_pages(page, 0);
545 return NULL;
546}
547
8fc3b9e9 548static struct sk_buff *receive_mergeable(struct net_device *dev,
fdd819b2 549 struct virtnet_info *vi,
8fc3b9e9 550 struct receive_queue *rq,
ab7db917 551 unsigned long ctx,
8fc3b9e9 552 unsigned int len)
9ab86bbc 553{
ab7db917 554 void *buf = mergeable_ctx_to_buf_address(ctx);
012873d0
MT
555 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
556 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
8fc3b9e9
MT
557 struct page *page = virt_to_head_page(buf);
558 int offset = buf - page_address(page);
f600b690
JF
559 struct sk_buff *head_skb, *curr_skb;
560 struct bpf_prog *xdp_prog;
561 unsigned int truesize;
562
56434a01
JF
563 head_skb = NULL;
564
f600b690
JF
565 rcu_read_lock();
566 xdp_prog = rcu_dereference(rq->xdp_prog);
567 if (xdp_prog) {
72979a6c 568 struct page *xdp_page;
0354e4d1 569 struct xdp_buff xdp;
0354e4d1 570 void *data;
f600b690
JF
571 u32 act;
572
73b62bd0 573 /* This happens when rx buffer size is underestimated */
f600b690 574 if (unlikely(num_buf > 1)) {
72979a6c 575 /* linearize data for XDP */
56a86f84 576 xdp_page = xdp_linearize_page(rq, &num_buf,
72979a6c
JF
577 page, offset, &len);
578 if (!xdp_page)
579 goto err_xdp;
2de2f7f4 580 offset = VIRTIO_XDP_HEADROOM;
72979a6c
JF
581 } else {
582 xdp_page = page;
f600b690
JF
583 }
584
585 /* Transient failure which in theory could occur if
586 * in-flight packets from before XDP was enabled reach
587 * the receive path after XDP is loaded. In practice I
588 * was not able to create this condition.
589 */
b00f70b0 590 if (unlikely(hdr->hdr.gso_type))
f600b690
JF
591 goto err_xdp;
592
2de2f7f4
JF
593 /* Allow consuming headroom but reserve enough space to push
594 * the descriptor on if we get an XDP_TX return code.
595 */
0354e4d1 596 data = page_address(xdp_page) + offset;
2de2f7f4 597 xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
0354e4d1
JF
598 xdp.data = data + vi->hdr_len;
599 xdp.data_end = xdp.data + (len - vi->hdr_len);
600 act = bpf_prog_run_xdp(xdp_prog, &xdp);
601
56434a01
JF
602 switch (act) {
603 case XDP_PASS:
2de2f7f4
JF
604 /* recalculate offset to account for any header
605 * adjustments. Note other cases do not build an
606 * skb and avoid using offset
607 */
608 offset = xdp.data -
609 page_address(xdp_page) - vi->hdr_len;
610
1830f893
JW
611 /* We can only create skb based on xdp_page. */
612 if (unlikely(xdp_page != page)) {
613 rcu_read_unlock();
614 put_page(page);
615 head_skb = page_to_skb(vi, rq, xdp_page,
2de2f7f4 616 offset, len, PAGE_SIZE);
5c33474d 617 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
1830f893
JW
618 return head_skb;
619 }
56434a01
JF
620 break;
621 case XDP_TX:
f6b10209 622 if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
0354e4d1 623 trace_xdp_exception(vi->dev, xdp_prog, act);
5c33474d 624 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
72979a6c
JF
625 if (unlikely(xdp_page != page))
626 goto err_xdp;
56434a01
JF
627 rcu_read_unlock();
628 goto xdp_xmit;
56434a01 629 default:
0354e4d1
JF
630 bpf_warn_invalid_xdp_action(act);
631 case XDP_ABORTED:
632 trace_xdp_exception(vi->dev, xdp_prog, act);
633 case XDP_DROP:
72979a6c
JF
634 if (unlikely(xdp_page != page))
635 __free_pages(xdp_page, 0);
5c33474d 636 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
f600b690 637 goto err_xdp;
56434a01 638 }
f600b690
JF
639 }
640 rcu_read_unlock();
ab7db917 641
f600b690
JF
642 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
643 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
644 curr_skb = head_skb;
9ab86bbc 645
8fc3b9e9
MT
646 if (unlikely(!curr_skb))
647 goto err_skb;
9ab86bbc 648 while (--num_buf) {
8fc3b9e9
MT
649 int num_skb_frags;
650
ab7db917
MD
651 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
652 if (unlikely(!ctx)) {
8fc3b9e9 653 pr_debug("%s: rx error: %d buffers out of %d missing\n",
fdd819b2 654 dev->name, num_buf,
012873d0
MT
655 virtio16_to_cpu(vi->vdev,
656 hdr->num_buffers));
8fc3b9e9
MT
657 dev->stats.rx_length_errors++;
658 goto err_buf;
3f2c31d9 659 }
8fc3b9e9 660
ab7db917 661 buf = mergeable_ctx_to_buf_address(ctx);
8fc3b9e9 662 page = virt_to_head_page(buf);
8fc3b9e9
MT
663
664 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
2613af0e
MD
665 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
666 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
8fc3b9e9
MT
667
668 if (unlikely(!nskb))
669 goto err_skb;
2613af0e
MD
670 if (curr_skb == head_skb)
671 skb_shinfo(curr_skb)->frag_list = nskb;
672 else
673 curr_skb->next = nskb;
674 curr_skb = nskb;
675 head_skb->truesize += nskb->truesize;
676 num_skb_frags = 0;
677 }
ab7db917 678 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
2613af0e
MD
679 if (curr_skb != head_skb) {
680 head_skb->data_len += len;
681 head_skb->len += len;
fb51879d 682 head_skb->truesize += truesize;
2613af0e 683 }
8fc3b9e9 684 offset = buf - page_address(page);
ba275241
JW
685 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
686 put_page(page);
687 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
fb51879d 688 len, truesize);
ba275241
JW
689 } else {
690 skb_add_rx_frag(curr_skb, num_skb_frags, page,
fb51879d 691 offset, len, truesize);
ba275241 692 }
8fc3b9e9
MT
693 }
694
5377d758 695 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
8fc3b9e9
MT
696 return head_skb;
697
f600b690
JF
698err_xdp:
699 rcu_read_unlock();
8fc3b9e9
MT
700err_skb:
701 put_page(page);
702 while (--num_buf) {
ab7db917
MD
703 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
704 if (unlikely(!ctx)) {
8fc3b9e9
MT
705 pr_debug("%s: rx error: %d buffers missing\n",
706 dev->name, num_buf);
707 dev->stats.rx_length_errors++;
708 break;
709 }
ab7db917 710 page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
8fc3b9e9 711 put_page(page);
9ab86bbc 712 }
8fc3b9e9
MT
713err_buf:
714 dev->stats.rx_dropped++;
715 dev_kfree_skb(head_skb);
56434a01 716xdp_xmit:
8fc3b9e9 717 return NULL;
9ab86bbc
SM
718}
719
61845d20
JW
720static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
721 void *buf, unsigned int len)
9ab86bbc 722{
e9d7417b 723 struct net_device *dev = vi->dev;
9ab86bbc 724 struct sk_buff *skb;
012873d0 725 struct virtio_net_hdr_mrg_rxbuf *hdr;
61845d20 726 int ret;
3f2c31d9 727
bcff3162 728 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
9ab86bbc
SM
729 pr_debug("%s: short packet %i\n", dev->name, len);
730 dev->stats.rx_length_errors++;
ab7db917
MD
731 if (vi->mergeable_rx_bufs) {
732 unsigned long ctx = (unsigned long)buf;
733 void *base = mergeable_ctx_to_buf_address(ctx);
734 put_page(virt_to_head_page(base));
735 } else if (vi->big_packets) {
98bfd23c 736 give_pages(rq, buf);
ab7db917 737 } else {
f6b10209 738 put_page(virt_to_head_page(buf));
ab7db917 739 }
61845d20 740 return 0;
9ab86bbc 741 }
3f2c31d9 742
f121159d 743 if (vi->mergeable_rx_bufs)
fdd819b2 744 skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
f121159d 745 else if (vi->big_packets)
946fa564 746 skb = receive_big(dev, vi, rq, buf, len);
f121159d 747 else
bb91accf 748 skb = receive_small(dev, vi, rq, buf, len);
f121159d
MT
749
750 if (unlikely(!skb))
61845d20 751 return 0;
3f2c31d9 752
9ab86bbc 753 hdr = skb_vnet_hdr(skb);
3fa2a1df 754
61845d20 755 ret = skb->len;
296f96fc 756
e858fae2 757 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
10a8d94a 758 skb->ip_summed = CHECKSUM_UNNECESSARY;
296f96fc 759
e858fae2
MR
760 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
761 virtio_is_little_endian(vi->vdev))) {
762 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
763 dev->name, hdr->hdr.gso_type,
764 hdr->hdr.gso_size);
765 goto frame_err;
296f96fc
RR
766 }
767
d1dc06dc
MR
768 skb->protocol = eth_type_trans(skb, dev);
769 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
770 ntohs(skb->protocol), skb->len, skb->pkt_type);
771
0fbd050a 772 napi_gro_receive(&rq->napi, skb);
61845d20 773 return ret;
296f96fc
RR
774
775frame_err:
776 dev->stats.rx_frame_errors++;
296f96fc 777 dev_kfree_skb(skb);
61845d20 778 return 0;
296f96fc
RR
779}
780
946fa564
MT
781static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
782 gfp_t gfp)
296f96fc 783{
f6b10209
JW
784 struct page_frag *alloc_frag = &rq->alloc_frag;
785 char *buf;
2de2f7f4 786 unsigned int xdp_headroom = virtnet_get_headroom(vi);
f6b10209 787 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
9ab86bbc 788 int err;
3f2c31d9 789
f6b10209
JW
790 len = SKB_DATA_ALIGN(len) +
791 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
792 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
9ab86bbc 793 return -ENOMEM;
296f96fc 794
f6b10209
JW
795 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
796 get_page(alloc_frag->page);
797 alloc_frag->offset += len;
798 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
799 vi->hdr_len + GOOD_PACKET_LEN);
800 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
9ab86bbc 801 if (err < 0)
f6b10209 802 put_page(virt_to_head_page(buf));
97402b96 803
9ab86bbc
SM
804 return err;
805}
97402b96 806
012873d0
MT
807static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
808 gfp_t gfp)
9ab86bbc 809{
9ab86bbc
SM
810 struct page *first, *list = NULL;
811 char *p;
812 int i, err, offset;
813
a5835440
RR
814 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
815
e9d7417b 816 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
9ab86bbc 817 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
e9d7417b 818 first = get_a_page(rq, gfp);
9ab86bbc
SM
819 if (!first) {
820 if (list)
e9d7417b 821 give_pages(rq, list);
9ab86bbc 822 return -ENOMEM;
97402b96 823 }
e9d7417b 824 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
97402b96 825
9ab86bbc
SM
826 /* chain new page in list head to match sg */
827 first->private = (unsigned long)list;
828 list = first;
829 }
296f96fc 830
e9d7417b 831 first = get_a_page(rq, gfp);
9ab86bbc 832 if (!first) {
e9d7417b 833 give_pages(rq, list);
9ab86bbc
SM
834 return -ENOMEM;
835 }
836 p = page_address(first);
837
e9d7417b 838 /* rq->sg[0], rq->sg[1] share the same page */
012873d0
MT
839 /* a separated rq->sg[0] for header - required in case !any_header_sg */
840 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
9ab86bbc 841
e9d7417b 842 /* rq->sg[1] for data packet, from offset */
9ab86bbc 843 offset = sizeof(struct padded_vnet_hdr);
e9d7417b 844 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
9ab86bbc
SM
845
846 /* chain first in list head */
847 first->private = (unsigned long)list;
9dc7b9e4
RR
848 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
849 first, gfp);
9ab86bbc 850 if (err < 0)
e9d7417b 851 give_pages(rq, first);
9ab86bbc
SM
852
853 return err;
296f96fc
RR
854}
855
5377d758 856static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
3f2c31d9 857{
ab7db917 858 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
fbf28d78
MD
859 unsigned int len;
860
5377d758 861 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
fbf28d78
MD
862 GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
863 return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
864}
865
2de2f7f4
JF
866static int add_recvbuf_mergeable(struct virtnet_info *vi,
867 struct receive_queue *rq, gfp_t gfp)
fbf28d78 868{
fb51879d 869 struct page_frag *alloc_frag = &rq->alloc_frag;
2de2f7f4 870 unsigned int headroom = virtnet_get_headroom(vi);
fb51879d 871 char *buf;
ab7db917 872 unsigned long ctx;
3f2c31d9 873 int err;
fb51879d 874 unsigned int len, hole;
3f2c31d9 875
fbf28d78 876 len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
2de2f7f4 877 if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp)))
9ab86bbc 878 return -ENOMEM;
ab7db917 879
fb51879d 880 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
2de2f7f4 881 buf += headroom; /* advance address leaving hole at front of pkt */
ab7db917 882 ctx = mergeable_buf_to_ctx(buf, len);
fb51879d 883 get_page(alloc_frag->page);
2de2f7f4 884 alloc_frag->offset += len + headroom;
fb51879d 885 hole = alloc_frag->size - alloc_frag->offset;
2de2f7f4 886 if (hole < len + headroom) {
ab7db917
MD
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 */
fb51879d
MD
892 len += hole;
893 alloc_frag->offset += hole;
894 }
3f2c31d9 895
fb51879d 896 sg_init_one(rq->sg, buf, len);
ab7db917 897 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
9ab86bbc 898 if (err < 0)
2613af0e 899 put_page(virt_to_head_page(buf));
3f2c31d9 900
9ab86bbc
SM
901 return err;
902}
3f2c31d9 903
b2baed69
RR
904/*
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 */
946fa564
MT
911static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
912 gfp_t gfp)
9ab86bbc
SM
913{
914 int err;
1788f495 915 bool oom;
3f2c31d9 916
fb51879d 917 gfp |= __GFP_COLD;
9ab86bbc
SM
918 do {
919 if (vi->mergeable_rx_bufs)
2de2f7f4 920 err = add_recvbuf_mergeable(vi, rq, gfp);
9ab86bbc 921 else if (vi->big_packets)
012873d0 922 err = add_recvbuf_big(vi, rq, gfp);
9ab86bbc 923 else
946fa564 924 err = add_recvbuf_small(vi, rq, gfp);
3f2c31d9 925
1788f495 926 oom = err == -ENOMEM;
9ed4cb07 927 if (err)
3f2c31d9 928 break;
b7dfde95 929 } while (rq->vq->num_free);
681daee2 930 virtqueue_kick(rq->vq);
3161e453 931 return !oom;
3f2c31d9
MM
932}
933
18445c4d 934static void skb_recv_done(struct virtqueue *rvq)
296f96fc
RR
935{
936 struct virtnet_info *vi = rvq->vdev->priv;
986a4f4d 937 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
e9d7417b 938
18445c4d 939 /* Schedule NAPI, Suppress further interrupts if successful. */
e9d7417b 940 if (napi_schedule_prep(&rq->napi)) {
1915a712 941 virtqueue_disable_cb(rvq);
e9d7417b 942 __napi_schedule(&rq->napi);
18445c4d 943 }
296f96fc
RR
944}
945
e9d7417b 946static void virtnet_napi_enable(struct receive_queue *rq)
3e9d08ec 947{
e9d7417b 948 napi_enable(&rq->napi);
3e9d08ec
BR
949
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 */
e9d7417b
JW
954 if (napi_schedule_prep(&rq->napi)) {
955 virtqueue_disable_cb(rq->vq);
ec13ee80 956 local_bh_disable();
e9d7417b 957 __napi_schedule(&rq->napi);
ec13ee80 958 local_bh_enable();
3e9d08ec
BR
959 }
960}
961
3161e453
RR
962static void refill_work(struct work_struct *work)
963{
e9d7417b
JW
964 struct virtnet_info *vi =
965 container_of(work, struct virtnet_info, refill.work);
3161e453 966 bool still_empty;
986a4f4d
JW
967 int i;
968
55257d72 969 for (i = 0; i < vi->curr_queue_pairs; i++) {
986a4f4d 970 struct receive_queue *rq = &vi->rq[i];
3161e453 971
986a4f4d 972 napi_disable(&rq->napi);
946fa564 973 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
986a4f4d 974 virtnet_napi_enable(rq);
3161e453 975
986a4f4d
JW
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 }
3161e453
RR
982}
983
2ffa7598 984static int virtnet_receive(struct receive_queue *rq, int budget)
296f96fc 985{
e9d7417b 986 struct virtnet_info *vi = rq->vq->vdev->priv;
61845d20 987 unsigned int len, received = 0, bytes = 0;
9ab86bbc 988 void *buf;
61845d20 989 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
296f96fc 990
296f96fc 991 while (received < budget &&
e9d7417b 992 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
61845d20 993 bytes += receive_buf(vi, rq, buf, len);
296f96fc
RR
994 received++;
995 }
996
be121f46 997 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
946fa564 998 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
3b07e9ca 999 schedule_delayed_work(&vi->refill, 0);
3161e453 1000 }
296f96fc 1001
61845d20
JW
1002 u64_stats_update_begin(&stats->rx_syncp);
1003 stats->rx_bytes += bytes;
1004 stats->rx_packets += received;
1005 u64_stats_update_end(&stats->rx_syncp);
1006
2ffa7598
JW
1007 return received;
1008}
1009
1010static int virtnet_poll(struct napi_struct *napi, int budget)
1011{
1012 struct receive_queue *rq =
1013 container_of(napi, struct receive_queue, napi);
faadb05f 1014 unsigned int r, received;
2ffa7598 1015
faadb05f 1016 received = virtnet_receive(rq, budget);
2ffa7598 1017
8329d98e
RR
1018 /* Out of packets? */
1019 if (received < budget) {
cbdadbbf 1020 r = virtqueue_enable_cb_prepare(rq->vq);
4d6308aa
ED
1021 if (napi_complete_done(napi, received)) {
1022 if (unlikely(virtqueue_poll(rq->vq, r)) &&
1023 napi_schedule_prep(napi)) {
1024 virtqueue_disable_cb(rq->vq);
1025 __napi_schedule(napi);
1026 }
4265f161 1027 }
296f96fc
RR
1028 }
1029
1030 return received;
1031}
1032
986a4f4d
JW
1033static int virtnet_open(struct net_device *dev)
1034{
1035 struct virtnet_info *vi = netdev_priv(dev);
1036 int i;
1037
e4166625
JW
1038 for (i = 0; i < vi->max_queue_pairs; i++) {
1039 if (i < vi->curr_queue_pairs)
1040 /* Make sure we have some buffers: if oom use wq. */
946fa564 1041 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
e4166625 1042 schedule_delayed_work(&vi->refill, 0);
986a4f4d
JW
1043 virtnet_napi_enable(&vi->rq[i]);
1044 }
1045
1046 return 0;
1047}
1048
b7dfde95 1049static void free_old_xmit_skbs(struct send_queue *sq)
296f96fc
RR
1050{
1051 struct sk_buff *skb;
6ee57bcc 1052 unsigned int len;
e9d7417b 1053 struct virtnet_info *vi = sq->vq->vdev->priv;
58472a76 1054 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
61845d20
JW
1055 unsigned int packets = 0;
1056 unsigned int bytes = 0;
296f96fc 1057
e9d7417b 1058 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
296f96fc 1059 pr_debug("Sent skb %p\n", skb);
3fa2a1df 1060
61845d20
JW
1061 bytes += skb->len;
1062 packets++;
3fa2a1df 1063
ed79bab8 1064 dev_kfree_skb_any(skb);
296f96fc 1065 }
61845d20
JW
1066
1067 /* Avoid overhead when no packets have been processed
1068 * happens when called speculatively from start_xmit.
1069 */
1070 if (!packets)
1071 return;
1072
1073 u64_stats_update_begin(&stats->tx_syncp);
1074 stats->tx_bytes += bytes;
1075 stats->tx_packets += packets;
1076 u64_stats_update_end(&stats->tx_syncp);
296f96fc
RR
1077}
1078
e9d7417b 1079static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
296f96fc 1080{
012873d0 1081 struct virtio_net_hdr_mrg_rxbuf *hdr;
296f96fc 1082 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
e9d7417b 1083 struct virtnet_info *vi = sq->vq->vdev->priv;
7bedc7dc 1084 unsigned num_sg;
012873d0 1085 unsigned hdr_len = vi->hdr_len;
e7428e95 1086 bool can_push;
296f96fc 1087
e174961c 1088 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
e7428e95
MT
1089
1090 can_push = vi->any_header_sg &&
1091 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1092 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1093 /* Even if we can, don't push here yet as this would skew
1094 * csum_start offset below. */
1095 if (can_push)
012873d0 1096 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
e7428e95
MT
1097 else
1098 hdr = skb_vnet_hdr(skb);
296f96fc 1099
e858fae2 1100 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
6391a448 1101 virtio_is_little_endian(vi->vdev), false))
e858fae2 1102 BUG();
296f96fc 1103
3f2c31d9 1104 if (vi->mergeable_rx_bufs)
012873d0 1105 hdr->num_buffers = 0;
3f2c31d9 1106
547c890c 1107 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
e7428e95
MT
1108 if (can_push) {
1109 __skb_push(skb, hdr_len);
1110 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1111 /* Pull header back to avoid skew in tx bytes calculations. */
1112 __skb_pull(skb, hdr_len);
1113 } else {
1114 sg_set_buf(sq->sg, hdr, hdr_len);
1115 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1116 }
9dc7b9e4 1117 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
11a3a154
RR
1118}
1119
424efe9c 1120static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
99ffc696
RR
1121{
1122 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d
JW
1123 int qnum = skb_get_queue_mapping(skb);
1124 struct send_queue *sq = &vi->sq[qnum];
9ed4cb07 1125 int err;
4b7fd2e6
MT
1126 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1127 bool kick = !skb->xmit_more;
2cb9c6ba 1128
2cb9c6ba 1129 /* Free up any pending old buffers before queueing new ones. */
e9d7417b 1130 free_old_xmit_skbs(sq);
99ffc696 1131
074c3582
JK
1132 /* timestamp packet in software */
1133 skb_tx_timestamp(skb);
1134
03f191ba 1135 /* Try to transmit */
b7dfde95 1136 err = xmit_skb(sq, skb);
48925e37 1137
9ed4cb07 1138 /* This should not happen! */
681daee2 1139 if (unlikely(err)) {
9ed4cb07
RR
1140 dev->stats.tx_fifo_errors++;
1141 if (net_ratelimit())
1142 dev_warn(&dev->dev,
b7dfde95 1143 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
58eba97d 1144 dev->stats.tx_dropped++;
85e94525 1145 dev_kfree_skb_any(skb);
58eba97d 1146 return NETDEV_TX_OK;
296f96fc 1147 }
03f191ba 1148
48925e37
RR
1149 /* Don't wait up for transmitted skbs to be freed. */
1150 skb_orphan(skb);
1151 nf_reset(skb);
1152
60302ff6
MT
1153 /* If running out of space, stop queue to avoid getting packets that we
1154 * are then unable to transmit.
1155 * An alternative would be to force queuing layer to requeue the skb by
1156 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1157 * returned in a normal path of operation: it means that driver is not
1158 * maintaining the TX queue stop/start state properly, and causes
1159 * the stack to do a non-trivial amount of useless work.
1160 * Since most packets only take 1 or 2 ring slots, stopping the queue
1161 * early means 16 slots are typically wasted.
d631b94e 1162 */
b7dfde95 1163 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
986a4f4d 1164 netif_stop_subqueue(dev, qnum);
e9d7417b 1165 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
48925e37 1166 /* More just got used, free them then recheck. */
b7dfde95
LT
1167 free_old_xmit_skbs(sq);
1168 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
986a4f4d 1169 netif_start_subqueue(dev, qnum);
e9d7417b 1170 virtqueue_disable_cb(sq->vq);
48925e37
RR
1171 }
1172 }
99ffc696 1173 }
48925e37 1174
4b7fd2e6 1175 if (kick || netif_xmit_stopped(txq))
0b725a2c 1176 virtqueue_kick(sq->vq);
296f96fc 1177
0b725a2c 1178 return NETDEV_TX_OK;
c223a078
DM
1179}
1180
40cbfc37
AK
1181/*
1182 * Send command via the control virtqueue and check status. Commands
1183 * supported by the hypervisor, as indicated by feature bits, should
788a8b6d 1184 * never fail unless improperly formatted.
40cbfc37
AK
1185 */
1186static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
d24bae32 1187 struct scatterlist *out)
40cbfc37 1188{
f7bc9594 1189 struct scatterlist *sgs[4], hdr, stat;
d24bae32 1190 unsigned out_num = 0, tmp;
40cbfc37
AK
1191
1192 /* Caller should know better */
f7bc9594 1193 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
40cbfc37 1194
2ac46030
MT
1195 vi->ctrl_status = ~0;
1196 vi->ctrl_hdr.class = class;
1197 vi->ctrl_hdr.cmd = cmd;
f7bc9594 1198 /* Add header */
2ac46030 1199 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
f7bc9594 1200 sgs[out_num++] = &hdr;
40cbfc37 1201
f7bc9594
RR
1202 if (out)
1203 sgs[out_num++] = out;
40cbfc37 1204
f7bc9594 1205 /* Add return status. */
2ac46030 1206 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
d24bae32 1207 sgs[out_num] = &stat;
40cbfc37 1208
d24bae32 1209 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
a7c58146 1210 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
40cbfc37 1211
67975901 1212 if (unlikely(!virtqueue_kick(vi->cvq)))
2ac46030 1213 return vi->ctrl_status == VIRTIO_NET_OK;
40cbfc37
AK
1214
1215 /* Spin for a response, the kick causes an ioport write, trapping
1216 * into the hypervisor, so the request should be handled immediately.
1217 */
047b9b94
HG
1218 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1219 !virtqueue_is_broken(vi->cvq))
40cbfc37
AK
1220 cpu_relax();
1221
2ac46030 1222 return vi->ctrl_status == VIRTIO_NET_OK;
40cbfc37
AK
1223}
1224
9c46f6d4
AW
1225static int virtnet_set_mac_address(struct net_device *dev, void *p)
1226{
1227 struct virtnet_info *vi = netdev_priv(dev);
1228 struct virtio_device *vdev = vi->vdev;
f2f2c8b4 1229 int ret;
e37e2ff3 1230 struct sockaddr *addr;
7e58d5ae 1231 struct scatterlist sg;
9c46f6d4 1232
801822d1 1233 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
e37e2ff3
AL
1234 if (!addr)
1235 return -ENOMEM;
e37e2ff3
AL
1236
1237 ret = eth_prepare_mac_addr_change(dev, addr);
f2f2c8b4 1238 if (ret)
e37e2ff3 1239 goto out;
9c46f6d4 1240
7e58d5ae
AK
1241 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1242 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1243 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
d24bae32 1244 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
7e58d5ae
AK
1245 dev_warn(&vdev->dev,
1246 "Failed to set mac address by vq command.\n");
e37e2ff3
AL
1247 ret = -EINVAL;
1248 goto out;
7e58d5ae 1249 }
7e93a02f
MT
1250 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1251 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
855e0c52
RR
1252 unsigned int i;
1253
1254 /* Naturally, this has an atomicity problem. */
1255 for (i = 0; i < dev->addr_len; i++)
1256 virtio_cwrite8(vdev,
1257 offsetof(struct virtio_net_config, mac) +
1258 i, addr->sa_data[i]);
7e58d5ae
AK
1259 }
1260
1261 eth_commit_mac_addr_change(dev, p);
e37e2ff3 1262 ret = 0;
9c46f6d4 1263
e37e2ff3
AL
1264out:
1265 kfree(addr);
1266 return ret;
9c46f6d4
AW
1267}
1268
bc1f4470 1269static void virtnet_stats(struct net_device *dev,
1270 struct rtnl_link_stats64 *tot)
3fa2a1df 1271{
1272 struct virtnet_info *vi = netdev_priv(dev);
1273 int cpu;
1274 unsigned int start;
1275
1276 for_each_possible_cpu(cpu) {
58472a76 1277 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
3fa2a1df 1278 u64 tpackets, tbytes, rpackets, rbytes;
1279
1280 do {
57a7744e 1281 start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
3fa2a1df 1282 tpackets = stats->tx_packets;
1283 tbytes = stats->tx_bytes;
57a7744e 1284 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
83a27052
ED
1285
1286 do {
57a7744e 1287 start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
3fa2a1df 1288 rpackets = stats->rx_packets;
1289 rbytes = stats->rx_bytes;
57a7744e 1290 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
3fa2a1df 1291
1292 tot->rx_packets += rpackets;
1293 tot->tx_packets += tpackets;
1294 tot->rx_bytes += rbytes;
1295 tot->tx_bytes += tbytes;
1296 }
1297
1298 tot->tx_dropped = dev->stats.tx_dropped;
021ac8d3 1299 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
3fa2a1df 1300 tot->rx_dropped = dev->stats.rx_dropped;
1301 tot->rx_length_errors = dev->stats.rx_length_errors;
1302 tot->rx_frame_errors = dev->stats.rx_frame_errors;
3fa2a1df 1303}
1304
da74e89d
AS
1305#ifdef CONFIG_NET_POLL_CONTROLLER
1306static void virtnet_netpoll(struct net_device *dev)
1307{
1308 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d 1309 int i;
da74e89d 1310
986a4f4d
JW
1311 for (i = 0; i < vi->curr_queue_pairs; i++)
1312 napi_schedule(&vi->rq[i].napi);
da74e89d
AS
1313}
1314#endif
1315
586d17c5
JW
1316static void virtnet_ack_link_announce(struct virtnet_info *vi)
1317{
1318 rtnl_lock();
1319 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
d24bae32 1320 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
586d17c5
JW
1321 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1322 rtnl_unlock();
1323}
1324
47315329 1325static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
986a4f4d
JW
1326{
1327 struct scatterlist sg;
986a4f4d
JW
1328 struct net_device *dev = vi->dev;
1329
1330 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1331 return 0;
1332
a725ee3e
AL
1333 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1334 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
986a4f4d
JW
1335
1336 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
d24bae32 1337 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
986a4f4d
JW
1338 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1339 queue_pairs);
1340 return -EINVAL;
55257d72 1341 } else {
986a4f4d 1342 vi->curr_queue_pairs = queue_pairs;
35ed159b
JW
1343 /* virtnet_open() will refill when device is going to up. */
1344 if (dev->flags & IFF_UP)
1345 schedule_delayed_work(&vi->refill, 0);
55257d72 1346 }
986a4f4d
JW
1347
1348 return 0;
1349}
1350
47315329
JF
1351static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1352{
1353 int err;
1354
1355 rtnl_lock();
1356 err = _virtnet_set_queues(vi, queue_pairs);
1357 rtnl_unlock();
1358 return err;
1359}
1360
296f96fc
RR
1361static int virtnet_close(struct net_device *dev)
1362{
1363 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d 1364 int i;
296f96fc 1365
b2baed69
RR
1366 /* Make sure refill_work doesn't re-enable napi! */
1367 cancel_delayed_work_sync(&vi->refill);
986a4f4d
JW
1368
1369 for (i = 0; i < vi->max_queue_pairs; i++)
1370 napi_disable(&vi->rq[i].napi);
296f96fc 1371
296f96fc
RR
1372 return 0;
1373}
1374
2af7698e
AW
1375static void virtnet_set_rx_mode(struct net_device *dev)
1376{
1377 struct virtnet_info *vi = netdev_priv(dev);
f565a7c2 1378 struct scatterlist sg[2];
f565a7c2 1379 struct virtio_net_ctrl_mac *mac_data;
ccffad25 1380 struct netdev_hw_addr *ha;
32e7bfc4 1381 int uc_count;
4cd24eaf 1382 int mc_count;
f565a7c2
AW
1383 void *buf;
1384 int i;
2af7698e 1385
788a8b6d 1386 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
2af7698e
AW
1387 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1388 return;
1389
2ac46030
MT
1390 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1391 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2af7698e 1392
2ac46030 1393 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
2af7698e
AW
1394
1395 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
d24bae32 1396 VIRTIO_NET_CTRL_RX_PROMISC, sg))
2af7698e 1397 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
2ac46030 1398 vi->ctrl_promisc ? "en" : "dis");
2af7698e 1399
2ac46030 1400 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
2af7698e
AW
1401
1402 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
d24bae32 1403 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2af7698e 1404 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2ac46030 1405 vi->ctrl_allmulti ? "en" : "dis");
f565a7c2 1406
32e7bfc4 1407 uc_count = netdev_uc_count(dev);
4cd24eaf 1408 mc_count = netdev_mc_count(dev);
f565a7c2 1409 /* MAC filter - use one buffer for both lists */
4cd24eaf
JP
1410 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1411 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1412 mac_data = buf;
e68ed8f0 1413 if (!buf)
f565a7c2 1414 return;
f565a7c2 1415
23e258e1
AW
1416 sg_init_table(sg, 2);
1417
f565a7c2 1418 /* Store the unicast list and count in the front of the buffer */
fdd819b2 1419 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
ccffad25 1420 i = 0;
32e7bfc4 1421 netdev_for_each_uc_addr(ha, dev)
ccffad25 1422 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
f565a7c2
AW
1423
1424 sg_set_buf(&sg[0], mac_data,
32e7bfc4 1425 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
f565a7c2
AW
1426
1427 /* multicast list and count fill the end */
32e7bfc4 1428 mac_data = (void *)&mac_data->macs[uc_count][0];
f565a7c2 1429
fdd819b2 1430 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
567ec874 1431 i = 0;
22bedad3
JP
1432 netdev_for_each_mc_addr(ha, dev)
1433 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
f565a7c2
AW
1434
1435 sg_set_buf(&sg[1], mac_data,
4cd24eaf 1436 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
f565a7c2
AW
1437
1438 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
d24bae32 1439 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
99e872ae 1440 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
f565a7c2
AW
1441
1442 kfree(buf);
2af7698e
AW
1443}
1444
80d5c368
PM
1445static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1446 __be16 proto, u16 vid)
0bde9569
AW
1447{
1448 struct virtnet_info *vi = netdev_priv(dev);
1449 struct scatterlist sg;
1450
a725ee3e
AL
1451 vi->ctrl_vid = vid;
1452 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
0bde9569
AW
1453
1454 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
d24bae32 1455 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
0bde9569 1456 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
8e586137 1457 return 0;
0bde9569
AW
1458}
1459
80d5c368
PM
1460static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1461 __be16 proto, u16 vid)
0bde9569
AW
1462{
1463 struct virtnet_info *vi = netdev_priv(dev);
1464 struct scatterlist sg;
1465
a725ee3e
AL
1466 vi->ctrl_vid = vid;
1467 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
0bde9569
AW
1468
1469 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
d24bae32 1470 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
0bde9569 1471 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
8e586137 1472 return 0;
0bde9569
AW
1473}
1474
8898c21c 1475static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
986a4f4d
JW
1476{
1477 int i;
1478
8898c21c
WG
1479 if (vi->affinity_hint_set) {
1480 for (i = 0; i < vi->max_queue_pairs; i++) {
47be2479
WG
1481 virtqueue_set_affinity(vi->rq[i].vq, -1);
1482 virtqueue_set_affinity(vi->sq[i].vq, -1);
1483 }
1484
8898c21c
WG
1485 vi->affinity_hint_set = false;
1486 }
8898c21c 1487}
47be2479 1488
8898c21c
WG
1489static void virtnet_set_affinity(struct virtnet_info *vi)
1490{
1491 int i;
1492 int cpu;
986a4f4d
JW
1493
1494 /* In multiqueue mode, when the number of cpu is equal to the number of
1495 * queue pairs, we let the queue pairs to be private to one cpu by
1496 * setting the affinity hint to eliminate the contention.
1497 */
8898c21c
WG
1498 if (vi->curr_queue_pairs == 1 ||
1499 vi->max_queue_pairs != num_online_cpus()) {
1500 virtnet_clean_affinity(vi, -1);
1501 return;
986a4f4d
JW
1502 }
1503
8898c21c
WG
1504 i = 0;
1505 for_each_online_cpu(cpu) {
986a4f4d
JW
1506 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1507 virtqueue_set_affinity(vi->sq[i].vq, cpu);
9bb8ca86 1508 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
8898c21c 1509 i++;
986a4f4d
JW
1510 }
1511
8898c21c 1512 vi->affinity_hint_set = true;
986a4f4d
JW
1513}
1514
8017c279 1515static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
8de4b2f3 1516{
8017c279
SAS
1517 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1518 node);
1519 virtnet_set_affinity(vi);
1520 return 0;
1521}
8de4b2f3 1522
8017c279
SAS
1523static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1524{
1525 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1526 node_dead);
1527 virtnet_set_affinity(vi);
1528 return 0;
1529}
3ab098df 1530
8017c279
SAS
1531static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1532{
1533 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1534 node);
1535
1536 virtnet_clean_affinity(vi, cpu);
1537 return 0;
1538}
1539
1540static enum cpuhp_state virtionet_online;
1541
1542static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1543{
1544 int ret;
1545
1546 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1547 if (ret)
1548 return ret;
1549 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1550 &vi->node_dead);
1551 if (!ret)
1552 return ret;
1553 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1554 return ret;
1555}
1556
1557static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1558{
1559 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1560 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1561 &vi->node_dead);
986a4f4d
JW
1562}
1563
8f9f4668
RJ
1564static void virtnet_get_ringparam(struct net_device *dev,
1565 struct ethtool_ringparam *ring)
1566{
1567 struct virtnet_info *vi = netdev_priv(dev);
1568
986a4f4d
JW
1569 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1570 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
8f9f4668
RJ
1571 ring->rx_pending = ring->rx_max_pending;
1572 ring->tx_pending = ring->tx_max_pending;
8f9f4668
RJ
1573}
1574
66846048
RJ
1575
1576static void virtnet_get_drvinfo(struct net_device *dev,
1577 struct ethtool_drvinfo *info)
1578{
1579 struct virtnet_info *vi = netdev_priv(dev);
1580 struct virtio_device *vdev = vi->vdev;
1581
1582 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1583 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1584 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1585
1586}
1587
d73bcd2c
JW
1588/* TODO: Eliminate OOO packets during switching */
1589static int virtnet_set_channels(struct net_device *dev,
1590 struct ethtool_channels *channels)
1591{
1592 struct virtnet_info *vi = netdev_priv(dev);
1593 u16 queue_pairs = channels->combined_count;
1594 int err;
1595
1596 /* We don't support separate rx/tx channels.
1597 * We don't allow setting 'other' channels.
1598 */
1599 if (channels->rx_count || channels->tx_count || channels->other_count)
1600 return -EINVAL;
1601
c18e9cd6 1602 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
d73bcd2c
JW
1603 return -EINVAL;
1604
f600b690
JF
1605 /* For now we don't support modifying channels while XDP is loaded
1606 * also when XDP is loaded all RX queues have XDP programs so we only
1607 * need to check a single RX queue.
1608 */
1609 if (vi->rq[0].xdp_prog)
1610 return -EINVAL;
1611
47be2479 1612 get_online_cpus();
47315329 1613 err = _virtnet_set_queues(vi, queue_pairs);
d73bcd2c
JW
1614 if (!err) {
1615 netif_set_real_num_tx_queues(dev, queue_pairs);
1616 netif_set_real_num_rx_queues(dev, queue_pairs);
1617
8898c21c 1618 virtnet_set_affinity(vi);
d73bcd2c 1619 }
47be2479 1620 put_online_cpus();
d73bcd2c
JW
1621
1622 return err;
1623}
1624
1625static void virtnet_get_channels(struct net_device *dev,
1626 struct ethtool_channels *channels)
1627{
1628 struct virtnet_info *vi = netdev_priv(dev);
1629
1630 channels->combined_count = vi->curr_queue_pairs;
1631 channels->max_combined = vi->max_queue_pairs;
1632 channels->max_other = 0;
1633 channels->rx_count = 0;
1634 channels->tx_count = 0;
1635 channels->other_count = 0;
1636}
1637
16032be5
NA
1638/* Check if the user is trying to change anything besides speed/duplex */
1639static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
1640{
1641 struct ethtool_cmd diff1 = *cmd;
1642 struct ethtool_cmd diff2 = {};
1643
0cf3ace9
NA
1644 /* cmd is always set so we need to clear it, validate the port type
1645 * and also without autonegotiation we can ignore advertising
1646 */
16032be5 1647 ethtool_cmd_speed_set(&diff1, 0);
0cf3ace9 1648 diff2.port = PORT_OTHER;
16032be5
NA
1649 diff1.advertising = 0;
1650 diff1.duplex = 0;
16032be5
NA
1651 diff1.cmd = 0;
1652
1653 return !memcmp(&diff1, &diff2, sizeof(diff1));
1654}
1655
1656static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1657{
1658 struct virtnet_info *vi = netdev_priv(dev);
1659 u32 speed;
1660
1661 speed = ethtool_cmd_speed(cmd);
1662 /* don't allow custom speed and duplex */
1663 if (!ethtool_validate_speed(speed) ||
1664 !ethtool_validate_duplex(cmd->duplex) ||
1665 !virtnet_validate_ethtool_cmd(cmd))
1666 return -EINVAL;
1667 vi->speed = speed;
1668 vi->duplex = cmd->duplex;
1669
1670 return 0;
1671}
1672
1673static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1674{
1675 struct virtnet_info *vi = netdev_priv(dev);
1676
1677 ethtool_cmd_speed_set(cmd, vi->speed);
1678 cmd->duplex = vi->duplex;
1679 cmd->port = PORT_OTHER;
1680
1681 return 0;
1682}
1683
1684static void virtnet_init_settings(struct net_device *dev)
1685{
1686 struct virtnet_info *vi = netdev_priv(dev);
1687
1688 vi->speed = SPEED_UNKNOWN;
1689 vi->duplex = DUPLEX_UNKNOWN;
1690}
1691
0fc0b732 1692static const struct ethtool_ops virtnet_ethtool_ops = {
66846048 1693 .get_drvinfo = virtnet_get_drvinfo,
9f4d26d0 1694 .get_link = ethtool_op_get_link,
8f9f4668 1695 .get_ringparam = virtnet_get_ringparam,
d73bcd2c
JW
1696 .set_channels = virtnet_set_channels,
1697 .get_channels = virtnet_get_channels,
074c3582 1698 .get_ts_info = ethtool_op_get_ts_info,
16032be5
NA
1699 .get_settings = virtnet_get_settings,
1700 .set_settings = virtnet_set_settings,
a9ea3fc6
HX
1701};
1702
9fe7bfce
JF
1703static void virtnet_freeze_down(struct virtio_device *vdev)
1704{
1705 struct virtnet_info *vi = vdev->priv;
1706 int i;
1707
1708 /* Make sure no work handler is accessing the device */
1709 flush_work(&vi->config_work);
1710
1711 netif_device_detach(vi->dev);
1712 cancel_delayed_work_sync(&vi->refill);
1713
1714 if (netif_running(vi->dev)) {
1715 for (i = 0; i < vi->max_queue_pairs; i++)
1716 napi_disable(&vi->rq[i].napi);
1717 }
1718}
1719
1720static int init_vqs(struct virtnet_info *vi);
2de2f7f4 1721static void _remove_vq_common(struct virtnet_info *vi);
9fe7bfce
JF
1722
1723static int virtnet_restore_up(struct virtio_device *vdev)
1724{
1725 struct virtnet_info *vi = vdev->priv;
1726 int err, i;
1727
1728 err = init_vqs(vi);
1729 if (err)
1730 return err;
1731
1732 virtio_device_ready(vdev);
1733
1734 if (netif_running(vi->dev)) {
1735 for (i = 0; i < vi->curr_queue_pairs; i++)
1736 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1737 schedule_delayed_work(&vi->refill, 0);
1738
1739 for (i = 0; i < vi->max_queue_pairs; i++)
1740 virtnet_napi_enable(&vi->rq[i]);
1741 }
1742
1743 netif_device_attach(vi->dev);
1744 return err;
1745}
1746
017b29c3 1747static int virtnet_reset(struct virtnet_info *vi, int curr_qp, int xdp_qp)
2de2f7f4
JF
1748{
1749 struct virtio_device *dev = vi->vdev;
1750 int ret;
1751
1752 virtio_config_disable(dev);
1753 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
1754 virtnet_freeze_down(dev);
1755 _remove_vq_common(vi);
1756
1757 dev->config->reset(dev);
1758 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
1759 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
1760
1761 ret = virtio_finalize_features(dev);
1762 if (ret)
1763 goto err;
1764
017b29c3 1765 vi->xdp_queue_pairs = xdp_qp;
2de2f7f4
JF
1766 ret = virtnet_restore_up(dev);
1767 if (ret)
1768 goto err;
017b29c3 1769 ret = _virtnet_set_queues(vi, curr_qp);
2de2f7f4
JF
1770 if (ret)
1771 goto err;
1772
1773 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
1774 virtio_config_enable(dev);
1775 return 0;
1776err:
1777 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
1778 return ret;
1779}
1780
f600b690
JF
1781static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1782{
1783 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1784 struct virtnet_info *vi = netdev_priv(dev);
1785 struct bpf_prog *old_prog;
017b29c3 1786 u16 xdp_qp = 0, curr_qp;
672aafd5 1787 int i, err;
f600b690
JF
1788
1789 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
92502fe8
JW
1790 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1791 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1792 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
f600b690
JF
1793 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1794 return -EOPNOTSUPP;
1795 }
1796
1797 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1798 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1799 return -EINVAL;
1800 }
1801
1802 if (dev->mtu > max_sz) {
1803 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1804 return -EINVAL;
1805 }
1806
672aafd5
JF
1807 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1808 if (prog)
1809 xdp_qp = nr_cpu_ids;
1810
1811 /* XDP requires extra queues for XDP_TX */
1812 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1813 netdev_warn(dev, "request %i queues but max is %i\n",
1814 curr_qp + xdp_qp, vi->max_queue_pairs);
1815 return -ENOMEM;
1816 }
1817
2de2f7f4
JF
1818 if (prog) {
1819 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
1820 if (IS_ERR(prog))
1821 return PTR_ERR(prog);
1822 }
1823
2de2f7f4
JF
1824 /* Changing the headroom in buffers is a disruptive operation because
1825 * existing buffers must be flushed and reallocated. This will happen
1826 * when a xdp program is initially added or xdp is disabled by removing
1827 * the xdp program resulting in number of XDP queues changing.
1828 */
1829 if (vi->xdp_queue_pairs != xdp_qp) {
017b29c3
JW
1830 err = virtnet_reset(vi, curr_qp + xdp_qp, xdp_qp);
1831 if (err) {
1832 dev_warn(&dev->dev, "XDP reset failure.\n");
2de2f7f4 1833 goto virtio_reset_err;
017b29c3 1834 }
f600b690
JF
1835 }
1836
672aafd5
JF
1837 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1838
f600b690
JF
1839 for (i = 0; i < vi->max_queue_pairs; i++) {
1840 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1841 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1842 if (old_prog)
1843 bpf_prog_put(old_prog);
1844 }
1845
1846 return 0;
2de2f7f4
JF
1847
1848virtio_reset_err:
1849 /* On reset error do our best to unwind XDP changes inflight and return
1850 * error up to user space for resolution. The underlying reset hung on
1851 * us so not much we can do here.
1852 */
2de2f7f4
JF
1853 if (prog)
1854 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
1855 return err;
f600b690
JF
1856}
1857
1858static bool virtnet_xdp_query(struct net_device *dev)
1859{
1860 struct virtnet_info *vi = netdev_priv(dev);
1861 int i;
1862
1863 for (i = 0; i < vi->max_queue_pairs; i++) {
1864 if (vi->rq[i].xdp_prog)
1865 return true;
1866 }
1867 return false;
1868}
1869
1870static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1871{
1872 switch (xdp->command) {
1873 case XDP_SETUP_PROG:
1874 return virtnet_xdp_set(dev, xdp->prog);
1875 case XDP_QUERY_PROG:
1876 xdp->prog_attached = virtnet_xdp_query(dev);
1877 return 0;
1878 default:
1879 return -EINVAL;
1880 }
1881}
1882
76288b4e
SH
1883static const struct net_device_ops virtnet_netdev = {
1884 .ndo_open = virtnet_open,
1885 .ndo_stop = virtnet_close,
1886 .ndo_start_xmit = start_xmit,
1887 .ndo_validate_addr = eth_validate_addr,
9c46f6d4 1888 .ndo_set_mac_address = virtnet_set_mac_address,
2af7698e 1889 .ndo_set_rx_mode = virtnet_set_rx_mode,
3fa2a1df 1890 .ndo_get_stats64 = virtnet_stats,
1824a989
AW
1891 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1892 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
76288b4e
SH
1893#ifdef CONFIG_NET_POLL_CONTROLLER
1894 .ndo_poll_controller = virtnet_netpoll,
91815639 1895#endif
f600b690 1896 .ndo_xdp = virtnet_xdp,
76288b4e
SH
1897};
1898
586d17c5 1899static void virtnet_config_changed_work(struct work_struct *work)
9f4d26d0 1900{
586d17c5
JW
1901 struct virtnet_info *vi =
1902 container_of(work, struct virtnet_info, config_work);
9f4d26d0
MM
1903 u16 v;
1904
855e0c52
RR
1905 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1906 struct virtio_net_config, status, &v) < 0)
507613bf 1907 return;
586d17c5
JW
1908
1909 if (v & VIRTIO_NET_S_ANNOUNCE) {
ee89bab1 1910 netdev_notify_peers(vi->dev);
586d17c5
JW
1911 virtnet_ack_link_announce(vi);
1912 }
9f4d26d0
MM
1913
1914 /* Ignore unknown (future) status bits */
1915 v &= VIRTIO_NET_S_LINK_UP;
1916
1917 if (vi->status == v)
507613bf 1918 return;
9f4d26d0
MM
1919
1920 vi->status = v;
1921
1922 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1923 netif_carrier_on(vi->dev);
986a4f4d 1924 netif_tx_wake_all_queues(vi->dev);
9f4d26d0
MM
1925 } else {
1926 netif_carrier_off(vi->dev);
986a4f4d 1927 netif_tx_stop_all_queues(vi->dev);
9f4d26d0
MM
1928 }
1929}
1930
1931static void virtnet_config_changed(struct virtio_device *vdev)
1932{
1933 struct virtnet_info *vi = vdev->priv;
1934
3b07e9ca 1935 schedule_work(&vi->config_work);
9f4d26d0
MM
1936}
1937
986a4f4d
JW
1938static void virtnet_free_queues(struct virtnet_info *vi)
1939{
d4fb84ee
AV
1940 int i;
1941
ab3971b1
JW
1942 for (i = 0; i < vi->max_queue_pairs; i++) {
1943 napi_hash_del(&vi->rq[i].napi);
d4fb84ee 1944 netif_napi_del(&vi->rq[i].napi);
ab3971b1 1945 }
d4fb84ee 1946
963abe5c
ED
1947 /* We called napi_hash_del() before netif_napi_del(),
1948 * we need to respect an RCU grace period before freeing vi->rq
1949 */
1950 synchronize_net();
1951
986a4f4d
JW
1952 kfree(vi->rq);
1953 kfree(vi->sq);
1954}
1955
47315329 1956static void _free_receive_bufs(struct virtnet_info *vi)
986a4f4d 1957{
f600b690 1958 struct bpf_prog *old_prog;
986a4f4d
JW
1959 int i;
1960
1961 for (i = 0; i < vi->max_queue_pairs; i++) {
1962 while (vi->rq[i].pages)
1963 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
f600b690
JF
1964
1965 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1966 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1967 if (old_prog)
1968 bpf_prog_put(old_prog);
986a4f4d 1969 }
47315329
JF
1970}
1971
1972static void free_receive_bufs(struct virtnet_info *vi)
1973{
1974 rtnl_lock();
1975 _free_receive_bufs(vi);
f600b690 1976 rtnl_unlock();
986a4f4d
JW
1977}
1978
fb51879d
MD
1979static void free_receive_page_frags(struct virtnet_info *vi)
1980{
1981 int i;
1982 for (i = 0; i < vi->max_queue_pairs; i++)
1983 if (vi->rq[i].alloc_frag.page)
1984 put_page(vi->rq[i].alloc_frag.page);
1985}
1986
b68df015 1987static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
56434a01
JF
1988{
1989 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1990 return false;
1991 else if (q < vi->curr_queue_pairs)
1992 return true;
1993 else
1994 return false;
1995}
1996
986a4f4d
JW
1997static void free_unused_bufs(struct virtnet_info *vi)
1998{
1999 void *buf;
2000 int i;
2001
2002 for (i = 0; i < vi->max_queue_pairs; i++) {
2003 struct virtqueue *vq = vi->sq[i].vq;
56434a01 2004 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
b68df015 2005 if (!is_xdp_raw_buffer_queue(vi, i))
56434a01
JF
2006 dev_kfree_skb(buf);
2007 else
2008 put_page(virt_to_head_page(buf));
2009 }
986a4f4d
JW
2010 }
2011
2012 for (i = 0; i < vi->max_queue_pairs; i++) {
2013 struct virtqueue *vq = vi->rq[i].vq;
2014
2015 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
ab7db917
MD
2016 if (vi->mergeable_rx_bufs) {
2017 unsigned long ctx = (unsigned long)buf;
2018 void *base = mergeable_ctx_to_buf_address(ctx);
2019 put_page(virt_to_head_page(base));
2020 } else if (vi->big_packets) {
fa9fac17 2021 give_pages(&vi->rq[i], buf);
ab7db917 2022 } else {
f6b10209 2023 put_page(virt_to_head_page(buf));
ab7db917 2024 }
986a4f4d 2025 }
986a4f4d
JW
2026 }
2027}
2028
e9d7417b
JW
2029static void virtnet_del_vqs(struct virtnet_info *vi)
2030{
2031 struct virtio_device *vdev = vi->vdev;
2032
8898c21c 2033 virtnet_clean_affinity(vi, -1);
986a4f4d 2034
e9d7417b 2035 vdev->config->del_vqs(vdev);
986a4f4d
JW
2036
2037 virtnet_free_queues(vi);
e9d7417b
JW
2038}
2039
986a4f4d 2040static int virtnet_find_vqs(struct virtnet_info *vi)
3f9c10b0 2041{
986a4f4d
JW
2042 vq_callback_t **callbacks;
2043 struct virtqueue **vqs;
2044 int ret = -ENOMEM;
2045 int i, total_vqs;
2046 const char **names;
2047
2048 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2049 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2050 * possible control vq.
2051 */
2052 total_vqs = vi->max_queue_pairs * 2 +
2053 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2054
2055 /* Allocate space for find_vqs parameters */
2056 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
2057 if (!vqs)
2058 goto err_vq;
2059 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
2060 if (!callbacks)
2061 goto err_callback;
2062 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
2063 if (!names)
2064 goto err_names;
2065
2066 /* Parameters for control virtqueue, if any */
2067 if (vi->has_cvq) {
2068 callbacks[total_vqs - 1] = NULL;
2069 names[total_vqs - 1] = "control";
2070 }
3f9c10b0 2071
986a4f4d
JW
2072 /* Allocate/initialize parameters for send/receive virtqueues */
2073 for (i = 0; i < vi->max_queue_pairs; i++) {
2074 callbacks[rxq2vq(i)] = skb_recv_done;
2075 callbacks[txq2vq(i)] = skb_xmit_done;
2076 sprintf(vi->rq[i].name, "input.%d", i);
2077 sprintf(vi->sq[i].name, "output.%d", i);
2078 names[rxq2vq(i)] = vi->rq[i].name;
2079 names[txq2vq(i)] = vi->sq[i].name;
2080 }
3f9c10b0 2081
986a4f4d 2082 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
fb5e31d9 2083 names, NULL);
986a4f4d
JW
2084 if (ret)
2085 goto err_find;
3f9c10b0 2086
986a4f4d
JW
2087 if (vi->has_cvq) {
2088 vi->cvq = vqs[total_vqs - 1];
3f9c10b0 2089 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
f646968f 2090 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3f9c10b0 2091 }
986a4f4d
JW
2092
2093 for (i = 0; i < vi->max_queue_pairs; i++) {
2094 vi->rq[i].vq = vqs[rxq2vq(i)];
2095 vi->sq[i].vq = vqs[txq2vq(i)];
2096 }
2097
2098 kfree(names);
2099 kfree(callbacks);
2100 kfree(vqs);
2101
3f9c10b0 2102 return 0;
986a4f4d
JW
2103
2104err_find:
2105 kfree(names);
2106err_names:
2107 kfree(callbacks);
2108err_callback:
2109 kfree(vqs);
2110err_vq:
2111 return ret;
2112}
2113
2114static int virtnet_alloc_queues(struct virtnet_info *vi)
2115{
2116 int i;
2117
2118 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2119 if (!vi->sq)
2120 goto err_sq;
2121 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
008d4278 2122 if (!vi->rq)
986a4f4d
JW
2123 goto err_rq;
2124
2125 INIT_DELAYED_WORK(&vi->refill, refill_work);
2126 for (i = 0; i < vi->max_queue_pairs; i++) {
2127 vi->rq[i].pages = NULL;
2128 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2129 napi_weight);
2130
2131 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
5377d758 2132 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
986a4f4d
JW
2133 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2134 }
2135
2136 return 0;
2137
2138err_rq:
2139 kfree(vi->sq);
2140err_sq:
2141 return -ENOMEM;
2142}
2143
2144static int init_vqs(struct virtnet_info *vi)
2145{
2146 int ret;
2147
2148 /* Allocate send & receive queues */
2149 ret = virtnet_alloc_queues(vi);
2150 if (ret)
2151 goto err;
2152
2153 ret = virtnet_find_vqs(vi);
2154 if (ret)
2155 goto err_free;
2156
47be2479 2157 get_online_cpus();
8898c21c 2158 virtnet_set_affinity(vi);
47be2479
WG
2159 put_online_cpus();
2160
986a4f4d
JW
2161 return 0;
2162
2163err_free:
2164 virtnet_free_queues(vi);
2165err:
2166 return ret;
3f9c10b0
AS
2167}
2168
fbf28d78
MD
2169#ifdef CONFIG_SYSFS
2170static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2171 struct rx_queue_attribute *attribute, char *buf)
2172{
2173 struct virtnet_info *vi = netdev_priv(queue->dev);
2174 unsigned int queue_index = get_netdev_rx_queue_index(queue);
5377d758 2175 struct ewma_pkt_len *avg;
fbf28d78
MD
2176
2177 BUG_ON(queue_index >= vi->max_queue_pairs);
2178 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2179 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2180}
2181
2182static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2183 __ATTR_RO(mergeable_rx_buffer_size);
2184
2185static struct attribute *virtio_net_mrg_rx_attrs[] = {
2186 &mergeable_rx_buffer_size_attribute.attr,
2187 NULL
2188};
2189
2190static const struct attribute_group virtio_net_mrg_rx_group = {
2191 .name = "virtio_net",
2192 .attrs = virtio_net_mrg_rx_attrs
2193};
2194#endif
2195
892d6eb1
JW
2196static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2197 unsigned int fbit,
2198 const char *fname, const char *dname)
2199{
2200 if (!virtio_has_feature(vdev, fbit))
2201 return false;
2202
2203 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2204 fname, dname);
2205
2206 return true;
2207}
2208
2209#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2210 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2211
2212static bool virtnet_validate_features(struct virtio_device *vdev)
2213{
2214 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2215 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2216 "VIRTIO_NET_F_CTRL_VQ") ||
2217 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2218 "VIRTIO_NET_F_CTRL_VQ") ||
2219 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2220 "VIRTIO_NET_F_CTRL_VQ") ||
2221 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2222 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2223 "VIRTIO_NET_F_CTRL_VQ"))) {
2224 return false;
2225 }
2226
2227 return true;
2228}
2229
d0c2c997
JW
2230#define MIN_MTU ETH_MIN_MTU
2231#define MAX_MTU ETH_MAX_MTU
2232
296f96fc
RR
2233static int virtnet_probe(struct virtio_device *vdev)
2234{
986a4f4d 2235 int i, err;
296f96fc
RR
2236 struct net_device *dev;
2237 struct virtnet_info *vi;
986a4f4d 2238 u16 max_queue_pairs;
14de9d11 2239 int mtu;
986a4f4d 2240
6ba42248
MT
2241 if (!vdev->config->get) {
2242 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2243 __func__);
2244 return -EINVAL;
2245 }
2246
892d6eb1
JW
2247 if (!virtnet_validate_features(vdev))
2248 return -EINVAL;
2249
986a4f4d 2250 /* Find if host supports multiqueue virtio_net device */
855e0c52
RR
2251 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2252 struct virtio_net_config,
2253 max_virtqueue_pairs, &max_queue_pairs);
986a4f4d
JW
2254
2255 /* We need at least 2 queue's */
2256 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2257 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2258 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2259 max_queue_pairs = 1;
296f96fc
RR
2260
2261 /* Allocate ourselves a network device with room for our info */
986a4f4d 2262 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
296f96fc
RR
2263 if (!dev)
2264 return -ENOMEM;
2265
2266 /* Set up network device as normal. */
f2f2c8b4 2267 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
76288b4e 2268 dev->netdev_ops = &virtnet_netdev;
296f96fc 2269 dev->features = NETIF_F_HIGHDMA;
3fa2a1df 2270
7ad24ea4 2271 dev->ethtool_ops = &virtnet_ethtool_ops;
296f96fc
RR
2272 SET_NETDEV_DEV(dev, &vdev->dev);
2273
2274 /* Do we support "hardware" checksums? */
98e778c9 2275 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
296f96fc 2276 /* This opens up the world of extra features. */
48900cb6 2277 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
98e778c9 2278 if (csum)
48900cb6 2279 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
98e778c9
MM
2280
2281 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
e3e3c423 2282 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
34a48579
RR
2283 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2284 }
5539ae96 2285 /* Individual feature bits: what can host handle? */
98e778c9
MM
2286 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2287 dev->hw_features |= NETIF_F_TSO;
2288 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2289 dev->hw_features |= NETIF_F_TSO6;
2290 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2291 dev->hw_features |= NETIF_F_TSO_ECN;
e3e3c423
VY
2292 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2293 dev->hw_features |= NETIF_F_UFO;
98e778c9 2294
41f2f127
JW
2295 dev->features |= NETIF_F_GSO_ROBUST;
2296
98e778c9 2297 if (gso)
e3e3c423 2298 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
98e778c9 2299 /* (!csum && gso) case will be fixed by register_netdev() */
296f96fc 2300 }
4f49129b
TH
2301 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2302 dev->features |= NETIF_F_RXCSUM;
296f96fc 2303
4fda8302
JW
2304 dev->vlan_features = dev->features;
2305
d0c2c997
JW
2306 /* MTU range: 68 - 65535 */
2307 dev->min_mtu = MIN_MTU;
2308 dev->max_mtu = MAX_MTU;
2309
296f96fc 2310 /* Configuration may specify what MAC to use. Otherwise random. */
855e0c52
RR
2311 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2312 virtio_cread_bytes(vdev,
2313 offsetof(struct virtio_net_config, mac),
2314 dev->dev_addr, dev->addr_len);
2315 else
f2cedb63 2316 eth_hw_addr_random(dev);
296f96fc
RR
2317
2318 /* Set up our device-specific information */
2319 vi = netdev_priv(dev);
296f96fc
RR
2320 vi->dev = dev;
2321 vi->vdev = vdev;
d9d5dcc8 2322 vdev->priv = vi;
3fa2a1df 2323 vi->stats = alloc_percpu(struct virtnet_stats);
2324 err = -ENOMEM;
2325 if (vi->stats == NULL)
2326 goto free;
2327
827da44c
JS
2328 for_each_possible_cpu(i) {
2329 struct virtnet_stats *virtnet_stats;
2330 virtnet_stats = per_cpu_ptr(vi->stats, i);
2331 u64_stats_init(&virtnet_stats->tx_syncp);
2332 u64_stats_init(&virtnet_stats->rx_syncp);
2333 }
2334
586d17c5 2335 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
296f96fc 2336
97402b96 2337 /* If we can receive ANY GSO packets, we must allocate large ones. */
8e95a202
JP
2338 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2339 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
e3e3c423
VY
2340 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2341 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
97402b96
HX
2342 vi->big_packets = true;
2343
3f2c31d9
MM
2344 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2345 vi->mergeable_rx_bufs = true;
2346
d04302b3
MT
2347 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2348 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
012873d0
MT
2349 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2350 else
2351 vi->hdr_len = sizeof(struct virtio_net_hdr);
2352
75993300
MT
2353 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2354 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
e7428e95
MT
2355 vi->any_header_sg = true;
2356
986a4f4d
JW
2357 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2358 vi->has_cvq = true;
2359
14de9d11
AC
2360 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2361 mtu = virtio_cread16(vdev,
2362 offsetof(struct virtio_net_config,
2363 mtu));
93a205ee 2364 if (mtu < dev->min_mtu) {
14de9d11 2365 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
93a205ee 2366 } else {
d0c2c997 2367 dev->mtu = mtu;
93a205ee
AC
2368 dev->max_mtu = mtu;
2369 }
14de9d11
AC
2370 }
2371
012873d0
MT
2372 if (vi->any_header_sg)
2373 dev->needed_headroom = vi->hdr_len;
6ebbc1a6 2374
44900010
JW
2375 /* Enable multiqueue by default */
2376 if (num_online_cpus() >= max_queue_pairs)
2377 vi->curr_queue_pairs = max_queue_pairs;
2378 else
2379 vi->curr_queue_pairs = num_online_cpus();
986a4f4d
JW
2380 vi->max_queue_pairs = max_queue_pairs;
2381
2382 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
3f9c10b0 2383 err = init_vqs(vi);
d2a7ddda 2384 if (err)
9bb8ca86 2385 goto free_stats;
296f96fc 2386
fbf28d78
MD
2387#ifdef CONFIG_SYSFS
2388 if (vi->mergeable_rx_bufs)
2389 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2390#endif
0f13b66b
ZYW
2391 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2392 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
986a4f4d 2393
16032be5
NA
2394 virtnet_init_settings(dev);
2395
296f96fc
RR
2396 err = register_netdev(dev);
2397 if (err) {
2398 pr_debug("virtio_net: registering device failed\n");
d2a7ddda 2399 goto free_vqs;
296f96fc 2400 }
b3369c1f 2401
4baf1e33
MT
2402 virtio_device_ready(vdev);
2403
8017c279 2404 err = virtnet_cpu_notif_add(vi);
8de4b2f3
WG
2405 if (err) {
2406 pr_debug("virtio_net: registering cpu notifier failed\n");
f00e35e2 2407 goto free_unregister_netdev;
8de4b2f3
WG
2408 }
2409
a220871b 2410 virtnet_set_queues(vi, vi->curr_queue_pairs);
44900010 2411
167c25e4
JW
2412 /* Assume link up if device can't report link status,
2413 otherwise get link status from config. */
2414 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2415 netif_carrier_off(dev);
3b07e9ca 2416 schedule_work(&vi->config_work);
167c25e4
JW
2417 } else {
2418 vi->status = VIRTIO_NET_S_LINK_UP;
2419 netif_carrier_on(dev);
2420 }
9f4d26d0 2421
986a4f4d
JW
2422 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2423 dev->name, max_queue_pairs);
2424
296f96fc
RR
2425 return 0;
2426
f00e35e2 2427free_unregister_netdev:
02465555
MT
2428 vi->vdev->config->reset(vdev);
2429
b3369c1f 2430 unregister_netdev(dev);
d2a7ddda 2431free_vqs:
986a4f4d 2432 cancel_delayed_work_sync(&vi->refill);
fb51879d 2433 free_receive_page_frags(vi);
e9d7417b 2434 virtnet_del_vqs(vi);
3fa2a1df 2435free_stats:
2436 free_percpu(vi->stats);
296f96fc
RR
2437free:
2438 free_netdev(dev);
2439 return err;
2440}
2441
2de2f7f4
JF
2442static void _remove_vq_common(struct virtnet_info *vi)
2443{
2444 vi->vdev->config->reset(vi->vdev);
2445 free_unused_bufs(vi);
2446 _free_receive_bufs(vi);
2447 free_receive_page_frags(vi);
2448 virtnet_del_vqs(vi);
2449}
2450
04486ed0 2451static void remove_vq_common(struct virtnet_info *vi)
296f96fc 2452{
04486ed0 2453 vi->vdev->config->reset(vi->vdev);
830a8a97
SM
2454
2455 /* Free unused buffers in both send and recv, if any. */
9ab86bbc 2456 free_unused_bufs(vi);
fb6813f4 2457
986a4f4d 2458 free_receive_bufs(vi);
d2a7ddda 2459
fb51879d
MD
2460 free_receive_page_frags(vi);
2461
986a4f4d 2462 virtnet_del_vqs(vi);
04486ed0
AS
2463}
2464
8cc085d6 2465static void virtnet_remove(struct virtio_device *vdev)
04486ed0
AS
2466{
2467 struct virtnet_info *vi = vdev->priv;
2468
8017c279 2469 virtnet_cpu_notif_remove(vi);
8de4b2f3 2470
102a2786
MT
2471 /* Make sure no work handler is accessing the device. */
2472 flush_work(&vi->config_work);
586d17c5 2473
04486ed0
AS
2474 unregister_netdev(vi->dev);
2475
2476 remove_vq_common(vi);
fb6813f4 2477
2e66f55b 2478 free_percpu(vi->stats);
74b2553f 2479 free_netdev(vi->dev);
296f96fc
RR
2480}
2481
89107000 2482#ifdef CONFIG_PM_SLEEP
0741bcb5
AS
2483static int virtnet_freeze(struct virtio_device *vdev)
2484{
2485 struct virtnet_info *vi = vdev->priv;
2486
8017c279 2487 virtnet_cpu_notif_remove(vi);
9fe7bfce 2488 virtnet_freeze_down(vdev);
0741bcb5
AS
2489 remove_vq_common(vi);
2490
2491 return 0;
2492}
2493
2494static int virtnet_restore(struct virtio_device *vdev)
2495{
2496 struct virtnet_info *vi = vdev->priv;
9fe7bfce 2497 int err;
0741bcb5 2498
9fe7bfce 2499 err = virtnet_restore_up(vdev);
0741bcb5
AS
2500 if (err)
2501 return err;
986a4f4d
JW
2502 virtnet_set_queues(vi, vi->curr_queue_pairs);
2503
8017c279 2504 err = virtnet_cpu_notif_add(vi);
ec9debbd
JW
2505 if (err)
2506 return err;
2507
0741bcb5
AS
2508 return 0;
2509}
2510#endif
2511
296f96fc
RR
2512static struct virtio_device_id id_table[] = {
2513 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2514 { 0 },
2515};
2516
f3358507
MT
2517#define VIRTNET_FEATURES \
2518 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2519 VIRTIO_NET_F_MAC, \
2520 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2521 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2522 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2523 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2524 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2525 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2526 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2527 VIRTIO_NET_F_MTU
2528
c45a6816 2529static unsigned int features[] = {
f3358507
MT
2530 VIRTNET_FEATURES,
2531};
2532
2533static unsigned int features_legacy[] = {
2534 VIRTNET_FEATURES,
2535 VIRTIO_NET_F_GSO,
e7428e95 2536 VIRTIO_F_ANY_LAYOUT,
c45a6816
RR
2537};
2538
22402529 2539static struct virtio_driver virtio_net_driver = {
c45a6816
RR
2540 .feature_table = features,
2541 .feature_table_size = ARRAY_SIZE(features),
f3358507
MT
2542 .feature_table_legacy = features_legacy,
2543 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
296f96fc
RR
2544 .driver.name = KBUILD_MODNAME,
2545 .driver.owner = THIS_MODULE,
2546 .id_table = id_table,
2547 .probe = virtnet_probe,
8cc085d6 2548 .remove = virtnet_remove,
9f4d26d0 2549 .config_changed = virtnet_config_changed,
89107000 2550#ifdef CONFIG_PM_SLEEP
0741bcb5
AS
2551 .freeze = virtnet_freeze,
2552 .restore = virtnet_restore,
2553#endif
296f96fc
RR
2554};
2555
8017c279
SAS
2556static __init int virtio_net_driver_init(void)
2557{
2558 int ret;
2559
73c1b41e 2560 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
8017c279
SAS
2561 virtnet_cpu_online,
2562 virtnet_cpu_down_prep);
2563 if (ret < 0)
2564 goto out;
2565 virtionet_online = ret;
73c1b41e 2566 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
8017c279
SAS
2567 NULL, virtnet_cpu_dead);
2568 if (ret)
2569 goto err_dead;
2570
2571 ret = register_virtio_driver(&virtio_net_driver);
2572 if (ret)
2573 goto err_virtio;
2574 return 0;
2575err_virtio:
2576 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2577err_dead:
2578 cpuhp_remove_multi_state(virtionet_online);
2579out:
2580 return ret;
2581}
2582module_init(virtio_net_driver_init);
2583
2584static __exit void virtio_net_driver_exit(void)
2585{
2586 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2587 cpuhp_remove_multi_state(virtionet_online);
2588 unregister_virtio_driver(&virtio_net_driver);
2589}
2590module_exit(virtio_net_driver_exit);
296f96fc
RR
2591
2592MODULE_DEVICE_TABLE(virtio, id_table);
2593MODULE_DESCRIPTION("Virtio network driver");
2594MODULE_LICENSE("GPL");