1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* A network driver using virtio.
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/ethtool.h>
10 #include <linux/module.h>
11 #include <linux/virtio.h>
12 #include <linux/virtio_net.h>
13 #include <linux/bpf.h>
14 #include <linux/bpf_trace.h>
15 #include <linux/scatterlist.h>
16 #include <linux/if_vlan.h>
17 #include <linux/slab.h>
18 #include <linux/cpu.h>
19 #include <linux/average.h>
20 #include <linux/filter.h>
21 #include <linux/kernel.h>
22 #include <net/route.h>
24 #include <net/net_failover.h>
26 static int napi_weight = NAPI_POLL_WEIGHT;
27 module_param(napi_weight, int, 0444);
29 static bool csum = true, gso = true, napi_tx = true;
30 module_param(csum, bool, 0444);
31 module_param(gso, bool, 0444);
32 module_param(napi_tx, bool, 0644);
34 /* FIXME: MTU in config. */
35 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
36 #define GOOD_COPY_LEN 128
38 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
40 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
41 #define VIRTIO_XDP_HEADROOM 256
43 /* Separating two types of XDP xmit */
44 #define VIRTIO_XDP_TX BIT(0)
45 #define VIRTIO_XDP_REDIR BIT(1)
47 #define VIRTIO_XDP_FLAG BIT(0)
49 /* RX packet size EWMA. The average packet size is used to determine the packet
50 * buffer size when refilling RX rings. As the entire RX ring may be refilled
51 * at once, the weight is chosen so that the EWMA will be insensitive to short-
52 * term, transient changes in packet size.
54 DECLARE_EWMA(pkt_len, 0, 64)
56 #define VIRTNET_DRIVER_VERSION "1.0.0"
58 static const unsigned long guest_offloads[] = {
59 VIRTIO_NET_F_GUEST_TSO4,
60 VIRTIO_NET_F_GUEST_TSO6,
61 VIRTIO_NET_F_GUEST_ECN,
62 VIRTIO_NET_F_GUEST_UFO,
63 VIRTIO_NET_F_GUEST_CSUM,
64 VIRTIO_NET_F_GUEST_USO4,
65 VIRTIO_NET_F_GUEST_USO6
68 #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
69 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
70 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
71 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
72 (1ULL << VIRTIO_NET_F_GUEST_USO4) | \
73 (1ULL << VIRTIO_NET_F_GUEST_USO6))
75 struct virtnet_stat_desc {
76 char desc[ETH_GSTRING_LEN];
80 struct virtnet_sq_stats {
81 struct u64_stats_sync syncp;
90 struct virtnet_rq_stats {
91 struct u64_stats_sync syncp;
102 #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
103 #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
105 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
106 { "packets", VIRTNET_SQ_STAT(packets) },
107 { "bytes", VIRTNET_SQ_STAT(bytes) },
108 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
109 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
110 { "kicks", VIRTNET_SQ_STAT(kicks) },
111 { "tx_timeouts", VIRTNET_SQ_STAT(tx_timeouts) },
114 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
115 { "packets", VIRTNET_RQ_STAT(packets) },
116 { "bytes", VIRTNET_RQ_STAT(bytes) },
117 { "drops", VIRTNET_RQ_STAT(drops) },
118 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
119 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
120 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
121 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
122 { "kicks", VIRTNET_RQ_STAT(kicks) },
125 #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
126 #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
128 /* Internal representation of a send virtqueue */
130 /* Virtqueue associated with this send _queue */
131 struct virtqueue *vq;
133 /* TX: fragments + linear part + virtio header */
134 struct scatterlist sg[MAX_SKB_FRAGS + 2];
136 /* Name of the send queue: output.$index */
139 struct virtnet_sq_stats stats;
141 struct napi_struct napi;
143 /* Record whether sq is in reset state. */
147 /* Internal representation of a receive virtqueue */
148 struct receive_queue {
149 /* Virtqueue associated with this receive_queue */
150 struct virtqueue *vq;
152 struct napi_struct napi;
154 struct bpf_prog __rcu *xdp_prog;
156 struct virtnet_rq_stats stats;
158 /* Chain pages by the private ptr. */
161 /* Average packet length for mergeable receive buffers. */
162 struct ewma_pkt_len mrg_avg_pkt_len;
164 /* Page frag for packet buffer allocation. */
165 struct page_frag alloc_frag;
167 /* RX: fragments + linear part + virtio header */
168 struct scatterlist sg[MAX_SKB_FRAGS + 2];
170 /* Min single buffer size for mergeable buffers case. */
171 unsigned int min_buf_len;
173 /* Name of this receive queue: input.$index */
176 struct xdp_rxq_info xdp_rxq;
179 /* This structure can contain rss message with maximum settings for indirection table and keysize
180 * Note, that default structure that describes RSS configuration virtio_net_rss_config
181 * contains same info but can't handle table values.
182 * In any case, structure would be passed to virtio hw through sg_buf split by parts
183 * because table sizes may be differ according to the device configuration.
185 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
186 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
187 struct virtio_net_ctrl_rss {
189 u16 indirection_table_mask;
190 u16 unclassified_queue;
191 u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
194 u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
197 /* Control VQ buffers: protected by the rtnl lock */
199 struct virtio_net_ctrl_hdr hdr;
200 virtio_net_ctrl_ack status;
201 struct virtio_net_ctrl_mq mq;
206 struct virtio_net_ctrl_rss rss;
209 struct virtnet_info {
210 struct virtio_device *vdev;
211 struct virtqueue *cvq;
212 struct net_device *dev;
213 struct send_queue *sq;
214 struct receive_queue *rq;
217 /* Max # of queue pairs supported by the device */
220 /* # of queue pairs currently used by the driver */
221 u16 curr_queue_pairs;
223 /* # of XDP queue pairs currently used by the driver */
226 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
229 /* I like... big packets and I cannot lie! */
232 /* number of sg entries allocated for big packets */
233 unsigned int big_packets_num_skbfrags;
235 /* Host will merge rx buffers for big packets (shake it! shake it!) */
236 bool mergeable_rx_bufs;
238 /* Host supports rss and/or hash report */
240 bool has_rss_hash_report;
242 u16 rss_indir_table_size;
243 u32 rss_hash_types_supported;
244 u32 rss_hash_types_saved;
246 /* Has control virtqueue */
249 /* Host can handle any s/g split between our header and packet data */
252 /* Packet virtio header size */
255 /* Work struct for delayed refilling if we run low on memory. */
256 struct delayed_work refill;
258 /* Is delayed refill enabled? */
261 /* The lock to synchronize the access to refill_enabled */
262 spinlock_t refill_lock;
264 /* Work struct for config space updates */
265 struct work_struct config_work;
267 /* Does the affinity hint is set for virtqueues? */
268 bool affinity_hint_set;
270 /* CPU hotplug instances for online & dead */
271 struct hlist_node node;
272 struct hlist_node node_dead;
274 struct control_buf *ctrl;
276 /* Ethtool settings */
280 /* Interrupt coalescing settings */
286 unsigned long guest_offloads;
287 unsigned long guest_offloads_capable;
289 /* failover when STANDBY feature enabled */
290 struct failover *failover;
293 struct padded_vnet_hdr {
294 struct virtio_net_hdr_v1_hash hdr;
296 * hdr is in a separate sg buffer, and data sg buffer shares same page
297 * with this header sg. This padding makes next sg 16 byte aligned
303 static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf);
304 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
306 static bool is_xdp_frame(void *ptr)
308 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
311 static void *xdp_to_ptr(struct xdp_frame *ptr)
313 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
316 static struct xdp_frame *ptr_to_xdp(void *ptr)
318 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
321 /* Converting between virtqueue no. and kernel tx/rx queue no.
322 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
324 static int vq2txq(struct virtqueue *vq)
326 return (vq->index - 1) / 2;
329 static int txq2vq(int txq)
334 static int vq2rxq(struct virtqueue *vq)
336 return vq->index / 2;
339 static int rxq2vq(int rxq)
344 static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
346 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
350 * private is used to chain pages for big packets, put the whole
351 * most recent used list in the beginning for reuse
353 static void give_pages(struct receive_queue *rq, struct page *page)
357 /* Find end of list, sew whole thing into vi->rq.pages. */
358 for (end = page; end->private; end = (struct page *)end->private);
359 end->private = (unsigned long)rq->pages;
363 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
365 struct page *p = rq->pages;
368 rq->pages = (struct page *)p->private;
369 /* clear private here, it is used to chain pages */
372 p = alloc_page(gfp_mask);
376 static void enable_delayed_refill(struct virtnet_info *vi)
378 spin_lock_bh(&vi->refill_lock);
379 vi->refill_enabled = true;
380 spin_unlock_bh(&vi->refill_lock);
383 static void disable_delayed_refill(struct virtnet_info *vi)
385 spin_lock_bh(&vi->refill_lock);
386 vi->refill_enabled = false;
387 spin_unlock_bh(&vi->refill_lock);
390 static void virtqueue_napi_schedule(struct napi_struct *napi,
391 struct virtqueue *vq)
393 if (napi_schedule_prep(napi)) {
394 virtqueue_disable_cb(vq);
395 __napi_schedule(napi);
399 static void virtqueue_napi_complete(struct napi_struct *napi,
400 struct virtqueue *vq, int processed)
404 opaque = virtqueue_enable_cb_prepare(vq);
405 if (napi_complete_done(napi, processed)) {
406 if (unlikely(virtqueue_poll(vq, opaque)))
407 virtqueue_napi_schedule(napi, vq);
409 virtqueue_disable_cb(vq);
413 static void skb_xmit_done(struct virtqueue *vq)
415 struct virtnet_info *vi = vq->vdev->priv;
416 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
418 /* Suppress further interrupts. */
419 virtqueue_disable_cb(vq);
422 virtqueue_napi_schedule(napi, vq);
424 /* We were probably waiting for more output buffers. */
425 netif_wake_subqueue(vi->dev, vq2txq(vq));
428 #define MRG_CTX_HEADER_SHIFT 22
429 static void *mergeable_len_to_ctx(unsigned int truesize,
430 unsigned int headroom)
432 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
435 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
437 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
440 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
442 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
445 /* Called from bottom half context */
446 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
447 struct receive_queue *rq,
448 struct page *page, unsigned int offset,
449 unsigned int len, unsigned int truesize)
452 struct virtio_net_hdr_mrg_rxbuf *hdr;
453 unsigned int copy, hdr_len, hdr_padded_len;
454 struct page *page_to_free = NULL;
455 int tailroom, shinfo_size;
456 char *p, *hdr_p, *buf;
458 p = page_address(page) + offset;
461 hdr_len = vi->hdr_len;
462 if (vi->mergeable_rx_bufs)
463 hdr_padded_len = hdr_len;
465 hdr_padded_len = sizeof(struct padded_vnet_hdr);
469 offset += hdr_padded_len;
471 tailroom = truesize - hdr_padded_len - len;
473 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
475 /* copy small packet so we can reuse these pages */
476 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
477 skb = build_skb(buf, truesize);
481 skb_reserve(skb, p - buf);
484 page = (struct page *)page->private;
486 give_pages(rq, page);
490 /* copy small packet so we can reuse these pages for small data */
491 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
495 /* Copy all frame if it fits skb->head, otherwise
496 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
498 if (len <= skb_tailroom(skb))
502 skb_put_data(skb, p, copy);
507 if (vi->mergeable_rx_bufs) {
509 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
516 * Verify that we can indeed put this data into a skb.
517 * This is here to handle cases when the device erroneously
518 * tries to receive more than is possible. This is usually
519 * the case of a broken device.
521 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
522 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
526 BUG_ON(offset >= PAGE_SIZE);
528 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
529 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
530 frag_size, truesize);
532 page = (struct page *)page->private;
537 give_pages(rq, page);
540 hdr = skb_vnet_hdr(skb);
541 memcpy(hdr, hdr_p, hdr_len);
543 put_page(page_to_free);
548 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
549 struct send_queue *sq,
550 struct xdp_frame *xdpf)
552 struct virtio_net_hdr_mrg_rxbuf *hdr;
553 struct skb_shared_info *shinfo;
557 if (unlikely(xdpf->headroom < vi->hdr_len))
560 if (unlikely(xdp_frame_has_frags(xdpf))) {
561 shinfo = xdp_get_shared_info_from_frame(xdpf);
562 nr_frags = shinfo->nr_frags;
565 /* In wrapping function virtnet_xdp_xmit(), we need to free
566 * up the pending old buffers, where we need to calculate the
567 * position of skb_shared_info in xdp_get_frame_len() and
568 * xdp_return_frame(), which will involve to xdpf->data and
569 * xdpf->headroom. Therefore, we need to update the value of
570 * headroom synchronously here.
572 xdpf->headroom -= vi->hdr_len;
573 xdpf->data -= vi->hdr_len;
574 /* Zero header and leave csum up to XDP layers */
576 memset(hdr, 0, vi->hdr_len);
577 xdpf->len += vi->hdr_len;
579 sg_init_table(sq->sg, nr_frags + 1);
580 sg_set_buf(sq->sg, xdpf->data, xdpf->len);
581 for (i = 0; i < nr_frags; i++) {
582 skb_frag_t *frag = &shinfo->frags[i];
584 sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
585 skb_frag_size(frag), skb_frag_off(frag));
588 err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
589 xdp_to_ptr(xdpf), GFP_ATOMIC);
591 return -ENOSPC; /* Caller handle free/refcnt */
596 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
597 * the current cpu, so it does not need to be locked.
599 * Here we use marco instead of inline functions because we have to deal with
600 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
601 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
602 * functions to perfectly solve these three problems at the same time.
604 #define virtnet_xdp_get_sq(vi) ({ \
605 int cpu = smp_processor_id(); \
606 struct netdev_queue *txq; \
607 typeof(vi) v = (vi); \
610 if (v->curr_queue_pairs > nr_cpu_ids) { \
611 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
613 txq = netdev_get_tx_queue(v->dev, qp); \
614 __netif_tx_acquire(txq); \
616 qp = cpu % v->curr_queue_pairs; \
617 txq = netdev_get_tx_queue(v->dev, qp); \
618 __netif_tx_lock(txq, cpu); \
623 #define virtnet_xdp_put_sq(vi, q) { \
624 struct netdev_queue *txq; \
625 typeof(vi) v = (vi); \
627 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
628 if (v->curr_queue_pairs > nr_cpu_ids) \
629 __netif_tx_release(txq); \
631 __netif_tx_unlock(txq); \
634 static int virtnet_xdp_xmit(struct net_device *dev,
635 int n, struct xdp_frame **frames, u32 flags)
637 struct virtnet_info *vi = netdev_priv(dev);
638 struct receive_queue *rq = vi->rq;
639 struct bpf_prog *xdp_prog;
640 struct send_queue *sq;
650 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
651 * indicate XDP resources have been successfully allocated.
653 xdp_prog = rcu_access_pointer(rq->xdp_prog);
657 sq = virtnet_xdp_get_sq(vi);
659 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
664 /* Free up any pending old buffers before queueing new ones. */
665 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
666 if (likely(is_xdp_frame(ptr))) {
667 struct xdp_frame *frame = ptr_to_xdp(ptr);
669 bytes += xdp_get_frame_len(frame);
670 xdp_return_frame(frame);
672 struct sk_buff *skb = ptr;
675 napi_consume_skb(skb, false);
680 for (i = 0; i < n; i++) {
681 struct xdp_frame *xdpf = frames[i];
683 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
689 if (flags & XDP_XMIT_FLUSH) {
690 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
694 u64_stats_update_begin(&sq->stats.syncp);
695 sq->stats.bytes += bytes;
696 sq->stats.packets += packets;
697 sq->stats.xdp_tx += n;
698 sq->stats.xdp_tx_drops += n - nxmit;
699 sq->stats.kicks += kicks;
700 u64_stats_update_end(&sq->stats.syncp);
702 virtnet_xdp_put_sq(vi, sq);
706 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
708 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
711 /* We copy the packet for XDP in the following cases:
713 * 1) Packet is scattered across multiple rx buffers.
714 * 2) Headroom space is insufficient.
716 * This is inefficient but it's a temporary condition that
717 * we hit right after XDP is enabled and until queue is refilled
718 * with large buffers with sufficient headroom - so it should affect
719 * at most queue size packets.
720 * Afterwards, the conditions to enable
721 * XDP should preclude the underlying device from sending packets
722 * across multiple buffers (num_buf > 1), and we make sure buffers
723 * have enough headroom.
725 static struct page *xdp_linearize_page(struct receive_queue *rq,
732 struct page *page = alloc_page(GFP_ATOMIC);
737 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
741 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
746 buf = virtqueue_get_buf(rq->vq, &buflen);
750 p = virt_to_head_page(buf);
751 off = buf - page_address(p);
753 /* guard against a misconfigured or uncooperative backend that
754 * is sending packet larger than the MTU.
756 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
761 memcpy(page_address(page) + page_off,
762 page_address(p) + off, buflen);
767 /* Headroom does not contribute to packet length */
768 *len = page_off - VIRTIO_XDP_HEADROOM;
771 __free_pages(page, 0);
775 static struct sk_buff *receive_small(struct net_device *dev,
776 struct virtnet_info *vi,
777 struct receive_queue *rq,
778 void *buf, void *ctx,
780 unsigned int *xdp_xmit,
781 struct virtnet_rq_stats *stats)
784 struct bpf_prog *xdp_prog;
785 unsigned int xdp_headroom = (unsigned long)ctx;
786 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
787 unsigned int headroom = vi->hdr_len + header_offset;
788 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
789 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
790 struct page *page = virt_to_head_page(buf);
791 unsigned int delta = 0;
792 struct page *xdp_page;
794 unsigned int metasize = 0;
799 if (unlikely(len > GOOD_PACKET_LEN)) {
800 pr_debug("%s: rx error: len %u exceeds max size %d\n",
801 dev->name, len, GOOD_PACKET_LEN);
802 dev->stats.rx_length_errors++;
806 if (likely(!vi->xdp_enabled)) {
812 xdp_prog = rcu_dereference(rq->xdp_prog);
814 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
815 struct xdp_frame *xdpf;
820 if (unlikely(hdr->hdr.gso_type))
823 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
824 int offset = buf - page_address(page) + header_offset;
825 unsigned int tlen = len + vi->hdr_len;
828 xdp_headroom = virtnet_get_headroom(vi);
829 header_offset = VIRTNET_RX_PAD + xdp_headroom;
830 headroom = vi->hdr_len + header_offset;
831 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
832 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
833 xdp_page = xdp_linearize_page(rq, &num_buf, page,
834 offset, header_offset,
839 buf = page_address(xdp_page);
844 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
845 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
846 xdp_headroom, len, true);
847 orig_data = xdp.data;
848 act = bpf_prog_run_xdp(xdp_prog, &xdp);
849 stats->xdp_packets++;
853 /* Recalculate length in case bpf program changed it */
854 delta = orig_data - xdp.data;
855 len = xdp.data_end - xdp.data;
856 metasize = xdp.data - xdp.data_meta;
860 xdpf = xdp_convert_buff_to_frame(&xdp);
863 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
864 if (unlikely(!err)) {
865 xdp_return_frame_rx_napi(xdpf);
866 } else if (unlikely(err < 0)) {
867 trace_xdp_exception(vi->dev, xdp_prog, act);
870 *xdp_xmit |= VIRTIO_XDP_TX;
874 stats->xdp_redirects++;
875 err = xdp_do_redirect(dev, &xdp, xdp_prog);
878 *xdp_xmit |= VIRTIO_XDP_REDIR;
882 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
885 trace_xdp_exception(vi->dev, xdp_prog, act);
894 skb = build_skb(buf, buflen);
897 skb_reserve(skb, headroom - delta);
900 buf += header_offset;
901 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
902 } /* keep zeroed vnet hdr since XDP is loaded */
905 skb_metadata_set(skb, metasize);
919 static struct sk_buff *receive_big(struct net_device *dev,
920 struct virtnet_info *vi,
921 struct receive_queue *rq,
924 struct virtnet_rq_stats *stats)
926 struct page *page = buf;
927 struct sk_buff *skb =
928 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
930 stats->bytes += len - vi->hdr_len;
938 give_pages(rq, page);
942 /* Why not use xdp_build_skb_from_frame() ?
943 * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
944 * virtio-net there are 2 points that do not match its requirements:
945 * 1. The size of the prefilled buffer is not fixed before xdp is set.
946 * 2. xdp_build_skb_from_frame() does more checks that we don't need,
947 * like eth_type_trans() (which virtio-net does in receive_buf()).
949 static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
950 struct virtnet_info *vi,
951 struct xdp_buff *xdp,
952 unsigned int xdp_frags_truesz)
954 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
955 unsigned int headroom, data_len;
960 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
961 pr_debug("Error building skb as missing reserved tailroom for xdp");
965 if (unlikely(xdp_buff_has_frags(xdp)))
966 nr_frags = sinfo->nr_frags;
968 skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
972 headroom = xdp->data - xdp->data_hard_start;
973 data_len = xdp->data_end - xdp->data;
974 skb_reserve(skb, headroom);
975 __skb_put(skb, data_len);
977 metasize = xdp->data - xdp->data_meta;
978 metasize = metasize > 0 ? metasize : 0;
980 skb_metadata_set(skb, metasize);
982 if (unlikely(xdp_buff_has_frags(xdp)))
983 xdp_update_skb_shared_info(skb, nr_frags,
984 sinfo->xdp_frags_size,
986 xdp_buff_is_frag_pfmemalloc(xdp));
991 /* TODO: build xdp in big mode */
992 static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
993 struct virtnet_info *vi,
994 struct receive_queue *rq,
995 struct xdp_buff *xdp,
998 unsigned int frame_sz,
1000 unsigned int *xdp_frags_truesize,
1001 struct virtnet_rq_stats *stats)
1003 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1004 unsigned int headroom, tailroom, room;
1005 unsigned int truesize, cur_frag_size;
1006 struct skb_shared_info *shinfo;
1007 unsigned int xdp_frags_truesz = 0;
1013 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
1014 xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM,
1015 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
1021 /* If we want to build multi-buffer xdp, we need
1022 * to specify that the flags of xdp_buff have the
1023 * XDP_FLAGS_HAS_FRAG bit.
1025 if (!xdp_buff_has_frags(xdp))
1026 xdp_buff_set_frags_flag(xdp);
1028 shinfo = xdp_get_shared_info_from_buff(xdp);
1029 shinfo->nr_frags = 0;
1030 shinfo->xdp_frags_size = 0;
1033 if (*num_buf > MAX_SKB_FRAGS + 1)
1036 while (--*num_buf > 0) {
1037 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
1038 if (unlikely(!buf)) {
1039 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1040 dev->name, *num_buf,
1041 virtio16_to_cpu(vi->vdev, hdr->num_buffers));
1042 dev->stats.rx_length_errors++;
1046 stats->bytes += len;
1047 page = virt_to_head_page(buf);
1048 offset = buf - page_address(page);
1050 truesize = mergeable_ctx_to_truesize(ctx);
1051 headroom = mergeable_ctx_to_headroom(ctx);
1052 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1053 room = SKB_DATA_ALIGN(headroom + tailroom);
1055 cur_frag_size = truesize;
1056 xdp_frags_truesz += cur_frag_size;
1057 if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) {
1059 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1060 dev->name, len, (unsigned long)(truesize - room));
1061 dev->stats.rx_length_errors++;
1065 frag = &shinfo->frags[shinfo->nr_frags++];
1066 __skb_frag_set_page(frag, page);
1067 skb_frag_off_set(frag, offset);
1068 skb_frag_size_set(frag, len);
1069 if (page_is_pfmemalloc(page))
1070 xdp_buff_set_frag_pfmemalloc(xdp);
1072 shinfo->xdp_frags_size += len;
1075 *xdp_frags_truesize = xdp_frags_truesz;
1079 static struct sk_buff *receive_mergeable(struct net_device *dev,
1080 struct virtnet_info *vi,
1081 struct receive_queue *rq,
1085 unsigned int *xdp_xmit,
1086 struct virtnet_rq_stats *stats)
1088 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1089 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1090 struct page *page = virt_to_head_page(buf);
1091 int offset = buf - page_address(page);
1092 struct sk_buff *head_skb, *curr_skb;
1093 struct bpf_prog *xdp_prog;
1094 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
1095 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1096 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1097 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1098 unsigned int frame_sz, xdp_room;
1102 stats->bytes += len - vi->hdr_len;
1104 if (unlikely(len > truesize - room)) {
1105 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1106 dev->name, len, (unsigned long)(truesize - room));
1107 dev->stats.rx_length_errors++;
1111 if (likely(!vi->xdp_enabled)) {
1117 xdp_prog = rcu_dereference(rq->xdp_prog);
1119 unsigned int xdp_frags_truesz = 0;
1120 struct skb_shared_info *shinfo;
1121 struct xdp_frame *xdpf;
1122 struct page *xdp_page;
1123 struct xdp_buff xdp;
1128 /* Transient failure which in theory could occur if
1129 * in-flight packets from before XDP was enabled reach
1130 * the receive path after XDP is loaded.
1132 if (unlikely(hdr->hdr.gso_type))
1135 /* Now XDP core assumes frag size is PAGE_SIZE, but buffers
1136 * with headroom may add hole in truesize, which
1137 * make their length exceed PAGE_SIZE. So we disabled the
1138 * hole mechanism for xdp. See add_recvbuf_mergeable().
1140 frame_sz = truesize;
1142 /* This happens when headroom is not enough because
1143 * of the buffer was prefilled before XDP is set.
1144 * This should only happen for the first several packets.
1145 * In fact, vq reset can be used here to help us clean up
1146 * the prefilled buffers, but many existing devices do not
1147 * support it, and we don't want to bother users who are
1148 * using xdp normally.
1150 if (!xdp_prog->aux->xdp_has_frags &&
1151 (num_buf > 1 || headroom < virtnet_get_headroom(vi))) {
1152 /* linearize data for XDP */
1153 xdp_page = xdp_linearize_page(rq, &num_buf,
1155 VIRTIO_XDP_HEADROOM,
1157 frame_sz = PAGE_SIZE;
1161 offset = VIRTIO_XDP_HEADROOM;
1162 } else if (unlikely(headroom < virtnet_get_headroom(vi))) {
1163 xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
1164 sizeof(struct skb_shared_info));
1165 if (len + xdp_room > PAGE_SIZE)
1168 xdp_page = alloc_page(GFP_ATOMIC);
1172 memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM,
1173 page_address(page) + offset, len);
1174 frame_sz = PAGE_SIZE;
1175 offset = VIRTIO_XDP_HEADROOM;
1180 data = page_address(xdp_page) + offset;
1181 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
1182 &num_buf, &xdp_frags_truesz, stats);
1186 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1187 stats->xdp_packets++;
1191 if (unlikely(xdp_page != page))
1193 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
1198 xdpf = xdp_convert_buff_to_frame(&xdp);
1199 if (unlikely(!xdpf)) {
1200 netdev_dbg(dev, "convert buff to frame failed for xdp\n");
1203 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1204 if (unlikely(!err)) {
1205 xdp_return_frame_rx_napi(xdpf);
1206 } else if (unlikely(err < 0)) {
1207 trace_xdp_exception(vi->dev, xdp_prog, act);
1210 *xdp_xmit |= VIRTIO_XDP_TX;
1211 if (unlikely(xdp_page != page))
1216 stats->xdp_redirects++;
1217 err = xdp_do_redirect(dev, &xdp, xdp_prog);
1220 *xdp_xmit |= VIRTIO_XDP_REDIR;
1221 if (unlikely(xdp_page != page))
1226 bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
1229 trace_xdp_exception(vi->dev, xdp_prog, act);
1235 if (unlikely(xdp_page != page))
1236 __free_pages(xdp_page, 0);
1238 if (xdp_buff_has_frags(&xdp)) {
1239 shinfo = xdp_get_shared_info_from_buff(&xdp);
1240 for (i = 0; i < shinfo->nr_frags; i++) {
1241 xdp_page = skb_frag_page(&shinfo->frags[i]);
1251 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
1252 curr_skb = head_skb;
1254 if (unlikely(!curr_skb))
1259 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
1260 if (unlikely(!buf)) {
1261 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1263 virtio16_to_cpu(vi->vdev,
1265 dev->stats.rx_length_errors++;
1269 stats->bytes += len;
1270 page = virt_to_head_page(buf);
1272 truesize = mergeable_ctx_to_truesize(ctx);
1273 headroom = mergeable_ctx_to_headroom(ctx);
1274 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1275 room = SKB_DATA_ALIGN(headroom + tailroom);
1276 if (unlikely(len > truesize - room)) {
1277 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1278 dev->name, len, (unsigned long)(truesize - room));
1279 dev->stats.rx_length_errors++;
1283 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
1284 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1285 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
1287 if (unlikely(!nskb))
1289 if (curr_skb == head_skb)
1290 skb_shinfo(curr_skb)->frag_list = nskb;
1292 curr_skb->next = nskb;
1294 head_skb->truesize += nskb->truesize;
1297 if (curr_skb != head_skb) {
1298 head_skb->data_len += len;
1299 head_skb->len += len;
1300 head_skb->truesize += truesize;
1302 offset = buf - page_address(page);
1303 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1305 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1308 skb_add_rx_frag(curr_skb, num_skb_frags, page,
1309 offset, len, truesize);
1313 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
1321 while (num_buf-- > 1) {
1322 buf = virtqueue_get_buf(rq->vq, &len);
1323 if (unlikely(!buf)) {
1324 pr_debug("%s: rx error: %d buffers missing\n",
1325 dev->name, num_buf);
1326 dev->stats.rx_length_errors++;
1329 stats->bytes += len;
1330 page = virt_to_head_page(buf);
1335 dev_kfree_skb(head_skb);
1340 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
1341 struct sk_buff *skb)
1343 enum pkt_hash_types rss_hash_type;
1345 if (!hdr_hash || !skb)
1348 switch (__le16_to_cpu(hdr_hash->hash_report)) {
1349 case VIRTIO_NET_HASH_REPORT_TCPv4:
1350 case VIRTIO_NET_HASH_REPORT_UDPv4:
1351 case VIRTIO_NET_HASH_REPORT_TCPv6:
1352 case VIRTIO_NET_HASH_REPORT_UDPv6:
1353 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
1354 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
1355 rss_hash_type = PKT_HASH_TYPE_L4;
1357 case VIRTIO_NET_HASH_REPORT_IPv4:
1358 case VIRTIO_NET_HASH_REPORT_IPv6:
1359 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
1360 rss_hash_type = PKT_HASH_TYPE_L3;
1362 case VIRTIO_NET_HASH_REPORT_NONE:
1364 rss_hash_type = PKT_HASH_TYPE_NONE;
1366 skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
1369 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1370 void *buf, unsigned int len, void **ctx,
1371 unsigned int *xdp_xmit,
1372 struct virtnet_rq_stats *stats)
1374 struct net_device *dev = vi->dev;
1375 struct sk_buff *skb;
1376 struct virtio_net_hdr_mrg_rxbuf *hdr;
1378 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
1379 pr_debug("%s: short packet %i\n", dev->name, len);
1380 dev->stats.rx_length_errors++;
1381 virtnet_rq_free_unused_buf(rq->vq, buf);
1385 if (vi->mergeable_rx_bufs)
1386 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1388 else if (vi->big_packets)
1389 skb = receive_big(dev, vi, rq, buf, len, stats);
1391 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1396 hdr = skb_vnet_hdr(skb);
1397 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
1398 virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
1400 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
1401 skb->ip_summed = CHECKSUM_UNNECESSARY;
1403 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1404 virtio_is_little_endian(vi->vdev))) {
1405 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1406 dev->name, hdr->hdr.gso_type,
1411 skb_record_rx_queue(skb, vq2rxq(rq->vq));
1412 skb->protocol = eth_type_trans(skb, dev);
1413 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1414 ntohs(skb->protocol), skb->len, skb->pkt_type);
1416 napi_gro_receive(&rq->napi, skb);
1420 dev->stats.rx_frame_errors++;
1424 /* Unlike mergeable buffers, all buffers are allocated to the
1425 * same size, except for the headroom. For this reason we do
1426 * not need to use mergeable_len_to_ctx here - it is enough
1427 * to store the headroom as the context ignoring the truesize.
1429 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1432 struct page_frag *alloc_frag = &rq->alloc_frag;
1434 unsigned int xdp_headroom = virtnet_get_headroom(vi);
1435 void *ctx = (void *)(unsigned long)xdp_headroom;
1436 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
1439 len = SKB_DATA_ALIGN(len) +
1440 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1441 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
1444 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1445 get_page(alloc_frag->page);
1446 alloc_frag->offset += len;
1447 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1448 vi->hdr_len + GOOD_PACKET_LEN);
1449 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1451 put_page(virt_to_head_page(buf));
1455 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1458 struct page *first, *list = NULL;
1462 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
1464 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
1465 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
1466 first = get_a_page(rq, gfp);
1469 give_pages(rq, list);
1472 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
1474 /* chain new page in list head to match sg */
1475 first->private = (unsigned long)list;
1479 first = get_a_page(rq, gfp);
1481 give_pages(rq, list);
1484 p = page_address(first);
1486 /* rq->sg[0], rq->sg[1] share the same page */
1487 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1488 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
1490 /* rq->sg[1] for data packet, from offset */
1491 offset = sizeof(struct padded_vnet_hdr);
1492 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
1494 /* chain first in list head */
1495 first->private = (unsigned long)list;
1496 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
1499 give_pages(rq, first);
1504 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
1505 struct ewma_pkt_len *avg_pkt_len,
1508 struct virtnet_info *vi = rq->vq->vdev->priv;
1509 const size_t hdr_len = vi->hdr_len;
1513 return PAGE_SIZE - room;
1515 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1516 rq->min_buf_len, PAGE_SIZE - hdr_len);
1518 return ALIGN(len, L1_CACHE_BYTES);
1521 static int add_recvbuf_mergeable(struct virtnet_info *vi,
1522 struct receive_queue *rq, gfp_t gfp)
1524 struct page_frag *alloc_frag = &rq->alloc_frag;
1525 unsigned int headroom = virtnet_get_headroom(vi);
1526 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1527 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1531 unsigned int len, hole;
1533 /* Extra tailroom is needed to satisfy XDP's assumption. This
1534 * means rx frags coalescing won't work, but consider we've
1535 * disabled GSO for XDP, it won't be a big issue.
1537 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1538 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
1541 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1542 buf += headroom; /* advance address leaving hole at front of pkt */
1543 get_page(alloc_frag->page);
1544 alloc_frag->offset += len + room;
1545 hole = alloc_frag->size - alloc_frag->offset;
1546 if (hole < len + room) {
1547 /* To avoid internal fragmentation, if there is very likely not
1548 * enough space for another buffer, add the remaining space to
1549 * the current buffer.
1550 * XDP core assumes that frame_size of xdp_buff and the length
1551 * of the frag are PAGE_SIZE, so we disable the hole mechanism.
1555 alloc_frag->offset += hole;
1558 sg_init_one(rq->sg, buf, len);
1559 ctx = mergeable_len_to_ctx(len + room, headroom);
1560 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1562 put_page(virt_to_head_page(buf));
1568 * Returns false if we couldn't fill entirely (OOM).
1570 * Normally run in the receive path, but can also be run from ndo_open
1571 * before we're receiving packets, or from refill_work which is
1572 * careful to disable receiving (using napi_disable).
1574 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1581 if (vi->mergeable_rx_bufs)
1582 err = add_recvbuf_mergeable(vi, rq, gfp);
1583 else if (vi->big_packets)
1584 err = add_recvbuf_big(vi, rq, gfp);
1586 err = add_recvbuf_small(vi, rq, gfp);
1588 oom = err == -ENOMEM;
1591 } while (rq->vq->num_free);
1592 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1593 unsigned long flags;
1595 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
1597 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
1603 static void skb_recv_done(struct virtqueue *rvq)
1605 struct virtnet_info *vi = rvq->vdev->priv;
1606 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1608 virtqueue_napi_schedule(&rq->napi, rvq);
1611 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
1615 /* If all buffers were filled by other side before we napi_enabled, we
1616 * won't get another interrupt, so process any outstanding packets now.
1617 * Call local_bh_enable after to trigger softIRQ processing.
1620 virtqueue_napi_schedule(napi, vq);
1624 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1625 struct virtqueue *vq,
1626 struct napi_struct *napi)
1631 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1632 * enable the feature if this is likely affine with the transmit path.
1634 if (!vi->affinity_hint_set) {
1639 return virtnet_napi_enable(vq, napi);
1642 static void virtnet_napi_tx_disable(struct napi_struct *napi)
1648 static void refill_work(struct work_struct *work)
1650 struct virtnet_info *vi =
1651 container_of(work, struct virtnet_info, refill.work);
1655 for (i = 0; i < vi->curr_queue_pairs; i++) {
1656 struct receive_queue *rq = &vi->rq[i];
1658 napi_disable(&rq->napi);
1659 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1660 virtnet_napi_enable(rq->vq, &rq->napi);
1662 /* In theory, this can happen: if we don't get any buffers in
1663 * we will *never* try to fill again.
1666 schedule_delayed_work(&vi->refill, HZ/2);
1670 static int virtnet_receive(struct receive_queue *rq, int budget,
1671 unsigned int *xdp_xmit)
1673 struct virtnet_info *vi = rq->vq->vdev->priv;
1674 struct virtnet_rq_stats stats = {};
1679 if (!vi->big_packets || vi->mergeable_rx_bufs) {
1682 while (stats.packets < budget &&
1683 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1684 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1688 while (stats.packets < budget &&
1689 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1690 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1695 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
1696 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
1697 spin_lock(&vi->refill_lock);
1698 if (vi->refill_enabled)
1699 schedule_delayed_work(&vi->refill, 0);
1700 spin_unlock(&vi->refill_lock);
1704 u64_stats_update_begin(&rq->stats.syncp);
1705 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1706 size_t offset = virtnet_rq_stats_desc[i].offset;
1709 item = (u64 *)((u8 *)&rq->stats + offset);
1710 *item += *(u64 *)((u8 *)&stats + offset);
1712 u64_stats_update_end(&rq->stats.syncp);
1714 return stats.packets;
1717 static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
1720 unsigned int packets = 0;
1721 unsigned int bytes = 0;
1724 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1725 if (likely(!is_xdp_frame(ptr))) {
1726 struct sk_buff *skb = ptr;
1728 pr_debug("Sent skb %p\n", skb);
1731 napi_consume_skb(skb, in_napi);
1733 struct xdp_frame *frame = ptr_to_xdp(ptr);
1735 bytes += xdp_get_frame_len(frame);
1736 xdp_return_frame(frame);
1741 /* Avoid overhead when no packets have been processed
1742 * happens when called speculatively from start_xmit.
1747 u64_stats_update_begin(&sq->stats.syncp);
1748 sq->stats.bytes += bytes;
1749 sq->stats.packets += packets;
1750 u64_stats_update_end(&sq->stats.syncp);
1753 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1755 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1757 else if (q < vi->curr_queue_pairs)
1763 static void virtnet_poll_cleantx(struct receive_queue *rq)
1765 struct virtnet_info *vi = rq->vq->vdev->priv;
1766 unsigned int index = vq2rxq(rq->vq);
1767 struct send_queue *sq = &vi->sq[index];
1768 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1770 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
1773 if (__netif_tx_trylock(txq)) {
1775 __netif_tx_unlock(txq);
1780 virtqueue_disable_cb(sq->vq);
1781 free_old_xmit_skbs(sq, true);
1782 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1784 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1785 netif_tx_wake_queue(txq);
1787 __netif_tx_unlock(txq);
1791 static int virtnet_poll(struct napi_struct *napi, int budget)
1793 struct receive_queue *rq =
1794 container_of(napi, struct receive_queue, napi);
1795 struct virtnet_info *vi = rq->vq->vdev->priv;
1796 struct send_queue *sq;
1797 unsigned int received;
1798 unsigned int xdp_xmit = 0;
1800 virtnet_poll_cleantx(rq);
1802 received = virtnet_receive(rq, budget, &xdp_xmit);
1804 if (xdp_xmit & VIRTIO_XDP_REDIR)
1807 /* Out of packets? */
1808 if (received < budget)
1809 virtqueue_napi_complete(napi, rq->vq, received);
1811 if (xdp_xmit & VIRTIO_XDP_TX) {
1812 sq = virtnet_xdp_get_sq(vi);
1813 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1814 u64_stats_update_begin(&sq->stats.syncp);
1816 u64_stats_update_end(&sq->stats.syncp);
1818 virtnet_xdp_put_sq(vi, sq);
1824 static int virtnet_open(struct net_device *dev)
1826 struct virtnet_info *vi = netdev_priv(dev);
1829 enable_delayed_refill(vi);
1831 for (i = 0; i < vi->max_queue_pairs; i++) {
1832 if (i < vi->curr_queue_pairs)
1833 /* Make sure we have some buffers: if oom use wq. */
1834 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1835 schedule_delayed_work(&vi->refill, 0);
1837 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
1841 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1842 MEM_TYPE_PAGE_SHARED, NULL);
1844 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1848 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1849 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1855 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1857 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1858 struct virtnet_info *vi = sq->vq->vdev->priv;
1859 unsigned int index = vq2txq(sq->vq);
1860 struct netdev_queue *txq;
1864 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1865 /* We don't need to enable cb for XDP */
1866 napi_complete_done(napi, 0);
1870 txq = netdev_get_tx_queue(vi->dev, index);
1871 __netif_tx_lock(txq, raw_smp_processor_id());
1872 virtqueue_disable_cb(sq->vq);
1873 free_old_xmit_skbs(sq, true);
1875 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1876 netif_tx_wake_queue(txq);
1878 opaque = virtqueue_enable_cb_prepare(sq->vq);
1880 done = napi_complete_done(napi, 0);
1883 virtqueue_disable_cb(sq->vq);
1885 __netif_tx_unlock(txq);
1888 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
1889 if (napi_schedule_prep(napi)) {
1890 __netif_tx_lock(txq, raw_smp_processor_id());
1891 virtqueue_disable_cb(sq->vq);
1892 __netif_tx_unlock(txq);
1893 __napi_schedule(napi);
1901 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1903 struct virtio_net_hdr_mrg_rxbuf *hdr;
1904 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1905 struct virtnet_info *vi = sq->vq->vdev->priv;
1907 unsigned hdr_len = vi->hdr_len;
1910 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1912 can_push = vi->any_header_sg &&
1913 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1914 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1915 /* Even if we can, don't push here yet as this would skew
1916 * csum_start offset below. */
1918 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1920 hdr = skb_vnet_hdr(skb);
1922 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1923 virtio_is_little_endian(vi->vdev), false,
1927 if (vi->mergeable_rx_bufs)
1928 hdr->num_buffers = 0;
1930 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1932 __skb_push(skb, hdr_len);
1933 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1934 if (unlikely(num_sg < 0))
1936 /* Pull header back to avoid skew in tx bytes calculations. */
1937 __skb_pull(skb, hdr_len);
1939 sg_set_buf(sq->sg, hdr, hdr_len);
1940 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1941 if (unlikely(num_sg < 0))
1945 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
1948 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
1950 struct virtnet_info *vi = netdev_priv(dev);
1951 int qnum = skb_get_queue_mapping(skb);
1952 struct send_queue *sq = &vi->sq[qnum];
1954 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1955 bool kick = !netdev_xmit_more();
1956 bool use_napi = sq->napi.weight;
1958 /* Free up any pending old buffers before queueing new ones. */
1961 virtqueue_disable_cb(sq->vq);
1963 free_old_xmit_skbs(sq, false);
1965 } while (use_napi && kick &&
1966 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1968 /* timestamp packet in software */
1969 skb_tx_timestamp(skb);
1971 /* Try to transmit */
1972 err = xmit_skb(sq, skb);
1974 /* This should not happen! */
1975 if (unlikely(err)) {
1976 dev->stats.tx_fifo_errors++;
1977 if (net_ratelimit())
1979 "Unexpected TXQ (%d) queue failure: %d\n",
1981 dev->stats.tx_dropped++;
1982 dev_kfree_skb_any(skb);
1983 return NETDEV_TX_OK;
1986 /* Don't wait up for transmitted skbs to be freed. */
1992 /* If running out of space, stop queue to avoid getting packets that we
1993 * are then unable to transmit.
1994 * An alternative would be to force queuing layer to requeue the skb by
1995 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1996 * returned in a normal path of operation: it means that driver is not
1997 * maintaining the TX queue stop/start state properly, and causes
1998 * the stack to do a non-trivial amount of useless work.
1999 * Since most packets only take 1 or 2 ring slots, stopping the queue
2000 * early means 16 slots are typically wasted.
2002 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
2003 netif_stop_subqueue(dev, qnum);
2005 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
2006 virtqueue_napi_schedule(&sq->napi, sq->vq);
2007 } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
2008 /* More just got used, free them then recheck. */
2009 free_old_xmit_skbs(sq, false);
2010 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
2011 netif_start_subqueue(dev, qnum);
2012 virtqueue_disable_cb(sq->vq);
2017 if (kick || netif_xmit_stopped(txq)) {
2018 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2019 u64_stats_update_begin(&sq->stats.syncp);
2021 u64_stats_update_end(&sq->stats.syncp);
2025 return NETDEV_TX_OK;
2028 static int virtnet_rx_resize(struct virtnet_info *vi,
2029 struct receive_queue *rq, u32 ring_num)
2031 bool running = netif_running(vi->dev);
2034 qindex = rq - vi->rq;
2037 napi_disable(&rq->napi);
2039 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_free_unused_buf);
2041 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
2043 if (!try_fill_recv(vi, rq, GFP_KERNEL))
2044 schedule_delayed_work(&vi->refill, 0);
2047 virtnet_napi_enable(rq->vq, &rq->napi);
2051 static int virtnet_tx_resize(struct virtnet_info *vi,
2052 struct send_queue *sq, u32 ring_num)
2054 bool running = netif_running(vi->dev);
2055 struct netdev_queue *txq;
2058 qindex = sq - vi->sq;
2061 virtnet_napi_tx_disable(&sq->napi);
2063 txq = netdev_get_tx_queue(vi->dev, qindex);
2065 /* 1. wait all ximt complete
2066 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
2068 __netif_tx_lock_bh(txq);
2070 /* Prevent rx poll from accessing sq. */
2073 /* Prevent the upper layer from trying to send packets. */
2074 netif_stop_subqueue(vi->dev, qindex);
2076 __netif_tx_unlock_bh(txq);
2078 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf);
2080 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
2082 __netif_tx_lock_bh(txq);
2084 netif_tx_wake_queue(txq);
2085 __netif_tx_unlock_bh(txq);
2088 virtnet_napi_tx_enable(vi, sq->vq, &sq->napi);
2093 * Send command via the control virtqueue and check status. Commands
2094 * supported by the hypervisor, as indicated by feature bits, should
2095 * never fail unless improperly formatted.
2097 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
2098 struct scatterlist *out)
2100 struct scatterlist *sgs[4], hdr, stat;
2101 unsigned out_num = 0, tmp;
2104 /* Caller should know better */
2105 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
2107 vi->ctrl->status = ~0;
2108 vi->ctrl->hdr.class = class;
2109 vi->ctrl->hdr.cmd = cmd;
2111 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
2112 sgs[out_num++] = &hdr;
2115 sgs[out_num++] = out;
2117 /* Add return status. */
2118 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
2119 sgs[out_num] = &stat;
2121 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
2122 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
2124 dev_warn(&vi->vdev->dev,
2125 "Failed to add sgs for command vq: %d\n.", ret);
2129 if (unlikely(!virtqueue_kick(vi->cvq)))
2130 return vi->ctrl->status == VIRTIO_NET_OK;
2132 /* Spin for a response, the kick causes an ioport write, trapping
2133 * into the hypervisor, so the request should be handled immediately.
2135 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
2136 !virtqueue_is_broken(vi->cvq))
2139 return vi->ctrl->status == VIRTIO_NET_OK;
2142 static int virtnet_set_mac_address(struct net_device *dev, void *p)
2144 struct virtnet_info *vi = netdev_priv(dev);
2145 struct virtio_device *vdev = vi->vdev;
2147 struct sockaddr *addr;
2148 struct scatterlist sg;
2150 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2153 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
2157 ret = eth_prepare_mac_addr_change(dev, addr);
2161 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
2162 sg_init_one(&sg, addr->sa_data, dev->addr_len);
2163 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2164 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
2165 dev_warn(&vdev->dev,
2166 "Failed to set mac address by vq command.\n");
2170 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
2171 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2174 /* Naturally, this has an atomicity problem. */
2175 for (i = 0; i < dev->addr_len; i++)
2176 virtio_cwrite8(vdev,
2177 offsetof(struct virtio_net_config, mac) +
2178 i, addr->sa_data[i]);
2181 eth_commit_mac_addr_change(dev, p);
2189 static void virtnet_stats(struct net_device *dev,
2190 struct rtnl_link_stats64 *tot)
2192 struct virtnet_info *vi = netdev_priv(dev);
2196 for (i = 0; i < vi->max_queue_pairs; i++) {
2197 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
2198 struct receive_queue *rq = &vi->rq[i];
2199 struct send_queue *sq = &vi->sq[i];
2202 start = u64_stats_fetch_begin(&sq->stats.syncp);
2203 tpackets = sq->stats.packets;
2204 tbytes = sq->stats.bytes;
2205 terrors = sq->stats.tx_timeouts;
2206 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
2209 start = u64_stats_fetch_begin(&rq->stats.syncp);
2210 rpackets = rq->stats.packets;
2211 rbytes = rq->stats.bytes;
2212 rdrops = rq->stats.drops;
2213 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
2215 tot->rx_packets += rpackets;
2216 tot->tx_packets += tpackets;
2217 tot->rx_bytes += rbytes;
2218 tot->tx_bytes += tbytes;
2219 tot->rx_dropped += rdrops;
2220 tot->tx_errors += terrors;
2223 tot->tx_dropped = dev->stats.tx_dropped;
2224 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
2225 tot->rx_length_errors = dev->stats.rx_length_errors;
2226 tot->rx_frame_errors = dev->stats.rx_frame_errors;
2229 static void virtnet_ack_link_announce(struct virtnet_info *vi)
2232 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
2233 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
2234 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
2238 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2240 struct scatterlist sg;
2241 struct net_device *dev = vi->dev;
2243 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
2246 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
2247 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
2249 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2250 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
2251 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
2255 vi->curr_queue_pairs = queue_pairs;
2256 /* virtnet_open() will refill when device is going to up. */
2257 if (dev->flags & IFF_UP)
2258 schedule_delayed_work(&vi->refill, 0);
2264 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2269 err = _virtnet_set_queues(vi, queue_pairs);
2274 static int virtnet_close(struct net_device *dev)
2276 struct virtnet_info *vi = netdev_priv(dev);
2279 /* Make sure NAPI doesn't schedule refill work */
2280 disable_delayed_refill(vi);
2281 /* Make sure refill_work doesn't re-enable napi! */
2282 cancel_delayed_work_sync(&vi->refill);
2284 for (i = 0; i < vi->max_queue_pairs; i++) {
2285 virtnet_napi_tx_disable(&vi->sq[i].napi);
2286 napi_disable(&vi->rq[i].napi);
2287 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
2293 static void virtnet_set_rx_mode(struct net_device *dev)
2295 struct virtnet_info *vi = netdev_priv(dev);
2296 struct scatterlist sg[2];
2297 struct virtio_net_ctrl_mac *mac_data;
2298 struct netdev_hw_addr *ha;
2304 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
2305 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
2308 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
2309 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2311 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
2313 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2314 VIRTIO_NET_CTRL_RX_PROMISC, sg))
2315 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
2316 vi->ctrl->promisc ? "en" : "dis");
2318 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
2320 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2321 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2322 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2323 vi->ctrl->allmulti ? "en" : "dis");
2325 uc_count = netdev_uc_count(dev);
2326 mc_count = netdev_mc_count(dev);
2327 /* MAC filter - use one buffer for both lists */
2328 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2329 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
2334 sg_init_table(sg, 2);
2336 /* Store the unicast list and count in the front of the buffer */
2337 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2339 netdev_for_each_uc_addr(ha, dev)
2340 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2342 sg_set_buf(&sg[0], mac_data,
2343 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2345 /* multicast list and count fill the end */
2346 mac_data = (void *)&mac_data->macs[uc_count][0];
2348 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2350 netdev_for_each_mc_addr(ha, dev)
2351 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2353 sg_set_buf(&sg[1], mac_data,
2354 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2356 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2357 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
2358 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2363 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2364 __be16 proto, u16 vid)
2366 struct virtnet_info *vi = netdev_priv(dev);
2367 struct scatterlist sg;
2369 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2370 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2372 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2373 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
2374 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
2378 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2379 __be16 proto, u16 vid)
2381 struct virtnet_info *vi = netdev_priv(dev);
2382 struct scatterlist sg;
2384 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2385 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2387 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2388 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
2389 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
2393 static void virtnet_clean_affinity(struct virtnet_info *vi)
2397 if (vi->affinity_hint_set) {
2398 for (i = 0; i < vi->max_queue_pairs; i++) {
2399 virtqueue_set_affinity(vi->rq[i].vq, NULL);
2400 virtqueue_set_affinity(vi->sq[i].vq, NULL);
2403 vi->affinity_hint_set = false;
2407 static void virtnet_set_affinity(struct virtnet_info *vi)
2416 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2417 virtnet_clean_affinity(vi);
2421 num_cpu = num_online_cpus();
2422 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2423 stragglers = num_cpu >= vi->curr_queue_pairs ?
2424 num_cpu % vi->curr_queue_pairs :
2426 cpu = cpumask_first(cpu_online_mask);
2428 for (i = 0; i < vi->curr_queue_pairs; i++) {
2429 group_size = stride + (i < stragglers ? 1 : 0);
2431 for (j = 0; j < group_size; j++) {
2432 cpumask_set_cpu(cpu, mask);
2433 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2436 virtqueue_set_affinity(vi->rq[i].vq, mask);
2437 virtqueue_set_affinity(vi->sq[i].vq, mask);
2438 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
2439 cpumask_clear(mask);
2442 vi->affinity_hint_set = true;
2443 free_cpumask_var(mask);
2446 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
2448 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2450 virtnet_set_affinity(vi);
2454 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2456 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2458 virtnet_set_affinity(vi);
2462 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2464 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2467 virtnet_clean_affinity(vi);
2471 static enum cpuhp_state virtionet_online;
2473 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2477 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2480 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2484 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2488 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2490 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2491 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2495 static void virtnet_get_ringparam(struct net_device *dev,
2496 struct ethtool_ringparam *ring,
2497 struct kernel_ethtool_ringparam *kernel_ring,
2498 struct netlink_ext_ack *extack)
2500 struct virtnet_info *vi = netdev_priv(dev);
2502 ring->rx_max_pending = vi->rq[0].vq->num_max;
2503 ring->tx_max_pending = vi->sq[0].vq->num_max;
2504 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2505 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2508 static int virtnet_set_ringparam(struct net_device *dev,
2509 struct ethtool_ringparam *ring,
2510 struct kernel_ethtool_ringparam *kernel_ring,
2511 struct netlink_ext_ack *extack)
2513 struct virtnet_info *vi = netdev_priv(dev);
2514 u32 rx_pending, tx_pending;
2515 struct receive_queue *rq;
2516 struct send_queue *sq;
2519 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2522 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2523 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2525 if (ring->rx_pending == rx_pending &&
2526 ring->tx_pending == tx_pending)
2529 if (ring->rx_pending > vi->rq[0].vq->num_max)
2532 if (ring->tx_pending > vi->sq[0].vq->num_max)
2535 for (i = 0; i < vi->max_queue_pairs; i++) {
2539 if (ring->tx_pending != tx_pending) {
2540 err = virtnet_tx_resize(vi, sq, ring->tx_pending);
2545 if (ring->rx_pending != rx_pending) {
2546 err = virtnet_rx_resize(vi, rq, ring->rx_pending);
2555 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
2557 struct net_device *dev = vi->dev;
2558 struct scatterlist sgs[4];
2559 unsigned int sg_buf_size;
2562 sg_init_table(sgs, 4);
2564 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
2565 sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
2567 sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
2568 sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
2570 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
2571 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
2572 sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
2574 sg_buf_size = vi->rss_key_size;
2575 sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
2577 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2578 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
2579 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
2580 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
2586 static void virtnet_init_default_rss(struct virtnet_info *vi)
2591 vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
2592 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
2593 vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
2594 ? vi->rss_indir_table_size - 1 : 0;
2595 vi->ctrl->rss.unclassified_queue = 0;
2597 for (; i < vi->rss_indir_table_size; ++i) {
2598 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
2599 vi->ctrl->rss.indirection_table[i] = indir_val;
2602 vi->ctrl->rss.max_tx_vq = vi->curr_queue_pairs;
2603 vi->ctrl->rss.hash_key_length = vi->rss_key_size;
2605 netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
2608 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
2611 switch (info->flow_type) {
2613 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
2614 info->data = RXH_IP_SRC | RXH_IP_DST |
2615 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2616 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2617 info->data = RXH_IP_SRC | RXH_IP_DST;
2621 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
2622 info->data = RXH_IP_SRC | RXH_IP_DST |
2623 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2624 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2625 info->data = RXH_IP_SRC | RXH_IP_DST;
2629 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
2630 info->data = RXH_IP_SRC | RXH_IP_DST |
2631 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2632 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2633 info->data = RXH_IP_SRC | RXH_IP_DST;
2637 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
2638 info->data = RXH_IP_SRC | RXH_IP_DST |
2639 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2640 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2641 info->data = RXH_IP_SRC | RXH_IP_DST;
2645 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
2646 info->data = RXH_IP_SRC | RXH_IP_DST;
2650 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
2651 info->data = RXH_IP_SRC | RXH_IP_DST;
2660 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
2662 u32 new_hashtypes = vi->rss_hash_types_saved;
2663 bool is_disable = info->data & RXH_DISCARD;
2664 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
2666 /* supports only 'sd', 'sdfn' and 'r' */
2667 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
2670 switch (info->flow_type) {
2672 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
2674 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2675 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
2678 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
2680 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2681 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
2684 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2686 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2689 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
2691 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2692 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
2695 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
2697 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2698 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
2701 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2703 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2706 /* unsupported flow */
2710 /* if unsupported hashtype was set */
2711 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
2714 if (new_hashtypes != vi->rss_hash_types_saved) {
2715 vi->rss_hash_types_saved = new_hashtypes;
2716 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
2717 if (vi->dev->features & NETIF_F_RXHASH)
2718 return virtnet_commit_rss_command(vi);
2724 static void virtnet_get_drvinfo(struct net_device *dev,
2725 struct ethtool_drvinfo *info)
2727 struct virtnet_info *vi = netdev_priv(dev);
2728 struct virtio_device *vdev = vi->vdev;
2730 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2731 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2732 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
2736 /* TODO: Eliminate OOO packets during switching */
2737 static int virtnet_set_channels(struct net_device *dev,
2738 struct ethtool_channels *channels)
2740 struct virtnet_info *vi = netdev_priv(dev);
2741 u16 queue_pairs = channels->combined_count;
2744 /* We don't support separate rx/tx channels.
2745 * We don't allow setting 'other' channels.
2747 if (channels->rx_count || channels->tx_count || channels->other_count)
2750 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
2753 /* For now we don't support modifying channels while XDP is loaded
2754 * also when XDP is loaded all RX queues have XDP programs so we only
2755 * need to check a single RX queue.
2757 if (vi->rq[0].xdp_prog)
2761 err = _virtnet_set_queues(vi, queue_pairs);
2766 virtnet_set_affinity(vi);
2769 netif_set_real_num_tx_queues(dev, queue_pairs);
2770 netif_set_real_num_rx_queues(dev, queue_pairs);
2775 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2777 struct virtnet_info *vi = netdev_priv(dev);
2781 switch (stringset) {
2783 for (i = 0; i < vi->curr_queue_pairs; i++) {
2784 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2785 ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2786 virtnet_rq_stats_desc[j].desc);
2789 for (i = 0; i < vi->curr_queue_pairs; i++) {
2790 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2791 ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2792 virtnet_sq_stats_desc[j].desc);
2798 static int virtnet_get_sset_count(struct net_device *dev, int sset)
2800 struct virtnet_info *vi = netdev_priv(dev);
2804 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2805 VIRTNET_SQ_STATS_LEN);
2811 static void virtnet_get_ethtool_stats(struct net_device *dev,
2812 struct ethtool_stats *stats, u64 *data)
2814 struct virtnet_info *vi = netdev_priv(dev);
2815 unsigned int idx = 0, start, i, j;
2816 const u8 *stats_base;
2819 for (i = 0; i < vi->curr_queue_pairs; i++) {
2820 struct receive_queue *rq = &vi->rq[i];
2822 stats_base = (u8 *)&rq->stats;
2824 start = u64_stats_fetch_begin(&rq->stats.syncp);
2825 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2826 offset = virtnet_rq_stats_desc[j].offset;
2827 data[idx + j] = *(u64 *)(stats_base + offset);
2829 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
2830 idx += VIRTNET_RQ_STATS_LEN;
2833 for (i = 0; i < vi->curr_queue_pairs; i++) {
2834 struct send_queue *sq = &vi->sq[i];
2836 stats_base = (u8 *)&sq->stats;
2838 start = u64_stats_fetch_begin(&sq->stats.syncp);
2839 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2840 offset = virtnet_sq_stats_desc[j].offset;
2841 data[idx + j] = *(u64 *)(stats_base + offset);
2843 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
2844 idx += VIRTNET_SQ_STATS_LEN;
2848 static void virtnet_get_channels(struct net_device *dev,
2849 struct ethtool_channels *channels)
2851 struct virtnet_info *vi = netdev_priv(dev);
2853 channels->combined_count = vi->curr_queue_pairs;
2854 channels->max_combined = vi->max_queue_pairs;
2855 channels->max_other = 0;
2856 channels->rx_count = 0;
2857 channels->tx_count = 0;
2858 channels->other_count = 0;
2861 static int virtnet_set_link_ksettings(struct net_device *dev,
2862 const struct ethtool_link_ksettings *cmd)
2864 struct virtnet_info *vi = netdev_priv(dev);
2866 return ethtool_virtdev_set_link_ksettings(dev, cmd,
2867 &vi->speed, &vi->duplex);
2870 static int virtnet_get_link_ksettings(struct net_device *dev,
2871 struct ethtool_link_ksettings *cmd)
2873 struct virtnet_info *vi = netdev_priv(dev);
2875 cmd->base.speed = vi->speed;
2876 cmd->base.duplex = vi->duplex;
2877 cmd->base.port = PORT_OTHER;
2882 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
2883 struct ethtool_coalesce *ec)
2885 struct scatterlist sgs_tx, sgs_rx;
2886 struct virtio_net_ctrl_coal_tx coal_tx;
2887 struct virtio_net_ctrl_coal_rx coal_rx;
2889 coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
2890 coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
2891 sg_init_one(&sgs_tx, &coal_tx, sizeof(coal_tx));
2893 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
2894 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
2898 /* Save parameters */
2899 vi->tx_usecs = ec->tx_coalesce_usecs;
2900 vi->tx_max_packets = ec->tx_max_coalesced_frames;
2902 coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
2903 coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
2904 sg_init_one(&sgs_rx, &coal_rx, sizeof(coal_rx));
2906 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
2907 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
2911 /* Save parameters */
2912 vi->rx_usecs = ec->rx_coalesce_usecs;
2913 vi->rx_max_packets = ec->rx_max_coalesced_frames;
2918 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
2920 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
2921 * feature is negotiated.
2923 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
2926 if (ec->tx_max_coalesced_frames > 1 ||
2927 ec->rx_max_coalesced_frames != 1)
2933 static int virtnet_set_coalesce(struct net_device *dev,
2934 struct ethtool_coalesce *ec,
2935 struct kernel_ethtool_coalesce *kernel_coal,
2936 struct netlink_ext_ack *extack)
2938 struct virtnet_info *vi = netdev_priv(dev);
2939 int ret, i, napi_weight;
2940 bool update_napi = false;
2942 /* Can't change NAPI weight if the link is up */
2943 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
2944 if (napi_weight ^ vi->sq[0].napi.weight) {
2945 if (dev->flags & IFF_UP)
2951 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
2952 ret = virtnet_send_notf_coal_cmds(vi, ec);
2954 ret = virtnet_coal_params_supported(ec);
2960 for (i = 0; i < vi->max_queue_pairs; i++)
2961 vi->sq[i].napi.weight = napi_weight;
2967 static int virtnet_get_coalesce(struct net_device *dev,
2968 struct ethtool_coalesce *ec,
2969 struct kernel_ethtool_coalesce *kernel_coal,
2970 struct netlink_ext_ack *extack)
2972 struct virtnet_info *vi = netdev_priv(dev);
2974 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
2975 ec->rx_coalesce_usecs = vi->rx_usecs;
2976 ec->tx_coalesce_usecs = vi->tx_usecs;
2977 ec->tx_max_coalesced_frames = vi->tx_max_packets;
2978 ec->rx_max_coalesced_frames = vi->rx_max_packets;
2980 ec->rx_max_coalesced_frames = 1;
2982 if (vi->sq[0].napi.weight)
2983 ec->tx_max_coalesced_frames = 1;
2989 static void virtnet_init_settings(struct net_device *dev)
2991 struct virtnet_info *vi = netdev_priv(dev);
2993 vi->speed = SPEED_UNKNOWN;
2994 vi->duplex = DUPLEX_UNKNOWN;
2997 static void virtnet_update_settings(struct virtnet_info *vi)
3002 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
3005 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
3007 if (ethtool_validate_speed(speed))
3010 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
3012 if (ethtool_validate_duplex(duplex))
3013 vi->duplex = duplex;
3016 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
3018 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
3021 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
3023 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
3026 static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
3028 struct virtnet_info *vi = netdev_priv(dev);
3032 for (i = 0; i < vi->rss_indir_table_size; ++i)
3033 indir[i] = vi->ctrl->rss.indirection_table[i];
3037 memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
3040 *hfunc = ETH_RSS_HASH_TOP;
3045 static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
3047 struct virtnet_info *vi = netdev_priv(dev);
3050 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3054 for (i = 0; i < vi->rss_indir_table_size; ++i)
3055 vi->ctrl->rss.indirection_table[i] = indir[i];
3058 memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
3060 virtnet_commit_rss_command(vi);
3065 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
3067 struct virtnet_info *vi = netdev_priv(dev);
3070 switch (info->cmd) {
3071 case ETHTOOL_GRXRINGS:
3072 info->data = vi->curr_queue_pairs;
3075 virtnet_get_hashflow(vi, info);
3084 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
3086 struct virtnet_info *vi = netdev_priv(dev);
3089 switch (info->cmd) {
3091 if (!virtnet_set_hashflow(vi, info))
3102 static const struct ethtool_ops virtnet_ethtool_ops = {
3103 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
3104 ETHTOOL_COALESCE_USECS,
3105 .get_drvinfo = virtnet_get_drvinfo,
3106 .get_link = ethtool_op_get_link,
3107 .get_ringparam = virtnet_get_ringparam,
3108 .set_ringparam = virtnet_set_ringparam,
3109 .get_strings = virtnet_get_strings,
3110 .get_sset_count = virtnet_get_sset_count,
3111 .get_ethtool_stats = virtnet_get_ethtool_stats,
3112 .set_channels = virtnet_set_channels,
3113 .get_channels = virtnet_get_channels,
3114 .get_ts_info = ethtool_op_get_ts_info,
3115 .get_link_ksettings = virtnet_get_link_ksettings,
3116 .set_link_ksettings = virtnet_set_link_ksettings,
3117 .set_coalesce = virtnet_set_coalesce,
3118 .get_coalesce = virtnet_get_coalesce,
3119 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
3120 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
3121 .get_rxfh = virtnet_get_rxfh,
3122 .set_rxfh = virtnet_set_rxfh,
3123 .get_rxnfc = virtnet_get_rxnfc,
3124 .set_rxnfc = virtnet_set_rxnfc,
3127 static void virtnet_freeze_down(struct virtio_device *vdev)
3129 struct virtnet_info *vi = vdev->priv;
3131 /* Make sure no work handler is accessing the device */
3132 flush_work(&vi->config_work);
3134 netif_tx_lock_bh(vi->dev);
3135 netif_device_detach(vi->dev);
3136 netif_tx_unlock_bh(vi->dev);
3137 if (netif_running(vi->dev))
3138 virtnet_close(vi->dev);
3141 static int init_vqs(struct virtnet_info *vi);
3143 static int virtnet_restore_up(struct virtio_device *vdev)
3145 struct virtnet_info *vi = vdev->priv;
3152 virtio_device_ready(vdev);
3154 enable_delayed_refill(vi);
3156 if (netif_running(vi->dev)) {
3157 err = virtnet_open(vi->dev);
3162 netif_tx_lock_bh(vi->dev);
3163 netif_device_attach(vi->dev);
3164 netif_tx_unlock_bh(vi->dev);
3168 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
3170 struct scatterlist sg;
3171 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
3173 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
3175 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
3176 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
3177 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
3184 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
3188 if (!vi->guest_offloads)
3191 return virtnet_set_guest_offloads(vi, offloads);
3194 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
3196 u64 offloads = vi->guest_offloads;
3198 if (!vi->guest_offloads)
3201 return virtnet_set_guest_offloads(vi, offloads);
3204 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
3205 struct netlink_ext_ack *extack)
3207 unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
3208 sizeof(struct skb_shared_info));
3209 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
3210 struct virtnet_info *vi = netdev_priv(dev);
3211 struct bpf_prog *old_prog;
3212 u16 xdp_qp = 0, curr_qp;
3215 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
3216 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3217 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3218 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
3219 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
3220 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
3221 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
3222 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
3223 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
3227 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
3228 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
3232 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
3233 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
3234 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
3238 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
3240 xdp_qp = nr_cpu_ids;
3242 /* XDP requires extra queues for XDP_TX */
3243 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
3244 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",
3245 curr_qp + xdp_qp, vi->max_queue_pairs);
3249 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
3250 if (!prog && !old_prog)
3254 bpf_prog_add(prog, vi->max_queue_pairs - 1);
3256 /* Make sure NAPI is not using any XDP TX queues for RX. */
3257 if (netif_running(dev)) {
3258 for (i = 0; i < vi->max_queue_pairs; i++) {
3259 napi_disable(&vi->rq[i].napi);
3260 virtnet_napi_tx_disable(&vi->sq[i].napi);
3265 for (i = 0; i < vi->max_queue_pairs; i++) {
3266 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
3268 virtnet_restore_guest_offloads(vi);
3273 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
3276 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
3277 vi->xdp_queue_pairs = xdp_qp;
3280 vi->xdp_enabled = true;
3281 for (i = 0; i < vi->max_queue_pairs; i++) {
3282 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
3283 if (i == 0 && !old_prog)
3284 virtnet_clear_guest_offloads(vi);
3287 xdp_features_set_redirect_target(dev, true);
3289 xdp_features_clear_redirect_target(dev);
3290 vi->xdp_enabled = false;
3293 for (i = 0; i < vi->max_queue_pairs; i++) {
3295 bpf_prog_put(old_prog);
3296 if (netif_running(dev)) {
3297 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3298 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3307 virtnet_clear_guest_offloads(vi);
3308 for (i = 0; i < vi->max_queue_pairs; i++)
3309 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
3312 if (netif_running(dev)) {
3313 for (i = 0; i < vi->max_queue_pairs; i++) {
3314 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3315 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3320 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
3324 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3326 switch (xdp->command) {
3327 case XDP_SETUP_PROG:
3328 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
3334 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
3337 struct virtnet_info *vi = netdev_priv(dev);
3340 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3343 ret = snprintf(buf, len, "sby");
3350 static int virtnet_set_features(struct net_device *dev,
3351 netdev_features_t features)
3353 struct virtnet_info *vi = netdev_priv(dev);
3357 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
3358 if (vi->xdp_enabled)
3361 if (features & NETIF_F_GRO_HW)
3362 offloads = vi->guest_offloads_capable;
3364 offloads = vi->guest_offloads_capable &
3365 ~GUEST_OFFLOAD_GRO_HW_MASK;
3367 err = virtnet_set_guest_offloads(vi, offloads);
3370 vi->guest_offloads = offloads;
3373 if ((dev->features ^ features) & NETIF_F_RXHASH) {
3374 if (features & NETIF_F_RXHASH)
3375 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3377 vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
3379 if (!virtnet_commit_rss_command(vi))
3386 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
3388 struct virtnet_info *priv = netdev_priv(dev);
3389 struct send_queue *sq = &priv->sq[txqueue];
3390 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
3392 u64_stats_update_begin(&sq->stats.syncp);
3393 sq->stats.tx_timeouts++;
3394 u64_stats_update_end(&sq->stats.syncp);
3396 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
3397 txqueue, sq->name, sq->vq->index, sq->vq->name,
3398 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
3401 static const struct net_device_ops virtnet_netdev = {
3402 .ndo_open = virtnet_open,
3403 .ndo_stop = virtnet_close,
3404 .ndo_start_xmit = start_xmit,
3405 .ndo_validate_addr = eth_validate_addr,
3406 .ndo_set_mac_address = virtnet_set_mac_address,
3407 .ndo_set_rx_mode = virtnet_set_rx_mode,
3408 .ndo_get_stats64 = virtnet_stats,
3409 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
3410 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
3411 .ndo_bpf = virtnet_xdp,
3412 .ndo_xdp_xmit = virtnet_xdp_xmit,
3413 .ndo_features_check = passthru_features_check,
3414 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
3415 .ndo_set_features = virtnet_set_features,
3416 .ndo_tx_timeout = virtnet_tx_timeout,
3419 static void virtnet_config_changed_work(struct work_struct *work)
3421 struct virtnet_info *vi =
3422 container_of(work, struct virtnet_info, config_work);
3425 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
3426 struct virtio_net_config, status, &v) < 0)
3429 if (v & VIRTIO_NET_S_ANNOUNCE) {
3430 netdev_notify_peers(vi->dev);
3431 virtnet_ack_link_announce(vi);
3434 /* Ignore unknown (future) status bits */
3435 v &= VIRTIO_NET_S_LINK_UP;
3437 if (vi->status == v)
3442 if (vi->status & VIRTIO_NET_S_LINK_UP) {
3443 virtnet_update_settings(vi);
3444 netif_carrier_on(vi->dev);
3445 netif_tx_wake_all_queues(vi->dev);
3447 netif_carrier_off(vi->dev);
3448 netif_tx_stop_all_queues(vi->dev);
3452 static void virtnet_config_changed(struct virtio_device *vdev)
3454 struct virtnet_info *vi = vdev->priv;
3456 schedule_work(&vi->config_work);
3459 static void virtnet_free_queues(struct virtnet_info *vi)
3463 for (i = 0; i < vi->max_queue_pairs; i++) {
3464 __netif_napi_del(&vi->rq[i].napi);
3465 __netif_napi_del(&vi->sq[i].napi);
3468 /* We called __netif_napi_del(),
3469 * we need to respect an RCU grace period before freeing vi->rq
3478 static void _free_receive_bufs(struct virtnet_info *vi)
3480 struct bpf_prog *old_prog;
3483 for (i = 0; i < vi->max_queue_pairs; i++) {
3484 while (vi->rq[i].pages)
3485 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
3487 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
3488 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
3490 bpf_prog_put(old_prog);
3494 static void free_receive_bufs(struct virtnet_info *vi)
3497 _free_receive_bufs(vi);
3501 static void free_receive_page_frags(struct virtnet_info *vi)
3504 for (i = 0; i < vi->max_queue_pairs; i++)
3505 if (vi->rq[i].alloc_frag.page)
3506 put_page(vi->rq[i].alloc_frag.page);
3509 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
3511 if (!is_xdp_frame(buf))
3514 xdp_return_frame(ptr_to_xdp(buf));
3517 static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf)
3519 struct virtnet_info *vi = vq->vdev->priv;
3522 if (vi->mergeable_rx_bufs)
3523 put_page(virt_to_head_page(buf));
3524 else if (vi->big_packets)
3525 give_pages(&vi->rq[i], buf);
3527 put_page(virt_to_head_page(buf));
3530 static void free_unused_bufs(struct virtnet_info *vi)
3535 for (i = 0; i < vi->max_queue_pairs; i++) {
3536 struct virtqueue *vq = vi->sq[i].vq;
3537 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
3538 virtnet_sq_free_unused_buf(vq, buf);
3541 for (i = 0; i < vi->max_queue_pairs; i++) {
3542 struct virtqueue *vq = vi->rq[i].vq;
3543 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
3544 virtnet_rq_free_unused_buf(vq, buf);
3548 static void virtnet_del_vqs(struct virtnet_info *vi)
3550 struct virtio_device *vdev = vi->vdev;
3552 virtnet_clean_affinity(vi);
3554 vdev->config->del_vqs(vdev);
3556 virtnet_free_queues(vi);
3559 /* How large should a single buffer be so a queue full of these can fit at
3560 * least one full packet?
3561 * Logic below assumes the mergeable buffer header is used.
3563 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
3565 const unsigned int hdr_len = vi->hdr_len;
3566 unsigned int rq_size = virtqueue_get_vring_size(vq);
3567 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
3568 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
3569 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
3571 return max(max(min_buf_len, hdr_len) - hdr_len,
3572 (unsigned int)GOOD_PACKET_LEN);
3575 static int virtnet_find_vqs(struct virtnet_info *vi)
3577 vq_callback_t **callbacks;
3578 struct virtqueue **vqs;
3584 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
3585 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
3586 * possible control vq.
3588 total_vqs = vi->max_queue_pairs * 2 +
3589 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
3591 /* Allocate space for find_vqs parameters */
3592 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
3595 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
3598 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
3601 if (!vi->big_packets || vi->mergeable_rx_bufs) {
3602 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
3609 /* Parameters for control virtqueue, if any */
3611 callbacks[total_vqs - 1] = NULL;
3612 names[total_vqs - 1] = "control";
3615 /* Allocate/initialize parameters for send/receive virtqueues */
3616 for (i = 0; i < vi->max_queue_pairs; i++) {
3617 callbacks[rxq2vq(i)] = skb_recv_done;
3618 callbacks[txq2vq(i)] = skb_xmit_done;
3619 sprintf(vi->rq[i].name, "input.%d", i);
3620 sprintf(vi->sq[i].name, "output.%d", i);
3621 names[rxq2vq(i)] = vi->rq[i].name;
3622 names[txq2vq(i)] = vi->sq[i].name;
3624 ctx[rxq2vq(i)] = true;
3627 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
3633 vi->cvq = vqs[total_vqs - 1];
3634 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
3635 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3638 for (i = 0; i < vi->max_queue_pairs; i++) {
3639 vi->rq[i].vq = vqs[rxq2vq(i)];
3640 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
3641 vi->sq[i].vq = vqs[txq2vq(i)];
3644 /* run here: ret == 0. */
3659 static int virtnet_alloc_queues(struct virtnet_info *vi)
3664 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
3670 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
3673 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
3677 INIT_DELAYED_WORK(&vi->refill, refill_work);
3678 for (i = 0; i < vi->max_queue_pairs; i++) {
3679 vi->rq[i].pages = NULL;
3680 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
3682 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
3684 napi_tx ? napi_weight : 0);
3686 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
3687 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
3688 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
3690 u64_stats_init(&vi->rq[i].stats.syncp);
3691 u64_stats_init(&vi->sq[i].stats.syncp);
3704 static int init_vqs(struct virtnet_info *vi)
3708 /* Allocate send & receive queues */
3709 ret = virtnet_alloc_queues(vi);
3713 ret = virtnet_find_vqs(vi);
3718 virtnet_set_affinity(vi);
3724 virtnet_free_queues(vi);
3730 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
3733 struct virtnet_info *vi = netdev_priv(queue->dev);
3734 unsigned int queue_index = get_netdev_rx_queue_index(queue);
3735 unsigned int headroom = virtnet_get_headroom(vi);
3736 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
3737 struct ewma_pkt_len *avg;
3739 BUG_ON(queue_index >= vi->max_queue_pairs);
3740 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
3741 return sprintf(buf, "%u\n",
3742 get_mergeable_buf_len(&vi->rq[queue_index], avg,
3743 SKB_DATA_ALIGN(headroom + tailroom)));
3746 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
3747 __ATTR_RO(mergeable_rx_buffer_size);
3749 static struct attribute *virtio_net_mrg_rx_attrs[] = {
3750 &mergeable_rx_buffer_size_attribute.attr,
3754 static const struct attribute_group virtio_net_mrg_rx_group = {
3755 .name = "virtio_net",
3756 .attrs = virtio_net_mrg_rx_attrs
3760 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
3762 const char *fname, const char *dname)
3764 if (!virtio_has_feature(vdev, fbit))
3767 dev_err(&vdev->dev, "device advertises feature %s but not %s",
3773 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
3774 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
3776 static bool virtnet_validate_features(struct virtio_device *vdev)
3778 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
3779 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
3780 "VIRTIO_NET_F_CTRL_VQ") ||
3781 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
3782 "VIRTIO_NET_F_CTRL_VQ") ||
3783 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
3784 "VIRTIO_NET_F_CTRL_VQ") ||
3785 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
3786 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
3787 "VIRTIO_NET_F_CTRL_VQ") ||
3788 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
3789 "VIRTIO_NET_F_CTRL_VQ") ||
3790 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
3791 "VIRTIO_NET_F_CTRL_VQ") ||
3792 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
3793 "VIRTIO_NET_F_CTRL_VQ"))) {
3800 #define MIN_MTU ETH_MIN_MTU
3801 #define MAX_MTU ETH_MAX_MTU
3803 static int virtnet_validate(struct virtio_device *vdev)
3805 if (!vdev->config->get) {
3806 dev_err(&vdev->dev, "%s failure: config access disabled\n",
3811 if (!virtnet_validate_features(vdev))
3814 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3815 int mtu = virtio_cread16(vdev,
3816 offsetof(struct virtio_net_config,
3819 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3822 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
3823 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
3824 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
3825 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
3831 static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
3833 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3834 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3835 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
3836 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
3837 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
3838 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
3841 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
3843 bool guest_gso = virtnet_check_guest_gso(vi);
3845 /* If device can receive ANY guest GSO packets, regardless of mtu,
3846 * allocate packets of maximum size, otherwise limit it to only
3847 * mtu size worth only.
3849 if (mtu > ETH_DATA_LEN || guest_gso) {
3850 vi->big_packets = true;
3851 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
3855 static int virtnet_probe(struct virtio_device *vdev)
3857 int i, err = -ENOMEM;
3858 struct net_device *dev;
3859 struct virtnet_info *vi;
3860 u16 max_queue_pairs;
3863 /* Find if host supports multiqueue/rss virtio_net device */
3864 max_queue_pairs = 1;
3865 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3867 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
3869 /* We need at least 2 queue's */
3870 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3871 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3872 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3873 max_queue_pairs = 1;
3875 /* Allocate ourselves a network device with room for our info */
3876 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
3880 /* Set up network device as normal. */
3881 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3882 IFF_TX_SKB_NO_LINEAR;
3883 dev->netdev_ops = &virtnet_netdev;
3884 dev->features = NETIF_F_HIGHDMA;
3886 dev->ethtool_ops = &virtnet_ethtool_ops;
3887 SET_NETDEV_DEV(dev, &vdev->dev);
3889 /* Do we support "hardware" checksums? */
3890 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
3891 /* This opens up the world of extra features. */
3892 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3894 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3896 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
3897 dev->hw_features |= NETIF_F_TSO
3898 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
3900 /* Individual feature bits: what can host handle? */
3901 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
3902 dev->hw_features |= NETIF_F_TSO;
3903 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
3904 dev->hw_features |= NETIF_F_TSO6;
3905 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
3906 dev->hw_features |= NETIF_F_TSO_ECN;
3907 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
3908 dev->hw_features |= NETIF_F_GSO_UDP_L4;
3910 dev->features |= NETIF_F_GSO_ROBUST;
3913 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
3914 /* (!csum && gso) case will be fixed by register_netdev() */
3916 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
3917 dev->features |= NETIF_F_RXCSUM;
3918 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3919 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
3920 dev->features |= NETIF_F_GRO_HW;
3921 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
3922 dev->hw_features |= NETIF_F_GRO_HW;
3924 dev->vlan_features = dev->features;
3925 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
3927 /* MTU range: 68 - 65535 */
3928 dev->min_mtu = MIN_MTU;
3929 dev->max_mtu = MAX_MTU;
3931 /* Configuration may specify what MAC to use. Otherwise random. */
3932 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
3935 virtio_cread_bytes(vdev,
3936 offsetof(struct virtio_net_config, mac),
3938 eth_hw_addr_set(dev, addr);
3940 eth_hw_addr_random(dev);
3941 dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
3945 /* Set up our device-specific information */
3946 vi = netdev_priv(dev);
3951 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
3952 spin_lock_init(&vi->refill_lock);
3954 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
3955 vi->mergeable_rx_bufs = true;
3956 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
3959 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
3962 vi->tx_max_packets = 0;
3963 vi->rx_max_packets = 0;
3966 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
3967 vi->has_rss_hash_report = true;
3969 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3972 if (vi->has_rss || vi->has_rss_hash_report) {
3973 vi->rss_indir_table_size =
3974 virtio_cread16(vdev, offsetof(struct virtio_net_config,
3975 rss_max_indirection_table_length));
3977 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
3979 vi->rss_hash_types_supported =
3980 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
3981 vi->rss_hash_types_supported &=
3982 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
3983 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
3984 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
3986 dev->hw_features |= NETIF_F_RXHASH;
3989 if (vi->has_rss_hash_report)
3990 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
3991 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
3992 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3993 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3995 vi->hdr_len = sizeof(struct virtio_net_hdr);
3997 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
3998 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3999 vi->any_header_sg = true;
4001 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
4004 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
4005 mtu = virtio_cread16(vdev,
4006 offsetof(struct virtio_net_config,
4008 if (mtu < dev->min_mtu) {
4009 /* Should never trigger: MTU was previously validated
4010 * in virtnet_validate.
4013 "device MTU appears to have changed it is now %d < %d",
4023 virtnet_set_big_packets(vi, mtu);
4025 if (vi->any_header_sg)
4026 dev->needed_headroom = vi->hdr_len;
4028 /* Enable multiqueue by default */
4029 if (num_online_cpus() >= max_queue_pairs)
4030 vi->curr_queue_pairs = max_queue_pairs;
4032 vi->curr_queue_pairs = num_online_cpus();
4033 vi->max_queue_pairs = max_queue_pairs;
4035 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
4041 if (vi->mergeable_rx_bufs)
4042 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
4044 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
4045 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
4047 virtnet_init_settings(dev);
4049 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
4050 vi->failover = net_failover_create(vi->dev);
4051 if (IS_ERR(vi->failover)) {
4052 err = PTR_ERR(vi->failover);
4057 if (vi->has_rss || vi->has_rss_hash_report)
4058 virtnet_init_default_rss(vi);
4060 /* serialize netdev register + virtio_device_ready() with ndo_open() */
4063 err = register_netdevice(dev);
4065 pr_debug("virtio_net: registering device failed\n");
4070 virtio_device_ready(vdev);
4072 /* a random MAC address has been assigned, notify the device.
4073 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
4074 * because many devices work fine without getting MAC explicitly
4076 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
4077 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
4078 struct scatterlist sg;
4080 sg_init_one(&sg, dev->dev_addr, dev->addr_len);
4081 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
4082 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
4083 pr_debug("virtio_net: setting MAC address failed\n");
4086 goto free_unregister_netdev;
4092 err = virtnet_cpu_notif_add(vi);
4094 pr_debug("virtio_net: registering cpu notifier failed\n");
4095 goto free_unregister_netdev;
4098 virtnet_set_queues(vi, vi->curr_queue_pairs);
4100 /* Assume link up if device can't report link status,
4101 otherwise get link status from config. */
4102 netif_carrier_off(dev);
4103 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
4104 schedule_work(&vi->config_work);
4106 vi->status = VIRTIO_NET_S_LINK_UP;
4107 virtnet_update_settings(vi);
4108 netif_carrier_on(dev);
4111 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
4112 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
4113 set_bit(guest_offloads[i], &vi->guest_offloads);
4114 vi->guest_offloads_capable = vi->guest_offloads;
4116 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
4117 dev->name, max_queue_pairs);
4121 free_unregister_netdev:
4122 unregister_netdev(dev);
4124 net_failover_destroy(vi->failover);
4126 virtio_reset_device(vdev);
4127 cancel_delayed_work_sync(&vi->refill);
4128 free_receive_page_frags(vi);
4129 virtnet_del_vqs(vi);
4135 static void remove_vq_common(struct virtnet_info *vi)
4137 virtio_reset_device(vi->vdev);
4139 /* Free unused buffers in both send and recv, if any. */
4140 free_unused_bufs(vi);
4142 free_receive_bufs(vi);
4144 free_receive_page_frags(vi);
4146 virtnet_del_vqs(vi);
4149 static void virtnet_remove(struct virtio_device *vdev)
4151 struct virtnet_info *vi = vdev->priv;
4153 virtnet_cpu_notif_remove(vi);
4155 /* Make sure no work handler is accessing the device. */
4156 flush_work(&vi->config_work);
4158 unregister_netdev(vi->dev);
4160 net_failover_destroy(vi->failover);
4162 remove_vq_common(vi);
4164 free_netdev(vi->dev);
4167 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
4169 struct virtnet_info *vi = vdev->priv;
4171 virtnet_cpu_notif_remove(vi);
4172 virtnet_freeze_down(vdev);
4173 remove_vq_common(vi);
4178 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
4180 struct virtnet_info *vi = vdev->priv;
4183 err = virtnet_restore_up(vdev);
4186 virtnet_set_queues(vi, vi->curr_queue_pairs);
4188 err = virtnet_cpu_notif_add(vi);
4190 virtnet_freeze_down(vdev);
4191 remove_vq_common(vi);
4198 static struct virtio_device_id id_table[] = {
4199 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
4203 #define VIRTNET_FEATURES \
4204 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
4206 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
4207 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
4208 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
4209 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
4210 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
4211 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
4212 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
4213 VIRTIO_NET_F_CTRL_MAC_ADDR, \
4214 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
4215 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
4216 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL
4218 static unsigned int features[] = {
4222 static unsigned int features_legacy[] = {
4225 VIRTIO_F_ANY_LAYOUT,
4228 static struct virtio_driver virtio_net_driver = {
4229 .feature_table = features,
4230 .feature_table_size = ARRAY_SIZE(features),
4231 .feature_table_legacy = features_legacy,
4232 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
4233 .driver.name = KBUILD_MODNAME,
4234 .driver.owner = THIS_MODULE,
4235 .id_table = id_table,
4236 .validate = virtnet_validate,
4237 .probe = virtnet_probe,
4238 .remove = virtnet_remove,
4239 .config_changed = virtnet_config_changed,
4240 #ifdef CONFIG_PM_SLEEP
4241 .freeze = virtnet_freeze,
4242 .restore = virtnet_restore,
4246 static __init int virtio_net_driver_init(void)
4250 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
4252 virtnet_cpu_down_prep);
4255 virtionet_online = ret;
4256 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
4257 NULL, virtnet_cpu_dead);
4260 ret = register_virtio_driver(&virtio_net_driver);
4265 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
4267 cpuhp_remove_multi_state(virtionet_online);
4271 module_init(virtio_net_driver_init);
4273 static __exit void virtio_net_driver_exit(void)
4275 unregister_virtio_driver(&virtio_net_driver);
4276 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
4277 cpuhp_remove_multi_state(virtionet_online);
4279 module_exit(virtio_net_driver_exit);
4281 MODULE_DEVICE_TABLE(virtio, id_table);
4282 MODULE_DESCRIPTION("Virtio network driver");
4283 MODULE_LICENSE("GPL");