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