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