1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* A network driver using virtio.
3  *
4  * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
5  */
6 //#define DEBUG
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/ethtool.h>
10 #include <linux/module.h>
11 #include <linux/virtio.h>
12 #include <linux/virtio_net.h>
13 #include <linux/bpf.h>
14 #include <linux/bpf_trace.h>
15 #include <linux/scatterlist.h>
16 #include <linux/if_vlan.h>
17 #include <linux/slab.h>
18 #include <linux/cpu.h>
19 #include <linux/average.h>
20 #include <linux/filter.h>
21 #include <linux/kernel.h>
22 #include <net/route.h>
23 #include <net/xdp.h>
24 #include <net/net_failover.h>
25 
26 static int napi_weight = NAPI_POLL_WEIGHT;
27 module_param(napi_weight, int, 0444);
28 
29 static bool csum = true, gso = true, napi_tx = true;
30 module_param(csum, bool, 0444);
31 module_param(gso, bool, 0444);
32 module_param(napi_tx, bool, 0644);
33 
34 /* FIXME: MTU in config. */
35 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
36 #define GOOD_COPY_LEN	128
37 
38 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
39 
40 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
41 #define VIRTIO_XDP_HEADROOM 256
42 
43 /* Separating two types of XDP xmit */
44 #define VIRTIO_XDP_TX		BIT(0)
45 #define VIRTIO_XDP_REDIR	BIT(1)
46 
47 #define VIRTIO_XDP_FLAG	BIT(0)
48 
49 /* RX packet size EWMA. The average packet size is used to determine the packet
50  * buffer size when refilling RX rings. As the entire RX ring may be refilled
51  * at once, the weight is chosen so that the EWMA will be insensitive to short-
52  * term, transient changes in packet size.
53  */
54 DECLARE_EWMA(pkt_len, 0, 64)
55 
56 #define VIRTNET_DRIVER_VERSION "1.0.0"
57 
58 static const unsigned long guest_offloads[] = {
59 	VIRTIO_NET_F_GUEST_TSO4,
60 	VIRTIO_NET_F_GUEST_TSO6,
61 	VIRTIO_NET_F_GUEST_ECN,
62 	VIRTIO_NET_F_GUEST_UFO,
63 	VIRTIO_NET_F_GUEST_CSUM
64 };
65 
66 #define GUEST_OFFLOAD_LRO_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
67 				(1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
68 				(1ULL << VIRTIO_NET_F_GUEST_ECN)  | \
69 				(1ULL << VIRTIO_NET_F_GUEST_UFO))
70 
71 struct virtnet_stat_desc {
72 	char desc[ETH_GSTRING_LEN];
73 	size_t offset;
74 };
75 
76 struct virtnet_sq_stats {
77 	struct u64_stats_sync syncp;
78 	u64 packets;
79 	u64 bytes;
80 	u64 xdp_tx;
81 	u64 xdp_tx_drops;
82 	u64 kicks;
83 };
84 
85 struct virtnet_rq_stats {
86 	struct u64_stats_sync syncp;
87 	u64 packets;
88 	u64 bytes;
89 	u64 drops;
90 	u64 xdp_packets;
91 	u64 xdp_tx;
92 	u64 xdp_redirects;
93 	u64 xdp_drops;
94 	u64 kicks;
95 };
96 
97 #define VIRTNET_SQ_STAT(m)	offsetof(struct virtnet_sq_stats, m)
98 #define VIRTNET_RQ_STAT(m)	offsetof(struct virtnet_rq_stats, m)
99 
100 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
101 	{ "packets",		VIRTNET_SQ_STAT(packets) },
102 	{ "bytes",		VIRTNET_SQ_STAT(bytes) },
103 	{ "xdp_tx",		VIRTNET_SQ_STAT(xdp_tx) },
104 	{ "xdp_tx_drops",	VIRTNET_SQ_STAT(xdp_tx_drops) },
105 	{ "kicks",		VIRTNET_SQ_STAT(kicks) },
106 };
107 
108 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
109 	{ "packets",		VIRTNET_RQ_STAT(packets) },
110 	{ "bytes",		VIRTNET_RQ_STAT(bytes) },
111 	{ "drops",		VIRTNET_RQ_STAT(drops) },
112 	{ "xdp_packets",	VIRTNET_RQ_STAT(xdp_packets) },
113 	{ "xdp_tx",		VIRTNET_RQ_STAT(xdp_tx) },
114 	{ "xdp_redirects",	VIRTNET_RQ_STAT(xdp_redirects) },
115 	{ "xdp_drops",		VIRTNET_RQ_STAT(xdp_drops) },
116 	{ "kicks",		VIRTNET_RQ_STAT(kicks) },
117 };
118 
119 #define VIRTNET_SQ_STATS_LEN	ARRAY_SIZE(virtnet_sq_stats_desc)
120 #define VIRTNET_RQ_STATS_LEN	ARRAY_SIZE(virtnet_rq_stats_desc)
121 
122 /* Internal representation of a send virtqueue */
123 struct send_queue {
124 	/* Virtqueue associated with this send _queue */
125 	struct virtqueue *vq;
126 
127 	/* TX: fragments + linear part + virtio header */
128 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
129 
130 	/* Name of the send queue: output.$index */
131 	char name[40];
132 
133 	struct virtnet_sq_stats stats;
134 
135 	struct napi_struct napi;
136 };
137 
138 /* Internal representation of a receive virtqueue */
139 struct receive_queue {
140 	/* Virtqueue associated with this receive_queue */
141 	struct virtqueue *vq;
142 
143 	struct napi_struct napi;
144 
145 	struct bpf_prog __rcu *xdp_prog;
146 
147 	struct virtnet_rq_stats stats;
148 
149 	/* Chain pages by the private ptr. */
150 	struct page *pages;
151 
152 	/* Average packet length for mergeable receive buffers. */
153 	struct ewma_pkt_len mrg_avg_pkt_len;
154 
155 	/* Page frag for packet buffer allocation. */
156 	struct page_frag alloc_frag;
157 
158 	/* RX: fragments + linear part + virtio header */
159 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
160 
161 	/* Min single buffer size for mergeable buffers case. */
162 	unsigned int min_buf_len;
163 
164 	/* Name of this receive queue: input.$index */
165 	char name[40];
166 
167 	struct xdp_rxq_info xdp_rxq;
168 };
169 
170 /* Control VQ buffers: protected by the rtnl lock */
171 struct control_buf {
172 	struct virtio_net_ctrl_hdr hdr;
173 	virtio_net_ctrl_ack status;
174 	struct virtio_net_ctrl_mq mq;
175 	u8 promisc;
176 	u8 allmulti;
177 	__virtio16 vid;
178 	__virtio64 offloads;
179 };
180 
181 struct virtnet_info {
182 	struct virtio_device *vdev;
183 	struct virtqueue *cvq;
184 	struct net_device *dev;
185 	struct send_queue *sq;
186 	struct receive_queue *rq;
187 	unsigned int status;
188 
189 	/* Max # of queue pairs supported by the device */
190 	u16 max_queue_pairs;
191 
192 	/* # of queue pairs currently used by the driver */
193 	u16 curr_queue_pairs;
194 
195 	/* # of XDP queue pairs currently used by the driver */
196 	u16 xdp_queue_pairs;
197 
198 	/* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
199 	bool xdp_enabled;
200 
201 	/* I like... big packets and I cannot lie! */
202 	bool big_packets;
203 
204 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
205 	bool mergeable_rx_bufs;
206 
207 	/* Has control virtqueue */
208 	bool has_cvq;
209 
210 	/* Host can handle any s/g split between our header and packet data */
211 	bool any_header_sg;
212 
213 	/* Packet virtio header size */
214 	u8 hdr_len;
215 
216 	/* Work struct for refilling if we run low on memory. */
217 	struct delayed_work refill;
218 
219 	/* Work struct for config space updates */
220 	struct work_struct config_work;
221 
222 	/* Does the affinity hint is set for virtqueues? */
223 	bool affinity_hint_set;
224 
225 	/* CPU hotplug instances for online & dead */
226 	struct hlist_node node;
227 	struct hlist_node node_dead;
228 
229 	struct control_buf *ctrl;
230 
231 	/* Ethtool settings */
232 	u8 duplex;
233 	u32 speed;
234 
235 	unsigned long guest_offloads;
236 	unsigned long guest_offloads_capable;
237 
238 	/* failover when STANDBY feature enabled */
239 	struct failover *failover;
240 };
241 
242 struct padded_vnet_hdr {
243 	struct virtio_net_hdr_mrg_rxbuf hdr;
244 	/*
245 	 * hdr is in a separate sg buffer, and data sg buffer shares same page
246 	 * with this header sg. This padding makes next sg 16 byte aligned
247 	 * after the header.
248 	 */
249 	char padding[4];
250 };
251 
is_xdp_frame(void * ptr)252 static bool is_xdp_frame(void *ptr)
253 {
254 	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
255 }
256 
xdp_to_ptr(struct xdp_frame * ptr)257 static void *xdp_to_ptr(struct xdp_frame *ptr)
258 {
259 	return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
260 }
261 
ptr_to_xdp(void * ptr)262 static struct xdp_frame *ptr_to_xdp(void *ptr)
263 {
264 	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
265 }
266 
267 /* Converting between virtqueue no. and kernel tx/rx queue no.
268  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
269  */
vq2txq(struct virtqueue * vq)270 static int vq2txq(struct virtqueue *vq)
271 {
272 	return (vq->index - 1) / 2;
273 }
274 
txq2vq(int txq)275 static int txq2vq(int txq)
276 {
277 	return txq * 2 + 1;
278 }
279 
vq2rxq(struct virtqueue * vq)280 static int vq2rxq(struct virtqueue *vq)
281 {
282 	return vq->index / 2;
283 }
284 
rxq2vq(int rxq)285 static int rxq2vq(int rxq)
286 {
287 	return rxq * 2;
288 }
289 
skb_vnet_hdr(struct sk_buff * skb)290 static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
291 {
292 	return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
293 }
294 
295 /*
296  * private is used to chain pages for big packets, put the whole
297  * most recent used list in the beginning for reuse
298  */
give_pages(struct receive_queue * rq,struct page * page)299 static void give_pages(struct receive_queue *rq, struct page *page)
300 {
301 	struct page *end;
302 
303 	/* Find end of list, sew whole thing into vi->rq.pages. */
304 	for (end = page; end->private; end = (struct page *)end->private);
305 	end->private = (unsigned long)rq->pages;
306 	rq->pages = page;
307 }
308 
get_a_page(struct receive_queue * rq,gfp_t gfp_mask)309 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
310 {
311 	struct page *p = rq->pages;
312 
313 	if (p) {
314 		rq->pages = (struct page *)p->private;
315 		/* clear private here, it is used to chain pages */
316 		p->private = 0;
317 	} else
318 		p = alloc_page(gfp_mask);
319 	return p;
320 }
321 
virtqueue_napi_schedule(struct napi_struct * napi,struct virtqueue * vq)322 static void virtqueue_napi_schedule(struct napi_struct *napi,
323 				    struct virtqueue *vq)
324 {
325 	if (napi_schedule_prep(napi)) {
326 		virtqueue_disable_cb(vq);
327 		__napi_schedule(napi);
328 	}
329 }
330 
virtqueue_napi_complete(struct napi_struct * napi,struct virtqueue * vq,int processed)331 static void virtqueue_napi_complete(struct napi_struct *napi,
332 				    struct virtqueue *vq, int processed)
333 {
334 	int opaque;
335 
336 	opaque = virtqueue_enable_cb_prepare(vq);
337 	if (napi_complete_done(napi, processed)) {
338 		if (unlikely(virtqueue_poll(vq, opaque)))
339 			virtqueue_napi_schedule(napi, vq);
340 	} else {
341 		virtqueue_disable_cb(vq);
342 	}
343 }
344 
skb_xmit_done(struct virtqueue * vq)345 static void skb_xmit_done(struct virtqueue *vq)
346 {
347 	struct virtnet_info *vi = vq->vdev->priv;
348 	struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
349 
350 	/* Suppress further interrupts. */
351 	virtqueue_disable_cb(vq);
352 
353 	if (napi->weight)
354 		virtqueue_napi_schedule(napi, vq);
355 	else
356 		/* We were probably waiting for more output buffers. */
357 		netif_wake_subqueue(vi->dev, vq2txq(vq));
358 }
359 
360 #define MRG_CTX_HEADER_SHIFT 22
mergeable_len_to_ctx(unsigned int truesize,unsigned int headroom)361 static void *mergeable_len_to_ctx(unsigned int truesize,
362 				  unsigned int headroom)
363 {
364 	return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
365 }
366 
mergeable_ctx_to_headroom(void * mrg_ctx)367 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
368 {
369 	return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
370 }
371 
mergeable_ctx_to_truesize(void * mrg_ctx)372 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
373 {
374 	return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
375 }
376 
377 /* Called from bottom half context */
page_to_skb(struct virtnet_info * vi,struct receive_queue * rq,struct page * page,unsigned int offset,unsigned int len,unsigned int truesize,bool hdr_valid,unsigned int metasize,unsigned int headroom)378 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
379 				   struct receive_queue *rq,
380 				   struct page *page, unsigned int offset,
381 				   unsigned int len, unsigned int truesize,
382 				   bool hdr_valid, unsigned int metasize,
383 				   unsigned int headroom)
384 {
385 	struct sk_buff *skb;
386 	struct virtio_net_hdr_mrg_rxbuf *hdr;
387 	unsigned int copy, hdr_len, hdr_padded_len;
388 	struct page *page_to_free = NULL;
389 	int tailroom, shinfo_size;
390 	char *p, *hdr_p, *buf;
391 
392 	p = page_address(page) + offset;
393 	hdr_p = p;
394 
395 	hdr_len = vi->hdr_len;
396 	if (vi->mergeable_rx_bufs)
397 		hdr_padded_len = sizeof(*hdr);
398 	else
399 		hdr_padded_len = sizeof(struct padded_vnet_hdr);
400 
401 	/* If headroom is not 0, there is an offset between the beginning of the
402 	 * data and the allocated space, otherwise the data and the allocated
403 	 * space are aligned.
404 	 */
405 	if (headroom) {
406 		/* Buffers with headroom use PAGE_SIZE as alloc size,
407 		 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
408 		 */
409 		truesize = PAGE_SIZE;
410 		tailroom = truesize - len - offset;
411 		buf = page_address(page);
412 	} else {
413 		tailroom = truesize - len;
414 		buf = p;
415 	}
416 
417 	len -= hdr_len;
418 	offset += hdr_padded_len;
419 	p += hdr_padded_len;
420 
421 	shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
422 
423 	/* copy small packet so we can reuse these pages */
424 	if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
425 		skb = build_skb(buf, truesize);
426 		if (unlikely(!skb))
427 			return NULL;
428 
429 		skb_reserve(skb, p - buf);
430 		skb_put(skb, len);
431 		goto ok;
432 	}
433 
434 	/* copy small packet so we can reuse these pages for small data */
435 	skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
436 	if (unlikely(!skb))
437 		return NULL;
438 
439 	/* Copy all frame if it fits skb->head, otherwise
440 	 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
441 	 */
442 	if (len <= skb_tailroom(skb))
443 		copy = len;
444 	else
445 		copy = ETH_HLEN + metasize;
446 	skb_put_data(skb, p, copy);
447 
448 	len -= copy;
449 	offset += copy;
450 
451 	if (vi->mergeable_rx_bufs) {
452 		if (len)
453 			skb_add_rx_frag(skb, 0, page, offset, len, truesize);
454 		else
455 			page_to_free = page;
456 		goto ok;
457 	}
458 
459 	/*
460 	 * Verify that we can indeed put this data into a skb.
461 	 * This is here to handle cases when the device erroneously
462 	 * tries to receive more than is possible. This is usually
463 	 * the case of a broken device.
464 	 */
465 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
466 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
467 		dev_kfree_skb(skb);
468 		return NULL;
469 	}
470 	BUG_ON(offset >= PAGE_SIZE);
471 	while (len) {
472 		unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
473 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
474 				frag_size, truesize);
475 		len -= frag_size;
476 		page = (struct page *)page->private;
477 		offset = 0;
478 	}
479 
480 	if (page)
481 		give_pages(rq, page);
482 
483 ok:
484 	/* hdr_valid means no XDP, so we can copy the vnet header */
485 	if (hdr_valid) {
486 		hdr = skb_vnet_hdr(skb);
487 		memcpy(hdr, hdr_p, hdr_len);
488 	}
489 	if (page_to_free)
490 		put_page(page_to_free);
491 
492 	if (metasize) {
493 		__skb_pull(skb, metasize);
494 		skb_metadata_set(skb, metasize);
495 	}
496 
497 	return skb;
498 }
499 
__virtnet_xdp_xmit_one(struct virtnet_info * vi,struct send_queue * sq,struct xdp_frame * xdpf)500 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
501 				   struct send_queue *sq,
502 				   struct xdp_frame *xdpf)
503 {
504 	struct virtio_net_hdr_mrg_rxbuf *hdr;
505 	int err;
506 
507 	if (unlikely(xdpf->headroom < vi->hdr_len))
508 		return -EOVERFLOW;
509 
510 	/* Make room for virtqueue hdr (also change xdpf->headroom?) */
511 	xdpf->data -= vi->hdr_len;
512 	/* Zero header and leave csum up to XDP layers */
513 	hdr = xdpf->data;
514 	memset(hdr, 0, vi->hdr_len);
515 	xdpf->len   += vi->hdr_len;
516 
517 	sg_init_one(sq->sg, xdpf->data, xdpf->len);
518 
519 	err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
520 				   GFP_ATOMIC);
521 	if (unlikely(err))
522 		return -ENOSPC; /* Caller handle free/refcnt */
523 
524 	return 0;
525 }
526 
527 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
528  * the current cpu, so it does not need to be locked.
529  *
530  * Here we use marco instead of inline functions because we have to deal with
531  * three issues at the same time: 1. the choice of sq. 2. judge and execute the
532  * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
533  * functions to perfectly solve these three problems at the same time.
534  */
535 #define virtnet_xdp_get_sq(vi) ({                                       \
536 	struct netdev_queue *txq;                                       \
537 	typeof(vi) v = (vi);                                            \
538 	unsigned int qp;                                                \
539 									\
540 	if (v->curr_queue_pairs > nr_cpu_ids) {                         \
541 		qp = v->curr_queue_pairs - v->xdp_queue_pairs;          \
542 		qp += smp_processor_id();                               \
543 		txq = netdev_get_tx_queue(v->dev, qp);                  \
544 		__netif_tx_acquire(txq);                                \
545 	} else {                                                        \
546 		qp = smp_processor_id() % v->curr_queue_pairs;          \
547 		txq = netdev_get_tx_queue(v->dev, qp);                  \
548 		__netif_tx_lock(txq, raw_smp_processor_id());           \
549 	}                                                               \
550 	v->sq + qp;                                                     \
551 })
552 
553 #define virtnet_xdp_put_sq(vi, q) {                                     \
554 	struct netdev_queue *txq;                                       \
555 	typeof(vi) v = (vi);                                            \
556 									\
557 	txq = netdev_get_tx_queue(v->dev, (q) - v->sq);                 \
558 	if (v->curr_queue_pairs > nr_cpu_ids)                           \
559 		__netif_tx_release(txq);                                \
560 	else                                                            \
561 		__netif_tx_unlock(txq);                                 \
562 }
563 
virtnet_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)564 static int virtnet_xdp_xmit(struct net_device *dev,
565 			    int n, struct xdp_frame **frames, u32 flags)
566 {
567 	struct virtnet_info *vi = netdev_priv(dev);
568 	struct receive_queue *rq = vi->rq;
569 	struct bpf_prog *xdp_prog;
570 	struct send_queue *sq;
571 	unsigned int len;
572 	int packets = 0;
573 	int bytes = 0;
574 	int nxmit = 0;
575 	int kicks = 0;
576 	void *ptr;
577 	int ret;
578 	int i;
579 
580 	/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
581 	 * indicate XDP resources have been successfully allocated.
582 	 */
583 	xdp_prog = rcu_access_pointer(rq->xdp_prog);
584 	if (!xdp_prog)
585 		return -ENXIO;
586 
587 	sq = virtnet_xdp_get_sq(vi);
588 
589 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
590 		ret = -EINVAL;
591 		goto out;
592 	}
593 
594 	/* Free up any pending old buffers before queueing new ones. */
595 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
596 		if (likely(is_xdp_frame(ptr))) {
597 			struct xdp_frame *frame = ptr_to_xdp(ptr);
598 
599 			bytes += frame->len;
600 			xdp_return_frame(frame);
601 		} else {
602 			struct sk_buff *skb = ptr;
603 
604 			bytes += skb->len;
605 			napi_consume_skb(skb, false);
606 		}
607 		packets++;
608 	}
609 
610 	for (i = 0; i < n; i++) {
611 		struct xdp_frame *xdpf = frames[i];
612 
613 		if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
614 			break;
615 		nxmit++;
616 	}
617 	ret = nxmit;
618 
619 	if (flags & XDP_XMIT_FLUSH) {
620 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
621 			kicks = 1;
622 	}
623 out:
624 	u64_stats_update_begin(&sq->stats.syncp);
625 	sq->stats.bytes += bytes;
626 	sq->stats.packets += packets;
627 	sq->stats.xdp_tx += n;
628 	sq->stats.xdp_tx_drops += n - nxmit;
629 	sq->stats.kicks += kicks;
630 	u64_stats_update_end(&sq->stats.syncp);
631 
632 	virtnet_xdp_put_sq(vi, sq);
633 	return ret;
634 }
635 
virtnet_get_headroom(struct virtnet_info * vi)636 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
637 {
638 	return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
639 }
640 
641 /* We copy the packet for XDP in the following cases:
642  *
643  * 1) Packet is scattered across multiple rx buffers.
644  * 2) Headroom space is insufficient.
645  *
646  * This is inefficient but it's a temporary condition that
647  * we hit right after XDP is enabled and until queue is refilled
648  * with large buffers with sufficient headroom - so it should affect
649  * at most queue size packets.
650  * Afterwards, the conditions to enable
651  * XDP should preclude the underlying device from sending packets
652  * across multiple buffers (num_buf > 1), and we make sure buffers
653  * have enough headroom.
654  */
xdp_linearize_page(struct receive_queue * rq,u16 * num_buf,struct page * p,int offset,int page_off,unsigned int * len)655 static struct page *xdp_linearize_page(struct receive_queue *rq,
656 				       u16 *num_buf,
657 				       struct page *p,
658 				       int offset,
659 				       int page_off,
660 				       unsigned int *len)
661 {
662 	struct page *page = alloc_page(GFP_ATOMIC);
663 
664 	if (!page)
665 		return NULL;
666 
667 	memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
668 	page_off += *len;
669 
670 	while (--*num_buf) {
671 		int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
672 		unsigned int buflen;
673 		void *buf;
674 		int off;
675 
676 		buf = virtqueue_get_buf(rq->vq, &buflen);
677 		if (unlikely(!buf))
678 			goto err_buf;
679 
680 		p = virt_to_head_page(buf);
681 		off = buf - page_address(p);
682 
683 		/* guard against a misconfigured or uncooperative backend that
684 		 * is sending packet larger than the MTU.
685 		 */
686 		if ((page_off + buflen + tailroom) > PAGE_SIZE) {
687 			put_page(p);
688 			goto err_buf;
689 		}
690 
691 		memcpy(page_address(page) + page_off,
692 		       page_address(p) + off, buflen);
693 		page_off += buflen;
694 		put_page(p);
695 	}
696 
697 	/* Headroom does not contribute to packet length */
698 	*len = page_off - VIRTIO_XDP_HEADROOM;
699 	return page;
700 err_buf:
701 	__free_pages(page, 0);
702 	return NULL;
703 }
704 
receive_small(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)705 static struct sk_buff *receive_small(struct net_device *dev,
706 				     struct virtnet_info *vi,
707 				     struct receive_queue *rq,
708 				     void *buf, void *ctx,
709 				     unsigned int len,
710 				     unsigned int *xdp_xmit,
711 				     struct virtnet_rq_stats *stats)
712 {
713 	struct sk_buff *skb;
714 	struct bpf_prog *xdp_prog;
715 	unsigned int xdp_headroom = (unsigned long)ctx;
716 	unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
717 	unsigned int headroom = vi->hdr_len + header_offset;
718 	unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
719 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
720 	struct page *page = virt_to_head_page(buf);
721 	unsigned int delta = 0;
722 	struct page *xdp_page;
723 	int err;
724 	unsigned int metasize = 0;
725 
726 	len -= vi->hdr_len;
727 	stats->bytes += len;
728 
729 	rcu_read_lock();
730 	xdp_prog = rcu_dereference(rq->xdp_prog);
731 	if (xdp_prog) {
732 		struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
733 		struct xdp_frame *xdpf;
734 		struct xdp_buff xdp;
735 		void *orig_data;
736 		u32 act;
737 
738 		if (unlikely(hdr->hdr.gso_type))
739 			goto err_xdp;
740 
741 		if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
742 			int offset = buf - page_address(page) + header_offset;
743 			unsigned int tlen = len + vi->hdr_len;
744 			u16 num_buf = 1;
745 
746 			xdp_headroom = virtnet_get_headroom(vi);
747 			header_offset = VIRTNET_RX_PAD + xdp_headroom;
748 			headroom = vi->hdr_len + header_offset;
749 			buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
750 				 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
751 			xdp_page = xdp_linearize_page(rq, &num_buf, page,
752 						      offset, header_offset,
753 						      &tlen);
754 			if (!xdp_page)
755 				goto err_xdp;
756 
757 			buf = page_address(xdp_page);
758 			put_page(page);
759 			page = xdp_page;
760 		}
761 
762 		xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
763 		xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
764 				 xdp_headroom, len, true);
765 		orig_data = xdp.data;
766 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
767 		stats->xdp_packets++;
768 
769 		switch (act) {
770 		case XDP_PASS:
771 			/* Recalculate length in case bpf program changed it */
772 			delta = orig_data - xdp.data;
773 			len = xdp.data_end - xdp.data;
774 			metasize = xdp.data - xdp.data_meta;
775 			break;
776 		case XDP_TX:
777 			stats->xdp_tx++;
778 			xdpf = xdp_convert_buff_to_frame(&xdp);
779 			if (unlikely(!xdpf))
780 				goto err_xdp;
781 			err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
782 			if (unlikely(!err)) {
783 				xdp_return_frame_rx_napi(xdpf);
784 			} else if (unlikely(err < 0)) {
785 				trace_xdp_exception(vi->dev, xdp_prog, act);
786 				goto err_xdp;
787 			}
788 			*xdp_xmit |= VIRTIO_XDP_TX;
789 			rcu_read_unlock();
790 			goto xdp_xmit;
791 		case XDP_REDIRECT:
792 			stats->xdp_redirects++;
793 			err = xdp_do_redirect(dev, &xdp, xdp_prog);
794 			if (err)
795 				goto err_xdp;
796 			*xdp_xmit |= VIRTIO_XDP_REDIR;
797 			rcu_read_unlock();
798 			goto xdp_xmit;
799 		default:
800 			bpf_warn_invalid_xdp_action(act);
801 			fallthrough;
802 		case XDP_ABORTED:
803 			trace_xdp_exception(vi->dev, xdp_prog, act);
804 			goto err_xdp;
805 		case XDP_DROP:
806 			goto err_xdp;
807 		}
808 	}
809 	rcu_read_unlock();
810 
811 	skb = build_skb(buf, buflen);
812 	if (!skb) {
813 		put_page(page);
814 		goto err;
815 	}
816 	skb_reserve(skb, headroom - delta);
817 	skb_put(skb, len);
818 	if (!xdp_prog) {
819 		buf += header_offset;
820 		memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
821 	} /* keep zeroed vnet hdr since XDP is loaded */
822 
823 	if (metasize)
824 		skb_metadata_set(skb, metasize);
825 
826 err:
827 	return skb;
828 
829 err_xdp:
830 	rcu_read_unlock();
831 	stats->xdp_drops++;
832 	stats->drops++;
833 	put_page(page);
834 xdp_xmit:
835 	return NULL;
836 }
837 
receive_big(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,struct virtnet_rq_stats * stats)838 static struct sk_buff *receive_big(struct net_device *dev,
839 				   struct virtnet_info *vi,
840 				   struct receive_queue *rq,
841 				   void *buf,
842 				   unsigned int len,
843 				   struct virtnet_rq_stats *stats)
844 {
845 	struct page *page = buf;
846 	struct sk_buff *skb =
847 		page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0);
848 
849 	stats->bytes += len - vi->hdr_len;
850 	if (unlikely(!skb))
851 		goto err;
852 
853 	return skb;
854 
855 err:
856 	stats->drops++;
857 	give_pages(rq, page);
858 	return NULL;
859 }
860 
receive_mergeable(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)861 static struct sk_buff *receive_mergeable(struct net_device *dev,
862 					 struct virtnet_info *vi,
863 					 struct receive_queue *rq,
864 					 void *buf,
865 					 void *ctx,
866 					 unsigned int len,
867 					 unsigned int *xdp_xmit,
868 					 struct virtnet_rq_stats *stats)
869 {
870 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
871 	u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
872 	struct page *page = virt_to_head_page(buf);
873 	int offset = buf - page_address(page);
874 	struct sk_buff *head_skb, *curr_skb;
875 	struct bpf_prog *xdp_prog;
876 	unsigned int truesize = mergeable_ctx_to_truesize(ctx);
877 	unsigned int headroom = mergeable_ctx_to_headroom(ctx);
878 	unsigned int metasize = 0;
879 	unsigned int frame_sz;
880 	int err;
881 
882 	head_skb = NULL;
883 	stats->bytes += len - vi->hdr_len;
884 
885 	rcu_read_lock();
886 	xdp_prog = rcu_dereference(rq->xdp_prog);
887 	if (xdp_prog) {
888 		struct xdp_frame *xdpf;
889 		struct page *xdp_page;
890 		struct xdp_buff xdp;
891 		void *data;
892 		u32 act;
893 
894 		/* Transient failure which in theory could occur if
895 		 * in-flight packets from before XDP was enabled reach
896 		 * the receive path after XDP is loaded.
897 		 */
898 		if (unlikely(hdr->hdr.gso_type))
899 			goto err_xdp;
900 
901 		/* Buffers with headroom use PAGE_SIZE as alloc size,
902 		 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
903 		 */
904 		frame_sz = headroom ? PAGE_SIZE : truesize;
905 
906 		/* This happens when rx buffer size is underestimated
907 		 * or headroom is not enough because of the buffer
908 		 * was refilled before XDP is set. This should only
909 		 * happen for the first several packets, so we don't
910 		 * care much about its performance.
911 		 */
912 		if (unlikely(num_buf > 1 ||
913 			     headroom < virtnet_get_headroom(vi))) {
914 			/* linearize data for XDP */
915 			xdp_page = xdp_linearize_page(rq, &num_buf,
916 						      page, offset,
917 						      VIRTIO_XDP_HEADROOM,
918 						      &len);
919 			frame_sz = PAGE_SIZE;
920 
921 			if (!xdp_page)
922 				goto err_xdp;
923 			offset = VIRTIO_XDP_HEADROOM;
924 		} else {
925 			xdp_page = page;
926 		}
927 
928 		/* Allow consuming headroom but reserve enough space to push
929 		 * the descriptor on if we get an XDP_TX return code.
930 		 */
931 		data = page_address(xdp_page) + offset;
932 		xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq);
933 		xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len,
934 				 VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true);
935 
936 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
937 		stats->xdp_packets++;
938 
939 		switch (act) {
940 		case XDP_PASS:
941 			metasize = xdp.data - xdp.data_meta;
942 
943 			/* recalculate offset to account for any header
944 			 * adjustments and minus the metasize to copy the
945 			 * metadata in page_to_skb(). Note other cases do not
946 			 * build an skb and avoid using offset
947 			 */
948 			offset = xdp.data - page_address(xdp_page) -
949 				 vi->hdr_len - metasize;
950 
951 			/* recalculate len if xdp.data, xdp.data_end or
952 			 * xdp.data_meta were adjusted
953 			 */
954 			len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
955 			/* We can only create skb based on xdp_page. */
956 			if (unlikely(xdp_page != page)) {
957 				rcu_read_unlock();
958 				put_page(page);
959 				head_skb = page_to_skb(vi, rq, xdp_page, offset,
960 						       len, PAGE_SIZE, false,
961 						       metasize, headroom);
962 				return head_skb;
963 			}
964 			break;
965 		case XDP_TX:
966 			stats->xdp_tx++;
967 			xdpf = xdp_convert_buff_to_frame(&xdp);
968 			if (unlikely(!xdpf))
969 				goto err_xdp;
970 			err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
971 			if (unlikely(!err)) {
972 				xdp_return_frame_rx_napi(xdpf);
973 			} else if (unlikely(err < 0)) {
974 				trace_xdp_exception(vi->dev, xdp_prog, act);
975 				if (unlikely(xdp_page != page))
976 					put_page(xdp_page);
977 				goto err_xdp;
978 			}
979 			*xdp_xmit |= VIRTIO_XDP_TX;
980 			if (unlikely(xdp_page != page))
981 				put_page(page);
982 			rcu_read_unlock();
983 			goto xdp_xmit;
984 		case XDP_REDIRECT:
985 			stats->xdp_redirects++;
986 			err = xdp_do_redirect(dev, &xdp, xdp_prog);
987 			if (err) {
988 				if (unlikely(xdp_page != page))
989 					put_page(xdp_page);
990 				goto err_xdp;
991 			}
992 			*xdp_xmit |= VIRTIO_XDP_REDIR;
993 			if (unlikely(xdp_page != page))
994 				put_page(page);
995 			rcu_read_unlock();
996 			goto xdp_xmit;
997 		default:
998 			bpf_warn_invalid_xdp_action(act);
999 			fallthrough;
1000 		case XDP_ABORTED:
1001 			trace_xdp_exception(vi->dev, xdp_prog, act);
1002 			fallthrough;
1003 		case XDP_DROP:
1004 			if (unlikely(xdp_page != page))
1005 				__free_pages(xdp_page, 0);
1006 			goto err_xdp;
1007 		}
1008 	}
1009 	rcu_read_unlock();
1010 
1011 	if (unlikely(len > truesize)) {
1012 		pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1013 			 dev->name, len, (unsigned long)ctx);
1014 		dev->stats.rx_length_errors++;
1015 		goto err_skb;
1016 	}
1017 
1018 	head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
1019 			       metasize, headroom);
1020 	curr_skb = head_skb;
1021 
1022 	if (unlikely(!curr_skb))
1023 		goto err_skb;
1024 	while (--num_buf) {
1025 		int num_skb_frags;
1026 
1027 		buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
1028 		if (unlikely(!buf)) {
1029 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
1030 				 dev->name, num_buf,
1031 				 virtio16_to_cpu(vi->vdev,
1032 						 hdr->num_buffers));
1033 			dev->stats.rx_length_errors++;
1034 			goto err_buf;
1035 		}
1036 
1037 		stats->bytes += len;
1038 		page = virt_to_head_page(buf);
1039 
1040 		truesize = mergeable_ctx_to_truesize(ctx);
1041 		if (unlikely(len > truesize)) {
1042 			pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1043 				 dev->name, len, (unsigned long)ctx);
1044 			dev->stats.rx_length_errors++;
1045 			goto err_skb;
1046 		}
1047 
1048 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
1049 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1050 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
1051 
1052 			if (unlikely(!nskb))
1053 				goto err_skb;
1054 			if (curr_skb == head_skb)
1055 				skb_shinfo(curr_skb)->frag_list = nskb;
1056 			else
1057 				curr_skb->next = nskb;
1058 			curr_skb = nskb;
1059 			head_skb->truesize += nskb->truesize;
1060 			num_skb_frags = 0;
1061 		}
1062 		if (curr_skb != head_skb) {
1063 			head_skb->data_len += len;
1064 			head_skb->len += len;
1065 			head_skb->truesize += truesize;
1066 		}
1067 		offset = buf - page_address(page);
1068 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1069 			put_page(page);
1070 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1071 					     len, truesize);
1072 		} else {
1073 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
1074 					offset, len, truesize);
1075 		}
1076 	}
1077 
1078 	ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
1079 	return head_skb;
1080 
1081 err_xdp:
1082 	rcu_read_unlock();
1083 	stats->xdp_drops++;
1084 err_skb:
1085 	put_page(page);
1086 	while (num_buf-- > 1) {
1087 		buf = virtqueue_get_buf(rq->vq, &len);
1088 		if (unlikely(!buf)) {
1089 			pr_debug("%s: rx error: %d buffers missing\n",
1090 				 dev->name, num_buf);
1091 			dev->stats.rx_length_errors++;
1092 			break;
1093 		}
1094 		stats->bytes += len;
1095 		page = virt_to_head_page(buf);
1096 		put_page(page);
1097 	}
1098 err_buf:
1099 	stats->drops++;
1100 	dev_kfree_skb(head_skb);
1101 xdp_xmit:
1102 	return NULL;
1103 }
1104 
receive_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,void ** ctx,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1105 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1106 			void *buf, unsigned int len, void **ctx,
1107 			unsigned int *xdp_xmit,
1108 			struct virtnet_rq_stats *stats)
1109 {
1110 	struct net_device *dev = vi->dev;
1111 	struct sk_buff *skb;
1112 	struct virtio_net_hdr_mrg_rxbuf *hdr;
1113 
1114 	if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
1115 		pr_debug("%s: short packet %i\n", dev->name, len);
1116 		dev->stats.rx_length_errors++;
1117 		if (vi->mergeable_rx_bufs) {
1118 			put_page(virt_to_head_page(buf));
1119 		} else if (vi->big_packets) {
1120 			give_pages(rq, buf);
1121 		} else {
1122 			put_page(virt_to_head_page(buf));
1123 		}
1124 		return;
1125 	}
1126 
1127 	if (vi->mergeable_rx_bufs)
1128 		skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1129 					stats);
1130 	else if (vi->big_packets)
1131 		skb = receive_big(dev, vi, rq, buf, len, stats);
1132 	else
1133 		skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1134 
1135 	if (unlikely(!skb))
1136 		return;
1137 
1138 	hdr = skb_vnet_hdr(skb);
1139 
1140 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
1141 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1142 
1143 	if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1144 				  virtio_is_little_endian(vi->vdev))) {
1145 		net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1146 				     dev->name, hdr->hdr.gso_type,
1147 				     hdr->hdr.gso_size);
1148 		goto frame_err;
1149 	}
1150 
1151 	skb_record_rx_queue(skb, vq2rxq(rq->vq));
1152 	skb->protocol = eth_type_trans(skb, dev);
1153 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1154 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
1155 
1156 	napi_gro_receive(&rq->napi, skb);
1157 	return;
1158 
1159 frame_err:
1160 	dev->stats.rx_frame_errors++;
1161 	dev_kfree_skb(skb);
1162 }
1163 
1164 /* Unlike mergeable buffers, all buffers are allocated to the
1165  * same size, except for the headroom. For this reason we do
1166  * not need to use  mergeable_len_to_ctx here - it is enough
1167  * to store the headroom as the context ignoring the truesize.
1168  */
add_recvbuf_small(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1169 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1170 			     gfp_t gfp)
1171 {
1172 	struct page_frag *alloc_frag = &rq->alloc_frag;
1173 	char *buf;
1174 	unsigned int xdp_headroom = virtnet_get_headroom(vi);
1175 	void *ctx = (void *)(unsigned long)xdp_headroom;
1176 	int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
1177 	int err;
1178 
1179 	len = SKB_DATA_ALIGN(len) +
1180 	      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1181 	if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
1182 		return -ENOMEM;
1183 
1184 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1185 	get_page(alloc_frag->page);
1186 	alloc_frag->offset += len;
1187 	sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1188 		    vi->hdr_len + GOOD_PACKET_LEN);
1189 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1190 	if (err < 0)
1191 		put_page(virt_to_head_page(buf));
1192 	return err;
1193 }
1194 
add_recvbuf_big(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1195 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1196 			   gfp_t gfp)
1197 {
1198 	struct page *first, *list = NULL;
1199 	char *p;
1200 	int i, err, offset;
1201 
1202 	sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1203 
1204 	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
1205 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
1206 		first = get_a_page(rq, gfp);
1207 		if (!first) {
1208 			if (list)
1209 				give_pages(rq, list);
1210 			return -ENOMEM;
1211 		}
1212 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
1213 
1214 		/* chain new page in list head to match sg */
1215 		first->private = (unsigned long)list;
1216 		list = first;
1217 	}
1218 
1219 	first = get_a_page(rq, gfp);
1220 	if (!first) {
1221 		give_pages(rq, list);
1222 		return -ENOMEM;
1223 	}
1224 	p = page_address(first);
1225 
1226 	/* rq->sg[0], rq->sg[1] share the same page */
1227 	/* a separated rq->sg[0] for header - required in case !any_header_sg */
1228 	sg_set_buf(&rq->sg[0], p, vi->hdr_len);
1229 
1230 	/* rq->sg[1] for data packet, from offset */
1231 	offset = sizeof(struct padded_vnet_hdr);
1232 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
1233 
1234 	/* chain first in list head */
1235 	first->private = (unsigned long)list;
1236 	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1237 				  first, gfp);
1238 	if (err < 0)
1239 		give_pages(rq, first);
1240 
1241 	return err;
1242 }
1243 
get_mergeable_buf_len(struct receive_queue * rq,struct ewma_pkt_len * avg_pkt_len,unsigned int room)1244 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
1245 					  struct ewma_pkt_len *avg_pkt_len,
1246 					  unsigned int room)
1247 {
1248 	const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1249 	unsigned int len;
1250 
1251 	if (room)
1252 		return PAGE_SIZE - room;
1253 
1254 	len = hdr_len +	clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1255 				rq->min_buf_len, PAGE_SIZE - hdr_len);
1256 
1257 	return ALIGN(len, L1_CACHE_BYTES);
1258 }
1259 
add_recvbuf_mergeable(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1260 static int add_recvbuf_mergeable(struct virtnet_info *vi,
1261 				 struct receive_queue *rq, gfp_t gfp)
1262 {
1263 	struct page_frag *alloc_frag = &rq->alloc_frag;
1264 	unsigned int headroom = virtnet_get_headroom(vi);
1265 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1266 	unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1267 	char *buf;
1268 	void *ctx;
1269 	int err;
1270 	unsigned int len, hole;
1271 
1272 	/* Extra tailroom is needed to satisfy XDP's assumption. This
1273 	 * means rx frags coalescing won't work, but consider we've
1274 	 * disabled GSO for XDP, it won't be a big issue.
1275 	 */
1276 	len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1277 	if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
1278 		return -ENOMEM;
1279 
1280 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1281 	buf += headroom; /* advance address leaving hole at front of pkt */
1282 	get_page(alloc_frag->page);
1283 	alloc_frag->offset += len + room;
1284 	hole = alloc_frag->size - alloc_frag->offset;
1285 	if (hole < len + room) {
1286 		/* To avoid internal fragmentation, if there is very likely not
1287 		 * enough space for another buffer, add the remaining space to
1288 		 * the current buffer.
1289 		 */
1290 		len += hole;
1291 		alloc_frag->offset += hole;
1292 	}
1293 
1294 	sg_init_one(rq->sg, buf, len);
1295 	ctx = mergeable_len_to_ctx(len, headroom);
1296 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1297 	if (err < 0)
1298 		put_page(virt_to_head_page(buf));
1299 
1300 	return err;
1301 }
1302 
1303 /*
1304  * Returns false if we couldn't fill entirely (OOM).
1305  *
1306  * Normally run in the receive path, but can also be run from ndo_open
1307  * before we're receiving packets, or from refill_work which is
1308  * careful to disable receiving (using napi_disable).
1309  */
try_fill_recv(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1310 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1311 			  gfp_t gfp)
1312 {
1313 	int err;
1314 	bool oom;
1315 
1316 	do {
1317 		if (vi->mergeable_rx_bufs)
1318 			err = add_recvbuf_mergeable(vi, rq, gfp);
1319 		else if (vi->big_packets)
1320 			err = add_recvbuf_big(vi, rq, gfp);
1321 		else
1322 			err = add_recvbuf_small(vi, rq, gfp);
1323 
1324 		oom = err == -ENOMEM;
1325 		if (err)
1326 			break;
1327 	} while (rq->vq->num_free);
1328 	if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
1329 		unsigned long flags;
1330 
1331 		flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
1332 		rq->stats.kicks++;
1333 		u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
1334 	}
1335 
1336 	return !oom;
1337 }
1338 
skb_recv_done(struct virtqueue * rvq)1339 static void skb_recv_done(struct virtqueue *rvq)
1340 {
1341 	struct virtnet_info *vi = rvq->vdev->priv;
1342 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1343 
1344 	virtqueue_napi_schedule(&rq->napi, rvq);
1345 }
1346 
virtnet_napi_enable(struct virtqueue * vq,struct napi_struct * napi)1347 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
1348 {
1349 	napi_enable(napi);
1350 
1351 	/* If all buffers were filled by other side before we napi_enabled, we
1352 	 * won't get another interrupt, so process any outstanding packets now.
1353 	 * Call local_bh_enable after to trigger softIRQ processing.
1354 	 */
1355 	local_bh_disable();
1356 	virtqueue_napi_schedule(napi, vq);
1357 	local_bh_enable();
1358 }
1359 
virtnet_napi_tx_enable(struct virtnet_info * vi,struct virtqueue * vq,struct napi_struct * napi)1360 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1361 				   struct virtqueue *vq,
1362 				   struct napi_struct *napi)
1363 {
1364 	if (!napi->weight)
1365 		return;
1366 
1367 	/* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1368 	 * enable the feature if this is likely affine with the transmit path.
1369 	 */
1370 	if (!vi->affinity_hint_set) {
1371 		napi->weight = 0;
1372 		return;
1373 	}
1374 
1375 	return virtnet_napi_enable(vq, napi);
1376 }
1377 
virtnet_napi_tx_disable(struct napi_struct * napi)1378 static void virtnet_napi_tx_disable(struct napi_struct *napi)
1379 {
1380 	if (napi->weight)
1381 		napi_disable(napi);
1382 }
1383 
refill_work(struct work_struct * work)1384 static void refill_work(struct work_struct *work)
1385 {
1386 	struct virtnet_info *vi =
1387 		container_of(work, struct virtnet_info, refill.work);
1388 	bool still_empty;
1389 	int i;
1390 
1391 	for (i = 0; i < vi->curr_queue_pairs; i++) {
1392 		struct receive_queue *rq = &vi->rq[i];
1393 
1394 		napi_disable(&rq->napi);
1395 		still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1396 		virtnet_napi_enable(rq->vq, &rq->napi);
1397 
1398 		/* In theory, this can happen: if we don't get any buffers in
1399 		 * we will *never* try to fill again.
1400 		 */
1401 		if (still_empty)
1402 			schedule_delayed_work(&vi->refill, HZ/2);
1403 	}
1404 }
1405 
virtnet_receive(struct receive_queue * rq,int budget,unsigned int * xdp_xmit)1406 static int virtnet_receive(struct receive_queue *rq, int budget,
1407 			   unsigned int *xdp_xmit)
1408 {
1409 	struct virtnet_info *vi = rq->vq->vdev->priv;
1410 	struct virtnet_rq_stats stats = {};
1411 	unsigned int len;
1412 	void *buf;
1413 	int i;
1414 
1415 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
1416 		void *ctx;
1417 
1418 		while (stats.packets < budget &&
1419 		       (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1420 			receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1421 			stats.packets++;
1422 		}
1423 	} else {
1424 		while (stats.packets < budget &&
1425 		       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1426 			receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1427 			stats.packets++;
1428 		}
1429 	}
1430 
1431 	if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
1432 		if (!try_fill_recv(vi, rq, GFP_ATOMIC))
1433 			schedule_delayed_work(&vi->refill, 0);
1434 	}
1435 
1436 	u64_stats_update_begin(&rq->stats.syncp);
1437 	for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1438 		size_t offset = virtnet_rq_stats_desc[i].offset;
1439 		u64 *item;
1440 
1441 		item = (u64 *)((u8 *)&rq->stats + offset);
1442 		*item += *(u64 *)((u8 *)&stats + offset);
1443 	}
1444 	u64_stats_update_end(&rq->stats.syncp);
1445 
1446 	return stats.packets;
1447 }
1448 
free_old_xmit_skbs(struct send_queue * sq,bool in_napi)1449 static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
1450 {
1451 	unsigned int len;
1452 	unsigned int packets = 0;
1453 	unsigned int bytes = 0;
1454 	void *ptr;
1455 
1456 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1457 		if (likely(!is_xdp_frame(ptr))) {
1458 			struct sk_buff *skb = ptr;
1459 
1460 			pr_debug("Sent skb %p\n", skb);
1461 
1462 			bytes += skb->len;
1463 			napi_consume_skb(skb, in_napi);
1464 		} else {
1465 			struct xdp_frame *frame = ptr_to_xdp(ptr);
1466 
1467 			bytes += frame->len;
1468 			xdp_return_frame(frame);
1469 		}
1470 		packets++;
1471 	}
1472 
1473 	/* Avoid overhead when no packets have been processed
1474 	 * happens when called speculatively from start_xmit.
1475 	 */
1476 	if (!packets)
1477 		return;
1478 
1479 	u64_stats_update_begin(&sq->stats.syncp);
1480 	sq->stats.bytes += bytes;
1481 	sq->stats.packets += packets;
1482 	u64_stats_update_end(&sq->stats.syncp);
1483 }
1484 
is_xdp_raw_buffer_queue(struct virtnet_info * vi,int q)1485 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1486 {
1487 	if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1488 		return false;
1489 	else if (q < vi->curr_queue_pairs)
1490 		return true;
1491 	else
1492 		return false;
1493 }
1494 
virtnet_poll_cleantx(struct receive_queue * rq)1495 static void virtnet_poll_cleantx(struct receive_queue *rq)
1496 {
1497 	struct virtnet_info *vi = rq->vq->vdev->priv;
1498 	unsigned int index = vq2rxq(rq->vq);
1499 	struct send_queue *sq = &vi->sq[index];
1500 	struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1501 
1502 	if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
1503 		return;
1504 
1505 	if (__netif_tx_trylock(txq)) {
1506 		free_old_xmit_skbs(sq, true);
1507 		__netif_tx_unlock(txq);
1508 	}
1509 
1510 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1511 		netif_tx_wake_queue(txq);
1512 }
1513 
virtnet_poll(struct napi_struct * napi,int budget)1514 static int virtnet_poll(struct napi_struct *napi, int budget)
1515 {
1516 	struct receive_queue *rq =
1517 		container_of(napi, struct receive_queue, napi);
1518 	struct virtnet_info *vi = rq->vq->vdev->priv;
1519 	struct send_queue *sq;
1520 	unsigned int received;
1521 	unsigned int xdp_xmit = 0;
1522 
1523 	virtnet_poll_cleantx(rq);
1524 
1525 	received = virtnet_receive(rq, budget, &xdp_xmit);
1526 
1527 	/* Out of packets? */
1528 	if (received < budget)
1529 		virtqueue_napi_complete(napi, rq->vq, received);
1530 
1531 	if (xdp_xmit & VIRTIO_XDP_REDIR)
1532 		xdp_do_flush();
1533 
1534 	if (xdp_xmit & VIRTIO_XDP_TX) {
1535 		sq = virtnet_xdp_get_sq(vi);
1536 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1537 			u64_stats_update_begin(&sq->stats.syncp);
1538 			sq->stats.kicks++;
1539 			u64_stats_update_end(&sq->stats.syncp);
1540 		}
1541 		virtnet_xdp_put_sq(vi, sq);
1542 	}
1543 
1544 	return received;
1545 }
1546 
virtnet_open(struct net_device * dev)1547 static int virtnet_open(struct net_device *dev)
1548 {
1549 	struct virtnet_info *vi = netdev_priv(dev);
1550 	int i, err;
1551 
1552 	for (i = 0; i < vi->max_queue_pairs; i++) {
1553 		if (i < vi->curr_queue_pairs)
1554 			/* Make sure we have some buffers: if oom use wq. */
1555 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1556 				schedule_delayed_work(&vi->refill, 0);
1557 
1558 		err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
1559 		if (err < 0)
1560 			return err;
1561 
1562 		err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1563 						 MEM_TYPE_PAGE_SHARED, NULL);
1564 		if (err < 0) {
1565 			xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1566 			return err;
1567 		}
1568 
1569 		virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1570 		virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1571 	}
1572 
1573 	return 0;
1574 }
1575 
virtnet_poll_tx(struct napi_struct * napi,int budget)1576 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1577 {
1578 	struct send_queue *sq = container_of(napi, struct send_queue, napi);
1579 	struct virtnet_info *vi = sq->vq->vdev->priv;
1580 	unsigned int index = vq2txq(sq->vq);
1581 	struct netdev_queue *txq;
1582 
1583 	if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1584 		/* We don't need to enable cb for XDP */
1585 		napi_complete_done(napi, 0);
1586 		return 0;
1587 	}
1588 
1589 	txq = netdev_get_tx_queue(vi->dev, index);
1590 	__netif_tx_lock(txq, raw_smp_processor_id());
1591 	free_old_xmit_skbs(sq, true);
1592 	__netif_tx_unlock(txq);
1593 
1594 	virtqueue_napi_complete(napi, sq->vq, 0);
1595 
1596 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1597 		netif_tx_wake_queue(txq);
1598 
1599 	return 0;
1600 }
1601 
xmit_skb(struct send_queue * sq,struct sk_buff * skb)1602 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1603 {
1604 	struct virtio_net_hdr_mrg_rxbuf *hdr;
1605 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1606 	struct virtnet_info *vi = sq->vq->vdev->priv;
1607 	int num_sg;
1608 	unsigned hdr_len = vi->hdr_len;
1609 	bool can_push;
1610 
1611 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1612 
1613 	can_push = vi->any_header_sg &&
1614 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1615 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1616 	/* Even if we can, don't push here yet as this would skew
1617 	 * csum_start offset below. */
1618 	if (can_push)
1619 		hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1620 	else
1621 		hdr = skb_vnet_hdr(skb);
1622 
1623 	if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1624 				    virtio_is_little_endian(vi->vdev), false,
1625 				    0))
1626 		BUG();
1627 
1628 	if (vi->mergeable_rx_bufs)
1629 		hdr->num_buffers = 0;
1630 
1631 	sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1632 	if (can_push) {
1633 		__skb_push(skb, hdr_len);
1634 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1635 		if (unlikely(num_sg < 0))
1636 			return num_sg;
1637 		/* Pull header back to avoid skew in tx bytes calculations. */
1638 		__skb_pull(skb, hdr_len);
1639 	} else {
1640 		sg_set_buf(sq->sg, hdr, hdr_len);
1641 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1642 		if (unlikely(num_sg < 0))
1643 			return num_sg;
1644 		num_sg++;
1645 	}
1646 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
1647 }
1648 
start_xmit(struct sk_buff * skb,struct net_device * dev)1649 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
1650 {
1651 	struct virtnet_info *vi = netdev_priv(dev);
1652 	int qnum = skb_get_queue_mapping(skb);
1653 	struct send_queue *sq = &vi->sq[qnum];
1654 	int err;
1655 	struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1656 	bool kick = !netdev_xmit_more();
1657 	bool use_napi = sq->napi.weight;
1658 
1659 	/* Free up any pending old buffers before queueing new ones. */
1660 	free_old_xmit_skbs(sq, false);
1661 
1662 	if (use_napi && kick)
1663 		virtqueue_enable_cb_delayed(sq->vq);
1664 
1665 	/* timestamp packet in software */
1666 	skb_tx_timestamp(skb);
1667 
1668 	/* Try to transmit */
1669 	err = xmit_skb(sq, skb);
1670 
1671 	/* This should not happen! */
1672 	if (unlikely(err)) {
1673 		dev->stats.tx_fifo_errors++;
1674 		if (net_ratelimit())
1675 			dev_warn(&dev->dev,
1676 				 "Unexpected TXQ (%d) queue failure: %d\n",
1677 				 qnum, err);
1678 		dev->stats.tx_dropped++;
1679 		dev_kfree_skb_any(skb);
1680 		return NETDEV_TX_OK;
1681 	}
1682 
1683 	/* Don't wait up for transmitted skbs to be freed. */
1684 	if (!use_napi) {
1685 		skb_orphan(skb);
1686 		nf_reset_ct(skb);
1687 	}
1688 
1689 	/* If running out of space, stop queue to avoid getting packets that we
1690 	 * are then unable to transmit.
1691 	 * An alternative would be to force queuing layer to requeue the skb by
1692 	 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1693 	 * returned in a normal path of operation: it means that driver is not
1694 	 * maintaining the TX queue stop/start state properly, and causes
1695 	 * the stack to do a non-trivial amount of useless work.
1696 	 * Since most packets only take 1 or 2 ring slots, stopping the queue
1697 	 * early means 16 slots are typically wasted.
1698 	 */
1699 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1700 		netif_stop_subqueue(dev, qnum);
1701 		if (!use_napi &&
1702 		    unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1703 			/* More just got used, free them then recheck. */
1704 			free_old_xmit_skbs(sq, false);
1705 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1706 				netif_start_subqueue(dev, qnum);
1707 				virtqueue_disable_cb(sq->vq);
1708 			}
1709 		}
1710 	}
1711 
1712 	if (kick || netif_xmit_stopped(txq)) {
1713 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1714 			u64_stats_update_begin(&sq->stats.syncp);
1715 			sq->stats.kicks++;
1716 			u64_stats_update_end(&sq->stats.syncp);
1717 		}
1718 	}
1719 
1720 	return NETDEV_TX_OK;
1721 }
1722 
1723 /*
1724  * Send command via the control virtqueue and check status.  Commands
1725  * supported by the hypervisor, as indicated by feature bits, should
1726  * never fail unless improperly formatted.
1727  */
virtnet_send_command(struct virtnet_info * vi,u8 class,u8 cmd,struct scatterlist * out)1728 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1729 				 struct scatterlist *out)
1730 {
1731 	struct scatterlist *sgs[4], hdr, stat;
1732 	unsigned out_num = 0, tmp;
1733 
1734 	/* Caller should know better */
1735 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
1736 
1737 	vi->ctrl->status = ~0;
1738 	vi->ctrl->hdr.class = class;
1739 	vi->ctrl->hdr.cmd = cmd;
1740 	/* Add header */
1741 	sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
1742 	sgs[out_num++] = &hdr;
1743 
1744 	if (out)
1745 		sgs[out_num++] = out;
1746 
1747 	/* Add return status. */
1748 	sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
1749 	sgs[out_num] = &stat;
1750 
1751 	BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1752 	virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1753 
1754 	if (unlikely(!virtqueue_kick(vi->cvq)))
1755 		return vi->ctrl->status == VIRTIO_NET_OK;
1756 
1757 	/* Spin for a response, the kick causes an ioport write, trapping
1758 	 * into the hypervisor, so the request should be handled immediately.
1759 	 */
1760 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1761 	       !virtqueue_is_broken(vi->cvq))
1762 		cpu_relax();
1763 
1764 	return vi->ctrl->status == VIRTIO_NET_OK;
1765 }
1766 
virtnet_set_mac_address(struct net_device * dev,void * p)1767 static int virtnet_set_mac_address(struct net_device *dev, void *p)
1768 {
1769 	struct virtnet_info *vi = netdev_priv(dev);
1770 	struct virtio_device *vdev = vi->vdev;
1771 	int ret;
1772 	struct sockaddr *addr;
1773 	struct scatterlist sg;
1774 
1775 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1776 		return -EOPNOTSUPP;
1777 
1778 	addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
1779 	if (!addr)
1780 		return -ENOMEM;
1781 
1782 	ret = eth_prepare_mac_addr_change(dev, addr);
1783 	if (ret)
1784 		goto out;
1785 
1786 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1787 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
1788 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1789 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
1790 			dev_warn(&vdev->dev,
1791 				 "Failed to set mac address by vq command.\n");
1792 			ret = -EINVAL;
1793 			goto out;
1794 		}
1795 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1796 		   !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1797 		unsigned int i;
1798 
1799 		/* Naturally, this has an atomicity problem. */
1800 		for (i = 0; i < dev->addr_len; i++)
1801 			virtio_cwrite8(vdev,
1802 				       offsetof(struct virtio_net_config, mac) +
1803 				       i, addr->sa_data[i]);
1804 	}
1805 
1806 	eth_commit_mac_addr_change(dev, p);
1807 	ret = 0;
1808 
1809 out:
1810 	kfree(addr);
1811 	return ret;
1812 }
1813 
virtnet_stats(struct net_device * dev,struct rtnl_link_stats64 * tot)1814 static void virtnet_stats(struct net_device *dev,
1815 			  struct rtnl_link_stats64 *tot)
1816 {
1817 	struct virtnet_info *vi = netdev_priv(dev);
1818 	unsigned int start;
1819 	int i;
1820 
1821 	for (i = 0; i < vi->max_queue_pairs; i++) {
1822 		u64 tpackets, tbytes, rpackets, rbytes, rdrops;
1823 		struct receive_queue *rq = &vi->rq[i];
1824 		struct send_queue *sq = &vi->sq[i];
1825 
1826 		do {
1827 			start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1828 			tpackets = sq->stats.packets;
1829 			tbytes   = sq->stats.bytes;
1830 		} while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
1831 
1832 		do {
1833 			start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
1834 			rpackets = rq->stats.packets;
1835 			rbytes   = rq->stats.bytes;
1836 			rdrops   = rq->stats.drops;
1837 		} while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
1838 
1839 		tot->rx_packets += rpackets;
1840 		tot->tx_packets += tpackets;
1841 		tot->rx_bytes   += rbytes;
1842 		tot->tx_bytes   += tbytes;
1843 		tot->rx_dropped += rdrops;
1844 	}
1845 
1846 	tot->tx_dropped = dev->stats.tx_dropped;
1847 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
1848 	tot->rx_length_errors = dev->stats.rx_length_errors;
1849 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
1850 }
1851 
virtnet_ack_link_announce(struct virtnet_info * vi)1852 static void virtnet_ack_link_announce(struct virtnet_info *vi)
1853 {
1854 	rtnl_lock();
1855 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1856 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1857 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1858 	rtnl_unlock();
1859 }
1860 
_virtnet_set_queues(struct virtnet_info * vi,u16 queue_pairs)1861 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1862 {
1863 	struct scatterlist sg;
1864 	struct net_device *dev = vi->dev;
1865 
1866 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1867 		return 0;
1868 
1869 	vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1870 	sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
1871 
1872 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1873 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1874 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1875 			 queue_pairs);
1876 		return -EINVAL;
1877 	} else {
1878 		vi->curr_queue_pairs = queue_pairs;
1879 		/* virtnet_open() will refill when device is going to up. */
1880 		if (dev->flags & IFF_UP)
1881 			schedule_delayed_work(&vi->refill, 0);
1882 	}
1883 
1884 	return 0;
1885 }
1886 
virtnet_set_queues(struct virtnet_info * vi,u16 queue_pairs)1887 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1888 {
1889 	int err;
1890 
1891 	rtnl_lock();
1892 	err = _virtnet_set_queues(vi, queue_pairs);
1893 	rtnl_unlock();
1894 	return err;
1895 }
1896 
virtnet_close(struct net_device * dev)1897 static int virtnet_close(struct net_device *dev)
1898 {
1899 	struct virtnet_info *vi = netdev_priv(dev);
1900 	int i;
1901 
1902 	/* Make sure refill_work doesn't re-enable napi! */
1903 	cancel_delayed_work_sync(&vi->refill);
1904 
1905 	for (i = 0; i < vi->max_queue_pairs; i++) {
1906 		xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1907 		napi_disable(&vi->rq[i].napi);
1908 		virtnet_napi_tx_disable(&vi->sq[i].napi);
1909 	}
1910 
1911 	return 0;
1912 }
1913 
virtnet_set_rx_mode(struct net_device * dev)1914 static void virtnet_set_rx_mode(struct net_device *dev)
1915 {
1916 	struct virtnet_info *vi = netdev_priv(dev);
1917 	struct scatterlist sg[2];
1918 	struct virtio_net_ctrl_mac *mac_data;
1919 	struct netdev_hw_addr *ha;
1920 	int uc_count;
1921 	int mc_count;
1922 	void *buf;
1923 	int i;
1924 
1925 	/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
1926 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1927 		return;
1928 
1929 	vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
1930 	vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
1931 
1932 	sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
1933 
1934 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1935 				  VIRTIO_NET_CTRL_RX_PROMISC, sg))
1936 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1937 			 vi->ctrl->promisc ? "en" : "dis");
1938 
1939 	sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
1940 
1941 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1942 				  VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
1943 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1944 			 vi->ctrl->allmulti ? "en" : "dis");
1945 
1946 	uc_count = netdev_uc_count(dev);
1947 	mc_count = netdev_mc_count(dev);
1948 	/* MAC filter - use one buffer for both lists */
1949 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1950 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1951 	mac_data = buf;
1952 	if (!buf)
1953 		return;
1954 
1955 	sg_init_table(sg, 2);
1956 
1957 	/* Store the unicast list and count in the front of the buffer */
1958 	mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
1959 	i = 0;
1960 	netdev_for_each_uc_addr(ha, dev)
1961 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1962 
1963 	sg_set_buf(&sg[0], mac_data,
1964 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1965 
1966 	/* multicast list and count fill the end */
1967 	mac_data = (void *)&mac_data->macs[uc_count][0];
1968 
1969 	mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
1970 	i = 0;
1971 	netdev_for_each_mc_addr(ha, dev)
1972 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1973 
1974 	sg_set_buf(&sg[1], mac_data,
1975 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1976 
1977 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1978 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
1979 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1980 
1981 	kfree(buf);
1982 }
1983 
virtnet_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)1984 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1985 				   __be16 proto, u16 vid)
1986 {
1987 	struct virtnet_info *vi = netdev_priv(dev);
1988 	struct scatterlist sg;
1989 
1990 	vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
1991 	sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
1992 
1993 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1994 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg))
1995 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
1996 	return 0;
1997 }
1998 
virtnet_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)1999 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2000 				    __be16 proto, u16 vid)
2001 {
2002 	struct virtnet_info *vi = netdev_priv(dev);
2003 	struct scatterlist sg;
2004 
2005 	vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2006 	sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2007 
2008 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2009 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg))
2010 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
2011 	return 0;
2012 }
2013 
virtnet_clean_affinity(struct virtnet_info * vi)2014 static void virtnet_clean_affinity(struct virtnet_info *vi)
2015 {
2016 	int i;
2017 
2018 	if (vi->affinity_hint_set) {
2019 		for (i = 0; i < vi->max_queue_pairs; i++) {
2020 			virtqueue_set_affinity(vi->rq[i].vq, NULL);
2021 			virtqueue_set_affinity(vi->sq[i].vq, NULL);
2022 		}
2023 
2024 		vi->affinity_hint_set = false;
2025 	}
2026 }
2027 
virtnet_set_affinity(struct virtnet_info * vi)2028 static void virtnet_set_affinity(struct virtnet_info *vi)
2029 {
2030 	cpumask_var_t mask;
2031 	int stragglers;
2032 	int group_size;
2033 	int i, j, cpu;
2034 	int num_cpu;
2035 	int stride;
2036 
2037 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2038 		virtnet_clean_affinity(vi);
2039 		return;
2040 	}
2041 
2042 	num_cpu = num_online_cpus();
2043 	stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2044 	stragglers = num_cpu >= vi->curr_queue_pairs ?
2045 			num_cpu % vi->curr_queue_pairs :
2046 			0;
2047 	cpu = cpumask_next(-1, cpu_online_mask);
2048 
2049 	for (i = 0; i < vi->curr_queue_pairs; i++) {
2050 		group_size = stride + (i < stragglers ? 1 : 0);
2051 
2052 		for (j = 0; j < group_size; j++) {
2053 			cpumask_set_cpu(cpu, mask);
2054 			cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2055 						nr_cpu_ids, false);
2056 		}
2057 		virtqueue_set_affinity(vi->rq[i].vq, mask);
2058 		virtqueue_set_affinity(vi->sq[i].vq, mask);
2059 		__netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
2060 		cpumask_clear(mask);
2061 	}
2062 
2063 	vi->affinity_hint_set = true;
2064 	free_cpumask_var(mask);
2065 }
2066 
virtnet_cpu_online(unsigned int cpu,struct hlist_node * node)2067 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
2068 {
2069 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2070 						   node);
2071 	virtnet_set_affinity(vi);
2072 	return 0;
2073 }
2074 
virtnet_cpu_dead(unsigned int cpu,struct hlist_node * node)2075 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2076 {
2077 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2078 						   node_dead);
2079 	virtnet_set_affinity(vi);
2080 	return 0;
2081 }
2082 
virtnet_cpu_down_prep(unsigned int cpu,struct hlist_node * node)2083 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2084 {
2085 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2086 						   node);
2087 
2088 	virtnet_clean_affinity(vi);
2089 	return 0;
2090 }
2091 
2092 static enum cpuhp_state virtionet_online;
2093 
virtnet_cpu_notif_add(struct virtnet_info * vi)2094 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2095 {
2096 	int ret;
2097 
2098 	ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2099 	if (ret)
2100 		return ret;
2101 	ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2102 					       &vi->node_dead);
2103 	if (!ret)
2104 		return ret;
2105 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2106 	return ret;
2107 }
2108 
virtnet_cpu_notif_remove(struct virtnet_info * vi)2109 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2110 {
2111 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2112 	cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2113 					    &vi->node_dead);
2114 }
2115 
virtnet_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring)2116 static void virtnet_get_ringparam(struct net_device *dev,
2117 				struct ethtool_ringparam *ring)
2118 {
2119 	struct virtnet_info *vi = netdev_priv(dev);
2120 
2121 	ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2122 	ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2123 	ring->rx_pending = ring->rx_max_pending;
2124 	ring->tx_pending = ring->tx_max_pending;
2125 }
2126 
2127 
virtnet_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)2128 static void virtnet_get_drvinfo(struct net_device *dev,
2129 				struct ethtool_drvinfo *info)
2130 {
2131 	struct virtnet_info *vi = netdev_priv(dev);
2132 	struct virtio_device *vdev = vi->vdev;
2133 
2134 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2135 	strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2136 	strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
2137 
2138 }
2139 
2140 /* TODO: Eliminate OOO packets during switching */
virtnet_set_channels(struct net_device * dev,struct ethtool_channels * channels)2141 static int virtnet_set_channels(struct net_device *dev,
2142 				struct ethtool_channels *channels)
2143 {
2144 	struct virtnet_info *vi = netdev_priv(dev);
2145 	u16 queue_pairs = channels->combined_count;
2146 	int err;
2147 
2148 	/* We don't support separate rx/tx channels.
2149 	 * We don't allow setting 'other' channels.
2150 	 */
2151 	if (channels->rx_count || channels->tx_count || channels->other_count)
2152 		return -EINVAL;
2153 
2154 	if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
2155 		return -EINVAL;
2156 
2157 	/* For now we don't support modifying channels while XDP is loaded
2158 	 * also when XDP is loaded all RX queues have XDP programs so we only
2159 	 * need to check a single RX queue.
2160 	 */
2161 	if (vi->rq[0].xdp_prog)
2162 		return -EINVAL;
2163 
2164 	get_online_cpus();
2165 	err = _virtnet_set_queues(vi, queue_pairs);
2166 	if (err) {
2167 		put_online_cpus();
2168 		goto err;
2169 	}
2170 	virtnet_set_affinity(vi);
2171 	put_online_cpus();
2172 
2173 	netif_set_real_num_tx_queues(dev, queue_pairs);
2174 	netif_set_real_num_rx_queues(dev, queue_pairs);
2175  err:
2176 	return err;
2177 }
2178 
virtnet_get_strings(struct net_device * dev,u32 stringset,u8 * data)2179 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2180 {
2181 	struct virtnet_info *vi = netdev_priv(dev);
2182 	unsigned int i, j;
2183 	u8 *p = data;
2184 
2185 	switch (stringset) {
2186 	case ETH_SS_STATS:
2187 		for (i = 0; i < vi->curr_queue_pairs; i++) {
2188 			for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2189 				ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2190 						virtnet_rq_stats_desc[j].desc);
2191 		}
2192 
2193 		for (i = 0; i < vi->curr_queue_pairs; i++) {
2194 			for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2195 				ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2196 						virtnet_sq_stats_desc[j].desc);
2197 		}
2198 		break;
2199 	}
2200 }
2201 
virtnet_get_sset_count(struct net_device * dev,int sset)2202 static int virtnet_get_sset_count(struct net_device *dev, int sset)
2203 {
2204 	struct virtnet_info *vi = netdev_priv(dev);
2205 
2206 	switch (sset) {
2207 	case ETH_SS_STATS:
2208 		return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2209 					       VIRTNET_SQ_STATS_LEN);
2210 	default:
2211 		return -EOPNOTSUPP;
2212 	}
2213 }
2214 
virtnet_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)2215 static void virtnet_get_ethtool_stats(struct net_device *dev,
2216 				      struct ethtool_stats *stats, u64 *data)
2217 {
2218 	struct virtnet_info *vi = netdev_priv(dev);
2219 	unsigned int idx = 0, start, i, j;
2220 	const u8 *stats_base;
2221 	size_t offset;
2222 
2223 	for (i = 0; i < vi->curr_queue_pairs; i++) {
2224 		struct receive_queue *rq = &vi->rq[i];
2225 
2226 		stats_base = (u8 *)&rq->stats;
2227 		do {
2228 			start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2229 			for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2230 				offset = virtnet_rq_stats_desc[j].offset;
2231 				data[idx + j] = *(u64 *)(stats_base + offset);
2232 			}
2233 		} while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2234 		idx += VIRTNET_RQ_STATS_LEN;
2235 	}
2236 
2237 	for (i = 0; i < vi->curr_queue_pairs; i++) {
2238 		struct send_queue *sq = &vi->sq[i];
2239 
2240 		stats_base = (u8 *)&sq->stats;
2241 		do {
2242 			start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2243 			for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2244 				offset = virtnet_sq_stats_desc[j].offset;
2245 				data[idx + j] = *(u64 *)(stats_base + offset);
2246 			}
2247 		} while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2248 		idx += VIRTNET_SQ_STATS_LEN;
2249 	}
2250 }
2251 
virtnet_get_channels(struct net_device * dev,struct ethtool_channels * channels)2252 static void virtnet_get_channels(struct net_device *dev,
2253 				 struct ethtool_channels *channels)
2254 {
2255 	struct virtnet_info *vi = netdev_priv(dev);
2256 
2257 	channels->combined_count = vi->curr_queue_pairs;
2258 	channels->max_combined = vi->max_queue_pairs;
2259 	channels->max_other = 0;
2260 	channels->rx_count = 0;
2261 	channels->tx_count = 0;
2262 	channels->other_count = 0;
2263 }
2264 
virtnet_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)2265 static int virtnet_set_link_ksettings(struct net_device *dev,
2266 				      const struct ethtool_link_ksettings *cmd)
2267 {
2268 	struct virtnet_info *vi = netdev_priv(dev);
2269 
2270 	return ethtool_virtdev_set_link_ksettings(dev, cmd,
2271 						  &vi->speed, &vi->duplex);
2272 }
2273 
virtnet_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)2274 static int virtnet_get_link_ksettings(struct net_device *dev,
2275 				      struct ethtool_link_ksettings *cmd)
2276 {
2277 	struct virtnet_info *vi = netdev_priv(dev);
2278 
2279 	cmd->base.speed = vi->speed;
2280 	cmd->base.duplex = vi->duplex;
2281 	cmd->base.port = PORT_OTHER;
2282 
2283 	return 0;
2284 }
2285 
virtnet_set_coalesce(struct net_device * dev,struct ethtool_coalesce * ec)2286 static int virtnet_set_coalesce(struct net_device *dev,
2287 				struct ethtool_coalesce *ec)
2288 {
2289 	struct virtnet_info *vi = netdev_priv(dev);
2290 	int i, napi_weight;
2291 
2292 	if (ec->tx_max_coalesced_frames > 1 ||
2293 	    ec->rx_max_coalesced_frames != 1)
2294 		return -EINVAL;
2295 
2296 	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
2297 	if (napi_weight ^ vi->sq[0].napi.weight) {
2298 		if (dev->flags & IFF_UP)
2299 			return -EBUSY;
2300 		for (i = 0; i < vi->max_queue_pairs; i++)
2301 			vi->sq[i].napi.weight = napi_weight;
2302 	}
2303 
2304 	return 0;
2305 }
2306 
virtnet_get_coalesce(struct net_device * dev,struct ethtool_coalesce * ec)2307 static int virtnet_get_coalesce(struct net_device *dev,
2308 				struct ethtool_coalesce *ec)
2309 {
2310 	struct ethtool_coalesce ec_default = {
2311 		.cmd = ETHTOOL_GCOALESCE,
2312 		.rx_max_coalesced_frames = 1,
2313 	};
2314 	struct virtnet_info *vi = netdev_priv(dev);
2315 
2316 	memcpy(ec, &ec_default, sizeof(ec_default));
2317 
2318 	if (vi->sq[0].napi.weight)
2319 		ec->tx_max_coalesced_frames = 1;
2320 
2321 	return 0;
2322 }
2323 
virtnet_init_settings(struct net_device * dev)2324 static void virtnet_init_settings(struct net_device *dev)
2325 {
2326 	struct virtnet_info *vi = netdev_priv(dev);
2327 
2328 	vi->speed = SPEED_UNKNOWN;
2329 	vi->duplex = DUPLEX_UNKNOWN;
2330 }
2331 
virtnet_update_settings(struct virtnet_info * vi)2332 static void virtnet_update_settings(struct virtnet_info *vi)
2333 {
2334 	u32 speed;
2335 	u8 duplex;
2336 
2337 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2338 		return;
2339 
2340 	virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
2341 
2342 	if (ethtool_validate_speed(speed))
2343 		vi->speed = speed;
2344 
2345 	virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
2346 
2347 	if (ethtool_validate_duplex(duplex))
2348 		vi->duplex = duplex;
2349 }
2350 
2351 static const struct ethtool_ops virtnet_ethtool_ops = {
2352 	.supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
2353 	.get_drvinfo = virtnet_get_drvinfo,
2354 	.get_link = ethtool_op_get_link,
2355 	.get_ringparam = virtnet_get_ringparam,
2356 	.get_strings = virtnet_get_strings,
2357 	.get_sset_count = virtnet_get_sset_count,
2358 	.get_ethtool_stats = virtnet_get_ethtool_stats,
2359 	.set_channels = virtnet_set_channels,
2360 	.get_channels = virtnet_get_channels,
2361 	.get_ts_info = ethtool_op_get_ts_info,
2362 	.get_link_ksettings = virtnet_get_link_ksettings,
2363 	.set_link_ksettings = virtnet_set_link_ksettings,
2364 	.set_coalesce = virtnet_set_coalesce,
2365 	.get_coalesce = virtnet_get_coalesce,
2366 };
2367 
virtnet_freeze_down(struct virtio_device * vdev)2368 static void virtnet_freeze_down(struct virtio_device *vdev)
2369 {
2370 	struct virtnet_info *vi = vdev->priv;
2371 	int i;
2372 
2373 	/* Make sure no work handler is accessing the device */
2374 	flush_work(&vi->config_work);
2375 
2376 	netif_tx_lock_bh(vi->dev);
2377 	netif_device_detach(vi->dev);
2378 	netif_tx_unlock_bh(vi->dev);
2379 	cancel_delayed_work_sync(&vi->refill);
2380 
2381 	if (netif_running(vi->dev)) {
2382 		for (i = 0; i < vi->max_queue_pairs; i++) {
2383 			napi_disable(&vi->rq[i].napi);
2384 			virtnet_napi_tx_disable(&vi->sq[i].napi);
2385 		}
2386 	}
2387 }
2388 
2389 static int init_vqs(struct virtnet_info *vi);
2390 
virtnet_restore_up(struct virtio_device * vdev)2391 static int virtnet_restore_up(struct virtio_device *vdev)
2392 {
2393 	struct virtnet_info *vi = vdev->priv;
2394 	int err, i;
2395 
2396 	err = init_vqs(vi);
2397 	if (err)
2398 		return err;
2399 
2400 	virtio_device_ready(vdev);
2401 
2402 	if (netif_running(vi->dev)) {
2403 		for (i = 0; i < vi->curr_queue_pairs; i++)
2404 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2405 				schedule_delayed_work(&vi->refill, 0);
2406 
2407 		for (i = 0; i < vi->max_queue_pairs; i++) {
2408 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2409 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2410 					       &vi->sq[i].napi);
2411 		}
2412 	}
2413 
2414 	netif_tx_lock_bh(vi->dev);
2415 	netif_device_attach(vi->dev);
2416 	netif_tx_unlock_bh(vi->dev);
2417 	return err;
2418 }
2419 
virtnet_set_guest_offloads(struct virtnet_info * vi,u64 offloads)2420 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2421 {
2422 	struct scatterlist sg;
2423 	vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
2424 
2425 	sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
2426 
2427 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2428 				  VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
2429 		dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
2430 		return -EINVAL;
2431 	}
2432 
2433 	return 0;
2434 }
2435 
virtnet_clear_guest_offloads(struct virtnet_info * vi)2436 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
2437 {
2438 	u64 offloads = 0;
2439 
2440 	if (!vi->guest_offloads)
2441 		return 0;
2442 
2443 	return virtnet_set_guest_offloads(vi, offloads);
2444 }
2445 
virtnet_restore_guest_offloads(struct virtnet_info * vi)2446 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
2447 {
2448 	u64 offloads = vi->guest_offloads;
2449 
2450 	if (!vi->guest_offloads)
2451 		return 0;
2452 
2453 	return virtnet_set_guest_offloads(vi, offloads);
2454 }
2455 
virtnet_xdp_set(struct net_device * dev,struct bpf_prog * prog,struct netlink_ext_ack * extack)2456 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
2457 			   struct netlink_ext_ack *extack)
2458 {
2459 	unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2460 	struct virtnet_info *vi = netdev_priv(dev);
2461 	struct bpf_prog *old_prog;
2462 	u16 xdp_qp = 0, curr_qp;
2463 	int i, err;
2464 
2465 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
2466 	    && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2467 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2468 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
2469 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
2470 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
2471 		NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO/CSUM, disable LRO/CSUM first");
2472 		return -EOPNOTSUPP;
2473 	}
2474 
2475 	if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
2476 		NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
2477 		return -EINVAL;
2478 	}
2479 
2480 	if (dev->mtu > max_sz) {
2481 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
2482 		netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2483 		return -EINVAL;
2484 	}
2485 
2486 	curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2487 	if (prog)
2488 		xdp_qp = nr_cpu_ids;
2489 
2490 	/* XDP requires extra queues for XDP_TX */
2491 	if (curr_qp + xdp_qp > vi->max_queue_pairs) {
2492 		netdev_warn(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n",
2493 			    curr_qp + xdp_qp, vi->max_queue_pairs);
2494 		xdp_qp = 0;
2495 	}
2496 
2497 	old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
2498 	if (!prog && !old_prog)
2499 		return 0;
2500 
2501 	if (prog)
2502 		bpf_prog_add(prog, vi->max_queue_pairs - 1);
2503 
2504 	/* Make sure NAPI is not using any XDP TX queues for RX. */
2505 	if (netif_running(dev)) {
2506 		for (i = 0; i < vi->max_queue_pairs; i++) {
2507 			napi_disable(&vi->rq[i].napi);
2508 			virtnet_napi_tx_disable(&vi->sq[i].napi);
2509 		}
2510 	}
2511 
2512 	if (!prog) {
2513 		for (i = 0; i < vi->max_queue_pairs; i++) {
2514 			rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2515 			if (i == 0)
2516 				virtnet_restore_guest_offloads(vi);
2517 		}
2518 		synchronize_net();
2519 	}
2520 
2521 	err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
2522 	if (err)
2523 		goto err;
2524 	netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
2525 	vi->xdp_queue_pairs = xdp_qp;
2526 
2527 	if (prog) {
2528 		vi->xdp_enabled = true;
2529 		for (i = 0; i < vi->max_queue_pairs; i++) {
2530 			rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2531 			if (i == 0 && !old_prog)
2532 				virtnet_clear_guest_offloads(vi);
2533 		}
2534 	} else {
2535 		vi->xdp_enabled = false;
2536 	}
2537 
2538 	for (i = 0; i < vi->max_queue_pairs; i++) {
2539 		if (old_prog)
2540 			bpf_prog_put(old_prog);
2541 		if (netif_running(dev)) {
2542 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2543 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2544 					       &vi->sq[i].napi);
2545 		}
2546 	}
2547 
2548 	return 0;
2549 
2550 err:
2551 	if (!prog) {
2552 		virtnet_clear_guest_offloads(vi);
2553 		for (i = 0; i < vi->max_queue_pairs; i++)
2554 			rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
2555 	}
2556 
2557 	if (netif_running(dev)) {
2558 		for (i = 0; i < vi->max_queue_pairs; i++) {
2559 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2560 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2561 					       &vi->sq[i].napi);
2562 		}
2563 	}
2564 	if (prog)
2565 		bpf_prog_sub(prog, vi->max_queue_pairs - 1);
2566 	return err;
2567 }
2568 
virtnet_xdp(struct net_device * dev,struct netdev_bpf * xdp)2569 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2570 {
2571 	switch (xdp->command) {
2572 	case XDP_SETUP_PROG:
2573 		return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
2574 	default:
2575 		return -EINVAL;
2576 	}
2577 }
2578 
virtnet_get_phys_port_name(struct net_device * dev,char * buf,size_t len)2579 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2580 				      size_t len)
2581 {
2582 	struct virtnet_info *vi = netdev_priv(dev);
2583 	int ret;
2584 
2585 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2586 		return -EOPNOTSUPP;
2587 
2588 	ret = snprintf(buf, len, "sby");
2589 	if (ret >= len)
2590 		return -EOPNOTSUPP;
2591 
2592 	return 0;
2593 }
2594 
virtnet_set_features(struct net_device * dev,netdev_features_t features)2595 static int virtnet_set_features(struct net_device *dev,
2596 				netdev_features_t features)
2597 {
2598 	struct virtnet_info *vi = netdev_priv(dev);
2599 	u64 offloads;
2600 	int err;
2601 
2602 	if ((dev->features ^ features) & NETIF_F_LRO) {
2603 		if (vi->xdp_enabled)
2604 			return -EBUSY;
2605 
2606 		if (features & NETIF_F_LRO)
2607 			offloads = vi->guest_offloads_capable;
2608 		else
2609 			offloads = vi->guest_offloads_capable &
2610 				   ~GUEST_OFFLOAD_LRO_MASK;
2611 
2612 		err = virtnet_set_guest_offloads(vi, offloads);
2613 		if (err)
2614 			return err;
2615 		vi->guest_offloads = offloads;
2616 	}
2617 
2618 	return 0;
2619 }
2620 
2621 static const struct net_device_ops virtnet_netdev = {
2622 	.ndo_open            = virtnet_open,
2623 	.ndo_stop   	     = virtnet_close,
2624 	.ndo_start_xmit      = start_xmit,
2625 	.ndo_validate_addr   = eth_validate_addr,
2626 	.ndo_set_mac_address = virtnet_set_mac_address,
2627 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
2628 	.ndo_get_stats64     = virtnet_stats,
2629 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
2630 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
2631 	.ndo_bpf		= virtnet_xdp,
2632 	.ndo_xdp_xmit		= virtnet_xdp_xmit,
2633 	.ndo_features_check	= passthru_features_check,
2634 	.ndo_get_phys_port_name	= virtnet_get_phys_port_name,
2635 	.ndo_set_features	= virtnet_set_features,
2636 };
2637 
virtnet_config_changed_work(struct work_struct * work)2638 static void virtnet_config_changed_work(struct work_struct *work)
2639 {
2640 	struct virtnet_info *vi =
2641 		container_of(work, struct virtnet_info, config_work);
2642 	u16 v;
2643 
2644 	if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2645 				 struct virtio_net_config, status, &v) < 0)
2646 		return;
2647 
2648 	if (v & VIRTIO_NET_S_ANNOUNCE) {
2649 		netdev_notify_peers(vi->dev);
2650 		virtnet_ack_link_announce(vi);
2651 	}
2652 
2653 	/* Ignore unknown (future) status bits */
2654 	v &= VIRTIO_NET_S_LINK_UP;
2655 
2656 	if (vi->status == v)
2657 		return;
2658 
2659 	vi->status = v;
2660 
2661 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
2662 		virtnet_update_settings(vi);
2663 		netif_carrier_on(vi->dev);
2664 		netif_tx_wake_all_queues(vi->dev);
2665 	} else {
2666 		netif_carrier_off(vi->dev);
2667 		netif_tx_stop_all_queues(vi->dev);
2668 	}
2669 }
2670 
virtnet_config_changed(struct virtio_device * vdev)2671 static void virtnet_config_changed(struct virtio_device *vdev)
2672 {
2673 	struct virtnet_info *vi = vdev->priv;
2674 
2675 	schedule_work(&vi->config_work);
2676 }
2677 
virtnet_free_queues(struct virtnet_info * vi)2678 static void virtnet_free_queues(struct virtnet_info *vi)
2679 {
2680 	int i;
2681 
2682 	for (i = 0; i < vi->max_queue_pairs; i++) {
2683 		__netif_napi_del(&vi->rq[i].napi);
2684 		__netif_napi_del(&vi->sq[i].napi);
2685 	}
2686 
2687 	/* We called __netif_napi_del(),
2688 	 * we need to respect an RCU grace period before freeing vi->rq
2689 	 */
2690 	synchronize_net();
2691 
2692 	kfree(vi->rq);
2693 	kfree(vi->sq);
2694 	kfree(vi->ctrl);
2695 }
2696 
_free_receive_bufs(struct virtnet_info * vi)2697 static void _free_receive_bufs(struct virtnet_info *vi)
2698 {
2699 	struct bpf_prog *old_prog;
2700 	int i;
2701 
2702 	for (i = 0; i < vi->max_queue_pairs; i++) {
2703 		while (vi->rq[i].pages)
2704 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
2705 
2706 		old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2707 		RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2708 		if (old_prog)
2709 			bpf_prog_put(old_prog);
2710 	}
2711 }
2712 
free_receive_bufs(struct virtnet_info * vi)2713 static void free_receive_bufs(struct virtnet_info *vi)
2714 {
2715 	rtnl_lock();
2716 	_free_receive_bufs(vi);
2717 	rtnl_unlock();
2718 }
2719 
free_receive_page_frags(struct virtnet_info * vi)2720 static void free_receive_page_frags(struct virtnet_info *vi)
2721 {
2722 	int i;
2723 	for (i = 0; i < vi->max_queue_pairs; i++)
2724 		if (vi->rq[i].alloc_frag.page)
2725 			put_page(vi->rq[i].alloc_frag.page);
2726 }
2727 
free_unused_bufs(struct virtnet_info * vi)2728 static void free_unused_bufs(struct virtnet_info *vi)
2729 {
2730 	void *buf;
2731 	int i;
2732 
2733 	for (i = 0; i < vi->max_queue_pairs; i++) {
2734 		struct virtqueue *vq = vi->sq[i].vq;
2735 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2736 			if (!is_xdp_frame(buf))
2737 				dev_kfree_skb(buf);
2738 			else
2739 				xdp_return_frame(ptr_to_xdp(buf));
2740 		}
2741 	}
2742 
2743 	for (i = 0; i < vi->max_queue_pairs; i++) {
2744 		struct virtqueue *vq = vi->rq[i].vq;
2745 
2746 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2747 			if (vi->mergeable_rx_bufs) {
2748 				put_page(virt_to_head_page(buf));
2749 			} else if (vi->big_packets) {
2750 				give_pages(&vi->rq[i], buf);
2751 			} else {
2752 				put_page(virt_to_head_page(buf));
2753 			}
2754 		}
2755 	}
2756 }
2757 
virtnet_del_vqs(struct virtnet_info * vi)2758 static void virtnet_del_vqs(struct virtnet_info *vi)
2759 {
2760 	struct virtio_device *vdev = vi->vdev;
2761 
2762 	virtnet_clean_affinity(vi);
2763 
2764 	vdev->config->del_vqs(vdev);
2765 
2766 	virtnet_free_queues(vi);
2767 }
2768 
2769 /* How large should a single buffer be so a queue full of these can fit at
2770  * least one full packet?
2771  * Logic below assumes the mergeable buffer header is used.
2772  */
mergeable_min_buf_len(struct virtnet_info * vi,struct virtqueue * vq)2773 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2774 {
2775 	const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2776 	unsigned int rq_size = virtqueue_get_vring_size(vq);
2777 	unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2778 	unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2779 	unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2780 
2781 	return max(max(min_buf_len, hdr_len) - hdr_len,
2782 		   (unsigned int)GOOD_PACKET_LEN);
2783 }
2784 
virtnet_find_vqs(struct virtnet_info * vi)2785 static int virtnet_find_vqs(struct virtnet_info *vi)
2786 {
2787 	vq_callback_t **callbacks;
2788 	struct virtqueue **vqs;
2789 	int ret = -ENOMEM;
2790 	int i, total_vqs;
2791 	const char **names;
2792 	bool *ctx;
2793 
2794 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2795 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2796 	 * possible control vq.
2797 	 */
2798 	total_vqs = vi->max_queue_pairs * 2 +
2799 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2800 
2801 	/* Allocate space for find_vqs parameters */
2802 	vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
2803 	if (!vqs)
2804 		goto err_vq;
2805 	callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
2806 	if (!callbacks)
2807 		goto err_callback;
2808 	names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
2809 	if (!names)
2810 		goto err_names;
2811 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
2812 		ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
2813 		if (!ctx)
2814 			goto err_ctx;
2815 	} else {
2816 		ctx = NULL;
2817 	}
2818 
2819 	/* Parameters for control virtqueue, if any */
2820 	if (vi->has_cvq) {
2821 		callbacks[total_vqs - 1] = NULL;
2822 		names[total_vqs - 1] = "control";
2823 	}
2824 
2825 	/* Allocate/initialize parameters for send/receive virtqueues */
2826 	for (i = 0; i < vi->max_queue_pairs; i++) {
2827 		callbacks[rxq2vq(i)] = skb_recv_done;
2828 		callbacks[txq2vq(i)] = skb_xmit_done;
2829 		sprintf(vi->rq[i].name, "input.%d", i);
2830 		sprintf(vi->sq[i].name, "output.%d", i);
2831 		names[rxq2vq(i)] = vi->rq[i].name;
2832 		names[txq2vq(i)] = vi->sq[i].name;
2833 		if (ctx)
2834 			ctx[rxq2vq(i)] = true;
2835 	}
2836 
2837 	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
2838 					 names, ctx, NULL);
2839 	if (ret)
2840 		goto err_find;
2841 
2842 	if (vi->has_cvq) {
2843 		vi->cvq = vqs[total_vqs - 1];
2844 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
2845 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2846 	}
2847 
2848 	for (i = 0; i < vi->max_queue_pairs; i++) {
2849 		vi->rq[i].vq = vqs[rxq2vq(i)];
2850 		vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
2851 		vi->sq[i].vq = vqs[txq2vq(i)];
2852 	}
2853 
2854 	/* run here: ret == 0. */
2855 
2856 
2857 err_find:
2858 	kfree(ctx);
2859 err_ctx:
2860 	kfree(names);
2861 err_names:
2862 	kfree(callbacks);
2863 err_callback:
2864 	kfree(vqs);
2865 err_vq:
2866 	return ret;
2867 }
2868 
virtnet_alloc_queues(struct virtnet_info * vi)2869 static int virtnet_alloc_queues(struct virtnet_info *vi)
2870 {
2871 	int i;
2872 
2873 	if (vi->has_cvq) {
2874 		vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
2875 		if (!vi->ctrl)
2876 			goto err_ctrl;
2877 	} else {
2878 		vi->ctrl = NULL;
2879 	}
2880 	vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
2881 	if (!vi->sq)
2882 		goto err_sq;
2883 	vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
2884 	if (!vi->rq)
2885 		goto err_rq;
2886 
2887 	INIT_DELAYED_WORK(&vi->refill, refill_work);
2888 	for (i = 0; i < vi->max_queue_pairs; i++) {
2889 		vi->rq[i].pages = NULL;
2890 		netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2891 			       napi_weight);
2892 		netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2893 				  napi_tx ? napi_weight : 0);
2894 
2895 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
2896 		ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
2897 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2898 
2899 		u64_stats_init(&vi->rq[i].stats.syncp);
2900 		u64_stats_init(&vi->sq[i].stats.syncp);
2901 	}
2902 
2903 	return 0;
2904 
2905 err_rq:
2906 	kfree(vi->sq);
2907 err_sq:
2908 	kfree(vi->ctrl);
2909 err_ctrl:
2910 	return -ENOMEM;
2911 }
2912 
init_vqs(struct virtnet_info * vi)2913 static int init_vqs(struct virtnet_info *vi)
2914 {
2915 	int ret;
2916 
2917 	/* Allocate send & receive queues */
2918 	ret = virtnet_alloc_queues(vi);
2919 	if (ret)
2920 		goto err;
2921 
2922 	ret = virtnet_find_vqs(vi);
2923 	if (ret)
2924 		goto err_free;
2925 
2926 	get_online_cpus();
2927 	virtnet_set_affinity(vi);
2928 	put_online_cpus();
2929 
2930 	return 0;
2931 
2932 err_free:
2933 	virtnet_free_queues(vi);
2934 err:
2935 	return ret;
2936 }
2937 
2938 #ifdef CONFIG_SYSFS
mergeable_rx_buffer_size_show(struct netdev_rx_queue * queue,char * buf)2939 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2940 		char *buf)
2941 {
2942 	struct virtnet_info *vi = netdev_priv(queue->dev);
2943 	unsigned int queue_index = get_netdev_rx_queue_index(queue);
2944 	unsigned int headroom = virtnet_get_headroom(vi);
2945 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
2946 	struct ewma_pkt_len *avg;
2947 
2948 	BUG_ON(queue_index >= vi->max_queue_pairs);
2949 	avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2950 	return sprintf(buf, "%u\n",
2951 		       get_mergeable_buf_len(&vi->rq[queue_index], avg,
2952 				       SKB_DATA_ALIGN(headroom + tailroom)));
2953 }
2954 
2955 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2956 	__ATTR_RO(mergeable_rx_buffer_size);
2957 
2958 static struct attribute *virtio_net_mrg_rx_attrs[] = {
2959 	&mergeable_rx_buffer_size_attribute.attr,
2960 	NULL
2961 };
2962 
2963 static const struct attribute_group virtio_net_mrg_rx_group = {
2964 	.name = "virtio_net",
2965 	.attrs = virtio_net_mrg_rx_attrs
2966 };
2967 #endif
2968 
virtnet_fail_on_feature(struct virtio_device * vdev,unsigned int fbit,const char * fname,const char * dname)2969 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2970 				    unsigned int fbit,
2971 				    const char *fname, const char *dname)
2972 {
2973 	if (!virtio_has_feature(vdev, fbit))
2974 		return false;
2975 
2976 	dev_err(&vdev->dev, "device advertises feature %s but not %s",
2977 		fname, dname);
2978 
2979 	return true;
2980 }
2981 
2982 #define VIRTNET_FAIL_ON(vdev, fbit, dbit)			\
2983 	virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2984 
virtnet_validate_features(struct virtio_device * vdev)2985 static bool virtnet_validate_features(struct virtio_device *vdev)
2986 {
2987 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2988 	    (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2989 			     "VIRTIO_NET_F_CTRL_VQ") ||
2990 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2991 			     "VIRTIO_NET_F_CTRL_VQ") ||
2992 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2993 			     "VIRTIO_NET_F_CTRL_VQ") ||
2994 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2995 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2996 			     "VIRTIO_NET_F_CTRL_VQ"))) {
2997 		return false;
2998 	}
2999 
3000 	return true;
3001 }
3002 
3003 #define MIN_MTU ETH_MIN_MTU
3004 #define MAX_MTU ETH_MAX_MTU
3005 
virtnet_validate(struct virtio_device * vdev)3006 static int virtnet_validate(struct virtio_device *vdev)
3007 {
3008 	if (!vdev->config->get) {
3009 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
3010 			__func__);
3011 		return -EINVAL;
3012 	}
3013 
3014 	if (!virtnet_validate_features(vdev))
3015 		return -EINVAL;
3016 
3017 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3018 		int mtu = virtio_cread16(vdev,
3019 					 offsetof(struct virtio_net_config,
3020 						  mtu));
3021 		if (mtu < MIN_MTU)
3022 			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3023 	}
3024 
3025 	return 0;
3026 }
3027 
virtnet_probe(struct virtio_device * vdev)3028 static int virtnet_probe(struct virtio_device *vdev)
3029 {
3030 	int i, err = -ENOMEM;
3031 	struct net_device *dev;
3032 	struct virtnet_info *vi;
3033 	u16 max_queue_pairs;
3034 	int mtu;
3035 
3036 	/* Find if host supports multiqueue virtio_net device */
3037 	err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
3038 				   struct virtio_net_config,
3039 				   max_virtqueue_pairs, &max_queue_pairs);
3040 
3041 	/* We need at least 2 queue's */
3042 	if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3043 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3044 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3045 		max_queue_pairs = 1;
3046 
3047 	/* Allocate ourselves a network device with room for our info */
3048 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
3049 	if (!dev)
3050 		return -ENOMEM;
3051 
3052 	/* Set up network device as normal. */
3053 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3054 			   IFF_TX_SKB_NO_LINEAR;
3055 	dev->netdev_ops = &virtnet_netdev;
3056 	dev->features = NETIF_F_HIGHDMA;
3057 
3058 	dev->ethtool_ops = &virtnet_ethtool_ops;
3059 	SET_NETDEV_DEV(dev, &vdev->dev);
3060 
3061 	/* Do we support "hardware" checksums? */
3062 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
3063 		/* This opens up the world of extra features. */
3064 		dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3065 		if (csum)
3066 			dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
3067 
3068 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
3069 			dev->hw_features |= NETIF_F_TSO
3070 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
3071 		}
3072 		/* Individual feature bits: what can host handle? */
3073 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
3074 			dev->hw_features |= NETIF_F_TSO;
3075 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
3076 			dev->hw_features |= NETIF_F_TSO6;
3077 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
3078 			dev->hw_features |= NETIF_F_TSO_ECN;
3079 
3080 		dev->features |= NETIF_F_GSO_ROBUST;
3081 
3082 		if (gso)
3083 			dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
3084 		/* (!csum && gso) case will be fixed by register_netdev() */
3085 	}
3086 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
3087 		dev->features |= NETIF_F_RXCSUM;
3088 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3089 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
3090 		dev->features |= NETIF_F_LRO;
3091 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
3092 		dev->hw_features |= NETIF_F_LRO;
3093 
3094 	dev->vlan_features = dev->features;
3095 
3096 	/* MTU range: 68 - 65535 */
3097 	dev->min_mtu = MIN_MTU;
3098 	dev->max_mtu = MAX_MTU;
3099 
3100 	/* Configuration may specify what MAC to use.  Otherwise random. */
3101 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
3102 		virtio_cread_bytes(vdev,
3103 				   offsetof(struct virtio_net_config, mac),
3104 				   dev->dev_addr, dev->addr_len);
3105 	else
3106 		eth_hw_addr_random(dev);
3107 
3108 	/* Set up our device-specific information */
3109 	vi = netdev_priv(dev);
3110 	vi->dev = dev;
3111 	vi->vdev = vdev;
3112 	vdev->priv = vi;
3113 
3114 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
3115 
3116 	/* If we can receive ANY GSO packets, we must allocate large ones. */
3117 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3118 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3119 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
3120 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
3121 		vi->big_packets = true;
3122 
3123 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
3124 		vi->mergeable_rx_bufs = true;
3125 
3126 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
3127 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3128 		vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3129 	else
3130 		vi->hdr_len = sizeof(struct virtio_net_hdr);
3131 
3132 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
3133 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3134 		vi->any_header_sg = true;
3135 
3136 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3137 		vi->has_cvq = true;
3138 
3139 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3140 		mtu = virtio_cread16(vdev,
3141 				     offsetof(struct virtio_net_config,
3142 					      mtu));
3143 		if (mtu < dev->min_mtu) {
3144 			/* Should never trigger: MTU was previously validated
3145 			 * in virtnet_validate.
3146 			 */
3147 			dev_err(&vdev->dev,
3148 				"device MTU appears to have changed it is now %d < %d",
3149 				mtu, dev->min_mtu);
3150 			err = -EINVAL;
3151 			goto free;
3152 		}
3153 
3154 		dev->mtu = mtu;
3155 		dev->max_mtu = mtu;
3156 
3157 		/* TODO: size buffers correctly in this case. */
3158 		if (dev->mtu > ETH_DATA_LEN)
3159 			vi->big_packets = true;
3160 	}
3161 
3162 	if (vi->any_header_sg)
3163 		dev->needed_headroom = vi->hdr_len;
3164 
3165 	/* Enable multiqueue by default */
3166 	if (num_online_cpus() >= max_queue_pairs)
3167 		vi->curr_queue_pairs = max_queue_pairs;
3168 	else
3169 		vi->curr_queue_pairs = num_online_cpus();
3170 	vi->max_queue_pairs = max_queue_pairs;
3171 
3172 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
3173 	err = init_vqs(vi);
3174 	if (err)
3175 		goto free;
3176 
3177 #ifdef CONFIG_SYSFS
3178 	if (vi->mergeable_rx_bufs)
3179 		dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3180 #endif
3181 	netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
3182 	netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
3183 
3184 	virtnet_init_settings(dev);
3185 
3186 	if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3187 		vi->failover = net_failover_create(vi->dev);
3188 		if (IS_ERR(vi->failover)) {
3189 			err = PTR_ERR(vi->failover);
3190 			goto free_vqs;
3191 		}
3192 	}
3193 
3194 	err = register_netdev(dev);
3195 	if (err) {
3196 		pr_debug("virtio_net: registering device failed\n");
3197 		goto free_failover;
3198 	}
3199 
3200 	virtio_device_ready(vdev);
3201 
3202 	err = virtnet_cpu_notif_add(vi);
3203 	if (err) {
3204 		pr_debug("virtio_net: registering cpu notifier failed\n");
3205 		goto free_unregister_netdev;
3206 	}
3207 
3208 	virtnet_set_queues(vi, vi->curr_queue_pairs);
3209 
3210 	/* Assume link up if device can't report link status,
3211 	   otherwise get link status from config. */
3212 	netif_carrier_off(dev);
3213 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
3214 		schedule_work(&vi->config_work);
3215 	} else {
3216 		vi->status = VIRTIO_NET_S_LINK_UP;
3217 		virtnet_update_settings(vi);
3218 		netif_carrier_on(dev);
3219 	}
3220 
3221 	for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
3222 		if (virtio_has_feature(vi->vdev, guest_offloads[i]))
3223 			set_bit(guest_offloads[i], &vi->guest_offloads);
3224 	vi->guest_offloads_capable = vi->guest_offloads;
3225 
3226 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3227 		 dev->name, max_queue_pairs);
3228 
3229 	return 0;
3230 
3231 free_unregister_netdev:
3232 	vi->vdev->config->reset(vdev);
3233 
3234 	unregister_netdev(dev);
3235 free_failover:
3236 	net_failover_destroy(vi->failover);
3237 free_vqs:
3238 	cancel_delayed_work_sync(&vi->refill);
3239 	free_receive_page_frags(vi);
3240 	virtnet_del_vqs(vi);
3241 free:
3242 	free_netdev(dev);
3243 	return err;
3244 }
3245 
remove_vq_common(struct virtnet_info * vi)3246 static void remove_vq_common(struct virtnet_info *vi)
3247 {
3248 	vi->vdev->config->reset(vi->vdev);
3249 
3250 	/* Free unused buffers in both send and recv, if any. */
3251 	free_unused_bufs(vi);
3252 
3253 	free_receive_bufs(vi);
3254 
3255 	free_receive_page_frags(vi);
3256 
3257 	virtnet_del_vqs(vi);
3258 }
3259 
virtnet_remove(struct virtio_device * vdev)3260 static void virtnet_remove(struct virtio_device *vdev)
3261 {
3262 	struct virtnet_info *vi = vdev->priv;
3263 
3264 	virtnet_cpu_notif_remove(vi);
3265 
3266 	/* Make sure no work handler is accessing the device. */
3267 	flush_work(&vi->config_work);
3268 
3269 	unregister_netdev(vi->dev);
3270 
3271 	net_failover_destroy(vi->failover);
3272 
3273 	remove_vq_common(vi);
3274 
3275 	free_netdev(vi->dev);
3276 }
3277 
virtnet_freeze(struct virtio_device * vdev)3278 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
3279 {
3280 	struct virtnet_info *vi = vdev->priv;
3281 
3282 	virtnet_cpu_notif_remove(vi);
3283 	virtnet_freeze_down(vdev);
3284 	remove_vq_common(vi);
3285 
3286 	return 0;
3287 }
3288 
virtnet_restore(struct virtio_device * vdev)3289 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
3290 {
3291 	struct virtnet_info *vi = vdev->priv;
3292 	int err;
3293 
3294 	err = virtnet_restore_up(vdev);
3295 	if (err)
3296 		return err;
3297 	virtnet_set_queues(vi, vi->curr_queue_pairs);
3298 
3299 	err = virtnet_cpu_notif_add(vi);
3300 	if (err)
3301 		return err;
3302 
3303 	return 0;
3304 }
3305 
3306 static struct virtio_device_id id_table[] = {
3307 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3308 	{ 0 },
3309 };
3310 
3311 #define VIRTNET_FEATURES \
3312 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3313 	VIRTIO_NET_F_MAC, \
3314 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3315 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3316 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3317 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3318 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3319 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3320 	VIRTIO_NET_F_CTRL_MAC_ADDR, \
3321 	VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
3322 	VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
3323 
3324 static unsigned int features[] = {
3325 	VIRTNET_FEATURES,
3326 };
3327 
3328 static unsigned int features_legacy[] = {
3329 	VIRTNET_FEATURES,
3330 	VIRTIO_NET_F_GSO,
3331 	VIRTIO_F_ANY_LAYOUT,
3332 };
3333 
3334 static struct virtio_driver virtio_net_driver = {
3335 	.feature_table = features,
3336 	.feature_table_size = ARRAY_SIZE(features),
3337 	.feature_table_legacy = features_legacy,
3338 	.feature_table_size_legacy = ARRAY_SIZE(features_legacy),
3339 	.driver.name =	KBUILD_MODNAME,
3340 	.driver.owner =	THIS_MODULE,
3341 	.id_table =	id_table,
3342 	.validate =	virtnet_validate,
3343 	.probe =	virtnet_probe,
3344 	.remove =	virtnet_remove,
3345 	.config_changed = virtnet_config_changed,
3346 #ifdef CONFIG_PM_SLEEP
3347 	.freeze =	virtnet_freeze,
3348 	.restore =	virtnet_restore,
3349 #endif
3350 };
3351 
virtio_net_driver_init(void)3352 static __init int virtio_net_driver_init(void)
3353 {
3354 	int ret;
3355 
3356 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
3357 				      virtnet_cpu_online,
3358 				      virtnet_cpu_down_prep);
3359 	if (ret < 0)
3360 		goto out;
3361 	virtionet_online = ret;
3362 	ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
3363 				      NULL, virtnet_cpu_dead);
3364 	if (ret)
3365 		goto err_dead;
3366 
3367         ret = register_virtio_driver(&virtio_net_driver);
3368 	if (ret)
3369 		goto err_virtio;
3370 	return 0;
3371 err_virtio:
3372 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3373 err_dead:
3374 	cpuhp_remove_multi_state(virtionet_online);
3375 out:
3376 	return ret;
3377 }
3378 module_init(virtio_net_driver_init);
3379 
virtio_net_driver_exit(void)3380 static __exit void virtio_net_driver_exit(void)
3381 {
3382 	unregister_virtio_driver(&virtio_net_driver);
3383 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
3384 	cpuhp_remove_multi_state(virtionet_online);
3385 }
3386 module_exit(virtio_net_driver_exit);
3387 
3388 MODULE_DEVICE_TABLE(virtio, id_table);
3389 MODULE_DESCRIPTION("Virtio network driver");
3390 MODULE_LICENSE("GPL");
3391