1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* A network driver using virtio.
4 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
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>
24 #include <net/net_failover.h>
26 static int napi_weight = NAPI_POLL_WEIGHT;
27 module_param(napi_weight, int, 0444);
29 static bool csum = true, gso = true, napi_tx = true;
30 module_param(csum, bool, 0444);
31 module_param(gso, bool, 0444);
32 module_param(napi_tx, bool, 0644);
34 /* FIXME: MTU in config. */
35 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
36 #define GOOD_COPY_LEN 128
38 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
40 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
41 #define VIRTIO_XDP_HEADROOM 256
43 /* Separating two types of XDP xmit */
44 #define VIRTIO_XDP_TX BIT(0)
45 #define VIRTIO_XDP_REDIR BIT(1)
47 #define VIRTIO_XDP_FLAG BIT(0)
49 /* RX packet size EWMA. The average packet size is used to determine the packet
50 * buffer size when refilling RX rings. As the entire RX ring may be refilled
51 * at once, the weight is chosen so that the EWMA will be insensitive to short-
52 * term, transient changes in packet size.
54 DECLARE_EWMA(pkt_len, 0, 64)
56 #define VIRTNET_DRIVER_VERSION "1.0.0"
58 static 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
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))
71 struct virtnet_stat_desc {
72 char desc[ETH_GSTRING_LEN];
76 struct virtnet_sq_stats {
77 struct u64_stats_sync syncp;
86 struct virtnet_rq_stats {
87 struct u64_stats_sync syncp;
98 #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
99 #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
101 static 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) },
110 static 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) },
121 #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
122 #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
124 /* Internal representation of a send virtqueue */
126 /* Virtqueue associated with this send _queue */
127 struct virtqueue *vq;
129 /* TX: fragments + linear part + virtio header */
130 struct scatterlist sg[MAX_SKB_FRAGS + 2];
132 /* Name of the send queue: output.$index */
135 struct virtnet_sq_stats stats;
137 struct napi_struct napi;
140 /* Internal representation of a receive virtqueue */
141 struct receive_queue {
142 /* Virtqueue associated with this receive_queue */
143 struct virtqueue *vq;
145 struct napi_struct napi;
147 struct bpf_prog __rcu *xdp_prog;
149 struct virtnet_rq_stats stats;
151 /* Chain pages by the private ptr. */
154 /* Average packet length for mergeable receive buffers. */
155 struct ewma_pkt_len mrg_avg_pkt_len;
157 /* Page frag for packet buffer allocation. */
158 struct page_frag alloc_frag;
160 /* RX: fragments + linear part + virtio header */
161 struct scatterlist sg[MAX_SKB_FRAGS + 2];
163 /* Min single buffer size for mergeable buffers case. */
164 unsigned int min_buf_len;
166 /* Name of this receive queue: input.$index */
169 struct xdp_rxq_info xdp_rxq;
172 /* This structure can contain rss message with maximum settings for indirection table and keysize
173 * Note, that default structure that describes RSS configuration virtio_net_rss_config
174 * contains same info but can't handle table values.
175 * In any case, structure would be passed to virtio hw through sg_buf split by parts
176 * because table sizes may be differ according to the device configuration.
178 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
179 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
180 struct virtio_net_ctrl_rss {
182 u16 indirection_table_mask;
183 u16 unclassified_queue;
184 u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
187 u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
190 /* Control VQ buffers: protected by the rtnl lock */
192 struct virtio_net_ctrl_hdr hdr;
193 virtio_net_ctrl_ack status;
194 struct virtio_net_ctrl_mq mq;
199 struct virtio_net_ctrl_rss rss;
202 struct virtnet_info {
203 struct virtio_device *vdev;
204 struct virtqueue *cvq;
205 struct net_device *dev;
206 struct send_queue *sq;
207 struct receive_queue *rq;
210 /* Max # of queue pairs supported by the device */
213 /* # of queue pairs currently used by the driver */
214 u16 curr_queue_pairs;
216 /* # of XDP queue pairs currently used by the driver */
219 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
222 /* I like... big packets and I cannot lie! */
225 /* Host will merge rx buffers for big packets (shake it! shake it!) */
226 bool mergeable_rx_bufs;
228 /* Host supports rss and/or hash report */
230 bool has_rss_hash_report;
232 u16 rss_indir_table_size;
233 u32 rss_hash_types_supported;
234 u32 rss_hash_types_saved;
236 /* Has control virtqueue */
239 /* Host can handle any s/g split between our header and packet data */
242 /* Packet virtio header size */
245 /* Work struct for refilling if we run low on memory. */
246 struct delayed_work refill;
248 /* Work struct for config space updates */
249 struct work_struct config_work;
251 /* Does the affinity hint is set for virtqueues? */
252 bool affinity_hint_set;
254 /* CPU hotplug instances for online & dead */
255 struct hlist_node node;
256 struct hlist_node node_dead;
258 struct control_buf *ctrl;
260 /* Ethtool settings */
264 unsigned long guest_offloads;
265 unsigned long guest_offloads_capable;
267 /* failover when STANDBY feature enabled */
268 struct failover *failover;
271 struct padded_vnet_hdr {
272 struct virtio_net_hdr_v1_hash hdr;
274 * hdr is in a separate sg buffer, and data sg buffer shares same page
275 * with this header sg. This padding makes next sg 16 byte aligned
281 static bool is_xdp_frame(void *ptr)
283 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
286 static void *xdp_to_ptr(struct xdp_frame *ptr)
288 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
291 static struct xdp_frame *ptr_to_xdp(void *ptr)
293 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
296 /* Converting between virtqueue no. and kernel tx/rx queue no.
297 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
299 static int vq2txq(struct virtqueue *vq)
301 return (vq->index - 1) / 2;
304 static int txq2vq(int txq)
309 static int vq2rxq(struct virtqueue *vq)
311 return vq->index / 2;
314 static int rxq2vq(int rxq)
319 static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
321 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
325 * private is used to chain pages for big packets, put the whole
326 * most recent used list in the beginning for reuse
328 static void give_pages(struct receive_queue *rq, struct page *page)
332 /* Find end of list, sew whole thing into vi->rq.pages. */
333 for (end = page; end->private; end = (struct page *)end->private);
334 end->private = (unsigned long)rq->pages;
338 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
340 struct page *p = rq->pages;
343 rq->pages = (struct page *)p->private;
344 /* clear private here, it is used to chain pages */
347 p = alloc_page(gfp_mask);
351 static void virtqueue_napi_schedule(struct napi_struct *napi,
352 struct virtqueue *vq)
354 if (napi_schedule_prep(napi)) {
355 virtqueue_disable_cb(vq);
356 __napi_schedule(napi);
360 static void virtqueue_napi_complete(struct napi_struct *napi,
361 struct virtqueue *vq, int processed)
365 opaque = virtqueue_enable_cb_prepare(vq);
366 if (napi_complete_done(napi, processed)) {
367 if (unlikely(virtqueue_poll(vq, opaque)))
368 virtqueue_napi_schedule(napi, vq);
370 virtqueue_disable_cb(vq);
374 static void skb_xmit_done(struct virtqueue *vq)
376 struct virtnet_info *vi = vq->vdev->priv;
377 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
379 /* Suppress further interrupts. */
380 virtqueue_disable_cb(vq);
383 virtqueue_napi_schedule(napi, vq);
385 /* We were probably waiting for more output buffers. */
386 netif_wake_subqueue(vi->dev, vq2txq(vq));
389 #define MRG_CTX_HEADER_SHIFT 22
390 static void *mergeable_len_to_ctx(unsigned int truesize,
391 unsigned int headroom)
393 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
396 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
398 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
401 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
403 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
406 /* Called from bottom half context */
407 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
408 struct receive_queue *rq,
409 struct page *page, unsigned int offset,
410 unsigned int len, unsigned int truesize,
411 bool hdr_valid, unsigned int metasize,
412 unsigned int headroom)
415 struct virtio_net_hdr_mrg_rxbuf *hdr;
416 unsigned int copy, hdr_len, hdr_padded_len;
417 struct page *page_to_free = NULL;
418 int tailroom, shinfo_size;
419 char *p, *hdr_p, *buf;
421 p = page_address(page) + offset;
424 hdr_len = vi->hdr_len;
425 if (vi->mergeable_rx_bufs)
426 hdr_padded_len = hdr_len;
428 hdr_padded_len = sizeof(struct padded_vnet_hdr);
430 /* If headroom is not 0, there is an offset between the beginning of the
431 * data and the allocated space, otherwise the data and the allocated
434 * Buffers with headroom use PAGE_SIZE as alloc size, see
435 * add_recvbuf_mergeable() + get_mergeable_buf_len()
437 truesize = headroom ? PAGE_SIZE : truesize;
438 tailroom = truesize - headroom;
442 offset += hdr_padded_len;
444 tailroom -= hdr_padded_len + len;
446 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
448 /* copy small packet so we can reuse these pages */
449 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
450 skb = build_skb(buf, truesize);
454 skb_reserve(skb, p - buf);
457 page = (struct page *)page->private;
459 give_pages(rq, page);
463 /* copy small packet so we can reuse these pages for small data */
464 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
468 /* Copy all frame if it fits skb->head, otherwise
469 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
471 if (len <= skb_tailroom(skb))
474 copy = ETH_HLEN + metasize;
475 skb_put_data(skb, p, copy);
480 if (vi->mergeable_rx_bufs) {
482 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
489 * Verify that we can indeed put this data into a skb.
490 * This is here to handle cases when the device erroneously
491 * tries to receive more than is possible. This is usually
492 * the case of a broken device.
494 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
495 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
499 BUG_ON(offset >= PAGE_SIZE);
501 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
502 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
503 frag_size, truesize);
505 page = (struct page *)page->private;
510 give_pages(rq, page);
513 /* hdr_valid means no XDP, so we can copy the vnet header */
515 hdr = skb_vnet_hdr(skb);
516 memcpy(hdr, hdr_p, hdr_len);
519 put_page(page_to_free);
522 __skb_pull(skb, metasize);
523 skb_metadata_set(skb, metasize);
529 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
530 struct send_queue *sq,
531 struct xdp_frame *xdpf)
533 struct virtio_net_hdr_mrg_rxbuf *hdr;
536 if (unlikely(xdpf->headroom < vi->hdr_len))
539 /* Make room for virtqueue hdr (also change xdpf->headroom?) */
540 xdpf->data -= vi->hdr_len;
541 /* Zero header and leave csum up to XDP layers */
543 memset(hdr, 0, vi->hdr_len);
544 xdpf->len += vi->hdr_len;
546 sg_init_one(sq->sg, xdpf->data, xdpf->len);
548 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
551 return -ENOSPC; /* Caller handle free/refcnt */
556 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
557 * the current cpu, so it does not need to be locked.
559 * Here we use marco instead of inline functions because we have to deal with
560 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
561 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
562 * functions to perfectly solve these three problems at the same time.
564 #define virtnet_xdp_get_sq(vi) ({ \
565 int cpu = smp_processor_id(); \
566 struct netdev_queue *txq; \
567 typeof(vi) v = (vi); \
570 if (v->curr_queue_pairs > nr_cpu_ids) { \
571 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
573 txq = netdev_get_tx_queue(v->dev, qp); \
574 __netif_tx_acquire(txq); \
576 qp = cpu % v->curr_queue_pairs; \
577 txq = netdev_get_tx_queue(v->dev, qp); \
578 __netif_tx_lock(txq, cpu); \
583 #define virtnet_xdp_put_sq(vi, q) { \
584 struct netdev_queue *txq; \
585 typeof(vi) v = (vi); \
587 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
588 if (v->curr_queue_pairs > nr_cpu_ids) \
589 __netif_tx_release(txq); \
591 __netif_tx_unlock(txq); \
594 static int virtnet_xdp_xmit(struct net_device *dev,
595 int n, struct xdp_frame **frames, u32 flags)
597 struct virtnet_info *vi = netdev_priv(dev);
598 struct receive_queue *rq = vi->rq;
599 struct bpf_prog *xdp_prog;
600 struct send_queue *sq;
610 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
611 * indicate XDP resources have been successfully allocated.
613 xdp_prog = rcu_access_pointer(rq->xdp_prog);
617 sq = virtnet_xdp_get_sq(vi);
619 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
624 /* Free up any pending old buffers before queueing new ones. */
625 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
626 if (likely(is_xdp_frame(ptr))) {
627 struct xdp_frame *frame = ptr_to_xdp(ptr);
630 xdp_return_frame(frame);
632 struct sk_buff *skb = ptr;
635 napi_consume_skb(skb, false);
640 for (i = 0; i < n; i++) {
641 struct xdp_frame *xdpf = frames[i];
643 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
649 if (flags & XDP_XMIT_FLUSH) {
650 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
654 u64_stats_update_begin(&sq->stats.syncp);
655 sq->stats.bytes += bytes;
656 sq->stats.packets += packets;
657 sq->stats.xdp_tx += n;
658 sq->stats.xdp_tx_drops += n - nxmit;
659 sq->stats.kicks += kicks;
660 u64_stats_update_end(&sq->stats.syncp);
662 virtnet_xdp_put_sq(vi, sq);
666 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
668 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
671 /* We copy the packet for XDP in the following cases:
673 * 1) Packet is scattered across multiple rx buffers.
674 * 2) Headroom space is insufficient.
676 * This is inefficient but it's a temporary condition that
677 * we hit right after XDP is enabled and until queue is refilled
678 * with large buffers with sufficient headroom - so it should affect
679 * at most queue size packets.
680 * Afterwards, the conditions to enable
681 * XDP should preclude the underlying device from sending packets
682 * across multiple buffers (num_buf > 1), and we make sure buffers
683 * have enough headroom.
685 static struct page *xdp_linearize_page(struct receive_queue *rq,
692 struct page *page = alloc_page(GFP_ATOMIC);
697 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
701 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
706 buf = virtqueue_get_buf(rq->vq, &buflen);
710 p = virt_to_head_page(buf);
711 off = buf - page_address(p);
713 /* guard against a misconfigured or uncooperative backend that
714 * is sending packet larger than the MTU.
716 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
721 memcpy(page_address(page) + page_off,
722 page_address(p) + off, buflen);
727 /* Headroom does not contribute to packet length */
728 *len = page_off - VIRTIO_XDP_HEADROOM;
731 __free_pages(page, 0);
735 static struct sk_buff *receive_small(struct net_device *dev,
736 struct virtnet_info *vi,
737 struct receive_queue *rq,
738 void *buf, void *ctx,
740 unsigned int *xdp_xmit,
741 struct virtnet_rq_stats *stats)
744 struct bpf_prog *xdp_prog;
745 unsigned int xdp_headroom = (unsigned long)ctx;
746 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
747 unsigned int headroom = vi->hdr_len + header_offset;
748 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
749 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
750 struct page *page = virt_to_head_page(buf);
751 unsigned int delta = 0;
752 struct page *xdp_page;
754 unsigned int metasize = 0;
759 if (unlikely(len > GOOD_PACKET_LEN)) {
760 pr_debug("%s: rx error: len %u exceeds max size %d\n",
761 dev->name, len, GOOD_PACKET_LEN);
762 dev->stats.rx_length_errors++;
766 if (likely(!vi->xdp_enabled)) {
772 xdp_prog = rcu_dereference(rq->xdp_prog);
774 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
775 struct xdp_frame *xdpf;
780 if (unlikely(hdr->hdr.gso_type))
783 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
784 int offset = buf - page_address(page) + header_offset;
785 unsigned int tlen = len + vi->hdr_len;
788 xdp_headroom = virtnet_get_headroom(vi);
789 header_offset = VIRTNET_RX_PAD + xdp_headroom;
790 headroom = vi->hdr_len + header_offset;
791 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
792 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
793 xdp_page = xdp_linearize_page(rq, &num_buf, page,
794 offset, header_offset,
799 buf = page_address(xdp_page);
804 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
805 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
806 xdp_headroom, len, true);
807 orig_data = xdp.data;
808 act = bpf_prog_run_xdp(xdp_prog, &xdp);
809 stats->xdp_packets++;
813 /* Recalculate length in case bpf program changed it */
814 delta = orig_data - xdp.data;
815 len = xdp.data_end - xdp.data;
816 metasize = xdp.data - xdp.data_meta;
820 xdpf = xdp_convert_buff_to_frame(&xdp);
823 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
824 if (unlikely(!err)) {
825 xdp_return_frame_rx_napi(xdpf);
826 } else if (unlikely(err < 0)) {
827 trace_xdp_exception(vi->dev, xdp_prog, act);
830 *xdp_xmit |= VIRTIO_XDP_TX;
834 stats->xdp_redirects++;
835 err = xdp_do_redirect(dev, &xdp, xdp_prog);
838 *xdp_xmit |= VIRTIO_XDP_REDIR;
842 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
845 trace_xdp_exception(vi->dev, xdp_prog, act);
854 skb = build_skb(buf, buflen);
857 skb_reserve(skb, headroom - delta);
860 buf += header_offset;
861 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
862 } /* keep zeroed vnet hdr since XDP is loaded */
865 skb_metadata_set(skb, metasize);
879 static struct sk_buff *receive_big(struct net_device *dev,
880 struct virtnet_info *vi,
881 struct receive_queue *rq,
884 struct virtnet_rq_stats *stats)
886 struct page *page = buf;
887 struct sk_buff *skb =
888 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0);
890 stats->bytes += len - vi->hdr_len;
898 give_pages(rq, page);
902 static struct sk_buff *receive_mergeable(struct net_device *dev,
903 struct virtnet_info *vi,
904 struct receive_queue *rq,
908 unsigned int *xdp_xmit,
909 struct virtnet_rq_stats *stats)
911 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
912 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
913 struct page *page = virt_to_head_page(buf);
914 int offset = buf - page_address(page);
915 struct sk_buff *head_skb, *curr_skb;
916 struct bpf_prog *xdp_prog;
917 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
918 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
919 unsigned int metasize = 0;
920 unsigned int frame_sz;
924 stats->bytes += len - vi->hdr_len;
926 if (unlikely(len > truesize)) {
927 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
928 dev->name, len, (unsigned long)ctx);
929 dev->stats.rx_length_errors++;
933 if (likely(!vi->xdp_enabled)) {
939 xdp_prog = rcu_dereference(rq->xdp_prog);
941 struct xdp_frame *xdpf;
942 struct page *xdp_page;
947 /* Transient failure which in theory could occur if
948 * in-flight packets from before XDP was enabled reach
949 * the receive path after XDP is loaded.
951 if (unlikely(hdr->hdr.gso_type))
954 /* Buffers with headroom use PAGE_SIZE as alloc size,
955 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
957 frame_sz = headroom ? PAGE_SIZE : truesize;
959 /* This happens when rx buffer size is underestimated
960 * or headroom is not enough because of the buffer
961 * was refilled before XDP is set. This should only
962 * happen for the first several packets, so we don't
963 * care much about its performance.
965 if (unlikely(num_buf > 1 ||
966 headroom < virtnet_get_headroom(vi))) {
967 /* linearize data for XDP */
968 xdp_page = xdp_linearize_page(rq, &num_buf,
972 frame_sz = PAGE_SIZE;
976 offset = VIRTIO_XDP_HEADROOM;
981 /* Allow consuming headroom but reserve enough space to push
982 * the descriptor on if we get an XDP_TX return code.
984 data = page_address(xdp_page) + offset;
985 xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq);
986 xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len,
987 VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true);
989 act = bpf_prog_run_xdp(xdp_prog, &xdp);
990 stats->xdp_packets++;
994 metasize = xdp.data - xdp.data_meta;
996 /* recalculate offset to account for any header
997 * adjustments and minus the metasize to copy the
998 * metadata in page_to_skb(). Note other cases do not
999 * build an skb and avoid using offset
1001 offset = xdp.data - page_address(xdp_page) -
1002 vi->hdr_len - metasize;
1004 /* recalculate len if xdp.data, xdp.data_end or
1005 * xdp.data_meta were adjusted
1007 len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
1009 /* recalculate headroom if xdp.data or xdp_data_meta
1010 * were adjusted, note that offset should always point
1011 * to the start of the reserved bytes for virtio_net
1012 * header which are followed by xdp.data, that means
1013 * that offset is equal to the headroom (when buf is
1014 * starting at the beginning of the page, otherwise
1015 * there is a base offset inside the page) but it's used
1016 * with a different starting point (buf start) than
1017 * xdp.data (buf start + vnet hdr size). If xdp.data or
1018 * data_meta were adjusted by the xdp prog then the
1019 * headroom size has changed and so has the offset, we
1020 * can use data_hard_start, which points at buf start +
1021 * vnet hdr size, to calculate the new headroom and use
1022 * it later to compute buf start in page_to_skb()
1024 headroom = xdp.data - xdp.data_hard_start - metasize;
1026 /* We can only create skb based on xdp_page. */
1027 if (unlikely(xdp_page != page)) {
1030 head_skb = page_to_skb(vi, rq, xdp_page, offset,
1031 len, PAGE_SIZE, false,
1039 xdpf = xdp_convert_buff_to_frame(&xdp);
1040 if (unlikely(!xdpf))
1042 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1043 if (unlikely(!err)) {
1044 xdp_return_frame_rx_napi(xdpf);
1045 } else if (unlikely(err < 0)) {
1046 trace_xdp_exception(vi->dev, xdp_prog, act);
1047 if (unlikely(xdp_page != page))
1051 *xdp_xmit |= VIRTIO_XDP_TX;
1052 if (unlikely(xdp_page != page))
1057 stats->xdp_redirects++;
1058 err = xdp_do_redirect(dev, &xdp, xdp_prog);
1060 if (unlikely(xdp_page != page))
1064 *xdp_xmit |= VIRTIO_XDP_REDIR;
1065 if (unlikely(xdp_page != page))
1070 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
1073 trace_xdp_exception(vi->dev, xdp_prog, act);
1076 if (unlikely(xdp_page != page))
1077 __free_pages(xdp_page, 0);
1084 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
1085 metasize, headroom);
1086 curr_skb = head_skb;
1088 if (unlikely(!curr_skb))
1093 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
1094 if (unlikely(!buf)) {
1095 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1097 virtio16_to_cpu(vi->vdev,
1099 dev->stats.rx_length_errors++;
1103 stats->bytes += len;
1104 page = virt_to_head_page(buf);
1106 truesize = mergeable_ctx_to_truesize(ctx);
1107 if (unlikely(len > truesize)) {
1108 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1109 dev->name, len, (unsigned long)ctx);
1110 dev->stats.rx_length_errors++;
1114 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
1115 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1116 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
1118 if (unlikely(!nskb))
1120 if (curr_skb == head_skb)
1121 skb_shinfo(curr_skb)->frag_list = nskb;
1123 curr_skb->next = nskb;
1125 head_skb->truesize += nskb->truesize;
1128 if (curr_skb != head_skb) {
1129 head_skb->data_len += len;
1130 head_skb->len += len;
1131 head_skb->truesize += truesize;
1133 offset = buf - page_address(page);
1134 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1136 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1139 skb_add_rx_frag(curr_skb, num_skb_frags, page,
1140 offset, len, truesize);
1144 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
1152 while (num_buf-- > 1) {
1153 buf = virtqueue_get_buf(rq->vq, &len);
1154 if (unlikely(!buf)) {
1155 pr_debug("%s: rx error: %d buffers missing\n",
1156 dev->name, num_buf);
1157 dev->stats.rx_length_errors++;
1160 stats->bytes += len;
1161 page = virt_to_head_page(buf);
1166 dev_kfree_skb(head_skb);
1171 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
1172 struct sk_buff *skb)
1174 enum pkt_hash_types rss_hash_type;
1176 if (!hdr_hash || !skb)
1179 switch ((int)hdr_hash->hash_report) {
1180 case VIRTIO_NET_HASH_REPORT_TCPv4:
1181 case VIRTIO_NET_HASH_REPORT_UDPv4:
1182 case VIRTIO_NET_HASH_REPORT_TCPv6:
1183 case VIRTIO_NET_HASH_REPORT_UDPv6:
1184 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
1185 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
1186 rss_hash_type = PKT_HASH_TYPE_L4;
1188 case VIRTIO_NET_HASH_REPORT_IPv4:
1189 case VIRTIO_NET_HASH_REPORT_IPv6:
1190 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
1191 rss_hash_type = PKT_HASH_TYPE_L3;
1193 case VIRTIO_NET_HASH_REPORT_NONE:
1195 rss_hash_type = PKT_HASH_TYPE_NONE;
1197 skb_set_hash(skb, (unsigned int)hdr_hash->hash_value, rss_hash_type);
1200 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1201 void *buf, unsigned int len, void **ctx,
1202 unsigned int *xdp_xmit,
1203 struct virtnet_rq_stats *stats)
1205 struct net_device *dev = vi->dev;
1206 struct sk_buff *skb;
1207 struct virtio_net_hdr_mrg_rxbuf *hdr;
1209 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
1210 pr_debug("%s: short packet %i\n", dev->name, len);
1211 dev->stats.rx_length_errors++;
1212 if (vi->mergeable_rx_bufs) {
1213 put_page(virt_to_head_page(buf));
1214 } else if (vi->big_packets) {
1215 give_pages(rq, buf);
1217 put_page(virt_to_head_page(buf));
1222 if (vi->mergeable_rx_bufs)
1223 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1225 else if (vi->big_packets)
1226 skb = receive_big(dev, vi, rq, buf, len, stats);
1228 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1233 hdr = skb_vnet_hdr(skb);
1234 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
1235 virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
1237 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
1238 skb->ip_summed = CHECKSUM_UNNECESSARY;
1240 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1241 virtio_is_little_endian(vi->vdev))) {
1242 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1243 dev->name, hdr->hdr.gso_type,
1248 skb_record_rx_queue(skb, vq2rxq(rq->vq));
1249 skb->protocol = eth_type_trans(skb, dev);
1250 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1251 ntohs(skb->protocol), skb->len, skb->pkt_type);
1253 napi_gro_receive(&rq->napi, skb);
1257 dev->stats.rx_frame_errors++;
1261 /* Unlike mergeable buffers, all buffers are allocated to the
1262 * same size, except for the headroom. For this reason we do
1263 * not need to use mergeable_len_to_ctx here - it is enough
1264 * to store the headroom as the context ignoring the truesize.
1266 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1269 struct page_frag *alloc_frag = &rq->alloc_frag;
1271 unsigned int xdp_headroom = virtnet_get_headroom(vi);
1272 void *ctx = (void *)(unsigned long)xdp_headroom;
1273 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
1276 len = SKB_DATA_ALIGN(len) +
1277 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1278 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
1281 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1282 get_page(alloc_frag->page);
1283 alloc_frag->offset += len;
1284 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1285 vi->hdr_len + GOOD_PACKET_LEN);
1286 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1288 put_page(virt_to_head_page(buf));
1292 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1295 struct page *first, *list = NULL;
1299 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1301 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
1302 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
1303 first = get_a_page(rq, gfp);
1306 give_pages(rq, list);
1309 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
1311 /* chain new page in list head to match sg */
1312 first->private = (unsigned long)list;
1316 first = get_a_page(rq, gfp);
1318 give_pages(rq, list);
1321 p = page_address(first);
1323 /* rq->sg[0], rq->sg[1] share the same page */
1324 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1325 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
1327 /* rq->sg[1] for data packet, from offset */
1328 offset = sizeof(struct padded_vnet_hdr);
1329 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
1331 /* chain first in list head */
1332 first->private = (unsigned long)list;
1333 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1336 give_pages(rq, first);
1341 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
1342 struct ewma_pkt_len *avg_pkt_len,
1345 struct virtnet_info *vi = rq->vq->vdev->priv;
1346 const size_t hdr_len = vi->hdr_len;
1350 return PAGE_SIZE - room;
1352 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1353 rq->min_buf_len, PAGE_SIZE - hdr_len);
1355 return ALIGN(len, L1_CACHE_BYTES);
1358 static int add_recvbuf_mergeable(struct virtnet_info *vi,
1359 struct receive_queue *rq, gfp_t gfp)
1361 struct page_frag *alloc_frag = &rq->alloc_frag;
1362 unsigned int headroom = virtnet_get_headroom(vi);
1363 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1364 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1368 unsigned int len, hole;
1370 /* Extra tailroom is needed to satisfy XDP's assumption. This
1371 * means rx frags coalescing won't work, but consider we've
1372 * disabled GSO for XDP, it won't be a big issue.
1374 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1375 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
1378 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1379 buf += headroom; /* advance address leaving hole at front of pkt */
1380 get_page(alloc_frag->page);
1381 alloc_frag->offset += len + room;
1382 hole = alloc_frag->size - alloc_frag->offset;
1383 if (hole < len + room) {
1384 /* To avoid internal fragmentation, if there is very likely not
1385 * enough space for another buffer, add the remaining space to
1386 * the current buffer.
1389 alloc_frag->offset += hole;
1392 sg_init_one(rq->sg, buf, len);
1393 ctx = mergeable_len_to_ctx(len, headroom);
1394 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1396 put_page(virt_to_head_page(buf));
1402 * Returns false if we couldn't fill entirely (OOM).
1404 * Normally run in the receive path, but can also be run from ndo_open
1405 * before we're receiving packets, or from refill_work which is
1406 * careful to disable receiving (using napi_disable).
1408 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1415 if (vi->mergeable_rx_bufs)
1416 err = add_recvbuf_mergeable(vi, rq, gfp);
1417 else if (vi->big_packets)
1418 err = add_recvbuf_big(vi, rq, gfp);
1420 err = add_recvbuf_small(vi, rq, gfp);
1422 oom = err == -ENOMEM;
1425 } while (rq->vq->num_free);
1426 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1427 unsigned long flags;
1429 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
1431 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
1437 static void skb_recv_done(struct virtqueue *rvq)
1439 struct virtnet_info *vi = rvq->vdev->priv;
1440 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1442 virtqueue_napi_schedule(&rq->napi, rvq);
1445 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
1449 /* If all buffers were filled by other side before we napi_enabled, we
1450 * won't get another interrupt, so process any outstanding packets now.
1451 * Call local_bh_enable after to trigger softIRQ processing.
1454 virtqueue_napi_schedule(napi, vq);
1458 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1459 struct virtqueue *vq,
1460 struct napi_struct *napi)
1465 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1466 * enable the feature if this is likely affine with the transmit path.
1468 if (!vi->affinity_hint_set) {
1473 return virtnet_napi_enable(vq, napi);
1476 static void virtnet_napi_tx_disable(struct napi_struct *napi)
1482 static void refill_work(struct work_struct *work)
1484 struct virtnet_info *vi =
1485 container_of(work, struct virtnet_info, refill.work);
1489 for (i = 0; i < vi->curr_queue_pairs; i++) {
1490 struct receive_queue *rq = &vi->rq[i];
1492 napi_disable(&rq->napi);
1493 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1494 virtnet_napi_enable(rq->vq, &rq->napi);
1496 /* In theory, this can happen: if we don't get any buffers in
1497 * we will *never* try to fill again.
1500 schedule_delayed_work(&vi->refill, HZ/2);
1504 static int virtnet_receive(struct receive_queue *rq, int budget,
1505 unsigned int *xdp_xmit)
1507 struct virtnet_info *vi = rq->vq->vdev->priv;
1508 struct virtnet_rq_stats stats = {};
1513 if (!vi->big_packets || vi->mergeable_rx_bufs) {
1516 while (stats.packets < budget &&
1517 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1518 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1522 while (stats.packets < budget &&
1523 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1524 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1529 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
1530 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
1531 schedule_delayed_work(&vi->refill, 0);
1534 u64_stats_update_begin(&rq->stats.syncp);
1535 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1536 size_t offset = virtnet_rq_stats_desc[i].offset;
1539 item = (u64 *)((u8 *)&rq->stats + offset);
1540 *item += *(u64 *)((u8 *)&stats + offset);
1542 u64_stats_update_end(&rq->stats.syncp);
1544 return stats.packets;
1547 static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
1550 unsigned int packets = 0;
1551 unsigned int bytes = 0;
1554 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1555 if (likely(!is_xdp_frame(ptr))) {
1556 struct sk_buff *skb = ptr;
1558 pr_debug("Sent skb %p\n", skb);
1561 napi_consume_skb(skb, in_napi);
1563 struct xdp_frame *frame = ptr_to_xdp(ptr);
1565 bytes += frame->len;
1566 xdp_return_frame(frame);
1571 /* Avoid overhead when no packets have been processed
1572 * happens when called speculatively from start_xmit.
1577 u64_stats_update_begin(&sq->stats.syncp);
1578 sq->stats.bytes += bytes;
1579 sq->stats.packets += packets;
1580 u64_stats_update_end(&sq->stats.syncp);
1583 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1585 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1587 else if (q < vi->curr_queue_pairs)
1593 static void virtnet_poll_cleantx(struct receive_queue *rq)
1595 struct virtnet_info *vi = rq->vq->vdev->priv;
1596 unsigned int index = vq2rxq(rq->vq);
1597 struct send_queue *sq = &vi->sq[index];
1598 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1600 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
1603 if (__netif_tx_trylock(txq)) {
1605 virtqueue_disable_cb(sq->vq);
1606 free_old_xmit_skbs(sq, true);
1607 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1609 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1610 netif_tx_wake_queue(txq);
1612 __netif_tx_unlock(txq);
1616 static int virtnet_poll(struct napi_struct *napi, int budget)
1618 struct receive_queue *rq =
1619 container_of(napi, struct receive_queue, napi);
1620 struct virtnet_info *vi = rq->vq->vdev->priv;
1621 struct send_queue *sq;
1622 unsigned int received;
1623 unsigned int xdp_xmit = 0;
1625 virtnet_poll_cleantx(rq);
1627 received = virtnet_receive(rq, budget, &xdp_xmit);
1629 /* Out of packets? */
1630 if (received < budget)
1631 virtqueue_napi_complete(napi, rq->vq, received);
1633 if (xdp_xmit & VIRTIO_XDP_REDIR)
1636 if (xdp_xmit & VIRTIO_XDP_TX) {
1637 sq = virtnet_xdp_get_sq(vi);
1638 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1639 u64_stats_update_begin(&sq->stats.syncp);
1641 u64_stats_update_end(&sq->stats.syncp);
1643 virtnet_xdp_put_sq(vi, sq);
1649 static int virtnet_open(struct net_device *dev)
1651 struct virtnet_info *vi = netdev_priv(dev);
1654 for (i = 0; i < vi->max_queue_pairs; i++) {
1655 if (i < vi->curr_queue_pairs)
1656 /* Make sure we have some buffers: if oom use wq. */
1657 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1658 schedule_delayed_work(&vi->refill, 0);
1660 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
1664 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1665 MEM_TYPE_PAGE_SHARED, NULL);
1667 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1671 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1672 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1678 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1680 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1681 struct virtnet_info *vi = sq->vq->vdev->priv;
1682 unsigned int index = vq2txq(sq->vq);
1683 struct netdev_queue *txq;
1687 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1688 /* We don't need to enable cb for XDP */
1689 napi_complete_done(napi, 0);
1693 txq = netdev_get_tx_queue(vi->dev, index);
1694 __netif_tx_lock(txq, raw_smp_processor_id());
1695 virtqueue_disable_cb(sq->vq);
1696 free_old_xmit_skbs(sq, true);
1698 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1699 netif_tx_wake_queue(txq);
1701 opaque = virtqueue_enable_cb_prepare(sq->vq);
1703 done = napi_complete_done(napi, 0);
1706 virtqueue_disable_cb(sq->vq);
1708 __netif_tx_unlock(txq);
1711 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
1712 if (napi_schedule_prep(napi)) {
1713 __netif_tx_lock(txq, raw_smp_processor_id());
1714 virtqueue_disable_cb(sq->vq);
1715 __netif_tx_unlock(txq);
1716 __napi_schedule(napi);
1724 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1726 struct virtio_net_hdr_mrg_rxbuf *hdr;
1727 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1728 struct virtnet_info *vi = sq->vq->vdev->priv;
1730 unsigned hdr_len = vi->hdr_len;
1733 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1735 can_push = vi->any_header_sg &&
1736 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1737 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1738 /* Even if we can, don't push here yet as this would skew
1739 * csum_start offset below. */
1741 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1743 hdr = skb_vnet_hdr(skb);
1745 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1746 virtio_is_little_endian(vi->vdev), false,
1750 if (vi->mergeable_rx_bufs)
1751 hdr->num_buffers = 0;
1753 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1755 __skb_push(skb, hdr_len);
1756 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1757 if (unlikely(num_sg < 0))
1759 /* Pull header back to avoid skew in tx bytes calculations. */
1760 __skb_pull(skb, hdr_len);
1762 sg_set_buf(sq->sg, hdr, hdr_len);
1763 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1764 if (unlikely(num_sg < 0))
1768 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
1771 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
1773 struct virtnet_info *vi = netdev_priv(dev);
1774 int qnum = skb_get_queue_mapping(skb);
1775 struct send_queue *sq = &vi->sq[qnum];
1777 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1778 bool kick = !netdev_xmit_more();
1779 bool use_napi = sq->napi.weight;
1781 /* Free up any pending old buffers before queueing new ones. */
1784 virtqueue_disable_cb(sq->vq);
1786 free_old_xmit_skbs(sq, false);
1788 } while (use_napi && kick &&
1789 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1791 /* timestamp packet in software */
1792 skb_tx_timestamp(skb);
1794 /* Try to transmit */
1795 err = xmit_skb(sq, skb);
1797 /* This should not happen! */
1798 if (unlikely(err)) {
1799 dev->stats.tx_fifo_errors++;
1800 if (net_ratelimit())
1802 "Unexpected TXQ (%d) queue failure: %d\n",
1804 dev->stats.tx_dropped++;
1805 dev_kfree_skb_any(skb);
1806 return NETDEV_TX_OK;
1809 /* Don't wait up for transmitted skbs to be freed. */
1815 /* If running out of space, stop queue to avoid getting packets that we
1816 * are then unable to transmit.
1817 * An alternative would be to force queuing layer to requeue the skb by
1818 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1819 * returned in a normal path of operation: it means that driver is not
1820 * maintaining the TX queue stop/start state properly, and causes
1821 * the stack to do a non-trivial amount of useless work.
1822 * Since most packets only take 1 or 2 ring slots, stopping the queue
1823 * early means 16 slots are typically wasted.
1825 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1826 netif_stop_subqueue(dev, qnum);
1828 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1829 /* More just got used, free them then recheck. */
1830 free_old_xmit_skbs(sq, false);
1831 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1832 netif_start_subqueue(dev, qnum);
1833 virtqueue_disable_cb(sq->vq);
1838 if (kick || netif_xmit_stopped(txq)) {
1839 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1840 u64_stats_update_begin(&sq->stats.syncp);
1842 u64_stats_update_end(&sq->stats.syncp);
1846 return NETDEV_TX_OK;
1850 * Send command via the control virtqueue and check status. Commands
1851 * supported by the hypervisor, as indicated by feature bits, should
1852 * never fail unless improperly formatted.
1854 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1855 struct scatterlist *out)
1857 struct scatterlist *sgs[4], hdr, stat;
1858 unsigned out_num = 0, tmp;
1861 /* Caller should know better */
1862 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
1864 vi->ctrl->status = ~0;
1865 vi->ctrl->hdr.class = class;
1866 vi->ctrl->hdr.cmd = cmd;
1868 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
1869 sgs[out_num++] = &hdr;
1872 sgs[out_num++] = out;
1874 /* Add return status. */
1875 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
1876 sgs[out_num] = &stat;
1878 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1879 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1881 dev_warn(&vi->vdev->dev,
1882 "Failed to add sgs for command vq: %d\n.", ret);
1886 if (unlikely(!virtqueue_kick(vi->cvq)))
1887 return vi->ctrl->status == VIRTIO_NET_OK;
1889 /* Spin for a response, the kick causes an ioport write, trapping
1890 * into the hypervisor, so the request should be handled immediately.
1892 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1893 !virtqueue_is_broken(vi->cvq))
1896 return vi->ctrl->status == VIRTIO_NET_OK;
1899 static int virtnet_set_mac_address(struct net_device *dev, void *p)
1901 struct virtnet_info *vi = netdev_priv(dev);
1902 struct virtio_device *vdev = vi->vdev;
1904 struct sockaddr *addr;
1905 struct scatterlist sg;
1907 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1910 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
1914 ret = eth_prepare_mac_addr_change(dev, addr);
1918 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1919 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1920 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1921 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
1922 dev_warn(&vdev->dev,
1923 "Failed to set mac address by vq command.\n");
1927 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1928 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1931 /* Naturally, this has an atomicity problem. */
1932 for (i = 0; i < dev->addr_len; i++)
1933 virtio_cwrite8(vdev,
1934 offsetof(struct virtio_net_config, mac) +
1935 i, addr->sa_data[i]);
1938 eth_commit_mac_addr_change(dev, p);
1946 static void virtnet_stats(struct net_device *dev,
1947 struct rtnl_link_stats64 *tot)
1949 struct virtnet_info *vi = netdev_priv(dev);
1953 for (i = 0; i < vi->max_queue_pairs; i++) {
1954 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
1955 struct receive_queue *rq = &vi->rq[i];
1956 struct send_queue *sq = &vi->sq[i];
1959 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1960 tpackets = sq->stats.packets;
1961 tbytes = sq->stats.bytes;
1962 terrors = sq->stats.tx_timeouts;
1963 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
1966 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
1967 rpackets = rq->stats.packets;
1968 rbytes = rq->stats.bytes;
1969 rdrops = rq->stats.drops;
1970 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
1972 tot->rx_packets += rpackets;
1973 tot->tx_packets += tpackets;
1974 tot->rx_bytes += rbytes;
1975 tot->tx_bytes += tbytes;
1976 tot->rx_dropped += rdrops;
1977 tot->tx_errors += terrors;
1980 tot->tx_dropped = dev->stats.tx_dropped;
1981 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
1982 tot->rx_length_errors = dev->stats.rx_length_errors;
1983 tot->rx_frame_errors = dev->stats.rx_frame_errors;
1986 static void virtnet_ack_link_announce(struct virtnet_info *vi)
1989 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1990 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1991 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1995 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1997 struct scatterlist sg;
1998 struct net_device *dev = vi->dev;
2000 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
2003 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
2004 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
2006 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2007 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
2008 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
2012 vi->curr_queue_pairs = queue_pairs;
2013 /* virtnet_open() will refill when device is going to up. */
2014 if (dev->flags & IFF_UP)
2015 schedule_delayed_work(&vi->refill, 0);
2021 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2026 err = _virtnet_set_queues(vi, queue_pairs);
2031 static int virtnet_close(struct net_device *dev)
2033 struct virtnet_info *vi = netdev_priv(dev);
2036 /* Make sure refill_work doesn't re-enable napi! */
2037 cancel_delayed_work_sync(&vi->refill);
2039 for (i = 0; i < vi->max_queue_pairs; i++) {
2040 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
2041 napi_disable(&vi->rq[i].napi);
2042 virtnet_napi_tx_disable(&vi->sq[i].napi);
2048 static void virtnet_set_rx_mode(struct net_device *dev)
2050 struct virtnet_info *vi = netdev_priv(dev);
2051 struct scatterlist sg[2];
2052 struct virtio_net_ctrl_mac *mac_data;
2053 struct netdev_hw_addr *ha;
2059 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
2060 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
2063 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
2064 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2066 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
2068 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2069 VIRTIO_NET_CTRL_RX_PROMISC, sg))
2070 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
2071 vi->ctrl->promisc ? "en" : "dis");
2073 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
2075 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2076 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2077 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2078 vi->ctrl->allmulti ? "en" : "dis");
2080 uc_count = netdev_uc_count(dev);
2081 mc_count = netdev_mc_count(dev);
2082 /* MAC filter - use one buffer for both lists */
2083 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2084 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
2089 sg_init_table(sg, 2);
2091 /* Store the unicast list and count in the front of the buffer */
2092 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2094 netdev_for_each_uc_addr(ha, dev)
2095 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2097 sg_set_buf(&sg[0], mac_data,
2098 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2100 /* multicast list and count fill the end */
2101 mac_data = (void *)&mac_data->macs[uc_count][0];
2103 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2105 netdev_for_each_mc_addr(ha, dev)
2106 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2108 sg_set_buf(&sg[1], mac_data,
2109 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2111 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2112 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
2113 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2118 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2119 __be16 proto, u16 vid)
2121 struct virtnet_info *vi = netdev_priv(dev);
2122 struct scatterlist sg;
2124 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2125 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2127 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2128 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
2129 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
2133 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2134 __be16 proto, u16 vid)
2136 struct virtnet_info *vi = netdev_priv(dev);
2137 struct scatterlist sg;
2139 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2140 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2142 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2143 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
2144 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
2148 static void virtnet_clean_affinity(struct virtnet_info *vi)
2152 if (vi->affinity_hint_set) {
2153 for (i = 0; i < vi->max_queue_pairs; i++) {
2154 virtqueue_set_affinity(vi->rq[i].vq, NULL);
2155 virtqueue_set_affinity(vi->sq[i].vq, NULL);
2158 vi->affinity_hint_set = false;
2162 static void virtnet_set_affinity(struct virtnet_info *vi)
2171 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2172 virtnet_clean_affinity(vi);
2176 num_cpu = num_online_cpus();
2177 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2178 stragglers = num_cpu >= vi->curr_queue_pairs ?
2179 num_cpu % vi->curr_queue_pairs :
2181 cpu = cpumask_first(cpu_online_mask);
2183 for (i = 0; i < vi->curr_queue_pairs; i++) {
2184 group_size = stride + (i < stragglers ? 1 : 0);
2186 for (j = 0; j < group_size; j++) {
2187 cpumask_set_cpu(cpu, mask);
2188 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2191 virtqueue_set_affinity(vi->rq[i].vq, mask);
2192 virtqueue_set_affinity(vi->sq[i].vq, mask);
2193 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
2194 cpumask_clear(mask);
2197 vi->affinity_hint_set = true;
2198 free_cpumask_var(mask);
2201 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
2203 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2205 virtnet_set_affinity(vi);
2209 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2211 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2213 virtnet_set_affinity(vi);
2217 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2219 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2222 virtnet_clean_affinity(vi);
2226 static enum cpuhp_state virtionet_online;
2228 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2232 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2235 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2239 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2243 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2245 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2246 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2250 static void virtnet_get_ringparam(struct net_device *dev,
2251 struct ethtool_ringparam *ring,
2252 struct kernel_ethtool_ringparam *kernel_ring,
2253 struct netlink_ext_ack *extack)
2255 struct virtnet_info *vi = netdev_priv(dev);
2257 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2258 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2259 ring->rx_pending = ring->rx_max_pending;
2260 ring->tx_pending = ring->tx_max_pending;
2263 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
2265 struct net_device *dev = vi->dev;
2266 struct scatterlist sgs[4];
2267 unsigned int sg_buf_size;
2270 sg_init_table(sgs, 4);
2272 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
2273 sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
2275 sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
2276 sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
2278 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
2279 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
2280 sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
2282 sg_buf_size = vi->rss_key_size;
2283 sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
2285 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2286 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
2287 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
2288 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
2294 static void virtnet_init_default_rss(struct virtnet_info *vi)
2299 vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
2300 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
2301 vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
2302 ? vi->rss_indir_table_size - 1 : 0;
2303 vi->ctrl->rss.unclassified_queue = 0;
2305 for (; i < vi->rss_indir_table_size; ++i) {
2306 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
2307 vi->ctrl->rss.indirection_table[i] = indir_val;
2310 vi->ctrl->rss.max_tx_vq = vi->curr_queue_pairs;
2311 vi->ctrl->rss.hash_key_length = vi->rss_key_size;
2313 netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
2316 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
2319 switch (info->flow_type) {
2321 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
2322 info->data = RXH_IP_SRC | RXH_IP_DST |
2323 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2324 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2325 info->data = RXH_IP_SRC | RXH_IP_DST;
2329 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
2330 info->data = RXH_IP_SRC | RXH_IP_DST |
2331 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2332 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2333 info->data = RXH_IP_SRC | RXH_IP_DST;
2337 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
2338 info->data = RXH_IP_SRC | RXH_IP_DST |
2339 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2340 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2341 info->data = RXH_IP_SRC | RXH_IP_DST;
2345 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
2346 info->data = RXH_IP_SRC | RXH_IP_DST |
2347 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2348 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2349 info->data = RXH_IP_SRC | RXH_IP_DST;
2353 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
2354 info->data = RXH_IP_SRC | RXH_IP_DST;
2358 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
2359 info->data = RXH_IP_SRC | RXH_IP_DST;
2368 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
2370 u32 new_hashtypes = vi->rss_hash_types_saved;
2371 bool is_disable = info->data & RXH_DISCARD;
2372 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
2374 /* supports only 'sd', 'sdfn' and 'r' */
2375 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
2378 switch (info->flow_type) {
2380 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
2382 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2383 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
2386 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
2388 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2389 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
2392 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2394 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2397 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
2399 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2400 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
2403 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
2405 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2406 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
2409 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2411 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2414 /* unsupported flow */
2418 /* if unsupported hashtype was set */
2419 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
2422 if (new_hashtypes != vi->rss_hash_types_saved) {
2423 vi->rss_hash_types_saved = new_hashtypes;
2424 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
2425 if (vi->dev->features & NETIF_F_RXHASH)
2426 return virtnet_commit_rss_command(vi);
2432 static void virtnet_get_drvinfo(struct net_device *dev,
2433 struct ethtool_drvinfo *info)
2435 struct virtnet_info *vi = netdev_priv(dev);
2436 struct virtio_device *vdev = vi->vdev;
2438 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2439 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2440 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
2444 /* TODO: Eliminate OOO packets during switching */
2445 static int virtnet_set_channels(struct net_device *dev,
2446 struct ethtool_channels *channels)
2448 struct virtnet_info *vi = netdev_priv(dev);
2449 u16 queue_pairs = channels->combined_count;
2452 /* We don't support separate rx/tx channels.
2453 * We don't allow setting 'other' channels.
2455 if (channels->rx_count || channels->tx_count || channels->other_count)
2458 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
2461 /* For now we don't support modifying channels while XDP is loaded
2462 * also when XDP is loaded all RX queues have XDP programs so we only
2463 * need to check a single RX queue.
2465 if (vi->rq[0].xdp_prog)
2469 err = _virtnet_set_queues(vi, queue_pairs);
2474 virtnet_set_affinity(vi);
2477 netif_set_real_num_tx_queues(dev, queue_pairs);
2478 netif_set_real_num_rx_queues(dev, queue_pairs);
2483 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2485 struct virtnet_info *vi = netdev_priv(dev);
2489 switch (stringset) {
2491 for (i = 0; i < vi->curr_queue_pairs; i++) {
2492 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2493 ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2494 virtnet_rq_stats_desc[j].desc);
2497 for (i = 0; i < vi->curr_queue_pairs; i++) {
2498 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2499 ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2500 virtnet_sq_stats_desc[j].desc);
2506 static int virtnet_get_sset_count(struct net_device *dev, int sset)
2508 struct virtnet_info *vi = netdev_priv(dev);
2512 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2513 VIRTNET_SQ_STATS_LEN);
2519 static void virtnet_get_ethtool_stats(struct net_device *dev,
2520 struct ethtool_stats *stats, u64 *data)
2522 struct virtnet_info *vi = netdev_priv(dev);
2523 unsigned int idx = 0, start, i, j;
2524 const u8 *stats_base;
2527 for (i = 0; i < vi->curr_queue_pairs; i++) {
2528 struct receive_queue *rq = &vi->rq[i];
2530 stats_base = (u8 *)&rq->stats;
2532 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2533 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2534 offset = virtnet_rq_stats_desc[j].offset;
2535 data[idx + j] = *(u64 *)(stats_base + offset);
2537 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2538 idx += VIRTNET_RQ_STATS_LEN;
2541 for (i = 0; i < vi->curr_queue_pairs; i++) {
2542 struct send_queue *sq = &vi->sq[i];
2544 stats_base = (u8 *)&sq->stats;
2546 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2547 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2548 offset = virtnet_sq_stats_desc[j].offset;
2549 data[idx + j] = *(u64 *)(stats_base + offset);
2551 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2552 idx += VIRTNET_SQ_STATS_LEN;
2556 static void virtnet_get_channels(struct net_device *dev,
2557 struct ethtool_channels *channels)
2559 struct virtnet_info *vi = netdev_priv(dev);
2561 channels->combined_count = vi->curr_queue_pairs;
2562 channels->max_combined = vi->max_queue_pairs;
2563 channels->max_other = 0;
2564 channels->rx_count = 0;
2565 channels->tx_count = 0;
2566 channels->other_count = 0;
2569 static int virtnet_set_link_ksettings(struct net_device *dev,
2570 const struct ethtool_link_ksettings *cmd)
2572 struct virtnet_info *vi = netdev_priv(dev);
2574 return ethtool_virtdev_set_link_ksettings(dev, cmd,
2575 &vi->speed, &vi->duplex);
2578 static int virtnet_get_link_ksettings(struct net_device *dev,
2579 struct ethtool_link_ksettings *cmd)
2581 struct virtnet_info *vi = netdev_priv(dev);
2583 cmd->base.speed = vi->speed;
2584 cmd->base.duplex = vi->duplex;
2585 cmd->base.port = PORT_OTHER;
2590 static int virtnet_set_coalesce(struct net_device *dev,
2591 struct ethtool_coalesce *ec,
2592 struct kernel_ethtool_coalesce *kernel_coal,
2593 struct netlink_ext_ack *extack)
2595 struct virtnet_info *vi = netdev_priv(dev);
2598 if (ec->tx_max_coalesced_frames > 1 ||
2599 ec->rx_max_coalesced_frames != 1)
2602 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
2603 if (napi_weight ^ vi->sq[0].napi.weight) {
2604 if (dev->flags & IFF_UP)
2606 for (i = 0; i < vi->max_queue_pairs; i++)
2607 vi->sq[i].napi.weight = napi_weight;
2613 static int virtnet_get_coalesce(struct net_device *dev,
2614 struct ethtool_coalesce *ec,
2615 struct kernel_ethtool_coalesce *kernel_coal,
2616 struct netlink_ext_ack *extack)
2618 struct ethtool_coalesce ec_default = {
2619 .cmd = ETHTOOL_GCOALESCE,
2620 .rx_max_coalesced_frames = 1,
2622 struct virtnet_info *vi = netdev_priv(dev);
2624 memcpy(ec, &ec_default, sizeof(ec_default));
2626 if (vi->sq[0].napi.weight)
2627 ec->tx_max_coalesced_frames = 1;
2632 static void virtnet_init_settings(struct net_device *dev)
2634 struct virtnet_info *vi = netdev_priv(dev);
2636 vi->speed = SPEED_UNKNOWN;
2637 vi->duplex = DUPLEX_UNKNOWN;
2640 static void virtnet_update_settings(struct virtnet_info *vi)
2645 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2648 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
2650 if (ethtool_validate_speed(speed))
2653 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
2655 if (ethtool_validate_duplex(duplex))
2656 vi->duplex = duplex;
2659 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
2661 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
2664 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
2666 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
2669 static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
2671 struct virtnet_info *vi = netdev_priv(dev);
2675 for (i = 0; i < vi->rss_indir_table_size; ++i)
2676 indir[i] = vi->ctrl->rss.indirection_table[i];
2680 memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
2683 *hfunc = ETH_RSS_HASH_TOP;
2688 static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
2690 struct virtnet_info *vi = netdev_priv(dev);
2693 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
2697 for (i = 0; i < vi->rss_indir_table_size; ++i)
2698 vi->ctrl->rss.indirection_table[i] = indir[i];
2701 memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
2703 virtnet_commit_rss_command(vi);
2708 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
2710 struct virtnet_info *vi = netdev_priv(dev);
2713 switch (info->cmd) {
2714 case ETHTOOL_GRXRINGS:
2715 info->data = vi->curr_queue_pairs;
2718 virtnet_get_hashflow(vi, info);
2727 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
2729 struct virtnet_info *vi = netdev_priv(dev);
2732 switch (info->cmd) {
2734 if (!virtnet_set_hashflow(vi, info))
2745 static const struct ethtool_ops virtnet_ethtool_ops = {
2746 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
2747 .get_drvinfo = virtnet_get_drvinfo,
2748 .get_link = ethtool_op_get_link,
2749 .get_ringparam = virtnet_get_ringparam,
2750 .get_strings = virtnet_get_strings,
2751 .get_sset_count = virtnet_get_sset_count,
2752 .get_ethtool_stats = virtnet_get_ethtool_stats,
2753 .set_channels = virtnet_set_channels,
2754 .get_channels = virtnet_get_channels,
2755 .get_ts_info = ethtool_op_get_ts_info,
2756 .get_link_ksettings = virtnet_get_link_ksettings,
2757 .set_link_ksettings = virtnet_set_link_ksettings,
2758 .set_coalesce = virtnet_set_coalesce,
2759 .get_coalesce = virtnet_get_coalesce,
2760 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
2761 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
2762 .get_rxfh = virtnet_get_rxfh,
2763 .set_rxfh = virtnet_set_rxfh,
2764 .get_rxnfc = virtnet_get_rxnfc,
2765 .set_rxnfc = virtnet_set_rxnfc,
2768 static void virtnet_freeze_down(struct virtio_device *vdev)
2770 struct virtnet_info *vi = vdev->priv;
2772 /* Make sure no work handler is accessing the device */
2773 flush_work(&vi->config_work);
2775 netif_tx_lock_bh(vi->dev);
2776 netif_device_detach(vi->dev);
2777 netif_tx_unlock_bh(vi->dev);
2778 if (netif_running(vi->dev))
2779 virtnet_close(vi->dev);
2782 static int init_vqs(struct virtnet_info *vi);
2784 static int virtnet_restore_up(struct virtio_device *vdev)
2786 struct virtnet_info *vi = vdev->priv;
2793 virtio_device_ready(vdev);
2795 if (netif_running(vi->dev)) {
2796 err = virtnet_open(vi->dev);
2801 netif_tx_lock_bh(vi->dev);
2802 netif_device_attach(vi->dev);
2803 netif_tx_unlock_bh(vi->dev);
2807 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2809 struct scatterlist sg;
2810 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
2812 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
2814 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2815 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
2816 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
2823 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2827 if (!vi->guest_offloads)
2830 return virtnet_set_guest_offloads(vi, offloads);
2833 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2835 u64 offloads = vi->guest_offloads;
2837 if (!vi->guest_offloads)
2840 return virtnet_set_guest_offloads(vi, offloads);
2843 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2844 struct netlink_ext_ack *extack)
2846 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2847 struct virtnet_info *vi = netdev_priv(dev);
2848 struct bpf_prog *old_prog;
2849 u16 xdp_qp = 0, curr_qp;
2852 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2853 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2854 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2855 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
2856 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
2857 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
2858 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
2862 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
2863 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
2867 if (dev->mtu > max_sz) {
2868 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
2869 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2873 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2875 xdp_qp = nr_cpu_ids;
2877 /* XDP requires extra queues for XDP_TX */
2878 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
2879 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",
2880 curr_qp + xdp_qp, vi->max_queue_pairs);
2884 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
2885 if (!prog && !old_prog)
2889 bpf_prog_add(prog, vi->max_queue_pairs - 1);
2891 /* Make sure NAPI is not using any XDP TX queues for RX. */
2892 if (netif_running(dev)) {
2893 for (i = 0; i < vi->max_queue_pairs; i++) {
2894 napi_disable(&vi->rq[i].napi);
2895 virtnet_napi_tx_disable(&vi->sq[i].napi);
2900 for (i = 0; i < vi->max_queue_pairs; i++) {
2901 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2903 virtnet_restore_guest_offloads(vi);
2908 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2911 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
2912 vi->xdp_queue_pairs = xdp_qp;
2915 vi->xdp_enabled = true;
2916 for (i = 0; i < vi->max_queue_pairs; i++) {
2917 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2918 if (i == 0 && !old_prog)
2919 virtnet_clear_guest_offloads(vi);
2922 vi->xdp_enabled = false;
2925 for (i = 0; i < vi->max_queue_pairs; i++) {
2927 bpf_prog_put(old_prog);
2928 if (netif_running(dev)) {
2929 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2930 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2939 virtnet_clear_guest_offloads(vi);
2940 for (i = 0; i < vi->max_queue_pairs; i++)
2941 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
2944 if (netif_running(dev)) {
2945 for (i = 0; i < vi->max_queue_pairs; i++) {
2946 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2947 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2952 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2956 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2958 switch (xdp->command) {
2959 case XDP_SETUP_PROG:
2960 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
2966 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2969 struct virtnet_info *vi = netdev_priv(dev);
2972 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2975 ret = snprintf(buf, len, "sby");
2982 static int virtnet_set_features(struct net_device *dev,
2983 netdev_features_t features)
2985 struct virtnet_info *vi = netdev_priv(dev);
2989 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
2990 if (vi->xdp_enabled)
2993 if (features & NETIF_F_GRO_HW)
2994 offloads = vi->guest_offloads_capable;
2996 offloads = vi->guest_offloads_capable &
2997 ~GUEST_OFFLOAD_GRO_HW_MASK;
2999 err = virtnet_set_guest_offloads(vi, offloads);
3002 vi->guest_offloads = offloads;
3005 if ((dev->features ^ features) & NETIF_F_RXHASH) {
3006 if (features & NETIF_F_RXHASH)
3007 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3009 vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
3011 if (!virtnet_commit_rss_command(vi))
3018 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
3020 struct virtnet_info *priv = netdev_priv(dev);
3021 struct send_queue *sq = &priv->sq[txqueue];
3022 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
3024 u64_stats_update_begin(&sq->stats.syncp);
3025 sq->stats.tx_timeouts++;
3026 u64_stats_update_end(&sq->stats.syncp);
3028 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
3029 txqueue, sq->name, sq->vq->index, sq->vq->name,
3030 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
3033 static const struct net_device_ops virtnet_netdev = {
3034 .ndo_open = virtnet_open,
3035 .ndo_stop = virtnet_close,
3036 .ndo_start_xmit = start_xmit,
3037 .ndo_validate_addr = eth_validate_addr,
3038 .ndo_set_mac_address = virtnet_set_mac_address,
3039 .ndo_set_rx_mode = virtnet_set_rx_mode,
3040 .ndo_get_stats64 = virtnet_stats,
3041 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
3042 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
3043 .ndo_bpf = virtnet_xdp,
3044 .ndo_xdp_xmit = virtnet_xdp_xmit,
3045 .ndo_features_check = passthru_features_check,
3046 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
3047 .ndo_set_features = virtnet_set_features,
3048 .ndo_tx_timeout = virtnet_tx_timeout,
3051 static void virtnet_config_changed_work(struct work_struct *work)
3053 struct virtnet_info *vi =
3054 container_of(work, struct virtnet_info, config_work);
3057 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
3058 struct virtio_net_config, status, &v) < 0)
3061 if (v & VIRTIO_NET_S_ANNOUNCE) {
3062 netdev_notify_peers(vi->dev);
3063 virtnet_ack_link_announce(vi);
3066 /* Ignore unknown (future) status bits */
3067 v &= VIRTIO_NET_S_LINK_UP;
3069 if (vi->status == v)
3074 if (vi->status & VIRTIO_NET_S_LINK_UP) {
3075 virtnet_update_settings(vi);
3076 netif_carrier_on(vi->dev);
3077 netif_tx_wake_all_queues(vi->dev);
3079 netif_carrier_off(vi->dev);
3080 netif_tx_stop_all_queues(vi->dev);
3084 static void virtnet_config_changed(struct virtio_device *vdev)
3086 struct virtnet_info *vi = vdev->priv;
3088 schedule_work(&vi->config_work);
3091 static void virtnet_free_queues(struct virtnet_info *vi)
3095 for (i = 0; i < vi->max_queue_pairs; i++) {
3096 __netif_napi_del(&vi->rq[i].napi);
3097 __netif_napi_del(&vi->sq[i].napi);
3100 /* We called __netif_napi_del(),
3101 * we need to respect an RCU grace period before freeing vi->rq
3110 static void _free_receive_bufs(struct virtnet_info *vi)
3112 struct bpf_prog *old_prog;
3115 for (i = 0; i < vi->max_queue_pairs; i++) {
3116 while (vi->rq[i].pages)
3117 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
3119 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
3120 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
3122 bpf_prog_put(old_prog);
3126 static void free_receive_bufs(struct virtnet_info *vi)
3129 _free_receive_bufs(vi);
3133 static void free_receive_page_frags(struct virtnet_info *vi)
3136 for (i = 0; i < vi->max_queue_pairs; i++)
3137 if (vi->rq[i].alloc_frag.page)
3138 put_page(vi->rq[i].alloc_frag.page);
3141 static void free_unused_bufs(struct virtnet_info *vi)
3146 for (i = 0; i < vi->max_queue_pairs; i++) {
3147 struct virtqueue *vq = vi->sq[i].vq;
3148 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
3149 if (!is_xdp_frame(buf))
3152 xdp_return_frame(ptr_to_xdp(buf));
3156 for (i = 0; i < vi->max_queue_pairs; i++) {
3157 struct virtqueue *vq = vi->rq[i].vq;
3159 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
3160 if (vi->mergeable_rx_bufs) {
3161 put_page(virt_to_head_page(buf));
3162 } else if (vi->big_packets) {
3163 give_pages(&vi->rq[i], buf);
3165 put_page(virt_to_head_page(buf));
3171 static void virtnet_del_vqs(struct virtnet_info *vi)
3173 struct virtio_device *vdev = vi->vdev;
3175 virtnet_clean_affinity(vi);
3177 vdev->config->del_vqs(vdev);
3179 virtnet_free_queues(vi);
3182 /* How large should a single buffer be so a queue full of these can fit at
3183 * least one full packet?
3184 * Logic below assumes the mergeable buffer header is used.
3186 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
3188 const unsigned int hdr_len = vi->hdr_len;
3189 unsigned int rq_size = virtqueue_get_vring_size(vq);
3190 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
3191 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
3192 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
3194 return max(max(min_buf_len, hdr_len) - hdr_len,
3195 (unsigned int)GOOD_PACKET_LEN);
3198 static int virtnet_find_vqs(struct virtnet_info *vi)
3200 vq_callback_t **callbacks;
3201 struct virtqueue **vqs;
3207 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
3208 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
3209 * possible control vq.
3211 total_vqs = vi->max_queue_pairs * 2 +
3212 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
3214 /* Allocate space for find_vqs parameters */
3215 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
3218 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
3221 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
3224 if (!vi->big_packets || vi->mergeable_rx_bufs) {
3225 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
3232 /* Parameters for control virtqueue, if any */
3234 callbacks[total_vqs - 1] = NULL;
3235 names[total_vqs - 1] = "control";
3238 /* Allocate/initialize parameters for send/receive virtqueues */
3239 for (i = 0; i < vi->max_queue_pairs; i++) {
3240 callbacks[rxq2vq(i)] = skb_recv_done;
3241 callbacks[txq2vq(i)] = skb_xmit_done;
3242 sprintf(vi->rq[i].name, "input.%d", i);
3243 sprintf(vi->sq[i].name, "output.%d", i);
3244 names[rxq2vq(i)] = vi->rq[i].name;
3245 names[txq2vq(i)] = vi->sq[i].name;
3247 ctx[rxq2vq(i)] = true;
3250 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
3256 vi->cvq = vqs[total_vqs - 1];
3257 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
3258 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3261 for (i = 0; i < vi->max_queue_pairs; i++) {
3262 vi->rq[i].vq = vqs[rxq2vq(i)];
3263 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
3264 vi->sq[i].vq = vqs[txq2vq(i)];
3267 /* run here: ret == 0. */
3282 static int virtnet_alloc_queues(struct virtnet_info *vi)
3287 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
3293 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
3296 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
3300 INIT_DELAYED_WORK(&vi->refill, refill_work);
3301 for (i = 0; i < vi->max_queue_pairs; i++) {
3302 vi->rq[i].pages = NULL;
3303 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
3305 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
3307 napi_tx ? napi_weight : 0);
3309 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
3310 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
3311 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
3313 u64_stats_init(&vi->rq[i].stats.syncp);
3314 u64_stats_init(&vi->sq[i].stats.syncp);
3327 static int init_vqs(struct virtnet_info *vi)
3331 /* Allocate send & receive queues */
3332 ret = virtnet_alloc_queues(vi);
3336 ret = virtnet_find_vqs(vi);
3341 virtnet_set_affinity(vi);
3347 virtnet_free_queues(vi);
3353 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
3356 struct virtnet_info *vi = netdev_priv(queue->dev);
3357 unsigned int queue_index = get_netdev_rx_queue_index(queue);
3358 unsigned int headroom = virtnet_get_headroom(vi);
3359 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
3360 struct ewma_pkt_len *avg;
3362 BUG_ON(queue_index >= vi->max_queue_pairs);
3363 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
3364 return sprintf(buf, "%u\n",
3365 get_mergeable_buf_len(&vi->rq[queue_index], avg,
3366 SKB_DATA_ALIGN(headroom + tailroom)));
3369 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
3370 __ATTR_RO(mergeable_rx_buffer_size);
3372 static struct attribute *virtio_net_mrg_rx_attrs[] = {
3373 &mergeable_rx_buffer_size_attribute.attr,
3377 static const struct attribute_group virtio_net_mrg_rx_group = {
3378 .name = "virtio_net",
3379 .attrs = virtio_net_mrg_rx_attrs
3383 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
3385 const char *fname, const char *dname)
3387 if (!virtio_has_feature(vdev, fbit))
3390 dev_err(&vdev->dev, "device advertises feature %s but not %s",
3396 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
3397 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
3399 static bool virtnet_validate_features(struct virtio_device *vdev)
3401 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
3402 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
3403 "VIRTIO_NET_F_CTRL_VQ") ||
3404 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
3405 "VIRTIO_NET_F_CTRL_VQ") ||
3406 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
3407 "VIRTIO_NET_F_CTRL_VQ") ||
3408 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
3409 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
3410 "VIRTIO_NET_F_CTRL_VQ") ||
3411 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
3412 "VIRTIO_NET_F_CTRL_VQ") ||
3413 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
3414 "VIRTIO_NET_F_CTRL_VQ"))) {
3421 #define MIN_MTU ETH_MIN_MTU
3422 #define MAX_MTU ETH_MAX_MTU
3424 static int virtnet_validate(struct virtio_device *vdev)
3426 if (!vdev->config->get) {
3427 dev_err(&vdev->dev, "%s failure: config access disabled\n",
3432 if (!virtnet_validate_features(vdev))
3435 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3436 int mtu = virtio_cread16(vdev,
3437 offsetof(struct virtio_net_config,
3440 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3446 static int virtnet_probe(struct virtio_device *vdev)
3448 int i, err = -ENOMEM;
3449 struct net_device *dev;
3450 struct virtnet_info *vi;
3451 u16 max_queue_pairs;
3454 /* Find if host supports multiqueue/rss virtio_net device */
3455 max_queue_pairs = 1;
3456 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3458 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
3460 /* We need at least 2 queue's */
3461 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3462 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3463 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3464 max_queue_pairs = 1;
3466 /* Allocate ourselves a network device with room for our info */
3467 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
3471 /* Set up network device as normal. */
3472 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3473 IFF_TX_SKB_NO_LINEAR;
3474 dev->netdev_ops = &virtnet_netdev;
3475 dev->features = NETIF_F_HIGHDMA;
3477 dev->ethtool_ops = &virtnet_ethtool_ops;
3478 SET_NETDEV_DEV(dev, &vdev->dev);
3480 /* Do we support "hardware" checksums? */
3481 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
3482 /* This opens up the world of extra features. */
3483 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3485 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3487 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
3488 dev->hw_features |= NETIF_F_TSO
3489 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
3491 /* Individual feature bits: what can host handle? */
3492 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
3493 dev->hw_features |= NETIF_F_TSO;
3494 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
3495 dev->hw_features |= NETIF_F_TSO6;
3496 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
3497 dev->hw_features |= NETIF_F_TSO_ECN;
3499 dev->features |= NETIF_F_GSO_ROBUST;
3502 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
3503 /* (!csum && gso) case will be fixed by register_netdev() */
3505 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
3506 dev->features |= NETIF_F_RXCSUM;
3507 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3508 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
3509 dev->features |= NETIF_F_GRO_HW;
3510 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
3511 dev->hw_features |= NETIF_F_GRO_HW;
3513 dev->vlan_features = dev->features;
3515 /* MTU range: 68 - 65535 */
3516 dev->min_mtu = MIN_MTU;
3517 dev->max_mtu = MAX_MTU;
3519 /* Configuration may specify what MAC to use. Otherwise random. */
3520 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
3523 virtio_cread_bytes(vdev,
3524 offsetof(struct virtio_net_config, mac),
3526 eth_hw_addr_set(dev, addr);
3528 eth_hw_addr_random(dev);
3531 /* Set up our device-specific information */
3532 vi = netdev_priv(dev);
3537 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
3539 /* If we can receive ANY GSO packets, we must allocate large ones. */
3540 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3541 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3542 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
3543 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
3544 vi->big_packets = true;
3546 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
3547 vi->mergeable_rx_bufs = true;
3549 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
3550 vi->has_rss_hash_report = true;
3552 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3555 if (vi->has_rss || vi->has_rss_hash_report) {
3556 vi->rss_indir_table_size =
3557 virtio_cread16(vdev, offsetof(struct virtio_net_config,
3558 rss_max_indirection_table_length));
3560 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
3562 vi->rss_hash_types_supported =
3563 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
3564 vi->rss_hash_types_supported &=
3565 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
3566 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
3567 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
3569 dev->hw_features |= NETIF_F_RXHASH;
3572 if (vi->has_rss_hash_report)
3573 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
3574 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
3575 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3576 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3578 vi->hdr_len = sizeof(struct virtio_net_hdr);
3580 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
3581 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3582 vi->any_header_sg = true;
3584 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3587 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3588 mtu = virtio_cread16(vdev,
3589 offsetof(struct virtio_net_config,
3591 if (mtu < dev->min_mtu) {
3592 /* Should never trigger: MTU was previously validated
3593 * in virtnet_validate.
3596 "device MTU appears to have changed it is now %d < %d",
3605 /* TODO: size buffers correctly in this case. */
3606 if (dev->mtu > ETH_DATA_LEN)
3607 vi->big_packets = true;
3610 if (vi->any_header_sg)
3611 dev->needed_headroom = vi->hdr_len;
3613 /* Enable multiqueue by default */
3614 if (num_online_cpus() >= max_queue_pairs)
3615 vi->curr_queue_pairs = max_queue_pairs;
3617 vi->curr_queue_pairs = num_online_cpus();
3618 vi->max_queue_pairs = max_queue_pairs;
3620 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
3626 if (vi->mergeable_rx_bufs)
3627 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3629 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
3630 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
3632 virtnet_init_settings(dev);
3634 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3635 vi->failover = net_failover_create(vi->dev);
3636 if (IS_ERR(vi->failover)) {
3637 err = PTR_ERR(vi->failover);
3642 if (vi->has_rss || vi->has_rss_hash_report)
3643 virtnet_init_default_rss(vi);
3645 /* serialize netdev register + virtio_device_ready() with ndo_open() */
3648 err = register_netdevice(dev);
3650 pr_debug("virtio_net: registering device failed\n");
3655 virtio_device_ready(vdev);
3659 err = virtnet_cpu_notif_add(vi);
3661 pr_debug("virtio_net: registering cpu notifier failed\n");
3662 goto free_unregister_netdev;
3665 virtnet_set_queues(vi, vi->curr_queue_pairs);
3667 /* Assume link up if device can't report link status,
3668 otherwise get link status from config. */
3669 netif_carrier_off(dev);
3670 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
3671 schedule_work(&vi->config_work);
3673 vi->status = VIRTIO_NET_S_LINK_UP;
3674 virtnet_update_settings(vi);
3675 netif_carrier_on(dev);
3678 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3679 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3680 set_bit(guest_offloads[i], &vi->guest_offloads);
3681 vi->guest_offloads_capable = vi->guest_offloads;
3683 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3684 dev->name, max_queue_pairs);
3688 free_unregister_netdev:
3689 virtio_reset_device(vdev);
3691 unregister_netdev(dev);
3693 net_failover_destroy(vi->failover);
3695 cancel_delayed_work_sync(&vi->refill);
3696 free_receive_page_frags(vi);
3697 virtnet_del_vqs(vi);
3703 static void remove_vq_common(struct virtnet_info *vi)
3705 virtio_reset_device(vi->vdev);
3707 /* Free unused buffers in both send and recv, if any. */
3708 free_unused_bufs(vi);
3710 free_receive_bufs(vi);
3712 free_receive_page_frags(vi);
3714 virtnet_del_vqs(vi);
3717 static void virtnet_remove(struct virtio_device *vdev)
3719 struct virtnet_info *vi = vdev->priv;
3721 virtnet_cpu_notif_remove(vi);
3723 /* Make sure no work handler is accessing the device. */
3724 flush_work(&vi->config_work);
3726 unregister_netdev(vi->dev);
3728 net_failover_destroy(vi->failover);
3730 remove_vq_common(vi);
3732 free_netdev(vi->dev);
3735 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
3737 struct virtnet_info *vi = vdev->priv;
3739 virtnet_cpu_notif_remove(vi);
3740 virtnet_freeze_down(vdev);
3741 remove_vq_common(vi);
3746 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
3748 struct virtnet_info *vi = vdev->priv;
3751 err = virtnet_restore_up(vdev);
3754 virtnet_set_queues(vi, vi->curr_queue_pairs);
3756 err = virtnet_cpu_notif_add(vi);
3758 virtnet_freeze_down(vdev);
3759 remove_vq_common(vi);
3766 static struct virtio_device_id id_table[] = {
3767 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3771 #define VIRTNET_FEATURES \
3772 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3774 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3775 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3776 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3777 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3778 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3779 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3780 VIRTIO_NET_F_CTRL_MAC_ADDR, \
3781 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
3782 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
3783 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT
3785 static unsigned int features[] = {
3789 static unsigned int features_legacy[] = {
3792 VIRTIO_F_ANY_LAYOUT,
3795 static struct virtio_driver virtio_net_driver = {
3796 .feature_table = features,
3797 .feature_table_size = ARRAY_SIZE(features),
3798 .feature_table_legacy = features_legacy,
3799 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
3800 .driver.name = KBUILD_MODNAME,
3801 .driver.owner = THIS_MODULE,
3802 .id_table = id_table,
3803 .validate = virtnet_validate,
3804 .probe = virtnet_probe,
3805 .remove = virtnet_remove,
3806 .config_changed = virtnet_config_changed,
3807 #ifdef CONFIG_PM_SLEEP
3808 .freeze = virtnet_freeze,
3809 .restore = virtnet_restore,
3813 static __init int virtio_net_driver_init(void)
3817 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
3819 virtnet_cpu_down_prep);
3822 virtionet_online = ret;
3823 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
3824 NULL, virtnet_cpu_dead);
3827 ret = register_virtio_driver(&virtio_net_driver);
3832 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3834 cpuhp_remove_multi_state(virtionet_online);
3838 module_init(virtio_net_driver_init);
3840 static __exit void virtio_net_driver_exit(void)
3842 unregister_virtio_driver(&virtio_net_driver);
3843 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3844 cpuhp_remove_multi_state(virtionet_online);
3846 module_exit(virtio_net_driver_exit);
3848 MODULE_DEVICE_TABLE(virtio, id_table);
3849 MODULE_DESCRIPTION("Virtio network driver");
3850 MODULE_LICENSE("GPL");