xref: /linux/drivers/net/xen-netfront.c (revision 02e2af20f4f9f2aa0c84e9a30a35c02f0fbb7daa)
1 /*
2  * Virtual network driver for conversing with remote driver backends.
3  *
4  * Copyright (c) 2002-2005, K A Fraser
5  * Copyright (c) 2005, XenSource Ltd
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version 2
9  * as published by the Free Software Foundation; or, when distributed
10  * separately from the Linux kernel or incorporated into other
11  * software packages, subject to the following license:
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a copy
14  * of this source file (the "Software"), to deal in the Software without
15  * restriction, including without limitation the rights to use, copy, modify,
16  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17  * and to permit persons to whom the Software is furnished to do so, subject to
18  * the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  * IN THE SOFTWARE.
30  */
31 
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/ethtool.h>
40 #include <linux/if_ether.h>
41 #include <net/tcp.h>
42 #include <linux/udp.h>
43 #include <linux/moduleparam.h>
44 #include <linux/mm.h>
45 #include <linux/slab.h>
46 #include <net/ip.h>
47 #include <linux/bpf.h>
48 #include <net/page_pool.h>
49 #include <linux/bpf_trace.h>
50 
51 #include <xen/xen.h>
52 #include <xen/xenbus.h>
53 #include <xen/events.h>
54 #include <xen/page.h>
55 #include <xen/platform_pci.h>
56 #include <xen/grant_table.h>
57 
58 #include <xen/interface/io/netif.h>
59 #include <xen/interface/memory.h>
60 #include <xen/interface/grant_table.h>
61 
62 /* Module parameters */
63 #define MAX_QUEUES_DEFAULT 8
64 static unsigned int xennet_max_queues;
65 module_param_named(max_queues, xennet_max_queues, uint, 0644);
66 MODULE_PARM_DESC(max_queues,
67 		 "Maximum number of queues per virtual interface");
68 
69 #define XENNET_TIMEOUT  (5 * HZ)
70 
71 static const struct ethtool_ops xennet_ethtool_ops;
72 
73 struct netfront_cb {
74 	int pull_to;
75 };
76 
77 #define NETFRONT_SKB_CB(skb)	((struct netfront_cb *)((skb)->cb))
78 
79 #define RX_COPY_THRESHOLD 256
80 
81 #define GRANT_INVALID_REF	0
82 
83 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
84 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
85 
86 /* Minimum number of Rx slots (includes slot for GSO metadata). */
87 #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
88 
89 /* Queue name is interface name with "-qNNN" appended */
90 #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
91 
92 /* IRQ name is queue name with "-tx" or "-rx" appended */
93 #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
94 
95 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
96 
97 struct netfront_stats {
98 	u64			packets;
99 	u64			bytes;
100 	struct u64_stats_sync	syncp;
101 };
102 
103 struct netfront_info;
104 
105 struct netfront_queue {
106 	unsigned int id; /* Queue ID, 0-based */
107 	char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
108 	struct netfront_info *info;
109 
110 	struct bpf_prog __rcu *xdp_prog;
111 
112 	struct napi_struct napi;
113 
114 	/* Split event channels support, tx_* == rx_* when using
115 	 * single event channel.
116 	 */
117 	unsigned int tx_evtchn, rx_evtchn;
118 	unsigned int tx_irq, rx_irq;
119 	/* Only used when split event channels support is enabled */
120 	char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
121 	char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
122 
123 	spinlock_t   tx_lock;
124 	struct xen_netif_tx_front_ring tx;
125 	int tx_ring_ref;
126 
127 	/*
128 	 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
129 	 * are linked from tx_skb_freelist through tx_link.
130 	 */
131 	struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
132 	unsigned short tx_link[NET_TX_RING_SIZE];
133 #define TX_LINK_NONE 0xffff
134 #define TX_PENDING   0xfffe
135 	grant_ref_t gref_tx_head;
136 	grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
137 	struct page *grant_tx_page[NET_TX_RING_SIZE];
138 	unsigned tx_skb_freelist;
139 	unsigned int tx_pend_queue;
140 
141 	spinlock_t   rx_lock ____cacheline_aligned_in_smp;
142 	struct xen_netif_rx_front_ring rx;
143 	int rx_ring_ref;
144 
145 	struct timer_list rx_refill_timer;
146 
147 	struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
148 	grant_ref_t gref_rx_head;
149 	grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
150 
151 	unsigned int rx_rsp_unconsumed;
152 	spinlock_t rx_cons_lock;
153 
154 	struct page_pool *page_pool;
155 	struct xdp_rxq_info xdp_rxq;
156 };
157 
158 struct netfront_info {
159 	struct list_head list;
160 	struct net_device *netdev;
161 
162 	struct xenbus_device *xbdev;
163 
164 	/* Multi-queue support */
165 	struct netfront_queue *queues;
166 
167 	/* Statistics */
168 	struct netfront_stats __percpu *rx_stats;
169 	struct netfront_stats __percpu *tx_stats;
170 
171 	/* XDP state */
172 	bool netback_has_xdp_headroom;
173 	bool netfront_xdp_enabled;
174 
175 	/* Is device behaving sane? */
176 	bool broken;
177 
178 	atomic_t rx_gso_checksum_fixup;
179 };
180 
181 struct netfront_rx_info {
182 	struct xen_netif_rx_response rx;
183 	struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
184 };
185 
186 /*
187  * Access macros for acquiring freeing slots in tx_skbs[].
188  */
189 
190 static void add_id_to_list(unsigned *head, unsigned short *list,
191 			   unsigned short id)
192 {
193 	list[id] = *head;
194 	*head = id;
195 }
196 
197 static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
198 {
199 	unsigned int id = *head;
200 
201 	if (id != TX_LINK_NONE) {
202 		*head = list[id];
203 		list[id] = TX_LINK_NONE;
204 	}
205 	return id;
206 }
207 
208 static int xennet_rxidx(RING_IDX idx)
209 {
210 	return idx & (NET_RX_RING_SIZE - 1);
211 }
212 
213 static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
214 					 RING_IDX ri)
215 {
216 	int i = xennet_rxidx(ri);
217 	struct sk_buff *skb = queue->rx_skbs[i];
218 	queue->rx_skbs[i] = NULL;
219 	return skb;
220 }
221 
222 static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
223 					    RING_IDX ri)
224 {
225 	int i = xennet_rxidx(ri);
226 	grant_ref_t ref = queue->grant_rx_ref[i];
227 	queue->grant_rx_ref[i] = GRANT_INVALID_REF;
228 	return ref;
229 }
230 
231 #ifdef CONFIG_SYSFS
232 static const struct attribute_group xennet_dev_group;
233 #endif
234 
235 static bool xennet_can_sg(struct net_device *dev)
236 {
237 	return dev->features & NETIF_F_SG;
238 }
239 
240 
241 static void rx_refill_timeout(struct timer_list *t)
242 {
243 	struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
244 	napi_schedule(&queue->napi);
245 }
246 
247 static int netfront_tx_slot_available(struct netfront_queue *queue)
248 {
249 	return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
250 		(NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
251 }
252 
253 static void xennet_maybe_wake_tx(struct netfront_queue *queue)
254 {
255 	struct net_device *dev = queue->info->netdev;
256 	struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
257 
258 	if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
259 	    netfront_tx_slot_available(queue) &&
260 	    likely(netif_running(dev)))
261 		netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
262 }
263 
264 
265 static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
266 {
267 	struct sk_buff *skb;
268 	struct page *page;
269 
270 	skb = __netdev_alloc_skb(queue->info->netdev,
271 				 RX_COPY_THRESHOLD + NET_IP_ALIGN,
272 				 GFP_ATOMIC | __GFP_NOWARN);
273 	if (unlikely(!skb))
274 		return NULL;
275 
276 	page = page_pool_dev_alloc_pages(queue->page_pool);
277 	if (unlikely(!page)) {
278 		kfree_skb(skb);
279 		return NULL;
280 	}
281 	skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
282 
283 	/* Align ip header to a 16 bytes boundary */
284 	skb_reserve(skb, NET_IP_ALIGN);
285 	skb->dev = queue->info->netdev;
286 
287 	return skb;
288 }
289 
290 
291 static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
292 {
293 	RING_IDX req_prod = queue->rx.req_prod_pvt;
294 	int notify;
295 	int err = 0;
296 
297 	if (unlikely(!netif_carrier_ok(queue->info->netdev)))
298 		return;
299 
300 	for (req_prod = queue->rx.req_prod_pvt;
301 	     req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
302 	     req_prod++) {
303 		struct sk_buff *skb;
304 		unsigned short id;
305 		grant_ref_t ref;
306 		struct page *page;
307 		struct xen_netif_rx_request *req;
308 
309 		skb = xennet_alloc_one_rx_buffer(queue);
310 		if (!skb) {
311 			err = -ENOMEM;
312 			break;
313 		}
314 
315 		id = xennet_rxidx(req_prod);
316 
317 		BUG_ON(queue->rx_skbs[id]);
318 		queue->rx_skbs[id] = skb;
319 
320 		ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
321 		WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
322 		queue->grant_rx_ref[id] = ref;
323 
324 		page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
325 
326 		req = RING_GET_REQUEST(&queue->rx, req_prod);
327 		gnttab_page_grant_foreign_access_ref_one(ref,
328 							 queue->info->xbdev->otherend_id,
329 							 page,
330 							 0);
331 		req->id = id;
332 		req->gref = ref;
333 	}
334 
335 	queue->rx.req_prod_pvt = req_prod;
336 
337 	/* Try again later if there are not enough requests or skb allocation
338 	 * failed.
339 	 * Enough requests is quantified as the sum of newly created slots and
340 	 * the unconsumed slots at the backend.
341 	 */
342 	if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
343 	    unlikely(err)) {
344 		mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
345 		return;
346 	}
347 
348 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
349 	if (notify)
350 		notify_remote_via_irq(queue->rx_irq);
351 }
352 
353 static int xennet_open(struct net_device *dev)
354 {
355 	struct netfront_info *np = netdev_priv(dev);
356 	unsigned int num_queues = dev->real_num_tx_queues;
357 	unsigned int i = 0;
358 	struct netfront_queue *queue = NULL;
359 
360 	if (!np->queues || np->broken)
361 		return -ENODEV;
362 
363 	for (i = 0; i < num_queues; ++i) {
364 		queue = &np->queues[i];
365 		napi_enable(&queue->napi);
366 
367 		spin_lock_bh(&queue->rx_lock);
368 		if (netif_carrier_ok(dev)) {
369 			xennet_alloc_rx_buffers(queue);
370 			queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
371 			if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
372 				napi_schedule(&queue->napi);
373 		}
374 		spin_unlock_bh(&queue->rx_lock);
375 	}
376 
377 	netif_tx_start_all_queues(dev);
378 
379 	return 0;
380 }
381 
382 static bool xennet_tx_buf_gc(struct netfront_queue *queue)
383 {
384 	RING_IDX cons, prod;
385 	unsigned short id;
386 	struct sk_buff *skb;
387 	bool more_to_do;
388 	bool work_done = false;
389 	const struct device *dev = &queue->info->netdev->dev;
390 
391 	BUG_ON(!netif_carrier_ok(queue->info->netdev));
392 
393 	do {
394 		prod = queue->tx.sring->rsp_prod;
395 		if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
396 			dev_alert(dev, "Illegal number of responses %u\n",
397 				  prod - queue->tx.rsp_cons);
398 			goto err;
399 		}
400 		rmb(); /* Ensure we see responses up to 'rp'. */
401 
402 		for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
403 			struct xen_netif_tx_response txrsp;
404 
405 			work_done = true;
406 
407 			RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
408 			if (txrsp.status == XEN_NETIF_RSP_NULL)
409 				continue;
410 
411 			id = txrsp.id;
412 			if (id >= RING_SIZE(&queue->tx)) {
413 				dev_alert(dev,
414 					  "Response has incorrect id (%u)\n",
415 					  id);
416 				goto err;
417 			}
418 			if (queue->tx_link[id] != TX_PENDING) {
419 				dev_alert(dev,
420 					  "Response for inactive request\n");
421 				goto err;
422 			}
423 
424 			queue->tx_link[id] = TX_LINK_NONE;
425 			skb = queue->tx_skbs[id];
426 			queue->tx_skbs[id] = NULL;
427 			if (unlikely(!gnttab_end_foreign_access_ref(
428 				queue->grant_tx_ref[id], GNTMAP_readonly))) {
429 				dev_alert(dev,
430 					  "Grant still in use by backend domain\n");
431 				goto err;
432 			}
433 			gnttab_release_grant_reference(
434 				&queue->gref_tx_head, queue->grant_tx_ref[id]);
435 			queue->grant_tx_ref[id] = GRANT_INVALID_REF;
436 			queue->grant_tx_page[id] = NULL;
437 			add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
438 			dev_kfree_skb_irq(skb);
439 		}
440 
441 		queue->tx.rsp_cons = prod;
442 
443 		RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
444 	} while (more_to_do);
445 
446 	xennet_maybe_wake_tx(queue);
447 
448 	return work_done;
449 
450  err:
451 	queue->info->broken = true;
452 	dev_alert(dev, "Disabled for further use\n");
453 
454 	return work_done;
455 }
456 
457 struct xennet_gnttab_make_txreq {
458 	struct netfront_queue *queue;
459 	struct sk_buff *skb;
460 	struct page *page;
461 	struct xen_netif_tx_request *tx;      /* Last request on ring page */
462 	struct xen_netif_tx_request tx_local; /* Last request local copy*/
463 	unsigned int size;
464 };
465 
466 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
467 				  unsigned int len, void *data)
468 {
469 	struct xennet_gnttab_make_txreq *info = data;
470 	unsigned int id;
471 	struct xen_netif_tx_request *tx;
472 	grant_ref_t ref;
473 	/* convenient aliases */
474 	struct page *page = info->page;
475 	struct netfront_queue *queue = info->queue;
476 	struct sk_buff *skb = info->skb;
477 
478 	id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
479 	tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
480 	ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
481 	WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
482 
483 	gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
484 					gfn, GNTMAP_readonly);
485 
486 	queue->tx_skbs[id] = skb;
487 	queue->grant_tx_page[id] = page;
488 	queue->grant_tx_ref[id] = ref;
489 
490 	info->tx_local.id = id;
491 	info->tx_local.gref = ref;
492 	info->tx_local.offset = offset;
493 	info->tx_local.size = len;
494 	info->tx_local.flags = 0;
495 
496 	*tx = info->tx_local;
497 
498 	/*
499 	 * Put the request in the pending queue, it will be set to be pending
500 	 * when the producer index is about to be raised.
501 	 */
502 	add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
503 
504 	info->tx = tx;
505 	info->size += info->tx_local.size;
506 }
507 
508 static struct xen_netif_tx_request *xennet_make_first_txreq(
509 	struct xennet_gnttab_make_txreq *info,
510 	unsigned int offset, unsigned int len)
511 {
512 	info->size = 0;
513 
514 	gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
515 
516 	return info->tx;
517 }
518 
519 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
520 				  unsigned int len, void *data)
521 {
522 	struct xennet_gnttab_make_txreq *info = data;
523 
524 	info->tx->flags |= XEN_NETTXF_more_data;
525 	skb_get(info->skb);
526 	xennet_tx_setup_grant(gfn, offset, len, data);
527 }
528 
529 static void xennet_make_txreqs(
530 	struct xennet_gnttab_make_txreq *info,
531 	struct page *page,
532 	unsigned int offset, unsigned int len)
533 {
534 	/* Skip unused frames from start of page */
535 	page += offset >> PAGE_SHIFT;
536 	offset &= ~PAGE_MASK;
537 
538 	while (len) {
539 		info->page = page;
540 		info->size = 0;
541 
542 		gnttab_foreach_grant_in_range(page, offset, len,
543 					      xennet_make_one_txreq,
544 					      info);
545 
546 		page++;
547 		offset = 0;
548 		len -= info->size;
549 	}
550 }
551 
552 /*
553  * Count how many ring slots are required to send this skb. Each frag
554  * might be a compound page.
555  */
556 static int xennet_count_skb_slots(struct sk_buff *skb)
557 {
558 	int i, frags = skb_shinfo(skb)->nr_frags;
559 	int slots;
560 
561 	slots = gnttab_count_grant(offset_in_page(skb->data),
562 				   skb_headlen(skb));
563 
564 	for (i = 0; i < frags; i++) {
565 		skb_frag_t *frag = skb_shinfo(skb)->frags + i;
566 		unsigned long size = skb_frag_size(frag);
567 		unsigned long offset = skb_frag_off(frag);
568 
569 		/* Skip unused frames from start of page */
570 		offset &= ~PAGE_MASK;
571 
572 		slots += gnttab_count_grant(offset, size);
573 	}
574 
575 	return slots;
576 }
577 
578 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
579 			       struct net_device *sb_dev)
580 {
581 	unsigned int num_queues = dev->real_num_tx_queues;
582 	u32 hash;
583 	u16 queue_idx;
584 
585 	/* First, check if there is only one queue */
586 	if (num_queues == 1) {
587 		queue_idx = 0;
588 	} else {
589 		hash = skb_get_hash(skb);
590 		queue_idx = hash % num_queues;
591 	}
592 
593 	return queue_idx;
594 }
595 
596 static void xennet_mark_tx_pending(struct netfront_queue *queue)
597 {
598 	unsigned int i;
599 
600 	while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
601 	       TX_LINK_NONE)
602 		queue->tx_link[i] = TX_PENDING;
603 }
604 
605 static int xennet_xdp_xmit_one(struct net_device *dev,
606 			       struct netfront_queue *queue,
607 			       struct xdp_frame *xdpf)
608 {
609 	struct netfront_info *np = netdev_priv(dev);
610 	struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
611 	struct xennet_gnttab_make_txreq info = {
612 		.queue = queue,
613 		.skb = NULL,
614 		.page = virt_to_page(xdpf->data),
615 	};
616 	int notify;
617 
618 	xennet_make_first_txreq(&info,
619 				offset_in_page(xdpf->data),
620 				xdpf->len);
621 
622 	xennet_mark_tx_pending(queue);
623 
624 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
625 	if (notify)
626 		notify_remote_via_irq(queue->tx_irq);
627 
628 	u64_stats_update_begin(&tx_stats->syncp);
629 	tx_stats->bytes += xdpf->len;
630 	tx_stats->packets++;
631 	u64_stats_update_end(&tx_stats->syncp);
632 
633 	xennet_tx_buf_gc(queue);
634 
635 	return 0;
636 }
637 
638 static int xennet_xdp_xmit(struct net_device *dev, int n,
639 			   struct xdp_frame **frames, u32 flags)
640 {
641 	unsigned int num_queues = dev->real_num_tx_queues;
642 	struct netfront_info *np = netdev_priv(dev);
643 	struct netfront_queue *queue = NULL;
644 	unsigned long irq_flags;
645 	int nxmit = 0;
646 	int i;
647 
648 	if (unlikely(np->broken))
649 		return -ENODEV;
650 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
651 		return -EINVAL;
652 
653 	queue = &np->queues[smp_processor_id() % num_queues];
654 
655 	spin_lock_irqsave(&queue->tx_lock, irq_flags);
656 	for (i = 0; i < n; i++) {
657 		struct xdp_frame *xdpf = frames[i];
658 
659 		if (!xdpf)
660 			continue;
661 		if (xennet_xdp_xmit_one(dev, queue, xdpf))
662 			break;
663 		nxmit++;
664 	}
665 	spin_unlock_irqrestore(&queue->tx_lock, irq_flags);
666 
667 	return nxmit;
668 }
669 
670 
671 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
672 
673 static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
674 {
675 	struct netfront_info *np = netdev_priv(dev);
676 	struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
677 	struct xen_netif_tx_request *first_tx;
678 	unsigned int i;
679 	int notify;
680 	int slots;
681 	struct page *page;
682 	unsigned int offset;
683 	unsigned int len;
684 	unsigned long flags;
685 	struct netfront_queue *queue = NULL;
686 	struct xennet_gnttab_make_txreq info = { };
687 	unsigned int num_queues = dev->real_num_tx_queues;
688 	u16 queue_index;
689 	struct sk_buff *nskb;
690 
691 	/* Drop the packet if no queues are set up */
692 	if (num_queues < 1)
693 		goto drop;
694 	if (unlikely(np->broken))
695 		goto drop;
696 	/* Determine which queue to transmit this SKB on */
697 	queue_index = skb_get_queue_mapping(skb);
698 	queue = &np->queues[queue_index];
699 
700 	/* If skb->len is too big for wire format, drop skb and alert
701 	 * user about misconfiguration.
702 	 */
703 	if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
704 		net_alert_ratelimited(
705 			"xennet: skb->len = %u, too big for wire format\n",
706 			skb->len);
707 		goto drop;
708 	}
709 
710 	slots = xennet_count_skb_slots(skb);
711 	if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
712 		net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
713 				    slots, skb->len);
714 		if (skb_linearize(skb))
715 			goto drop;
716 	}
717 
718 	page = virt_to_page(skb->data);
719 	offset = offset_in_page(skb->data);
720 
721 	/* The first req should be at least ETH_HLEN size or the packet will be
722 	 * dropped by netback.
723 	 */
724 	if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
725 		nskb = skb_copy(skb, GFP_ATOMIC);
726 		if (!nskb)
727 			goto drop;
728 		dev_consume_skb_any(skb);
729 		skb = nskb;
730 		page = virt_to_page(skb->data);
731 		offset = offset_in_page(skb->data);
732 	}
733 
734 	len = skb_headlen(skb);
735 
736 	spin_lock_irqsave(&queue->tx_lock, flags);
737 
738 	if (unlikely(!netif_carrier_ok(dev) ||
739 		     (slots > 1 && !xennet_can_sg(dev)) ||
740 		     netif_needs_gso(skb, netif_skb_features(skb)))) {
741 		spin_unlock_irqrestore(&queue->tx_lock, flags);
742 		goto drop;
743 	}
744 
745 	/* First request for the linear area. */
746 	info.queue = queue;
747 	info.skb = skb;
748 	info.page = page;
749 	first_tx = xennet_make_first_txreq(&info, offset, len);
750 	offset += info.tx_local.size;
751 	if (offset == PAGE_SIZE) {
752 		page++;
753 		offset = 0;
754 	}
755 	len -= info.tx_local.size;
756 
757 	if (skb->ip_summed == CHECKSUM_PARTIAL)
758 		/* local packet? */
759 		first_tx->flags |= XEN_NETTXF_csum_blank |
760 				   XEN_NETTXF_data_validated;
761 	else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
762 		/* remote but checksummed. */
763 		first_tx->flags |= XEN_NETTXF_data_validated;
764 
765 	/* Optional extra info after the first request. */
766 	if (skb_shinfo(skb)->gso_size) {
767 		struct xen_netif_extra_info *gso;
768 
769 		gso = (struct xen_netif_extra_info *)
770 			RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
771 
772 		first_tx->flags |= XEN_NETTXF_extra_info;
773 
774 		gso->u.gso.size = skb_shinfo(skb)->gso_size;
775 		gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
776 			XEN_NETIF_GSO_TYPE_TCPV6 :
777 			XEN_NETIF_GSO_TYPE_TCPV4;
778 		gso->u.gso.pad = 0;
779 		gso->u.gso.features = 0;
780 
781 		gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
782 		gso->flags = 0;
783 	}
784 
785 	/* Requests for the rest of the linear area. */
786 	xennet_make_txreqs(&info, page, offset, len);
787 
788 	/* Requests for all the frags. */
789 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
790 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
791 		xennet_make_txreqs(&info, skb_frag_page(frag),
792 					skb_frag_off(frag),
793 					skb_frag_size(frag));
794 	}
795 
796 	/* First request has the packet length. */
797 	first_tx->size = skb->len;
798 
799 	/* timestamp packet in software */
800 	skb_tx_timestamp(skb);
801 
802 	xennet_mark_tx_pending(queue);
803 
804 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
805 	if (notify)
806 		notify_remote_via_irq(queue->tx_irq);
807 
808 	u64_stats_update_begin(&tx_stats->syncp);
809 	tx_stats->bytes += skb->len;
810 	tx_stats->packets++;
811 	u64_stats_update_end(&tx_stats->syncp);
812 
813 	/* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
814 	xennet_tx_buf_gc(queue);
815 
816 	if (!netfront_tx_slot_available(queue))
817 		netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
818 
819 	spin_unlock_irqrestore(&queue->tx_lock, flags);
820 
821 	return NETDEV_TX_OK;
822 
823  drop:
824 	dev->stats.tx_dropped++;
825 	dev_kfree_skb_any(skb);
826 	return NETDEV_TX_OK;
827 }
828 
829 static int xennet_close(struct net_device *dev)
830 {
831 	struct netfront_info *np = netdev_priv(dev);
832 	unsigned int num_queues = dev->real_num_tx_queues;
833 	unsigned int i;
834 	struct netfront_queue *queue;
835 	netif_tx_stop_all_queues(np->netdev);
836 	for (i = 0; i < num_queues; ++i) {
837 		queue = &np->queues[i];
838 		napi_disable(&queue->napi);
839 	}
840 	return 0;
841 }
842 
843 static void xennet_destroy_queues(struct netfront_info *info)
844 {
845 	unsigned int i;
846 
847 	for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
848 		struct netfront_queue *queue = &info->queues[i];
849 
850 		if (netif_running(info->netdev))
851 			napi_disable(&queue->napi);
852 		netif_napi_del(&queue->napi);
853 	}
854 
855 	kfree(info->queues);
856 	info->queues = NULL;
857 }
858 
859 static void xennet_uninit(struct net_device *dev)
860 {
861 	struct netfront_info *np = netdev_priv(dev);
862 	xennet_destroy_queues(np);
863 }
864 
865 static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
866 {
867 	unsigned long flags;
868 
869 	spin_lock_irqsave(&queue->rx_cons_lock, flags);
870 	queue->rx.rsp_cons = val;
871 	queue->rx_rsp_unconsumed = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
872 	spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
873 }
874 
875 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
876 				grant_ref_t ref)
877 {
878 	int new = xennet_rxidx(queue->rx.req_prod_pvt);
879 
880 	BUG_ON(queue->rx_skbs[new]);
881 	queue->rx_skbs[new] = skb;
882 	queue->grant_rx_ref[new] = ref;
883 	RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
884 	RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
885 	queue->rx.req_prod_pvt++;
886 }
887 
888 static int xennet_get_extras(struct netfront_queue *queue,
889 			     struct xen_netif_extra_info *extras,
890 			     RING_IDX rp)
891 
892 {
893 	struct xen_netif_extra_info extra;
894 	struct device *dev = &queue->info->netdev->dev;
895 	RING_IDX cons = queue->rx.rsp_cons;
896 	int err = 0;
897 
898 	do {
899 		struct sk_buff *skb;
900 		grant_ref_t ref;
901 
902 		if (unlikely(cons + 1 == rp)) {
903 			if (net_ratelimit())
904 				dev_warn(dev, "Missing extra info\n");
905 			err = -EBADR;
906 			break;
907 		}
908 
909 		RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
910 
911 		if (unlikely(!extra.type ||
912 			     extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
913 			if (net_ratelimit())
914 				dev_warn(dev, "Invalid extra type: %d\n",
915 					 extra.type);
916 			err = -EINVAL;
917 		} else {
918 			extras[extra.type - 1] = extra;
919 		}
920 
921 		skb = xennet_get_rx_skb(queue, cons);
922 		ref = xennet_get_rx_ref(queue, cons);
923 		xennet_move_rx_slot(queue, skb, ref);
924 	} while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
925 
926 	xennet_set_rx_rsp_cons(queue, cons);
927 	return err;
928 }
929 
930 static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata,
931 		   struct xen_netif_rx_response *rx, struct bpf_prog *prog,
932 		   struct xdp_buff *xdp, bool *need_xdp_flush)
933 {
934 	struct xdp_frame *xdpf;
935 	u32 len = rx->status;
936 	u32 act;
937 	int err;
938 
939 	xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
940 		      &queue->xdp_rxq);
941 	xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM,
942 			 len, false);
943 
944 	act = bpf_prog_run_xdp(prog, xdp);
945 	switch (act) {
946 	case XDP_TX:
947 		get_page(pdata);
948 		xdpf = xdp_convert_buff_to_frame(xdp);
949 		err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
950 		if (unlikely(!err))
951 			xdp_return_frame_rx_napi(xdpf);
952 		else if (unlikely(err < 0))
953 			trace_xdp_exception(queue->info->netdev, prog, act);
954 		break;
955 	case XDP_REDIRECT:
956 		get_page(pdata);
957 		err = xdp_do_redirect(queue->info->netdev, xdp, prog);
958 		*need_xdp_flush = true;
959 		if (unlikely(err))
960 			trace_xdp_exception(queue->info->netdev, prog, act);
961 		break;
962 	case XDP_PASS:
963 	case XDP_DROP:
964 		break;
965 
966 	case XDP_ABORTED:
967 		trace_xdp_exception(queue->info->netdev, prog, act);
968 		break;
969 
970 	default:
971 		bpf_warn_invalid_xdp_action(queue->info->netdev, prog, act);
972 	}
973 
974 	return act;
975 }
976 
977 static int xennet_get_responses(struct netfront_queue *queue,
978 				struct netfront_rx_info *rinfo, RING_IDX rp,
979 				struct sk_buff_head *list,
980 				bool *need_xdp_flush)
981 {
982 	struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
983 	int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
984 	RING_IDX cons = queue->rx.rsp_cons;
985 	struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
986 	struct xen_netif_extra_info *extras = rinfo->extras;
987 	grant_ref_t ref = xennet_get_rx_ref(queue, cons);
988 	struct device *dev = &queue->info->netdev->dev;
989 	struct bpf_prog *xdp_prog;
990 	struct xdp_buff xdp;
991 	int slots = 1;
992 	int err = 0;
993 	u32 verdict;
994 
995 	if (rx->flags & XEN_NETRXF_extra_info) {
996 		err = xennet_get_extras(queue, extras, rp);
997 		if (!err) {
998 			if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) {
999 				struct xen_netif_extra_info *xdp;
1000 
1001 				xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1];
1002 				rx->offset = xdp->u.xdp.headroom;
1003 			}
1004 		}
1005 		cons = queue->rx.rsp_cons;
1006 	}
1007 
1008 	for (;;) {
1009 		if (unlikely(rx->status < 0 ||
1010 			     rx->offset + rx->status > XEN_PAGE_SIZE)) {
1011 			if (net_ratelimit())
1012 				dev_warn(dev, "rx->offset: %u, size: %d\n",
1013 					 rx->offset, rx->status);
1014 			xennet_move_rx_slot(queue, skb, ref);
1015 			err = -EINVAL;
1016 			goto next;
1017 		}
1018 
1019 		/*
1020 		 * This definitely indicates a bug, either in this driver or in
1021 		 * the backend driver. In future this should flag the bad
1022 		 * situation to the system controller to reboot the backend.
1023 		 */
1024 		if (ref == GRANT_INVALID_REF) {
1025 			if (net_ratelimit())
1026 				dev_warn(dev, "Bad rx response id %d.\n",
1027 					 rx->id);
1028 			err = -EINVAL;
1029 			goto next;
1030 		}
1031 
1032 		if (!gnttab_end_foreign_access_ref(ref, 0)) {
1033 			dev_alert(dev,
1034 				  "Grant still in use by backend domain\n");
1035 			queue->info->broken = true;
1036 			dev_alert(dev, "Disabled for further use\n");
1037 			return -EINVAL;
1038 		}
1039 
1040 		gnttab_release_grant_reference(&queue->gref_rx_head, ref);
1041 
1042 		rcu_read_lock();
1043 		xdp_prog = rcu_dereference(queue->xdp_prog);
1044 		if (xdp_prog) {
1045 			if (!(rx->flags & XEN_NETRXF_more_data)) {
1046 				/* currently only a single page contains data */
1047 				verdict = xennet_run_xdp(queue,
1048 							 skb_frag_page(&skb_shinfo(skb)->frags[0]),
1049 							 rx, xdp_prog, &xdp, need_xdp_flush);
1050 				if (verdict != XDP_PASS)
1051 					err = -EINVAL;
1052 			} else {
1053 				/* drop the frame */
1054 				err = -EINVAL;
1055 			}
1056 		}
1057 		rcu_read_unlock();
1058 next:
1059 		__skb_queue_tail(list, skb);
1060 		if (!(rx->flags & XEN_NETRXF_more_data))
1061 			break;
1062 
1063 		if (cons + slots == rp) {
1064 			if (net_ratelimit())
1065 				dev_warn(dev, "Need more slots\n");
1066 			err = -ENOENT;
1067 			break;
1068 		}
1069 
1070 		RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
1071 		rx = &rx_local;
1072 		skb = xennet_get_rx_skb(queue, cons + slots);
1073 		ref = xennet_get_rx_ref(queue, cons + slots);
1074 		slots++;
1075 	}
1076 
1077 	if (unlikely(slots > max)) {
1078 		if (net_ratelimit())
1079 			dev_warn(dev, "Too many slots\n");
1080 		err = -E2BIG;
1081 	}
1082 
1083 	if (unlikely(err))
1084 		xennet_set_rx_rsp_cons(queue, cons + slots);
1085 
1086 	return err;
1087 }
1088 
1089 static int xennet_set_skb_gso(struct sk_buff *skb,
1090 			      struct xen_netif_extra_info *gso)
1091 {
1092 	if (!gso->u.gso.size) {
1093 		if (net_ratelimit())
1094 			pr_warn("GSO size must not be zero\n");
1095 		return -EINVAL;
1096 	}
1097 
1098 	if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
1099 	    gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
1100 		if (net_ratelimit())
1101 			pr_warn("Bad GSO type %d\n", gso->u.gso.type);
1102 		return -EINVAL;
1103 	}
1104 
1105 	skb_shinfo(skb)->gso_size = gso->u.gso.size;
1106 	skb_shinfo(skb)->gso_type =
1107 		(gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
1108 		SKB_GSO_TCPV4 :
1109 		SKB_GSO_TCPV6;
1110 
1111 	/* Header must be checked, and gso_segs computed. */
1112 	skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1113 	skb_shinfo(skb)->gso_segs = 0;
1114 
1115 	return 0;
1116 }
1117 
1118 static int xennet_fill_frags(struct netfront_queue *queue,
1119 			     struct sk_buff *skb,
1120 			     struct sk_buff_head *list)
1121 {
1122 	RING_IDX cons = queue->rx.rsp_cons;
1123 	struct sk_buff *nskb;
1124 
1125 	while ((nskb = __skb_dequeue(list))) {
1126 		struct xen_netif_rx_response rx;
1127 		skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
1128 
1129 		RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
1130 
1131 		if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
1132 			unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1133 
1134 			BUG_ON(pull_to < skb_headlen(skb));
1135 			__pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1136 		}
1137 		if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
1138 			xennet_set_rx_rsp_cons(queue,
1139 					       ++cons + skb_queue_len(list));
1140 			kfree_skb(nskb);
1141 			return -ENOENT;
1142 		}
1143 
1144 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1145 				skb_frag_page(nfrag),
1146 				rx.offset, rx.status, PAGE_SIZE);
1147 
1148 		skb_shinfo(nskb)->nr_frags = 0;
1149 		kfree_skb(nskb);
1150 	}
1151 
1152 	xennet_set_rx_rsp_cons(queue, cons);
1153 
1154 	return 0;
1155 }
1156 
1157 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
1158 {
1159 	bool recalculate_partial_csum = false;
1160 
1161 	/*
1162 	 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1163 	 * peers can fail to set NETRXF_csum_blank when sending a GSO
1164 	 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1165 	 * recalculate the partial checksum.
1166 	 */
1167 	if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1168 		struct netfront_info *np = netdev_priv(dev);
1169 		atomic_inc(&np->rx_gso_checksum_fixup);
1170 		skb->ip_summed = CHECKSUM_PARTIAL;
1171 		recalculate_partial_csum = true;
1172 	}
1173 
1174 	/* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1175 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1176 		return 0;
1177 
1178 	return skb_checksum_setup(skb, recalculate_partial_csum);
1179 }
1180 
1181 static int handle_incoming_queue(struct netfront_queue *queue,
1182 				 struct sk_buff_head *rxq)
1183 {
1184 	struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
1185 	int packets_dropped = 0;
1186 	struct sk_buff *skb;
1187 
1188 	while ((skb = __skb_dequeue(rxq)) != NULL) {
1189 		int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1190 
1191 		if (pull_to > skb_headlen(skb))
1192 			__pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1193 
1194 		/* Ethernet work: Delayed to here as it peeks the header. */
1195 		skb->protocol = eth_type_trans(skb, queue->info->netdev);
1196 		skb_reset_network_header(skb);
1197 
1198 		if (checksum_setup(queue->info->netdev, skb)) {
1199 			kfree_skb(skb);
1200 			packets_dropped++;
1201 			queue->info->netdev->stats.rx_errors++;
1202 			continue;
1203 		}
1204 
1205 		u64_stats_update_begin(&rx_stats->syncp);
1206 		rx_stats->packets++;
1207 		rx_stats->bytes += skb->len;
1208 		u64_stats_update_end(&rx_stats->syncp);
1209 
1210 		/* Pass it up. */
1211 		napi_gro_receive(&queue->napi, skb);
1212 	}
1213 
1214 	return packets_dropped;
1215 }
1216 
1217 static int xennet_poll(struct napi_struct *napi, int budget)
1218 {
1219 	struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1220 	struct net_device *dev = queue->info->netdev;
1221 	struct sk_buff *skb;
1222 	struct netfront_rx_info rinfo;
1223 	struct xen_netif_rx_response *rx = &rinfo.rx;
1224 	struct xen_netif_extra_info *extras = rinfo.extras;
1225 	RING_IDX i, rp;
1226 	int work_done;
1227 	struct sk_buff_head rxq;
1228 	struct sk_buff_head errq;
1229 	struct sk_buff_head tmpq;
1230 	int err;
1231 	bool need_xdp_flush = false;
1232 
1233 	spin_lock(&queue->rx_lock);
1234 
1235 	skb_queue_head_init(&rxq);
1236 	skb_queue_head_init(&errq);
1237 	skb_queue_head_init(&tmpq);
1238 
1239 	rp = queue->rx.sring->rsp_prod;
1240 	if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1241 		dev_alert(&dev->dev, "Illegal number of responses %u\n",
1242 			  rp - queue->rx.rsp_cons);
1243 		queue->info->broken = true;
1244 		spin_unlock(&queue->rx_lock);
1245 		return 0;
1246 	}
1247 	rmb(); /* Ensure we see queued responses up to 'rp'. */
1248 
1249 	i = queue->rx.rsp_cons;
1250 	work_done = 0;
1251 	while ((i != rp) && (work_done < budget)) {
1252 		RING_COPY_RESPONSE(&queue->rx, i, rx);
1253 		memset(extras, 0, sizeof(rinfo.extras));
1254 
1255 		err = xennet_get_responses(queue, &rinfo, rp, &tmpq,
1256 					   &need_xdp_flush);
1257 
1258 		if (unlikely(err)) {
1259 			if (queue->info->broken) {
1260 				spin_unlock(&queue->rx_lock);
1261 				return 0;
1262 			}
1263 err:
1264 			while ((skb = __skb_dequeue(&tmpq)))
1265 				__skb_queue_tail(&errq, skb);
1266 			dev->stats.rx_errors++;
1267 			i = queue->rx.rsp_cons;
1268 			continue;
1269 		}
1270 
1271 		skb = __skb_dequeue(&tmpq);
1272 
1273 		if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1274 			struct xen_netif_extra_info *gso;
1275 			gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1276 
1277 			if (unlikely(xennet_set_skb_gso(skb, gso))) {
1278 				__skb_queue_head(&tmpq, skb);
1279 				xennet_set_rx_rsp_cons(queue,
1280 						       queue->rx.rsp_cons +
1281 						       skb_queue_len(&tmpq));
1282 				goto err;
1283 			}
1284 		}
1285 
1286 		NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1287 		if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1288 			NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
1289 
1290 		skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
1291 		skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1292 		skb->data_len = rx->status;
1293 		skb->len += rx->status;
1294 
1295 		if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
1296 			goto err;
1297 
1298 		if (rx->flags & XEN_NETRXF_csum_blank)
1299 			skb->ip_summed = CHECKSUM_PARTIAL;
1300 		else if (rx->flags & XEN_NETRXF_data_validated)
1301 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1302 
1303 		__skb_queue_tail(&rxq, skb);
1304 
1305 		i = queue->rx.rsp_cons + 1;
1306 		xennet_set_rx_rsp_cons(queue, i);
1307 		work_done++;
1308 	}
1309 	if (need_xdp_flush)
1310 		xdp_do_flush();
1311 
1312 	__skb_queue_purge(&errq);
1313 
1314 	work_done -= handle_incoming_queue(queue, &rxq);
1315 
1316 	xennet_alloc_rx_buffers(queue);
1317 
1318 	if (work_done < budget) {
1319 		int more_to_do = 0;
1320 
1321 		napi_complete_done(napi, work_done);
1322 
1323 		RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1324 		if (more_to_do)
1325 			napi_schedule(napi);
1326 	}
1327 
1328 	spin_unlock(&queue->rx_lock);
1329 
1330 	return work_done;
1331 }
1332 
1333 static int xennet_change_mtu(struct net_device *dev, int mtu)
1334 {
1335 	int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
1336 
1337 	if (mtu > max)
1338 		return -EINVAL;
1339 	dev->mtu = mtu;
1340 	return 0;
1341 }
1342 
1343 static void xennet_get_stats64(struct net_device *dev,
1344 			       struct rtnl_link_stats64 *tot)
1345 {
1346 	struct netfront_info *np = netdev_priv(dev);
1347 	int cpu;
1348 
1349 	for_each_possible_cpu(cpu) {
1350 		struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1351 		struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
1352 		u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1353 		unsigned int start;
1354 
1355 		do {
1356 			start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1357 			tx_packets = tx_stats->packets;
1358 			tx_bytes = tx_stats->bytes;
1359 		} while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
1360 
1361 		do {
1362 			start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1363 			rx_packets = rx_stats->packets;
1364 			rx_bytes = rx_stats->bytes;
1365 		} while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
1366 
1367 		tot->rx_packets += rx_packets;
1368 		tot->tx_packets += tx_packets;
1369 		tot->rx_bytes   += rx_bytes;
1370 		tot->tx_bytes   += tx_bytes;
1371 	}
1372 
1373 	tot->rx_errors  = dev->stats.rx_errors;
1374 	tot->tx_dropped = dev->stats.tx_dropped;
1375 }
1376 
1377 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1378 {
1379 	struct sk_buff *skb;
1380 	int i;
1381 
1382 	for (i = 0; i < NET_TX_RING_SIZE; i++) {
1383 		/* Skip over entries which are actually freelist references */
1384 		if (!queue->tx_skbs[i])
1385 			continue;
1386 
1387 		skb = queue->tx_skbs[i];
1388 		queue->tx_skbs[i] = NULL;
1389 		get_page(queue->grant_tx_page[i]);
1390 		gnttab_end_foreign_access(queue->grant_tx_ref[i],
1391 					  GNTMAP_readonly,
1392 					  (unsigned long)page_address(queue->grant_tx_page[i]));
1393 		queue->grant_tx_page[i] = NULL;
1394 		queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1395 		add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
1396 		dev_kfree_skb_irq(skb);
1397 	}
1398 }
1399 
1400 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1401 {
1402 	int id, ref;
1403 
1404 	spin_lock_bh(&queue->rx_lock);
1405 
1406 	for (id = 0; id < NET_RX_RING_SIZE; id++) {
1407 		struct sk_buff *skb;
1408 		struct page *page;
1409 
1410 		skb = queue->rx_skbs[id];
1411 		if (!skb)
1412 			continue;
1413 
1414 		ref = queue->grant_rx_ref[id];
1415 		if (ref == GRANT_INVALID_REF)
1416 			continue;
1417 
1418 		page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1419 
1420 		/* gnttab_end_foreign_access() needs a page ref until
1421 		 * foreign access is ended (which may be deferred).
1422 		 */
1423 		get_page(page);
1424 		gnttab_end_foreign_access(ref, 0,
1425 					  (unsigned long)page_address(page));
1426 		queue->grant_rx_ref[id] = GRANT_INVALID_REF;
1427 
1428 		kfree_skb(skb);
1429 	}
1430 
1431 	spin_unlock_bh(&queue->rx_lock);
1432 }
1433 
1434 static netdev_features_t xennet_fix_features(struct net_device *dev,
1435 	netdev_features_t features)
1436 {
1437 	struct netfront_info *np = netdev_priv(dev);
1438 
1439 	if (features & NETIF_F_SG &&
1440 	    !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1441 		features &= ~NETIF_F_SG;
1442 
1443 	if (features & NETIF_F_IPV6_CSUM &&
1444 	    !xenbus_read_unsigned(np->xbdev->otherend,
1445 				  "feature-ipv6-csum-offload", 0))
1446 		features &= ~NETIF_F_IPV6_CSUM;
1447 
1448 	if (features & NETIF_F_TSO &&
1449 	    !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1450 		features &= ~NETIF_F_TSO;
1451 
1452 	if (features & NETIF_F_TSO6 &&
1453 	    !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1454 		features &= ~NETIF_F_TSO6;
1455 
1456 	return features;
1457 }
1458 
1459 static int xennet_set_features(struct net_device *dev,
1460 	netdev_features_t features)
1461 {
1462 	if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1463 		netdev_info(dev, "Reducing MTU because no SG offload");
1464 		dev->mtu = ETH_DATA_LEN;
1465 	}
1466 
1467 	return 0;
1468 }
1469 
1470 static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
1471 {
1472 	unsigned long flags;
1473 
1474 	if (unlikely(queue->info->broken))
1475 		return false;
1476 
1477 	spin_lock_irqsave(&queue->tx_lock, flags);
1478 	if (xennet_tx_buf_gc(queue))
1479 		*eoi = 0;
1480 	spin_unlock_irqrestore(&queue->tx_lock, flags);
1481 
1482 	return true;
1483 }
1484 
1485 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1486 {
1487 	unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1488 
1489 	if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1490 		xen_irq_lateeoi(irq, eoiflag);
1491 
1492 	return IRQ_HANDLED;
1493 }
1494 
1495 static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
1496 {
1497 	unsigned int work_queued;
1498 	unsigned long flags;
1499 
1500 	if (unlikely(queue->info->broken))
1501 		return false;
1502 
1503 	spin_lock_irqsave(&queue->rx_cons_lock, flags);
1504 	work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
1505 	if (work_queued > queue->rx_rsp_unconsumed) {
1506 		queue->rx_rsp_unconsumed = work_queued;
1507 		*eoi = 0;
1508 	} else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1509 		const struct device *dev = &queue->info->netdev->dev;
1510 
1511 		spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1512 		dev_alert(dev, "RX producer index going backwards\n");
1513 		dev_alert(dev, "Disabled for further use\n");
1514 		queue->info->broken = true;
1515 		return false;
1516 	}
1517 	spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1518 
1519 	if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
1520 		napi_schedule(&queue->napi);
1521 
1522 	return true;
1523 }
1524 
1525 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1526 {
1527 	unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1528 
1529 	if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1530 		xen_irq_lateeoi(irq, eoiflag);
1531 
1532 	return IRQ_HANDLED;
1533 }
1534 
1535 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1536 {
1537 	unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1538 
1539 	if (xennet_handle_tx(dev_id, &eoiflag) &&
1540 	    xennet_handle_rx(dev_id, &eoiflag))
1541 		xen_irq_lateeoi(irq, eoiflag);
1542 
1543 	return IRQ_HANDLED;
1544 }
1545 
1546 #ifdef CONFIG_NET_POLL_CONTROLLER
1547 static void xennet_poll_controller(struct net_device *dev)
1548 {
1549 	/* Poll each queue */
1550 	struct netfront_info *info = netdev_priv(dev);
1551 	unsigned int num_queues = dev->real_num_tx_queues;
1552 	unsigned int i;
1553 
1554 	if (info->broken)
1555 		return;
1556 
1557 	for (i = 0; i < num_queues; ++i)
1558 		xennet_interrupt(0, &info->queues[i]);
1559 }
1560 #endif
1561 
1562 #define NETBACK_XDP_HEADROOM_DISABLE	0
1563 #define NETBACK_XDP_HEADROOM_ENABLE	1
1564 
1565 static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
1566 {
1567 	int err;
1568 	unsigned short headroom;
1569 
1570 	headroom = xdp ? XDP_PACKET_HEADROOM : 0;
1571 	err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
1572 			    "xdp-headroom", "%hu",
1573 			    headroom);
1574 	if (err)
1575 		pr_warn("Error writing xdp-headroom\n");
1576 
1577 	return err;
1578 }
1579 
1580 static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1581 			  struct netlink_ext_ack *extack)
1582 {
1583 	unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
1584 	struct netfront_info *np = netdev_priv(dev);
1585 	struct bpf_prog *old_prog;
1586 	unsigned int i, err;
1587 
1588 	if (dev->mtu > max_mtu) {
1589 		netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
1590 		return -EINVAL;
1591 	}
1592 
1593 	if (!np->netback_has_xdp_headroom)
1594 		return 0;
1595 
1596 	xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
1597 
1598 	err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
1599 				  NETBACK_XDP_HEADROOM_DISABLE);
1600 	if (err)
1601 		return err;
1602 
1603 	/* avoid the race with XDP headroom adjustment */
1604 	wait_event(module_wq,
1605 		   xenbus_read_driver_state(np->xbdev->otherend) ==
1606 		   XenbusStateReconfigured);
1607 	np->netfront_xdp_enabled = true;
1608 
1609 	old_prog = rtnl_dereference(np->queues[0].xdp_prog);
1610 
1611 	if (prog)
1612 		bpf_prog_add(prog, dev->real_num_tx_queues);
1613 
1614 	for (i = 0; i < dev->real_num_tx_queues; ++i)
1615 		rcu_assign_pointer(np->queues[i].xdp_prog, prog);
1616 
1617 	if (old_prog)
1618 		for (i = 0; i < dev->real_num_tx_queues; ++i)
1619 			bpf_prog_put(old_prog);
1620 
1621 	xenbus_switch_state(np->xbdev, XenbusStateConnected);
1622 
1623 	return 0;
1624 }
1625 
1626 static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1627 {
1628 	struct netfront_info *np = netdev_priv(dev);
1629 
1630 	if (np->broken)
1631 		return -ENODEV;
1632 
1633 	switch (xdp->command) {
1634 	case XDP_SETUP_PROG:
1635 		return xennet_xdp_set(dev, xdp->prog, xdp->extack);
1636 	default:
1637 		return -EINVAL;
1638 	}
1639 }
1640 
1641 static const struct net_device_ops xennet_netdev_ops = {
1642 	.ndo_uninit          = xennet_uninit,
1643 	.ndo_open            = xennet_open,
1644 	.ndo_stop            = xennet_close,
1645 	.ndo_start_xmit      = xennet_start_xmit,
1646 	.ndo_change_mtu	     = xennet_change_mtu,
1647 	.ndo_get_stats64     = xennet_get_stats64,
1648 	.ndo_set_mac_address = eth_mac_addr,
1649 	.ndo_validate_addr   = eth_validate_addr,
1650 	.ndo_fix_features    = xennet_fix_features,
1651 	.ndo_set_features    = xennet_set_features,
1652 	.ndo_select_queue    = xennet_select_queue,
1653 	.ndo_bpf            = xennet_xdp,
1654 	.ndo_xdp_xmit	    = xennet_xdp_xmit,
1655 #ifdef CONFIG_NET_POLL_CONTROLLER
1656 	.ndo_poll_controller = xennet_poll_controller,
1657 #endif
1658 };
1659 
1660 static void xennet_free_netdev(struct net_device *netdev)
1661 {
1662 	struct netfront_info *np = netdev_priv(netdev);
1663 
1664 	free_percpu(np->rx_stats);
1665 	free_percpu(np->tx_stats);
1666 	free_netdev(netdev);
1667 }
1668 
1669 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1670 {
1671 	int err;
1672 	struct net_device *netdev;
1673 	struct netfront_info *np;
1674 
1675 	netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1676 	if (!netdev)
1677 		return ERR_PTR(-ENOMEM);
1678 
1679 	np                   = netdev_priv(netdev);
1680 	np->xbdev            = dev;
1681 
1682 	np->queues = NULL;
1683 
1684 	err = -ENOMEM;
1685 	np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1686 	if (np->rx_stats == NULL)
1687 		goto exit;
1688 	np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1689 	if (np->tx_stats == NULL)
1690 		goto exit;
1691 
1692 	netdev->netdev_ops	= &xennet_netdev_ops;
1693 
1694 	netdev->features        = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1695 				  NETIF_F_GSO_ROBUST;
1696 	netdev->hw_features	= NETIF_F_SG |
1697 				  NETIF_F_IPV6_CSUM |
1698 				  NETIF_F_TSO | NETIF_F_TSO6;
1699 
1700 	/*
1701          * Assume that all hw features are available for now. This set
1702          * will be adjusted by the call to netdev_update_features() in
1703          * xennet_connect() which is the earliest point where we can
1704          * negotiate with the backend regarding supported features.
1705          */
1706 	netdev->features |= netdev->hw_features;
1707 
1708 	netdev->ethtool_ops = &xennet_ethtool_ops;
1709 	netdev->min_mtu = ETH_MIN_MTU;
1710 	netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
1711 	SET_NETDEV_DEV(netdev, &dev->dev);
1712 
1713 	np->netdev = netdev;
1714 	np->netfront_xdp_enabled = false;
1715 
1716 	netif_carrier_off(netdev);
1717 
1718 	do {
1719 		xenbus_switch_state(dev, XenbusStateInitialising);
1720 		err = wait_event_timeout(module_wq,
1721 				 xenbus_read_driver_state(dev->otherend) !=
1722 				 XenbusStateClosed &&
1723 				 xenbus_read_driver_state(dev->otherend) !=
1724 				 XenbusStateUnknown, XENNET_TIMEOUT);
1725 	} while (!err);
1726 
1727 	return netdev;
1728 
1729  exit:
1730 	xennet_free_netdev(netdev);
1731 	return ERR_PTR(err);
1732 }
1733 
1734 /*
1735  * Entry point to this code when a new device is created.  Allocate the basic
1736  * structures and the ring buffers for communication with the backend, and
1737  * inform the backend of the appropriate details for those.
1738  */
1739 static int netfront_probe(struct xenbus_device *dev,
1740 			  const struct xenbus_device_id *id)
1741 {
1742 	int err;
1743 	struct net_device *netdev;
1744 	struct netfront_info *info;
1745 
1746 	netdev = xennet_create_dev(dev);
1747 	if (IS_ERR(netdev)) {
1748 		err = PTR_ERR(netdev);
1749 		xenbus_dev_fatal(dev, err, "creating netdev");
1750 		return err;
1751 	}
1752 
1753 	info = netdev_priv(netdev);
1754 	dev_set_drvdata(&dev->dev, info);
1755 #ifdef CONFIG_SYSFS
1756 	info->netdev->sysfs_groups[0] = &xennet_dev_group;
1757 #endif
1758 
1759 	return 0;
1760 }
1761 
1762 static void xennet_end_access(int ref, void *page)
1763 {
1764 	/* This frees the page as a side-effect */
1765 	if (ref != GRANT_INVALID_REF)
1766 		gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1767 }
1768 
1769 static void xennet_disconnect_backend(struct netfront_info *info)
1770 {
1771 	unsigned int i = 0;
1772 	unsigned int num_queues = info->netdev->real_num_tx_queues;
1773 
1774 	netif_carrier_off(info->netdev);
1775 
1776 	for (i = 0; i < num_queues && info->queues; ++i) {
1777 		struct netfront_queue *queue = &info->queues[i];
1778 
1779 		del_timer_sync(&queue->rx_refill_timer);
1780 
1781 		if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1782 			unbind_from_irqhandler(queue->tx_irq, queue);
1783 		if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1784 			unbind_from_irqhandler(queue->tx_irq, queue);
1785 			unbind_from_irqhandler(queue->rx_irq, queue);
1786 		}
1787 		queue->tx_evtchn = queue->rx_evtchn = 0;
1788 		queue->tx_irq = queue->rx_irq = 0;
1789 
1790 		if (netif_running(info->netdev))
1791 			napi_synchronize(&queue->napi);
1792 
1793 		xennet_release_tx_bufs(queue);
1794 		xennet_release_rx_bufs(queue);
1795 		gnttab_free_grant_references(queue->gref_tx_head);
1796 		gnttab_free_grant_references(queue->gref_rx_head);
1797 
1798 		/* End access and free the pages */
1799 		xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1800 		xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1801 
1802 		queue->tx_ring_ref = GRANT_INVALID_REF;
1803 		queue->rx_ring_ref = GRANT_INVALID_REF;
1804 		queue->tx.sring = NULL;
1805 		queue->rx.sring = NULL;
1806 
1807 		page_pool_destroy(queue->page_pool);
1808 	}
1809 }
1810 
1811 /*
1812  * We are reconnecting to the backend, due to a suspend/resume, or a backend
1813  * driver restart.  We tear down our netif structure and recreate it, but
1814  * leave the device-layer structures intact so that this is transparent to the
1815  * rest of the kernel.
1816  */
1817 static int netfront_resume(struct xenbus_device *dev)
1818 {
1819 	struct netfront_info *info = dev_get_drvdata(&dev->dev);
1820 
1821 	dev_dbg(&dev->dev, "%s\n", dev->nodename);
1822 
1823 	netif_tx_lock_bh(info->netdev);
1824 	netif_device_detach(info->netdev);
1825 	netif_tx_unlock_bh(info->netdev);
1826 
1827 	xennet_disconnect_backend(info);
1828 	return 0;
1829 }
1830 
1831 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1832 {
1833 	char *s, *e, *macstr;
1834 	int i;
1835 
1836 	macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1837 	if (IS_ERR(macstr))
1838 		return PTR_ERR(macstr);
1839 
1840 	for (i = 0; i < ETH_ALEN; i++) {
1841 		mac[i] = simple_strtoul(s, &e, 16);
1842 		if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1843 			kfree(macstr);
1844 			return -ENOENT;
1845 		}
1846 		s = e+1;
1847 	}
1848 
1849 	kfree(macstr);
1850 	return 0;
1851 }
1852 
1853 static int setup_netfront_single(struct netfront_queue *queue)
1854 {
1855 	int err;
1856 
1857 	err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1858 	if (err < 0)
1859 		goto fail;
1860 
1861 	err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1862 						xennet_interrupt, 0,
1863 						queue->info->netdev->name,
1864 						queue);
1865 	if (err < 0)
1866 		goto bind_fail;
1867 	queue->rx_evtchn = queue->tx_evtchn;
1868 	queue->rx_irq = queue->tx_irq = err;
1869 
1870 	return 0;
1871 
1872 bind_fail:
1873 	xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1874 	queue->tx_evtchn = 0;
1875 fail:
1876 	return err;
1877 }
1878 
1879 static int setup_netfront_split(struct netfront_queue *queue)
1880 {
1881 	int err;
1882 
1883 	err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1884 	if (err < 0)
1885 		goto fail;
1886 	err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1887 	if (err < 0)
1888 		goto alloc_rx_evtchn_fail;
1889 
1890 	snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1891 		 "%s-tx", queue->name);
1892 	err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1893 						xennet_tx_interrupt, 0,
1894 						queue->tx_irq_name, queue);
1895 	if (err < 0)
1896 		goto bind_tx_fail;
1897 	queue->tx_irq = err;
1898 
1899 	snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1900 		 "%s-rx", queue->name);
1901 	err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1902 						xennet_rx_interrupt, 0,
1903 						queue->rx_irq_name, queue);
1904 	if (err < 0)
1905 		goto bind_rx_fail;
1906 	queue->rx_irq = err;
1907 
1908 	return 0;
1909 
1910 bind_rx_fail:
1911 	unbind_from_irqhandler(queue->tx_irq, queue);
1912 	queue->tx_irq = 0;
1913 bind_tx_fail:
1914 	xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1915 	queue->rx_evtchn = 0;
1916 alloc_rx_evtchn_fail:
1917 	xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1918 	queue->tx_evtchn = 0;
1919 fail:
1920 	return err;
1921 }
1922 
1923 static int setup_netfront(struct xenbus_device *dev,
1924 			struct netfront_queue *queue, unsigned int feature_split_evtchn)
1925 {
1926 	struct xen_netif_tx_sring *txs;
1927 	struct xen_netif_rx_sring *rxs = NULL;
1928 	grant_ref_t gref;
1929 	int err;
1930 
1931 	queue->tx_ring_ref = GRANT_INVALID_REF;
1932 	queue->rx_ring_ref = GRANT_INVALID_REF;
1933 	queue->rx.sring = NULL;
1934 	queue->tx.sring = NULL;
1935 
1936 	txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1937 	if (!txs) {
1938 		err = -ENOMEM;
1939 		xenbus_dev_fatal(dev, err, "allocating tx ring page");
1940 		goto fail;
1941 	}
1942 	SHARED_RING_INIT(txs);
1943 	FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1944 
1945 	err = xenbus_grant_ring(dev, txs, 1, &gref);
1946 	if (err < 0)
1947 		goto fail;
1948 	queue->tx_ring_ref = gref;
1949 
1950 	rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1951 	if (!rxs) {
1952 		err = -ENOMEM;
1953 		xenbus_dev_fatal(dev, err, "allocating rx ring page");
1954 		goto fail;
1955 	}
1956 	SHARED_RING_INIT(rxs);
1957 	FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1958 
1959 	err = xenbus_grant_ring(dev, rxs, 1, &gref);
1960 	if (err < 0)
1961 		goto fail;
1962 	queue->rx_ring_ref = gref;
1963 
1964 	if (feature_split_evtchn)
1965 		err = setup_netfront_split(queue);
1966 	/* setup single event channel if
1967 	 *  a) feature-split-event-channels == 0
1968 	 *  b) feature-split-event-channels == 1 but failed to setup
1969 	 */
1970 	if (!feature_split_evtchn || err)
1971 		err = setup_netfront_single(queue);
1972 
1973 	if (err)
1974 		goto fail;
1975 
1976 	return 0;
1977 
1978 	/* If we fail to setup netfront, it is safe to just revoke access to
1979 	 * granted pages because backend is not accessing it at this point.
1980 	 */
1981  fail:
1982 	if (queue->rx_ring_ref != GRANT_INVALID_REF) {
1983 		gnttab_end_foreign_access(queue->rx_ring_ref, 0,
1984 					  (unsigned long)rxs);
1985 		queue->rx_ring_ref = GRANT_INVALID_REF;
1986 	} else {
1987 		free_page((unsigned long)rxs);
1988 	}
1989 	if (queue->tx_ring_ref != GRANT_INVALID_REF) {
1990 		gnttab_end_foreign_access(queue->tx_ring_ref, 0,
1991 					  (unsigned long)txs);
1992 		queue->tx_ring_ref = GRANT_INVALID_REF;
1993 	} else {
1994 		free_page((unsigned long)txs);
1995 	}
1996 	return err;
1997 }
1998 
1999 /* Queue-specific initialisation
2000  * This used to be done in xennet_create_dev() but must now
2001  * be run per-queue.
2002  */
2003 static int xennet_init_queue(struct netfront_queue *queue)
2004 {
2005 	unsigned short i;
2006 	int err = 0;
2007 	char *devid;
2008 
2009 	spin_lock_init(&queue->tx_lock);
2010 	spin_lock_init(&queue->rx_lock);
2011 	spin_lock_init(&queue->rx_cons_lock);
2012 
2013 	timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
2014 
2015 	devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
2016 	snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
2017 		 devid, queue->id);
2018 
2019 	/* Initialise tx_skb_freelist as a free chain containing every entry. */
2020 	queue->tx_skb_freelist = 0;
2021 	queue->tx_pend_queue = TX_LINK_NONE;
2022 	for (i = 0; i < NET_TX_RING_SIZE; i++) {
2023 		queue->tx_link[i] = i + 1;
2024 		queue->grant_tx_ref[i] = GRANT_INVALID_REF;
2025 		queue->grant_tx_page[i] = NULL;
2026 	}
2027 	queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
2028 
2029 	/* Clear out rx_skbs */
2030 	for (i = 0; i < NET_RX_RING_SIZE; i++) {
2031 		queue->rx_skbs[i] = NULL;
2032 		queue->grant_rx_ref[i] = GRANT_INVALID_REF;
2033 	}
2034 
2035 	/* A grant for every tx ring slot */
2036 	if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
2037 					  &queue->gref_tx_head) < 0) {
2038 		pr_alert("can't alloc tx grant refs\n");
2039 		err = -ENOMEM;
2040 		goto exit;
2041 	}
2042 
2043 	/* A grant for every rx ring slot */
2044 	if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
2045 					  &queue->gref_rx_head) < 0) {
2046 		pr_alert("can't alloc rx grant refs\n");
2047 		err = -ENOMEM;
2048 		goto exit_free_tx;
2049 	}
2050 
2051 	return 0;
2052 
2053  exit_free_tx:
2054 	gnttab_free_grant_references(queue->gref_tx_head);
2055  exit:
2056 	return err;
2057 }
2058 
2059 static int write_queue_xenstore_keys(struct netfront_queue *queue,
2060 			   struct xenbus_transaction *xbt, int write_hierarchical)
2061 {
2062 	/* Write the queue-specific keys into XenStore in the traditional
2063 	 * way for a single queue, or in a queue subkeys for multiple
2064 	 * queues.
2065 	 */
2066 	struct xenbus_device *dev = queue->info->xbdev;
2067 	int err;
2068 	const char *message;
2069 	char *path;
2070 	size_t pathsize;
2071 
2072 	/* Choose the correct place to write the keys */
2073 	if (write_hierarchical) {
2074 		pathsize = strlen(dev->nodename) + 10;
2075 		path = kzalloc(pathsize, GFP_KERNEL);
2076 		if (!path) {
2077 			err = -ENOMEM;
2078 			message = "out of memory while writing ring references";
2079 			goto error;
2080 		}
2081 		snprintf(path, pathsize, "%s/queue-%u",
2082 				dev->nodename, queue->id);
2083 	} else {
2084 		path = (char *)dev->nodename;
2085 	}
2086 
2087 	/* Write ring references */
2088 	err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
2089 			queue->tx_ring_ref);
2090 	if (err) {
2091 		message = "writing tx-ring-ref";
2092 		goto error;
2093 	}
2094 
2095 	err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
2096 			queue->rx_ring_ref);
2097 	if (err) {
2098 		message = "writing rx-ring-ref";
2099 		goto error;
2100 	}
2101 
2102 	/* Write event channels; taking into account both shared
2103 	 * and split event channel scenarios.
2104 	 */
2105 	if (queue->tx_evtchn == queue->rx_evtchn) {
2106 		/* Shared event channel */
2107 		err = xenbus_printf(*xbt, path,
2108 				"event-channel", "%u", queue->tx_evtchn);
2109 		if (err) {
2110 			message = "writing event-channel";
2111 			goto error;
2112 		}
2113 	} else {
2114 		/* Split event channels */
2115 		err = xenbus_printf(*xbt, path,
2116 				"event-channel-tx", "%u", queue->tx_evtchn);
2117 		if (err) {
2118 			message = "writing event-channel-tx";
2119 			goto error;
2120 		}
2121 
2122 		err = xenbus_printf(*xbt, path,
2123 				"event-channel-rx", "%u", queue->rx_evtchn);
2124 		if (err) {
2125 			message = "writing event-channel-rx";
2126 			goto error;
2127 		}
2128 	}
2129 
2130 	if (write_hierarchical)
2131 		kfree(path);
2132 	return 0;
2133 
2134 error:
2135 	if (write_hierarchical)
2136 		kfree(path);
2137 	xenbus_dev_fatal(dev, err, "%s", message);
2138 	return err;
2139 }
2140 
2141 
2142 
2143 static int xennet_create_page_pool(struct netfront_queue *queue)
2144 {
2145 	int err;
2146 	struct page_pool_params pp_params = {
2147 		.order = 0,
2148 		.flags = 0,
2149 		.pool_size = NET_RX_RING_SIZE,
2150 		.nid = NUMA_NO_NODE,
2151 		.dev = &queue->info->netdev->dev,
2152 		.offset = XDP_PACKET_HEADROOM,
2153 		.max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
2154 	};
2155 
2156 	queue->page_pool = page_pool_create(&pp_params);
2157 	if (IS_ERR(queue->page_pool)) {
2158 		err = PTR_ERR(queue->page_pool);
2159 		queue->page_pool = NULL;
2160 		return err;
2161 	}
2162 
2163 	err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
2164 			       queue->id, 0);
2165 	if (err) {
2166 		netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
2167 		goto err_free_pp;
2168 	}
2169 
2170 	err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
2171 					 MEM_TYPE_PAGE_POOL, queue->page_pool);
2172 	if (err) {
2173 		netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
2174 		goto err_unregister_rxq;
2175 	}
2176 	return 0;
2177 
2178 err_unregister_rxq:
2179 	xdp_rxq_info_unreg(&queue->xdp_rxq);
2180 err_free_pp:
2181 	page_pool_destroy(queue->page_pool);
2182 	queue->page_pool = NULL;
2183 	return err;
2184 }
2185 
2186 static int xennet_create_queues(struct netfront_info *info,
2187 				unsigned int *num_queues)
2188 {
2189 	unsigned int i;
2190 	int ret;
2191 
2192 	info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
2193 			       GFP_KERNEL);
2194 	if (!info->queues)
2195 		return -ENOMEM;
2196 
2197 	for (i = 0; i < *num_queues; i++) {
2198 		struct netfront_queue *queue = &info->queues[i];
2199 
2200 		queue->id = i;
2201 		queue->info = info;
2202 
2203 		ret = xennet_init_queue(queue);
2204 		if (ret < 0) {
2205 			dev_warn(&info->xbdev->dev,
2206 				 "only created %d queues\n", i);
2207 			*num_queues = i;
2208 			break;
2209 		}
2210 
2211 		/* use page pool recycling instead of buddy allocator */
2212 		ret = xennet_create_page_pool(queue);
2213 		if (ret < 0) {
2214 			dev_err(&info->xbdev->dev, "can't allocate page pool\n");
2215 			*num_queues = i;
2216 			return ret;
2217 		}
2218 
2219 		netif_napi_add(queue->info->netdev, &queue->napi,
2220 			       xennet_poll, 64);
2221 		if (netif_running(info->netdev))
2222 			napi_enable(&queue->napi);
2223 	}
2224 
2225 	netif_set_real_num_tx_queues(info->netdev, *num_queues);
2226 
2227 	if (*num_queues == 0) {
2228 		dev_err(&info->xbdev->dev, "no queues\n");
2229 		return -EINVAL;
2230 	}
2231 	return 0;
2232 }
2233 
2234 /* Common code used when first setting up, and when resuming. */
2235 static int talk_to_netback(struct xenbus_device *dev,
2236 			   struct netfront_info *info)
2237 {
2238 	const char *message;
2239 	struct xenbus_transaction xbt;
2240 	int err;
2241 	unsigned int feature_split_evtchn;
2242 	unsigned int i = 0;
2243 	unsigned int max_queues = 0;
2244 	struct netfront_queue *queue = NULL;
2245 	unsigned int num_queues = 1;
2246 	u8 addr[ETH_ALEN];
2247 
2248 	info->netdev->irq = 0;
2249 
2250 	/* Check if backend supports multiple queues */
2251 	max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2252 					  "multi-queue-max-queues", 1);
2253 	num_queues = min(max_queues, xennet_max_queues);
2254 
2255 	/* Check feature-split-event-channels */
2256 	feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2257 					"feature-split-event-channels", 0);
2258 
2259 	/* Read mac addr. */
2260 	err = xen_net_read_mac(dev, addr);
2261 	if (err) {
2262 		xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
2263 		goto out_unlocked;
2264 	}
2265 	eth_hw_addr_set(info->netdev, addr);
2266 
2267 	info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
2268 							      "feature-xdp-headroom", 0);
2269 	if (info->netback_has_xdp_headroom) {
2270 		/* set the current xen-netfront xdp state */
2271 		err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
2272 					  NETBACK_XDP_HEADROOM_ENABLE :
2273 					  NETBACK_XDP_HEADROOM_DISABLE);
2274 		if (err)
2275 			goto out_unlocked;
2276 	}
2277 
2278 	rtnl_lock();
2279 	if (info->queues)
2280 		xennet_destroy_queues(info);
2281 
2282 	/* For the case of a reconnect reset the "broken" indicator. */
2283 	info->broken = false;
2284 
2285 	err = xennet_create_queues(info, &num_queues);
2286 	if (err < 0) {
2287 		xenbus_dev_fatal(dev, err, "creating queues");
2288 		kfree(info->queues);
2289 		info->queues = NULL;
2290 		goto out;
2291 	}
2292 	rtnl_unlock();
2293 
2294 	/* Create shared ring, alloc event channel -- for each queue */
2295 	for (i = 0; i < num_queues; ++i) {
2296 		queue = &info->queues[i];
2297 		err = setup_netfront(dev, queue, feature_split_evtchn);
2298 		if (err)
2299 			goto destroy_ring;
2300 	}
2301 
2302 again:
2303 	err = xenbus_transaction_start(&xbt);
2304 	if (err) {
2305 		xenbus_dev_fatal(dev, err, "starting transaction");
2306 		goto destroy_ring;
2307 	}
2308 
2309 	if (xenbus_exists(XBT_NIL,
2310 			  info->xbdev->otherend, "multi-queue-max-queues")) {
2311 		/* Write the number of queues */
2312 		err = xenbus_printf(xbt, dev->nodename,
2313 				    "multi-queue-num-queues", "%u", num_queues);
2314 		if (err) {
2315 			message = "writing multi-queue-num-queues";
2316 			goto abort_transaction_no_dev_fatal;
2317 		}
2318 	}
2319 
2320 	if (num_queues == 1) {
2321 		err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2322 		if (err)
2323 			goto abort_transaction_no_dev_fatal;
2324 	} else {
2325 		/* Write the keys for each queue */
2326 		for (i = 0; i < num_queues; ++i) {
2327 			queue = &info->queues[i];
2328 			err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2329 			if (err)
2330 				goto abort_transaction_no_dev_fatal;
2331 		}
2332 	}
2333 
2334 	/* The remaining keys are not queue-specific */
2335 	err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2336 			    1);
2337 	if (err) {
2338 		message = "writing request-rx-copy";
2339 		goto abort_transaction;
2340 	}
2341 
2342 	err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2343 	if (err) {
2344 		message = "writing feature-rx-notify";
2345 		goto abort_transaction;
2346 	}
2347 
2348 	err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2349 	if (err) {
2350 		message = "writing feature-sg";
2351 		goto abort_transaction;
2352 	}
2353 
2354 	err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2355 	if (err) {
2356 		message = "writing feature-gso-tcpv4";
2357 		goto abort_transaction;
2358 	}
2359 
2360 	err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2361 	if (err) {
2362 		message = "writing feature-gso-tcpv6";
2363 		goto abort_transaction;
2364 	}
2365 
2366 	err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2367 			   "1");
2368 	if (err) {
2369 		message = "writing feature-ipv6-csum-offload";
2370 		goto abort_transaction;
2371 	}
2372 
2373 	err = xenbus_transaction_end(xbt, 0);
2374 	if (err) {
2375 		if (err == -EAGAIN)
2376 			goto again;
2377 		xenbus_dev_fatal(dev, err, "completing transaction");
2378 		goto destroy_ring;
2379 	}
2380 
2381 	return 0;
2382 
2383  abort_transaction:
2384 	xenbus_dev_fatal(dev, err, "%s", message);
2385 abort_transaction_no_dev_fatal:
2386 	xenbus_transaction_end(xbt, 1);
2387  destroy_ring:
2388 	xennet_disconnect_backend(info);
2389 	rtnl_lock();
2390 	xennet_destroy_queues(info);
2391  out:
2392 	rtnl_unlock();
2393 out_unlocked:
2394 	device_unregister(&dev->dev);
2395 	return err;
2396 }
2397 
2398 static int xennet_connect(struct net_device *dev)
2399 {
2400 	struct netfront_info *np = netdev_priv(dev);
2401 	unsigned int num_queues = 0;
2402 	int err;
2403 	unsigned int j = 0;
2404 	struct netfront_queue *queue = NULL;
2405 
2406 	if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
2407 		dev_info(&dev->dev,
2408 			 "backend does not support copying receive path\n");
2409 		return -ENODEV;
2410 	}
2411 
2412 	err = talk_to_netback(np->xbdev, np);
2413 	if (err)
2414 		return err;
2415 	if (np->netback_has_xdp_headroom)
2416 		pr_info("backend supports XDP headroom\n");
2417 
2418 	/* talk_to_netback() sets the correct number of queues */
2419 	num_queues = dev->real_num_tx_queues;
2420 
2421 	if (dev->reg_state == NETREG_UNINITIALIZED) {
2422 		err = register_netdev(dev);
2423 		if (err) {
2424 			pr_warn("%s: register_netdev err=%d\n", __func__, err);
2425 			device_unregister(&np->xbdev->dev);
2426 			return err;
2427 		}
2428 	}
2429 
2430 	rtnl_lock();
2431 	netdev_update_features(dev);
2432 	rtnl_unlock();
2433 
2434 	/*
2435 	 * All public and private state should now be sane.  Get
2436 	 * ready to start sending and receiving packets and give the driver
2437 	 * domain a kick because we've probably just requeued some
2438 	 * packets.
2439 	 */
2440 	netif_tx_lock_bh(np->netdev);
2441 	netif_device_attach(np->netdev);
2442 	netif_tx_unlock_bh(np->netdev);
2443 
2444 	netif_carrier_on(np->netdev);
2445 	for (j = 0; j < num_queues; ++j) {
2446 		queue = &np->queues[j];
2447 
2448 		notify_remote_via_irq(queue->tx_irq);
2449 		if (queue->tx_irq != queue->rx_irq)
2450 			notify_remote_via_irq(queue->rx_irq);
2451 
2452 		spin_lock_irq(&queue->tx_lock);
2453 		xennet_tx_buf_gc(queue);
2454 		spin_unlock_irq(&queue->tx_lock);
2455 
2456 		spin_lock_bh(&queue->rx_lock);
2457 		xennet_alloc_rx_buffers(queue);
2458 		spin_unlock_bh(&queue->rx_lock);
2459 	}
2460 
2461 	return 0;
2462 }
2463 
2464 /*
2465  * Callback received when the backend's state changes.
2466  */
2467 static void netback_changed(struct xenbus_device *dev,
2468 			    enum xenbus_state backend_state)
2469 {
2470 	struct netfront_info *np = dev_get_drvdata(&dev->dev);
2471 	struct net_device *netdev = np->netdev;
2472 
2473 	dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2474 
2475 	wake_up_all(&module_wq);
2476 
2477 	switch (backend_state) {
2478 	case XenbusStateInitialising:
2479 	case XenbusStateInitialised:
2480 	case XenbusStateReconfiguring:
2481 	case XenbusStateReconfigured:
2482 	case XenbusStateUnknown:
2483 		break;
2484 
2485 	case XenbusStateInitWait:
2486 		if (dev->state != XenbusStateInitialising)
2487 			break;
2488 		if (xennet_connect(netdev) != 0)
2489 			break;
2490 		xenbus_switch_state(dev, XenbusStateConnected);
2491 		break;
2492 
2493 	case XenbusStateConnected:
2494 		netdev_notify_peers(netdev);
2495 		break;
2496 
2497 	case XenbusStateClosed:
2498 		if (dev->state == XenbusStateClosed)
2499 			break;
2500 		fallthrough;	/* Missed the backend's CLOSING state */
2501 	case XenbusStateClosing:
2502 		xenbus_frontend_closed(dev);
2503 		break;
2504 	}
2505 }
2506 
2507 static const struct xennet_stat {
2508 	char name[ETH_GSTRING_LEN];
2509 	u16 offset;
2510 } xennet_stats[] = {
2511 	{
2512 		"rx_gso_checksum_fixup",
2513 		offsetof(struct netfront_info, rx_gso_checksum_fixup)
2514 	},
2515 };
2516 
2517 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2518 {
2519 	switch (string_set) {
2520 	case ETH_SS_STATS:
2521 		return ARRAY_SIZE(xennet_stats);
2522 	default:
2523 		return -EINVAL;
2524 	}
2525 }
2526 
2527 static void xennet_get_ethtool_stats(struct net_device *dev,
2528 				     struct ethtool_stats *stats, u64 * data)
2529 {
2530 	void *np = netdev_priv(dev);
2531 	int i;
2532 
2533 	for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2534 		data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2535 }
2536 
2537 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2538 {
2539 	int i;
2540 
2541 	switch (stringset) {
2542 	case ETH_SS_STATS:
2543 		for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2544 			memcpy(data + i * ETH_GSTRING_LEN,
2545 			       xennet_stats[i].name, ETH_GSTRING_LEN);
2546 		break;
2547 	}
2548 }
2549 
2550 static const struct ethtool_ops xennet_ethtool_ops =
2551 {
2552 	.get_link = ethtool_op_get_link,
2553 
2554 	.get_sset_count = xennet_get_sset_count,
2555 	.get_ethtool_stats = xennet_get_ethtool_stats,
2556 	.get_strings = xennet_get_strings,
2557 	.get_ts_info = ethtool_op_get_ts_info,
2558 };
2559 
2560 #ifdef CONFIG_SYSFS
2561 static ssize_t show_rxbuf(struct device *dev,
2562 			  struct device_attribute *attr, char *buf)
2563 {
2564 	return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2565 }
2566 
2567 static ssize_t store_rxbuf(struct device *dev,
2568 			   struct device_attribute *attr,
2569 			   const char *buf, size_t len)
2570 {
2571 	char *endp;
2572 
2573 	if (!capable(CAP_NET_ADMIN))
2574 		return -EPERM;
2575 
2576 	simple_strtoul(buf, &endp, 0);
2577 	if (endp == buf)
2578 		return -EBADMSG;
2579 
2580 	/* rxbuf_min and rxbuf_max are no longer configurable. */
2581 
2582 	return len;
2583 }
2584 
2585 static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2586 static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2587 static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
2588 
2589 static struct attribute *xennet_dev_attrs[] = {
2590 	&dev_attr_rxbuf_min.attr,
2591 	&dev_attr_rxbuf_max.attr,
2592 	&dev_attr_rxbuf_cur.attr,
2593 	NULL
2594 };
2595 
2596 static const struct attribute_group xennet_dev_group = {
2597 	.attrs = xennet_dev_attrs
2598 };
2599 #endif /* CONFIG_SYSFS */
2600 
2601 static void xennet_bus_close(struct xenbus_device *dev)
2602 {
2603 	int ret;
2604 
2605 	if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2606 		return;
2607 	do {
2608 		xenbus_switch_state(dev, XenbusStateClosing);
2609 		ret = wait_event_timeout(module_wq,
2610 				   xenbus_read_driver_state(dev->otherend) ==
2611 				   XenbusStateClosing ||
2612 				   xenbus_read_driver_state(dev->otherend) ==
2613 				   XenbusStateClosed ||
2614 				   xenbus_read_driver_state(dev->otherend) ==
2615 				   XenbusStateUnknown,
2616 				   XENNET_TIMEOUT);
2617 	} while (!ret);
2618 
2619 	if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2620 		return;
2621 
2622 	do {
2623 		xenbus_switch_state(dev, XenbusStateClosed);
2624 		ret = wait_event_timeout(module_wq,
2625 				   xenbus_read_driver_state(dev->otherend) ==
2626 				   XenbusStateClosed ||
2627 				   xenbus_read_driver_state(dev->otherend) ==
2628 				   XenbusStateUnknown,
2629 				   XENNET_TIMEOUT);
2630 	} while (!ret);
2631 }
2632 
2633 static int xennet_remove(struct xenbus_device *dev)
2634 {
2635 	struct netfront_info *info = dev_get_drvdata(&dev->dev);
2636 
2637 	xennet_bus_close(dev);
2638 	xennet_disconnect_backend(info);
2639 
2640 	if (info->netdev->reg_state == NETREG_REGISTERED)
2641 		unregister_netdev(info->netdev);
2642 
2643 	if (info->queues) {
2644 		rtnl_lock();
2645 		xennet_destroy_queues(info);
2646 		rtnl_unlock();
2647 	}
2648 	xennet_free_netdev(info->netdev);
2649 
2650 	return 0;
2651 }
2652 
2653 static const struct xenbus_device_id netfront_ids[] = {
2654 	{ "vif" },
2655 	{ "" }
2656 };
2657 
2658 static struct xenbus_driver netfront_driver = {
2659 	.ids = netfront_ids,
2660 	.probe = netfront_probe,
2661 	.remove = xennet_remove,
2662 	.resume = netfront_resume,
2663 	.otherend_changed = netback_changed,
2664 };
2665 
2666 static int __init netif_init(void)
2667 {
2668 	if (!xen_domain())
2669 		return -ENODEV;
2670 
2671 	if (!xen_has_pv_nic_devices())
2672 		return -ENODEV;
2673 
2674 	pr_info("Initialising Xen virtual ethernet driver\n");
2675 
2676 	/* Allow as many queues as there are CPUs inut max. 8 if user has not
2677 	 * specified a value.
2678 	 */
2679 	if (xennet_max_queues == 0)
2680 		xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2681 					  num_online_cpus());
2682 
2683 	return xenbus_register_frontend(&netfront_driver);
2684 }
2685 module_init(netif_init);
2686 
2687 
2688 static void __exit netif_exit(void)
2689 {
2690 	xenbus_unregister_driver(&netfront_driver);
2691 }
2692 module_exit(netif_exit);
2693 
2694 MODULE_DESCRIPTION("Xen virtual network device frontend");
2695 MODULE_LICENSE("GPL");
2696 MODULE_ALIAS("xen:vif");
2697 MODULE_ALIAS("xennet");
2698