1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* A network driver using virtio.
3 *
4 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
5 */
6 //#define DEBUG
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/ethtool.h>
10 #include <linux/module.h>
11 #include <linux/virtio.h>
12 #include <linux/virtio_net.h>
13 #include <linux/bpf.h>
14 #include <linux/bpf_trace.h>
15 #include <linux/scatterlist.h>
16 #include <linux/if_vlan.h>
17 #include <linux/slab.h>
18 #include <linux/cpu.h>
19 #include <linux/average.h>
20 #include <linux/filter.h>
21 #include <linux/kernel.h>
22 #include <net/route.h>
23 #include <net/xdp.h>
24 #include <net/net_failover.h>
25 #include <net/netdev_rx_queue.h>
26
27 static int napi_weight = NAPI_POLL_WEIGHT;
28 module_param(napi_weight, int, 0444);
29
30 static bool csum = true, gso = true, napi_tx = true;
31 module_param(csum, bool, 0444);
32 module_param(gso, bool, 0444);
33 module_param(napi_tx, bool, 0644);
34
35 /* FIXME: MTU in config. */
36 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
37 #define GOOD_COPY_LEN 128
38
39 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
40
41 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
42 #define VIRTIO_XDP_HEADROOM 256
43
44 /* Separating two types of XDP xmit */
45 #define VIRTIO_XDP_TX BIT(0)
46 #define VIRTIO_XDP_REDIR BIT(1)
47
48 #define VIRTIO_XDP_FLAG BIT(0)
49
50 /* RX packet size EWMA. The average packet size is used to determine the packet
51 * buffer size when refilling RX rings. As the entire RX ring may be refilled
52 * at once, the weight is chosen so that the EWMA will be insensitive to short-
53 * term, transient changes in packet size.
54 */
55 DECLARE_EWMA(pkt_len, 0, 64)
56
57 #define VIRTNET_DRIVER_VERSION "1.0.0"
58
59 static const unsigned long guest_offloads[] = {
60 VIRTIO_NET_F_GUEST_TSO4,
61 VIRTIO_NET_F_GUEST_TSO6,
62 VIRTIO_NET_F_GUEST_ECN,
63 VIRTIO_NET_F_GUEST_UFO,
64 VIRTIO_NET_F_GUEST_CSUM,
65 VIRTIO_NET_F_GUEST_USO4,
66 VIRTIO_NET_F_GUEST_USO6,
67 VIRTIO_NET_F_GUEST_HDRLEN
68 };
69
70 #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
71 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
72 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
73 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
74 (1ULL << VIRTIO_NET_F_GUEST_USO4) | \
75 (1ULL << VIRTIO_NET_F_GUEST_USO6))
76
77 struct virtnet_stat_desc {
78 char desc[ETH_GSTRING_LEN];
79 size_t offset;
80 };
81
82 struct virtnet_sq_stats {
83 struct u64_stats_sync syncp;
84 u64_stats_t packets;
85 u64_stats_t bytes;
86 u64_stats_t xdp_tx;
87 u64_stats_t xdp_tx_drops;
88 u64_stats_t kicks;
89 u64_stats_t tx_timeouts;
90 };
91
92 struct virtnet_rq_stats {
93 struct u64_stats_sync syncp;
94 u64_stats_t packets;
95 u64_stats_t bytes;
96 u64_stats_t drops;
97 u64_stats_t xdp_packets;
98 u64_stats_t xdp_tx;
99 u64_stats_t xdp_redirects;
100 u64_stats_t xdp_drops;
101 u64_stats_t kicks;
102 };
103
104 #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
105 #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
106
107 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
108 { "packets", VIRTNET_SQ_STAT(packets) },
109 { "bytes", VIRTNET_SQ_STAT(bytes) },
110 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
111 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
112 { "kicks", VIRTNET_SQ_STAT(kicks) },
113 { "tx_timeouts", VIRTNET_SQ_STAT(tx_timeouts) },
114 };
115
116 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
117 { "packets", VIRTNET_RQ_STAT(packets) },
118 { "bytes", VIRTNET_RQ_STAT(bytes) },
119 { "drops", VIRTNET_RQ_STAT(drops) },
120 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
121 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
122 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
123 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
124 { "kicks", VIRTNET_RQ_STAT(kicks) },
125 };
126
127 #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
128 #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
129
130 struct virtnet_interrupt_coalesce {
131 u32 max_packets;
132 u32 max_usecs;
133 };
134
135 /* The dma information of pages allocated at a time. */
136 struct virtnet_rq_dma {
137 dma_addr_t addr;
138 u32 ref;
139 u16 len;
140 u16 need_sync;
141 };
142
143 /* Internal representation of a send virtqueue */
144 struct send_queue {
145 /* Virtqueue associated with this send _queue */
146 struct virtqueue *vq;
147
148 /* TX: fragments + linear part + virtio header */
149 struct scatterlist sg[MAX_SKB_FRAGS + 2];
150
151 /* Name of the send queue: output.$index */
152 char name[16];
153
154 struct virtnet_sq_stats stats;
155
156 struct virtnet_interrupt_coalesce intr_coal;
157
158 struct napi_struct napi;
159
160 /* Record whether sq is in reset state. */
161 bool reset;
162 };
163
164 /* Internal representation of a receive virtqueue */
165 struct receive_queue {
166 /* Virtqueue associated with this receive_queue */
167 struct virtqueue *vq;
168
169 struct napi_struct napi;
170
171 struct bpf_prog __rcu *xdp_prog;
172
173 struct virtnet_rq_stats stats;
174
175 struct virtnet_interrupt_coalesce intr_coal;
176
177 /* Chain pages by the private ptr. */
178 struct page *pages;
179
180 /* Average packet length for mergeable receive buffers. */
181 struct ewma_pkt_len mrg_avg_pkt_len;
182
183 /* Page frag for packet buffer allocation. */
184 struct page_frag alloc_frag;
185
186 /* RX: fragments + linear part + virtio header */
187 struct scatterlist sg[MAX_SKB_FRAGS + 2];
188
189 /* Min single buffer size for mergeable buffers case. */
190 unsigned int min_buf_len;
191
192 /* Name of this receive queue: input.$index */
193 char name[16];
194
195 struct xdp_rxq_info xdp_rxq;
196
197 /* Record the last dma info to free after new pages is allocated. */
198 struct virtnet_rq_dma *last_dma;
199
200 /* Do dma by self */
201 bool do_dma;
202 };
203
204 /* This structure can contain rss message with maximum settings for indirection table and keysize
205 * Note, that default structure that describes RSS configuration virtio_net_rss_config
206 * contains same info but can't handle table values.
207 * In any case, structure would be passed to virtio hw through sg_buf split by parts
208 * because table sizes may be differ according to the device configuration.
209 */
210 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
211 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
212 struct virtio_net_ctrl_rss {
213 u32 hash_types;
214 u16 indirection_table_mask;
215 u16 unclassified_queue;
216 u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
217 u16 max_tx_vq;
218 u8 hash_key_length;
219 u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
220 };
221
222 /* Control VQ buffers: protected by the rtnl lock */
223 struct control_buf {
224 struct virtio_net_ctrl_hdr hdr;
225 virtio_net_ctrl_ack status;
226 struct virtio_net_ctrl_mq mq;
227 u8 promisc;
228 u8 allmulti;
229 __virtio16 vid;
230 __virtio64 offloads;
231 struct virtio_net_ctrl_rss rss;
232 struct virtio_net_ctrl_coal_tx coal_tx;
233 struct virtio_net_ctrl_coal_rx coal_rx;
234 struct virtio_net_ctrl_coal_vq coal_vq;
235 };
236
237 struct virtnet_info {
238 struct virtio_device *vdev;
239 struct virtqueue *cvq;
240 struct net_device *dev;
241 struct send_queue *sq;
242 struct receive_queue *rq;
243 unsigned int status;
244
245 /* Max # of queue pairs supported by the device */
246 u16 max_queue_pairs;
247
248 /* # of queue pairs currently used by the driver */
249 u16 curr_queue_pairs;
250
251 /* # of XDP queue pairs currently used by the driver */
252 u16 xdp_queue_pairs;
253
254 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
255 bool xdp_enabled;
256
257 /* I like... big packets and I cannot lie! */
258 bool big_packets;
259
260 /* number of sg entries allocated for big packets */
261 unsigned int big_packets_num_skbfrags;
262
263 /* Host will merge rx buffers for big packets (shake it! shake it!) */
264 bool mergeable_rx_bufs;
265
266 /* Host supports rss and/or hash report */
267 bool has_rss;
268 bool has_rss_hash_report;
269 u8 rss_key_size;
270 u16 rss_indir_table_size;
271 u32 rss_hash_types_supported;
272 u32 rss_hash_types_saved;
273
274 /* Has control virtqueue */
275 bool has_cvq;
276
277 /* Host can handle any s/g split between our header and packet data */
278 bool any_header_sg;
279
280 /* Packet virtio header size */
281 u8 hdr_len;
282
283 /* Work struct for delayed refilling if we run low on memory. */
284 struct delayed_work refill;
285
286 /* Is delayed refill enabled? */
287 bool refill_enabled;
288
289 /* The lock to synchronize the access to refill_enabled */
290 spinlock_t refill_lock;
291
292 /* Work struct for config space updates */
293 struct work_struct config_work;
294
295 /* Does the affinity hint is set for virtqueues? */
296 bool affinity_hint_set;
297
298 /* CPU hotplug instances for online & dead */
299 struct hlist_node node;
300 struct hlist_node node_dead;
301
302 struct control_buf *ctrl;
303
304 /* Ethtool settings */
305 u8 duplex;
306 u32 speed;
307
308 /* Interrupt coalescing settings */
309 struct virtnet_interrupt_coalesce intr_coal_tx;
310 struct virtnet_interrupt_coalesce intr_coal_rx;
311
312 unsigned long guest_offloads;
313 unsigned long guest_offloads_capable;
314
315 /* failover when STANDBY feature enabled */
316 struct failover *failover;
317 };
318
319 struct padded_vnet_hdr {
320 struct virtio_net_hdr_v1_hash hdr;
321 /*
322 * hdr is in a separate sg buffer, and data sg buffer shares same page
323 * with this header sg. This padding makes next sg 16 byte aligned
324 * after the header.
325 */
326 char padding[12];
327 };
328
329 struct virtio_net_common_hdr {
330 union {
331 struct virtio_net_hdr hdr;
332 struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
333 struct virtio_net_hdr_v1_hash hash_v1_hdr;
334 };
335 };
336
337 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
338
is_xdp_frame(void * ptr)339 static bool is_xdp_frame(void *ptr)
340 {
341 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
342 }
343
xdp_to_ptr(struct xdp_frame * ptr)344 static void *xdp_to_ptr(struct xdp_frame *ptr)
345 {
346 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
347 }
348
ptr_to_xdp(void * ptr)349 static struct xdp_frame *ptr_to_xdp(void *ptr)
350 {
351 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
352 }
353
354 /* Converting between virtqueue no. and kernel tx/rx queue no.
355 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
356 */
vq2txq(struct virtqueue * vq)357 static int vq2txq(struct virtqueue *vq)
358 {
359 return (vq->index - 1) / 2;
360 }
361
txq2vq(int txq)362 static int txq2vq(int txq)
363 {
364 return txq * 2 + 1;
365 }
366
vq2rxq(struct virtqueue * vq)367 static int vq2rxq(struct virtqueue *vq)
368 {
369 return vq->index / 2;
370 }
371
rxq2vq(int rxq)372 static int rxq2vq(int rxq)
373 {
374 return rxq * 2;
375 }
376
377 static inline struct virtio_net_common_hdr *
skb_vnet_common_hdr(struct sk_buff * skb)378 skb_vnet_common_hdr(struct sk_buff *skb)
379 {
380 return (struct virtio_net_common_hdr *)skb->cb;
381 }
382
383 /*
384 * private is used to chain pages for big packets, put the whole
385 * most recent used list in the beginning for reuse
386 */
give_pages(struct receive_queue * rq,struct page * page)387 static void give_pages(struct receive_queue *rq, struct page *page)
388 {
389 struct page *end;
390
391 /* Find end of list, sew whole thing into vi->rq.pages. */
392 for (end = page; end->private; end = (struct page *)end->private);
393 end->private = (unsigned long)rq->pages;
394 rq->pages = page;
395 }
396
get_a_page(struct receive_queue * rq,gfp_t gfp_mask)397 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
398 {
399 struct page *p = rq->pages;
400
401 if (p) {
402 rq->pages = (struct page *)p->private;
403 /* clear private here, it is used to chain pages */
404 p->private = 0;
405 } else
406 p = alloc_page(gfp_mask);
407 return p;
408 }
409
virtnet_rq_free_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf)410 static void virtnet_rq_free_buf(struct virtnet_info *vi,
411 struct receive_queue *rq, void *buf)
412 {
413 if (vi->mergeable_rx_bufs)
414 put_page(virt_to_head_page(buf));
415 else if (vi->big_packets)
416 give_pages(rq, buf);
417 else
418 put_page(virt_to_head_page(buf));
419 }
420
enable_delayed_refill(struct virtnet_info * vi)421 static void enable_delayed_refill(struct virtnet_info *vi)
422 {
423 spin_lock_bh(&vi->refill_lock);
424 vi->refill_enabled = true;
425 spin_unlock_bh(&vi->refill_lock);
426 }
427
disable_delayed_refill(struct virtnet_info * vi)428 static void disable_delayed_refill(struct virtnet_info *vi)
429 {
430 spin_lock_bh(&vi->refill_lock);
431 vi->refill_enabled = false;
432 spin_unlock_bh(&vi->refill_lock);
433 }
434
virtqueue_napi_schedule(struct napi_struct * napi,struct virtqueue * vq)435 static void virtqueue_napi_schedule(struct napi_struct *napi,
436 struct virtqueue *vq)
437 {
438 if (napi_schedule_prep(napi)) {
439 virtqueue_disable_cb(vq);
440 __napi_schedule(napi);
441 }
442 }
443
virtqueue_napi_complete(struct napi_struct * napi,struct virtqueue * vq,int processed)444 static void virtqueue_napi_complete(struct napi_struct *napi,
445 struct virtqueue *vq, int processed)
446 {
447 int opaque;
448
449 opaque = virtqueue_enable_cb_prepare(vq);
450 if (napi_complete_done(napi, processed)) {
451 if (unlikely(virtqueue_poll(vq, opaque)))
452 virtqueue_napi_schedule(napi, vq);
453 } else {
454 virtqueue_disable_cb(vq);
455 }
456 }
457
skb_xmit_done(struct virtqueue * vq)458 static void skb_xmit_done(struct virtqueue *vq)
459 {
460 struct virtnet_info *vi = vq->vdev->priv;
461 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
462
463 /* Suppress further interrupts. */
464 virtqueue_disable_cb(vq);
465
466 if (napi->weight)
467 virtqueue_napi_schedule(napi, vq);
468 else
469 /* We were probably waiting for more output buffers. */
470 netif_wake_subqueue(vi->dev, vq2txq(vq));
471 }
472
473 #define MRG_CTX_HEADER_SHIFT 22
mergeable_len_to_ctx(unsigned int truesize,unsigned int headroom)474 static void *mergeable_len_to_ctx(unsigned int truesize,
475 unsigned int headroom)
476 {
477 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
478 }
479
mergeable_ctx_to_headroom(void * mrg_ctx)480 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
481 {
482 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
483 }
484
mergeable_ctx_to_truesize(void * mrg_ctx)485 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
486 {
487 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
488 }
489
virtnet_build_skb(void * buf,unsigned int buflen,unsigned int headroom,unsigned int len)490 static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen,
491 unsigned int headroom,
492 unsigned int len)
493 {
494 struct sk_buff *skb;
495
496 skb = build_skb(buf, buflen);
497 if (unlikely(!skb))
498 return NULL;
499
500 skb_reserve(skb, headroom);
501 skb_put(skb, len);
502
503 return skb;
504 }
505
506 /* Called from bottom half context */
page_to_skb(struct virtnet_info * vi,struct receive_queue * rq,struct page * page,unsigned int offset,unsigned int len,unsigned int truesize,unsigned int headroom)507 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
508 struct receive_queue *rq,
509 struct page *page, unsigned int offset,
510 unsigned int len, unsigned int truesize,
511 unsigned int headroom)
512 {
513 struct sk_buff *skb;
514 struct virtio_net_common_hdr *hdr;
515 unsigned int copy, hdr_len, hdr_padded_len;
516 struct page *page_to_free = NULL;
517 int tailroom, shinfo_size;
518 char *p, *hdr_p, *buf;
519
520 p = page_address(page) + offset;
521 hdr_p = p;
522
523 hdr_len = vi->hdr_len;
524 if (vi->mergeable_rx_bufs)
525 hdr_padded_len = hdr_len;
526 else
527 hdr_padded_len = sizeof(struct padded_vnet_hdr);
528
529 buf = p - headroom;
530 len -= hdr_len;
531 offset += hdr_padded_len;
532 p += hdr_padded_len;
533 tailroom = truesize - headroom - hdr_padded_len - len;
534
535 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
536
537 /* copy small packet so we can reuse these pages */
538 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
539 skb = virtnet_build_skb(buf, truesize, p - buf, len);
540 if (unlikely(!skb))
541 return NULL;
542
543 page = (struct page *)page->private;
544 if (page)
545 give_pages(rq, page);
546 goto ok;
547 }
548
549 /* copy small packet so we can reuse these pages for small data */
550 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
551 if (unlikely(!skb))
552 return NULL;
553
554 /* Copy all frame if it fits skb->head, otherwise
555 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
556 */
557 if (len <= skb_tailroom(skb))
558 copy = len;
559 else
560 copy = ETH_HLEN;
561 skb_put_data(skb, p, copy);
562
563 len -= copy;
564 offset += copy;
565
566 if (vi->mergeable_rx_bufs) {
567 if (len)
568 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
569 else
570 page_to_free = page;
571 goto ok;
572 }
573
574 /*
575 * Verify that we can indeed put this data into a skb.
576 * This is here to handle cases when the device erroneously
577 * tries to receive more than is possible. This is usually
578 * the case of a broken device.
579 */
580 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
581 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
582 dev_kfree_skb(skb);
583 return NULL;
584 }
585 BUG_ON(offset >= PAGE_SIZE);
586 while (len) {
587 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
588 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
589 frag_size, truesize);
590 len -= frag_size;
591 page = (struct page *)page->private;
592 offset = 0;
593 }
594
595 if (page)
596 give_pages(rq, page);
597
598 ok:
599 hdr = skb_vnet_common_hdr(skb);
600 memcpy(hdr, hdr_p, hdr_len);
601 if (page_to_free)
602 put_page(page_to_free);
603
604 return skb;
605 }
606
virtnet_rq_unmap(struct receive_queue * rq,void * buf,u32 len)607 static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
608 {
609 struct page *page = virt_to_head_page(buf);
610 struct virtnet_rq_dma *dma;
611 void *head;
612 int offset;
613
614 head = page_address(page);
615
616 dma = head;
617
618 --dma->ref;
619
620 if (dma->need_sync && len) {
621 offset = buf - (head + sizeof(*dma));
622
623 virtqueue_dma_sync_single_range_for_cpu(rq->vq, dma->addr,
624 offset, len,
625 DMA_FROM_DEVICE);
626 }
627
628 if (dma->ref)
629 return;
630
631 virtqueue_dma_unmap_single_attrs(rq->vq, dma->addr, dma->len,
632 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
633 put_page(page);
634 }
635
virtnet_rq_get_buf(struct receive_queue * rq,u32 * len,void ** ctx)636 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
637 {
638 void *buf;
639
640 buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
641 if (buf && rq->do_dma)
642 virtnet_rq_unmap(rq, buf, *len);
643
644 return buf;
645 }
646
virtnet_rq_init_one_sg(struct receive_queue * rq,void * buf,u32 len)647 static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
648 {
649 struct virtnet_rq_dma *dma;
650 dma_addr_t addr;
651 u32 offset;
652 void *head;
653
654 if (!rq->do_dma) {
655 sg_init_one(rq->sg, buf, len);
656 return;
657 }
658
659 head = page_address(rq->alloc_frag.page);
660
661 offset = buf - head;
662
663 dma = head;
664
665 addr = dma->addr - sizeof(*dma) + offset;
666
667 sg_init_table(rq->sg, 1);
668 rq->sg[0].dma_address = addr;
669 rq->sg[0].length = len;
670 }
671
virtnet_rq_alloc(struct receive_queue * rq,u32 size,gfp_t gfp)672 static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
673 {
674 struct page_frag *alloc_frag = &rq->alloc_frag;
675 struct virtnet_rq_dma *dma;
676 void *buf, *head;
677 dma_addr_t addr;
678
679 head = page_address(alloc_frag->page);
680
681 if (rq->do_dma) {
682 dma = head;
683
684 /* new pages */
685 if (!alloc_frag->offset) {
686 if (rq->last_dma) {
687 /* Now, the new page is allocated, the last dma
688 * will not be used. So the dma can be unmapped
689 * if the ref is 0.
690 */
691 virtnet_rq_unmap(rq, rq->last_dma, 0);
692 rq->last_dma = NULL;
693 }
694
695 dma->len = alloc_frag->size - sizeof(*dma);
696
697 addr = virtqueue_dma_map_single_attrs(rq->vq, dma + 1,
698 dma->len, DMA_FROM_DEVICE, 0);
699 if (virtqueue_dma_mapping_error(rq->vq, addr))
700 return NULL;
701
702 dma->addr = addr;
703 dma->need_sync = virtqueue_dma_need_sync(rq->vq, addr);
704
705 /* Add a reference to dma to prevent the entire dma from
706 * being released during error handling. This reference
707 * will be freed after the pages are no longer used.
708 */
709 get_page(alloc_frag->page);
710 dma->ref = 1;
711 alloc_frag->offset = sizeof(*dma);
712
713 rq->last_dma = dma;
714 }
715
716 ++dma->ref;
717 }
718
719 buf = head + alloc_frag->offset;
720
721 get_page(alloc_frag->page);
722 alloc_frag->offset += size;
723
724 return buf;
725 }
726
virtnet_rq_set_premapped(struct virtnet_info * vi)727 static void virtnet_rq_set_premapped(struct virtnet_info *vi)
728 {
729 int i;
730
731 /* disable for big mode */
732 if (!vi->mergeable_rx_bufs && vi->big_packets)
733 return;
734
735 for (i = 0; i < vi->max_queue_pairs; i++) {
736 if (virtqueue_set_dma_premapped(vi->rq[i].vq))
737 continue;
738
739 vi->rq[i].do_dma = true;
740 }
741 }
742
virtnet_rq_unmap_free_buf(struct virtqueue * vq,void * buf)743 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
744 {
745 struct virtnet_info *vi = vq->vdev->priv;
746 struct receive_queue *rq;
747 int i = vq2rxq(vq);
748
749 rq = &vi->rq[i];
750
751 if (rq->do_dma)
752 virtnet_rq_unmap(rq, buf, 0);
753
754 virtnet_rq_free_buf(vi, rq, buf);
755 }
756
free_old_xmit_skbs(struct send_queue * sq,bool in_napi)757 static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
758 {
759 unsigned int len;
760 unsigned int packets = 0;
761 unsigned int bytes = 0;
762 void *ptr;
763
764 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
765 if (likely(!is_xdp_frame(ptr))) {
766 struct sk_buff *skb = ptr;
767
768 pr_debug("Sent skb %p\n", skb);
769
770 bytes += skb->len;
771 napi_consume_skb(skb, in_napi);
772 } else {
773 struct xdp_frame *frame = ptr_to_xdp(ptr);
774
775 bytes += xdp_get_frame_len(frame);
776 xdp_return_frame(frame);
777 }
778 packets++;
779 }
780
781 /* Avoid overhead when no packets have been processed
782 * happens when called speculatively from start_xmit.
783 */
784 if (!packets)
785 return;
786
787 u64_stats_update_begin(&sq->stats.syncp);
788 u64_stats_add(&sq->stats.bytes, bytes);
789 u64_stats_add(&sq->stats.packets, packets);
790 u64_stats_update_end(&sq->stats.syncp);
791 }
792
is_xdp_raw_buffer_queue(struct virtnet_info * vi,int q)793 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
794 {
795 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
796 return false;
797 else if (q < vi->curr_queue_pairs)
798 return true;
799 else
800 return false;
801 }
802
check_sq_full_and_disable(struct virtnet_info * vi,struct net_device * dev,struct send_queue * sq)803 static void check_sq_full_and_disable(struct virtnet_info *vi,
804 struct net_device *dev,
805 struct send_queue *sq)
806 {
807 bool use_napi = sq->napi.weight;
808 int qnum;
809
810 qnum = sq - vi->sq;
811
812 /* If running out of space, stop queue to avoid getting packets that we
813 * are then unable to transmit.
814 * An alternative would be to force queuing layer to requeue the skb by
815 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
816 * returned in a normal path of operation: it means that driver is not
817 * maintaining the TX queue stop/start state properly, and causes
818 * the stack to do a non-trivial amount of useless work.
819 * Since most packets only take 1 or 2 ring slots, stopping the queue
820 * early means 16 slots are typically wasted.
821 */
822 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
823 netif_stop_subqueue(dev, qnum);
824 if (use_napi) {
825 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
826 virtqueue_napi_schedule(&sq->napi, sq->vq);
827 } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
828 /* More just got used, free them then recheck. */
829 free_old_xmit_skbs(sq, false);
830 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
831 netif_start_subqueue(dev, qnum);
832 virtqueue_disable_cb(sq->vq);
833 }
834 }
835 }
836 }
837
__virtnet_xdp_xmit_one(struct virtnet_info * vi,struct send_queue * sq,struct xdp_frame * xdpf)838 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
839 struct send_queue *sq,
840 struct xdp_frame *xdpf)
841 {
842 struct virtio_net_hdr_mrg_rxbuf *hdr;
843 struct skb_shared_info *shinfo;
844 u8 nr_frags = 0;
845 int err, i;
846
847 if (unlikely(xdpf->headroom < vi->hdr_len))
848 return -EOVERFLOW;
849
850 if (unlikely(xdp_frame_has_frags(xdpf))) {
851 shinfo = xdp_get_shared_info_from_frame(xdpf);
852 nr_frags = shinfo->nr_frags;
853 }
854
855 /* In wrapping function virtnet_xdp_xmit(), we need to free
856 * up the pending old buffers, where we need to calculate the
857 * position of skb_shared_info in xdp_get_frame_len() and
858 * xdp_return_frame(), which will involve to xdpf->data and
859 * xdpf->headroom. Therefore, we need to update the value of
860 * headroom synchronously here.
861 */
862 xdpf->headroom -= vi->hdr_len;
863 xdpf->data -= vi->hdr_len;
864 /* Zero header and leave csum up to XDP layers */
865 hdr = xdpf->data;
866 memset(hdr, 0, vi->hdr_len);
867 xdpf->len += vi->hdr_len;
868
869 sg_init_table(sq->sg, nr_frags + 1);
870 sg_set_buf(sq->sg, xdpf->data, xdpf->len);
871 for (i = 0; i < nr_frags; i++) {
872 skb_frag_t *frag = &shinfo->frags[i];
873
874 sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
875 skb_frag_size(frag), skb_frag_off(frag));
876 }
877
878 err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
879 xdp_to_ptr(xdpf), GFP_ATOMIC);
880 if (unlikely(err))
881 return -ENOSPC; /* Caller handle free/refcnt */
882
883 return 0;
884 }
885
886 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
887 * the current cpu, so it does not need to be locked.
888 *
889 * Here we use marco instead of inline functions because we have to deal with
890 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
891 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
892 * functions to perfectly solve these three problems at the same time.
893 */
894 #define virtnet_xdp_get_sq(vi) ({ \
895 int cpu = smp_processor_id(); \
896 struct netdev_queue *txq; \
897 typeof(vi) v = (vi); \
898 unsigned int qp; \
899 \
900 if (v->curr_queue_pairs > nr_cpu_ids) { \
901 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
902 qp += cpu; \
903 txq = netdev_get_tx_queue(v->dev, qp); \
904 __netif_tx_acquire(txq); \
905 } else { \
906 qp = cpu % v->curr_queue_pairs; \
907 txq = netdev_get_tx_queue(v->dev, qp); \
908 __netif_tx_lock(txq, cpu); \
909 } \
910 v->sq + qp; \
911 })
912
913 #define virtnet_xdp_put_sq(vi, q) { \
914 struct netdev_queue *txq; \
915 typeof(vi) v = (vi); \
916 \
917 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
918 if (v->curr_queue_pairs > nr_cpu_ids) \
919 __netif_tx_release(txq); \
920 else \
921 __netif_tx_unlock(txq); \
922 }
923
virtnet_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)924 static int virtnet_xdp_xmit(struct net_device *dev,
925 int n, struct xdp_frame **frames, u32 flags)
926 {
927 struct virtnet_info *vi = netdev_priv(dev);
928 struct receive_queue *rq = vi->rq;
929 struct bpf_prog *xdp_prog;
930 struct send_queue *sq;
931 unsigned int len;
932 int packets = 0;
933 int bytes = 0;
934 int nxmit = 0;
935 int kicks = 0;
936 void *ptr;
937 int ret;
938 int i;
939
940 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
941 * indicate XDP resources have been successfully allocated.
942 */
943 xdp_prog = rcu_access_pointer(rq->xdp_prog);
944 if (!xdp_prog)
945 return -ENXIO;
946
947 sq = virtnet_xdp_get_sq(vi);
948
949 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
950 ret = -EINVAL;
951 goto out;
952 }
953
954 /* Free up any pending old buffers before queueing new ones. */
955 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
956 if (likely(is_xdp_frame(ptr))) {
957 struct xdp_frame *frame = ptr_to_xdp(ptr);
958
959 bytes += xdp_get_frame_len(frame);
960 xdp_return_frame(frame);
961 } else {
962 struct sk_buff *skb = ptr;
963
964 bytes += skb->len;
965 napi_consume_skb(skb, false);
966 }
967 packets++;
968 }
969
970 for (i = 0; i < n; i++) {
971 struct xdp_frame *xdpf = frames[i];
972
973 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
974 break;
975 nxmit++;
976 }
977 ret = nxmit;
978
979 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
980 check_sq_full_and_disable(vi, dev, sq);
981
982 if (flags & XDP_XMIT_FLUSH) {
983 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
984 kicks = 1;
985 }
986 out:
987 u64_stats_update_begin(&sq->stats.syncp);
988 u64_stats_add(&sq->stats.bytes, bytes);
989 u64_stats_add(&sq->stats.packets, packets);
990 u64_stats_add(&sq->stats.xdp_tx, n);
991 u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit);
992 u64_stats_add(&sq->stats.kicks, kicks);
993 u64_stats_update_end(&sq->stats.syncp);
994
995 virtnet_xdp_put_sq(vi, sq);
996 return ret;
997 }
998
put_xdp_frags(struct xdp_buff * xdp)999 static void put_xdp_frags(struct xdp_buff *xdp)
1000 {
1001 struct skb_shared_info *shinfo;
1002 struct page *xdp_page;
1003 int i;
1004
1005 if (xdp_buff_has_frags(xdp)) {
1006 shinfo = xdp_get_shared_info_from_buff(xdp);
1007 for (i = 0; i < shinfo->nr_frags; i++) {
1008 xdp_page = skb_frag_page(&shinfo->frags[i]);
1009 put_page(xdp_page);
1010 }
1011 }
1012 }
1013
virtnet_xdp_handler(struct bpf_prog * xdp_prog,struct xdp_buff * xdp,struct net_device * dev,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1014 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
1015 struct net_device *dev,
1016 unsigned int *xdp_xmit,
1017 struct virtnet_rq_stats *stats)
1018 {
1019 struct xdp_frame *xdpf;
1020 int err;
1021 u32 act;
1022
1023 act = bpf_prog_run_xdp(xdp_prog, xdp);
1024 u64_stats_inc(&stats->xdp_packets);
1025
1026 switch (act) {
1027 case XDP_PASS:
1028 return act;
1029
1030 case XDP_TX:
1031 u64_stats_inc(&stats->xdp_tx);
1032 xdpf = xdp_convert_buff_to_frame(xdp);
1033 if (unlikely(!xdpf)) {
1034 netdev_dbg(dev, "convert buff to frame failed for xdp\n");
1035 return XDP_DROP;
1036 }
1037
1038 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1039 if (unlikely(!err)) {
1040 xdp_return_frame_rx_napi(xdpf);
1041 } else if (unlikely(err < 0)) {
1042 trace_xdp_exception(dev, xdp_prog, act);
1043 return XDP_DROP;
1044 }
1045 *xdp_xmit |= VIRTIO_XDP_TX;
1046 return act;
1047
1048 case XDP_REDIRECT:
1049 u64_stats_inc(&stats->xdp_redirects);
1050 err = xdp_do_redirect(dev, xdp, xdp_prog);
1051 if (err)
1052 return XDP_DROP;
1053
1054 *xdp_xmit |= VIRTIO_XDP_REDIR;
1055 return act;
1056
1057 default:
1058 bpf_warn_invalid_xdp_action(dev, xdp_prog, act);
1059 fallthrough;
1060 case XDP_ABORTED:
1061 trace_xdp_exception(dev, xdp_prog, act);
1062 fallthrough;
1063 case XDP_DROP:
1064 return XDP_DROP;
1065 }
1066 }
1067
virtnet_get_headroom(struct virtnet_info * vi)1068 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
1069 {
1070 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
1071 }
1072
1073 /* We copy the packet for XDP in the following cases:
1074 *
1075 * 1) Packet is scattered across multiple rx buffers.
1076 * 2) Headroom space is insufficient.
1077 *
1078 * This is inefficient but it's a temporary condition that
1079 * we hit right after XDP is enabled and until queue is refilled
1080 * with large buffers with sufficient headroom - so it should affect
1081 * at most queue size packets.
1082 * Afterwards, the conditions to enable
1083 * XDP should preclude the underlying device from sending packets
1084 * across multiple buffers (num_buf > 1), and we make sure buffers
1085 * have enough headroom.
1086 */
xdp_linearize_page(struct receive_queue * rq,int * num_buf,struct page * p,int offset,int page_off,unsigned int * len)1087 static struct page *xdp_linearize_page(struct receive_queue *rq,
1088 int *num_buf,
1089 struct page *p,
1090 int offset,
1091 int page_off,
1092 unsigned int *len)
1093 {
1094 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1095 struct page *page;
1096
1097 if (page_off + *len + tailroom > PAGE_SIZE)
1098 return NULL;
1099
1100 page = alloc_page(GFP_ATOMIC);
1101 if (!page)
1102 return NULL;
1103
1104 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
1105 page_off += *len;
1106
1107 while (--*num_buf) {
1108 unsigned int buflen;
1109 void *buf;
1110 int off;
1111
1112 buf = virtnet_rq_get_buf(rq, &buflen, NULL);
1113 if (unlikely(!buf))
1114 goto err_buf;
1115
1116 p = virt_to_head_page(buf);
1117 off = buf - page_address(p);
1118
1119 /* guard against a misconfigured or uncooperative backend that
1120 * is sending packet larger than the MTU.
1121 */
1122 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
1123 put_page(p);
1124 goto err_buf;
1125 }
1126
1127 memcpy(page_address(page) + page_off,
1128 page_address(p) + off, buflen);
1129 page_off += buflen;
1130 put_page(p);
1131 }
1132
1133 /* Headroom does not contribute to packet length */
1134 *len = page_off - VIRTIO_XDP_HEADROOM;
1135 return page;
1136 err_buf:
1137 __free_pages(page, 0);
1138 return NULL;
1139 }
1140
receive_small_build_skb(struct virtnet_info * vi,unsigned int xdp_headroom,void * buf,unsigned int len)1141 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
1142 unsigned int xdp_headroom,
1143 void *buf,
1144 unsigned int len)
1145 {
1146 unsigned int header_offset;
1147 unsigned int headroom;
1148 unsigned int buflen;
1149 struct sk_buff *skb;
1150
1151 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1152 headroom = vi->hdr_len + header_offset;
1153 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1154 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1155
1156 skb = virtnet_build_skb(buf, buflen, headroom, len);
1157 if (unlikely(!skb))
1158 return NULL;
1159
1160 buf += header_offset;
1161 memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
1162
1163 return skb;
1164 }
1165
receive_small_xdp(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * buf,unsigned int xdp_headroom,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1166 static struct sk_buff *receive_small_xdp(struct net_device *dev,
1167 struct virtnet_info *vi,
1168 struct receive_queue *rq,
1169 struct bpf_prog *xdp_prog,
1170 void *buf,
1171 unsigned int xdp_headroom,
1172 unsigned int len,
1173 unsigned int *xdp_xmit,
1174 struct virtnet_rq_stats *stats)
1175 {
1176 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
1177 unsigned int headroom = vi->hdr_len + header_offset;
1178 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
1179 struct page *page = virt_to_head_page(buf);
1180 struct page *xdp_page;
1181 unsigned int buflen;
1182 struct xdp_buff xdp;
1183 struct sk_buff *skb;
1184 unsigned int metasize = 0;
1185 u32 act;
1186
1187 if (unlikely(hdr->hdr.gso_type))
1188 goto err_xdp;
1189
1190 /* Partially checksummed packets must be dropped. */
1191 if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
1192 goto err_xdp;
1193
1194 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1195 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1196
1197 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
1198 int offset = buf - page_address(page) + header_offset;
1199 unsigned int tlen = len + vi->hdr_len;
1200 int num_buf = 1;
1201
1202 xdp_headroom = virtnet_get_headroom(vi);
1203 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1204 headroom = vi->hdr_len + header_offset;
1205 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1206 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1207 xdp_page = xdp_linearize_page(rq, &num_buf, page,
1208 offset, header_offset,
1209 &tlen);
1210 if (!xdp_page)
1211 goto err_xdp;
1212
1213 buf = page_address(xdp_page);
1214 put_page(page);
1215 page = xdp_page;
1216 }
1217
1218 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
1219 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
1220 xdp_headroom, len, true);
1221
1222 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1223
1224 switch (act) {
1225 case XDP_PASS:
1226 /* Recalculate length in case bpf program changed it */
1227 len = xdp.data_end - xdp.data;
1228 metasize = xdp.data - xdp.data_meta;
1229 break;
1230
1231 case XDP_TX:
1232 case XDP_REDIRECT:
1233 goto xdp_xmit;
1234
1235 default:
1236 goto err_xdp;
1237 }
1238
1239 skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len);
1240 if (unlikely(!skb))
1241 goto err;
1242
1243 if (metasize)
1244 skb_metadata_set(skb, metasize);
1245
1246 return skb;
1247
1248 err_xdp:
1249 u64_stats_inc(&stats->xdp_drops);
1250 err:
1251 u64_stats_inc(&stats->drops);
1252 put_page(page);
1253 xdp_xmit:
1254 return NULL;
1255 }
1256
receive_small(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1257 static struct sk_buff *receive_small(struct net_device *dev,
1258 struct virtnet_info *vi,
1259 struct receive_queue *rq,
1260 void *buf, void *ctx,
1261 unsigned int len,
1262 unsigned int *xdp_xmit,
1263 struct virtnet_rq_stats *stats)
1264 {
1265 unsigned int xdp_headroom = (unsigned long)ctx;
1266 struct page *page = virt_to_head_page(buf);
1267 struct sk_buff *skb;
1268
1269 /* We passed the address of virtnet header to virtio-core,
1270 * so truncate the padding.
1271 */
1272 buf -= VIRTNET_RX_PAD + xdp_headroom;
1273
1274 len -= vi->hdr_len;
1275 u64_stats_add(&stats->bytes, len);
1276
1277 if (unlikely(len > GOOD_PACKET_LEN)) {
1278 pr_debug("%s: rx error: len %u exceeds max size %d\n",
1279 dev->name, len, GOOD_PACKET_LEN);
1280 DEV_STATS_INC(dev, rx_length_errors);
1281 goto err;
1282 }
1283
1284 if (unlikely(vi->xdp_enabled)) {
1285 struct bpf_prog *xdp_prog;
1286
1287 rcu_read_lock();
1288 xdp_prog = rcu_dereference(rq->xdp_prog);
1289 if (xdp_prog) {
1290 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf,
1291 xdp_headroom, len, xdp_xmit,
1292 stats);
1293 rcu_read_unlock();
1294 return skb;
1295 }
1296 rcu_read_unlock();
1297 }
1298
1299 skb = receive_small_build_skb(vi, xdp_headroom, buf, len);
1300 if (likely(skb))
1301 return skb;
1302
1303 err:
1304 u64_stats_inc(&stats->drops);
1305 put_page(page);
1306 return NULL;
1307 }
1308
receive_big(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,struct virtnet_rq_stats * stats)1309 static struct sk_buff *receive_big(struct net_device *dev,
1310 struct virtnet_info *vi,
1311 struct receive_queue *rq,
1312 void *buf,
1313 unsigned int len,
1314 struct virtnet_rq_stats *stats)
1315 {
1316 struct page *page = buf;
1317 struct sk_buff *skb =
1318 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0);
1319
1320 u64_stats_add(&stats->bytes, len - vi->hdr_len);
1321 if (unlikely(!skb))
1322 goto err;
1323
1324 return skb;
1325
1326 err:
1327 u64_stats_inc(&stats->drops);
1328 give_pages(rq, page);
1329 return NULL;
1330 }
1331
mergeable_buf_free(struct receive_queue * rq,int num_buf,struct net_device * dev,struct virtnet_rq_stats * stats)1332 static void mergeable_buf_free(struct receive_queue *rq, int num_buf,
1333 struct net_device *dev,
1334 struct virtnet_rq_stats *stats)
1335 {
1336 struct page *page;
1337 void *buf;
1338 int len;
1339
1340 while (num_buf-- > 1) {
1341 buf = virtnet_rq_get_buf(rq, &len, NULL);
1342 if (unlikely(!buf)) {
1343 pr_debug("%s: rx error: %d buffers missing\n",
1344 dev->name, num_buf);
1345 DEV_STATS_INC(dev, rx_length_errors);
1346 break;
1347 }
1348 u64_stats_add(&stats->bytes, len);
1349 page = virt_to_head_page(buf);
1350 put_page(page);
1351 }
1352 }
1353
1354 /* Why not use xdp_build_skb_from_frame() ?
1355 * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
1356 * virtio-net there are 2 points that do not match its requirements:
1357 * 1. The size of the prefilled buffer is not fixed before xdp is set.
1358 * 2. xdp_build_skb_from_frame() does more checks that we don't need,
1359 * like eth_type_trans() (which virtio-net does in receive_buf()).
1360 */
build_skb_from_xdp_buff(struct net_device * dev,struct virtnet_info * vi,struct xdp_buff * xdp,unsigned int xdp_frags_truesz)1361 static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
1362 struct virtnet_info *vi,
1363 struct xdp_buff *xdp,
1364 unsigned int xdp_frags_truesz)
1365 {
1366 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
1367 unsigned int headroom, data_len;
1368 struct sk_buff *skb;
1369 int metasize;
1370 u8 nr_frags;
1371
1372 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
1373 pr_debug("Error building skb as missing reserved tailroom for xdp");
1374 return NULL;
1375 }
1376
1377 if (unlikely(xdp_buff_has_frags(xdp)))
1378 nr_frags = sinfo->nr_frags;
1379
1380 skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
1381 if (unlikely(!skb))
1382 return NULL;
1383
1384 headroom = xdp->data - xdp->data_hard_start;
1385 data_len = xdp->data_end - xdp->data;
1386 skb_reserve(skb, headroom);
1387 __skb_put(skb, data_len);
1388
1389 metasize = xdp->data - xdp->data_meta;
1390 metasize = metasize > 0 ? metasize : 0;
1391 if (metasize)
1392 skb_metadata_set(skb, metasize);
1393
1394 if (unlikely(xdp_buff_has_frags(xdp)))
1395 xdp_update_skb_shared_info(skb, nr_frags,
1396 sinfo->xdp_frags_size,
1397 xdp_frags_truesz,
1398 xdp_buff_is_frag_pfmemalloc(xdp));
1399
1400 return skb;
1401 }
1402
1403 /* TODO: build xdp in big mode */
virtnet_build_xdp_buff_mrg(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct xdp_buff * xdp,void * buf,unsigned int len,unsigned int frame_sz,int * num_buf,unsigned int * xdp_frags_truesize,struct virtnet_rq_stats * stats)1404 static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
1405 struct virtnet_info *vi,
1406 struct receive_queue *rq,
1407 struct xdp_buff *xdp,
1408 void *buf,
1409 unsigned int len,
1410 unsigned int frame_sz,
1411 int *num_buf,
1412 unsigned int *xdp_frags_truesize,
1413 struct virtnet_rq_stats *stats)
1414 {
1415 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1416 unsigned int headroom, tailroom, room;
1417 unsigned int truesize, cur_frag_size;
1418 struct skb_shared_info *shinfo;
1419 unsigned int xdp_frags_truesz = 0;
1420 struct page *page;
1421 skb_frag_t *frag;
1422 int offset;
1423 void *ctx;
1424
1425 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
1426 xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM,
1427 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
1428
1429 if (!*num_buf)
1430 return 0;
1431
1432 if (*num_buf > 1) {
1433 /* If we want to build multi-buffer xdp, we need
1434 * to specify that the flags of xdp_buff have the
1435 * XDP_FLAGS_HAS_FRAG bit.
1436 */
1437 if (!xdp_buff_has_frags(xdp))
1438 xdp_buff_set_frags_flag(xdp);
1439
1440 shinfo = xdp_get_shared_info_from_buff(xdp);
1441 shinfo->nr_frags = 0;
1442 shinfo->xdp_frags_size = 0;
1443 }
1444
1445 if (*num_buf > MAX_SKB_FRAGS + 1)
1446 return -EINVAL;
1447
1448 while (--*num_buf > 0) {
1449 buf = virtnet_rq_get_buf(rq, &len, &ctx);
1450 if (unlikely(!buf)) {
1451 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1452 dev->name, *num_buf,
1453 virtio16_to_cpu(vi->vdev, hdr->num_buffers));
1454 DEV_STATS_INC(dev, rx_length_errors);
1455 goto err;
1456 }
1457
1458 u64_stats_add(&stats->bytes, len);
1459 page = virt_to_head_page(buf);
1460 offset = buf - page_address(page);
1461
1462 truesize = mergeable_ctx_to_truesize(ctx);
1463 headroom = mergeable_ctx_to_headroom(ctx);
1464 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1465 room = SKB_DATA_ALIGN(headroom + tailroom);
1466
1467 cur_frag_size = truesize;
1468 xdp_frags_truesz += cur_frag_size;
1469 if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) {
1470 put_page(page);
1471 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1472 dev->name, len, (unsigned long)(truesize - room));
1473 DEV_STATS_INC(dev, rx_length_errors);
1474 goto err;
1475 }
1476
1477 frag = &shinfo->frags[shinfo->nr_frags++];
1478 skb_frag_fill_page_desc(frag, page, offset, len);
1479 if (page_is_pfmemalloc(page))
1480 xdp_buff_set_frag_pfmemalloc(xdp);
1481
1482 shinfo->xdp_frags_size += len;
1483 }
1484
1485 *xdp_frags_truesize = xdp_frags_truesz;
1486 return 0;
1487
1488 err:
1489 put_xdp_frags(xdp);
1490 return -EINVAL;
1491 }
1492
mergeable_xdp_get_buf(struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * ctx,unsigned int * frame_sz,int * num_buf,struct page ** page,int offset,unsigned int * len,struct virtio_net_hdr_mrg_rxbuf * hdr)1493 static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
1494 struct receive_queue *rq,
1495 struct bpf_prog *xdp_prog,
1496 void *ctx,
1497 unsigned int *frame_sz,
1498 int *num_buf,
1499 struct page **page,
1500 int offset,
1501 unsigned int *len,
1502 struct virtio_net_hdr_mrg_rxbuf *hdr)
1503 {
1504 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
1505 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1506 struct page *xdp_page;
1507 unsigned int xdp_room;
1508
1509 /* Transient failure which in theory could occur if
1510 * in-flight packets from before XDP was enabled reach
1511 * the receive path after XDP is loaded.
1512 */
1513 if (unlikely(hdr->hdr.gso_type))
1514 return NULL;
1515
1516 /* Partially checksummed packets must be dropped. */
1517 if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
1518 return NULL;
1519
1520 /* Now XDP core assumes frag size is PAGE_SIZE, but buffers
1521 * with headroom may add hole in truesize, which
1522 * make their length exceed PAGE_SIZE. So we disabled the
1523 * hole mechanism for xdp. See add_recvbuf_mergeable().
1524 */
1525 *frame_sz = truesize;
1526
1527 if (likely(headroom >= virtnet_get_headroom(vi) &&
1528 (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) {
1529 return page_address(*page) + offset;
1530 }
1531
1532 /* This happens when headroom is not enough because
1533 * of the buffer was prefilled before XDP is set.
1534 * This should only happen for the first several packets.
1535 * In fact, vq reset can be used here to help us clean up
1536 * the prefilled buffers, but many existing devices do not
1537 * support it, and we don't want to bother users who are
1538 * using xdp normally.
1539 */
1540 if (!xdp_prog->aux->xdp_has_frags) {
1541 /* linearize data for XDP */
1542 xdp_page = xdp_linearize_page(rq, num_buf,
1543 *page, offset,
1544 VIRTIO_XDP_HEADROOM,
1545 len);
1546 if (!xdp_page)
1547 return NULL;
1548 } else {
1549 xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
1550 sizeof(struct skb_shared_info));
1551 if (*len + xdp_room > PAGE_SIZE)
1552 return NULL;
1553
1554 xdp_page = alloc_page(GFP_ATOMIC);
1555 if (!xdp_page)
1556 return NULL;
1557
1558 memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM,
1559 page_address(*page) + offset, *len);
1560 }
1561
1562 *frame_sz = PAGE_SIZE;
1563
1564 put_page(*page);
1565
1566 *page = xdp_page;
1567
1568 return page_address(*page) + VIRTIO_XDP_HEADROOM;
1569 }
1570
receive_mergeable_xdp(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1571 static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
1572 struct virtnet_info *vi,
1573 struct receive_queue *rq,
1574 struct bpf_prog *xdp_prog,
1575 void *buf,
1576 void *ctx,
1577 unsigned int len,
1578 unsigned int *xdp_xmit,
1579 struct virtnet_rq_stats *stats)
1580 {
1581 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1582 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1583 struct page *page = virt_to_head_page(buf);
1584 int offset = buf - page_address(page);
1585 unsigned int xdp_frags_truesz = 0;
1586 struct sk_buff *head_skb;
1587 unsigned int frame_sz;
1588 struct xdp_buff xdp;
1589 void *data;
1590 u32 act;
1591 int err;
1592
1593 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page,
1594 offset, &len, hdr);
1595 if (unlikely(!data))
1596 goto err_xdp;
1597
1598 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
1599 &num_buf, &xdp_frags_truesz, stats);
1600 if (unlikely(err))
1601 goto err_xdp;
1602
1603 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1604
1605 switch (act) {
1606 case XDP_PASS:
1607 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
1608 if (unlikely(!head_skb))
1609 break;
1610 return head_skb;
1611
1612 case XDP_TX:
1613 case XDP_REDIRECT:
1614 return NULL;
1615
1616 default:
1617 break;
1618 }
1619
1620 put_xdp_frags(&xdp);
1621
1622 err_xdp:
1623 put_page(page);
1624 mergeable_buf_free(rq, num_buf, dev, stats);
1625
1626 u64_stats_inc(&stats->xdp_drops);
1627 u64_stats_inc(&stats->drops);
1628 return NULL;
1629 }
1630
receive_mergeable(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1631 static struct sk_buff *receive_mergeable(struct net_device *dev,
1632 struct virtnet_info *vi,
1633 struct receive_queue *rq,
1634 void *buf,
1635 void *ctx,
1636 unsigned int len,
1637 unsigned int *xdp_xmit,
1638 struct virtnet_rq_stats *stats)
1639 {
1640 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1641 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1642 struct page *page = virt_to_head_page(buf);
1643 int offset = buf - page_address(page);
1644 struct sk_buff *head_skb, *curr_skb;
1645 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
1646 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1647 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1648 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1649
1650 head_skb = NULL;
1651 u64_stats_add(&stats->bytes, len - vi->hdr_len);
1652
1653 if (unlikely(len > truesize - room)) {
1654 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1655 dev->name, len, (unsigned long)(truesize - room));
1656 DEV_STATS_INC(dev, rx_length_errors);
1657 goto err_skb;
1658 }
1659
1660 if (unlikely(vi->xdp_enabled)) {
1661 struct bpf_prog *xdp_prog;
1662
1663 rcu_read_lock();
1664 xdp_prog = rcu_dereference(rq->xdp_prog);
1665 if (xdp_prog) {
1666 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx,
1667 len, xdp_xmit, stats);
1668 rcu_read_unlock();
1669 return head_skb;
1670 }
1671 rcu_read_unlock();
1672 }
1673
1674 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom);
1675 curr_skb = head_skb;
1676
1677 if (unlikely(!curr_skb))
1678 goto err_skb;
1679 while (--num_buf) {
1680 int num_skb_frags;
1681
1682 buf = virtnet_rq_get_buf(rq, &len, &ctx);
1683 if (unlikely(!buf)) {
1684 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1685 dev->name, num_buf,
1686 virtio16_to_cpu(vi->vdev,
1687 hdr->num_buffers));
1688 DEV_STATS_INC(dev, rx_length_errors);
1689 goto err_buf;
1690 }
1691
1692 u64_stats_add(&stats->bytes, len);
1693 page = virt_to_head_page(buf);
1694
1695 truesize = mergeable_ctx_to_truesize(ctx);
1696 headroom = mergeable_ctx_to_headroom(ctx);
1697 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1698 room = SKB_DATA_ALIGN(headroom + tailroom);
1699 if (unlikely(len > truesize - room)) {
1700 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1701 dev->name, len, (unsigned long)(truesize - room));
1702 DEV_STATS_INC(dev, rx_length_errors);
1703 goto err_skb;
1704 }
1705
1706 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
1707 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1708 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
1709
1710 if (unlikely(!nskb))
1711 goto err_skb;
1712 if (curr_skb == head_skb)
1713 skb_shinfo(curr_skb)->frag_list = nskb;
1714 else
1715 curr_skb->next = nskb;
1716 curr_skb = nskb;
1717 head_skb->truesize += nskb->truesize;
1718 num_skb_frags = 0;
1719 }
1720 if (curr_skb != head_skb) {
1721 head_skb->data_len += len;
1722 head_skb->len += len;
1723 head_skb->truesize += truesize;
1724 }
1725 offset = buf - page_address(page);
1726 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1727 put_page(page);
1728 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1729 len, truesize);
1730 } else {
1731 skb_add_rx_frag(curr_skb, num_skb_frags, page,
1732 offset, len, truesize);
1733 }
1734 }
1735
1736 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
1737 return head_skb;
1738
1739 err_skb:
1740 put_page(page);
1741 mergeable_buf_free(rq, num_buf, dev, stats);
1742
1743 err_buf:
1744 u64_stats_inc(&stats->drops);
1745 dev_kfree_skb(head_skb);
1746 return NULL;
1747 }
1748
virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash * hdr_hash,struct sk_buff * skb)1749 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
1750 struct sk_buff *skb)
1751 {
1752 enum pkt_hash_types rss_hash_type;
1753
1754 if (!hdr_hash || !skb)
1755 return;
1756
1757 switch (__le16_to_cpu(hdr_hash->hash_report)) {
1758 case VIRTIO_NET_HASH_REPORT_TCPv4:
1759 case VIRTIO_NET_HASH_REPORT_UDPv4:
1760 case VIRTIO_NET_HASH_REPORT_TCPv6:
1761 case VIRTIO_NET_HASH_REPORT_UDPv6:
1762 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
1763 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
1764 rss_hash_type = PKT_HASH_TYPE_L4;
1765 break;
1766 case VIRTIO_NET_HASH_REPORT_IPv4:
1767 case VIRTIO_NET_HASH_REPORT_IPv6:
1768 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
1769 rss_hash_type = PKT_HASH_TYPE_L3;
1770 break;
1771 case VIRTIO_NET_HASH_REPORT_NONE:
1772 default:
1773 rss_hash_type = PKT_HASH_TYPE_NONE;
1774 }
1775 skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
1776 }
1777
receive_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,void ** ctx,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1778 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1779 void *buf, unsigned int len, void **ctx,
1780 unsigned int *xdp_xmit,
1781 struct virtnet_rq_stats *stats)
1782 {
1783 struct net_device *dev = vi->dev;
1784 struct sk_buff *skb;
1785 struct virtio_net_common_hdr *hdr;
1786 u8 flags;
1787
1788 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
1789 pr_debug("%s: short packet %i\n", dev->name, len);
1790 DEV_STATS_INC(dev, rx_length_errors);
1791 virtnet_rq_free_buf(vi, rq, buf);
1792 return;
1793 }
1794
1795 /* 1. Save the flags early, as the XDP program might overwrite them.
1796 * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
1797 * stay valid after XDP processing.
1798 * 2. XDP doesn't work with partially checksummed packets (refer to
1799 * virtnet_xdp_set()), so packets marked as
1800 * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
1801 */
1802 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
1803
1804 if (vi->mergeable_rx_bufs)
1805 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1806 stats);
1807 else if (vi->big_packets)
1808 skb = receive_big(dev, vi, rq, buf, len, stats);
1809 else
1810 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1811
1812 if (unlikely(!skb))
1813 return;
1814
1815 hdr = skb_vnet_common_hdr(skb);
1816 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
1817 virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
1818
1819 if (flags & VIRTIO_NET_HDR_F_DATA_VALID)
1820 skb->ip_summed = CHECKSUM_UNNECESSARY;
1821
1822 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1823 virtio_is_little_endian(vi->vdev))) {
1824 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1825 dev->name, hdr->hdr.gso_type,
1826 hdr->hdr.gso_size);
1827 goto frame_err;
1828 }
1829
1830 skb_record_rx_queue(skb, vq2rxq(rq->vq));
1831 skb->protocol = eth_type_trans(skb, dev);
1832 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1833 ntohs(skb->protocol), skb->len, skb->pkt_type);
1834
1835 napi_gro_receive(&rq->napi, skb);
1836 return;
1837
1838 frame_err:
1839 DEV_STATS_INC(dev, rx_frame_errors);
1840 dev_kfree_skb(skb);
1841 }
1842
1843 /* Unlike mergeable buffers, all buffers are allocated to the
1844 * same size, except for the headroom. For this reason we do
1845 * not need to use mergeable_len_to_ctx here - it is enough
1846 * to store the headroom as the context ignoring the truesize.
1847 */
add_recvbuf_small(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1848 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1849 gfp_t gfp)
1850 {
1851 char *buf;
1852 unsigned int xdp_headroom = virtnet_get_headroom(vi);
1853 void *ctx = (void *)(unsigned long)xdp_headroom;
1854 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
1855 int err;
1856
1857 len = SKB_DATA_ALIGN(len) +
1858 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1859
1860 if (unlikely(!skb_page_frag_refill(len, &rq->alloc_frag, gfp)))
1861 return -ENOMEM;
1862
1863 buf = virtnet_rq_alloc(rq, len, gfp);
1864 if (unlikely(!buf))
1865 return -ENOMEM;
1866
1867 buf += VIRTNET_RX_PAD + xdp_headroom;
1868
1869 virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN);
1870
1871 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1872 if (err < 0) {
1873 if (rq->do_dma)
1874 virtnet_rq_unmap(rq, buf, 0);
1875 put_page(virt_to_head_page(buf));
1876 }
1877
1878 return err;
1879 }
1880
add_recvbuf_big(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1881 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1882 gfp_t gfp)
1883 {
1884 struct page *first, *list = NULL;
1885 char *p;
1886 int i, err, offset;
1887
1888 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
1889
1890 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
1891 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
1892 first = get_a_page(rq, gfp);
1893 if (!first) {
1894 if (list)
1895 give_pages(rq, list);
1896 return -ENOMEM;
1897 }
1898 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
1899
1900 /* chain new page in list head to match sg */
1901 first->private = (unsigned long)list;
1902 list = first;
1903 }
1904
1905 first = get_a_page(rq, gfp);
1906 if (!first) {
1907 give_pages(rq, list);
1908 return -ENOMEM;
1909 }
1910 p = page_address(first);
1911
1912 /* rq->sg[0], rq->sg[1] share the same page */
1913 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1914 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
1915
1916 /* rq->sg[1] for data packet, from offset */
1917 offset = sizeof(struct padded_vnet_hdr);
1918 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
1919
1920 /* chain first in list head */
1921 first->private = (unsigned long)list;
1922 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
1923 first, gfp);
1924 if (err < 0)
1925 give_pages(rq, first);
1926
1927 return err;
1928 }
1929
get_mergeable_buf_len(struct receive_queue * rq,struct ewma_pkt_len * avg_pkt_len,unsigned int room)1930 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
1931 struct ewma_pkt_len *avg_pkt_len,
1932 unsigned int room)
1933 {
1934 struct virtnet_info *vi = rq->vq->vdev->priv;
1935 const size_t hdr_len = vi->hdr_len;
1936 unsigned int len;
1937
1938 if (room)
1939 return PAGE_SIZE - room;
1940
1941 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1942 rq->min_buf_len, PAGE_SIZE - hdr_len);
1943
1944 return ALIGN(len, L1_CACHE_BYTES);
1945 }
1946
add_recvbuf_mergeable(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1947 static int add_recvbuf_mergeable(struct virtnet_info *vi,
1948 struct receive_queue *rq, gfp_t gfp)
1949 {
1950 struct page_frag *alloc_frag = &rq->alloc_frag;
1951 unsigned int headroom = virtnet_get_headroom(vi);
1952 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1953 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1954 unsigned int len, hole;
1955 void *ctx;
1956 char *buf;
1957 int err;
1958
1959 /* Extra tailroom is needed to satisfy XDP's assumption. This
1960 * means rx frags coalescing won't work, but consider we've
1961 * disabled GSO for XDP, it won't be a big issue.
1962 */
1963 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1964
1965 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
1966 return -ENOMEM;
1967
1968 if (!alloc_frag->offset && len + room + sizeof(struct virtnet_rq_dma) > alloc_frag->size)
1969 len -= sizeof(struct virtnet_rq_dma);
1970
1971 buf = virtnet_rq_alloc(rq, len + room, gfp);
1972 if (unlikely(!buf))
1973 return -ENOMEM;
1974
1975 buf += headroom; /* advance address leaving hole at front of pkt */
1976 hole = alloc_frag->size - alloc_frag->offset;
1977 if (hole < len + room) {
1978 /* To avoid internal fragmentation, if there is very likely not
1979 * enough space for another buffer, add the remaining space to
1980 * the current buffer.
1981 * XDP core assumes that frame_size of xdp_buff and the length
1982 * of the frag are PAGE_SIZE, so we disable the hole mechanism.
1983 */
1984 if (!headroom)
1985 len += hole;
1986 alloc_frag->offset += hole;
1987 }
1988
1989 virtnet_rq_init_one_sg(rq, buf, len);
1990
1991 ctx = mergeable_len_to_ctx(len + room, headroom);
1992 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1993 if (err < 0) {
1994 if (rq->do_dma)
1995 virtnet_rq_unmap(rq, buf, 0);
1996 put_page(virt_to_head_page(buf));
1997 }
1998
1999 return err;
2000 }
2001
2002 /*
2003 * Returns false if we couldn't fill entirely (OOM).
2004 *
2005 * Normally run in the receive path, but can also be run from ndo_open
2006 * before we're receiving packets, or from refill_work which is
2007 * careful to disable receiving (using napi_disable).
2008 */
try_fill_recv(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2009 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
2010 gfp_t gfp)
2011 {
2012 int err;
2013 bool oom;
2014
2015 do {
2016 if (vi->mergeable_rx_bufs)
2017 err = add_recvbuf_mergeable(vi, rq, gfp);
2018 else if (vi->big_packets)
2019 err = add_recvbuf_big(vi, rq, gfp);
2020 else
2021 err = add_recvbuf_small(vi, rq, gfp);
2022
2023 oom = err == -ENOMEM;
2024 if (err)
2025 break;
2026 } while (rq->vq->num_free);
2027 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
2028 unsigned long flags;
2029
2030 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
2031 u64_stats_inc(&rq->stats.kicks);
2032 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
2033 }
2034
2035 return !oom;
2036 }
2037
skb_recv_done(struct virtqueue * rvq)2038 static void skb_recv_done(struct virtqueue *rvq)
2039 {
2040 struct virtnet_info *vi = rvq->vdev->priv;
2041 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
2042
2043 virtqueue_napi_schedule(&rq->napi, rvq);
2044 }
2045
virtnet_napi_enable(struct virtqueue * vq,struct napi_struct * napi)2046 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
2047 {
2048 napi_enable(napi);
2049
2050 /* If all buffers were filled by other side before we napi_enabled, we
2051 * won't get another interrupt, so process any outstanding packets now.
2052 * Call local_bh_enable after to trigger softIRQ processing.
2053 */
2054 local_bh_disable();
2055 virtqueue_napi_schedule(napi, vq);
2056 local_bh_enable();
2057 }
2058
virtnet_napi_tx_enable(struct virtnet_info * vi,struct virtqueue * vq,struct napi_struct * napi)2059 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
2060 struct virtqueue *vq,
2061 struct napi_struct *napi)
2062 {
2063 if (!napi->weight)
2064 return;
2065
2066 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
2067 * enable the feature if this is likely affine with the transmit path.
2068 */
2069 if (!vi->affinity_hint_set) {
2070 napi->weight = 0;
2071 return;
2072 }
2073
2074 return virtnet_napi_enable(vq, napi);
2075 }
2076
virtnet_napi_tx_disable(struct napi_struct * napi)2077 static void virtnet_napi_tx_disable(struct napi_struct *napi)
2078 {
2079 if (napi->weight)
2080 napi_disable(napi);
2081 }
2082
refill_work(struct work_struct * work)2083 static void refill_work(struct work_struct *work)
2084 {
2085 struct virtnet_info *vi =
2086 container_of(work, struct virtnet_info, refill.work);
2087 bool still_empty;
2088 int i;
2089
2090 for (i = 0; i < vi->curr_queue_pairs; i++) {
2091 struct receive_queue *rq = &vi->rq[i];
2092
2093 napi_disable(&rq->napi);
2094 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
2095 virtnet_napi_enable(rq->vq, &rq->napi);
2096
2097 /* In theory, this can happen: if we don't get any buffers in
2098 * we will *never* try to fill again.
2099 */
2100 if (still_empty)
2101 schedule_delayed_work(&vi->refill, HZ/2);
2102 }
2103 }
2104
virtnet_receive(struct receive_queue * rq,int budget,unsigned int * xdp_xmit)2105 static int virtnet_receive(struct receive_queue *rq, int budget,
2106 unsigned int *xdp_xmit)
2107 {
2108 struct virtnet_info *vi = rq->vq->vdev->priv;
2109 struct virtnet_rq_stats stats = {};
2110 unsigned int len;
2111 int packets = 0;
2112 void *buf;
2113 int i;
2114
2115 if (!vi->big_packets || vi->mergeable_rx_bufs) {
2116 void *ctx;
2117
2118 while (packets < budget &&
2119 (buf = virtnet_rq_get_buf(rq, &len, &ctx))) {
2120 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
2121 packets++;
2122 }
2123 } else {
2124 while (packets < budget &&
2125 (buf = virtnet_rq_get_buf(rq, &len, NULL)) != NULL) {
2126 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
2127 packets++;
2128 }
2129 }
2130
2131 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
2132 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
2133 spin_lock(&vi->refill_lock);
2134 if (vi->refill_enabled)
2135 schedule_delayed_work(&vi->refill, 0);
2136 spin_unlock(&vi->refill_lock);
2137 }
2138 }
2139
2140 u64_stats_set(&stats.packets, packets);
2141 u64_stats_update_begin(&rq->stats.syncp);
2142 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
2143 size_t offset = virtnet_rq_stats_desc[i].offset;
2144 u64_stats_t *item, *src;
2145
2146 item = (u64_stats_t *)((u8 *)&rq->stats + offset);
2147 src = (u64_stats_t *)((u8 *)&stats + offset);
2148 u64_stats_add(item, u64_stats_read(src));
2149 }
2150 u64_stats_update_end(&rq->stats.syncp);
2151
2152 return packets;
2153 }
2154
virtnet_poll_cleantx(struct receive_queue * rq,int budget)2155 static void virtnet_poll_cleantx(struct receive_queue *rq, int budget)
2156 {
2157 struct virtnet_info *vi = rq->vq->vdev->priv;
2158 unsigned int index = vq2rxq(rq->vq);
2159 struct send_queue *sq = &vi->sq[index];
2160 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
2161
2162 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
2163 return;
2164
2165 if (__netif_tx_trylock(txq)) {
2166 if (sq->reset) {
2167 __netif_tx_unlock(txq);
2168 return;
2169 }
2170
2171 do {
2172 virtqueue_disable_cb(sq->vq);
2173 free_old_xmit_skbs(sq, !!budget);
2174 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2175
2176 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
2177 netif_tx_wake_queue(txq);
2178
2179 __netif_tx_unlock(txq);
2180 }
2181 }
2182
virtnet_poll(struct napi_struct * napi,int budget)2183 static int virtnet_poll(struct napi_struct *napi, int budget)
2184 {
2185 struct receive_queue *rq =
2186 container_of(napi, struct receive_queue, napi);
2187 struct virtnet_info *vi = rq->vq->vdev->priv;
2188 struct send_queue *sq;
2189 unsigned int received;
2190 unsigned int xdp_xmit = 0;
2191
2192 virtnet_poll_cleantx(rq, budget);
2193
2194 received = virtnet_receive(rq, budget, &xdp_xmit);
2195
2196 if (xdp_xmit & VIRTIO_XDP_REDIR)
2197 xdp_do_flush();
2198
2199 /* Out of packets? */
2200 if (received < budget)
2201 virtqueue_napi_complete(napi, rq->vq, received);
2202
2203 if (xdp_xmit & VIRTIO_XDP_TX) {
2204 sq = virtnet_xdp_get_sq(vi);
2205 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2206 u64_stats_update_begin(&sq->stats.syncp);
2207 u64_stats_inc(&sq->stats.kicks);
2208 u64_stats_update_end(&sq->stats.syncp);
2209 }
2210 virtnet_xdp_put_sq(vi, sq);
2211 }
2212
2213 return received;
2214 }
2215
virtnet_disable_queue_pair(struct virtnet_info * vi,int qp_index)2216 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
2217 {
2218 virtnet_napi_tx_disable(&vi->sq[qp_index].napi);
2219 napi_disable(&vi->rq[qp_index].napi);
2220 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
2221 }
2222
virtnet_enable_queue_pair(struct virtnet_info * vi,int qp_index)2223 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
2224 {
2225 struct net_device *dev = vi->dev;
2226 int err;
2227
2228 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
2229 vi->rq[qp_index].napi.napi_id);
2230 if (err < 0)
2231 return err;
2232
2233 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
2234 MEM_TYPE_PAGE_SHARED, NULL);
2235 if (err < 0)
2236 goto err_xdp_reg_mem_model;
2237
2238 virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi);
2239 virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi);
2240
2241 return 0;
2242
2243 err_xdp_reg_mem_model:
2244 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
2245 return err;
2246 }
2247
virtnet_open(struct net_device * dev)2248 static int virtnet_open(struct net_device *dev)
2249 {
2250 struct virtnet_info *vi = netdev_priv(dev);
2251 int i, err;
2252
2253 enable_delayed_refill(vi);
2254
2255 for (i = 0; i < vi->max_queue_pairs; i++) {
2256 if (i < vi->curr_queue_pairs)
2257 /* Make sure we have some buffers: if oom use wq. */
2258 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2259 schedule_delayed_work(&vi->refill, 0);
2260
2261 err = virtnet_enable_queue_pair(vi, i);
2262 if (err < 0)
2263 goto err_enable_qp;
2264 }
2265
2266 return 0;
2267
2268 err_enable_qp:
2269 disable_delayed_refill(vi);
2270 cancel_delayed_work_sync(&vi->refill);
2271
2272 for (i--; i >= 0; i--)
2273 virtnet_disable_queue_pair(vi, i);
2274 return err;
2275 }
2276
virtnet_poll_tx(struct napi_struct * napi,int budget)2277 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
2278 {
2279 struct send_queue *sq = container_of(napi, struct send_queue, napi);
2280 struct virtnet_info *vi = sq->vq->vdev->priv;
2281 unsigned int index = vq2txq(sq->vq);
2282 struct netdev_queue *txq;
2283 int opaque;
2284 bool done;
2285
2286 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
2287 /* We don't need to enable cb for XDP */
2288 napi_complete_done(napi, 0);
2289 return 0;
2290 }
2291
2292 txq = netdev_get_tx_queue(vi->dev, index);
2293 __netif_tx_lock(txq, raw_smp_processor_id());
2294 virtqueue_disable_cb(sq->vq);
2295 free_old_xmit_skbs(sq, !!budget);
2296
2297 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
2298 netif_tx_wake_queue(txq);
2299
2300 opaque = virtqueue_enable_cb_prepare(sq->vq);
2301
2302 done = napi_complete_done(napi, 0);
2303
2304 if (!done)
2305 virtqueue_disable_cb(sq->vq);
2306
2307 __netif_tx_unlock(txq);
2308
2309 if (done) {
2310 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
2311 if (napi_schedule_prep(napi)) {
2312 __netif_tx_lock(txq, raw_smp_processor_id());
2313 virtqueue_disable_cb(sq->vq);
2314 __netif_tx_unlock(txq);
2315 __napi_schedule(napi);
2316 }
2317 }
2318 }
2319
2320 return 0;
2321 }
2322
xmit_skb(struct send_queue * sq,struct sk_buff * skb)2323 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
2324 {
2325 struct virtio_net_hdr_mrg_rxbuf *hdr;
2326 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
2327 struct virtnet_info *vi = sq->vq->vdev->priv;
2328 int num_sg;
2329 unsigned hdr_len = vi->hdr_len;
2330 bool can_push;
2331
2332 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
2333
2334 can_push = vi->any_header_sg &&
2335 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
2336 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
2337 /* Even if we can, don't push here yet as this would skew
2338 * csum_start offset below. */
2339 if (can_push)
2340 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
2341 else
2342 hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
2343
2344 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
2345 virtio_is_little_endian(vi->vdev), false,
2346 0))
2347 return -EPROTO;
2348
2349 if (vi->mergeable_rx_bufs)
2350 hdr->num_buffers = 0;
2351
2352 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
2353 if (can_push) {
2354 __skb_push(skb, hdr_len);
2355 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
2356 if (unlikely(num_sg < 0))
2357 return num_sg;
2358 /* Pull header back to avoid skew in tx bytes calculations. */
2359 __skb_pull(skb, hdr_len);
2360 } else {
2361 sg_set_buf(sq->sg, hdr, hdr_len);
2362 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
2363 if (unlikely(num_sg < 0))
2364 return num_sg;
2365 num_sg++;
2366 }
2367 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
2368 }
2369
start_xmit(struct sk_buff * skb,struct net_device * dev)2370 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
2371 {
2372 struct virtnet_info *vi = netdev_priv(dev);
2373 int qnum = skb_get_queue_mapping(skb);
2374 struct send_queue *sq = &vi->sq[qnum];
2375 int err;
2376 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
2377 bool kick = !netdev_xmit_more();
2378 bool use_napi = sq->napi.weight;
2379
2380 /* Free up any pending old buffers before queueing new ones. */
2381 do {
2382 if (use_napi)
2383 virtqueue_disable_cb(sq->vq);
2384
2385 free_old_xmit_skbs(sq, false);
2386
2387 } while (use_napi && kick &&
2388 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2389
2390 /* timestamp packet in software */
2391 skb_tx_timestamp(skb);
2392
2393 /* Try to transmit */
2394 err = xmit_skb(sq, skb);
2395
2396 /* This should not happen! */
2397 if (unlikely(err)) {
2398 DEV_STATS_INC(dev, tx_fifo_errors);
2399 if (net_ratelimit())
2400 dev_warn(&dev->dev,
2401 "Unexpected TXQ (%d) queue failure: %d\n",
2402 qnum, err);
2403 DEV_STATS_INC(dev, tx_dropped);
2404 dev_kfree_skb_any(skb);
2405 return NETDEV_TX_OK;
2406 }
2407
2408 /* Don't wait up for transmitted skbs to be freed. */
2409 if (!use_napi) {
2410 skb_orphan(skb);
2411 nf_reset_ct(skb);
2412 }
2413
2414 check_sq_full_and_disable(vi, dev, sq);
2415
2416 if (kick || netif_xmit_stopped(txq)) {
2417 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2418 u64_stats_update_begin(&sq->stats.syncp);
2419 u64_stats_inc(&sq->stats.kicks);
2420 u64_stats_update_end(&sq->stats.syncp);
2421 }
2422 }
2423
2424 return NETDEV_TX_OK;
2425 }
2426
virtnet_rx_resize(struct virtnet_info * vi,struct receive_queue * rq,u32 ring_num)2427 static int virtnet_rx_resize(struct virtnet_info *vi,
2428 struct receive_queue *rq, u32 ring_num)
2429 {
2430 bool running = netif_running(vi->dev);
2431 int err, qindex;
2432
2433 qindex = rq - vi->rq;
2434
2435 if (running)
2436 napi_disable(&rq->napi);
2437
2438 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf);
2439 if (err)
2440 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
2441
2442 if (!try_fill_recv(vi, rq, GFP_KERNEL))
2443 schedule_delayed_work(&vi->refill, 0);
2444
2445 if (running)
2446 virtnet_napi_enable(rq->vq, &rq->napi);
2447 return err;
2448 }
2449
virtnet_tx_resize(struct virtnet_info * vi,struct send_queue * sq,u32 ring_num)2450 static int virtnet_tx_resize(struct virtnet_info *vi,
2451 struct send_queue *sq, u32 ring_num)
2452 {
2453 bool running = netif_running(vi->dev);
2454 struct netdev_queue *txq;
2455 int err, qindex;
2456
2457 qindex = sq - vi->sq;
2458
2459 if (running)
2460 virtnet_napi_tx_disable(&sq->napi);
2461
2462 txq = netdev_get_tx_queue(vi->dev, qindex);
2463
2464 /* 1. wait all ximt complete
2465 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
2466 */
2467 __netif_tx_lock_bh(txq);
2468
2469 /* Prevent rx poll from accessing sq. */
2470 sq->reset = true;
2471
2472 /* Prevent the upper layer from trying to send packets. */
2473 netif_stop_subqueue(vi->dev, qindex);
2474
2475 __netif_tx_unlock_bh(txq);
2476
2477 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf);
2478 if (err)
2479 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
2480
2481 __netif_tx_lock_bh(txq);
2482 sq->reset = false;
2483 netif_tx_wake_queue(txq);
2484 __netif_tx_unlock_bh(txq);
2485
2486 if (running)
2487 virtnet_napi_tx_enable(vi, sq->vq, &sq->napi);
2488 return err;
2489 }
2490
2491 /*
2492 * Send command via the control virtqueue and check status. Commands
2493 * supported by the hypervisor, as indicated by feature bits, should
2494 * never fail unless improperly formatted.
2495 */
virtnet_send_command(struct virtnet_info * vi,u8 class,u8 cmd,struct scatterlist * out)2496 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
2497 struct scatterlist *out)
2498 {
2499 struct scatterlist *sgs[4], hdr, stat;
2500 unsigned out_num = 0, tmp;
2501 int ret;
2502
2503 /* Caller should know better */
2504 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
2505
2506 vi->ctrl->status = ~0;
2507 vi->ctrl->hdr.class = class;
2508 vi->ctrl->hdr.cmd = cmd;
2509 /* Add header */
2510 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
2511 sgs[out_num++] = &hdr;
2512
2513 if (out)
2514 sgs[out_num++] = out;
2515
2516 /* Add return status. */
2517 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
2518 sgs[out_num] = &stat;
2519
2520 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
2521 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
2522 if (ret < 0) {
2523 dev_warn(&vi->vdev->dev,
2524 "Failed to add sgs for command vq: %d\n.", ret);
2525 return false;
2526 }
2527
2528 if (unlikely(!virtqueue_kick(vi->cvq)))
2529 return vi->ctrl->status == VIRTIO_NET_OK;
2530
2531 /* Spin for a response, the kick causes an ioport write, trapping
2532 * into the hypervisor, so the request should be handled immediately.
2533 */
2534 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
2535 !virtqueue_is_broken(vi->cvq))
2536 cpu_relax();
2537
2538 return vi->ctrl->status == VIRTIO_NET_OK;
2539 }
2540
virtnet_set_mac_address(struct net_device * dev,void * p)2541 static int virtnet_set_mac_address(struct net_device *dev, void *p)
2542 {
2543 struct virtnet_info *vi = netdev_priv(dev);
2544 struct virtio_device *vdev = vi->vdev;
2545 int ret;
2546 struct sockaddr *addr;
2547 struct scatterlist sg;
2548
2549 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2550 return -EOPNOTSUPP;
2551
2552 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
2553 if (!addr)
2554 return -ENOMEM;
2555
2556 ret = eth_prepare_mac_addr_change(dev, addr);
2557 if (ret)
2558 goto out;
2559
2560 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
2561 sg_init_one(&sg, addr->sa_data, dev->addr_len);
2562 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2563 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
2564 dev_warn(&vdev->dev,
2565 "Failed to set mac address by vq command.\n");
2566 ret = -EINVAL;
2567 goto out;
2568 }
2569 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
2570 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2571 unsigned int i;
2572
2573 /* Naturally, this has an atomicity problem. */
2574 for (i = 0; i < dev->addr_len; i++)
2575 virtio_cwrite8(vdev,
2576 offsetof(struct virtio_net_config, mac) +
2577 i, addr->sa_data[i]);
2578 }
2579
2580 eth_commit_mac_addr_change(dev, p);
2581 ret = 0;
2582
2583 out:
2584 kfree(addr);
2585 return ret;
2586 }
2587
virtnet_stats(struct net_device * dev,struct rtnl_link_stats64 * tot)2588 static void virtnet_stats(struct net_device *dev,
2589 struct rtnl_link_stats64 *tot)
2590 {
2591 struct virtnet_info *vi = netdev_priv(dev);
2592 unsigned int start;
2593 int i;
2594
2595 for (i = 0; i < vi->max_queue_pairs; i++) {
2596 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
2597 struct receive_queue *rq = &vi->rq[i];
2598 struct send_queue *sq = &vi->sq[i];
2599
2600 do {
2601 start = u64_stats_fetch_begin(&sq->stats.syncp);
2602 tpackets = u64_stats_read(&sq->stats.packets);
2603 tbytes = u64_stats_read(&sq->stats.bytes);
2604 terrors = u64_stats_read(&sq->stats.tx_timeouts);
2605 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
2606
2607 do {
2608 start = u64_stats_fetch_begin(&rq->stats.syncp);
2609 rpackets = u64_stats_read(&rq->stats.packets);
2610 rbytes = u64_stats_read(&rq->stats.bytes);
2611 rdrops = u64_stats_read(&rq->stats.drops);
2612 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
2613
2614 tot->rx_packets += rpackets;
2615 tot->tx_packets += tpackets;
2616 tot->rx_bytes += rbytes;
2617 tot->tx_bytes += tbytes;
2618 tot->rx_dropped += rdrops;
2619 tot->tx_errors += terrors;
2620 }
2621
2622 tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
2623 tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors);
2624 tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors);
2625 tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors);
2626 }
2627
virtnet_ack_link_announce(struct virtnet_info * vi)2628 static void virtnet_ack_link_announce(struct virtnet_info *vi)
2629 {
2630 rtnl_lock();
2631 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
2632 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
2633 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
2634 rtnl_unlock();
2635 }
2636
_virtnet_set_queues(struct virtnet_info * vi,u16 queue_pairs)2637 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2638 {
2639 struct scatterlist sg;
2640 struct net_device *dev = vi->dev;
2641
2642 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
2643 return 0;
2644
2645 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
2646 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
2647
2648 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2649 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
2650 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
2651 queue_pairs);
2652 return -EINVAL;
2653 } else {
2654 vi->curr_queue_pairs = queue_pairs;
2655 /* virtnet_open() will refill when device is going to up. */
2656 if (dev->flags & IFF_UP)
2657 schedule_delayed_work(&vi->refill, 0);
2658 }
2659
2660 return 0;
2661 }
2662
virtnet_set_queues(struct virtnet_info * vi,u16 queue_pairs)2663 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2664 {
2665 int err;
2666
2667 rtnl_lock();
2668 err = _virtnet_set_queues(vi, queue_pairs);
2669 rtnl_unlock();
2670 return err;
2671 }
2672
virtnet_close(struct net_device * dev)2673 static int virtnet_close(struct net_device *dev)
2674 {
2675 struct virtnet_info *vi = netdev_priv(dev);
2676 int i;
2677
2678 /* Make sure NAPI doesn't schedule refill work */
2679 disable_delayed_refill(vi);
2680 /* Make sure refill_work doesn't re-enable napi! */
2681 cancel_delayed_work_sync(&vi->refill);
2682
2683 for (i = 0; i < vi->max_queue_pairs; i++)
2684 virtnet_disable_queue_pair(vi, i);
2685
2686 return 0;
2687 }
2688
virtnet_set_rx_mode(struct net_device * dev)2689 static void virtnet_set_rx_mode(struct net_device *dev)
2690 {
2691 struct virtnet_info *vi = netdev_priv(dev);
2692 struct scatterlist sg[2];
2693 struct virtio_net_ctrl_mac *mac_data;
2694 struct netdev_hw_addr *ha;
2695 int uc_count;
2696 int mc_count;
2697 void *buf;
2698 int i;
2699
2700 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
2701 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
2702 return;
2703
2704 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
2705 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2706
2707 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
2708
2709 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2710 VIRTIO_NET_CTRL_RX_PROMISC, sg))
2711 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
2712 vi->ctrl->promisc ? "en" : "dis");
2713
2714 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
2715
2716 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2717 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2718 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2719 vi->ctrl->allmulti ? "en" : "dis");
2720
2721 uc_count = netdev_uc_count(dev);
2722 mc_count = netdev_mc_count(dev);
2723 /* MAC filter - use one buffer for both lists */
2724 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2725 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
2726 mac_data = buf;
2727 if (!buf)
2728 return;
2729
2730 sg_init_table(sg, 2);
2731
2732 /* Store the unicast list and count in the front of the buffer */
2733 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2734 i = 0;
2735 netdev_for_each_uc_addr(ha, dev)
2736 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2737
2738 sg_set_buf(&sg[0], mac_data,
2739 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2740
2741 /* multicast list and count fill the end */
2742 mac_data = (void *)&mac_data->macs[uc_count][0];
2743
2744 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2745 i = 0;
2746 netdev_for_each_mc_addr(ha, dev)
2747 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2748
2749 sg_set_buf(&sg[1], mac_data,
2750 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2751
2752 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2753 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
2754 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2755
2756 kfree(buf);
2757 }
2758
virtnet_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)2759 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2760 __be16 proto, u16 vid)
2761 {
2762 struct virtnet_info *vi = netdev_priv(dev);
2763 struct scatterlist sg;
2764
2765 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2766 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2767
2768 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2769 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
2770 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
2771 return 0;
2772 }
2773
virtnet_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)2774 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2775 __be16 proto, u16 vid)
2776 {
2777 struct virtnet_info *vi = netdev_priv(dev);
2778 struct scatterlist sg;
2779
2780 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2781 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2782
2783 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2784 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
2785 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
2786 return 0;
2787 }
2788
virtnet_clean_affinity(struct virtnet_info * vi)2789 static void virtnet_clean_affinity(struct virtnet_info *vi)
2790 {
2791 int i;
2792
2793 if (vi->affinity_hint_set) {
2794 for (i = 0; i < vi->max_queue_pairs; i++) {
2795 virtqueue_set_affinity(vi->rq[i].vq, NULL);
2796 virtqueue_set_affinity(vi->sq[i].vq, NULL);
2797 }
2798
2799 vi->affinity_hint_set = false;
2800 }
2801 }
2802
virtnet_set_affinity(struct virtnet_info * vi)2803 static void virtnet_set_affinity(struct virtnet_info *vi)
2804 {
2805 cpumask_var_t mask;
2806 int stragglers;
2807 int group_size;
2808 int i, j, cpu;
2809 int num_cpu;
2810 int stride;
2811
2812 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2813 virtnet_clean_affinity(vi);
2814 return;
2815 }
2816
2817 num_cpu = num_online_cpus();
2818 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2819 stragglers = num_cpu >= vi->curr_queue_pairs ?
2820 num_cpu % vi->curr_queue_pairs :
2821 0;
2822 cpu = cpumask_first(cpu_online_mask);
2823
2824 for (i = 0; i < vi->curr_queue_pairs; i++) {
2825 group_size = stride + (i < stragglers ? 1 : 0);
2826
2827 for (j = 0; j < group_size; j++) {
2828 cpumask_set_cpu(cpu, mask);
2829 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2830 nr_cpu_ids, false);
2831 }
2832 virtqueue_set_affinity(vi->rq[i].vq, mask);
2833 virtqueue_set_affinity(vi->sq[i].vq, mask);
2834 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
2835 cpumask_clear(mask);
2836 }
2837
2838 vi->affinity_hint_set = true;
2839 free_cpumask_var(mask);
2840 }
2841
virtnet_cpu_online(unsigned int cpu,struct hlist_node * node)2842 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
2843 {
2844 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2845 node);
2846 virtnet_set_affinity(vi);
2847 return 0;
2848 }
2849
virtnet_cpu_dead(unsigned int cpu,struct hlist_node * node)2850 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2851 {
2852 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2853 node_dead);
2854 virtnet_set_affinity(vi);
2855 return 0;
2856 }
2857
virtnet_cpu_down_prep(unsigned int cpu,struct hlist_node * node)2858 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2859 {
2860 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2861 node);
2862
2863 virtnet_clean_affinity(vi);
2864 return 0;
2865 }
2866
2867 static enum cpuhp_state virtionet_online;
2868
virtnet_cpu_notif_add(struct virtnet_info * vi)2869 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2870 {
2871 int ret;
2872
2873 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2874 if (ret)
2875 return ret;
2876 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2877 &vi->node_dead);
2878 if (!ret)
2879 return ret;
2880 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2881 return ret;
2882 }
2883
virtnet_cpu_notif_remove(struct virtnet_info * vi)2884 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2885 {
2886 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2887 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2888 &vi->node_dead);
2889 }
2890
virtnet_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)2891 static void virtnet_get_ringparam(struct net_device *dev,
2892 struct ethtool_ringparam *ring,
2893 struct kernel_ethtool_ringparam *kernel_ring,
2894 struct netlink_ext_ack *extack)
2895 {
2896 struct virtnet_info *vi = netdev_priv(dev);
2897
2898 ring->rx_max_pending = vi->rq[0].vq->num_max;
2899 ring->tx_max_pending = vi->sq[0].vq->num_max;
2900 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2901 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2902 }
2903
2904 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
2905 u16 vqn, u32 max_usecs, u32 max_packets);
2906
virtnet_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)2907 static int virtnet_set_ringparam(struct net_device *dev,
2908 struct ethtool_ringparam *ring,
2909 struct kernel_ethtool_ringparam *kernel_ring,
2910 struct netlink_ext_ack *extack)
2911 {
2912 struct virtnet_info *vi = netdev_priv(dev);
2913 u32 rx_pending, tx_pending;
2914 struct receive_queue *rq;
2915 struct send_queue *sq;
2916 int i, err;
2917
2918 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2919 return -EINVAL;
2920
2921 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2922 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2923
2924 if (ring->rx_pending == rx_pending &&
2925 ring->tx_pending == tx_pending)
2926 return 0;
2927
2928 if (ring->rx_pending > vi->rq[0].vq->num_max)
2929 return -EINVAL;
2930
2931 if (ring->tx_pending > vi->sq[0].vq->num_max)
2932 return -EINVAL;
2933
2934 for (i = 0; i < vi->max_queue_pairs; i++) {
2935 rq = vi->rq + i;
2936 sq = vi->sq + i;
2937
2938 if (ring->tx_pending != tx_pending) {
2939 err = virtnet_tx_resize(vi, sq, ring->tx_pending);
2940 if (err)
2941 return err;
2942
2943 /* Upon disabling and re-enabling a transmit virtqueue, the device must
2944 * set the coalescing parameters of the virtqueue to those configured
2945 * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver
2946 * did not set any TX coalescing parameters, to 0.
2947 */
2948 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(i),
2949 vi->intr_coal_tx.max_usecs,
2950 vi->intr_coal_tx.max_packets);
2951 if (err)
2952 return err;
2953
2954 vi->sq[i].intr_coal.max_usecs = vi->intr_coal_tx.max_usecs;
2955 vi->sq[i].intr_coal.max_packets = vi->intr_coal_tx.max_packets;
2956 }
2957
2958 if (ring->rx_pending != rx_pending) {
2959 err = virtnet_rx_resize(vi, rq, ring->rx_pending);
2960 if (err)
2961 return err;
2962
2963 /* The reason is same as the transmit virtqueue reset */
2964 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(i),
2965 vi->intr_coal_rx.max_usecs,
2966 vi->intr_coal_rx.max_packets);
2967 if (err)
2968 return err;
2969
2970 vi->rq[i].intr_coal.max_usecs = vi->intr_coal_rx.max_usecs;
2971 vi->rq[i].intr_coal.max_packets = vi->intr_coal_rx.max_packets;
2972 }
2973 }
2974
2975 return 0;
2976 }
2977
virtnet_commit_rss_command(struct virtnet_info * vi)2978 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
2979 {
2980 struct net_device *dev = vi->dev;
2981 struct scatterlist sgs[4];
2982 unsigned int sg_buf_size;
2983
2984 /* prepare sgs */
2985 sg_init_table(sgs, 4);
2986
2987 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
2988 sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
2989
2990 sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
2991 sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
2992
2993 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
2994 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
2995 sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
2996
2997 sg_buf_size = vi->rss_key_size;
2998 sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
2999
3000 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
3001 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
3002 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
3003 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
3004 return false;
3005 }
3006 return true;
3007 }
3008
virtnet_init_default_rss(struct virtnet_info * vi)3009 static void virtnet_init_default_rss(struct virtnet_info *vi)
3010 {
3011 u32 indir_val = 0;
3012 int i = 0;
3013
3014 vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
3015 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
3016 vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
3017 ? vi->rss_indir_table_size - 1 : 0;
3018 vi->ctrl->rss.unclassified_queue = 0;
3019
3020 for (; i < vi->rss_indir_table_size; ++i) {
3021 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
3022 vi->ctrl->rss.indirection_table[i] = indir_val;
3023 }
3024
3025 vi->ctrl->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0;
3026 vi->ctrl->rss.hash_key_length = vi->rss_key_size;
3027
3028 netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
3029 }
3030
virtnet_get_hashflow(const struct virtnet_info * vi,struct ethtool_rxnfc * info)3031 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
3032 {
3033 info->data = 0;
3034 switch (info->flow_type) {
3035 case TCP_V4_FLOW:
3036 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
3037 info->data = RXH_IP_SRC | RXH_IP_DST |
3038 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3039 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3040 info->data = RXH_IP_SRC | RXH_IP_DST;
3041 }
3042 break;
3043 case TCP_V6_FLOW:
3044 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
3045 info->data = RXH_IP_SRC | RXH_IP_DST |
3046 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3047 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3048 info->data = RXH_IP_SRC | RXH_IP_DST;
3049 }
3050 break;
3051 case UDP_V4_FLOW:
3052 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
3053 info->data = RXH_IP_SRC | RXH_IP_DST |
3054 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3055 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3056 info->data = RXH_IP_SRC | RXH_IP_DST;
3057 }
3058 break;
3059 case UDP_V6_FLOW:
3060 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
3061 info->data = RXH_IP_SRC | RXH_IP_DST |
3062 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3063 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3064 info->data = RXH_IP_SRC | RXH_IP_DST;
3065 }
3066 break;
3067 case IPV4_FLOW:
3068 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
3069 info->data = RXH_IP_SRC | RXH_IP_DST;
3070
3071 break;
3072 case IPV6_FLOW:
3073 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
3074 info->data = RXH_IP_SRC | RXH_IP_DST;
3075
3076 break;
3077 default:
3078 info->data = 0;
3079 break;
3080 }
3081 }
3082
virtnet_set_hashflow(struct virtnet_info * vi,struct ethtool_rxnfc * info)3083 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
3084 {
3085 u32 new_hashtypes = vi->rss_hash_types_saved;
3086 bool is_disable = info->data & RXH_DISCARD;
3087 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
3088
3089 /* supports only 'sd', 'sdfn' and 'r' */
3090 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
3091 return false;
3092
3093 switch (info->flow_type) {
3094 case TCP_V4_FLOW:
3095 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
3096 if (!is_disable)
3097 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3098 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
3099 break;
3100 case UDP_V4_FLOW:
3101 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
3102 if (!is_disable)
3103 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3104 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
3105 break;
3106 case IPV4_FLOW:
3107 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3108 if (!is_disable)
3109 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3110 break;
3111 case TCP_V6_FLOW:
3112 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
3113 if (!is_disable)
3114 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3115 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
3116 break;
3117 case UDP_V6_FLOW:
3118 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
3119 if (!is_disable)
3120 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3121 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
3122 break;
3123 case IPV6_FLOW:
3124 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3125 if (!is_disable)
3126 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3127 break;
3128 default:
3129 /* unsupported flow */
3130 return false;
3131 }
3132
3133 /* if unsupported hashtype was set */
3134 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
3135 return false;
3136
3137 if (new_hashtypes != vi->rss_hash_types_saved) {
3138 vi->rss_hash_types_saved = new_hashtypes;
3139 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3140 if (vi->dev->features & NETIF_F_RXHASH)
3141 return virtnet_commit_rss_command(vi);
3142 }
3143
3144 return true;
3145 }
3146
virtnet_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)3147 static void virtnet_get_drvinfo(struct net_device *dev,
3148 struct ethtool_drvinfo *info)
3149 {
3150 struct virtnet_info *vi = netdev_priv(dev);
3151 struct virtio_device *vdev = vi->vdev;
3152
3153 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
3154 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
3155 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
3156
3157 }
3158
3159 /* TODO: Eliminate OOO packets during switching */
virtnet_set_channels(struct net_device * dev,struct ethtool_channels * channels)3160 static int virtnet_set_channels(struct net_device *dev,
3161 struct ethtool_channels *channels)
3162 {
3163 struct virtnet_info *vi = netdev_priv(dev);
3164 u16 queue_pairs = channels->combined_count;
3165 int err;
3166
3167 /* We don't support separate rx/tx channels.
3168 * We don't allow setting 'other' channels.
3169 */
3170 if (channels->rx_count || channels->tx_count || channels->other_count)
3171 return -EINVAL;
3172
3173 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
3174 return -EINVAL;
3175
3176 /* For now we don't support modifying channels while XDP is loaded
3177 * also when XDP is loaded all RX queues have XDP programs so we only
3178 * need to check a single RX queue.
3179 */
3180 if (vi->rq[0].xdp_prog)
3181 return -EINVAL;
3182
3183 cpus_read_lock();
3184 err = _virtnet_set_queues(vi, queue_pairs);
3185 if (err) {
3186 cpus_read_unlock();
3187 goto err;
3188 }
3189 virtnet_set_affinity(vi);
3190 cpus_read_unlock();
3191
3192 netif_set_real_num_tx_queues(dev, queue_pairs);
3193 netif_set_real_num_rx_queues(dev, queue_pairs);
3194 err:
3195 return err;
3196 }
3197
virtnet_get_strings(struct net_device * dev,u32 stringset,u8 * data)3198 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3199 {
3200 struct virtnet_info *vi = netdev_priv(dev);
3201 unsigned int i, j;
3202 u8 *p = data;
3203
3204 switch (stringset) {
3205 case ETH_SS_STATS:
3206 for (i = 0; i < vi->curr_queue_pairs; i++) {
3207 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
3208 ethtool_sprintf(&p, "rx_queue_%u_%s", i,
3209 virtnet_rq_stats_desc[j].desc);
3210 }
3211
3212 for (i = 0; i < vi->curr_queue_pairs; i++) {
3213 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
3214 ethtool_sprintf(&p, "tx_queue_%u_%s", i,
3215 virtnet_sq_stats_desc[j].desc);
3216 }
3217 break;
3218 }
3219 }
3220
virtnet_get_sset_count(struct net_device * dev,int sset)3221 static int virtnet_get_sset_count(struct net_device *dev, int sset)
3222 {
3223 struct virtnet_info *vi = netdev_priv(dev);
3224
3225 switch (sset) {
3226 case ETH_SS_STATS:
3227 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
3228 VIRTNET_SQ_STATS_LEN);
3229 default:
3230 return -EOPNOTSUPP;
3231 }
3232 }
3233
virtnet_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)3234 static void virtnet_get_ethtool_stats(struct net_device *dev,
3235 struct ethtool_stats *stats, u64 *data)
3236 {
3237 struct virtnet_info *vi = netdev_priv(dev);
3238 unsigned int idx = 0, start, i, j;
3239 const u8 *stats_base;
3240 const u64_stats_t *p;
3241 size_t offset;
3242
3243 for (i = 0; i < vi->curr_queue_pairs; i++) {
3244 struct receive_queue *rq = &vi->rq[i];
3245
3246 stats_base = (const u8 *)&rq->stats;
3247 do {
3248 start = u64_stats_fetch_begin(&rq->stats.syncp);
3249 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
3250 offset = virtnet_rq_stats_desc[j].offset;
3251 p = (const u64_stats_t *)(stats_base + offset);
3252 data[idx + j] = u64_stats_read(p);
3253 }
3254 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
3255 idx += VIRTNET_RQ_STATS_LEN;
3256 }
3257
3258 for (i = 0; i < vi->curr_queue_pairs; i++) {
3259 struct send_queue *sq = &vi->sq[i];
3260
3261 stats_base = (const u8 *)&sq->stats;
3262 do {
3263 start = u64_stats_fetch_begin(&sq->stats.syncp);
3264 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
3265 offset = virtnet_sq_stats_desc[j].offset;
3266 p = (const u64_stats_t *)(stats_base + offset);
3267 data[idx + j] = u64_stats_read(p);
3268 }
3269 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
3270 idx += VIRTNET_SQ_STATS_LEN;
3271 }
3272 }
3273
virtnet_get_channels(struct net_device * dev,struct ethtool_channels * channels)3274 static void virtnet_get_channels(struct net_device *dev,
3275 struct ethtool_channels *channels)
3276 {
3277 struct virtnet_info *vi = netdev_priv(dev);
3278
3279 channels->combined_count = vi->curr_queue_pairs;
3280 channels->max_combined = vi->max_queue_pairs;
3281 channels->max_other = 0;
3282 channels->rx_count = 0;
3283 channels->tx_count = 0;
3284 channels->other_count = 0;
3285 }
3286
virtnet_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)3287 static int virtnet_set_link_ksettings(struct net_device *dev,
3288 const struct ethtool_link_ksettings *cmd)
3289 {
3290 struct virtnet_info *vi = netdev_priv(dev);
3291
3292 return ethtool_virtdev_set_link_ksettings(dev, cmd,
3293 &vi->speed, &vi->duplex);
3294 }
3295
virtnet_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)3296 static int virtnet_get_link_ksettings(struct net_device *dev,
3297 struct ethtool_link_ksettings *cmd)
3298 {
3299 struct virtnet_info *vi = netdev_priv(dev);
3300
3301 cmd->base.speed = vi->speed;
3302 cmd->base.duplex = vi->duplex;
3303 cmd->base.port = PORT_OTHER;
3304
3305 return 0;
3306 }
3307
virtnet_send_notf_coal_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec)3308 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
3309 struct ethtool_coalesce *ec)
3310 {
3311 struct scatterlist sgs_tx, sgs_rx;
3312 int i;
3313
3314 vi->ctrl->coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
3315 vi->ctrl->coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
3316 sg_init_one(&sgs_tx, &vi->ctrl->coal_tx, sizeof(vi->ctrl->coal_tx));
3317
3318 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3319 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
3320 &sgs_tx))
3321 return -EINVAL;
3322
3323 /* Save parameters */
3324 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs;
3325 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames;
3326 for (i = 0; i < vi->max_queue_pairs; i++) {
3327 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs;
3328 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames;
3329 }
3330
3331 vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
3332 vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
3333 sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx));
3334
3335 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3336 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
3337 &sgs_rx))
3338 return -EINVAL;
3339
3340 /* Save parameters */
3341 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs;
3342 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames;
3343 for (i = 0; i < vi->max_queue_pairs; i++) {
3344 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs;
3345 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames;
3346 }
3347
3348 return 0;
3349 }
3350
virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info * vi,u16 vqn,u32 max_usecs,u32 max_packets)3351 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
3352 u16 vqn, u32 max_usecs, u32 max_packets)
3353 {
3354 struct scatterlist sgs;
3355
3356 vi->ctrl->coal_vq.vqn = cpu_to_le16(vqn);
3357 vi->ctrl->coal_vq.coal.max_usecs = cpu_to_le32(max_usecs);
3358 vi->ctrl->coal_vq.coal.max_packets = cpu_to_le32(max_packets);
3359 sg_init_one(&sgs, &vi->ctrl->coal_vq, sizeof(vi->ctrl->coal_vq));
3360
3361 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3362 VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
3363 &sgs))
3364 return -EINVAL;
3365
3366 return 0;
3367 }
3368
virtnet_send_notf_coal_vq_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec,u16 queue)3369 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
3370 struct ethtool_coalesce *ec,
3371 u16 queue)
3372 {
3373 int err;
3374
3375 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
3376 ec->rx_coalesce_usecs,
3377 ec->rx_max_coalesced_frames);
3378 if (err)
3379 return err;
3380
3381 vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
3382 vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames;
3383
3384 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
3385 ec->tx_coalesce_usecs,
3386 ec->tx_max_coalesced_frames);
3387 if (err)
3388 return err;
3389
3390 vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
3391 vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames;
3392
3393 return 0;
3394 }
3395
virtnet_coal_params_supported(struct ethtool_coalesce * ec)3396 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
3397 {
3398 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
3399 * feature is negotiated.
3400 */
3401 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
3402 return -EOPNOTSUPP;
3403
3404 if (ec->tx_max_coalesced_frames > 1 ||
3405 ec->rx_max_coalesced_frames != 1)
3406 return -EINVAL;
3407
3408 return 0;
3409 }
3410
virtnet_should_update_vq_weight(int dev_flags,int weight,int vq_weight,bool * should_update)3411 static int virtnet_should_update_vq_weight(int dev_flags, int weight,
3412 int vq_weight, bool *should_update)
3413 {
3414 if (weight ^ vq_weight) {
3415 if (dev_flags & IFF_UP)
3416 return -EBUSY;
3417 *should_update = true;
3418 }
3419
3420 return 0;
3421 }
3422
virtnet_set_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)3423 static int virtnet_set_coalesce(struct net_device *dev,
3424 struct ethtool_coalesce *ec,
3425 struct kernel_ethtool_coalesce *kernel_coal,
3426 struct netlink_ext_ack *extack)
3427 {
3428 struct virtnet_info *vi = netdev_priv(dev);
3429 int ret, queue_number, napi_weight;
3430 bool update_napi = false;
3431
3432 /* Can't change NAPI weight if the link is up */
3433 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
3434 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) {
3435 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
3436 vi->sq[queue_number].napi.weight,
3437 &update_napi);
3438 if (ret)
3439 return ret;
3440
3441 if (update_napi) {
3442 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be
3443 * updated for the sake of simplicity, which might not be necessary
3444 */
3445 break;
3446 }
3447 }
3448
3449 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
3450 ret = virtnet_send_notf_coal_cmds(vi, ec);
3451 else
3452 ret = virtnet_coal_params_supported(ec);
3453
3454 if (ret)
3455 return ret;
3456
3457 if (update_napi) {
3458 for (; queue_number < vi->max_queue_pairs; queue_number++)
3459 vi->sq[queue_number].napi.weight = napi_weight;
3460 }
3461
3462 return ret;
3463 }
3464
virtnet_get_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)3465 static int virtnet_get_coalesce(struct net_device *dev,
3466 struct ethtool_coalesce *ec,
3467 struct kernel_ethtool_coalesce *kernel_coal,
3468 struct netlink_ext_ack *extack)
3469 {
3470 struct virtnet_info *vi = netdev_priv(dev);
3471
3472 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
3473 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
3474 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
3475 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
3476 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
3477 } else {
3478 ec->rx_max_coalesced_frames = 1;
3479
3480 if (vi->sq[0].napi.weight)
3481 ec->tx_max_coalesced_frames = 1;
3482 }
3483
3484 return 0;
3485 }
3486
virtnet_set_per_queue_coalesce(struct net_device * dev,u32 queue,struct ethtool_coalesce * ec)3487 static int virtnet_set_per_queue_coalesce(struct net_device *dev,
3488 u32 queue,
3489 struct ethtool_coalesce *ec)
3490 {
3491 struct virtnet_info *vi = netdev_priv(dev);
3492 int ret, napi_weight;
3493 bool update_napi = false;
3494
3495 if (queue >= vi->max_queue_pairs)
3496 return -EINVAL;
3497
3498 /* Can't change NAPI weight if the link is up */
3499 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
3500 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
3501 vi->sq[queue].napi.weight,
3502 &update_napi);
3503 if (ret)
3504 return ret;
3505
3506 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
3507 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
3508 else
3509 ret = virtnet_coal_params_supported(ec);
3510
3511 if (ret)
3512 return ret;
3513
3514 if (update_napi)
3515 vi->sq[queue].napi.weight = napi_weight;
3516
3517 return 0;
3518 }
3519
virtnet_get_per_queue_coalesce(struct net_device * dev,u32 queue,struct ethtool_coalesce * ec)3520 static int virtnet_get_per_queue_coalesce(struct net_device *dev,
3521 u32 queue,
3522 struct ethtool_coalesce *ec)
3523 {
3524 struct virtnet_info *vi = netdev_priv(dev);
3525
3526 if (queue >= vi->max_queue_pairs)
3527 return -EINVAL;
3528
3529 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
3530 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
3531 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
3532 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
3533 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
3534 } else {
3535 ec->rx_max_coalesced_frames = 1;
3536
3537 if (vi->sq[queue].napi.weight)
3538 ec->tx_max_coalesced_frames = 1;
3539 }
3540
3541 return 0;
3542 }
3543
virtnet_init_settings(struct net_device * dev)3544 static void virtnet_init_settings(struct net_device *dev)
3545 {
3546 struct virtnet_info *vi = netdev_priv(dev);
3547
3548 vi->speed = SPEED_UNKNOWN;
3549 vi->duplex = DUPLEX_UNKNOWN;
3550 }
3551
virtnet_update_settings(struct virtnet_info * vi)3552 static void virtnet_update_settings(struct virtnet_info *vi)
3553 {
3554 u32 speed;
3555 u8 duplex;
3556
3557 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
3558 return;
3559
3560 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
3561
3562 if (ethtool_validate_speed(speed))
3563 vi->speed = speed;
3564
3565 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
3566
3567 if (ethtool_validate_duplex(duplex))
3568 vi->duplex = duplex;
3569 }
3570
virtnet_get_rxfh_key_size(struct net_device * dev)3571 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
3572 {
3573 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
3574 }
3575
virtnet_get_rxfh_indir_size(struct net_device * dev)3576 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
3577 {
3578 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
3579 }
3580
virtnet_get_rxfh(struct net_device * dev,u32 * indir,u8 * key,u8 * hfunc)3581 static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
3582 {
3583 struct virtnet_info *vi = netdev_priv(dev);
3584 int i;
3585
3586 if (indir) {
3587 for (i = 0; i < vi->rss_indir_table_size; ++i)
3588 indir[i] = vi->ctrl->rss.indirection_table[i];
3589 }
3590
3591 if (key)
3592 memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
3593
3594 if (hfunc)
3595 *hfunc = ETH_RSS_HASH_TOP;
3596
3597 return 0;
3598 }
3599
virtnet_set_rxfh(struct net_device * dev,const u32 * indir,const u8 * key,const u8 hfunc)3600 static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
3601 {
3602 struct virtnet_info *vi = netdev_priv(dev);
3603 bool update = false;
3604 int i;
3605
3606 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3607 return -EOPNOTSUPP;
3608
3609 if (indir) {
3610 if (!vi->has_rss)
3611 return -EOPNOTSUPP;
3612
3613 for (i = 0; i < vi->rss_indir_table_size; ++i)
3614 vi->ctrl->rss.indirection_table[i] = indir[i];
3615 update = true;
3616 }
3617 if (key) {
3618 /* If either _F_HASH_REPORT or _F_RSS are negotiated, the
3619 * device provides hash calculation capabilities, that is,
3620 * hash_key is configured.
3621 */
3622 if (!vi->has_rss && !vi->has_rss_hash_report)
3623 return -EOPNOTSUPP;
3624
3625 memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
3626 update = true;
3627 }
3628
3629 if (update)
3630 virtnet_commit_rss_command(vi);
3631
3632 return 0;
3633 }
3634
virtnet_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info,u32 * rule_locs)3635 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
3636 {
3637 struct virtnet_info *vi = netdev_priv(dev);
3638 int rc = 0;
3639
3640 switch (info->cmd) {
3641 case ETHTOOL_GRXRINGS:
3642 info->data = vi->curr_queue_pairs;
3643 break;
3644 case ETHTOOL_GRXFH:
3645 virtnet_get_hashflow(vi, info);
3646 break;
3647 default:
3648 rc = -EOPNOTSUPP;
3649 }
3650
3651 return rc;
3652 }
3653
virtnet_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info)3654 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
3655 {
3656 struct virtnet_info *vi = netdev_priv(dev);
3657 int rc = 0;
3658
3659 switch (info->cmd) {
3660 case ETHTOOL_SRXFH:
3661 if (!virtnet_set_hashflow(vi, info))
3662 rc = -EINVAL;
3663
3664 break;
3665 default:
3666 rc = -EOPNOTSUPP;
3667 }
3668
3669 return rc;
3670 }
3671
3672 static const struct ethtool_ops virtnet_ethtool_ops = {
3673 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
3674 ETHTOOL_COALESCE_USECS,
3675 .get_drvinfo = virtnet_get_drvinfo,
3676 .get_link = ethtool_op_get_link,
3677 .get_ringparam = virtnet_get_ringparam,
3678 .set_ringparam = virtnet_set_ringparam,
3679 .get_strings = virtnet_get_strings,
3680 .get_sset_count = virtnet_get_sset_count,
3681 .get_ethtool_stats = virtnet_get_ethtool_stats,
3682 .set_channels = virtnet_set_channels,
3683 .get_channels = virtnet_get_channels,
3684 .get_ts_info = ethtool_op_get_ts_info,
3685 .get_link_ksettings = virtnet_get_link_ksettings,
3686 .set_link_ksettings = virtnet_set_link_ksettings,
3687 .set_coalesce = virtnet_set_coalesce,
3688 .get_coalesce = virtnet_get_coalesce,
3689 .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
3690 .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
3691 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
3692 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
3693 .get_rxfh = virtnet_get_rxfh,
3694 .set_rxfh = virtnet_set_rxfh,
3695 .get_rxnfc = virtnet_get_rxnfc,
3696 .set_rxnfc = virtnet_set_rxnfc,
3697 };
3698
virtnet_freeze_down(struct virtio_device * vdev)3699 static void virtnet_freeze_down(struct virtio_device *vdev)
3700 {
3701 struct virtnet_info *vi = vdev->priv;
3702
3703 /* Make sure no work handler is accessing the device */
3704 flush_work(&vi->config_work);
3705
3706 netif_tx_lock_bh(vi->dev);
3707 netif_device_detach(vi->dev);
3708 netif_tx_unlock_bh(vi->dev);
3709 if (netif_running(vi->dev))
3710 virtnet_close(vi->dev);
3711 }
3712
3713 static int init_vqs(struct virtnet_info *vi);
3714
virtnet_restore_up(struct virtio_device * vdev)3715 static int virtnet_restore_up(struct virtio_device *vdev)
3716 {
3717 struct virtnet_info *vi = vdev->priv;
3718 int err;
3719
3720 err = init_vqs(vi);
3721 if (err)
3722 return err;
3723
3724 virtio_device_ready(vdev);
3725
3726 enable_delayed_refill(vi);
3727
3728 if (netif_running(vi->dev)) {
3729 err = virtnet_open(vi->dev);
3730 if (err)
3731 return err;
3732 }
3733
3734 netif_tx_lock_bh(vi->dev);
3735 netif_device_attach(vi->dev);
3736 netif_tx_unlock_bh(vi->dev);
3737 return err;
3738 }
3739
virtnet_set_guest_offloads(struct virtnet_info * vi,u64 offloads)3740 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
3741 {
3742 struct scatterlist sg;
3743 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
3744
3745 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
3746
3747 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
3748 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
3749 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
3750 return -EINVAL;
3751 }
3752
3753 return 0;
3754 }
3755
virtnet_clear_guest_offloads(struct virtnet_info * vi)3756 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
3757 {
3758 u64 offloads = 0;
3759
3760 if (!vi->guest_offloads)
3761 return 0;
3762
3763 return virtnet_set_guest_offloads(vi, offloads);
3764 }
3765
virtnet_restore_guest_offloads(struct virtnet_info * vi)3766 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
3767 {
3768 u64 offloads = vi->guest_offloads;
3769
3770 if (!vi->guest_offloads)
3771 return 0;
3772
3773 return virtnet_set_guest_offloads(vi, offloads);
3774 }
3775
virtnet_xdp_set(struct net_device * dev,struct bpf_prog * prog,struct netlink_ext_ack * extack)3776 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
3777 struct netlink_ext_ack *extack)
3778 {
3779 unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
3780 sizeof(struct skb_shared_info));
3781 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
3782 struct virtnet_info *vi = netdev_priv(dev);
3783 struct bpf_prog *old_prog;
3784 u16 xdp_qp = 0, curr_qp;
3785 int i, err;
3786
3787 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
3788 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3789 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3790 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
3791 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
3792 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
3793 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
3794 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
3795 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
3796 return -EOPNOTSUPP;
3797 }
3798
3799 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
3800 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
3801 return -EINVAL;
3802 }
3803
3804 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
3805 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
3806 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
3807 return -EINVAL;
3808 }
3809
3810 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
3811 if (prog)
3812 xdp_qp = nr_cpu_ids;
3813
3814 /* XDP requires extra queues for XDP_TX */
3815 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
3816 netdev_warn_once(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n",
3817 curr_qp + xdp_qp, vi->max_queue_pairs);
3818 xdp_qp = 0;
3819 }
3820
3821 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
3822 if (!prog && !old_prog)
3823 return 0;
3824
3825 if (prog)
3826 bpf_prog_add(prog, vi->max_queue_pairs - 1);
3827
3828 /* Make sure NAPI is not using any XDP TX queues for RX. */
3829 if (netif_running(dev)) {
3830 for (i = 0; i < vi->max_queue_pairs; i++) {
3831 napi_disable(&vi->rq[i].napi);
3832 virtnet_napi_tx_disable(&vi->sq[i].napi);
3833 }
3834 }
3835
3836 if (!prog) {
3837 for (i = 0; i < vi->max_queue_pairs; i++) {
3838 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
3839 if (i == 0)
3840 virtnet_restore_guest_offloads(vi);
3841 }
3842 synchronize_net();
3843 }
3844
3845 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
3846 if (err)
3847 goto err;
3848 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
3849 vi->xdp_queue_pairs = xdp_qp;
3850
3851 if (prog) {
3852 vi->xdp_enabled = true;
3853 for (i = 0; i < vi->max_queue_pairs; i++) {
3854 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
3855 if (i == 0 && !old_prog)
3856 virtnet_clear_guest_offloads(vi);
3857 }
3858 if (!old_prog)
3859 xdp_features_set_redirect_target(dev, true);
3860 } else {
3861 xdp_features_clear_redirect_target(dev);
3862 vi->xdp_enabled = false;
3863 }
3864
3865 for (i = 0; i < vi->max_queue_pairs; i++) {
3866 if (old_prog)
3867 bpf_prog_put(old_prog);
3868 if (netif_running(dev)) {
3869 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3870 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3871 &vi->sq[i].napi);
3872 }
3873 }
3874
3875 return 0;
3876
3877 err:
3878 if (!prog) {
3879 virtnet_clear_guest_offloads(vi);
3880 for (i = 0; i < vi->max_queue_pairs; i++)
3881 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
3882 }
3883
3884 if (netif_running(dev)) {
3885 for (i = 0; i < vi->max_queue_pairs; i++) {
3886 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3887 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3888 &vi->sq[i].napi);
3889 }
3890 }
3891 if (prog)
3892 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
3893 return err;
3894 }
3895
virtnet_xdp(struct net_device * dev,struct netdev_bpf * xdp)3896 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3897 {
3898 switch (xdp->command) {
3899 case XDP_SETUP_PROG:
3900 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
3901 default:
3902 return -EINVAL;
3903 }
3904 }
3905
virtnet_get_phys_port_name(struct net_device * dev,char * buf,size_t len)3906 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
3907 size_t len)
3908 {
3909 struct virtnet_info *vi = netdev_priv(dev);
3910 int ret;
3911
3912 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3913 return -EOPNOTSUPP;
3914
3915 ret = snprintf(buf, len, "sby");
3916 if (ret >= len)
3917 return -EOPNOTSUPP;
3918
3919 return 0;
3920 }
3921
virtnet_set_features(struct net_device * dev,netdev_features_t features)3922 static int virtnet_set_features(struct net_device *dev,
3923 netdev_features_t features)
3924 {
3925 struct virtnet_info *vi = netdev_priv(dev);
3926 u64 offloads;
3927 int err;
3928
3929 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
3930 if (vi->xdp_enabled)
3931 return -EBUSY;
3932
3933 if (features & NETIF_F_GRO_HW)
3934 offloads = vi->guest_offloads_capable;
3935 else
3936 offloads = vi->guest_offloads_capable &
3937 ~GUEST_OFFLOAD_GRO_HW_MASK;
3938
3939 err = virtnet_set_guest_offloads(vi, offloads);
3940 if (err)
3941 return err;
3942 vi->guest_offloads = offloads;
3943 }
3944
3945 if ((dev->features ^ features) & NETIF_F_RXHASH) {
3946 if (features & NETIF_F_RXHASH)
3947 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3948 else
3949 vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
3950
3951 if (!virtnet_commit_rss_command(vi))
3952 return -EINVAL;
3953 }
3954
3955 return 0;
3956 }
3957
virtnet_tx_timeout(struct net_device * dev,unsigned int txqueue)3958 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
3959 {
3960 struct virtnet_info *priv = netdev_priv(dev);
3961 struct send_queue *sq = &priv->sq[txqueue];
3962 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
3963
3964 u64_stats_update_begin(&sq->stats.syncp);
3965 u64_stats_inc(&sq->stats.tx_timeouts);
3966 u64_stats_update_end(&sq->stats.syncp);
3967
3968 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
3969 txqueue, sq->name, sq->vq->index, sq->vq->name,
3970 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
3971 }
3972
3973 static const struct net_device_ops virtnet_netdev = {
3974 .ndo_open = virtnet_open,
3975 .ndo_stop = virtnet_close,
3976 .ndo_start_xmit = start_xmit,
3977 .ndo_validate_addr = eth_validate_addr,
3978 .ndo_set_mac_address = virtnet_set_mac_address,
3979 .ndo_set_rx_mode = virtnet_set_rx_mode,
3980 .ndo_get_stats64 = virtnet_stats,
3981 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
3982 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
3983 .ndo_bpf = virtnet_xdp,
3984 .ndo_xdp_xmit = virtnet_xdp_xmit,
3985 .ndo_features_check = passthru_features_check,
3986 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
3987 .ndo_set_features = virtnet_set_features,
3988 .ndo_tx_timeout = virtnet_tx_timeout,
3989 };
3990
virtnet_config_changed_work(struct work_struct * work)3991 static void virtnet_config_changed_work(struct work_struct *work)
3992 {
3993 struct virtnet_info *vi =
3994 container_of(work, struct virtnet_info, config_work);
3995 u16 v;
3996
3997 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
3998 struct virtio_net_config, status, &v) < 0)
3999 return;
4000
4001 if (v & VIRTIO_NET_S_ANNOUNCE) {
4002 netdev_notify_peers(vi->dev);
4003 virtnet_ack_link_announce(vi);
4004 }
4005
4006 /* Ignore unknown (future) status bits */
4007 v &= VIRTIO_NET_S_LINK_UP;
4008
4009 if (vi->status == v)
4010 return;
4011
4012 vi->status = v;
4013
4014 if (vi->status & VIRTIO_NET_S_LINK_UP) {
4015 virtnet_update_settings(vi);
4016 netif_carrier_on(vi->dev);
4017 netif_tx_wake_all_queues(vi->dev);
4018 } else {
4019 netif_carrier_off(vi->dev);
4020 netif_tx_stop_all_queues(vi->dev);
4021 }
4022 }
4023
virtnet_config_changed(struct virtio_device * vdev)4024 static void virtnet_config_changed(struct virtio_device *vdev)
4025 {
4026 struct virtnet_info *vi = vdev->priv;
4027
4028 schedule_work(&vi->config_work);
4029 }
4030
virtnet_free_queues(struct virtnet_info * vi)4031 static void virtnet_free_queues(struct virtnet_info *vi)
4032 {
4033 int i;
4034
4035 for (i = 0; i < vi->max_queue_pairs; i++) {
4036 __netif_napi_del(&vi->rq[i].napi);
4037 __netif_napi_del(&vi->sq[i].napi);
4038 }
4039
4040 /* We called __netif_napi_del(),
4041 * we need to respect an RCU grace period before freeing vi->rq
4042 */
4043 synchronize_net();
4044
4045 kfree(vi->rq);
4046 kfree(vi->sq);
4047 kfree(vi->ctrl);
4048 }
4049
_free_receive_bufs(struct virtnet_info * vi)4050 static void _free_receive_bufs(struct virtnet_info *vi)
4051 {
4052 struct bpf_prog *old_prog;
4053 int i;
4054
4055 for (i = 0; i < vi->max_queue_pairs; i++) {
4056 while (vi->rq[i].pages)
4057 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
4058
4059 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
4060 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
4061 if (old_prog)
4062 bpf_prog_put(old_prog);
4063 }
4064 }
4065
free_receive_bufs(struct virtnet_info * vi)4066 static void free_receive_bufs(struct virtnet_info *vi)
4067 {
4068 rtnl_lock();
4069 _free_receive_bufs(vi);
4070 rtnl_unlock();
4071 }
4072
free_receive_page_frags(struct virtnet_info * vi)4073 static void free_receive_page_frags(struct virtnet_info *vi)
4074 {
4075 int i;
4076 for (i = 0; i < vi->max_queue_pairs; i++)
4077 if (vi->rq[i].alloc_frag.page) {
4078 if (vi->rq[i].do_dma && vi->rq[i].last_dma)
4079 virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0);
4080 put_page(vi->rq[i].alloc_frag.page);
4081 }
4082 }
4083
virtnet_sq_free_unused_buf(struct virtqueue * vq,void * buf)4084 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
4085 {
4086 if (!is_xdp_frame(buf))
4087 dev_kfree_skb(buf);
4088 else
4089 xdp_return_frame(ptr_to_xdp(buf));
4090 }
4091
free_unused_bufs(struct virtnet_info * vi)4092 static void free_unused_bufs(struct virtnet_info *vi)
4093 {
4094 void *buf;
4095 int i;
4096
4097 for (i = 0; i < vi->max_queue_pairs; i++) {
4098 struct virtqueue *vq = vi->sq[i].vq;
4099 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
4100 virtnet_sq_free_unused_buf(vq, buf);
4101 cond_resched();
4102 }
4103
4104 for (i = 0; i < vi->max_queue_pairs; i++) {
4105 struct virtqueue *vq = vi->rq[i].vq;
4106
4107 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
4108 virtnet_rq_unmap_free_buf(vq, buf);
4109 cond_resched();
4110 }
4111 }
4112
virtnet_del_vqs(struct virtnet_info * vi)4113 static void virtnet_del_vqs(struct virtnet_info *vi)
4114 {
4115 struct virtio_device *vdev = vi->vdev;
4116
4117 virtnet_clean_affinity(vi);
4118
4119 vdev->config->del_vqs(vdev);
4120
4121 virtnet_free_queues(vi);
4122 }
4123
4124 /* How large should a single buffer be so a queue full of these can fit at
4125 * least one full packet?
4126 * Logic below assumes the mergeable buffer header is used.
4127 */
mergeable_min_buf_len(struct virtnet_info * vi,struct virtqueue * vq)4128 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
4129 {
4130 const unsigned int hdr_len = vi->hdr_len;
4131 unsigned int rq_size = virtqueue_get_vring_size(vq);
4132 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
4133 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
4134 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
4135
4136 return max(max(min_buf_len, hdr_len) - hdr_len,
4137 (unsigned int)GOOD_PACKET_LEN);
4138 }
4139
virtnet_find_vqs(struct virtnet_info * vi)4140 static int virtnet_find_vqs(struct virtnet_info *vi)
4141 {
4142 vq_callback_t **callbacks;
4143 struct virtqueue **vqs;
4144 const char **names;
4145 int ret = -ENOMEM;
4146 int total_vqs;
4147 bool *ctx;
4148 u16 i;
4149
4150 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
4151 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
4152 * possible control vq.
4153 */
4154 total_vqs = vi->max_queue_pairs * 2 +
4155 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
4156
4157 /* Allocate space for find_vqs parameters */
4158 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
4159 if (!vqs)
4160 goto err_vq;
4161 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
4162 if (!callbacks)
4163 goto err_callback;
4164 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
4165 if (!names)
4166 goto err_names;
4167 if (!vi->big_packets || vi->mergeable_rx_bufs) {
4168 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
4169 if (!ctx)
4170 goto err_ctx;
4171 } else {
4172 ctx = NULL;
4173 }
4174
4175 /* Parameters for control virtqueue, if any */
4176 if (vi->has_cvq) {
4177 callbacks[total_vqs - 1] = NULL;
4178 names[total_vqs - 1] = "control";
4179 }
4180
4181 /* Allocate/initialize parameters for send/receive virtqueues */
4182 for (i = 0; i < vi->max_queue_pairs; i++) {
4183 callbacks[rxq2vq(i)] = skb_recv_done;
4184 callbacks[txq2vq(i)] = skb_xmit_done;
4185 sprintf(vi->rq[i].name, "input.%u", i);
4186 sprintf(vi->sq[i].name, "output.%u", i);
4187 names[rxq2vq(i)] = vi->rq[i].name;
4188 names[txq2vq(i)] = vi->sq[i].name;
4189 if (ctx)
4190 ctx[rxq2vq(i)] = true;
4191 }
4192
4193 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
4194 names, ctx, NULL);
4195 if (ret)
4196 goto err_find;
4197
4198 if (vi->has_cvq) {
4199 vi->cvq = vqs[total_vqs - 1];
4200 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
4201 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4202 }
4203
4204 for (i = 0; i < vi->max_queue_pairs; i++) {
4205 vi->rq[i].vq = vqs[rxq2vq(i)];
4206 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
4207 vi->sq[i].vq = vqs[txq2vq(i)];
4208 }
4209
4210 /* run here: ret == 0. */
4211
4212
4213 err_find:
4214 kfree(ctx);
4215 err_ctx:
4216 kfree(names);
4217 err_names:
4218 kfree(callbacks);
4219 err_callback:
4220 kfree(vqs);
4221 err_vq:
4222 return ret;
4223 }
4224
virtnet_alloc_queues(struct virtnet_info * vi)4225 static int virtnet_alloc_queues(struct virtnet_info *vi)
4226 {
4227 int i;
4228
4229 if (vi->has_cvq) {
4230 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
4231 if (!vi->ctrl)
4232 goto err_ctrl;
4233 } else {
4234 vi->ctrl = NULL;
4235 }
4236 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
4237 if (!vi->sq)
4238 goto err_sq;
4239 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
4240 if (!vi->rq)
4241 goto err_rq;
4242
4243 INIT_DELAYED_WORK(&vi->refill, refill_work);
4244 for (i = 0; i < vi->max_queue_pairs; i++) {
4245 vi->rq[i].pages = NULL;
4246 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
4247 napi_weight);
4248 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
4249 virtnet_poll_tx,
4250 napi_tx ? napi_weight : 0);
4251
4252 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
4253 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
4254 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
4255
4256 u64_stats_init(&vi->rq[i].stats.syncp);
4257 u64_stats_init(&vi->sq[i].stats.syncp);
4258 }
4259
4260 return 0;
4261
4262 err_rq:
4263 kfree(vi->sq);
4264 err_sq:
4265 kfree(vi->ctrl);
4266 err_ctrl:
4267 return -ENOMEM;
4268 }
4269
init_vqs(struct virtnet_info * vi)4270 static int init_vqs(struct virtnet_info *vi)
4271 {
4272 int ret;
4273
4274 /* Allocate send & receive queues */
4275 ret = virtnet_alloc_queues(vi);
4276 if (ret)
4277 goto err;
4278
4279 ret = virtnet_find_vqs(vi);
4280 if (ret)
4281 goto err_free;
4282
4283 virtnet_rq_set_premapped(vi);
4284
4285 cpus_read_lock();
4286 virtnet_set_affinity(vi);
4287 cpus_read_unlock();
4288
4289 return 0;
4290
4291 err_free:
4292 virtnet_free_queues(vi);
4293 err:
4294 return ret;
4295 }
4296
4297 #ifdef CONFIG_SYSFS
mergeable_rx_buffer_size_show(struct netdev_rx_queue * queue,char * buf)4298 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
4299 char *buf)
4300 {
4301 struct virtnet_info *vi = netdev_priv(queue->dev);
4302 unsigned int queue_index = get_netdev_rx_queue_index(queue);
4303 unsigned int headroom = virtnet_get_headroom(vi);
4304 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
4305 struct ewma_pkt_len *avg;
4306
4307 BUG_ON(queue_index >= vi->max_queue_pairs);
4308 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
4309 return sprintf(buf, "%u\n",
4310 get_mergeable_buf_len(&vi->rq[queue_index], avg,
4311 SKB_DATA_ALIGN(headroom + tailroom)));
4312 }
4313
4314 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
4315 __ATTR_RO(mergeable_rx_buffer_size);
4316
4317 static struct attribute *virtio_net_mrg_rx_attrs[] = {
4318 &mergeable_rx_buffer_size_attribute.attr,
4319 NULL
4320 };
4321
4322 static const struct attribute_group virtio_net_mrg_rx_group = {
4323 .name = "virtio_net",
4324 .attrs = virtio_net_mrg_rx_attrs
4325 };
4326 #endif
4327
virtnet_fail_on_feature(struct virtio_device * vdev,unsigned int fbit,const char * fname,const char * dname)4328 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
4329 unsigned int fbit,
4330 const char *fname, const char *dname)
4331 {
4332 if (!virtio_has_feature(vdev, fbit))
4333 return false;
4334
4335 dev_err(&vdev->dev, "device advertises feature %s but not %s",
4336 fname, dname);
4337
4338 return true;
4339 }
4340
4341 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
4342 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
4343
virtnet_validate_features(struct virtio_device * vdev)4344 static bool virtnet_validate_features(struct virtio_device *vdev)
4345 {
4346 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
4347 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
4348 "VIRTIO_NET_F_CTRL_VQ") ||
4349 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
4350 "VIRTIO_NET_F_CTRL_VQ") ||
4351 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
4352 "VIRTIO_NET_F_CTRL_VQ") ||
4353 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
4354 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
4355 "VIRTIO_NET_F_CTRL_VQ") ||
4356 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
4357 "VIRTIO_NET_F_CTRL_VQ") ||
4358 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
4359 "VIRTIO_NET_F_CTRL_VQ") ||
4360 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
4361 "VIRTIO_NET_F_CTRL_VQ") ||
4362 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL,
4363 "VIRTIO_NET_F_CTRL_VQ"))) {
4364 return false;
4365 }
4366
4367 return true;
4368 }
4369
4370 #define MIN_MTU ETH_MIN_MTU
4371 #define MAX_MTU ETH_MAX_MTU
4372
virtnet_validate(struct virtio_device * vdev)4373 static int virtnet_validate(struct virtio_device *vdev)
4374 {
4375 if (!vdev->config->get) {
4376 dev_err(&vdev->dev, "%s failure: config access disabled\n",
4377 __func__);
4378 return -EINVAL;
4379 }
4380
4381 if (!virtnet_validate_features(vdev))
4382 return -EINVAL;
4383
4384 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
4385 int mtu = virtio_cread16(vdev,
4386 offsetof(struct virtio_net_config,
4387 mtu));
4388 if (mtu < MIN_MTU)
4389 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
4390 }
4391
4392 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
4393 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
4394 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
4395 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
4396 }
4397
4398 return 0;
4399 }
4400
virtnet_check_guest_gso(const struct virtnet_info * vi)4401 static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
4402 {
4403 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
4404 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
4405 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
4406 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
4407 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
4408 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
4409 }
4410
virtnet_set_big_packets(struct virtnet_info * vi,const int mtu)4411 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
4412 {
4413 bool guest_gso = virtnet_check_guest_gso(vi);
4414
4415 /* If device can receive ANY guest GSO packets, regardless of mtu,
4416 * allocate packets of maximum size, otherwise limit it to only
4417 * mtu size worth only.
4418 */
4419 if (mtu > ETH_DATA_LEN || guest_gso) {
4420 vi->big_packets = true;
4421 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
4422 }
4423 }
4424
virtnet_probe(struct virtio_device * vdev)4425 static int virtnet_probe(struct virtio_device *vdev)
4426 {
4427 int i, err = -ENOMEM;
4428 struct net_device *dev;
4429 struct virtnet_info *vi;
4430 u16 max_queue_pairs;
4431 int mtu = 0;
4432
4433 /* Find if host supports multiqueue/rss virtio_net device */
4434 max_queue_pairs = 1;
4435 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
4436 max_queue_pairs =
4437 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
4438
4439 /* We need at least 2 queue's */
4440 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
4441 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
4442 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
4443 max_queue_pairs = 1;
4444
4445 /* Allocate ourselves a network device with room for our info */
4446 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
4447 if (!dev)
4448 return -ENOMEM;
4449
4450 /* Set up network device as normal. */
4451 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
4452 IFF_TX_SKB_NO_LINEAR;
4453 dev->netdev_ops = &virtnet_netdev;
4454 dev->features = NETIF_F_HIGHDMA;
4455
4456 dev->ethtool_ops = &virtnet_ethtool_ops;
4457 SET_NETDEV_DEV(dev, &vdev->dev);
4458
4459 /* Do we support "hardware" checksums? */
4460 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
4461 /* This opens up the world of extra features. */
4462 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
4463 if (csum)
4464 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
4465
4466 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
4467 dev->hw_features |= NETIF_F_TSO
4468 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
4469 }
4470 /* Individual feature bits: what can host handle? */
4471 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
4472 dev->hw_features |= NETIF_F_TSO;
4473 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
4474 dev->hw_features |= NETIF_F_TSO6;
4475 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
4476 dev->hw_features |= NETIF_F_TSO_ECN;
4477 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
4478 dev->hw_features |= NETIF_F_GSO_UDP_L4;
4479
4480 dev->features |= NETIF_F_GSO_ROBUST;
4481
4482 if (gso)
4483 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
4484 /* (!csum && gso) case will be fixed by register_netdev() */
4485 }
4486
4487 /* 1. With VIRTIO_NET_F_GUEST_CSUM negotiation, the driver doesn't
4488 * need to calculate checksums for partially checksummed packets,
4489 * as they're considered valid by the upper layer.
4490 * 2. Without VIRTIO_NET_F_GUEST_CSUM negotiation, the driver only
4491 * receives fully checksummed packets. The device may assist in
4492 * validating these packets' checksums, so the driver won't have to.
4493 */
4494 dev->features |= NETIF_F_RXCSUM;
4495
4496 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
4497 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
4498 dev->features |= NETIF_F_GRO_HW;
4499 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
4500 dev->hw_features |= NETIF_F_GRO_HW;
4501
4502 dev->vlan_features = dev->features;
4503 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
4504
4505 /* MTU range: 68 - 65535 */
4506 dev->min_mtu = MIN_MTU;
4507 dev->max_mtu = MAX_MTU;
4508
4509 /* Configuration may specify what MAC to use. Otherwise random. */
4510 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
4511 u8 addr[ETH_ALEN];
4512
4513 virtio_cread_bytes(vdev,
4514 offsetof(struct virtio_net_config, mac),
4515 addr, ETH_ALEN);
4516 eth_hw_addr_set(dev, addr);
4517 } else {
4518 eth_hw_addr_random(dev);
4519 dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
4520 dev->dev_addr);
4521 }
4522
4523 /* Set up our device-specific information */
4524 vi = netdev_priv(dev);
4525 vi->dev = dev;
4526 vi->vdev = vdev;
4527 vdev->priv = vi;
4528
4529 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
4530 spin_lock_init(&vi->refill_lock);
4531
4532 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
4533 vi->mergeable_rx_bufs = true;
4534 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
4535 }
4536
4537 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
4538 vi->intr_coal_rx.max_usecs = 0;
4539 vi->intr_coal_tx.max_usecs = 0;
4540 vi->intr_coal_tx.max_packets = 0;
4541 vi->intr_coal_rx.max_packets = 0;
4542 }
4543
4544 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
4545 vi->has_rss_hash_report = true;
4546
4547 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) {
4548 vi->has_rss = true;
4549
4550 vi->rss_indir_table_size =
4551 virtio_cread16(vdev, offsetof(struct virtio_net_config,
4552 rss_max_indirection_table_length));
4553 }
4554
4555 if (vi->has_rss || vi->has_rss_hash_report) {
4556 vi->rss_key_size =
4557 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
4558 if (vi->rss_key_size > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
4559 dev_err(&vdev->dev, "rss_max_key_size=%u exceeds the limit %u.\n",
4560 vi->rss_key_size, VIRTIO_NET_RSS_MAX_KEY_SIZE);
4561 err = -EINVAL;
4562 goto free;
4563 }
4564
4565 vi->rss_hash_types_supported =
4566 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
4567 vi->rss_hash_types_supported &=
4568 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
4569 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
4570 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
4571
4572 dev->hw_features |= NETIF_F_RXHASH;
4573 }
4574
4575 if (vi->has_rss_hash_report)
4576 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
4577 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
4578 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
4579 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
4580 else
4581 vi->hdr_len = sizeof(struct virtio_net_hdr);
4582
4583 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
4584 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
4585 vi->any_header_sg = true;
4586
4587 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
4588 vi->has_cvq = true;
4589
4590 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
4591 mtu = virtio_cread16(vdev,
4592 offsetof(struct virtio_net_config,
4593 mtu));
4594 if (mtu < dev->min_mtu) {
4595 /* Should never trigger: MTU was previously validated
4596 * in virtnet_validate.
4597 */
4598 dev_err(&vdev->dev,
4599 "device MTU appears to have changed it is now %d < %d",
4600 mtu, dev->min_mtu);
4601 err = -EINVAL;
4602 goto free;
4603 }
4604
4605 dev->mtu = mtu;
4606 dev->max_mtu = mtu;
4607 }
4608
4609 virtnet_set_big_packets(vi, mtu);
4610
4611 if (vi->any_header_sg)
4612 dev->needed_headroom = vi->hdr_len;
4613
4614 /* Enable multiqueue by default */
4615 if (num_online_cpus() >= max_queue_pairs)
4616 vi->curr_queue_pairs = max_queue_pairs;
4617 else
4618 vi->curr_queue_pairs = num_online_cpus();
4619 vi->max_queue_pairs = max_queue_pairs;
4620
4621 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
4622 err = init_vqs(vi);
4623 if (err)
4624 goto free;
4625
4626 #ifdef CONFIG_SYSFS
4627 if (vi->mergeable_rx_bufs)
4628 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
4629 #endif
4630 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
4631 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
4632
4633 virtnet_init_settings(dev);
4634
4635 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
4636 vi->failover = net_failover_create(vi->dev);
4637 if (IS_ERR(vi->failover)) {
4638 err = PTR_ERR(vi->failover);
4639 goto free_vqs;
4640 }
4641 }
4642
4643 if (vi->has_rss || vi->has_rss_hash_report)
4644 virtnet_init_default_rss(vi);
4645
4646 /* serialize netdev register + virtio_device_ready() with ndo_open() */
4647 rtnl_lock();
4648
4649 err = register_netdevice(dev);
4650 if (err) {
4651 pr_debug("virtio_net: registering device failed\n");
4652 rtnl_unlock();
4653 goto free_failover;
4654 }
4655
4656 virtio_device_ready(vdev);
4657
4658 _virtnet_set_queues(vi, vi->curr_queue_pairs);
4659
4660 /* a random MAC address has been assigned, notify the device.
4661 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
4662 * because many devices work fine without getting MAC explicitly
4663 */
4664 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
4665 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
4666 struct scatterlist sg;
4667
4668 sg_init_one(&sg, dev->dev_addr, dev->addr_len);
4669 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
4670 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
4671 pr_debug("virtio_net: setting MAC address failed\n");
4672 rtnl_unlock();
4673 err = -EINVAL;
4674 goto free_unregister_netdev;
4675 }
4676 }
4677
4678 rtnl_unlock();
4679
4680 err = virtnet_cpu_notif_add(vi);
4681 if (err) {
4682 pr_debug("virtio_net: registering cpu notifier failed\n");
4683 goto free_unregister_netdev;
4684 }
4685
4686 /* Assume link up if device can't report link status,
4687 otherwise get link status from config. */
4688 netif_carrier_off(dev);
4689 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
4690 schedule_work(&vi->config_work);
4691 } else {
4692 vi->status = VIRTIO_NET_S_LINK_UP;
4693 virtnet_update_settings(vi);
4694 netif_carrier_on(dev);
4695 }
4696
4697 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
4698 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
4699 set_bit(guest_offloads[i], &vi->guest_offloads);
4700 vi->guest_offloads_capable = vi->guest_offloads;
4701
4702 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
4703 dev->name, max_queue_pairs);
4704
4705 return 0;
4706
4707 free_unregister_netdev:
4708 unregister_netdev(dev);
4709 free_failover:
4710 net_failover_destroy(vi->failover);
4711 free_vqs:
4712 virtio_reset_device(vdev);
4713 cancel_delayed_work_sync(&vi->refill);
4714 free_receive_page_frags(vi);
4715 virtnet_del_vqs(vi);
4716 free:
4717 free_netdev(dev);
4718 return err;
4719 }
4720
remove_vq_common(struct virtnet_info * vi)4721 static void remove_vq_common(struct virtnet_info *vi)
4722 {
4723 virtio_reset_device(vi->vdev);
4724
4725 /* Free unused buffers in both send and recv, if any. */
4726 free_unused_bufs(vi);
4727
4728 free_receive_bufs(vi);
4729
4730 free_receive_page_frags(vi);
4731
4732 virtnet_del_vqs(vi);
4733 }
4734
virtnet_remove(struct virtio_device * vdev)4735 static void virtnet_remove(struct virtio_device *vdev)
4736 {
4737 struct virtnet_info *vi = vdev->priv;
4738
4739 virtnet_cpu_notif_remove(vi);
4740
4741 /* Make sure no work handler is accessing the device. */
4742 flush_work(&vi->config_work);
4743
4744 unregister_netdev(vi->dev);
4745
4746 net_failover_destroy(vi->failover);
4747
4748 remove_vq_common(vi);
4749
4750 free_netdev(vi->dev);
4751 }
4752
virtnet_freeze(struct virtio_device * vdev)4753 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
4754 {
4755 struct virtnet_info *vi = vdev->priv;
4756
4757 virtnet_cpu_notif_remove(vi);
4758 virtnet_freeze_down(vdev);
4759 remove_vq_common(vi);
4760
4761 return 0;
4762 }
4763
virtnet_restore(struct virtio_device * vdev)4764 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
4765 {
4766 struct virtnet_info *vi = vdev->priv;
4767 int err;
4768
4769 err = virtnet_restore_up(vdev);
4770 if (err)
4771 return err;
4772 virtnet_set_queues(vi, vi->curr_queue_pairs);
4773
4774 err = virtnet_cpu_notif_add(vi);
4775 if (err) {
4776 virtnet_freeze_down(vdev);
4777 remove_vq_common(vi);
4778 return err;
4779 }
4780
4781 return 0;
4782 }
4783
4784 static struct virtio_device_id id_table[] = {
4785 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
4786 { 0 },
4787 };
4788
4789 #define VIRTNET_FEATURES \
4790 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
4791 VIRTIO_NET_F_MAC, \
4792 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
4793 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
4794 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
4795 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
4796 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
4797 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
4798 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
4799 VIRTIO_NET_F_CTRL_MAC_ADDR, \
4800 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
4801 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
4802 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
4803 VIRTIO_NET_F_VQ_NOTF_COAL, \
4804 VIRTIO_NET_F_GUEST_HDRLEN
4805
4806 static unsigned int features[] = {
4807 VIRTNET_FEATURES,
4808 };
4809
4810 static unsigned int features_legacy[] = {
4811 VIRTNET_FEATURES,
4812 VIRTIO_NET_F_GSO,
4813 VIRTIO_F_ANY_LAYOUT,
4814 };
4815
4816 static struct virtio_driver virtio_net_driver = {
4817 .feature_table = features,
4818 .feature_table_size = ARRAY_SIZE(features),
4819 .feature_table_legacy = features_legacy,
4820 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
4821 .driver.name = KBUILD_MODNAME,
4822 .driver.owner = THIS_MODULE,
4823 .id_table = id_table,
4824 .validate = virtnet_validate,
4825 .probe = virtnet_probe,
4826 .remove = virtnet_remove,
4827 .config_changed = virtnet_config_changed,
4828 #ifdef CONFIG_PM_SLEEP
4829 .freeze = virtnet_freeze,
4830 .restore = virtnet_restore,
4831 #endif
4832 };
4833
virtio_net_driver_init(void)4834 static __init int virtio_net_driver_init(void)
4835 {
4836 int ret;
4837
4838 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
4839 virtnet_cpu_online,
4840 virtnet_cpu_down_prep);
4841 if (ret < 0)
4842 goto out;
4843 virtionet_online = ret;
4844 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
4845 NULL, virtnet_cpu_dead);
4846 if (ret)
4847 goto err_dead;
4848 ret = register_virtio_driver(&virtio_net_driver);
4849 if (ret)
4850 goto err_virtio;
4851 return 0;
4852 err_virtio:
4853 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
4854 err_dead:
4855 cpuhp_remove_multi_state(virtionet_online);
4856 out:
4857 return ret;
4858 }
4859 module_init(virtio_net_driver_init);
4860
virtio_net_driver_exit(void)4861 static __exit void virtio_net_driver_exit(void)
4862 {
4863 unregister_virtio_driver(&virtio_net_driver);
4864 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
4865 cpuhp_remove_multi_state(virtionet_online);
4866 }
4867 module_exit(virtio_net_driver_exit);
4868
4869 MODULE_DEVICE_TABLE(virtio, id_table);
4870 MODULE_DESCRIPTION("Virtio network driver");
4871 MODULE_LICENSE("GPL");
4872