2 * Virtual network driver for conversing with remote driver backends.
4 * Copyright (c) 2002-2005, K A Fraser
5 * Copyright (c) 2005, XenSource Ltd
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:
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:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
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
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
42 #include <linux/udp.h>
43 #include <linux/moduleparam.h>
45 #include <linux/slab.h>
47 #include <linux/bpf.h>
48 #include <net/page_pool.h>
49 #include <linux/bpf_trace.h>
52 #include <xen/xenbus.h>
53 #include <xen/events.h>
55 #include <xen/platform_pci.h>
56 #include <xen/grant_table.h>
58 #include <xen/interface/io/netif.h>
59 #include <xen/interface/memory.h>
60 #include <xen/interface/grant_table.h>
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");
69 #define XENNET_TIMEOUT (5 * HZ)
71 static const struct ethtool_ops xennet_ethtool_ops;
77 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
79 #define RX_COPY_THRESHOLD 256
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)
84 /* Minimum number of Rx slots (includes slot for GSO metadata). */
85 #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
87 /* Queue name is interface name with "-qNNN" appended */
88 #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
90 /* IRQ name is queue name with "-tx" or "-rx" appended */
91 #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
93 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
95 struct netfront_stats {
98 struct u64_stats_sync syncp;
101 struct netfront_info;
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;
108 struct bpf_prog __rcu *xdp_prog;
110 struct napi_struct napi;
112 /* Split event channels support, tx_* == rx_* when using
113 * single event channel.
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 */
122 struct xen_netif_tx_front_ring tx;
126 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
127 * are linked from tx_skb_freelist through tx_link.
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;
139 spinlock_t rx_lock ____cacheline_aligned_in_smp;
140 struct xen_netif_rx_front_ring rx;
143 struct timer_list rx_refill_timer;
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];
149 unsigned int rx_rsp_unconsumed;
150 spinlock_t rx_cons_lock;
152 struct page_pool *page_pool;
153 struct xdp_rxq_info xdp_rxq;
156 struct netfront_info {
157 struct list_head list;
158 struct net_device *netdev;
160 struct xenbus_device *xbdev;
162 /* Multi-queue support */
163 struct netfront_queue *queues;
166 struct netfront_stats __percpu *rx_stats;
167 struct netfront_stats __percpu *tx_stats;
170 bool netback_has_xdp_headroom;
171 bool netfront_xdp_enabled;
173 /* Is device behaving sane? */
176 atomic_t rx_gso_checksum_fixup;
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];
185 * Access macros for acquiring freeing slots in tx_skbs[].
188 static void add_id_to_list(unsigned *head, unsigned short *list,
195 static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
197 unsigned int id = *head;
199 if (id != TX_LINK_NONE) {
201 list[id] = TX_LINK_NONE;
206 static int xennet_rxidx(RING_IDX idx)
208 return idx & (NET_RX_RING_SIZE - 1);
211 static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
214 int i = xennet_rxidx(ri);
215 struct sk_buff *skb = queue->rx_skbs[i];
216 queue->rx_skbs[i] = NULL;
220 static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
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;
230 static const struct attribute_group xennet_dev_group;
233 static bool xennet_can_sg(struct net_device *dev)
235 return dev->features & NETIF_F_SG;
239 static void rx_refill_timeout(struct timer_list *t)
241 struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
242 napi_schedule(&queue->napi);
245 static int netfront_tx_slot_available(struct netfront_queue *queue)
247 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
248 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
251 static void xennet_maybe_wake_tx(struct netfront_queue *queue)
253 struct net_device *dev = queue->info->netdev;
254 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
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));
263 static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
268 skb = __netdev_alloc_skb(queue->info->netdev,
269 RX_COPY_THRESHOLD + NET_IP_ALIGN,
270 GFP_ATOMIC | __GFP_NOWARN);
274 page = page_pool_dev_alloc_pages(queue->page_pool);
275 if (unlikely(!page)) {
279 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
281 /* Align ip header to a 16 bytes boundary */
282 skb_reserve(skb, NET_IP_ALIGN);
283 skb->dev = queue->info->netdev;
289 static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
291 RING_IDX req_prod = queue->rx.req_prod_pvt;
295 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
298 for (req_prod = queue->rx.req_prod_pvt;
299 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
305 struct xen_netif_rx_request *req;
307 skb = xennet_alloc_one_rx_buffer(queue);
313 id = xennet_rxidx(req_prod);
315 BUG_ON(queue->rx_skbs[id]);
316 queue->rx_skbs[id] = skb;
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;
322 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
324 req = RING_GET_REQUEST(&queue->rx, req_prod);
325 gnttab_page_grant_foreign_access_ref_one(ref,
326 queue->info->xbdev->otherend_id,
333 queue->rx.req_prod_pvt = req_prod;
335 /* Try again later if there are not enough requests or skb allocation
337 * Enough requests is quantified as the sum of newly created slots and
338 * the unconsumed slots at the backend.
340 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
342 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
346 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
348 notify_remote_via_irq(queue->rx_irq);
351 static int xennet_open(struct net_device *dev)
353 struct netfront_info *np = netdev_priv(dev);
354 unsigned int num_queues = dev->real_num_tx_queues;
356 struct netfront_queue *queue = NULL;
358 if (!np->queues || np->broken)
361 for (i = 0; i < num_queues; ++i) {
362 queue = &np->queues[i];
363 napi_enable(&queue->napi);
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);
372 spin_unlock_bh(&queue->rx_lock);
375 netif_tx_start_all_queues(dev);
380 static bool xennet_tx_buf_gc(struct netfront_queue *queue)
386 bool work_done = false;
387 const struct device *dev = &queue->info->netdev->dev;
389 BUG_ON(!netif_carrier_ok(queue->info->netdev));
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);
398 rmb(); /* Ensure we see responses up to 'rp'. */
400 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
401 struct xen_netif_tx_response txrsp;
405 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
406 if (txrsp.status == XEN_NETIF_RSP_NULL)
410 if (id >= RING_SIZE(&queue->tx)) {
412 "Response has incorrect id (%u)\n",
416 if (queue->tx_link[id] != TX_PENDING) {
418 "Response for inactive request\n");
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]))) {
428 "Grant still in use by backend domain\n");
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);
439 queue->tx.rsp_cons = prod;
441 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
442 } while (more_to_do);
444 xennet_maybe_wake_tx(queue);
449 queue->info->broken = true;
450 dev_alert(dev, "Disabled for further use\n");
455 struct xennet_gnttab_make_txreq {
456 struct netfront_queue *queue;
459 struct xen_netif_tx_request *tx; /* Last request on ring page */
460 struct xen_netif_tx_request tx_local; /* Last request local copy*/
464 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
465 unsigned int len, void *data)
467 struct xennet_gnttab_make_txreq *info = data;
469 struct xen_netif_tx_request *tx;
471 /* convenient aliases */
472 struct page *page = info->page;
473 struct netfront_queue *queue = info->queue;
474 struct sk_buff *skb = info->skb;
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));
481 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
482 gfn, GNTMAP_readonly);
484 queue->tx_skbs[id] = skb;
485 queue->grant_tx_page[id] = page;
486 queue->grant_tx_ref[id] = ref;
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;
494 *tx = info->tx_local;
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.
500 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
503 info->size += info->tx_local.size;
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)
512 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
517 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
518 unsigned int len, void *data)
520 struct xennet_gnttab_make_txreq *info = data;
522 info->tx->flags |= XEN_NETTXF_more_data;
524 xennet_tx_setup_grant(gfn, offset, len, data);
527 static void xennet_make_txreqs(
528 struct xennet_gnttab_make_txreq *info,
530 unsigned int offset, unsigned int len)
532 /* Skip unused frames from start of page */
533 page += offset >> PAGE_SHIFT;
534 offset &= ~PAGE_MASK;
540 gnttab_foreach_grant_in_range(page, offset, len,
541 xennet_make_one_txreq,
551 * Count how many ring slots are required to send this skb. Each frag
552 * might be a compound page.
554 static int xennet_count_skb_slots(struct sk_buff *skb)
556 int i, frags = skb_shinfo(skb)->nr_frags;
559 slots = gnttab_count_grant(offset_in_page(skb->data),
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);
567 /* Skip unused frames from start of page */
568 offset &= ~PAGE_MASK;
570 slots += gnttab_count_grant(offset, size);
576 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
577 struct net_device *sb_dev)
579 unsigned int num_queues = dev->real_num_tx_queues;
583 /* First, check if there is only one queue */
584 if (num_queues == 1) {
587 hash = skb_get_hash(skb);
588 queue_idx = hash % num_queues;
594 static void xennet_mark_tx_pending(struct netfront_queue *queue)
598 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
600 queue->tx_link[i] = TX_PENDING;
603 static int xennet_xdp_xmit_one(struct net_device *dev,
604 struct netfront_queue *queue,
605 struct xdp_frame *xdpf)
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 = {
612 .page = virt_to_page(xdpf->data),
616 xennet_make_first_txreq(&info,
617 offset_in_page(xdpf->data),
620 xennet_mark_tx_pending(queue);
622 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
624 notify_remote_via_irq(queue->tx_irq);
626 u64_stats_update_begin(&tx_stats->syncp);
627 tx_stats->bytes += xdpf->len;
629 u64_stats_update_end(&tx_stats->syncp);
631 xennet_tx_buf_gc(queue);
636 static int xennet_xdp_xmit(struct net_device *dev, int n,
637 struct xdp_frame **frames, u32 flags)
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;
646 if (unlikely(np->broken))
648 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
651 queue = &np->queues[smp_processor_id() % num_queues];
653 spin_lock_irqsave(&queue->tx_lock, irq_flags);
654 for (i = 0; i < n; i++) {
655 struct xdp_frame *xdpf = frames[i];
659 if (xennet_xdp_xmit_one(dev, queue, xdpf))
663 spin_unlock_irqrestore(&queue->tx_lock, irq_flags);
669 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
671 static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
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;
683 struct netfront_queue *queue = NULL;
684 struct xennet_gnttab_make_txreq info = { };
685 unsigned int num_queues = dev->real_num_tx_queues;
687 struct sk_buff *nskb;
689 /* Drop the packet if no queues are set up */
692 if (unlikely(np->broken))
694 /* Determine which queue to transmit this SKB on */
695 queue_index = skb_get_queue_mapping(skb);
696 queue = &np->queues[queue_index];
698 /* If skb->len is too big for wire format, drop skb and alert
699 * user about misconfiguration.
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",
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",
712 if (skb_linearize(skb))
716 page = virt_to_page(skb->data);
717 offset = offset_in_page(skb->data);
719 /* The first req should be at least ETH_HLEN size or the packet will be
720 * dropped by netback.
722 if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
723 nskb = skb_copy(skb, GFP_ATOMIC);
726 dev_consume_skb_any(skb);
728 page = virt_to_page(skb->data);
729 offset = offset_in_page(skb->data);
732 len = skb_headlen(skb);
734 spin_lock_irqsave(&queue->tx_lock, flags);
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);
743 /* First request for the linear area. */
747 first_tx = xennet_make_first_txreq(&info, offset, len);
748 offset += info.tx_local.size;
749 if (offset == PAGE_SIZE) {
753 len -= info.tx_local.size;
755 if (skb->ip_summed == CHECKSUM_PARTIAL)
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;
763 /* Optional extra info after the first request. */
764 if (skb_shinfo(skb)->gso_size) {
765 struct xen_netif_extra_info *gso;
767 gso = (struct xen_netif_extra_info *)
768 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
770 first_tx->flags |= XEN_NETTXF_extra_info;
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;
777 gso->u.gso.features = 0;
779 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
783 /* Requests for the rest of the linear area. */
784 xennet_make_txreqs(&info, page, offset, len);
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),
791 skb_frag_size(frag));
794 /* First request has the packet length. */
795 first_tx->size = skb->len;
797 /* timestamp packet in software */
798 skb_tx_timestamp(skb);
800 xennet_mark_tx_pending(queue);
802 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
804 notify_remote_via_irq(queue->tx_irq);
806 u64_stats_update_begin(&tx_stats->syncp);
807 tx_stats->bytes += skb->len;
809 u64_stats_update_end(&tx_stats->syncp);
811 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
812 xennet_tx_buf_gc(queue);
814 if (!netfront_tx_slot_available(queue))
815 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
817 spin_unlock_irqrestore(&queue->tx_lock, flags);
822 dev->stats.tx_dropped++;
823 dev_kfree_skb_any(skb);
827 static int xennet_close(struct net_device *dev)
829 struct netfront_info *np = netdev_priv(dev);
830 unsigned int num_queues = dev->real_num_tx_queues;
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);
841 static void xennet_destroy_queues(struct netfront_info *info)
845 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
846 struct netfront_queue *queue = &info->queues[i];
848 if (netif_running(info->netdev))
849 napi_disable(&queue->napi);
850 netif_napi_del(&queue->napi);
857 static void xennet_uninit(struct net_device *dev)
859 struct netfront_info *np = netdev_priv(dev);
860 xennet_destroy_queues(np);
863 static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
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);
873 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
876 int new = xennet_rxidx(queue->rx.req_prod_pvt);
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++;
886 static int xennet_get_extras(struct netfront_queue *queue,
887 struct xen_netif_extra_info *extras,
891 struct xen_netif_extra_info extra;
892 struct device *dev = &queue->info->netdev->dev;
893 RING_IDX cons = queue->rx.rsp_cons;
900 if (unlikely(cons + 1 == rp)) {
902 dev_warn(dev, "Missing extra info\n");
907 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
909 if (unlikely(!extra.type ||
910 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
912 dev_warn(dev, "Invalid extra type: %d\n",
916 extras[extra.type - 1] = extra;
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);
924 xennet_set_rx_rsp_cons(queue, cons);
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)
932 struct xdp_frame *xdpf;
933 u32 len = rx->status;
937 xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
939 xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM,
942 act = bpf_prog_run_xdp(prog, xdp);
946 xdpf = xdp_convert_buff_to_frame(xdp);
947 err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
949 xdp_return_frame_rx_napi(xdpf);
950 else if (unlikely(err < 0))
951 trace_xdp_exception(queue->info->netdev, prog, act);
955 err = xdp_do_redirect(queue->info->netdev, xdp, prog);
956 *need_xdp_flush = true;
958 trace_xdp_exception(queue->info->netdev, prog, act);
965 trace_xdp_exception(queue->info->netdev, prog, act);
969 bpf_warn_invalid_xdp_action(queue->info->netdev, prog, act);
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)
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;
993 if (rx->flags & XEN_NETRXF_extra_info) {
994 err = xennet_get_extras(queue, extras, rp);
996 if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) {
997 struct xen_netif_extra_info *xdp;
999 xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1];
1000 rx->offset = xdp->u.xdp.headroom;
1003 cons = queue->rx.rsp_cons;
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);
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.
1022 if (ref == INVALID_GRANT_REF) {
1023 if (net_ratelimit())
1024 dev_warn(dev, "Bad rx response id %d.\n",
1030 if (!gnttab_end_foreign_access_ref(ref)) {
1032 "Grant still in use by backend domain\n");
1033 queue->info->broken = true;
1034 dev_alert(dev, "Disabled for further use\n");
1038 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
1041 xdp_prog = rcu_dereference(queue->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)
1051 /* drop the frame */
1057 __skb_queue_tail(list, skb);
1058 if (!(rx->flags & XEN_NETRXF_more_data))
1061 if (cons + slots == rp) {
1062 if (net_ratelimit())
1063 dev_warn(dev, "Need more slots\n");
1068 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
1070 skb = xennet_get_rx_skb(queue, cons + slots);
1071 ref = xennet_get_rx_ref(queue, cons + slots);
1075 if (unlikely(slots > max)) {
1076 if (net_ratelimit())
1077 dev_warn(dev, "Too many slots\n");
1082 xennet_set_rx_rsp_cons(queue, cons + slots);
1087 static int xennet_set_skb_gso(struct sk_buff *skb,
1088 struct xen_netif_extra_info *gso)
1090 if (!gso->u.gso.size) {
1091 if (net_ratelimit())
1092 pr_warn("GSO size must not be zero\n");
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);
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) ?
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;
1116 static int xennet_fill_frags(struct netfront_queue *queue,
1117 struct sk_buff *skb,
1118 struct sk_buff_head *list)
1120 RING_IDX cons = queue->rx.rsp_cons;
1121 struct sk_buff *nskb;
1123 while ((nskb = __skb_dequeue(list))) {
1124 struct xen_netif_rx_response rx;
1125 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
1127 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
1129 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
1130 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1132 BUG_ON(pull_to < skb_headlen(skb));
1133 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1135 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
1136 xennet_set_rx_rsp_cons(queue,
1137 ++cons + skb_queue_len(list));
1142 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1143 skb_frag_page(nfrag),
1144 rx.offset, rx.status, PAGE_SIZE);
1146 skb_shinfo(nskb)->nr_frags = 0;
1150 xennet_set_rx_rsp_cons(queue, cons);
1155 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
1157 bool recalculate_partial_csum = false;
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.
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;
1172 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1173 if (skb->ip_summed != CHECKSUM_PARTIAL)
1176 return skb_checksum_setup(skb, recalculate_partial_csum);
1179 static int handle_incoming_queue(struct netfront_queue *queue,
1180 struct sk_buff_head *rxq)
1182 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
1183 int packets_dropped = 0;
1184 struct sk_buff *skb;
1186 while ((skb = __skb_dequeue(rxq)) != NULL) {
1187 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1189 if (pull_to > skb_headlen(skb))
1190 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
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);
1196 if (checksum_setup(queue->info->netdev, skb)) {
1199 queue->info->netdev->stats.rx_errors++;
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);
1209 napi_gro_receive(&queue->napi, skb);
1212 return packets_dropped;
1215 static int xennet_poll(struct napi_struct *napi, int budget)
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;
1225 struct sk_buff_head rxq;
1226 struct sk_buff_head errq;
1227 struct sk_buff_head tmpq;
1229 bool need_xdp_flush = false;
1231 spin_lock(&queue->rx_lock);
1233 skb_queue_head_init(&rxq);
1234 skb_queue_head_init(&errq);
1235 skb_queue_head_init(&tmpq);
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);
1245 rmb(); /* Ensure we see queued responses up to 'rp'. */
1247 i = queue->rx.rsp_cons;
1249 while ((i != rp) && (work_done < budget)) {
1250 RING_COPY_RESPONSE(&queue->rx, i, rx);
1251 memset(extras, 0, sizeof(rinfo.extras));
1253 err = xennet_get_responses(queue, &rinfo, rp, &tmpq,
1256 if (unlikely(err)) {
1257 if (queue->info->broken) {
1258 spin_unlock(&queue->rx_lock);
1262 while ((skb = __skb_dequeue(&tmpq)))
1263 __skb_queue_tail(&errq, skb);
1264 dev->stats.rx_errors++;
1265 i = queue->rx.rsp_cons;
1269 skb = __skb_dequeue(&tmpq);
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];
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));
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;
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;
1293 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
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;
1301 __skb_queue_tail(&rxq, skb);
1303 i = queue->rx.rsp_cons + 1;
1304 xennet_set_rx_rsp_cons(queue, i);
1310 __skb_queue_purge(&errq);
1312 work_done -= handle_incoming_queue(queue, &rxq);
1314 xennet_alloc_rx_buffers(queue);
1316 if (work_done < budget) {
1319 napi_complete_done(napi, work_done);
1321 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1323 napi_schedule(napi);
1326 spin_unlock(&queue->rx_lock);
1331 static int xennet_change_mtu(struct net_device *dev, int mtu)
1333 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
1341 static void xennet_get_stats64(struct net_device *dev,
1342 struct rtnl_link_stats64 *tot)
1344 struct netfront_info *np = netdev_priv(dev);
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;
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));
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));
1365 tot->rx_packets += rx_packets;
1366 tot->tx_packets += tx_packets;
1367 tot->rx_bytes += rx_bytes;
1368 tot->tx_bytes += tx_bytes;
1371 tot->rx_errors = dev->stats.rx_errors;
1372 tot->tx_dropped = dev->stats.tx_dropped;
1375 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1377 struct sk_buff *skb;
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])
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 (unsigned long)page_address(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);
1397 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1401 spin_lock_bh(&queue->rx_lock);
1403 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1404 struct sk_buff *skb;
1407 skb = queue->rx_skbs[id];
1411 ref = queue->grant_rx_ref[id];
1412 if (ref == INVALID_GRANT_REF)
1415 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1417 /* gnttab_end_foreign_access() needs a page ref until
1418 * foreign access is ended (which may be deferred).
1421 gnttab_end_foreign_access(ref,
1422 (unsigned long)page_address(page));
1423 queue->grant_rx_ref[id] = INVALID_GRANT_REF;
1428 spin_unlock_bh(&queue->rx_lock);
1431 static netdev_features_t xennet_fix_features(struct net_device *dev,
1432 netdev_features_t features)
1434 struct netfront_info *np = netdev_priv(dev);
1436 if (features & NETIF_F_SG &&
1437 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1438 features &= ~NETIF_F_SG;
1440 if (features & NETIF_F_IPV6_CSUM &&
1441 !xenbus_read_unsigned(np->xbdev->otherend,
1442 "feature-ipv6-csum-offload", 0))
1443 features &= ~NETIF_F_IPV6_CSUM;
1445 if (features & NETIF_F_TSO &&
1446 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1447 features &= ~NETIF_F_TSO;
1449 if (features & NETIF_F_TSO6 &&
1450 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1451 features &= ~NETIF_F_TSO6;
1456 static int xennet_set_features(struct net_device *dev,
1457 netdev_features_t features)
1459 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1460 netdev_info(dev, "Reducing MTU because no SG offload");
1461 dev->mtu = ETH_DATA_LEN;
1467 static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
1469 unsigned long flags;
1471 if (unlikely(queue->info->broken))
1474 spin_lock_irqsave(&queue->tx_lock, flags);
1475 if (xennet_tx_buf_gc(queue))
1477 spin_unlock_irqrestore(&queue->tx_lock, flags);
1482 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1484 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1486 if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1487 xen_irq_lateeoi(irq, eoiflag);
1492 static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
1494 unsigned int work_queued;
1495 unsigned long flags;
1497 if (unlikely(queue->info->broken))
1500 spin_lock_irqsave(&queue->rx_cons_lock, flags);
1501 work_queued = XEN_RING_NR_UNCONSUMED_RESPONSES(&queue->rx);
1502 if (work_queued > queue->rx_rsp_unconsumed) {
1503 queue->rx_rsp_unconsumed = work_queued;
1505 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1506 const struct device *dev = &queue->info->netdev->dev;
1508 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1509 dev_alert(dev, "RX producer index going backwards\n");
1510 dev_alert(dev, "Disabled for further use\n");
1511 queue->info->broken = true;
1514 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1516 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
1517 napi_schedule(&queue->napi);
1522 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1524 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1526 if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1527 xen_irq_lateeoi(irq, eoiflag);
1532 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1534 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1536 if (xennet_handle_tx(dev_id, &eoiflag) &&
1537 xennet_handle_rx(dev_id, &eoiflag))
1538 xen_irq_lateeoi(irq, eoiflag);
1543 #ifdef CONFIG_NET_POLL_CONTROLLER
1544 static void xennet_poll_controller(struct net_device *dev)
1546 /* Poll each queue */
1547 struct netfront_info *info = netdev_priv(dev);
1548 unsigned int num_queues = dev->real_num_tx_queues;
1554 for (i = 0; i < num_queues; ++i)
1555 xennet_interrupt(0, &info->queues[i]);
1559 #define NETBACK_XDP_HEADROOM_DISABLE 0
1560 #define NETBACK_XDP_HEADROOM_ENABLE 1
1562 static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
1565 unsigned short headroom;
1567 headroom = xdp ? XDP_PACKET_HEADROOM : 0;
1568 err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
1569 "xdp-headroom", "%hu",
1572 pr_warn("Error writing xdp-headroom\n");
1577 static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1578 struct netlink_ext_ack *extack)
1580 unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
1581 struct netfront_info *np = netdev_priv(dev);
1582 struct bpf_prog *old_prog;
1583 unsigned int i, err;
1585 if (dev->mtu > max_mtu) {
1586 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
1590 if (!np->netback_has_xdp_headroom)
1593 xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
1595 err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
1596 NETBACK_XDP_HEADROOM_DISABLE);
1600 /* avoid the race with XDP headroom adjustment */
1601 wait_event(module_wq,
1602 xenbus_read_driver_state(np->xbdev->otherend) ==
1603 XenbusStateReconfigured);
1604 np->netfront_xdp_enabled = true;
1606 old_prog = rtnl_dereference(np->queues[0].xdp_prog);
1609 bpf_prog_add(prog, dev->real_num_tx_queues);
1611 for (i = 0; i < dev->real_num_tx_queues; ++i)
1612 rcu_assign_pointer(np->queues[i].xdp_prog, prog);
1615 for (i = 0; i < dev->real_num_tx_queues; ++i)
1616 bpf_prog_put(old_prog);
1618 xenbus_switch_state(np->xbdev, XenbusStateConnected);
1623 static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1625 struct netfront_info *np = netdev_priv(dev);
1630 switch (xdp->command) {
1631 case XDP_SETUP_PROG:
1632 return xennet_xdp_set(dev, xdp->prog, xdp->extack);
1638 static const struct net_device_ops xennet_netdev_ops = {
1639 .ndo_uninit = xennet_uninit,
1640 .ndo_open = xennet_open,
1641 .ndo_stop = xennet_close,
1642 .ndo_start_xmit = xennet_start_xmit,
1643 .ndo_change_mtu = xennet_change_mtu,
1644 .ndo_get_stats64 = xennet_get_stats64,
1645 .ndo_set_mac_address = eth_mac_addr,
1646 .ndo_validate_addr = eth_validate_addr,
1647 .ndo_fix_features = xennet_fix_features,
1648 .ndo_set_features = xennet_set_features,
1649 .ndo_select_queue = xennet_select_queue,
1650 .ndo_bpf = xennet_xdp,
1651 .ndo_xdp_xmit = xennet_xdp_xmit,
1652 #ifdef CONFIG_NET_POLL_CONTROLLER
1653 .ndo_poll_controller = xennet_poll_controller,
1657 static void xennet_free_netdev(struct net_device *netdev)
1659 struct netfront_info *np = netdev_priv(netdev);
1661 free_percpu(np->rx_stats);
1662 free_percpu(np->tx_stats);
1663 free_netdev(netdev);
1666 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1669 struct net_device *netdev;
1670 struct netfront_info *np;
1672 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1674 return ERR_PTR(-ENOMEM);
1676 np = netdev_priv(netdev);
1682 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1683 if (np->rx_stats == NULL)
1685 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1686 if (np->tx_stats == NULL)
1689 netdev->netdev_ops = &xennet_netdev_ops;
1691 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1693 netdev->hw_features = NETIF_F_SG |
1695 NETIF_F_TSO | NETIF_F_TSO6;
1698 * Assume that all hw features are available for now. This set
1699 * will be adjusted by the call to netdev_update_features() in
1700 * xennet_connect() which is the earliest point where we can
1701 * negotiate with the backend regarding supported features.
1703 netdev->features |= netdev->hw_features;
1705 netdev->ethtool_ops = &xennet_ethtool_ops;
1706 netdev->min_mtu = ETH_MIN_MTU;
1707 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
1708 SET_NETDEV_DEV(netdev, &dev->dev);
1710 np->netdev = netdev;
1711 np->netfront_xdp_enabled = false;
1713 netif_carrier_off(netdev);
1716 xenbus_switch_state(dev, XenbusStateInitialising);
1717 err = wait_event_timeout(module_wq,
1718 xenbus_read_driver_state(dev->otherend) !=
1719 XenbusStateClosed &&
1720 xenbus_read_driver_state(dev->otherend) !=
1721 XenbusStateUnknown, XENNET_TIMEOUT);
1727 xennet_free_netdev(netdev);
1728 return ERR_PTR(err);
1732 * Entry point to this code when a new device is created. Allocate the basic
1733 * structures and the ring buffers for communication with the backend, and
1734 * inform the backend of the appropriate details for those.
1736 static int netfront_probe(struct xenbus_device *dev,
1737 const struct xenbus_device_id *id)
1740 struct net_device *netdev;
1741 struct netfront_info *info;
1743 netdev = xennet_create_dev(dev);
1744 if (IS_ERR(netdev)) {
1745 err = PTR_ERR(netdev);
1746 xenbus_dev_fatal(dev, err, "creating netdev");
1750 info = netdev_priv(netdev);
1751 dev_set_drvdata(&dev->dev, info);
1753 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1759 static void xennet_end_access(int ref, void *page)
1761 /* This frees the page as a side-effect */
1762 if (ref != INVALID_GRANT_REF)
1763 gnttab_end_foreign_access(ref, (unsigned long)page);
1766 static void xennet_disconnect_backend(struct netfront_info *info)
1769 unsigned int num_queues = info->netdev->real_num_tx_queues;
1771 netif_carrier_off(info->netdev);
1773 for (i = 0; i < num_queues && info->queues; ++i) {
1774 struct netfront_queue *queue = &info->queues[i];
1776 del_timer_sync(&queue->rx_refill_timer);
1778 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1779 unbind_from_irqhandler(queue->tx_irq, queue);
1780 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1781 unbind_from_irqhandler(queue->tx_irq, queue);
1782 unbind_from_irqhandler(queue->rx_irq, queue);
1784 queue->tx_evtchn = queue->rx_evtchn = 0;
1785 queue->tx_irq = queue->rx_irq = 0;
1787 if (netif_running(info->netdev))
1788 napi_synchronize(&queue->napi);
1790 xennet_release_tx_bufs(queue);
1791 xennet_release_rx_bufs(queue);
1792 gnttab_free_grant_references(queue->gref_tx_head);
1793 gnttab_free_grant_references(queue->gref_rx_head);
1795 /* End access and free the pages */
1796 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1797 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1799 queue->tx_ring_ref = INVALID_GRANT_REF;
1800 queue->rx_ring_ref = INVALID_GRANT_REF;
1801 queue->tx.sring = NULL;
1802 queue->rx.sring = NULL;
1804 page_pool_destroy(queue->page_pool);
1809 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1810 * driver restart. We tear down our netif structure and recreate it, but
1811 * leave the device-layer structures intact so that this is transparent to the
1812 * rest of the kernel.
1814 static int netfront_resume(struct xenbus_device *dev)
1816 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1818 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1820 netif_tx_lock_bh(info->netdev);
1821 netif_device_detach(info->netdev);
1822 netif_tx_unlock_bh(info->netdev);
1824 xennet_disconnect_backend(info);
1828 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1830 char *s, *e, *macstr;
1833 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1835 return PTR_ERR(macstr);
1837 for (i = 0; i < ETH_ALEN; i++) {
1838 mac[i] = simple_strtoul(s, &e, 16);
1839 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1850 static int setup_netfront_single(struct netfront_queue *queue)
1854 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1858 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1859 xennet_interrupt, 0,
1860 queue->info->netdev->name,
1864 queue->rx_evtchn = queue->tx_evtchn;
1865 queue->rx_irq = queue->tx_irq = err;
1870 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1871 queue->tx_evtchn = 0;
1876 static int setup_netfront_split(struct netfront_queue *queue)
1880 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1883 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1885 goto alloc_rx_evtchn_fail;
1887 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1888 "%s-tx", queue->name);
1889 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1890 xennet_tx_interrupt, 0,
1891 queue->tx_irq_name, queue);
1894 queue->tx_irq = err;
1896 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1897 "%s-rx", queue->name);
1898 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1899 xennet_rx_interrupt, 0,
1900 queue->rx_irq_name, queue);
1903 queue->rx_irq = err;
1908 unbind_from_irqhandler(queue->tx_irq, queue);
1911 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1912 queue->rx_evtchn = 0;
1913 alloc_rx_evtchn_fail:
1914 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1915 queue->tx_evtchn = 0;
1920 static int setup_netfront(struct xenbus_device *dev,
1921 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1923 struct xen_netif_tx_sring *txs;
1924 struct xen_netif_rx_sring *rxs = NULL;
1928 queue->tx_ring_ref = INVALID_GRANT_REF;
1929 queue->rx_ring_ref = INVALID_GRANT_REF;
1930 queue->rx.sring = NULL;
1931 queue->tx.sring = NULL;
1933 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1936 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1939 SHARED_RING_INIT(txs);
1940 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1942 err = xenbus_grant_ring(dev, txs, 1, &gref);
1945 queue->tx_ring_ref = gref;
1947 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1950 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1953 SHARED_RING_INIT(rxs);
1954 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1956 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1959 queue->rx_ring_ref = gref;
1961 if (feature_split_evtchn)
1962 err = setup_netfront_split(queue);
1963 /* setup single event channel if
1964 * a) feature-split-event-channels == 0
1965 * b) feature-split-event-channels == 1 but failed to setup
1967 if (!feature_split_evtchn || err)
1968 err = setup_netfront_single(queue);
1975 /* If we fail to setup netfront, it is safe to just revoke access to
1976 * granted pages because backend is not accessing it at this point.
1979 if (queue->rx_ring_ref != INVALID_GRANT_REF) {
1980 gnttab_end_foreign_access(queue->rx_ring_ref,
1981 (unsigned long)rxs);
1982 queue->rx_ring_ref = INVALID_GRANT_REF;
1984 free_page((unsigned long)rxs);
1986 if (queue->tx_ring_ref != INVALID_GRANT_REF) {
1987 gnttab_end_foreign_access(queue->tx_ring_ref,
1988 (unsigned long)txs);
1989 queue->tx_ring_ref = INVALID_GRANT_REF;
1991 free_page((unsigned long)txs);
1996 /* Queue-specific initialisation
1997 * This used to be done in xennet_create_dev() but must now
2000 static int xennet_init_queue(struct netfront_queue *queue)
2006 spin_lock_init(&queue->tx_lock);
2007 spin_lock_init(&queue->rx_lock);
2008 spin_lock_init(&queue->rx_cons_lock);
2010 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
2012 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
2013 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
2016 /* Initialise tx_skb_freelist as a free chain containing every entry. */
2017 queue->tx_skb_freelist = 0;
2018 queue->tx_pend_queue = TX_LINK_NONE;
2019 for (i = 0; i < NET_TX_RING_SIZE; i++) {
2020 queue->tx_link[i] = i + 1;
2021 queue->grant_tx_ref[i] = INVALID_GRANT_REF;
2022 queue->grant_tx_page[i] = NULL;
2024 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
2026 /* Clear out rx_skbs */
2027 for (i = 0; i < NET_RX_RING_SIZE; i++) {
2028 queue->rx_skbs[i] = NULL;
2029 queue->grant_rx_ref[i] = INVALID_GRANT_REF;
2032 /* A grant for every tx ring slot */
2033 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
2034 &queue->gref_tx_head) < 0) {
2035 pr_alert("can't alloc tx grant refs\n");
2040 /* A grant for every rx ring slot */
2041 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
2042 &queue->gref_rx_head) < 0) {
2043 pr_alert("can't alloc rx grant refs\n");
2051 gnttab_free_grant_references(queue->gref_tx_head);
2056 static int write_queue_xenstore_keys(struct netfront_queue *queue,
2057 struct xenbus_transaction *xbt, int write_hierarchical)
2059 /* Write the queue-specific keys into XenStore in the traditional
2060 * way for a single queue, or in a queue subkeys for multiple
2063 struct xenbus_device *dev = queue->info->xbdev;
2065 const char *message;
2069 /* Choose the correct place to write the keys */
2070 if (write_hierarchical) {
2071 pathsize = strlen(dev->nodename) + 10;
2072 path = kzalloc(pathsize, GFP_KERNEL);
2075 message = "out of memory while writing ring references";
2078 snprintf(path, pathsize, "%s/queue-%u",
2079 dev->nodename, queue->id);
2081 path = (char *)dev->nodename;
2084 /* Write ring references */
2085 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
2086 queue->tx_ring_ref);
2088 message = "writing tx-ring-ref";
2092 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
2093 queue->rx_ring_ref);
2095 message = "writing rx-ring-ref";
2099 /* Write event channels; taking into account both shared
2100 * and split event channel scenarios.
2102 if (queue->tx_evtchn == queue->rx_evtchn) {
2103 /* Shared event channel */
2104 err = xenbus_printf(*xbt, path,
2105 "event-channel", "%u", queue->tx_evtchn);
2107 message = "writing event-channel";
2111 /* Split event channels */
2112 err = xenbus_printf(*xbt, path,
2113 "event-channel-tx", "%u", queue->tx_evtchn);
2115 message = "writing event-channel-tx";
2119 err = xenbus_printf(*xbt, path,
2120 "event-channel-rx", "%u", queue->rx_evtchn);
2122 message = "writing event-channel-rx";
2127 if (write_hierarchical)
2132 if (write_hierarchical)
2134 xenbus_dev_fatal(dev, err, "%s", message);
2140 static int xennet_create_page_pool(struct netfront_queue *queue)
2143 struct page_pool_params pp_params = {
2146 .pool_size = NET_RX_RING_SIZE,
2147 .nid = NUMA_NO_NODE,
2148 .dev = &queue->info->netdev->dev,
2149 .offset = XDP_PACKET_HEADROOM,
2150 .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
2153 queue->page_pool = page_pool_create(&pp_params);
2154 if (IS_ERR(queue->page_pool)) {
2155 err = PTR_ERR(queue->page_pool);
2156 queue->page_pool = NULL;
2160 err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
2163 netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
2167 err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
2168 MEM_TYPE_PAGE_POOL, queue->page_pool);
2170 netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
2171 goto err_unregister_rxq;
2176 xdp_rxq_info_unreg(&queue->xdp_rxq);
2178 page_pool_destroy(queue->page_pool);
2179 queue->page_pool = NULL;
2183 static int xennet_create_queues(struct netfront_info *info,
2184 unsigned int *num_queues)
2189 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
2194 for (i = 0; i < *num_queues; i++) {
2195 struct netfront_queue *queue = &info->queues[i];
2200 ret = xennet_init_queue(queue);
2202 dev_warn(&info->xbdev->dev,
2203 "only created %d queues\n", i);
2208 /* use page pool recycling instead of buddy allocator */
2209 ret = xennet_create_page_pool(queue);
2211 dev_err(&info->xbdev->dev, "can't allocate page pool\n");
2216 netif_napi_add(queue->info->netdev, &queue->napi,
2218 if (netif_running(info->netdev))
2219 napi_enable(&queue->napi);
2222 netif_set_real_num_tx_queues(info->netdev, *num_queues);
2224 if (*num_queues == 0) {
2225 dev_err(&info->xbdev->dev, "no queues\n");
2231 /* Common code used when first setting up, and when resuming. */
2232 static int talk_to_netback(struct xenbus_device *dev,
2233 struct netfront_info *info)
2235 const char *message;
2236 struct xenbus_transaction xbt;
2238 unsigned int feature_split_evtchn;
2240 unsigned int max_queues = 0;
2241 struct netfront_queue *queue = NULL;
2242 unsigned int num_queues = 1;
2245 info->netdev->irq = 0;
2247 /* Check if backend supports multiple queues */
2248 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2249 "multi-queue-max-queues", 1);
2250 num_queues = min(max_queues, xennet_max_queues);
2252 /* Check feature-split-event-channels */
2253 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2254 "feature-split-event-channels", 0);
2256 /* Read mac addr. */
2257 err = xen_net_read_mac(dev, addr);
2259 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
2262 eth_hw_addr_set(info->netdev, addr);
2264 info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
2265 "feature-xdp-headroom", 0);
2266 if (info->netback_has_xdp_headroom) {
2267 /* set the current xen-netfront xdp state */
2268 err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
2269 NETBACK_XDP_HEADROOM_ENABLE :
2270 NETBACK_XDP_HEADROOM_DISABLE);
2277 xennet_destroy_queues(info);
2279 /* For the case of a reconnect reset the "broken" indicator. */
2280 info->broken = false;
2282 err = xennet_create_queues(info, &num_queues);
2284 xenbus_dev_fatal(dev, err, "creating queues");
2285 kfree(info->queues);
2286 info->queues = NULL;
2291 /* Create shared ring, alloc event channel -- for each queue */
2292 for (i = 0; i < num_queues; ++i) {
2293 queue = &info->queues[i];
2294 err = setup_netfront(dev, queue, feature_split_evtchn);
2300 err = xenbus_transaction_start(&xbt);
2302 xenbus_dev_fatal(dev, err, "starting transaction");
2306 if (xenbus_exists(XBT_NIL,
2307 info->xbdev->otherend, "multi-queue-max-queues")) {
2308 /* Write the number of queues */
2309 err = xenbus_printf(xbt, dev->nodename,
2310 "multi-queue-num-queues", "%u", num_queues);
2312 message = "writing multi-queue-num-queues";
2313 goto abort_transaction_no_dev_fatal;
2317 if (num_queues == 1) {
2318 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2320 goto abort_transaction_no_dev_fatal;
2322 /* Write the keys for each queue */
2323 for (i = 0; i < num_queues; ++i) {
2324 queue = &info->queues[i];
2325 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2327 goto abort_transaction_no_dev_fatal;
2331 /* The remaining keys are not queue-specific */
2332 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2335 message = "writing request-rx-copy";
2336 goto abort_transaction;
2339 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2341 message = "writing feature-rx-notify";
2342 goto abort_transaction;
2345 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2347 message = "writing feature-sg";
2348 goto abort_transaction;
2351 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2353 message = "writing feature-gso-tcpv4";
2354 goto abort_transaction;
2357 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2359 message = "writing feature-gso-tcpv6";
2360 goto abort_transaction;
2363 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2366 message = "writing feature-ipv6-csum-offload";
2367 goto abort_transaction;
2370 err = xenbus_transaction_end(xbt, 0);
2374 xenbus_dev_fatal(dev, err, "completing transaction");
2381 xenbus_dev_fatal(dev, err, "%s", message);
2382 abort_transaction_no_dev_fatal:
2383 xenbus_transaction_end(xbt, 1);
2385 xennet_disconnect_backend(info);
2387 xennet_destroy_queues(info);
2391 device_unregister(&dev->dev);
2395 static int xennet_connect(struct net_device *dev)
2397 struct netfront_info *np = netdev_priv(dev);
2398 unsigned int num_queues = 0;
2401 struct netfront_queue *queue = NULL;
2403 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
2405 "backend does not support copying receive path\n");
2409 err = talk_to_netback(np->xbdev, np);
2412 if (np->netback_has_xdp_headroom)
2413 pr_info("backend supports XDP headroom\n");
2415 /* talk_to_netback() sets the correct number of queues */
2416 num_queues = dev->real_num_tx_queues;
2418 if (dev->reg_state == NETREG_UNINITIALIZED) {
2419 err = register_netdev(dev);
2421 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2422 device_unregister(&np->xbdev->dev);
2428 netdev_update_features(dev);
2432 * All public and private state should now be sane. Get
2433 * ready to start sending and receiving packets and give the driver
2434 * domain a kick because we've probably just requeued some
2437 netif_tx_lock_bh(np->netdev);
2438 netif_device_attach(np->netdev);
2439 netif_tx_unlock_bh(np->netdev);
2441 netif_carrier_on(np->netdev);
2442 for (j = 0; j < num_queues; ++j) {
2443 queue = &np->queues[j];
2445 notify_remote_via_irq(queue->tx_irq);
2446 if (queue->tx_irq != queue->rx_irq)
2447 notify_remote_via_irq(queue->rx_irq);
2449 spin_lock_irq(&queue->tx_lock);
2450 xennet_tx_buf_gc(queue);
2451 spin_unlock_irq(&queue->tx_lock);
2453 spin_lock_bh(&queue->rx_lock);
2454 xennet_alloc_rx_buffers(queue);
2455 spin_unlock_bh(&queue->rx_lock);
2462 * Callback received when the backend's state changes.
2464 static void netback_changed(struct xenbus_device *dev,
2465 enum xenbus_state backend_state)
2467 struct netfront_info *np = dev_get_drvdata(&dev->dev);
2468 struct net_device *netdev = np->netdev;
2470 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2472 wake_up_all(&module_wq);
2474 switch (backend_state) {
2475 case XenbusStateInitialising:
2476 case XenbusStateInitialised:
2477 case XenbusStateReconfiguring:
2478 case XenbusStateReconfigured:
2479 case XenbusStateUnknown:
2482 case XenbusStateInitWait:
2483 if (dev->state != XenbusStateInitialising)
2485 if (xennet_connect(netdev) != 0)
2487 xenbus_switch_state(dev, XenbusStateConnected);
2490 case XenbusStateConnected:
2491 netdev_notify_peers(netdev);
2494 case XenbusStateClosed:
2495 if (dev->state == XenbusStateClosed)
2497 fallthrough; /* Missed the backend's CLOSING state */
2498 case XenbusStateClosing:
2499 xenbus_frontend_closed(dev);
2504 static const struct xennet_stat {
2505 char name[ETH_GSTRING_LEN];
2507 } xennet_stats[] = {
2509 "rx_gso_checksum_fixup",
2510 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2514 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2516 switch (string_set) {
2518 return ARRAY_SIZE(xennet_stats);
2524 static void xennet_get_ethtool_stats(struct net_device *dev,
2525 struct ethtool_stats *stats, u64 * data)
2527 void *np = netdev_priv(dev);
2530 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2531 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2534 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2538 switch (stringset) {
2540 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2541 memcpy(data + i * ETH_GSTRING_LEN,
2542 xennet_stats[i].name, ETH_GSTRING_LEN);
2547 static const struct ethtool_ops xennet_ethtool_ops =
2549 .get_link = ethtool_op_get_link,
2551 .get_sset_count = xennet_get_sset_count,
2552 .get_ethtool_stats = xennet_get_ethtool_stats,
2553 .get_strings = xennet_get_strings,
2554 .get_ts_info = ethtool_op_get_ts_info,
2558 static ssize_t show_rxbuf(struct device *dev,
2559 struct device_attribute *attr, char *buf)
2561 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2564 static ssize_t store_rxbuf(struct device *dev,
2565 struct device_attribute *attr,
2566 const char *buf, size_t len)
2570 if (!capable(CAP_NET_ADMIN))
2573 simple_strtoul(buf, &endp, 0);
2577 /* rxbuf_min and rxbuf_max are no longer configurable. */
2582 static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2583 static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2584 static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
2586 static struct attribute *xennet_dev_attrs[] = {
2587 &dev_attr_rxbuf_min.attr,
2588 &dev_attr_rxbuf_max.attr,
2589 &dev_attr_rxbuf_cur.attr,
2593 static const struct attribute_group xennet_dev_group = {
2594 .attrs = xennet_dev_attrs
2596 #endif /* CONFIG_SYSFS */
2598 static void xennet_bus_close(struct xenbus_device *dev)
2602 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2605 xenbus_switch_state(dev, XenbusStateClosing);
2606 ret = wait_event_timeout(module_wq,
2607 xenbus_read_driver_state(dev->otherend) ==
2608 XenbusStateClosing ||
2609 xenbus_read_driver_state(dev->otherend) ==
2610 XenbusStateClosed ||
2611 xenbus_read_driver_state(dev->otherend) ==
2616 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2620 xenbus_switch_state(dev, XenbusStateClosed);
2621 ret = wait_event_timeout(module_wq,
2622 xenbus_read_driver_state(dev->otherend) ==
2623 XenbusStateClosed ||
2624 xenbus_read_driver_state(dev->otherend) ==
2630 static int xennet_remove(struct xenbus_device *dev)
2632 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2634 xennet_bus_close(dev);
2635 xennet_disconnect_backend(info);
2637 if (info->netdev->reg_state == NETREG_REGISTERED)
2638 unregister_netdev(info->netdev);
2642 xennet_destroy_queues(info);
2645 xennet_free_netdev(info->netdev);
2650 static const struct xenbus_device_id netfront_ids[] = {
2655 static struct xenbus_driver netfront_driver = {
2656 .ids = netfront_ids,
2657 .probe = netfront_probe,
2658 .remove = xennet_remove,
2659 .resume = netfront_resume,
2660 .otherend_changed = netback_changed,
2663 static int __init netif_init(void)
2668 if (!xen_has_pv_nic_devices())
2671 pr_info("Initialising Xen virtual ethernet driver\n");
2673 /* Allow as many queues as there are CPUs inut max. 8 if user has not
2674 * specified a value.
2676 if (xennet_max_queues == 0)
2677 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2680 return xenbus_register_frontend(&netfront_driver);
2682 module_init(netif_init);
2685 static void __exit netif_exit(void)
2687 xenbus_unregister_driver(&netfront_driver);
2689 module_exit(netif_exit);
2691 MODULE_DESCRIPTION("Xen virtual network device frontend");
2692 MODULE_LICENSE("GPL");
2693 MODULE_ALIAS("xen:vif");
2694 MODULE_ALIAS("xennet");