xref: /openbmc/linux/drivers/net/virtio_net.c (revision 680557cf79f82623e2c4fd42733077d60a843513)
148925e37SRusty Russell /* A network driver using virtio.
2296f96fcSRusty Russell  *
3296f96fcSRusty Russell  * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4296f96fcSRusty Russell  *
5296f96fcSRusty Russell  * This program is free software; you can redistribute it and/or modify
6296f96fcSRusty Russell  * it under the terms of the GNU General Public License as published by
7296f96fcSRusty Russell  * the Free Software Foundation; either version 2 of the License, or
8296f96fcSRusty Russell  * (at your option) any later version.
9296f96fcSRusty Russell  *
10296f96fcSRusty Russell  * This program is distributed in the hope that it will be useful,
11296f96fcSRusty Russell  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12296f96fcSRusty Russell  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13296f96fcSRusty Russell  * GNU General Public License for more details.
14296f96fcSRusty Russell  *
15296f96fcSRusty Russell  * You should have received a copy of the GNU General Public License
16adf8d3ffSJeff Kirsher  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17296f96fcSRusty Russell  */
18296f96fcSRusty Russell //#define DEBUG
19296f96fcSRusty Russell #include <linux/netdevice.h>
20296f96fcSRusty Russell #include <linux/etherdevice.h>
21a9ea3fc6SHerbert Xu #include <linux/ethtool.h>
22296f96fcSRusty Russell #include <linux/module.h>
23296f96fcSRusty Russell #include <linux/virtio.h>
24296f96fcSRusty Russell #include <linux/virtio_net.h>
25f600b690SJohn Fastabend #include <linux/bpf.h>
26a67edbf4SDaniel Borkmann #include <linux/bpf_trace.h>
27296f96fcSRusty Russell #include <linux/scatterlist.h>
28e918085aSAlex Williamson #include <linux/if_vlan.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
308de4b2f3SWanlong Gao #include <linux/cpu.h>
31ab7db917SMichael Dalton #include <linux/average.h>
32296f96fcSRusty Russell 
33d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT;
346c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
356c0cd7c0SDor Laor 
36eb939922SRusty Russell static bool csum = true, gso = true;
3734a48579SRusty Russell module_param(csum, bool, 0444);
3834a48579SRusty Russell module_param(gso, bool, 0444);
3934a48579SRusty Russell 
40296f96fcSRusty Russell /* FIXME: MTU in config. */
415061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
423f2c31d9SMark McLoughlin #define GOOD_COPY_LEN	128
43296f96fcSRusty Russell 
44f6b10209SJason Wang #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
45f6b10209SJason Wang 
462de2f7f4SJohn Fastabend /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
472de2f7f4SJohn Fastabend #define VIRTIO_XDP_HEADROOM 256
482de2f7f4SJohn Fastabend 
495377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet
505377d758SJohannes Berg  * buffer size when refilling RX rings. As the entire RX ring may be refilled
515377d758SJohannes Berg  * at once, the weight is chosen so that the EWMA will be insensitive to short-
525377d758SJohannes Berg  * term, transient changes in packet size.
53ab7db917SMichael Dalton  */
54eb1e011aSJohannes Berg DECLARE_EWMA(pkt_len, 0, 64)
55ab7db917SMichael Dalton 
56d0fa28f0SMichael S. Tsirkin /* With mergeable buffers we align buffer address and use the low bits to
57d0fa28f0SMichael S. Tsirkin  * encode its true size. Buffer size is up to 1 page so we need to align to
58d0fa28f0SMichael S. Tsirkin  * square root of page size to ensure we reserve enough bits to encode the true
59d0fa28f0SMichael S. Tsirkin  * size.
60d0fa28f0SMichael S. Tsirkin  */
61d0fa28f0SMichael S. Tsirkin #define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2)
62d0fa28f0SMichael S. Tsirkin 
63ab7db917SMichael Dalton /* Minimum alignment for mergeable packet buffers. */
64d0fa28f0SMichael S. Tsirkin #define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \
65d0fa28f0SMichael S. Tsirkin 				   1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT)
66ab7db917SMichael Dalton 
6766846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0"
682a41f71dSAlex Williamson 
693fa2a1dfSstephen hemminger struct virtnet_stats {
7083a27052SEric Dumazet 	struct u64_stats_sync tx_syncp;
7183a27052SEric Dumazet 	struct u64_stats_sync rx_syncp;
723fa2a1dfSstephen hemminger 	u64 tx_bytes;
733fa2a1dfSstephen hemminger 	u64 tx_packets;
743fa2a1dfSstephen hemminger 
753fa2a1dfSstephen hemminger 	u64 rx_bytes;
763fa2a1dfSstephen hemminger 	u64 rx_packets;
773fa2a1dfSstephen hemminger };
783fa2a1dfSstephen hemminger 
79e9d7417bSJason Wang /* Internal representation of a send virtqueue */
80e9d7417bSJason Wang struct send_queue {
81e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
82e9d7417bSJason Wang 	struct virtqueue *vq;
83e9d7417bSJason Wang 
84e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
85e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
86986a4f4dSJason Wang 
87986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
88986a4f4dSJason Wang 	char name[40];
89e9d7417bSJason Wang };
90e9d7417bSJason Wang 
91e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
92e9d7417bSJason Wang struct receive_queue {
93e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
94e9d7417bSJason Wang 	struct virtqueue *vq;
95e9d7417bSJason Wang 
96296f96fcSRusty Russell 	struct napi_struct napi;
97296f96fcSRusty Russell 
98f600b690SJohn Fastabend 	struct bpf_prog __rcu *xdp_prog;
99f600b690SJohn Fastabend 
100e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
101e9d7417bSJason Wang 	struct page *pages;
102e9d7417bSJason Wang 
103ab7db917SMichael Dalton 	/* Average packet length for mergeable receive buffers. */
1045377d758SJohannes Berg 	struct ewma_pkt_len mrg_avg_pkt_len;
105ab7db917SMichael Dalton 
106fb51879dSMichael Dalton 	/* Page frag for packet buffer allocation. */
107fb51879dSMichael Dalton 	struct page_frag alloc_frag;
108fb51879dSMichael Dalton 
109e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
110e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
111986a4f4dSJason Wang 
112986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
113986a4f4dSJason Wang 	char name[40];
114e9d7417bSJason Wang };
115e9d7417bSJason Wang 
116e9d7417bSJason Wang struct virtnet_info {
117e9d7417bSJason Wang 	struct virtio_device *vdev;
118e9d7417bSJason Wang 	struct virtqueue *cvq;
119e9d7417bSJason Wang 	struct net_device *dev;
120986a4f4dSJason Wang 	struct send_queue *sq;
121986a4f4dSJason Wang 	struct receive_queue *rq;
122e9d7417bSJason Wang 	unsigned int status;
123e9d7417bSJason Wang 
124986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
125986a4f4dSJason Wang 	u16 max_queue_pairs;
126986a4f4dSJason Wang 
127986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
128986a4f4dSJason Wang 	u16 curr_queue_pairs;
129986a4f4dSJason Wang 
130672aafd5SJohn Fastabend 	/* # of XDP queue pairs currently used by the driver */
131672aafd5SJohn Fastabend 	u16 xdp_queue_pairs;
132672aafd5SJohn Fastabend 
13397402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
13497402b96SHerbert Xu 	bool big_packets;
13597402b96SHerbert Xu 
1363f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
1373f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
1383f2c31d9SMark McLoughlin 
139986a4f4dSJason Wang 	/* Has control virtqueue */
140986a4f4dSJason Wang 	bool has_cvq;
141986a4f4dSJason Wang 
142e7428e95SMichael S. Tsirkin 	/* Host can handle any s/g split between our header and packet data */
143e7428e95SMichael S. Tsirkin 	bool any_header_sg;
144e7428e95SMichael S. Tsirkin 
145012873d0SMichael S. Tsirkin 	/* Packet virtio header size */
146012873d0SMichael S. Tsirkin 	u8 hdr_len;
147012873d0SMichael S. Tsirkin 
1483fa2a1dfSstephen hemminger 	/* Active statistics */
1493fa2a1dfSstephen hemminger 	struct virtnet_stats __percpu *stats;
1503fa2a1dfSstephen hemminger 
1513161e453SRusty Russell 	/* Work struct for refilling if we run low on memory. */
1523161e453SRusty Russell 	struct delayed_work refill;
1533161e453SRusty Russell 
154586d17c5SJason Wang 	/* Work struct for config space updates */
155586d17c5SJason Wang 	struct work_struct config_work;
156586d17c5SJason Wang 
157986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
158986a4f4dSJason Wang 	bool affinity_hint_set;
15947be2479SWanlong Gao 
1608017c279SSebastian Andrzej Siewior 	/* CPU hotplug instances for online & dead */
1618017c279SSebastian Andrzej Siewior 	struct hlist_node node;
1628017c279SSebastian Andrzej Siewior 	struct hlist_node node_dead;
1632ac46030SMichael S. Tsirkin 
1642ac46030SMichael S. Tsirkin 	/* Control VQ buffers: protected by the rtnl lock */
1652ac46030SMichael S. Tsirkin 	struct virtio_net_ctrl_hdr ctrl_hdr;
1662ac46030SMichael S. Tsirkin 	virtio_net_ctrl_ack ctrl_status;
167a725ee3eSAndy Lutomirski 	struct virtio_net_ctrl_mq ctrl_mq;
1682ac46030SMichael S. Tsirkin 	u8 ctrl_promisc;
1692ac46030SMichael S. Tsirkin 	u8 ctrl_allmulti;
170a725ee3eSAndy Lutomirski 	u16 ctrl_vid;
17116032be5SNikolay Aleksandrov 
17216032be5SNikolay Aleksandrov 	/* Ethtool settings */
17316032be5SNikolay Aleksandrov 	u8 duplex;
17416032be5SNikolay Aleksandrov 	u32 speed;
175296f96fcSRusty Russell };
176296f96fcSRusty Russell 
1779ab86bbcSShirley Ma struct padded_vnet_hdr {
178012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf hdr;
1799ab86bbcSShirley Ma 	/*
180012873d0SMichael S. Tsirkin 	 * hdr is in a separate sg buffer, and data sg buffer shares same page
181012873d0SMichael S. Tsirkin 	 * with this header sg. This padding makes next sg 16 byte aligned
182012873d0SMichael S. Tsirkin 	 * after the header.
1839ab86bbcSShirley Ma 	 */
184012873d0SMichael S. Tsirkin 	char padding[4];
1859ab86bbcSShirley Ma };
1869ab86bbcSShirley Ma 
187986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
188986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
189986a4f4dSJason Wang  */
190986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
191986a4f4dSJason Wang {
1929d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
193986a4f4dSJason Wang }
194986a4f4dSJason Wang 
195986a4f4dSJason Wang static int txq2vq(int txq)
196986a4f4dSJason Wang {
197986a4f4dSJason Wang 	return txq * 2 + 1;
198986a4f4dSJason Wang }
199986a4f4dSJason Wang 
200986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
201986a4f4dSJason Wang {
2029d0ca6edSRusty Russell 	return vq->index / 2;
203986a4f4dSJason Wang }
204986a4f4dSJason Wang 
205986a4f4dSJason Wang static int rxq2vq(int rxq)
206986a4f4dSJason Wang {
207986a4f4dSJason Wang 	return rxq * 2;
208986a4f4dSJason Wang }
209986a4f4dSJason Wang 
210012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
211296f96fcSRusty Russell {
212012873d0SMichael S. Tsirkin 	return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
213296f96fcSRusty Russell }
214296f96fcSRusty Russell 
2159ab86bbcSShirley Ma /*
2169ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
2179ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
2189ab86bbcSShirley Ma  */
219e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
220fb6813f4SRusty Russell {
2219ab86bbcSShirley Ma 	struct page *end;
2229ab86bbcSShirley Ma 
223e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
2249ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
225e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
226e9d7417bSJason Wang 	rq->pages = page;
227fb6813f4SRusty Russell }
228fb6813f4SRusty Russell 
229e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
230fb6813f4SRusty Russell {
231e9d7417bSJason Wang 	struct page *p = rq->pages;
232fb6813f4SRusty Russell 
2339ab86bbcSShirley Ma 	if (p) {
234e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
2359ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
2369ab86bbcSShirley Ma 		p->private = 0;
2379ab86bbcSShirley Ma 	} else
238fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
239fb6813f4SRusty Russell 	return p;
240fb6813f4SRusty Russell }
241fb6813f4SRusty Russell 
242e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
243296f96fcSRusty Russell {
244e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
245296f96fcSRusty Russell 
2462cb9c6baSRusty Russell 	/* Suppress further interrupts. */
247e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
24811a3a154SRusty Russell 
249363f1514SRusty Russell 	/* We were probably waiting for more output buffers. */
250986a4f4dSJason Wang 	netif_wake_subqueue(vi->dev, vq2txq(vq));
251296f96fcSRusty Russell }
252296f96fcSRusty Russell 
2533464645aSMike Waychison /* Called from bottom half context */
254946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi,
255946fa564SMichael S. Tsirkin 				   struct receive_queue *rq,
2562613af0eSMichael Dalton 				   struct page *page, unsigned int offset,
2572613af0eSMichael Dalton 				   unsigned int len, unsigned int truesize)
2589ab86bbcSShirley Ma {
2599ab86bbcSShirley Ma 	struct sk_buff *skb;
260012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
2612613af0eSMichael Dalton 	unsigned int copy, hdr_len, hdr_padded_len;
2629ab86bbcSShirley Ma 	char *p;
2639ab86bbcSShirley Ma 
2642613af0eSMichael Dalton 	p = page_address(page) + offset;
2659ab86bbcSShirley Ma 
2669ab86bbcSShirley Ma 	/* copy small packet so we can reuse these pages for small data */
267c67f5db8SPaolo Abeni 	skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
2689ab86bbcSShirley Ma 	if (unlikely(!skb))
2699ab86bbcSShirley Ma 		return NULL;
2709ab86bbcSShirley Ma 
2719ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
2729ab86bbcSShirley Ma 
273012873d0SMichael S. Tsirkin 	hdr_len = vi->hdr_len;
274012873d0SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
275012873d0SMichael S. Tsirkin 		hdr_padded_len = sizeof *hdr;
276012873d0SMichael S. Tsirkin 	else
2772613af0eSMichael Dalton 		hdr_padded_len = sizeof(struct padded_vnet_hdr);
2783f2c31d9SMark McLoughlin 
2799ab86bbcSShirley Ma 	memcpy(hdr, p, hdr_len);
2803f2c31d9SMark McLoughlin 
2819ab86bbcSShirley Ma 	len -= hdr_len;
2822613af0eSMichael Dalton 	offset += hdr_padded_len;
2832613af0eSMichael Dalton 	p += hdr_padded_len;
2843f2c31d9SMark McLoughlin 
2853f2c31d9SMark McLoughlin 	copy = len;
2863f2c31d9SMark McLoughlin 	if (copy > skb_tailroom(skb))
2873f2c31d9SMark McLoughlin 		copy = skb_tailroom(skb);
2883f2c31d9SMark McLoughlin 	memcpy(skb_put(skb, copy), p, copy);
2893f2c31d9SMark McLoughlin 
2903f2c31d9SMark McLoughlin 	len -= copy;
2919ab86bbcSShirley Ma 	offset += copy;
2923f2c31d9SMark McLoughlin 
2932613af0eSMichael Dalton 	if (vi->mergeable_rx_bufs) {
2942613af0eSMichael Dalton 		if (len)
2952613af0eSMichael Dalton 			skb_add_rx_frag(skb, 0, page, offset, len, truesize);
2962613af0eSMichael Dalton 		else
2972613af0eSMichael Dalton 			put_page(page);
2982613af0eSMichael Dalton 		return skb;
2992613af0eSMichael Dalton 	}
3002613af0eSMichael Dalton 
301e878d78bSSasha Levin 	/*
302e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
303e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
304e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
305e878d78bSSasha Levin 	 * the case of a broken device.
306e878d78bSSasha Levin 	 */
307e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
308be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
309e878d78bSSasha Levin 		dev_kfree_skb(skb);
310e878d78bSSasha Levin 		return NULL;
311e878d78bSSasha Levin 	}
3122613af0eSMichael Dalton 	BUG_ON(offset >= PAGE_SIZE);
3139ab86bbcSShirley Ma 	while (len) {
3142613af0eSMichael Dalton 		unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
3152613af0eSMichael Dalton 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
3162613af0eSMichael Dalton 				frag_size, truesize);
3172613af0eSMichael Dalton 		len -= frag_size;
3189ab86bbcSShirley Ma 		page = (struct page *)page->private;
3199ab86bbcSShirley Ma 		offset = 0;
3203f2c31d9SMark McLoughlin 	}
3213f2c31d9SMark McLoughlin 
3229ab86bbcSShirley Ma 	if (page)
323e9d7417bSJason Wang 		give_pages(rq, page);
3243f2c31d9SMark McLoughlin 
3259ab86bbcSShirley Ma 	return skb;
3269ab86bbcSShirley Ma }
3279ab86bbcSShirley Ma 
328a67edbf4SDaniel Borkmann static bool virtnet_xdp_xmit(struct virtnet_info *vi,
32956434a01SJohn Fastabend 			     struct receive_queue *rq,
330f6b10209SJason Wang 			     struct xdp_buff *xdp)
33156434a01SJohn Fastabend {
33256434a01SJohn Fastabend 	struct virtio_net_hdr_mrg_rxbuf *hdr;
333f6b10209SJason Wang 	unsigned int len;
334722d8283SJohn Fastabend 	struct send_queue *sq;
335722d8283SJohn Fastabend 	unsigned int qp;
33656434a01SJohn Fastabend 	void *xdp_sent;
33756434a01SJohn Fastabend 	int err;
33856434a01SJohn Fastabend 
339722d8283SJohn Fastabend 	qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
340722d8283SJohn Fastabend 	sq = &vi->sq[qp];
341722d8283SJohn Fastabend 
34256434a01SJohn Fastabend 	/* Free up any pending old buffers before queueing new ones. */
34356434a01SJohn Fastabend 	while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
34456434a01SJohn Fastabend 		struct page *sent_page = virt_to_head_page(xdp_sent);
345bb91accfSJason Wang 
34656434a01SJohn Fastabend 		put_page(sent_page);
34756434a01SJohn Fastabend 	}
34856434a01SJohn Fastabend 
349f6b10209SJason Wang 	xdp->data -= vi->hdr_len;
35056434a01SJohn Fastabend 	/* Zero header and leave csum up to XDP layers */
35156434a01SJohn Fastabend 	hdr = xdp->data;
35256434a01SJohn Fastabend 	memset(hdr, 0, vi->hdr_len);
35356434a01SJohn Fastabend 
35456434a01SJohn Fastabend 	sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
355bb91accfSJason Wang 
356f6b10209SJason Wang 	err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC);
35756434a01SJohn Fastabend 	if (unlikely(err)) {
358bb91accfSJason Wang 		struct page *page = virt_to_head_page(xdp->data);
359bb91accfSJason Wang 
36056434a01SJohn Fastabend 		put_page(page);
361a67edbf4SDaniel Borkmann 		return false;
36256434a01SJohn Fastabend 	}
36356434a01SJohn Fastabend 
36456434a01SJohn Fastabend 	virtqueue_kick(sq->vq);
365a67edbf4SDaniel Borkmann 	return true;
36656434a01SJohn Fastabend }
36756434a01SJohn Fastabend 
368f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
369f6b10209SJason Wang {
370f6b10209SJason Wang 	return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
371f6b10209SJason Wang }
372f6b10209SJason Wang 
373bb91accfSJason Wang static struct sk_buff *receive_small(struct net_device *dev,
374bb91accfSJason Wang 				     struct virtnet_info *vi,
375bb91accfSJason Wang 				     struct receive_queue *rq,
376bb91accfSJason Wang 				     void *buf, unsigned int len)
377f121159dSMichael S. Tsirkin {
378f6b10209SJason Wang 	struct sk_buff *skb;
379bb91accfSJason Wang 	struct bpf_prog *xdp_prog;
380f6b10209SJason Wang 	unsigned int xdp_headroom = virtnet_get_headroom(vi);
381f6b10209SJason Wang 	unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
382f6b10209SJason Wang 	unsigned int headroom = vi->hdr_len + header_offset;
383f6b10209SJason Wang 	unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
384f6b10209SJason Wang 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
385f6b10209SJason Wang 	unsigned int delta = 0;
386012873d0SMichael S. Tsirkin 	len -= vi->hdr_len;
387f121159dSMichael S. Tsirkin 
388bb91accfSJason Wang 	rcu_read_lock();
389bb91accfSJason Wang 	xdp_prog = rcu_dereference(rq->xdp_prog);
390bb91accfSJason Wang 	if (xdp_prog) {
391f6b10209SJason Wang 		struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
3920354e4d1SJohn Fastabend 		struct xdp_buff xdp;
393f6b10209SJason Wang 		void *orig_data;
394bb91accfSJason Wang 		u32 act;
395bb91accfSJason Wang 
396bb91accfSJason Wang 		if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
397bb91accfSJason Wang 			goto err_xdp;
3980354e4d1SJohn Fastabend 
399f6b10209SJason Wang 		xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
400f6b10209SJason Wang 		xdp.data = xdp.data_hard_start + xdp_headroom;
4010354e4d1SJohn Fastabend 		xdp.data_end = xdp.data + len;
402f6b10209SJason Wang 		orig_data = xdp.data;
4030354e4d1SJohn Fastabend 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
4040354e4d1SJohn Fastabend 
405bb91accfSJason Wang 		switch (act) {
406bb91accfSJason Wang 		case XDP_PASS:
4072de2f7f4SJohn Fastabend 			/* Recalculate length in case bpf program changed it */
408f6b10209SJason Wang 			delta = orig_data - xdp.data;
409bb91accfSJason Wang 			break;
410bb91accfSJason Wang 		case XDP_TX:
411f6b10209SJason Wang 			if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
4120354e4d1SJohn Fastabend 				trace_xdp_exception(vi->dev, xdp_prog, act);
413bb91accfSJason Wang 			rcu_read_unlock();
414bb91accfSJason Wang 			goto xdp_xmit;
415bb91accfSJason Wang 		default:
4160354e4d1SJohn Fastabend 			bpf_warn_invalid_xdp_action(act);
4170354e4d1SJohn Fastabend 		case XDP_ABORTED:
4180354e4d1SJohn Fastabend 			trace_xdp_exception(vi->dev, xdp_prog, act);
4190354e4d1SJohn Fastabend 		case XDP_DROP:
420bb91accfSJason Wang 			goto err_xdp;
421bb91accfSJason Wang 		}
422bb91accfSJason Wang 	}
423bb91accfSJason Wang 	rcu_read_unlock();
424bb91accfSJason Wang 
425f6b10209SJason Wang 	skb = build_skb(buf, buflen);
426f6b10209SJason Wang 	if (!skb) {
427f6b10209SJason Wang 		put_page(virt_to_head_page(buf));
428f6b10209SJason Wang 		goto err;
429f6b10209SJason Wang 	}
430f6b10209SJason Wang 	skb_reserve(skb, headroom - delta);
431f6b10209SJason Wang 	skb_put(skb, len + delta);
432f6b10209SJason Wang 	if (!delta) {
433f6b10209SJason Wang 		buf += header_offset;
434f6b10209SJason Wang 		memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
435f6b10209SJason Wang 	} /* keep zeroed vnet hdr since packet was changed by bpf */
436f6b10209SJason Wang 
437f6b10209SJason Wang err:
438f121159dSMichael S. Tsirkin 	return skb;
439bb91accfSJason Wang 
440bb91accfSJason Wang err_xdp:
441bb91accfSJason Wang 	rcu_read_unlock();
442bb91accfSJason Wang 	dev->stats.rx_dropped++;
443f6b10209SJason Wang 	put_page(virt_to_head_page(buf));
444bb91accfSJason Wang xdp_xmit:
445bb91accfSJason Wang 	return NULL;
446f121159dSMichael S. Tsirkin }
447f121159dSMichael S. Tsirkin 
448f121159dSMichael S. Tsirkin static struct sk_buff *receive_big(struct net_device *dev,
449946fa564SMichael S. Tsirkin 				   struct virtnet_info *vi,
450f121159dSMichael S. Tsirkin 				   struct receive_queue *rq,
451f121159dSMichael S. Tsirkin 				   void *buf,
452f121159dSMichael S. Tsirkin 				   unsigned int len)
453f121159dSMichael S. Tsirkin {
454f121159dSMichael S. Tsirkin 	struct page *page = buf;
455c47a43d3SJason Wang 	struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
456f121159dSMichael S. Tsirkin 
457f121159dSMichael S. Tsirkin 	if (unlikely(!skb))
458f121159dSMichael S. Tsirkin 		goto err;
459f121159dSMichael S. Tsirkin 
460f121159dSMichael S. Tsirkin 	return skb;
461f121159dSMichael S. Tsirkin 
462f121159dSMichael S. Tsirkin err:
463f121159dSMichael S. Tsirkin 	dev->stats.rx_dropped++;
464f121159dSMichael S. Tsirkin 	give_pages(rq, page);
465f121159dSMichael S. Tsirkin 	return NULL;
466f121159dSMichael S. Tsirkin }
467f121159dSMichael S. Tsirkin 
46872979a6cSJohn Fastabend /* The conditions to enable XDP should preclude the underlying device from
46972979a6cSJohn Fastabend  * sending packets across multiple buffers (num_buf > 1). However per spec
47072979a6cSJohn Fastabend  * it does not appear to be illegal to do so but rather just against convention.
47172979a6cSJohn Fastabend  * So in order to avoid making a system unresponsive the packets are pushed
47272979a6cSJohn Fastabend  * into a page and the XDP program is run. This will be extremely slow and we
47372979a6cSJohn Fastabend  * push a warning to the user to fix this as soon as possible. Fixing this may
47472979a6cSJohn Fastabend  * require resolving the underlying hardware to determine why multiple buffers
47572979a6cSJohn Fastabend  * are being received or simply loading the XDP program in the ingress stack
47672979a6cSJohn Fastabend  * after the skb is built because there is no advantage to running it here
47772979a6cSJohn Fastabend  * anymore.
47872979a6cSJohn Fastabend  */
47972979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq,
48056a86f84SJason Wang 				       u16 *num_buf,
48172979a6cSJohn Fastabend 				       struct page *p,
48272979a6cSJohn Fastabend 				       int offset,
48372979a6cSJohn Fastabend 				       unsigned int *len)
48472979a6cSJohn Fastabend {
48572979a6cSJohn Fastabend 	struct page *page = alloc_page(GFP_ATOMIC);
4862de2f7f4SJohn Fastabend 	unsigned int page_off = VIRTIO_XDP_HEADROOM;
48772979a6cSJohn Fastabend 
48872979a6cSJohn Fastabend 	if (!page)
48972979a6cSJohn Fastabend 		return NULL;
49072979a6cSJohn Fastabend 
49172979a6cSJohn Fastabend 	memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
49272979a6cSJohn Fastabend 	page_off += *len;
49372979a6cSJohn Fastabend 
49456a86f84SJason Wang 	while (--*num_buf) {
49572979a6cSJohn Fastabend 		unsigned int buflen;
49672979a6cSJohn Fastabend 		void *buf;
49772979a6cSJohn Fastabend 		int off;
49872979a6cSJohn Fastabend 
499*680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &buflen);
500*680557cfSMichael S. Tsirkin 		if (unlikely(!buf))
50172979a6cSJohn Fastabend 			goto err_buf;
50272979a6cSJohn Fastabend 
50372979a6cSJohn Fastabend 		p = virt_to_head_page(buf);
50472979a6cSJohn Fastabend 		off = buf - page_address(p);
50572979a6cSJohn Fastabend 
50656a86f84SJason Wang 		/* guard against a misconfigured or uncooperative backend that
50756a86f84SJason Wang 		 * is sending packet larger than the MTU.
50856a86f84SJason Wang 		 */
50956a86f84SJason Wang 		if ((page_off + buflen) > PAGE_SIZE) {
51056a86f84SJason Wang 			put_page(p);
51156a86f84SJason Wang 			goto err_buf;
51256a86f84SJason Wang 		}
51356a86f84SJason Wang 
51472979a6cSJohn Fastabend 		memcpy(page_address(page) + page_off,
51572979a6cSJohn Fastabend 		       page_address(p) + off, buflen);
51672979a6cSJohn Fastabend 		page_off += buflen;
51756a86f84SJason Wang 		put_page(p);
51872979a6cSJohn Fastabend 	}
51972979a6cSJohn Fastabend 
5202de2f7f4SJohn Fastabend 	/* Headroom does not contribute to packet length */
5212de2f7f4SJohn Fastabend 	*len = page_off - VIRTIO_XDP_HEADROOM;
52272979a6cSJohn Fastabend 	return page;
52372979a6cSJohn Fastabend err_buf:
52472979a6cSJohn Fastabend 	__free_pages(page, 0);
52572979a6cSJohn Fastabend 	return NULL;
52672979a6cSJohn Fastabend }
52772979a6cSJohn Fastabend 
5288fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev,
529fdd819b2SMichael S. Tsirkin 					 struct virtnet_info *vi,
5308fc3b9e9SMichael S. Tsirkin 					 struct receive_queue *rq,
531*680557cfSMichael S. Tsirkin 					 void *buf,
532*680557cfSMichael S. Tsirkin 					 void *ctx,
5338fc3b9e9SMichael S. Tsirkin 					 unsigned int len)
5349ab86bbcSShirley Ma {
535012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
536012873d0SMichael S. Tsirkin 	u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
5378fc3b9e9SMichael S. Tsirkin 	struct page *page = virt_to_head_page(buf);
5388fc3b9e9SMichael S. Tsirkin 	int offset = buf - page_address(page);
539f600b690SJohn Fastabend 	struct sk_buff *head_skb, *curr_skb;
540f600b690SJohn Fastabend 	struct bpf_prog *xdp_prog;
541f600b690SJohn Fastabend 	unsigned int truesize;
542ab7db917SMichael Dalton 
54356434a01SJohn Fastabend 	head_skb = NULL;
54456434a01SJohn Fastabend 
545f600b690SJohn Fastabend 	rcu_read_lock();
546f600b690SJohn Fastabend 	xdp_prog = rcu_dereference(rq->xdp_prog);
547f600b690SJohn Fastabend 	if (xdp_prog) {
54872979a6cSJohn Fastabend 		struct page *xdp_page;
5490354e4d1SJohn Fastabend 		struct xdp_buff xdp;
5500354e4d1SJohn Fastabend 		void *data;
551f600b690SJohn Fastabend 		u32 act;
552f600b690SJohn Fastabend 
55373b62bd0SJason Wang 		/* This happens when rx buffer size is underestimated */
554f600b690SJohn Fastabend 		if (unlikely(num_buf > 1)) {
55572979a6cSJohn Fastabend 			/* linearize data for XDP */
55656a86f84SJason Wang 			xdp_page = xdp_linearize_page(rq, &num_buf,
55772979a6cSJohn Fastabend 						      page, offset, &len);
55872979a6cSJohn Fastabend 			if (!xdp_page)
559f600b690SJohn Fastabend 				goto err_xdp;
5602de2f7f4SJohn Fastabend 			offset = VIRTIO_XDP_HEADROOM;
56172979a6cSJohn Fastabend 		} else {
56272979a6cSJohn Fastabend 			xdp_page = page;
563f600b690SJohn Fastabend 		}
564f600b690SJohn Fastabend 
565f600b690SJohn Fastabend 		/* Transient failure which in theory could occur if
566f600b690SJohn Fastabend 		 * in-flight packets from before XDP was enabled reach
567f600b690SJohn Fastabend 		 * the receive path after XDP is loaded. In practice I
568f600b690SJohn Fastabend 		 * was not able to create this condition.
569f600b690SJohn Fastabend 		 */
570b00f70b0SJason Wang 		if (unlikely(hdr->hdr.gso_type))
571f600b690SJohn Fastabend 			goto err_xdp;
572f600b690SJohn Fastabend 
5732de2f7f4SJohn Fastabend 		/* Allow consuming headroom but reserve enough space to push
5742de2f7f4SJohn Fastabend 		 * the descriptor on if we get an XDP_TX return code.
5752de2f7f4SJohn Fastabend 		 */
5760354e4d1SJohn Fastabend 		data = page_address(xdp_page) + offset;
5772de2f7f4SJohn Fastabend 		xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
5780354e4d1SJohn Fastabend 		xdp.data = data + vi->hdr_len;
5790354e4d1SJohn Fastabend 		xdp.data_end = xdp.data + (len - vi->hdr_len);
5800354e4d1SJohn Fastabend 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
5810354e4d1SJohn Fastabend 
58256434a01SJohn Fastabend 		switch (act) {
58356434a01SJohn Fastabend 		case XDP_PASS:
5842de2f7f4SJohn Fastabend 			/* recalculate offset to account for any header
5852de2f7f4SJohn Fastabend 			 * adjustments. Note other cases do not build an
5862de2f7f4SJohn Fastabend 			 * skb and avoid using offset
5872de2f7f4SJohn Fastabend 			 */
5882de2f7f4SJohn Fastabend 			offset = xdp.data -
5892de2f7f4SJohn Fastabend 					page_address(xdp_page) - vi->hdr_len;
5902de2f7f4SJohn Fastabend 
5911830f893SJason Wang 			/* We can only create skb based on xdp_page. */
5921830f893SJason Wang 			if (unlikely(xdp_page != page)) {
5931830f893SJason Wang 				rcu_read_unlock();
5941830f893SJason Wang 				put_page(page);
5951830f893SJason Wang 				head_skb = page_to_skb(vi, rq, xdp_page,
5962de2f7f4SJohn Fastabend 						       offset, len, PAGE_SIZE);
5975c33474dSJason Wang 				ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
5981830f893SJason Wang 				return head_skb;
5991830f893SJason Wang 			}
60056434a01SJohn Fastabend 			break;
60156434a01SJohn Fastabend 		case XDP_TX:
602f6b10209SJason Wang 			if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
6030354e4d1SJohn Fastabend 				trace_xdp_exception(vi->dev, xdp_prog, act);
6045c33474dSJason Wang 			ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
60572979a6cSJohn Fastabend 			if (unlikely(xdp_page != page))
60672979a6cSJohn Fastabend 				goto err_xdp;
60756434a01SJohn Fastabend 			rcu_read_unlock();
60856434a01SJohn Fastabend 			goto xdp_xmit;
60956434a01SJohn Fastabend 		default:
6100354e4d1SJohn Fastabend 			bpf_warn_invalid_xdp_action(act);
6110354e4d1SJohn Fastabend 		case XDP_ABORTED:
6120354e4d1SJohn Fastabend 			trace_xdp_exception(vi->dev, xdp_prog, act);
6130354e4d1SJohn Fastabend 		case XDP_DROP:
61472979a6cSJohn Fastabend 			if (unlikely(xdp_page != page))
61572979a6cSJohn Fastabend 				__free_pages(xdp_page, 0);
6165c33474dSJason Wang 			ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
617f600b690SJohn Fastabend 			goto err_xdp;
618f600b690SJohn Fastabend 		}
61956434a01SJohn Fastabend 	}
620f600b690SJohn Fastabend 	rcu_read_unlock();
621f600b690SJohn Fastabend 
622*680557cfSMichael S. Tsirkin 	if (unlikely(len > (unsigned long)ctx)) {
623*680557cfSMichael S. Tsirkin 		pr_debug("%s: rx error: len %u exceeds truesize 0x%lu\n",
624*680557cfSMichael S. Tsirkin 			 dev->name, len, (unsigned long)ctx);
625*680557cfSMichael S. Tsirkin 		dev->stats.rx_length_errors++;
626*680557cfSMichael S. Tsirkin 		goto err_skb;
627*680557cfSMichael S. Tsirkin 	}
628*680557cfSMichael S. Tsirkin 	truesize = (unsigned long)ctx;
629f600b690SJohn Fastabend 	head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
630f600b690SJohn Fastabend 	curr_skb = head_skb;
6319ab86bbcSShirley Ma 
6328fc3b9e9SMichael S. Tsirkin 	if (unlikely(!curr_skb))
6338fc3b9e9SMichael S. Tsirkin 		goto err_skb;
6349ab86bbcSShirley Ma 	while (--num_buf) {
6358fc3b9e9SMichael S. Tsirkin 		int num_skb_frags;
6368fc3b9e9SMichael S. Tsirkin 
637*680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
638ab7db917SMichael Dalton 		if (unlikely(!ctx)) {
6398fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
640fdd819b2SMichael S. Tsirkin 				 dev->name, num_buf,
641012873d0SMichael S. Tsirkin 				 virtio16_to_cpu(vi->vdev,
642012873d0SMichael S. Tsirkin 						 hdr->num_buffers));
6438fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
6448fc3b9e9SMichael S. Tsirkin 			goto err_buf;
6453f2c31d9SMark McLoughlin 		}
6468fc3b9e9SMichael S. Tsirkin 
6478fc3b9e9SMichael S. Tsirkin 		page = virt_to_head_page(buf);
648*680557cfSMichael S. Tsirkin 		if (unlikely(len > (unsigned long)ctx)) {
649*680557cfSMichael S. Tsirkin 			pr_debug("%s: rx error: len %u exceeds truesize 0x%lu\n",
650*680557cfSMichael S. Tsirkin 				 dev->name, len, (unsigned long)ctx);
651*680557cfSMichael S. Tsirkin 			dev->stats.rx_length_errors++;
652*680557cfSMichael S. Tsirkin 			goto err_skb;
653*680557cfSMichael S. Tsirkin 		}
654*680557cfSMichael S. Tsirkin 		truesize = (unsigned long)ctx;
6558fc3b9e9SMichael S. Tsirkin 
6568fc3b9e9SMichael S. Tsirkin 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
6572613af0eSMichael Dalton 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
6582613af0eSMichael Dalton 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
6598fc3b9e9SMichael S. Tsirkin 
6608fc3b9e9SMichael S. Tsirkin 			if (unlikely(!nskb))
6618fc3b9e9SMichael S. Tsirkin 				goto err_skb;
6622613af0eSMichael Dalton 			if (curr_skb == head_skb)
6632613af0eSMichael Dalton 				skb_shinfo(curr_skb)->frag_list = nskb;
6642613af0eSMichael Dalton 			else
6652613af0eSMichael Dalton 				curr_skb->next = nskb;
6662613af0eSMichael Dalton 			curr_skb = nskb;
6672613af0eSMichael Dalton 			head_skb->truesize += nskb->truesize;
6682613af0eSMichael Dalton 			num_skb_frags = 0;
6692613af0eSMichael Dalton 		}
6702613af0eSMichael Dalton 		if (curr_skb != head_skb) {
6712613af0eSMichael Dalton 			head_skb->data_len += len;
6722613af0eSMichael Dalton 			head_skb->len += len;
673fb51879dSMichael Dalton 			head_skb->truesize += truesize;
6742613af0eSMichael Dalton 		}
6758fc3b9e9SMichael S. Tsirkin 		offset = buf - page_address(page);
676ba275241SJason Wang 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
677ba275241SJason Wang 			put_page(page);
678ba275241SJason Wang 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
679fb51879dSMichael Dalton 					     len, truesize);
680ba275241SJason Wang 		} else {
6812613af0eSMichael Dalton 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
682fb51879dSMichael Dalton 					offset, len, truesize);
683ba275241SJason Wang 		}
6848fc3b9e9SMichael S. Tsirkin 	}
6858fc3b9e9SMichael S. Tsirkin 
6865377d758SJohannes Berg 	ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
6878fc3b9e9SMichael S. Tsirkin 	return head_skb;
6888fc3b9e9SMichael S. Tsirkin 
689f600b690SJohn Fastabend err_xdp:
690f600b690SJohn Fastabend 	rcu_read_unlock();
6918fc3b9e9SMichael S. Tsirkin err_skb:
6928fc3b9e9SMichael S. Tsirkin 	put_page(page);
6938fc3b9e9SMichael S. Tsirkin 	while (--num_buf) {
694*680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &len);
695*680557cfSMichael S. Tsirkin 		if (unlikely(!buf)) {
6968fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers missing\n",
6978fc3b9e9SMichael S. Tsirkin 				 dev->name, num_buf);
6988fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
6998fc3b9e9SMichael S. Tsirkin 			break;
7008fc3b9e9SMichael S. Tsirkin 		}
701*680557cfSMichael S. Tsirkin 		page = virt_to_head_page(buf);
7028fc3b9e9SMichael S. Tsirkin 		put_page(page);
7033f2c31d9SMark McLoughlin 	}
7048fc3b9e9SMichael S. Tsirkin err_buf:
7058fc3b9e9SMichael S. Tsirkin 	dev->stats.rx_dropped++;
7068fc3b9e9SMichael S. Tsirkin 	dev_kfree_skb(head_skb);
70756434a01SJohn Fastabend xdp_xmit:
7088fc3b9e9SMichael S. Tsirkin 	return NULL;
7099ab86bbcSShirley Ma }
7109ab86bbcSShirley Ma 
71161845d20SJason Wang static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
712*680557cfSMichael S. Tsirkin 		       void *buf, unsigned int len, void **ctx)
7139ab86bbcSShirley Ma {
714e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
7159ab86bbcSShirley Ma 	struct sk_buff *skb;
716012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
71761845d20SJason Wang 	int ret;
7189ab86bbcSShirley Ma 
719bcff3162SMichael S. Tsirkin 	if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
7209ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
7219ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
722ab7db917SMichael Dalton 		if (vi->mergeable_rx_bufs) {
723*680557cfSMichael S. Tsirkin 			put_page(virt_to_head_page(buf));
724ab7db917SMichael Dalton 		} else if (vi->big_packets) {
72598bfd23cSMichael Dalton 			give_pages(rq, buf);
726ab7db917SMichael Dalton 		} else {
727f6b10209SJason Wang 			put_page(virt_to_head_page(buf));
728ab7db917SMichael Dalton 		}
72961845d20SJason Wang 		return 0;
7309ab86bbcSShirley Ma 	}
7319ab86bbcSShirley Ma 
732f121159dSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
733*680557cfSMichael S. Tsirkin 		skb = receive_mergeable(dev, vi, rq, buf, ctx, len);
734f121159dSMichael S. Tsirkin 	else if (vi->big_packets)
735946fa564SMichael S. Tsirkin 		skb = receive_big(dev, vi, rq, buf, len);
736f121159dSMichael S. Tsirkin 	else
737bb91accfSJason Wang 		skb = receive_small(dev, vi, rq, buf, len);
738f121159dSMichael S. Tsirkin 
7398fc3b9e9SMichael S. Tsirkin 	if (unlikely(!skb))
74061845d20SJason Wang 		return 0;
7413f2c31d9SMark McLoughlin 
7429ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
7433fa2a1dfSstephen hemminger 
74461845d20SJason Wang 	ret = skb->len;
745296f96fcSRusty Russell 
746e858fae2SMike Rapoport 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
74710a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
748296f96fcSRusty Russell 
749e858fae2SMike Rapoport 	if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
750e858fae2SMike Rapoport 				  virtio_is_little_endian(vi->vdev))) {
751e858fae2SMike Rapoport 		net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
752e858fae2SMike Rapoport 				     dev->name, hdr->hdr.gso_type,
753fdd819b2SMichael S. Tsirkin 				     hdr->hdr.gso_size);
754296f96fcSRusty Russell 		goto frame_err;
755296f96fcSRusty Russell 	}
756296f96fcSRusty Russell 
757d1dc06dcSMike Rapoport 	skb->protocol = eth_type_trans(skb, dev);
758d1dc06dcSMike Rapoport 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
759d1dc06dcSMike Rapoport 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
760d1dc06dcSMike Rapoport 
7610fbd050aSEric Dumazet 	napi_gro_receive(&rq->napi, skb);
76261845d20SJason Wang 	return ret;
763296f96fcSRusty Russell 
764296f96fcSRusty Russell frame_err:
765296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
766296f96fcSRusty Russell 	dev_kfree_skb(skb);
76761845d20SJason Wang 	return 0;
768296f96fcSRusty Russell }
769296f96fcSRusty Russell 
770946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
771946fa564SMichael S. Tsirkin 			     gfp_t gfp)
772296f96fcSRusty Russell {
773f6b10209SJason Wang 	struct page_frag *alloc_frag = &rq->alloc_frag;
774f6b10209SJason Wang 	char *buf;
7752de2f7f4SJohn Fastabend 	unsigned int xdp_headroom = virtnet_get_headroom(vi);
776f6b10209SJason Wang 	int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
7779ab86bbcSShirley Ma 	int err;
7783f2c31d9SMark McLoughlin 
779f6b10209SJason Wang 	len = SKB_DATA_ALIGN(len) +
780f6b10209SJason Wang 	      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
781f6b10209SJason Wang 	if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
7829ab86bbcSShirley Ma 		return -ENOMEM;
783296f96fcSRusty Russell 
784f6b10209SJason Wang 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
785f6b10209SJason Wang 	get_page(alloc_frag->page);
786f6b10209SJason Wang 	alloc_frag->offset += len;
787f6b10209SJason Wang 	sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
788f6b10209SJason Wang 		    vi->hdr_len + GOOD_PACKET_LEN);
789f6b10209SJason Wang 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
7909ab86bbcSShirley Ma 	if (err < 0)
791f6b10209SJason Wang 		put_page(virt_to_head_page(buf));
79297402b96SHerbert Xu 
7939ab86bbcSShirley Ma 	return err;
79497402b96SHerbert Xu }
79597402b96SHerbert Xu 
796012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
797012873d0SMichael S. Tsirkin 			   gfp_t gfp)
7989ab86bbcSShirley Ma {
7999ab86bbcSShirley Ma 	struct page *first, *list = NULL;
8009ab86bbcSShirley Ma 	char *p;
8019ab86bbcSShirley Ma 	int i, err, offset;
802296f96fcSRusty Russell 
803a5835440SRusty Russell 	sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
804a5835440SRusty Russell 
805e9d7417bSJason Wang 	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
8069ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
807e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
8089ab86bbcSShirley Ma 		if (!first) {
8099ab86bbcSShirley Ma 			if (list)
810e9d7417bSJason Wang 				give_pages(rq, list);
8119ab86bbcSShirley Ma 			return -ENOMEM;
812296f96fcSRusty Russell 		}
813e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
8149ab86bbcSShirley Ma 
8159ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
8169ab86bbcSShirley Ma 		first->private = (unsigned long)list;
8179ab86bbcSShirley Ma 		list = first;
8189ab86bbcSShirley Ma 	}
8199ab86bbcSShirley Ma 
820e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
8219ab86bbcSShirley Ma 	if (!first) {
822e9d7417bSJason Wang 		give_pages(rq, list);
8239ab86bbcSShirley Ma 		return -ENOMEM;
8249ab86bbcSShirley Ma 	}
8259ab86bbcSShirley Ma 	p = page_address(first);
8269ab86bbcSShirley Ma 
827e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
828012873d0SMichael S. Tsirkin 	/* a separated rq->sg[0] for header - required in case !any_header_sg */
829012873d0SMichael S. Tsirkin 	sg_set_buf(&rq->sg[0], p, vi->hdr_len);
8309ab86bbcSShirley Ma 
831e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
8329ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
833e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
8349ab86bbcSShirley Ma 
8359ab86bbcSShirley Ma 	/* chain first in list head */
8369ab86bbcSShirley Ma 	first->private = (unsigned long)list;
8379dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
838aa989f5eSMichael S. Tsirkin 				  first, gfp);
8399ab86bbcSShirley Ma 	if (err < 0)
840e9d7417bSJason Wang 		give_pages(rq, first);
8419ab86bbcSShirley Ma 
8429ab86bbcSShirley Ma 	return err;
8439ab86bbcSShirley Ma }
8449ab86bbcSShirley Ma 
8455377d758SJohannes Berg static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
8469ab86bbcSShirley Ma {
847ab7db917SMichael Dalton 	const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
848fbf28d78SMichael Dalton 	unsigned int len;
849fbf28d78SMichael Dalton 
8505377d758SJohannes Berg 	len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
851fbf28d78SMichael Dalton 			GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
852fbf28d78SMichael Dalton 	return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
853fbf28d78SMichael Dalton }
854fbf28d78SMichael Dalton 
8552de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi,
8562de2f7f4SJohn Fastabend 				 struct receive_queue *rq, gfp_t gfp)
857fbf28d78SMichael Dalton {
858fb51879dSMichael Dalton 	struct page_frag *alloc_frag = &rq->alloc_frag;
8592de2f7f4SJohn Fastabend 	unsigned int headroom = virtnet_get_headroom(vi);
860fb51879dSMichael Dalton 	char *buf;
861*680557cfSMichael S. Tsirkin 	void *ctx;
8629ab86bbcSShirley Ma 	int err;
863fb51879dSMichael Dalton 	unsigned int len, hole;
8649ab86bbcSShirley Ma 
865fbf28d78SMichael Dalton 	len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
8662de2f7f4SJohn Fastabend 	if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp)))
8679ab86bbcSShirley Ma 		return -ENOMEM;
868ab7db917SMichael Dalton 
869fb51879dSMichael Dalton 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
8702de2f7f4SJohn Fastabend 	buf += headroom; /* advance address leaving hole at front of pkt */
871*680557cfSMichael S. Tsirkin 	ctx = (void *)(unsigned long)len;
872fb51879dSMichael Dalton 	get_page(alloc_frag->page);
8732de2f7f4SJohn Fastabend 	alloc_frag->offset += len + headroom;
874fb51879dSMichael Dalton 	hole = alloc_frag->size - alloc_frag->offset;
8752de2f7f4SJohn Fastabend 	if (hole < len + headroom) {
876ab7db917SMichael Dalton 		/* To avoid internal fragmentation, if there is very likely not
877ab7db917SMichael Dalton 		 * enough space for another buffer, add the remaining space to
878ab7db917SMichael Dalton 		 * the current buffer. This extra space is not included in
879ab7db917SMichael Dalton 		 * the truesize stored in ctx.
880ab7db917SMichael Dalton 		 */
881fb51879dSMichael Dalton 		len += hole;
882fb51879dSMichael Dalton 		alloc_frag->offset += hole;
883fb51879dSMichael Dalton 	}
8849ab86bbcSShirley Ma 
885fb51879dSMichael Dalton 	sg_init_one(rq->sg, buf, len);
886*680557cfSMichael S. Tsirkin 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
8879ab86bbcSShirley Ma 	if (err < 0)
8882613af0eSMichael Dalton 		put_page(virt_to_head_page(buf));
8899ab86bbcSShirley Ma 
8909ab86bbcSShirley Ma 	return err;
891296f96fcSRusty Russell }
892296f96fcSRusty Russell 
893b2baed69SRusty Russell /*
894b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
895b2baed69SRusty Russell  *
896b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
897b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
898b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
899b2baed69SRusty Russell  */
900946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
901946fa564SMichael S. Tsirkin 			  gfp_t gfp)
9023f2c31d9SMark McLoughlin {
9033f2c31d9SMark McLoughlin 	int err;
9041788f495SMichael S. Tsirkin 	bool oom;
9053f2c31d9SMark McLoughlin 
906fb51879dSMichael Dalton 	gfp |= __GFP_COLD;
9070aea51c3SAmit Shah 	do {
9089ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
9092de2f7f4SJohn Fastabend 			err = add_recvbuf_mergeable(vi, rq, gfp);
9109ab86bbcSShirley Ma 		else if (vi->big_packets)
911012873d0SMichael S. Tsirkin 			err = add_recvbuf_big(vi, rq, gfp);
9129ab86bbcSShirley Ma 		else
913946fa564SMichael S. Tsirkin 			err = add_recvbuf_small(vi, rq, gfp);
9143f2c31d9SMark McLoughlin 
9151788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
9169ed4cb07SRusty Russell 		if (err)
9173f2c31d9SMark McLoughlin 			break;
918b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
919681daee2SJason Wang 	virtqueue_kick(rq->vq);
9203161e453SRusty Russell 	return !oom;
9213f2c31d9SMark McLoughlin }
9223f2c31d9SMark McLoughlin 
92318445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
924296f96fcSRusty Russell {
925296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
926986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
927e9d7417bSJason Wang 
92818445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
929e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
9301915a712SMichael S. Tsirkin 		virtqueue_disable_cb(rvq);
931e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
93218445c4dSRusty Russell 	}
933296f96fcSRusty Russell }
934296f96fcSRusty Russell 
935e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq)
9363e9d08ecSBruce Rogers {
937e9d7417bSJason Wang 	napi_enable(&rq->napi);
9383e9d08ecSBruce Rogers 
9393e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
9403e9d08ecSBruce Rogers 	 * won't get another interrupt, so process any outstanding packets
9413e9d08ecSBruce Rogers 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
9423e9d08ecSBruce Rogers 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
943e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
944e9d7417bSJason Wang 		virtqueue_disable_cb(rq->vq);
945ec13ee80SMichael S. Tsirkin 		local_bh_disable();
946e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
947ec13ee80SMichael S. Tsirkin 		local_bh_enable();
9483e9d08ecSBruce Rogers 	}
9493e9d08ecSBruce Rogers }
9503e9d08ecSBruce Rogers 
9513161e453SRusty Russell static void refill_work(struct work_struct *work)
9523161e453SRusty Russell {
953e9d7417bSJason Wang 	struct virtnet_info *vi =
954e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
9553161e453SRusty Russell 	bool still_empty;
956986a4f4dSJason Wang 	int i;
9573161e453SRusty Russell 
95855257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
959986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
960986a4f4dSJason Wang 
961986a4f4dSJason Wang 		napi_disable(&rq->napi);
962946fa564SMichael S. Tsirkin 		still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
963986a4f4dSJason Wang 		virtnet_napi_enable(rq);
9643161e453SRusty Russell 
9653161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
966986a4f4dSJason Wang 		 * we will *never* try to fill again.
967986a4f4dSJason Wang 		 */
9683161e453SRusty Russell 		if (still_empty)
9693b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
9703161e453SRusty Russell 	}
971986a4f4dSJason Wang }
9723161e453SRusty Russell 
9732ffa7598SJason Wang static int virtnet_receive(struct receive_queue *rq, int budget)
974296f96fcSRusty Russell {
975e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
97661845d20SJason Wang 	unsigned int len, received = 0, bytes = 0;
9779ab86bbcSShirley Ma 	void *buf;
97861845d20SJason Wang 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
979296f96fcSRusty Russell 
980*680557cfSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs) {
981*680557cfSMichael S. Tsirkin 		void *ctx;
982*680557cfSMichael S. Tsirkin 
983*680557cfSMichael S. Tsirkin 		while (received < budget &&
984*680557cfSMichael S. Tsirkin 		       (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
985*680557cfSMichael S. Tsirkin 			bytes += receive_buf(vi, rq, buf, len, ctx);
986*680557cfSMichael S. Tsirkin 			received++;
987*680557cfSMichael S. Tsirkin 		}
988*680557cfSMichael S. Tsirkin 	} else {
989296f96fcSRusty Russell 		while (received < budget &&
990e9d7417bSJason Wang 		       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
991*680557cfSMichael S. Tsirkin 			bytes += receive_buf(vi, rq, buf, len, NULL);
992296f96fcSRusty Russell 			received++;
993296f96fcSRusty Russell 		}
994*680557cfSMichael S. Tsirkin 	}
995296f96fcSRusty Russell 
996be121f46SJason Wang 	if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
997946fa564SMichael S. Tsirkin 		if (!try_fill_recv(vi, rq, GFP_ATOMIC))
9983b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
9993161e453SRusty Russell 	}
1000296f96fcSRusty Russell 
100161845d20SJason Wang 	u64_stats_update_begin(&stats->rx_syncp);
100261845d20SJason Wang 	stats->rx_bytes += bytes;
100361845d20SJason Wang 	stats->rx_packets += received;
100461845d20SJason Wang 	u64_stats_update_end(&stats->rx_syncp);
100561845d20SJason Wang 
10062ffa7598SJason Wang 	return received;
10072ffa7598SJason Wang }
10082ffa7598SJason Wang 
10092ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget)
10102ffa7598SJason Wang {
10112ffa7598SJason Wang 	struct receive_queue *rq =
10122ffa7598SJason Wang 		container_of(napi, struct receive_queue, napi);
1013faadb05fSLi RongQing 	unsigned int r, received;
10142ffa7598SJason Wang 
1015faadb05fSLi RongQing 	received = virtnet_receive(rq, budget);
10162ffa7598SJason Wang 
10178329d98eSRusty Russell 	/* Out of packets? */
10188329d98eSRusty Russell 	if (received < budget) {
1019cbdadbbfSMichael S. Tsirkin 		r = virtqueue_enable_cb_prepare(rq->vq);
10204d6308aaSEric Dumazet 		if (napi_complete_done(napi, received)) {
1021cbdadbbfSMichael S. Tsirkin 			if (unlikely(virtqueue_poll(rq->vq, r)) &&
10228e95a202SJoe Perches 			    napi_schedule_prep(napi)) {
1023e9d7417bSJason Wang 				virtqueue_disable_cb(rq->vq);
1024288379f0SBen Hutchings 				__napi_schedule(napi);
1025296f96fcSRusty Russell 			}
10264265f161SChristian Borntraeger 		}
10274d6308aaSEric Dumazet 	}
1028296f96fcSRusty Russell 
1029296f96fcSRusty Russell 	return received;
1030296f96fcSRusty Russell }
1031296f96fcSRusty Russell 
1032986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
1033986a4f4dSJason Wang {
1034986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1035986a4f4dSJason Wang 	int i;
1036986a4f4dSJason Wang 
1037e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1038e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
1039986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
1040946fa564SMichael S. Tsirkin 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1041986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
1042986a4f4dSJason Wang 		virtnet_napi_enable(&vi->rq[i]);
1043986a4f4dSJason Wang 	}
1044986a4f4dSJason Wang 
1045986a4f4dSJason Wang 	return 0;
1046986a4f4dSJason Wang }
1047986a4f4dSJason Wang 
1048b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq)
1049296f96fcSRusty Russell {
1050296f96fcSRusty Russell 	struct sk_buff *skb;
10516ee57bccSMichael S. Tsirkin 	unsigned int len;
1052e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
105358472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
105461845d20SJason Wang 	unsigned int packets = 0;
105561845d20SJason Wang 	unsigned int bytes = 0;
1056296f96fcSRusty Russell 
1057e9d7417bSJason Wang 	while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1058296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
10593fa2a1dfSstephen hemminger 
106061845d20SJason Wang 		bytes += skb->len;
106161845d20SJason Wang 		packets++;
10623fa2a1dfSstephen hemminger 
1063ed79bab8SEric Dumazet 		dev_kfree_skb_any(skb);
1064296f96fcSRusty Russell 	}
106561845d20SJason Wang 
106661845d20SJason Wang 	/* Avoid overhead when no packets have been processed
106761845d20SJason Wang 	 * happens when called speculatively from start_xmit.
106861845d20SJason Wang 	 */
106961845d20SJason Wang 	if (!packets)
107061845d20SJason Wang 		return;
107161845d20SJason Wang 
107261845d20SJason Wang 	u64_stats_update_begin(&stats->tx_syncp);
107361845d20SJason Wang 	stats->tx_bytes += bytes;
107461845d20SJason Wang 	stats->tx_packets += packets;
107561845d20SJason Wang 	u64_stats_update_end(&stats->tx_syncp);
1076296f96fcSRusty Russell }
1077296f96fcSRusty Russell 
1078e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1079296f96fcSRusty Russell {
1080012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
1081296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1082e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
10837bedc7dcSMichael S. Tsirkin 	unsigned num_sg;
1084012873d0SMichael S. Tsirkin 	unsigned hdr_len = vi->hdr_len;
1085e7428e95SMichael S. Tsirkin 	bool can_push;
1086296f96fcSRusty Russell 
1087e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1088e7428e95SMichael S. Tsirkin 
1089e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
1090e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1091e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1092e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
1093e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
1094e7428e95SMichael S. Tsirkin 	if (can_push)
1095012873d0SMichael S. Tsirkin 		hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1096e7428e95SMichael S. Tsirkin 	else
1097e7428e95SMichael S. Tsirkin 		hdr = skb_vnet_hdr(skb);
1098296f96fcSRusty Russell 
1099e858fae2SMike Rapoport 	if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
11006391a448SJason Wang 				    virtio_is_little_endian(vi->vdev), false))
1101296f96fcSRusty Russell 		BUG();
1102296f96fcSRusty Russell 
1103e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
1104012873d0SMichael S. Tsirkin 		hdr->num_buffers = 0;
11053f2c31d9SMark McLoughlin 
1106547c890cSJason Wang 	sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1107e7428e95SMichael S. Tsirkin 	if (can_push) {
1108e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
1109e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1110e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
1111e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
1112e7428e95SMichael S. Tsirkin 	} else {
1113e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
1114b7dfde95SLinus Torvalds 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1115e7428e95SMichael S. Tsirkin 	}
11169dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
111711a3a154SRusty Russell }
111811a3a154SRusty Russell 
1119424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
112099ffc696SRusty Russell {
112199ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1122986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
1123986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
11249ed4cb07SRusty Russell 	int err;
11254b7fd2e6SMichael S. Tsirkin 	struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
11264b7fd2e6SMichael S. Tsirkin 	bool kick = !skb->xmit_more;
11272cb9c6baSRusty Russell 
11282cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
1129e9d7417bSJason Wang 	free_old_xmit_skbs(sq);
113099ffc696SRusty Russell 
1131074c3582SJacob Keller 	/* timestamp packet in software */
1132074c3582SJacob Keller 	skb_tx_timestamp(skb);
1133074c3582SJacob Keller 
113403f191baSMichael S. Tsirkin 	/* Try to transmit */
1135b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
113699ffc696SRusty Russell 
11379ed4cb07SRusty Russell 	/* This should not happen! */
1138681daee2SJason Wang 	if (unlikely(err)) {
113958eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
11402e57b79cSRick Jones 		if (net_ratelimit())
114158eba97dSRusty Russell 			dev_warn(&dev->dev,
1142b7dfde95SLinus Torvalds 				 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
114358eba97dSRusty Russell 		dev->stats.tx_dropped++;
114485e94525SEric W. Biederman 		dev_kfree_skb_any(skb);
114558eba97dSRusty Russell 		return NETDEV_TX_OK;
1146296f96fcSRusty Russell 	}
114703f191baSMichael S. Tsirkin 
114848925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
114948925e37SRusty Russell 	skb_orphan(skb);
115048925e37SRusty Russell 	nf_reset(skb);
115148925e37SRusty Russell 
115260302ff6SMichael S. Tsirkin 	/* If running out of space, stop queue to avoid getting packets that we
115360302ff6SMichael S. Tsirkin 	 * are then unable to transmit.
115460302ff6SMichael S. Tsirkin 	 * An alternative would be to force queuing layer to requeue the skb by
115560302ff6SMichael S. Tsirkin 	 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
115660302ff6SMichael S. Tsirkin 	 * returned in a normal path of operation: it means that driver is not
115760302ff6SMichael S. Tsirkin 	 * maintaining the TX queue stop/start state properly, and causes
115860302ff6SMichael S. Tsirkin 	 * the stack to do a non-trivial amount of useless work.
115960302ff6SMichael S. Tsirkin 	 * Since most packets only take 1 or 2 ring slots, stopping the queue
116060302ff6SMichael S. Tsirkin 	 * early means 16 slots are typically wasted.
1161d631b94eSstephen hemminger 	 */
1162b7dfde95SLinus Torvalds 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1163986a4f4dSJason Wang 		netif_stop_subqueue(dev, qnum);
1164e9d7417bSJason Wang 		if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
116548925e37SRusty Russell 			/* More just got used, free them then recheck. */
1166b7dfde95SLinus Torvalds 			free_old_xmit_skbs(sq);
1167b7dfde95SLinus Torvalds 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1168986a4f4dSJason Wang 				netif_start_subqueue(dev, qnum);
1169e9d7417bSJason Wang 				virtqueue_disable_cb(sq->vq);
117048925e37SRusty Russell 			}
117148925e37SRusty Russell 		}
117248925e37SRusty Russell 	}
117348925e37SRusty Russell 
11744b7fd2e6SMichael S. Tsirkin 	if (kick || netif_xmit_stopped(txq))
1175c223a078SDavid S. Miller 		virtqueue_kick(sq->vq);
11760b725a2cSDavid S. Miller 
11770b725a2cSDavid S. Miller 	return NETDEV_TX_OK;
1178c223a078SDavid S. Miller }
1179c223a078SDavid S. Miller 
118040cbfc37SAmos Kong /*
118140cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
118240cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
1183788a8b6dSstephen hemminger  * never fail unless improperly formatted.
118440cbfc37SAmos Kong  */
118540cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1186d24bae32Sstephen hemminger 				 struct scatterlist *out)
118740cbfc37SAmos Kong {
1188f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
1189d24bae32Sstephen hemminger 	unsigned out_num = 0, tmp;
119040cbfc37SAmos Kong 
119140cbfc37SAmos Kong 	/* Caller should know better */
1192f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
119340cbfc37SAmos Kong 
11942ac46030SMichael S. Tsirkin 	vi->ctrl_status = ~0;
11952ac46030SMichael S. Tsirkin 	vi->ctrl_hdr.class = class;
11962ac46030SMichael S. Tsirkin 	vi->ctrl_hdr.cmd = cmd;
1197f7bc9594SRusty Russell 	/* Add header */
11982ac46030SMichael S. Tsirkin 	sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
1199f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
120040cbfc37SAmos Kong 
1201f7bc9594SRusty Russell 	if (out)
1202f7bc9594SRusty Russell 		sgs[out_num++] = out;
120340cbfc37SAmos Kong 
1204f7bc9594SRusty Russell 	/* Add return status. */
12052ac46030SMichael S. Tsirkin 	sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
1206d24bae32Sstephen hemminger 	sgs[out_num] = &stat;
120740cbfc37SAmos Kong 
1208d24bae32Sstephen hemminger 	BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1209a7c58146SRusty Russell 	virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
121040cbfc37SAmos Kong 
121167975901SHeinz Graalfs 	if (unlikely(!virtqueue_kick(vi->cvq)))
12122ac46030SMichael S. Tsirkin 		return vi->ctrl_status == VIRTIO_NET_OK;
121340cbfc37SAmos Kong 
121440cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
121540cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
121640cbfc37SAmos Kong 	 */
1217047b9b94SHeinz Graalfs 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1218047b9b94SHeinz Graalfs 	       !virtqueue_is_broken(vi->cvq))
121940cbfc37SAmos Kong 		cpu_relax();
122040cbfc37SAmos Kong 
12212ac46030SMichael S. Tsirkin 	return vi->ctrl_status == VIRTIO_NET_OK;
122240cbfc37SAmos Kong }
122340cbfc37SAmos Kong 
12249c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
12259c46f6d4SAlex Williamson {
12269c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
12279c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
1228f2f2c8b4SJiri Pirko 	int ret;
1229e37e2ff3SAndy Lutomirski 	struct sockaddr *addr;
12307e58d5aeSAmos Kong 	struct scatterlist sg;
12319c46f6d4SAlex Williamson 
1232801822d1SShyam Saini 	addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
1233e37e2ff3SAndy Lutomirski 	if (!addr)
1234e37e2ff3SAndy Lutomirski 		return -ENOMEM;
1235e37e2ff3SAndy Lutomirski 
1236e37e2ff3SAndy Lutomirski 	ret = eth_prepare_mac_addr_change(dev, addr);
1237f2f2c8b4SJiri Pirko 	if (ret)
1238e37e2ff3SAndy Lutomirski 		goto out;
12399c46f6d4SAlex Williamson 
12407e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
12417e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
12427e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1243d24bae32Sstephen hemminger 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
12447e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
12457e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
1246e37e2ff3SAndy Lutomirski 			ret = -EINVAL;
1247e37e2ff3SAndy Lutomirski 			goto out;
12487e58d5aeSAmos Kong 		}
12497e93a02fSMichael S. Tsirkin 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
12507e93a02fSMichael S. Tsirkin 		   !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1251855e0c52SRusty Russell 		unsigned int i;
1252855e0c52SRusty Russell 
1253855e0c52SRusty Russell 		/* Naturally, this has an atomicity problem. */
1254855e0c52SRusty Russell 		for (i = 0; i < dev->addr_len; i++)
1255855e0c52SRusty Russell 			virtio_cwrite8(vdev,
1256855e0c52SRusty Russell 				       offsetof(struct virtio_net_config, mac) +
1257855e0c52SRusty Russell 				       i, addr->sa_data[i]);
12587e58d5aeSAmos Kong 	}
12597e58d5aeSAmos Kong 
12607e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
1261e37e2ff3SAndy Lutomirski 	ret = 0;
12629c46f6d4SAlex Williamson 
1263e37e2ff3SAndy Lutomirski out:
1264e37e2ff3SAndy Lutomirski 	kfree(addr);
1265e37e2ff3SAndy Lutomirski 	return ret;
12669c46f6d4SAlex Williamson }
12679c46f6d4SAlex Williamson 
1268bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev,
12693fa2a1dfSstephen hemminger 			  struct rtnl_link_stats64 *tot)
12703fa2a1dfSstephen hemminger {
12713fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
12723fa2a1dfSstephen hemminger 	int cpu;
12733fa2a1dfSstephen hemminger 	unsigned int start;
12743fa2a1dfSstephen hemminger 
12753fa2a1dfSstephen hemminger 	for_each_possible_cpu(cpu) {
127658472a76SEric Dumazet 		struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
12773fa2a1dfSstephen hemminger 		u64 tpackets, tbytes, rpackets, rbytes;
12783fa2a1dfSstephen hemminger 
12793fa2a1dfSstephen hemminger 		do {
128057a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
12813fa2a1dfSstephen hemminger 			tpackets = stats->tx_packets;
12823fa2a1dfSstephen hemminger 			tbytes   = stats->tx_bytes;
128357a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
128483a27052SEric Dumazet 
128583a27052SEric Dumazet 		do {
128657a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
12873fa2a1dfSstephen hemminger 			rpackets = stats->rx_packets;
12883fa2a1dfSstephen hemminger 			rbytes   = stats->rx_bytes;
128957a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
12903fa2a1dfSstephen hemminger 
12913fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
12923fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
12933fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
12943fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
12953fa2a1dfSstephen hemminger 	}
12963fa2a1dfSstephen hemminger 
12973fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
1298021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
12993fa2a1dfSstephen hemminger 	tot->rx_dropped = dev->stats.rx_dropped;
13003fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
13013fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
13023fa2a1dfSstephen hemminger }
13033fa2a1dfSstephen hemminger 
1304da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
1305da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
1306da74e89dSAmit Shah {
1307da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
1308986a4f4dSJason Wang 	int i;
1309da74e89dSAmit Shah 
1310986a4f4dSJason Wang 	for (i = 0; i < vi->curr_queue_pairs; i++)
1311986a4f4dSJason Wang 		napi_schedule(&vi->rq[i].napi);
1312da74e89dSAmit Shah }
1313da74e89dSAmit Shah #endif
1314da74e89dSAmit Shah 
1315586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
1316586d17c5SJason Wang {
1317586d17c5SJason Wang 	rtnl_lock();
1318586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1319d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1320586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1321586d17c5SJason Wang 	rtnl_unlock();
1322586d17c5SJason Wang }
1323586d17c5SJason Wang 
132447315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1325986a4f4dSJason Wang {
1326986a4f4dSJason Wang 	struct scatterlist sg;
1327986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
1328986a4f4dSJason Wang 
1329986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1330986a4f4dSJason Wang 		return 0;
1331986a4f4dSJason Wang 
1332a725ee3eSAndy Lutomirski 	vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1333a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
1334986a4f4dSJason Wang 
1335986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1336d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1337986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1338986a4f4dSJason Wang 			 queue_pairs);
1339986a4f4dSJason Wang 		return -EINVAL;
134055257d72SSasha Levin 	} else {
1341986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
134235ed159bSJason Wang 		/* virtnet_open() will refill when device is going to up. */
134335ed159bSJason Wang 		if (dev->flags & IFF_UP)
13449b9cd802SJason Wang 			schedule_delayed_work(&vi->refill, 0);
134555257d72SSasha Levin 	}
1346986a4f4dSJason Wang 
1347986a4f4dSJason Wang 	return 0;
1348986a4f4dSJason Wang }
1349986a4f4dSJason Wang 
135047315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
135147315329SJohn Fastabend {
135247315329SJohn Fastabend 	int err;
135347315329SJohn Fastabend 
135447315329SJohn Fastabend 	rtnl_lock();
135547315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
135647315329SJohn Fastabend 	rtnl_unlock();
135747315329SJohn Fastabend 	return err;
135847315329SJohn Fastabend }
135947315329SJohn Fastabend 
1360296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
1361296f96fcSRusty Russell {
1362296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1363986a4f4dSJason Wang 	int i;
1364296f96fcSRusty Russell 
1365b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
1366b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1367986a4f4dSJason Wang 
1368986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++)
1369986a4f4dSJason Wang 		napi_disable(&vi->rq[i].napi);
1370296f96fcSRusty Russell 
1371296f96fcSRusty Russell 	return 0;
1372296f96fcSRusty Russell }
1373296f96fcSRusty Russell 
13742af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
13752af7698eSAlex Williamson {
13762af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
1377f565a7c2SAlex Williamson 	struct scatterlist sg[2];
1378f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
1379ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
138032e7bfc4SJiri Pirko 	int uc_count;
13814cd24eafSJiri Pirko 	int mc_count;
1382f565a7c2SAlex Williamson 	void *buf;
1383f565a7c2SAlex Williamson 	int i;
13842af7698eSAlex Williamson 
1385788a8b6dSstephen hemminger 	/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
13862af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
13872af7698eSAlex Williamson 		return;
13882af7698eSAlex Williamson 
13892ac46030SMichael S. Tsirkin 	vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
13902ac46030SMichael S. Tsirkin 	vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
13912af7698eSAlex Williamson 
13922ac46030SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
13932af7698eSAlex Williamson 
13942af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1395d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_PROMISC, sg))
13962af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
13972ac46030SMichael S. Tsirkin 			 vi->ctrl_promisc ? "en" : "dis");
13982af7698eSAlex Williamson 
13992ac46030SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
14002af7698eSAlex Williamson 
14012af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1402d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
14032af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
14042ac46030SMichael S. Tsirkin 			 vi->ctrl_allmulti ? "en" : "dis");
1405f565a7c2SAlex Williamson 
140632e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
14074cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
1408f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
14094cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1410f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
14114cd24eafSJiri Pirko 	mac_data = buf;
1412e68ed8f0SJoe Perches 	if (!buf)
1413f565a7c2SAlex Williamson 		return;
1414f565a7c2SAlex Williamson 
141523e258e1SAlex Williamson 	sg_init_table(sg, 2);
141623e258e1SAlex Williamson 
1417f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
1418fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
1419ccffad25SJiri Pirko 	i = 0;
142032e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
1421ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1422f565a7c2SAlex Williamson 
1423f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
142432e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1425f565a7c2SAlex Williamson 
1426f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
142732e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
1428f565a7c2SAlex Williamson 
1429fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
1430567ec874SJiri Pirko 	i = 0;
143122bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
143222bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1433f565a7c2SAlex Williamson 
1434f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
14354cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1436f565a7c2SAlex Williamson 
1437f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1438d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
143999e872aeSThomas Huth 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1440f565a7c2SAlex Williamson 
1441f565a7c2SAlex Williamson 	kfree(buf);
14422af7698eSAlex Williamson }
14432af7698eSAlex Williamson 
144480d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
144580d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
14460bde9569SAlex Williamson {
14470bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
14480bde9569SAlex Williamson 	struct scatterlist sg;
14490bde9569SAlex Williamson 
1450a725ee3eSAndy Lutomirski 	vi->ctrl_vid = vid;
1451a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
14520bde9569SAlex Williamson 
14530bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1454d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg))
14550bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
14568e586137SJiri Pirko 	return 0;
14570bde9569SAlex Williamson }
14580bde9569SAlex Williamson 
145980d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
146080d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
14610bde9569SAlex Williamson {
14620bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
14630bde9569SAlex Williamson 	struct scatterlist sg;
14640bde9569SAlex Williamson 
1465a725ee3eSAndy Lutomirski 	vi->ctrl_vid = vid;
1466a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
14670bde9569SAlex Williamson 
14680bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1469d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg))
14700bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
14718e586137SJiri Pirko 	return 0;
14720bde9569SAlex Williamson }
14730bde9569SAlex Williamson 
14748898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1475986a4f4dSJason Wang {
1476986a4f4dSJason Wang 	int i;
14778898c21cSWanlong Gao 
14788898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
14798898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
14808898c21cSWanlong Gao 			virtqueue_set_affinity(vi->rq[i].vq, -1);
14818898c21cSWanlong Gao 			virtqueue_set_affinity(vi->sq[i].vq, -1);
14828898c21cSWanlong Gao 		}
14838898c21cSWanlong Gao 
14848898c21cSWanlong Gao 		vi->affinity_hint_set = false;
14858898c21cSWanlong Gao 	}
14868898c21cSWanlong Gao }
14878898c21cSWanlong Gao 
14888898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
1489986a4f4dSJason Wang {
1490986a4f4dSJason Wang 	int i;
149147be2479SWanlong Gao 	int cpu;
1492986a4f4dSJason Wang 
1493986a4f4dSJason Wang 	/* In multiqueue mode, when the number of cpu is equal to the number of
1494986a4f4dSJason Wang 	 * queue pairs, we let the queue pairs to be private to one cpu by
1495986a4f4dSJason Wang 	 * setting the affinity hint to eliminate the contention.
1496986a4f4dSJason Wang 	 */
14978898c21cSWanlong Gao 	if (vi->curr_queue_pairs == 1 ||
14988898c21cSWanlong Gao 	    vi->max_queue_pairs != num_online_cpus()) {
14998898c21cSWanlong Gao 		virtnet_clean_affinity(vi, -1);
1500986a4f4dSJason Wang 		return;
1501986a4f4dSJason Wang 	}
1502986a4f4dSJason Wang 
150347be2479SWanlong Gao 	i = 0;
150447be2479SWanlong Gao 	for_each_online_cpu(cpu) {
1505986a4f4dSJason Wang 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
1506986a4f4dSJason Wang 		virtqueue_set_affinity(vi->sq[i].vq, cpu);
15079bb8ca86SJason Wang 		netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
150847be2479SWanlong Gao 		i++;
1509986a4f4dSJason Wang 	}
1510986a4f4dSJason Wang 
1511986a4f4dSJason Wang 	vi->affinity_hint_set = true;
151247be2479SWanlong Gao }
1513986a4f4dSJason Wang 
15148017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
15158de4b2f3SWanlong Gao {
15168017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
15178017c279SSebastian Andrzej Siewior 						   node);
15188de4b2f3SWanlong Gao 	virtnet_set_affinity(vi);
15198017c279SSebastian Andrzej Siewior 	return 0;
15208de4b2f3SWanlong Gao }
15213ab098dfSJason Wang 
15228017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
15238017c279SSebastian Andrzej Siewior {
15248017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
15258017c279SSebastian Andrzej Siewior 						   node_dead);
15268017c279SSebastian Andrzej Siewior 	virtnet_set_affinity(vi);
15278017c279SSebastian Andrzej Siewior 	return 0;
15288017c279SSebastian Andrzej Siewior }
15298017c279SSebastian Andrzej Siewior 
15308017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
15318017c279SSebastian Andrzej Siewior {
15328017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
15338017c279SSebastian Andrzej Siewior 						   node);
15348017c279SSebastian Andrzej Siewior 
15358017c279SSebastian Andrzej Siewior 	virtnet_clean_affinity(vi, cpu);
15368017c279SSebastian Andrzej Siewior 	return 0;
15378017c279SSebastian Andrzej Siewior }
15388017c279SSebastian Andrzej Siewior 
15398017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online;
15408017c279SSebastian Andrzej Siewior 
15418017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi)
15428017c279SSebastian Andrzej Siewior {
15438017c279SSebastian Andrzej Siewior 	int ret;
15448017c279SSebastian Andrzej Siewior 
15458017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
15468017c279SSebastian Andrzej Siewior 	if (ret)
15478017c279SSebastian Andrzej Siewior 		return ret;
15488017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
15498017c279SSebastian Andrzej Siewior 					       &vi->node_dead);
15508017c279SSebastian Andrzej Siewior 	if (!ret)
15518017c279SSebastian Andrzej Siewior 		return ret;
15528017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
15538017c279SSebastian Andrzej Siewior 	return ret;
15548017c279SSebastian Andrzej Siewior }
15558017c279SSebastian Andrzej Siewior 
15568017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
15578017c279SSebastian Andrzej Siewior {
15588017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
15598017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
15608017c279SSebastian Andrzej Siewior 					    &vi->node_dead);
1561a9ea3fc6SHerbert Xu }
1562a9ea3fc6SHerbert Xu 
15638f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
15648f9f4668SRick Jones 				struct ethtool_ringparam *ring)
15658f9f4668SRick Jones {
15668f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
15678f9f4668SRick Jones 
1568986a4f4dSJason Wang 	ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1569986a4f4dSJason Wang 	ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
15708f9f4668SRick Jones 	ring->rx_pending = ring->rx_max_pending;
15718f9f4668SRick Jones 	ring->tx_pending = ring->tx_max_pending;
15728f9f4668SRick Jones }
15738f9f4668SRick Jones 
157466846048SRick Jones 
157566846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
157666846048SRick Jones 				struct ethtool_drvinfo *info)
157766846048SRick Jones {
157866846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
157966846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
158066846048SRick Jones 
158166846048SRick Jones 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
158266846048SRick Jones 	strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
158366846048SRick Jones 	strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
158466846048SRick Jones 
158566846048SRick Jones }
158666846048SRick Jones 
1587d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
1588d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
1589d73bcd2cSJason Wang 				struct ethtool_channels *channels)
1590d73bcd2cSJason Wang {
1591d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1592d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
1593d73bcd2cSJason Wang 	int err;
1594d73bcd2cSJason Wang 
1595d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
1596d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
1597d73bcd2cSJason Wang 	 */
1598d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
1599d73bcd2cSJason Wang 		return -EINVAL;
1600d73bcd2cSJason Wang 
1601c18e9cd6SAmos Kong 	if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
1602d73bcd2cSJason Wang 		return -EINVAL;
1603d73bcd2cSJason Wang 
1604f600b690SJohn Fastabend 	/* For now we don't support modifying channels while XDP is loaded
1605f600b690SJohn Fastabend 	 * also when XDP is loaded all RX queues have XDP programs so we only
1606f600b690SJohn Fastabend 	 * need to check a single RX queue.
1607f600b690SJohn Fastabend 	 */
1608f600b690SJohn Fastabend 	if (vi->rq[0].xdp_prog)
1609f600b690SJohn Fastabend 		return -EINVAL;
1610f600b690SJohn Fastabend 
161147be2479SWanlong Gao 	get_online_cpus();
161247315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
1613d73bcd2cSJason Wang 	if (!err) {
1614d73bcd2cSJason Wang 		netif_set_real_num_tx_queues(dev, queue_pairs);
1615d73bcd2cSJason Wang 		netif_set_real_num_rx_queues(dev, queue_pairs);
1616d73bcd2cSJason Wang 
16178898c21cSWanlong Gao 		virtnet_set_affinity(vi);
1618d73bcd2cSJason Wang 	}
161947be2479SWanlong Gao 	put_online_cpus();
1620d73bcd2cSJason Wang 
1621d73bcd2cSJason Wang 	return err;
1622d73bcd2cSJason Wang }
1623d73bcd2cSJason Wang 
1624d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
1625d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
1626d73bcd2cSJason Wang {
1627d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1628d73bcd2cSJason Wang 
1629d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
1630d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
1631d73bcd2cSJason Wang 	channels->max_other = 0;
1632d73bcd2cSJason Wang 	channels->rx_count = 0;
1633d73bcd2cSJason Wang 	channels->tx_count = 0;
1634d73bcd2cSJason Wang 	channels->other_count = 0;
1635d73bcd2cSJason Wang }
1636d73bcd2cSJason Wang 
163716032be5SNikolay Aleksandrov /* Check if the user is trying to change anything besides speed/duplex */
163816032be5SNikolay Aleksandrov static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
163916032be5SNikolay Aleksandrov {
164016032be5SNikolay Aleksandrov 	struct ethtool_cmd diff1 = *cmd;
164116032be5SNikolay Aleksandrov 	struct ethtool_cmd diff2 = {};
164216032be5SNikolay Aleksandrov 
16430cf3ace9SNikolay Aleksandrov 	/* cmd is always set so we need to clear it, validate the port type
16440cf3ace9SNikolay Aleksandrov 	 * and also without autonegotiation we can ignore advertising
16450cf3ace9SNikolay Aleksandrov 	 */
164616032be5SNikolay Aleksandrov 	ethtool_cmd_speed_set(&diff1, 0);
16470cf3ace9SNikolay Aleksandrov 	diff2.port = PORT_OTHER;
164816032be5SNikolay Aleksandrov 	diff1.advertising = 0;
164916032be5SNikolay Aleksandrov 	diff1.duplex = 0;
165016032be5SNikolay Aleksandrov 	diff1.cmd = 0;
165116032be5SNikolay Aleksandrov 
165216032be5SNikolay Aleksandrov 	return !memcmp(&diff1, &diff2, sizeof(diff1));
165316032be5SNikolay Aleksandrov }
165416032be5SNikolay Aleksandrov 
165516032be5SNikolay Aleksandrov static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
165616032be5SNikolay Aleksandrov {
165716032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
165816032be5SNikolay Aleksandrov 	u32 speed;
165916032be5SNikolay Aleksandrov 
166016032be5SNikolay Aleksandrov 	speed = ethtool_cmd_speed(cmd);
166116032be5SNikolay Aleksandrov 	/* don't allow custom speed and duplex */
166216032be5SNikolay Aleksandrov 	if (!ethtool_validate_speed(speed) ||
166316032be5SNikolay Aleksandrov 	    !ethtool_validate_duplex(cmd->duplex) ||
166416032be5SNikolay Aleksandrov 	    !virtnet_validate_ethtool_cmd(cmd))
166516032be5SNikolay Aleksandrov 		return -EINVAL;
166616032be5SNikolay Aleksandrov 	vi->speed = speed;
166716032be5SNikolay Aleksandrov 	vi->duplex = cmd->duplex;
166816032be5SNikolay Aleksandrov 
166916032be5SNikolay Aleksandrov 	return 0;
167016032be5SNikolay Aleksandrov }
167116032be5SNikolay Aleksandrov 
167216032be5SNikolay Aleksandrov static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
167316032be5SNikolay Aleksandrov {
167416032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
167516032be5SNikolay Aleksandrov 
167616032be5SNikolay Aleksandrov 	ethtool_cmd_speed_set(cmd, vi->speed);
167716032be5SNikolay Aleksandrov 	cmd->duplex = vi->duplex;
167816032be5SNikolay Aleksandrov 	cmd->port = PORT_OTHER;
167916032be5SNikolay Aleksandrov 
168016032be5SNikolay Aleksandrov 	return 0;
168116032be5SNikolay Aleksandrov }
168216032be5SNikolay Aleksandrov 
168316032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev)
168416032be5SNikolay Aleksandrov {
168516032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
168616032be5SNikolay Aleksandrov 
168716032be5SNikolay Aleksandrov 	vi->speed = SPEED_UNKNOWN;
168816032be5SNikolay Aleksandrov 	vi->duplex = DUPLEX_UNKNOWN;
168916032be5SNikolay Aleksandrov }
169016032be5SNikolay Aleksandrov 
16910fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
169266846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
16939f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
16948f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
1695d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
1696d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
1697074c3582SJacob Keller 	.get_ts_info = ethtool_op_get_ts_info,
169816032be5SNikolay Aleksandrov 	.get_settings = virtnet_get_settings,
169916032be5SNikolay Aleksandrov 	.set_settings = virtnet_set_settings,
1700a9ea3fc6SHerbert Xu };
1701a9ea3fc6SHerbert Xu 
17029fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev)
17039fe7bfceSJohn Fastabend {
17049fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
17059fe7bfceSJohn Fastabend 	int i;
17069fe7bfceSJohn Fastabend 
17079fe7bfceSJohn Fastabend 	/* Make sure no work handler is accessing the device */
17089fe7bfceSJohn Fastabend 	flush_work(&vi->config_work);
17099fe7bfceSJohn Fastabend 
17109fe7bfceSJohn Fastabend 	netif_device_detach(vi->dev);
17119fe7bfceSJohn Fastabend 	cancel_delayed_work_sync(&vi->refill);
17129fe7bfceSJohn Fastabend 
17139fe7bfceSJohn Fastabend 	if (netif_running(vi->dev)) {
17149fe7bfceSJohn Fastabend 		for (i = 0; i < vi->max_queue_pairs; i++)
17159fe7bfceSJohn Fastabend 			napi_disable(&vi->rq[i].napi);
17169fe7bfceSJohn Fastabend 	}
17179fe7bfceSJohn Fastabend }
17189fe7bfceSJohn Fastabend 
17199fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi);
17202de2f7f4SJohn Fastabend static void _remove_vq_common(struct virtnet_info *vi);
17219fe7bfceSJohn Fastabend 
17229fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev)
17239fe7bfceSJohn Fastabend {
17249fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
17259fe7bfceSJohn Fastabend 	int err, i;
17269fe7bfceSJohn Fastabend 
17279fe7bfceSJohn Fastabend 	err = init_vqs(vi);
17289fe7bfceSJohn Fastabend 	if (err)
17299fe7bfceSJohn Fastabend 		return err;
17309fe7bfceSJohn Fastabend 
17319fe7bfceSJohn Fastabend 	virtio_device_ready(vdev);
17329fe7bfceSJohn Fastabend 
17339fe7bfceSJohn Fastabend 	if (netif_running(vi->dev)) {
17349fe7bfceSJohn Fastabend 		for (i = 0; i < vi->curr_queue_pairs; i++)
17359fe7bfceSJohn Fastabend 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
17369fe7bfceSJohn Fastabend 				schedule_delayed_work(&vi->refill, 0);
17379fe7bfceSJohn Fastabend 
17389fe7bfceSJohn Fastabend 		for (i = 0; i < vi->max_queue_pairs; i++)
17399fe7bfceSJohn Fastabend 			virtnet_napi_enable(&vi->rq[i]);
17409fe7bfceSJohn Fastabend 	}
17419fe7bfceSJohn Fastabend 
17429fe7bfceSJohn Fastabend 	netif_device_attach(vi->dev);
17439fe7bfceSJohn Fastabend 	return err;
17449fe7bfceSJohn Fastabend }
17459fe7bfceSJohn Fastabend 
1746017b29c3SJason Wang static int virtnet_reset(struct virtnet_info *vi, int curr_qp, int xdp_qp)
17472de2f7f4SJohn Fastabend {
17482de2f7f4SJohn Fastabend 	struct virtio_device *dev = vi->vdev;
17492de2f7f4SJohn Fastabend 	int ret;
17502de2f7f4SJohn Fastabend 
17512de2f7f4SJohn Fastabend 	virtio_config_disable(dev);
17522de2f7f4SJohn Fastabend 	dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
17532de2f7f4SJohn Fastabend 	virtnet_freeze_down(dev);
17542de2f7f4SJohn Fastabend 	_remove_vq_common(vi);
17552de2f7f4SJohn Fastabend 
17562de2f7f4SJohn Fastabend 	dev->config->reset(dev);
17572de2f7f4SJohn Fastabend 	virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
17582de2f7f4SJohn Fastabend 	virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
17592de2f7f4SJohn Fastabend 
17602de2f7f4SJohn Fastabend 	ret = virtio_finalize_features(dev);
17612de2f7f4SJohn Fastabend 	if (ret)
17622de2f7f4SJohn Fastabend 		goto err;
17632de2f7f4SJohn Fastabend 
1764017b29c3SJason Wang 	vi->xdp_queue_pairs = xdp_qp;
17652de2f7f4SJohn Fastabend 	ret = virtnet_restore_up(dev);
17662de2f7f4SJohn Fastabend 	if (ret)
17672de2f7f4SJohn Fastabend 		goto err;
1768017b29c3SJason Wang 	ret = _virtnet_set_queues(vi, curr_qp);
17692de2f7f4SJohn Fastabend 	if (ret)
17702de2f7f4SJohn Fastabend 		goto err;
17712de2f7f4SJohn Fastabend 
17722de2f7f4SJohn Fastabend 	virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
17732de2f7f4SJohn Fastabend 	virtio_config_enable(dev);
17742de2f7f4SJohn Fastabend 	return 0;
17752de2f7f4SJohn Fastabend err:
17762de2f7f4SJohn Fastabend 	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
17772de2f7f4SJohn Fastabend 	return ret;
17782de2f7f4SJohn Fastabend }
17792de2f7f4SJohn Fastabend 
1780f600b690SJohn Fastabend static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1781f600b690SJohn Fastabend {
1782f600b690SJohn Fastabend 	unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1783f600b690SJohn Fastabend 	struct virtnet_info *vi = netdev_priv(dev);
1784f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
1785017b29c3SJason Wang 	u16 xdp_qp = 0, curr_qp;
1786672aafd5SJohn Fastabend 	int i, err;
1787f600b690SJohn Fastabend 
1788f600b690SJohn Fastabend 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
178992502fe8SJason Wang 	    virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
179092502fe8SJason Wang 	    virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
179192502fe8SJason Wang 	    virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
1792f600b690SJohn Fastabend 		netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1793f600b690SJohn Fastabend 		return -EOPNOTSUPP;
1794f600b690SJohn Fastabend 	}
1795f600b690SJohn Fastabend 
1796f600b690SJohn Fastabend 	if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1797f600b690SJohn Fastabend 		netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1798f600b690SJohn Fastabend 		return -EINVAL;
1799f600b690SJohn Fastabend 	}
1800f600b690SJohn Fastabend 
1801f600b690SJohn Fastabend 	if (dev->mtu > max_sz) {
1802f600b690SJohn Fastabend 		netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1803f600b690SJohn Fastabend 		return -EINVAL;
1804f600b690SJohn Fastabend 	}
1805f600b690SJohn Fastabend 
1806672aafd5SJohn Fastabend 	curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1807672aafd5SJohn Fastabend 	if (prog)
1808672aafd5SJohn Fastabend 		xdp_qp = nr_cpu_ids;
1809672aafd5SJohn Fastabend 
1810672aafd5SJohn Fastabend 	/* XDP requires extra queues for XDP_TX */
1811672aafd5SJohn Fastabend 	if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1812672aafd5SJohn Fastabend 		netdev_warn(dev, "request %i queues but max is %i\n",
1813672aafd5SJohn Fastabend 			    curr_qp + xdp_qp, vi->max_queue_pairs);
1814672aafd5SJohn Fastabend 		return -ENOMEM;
1815672aafd5SJohn Fastabend 	}
1816672aafd5SJohn Fastabend 
18172de2f7f4SJohn Fastabend 	if (prog) {
18182de2f7f4SJohn Fastabend 		prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
18192de2f7f4SJohn Fastabend 		if (IS_ERR(prog))
18202de2f7f4SJohn Fastabend 			return PTR_ERR(prog);
18212de2f7f4SJohn Fastabend 	}
18222de2f7f4SJohn Fastabend 
18232de2f7f4SJohn Fastabend 	/* Changing the headroom in buffers is a disruptive operation because
18242de2f7f4SJohn Fastabend 	 * existing buffers must be flushed and reallocated. This will happen
18252de2f7f4SJohn Fastabend 	 * when a xdp program is initially added or xdp is disabled by removing
18262de2f7f4SJohn Fastabend 	 * the xdp program resulting in number of XDP queues changing.
18272de2f7f4SJohn Fastabend 	 */
18282de2f7f4SJohn Fastabend 	if (vi->xdp_queue_pairs != xdp_qp) {
1829017b29c3SJason Wang 		err = virtnet_reset(vi, curr_qp + xdp_qp, xdp_qp);
1830017b29c3SJason Wang 		if (err) {
1831017b29c3SJason Wang 			dev_warn(&dev->dev, "XDP reset failure.\n");
18322de2f7f4SJohn Fastabend 			goto virtio_reset_err;
18332de2f7f4SJohn Fastabend 		}
1834017b29c3SJason Wang 	}
18352de2f7f4SJohn Fastabend 
1836672aafd5SJohn Fastabend 	netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1837f600b690SJohn Fastabend 
1838f600b690SJohn Fastabend 	for (i = 0; i < vi->max_queue_pairs; i++) {
1839f600b690SJohn Fastabend 		old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1840f600b690SJohn Fastabend 		rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1841f600b690SJohn Fastabend 		if (old_prog)
1842f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
1843f600b690SJohn Fastabend 	}
1844f600b690SJohn Fastabend 
1845f600b690SJohn Fastabend 	return 0;
18462de2f7f4SJohn Fastabend 
18472de2f7f4SJohn Fastabend virtio_reset_err:
18482de2f7f4SJohn Fastabend 	/* On reset error do our best to unwind XDP changes inflight and return
18492de2f7f4SJohn Fastabend 	 * error up to user space for resolution. The underlying reset hung on
18502de2f7f4SJohn Fastabend 	 * us so not much we can do here.
18512de2f7f4SJohn Fastabend 	 */
18522de2f7f4SJohn Fastabend 	if (prog)
18532de2f7f4SJohn Fastabend 		bpf_prog_sub(prog, vi->max_queue_pairs - 1);
18542de2f7f4SJohn Fastabend 	return err;
1855f600b690SJohn Fastabend }
1856f600b690SJohn Fastabend 
1857f600b690SJohn Fastabend static bool virtnet_xdp_query(struct net_device *dev)
1858f600b690SJohn Fastabend {
1859f600b690SJohn Fastabend 	struct virtnet_info *vi = netdev_priv(dev);
1860f600b690SJohn Fastabend 	int i;
1861f600b690SJohn Fastabend 
1862f600b690SJohn Fastabend 	for (i = 0; i < vi->max_queue_pairs; i++) {
1863f600b690SJohn Fastabend 		if (vi->rq[i].xdp_prog)
1864f600b690SJohn Fastabend 			return true;
1865f600b690SJohn Fastabend 	}
1866f600b690SJohn Fastabend 	return false;
1867f600b690SJohn Fastabend }
1868f600b690SJohn Fastabend 
1869f600b690SJohn Fastabend static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1870f600b690SJohn Fastabend {
1871f600b690SJohn Fastabend 	switch (xdp->command) {
1872f600b690SJohn Fastabend 	case XDP_SETUP_PROG:
1873f600b690SJohn Fastabend 		return virtnet_xdp_set(dev, xdp->prog);
1874f600b690SJohn Fastabend 	case XDP_QUERY_PROG:
1875f600b690SJohn Fastabend 		xdp->prog_attached = virtnet_xdp_query(dev);
1876f600b690SJohn Fastabend 		return 0;
1877f600b690SJohn Fastabend 	default:
1878f600b690SJohn Fastabend 		return -EINVAL;
1879f600b690SJohn Fastabend 	}
1880f600b690SJohn Fastabend }
1881f600b690SJohn Fastabend 
188276288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
188376288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
188476288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
188576288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
188676288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
18879c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
18882af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
18893fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
18901824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
18911824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
189276288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER
189376288b4eSStephen Hemminger 	.ndo_poll_controller = virtnet_netpoll,
189476288b4eSStephen Hemminger #endif
1895f600b690SJohn Fastabend 	.ndo_xdp		= virtnet_xdp,
189676288b4eSStephen Hemminger };
189776288b4eSStephen Hemminger 
1898586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
18999f4d26d0SMark McLoughlin {
1900586d17c5SJason Wang 	struct virtnet_info *vi =
1901586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
19029f4d26d0SMark McLoughlin 	u16 v;
19039f4d26d0SMark McLoughlin 
1904855e0c52SRusty Russell 	if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1905855e0c52SRusty Russell 				 struct virtio_net_config, status, &v) < 0)
1906507613bfSMichael S. Tsirkin 		return;
1907586d17c5SJason Wang 
1908586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
1909ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
1910586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
1911586d17c5SJason Wang 	}
19129f4d26d0SMark McLoughlin 
19139f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
19149f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
19159f4d26d0SMark McLoughlin 
19169f4d26d0SMark McLoughlin 	if (vi->status == v)
1917507613bfSMichael S. Tsirkin 		return;
19189f4d26d0SMark McLoughlin 
19199f4d26d0SMark McLoughlin 	vi->status = v;
19209f4d26d0SMark McLoughlin 
19219f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
19229f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
1923986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
19249f4d26d0SMark McLoughlin 	} else {
19259f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
1926986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
19279f4d26d0SMark McLoughlin 	}
19289f4d26d0SMark McLoughlin }
19299f4d26d0SMark McLoughlin 
19309f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
19319f4d26d0SMark McLoughlin {
19329f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
19339f4d26d0SMark McLoughlin 
19343b07e9caSTejun Heo 	schedule_work(&vi->config_work);
19359f4d26d0SMark McLoughlin }
19369f4d26d0SMark McLoughlin 
1937986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
1938986a4f4dSJason Wang {
1939d4fb84eeSAndrey Vagin 	int i;
1940d4fb84eeSAndrey Vagin 
1941ab3971b1SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1942ab3971b1SJason Wang 		napi_hash_del(&vi->rq[i].napi);
1943d4fb84eeSAndrey Vagin 		netif_napi_del(&vi->rq[i].napi);
1944ab3971b1SJason Wang 	}
1945d4fb84eeSAndrey Vagin 
1946963abe5cSEric Dumazet 	/* We called napi_hash_del() before netif_napi_del(),
1947963abe5cSEric Dumazet 	 * we need to respect an RCU grace period before freeing vi->rq
1948963abe5cSEric Dumazet 	 */
1949963abe5cSEric Dumazet 	synchronize_net();
1950963abe5cSEric Dumazet 
1951986a4f4dSJason Wang 	kfree(vi->rq);
1952986a4f4dSJason Wang 	kfree(vi->sq);
1953986a4f4dSJason Wang }
1954986a4f4dSJason Wang 
195547315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi)
1956986a4f4dSJason Wang {
1957f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
1958986a4f4dSJason Wang 	int i;
1959986a4f4dSJason Wang 
1960986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1961986a4f4dSJason Wang 		while (vi->rq[i].pages)
1962986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1963f600b690SJohn Fastabend 
1964f600b690SJohn Fastabend 		old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1965f600b690SJohn Fastabend 		RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1966f600b690SJohn Fastabend 		if (old_prog)
1967f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
1968986a4f4dSJason Wang 	}
196947315329SJohn Fastabend }
197047315329SJohn Fastabend 
197147315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi)
197247315329SJohn Fastabend {
197347315329SJohn Fastabend 	rtnl_lock();
197447315329SJohn Fastabend 	_free_receive_bufs(vi);
1975f600b690SJohn Fastabend 	rtnl_unlock();
1976986a4f4dSJason Wang }
1977986a4f4dSJason Wang 
1978fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi)
1979fb51879dSMichael Dalton {
1980fb51879dSMichael Dalton 	int i;
1981fb51879dSMichael Dalton 	for (i = 0; i < vi->max_queue_pairs; i++)
1982fb51879dSMichael Dalton 		if (vi->rq[i].alloc_frag.page)
1983fb51879dSMichael Dalton 			put_page(vi->rq[i].alloc_frag.page);
1984fb51879dSMichael Dalton }
1985fb51879dSMichael Dalton 
1986b68df015SJohn Fastabend static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
198756434a01SJohn Fastabend {
198856434a01SJohn Fastabend 	if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
198956434a01SJohn Fastabend 		return false;
199056434a01SJohn Fastabend 	else if (q < vi->curr_queue_pairs)
199156434a01SJohn Fastabend 		return true;
199256434a01SJohn Fastabend 	else
199356434a01SJohn Fastabend 		return false;
199456434a01SJohn Fastabend }
199556434a01SJohn Fastabend 
1996986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
1997986a4f4dSJason Wang {
1998986a4f4dSJason Wang 	void *buf;
1999986a4f4dSJason Wang 	int i;
2000986a4f4dSJason Wang 
2001986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2002986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
200356434a01SJohn Fastabend 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2004b68df015SJohn Fastabend 			if (!is_xdp_raw_buffer_queue(vi, i))
2005986a4f4dSJason Wang 				dev_kfree_skb(buf);
200656434a01SJohn Fastabend 			else
200756434a01SJohn Fastabend 				put_page(virt_to_head_page(buf));
200856434a01SJohn Fastabend 		}
2009986a4f4dSJason Wang 	}
2010986a4f4dSJason Wang 
2011986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2012986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
2013986a4f4dSJason Wang 
2014986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2015ab7db917SMichael Dalton 			if (vi->mergeable_rx_bufs) {
2016*680557cfSMichael S. Tsirkin 				put_page(virt_to_head_page(buf));
2017ab7db917SMichael Dalton 			} else if (vi->big_packets) {
2018fa9fac17SAndrey Vagin 				give_pages(&vi->rq[i], buf);
2019ab7db917SMichael Dalton 			} else {
2020f6b10209SJason Wang 				put_page(virt_to_head_page(buf));
2021986a4f4dSJason Wang 			}
2022986a4f4dSJason Wang 		}
2023986a4f4dSJason Wang 	}
2024ab7db917SMichael Dalton }
2025986a4f4dSJason Wang 
2026e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
2027e9d7417bSJason Wang {
2028e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
2029e9d7417bSJason Wang 
20308898c21cSWanlong Gao 	virtnet_clean_affinity(vi, -1);
2031986a4f4dSJason Wang 
2032e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
2033986a4f4dSJason Wang 
2034986a4f4dSJason Wang 	virtnet_free_queues(vi);
2035986a4f4dSJason Wang }
2036986a4f4dSJason Wang 
2037986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
2038986a4f4dSJason Wang {
2039986a4f4dSJason Wang 	vq_callback_t **callbacks;
2040986a4f4dSJason Wang 	struct virtqueue **vqs;
2041986a4f4dSJason Wang 	int ret = -ENOMEM;
2042986a4f4dSJason Wang 	int i, total_vqs;
2043986a4f4dSJason Wang 	const char **names;
2044d45b897bSMichael S. Tsirkin 	bool *ctx;
2045986a4f4dSJason Wang 
2046986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2047986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2048986a4f4dSJason Wang 	 * possible control vq.
2049986a4f4dSJason Wang 	 */
2050986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
2051986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2052986a4f4dSJason Wang 
2053986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
2054986a4f4dSJason Wang 	vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
2055986a4f4dSJason Wang 	if (!vqs)
2056986a4f4dSJason Wang 		goto err_vq;
2057986a4f4dSJason Wang 	callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
2058986a4f4dSJason Wang 	if (!callbacks)
2059986a4f4dSJason Wang 		goto err_callback;
2060986a4f4dSJason Wang 	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
2061986a4f4dSJason Wang 	if (!names)
2062986a4f4dSJason Wang 		goto err_names;
2063d45b897bSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs) {
2064d45b897bSMichael S. Tsirkin 		ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL);
2065d45b897bSMichael S. Tsirkin 		if (!ctx)
2066d45b897bSMichael S. Tsirkin 			goto err_ctx;
2067d45b897bSMichael S. Tsirkin 	} else {
2068d45b897bSMichael S. Tsirkin 		ctx = NULL;
2069d45b897bSMichael S. Tsirkin 	}
2070986a4f4dSJason Wang 
2071986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
2072986a4f4dSJason Wang 	if (vi->has_cvq) {
2073986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
2074986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
2075986a4f4dSJason Wang 	}
2076986a4f4dSJason Wang 
2077986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
2078986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2079986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
2080986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
2081986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
2082986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
2083986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
2084986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
2085d45b897bSMichael S. Tsirkin 		if (ctx)
2086d45b897bSMichael S. Tsirkin 			ctx[rxq2vq(i)] = true;
2087986a4f4dSJason Wang 	}
2088986a4f4dSJason Wang 
2089d45b897bSMichael S. Tsirkin 	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
2090d45b897bSMichael S. Tsirkin 					 names, ctx, NULL);
2091986a4f4dSJason Wang 	if (ret)
2092986a4f4dSJason Wang 		goto err_find;
2093986a4f4dSJason Wang 
2094986a4f4dSJason Wang 	if (vi->has_cvq) {
2095986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
2096986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
2097f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2098986a4f4dSJason Wang 	}
2099986a4f4dSJason Wang 
2100986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2101986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
2102986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
2103986a4f4dSJason Wang 	}
2104986a4f4dSJason Wang 
2105986a4f4dSJason Wang 	kfree(names);
2106986a4f4dSJason Wang 	kfree(callbacks);
2107986a4f4dSJason Wang 	kfree(vqs);
2108986a4f4dSJason Wang 
2109986a4f4dSJason Wang 	return 0;
2110986a4f4dSJason Wang 
2111986a4f4dSJason Wang err_find:
2112d45b897bSMichael S. Tsirkin 	kfree(ctx);
2113d45b897bSMichael S. Tsirkin err_ctx:
2114986a4f4dSJason Wang 	kfree(names);
2115986a4f4dSJason Wang err_names:
2116986a4f4dSJason Wang 	kfree(callbacks);
2117986a4f4dSJason Wang err_callback:
2118986a4f4dSJason Wang 	kfree(vqs);
2119986a4f4dSJason Wang err_vq:
2120986a4f4dSJason Wang 	return ret;
2121986a4f4dSJason Wang }
2122986a4f4dSJason Wang 
2123986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
2124986a4f4dSJason Wang {
2125986a4f4dSJason Wang 	int i;
2126986a4f4dSJason Wang 
2127986a4f4dSJason Wang 	vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2128986a4f4dSJason Wang 	if (!vi->sq)
2129986a4f4dSJason Wang 		goto err_sq;
2130986a4f4dSJason Wang 	vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
2131008d4278SAmerigo Wang 	if (!vi->rq)
2132986a4f4dSJason Wang 		goto err_rq;
2133986a4f4dSJason Wang 
2134986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
2135986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2136986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
2137986a4f4dSJason Wang 		netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2138986a4f4dSJason Wang 			       napi_weight);
2139986a4f4dSJason Wang 
2140986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
21415377d758SJohannes Berg 		ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
2142986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2143986a4f4dSJason Wang 	}
2144986a4f4dSJason Wang 
2145986a4f4dSJason Wang 	return 0;
2146986a4f4dSJason Wang 
2147986a4f4dSJason Wang err_rq:
2148986a4f4dSJason Wang 	kfree(vi->sq);
2149986a4f4dSJason Wang err_sq:
2150986a4f4dSJason Wang 	return -ENOMEM;
2151e9d7417bSJason Wang }
2152e9d7417bSJason Wang 
21533f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
21543f9c10b0SAmit Shah {
2155986a4f4dSJason Wang 	int ret;
21563f9c10b0SAmit Shah 
2157986a4f4dSJason Wang 	/* Allocate send & receive queues */
2158986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
2159986a4f4dSJason Wang 	if (ret)
2160986a4f4dSJason Wang 		goto err;
21613f9c10b0SAmit Shah 
2162986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
2163986a4f4dSJason Wang 	if (ret)
2164986a4f4dSJason Wang 		goto err_free;
21653f9c10b0SAmit Shah 
216647be2479SWanlong Gao 	get_online_cpus();
21678898c21cSWanlong Gao 	virtnet_set_affinity(vi);
216847be2479SWanlong Gao 	put_online_cpus();
216947be2479SWanlong Gao 
21703f9c10b0SAmit Shah 	return 0;
2171986a4f4dSJason Wang 
2172986a4f4dSJason Wang err_free:
2173986a4f4dSJason Wang 	virtnet_free_queues(vi);
2174986a4f4dSJason Wang err:
2175986a4f4dSJason Wang 	return ret;
21763f9c10b0SAmit Shah }
21773f9c10b0SAmit Shah 
2178fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
2179fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2180fbf28d78SMichael Dalton 		struct rx_queue_attribute *attribute, char *buf)
2181fbf28d78SMichael Dalton {
2182fbf28d78SMichael Dalton 	struct virtnet_info *vi = netdev_priv(queue->dev);
2183fbf28d78SMichael Dalton 	unsigned int queue_index = get_netdev_rx_queue_index(queue);
21845377d758SJohannes Berg 	struct ewma_pkt_len *avg;
2185fbf28d78SMichael Dalton 
2186fbf28d78SMichael Dalton 	BUG_ON(queue_index >= vi->max_queue_pairs);
2187fbf28d78SMichael Dalton 	avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2188fbf28d78SMichael Dalton 	return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2189fbf28d78SMichael Dalton }
2190fbf28d78SMichael Dalton 
2191fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2192fbf28d78SMichael Dalton 	__ATTR_RO(mergeable_rx_buffer_size);
2193fbf28d78SMichael Dalton 
2194fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = {
2195fbf28d78SMichael Dalton 	&mergeable_rx_buffer_size_attribute.attr,
2196fbf28d78SMichael Dalton 	NULL
2197fbf28d78SMichael Dalton };
2198fbf28d78SMichael Dalton 
2199fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = {
2200fbf28d78SMichael Dalton 	.name = "virtio_net",
2201fbf28d78SMichael Dalton 	.attrs = virtio_net_mrg_rx_attrs
2202fbf28d78SMichael Dalton };
2203fbf28d78SMichael Dalton #endif
2204fbf28d78SMichael Dalton 
2205892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2206892d6eb1SJason Wang 				    unsigned int fbit,
2207892d6eb1SJason Wang 				    const char *fname, const char *dname)
2208892d6eb1SJason Wang {
2209892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, fbit))
2210892d6eb1SJason Wang 		return false;
2211892d6eb1SJason Wang 
2212892d6eb1SJason Wang 	dev_err(&vdev->dev, "device advertises feature %s but not %s",
2213892d6eb1SJason Wang 		fname, dname);
2214892d6eb1SJason Wang 
2215892d6eb1SJason Wang 	return true;
2216892d6eb1SJason Wang }
2217892d6eb1SJason Wang 
2218892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit)			\
2219892d6eb1SJason Wang 	virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2220892d6eb1SJason Wang 
2221892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev)
2222892d6eb1SJason Wang {
2223892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2224892d6eb1SJason Wang 	    (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2225892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
2226892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2227892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
2228892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2229892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
2230892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2231892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2232892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ"))) {
2233892d6eb1SJason Wang 		return false;
2234892d6eb1SJason Wang 	}
2235892d6eb1SJason Wang 
2236892d6eb1SJason Wang 	return true;
2237892d6eb1SJason Wang }
2238892d6eb1SJason Wang 
2239d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU
2240d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU
2241d0c2c997SJarod Wilson 
2242fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev)
2243296f96fcSRusty Russell {
22446ba42248SMichael S. Tsirkin 	if (!vdev->config->get) {
22456ba42248SMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
22466ba42248SMichael S. Tsirkin 			__func__);
22476ba42248SMichael S. Tsirkin 		return -EINVAL;
22486ba42248SMichael S. Tsirkin 	}
22496ba42248SMichael S. Tsirkin 
2250892d6eb1SJason Wang 	if (!virtnet_validate_features(vdev))
2251892d6eb1SJason Wang 		return -EINVAL;
2252892d6eb1SJason Wang 
2253fe36cbe0SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2254fe36cbe0SMichael S. Tsirkin 		int mtu = virtio_cread16(vdev,
2255fe36cbe0SMichael S. Tsirkin 					 offsetof(struct virtio_net_config,
2256fe36cbe0SMichael S. Tsirkin 						  mtu));
2257fe36cbe0SMichael S. Tsirkin 		if (mtu < MIN_MTU)
2258fe36cbe0SMichael S. Tsirkin 			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2259fe36cbe0SMichael S. Tsirkin 	}
2260fe36cbe0SMichael S. Tsirkin 
2261fe36cbe0SMichael S. Tsirkin 	return 0;
2262fe36cbe0SMichael S. Tsirkin }
2263fe36cbe0SMichael S. Tsirkin 
2264fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev)
2265fe36cbe0SMichael S. Tsirkin {
2266fe36cbe0SMichael S. Tsirkin 	int i, err;
2267fe36cbe0SMichael S. Tsirkin 	struct net_device *dev;
2268fe36cbe0SMichael S. Tsirkin 	struct virtnet_info *vi;
2269fe36cbe0SMichael S. Tsirkin 	u16 max_queue_pairs;
2270fe36cbe0SMichael S. Tsirkin 	int mtu;
2271fe36cbe0SMichael S. Tsirkin 
2272986a4f4dSJason Wang 	/* Find if host supports multiqueue virtio_net device */
2273855e0c52SRusty Russell 	err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2274855e0c52SRusty Russell 				   struct virtio_net_config,
2275855e0c52SRusty Russell 				   max_virtqueue_pairs, &max_queue_pairs);
2276986a4f4dSJason Wang 
2277986a4f4dSJason Wang 	/* We need at least 2 queue's */
2278986a4f4dSJason Wang 	if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2279986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2280986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2281986a4f4dSJason Wang 		max_queue_pairs = 1;
2282296f96fcSRusty Russell 
2283296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
2284986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
2285296f96fcSRusty Russell 	if (!dev)
2286296f96fcSRusty Russell 		return -ENOMEM;
2287296f96fcSRusty Russell 
2288296f96fcSRusty Russell 	/* Set up network device as normal. */
2289f2f2c8b4SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
229076288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
2291296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
22923fa2a1dfSstephen hemminger 
22937ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &virtnet_ethtool_ops;
2294296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
2295296f96fcSRusty Russell 
2296296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
229798e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
2298296f96fcSRusty Russell 		/* This opens up the world of extra features. */
229948900cb6SJason Wang 		dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
230098e778c9SMichał Mirosław 		if (csum)
230148900cb6SJason Wang 			dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
230298e778c9SMichał Mirosław 
230398e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
2304e3e3c423SVlad Yasevich 			dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
230534a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
230634a48579SRusty Russell 		}
23075539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
230898e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
230998e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
231098e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
231198e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
231298e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
231398e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
2314e3e3c423SVlad Yasevich 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2315e3e3c423SVlad Yasevich 			dev->hw_features |= NETIF_F_UFO;
231698e778c9SMichał Mirosław 
231741f2f127SJason Wang 		dev->features |= NETIF_F_GSO_ROBUST;
231841f2f127SJason Wang 
231998e778c9SMichał Mirosław 		if (gso)
2320e3e3c423SVlad Yasevich 			dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
232198e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
2322296f96fcSRusty Russell 	}
23234f49129bSThomas Huth 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
23244f49129bSThomas Huth 		dev->features |= NETIF_F_RXCSUM;
2325296f96fcSRusty Russell 
23264fda8302SJason Wang 	dev->vlan_features = dev->features;
23274fda8302SJason Wang 
2328d0c2c997SJarod Wilson 	/* MTU range: 68 - 65535 */
2329d0c2c997SJarod Wilson 	dev->min_mtu = MIN_MTU;
2330d0c2c997SJarod Wilson 	dev->max_mtu = MAX_MTU;
2331d0c2c997SJarod Wilson 
2332296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
2333855e0c52SRusty Russell 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2334855e0c52SRusty Russell 		virtio_cread_bytes(vdev,
2335a586d4f6SRusty Russell 				   offsetof(struct virtio_net_config, mac),
2336855e0c52SRusty Russell 				   dev->dev_addr, dev->addr_len);
2337855e0c52SRusty Russell 	else
2338f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
2339296f96fcSRusty Russell 
2340296f96fcSRusty Russell 	/* Set up our device-specific information */
2341296f96fcSRusty Russell 	vi = netdev_priv(dev);
2342296f96fcSRusty Russell 	vi->dev = dev;
2343296f96fcSRusty Russell 	vi->vdev = vdev;
2344d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
23453fa2a1dfSstephen hemminger 	vi->stats = alloc_percpu(struct virtnet_stats);
23463fa2a1dfSstephen hemminger 	err = -ENOMEM;
23473fa2a1dfSstephen hemminger 	if (vi->stats == NULL)
23483fa2a1dfSstephen hemminger 		goto free;
23493fa2a1dfSstephen hemminger 
2350827da44cSJohn Stultz 	for_each_possible_cpu(i) {
2351827da44cSJohn Stultz 		struct virtnet_stats *virtnet_stats;
2352827da44cSJohn Stultz 		virtnet_stats = per_cpu_ptr(vi->stats, i);
2353827da44cSJohn Stultz 		u64_stats_init(&virtnet_stats->tx_syncp);
2354827da44cSJohn Stultz 		u64_stats_init(&virtnet_stats->rx_syncp);
2355827da44cSJohn Stultz 	}
2356827da44cSJohn Stultz 
2357586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
2358296f96fcSRusty Russell 
235997402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
23608e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
23618e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2362e3e3c423SVlad Yasevich 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2363e3e3c423SVlad Yasevich 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
236497402b96SHerbert Xu 		vi->big_packets = true;
236597402b96SHerbert Xu 
23663f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
23673f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
23683f2c31d9SMark McLoughlin 
2369d04302b3SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2370d04302b3SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
2371012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2372012873d0SMichael S. Tsirkin 	else
2373012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr);
2374012873d0SMichael S. Tsirkin 
237575993300SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
237675993300SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
2377e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
2378e7428e95SMichael S. Tsirkin 
2379986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2380986a4f4dSJason Wang 		vi->has_cvq = true;
2381986a4f4dSJason Wang 
238214de9d11SAaron Conole 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
238314de9d11SAaron Conole 		mtu = virtio_cread16(vdev,
238414de9d11SAaron Conole 				     offsetof(struct virtio_net_config,
238514de9d11SAaron Conole 					      mtu));
238693a205eeSAaron Conole 		if (mtu < dev->min_mtu) {
2387fe36cbe0SMichael S. Tsirkin 			/* Should never trigger: MTU was previously validated
2388fe36cbe0SMichael S. Tsirkin 			 * in virtnet_validate.
2389fe36cbe0SMichael S. Tsirkin 			 */
2390fe36cbe0SMichael S. Tsirkin 			dev_err(&vdev->dev, "device MTU appears to have changed "
2391fe36cbe0SMichael S. Tsirkin 				"it is now %d < %d", mtu, dev->min_mtu);
2392fe36cbe0SMichael S. Tsirkin 			goto free_stats;
2393fe36cbe0SMichael S. Tsirkin 		}
2394fe36cbe0SMichael S. Tsirkin 
2395d0c2c997SJarod Wilson 		dev->mtu = mtu;
239693a205eeSAaron Conole 		dev->max_mtu = mtu;
23972e123b44SMichael S. Tsirkin 
23982e123b44SMichael S. Tsirkin 		/* TODO: size buffers correctly in this case. */
23992e123b44SMichael S. Tsirkin 		if (dev->mtu > ETH_DATA_LEN)
24002e123b44SMichael S. Tsirkin 			vi->big_packets = true;
240114de9d11SAaron Conole 	}
240214de9d11SAaron Conole 
2403012873d0SMichael S. Tsirkin 	if (vi->any_header_sg)
2404012873d0SMichael S. Tsirkin 		dev->needed_headroom = vi->hdr_len;
24056ebbc1a6SZhangjie \(HZ\) 
240644900010SJason Wang 	/* Enable multiqueue by default */
240744900010SJason Wang 	if (num_online_cpus() >= max_queue_pairs)
240844900010SJason Wang 		vi->curr_queue_pairs = max_queue_pairs;
240944900010SJason Wang 	else
241044900010SJason Wang 		vi->curr_queue_pairs = num_online_cpus();
2411986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
2412986a4f4dSJason Wang 
2413986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
24143f9c10b0SAmit Shah 	err = init_vqs(vi);
2415d2a7dddaSMichael S. Tsirkin 	if (err)
24169bb8ca86SJason Wang 		goto free_stats;
2417d2a7dddaSMichael S. Tsirkin 
2418fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
2419fbf28d78SMichael Dalton 	if (vi->mergeable_rx_bufs)
2420fbf28d78SMichael Dalton 		dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2421fbf28d78SMichael Dalton #endif
24220f13b66bSZhi Yong Wu 	netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
24230f13b66bSZhi Yong Wu 	netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
2424986a4f4dSJason Wang 
242516032be5SNikolay Aleksandrov 	virtnet_init_settings(dev);
242616032be5SNikolay Aleksandrov 
2427296f96fcSRusty Russell 	err = register_netdev(dev);
2428296f96fcSRusty Russell 	if (err) {
2429296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
2430d2a7dddaSMichael S. Tsirkin 		goto free_vqs;
2431296f96fcSRusty Russell 	}
2432b3369c1fSRusty Russell 
24334baf1e33SMichael S. Tsirkin 	virtio_device_ready(vdev);
24344baf1e33SMichael S. Tsirkin 
24358017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
24368de4b2f3SWanlong Gao 	if (err) {
24378de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
2438f00e35e2Swangyunjian 		goto free_unregister_netdev;
24398de4b2f3SWanlong Gao 	}
24408de4b2f3SWanlong Gao 
2441a220871bSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
244244900010SJason Wang 
2443167c25e4SJason Wang 	/* Assume link up if device can't report link status,
2444167c25e4SJason Wang 	   otherwise get link status from config. */
2445167c25e4SJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2446167c25e4SJason Wang 		netif_carrier_off(dev);
24473b07e9caSTejun Heo 		schedule_work(&vi->config_work);
2448167c25e4SJason Wang 	} else {
2449167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
24504783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
2451167c25e4SJason Wang 	}
24529f4d26d0SMark McLoughlin 
2453986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2454986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
2455986a4f4dSJason Wang 
2456296f96fcSRusty Russell 	return 0;
2457296f96fcSRusty Russell 
2458f00e35e2Swangyunjian free_unregister_netdev:
245902465555SMichael S. Tsirkin 	vi->vdev->config->reset(vdev);
246002465555SMichael S. Tsirkin 
2461b3369c1fSRusty Russell 	unregister_netdev(dev);
2462d2a7dddaSMichael S. Tsirkin free_vqs:
2463986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
2464fb51879dSMichael Dalton 	free_receive_page_frags(vi);
2465e9d7417bSJason Wang 	virtnet_del_vqs(vi);
24663fa2a1dfSstephen hemminger free_stats:
24673fa2a1dfSstephen hemminger 	free_percpu(vi->stats);
2468296f96fcSRusty Russell free:
2469296f96fcSRusty Russell 	free_netdev(dev);
2470296f96fcSRusty Russell 	return err;
2471296f96fcSRusty Russell }
2472296f96fcSRusty Russell 
24732de2f7f4SJohn Fastabend static void _remove_vq_common(struct virtnet_info *vi)
24742de2f7f4SJohn Fastabend {
24752de2f7f4SJohn Fastabend 	vi->vdev->config->reset(vi->vdev);
24762de2f7f4SJohn Fastabend 	free_unused_bufs(vi);
24772de2f7f4SJohn Fastabend 	_free_receive_bufs(vi);
24782de2f7f4SJohn Fastabend 	free_receive_page_frags(vi);
24792de2f7f4SJohn Fastabend 	virtnet_del_vqs(vi);
24802de2f7f4SJohn Fastabend }
24812de2f7f4SJohn Fastabend 
248204486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
2483296f96fcSRusty Russell {
248404486ed0SAmit Shah 	vi->vdev->config->reset(vi->vdev);
2485830a8a97SShirley Ma 
2486830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
24879ab86bbcSShirley Ma 	free_unused_bufs(vi);
2488fb6813f4SRusty Russell 
2489986a4f4dSJason Wang 	free_receive_bufs(vi);
2490d2a7dddaSMichael S. Tsirkin 
2491fb51879dSMichael Dalton 	free_receive_page_frags(vi);
2492fb51879dSMichael Dalton 
2493986a4f4dSJason Wang 	virtnet_del_vqs(vi);
249404486ed0SAmit Shah }
249504486ed0SAmit Shah 
24968cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
249704486ed0SAmit Shah {
249804486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
249904486ed0SAmit Shah 
25008017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
25018de4b2f3SWanlong Gao 
2502102a2786SMichael S. Tsirkin 	/* Make sure no work handler is accessing the device. */
2503102a2786SMichael S. Tsirkin 	flush_work(&vi->config_work);
2504586d17c5SJason Wang 
250504486ed0SAmit Shah 	unregister_netdev(vi->dev);
250604486ed0SAmit Shah 
250704486ed0SAmit Shah 	remove_vq_common(vi);
2508fb6813f4SRusty Russell 
25092e66f55bSKrishna Kumar 	free_percpu(vi->stats);
251074b2553fSRusty Russell 	free_netdev(vi->dev);
2511296f96fcSRusty Russell }
2512296f96fcSRusty Russell 
251389107000SAaron Lu #ifdef CONFIG_PM_SLEEP
25140741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev)
25150741bcb5SAmit Shah {
25160741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
25170741bcb5SAmit Shah 
25188017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
25199fe7bfceSJohn Fastabend 	virtnet_freeze_down(vdev);
25200741bcb5SAmit Shah 	remove_vq_common(vi);
25210741bcb5SAmit Shah 
25220741bcb5SAmit Shah 	return 0;
25230741bcb5SAmit Shah }
25240741bcb5SAmit Shah 
25250741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev)
25260741bcb5SAmit Shah {
25270741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
25289fe7bfceSJohn Fastabend 	int err;
25290741bcb5SAmit Shah 
25309fe7bfceSJohn Fastabend 	err = virtnet_restore_up(vdev);
25310741bcb5SAmit Shah 	if (err)
25320741bcb5SAmit Shah 		return err;
2533986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
2534986a4f4dSJason Wang 
25358017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
2536ec9debbdSJason Wang 	if (err)
2537ec9debbdSJason Wang 		return err;
2538ec9debbdSJason Wang 
25390741bcb5SAmit Shah 	return 0;
25400741bcb5SAmit Shah }
25410741bcb5SAmit Shah #endif
25420741bcb5SAmit Shah 
2543296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
2544296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2545296f96fcSRusty Russell 	{ 0 },
2546296f96fcSRusty Russell };
2547296f96fcSRusty Russell 
2548f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \
2549f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2550f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MAC, \
2551f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2552f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2553f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2554f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2555f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2556f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2557f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_MAC_ADDR, \
2558f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MTU
2559f3358507SMichael S. Tsirkin 
2560c45a6816SRusty Russell static unsigned int features[] = {
2561f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
2562f3358507SMichael S. Tsirkin };
2563f3358507SMichael S. Tsirkin 
2564f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = {
2565f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
2566f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GSO,
2567e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
2568c45a6816SRusty Russell };
2569c45a6816SRusty Russell 
257022402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
2571c45a6816SRusty Russell 	.feature_table = features,
2572c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
2573f3358507SMichael S. Tsirkin 	.feature_table_legacy = features_legacy,
2574f3358507SMichael S. Tsirkin 	.feature_table_size_legacy = ARRAY_SIZE(features_legacy),
2575296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
2576296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
2577296f96fcSRusty Russell 	.id_table =	id_table,
2578fe36cbe0SMichael S. Tsirkin 	.validate =	virtnet_validate,
2579296f96fcSRusty Russell 	.probe =	virtnet_probe,
25808cc085d6SBill Pemberton 	.remove =	virtnet_remove,
25819f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
258289107000SAaron Lu #ifdef CONFIG_PM_SLEEP
25830741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
25840741bcb5SAmit Shah 	.restore =	virtnet_restore,
25850741bcb5SAmit Shah #endif
2586296f96fcSRusty Russell };
2587296f96fcSRusty Russell 
25888017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void)
25898017c279SSebastian Andrzej Siewior {
25908017c279SSebastian Andrzej Siewior 	int ret;
25918017c279SSebastian Andrzej Siewior 
259273c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
25938017c279SSebastian Andrzej Siewior 				      virtnet_cpu_online,
25948017c279SSebastian Andrzej Siewior 				      virtnet_cpu_down_prep);
25958017c279SSebastian Andrzej Siewior 	if (ret < 0)
25968017c279SSebastian Andrzej Siewior 		goto out;
25978017c279SSebastian Andrzej Siewior 	virtionet_online = ret;
259873c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
25998017c279SSebastian Andrzej Siewior 				      NULL, virtnet_cpu_dead);
26008017c279SSebastian Andrzej Siewior 	if (ret)
26018017c279SSebastian Andrzej Siewior 		goto err_dead;
26028017c279SSebastian Andrzej Siewior 
26038017c279SSebastian Andrzej Siewior         ret = register_virtio_driver(&virtio_net_driver);
26048017c279SSebastian Andrzej Siewior 	if (ret)
26058017c279SSebastian Andrzej Siewior 		goto err_virtio;
26068017c279SSebastian Andrzej Siewior 	return 0;
26078017c279SSebastian Andrzej Siewior err_virtio:
26088017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
26098017c279SSebastian Andrzej Siewior err_dead:
26108017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
26118017c279SSebastian Andrzej Siewior out:
26128017c279SSebastian Andrzej Siewior 	return ret;
26138017c279SSebastian Andrzej Siewior }
26148017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init);
26158017c279SSebastian Andrzej Siewior 
26168017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void)
26178017c279SSebastian Andrzej Siewior {
26188017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
26198017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
26208017c279SSebastian Andrzej Siewior 	unregister_virtio_driver(&virtio_net_driver);
26218017c279SSebastian Andrzej Siewior }
26228017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit);
2623296f96fcSRusty Russell 
2624296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
2625296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
2626296f96fcSRusty Russell MODULE_LICENSE("GPL");
2627