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>
28 #include <net/xdp_sock_drv.h>
30 static int napi_weight = NAPI_POLL_WEIGHT;
31 module_param(napi_weight, int, 0444);
33 static bool csum = true, gso = true, napi_tx = true;
34 module_param(csum, bool, 0444);
35 module_param(gso, bool, 0444);
36 module_param(napi_tx, bool, 0644);
38 /* FIXME: MTU in config. */
39 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
40 #define GOOD_COPY_LEN 128
42 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
44 /* Separating two types of XDP xmit */
45 #define VIRTIO_XDP_TX BIT(0)
46 #define VIRTIO_XDP_REDIR BIT(1)
48 #define VIRTIO_XDP_FLAG BIT(0)
49 #define VIRTIO_ORPHAN_FLAG BIT(1)
51 /* RX packet size EWMA. The average packet size is used to determine the packet
52 * buffer size when refilling RX rings. As the entire RX ring may be refilled
53 * at once, the weight is chosen so that the EWMA will be insensitive to short-
54 * term, transient changes in packet size.
56 DECLARE_EWMA(pkt_len, 0, 64)
58 #define VIRTNET_DRIVER_VERSION "1.0.0"
60 static const unsigned long guest_offloads[] = {
61 VIRTIO_NET_F_GUEST_TSO4,
62 VIRTIO_NET_F_GUEST_TSO6,
63 VIRTIO_NET_F_GUEST_ECN,
64 VIRTIO_NET_F_GUEST_UFO,
65 VIRTIO_NET_F_GUEST_CSUM,
66 VIRTIO_NET_F_GUEST_USO4,
67 VIRTIO_NET_F_GUEST_USO6,
68 VIRTIO_NET_F_GUEST_HDRLEN
71 #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
72 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
73 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
74 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
75 (1ULL << VIRTIO_NET_F_GUEST_USO4) | \
76 (1ULL << VIRTIO_NET_F_GUEST_USO6))
78 struct virtnet_stat_desc {
79 char desc[ETH_GSTRING_LEN];
84 struct virtnet_sq_free_stats {
91 struct virtnet_sq_stats {
92 struct u64_stats_sync syncp;
96 u64_stats_t xdp_tx_drops;
98 u64_stats_t tx_timeouts;
103 struct virtnet_rq_stats {
104 struct u64_stats_sync syncp;
108 u64_stats_t xdp_packets;
110 u64_stats_t xdp_redirects;
111 u64_stats_t xdp_drops;
115 #define VIRTNET_SQ_STAT(name, m) {name, offsetof(struct virtnet_sq_stats, m), -1}
116 #define VIRTNET_RQ_STAT(name, m) {name, offsetof(struct virtnet_rq_stats, m), -1}
118 #define VIRTNET_SQ_STAT_QSTAT(name, m) \
121 offsetof(struct virtnet_sq_stats, m), \
122 offsetof(struct netdev_queue_stats_tx, m), \
125 #define VIRTNET_RQ_STAT_QSTAT(name, m) \
128 offsetof(struct virtnet_rq_stats, m), \
129 offsetof(struct netdev_queue_stats_rx, m), \
132 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
133 VIRTNET_SQ_STAT("xdp_tx", xdp_tx),
134 VIRTNET_SQ_STAT("xdp_tx_drops", xdp_tx_drops),
135 VIRTNET_SQ_STAT("kicks", kicks),
136 VIRTNET_SQ_STAT("tx_timeouts", tx_timeouts),
139 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
140 VIRTNET_RQ_STAT("drops", drops),
141 VIRTNET_RQ_STAT("xdp_packets", xdp_packets),
142 VIRTNET_RQ_STAT("xdp_tx", xdp_tx),
143 VIRTNET_RQ_STAT("xdp_redirects", xdp_redirects),
144 VIRTNET_RQ_STAT("xdp_drops", xdp_drops),
145 VIRTNET_RQ_STAT("kicks", kicks),
148 static const struct virtnet_stat_desc virtnet_sq_stats_desc_qstat[] = {
149 VIRTNET_SQ_STAT_QSTAT("packets", packets),
150 VIRTNET_SQ_STAT_QSTAT("bytes", bytes),
151 VIRTNET_SQ_STAT_QSTAT("stop", stop),
152 VIRTNET_SQ_STAT_QSTAT("wake", wake),
155 static const struct virtnet_stat_desc virtnet_rq_stats_desc_qstat[] = {
156 VIRTNET_RQ_STAT_QSTAT("packets", packets),
157 VIRTNET_RQ_STAT_QSTAT("bytes", bytes),
160 #define VIRTNET_STATS_DESC_CQ(name) \
161 {#name, offsetof(struct virtio_net_stats_cvq, name), -1}
163 #define VIRTNET_STATS_DESC_RX(class, name) \
164 {#name, offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), -1}
166 #define VIRTNET_STATS_DESC_TX(class, name) \
167 {#name, offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), -1}
170 static const struct virtnet_stat_desc virtnet_stats_cvq_desc[] = {
171 VIRTNET_STATS_DESC_CQ(command_num),
172 VIRTNET_STATS_DESC_CQ(ok_num),
175 static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc[] = {
176 VIRTNET_STATS_DESC_RX(basic, packets),
177 VIRTNET_STATS_DESC_RX(basic, bytes),
179 VIRTNET_STATS_DESC_RX(basic, notifications),
180 VIRTNET_STATS_DESC_RX(basic, interrupts),
183 static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc[] = {
184 VIRTNET_STATS_DESC_TX(basic, packets),
185 VIRTNET_STATS_DESC_TX(basic, bytes),
187 VIRTNET_STATS_DESC_TX(basic, notifications),
188 VIRTNET_STATS_DESC_TX(basic, interrupts),
191 static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc[] = {
192 VIRTNET_STATS_DESC_RX(csum, needs_csum),
195 static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc[] = {
196 VIRTNET_STATS_DESC_TX(gso, gso_packets_noseg),
197 VIRTNET_STATS_DESC_TX(gso, gso_bytes_noseg),
200 static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc[] = {
201 VIRTNET_STATS_DESC_RX(speed, ratelimit_bytes),
204 static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc[] = {
205 VIRTNET_STATS_DESC_TX(speed, ratelimit_bytes),
208 #define VIRTNET_STATS_DESC_RX_QSTAT(class, name, qstat_field) \
211 offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), \
212 offsetof(struct netdev_queue_stats_rx, qstat_field), \
215 #define VIRTNET_STATS_DESC_TX_QSTAT(class, name, qstat_field) \
218 offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), \
219 offsetof(struct netdev_queue_stats_tx, qstat_field), \
222 static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc_qstat[] = {
223 VIRTNET_STATS_DESC_RX_QSTAT(basic, drops, hw_drops),
224 VIRTNET_STATS_DESC_RX_QSTAT(basic, drop_overruns, hw_drop_overruns),
227 static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc_qstat[] = {
228 VIRTNET_STATS_DESC_TX_QSTAT(basic, drops, hw_drops),
229 VIRTNET_STATS_DESC_TX_QSTAT(basic, drop_malformed, hw_drop_errors),
232 static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc_qstat[] = {
233 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_valid, csum_unnecessary),
234 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_none, csum_none),
235 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_bad, csum_bad),
238 static const struct virtnet_stat_desc virtnet_stats_tx_csum_desc_qstat[] = {
239 VIRTNET_STATS_DESC_TX_QSTAT(csum, csum_none, csum_none),
240 VIRTNET_STATS_DESC_TX_QSTAT(csum, needs_csum, needs_csum),
243 static const struct virtnet_stat_desc virtnet_stats_rx_gso_desc_qstat[] = {
244 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets, hw_gro_packets),
245 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes, hw_gro_bytes),
246 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets_coalesced, hw_gro_wire_packets),
247 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes_coalesced, hw_gro_wire_bytes),
250 static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc_qstat[] = {
251 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_packets, hw_gso_packets),
252 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_bytes, hw_gso_bytes),
253 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments, hw_gso_wire_packets),
254 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments_bytes, hw_gso_wire_bytes),
257 static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc_qstat[] = {
258 VIRTNET_STATS_DESC_RX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
261 static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = {
262 VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
265 #define VIRTNET_Q_TYPE_RX 0
266 #define VIRTNET_Q_TYPE_TX 1
267 #define VIRTNET_Q_TYPE_CQ 2
269 struct virtnet_interrupt_coalesce {
274 /* The dma information of pages allocated at a time. */
275 struct virtnet_rq_dma {
282 /* Internal representation of a send virtqueue */
284 /* Virtqueue associated with this send _queue */
285 struct virtqueue *vq;
287 /* TX: fragments + linear part + virtio header */
288 struct scatterlist sg[MAX_SKB_FRAGS + 2];
290 /* Name of the send queue: output.$index */
293 struct virtnet_sq_stats stats;
295 struct virtnet_interrupt_coalesce intr_coal;
297 struct napi_struct napi;
299 /* Record whether sq is in reset state. */
303 /* Internal representation of a receive virtqueue */
304 struct receive_queue {
305 /* Virtqueue associated with this receive_queue */
306 struct virtqueue *vq;
308 struct napi_struct napi;
310 struct bpf_prog __rcu *xdp_prog;
312 struct virtnet_rq_stats stats;
314 /* The number of rx notifications */
317 /* Is dynamic interrupt moderation enabled? */
320 /* Used to protect dim_enabled and inter_coal */
321 struct mutex dim_lock;
323 /* Dynamic Interrupt Moderation */
328 struct virtnet_interrupt_coalesce intr_coal;
330 /* Chain pages by the private ptr. */
333 /* Average packet length for mergeable receive buffers. */
334 struct ewma_pkt_len mrg_avg_pkt_len;
336 /* Page frag for packet buffer allocation. */
337 struct page_frag alloc_frag;
339 /* RX: fragments + linear part + virtio header */
340 struct scatterlist sg[MAX_SKB_FRAGS + 2];
342 /* Min single buffer size for mergeable buffers case. */
343 unsigned int min_buf_len;
345 /* Name of this receive queue: input.$index */
348 struct xdp_rxq_info xdp_rxq;
350 /* Record the last dma info to free after new pages is allocated. */
351 struct virtnet_rq_dma *last_dma;
353 struct xsk_buff_pool *xsk_pool;
355 /* xdp rxq used by xsk */
356 struct xdp_rxq_info xsk_rxq_info;
358 struct xdp_buff **xsk_buffs;
361 /* This structure can contain rss message with maximum settings for indirection table and keysize
362 * Note, that default structure that describes RSS configuration virtio_net_rss_config
363 * contains same info but can't handle table values.
364 * In any case, structure would be passed to virtio hw through sg_buf split by parts
365 * because table sizes may be differ according to the device configuration.
367 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
368 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
369 struct virtio_net_ctrl_rss {
371 u16 indirection_table_mask;
372 u16 unclassified_queue;
373 u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
376 u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
379 /* Control VQ buffers: protected by the rtnl lock */
381 struct virtio_net_ctrl_hdr hdr;
382 virtio_net_ctrl_ack status;
385 struct virtnet_info {
386 struct virtio_device *vdev;
387 struct virtqueue *cvq;
388 struct net_device *dev;
389 struct send_queue *sq;
390 struct receive_queue *rq;
393 /* Max # of queue pairs supported by the device */
396 /* # of queue pairs currently used by the driver */
397 u16 curr_queue_pairs;
399 /* # of XDP queue pairs currently used by the driver */
402 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
405 /* I like... big packets and I cannot lie! */
408 /* number of sg entries allocated for big packets */
409 unsigned int big_packets_num_skbfrags;
411 /* Host will merge rx buffers for big packets (shake it! shake it!) */
412 bool mergeable_rx_bufs;
414 /* Host supports rss and/or hash report */
416 bool has_rss_hash_report;
418 u16 rss_indir_table_size;
419 u32 rss_hash_types_supported;
420 u32 rss_hash_types_saved;
421 struct virtio_net_ctrl_rss rss;
423 /* Has control virtqueue */
426 /* Lock to protect the control VQ */
427 struct mutex cvq_lock;
429 /* Host can handle any s/g split between our header and packet data */
432 /* Packet virtio header size */
435 /* Work struct for delayed refilling if we run low on memory. */
436 struct delayed_work refill;
438 /* Is delayed refill enabled? */
441 /* The lock to synchronize the access to refill_enabled */
442 spinlock_t refill_lock;
444 /* Work struct for config space updates */
445 struct work_struct config_work;
447 /* Work struct for setting rx mode */
448 struct work_struct rx_mode_work;
450 /* OK to queue work setting RX mode? */
451 bool rx_mode_work_enabled;
453 /* Does the affinity hint is set for virtqueues? */
454 bool affinity_hint_set;
456 /* CPU hotplug instances for online & dead */
457 struct hlist_node node;
458 struct hlist_node node_dead;
460 struct control_buf *ctrl;
462 /* Ethtool settings */
466 /* Is rx dynamic interrupt moderation enabled? */
469 /* Interrupt coalescing settings */
470 struct virtnet_interrupt_coalesce intr_coal_tx;
471 struct virtnet_interrupt_coalesce intr_coal_rx;
473 unsigned long guest_offloads;
474 unsigned long guest_offloads_capable;
476 /* failover when STANDBY feature enabled */
477 struct failover *failover;
479 u64 device_stats_cap;
482 struct padded_vnet_hdr {
483 struct virtio_net_hdr_v1_hash hdr;
485 * hdr is in a separate sg buffer, and data sg buffer shares same page
486 * with this header sg. This padding makes next sg 16 byte aligned
492 struct virtio_net_common_hdr {
494 struct virtio_net_hdr hdr;
495 struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
496 struct virtio_net_hdr_v1_hash hash_v1_hdr;
500 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
501 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
502 struct net_device *dev,
503 unsigned int *xdp_xmit,
504 struct virtnet_rq_stats *stats);
505 static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
506 struct sk_buff *skb, u8 flags);
507 static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
508 struct sk_buff *curr_skb,
509 struct page *page, void *buf,
510 int len, int truesize);
512 static bool is_xdp_frame(void *ptr)
514 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
517 static void *xdp_to_ptr(struct xdp_frame *ptr)
519 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
522 static struct xdp_frame *ptr_to_xdp(void *ptr)
524 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
527 static bool is_orphan_skb(void *ptr)
529 return (unsigned long)ptr & VIRTIO_ORPHAN_FLAG;
532 static void *skb_to_ptr(struct sk_buff *skb, bool orphan)
534 return (void *)((unsigned long)skb | (orphan ? VIRTIO_ORPHAN_FLAG : 0));
537 static struct sk_buff *ptr_to_skb(void *ptr)
539 return (struct sk_buff *)((unsigned long)ptr & ~VIRTIO_ORPHAN_FLAG);
542 static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
543 bool in_napi, struct virtnet_sq_free_stats *stats)
548 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
549 if (!is_xdp_frame(ptr)) {
550 struct sk_buff *skb = ptr_to_skb(ptr);
552 pr_debug("Sent skb %p\n", skb);
554 if (is_orphan_skb(ptr)) {
556 stats->bytes += skb->len;
558 stats->napi_packets++;
559 stats->napi_bytes += skb->len;
561 napi_consume_skb(skb, in_napi);
563 struct xdp_frame *frame = ptr_to_xdp(ptr);
566 stats->bytes += xdp_get_frame_len(frame);
567 xdp_return_frame(frame);
570 netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
573 /* Converting between virtqueue no. and kernel tx/rx queue no.
574 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
576 static int vq2txq(struct virtqueue *vq)
578 return (vq->index - 1) / 2;
581 static int txq2vq(int txq)
586 static int vq2rxq(struct virtqueue *vq)
588 return vq->index / 2;
591 static int rxq2vq(int rxq)
596 static int vq_type(struct virtnet_info *vi, int qid)
598 if (qid == vi->max_queue_pairs * 2)
599 return VIRTNET_Q_TYPE_CQ;
602 return VIRTNET_Q_TYPE_TX;
604 return VIRTNET_Q_TYPE_RX;
607 static inline struct virtio_net_common_hdr *
608 skb_vnet_common_hdr(struct sk_buff *skb)
610 return (struct virtio_net_common_hdr *)skb->cb;
614 * private is used to chain pages for big packets, put the whole
615 * most recent used list in the beginning for reuse
617 static void give_pages(struct receive_queue *rq, struct page *page)
621 /* Find end of list, sew whole thing into vi->rq.pages. */
622 for (end = page; end->private; end = (struct page *)end->private);
623 end->private = (unsigned long)rq->pages;
627 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
629 struct page *p = rq->pages;
632 rq->pages = (struct page *)p->private;
633 /* clear private here, it is used to chain pages */
636 p = alloc_page(gfp_mask);
640 static void virtnet_rq_free_buf(struct virtnet_info *vi,
641 struct receive_queue *rq, void *buf)
643 if (vi->mergeable_rx_bufs)
644 put_page(virt_to_head_page(buf));
645 else if (vi->big_packets)
648 put_page(virt_to_head_page(buf));
651 static void enable_delayed_refill(struct virtnet_info *vi)
653 spin_lock_bh(&vi->refill_lock);
654 vi->refill_enabled = true;
655 spin_unlock_bh(&vi->refill_lock);
658 static void disable_delayed_refill(struct virtnet_info *vi)
660 spin_lock_bh(&vi->refill_lock);
661 vi->refill_enabled = false;
662 spin_unlock_bh(&vi->refill_lock);
665 static void enable_rx_mode_work(struct virtnet_info *vi)
668 vi->rx_mode_work_enabled = true;
672 static void disable_rx_mode_work(struct virtnet_info *vi)
675 vi->rx_mode_work_enabled = false;
679 static void virtqueue_napi_schedule(struct napi_struct *napi,
680 struct virtqueue *vq)
682 if (napi_schedule_prep(napi)) {
683 virtqueue_disable_cb(vq);
684 __napi_schedule(napi);
688 static bool virtqueue_napi_complete(struct napi_struct *napi,
689 struct virtqueue *vq, int processed)
693 opaque = virtqueue_enable_cb_prepare(vq);
694 if (napi_complete_done(napi, processed)) {
695 if (unlikely(virtqueue_poll(vq, opaque)))
696 virtqueue_napi_schedule(napi, vq);
700 virtqueue_disable_cb(vq);
706 static void skb_xmit_done(struct virtqueue *vq)
708 struct virtnet_info *vi = vq->vdev->priv;
709 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
711 /* Suppress further interrupts. */
712 virtqueue_disable_cb(vq);
715 virtqueue_napi_schedule(napi, vq);
717 /* We were probably waiting for more output buffers. */
718 netif_wake_subqueue(vi->dev, vq2txq(vq));
721 #define MRG_CTX_HEADER_SHIFT 22
722 static void *mergeable_len_to_ctx(unsigned int truesize,
723 unsigned int headroom)
725 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
728 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
730 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
733 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
735 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
738 static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen,
739 unsigned int headroom,
744 skb = build_skb(buf, buflen);
748 skb_reserve(skb, headroom);
754 /* Called from bottom half context */
755 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
756 struct receive_queue *rq,
757 struct page *page, unsigned int offset,
758 unsigned int len, unsigned int truesize,
759 unsigned int headroom)
762 struct virtio_net_common_hdr *hdr;
763 unsigned int copy, hdr_len, hdr_padded_len;
764 struct page *page_to_free = NULL;
765 int tailroom, shinfo_size;
766 char *p, *hdr_p, *buf;
768 p = page_address(page) + offset;
771 hdr_len = vi->hdr_len;
772 if (vi->mergeable_rx_bufs)
773 hdr_padded_len = hdr_len;
775 hdr_padded_len = sizeof(struct padded_vnet_hdr);
779 offset += hdr_padded_len;
781 tailroom = truesize - headroom - hdr_padded_len - len;
783 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
785 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
786 skb = virtnet_build_skb(buf, truesize, p - buf, len);
790 page = (struct page *)page->private;
792 give_pages(rq, page);
796 /* copy small packet so we can reuse these pages for small data */
797 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
801 /* Copy all frame if it fits skb->head, otherwise
802 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
804 if (len <= skb_tailroom(skb))
808 skb_put_data(skb, p, copy);
813 if (vi->mergeable_rx_bufs) {
815 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
822 * Verify that we can indeed put this data into a skb.
823 * This is here to handle cases when the device erroneously
824 * tries to receive more than is possible. This is usually
825 * the case of a broken device.
827 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
828 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
832 BUG_ON(offset >= PAGE_SIZE);
834 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
835 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
836 frag_size, truesize);
838 page = (struct page *)page->private;
843 give_pages(rq, page);
846 hdr = skb_vnet_common_hdr(skb);
847 memcpy(hdr, hdr_p, hdr_len);
849 put_page(page_to_free);
854 static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
856 struct page *page = virt_to_head_page(buf);
857 struct virtnet_rq_dma *dma;
861 head = page_address(page);
867 if (dma->need_sync && len) {
868 offset = buf - (head + sizeof(*dma));
870 virtqueue_dma_sync_single_range_for_cpu(rq->vq, dma->addr,
878 virtqueue_dma_unmap_single_attrs(rq->vq, dma->addr, dma->len,
879 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
883 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
887 buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
889 virtnet_rq_unmap(rq, buf, *len);
894 static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
896 struct virtnet_rq_dma *dma;
901 head = page_address(rq->alloc_frag.page);
907 addr = dma->addr - sizeof(*dma) + offset;
909 sg_init_table(rq->sg, 1);
910 rq->sg[0].dma_address = addr;
911 rq->sg[0].length = len;
914 static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
916 struct page_frag *alloc_frag = &rq->alloc_frag;
917 struct virtnet_rq_dma *dma;
921 if (unlikely(!skb_page_frag_refill(size, alloc_frag, gfp)))
924 head = page_address(alloc_frag->page);
929 if (!alloc_frag->offset) {
931 /* Now, the new page is allocated, the last dma
932 * will not be used. So the dma can be unmapped
935 virtnet_rq_unmap(rq, rq->last_dma, 0);
939 dma->len = alloc_frag->size - sizeof(*dma);
941 addr = virtqueue_dma_map_single_attrs(rq->vq, dma + 1,
942 dma->len, DMA_FROM_DEVICE, 0);
943 if (virtqueue_dma_mapping_error(rq->vq, addr))
947 dma->need_sync = virtqueue_dma_need_sync(rq->vq, addr);
949 /* Add a reference to dma to prevent the entire dma from
950 * being released during error handling. This reference
951 * will be freed after the pages are no longer used.
953 get_page(alloc_frag->page);
955 alloc_frag->offset = sizeof(*dma);
962 buf = head + alloc_frag->offset;
964 get_page(alloc_frag->page);
965 alloc_frag->offset += size;
970 static void virtnet_rq_set_premapped(struct virtnet_info *vi)
974 /* disable for big mode */
975 if (!vi->mergeable_rx_bufs && vi->big_packets)
978 for (i = 0; i < vi->max_queue_pairs; i++)
979 /* error should never happen */
980 BUG_ON(virtqueue_set_dma_premapped(vi->rq[i].vq));
983 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
985 struct virtnet_info *vi = vq->vdev->priv;
986 struct receive_queue *rq;
992 xsk_buff_free((struct xdp_buff *)buf);
996 if (!vi->big_packets || vi->mergeable_rx_bufs)
997 virtnet_rq_unmap(rq, buf, 0);
999 virtnet_rq_free_buf(vi, rq, buf);
1002 static void free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
1005 struct virtnet_sq_free_stats stats = {0};
1007 __free_old_xmit(sq, txq, in_napi, &stats);
1009 /* Avoid overhead when no packets have been processed
1010 * happens when called speculatively from start_xmit.
1012 if (!stats.packets && !stats.napi_packets)
1015 u64_stats_update_begin(&sq->stats.syncp);
1016 u64_stats_add(&sq->stats.bytes, stats.bytes + stats.napi_bytes);
1017 u64_stats_add(&sq->stats.packets, stats.packets + stats.napi_packets);
1018 u64_stats_update_end(&sq->stats.syncp);
1021 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1023 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1025 else if (q < vi->curr_queue_pairs)
1031 static void check_sq_full_and_disable(struct virtnet_info *vi,
1032 struct net_device *dev,
1033 struct send_queue *sq)
1035 bool use_napi = sq->napi.weight;
1040 /* If running out of space, stop queue to avoid getting packets that we
1041 * are then unable to transmit.
1042 * An alternative would be to force queuing layer to requeue the skb by
1043 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1044 * returned in a normal path of operation: it means that driver is not
1045 * maintaining the TX queue stop/start state properly, and causes
1046 * the stack to do a non-trivial amount of useless work.
1047 * Since most packets only take 1 or 2 ring slots, stopping the queue
1048 * early means 16 slots are typically wasted.
1050 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1051 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1053 netif_tx_stop_queue(txq);
1054 u64_stats_update_begin(&sq->stats.syncp);
1055 u64_stats_inc(&sq->stats.stop);
1056 u64_stats_update_end(&sq->stats.syncp);
1058 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
1059 virtqueue_napi_schedule(&sq->napi, sq->vq);
1060 } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1061 /* More just got used, free them then recheck. */
1062 free_old_xmit(sq, txq, false);
1063 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1064 netif_start_subqueue(dev, qnum);
1065 u64_stats_update_begin(&sq->stats.syncp);
1066 u64_stats_inc(&sq->stats.wake);
1067 u64_stats_update_end(&sq->stats.syncp);
1068 virtqueue_disable_cb(sq->vq);
1074 static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
1076 sg->dma_address = addr;
1080 static struct xdp_buff *buf_to_xdp(struct virtnet_info *vi,
1081 struct receive_queue *rq, void *buf, u32 len)
1083 struct xdp_buff *xdp;
1086 xdp = (struct xdp_buff *)buf;
1088 bufsize = xsk_pool_get_rx_frame_size(rq->xsk_pool) + vi->hdr_len;
1090 if (unlikely(len > bufsize)) {
1091 pr_debug("%s: rx error: len %u exceeds truesize %u\n",
1092 vi->dev->name, len, bufsize);
1093 DEV_STATS_INC(vi->dev, rx_length_errors);
1098 xsk_buff_set_size(xdp, len);
1099 xsk_buff_dma_sync_for_cpu(xdp);
1104 static struct sk_buff *xsk_construct_skb(struct receive_queue *rq,
1105 struct xdp_buff *xdp)
1107 unsigned int metasize = xdp->data - xdp->data_meta;
1108 struct sk_buff *skb;
1111 size = xdp->data_end - xdp->data_hard_start;
1112 skb = napi_alloc_skb(&rq->napi, size);
1113 if (unlikely(!skb)) {
1118 skb_reserve(skb, xdp->data_meta - xdp->data_hard_start);
1120 size = xdp->data_end - xdp->data_meta;
1121 memcpy(__skb_put(skb, size), xdp->data_meta, size);
1124 __skb_pull(skb, metasize);
1125 skb_metadata_set(skb, metasize);
1133 static struct sk_buff *virtnet_receive_xsk_small(struct net_device *dev, struct virtnet_info *vi,
1134 struct receive_queue *rq, struct xdp_buff *xdp,
1135 unsigned int *xdp_xmit,
1136 struct virtnet_rq_stats *stats)
1138 struct bpf_prog *prog;
1143 prog = rcu_dereference(rq->xdp_prog);
1145 ret = virtnet_xdp_handler(prog, xdp, dev, xdp_xmit, stats);
1150 return xsk_construct_skb(rq, xdp);
1159 u64_stats_inc(&stats->drops);
1164 static void xsk_drop_follow_bufs(struct net_device *dev,
1165 struct receive_queue *rq,
1167 struct virtnet_rq_stats *stats)
1169 struct xdp_buff *xdp;
1172 while (num_buf-- > 1) {
1173 xdp = virtqueue_get_buf(rq->vq, &len);
1174 if (unlikely(!xdp)) {
1175 pr_debug("%s: rx error: %d buffers missing\n",
1176 dev->name, num_buf);
1177 DEV_STATS_INC(dev, rx_length_errors);
1180 u64_stats_add(&stats->bytes, len);
1185 static int xsk_append_merge_buffer(struct virtnet_info *vi,
1186 struct receive_queue *rq,
1187 struct sk_buff *head_skb,
1189 struct virtio_net_hdr_mrg_rxbuf *hdr,
1190 struct virtnet_rq_stats *stats)
1192 struct sk_buff *curr_skb;
1193 struct xdp_buff *xdp;
1198 curr_skb = head_skb;
1201 buf = virtqueue_get_buf(rq->vq, &len);
1202 if (unlikely(!buf)) {
1203 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1204 vi->dev->name, num_buf,
1205 virtio16_to_cpu(vi->vdev,
1207 DEV_STATS_INC(vi->dev, rx_length_errors);
1211 u64_stats_add(&stats->bytes, len);
1213 xdp = buf_to_xdp(vi, rq, buf, len);
1217 buf = napi_alloc_frag(len);
1223 memcpy(buf, xdp->data - vi->hdr_len, len);
1227 page = virt_to_page(buf);
1231 curr_skb = virtnet_skb_append_frag(head_skb, curr_skb, page,
1232 buf, len, truesize);
1242 xsk_drop_follow_bufs(vi->dev, rq, num_buf, stats);
1246 static struct sk_buff *virtnet_receive_xsk_merge(struct net_device *dev, struct virtnet_info *vi,
1247 struct receive_queue *rq, struct xdp_buff *xdp,
1248 unsigned int *xdp_xmit,
1249 struct virtnet_rq_stats *stats)
1251 struct virtio_net_hdr_mrg_rxbuf *hdr;
1252 struct bpf_prog *prog;
1253 struct sk_buff *skb;
1256 hdr = xdp->data - vi->hdr_len;
1257 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1261 prog = rcu_dereference(rq->xdp_prog);
1262 /* TODO: support multi buffer. */
1263 if (prog && num_buf == 1)
1264 ret = virtnet_xdp_handler(prog, xdp, dev, xdp_xmit, stats);
1269 skb = xsk_construct_skb(rq, xdp);
1273 if (xsk_append_merge_buffer(vi, rq, skb, num_buf, hdr, stats)) {
1290 xsk_drop_follow_bufs(dev, rq, num_buf, stats);
1293 u64_stats_inc(&stats->drops);
1297 static void virtnet_receive_xsk_buf(struct virtnet_info *vi, struct receive_queue *rq,
1299 unsigned int *xdp_xmit,
1300 struct virtnet_rq_stats *stats)
1302 struct net_device *dev = vi->dev;
1303 struct sk_buff *skb = NULL;
1304 struct xdp_buff *xdp;
1309 u64_stats_add(&stats->bytes, len);
1311 xdp = buf_to_xdp(vi, rq, buf, len);
1315 if (unlikely(len < ETH_HLEN)) {
1316 pr_debug("%s: short packet %i\n", dev->name, len);
1317 DEV_STATS_INC(dev, rx_length_errors);
1322 flags = ((struct virtio_net_common_hdr *)(xdp->data - vi->hdr_len))->hdr.flags;
1324 if (!vi->mergeable_rx_bufs)
1325 skb = virtnet_receive_xsk_small(dev, vi, rq, xdp, xdp_xmit, stats);
1327 skb = virtnet_receive_xsk_merge(dev, vi, rq, xdp, xdp_xmit, stats);
1330 virtnet_receive_done(vi, rq, skb, flags);
1333 static int virtnet_add_recvbuf_xsk(struct virtnet_info *vi, struct receive_queue *rq,
1334 struct xsk_buff_pool *pool, gfp_t gfp)
1336 struct xdp_buff **xsk_buffs;
1342 xsk_buffs = rq->xsk_buffs;
1344 num = xsk_buff_alloc_batch(pool, xsk_buffs, rq->vq->num_free);
1348 len = xsk_pool_get_rx_frame_size(pool) + vi->hdr_len;
1350 for (i = 0; i < num; ++i) {
1351 /* Use the part of XDP_PACKET_HEADROOM as the virtnet hdr space.
1352 * We assume XDP_PACKET_HEADROOM is larger than hdr->len.
1353 * (see function virtnet_xsk_pool_enable)
1355 addr = xsk_buff_xdp_get_dma(xsk_buffs[i]) - vi->hdr_len;
1357 sg_init_table(rq->sg, 1);
1358 sg_fill_dma(rq->sg, addr, len);
1360 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, xsk_buffs[i], gfp);
1368 for (; i < num; ++i)
1369 xsk_buff_free(xsk_buffs[i]);
1374 static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag)
1376 struct virtnet_info *vi = netdev_priv(dev);
1377 struct send_queue *sq;
1379 if (!netif_running(dev))
1382 if (qid >= vi->curr_queue_pairs)
1387 if (napi_if_scheduled_mark_missed(&sq->napi))
1391 virtqueue_napi_schedule(&sq->napi, sq->vq);
1397 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
1398 struct send_queue *sq,
1399 struct xdp_frame *xdpf)
1401 struct virtio_net_hdr_mrg_rxbuf *hdr;
1402 struct skb_shared_info *shinfo;
1406 if (unlikely(xdpf->headroom < vi->hdr_len))
1409 if (unlikely(xdp_frame_has_frags(xdpf))) {
1410 shinfo = xdp_get_shared_info_from_frame(xdpf);
1411 nr_frags = shinfo->nr_frags;
1414 /* In wrapping function virtnet_xdp_xmit(), we need to free
1415 * up the pending old buffers, where we need to calculate the
1416 * position of skb_shared_info in xdp_get_frame_len() and
1417 * xdp_return_frame(), which will involve to xdpf->data and
1418 * xdpf->headroom. Therefore, we need to update the value of
1419 * headroom synchronously here.
1421 xdpf->headroom -= vi->hdr_len;
1422 xdpf->data -= vi->hdr_len;
1423 /* Zero header and leave csum up to XDP layers */
1425 memset(hdr, 0, vi->hdr_len);
1426 xdpf->len += vi->hdr_len;
1428 sg_init_table(sq->sg, nr_frags + 1);
1429 sg_set_buf(sq->sg, xdpf->data, xdpf->len);
1430 for (i = 0; i < nr_frags; i++) {
1431 skb_frag_t *frag = &shinfo->frags[i];
1433 sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
1434 skb_frag_size(frag), skb_frag_off(frag));
1437 err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
1438 xdp_to_ptr(xdpf), GFP_ATOMIC);
1440 return -ENOSPC; /* Caller handle free/refcnt */
1445 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
1446 * the current cpu, so it does not need to be locked.
1448 * Here we use marco instead of inline functions because we have to deal with
1449 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
1450 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
1451 * functions to perfectly solve these three problems at the same time.
1453 #define virtnet_xdp_get_sq(vi) ({ \
1454 int cpu = smp_processor_id(); \
1455 struct netdev_queue *txq; \
1456 typeof(vi) v = (vi); \
1459 if (v->curr_queue_pairs > nr_cpu_ids) { \
1460 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
1462 txq = netdev_get_tx_queue(v->dev, qp); \
1463 __netif_tx_acquire(txq); \
1465 qp = cpu % v->curr_queue_pairs; \
1466 txq = netdev_get_tx_queue(v->dev, qp); \
1467 __netif_tx_lock(txq, cpu); \
1472 #define virtnet_xdp_put_sq(vi, q) { \
1473 struct netdev_queue *txq; \
1474 typeof(vi) v = (vi); \
1476 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
1477 if (v->curr_queue_pairs > nr_cpu_ids) \
1478 __netif_tx_release(txq); \
1480 __netif_tx_unlock(txq); \
1483 static int virtnet_xdp_xmit(struct net_device *dev,
1484 int n, struct xdp_frame **frames, u32 flags)
1486 struct virtnet_info *vi = netdev_priv(dev);
1487 struct virtnet_sq_free_stats stats = {0};
1488 struct receive_queue *rq = vi->rq;
1489 struct bpf_prog *xdp_prog;
1490 struct send_queue *sq;
1496 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
1497 * indicate XDP resources have been successfully allocated.
1499 xdp_prog = rcu_access_pointer(rq->xdp_prog);
1503 sq = virtnet_xdp_get_sq(vi);
1505 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
1510 /* Free up any pending old buffers before queueing new ones. */
1511 __free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq),
1514 for (i = 0; i < n; i++) {
1515 struct xdp_frame *xdpf = frames[i];
1517 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
1523 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
1524 check_sq_full_and_disable(vi, dev, sq);
1526 if (flags & XDP_XMIT_FLUSH) {
1527 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
1531 u64_stats_update_begin(&sq->stats.syncp);
1532 u64_stats_add(&sq->stats.bytes, stats.bytes);
1533 u64_stats_add(&sq->stats.packets, stats.packets);
1534 u64_stats_add(&sq->stats.xdp_tx, n);
1535 u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit);
1536 u64_stats_add(&sq->stats.kicks, kicks);
1537 u64_stats_update_end(&sq->stats.syncp);
1539 virtnet_xdp_put_sq(vi, sq);
1543 static void put_xdp_frags(struct xdp_buff *xdp)
1545 struct skb_shared_info *shinfo;
1546 struct page *xdp_page;
1549 if (xdp_buff_has_frags(xdp)) {
1550 shinfo = xdp_get_shared_info_from_buff(xdp);
1551 for (i = 0; i < shinfo->nr_frags; i++) {
1552 xdp_page = skb_frag_page(&shinfo->frags[i]);
1558 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
1559 struct net_device *dev,
1560 unsigned int *xdp_xmit,
1561 struct virtnet_rq_stats *stats)
1563 struct xdp_frame *xdpf;
1567 act = bpf_prog_run_xdp(xdp_prog, xdp);
1568 u64_stats_inc(&stats->xdp_packets);
1575 u64_stats_inc(&stats->xdp_tx);
1576 xdpf = xdp_convert_buff_to_frame(xdp);
1577 if (unlikely(!xdpf)) {
1578 netdev_dbg(dev, "convert buff to frame failed for xdp\n");
1582 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1583 if (unlikely(!err)) {
1584 xdp_return_frame_rx_napi(xdpf);
1585 } else if (unlikely(err < 0)) {
1586 trace_xdp_exception(dev, xdp_prog, act);
1589 *xdp_xmit |= VIRTIO_XDP_TX;
1593 u64_stats_inc(&stats->xdp_redirects);
1594 err = xdp_do_redirect(dev, xdp, xdp_prog);
1598 *xdp_xmit |= VIRTIO_XDP_REDIR;
1602 bpf_warn_invalid_xdp_action(dev, xdp_prog, act);
1605 trace_xdp_exception(dev, xdp_prog, act);
1612 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
1614 return vi->xdp_enabled ? XDP_PACKET_HEADROOM : 0;
1617 /* We copy the packet for XDP in the following cases:
1619 * 1) Packet is scattered across multiple rx buffers.
1620 * 2) Headroom space is insufficient.
1622 * This is inefficient but it's a temporary condition that
1623 * we hit right after XDP is enabled and until queue is refilled
1624 * with large buffers with sufficient headroom - so it should affect
1625 * at most queue size packets.
1626 * Afterwards, the conditions to enable
1627 * XDP should preclude the underlying device from sending packets
1628 * across multiple buffers (num_buf > 1), and we make sure buffers
1629 * have enough headroom.
1631 static struct page *xdp_linearize_page(struct receive_queue *rq,
1638 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1641 if (page_off + *len + tailroom > PAGE_SIZE)
1644 page = alloc_page(GFP_ATOMIC);
1648 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
1651 while (--*num_buf) {
1652 unsigned int buflen;
1656 buf = virtnet_rq_get_buf(rq, &buflen, NULL);
1660 p = virt_to_head_page(buf);
1661 off = buf - page_address(p);
1663 /* guard against a misconfigured or uncooperative backend that
1664 * is sending packet larger than the MTU.
1666 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
1671 memcpy(page_address(page) + page_off,
1672 page_address(p) + off, buflen);
1677 /* Headroom does not contribute to packet length */
1678 *len = page_off - XDP_PACKET_HEADROOM;
1681 __free_pages(page, 0);
1685 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
1686 unsigned int xdp_headroom,
1690 unsigned int header_offset;
1691 unsigned int headroom;
1692 unsigned int buflen;
1693 struct sk_buff *skb;
1695 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1696 headroom = vi->hdr_len + header_offset;
1697 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1698 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1700 skb = virtnet_build_skb(buf, buflen, headroom, len);
1704 buf += header_offset;
1705 memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
1710 static struct sk_buff *receive_small_xdp(struct net_device *dev,
1711 struct virtnet_info *vi,
1712 struct receive_queue *rq,
1713 struct bpf_prog *xdp_prog,
1715 unsigned int xdp_headroom,
1717 unsigned int *xdp_xmit,
1718 struct virtnet_rq_stats *stats)
1720 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
1721 unsigned int headroom = vi->hdr_len + header_offset;
1722 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
1723 struct page *page = virt_to_head_page(buf);
1724 struct page *xdp_page;
1725 unsigned int buflen;
1726 struct xdp_buff xdp;
1727 struct sk_buff *skb;
1728 unsigned int metasize = 0;
1731 if (unlikely(hdr->hdr.gso_type))
1734 /* Partially checksummed packets must be dropped. */
1735 if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
1738 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1739 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1741 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
1742 int offset = buf - page_address(page) + header_offset;
1743 unsigned int tlen = len + vi->hdr_len;
1746 xdp_headroom = virtnet_get_headroom(vi);
1747 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1748 headroom = vi->hdr_len + header_offset;
1749 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1750 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1751 xdp_page = xdp_linearize_page(rq, &num_buf, page,
1752 offset, header_offset,
1757 buf = page_address(xdp_page);
1762 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
1763 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
1764 xdp_headroom, len, true);
1766 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1770 /* Recalculate length in case bpf program changed it */
1771 len = xdp.data_end - xdp.data;
1772 metasize = xdp.data - xdp.data_meta;
1783 skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len);
1788 skb_metadata_set(skb, metasize);
1793 u64_stats_inc(&stats->xdp_drops);
1795 u64_stats_inc(&stats->drops);
1801 static struct sk_buff *receive_small(struct net_device *dev,
1802 struct virtnet_info *vi,
1803 struct receive_queue *rq,
1804 void *buf, void *ctx,
1806 unsigned int *xdp_xmit,
1807 struct virtnet_rq_stats *stats)
1809 unsigned int xdp_headroom = (unsigned long)ctx;
1810 struct page *page = virt_to_head_page(buf);
1811 struct sk_buff *skb;
1814 u64_stats_add(&stats->bytes, len);
1816 if (unlikely(len > GOOD_PACKET_LEN)) {
1817 pr_debug("%s: rx error: len %u exceeds max size %d\n",
1818 dev->name, len, GOOD_PACKET_LEN);
1819 DEV_STATS_INC(dev, rx_length_errors);
1823 if (unlikely(vi->xdp_enabled)) {
1824 struct bpf_prog *xdp_prog;
1827 xdp_prog = rcu_dereference(rq->xdp_prog);
1829 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf,
1830 xdp_headroom, len, xdp_xmit,
1838 skb = receive_small_build_skb(vi, xdp_headroom, buf, len);
1843 u64_stats_inc(&stats->drops);
1848 static struct sk_buff *receive_big(struct net_device *dev,
1849 struct virtnet_info *vi,
1850 struct receive_queue *rq,
1853 struct virtnet_rq_stats *stats)
1855 struct page *page = buf;
1856 struct sk_buff *skb =
1857 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0);
1859 u64_stats_add(&stats->bytes, len - vi->hdr_len);
1866 u64_stats_inc(&stats->drops);
1867 give_pages(rq, page);
1871 static void mergeable_buf_free(struct receive_queue *rq, int num_buf,
1872 struct net_device *dev,
1873 struct virtnet_rq_stats *stats)
1879 while (num_buf-- > 1) {
1880 buf = virtnet_rq_get_buf(rq, &len, NULL);
1881 if (unlikely(!buf)) {
1882 pr_debug("%s: rx error: %d buffers missing\n",
1883 dev->name, num_buf);
1884 DEV_STATS_INC(dev, rx_length_errors);
1887 u64_stats_add(&stats->bytes, len);
1888 page = virt_to_head_page(buf);
1893 /* Why not use xdp_build_skb_from_frame() ?
1894 * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
1895 * virtio-net there are 2 points that do not match its requirements:
1896 * 1. The size of the prefilled buffer is not fixed before xdp is set.
1897 * 2. xdp_build_skb_from_frame() does more checks that we don't need,
1898 * like eth_type_trans() (which virtio-net does in receive_buf()).
1900 static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
1901 struct virtnet_info *vi,
1902 struct xdp_buff *xdp,
1903 unsigned int xdp_frags_truesz)
1905 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
1906 unsigned int headroom, data_len;
1907 struct sk_buff *skb;
1911 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
1912 pr_debug("Error building skb as missing reserved tailroom for xdp");
1916 if (unlikely(xdp_buff_has_frags(xdp)))
1917 nr_frags = sinfo->nr_frags;
1919 skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
1923 headroom = xdp->data - xdp->data_hard_start;
1924 data_len = xdp->data_end - xdp->data;
1925 skb_reserve(skb, headroom);
1926 __skb_put(skb, data_len);
1928 metasize = xdp->data - xdp->data_meta;
1929 metasize = metasize > 0 ? metasize : 0;
1931 skb_metadata_set(skb, metasize);
1933 if (unlikely(xdp_buff_has_frags(xdp)))
1934 xdp_update_skb_shared_info(skb, nr_frags,
1935 sinfo->xdp_frags_size,
1937 xdp_buff_is_frag_pfmemalloc(xdp));
1942 /* TODO: build xdp in big mode */
1943 static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
1944 struct virtnet_info *vi,
1945 struct receive_queue *rq,
1946 struct xdp_buff *xdp,
1949 unsigned int frame_sz,
1951 unsigned int *xdp_frags_truesize,
1952 struct virtnet_rq_stats *stats)
1954 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1955 unsigned int headroom, tailroom, room;
1956 unsigned int truesize, cur_frag_size;
1957 struct skb_shared_info *shinfo;
1958 unsigned int xdp_frags_truesz = 0;
1964 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
1965 xdp_prepare_buff(xdp, buf - XDP_PACKET_HEADROOM,
1966 XDP_PACKET_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
1972 /* If we want to build multi-buffer xdp, we need
1973 * to specify that the flags of xdp_buff have the
1974 * XDP_FLAGS_HAS_FRAG bit.
1976 if (!xdp_buff_has_frags(xdp))
1977 xdp_buff_set_frags_flag(xdp);
1979 shinfo = xdp_get_shared_info_from_buff(xdp);
1980 shinfo->nr_frags = 0;
1981 shinfo->xdp_frags_size = 0;
1984 if (*num_buf > MAX_SKB_FRAGS + 1)
1987 while (--*num_buf > 0) {
1988 buf = virtnet_rq_get_buf(rq, &len, &ctx);
1989 if (unlikely(!buf)) {
1990 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1991 dev->name, *num_buf,
1992 virtio16_to_cpu(vi->vdev, hdr->num_buffers));
1993 DEV_STATS_INC(dev, rx_length_errors);
1997 u64_stats_add(&stats->bytes, len);
1998 page = virt_to_head_page(buf);
1999 offset = buf - page_address(page);
2001 truesize = mergeable_ctx_to_truesize(ctx);
2002 headroom = mergeable_ctx_to_headroom(ctx);
2003 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
2004 room = SKB_DATA_ALIGN(headroom + tailroom);
2006 cur_frag_size = truesize;
2007 xdp_frags_truesz += cur_frag_size;
2008 if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) {
2010 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
2011 dev->name, len, (unsigned long)(truesize - room));
2012 DEV_STATS_INC(dev, rx_length_errors);
2016 frag = &shinfo->frags[shinfo->nr_frags++];
2017 skb_frag_fill_page_desc(frag, page, offset, len);
2018 if (page_is_pfmemalloc(page))
2019 xdp_buff_set_frag_pfmemalloc(xdp);
2021 shinfo->xdp_frags_size += len;
2024 *xdp_frags_truesize = xdp_frags_truesz;
2032 static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
2033 struct receive_queue *rq,
2034 struct bpf_prog *xdp_prog,
2036 unsigned int *frame_sz,
2041 struct virtio_net_hdr_mrg_rxbuf *hdr)
2043 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
2044 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
2045 struct page *xdp_page;
2046 unsigned int xdp_room;
2048 /* Transient failure which in theory could occur if
2049 * in-flight packets from before XDP was enabled reach
2050 * the receive path after XDP is loaded.
2052 if (unlikely(hdr->hdr.gso_type))
2055 /* Partially checksummed packets must be dropped. */
2056 if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
2059 /* Now XDP core assumes frag size is PAGE_SIZE, but buffers
2060 * with headroom may add hole in truesize, which
2061 * make their length exceed PAGE_SIZE. So we disabled the
2062 * hole mechanism for xdp. See add_recvbuf_mergeable().
2064 *frame_sz = truesize;
2066 if (likely(headroom >= virtnet_get_headroom(vi) &&
2067 (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) {
2068 return page_address(*page) + offset;
2071 /* This happens when headroom is not enough because
2072 * of the buffer was prefilled before XDP is set.
2073 * This should only happen for the first several packets.
2074 * In fact, vq reset can be used here to help us clean up
2075 * the prefilled buffers, but many existing devices do not
2076 * support it, and we don't want to bother users who are
2077 * using xdp normally.
2079 if (!xdp_prog->aux->xdp_has_frags) {
2080 /* linearize data for XDP */
2081 xdp_page = xdp_linearize_page(rq, num_buf,
2083 XDP_PACKET_HEADROOM,
2088 xdp_room = SKB_DATA_ALIGN(XDP_PACKET_HEADROOM +
2089 sizeof(struct skb_shared_info));
2090 if (*len + xdp_room > PAGE_SIZE)
2093 xdp_page = alloc_page(GFP_ATOMIC);
2097 memcpy(page_address(xdp_page) + XDP_PACKET_HEADROOM,
2098 page_address(*page) + offset, *len);
2101 *frame_sz = PAGE_SIZE;
2107 return page_address(*page) + XDP_PACKET_HEADROOM;
2110 static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
2111 struct virtnet_info *vi,
2112 struct receive_queue *rq,
2113 struct bpf_prog *xdp_prog,
2117 unsigned int *xdp_xmit,
2118 struct virtnet_rq_stats *stats)
2120 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
2121 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
2122 struct page *page = virt_to_head_page(buf);
2123 int offset = buf - page_address(page);
2124 unsigned int xdp_frags_truesz = 0;
2125 struct sk_buff *head_skb;
2126 unsigned int frame_sz;
2127 struct xdp_buff xdp;
2132 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page,
2134 if (unlikely(!data))
2137 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
2138 &num_buf, &xdp_frags_truesz, stats);
2142 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
2146 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
2147 if (unlikely(!head_skb))
2159 put_xdp_frags(&xdp);
2163 mergeable_buf_free(rq, num_buf, dev, stats);
2165 u64_stats_inc(&stats->xdp_drops);
2166 u64_stats_inc(&stats->drops);
2170 static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
2171 struct sk_buff *curr_skb,
2172 struct page *page, void *buf,
2173 int len, int truesize)
2178 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
2179 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
2180 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
2182 if (unlikely(!nskb))
2185 if (curr_skb == head_skb)
2186 skb_shinfo(curr_skb)->frag_list = nskb;
2188 curr_skb->next = nskb;
2190 head_skb->truesize += nskb->truesize;
2194 if (curr_skb != head_skb) {
2195 head_skb->data_len += len;
2196 head_skb->len += len;
2197 head_skb->truesize += truesize;
2200 offset = buf - page_address(page);
2201 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
2203 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
2206 skb_add_rx_frag(curr_skb, num_skb_frags, page,
2207 offset, len, truesize);
2213 static struct sk_buff *receive_mergeable(struct net_device *dev,
2214 struct virtnet_info *vi,
2215 struct receive_queue *rq,
2219 unsigned int *xdp_xmit,
2220 struct virtnet_rq_stats *stats)
2222 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
2223 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
2224 struct page *page = virt_to_head_page(buf);
2225 int offset = buf - page_address(page);
2226 struct sk_buff *head_skb, *curr_skb;
2227 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
2228 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
2229 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
2230 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
2233 u64_stats_add(&stats->bytes, len - vi->hdr_len);
2235 if (unlikely(len > truesize - room)) {
2236 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
2237 dev->name, len, (unsigned long)(truesize - room));
2238 DEV_STATS_INC(dev, rx_length_errors);
2242 if (unlikely(vi->xdp_enabled)) {
2243 struct bpf_prog *xdp_prog;
2246 xdp_prog = rcu_dereference(rq->xdp_prog);
2248 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx,
2249 len, xdp_xmit, stats);
2256 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom);
2257 curr_skb = head_skb;
2259 if (unlikely(!curr_skb))
2262 buf = virtnet_rq_get_buf(rq, &len, &ctx);
2263 if (unlikely(!buf)) {
2264 pr_debug("%s: rx error: %d buffers out of %d missing\n",
2266 virtio16_to_cpu(vi->vdev,
2268 DEV_STATS_INC(dev, rx_length_errors);
2272 u64_stats_add(&stats->bytes, len);
2273 page = virt_to_head_page(buf);
2275 truesize = mergeable_ctx_to_truesize(ctx);
2276 headroom = mergeable_ctx_to_headroom(ctx);
2277 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
2278 room = SKB_DATA_ALIGN(headroom + tailroom);
2279 if (unlikely(len > truesize - room)) {
2280 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
2281 dev->name, len, (unsigned long)(truesize - room));
2282 DEV_STATS_INC(dev, rx_length_errors);
2286 curr_skb = virtnet_skb_append_frag(head_skb, curr_skb, page,
2287 buf, len, truesize);
2292 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
2297 mergeable_buf_free(rq, num_buf, dev, stats);
2300 u64_stats_inc(&stats->drops);
2301 dev_kfree_skb(head_skb);
2305 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
2306 struct sk_buff *skb)
2308 enum pkt_hash_types rss_hash_type;
2310 if (!hdr_hash || !skb)
2313 switch (__le16_to_cpu(hdr_hash->hash_report)) {
2314 case VIRTIO_NET_HASH_REPORT_TCPv4:
2315 case VIRTIO_NET_HASH_REPORT_UDPv4:
2316 case VIRTIO_NET_HASH_REPORT_TCPv6:
2317 case VIRTIO_NET_HASH_REPORT_UDPv6:
2318 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
2319 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
2320 rss_hash_type = PKT_HASH_TYPE_L4;
2322 case VIRTIO_NET_HASH_REPORT_IPv4:
2323 case VIRTIO_NET_HASH_REPORT_IPv6:
2324 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
2325 rss_hash_type = PKT_HASH_TYPE_L3;
2327 case VIRTIO_NET_HASH_REPORT_NONE:
2329 rss_hash_type = PKT_HASH_TYPE_NONE;
2331 skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
2334 static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
2335 struct sk_buff *skb, u8 flags)
2337 struct virtio_net_common_hdr *hdr;
2338 struct net_device *dev = vi->dev;
2340 hdr = skb_vnet_common_hdr(skb);
2341 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
2342 virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
2344 if (flags & VIRTIO_NET_HDR_F_DATA_VALID)
2345 skb->ip_summed = CHECKSUM_UNNECESSARY;
2347 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
2348 virtio_is_little_endian(vi->vdev))) {
2349 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
2350 dev->name, hdr->hdr.gso_type,
2355 skb_record_rx_queue(skb, vq2rxq(rq->vq));
2356 skb->protocol = eth_type_trans(skb, dev);
2357 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
2358 ntohs(skb->protocol), skb->len, skb->pkt_type);
2360 napi_gro_receive(&rq->napi, skb);
2364 DEV_STATS_INC(dev, rx_frame_errors);
2368 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
2369 void *buf, unsigned int len, void **ctx,
2370 unsigned int *xdp_xmit,
2371 struct virtnet_rq_stats *stats)
2373 struct net_device *dev = vi->dev;
2374 struct sk_buff *skb;
2377 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
2378 pr_debug("%s: short packet %i\n", dev->name, len);
2379 DEV_STATS_INC(dev, rx_length_errors);
2380 virtnet_rq_free_buf(vi, rq, buf);
2384 /* 1. Save the flags early, as the XDP program might overwrite them.
2385 * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
2386 * stay valid after XDP processing.
2387 * 2. XDP doesn't work with partially checksummed packets (refer to
2388 * virtnet_xdp_set()), so packets marked as
2389 * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
2391 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
2393 if (vi->mergeable_rx_bufs)
2394 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
2396 else if (vi->big_packets)
2397 skb = receive_big(dev, vi, rq, buf, len, stats);
2399 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
2404 virtnet_receive_done(vi, rq, skb, flags);
2407 /* Unlike mergeable buffers, all buffers are allocated to the
2408 * same size, except for the headroom. For this reason we do
2409 * not need to use mergeable_len_to_ctx here - it is enough
2410 * to store the headroom as the context ignoring the truesize.
2412 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
2416 unsigned int xdp_headroom = virtnet_get_headroom(vi);
2417 void *ctx = (void *)(unsigned long)xdp_headroom;
2418 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
2421 len = SKB_DATA_ALIGN(len) +
2422 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2424 buf = virtnet_rq_alloc(rq, len, gfp);
2428 virtnet_rq_init_one_sg(rq, buf + VIRTNET_RX_PAD + xdp_headroom,
2429 vi->hdr_len + GOOD_PACKET_LEN);
2431 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
2433 virtnet_rq_unmap(rq, buf, 0);
2434 put_page(virt_to_head_page(buf));
2440 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
2443 struct page *first, *list = NULL;
2447 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
2449 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
2450 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
2451 first = get_a_page(rq, gfp);
2454 give_pages(rq, list);
2457 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
2459 /* chain new page in list head to match sg */
2460 first->private = (unsigned long)list;
2464 first = get_a_page(rq, gfp);
2466 give_pages(rq, list);
2469 p = page_address(first);
2471 /* rq->sg[0], rq->sg[1] share the same page */
2472 /* a separated rq->sg[0] for header - required in case !any_header_sg */
2473 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
2475 /* rq->sg[1] for data packet, from offset */
2476 offset = sizeof(struct padded_vnet_hdr);
2477 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
2479 /* chain first in list head */
2480 first->private = (unsigned long)list;
2481 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
2484 give_pages(rq, first);
2489 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
2490 struct ewma_pkt_len *avg_pkt_len,
2493 struct virtnet_info *vi = rq->vq->vdev->priv;
2494 const size_t hdr_len = vi->hdr_len;
2498 return PAGE_SIZE - room;
2500 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
2501 rq->min_buf_len, PAGE_SIZE - hdr_len);
2503 return ALIGN(len, L1_CACHE_BYTES);
2506 static int add_recvbuf_mergeable(struct virtnet_info *vi,
2507 struct receive_queue *rq, gfp_t gfp)
2509 struct page_frag *alloc_frag = &rq->alloc_frag;
2510 unsigned int headroom = virtnet_get_headroom(vi);
2511 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
2512 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
2513 unsigned int len, hole;
2518 /* Extra tailroom is needed to satisfy XDP's assumption. This
2519 * means rx frags coalescing won't work, but consider we've
2520 * disabled GSO for XDP, it won't be a big issue.
2522 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
2524 buf = virtnet_rq_alloc(rq, len + room, gfp);
2528 buf += headroom; /* advance address leaving hole at front of pkt */
2529 hole = alloc_frag->size - alloc_frag->offset;
2530 if (hole < len + room) {
2531 /* To avoid internal fragmentation, if there is very likely not
2532 * enough space for another buffer, add the remaining space to
2533 * the current buffer.
2534 * XDP core assumes that frame_size of xdp_buff and the length
2535 * of the frag are PAGE_SIZE, so we disable the hole mechanism.
2539 alloc_frag->offset += hole;
2542 virtnet_rq_init_one_sg(rq, buf, len);
2544 ctx = mergeable_len_to_ctx(len + room, headroom);
2545 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
2547 virtnet_rq_unmap(rq, buf, 0);
2548 put_page(virt_to_head_page(buf));
2555 * Returns false if we couldn't fill entirely (OOM).
2557 * Normally run in the receive path, but can also be run from ndo_open
2558 * before we're receiving packets, or from refill_work which is
2559 * careful to disable receiving (using napi_disable).
2561 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
2567 err = virtnet_add_recvbuf_xsk(vi, rq, rq->xsk_pool, gfp);
2572 if (vi->mergeable_rx_bufs)
2573 err = add_recvbuf_mergeable(vi, rq, gfp);
2574 else if (vi->big_packets)
2575 err = add_recvbuf_big(vi, rq, gfp);
2577 err = add_recvbuf_small(vi, rq, gfp);
2581 } while (rq->vq->num_free);
2584 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
2585 unsigned long flags;
2587 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
2588 u64_stats_inc(&rq->stats.kicks);
2589 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
2592 return err != -ENOMEM;
2595 static void skb_recv_done(struct virtqueue *rvq)
2597 struct virtnet_info *vi = rvq->vdev->priv;
2598 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
2601 virtqueue_napi_schedule(&rq->napi, rvq);
2604 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
2608 /* If all buffers were filled by other side before we napi_enabled, we
2609 * won't get another interrupt, so process any outstanding packets now.
2610 * Call local_bh_enable after to trigger softIRQ processing.
2613 virtqueue_napi_schedule(napi, vq);
2617 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
2618 struct virtqueue *vq,
2619 struct napi_struct *napi)
2624 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
2625 * enable the feature if this is likely affine with the transmit path.
2627 if (!vi->affinity_hint_set) {
2632 return virtnet_napi_enable(vq, napi);
2635 static void virtnet_napi_tx_disable(struct napi_struct *napi)
2641 static void refill_work(struct work_struct *work)
2643 struct virtnet_info *vi =
2644 container_of(work, struct virtnet_info, refill.work);
2648 for (i = 0; i < vi->curr_queue_pairs; i++) {
2649 struct receive_queue *rq = &vi->rq[i];
2651 napi_disable(&rq->napi);
2652 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
2653 virtnet_napi_enable(rq->vq, &rq->napi);
2655 /* In theory, this can happen: if we don't get any buffers in
2656 * we will *never* try to fill again.
2659 schedule_delayed_work(&vi->refill, HZ/2);
2663 static int virtnet_receive_xsk_bufs(struct virtnet_info *vi,
2664 struct receive_queue *rq,
2666 unsigned int *xdp_xmit,
2667 struct virtnet_rq_stats *stats)
2673 while (packets < budget) {
2674 buf = virtqueue_get_buf(rq->vq, &len);
2678 virtnet_receive_xsk_buf(vi, rq, buf, len, xdp_xmit, stats);
2685 static int virtnet_receive_packets(struct virtnet_info *vi,
2686 struct receive_queue *rq,
2688 unsigned int *xdp_xmit,
2689 struct virtnet_rq_stats *stats)
2695 if (!vi->big_packets || vi->mergeable_rx_bufs) {
2697 while (packets < budget &&
2698 (buf = virtnet_rq_get_buf(rq, &len, &ctx))) {
2699 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, stats);
2703 while (packets < budget &&
2704 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
2705 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, stats);
2713 static int virtnet_receive(struct receive_queue *rq, int budget,
2714 unsigned int *xdp_xmit)
2716 struct virtnet_info *vi = rq->vq->vdev->priv;
2717 struct virtnet_rq_stats stats = {};
2721 packets = virtnet_receive_xsk_bufs(vi, rq, budget, xdp_xmit, &stats);
2723 packets = virtnet_receive_packets(vi, rq, budget, xdp_xmit, &stats);
2725 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
2726 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
2727 spin_lock(&vi->refill_lock);
2728 if (vi->refill_enabled)
2729 schedule_delayed_work(&vi->refill, 0);
2730 spin_unlock(&vi->refill_lock);
2734 u64_stats_set(&stats.packets, packets);
2735 u64_stats_update_begin(&rq->stats.syncp);
2736 for (i = 0; i < ARRAY_SIZE(virtnet_rq_stats_desc); i++) {
2737 size_t offset = virtnet_rq_stats_desc[i].offset;
2738 u64_stats_t *item, *src;
2740 item = (u64_stats_t *)((u8 *)&rq->stats + offset);
2741 src = (u64_stats_t *)((u8 *)&stats + offset);
2742 u64_stats_add(item, u64_stats_read(src));
2745 u64_stats_add(&rq->stats.packets, u64_stats_read(&stats.packets));
2746 u64_stats_add(&rq->stats.bytes, u64_stats_read(&stats.bytes));
2748 u64_stats_update_end(&rq->stats.syncp);
2753 static void virtnet_poll_cleantx(struct receive_queue *rq, int budget)
2755 struct virtnet_info *vi = rq->vq->vdev->priv;
2756 unsigned int index = vq2rxq(rq->vq);
2757 struct send_queue *sq = &vi->sq[index];
2758 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
2760 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
2763 if (__netif_tx_trylock(txq)) {
2765 __netif_tx_unlock(txq);
2770 virtqueue_disable_cb(sq->vq);
2771 free_old_xmit(sq, txq, !!budget);
2772 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2774 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) {
2775 if (netif_tx_queue_stopped(txq)) {
2776 u64_stats_update_begin(&sq->stats.syncp);
2777 u64_stats_inc(&sq->stats.wake);
2778 u64_stats_update_end(&sq->stats.syncp);
2780 netif_tx_wake_queue(txq);
2783 __netif_tx_unlock(txq);
2787 static void virtnet_rx_dim_update(struct virtnet_info *vi, struct receive_queue *rq)
2789 struct dim_sample cur_sample = {};
2791 if (!rq->packets_in_napi)
2794 /* Don't need protection when fetching stats, since fetcher and
2795 * updater of the stats are in same context
2797 dim_update_sample(rq->calls,
2798 u64_stats_read(&rq->stats.packets),
2799 u64_stats_read(&rq->stats.bytes),
2802 net_dim(&rq->dim, cur_sample);
2803 rq->packets_in_napi = 0;
2806 static int virtnet_poll(struct napi_struct *napi, int budget)
2808 struct receive_queue *rq =
2809 container_of(napi, struct receive_queue, napi);
2810 struct virtnet_info *vi = rq->vq->vdev->priv;
2811 struct send_queue *sq;
2812 unsigned int received;
2813 unsigned int xdp_xmit = 0;
2816 virtnet_poll_cleantx(rq, budget);
2818 received = virtnet_receive(rq, budget, &xdp_xmit);
2819 rq->packets_in_napi += received;
2821 if (xdp_xmit & VIRTIO_XDP_REDIR)
2824 /* Out of packets? */
2825 if (received < budget) {
2826 napi_complete = virtqueue_napi_complete(napi, rq->vq, received);
2827 /* Intentionally not taking dim_lock here. This may result in a
2828 * spurious net_dim call. But if that happens virtnet_rx_dim_work
2829 * will not act on the scheduled work.
2831 if (napi_complete && rq->dim_enabled)
2832 virtnet_rx_dim_update(vi, rq);
2835 if (xdp_xmit & VIRTIO_XDP_TX) {
2836 sq = virtnet_xdp_get_sq(vi);
2837 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2838 u64_stats_update_begin(&sq->stats.syncp);
2839 u64_stats_inc(&sq->stats.kicks);
2840 u64_stats_update_end(&sq->stats.syncp);
2842 virtnet_xdp_put_sq(vi, sq);
2848 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
2850 virtnet_napi_tx_disable(&vi->sq[qp_index].napi);
2851 napi_disable(&vi->rq[qp_index].napi);
2852 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
2855 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
2857 struct net_device *dev = vi->dev;
2860 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
2861 vi->rq[qp_index].napi.napi_id);
2865 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
2866 MEM_TYPE_PAGE_SHARED, NULL);
2868 goto err_xdp_reg_mem_model;
2870 netdev_tx_reset_queue(netdev_get_tx_queue(vi->dev, qp_index));
2871 virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi);
2872 virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi);
2876 err_xdp_reg_mem_model:
2877 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
2881 static void virtnet_cancel_dim(struct virtnet_info *vi, struct dim *dim)
2883 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
2885 net_dim_work_cancel(dim);
2888 static int virtnet_open(struct net_device *dev)
2890 struct virtnet_info *vi = netdev_priv(dev);
2893 enable_delayed_refill(vi);
2895 for (i = 0; i < vi->max_queue_pairs; i++) {
2896 if (i < vi->curr_queue_pairs)
2897 /* Make sure we have some buffers: if oom use wq. */
2898 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2899 schedule_delayed_work(&vi->refill, 0);
2901 err = virtnet_enable_queue_pair(vi, i);
2909 disable_delayed_refill(vi);
2910 cancel_delayed_work_sync(&vi->refill);
2912 for (i--; i >= 0; i--) {
2913 virtnet_disable_queue_pair(vi, i);
2914 virtnet_cancel_dim(vi, &vi->rq[i].dim);
2920 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
2922 struct send_queue *sq = container_of(napi, struct send_queue, napi);
2923 struct virtnet_info *vi = sq->vq->vdev->priv;
2924 unsigned int index = vq2txq(sq->vq);
2925 struct netdev_queue *txq;
2929 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
2930 /* We don't need to enable cb for XDP */
2931 napi_complete_done(napi, 0);
2935 txq = netdev_get_tx_queue(vi->dev, index);
2936 __netif_tx_lock(txq, raw_smp_processor_id());
2937 virtqueue_disable_cb(sq->vq);
2938 free_old_xmit(sq, txq, !!budget);
2940 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) {
2941 if (netif_tx_queue_stopped(txq)) {
2942 u64_stats_update_begin(&sq->stats.syncp);
2943 u64_stats_inc(&sq->stats.wake);
2944 u64_stats_update_end(&sq->stats.syncp);
2946 netif_tx_wake_queue(txq);
2949 opaque = virtqueue_enable_cb_prepare(sq->vq);
2951 done = napi_complete_done(napi, 0);
2954 virtqueue_disable_cb(sq->vq);
2956 __netif_tx_unlock(txq);
2959 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
2960 if (napi_schedule_prep(napi)) {
2961 __netif_tx_lock(txq, raw_smp_processor_id());
2962 virtqueue_disable_cb(sq->vq);
2963 __netif_tx_unlock(txq);
2964 __napi_schedule(napi);
2972 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
2974 struct virtio_net_hdr_mrg_rxbuf *hdr;
2975 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
2976 struct virtnet_info *vi = sq->vq->vdev->priv;
2978 unsigned hdr_len = vi->hdr_len;
2981 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
2983 can_push = vi->any_header_sg &&
2984 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
2985 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
2986 /* Even if we can, don't push here yet as this would skew
2987 * csum_start offset below. */
2989 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
2991 hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
2993 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
2994 virtio_is_little_endian(vi->vdev), false,
2998 if (vi->mergeable_rx_bufs)
2999 hdr->num_buffers = 0;
3001 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
3003 __skb_push(skb, hdr_len);
3004 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
3005 if (unlikely(num_sg < 0))
3007 /* Pull header back to avoid skew in tx bytes calculations. */
3008 __skb_pull(skb, hdr_len);
3010 sg_set_buf(sq->sg, hdr, hdr_len);
3011 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
3012 if (unlikely(num_sg < 0))
3016 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
3017 skb_to_ptr(skb, orphan), GFP_ATOMIC);
3020 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
3022 struct virtnet_info *vi = netdev_priv(dev);
3023 int qnum = skb_get_queue_mapping(skb);
3024 struct send_queue *sq = &vi->sq[qnum];
3026 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
3027 bool xmit_more = netdev_xmit_more();
3028 bool use_napi = sq->napi.weight;
3031 /* Free up any pending old buffers before queueing new ones. */
3034 virtqueue_disable_cb(sq->vq);
3036 free_old_xmit(sq, txq, false);
3038 } while (use_napi && !xmit_more &&
3039 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
3041 /* timestamp packet in software */
3042 skb_tx_timestamp(skb);
3044 /* Try to transmit */
3045 err = xmit_skb(sq, skb, !use_napi);
3047 /* This should not happen! */
3048 if (unlikely(err)) {
3049 DEV_STATS_INC(dev, tx_fifo_errors);
3050 if (net_ratelimit())
3052 "Unexpected TXQ (%d) queue failure: %d\n",
3054 DEV_STATS_INC(dev, tx_dropped);
3055 dev_kfree_skb_any(skb);
3056 return NETDEV_TX_OK;
3059 /* Don't wait up for transmitted skbs to be freed. */
3065 check_sq_full_and_disable(vi, dev, sq);
3067 kick = use_napi ? __netdev_tx_sent_queue(txq, skb->len, xmit_more) :
3068 !xmit_more || netif_xmit_stopped(txq);
3070 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
3071 u64_stats_update_begin(&sq->stats.syncp);
3072 u64_stats_inc(&sq->stats.kicks);
3073 u64_stats_update_end(&sq->stats.syncp);
3077 return NETDEV_TX_OK;
3080 static void virtnet_rx_pause(struct virtnet_info *vi, struct receive_queue *rq)
3082 bool running = netif_running(vi->dev);
3085 napi_disable(&rq->napi);
3086 virtnet_cancel_dim(vi, &rq->dim);
3090 static void virtnet_rx_resume(struct virtnet_info *vi, struct receive_queue *rq)
3092 bool running = netif_running(vi->dev);
3094 if (!try_fill_recv(vi, rq, GFP_KERNEL))
3095 schedule_delayed_work(&vi->refill, 0);
3098 virtnet_napi_enable(rq->vq, &rq->napi);
3101 static int virtnet_rx_resize(struct virtnet_info *vi,
3102 struct receive_queue *rq, u32 ring_num)
3106 qindex = rq - vi->rq;
3108 virtnet_rx_pause(vi, rq);
3110 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf);
3112 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
3114 virtnet_rx_resume(vi, rq);
3118 static void virtnet_tx_pause(struct virtnet_info *vi, struct send_queue *sq)
3120 bool running = netif_running(vi->dev);
3121 struct netdev_queue *txq;
3124 qindex = sq - vi->sq;
3127 virtnet_napi_tx_disable(&sq->napi);
3129 txq = netdev_get_tx_queue(vi->dev, qindex);
3131 /* 1. wait all ximt complete
3132 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
3134 __netif_tx_lock_bh(txq);
3136 /* Prevent rx poll from accessing sq. */
3139 /* Prevent the upper layer from trying to send packets. */
3140 netif_stop_subqueue(vi->dev, qindex);
3142 __netif_tx_unlock_bh(txq);
3145 static void virtnet_tx_resume(struct virtnet_info *vi, struct send_queue *sq)
3147 bool running = netif_running(vi->dev);
3148 struct netdev_queue *txq;
3151 qindex = sq - vi->sq;
3153 txq = netdev_get_tx_queue(vi->dev, qindex);
3155 __netif_tx_lock_bh(txq);
3157 netif_tx_wake_queue(txq);
3158 __netif_tx_unlock_bh(txq);
3161 virtnet_napi_tx_enable(vi, sq->vq, &sq->napi);
3164 static int virtnet_tx_resize(struct virtnet_info *vi, struct send_queue *sq,
3169 qindex = sq - vi->sq;
3171 virtnet_tx_pause(vi, sq);
3173 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf);
3175 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
3177 virtnet_tx_resume(vi, sq);
3183 * Send command via the control virtqueue and check status. Commands
3184 * supported by the hypervisor, as indicated by feature bits, should
3185 * never fail unless improperly formatted.
3187 static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd,
3188 struct scatterlist *out,
3189 struct scatterlist *in)
3191 struct scatterlist *sgs[5], hdr, stat;
3192 u32 out_num = 0, tmp, in_num = 0;
3196 /* Caller should know better */
3197 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
3199 mutex_lock(&vi->cvq_lock);
3200 vi->ctrl->status = ~0;
3201 vi->ctrl->hdr.class = class;
3202 vi->ctrl->hdr.cmd = cmd;
3204 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
3205 sgs[out_num++] = &hdr;
3208 sgs[out_num++] = out;
3210 /* Add return status. */
3211 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
3212 sgs[out_num + in_num++] = &stat;
3215 sgs[out_num + in_num++] = in;
3217 BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
3218 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC);
3220 dev_warn(&vi->vdev->dev,
3221 "Failed to add sgs for command vq: %d\n.", ret);
3222 mutex_unlock(&vi->cvq_lock);
3226 if (unlikely(!virtqueue_kick(vi->cvq)))
3229 /* Spin for a response, the kick causes an ioport write, trapping
3230 * into the hypervisor, so the request should be handled immediately.
3232 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
3233 !virtqueue_is_broken(vi->cvq)) {
3239 ok = vi->ctrl->status == VIRTIO_NET_OK;
3240 mutex_unlock(&vi->cvq_lock);
3244 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
3245 struct scatterlist *out)
3247 return virtnet_send_command_reply(vi, class, cmd, out, NULL);
3250 static int virtnet_set_mac_address(struct net_device *dev, void *p)
3252 struct virtnet_info *vi = netdev_priv(dev);
3253 struct virtio_device *vdev = vi->vdev;
3255 struct sockaddr *addr;
3256 struct scatterlist sg;
3258 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3261 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
3265 ret = eth_prepare_mac_addr_change(dev, addr);
3269 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
3270 sg_init_one(&sg, addr->sa_data, dev->addr_len);
3271 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
3272 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
3273 dev_warn(&vdev->dev,
3274 "Failed to set mac address by vq command.\n");
3278 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
3279 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3282 /* Naturally, this has an atomicity problem. */
3283 for (i = 0; i < dev->addr_len; i++)
3284 virtio_cwrite8(vdev,
3285 offsetof(struct virtio_net_config, mac) +
3286 i, addr->sa_data[i]);
3289 eth_commit_mac_addr_change(dev, p);
3297 static void virtnet_stats(struct net_device *dev,
3298 struct rtnl_link_stats64 *tot)
3300 struct virtnet_info *vi = netdev_priv(dev);
3304 for (i = 0; i < vi->max_queue_pairs; i++) {
3305 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
3306 struct receive_queue *rq = &vi->rq[i];
3307 struct send_queue *sq = &vi->sq[i];
3310 start = u64_stats_fetch_begin(&sq->stats.syncp);
3311 tpackets = u64_stats_read(&sq->stats.packets);
3312 tbytes = u64_stats_read(&sq->stats.bytes);
3313 terrors = u64_stats_read(&sq->stats.tx_timeouts);
3314 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
3317 start = u64_stats_fetch_begin(&rq->stats.syncp);
3318 rpackets = u64_stats_read(&rq->stats.packets);
3319 rbytes = u64_stats_read(&rq->stats.bytes);
3320 rdrops = u64_stats_read(&rq->stats.drops);
3321 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
3323 tot->rx_packets += rpackets;
3324 tot->tx_packets += tpackets;
3325 tot->rx_bytes += rbytes;
3326 tot->tx_bytes += tbytes;
3327 tot->rx_dropped += rdrops;
3328 tot->tx_errors += terrors;
3331 tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
3332 tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors);
3333 tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors);
3334 tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors);
3337 static void virtnet_ack_link_announce(struct virtnet_info *vi)
3339 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
3340 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
3341 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
3344 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
3346 struct virtio_net_ctrl_mq *mq __free(kfree) = NULL;
3347 struct scatterlist sg;
3348 struct net_device *dev = vi->dev;
3350 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
3353 mq = kzalloc(sizeof(*mq), GFP_KERNEL);
3357 mq->virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
3358 sg_init_one(&sg, mq, sizeof(*mq));
3360 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
3361 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
3362 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
3366 vi->curr_queue_pairs = queue_pairs;
3367 /* virtnet_open() will refill when device is going to up. */
3368 if (dev->flags & IFF_UP)
3369 schedule_delayed_work(&vi->refill, 0);
3375 static int virtnet_close(struct net_device *dev)
3377 struct virtnet_info *vi = netdev_priv(dev);
3380 /* Make sure NAPI doesn't schedule refill work */
3381 disable_delayed_refill(vi);
3382 /* Make sure refill_work doesn't re-enable napi! */
3383 cancel_delayed_work_sync(&vi->refill);
3385 for (i = 0; i < vi->max_queue_pairs; i++) {
3386 virtnet_disable_queue_pair(vi, i);
3387 virtnet_cancel_dim(vi, &vi->rq[i].dim);
3393 static void virtnet_rx_mode_work(struct work_struct *work)
3395 struct virtnet_info *vi =
3396 container_of(work, struct virtnet_info, rx_mode_work);
3397 u8 *promisc_allmulti __free(kfree) = NULL;
3398 struct net_device *dev = vi->dev;
3399 struct scatterlist sg[2];
3400 struct virtio_net_ctrl_mac *mac_data;
3401 struct netdev_hw_addr *ha;
3407 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
3408 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
3411 promisc_allmulti = kzalloc(sizeof(*promisc_allmulti), GFP_KERNEL);
3412 if (!promisc_allmulti) {
3413 dev_warn(&dev->dev, "Failed to set RX mode, no memory.\n");
3419 *promisc_allmulti = !!(dev->flags & IFF_PROMISC);
3420 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti));
3422 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
3423 VIRTIO_NET_CTRL_RX_PROMISC, sg))
3424 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
3425 *promisc_allmulti ? "en" : "dis");
3427 *promisc_allmulti = !!(dev->flags & IFF_ALLMULTI);
3428 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti));
3430 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
3431 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
3432 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
3433 *promisc_allmulti ? "en" : "dis");
3435 netif_addr_lock_bh(dev);
3437 uc_count = netdev_uc_count(dev);
3438 mc_count = netdev_mc_count(dev);
3439 /* MAC filter - use one buffer for both lists */
3440 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
3441 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
3444 netif_addr_unlock_bh(dev);
3449 sg_init_table(sg, 2);
3451 /* Store the unicast list and count in the front of the buffer */
3452 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
3454 netdev_for_each_uc_addr(ha, dev)
3455 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
3457 sg_set_buf(&sg[0], mac_data,
3458 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
3460 /* multicast list and count fill the end */
3461 mac_data = (void *)&mac_data->macs[uc_count][0];
3463 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
3465 netdev_for_each_mc_addr(ha, dev)
3466 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
3468 netif_addr_unlock_bh(dev);
3470 sg_set_buf(&sg[1], mac_data,
3471 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
3473 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
3474 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
3475 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
3482 static void virtnet_set_rx_mode(struct net_device *dev)
3484 struct virtnet_info *vi = netdev_priv(dev);
3486 if (vi->rx_mode_work_enabled)
3487 schedule_work(&vi->rx_mode_work);
3490 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
3491 __be16 proto, u16 vid)
3493 struct virtnet_info *vi = netdev_priv(dev);
3494 __virtio16 *_vid __free(kfree) = NULL;
3495 struct scatterlist sg;
3497 _vid = kzalloc(sizeof(*_vid), GFP_KERNEL);
3501 *_vid = cpu_to_virtio16(vi->vdev, vid);
3502 sg_init_one(&sg, _vid, sizeof(*_vid));
3504 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
3505 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
3506 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
3510 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
3511 __be16 proto, u16 vid)
3513 struct virtnet_info *vi = netdev_priv(dev);
3514 __virtio16 *_vid __free(kfree) = NULL;
3515 struct scatterlist sg;
3517 _vid = kzalloc(sizeof(*_vid), GFP_KERNEL);
3521 *_vid = cpu_to_virtio16(vi->vdev, vid);
3522 sg_init_one(&sg, _vid, sizeof(*_vid));
3524 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
3525 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
3526 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
3530 static void virtnet_clean_affinity(struct virtnet_info *vi)
3534 if (vi->affinity_hint_set) {
3535 for (i = 0; i < vi->max_queue_pairs; i++) {
3536 virtqueue_set_affinity(vi->rq[i].vq, NULL);
3537 virtqueue_set_affinity(vi->sq[i].vq, NULL);
3540 vi->affinity_hint_set = false;
3544 static void virtnet_set_affinity(struct virtnet_info *vi)
3553 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
3554 virtnet_clean_affinity(vi);
3558 num_cpu = num_online_cpus();
3559 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
3560 stragglers = num_cpu >= vi->curr_queue_pairs ?
3561 num_cpu % vi->curr_queue_pairs :
3563 cpu = cpumask_first(cpu_online_mask);
3565 for (i = 0; i < vi->curr_queue_pairs; i++) {
3566 group_size = stride + (i < stragglers ? 1 : 0);
3568 for (j = 0; j < group_size; j++) {
3569 cpumask_set_cpu(cpu, mask);
3570 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
3573 virtqueue_set_affinity(vi->rq[i].vq, mask);
3574 virtqueue_set_affinity(vi->sq[i].vq, mask);
3575 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
3576 cpumask_clear(mask);
3579 vi->affinity_hint_set = true;
3580 free_cpumask_var(mask);
3583 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
3585 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
3587 virtnet_set_affinity(vi);
3591 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
3593 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
3595 virtnet_set_affinity(vi);
3599 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
3601 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
3604 virtnet_clean_affinity(vi);
3608 static enum cpuhp_state virtionet_online;
3610 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
3614 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
3617 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
3621 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
3625 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
3627 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
3628 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
3632 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
3633 u16 vqn, u32 max_usecs, u32 max_packets)
3635 struct virtio_net_ctrl_coal_vq *coal_vq __free(kfree) = NULL;
3636 struct scatterlist sgs;
3638 coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL);
3642 coal_vq->vqn = cpu_to_le16(vqn);
3643 coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
3644 coal_vq->coal.max_packets = cpu_to_le32(max_packets);
3645 sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
3647 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3648 VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
3655 static int virtnet_send_rx_ctrl_coal_vq_cmd(struct virtnet_info *vi,
3656 u16 queue, u32 max_usecs,
3661 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
3664 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
3665 max_usecs, max_packets);
3669 vi->rq[queue].intr_coal.max_usecs = max_usecs;
3670 vi->rq[queue].intr_coal.max_packets = max_packets;
3675 static int virtnet_send_tx_ctrl_coal_vq_cmd(struct virtnet_info *vi,
3676 u16 queue, u32 max_usecs,
3681 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
3684 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
3685 max_usecs, max_packets);
3689 vi->sq[queue].intr_coal.max_usecs = max_usecs;
3690 vi->sq[queue].intr_coal.max_packets = max_packets;
3695 static void virtnet_get_ringparam(struct net_device *dev,
3696 struct ethtool_ringparam *ring,
3697 struct kernel_ethtool_ringparam *kernel_ring,
3698 struct netlink_ext_ack *extack)
3700 struct virtnet_info *vi = netdev_priv(dev);
3702 ring->rx_max_pending = vi->rq[0].vq->num_max;
3703 ring->tx_max_pending = vi->sq[0].vq->num_max;
3704 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
3705 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
3708 static int virtnet_set_ringparam(struct net_device *dev,
3709 struct ethtool_ringparam *ring,
3710 struct kernel_ethtool_ringparam *kernel_ring,
3711 struct netlink_ext_ack *extack)
3713 struct virtnet_info *vi = netdev_priv(dev);
3714 u32 rx_pending, tx_pending;
3715 struct receive_queue *rq;
3716 struct send_queue *sq;
3719 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
3722 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
3723 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
3725 if (ring->rx_pending == rx_pending &&
3726 ring->tx_pending == tx_pending)
3729 if (ring->rx_pending > vi->rq[0].vq->num_max)
3732 if (ring->tx_pending > vi->sq[0].vq->num_max)
3735 for (i = 0; i < vi->max_queue_pairs; i++) {
3739 if (ring->tx_pending != tx_pending) {
3740 err = virtnet_tx_resize(vi, sq, ring->tx_pending);
3744 /* Upon disabling and re-enabling a transmit virtqueue, the device must
3745 * set the coalescing parameters of the virtqueue to those configured
3746 * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver
3747 * did not set any TX coalescing parameters, to 0.
3749 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, i,
3750 vi->intr_coal_tx.max_usecs,
3751 vi->intr_coal_tx.max_packets);
3753 /* Don't break the tx resize action if the vq coalescing is not
3754 * supported. The same is true for rx resize below.
3756 if (err && err != -EOPNOTSUPP)
3760 if (ring->rx_pending != rx_pending) {
3761 err = virtnet_rx_resize(vi, rq, ring->rx_pending);
3765 /* The reason is same as the transmit virtqueue reset */
3766 mutex_lock(&vi->rq[i].dim_lock);
3767 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, i,
3768 vi->intr_coal_rx.max_usecs,
3769 vi->intr_coal_rx.max_packets);
3770 mutex_unlock(&vi->rq[i].dim_lock);
3771 if (err && err != -EOPNOTSUPP)
3779 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
3781 struct net_device *dev = vi->dev;
3782 struct scatterlist sgs[4];
3783 unsigned int sg_buf_size;
3786 sg_init_table(sgs, 4);
3788 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
3789 sg_set_buf(&sgs[0], &vi->rss, sg_buf_size);
3791 sg_buf_size = sizeof(uint16_t) * (vi->rss.indirection_table_mask + 1);
3792 sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
3794 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
3795 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
3796 sg_set_buf(&sgs[2], &vi->rss.max_tx_vq, sg_buf_size);
3798 sg_buf_size = vi->rss_key_size;
3799 sg_set_buf(&sgs[3], vi->rss.key, sg_buf_size);
3801 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
3802 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
3803 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs))
3809 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
3814 static void virtnet_init_default_rss(struct virtnet_info *vi)
3819 vi->rss.hash_types = vi->rss_hash_types_supported;
3820 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
3821 vi->rss.indirection_table_mask = vi->rss_indir_table_size
3822 ? vi->rss_indir_table_size - 1 : 0;
3823 vi->rss.unclassified_queue = 0;
3825 for (; i < vi->rss_indir_table_size; ++i) {
3826 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
3827 vi->rss.indirection_table[i] = indir_val;
3830 vi->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0;
3831 vi->rss.hash_key_length = vi->rss_key_size;
3833 netdev_rss_key_fill(vi->rss.key, vi->rss_key_size);
3836 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
3839 switch (info->flow_type) {
3841 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
3842 info->data = RXH_IP_SRC | RXH_IP_DST |
3843 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3844 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3845 info->data = RXH_IP_SRC | RXH_IP_DST;
3849 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
3850 info->data = RXH_IP_SRC | RXH_IP_DST |
3851 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3852 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3853 info->data = RXH_IP_SRC | RXH_IP_DST;
3857 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
3858 info->data = RXH_IP_SRC | RXH_IP_DST |
3859 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3860 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3861 info->data = RXH_IP_SRC | RXH_IP_DST;
3865 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
3866 info->data = RXH_IP_SRC | RXH_IP_DST |
3867 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3868 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3869 info->data = RXH_IP_SRC | RXH_IP_DST;
3873 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
3874 info->data = RXH_IP_SRC | RXH_IP_DST;
3878 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
3879 info->data = RXH_IP_SRC | RXH_IP_DST;
3888 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
3890 u32 new_hashtypes = vi->rss_hash_types_saved;
3891 bool is_disable = info->data & RXH_DISCARD;
3892 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
3894 /* supports only 'sd', 'sdfn' and 'r' */
3895 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
3898 switch (info->flow_type) {
3900 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
3902 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3903 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
3906 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
3908 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3909 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
3912 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3914 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3917 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
3919 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3920 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
3923 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
3925 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3926 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
3929 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3931 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3934 /* unsupported flow */
3938 /* if unsupported hashtype was set */
3939 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
3942 if (new_hashtypes != vi->rss_hash_types_saved) {
3943 vi->rss_hash_types_saved = new_hashtypes;
3944 vi->rss.hash_types = vi->rss_hash_types_saved;
3945 if (vi->dev->features & NETIF_F_RXHASH)
3946 return virtnet_commit_rss_command(vi);
3952 static void virtnet_get_drvinfo(struct net_device *dev,
3953 struct ethtool_drvinfo *info)
3955 struct virtnet_info *vi = netdev_priv(dev);
3956 struct virtio_device *vdev = vi->vdev;
3958 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
3959 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
3960 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
3964 /* TODO: Eliminate OOO packets during switching */
3965 static int virtnet_set_channels(struct net_device *dev,
3966 struct ethtool_channels *channels)
3968 struct virtnet_info *vi = netdev_priv(dev);
3969 u16 queue_pairs = channels->combined_count;
3972 /* We don't support separate rx/tx channels.
3973 * We don't allow setting 'other' channels.
3975 if (channels->rx_count || channels->tx_count || channels->other_count)
3978 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
3981 /* For now we don't support modifying channels while XDP is loaded
3982 * also when XDP is loaded all RX queues have XDP programs so we only
3983 * need to check a single RX queue.
3985 if (vi->rq[0].xdp_prog)
3989 err = virtnet_set_queues(vi, queue_pairs);
3994 virtnet_set_affinity(vi);
3997 netif_set_real_num_tx_queues(dev, queue_pairs);
3998 netif_set_real_num_rx_queues(dev, queue_pairs);
4003 static void virtnet_stats_sprintf(u8 **p, const char *fmt, const char *noq_fmt,
4004 int num, int qid, const struct virtnet_stat_desc *desc)
4009 for (i = 0; i < num; ++i)
4010 ethtool_sprintf(p, noq_fmt, desc[i].desc);
4012 for (i = 0; i < num; ++i)
4013 ethtool_sprintf(p, fmt, qid, desc[i].desc);
4017 /* qid == -1: for rx/tx queue total field */
4018 static void virtnet_get_stats_string(struct virtnet_info *vi, int type, int qid, u8 **data)
4020 const struct virtnet_stat_desc *desc;
4021 const char *fmt, *noq_fmt;
4025 if (type == VIRTNET_Q_TYPE_CQ && qid >= 0) {
4026 noq_fmt = "cq_hw_%s";
4028 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
4029 desc = &virtnet_stats_cvq_desc[0];
4030 num = ARRAY_SIZE(virtnet_stats_cvq_desc);
4032 virtnet_stats_sprintf(&p, NULL, noq_fmt, num, -1, desc);
4036 if (type == VIRTNET_Q_TYPE_RX) {
4040 desc = &virtnet_rq_stats_desc[0];
4041 num = ARRAY_SIZE(virtnet_rq_stats_desc);
4043 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4046 noq_fmt = "rx_hw_%s";
4048 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4049 desc = &virtnet_stats_rx_basic_desc[0];
4050 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
4052 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4055 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4056 desc = &virtnet_stats_rx_csum_desc[0];
4057 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
4059 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4062 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4063 desc = &virtnet_stats_rx_speed_desc[0];
4064 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
4066 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4070 if (type == VIRTNET_Q_TYPE_TX) {
4074 desc = &virtnet_sq_stats_desc[0];
4075 num = ARRAY_SIZE(virtnet_sq_stats_desc);
4077 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4080 noq_fmt = "tx_hw_%s";
4082 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4083 desc = &virtnet_stats_tx_basic_desc[0];
4084 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
4086 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4089 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4090 desc = &virtnet_stats_tx_gso_desc[0];
4091 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
4093 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4096 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4097 desc = &virtnet_stats_tx_speed_desc[0];
4098 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
4100 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4107 struct virtnet_stats_ctx {
4108 /* The stats are write to qstats or ethtool -S */
4111 /* Used to calculate the offset inside the output buffer. */
4114 /* The actual supported stat types. */
4117 /* Used to calculate the reply buffer size. */
4120 /* Record the output buffer. */
4124 static void virtnet_stats_ctx_init(struct virtnet_info *vi,
4125 struct virtnet_stats_ctx *ctx,
4126 u64 *data, bool to_qstat)
4131 ctx->to_qstat = to_qstat;
4134 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc_qstat);
4135 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc_qstat);
4137 queue_type = VIRTNET_Q_TYPE_RX;
4139 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4140 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC;
4141 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat);
4142 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic);
4145 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4146 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM;
4147 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat);
4148 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum);
4151 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
4152 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_GSO;
4153 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat);
4154 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_gso);
4157 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4158 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED;
4159 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat);
4160 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed);
4163 queue_type = VIRTNET_Q_TYPE_TX;
4165 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4166 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC;
4167 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat);
4168 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic);
4171 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
4172 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_CSUM;
4173 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat);
4174 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_csum);
4177 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4178 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO;
4179 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat);
4180 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso);
4183 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4184 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED;
4185 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat);
4186 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed);
4192 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc);
4193 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc);
4195 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
4196 queue_type = VIRTNET_Q_TYPE_CQ;
4198 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_CVQ;
4199 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_cvq_desc);
4200 ctx->size[queue_type] += sizeof(struct virtio_net_stats_cvq);
4203 queue_type = VIRTNET_Q_TYPE_RX;
4205 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4206 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC;
4207 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc);
4208 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic);
4211 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4212 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM;
4213 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc);
4214 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum);
4217 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4218 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED;
4219 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc);
4220 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed);
4223 queue_type = VIRTNET_Q_TYPE_TX;
4225 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4226 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC;
4227 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc);
4228 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic);
4231 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4232 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO;
4233 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc);
4234 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso);
4237 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4238 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED;
4239 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc);
4240 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed);
4244 /* stats_sum_queue - Calculate the sum of the same fields in sq or rq.
4245 * @sum: the position to store the sum values
4247 * @q_value: the first queue fields
4248 * @q_num: number of the queues
4250 static void stats_sum_queue(u64 *sum, u32 num, u64 *q_value, u32 q_num)
4256 for (i = 0; i < num; ++i) {
4260 for (j = 0; j < q_num; ++j)
4261 *p += *(q_value + i + j * step);
4265 static void virtnet_fill_total_fields(struct virtnet_info *vi,
4266 struct virtnet_stats_ctx *ctx)
4268 u64 *data, *first_rx_q, *first_tx_q;
4269 u32 num_cq, num_rx, num_tx;
4271 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
4272 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
4273 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
4275 first_rx_q = ctx->data + num_rx + num_tx + num_cq;
4276 first_tx_q = first_rx_q + vi->curr_queue_pairs * num_rx;
4280 stats_sum_queue(data, num_rx, first_rx_q, vi->curr_queue_pairs);
4282 data = ctx->data + num_rx;
4284 stats_sum_queue(data, num_tx, first_tx_q, vi->curr_queue_pairs);
4287 static void virtnet_fill_stats_qstat(struct virtnet_info *vi, u32 qid,
4288 struct virtnet_stats_ctx *ctx,
4289 const u8 *base, bool drv_stats, u8 reply_type)
4291 const struct virtnet_stat_desc *desc;
4292 const u64_stats_t *v_stat;
4298 queue_type = vq_type(vi, qid);
4299 bitmap = ctx->bitmap[queue_type];
4302 if (queue_type == VIRTNET_Q_TYPE_RX) {
4303 desc = &virtnet_rq_stats_desc_qstat[0];
4304 num = ARRAY_SIZE(virtnet_rq_stats_desc_qstat);
4306 desc = &virtnet_sq_stats_desc_qstat[0];
4307 num = ARRAY_SIZE(virtnet_sq_stats_desc_qstat);
4310 for (i = 0; i < num; ++i) {
4311 offset = desc[i].qstat_offset / sizeof(*ctx->data);
4312 v_stat = (const u64_stats_t *)(base + desc[i].offset);
4313 ctx->data[offset] = u64_stats_read(v_stat);
4318 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4319 desc = &virtnet_stats_rx_basic_desc_qstat[0];
4320 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat);
4321 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC)
4325 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4326 desc = &virtnet_stats_rx_csum_desc_qstat[0];
4327 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat);
4328 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM)
4332 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
4333 desc = &virtnet_stats_rx_gso_desc_qstat[0];
4334 num = ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat);
4335 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_GSO)
4339 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4340 desc = &virtnet_stats_rx_speed_desc_qstat[0];
4341 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat);
4342 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED)
4346 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4347 desc = &virtnet_stats_tx_basic_desc_qstat[0];
4348 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat);
4349 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC)
4353 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
4354 desc = &virtnet_stats_tx_csum_desc_qstat[0];
4355 num = ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat);
4356 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_CSUM)
4360 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4361 desc = &virtnet_stats_tx_gso_desc_qstat[0];
4362 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat);
4363 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO)
4367 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4368 desc = &virtnet_stats_tx_speed_desc_qstat[0];
4369 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat);
4370 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED)
4377 for (i = 0; i < num; ++i) {
4378 offset = desc[i].qstat_offset / sizeof(*ctx->data);
4379 v = (const __le64 *)(base + desc[i].offset);
4380 ctx->data[offset] = le64_to_cpu(*v);
4384 /* virtnet_fill_stats - copy the stats to qstats or ethtool -S
4385 * The stats source is the device or the driver.
4387 * @vi: virtio net info
4389 * @ctx: stats ctx (initiated by virtnet_stats_ctx_init())
4390 * @base: pointer to the device reply or the driver stats structure.
4391 * @drv_stats: designate the base type (device reply, driver stats)
4392 * @type: the type of the device reply (if drv_stats is true, this must be zero)
4394 static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid,
4395 struct virtnet_stats_ctx *ctx,
4396 const u8 *base, bool drv_stats, u8 reply_type)
4398 u32 queue_type, num_rx, num_tx, num_cq;
4399 const struct virtnet_stat_desc *desc;
4400 const u64_stats_t *v_stat;
4406 return virtnet_fill_stats_qstat(vi, qid, ctx, base, drv_stats, reply_type);
4408 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
4409 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
4410 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
4412 queue_type = vq_type(vi, qid);
4413 bitmap = ctx->bitmap[queue_type];
4415 /* skip the total fields of pairs */
4416 offset = num_rx + num_tx;
4418 if (queue_type == VIRTNET_Q_TYPE_TX) {
4419 offset += num_cq + num_rx * vi->curr_queue_pairs + num_tx * (qid / 2);
4421 num = ARRAY_SIZE(virtnet_sq_stats_desc);
4423 desc = &virtnet_sq_stats_desc[0];
4429 } else if (queue_type == VIRTNET_Q_TYPE_RX) {
4430 offset += num_cq + num_rx * (qid / 2);
4432 num = ARRAY_SIZE(virtnet_rq_stats_desc);
4434 desc = &virtnet_rq_stats_desc[0];
4441 if (bitmap & VIRTIO_NET_STATS_TYPE_CVQ) {
4442 desc = &virtnet_stats_cvq_desc[0];
4443 num = ARRAY_SIZE(virtnet_stats_cvq_desc);
4444 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_CVQ)
4450 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4451 desc = &virtnet_stats_rx_basic_desc[0];
4452 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
4453 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC)
4459 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4460 desc = &virtnet_stats_rx_csum_desc[0];
4461 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
4462 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM)
4468 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4469 desc = &virtnet_stats_rx_speed_desc[0];
4470 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
4471 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED)
4477 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4478 desc = &virtnet_stats_tx_basic_desc[0];
4479 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
4480 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC)
4486 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4487 desc = &virtnet_stats_tx_gso_desc[0];
4488 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
4489 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO)
4495 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4496 desc = &virtnet_stats_tx_speed_desc[0];
4497 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
4498 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED)
4507 for (i = 0; i < num; ++i) {
4508 v = (const __le64 *)(base + desc[i].offset);
4509 ctx->data[offset + i] = le64_to_cpu(*v);
4515 for (i = 0; i < num; ++i) {
4516 v_stat = (const u64_stats_t *)(base + desc[i].offset);
4517 ctx->data[offset + i] = u64_stats_read(v_stat);
4521 static int __virtnet_get_hw_stats(struct virtnet_info *vi,
4522 struct virtnet_stats_ctx *ctx,
4523 struct virtio_net_ctrl_queue_stats *req,
4524 int req_size, void *reply, int res_size)
4526 struct virtio_net_stats_reply_hdr *hdr;
4527 struct scatterlist sgs_in, sgs_out;
4532 sg_init_one(&sgs_out, req, req_size);
4533 sg_init_one(&sgs_in, reply, res_size);
4535 ok = virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
4536 VIRTIO_NET_CTRL_STATS_GET,
4542 for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
4544 qid = le16_to_cpu(hdr->vq_index);
4545 virtnet_fill_stats(vi, qid, ctx, p, false, hdr->type);
4551 static void virtnet_make_stat_req(struct virtnet_info *vi,
4552 struct virtnet_stats_ctx *ctx,
4553 struct virtio_net_ctrl_queue_stats *req,
4556 int qtype = vq_type(vi, qid);
4557 u64 bitmap = ctx->bitmap[qtype];
4562 req->stats[*idx].vq_index = cpu_to_le16(qid);
4563 req->stats[*idx].types_bitmap[0] = cpu_to_le64(bitmap);
4567 /* qid: -1: get stats of all vq.
4568 * > 0: get the stats for the special vq. This must not be cvq.
4570 static int virtnet_get_hw_stats(struct virtnet_info *vi,
4571 struct virtnet_stats_ctx *ctx, int qid)
4573 int qnum, i, j, res_size, qtype, last_vq, first_vq;
4574 struct virtio_net_ctrl_queue_stats *req;
4579 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS))
4583 last_vq = vi->curr_queue_pairs * 2 - 1;
4594 for (i = first_vq; i <= last_vq ; ++i) {
4595 qtype = vq_type(vi, i);
4596 if (ctx->bitmap[qtype]) {
4598 res_size += ctx->size[qtype];
4602 if (enable_cvq && ctx->bitmap[VIRTNET_Q_TYPE_CQ]) {
4603 res_size += ctx->size[VIRTNET_Q_TYPE_CQ];
4607 req = kcalloc(qnum, sizeof(*req), GFP_KERNEL);
4611 reply = kmalloc(res_size, GFP_KERNEL);
4618 for (i = first_vq; i <= last_vq ; ++i)
4619 virtnet_make_stat_req(vi, ctx, req, i, &j);
4622 virtnet_make_stat_req(vi, ctx, req, vi->max_queue_pairs * 2, &j);
4624 ok = __virtnet_get_hw_stats(vi, ctx, req, sizeof(*req) * j, reply, res_size);
4632 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4634 struct virtnet_info *vi = netdev_priv(dev);
4638 switch (stringset) {
4640 /* Generate the total field names. */
4641 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, -1, &p);
4642 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, -1, &p);
4644 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_CQ, 0, &p);
4646 for (i = 0; i < vi->curr_queue_pairs; ++i)
4647 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, i, &p);
4649 for (i = 0; i < vi->curr_queue_pairs; ++i)
4650 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, i, &p);
4655 static int virtnet_get_sset_count(struct net_device *dev, int sset)
4657 struct virtnet_info *vi = netdev_priv(dev);
4658 struct virtnet_stats_ctx ctx = {0};
4663 virtnet_stats_ctx_init(vi, &ctx, NULL, false);
4665 pair_count = ctx.desc_num[VIRTNET_Q_TYPE_RX] + ctx.desc_num[VIRTNET_Q_TYPE_TX];
4667 return pair_count + ctx.desc_num[VIRTNET_Q_TYPE_CQ] +
4668 vi->curr_queue_pairs * pair_count;
4674 static void virtnet_get_ethtool_stats(struct net_device *dev,
4675 struct ethtool_stats *stats, u64 *data)
4677 struct virtnet_info *vi = netdev_priv(dev);
4678 struct virtnet_stats_ctx ctx = {0};
4679 unsigned int start, i;
4680 const u8 *stats_base;
4682 virtnet_stats_ctx_init(vi, &ctx, data, false);
4683 if (virtnet_get_hw_stats(vi, &ctx, -1))
4684 dev_warn(&vi->dev->dev, "Failed to get hw stats.\n");
4686 for (i = 0; i < vi->curr_queue_pairs; i++) {
4687 struct receive_queue *rq = &vi->rq[i];
4688 struct send_queue *sq = &vi->sq[i];
4690 stats_base = (const u8 *)&rq->stats;
4692 start = u64_stats_fetch_begin(&rq->stats.syncp);
4693 virtnet_fill_stats(vi, i * 2, &ctx, stats_base, true, 0);
4694 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
4696 stats_base = (const u8 *)&sq->stats;
4698 start = u64_stats_fetch_begin(&sq->stats.syncp);
4699 virtnet_fill_stats(vi, i * 2 + 1, &ctx, stats_base, true, 0);
4700 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
4703 virtnet_fill_total_fields(vi, &ctx);
4706 static void virtnet_get_channels(struct net_device *dev,
4707 struct ethtool_channels *channels)
4709 struct virtnet_info *vi = netdev_priv(dev);
4711 channels->combined_count = vi->curr_queue_pairs;
4712 channels->max_combined = vi->max_queue_pairs;
4713 channels->max_other = 0;
4714 channels->rx_count = 0;
4715 channels->tx_count = 0;
4716 channels->other_count = 0;
4719 static int virtnet_set_link_ksettings(struct net_device *dev,
4720 const struct ethtool_link_ksettings *cmd)
4722 struct virtnet_info *vi = netdev_priv(dev);
4724 return ethtool_virtdev_set_link_ksettings(dev, cmd,
4725 &vi->speed, &vi->duplex);
4728 static int virtnet_get_link_ksettings(struct net_device *dev,
4729 struct ethtool_link_ksettings *cmd)
4731 struct virtnet_info *vi = netdev_priv(dev);
4733 cmd->base.speed = vi->speed;
4734 cmd->base.duplex = vi->duplex;
4735 cmd->base.port = PORT_OTHER;
4740 static int virtnet_send_tx_notf_coal_cmds(struct virtnet_info *vi,
4741 struct ethtool_coalesce *ec)
4743 struct virtio_net_ctrl_coal_tx *coal_tx __free(kfree) = NULL;
4744 struct scatterlist sgs_tx;
4747 coal_tx = kzalloc(sizeof(*coal_tx), GFP_KERNEL);
4751 coal_tx->tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
4752 coal_tx->tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
4753 sg_init_one(&sgs_tx, coal_tx, sizeof(*coal_tx));
4755 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
4756 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
4760 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs;
4761 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames;
4762 for (i = 0; i < vi->max_queue_pairs; i++) {
4763 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs;
4764 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames;
4770 static int virtnet_send_rx_notf_coal_cmds(struct virtnet_info *vi,
4771 struct ethtool_coalesce *ec)
4773 struct virtio_net_ctrl_coal_rx *coal_rx __free(kfree) = NULL;
4774 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce;
4775 struct scatterlist sgs_rx;
4778 if (rx_ctrl_dim_on && !virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
4781 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != vi->intr_coal_rx.max_usecs ||
4782 ec->rx_max_coalesced_frames != vi->intr_coal_rx.max_packets))
4785 if (rx_ctrl_dim_on && !vi->rx_dim_enabled) {
4786 vi->rx_dim_enabled = true;
4787 for (i = 0; i < vi->max_queue_pairs; i++) {
4788 mutex_lock(&vi->rq[i].dim_lock);
4789 vi->rq[i].dim_enabled = true;
4790 mutex_unlock(&vi->rq[i].dim_lock);
4795 coal_rx = kzalloc(sizeof(*coal_rx), GFP_KERNEL);
4799 if (!rx_ctrl_dim_on && vi->rx_dim_enabled) {
4800 vi->rx_dim_enabled = false;
4801 for (i = 0; i < vi->max_queue_pairs; i++) {
4802 mutex_lock(&vi->rq[i].dim_lock);
4803 vi->rq[i].dim_enabled = false;
4804 mutex_unlock(&vi->rq[i].dim_lock);
4808 /* Since the per-queue coalescing params can be set,
4809 * we need apply the global new params even if they
4812 coal_rx->rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
4813 coal_rx->rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
4814 sg_init_one(&sgs_rx, coal_rx, sizeof(*coal_rx));
4816 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
4817 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
4821 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs;
4822 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames;
4823 for (i = 0; i < vi->max_queue_pairs; i++) {
4824 mutex_lock(&vi->rq[i].dim_lock);
4825 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs;
4826 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames;
4827 mutex_unlock(&vi->rq[i].dim_lock);
4833 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
4834 struct ethtool_coalesce *ec)
4838 err = virtnet_send_tx_notf_coal_cmds(vi, ec);
4842 err = virtnet_send_rx_notf_coal_cmds(vi, ec);
4849 static int virtnet_send_rx_notf_coal_vq_cmds(struct virtnet_info *vi,
4850 struct ethtool_coalesce *ec,
4853 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce;
4854 u32 max_usecs, max_packets;
4858 mutex_lock(&vi->rq[queue].dim_lock);
4859 cur_rx_dim = vi->rq[queue].dim_enabled;
4860 max_usecs = vi->rq[queue].intr_coal.max_usecs;
4861 max_packets = vi->rq[queue].intr_coal.max_packets;
4863 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != max_usecs ||
4864 ec->rx_max_coalesced_frames != max_packets)) {
4865 mutex_unlock(&vi->rq[queue].dim_lock);
4869 if (rx_ctrl_dim_on && !cur_rx_dim) {
4870 vi->rq[queue].dim_enabled = true;
4871 mutex_unlock(&vi->rq[queue].dim_lock);
4875 if (!rx_ctrl_dim_on && cur_rx_dim)
4876 vi->rq[queue].dim_enabled = false;
4878 /* If no params are updated, userspace ethtool will
4879 * reject the modification.
4881 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, queue,
4882 ec->rx_coalesce_usecs,
4883 ec->rx_max_coalesced_frames);
4884 mutex_unlock(&vi->rq[queue].dim_lock);
4888 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
4889 struct ethtool_coalesce *ec,
4894 err = virtnet_send_rx_notf_coal_vq_cmds(vi, ec, queue);
4898 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, queue,
4899 ec->tx_coalesce_usecs,
4900 ec->tx_max_coalesced_frames);
4907 static void virtnet_rx_dim_work(struct work_struct *work)
4909 struct dim *dim = container_of(work, struct dim, work);
4910 struct receive_queue *rq = container_of(dim,
4911 struct receive_queue, dim);
4912 struct virtnet_info *vi = rq->vq->vdev->priv;
4913 struct net_device *dev = vi->dev;
4914 struct dim_cq_moder update_moder;
4919 mutex_lock(&rq->dim_lock);
4920 if (!rq->dim_enabled)
4923 update_moder = net_dim_get_rx_irq_moder(dev, dim);
4924 if (update_moder.usec != rq->intr_coal.max_usecs ||
4925 update_moder.pkts != rq->intr_coal.max_packets) {
4926 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, qnum,
4930 pr_debug("%s: Failed to send dim parameters on rxq%d\n",
4934 dim->state = DIM_START_MEASURE;
4935 mutex_unlock(&rq->dim_lock);
4938 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
4940 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
4941 * or VIRTIO_NET_F_VQ_NOTF_COAL feature is negotiated.
4943 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
4946 if (ec->tx_max_coalesced_frames > 1 ||
4947 ec->rx_max_coalesced_frames != 1)
4953 static int virtnet_should_update_vq_weight(int dev_flags, int weight,
4954 int vq_weight, bool *should_update)
4956 if (weight ^ vq_weight) {
4957 if (dev_flags & IFF_UP)
4959 *should_update = true;
4965 static int virtnet_set_coalesce(struct net_device *dev,
4966 struct ethtool_coalesce *ec,
4967 struct kernel_ethtool_coalesce *kernel_coal,
4968 struct netlink_ext_ack *extack)
4970 struct virtnet_info *vi = netdev_priv(dev);
4971 int ret, queue_number, napi_weight;
4972 bool update_napi = false;
4974 /* Can't change NAPI weight if the link is up */
4975 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
4976 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) {
4977 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
4978 vi->sq[queue_number].napi.weight,
4984 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be
4985 * updated for the sake of simplicity, which might not be necessary
4991 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
4992 ret = virtnet_send_notf_coal_cmds(vi, ec);
4994 ret = virtnet_coal_params_supported(ec);
5000 for (; queue_number < vi->max_queue_pairs; queue_number++)
5001 vi->sq[queue_number].napi.weight = napi_weight;
5007 static int virtnet_get_coalesce(struct net_device *dev,
5008 struct ethtool_coalesce *ec,
5009 struct kernel_ethtool_coalesce *kernel_coal,
5010 struct netlink_ext_ack *extack)
5012 struct virtnet_info *vi = netdev_priv(dev);
5014 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
5015 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
5016 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
5017 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
5018 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
5019 ec->use_adaptive_rx_coalesce = vi->rx_dim_enabled;
5021 ec->rx_max_coalesced_frames = 1;
5023 if (vi->sq[0].napi.weight)
5024 ec->tx_max_coalesced_frames = 1;
5030 static int virtnet_set_per_queue_coalesce(struct net_device *dev,
5032 struct ethtool_coalesce *ec)
5034 struct virtnet_info *vi = netdev_priv(dev);
5035 int ret, napi_weight;
5036 bool update_napi = false;
5038 if (queue >= vi->max_queue_pairs)
5041 /* Can't change NAPI weight if the link is up */
5042 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
5043 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
5044 vi->sq[queue].napi.weight,
5049 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
5050 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
5052 ret = virtnet_coal_params_supported(ec);
5058 vi->sq[queue].napi.weight = napi_weight;
5063 static int virtnet_get_per_queue_coalesce(struct net_device *dev,
5065 struct ethtool_coalesce *ec)
5067 struct virtnet_info *vi = netdev_priv(dev);
5069 if (queue >= vi->max_queue_pairs)
5072 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
5073 mutex_lock(&vi->rq[queue].dim_lock);
5074 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
5075 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
5076 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
5077 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
5078 ec->use_adaptive_rx_coalesce = vi->rq[queue].dim_enabled;
5079 mutex_unlock(&vi->rq[queue].dim_lock);
5081 ec->rx_max_coalesced_frames = 1;
5083 if (vi->sq[queue].napi.weight)
5084 ec->tx_max_coalesced_frames = 1;
5090 static void virtnet_init_settings(struct net_device *dev)
5092 struct virtnet_info *vi = netdev_priv(dev);
5094 vi->speed = SPEED_UNKNOWN;
5095 vi->duplex = DUPLEX_UNKNOWN;
5098 static void virtnet_update_settings(struct virtnet_info *vi)
5103 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
5106 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
5108 if (ethtool_validate_speed(speed))
5111 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
5113 if (ethtool_validate_duplex(duplex))
5114 vi->duplex = duplex;
5117 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
5119 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
5122 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
5124 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
5127 static int virtnet_get_rxfh(struct net_device *dev,
5128 struct ethtool_rxfh_param *rxfh)
5130 struct virtnet_info *vi = netdev_priv(dev);
5134 for (i = 0; i < vi->rss_indir_table_size; ++i)
5135 rxfh->indir[i] = vi->rss.indirection_table[i];
5139 memcpy(rxfh->key, vi->rss.key, vi->rss_key_size);
5141 rxfh->hfunc = ETH_RSS_HASH_TOP;
5146 static int virtnet_set_rxfh(struct net_device *dev,
5147 struct ethtool_rxfh_param *rxfh,
5148 struct netlink_ext_ack *extack)
5150 struct virtnet_info *vi = netdev_priv(dev);
5151 bool update = false;
5154 if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
5155 rxfh->hfunc != ETH_RSS_HASH_TOP)
5162 for (i = 0; i < vi->rss_indir_table_size; ++i)
5163 vi->rss.indirection_table[i] = rxfh->indir[i];
5168 /* If either _F_HASH_REPORT or _F_RSS are negotiated, the
5169 * device provides hash calculation capabilities, that is,
5170 * hash_key is configured.
5172 if (!vi->has_rss && !vi->has_rss_hash_report)
5175 memcpy(vi->rss.key, rxfh->key, vi->rss_key_size);
5180 virtnet_commit_rss_command(vi);
5185 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
5187 struct virtnet_info *vi = netdev_priv(dev);
5190 switch (info->cmd) {
5191 case ETHTOOL_GRXRINGS:
5192 info->data = vi->curr_queue_pairs;
5195 virtnet_get_hashflow(vi, info);
5204 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
5206 struct virtnet_info *vi = netdev_priv(dev);
5209 switch (info->cmd) {
5211 if (!virtnet_set_hashflow(vi, info))
5222 static const struct ethtool_ops virtnet_ethtool_ops = {
5223 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
5224 ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
5225 .get_drvinfo = virtnet_get_drvinfo,
5226 .get_link = ethtool_op_get_link,
5227 .get_ringparam = virtnet_get_ringparam,
5228 .set_ringparam = virtnet_set_ringparam,
5229 .get_strings = virtnet_get_strings,
5230 .get_sset_count = virtnet_get_sset_count,
5231 .get_ethtool_stats = virtnet_get_ethtool_stats,
5232 .set_channels = virtnet_set_channels,
5233 .get_channels = virtnet_get_channels,
5234 .get_ts_info = ethtool_op_get_ts_info,
5235 .get_link_ksettings = virtnet_get_link_ksettings,
5236 .set_link_ksettings = virtnet_set_link_ksettings,
5237 .set_coalesce = virtnet_set_coalesce,
5238 .get_coalesce = virtnet_get_coalesce,
5239 .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
5240 .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
5241 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
5242 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
5243 .get_rxfh = virtnet_get_rxfh,
5244 .set_rxfh = virtnet_set_rxfh,
5245 .get_rxnfc = virtnet_get_rxnfc,
5246 .set_rxnfc = virtnet_set_rxnfc,
5249 static void virtnet_get_queue_stats_rx(struct net_device *dev, int i,
5250 struct netdev_queue_stats_rx *stats)
5252 struct virtnet_info *vi = netdev_priv(dev);
5253 struct receive_queue *rq = &vi->rq[i];
5254 struct virtnet_stats_ctx ctx = {0};
5256 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true);
5258 virtnet_get_hw_stats(vi, &ctx, i * 2);
5259 virtnet_fill_stats(vi, i * 2, &ctx, (void *)&rq->stats, true, 0);
5262 static void virtnet_get_queue_stats_tx(struct net_device *dev, int i,
5263 struct netdev_queue_stats_tx *stats)
5265 struct virtnet_info *vi = netdev_priv(dev);
5266 struct send_queue *sq = &vi->sq[i];
5267 struct virtnet_stats_ctx ctx = {0};
5269 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true);
5271 virtnet_get_hw_stats(vi, &ctx, i * 2 + 1);
5272 virtnet_fill_stats(vi, i * 2 + 1, &ctx, (void *)&sq->stats, true, 0);
5275 static void virtnet_get_base_stats(struct net_device *dev,
5276 struct netdev_queue_stats_rx *rx,
5277 struct netdev_queue_stats_tx *tx)
5279 struct virtnet_info *vi = netdev_priv(dev);
5281 /* The queue stats of the virtio-net will not be reset. So here we
5287 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
5289 rx->hw_drop_overruns = 0;
5292 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
5293 rx->csum_unnecessary = 0;
5298 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
5299 rx->hw_gro_packets = 0;
5300 rx->hw_gro_bytes = 0;
5301 rx->hw_gro_wire_packets = 0;
5302 rx->hw_gro_wire_bytes = 0;
5305 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED)
5306 rx->hw_drop_ratelimits = 0;
5313 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
5315 tx->hw_drop_errors = 0;
5318 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
5323 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
5324 tx->hw_gso_packets = 0;
5325 tx->hw_gso_bytes = 0;
5326 tx->hw_gso_wire_packets = 0;
5327 tx->hw_gso_wire_bytes = 0;
5330 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED)
5331 tx->hw_drop_ratelimits = 0;
5334 static const struct netdev_stat_ops virtnet_stat_ops = {
5335 .get_queue_stats_rx = virtnet_get_queue_stats_rx,
5336 .get_queue_stats_tx = virtnet_get_queue_stats_tx,
5337 .get_base_stats = virtnet_get_base_stats,
5340 static void virtnet_freeze_down(struct virtio_device *vdev)
5342 struct virtnet_info *vi = vdev->priv;
5344 /* Make sure no work handler is accessing the device */
5345 flush_work(&vi->config_work);
5346 disable_rx_mode_work(vi);
5347 flush_work(&vi->rx_mode_work);
5349 netif_tx_lock_bh(vi->dev);
5350 netif_device_detach(vi->dev);
5351 netif_tx_unlock_bh(vi->dev);
5352 if (netif_running(vi->dev))
5353 virtnet_close(vi->dev);
5356 static int init_vqs(struct virtnet_info *vi);
5358 static int virtnet_restore_up(struct virtio_device *vdev)
5360 struct virtnet_info *vi = vdev->priv;
5367 virtio_device_ready(vdev);
5369 enable_delayed_refill(vi);
5370 enable_rx_mode_work(vi);
5372 if (netif_running(vi->dev)) {
5373 err = virtnet_open(vi->dev);
5378 netif_tx_lock_bh(vi->dev);
5379 netif_device_attach(vi->dev);
5380 netif_tx_unlock_bh(vi->dev);
5384 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
5386 __virtio64 *_offloads __free(kfree) = NULL;
5387 struct scatterlist sg;
5389 _offloads = kzalloc(sizeof(*_offloads), GFP_KERNEL);
5393 *_offloads = cpu_to_virtio64(vi->vdev, offloads);
5395 sg_init_one(&sg, _offloads, sizeof(*_offloads));
5397 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
5398 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
5399 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
5406 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
5410 if (!vi->guest_offloads)
5413 return virtnet_set_guest_offloads(vi, offloads);
5416 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
5418 u64 offloads = vi->guest_offloads;
5420 if (!vi->guest_offloads)
5423 return virtnet_set_guest_offloads(vi, offloads);
5426 static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queue *rq,
5427 struct xsk_buff_pool *pool)
5431 qindex = rq - vi->rq;
5434 err = xdp_rxq_info_reg(&rq->xsk_rxq_info, vi->dev, qindex, rq->napi.napi_id);
5438 err = xdp_rxq_info_reg_mem_model(&rq->xsk_rxq_info,
5439 MEM_TYPE_XSK_BUFF_POOL, NULL);
5443 xsk_pool_set_rxq_info(pool, &rq->xsk_rxq_info);
5446 virtnet_rx_pause(vi, rq);
5448 err = virtqueue_reset(rq->vq, virtnet_rq_unmap_free_buf);
5450 netdev_err(vi->dev, "reset rx fail: rx queue index: %d err: %d\n", qindex, err);
5455 rq->xsk_pool = pool;
5457 virtnet_rx_resume(vi, rq);
5463 xdp_rxq_info_unreg(&rq->xsk_rxq_info);
5467 static int virtnet_xsk_pool_enable(struct net_device *dev,
5468 struct xsk_buff_pool *pool,
5471 struct virtnet_info *vi = netdev_priv(dev);
5472 struct receive_queue *rq;
5473 struct device *dma_dev;
5474 struct send_queue *sq;
5477 if (vi->hdr_len > xsk_pool_get_headroom(pool))
5480 /* In big_packets mode, xdp cannot work, so there is no need to
5481 * initialize xsk of rq.
5483 if (vi->big_packets && !vi->mergeable_rx_bufs)
5486 if (qid >= vi->curr_queue_pairs)
5492 /* xsk assumes that tx and rx must have the same dma device. The af-xdp
5493 * may use one buffer to receive from the rx and reuse this buffer to
5494 * send by the tx. So the dma dev of sq and rq must be the same one.
5496 * But vq->dma_dev allows every vq has the respective dma dev. So I
5497 * check the dma dev of vq and sq is the same dev.
5499 if (virtqueue_dma_dev(rq->vq) != virtqueue_dma_dev(sq->vq))
5502 dma_dev = virtqueue_dma_dev(rq->vq);
5506 size = virtqueue_get_vring_size(rq->vq);
5508 rq->xsk_buffs = kvcalloc(size, sizeof(*rq->xsk_buffs), GFP_KERNEL);
5512 err = xsk_pool_dma_map(pool, dma_dev, 0);
5516 err = virtnet_rq_bind_xsk_pool(vi, rq, pool);
5523 xsk_pool_dma_unmap(pool, 0);
5528 static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)
5530 struct virtnet_info *vi = netdev_priv(dev);
5531 struct xsk_buff_pool *pool;
5532 struct receive_queue *rq;
5535 if (qid >= vi->curr_queue_pairs)
5540 pool = rq->xsk_pool;
5542 err = virtnet_rq_bind_xsk_pool(vi, rq, NULL);
5544 xsk_pool_dma_unmap(pool, 0);
5546 kvfree(rq->xsk_buffs);
5551 static int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp)
5554 return virtnet_xsk_pool_enable(dev, xdp->xsk.pool,
5557 return virtnet_xsk_pool_disable(dev, xdp->xsk.queue_id);
5560 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
5561 struct netlink_ext_ack *extack)
5563 unsigned int room = SKB_DATA_ALIGN(XDP_PACKET_HEADROOM +
5564 sizeof(struct skb_shared_info));
5565 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
5566 struct virtnet_info *vi = netdev_priv(dev);
5567 struct bpf_prog *old_prog;
5568 u16 xdp_qp = 0, curr_qp;
5571 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
5572 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
5573 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
5574 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
5575 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
5576 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
5577 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
5578 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
5579 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
5583 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
5584 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
5588 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
5589 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
5590 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
5594 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
5596 xdp_qp = nr_cpu_ids;
5598 /* XDP requires extra queues for XDP_TX */
5599 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
5600 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",
5601 curr_qp + xdp_qp, vi->max_queue_pairs);
5605 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
5606 if (!prog && !old_prog)
5610 bpf_prog_add(prog, vi->max_queue_pairs - 1);
5612 /* Make sure NAPI is not using any XDP TX queues for RX. */
5613 if (netif_running(dev)) {
5614 for (i = 0; i < vi->max_queue_pairs; i++) {
5615 napi_disable(&vi->rq[i].napi);
5616 virtnet_napi_tx_disable(&vi->sq[i].napi);
5621 for (i = 0; i < vi->max_queue_pairs; i++) {
5622 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
5624 virtnet_restore_guest_offloads(vi);
5629 err = virtnet_set_queues(vi, curr_qp + xdp_qp);
5632 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
5633 vi->xdp_queue_pairs = xdp_qp;
5636 vi->xdp_enabled = true;
5637 for (i = 0; i < vi->max_queue_pairs; i++) {
5638 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
5639 if (i == 0 && !old_prog)
5640 virtnet_clear_guest_offloads(vi);
5643 xdp_features_set_redirect_target(dev, true);
5645 xdp_features_clear_redirect_target(dev);
5646 vi->xdp_enabled = false;
5649 for (i = 0; i < vi->max_queue_pairs; i++) {
5651 bpf_prog_put(old_prog);
5652 if (netif_running(dev)) {
5653 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
5654 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
5663 virtnet_clear_guest_offloads(vi);
5664 for (i = 0; i < vi->max_queue_pairs; i++)
5665 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
5668 if (netif_running(dev)) {
5669 for (i = 0; i < vi->max_queue_pairs; i++) {
5670 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
5671 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
5676 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
5680 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
5682 switch (xdp->command) {
5683 case XDP_SETUP_PROG:
5684 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
5685 case XDP_SETUP_XSK_POOL:
5686 return virtnet_xsk_pool_setup(dev, xdp);
5692 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
5695 struct virtnet_info *vi = netdev_priv(dev);
5698 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
5701 ret = snprintf(buf, len, "sby");
5708 static int virtnet_set_features(struct net_device *dev,
5709 netdev_features_t features)
5711 struct virtnet_info *vi = netdev_priv(dev);
5715 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
5716 if (vi->xdp_enabled)
5719 if (features & NETIF_F_GRO_HW)
5720 offloads = vi->guest_offloads_capable;
5722 offloads = vi->guest_offloads_capable &
5723 ~GUEST_OFFLOAD_GRO_HW_MASK;
5725 err = virtnet_set_guest_offloads(vi, offloads);
5728 vi->guest_offloads = offloads;
5731 if ((dev->features ^ features) & NETIF_F_RXHASH) {
5732 if (features & NETIF_F_RXHASH)
5733 vi->rss.hash_types = vi->rss_hash_types_saved;
5735 vi->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
5737 if (!virtnet_commit_rss_command(vi))
5744 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
5746 struct virtnet_info *priv = netdev_priv(dev);
5747 struct send_queue *sq = &priv->sq[txqueue];
5748 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
5750 u64_stats_update_begin(&sq->stats.syncp);
5751 u64_stats_inc(&sq->stats.tx_timeouts);
5752 u64_stats_update_end(&sq->stats.syncp);
5754 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
5755 txqueue, sq->name, sq->vq->index, sq->vq->name,
5756 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
5759 static int virtnet_init_irq_moder(struct virtnet_info *vi)
5761 u8 profile_flags = 0, coal_flags = 0;
5764 profile_flags |= DIM_PROFILE_RX;
5765 coal_flags |= DIM_COALESCE_USEC | DIM_COALESCE_PKTS;
5766 ret = net_dim_init_irq_moder(vi->dev, profile_flags, coal_flags,
5767 DIM_CQ_PERIOD_MODE_START_FROM_EQE,
5768 0, virtnet_rx_dim_work, NULL);
5773 for (i = 0; i < vi->max_queue_pairs; i++)
5774 net_dim_setting(vi->dev, &vi->rq[i].dim, false);
5779 static void virtnet_free_irq_moder(struct virtnet_info *vi)
5781 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
5785 net_dim_free_irq_moder(vi->dev);
5789 static const struct net_device_ops virtnet_netdev = {
5790 .ndo_open = virtnet_open,
5791 .ndo_stop = virtnet_close,
5792 .ndo_start_xmit = start_xmit,
5793 .ndo_validate_addr = eth_validate_addr,
5794 .ndo_set_mac_address = virtnet_set_mac_address,
5795 .ndo_set_rx_mode = virtnet_set_rx_mode,
5796 .ndo_get_stats64 = virtnet_stats,
5797 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
5798 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
5799 .ndo_bpf = virtnet_xdp,
5800 .ndo_xdp_xmit = virtnet_xdp_xmit,
5801 .ndo_xsk_wakeup = virtnet_xsk_wakeup,
5802 .ndo_features_check = passthru_features_check,
5803 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
5804 .ndo_set_features = virtnet_set_features,
5805 .ndo_tx_timeout = virtnet_tx_timeout,
5808 static void virtnet_config_changed_work(struct work_struct *work)
5810 struct virtnet_info *vi =
5811 container_of(work, struct virtnet_info, config_work);
5814 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
5815 struct virtio_net_config, status, &v) < 0)
5818 if (v & VIRTIO_NET_S_ANNOUNCE) {
5819 netdev_notify_peers(vi->dev);
5820 virtnet_ack_link_announce(vi);
5823 /* Ignore unknown (future) status bits */
5824 v &= VIRTIO_NET_S_LINK_UP;
5826 if (vi->status == v)
5831 if (vi->status & VIRTIO_NET_S_LINK_UP) {
5832 virtnet_update_settings(vi);
5833 netif_carrier_on(vi->dev);
5834 netif_tx_wake_all_queues(vi->dev);
5836 netif_carrier_off(vi->dev);
5837 netif_tx_stop_all_queues(vi->dev);
5841 static void virtnet_config_changed(struct virtio_device *vdev)
5843 struct virtnet_info *vi = vdev->priv;
5845 schedule_work(&vi->config_work);
5848 static void virtnet_free_queues(struct virtnet_info *vi)
5852 for (i = 0; i < vi->max_queue_pairs; i++) {
5853 __netif_napi_del(&vi->rq[i].napi);
5854 __netif_napi_del(&vi->sq[i].napi);
5857 /* We called __netif_napi_del(),
5858 * we need to respect an RCU grace period before freeing vi->rq
5867 static void _free_receive_bufs(struct virtnet_info *vi)
5869 struct bpf_prog *old_prog;
5872 for (i = 0; i < vi->max_queue_pairs; i++) {
5873 while (vi->rq[i].pages)
5874 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
5876 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
5877 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
5879 bpf_prog_put(old_prog);
5883 static void free_receive_bufs(struct virtnet_info *vi)
5886 _free_receive_bufs(vi);
5890 static void free_receive_page_frags(struct virtnet_info *vi)
5893 for (i = 0; i < vi->max_queue_pairs; i++)
5894 if (vi->rq[i].alloc_frag.page) {
5895 if (vi->rq[i].last_dma)
5896 virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0);
5897 put_page(vi->rq[i].alloc_frag.page);
5901 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
5903 if (!is_xdp_frame(buf))
5906 xdp_return_frame(ptr_to_xdp(buf));
5909 static void free_unused_bufs(struct virtnet_info *vi)
5914 for (i = 0; i < vi->max_queue_pairs; i++) {
5915 struct virtqueue *vq = vi->sq[i].vq;
5916 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
5917 virtnet_sq_free_unused_buf(vq, buf);
5921 for (i = 0; i < vi->max_queue_pairs; i++) {
5922 struct virtqueue *vq = vi->rq[i].vq;
5924 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
5925 virtnet_rq_unmap_free_buf(vq, buf);
5930 static void virtnet_del_vqs(struct virtnet_info *vi)
5932 struct virtio_device *vdev = vi->vdev;
5934 virtnet_clean_affinity(vi);
5936 vdev->config->del_vqs(vdev);
5938 virtnet_free_queues(vi);
5941 /* How large should a single buffer be so a queue full of these can fit at
5942 * least one full packet?
5943 * Logic below assumes the mergeable buffer header is used.
5945 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
5947 const unsigned int hdr_len = vi->hdr_len;
5948 unsigned int rq_size = virtqueue_get_vring_size(vq);
5949 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
5950 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
5951 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
5953 return max(max(min_buf_len, hdr_len) - hdr_len,
5954 (unsigned int)GOOD_PACKET_LEN);
5957 static int virtnet_find_vqs(struct virtnet_info *vi)
5959 struct virtqueue_info *vqs_info;
5960 struct virtqueue **vqs;
5966 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
5967 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
5968 * possible control vq.
5970 total_vqs = vi->max_queue_pairs * 2 +
5971 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
5973 /* Allocate space for find_vqs parameters */
5974 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
5977 vqs_info = kcalloc(total_vqs, sizeof(*vqs_info), GFP_KERNEL);
5980 if (!vi->big_packets || vi->mergeable_rx_bufs) {
5981 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
5988 /* Parameters for control virtqueue, if any */
5990 vqs_info[total_vqs - 1].name = "control";
5993 /* Allocate/initialize parameters for send/receive virtqueues */
5994 for (i = 0; i < vi->max_queue_pairs; i++) {
5995 vqs_info[rxq2vq(i)].callback = skb_recv_done;
5996 vqs_info[txq2vq(i)].callback = skb_xmit_done;
5997 sprintf(vi->rq[i].name, "input.%u", i);
5998 sprintf(vi->sq[i].name, "output.%u", i);
5999 vqs_info[rxq2vq(i)].name = vi->rq[i].name;
6000 vqs_info[txq2vq(i)].name = vi->sq[i].name;
6002 vqs_info[rxq2vq(i)].ctx = true;
6005 ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, vqs_info, NULL);
6010 vi->cvq = vqs[total_vqs - 1];
6011 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
6012 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6015 for (i = 0; i < vi->max_queue_pairs; i++) {
6016 vi->rq[i].vq = vqs[rxq2vq(i)];
6017 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
6018 vi->sq[i].vq = vqs[txq2vq(i)];
6021 /* run here: ret == 0. */
6034 static int virtnet_alloc_queues(struct virtnet_info *vi)
6039 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
6045 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
6048 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
6052 INIT_DELAYED_WORK(&vi->refill, refill_work);
6053 for (i = 0; i < vi->max_queue_pairs; i++) {
6054 vi->rq[i].pages = NULL;
6055 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
6057 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
6059 napi_tx ? napi_weight : 0);
6061 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
6062 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
6063 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
6065 u64_stats_init(&vi->rq[i].stats.syncp);
6066 u64_stats_init(&vi->sq[i].stats.syncp);
6067 mutex_init(&vi->rq[i].dim_lock);
6080 static int init_vqs(struct virtnet_info *vi)
6084 /* Allocate send & receive queues */
6085 ret = virtnet_alloc_queues(vi);
6089 ret = virtnet_find_vqs(vi);
6093 virtnet_rq_set_premapped(vi);
6096 virtnet_set_affinity(vi);
6102 virtnet_free_queues(vi);
6108 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
6111 struct virtnet_info *vi = netdev_priv(queue->dev);
6112 unsigned int queue_index = get_netdev_rx_queue_index(queue);
6113 unsigned int headroom = virtnet_get_headroom(vi);
6114 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
6115 struct ewma_pkt_len *avg;
6117 BUG_ON(queue_index >= vi->max_queue_pairs);
6118 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
6119 return sprintf(buf, "%u\n",
6120 get_mergeable_buf_len(&vi->rq[queue_index], avg,
6121 SKB_DATA_ALIGN(headroom + tailroom)));
6124 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
6125 __ATTR_RO(mergeable_rx_buffer_size);
6127 static struct attribute *virtio_net_mrg_rx_attrs[] = {
6128 &mergeable_rx_buffer_size_attribute.attr,
6132 static const struct attribute_group virtio_net_mrg_rx_group = {
6133 .name = "virtio_net",
6134 .attrs = virtio_net_mrg_rx_attrs
6138 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
6140 const char *fname, const char *dname)
6142 if (!virtio_has_feature(vdev, fbit))
6145 dev_err(&vdev->dev, "device advertises feature %s but not %s",
6151 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
6152 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
6154 static bool virtnet_validate_features(struct virtio_device *vdev)
6156 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
6157 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
6158 "VIRTIO_NET_F_CTRL_VQ") ||
6159 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
6160 "VIRTIO_NET_F_CTRL_VQ") ||
6161 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
6162 "VIRTIO_NET_F_CTRL_VQ") ||
6163 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
6164 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
6165 "VIRTIO_NET_F_CTRL_VQ") ||
6166 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
6167 "VIRTIO_NET_F_CTRL_VQ") ||
6168 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
6169 "VIRTIO_NET_F_CTRL_VQ") ||
6170 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
6171 "VIRTIO_NET_F_CTRL_VQ") ||
6172 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL,
6173 "VIRTIO_NET_F_CTRL_VQ"))) {
6180 #define MIN_MTU ETH_MIN_MTU
6181 #define MAX_MTU ETH_MAX_MTU
6183 static int virtnet_validate(struct virtio_device *vdev)
6185 if (!vdev->config->get) {
6186 dev_err(&vdev->dev, "%s failure: config access disabled\n",
6191 if (!virtnet_validate_features(vdev))
6194 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
6195 int mtu = virtio_cread16(vdev,
6196 offsetof(struct virtio_net_config,
6199 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
6202 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
6203 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
6204 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
6205 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
6211 static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
6213 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
6214 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
6215 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
6216 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
6217 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
6218 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
6221 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
6223 bool guest_gso = virtnet_check_guest_gso(vi);
6225 /* If device can receive ANY guest GSO packets, regardless of mtu,
6226 * allocate packets of maximum size, otherwise limit it to only
6227 * mtu size worth only.
6229 if (mtu > ETH_DATA_LEN || guest_gso) {
6230 vi->big_packets = true;
6231 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
6235 #define VIRTIO_NET_HASH_REPORT_MAX_TABLE 10
6236 static enum xdp_rss_hash_type
6237 virtnet_xdp_rss_type[VIRTIO_NET_HASH_REPORT_MAX_TABLE] = {
6238 [VIRTIO_NET_HASH_REPORT_NONE] = XDP_RSS_TYPE_NONE,
6239 [VIRTIO_NET_HASH_REPORT_IPv4] = XDP_RSS_TYPE_L3_IPV4,
6240 [VIRTIO_NET_HASH_REPORT_TCPv4] = XDP_RSS_TYPE_L4_IPV4_TCP,
6241 [VIRTIO_NET_HASH_REPORT_UDPv4] = XDP_RSS_TYPE_L4_IPV4_UDP,
6242 [VIRTIO_NET_HASH_REPORT_IPv6] = XDP_RSS_TYPE_L3_IPV6,
6243 [VIRTIO_NET_HASH_REPORT_TCPv6] = XDP_RSS_TYPE_L4_IPV6_TCP,
6244 [VIRTIO_NET_HASH_REPORT_UDPv6] = XDP_RSS_TYPE_L4_IPV6_UDP,
6245 [VIRTIO_NET_HASH_REPORT_IPv6_EX] = XDP_RSS_TYPE_L3_IPV6_EX,
6246 [VIRTIO_NET_HASH_REPORT_TCPv6_EX] = XDP_RSS_TYPE_L4_IPV6_TCP_EX,
6247 [VIRTIO_NET_HASH_REPORT_UDPv6_EX] = XDP_RSS_TYPE_L4_IPV6_UDP_EX
6250 static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
6251 enum xdp_rss_hash_type *rss_type)
6253 const struct xdp_buff *xdp = (void *)_ctx;
6254 struct virtio_net_hdr_v1_hash *hdr_hash;
6255 struct virtnet_info *vi;
6258 if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
6261 vi = netdev_priv(xdp->rxq->dev);
6262 hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
6263 hash_report = __le16_to_cpu(hdr_hash->hash_report);
6265 if (hash_report >= VIRTIO_NET_HASH_REPORT_MAX_TABLE)
6266 hash_report = VIRTIO_NET_HASH_REPORT_NONE;
6268 *rss_type = virtnet_xdp_rss_type[hash_report];
6269 *hash = __le32_to_cpu(hdr_hash->hash_value);
6273 static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
6274 .xmo_rx_hash = virtnet_xdp_rx_hash,
6277 static int virtnet_probe(struct virtio_device *vdev)
6279 int i, err = -ENOMEM;
6280 struct net_device *dev;
6281 struct virtnet_info *vi;
6282 u16 max_queue_pairs;
6285 /* Find if host supports multiqueue/rss virtio_net device */
6286 max_queue_pairs = 1;
6287 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
6289 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
6291 /* We need at least 2 queue's */
6292 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
6293 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
6294 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
6295 max_queue_pairs = 1;
6297 /* Allocate ourselves a network device with room for our info */
6298 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
6302 /* Set up network device as normal. */
6303 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
6304 IFF_TX_SKB_NO_LINEAR;
6305 dev->netdev_ops = &virtnet_netdev;
6306 dev->stat_ops = &virtnet_stat_ops;
6307 dev->features = NETIF_F_HIGHDMA;
6309 dev->ethtool_ops = &virtnet_ethtool_ops;
6310 SET_NETDEV_DEV(dev, &vdev->dev);
6312 /* Do we support "hardware" checksums? */
6313 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
6314 /* This opens up the world of extra features. */
6315 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
6317 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
6319 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
6320 dev->hw_features |= NETIF_F_TSO
6321 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
6323 /* Individual feature bits: what can host handle? */
6324 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
6325 dev->hw_features |= NETIF_F_TSO;
6326 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
6327 dev->hw_features |= NETIF_F_TSO6;
6328 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
6329 dev->hw_features |= NETIF_F_TSO_ECN;
6330 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
6331 dev->hw_features |= NETIF_F_GSO_UDP_L4;
6333 dev->features |= NETIF_F_GSO_ROBUST;
6336 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
6337 /* (!csum && gso) case will be fixed by register_netdev() */
6340 /* 1. With VIRTIO_NET_F_GUEST_CSUM negotiation, the driver doesn't
6341 * need to calculate checksums for partially checksummed packets,
6342 * as they're considered valid by the upper layer.
6343 * 2. Without VIRTIO_NET_F_GUEST_CSUM negotiation, the driver only
6344 * receives fully checksummed packets. The device may assist in
6345 * validating these packets' checksums, so the driver won't have to.
6347 dev->features |= NETIF_F_RXCSUM;
6349 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
6350 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
6351 dev->features |= NETIF_F_GRO_HW;
6352 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
6353 dev->hw_features |= NETIF_F_GRO_HW;
6355 dev->vlan_features = dev->features;
6356 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
6358 /* MTU range: 68 - 65535 */
6359 dev->min_mtu = MIN_MTU;
6360 dev->max_mtu = MAX_MTU;
6362 /* Configuration may specify what MAC to use. Otherwise random. */
6363 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
6366 virtio_cread_bytes(vdev,
6367 offsetof(struct virtio_net_config, mac),
6369 eth_hw_addr_set(dev, addr);
6371 eth_hw_addr_random(dev);
6372 dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
6376 /* Set up our device-specific information */
6377 vi = netdev_priv(dev);
6382 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
6383 INIT_WORK(&vi->rx_mode_work, virtnet_rx_mode_work);
6384 spin_lock_init(&vi->refill_lock);
6386 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
6387 vi->mergeable_rx_bufs = true;
6388 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
6391 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
6392 vi->has_rss_hash_report = true;
6394 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) {
6397 vi->rss_indir_table_size =
6398 virtio_cread16(vdev, offsetof(struct virtio_net_config,
6399 rss_max_indirection_table_length));
6402 if (vi->has_rss || vi->has_rss_hash_report) {
6404 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
6406 vi->rss_hash_types_supported =
6407 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
6408 vi->rss_hash_types_supported &=
6409 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
6410 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
6411 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
6413 dev->hw_features |= NETIF_F_RXHASH;
6414 dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
6417 if (vi->has_rss_hash_report)
6418 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
6419 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
6420 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
6421 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
6423 vi->hdr_len = sizeof(struct virtio_net_hdr);
6425 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
6426 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
6427 vi->any_header_sg = true;
6429 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
6432 mutex_init(&vi->cvq_lock);
6434 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
6435 mtu = virtio_cread16(vdev,
6436 offsetof(struct virtio_net_config,
6438 if (mtu < dev->min_mtu) {
6439 /* Should never trigger: MTU was previously validated
6440 * in virtnet_validate.
6443 "device MTU appears to have changed it is now %d < %d",
6453 virtnet_set_big_packets(vi, mtu);
6455 if (vi->any_header_sg)
6456 dev->needed_headroom = vi->hdr_len;
6458 /* Enable multiqueue by default */
6459 if (num_online_cpus() >= max_queue_pairs)
6460 vi->curr_queue_pairs = max_queue_pairs;
6462 vi->curr_queue_pairs = num_online_cpus();
6463 vi->max_queue_pairs = max_queue_pairs;
6465 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
6470 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
6471 vi->intr_coal_rx.max_usecs = 0;
6472 vi->intr_coal_tx.max_usecs = 0;
6473 vi->intr_coal_rx.max_packets = 0;
6475 /* Keep the default values of the coalescing parameters
6476 * aligned with the default napi_tx state.
6478 if (vi->sq[0].napi.weight)
6479 vi->intr_coal_tx.max_packets = 1;
6481 vi->intr_coal_tx.max_packets = 0;
6484 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
6485 /* The reason is the same as VIRTIO_NET_F_NOTF_COAL. */
6486 for (i = 0; i < vi->max_queue_pairs; i++)
6487 if (vi->sq[i].napi.weight)
6488 vi->sq[i].intr_coal.max_packets = 1;
6490 err = virtnet_init_irq_moder(vi);
6496 if (vi->mergeable_rx_bufs)
6497 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
6499 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
6500 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
6502 virtnet_init_settings(dev);
6504 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
6505 vi->failover = net_failover_create(vi->dev);
6506 if (IS_ERR(vi->failover)) {
6507 err = PTR_ERR(vi->failover);
6512 if (vi->has_rss || vi->has_rss_hash_report)
6513 virtnet_init_default_rss(vi);
6515 enable_rx_mode_work(vi);
6517 /* serialize netdev register + virtio_device_ready() with ndo_open() */
6520 err = register_netdevice(dev);
6522 pr_debug("virtio_net: registering device failed\n");
6527 virtio_device_ready(vdev);
6529 virtnet_set_queues(vi, vi->curr_queue_pairs);
6531 /* a random MAC address has been assigned, notify the device.
6532 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
6533 * because many devices work fine without getting MAC explicitly
6535 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
6536 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
6537 struct scatterlist sg;
6539 sg_init_one(&sg, dev->dev_addr, dev->addr_len);
6540 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
6541 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
6542 pr_debug("virtio_net: setting MAC address failed\n");
6545 goto free_unregister_netdev;
6549 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS)) {
6550 struct virtio_net_stats_capabilities *stats_cap __free(kfree) = NULL;
6551 struct scatterlist sg;
6554 stats_cap = kzalloc(sizeof(*stats_cap), GFP_KERNEL);
6558 goto free_unregister_netdev;
6561 sg_init_one(&sg, stats_cap, sizeof(*stats_cap));
6563 if (!virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
6564 VIRTIO_NET_CTRL_STATS_QUERY,
6566 pr_debug("virtio_net: fail to get stats capability\n");
6569 goto free_unregister_netdev;
6572 v = stats_cap->supported_stats_types[0];
6573 vi->device_stats_cap = le64_to_cpu(v);
6578 err = virtnet_cpu_notif_add(vi);
6580 pr_debug("virtio_net: registering cpu notifier failed\n");
6581 goto free_unregister_netdev;
6584 /* Assume link up if device can't report link status,
6585 otherwise get link status from config. */
6586 netif_carrier_off(dev);
6587 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
6588 schedule_work(&vi->config_work);
6590 vi->status = VIRTIO_NET_S_LINK_UP;
6591 virtnet_update_settings(vi);
6592 netif_carrier_on(dev);
6595 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
6596 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
6597 set_bit(guest_offloads[i], &vi->guest_offloads);
6598 vi->guest_offloads_capable = vi->guest_offloads;
6600 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
6601 dev->name, max_queue_pairs);
6605 free_unregister_netdev:
6606 unregister_netdev(dev);
6608 net_failover_destroy(vi->failover);
6610 virtio_reset_device(vdev);
6611 cancel_delayed_work_sync(&vi->refill);
6612 free_receive_page_frags(vi);
6613 virtnet_del_vqs(vi);
6619 static void remove_vq_common(struct virtnet_info *vi)
6621 virtio_reset_device(vi->vdev);
6623 /* Free unused buffers in both send and recv, if any. */
6624 free_unused_bufs(vi);
6626 free_receive_bufs(vi);
6628 free_receive_page_frags(vi);
6630 virtnet_del_vqs(vi);
6633 static void virtnet_remove(struct virtio_device *vdev)
6635 struct virtnet_info *vi = vdev->priv;
6637 virtnet_cpu_notif_remove(vi);
6639 /* Make sure no work handler is accessing the device. */
6640 flush_work(&vi->config_work);
6641 disable_rx_mode_work(vi);
6642 flush_work(&vi->rx_mode_work);
6644 virtnet_free_irq_moder(vi);
6646 unregister_netdev(vi->dev);
6648 net_failover_destroy(vi->failover);
6650 remove_vq_common(vi);
6652 free_netdev(vi->dev);
6655 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
6657 struct virtnet_info *vi = vdev->priv;
6659 virtnet_cpu_notif_remove(vi);
6660 virtnet_freeze_down(vdev);
6661 remove_vq_common(vi);
6666 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
6668 struct virtnet_info *vi = vdev->priv;
6671 err = virtnet_restore_up(vdev);
6674 virtnet_set_queues(vi, vi->curr_queue_pairs);
6676 err = virtnet_cpu_notif_add(vi);
6678 virtnet_freeze_down(vdev);
6679 remove_vq_common(vi);
6686 static struct virtio_device_id id_table[] = {
6687 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
6691 #define VIRTNET_FEATURES \
6692 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
6694 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
6695 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
6696 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
6697 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
6698 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
6699 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
6700 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
6701 VIRTIO_NET_F_CTRL_MAC_ADDR, \
6702 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
6703 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
6704 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
6705 VIRTIO_NET_F_VQ_NOTF_COAL, \
6706 VIRTIO_NET_F_GUEST_HDRLEN, VIRTIO_NET_F_DEVICE_STATS
6708 static unsigned int features[] = {
6712 static unsigned int features_legacy[] = {
6715 VIRTIO_F_ANY_LAYOUT,
6718 static struct virtio_driver virtio_net_driver = {
6719 .feature_table = features,
6720 .feature_table_size = ARRAY_SIZE(features),
6721 .feature_table_legacy = features_legacy,
6722 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
6723 .driver.name = KBUILD_MODNAME,
6724 .id_table = id_table,
6725 .validate = virtnet_validate,
6726 .probe = virtnet_probe,
6727 .remove = virtnet_remove,
6728 .config_changed = virtnet_config_changed,
6729 #ifdef CONFIG_PM_SLEEP
6730 .freeze = virtnet_freeze,
6731 .restore = virtnet_restore,
6735 static __init int virtio_net_driver_init(void)
6739 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
6741 virtnet_cpu_down_prep);
6744 virtionet_online = ret;
6745 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
6746 NULL, virtnet_cpu_dead);
6749 ret = register_virtio_driver(&virtio_net_driver);
6754 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
6756 cpuhp_remove_multi_state(virtionet_online);
6760 module_init(virtio_net_driver_init);
6762 static __exit void virtio_net_driver_exit(void)
6764 unregister_virtio_driver(&virtio_net_driver);
6765 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
6766 cpuhp_remove_multi_state(virtionet_online);
6768 module_exit(virtio_net_driver_exit);
6770 MODULE_DEVICE_TABLE(virtio, id_table);
6771 MODULE_DESCRIPTION("Virtio network driver");
6772 MODULE_LICENSE("GPL");