1 /* A network driver using virtio.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/ethtool.h>
22 #include <linux/module.h>
23 #include <linux/virtio.h>
24 #include <linux/virtio_net.h>
25 #include <linux/bpf.h>
26 #include <linux/bpf_trace.h>
27 #include <linux/scatterlist.h>
28 #include <linux/if_vlan.h>
29 #include <linux/slab.h>
30 #include <linux/cpu.h>
31 #include <linux/average.h>
32 #include <linux/filter.h>
33 #include <linux/kernel.h>
34 #include <linux/pci.h>
35 #include <net/route.h>
37 #include <net/net_failover.h>
39 static int napi_weight = NAPI_POLL_WEIGHT;
40 module_param(napi_weight, int, 0444);
42 static bool csum = true, gso = true, napi_tx;
43 module_param(csum, bool, 0444);
44 module_param(gso, bool, 0444);
45 module_param(napi_tx, bool, 0644);
47 /* FIXME: MTU in config. */
48 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
49 #define GOOD_COPY_LEN 128
51 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
53 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
54 #define VIRTIO_XDP_HEADROOM 256
56 /* Separating two types of XDP xmit */
57 #define VIRTIO_XDP_TX BIT(0)
58 #define VIRTIO_XDP_REDIR BIT(1)
60 /* RX packet size EWMA. The average packet size is used to determine the packet
61 * buffer size when refilling RX rings. As the entire RX ring may be refilled
62 * at once, the weight is chosen so that the EWMA will be insensitive to short-
63 * term, transient changes in packet size.
65 DECLARE_EWMA(pkt_len, 0, 64)
67 #define VIRTNET_DRIVER_VERSION "1.0.0"
69 static const unsigned long guest_offloads[] = {
70 VIRTIO_NET_F_GUEST_TSO4,
71 VIRTIO_NET_F_GUEST_TSO6,
72 VIRTIO_NET_F_GUEST_ECN,
73 VIRTIO_NET_F_GUEST_UFO,
74 VIRTIO_NET_F_GUEST_CSUM
77 struct virtnet_stat_desc {
78 char desc[ETH_GSTRING_LEN];
82 struct virtnet_sq_stats {
83 struct u64_stats_sync syncp;
91 struct virtnet_rq_stats {
92 struct u64_stats_sync syncp;
103 #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
104 #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
106 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
107 { "packets", VIRTNET_SQ_STAT(packets) },
108 { "bytes", VIRTNET_SQ_STAT(bytes) },
109 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
110 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
111 { "kicks", VIRTNET_SQ_STAT(kicks) },
114 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
115 { "packets", VIRTNET_RQ_STAT(packets) },
116 { "bytes", VIRTNET_RQ_STAT(bytes) },
117 { "drops", VIRTNET_RQ_STAT(drops) },
118 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
119 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
120 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
121 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
122 { "kicks", VIRTNET_RQ_STAT(kicks) },
125 #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
126 #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
128 /* Internal representation of a send virtqueue */
130 /* Virtqueue associated with this send _queue */
131 struct virtqueue *vq;
133 /* TX: fragments + linear part + virtio header */
134 struct scatterlist sg[MAX_SKB_FRAGS + 2];
136 /* Name of the send queue: output.$index */
139 struct virtnet_sq_stats stats;
141 struct napi_struct napi;
144 /* Internal representation of a receive virtqueue */
145 struct receive_queue {
146 /* Virtqueue associated with this receive_queue */
147 struct virtqueue *vq;
149 struct napi_struct napi;
151 struct bpf_prog __rcu *xdp_prog;
153 struct virtnet_rq_stats stats;
155 /* Chain pages by the private ptr. */
158 /* Average packet length for mergeable receive buffers. */
159 struct ewma_pkt_len mrg_avg_pkt_len;
161 /* Page frag for packet buffer allocation. */
162 struct page_frag alloc_frag;
164 /* RX: fragments + linear part + virtio header */
165 struct scatterlist sg[MAX_SKB_FRAGS + 2];
167 /* Min single buffer size for mergeable buffers case. */
168 unsigned int min_buf_len;
170 /* Name of this receive queue: input.$index */
173 struct xdp_rxq_info xdp_rxq;
176 /* Control VQ buffers: protected by the rtnl lock */
178 struct virtio_net_ctrl_hdr hdr;
179 virtio_net_ctrl_ack status;
180 struct virtio_net_ctrl_mq mq;
187 struct virtnet_info {
188 struct virtio_device *vdev;
189 struct virtqueue *cvq;
190 struct net_device *dev;
191 struct send_queue *sq;
192 struct receive_queue *rq;
195 /* Max # of queue pairs supported by the device */
198 /* # of queue pairs currently used by the driver */
199 u16 curr_queue_pairs;
201 /* # of XDP queue pairs currently used by the driver */
204 /* I like... big packets and I cannot lie! */
207 /* Host will merge rx buffers for big packets (shake it! shake it!) */
208 bool mergeable_rx_bufs;
210 /* Has control virtqueue */
213 /* Host can handle any s/g split between our header and packet data */
216 /* Packet virtio header size */
219 /* Work struct for refilling if we run low on memory. */
220 struct delayed_work refill;
222 /* Work struct for config space updates */
223 struct work_struct config_work;
225 /* Does the affinity hint is set for virtqueues? */
226 bool affinity_hint_set;
228 /* CPU hotplug instances for online & dead */
229 struct hlist_node node;
230 struct hlist_node node_dead;
232 struct control_buf *ctrl;
234 /* Ethtool settings */
238 unsigned long guest_offloads;
240 /* failover when STANDBY feature enabled */
241 struct failover *failover;
244 struct padded_vnet_hdr {
245 struct virtio_net_hdr_mrg_rxbuf hdr;
247 * hdr is in a separate sg buffer, and data sg buffer shares same page
248 * with this header sg. This padding makes next sg 16 byte aligned
254 /* Converting between virtqueue no. and kernel tx/rx queue no.
255 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
257 static int vq2txq(struct virtqueue *vq)
259 return (vq->index - 1) / 2;
262 static int txq2vq(int txq)
267 static int vq2rxq(struct virtqueue *vq)
269 return vq->index / 2;
272 static int rxq2vq(int rxq)
277 static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
279 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
283 * private is used to chain pages for big packets, put the whole
284 * most recent used list in the beginning for reuse
286 static void give_pages(struct receive_queue *rq, struct page *page)
290 /* Find end of list, sew whole thing into vi->rq.pages. */
291 for (end = page; end->private; end = (struct page *)end->private);
292 end->private = (unsigned long)rq->pages;
296 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
298 struct page *p = rq->pages;
301 rq->pages = (struct page *)p->private;
302 /* clear private here, it is used to chain pages */
305 p = alloc_page(gfp_mask);
309 static void virtqueue_napi_schedule(struct napi_struct *napi,
310 struct virtqueue *vq)
312 if (napi_schedule_prep(napi)) {
313 virtqueue_disable_cb(vq);
314 __napi_schedule(napi);
318 static void virtqueue_napi_complete(struct napi_struct *napi,
319 struct virtqueue *vq, int processed)
323 opaque = virtqueue_enable_cb_prepare(vq);
324 if (napi_complete_done(napi, processed)) {
325 if (unlikely(virtqueue_poll(vq, opaque)))
326 virtqueue_napi_schedule(napi, vq);
328 virtqueue_disable_cb(vq);
332 static void skb_xmit_done(struct virtqueue *vq)
334 struct virtnet_info *vi = vq->vdev->priv;
335 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
337 /* Suppress further interrupts. */
338 virtqueue_disable_cb(vq);
341 virtqueue_napi_schedule(napi, vq);
343 /* We were probably waiting for more output buffers. */
344 netif_wake_subqueue(vi->dev, vq2txq(vq));
347 #define MRG_CTX_HEADER_SHIFT 22
348 static void *mergeable_len_to_ctx(unsigned int truesize,
349 unsigned int headroom)
351 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
354 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
356 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
359 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
361 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
364 /* Called from bottom half context */
365 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
366 struct receive_queue *rq,
367 struct page *page, unsigned int offset,
368 unsigned int len, unsigned int truesize)
371 struct virtio_net_hdr_mrg_rxbuf *hdr;
372 unsigned int copy, hdr_len, hdr_padded_len;
375 p = page_address(page) + offset;
377 /* copy small packet so we can reuse these pages for small data */
378 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
382 hdr = skb_vnet_hdr(skb);
384 hdr_len = vi->hdr_len;
385 if (vi->mergeable_rx_bufs)
386 hdr_padded_len = sizeof(*hdr);
388 hdr_padded_len = sizeof(struct padded_vnet_hdr);
390 memcpy(hdr, p, hdr_len);
393 offset += hdr_padded_len;
397 if (copy > skb_tailroom(skb))
398 copy = skb_tailroom(skb);
399 skb_put_data(skb, p, copy);
404 if (vi->mergeable_rx_bufs) {
406 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
413 * Verify that we can indeed put this data into a skb.
414 * This is here to handle cases when the device erroneously
415 * tries to receive more than is possible. This is usually
416 * the case of a broken device.
418 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
419 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
423 BUG_ON(offset >= PAGE_SIZE);
425 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
426 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
427 frag_size, truesize);
429 page = (struct page *)page->private;
434 give_pages(rq, page);
439 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
440 struct send_queue *sq,
441 struct xdp_frame *xdpf)
443 struct virtio_net_hdr_mrg_rxbuf *hdr;
446 /* virtqueue want to use data area in-front of packet */
447 if (unlikely(xdpf->metasize > 0))
450 if (unlikely(xdpf->headroom < vi->hdr_len))
453 /* Make room for virtqueue hdr (also change xdpf->headroom?) */
454 xdpf->data -= vi->hdr_len;
455 /* Zero header and leave csum up to XDP layers */
457 memset(hdr, 0, vi->hdr_len);
458 xdpf->len += vi->hdr_len;
460 sg_init_one(sq->sg, xdpf->data, xdpf->len);
462 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdpf, GFP_ATOMIC);
464 return -ENOSPC; /* Caller handle free/refcnt */
469 static struct send_queue *virtnet_xdp_sq(struct virtnet_info *vi)
473 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
477 static int virtnet_xdp_xmit(struct net_device *dev,
478 int n, struct xdp_frame **frames, u32 flags)
480 struct virtnet_info *vi = netdev_priv(dev);
481 struct receive_queue *rq = vi->rq;
482 struct xdp_frame *xdpf_sent;
483 struct bpf_prog *xdp_prog;
484 struct send_queue *sq;
491 sq = virtnet_xdp_sq(vi);
493 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
499 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
500 * indicate XDP resources have been successfully allocated.
502 xdp_prog = rcu_dereference(rq->xdp_prog);
509 /* Free up any pending old buffers before queueing new ones. */
510 while ((xdpf_sent = virtqueue_get_buf(sq->vq, &len)) != NULL)
511 xdp_return_frame(xdpf_sent);
513 for (i = 0; i < n; i++) {
514 struct xdp_frame *xdpf = frames[i];
516 err = __virtnet_xdp_xmit_one(vi, sq, xdpf);
518 xdp_return_frame_rx_napi(xdpf);
524 if (flags & XDP_XMIT_FLUSH) {
525 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
529 u64_stats_update_begin(&sq->stats.syncp);
530 sq->stats.xdp_tx += n;
531 sq->stats.xdp_tx_drops += drops;
532 sq->stats.kicks += kicks;
533 u64_stats_update_end(&sq->stats.syncp);
538 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
540 return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
543 /* We copy the packet for XDP in the following cases:
545 * 1) Packet is scattered across multiple rx buffers.
546 * 2) Headroom space is insufficient.
548 * This is inefficient but it's a temporary condition that
549 * we hit right after XDP is enabled and until queue is refilled
550 * with large buffers with sufficient headroom - so it should affect
551 * at most queue size packets.
552 * Afterwards, the conditions to enable
553 * XDP should preclude the underlying device from sending packets
554 * across multiple buffers (num_buf > 1), and we make sure buffers
555 * have enough headroom.
557 static struct page *xdp_linearize_page(struct receive_queue *rq,
564 struct page *page = alloc_page(GFP_ATOMIC);
569 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
573 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
578 buf = virtqueue_get_buf(rq->vq, &buflen);
582 p = virt_to_head_page(buf);
583 off = buf - page_address(p);
585 /* guard against a misconfigured or uncooperative backend that
586 * is sending packet larger than the MTU.
588 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
593 memcpy(page_address(page) + page_off,
594 page_address(p) + off, buflen);
599 /* Headroom does not contribute to packet length */
600 *len = page_off - VIRTIO_XDP_HEADROOM;
603 __free_pages(page, 0);
607 static struct sk_buff *receive_small(struct net_device *dev,
608 struct virtnet_info *vi,
609 struct receive_queue *rq,
610 void *buf, void *ctx,
612 unsigned int *xdp_xmit,
613 struct virtnet_rq_stats *stats)
616 struct bpf_prog *xdp_prog;
617 unsigned int xdp_headroom = (unsigned long)ctx;
618 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
619 unsigned int headroom = vi->hdr_len + header_offset;
620 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
621 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
622 struct page *page = virt_to_head_page(buf);
623 unsigned int delta = 0;
624 struct page *xdp_page;
631 xdp_prog = rcu_dereference(rq->xdp_prog);
633 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
634 struct xdp_frame *xdpf;
639 if (unlikely(hdr->hdr.gso_type))
642 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
643 int offset = buf - page_address(page) + header_offset;
644 unsigned int tlen = len + vi->hdr_len;
647 xdp_headroom = virtnet_get_headroom(vi);
648 header_offset = VIRTNET_RX_PAD + xdp_headroom;
649 headroom = vi->hdr_len + header_offset;
650 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
651 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
652 xdp_page = xdp_linearize_page(rq, &num_buf, page,
653 offset, header_offset,
658 buf = page_address(xdp_page);
663 xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
664 xdp.data = xdp.data_hard_start + xdp_headroom;
665 xdp_set_data_meta_invalid(&xdp);
666 xdp.data_end = xdp.data + len;
667 xdp.rxq = &rq->xdp_rxq;
668 orig_data = xdp.data;
669 act = bpf_prog_run_xdp(xdp_prog, &xdp);
670 stats->xdp_packets++;
674 /* Recalculate length in case bpf program changed it */
675 delta = orig_data - xdp.data;
676 len = xdp.data_end - xdp.data;
680 xdpf = convert_to_xdp_frame(&xdp);
683 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
684 if (unlikely(err < 0)) {
685 trace_xdp_exception(vi->dev, xdp_prog, act);
688 *xdp_xmit |= VIRTIO_XDP_TX;
692 stats->xdp_redirects++;
693 err = xdp_do_redirect(dev, &xdp, xdp_prog);
696 *xdp_xmit |= VIRTIO_XDP_REDIR;
700 bpf_warn_invalid_xdp_action(act);
703 trace_xdp_exception(vi->dev, xdp_prog, act);
710 skb = build_skb(buf, buflen);
715 skb_reserve(skb, headroom - delta);
718 buf += header_offset;
719 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
720 } /* keep zeroed vnet hdr since packet was changed by bpf */
734 static struct sk_buff *receive_big(struct net_device *dev,
735 struct virtnet_info *vi,
736 struct receive_queue *rq,
739 struct virtnet_rq_stats *stats)
741 struct page *page = buf;
742 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
744 stats->bytes += len - vi->hdr_len;
752 give_pages(rq, page);
756 static struct sk_buff *receive_mergeable(struct net_device *dev,
757 struct virtnet_info *vi,
758 struct receive_queue *rq,
762 unsigned int *xdp_xmit,
763 struct virtnet_rq_stats *stats)
765 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
766 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
767 struct page *page = virt_to_head_page(buf);
768 int offset = buf - page_address(page);
769 struct sk_buff *head_skb, *curr_skb;
770 struct bpf_prog *xdp_prog;
771 unsigned int truesize;
772 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
776 stats->bytes += len - vi->hdr_len;
779 xdp_prog = rcu_dereference(rq->xdp_prog);
781 struct xdp_frame *xdpf;
782 struct page *xdp_page;
787 /* Transient failure which in theory could occur if
788 * in-flight packets from before XDP was enabled reach
789 * the receive path after XDP is loaded.
791 if (unlikely(hdr->hdr.gso_type))
794 /* This happens when rx buffer size is underestimated
795 * or headroom is not enough because of the buffer
796 * was refilled before XDP is set. This should only
797 * happen for the first several packets, so we don't
798 * care much about its performance.
800 if (unlikely(num_buf > 1 ||
801 headroom < virtnet_get_headroom(vi))) {
802 /* linearize data for XDP */
803 xdp_page = xdp_linearize_page(rq, &num_buf,
809 offset = VIRTIO_XDP_HEADROOM;
814 /* Allow consuming headroom but reserve enough space to push
815 * the descriptor on if we get an XDP_TX return code.
817 data = page_address(xdp_page) + offset;
818 xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
819 xdp.data = data + vi->hdr_len;
820 xdp_set_data_meta_invalid(&xdp);
821 xdp.data_end = xdp.data + (len - vi->hdr_len);
822 xdp.rxq = &rq->xdp_rxq;
824 act = bpf_prog_run_xdp(xdp_prog, &xdp);
825 stats->xdp_packets++;
829 /* recalculate offset to account for any header
830 * adjustments. Note other cases do not build an
831 * skb and avoid using offset
834 page_address(xdp_page) - vi->hdr_len;
836 /* recalculate len if xdp.data or xdp.data_end were
839 len = xdp.data_end - xdp.data + vi->hdr_len;
840 /* We can only create skb based on xdp_page. */
841 if (unlikely(xdp_page != page)) {
844 head_skb = page_to_skb(vi, rq, xdp_page,
845 offset, len, PAGE_SIZE);
851 xdpf = convert_to_xdp_frame(&xdp);
854 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
855 if (unlikely(err < 0)) {
856 trace_xdp_exception(vi->dev, xdp_prog, act);
857 if (unlikely(xdp_page != page))
861 *xdp_xmit |= VIRTIO_XDP_TX;
862 if (unlikely(xdp_page != page))
867 stats->xdp_redirects++;
868 err = xdp_do_redirect(dev, &xdp, xdp_prog);
870 if (unlikely(xdp_page != page))
874 *xdp_xmit |= VIRTIO_XDP_REDIR;
875 if (unlikely(xdp_page != page))
880 bpf_warn_invalid_xdp_action(act);
883 trace_xdp_exception(vi->dev, xdp_prog, act);
886 if (unlikely(xdp_page != page))
887 __free_pages(xdp_page, 0);
893 truesize = mergeable_ctx_to_truesize(ctx);
894 if (unlikely(len > truesize)) {
895 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
896 dev->name, len, (unsigned long)ctx);
897 dev->stats.rx_length_errors++;
901 head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
904 if (unlikely(!curr_skb))
909 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
910 if (unlikely(!buf)) {
911 pr_debug("%s: rx error: %d buffers out of %d missing\n",
913 virtio16_to_cpu(vi->vdev,
915 dev->stats.rx_length_errors++;
920 page = virt_to_head_page(buf);
922 truesize = mergeable_ctx_to_truesize(ctx);
923 if (unlikely(len > truesize)) {
924 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
925 dev->name, len, (unsigned long)ctx);
926 dev->stats.rx_length_errors++;
930 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
931 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
932 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
936 if (curr_skb == head_skb)
937 skb_shinfo(curr_skb)->frag_list = nskb;
939 curr_skb->next = nskb;
941 head_skb->truesize += nskb->truesize;
944 if (curr_skb != head_skb) {
945 head_skb->data_len += len;
946 head_skb->len += len;
947 head_skb->truesize += truesize;
949 offset = buf - page_address(page);
950 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
952 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
955 skb_add_rx_frag(curr_skb, num_skb_frags, page,
956 offset, len, truesize);
960 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
968 while (num_buf-- > 1) {
969 buf = virtqueue_get_buf(rq->vq, &len);
970 if (unlikely(!buf)) {
971 pr_debug("%s: rx error: %d buffers missing\n",
973 dev->stats.rx_length_errors++;
977 page = virt_to_head_page(buf);
982 dev_kfree_skb(head_skb);
987 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
988 void *buf, unsigned int len, void **ctx,
989 unsigned int *xdp_xmit,
990 struct virtnet_rq_stats *stats)
992 struct net_device *dev = vi->dev;
994 struct virtio_net_hdr_mrg_rxbuf *hdr;
996 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
997 pr_debug("%s: short packet %i\n", dev->name, len);
998 dev->stats.rx_length_errors++;
999 if (vi->mergeable_rx_bufs) {
1000 put_page(virt_to_head_page(buf));
1001 } else if (vi->big_packets) {
1002 give_pages(rq, buf);
1004 put_page(virt_to_head_page(buf));
1009 if (vi->mergeable_rx_bufs)
1010 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1012 else if (vi->big_packets)
1013 skb = receive_big(dev, vi, rq, buf, len, stats);
1015 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1020 hdr = skb_vnet_hdr(skb);
1022 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
1023 skb->ip_summed = CHECKSUM_UNNECESSARY;
1025 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1026 virtio_is_little_endian(vi->vdev))) {
1027 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1028 dev->name, hdr->hdr.gso_type,
1033 skb->protocol = eth_type_trans(skb, dev);
1034 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1035 ntohs(skb->protocol), skb->len, skb->pkt_type);
1037 napi_gro_receive(&rq->napi, skb);
1041 dev->stats.rx_frame_errors++;
1045 /* Unlike mergeable buffers, all buffers are allocated to the
1046 * same size, except for the headroom. For this reason we do
1047 * not need to use mergeable_len_to_ctx here - it is enough
1048 * to store the headroom as the context ignoring the truesize.
1050 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1053 struct page_frag *alloc_frag = &rq->alloc_frag;
1055 unsigned int xdp_headroom = virtnet_get_headroom(vi);
1056 void *ctx = (void *)(unsigned long)xdp_headroom;
1057 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
1060 len = SKB_DATA_ALIGN(len) +
1061 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1062 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
1065 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1066 get_page(alloc_frag->page);
1067 alloc_frag->offset += len;
1068 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1069 vi->hdr_len + GOOD_PACKET_LEN);
1070 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1072 put_page(virt_to_head_page(buf));
1076 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1079 struct page *first, *list = NULL;
1083 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1085 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
1086 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
1087 first = get_a_page(rq, gfp);
1090 give_pages(rq, list);
1093 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
1095 /* chain new page in list head to match sg */
1096 first->private = (unsigned long)list;
1100 first = get_a_page(rq, gfp);
1102 give_pages(rq, list);
1105 p = page_address(first);
1107 /* rq->sg[0], rq->sg[1] share the same page */
1108 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1109 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
1111 /* rq->sg[1] for data packet, from offset */
1112 offset = sizeof(struct padded_vnet_hdr);
1113 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
1115 /* chain first in list head */
1116 first->private = (unsigned long)list;
1117 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1120 give_pages(rq, first);
1125 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
1126 struct ewma_pkt_len *avg_pkt_len,
1129 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1133 return PAGE_SIZE - room;
1135 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1136 rq->min_buf_len, PAGE_SIZE - hdr_len);
1138 return ALIGN(len, L1_CACHE_BYTES);
1141 static int add_recvbuf_mergeable(struct virtnet_info *vi,
1142 struct receive_queue *rq, gfp_t gfp)
1144 struct page_frag *alloc_frag = &rq->alloc_frag;
1145 unsigned int headroom = virtnet_get_headroom(vi);
1146 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1147 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1151 unsigned int len, hole;
1153 /* Extra tailroom is needed to satisfy XDP's assumption. This
1154 * means rx frags coalescing won't work, but consider we've
1155 * disabled GSO for XDP, it won't be a big issue.
1157 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1158 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
1161 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1162 buf += headroom; /* advance address leaving hole at front of pkt */
1163 get_page(alloc_frag->page);
1164 alloc_frag->offset += len + room;
1165 hole = alloc_frag->size - alloc_frag->offset;
1166 if (hole < len + room) {
1167 /* To avoid internal fragmentation, if there is very likely not
1168 * enough space for another buffer, add the remaining space to
1169 * the current buffer.
1172 alloc_frag->offset += hole;
1175 sg_init_one(rq->sg, buf, len);
1176 ctx = mergeable_len_to_ctx(len, headroom);
1177 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1179 put_page(virt_to_head_page(buf));
1185 * Returns false if we couldn't fill entirely (OOM).
1187 * Normally run in the receive path, but can also be run from ndo_open
1188 * before we're receiving packets, or from refill_work which is
1189 * careful to disable receiving (using napi_disable).
1191 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1198 if (vi->mergeable_rx_bufs)
1199 err = add_recvbuf_mergeable(vi, rq, gfp);
1200 else if (vi->big_packets)
1201 err = add_recvbuf_big(vi, rq, gfp);
1203 err = add_recvbuf_small(vi, rq, gfp);
1205 oom = err == -ENOMEM;
1208 } while (rq->vq->num_free);
1209 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1210 u64_stats_update_begin(&rq->stats.syncp);
1212 u64_stats_update_end(&rq->stats.syncp);
1218 static void skb_recv_done(struct virtqueue *rvq)
1220 struct virtnet_info *vi = rvq->vdev->priv;
1221 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1223 virtqueue_napi_schedule(&rq->napi, rvq);
1226 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
1230 /* If all buffers were filled by other side before we napi_enabled, we
1231 * won't get another interrupt, so process any outstanding packets now.
1232 * Call local_bh_enable after to trigger softIRQ processing.
1235 virtqueue_napi_schedule(napi, vq);
1239 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1240 struct virtqueue *vq,
1241 struct napi_struct *napi)
1246 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1247 * enable the feature if this is likely affine with the transmit path.
1249 if (!vi->affinity_hint_set) {
1254 return virtnet_napi_enable(vq, napi);
1257 static void virtnet_napi_tx_disable(struct napi_struct *napi)
1263 static void refill_work(struct work_struct *work)
1265 struct virtnet_info *vi =
1266 container_of(work, struct virtnet_info, refill.work);
1270 for (i = 0; i < vi->curr_queue_pairs; i++) {
1271 struct receive_queue *rq = &vi->rq[i];
1273 napi_disable(&rq->napi);
1274 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1275 virtnet_napi_enable(rq->vq, &rq->napi);
1277 /* In theory, this can happen: if we don't get any buffers in
1278 * we will *never* try to fill again.
1281 schedule_delayed_work(&vi->refill, HZ/2);
1285 static int virtnet_receive(struct receive_queue *rq, int budget,
1286 unsigned int *xdp_xmit)
1288 struct virtnet_info *vi = rq->vq->vdev->priv;
1289 struct virtnet_rq_stats stats = {};
1294 if (!vi->big_packets || vi->mergeable_rx_bufs) {
1297 while (stats.packets < budget &&
1298 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1299 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1303 while (stats.packets < budget &&
1304 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1305 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1310 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
1311 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
1312 schedule_delayed_work(&vi->refill, 0);
1315 u64_stats_update_begin(&rq->stats.syncp);
1316 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1317 size_t offset = virtnet_rq_stats_desc[i].offset;
1320 item = (u64 *)((u8 *)&rq->stats + offset);
1321 *item += *(u64 *)((u8 *)&stats + offset);
1323 u64_stats_update_end(&rq->stats.syncp);
1325 return stats.packets;
1328 static void free_old_xmit_skbs(struct send_queue *sq)
1330 struct sk_buff *skb;
1332 unsigned int packets = 0;
1333 unsigned int bytes = 0;
1335 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1336 pr_debug("Sent skb %p\n", skb);
1341 dev_consume_skb_any(skb);
1344 /* Avoid overhead when no packets have been processed
1345 * happens when called speculatively from start_xmit.
1350 u64_stats_update_begin(&sq->stats.syncp);
1351 sq->stats.bytes += bytes;
1352 sq->stats.packets += packets;
1353 u64_stats_update_end(&sq->stats.syncp);
1356 static void virtnet_poll_cleantx(struct receive_queue *rq)
1358 struct virtnet_info *vi = rq->vq->vdev->priv;
1359 unsigned int index = vq2rxq(rq->vq);
1360 struct send_queue *sq = &vi->sq[index];
1361 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1363 if (!sq->napi.weight)
1366 if (__netif_tx_trylock(txq)) {
1367 free_old_xmit_skbs(sq);
1368 __netif_tx_unlock(txq);
1371 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1372 netif_tx_wake_queue(txq);
1375 static int virtnet_poll(struct napi_struct *napi, int budget)
1377 struct receive_queue *rq =
1378 container_of(napi, struct receive_queue, napi);
1379 struct virtnet_info *vi = rq->vq->vdev->priv;
1380 struct send_queue *sq;
1381 unsigned int received;
1382 unsigned int xdp_xmit = 0;
1384 virtnet_poll_cleantx(rq);
1386 received = virtnet_receive(rq, budget, &xdp_xmit);
1388 /* Out of packets? */
1389 if (received < budget)
1390 virtqueue_napi_complete(napi, rq->vq, received);
1392 if (xdp_xmit & VIRTIO_XDP_REDIR)
1395 if (xdp_xmit & VIRTIO_XDP_TX) {
1396 sq = virtnet_xdp_sq(vi);
1397 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1398 u64_stats_update_begin(&sq->stats.syncp);
1400 u64_stats_update_end(&sq->stats.syncp);
1407 static int virtnet_open(struct net_device *dev)
1409 struct virtnet_info *vi = netdev_priv(dev);
1412 for (i = 0; i < vi->max_queue_pairs; i++) {
1413 if (i < vi->curr_queue_pairs)
1414 /* Make sure we have some buffers: if oom use wq. */
1415 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1416 schedule_delayed_work(&vi->refill, 0);
1418 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i);
1422 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1423 MEM_TYPE_PAGE_SHARED, NULL);
1425 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1429 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1430 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1436 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1438 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1439 struct virtnet_info *vi = sq->vq->vdev->priv;
1440 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq));
1442 __netif_tx_lock(txq, raw_smp_processor_id());
1443 free_old_xmit_skbs(sq);
1444 __netif_tx_unlock(txq);
1446 virtqueue_napi_complete(napi, sq->vq, 0);
1448 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1449 netif_tx_wake_queue(txq);
1454 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1456 struct virtio_net_hdr_mrg_rxbuf *hdr;
1457 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1458 struct virtnet_info *vi = sq->vq->vdev->priv;
1460 unsigned hdr_len = vi->hdr_len;
1463 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1465 can_push = vi->any_header_sg &&
1466 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1467 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1468 /* Even if we can, don't push here yet as this would skew
1469 * csum_start offset below. */
1471 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1473 hdr = skb_vnet_hdr(skb);
1475 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1476 virtio_is_little_endian(vi->vdev), false,
1480 if (vi->mergeable_rx_bufs)
1481 hdr->num_buffers = 0;
1483 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1485 __skb_push(skb, hdr_len);
1486 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1487 if (unlikely(num_sg < 0))
1489 /* Pull header back to avoid skew in tx bytes calculations. */
1490 __skb_pull(skb, hdr_len);
1492 sg_set_buf(sq->sg, hdr, hdr_len);
1493 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1494 if (unlikely(num_sg < 0))
1498 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
1501 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
1503 struct virtnet_info *vi = netdev_priv(dev);
1504 int qnum = skb_get_queue_mapping(skb);
1505 struct send_queue *sq = &vi->sq[qnum];
1507 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1508 bool kick = !skb->xmit_more;
1509 bool use_napi = sq->napi.weight;
1511 /* Free up any pending old buffers before queueing new ones. */
1512 free_old_xmit_skbs(sq);
1514 if (use_napi && kick)
1515 virtqueue_enable_cb_delayed(sq->vq);
1517 /* timestamp packet in software */
1518 skb_tx_timestamp(skb);
1520 /* Try to transmit */
1521 err = xmit_skb(sq, skb);
1523 /* This should not happen! */
1524 if (unlikely(err)) {
1525 dev->stats.tx_fifo_errors++;
1526 if (net_ratelimit())
1528 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
1529 dev->stats.tx_dropped++;
1530 dev_kfree_skb_any(skb);
1531 return NETDEV_TX_OK;
1534 /* Don't wait up for transmitted skbs to be freed. */
1540 /* If running out of space, stop queue to avoid getting packets that we
1541 * are then unable to transmit.
1542 * An alternative would be to force queuing layer to requeue the skb by
1543 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1544 * returned in a normal path of operation: it means that driver is not
1545 * maintaining the TX queue stop/start state properly, and causes
1546 * the stack to do a non-trivial amount of useless work.
1547 * Since most packets only take 1 or 2 ring slots, stopping the queue
1548 * early means 16 slots are typically wasted.
1550 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1551 netif_stop_subqueue(dev, qnum);
1553 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1554 /* More just got used, free them then recheck. */
1555 free_old_xmit_skbs(sq);
1556 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1557 netif_start_subqueue(dev, qnum);
1558 virtqueue_disable_cb(sq->vq);
1563 if (kick || netif_xmit_stopped(txq)) {
1564 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1565 u64_stats_update_begin(&sq->stats.syncp);
1567 u64_stats_update_end(&sq->stats.syncp);
1571 return NETDEV_TX_OK;
1575 * Send command via the control virtqueue and check status. Commands
1576 * supported by the hypervisor, as indicated by feature bits, should
1577 * never fail unless improperly formatted.
1579 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1580 struct scatterlist *out)
1582 struct scatterlist *sgs[4], hdr, stat;
1583 unsigned out_num = 0, tmp;
1585 /* Caller should know better */
1586 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
1588 vi->ctrl->status = ~0;
1589 vi->ctrl->hdr.class = class;
1590 vi->ctrl->hdr.cmd = cmd;
1592 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
1593 sgs[out_num++] = &hdr;
1596 sgs[out_num++] = out;
1598 /* Add return status. */
1599 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
1600 sgs[out_num] = &stat;
1602 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1603 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1605 if (unlikely(!virtqueue_kick(vi->cvq)))
1606 return vi->ctrl->status == VIRTIO_NET_OK;
1608 /* Spin for a response, the kick causes an ioport write, trapping
1609 * into the hypervisor, so the request should be handled immediately.
1611 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1612 !virtqueue_is_broken(vi->cvq))
1615 return vi->ctrl->status == VIRTIO_NET_OK;
1618 static int virtnet_set_mac_address(struct net_device *dev, void *p)
1620 struct virtnet_info *vi = netdev_priv(dev);
1621 struct virtio_device *vdev = vi->vdev;
1623 struct sockaddr *addr;
1624 struct scatterlist sg;
1626 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1629 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
1633 ret = eth_prepare_mac_addr_change(dev, addr);
1637 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1638 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1639 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1640 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
1641 dev_warn(&vdev->dev,
1642 "Failed to set mac address by vq command.\n");
1646 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1647 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1650 /* Naturally, this has an atomicity problem. */
1651 for (i = 0; i < dev->addr_len; i++)
1652 virtio_cwrite8(vdev,
1653 offsetof(struct virtio_net_config, mac) +
1654 i, addr->sa_data[i]);
1657 eth_commit_mac_addr_change(dev, p);
1665 static void virtnet_stats(struct net_device *dev,
1666 struct rtnl_link_stats64 *tot)
1668 struct virtnet_info *vi = netdev_priv(dev);
1672 for (i = 0; i < vi->max_queue_pairs; i++) {
1673 u64 tpackets, tbytes, rpackets, rbytes, rdrops;
1674 struct receive_queue *rq = &vi->rq[i];
1675 struct send_queue *sq = &vi->sq[i];
1678 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1679 tpackets = sq->stats.packets;
1680 tbytes = sq->stats.bytes;
1681 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
1684 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
1685 rpackets = rq->stats.packets;
1686 rbytes = rq->stats.bytes;
1687 rdrops = rq->stats.drops;
1688 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
1690 tot->rx_packets += rpackets;
1691 tot->tx_packets += tpackets;
1692 tot->rx_bytes += rbytes;
1693 tot->tx_bytes += tbytes;
1694 tot->rx_dropped += rdrops;
1697 tot->tx_dropped = dev->stats.tx_dropped;
1698 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
1699 tot->rx_length_errors = dev->stats.rx_length_errors;
1700 tot->rx_frame_errors = dev->stats.rx_frame_errors;
1703 static void virtnet_ack_link_announce(struct virtnet_info *vi)
1706 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1707 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1708 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1712 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1714 struct scatterlist sg;
1715 struct net_device *dev = vi->dev;
1717 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1720 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1721 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
1723 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1724 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1725 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1729 vi->curr_queue_pairs = queue_pairs;
1730 /* virtnet_open() will refill when device is going to up. */
1731 if (dev->flags & IFF_UP)
1732 schedule_delayed_work(&vi->refill, 0);
1738 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1743 err = _virtnet_set_queues(vi, queue_pairs);
1748 static int virtnet_close(struct net_device *dev)
1750 struct virtnet_info *vi = netdev_priv(dev);
1753 /* Make sure refill_work doesn't re-enable napi! */
1754 cancel_delayed_work_sync(&vi->refill);
1756 for (i = 0; i < vi->max_queue_pairs; i++) {
1757 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1758 napi_disable(&vi->rq[i].napi);
1759 virtnet_napi_tx_disable(&vi->sq[i].napi);
1765 static void virtnet_set_rx_mode(struct net_device *dev)
1767 struct virtnet_info *vi = netdev_priv(dev);
1768 struct scatterlist sg[2];
1769 struct virtio_net_ctrl_mac *mac_data;
1770 struct netdev_hw_addr *ha;
1776 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
1777 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1780 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
1781 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
1783 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
1785 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1786 VIRTIO_NET_CTRL_RX_PROMISC, sg))
1787 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1788 vi->ctrl->promisc ? "en" : "dis");
1790 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
1792 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1793 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
1794 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1795 vi->ctrl->allmulti ? "en" : "dis");
1797 uc_count = netdev_uc_count(dev);
1798 mc_count = netdev_mc_count(dev);
1799 /* MAC filter - use one buffer for both lists */
1800 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1801 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1806 sg_init_table(sg, 2);
1808 /* Store the unicast list and count in the front of the buffer */
1809 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
1811 netdev_for_each_uc_addr(ha, dev)
1812 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1814 sg_set_buf(&sg[0], mac_data,
1815 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1817 /* multicast list and count fill the end */
1818 mac_data = (void *)&mac_data->macs[uc_count][0];
1820 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
1822 netdev_for_each_mc_addr(ha, dev)
1823 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1825 sg_set_buf(&sg[1], mac_data,
1826 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1828 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1829 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
1830 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1835 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1836 __be16 proto, u16 vid)
1838 struct virtnet_info *vi = netdev_priv(dev);
1839 struct scatterlist sg;
1841 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
1842 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
1844 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1845 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
1846 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
1850 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1851 __be16 proto, u16 vid)
1853 struct virtnet_info *vi = netdev_priv(dev);
1854 struct scatterlist sg;
1856 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
1857 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
1859 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1860 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
1861 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
1865 static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1869 if (vi->affinity_hint_set) {
1870 for (i = 0; i < vi->max_queue_pairs; i++) {
1871 virtqueue_set_affinity(vi->rq[i].vq, NULL);
1872 virtqueue_set_affinity(vi->sq[i].vq, NULL);
1875 vi->affinity_hint_set = false;
1879 static void virtnet_set_affinity(struct virtnet_info *vi)
1888 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
1889 virtnet_clean_affinity(vi, -1);
1893 num_cpu = num_online_cpus();
1894 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
1895 stragglers = num_cpu >= vi->curr_queue_pairs ?
1896 num_cpu % vi->curr_queue_pairs :
1898 cpu = cpumask_next(-1, cpu_online_mask);
1900 for (i = 0; i < vi->curr_queue_pairs; i++) {
1901 group_size = stride + (i < stragglers ? 1 : 0);
1903 for (j = 0; j < group_size; j++) {
1904 cpumask_set_cpu(cpu, mask);
1905 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
1908 virtqueue_set_affinity(vi->rq[i].vq, mask);
1909 virtqueue_set_affinity(vi->sq[i].vq, mask);
1910 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, false);
1911 cpumask_clear(mask);
1914 vi->affinity_hint_set = true;
1915 free_cpumask_var(mask);
1918 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
1920 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1922 virtnet_set_affinity(vi);
1926 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1928 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1930 virtnet_set_affinity(vi);
1934 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1936 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1939 virtnet_clean_affinity(vi, cpu);
1943 static enum cpuhp_state virtionet_online;
1945 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1949 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1952 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1956 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1960 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1962 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1963 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1967 static void virtnet_get_ringparam(struct net_device *dev,
1968 struct ethtool_ringparam *ring)
1970 struct virtnet_info *vi = netdev_priv(dev);
1972 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1973 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
1974 ring->rx_pending = ring->rx_max_pending;
1975 ring->tx_pending = ring->tx_max_pending;
1979 static void virtnet_get_drvinfo(struct net_device *dev,
1980 struct ethtool_drvinfo *info)
1982 struct virtnet_info *vi = netdev_priv(dev);
1983 struct virtio_device *vdev = vi->vdev;
1985 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1986 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1987 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1991 /* TODO: Eliminate OOO packets during switching */
1992 static int virtnet_set_channels(struct net_device *dev,
1993 struct ethtool_channels *channels)
1995 struct virtnet_info *vi = netdev_priv(dev);
1996 u16 queue_pairs = channels->combined_count;
1999 /* We don't support separate rx/tx channels.
2000 * We don't allow setting 'other' channels.
2002 if (channels->rx_count || channels->tx_count || channels->other_count)
2005 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
2008 /* For now we don't support modifying channels while XDP is loaded
2009 * also when XDP is loaded all RX queues have XDP programs so we only
2010 * need to check a single RX queue.
2012 if (vi->rq[0].xdp_prog)
2016 err = _virtnet_set_queues(vi, queue_pairs);
2018 netif_set_real_num_tx_queues(dev, queue_pairs);
2019 netif_set_real_num_rx_queues(dev, queue_pairs);
2021 virtnet_set_affinity(vi);
2028 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2030 struct virtnet_info *vi = netdev_priv(dev);
2031 char *p = (char *)data;
2034 switch (stringset) {
2036 for (i = 0; i < vi->curr_queue_pairs; i++) {
2037 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2038 snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_%s",
2039 i, virtnet_rq_stats_desc[j].desc);
2040 p += ETH_GSTRING_LEN;
2044 for (i = 0; i < vi->curr_queue_pairs; i++) {
2045 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2046 snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_%s",
2047 i, virtnet_sq_stats_desc[j].desc);
2048 p += ETH_GSTRING_LEN;
2055 static int virtnet_get_sset_count(struct net_device *dev, int sset)
2057 struct virtnet_info *vi = netdev_priv(dev);
2061 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2062 VIRTNET_SQ_STATS_LEN);
2068 static void virtnet_get_ethtool_stats(struct net_device *dev,
2069 struct ethtool_stats *stats, u64 *data)
2071 struct virtnet_info *vi = netdev_priv(dev);
2072 unsigned int idx = 0, start, i, j;
2073 const u8 *stats_base;
2076 for (i = 0; i < vi->curr_queue_pairs; i++) {
2077 struct receive_queue *rq = &vi->rq[i];
2079 stats_base = (u8 *)&rq->stats;
2081 start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2082 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2083 offset = virtnet_rq_stats_desc[j].offset;
2084 data[idx + j] = *(u64 *)(stats_base + offset);
2086 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2087 idx += VIRTNET_RQ_STATS_LEN;
2090 for (i = 0; i < vi->curr_queue_pairs; i++) {
2091 struct send_queue *sq = &vi->sq[i];
2093 stats_base = (u8 *)&sq->stats;
2095 start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2096 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2097 offset = virtnet_sq_stats_desc[j].offset;
2098 data[idx + j] = *(u64 *)(stats_base + offset);
2100 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2101 idx += VIRTNET_SQ_STATS_LEN;
2105 static void virtnet_get_channels(struct net_device *dev,
2106 struct ethtool_channels *channels)
2108 struct virtnet_info *vi = netdev_priv(dev);
2110 channels->combined_count = vi->curr_queue_pairs;
2111 channels->max_combined = vi->max_queue_pairs;
2112 channels->max_other = 0;
2113 channels->rx_count = 0;
2114 channels->tx_count = 0;
2115 channels->other_count = 0;
2118 /* Check if the user is trying to change anything besides speed/duplex */
2120 virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
2122 struct ethtool_link_ksettings diff1 = *cmd;
2123 struct ethtool_link_ksettings diff2 = {};
2125 /* cmd is always set so we need to clear it, validate the port type
2126 * and also without autonegotiation we can ignore advertising
2128 diff1.base.speed = 0;
2129 diff2.base.port = PORT_OTHER;
2130 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
2131 diff1.base.duplex = 0;
2133 diff1.base.link_mode_masks_nwords = 0;
2135 return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
2136 bitmap_empty(diff1.link_modes.supported,
2137 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
2138 bitmap_empty(diff1.link_modes.advertising,
2139 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
2140 bitmap_empty(diff1.link_modes.lp_advertising,
2141 __ETHTOOL_LINK_MODE_MASK_NBITS);
2144 static int virtnet_set_link_ksettings(struct net_device *dev,
2145 const struct ethtool_link_ksettings *cmd)
2147 struct virtnet_info *vi = netdev_priv(dev);
2150 speed = cmd->base.speed;
2151 /* don't allow custom speed and duplex */
2152 if (!ethtool_validate_speed(speed) ||
2153 !ethtool_validate_duplex(cmd->base.duplex) ||
2154 !virtnet_validate_ethtool_cmd(cmd))
2157 vi->duplex = cmd->base.duplex;
2162 static int virtnet_get_link_ksettings(struct net_device *dev,
2163 struct ethtool_link_ksettings *cmd)
2165 struct virtnet_info *vi = netdev_priv(dev);
2167 cmd->base.speed = vi->speed;
2168 cmd->base.duplex = vi->duplex;
2169 cmd->base.port = PORT_OTHER;
2174 static int virtnet_set_coalesce(struct net_device *dev,
2175 struct ethtool_coalesce *ec)
2177 struct ethtool_coalesce ec_default = {
2178 .cmd = ETHTOOL_SCOALESCE,
2179 .rx_max_coalesced_frames = 1,
2181 struct virtnet_info *vi = netdev_priv(dev);
2184 if (ec->tx_max_coalesced_frames > 1)
2187 ec_default.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
2188 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
2190 /* disallow changes to fields not explicitly tested above */
2191 if (memcmp(ec, &ec_default, sizeof(ec_default)))
2194 if (napi_weight ^ vi->sq[0].napi.weight) {
2195 if (dev->flags & IFF_UP)
2197 for (i = 0; i < vi->max_queue_pairs; i++)
2198 vi->sq[i].napi.weight = napi_weight;
2204 static int virtnet_get_coalesce(struct net_device *dev,
2205 struct ethtool_coalesce *ec)
2207 struct ethtool_coalesce ec_default = {
2208 .cmd = ETHTOOL_GCOALESCE,
2209 .rx_max_coalesced_frames = 1,
2211 struct virtnet_info *vi = netdev_priv(dev);
2213 memcpy(ec, &ec_default, sizeof(ec_default));
2215 if (vi->sq[0].napi.weight)
2216 ec->tx_max_coalesced_frames = 1;
2221 static void virtnet_init_settings(struct net_device *dev)
2223 struct virtnet_info *vi = netdev_priv(dev);
2225 vi->speed = SPEED_UNKNOWN;
2226 vi->duplex = DUPLEX_UNKNOWN;
2229 static void virtnet_update_settings(struct virtnet_info *vi)
2234 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2237 speed = virtio_cread32(vi->vdev, offsetof(struct virtio_net_config,
2239 if (ethtool_validate_speed(speed))
2241 duplex = virtio_cread8(vi->vdev, offsetof(struct virtio_net_config,
2243 if (ethtool_validate_duplex(duplex))
2244 vi->duplex = duplex;
2247 static const struct ethtool_ops virtnet_ethtool_ops = {
2248 .get_drvinfo = virtnet_get_drvinfo,
2249 .get_link = ethtool_op_get_link,
2250 .get_ringparam = virtnet_get_ringparam,
2251 .get_strings = virtnet_get_strings,
2252 .get_sset_count = virtnet_get_sset_count,
2253 .get_ethtool_stats = virtnet_get_ethtool_stats,
2254 .set_channels = virtnet_set_channels,
2255 .get_channels = virtnet_get_channels,
2256 .get_ts_info = ethtool_op_get_ts_info,
2257 .get_link_ksettings = virtnet_get_link_ksettings,
2258 .set_link_ksettings = virtnet_set_link_ksettings,
2259 .set_coalesce = virtnet_set_coalesce,
2260 .get_coalesce = virtnet_get_coalesce,
2263 static void virtnet_freeze_down(struct virtio_device *vdev)
2265 struct virtnet_info *vi = vdev->priv;
2268 /* Make sure no work handler is accessing the device */
2269 flush_work(&vi->config_work);
2271 netif_tx_lock_bh(vi->dev);
2272 netif_device_detach(vi->dev);
2273 netif_tx_unlock_bh(vi->dev);
2274 cancel_delayed_work_sync(&vi->refill);
2276 if (netif_running(vi->dev)) {
2277 for (i = 0; i < vi->max_queue_pairs; i++) {
2278 napi_disable(&vi->rq[i].napi);
2279 virtnet_napi_tx_disable(&vi->sq[i].napi);
2284 static int init_vqs(struct virtnet_info *vi);
2286 static int virtnet_restore_up(struct virtio_device *vdev)
2288 struct virtnet_info *vi = vdev->priv;
2295 virtio_device_ready(vdev);
2297 if (netif_running(vi->dev)) {
2298 for (i = 0; i < vi->curr_queue_pairs; i++)
2299 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2300 schedule_delayed_work(&vi->refill, 0);
2302 for (i = 0; i < vi->max_queue_pairs; i++) {
2303 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2304 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2309 netif_tx_lock_bh(vi->dev);
2310 netif_device_attach(vi->dev);
2311 netif_tx_unlock_bh(vi->dev);
2315 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2317 struct scatterlist sg;
2318 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
2320 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
2322 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2323 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
2324 dev_warn(&vi->dev->dev, "Fail to set guest offload. \n");
2331 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2335 if (!vi->guest_offloads)
2338 return virtnet_set_guest_offloads(vi, offloads);
2341 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2343 u64 offloads = vi->guest_offloads;
2345 if (!vi->guest_offloads)
2348 return virtnet_set_guest_offloads(vi, offloads);
2351 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2352 struct netlink_ext_ack *extack)
2354 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2355 struct virtnet_info *vi = netdev_priv(dev);
2356 struct bpf_prog *old_prog;
2357 u16 xdp_qp = 0, curr_qp;
2360 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2361 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2362 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2363 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
2364 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
2365 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
2366 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO/CSUM, disable LRO/CSUM first");
2370 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
2371 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
2375 if (dev->mtu > max_sz) {
2376 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
2377 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2381 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2383 xdp_qp = nr_cpu_ids;
2385 /* XDP requires extra queues for XDP_TX */
2386 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
2387 NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available");
2388 netdev_warn(dev, "request %i queues but max is %i\n",
2389 curr_qp + xdp_qp, vi->max_queue_pairs);
2394 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
2396 return PTR_ERR(prog);
2399 /* Make sure NAPI is not using any XDP TX queues for RX. */
2400 if (netif_running(dev))
2401 for (i = 0; i < vi->max_queue_pairs; i++)
2402 napi_disable(&vi->rq[i].napi);
2404 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
2405 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2408 vi->xdp_queue_pairs = xdp_qp;
2410 for (i = 0; i < vi->max_queue_pairs; i++) {
2411 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2412 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2415 virtnet_clear_guest_offloads(vi);
2417 virtnet_restore_guest_offloads(vi);
2420 bpf_prog_put(old_prog);
2421 if (netif_running(dev))
2422 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2428 for (i = 0; i < vi->max_queue_pairs; i++)
2429 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2431 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2435 static u32 virtnet_xdp_query(struct net_device *dev)
2437 struct virtnet_info *vi = netdev_priv(dev);
2438 const struct bpf_prog *xdp_prog;
2441 for (i = 0; i < vi->max_queue_pairs; i++) {
2442 xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2444 return xdp_prog->aux->id;
2449 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2451 switch (xdp->command) {
2452 case XDP_SETUP_PROG:
2453 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
2454 case XDP_QUERY_PROG:
2455 xdp->prog_id = virtnet_xdp_query(dev);
2462 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2465 struct virtnet_info *vi = netdev_priv(dev);
2468 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2471 ret = snprintf(buf, len, "sby");
2478 static const struct net_device_ops virtnet_netdev = {
2479 .ndo_open = virtnet_open,
2480 .ndo_stop = virtnet_close,
2481 .ndo_start_xmit = start_xmit,
2482 .ndo_validate_addr = eth_validate_addr,
2483 .ndo_set_mac_address = virtnet_set_mac_address,
2484 .ndo_set_rx_mode = virtnet_set_rx_mode,
2485 .ndo_get_stats64 = virtnet_stats,
2486 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
2487 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
2488 .ndo_bpf = virtnet_xdp,
2489 .ndo_xdp_xmit = virtnet_xdp_xmit,
2490 .ndo_features_check = passthru_features_check,
2491 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
2494 static void virtnet_config_changed_work(struct work_struct *work)
2496 struct virtnet_info *vi =
2497 container_of(work, struct virtnet_info, config_work);
2500 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2501 struct virtio_net_config, status, &v) < 0)
2504 if (v & VIRTIO_NET_S_ANNOUNCE) {
2505 netdev_notify_peers(vi->dev);
2506 virtnet_ack_link_announce(vi);
2509 /* Ignore unknown (future) status bits */
2510 v &= VIRTIO_NET_S_LINK_UP;
2512 if (vi->status == v)
2517 if (vi->status & VIRTIO_NET_S_LINK_UP) {
2518 virtnet_update_settings(vi);
2519 netif_carrier_on(vi->dev);
2520 netif_tx_wake_all_queues(vi->dev);
2522 netif_carrier_off(vi->dev);
2523 netif_tx_stop_all_queues(vi->dev);
2527 static void virtnet_config_changed(struct virtio_device *vdev)
2529 struct virtnet_info *vi = vdev->priv;
2531 schedule_work(&vi->config_work);
2534 static void virtnet_free_queues(struct virtnet_info *vi)
2538 for (i = 0; i < vi->max_queue_pairs; i++) {
2539 napi_hash_del(&vi->rq[i].napi);
2540 netif_napi_del(&vi->rq[i].napi);
2541 netif_napi_del(&vi->sq[i].napi);
2544 /* We called napi_hash_del() before netif_napi_del(),
2545 * we need to respect an RCU grace period before freeing vi->rq
2554 static void _free_receive_bufs(struct virtnet_info *vi)
2556 struct bpf_prog *old_prog;
2559 for (i = 0; i < vi->max_queue_pairs; i++) {
2560 while (vi->rq[i].pages)
2561 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
2563 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2564 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2566 bpf_prog_put(old_prog);
2570 static void free_receive_bufs(struct virtnet_info *vi)
2573 _free_receive_bufs(vi);
2577 static void free_receive_page_frags(struct virtnet_info *vi)
2580 for (i = 0; i < vi->max_queue_pairs; i++)
2581 if (vi->rq[i].alloc_frag.page)
2582 put_page(vi->rq[i].alloc_frag.page);
2585 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
2587 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
2589 else if (q < vi->curr_queue_pairs)
2595 static void free_unused_bufs(struct virtnet_info *vi)
2600 for (i = 0; i < vi->max_queue_pairs; i++) {
2601 struct virtqueue *vq = vi->sq[i].vq;
2602 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2603 if (!is_xdp_raw_buffer_queue(vi, i))
2606 put_page(virt_to_head_page(buf));
2610 for (i = 0; i < vi->max_queue_pairs; i++) {
2611 struct virtqueue *vq = vi->rq[i].vq;
2613 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2614 if (vi->mergeable_rx_bufs) {
2615 put_page(virt_to_head_page(buf));
2616 } else if (vi->big_packets) {
2617 give_pages(&vi->rq[i], buf);
2619 put_page(virt_to_head_page(buf));
2625 static void virtnet_del_vqs(struct virtnet_info *vi)
2627 struct virtio_device *vdev = vi->vdev;
2629 virtnet_clean_affinity(vi, -1);
2631 vdev->config->del_vqs(vdev);
2633 virtnet_free_queues(vi);
2636 /* How large should a single buffer be so a queue full of these can fit at
2637 * least one full packet?
2638 * Logic below assumes the mergeable buffer header is used.
2640 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2642 const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2643 unsigned int rq_size = virtqueue_get_vring_size(vq);
2644 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2645 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2646 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2648 return max(max(min_buf_len, hdr_len) - hdr_len,
2649 (unsigned int)GOOD_PACKET_LEN);
2652 static int virtnet_find_vqs(struct virtnet_info *vi)
2654 vq_callback_t **callbacks;
2655 struct virtqueue **vqs;
2661 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2662 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2663 * possible control vq.
2665 total_vqs = vi->max_queue_pairs * 2 +
2666 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2668 /* Allocate space for find_vqs parameters */
2669 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
2672 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
2675 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
2678 if (!vi->big_packets || vi->mergeable_rx_bufs) {
2679 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
2686 /* Parameters for control virtqueue, if any */
2688 callbacks[total_vqs - 1] = NULL;
2689 names[total_vqs - 1] = "control";
2692 /* Allocate/initialize parameters for send/receive virtqueues */
2693 for (i = 0; i < vi->max_queue_pairs; i++) {
2694 callbacks[rxq2vq(i)] = skb_recv_done;
2695 callbacks[txq2vq(i)] = skb_xmit_done;
2696 sprintf(vi->rq[i].name, "input.%d", i);
2697 sprintf(vi->sq[i].name, "output.%d", i);
2698 names[rxq2vq(i)] = vi->rq[i].name;
2699 names[txq2vq(i)] = vi->sq[i].name;
2701 ctx[rxq2vq(i)] = true;
2704 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
2710 vi->cvq = vqs[total_vqs - 1];
2711 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
2712 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2715 for (i = 0; i < vi->max_queue_pairs; i++) {
2716 vi->rq[i].vq = vqs[rxq2vq(i)];
2717 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
2718 vi->sq[i].vq = vqs[txq2vq(i)];
2721 /* run here: ret == 0. */
2736 static int virtnet_alloc_queues(struct virtnet_info *vi)
2740 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
2743 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
2746 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
2750 INIT_DELAYED_WORK(&vi->refill, refill_work);
2751 for (i = 0; i < vi->max_queue_pairs; i++) {
2752 vi->rq[i].pages = NULL;
2753 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2755 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2756 napi_tx ? napi_weight : 0);
2758 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
2759 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
2760 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2762 u64_stats_init(&vi->rq[i].stats.syncp);
2763 u64_stats_init(&vi->sq[i].stats.syncp);
2776 static int init_vqs(struct virtnet_info *vi)
2780 /* Allocate send & receive queues */
2781 ret = virtnet_alloc_queues(vi);
2785 ret = virtnet_find_vqs(vi);
2790 virtnet_set_affinity(vi);
2796 virtnet_free_queues(vi);
2802 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2805 struct virtnet_info *vi = netdev_priv(queue->dev);
2806 unsigned int queue_index = get_netdev_rx_queue_index(queue);
2807 unsigned int headroom = virtnet_get_headroom(vi);
2808 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
2809 struct ewma_pkt_len *avg;
2811 BUG_ON(queue_index >= vi->max_queue_pairs);
2812 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2813 return sprintf(buf, "%u\n",
2814 get_mergeable_buf_len(&vi->rq[queue_index], avg,
2815 SKB_DATA_ALIGN(headroom + tailroom)));
2818 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2819 __ATTR_RO(mergeable_rx_buffer_size);
2821 static struct attribute *virtio_net_mrg_rx_attrs[] = {
2822 &mergeable_rx_buffer_size_attribute.attr,
2826 static const struct attribute_group virtio_net_mrg_rx_group = {
2827 .name = "virtio_net",
2828 .attrs = virtio_net_mrg_rx_attrs
2832 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2834 const char *fname, const char *dname)
2836 if (!virtio_has_feature(vdev, fbit))
2839 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2845 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2846 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2848 static bool virtnet_validate_features(struct virtio_device *vdev)
2850 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2851 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2852 "VIRTIO_NET_F_CTRL_VQ") ||
2853 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2854 "VIRTIO_NET_F_CTRL_VQ") ||
2855 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2856 "VIRTIO_NET_F_CTRL_VQ") ||
2857 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2858 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2859 "VIRTIO_NET_F_CTRL_VQ"))) {
2866 #define MIN_MTU ETH_MIN_MTU
2867 #define MAX_MTU ETH_MAX_MTU
2869 static int virtnet_validate(struct virtio_device *vdev)
2871 if (!vdev->config->get) {
2872 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2877 if (!virtnet_validate_features(vdev))
2880 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2881 int mtu = virtio_cread16(vdev,
2882 offsetof(struct virtio_net_config,
2885 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2891 static int virtnet_probe(struct virtio_device *vdev)
2893 int i, err = -ENOMEM;
2894 struct net_device *dev;
2895 struct virtnet_info *vi;
2896 u16 max_queue_pairs;
2899 /* Find if host supports multiqueue virtio_net device */
2900 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2901 struct virtio_net_config,
2902 max_virtqueue_pairs, &max_queue_pairs);
2904 /* We need at least 2 queue's */
2905 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2906 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2907 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2908 max_queue_pairs = 1;
2910 /* Allocate ourselves a network device with room for our info */
2911 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
2915 /* Set up network device as normal. */
2916 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2917 dev->netdev_ops = &virtnet_netdev;
2918 dev->features = NETIF_F_HIGHDMA;
2920 dev->ethtool_ops = &virtnet_ethtool_ops;
2921 SET_NETDEV_DEV(dev, &vdev->dev);
2923 /* Do we support "hardware" checksums? */
2924 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
2925 /* This opens up the world of extra features. */
2926 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
2928 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
2930 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
2931 dev->hw_features |= NETIF_F_TSO
2932 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2934 /* Individual feature bits: what can host handle? */
2935 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2936 dev->hw_features |= NETIF_F_TSO;
2937 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2938 dev->hw_features |= NETIF_F_TSO6;
2939 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2940 dev->hw_features |= NETIF_F_TSO_ECN;
2942 dev->features |= NETIF_F_GSO_ROBUST;
2945 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
2946 /* (!csum && gso) case will be fixed by register_netdev() */
2948 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2949 dev->features |= NETIF_F_RXCSUM;
2951 dev->vlan_features = dev->features;
2953 /* MTU range: 68 - 65535 */
2954 dev->min_mtu = MIN_MTU;
2955 dev->max_mtu = MAX_MTU;
2957 /* Configuration may specify what MAC to use. Otherwise random. */
2958 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2959 virtio_cread_bytes(vdev,
2960 offsetof(struct virtio_net_config, mac),
2961 dev->dev_addr, dev->addr_len);
2963 eth_hw_addr_random(dev);
2965 /* Set up our device-specific information */
2966 vi = netdev_priv(dev);
2971 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
2973 /* If we can receive ANY GSO packets, we must allocate large ones. */
2974 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2975 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2976 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2977 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
2978 vi->big_packets = true;
2980 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2981 vi->mergeable_rx_bufs = true;
2983 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2984 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
2985 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2987 vi->hdr_len = sizeof(struct virtio_net_hdr);
2989 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2990 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
2991 vi->any_header_sg = true;
2993 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2996 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2997 mtu = virtio_cread16(vdev,
2998 offsetof(struct virtio_net_config,
3000 if (mtu < dev->min_mtu) {
3001 /* Should never trigger: MTU was previously validated
3002 * in virtnet_validate.
3004 dev_err(&vdev->dev, "device MTU appears to have changed "
3005 "it is now %d < %d", mtu, dev->min_mtu);
3012 /* TODO: size buffers correctly in this case. */
3013 if (dev->mtu > ETH_DATA_LEN)
3014 vi->big_packets = true;
3017 if (vi->any_header_sg)
3018 dev->needed_headroom = vi->hdr_len;
3020 /* Enable multiqueue by default */
3021 if (num_online_cpus() >= max_queue_pairs)
3022 vi->curr_queue_pairs = max_queue_pairs;
3024 vi->curr_queue_pairs = num_online_cpus();
3025 vi->max_queue_pairs = max_queue_pairs;
3027 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
3033 if (vi->mergeable_rx_bufs)
3034 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3036 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
3037 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
3039 virtnet_init_settings(dev);
3041 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3042 vi->failover = net_failover_create(vi->dev);
3043 if (IS_ERR(vi->failover)) {
3044 err = PTR_ERR(vi->failover);
3049 err = register_netdev(dev);
3051 pr_debug("virtio_net: registering device failed\n");
3055 virtio_device_ready(vdev);
3057 err = virtnet_cpu_notif_add(vi);
3059 pr_debug("virtio_net: registering cpu notifier failed\n");
3060 goto free_unregister_netdev;
3063 virtnet_set_queues(vi, vi->curr_queue_pairs);
3065 /* Assume link up if device can't report link status,
3066 otherwise get link status from config. */
3067 netif_carrier_off(dev);
3068 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
3069 schedule_work(&vi->config_work);
3071 vi->status = VIRTIO_NET_S_LINK_UP;
3072 virtnet_update_settings(vi);
3073 netif_carrier_on(dev);
3076 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3077 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3078 set_bit(guest_offloads[i], &vi->guest_offloads);
3080 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3081 dev->name, max_queue_pairs);
3085 free_unregister_netdev:
3086 vi->vdev->config->reset(vdev);
3088 unregister_netdev(dev);
3090 net_failover_destroy(vi->failover);
3092 cancel_delayed_work_sync(&vi->refill);
3093 free_receive_page_frags(vi);
3094 virtnet_del_vqs(vi);
3100 static void remove_vq_common(struct virtnet_info *vi)
3102 vi->vdev->config->reset(vi->vdev);
3104 /* Free unused buffers in both send and recv, if any. */
3105 free_unused_bufs(vi);
3107 free_receive_bufs(vi);
3109 free_receive_page_frags(vi);
3111 virtnet_del_vqs(vi);
3114 static void virtnet_remove(struct virtio_device *vdev)
3116 struct virtnet_info *vi = vdev->priv;
3118 virtnet_cpu_notif_remove(vi);
3120 /* Make sure no work handler is accessing the device. */
3121 flush_work(&vi->config_work);
3123 unregister_netdev(vi->dev);
3125 net_failover_destroy(vi->failover);
3127 remove_vq_common(vi);
3129 free_netdev(vi->dev);
3132 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
3134 struct virtnet_info *vi = vdev->priv;
3136 virtnet_cpu_notif_remove(vi);
3137 virtnet_freeze_down(vdev);
3138 remove_vq_common(vi);
3143 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
3145 struct virtnet_info *vi = vdev->priv;
3148 err = virtnet_restore_up(vdev);
3151 virtnet_set_queues(vi, vi->curr_queue_pairs);
3153 err = virtnet_cpu_notif_add(vi);
3160 static struct virtio_device_id id_table[] = {
3161 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3165 #define VIRTNET_FEATURES \
3166 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3168 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3169 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3170 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3171 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3172 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3173 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3174 VIRTIO_NET_F_CTRL_MAC_ADDR, \
3175 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
3176 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
3178 static unsigned int features[] = {
3182 static unsigned int features_legacy[] = {
3185 VIRTIO_F_ANY_LAYOUT,
3188 static struct virtio_driver virtio_net_driver = {
3189 .feature_table = features,
3190 .feature_table_size = ARRAY_SIZE(features),
3191 .feature_table_legacy = features_legacy,
3192 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
3193 .driver.name = KBUILD_MODNAME,
3194 .driver.owner = THIS_MODULE,
3195 .id_table = id_table,
3196 .validate = virtnet_validate,
3197 .probe = virtnet_probe,
3198 .remove = virtnet_remove,
3199 .config_changed = virtnet_config_changed,
3200 #ifdef CONFIG_PM_SLEEP
3201 .freeze = virtnet_freeze,
3202 .restore = virtnet_restore,
3206 static __init int virtio_net_driver_init(void)
3210 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
3212 virtnet_cpu_down_prep);
3215 virtionet_online = ret;
3216 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
3217 NULL, virtnet_cpu_dead);
3221 ret = register_virtio_driver(&virtio_net_driver);
3226 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3228 cpuhp_remove_multi_state(virtionet_online);
3232 module_init(virtio_net_driver_init);
3234 static __exit void virtio_net_driver_exit(void)
3236 unregister_virtio_driver(&virtio_net_driver);
3237 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3238 cpuhp_remove_multi_state(virtionet_online);
3240 module_exit(virtio_net_driver_exit);
3242 MODULE_DEVICE_TABLE(virtio, id_table);
3243 MODULE_DESCRIPTION("Virtio network driver");
3244 MODULE_LICENSE("GPL");