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 delayed refilling if we run low on memory. */
246 struct delayed_work refill;
248 /* Is delayed refill enabled? */
251 /* The lock to synchronize the access to refill_enabled */
252 spinlock_t refill_lock;
254 /* Work struct for config space updates */
255 struct work_struct config_work;
257 /* Does the affinity hint is set for virtqueues? */
258 bool affinity_hint_set;
260 /* CPU hotplug instances for online & dead */
261 struct hlist_node node;
262 struct hlist_node node_dead;
264 struct control_buf *ctrl;
266 /* Ethtool settings */
270 unsigned long guest_offloads;
271 unsigned long guest_offloads_capable;
273 /* failover when STANDBY feature enabled */
274 struct failover *failover;
277 struct padded_vnet_hdr {
278 struct virtio_net_hdr_v1_hash hdr;
280 * hdr is in a separate sg buffer, and data sg buffer shares same page
281 * with this header sg. This padding makes next sg 16 byte aligned
287 static bool is_xdp_frame(void *ptr)
289 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
292 static void *xdp_to_ptr(struct xdp_frame *ptr)
294 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
297 static struct xdp_frame *ptr_to_xdp(void *ptr)
299 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
302 /* Converting between virtqueue no. and kernel tx/rx queue no.
303 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
305 static int vq2txq(struct virtqueue *vq)
307 return (vq->index - 1) / 2;
310 static int txq2vq(int txq)
315 static int vq2rxq(struct virtqueue *vq)
317 return vq->index / 2;
320 static int rxq2vq(int rxq)
325 static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
327 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
331 * private is used to chain pages for big packets, put the whole
332 * most recent used list in the beginning for reuse
334 static void give_pages(struct receive_queue *rq, struct page *page)
338 /* Find end of list, sew whole thing into vi->rq.pages. */
339 for (end = page; end->private; end = (struct page *)end->private);
340 end->private = (unsigned long)rq->pages;
344 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
346 struct page *p = rq->pages;
349 rq->pages = (struct page *)p->private;
350 /* clear private here, it is used to chain pages */
353 p = alloc_page(gfp_mask);
357 static void enable_delayed_refill(struct virtnet_info *vi)
359 spin_lock_bh(&vi->refill_lock);
360 vi->refill_enabled = true;
361 spin_unlock_bh(&vi->refill_lock);
364 static void disable_delayed_refill(struct virtnet_info *vi)
366 spin_lock_bh(&vi->refill_lock);
367 vi->refill_enabled = false;
368 spin_unlock_bh(&vi->refill_lock);
371 static void virtqueue_napi_schedule(struct napi_struct *napi,
372 struct virtqueue *vq)
374 if (napi_schedule_prep(napi)) {
375 virtqueue_disable_cb(vq);
376 __napi_schedule(napi);
380 static void virtqueue_napi_complete(struct napi_struct *napi,
381 struct virtqueue *vq, int processed)
385 opaque = virtqueue_enable_cb_prepare(vq);
386 if (napi_complete_done(napi, processed)) {
387 if (unlikely(virtqueue_poll(vq, opaque)))
388 virtqueue_napi_schedule(napi, vq);
390 virtqueue_disable_cb(vq);
394 static void skb_xmit_done(struct virtqueue *vq)
396 struct virtnet_info *vi = vq->vdev->priv;
397 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
399 /* Suppress further interrupts. */
400 virtqueue_disable_cb(vq);
403 virtqueue_napi_schedule(napi, vq);
405 /* We were probably waiting for more output buffers. */
406 netif_wake_subqueue(vi->dev, vq2txq(vq));
409 #define MRG_CTX_HEADER_SHIFT 22
410 static void *mergeable_len_to_ctx(unsigned int truesize,
411 unsigned int headroom)
413 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
416 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
418 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
421 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
423 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
426 /* Called from bottom half context */
427 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
428 struct receive_queue *rq,
429 struct page *page, unsigned int offset,
430 unsigned int len, unsigned int truesize,
431 bool hdr_valid, unsigned int metasize,
432 unsigned int headroom)
435 struct virtio_net_hdr_mrg_rxbuf *hdr;
436 unsigned int copy, hdr_len, hdr_padded_len;
437 struct page *page_to_free = NULL;
438 int tailroom, shinfo_size;
439 char *p, *hdr_p, *buf;
441 p = page_address(page) + offset;
444 hdr_len = vi->hdr_len;
445 if (vi->mergeable_rx_bufs)
446 hdr_padded_len = hdr_len;
448 hdr_padded_len = sizeof(struct padded_vnet_hdr);
450 /* If headroom is not 0, there is an offset between the beginning of the
451 * data and the allocated space, otherwise the data and the allocated
454 * Buffers with headroom use PAGE_SIZE as alloc size, see
455 * add_recvbuf_mergeable() + get_mergeable_buf_len()
457 truesize = headroom ? PAGE_SIZE : truesize;
458 tailroom = truesize - headroom;
462 offset += hdr_padded_len;
464 tailroom -= hdr_padded_len + len;
466 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
468 /* copy small packet so we can reuse these pages */
469 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
470 skb = build_skb(buf, truesize);
474 skb_reserve(skb, p - buf);
477 page = (struct page *)page->private;
479 give_pages(rq, page);
483 /* copy small packet so we can reuse these pages for small data */
484 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
488 /* Copy all frame if it fits skb->head, otherwise
489 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
491 if (len <= skb_tailroom(skb))
494 copy = ETH_HLEN + metasize;
495 skb_put_data(skb, p, copy);
500 if (vi->mergeable_rx_bufs) {
502 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
509 * Verify that we can indeed put this data into a skb.
510 * This is here to handle cases when the device erroneously
511 * tries to receive more than is possible. This is usually
512 * the case of a broken device.
514 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
515 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
519 BUG_ON(offset >= PAGE_SIZE);
521 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
522 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
523 frag_size, truesize);
525 page = (struct page *)page->private;
530 give_pages(rq, page);
533 /* hdr_valid means no XDP, so we can copy the vnet header */
535 hdr = skb_vnet_hdr(skb);
536 memcpy(hdr, hdr_p, hdr_len);
539 put_page(page_to_free);
542 __skb_pull(skb, metasize);
543 skb_metadata_set(skb, metasize);
549 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
550 struct send_queue *sq,
551 struct xdp_frame *xdpf)
553 struct virtio_net_hdr_mrg_rxbuf *hdr;
556 if (unlikely(xdpf->headroom < vi->hdr_len))
559 /* Make room for virtqueue hdr (also change xdpf->headroom?) */
560 xdpf->data -= vi->hdr_len;
561 /* Zero header and leave csum up to XDP layers */
563 memset(hdr, 0, vi->hdr_len);
564 xdpf->len += vi->hdr_len;
566 sg_init_one(sq->sg, xdpf->data, xdpf->len);
568 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
571 return -ENOSPC; /* Caller handle free/refcnt */
576 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
577 * the current cpu, so it does not need to be locked.
579 * Here we use marco instead of inline functions because we have to deal with
580 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
581 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
582 * functions to perfectly solve these three problems at the same time.
584 #define virtnet_xdp_get_sq(vi) ({ \
585 int cpu = smp_processor_id(); \
586 struct netdev_queue *txq; \
587 typeof(vi) v = (vi); \
590 if (v->curr_queue_pairs > nr_cpu_ids) { \
591 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
593 txq = netdev_get_tx_queue(v->dev, qp); \
594 __netif_tx_acquire(txq); \
596 qp = cpu % v->curr_queue_pairs; \
597 txq = netdev_get_tx_queue(v->dev, qp); \
598 __netif_tx_lock(txq, cpu); \
603 #define virtnet_xdp_put_sq(vi, q) { \
604 struct netdev_queue *txq; \
605 typeof(vi) v = (vi); \
607 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
608 if (v->curr_queue_pairs > nr_cpu_ids) \
609 __netif_tx_release(txq); \
611 __netif_tx_unlock(txq); \
614 static int virtnet_xdp_xmit(struct net_device *dev,
615 int n, struct xdp_frame **frames, u32 flags)
617 struct virtnet_info *vi = netdev_priv(dev);
618 struct receive_queue *rq = vi->rq;
619 struct bpf_prog *xdp_prog;
620 struct send_queue *sq;
630 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
631 * indicate XDP resources have been successfully allocated.
633 xdp_prog = rcu_access_pointer(rq->xdp_prog);
637 sq = virtnet_xdp_get_sq(vi);
639 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
644 /* Free up any pending old buffers before queueing new ones. */
645 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
646 if (likely(is_xdp_frame(ptr))) {
647 struct xdp_frame *frame = ptr_to_xdp(ptr);
650 xdp_return_frame(frame);
652 struct sk_buff *skb = ptr;
655 napi_consume_skb(skb, false);
660 for (i = 0; i < n; i++) {
661 struct xdp_frame *xdpf = frames[i];
663 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
669 if (flags & XDP_XMIT_FLUSH) {
670 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
674 u64_stats_update_begin(&sq->stats.syncp);
675 sq->stats.bytes += bytes;
676 sq->stats.packets += packets;
677 sq->stats.xdp_tx += n;
678 sq->stats.xdp_tx_drops += n - nxmit;
679 sq->stats.kicks += kicks;
680 u64_stats_update_end(&sq->stats.syncp);
682 virtnet_xdp_put_sq(vi, sq);
686 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
688 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
691 /* We copy the packet for XDP in the following cases:
693 * 1) Packet is scattered across multiple rx buffers.
694 * 2) Headroom space is insufficient.
696 * This is inefficient but it's a temporary condition that
697 * we hit right after XDP is enabled and until queue is refilled
698 * with large buffers with sufficient headroom - so it should affect
699 * at most queue size packets.
700 * Afterwards, the conditions to enable
701 * XDP should preclude the underlying device from sending packets
702 * across multiple buffers (num_buf > 1), and we make sure buffers
703 * have enough headroom.
705 static struct page *xdp_linearize_page(struct receive_queue *rq,
712 struct page *page = alloc_page(GFP_ATOMIC);
717 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
721 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
726 buf = virtqueue_get_buf(rq->vq, &buflen);
730 p = virt_to_head_page(buf);
731 off = buf - page_address(p);
733 /* guard against a misconfigured or uncooperative backend that
734 * is sending packet larger than the MTU.
736 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
741 memcpy(page_address(page) + page_off,
742 page_address(p) + off, buflen);
747 /* Headroom does not contribute to packet length */
748 *len = page_off - VIRTIO_XDP_HEADROOM;
751 __free_pages(page, 0);
755 static struct sk_buff *receive_small(struct net_device *dev,
756 struct virtnet_info *vi,
757 struct receive_queue *rq,
758 void *buf, void *ctx,
760 unsigned int *xdp_xmit,
761 struct virtnet_rq_stats *stats)
764 struct bpf_prog *xdp_prog;
765 unsigned int xdp_headroom = (unsigned long)ctx;
766 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
767 unsigned int headroom = vi->hdr_len + header_offset;
768 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
769 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
770 struct page *page = virt_to_head_page(buf);
771 unsigned int delta = 0;
772 struct page *xdp_page;
774 unsigned int metasize = 0;
779 if (unlikely(len > GOOD_PACKET_LEN)) {
780 pr_debug("%s: rx error: len %u exceeds max size %d\n",
781 dev->name, len, GOOD_PACKET_LEN);
782 dev->stats.rx_length_errors++;
786 if (likely(!vi->xdp_enabled)) {
792 xdp_prog = rcu_dereference(rq->xdp_prog);
794 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
795 struct xdp_frame *xdpf;
800 if (unlikely(hdr->hdr.gso_type))
803 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
804 int offset = buf - page_address(page) + header_offset;
805 unsigned int tlen = len + vi->hdr_len;
808 xdp_headroom = virtnet_get_headroom(vi);
809 header_offset = VIRTNET_RX_PAD + xdp_headroom;
810 headroom = vi->hdr_len + header_offset;
811 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
812 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
813 xdp_page = xdp_linearize_page(rq, &num_buf, page,
814 offset, header_offset,
819 buf = page_address(xdp_page);
824 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
825 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
826 xdp_headroom, len, true);
827 orig_data = xdp.data;
828 act = bpf_prog_run_xdp(xdp_prog, &xdp);
829 stats->xdp_packets++;
833 /* Recalculate length in case bpf program changed it */
834 delta = orig_data - xdp.data;
835 len = xdp.data_end - xdp.data;
836 metasize = xdp.data - xdp.data_meta;
840 xdpf = xdp_convert_buff_to_frame(&xdp);
843 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
844 if (unlikely(!err)) {
845 xdp_return_frame_rx_napi(xdpf);
846 } else if (unlikely(err < 0)) {
847 trace_xdp_exception(vi->dev, xdp_prog, act);
850 *xdp_xmit |= VIRTIO_XDP_TX;
854 stats->xdp_redirects++;
855 err = xdp_do_redirect(dev, &xdp, xdp_prog);
858 *xdp_xmit |= VIRTIO_XDP_REDIR;
862 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
865 trace_xdp_exception(vi->dev, xdp_prog, act);
874 skb = build_skb(buf, buflen);
877 skb_reserve(skb, headroom - delta);
880 buf += header_offset;
881 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
882 } /* keep zeroed vnet hdr since XDP is loaded */
885 skb_metadata_set(skb, metasize);
899 static struct sk_buff *receive_big(struct net_device *dev,
900 struct virtnet_info *vi,
901 struct receive_queue *rq,
904 struct virtnet_rq_stats *stats)
906 struct page *page = buf;
907 struct sk_buff *skb =
908 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0);
910 stats->bytes += len - vi->hdr_len;
918 give_pages(rq, page);
922 static struct sk_buff *receive_mergeable(struct net_device *dev,
923 struct virtnet_info *vi,
924 struct receive_queue *rq,
928 unsigned int *xdp_xmit,
929 struct virtnet_rq_stats *stats)
931 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
932 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
933 struct page *page = virt_to_head_page(buf);
934 int offset = buf - page_address(page);
935 struct sk_buff *head_skb, *curr_skb;
936 struct bpf_prog *xdp_prog;
937 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
938 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
939 unsigned int metasize = 0;
940 unsigned int frame_sz;
944 stats->bytes += len - vi->hdr_len;
946 if (unlikely(len > truesize)) {
947 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
948 dev->name, len, (unsigned long)ctx);
949 dev->stats.rx_length_errors++;
953 if (likely(!vi->xdp_enabled)) {
959 xdp_prog = rcu_dereference(rq->xdp_prog);
961 struct xdp_frame *xdpf;
962 struct page *xdp_page;
967 /* Transient failure which in theory could occur if
968 * in-flight packets from before XDP was enabled reach
969 * the receive path after XDP is loaded.
971 if (unlikely(hdr->hdr.gso_type))
974 /* Buffers with headroom use PAGE_SIZE as alloc size,
975 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
977 frame_sz = headroom ? PAGE_SIZE : truesize;
979 /* This happens when rx buffer size is underestimated
980 * or headroom is not enough because of the buffer
981 * was refilled before XDP is set. This should only
982 * happen for the first several packets, so we don't
983 * care much about its performance.
985 if (unlikely(num_buf > 1 ||
986 headroom < virtnet_get_headroom(vi))) {
987 /* linearize data for XDP */
988 xdp_page = xdp_linearize_page(rq, &num_buf,
992 frame_sz = PAGE_SIZE;
996 offset = VIRTIO_XDP_HEADROOM;
1001 /* Allow consuming headroom but reserve enough space to push
1002 * the descriptor on if we get an XDP_TX return code.
1004 data = page_address(xdp_page) + offset;
1005 xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq);
1006 xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len,
1007 VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true);
1009 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1010 stats->xdp_packets++;
1014 metasize = xdp.data - xdp.data_meta;
1016 /* recalculate offset to account for any header
1017 * adjustments and minus the metasize to copy the
1018 * metadata in page_to_skb(). Note other cases do not
1019 * build an skb and avoid using offset
1021 offset = xdp.data - page_address(xdp_page) -
1022 vi->hdr_len - metasize;
1024 /* recalculate len if xdp.data, xdp.data_end or
1025 * xdp.data_meta were adjusted
1027 len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
1029 /* recalculate headroom if xdp.data or xdp_data_meta
1030 * were adjusted, note that offset should always point
1031 * to the start of the reserved bytes for virtio_net
1032 * header which are followed by xdp.data, that means
1033 * that offset is equal to the headroom (when buf is
1034 * starting at the beginning of the page, otherwise
1035 * there is a base offset inside the page) but it's used
1036 * with a different starting point (buf start) than
1037 * xdp.data (buf start + vnet hdr size). If xdp.data or
1038 * data_meta were adjusted by the xdp prog then the
1039 * headroom size has changed and so has the offset, we
1040 * can use data_hard_start, which points at buf start +
1041 * vnet hdr size, to calculate the new headroom and use
1042 * it later to compute buf start in page_to_skb()
1044 headroom = xdp.data - xdp.data_hard_start - metasize;
1046 /* We can only create skb based on xdp_page. */
1047 if (unlikely(xdp_page != page)) {
1050 head_skb = page_to_skb(vi, rq, xdp_page, offset,
1051 len, PAGE_SIZE, false,
1059 xdpf = xdp_convert_buff_to_frame(&xdp);
1060 if (unlikely(!xdpf))
1062 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1063 if (unlikely(!err)) {
1064 xdp_return_frame_rx_napi(xdpf);
1065 } else if (unlikely(err < 0)) {
1066 trace_xdp_exception(vi->dev, xdp_prog, act);
1067 if (unlikely(xdp_page != page))
1071 *xdp_xmit |= VIRTIO_XDP_TX;
1072 if (unlikely(xdp_page != page))
1077 stats->xdp_redirects++;
1078 err = xdp_do_redirect(dev, &xdp, xdp_prog);
1080 if (unlikely(xdp_page != page))
1084 *xdp_xmit |= VIRTIO_XDP_REDIR;
1085 if (unlikely(xdp_page != page))
1090 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
1093 trace_xdp_exception(vi->dev, xdp_prog, act);
1096 if (unlikely(xdp_page != page))
1097 __free_pages(xdp_page, 0);
1104 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
1105 metasize, headroom);
1106 curr_skb = head_skb;
1108 if (unlikely(!curr_skb))
1113 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
1114 if (unlikely(!buf)) {
1115 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1117 virtio16_to_cpu(vi->vdev,
1119 dev->stats.rx_length_errors++;
1123 stats->bytes += len;
1124 page = virt_to_head_page(buf);
1126 truesize = mergeable_ctx_to_truesize(ctx);
1127 if (unlikely(len > truesize)) {
1128 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1129 dev->name, len, (unsigned long)ctx);
1130 dev->stats.rx_length_errors++;
1134 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
1135 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1136 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
1138 if (unlikely(!nskb))
1140 if (curr_skb == head_skb)
1141 skb_shinfo(curr_skb)->frag_list = nskb;
1143 curr_skb->next = nskb;
1145 head_skb->truesize += nskb->truesize;
1148 if (curr_skb != head_skb) {
1149 head_skb->data_len += len;
1150 head_skb->len += len;
1151 head_skb->truesize += truesize;
1153 offset = buf - page_address(page);
1154 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1156 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1159 skb_add_rx_frag(curr_skb, num_skb_frags, page,
1160 offset, len, truesize);
1164 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
1172 while (num_buf-- > 1) {
1173 buf = virtqueue_get_buf(rq->vq, &len);
1174 if (unlikely(!buf)) {
1175 pr_debug("%s: rx error: %d buffers missing\n",
1176 dev->name, num_buf);
1177 dev->stats.rx_length_errors++;
1180 stats->bytes += len;
1181 page = virt_to_head_page(buf);
1186 dev_kfree_skb(head_skb);
1191 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
1192 struct sk_buff *skb)
1194 enum pkt_hash_types rss_hash_type;
1196 if (!hdr_hash || !skb)
1199 switch ((int)hdr_hash->hash_report) {
1200 case VIRTIO_NET_HASH_REPORT_TCPv4:
1201 case VIRTIO_NET_HASH_REPORT_UDPv4:
1202 case VIRTIO_NET_HASH_REPORT_TCPv6:
1203 case VIRTIO_NET_HASH_REPORT_UDPv6:
1204 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
1205 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
1206 rss_hash_type = PKT_HASH_TYPE_L4;
1208 case VIRTIO_NET_HASH_REPORT_IPv4:
1209 case VIRTIO_NET_HASH_REPORT_IPv6:
1210 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
1211 rss_hash_type = PKT_HASH_TYPE_L3;
1213 case VIRTIO_NET_HASH_REPORT_NONE:
1215 rss_hash_type = PKT_HASH_TYPE_NONE;
1217 skb_set_hash(skb, (unsigned int)hdr_hash->hash_value, rss_hash_type);
1220 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1221 void *buf, unsigned int len, void **ctx,
1222 unsigned int *xdp_xmit,
1223 struct virtnet_rq_stats *stats)
1225 struct net_device *dev = vi->dev;
1226 struct sk_buff *skb;
1227 struct virtio_net_hdr_mrg_rxbuf *hdr;
1229 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
1230 pr_debug("%s: short packet %i\n", dev->name, len);
1231 dev->stats.rx_length_errors++;
1232 if (vi->mergeable_rx_bufs) {
1233 put_page(virt_to_head_page(buf));
1234 } else if (vi->big_packets) {
1235 give_pages(rq, buf);
1237 put_page(virt_to_head_page(buf));
1242 if (vi->mergeable_rx_bufs)
1243 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1245 else if (vi->big_packets)
1246 skb = receive_big(dev, vi, rq, buf, len, stats);
1248 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1253 hdr = skb_vnet_hdr(skb);
1254 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
1255 virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
1257 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
1258 skb->ip_summed = CHECKSUM_UNNECESSARY;
1260 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1261 virtio_is_little_endian(vi->vdev))) {
1262 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1263 dev->name, hdr->hdr.gso_type,
1268 skb_record_rx_queue(skb, vq2rxq(rq->vq));
1269 skb->protocol = eth_type_trans(skb, dev);
1270 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1271 ntohs(skb->protocol), skb->len, skb->pkt_type);
1273 napi_gro_receive(&rq->napi, skb);
1277 dev->stats.rx_frame_errors++;
1281 /* Unlike mergeable buffers, all buffers are allocated to the
1282 * same size, except for the headroom. For this reason we do
1283 * not need to use mergeable_len_to_ctx here - it is enough
1284 * to store the headroom as the context ignoring the truesize.
1286 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1289 struct page_frag *alloc_frag = &rq->alloc_frag;
1291 unsigned int xdp_headroom = virtnet_get_headroom(vi);
1292 void *ctx = (void *)(unsigned long)xdp_headroom;
1293 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
1296 len = SKB_DATA_ALIGN(len) +
1297 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1298 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
1301 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1302 get_page(alloc_frag->page);
1303 alloc_frag->offset += len;
1304 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1305 vi->hdr_len + GOOD_PACKET_LEN);
1306 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1308 put_page(virt_to_head_page(buf));
1312 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1315 struct page *first, *list = NULL;
1319 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1321 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
1322 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
1323 first = get_a_page(rq, gfp);
1326 give_pages(rq, list);
1329 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
1331 /* chain new page in list head to match sg */
1332 first->private = (unsigned long)list;
1336 first = get_a_page(rq, gfp);
1338 give_pages(rq, list);
1341 p = page_address(first);
1343 /* rq->sg[0], rq->sg[1] share the same page */
1344 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1345 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
1347 /* rq->sg[1] for data packet, from offset */
1348 offset = sizeof(struct padded_vnet_hdr);
1349 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
1351 /* chain first in list head */
1352 first->private = (unsigned long)list;
1353 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1356 give_pages(rq, first);
1361 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
1362 struct ewma_pkt_len *avg_pkt_len,
1365 struct virtnet_info *vi = rq->vq->vdev->priv;
1366 const size_t hdr_len = vi->hdr_len;
1370 return PAGE_SIZE - room;
1372 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1373 rq->min_buf_len, PAGE_SIZE - hdr_len);
1375 return ALIGN(len, L1_CACHE_BYTES);
1378 static int add_recvbuf_mergeable(struct virtnet_info *vi,
1379 struct receive_queue *rq, gfp_t gfp)
1381 struct page_frag *alloc_frag = &rq->alloc_frag;
1382 unsigned int headroom = virtnet_get_headroom(vi);
1383 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1384 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1388 unsigned int len, hole;
1390 /* Extra tailroom is needed to satisfy XDP's assumption. This
1391 * means rx frags coalescing won't work, but consider we've
1392 * disabled GSO for XDP, it won't be a big issue.
1394 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1395 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
1398 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1399 buf += headroom; /* advance address leaving hole at front of pkt */
1400 get_page(alloc_frag->page);
1401 alloc_frag->offset += len + room;
1402 hole = alloc_frag->size - alloc_frag->offset;
1403 if (hole < len + room) {
1404 /* To avoid internal fragmentation, if there is very likely not
1405 * enough space for another buffer, add the remaining space to
1406 * the current buffer.
1409 alloc_frag->offset += hole;
1412 sg_init_one(rq->sg, buf, len);
1413 ctx = mergeable_len_to_ctx(len, headroom);
1414 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1416 put_page(virt_to_head_page(buf));
1422 * Returns false if we couldn't fill entirely (OOM).
1424 * Normally run in the receive path, but can also be run from ndo_open
1425 * before we're receiving packets, or from refill_work which is
1426 * careful to disable receiving (using napi_disable).
1428 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1435 if (vi->mergeable_rx_bufs)
1436 err = add_recvbuf_mergeable(vi, rq, gfp);
1437 else if (vi->big_packets)
1438 err = add_recvbuf_big(vi, rq, gfp);
1440 err = add_recvbuf_small(vi, rq, gfp);
1442 oom = err == -ENOMEM;
1445 } while (rq->vq->num_free);
1446 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1447 unsigned long flags;
1449 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
1451 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
1457 static void skb_recv_done(struct virtqueue *rvq)
1459 struct virtnet_info *vi = rvq->vdev->priv;
1460 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1462 virtqueue_napi_schedule(&rq->napi, rvq);
1465 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
1469 /* If all buffers were filled by other side before we napi_enabled, we
1470 * won't get another interrupt, so process any outstanding packets now.
1471 * Call local_bh_enable after to trigger softIRQ processing.
1474 virtqueue_napi_schedule(napi, vq);
1478 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1479 struct virtqueue *vq,
1480 struct napi_struct *napi)
1485 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1486 * enable the feature if this is likely affine with the transmit path.
1488 if (!vi->affinity_hint_set) {
1493 return virtnet_napi_enable(vq, napi);
1496 static void virtnet_napi_tx_disable(struct napi_struct *napi)
1502 static void refill_work(struct work_struct *work)
1504 struct virtnet_info *vi =
1505 container_of(work, struct virtnet_info, refill.work);
1509 for (i = 0; i < vi->curr_queue_pairs; i++) {
1510 struct receive_queue *rq = &vi->rq[i];
1512 napi_disable(&rq->napi);
1513 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1514 virtnet_napi_enable(rq->vq, &rq->napi);
1516 /* In theory, this can happen: if we don't get any buffers in
1517 * we will *never* try to fill again.
1520 schedule_delayed_work(&vi->refill, HZ/2);
1524 static int virtnet_receive(struct receive_queue *rq, int budget,
1525 unsigned int *xdp_xmit)
1527 struct virtnet_info *vi = rq->vq->vdev->priv;
1528 struct virtnet_rq_stats stats = {};
1533 if (!vi->big_packets || vi->mergeable_rx_bufs) {
1536 while (stats.packets < budget &&
1537 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1538 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1542 while (stats.packets < budget &&
1543 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1544 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1549 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
1550 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
1551 spin_lock(&vi->refill_lock);
1552 if (vi->refill_enabled)
1553 schedule_delayed_work(&vi->refill, 0);
1554 spin_unlock(&vi->refill_lock);
1558 u64_stats_update_begin(&rq->stats.syncp);
1559 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1560 size_t offset = virtnet_rq_stats_desc[i].offset;
1563 item = (u64 *)((u8 *)&rq->stats + offset);
1564 *item += *(u64 *)((u8 *)&stats + offset);
1566 u64_stats_update_end(&rq->stats.syncp);
1568 return stats.packets;
1571 static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
1574 unsigned int packets = 0;
1575 unsigned int bytes = 0;
1578 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1579 if (likely(!is_xdp_frame(ptr))) {
1580 struct sk_buff *skb = ptr;
1582 pr_debug("Sent skb %p\n", skb);
1585 napi_consume_skb(skb, in_napi);
1587 struct xdp_frame *frame = ptr_to_xdp(ptr);
1589 bytes += frame->len;
1590 xdp_return_frame(frame);
1595 /* Avoid overhead when no packets have been processed
1596 * happens when called speculatively from start_xmit.
1601 u64_stats_update_begin(&sq->stats.syncp);
1602 sq->stats.bytes += bytes;
1603 sq->stats.packets += packets;
1604 u64_stats_update_end(&sq->stats.syncp);
1607 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1609 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1611 else if (q < vi->curr_queue_pairs)
1617 static void virtnet_poll_cleantx(struct receive_queue *rq)
1619 struct virtnet_info *vi = rq->vq->vdev->priv;
1620 unsigned int index = vq2rxq(rq->vq);
1621 struct send_queue *sq = &vi->sq[index];
1622 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1624 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
1627 if (__netif_tx_trylock(txq)) {
1629 virtqueue_disable_cb(sq->vq);
1630 free_old_xmit_skbs(sq, true);
1631 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1633 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1634 netif_tx_wake_queue(txq);
1636 __netif_tx_unlock(txq);
1640 static int virtnet_poll(struct napi_struct *napi, int budget)
1642 struct receive_queue *rq =
1643 container_of(napi, struct receive_queue, napi);
1644 struct virtnet_info *vi = rq->vq->vdev->priv;
1645 struct send_queue *sq;
1646 unsigned int received;
1647 unsigned int xdp_xmit = 0;
1649 virtnet_poll_cleantx(rq);
1651 received = virtnet_receive(rq, budget, &xdp_xmit);
1653 /* Out of packets? */
1654 if (received < budget)
1655 virtqueue_napi_complete(napi, rq->vq, received);
1657 if (xdp_xmit & VIRTIO_XDP_REDIR)
1660 if (xdp_xmit & VIRTIO_XDP_TX) {
1661 sq = virtnet_xdp_get_sq(vi);
1662 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1663 u64_stats_update_begin(&sq->stats.syncp);
1665 u64_stats_update_end(&sq->stats.syncp);
1667 virtnet_xdp_put_sq(vi, sq);
1673 static int virtnet_open(struct net_device *dev)
1675 struct virtnet_info *vi = netdev_priv(dev);
1678 enable_delayed_refill(vi);
1680 for (i = 0; i < vi->max_queue_pairs; i++) {
1681 if (i < vi->curr_queue_pairs)
1682 /* Make sure we have some buffers: if oom use wq. */
1683 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1684 schedule_delayed_work(&vi->refill, 0);
1686 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
1690 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1691 MEM_TYPE_PAGE_SHARED, NULL);
1693 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1697 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1698 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1704 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1706 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1707 struct virtnet_info *vi = sq->vq->vdev->priv;
1708 unsigned int index = vq2txq(sq->vq);
1709 struct netdev_queue *txq;
1713 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1714 /* We don't need to enable cb for XDP */
1715 napi_complete_done(napi, 0);
1719 txq = netdev_get_tx_queue(vi->dev, index);
1720 __netif_tx_lock(txq, raw_smp_processor_id());
1721 virtqueue_disable_cb(sq->vq);
1722 free_old_xmit_skbs(sq, true);
1724 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1725 netif_tx_wake_queue(txq);
1727 opaque = virtqueue_enable_cb_prepare(sq->vq);
1729 done = napi_complete_done(napi, 0);
1732 virtqueue_disable_cb(sq->vq);
1734 __netif_tx_unlock(txq);
1737 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
1738 if (napi_schedule_prep(napi)) {
1739 __netif_tx_lock(txq, raw_smp_processor_id());
1740 virtqueue_disable_cb(sq->vq);
1741 __netif_tx_unlock(txq);
1742 __napi_schedule(napi);
1750 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1752 struct virtio_net_hdr_mrg_rxbuf *hdr;
1753 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1754 struct virtnet_info *vi = sq->vq->vdev->priv;
1756 unsigned hdr_len = vi->hdr_len;
1759 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1761 can_push = vi->any_header_sg &&
1762 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1763 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1764 /* Even if we can, don't push here yet as this would skew
1765 * csum_start offset below. */
1767 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1769 hdr = skb_vnet_hdr(skb);
1771 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1772 virtio_is_little_endian(vi->vdev), false,
1776 if (vi->mergeable_rx_bufs)
1777 hdr->num_buffers = 0;
1779 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1781 __skb_push(skb, hdr_len);
1782 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1783 if (unlikely(num_sg < 0))
1785 /* Pull header back to avoid skew in tx bytes calculations. */
1786 __skb_pull(skb, hdr_len);
1788 sg_set_buf(sq->sg, hdr, hdr_len);
1789 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1790 if (unlikely(num_sg < 0))
1794 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
1797 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
1799 struct virtnet_info *vi = netdev_priv(dev);
1800 int qnum = skb_get_queue_mapping(skb);
1801 struct send_queue *sq = &vi->sq[qnum];
1803 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1804 bool kick = !netdev_xmit_more();
1805 bool use_napi = sq->napi.weight;
1807 /* Free up any pending old buffers before queueing new ones. */
1810 virtqueue_disable_cb(sq->vq);
1812 free_old_xmit_skbs(sq, false);
1814 } while (use_napi && kick &&
1815 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1817 /* timestamp packet in software */
1818 skb_tx_timestamp(skb);
1820 /* Try to transmit */
1821 err = xmit_skb(sq, skb);
1823 /* This should not happen! */
1824 if (unlikely(err)) {
1825 dev->stats.tx_fifo_errors++;
1826 if (net_ratelimit())
1828 "Unexpected TXQ (%d) queue failure: %d\n",
1830 dev->stats.tx_dropped++;
1831 dev_kfree_skb_any(skb);
1832 return NETDEV_TX_OK;
1835 /* Don't wait up for transmitted skbs to be freed. */
1841 /* If running out of space, stop queue to avoid getting packets that we
1842 * are then unable to transmit.
1843 * An alternative would be to force queuing layer to requeue the skb by
1844 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1845 * returned in a normal path of operation: it means that driver is not
1846 * maintaining the TX queue stop/start state properly, and causes
1847 * the stack to do a non-trivial amount of useless work.
1848 * Since most packets only take 1 or 2 ring slots, stopping the queue
1849 * early means 16 slots are typically wasted.
1851 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1852 netif_stop_subqueue(dev, qnum);
1854 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1855 /* More just got used, free them then recheck. */
1856 free_old_xmit_skbs(sq, false);
1857 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1858 netif_start_subqueue(dev, qnum);
1859 virtqueue_disable_cb(sq->vq);
1864 if (kick || netif_xmit_stopped(txq)) {
1865 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1866 u64_stats_update_begin(&sq->stats.syncp);
1868 u64_stats_update_end(&sq->stats.syncp);
1872 return NETDEV_TX_OK;
1876 * Send command via the control virtqueue and check status. Commands
1877 * supported by the hypervisor, as indicated by feature bits, should
1878 * never fail unless improperly formatted.
1880 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1881 struct scatterlist *out)
1883 struct scatterlist *sgs[4], hdr, stat;
1884 unsigned out_num = 0, tmp;
1887 /* Caller should know better */
1888 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
1890 vi->ctrl->status = ~0;
1891 vi->ctrl->hdr.class = class;
1892 vi->ctrl->hdr.cmd = cmd;
1894 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
1895 sgs[out_num++] = &hdr;
1898 sgs[out_num++] = out;
1900 /* Add return status. */
1901 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
1902 sgs[out_num] = &stat;
1904 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1905 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1907 dev_warn(&vi->vdev->dev,
1908 "Failed to add sgs for command vq: %d\n.", ret);
1912 if (unlikely(!virtqueue_kick(vi->cvq)))
1913 return vi->ctrl->status == VIRTIO_NET_OK;
1915 /* Spin for a response, the kick causes an ioport write, trapping
1916 * into the hypervisor, so the request should be handled immediately.
1918 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1919 !virtqueue_is_broken(vi->cvq))
1922 return vi->ctrl->status == VIRTIO_NET_OK;
1925 static int virtnet_set_mac_address(struct net_device *dev, void *p)
1927 struct virtnet_info *vi = netdev_priv(dev);
1928 struct virtio_device *vdev = vi->vdev;
1930 struct sockaddr *addr;
1931 struct scatterlist sg;
1933 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1936 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
1940 ret = eth_prepare_mac_addr_change(dev, addr);
1944 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1945 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1946 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1947 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
1948 dev_warn(&vdev->dev,
1949 "Failed to set mac address by vq command.\n");
1953 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1954 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1957 /* Naturally, this has an atomicity problem. */
1958 for (i = 0; i < dev->addr_len; i++)
1959 virtio_cwrite8(vdev,
1960 offsetof(struct virtio_net_config, mac) +
1961 i, addr->sa_data[i]);
1964 eth_commit_mac_addr_change(dev, p);
1972 static void virtnet_stats(struct net_device *dev,
1973 struct rtnl_link_stats64 *tot)
1975 struct virtnet_info *vi = netdev_priv(dev);
1979 for (i = 0; i < vi->max_queue_pairs; i++) {
1980 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
1981 struct receive_queue *rq = &vi->rq[i];
1982 struct send_queue *sq = &vi->sq[i];
1985 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1986 tpackets = sq->stats.packets;
1987 tbytes = sq->stats.bytes;
1988 terrors = sq->stats.tx_timeouts;
1989 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
1992 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
1993 rpackets = rq->stats.packets;
1994 rbytes = rq->stats.bytes;
1995 rdrops = rq->stats.drops;
1996 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
1998 tot->rx_packets += rpackets;
1999 tot->tx_packets += tpackets;
2000 tot->rx_bytes += rbytes;
2001 tot->tx_bytes += tbytes;
2002 tot->rx_dropped += rdrops;
2003 tot->tx_errors += terrors;
2006 tot->tx_dropped = dev->stats.tx_dropped;
2007 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
2008 tot->rx_length_errors = dev->stats.rx_length_errors;
2009 tot->rx_frame_errors = dev->stats.rx_frame_errors;
2012 static void virtnet_ack_link_announce(struct virtnet_info *vi)
2015 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
2016 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
2017 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
2021 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2023 struct scatterlist sg;
2024 struct net_device *dev = vi->dev;
2026 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
2029 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
2030 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
2032 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2033 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
2034 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
2038 vi->curr_queue_pairs = queue_pairs;
2039 /* virtnet_open() will refill when device is going to up. */
2040 if (dev->flags & IFF_UP)
2041 schedule_delayed_work(&vi->refill, 0);
2047 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2052 err = _virtnet_set_queues(vi, queue_pairs);
2057 static int virtnet_close(struct net_device *dev)
2059 struct virtnet_info *vi = netdev_priv(dev);
2062 /* Make sure NAPI doesn't schedule refill work */
2063 disable_delayed_refill(vi);
2064 /* Make sure refill_work doesn't re-enable napi! */
2065 cancel_delayed_work_sync(&vi->refill);
2067 for (i = 0; i < vi->max_queue_pairs; i++) {
2068 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
2069 napi_disable(&vi->rq[i].napi);
2070 virtnet_napi_tx_disable(&vi->sq[i].napi);
2076 static void virtnet_set_rx_mode(struct net_device *dev)
2078 struct virtnet_info *vi = netdev_priv(dev);
2079 struct scatterlist sg[2];
2080 struct virtio_net_ctrl_mac *mac_data;
2081 struct netdev_hw_addr *ha;
2087 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
2088 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
2091 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
2092 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2094 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
2096 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2097 VIRTIO_NET_CTRL_RX_PROMISC, sg))
2098 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
2099 vi->ctrl->promisc ? "en" : "dis");
2101 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
2103 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2104 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2105 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2106 vi->ctrl->allmulti ? "en" : "dis");
2108 uc_count = netdev_uc_count(dev);
2109 mc_count = netdev_mc_count(dev);
2110 /* MAC filter - use one buffer for both lists */
2111 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2112 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
2117 sg_init_table(sg, 2);
2119 /* Store the unicast list and count in the front of the buffer */
2120 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2122 netdev_for_each_uc_addr(ha, dev)
2123 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2125 sg_set_buf(&sg[0], mac_data,
2126 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2128 /* multicast list and count fill the end */
2129 mac_data = (void *)&mac_data->macs[uc_count][0];
2131 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2133 netdev_for_each_mc_addr(ha, dev)
2134 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2136 sg_set_buf(&sg[1], mac_data,
2137 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2139 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2140 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
2141 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2146 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2147 __be16 proto, u16 vid)
2149 struct virtnet_info *vi = netdev_priv(dev);
2150 struct scatterlist sg;
2152 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2153 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2155 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2156 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
2157 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
2161 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2162 __be16 proto, u16 vid)
2164 struct virtnet_info *vi = netdev_priv(dev);
2165 struct scatterlist sg;
2167 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2168 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2170 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2171 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
2172 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
2176 static void virtnet_clean_affinity(struct virtnet_info *vi)
2180 if (vi->affinity_hint_set) {
2181 for (i = 0; i < vi->max_queue_pairs; i++) {
2182 virtqueue_set_affinity(vi->rq[i].vq, NULL);
2183 virtqueue_set_affinity(vi->sq[i].vq, NULL);
2186 vi->affinity_hint_set = false;
2190 static void virtnet_set_affinity(struct virtnet_info *vi)
2199 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2200 virtnet_clean_affinity(vi);
2204 num_cpu = num_online_cpus();
2205 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2206 stragglers = num_cpu >= vi->curr_queue_pairs ?
2207 num_cpu % vi->curr_queue_pairs :
2209 cpu = cpumask_first(cpu_online_mask);
2211 for (i = 0; i < vi->curr_queue_pairs; i++) {
2212 group_size = stride + (i < stragglers ? 1 : 0);
2214 for (j = 0; j < group_size; j++) {
2215 cpumask_set_cpu(cpu, mask);
2216 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2219 virtqueue_set_affinity(vi->rq[i].vq, mask);
2220 virtqueue_set_affinity(vi->sq[i].vq, mask);
2221 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
2222 cpumask_clear(mask);
2225 vi->affinity_hint_set = true;
2226 free_cpumask_var(mask);
2229 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
2231 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2233 virtnet_set_affinity(vi);
2237 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2239 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2241 virtnet_set_affinity(vi);
2245 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2247 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2250 virtnet_clean_affinity(vi);
2254 static enum cpuhp_state virtionet_online;
2256 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2260 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2263 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2267 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2271 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2273 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2274 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2278 static void virtnet_get_ringparam(struct net_device *dev,
2279 struct ethtool_ringparam *ring,
2280 struct kernel_ethtool_ringparam *kernel_ring,
2281 struct netlink_ext_ack *extack)
2283 struct virtnet_info *vi = netdev_priv(dev);
2285 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2286 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2287 ring->rx_pending = ring->rx_max_pending;
2288 ring->tx_pending = ring->tx_max_pending;
2291 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
2293 struct net_device *dev = vi->dev;
2294 struct scatterlist sgs[4];
2295 unsigned int sg_buf_size;
2298 sg_init_table(sgs, 4);
2300 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
2301 sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
2303 sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
2304 sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
2306 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
2307 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
2308 sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
2310 sg_buf_size = vi->rss_key_size;
2311 sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
2313 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2314 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
2315 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
2316 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
2322 static void virtnet_init_default_rss(struct virtnet_info *vi)
2327 vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
2328 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
2329 vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
2330 ? vi->rss_indir_table_size - 1 : 0;
2331 vi->ctrl->rss.unclassified_queue = 0;
2333 for (; i < vi->rss_indir_table_size; ++i) {
2334 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
2335 vi->ctrl->rss.indirection_table[i] = indir_val;
2338 vi->ctrl->rss.max_tx_vq = vi->curr_queue_pairs;
2339 vi->ctrl->rss.hash_key_length = vi->rss_key_size;
2341 netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
2344 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
2347 switch (info->flow_type) {
2349 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
2350 info->data = RXH_IP_SRC | RXH_IP_DST |
2351 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2352 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2353 info->data = RXH_IP_SRC | RXH_IP_DST;
2357 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
2358 info->data = RXH_IP_SRC | RXH_IP_DST |
2359 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2360 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2361 info->data = RXH_IP_SRC | RXH_IP_DST;
2365 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
2366 info->data = RXH_IP_SRC | RXH_IP_DST |
2367 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2368 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2369 info->data = RXH_IP_SRC | RXH_IP_DST;
2373 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
2374 info->data = RXH_IP_SRC | RXH_IP_DST |
2375 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2376 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2377 info->data = RXH_IP_SRC | RXH_IP_DST;
2381 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
2382 info->data = RXH_IP_SRC | RXH_IP_DST;
2386 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
2387 info->data = RXH_IP_SRC | RXH_IP_DST;
2396 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
2398 u32 new_hashtypes = vi->rss_hash_types_saved;
2399 bool is_disable = info->data & RXH_DISCARD;
2400 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
2402 /* supports only 'sd', 'sdfn' and 'r' */
2403 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
2406 switch (info->flow_type) {
2408 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
2410 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2411 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
2414 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
2416 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2417 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
2420 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2422 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2425 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
2427 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2428 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
2431 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
2433 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2434 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
2437 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2439 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2442 /* unsupported flow */
2446 /* if unsupported hashtype was set */
2447 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
2450 if (new_hashtypes != vi->rss_hash_types_saved) {
2451 vi->rss_hash_types_saved = new_hashtypes;
2452 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
2453 if (vi->dev->features & NETIF_F_RXHASH)
2454 return virtnet_commit_rss_command(vi);
2460 static void virtnet_get_drvinfo(struct net_device *dev,
2461 struct ethtool_drvinfo *info)
2463 struct virtnet_info *vi = netdev_priv(dev);
2464 struct virtio_device *vdev = vi->vdev;
2466 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2467 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2468 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
2472 /* TODO: Eliminate OOO packets during switching */
2473 static int virtnet_set_channels(struct net_device *dev,
2474 struct ethtool_channels *channels)
2476 struct virtnet_info *vi = netdev_priv(dev);
2477 u16 queue_pairs = channels->combined_count;
2480 /* We don't support separate rx/tx channels.
2481 * We don't allow setting 'other' channels.
2483 if (channels->rx_count || channels->tx_count || channels->other_count)
2486 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
2489 /* For now we don't support modifying channels while XDP is loaded
2490 * also when XDP is loaded all RX queues have XDP programs so we only
2491 * need to check a single RX queue.
2493 if (vi->rq[0].xdp_prog)
2497 err = _virtnet_set_queues(vi, queue_pairs);
2502 virtnet_set_affinity(vi);
2505 netif_set_real_num_tx_queues(dev, queue_pairs);
2506 netif_set_real_num_rx_queues(dev, queue_pairs);
2511 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2513 struct virtnet_info *vi = netdev_priv(dev);
2517 switch (stringset) {
2519 for (i = 0; i < vi->curr_queue_pairs; i++) {
2520 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2521 ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2522 virtnet_rq_stats_desc[j].desc);
2525 for (i = 0; i < vi->curr_queue_pairs; i++) {
2526 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2527 ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2528 virtnet_sq_stats_desc[j].desc);
2534 static int virtnet_get_sset_count(struct net_device *dev, int sset)
2536 struct virtnet_info *vi = netdev_priv(dev);
2540 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2541 VIRTNET_SQ_STATS_LEN);
2547 static void virtnet_get_ethtool_stats(struct net_device *dev,
2548 struct ethtool_stats *stats, u64 *data)
2550 struct virtnet_info *vi = netdev_priv(dev);
2551 unsigned int idx = 0, start, i, j;
2552 const u8 *stats_base;
2555 for (i = 0; i < vi->curr_queue_pairs; i++) {
2556 struct receive_queue *rq = &vi->rq[i];
2558 stats_base = (u8 *)&rq->stats;
2560 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2561 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2562 offset = virtnet_rq_stats_desc[j].offset;
2563 data[idx + j] = *(u64 *)(stats_base + offset);
2565 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2566 idx += VIRTNET_RQ_STATS_LEN;
2569 for (i = 0; i < vi->curr_queue_pairs; i++) {
2570 struct send_queue *sq = &vi->sq[i];
2572 stats_base = (u8 *)&sq->stats;
2574 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2575 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2576 offset = virtnet_sq_stats_desc[j].offset;
2577 data[idx + j] = *(u64 *)(stats_base + offset);
2579 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2580 idx += VIRTNET_SQ_STATS_LEN;
2584 static void virtnet_get_channels(struct net_device *dev,
2585 struct ethtool_channels *channels)
2587 struct virtnet_info *vi = netdev_priv(dev);
2589 channels->combined_count = vi->curr_queue_pairs;
2590 channels->max_combined = vi->max_queue_pairs;
2591 channels->max_other = 0;
2592 channels->rx_count = 0;
2593 channels->tx_count = 0;
2594 channels->other_count = 0;
2597 static int virtnet_set_link_ksettings(struct net_device *dev,
2598 const struct ethtool_link_ksettings *cmd)
2600 struct virtnet_info *vi = netdev_priv(dev);
2602 return ethtool_virtdev_set_link_ksettings(dev, cmd,
2603 &vi->speed, &vi->duplex);
2606 static int virtnet_get_link_ksettings(struct net_device *dev,
2607 struct ethtool_link_ksettings *cmd)
2609 struct virtnet_info *vi = netdev_priv(dev);
2611 cmd->base.speed = vi->speed;
2612 cmd->base.duplex = vi->duplex;
2613 cmd->base.port = PORT_OTHER;
2618 static int virtnet_set_coalesce(struct net_device *dev,
2619 struct ethtool_coalesce *ec,
2620 struct kernel_ethtool_coalesce *kernel_coal,
2621 struct netlink_ext_ack *extack)
2623 struct virtnet_info *vi = netdev_priv(dev);
2626 if (ec->tx_max_coalesced_frames > 1 ||
2627 ec->rx_max_coalesced_frames != 1)
2630 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
2631 if (napi_weight ^ vi->sq[0].napi.weight) {
2632 if (dev->flags & IFF_UP)
2634 for (i = 0; i < vi->max_queue_pairs; i++)
2635 vi->sq[i].napi.weight = napi_weight;
2641 static int virtnet_get_coalesce(struct net_device *dev,
2642 struct ethtool_coalesce *ec,
2643 struct kernel_ethtool_coalesce *kernel_coal,
2644 struct netlink_ext_ack *extack)
2646 struct ethtool_coalesce ec_default = {
2647 .cmd = ETHTOOL_GCOALESCE,
2648 .rx_max_coalesced_frames = 1,
2650 struct virtnet_info *vi = netdev_priv(dev);
2652 memcpy(ec, &ec_default, sizeof(ec_default));
2654 if (vi->sq[0].napi.weight)
2655 ec->tx_max_coalesced_frames = 1;
2660 static void virtnet_init_settings(struct net_device *dev)
2662 struct virtnet_info *vi = netdev_priv(dev);
2664 vi->speed = SPEED_UNKNOWN;
2665 vi->duplex = DUPLEX_UNKNOWN;
2668 static void virtnet_update_settings(struct virtnet_info *vi)
2673 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2676 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
2678 if (ethtool_validate_speed(speed))
2681 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
2683 if (ethtool_validate_duplex(duplex))
2684 vi->duplex = duplex;
2687 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
2689 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
2692 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
2694 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
2697 static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
2699 struct virtnet_info *vi = netdev_priv(dev);
2703 for (i = 0; i < vi->rss_indir_table_size; ++i)
2704 indir[i] = vi->ctrl->rss.indirection_table[i];
2708 memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
2711 *hfunc = ETH_RSS_HASH_TOP;
2716 static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
2718 struct virtnet_info *vi = netdev_priv(dev);
2721 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
2725 for (i = 0; i < vi->rss_indir_table_size; ++i)
2726 vi->ctrl->rss.indirection_table[i] = indir[i];
2729 memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
2731 virtnet_commit_rss_command(vi);
2736 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
2738 struct virtnet_info *vi = netdev_priv(dev);
2741 switch (info->cmd) {
2742 case ETHTOOL_GRXRINGS:
2743 info->data = vi->curr_queue_pairs;
2746 virtnet_get_hashflow(vi, info);
2755 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
2757 struct virtnet_info *vi = netdev_priv(dev);
2760 switch (info->cmd) {
2762 if (!virtnet_set_hashflow(vi, info))
2773 static const struct ethtool_ops virtnet_ethtool_ops = {
2774 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
2775 .get_drvinfo = virtnet_get_drvinfo,
2776 .get_link = ethtool_op_get_link,
2777 .get_ringparam = virtnet_get_ringparam,
2778 .get_strings = virtnet_get_strings,
2779 .get_sset_count = virtnet_get_sset_count,
2780 .get_ethtool_stats = virtnet_get_ethtool_stats,
2781 .set_channels = virtnet_set_channels,
2782 .get_channels = virtnet_get_channels,
2783 .get_ts_info = ethtool_op_get_ts_info,
2784 .get_link_ksettings = virtnet_get_link_ksettings,
2785 .set_link_ksettings = virtnet_set_link_ksettings,
2786 .set_coalesce = virtnet_set_coalesce,
2787 .get_coalesce = virtnet_get_coalesce,
2788 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
2789 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
2790 .get_rxfh = virtnet_get_rxfh,
2791 .set_rxfh = virtnet_set_rxfh,
2792 .get_rxnfc = virtnet_get_rxnfc,
2793 .set_rxnfc = virtnet_set_rxnfc,
2796 static void virtnet_freeze_down(struct virtio_device *vdev)
2798 struct virtnet_info *vi = vdev->priv;
2800 /* Make sure no work handler is accessing the device */
2801 flush_work(&vi->config_work);
2803 netif_tx_lock_bh(vi->dev);
2804 netif_device_detach(vi->dev);
2805 netif_tx_unlock_bh(vi->dev);
2806 if (netif_running(vi->dev))
2807 virtnet_close(vi->dev);
2810 static int init_vqs(struct virtnet_info *vi);
2812 static int virtnet_restore_up(struct virtio_device *vdev)
2814 struct virtnet_info *vi = vdev->priv;
2821 virtio_device_ready(vdev);
2823 enable_delayed_refill(vi);
2825 if (netif_running(vi->dev)) {
2826 err = virtnet_open(vi->dev);
2831 netif_tx_lock_bh(vi->dev);
2832 netif_device_attach(vi->dev);
2833 netif_tx_unlock_bh(vi->dev);
2837 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2839 struct scatterlist sg;
2840 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
2842 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
2844 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2845 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
2846 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
2853 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2857 if (!vi->guest_offloads)
2860 return virtnet_set_guest_offloads(vi, offloads);
2863 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2865 u64 offloads = vi->guest_offloads;
2867 if (!vi->guest_offloads)
2870 return virtnet_set_guest_offloads(vi, offloads);
2873 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2874 struct netlink_ext_ack *extack)
2876 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2877 struct virtnet_info *vi = netdev_priv(dev);
2878 struct bpf_prog *old_prog;
2879 u16 xdp_qp = 0, curr_qp;
2882 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2883 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2884 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2885 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
2886 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
2887 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
2888 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
2892 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
2893 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
2897 if (dev->mtu > max_sz) {
2898 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
2899 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2903 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2905 xdp_qp = nr_cpu_ids;
2907 /* XDP requires extra queues for XDP_TX */
2908 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
2909 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",
2910 curr_qp + xdp_qp, vi->max_queue_pairs);
2914 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
2915 if (!prog && !old_prog)
2919 bpf_prog_add(prog, vi->max_queue_pairs - 1);
2921 /* Make sure NAPI is not using any XDP TX queues for RX. */
2922 if (netif_running(dev)) {
2923 for (i = 0; i < vi->max_queue_pairs; i++) {
2924 napi_disable(&vi->rq[i].napi);
2925 virtnet_napi_tx_disable(&vi->sq[i].napi);
2930 for (i = 0; i < vi->max_queue_pairs; i++) {
2931 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2933 virtnet_restore_guest_offloads(vi);
2938 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2941 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
2942 vi->xdp_queue_pairs = xdp_qp;
2945 vi->xdp_enabled = true;
2946 for (i = 0; i < vi->max_queue_pairs; i++) {
2947 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2948 if (i == 0 && !old_prog)
2949 virtnet_clear_guest_offloads(vi);
2952 vi->xdp_enabled = false;
2955 for (i = 0; i < vi->max_queue_pairs; i++) {
2957 bpf_prog_put(old_prog);
2958 if (netif_running(dev)) {
2959 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2960 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2969 virtnet_clear_guest_offloads(vi);
2970 for (i = 0; i < vi->max_queue_pairs; i++)
2971 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
2974 if (netif_running(dev)) {
2975 for (i = 0; i < vi->max_queue_pairs; i++) {
2976 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2977 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2982 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2986 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2988 switch (xdp->command) {
2989 case XDP_SETUP_PROG:
2990 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
2996 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2999 struct virtnet_info *vi = netdev_priv(dev);
3002 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3005 ret = snprintf(buf, len, "sby");
3012 static int virtnet_set_features(struct net_device *dev,
3013 netdev_features_t features)
3015 struct virtnet_info *vi = netdev_priv(dev);
3019 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
3020 if (vi->xdp_enabled)
3023 if (features & NETIF_F_GRO_HW)
3024 offloads = vi->guest_offloads_capable;
3026 offloads = vi->guest_offloads_capable &
3027 ~GUEST_OFFLOAD_GRO_HW_MASK;
3029 err = virtnet_set_guest_offloads(vi, offloads);
3032 vi->guest_offloads = offloads;
3035 if ((dev->features ^ features) & NETIF_F_RXHASH) {
3036 if (features & NETIF_F_RXHASH)
3037 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3039 vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
3041 if (!virtnet_commit_rss_command(vi))
3048 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
3050 struct virtnet_info *priv = netdev_priv(dev);
3051 struct send_queue *sq = &priv->sq[txqueue];
3052 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
3054 u64_stats_update_begin(&sq->stats.syncp);
3055 sq->stats.tx_timeouts++;
3056 u64_stats_update_end(&sq->stats.syncp);
3058 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
3059 txqueue, sq->name, sq->vq->index, sq->vq->name,
3060 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
3063 static const struct net_device_ops virtnet_netdev = {
3064 .ndo_open = virtnet_open,
3065 .ndo_stop = virtnet_close,
3066 .ndo_start_xmit = start_xmit,
3067 .ndo_validate_addr = eth_validate_addr,
3068 .ndo_set_mac_address = virtnet_set_mac_address,
3069 .ndo_set_rx_mode = virtnet_set_rx_mode,
3070 .ndo_get_stats64 = virtnet_stats,
3071 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
3072 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
3073 .ndo_bpf = virtnet_xdp,
3074 .ndo_xdp_xmit = virtnet_xdp_xmit,
3075 .ndo_features_check = passthru_features_check,
3076 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
3077 .ndo_set_features = virtnet_set_features,
3078 .ndo_tx_timeout = virtnet_tx_timeout,
3081 static void virtnet_config_changed_work(struct work_struct *work)
3083 struct virtnet_info *vi =
3084 container_of(work, struct virtnet_info, config_work);
3087 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
3088 struct virtio_net_config, status, &v) < 0)
3091 if (v & VIRTIO_NET_S_ANNOUNCE) {
3092 netdev_notify_peers(vi->dev);
3093 virtnet_ack_link_announce(vi);
3096 /* Ignore unknown (future) status bits */
3097 v &= VIRTIO_NET_S_LINK_UP;
3099 if (vi->status == v)
3104 if (vi->status & VIRTIO_NET_S_LINK_UP) {
3105 virtnet_update_settings(vi);
3106 netif_carrier_on(vi->dev);
3107 netif_tx_wake_all_queues(vi->dev);
3109 netif_carrier_off(vi->dev);
3110 netif_tx_stop_all_queues(vi->dev);
3114 static void virtnet_config_changed(struct virtio_device *vdev)
3116 struct virtnet_info *vi = vdev->priv;
3118 schedule_work(&vi->config_work);
3121 static void virtnet_free_queues(struct virtnet_info *vi)
3125 for (i = 0; i < vi->max_queue_pairs; i++) {
3126 __netif_napi_del(&vi->rq[i].napi);
3127 __netif_napi_del(&vi->sq[i].napi);
3130 /* We called __netif_napi_del(),
3131 * we need to respect an RCU grace period before freeing vi->rq
3140 static void _free_receive_bufs(struct virtnet_info *vi)
3142 struct bpf_prog *old_prog;
3145 for (i = 0; i < vi->max_queue_pairs; i++) {
3146 while (vi->rq[i].pages)
3147 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
3149 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
3150 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
3152 bpf_prog_put(old_prog);
3156 static void free_receive_bufs(struct virtnet_info *vi)
3159 _free_receive_bufs(vi);
3163 static void free_receive_page_frags(struct virtnet_info *vi)
3166 for (i = 0; i < vi->max_queue_pairs; i++)
3167 if (vi->rq[i].alloc_frag.page)
3168 put_page(vi->rq[i].alloc_frag.page);
3171 static void free_unused_bufs(struct virtnet_info *vi)
3176 for (i = 0; i < vi->max_queue_pairs; i++) {
3177 struct virtqueue *vq = vi->sq[i].vq;
3178 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
3179 if (!is_xdp_frame(buf))
3182 xdp_return_frame(ptr_to_xdp(buf));
3186 for (i = 0; i < vi->max_queue_pairs; i++) {
3187 struct virtqueue *vq = vi->rq[i].vq;
3189 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
3190 if (vi->mergeable_rx_bufs) {
3191 put_page(virt_to_head_page(buf));
3192 } else if (vi->big_packets) {
3193 give_pages(&vi->rq[i], buf);
3195 put_page(virt_to_head_page(buf));
3201 static void virtnet_del_vqs(struct virtnet_info *vi)
3203 struct virtio_device *vdev = vi->vdev;
3205 virtnet_clean_affinity(vi);
3207 vdev->config->del_vqs(vdev);
3209 virtnet_free_queues(vi);
3212 /* How large should a single buffer be so a queue full of these can fit at
3213 * least one full packet?
3214 * Logic below assumes the mergeable buffer header is used.
3216 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
3218 const unsigned int hdr_len = vi->hdr_len;
3219 unsigned int rq_size = virtqueue_get_vring_size(vq);
3220 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
3221 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
3222 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
3224 return max(max(min_buf_len, hdr_len) - hdr_len,
3225 (unsigned int)GOOD_PACKET_LEN);
3228 static int virtnet_find_vqs(struct virtnet_info *vi)
3230 vq_callback_t **callbacks;
3231 struct virtqueue **vqs;
3237 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
3238 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
3239 * possible control vq.
3241 total_vqs = vi->max_queue_pairs * 2 +
3242 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
3244 /* Allocate space for find_vqs parameters */
3245 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
3248 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
3251 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
3254 if (!vi->big_packets || vi->mergeable_rx_bufs) {
3255 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
3262 /* Parameters for control virtqueue, if any */
3264 callbacks[total_vqs - 1] = NULL;
3265 names[total_vqs - 1] = "control";
3268 /* Allocate/initialize parameters for send/receive virtqueues */
3269 for (i = 0; i < vi->max_queue_pairs; i++) {
3270 callbacks[rxq2vq(i)] = skb_recv_done;
3271 callbacks[txq2vq(i)] = skb_xmit_done;
3272 sprintf(vi->rq[i].name, "input.%d", i);
3273 sprintf(vi->sq[i].name, "output.%d", i);
3274 names[rxq2vq(i)] = vi->rq[i].name;
3275 names[txq2vq(i)] = vi->sq[i].name;
3277 ctx[rxq2vq(i)] = true;
3280 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
3286 vi->cvq = vqs[total_vqs - 1];
3287 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
3288 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3291 for (i = 0; i < vi->max_queue_pairs; i++) {
3292 vi->rq[i].vq = vqs[rxq2vq(i)];
3293 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
3294 vi->sq[i].vq = vqs[txq2vq(i)];
3297 /* run here: ret == 0. */
3312 static int virtnet_alloc_queues(struct virtnet_info *vi)
3317 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
3323 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
3326 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
3330 INIT_DELAYED_WORK(&vi->refill, refill_work);
3331 for (i = 0; i < vi->max_queue_pairs; i++) {
3332 vi->rq[i].pages = NULL;
3333 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
3335 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
3337 napi_tx ? napi_weight : 0);
3339 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
3340 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
3341 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
3343 u64_stats_init(&vi->rq[i].stats.syncp);
3344 u64_stats_init(&vi->sq[i].stats.syncp);
3357 static int init_vqs(struct virtnet_info *vi)
3361 /* Allocate send & receive queues */
3362 ret = virtnet_alloc_queues(vi);
3366 ret = virtnet_find_vqs(vi);
3371 virtnet_set_affinity(vi);
3377 virtnet_free_queues(vi);
3383 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
3386 struct virtnet_info *vi = netdev_priv(queue->dev);
3387 unsigned int queue_index = get_netdev_rx_queue_index(queue);
3388 unsigned int headroom = virtnet_get_headroom(vi);
3389 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
3390 struct ewma_pkt_len *avg;
3392 BUG_ON(queue_index >= vi->max_queue_pairs);
3393 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
3394 return sprintf(buf, "%u\n",
3395 get_mergeable_buf_len(&vi->rq[queue_index], avg,
3396 SKB_DATA_ALIGN(headroom + tailroom)));
3399 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
3400 __ATTR_RO(mergeable_rx_buffer_size);
3402 static struct attribute *virtio_net_mrg_rx_attrs[] = {
3403 &mergeable_rx_buffer_size_attribute.attr,
3407 static const struct attribute_group virtio_net_mrg_rx_group = {
3408 .name = "virtio_net",
3409 .attrs = virtio_net_mrg_rx_attrs
3413 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
3415 const char *fname, const char *dname)
3417 if (!virtio_has_feature(vdev, fbit))
3420 dev_err(&vdev->dev, "device advertises feature %s but not %s",
3426 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
3427 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
3429 static bool virtnet_validate_features(struct virtio_device *vdev)
3431 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
3432 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
3433 "VIRTIO_NET_F_CTRL_VQ") ||
3434 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
3435 "VIRTIO_NET_F_CTRL_VQ") ||
3436 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
3437 "VIRTIO_NET_F_CTRL_VQ") ||
3438 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
3439 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
3440 "VIRTIO_NET_F_CTRL_VQ") ||
3441 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
3442 "VIRTIO_NET_F_CTRL_VQ") ||
3443 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
3444 "VIRTIO_NET_F_CTRL_VQ"))) {
3451 #define MIN_MTU ETH_MIN_MTU
3452 #define MAX_MTU ETH_MAX_MTU
3454 static int virtnet_validate(struct virtio_device *vdev)
3456 if (!vdev->config->get) {
3457 dev_err(&vdev->dev, "%s failure: config access disabled\n",
3462 if (!virtnet_validate_features(vdev))
3465 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3466 int mtu = virtio_cread16(vdev,
3467 offsetof(struct virtio_net_config,
3470 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3476 static int virtnet_probe(struct virtio_device *vdev)
3478 int i, err = -ENOMEM;
3479 struct net_device *dev;
3480 struct virtnet_info *vi;
3481 u16 max_queue_pairs;
3484 /* Find if host supports multiqueue/rss virtio_net device */
3485 max_queue_pairs = 1;
3486 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3488 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
3490 /* We need at least 2 queue's */
3491 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3492 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3493 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3494 max_queue_pairs = 1;
3496 /* Allocate ourselves a network device with room for our info */
3497 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
3501 /* Set up network device as normal. */
3502 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3503 IFF_TX_SKB_NO_LINEAR;
3504 dev->netdev_ops = &virtnet_netdev;
3505 dev->features = NETIF_F_HIGHDMA;
3507 dev->ethtool_ops = &virtnet_ethtool_ops;
3508 SET_NETDEV_DEV(dev, &vdev->dev);
3510 /* Do we support "hardware" checksums? */
3511 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
3512 /* This opens up the world of extra features. */
3513 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3515 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3517 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
3518 dev->hw_features |= NETIF_F_TSO
3519 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
3521 /* Individual feature bits: what can host handle? */
3522 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
3523 dev->hw_features |= NETIF_F_TSO;
3524 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
3525 dev->hw_features |= NETIF_F_TSO6;
3526 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
3527 dev->hw_features |= NETIF_F_TSO_ECN;
3529 dev->features |= NETIF_F_GSO_ROBUST;
3532 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
3533 /* (!csum && gso) case will be fixed by register_netdev() */
3535 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
3536 dev->features |= NETIF_F_RXCSUM;
3537 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3538 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
3539 dev->features |= NETIF_F_GRO_HW;
3540 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
3541 dev->hw_features |= NETIF_F_GRO_HW;
3543 dev->vlan_features = dev->features;
3545 /* MTU range: 68 - 65535 */
3546 dev->min_mtu = MIN_MTU;
3547 dev->max_mtu = MAX_MTU;
3549 /* Configuration may specify what MAC to use. Otherwise random. */
3550 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
3553 virtio_cread_bytes(vdev,
3554 offsetof(struct virtio_net_config, mac),
3556 eth_hw_addr_set(dev, addr);
3558 eth_hw_addr_random(dev);
3561 /* Set up our device-specific information */
3562 vi = netdev_priv(dev);
3567 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
3568 spin_lock_init(&vi->refill_lock);
3570 /* If we can receive ANY GSO packets, we must allocate large ones. */
3571 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3572 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3573 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
3574 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
3575 vi->big_packets = true;
3577 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
3578 vi->mergeable_rx_bufs = true;
3580 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
3581 vi->has_rss_hash_report = true;
3583 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3586 if (vi->has_rss || vi->has_rss_hash_report) {
3587 vi->rss_indir_table_size =
3588 virtio_cread16(vdev, offsetof(struct virtio_net_config,
3589 rss_max_indirection_table_length));
3591 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
3593 vi->rss_hash_types_supported =
3594 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
3595 vi->rss_hash_types_supported &=
3596 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
3597 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
3598 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
3600 dev->hw_features |= NETIF_F_RXHASH;
3603 if (vi->has_rss_hash_report)
3604 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
3605 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
3606 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3607 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3609 vi->hdr_len = sizeof(struct virtio_net_hdr);
3611 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
3612 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3613 vi->any_header_sg = true;
3615 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3618 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3619 mtu = virtio_cread16(vdev,
3620 offsetof(struct virtio_net_config,
3622 if (mtu < dev->min_mtu) {
3623 /* Should never trigger: MTU was previously validated
3624 * in virtnet_validate.
3627 "device MTU appears to have changed it is now %d < %d",
3636 /* TODO: size buffers correctly in this case. */
3637 if (dev->mtu > ETH_DATA_LEN)
3638 vi->big_packets = true;
3641 if (vi->any_header_sg)
3642 dev->needed_headroom = vi->hdr_len;
3644 /* Enable multiqueue by default */
3645 if (num_online_cpus() >= max_queue_pairs)
3646 vi->curr_queue_pairs = max_queue_pairs;
3648 vi->curr_queue_pairs = num_online_cpus();
3649 vi->max_queue_pairs = max_queue_pairs;
3651 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
3657 if (vi->mergeable_rx_bufs)
3658 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3660 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
3661 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
3663 virtnet_init_settings(dev);
3665 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3666 vi->failover = net_failover_create(vi->dev);
3667 if (IS_ERR(vi->failover)) {
3668 err = PTR_ERR(vi->failover);
3673 if (vi->has_rss || vi->has_rss_hash_report)
3674 virtnet_init_default_rss(vi);
3676 /* serialize netdev register + virtio_device_ready() with ndo_open() */
3679 err = register_netdevice(dev);
3681 pr_debug("virtio_net: registering device failed\n");
3686 virtio_device_ready(vdev);
3690 err = virtnet_cpu_notif_add(vi);
3692 pr_debug("virtio_net: registering cpu notifier failed\n");
3693 goto free_unregister_netdev;
3696 virtnet_set_queues(vi, vi->curr_queue_pairs);
3698 /* Assume link up if device can't report link status,
3699 otherwise get link status from config. */
3700 netif_carrier_off(dev);
3701 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
3702 schedule_work(&vi->config_work);
3704 vi->status = VIRTIO_NET_S_LINK_UP;
3705 virtnet_update_settings(vi);
3706 netif_carrier_on(dev);
3709 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3710 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3711 set_bit(guest_offloads[i], &vi->guest_offloads);
3712 vi->guest_offloads_capable = vi->guest_offloads;
3714 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3715 dev->name, max_queue_pairs);
3719 free_unregister_netdev:
3720 virtio_reset_device(vdev);
3722 unregister_netdev(dev);
3724 net_failover_destroy(vi->failover);
3726 cancel_delayed_work_sync(&vi->refill);
3727 free_receive_page_frags(vi);
3728 virtnet_del_vqs(vi);
3734 static void remove_vq_common(struct virtnet_info *vi)
3736 virtio_reset_device(vi->vdev);
3738 /* Free unused buffers in both send and recv, if any. */
3739 free_unused_bufs(vi);
3741 free_receive_bufs(vi);
3743 free_receive_page_frags(vi);
3745 virtnet_del_vqs(vi);
3748 static void virtnet_remove(struct virtio_device *vdev)
3750 struct virtnet_info *vi = vdev->priv;
3752 virtnet_cpu_notif_remove(vi);
3754 /* Make sure no work handler is accessing the device. */
3755 flush_work(&vi->config_work);
3757 unregister_netdev(vi->dev);
3759 net_failover_destroy(vi->failover);
3761 remove_vq_common(vi);
3763 free_netdev(vi->dev);
3766 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
3768 struct virtnet_info *vi = vdev->priv;
3770 virtnet_cpu_notif_remove(vi);
3771 virtnet_freeze_down(vdev);
3772 remove_vq_common(vi);
3777 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
3779 struct virtnet_info *vi = vdev->priv;
3782 err = virtnet_restore_up(vdev);
3785 virtnet_set_queues(vi, vi->curr_queue_pairs);
3787 err = virtnet_cpu_notif_add(vi);
3789 virtnet_freeze_down(vdev);
3790 remove_vq_common(vi);
3797 static struct virtio_device_id id_table[] = {
3798 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3802 #define VIRTNET_FEATURES \
3803 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3805 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3806 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3807 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3808 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3809 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3810 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3811 VIRTIO_NET_F_CTRL_MAC_ADDR, \
3812 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
3813 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
3814 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT
3816 static unsigned int features[] = {
3820 static unsigned int features_legacy[] = {
3823 VIRTIO_F_ANY_LAYOUT,
3826 static struct virtio_driver virtio_net_driver = {
3827 .feature_table = features,
3828 .feature_table_size = ARRAY_SIZE(features),
3829 .feature_table_legacy = features_legacy,
3830 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
3831 .driver.name = KBUILD_MODNAME,
3832 .driver.owner = THIS_MODULE,
3833 .id_table = id_table,
3834 .validate = virtnet_validate,
3835 .probe = virtnet_probe,
3836 .remove = virtnet_remove,
3837 .config_changed = virtnet_config_changed,
3838 #ifdef CONFIG_PM_SLEEP
3839 .freeze = virtnet_freeze,
3840 .restore = virtnet_restore,
3844 static __init int virtio_net_driver_init(void)
3848 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
3850 virtnet_cpu_down_prep);
3853 virtionet_online = ret;
3854 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
3855 NULL, virtnet_cpu_dead);
3858 ret = register_virtio_driver(&virtio_net_driver);
3863 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3865 cpuhp_remove_multi_state(virtionet_online);
3869 module_init(virtio_net_driver_init);
3871 static __exit void virtio_net_driver_exit(void)
3873 unregister_virtio_driver(&virtio_net_driver);
3874 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3875 cpuhp_remove_multi_state(virtionet_online);
3877 module_exit(virtio_net_driver_exit);
3879 MODULE_DEVICE_TABLE(virtio, id_table);
3880 MODULE_DESCRIPTION("Virtio network driver");
3881 MODULE_LICENSE("GPL");