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