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