1
2
3
4
5
6
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
26static int napi_weight = NAPI_POLL_WEIGHT;
27module_param(napi_weight, int, 0444);
28
29static bool csum = true, gso = true, napi_tx = true;
30module_param(csum, bool, 0444);
31module_param(gso, bool, 0444);
32module_param(napi_tx, bool, 0644);
33
34
35#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
36#define GOOD_COPY_LEN 128
37
38#define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
39
40
41#define VIRTIO_XDP_HEADROOM 256
42
43
44#define VIRTIO_XDP_TX BIT(0)
45#define VIRTIO_XDP_REDIR BIT(1)
46
47#define VIRTIO_XDP_FLAG BIT(0)
48
49
50
51
52
53
54DECLARE_EWMA(pkt_len, 0, 64)
55
56#define VIRTNET_DRIVER_VERSION "1.0.0"
57
58static const unsigned long guest_offloads[] = {
59 VIRTIO_NET_F_GUEST_TSO4,
60 VIRTIO_NET_F_GUEST_TSO6,
61 VIRTIO_NET_F_GUEST_ECN,
62 VIRTIO_NET_F_GUEST_UFO,
63 VIRTIO_NET_F_GUEST_CSUM
64};
65
66#define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
67 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
68 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
69 (1ULL << VIRTIO_NET_F_GUEST_UFO))
70
71struct virtnet_stat_desc {
72 char desc[ETH_GSTRING_LEN];
73 size_t offset;
74};
75
76struct virtnet_sq_stats {
77 struct u64_stats_sync syncp;
78 u64 packets;
79 u64 bytes;
80 u64 xdp_tx;
81 u64 xdp_tx_drops;
82 u64 kicks;
83 u64 tx_timeouts;
84};
85
86struct virtnet_rq_stats {
87 struct u64_stats_sync syncp;
88 u64 packets;
89 u64 bytes;
90 u64 drops;
91 u64 xdp_packets;
92 u64 xdp_tx;
93 u64 xdp_redirects;
94 u64 xdp_drops;
95 u64 kicks;
96};
97
98#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
99#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
100
101static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
102 { "packets", VIRTNET_SQ_STAT(packets) },
103 { "bytes", VIRTNET_SQ_STAT(bytes) },
104 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
105 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
106 { "kicks", VIRTNET_SQ_STAT(kicks) },
107 { "tx_timeouts", VIRTNET_SQ_STAT(tx_timeouts) },
108};
109
110static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
111 { "packets", VIRTNET_RQ_STAT(packets) },
112 { "bytes", VIRTNET_RQ_STAT(bytes) },
113 { "drops", VIRTNET_RQ_STAT(drops) },
114 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
115 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
116 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
117 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
118 { "kicks", VIRTNET_RQ_STAT(kicks) },
119};
120
121#define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
122#define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
123
124
125struct send_queue {
126
127 struct virtqueue *vq;
128
129
130 struct scatterlist sg[MAX_SKB_FRAGS + 2];
131
132
133 char name[40];
134
135 struct virtnet_sq_stats stats;
136
137 struct napi_struct napi;
138};
139
140
141struct receive_queue {
142
143 struct virtqueue *vq;
144
145 struct napi_struct napi;
146
147 struct bpf_prog __rcu *xdp_prog;
148
149 struct virtnet_rq_stats stats;
150
151
152 struct page *pages;
153
154
155 struct ewma_pkt_len mrg_avg_pkt_len;
156
157
158 struct page_frag alloc_frag;
159
160
161 struct scatterlist sg[MAX_SKB_FRAGS + 2];
162
163
164 unsigned int min_buf_len;
165
166
167 char name[40];
168
169 struct xdp_rxq_info xdp_rxq;
170};
171
172
173struct control_buf {
174 struct virtio_net_ctrl_hdr hdr;
175 virtio_net_ctrl_ack status;
176 struct virtio_net_ctrl_mq mq;
177 u8 promisc;
178 u8 allmulti;
179 __virtio16 vid;
180 __virtio64 offloads;
181};
182
183struct virtnet_info {
184 struct virtio_device *vdev;
185 struct virtqueue *cvq;
186 struct net_device *dev;
187 struct send_queue *sq;
188 struct receive_queue *rq;
189 unsigned int status;
190
191
192 u16 max_queue_pairs;
193
194
195 u16 curr_queue_pairs;
196
197
198 u16 xdp_queue_pairs;
199
200
201 bool xdp_enabled;
202
203
204 bool big_packets;
205
206
207 bool mergeable_rx_bufs;
208
209
210 bool has_cvq;
211
212
213 bool any_header_sg;
214
215
216 u8 hdr_len;
217
218
219 struct delayed_work refill;
220
221
222 struct work_struct config_work;
223
224
225 bool affinity_hint_set;
226
227
228 struct hlist_node node;
229 struct hlist_node node_dead;
230
231 struct control_buf *ctrl;
232
233
234 u8 duplex;
235 u32 speed;
236
237 unsigned long guest_offloads;
238 unsigned long guest_offloads_capable;
239
240
241 struct failover *failover;
242};
243
244struct padded_vnet_hdr {
245 struct virtio_net_hdr_mrg_rxbuf hdr;
246
247
248
249
250
251 char padding[4];
252};
253
254static bool is_xdp_frame(void *ptr)
255{
256 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
257}
258
259static void *xdp_to_ptr(struct xdp_frame *ptr)
260{
261 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
262}
263
264static struct xdp_frame *ptr_to_xdp(void *ptr)
265{
266 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
267}
268
269
270
271
272static int vq2txq(struct virtqueue *vq)
273{
274 return (vq->index - 1) / 2;
275}
276
277static int txq2vq(int txq)
278{
279 return txq * 2 + 1;
280}
281
282static int vq2rxq(struct virtqueue *vq)
283{
284 return vq->index / 2;
285}
286
287static int rxq2vq(int rxq)
288{
289 return rxq * 2;
290}
291
292static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
293{
294 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
295}
296
297
298
299
300
301static void give_pages(struct receive_queue *rq, struct page *page)
302{
303 struct page *end;
304
305
306 for (end = page; end->private; end = (struct page *)end->private);
307 end->private = (unsigned long)rq->pages;
308 rq->pages = page;
309}
310
311static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
312{
313 struct page *p = rq->pages;
314
315 if (p) {
316 rq->pages = (struct page *)p->private;
317
318 p->private = 0;
319 } else
320 p = alloc_page(gfp_mask);
321 return p;
322}
323
324static void virtqueue_napi_schedule(struct napi_struct *napi,
325 struct virtqueue *vq)
326{
327 if (napi_schedule_prep(napi)) {
328 virtqueue_disable_cb(vq);
329 __napi_schedule(napi);
330 }
331}
332
333static void virtqueue_napi_complete(struct napi_struct *napi,
334 struct virtqueue *vq, int processed)
335{
336 int opaque;
337
338 opaque = virtqueue_enable_cb_prepare(vq);
339 if (napi_complete_done(napi, processed)) {
340 if (unlikely(virtqueue_poll(vq, opaque)))
341 virtqueue_napi_schedule(napi, vq);
342 } else {
343 virtqueue_disable_cb(vq);
344 }
345}
346
347static void skb_xmit_done(struct virtqueue *vq)
348{
349 struct virtnet_info *vi = vq->vdev->priv;
350 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
351
352
353 virtqueue_disable_cb(vq);
354
355 if (napi->weight)
356 virtqueue_napi_schedule(napi, vq);
357 else
358
359 netif_wake_subqueue(vi->dev, vq2txq(vq));
360}
361
362#define MRG_CTX_HEADER_SHIFT 22
363static void *mergeable_len_to_ctx(unsigned int truesize,
364 unsigned int headroom)
365{
366 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
367}
368
369static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
370{
371 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
372}
373
374static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
375{
376 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
377}
378
379
380static struct sk_buff *page_to_skb(struct virtnet_info *vi,
381 struct receive_queue *rq,
382 struct page *page, unsigned int offset,
383 unsigned int len, unsigned int truesize,
384 bool hdr_valid, unsigned int metasize,
385 unsigned int headroom)
386{
387 struct sk_buff *skb;
388 struct virtio_net_hdr_mrg_rxbuf *hdr;
389 unsigned int copy, hdr_len, hdr_padded_len;
390 struct page *page_to_free = NULL;
391 int tailroom, shinfo_size;
392 char *p, *hdr_p, *buf;
393
394 p = page_address(page) + offset;
395 hdr_p = p;
396
397 hdr_len = vi->hdr_len;
398 if (vi->mergeable_rx_bufs)
399 hdr_padded_len = sizeof(*hdr);
400 else
401 hdr_padded_len = sizeof(struct padded_vnet_hdr);
402
403
404
405
406
407
408
409
410 truesize = headroom ? PAGE_SIZE : truesize;
411 tailroom = truesize - headroom;
412 buf = p - headroom;
413
414 len -= hdr_len;
415 offset += hdr_padded_len;
416 p += hdr_padded_len;
417 tailroom -= hdr_padded_len + len;
418
419 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
420
421
422 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
423 skb = build_skb(buf, truesize);
424 if (unlikely(!skb))
425 return NULL;
426
427 skb_reserve(skb, p - buf);
428 skb_put(skb, len);
429
430 page = (struct page *)page->private;
431 if (page)
432 give_pages(rq, page);
433 goto ok;
434 }
435
436
437 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
438 if (unlikely(!skb))
439 return NULL;
440
441
442
443
444 if (len <= skb_tailroom(skb))
445 copy = len;
446 else
447 copy = ETH_HLEN + metasize;
448 skb_put_data(skb, p, copy);
449
450 len -= copy;
451 offset += copy;
452
453 if (vi->mergeable_rx_bufs) {
454 if (len)
455 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
456 else
457 page_to_free = page;
458 goto ok;
459 }
460
461
462
463
464
465
466
467 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
468 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
469 dev_kfree_skb(skb);
470 return NULL;
471 }
472 BUG_ON(offset >= PAGE_SIZE);
473 while (len) {
474 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
475 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
476 frag_size, truesize);
477 len -= frag_size;
478 page = (struct page *)page->private;
479 offset = 0;
480 }
481
482 if (page)
483 give_pages(rq, page);
484
485ok:
486
487 if (hdr_valid) {
488 hdr = skb_vnet_hdr(skb);
489 memcpy(hdr, hdr_p, hdr_len);
490 }
491 if (page_to_free)
492 put_page(page_to_free);
493
494 if (metasize) {
495 __skb_pull(skb, metasize);
496 skb_metadata_set(skb, metasize);
497 }
498
499 return skb;
500}
501
502static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
503 struct send_queue *sq,
504 struct xdp_frame *xdpf)
505{
506 struct virtio_net_hdr_mrg_rxbuf *hdr;
507 int err;
508
509 if (unlikely(xdpf->headroom < vi->hdr_len))
510 return -EOVERFLOW;
511
512
513 xdpf->data -= vi->hdr_len;
514
515 hdr = xdpf->data;
516 memset(hdr, 0, vi->hdr_len);
517 xdpf->len += vi->hdr_len;
518
519 sg_init_one(sq->sg, xdpf->data, xdpf->len);
520
521 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
522 GFP_ATOMIC);
523 if (unlikely(err))
524 return -ENOSPC;
525
526 return 0;
527}
528
529
530
531
532
533
534
535
536
537#define virtnet_xdp_get_sq(vi) ({ \
538 int cpu = smp_processor_id(); \
539 struct netdev_queue *txq; \
540 typeof(vi) v = (vi); \
541 unsigned int qp; \
542 \
543 if (v->curr_queue_pairs > nr_cpu_ids) { \
544 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
545 qp += cpu; \
546 txq = netdev_get_tx_queue(v->dev, qp); \
547 __netif_tx_acquire(txq); \
548 } else { \
549 qp = cpu % v->curr_queue_pairs; \
550 txq = netdev_get_tx_queue(v->dev, qp); \
551 __netif_tx_lock(txq, cpu); \
552 } \
553 v->sq + qp; \
554})
555
556#define virtnet_xdp_put_sq(vi, q) { \
557 struct netdev_queue *txq; \
558 typeof(vi) v = (vi); \
559 \
560 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
561 if (v->curr_queue_pairs > nr_cpu_ids) \
562 __netif_tx_release(txq); \
563 else \
564 __netif_tx_unlock(txq); \
565}
566
567static int virtnet_xdp_xmit(struct net_device *dev,
568 int n, struct xdp_frame **frames, u32 flags)
569{
570 struct virtnet_info *vi = netdev_priv(dev);
571 struct receive_queue *rq = vi->rq;
572 struct bpf_prog *xdp_prog;
573 struct send_queue *sq;
574 unsigned int len;
575 int packets = 0;
576 int bytes = 0;
577 int nxmit = 0;
578 int kicks = 0;
579 void *ptr;
580 int ret;
581 int i;
582
583
584
585
586 xdp_prog = rcu_access_pointer(rq->xdp_prog);
587 if (!xdp_prog)
588 return -ENXIO;
589
590 sq = virtnet_xdp_get_sq(vi);
591
592 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
593 ret = -EINVAL;
594 goto out;
595 }
596
597
598 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
599 if (likely(is_xdp_frame(ptr))) {
600 struct xdp_frame *frame = ptr_to_xdp(ptr);
601
602 bytes += frame->len;
603 xdp_return_frame(frame);
604 } else {
605 struct sk_buff *skb = ptr;
606
607 bytes += skb->len;
608 napi_consume_skb(skb, false);
609 }
610 packets++;
611 }
612
613 for (i = 0; i < n; i++) {
614 struct xdp_frame *xdpf = frames[i];
615
616 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
617 break;
618 nxmit++;
619 }
620 ret = nxmit;
621
622 if (flags & XDP_XMIT_FLUSH) {
623 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
624 kicks = 1;
625 }
626out:
627 u64_stats_update_begin(&sq->stats.syncp);
628 sq->stats.bytes += bytes;
629 sq->stats.packets += packets;
630 sq->stats.xdp_tx += n;
631 sq->stats.xdp_tx_drops += n - nxmit;
632 sq->stats.kicks += kicks;
633 u64_stats_update_end(&sq->stats.syncp);
634
635 virtnet_xdp_put_sq(vi, sq);
636 return ret;
637}
638
639static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
640{
641 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
642}
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658static struct page *xdp_linearize_page(struct receive_queue *rq,
659 u16 *num_buf,
660 struct page *p,
661 int offset,
662 int page_off,
663 unsigned int *len)
664{
665 struct page *page = alloc_page(GFP_ATOMIC);
666
667 if (!page)
668 return NULL;
669
670 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
671 page_off += *len;
672
673 while (--*num_buf) {
674 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
675 unsigned int buflen;
676 void *buf;
677 int off;
678
679 buf = virtqueue_get_buf(rq->vq, &buflen);
680 if (unlikely(!buf))
681 goto err_buf;
682
683 p = virt_to_head_page(buf);
684 off = buf - page_address(p);
685
686
687
688
689 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
690 put_page(p);
691 goto err_buf;
692 }
693
694 memcpy(page_address(page) + page_off,
695 page_address(p) + off, buflen);
696 page_off += buflen;
697 put_page(p);
698 }
699
700
701 *len = page_off - VIRTIO_XDP_HEADROOM;
702 return page;
703err_buf:
704 __free_pages(page, 0);
705 return NULL;
706}
707
708static struct sk_buff *receive_small(struct net_device *dev,
709 struct virtnet_info *vi,
710 struct receive_queue *rq,
711 void *buf, void *ctx,
712 unsigned int len,
713 unsigned int *xdp_xmit,
714 struct virtnet_rq_stats *stats)
715{
716 struct sk_buff *skb;
717 struct bpf_prog *xdp_prog;
718 unsigned int xdp_headroom = (unsigned long)ctx;
719 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
720 unsigned int headroom = vi->hdr_len + header_offset;
721 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
722 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
723 struct page *page = virt_to_head_page(buf);
724 unsigned int delta = 0;
725 struct page *xdp_page;
726 int err;
727 unsigned int metasize = 0;
728
729 len -= vi->hdr_len;
730 stats->bytes += len;
731
732 if (unlikely(len > GOOD_PACKET_LEN)) {
733 pr_debug("%s: rx error: len %u exceeds max size %d\n",
734 dev->name, len, GOOD_PACKET_LEN);
735 dev->stats.rx_length_errors++;
736 goto err;
737 }
738
739 if (likely(!vi->xdp_enabled)) {
740 xdp_prog = NULL;
741 goto skip_xdp;
742 }
743
744 rcu_read_lock();
745 xdp_prog = rcu_dereference(rq->xdp_prog);
746 if (xdp_prog) {
747 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
748 struct xdp_frame *xdpf;
749 struct xdp_buff xdp;
750 void *orig_data;
751 u32 act;
752
753 if (unlikely(hdr->hdr.gso_type))
754 goto err_xdp;
755
756 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
757 int offset = buf - page_address(page) + header_offset;
758 unsigned int tlen = len + vi->hdr_len;
759 u16 num_buf = 1;
760
761 xdp_headroom = virtnet_get_headroom(vi);
762 header_offset = VIRTNET_RX_PAD + xdp_headroom;
763 headroom = vi->hdr_len + header_offset;
764 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
765 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
766 xdp_page = xdp_linearize_page(rq, &num_buf, page,
767 offset, header_offset,
768 &tlen);
769 if (!xdp_page)
770 goto err_xdp;
771
772 buf = page_address(xdp_page);
773 put_page(page);
774 page = xdp_page;
775 }
776
777 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
778 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
779 xdp_headroom, len, true);
780 orig_data = xdp.data;
781 act = bpf_prog_run_xdp(xdp_prog, &xdp);
782 stats->xdp_packets++;
783
784 switch (act) {
785 case XDP_PASS:
786
787 delta = orig_data - xdp.data;
788 len = xdp.data_end - xdp.data;
789 metasize = xdp.data - xdp.data_meta;
790 break;
791 case XDP_TX:
792 stats->xdp_tx++;
793 xdpf = xdp_convert_buff_to_frame(&xdp);
794 if (unlikely(!xdpf))
795 goto err_xdp;
796 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
797 if (unlikely(!err)) {
798 xdp_return_frame_rx_napi(xdpf);
799 } else if (unlikely(err < 0)) {
800 trace_xdp_exception(vi->dev, xdp_prog, act);
801 goto err_xdp;
802 }
803 *xdp_xmit |= VIRTIO_XDP_TX;
804 rcu_read_unlock();
805 goto xdp_xmit;
806 case XDP_REDIRECT:
807 stats->xdp_redirects++;
808 err = xdp_do_redirect(dev, &xdp, xdp_prog);
809 if (err)
810 goto err_xdp;
811 *xdp_xmit |= VIRTIO_XDP_REDIR;
812 rcu_read_unlock();
813 goto xdp_xmit;
814 default:
815 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
816 fallthrough;
817 case XDP_ABORTED:
818 trace_xdp_exception(vi->dev, xdp_prog, act);
819 goto err_xdp;
820 case XDP_DROP:
821 goto err_xdp;
822 }
823 }
824 rcu_read_unlock();
825
826skip_xdp:
827 skb = build_skb(buf, buflen);
828 if (!skb)
829 goto err;
830 skb_reserve(skb, headroom - delta);
831 skb_put(skb, len);
832 if (!xdp_prog) {
833 buf += header_offset;
834 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
835 }
836
837 if (metasize)
838 skb_metadata_set(skb, metasize);
839
840 return skb;
841
842err_xdp:
843 rcu_read_unlock();
844 stats->xdp_drops++;
845err:
846 stats->drops++;
847 put_page(page);
848xdp_xmit:
849 return NULL;
850}
851
852static struct sk_buff *receive_big(struct net_device *dev,
853 struct virtnet_info *vi,
854 struct receive_queue *rq,
855 void *buf,
856 unsigned int len,
857 struct virtnet_rq_stats *stats)
858{
859 struct page *page = buf;
860 struct sk_buff *skb =
861 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0);
862
863 stats->bytes += len - vi->hdr_len;
864 if (unlikely(!skb))
865 goto err;
866
867 return skb;
868
869err:
870 stats->drops++;
871 give_pages(rq, page);
872 return NULL;
873}
874
875static struct sk_buff *receive_mergeable(struct net_device *dev,
876 struct virtnet_info *vi,
877 struct receive_queue *rq,
878 void *buf,
879 void *ctx,
880 unsigned int len,
881 unsigned int *xdp_xmit,
882 struct virtnet_rq_stats *stats)
883{
884 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
885 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
886 struct page *page = virt_to_head_page(buf);
887 int offset = buf - page_address(page);
888 struct sk_buff *head_skb, *curr_skb;
889 struct bpf_prog *xdp_prog;
890 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
891 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
892 unsigned int metasize = 0;
893 unsigned int frame_sz;
894 int err;
895
896 head_skb = NULL;
897 stats->bytes += len - vi->hdr_len;
898
899 if (unlikely(len > truesize)) {
900 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
901 dev->name, len, (unsigned long)ctx);
902 dev->stats.rx_length_errors++;
903 goto err_skb;
904 }
905
906 if (likely(!vi->xdp_enabled)) {
907 xdp_prog = NULL;
908 goto skip_xdp;
909 }
910
911 rcu_read_lock();
912 xdp_prog = rcu_dereference(rq->xdp_prog);
913 if (xdp_prog) {
914 struct xdp_frame *xdpf;
915 struct page *xdp_page;
916 struct xdp_buff xdp;
917 void *data;
918 u32 act;
919
920
921
922
923
924 if (unlikely(hdr->hdr.gso_type))
925 goto err_xdp;
926
927
928
929
930 frame_sz = headroom ? PAGE_SIZE : truesize;
931
932
933
934
935
936
937
938 if (unlikely(num_buf > 1 ||
939 headroom < virtnet_get_headroom(vi))) {
940
941 xdp_page = xdp_linearize_page(rq, &num_buf,
942 page, offset,
943 VIRTIO_XDP_HEADROOM,
944 &len);
945 frame_sz = PAGE_SIZE;
946
947 if (!xdp_page)
948 goto err_xdp;
949 offset = VIRTIO_XDP_HEADROOM;
950 } else {
951 xdp_page = page;
952 }
953
954
955
956
957 data = page_address(xdp_page) + offset;
958 xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq);
959 xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len,
960 VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true);
961
962 act = bpf_prog_run_xdp(xdp_prog, &xdp);
963 stats->xdp_packets++;
964
965 switch (act) {
966 case XDP_PASS:
967 metasize = xdp.data - xdp.data_meta;
968
969
970
971
972
973
974 offset = xdp.data - page_address(xdp_page) -
975 vi->hdr_len - metasize;
976
977
978
979
980 len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
981
982 if (unlikely(xdp_page != page)) {
983 rcu_read_unlock();
984 put_page(page);
985 head_skb = page_to_skb(vi, rq, xdp_page, offset,
986 len, PAGE_SIZE, false,
987 metasize,
988 VIRTIO_XDP_HEADROOM);
989 return head_skb;
990 }
991 break;
992 case XDP_TX:
993 stats->xdp_tx++;
994 xdpf = xdp_convert_buff_to_frame(&xdp);
995 if (unlikely(!xdpf))
996 goto err_xdp;
997 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
998 if (unlikely(!err)) {
999 xdp_return_frame_rx_napi(xdpf);
1000 } else if (unlikely(err < 0)) {
1001 trace_xdp_exception(vi->dev, xdp_prog, act);
1002 if (unlikely(xdp_page != page))
1003 put_page(xdp_page);
1004 goto err_xdp;
1005 }
1006 *xdp_xmit |= VIRTIO_XDP_TX;
1007 if (unlikely(xdp_page != page))
1008 put_page(page);
1009 rcu_read_unlock();
1010 goto xdp_xmit;
1011 case XDP_REDIRECT:
1012 stats->xdp_redirects++;
1013 err = xdp_do_redirect(dev, &xdp, xdp_prog);
1014 if (err) {
1015 if (unlikely(xdp_page != page))
1016 put_page(xdp_page);
1017 goto err_xdp;
1018 }
1019 *xdp_xmit |= VIRTIO_XDP_REDIR;
1020 if (unlikely(xdp_page != page))
1021 put_page(page);
1022 rcu_read_unlock();
1023 goto xdp_xmit;
1024 default:
1025 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
1026 fallthrough;
1027 case XDP_ABORTED:
1028 trace_xdp_exception(vi->dev, xdp_prog, act);
1029 fallthrough;
1030 case XDP_DROP:
1031 if (unlikely(xdp_page != page))
1032 __free_pages(xdp_page, 0);
1033 goto err_xdp;
1034 }
1035 }
1036 rcu_read_unlock();
1037
1038skip_xdp:
1039 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
1040 metasize, headroom);
1041 curr_skb = head_skb;
1042
1043 if (unlikely(!curr_skb))
1044 goto err_skb;
1045 while (--num_buf) {
1046 int num_skb_frags;
1047
1048 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
1049 if (unlikely(!buf)) {
1050 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1051 dev->name, num_buf,
1052 virtio16_to_cpu(vi->vdev,
1053 hdr->num_buffers));
1054 dev->stats.rx_length_errors++;
1055 goto err_buf;
1056 }
1057
1058 stats->bytes += len;
1059 page = virt_to_head_page(buf);
1060
1061 truesize = mergeable_ctx_to_truesize(ctx);
1062 if (unlikely(len > truesize)) {
1063 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1064 dev->name, len, (unsigned long)ctx);
1065 dev->stats.rx_length_errors++;
1066 goto err_skb;
1067 }
1068
1069 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
1070 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1071 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
1072
1073 if (unlikely(!nskb))
1074 goto err_skb;
1075 if (curr_skb == head_skb)
1076 skb_shinfo(curr_skb)->frag_list = nskb;
1077 else
1078 curr_skb->next = nskb;
1079 curr_skb = nskb;
1080 head_skb->truesize += nskb->truesize;
1081 num_skb_frags = 0;
1082 }
1083 if (curr_skb != head_skb) {
1084 head_skb->data_len += len;
1085 head_skb->len += len;
1086 head_skb->truesize += truesize;
1087 }
1088 offset = buf - page_address(page);
1089 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1090 put_page(page);
1091 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1092 len, truesize);
1093 } else {
1094 skb_add_rx_frag(curr_skb, num_skb_frags, page,
1095 offset, len, truesize);
1096 }
1097 }
1098
1099 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
1100 return head_skb;
1101
1102err_xdp:
1103 rcu_read_unlock();
1104 stats->xdp_drops++;
1105err_skb:
1106 put_page(page);
1107 while (num_buf-- > 1) {
1108 buf = virtqueue_get_buf(rq->vq, &len);
1109 if (unlikely(!buf)) {
1110 pr_debug("%s: rx error: %d buffers missing\n",
1111 dev->name, num_buf);
1112 dev->stats.rx_length_errors++;
1113 break;
1114 }
1115 stats->bytes += len;
1116 page = virt_to_head_page(buf);
1117 put_page(page);
1118 }
1119err_buf:
1120 stats->drops++;
1121 dev_kfree_skb(head_skb);
1122xdp_xmit:
1123 return NULL;
1124}
1125
1126static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1127 void *buf, unsigned int len, void **ctx,
1128 unsigned int *xdp_xmit,
1129 struct virtnet_rq_stats *stats)
1130{
1131 struct net_device *dev = vi->dev;
1132 struct sk_buff *skb;
1133 struct virtio_net_hdr_mrg_rxbuf *hdr;
1134
1135 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
1136 pr_debug("%s: short packet %i\n", dev->name, len);
1137 dev->stats.rx_length_errors++;
1138 if (vi->mergeable_rx_bufs) {
1139 put_page(virt_to_head_page(buf));
1140 } else if (vi->big_packets) {
1141 give_pages(rq, buf);
1142 } else {
1143 put_page(virt_to_head_page(buf));
1144 }
1145 return;
1146 }
1147
1148 if (vi->mergeable_rx_bufs)
1149 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1150 stats);
1151 else if (vi->big_packets)
1152 skb = receive_big(dev, vi, rq, buf, len, stats);
1153 else
1154 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1155
1156 if (unlikely(!skb))
1157 return;
1158
1159 hdr = skb_vnet_hdr(skb);
1160
1161 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
1162 skb->ip_summed = CHECKSUM_UNNECESSARY;
1163
1164 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1165 virtio_is_little_endian(vi->vdev))) {
1166 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1167 dev->name, hdr->hdr.gso_type,
1168 hdr->hdr.gso_size);
1169 goto frame_err;
1170 }
1171
1172 skb_record_rx_queue(skb, vq2rxq(rq->vq));
1173 skb->protocol = eth_type_trans(skb, dev);
1174 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1175 ntohs(skb->protocol), skb->len, skb->pkt_type);
1176
1177 napi_gro_receive(&rq->napi, skb);
1178 return;
1179
1180frame_err:
1181 dev->stats.rx_frame_errors++;
1182 dev_kfree_skb(skb);
1183}
1184
1185
1186
1187
1188
1189
1190static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1191 gfp_t gfp)
1192{
1193 struct page_frag *alloc_frag = &rq->alloc_frag;
1194 char *buf;
1195 unsigned int xdp_headroom = virtnet_get_headroom(vi);
1196 void *ctx = (void *)(unsigned long)xdp_headroom;
1197 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
1198 int err;
1199
1200 len = SKB_DATA_ALIGN(len) +
1201 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1202 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
1203 return -ENOMEM;
1204
1205 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1206 get_page(alloc_frag->page);
1207 alloc_frag->offset += len;
1208 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1209 vi->hdr_len + GOOD_PACKET_LEN);
1210 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1211 if (err < 0)
1212 put_page(virt_to_head_page(buf));
1213 return err;
1214}
1215
1216static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1217 gfp_t gfp)
1218{
1219 struct page *first, *list = NULL;
1220 char *p;
1221 int i, err, offset;
1222
1223 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1224
1225
1226 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
1227 first = get_a_page(rq, gfp);
1228 if (!first) {
1229 if (list)
1230 give_pages(rq, list);
1231 return -ENOMEM;
1232 }
1233 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
1234
1235
1236 first->private = (unsigned long)list;
1237 list = first;
1238 }
1239
1240 first = get_a_page(rq, gfp);
1241 if (!first) {
1242 give_pages(rq, list);
1243 return -ENOMEM;
1244 }
1245 p = page_address(first);
1246
1247
1248
1249 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
1250
1251
1252 offset = sizeof(struct padded_vnet_hdr);
1253 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
1254
1255
1256 first->private = (unsigned long)list;
1257 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1258 first, gfp);
1259 if (err < 0)
1260 give_pages(rq, first);
1261
1262 return err;
1263}
1264
1265static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
1266 struct ewma_pkt_len *avg_pkt_len,
1267 unsigned int room)
1268{
1269 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1270 unsigned int len;
1271
1272 if (room)
1273 return PAGE_SIZE - room;
1274
1275 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1276 rq->min_buf_len, PAGE_SIZE - hdr_len);
1277
1278 return ALIGN(len, L1_CACHE_BYTES);
1279}
1280
1281static int add_recvbuf_mergeable(struct virtnet_info *vi,
1282 struct receive_queue *rq, gfp_t gfp)
1283{
1284 struct page_frag *alloc_frag = &rq->alloc_frag;
1285 unsigned int headroom = virtnet_get_headroom(vi);
1286 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1287 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1288 char *buf;
1289 void *ctx;
1290 int err;
1291 unsigned int len, hole;
1292
1293
1294
1295
1296
1297 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1298 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
1299 return -ENOMEM;
1300
1301 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1302 buf += headroom;
1303 get_page(alloc_frag->page);
1304 alloc_frag->offset += len + room;
1305 hole = alloc_frag->size - alloc_frag->offset;
1306 if (hole < len + room) {
1307
1308
1309
1310
1311 len += hole;
1312 alloc_frag->offset += hole;
1313 }
1314
1315 sg_init_one(rq->sg, buf, len);
1316 ctx = mergeable_len_to_ctx(len, headroom);
1317 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1318 if (err < 0)
1319 put_page(virt_to_head_page(buf));
1320
1321 return err;
1322}
1323
1324
1325
1326
1327
1328
1329
1330
1331static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1332 gfp_t gfp)
1333{
1334 int err;
1335 bool oom;
1336
1337 do {
1338 if (vi->mergeable_rx_bufs)
1339 err = add_recvbuf_mergeable(vi, rq, gfp);
1340 else if (vi->big_packets)
1341 err = add_recvbuf_big(vi, rq, gfp);
1342 else
1343 err = add_recvbuf_small(vi, rq, gfp);
1344
1345 oom = err == -ENOMEM;
1346 if (err)
1347 break;
1348 } while (rq->vq->num_free);
1349 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1350 unsigned long flags;
1351
1352 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
1353 rq->stats.kicks++;
1354 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
1355 }
1356
1357 return !oom;
1358}
1359
1360static void skb_recv_done(struct virtqueue *rvq)
1361{
1362 struct virtnet_info *vi = rvq->vdev->priv;
1363 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1364
1365 virtqueue_napi_schedule(&rq->napi, rvq);
1366}
1367
1368static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
1369{
1370 napi_enable(napi);
1371
1372
1373
1374
1375
1376 local_bh_disable();
1377 virtqueue_napi_schedule(napi, vq);
1378 local_bh_enable();
1379}
1380
1381static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1382 struct virtqueue *vq,
1383 struct napi_struct *napi)
1384{
1385 if (!napi->weight)
1386 return;
1387
1388
1389
1390
1391 if (!vi->affinity_hint_set) {
1392 napi->weight = 0;
1393 return;
1394 }
1395
1396 return virtnet_napi_enable(vq, napi);
1397}
1398
1399static void virtnet_napi_tx_disable(struct napi_struct *napi)
1400{
1401 if (napi->weight)
1402 napi_disable(napi);
1403}
1404
1405static void refill_work(struct work_struct *work)
1406{
1407 struct virtnet_info *vi =
1408 container_of(work, struct virtnet_info, refill.work);
1409 bool still_empty;
1410 int i;
1411
1412 for (i = 0; i < vi->curr_queue_pairs; i++) {
1413 struct receive_queue *rq = &vi->rq[i];
1414
1415 napi_disable(&rq->napi);
1416 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1417 virtnet_napi_enable(rq->vq, &rq->napi);
1418
1419
1420
1421
1422 if (still_empty)
1423 schedule_delayed_work(&vi->refill, HZ/2);
1424 }
1425}
1426
1427static int virtnet_receive(struct receive_queue *rq, int budget,
1428 unsigned int *xdp_xmit)
1429{
1430 struct virtnet_info *vi = rq->vq->vdev->priv;
1431 struct virtnet_rq_stats stats = {};
1432 unsigned int len;
1433 void *buf;
1434 int i;
1435
1436 if (!vi->big_packets || vi->mergeable_rx_bufs) {
1437 void *ctx;
1438
1439 while (stats.packets < budget &&
1440 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1441 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1442 stats.packets++;
1443 }
1444 } else {
1445 while (stats.packets < budget &&
1446 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1447 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1448 stats.packets++;
1449 }
1450 }
1451
1452 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
1453 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
1454 schedule_delayed_work(&vi->refill, 0);
1455 }
1456
1457 u64_stats_update_begin(&rq->stats.syncp);
1458 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1459 size_t offset = virtnet_rq_stats_desc[i].offset;
1460 u64 *item;
1461
1462 item = (u64 *)((u8 *)&rq->stats + offset);
1463 *item += *(u64 *)((u8 *)&stats + offset);
1464 }
1465 u64_stats_update_end(&rq->stats.syncp);
1466
1467 return stats.packets;
1468}
1469
1470static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
1471{
1472 unsigned int len;
1473 unsigned int packets = 0;
1474 unsigned int bytes = 0;
1475 void *ptr;
1476
1477 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1478 if (likely(!is_xdp_frame(ptr))) {
1479 struct sk_buff *skb = ptr;
1480
1481 pr_debug("Sent skb %p\n", skb);
1482
1483 bytes += skb->len;
1484 napi_consume_skb(skb, in_napi);
1485 } else {
1486 struct xdp_frame *frame = ptr_to_xdp(ptr);
1487
1488 bytes += frame->len;
1489 xdp_return_frame(frame);
1490 }
1491 packets++;
1492 }
1493
1494
1495
1496
1497 if (!packets)
1498 return;
1499
1500 u64_stats_update_begin(&sq->stats.syncp);
1501 sq->stats.bytes += bytes;
1502 sq->stats.packets += packets;
1503 u64_stats_update_end(&sq->stats.syncp);
1504}
1505
1506static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1507{
1508 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1509 return false;
1510 else if (q < vi->curr_queue_pairs)
1511 return true;
1512 else
1513 return false;
1514}
1515
1516static void virtnet_poll_cleantx(struct receive_queue *rq)
1517{
1518 struct virtnet_info *vi = rq->vq->vdev->priv;
1519 unsigned int index = vq2rxq(rq->vq);
1520 struct send_queue *sq = &vi->sq[index];
1521 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1522
1523 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
1524 return;
1525
1526 if (__netif_tx_trylock(txq)) {
1527 do {
1528 virtqueue_disable_cb(sq->vq);
1529 free_old_xmit_skbs(sq, true);
1530 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1531
1532 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1533 netif_tx_wake_queue(txq);
1534
1535 __netif_tx_unlock(txq);
1536 }
1537}
1538
1539static int virtnet_poll(struct napi_struct *napi, int budget)
1540{
1541 struct receive_queue *rq =
1542 container_of(napi, struct receive_queue, napi);
1543 struct virtnet_info *vi = rq->vq->vdev->priv;
1544 struct send_queue *sq;
1545 unsigned int received;
1546 unsigned int xdp_xmit = 0;
1547
1548 virtnet_poll_cleantx(rq);
1549
1550 received = virtnet_receive(rq, budget, &xdp_xmit);
1551
1552
1553 if (received < budget)
1554 virtqueue_napi_complete(napi, rq->vq, received);
1555
1556 if (xdp_xmit & VIRTIO_XDP_REDIR)
1557 xdp_do_flush();
1558
1559 if (xdp_xmit & VIRTIO_XDP_TX) {
1560 sq = virtnet_xdp_get_sq(vi);
1561 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1562 u64_stats_update_begin(&sq->stats.syncp);
1563 sq->stats.kicks++;
1564 u64_stats_update_end(&sq->stats.syncp);
1565 }
1566 virtnet_xdp_put_sq(vi, sq);
1567 }
1568
1569 return received;
1570}
1571
1572static int virtnet_open(struct net_device *dev)
1573{
1574 struct virtnet_info *vi = netdev_priv(dev);
1575 int i, err;
1576
1577 for (i = 0; i < vi->max_queue_pairs; i++) {
1578 if (i < vi->curr_queue_pairs)
1579
1580 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1581 schedule_delayed_work(&vi->refill, 0);
1582
1583 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
1584 if (err < 0)
1585 return err;
1586
1587 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1588 MEM_TYPE_PAGE_SHARED, NULL);
1589 if (err < 0) {
1590 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1591 return err;
1592 }
1593
1594 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1595 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1596 }
1597
1598 return 0;
1599}
1600
1601static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1602{
1603 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1604 struct virtnet_info *vi = sq->vq->vdev->priv;
1605 unsigned int index = vq2txq(sq->vq);
1606 struct netdev_queue *txq;
1607 int opaque;
1608 bool done;
1609
1610 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1611
1612 napi_complete_done(napi, 0);
1613 return 0;
1614 }
1615
1616 txq = netdev_get_tx_queue(vi->dev, index);
1617 __netif_tx_lock(txq, raw_smp_processor_id());
1618 virtqueue_disable_cb(sq->vq);
1619 free_old_xmit_skbs(sq, true);
1620
1621 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1622 netif_tx_wake_queue(txq);
1623
1624 opaque = virtqueue_enable_cb_prepare(sq->vq);
1625
1626 done = napi_complete_done(napi, 0);
1627
1628 if (!done)
1629 virtqueue_disable_cb(sq->vq);
1630
1631 __netif_tx_unlock(txq);
1632
1633 if (done) {
1634 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
1635 if (napi_schedule_prep(napi)) {
1636 __netif_tx_lock(txq, raw_smp_processor_id());
1637 virtqueue_disable_cb(sq->vq);
1638 __netif_tx_unlock(txq);
1639 __napi_schedule(napi);
1640 }
1641 }
1642 }
1643
1644 return 0;
1645}
1646
1647static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1648{
1649 struct virtio_net_hdr_mrg_rxbuf *hdr;
1650 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1651 struct virtnet_info *vi = sq->vq->vdev->priv;
1652 int num_sg;
1653 unsigned hdr_len = vi->hdr_len;
1654 bool can_push;
1655
1656 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1657
1658 can_push = vi->any_header_sg &&
1659 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1660 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1661
1662
1663 if (can_push)
1664 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1665 else
1666 hdr = skb_vnet_hdr(skb);
1667
1668 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1669 virtio_is_little_endian(vi->vdev), false,
1670 0))
1671 return -EPROTO;
1672
1673 if (vi->mergeable_rx_bufs)
1674 hdr->num_buffers = 0;
1675
1676 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1677 if (can_push) {
1678 __skb_push(skb, hdr_len);
1679 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1680 if (unlikely(num_sg < 0))
1681 return num_sg;
1682
1683 __skb_pull(skb, hdr_len);
1684 } else {
1685 sg_set_buf(sq->sg, hdr, hdr_len);
1686 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1687 if (unlikely(num_sg < 0))
1688 return num_sg;
1689 num_sg++;
1690 }
1691 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
1692}
1693
1694static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
1695{
1696 struct virtnet_info *vi = netdev_priv(dev);
1697 int qnum = skb_get_queue_mapping(skb);
1698 struct send_queue *sq = &vi->sq[qnum];
1699 int err;
1700 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1701 bool kick = !netdev_xmit_more();
1702 bool use_napi = sq->napi.weight;
1703
1704
1705 do {
1706 if (use_napi)
1707 virtqueue_disable_cb(sq->vq);
1708
1709 free_old_xmit_skbs(sq, false);
1710
1711 } while (use_napi && kick &&
1712 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1713
1714
1715 skb_tx_timestamp(skb);
1716
1717
1718 err = xmit_skb(sq, skb);
1719
1720
1721 if (unlikely(err)) {
1722 dev->stats.tx_fifo_errors++;
1723 if (net_ratelimit())
1724 dev_warn(&dev->dev,
1725 "Unexpected TXQ (%d) queue failure: %d\n",
1726 qnum, err);
1727 dev->stats.tx_dropped++;
1728 dev_kfree_skb_any(skb);
1729 return NETDEV_TX_OK;
1730 }
1731
1732
1733 if (!use_napi) {
1734 skb_orphan(skb);
1735 nf_reset_ct(skb);
1736 }
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1749 netif_stop_subqueue(dev, qnum);
1750 if (!use_napi &&
1751 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1752
1753 free_old_xmit_skbs(sq, false);
1754 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1755 netif_start_subqueue(dev, qnum);
1756 virtqueue_disable_cb(sq->vq);
1757 }
1758 }
1759 }
1760
1761 if (kick || netif_xmit_stopped(txq)) {
1762 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1763 u64_stats_update_begin(&sq->stats.syncp);
1764 sq->stats.kicks++;
1765 u64_stats_update_end(&sq->stats.syncp);
1766 }
1767 }
1768
1769 return NETDEV_TX_OK;
1770}
1771
1772
1773
1774
1775
1776
1777static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1778 struct scatterlist *out)
1779{
1780 struct scatterlist *sgs[4], hdr, stat;
1781 unsigned out_num = 0, tmp;
1782 int ret;
1783
1784
1785 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
1786
1787 vi->ctrl->status = ~0;
1788 vi->ctrl->hdr.class = class;
1789 vi->ctrl->hdr.cmd = cmd;
1790
1791 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
1792 sgs[out_num++] = &hdr;
1793
1794 if (out)
1795 sgs[out_num++] = out;
1796
1797
1798 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
1799 sgs[out_num] = &stat;
1800
1801 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1802 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1803 if (ret < 0) {
1804 dev_warn(&vi->vdev->dev,
1805 "Failed to add sgs for command vq: %d\n.", ret);
1806 return false;
1807 }
1808
1809 if (unlikely(!virtqueue_kick(vi->cvq)))
1810 return vi->ctrl->status == VIRTIO_NET_OK;
1811
1812
1813
1814
1815 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1816 !virtqueue_is_broken(vi->cvq))
1817 cpu_relax();
1818
1819 return vi->ctrl->status == VIRTIO_NET_OK;
1820}
1821
1822static int virtnet_set_mac_address(struct net_device *dev, void *p)
1823{
1824 struct virtnet_info *vi = netdev_priv(dev);
1825 struct virtio_device *vdev = vi->vdev;
1826 int ret;
1827 struct sockaddr *addr;
1828 struct scatterlist sg;
1829
1830 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1831 return -EOPNOTSUPP;
1832
1833 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
1834 if (!addr)
1835 return -ENOMEM;
1836
1837 ret = eth_prepare_mac_addr_change(dev, addr);
1838 if (ret)
1839 goto out;
1840
1841 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1842 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1843 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1844 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
1845 dev_warn(&vdev->dev,
1846 "Failed to set mac address by vq command.\n");
1847 ret = -EINVAL;
1848 goto out;
1849 }
1850 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1851 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1852 unsigned int i;
1853
1854
1855 for (i = 0; i < dev->addr_len; i++)
1856 virtio_cwrite8(vdev,
1857 offsetof(struct virtio_net_config, mac) +
1858 i, addr->sa_data[i]);
1859 }
1860
1861 eth_commit_mac_addr_change(dev, p);
1862 ret = 0;
1863
1864out:
1865 kfree(addr);
1866 return ret;
1867}
1868
1869static void virtnet_stats(struct net_device *dev,
1870 struct rtnl_link_stats64 *tot)
1871{
1872 struct virtnet_info *vi = netdev_priv(dev);
1873 unsigned int start;
1874 int i;
1875
1876 for (i = 0; i < vi->max_queue_pairs; i++) {
1877 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
1878 struct receive_queue *rq = &vi->rq[i];
1879 struct send_queue *sq = &vi->sq[i];
1880
1881 do {
1882 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1883 tpackets = sq->stats.packets;
1884 tbytes = sq->stats.bytes;
1885 terrors = sq->stats.tx_timeouts;
1886 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
1887
1888 do {
1889 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
1890 rpackets = rq->stats.packets;
1891 rbytes = rq->stats.bytes;
1892 rdrops = rq->stats.drops;
1893 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
1894
1895 tot->rx_packets += rpackets;
1896 tot->tx_packets += tpackets;
1897 tot->rx_bytes += rbytes;
1898 tot->tx_bytes += tbytes;
1899 tot->rx_dropped += rdrops;
1900 tot->tx_errors += terrors;
1901 }
1902
1903 tot->tx_dropped = dev->stats.tx_dropped;
1904 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
1905 tot->rx_length_errors = dev->stats.rx_length_errors;
1906 tot->rx_frame_errors = dev->stats.rx_frame_errors;
1907}
1908
1909static void virtnet_ack_link_announce(struct virtnet_info *vi)
1910{
1911 rtnl_lock();
1912 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1913 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1914 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1915 rtnl_unlock();
1916}
1917
1918static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1919{
1920 struct scatterlist sg;
1921 struct net_device *dev = vi->dev;
1922
1923 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1924 return 0;
1925
1926 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1927 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
1928
1929 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1930 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1931 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1932 queue_pairs);
1933 return -EINVAL;
1934 } else {
1935 vi->curr_queue_pairs = queue_pairs;
1936
1937 if (dev->flags & IFF_UP)
1938 schedule_delayed_work(&vi->refill, 0);
1939 }
1940
1941 return 0;
1942}
1943
1944static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1945{
1946 int err;
1947
1948 rtnl_lock();
1949 err = _virtnet_set_queues(vi, queue_pairs);
1950 rtnl_unlock();
1951 return err;
1952}
1953
1954static int virtnet_close(struct net_device *dev)
1955{
1956 struct virtnet_info *vi = netdev_priv(dev);
1957 int i;
1958
1959
1960 cancel_delayed_work_sync(&vi->refill);
1961
1962 for (i = 0; i < vi->max_queue_pairs; i++) {
1963 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1964 napi_disable(&vi->rq[i].napi);
1965 virtnet_napi_tx_disable(&vi->sq[i].napi);
1966 }
1967
1968 return 0;
1969}
1970
1971static void virtnet_set_rx_mode(struct net_device *dev)
1972{
1973 struct virtnet_info *vi = netdev_priv(dev);
1974 struct scatterlist sg[2];
1975 struct virtio_net_ctrl_mac *mac_data;
1976 struct netdev_hw_addr *ha;
1977 int uc_count;
1978 int mc_count;
1979 void *buf;
1980 int i;
1981
1982
1983 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1984 return;
1985
1986 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
1987 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
1988
1989 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
1990
1991 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1992 VIRTIO_NET_CTRL_RX_PROMISC, sg))
1993 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1994 vi->ctrl->promisc ? "en" : "dis");
1995
1996 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
1997
1998 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1999 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2000 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2001 vi->ctrl->allmulti ? "en" : "dis");
2002
2003 uc_count = netdev_uc_count(dev);
2004 mc_count = netdev_mc_count(dev);
2005
2006 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2007 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
2008 mac_data = buf;
2009 if (!buf)
2010 return;
2011
2012 sg_init_table(sg, 2);
2013
2014
2015 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2016 i = 0;
2017 netdev_for_each_uc_addr(ha, dev)
2018 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2019
2020 sg_set_buf(&sg[0], mac_data,
2021 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2022
2023
2024 mac_data = (void *)&mac_data->macs[uc_count][0];
2025
2026 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2027 i = 0;
2028 netdev_for_each_mc_addr(ha, dev)
2029 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2030
2031 sg_set_buf(&sg[1], mac_data,
2032 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2033
2034 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2035 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
2036 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2037
2038 kfree(buf);
2039}
2040
2041static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2042 __be16 proto, u16 vid)
2043{
2044 struct virtnet_info *vi = netdev_priv(dev);
2045 struct scatterlist sg;
2046
2047 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2048 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2049
2050 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2051 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
2052 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
2053 return 0;
2054}
2055
2056static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2057 __be16 proto, u16 vid)
2058{
2059 struct virtnet_info *vi = netdev_priv(dev);
2060 struct scatterlist sg;
2061
2062 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2063 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2064
2065 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2066 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
2067 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
2068 return 0;
2069}
2070
2071static void virtnet_clean_affinity(struct virtnet_info *vi)
2072{
2073 int i;
2074
2075 if (vi->affinity_hint_set) {
2076 for (i = 0; i < vi->max_queue_pairs; i++) {
2077 virtqueue_set_affinity(vi->rq[i].vq, NULL);
2078 virtqueue_set_affinity(vi->sq[i].vq, NULL);
2079 }
2080
2081 vi->affinity_hint_set = false;
2082 }
2083}
2084
2085static void virtnet_set_affinity(struct virtnet_info *vi)
2086{
2087 cpumask_var_t mask;
2088 int stragglers;
2089 int group_size;
2090 int i, j, cpu;
2091 int num_cpu;
2092 int stride;
2093
2094 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2095 virtnet_clean_affinity(vi);
2096 return;
2097 }
2098
2099 num_cpu = num_online_cpus();
2100 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2101 stragglers = num_cpu >= vi->curr_queue_pairs ?
2102 num_cpu % vi->curr_queue_pairs :
2103 0;
2104 cpu = cpumask_first(cpu_online_mask);
2105
2106 for (i = 0; i < vi->curr_queue_pairs; i++) {
2107 group_size = stride + (i < stragglers ? 1 : 0);
2108
2109 for (j = 0; j < group_size; j++) {
2110 cpumask_set_cpu(cpu, mask);
2111 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2112 nr_cpu_ids, false);
2113 }
2114 virtqueue_set_affinity(vi->rq[i].vq, mask);
2115 virtqueue_set_affinity(vi->sq[i].vq, mask);
2116 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
2117 cpumask_clear(mask);
2118 }
2119
2120 vi->affinity_hint_set = true;
2121 free_cpumask_var(mask);
2122}
2123
2124static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
2125{
2126 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2127 node);
2128 virtnet_set_affinity(vi);
2129 return 0;
2130}
2131
2132static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2133{
2134 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2135 node_dead);
2136 virtnet_set_affinity(vi);
2137 return 0;
2138}
2139
2140static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2141{
2142 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2143 node);
2144
2145 virtnet_clean_affinity(vi);
2146 return 0;
2147}
2148
2149static enum cpuhp_state virtionet_online;
2150
2151static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2152{
2153 int ret;
2154
2155 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2156 if (ret)
2157 return ret;
2158 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2159 &vi->node_dead);
2160 if (!ret)
2161 return ret;
2162 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2163 return ret;
2164}
2165
2166static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2167{
2168 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2169 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2170 &vi->node_dead);
2171}
2172
2173static void virtnet_get_ringparam(struct net_device *dev,
2174 struct ethtool_ringparam *ring,
2175 struct kernel_ethtool_ringparam *kernel_ring,
2176 struct netlink_ext_ack *extack)
2177{
2178 struct virtnet_info *vi = netdev_priv(dev);
2179
2180 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2181 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2182 ring->rx_pending = ring->rx_max_pending;
2183 ring->tx_pending = ring->tx_max_pending;
2184}
2185
2186
2187static void virtnet_get_drvinfo(struct net_device *dev,
2188 struct ethtool_drvinfo *info)
2189{
2190 struct virtnet_info *vi = netdev_priv(dev);
2191 struct virtio_device *vdev = vi->vdev;
2192
2193 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2194 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2195 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
2196
2197}
2198
2199
2200static int virtnet_set_channels(struct net_device *dev,
2201 struct ethtool_channels *channels)
2202{
2203 struct virtnet_info *vi = netdev_priv(dev);
2204 u16 queue_pairs = channels->combined_count;
2205 int err;
2206
2207
2208
2209
2210 if (channels->rx_count || channels->tx_count || channels->other_count)
2211 return -EINVAL;
2212
2213 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
2214 return -EINVAL;
2215
2216
2217
2218
2219
2220 if (vi->rq[0].xdp_prog)
2221 return -EINVAL;
2222
2223 cpus_read_lock();
2224 err = _virtnet_set_queues(vi, queue_pairs);
2225 if (err) {
2226 cpus_read_unlock();
2227 goto err;
2228 }
2229 virtnet_set_affinity(vi);
2230 cpus_read_unlock();
2231
2232 netif_set_real_num_tx_queues(dev, queue_pairs);
2233 netif_set_real_num_rx_queues(dev, queue_pairs);
2234 err:
2235 return err;
2236}
2237
2238static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2239{
2240 struct virtnet_info *vi = netdev_priv(dev);
2241 unsigned int i, j;
2242 u8 *p = data;
2243
2244 switch (stringset) {
2245 case ETH_SS_STATS:
2246 for (i = 0; i < vi->curr_queue_pairs; i++) {
2247 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2248 ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2249 virtnet_rq_stats_desc[j].desc);
2250 }
2251
2252 for (i = 0; i < vi->curr_queue_pairs; i++) {
2253 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2254 ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2255 virtnet_sq_stats_desc[j].desc);
2256 }
2257 break;
2258 }
2259}
2260
2261static int virtnet_get_sset_count(struct net_device *dev, int sset)
2262{
2263 struct virtnet_info *vi = netdev_priv(dev);
2264
2265 switch (sset) {
2266 case ETH_SS_STATS:
2267 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2268 VIRTNET_SQ_STATS_LEN);
2269 default:
2270 return -EOPNOTSUPP;
2271 }
2272}
2273
2274static void virtnet_get_ethtool_stats(struct net_device *dev,
2275 struct ethtool_stats *stats, u64 *data)
2276{
2277 struct virtnet_info *vi = netdev_priv(dev);
2278 unsigned int idx = 0, start, i, j;
2279 const u8 *stats_base;
2280 size_t offset;
2281
2282 for (i = 0; i < vi->curr_queue_pairs; i++) {
2283 struct receive_queue *rq = &vi->rq[i];
2284
2285 stats_base = (u8 *)&rq->stats;
2286 do {
2287 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2288 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2289 offset = virtnet_rq_stats_desc[j].offset;
2290 data[idx + j] = *(u64 *)(stats_base + offset);
2291 }
2292 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2293 idx += VIRTNET_RQ_STATS_LEN;
2294 }
2295
2296 for (i = 0; i < vi->curr_queue_pairs; i++) {
2297 struct send_queue *sq = &vi->sq[i];
2298
2299 stats_base = (u8 *)&sq->stats;
2300 do {
2301 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2302 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2303 offset = virtnet_sq_stats_desc[j].offset;
2304 data[idx + j] = *(u64 *)(stats_base + offset);
2305 }
2306 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2307 idx += VIRTNET_SQ_STATS_LEN;
2308 }
2309}
2310
2311static void virtnet_get_channels(struct net_device *dev,
2312 struct ethtool_channels *channels)
2313{
2314 struct virtnet_info *vi = netdev_priv(dev);
2315
2316 channels->combined_count = vi->curr_queue_pairs;
2317 channels->max_combined = vi->max_queue_pairs;
2318 channels->max_other = 0;
2319 channels->rx_count = 0;
2320 channels->tx_count = 0;
2321 channels->other_count = 0;
2322}
2323
2324static int virtnet_set_link_ksettings(struct net_device *dev,
2325 const struct ethtool_link_ksettings *cmd)
2326{
2327 struct virtnet_info *vi = netdev_priv(dev);
2328
2329 return ethtool_virtdev_set_link_ksettings(dev, cmd,
2330 &vi->speed, &vi->duplex);
2331}
2332
2333static int virtnet_get_link_ksettings(struct net_device *dev,
2334 struct ethtool_link_ksettings *cmd)
2335{
2336 struct virtnet_info *vi = netdev_priv(dev);
2337
2338 cmd->base.speed = vi->speed;
2339 cmd->base.duplex = vi->duplex;
2340 cmd->base.port = PORT_OTHER;
2341
2342 return 0;
2343}
2344
2345static int virtnet_set_coalesce(struct net_device *dev,
2346 struct ethtool_coalesce *ec,
2347 struct kernel_ethtool_coalesce *kernel_coal,
2348 struct netlink_ext_ack *extack)
2349{
2350 struct virtnet_info *vi = netdev_priv(dev);
2351 int i, napi_weight;
2352
2353 if (ec->tx_max_coalesced_frames > 1 ||
2354 ec->rx_max_coalesced_frames != 1)
2355 return -EINVAL;
2356
2357 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
2358 if (napi_weight ^ vi->sq[0].napi.weight) {
2359 if (dev->flags & IFF_UP)
2360 return -EBUSY;
2361 for (i = 0; i < vi->max_queue_pairs; i++)
2362 vi->sq[i].napi.weight = napi_weight;
2363 }
2364
2365 return 0;
2366}
2367
2368static int virtnet_get_coalesce(struct net_device *dev,
2369 struct ethtool_coalesce *ec,
2370 struct kernel_ethtool_coalesce *kernel_coal,
2371 struct netlink_ext_ack *extack)
2372{
2373 struct ethtool_coalesce ec_default = {
2374 .cmd = ETHTOOL_GCOALESCE,
2375 .rx_max_coalesced_frames = 1,
2376 };
2377 struct virtnet_info *vi = netdev_priv(dev);
2378
2379 memcpy(ec, &ec_default, sizeof(ec_default));
2380
2381 if (vi->sq[0].napi.weight)
2382 ec->tx_max_coalesced_frames = 1;
2383
2384 return 0;
2385}
2386
2387static void virtnet_init_settings(struct net_device *dev)
2388{
2389 struct virtnet_info *vi = netdev_priv(dev);
2390
2391 vi->speed = SPEED_UNKNOWN;
2392 vi->duplex = DUPLEX_UNKNOWN;
2393}
2394
2395static void virtnet_update_settings(struct virtnet_info *vi)
2396{
2397 u32 speed;
2398 u8 duplex;
2399
2400 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2401 return;
2402
2403 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
2404
2405 if (ethtool_validate_speed(speed))
2406 vi->speed = speed;
2407
2408 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
2409
2410 if (ethtool_validate_duplex(duplex))
2411 vi->duplex = duplex;
2412}
2413
2414static const struct ethtool_ops virtnet_ethtool_ops = {
2415 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
2416 .get_drvinfo = virtnet_get_drvinfo,
2417 .get_link = ethtool_op_get_link,
2418 .get_ringparam = virtnet_get_ringparam,
2419 .get_strings = virtnet_get_strings,
2420 .get_sset_count = virtnet_get_sset_count,
2421 .get_ethtool_stats = virtnet_get_ethtool_stats,
2422 .set_channels = virtnet_set_channels,
2423 .get_channels = virtnet_get_channels,
2424 .get_ts_info = ethtool_op_get_ts_info,
2425 .get_link_ksettings = virtnet_get_link_ksettings,
2426 .set_link_ksettings = virtnet_set_link_ksettings,
2427 .set_coalesce = virtnet_set_coalesce,
2428 .get_coalesce = virtnet_get_coalesce,
2429};
2430
2431static void virtnet_freeze_down(struct virtio_device *vdev)
2432{
2433 struct virtnet_info *vi = vdev->priv;
2434 int i;
2435
2436
2437 flush_work(&vi->config_work);
2438
2439 netif_tx_lock_bh(vi->dev);
2440 netif_device_detach(vi->dev);
2441 netif_tx_unlock_bh(vi->dev);
2442 cancel_delayed_work_sync(&vi->refill);
2443
2444 if (netif_running(vi->dev)) {
2445 for (i = 0; i < vi->max_queue_pairs; i++) {
2446 napi_disable(&vi->rq[i].napi);
2447 virtnet_napi_tx_disable(&vi->sq[i].napi);
2448 }
2449 }
2450}
2451
2452static int init_vqs(struct virtnet_info *vi);
2453
2454static int virtnet_restore_up(struct virtio_device *vdev)
2455{
2456 struct virtnet_info *vi = vdev->priv;
2457 int err, i;
2458
2459 err = init_vqs(vi);
2460 if (err)
2461 return err;
2462
2463 virtio_device_ready(vdev);
2464
2465 if (netif_running(vi->dev)) {
2466 for (i = 0; i < vi->curr_queue_pairs; i++)
2467 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2468 schedule_delayed_work(&vi->refill, 0);
2469
2470 for (i = 0; i < vi->max_queue_pairs; i++) {
2471 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2472 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2473 &vi->sq[i].napi);
2474 }
2475 }
2476
2477 netif_tx_lock_bh(vi->dev);
2478 netif_device_attach(vi->dev);
2479 netif_tx_unlock_bh(vi->dev);
2480 return err;
2481}
2482
2483static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2484{
2485 struct scatterlist sg;
2486 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
2487
2488 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
2489
2490 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2491 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
2492 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
2493 return -EINVAL;
2494 }
2495
2496 return 0;
2497}
2498
2499static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2500{
2501 u64 offloads = 0;
2502
2503 if (!vi->guest_offloads)
2504 return 0;
2505
2506 return virtnet_set_guest_offloads(vi, offloads);
2507}
2508
2509static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2510{
2511 u64 offloads = vi->guest_offloads;
2512
2513 if (!vi->guest_offloads)
2514 return 0;
2515
2516 return virtnet_set_guest_offloads(vi, offloads);
2517}
2518
2519static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2520 struct netlink_ext_ack *extack)
2521{
2522 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2523 struct virtnet_info *vi = netdev_priv(dev);
2524 struct bpf_prog *old_prog;
2525 u16 xdp_qp = 0, curr_qp;
2526 int i, err;
2527
2528 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2529 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2530 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2531 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
2532 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
2533 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
2534 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
2535 return -EOPNOTSUPP;
2536 }
2537
2538 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
2539 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
2540 return -EINVAL;
2541 }
2542
2543 if (dev->mtu > max_sz) {
2544 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
2545 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2546 return -EINVAL;
2547 }
2548
2549 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2550 if (prog)
2551 xdp_qp = nr_cpu_ids;
2552
2553
2554 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
2555 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",
2556 curr_qp + xdp_qp, vi->max_queue_pairs);
2557 xdp_qp = 0;
2558 }
2559
2560 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
2561 if (!prog && !old_prog)
2562 return 0;
2563
2564 if (prog)
2565 bpf_prog_add(prog, vi->max_queue_pairs - 1);
2566
2567
2568 if (netif_running(dev)) {
2569 for (i = 0; i < vi->max_queue_pairs; i++) {
2570 napi_disable(&vi->rq[i].napi);
2571 virtnet_napi_tx_disable(&vi->sq[i].napi);
2572 }
2573 }
2574
2575 if (!prog) {
2576 for (i = 0; i < vi->max_queue_pairs; i++) {
2577 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2578 if (i == 0)
2579 virtnet_restore_guest_offloads(vi);
2580 }
2581 synchronize_net();
2582 }
2583
2584 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2585 if (err)
2586 goto err;
2587 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
2588 vi->xdp_queue_pairs = xdp_qp;
2589
2590 if (prog) {
2591 vi->xdp_enabled = true;
2592 for (i = 0; i < vi->max_queue_pairs; i++) {
2593 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2594 if (i == 0 && !old_prog)
2595 virtnet_clear_guest_offloads(vi);
2596 }
2597 } else {
2598 vi->xdp_enabled = false;
2599 }
2600
2601 for (i = 0; i < vi->max_queue_pairs; i++) {
2602 if (old_prog)
2603 bpf_prog_put(old_prog);
2604 if (netif_running(dev)) {
2605 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2606 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2607 &vi->sq[i].napi);
2608 }
2609 }
2610
2611 return 0;
2612
2613err:
2614 if (!prog) {
2615 virtnet_clear_guest_offloads(vi);
2616 for (i = 0; i < vi->max_queue_pairs; i++)
2617 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
2618 }
2619
2620 if (netif_running(dev)) {
2621 for (i = 0; i < vi->max_queue_pairs; i++) {
2622 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2623 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2624 &vi->sq[i].napi);
2625 }
2626 }
2627 if (prog)
2628 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2629 return err;
2630}
2631
2632static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2633{
2634 switch (xdp->command) {
2635 case XDP_SETUP_PROG:
2636 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
2637 default:
2638 return -EINVAL;
2639 }
2640}
2641
2642static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2643 size_t len)
2644{
2645 struct virtnet_info *vi = netdev_priv(dev);
2646 int ret;
2647
2648 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2649 return -EOPNOTSUPP;
2650
2651 ret = snprintf(buf, len, "sby");
2652 if (ret >= len)
2653 return -EOPNOTSUPP;
2654
2655 return 0;
2656}
2657
2658static int virtnet_set_features(struct net_device *dev,
2659 netdev_features_t features)
2660{
2661 struct virtnet_info *vi = netdev_priv(dev);
2662 u64 offloads;
2663 int err;
2664
2665 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
2666 if (vi->xdp_enabled)
2667 return -EBUSY;
2668
2669 if (features & NETIF_F_GRO_HW)
2670 offloads = vi->guest_offloads_capable;
2671 else
2672 offloads = vi->guest_offloads_capable &
2673 ~GUEST_OFFLOAD_GRO_HW_MASK;
2674
2675 err = virtnet_set_guest_offloads(vi, offloads);
2676 if (err)
2677 return err;
2678 vi->guest_offloads = offloads;
2679 }
2680
2681 return 0;
2682}
2683
2684static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
2685{
2686 struct virtnet_info *priv = netdev_priv(dev);
2687 struct send_queue *sq = &priv->sq[txqueue];
2688 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
2689
2690 u64_stats_update_begin(&sq->stats.syncp);
2691 sq->stats.tx_timeouts++;
2692 u64_stats_update_end(&sq->stats.syncp);
2693
2694 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
2695 txqueue, sq->name, sq->vq->index, sq->vq->name,
2696 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
2697}
2698
2699static const struct net_device_ops virtnet_netdev = {
2700 .ndo_open = virtnet_open,
2701 .ndo_stop = virtnet_close,
2702 .ndo_start_xmit = start_xmit,
2703 .ndo_validate_addr = eth_validate_addr,
2704 .ndo_set_mac_address = virtnet_set_mac_address,
2705 .ndo_set_rx_mode = virtnet_set_rx_mode,
2706 .ndo_get_stats64 = virtnet_stats,
2707 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
2708 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
2709 .ndo_bpf = virtnet_xdp,
2710 .ndo_xdp_xmit = virtnet_xdp_xmit,
2711 .ndo_features_check = passthru_features_check,
2712 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
2713 .ndo_set_features = virtnet_set_features,
2714 .ndo_tx_timeout = virtnet_tx_timeout,
2715};
2716
2717static void virtnet_config_changed_work(struct work_struct *work)
2718{
2719 struct virtnet_info *vi =
2720 container_of(work, struct virtnet_info, config_work);
2721 u16 v;
2722
2723 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2724 struct virtio_net_config, status, &v) < 0)
2725 return;
2726
2727 if (v & VIRTIO_NET_S_ANNOUNCE) {
2728 netdev_notify_peers(vi->dev);
2729 virtnet_ack_link_announce(vi);
2730 }
2731
2732
2733 v &= VIRTIO_NET_S_LINK_UP;
2734
2735 if (vi->status == v)
2736 return;
2737
2738 vi->status = v;
2739
2740 if (vi->status & VIRTIO_NET_S_LINK_UP) {
2741 virtnet_update_settings(vi);
2742 netif_carrier_on(vi->dev);
2743 netif_tx_wake_all_queues(vi->dev);
2744 } else {
2745 netif_carrier_off(vi->dev);
2746 netif_tx_stop_all_queues(vi->dev);
2747 }
2748}
2749
2750static void virtnet_config_changed(struct virtio_device *vdev)
2751{
2752 struct virtnet_info *vi = vdev->priv;
2753
2754 schedule_work(&vi->config_work);
2755}
2756
2757static void virtnet_free_queues(struct virtnet_info *vi)
2758{
2759 int i;
2760
2761 for (i = 0; i < vi->max_queue_pairs; i++) {
2762 __netif_napi_del(&vi->rq[i].napi);
2763 __netif_napi_del(&vi->sq[i].napi);
2764 }
2765
2766
2767
2768
2769 synchronize_net();
2770
2771 kfree(vi->rq);
2772 kfree(vi->sq);
2773 kfree(vi->ctrl);
2774}
2775
2776static void _free_receive_bufs(struct virtnet_info *vi)
2777{
2778 struct bpf_prog *old_prog;
2779 int i;
2780
2781 for (i = 0; i < vi->max_queue_pairs; i++) {
2782 while (vi->rq[i].pages)
2783 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
2784
2785 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2786 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2787 if (old_prog)
2788 bpf_prog_put(old_prog);
2789 }
2790}
2791
2792static void free_receive_bufs(struct virtnet_info *vi)
2793{
2794 rtnl_lock();
2795 _free_receive_bufs(vi);
2796 rtnl_unlock();
2797}
2798
2799static void free_receive_page_frags(struct virtnet_info *vi)
2800{
2801 int i;
2802 for (i = 0; i < vi->max_queue_pairs; i++)
2803 if (vi->rq[i].alloc_frag.page)
2804 put_page(vi->rq[i].alloc_frag.page);
2805}
2806
2807static void free_unused_bufs(struct virtnet_info *vi)
2808{
2809 void *buf;
2810 int i;
2811
2812 for (i = 0; i < vi->max_queue_pairs; i++) {
2813 struct virtqueue *vq = vi->sq[i].vq;
2814 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2815 if (!is_xdp_frame(buf))
2816 dev_kfree_skb(buf);
2817 else
2818 xdp_return_frame(ptr_to_xdp(buf));
2819 }
2820 }
2821
2822 for (i = 0; i < vi->max_queue_pairs; i++) {
2823 struct virtqueue *vq = vi->rq[i].vq;
2824
2825 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2826 if (vi->mergeable_rx_bufs) {
2827 put_page(virt_to_head_page(buf));
2828 } else if (vi->big_packets) {
2829 give_pages(&vi->rq[i], buf);
2830 } else {
2831 put_page(virt_to_head_page(buf));
2832 }
2833 }
2834 }
2835}
2836
2837static void virtnet_del_vqs(struct virtnet_info *vi)
2838{
2839 struct virtio_device *vdev = vi->vdev;
2840
2841 virtnet_clean_affinity(vi);
2842
2843 vdev->config->del_vqs(vdev);
2844
2845 virtnet_free_queues(vi);
2846}
2847
2848
2849
2850
2851
2852static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2853{
2854 const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2855 unsigned int rq_size = virtqueue_get_vring_size(vq);
2856 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2857 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2858 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2859
2860 return max(max(min_buf_len, hdr_len) - hdr_len,
2861 (unsigned int)GOOD_PACKET_LEN);
2862}
2863
2864static int virtnet_find_vqs(struct virtnet_info *vi)
2865{
2866 vq_callback_t **callbacks;
2867 struct virtqueue **vqs;
2868 int ret = -ENOMEM;
2869 int i, total_vqs;
2870 const char **names;
2871 bool *ctx;
2872
2873
2874
2875
2876
2877 total_vqs = vi->max_queue_pairs * 2 +
2878 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2879
2880
2881 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
2882 if (!vqs)
2883 goto err_vq;
2884 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
2885 if (!callbacks)
2886 goto err_callback;
2887 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
2888 if (!names)
2889 goto err_names;
2890 if (!vi->big_packets || vi->mergeable_rx_bufs) {
2891 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
2892 if (!ctx)
2893 goto err_ctx;
2894 } else {
2895 ctx = NULL;
2896 }
2897
2898
2899 if (vi->has_cvq) {
2900 callbacks[total_vqs - 1] = NULL;
2901 names[total_vqs - 1] = "control";
2902 }
2903
2904
2905 for (i = 0; i < vi->max_queue_pairs; i++) {
2906 callbacks[rxq2vq(i)] = skb_recv_done;
2907 callbacks[txq2vq(i)] = skb_xmit_done;
2908 sprintf(vi->rq[i].name, "input.%d", i);
2909 sprintf(vi->sq[i].name, "output.%d", i);
2910 names[rxq2vq(i)] = vi->rq[i].name;
2911 names[txq2vq(i)] = vi->sq[i].name;
2912 if (ctx)
2913 ctx[rxq2vq(i)] = true;
2914 }
2915
2916 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
2917 names, ctx, NULL);
2918 if (ret)
2919 goto err_find;
2920
2921 if (vi->has_cvq) {
2922 vi->cvq = vqs[total_vqs - 1];
2923 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
2924 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2925 }
2926
2927 for (i = 0; i < vi->max_queue_pairs; i++) {
2928 vi->rq[i].vq = vqs[rxq2vq(i)];
2929 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
2930 vi->sq[i].vq = vqs[txq2vq(i)];
2931 }
2932
2933
2934
2935
2936err_find:
2937 kfree(ctx);
2938err_ctx:
2939 kfree(names);
2940err_names:
2941 kfree(callbacks);
2942err_callback:
2943 kfree(vqs);
2944err_vq:
2945 return ret;
2946}
2947
2948static int virtnet_alloc_queues(struct virtnet_info *vi)
2949{
2950 int i;
2951
2952 if (vi->has_cvq) {
2953 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
2954 if (!vi->ctrl)
2955 goto err_ctrl;
2956 } else {
2957 vi->ctrl = NULL;
2958 }
2959 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
2960 if (!vi->sq)
2961 goto err_sq;
2962 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
2963 if (!vi->rq)
2964 goto err_rq;
2965
2966 INIT_DELAYED_WORK(&vi->refill, refill_work);
2967 for (i = 0; i < vi->max_queue_pairs; i++) {
2968 vi->rq[i].pages = NULL;
2969 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2970 napi_weight);
2971 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2972 napi_tx ? napi_weight : 0);
2973
2974 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
2975 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
2976 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2977
2978 u64_stats_init(&vi->rq[i].stats.syncp);
2979 u64_stats_init(&vi->sq[i].stats.syncp);
2980 }
2981
2982 return 0;
2983
2984err_rq:
2985 kfree(vi->sq);
2986err_sq:
2987 kfree(vi->ctrl);
2988err_ctrl:
2989 return -ENOMEM;
2990}
2991
2992static int init_vqs(struct virtnet_info *vi)
2993{
2994 int ret;
2995
2996
2997 ret = virtnet_alloc_queues(vi);
2998 if (ret)
2999 goto err;
3000
3001 ret = virtnet_find_vqs(vi);
3002 if (ret)
3003 goto err_free;
3004
3005 cpus_read_lock();
3006 virtnet_set_affinity(vi);
3007 cpus_read_unlock();
3008
3009 return 0;
3010
3011err_free:
3012 virtnet_free_queues(vi);
3013err:
3014 return ret;
3015}
3016
3017#ifdef CONFIG_SYSFS
3018static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
3019 char *buf)
3020{
3021 struct virtnet_info *vi = netdev_priv(queue->dev);
3022 unsigned int queue_index = get_netdev_rx_queue_index(queue);
3023 unsigned int headroom = virtnet_get_headroom(vi);
3024 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
3025 struct ewma_pkt_len *avg;
3026
3027 BUG_ON(queue_index >= vi->max_queue_pairs);
3028 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
3029 return sprintf(buf, "%u\n",
3030 get_mergeable_buf_len(&vi->rq[queue_index], avg,
3031 SKB_DATA_ALIGN(headroom + tailroom)));
3032}
3033
3034static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
3035 __ATTR_RO(mergeable_rx_buffer_size);
3036
3037static struct attribute *virtio_net_mrg_rx_attrs[] = {
3038 &mergeable_rx_buffer_size_attribute.attr,
3039 NULL
3040};
3041
3042static const struct attribute_group virtio_net_mrg_rx_group = {
3043 .name = "virtio_net",
3044 .attrs = virtio_net_mrg_rx_attrs
3045};
3046#endif
3047
3048static bool virtnet_fail_on_feature(struct virtio_device *vdev,
3049 unsigned int fbit,
3050 const char *fname, const char *dname)
3051{
3052 if (!virtio_has_feature(vdev, fbit))
3053 return false;
3054
3055 dev_err(&vdev->dev, "device advertises feature %s but not %s",
3056 fname, dname);
3057
3058 return true;
3059}
3060
3061#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
3062 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
3063
3064static bool virtnet_validate_features(struct virtio_device *vdev)
3065{
3066 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
3067 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
3068 "VIRTIO_NET_F_CTRL_VQ") ||
3069 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
3070 "VIRTIO_NET_F_CTRL_VQ") ||
3071 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
3072 "VIRTIO_NET_F_CTRL_VQ") ||
3073 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
3074 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
3075 "VIRTIO_NET_F_CTRL_VQ"))) {
3076 return false;
3077 }
3078
3079 return true;
3080}
3081
3082#define MIN_MTU ETH_MIN_MTU
3083#define MAX_MTU ETH_MAX_MTU
3084
3085static int virtnet_validate(struct virtio_device *vdev)
3086{
3087 if (!vdev->config->get) {
3088 dev_err(&vdev->dev, "%s failure: config access disabled\n",
3089 __func__);
3090 return -EINVAL;
3091 }
3092
3093 if (!virtnet_validate_features(vdev))
3094 return -EINVAL;
3095
3096 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3097 int mtu = virtio_cread16(vdev,
3098 offsetof(struct virtio_net_config,
3099 mtu));
3100 if (mtu < MIN_MTU)
3101 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3102 }
3103
3104 return 0;
3105}
3106
3107static int virtnet_probe(struct virtio_device *vdev)
3108{
3109 int i, err = -ENOMEM;
3110 struct net_device *dev;
3111 struct virtnet_info *vi;
3112 u16 max_queue_pairs;
3113 int mtu;
3114
3115
3116 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
3117 struct virtio_net_config,
3118 max_virtqueue_pairs, &max_queue_pairs);
3119
3120
3121 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3122 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3123 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3124 max_queue_pairs = 1;
3125
3126
3127 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
3128 if (!dev)
3129 return -ENOMEM;
3130
3131
3132 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3133 IFF_TX_SKB_NO_LINEAR;
3134 dev->netdev_ops = &virtnet_netdev;
3135 dev->features = NETIF_F_HIGHDMA;
3136
3137 dev->ethtool_ops = &virtnet_ethtool_ops;
3138 SET_NETDEV_DEV(dev, &vdev->dev);
3139
3140
3141 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
3142
3143 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3144 if (csum)
3145 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3146
3147 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
3148 dev->hw_features |= NETIF_F_TSO
3149 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
3150 }
3151
3152 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
3153 dev->hw_features |= NETIF_F_TSO;
3154 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
3155 dev->hw_features |= NETIF_F_TSO6;
3156 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
3157 dev->hw_features |= NETIF_F_TSO_ECN;
3158
3159 dev->features |= NETIF_F_GSO_ROBUST;
3160
3161 if (gso)
3162 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
3163
3164 }
3165 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
3166 dev->features |= NETIF_F_RXCSUM;
3167 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3168 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
3169 dev->features |= NETIF_F_GRO_HW;
3170 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
3171 dev->hw_features |= NETIF_F_GRO_HW;
3172
3173 dev->vlan_features = dev->features;
3174
3175
3176 dev->min_mtu = MIN_MTU;
3177 dev->max_mtu = MAX_MTU;
3178
3179
3180 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
3181 u8 addr[ETH_ALEN];
3182
3183 virtio_cread_bytes(vdev,
3184 offsetof(struct virtio_net_config, mac),
3185 addr, ETH_ALEN);
3186 eth_hw_addr_set(dev, addr);
3187 } else {
3188 eth_hw_addr_random(dev);
3189 }
3190
3191
3192 vi = netdev_priv(dev);
3193 vi->dev = dev;
3194 vi->vdev = vdev;
3195 vdev->priv = vi;
3196
3197 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
3198
3199
3200 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3201 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3202 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
3203 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
3204 vi->big_packets = true;
3205
3206 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
3207 vi->mergeable_rx_bufs = true;
3208
3209 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
3210 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3211 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3212 else
3213 vi->hdr_len = sizeof(struct virtio_net_hdr);
3214
3215 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
3216 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3217 vi->any_header_sg = true;
3218
3219 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3220 vi->has_cvq = true;
3221
3222 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3223 mtu = virtio_cread16(vdev,
3224 offsetof(struct virtio_net_config,
3225 mtu));
3226 if (mtu < dev->min_mtu) {
3227
3228
3229
3230 dev_err(&vdev->dev,
3231 "device MTU appears to have changed it is now %d < %d",
3232 mtu, dev->min_mtu);
3233 err = -EINVAL;
3234 goto free;
3235 }
3236
3237 dev->mtu = mtu;
3238 dev->max_mtu = mtu;
3239
3240
3241 if (dev->mtu > ETH_DATA_LEN)
3242 vi->big_packets = true;
3243 }
3244
3245 if (vi->any_header_sg)
3246 dev->needed_headroom = vi->hdr_len;
3247
3248
3249 if (num_online_cpus() >= max_queue_pairs)
3250 vi->curr_queue_pairs = max_queue_pairs;
3251 else
3252 vi->curr_queue_pairs = num_online_cpus();
3253 vi->max_queue_pairs = max_queue_pairs;
3254
3255
3256 err = init_vqs(vi);
3257 if (err)
3258 goto free;
3259
3260#ifdef CONFIG_SYSFS
3261 if (vi->mergeable_rx_bufs)
3262 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3263#endif
3264 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
3265 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
3266
3267 virtnet_init_settings(dev);
3268
3269 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3270 vi->failover = net_failover_create(vi->dev);
3271 if (IS_ERR(vi->failover)) {
3272 err = PTR_ERR(vi->failover);
3273 goto free_vqs;
3274 }
3275 }
3276
3277 err = register_netdev(dev);
3278 if (err) {
3279 pr_debug("virtio_net: registering device failed\n");
3280 goto free_failover;
3281 }
3282
3283 virtio_device_ready(vdev);
3284
3285 err = virtnet_cpu_notif_add(vi);
3286 if (err) {
3287 pr_debug("virtio_net: registering cpu notifier failed\n");
3288 goto free_unregister_netdev;
3289 }
3290
3291 virtnet_set_queues(vi, vi->curr_queue_pairs);
3292
3293
3294
3295 netif_carrier_off(dev);
3296 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
3297 schedule_work(&vi->config_work);
3298 } else {
3299 vi->status = VIRTIO_NET_S_LINK_UP;
3300 virtnet_update_settings(vi);
3301 netif_carrier_on(dev);
3302 }
3303
3304 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3305 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3306 set_bit(guest_offloads[i], &vi->guest_offloads);
3307 vi->guest_offloads_capable = vi->guest_offloads;
3308
3309 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3310 dev->name, max_queue_pairs);
3311
3312 return 0;
3313
3314free_unregister_netdev:
3315 virtio_reset_device(vdev);
3316
3317 unregister_netdev(dev);
3318free_failover:
3319 net_failover_destroy(vi->failover);
3320free_vqs:
3321 cancel_delayed_work_sync(&vi->refill);
3322 free_receive_page_frags(vi);
3323 virtnet_del_vqs(vi);
3324free:
3325 free_netdev(dev);
3326 return err;
3327}
3328
3329static void remove_vq_common(struct virtnet_info *vi)
3330{
3331 virtio_reset_device(vi->vdev);
3332
3333
3334 free_unused_bufs(vi);
3335
3336 free_receive_bufs(vi);
3337
3338 free_receive_page_frags(vi);
3339
3340 virtnet_del_vqs(vi);
3341}
3342
3343static void virtnet_remove(struct virtio_device *vdev)
3344{
3345 struct virtnet_info *vi = vdev->priv;
3346
3347 virtnet_cpu_notif_remove(vi);
3348
3349
3350 flush_work(&vi->config_work);
3351
3352 unregister_netdev(vi->dev);
3353
3354 net_failover_destroy(vi->failover);
3355
3356 remove_vq_common(vi);
3357
3358 free_netdev(vi->dev);
3359}
3360
3361static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
3362{
3363 struct virtnet_info *vi = vdev->priv;
3364
3365 virtnet_cpu_notif_remove(vi);
3366 virtnet_freeze_down(vdev);
3367 remove_vq_common(vi);
3368
3369 return 0;
3370}
3371
3372static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
3373{
3374 struct virtnet_info *vi = vdev->priv;
3375 int err;
3376
3377 err = virtnet_restore_up(vdev);
3378 if (err)
3379 return err;
3380 virtnet_set_queues(vi, vi->curr_queue_pairs);
3381
3382 err = virtnet_cpu_notif_add(vi);
3383 if (err) {
3384 virtnet_freeze_down(vdev);
3385 remove_vq_common(vi);
3386 return err;
3387 }
3388
3389 return 0;
3390}
3391
3392static struct virtio_device_id id_table[] = {
3393 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3394 { 0 },
3395};
3396
3397#define VIRTNET_FEATURES \
3398 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3399 VIRTIO_NET_F_MAC, \
3400 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3401 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3402 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3403 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3404 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3405 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3406 VIRTIO_NET_F_CTRL_MAC_ADDR, \
3407 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
3408 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
3409
3410static unsigned int features[] = {
3411 VIRTNET_FEATURES,
3412};
3413
3414static unsigned int features_legacy[] = {
3415 VIRTNET_FEATURES,
3416 VIRTIO_NET_F_GSO,
3417 VIRTIO_F_ANY_LAYOUT,
3418};
3419
3420static struct virtio_driver virtio_net_driver = {
3421 .feature_table = features,
3422 .feature_table_size = ARRAY_SIZE(features),
3423 .feature_table_legacy = features_legacy,
3424 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
3425 .driver.name = KBUILD_MODNAME,
3426 .driver.owner = THIS_MODULE,
3427 .id_table = id_table,
3428 .validate = virtnet_validate,
3429 .probe = virtnet_probe,
3430 .remove = virtnet_remove,
3431 .config_changed = virtnet_config_changed,
3432#ifdef CONFIG_PM_SLEEP
3433 .freeze = virtnet_freeze,
3434 .restore = virtnet_restore,
3435#endif
3436};
3437
3438static __init int virtio_net_driver_init(void)
3439{
3440 int ret;
3441
3442 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
3443 virtnet_cpu_online,
3444 virtnet_cpu_down_prep);
3445 if (ret < 0)
3446 goto out;
3447 virtionet_online = ret;
3448 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
3449 NULL, virtnet_cpu_dead);
3450 if (ret)
3451 goto err_dead;
3452
3453 ret = register_virtio_driver(&virtio_net_driver);
3454 if (ret)
3455 goto err_virtio;
3456 return 0;
3457err_virtio:
3458 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3459err_dead:
3460 cpuhp_remove_multi_state(virtionet_online);
3461out:
3462 return ret;
3463}
3464module_init(virtio_net_driver_init);
3465
3466static __exit void virtio_net_driver_exit(void)
3467{
3468 unregister_virtio_driver(&virtio_net_driver);
3469 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3470 cpuhp_remove_multi_state(virtionet_online);
3471}
3472module_exit(virtio_net_driver_exit);
3473
3474MODULE_DEVICE_TABLE(virtio, id_table);
3475MODULE_DESCRIPTION("Virtio network driver");
3476MODULE_LICENSE("GPL");
3477