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;
1008 /* We can only create skb based on xdp_page. */
1009 if (unlikely(xdp_page != page)) {
1012 head_skb = page_to_skb(vi, rq, xdp_page, offset,
1013 len, PAGE_SIZE, false,
1015 VIRTIO_XDP_HEADROOM);
1021 xdpf = xdp_convert_buff_to_frame(&xdp);
1022 if (unlikely(!xdpf))
1024 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1025 if (unlikely(!err)) {
1026 xdp_return_frame_rx_napi(xdpf);
1027 } else if (unlikely(err < 0)) {
1028 trace_xdp_exception(vi->dev, xdp_prog, act);
1029 if (unlikely(xdp_page != page))
1033 *xdp_xmit |= VIRTIO_XDP_TX;
1034 if (unlikely(xdp_page != page))
1039 stats->xdp_redirects++;
1040 err = xdp_do_redirect(dev, &xdp, xdp_prog);
1042 if (unlikely(xdp_page != page))
1046 *xdp_xmit |= VIRTIO_XDP_REDIR;
1047 if (unlikely(xdp_page != page))
1052 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
1055 trace_xdp_exception(vi->dev, xdp_prog, act);
1058 if (unlikely(xdp_page != page))
1059 __free_pages(xdp_page, 0);
1066 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
1067 metasize, headroom);
1068 curr_skb = head_skb;
1070 if (unlikely(!curr_skb))
1075 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
1076 if (unlikely(!buf)) {
1077 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1079 virtio16_to_cpu(vi->vdev,
1081 dev->stats.rx_length_errors++;
1085 stats->bytes += len;
1086 page = virt_to_head_page(buf);
1088 truesize = mergeable_ctx_to_truesize(ctx);
1089 if (unlikely(len > truesize)) {
1090 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1091 dev->name, len, (unsigned long)ctx);
1092 dev->stats.rx_length_errors++;
1096 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
1097 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1098 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
1100 if (unlikely(!nskb))
1102 if (curr_skb == head_skb)
1103 skb_shinfo(curr_skb)->frag_list = nskb;
1105 curr_skb->next = nskb;
1107 head_skb->truesize += nskb->truesize;
1110 if (curr_skb != head_skb) {
1111 head_skb->data_len += len;
1112 head_skb->len += len;
1113 head_skb->truesize += truesize;
1115 offset = buf - page_address(page);
1116 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1118 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1121 skb_add_rx_frag(curr_skb, num_skb_frags, page,
1122 offset, len, truesize);
1126 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
1134 while (num_buf-- > 1) {
1135 buf = virtqueue_get_buf(rq->vq, &len);
1136 if (unlikely(!buf)) {
1137 pr_debug("%s: rx error: %d buffers missing\n",
1138 dev->name, num_buf);
1139 dev->stats.rx_length_errors++;
1142 stats->bytes += len;
1143 page = virt_to_head_page(buf);
1148 dev_kfree_skb(head_skb);
1153 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
1154 struct sk_buff *skb)
1156 enum pkt_hash_types rss_hash_type;
1158 if (!hdr_hash || !skb)
1161 switch ((int)hdr_hash->hash_report) {
1162 case VIRTIO_NET_HASH_REPORT_TCPv4:
1163 case VIRTIO_NET_HASH_REPORT_UDPv4:
1164 case VIRTIO_NET_HASH_REPORT_TCPv6:
1165 case VIRTIO_NET_HASH_REPORT_UDPv6:
1166 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
1167 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
1168 rss_hash_type = PKT_HASH_TYPE_L4;
1170 case VIRTIO_NET_HASH_REPORT_IPv4:
1171 case VIRTIO_NET_HASH_REPORT_IPv6:
1172 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
1173 rss_hash_type = PKT_HASH_TYPE_L3;
1175 case VIRTIO_NET_HASH_REPORT_NONE:
1177 rss_hash_type = PKT_HASH_TYPE_NONE;
1179 skb_set_hash(skb, (unsigned int)hdr_hash->hash_value, rss_hash_type);
1182 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1183 void *buf, unsigned int len, void **ctx,
1184 unsigned int *xdp_xmit,
1185 struct virtnet_rq_stats *stats)
1187 struct net_device *dev = vi->dev;
1188 struct sk_buff *skb;
1189 struct virtio_net_hdr_mrg_rxbuf *hdr;
1191 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
1192 pr_debug("%s: short packet %i\n", dev->name, len);
1193 dev->stats.rx_length_errors++;
1194 if (vi->mergeable_rx_bufs) {
1195 put_page(virt_to_head_page(buf));
1196 } else if (vi->big_packets) {
1197 give_pages(rq, buf);
1199 put_page(virt_to_head_page(buf));
1204 if (vi->mergeable_rx_bufs)
1205 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1207 else if (vi->big_packets)
1208 skb = receive_big(dev, vi, rq, buf, len, stats);
1210 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1215 hdr = skb_vnet_hdr(skb);
1216 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
1217 virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
1219 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
1220 skb->ip_summed = CHECKSUM_UNNECESSARY;
1222 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1223 virtio_is_little_endian(vi->vdev))) {
1224 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1225 dev->name, hdr->hdr.gso_type,
1230 skb_record_rx_queue(skb, vq2rxq(rq->vq));
1231 skb->protocol = eth_type_trans(skb, dev);
1232 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1233 ntohs(skb->protocol), skb->len, skb->pkt_type);
1235 napi_gro_receive(&rq->napi, skb);
1239 dev->stats.rx_frame_errors++;
1243 /* Unlike mergeable buffers, all buffers are allocated to the
1244 * same size, except for the headroom. For this reason we do
1245 * not need to use mergeable_len_to_ctx here - it is enough
1246 * to store the headroom as the context ignoring the truesize.
1248 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1251 struct page_frag *alloc_frag = &rq->alloc_frag;
1253 unsigned int xdp_headroom = virtnet_get_headroom(vi);
1254 void *ctx = (void *)(unsigned long)xdp_headroom;
1255 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
1258 len = SKB_DATA_ALIGN(len) +
1259 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1260 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
1263 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1264 get_page(alloc_frag->page);
1265 alloc_frag->offset += len;
1266 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1267 vi->hdr_len + GOOD_PACKET_LEN);
1268 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1270 put_page(virt_to_head_page(buf));
1274 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1277 struct page *first, *list = NULL;
1281 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1283 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
1284 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
1285 first = get_a_page(rq, gfp);
1288 give_pages(rq, list);
1291 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
1293 /* chain new page in list head to match sg */
1294 first->private = (unsigned long)list;
1298 first = get_a_page(rq, gfp);
1300 give_pages(rq, list);
1303 p = page_address(first);
1305 /* rq->sg[0], rq->sg[1] share the same page */
1306 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1307 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
1309 /* rq->sg[1] for data packet, from offset */
1310 offset = sizeof(struct padded_vnet_hdr);
1311 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
1313 /* chain first in list head */
1314 first->private = (unsigned long)list;
1315 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1318 give_pages(rq, first);
1323 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
1324 struct ewma_pkt_len *avg_pkt_len,
1327 struct virtnet_info *vi = rq->vq->vdev->priv;
1328 const size_t hdr_len = vi->hdr_len;
1332 return PAGE_SIZE - room;
1334 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1335 rq->min_buf_len, PAGE_SIZE - hdr_len);
1337 return ALIGN(len, L1_CACHE_BYTES);
1340 static int add_recvbuf_mergeable(struct virtnet_info *vi,
1341 struct receive_queue *rq, gfp_t gfp)
1343 struct page_frag *alloc_frag = &rq->alloc_frag;
1344 unsigned int headroom = virtnet_get_headroom(vi);
1345 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1346 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1350 unsigned int len, hole;
1352 /* Extra tailroom is needed to satisfy XDP's assumption. This
1353 * means rx frags coalescing won't work, but consider we've
1354 * disabled GSO for XDP, it won't be a big issue.
1356 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1357 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
1360 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1361 buf += headroom; /* advance address leaving hole at front of pkt */
1362 get_page(alloc_frag->page);
1363 alloc_frag->offset += len + room;
1364 hole = alloc_frag->size - alloc_frag->offset;
1365 if (hole < len + room) {
1366 /* To avoid internal fragmentation, if there is very likely not
1367 * enough space for another buffer, add the remaining space to
1368 * the current buffer.
1371 alloc_frag->offset += hole;
1374 sg_init_one(rq->sg, buf, len);
1375 ctx = mergeable_len_to_ctx(len, headroom);
1376 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1378 put_page(virt_to_head_page(buf));
1384 * Returns false if we couldn't fill entirely (OOM).
1386 * Normally run in the receive path, but can also be run from ndo_open
1387 * before we're receiving packets, or from refill_work which is
1388 * careful to disable receiving (using napi_disable).
1390 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1397 if (vi->mergeable_rx_bufs)
1398 err = add_recvbuf_mergeable(vi, rq, gfp);
1399 else if (vi->big_packets)
1400 err = add_recvbuf_big(vi, rq, gfp);
1402 err = add_recvbuf_small(vi, rq, gfp);
1404 oom = err == -ENOMEM;
1407 } while (rq->vq->num_free);
1408 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1409 unsigned long flags;
1411 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
1413 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
1419 static void skb_recv_done(struct virtqueue *rvq)
1421 struct virtnet_info *vi = rvq->vdev->priv;
1422 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1424 virtqueue_napi_schedule(&rq->napi, rvq);
1427 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
1431 /* If all buffers were filled by other side before we napi_enabled, we
1432 * won't get another interrupt, so process any outstanding packets now.
1433 * Call local_bh_enable after to trigger softIRQ processing.
1436 virtqueue_napi_schedule(napi, vq);
1440 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1441 struct virtqueue *vq,
1442 struct napi_struct *napi)
1447 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1448 * enable the feature if this is likely affine with the transmit path.
1450 if (!vi->affinity_hint_set) {
1455 return virtnet_napi_enable(vq, napi);
1458 static void virtnet_napi_tx_disable(struct napi_struct *napi)
1464 static void refill_work(struct work_struct *work)
1466 struct virtnet_info *vi =
1467 container_of(work, struct virtnet_info, refill.work);
1471 for (i = 0; i < vi->curr_queue_pairs; i++) {
1472 struct receive_queue *rq = &vi->rq[i];
1474 napi_disable(&rq->napi);
1475 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1476 virtnet_napi_enable(rq->vq, &rq->napi);
1478 /* In theory, this can happen: if we don't get any buffers in
1479 * we will *never* try to fill again.
1482 schedule_delayed_work(&vi->refill, HZ/2);
1486 static int virtnet_receive(struct receive_queue *rq, int budget,
1487 unsigned int *xdp_xmit)
1489 struct virtnet_info *vi = rq->vq->vdev->priv;
1490 struct virtnet_rq_stats stats = {};
1495 if (!vi->big_packets || vi->mergeable_rx_bufs) {
1498 while (stats.packets < budget &&
1499 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1500 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1504 while (stats.packets < budget &&
1505 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1506 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1511 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
1512 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
1513 schedule_delayed_work(&vi->refill, 0);
1516 u64_stats_update_begin(&rq->stats.syncp);
1517 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1518 size_t offset = virtnet_rq_stats_desc[i].offset;
1521 item = (u64 *)((u8 *)&rq->stats + offset);
1522 *item += *(u64 *)((u8 *)&stats + offset);
1524 u64_stats_update_end(&rq->stats.syncp);
1526 return stats.packets;
1529 static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
1532 unsigned int packets = 0;
1533 unsigned int bytes = 0;
1536 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1537 if (likely(!is_xdp_frame(ptr))) {
1538 struct sk_buff *skb = ptr;
1540 pr_debug("Sent skb %p\n", skb);
1543 napi_consume_skb(skb, in_napi);
1545 struct xdp_frame *frame = ptr_to_xdp(ptr);
1547 bytes += frame->len;
1548 xdp_return_frame(frame);
1553 /* Avoid overhead when no packets have been processed
1554 * happens when called speculatively from start_xmit.
1559 u64_stats_update_begin(&sq->stats.syncp);
1560 sq->stats.bytes += bytes;
1561 sq->stats.packets += packets;
1562 u64_stats_update_end(&sq->stats.syncp);
1565 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1567 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1569 else if (q < vi->curr_queue_pairs)
1575 static void virtnet_poll_cleantx(struct receive_queue *rq)
1577 struct virtnet_info *vi = rq->vq->vdev->priv;
1578 unsigned int index = vq2rxq(rq->vq);
1579 struct send_queue *sq = &vi->sq[index];
1580 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1582 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
1585 if (__netif_tx_trylock(txq)) {
1587 virtqueue_disable_cb(sq->vq);
1588 free_old_xmit_skbs(sq, true);
1589 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1591 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1592 netif_tx_wake_queue(txq);
1594 __netif_tx_unlock(txq);
1598 static int virtnet_poll(struct napi_struct *napi, int budget)
1600 struct receive_queue *rq =
1601 container_of(napi, struct receive_queue, napi);
1602 struct virtnet_info *vi = rq->vq->vdev->priv;
1603 struct send_queue *sq;
1604 unsigned int received;
1605 unsigned int xdp_xmit = 0;
1607 virtnet_poll_cleantx(rq);
1609 received = virtnet_receive(rq, budget, &xdp_xmit);
1611 /* Out of packets? */
1612 if (received < budget)
1613 virtqueue_napi_complete(napi, rq->vq, received);
1615 if (xdp_xmit & VIRTIO_XDP_REDIR)
1618 if (xdp_xmit & VIRTIO_XDP_TX) {
1619 sq = virtnet_xdp_get_sq(vi);
1620 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1621 u64_stats_update_begin(&sq->stats.syncp);
1623 u64_stats_update_end(&sq->stats.syncp);
1625 virtnet_xdp_put_sq(vi, sq);
1631 static int virtnet_open(struct net_device *dev)
1633 struct virtnet_info *vi = netdev_priv(dev);
1636 for (i = 0; i < vi->max_queue_pairs; i++) {
1637 if (i < vi->curr_queue_pairs)
1638 /* Make sure we have some buffers: if oom use wq. */
1639 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1640 schedule_delayed_work(&vi->refill, 0);
1642 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
1646 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1647 MEM_TYPE_PAGE_SHARED, NULL);
1649 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1653 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1654 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1660 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1662 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1663 struct virtnet_info *vi = sq->vq->vdev->priv;
1664 unsigned int index = vq2txq(sq->vq);
1665 struct netdev_queue *txq;
1669 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1670 /* We don't need to enable cb for XDP */
1671 napi_complete_done(napi, 0);
1675 txq = netdev_get_tx_queue(vi->dev, index);
1676 __netif_tx_lock(txq, raw_smp_processor_id());
1677 virtqueue_disable_cb(sq->vq);
1678 free_old_xmit_skbs(sq, true);
1680 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1681 netif_tx_wake_queue(txq);
1683 opaque = virtqueue_enable_cb_prepare(sq->vq);
1685 done = napi_complete_done(napi, 0);
1688 virtqueue_disable_cb(sq->vq);
1690 __netif_tx_unlock(txq);
1693 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
1694 if (napi_schedule_prep(napi)) {
1695 __netif_tx_lock(txq, raw_smp_processor_id());
1696 virtqueue_disable_cb(sq->vq);
1697 __netif_tx_unlock(txq);
1698 __napi_schedule(napi);
1706 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1708 struct virtio_net_hdr_mrg_rxbuf *hdr;
1709 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1710 struct virtnet_info *vi = sq->vq->vdev->priv;
1712 unsigned hdr_len = vi->hdr_len;
1715 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1717 can_push = vi->any_header_sg &&
1718 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1719 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1720 /* Even if we can, don't push here yet as this would skew
1721 * csum_start offset below. */
1723 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1725 hdr = skb_vnet_hdr(skb);
1727 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1728 virtio_is_little_endian(vi->vdev), false,
1732 if (vi->mergeable_rx_bufs)
1733 hdr->num_buffers = 0;
1735 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1737 __skb_push(skb, hdr_len);
1738 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1739 if (unlikely(num_sg < 0))
1741 /* Pull header back to avoid skew in tx bytes calculations. */
1742 __skb_pull(skb, hdr_len);
1744 sg_set_buf(sq->sg, hdr, hdr_len);
1745 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1746 if (unlikely(num_sg < 0))
1750 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
1753 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
1755 struct virtnet_info *vi = netdev_priv(dev);
1756 int qnum = skb_get_queue_mapping(skb);
1757 struct send_queue *sq = &vi->sq[qnum];
1759 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1760 bool kick = !netdev_xmit_more();
1761 bool use_napi = sq->napi.weight;
1763 /* Free up any pending old buffers before queueing new ones. */
1766 virtqueue_disable_cb(sq->vq);
1768 free_old_xmit_skbs(sq, false);
1770 } while (use_napi && kick &&
1771 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1773 /* timestamp packet in software */
1774 skb_tx_timestamp(skb);
1776 /* Try to transmit */
1777 err = xmit_skb(sq, skb);
1779 /* This should not happen! */
1780 if (unlikely(err)) {
1781 dev->stats.tx_fifo_errors++;
1782 if (net_ratelimit())
1784 "Unexpected TXQ (%d) queue failure: %d\n",
1786 dev->stats.tx_dropped++;
1787 dev_kfree_skb_any(skb);
1788 return NETDEV_TX_OK;
1791 /* Don't wait up for transmitted skbs to be freed. */
1797 /* If running out of space, stop queue to avoid getting packets that we
1798 * are then unable to transmit.
1799 * An alternative would be to force queuing layer to requeue the skb by
1800 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1801 * returned in a normal path of operation: it means that driver is not
1802 * maintaining the TX queue stop/start state properly, and causes
1803 * the stack to do a non-trivial amount of useless work.
1804 * Since most packets only take 1 or 2 ring slots, stopping the queue
1805 * early means 16 slots are typically wasted.
1807 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1808 netif_stop_subqueue(dev, qnum);
1810 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1811 /* More just got used, free them then recheck. */
1812 free_old_xmit_skbs(sq, false);
1813 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1814 netif_start_subqueue(dev, qnum);
1815 virtqueue_disable_cb(sq->vq);
1820 if (kick || netif_xmit_stopped(txq)) {
1821 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1822 u64_stats_update_begin(&sq->stats.syncp);
1824 u64_stats_update_end(&sq->stats.syncp);
1828 return NETDEV_TX_OK;
1832 * Send command via the control virtqueue and check status. Commands
1833 * supported by the hypervisor, as indicated by feature bits, should
1834 * never fail unless improperly formatted.
1836 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1837 struct scatterlist *out)
1839 struct scatterlist *sgs[4], hdr, stat;
1840 unsigned out_num = 0, tmp;
1843 /* Caller should know better */
1844 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
1846 vi->ctrl->status = ~0;
1847 vi->ctrl->hdr.class = class;
1848 vi->ctrl->hdr.cmd = cmd;
1850 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
1851 sgs[out_num++] = &hdr;
1854 sgs[out_num++] = out;
1856 /* Add return status. */
1857 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
1858 sgs[out_num] = &stat;
1860 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1861 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1863 dev_warn(&vi->vdev->dev,
1864 "Failed to add sgs for command vq: %d\n.", ret);
1868 if (unlikely(!virtqueue_kick(vi->cvq)))
1869 return vi->ctrl->status == VIRTIO_NET_OK;
1871 /* Spin for a response, the kick causes an ioport write, trapping
1872 * into the hypervisor, so the request should be handled immediately.
1874 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1875 !virtqueue_is_broken(vi->cvq))
1878 return vi->ctrl->status == VIRTIO_NET_OK;
1881 static int virtnet_set_mac_address(struct net_device *dev, void *p)
1883 struct virtnet_info *vi = netdev_priv(dev);
1884 struct virtio_device *vdev = vi->vdev;
1886 struct sockaddr *addr;
1887 struct scatterlist sg;
1889 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1892 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
1896 ret = eth_prepare_mac_addr_change(dev, addr);
1900 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1901 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1902 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1903 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
1904 dev_warn(&vdev->dev,
1905 "Failed to set mac address by vq command.\n");
1909 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1910 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1913 /* Naturally, this has an atomicity problem. */
1914 for (i = 0; i < dev->addr_len; i++)
1915 virtio_cwrite8(vdev,
1916 offsetof(struct virtio_net_config, mac) +
1917 i, addr->sa_data[i]);
1920 eth_commit_mac_addr_change(dev, p);
1928 static void virtnet_stats(struct net_device *dev,
1929 struct rtnl_link_stats64 *tot)
1931 struct virtnet_info *vi = netdev_priv(dev);
1935 for (i = 0; i < vi->max_queue_pairs; i++) {
1936 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
1937 struct receive_queue *rq = &vi->rq[i];
1938 struct send_queue *sq = &vi->sq[i];
1941 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1942 tpackets = sq->stats.packets;
1943 tbytes = sq->stats.bytes;
1944 terrors = sq->stats.tx_timeouts;
1945 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
1948 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
1949 rpackets = rq->stats.packets;
1950 rbytes = rq->stats.bytes;
1951 rdrops = rq->stats.drops;
1952 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
1954 tot->rx_packets += rpackets;
1955 tot->tx_packets += tpackets;
1956 tot->rx_bytes += rbytes;
1957 tot->tx_bytes += tbytes;
1958 tot->rx_dropped += rdrops;
1959 tot->tx_errors += terrors;
1962 tot->tx_dropped = dev->stats.tx_dropped;
1963 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
1964 tot->rx_length_errors = dev->stats.rx_length_errors;
1965 tot->rx_frame_errors = dev->stats.rx_frame_errors;
1968 static void virtnet_ack_link_announce(struct virtnet_info *vi)
1971 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1972 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1973 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1977 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1979 struct scatterlist sg;
1980 struct net_device *dev = vi->dev;
1982 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1985 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1986 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
1988 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1989 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1990 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1994 vi->curr_queue_pairs = queue_pairs;
1995 /* virtnet_open() will refill when device is going to up. */
1996 if (dev->flags & IFF_UP)
1997 schedule_delayed_work(&vi->refill, 0);
2003 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2008 err = _virtnet_set_queues(vi, queue_pairs);
2013 static int virtnet_close(struct net_device *dev)
2015 struct virtnet_info *vi = netdev_priv(dev);
2018 /* Make sure refill_work doesn't re-enable napi! */
2019 cancel_delayed_work_sync(&vi->refill);
2021 for (i = 0; i < vi->max_queue_pairs; i++) {
2022 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
2023 napi_disable(&vi->rq[i].napi);
2024 virtnet_napi_tx_disable(&vi->sq[i].napi);
2030 static void virtnet_set_rx_mode(struct net_device *dev)
2032 struct virtnet_info *vi = netdev_priv(dev);
2033 struct scatterlist sg[2];
2034 struct virtio_net_ctrl_mac *mac_data;
2035 struct netdev_hw_addr *ha;
2041 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
2042 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
2045 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
2046 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2048 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
2050 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2051 VIRTIO_NET_CTRL_RX_PROMISC, sg))
2052 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
2053 vi->ctrl->promisc ? "en" : "dis");
2055 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
2057 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2058 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2059 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2060 vi->ctrl->allmulti ? "en" : "dis");
2062 uc_count = netdev_uc_count(dev);
2063 mc_count = netdev_mc_count(dev);
2064 /* MAC filter - use one buffer for both lists */
2065 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2066 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
2071 sg_init_table(sg, 2);
2073 /* Store the unicast list and count in the front of the buffer */
2074 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2076 netdev_for_each_uc_addr(ha, dev)
2077 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2079 sg_set_buf(&sg[0], mac_data,
2080 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2082 /* multicast list and count fill the end */
2083 mac_data = (void *)&mac_data->macs[uc_count][0];
2085 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2087 netdev_for_each_mc_addr(ha, dev)
2088 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2090 sg_set_buf(&sg[1], mac_data,
2091 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2093 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2094 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
2095 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2100 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2101 __be16 proto, u16 vid)
2103 struct virtnet_info *vi = netdev_priv(dev);
2104 struct scatterlist sg;
2106 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2107 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2109 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2110 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
2111 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
2115 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2116 __be16 proto, u16 vid)
2118 struct virtnet_info *vi = netdev_priv(dev);
2119 struct scatterlist sg;
2121 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2122 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2124 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2125 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
2126 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
2130 static void virtnet_clean_affinity(struct virtnet_info *vi)
2134 if (vi->affinity_hint_set) {
2135 for (i = 0; i < vi->max_queue_pairs; i++) {
2136 virtqueue_set_affinity(vi->rq[i].vq, NULL);
2137 virtqueue_set_affinity(vi->sq[i].vq, NULL);
2140 vi->affinity_hint_set = false;
2144 static void virtnet_set_affinity(struct virtnet_info *vi)
2153 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2154 virtnet_clean_affinity(vi);
2158 num_cpu = num_online_cpus();
2159 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2160 stragglers = num_cpu >= vi->curr_queue_pairs ?
2161 num_cpu % vi->curr_queue_pairs :
2163 cpu = cpumask_first(cpu_online_mask);
2165 for (i = 0; i < vi->curr_queue_pairs; i++) {
2166 group_size = stride + (i < stragglers ? 1 : 0);
2168 for (j = 0; j < group_size; j++) {
2169 cpumask_set_cpu(cpu, mask);
2170 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2173 virtqueue_set_affinity(vi->rq[i].vq, mask);
2174 virtqueue_set_affinity(vi->sq[i].vq, mask);
2175 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
2176 cpumask_clear(mask);
2179 vi->affinity_hint_set = true;
2180 free_cpumask_var(mask);
2183 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
2185 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2187 virtnet_set_affinity(vi);
2191 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2193 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2195 virtnet_set_affinity(vi);
2199 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2201 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2204 virtnet_clean_affinity(vi);
2208 static enum cpuhp_state virtionet_online;
2210 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2214 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2217 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2221 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2225 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2227 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2228 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2232 static void virtnet_get_ringparam(struct net_device *dev,
2233 struct ethtool_ringparam *ring,
2234 struct kernel_ethtool_ringparam *kernel_ring,
2235 struct netlink_ext_ack *extack)
2237 struct virtnet_info *vi = netdev_priv(dev);
2239 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2240 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2241 ring->rx_pending = ring->rx_max_pending;
2242 ring->tx_pending = ring->tx_max_pending;
2245 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
2247 struct net_device *dev = vi->dev;
2248 struct scatterlist sgs[4];
2249 unsigned int sg_buf_size;
2252 sg_init_table(sgs, 4);
2254 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
2255 sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
2257 sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
2258 sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
2260 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
2261 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
2262 sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
2264 sg_buf_size = vi->rss_key_size;
2265 sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
2267 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2268 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
2269 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
2270 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
2276 static void virtnet_init_default_rss(struct virtnet_info *vi)
2281 vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
2282 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
2283 vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
2284 ? vi->rss_indir_table_size - 1 : 0;
2285 vi->ctrl->rss.unclassified_queue = 0;
2287 for (; i < vi->rss_indir_table_size; ++i) {
2288 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
2289 vi->ctrl->rss.indirection_table[i] = indir_val;
2292 vi->ctrl->rss.max_tx_vq = vi->curr_queue_pairs;
2293 vi->ctrl->rss.hash_key_length = vi->rss_key_size;
2295 netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
2298 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
2301 switch (info->flow_type) {
2303 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
2304 info->data = RXH_IP_SRC | RXH_IP_DST |
2305 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2306 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2307 info->data = RXH_IP_SRC | RXH_IP_DST;
2311 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
2312 info->data = RXH_IP_SRC | RXH_IP_DST |
2313 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2314 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2315 info->data = RXH_IP_SRC | RXH_IP_DST;
2319 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
2320 info->data = RXH_IP_SRC | RXH_IP_DST |
2321 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2322 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2323 info->data = RXH_IP_SRC | RXH_IP_DST;
2327 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
2328 info->data = RXH_IP_SRC | RXH_IP_DST |
2329 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2330 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2331 info->data = RXH_IP_SRC | RXH_IP_DST;
2335 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
2336 info->data = RXH_IP_SRC | RXH_IP_DST;
2340 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
2341 info->data = RXH_IP_SRC | RXH_IP_DST;
2350 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
2352 u32 new_hashtypes = vi->rss_hash_types_saved;
2353 bool is_disable = info->data & RXH_DISCARD;
2354 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
2356 /* supports only 'sd', 'sdfn' and 'r' */
2357 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
2360 switch (info->flow_type) {
2362 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
2364 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2365 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
2368 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
2370 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2371 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
2374 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2376 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2379 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
2381 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2382 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
2385 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
2387 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2388 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
2391 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2393 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2396 /* unsupported flow */
2400 /* if unsupported hashtype was set */
2401 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
2404 if (new_hashtypes != vi->rss_hash_types_saved) {
2405 vi->rss_hash_types_saved = new_hashtypes;
2406 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
2407 if (vi->dev->features & NETIF_F_RXHASH)
2408 return virtnet_commit_rss_command(vi);
2414 static void virtnet_get_drvinfo(struct net_device *dev,
2415 struct ethtool_drvinfo *info)
2417 struct virtnet_info *vi = netdev_priv(dev);
2418 struct virtio_device *vdev = vi->vdev;
2420 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2421 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2422 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
2426 /* TODO: Eliminate OOO packets during switching */
2427 static int virtnet_set_channels(struct net_device *dev,
2428 struct ethtool_channels *channels)
2430 struct virtnet_info *vi = netdev_priv(dev);
2431 u16 queue_pairs = channels->combined_count;
2434 /* We don't support separate rx/tx channels.
2435 * We don't allow setting 'other' channels.
2437 if (channels->rx_count || channels->tx_count || channels->other_count)
2440 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
2443 /* For now we don't support modifying channels while XDP is loaded
2444 * also when XDP is loaded all RX queues have XDP programs so we only
2445 * need to check a single RX queue.
2447 if (vi->rq[0].xdp_prog)
2451 err = _virtnet_set_queues(vi, queue_pairs);
2456 virtnet_set_affinity(vi);
2459 netif_set_real_num_tx_queues(dev, queue_pairs);
2460 netif_set_real_num_rx_queues(dev, queue_pairs);
2465 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2467 struct virtnet_info *vi = netdev_priv(dev);
2471 switch (stringset) {
2473 for (i = 0; i < vi->curr_queue_pairs; i++) {
2474 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2475 ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2476 virtnet_rq_stats_desc[j].desc);
2479 for (i = 0; i < vi->curr_queue_pairs; i++) {
2480 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2481 ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2482 virtnet_sq_stats_desc[j].desc);
2488 static int virtnet_get_sset_count(struct net_device *dev, int sset)
2490 struct virtnet_info *vi = netdev_priv(dev);
2494 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2495 VIRTNET_SQ_STATS_LEN);
2501 static void virtnet_get_ethtool_stats(struct net_device *dev,
2502 struct ethtool_stats *stats, u64 *data)
2504 struct virtnet_info *vi = netdev_priv(dev);
2505 unsigned int idx = 0, start, i, j;
2506 const u8 *stats_base;
2509 for (i = 0; i < vi->curr_queue_pairs; i++) {
2510 struct receive_queue *rq = &vi->rq[i];
2512 stats_base = (u8 *)&rq->stats;
2514 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2515 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2516 offset = virtnet_rq_stats_desc[j].offset;
2517 data[idx + j] = *(u64 *)(stats_base + offset);
2519 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2520 idx += VIRTNET_RQ_STATS_LEN;
2523 for (i = 0; i < vi->curr_queue_pairs; i++) {
2524 struct send_queue *sq = &vi->sq[i];
2526 stats_base = (u8 *)&sq->stats;
2528 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2529 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2530 offset = virtnet_sq_stats_desc[j].offset;
2531 data[idx + j] = *(u64 *)(stats_base + offset);
2533 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2534 idx += VIRTNET_SQ_STATS_LEN;
2538 static void virtnet_get_channels(struct net_device *dev,
2539 struct ethtool_channels *channels)
2541 struct virtnet_info *vi = netdev_priv(dev);
2543 channels->combined_count = vi->curr_queue_pairs;
2544 channels->max_combined = vi->max_queue_pairs;
2545 channels->max_other = 0;
2546 channels->rx_count = 0;
2547 channels->tx_count = 0;
2548 channels->other_count = 0;
2551 static int virtnet_set_link_ksettings(struct net_device *dev,
2552 const struct ethtool_link_ksettings *cmd)
2554 struct virtnet_info *vi = netdev_priv(dev);
2556 return ethtool_virtdev_set_link_ksettings(dev, cmd,
2557 &vi->speed, &vi->duplex);
2560 static int virtnet_get_link_ksettings(struct net_device *dev,
2561 struct ethtool_link_ksettings *cmd)
2563 struct virtnet_info *vi = netdev_priv(dev);
2565 cmd->base.speed = vi->speed;
2566 cmd->base.duplex = vi->duplex;
2567 cmd->base.port = PORT_OTHER;
2572 static int virtnet_set_coalesce(struct net_device *dev,
2573 struct ethtool_coalesce *ec,
2574 struct kernel_ethtool_coalesce *kernel_coal,
2575 struct netlink_ext_ack *extack)
2577 struct virtnet_info *vi = netdev_priv(dev);
2580 if (ec->tx_max_coalesced_frames > 1 ||
2581 ec->rx_max_coalesced_frames != 1)
2584 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
2585 if (napi_weight ^ vi->sq[0].napi.weight) {
2586 if (dev->flags & IFF_UP)
2588 for (i = 0; i < vi->max_queue_pairs; i++)
2589 vi->sq[i].napi.weight = napi_weight;
2595 static int virtnet_get_coalesce(struct net_device *dev,
2596 struct ethtool_coalesce *ec,
2597 struct kernel_ethtool_coalesce *kernel_coal,
2598 struct netlink_ext_ack *extack)
2600 struct ethtool_coalesce ec_default = {
2601 .cmd = ETHTOOL_GCOALESCE,
2602 .rx_max_coalesced_frames = 1,
2604 struct virtnet_info *vi = netdev_priv(dev);
2606 memcpy(ec, &ec_default, sizeof(ec_default));
2608 if (vi->sq[0].napi.weight)
2609 ec->tx_max_coalesced_frames = 1;
2614 static void virtnet_init_settings(struct net_device *dev)
2616 struct virtnet_info *vi = netdev_priv(dev);
2618 vi->speed = SPEED_UNKNOWN;
2619 vi->duplex = DUPLEX_UNKNOWN;
2622 static void virtnet_update_settings(struct virtnet_info *vi)
2627 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2630 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
2632 if (ethtool_validate_speed(speed))
2635 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
2637 if (ethtool_validate_duplex(duplex))
2638 vi->duplex = duplex;
2641 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
2643 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
2646 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
2648 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
2651 static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
2653 struct virtnet_info *vi = netdev_priv(dev);
2657 for (i = 0; i < vi->rss_indir_table_size; ++i)
2658 indir[i] = vi->ctrl->rss.indirection_table[i];
2662 memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
2665 *hfunc = ETH_RSS_HASH_TOP;
2670 static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
2672 struct virtnet_info *vi = netdev_priv(dev);
2675 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
2679 for (i = 0; i < vi->rss_indir_table_size; ++i)
2680 vi->ctrl->rss.indirection_table[i] = indir[i];
2683 memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
2685 virtnet_commit_rss_command(vi);
2690 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
2692 struct virtnet_info *vi = netdev_priv(dev);
2695 switch (info->cmd) {
2696 case ETHTOOL_GRXRINGS:
2697 info->data = vi->curr_queue_pairs;
2700 virtnet_get_hashflow(vi, info);
2709 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
2711 struct virtnet_info *vi = netdev_priv(dev);
2714 switch (info->cmd) {
2716 if (!virtnet_set_hashflow(vi, info))
2727 static const struct ethtool_ops virtnet_ethtool_ops = {
2728 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
2729 .get_drvinfo = virtnet_get_drvinfo,
2730 .get_link = ethtool_op_get_link,
2731 .get_ringparam = virtnet_get_ringparam,
2732 .get_strings = virtnet_get_strings,
2733 .get_sset_count = virtnet_get_sset_count,
2734 .get_ethtool_stats = virtnet_get_ethtool_stats,
2735 .set_channels = virtnet_set_channels,
2736 .get_channels = virtnet_get_channels,
2737 .get_ts_info = ethtool_op_get_ts_info,
2738 .get_link_ksettings = virtnet_get_link_ksettings,
2739 .set_link_ksettings = virtnet_set_link_ksettings,
2740 .set_coalesce = virtnet_set_coalesce,
2741 .get_coalesce = virtnet_get_coalesce,
2742 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
2743 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
2744 .get_rxfh = virtnet_get_rxfh,
2745 .set_rxfh = virtnet_set_rxfh,
2746 .get_rxnfc = virtnet_get_rxnfc,
2747 .set_rxnfc = virtnet_set_rxnfc,
2750 static void virtnet_freeze_down(struct virtio_device *vdev)
2752 struct virtnet_info *vi = vdev->priv;
2755 /* Make sure no work handler is accessing the device */
2756 flush_work(&vi->config_work);
2758 netif_tx_lock_bh(vi->dev);
2759 netif_device_detach(vi->dev);
2760 netif_tx_unlock_bh(vi->dev);
2761 cancel_delayed_work_sync(&vi->refill);
2763 if (netif_running(vi->dev)) {
2764 for (i = 0; i < vi->max_queue_pairs; i++) {
2765 napi_disable(&vi->rq[i].napi);
2766 virtnet_napi_tx_disable(&vi->sq[i].napi);
2771 static int init_vqs(struct virtnet_info *vi);
2773 static int virtnet_restore_up(struct virtio_device *vdev)
2775 struct virtnet_info *vi = vdev->priv;
2782 virtio_device_ready(vdev);
2784 if (netif_running(vi->dev)) {
2785 for (i = 0; i < vi->curr_queue_pairs; i++)
2786 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2787 schedule_delayed_work(&vi->refill, 0);
2789 for (i = 0; i < vi->max_queue_pairs; i++) {
2790 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2791 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2796 netif_tx_lock_bh(vi->dev);
2797 netif_device_attach(vi->dev);
2798 netif_tx_unlock_bh(vi->dev);
2802 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2804 struct scatterlist sg;
2805 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
2807 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
2809 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2810 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
2811 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
2818 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2822 if (!vi->guest_offloads)
2825 return virtnet_set_guest_offloads(vi, offloads);
2828 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2830 u64 offloads = vi->guest_offloads;
2832 if (!vi->guest_offloads)
2835 return virtnet_set_guest_offloads(vi, offloads);
2838 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2839 struct netlink_ext_ack *extack)
2841 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2842 struct virtnet_info *vi = netdev_priv(dev);
2843 struct bpf_prog *old_prog;
2844 u16 xdp_qp = 0, curr_qp;
2847 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2848 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2849 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2850 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
2851 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
2852 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
2853 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
2857 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
2858 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
2862 if (dev->mtu > max_sz) {
2863 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
2864 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2868 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2870 xdp_qp = nr_cpu_ids;
2872 /* XDP requires extra queues for XDP_TX */
2873 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
2874 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",
2875 curr_qp + xdp_qp, vi->max_queue_pairs);
2879 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
2880 if (!prog && !old_prog)
2884 bpf_prog_add(prog, vi->max_queue_pairs - 1);
2886 /* Make sure NAPI is not using any XDP TX queues for RX. */
2887 if (netif_running(dev)) {
2888 for (i = 0; i < vi->max_queue_pairs; i++) {
2889 napi_disable(&vi->rq[i].napi);
2890 virtnet_napi_tx_disable(&vi->sq[i].napi);
2895 for (i = 0; i < vi->max_queue_pairs; i++) {
2896 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2898 virtnet_restore_guest_offloads(vi);
2903 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2906 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
2907 vi->xdp_queue_pairs = xdp_qp;
2910 vi->xdp_enabled = true;
2911 for (i = 0; i < vi->max_queue_pairs; i++) {
2912 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2913 if (i == 0 && !old_prog)
2914 virtnet_clear_guest_offloads(vi);
2917 vi->xdp_enabled = false;
2920 for (i = 0; i < vi->max_queue_pairs; i++) {
2922 bpf_prog_put(old_prog);
2923 if (netif_running(dev)) {
2924 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2925 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2934 virtnet_clear_guest_offloads(vi);
2935 for (i = 0; i < vi->max_queue_pairs; i++)
2936 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
2939 if (netif_running(dev)) {
2940 for (i = 0; i < vi->max_queue_pairs; i++) {
2941 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2942 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2947 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2951 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2953 switch (xdp->command) {
2954 case XDP_SETUP_PROG:
2955 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
2961 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2964 struct virtnet_info *vi = netdev_priv(dev);
2967 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2970 ret = snprintf(buf, len, "sby");
2977 static int virtnet_set_features(struct net_device *dev,
2978 netdev_features_t features)
2980 struct virtnet_info *vi = netdev_priv(dev);
2984 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
2985 if (vi->xdp_enabled)
2988 if (features & NETIF_F_GRO_HW)
2989 offloads = vi->guest_offloads_capable;
2991 offloads = vi->guest_offloads_capable &
2992 ~GUEST_OFFLOAD_GRO_HW_MASK;
2994 err = virtnet_set_guest_offloads(vi, offloads);
2997 vi->guest_offloads = offloads;
3000 if ((dev->features ^ features) & NETIF_F_RXHASH) {
3001 if (features & NETIF_F_RXHASH)
3002 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3004 vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
3006 if (!virtnet_commit_rss_command(vi))
3013 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
3015 struct virtnet_info *priv = netdev_priv(dev);
3016 struct send_queue *sq = &priv->sq[txqueue];
3017 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
3019 u64_stats_update_begin(&sq->stats.syncp);
3020 sq->stats.tx_timeouts++;
3021 u64_stats_update_end(&sq->stats.syncp);
3023 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
3024 txqueue, sq->name, sq->vq->index, sq->vq->name,
3025 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
3028 static const struct net_device_ops virtnet_netdev = {
3029 .ndo_open = virtnet_open,
3030 .ndo_stop = virtnet_close,
3031 .ndo_start_xmit = start_xmit,
3032 .ndo_validate_addr = eth_validate_addr,
3033 .ndo_set_mac_address = virtnet_set_mac_address,
3034 .ndo_set_rx_mode = virtnet_set_rx_mode,
3035 .ndo_get_stats64 = virtnet_stats,
3036 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
3037 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
3038 .ndo_bpf = virtnet_xdp,
3039 .ndo_xdp_xmit = virtnet_xdp_xmit,
3040 .ndo_features_check = passthru_features_check,
3041 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
3042 .ndo_set_features = virtnet_set_features,
3043 .ndo_tx_timeout = virtnet_tx_timeout,
3046 static void virtnet_config_changed_work(struct work_struct *work)
3048 struct virtnet_info *vi =
3049 container_of(work, struct virtnet_info, config_work);
3052 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
3053 struct virtio_net_config, status, &v) < 0)
3056 if (v & VIRTIO_NET_S_ANNOUNCE) {
3057 netdev_notify_peers(vi->dev);
3058 virtnet_ack_link_announce(vi);
3061 /* Ignore unknown (future) status bits */
3062 v &= VIRTIO_NET_S_LINK_UP;
3064 if (vi->status == v)
3069 if (vi->status & VIRTIO_NET_S_LINK_UP) {
3070 virtnet_update_settings(vi);
3071 netif_carrier_on(vi->dev);
3072 netif_tx_wake_all_queues(vi->dev);
3074 netif_carrier_off(vi->dev);
3075 netif_tx_stop_all_queues(vi->dev);
3079 static void virtnet_config_changed(struct virtio_device *vdev)
3081 struct virtnet_info *vi = vdev->priv;
3083 schedule_work(&vi->config_work);
3086 static void virtnet_free_queues(struct virtnet_info *vi)
3090 for (i = 0; i < vi->max_queue_pairs; i++) {
3091 __netif_napi_del(&vi->rq[i].napi);
3092 __netif_napi_del(&vi->sq[i].napi);
3095 /* We called __netif_napi_del(),
3096 * we need to respect an RCU grace period before freeing vi->rq
3105 static void _free_receive_bufs(struct virtnet_info *vi)
3107 struct bpf_prog *old_prog;
3110 for (i = 0; i < vi->max_queue_pairs; i++) {
3111 while (vi->rq[i].pages)
3112 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
3114 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
3115 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
3117 bpf_prog_put(old_prog);
3121 static void free_receive_bufs(struct virtnet_info *vi)
3124 _free_receive_bufs(vi);
3128 static void free_receive_page_frags(struct virtnet_info *vi)
3131 for (i = 0; i < vi->max_queue_pairs; i++)
3132 if (vi->rq[i].alloc_frag.page)
3133 put_page(vi->rq[i].alloc_frag.page);
3136 static void free_unused_bufs(struct virtnet_info *vi)
3141 for (i = 0; i < vi->max_queue_pairs; i++) {
3142 struct virtqueue *vq = vi->sq[i].vq;
3143 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
3144 if (!is_xdp_frame(buf))
3147 xdp_return_frame(ptr_to_xdp(buf));
3151 for (i = 0; i < vi->max_queue_pairs; i++) {
3152 struct virtqueue *vq = vi->rq[i].vq;
3154 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
3155 if (vi->mergeable_rx_bufs) {
3156 put_page(virt_to_head_page(buf));
3157 } else if (vi->big_packets) {
3158 give_pages(&vi->rq[i], buf);
3160 put_page(virt_to_head_page(buf));
3166 static void virtnet_del_vqs(struct virtnet_info *vi)
3168 struct virtio_device *vdev = vi->vdev;
3170 virtnet_clean_affinity(vi);
3172 vdev->config->del_vqs(vdev);
3174 virtnet_free_queues(vi);
3177 /* How large should a single buffer be so a queue full of these can fit at
3178 * least one full packet?
3179 * Logic below assumes the mergeable buffer header is used.
3181 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
3183 const unsigned int hdr_len = vi->hdr_len;
3184 unsigned int rq_size = virtqueue_get_vring_size(vq);
3185 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
3186 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
3187 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
3189 return max(max(min_buf_len, hdr_len) - hdr_len,
3190 (unsigned int)GOOD_PACKET_LEN);
3193 static int virtnet_find_vqs(struct virtnet_info *vi)
3195 vq_callback_t **callbacks;
3196 struct virtqueue **vqs;
3202 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
3203 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
3204 * possible control vq.
3206 total_vqs = vi->max_queue_pairs * 2 +
3207 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
3209 /* Allocate space for find_vqs parameters */
3210 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
3213 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
3216 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
3219 if (!vi->big_packets || vi->mergeable_rx_bufs) {
3220 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
3227 /* Parameters for control virtqueue, if any */
3229 callbacks[total_vqs - 1] = NULL;
3230 names[total_vqs - 1] = "control";
3233 /* Allocate/initialize parameters for send/receive virtqueues */
3234 for (i = 0; i < vi->max_queue_pairs; i++) {
3235 callbacks[rxq2vq(i)] = skb_recv_done;
3236 callbacks[txq2vq(i)] = skb_xmit_done;
3237 sprintf(vi->rq[i].name, "input.%d", i);
3238 sprintf(vi->sq[i].name, "output.%d", i);
3239 names[rxq2vq(i)] = vi->rq[i].name;
3240 names[txq2vq(i)] = vi->sq[i].name;
3242 ctx[rxq2vq(i)] = true;
3245 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
3251 vi->cvq = vqs[total_vqs - 1];
3252 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
3253 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3256 for (i = 0; i < vi->max_queue_pairs; i++) {
3257 vi->rq[i].vq = vqs[rxq2vq(i)];
3258 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
3259 vi->sq[i].vq = vqs[txq2vq(i)];
3262 /* run here: ret == 0. */
3277 static int virtnet_alloc_queues(struct virtnet_info *vi)
3282 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
3288 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
3291 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
3295 INIT_DELAYED_WORK(&vi->refill, refill_work);
3296 for (i = 0; i < vi->max_queue_pairs; i++) {
3297 vi->rq[i].pages = NULL;
3298 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
3300 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
3301 napi_tx ? napi_weight : 0);
3303 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
3304 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
3305 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
3307 u64_stats_init(&vi->rq[i].stats.syncp);
3308 u64_stats_init(&vi->sq[i].stats.syncp);
3321 static int init_vqs(struct virtnet_info *vi)
3325 /* Allocate send & receive queues */
3326 ret = virtnet_alloc_queues(vi);
3330 ret = virtnet_find_vqs(vi);
3335 virtnet_set_affinity(vi);
3341 virtnet_free_queues(vi);
3347 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
3350 struct virtnet_info *vi = netdev_priv(queue->dev);
3351 unsigned int queue_index = get_netdev_rx_queue_index(queue);
3352 unsigned int headroom = virtnet_get_headroom(vi);
3353 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
3354 struct ewma_pkt_len *avg;
3356 BUG_ON(queue_index >= vi->max_queue_pairs);
3357 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
3358 return sprintf(buf, "%u\n",
3359 get_mergeable_buf_len(&vi->rq[queue_index], avg,
3360 SKB_DATA_ALIGN(headroom + tailroom)));
3363 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
3364 __ATTR_RO(mergeable_rx_buffer_size);
3366 static struct attribute *virtio_net_mrg_rx_attrs[] = {
3367 &mergeable_rx_buffer_size_attribute.attr,
3371 static const struct attribute_group virtio_net_mrg_rx_group = {
3372 .name = "virtio_net",
3373 .attrs = virtio_net_mrg_rx_attrs
3377 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
3379 const char *fname, const char *dname)
3381 if (!virtio_has_feature(vdev, fbit))
3384 dev_err(&vdev->dev, "device advertises feature %s but not %s",
3390 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
3391 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
3393 static bool virtnet_validate_features(struct virtio_device *vdev)
3395 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
3396 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
3397 "VIRTIO_NET_F_CTRL_VQ") ||
3398 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
3399 "VIRTIO_NET_F_CTRL_VQ") ||
3400 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
3401 "VIRTIO_NET_F_CTRL_VQ") ||
3402 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
3403 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
3404 "VIRTIO_NET_F_CTRL_VQ") ||
3405 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
3406 "VIRTIO_NET_F_CTRL_VQ") ||
3407 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
3408 "VIRTIO_NET_F_CTRL_VQ"))) {
3415 #define MIN_MTU ETH_MIN_MTU
3416 #define MAX_MTU ETH_MAX_MTU
3418 static int virtnet_validate(struct virtio_device *vdev)
3420 if (!vdev->config->get) {
3421 dev_err(&vdev->dev, "%s failure: config access disabled\n",
3426 if (!virtnet_validate_features(vdev))
3429 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3430 int mtu = virtio_cread16(vdev,
3431 offsetof(struct virtio_net_config,
3434 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3440 static int virtnet_probe(struct virtio_device *vdev)
3442 int i, err = -ENOMEM;
3443 struct net_device *dev;
3444 struct virtnet_info *vi;
3445 u16 max_queue_pairs;
3448 /* Find if host supports multiqueue/rss virtio_net device */
3449 max_queue_pairs = 1;
3450 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3452 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
3454 /* We need at least 2 queue's */
3455 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3456 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3457 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3458 max_queue_pairs = 1;
3460 /* Allocate ourselves a network device with room for our info */
3461 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
3465 /* Set up network device as normal. */
3466 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3467 IFF_TX_SKB_NO_LINEAR;
3468 dev->netdev_ops = &virtnet_netdev;
3469 dev->features = NETIF_F_HIGHDMA;
3471 dev->ethtool_ops = &virtnet_ethtool_ops;
3472 SET_NETDEV_DEV(dev, &vdev->dev);
3474 /* Do we support "hardware" checksums? */
3475 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
3476 /* This opens up the world of extra features. */
3477 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3479 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3481 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
3482 dev->hw_features |= NETIF_F_TSO
3483 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
3485 /* Individual feature bits: what can host handle? */
3486 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
3487 dev->hw_features |= NETIF_F_TSO;
3488 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
3489 dev->hw_features |= NETIF_F_TSO6;
3490 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
3491 dev->hw_features |= NETIF_F_TSO_ECN;
3493 dev->features |= NETIF_F_GSO_ROBUST;
3496 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
3497 /* (!csum && gso) case will be fixed by register_netdev() */
3499 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
3500 dev->features |= NETIF_F_RXCSUM;
3501 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3502 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
3503 dev->features |= NETIF_F_GRO_HW;
3504 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
3505 dev->hw_features |= NETIF_F_GRO_HW;
3507 dev->vlan_features = dev->features;
3509 /* MTU range: 68 - 65535 */
3510 dev->min_mtu = MIN_MTU;
3511 dev->max_mtu = MAX_MTU;
3513 /* Configuration may specify what MAC to use. Otherwise random. */
3514 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
3517 virtio_cread_bytes(vdev,
3518 offsetof(struct virtio_net_config, mac),
3520 eth_hw_addr_set(dev, addr);
3522 eth_hw_addr_random(dev);
3525 /* Set up our device-specific information */
3526 vi = netdev_priv(dev);
3531 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
3533 /* If we can receive ANY GSO packets, we must allocate large ones. */
3534 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3535 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3536 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
3537 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
3538 vi->big_packets = true;
3540 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
3541 vi->mergeable_rx_bufs = true;
3543 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
3544 vi->has_rss_hash_report = true;
3546 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3549 if (vi->has_rss || vi->has_rss_hash_report) {
3550 vi->rss_indir_table_size =
3551 virtio_cread16(vdev, offsetof(struct virtio_net_config,
3552 rss_max_indirection_table_length));
3554 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
3556 vi->rss_hash_types_supported =
3557 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
3558 vi->rss_hash_types_supported &=
3559 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
3560 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
3561 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
3563 dev->hw_features |= NETIF_F_RXHASH;
3566 if (vi->has_rss_hash_report)
3567 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
3568 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
3569 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3570 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3572 vi->hdr_len = sizeof(struct virtio_net_hdr);
3574 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
3575 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3576 vi->any_header_sg = true;
3578 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3581 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3582 mtu = virtio_cread16(vdev,
3583 offsetof(struct virtio_net_config,
3585 if (mtu < dev->min_mtu) {
3586 /* Should never trigger: MTU was previously validated
3587 * in virtnet_validate.
3590 "device MTU appears to have changed it is now %d < %d",
3599 /* TODO: size buffers correctly in this case. */
3600 if (dev->mtu > ETH_DATA_LEN)
3601 vi->big_packets = true;
3604 if (vi->any_header_sg)
3605 dev->needed_headroom = vi->hdr_len;
3607 /* Enable multiqueue by default */
3608 if (num_online_cpus() >= max_queue_pairs)
3609 vi->curr_queue_pairs = max_queue_pairs;
3611 vi->curr_queue_pairs = num_online_cpus();
3612 vi->max_queue_pairs = max_queue_pairs;
3614 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
3620 if (vi->mergeable_rx_bufs)
3621 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3623 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
3624 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
3626 virtnet_init_settings(dev);
3628 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3629 vi->failover = net_failover_create(vi->dev);
3630 if (IS_ERR(vi->failover)) {
3631 err = PTR_ERR(vi->failover);
3636 if (vi->has_rss || vi->has_rss_hash_report)
3637 virtnet_init_default_rss(vi);
3639 err = register_netdev(dev);
3641 pr_debug("virtio_net: registering device failed\n");
3645 virtio_device_ready(vdev);
3647 err = virtnet_cpu_notif_add(vi);
3649 pr_debug("virtio_net: registering cpu notifier failed\n");
3650 goto free_unregister_netdev;
3653 virtnet_set_queues(vi, vi->curr_queue_pairs);
3655 /* Assume link up if device can't report link status,
3656 otherwise get link status from config. */
3657 netif_carrier_off(dev);
3658 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
3659 schedule_work(&vi->config_work);
3661 vi->status = VIRTIO_NET_S_LINK_UP;
3662 virtnet_update_settings(vi);
3663 netif_carrier_on(dev);
3666 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3667 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3668 set_bit(guest_offloads[i], &vi->guest_offloads);
3669 vi->guest_offloads_capable = vi->guest_offloads;
3671 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3672 dev->name, max_queue_pairs);
3676 free_unregister_netdev:
3677 virtio_reset_device(vdev);
3679 unregister_netdev(dev);
3681 net_failover_destroy(vi->failover);
3683 cancel_delayed_work_sync(&vi->refill);
3684 free_receive_page_frags(vi);
3685 virtnet_del_vqs(vi);
3691 static void remove_vq_common(struct virtnet_info *vi)
3693 virtio_reset_device(vi->vdev);
3695 /* Free unused buffers in both send and recv, if any. */
3696 free_unused_bufs(vi);
3698 free_receive_bufs(vi);
3700 free_receive_page_frags(vi);
3702 virtnet_del_vqs(vi);
3705 static void virtnet_remove(struct virtio_device *vdev)
3707 struct virtnet_info *vi = vdev->priv;
3709 virtnet_cpu_notif_remove(vi);
3711 /* Make sure no work handler is accessing the device. */
3712 flush_work(&vi->config_work);
3714 unregister_netdev(vi->dev);
3716 net_failover_destroy(vi->failover);
3718 remove_vq_common(vi);
3720 free_netdev(vi->dev);
3723 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
3725 struct virtnet_info *vi = vdev->priv;
3727 virtnet_cpu_notif_remove(vi);
3728 virtnet_freeze_down(vdev);
3729 remove_vq_common(vi);
3734 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
3736 struct virtnet_info *vi = vdev->priv;
3739 err = virtnet_restore_up(vdev);
3742 virtnet_set_queues(vi, vi->curr_queue_pairs);
3744 err = virtnet_cpu_notif_add(vi);
3746 virtnet_freeze_down(vdev);
3747 remove_vq_common(vi);
3754 static struct virtio_device_id id_table[] = {
3755 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3759 #define VIRTNET_FEATURES \
3760 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3762 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3763 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3764 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3765 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3766 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3767 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3768 VIRTIO_NET_F_CTRL_MAC_ADDR, \
3769 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
3770 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
3771 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT
3773 static unsigned int features[] = {
3777 static unsigned int features_legacy[] = {
3780 VIRTIO_F_ANY_LAYOUT,
3783 static struct virtio_driver virtio_net_driver = {
3784 .feature_table = features,
3785 .feature_table_size = ARRAY_SIZE(features),
3786 .feature_table_legacy = features_legacy,
3787 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
3788 .driver.name = KBUILD_MODNAME,
3789 .driver.owner = THIS_MODULE,
3790 .id_table = id_table,
3791 .validate = virtnet_validate,
3792 .probe = virtnet_probe,
3793 .remove = virtnet_remove,
3794 .config_changed = virtnet_config_changed,
3795 #ifdef CONFIG_PM_SLEEP
3796 .freeze = virtnet_freeze,
3797 .restore = virtnet_restore,
3801 static __init int virtio_net_driver_init(void)
3805 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
3807 virtnet_cpu_down_prep);
3810 virtionet_online = ret;
3811 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
3812 NULL, virtnet_cpu_dead);
3815 ret = register_virtio_driver(&virtio_net_driver);
3820 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3822 cpuhp_remove_multi_state(virtionet_online);
3826 module_init(virtio_net_driver_init);
3828 static __exit void virtio_net_driver_exit(void)
3830 unregister_virtio_driver(&virtio_net_driver);
3831 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3832 cpuhp_remove_multi_state(virtionet_online);
3834 module_exit(virtio_net_driver_exit);
3836 MODULE_DEVICE_TABLE(virtio, id_table);
3837 MODULE_DESCRIPTION("Virtio network driver");
3838 MODULE_LICENSE("GPL");