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