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 <linux/dim.h>
23 #include <net/route.h>
25 #include <net/net_failover.h>
26 #include <net/netdev_rx_queue.h>
27 #include <net/netdev_queues.h>
29 static int napi_weight = NAPI_POLL_WEIGHT;
30 module_param(napi_weight, int, 0444);
32 static bool csum = true, gso = true, napi_tx = true;
33 module_param(csum, bool, 0444);
34 module_param(gso, bool, 0444);
35 module_param(napi_tx, bool, 0644);
37 /* FIXME: MTU in config. */
38 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
39 #define GOOD_COPY_LEN 128
41 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
43 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
44 #define VIRTIO_XDP_HEADROOM 256
46 /* Separating two types of XDP xmit */
47 #define VIRTIO_XDP_TX BIT(0)
48 #define VIRTIO_XDP_REDIR BIT(1)
50 #define VIRTIO_XDP_FLAG BIT(0)
52 /* RX packet size EWMA. The average packet size is used to determine the packet
53 * buffer size when refilling RX rings. As the entire RX ring may be refilled
54 * at once, the weight is chosen so that the EWMA will be insensitive to short-
55 * term, transient changes in packet size.
57 DECLARE_EWMA(pkt_len, 0, 64)
59 #define VIRTNET_DRIVER_VERSION "1.0.0"
61 static const unsigned long guest_offloads[] = {
62 VIRTIO_NET_F_GUEST_TSO4,
63 VIRTIO_NET_F_GUEST_TSO6,
64 VIRTIO_NET_F_GUEST_ECN,
65 VIRTIO_NET_F_GUEST_UFO,
66 VIRTIO_NET_F_GUEST_CSUM,
67 VIRTIO_NET_F_GUEST_USO4,
68 VIRTIO_NET_F_GUEST_USO6,
69 VIRTIO_NET_F_GUEST_HDRLEN
72 #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
73 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
74 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
75 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
76 (1ULL << VIRTIO_NET_F_GUEST_USO4) | \
77 (1ULL << VIRTIO_NET_F_GUEST_USO6))
79 struct virtnet_stat_desc {
80 char desc[ETH_GSTRING_LEN];
85 struct virtnet_sq_free_stats {
90 struct virtnet_sq_stats {
91 struct u64_stats_sync syncp;
95 u64_stats_t xdp_tx_drops;
97 u64_stats_t tx_timeouts;
102 struct virtnet_rq_stats {
103 struct u64_stats_sync syncp;
107 u64_stats_t xdp_packets;
109 u64_stats_t xdp_redirects;
110 u64_stats_t xdp_drops;
114 #define VIRTNET_SQ_STAT(name, m) {name, offsetof(struct virtnet_sq_stats, m), -1}
115 #define VIRTNET_RQ_STAT(name, m) {name, offsetof(struct virtnet_rq_stats, m), -1}
117 #define VIRTNET_SQ_STAT_QSTAT(name, m) \
120 offsetof(struct virtnet_sq_stats, m), \
121 offsetof(struct netdev_queue_stats_tx, m), \
124 #define VIRTNET_RQ_STAT_QSTAT(name, m) \
127 offsetof(struct virtnet_rq_stats, m), \
128 offsetof(struct netdev_queue_stats_rx, m), \
131 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
132 VIRTNET_SQ_STAT("xdp_tx", xdp_tx),
133 VIRTNET_SQ_STAT("xdp_tx_drops", xdp_tx_drops),
134 VIRTNET_SQ_STAT("kicks", kicks),
135 VIRTNET_SQ_STAT("tx_timeouts", tx_timeouts),
138 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
139 VIRTNET_RQ_STAT("drops", drops),
140 VIRTNET_RQ_STAT("xdp_packets", xdp_packets),
141 VIRTNET_RQ_STAT("xdp_tx", xdp_tx),
142 VIRTNET_RQ_STAT("xdp_redirects", xdp_redirects),
143 VIRTNET_RQ_STAT("xdp_drops", xdp_drops),
144 VIRTNET_RQ_STAT("kicks", kicks),
147 static const struct virtnet_stat_desc virtnet_sq_stats_desc_qstat[] = {
148 VIRTNET_SQ_STAT_QSTAT("packets", packets),
149 VIRTNET_SQ_STAT_QSTAT("bytes", bytes),
150 VIRTNET_SQ_STAT_QSTAT("stop", stop),
151 VIRTNET_SQ_STAT_QSTAT("wake", wake),
154 static const struct virtnet_stat_desc virtnet_rq_stats_desc_qstat[] = {
155 VIRTNET_RQ_STAT_QSTAT("packets", packets),
156 VIRTNET_RQ_STAT_QSTAT("bytes", bytes),
159 #define VIRTNET_STATS_DESC_CQ(name) \
160 {#name, offsetof(struct virtio_net_stats_cvq, name), -1}
162 #define VIRTNET_STATS_DESC_RX(class, name) \
163 {#name, offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), -1}
165 #define VIRTNET_STATS_DESC_TX(class, name) \
166 {#name, offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), -1}
169 static const struct virtnet_stat_desc virtnet_stats_cvq_desc[] = {
170 VIRTNET_STATS_DESC_CQ(command_num),
171 VIRTNET_STATS_DESC_CQ(ok_num),
174 static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc[] = {
175 VIRTNET_STATS_DESC_RX(basic, packets),
176 VIRTNET_STATS_DESC_RX(basic, bytes),
178 VIRTNET_STATS_DESC_RX(basic, notifications),
179 VIRTNET_STATS_DESC_RX(basic, interrupts),
182 static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc[] = {
183 VIRTNET_STATS_DESC_TX(basic, packets),
184 VIRTNET_STATS_DESC_TX(basic, bytes),
186 VIRTNET_STATS_DESC_TX(basic, notifications),
187 VIRTNET_STATS_DESC_TX(basic, interrupts),
190 static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc[] = {
191 VIRTNET_STATS_DESC_RX(csum, needs_csum),
194 static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc[] = {
195 VIRTNET_STATS_DESC_TX(gso, gso_packets_noseg),
196 VIRTNET_STATS_DESC_TX(gso, gso_bytes_noseg),
199 static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc[] = {
200 VIRTNET_STATS_DESC_RX(speed, ratelimit_bytes),
203 static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc[] = {
204 VIRTNET_STATS_DESC_TX(speed, ratelimit_bytes),
207 #define VIRTNET_STATS_DESC_RX_QSTAT(class, name, qstat_field) \
210 offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), \
211 offsetof(struct netdev_queue_stats_rx, qstat_field), \
214 #define VIRTNET_STATS_DESC_TX_QSTAT(class, name, qstat_field) \
217 offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), \
218 offsetof(struct netdev_queue_stats_tx, qstat_field), \
221 static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc_qstat[] = {
222 VIRTNET_STATS_DESC_RX_QSTAT(basic, drops, hw_drops),
223 VIRTNET_STATS_DESC_RX_QSTAT(basic, drop_overruns, hw_drop_overruns),
226 static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc_qstat[] = {
227 VIRTNET_STATS_DESC_TX_QSTAT(basic, drops, hw_drops),
228 VIRTNET_STATS_DESC_TX_QSTAT(basic, drop_malformed, hw_drop_errors),
231 static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc_qstat[] = {
232 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_valid, csum_unnecessary),
233 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_none, csum_none),
234 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_bad, csum_bad),
237 static const struct virtnet_stat_desc virtnet_stats_tx_csum_desc_qstat[] = {
238 VIRTNET_STATS_DESC_TX_QSTAT(csum, csum_none, csum_none),
239 VIRTNET_STATS_DESC_TX_QSTAT(csum, needs_csum, needs_csum),
242 static const struct virtnet_stat_desc virtnet_stats_rx_gso_desc_qstat[] = {
243 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets, hw_gro_packets),
244 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes, hw_gro_bytes),
245 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets_coalesced, hw_gro_wire_packets),
246 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes_coalesced, hw_gro_wire_bytes),
249 static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc_qstat[] = {
250 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_packets, hw_gso_packets),
251 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_bytes, hw_gso_bytes),
252 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments, hw_gso_wire_packets),
253 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments_bytes, hw_gso_wire_bytes),
256 static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc_qstat[] = {
257 VIRTNET_STATS_DESC_RX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
260 static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = {
261 VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
264 #define VIRTNET_Q_TYPE_RX 0
265 #define VIRTNET_Q_TYPE_TX 1
266 #define VIRTNET_Q_TYPE_CQ 2
268 struct virtnet_interrupt_coalesce {
273 /* The dma information of pages allocated at a time. */
274 struct virtnet_rq_dma {
281 /* Internal representation of a send virtqueue */
283 /* Virtqueue associated with this send _queue */
284 struct virtqueue *vq;
286 /* TX: fragments + linear part + virtio header */
287 struct scatterlist sg[MAX_SKB_FRAGS + 2];
289 /* Name of the send queue: output.$index */
292 struct virtnet_sq_stats stats;
294 struct virtnet_interrupt_coalesce intr_coal;
296 struct napi_struct napi;
298 /* Record whether sq is in reset state. */
302 /* Internal representation of a receive virtqueue */
303 struct receive_queue {
304 /* Virtqueue associated with this receive_queue */
305 struct virtqueue *vq;
307 struct napi_struct napi;
309 struct bpf_prog __rcu *xdp_prog;
311 struct virtnet_rq_stats stats;
313 /* The number of rx notifications */
316 /* Is dynamic interrupt moderation enabled? */
319 /* Used to protect dim_enabled and inter_coal */
320 struct mutex dim_lock;
322 /* Dynamic Interrupt Moderation */
327 struct virtnet_interrupt_coalesce intr_coal;
329 /* Chain pages by the private ptr. */
332 /* Average packet length for mergeable receive buffers. */
333 struct ewma_pkt_len mrg_avg_pkt_len;
335 /* Page frag for packet buffer allocation. */
336 struct page_frag alloc_frag;
338 /* RX: fragments + linear part + virtio header */
339 struct scatterlist sg[MAX_SKB_FRAGS + 2];
341 /* Min single buffer size for mergeable buffers case. */
342 unsigned int min_buf_len;
344 /* Name of this receive queue: input.$index */
347 struct xdp_rxq_info xdp_rxq;
349 /* Record the last dma info to free after new pages is allocated. */
350 struct virtnet_rq_dma *last_dma;
353 /* This structure can contain rss message with maximum settings for indirection table and keysize
354 * Note, that default structure that describes RSS configuration virtio_net_rss_config
355 * contains same info but can't handle table values.
356 * In any case, structure would be passed to virtio hw through sg_buf split by parts
357 * because table sizes may be differ according to the device configuration.
359 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
360 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
361 struct virtio_net_ctrl_rss {
363 u16 indirection_table_mask;
364 u16 unclassified_queue;
365 u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
368 u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
371 /* Control VQ buffers: protected by the rtnl lock */
373 struct virtio_net_ctrl_hdr hdr;
374 virtio_net_ctrl_ack status;
377 struct virtnet_info {
378 struct virtio_device *vdev;
379 struct virtqueue *cvq;
380 struct net_device *dev;
381 struct send_queue *sq;
382 struct receive_queue *rq;
385 /* Max # of queue pairs supported by the device */
388 /* # of queue pairs currently used by the driver */
389 u16 curr_queue_pairs;
391 /* # of XDP queue pairs currently used by the driver */
394 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
397 /* I like... big packets and I cannot lie! */
400 /* number of sg entries allocated for big packets */
401 unsigned int big_packets_num_skbfrags;
403 /* Host will merge rx buffers for big packets (shake it! shake it!) */
404 bool mergeable_rx_bufs;
406 /* Host supports rss and/or hash report */
408 bool has_rss_hash_report;
410 u16 rss_indir_table_size;
411 u32 rss_hash_types_supported;
412 u32 rss_hash_types_saved;
413 struct virtio_net_ctrl_rss rss;
415 /* Has control virtqueue */
418 /* Lock to protect the control VQ */
419 struct mutex cvq_lock;
421 /* Host can handle any s/g split between our header and packet data */
424 /* Packet virtio header size */
427 /* Work struct for delayed refilling if we run low on memory. */
428 struct delayed_work refill;
430 /* Is delayed refill enabled? */
433 /* The lock to synchronize the access to refill_enabled */
434 spinlock_t refill_lock;
436 /* Work struct for config space updates */
437 struct work_struct config_work;
439 /* Work struct for setting rx mode */
440 struct work_struct rx_mode_work;
442 /* OK to queue work setting RX mode? */
443 bool rx_mode_work_enabled;
445 /* Does the affinity hint is set for virtqueues? */
446 bool affinity_hint_set;
448 /* CPU hotplug instances for online & dead */
449 struct hlist_node node;
450 struct hlist_node node_dead;
452 struct control_buf *ctrl;
454 /* Ethtool settings */
458 /* Is rx dynamic interrupt moderation enabled? */
461 /* Interrupt coalescing settings */
462 struct virtnet_interrupt_coalesce intr_coal_tx;
463 struct virtnet_interrupt_coalesce intr_coal_rx;
465 unsigned long guest_offloads;
466 unsigned long guest_offloads_capable;
468 /* failover when STANDBY feature enabled */
469 struct failover *failover;
471 u64 device_stats_cap;
474 struct padded_vnet_hdr {
475 struct virtio_net_hdr_v1_hash hdr;
477 * hdr is in a separate sg buffer, and data sg buffer shares same page
478 * with this header sg. This padding makes next sg 16 byte aligned
484 struct virtio_net_common_hdr {
486 struct virtio_net_hdr hdr;
487 struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
488 struct virtio_net_hdr_v1_hash hash_v1_hdr;
492 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
494 static bool is_xdp_frame(void *ptr)
496 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
499 static void *xdp_to_ptr(struct xdp_frame *ptr)
501 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
504 static struct xdp_frame *ptr_to_xdp(void *ptr)
506 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
509 static void __free_old_xmit(struct send_queue *sq, bool in_napi,
510 struct virtnet_sq_free_stats *stats)
515 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
518 if (!is_xdp_frame(ptr)) {
519 struct sk_buff *skb = ptr;
521 pr_debug("Sent skb %p\n", skb);
523 stats->bytes += skb->len;
524 napi_consume_skb(skb, in_napi);
526 struct xdp_frame *frame = ptr_to_xdp(ptr);
528 stats->bytes += xdp_get_frame_len(frame);
529 xdp_return_frame(frame);
534 /* Converting between virtqueue no. and kernel tx/rx queue no.
535 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
537 static int vq2txq(struct virtqueue *vq)
539 return (vq->index - 1) / 2;
542 static int txq2vq(int txq)
547 static int vq2rxq(struct virtqueue *vq)
549 return vq->index / 2;
552 static int rxq2vq(int rxq)
557 static int vq_type(struct virtnet_info *vi, int qid)
559 if (qid == vi->max_queue_pairs * 2)
560 return VIRTNET_Q_TYPE_CQ;
563 return VIRTNET_Q_TYPE_TX;
565 return VIRTNET_Q_TYPE_RX;
568 static inline struct virtio_net_common_hdr *
569 skb_vnet_common_hdr(struct sk_buff *skb)
571 return (struct virtio_net_common_hdr *)skb->cb;
575 * private is used to chain pages for big packets, put the whole
576 * most recent used list in the beginning for reuse
578 static void give_pages(struct receive_queue *rq, struct page *page)
582 /* Find end of list, sew whole thing into vi->rq.pages. */
583 for (end = page; end->private; end = (struct page *)end->private);
584 end->private = (unsigned long)rq->pages;
588 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
590 struct page *p = rq->pages;
593 rq->pages = (struct page *)p->private;
594 /* clear private here, it is used to chain pages */
597 p = alloc_page(gfp_mask);
601 static void virtnet_rq_free_buf(struct virtnet_info *vi,
602 struct receive_queue *rq, void *buf)
604 if (vi->mergeable_rx_bufs)
605 put_page(virt_to_head_page(buf));
606 else if (vi->big_packets)
609 put_page(virt_to_head_page(buf));
612 static void enable_delayed_refill(struct virtnet_info *vi)
614 spin_lock_bh(&vi->refill_lock);
615 vi->refill_enabled = true;
616 spin_unlock_bh(&vi->refill_lock);
619 static void disable_delayed_refill(struct virtnet_info *vi)
621 spin_lock_bh(&vi->refill_lock);
622 vi->refill_enabled = false;
623 spin_unlock_bh(&vi->refill_lock);
626 static void enable_rx_mode_work(struct virtnet_info *vi)
629 vi->rx_mode_work_enabled = true;
633 static void disable_rx_mode_work(struct virtnet_info *vi)
636 vi->rx_mode_work_enabled = false;
640 static void virtqueue_napi_schedule(struct napi_struct *napi,
641 struct virtqueue *vq)
643 if (napi_schedule_prep(napi)) {
644 virtqueue_disable_cb(vq);
645 __napi_schedule(napi);
649 static bool virtqueue_napi_complete(struct napi_struct *napi,
650 struct virtqueue *vq, int processed)
654 opaque = virtqueue_enable_cb_prepare(vq);
655 if (napi_complete_done(napi, processed)) {
656 if (unlikely(virtqueue_poll(vq, opaque)))
657 virtqueue_napi_schedule(napi, vq);
661 virtqueue_disable_cb(vq);
667 static void skb_xmit_done(struct virtqueue *vq)
669 struct virtnet_info *vi = vq->vdev->priv;
670 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
672 /* Suppress further interrupts. */
673 virtqueue_disable_cb(vq);
676 virtqueue_napi_schedule(napi, vq);
678 /* We were probably waiting for more output buffers. */
679 netif_wake_subqueue(vi->dev, vq2txq(vq));
682 #define MRG_CTX_HEADER_SHIFT 22
683 static void *mergeable_len_to_ctx(unsigned int truesize,
684 unsigned int headroom)
686 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
689 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
691 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
694 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
696 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
699 static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen,
700 unsigned int headroom,
705 skb = build_skb(buf, buflen);
709 skb_reserve(skb, headroom);
715 /* Called from bottom half context */
716 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
717 struct receive_queue *rq,
718 struct page *page, unsigned int offset,
719 unsigned int len, unsigned int truesize,
720 unsigned int headroom)
723 struct virtio_net_common_hdr *hdr;
724 unsigned int copy, hdr_len, hdr_padded_len;
725 struct page *page_to_free = NULL;
726 int tailroom, shinfo_size;
727 char *p, *hdr_p, *buf;
729 p = page_address(page) + offset;
732 hdr_len = vi->hdr_len;
733 if (vi->mergeable_rx_bufs)
734 hdr_padded_len = hdr_len;
736 hdr_padded_len = sizeof(struct padded_vnet_hdr);
740 offset += hdr_padded_len;
742 tailroom = truesize - headroom - hdr_padded_len - len;
744 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
746 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
747 skb = virtnet_build_skb(buf, truesize, p - buf, len);
751 page = (struct page *)page->private;
753 give_pages(rq, page);
757 /* copy small packet so we can reuse these pages for small data */
758 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
762 /* Copy all frame if it fits skb->head, otherwise
763 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
765 if (len <= skb_tailroom(skb))
769 skb_put_data(skb, p, copy);
774 if (vi->mergeable_rx_bufs) {
776 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
783 * Verify that we can indeed put this data into a skb.
784 * This is here to handle cases when the device erroneously
785 * tries to receive more than is possible. This is usually
786 * the case of a broken device.
788 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
789 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
793 BUG_ON(offset >= PAGE_SIZE);
795 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
796 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
797 frag_size, truesize);
799 page = (struct page *)page->private;
804 give_pages(rq, page);
807 hdr = skb_vnet_common_hdr(skb);
808 memcpy(hdr, hdr_p, hdr_len);
810 put_page(page_to_free);
815 static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
817 struct page *page = virt_to_head_page(buf);
818 struct virtnet_rq_dma *dma;
822 head = page_address(page);
828 if (dma->need_sync && len) {
829 offset = buf - (head + sizeof(*dma));
831 virtqueue_dma_sync_single_range_for_cpu(rq->vq, dma->addr,
839 virtqueue_dma_unmap_single_attrs(rq->vq, dma->addr, dma->len,
840 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
844 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
848 buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
850 virtnet_rq_unmap(rq, buf, *len);
855 static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
857 struct virtnet_rq_dma *dma;
862 head = page_address(rq->alloc_frag.page);
868 addr = dma->addr - sizeof(*dma) + offset;
870 sg_init_table(rq->sg, 1);
871 rq->sg[0].dma_address = addr;
872 rq->sg[0].length = len;
875 static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
877 struct page_frag *alloc_frag = &rq->alloc_frag;
878 struct virtnet_rq_dma *dma;
882 if (unlikely(!skb_page_frag_refill(size, alloc_frag, gfp)))
885 head = page_address(alloc_frag->page);
890 if (!alloc_frag->offset) {
892 /* Now, the new page is allocated, the last dma
893 * will not be used. So the dma can be unmapped
896 virtnet_rq_unmap(rq, rq->last_dma, 0);
900 dma->len = alloc_frag->size - sizeof(*dma);
902 addr = virtqueue_dma_map_single_attrs(rq->vq, dma + 1,
903 dma->len, DMA_FROM_DEVICE, 0);
904 if (virtqueue_dma_mapping_error(rq->vq, addr))
908 dma->need_sync = virtqueue_dma_need_sync(rq->vq, addr);
910 /* Add a reference to dma to prevent the entire dma from
911 * being released during error handling. This reference
912 * will be freed after the pages are no longer used.
914 get_page(alloc_frag->page);
916 alloc_frag->offset = sizeof(*dma);
923 buf = head + alloc_frag->offset;
925 get_page(alloc_frag->page);
926 alloc_frag->offset += size;
931 static void virtnet_rq_set_premapped(struct virtnet_info *vi)
935 /* disable for big mode */
936 if (!vi->mergeable_rx_bufs && vi->big_packets)
939 for (i = 0; i < vi->max_queue_pairs; i++)
940 /* error should never happen */
941 BUG_ON(virtqueue_set_dma_premapped(vi->rq[i].vq));
944 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
946 struct virtnet_info *vi = vq->vdev->priv;
947 struct receive_queue *rq;
952 if (!vi->big_packets || vi->mergeable_rx_bufs)
953 virtnet_rq_unmap(rq, buf, 0);
955 virtnet_rq_free_buf(vi, rq, buf);
958 static void free_old_xmit(struct send_queue *sq, bool in_napi)
960 struct virtnet_sq_free_stats stats = {0};
962 __free_old_xmit(sq, in_napi, &stats);
964 /* Avoid overhead when no packets have been processed
965 * happens when called speculatively from start_xmit.
970 u64_stats_update_begin(&sq->stats.syncp);
971 u64_stats_add(&sq->stats.bytes, stats.bytes);
972 u64_stats_add(&sq->stats.packets, stats.packets);
973 u64_stats_update_end(&sq->stats.syncp);
976 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
978 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
980 else if (q < vi->curr_queue_pairs)
986 static void check_sq_full_and_disable(struct virtnet_info *vi,
987 struct net_device *dev,
988 struct send_queue *sq)
990 bool use_napi = sq->napi.weight;
995 /* If running out of space, stop queue to avoid getting packets that we
996 * are then unable to transmit.
997 * An alternative would be to force queuing layer to requeue the skb by
998 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
999 * returned in a normal path of operation: it means that driver is not
1000 * maintaining the TX queue stop/start state properly, and causes
1001 * the stack to do a non-trivial amount of useless work.
1002 * Since most packets only take 1 or 2 ring slots, stopping the queue
1003 * early means 16 slots are typically wasted.
1005 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1006 netif_stop_subqueue(dev, qnum);
1007 u64_stats_update_begin(&sq->stats.syncp);
1008 u64_stats_inc(&sq->stats.stop);
1009 u64_stats_update_end(&sq->stats.syncp);
1011 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
1012 virtqueue_napi_schedule(&sq->napi, sq->vq);
1013 } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1014 /* More just got used, free them then recheck. */
1015 free_old_xmit(sq, false);
1016 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1017 netif_start_subqueue(dev, qnum);
1018 u64_stats_update_begin(&sq->stats.syncp);
1019 u64_stats_inc(&sq->stats.wake);
1020 u64_stats_update_end(&sq->stats.syncp);
1021 virtqueue_disable_cb(sq->vq);
1027 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
1028 struct send_queue *sq,
1029 struct xdp_frame *xdpf)
1031 struct virtio_net_hdr_mrg_rxbuf *hdr;
1032 struct skb_shared_info *shinfo;
1036 if (unlikely(xdpf->headroom < vi->hdr_len))
1039 if (unlikely(xdp_frame_has_frags(xdpf))) {
1040 shinfo = xdp_get_shared_info_from_frame(xdpf);
1041 nr_frags = shinfo->nr_frags;
1044 /* In wrapping function virtnet_xdp_xmit(), we need to free
1045 * up the pending old buffers, where we need to calculate the
1046 * position of skb_shared_info in xdp_get_frame_len() and
1047 * xdp_return_frame(), which will involve to xdpf->data and
1048 * xdpf->headroom. Therefore, we need to update the value of
1049 * headroom synchronously here.
1051 xdpf->headroom -= vi->hdr_len;
1052 xdpf->data -= vi->hdr_len;
1053 /* Zero header and leave csum up to XDP layers */
1055 memset(hdr, 0, vi->hdr_len);
1056 xdpf->len += vi->hdr_len;
1058 sg_init_table(sq->sg, nr_frags + 1);
1059 sg_set_buf(sq->sg, xdpf->data, xdpf->len);
1060 for (i = 0; i < nr_frags; i++) {
1061 skb_frag_t *frag = &shinfo->frags[i];
1063 sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
1064 skb_frag_size(frag), skb_frag_off(frag));
1067 err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
1068 xdp_to_ptr(xdpf), GFP_ATOMIC);
1070 return -ENOSPC; /* Caller handle free/refcnt */
1075 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
1076 * the current cpu, so it does not need to be locked.
1078 * Here we use marco instead of inline functions because we have to deal with
1079 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
1080 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
1081 * functions to perfectly solve these three problems at the same time.
1083 #define virtnet_xdp_get_sq(vi) ({ \
1084 int cpu = smp_processor_id(); \
1085 struct netdev_queue *txq; \
1086 typeof(vi) v = (vi); \
1089 if (v->curr_queue_pairs > nr_cpu_ids) { \
1090 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
1092 txq = netdev_get_tx_queue(v->dev, qp); \
1093 __netif_tx_acquire(txq); \
1095 qp = cpu % v->curr_queue_pairs; \
1096 txq = netdev_get_tx_queue(v->dev, qp); \
1097 __netif_tx_lock(txq, cpu); \
1102 #define virtnet_xdp_put_sq(vi, q) { \
1103 struct netdev_queue *txq; \
1104 typeof(vi) v = (vi); \
1106 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
1107 if (v->curr_queue_pairs > nr_cpu_ids) \
1108 __netif_tx_release(txq); \
1110 __netif_tx_unlock(txq); \
1113 static int virtnet_xdp_xmit(struct net_device *dev,
1114 int n, struct xdp_frame **frames, u32 flags)
1116 struct virtnet_info *vi = netdev_priv(dev);
1117 struct virtnet_sq_free_stats stats = {0};
1118 struct receive_queue *rq = vi->rq;
1119 struct bpf_prog *xdp_prog;
1120 struct send_queue *sq;
1126 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
1127 * indicate XDP resources have been successfully allocated.
1129 xdp_prog = rcu_access_pointer(rq->xdp_prog);
1133 sq = virtnet_xdp_get_sq(vi);
1135 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
1140 /* Free up any pending old buffers before queueing new ones. */
1141 __free_old_xmit(sq, false, &stats);
1143 for (i = 0; i < n; i++) {
1144 struct xdp_frame *xdpf = frames[i];
1146 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
1152 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
1153 check_sq_full_and_disable(vi, dev, sq);
1155 if (flags & XDP_XMIT_FLUSH) {
1156 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
1160 u64_stats_update_begin(&sq->stats.syncp);
1161 u64_stats_add(&sq->stats.bytes, stats.bytes);
1162 u64_stats_add(&sq->stats.packets, stats.packets);
1163 u64_stats_add(&sq->stats.xdp_tx, n);
1164 u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit);
1165 u64_stats_add(&sq->stats.kicks, kicks);
1166 u64_stats_update_end(&sq->stats.syncp);
1168 virtnet_xdp_put_sq(vi, sq);
1172 static void put_xdp_frags(struct xdp_buff *xdp)
1174 struct skb_shared_info *shinfo;
1175 struct page *xdp_page;
1178 if (xdp_buff_has_frags(xdp)) {
1179 shinfo = xdp_get_shared_info_from_buff(xdp);
1180 for (i = 0; i < shinfo->nr_frags; i++) {
1181 xdp_page = skb_frag_page(&shinfo->frags[i]);
1187 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
1188 struct net_device *dev,
1189 unsigned int *xdp_xmit,
1190 struct virtnet_rq_stats *stats)
1192 struct xdp_frame *xdpf;
1196 act = bpf_prog_run_xdp(xdp_prog, xdp);
1197 u64_stats_inc(&stats->xdp_packets);
1204 u64_stats_inc(&stats->xdp_tx);
1205 xdpf = xdp_convert_buff_to_frame(xdp);
1206 if (unlikely(!xdpf)) {
1207 netdev_dbg(dev, "convert buff to frame failed for xdp\n");
1211 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1212 if (unlikely(!err)) {
1213 xdp_return_frame_rx_napi(xdpf);
1214 } else if (unlikely(err < 0)) {
1215 trace_xdp_exception(dev, xdp_prog, act);
1218 *xdp_xmit |= VIRTIO_XDP_TX;
1222 u64_stats_inc(&stats->xdp_redirects);
1223 err = xdp_do_redirect(dev, xdp, xdp_prog);
1227 *xdp_xmit |= VIRTIO_XDP_REDIR;
1231 bpf_warn_invalid_xdp_action(dev, xdp_prog, act);
1234 trace_xdp_exception(dev, xdp_prog, act);
1241 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
1243 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
1246 /* We copy the packet for XDP in the following cases:
1248 * 1) Packet is scattered across multiple rx buffers.
1249 * 2) Headroom space is insufficient.
1251 * This is inefficient but it's a temporary condition that
1252 * we hit right after XDP is enabled and until queue is refilled
1253 * with large buffers with sufficient headroom - so it should affect
1254 * at most queue size packets.
1255 * Afterwards, the conditions to enable
1256 * XDP should preclude the underlying device from sending packets
1257 * across multiple buffers (num_buf > 1), and we make sure buffers
1258 * have enough headroom.
1260 static struct page *xdp_linearize_page(struct receive_queue *rq,
1267 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1270 if (page_off + *len + tailroom > PAGE_SIZE)
1273 page = alloc_page(GFP_ATOMIC);
1277 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
1280 while (--*num_buf) {
1281 unsigned int buflen;
1285 buf = virtnet_rq_get_buf(rq, &buflen, NULL);
1289 p = virt_to_head_page(buf);
1290 off = buf - page_address(p);
1292 /* guard against a misconfigured or uncooperative backend that
1293 * is sending packet larger than the MTU.
1295 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
1300 memcpy(page_address(page) + page_off,
1301 page_address(p) + off, buflen);
1306 /* Headroom does not contribute to packet length */
1307 *len = page_off - VIRTIO_XDP_HEADROOM;
1310 __free_pages(page, 0);
1314 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
1315 unsigned int xdp_headroom,
1319 unsigned int header_offset;
1320 unsigned int headroom;
1321 unsigned int buflen;
1322 struct sk_buff *skb;
1324 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1325 headroom = vi->hdr_len + header_offset;
1326 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1327 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1329 skb = virtnet_build_skb(buf, buflen, headroom, len);
1333 buf += header_offset;
1334 memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
1339 static struct sk_buff *receive_small_xdp(struct net_device *dev,
1340 struct virtnet_info *vi,
1341 struct receive_queue *rq,
1342 struct bpf_prog *xdp_prog,
1344 unsigned int xdp_headroom,
1346 unsigned int *xdp_xmit,
1347 struct virtnet_rq_stats *stats)
1349 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
1350 unsigned int headroom = vi->hdr_len + header_offset;
1351 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
1352 struct page *page = virt_to_head_page(buf);
1353 struct page *xdp_page;
1354 unsigned int buflen;
1355 struct xdp_buff xdp;
1356 struct sk_buff *skb;
1357 unsigned int metasize = 0;
1360 if (unlikely(hdr->hdr.gso_type))
1363 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1364 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1366 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
1367 int offset = buf - page_address(page) + header_offset;
1368 unsigned int tlen = len + vi->hdr_len;
1371 xdp_headroom = virtnet_get_headroom(vi);
1372 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1373 headroom = vi->hdr_len + header_offset;
1374 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1375 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1376 xdp_page = xdp_linearize_page(rq, &num_buf, page,
1377 offset, header_offset,
1382 buf = page_address(xdp_page);
1387 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
1388 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
1389 xdp_headroom, len, true);
1391 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1395 /* Recalculate length in case bpf program changed it */
1396 len = xdp.data_end - xdp.data;
1397 metasize = xdp.data - xdp.data_meta;
1408 skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len);
1413 skb_metadata_set(skb, metasize);
1418 u64_stats_inc(&stats->xdp_drops);
1420 u64_stats_inc(&stats->drops);
1426 static struct sk_buff *receive_small(struct net_device *dev,
1427 struct virtnet_info *vi,
1428 struct receive_queue *rq,
1429 void *buf, void *ctx,
1431 unsigned int *xdp_xmit,
1432 struct virtnet_rq_stats *stats)
1434 unsigned int xdp_headroom = (unsigned long)ctx;
1435 struct page *page = virt_to_head_page(buf);
1436 struct sk_buff *skb;
1439 u64_stats_add(&stats->bytes, len);
1441 if (unlikely(len > GOOD_PACKET_LEN)) {
1442 pr_debug("%s: rx error: len %u exceeds max size %d\n",
1443 dev->name, len, GOOD_PACKET_LEN);
1444 DEV_STATS_INC(dev, rx_length_errors);
1448 if (unlikely(vi->xdp_enabled)) {
1449 struct bpf_prog *xdp_prog;
1452 xdp_prog = rcu_dereference(rq->xdp_prog);
1454 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf,
1455 xdp_headroom, len, xdp_xmit,
1463 skb = receive_small_build_skb(vi, xdp_headroom, buf, len);
1468 u64_stats_inc(&stats->drops);
1473 static struct sk_buff *receive_big(struct net_device *dev,
1474 struct virtnet_info *vi,
1475 struct receive_queue *rq,
1478 struct virtnet_rq_stats *stats)
1480 struct page *page = buf;
1481 struct sk_buff *skb =
1482 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0);
1484 u64_stats_add(&stats->bytes, len - vi->hdr_len);
1491 u64_stats_inc(&stats->drops);
1492 give_pages(rq, page);
1496 static void mergeable_buf_free(struct receive_queue *rq, int num_buf,
1497 struct net_device *dev,
1498 struct virtnet_rq_stats *stats)
1504 while (num_buf-- > 1) {
1505 buf = virtnet_rq_get_buf(rq, &len, NULL);
1506 if (unlikely(!buf)) {
1507 pr_debug("%s: rx error: %d buffers missing\n",
1508 dev->name, num_buf);
1509 DEV_STATS_INC(dev, rx_length_errors);
1512 u64_stats_add(&stats->bytes, len);
1513 page = virt_to_head_page(buf);
1518 /* Why not use xdp_build_skb_from_frame() ?
1519 * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
1520 * virtio-net there are 2 points that do not match its requirements:
1521 * 1. The size of the prefilled buffer is not fixed before xdp is set.
1522 * 2. xdp_build_skb_from_frame() does more checks that we don't need,
1523 * like eth_type_trans() (which virtio-net does in receive_buf()).
1525 static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
1526 struct virtnet_info *vi,
1527 struct xdp_buff *xdp,
1528 unsigned int xdp_frags_truesz)
1530 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
1531 unsigned int headroom, data_len;
1532 struct sk_buff *skb;
1536 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
1537 pr_debug("Error building skb as missing reserved tailroom for xdp");
1541 if (unlikely(xdp_buff_has_frags(xdp)))
1542 nr_frags = sinfo->nr_frags;
1544 skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
1548 headroom = xdp->data - xdp->data_hard_start;
1549 data_len = xdp->data_end - xdp->data;
1550 skb_reserve(skb, headroom);
1551 __skb_put(skb, data_len);
1553 metasize = xdp->data - xdp->data_meta;
1554 metasize = metasize > 0 ? metasize : 0;
1556 skb_metadata_set(skb, metasize);
1558 if (unlikely(xdp_buff_has_frags(xdp)))
1559 xdp_update_skb_shared_info(skb, nr_frags,
1560 sinfo->xdp_frags_size,
1562 xdp_buff_is_frag_pfmemalloc(xdp));
1567 /* TODO: build xdp in big mode */
1568 static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
1569 struct virtnet_info *vi,
1570 struct receive_queue *rq,
1571 struct xdp_buff *xdp,
1574 unsigned int frame_sz,
1576 unsigned int *xdp_frags_truesize,
1577 struct virtnet_rq_stats *stats)
1579 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1580 unsigned int headroom, tailroom, room;
1581 unsigned int truesize, cur_frag_size;
1582 struct skb_shared_info *shinfo;
1583 unsigned int xdp_frags_truesz = 0;
1589 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
1590 xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM,
1591 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
1597 /* If we want to build multi-buffer xdp, we need
1598 * to specify that the flags of xdp_buff have the
1599 * XDP_FLAGS_HAS_FRAG bit.
1601 if (!xdp_buff_has_frags(xdp))
1602 xdp_buff_set_frags_flag(xdp);
1604 shinfo = xdp_get_shared_info_from_buff(xdp);
1605 shinfo->nr_frags = 0;
1606 shinfo->xdp_frags_size = 0;
1609 if (*num_buf > MAX_SKB_FRAGS + 1)
1612 while (--*num_buf > 0) {
1613 buf = virtnet_rq_get_buf(rq, &len, &ctx);
1614 if (unlikely(!buf)) {
1615 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1616 dev->name, *num_buf,
1617 virtio16_to_cpu(vi->vdev, hdr->num_buffers));
1618 DEV_STATS_INC(dev, rx_length_errors);
1622 u64_stats_add(&stats->bytes, len);
1623 page = virt_to_head_page(buf);
1624 offset = buf - page_address(page);
1626 truesize = mergeable_ctx_to_truesize(ctx);
1627 headroom = mergeable_ctx_to_headroom(ctx);
1628 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1629 room = SKB_DATA_ALIGN(headroom + tailroom);
1631 cur_frag_size = truesize;
1632 xdp_frags_truesz += cur_frag_size;
1633 if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) {
1635 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1636 dev->name, len, (unsigned long)(truesize - room));
1637 DEV_STATS_INC(dev, rx_length_errors);
1641 frag = &shinfo->frags[shinfo->nr_frags++];
1642 skb_frag_fill_page_desc(frag, page, offset, len);
1643 if (page_is_pfmemalloc(page))
1644 xdp_buff_set_frag_pfmemalloc(xdp);
1646 shinfo->xdp_frags_size += len;
1649 *xdp_frags_truesize = xdp_frags_truesz;
1657 static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
1658 struct receive_queue *rq,
1659 struct bpf_prog *xdp_prog,
1661 unsigned int *frame_sz,
1666 struct virtio_net_hdr_mrg_rxbuf *hdr)
1668 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
1669 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1670 struct page *xdp_page;
1671 unsigned int xdp_room;
1673 /* Transient failure which in theory could occur if
1674 * in-flight packets from before XDP was enabled reach
1675 * the receive path after XDP is loaded.
1677 if (unlikely(hdr->hdr.gso_type))
1680 /* Now XDP core assumes frag size is PAGE_SIZE, but buffers
1681 * with headroom may add hole in truesize, which
1682 * make their length exceed PAGE_SIZE. So we disabled the
1683 * hole mechanism for xdp. See add_recvbuf_mergeable().
1685 *frame_sz = truesize;
1687 if (likely(headroom >= virtnet_get_headroom(vi) &&
1688 (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) {
1689 return page_address(*page) + offset;
1692 /* This happens when headroom is not enough because
1693 * of the buffer was prefilled before XDP is set.
1694 * This should only happen for the first several packets.
1695 * In fact, vq reset can be used here to help us clean up
1696 * the prefilled buffers, but many existing devices do not
1697 * support it, and we don't want to bother users who are
1698 * using xdp normally.
1700 if (!xdp_prog->aux->xdp_has_frags) {
1701 /* linearize data for XDP */
1702 xdp_page = xdp_linearize_page(rq, num_buf,
1704 VIRTIO_XDP_HEADROOM,
1709 xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
1710 sizeof(struct skb_shared_info));
1711 if (*len + xdp_room > PAGE_SIZE)
1714 xdp_page = alloc_page(GFP_ATOMIC);
1718 memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM,
1719 page_address(*page) + offset, *len);
1722 *frame_sz = PAGE_SIZE;
1728 return page_address(*page) + VIRTIO_XDP_HEADROOM;
1731 static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
1732 struct virtnet_info *vi,
1733 struct receive_queue *rq,
1734 struct bpf_prog *xdp_prog,
1738 unsigned int *xdp_xmit,
1739 struct virtnet_rq_stats *stats)
1741 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1742 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1743 struct page *page = virt_to_head_page(buf);
1744 int offset = buf - page_address(page);
1745 unsigned int xdp_frags_truesz = 0;
1746 struct sk_buff *head_skb;
1747 unsigned int frame_sz;
1748 struct xdp_buff xdp;
1753 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page,
1755 if (unlikely(!data))
1758 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
1759 &num_buf, &xdp_frags_truesz, stats);
1763 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1767 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
1768 if (unlikely(!head_skb))
1780 put_xdp_frags(&xdp);
1784 mergeable_buf_free(rq, num_buf, dev, stats);
1786 u64_stats_inc(&stats->xdp_drops);
1787 u64_stats_inc(&stats->drops);
1791 static struct sk_buff *receive_mergeable(struct net_device *dev,
1792 struct virtnet_info *vi,
1793 struct receive_queue *rq,
1797 unsigned int *xdp_xmit,
1798 struct virtnet_rq_stats *stats)
1800 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1801 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1802 struct page *page = virt_to_head_page(buf);
1803 int offset = buf - page_address(page);
1804 struct sk_buff *head_skb, *curr_skb;
1805 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
1806 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1807 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1808 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1811 u64_stats_add(&stats->bytes, len - vi->hdr_len);
1813 if (unlikely(len > truesize - room)) {
1814 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1815 dev->name, len, (unsigned long)(truesize - room));
1816 DEV_STATS_INC(dev, rx_length_errors);
1820 if (unlikely(vi->xdp_enabled)) {
1821 struct bpf_prog *xdp_prog;
1824 xdp_prog = rcu_dereference(rq->xdp_prog);
1826 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx,
1827 len, xdp_xmit, stats);
1834 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom);
1835 curr_skb = head_skb;
1837 if (unlikely(!curr_skb))
1842 buf = virtnet_rq_get_buf(rq, &len, &ctx);
1843 if (unlikely(!buf)) {
1844 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1846 virtio16_to_cpu(vi->vdev,
1848 DEV_STATS_INC(dev, rx_length_errors);
1852 u64_stats_add(&stats->bytes, len);
1853 page = virt_to_head_page(buf);
1855 truesize = mergeable_ctx_to_truesize(ctx);
1856 headroom = mergeable_ctx_to_headroom(ctx);
1857 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1858 room = SKB_DATA_ALIGN(headroom + tailroom);
1859 if (unlikely(len > truesize - room)) {
1860 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1861 dev->name, len, (unsigned long)(truesize - room));
1862 DEV_STATS_INC(dev, rx_length_errors);
1866 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
1867 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1868 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
1870 if (unlikely(!nskb))
1872 if (curr_skb == head_skb)
1873 skb_shinfo(curr_skb)->frag_list = nskb;
1875 curr_skb->next = nskb;
1877 head_skb->truesize += nskb->truesize;
1880 if (curr_skb != head_skb) {
1881 head_skb->data_len += len;
1882 head_skb->len += len;
1883 head_skb->truesize += truesize;
1885 offset = buf - page_address(page);
1886 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1888 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1891 skb_add_rx_frag(curr_skb, num_skb_frags, page,
1892 offset, len, truesize);
1896 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
1901 mergeable_buf_free(rq, num_buf, dev, stats);
1904 u64_stats_inc(&stats->drops);
1905 dev_kfree_skb(head_skb);
1909 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
1910 struct sk_buff *skb)
1912 enum pkt_hash_types rss_hash_type;
1914 if (!hdr_hash || !skb)
1917 switch (__le16_to_cpu(hdr_hash->hash_report)) {
1918 case VIRTIO_NET_HASH_REPORT_TCPv4:
1919 case VIRTIO_NET_HASH_REPORT_UDPv4:
1920 case VIRTIO_NET_HASH_REPORT_TCPv6:
1921 case VIRTIO_NET_HASH_REPORT_UDPv6:
1922 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
1923 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
1924 rss_hash_type = PKT_HASH_TYPE_L4;
1926 case VIRTIO_NET_HASH_REPORT_IPv4:
1927 case VIRTIO_NET_HASH_REPORT_IPv6:
1928 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
1929 rss_hash_type = PKT_HASH_TYPE_L3;
1931 case VIRTIO_NET_HASH_REPORT_NONE:
1933 rss_hash_type = PKT_HASH_TYPE_NONE;
1935 skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
1938 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1939 void *buf, unsigned int len, void **ctx,
1940 unsigned int *xdp_xmit,
1941 struct virtnet_rq_stats *stats)
1943 struct net_device *dev = vi->dev;
1944 struct sk_buff *skb;
1945 struct virtio_net_common_hdr *hdr;
1947 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
1948 pr_debug("%s: short packet %i\n", dev->name, len);
1949 DEV_STATS_INC(dev, rx_length_errors);
1950 virtnet_rq_free_buf(vi, rq, buf);
1954 if (vi->mergeable_rx_bufs)
1955 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1957 else if (vi->big_packets)
1958 skb = receive_big(dev, vi, rq, buf, len, stats);
1960 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1965 hdr = skb_vnet_common_hdr(skb);
1966 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
1967 virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
1969 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
1970 skb->ip_summed = CHECKSUM_UNNECESSARY;
1972 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1973 virtio_is_little_endian(vi->vdev))) {
1974 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1975 dev->name, hdr->hdr.gso_type,
1980 skb_record_rx_queue(skb, vq2rxq(rq->vq));
1981 skb->protocol = eth_type_trans(skb, dev);
1982 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1983 ntohs(skb->protocol), skb->len, skb->pkt_type);
1985 napi_gro_receive(&rq->napi, skb);
1989 DEV_STATS_INC(dev, rx_frame_errors);
1993 /* Unlike mergeable buffers, all buffers are allocated to the
1994 * same size, except for the headroom. For this reason we do
1995 * not need to use mergeable_len_to_ctx here - it is enough
1996 * to store the headroom as the context ignoring the truesize.
1998 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
2002 unsigned int xdp_headroom = virtnet_get_headroom(vi);
2003 void *ctx = (void *)(unsigned long)xdp_headroom;
2004 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
2007 len = SKB_DATA_ALIGN(len) +
2008 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2010 buf = virtnet_rq_alloc(rq, len, gfp);
2014 virtnet_rq_init_one_sg(rq, buf + VIRTNET_RX_PAD + xdp_headroom,
2015 vi->hdr_len + GOOD_PACKET_LEN);
2017 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
2019 virtnet_rq_unmap(rq, buf, 0);
2020 put_page(virt_to_head_page(buf));
2026 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
2029 struct page *first, *list = NULL;
2033 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
2035 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
2036 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
2037 first = get_a_page(rq, gfp);
2040 give_pages(rq, list);
2043 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
2045 /* chain new page in list head to match sg */
2046 first->private = (unsigned long)list;
2050 first = get_a_page(rq, gfp);
2052 give_pages(rq, list);
2055 p = page_address(first);
2057 /* rq->sg[0], rq->sg[1] share the same page */
2058 /* a separated rq->sg[0] for header - required in case !any_header_sg */
2059 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
2061 /* rq->sg[1] for data packet, from offset */
2062 offset = sizeof(struct padded_vnet_hdr);
2063 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
2065 /* chain first in list head */
2066 first->private = (unsigned long)list;
2067 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
2070 give_pages(rq, first);
2075 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
2076 struct ewma_pkt_len *avg_pkt_len,
2079 struct virtnet_info *vi = rq->vq->vdev->priv;
2080 const size_t hdr_len = vi->hdr_len;
2084 return PAGE_SIZE - room;
2086 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
2087 rq->min_buf_len, PAGE_SIZE - hdr_len);
2089 return ALIGN(len, L1_CACHE_BYTES);
2092 static int add_recvbuf_mergeable(struct virtnet_info *vi,
2093 struct receive_queue *rq, gfp_t gfp)
2095 struct page_frag *alloc_frag = &rq->alloc_frag;
2096 unsigned int headroom = virtnet_get_headroom(vi);
2097 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
2098 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
2099 unsigned int len, hole;
2104 /* Extra tailroom is needed to satisfy XDP's assumption. This
2105 * means rx frags coalescing won't work, but consider we've
2106 * disabled GSO for XDP, it won't be a big issue.
2108 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
2110 buf = virtnet_rq_alloc(rq, len + room, gfp);
2114 buf += headroom; /* advance address leaving hole at front of pkt */
2115 hole = alloc_frag->size - alloc_frag->offset;
2116 if (hole < len + room) {
2117 /* To avoid internal fragmentation, if there is very likely not
2118 * enough space for another buffer, add the remaining space to
2119 * the current buffer.
2120 * XDP core assumes that frame_size of xdp_buff and the length
2121 * of the frag are PAGE_SIZE, so we disable the hole mechanism.
2125 alloc_frag->offset += hole;
2128 virtnet_rq_init_one_sg(rq, buf, len);
2130 ctx = mergeable_len_to_ctx(len + room, headroom);
2131 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
2133 virtnet_rq_unmap(rq, buf, 0);
2134 put_page(virt_to_head_page(buf));
2141 * Returns false if we couldn't fill entirely (OOM).
2143 * Normally run in the receive path, but can also be run from ndo_open
2144 * before we're receiving packets, or from refill_work which is
2145 * careful to disable receiving (using napi_disable).
2147 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
2154 if (vi->mergeable_rx_bufs)
2155 err = add_recvbuf_mergeable(vi, rq, gfp);
2156 else if (vi->big_packets)
2157 err = add_recvbuf_big(vi, rq, gfp);
2159 err = add_recvbuf_small(vi, rq, gfp);
2161 oom = err == -ENOMEM;
2164 } while (rq->vq->num_free);
2165 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
2166 unsigned long flags;
2168 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
2169 u64_stats_inc(&rq->stats.kicks);
2170 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
2176 static void skb_recv_done(struct virtqueue *rvq)
2178 struct virtnet_info *vi = rvq->vdev->priv;
2179 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
2182 virtqueue_napi_schedule(&rq->napi, rvq);
2185 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
2189 /* If all buffers were filled by other side before we napi_enabled, we
2190 * won't get another interrupt, so process any outstanding packets now.
2191 * Call local_bh_enable after to trigger softIRQ processing.
2194 virtqueue_napi_schedule(napi, vq);
2198 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
2199 struct virtqueue *vq,
2200 struct napi_struct *napi)
2205 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
2206 * enable the feature if this is likely affine with the transmit path.
2208 if (!vi->affinity_hint_set) {
2213 return virtnet_napi_enable(vq, napi);
2216 static void virtnet_napi_tx_disable(struct napi_struct *napi)
2222 static void refill_work(struct work_struct *work)
2224 struct virtnet_info *vi =
2225 container_of(work, struct virtnet_info, refill.work);
2229 for (i = 0; i < vi->curr_queue_pairs; i++) {
2230 struct receive_queue *rq = &vi->rq[i];
2232 napi_disable(&rq->napi);
2233 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
2234 virtnet_napi_enable(rq->vq, &rq->napi);
2236 /* In theory, this can happen: if we don't get any buffers in
2237 * we will *never* try to fill again.
2240 schedule_delayed_work(&vi->refill, HZ/2);
2244 static int virtnet_receive(struct receive_queue *rq, int budget,
2245 unsigned int *xdp_xmit)
2247 struct virtnet_info *vi = rq->vq->vdev->priv;
2248 struct virtnet_rq_stats stats = {};
2254 if (!vi->big_packets || vi->mergeable_rx_bufs) {
2257 while (packets < budget &&
2258 (buf = virtnet_rq_get_buf(rq, &len, &ctx))) {
2259 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
2263 while (packets < budget &&
2264 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
2265 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
2270 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
2271 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
2272 spin_lock(&vi->refill_lock);
2273 if (vi->refill_enabled)
2274 schedule_delayed_work(&vi->refill, 0);
2275 spin_unlock(&vi->refill_lock);
2279 u64_stats_set(&stats.packets, packets);
2280 u64_stats_update_begin(&rq->stats.syncp);
2281 for (i = 0; i < ARRAY_SIZE(virtnet_rq_stats_desc); i++) {
2282 size_t offset = virtnet_rq_stats_desc[i].offset;
2283 u64_stats_t *item, *src;
2285 item = (u64_stats_t *)((u8 *)&rq->stats + offset);
2286 src = (u64_stats_t *)((u8 *)&stats + offset);
2287 u64_stats_add(item, u64_stats_read(src));
2290 u64_stats_add(&rq->stats.packets, u64_stats_read(&stats.packets));
2291 u64_stats_add(&rq->stats.bytes, u64_stats_read(&stats.bytes));
2293 u64_stats_update_end(&rq->stats.syncp);
2298 static void virtnet_poll_cleantx(struct receive_queue *rq)
2300 struct virtnet_info *vi = rq->vq->vdev->priv;
2301 unsigned int index = vq2rxq(rq->vq);
2302 struct send_queue *sq = &vi->sq[index];
2303 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
2305 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
2308 if (__netif_tx_trylock(txq)) {
2310 __netif_tx_unlock(txq);
2315 virtqueue_disable_cb(sq->vq);
2316 free_old_xmit(sq, true);
2317 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2319 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) {
2320 if (netif_tx_queue_stopped(txq)) {
2321 u64_stats_update_begin(&sq->stats.syncp);
2322 u64_stats_inc(&sq->stats.wake);
2323 u64_stats_update_end(&sq->stats.syncp);
2325 netif_tx_wake_queue(txq);
2328 __netif_tx_unlock(txq);
2332 static void virtnet_rx_dim_update(struct virtnet_info *vi, struct receive_queue *rq)
2334 struct dim_sample cur_sample = {};
2336 if (!rq->packets_in_napi)
2339 u64_stats_update_begin(&rq->stats.syncp);
2340 dim_update_sample(rq->calls,
2341 u64_stats_read(&rq->stats.packets),
2342 u64_stats_read(&rq->stats.bytes),
2344 u64_stats_update_end(&rq->stats.syncp);
2346 net_dim(&rq->dim, cur_sample);
2347 rq->packets_in_napi = 0;
2350 static int virtnet_poll(struct napi_struct *napi, int budget)
2352 struct receive_queue *rq =
2353 container_of(napi, struct receive_queue, napi);
2354 struct virtnet_info *vi = rq->vq->vdev->priv;
2355 struct send_queue *sq;
2356 unsigned int received;
2357 unsigned int xdp_xmit = 0;
2360 virtnet_poll_cleantx(rq);
2362 received = virtnet_receive(rq, budget, &xdp_xmit);
2363 rq->packets_in_napi += received;
2365 if (xdp_xmit & VIRTIO_XDP_REDIR)
2368 /* Out of packets? */
2369 if (received < budget) {
2370 napi_complete = virtqueue_napi_complete(napi, rq->vq, received);
2371 /* Intentionally not taking dim_lock here. This may result in a
2372 * spurious net_dim call. But if that happens virtnet_rx_dim_work
2373 * will not act on the scheduled work.
2375 if (napi_complete && rq->dim_enabled)
2376 virtnet_rx_dim_update(vi, rq);
2379 if (xdp_xmit & VIRTIO_XDP_TX) {
2380 sq = virtnet_xdp_get_sq(vi);
2381 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2382 u64_stats_update_begin(&sq->stats.syncp);
2383 u64_stats_inc(&sq->stats.kicks);
2384 u64_stats_update_end(&sq->stats.syncp);
2386 virtnet_xdp_put_sq(vi, sq);
2392 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
2394 virtnet_napi_tx_disable(&vi->sq[qp_index].napi);
2395 napi_disable(&vi->rq[qp_index].napi);
2396 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
2399 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
2401 struct net_device *dev = vi->dev;
2404 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
2405 vi->rq[qp_index].napi.napi_id);
2409 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
2410 MEM_TYPE_PAGE_SHARED, NULL);
2412 goto err_xdp_reg_mem_model;
2414 virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi);
2415 virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi);
2419 err_xdp_reg_mem_model:
2420 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
2424 static int virtnet_open(struct net_device *dev)
2426 struct virtnet_info *vi = netdev_priv(dev);
2429 enable_delayed_refill(vi);
2431 for (i = 0; i < vi->max_queue_pairs; i++) {
2432 if (i < vi->curr_queue_pairs)
2433 /* Make sure we have some buffers: if oom use wq. */
2434 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2435 schedule_delayed_work(&vi->refill, 0);
2437 err = virtnet_enable_queue_pair(vi, i);
2445 disable_delayed_refill(vi);
2446 cancel_delayed_work_sync(&vi->refill);
2448 for (i--; i >= 0; i--) {
2449 virtnet_disable_queue_pair(vi, i);
2450 cancel_work_sync(&vi->rq[i].dim.work);
2456 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
2458 struct send_queue *sq = container_of(napi, struct send_queue, napi);
2459 struct virtnet_info *vi = sq->vq->vdev->priv;
2460 unsigned int index = vq2txq(sq->vq);
2461 struct netdev_queue *txq;
2465 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
2466 /* We don't need to enable cb for XDP */
2467 napi_complete_done(napi, 0);
2471 txq = netdev_get_tx_queue(vi->dev, index);
2472 __netif_tx_lock(txq, raw_smp_processor_id());
2473 virtqueue_disable_cb(sq->vq);
2474 free_old_xmit(sq, true);
2476 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) {
2477 if (netif_tx_queue_stopped(txq)) {
2478 u64_stats_update_begin(&sq->stats.syncp);
2479 u64_stats_inc(&sq->stats.wake);
2480 u64_stats_update_end(&sq->stats.syncp);
2482 netif_tx_wake_queue(txq);
2485 opaque = virtqueue_enable_cb_prepare(sq->vq);
2487 done = napi_complete_done(napi, 0);
2490 virtqueue_disable_cb(sq->vq);
2492 __netif_tx_unlock(txq);
2495 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
2496 if (napi_schedule_prep(napi)) {
2497 __netif_tx_lock(txq, raw_smp_processor_id());
2498 virtqueue_disable_cb(sq->vq);
2499 __netif_tx_unlock(txq);
2500 __napi_schedule(napi);
2508 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
2510 struct virtio_net_hdr_mrg_rxbuf *hdr;
2511 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
2512 struct virtnet_info *vi = sq->vq->vdev->priv;
2514 unsigned hdr_len = vi->hdr_len;
2517 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
2519 can_push = vi->any_header_sg &&
2520 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
2521 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
2522 /* Even if we can, don't push here yet as this would skew
2523 * csum_start offset below. */
2525 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
2527 hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
2529 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
2530 virtio_is_little_endian(vi->vdev), false,
2534 if (vi->mergeable_rx_bufs)
2535 hdr->num_buffers = 0;
2537 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
2539 __skb_push(skb, hdr_len);
2540 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
2541 if (unlikely(num_sg < 0))
2543 /* Pull header back to avoid skew in tx bytes calculations. */
2544 __skb_pull(skb, hdr_len);
2546 sg_set_buf(sq->sg, hdr, hdr_len);
2547 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
2548 if (unlikely(num_sg < 0))
2552 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
2555 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
2557 struct virtnet_info *vi = netdev_priv(dev);
2558 int qnum = skb_get_queue_mapping(skb);
2559 struct send_queue *sq = &vi->sq[qnum];
2561 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
2562 bool kick = !netdev_xmit_more();
2563 bool use_napi = sq->napi.weight;
2565 /* Free up any pending old buffers before queueing new ones. */
2568 virtqueue_disable_cb(sq->vq);
2570 free_old_xmit(sq, false);
2572 } while (use_napi && kick &&
2573 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2575 /* timestamp packet in software */
2576 skb_tx_timestamp(skb);
2578 /* Try to transmit */
2579 err = xmit_skb(sq, skb);
2581 /* This should not happen! */
2582 if (unlikely(err)) {
2583 DEV_STATS_INC(dev, tx_fifo_errors);
2584 if (net_ratelimit())
2586 "Unexpected TXQ (%d) queue failure: %d\n",
2588 DEV_STATS_INC(dev, tx_dropped);
2589 dev_kfree_skb_any(skb);
2590 return NETDEV_TX_OK;
2593 /* Don't wait up for transmitted skbs to be freed. */
2599 check_sq_full_and_disable(vi, dev, sq);
2601 if (kick || netif_xmit_stopped(txq)) {
2602 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2603 u64_stats_update_begin(&sq->stats.syncp);
2604 u64_stats_inc(&sq->stats.kicks);
2605 u64_stats_update_end(&sq->stats.syncp);
2609 return NETDEV_TX_OK;
2612 static int virtnet_rx_resize(struct virtnet_info *vi,
2613 struct receive_queue *rq, u32 ring_num)
2615 bool running = netif_running(vi->dev);
2618 qindex = rq - vi->rq;
2621 napi_disable(&rq->napi);
2622 cancel_work_sync(&rq->dim.work);
2625 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf);
2627 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
2629 if (!try_fill_recv(vi, rq, GFP_KERNEL))
2630 schedule_delayed_work(&vi->refill, 0);
2633 virtnet_napi_enable(rq->vq, &rq->napi);
2637 static int virtnet_tx_resize(struct virtnet_info *vi,
2638 struct send_queue *sq, u32 ring_num)
2640 bool running = netif_running(vi->dev);
2641 struct netdev_queue *txq;
2644 qindex = sq - vi->sq;
2647 virtnet_napi_tx_disable(&sq->napi);
2649 txq = netdev_get_tx_queue(vi->dev, qindex);
2651 /* 1. wait all ximt complete
2652 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
2654 __netif_tx_lock_bh(txq);
2656 /* Prevent rx poll from accessing sq. */
2659 /* Prevent the upper layer from trying to send packets. */
2660 netif_stop_subqueue(vi->dev, qindex);
2662 __netif_tx_unlock_bh(txq);
2664 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf);
2666 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
2668 __netif_tx_lock_bh(txq);
2670 netif_tx_wake_queue(txq);
2671 __netif_tx_unlock_bh(txq);
2674 virtnet_napi_tx_enable(vi, sq->vq, &sq->napi);
2679 * Send command via the control virtqueue and check status. Commands
2680 * supported by the hypervisor, as indicated by feature bits, should
2681 * never fail unless improperly formatted.
2683 static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd,
2684 struct scatterlist *out,
2685 struct scatterlist *in)
2687 struct scatterlist *sgs[5], hdr, stat;
2688 u32 out_num = 0, tmp, in_num = 0;
2691 /* Caller should know better */
2692 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
2694 mutex_lock(&vi->cvq_lock);
2695 vi->ctrl->status = ~0;
2696 vi->ctrl->hdr.class = class;
2697 vi->ctrl->hdr.cmd = cmd;
2699 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
2700 sgs[out_num++] = &hdr;
2703 sgs[out_num++] = out;
2705 /* Add return status. */
2706 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
2707 sgs[out_num + in_num++] = &stat;
2710 sgs[out_num + in_num++] = in;
2712 BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
2713 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC);
2715 dev_warn(&vi->vdev->dev,
2716 "Failed to add sgs for command vq: %d\n.", ret);
2717 mutex_unlock(&vi->cvq_lock);
2721 if (unlikely(!virtqueue_kick(vi->cvq)))
2724 /* Spin for a response, the kick causes an ioport write, trapping
2725 * into the hypervisor, so the request should be handled immediately.
2727 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
2728 !virtqueue_is_broken(vi->cvq)) {
2734 mutex_unlock(&vi->cvq_lock);
2735 return vi->ctrl->status == VIRTIO_NET_OK;
2738 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
2739 struct scatterlist *out)
2741 return virtnet_send_command_reply(vi, class, cmd, out, NULL);
2744 static int virtnet_set_mac_address(struct net_device *dev, void *p)
2746 struct virtnet_info *vi = netdev_priv(dev);
2747 struct virtio_device *vdev = vi->vdev;
2749 struct sockaddr *addr;
2750 struct scatterlist sg;
2752 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2755 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
2759 ret = eth_prepare_mac_addr_change(dev, addr);
2763 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
2764 sg_init_one(&sg, addr->sa_data, dev->addr_len);
2765 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2766 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
2767 dev_warn(&vdev->dev,
2768 "Failed to set mac address by vq command.\n");
2772 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
2773 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2776 /* Naturally, this has an atomicity problem. */
2777 for (i = 0; i < dev->addr_len; i++)
2778 virtio_cwrite8(vdev,
2779 offsetof(struct virtio_net_config, mac) +
2780 i, addr->sa_data[i]);
2783 eth_commit_mac_addr_change(dev, p);
2791 static void virtnet_stats(struct net_device *dev,
2792 struct rtnl_link_stats64 *tot)
2794 struct virtnet_info *vi = netdev_priv(dev);
2798 for (i = 0; i < vi->max_queue_pairs; i++) {
2799 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
2800 struct receive_queue *rq = &vi->rq[i];
2801 struct send_queue *sq = &vi->sq[i];
2804 start = u64_stats_fetch_begin(&sq->stats.syncp);
2805 tpackets = u64_stats_read(&sq->stats.packets);
2806 tbytes = u64_stats_read(&sq->stats.bytes);
2807 terrors = u64_stats_read(&sq->stats.tx_timeouts);
2808 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
2811 start = u64_stats_fetch_begin(&rq->stats.syncp);
2812 rpackets = u64_stats_read(&rq->stats.packets);
2813 rbytes = u64_stats_read(&rq->stats.bytes);
2814 rdrops = u64_stats_read(&rq->stats.drops);
2815 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
2817 tot->rx_packets += rpackets;
2818 tot->tx_packets += tpackets;
2819 tot->rx_bytes += rbytes;
2820 tot->tx_bytes += tbytes;
2821 tot->rx_dropped += rdrops;
2822 tot->tx_errors += terrors;
2825 tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
2826 tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors);
2827 tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors);
2828 tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors);
2831 static void virtnet_ack_link_announce(struct virtnet_info *vi)
2833 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
2834 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
2835 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
2838 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2840 struct virtio_net_ctrl_mq *mq __free(kfree) = NULL;
2841 struct scatterlist sg;
2842 struct net_device *dev = vi->dev;
2844 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
2847 mq = kzalloc(sizeof(*mq), GFP_KERNEL);
2851 mq->virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
2852 sg_init_one(&sg, mq, sizeof(*mq));
2854 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2855 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
2856 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
2860 vi->curr_queue_pairs = queue_pairs;
2861 /* virtnet_open() will refill when device is going to up. */
2862 if (dev->flags & IFF_UP)
2863 schedule_delayed_work(&vi->refill, 0);
2869 static int virtnet_close(struct net_device *dev)
2871 struct virtnet_info *vi = netdev_priv(dev);
2874 /* Make sure NAPI doesn't schedule refill work */
2875 disable_delayed_refill(vi);
2876 /* Make sure refill_work doesn't re-enable napi! */
2877 cancel_delayed_work_sync(&vi->refill);
2879 for (i = 0; i < vi->max_queue_pairs; i++) {
2880 virtnet_disable_queue_pair(vi, i);
2881 cancel_work_sync(&vi->rq[i].dim.work);
2887 static void virtnet_rx_mode_work(struct work_struct *work)
2889 struct virtnet_info *vi =
2890 container_of(work, struct virtnet_info, rx_mode_work);
2891 u8 *promisc_allmulti __free(kfree) = NULL;
2892 struct net_device *dev = vi->dev;
2893 struct scatterlist sg[2];
2894 struct virtio_net_ctrl_mac *mac_data;
2895 struct netdev_hw_addr *ha;
2901 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
2902 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
2907 promisc_allmulti = kzalloc(sizeof(*promisc_allmulti), GFP_ATOMIC);
2908 if (!promisc_allmulti) {
2909 dev_warn(&dev->dev, "Failed to set RX mode, no memory.\n");
2913 *promisc_allmulti = !!(dev->flags & IFF_PROMISC);
2914 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti));
2916 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2917 VIRTIO_NET_CTRL_RX_PROMISC, sg))
2918 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
2919 *promisc_allmulti ? "en" : "dis");
2921 *promisc_allmulti = !!(dev->flags & IFF_ALLMULTI);
2922 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti));
2924 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2925 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2926 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2927 *promisc_allmulti ? "en" : "dis");
2929 netif_addr_lock_bh(dev);
2931 uc_count = netdev_uc_count(dev);
2932 mc_count = netdev_mc_count(dev);
2933 /* MAC filter - use one buffer for both lists */
2934 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2935 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
2938 netif_addr_unlock_bh(dev);
2943 sg_init_table(sg, 2);
2945 /* Store the unicast list and count in the front of the buffer */
2946 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2948 netdev_for_each_uc_addr(ha, dev)
2949 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2951 sg_set_buf(&sg[0], mac_data,
2952 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2954 /* multicast list and count fill the end */
2955 mac_data = (void *)&mac_data->macs[uc_count][0];
2957 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2959 netdev_for_each_mc_addr(ha, dev)
2960 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2962 netif_addr_unlock_bh(dev);
2964 sg_set_buf(&sg[1], mac_data,
2965 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2967 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2968 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
2969 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2976 static void virtnet_set_rx_mode(struct net_device *dev)
2978 struct virtnet_info *vi = netdev_priv(dev);
2980 if (vi->rx_mode_work_enabled)
2981 schedule_work(&vi->rx_mode_work);
2984 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2985 __be16 proto, u16 vid)
2987 struct virtnet_info *vi = netdev_priv(dev);
2988 __virtio16 *_vid __free(kfree) = NULL;
2989 struct scatterlist sg;
2991 _vid = kzalloc(sizeof(*_vid), GFP_KERNEL);
2995 *_vid = cpu_to_virtio16(vi->vdev, vid);
2996 sg_init_one(&sg, _vid, sizeof(*_vid));
2998 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2999 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
3000 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
3004 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
3005 __be16 proto, u16 vid)
3007 struct virtnet_info *vi = netdev_priv(dev);
3008 __virtio16 *_vid __free(kfree) = NULL;
3009 struct scatterlist sg;
3011 _vid = kzalloc(sizeof(*_vid), GFP_KERNEL);
3015 *_vid = cpu_to_virtio16(vi->vdev, vid);
3016 sg_init_one(&sg, _vid, sizeof(*_vid));
3018 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
3019 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
3020 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
3024 static void virtnet_clean_affinity(struct virtnet_info *vi)
3028 if (vi->affinity_hint_set) {
3029 for (i = 0; i < vi->max_queue_pairs; i++) {
3030 virtqueue_set_affinity(vi->rq[i].vq, NULL);
3031 virtqueue_set_affinity(vi->sq[i].vq, NULL);
3034 vi->affinity_hint_set = false;
3038 static void virtnet_set_affinity(struct virtnet_info *vi)
3047 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
3048 virtnet_clean_affinity(vi);
3052 num_cpu = num_online_cpus();
3053 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
3054 stragglers = num_cpu >= vi->curr_queue_pairs ?
3055 num_cpu % vi->curr_queue_pairs :
3057 cpu = cpumask_first(cpu_online_mask);
3059 for (i = 0; i < vi->curr_queue_pairs; i++) {
3060 group_size = stride + (i < stragglers ? 1 : 0);
3062 for (j = 0; j < group_size; j++) {
3063 cpumask_set_cpu(cpu, mask);
3064 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
3067 virtqueue_set_affinity(vi->rq[i].vq, mask);
3068 virtqueue_set_affinity(vi->sq[i].vq, mask);
3069 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
3070 cpumask_clear(mask);
3073 vi->affinity_hint_set = true;
3074 free_cpumask_var(mask);
3077 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
3079 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
3081 virtnet_set_affinity(vi);
3085 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
3087 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
3089 virtnet_set_affinity(vi);
3093 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
3095 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
3098 virtnet_clean_affinity(vi);
3102 static enum cpuhp_state virtionet_online;
3104 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
3108 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
3111 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
3115 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
3119 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
3121 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
3122 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
3126 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
3127 u16 vqn, u32 max_usecs, u32 max_packets)
3129 struct virtio_net_ctrl_coal_vq *coal_vq __free(kfree) = NULL;
3130 struct scatterlist sgs;
3132 coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL);
3136 coal_vq->vqn = cpu_to_le16(vqn);
3137 coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
3138 coal_vq->coal.max_packets = cpu_to_le32(max_packets);
3139 sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
3141 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3142 VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
3149 static int virtnet_send_rx_ctrl_coal_vq_cmd(struct virtnet_info *vi,
3150 u16 queue, u32 max_usecs,
3155 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
3156 max_usecs, max_packets);
3160 vi->rq[queue].intr_coal.max_usecs = max_usecs;
3161 vi->rq[queue].intr_coal.max_packets = max_packets;
3166 static int virtnet_send_tx_ctrl_coal_vq_cmd(struct virtnet_info *vi,
3167 u16 queue, u32 max_usecs,
3172 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
3173 max_usecs, max_packets);
3177 vi->sq[queue].intr_coal.max_usecs = max_usecs;
3178 vi->sq[queue].intr_coal.max_packets = max_packets;
3183 static void virtnet_get_ringparam(struct net_device *dev,
3184 struct ethtool_ringparam *ring,
3185 struct kernel_ethtool_ringparam *kernel_ring,
3186 struct netlink_ext_ack *extack)
3188 struct virtnet_info *vi = netdev_priv(dev);
3190 ring->rx_max_pending = vi->rq[0].vq->num_max;
3191 ring->tx_max_pending = vi->sq[0].vq->num_max;
3192 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
3193 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
3196 static int virtnet_set_ringparam(struct net_device *dev,
3197 struct ethtool_ringparam *ring,
3198 struct kernel_ethtool_ringparam *kernel_ring,
3199 struct netlink_ext_ack *extack)
3201 struct virtnet_info *vi = netdev_priv(dev);
3202 u32 rx_pending, tx_pending;
3203 struct receive_queue *rq;
3204 struct send_queue *sq;
3207 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
3210 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
3211 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
3213 if (ring->rx_pending == rx_pending &&
3214 ring->tx_pending == tx_pending)
3217 if (ring->rx_pending > vi->rq[0].vq->num_max)
3220 if (ring->tx_pending > vi->sq[0].vq->num_max)
3223 for (i = 0; i < vi->max_queue_pairs; i++) {
3227 if (ring->tx_pending != tx_pending) {
3228 err = virtnet_tx_resize(vi, sq, ring->tx_pending);
3232 /* Upon disabling and re-enabling a transmit virtqueue, the device must
3233 * set the coalescing parameters of the virtqueue to those configured
3234 * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver
3235 * did not set any TX coalescing parameters, to 0.
3237 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, i,
3238 vi->intr_coal_tx.max_usecs,
3239 vi->intr_coal_tx.max_packets);
3244 if (ring->rx_pending != rx_pending) {
3245 err = virtnet_rx_resize(vi, rq, ring->rx_pending);
3249 /* The reason is same as the transmit virtqueue reset */
3250 mutex_lock(&vi->rq[i].dim_lock);
3251 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, i,
3252 vi->intr_coal_rx.max_usecs,
3253 vi->intr_coal_rx.max_packets);
3254 mutex_unlock(&vi->rq[i].dim_lock);
3263 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
3265 struct net_device *dev = vi->dev;
3266 struct scatterlist sgs[4];
3267 unsigned int sg_buf_size;
3270 sg_init_table(sgs, 4);
3272 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
3273 sg_set_buf(&sgs[0], &vi->rss, sg_buf_size);
3275 sg_buf_size = sizeof(uint16_t) * (vi->rss.indirection_table_mask + 1);
3276 sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
3278 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
3279 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
3280 sg_set_buf(&sgs[2], &vi->rss.max_tx_vq, sg_buf_size);
3282 sg_buf_size = vi->rss_key_size;
3283 sg_set_buf(&sgs[3], vi->rss.key, sg_buf_size);
3285 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
3286 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
3287 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs))
3293 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
3298 static void virtnet_init_default_rss(struct virtnet_info *vi)
3303 vi->rss.hash_types = vi->rss_hash_types_supported;
3304 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
3305 vi->rss.indirection_table_mask = vi->rss_indir_table_size
3306 ? vi->rss_indir_table_size - 1 : 0;
3307 vi->rss.unclassified_queue = 0;
3309 for (; i < vi->rss_indir_table_size; ++i) {
3310 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
3311 vi->rss.indirection_table[i] = indir_val;
3314 vi->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0;
3315 vi->rss.hash_key_length = vi->rss_key_size;
3317 netdev_rss_key_fill(vi->rss.key, vi->rss_key_size);
3320 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
3323 switch (info->flow_type) {
3325 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
3326 info->data = RXH_IP_SRC | RXH_IP_DST |
3327 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3328 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3329 info->data = RXH_IP_SRC | RXH_IP_DST;
3333 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
3334 info->data = RXH_IP_SRC | RXH_IP_DST |
3335 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3336 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3337 info->data = RXH_IP_SRC | RXH_IP_DST;
3341 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
3342 info->data = RXH_IP_SRC | RXH_IP_DST |
3343 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3344 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3345 info->data = RXH_IP_SRC | RXH_IP_DST;
3349 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
3350 info->data = RXH_IP_SRC | RXH_IP_DST |
3351 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3352 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3353 info->data = RXH_IP_SRC | RXH_IP_DST;
3357 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
3358 info->data = RXH_IP_SRC | RXH_IP_DST;
3362 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
3363 info->data = RXH_IP_SRC | RXH_IP_DST;
3372 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
3374 u32 new_hashtypes = vi->rss_hash_types_saved;
3375 bool is_disable = info->data & RXH_DISCARD;
3376 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
3378 /* supports only 'sd', 'sdfn' and 'r' */
3379 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
3382 switch (info->flow_type) {
3384 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
3386 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3387 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
3390 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
3392 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3393 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
3396 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3398 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3401 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
3403 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3404 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
3407 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
3409 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3410 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
3413 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3415 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3418 /* unsupported flow */
3422 /* if unsupported hashtype was set */
3423 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
3426 if (new_hashtypes != vi->rss_hash_types_saved) {
3427 vi->rss_hash_types_saved = new_hashtypes;
3428 vi->rss.hash_types = vi->rss_hash_types_saved;
3429 if (vi->dev->features & NETIF_F_RXHASH)
3430 return virtnet_commit_rss_command(vi);
3436 static void virtnet_get_drvinfo(struct net_device *dev,
3437 struct ethtool_drvinfo *info)
3439 struct virtnet_info *vi = netdev_priv(dev);
3440 struct virtio_device *vdev = vi->vdev;
3442 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
3443 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
3444 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
3448 /* TODO: Eliminate OOO packets during switching */
3449 static int virtnet_set_channels(struct net_device *dev,
3450 struct ethtool_channels *channels)
3452 struct virtnet_info *vi = netdev_priv(dev);
3453 u16 queue_pairs = channels->combined_count;
3456 /* We don't support separate rx/tx channels.
3457 * We don't allow setting 'other' channels.
3459 if (channels->rx_count || channels->tx_count || channels->other_count)
3462 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
3465 /* For now we don't support modifying channels while XDP is loaded
3466 * also when XDP is loaded all RX queues have XDP programs so we only
3467 * need to check a single RX queue.
3469 if (vi->rq[0].xdp_prog)
3473 err = virtnet_set_queues(vi, queue_pairs);
3478 virtnet_set_affinity(vi);
3481 netif_set_real_num_tx_queues(dev, queue_pairs);
3482 netif_set_real_num_rx_queues(dev, queue_pairs);
3487 static void virtnet_stats_sprintf(u8 **p, const char *fmt, const char *noq_fmt,
3488 int num, int qid, const struct virtnet_stat_desc *desc)
3493 for (i = 0; i < num; ++i)
3494 ethtool_sprintf(p, noq_fmt, desc[i].desc);
3496 for (i = 0; i < num; ++i)
3497 ethtool_sprintf(p, fmt, qid, desc[i].desc);
3501 /* qid == -1: for rx/tx queue total field */
3502 static void virtnet_get_stats_string(struct virtnet_info *vi, int type, int qid, u8 **data)
3504 const struct virtnet_stat_desc *desc;
3505 const char *fmt, *noq_fmt;
3509 if (type == VIRTNET_Q_TYPE_CQ && qid >= 0) {
3510 noq_fmt = "cq_hw_%s";
3512 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
3513 desc = &virtnet_stats_cvq_desc[0];
3514 num = ARRAY_SIZE(virtnet_stats_cvq_desc);
3516 virtnet_stats_sprintf(&p, NULL, noq_fmt, num, -1, desc);
3520 if (type == VIRTNET_Q_TYPE_RX) {
3524 desc = &virtnet_rq_stats_desc[0];
3525 num = ARRAY_SIZE(virtnet_rq_stats_desc);
3527 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
3530 noq_fmt = "rx_hw_%s";
3532 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
3533 desc = &virtnet_stats_rx_basic_desc[0];
3534 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
3536 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
3539 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
3540 desc = &virtnet_stats_rx_csum_desc[0];
3541 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
3543 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
3546 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
3547 desc = &virtnet_stats_rx_speed_desc[0];
3548 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
3550 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
3554 if (type == VIRTNET_Q_TYPE_TX) {
3558 desc = &virtnet_sq_stats_desc[0];
3559 num = ARRAY_SIZE(virtnet_sq_stats_desc);
3561 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
3564 noq_fmt = "tx_hw_%s";
3566 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
3567 desc = &virtnet_stats_tx_basic_desc[0];
3568 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
3570 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
3573 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
3574 desc = &virtnet_stats_tx_gso_desc[0];
3575 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
3577 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
3580 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
3581 desc = &virtnet_stats_tx_speed_desc[0];
3582 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
3584 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
3591 struct virtnet_stats_ctx {
3592 /* The stats are write to qstats or ethtool -S */
3595 /* Used to calculate the offset inside the output buffer. */
3598 /* The actual supported stat types. */
3601 /* Used to calculate the reply buffer size. */
3604 /* Record the output buffer. */
3608 static void virtnet_stats_ctx_init(struct virtnet_info *vi,
3609 struct virtnet_stats_ctx *ctx,
3610 u64 *data, bool to_qstat)
3615 ctx->to_qstat = to_qstat;
3618 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc_qstat);
3619 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc_qstat);
3621 queue_type = VIRTNET_Q_TYPE_RX;
3623 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
3624 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC;
3625 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat);
3626 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic);
3629 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
3630 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM;
3631 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat);
3632 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum);
3635 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
3636 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_GSO;
3637 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat);
3638 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_gso);
3641 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
3642 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED;
3643 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat);
3644 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed);
3647 queue_type = VIRTNET_Q_TYPE_TX;
3649 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
3650 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC;
3651 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat);
3652 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic);
3655 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
3656 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_CSUM;
3657 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat);
3658 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_csum);
3661 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
3662 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO;
3663 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat);
3664 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso);
3667 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
3668 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED;
3669 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat);
3670 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed);
3676 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc);
3677 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc);
3679 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
3680 queue_type = VIRTNET_Q_TYPE_CQ;
3682 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_CVQ;
3683 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_cvq_desc);
3684 ctx->size[queue_type] += sizeof(struct virtio_net_stats_cvq);
3687 queue_type = VIRTNET_Q_TYPE_RX;
3689 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
3690 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC;
3691 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc);
3692 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic);
3695 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
3696 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM;
3697 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc);
3698 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum);
3701 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
3702 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED;
3703 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc);
3704 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed);
3707 queue_type = VIRTNET_Q_TYPE_TX;
3709 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
3710 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC;
3711 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc);
3712 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic);
3715 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
3716 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO;
3717 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc);
3718 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso);
3721 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
3722 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED;
3723 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc);
3724 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed);
3728 /* stats_sum_queue - Calculate the sum of the same fields in sq or rq.
3729 * @sum: the position to store the sum values
3731 * @q_value: the first queue fields
3732 * @q_num: number of the queues
3734 static void stats_sum_queue(u64 *sum, u32 num, u64 *q_value, u32 q_num)
3740 for (i = 0; i < num; ++i) {
3744 for (j = 0; j < q_num; ++j)
3745 *p += *(q_value + i + j * step);
3749 static void virtnet_fill_total_fields(struct virtnet_info *vi,
3750 struct virtnet_stats_ctx *ctx)
3752 u64 *data, *first_rx_q, *first_tx_q;
3753 u32 num_cq, num_rx, num_tx;
3755 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
3756 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
3757 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
3759 first_rx_q = ctx->data + num_rx + num_tx + num_cq;
3760 first_tx_q = first_rx_q + vi->curr_queue_pairs * num_rx;
3764 stats_sum_queue(data, num_rx, first_rx_q, vi->curr_queue_pairs);
3766 data = ctx->data + num_rx;
3768 stats_sum_queue(data, num_tx, first_tx_q, vi->curr_queue_pairs);
3771 static void virtnet_fill_stats_qstat(struct virtnet_info *vi, u32 qid,
3772 struct virtnet_stats_ctx *ctx,
3773 const u8 *base, bool drv_stats, u8 reply_type)
3775 const struct virtnet_stat_desc *desc;
3776 const u64_stats_t *v_stat;
3782 queue_type = vq_type(vi, qid);
3783 bitmap = ctx->bitmap[queue_type];
3786 if (queue_type == VIRTNET_Q_TYPE_RX) {
3787 desc = &virtnet_rq_stats_desc_qstat[0];
3788 num = ARRAY_SIZE(virtnet_rq_stats_desc_qstat);
3790 desc = &virtnet_sq_stats_desc_qstat[0];
3791 num = ARRAY_SIZE(virtnet_sq_stats_desc_qstat);
3794 for (i = 0; i < num; ++i) {
3795 offset = desc[i].qstat_offset / sizeof(*ctx->data);
3796 v_stat = (const u64_stats_t *)(base + desc[i].offset);
3797 ctx->data[offset] = u64_stats_read(v_stat);
3802 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
3803 desc = &virtnet_stats_rx_basic_desc_qstat[0];
3804 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat);
3805 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC)
3809 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
3810 desc = &virtnet_stats_rx_csum_desc_qstat[0];
3811 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat);
3812 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM)
3816 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
3817 desc = &virtnet_stats_rx_gso_desc_qstat[0];
3818 num = ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat);
3819 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_GSO)
3823 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
3824 desc = &virtnet_stats_rx_speed_desc_qstat[0];
3825 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat);
3826 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED)
3830 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
3831 desc = &virtnet_stats_tx_basic_desc_qstat[0];
3832 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat);
3833 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC)
3837 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
3838 desc = &virtnet_stats_tx_csum_desc_qstat[0];
3839 num = ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat);
3840 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_CSUM)
3844 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
3845 desc = &virtnet_stats_tx_gso_desc_qstat[0];
3846 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat);
3847 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO)
3851 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
3852 desc = &virtnet_stats_tx_speed_desc_qstat[0];
3853 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat);
3854 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED)
3861 for (i = 0; i < num; ++i) {
3862 offset = desc[i].qstat_offset / sizeof(*ctx->data);
3863 v = (const __le64 *)(base + desc[i].offset);
3864 ctx->data[offset] = le64_to_cpu(*v);
3868 /* virtnet_fill_stats - copy the stats to qstats or ethtool -S
3869 * The stats source is the device or the driver.
3871 * @vi: virtio net info
3873 * @ctx: stats ctx (initiated by virtnet_stats_ctx_init())
3874 * @base: pointer to the device reply or the driver stats structure.
3875 * @drv_stats: designate the base type (device reply, driver stats)
3876 * @type: the type of the device reply (if drv_stats is true, this must be zero)
3878 static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid,
3879 struct virtnet_stats_ctx *ctx,
3880 const u8 *base, bool drv_stats, u8 reply_type)
3882 u32 queue_type, num_rx, num_tx, num_cq;
3883 const struct virtnet_stat_desc *desc;
3884 const u64_stats_t *v_stat;
3890 return virtnet_fill_stats_qstat(vi, qid, ctx, base, drv_stats, reply_type);
3892 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
3893 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
3894 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
3896 queue_type = vq_type(vi, qid);
3897 bitmap = ctx->bitmap[queue_type];
3899 /* skip the total fields of pairs */
3900 offset = num_rx + num_tx;
3902 if (queue_type == VIRTNET_Q_TYPE_TX) {
3903 offset += num_cq + num_rx * vi->curr_queue_pairs + num_tx * (qid / 2);
3905 num = ARRAY_SIZE(virtnet_sq_stats_desc);
3907 desc = &virtnet_sq_stats_desc[0];
3913 } else if (queue_type == VIRTNET_Q_TYPE_RX) {
3914 offset += num_cq + num_rx * (qid / 2);
3916 num = ARRAY_SIZE(virtnet_rq_stats_desc);
3918 desc = &virtnet_rq_stats_desc[0];
3925 if (bitmap & VIRTIO_NET_STATS_TYPE_CVQ) {
3926 desc = &virtnet_stats_cvq_desc[0];
3927 num = ARRAY_SIZE(virtnet_stats_cvq_desc);
3928 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_CVQ)
3934 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
3935 desc = &virtnet_stats_rx_basic_desc[0];
3936 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
3937 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC)
3943 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
3944 desc = &virtnet_stats_rx_csum_desc[0];
3945 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
3946 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM)
3952 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
3953 desc = &virtnet_stats_rx_speed_desc[0];
3954 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
3955 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED)
3961 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
3962 desc = &virtnet_stats_tx_basic_desc[0];
3963 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
3964 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC)
3970 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
3971 desc = &virtnet_stats_tx_gso_desc[0];
3972 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
3973 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO)
3979 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
3980 desc = &virtnet_stats_tx_speed_desc[0];
3981 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
3982 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED)
3991 for (i = 0; i < num; ++i) {
3992 v = (const __le64 *)(base + desc[i].offset);
3993 ctx->data[offset + i] = le64_to_cpu(*v);
3999 for (i = 0; i < num; ++i) {
4000 v_stat = (const u64_stats_t *)(base + desc[i].offset);
4001 ctx->data[offset + i] = u64_stats_read(v_stat);
4005 static int __virtnet_get_hw_stats(struct virtnet_info *vi,
4006 struct virtnet_stats_ctx *ctx,
4007 struct virtio_net_ctrl_queue_stats *req,
4008 int req_size, void *reply, int res_size)
4010 struct virtio_net_stats_reply_hdr *hdr;
4011 struct scatterlist sgs_in, sgs_out;
4016 sg_init_one(&sgs_out, req, req_size);
4017 sg_init_one(&sgs_in, reply, res_size);
4019 ok = virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
4020 VIRTIO_NET_CTRL_STATS_GET,
4026 for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
4028 qid = le16_to_cpu(hdr->vq_index);
4029 virtnet_fill_stats(vi, qid, ctx, p, false, hdr->type);
4035 static void virtnet_make_stat_req(struct virtnet_info *vi,
4036 struct virtnet_stats_ctx *ctx,
4037 struct virtio_net_ctrl_queue_stats *req,
4040 int qtype = vq_type(vi, qid);
4041 u64 bitmap = ctx->bitmap[qtype];
4046 req->stats[*idx].vq_index = cpu_to_le16(qid);
4047 req->stats[*idx].types_bitmap[0] = cpu_to_le64(bitmap);
4051 /* qid: -1: get stats of all vq.
4052 * > 0: get the stats for the special vq. This must not be cvq.
4054 static int virtnet_get_hw_stats(struct virtnet_info *vi,
4055 struct virtnet_stats_ctx *ctx, int qid)
4057 int qnum, i, j, res_size, qtype, last_vq, first_vq;
4058 struct virtio_net_ctrl_queue_stats *req;
4063 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS))
4067 last_vq = vi->curr_queue_pairs * 2 - 1;
4078 for (i = first_vq; i <= last_vq ; ++i) {
4079 qtype = vq_type(vi, i);
4080 if (ctx->bitmap[qtype]) {
4082 res_size += ctx->size[qtype];
4086 if (enable_cvq && ctx->bitmap[VIRTNET_Q_TYPE_CQ]) {
4087 res_size += ctx->size[VIRTNET_Q_TYPE_CQ];
4091 req = kcalloc(qnum, sizeof(*req), GFP_KERNEL);
4095 reply = kmalloc(res_size, GFP_KERNEL);
4102 for (i = first_vq; i <= last_vq ; ++i)
4103 virtnet_make_stat_req(vi, ctx, req, i, &j);
4106 virtnet_make_stat_req(vi, ctx, req, vi->max_queue_pairs * 2, &j);
4108 ok = __virtnet_get_hw_stats(vi, ctx, req, sizeof(*req) * j, reply, res_size);
4116 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4118 struct virtnet_info *vi = netdev_priv(dev);
4122 switch (stringset) {
4124 /* Generate the total field names. */
4125 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, -1, &p);
4126 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, -1, &p);
4128 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_CQ, 0, &p);
4130 for (i = 0; i < vi->curr_queue_pairs; ++i)
4131 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, i, &p);
4133 for (i = 0; i < vi->curr_queue_pairs; ++i)
4134 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, i, &p);
4139 static int virtnet_get_sset_count(struct net_device *dev, int sset)
4141 struct virtnet_info *vi = netdev_priv(dev);
4142 struct virtnet_stats_ctx ctx = {0};
4147 virtnet_stats_ctx_init(vi, &ctx, NULL, false);
4149 pair_count = ctx.desc_num[VIRTNET_Q_TYPE_RX] + ctx.desc_num[VIRTNET_Q_TYPE_TX];
4151 return pair_count + ctx.desc_num[VIRTNET_Q_TYPE_CQ] +
4152 vi->curr_queue_pairs * pair_count;
4158 static void virtnet_get_ethtool_stats(struct net_device *dev,
4159 struct ethtool_stats *stats, u64 *data)
4161 struct virtnet_info *vi = netdev_priv(dev);
4162 struct virtnet_stats_ctx ctx = {0};
4163 unsigned int start, i;
4164 const u8 *stats_base;
4166 virtnet_stats_ctx_init(vi, &ctx, data, false);
4167 if (virtnet_get_hw_stats(vi, &ctx, -1))
4168 dev_warn(&vi->dev->dev, "Failed to get hw stats.\n");
4170 for (i = 0; i < vi->curr_queue_pairs; i++) {
4171 struct receive_queue *rq = &vi->rq[i];
4172 struct send_queue *sq = &vi->sq[i];
4174 stats_base = (const u8 *)&rq->stats;
4176 start = u64_stats_fetch_begin(&rq->stats.syncp);
4177 virtnet_fill_stats(vi, i * 2, &ctx, stats_base, true, 0);
4178 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
4180 stats_base = (const u8 *)&sq->stats;
4182 start = u64_stats_fetch_begin(&sq->stats.syncp);
4183 virtnet_fill_stats(vi, i * 2 + 1, &ctx, stats_base, true, 0);
4184 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
4187 virtnet_fill_total_fields(vi, &ctx);
4190 static void virtnet_get_channels(struct net_device *dev,
4191 struct ethtool_channels *channels)
4193 struct virtnet_info *vi = netdev_priv(dev);
4195 channels->combined_count = vi->curr_queue_pairs;
4196 channels->max_combined = vi->max_queue_pairs;
4197 channels->max_other = 0;
4198 channels->rx_count = 0;
4199 channels->tx_count = 0;
4200 channels->other_count = 0;
4203 static int virtnet_set_link_ksettings(struct net_device *dev,
4204 const struct ethtool_link_ksettings *cmd)
4206 struct virtnet_info *vi = netdev_priv(dev);
4208 return ethtool_virtdev_set_link_ksettings(dev, cmd,
4209 &vi->speed, &vi->duplex);
4212 static int virtnet_get_link_ksettings(struct net_device *dev,
4213 struct ethtool_link_ksettings *cmd)
4215 struct virtnet_info *vi = netdev_priv(dev);
4217 cmd->base.speed = vi->speed;
4218 cmd->base.duplex = vi->duplex;
4219 cmd->base.port = PORT_OTHER;
4224 static int virtnet_send_tx_notf_coal_cmds(struct virtnet_info *vi,
4225 struct ethtool_coalesce *ec)
4227 struct virtio_net_ctrl_coal_tx *coal_tx __free(kfree) = NULL;
4228 struct scatterlist sgs_tx;
4231 coal_tx = kzalloc(sizeof(*coal_tx), GFP_KERNEL);
4235 coal_tx->tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
4236 coal_tx->tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
4237 sg_init_one(&sgs_tx, coal_tx, sizeof(*coal_tx));
4239 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
4240 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
4244 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs;
4245 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames;
4246 for (i = 0; i < vi->max_queue_pairs; i++) {
4247 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs;
4248 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames;
4254 static int virtnet_send_rx_notf_coal_cmds(struct virtnet_info *vi,
4255 struct ethtool_coalesce *ec)
4257 struct virtio_net_ctrl_coal_rx *coal_rx __free(kfree) = NULL;
4258 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce;
4259 struct scatterlist sgs_rx;
4263 if (rx_ctrl_dim_on && !virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
4266 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != vi->intr_coal_rx.max_usecs ||
4267 ec->rx_max_coalesced_frames != vi->intr_coal_rx.max_packets))
4270 /* Acquire all queues dim_locks */
4271 for (i = 0; i < vi->max_queue_pairs; i++)
4272 mutex_lock(&vi->rq[i].dim_lock);
4274 if (rx_ctrl_dim_on && !vi->rx_dim_enabled) {
4275 vi->rx_dim_enabled = true;
4276 for (i = 0; i < vi->max_queue_pairs; i++)
4277 vi->rq[i].dim_enabled = true;
4281 coal_rx = kzalloc(sizeof(*coal_rx), GFP_KERNEL);
4287 if (!rx_ctrl_dim_on && vi->rx_dim_enabled) {
4288 vi->rx_dim_enabled = false;
4289 for (i = 0; i < vi->max_queue_pairs; i++)
4290 vi->rq[i].dim_enabled = false;
4293 /* Since the per-queue coalescing params can be set,
4294 * we need apply the global new params even if they
4297 coal_rx->rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
4298 coal_rx->rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
4299 sg_init_one(&sgs_rx, coal_rx, sizeof(*coal_rx));
4301 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
4302 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
4308 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs;
4309 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames;
4310 for (i = 0; i < vi->max_queue_pairs; i++) {
4311 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs;
4312 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames;
4315 for (i = vi->max_queue_pairs - 1; i >= 0; i--)
4316 mutex_unlock(&vi->rq[i].dim_lock);
4321 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
4322 struct ethtool_coalesce *ec)
4326 err = virtnet_send_tx_notf_coal_cmds(vi, ec);
4330 err = virtnet_send_rx_notf_coal_cmds(vi, ec);
4337 static int virtnet_send_rx_notf_coal_vq_cmds(struct virtnet_info *vi,
4338 struct ethtool_coalesce *ec,
4341 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce;
4342 u32 max_usecs, max_packets;
4346 mutex_lock(&vi->rq[queue].dim_lock);
4347 cur_rx_dim = vi->rq[queue].dim_enabled;
4348 max_usecs = vi->rq[queue].intr_coal.max_usecs;
4349 max_packets = vi->rq[queue].intr_coal.max_packets;
4351 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != max_usecs ||
4352 ec->rx_max_coalesced_frames != max_packets)) {
4353 mutex_unlock(&vi->rq[queue].dim_lock);
4357 if (rx_ctrl_dim_on && !cur_rx_dim) {
4358 vi->rq[queue].dim_enabled = true;
4359 mutex_unlock(&vi->rq[queue].dim_lock);
4363 if (!rx_ctrl_dim_on && cur_rx_dim)
4364 vi->rq[queue].dim_enabled = false;
4366 /* If no params are updated, userspace ethtool will
4367 * reject the modification.
4369 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, queue,
4370 ec->rx_coalesce_usecs,
4371 ec->rx_max_coalesced_frames);
4372 mutex_unlock(&vi->rq[queue].dim_lock);
4376 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
4377 struct ethtool_coalesce *ec,
4382 err = virtnet_send_rx_notf_coal_vq_cmds(vi, ec, queue);
4386 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, queue,
4387 ec->tx_coalesce_usecs,
4388 ec->tx_max_coalesced_frames);
4395 static void virtnet_rx_dim_work(struct work_struct *work)
4397 struct dim *dim = container_of(work, struct dim, work);
4398 struct receive_queue *rq = container_of(dim,
4399 struct receive_queue, dim);
4400 struct virtnet_info *vi = rq->vq->vdev->priv;
4401 struct net_device *dev = vi->dev;
4402 struct dim_cq_moder update_moder;
4407 mutex_lock(&rq->dim_lock);
4408 if (!rq->dim_enabled)
4411 update_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
4412 if (update_moder.usec != rq->intr_coal.max_usecs ||
4413 update_moder.pkts != rq->intr_coal.max_packets) {
4414 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, qnum,
4418 pr_debug("%s: Failed to send dim parameters on rxq%d\n",
4420 dim->state = DIM_START_MEASURE;
4423 mutex_unlock(&rq->dim_lock);
4426 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
4428 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
4429 * or VIRTIO_NET_F_VQ_NOTF_COAL feature is negotiated.
4431 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
4434 if (ec->tx_max_coalesced_frames > 1 ||
4435 ec->rx_max_coalesced_frames != 1)
4441 static int virtnet_should_update_vq_weight(int dev_flags, int weight,
4442 int vq_weight, bool *should_update)
4444 if (weight ^ vq_weight) {
4445 if (dev_flags & IFF_UP)
4447 *should_update = true;
4453 static int virtnet_set_coalesce(struct net_device *dev,
4454 struct ethtool_coalesce *ec,
4455 struct kernel_ethtool_coalesce *kernel_coal,
4456 struct netlink_ext_ack *extack)
4458 struct virtnet_info *vi = netdev_priv(dev);
4459 int ret, queue_number, napi_weight;
4460 bool update_napi = false;
4462 /* Can't change NAPI weight if the link is up */
4463 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
4464 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) {
4465 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
4466 vi->sq[queue_number].napi.weight,
4472 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be
4473 * updated for the sake of simplicity, which might not be necessary
4479 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
4480 ret = virtnet_send_notf_coal_cmds(vi, ec);
4482 ret = virtnet_coal_params_supported(ec);
4488 for (; queue_number < vi->max_queue_pairs; queue_number++)
4489 vi->sq[queue_number].napi.weight = napi_weight;
4495 static int virtnet_get_coalesce(struct net_device *dev,
4496 struct ethtool_coalesce *ec,
4497 struct kernel_ethtool_coalesce *kernel_coal,
4498 struct netlink_ext_ack *extack)
4500 struct virtnet_info *vi = netdev_priv(dev);
4502 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
4503 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
4504 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
4505 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
4506 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
4507 ec->use_adaptive_rx_coalesce = vi->rx_dim_enabled;
4509 ec->rx_max_coalesced_frames = 1;
4511 if (vi->sq[0].napi.weight)
4512 ec->tx_max_coalesced_frames = 1;
4518 static int virtnet_set_per_queue_coalesce(struct net_device *dev,
4520 struct ethtool_coalesce *ec)
4522 struct virtnet_info *vi = netdev_priv(dev);
4523 int ret, napi_weight;
4524 bool update_napi = false;
4526 if (queue >= vi->max_queue_pairs)
4529 /* Can't change NAPI weight if the link is up */
4530 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
4531 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
4532 vi->sq[queue].napi.weight,
4537 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
4538 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
4540 ret = virtnet_coal_params_supported(ec);
4546 vi->sq[queue].napi.weight = napi_weight;
4551 static int virtnet_get_per_queue_coalesce(struct net_device *dev,
4553 struct ethtool_coalesce *ec)
4555 struct virtnet_info *vi = netdev_priv(dev);
4557 if (queue >= vi->max_queue_pairs)
4560 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
4561 mutex_lock(&vi->rq[queue].dim_lock);
4562 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
4563 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
4564 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
4565 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
4566 ec->use_adaptive_rx_coalesce = vi->rq[queue].dim_enabled;
4567 mutex_unlock(&vi->rq[queue].dim_lock);
4569 ec->rx_max_coalesced_frames = 1;
4571 if (vi->sq[queue].napi.weight)
4572 ec->tx_max_coalesced_frames = 1;
4578 static void virtnet_init_settings(struct net_device *dev)
4580 struct virtnet_info *vi = netdev_priv(dev);
4582 vi->speed = SPEED_UNKNOWN;
4583 vi->duplex = DUPLEX_UNKNOWN;
4586 static void virtnet_update_settings(struct virtnet_info *vi)
4591 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
4594 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
4596 if (ethtool_validate_speed(speed))
4599 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
4601 if (ethtool_validate_duplex(duplex))
4602 vi->duplex = duplex;
4605 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
4607 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
4610 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
4612 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
4615 static int virtnet_get_rxfh(struct net_device *dev,
4616 struct ethtool_rxfh_param *rxfh)
4618 struct virtnet_info *vi = netdev_priv(dev);
4622 for (i = 0; i < vi->rss_indir_table_size; ++i)
4623 rxfh->indir[i] = vi->rss.indirection_table[i];
4627 memcpy(rxfh->key, vi->rss.key, vi->rss_key_size);
4629 rxfh->hfunc = ETH_RSS_HASH_TOP;
4634 static int virtnet_set_rxfh(struct net_device *dev,
4635 struct ethtool_rxfh_param *rxfh,
4636 struct netlink_ext_ack *extack)
4638 struct virtnet_info *vi = netdev_priv(dev);
4639 bool update = false;
4642 if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
4643 rxfh->hfunc != ETH_RSS_HASH_TOP)
4650 for (i = 0; i < vi->rss_indir_table_size; ++i)
4651 vi->rss.indirection_table[i] = rxfh->indir[i];
4656 /* If either _F_HASH_REPORT or _F_RSS are negotiated, the
4657 * device provides hash calculation capabilities, that is,
4658 * hash_key is configured.
4660 if (!vi->has_rss && !vi->has_rss_hash_report)
4663 memcpy(vi->rss.key, rxfh->key, vi->rss_key_size);
4668 virtnet_commit_rss_command(vi);
4673 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
4675 struct virtnet_info *vi = netdev_priv(dev);
4678 switch (info->cmd) {
4679 case ETHTOOL_GRXRINGS:
4680 info->data = vi->curr_queue_pairs;
4683 virtnet_get_hashflow(vi, info);
4692 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
4694 struct virtnet_info *vi = netdev_priv(dev);
4697 switch (info->cmd) {
4699 if (!virtnet_set_hashflow(vi, info))
4710 static const struct ethtool_ops virtnet_ethtool_ops = {
4711 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
4712 ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
4713 .get_drvinfo = virtnet_get_drvinfo,
4714 .get_link = ethtool_op_get_link,
4715 .get_ringparam = virtnet_get_ringparam,
4716 .set_ringparam = virtnet_set_ringparam,
4717 .get_strings = virtnet_get_strings,
4718 .get_sset_count = virtnet_get_sset_count,
4719 .get_ethtool_stats = virtnet_get_ethtool_stats,
4720 .set_channels = virtnet_set_channels,
4721 .get_channels = virtnet_get_channels,
4722 .get_ts_info = ethtool_op_get_ts_info,
4723 .get_link_ksettings = virtnet_get_link_ksettings,
4724 .set_link_ksettings = virtnet_set_link_ksettings,
4725 .set_coalesce = virtnet_set_coalesce,
4726 .get_coalesce = virtnet_get_coalesce,
4727 .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
4728 .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
4729 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
4730 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
4731 .get_rxfh = virtnet_get_rxfh,
4732 .set_rxfh = virtnet_set_rxfh,
4733 .get_rxnfc = virtnet_get_rxnfc,
4734 .set_rxnfc = virtnet_set_rxnfc,
4737 static void virtnet_get_queue_stats_rx(struct net_device *dev, int i,
4738 struct netdev_queue_stats_rx *stats)
4740 struct virtnet_info *vi = netdev_priv(dev);
4741 struct receive_queue *rq = &vi->rq[i];
4742 struct virtnet_stats_ctx ctx = {0};
4744 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true);
4746 virtnet_get_hw_stats(vi, &ctx, i * 2);
4747 virtnet_fill_stats(vi, i * 2, &ctx, (void *)&rq->stats, true, 0);
4750 static void virtnet_get_queue_stats_tx(struct net_device *dev, int i,
4751 struct netdev_queue_stats_tx *stats)
4753 struct virtnet_info *vi = netdev_priv(dev);
4754 struct send_queue *sq = &vi->sq[i];
4755 struct virtnet_stats_ctx ctx = {0};
4757 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true);
4759 virtnet_get_hw_stats(vi, &ctx, i * 2 + 1);
4760 virtnet_fill_stats(vi, i * 2 + 1, &ctx, (void *)&sq->stats, true, 0);
4763 static void virtnet_get_base_stats(struct net_device *dev,
4764 struct netdev_queue_stats_rx *rx,
4765 struct netdev_queue_stats_tx *tx)
4767 struct virtnet_info *vi = netdev_priv(dev);
4769 /* The queue stats of the virtio-net will not be reset. So here we
4775 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4777 rx->hw_drop_overruns = 0;
4780 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4781 rx->csum_unnecessary = 0;
4786 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
4787 rx->hw_gro_packets = 0;
4788 rx->hw_gro_bytes = 0;
4789 rx->hw_gro_wire_packets = 0;
4790 rx->hw_gro_wire_bytes = 0;
4793 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED)
4794 rx->hw_drop_ratelimits = 0;
4801 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4803 tx->hw_drop_errors = 0;
4806 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
4811 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4812 tx->hw_gso_packets = 0;
4813 tx->hw_gso_bytes = 0;
4814 tx->hw_gso_wire_packets = 0;
4815 tx->hw_gso_wire_bytes = 0;
4818 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED)
4819 tx->hw_drop_ratelimits = 0;
4822 static const struct netdev_stat_ops virtnet_stat_ops = {
4823 .get_queue_stats_rx = virtnet_get_queue_stats_rx,
4824 .get_queue_stats_tx = virtnet_get_queue_stats_tx,
4825 .get_base_stats = virtnet_get_base_stats,
4828 static void virtnet_freeze_down(struct virtio_device *vdev)
4830 struct virtnet_info *vi = vdev->priv;
4832 /* Make sure no work handler is accessing the device */
4833 flush_work(&vi->config_work);
4834 disable_rx_mode_work(vi);
4835 flush_work(&vi->rx_mode_work);
4837 netif_tx_lock_bh(vi->dev);
4838 netif_device_detach(vi->dev);
4839 netif_tx_unlock_bh(vi->dev);
4840 if (netif_running(vi->dev))
4841 virtnet_close(vi->dev);
4844 static int init_vqs(struct virtnet_info *vi);
4846 static int virtnet_restore_up(struct virtio_device *vdev)
4848 struct virtnet_info *vi = vdev->priv;
4855 virtio_device_ready(vdev);
4857 enable_delayed_refill(vi);
4858 enable_rx_mode_work(vi);
4860 if (netif_running(vi->dev)) {
4861 err = virtnet_open(vi->dev);
4866 netif_tx_lock_bh(vi->dev);
4867 netif_device_attach(vi->dev);
4868 netif_tx_unlock_bh(vi->dev);
4872 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
4874 __virtio64 *_offloads __free(kfree) = NULL;
4875 struct scatterlist sg;
4877 _offloads = kzalloc(sizeof(*_offloads), GFP_KERNEL);
4881 *_offloads = cpu_to_virtio64(vi->vdev, offloads);
4883 sg_init_one(&sg, _offloads, sizeof(*_offloads));
4885 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
4886 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
4887 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
4894 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
4898 if (!vi->guest_offloads)
4901 return virtnet_set_guest_offloads(vi, offloads);
4904 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
4906 u64 offloads = vi->guest_offloads;
4908 if (!vi->guest_offloads)
4911 return virtnet_set_guest_offloads(vi, offloads);
4914 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
4915 struct netlink_ext_ack *extack)
4917 unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
4918 sizeof(struct skb_shared_info));
4919 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
4920 struct virtnet_info *vi = netdev_priv(dev);
4921 struct bpf_prog *old_prog;
4922 u16 xdp_qp = 0, curr_qp;
4925 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
4926 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
4927 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
4928 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
4929 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
4930 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
4931 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
4932 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
4933 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
4937 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
4938 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
4942 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
4943 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
4944 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
4948 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
4950 xdp_qp = nr_cpu_ids;
4952 /* XDP requires extra queues for XDP_TX */
4953 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
4954 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",
4955 curr_qp + xdp_qp, vi->max_queue_pairs);
4959 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
4960 if (!prog && !old_prog)
4964 bpf_prog_add(prog, vi->max_queue_pairs - 1);
4966 /* Make sure NAPI is not using any XDP TX queues for RX. */
4967 if (netif_running(dev)) {
4968 for (i = 0; i < vi->max_queue_pairs; i++) {
4969 napi_disable(&vi->rq[i].napi);
4970 virtnet_napi_tx_disable(&vi->sq[i].napi);
4975 for (i = 0; i < vi->max_queue_pairs; i++) {
4976 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
4978 virtnet_restore_guest_offloads(vi);
4983 err = virtnet_set_queues(vi, curr_qp + xdp_qp);
4986 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
4987 vi->xdp_queue_pairs = xdp_qp;
4990 vi->xdp_enabled = true;
4991 for (i = 0; i < vi->max_queue_pairs; i++) {
4992 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
4993 if (i == 0 && !old_prog)
4994 virtnet_clear_guest_offloads(vi);
4997 xdp_features_set_redirect_target(dev, true);
4999 xdp_features_clear_redirect_target(dev);
5000 vi->xdp_enabled = false;
5003 for (i = 0; i < vi->max_queue_pairs; i++) {
5005 bpf_prog_put(old_prog);
5006 if (netif_running(dev)) {
5007 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
5008 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
5017 virtnet_clear_guest_offloads(vi);
5018 for (i = 0; i < vi->max_queue_pairs; i++)
5019 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
5022 if (netif_running(dev)) {
5023 for (i = 0; i < vi->max_queue_pairs; i++) {
5024 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
5025 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
5030 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
5034 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
5036 switch (xdp->command) {
5037 case XDP_SETUP_PROG:
5038 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
5044 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
5047 struct virtnet_info *vi = netdev_priv(dev);
5050 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
5053 ret = snprintf(buf, len, "sby");
5060 static int virtnet_set_features(struct net_device *dev,
5061 netdev_features_t features)
5063 struct virtnet_info *vi = netdev_priv(dev);
5067 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
5068 if (vi->xdp_enabled)
5071 if (features & NETIF_F_GRO_HW)
5072 offloads = vi->guest_offloads_capable;
5074 offloads = vi->guest_offloads_capable &
5075 ~GUEST_OFFLOAD_GRO_HW_MASK;
5077 err = virtnet_set_guest_offloads(vi, offloads);
5080 vi->guest_offloads = offloads;
5083 if ((dev->features ^ features) & NETIF_F_RXHASH) {
5084 if (features & NETIF_F_RXHASH)
5085 vi->rss.hash_types = vi->rss_hash_types_saved;
5087 vi->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
5089 if (!virtnet_commit_rss_command(vi))
5096 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
5098 struct virtnet_info *priv = netdev_priv(dev);
5099 struct send_queue *sq = &priv->sq[txqueue];
5100 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
5102 u64_stats_update_begin(&sq->stats.syncp);
5103 u64_stats_inc(&sq->stats.tx_timeouts);
5104 u64_stats_update_end(&sq->stats.syncp);
5106 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
5107 txqueue, sq->name, sq->vq->index, sq->vq->name,
5108 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
5111 static const struct net_device_ops virtnet_netdev = {
5112 .ndo_open = virtnet_open,
5113 .ndo_stop = virtnet_close,
5114 .ndo_start_xmit = start_xmit,
5115 .ndo_validate_addr = eth_validate_addr,
5116 .ndo_set_mac_address = virtnet_set_mac_address,
5117 .ndo_set_rx_mode = virtnet_set_rx_mode,
5118 .ndo_get_stats64 = virtnet_stats,
5119 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
5120 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
5121 .ndo_bpf = virtnet_xdp,
5122 .ndo_xdp_xmit = virtnet_xdp_xmit,
5123 .ndo_features_check = passthru_features_check,
5124 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
5125 .ndo_set_features = virtnet_set_features,
5126 .ndo_tx_timeout = virtnet_tx_timeout,
5129 static void virtnet_config_changed_work(struct work_struct *work)
5131 struct virtnet_info *vi =
5132 container_of(work, struct virtnet_info, config_work);
5135 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
5136 struct virtio_net_config, status, &v) < 0)
5139 if (v & VIRTIO_NET_S_ANNOUNCE) {
5140 netdev_notify_peers(vi->dev);
5141 virtnet_ack_link_announce(vi);
5144 /* Ignore unknown (future) status bits */
5145 v &= VIRTIO_NET_S_LINK_UP;
5147 if (vi->status == v)
5152 if (vi->status & VIRTIO_NET_S_LINK_UP) {
5153 virtnet_update_settings(vi);
5154 netif_carrier_on(vi->dev);
5155 netif_tx_wake_all_queues(vi->dev);
5157 netif_carrier_off(vi->dev);
5158 netif_tx_stop_all_queues(vi->dev);
5162 static void virtnet_config_changed(struct virtio_device *vdev)
5164 struct virtnet_info *vi = vdev->priv;
5166 schedule_work(&vi->config_work);
5169 static void virtnet_free_queues(struct virtnet_info *vi)
5173 for (i = 0; i < vi->max_queue_pairs; i++) {
5174 __netif_napi_del(&vi->rq[i].napi);
5175 __netif_napi_del(&vi->sq[i].napi);
5178 /* We called __netif_napi_del(),
5179 * we need to respect an RCU grace period before freeing vi->rq
5188 static void _free_receive_bufs(struct virtnet_info *vi)
5190 struct bpf_prog *old_prog;
5193 for (i = 0; i < vi->max_queue_pairs; i++) {
5194 while (vi->rq[i].pages)
5195 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
5197 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
5198 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
5200 bpf_prog_put(old_prog);
5204 static void free_receive_bufs(struct virtnet_info *vi)
5207 _free_receive_bufs(vi);
5211 static void free_receive_page_frags(struct virtnet_info *vi)
5214 for (i = 0; i < vi->max_queue_pairs; i++)
5215 if (vi->rq[i].alloc_frag.page) {
5216 if (vi->rq[i].last_dma)
5217 virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0);
5218 put_page(vi->rq[i].alloc_frag.page);
5222 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
5224 if (!is_xdp_frame(buf))
5227 xdp_return_frame(ptr_to_xdp(buf));
5230 static void free_unused_bufs(struct virtnet_info *vi)
5235 for (i = 0; i < vi->max_queue_pairs; i++) {
5236 struct virtqueue *vq = vi->sq[i].vq;
5237 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
5238 virtnet_sq_free_unused_buf(vq, buf);
5242 for (i = 0; i < vi->max_queue_pairs; i++) {
5243 struct virtqueue *vq = vi->rq[i].vq;
5245 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
5246 virtnet_rq_unmap_free_buf(vq, buf);
5251 static void virtnet_del_vqs(struct virtnet_info *vi)
5253 struct virtio_device *vdev = vi->vdev;
5255 virtnet_clean_affinity(vi);
5257 vdev->config->del_vqs(vdev);
5259 virtnet_free_queues(vi);
5262 /* How large should a single buffer be so a queue full of these can fit at
5263 * least one full packet?
5264 * Logic below assumes the mergeable buffer header is used.
5266 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
5268 const unsigned int hdr_len = vi->hdr_len;
5269 unsigned int rq_size = virtqueue_get_vring_size(vq);
5270 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
5271 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
5272 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
5274 return max(max(min_buf_len, hdr_len) - hdr_len,
5275 (unsigned int)GOOD_PACKET_LEN);
5278 static int virtnet_find_vqs(struct virtnet_info *vi)
5280 vq_callback_t **callbacks;
5281 struct virtqueue **vqs;
5288 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
5289 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
5290 * possible control vq.
5292 total_vqs = vi->max_queue_pairs * 2 +
5293 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
5295 /* Allocate space for find_vqs parameters */
5296 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
5299 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
5302 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
5305 if (!vi->big_packets || vi->mergeable_rx_bufs) {
5306 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
5313 /* Parameters for control virtqueue, if any */
5315 callbacks[total_vqs - 1] = NULL;
5316 names[total_vqs - 1] = "control";
5319 /* Allocate/initialize parameters for send/receive virtqueues */
5320 for (i = 0; i < vi->max_queue_pairs; i++) {
5321 callbacks[rxq2vq(i)] = skb_recv_done;
5322 callbacks[txq2vq(i)] = skb_xmit_done;
5323 sprintf(vi->rq[i].name, "input.%u", i);
5324 sprintf(vi->sq[i].name, "output.%u", i);
5325 names[rxq2vq(i)] = vi->rq[i].name;
5326 names[txq2vq(i)] = vi->sq[i].name;
5328 ctx[rxq2vq(i)] = true;
5331 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
5337 vi->cvq = vqs[total_vqs - 1];
5338 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
5339 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
5342 for (i = 0; i < vi->max_queue_pairs; i++) {
5343 vi->rq[i].vq = vqs[rxq2vq(i)];
5344 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
5345 vi->sq[i].vq = vqs[txq2vq(i)];
5348 /* run here: ret == 0. */
5363 static int virtnet_alloc_queues(struct virtnet_info *vi)
5368 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
5374 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
5377 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
5381 INIT_DELAYED_WORK(&vi->refill, refill_work);
5382 for (i = 0; i < vi->max_queue_pairs; i++) {
5383 vi->rq[i].pages = NULL;
5384 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
5386 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
5388 napi_tx ? napi_weight : 0);
5390 INIT_WORK(&vi->rq[i].dim.work, virtnet_rx_dim_work);
5391 vi->rq[i].dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
5393 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
5394 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
5395 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
5397 u64_stats_init(&vi->rq[i].stats.syncp);
5398 u64_stats_init(&vi->sq[i].stats.syncp);
5399 mutex_init(&vi->rq[i].dim_lock);
5412 static int init_vqs(struct virtnet_info *vi)
5416 /* Allocate send & receive queues */
5417 ret = virtnet_alloc_queues(vi);
5421 ret = virtnet_find_vqs(vi);
5425 virtnet_rq_set_premapped(vi);
5428 virtnet_set_affinity(vi);
5434 virtnet_free_queues(vi);
5440 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
5443 struct virtnet_info *vi = netdev_priv(queue->dev);
5444 unsigned int queue_index = get_netdev_rx_queue_index(queue);
5445 unsigned int headroom = virtnet_get_headroom(vi);
5446 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
5447 struct ewma_pkt_len *avg;
5449 BUG_ON(queue_index >= vi->max_queue_pairs);
5450 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
5451 return sprintf(buf, "%u\n",
5452 get_mergeable_buf_len(&vi->rq[queue_index], avg,
5453 SKB_DATA_ALIGN(headroom + tailroom)));
5456 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
5457 __ATTR_RO(mergeable_rx_buffer_size);
5459 static struct attribute *virtio_net_mrg_rx_attrs[] = {
5460 &mergeable_rx_buffer_size_attribute.attr,
5464 static const struct attribute_group virtio_net_mrg_rx_group = {
5465 .name = "virtio_net",
5466 .attrs = virtio_net_mrg_rx_attrs
5470 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
5472 const char *fname, const char *dname)
5474 if (!virtio_has_feature(vdev, fbit))
5477 dev_err(&vdev->dev, "device advertises feature %s but not %s",
5483 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
5484 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
5486 static bool virtnet_validate_features(struct virtio_device *vdev)
5488 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
5489 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
5490 "VIRTIO_NET_F_CTRL_VQ") ||
5491 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
5492 "VIRTIO_NET_F_CTRL_VQ") ||
5493 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
5494 "VIRTIO_NET_F_CTRL_VQ") ||
5495 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
5496 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
5497 "VIRTIO_NET_F_CTRL_VQ") ||
5498 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
5499 "VIRTIO_NET_F_CTRL_VQ") ||
5500 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
5501 "VIRTIO_NET_F_CTRL_VQ") ||
5502 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
5503 "VIRTIO_NET_F_CTRL_VQ") ||
5504 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL,
5505 "VIRTIO_NET_F_CTRL_VQ"))) {
5512 #define MIN_MTU ETH_MIN_MTU
5513 #define MAX_MTU ETH_MAX_MTU
5515 static int virtnet_validate(struct virtio_device *vdev)
5517 if (!vdev->config->get) {
5518 dev_err(&vdev->dev, "%s failure: config access disabled\n",
5523 if (!virtnet_validate_features(vdev))
5526 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
5527 int mtu = virtio_cread16(vdev,
5528 offsetof(struct virtio_net_config,
5531 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
5534 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
5535 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
5536 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
5537 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
5543 static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
5545 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
5546 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
5547 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
5548 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
5549 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
5550 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
5553 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
5555 bool guest_gso = virtnet_check_guest_gso(vi);
5557 /* If device can receive ANY guest GSO packets, regardless of mtu,
5558 * allocate packets of maximum size, otherwise limit it to only
5559 * mtu size worth only.
5561 if (mtu > ETH_DATA_LEN || guest_gso) {
5562 vi->big_packets = true;
5563 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
5567 #define VIRTIO_NET_HASH_REPORT_MAX_TABLE 10
5568 static enum xdp_rss_hash_type
5569 virtnet_xdp_rss_type[VIRTIO_NET_HASH_REPORT_MAX_TABLE] = {
5570 [VIRTIO_NET_HASH_REPORT_NONE] = XDP_RSS_TYPE_NONE,
5571 [VIRTIO_NET_HASH_REPORT_IPv4] = XDP_RSS_TYPE_L3_IPV4,
5572 [VIRTIO_NET_HASH_REPORT_TCPv4] = XDP_RSS_TYPE_L4_IPV4_TCP,
5573 [VIRTIO_NET_HASH_REPORT_UDPv4] = XDP_RSS_TYPE_L4_IPV4_UDP,
5574 [VIRTIO_NET_HASH_REPORT_IPv6] = XDP_RSS_TYPE_L3_IPV6,
5575 [VIRTIO_NET_HASH_REPORT_TCPv6] = XDP_RSS_TYPE_L4_IPV6_TCP,
5576 [VIRTIO_NET_HASH_REPORT_UDPv6] = XDP_RSS_TYPE_L4_IPV6_UDP,
5577 [VIRTIO_NET_HASH_REPORT_IPv6_EX] = XDP_RSS_TYPE_L3_IPV6_EX,
5578 [VIRTIO_NET_HASH_REPORT_TCPv6_EX] = XDP_RSS_TYPE_L4_IPV6_TCP_EX,
5579 [VIRTIO_NET_HASH_REPORT_UDPv6_EX] = XDP_RSS_TYPE_L4_IPV6_UDP_EX
5582 static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
5583 enum xdp_rss_hash_type *rss_type)
5585 const struct xdp_buff *xdp = (void *)_ctx;
5586 struct virtio_net_hdr_v1_hash *hdr_hash;
5587 struct virtnet_info *vi;
5590 if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
5593 vi = netdev_priv(xdp->rxq->dev);
5594 hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
5595 hash_report = __le16_to_cpu(hdr_hash->hash_report);
5597 if (hash_report >= VIRTIO_NET_HASH_REPORT_MAX_TABLE)
5598 hash_report = VIRTIO_NET_HASH_REPORT_NONE;
5600 *rss_type = virtnet_xdp_rss_type[hash_report];
5601 *hash = __le32_to_cpu(hdr_hash->hash_value);
5605 static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
5606 .xmo_rx_hash = virtnet_xdp_rx_hash,
5609 static int virtnet_probe(struct virtio_device *vdev)
5611 int i, err = -ENOMEM;
5612 struct net_device *dev;
5613 struct virtnet_info *vi;
5614 u16 max_queue_pairs;
5617 /* Find if host supports multiqueue/rss virtio_net device */
5618 max_queue_pairs = 1;
5619 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
5621 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
5623 /* We need at least 2 queue's */
5624 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
5625 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
5626 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
5627 max_queue_pairs = 1;
5629 /* Allocate ourselves a network device with room for our info */
5630 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
5634 /* Set up network device as normal. */
5635 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
5636 IFF_TX_SKB_NO_LINEAR;
5637 dev->netdev_ops = &virtnet_netdev;
5638 dev->stat_ops = &virtnet_stat_ops;
5639 dev->features = NETIF_F_HIGHDMA;
5641 dev->ethtool_ops = &virtnet_ethtool_ops;
5642 SET_NETDEV_DEV(dev, &vdev->dev);
5644 /* Do we support "hardware" checksums? */
5645 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
5646 /* This opens up the world of extra features. */
5647 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
5649 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
5651 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
5652 dev->hw_features |= NETIF_F_TSO
5653 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
5655 /* Individual feature bits: what can host handle? */
5656 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
5657 dev->hw_features |= NETIF_F_TSO;
5658 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
5659 dev->hw_features |= NETIF_F_TSO6;
5660 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
5661 dev->hw_features |= NETIF_F_TSO_ECN;
5662 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
5663 dev->hw_features |= NETIF_F_GSO_UDP_L4;
5665 dev->features |= NETIF_F_GSO_ROBUST;
5668 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
5669 /* (!csum && gso) case will be fixed by register_netdev() */
5671 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
5672 dev->features |= NETIF_F_RXCSUM;
5673 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
5674 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
5675 dev->features |= NETIF_F_GRO_HW;
5676 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
5677 dev->hw_features |= NETIF_F_GRO_HW;
5679 dev->vlan_features = dev->features;
5680 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
5682 /* MTU range: 68 - 65535 */
5683 dev->min_mtu = MIN_MTU;
5684 dev->max_mtu = MAX_MTU;
5686 /* Configuration may specify what MAC to use. Otherwise random. */
5687 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
5690 virtio_cread_bytes(vdev,
5691 offsetof(struct virtio_net_config, mac),
5693 eth_hw_addr_set(dev, addr);
5695 eth_hw_addr_random(dev);
5696 dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
5700 /* Set up our device-specific information */
5701 vi = netdev_priv(dev);
5706 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
5707 INIT_WORK(&vi->rx_mode_work, virtnet_rx_mode_work);
5708 spin_lock_init(&vi->refill_lock);
5710 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
5711 vi->mergeable_rx_bufs = true;
5712 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
5715 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
5716 vi->has_rss_hash_report = true;
5718 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) {
5721 vi->rss_indir_table_size =
5722 virtio_cread16(vdev, offsetof(struct virtio_net_config,
5723 rss_max_indirection_table_length));
5726 if (vi->has_rss || vi->has_rss_hash_report) {
5728 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
5730 vi->rss_hash_types_supported =
5731 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
5732 vi->rss_hash_types_supported &=
5733 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
5734 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
5735 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
5737 dev->hw_features |= NETIF_F_RXHASH;
5738 dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
5741 if (vi->has_rss_hash_report)
5742 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
5743 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
5744 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
5745 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
5747 vi->hdr_len = sizeof(struct virtio_net_hdr);
5749 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
5750 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
5751 vi->any_header_sg = true;
5753 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
5756 mutex_init(&vi->cvq_lock);
5758 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
5759 mtu = virtio_cread16(vdev,
5760 offsetof(struct virtio_net_config,
5762 if (mtu < dev->min_mtu) {
5763 /* Should never trigger: MTU was previously validated
5764 * in virtnet_validate.
5767 "device MTU appears to have changed it is now %d < %d",
5777 virtnet_set_big_packets(vi, mtu);
5779 if (vi->any_header_sg)
5780 dev->needed_headroom = vi->hdr_len;
5782 /* Enable multiqueue by default */
5783 if (num_online_cpus() >= max_queue_pairs)
5784 vi->curr_queue_pairs = max_queue_pairs;
5786 vi->curr_queue_pairs = num_online_cpus();
5787 vi->max_queue_pairs = max_queue_pairs;
5789 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
5794 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
5795 vi->intr_coal_rx.max_usecs = 0;
5796 vi->intr_coal_tx.max_usecs = 0;
5797 vi->intr_coal_rx.max_packets = 0;
5799 /* Keep the default values of the coalescing parameters
5800 * aligned with the default napi_tx state.
5802 if (vi->sq[0].napi.weight)
5803 vi->intr_coal_tx.max_packets = 1;
5805 vi->intr_coal_tx.max_packets = 0;
5808 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
5809 /* The reason is the same as VIRTIO_NET_F_NOTF_COAL. */
5810 for (i = 0; i < vi->max_queue_pairs; i++)
5811 if (vi->sq[i].napi.weight)
5812 vi->sq[i].intr_coal.max_packets = 1;
5816 if (vi->mergeable_rx_bufs)
5817 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
5819 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
5820 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
5822 virtnet_init_settings(dev);
5824 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
5825 vi->failover = net_failover_create(vi->dev);
5826 if (IS_ERR(vi->failover)) {
5827 err = PTR_ERR(vi->failover);
5832 if (vi->has_rss || vi->has_rss_hash_report)
5833 virtnet_init_default_rss(vi);
5835 enable_rx_mode_work(vi);
5837 /* serialize netdev register + virtio_device_ready() with ndo_open() */
5840 err = register_netdevice(dev);
5842 pr_debug("virtio_net: registering device failed\n");
5847 virtio_device_ready(vdev);
5849 virtnet_set_queues(vi, vi->curr_queue_pairs);
5851 /* a random MAC address has been assigned, notify the device.
5852 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
5853 * because many devices work fine without getting MAC explicitly
5855 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
5856 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
5857 struct scatterlist sg;
5859 sg_init_one(&sg, dev->dev_addr, dev->addr_len);
5860 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
5861 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
5862 pr_debug("virtio_net: setting MAC address failed\n");
5865 goto free_unregister_netdev;
5869 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS)) {
5870 struct virtio_net_stats_capabilities *stats_cap __free(kfree) = NULL;
5871 struct scatterlist sg;
5874 stats_cap = kzalloc(sizeof(*stats_cap), GFP_KERNEL);
5878 goto free_unregister_netdev;
5881 sg_init_one(&sg, stats_cap, sizeof(*stats_cap));
5883 if (!virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
5884 VIRTIO_NET_CTRL_STATS_QUERY,
5886 pr_debug("virtio_net: fail to get stats capability\n");
5889 goto free_unregister_netdev;
5892 v = stats_cap->supported_stats_types[0];
5893 vi->device_stats_cap = le64_to_cpu(v);
5898 err = virtnet_cpu_notif_add(vi);
5900 pr_debug("virtio_net: registering cpu notifier failed\n");
5901 goto free_unregister_netdev;
5904 /* Assume link up if device can't report link status,
5905 otherwise get link status from config. */
5906 netif_carrier_off(dev);
5907 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
5908 schedule_work(&vi->config_work);
5910 vi->status = VIRTIO_NET_S_LINK_UP;
5911 virtnet_update_settings(vi);
5912 netif_carrier_on(dev);
5915 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
5916 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
5917 set_bit(guest_offloads[i], &vi->guest_offloads);
5918 vi->guest_offloads_capable = vi->guest_offloads;
5920 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
5921 dev->name, max_queue_pairs);
5925 free_unregister_netdev:
5926 unregister_netdev(dev);
5928 net_failover_destroy(vi->failover);
5930 virtio_reset_device(vdev);
5931 cancel_delayed_work_sync(&vi->refill);
5932 free_receive_page_frags(vi);
5933 virtnet_del_vqs(vi);
5939 static void remove_vq_common(struct virtnet_info *vi)
5941 virtio_reset_device(vi->vdev);
5943 /* Free unused buffers in both send and recv, if any. */
5944 free_unused_bufs(vi);
5946 free_receive_bufs(vi);
5948 free_receive_page_frags(vi);
5950 virtnet_del_vqs(vi);
5953 static void virtnet_remove(struct virtio_device *vdev)
5955 struct virtnet_info *vi = vdev->priv;
5957 virtnet_cpu_notif_remove(vi);
5959 /* Make sure no work handler is accessing the device. */
5960 flush_work(&vi->config_work);
5961 disable_rx_mode_work(vi);
5962 flush_work(&vi->rx_mode_work);
5964 unregister_netdev(vi->dev);
5966 net_failover_destroy(vi->failover);
5968 remove_vq_common(vi);
5970 free_netdev(vi->dev);
5973 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
5975 struct virtnet_info *vi = vdev->priv;
5977 virtnet_cpu_notif_remove(vi);
5978 virtnet_freeze_down(vdev);
5979 remove_vq_common(vi);
5984 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
5986 struct virtnet_info *vi = vdev->priv;
5989 err = virtnet_restore_up(vdev);
5992 virtnet_set_queues(vi, vi->curr_queue_pairs);
5994 err = virtnet_cpu_notif_add(vi);
5996 virtnet_freeze_down(vdev);
5997 remove_vq_common(vi);
6004 static struct virtio_device_id id_table[] = {
6005 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
6009 #define VIRTNET_FEATURES \
6010 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
6012 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
6013 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
6014 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
6015 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
6016 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
6017 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
6018 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
6019 VIRTIO_NET_F_CTRL_MAC_ADDR, \
6020 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
6021 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
6022 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
6023 VIRTIO_NET_F_VQ_NOTF_COAL, \
6024 VIRTIO_NET_F_GUEST_HDRLEN, VIRTIO_NET_F_DEVICE_STATS
6026 static unsigned int features[] = {
6030 static unsigned int features_legacy[] = {
6033 VIRTIO_F_ANY_LAYOUT,
6036 static struct virtio_driver virtio_net_driver = {
6037 .feature_table = features,
6038 .feature_table_size = ARRAY_SIZE(features),
6039 .feature_table_legacy = features_legacy,
6040 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
6041 .driver.name = KBUILD_MODNAME,
6042 .driver.owner = THIS_MODULE,
6043 .id_table = id_table,
6044 .validate = virtnet_validate,
6045 .probe = virtnet_probe,
6046 .remove = virtnet_remove,
6047 .config_changed = virtnet_config_changed,
6048 #ifdef CONFIG_PM_SLEEP
6049 .freeze = virtnet_freeze,
6050 .restore = virtnet_restore,
6054 static __init int virtio_net_driver_init(void)
6058 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
6060 virtnet_cpu_down_prep);
6063 virtionet_online = ret;
6064 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
6065 NULL, virtnet_cpu_dead);
6068 ret = register_virtio_driver(&virtio_net_driver);
6073 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
6075 cpuhp_remove_multi_state(virtionet_online);
6079 module_init(virtio_net_driver_init);
6081 static __exit void virtio_net_driver_exit(void)
6083 unregister_virtio_driver(&virtio_net_driver);
6084 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
6085 cpuhp_remove_multi_state(virtionet_online);
6087 module_exit(virtio_net_driver_exit);
6089 MODULE_DEVICE_TABLE(virtio, id_table);
6090 MODULE_DESCRIPTION("Virtio network driver");
6091 MODULE_LICENSE("GPL");