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 GRANT_INVALID_REF 0
83 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
84 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
86 /* Minimum number of Rx slots (includes slot for GSO metadata). */
87 #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
89 /* Queue name is interface name with "-qNNN" appended */
90 #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
92 /* IRQ name is queue name with "-tx" or "-rx" appended */
93 #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
95 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
97 struct netfront_stats {
100 struct u64_stats_sync syncp;
103 struct netfront_info;
105 struct netfront_queue {
106 unsigned int id; /* Queue ID, 0-based */
107 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
108 struct netfront_info *info;
110 struct bpf_prog __rcu *xdp_prog;
112 struct napi_struct napi;
114 /* Split event channels support, tx_* == rx_* when using
115 * single event channel.
117 unsigned int tx_evtchn, rx_evtchn;
118 unsigned int tx_irq, rx_irq;
119 /* Only used when split event channels support is enabled */
120 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
121 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
124 struct xen_netif_tx_front_ring tx;
128 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
129 * are linked from tx_skb_freelist through tx_link.
131 struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
132 unsigned short tx_link[NET_TX_RING_SIZE];
133 #define TX_LINK_NONE 0xffff
134 #define TX_PENDING 0xfffe
135 grant_ref_t gref_tx_head;
136 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
137 struct page *grant_tx_page[NET_TX_RING_SIZE];
138 unsigned tx_skb_freelist;
139 unsigned int tx_pend_queue;
141 spinlock_t rx_lock ____cacheline_aligned_in_smp;
142 struct xen_netif_rx_front_ring rx;
145 struct timer_list rx_refill_timer;
147 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
148 grant_ref_t gref_rx_head;
149 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
151 unsigned int rx_rsp_unconsumed;
152 spinlock_t rx_cons_lock;
154 struct page_pool *page_pool;
155 struct xdp_rxq_info xdp_rxq;
158 struct netfront_info {
159 struct list_head list;
160 struct net_device *netdev;
162 struct xenbus_device *xbdev;
164 /* Multi-queue support */
165 struct netfront_queue *queues;
168 struct netfront_stats __percpu *rx_stats;
169 struct netfront_stats __percpu *tx_stats;
172 bool netback_has_xdp_headroom;
173 bool netfront_xdp_enabled;
175 /* Is device behaving sane? */
178 atomic_t rx_gso_checksum_fixup;
181 struct netfront_rx_info {
182 struct xen_netif_rx_response rx;
183 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
187 * Access macros for acquiring freeing slots in tx_skbs[].
190 static void add_id_to_list(unsigned *head, unsigned short *list,
197 static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
199 unsigned int id = *head;
201 if (id != TX_LINK_NONE) {
203 list[id] = TX_LINK_NONE;
208 static int xennet_rxidx(RING_IDX idx)
210 return idx & (NET_RX_RING_SIZE - 1);
213 static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
216 int i = xennet_rxidx(ri);
217 struct sk_buff *skb = queue->rx_skbs[i];
218 queue->rx_skbs[i] = NULL;
222 static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
225 int i = xennet_rxidx(ri);
226 grant_ref_t ref = queue->grant_rx_ref[i];
227 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
232 static const struct attribute_group xennet_dev_group;
235 static bool xennet_can_sg(struct net_device *dev)
237 return dev->features & NETIF_F_SG;
241 static void rx_refill_timeout(struct timer_list *t)
243 struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
244 napi_schedule(&queue->napi);
247 static int netfront_tx_slot_available(struct netfront_queue *queue)
249 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
250 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
253 static void xennet_maybe_wake_tx(struct netfront_queue *queue)
255 struct net_device *dev = queue->info->netdev;
256 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
258 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
259 netfront_tx_slot_available(queue) &&
260 likely(netif_running(dev)))
261 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
265 static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
270 skb = __netdev_alloc_skb(queue->info->netdev,
271 RX_COPY_THRESHOLD + NET_IP_ALIGN,
272 GFP_ATOMIC | __GFP_NOWARN);
276 page = page_pool_dev_alloc_pages(queue->page_pool);
277 if (unlikely(!page)) {
281 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
283 /* Align ip header to a 16 bytes boundary */
284 skb_reserve(skb, NET_IP_ALIGN);
285 skb->dev = queue->info->netdev;
291 static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
293 RING_IDX req_prod = queue->rx.req_prod_pvt;
297 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
300 for (req_prod = queue->rx.req_prod_pvt;
301 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
307 struct xen_netif_rx_request *req;
309 skb = xennet_alloc_one_rx_buffer(queue);
315 id = xennet_rxidx(req_prod);
317 BUG_ON(queue->rx_skbs[id]);
318 queue->rx_skbs[id] = skb;
320 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
321 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
322 queue->grant_rx_ref[id] = ref;
324 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
326 req = RING_GET_REQUEST(&queue->rx, req_prod);
327 gnttab_page_grant_foreign_access_ref_one(ref,
328 queue->info->xbdev->otherend_id,
335 queue->rx.req_prod_pvt = req_prod;
337 /* Try again later if there are not enough requests or skb allocation
339 * Enough requests is quantified as the sum of newly created slots and
340 * the unconsumed slots at the backend.
342 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
344 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
348 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
350 notify_remote_via_irq(queue->rx_irq);
353 static int xennet_open(struct net_device *dev)
355 struct netfront_info *np = netdev_priv(dev);
356 unsigned int num_queues = dev->real_num_tx_queues;
358 struct netfront_queue *queue = NULL;
360 if (!np->queues || np->broken)
363 for (i = 0; i < num_queues; ++i) {
364 queue = &np->queues[i];
365 napi_enable(&queue->napi);
367 spin_lock_bh(&queue->rx_lock);
368 if (netif_carrier_ok(dev)) {
369 xennet_alloc_rx_buffers(queue);
370 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
371 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
372 napi_schedule(&queue->napi);
374 spin_unlock_bh(&queue->rx_lock);
377 netif_tx_start_all_queues(dev);
382 static bool xennet_tx_buf_gc(struct netfront_queue *queue)
388 bool work_done = false;
389 const struct device *dev = &queue->info->netdev->dev;
391 BUG_ON(!netif_carrier_ok(queue->info->netdev));
394 prod = queue->tx.sring->rsp_prod;
395 if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
396 dev_alert(dev, "Illegal number of responses %u\n",
397 prod - queue->tx.rsp_cons);
400 rmb(); /* Ensure we see responses up to 'rp'. */
402 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
403 struct xen_netif_tx_response txrsp;
407 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
408 if (txrsp.status == XEN_NETIF_RSP_NULL)
412 if (id >= RING_SIZE(&queue->tx)) {
414 "Response has incorrect id (%u)\n",
418 if (queue->tx_link[id] != TX_PENDING) {
420 "Response for inactive request\n");
424 queue->tx_link[id] = TX_LINK_NONE;
425 skb = queue->tx_skbs[id];
426 queue->tx_skbs[id] = NULL;
427 if (unlikely(!gnttab_end_foreign_access_ref(
428 queue->grant_tx_ref[id]))) {
430 "Grant still in use by backend domain\n");
433 gnttab_release_grant_reference(
434 &queue->gref_tx_head, queue->grant_tx_ref[id]);
435 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
436 queue->grant_tx_page[id] = NULL;
437 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
438 dev_kfree_skb_irq(skb);
441 queue->tx.rsp_cons = prod;
443 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
444 } while (more_to_do);
446 xennet_maybe_wake_tx(queue);
451 queue->info->broken = true;
452 dev_alert(dev, "Disabled for further use\n");
457 struct xennet_gnttab_make_txreq {
458 struct netfront_queue *queue;
461 struct xen_netif_tx_request *tx; /* Last request on ring page */
462 struct xen_netif_tx_request tx_local; /* Last request local copy*/
466 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
467 unsigned int len, void *data)
469 struct xennet_gnttab_make_txreq *info = data;
471 struct xen_netif_tx_request *tx;
473 /* convenient aliases */
474 struct page *page = info->page;
475 struct netfront_queue *queue = info->queue;
476 struct sk_buff *skb = info->skb;
478 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
479 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
480 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
481 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
483 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
484 gfn, GNTMAP_readonly);
486 queue->tx_skbs[id] = skb;
487 queue->grant_tx_page[id] = page;
488 queue->grant_tx_ref[id] = ref;
490 info->tx_local.id = id;
491 info->tx_local.gref = ref;
492 info->tx_local.offset = offset;
493 info->tx_local.size = len;
494 info->tx_local.flags = 0;
496 *tx = info->tx_local;
499 * Put the request in the pending queue, it will be set to be pending
500 * when the producer index is about to be raised.
502 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
505 info->size += info->tx_local.size;
508 static struct xen_netif_tx_request *xennet_make_first_txreq(
509 struct xennet_gnttab_make_txreq *info,
510 unsigned int offset, unsigned int len)
514 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
519 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
520 unsigned int len, void *data)
522 struct xennet_gnttab_make_txreq *info = data;
524 info->tx->flags |= XEN_NETTXF_more_data;
526 xennet_tx_setup_grant(gfn, offset, len, data);
529 static void xennet_make_txreqs(
530 struct xennet_gnttab_make_txreq *info,
532 unsigned int offset, unsigned int len)
534 /* Skip unused frames from start of page */
535 page += offset >> PAGE_SHIFT;
536 offset &= ~PAGE_MASK;
542 gnttab_foreach_grant_in_range(page, offset, len,
543 xennet_make_one_txreq,
553 * Count how many ring slots are required to send this skb. Each frag
554 * might be a compound page.
556 static int xennet_count_skb_slots(struct sk_buff *skb)
558 int i, frags = skb_shinfo(skb)->nr_frags;
561 slots = gnttab_count_grant(offset_in_page(skb->data),
564 for (i = 0; i < frags; i++) {
565 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
566 unsigned long size = skb_frag_size(frag);
567 unsigned long offset = skb_frag_off(frag);
569 /* Skip unused frames from start of page */
570 offset &= ~PAGE_MASK;
572 slots += gnttab_count_grant(offset, size);
578 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
579 struct net_device *sb_dev)
581 unsigned int num_queues = dev->real_num_tx_queues;
585 /* First, check if there is only one queue */
586 if (num_queues == 1) {
589 hash = skb_get_hash(skb);
590 queue_idx = hash % num_queues;
596 static void xennet_mark_tx_pending(struct netfront_queue *queue)
600 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
602 queue->tx_link[i] = TX_PENDING;
605 static int xennet_xdp_xmit_one(struct net_device *dev,
606 struct netfront_queue *queue,
607 struct xdp_frame *xdpf)
609 struct netfront_info *np = netdev_priv(dev);
610 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
611 struct xennet_gnttab_make_txreq info = {
614 .page = virt_to_page(xdpf->data),
618 xennet_make_first_txreq(&info,
619 offset_in_page(xdpf->data),
622 xennet_mark_tx_pending(queue);
624 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
626 notify_remote_via_irq(queue->tx_irq);
628 u64_stats_update_begin(&tx_stats->syncp);
629 tx_stats->bytes += xdpf->len;
631 u64_stats_update_end(&tx_stats->syncp);
633 xennet_tx_buf_gc(queue);
638 static int xennet_xdp_xmit(struct net_device *dev, int n,
639 struct xdp_frame **frames, u32 flags)
641 unsigned int num_queues = dev->real_num_tx_queues;
642 struct netfront_info *np = netdev_priv(dev);
643 struct netfront_queue *queue = NULL;
644 unsigned long irq_flags;
648 if (unlikely(np->broken))
650 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
653 queue = &np->queues[smp_processor_id() % num_queues];
655 spin_lock_irqsave(&queue->tx_lock, irq_flags);
656 for (i = 0; i < n; i++) {
657 struct xdp_frame *xdpf = frames[i];
661 if (xennet_xdp_xmit_one(dev, queue, xdpf))
665 spin_unlock_irqrestore(&queue->tx_lock, irq_flags);
671 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
673 static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
675 struct netfront_info *np = netdev_priv(dev);
676 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
677 struct xen_netif_tx_request *first_tx;
685 struct netfront_queue *queue = NULL;
686 struct xennet_gnttab_make_txreq info = { };
687 unsigned int num_queues = dev->real_num_tx_queues;
689 struct sk_buff *nskb;
691 /* Drop the packet if no queues are set up */
694 if (unlikely(np->broken))
696 /* Determine which queue to transmit this SKB on */
697 queue_index = skb_get_queue_mapping(skb);
698 queue = &np->queues[queue_index];
700 /* If skb->len is too big for wire format, drop skb and alert
701 * user about misconfiguration.
703 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
704 net_alert_ratelimited(
705 "xennet: skb->len = %u, too big for wire format\n",
710 slots = xennet_count_skb_slots(skb);
711 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
712 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
714 if (skb_linearize(skb))
718 page = virt_to_page(skb->data);
719 offset = offset_in_page(skb->data);
721 /* The first req should be at least ETH_HLEN size or the packet will be
722 * dropped by netback.
724 if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
725 nskb = skb_copy(skb, GFP_ATOMIC);
728 dev_consume_skb_any(skb);
730 page = virt_to_page(skb->data);
731 offset = offset_in_page(skb->data);
734 len = skb_headlen(skb);
736 spin_lock_irqsave(&queue->tx_lock, flags);
738 if (unlikely(!netif_carrier_ok(dev) ||
739 (slots > 1 && !xennet_can_sg(dev)) ||
740 netif_needs_gso(skb, netif_skb_features(skb)))) {
741 spin_unlock_irqrestore(&queue->tx_lock, flags);
745 /* First request for the linear area. */
749 first_tx = xennet_make_first_txreq(&info, offset, len);
750 offset += info.tx_local.size;
751 if (offset == PAGE_SIZE) {
755 len -= info.tx_local.size;
757 if (skb->ip_summed == CHECKSUM_PARTIAL)
759 first_tx->flags |= XEN_NETTXF_csum_blank |
760 XEN_NETTXF_data_validated;
761 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
762 /* remote but checksummed. */
763 first_tx->flags |= XEN_NETTXF_data_validated;
765 /* Optional extra info after the first request. */
766 if (skb_shinfo(skb)->gso_size) {
767 struct xen_netif_extra_info *gso;
769 gso = (struct xen_netif_extra_info *)
770 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
772 first_tx->flags |= XEN_NETTXF_extra_info;
774 gso->u.gso.size = skb_shinfo(skb)->gso_size;
775 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
776 XEN_NETIF_GSO_TYPE_TCPV6 :
777 XEN_NETIF_GSO_TYPE_TCPV4;
779 gso->u.gso.features = 0;
781 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
785 /* Requests for the rest of the linear area. */
786 xennet_make_txreqs(&info, page, offset, len);
788 /* Requests for all the frags. */
789 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
790 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
791 xennet_make_txreqs(&info, skb_frag_page(frag),
793 skb_frag_size(frag));
796 /* First request has the packet length. */
797 first_tx->size = skb->len;
799 /* timestamp packet in software */
800 skb_tx_timestamp(skb);
802 xennet_mark_tx_pending(queue);
804 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
806 notify_remote_via_irq(queue->tx_irq);
808 u64_stats_update_begin(&tx_stats->syncp);
809 tx_stats->bytes += skb->len;
811 u64_stats_update_end(&tx_stats->syncp);
813 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
814 xennet_tx_buf_gc(queue);
816 if (!netfront_tx_slot_available(queue))
817 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
819 spin_unlock_irqrestore(&queue->tx_lock, flags);
824 dev->stats.tx_dropped++;
825 dev_kfree_skb_any(skb);
829 static int xennet_close(struct net_device *dev)
831 struct netfront_info *np = netdev_priv(dev);
832 unsigned int num_queues = dev->real_num_tx_queues;
834 struct netfront_queue *queue;
835 netif_tx_stop_all_queues(np->netdev);
836 for (i = 0; i < num_queues; ++i) {
837 queue = &np->queues[i];
838 napi_disable(&queue->napi);
843 static void xennet_destroy_queues(struct netfront_info *info)
847 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
848 struct netfront_queue *queue = &info->queues[i];
850 if (netif_running(info->netdev))
851 napi_disable(&queue->napi);
852 netif_napi_del(&queue->napi);
859 static void xennet_uninit(struct net_device *dev)
861 struct netfront_info *np = netdev_priv(dev);
862 xennet_destroy_queues(np);
865 static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
869 spin_lock_irqsave(&queue->rx_cons_lock, flags);
870 queue->rx.rsp_cons = val;
871 queue->rx_rsp_unconsumed = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
872 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
875 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
878 int new = xennet_rxidx(queue->rx.req_prod_pvt);
880 BUG_ON(queue->rx_skbs[new]);
881 queue->rx_skbs[new] = skb;
882 queue->grant_rx_ref[new] = ref;
883 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
884 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
885 queue->rx.req_prod_pvt++;
888 static int xennet_get_extras(struct netfront_queue *queue,
889 struct xen_netif_extra_info *extras,
893 struct xen_netif_extra_info extra;
894 struct device *dev = &queue->info->netdev->dev;
895 RING_IDX cons = queue->rx.rsp_cons;
902 if (unlikely(cons + 1 == rp)) {
904 dev_warn(dev, "Missing extra info\n");
909 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
911 if (unlikely(!extra.type ||
912 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
914 dev_warn(dev, "Invalid extra type: %d\n",
918 extras[extra.type - 1] = extra;
921 skb = xennet_get_rx_skb(queue, cons);
922 ref = xennet_get_rx_ref(queue, cons);
923 xennet_move_rx_slot(queue, skb, ref);
924 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
926 xennet_set_rx_rsp_cons(queue, cons);
930 static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata,
931 struct xen_netif_rx_response *rx, struct bpf_prog *prog,
932 struct xdp_buff *xdp, bool *need_xdp_flush)
934 struct xdp_frame *xdpf;
935 u32 len = rx->status;
939 xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
941 xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM,
944 act = bpf_prog_run_xdp(prog, xdp);
948 xdpf = xdp_convert_buff_to_frame(xdp);
949 err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
951 xdp_return_frame_rx_napi(xdpf);
952 else if (unlikely(err < 0))
953 trace_xdp_exception(queue->info->netdev, prog, act);
957 err = xdp_do_redirect(queue->info->netdev, xdp, prog);
958 *need_xdp_flush = true;
960 trace_xdp_exception(queue->info->netdev, prog, act);
967 trace_xdp_exception(queue->info->netdev, prog, act);
971 bpf_warn_invalid_xdp_action(queue->info->netdev, prog, act);
977 static int xennet_get_responses(struct netfront_queue *queue,
978 struct netfront_rx_info *rinfo, RING_IDX rp,
979 struct sk_buff_head *list,
980 bool *need_xdp_flush)
982 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
983 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
984 RING_IDX cons = queue->rx.rsp_cons;
985 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
986 struct xen_netif_extra_info *extras = rinfo->extras;
987 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
988 struct device *dev = &queue->info->netdev->dev;
989 struct bpf_prog *xdp_prog;
995 if (rx->flags & XEN_NETRXF_extra_info) {
996 err = xennet_get_extras(queue, extras, rp);
998 if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) {
999 struct xen_netif_extra_info *xdp;
1001 xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1];
1002 rx->offset = xdp->u.xdp.headroom;
1005 cons = queue->rx.rsp_cons;
1009 if (unlikely(rx->status < 0 ||
1010 rx->offset + rx->status > XEN_PAGE_SIZE)) {
1011 if (net_ratelimit())
1012 dev_warn(dev, "rx->offset: %u, size: %d\n",
1013 rx->offset, rx->status);
1014 xennet_move_rx_slot(queue, skb, ref);
1020 * This definitely indicates a bug, either in this driver or in
1021 * the backend driver. In future this should flag the bad
1022 * situation to the system controller to reboot the backend.
1024 if (ref == GRANT_INVALID_REF) {
1025 if (net_ratelimit())
1026 dev_warn(dev, "Bad rx response id %d.\n",
1032 if (!gnttab_end_foreign_access_ref(ref)) {
1034 "Grant still in use by backend domain\n");
1035 queue->info->broken = true;
1036 dev_alert(dev, "Disabled for further use\n");
1040 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
1043 xdp_prog = rcu_dereference(queue->xdp_prog);
1045 if (!(rx->flags & XEN_NETRXF_more_data)) {
1046 /* currently only a single page contains data */
1047 verdict = xennet_run_xdp(queue,
1048 skb_frag_page(&skb_shinfo(skb)->frags[0]),
1049 rx, xdp_prog, &xdp, need_xdp_flush);
1050 if (verdict != XDP_PASS)
1053 /* drop the frame */
1059 __skb_queue_tail(list, skb);
1060 if (!(rx->flags & XEN_NETRXF_more_data))
1063 if (cons + slots == rp) {
1064 if (net_ratelimit())
1065 dev_warn(dev, "Need more slots\n");
1070 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
1072 skb = xennet_get_rx_skb(queue, cons + slots);
1073 ref = xennet_get_rx_ref(queue, cons + slots);
1077 if (unlikely(slots > max)) {
1078 if (net_ratelimit())
1079 dev_warn(dev, "Too many slots\n");
1084 xennet_set_rx_rsp_cons(queue, cons + slots);
1089 static int xennet_set_skb_gso(struct sk_buff *skb,
1090 struct xen_netif_extra_info *gso)
1092 if (!gso->u.gso.size) {
1093 if (net_ratelimit())
1094 pr_warn("GSO size must not be zero\n");
1098 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
1099 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
1100 if (net_ratelimit())
1101 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
1105 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1106 skb_shinfo(skb)->gso_type =
1107 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
1111 /* Header must be checked, and gso_segs computed. */
1112 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1113 skb_shinfo(skb)->gso_segs = 0;
1118 static int xennet_fill_frags(struct netfront_queue *queue,
1119 struct sk_buff *skb,
1120 struct sk_buff_head *list)
1122 RING_IDX cons = queue->rx.rsp_cons;
1123 struct sk_buff *nskb;
1125 while ((nskb = __skb_dequeue(list))) {
1126 struct xen_netif_rx_response rx;
1127 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
1129 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
1131 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
1132 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1134 BUG_ON(pull_to < skb_headlen(skb));
1135 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1137 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
1138 xennet_set_rx_rsp_cons(queue,
1139 ++cons + skb_queue_len(list));
1144 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1145 skb_frag_page(nfrag),
1146 rx.offset, rx.status, PAGE_SIZE);
1148 skb_shinfo(nskb)->nr_frags = 0;
1152 xennet_set_rx_rsp_cons(queue, cons);
1157 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
1159 bool recalculate_partial_csum = false;
1162 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1163 * peers can fail to set NETRXF_csum_blank when sending a GSO
1164 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1165 * recalculate the partial checksum.
1167 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1168 struct netfront_info *np = netdev_priv(dev);
1169 atomic_inc(&np->rx_gso_checksum_fixup);
1170 skb->ip_summed = CHECKSUM_PARTIAL;
1171 recalculate_partial_csum = true;
1174 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1175 if (skb->ip_summed != CHECKSUM_PARTIAL)
1178 return skb_checksum_setup(skb, recalculate_partial_csum);
1181 static int handle_incoming_queue(struct netfront_queue *queue,
1182 struct sk_buff_head *rxq)
1184 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
1185 int packets_dropped = 0;
1186 struct sk_buff *skb;
1188 while ((skb = __skb_dequeue(rxq)) != NULL) {
1189 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1191 if (pull_to > skb_headlen(skb))
1192 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1194 /* Ethernet work: Delayed to here as it peeks the header. */
1195 skb->protocol = eth_type_trans(skb, queue->info->netdev);
1196 skb_reset_network_header(skb);
1198 if (checksum_setup(queue->info->netdev, skb)) {
1201 queue->info->netdev->stats.rx_errors++;
1205 u64_stats_update_begin(&rx_stats->syncp);
1206 rx_stats->packets++;
1207 rx_stats->bytes += skb->len;
1208 u64_stats_update_end(&rx_stats->syncp);
1211 napi_gro_receive(&queue->napi, skb);
1214 return packets_dropped;
1217 static int xennet_poll(struct napi_struct *napi, int budget)
1219 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1220 struct net_device *dev = queue->info->netdev;
1221 struct sk_buff *skb;
1222 struct netfront_rx_info rinfo;
1223 struct xen_netif_rx_response *rx = &rinfo.rx;
1224 struct xen_netif_extra_info *extras = rinfo.extras;
1227 struct sk_buff_head rxq;
1228 struct sk_buff_head errq;
1229 struct sk_buff_head tmpq;
1231 bool need_xdp_flush = false;
1233 spin_lock(&queue->rx_lock);
1235 skb_queue_head_init(&rxq);
1236 skb_queue_head_init(&errq);
1237 skb_queue_head_init(&tmpq);
1239 rp = queue->rx.sring->rsp_prod;
1240 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1241 dev_alert(&dev->dev, "Illegal number of responses %u\n",
1242 rp - queue->rx.rsp_cons);
1243 queue->info->broken = true;
1244 spin_unlock(&queue->rx_lock);
1247 rmb(); /* Ensure we see queued responses up to 'rp'. */
1249 i = queue->rx.rsp_cons;
1251 while ((i != rp) && (work_done < budget)) {
1252 RING_COPY_RESPONSE(&queue->rx, i, rx);
1253 memset(extras, 0, sizeof(rinfo.extras));
1255 err = xennet_get_responses(queue, &rinfo, rp, &tmpq,
1258 if (unlikely(err)) {
1259 if (queue->info->broken) {
1260 spin_unlock(&queue->rx_lock);
1264 while ((skb = __skb_dequeue(&tmpq)))
1265 __skb_queue_tail(&errq, skb);
1266 dev->stats.rx_errors++;
1267 i = queue->rx.rsp_cons;
1271 skb = __skb_dequeue(&tmpq);
1273 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1274 struct xen_netif_extra_info *gso;
1275 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1277 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1278 __skb_queue_head(&tmpq, skb);
1279 xennet_set_rx_rsp_cons(queue,
1280 queue->rx.rsp_cons +
1281 skb_queue_len(&tmpq));
1286 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1287 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1288 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
1290 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
1291 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1292 skb->data_len = rx->status;
1293 skb->len += rx->status;
1295 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
1298 if (rx->flags & XEN_NETRXF_csum_blank)
1299 skb->ip_summed = CHECKSUM_PARTIAL;
1300 else if (rx->flags & XEN_NETRXF_data_validated)
1301 skb->ip_summed = CHECKSUM_UNNECESSARY;
1303 __skb_queue_tail(&rxq, skb);
1305 i = queue->rx.rsp_cons + 1;
1306 xennet_set_rx_rsp_cons(queue, i);
1312 __skb_queue_purge(&errq);
1314 work_done -= handle_incoming_queue(queue, &rxq);
1316 xennet_alloc_rx_buffers(queue);
1318 if (work_done < budget) {
1321 napi_complete_done(napi, work_done);
1323 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1325 napi_schedule(napi);
1328 spin_unlock(&queue->rx_lock);
1333 static int xennet_change_mtu(struct net_device *dev, int mtu)
1335 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
1343 static void xennet_get_stats64(struct net_device *dev,
1344 struct rtnl_link_stats64 *tot)
1346 struct netfront_info *np = netdev_priv(dev);
1349 for_each_possible_cpu(cpu) {
1350 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1351 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
1352 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1356 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1357 tx_packets = tx_stats->packets;
1358 tx_bytes = tx_stats->bytes;
1359 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
1362 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1363 rx_packets = rx_stats->packets;
1364 rx_bytes = rx_stats->bytes;
1365 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
1367 tot->rx_packets += rx_packets;
1368 tot->tx_packets += tx_packets;
1369 tot->rx_bytes += rx_bytes;
1370 tot->tx_bytes += tx_bytes;
1373 tot->rx_errors = dev->stats.rx_errors;
1374 tot->tx_dropped = dev->stats.tx_dropped;
1377 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1379 struct sk_buff *skb;
1382 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1383 /* Skip over entries which are actually freelist references */
1384 if (!queue->tx_skbs[i])
1387 skb = queue->tx_skbs[i];
1388 queue->tx_skbs[i] = NULL;
1389 get_page(queue->grant_tx_page[i]);
1390 gnttab_end_foreign_access(queue->grant_tx_ref[i],
1391 (unsigned long)page_address(queue->grant_tx_page[i]));
1392 queue->grant_tx_page[i] = NULL;
1393 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1394 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
1395 dev_kfree_skb_irq(skb);
1399 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1403 spin_lock_bh(&queue->rx_lock);
1405 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1406 struct sk_buff *skb;
1409 skb = queue->rx_skbs[id];
1413 ref = queue->grant_rx_ref[id];
1414 if (ref == GRANT_INVALID_REF)
1417 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1419 /* gnttab_end_foreign_access() needs a page ref until
1420 * foreign access is ended (which may be deferred).
1423 gnttab_end_foreign_access(ref,
1424 (unsigned long)page_address(page));
1425 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
1430 spin_unlock_bh(&queue->rx_lock);
1433 static netdev_features_t xennet_fix_features(struct net_device *dev,
1434 netdev_features_t features)
1436 struct netfront_info *np = netdev_priv(dev);
1438 if (features & NETIF_F_SG &&
1439 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1440 features &= ~NETIF_F_SG;
1442 if (features & NETIF_F_IPV6_CSUM &&
1443 !xenbus_read_unsigned(np->xbdev->otherend,
1444 "feature-ipv6-csum-offload", 0))
1445 features &= ~NETIF_F_IPV6_CSUM;
1447 if (features & NETIF_F_TSO &&
1448 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1449 features &= ~NETIF_F_TSO;
1451 if (features & NETIF_F_TSO6 &&
1452 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1453 features &= ~NETIF_F_TSO6;
1458 static int xennet_set_features(struct net_device *dev,
1459 netdev_features_t features)
1461 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1462 netdev_info(dev, "Reducing MTU because no SG offload");
1463 dev->mtu = ETH_DATA_LEN;
1469 static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
1471 unsigned long flags;
1473 if (unlikely(queue->info->broken))
1476 spin_lock_irqsave(&queue->tx_lock, flags);
1477 if (xennet_tx_buf_gc(queue))
1479 spin_unlock_irqrestore(&queue->tx_lock, flags);
1484 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1486 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1488 if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1489 xen_irq_lateeoi(irq, eoiflag);
1494 static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
1496 unsigned int work_queued;
1497 unsigned long flags;
1499 if (unlikely(queue->info->broken))
1502 spin_lock_irqsave(&queue->rx_cons_lock, flags);
1503 work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
1504 if (work_queued > queue->rx_rsp_unconsumed) {
1505 queue->rx_rsp_unconsumed = work_queued;
1507 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1508 const struct device *dev = &queue->info->netdev->dev;
1510 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1511 dev_alert(dev, "RX producer index going backwards\n");
1512 dev_alert(dev, "Disabled for further use\n");
1513 queue->info->broken = true;
1516 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1518 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
1519 napi_schedule(&queue->napi);
1524 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1526 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1528 if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1529 xen_irq_lateeoi(irq, eoiflag);
1534 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1536 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1538 if (xennet_handle_tx(dev_id, &eoiflag) &&
1539 xennet_handle_rx(dev_id, &eoiflag))
1540 xen_irq_lateeoi(irq, eoiflag);
1545 #ifdef CONFIG_NET_POLL_CONTROLLER
1546 static void xennet_poll_controller(struct net_device *dev)
1548 /* Poll each queue */
1549 struct netfront_info *info = netdev_priv(dev);
1550 unsigned int num_queues = dev->real_num_tx_queues;
1556 for (i = 0; i < num_queues; ++i)
1557 xennet_interrupt(0, &info->queues[i]);
1561 #define NETBACK_XDP_HEADROOM_DISABLE 0
1562 #define NETBACK_XDP_HEADROOM_ENABLE 1
1564 static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
1567 unsigned short headroom;
1569 headroom = xdp ? XDP_PACKET_HEADROOM : 0;
1570 err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
1571 "xdp-headroom", "%hu",
1574 pr_warn("Error writing xdp-headroom\n");
1579 static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1580 struct netlink_ext_ack *extack)
1582 unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
1583 struct netfront_info *np = netdev_priv(dev);
1584 struct bpf_prog *old_prog;
1585 unsigned int i, err;
1587 if (dev->mtu > max_mtu) {
1588 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
1592 if (!np->netback_has_xdp_headroom)
1595 xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
1597 err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
1598 NETBACK_XDP_HEADROOM_DISABLE);
1602 /* avoid the race with XDP headroom adjustment */
1603 wait_event(module_wq,
1604 xenbus_read_driver_state(np->xbdev->otherend) ==
1605 XenbusStateReconfigured);
1606 np->netfront_xdp_enabled = true;
1608 old_prog = rtnl_dereference(np->queues[0].xdp_prog);
1611 bpf_prog_add(prog, dev->real_num_tx_queues);
1613 for (i = 0; i < dev->real_num_tx_queues; ++i)
1614 rcu_assign_pointer(np->queues[i].xdp_prog, prog);
1617 for (i = 0; i < dev->real_num_tx_queues; ++i)
1618 bpf_prog_put(old_prog);
1620 xenbus_switch_state(np->xbdev, XenbusStateConnected);
1625 static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1627 struct netfront_info *np = netdev_priv(dev);
1632 switch (xdp->command) {
1633 case XDP_SETUP_PROG:
1634 return xennet_xdp_set(dev, xdp->prog, xdp->extack);
1640 static const struct net_device_ops xennet_netdev_ops = {
1641 .ndo_uninit = xennet_uninit,
1642 .ndo_open = xennet_open,
1643 .ndo_stop = xennet_close,
1644 .ndo_start_xmit = xennet_start_xmit,
1645 .ndo_change_mtu = xennet_change_mtu,
1646 .ndo_get_stats64 = xennet_get_stats64,
1647 .ndo_set_mac_address = eth_mac_addr,
1648 .ndo_validate_addr = eth_validate_addr,
1649 .ndo_fix_features = xennet_fix_features,
1650 .ndo_set_features = xennet_set_features,
1651 .ndo_select_queue = xennet_select_queue,
1652 .ndo_bpf = xennet_xdp,
1653 .ndo_xdp_xmit = xennet_xdp_xmit,
1654 #ifdef CONFIG_NET_POLL_CONTROLLER
1655 .ndo_poll_controller = xennet_poll_controller,
1659 static void xennet_free_netdev(struct net_device *netdev)
1661 struct netfront_info *np = netdev_priv(netdev);
1663 free_percpu(np->rx_stats);
1664 free_percpu(np->tx_stats);
1665 free_netdev(netdev);
1668 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1671 struct net_device *netdev;
1672 struct netfront_info *np;
1674 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1676 return ERR_PTR(-ENOMEM);
1678 np = netdev_priv(netdev);
1684 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1685 if (np->rx_stats == NULL)
1687 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1688 if (np->tx_stats == NULL)
1691 netdev->netdev_ops = &xennet_netdev_ops;
1693 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1695 netdev->hw_features = NETIF_F_SG |
1697 NETIF_F_TSO | NETIF_F_TSO6;
1700 * Assume that all hw features are available for now. This set
1701 * will be adjusted by the call to netdev_update_features() in
1702 * xennet_connect() which is the earliest point where we can
1703 * negotiate with the backend regarding supported features.
1705 netdev->features |= netdev->hw_features;
1707 netdev->ethtool_ops = &xennet_ethtool_ops;
1708 netdev->min_mtu = ETH_MIN_MTU;
1709 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
1710 SET_NETDEV_DEV(netdev, &dev->dev);
1712 np->netdev = netdev;
1713 np->netfront_xdp_enabled = false;
1715 netif_carrier_off(netdev);
1718 xenbus_switch_state(dev, XenbusStateInitialising);
1719 err = wait_event_timeout(module_wq,
1720 xenbus_read_driver_state(dev->otherend) !=
1721 XenbusStateClosed &&
1722 xenbus_read_driver_state(dev->otherend) !=
1723 XenbusStateUnknown, XENNET_TIMEOUT);
1729 xennet_free_netdev(netdev);
1730 return ERR_PTR(err);
1734 * Entry point to this code when a new device is created. Allocate the basic
1735 * structures and the ring buffers for communication with the backend, and
1736 * inform the backend of the appropriate details for those.
1738 static int netfront_probe(struct xenbus_device *dev,
1739 const struct xenbus_device_id *id)
1742 struct net_device *netdev;
1743 struct netfront_info *info;
1745 netdev = xennet_create_dev(dev);
1746 if (IS_ERR(netdev)) {
1747 err = PTR_ERR(netdev);
1748 xenbus_dev_fatal(dev, err, "creating netdev");
1752 info = netdev_priv(netdev);
1753 dev_set_drvdata(&dev->dev, info);
1755 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1761 static void xennet_end_access(int ref, void *page)
1763 /* This frees the page as a side-effect */
1764 if (ref != GRANT_INVALID_REF)
1765 gnttab_end_foreign_access(ref, (unsigned long)page);
1768 static void xennet_disconnect_backend(struct netfront_info *info)
1771 unsigned int num_queues = info->netdev->real_num_tx_queues;
1773 netif_carrier_off(info->netdev);
1775 for (i = 0; i < num_queues && info->queues; ++i) {
1776 struct netfront_queue *queue = &info->queues[i];
1778 del_timer_sync(&queue->rx_refill_timer);
1780 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1781 unbind_from_irqhandler(queue->tx_irq, queue);
1782 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1783 unbind_from_irqhandler(queue->tx_irq, queue);
1784 unbind_from_irqhandler(queue->rx_irq, queue);
1786 queue->tx_evtchn = queue->rx_evtchn = 0;
1787 queue->tx_irq = queue->rx_irq = 0;
1789 if (netif_running(info->netdev))
1790 napi_synchronize(&queue->napi);
1792 xennet_release_tx_bufs(queue);
1793 xennet_release_rx_bufs(queue);
1794 gnttab_free_grant_references(queue->gref_tx_head);
1795 gnttab_free_grant_references(queue->gref_rx_head);
1797 /* End access and free the pages */
1798 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1799 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1801 queue->tx_ring_ref = GRANT_INVALID_REF;
1802 queue->rx_ring_ref = GRANT_INVALID_REF;
1803 queue->tx.sring = NULL;
1804 queue->rx.sring = NULL;
1806 page_pool_destroy(queue->page_pool);
1811 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1812 * driver restart. We tear down our netif structure and recreate it, but
1813 * leave the device-layer structures intact so that this is transparent to the
1814 * rest of the kernel.
1816 static int netfront_resume(struct xenbus_device *dev)
1818 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1820 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1822 netif_tx_lock_bh(info->netdev);
1823 netif_device_detach(info->netdev);
1824 netif_tx_unlock_bh(info->netdev);
1826 xennet_disconnect_backend(info);
1830 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1832 char *s, *e, *macstr;
1835 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1837 return PTR_ERR(macstr);
1839 for (i = 0; i < ETH_ALEN; i++) {
1840 mac[i] = simple_strtoul(s, &e, 16);
1841 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1852 static int setup_netfront_single(struct netfront_queue *queue)
1856 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1860 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1861 xennet_interrupt, 0,
1862 queue->info->netdev->name,
1866 queue->rx_evtchn = queue->tx_evtchn;
1867 queue->rx_irq = queue->tx_irq = err;
1872 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1873 queue->tx_evtchn = 0;
1878 static int setup_netfront_split(struct netfront_queue *queue)
1882 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1885 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1887 goto alloc_rx_evtchn_fail;
1889 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1890 "%s-tx", queue->name);
1891 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1892 xennet_tx_interrupt, 0,
1893 queue->tx_irq_name, queue);
1896 queue->tx_irq = err;
1898 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1899 "%s-rx", queue->name);
1900 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1901 xennet_rx_interrupt, 0,
1902 queue->rx_irq_name, queue);
1905 queue->rx_irq = err;
1910 unbind_from_irqhandler(queue->tx_irq, queue);
1913 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1914 queue->rx_evtchn = 0;
1915 alloc_rx_evtchn_fail:
1916 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1917 queue->tx_evtchn = 0;
1922 static int setup_netfront(struct xenbus_device *dev,
1923 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1925 struct xen_netif_tx_sring *txs;
1926 struct xen_netif_rx_sring *rxs = NULL;
1930 queue->tx_ring_ref = GRANT_INVALID_REF;
1931 queue->rx_ring_ref = GRANT_INVALID_REF;
1932 queue->rx.sring = NULL;
1933 queue->tx.sring = NULL;
1935 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1938 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1941 SHARED_RING_INIT(txs);
1942 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1944 err = xenbus_grant_ring(dev, txs, 1, &gref);
1947 queue->tx_ring_ref = gref;
1949 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1952 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1955 SHARED_RING_INIT(rxs);
1956 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1958 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1961 queue->rx_ring_ref = gref;
1963 if (feature_split_evtchn)
1964 err = setup_netfront_split(queue);
1965 /* setup single event channel if
1966 * a) feature-split-event-channels == 0
1967 * b) feature-split-event-channels == 1 but failed to setup
1969 if (!feature_split_evtchn || err)
1970 err = setup_netfront_single(queue);
1977 /* If we fail to setup netfront, it is safe to just revoke access to
1978 * granted pages because backend is not accessing it at this point.
1981 if (queue->rx_ring_ref != GRANT_INVALID_REF) {
1982 gnttab_end_foreign_access(queue->rx_ring_ref,
1983 (unsigned long)rxs);
1984 queue->rx_ring_ref = GRANT_INVALID_REF;
1986 free_page((unsigned long)rxs);
1988 if (queue->tx_ring_ref != GRANT_INVALID_REF) {
1989 gnttab_end_foreign_access(queue->tx_ring_ref,
1990 (unsigned long)txs);
1991 queue->tx_ring_ref = GRANT_INVALID_REF;
1993 free_page((unsigned long)txs);
1998 /* Queue-specific initialisation
1999 * This used to be done in xennet_create_dev() but must now
2002 static int xennet_init_queue(struct netfront_queue *queue)
2008 spin_lock_init(&queue->tx_lock);
2009 spin_lock_init(&queue->rx_lock);
2010 spin_lock_init(&queue->rx_cons_lock);
2012 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
2014 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
2015 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
2018 /* Initialise tx_skb_freelist as a free chain containing every entry. */
2019 queue->tx_skb_freelist = 0;
2020 queue->tx_pend_queue = TX_LINK_NONE;
2021 for (i = 0; i < NET_TX_RING_SIZE; i++) {
2022 queue->tx_link[i] = i + 1;
2023 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
2024 queue->grant_tx_page[i] = NULL;
2026 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
2028 /* Clear out rx_skbs */
2029 for (i = 0; i < NET_RX_RING_SIZE; i++) {
2030 queue->rx_skbs[i] = NULL;
2031 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
2034 /* A grant for every tx ring slot */
2035 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
2036 &queue->gref_tx_head) < 0) {
2037 pr_alert("can't alloc tx grant refs\n");
2042 /* A grant for every rx ring slot */
2043 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
2044 &queue->gref_rx_head) < 0) {
2045 pr_alert("can't alloc rx grant refs\n");
2053 gnttab_free_grant_references(queue->gref_tx_head);
2058 static int write_queue_xenstore_keys(struct netfront_queue *queue,
2059 struct xenbus_transaction *xbt, int write_hierarchical)
2061 /* Write the queue-specific keys into XenStore in the traditional
2062 * way for a single queue, or in a queue subkeys for multiple
2065 struct xenbus_device *dev = queue->info->xbdev;
2067 const char *message;
2071 /* Choose the correct place to write the keys */
2072 if (write_hierarchical) {
2073 pathsize = strlen(dev->nodename) + 10;
2074 path = kzalloc(pathsize, GFP_KERNEL);
2077 message = "out of memory while writing ring references";
2080 snprintf(path, pathsize, "%s/queue-%u",
2081 dev->nodename, queue->id);
2083 path = (char *)dev->nodename;
2086 /* Write ring references */
2087 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
2088 queue->tx_ring_ref);
2090 message = "writing tx-ring-ref";
2094 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
2095 queue->rx_ring_ref);
2097 message = "writing rx-ring-ref";
2101 /* Write event channels; taking into account both shared
2102 * and split event channel scenarios.
2104 if (queue->tx_evtchn == queue->rx_evtchn) {
2105 /* Shared event channel */
2106 err = xenbus_printf(*xbt, path,
2107 "event-channel", "%u", queue->tx_evtchn);
2109 message = "writing event-channel";
2113 /* Split event channels */
2114 err = xenbus_printf(*xbt, path,
2115 "event-channel-tx", "%u", queue->tx_evtchn);
2117 message = "writing event-channel-tx";
2121 err = xenbus_printf(*xbt, path,
2122 "event-channel-rx", "%u", queue->rx_evtchn);
2124 message = "writing event-channel-rx";
2129 if (write_hierarchical)
2134 if (write_hierarchical)
2136 xenbus_dev_fatal(dev, err, "%s", message);
2142 static int xennet_create_page_pool(struct netfront_queue *queue)
2145 struct page_pool_params pp_params = {
2148 .pool_size = NET_RX_RING_SIZE,
2149 .nid = NUMA_NO_NODE,
2150 .dev = &queue->info->netdev->dev,
2151 .offset = XDP_PACKET_HEADROOM,
2152 .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
2155 queue->page_pool = page_pool_create(&pp_params);
2156 if (IS_ERR(queue->page_pool)) {
2157 err = PTR_ERR(queue->page_pool);
2158 queue->page_pool = NULL;
2162 err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
2165 netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
2169 err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
2170 MEM_TYPE_PAGE_POOL, queue->page_pool);
2172 netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
2173 goto err_unregister_rxq;
2178 xdp_rxq_info_unreg(&queue->xdp_rxq);
2180 page_pool_destroy(queue->page_pool);
2181 queue->page_pool = NULL;
2185 static int xennet_create_queues(struct netfront_info *info,
2186 unsigned int *num_queues)
2191 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
2196 for (i = 0; i < *num_queues; i++) {
2197 struct netfront_queue *queue = &info->queues[i];
2202 ret = xennet_init_queue(queue);
2204 dev_warn(&info->xbdev->dev,
2205 "only created %d queues\n", i);
2210 /* use page pool recycling instead of buddy allocator */
2211 ret = xennet_create_page_pool(queue);
2213 dev_err(&info->xbdev->dev, "can't allocate page pool\n");
2218 netif_napi_add(queue->info->netdev, &queue->napi,
2220 if (netif_running(info->netdev))
2221 napi_enable(&queue->napi);
2224 netif_set_real_num_tx_queues(info->netdev, *num_queues);
2226 if (*num_queues == 0) {
2227 dev_err(&info->xbdev->dev, "no queues\n");
2233 /* Common code used when first setting up, and when resuming. */
2234 static int talk_to_netback(struct xenbus_device *dev,
2235 struct netfront_info *info)
2237 const char *message;
2238 struct xenbus_transaction xbt;
2240 unsigned int feature_split_evtchn;
2242 unsigned int max_queues = 0;
2243 struct netfront_queue *queue = NULL;
2244 unsigned int num_queues = 1;
2247 info->netdev->irq = 0;
2249 /* Check if backend supports multiple queues */
2250 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2251 "multi-queue-max-queues", 1);
2252 num_queues = min(max_queues, xennet_max_queues);
2254 /* Check feature-split-event-channels */
2255 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2256 "feature-split-event-channels", 0);
2258 /* Read mac addr. */
2259 err = xen_net_read_mac(dev, addr);
2261 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
2264 eth_hw_addr_set(info->netdev, addr);
2266 info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
2267 "feature-xdp-headroom", 0);
2268 if (info->netback_has_xdp_headroom) {
2269 /* set the current xen-netfront xdp state */
2270 err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
2271 NETBACK_XDP_HEADROOM_ENABLE :
2272 NETBACK_XDP_HEADROOM_DISABLE);
2279 xennet_destroy_queues(info);
2281 /* For the case of a reconnect reset the "broken" indicator. */
2282 info->broken = false;
2284 err = xennet_create_queues(info, &num_queues);
2286 xenbus_dev_fatal(dev, err, "creating queues");
2287 kfree(info->queues);
2288 info->queues = NULL;
2293 /* Create shared ring, alloc event channel -- for each queue */
2294 for (i = 0; i < num_queues; ++i) {
2295 queue = &info->queues[i];
2296 err = setup_netfront(dev, queue, feature_split_evtchn);
2302 err = xenbus_transaction_start(&xbt);
2304 xenbus_dev_fatal(dev, err, "starting transaction");
2308 if (xenbus_exists(XBT_NIL,
2309 info->xbdev->otherend, "multi-queue-max-queues")) {
2310 /* Write the number of queues */
2311 err = xenbus_printf(xbt, dev->nodename,
2312 "multi-queue-num-queues", "%u", num_queues);
2314 message = "writing multi-queue-num-queues";
2315 goto abort_transaction_no_dev_fatal;
2319 if (num_queues == 1) {
2320 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2322 goto abort_transaction_no_dev_fatal;
2324 /* Write the keys for each queue */
2325 for (i = 0; i < num_queues; ++i) {
2326 queue = &info->queues[i];
2327 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2329 goto abort_transaction_no_dev_fatal;
2333 /* The remaining keys are not queue-specific */
2334 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2337 message = "writing request-rx-copy";
2338 goto abort_transaction;
2341 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2343 message = "writing feature-rx-notify";
2344 goto abort_transaction;
2347 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2349 message = "writing feature-sg";
2350 goto abort_transaction;
2353 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2355 message = "writing feature-gso-tcpv4";
2356 goto abort_transaction;
2359 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2361 message = "writing feature-gso-tcpv6";
2362 goto abort_transaction;
2365 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2368 message = "writing feature-ipv6-csum-offload";
2369 goto abort_transaction;
2372 err = xenbus_transaction_end(xbt, 0);
2376 xenbus_dev_fatal(dev, err, "completing transaction");
2383 xenbus_dev_fatal(dev, err, "%s", message);
2384 abort_transaction_no_dev_fatal:
2385 xenbus_transaction_end(xbt, 1);
2387 xennet_disconnect_backend(info);
2389 xennet_destroy_queues(info);
2393 device_unregister(&dev->dev);
2397 static int xennet_connect(struct net_device *dev)
2399 struct netfront_info *np = netdev_priv(dev);
2400 unsigned int num_queues = 0;
2403 struct netfront_queue *queue = NULL;
2405 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
2407 "backend does not support copying receive path\n");
2411 err = talk_to_netback(np->xbdev, np);
2414 if (np->netback_has_xdp_headroom)
2415 pr_info("backend supports XDP headroom\n");
2417 /* talk_to_netback() sets the correct number of queues */
2418 num_queues = dev->real_num_tx_queues;
2420 if (dev->reg_state == NETREG_UNINITIALIZED) {
2421 err = register_netdev(dev);
2423 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2424 device_unregister(&np->xbdev->dev);
2430 netdev_update_features(dev);
2434 * All public and private state should now be sane. Get
2435 * ready to start sending and receiving packets and give the driver
2436 * domain a kick because we've probably just requeued some
2439 netif_tx_lock_bh(np->netdev);
2440 netif_device_attach(np->netdev);
2441 netif_tx_unlock_bh(np->netdev);
2443 netif_carrier_on(np->netdev);
2444 for (j = 0; j < num_queues; ++j) {
2445 queue = &np->queues[j];
2447 notify_remote_via_irq(queue->tx_irq);
2448 if (queue->tx_irq != queue->rx_irq)
2449 notify_remote_via_irq(queue->rx_irq);
2451 spin_lock_irq(&queue->tx_lock);
2452 xennet_tx_buf_gc(queue);
2453 spin_unlock_irq(&queue->tx_lock);
2455 spin_lock_bh(&queue->rx_lock);
2456 xennet_alloc_rx_buffers(queue);
2457 spin_unlock_bh(&queue->rx_lock);
2464 * Callback received when the backend's state changes.
2466 static void netback_changed(struct xenbus_device *dev,
2467 enum xenbus_state backend_state)
2469 struct netfront_info *np = dev_get_drvdata(&dev->dev);
2470 struct net_device *netdev = np->netdev;
2472 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2474 wake_up_all(&module_wq);
2476 switch (backend_state) {
2477 case XenbusStateInitialising:
2478 case XenbusStateInitialised:
2479 case XenbusStateReconfiguring:
2480 case XenbusStateReconfigured:
2481 case XenbusStateUnknown:
2484 case XenbusStateInitWait:
2485 if (dev->state != XenbusStateInitialising)
2487 if (xennet_connect(netdev) != 0)
2489 xenbus_switch_state(dev, XenbusStateConnected);
2492 case XenbusStateConnected:
2493 netdev_notify_peers(netdev);
2496 case XenbusStateClosed:
2497 if (dev->state == XenbusStateClosed)
2499 fallthrough; /* Missed the backend's CLOSING state */
2500 case XenbusStateClosing:
2501 xenbus_frontend_closed(dev);
2506 static const struct xennet_stat {
2507 char name[ETH_GSTRING_LEN];
2509 } xennet_stats[] = {
2511 "rx_gso_checksum_fixup",
2512 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2516 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2518 switch (string_set) {
2520 return ARRAY_SIZE(xennet_stats);
2526 static void xennet_get_ethtool_stats(struct net_device *dev,
2527 struct ethtool_stats *stats, u64 * data)
2529 void *np = netdev_priv(dev);
2532 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2533 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2536 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2540 switch (stringset) {
2542 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2543 memcpy(data + i * ETH_GSTRING_LEN,
2544 xennet_stats[i].name, ETH_GSTRING_LEN);
2549 static const struct ethtool_ops xennet_ethtool_ops =
2551 .get_link = ethtool_op_get_link,
2553 .get_sset_count = xennet_get_sset_count,
2554 .get_ethtool_stats = xennet_get_ethtool_stats,
2555 .get_strings = xennet_get_strings,
2556 .get_ts_info = ethtool_op_get_ts_info,
2560 static ssize_t show_rxbuf(struct device *dev,
2561 struct device_attribute *attr, char *buf)
2563 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2566 static ssize_t store_rxbuf(struct device *dev,
2567 struct device_attribute *attr,
2568 const char *buf, size_t len)
2572 if (!capable(CAP_NET_ADMIN))
2575 simple_strtoul(buf, &endp, 0);
2579 /* rxbuf_min and rxbuf_max are no longer configurable. */
2584 static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2585 static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2586 static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
2588 static struct attribute *xennet_dev_attrs[] = {
2589 &dev_attr_rxbuf_min.attr,
2590 &dev_attr_rxbuf_max.attr,
2591 &dev_attr_rxbuf_cur.attr,
2595 static const struct attribute_group xennet_dev_group = {
2596 .attrs = xennet_dev_attrs
2598 #endif /* CONFIG_SYSFS */
2600 static void xennet_bus_close(struct xenbus_device *dev)
2604 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2607 xenbus_switch_state(dev, XenbusStateClosing);
2608 ret = wait_event_timeout(module_wq,
2609 xenbus_read_driver_state(dev->otherend) ==
2610 XenbusStateClosing ||
2611 xenbus_read_driver_state(dev->otherend) ==
2612 XenbusStateClosed ||
2613 xenbus_read_driver_state(dev->otherend) ==
2618 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2622 xenbus_switch_state(dev, XenbusStateClosed);
2623 ret = wait_event_timeout(module_wq,
2624 xenbus_read_driver_state(dev->otherend) ==
2625 XenbusStateClosed ||
2626 xenbus_read_driver_state(dev->otherend) ==
2632 static int xennet_remove(struct xenbus_device *dev)
2634 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2636 xennet_bus_close(dev);
2637 xennet_disconnect_backend(info);
2639 if (info->netdev->reg_state == NETREG_REGISTERED)
2640 unregister_netdev(info->netdev);
2644 xennet_destroy_queues(info);
2647 xennet_free_netdev(info->netdev);
2652 static const struct xenbus_device_id netfront_ids[] = {
2657 static struct xenbus_driver netfront_driver = {
2658 .ids = netfront_ids,
2659 .probe = netfront_probe,
2660 .remove = xennet_remove,
2661 .resume = netfront_resume,
2662 .otherend_changed = netback_changed,
2665 static int __init netif_init(void)
2670 if (!xen_has_pv_nic_devices())
2673 pr_info("Initialising Xen virtual ethernet driver\n");
2675 /* Allow as many queues as there are CPUs inut max. 8 if user has not
2676 * specified a value.
2678 if (xennet_max_queues == 0)
2679 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2682 return xenbus_register_frontend(&netfront_driver);
2684 module_init(netif_init);
2687 static void __exit netif_exit(void)
2689 xenbus_unregister_driver(&netfront_driver);
2691 module_exit(netif_exit);
2693 MODULE_DESCRIPTION("Xen virtual network device frontend");
2694 MODULE_LICENSE("GPL");
2695 MODULE_ALIAS("xen:vif");
2696 MODULE_ALIAS("xennet");