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>
25 #include <net/netdev_rx_queue.h>
27 static int napi_weight = NAPI_POLL_WEIGHT;
28 module_param(napi_weight, int, 0444);
30 static bool csum = true, gso = true, napi_tx = true;
31 module_param(csum, bool, 0444);
32 module_param(gso, bool, 0444);
33 module_param(napi_tx, bool, 0644);
35 /* FIXME: MTU in config. */
36 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
37 #define GOOD_COPY_LEN 128
39 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
41 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
42 #define VIRTIO_XDP_HEADROOM 256
44 /* Separating two types of XDP xmit */
45 #define VIRTIO_XDP_TX BIT(0)
46 #define VIRTIO_XDP_REDIR BIT(1)
48 #define VIRTIO_XDP_FLAG BIT(0)
50 /* RX packet size EWMA. The average packet size is used to determine the packet
51 * buffer size when refilling RX rings. As the entire RX ring may be refilled
52 * at once, the weight is chosen so that the EWMA will be insensitive to short-
53 * term, transient changes in packet size.
55 DECLARE_EWMA(pkt_len, 0, 64)
57 #define VIRTNET_DRIVER_VERSION "1.0.0"
59 static const unsigned long guest_offloads[] = {
60 VIRTIO_NET_F_GUEST_TSO4,
61 VIRTIO_NET_F_GUEST_TSO6,
62 VIRTIO_NET_F_GUEST_ECN,
63 VIRTIO_NET_F_GUEST_UFO,
64 VIRTIO_NET_F_GUEST_CSUM,
65 VIRTIO_NET_F_GUEST_USO4,
66 VIRTIO_NET_F_GUEST_USO6,
67 VIRTIO_NET_F_GUEST_HDRLEN
70 #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
71 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
72 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
73 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
74 (1ULL << VIRTIO_NET_F_GUEST_USO4) | \
75 (1ULL << VIRTIO_NET_F_GUEST_USO6))
77 struct virtnet_stat_desc {
78 char desc[ETH_GSTRING_LEN];
82 struct virtnet_sq_stats {
83 struct u64_stats_sync syncp;
87 u64_stats_t xdp_tx_drops;
89 u64_stats_t tx_timeouts;
92 struct virtnet_rq_stats {
93 struct u64_stats_sync syncp;
97 u64_stats_t xdp_packets;
99 u64_stats_t xdp_redirects;
100 u64_stats_t xdp_drops;
104 #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
105 #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
107 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
108 { "packets", VIRTNET_SQ_STAT(packets) },
109 { "bytes", VIRTNET_SQ_STAT(bytes) },
110 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
111 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
112 { "kicks", VIRTNET_SQ_STAT(kicks) },
113 { "tx_timeouts", VIRTNET_SQ_STAT(tx_timeouts) },
116 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
117 { "packets", VIRTNET_RQ_STAT(packets) },
118 { "bytes", VIRTNET_RQ_STAT(bytes) },
119 { "drops", VIRTNET_RQ_STAT(drops) },
120 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
121 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
122 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
123 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
124 { "kicks", VIRTNET_RQ_STAT(kicks) },
127 #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
128 #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
130 struct virtnet_interrupt_coalesce {
135 /* The dma information of pages allocated at a time. */
136 struct virtnet_rq_dma {
143 /* Internal representation of a send virtqueue */
145 /* Virtqueue associated with this send _queue */
146 struct virtqueue *vq;
148 /* TX: fragments + linear part + virtio header */
149 struct scatterlist sg[MAX_SKB_FRAGS + 2];
151 /* Name of the send queue: output.$index */
154 struct virtnet_sq_stats stats;
156 struct virtnet_interrupt_coalesce intr_coal;
158 struct napi_struct napi;
160 /* Record whether sq is in reset state. */
164 /* Internal representation of a receive virtqueue */
165 struct receive_queue {
166 /* Virtqueue associated with this receive_queue */
167 struct virtqueue *vq;
169 struct napi_struct napi;
171 struct bpf_prog __rcu *xdp_prog;
173 struct virtnet_rq_stats stats;
175 struct virtnet_interrupt_coalesce intr_coal;
177 /* Chain pages by the private ptr. */
180 /* Average packet length for mergeable receive buffers. */
181 struct ewma_pkt_len mrg_avg_pkt_len;
183 /* Page frag for packet buffer allocation. */
184 struct page_frag alloc_frag;
186 /* RX: fragments + linear part + virtio header */
187 struct scatterlist sg[MAX_SKB_FRAGS + 2];
189 /* Min single buffer size for mergeable buffers case. */
190 unsigned int min_buf_len;
192 /* Name of this receive queue: input.$index */
195 struct xdp_rxq_info xdp_rxq;
197 /* Record the last dma info to free after new pages is allocated. */
198 struct virtnet_rq_dma *last_dma;
204 /* This structure can contain rss message with maximum settings for indirection table and keysize
205 * Note, that default structure that describes RSS configuration virtio_net_rss_config
206 * contains same info but can't handle table values.
207 * In any case, structure would be passed to virtio hw through sg_buf split by parts
208 * because table sizes may be differ according to the device configuration.
210 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
211 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
212 struct virtio_net_ctrl_rss {
214 u16 indirection_table_mask;
215 u16 unclassified_queue;
216 u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
219 u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
222 /* Control VQ buffers: protected by the rtnl lock */
224 struct virtio_net_ctrl_hdr hdr;
225 virtio_net_ctrl_ack status;
226 struct virtio_net_ctrl_mq mq;
231 struct virtio_net_ctrl_rss rss;
232 struct virtio_net_ctrl_coal_tx coal_tx;
233 struct virtio_net_ctrl_coal_rx coal_rx;
234 struct virtio_net_ctrl_coal_vq coal_vq;
237 struct virtnet_info {
238 struct virtio_device *vdev;
239 struct virtqueue *cvq;
240 struct net_device *dev;
241 struct send_queue *sq;
242 struct receive_queue *rq;
245 /* Max # of queue pairs supported by the device */
248 /* # of queue pairs currently used by the driver */
249 u16 curr_queue_pairs;
251 /* # of XDP queue pairs currently used by the driver */
254 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
257 /* I like... big packets and I cannot lie! */
260 /* number of sg entries allocated for big packets */
261 unsigned int big_packets_num_skbfrags;
263 /* Host will merge rx buffers for big packets (shake it! shake it!) */
264 bool mergeable_rx_bufs;
266 /* Host supports rss and/or hash report */
268 bool has_rss_hash_report;
270 u16 rss_indir_table_size;
271 u32 rss_hash_types_supported;
272 u32 rss_hash_types_saved;
274 /* Has control virtqueue */
277 /* Host can handle any s/g split between our header and packet data */
280 /* Packet virtio header size */
283 /* Work struct for delayed refilling if we run low on memory. */
284 struct delayed_work refill;
286 /* Is delayed refill enabled? */
289 /* The lock to synchronize the access to refill_enabled */
290 spinlock_t refill_lock;
292 /* Work struct for config space updates */
293 struct work_struct config_work;
295 /* Does the affinity hint is set for virtqueues? */
296 bool affinity_hint_set;
298 /* CPU hotplug instances for online & dead */
299 struct hlist_node node;
300 struct hlist_node node_dead;
302 struct control_buf *ctrl;
304 /* Ethtool settings */
308 /* Interrupt coalescing settings */
309 struct virtnet_interrupt_coalesce intr_coal_tx;
310 struct virtnet_interrupt_coalesce intr_coal_rx;
312 unsigned long guest_offloads;
313 unsigned long guest_offloads_capable;
315 /* failover when STANDBY feature enabled */
316 struct failover *failover;
319 struct padded_vnet_hdr {
320 struct virtio_net_hdr_v1_hash hdr;
322 * hdr is in a separate sg buffer, and data sg buffer shares same page
323 * with this header sg. This padding makes next sg 16 byte aligned
329 struct virtio_net_common_hdr {
331 struct virtio_net_hdr hdr;
332 struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
333 struct virtio_net_hdr_v1_hash hash_v1_hdr;
337 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
339 static bool is_xdp_frame(void *ptr)
341 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
344 static void *xdp_to_ptr(struct xdp_frame *ptr)
346 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
349 static struct xdp_frame *ptr_to_xdp(void *ptr)
351 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
354 /* Converting between virtqueue no. and kernel tx/rx queue no.
355 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
357 static int vq2txq(struct virtqueue *vq)
359 return (vq->index - 1) / 2;
362 static int txq2vq(int txq)
367 static int vq2rxq(struct virtqueue *vq)
369 return vq->index / 2;
372 static int rxq2vq(int rxq)
377 static inline struct virtio_net_common_hdr *
378 skb_vnet_common_hdr(struct sk_buff *skb)
380 return (struct virtio_net_common_hdr *)skb->cb;
384 * private is used to chain pages for big packets, put the whole
385 * most recent used list in the beginning for reuse
387 static void give_pages(struct receive_queue *rq, struct page *page)
391 /* Find end of list, sew whole thing into vi->rq.pages. */
392 for (end = page; end->private; end = (struct page *)end->private);
393 end->private = (unsigned long)rq->pages;
397 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
399 struct page *p = rq->pages;
402 rq->pages = (struct page *)p->private;
403 /* clear private here, it is used to chain pages */
406 p = alloc_page(gfp_mask);
410 static void virtnet_rq_free_buf(struct virtnet_info *vi,
411 struct receive_queue *rq, void *buf)
413 if (vi->mergeable_rx_bufs)
414 put_page(virt_to_head_page(buf));
415 else if (vi->big_packets)
418 put_page(virt_to_head_page(buf));
421 static void enable_delayed_refill(struct virtnet_info *vi)
423 spin_lock_bh(&vi->refill_lock);
424 vi->refill_enabled = true;
425 spin_unlock_bh(&vi->refill_lock);
428 static void disable_delayed_refill(struct virtnet_info *vi)
430 spin_lock_bh(&vi->refill_lock);
431 vi->refill_enabled = false;
432 spin_unlock_bh(&vi->refill_lock);
435 static void virtqueue_napi_schedule(struct napi_struct *napi,
436 struct virtqueue *vq)
438 if (napi_schedule_prep(napi)) {
439 virtqueue_disable_cb(vq);
440 __napi_schedule(napi);
444 static void virtqueue_napi_complete(struct napi_struct *napi,
445 struct virtqueue *vq, int processed)
449 opaque = virtqueue_enable_cb_prepare(vq);
450 if (napi_complete_done(napi, processed)) {
451 if (unlikely(virtqueue_poll(vq, opaque)))
452 virtqueue_napi_schedule(napi, vq);
454 virtqueue_disable_cb(vq);
458 static void skb_xmit_done(struct virtqueue *vq)
460 struct virtnet_info *vi = vq->vdev->priv;
461 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
463 /* Suppress further interrupts. */
464 virtqueue_disable_cb(vq);
467 virtqueue_napi_schedule(napi, vq);
469 /* We were probably waiting for more output buffers. */
470 netif_wake_subqueue(vi->dev, vq2txq(vq));
473 #define MRG_CTX_HEADER_SHIFT 22
474 static void *mergeable_len_to_ctx(unsigned int truesize,
475 unsigned int headroom)
477 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
480 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
482 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
485 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
487 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
490 static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen,
491 unsigned int headroom,
496 skb = build_skb(buf, buflen);
500 skb_reserve(skb, headroom);
506 /* Called from bottom half context */
507 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
508 struct receive_queue *rq,
509 struct page *page, unsigned int offset,
510 unsigned int len, unsigned int truesize,
511 unsigned int headroom)
514 struct virtio_net_common_hdr *hdr;
515 unsigned int copy, hdr_len, hdr_padded_len;
516 struct page *page_to_free = NULL;
517 int tailroom, shinfo_size;
518 char *p, *hdr_p, *buf;
520 p = page_address(page) + offset;
523 hdr_len = vi->hdr_len;
524 if (vi->mergeable_rx_bufs)
525 hdr_padded_len = hdr_len;
527 hdr_padded_len = sizeof(struct padded_vnet_hdr);
531 offset += hdr_padded_len;
533 tailroom = truesize - headroom - hdr_padded_len - len;
535 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
537 /* copy small packet so we can reuse these pages */
538 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
539 skb = virtnet_build_skb(buf, truesize, p - buf, len);
543 page = (struct page *)page->private;
545 give_pages(rq, page);
549 /* copy small packet so we can reuse these pages for small data */
550 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
554 /* Copy all frame if it fits skb->head, otherwise
555 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
557 if (len <= skb_tailroom(skb))
561 skb_put_data(skb, p, copy);
566 if (vi->mergeable_rx_bufs) {
568 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
575 * Verify that we can indeed put this data into a skb.
576 * This is here to handle cases when the device erroneously
577 * tries to receive more than is possible. This is usually
578 * the case of a broken device.
580 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
581 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
585 BUG_ON(offset >= PAGE_SIZE);
587 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
588 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
589 frag_size, truesize);
591 page = (struct page *)page->private;
596 give_pages(rq, page);
599 hdr = skb_vnet_common_hdr(skb);
600 memcpy(hdr, hdr_p, hdr_len);
602 put_page(page_to_free);
607 static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
609 struct page *page = virt_to_head_page(buf);
610 struct virtnet_rq_dma *dma;
614 head = page_address(page);
620 if (dma->need_sync && len) {
621 offset = buf - (head + sizeof(*dma));
623 virtqueue_dma_sync_single_range_for_cpu(rq->vq, dma->addr,
631 virtqueue_dma_unmap_single_attrs(rq->vq, dma->addr, dma->len,
632 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
636 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
640 buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
641 if (buf && rq->do_dma)
642 virtnet_rq_unmap(rq, buf, *len);
647 static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
649 struct virtnet_rq_dma *dma;
655 sg_init_one(rq->sg, buf, len);
659 head = page_address(rq->alloc_frag.page);
665 addr = dma->addr - sizeof(*dma) + offset;
667 sg_init_table(rq->sg, 1);
668 rq->sg[0].dma_address = addr;
669 rq->sg[0].length = len;
672 static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
674 struct page_frag *alloc_frag = &rq->alloc_frag;
675 struct virtnet_rq_dma *dma;
679 if (unlikely(!skb_page_frag_refill(size, alloc_frag, gfp)))
682 head = page_address(alloc_frag->page);
688 if (!alloc_frag->offset) {
690 /* Now, the new page is allocated, the last dma
691 * will not be used. So the dma can be unmapped
694 virtnet_rq_unmap(rq, rq->last_dma, 0);
698 dma->len = alloc_frag->size - sizeof(*dma);
700 addr = virtqueue_dma_map_single_attrs(rq->vq, dma + 1,
701 dma->len, DMA_FROM_DEVICE, 0);
702 if (virtqueue_dma_mapping_error(rq->vq, addr))
706 dma->need_sync = virtqueue_dma_need_sync(rq->vq, addr);
708 /* Add a reference to dma to prevent the entire dma from
709 * being released during error handling. This reference
710 * will be freed after the pages are no longer used.
712 get_page(alloc_frag->page);
714 alloc_frag->offset = sizeof(*dma);
722 buf = head + alloc_frag->offset;
724 get_page(alloc_frag->page);
725 alloc_frag->offset += size;
730 static void virtnet_rq_set_premapped(struct virtnet_info *vi)
734 /* disable for big mode */
735 if (!vi->mergeable_rx_bufs && vi->big_packets)
738 for (i = 0; i < vi->max_queue_pairs; i++) {
739 if (virtqueue_set_dma_premapped(vi->rq[i].vq))
742 vi->rq[i].do_dma = true;
746 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
748 struct virtnet_info *vi = vq->vdev->priv;
749 struct receive_queue *rq;
755 virtnet_rq_unmap(rq, buf, 0);
757 virtnet_rq_free_buf(vi, rq, buf);
760 static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
763 unsigned int packets = 0;
764 unsigned int bytes = 0;
767 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
768 if (likely(!is_xdp_frame(ptr))) {
769 struct sk_buff *skb = ptr;
771 pr_debug("Sent skb %p\n", skb);
774 napi_consume_skb(skb, in_napi);
776 struct xdp_frame *frame = ptr_to_xdp(ptr);
778 bytes += xdp_get_frame_len(frame);
779 xdp_return_frame(frame);
784 /* Avoid overhead when no packets have been processed
785 * happens when called speculatively from start_xmit.
790 u64_stats_update_begin(&sq->stats.syncp);
791 u64_stats_add(&sq->stats.bytes, bytes);
792 u64_stats_add(&sq->stats.packets, packets);
793 u64_stats_update_end(&sq->stats.syncp);
796 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
798 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
800 else if (q < vi->curr_queue_pairs)
806 static void check_sq_full_and_disable(struct virtnet_info *vi,
807 struct net_device *dev,
808 struct send_queue *sq)
810 bool use_napi = sq->napi.weight;
815 /* If running out of space, stop queue to avoid getting packets that we
816 * are then unable to transmit.
817 * An alternative would be to force queuing layer to requeue the skb by
818 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
819 * returned in a normal path of operation: it means that driver is not
820 * maintaining the TX queue stop/start state properly, and causes
821 * the stack to do a non-trivial amount of useless work.
822 * Since most packets only take 1 or 2 ring slots, stopping the queue
823 * early means 16 slots are typically wasted.
825 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
826 netif_stop_subqueue(dev, qnum);
828 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
829 virtqueue_napi_schedule(&sq->napi, sq->vq);
830 } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
831 /* More just got used, free them then recheck. */
832 free_old_xmit_skbs(sq, false);
833 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
834 netif_start_subqueue(dev, qnum);
835 virtqueue_disable_cb(sq->vq);
841 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
842 struct send_queue *sq,
843 struct xdp_frame *xdpf)
845 struct virtio_net_hdr_mrg_rxbuf *hdr;
846 struct skb_shared_info *shinfo;
850 if (unlikely(xdpf->headroom < vi->hdr_len))
853 if (unlikely(xdp_frame_has_frags(xdpf))) {
854 shinfo = xdp_get_shared_info_from_frame(xdpf);
855 nr_frags = shinfo->nr_frags;
858 /* In wrapping function virtnet_xdp_xmit(), we need to free
859 * up the pending old buffers, where we need to calculate the
860 * position of skb_shared_info in xdp_get_frame_len() and
861 * xdp_return_frame(), which will involve to xdpf->data and
862 * xdpf->headroom. Therefore, we need to update the value of
863 * headroom synchronously here.
865 xdpf->headroom -= vi->hdr_len;
866 xdpf->data -= vi->hdr_len;
867 /* Zero header and leave csum up to XDP layers */
869 memset(hdr, 0, vi->hdr_len);
870 xdpf->len += vi->hdr_len;
872 sg_init_table(sq->sg, nr_frags + 1);
873 sg_set_buf(sq->sg, xdpf->data, xdpf->len);
874 for (i = 0; i < nr_frags; i++) {
875 skb_frag_t *frag = &shinfo->frags[i];
877 sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
878 skb_frag_size(frag), skb_frag_off(frag));
881 err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
882 xdp_to_ptr(xdpf), GFP_ATOMIC);
884 return -ENOSPC; /* Caller handle free/refcnt */
889 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
890 * the current cpu, so it does not need to be locked.
892 * Here we use marco instead of inline functions because we have to deal with
893 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
894 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
895 * functions to perfectly solve these three problems at the same time.
897 #define virtnet_xdp_get_sq(vi) ({ \
898 int cpu = smp_processor_id(); \
899 struct netdev_queue *txq; \
900 typeof(vi) v = (vi); \
903 if (v->curr_queue_pairs > nr_cpu_ids) { \
904 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
906 txq = netdev_get_tx_queue(v->dev, qp); \
907 __netif_tx_acquire(txq); \
909 qp = cpu % v->curr_queue_pairs; \
910 txq = netdev_get_tx_queue(v->dev, qp); \
911 __netif_tx_lock(txq, cpu); \
916 #define virtnet_xdp_put_sq(vi, q) { \
917 struct netdev_queue *txq; \
918 typeof(vi) v = (vi); \
920 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
921 if (v->curr_queue_pairs > nr_cpu_ids) \
922 __netif_tx_release(txq); \
924 __netif_tx_unlock(txq); \
927 static int virtnet_xdp_xmit(struct net_device *dev,
928 int n, struct xdp_frame **frames, u32 flags)
930 struct virtnet_info *vi = netdev_priv(dev);
931 struct receive_queue *rq = vi->rq;
932 struct bpf_prog *xdp_prog;
933 struct send_queue *sq;
943 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
944 * indicate XDP resources have been successfully allocated.
946 xdp_prog = rcu_access_pointer(rq->xdp_prog);
950 sq = virtnet_xdp_get_sq(vi);
952 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
957 /* Free up any pending old buffers before queueing new ones. */
958 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
959 if (likely(is_xdp_frame(ptr))) {
960 struct xdp_frame *frame = ptr_to_xdp(ptr);
962 bytes += xdp_get_frame_len(frame);
963 xdp_return_frame(frame);
965 struct sk_buff *skb = ptr;
968 napi_consume_skb(skb, false);
973 for (i = 0; i < n; i++) {
974 struct xdp_frame *xdpf = frames[i];
976 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
982 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
983 check_sq_full_and_disable(vi, dev, sq);
985 if (flags & XDP_XMIT_FLUSH) {
986 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
990 u64_stats_update_begin(&sq->stats.syncp);
991 u64_stats_add(&sq->stats.bytes, bytes);
992 u64_stats_add(&sq->stats.packets, packets);
993 u64_stats_add(&sq->stats.xdp_tx, n);
994 u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit);
995 u64_stats_add(&sq->stats.kicks, kicks);
996 u64_stats_update_end(&sq->stats.syncp);
998 virtnet_xdp_put_sq(vi, sq);
1002 static void put_xdp_frags(struct xdp_buff *xdp)
1004 struct skb_shared_info *shinfo;
1005 struct page *xdp_page;
1008 if (xdp_buff_has_frags(xdp)) {
1009 shinfo = xdp_get_shared_info_from_buff(xdp);
1010 for (i = 0; i < shinfo->nr_frags; i++) {
1011 xdp_page = skb_frag_page(&shinfo->frags[i]);
1017 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
1018 struct net_device *dev,
1019 unsigned int *xdp_xmit,
1020 struct virtnet_rq_stats *stats)
1022 struct xdp_frame *xdpf;
1026 act = bpf_prog_run_xdp(xdp_prog, xdp);
1027 u64_stats_inc(&stats->xdp_packets);
1034 u64_stats_inc(&stats->xdp_tx);
1035 xdpf = xdp_convert_buff_to_frame(xdp);
1036 if (unlikely(!xdpf)) {
1037 netdev_dbg(dev, "convert buff to frame failed for xdp\n");
1041 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1042 if (unlikely(!err)) {
1043 xdp_return_frame_rx_napi(xdpf);
1044 } else if (unlikely(err < 0)) {
1045 trace_xdp_exception(dev, xdp_prog, act);
1048 *xdp_xmit |= VIRTIO_XDP_TX;
1052 u64_stats_inc(&stats->xdp_redirects);
1053 err = xdp_do_redirect(dev, xdp, xdp_prog);
1057 *xdp_xmit |= VIRTIO_XDP_REDIR;
1061 bpf_warn_invalid_xdp_action(dev, xdp_prog, act);
1064 trace_xdp_exception(dev, xdp_prog, act);
1071 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
1073 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
1076 /* We copy the packet for XDP in the following cases:
1078 * 1) Packet is scattered across multiple rx buffers.
1079 * 2) Headroom space is insufficient.
1081 * This is inefficient but it's a temporary condition that
1082 * we hit right after XDP is enabled and until queue is refilled
1083 * with large buffers with sufficient headroom - so it should affect
1084 * at most queue size packets.
1085 * Afterwards, the conditions to enable
1086 * XDP should preclude the underlying device from sending packets
1087 * across multiple buffers (num_buf > 1), and we make sure buffers
1088 * have enough headroom.
1090 static struct page *xdp_linearize_page(struct receive_queue *rq,
1097 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1100 if (page_off + *len + tailroom > PAGE_SIZE)
1103 page = alloc_page(GFP_ATOMIC);
1107 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
1110 while (--*num_buf) {
1111 unsigned int buflen;
1115 buf = virtnet_rq_get_buf(rq, &buflen, NULL);
1119 p = virt_to_head_page(buf);
1120 off = buf - page_address(p);
1122 /* guard against a misconfigured or uncooperative backend that
1123 * is sending packet larger than the MTU.
1125 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
1130 memcpy(page_address(page) + page_off,
1131 page_address(p) + off, buflen);
1136 /* Headroom does not contribute to packet length */
1137 *len = page_off - VIRTIO_XDP_HEADROOM;
1140 __free_pages(page, 0);
1144 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
1145 unsigned int xdp_headroom,
1149 unsigned int header_offset;
1150 unsigned int headroom;
1151 unsigned int buflen;
1152 struct sk_buff *skb;
1154 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1155 headroom = vi->hdr_len + header_offset;
1156 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1157 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1159 skb = virtnet_build_skb(buf, buflen, headroom, len);
1163 buf += header_offset;
1164 memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
1169 static struct sk_buff *receive_small_xdp(struct net_device *dev,
1170 struct virtnet_info *vi,
1171 struct receive_queue *rq,
1172 struct bpf_prog *xdp_prog,
1174 unsigned int xdp_headroom,
1176 unsigned int *xdp_xmit,
1177 struct virtnet_rq_stats *stats)
1179 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
1180 unsigned int headroom = vi->hdr_len + header_offset;
1181 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
1182 struct page *page = virt_to_head_page(buf);
1183 struct page *xdp_page;
1184 unsigned int buflen;
1185 struct xdp_buff xdp;
1186 struct sk_buff *skb;
1187 unsigned int metasize = 0;
1190 if (unlikely(hdr->hdr.gso_type))
1193 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1194 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1196 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
1197 int offset = buf - page_address(page) + header_offset;
1198 unsigned int tlen = len + vi->hdr_len;
1201 xdp_headroom = virtnet_get_headroom(vi);
1202 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1203 headroom = vi->hdr_len + header_offset;
1204 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1205 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1206 xdp_page = xdp_linearize_page(rq, &num_buf, page,
1207 offset, header_offset,
1212 buf = page_address(xdp_page);
1217 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
1218 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
1219 xdp_headroom, len, true);
1221 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1225 /* Recalculate length in case bpf program changed it */
1226 len = xdp.data_end - xdp.data;
1227 metasize = xdp.data - xdp.data_meta;
1238 skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len);
1243 skb_metadata_set(skb, metasize);
1248 u64_stats_inc(&stats->xdp_drops);
1250 u64_stats_inc(&stats->drops);
1256 static struct sk_buff *receive_small(struct net_device *dev,
1257 struct virtnet_info *vi,
1258 struct receive_queue *rq,
1259 void *buf, void *ctx,
1261 unsigned int *xdp_xmit,
1262 struct virtnet_rq_stats *stats)
1264 unsigned int xdp_headroom = (unsigned long)ctx;
1265 struct page *page = virt_to_head_page(buf);
1266 struct sk_buff *skb;
1269 u64_stats_add(&stats->bytes, len);
1271 if (unlikely(len > GOOD_PACKET_LEN)) {
1272 pr_debug("%s: rx error: len %u exceeds max size %d\n",
1273 dev->name, len, GOOD_PACKET_LEN);
1274 DEV_STATS_INC(dev, rx_length_errors);
1278 if (unlikely(vi->xdp_enabled)) {
1279 struct bpf_prog *xdp_prog;
1282 xdp_prog = rcu_dereference(rq->xdp_prog);
1284 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf,
1285 xdp_headroom, len, xdp_xmit,
1293 skb = receive_small_build_skb(vi, xdp_headroom, buf, len);
1298 u64_stats_inc(&stats->drops);
1303 static struct sk_buff *receive_big(struct net_device *dev,
1304 struct virtnet_info *vi,
1305 struct receive_queue *rq,
1308 struct virtnet_rq_stats *stats)
1310 struct page *page = buf;
1311 struct sk_buff *skb =
1312 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0);
1314 u64_stats_add(&stats->bytes, len - vi->hdr_len);
1321 u64_stats_inc(&stats->drops);
1322 give_pages(rq, page);
1326 static void mergeable_buf_free(struct receive_queue *rq, int num_buf,
1327 struct net_device *dev,
1328 struct virtnet_rq_stats *stats)
1334 while (num_buf-- > 1) {
1335 buf = virtnet_rq_get_buf(rq, &len, NULL);
1336 if (unlikely(!buf)) {
1337 pr_debug("%s: rx error: %d buffers missing\n",
1338 dev->name, num_buf);
1339 DEV_STATS_INC(dev, rx_length_errors);
1342 u64_stats_add(&stats->bytes, len);
1343 page = virt_to_head_page(buf);
1348 /* Why not use xdp_build_skb_from_frame() ?
1349 * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
1350 * virtio-net there are 2 points that do not match its requirements:
1351 * 1. The size of the prefilled buffer is not fixed before xdp is set.
1352 * 2. xdp_build_skb_from_frame() does more checks that we don't need,
1353 * like eth_type_trans() (which virtio-net does in receive_buf()).
1355 static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
1356 struct virtnet_info *vi,
1357 struct xdp_buff *xdp,
1358 unsigned int xdp_frags_truesz)
1360 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
1361 unsigned int headroom, data_len;
1362 struct sk_buff *skb;
1366 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
1367 pr_debug("Error building skb as missing reserved tailroom for xdp");
1371 if (unlikely(xdp_buff_has_frags(xdp)))
1372 nr_frags = sinfo->nr_frags;
1374 skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
1378 headroom = xdp->data - xdp->data_hard_start;
1379 data_len = xdp->data_end - xdp->data;
1380 skb_reserve(skb, headroom);
1381 __skb_put(skb, data_len);
1383 metasize = xdp->data - xdp->data_meta;
1384 metasize = metasize > 0 ? metasize : 0;
1386 skb_metadata_set(skb, metasize);
1388 if (unlikely(xdp_buff_has_frags(xdp)))
1389 xdp_update_skb_shared_info(skb, nr_frags,
1390 sinfo->xdp_frags_size,
1392 xdp_buff_is_frag_pfmemalloc(xdp));
1397 /* TODO: build xdp in big mode */
1398 static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
1399 struct virtnet_info *vi,
1400 struct receive_queue *rq,
1401 struct xdp_buff *xdp,
1404 unsigned int frame_sz,
1406 unsigned int *xdp_frags_truesize,
1407 struct virtnet_rq_stats *stats)
1409 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1410 unsigned int headroom, tailroom, room;
1411 unsigned int truesize, cur_frag_size;
1412 struct skb_shared_info *shinfo;
1413 unsigned int xdp_frags_truesz = 0;
1419 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
1420 xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM,
1421 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
1427 /* If we want to build multi-buffer xdp, we need
1428 * to specify that the flags of xdp_buff have the
1429 * XDP_FLAGS_HAS_FRAG bit.
1431 if (!xdp_buff_has_frags(xdp))
1432 xdp_buff_set_frags_flag(xdp);
1434 shinfo = xdp_get_shared_info_from_buff(xdp);
1435 shinfo->nr_frags = 0;
1436 shinfo->xdp_frags_size = 0;
1439 if (*num_buf > MAX_SKB_FRAGS + 1)
1442 while (--*num_buf > 0) {
1443 buf = virtnet_rq_get_buf(rq, &len, &ctx);
1444 if (unlikely(!buf)) {
1445 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1446 dev->name, *num_buf,
1447 virtio16_to_cpu(vi->vdev, hdr->num_buffers));
1448 DEV_STATS_INC(dev, rx_length_errors);
1452 u64_stats_add(&stats->bytes, len);
1453 page = virt_to_head_page(buf);
1454 offset = buf - page_address(page);
1456 truesize = mergeable_ctx_to_truesize(ctx);
1457 headroom = mergeable_ctx_to_headroom(ctx);
1458 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1459 room = SKB_DATA_ALIGN(headroom + tailroom);
1461 cur_frag_size = truesize;
1462 xdp_frags_truesz += cur_frag_size;
1463 if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) {
1465 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1466 dev->name, len, (unsigned long)(truesize - room));
1467 DEV_STATS_INC(dev, rx_length_errors);
1471 frag = &shinfo->frags[shinfo->nr_frags++];
1472 skb_frag_fill_page_desc(frag, page, offset, len);
1473 if (page_is_pfmemalloc(page))
1474 xdp_buff_set_frag_pfmemalloc(xdp);
1476 shinfo->xdp_frags_size += len;
1479 *xdp_frags_truesize = xdp_frags_truesz;
1487 static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
1488 struct receive_queue *rq,
1489 struct bpf_prog *xdp_prog,
1491 unsigned int *frame_sz,
1496 struct virtio_net_hdr_mrg_rxbuf *hdr)
1498 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
1499 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1500 struct page *xdp_page;
1501 unsigned int xdp_room;
1503 /* Transient failure which in theory could occur if
1504 * in-flight packets from before XDP was enabled reach
1505 * the receive path after XDP is loaded.
1507 if (unlikely(hdr->hdr.gso_type))
1510 /* Now XDP core assumes frag size is PAGE_SIZE, but buffers
1511 * with headroom may add hole in truesize, which
1512 * make their length exceed PAGE_SIZE. So we disabled the
1513 * hole mechanism for xdp. See add_recvbuf_mergeable().
1515 *frame_sz = truesize;
1517 if (likely(headroom >= virtnet_get_headroom(vi) &&
1518 (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) {
1519 return page_address(*page) + offset;
1522 /* This happens when headroom is not enough because
1523 * of the buffer was prefilled before XDP is set.
1524 * This should only happen for the first several packets.
1525 * In fact, vq reset can be used here to help us clean up
1526 * the prefilled buffers, but many existing devices do not
1527 * support it, and we don't want to bother users who are
1528 * using xdp normally.
1530 if (!xdp_prog->aux->xdp_has_frags) {
1531 /* linearize data for XDP */
1532 xdp_page = xdp_linearize_page(rq, num_buf,
1534 VIRTIO_XDP_HEADROOM,
1539 xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
1540 sizeof(struct skb_shared_info));
1541 if (*len + xdp_room > PAGE_SIZE)
1544 xdp_page = alloc_page(GFP_ATOMIC);
1548 memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM,
1549 page_address(*page) + offset, *len);
1552 *frame_sz = PAGE_SIZE;
1558 return page_address(*page) + VIRTIO_XDP_HEADROOM;
1561 static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
1562 struct virtnet_info *vi,
1563 struct receive_queue *rq,
1564 struct bpf_prog *xdp_prog,
1568 unsigned int *xdp_xmit,
1569 struct virtnet_rq_stats *stats)
1571 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1572 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1573 struct page *page = virt_to_head_page(buf);
1574 int offset = buf - page_address(page);
1575 unsigned int xdp_frags_truesz = 0;
1576 struct sk_buff *head_skb;
1577 unsigned int frame_sz;
1578 struct xdp_buff xdp;
1583 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page,
1585 if (unlikely(!data))
1588 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
1589 &num_buf, &xdp_frags_truesz, stats);
1593 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1597 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
1598 if (unlikely(!head_skb))
1610 put_xdp_frags(&xdp);
1614 mergeable_buf_free(rq, num_buf, dev, stats);
1616 u64_stats_inc(&stats->xdp_drops);
1617 u64_stats_inc(&stats->drops);
1621 static struct sk_buff *receive_mergeable(struct net_device *dev,
1622 struct virtnet_info *vi,
1623 struct receive_queue *rq,
1627 unsigned int *xdp_xmit,
1628 struct virtnet_rq_stats *stats)
1630 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1631 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1632 struct page *page = virt_to_head_page(buf);
1633 int offset = buf - page_address(page);
1634 struct sk_buff *head_skb, *curr_skb;
1635 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
1636 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1637 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1638 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1641 u64_stats_add(&stats->bytes, len - vi->hdr_len);
1643 if (unlikely(len > truesize - room)) {
1644 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1645 dev->name, len, (unsigned long)(truesize - room));
1646 DEV_STATS_INC(dev, rx_length_errors);
1650 if (unlikely(vi->xdp_enabled)) {
1651 struct bpf_prog *xdp_prog;
1654 xdp_prog = rcu_dereference(rq->xdp_prog);
1656 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx,
1657 len, xdp_xmit, stats);
1664 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom);
1665 curr_skb = head_skb;
1667 if (unlikely(!curr_skb))
1672 buf = virtnet_rq_get_buf(rq, &len, &ctx);
1673 if (unlikely(!buf)) {
1674 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1676 virtio16_to_cpu(vi->vdev,
1678 DEV_STATS_INC(dev, rx_length_errors);
1682 u64_stats_add(&stats->bytes, len);
1683 page = virt_to_head_page(buf);
1685 truesize = mergeable_ctx_to_truesize(ctx);
1686 headroom = mergeable_ctx_to_headroom(ctx);
1687 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1688 room = SKB_DATA_ALIGN(headroom + tailroom);
1689 if (unlikely(len > truesize - room)) {
1690 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1691 dev->name, len, (unsigned long)(truesize - room));
1692 DEV_STATS_INC(dev, rx_length_errors);
1696 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
1697 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1698 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
1700 if (unlikely(!nskb))
1702 if (curr_skb == head_skb)
1703 skb_shinfo(curr_skb)->frag_list = nskb;
1705 curr_skb->next = nskb;
1707 head_skb->truesize += nskb->truesize;
1710 if (curr_skb != head_skb) {
1711 head_skb->data_len += len;
1712 head_skb->len += len;
1713 head_skb->truesize += truesize;
1715 offset = buf - page_address(page);
1716 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1718 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1721 skb_add_rx_frag(curr_skb, num_skb_frags, page,
1722 offset, len, truesize);
1726 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
1731 mergeable_buf_free(rq, num_buf, dev, stats);
1734 u64_stats_inc(&stats->drops);
1735 dev_kfree_skb(head_skb);
1739 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
1740 struct sk_buff *skb)
1742 enum pkt_hash_types rss_hash_type;
1744 if (!hdr_hash || !skb)
1747 switch (__le16_to_cpu(hdr_hash->hash_report)) {
1748 case VIRTIO_NET_HASH_REPORT_TCPv4:
1749 case VIRTIO_NET_HASH_REPORT_UDPv4:
1750 case VIRTIO_NET_HASH_REPORT_TCPv6:
1751 case VIRTIO_NET_HASH_REPORT_UDPv6:
1752 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
1753 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
1754 rss_hash_type = PKT_HASH_TYPE_L4;
1756 case VIRTIO_NET_HASH_REPORT_IPv4:
1757 case VIRTIO_NET_HASH_REPORT_IPv6:
1758 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
1759 rss_hash_type = PKT_HASH_TYPE_L3;
1761 case VIRTIO_NET_HASH_REPORT_NONE:
1763 rss_hash_type = PKT_HASH_TYPE_NONE;
1765 skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
1768 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1769 void *buf, unsigned int len, void **ctx,
1770 unsigned int *xdp_xmit,
1771 struct virtnet_rq_stats *stats)
1773 struct net_device *dev = vi->dev;
1774 struct sk_buff *skb;
1775 struct virtio_net_common_hdr *hdr;
1777 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
1778 pr_debug("%s: short packet %i\n", dev->name, len);
1779 DEV_STATS_INC(dev, rx_length_errors);
1780 virtnet_rq_free_buf(vi, rq, buf);
1784 if (vi->mergeable_rx_bufs)
1785 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1787 else if (vi->big_packets)
1788 skb = receive_big(dev, vi, rq, buf, len, stats);
1790 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1795 hdr = skb_vnet_common_hdr(skb);
1796 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
1797 virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
1799 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
1800 skb->ip_summed = CHECKSUM_UNNECESSARY;
1802 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1803 virtio_is_little_endian(vi->vdev))) {
1804 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1805 dev->name, hdr->hdr.gso_type,
1810 skb_record_rx_queue(skb, vq2rxq(rq->vq));
1811 skb->protocol = eth_type_trans(skb, dev);
1812 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1813 ntohs(skb->protocol), skb->len, skb->pkt_type);
1815 napi_gro_receive(&rq->napi, skb);
1819 DEV_STATS_INC(dev, rx_frame_errors);
1823 /* Unlike mergeable buffers, all buffers are allocated to the
1824 * same size, except for the headroom. For this reason we do
1825 * not need to use mergeable_len_to_ctx here - it is enough
1826 * to store the headroom as the context ignoring the truesize.
1828 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1832 unsigned int xdp_headroom = virtnet_get_headroom(vi);
1833 void *ctx = (void *)(unsigned long)xdp_headroom;
1834 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
1837 len = SKB_DATA_ALIGN(len) +
1838 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1840 buf = virtnet_rq_alloc(rq, len, gfp);
1844 virtnet_rq_init_one_sg(rq, buf + VIRTNET_RX_PAD + xdp_headroom,
1845 vi->hdr_len + GOOD_PACKET_LEN);
1847 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1850 virtnet_rq_unmap(rq, buf, 0);
1851 put_page(virt_to_head_page(buf));
1857 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1860 struct page *first, *list = NULL;
1864 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
1866 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
1867 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
1868 first = get_a_page(rq, gfp);
1871 give_pages(rq, list);
1874 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
1876 /* chain new page in list head to match sg */
1877 first->private = (unsigned long)list;
1881 first = get_a_page(rq, gfp);
1883 give_pages(rq, list);
1886 p = page_address(first);
1888 /* rq->sg[0], rq->sg[1] share the same page */
1889 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1890 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
1892 /* rq->sg[1] for data packet, from offset */
1893 offset = sizeof(struct padded_vnet_hdr);
1894 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
1896 /* chain first in list head */
1897 first->private = (unsigned long)list;
1898 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
1901 give_pages(rq, first);
1906 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
1907 struct ewma_pkt_len *avg_pkt_len,
1910 struct virtnet_info *vi = rq->vq->vdev->priv;
1911 const size_t hdr_len = vi->hdr_len;
1915 return PAGE_SIZE - room;
1917 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1918 rq->min_buf_len, PAGE_SIZE - hdr_len);
1920 return ALIGN(len, L1_CACHE_BYTES);
1923 static int add_recvbuf_mergeable(struct virtnet_info *vi,
1924 struct receive_queue *rq, gfp_t gfp)
1926 struct page_frag *alloc_frag = &rq->alloc_frag;
1927 unsigned int headroom = virtnet_get_headroom(vi);
1928 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1929 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1930 unsigned int len, hole;
1935 /* Extra tailroom is needed to satisfy XDP's assumption. This
1936 * means rx frags coalescing won't work, but consider we've
1937 * disabled GSO for XDP, it won't be a big issue.
1939 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1941 buf = virtnet_rq_alloc(rq, len + room, gfp);
1945 buf += headroom; /* advance address leaving hole at front of pkt */
1946 hole = alloc_frag->size - alloc_frag->offset;
1947 if (hole < len + room) {
1948 /* To avoid internal fragmentation, if there is very likely not
1949 * enough space for another buffer, add the remaining space to
1950 * the current buffer.
1951 * XDP core assumes that frame_size of xdp_buff and the length
1952 * of the frag are PAGE_SIZE, so we disable the hole mechanism.
1956 alloc_frag->offset += hole;
1959 virtnet_rq_init_one_sg(rq, buf, len);
1961 ctx = mergeable_len_to_ctx(len + room, headroom);
1962 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1965 virtnet_rq_unmap(rq, buf, 0);
1966 put_page(virt_to_head_page(buf));
1973 * Returns false if we couldn't fill entirely (OOM).
1975 * Normally run in the receive path, but can also be run from ndo_open
1976 * before we're receiving packets, or from refill_work which is
1977 * careful to disable receiving (using napi_disable).
1979 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1986 if (vi->mergeable_rx_bufs)
1987 err = add_recvbuf_mergeable(vi, rq, gfp);
1988 else if (vi->big_packets)
1989 err = add_recvbuf_big(vi, rq, gfp);
1991 err = add_recvbuf_small(vi, rq, gfp);
1993 oom = err == -ENOMEM;
1996 } while (rq->vq->num_free);
1997 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1998 unsigned long flags;
2000 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
2001 u64_stats_inc(&rq->stats.kicks);
2002 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
2008 static void skb_recv_done(struct virtqueue *rvq)
2010 struct virtnet_info *vi = rvq->vdev->priv;
2011 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
2013 virtqueue_napi_schedule(&rq->napi, rvq);
2016 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
2020 /* If all buffers were filled by other side before we napi_enabled, we
2021 * won't get another interrupt, so process any outstanding packets now.
2022 * Call local_bh_enable after to trigger softIRQ processing.
2025 virtqueue_napi_schedule(napi, vq);
2029 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
2030 struct virtqueue *vq,
2031 struct napi_struct *napi)
2036 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
2037 * enable the feature if this is likely affine with the transmit path.
2039 if (!vi->affinity_hint_set) {
2044 return virtnet_napi_enable(vq, napi);
2047 static void virtnet_napi_tx_disable(struct napi_struct *napi)
2053 static void refill_work(struct work_struct *work)
2055 struct virtnet_info *vi =
2056 container_of(work, struct virtnet_info, refill.work);
2060 for (i = 0; i < vi->curr_queue_pairs; i++) {
2061 struct receive_queue *rq = &vi->rq[i];
2063 napi_disable(&rq->napi);
2064 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
2065 virtnet_napi_enable(rq->vq, &rq->napi);
2067 /* In theory, this can happen: if we don't get any buffers in
2068 * we will *never* try to fill again.
2071 schedule_delayed_work(&vi->refill, HZ/2);
2075 static int virtnet_receive(struct receive_queue *rq, int budget,
2076 unsigned int *xdp_xmit)
2078 struct virtnet_info *vi = rq->vq->vdev->priv;
2079 struct virtnet_rq_stats stats = {};
2085 if (!vi->big_packets || vi->mergeable_rx_bufs) {
2088 while (packets < budget &&
2089 (buf = virtnet_rq_get_buf(rq, &len, &ctx))) {
2090 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
2094 while (packets < budget &&
2095 (buf = virtnet_rq_get_buf(rq, &len, NULL)) != NULL) {
2096 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
2101 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
2102 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
2103 spin_lock(&vi->refill_lock);
2104 if (vi->refill_enabled)
2105 schedule_delayed_work(&vi->refill, 0);
2106 spin_unlock(&vi->refill_lock);
2110 u64_stats_set(&stats.packets, packets);
2111 u64_stats_update_begin(&rq->stats.syncp);
2112 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
2113 size_t offset = virtnet_rq_stats_desc[i].offset;
2114 u64_stats_t *item, *src;
2116 item = (u64_stats_t *)((u8 *)&rq->stats + offset);
2117 src = (u64_stats_t *)((u8 *)&stats + offset);
2118 u64_stats_add(item, u64_stats_read(src));
2120 u64_stats_update_end(&rq->stats.syncp);
2125 static void virtnet_poll_cleantx(struct receive_queue *rq)
2127 struct virtnet_info *vi = rq->vq->vdev->priv;
2128 unsigned int index = vq2rxq(rq->vq);
2129 struct send_queue *sq = &vi->sq[index];
2130 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
2132 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
2135 if (__netif_tx_trylock(txq)) {
2137 __netif_tx_unlock(txq);
2142 virtqueue_disable_cb(sq->vq);
2143 free_old_xmit_skbs(sq, true);
2144 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2146 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
2147 netif_tx_wake_queue(txq);
2149 __netif_tx_unlock(txq);
2153 static int virtnet_poll(struct napi_struct *napi, int budget)
2155 struct receive_queue *rq =
2156 container_of(napi, struct receive_queue, napi);
2157 struct virtnet_info *vi = rq->vq->vdev->priv;
2158 struct send_queue *sq;
2159 unsigned int received;
2160 unsigned int xdp_xmit = 0;
2162 virtnet_poll_cleantx(rq);
2164 received = virtnet_receive(rq, budget, &xdp_xmit);
2166 if (xdp_xmit & VIRTIO_XDP_REDIR)
2169 /* Out of packets? */
2170 if (received < budget)
2171 virtqueue_napi_complete(napi, rq->vq, received);
2173 if (xdp_xmit & VIRTIO_XDP_TX) {
2174 sq = virtnet_xdp_get_sq(vi);
2175 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2176 u64_stats_update_begin(&sq->stats.syncp);
2177 u64_stats_inc(&sq->stats.kicks);
2178 u64_stats_update_end(&sq->stats.syncp);
2180 virtnet_xdp_put_sq(vi, sq);
2186 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
2188 virtnet_napi_tx_disable(&vi->sq[qp_index].napi);
2189 napi_disable(&vi->rq[qp_index].napi);
2190 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
2193 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
2195 struct net_device *dev = vi->dev;
2198 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
2199 vi->rq[qp_index].napi.napi_id);
2203 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
2204 MEM_TYPE_PAGE_SHARED, NULL);
2206 goto err_xdp_reg_mem_model;
2208 virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi);
2209 virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi);
2213 err_xdp_reg_mem_model:
2214 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
2218 static int virtnet_open(struct net_device *dev)
2220 struct virtnet_info *vi = netdev_priv(dev);
2223 enable_delayed_refill(vi);
2225 for (i = 0; i < vi->max_queue_pairs; i++) {
2226 if (i < vi->curr_queue_pairs)
2227 /* Make sure we have some buffers: if oom use wq. */
2228 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2229 schedule_delayed_work(&vi->refill, 0);
2231 err = virtnet_enable_queue_pair(vi, i);
2239 disable_delayed_refill(vi);
2240 cancel_delayed_work_sync(&vi->refill);
2242 for (i--; i >= 0; i--)
2243 virtnet_disable_queue_pair(vi, i);
2247 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
2249 struct send_queue *sq = container_of(napi, struct send_queue, napi);
2250 struct virtnet_info *vi = sq->vq->vdev->priv;
2251 unsigned int index = vq2txq(sq->vq);
2252 struct netdev_queue *txq;
2256 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
2257 /* We don't need to enable cb for XDP */
2258 napi_complete_done(napi, 0);
2262 txq = netdev_get_tx_queue(vi->dev, index);
2263 __netif_tx_lock(txq, raw_smp_processor_id());
2264 virtqueue_disable_cb(sq->vq);
2265 free_old_xmit_skbs(sq, true);
2267 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
2268 netif_tx_wake_queue(txq);
2270 opaque = virtqueue_enable_cb_prepare(sq->vq);
2272 done = napi_complete_done(napi, 0);
2275 virtqueue_disable_cb(sq->vq);
2277 __netif_tx_unlock(txq);
2280 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
2281 if (napi_schedule_prep(napi)) {
2282 __netif_tx_lock(txq, raw_smp_processor_id());
2283 virtqueue_disable_cb(sq->vq);
2284 __netif_tx_unlock(txq);
2285 __napi_schedule(napi);
2293 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
2295 struct virtio_net_hdr_mrg_rxbuf *hdr;
2296 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
2297 struct virtnet_info *vi = sq->vq->vdev->priv;
2299 unsigned hdr_len = vi->hdr_len;
2302 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
2304 can_push = vi->any_header_sg &&
2305 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
2306 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
2307 /* Even if we can, don't push here yet as this would skew
2308 * csum_start offset below. */
2310 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
2312 hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
2314 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
2315 virtio_is_little_endian(vi->vdev), false,
2319 if (vi->mergeable_rx_bufs)
2320 hdr->num_buffers = 0;
2322 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
2324 __skb_push(skb, hdr_len);
2325 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
2326 if (unlikely(num_sg < 0))
2328 /* Pull header back to avoid skew in tx bytes calculations. */
2329 __skb_pull(skb, hdr_len);
2331 sg_set_buf(sq->sg, hdr, hdr_len);
2332 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
2333 if (unlikely(num_sg < 0))
2337 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
2340 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
2342 struct virtnet_info *vi = netdev_priv(dev);
2343 int qnum = skb_get_queue_mapping(skb);
2344 struct send_queue *sq = &vi->sq[qnum];
2346 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
2347 bool kick = !netdev_xmit_more();
2348 bool use_napi = sq->napi.weight;
2350 /* Free up any pending old buffers before queueing new ones. */
2353 virtqueue_disable_cb(sq->vq);
2355 free_old_xmit_skbs(sq, false);
2357 } while (use_napi && kick &&
2358 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2360 /* timestamp packet in software */
2361 skb_tx_timestamp(skb);
2363 /* Try to transmit */
2364 err = xmit_skb(sq, skb);
2366 /* This should not happen! */
2367 if (unlikely(err)) {
2368 DEV_STATS_INC(dev, tx_fifo_errors);
2369 if (net_ratelimit())
2371 "Unexpected TXQ (%d) queue failure: %d\n",
2373 DEV_STATS_INC(dev, tx_dropped);
2374 dev_kfree_skb_any(skb);
2375 return NETDEV_TX_OK;
2378 /* Don't wait up for transmitted skbs to be freed. */
2384 check_sq_full_and_disable(vi, dev, sq);
2386 if (kick || netif_xmit_stopped(txq)) {
2387 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2388 u64_stats_update_begin(&sq->stats.syncp);
2389 u64_stats_inc(&sq->stats.kicks);
2390 u64_stats_update_end(&sq->stats.syncp);
2394 return NETDEV_TX_OK;
2397 static int virtnet_rx_resize(struct virtnet_info *vi,
2398 struct receive_queue *rq, u32 ring_num)
2400 bool running = netif_running(vi->dev);
2403 qindex = rq - vi->rq;
2406 napi_disable(&rq->napi);
2408 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf);
2410 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
2412 if (!try_fill_recv(vi, rq, GFP_KERNEL))
2413 schedule_delayed_work(&vi->refill, 0);
2416 virtnet_napi_enable(rq->vq, &rq->napi);
2420 static int virtnet_tx_resize(struct virtnet_info *vi,
2421 struct send_queue *sq, u32 ring_num)
2423 bool running = netif_running(vi->dev);
2424 struct netdev_queue *txq;
2427 qindex = sq - vi->sq;
2430 virtnet_napi_tx_disable(&sq->napi);
2432 txq = netdev_get_tx_queue(vi->dev, qindex);
2434 /* 1. wait all ximt complete
2435 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
2437 __netif_tx_lock_bh(txq);
2439 /* Prevent rx poll from accessing sq. */
2442 /* Prevent the upper layer from trying to send packets. */
2443 netif_stop_subqueue(vi->dev, qindex);
2445 __netif_tx_unlock_bh(txq);
2447 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf);
2449 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
2451 __netif_tx_lock_bh(txq);
2453 netif_tx_wake_queue(txq);
2454 __netif_tx_unlock_bh(txq);
2457 virtnet_napi_tx_enable(vi, sq->vq, &sq->napi);
2462 * Send command via the control virtqueue and check status. Commands
2463 * supported by the hypervisor, as indicated by feature bits, should
2464 * never fail unless improperly formatted.
2466 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
2467 struct scatterlist *out)
2469 struct scatterlist *sgs[4], hdr, stat;
2470 unsigned out_num = 0, tmp;
2473 /* Caller should know better */
2474 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
2476 vi->ctrl->status = ~0;
2477 vi->ctrl->hdr.class = class;
2478 vi->ctrl->hdr.cmd = cmd;
2480 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
2481 sgs[out_num++] = &hdr;
2484 sgs[out_num++] = out;
2486 /* Add return status. */
2487 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
2488 sgs[out_num] = &stat;
2490 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
2491 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
2493 dev_warn(&vi->vdev->dev,
2494 "Failed to add sgs for command vq: %d\n.", ret);
2498 if (unlikely(!virtqueue_kick(vi->cvq)))
2499 return vi->ctrl->status == VIRTIO_NET_OK;
2501 /* Spin for a response, the kick causes an ioport write, trapping
2502 * into the hypervisor, so the request should be handled immediately.
2504 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
2505 !virtqueue_is_broken(vi->cvq))
2508 return vi->ctrl->status == VIRTIO_NET_OK;
2511 static int virtnet_set_mac_address(struct net_device *dev, void *p)
2513 struct virtnet_info *vi = netdev_priv(dev);
2514 struct virtio_device *vdev = vi->vdev;
2516 struct sockaddr *addr;
2517 struct scatterlist sg;
2519 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2522 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
2526 ret = eth_prepare_mac_addr_change(dev, addr);
2530 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
2531 sg_init_one(&sg, addr->sa_data, dev->addr_len);
2532 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2533 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
2534 dev_warn(&vdev->dev,
2535 "Failed to set mac address by vq command.\n");
2539 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
2540 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2543 /* Naturally, this has an atomicity problem. */
2544 for (i = 0; i < dev->addr_len; i++)
2545 virtio_cwrite8(vdev,
2546 offsetof(struct virtio_net_config, mac) +
2547 i, addr->sa_data[i]);
2550 eth_commit_mac_addr_change(dev, p);
2558 static void virtnet_stats(struct net_device *dev,
2559 struct rtnl_link_stats64 *tot)
2561 struct virtnet_info *vi = netdev_priv(dev);
2565 for (i = 0; i < vi->max_queue_pairs; i++) {
2566 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
2567 struct receive_queue *rq = &vi->rq[i];
2568 struct send_queue *sq = &vi->sq[i];
2571 start = u64_stats_fetch_begin(&sq->stats.syncp);
2572 tpackets = u64_stats_read(&sq->stats.packets);
2573 tbytes = u64_stats_read(&sq->stats.bytes);
2574 terrors = u64_stats_read(&sq->stats.tx_timeouts);
2575 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
2578 start = u64_stats_fetch_begin(&rq->stats.syncp);
2579 rpackets = u64_stats_read(&rq->stats.packets);
2580 rbytes = u64_stats_read(&rq->stats.bytes);
2581 rdrops = u64_stats_read(&rq->stats.drops);
2582 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
2584 tot->rx_packets += rpackets;
2585 tot->tx_packets += tpackets;
2586 tot->rx_bytes += rbytes;
2587 tot->tx_bytes += tbytes;
2588 tot->rx_dropped += rdrops;
2589 tot->tx_errors += terrors;
2592 tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
2593 tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors);
2594 tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors);
2595 tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors);
2598 static void virtnet_ack_link_announce(struct virtnet_info *vi)
2601 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
2602 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
2603 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
2607 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2609 struct scatterlist sg;
2610 struct net_device *dev = vi->dev;
2612 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
2615 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
2616 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
2618 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2619 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
2620 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
2624 vi->curr_queue_pairs = queue_pairs;
2625 /* virtnet_open() will refill when device is going to up. */
2626 if (dev->flags & IFF_UP)
2627 schedule_delayed_work(&vi->refill, 0);
2633 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2638 err = _virtnet_set_queues(vi, queue_pairs);
2643 static int virtnet_close(struct net_device *dev)
2645 struct virtnet_info *vi = netdev_priv(dev);
2648 /* Make sure NAPI doesn't schedule refill work */
2649 disable_delayed_refill(vi);
2650 /* Make sure refill_work doesn't re-enable napi! */
2651 cancel_delayed_work_sync(&vi->refill);
2653 for (i = 0; i < vi->max_queue_pairs; i++)
2654 virtnet_disable_queue_pair(vi, i);
2659 static void virtnet_set_rx_mode(struct net_device *dev)
2661 struct virtnet_info *vi = netdev_priv(dev);
2662 struct scatterlist sg[2];
2663 struct virtio_net_ctrl_mac *mac_data;
2664 struct netdev_hw_addr *ha;
2670 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
2671 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
2674 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
2675 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2677 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
2679 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2680 VIRTIO_NET_CTRL_RX_PROMISC, sg))
2681 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
2682 vi->ctrl->promisc ? "en" : "dis");
2684 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
2686 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2687 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2688 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2689 vi->ctrl->allmulti ? "en" : "dis");
2691 uc_count = netdev_uc_count(dev);
2692 mc_count = netdev_mc_count(dev);
2693 /* MAC filter - use one buffer for both lists */
2694 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2695 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
2700 sg_init_table(sg, 2);
2702 /* Store the unicast list and count in the front of the buffer */
2703 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2705 netdev_for_each_uc_addr(ha, dev)
2706 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2708 sg_set_buf(&sg[0], mac_data,
2709 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2711 /* multicast list and count fill the end */
2712 mac_data = (void *)&mac_data->macs[uc_count][0];
2714 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2716 netdev_for_each_mc_addr(ha, dev)
2717 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2719 sg_set_buf(&sg[1], mac_data,
2720 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2722 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2723 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
2724 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2729 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2730 __be16 proto, u16 vid)
2732 struct virtnet_info *vi = netdev_priv(dev);
2733 struct scatterlist sg;
2735 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2736 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2738 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2739 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
2740 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
2744 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2745 __be16 proto, u16 vid)
2747 struct virtnet_info *vi = netdev_priv(dev);
2748 struct scatterlist sg;
2750 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2751 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2753 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2754 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
2755 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
2759 static void virtnet_clean_affinity(struct virtnet_info *vi)
2763 if (vi->affinity_hint_set) {
2764 for (i = 0; i < vi->max_queue_pairs; i++) {
2765 virtqueue_set_affinity(vi->rq[i].vq, NULL);
2766 virtqueue_set_affinity(vi->sq[i].vq, NULL);
2769 vi->affinity_hint_set = false;
2773 static void virtnet_set_affinity(struct virtnet_info *vi)
2782 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2783 virtnet_clean_affinity(vi);
2787 num_cpu = num_online_cpus();
2788 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2789 stragglers = num_cpu >= vi->curr_queue_pairs ?
2790 num_cpu % vi->curr_queue_pairs :
2792 cpu = cpumask_first(cpu_online_mask);
2794 for (i = 0; i < vi->curr_queue_pairs; i++) {
2795 group_size = stride + (i < stragglers ? 1 : 0);
2797 for (j = 0; j < group_size; j++) {
2798 cpumask_set_cpu(cpu, mask);
2799 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2802 virtqueue_set_affinity(vi->rq[i].vq, mask);
2803 virtqueue_set_affinity(vi->sq[i].vq, mask);
2804 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
2805 cpumask_clear(mask);
2808 vi->affinity_hint_set = true;
2809 free_cpumask_var(mask);
2812 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
2814 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2816 virtnet_set_affinity(vi);
2820 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2822 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2824 virtnet_set_affinity(vi);
2828 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2830 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2833 virtnet_clean_affinity(vi);
2837 static enum cpuhp_state virtionet_online;
2839 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2843 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2846 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2850 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2854 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2856 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2857 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2861 static void virtnet_get_ringparam(struct net_device *dev,
2862 struct ethtool_ringparam *ring,
2863 struct kernel_ethtool_ringparam *kernel_ring,
2864 struct netlink_ext_ack *extack)
2866 struct virtnet_info *vi = netdev_priv(dev);
2868 ring->rx_max_pending = vi->rq[0].vq->num_max;
2869 ring->tx_max_pending = vi->sq[0].vq->num_max;
2870 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2871 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2874 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
2875 u16 vqn, u32 max_usecs, u32 max_packets);
2877 static int virtnet_set_ringparam(struct net_device *dev,
2878 struct ethtool_ringparam *ring,
2879 struct kernel_ethtool_ringparam *kernel_ring,
2880 struct netlink_ext_ack *extack)
2882 struct virtnet_info *vi = netdev_priv(dev);
2883 u32 rx_pending, tx_pending;
2884 struct receive_queue *rq;
2885 struct send_queue *sq;
2888 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2891 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2892 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2894 if (ring->rx_pending == rx_pending &&
2895 ring->tx_pending == tx_pending)
2898 if (ring->rx_pending > vi->rq[0].vq->num_max)
2901 if (ring->tx_pending > vi->sq[0].vq->num_max)
2904 for (i = 0; i < vi->max_queue_pairs; i++) {
2908 if (ring->tx_pending != tx_pending) {
2909 err = virtnet_tx_resize(vi, sq, ring->tx_pending);
2913 /* Upon disabling and re-enabling a transmit virtqueue, the device must
2914 * set the coalescing parameters of the virtqueue to those configured
2915 * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver
2916 * did not set any TX coalescing parameters, to 0.
2918 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(i),
2919 vi->intr_coal_tx.max_usecs,
2920 vi->intr_coal_tx.max_packets);
2924 vi->sq[i].intr_coal.max_usecs = vi->intr_coal_tx.max_usecs;
2925 vi->sq[i].intr_coal.max_packets = vi->intr_coal_tx.max_packets;
2928 if (ring->rx_pending != rx_pending) {
2929 err = virtnet_rx_resize(vi, rq, ring->rx_pending);
2933 /* The reason is same as the transmit virtqueue reset */
2934 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(i),
2935 vi->intr_coal_rx.max_usecs,
2936 vi->intr_coal_rx.max_packets);
2940 vi->rq[i].intr_coal.max_usecs = vi->intr_coal_rx.max_usecs;
2941 vi->rq[i].intr_coal.max_packets = vi->intr_coal_rx.max_packets;
2948 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
2950 struct net_device *dev = vi->dev;
2951 struct scatterlist sgs[4];
2952 unsigned int sg_buf_size;
2955 sg_init_table(sgs, 4);
2957 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
2958 sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
2960 sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
2961 sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
2963 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
2964 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
2965 sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
2967 sg_buf_size = vi->rss_key_size;
2968 sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
2970 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2971 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
2972 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
2973 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
2979 static void virtnet_init_default_rss(struct virtnet_info *vi)
2984 vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
2985 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
2986 vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
2987 ? vi->rss_indir_table_size - 1 : 0;
2988 vi->ctrl->rss.unclassified_queue = 0;
2990 for (; i < vi->rss_indir_table_size; ++i) {
2991 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
2992 vi->ctrl->rss.indirection_table[i] = indir_val;
2995 vi->ctrl->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0;
2996 vi->ctrl->rss.hash_key_length = vi->rss_key_size;
2998 netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
3001 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
3004 switch (info->flow_type) {
3006 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
3007 info->data = RXH_IP_SRC | RXH_IP_DST |
3008 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3009 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3010 info->data = RXH_IP_SRC | RXH_IP_DST;
3014 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
3015 info->data = RXH_IP_SRC | RXH_IP_DST |
3016 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3017 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3018 info->data = RXH_IP_SRC | RXH_IP_DST;
3022 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
3023 info->data = RXH_IP_SRC | RXH_IP_DST |
3024 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3025 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3026 info->data = RXH_IP_SRC | RXH_IP_DST;
3030 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
3031 info->data = RXH_IP_SRC | RXH_IP_DST |
3032 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3033 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3034 info->data = RXH_IP_SRC | RXH_IP_DST;
3038 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
3039 info->data = RXH_IP_SRC | RXH_IP_DST;
3043 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
3044 info->data = RXH_IP_SRC | RXH_IP_DST;
3053 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
3055 u32 new_hashtypes = vi->rss_hash_types_saved;
3056 bool is_disable = info->data & RXH_DISCARD;
3057 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
3059 /* supports only 'sd', 'sdfn' and 'r' */
3060 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
3063 switch (info->flow_type) {
3065 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
3067 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3068 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
3071 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
3073 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3074 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
3077 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3079 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3082 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
3084 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3085 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
3088 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
3090 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3091 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
3094 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3096 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3099 /* unsupported flow */
3103 /* if unsupported hashtype was set */
3104 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
3107 if (new_hashtypes != vi->rss_hash_types_saved) {
3108 vi->rss_hash_types_saved = new_hashtypes;
3109 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3110 if (vi->dev->features & NETIF_F_RXHASH)
3111 return virtnet_commit_rss_command(vi);
3117 static void virtnet_get_drvinfo(struct net_device *dev,
3118 struct ethtool_drvinfo *info)
3120 struct virtnet_info *vi = netdev_priv(dev);
3121 struct virtio_device *vdev = vi->vdev;
3123 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
3124 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
3125 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
3129 /* TODO: Eliminate OOO packets during switching */
3130 static int virtnet_set_channels(struct net_device *dev,
3131 struct ethtool_channels *channels)
3133 struct virtnet_info *vi = netdev_priv(dev);
3134 u16 queue_pairs = channels->combined_count;
3137 /* We don't support separate rx/tx channels.
3138 * We don't allow setting 'other' channels.
3140 if (channels->rx_count || channels->tx_count || channels->other_count)
3143 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
3146 /* For now we don't support modifying channels while XDP is loaded
3147 * also when XDP is loaded all RX queues have XDP programs so we only
3148 * need to check a single RX queue.
3150 if (vi->rq[0].xdp_prog)
3154 err = _virtnet_set_queues(vi, queue_pairs);
3159 virtnet_set_affinity(vi);
3162 netif_set_real_num_tx_queues(dev, queue_pairs);
3163 netif_set_real_num_rx_queues(dev, queue_pairs);
3168 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3170 struct virtnet_info *vi = netdev_priv(dev);
3174 switch (stringset) {
3176 for (i = 0; i < vi->curr_queue_pairs; i++) {
3177 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
3178 ethtool_sprintf(&p, "rx_queue_%u_%s", i,
3179 virtnet_rq_stats_desc[j].desc);
3182 for (i = 0; i < vi->curr_queue_pairs; i++) {
3183 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
3184 ethtool_sprintf(&p, "tx_queue_%u_%s", i,
3185 virtnet_sq_stats_desc[j].desc);
3191 static int virtnet_get_sset_count(struct net_device *dev, int sset)
3193 struct virtnet_info *vi = netdev_priv(dev);
3197 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
3198 VIRTNET_SQ_STATS_LEN);
3204 static void virtnet_get_ethtool_stats(struct net_device *dev,
3205 struct ethtool_stats *stats, u64 *data)
3207 struct virtnet_info *vi = netdev_priv(dev);
3208 unsigned int idx = 0, start, i, j;
3209 const u8 *stats_base;
3210 const u64_stats_t *p;
3213 for (i = 0; i < vi->curr_queue_pairs; i++) {
3214 struct receive_queue *rq = &vi->rq[i];
3216 stats_base = (const u8 *)&rq->stats;
3218 start = u64_stats_fetch_begin(&rq->stats.syncp);
3219 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
3220 offset = virtnet_rq_stats_desc[j].offset;
3221 p = (const u64_stats_t *)(stats_base + offset);
3222 data[idx + j] = u64_stats_read(p);
3224 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
3225 idx += VIRTNET_RQ_STATS_LEN;
3228 for (i = 0; i < vi->curr_queue_pairs; i++) {
3229 struct send_queue *sq = &vi->sq[i];
3231 stats_base = (const u8 *)&sq->stats;
3233 start = u64_stats_fetch_begin(&sq->stats.syncp);
3234 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
3235 offset = virtnet_sq_stats_desc[j].offset;
3236 p = (const u64_stats_t *)(stats_base + offset);
3237 data[idx + j] = u64_stats_read(p);
3239 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
3240 idx += VIRTNET_SQ_STATS_LEN;
3244 static void virtnet_get_channels(struct net_device *dev,
3245 struct ethtool_channels *channels)
3247 struct virtnet_info *vi = netdev_priv(dev);
3249 channels->combined_count = vi->curr_queue_pairs;
3250 channels->max_combined = vi->max_queue_pairs;
3251 channels->max_other = 0;
3252 channels->rx_count = 0;
3253 channels->tx_count = 0;
3254 channels->other_count = 0;
3257 static int virtnet_set_link_ksettings(struct net_device *dev,
3258 const struct ethtool_link_ksettings *cmd)
3260 struct virtnet_info *vi = netdev_priv(dev);
3262 return ethtool_virtdev_set_link_ksettings(dev, cmd,
3263 &vi->speed, &vi->duplex);
3266 static int virtnet_get_link_ksettings(struct net_device *dev,
3267 struct ethtool_link_ksettings *cmd)
3269 struct virtnet_info *vi = netdev_priv(dev);
3271 cmd->base.speed = vi->speed;
3272 cmd->base.duplex = vi->duplex;
3273 cmd->base.port = PORT_OTHER;
3278 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
3279 struct ethtool_coalesce *ec)
3281 struct scatterlist sgs_tx, sgs_rx;
3284 vi->ctrl->coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
3285 vi->ctrl->coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
3286 sg_init_one(&sgs_tx, &vi->ctrl->coal_tx, sizeof(vi->ctrl->coal_tx));
3288 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3289 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
3293 /* Save parameters */
3294 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs;
3295 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames;
3296 for (i = 0; i < vi->max_queue_pairs; i++) {
3297 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs;
3298 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames;
3301 vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
3302 vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
3303 sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx));
3305 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3306 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
3310 /* Save parameters */
3311 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs;
3312 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames;
3313 for (i = 0; i < vi->max_queue_pairs; i++) {
3314 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs;
3315 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames;
3321 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
3322 u16 vqn, u32 max_usecs, u32 max_packets)
3324 struct scatterlist sgs;
3326 vi->ctrl->coal_vq.vqn = cpu_to_le16(vqn);
3327 vi->ctrl->coal_vq.coal.max_usecs = cpu_to_le32(max_usecs);
3328 vi->ctrl->coal_vq.coal.max_packets = cpu_to_le32(max_packets);
3329 sg_init_one(&sgs, &vi->ctrl->coal_vq, sizeof(vi->ctrl->coal_vq));
3331 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3332 VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
3339 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
3340 struct ethtool_coalesce *ec,
3345 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
3346 ec->rx_coalesce_usecs,
3347 ec->rx_max_coalesced_frames);
3351 vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
3352 vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames;
3354 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
3355 ec->tx_coalesce_usecs,
3356 ec->tx_max_coalesced_frames);
3360 vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
3361 vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames;
3366 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
3368 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
3369 * feature is negotiated.
3371 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
3374 if (ec->tx_max_coalesced_frames > 1 ||
3375 ec->rx_max_coalesced_frames != 1)
3381 static int virtnet_should_update_vq_weight(int dev_flags, int weight,
3382 int vq_weight, bool *should_update)
3384 if (weight ^ vq_weight) {
3385 if (dev_flags & IFF_UP)
3387 *should_update = true;
3393 static int virtnet_set_coalesce(struct net_device *dev,
3394 struct ethtool_coalesce *ec,
3395 struct kernel_ethtool_coalesce *kernel_coal,
3396 struct netlink_ext_ack *extack)
3398 struct virtnet_info *vi = netdev_priv(dev);
3399 int ret, queue_number, napi_weight;
3400 bool update_napi = false;
3402 /* Can't change NAPI weight if the link is up */
3403 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
3404 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) {
3405 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
3406 vi->sq[queue_number].napi.weight,
3412 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be
3413 * updated for the sake of simplicity, which might not be necessary
3419 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
3420 ret = virtnet_send_notf_coal_cmds(vi, ec);
3422 ret = virtnet_coal_params_supported(ec);
3428 for (; queue_number < vi->max_queue_pairs; queue_number++)
3429 vi->sq[queue_number].napi.weight = napi_weight;
3435 static int virtnet_get_coalesce(struct net_device *dev,
3436 struct ethtool_coalesce *ec,
3437 struct kernel_ethtool_coalesce *kernel_coal,
3438 struct netlink_ext_ack *extack)
3440 struct virtnet_info *vi = netdev_priv(dev);
3442 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
3443 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
3444 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
3445 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
3446 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
3448 ec->rx_max_coalesced_frames = 1;
3450 if (vi->sq[0].napi.weight)
3451 ec->tx_max_coalesced_frames = 1;
3457 static int virtnet_set_per_queue_coalesce(struct net_device *dev,
3459 struct ethtool_coalesce *ec)
3461 struct virtnet_info *vi = netdev_priv(dev);
3462 int ret, napi_weight;
3463 bool update_napi = false;
3465 if (queue >= vi->max_queue_pairs)
3468 /* Can't change NAPI weight if the link is up */
3469 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
3470 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
3471 vi->sq[queue].napi.weight,
3476 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
3477 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
3479 ret = virtnet_coal_params_supported(ec);
3485 vi->sq[queue].napi.weight = napi_weight;
3490 static int virtnet_get_per_queue_coalesce(struct net_device *dev,
3492 struct ethtool_coalesce *ec)
3494 struct virtnet_info *vi = netdev_priv(dev);
3496 if (queue >= vi->max_queue_pairs)
3499 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
3500 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
3501 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
3502 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
3503 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
3505 ec->rx_max_coalesced_frames = 1;
3507 if (vi->sq[queue].napi.weight)
3508 ec->tx_max_coalesced_frames = 1;
3514 static void virtnet_init_settings(struct net_device *dev)
3516 struct virtnet_info *vi = netdev_priv(dev);
3518 vi->speed = SPEED_UNKNOWN;
3519 vi->duplex = DUPLEX_UNKNOWN;
3522 static void virtnet_update_settings(struct virtnet_info *vi)
3527 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
3530 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
3532 if (ethtool_validate_speed(speed))
3535 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
3537 if (ethtool_validate_duplex(duplex))
3538 vi->duplex = duplex;
3541 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
3543 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
3546 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
3548 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
3551 static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
3553 struct virtnet_info *vi = netdev_priv(dev);
3557 for (i = 0; i < vi->rss_indir_table_size; ++i)
3558 indir[i] = vi->ctrl->rss.indirection_table[i];
3562 memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
3565 *hfunc = ETH_RSS_HASH_TOP;
3570 static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
3572 struct virtnet_info *vi = netdev_priv(dev);
3575 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3579 for (i = 0; i < vi->rss_indir_table_size; ++i)
3580 vi->ctrl->rss.indirection_table[i] = indir[i];
3583 memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
3585 virtnet_commit_rss_command(vi);
3590 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
3592 struct virtnet_info *vi = netdev_priv(dev);
3595 switch (info->cmd) {
3596 case ETHTOOL_GRXRINGS:
3597 info->data = vi->curr_queue_pairs;
3600 virtnet_get_hashflow(vi, info);
3609 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
3611 struct virtnet_info *vi = netdev_priv(dev);
3614 switch (info->cmd) {
3616 if (!virtnet_set_hashflow(vi, info))
3627 static const struct ethtool_ops virtnet_ethtool_ops = {
3628 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
3629 ETHTOOL_COALESCE_USECS,
3630 .get_drvinfo = virtnet_get_drvinfo,
3631 .get_link = ethtool_op_get_link,
3632 .get_ringparam = virtnet_get_ringparam,
3633 .set_ringparam = virtnet_set_ringparam,
3634 .get_strings = virtnet_get_strings,
3635 .get_sset_count = virtnet_get_sset_count,
3636 .get_ethtool_stats = virtnet_get_ethtool_stats,
3637 .set_channels = virtnet_set_channels,
3638 .get_channels = virtnet_get_channels,
3639 .get_ts_info = ethtool_op_get_ts_info,
3640 .get_link_ksettings = virtnet_get_link_ksettings,
3641 .set_link_ksettings = virtnet_set_link_ksettings,
3642 .set_coalesce = virtnet_set_coalesce,
3643 .get_coalesce = virtnet_get_coalesce,
3644 .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
3645 .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
3646 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
3647 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
3648 .get_rxfh = virtnet_get_rxfh,
3649 .set_rxfh = virtnet_set_rxfh,
3650 .get_rxnfc = virtnet_get_rxnfc,
3651 .set_rxnfc = virtnet_set_rxnfc,
3654 static void virtnet_freeze_down(struct virtio_device *vdev)
3656 struct virtnet_info *vi = vdev->priv;
3658 /* Make sure no work handler is accessing the device */
3659 flush_work(&vi->config_work);
3661 netif_tx_lock_bh(vi->dev);
3662 netif_device_detach(vi->dev);
3663 netif_tx_unlock_bh(vi->dev);
3664 if (netif_running(vi->dev))
3665 virtnet_close(vi->dev);
3668 static int init_vqs(struct virtnet_info *vi);
3670 static int virtnet_restore_up(struct virtio_device *vdev)
3672 struct virtnet_info *vi = vdev->priv;
3679 virtio_device_ready(vdev);
3681 enable_delayed_refill(vi);
3683 if (netif_running(vi->dev)) {
3684 err = virtnet_open(vi->dev);
3689 netif_tx_lock_bh(vi->dev);
3690 netif_device_attach(vi->dev);
3691 netif_tx_unlock_bh(vi->dev);
3695 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
3697 struct scatterlist sg;
3698 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
3700 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
3702 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
3703 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
3704 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
3711 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
3715 if (!vi->guest_offloads)
3718 return virtnet_set_guest_offloads(vi, offloads);
3721 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
3723 u64 offloads = vi->guest_offloads;
3725 if (!vi->guest_offloads)
3728 return virtnet_set_guest_offloads(vi, offloads);
3731 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
3732 struct netlink_ext_ack *extack)
3734 unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
3735 sizeof(struct skb_shared_info));
3736 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
3737 struct virtnet_info *vi = netdev_priv(dev);
3738 struct bpf_prog *old_prog;
3739 u16 xdp_qp = 0, curr_qp;
3742 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
3743 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3744 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3745 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
3746 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
3747 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
3748 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
3749 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
3750 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
3754 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
3755 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
3759 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
3760 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
3761 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
3765 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
3767 xdp_qp = nr_cpu_ids;
3769 /* XDP requires extra queues for XDP_TX */
3770 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
3771 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",
3772 curr_qp + xdp_qp, vi->max_queue_pairs);
3776 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
3777 if (!prog && !old_prog)
3781 bpf_prog_add(prog, vi->max_queue_pairs - 1);
3783 /* Make sure NAPI is not using any XDP TX queues for RX. */
3784 if (netif_running(dev)) {
3785 for (i = 0; i < vi->max_queue_pairs; i++) {
3786 napi_disable(&vi->rq[i].napi);
3787 virtnet_napi_tx_disable(&vi->sq[i].napi);
3792 for (i = 0; i < vi->max_queue_pairs; i++) {
3793 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
3795 virtnet_restore_guest_offloads(vi);
3800 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
3803 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
3804 vi->xdp_queue_pairs = xdp_qp;
3807 vi->xdp_enabled = true;
3808 for (i = 0; i < vi->max_queue_pairs; i++) {
3809 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
3810 if (i == 0 && !old_prog)
3811 virtnet_clear_guest_offloads(vi);
3814 xdp_features_set_redirect_target(dev, true);
3816 xdp_features_clear_redirect_target(dev);
3817 vi->xdp_enabled = false;
3820 for (i = 0; i < vi->max_queue_pairs; i++) {
3822 bpf_prog_put(old_prog);
3823 if (netif_running(dev)) {
3824 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3825 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3834 virtnet_clear_guest_offloads(vi);
3835 for (i = 0; i < vi->max_queue_pairs; i++)
3836 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
3839 if (netif_running(dev)) {
3840 for (i = 0; i < vi->max_queue_pairs; i++) {
3841 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3842 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3847 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
3851 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3853 switch (xdp->command) {
3854 case XDP_SETUP_PROG:
3855 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
3861 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
3864 struct virtnet_info *vi = netdev_priv(dev);
3867 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3870 ret = snprintf(buf, len, "sby");
3877 static int virtnet_set_features(struct net_device *dev,
3878 netdev_features_t features)
3880 struct virtnet_info *vi = netdev_priv(dev);
3884 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
3885 if (vi->xdp_enabled)
3888 if (features & NETIF_F_GRO_HW)
3889 offloads = vi->guest_offloads_capable;
3891 offloads = vi->guest_offloads_capable &
3892 ~GUEST_OFFLOAD_GRO_HW_MASK;
3894 err = virtnet_set_guest_offloads(vi, offloads);
3897 vi->guest_offloads = offloads;
3900 if ((dev->features ^ features) & NETIF_F_RXHASH) {
3901 if (features & NETIF_F_RXHASH)
3902 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3904 vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
3906 if (!virtnet_commit_rss_command(vi))
3913 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
3915 struct virtnet_info *priv = netdev_priv(dev);
3916 struct send_queue *sq = &priv->sq[txqueue];
3917 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
3919 u64_stats_update_begin(&sq->stats.syncp);
3920 u64_stats_inc(&sq->stats.tx_timeouts);
3921 u64_stats_update_end(&sq->stats.syncp);
3923 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
3924 txqueue, sq->name, sq->vq->index, sq->vq->name,
3925 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
3928 static const struct net_device_ops virtnet_netdev = {
3929 .ndo_open = virtnet_open,
3930 .ndo_stop = virtnet_close,
3931 .ndo_start_xmit = start_xmit,
3932 .ndo_validate_addr = eth_validate_addr,
3933 .ndo_set_mac_address = virtnet_set_mac_address,
3934 .ndo_set_rx_mode = virtnet_set_rx_mode,
3935 .ndo_get_stats64 = virtnet_stats,
3936 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
3937 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
3938 .ndo_bpf = virtnet_xdp,
3939 .ndo_xdp_xmit = virtnet_xdp_xmit,
3940 .ndo_features_check = passthru_features_check,
3941 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
3942 .ndo_set_features = virtnet_set_features,
3943 .ndo_tx_timeout = virtnet_tx_timeout,
3946 static void virtnet_config_changed_work(struct work_struct *work)
3948 struct virtnet_info *vi =
3949 container_of(work, struct virtnet_info, config_work);
3952 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
3953 struct virtio_net_config, status, &v) < 0)
3956 if (v & VIRTIO_NET_S_ANNOUNCE) {
3957 netdev_notify_peers(vi->dev);
3958 virtnet_ack_link_announce(vi);
3961 /* Ignore unknown (future) status bits */
3962 v &= VIRTIO_NET_S_LINK_UP;
3964 if (vi->status == v)
3969 if (vi->status & VIRTIO_NET_S_LINK_UP) {
3970 virtnet_update_settings(vi);
3971 netif_carrier_on(vi->dev);
3972 netif_tx_wake_all_queues(vi->dev);
3974 netif_carrier_off(vi->dev);
3975 netif_tx_stop_all_queues(vi->dev);
3979 static void virtnet_config_changed(struct virtio_device *vdev)
3981 struct virtnet_info *vi = vdev->priv;
3983 schedule_work(&vi->config_work);
3986 static void virtnet_free_queues(struct virtnet_info *vi)
3990 for (i = 0; i < vi->max_queue_pairs; i++) {
3991 __netif_napi_del(&vi->rq[i].napi);
3992 __netif_napi_del(&vi->sq[i].napi);
3995 /* We called __netif_napi_del(),
3996 * we need to respect an RCU grace period before freeing vi->rq
4005 static void _free_receive_bufs(struct virtnet_info *vi)
4007 struct bpf_prog *old_prog;
4010 for (i = 0; i < vi->max_queue_pairs; i++) {
4011 while (vi->rq[i].pages)
4012 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
4014 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
4015 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
4017 bpf_prog_put(old_prog);
4021 static void free_receive_bufs(struct virtnet_info *vi)
4024 _free_receive_bufs(vi);
4028 static void free_receive_page_frags(struct virtnet_info *vi)
4031 for (i = 0; i < vi->max_queue_pairs; i++)
4032 if (vi->rq[i].alloc_frag.page) {
4033 if (vi->rq[i].do_dma && vi->rq[i].last_dma)
4034 virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0);
4035 put_page(vi->rq[i].alloc_frag.page);
4039 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
4041 if (!is_xdp_frame(buf))
4044 xdp_return_frame(ptr_to_xdp(buf));
4047 static void free_unused_bufs(struct virtnet_info *vi)
4052 for (i = 0; i < vi->max_queue_pairs; i++) {
4053 struct virtqueue *vq = vi->sq[i].vq;
4054 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
4055 virtnet_sq_free_unused_buf(vq, buf);
4059 for (i = 0; i < vi->max_queue_pairs; i++) {
4060 struct virtqueue *vq = vi->rq[i].vq;
4062 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
4063 virtnet_rq_unmap_free_buf(vq, buf);
4068 static void virtnet_del_vqs(struct virtnet_info *vi)
4070 struct virtio_device *vdev = vi->vdev;
4072 virtnet_clean_affinity(vi);
4074 vdev->config->del_vqs(vdev);
4076 virtnet_free_queues(vi);
4079 /* How large should a single buffer be so a queue full of these can fit at
4080 * least one full packet?
4081 * Logic below assumes the mergeable buffer header is used.
4083 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
4085 const unsigned int hdr_len = vi->hdr_len;
4086 unsigned int rq_size = virtqueue_get_vring_size(vq);
4087 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
4088 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
4089 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
4091 return max(max(min_buf_len, hdr_len) - hdr_len,
4092 (unsigned int)GOOD_PACKET_LEN);
4095 static int virtnet_find_vqs(struct virtnet_info *vi)
4097 vq_callback_t **callbacks;
4098 struct virtqueue **vqs;
4105 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
4106 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
4107 * possible control vq.
4109 total_vqs = vi->max_queue_pairs * 2 +
4110 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
4112 /* Allocate space for find_vqs parameters */
4113 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
4116 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
4119 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
4122 if (!vi->big_packets || vi->mergeable_rx_bufs) {
4123 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
4130 /* Parameters for control virtqueue, if any */
4132 callbacks[total_vqs - 1] = NULL;
4133 names[total_vqs - 1] = "control";
4136 /* Allocate/initialize parameters for send/receive virtqueues */
4137 for (i = 0; i < vi->max_queue_pairs; i++) {
4138 callbacks[rxq2vq(i)] = skb_recv_done;
4139 callbacks[txq2vq(i)] = skb_xmit_done;
4140 sprintf(vi->rq[i].name, "input.%u", i);
4141 sprintf(vi->sq[i].name, "output.%u", i);
4142 names[rxq2vq(i)] = vi->rq[i].name;
4143 names[txq2vq(i)] = vi->sq[i].name;
4145 ctx[rxq2vq(i)] = true;
4148 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
4154 vi->cvq = vqs[total_vqs - 1];
4155 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
4156 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4159 for (i = 0; i < vi->max_queue_pairs; i++) {
4160 vi->rq[i].vq = vqs[rxq2vq(i)];
4161 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
4162 vi->sq[i].vq = vqs[txq2vq(i)];
4165 /* run here: ret == 0. */
4180 static int virtnet_alloc_queues(struct virtnet_info *vi)
4185 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
4191 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
4194 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
4198 INIT_DELAYED_WORK(&vi->refill, refill_work);
4199 for (i = 0; i < vi->max_queue_pairs; i++) {
4200 vi->rq[i].pages = NULL;
4201 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
4203 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
4205 napi_tx ? napi_weight : 0);
4207 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
4208 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
4209 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
4211 u64_stats_init(&vi->rq[i].stats.syncp);
4212 u64_stats_init(&vi->sq[i].stats.syncp);
4225 static int init_vqs(struct virtnet_info *vi)
4229 /* Allocate send & receive queues */
4230 ret = virtnet_alloc_queues(vi);
4234 ret = virtnet_find_vqs(vi);
4238 virtnet_rq_set_premapped(vi);
4241 virtnet_set_affinity(vi);
4247 virtnet_free_queues(vi);
4253 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
4256 struct virtnet_info *vi = netdev_priv(queue->dev);
4257 unsigned int queue_index = get_netdev_rx_queue_index(queue);
4258 unsigned int headroom = virtnet_get_headroom(vi);
4259 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
4260 struct ewma_pkt_len *avg;
4262 BUG_ON(queue_index >= vi->max_queue_pairs);
4263 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
4264 return sprintf(buf, "%u\n",
4265 get_mergeable_buf_len(&vi->rq[queue_index], avg,
4266 SKB_DATA_ALIGN(headroom + tailroom)));
4269 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
4270 __ATTR_RO(mergeable_rx_buffer_size);
4272 static struct attribute *virtio_net_mrg_rx_attrs[] = {
4273 &mergeable_rx_buffer_size_attribute.attr,
4277 static const struct attribute_group virtio_net_mrg_rx_group = {
4278 .name = "virtio_net",
4279 .attrs = virtio_net_mrg_rx_attrs
4283 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
4285 const char *fname, const char *dname)
4287 if (!virtio_has_feature(vdev, fbit))
4290 dev_err(&vdev->dev, "device advertises feature %s but not %s",
4296 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
4297 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
4299 static bool virtnet_validate_features(struct virtio_device *vdev)
4301 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
4302 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
4303 "VIRTIO_NET_F_CTRL_VQ") ||
4304 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
4305 "VIRTIO_NET_F_CTRL_VQ") ||
4306 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
4307 "VIRTIO_NET_F_CTRL_VQ") ||
4308 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
4309 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
4310 "VIRTIO_NET_F_CTRL_VQ") ||
4311 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
4312 "VIRTIO_NET_F_CTRL_VQ") ||
4313 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
4314 "VIRTIO_NET_F_CTRL_VQ") ||
4315 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
4316 "VIRTIO_NET_F_CTRL_VQ") ||
4317 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL,
4318 "VIRTIO_NET_F_CTRL_VQ"))) {
4325 #define MIN_MTU ETH_MIN_MTU
4326 #define MAX_MTU ETH_MAX_MTU
4328 static int virtnet_validate(struct virtio_device *vdev)
4330 if (!vdev->config->get) {
4331 dev_err(&vdev->dev, "%s failure: config access disabled\n",
4336 if (!virtnet_validate_features(vdev))
4339 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
4340 int mtu = virtio_cread16(vdev,
4341 offsetof(struct virtio_net_config,
4344 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
4347 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
4348 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
4349 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
4350 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
4356 static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
4358 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
4359 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
4360 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
4361 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
4362 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
4363 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
4366 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
4368 bool guest_gso = virtnet_check_guest_gso(vi);
4370 /* If device can receive ANY guest GSO packets, regardless of mtu,
4371 * allocate packets of maximum size, otherwise limit it to only
4372 * mtu size worth only.
4374 if (mtu > ETH_DATA_LEN || guest_gso) {
4375 vi->big_packets = true;
4376 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
4380 static int virtnet_probe(struct virtio_device *vdev)
4382 int i, err = -ENOMEM;
4383 struct net_device *dev;
4384 struct virtnet_info *vi;
4385 u16 max_queue_pairs;
4388 /* Find if host supports multiqueue/rss virtio_net device */
4389 max_queue_pairs = 1;
4390 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
4392 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
4394 /* We need at least 2 queue's */
4395 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
4396 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
4397 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
4398 max_queue_pairs = 1;
4400 /* Allocate ourselves a network device with room for our info */
4401 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
4405 /* Set up network device as normal. */
4406 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
4407 IFF_TX_SKB_NO_LINEAR;
4408 dev->netdev_ops = &virtnet_netdev;
4409 dev->features = NETIF_F_HIGHDMA;
4411 dev->ethtool_ops = &virtnet_ethtool_ops;
4412 SET_NETDEV_DEV(dev, &vdev->dev);
4414 /* Do we support "hardware" checksums? */
4415 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
4416 /* This opens up the world of extra features. */
4417 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
4419 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
4421 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
4422 dev->hw_features |= NETIF_F_TSO
4423 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
4425 /* Individual feature bits: what can host handle? */
4426 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
4427 dev->hw_features |= NETIF_F_TSO;
4428 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
4429 dev->hw_features |= NETIF_F_TSO6;
4430 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
4431 dev->hw_features |= NETIF_F_TSO_ECN;
4432 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
4433 dev->hw_features |= NETIF_F_GSO_UDP_L4;
4435 dev->features |= NETIF_F_GSO_ROBUST;
4438 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
4439 /* (!csum && gso) case will be fixed by register_netdev() */
4441 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
4442 dev->features |= NETIF_F_RXCSUM;
4443 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
4444 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
4445 dev->features |= NETIF_F_GRO_HW;
4446 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
4447 dev->hw_features |= NETIF_F_GRO_HW;
4449 dev->vlan_features = dev->features;
4450 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
4452 /* MTU range: 68 - 65535 */
4453 dev->min_mtu = MIN_MTU;
4454 dev->max_mtu = MAX_MTU;
4456 /* Configuration may specify what MAC to use. Otherwise random. */
4457 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
4460 virtio_cread_bytes(vdev,
4461 offsetof(struct virtio_net_config, mac),
4463 eth_hw_addr_set(dev, addr);
4465 eth_hw_addr_random(dev);
4466 dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
4470 /* Set up our device-specific information */
4471 vi = netdev_priv(dev);
4476 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
4477 spin_lock_init(&vi->refill_lock);
4479 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
4480 vi->mergeable_rx_bufs = true;
4481 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
4484 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
4485 vi->intr_coal_rx.max_usecs = 0;
4486 vi->intr_coal_tx.max_usecs = 0;
4487 vi->intr_coal_tx.max_packets = 0;
4488 vi->intr_coal_rx.max_packets = 0;
4491 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
4492 vi->has_rss_hash_report = true;
4494 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
4497 if (vi->has_rss || vi->has_rss_hash_report) {
4498 vi->rss_indir_table_size =
4499 virtio_cread16(vdev, offsetof(struct virtio_net_config,
4500 rss_max_indirection_table_length));
4502 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
4504 vi->rss_hash_types_supported =
4505 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
4506 vi->rss_hash_types_supported &=
4507 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
4508 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
4509 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
4511 dev->hw_features |= NETIF_F_RXHASH;
4514 if (vi->has_rss_hash_report)
4515 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
4516 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
4517 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
4518 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
4520 vi->hdr_len = sizeof(struct virtio_net_hdr);
4522 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
4523 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
4524 vi->any_header_sg = true;
4526 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
4529 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
4530 mtu = virtio_cread16(vdev,
4531 offsetof(struct virtio_net_config,
4533 if (mtu < dev->min_mtu) {
4534 /* Should never trigger: MTU was previously validated
4535 * in virtnet_validate.
4538 "device MTU appears to have changed it is now %d < %d",
4548 virtnet_set_big_packets(vi, mtu);
4550 if (vi->any_header_sg)
4551 dev->needed_headroom = vi->hdr_len;
4553 /* Enable multiqueue by default */
4554 if (num_online_cpus() >= max_queue_pairs)
4555 vi->curr_queue_pairs = max_queue_pairs;
4557 vi->curr_queue_pairs = num_online_cpus();
4558 vi->max_queue_pairs = max_queue_pairs;
4560 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
4566 if (vi->mergeable_rx_bufs)
4567 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
4569 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
4570 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
4572 virtnet_init_settings(dev);
4574 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
4575 vi->failover = net_failover_create(vi->dev);
4576 if (IS_ERR(vi->failover)) {
4577 err = PTR_ERR(vi->failover);
4582 if (vi->has_rss || vi->has_rss_hash_report)
4583 virtnet_init_default_rss(vi);
4585 /* serialize netdev register + virtio_device_ready() with ndo_open() */
4588 err = register_netdevice(dev);
4590 pr_debug("virtio_net: registering device failed\n");
4595 virtio_device_ready(vdev);
4597 _virtnet_set_queues(vi, vi->curr_queue_pairs);
4599 /* a random MAC address has been assigned, notify the device.
4600 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
4601 * because many devices work fine without getting MAC explicitly
4603 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
4604 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
4605 struct scatterlist sg;
4607 sg_init_one(&sg, dev->dev_addr, dev->addr_len);
4608 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
4609 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
4610 pr_debug("virtio_net: setting MAC address failed\n");
4613 goto free_unregister_netdev;
4619 err = virtnet_cpu_notif_add(vi);
4621 pr_debug("virtio_net: registering cpu notifier failed\n");
4622 goto free_unregister_netdev;
4625 /* Assume link up if device can't report link status,
4626 otherwise get link status from config. */
4627 netif_carrier_off(dev);
4628 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
4629 schedule_work(&vi->config_work);
4631 vi->status = VIRTIO_NET_S_LINK_UP;
4632 virtnet_update_settings(vi);
4633 netif_carrier_on(dev);
4636 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
4637 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
4638 set_bit(guest_offloads[i], &vi->guest_offloads);
4639 vi->guest_offloads_capable = vi->guest_offloads;
4641 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
4642 dev->name, max_queue_pairs);
4646 free_unregister_netdev:
4647 unregister_netdev(dev);
4649 net_failover_destroy(vi->failover);
4651 virtio_reset_device(vdev);
4652 cancel_delayed_work_sync(&vi->refill);
4653 free_receive_page_frags(vi);
4654 virtnet_del_vqs(vi);
4660 static void remove_vq_common(struct virtnet_info *vi)
4662 virtio_reset_device(vi->vdev);
4664 /* Free unused buffers in both send and recv, if any. */
4665 free_unused_bufs(vi);
4667 free_receive_bufs(vi);
4669 free_receive_page_frags(vi);
4671 virtnet_del_vqs(vi);
4674 static void virtnet_remove(struct virtio_device *vdev)
4676 struct virtnet_info *vi = vdev->priv;
4678 virtnet_cpu_notif_remove(vi);
4680 /* Make sure no work handler is accessing the device. */
4681 flush_work(&vi->config_work);
4683 unregister_netdev(vi->dev);
4685 net_failover_destroy(vi->failover);
4687 remove_vq_common(vi);
4689 free_netdev(vi->dev);
4692 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
4694 struct virtnet_info *vi = vdev->priv;
4696 virtnet_cpu_notif_remove(vi);
4697 virtnet_freeze_down(vdev);
4698 remove_vq_common(vi);
4703 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
4705 struct virtnet_info *vi = vdev->priv;
4708 err = virtnet_restore_up(vdev);
4711 virtnet_set_queues(vi, vi->curr_queue_pairs);
4713 err = virtnet_cpu_notif_add(vi);
4715 virtnet_freeze_down(vdev);
4716 remove_vq_common(vi);
4723 static struct virtio_device_id id_table[] = {
4724 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
4728 #define VIRTNET_FEATURES \
4729 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
4731 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
4732 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
4733 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
4734 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
4735 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
4736 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
4737 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
4738 VIRTIO_NET_F_CTRL_MAC_ADDR, \
4739 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
4740 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
4741 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
4742 VIRTIO_NET_F_VQ_NOTF_COAL, \
4743 VIRTIO_NET_F_GUEST_HDRLEN
4745 static unsigned int features[] = {
4749 static unsigned int features_legacy[] = {
4752 VIRTIO_F_ANY_LAYOUT,
4755 static struct virtio_driver virtio_net_driver = {
4756 .feature_table = features,
4757 .feature_table_size = ARRAY_SIZE(features),
4758 .feature_table_legacy = features_legacy,
4759 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
4760 .driver.name = KBUILD_MODNAME,
4761 .driver.owner = THIS_MODULE,
4762 .id_table = id_table,
4763 .validate = virtnet_validate,
4764 .probe = virtnet_probe,
4765 .remove = virtnet_remove,
4766 .config_changed = virtnet_config_changed,
4767 #ifdef CONFIG_PM_SLEEP
4768 .freeze = virtnet_freeze,
4769 .restore = virtnet_restore,
4773 static __init int virtio_net_driver_init(void)
4777 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
4779 virtnet_cpu_down_prep);
4782 virtionet_online = ret;
4783 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
4784 NULL, virtnet_cpu_dead);
4787 ret = register_virtio_driver(&virtio_net_driver);
4792 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
4794 cpuhp_remove_multi_state(virtionet_online);
4798 module_init(virtio_net_driver_init);
4800 static __exit void virtio_net_driver_exit(void)
4802 unregister_virtio_driver(&virtio_net_driver);
4803 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
4804 cpuhp_remove_multi_state(virtionet_online);
4806 module_exit(virtio_net_driver_exit);
4808 MODULE_DEVICE_TABLE(virtio, id_table);
4809 MODULE_DESCRIPTION("Virtio network driver");
4810 MODULE_LICENSE("GPL");