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], GNTMAP_readonly))) {
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, 0)) {
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],
1392 (unsigned long)page_address(queue->grant_tx_page[i]));
1393 queue->grant_tx_page[i] = NULL;
1394 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1395 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
1396 dev_kfree_skb_irq(skb);
1400 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1404 spin_lock_bh(&queue->rx_lock);
1406 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1407 struct sk_buff *skb;
1410 skb = queue->rx_skbs[id];
1414 ref = queue->grant_rx_ref[id];
1415 if (ref == GRANT_INVALID_REF)
1418 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1420 /* gnttab_end_foreign_access() needs a page ref until
1421 * foreign access is ended (which may be deferred).
1424 gnttab_end_foreign_access(ref, 0,
1425 (unsigned long)page_address(page));
1426 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
1431 spin_unlock_bh(&queue->rx_lock);
1434 static netdev_features_t xennet_fix_features(struct net_device *dev,
1435 netdev_features_t features)
1437 struct netfront_info *np = netdev_priv(dev);
1439 if (features & NETIF_F_SG &&
1440 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1441 features &= ~NETIF_F_SG;
1443 if (features & NETIF_F_IPV6_CSUM &&
1444 !xenbus_read_unsigned(np->xbdev->otherend,
1445 "feature-ipv6-csum-offload", 0))
1446 features &= ~NETIF_F_IPV6_CSUM;
1448 if (features & NETIF_F_TSO &&
1449 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1450 features &= ~NETIF_F_TSO;
1452 if (features & NETIF_F_TSO6 &&
1453 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1454 features &= ~NETIF_F_TSO6;
1459 static int xennet_set_features(struct net_device *dev,
1460 netdev_features_t features)
1462 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1463 netdev_info(dev, "Reducing MTU because no SG offload");
1464 dev->mtu = ETH_DATA_LEN;
1470 static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
1472 unsigned long flags;
1474 if (unlikely(queue->info->broken))
1477 spin_lock_irqsave(&queue->tx_lock, flags);
1478 if (xennet_tx_buf_gc(queue))
1480 spin_unlock_irqrestore(&queue->tx_lock, flags);
1485 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1487 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1489 if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1490 xen_irq_lateeoi(irq, eoiflag);
1495 static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
1497 unsigned int work_queued;
1498 unsigned long flags;
1500 if (unlikely(queue->info->broken))
1503 spin_lock_irqsave(&queue->rx_cons_lock, flags);
1504 work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
1505 if (work_queued > queue->rx_rsp_unconsumed) {
1506 queue->rx_rsp_unconsumed = work_queued;
1508 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1509 const struct device *dev = &queue->info->netdev->dev;
1511 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1512 dev_alert(dev, "RX producer index going backwards\n");
1513 dev_alert(dev, "Disabled for further use\n");
1514 queue->info->broken = true;
1517 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1519 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
1520 napi_schedule(&queue->napi);
1525 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1527 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1529 if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1530 xen_irq_lateeoi(irq, eoiflag);
1535 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1537 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1539 if (xennet_handle_tx(dev_id, &eoiflag) &&
1540 xennet_handle_rx(dev_id, &eoiflag))
1541 xen_irq_lateeoi(irq, eoiflag);
1546 #ifdef CONFIG_NET_POLL_CONTROLLER
1547 static void xennet_poll_controller(struct net_device *dev)
1549 /* Poll each queue */
1550 struct netfront_info *info = netdev_priv(dev);
1551 unsigned int num_queues = dev->real_num_tx_queues;
1557 for (i = 0; i < num_queues; ++i)
1558 xennet_interrupt(0, &info->queues[i]);
1562 #define NETBACK_XDP_HEADROOM_DISABLE 0
1563 #define NETBACK_XDP_HEADROOM_ENABLE 1
1565 static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
1568 unsigned short headroom;
1570 headroom = xdp ? XDP_PACKET_HEADROOM : 0;
1571 err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
1572 "xdp-headroom", "%hu",
1575 pr_warn("Error writing xdp-headroom\n");
1580 static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1581 struct netlink_ext_ack *extack)
1583 unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
1584 struct netfront_info *np = netdev_priv(dev);
1585 struct bpf_prog *old_prog;
1586 unsigned int i, err;
1588 if (dev->mtu > max_mtu) {
1589 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
1593 if (!np->netback_has_xdp_headroom)
1596 xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
1598 err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
1599 NETBACK_XDP_HEADROOM_DISABLE);
1603 /* avoid the race with XDP headroom adjustment */
1604 wait_event(module_wq,
1605 xenbus_read_driver_state(np->xbdev->otherend) ==
1606 XenbusStateReconfigured);
1607 np->netfront_xdp_enabled = true;
1609 old_prog = rtnl_dereference(np->queues[0].xdp_prog);
1612 bpf_prog_add(prog, dev->real_num_tx_queues);
1614 for (i = 0; i < dev->real_num_tx_queues; ++i)
1615 rcu_assign_pointer(np->queues[i].xdp_prog, prog);
1618 for (i = 0; i < dev->real_num_tx_queues; ++i)
1619 bpf_prog_put(old_prog);
1621 xenbus_switch_state(np->xbdev, XenbusStateConnected);
1626 static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1628 struct netfront_info *np = netdev_priv(dev);
1633 switch (xdp->command) {
1634 case XDP_SETUP_PROG:
1635 return xennet_xdp_set(dev, xdp->prog, xdp->extack);
1641 static const struct net_device_ops xennet_netdev_ops = {
1642 .ndo_uninit = xennet_uninit,
1643 .ndo_open = xennet_open,
1644 .ndo_stop = xennet_close,
1645 .ndo_start_xmit = xennet_start_xmit,
1646 .ndo_change_mtu = xennet_change_mtu,
1647 .ndo_get_stats64 = xennet_get_stats64,
1648 .ndo_set_mac_address = eth_mac_addr,
1649 .ndo_validate_addr = eth_validate_addr,
1650 .ndo_fix_features = xennet_fix_features,
1651 .ndo_set_features = xennet_set_features,
1652 .ndo_select_queue = xennet_select_queue,
1653 .ndo_bpf = xennet_xdp,
1654 .ndo_xdp_xmit = xennet_xdp_xmit,
1655 #ifdef CONFIG_NET_POLL_CONTROLLER
1656 .ndo_poll_controller = xennet_poll_controller,
1660 static void xennet_free_netdev(struct net_device *netdev)
1662 struct netfront_info *np = netdev_priv(netdev);
1664 free_percpu(np->rx_stats);
1665 free_percpu(np->tx_stats);
1666 free_netdev(netdev);
1669 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1672 struct net_device *netdev;
1673 struct netfront_info *np;
1675 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1677 return ERR_PTR(-ENOMEM);
1679 np = netdev_priv(netdev);
1685 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1686 if (np->rx_stats == NULL)
1688 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1689 if (np->tx_stats == NULL)
1692 netdev->netdev_ops = &xennet_netdev_ops;
1694 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1696 netdev->hw_features = NETIF_F_SG |
1698 NETIF_F_TSO | NETIF_F_TSO6;
1701 * Assume that all hw features are available for now. This set
1702 * will be adjusted by the call to netdev_update_features() in
1703 * xennet_connect() which is the earliest point where we can
1704 * negotiate with the backend regarding supported features.
1706 netdev->features |= netdev->hw_features;
1708 netdev->ethtool_ops = &xennet_ethtool_ops;
1709 netdev->min_mtu = ETH_MIN_MTU;
1710 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
1711 SET_NETDEV_DEV(netdev, &dev->dev);
1713 np->netdev = netdev;
1714 np->netfront_xdp_enabled = false;
1716 netif_carrier_off(netdev);
1719 xenbus_switch_state(dev, XenbusStateInitialising);
1720 err = wait_event_timeout(module_wq,
1721 xenbus_read_driver_state(dev->otherend) !=
1722 XenbusStateClosed &&
1723 xenbus_read_driver_state(dev->otherend) !=
1724 XenbusStateUnknown, XENNET_TIMEOUT);
1730 xennet_free_netdev(netdev);
1731 return ERR_PTR(err);
1735 * Entry point to this code when a new device is created. Allocate the basic
1736 * structures and the ring buffers for communication with the backend, and
1737 * inform the backend of the appropriate details for those.
1739 static int netfront_probe(struct xenbus_device *dev,
1740 const struct xenbus_device_id *id)
1743 struct net_device *netdev;
1744 struct netfront_info *info;
1746 netdev = xennet_create_dev(dev);
1747 if (IS_ERR(netdev)) {
1748 err = PTR_ERR(netdev);
1749 xenbus_dev_fatal(dev, err, "creating netdev");
1753 info = netdev_priv(netdev);
1754 dev_set_drvdata(&dev->dev, info);
1756 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1762 static void xennet_end_access(int ref, void *page)
1764 /* This frees the page as a side-effect */
1765 if (ref != GRANT_INVALID_REF)
1766 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1769 static void xennet_disconnect_backend(struct netfront_info *info)
1772 unsigned int num_queues = info->netdev->real_num_tx_queues;
1774 netif_carrier_off(info->netdev);
1776 for (i = 0; i < num_queues && info->queues; ++i) {
1777 struct netfront_queue *queue = &info->queues[i];
1779 del_timer_sync(&queue->rx_refill_timer);
1781 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1782 unbind_from_irqhandler(queue->tx_irq, queue);
1783 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1784 unbind_from_irqhandler(queue->tx_irq, queue);
1785 unbind_from_irqhandler(queue->rx_irq, queue);
1787 queue->tx_evtchn = queue->rx_evtchn = 0;
1788 queue->tx_irq = queue->rx_irq = 0;
1790 if (netif_running(info->netdev))
1791 napi_synchronize(&queue->napi);
1793 xennet_release_tx_bufs(queue);
1794 xennet_release_rx_bufs(queue);
1795 gnttab_free_grant_references(queue->gref_tx_head);
1796 gnttab_free_grant_references(queue->gref_rx_head);
1798 /* End access and free the pages */
1799 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1800 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1802 queue->tx_ring_ref = GRANT_INVALID_REF;
1803 queue->rx_ring_ref = GRANT_INVALID_REF;
1804 queue->tx.sring = NULL;
1805 queue->rx.sring = NULL;
1807 page_pool_destroy(queue->page_pool);
1812 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1813 * driver restart. We tear down our netif structure and recreate it, but
1814 * leave the device-layer structures intact so that this is transparent to the
1815 * rest of the kernel.
1817 static int netfront_resume(struct xenbus_device *dev)
1819 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1821 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1823 netif_tx_lock_bh(info->netdev);
1824 netif_device_detach(info->netdev);
1825 netif_tx_unlock_bh(info->netdev);
1827 xennet_disconnect_backend(info);
1831 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1833 char *s, *e, *macstr;
1836 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1838 return PTR_ERR(macstr);
1840 for (i = 0; i < ETH_ALEN; i++) {
1841 mac[i] = simple_strtoul(s, &e, 16);
1842 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1853 static int setup_netfront_single(struct netfront_queue *queue)
1857 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1861 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1862 xennet_interrupt, 0,
1863 queue->info->netdev->name,
1867 queue->rx_evtchn = queue->tx_evtchn;
1868 queue->rx_irq = queue->tx_irq = err;
1873 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1874 queue->tx_evtchn = 0;
1879 static int setup_netfront_split(struct netfront_queue *queue)
1883 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1886 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1888 goto alloc_rx_evtchn_fail;
1890 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1891 "%s-tx", queue->name);
1892 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1893 xennet_tx_interrupt, 0,
1894 queue->tx_irq_name, queue);
1897 queue->tx_irq = err;
1899 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1900 "%s-rx", queue->name);
1901 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1902 xennet_rx_interrupt, 0,
1903 queue->rx_irq_name, queue);
1906 queue->rx_irq = err;
1911 unbind_from_irqhandler(queue->tx_irq, queue);
1914 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1915 queue->rx_evtchn = 0;
1916 alloc_rx_evtchn_fail:
1917 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1918 queue->tx_evtchn = 0;
1923 static int setup_netfront(struct xenbus_device *dev,
1924 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1926 struct xen_netif_tx_sring *txs;
1927 struct xen_netif_rx_sring *rxs = NULL;
1931 queue->tx_ring_ref = GRANT_INVALID_REF;
1932 queue->rx_ring_ref = GRANT_INVALID_REF;
1933 queue->rx.sring = NULL;
1934 queue->tx.sring = NULL;
1936 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1939 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1942 SHARED_RING_INIT(txs);
1943 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1945 err = xenbus_grant_ring(dev, txs, 1, &gref);
1948 queue->tx_ring_ref = gref;
1950 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1953 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1956 SHARED_RING_INIT(rxs);
1957 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1959 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1962 queue->rx_ring_ref = gref;
1964 if (feature_split_evtchn)
1965 err = setup_netfront_split(queue);
1966 /* setup single event channel if
1967 * a) feature-split-event-channels == 0
1968 * b) feature-split-event-channels == 1 but failed to setup
1970 if (!feature_split_evtchn || err)
1971 err = setup_netfront_single(queue);
1978 /* If we fail to setup netfront, it is safe to just revoke access to
1979 * granted pages because backend is not accessing it at this point.
1982 if (queue->rx_ring_ref != GRANT_INVALID_REF) {
1983 gnttab_end_foreign_access(queue->rx_ring_ref, 0,
1984 (unsigned long)rxs);
1985 queue->rx_ring_ref = GRANT_INVALID_REF;
1987 free_page((unsigned long)rxs);
1989 if (queue->tx_ring_ref != GRANT_INVALID_REF) {
1990 gnttab_end_foreign_access(queue->tx_ring_ref, 0,
1991 (unsigned long)txs);
1992 queue->tx_ring_ref = GRANT_INVALID_REF;
1994 free_page((unsigned long)txs);
1999 /* Queue-specific initialisation
2000 * This used to be done in xennet_create_dev() but must now
2003 static int xennet_init_queue(struct netfront_queue *queue)
2009 spin_lock_init(&queue->tx_lock);
2010 spin_lock_init(&queue->rx_lock);
2011 spin_lock_init(&queue->rx_cons_lock);
2013 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
2015 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
2016 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
2019 /* Initialise tx_skb_freelist as a free chain containing every entry. */
2020 queue->tx_skb_freelist = 0;
2021 queue->tx_pend_queue = TX_LINK_NONE;
2022 for (i = 0; i < NET_TX_RING_SIZE; i++) {
2023 queue->tx_link[i] = i + 1;
2024 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
2025 queue->grant_tx_page[i] = NULL;
2027 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
2029 /* Clear out rx_skbs */
2030 for (i = 0; i < NET_RX_RING_SIZE; i++) {
2031 queue->rx_skbs[i] = NULL;
2032 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
2035 /* A grant for every tx ring slot */
2036 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
2037 &queue->gref_tx_head) < 0) {
2038 pr_alert("can't alloc tx grant refs\n");
2043 /* A grant for every rx ring slot */
2044 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
2045 &queue->gref_rx_head) < 0) {
2046 pr_alert("can't alloc rx grant refs\n");
2054 gnttab_free_grant_references(queue->gref_tx_head);
2059 static int write_queue_xenstore_keys(struct netfront_queue *queue,
2060 struct xenbus_transaction *xbt, int write_hierarchical)
2062 /* Write the queue-specific keys into XenStore in the traditional
2063 * way for a single queue, or in a queue subkeys for multiple
2066 struct xenbus_device *dev = queue->info->xbdev;
2068 const char *message;
2072 /* Choose the correct place to write the keys */
2073 if (write_hierarchical) {
2074 pathsize = strlen(dev->nodename) + 10;
2075 path = kzalloc(pathsize, GFP_KERNEL);
2078 message = "out of memory while writing ring references";
2081 snprintf(path, pathsize, "%s/queue-%u",
2082 dev->nodename, queue->id);
2084 path = (char *)dev->nodename;
2087 /* Write ring references */
2088 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
2089 queue->tx_ring_ref);
2091 message = "writing tx-ring-ref";
2095 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
2096 queue->rx_ring_ref);
2098 message = "writing rx-ring-ref";
2102 /* Write event channels; taking into account both shared
2103 * and split event channel scenarios.
2105 if (queue->tx_evtchn == queue->rx_evtchn) {
2106 /* Shared event channel */
2107 err = xenbus_printf(*xbt, path,
2108 "event-channel", "%u", queue->tx_evtchn);
2110 message = "writing event-channel";
2114 /* Split event channels */
2115 err = xenbus_printf(*xbt, path,
2116 "event-channel-tx", "%u", queue->tx_evtchn);
2118 message = "writing event-channel-tx";
2122 err = xenbus_printf(*xbt, path,
2123 "event-channel-rx", "%u", queue->rx_evtchn);
2125 message = "writing event-channel-rx";
2130 if (write_hierarchical)
2135 if (write_hierarchical)
2137 xenbus_dev_fatal(dev, err, "%s", message);
2143 static int xennet_create_page_pool(struct netfront_queue *queue)
2146 struct page_pool_params pp_params = {
2149 .pool_size = NET_RX_RING_SIZE,
2150 .nid = NUMA_NO_NODE,
2151 .dev = &queue->info->netdev->dev,
2152 .offset = XDP_PACKET_HEADROOM,
2153 .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
2156 queue->page_pool = page_pool_create(&pp_params);
2157 if (IS_ERR(queue->page_pool)) {
2158 err = PTR_ERR(queue->page_pool);
2159 queue->page_pool = NULL;
2163 err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
2166 netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
2170 err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
2171 MEM_TYPE_PAGE_POOL, queue->page_pool);
2173 netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
2174 goto err_unregister_rxq;
2179 xdp_rxq_info_unreg(&queue->xdp_rxq);
2181 page_pool_destroy(queue->page_pool);
2182 queue->page_pool = NULL;
2186 static int xennet_create_queues(struct netfront_info *info,
2187 unsigned int *num_queues)
2192 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
2197 for (i = 0; i < *num_queues; i++) {
2198 struct netfront_queue *queue = &info->queues[i];
2203 ret = xennet_init_queue(queue);
2205 dev_warn(&info->xbdev->dev,
2206 "only created %d queues\n", i);
2211 /* use page pool recycling instead of buddy allocator */
2212 ret = xennet_create_page_pool(queue);
2214 dev_err(&info->xbdev->dev, "can't allocate page pool\n");
2219 netif_napi_add(queue->info->netdev, &queue->napi,
2221 if (netif_running(info->netdev))
2222 napi_enable(&queue->napi);
2225 netif_set_real_num_tx_queues(info->netdev, *num_queues);
2227 if (*num_queues == 0) {
2228 dev_err(&info->xbdev->dev, "no queues\n");
2234 /* Common code used when first setting up, and when resuming. */
2235 static int talk_to_netback(struct xenbus_device *dev,
2236 struct netfront_info *info)
2238 const char *message;
2239 struct xenbus_transaction xbt;
2241 unsigned int feature_split_evtchn;
2243 unsigned int max_queues = 0;
2244 struct netfront_queue *queue = NULL;
2245 unsigned int num_queues = 1;
2248 info->netdev->irq = 0;
2250 /* Check if backend supports multiple queues */
2251 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2252 "multi-queue-max-queues", 1);
2253 num_queues = min(max_queues, xennet_max_queues);
2255 /* Check feature-split-event-channels */
2256 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2257 "feature-split-event-channels", 0);
2259 /* Read mac addr. */
2260 err = xen_net_read_mac(dev, addr);
2262 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
2265 eth_hw_addr_set(info->netdev, addr);
2267 info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
2268 "feature-xdp-headroom", 0);
2269 if (info->netback_has_xdp_headroom) {
2270 /* set the current xen-netfront xdp state */
2271 err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
2272 NETBACK_XDP_HEADROOM_ENABLE :
2273 NETBACK_XDP_HEADROOM_DISABLE);
2280 xennet_destroy_queues(info);
2282 /* For the case of a reconnect reset the "broken" indicator. */
2283 info->broken = false;
2285 err = xennet_create_queues(info, &num_queues);
2287 xenbus_dev_fatal(dev, err, "creating queues");
2288 kfree(info->queues);
2289 info->queues = NULL;
2294 /* Create shared ring, alloc event channel -- for each queue */
2295 for (i = 0; i < num_queues; ++i) {
2296 queue = &info->queues[i];
2297 err = setup_netfront(dev, queue, feature_split_evtchn);
2303 err = xenbus_transaction_start(&xbt);
2305 xenbus_dev_fatal(dev, err, "starting transaction");
2309 if (xenbus_exists(XBT_NIL,
2310 info->xbdev->otherend, "multi-queue-max-queues")) {
2311 /* Write the number of queues */
2312 err = xenbus_printf(xbt, dev->nodename,
2313 "multi-queue-num-queues", "%u", num_queues);
2315 message = "writing multi-queue-num-queues";
2316 goto abort_transaction_no_dev_fatal;
2320 if (num_queues == 1) {
2321 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2323 goto abort_transaction_no_dev_fatal;
2325 /* Write the keys for each queue */
2326 for (i = 0; i < num_queues; ++i) {
2327 queue = &info->queues[i];
2328 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2330 goto abort_transaction_no_dev_fatal;
2334 /* The remaining keys are not queue-specific */
2335 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2338 message = "writing request-rx-copy";
2339 goto abort_transaction;
2342 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2344 message = "writing feature-rx-notify";
2345 goto abort_transaction;
2348 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2350 message = "writing feature-sg";
2351 goto abort_transaction;
2354 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2356 message = "writing feature-gso-tcpv4";
2357 goto abort_transaction;
2360 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2362 message = "writing feature-gso-tcpv6";
2363 goto abort_transaction;
2366 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2369 message = "writing feature-ipv6-csum-offload";
2370 goto abort_transaction;
2373 err = xenbus_transaction_end(xbt, 0);
2377 xenbus_dev_fatal(dev, err, "completing transaction");
2384 xenbus_dev_fatal(dev, err, "%s", message);
2385 abort_transaction_no_dev_fatal:
2386 xenbus_transaction_end(xbt, 1);
2388 xennet_disconnect_backend(info);
2390 xennet_destroy_queues(info);
2394 device_unregister(&dev->dev);
2398 static int xennet_connect(struct net_device *dev)
2400 struct netfront_info *np = netdev_priv(dev);
2401 unsigned int num_queues = 0;
2404 struct netfront_queue *queue = NULL;
2406 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
2408 "backend does not support copying receive path\n");
2412 err = talk_to_netback(np->xbdev, np);
2415 if (np->netback_has_xdp_headroom)
2416 pr_info("backend supports XDP headroom\n");
2418 /* talk_to_netback() sets the correct number of queues */
2419 num_queues = dev->real_num_tx_queues;
2421 if (dev->reg_state == NETREG_UNINITIALIZED) {
2422 err = register_netdev(dev);
2424 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2425 device_unregister(&np->xbdev->dev);
2431 netdev_update_features(dev);
2435 * All public and private state should now be sane. Get
2436 * ready to start sending and receiving packets and give the driver
2437 * domain a kick because we've probably just requeued some
2440 netif_tx_lock_bh(np->netdev);
2441 netif_device_attach(np->netdev);
2442 netif_tx_unlock_bh(np->netdev);
2444 netif_carrier_on(np->netdev);
2445 for (j = 0; j < num_queues; ++j) {
2446 queue = &np->queues[j];
2448 notify_remote_via_irq(queue->tx_irq);
2449 if (queue->tx_irq != queue->rx_irq)
2450 notify_remote_via_irq(queue->rx_irq);
2452 spin_lock_irq(&queue->tx_lock);
2453 xennet_tx_buf_gc(queue);
2454 spin_unlock_irq(&queue->tx_lock);
2456 spin_lock_bh(&queue->rx_lock);
2457 xennet_alloc_rx_buffers(queue);
2458 spin_unlock_bh(&queue->rx_lock);
2465 * Callback received when the backend's state changes.
2467 static void netback_changed(struct xenbus_device *dev,
2468 enum xenbus_state backend_state)
2470 struct netfront_info *np = dev_get_drvdata(&dev->dev);
2471 struct net_device *netdev = np->netdev;
2473 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2475 wake_up_all(&module_wq);
2477 switch (backend_state) {
2478 case XenbusStateInitialising:
2479 case XenbusStateInitialised:
2480 case XenbusStateReconfiguring:
2481 case XenbusStateReconfigured:
2482 case XenbusStateUnknown:
2485 case XenbusStateInitWait:
2486 if (dev->state != XenbusStateInitialising)
2488 if (xennet_connect(netdev) != 0)
2490 xenbus_switch_state(dev, XenbusStateConnected);
2493 case XenbusStateConnected:
2494 netdev_notify_peers(netdev);
2497 case XenbusStateClosed:
2498 if (dev->state == XenbusStateClosed)
2500 fallthrough; /* Missed the backend's CLOSING state */
2501 case XenbusStateClosing:
2502 xenbus_frontend_closed(dev);
2507 static const struct xennet_stat {
2508 char name[ETH_GSTRING_LEN];
2510 } xennet_stats[] = {
2512 "rx_gso_checksum_fixup",
2513 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2517 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2519 switch (string_set) {
2521 return ARRAY_SIZE(xennet_stats);
2527 static void xennet_get_ethtool_stats(struct net_device *dev,
2528 struct ethtool_stats *stats, u64 * data)
2530 void *np = netdev_priv(dev);
2533 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2534 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2537 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2541 switch (stringset) {
2543 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2544 memcpy(data + i * ETH_GSTRING_LEN,
2545 xennet_stats[i].name, ETH_GSTRING_LEN);
2550 static const struct ethtool_ops xennet_ethtool_ops =
2552 .get_link = ethtool_op_get_link,
2554 .get_sset_count = xennet_get_sset_count,
2555 .get_ethtool_stats = xennet_get_ethtool_stats,
2556 .get_strings = xennet_get_strings,
2557 .get_ts_info = ethtool_op_get_ts_info,
2561 static ssize_t show_rxbuf(struct device *dev,
2562 struct device_attribute *attr, char *buf)
2564 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2567 static ssize_t store_rxbuf(struct device *dev,
2568 struct device_attribute *attr,
2569 const char *buf, size_t len)
2573 if (!capable(CAP_NET_ADMIN))
2576 simple_strtoul(buf, &endp, 0);
2580 /* rxbuf_min and rxbuf_max are no longer configurable. */
2585 static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2586 static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2587 static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
2589 static struct attribute *xennet_dev_attrs[] = {
2590 &dev_attr_rxbuf_min.attr,
2591 &dev_attr_rxbuf_max.attr,
2592 &dev_attr_rxbuf_cur.attr,
2596 static const struct attribute_group xennet_dev_group = {
2597 .attrs = xennet_dev_attrs
2599 #endif /* CONFIG_SYSFS */
2601 static void xennet_bus_close(struct xenbus_device *dev)
2605 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2608 xenbus_switch_state(dev, XenbusStateClosing);
2609 ret = wait_event_timeout(module_wq,
2610 xenbus_read_driver_state(dev->otherend) ==
2611 XenbusStateClosing ||
2612 xenbus_read_driver_state(dev->otherend) ==
2613 XenbusStateClosed ||
2614 xenbus_read_driver_state(dev->otherend) ==
2619 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2623 xenbus_switch_state(dev, XenbusStateClosed);
2624 ret = wait_event_timeout(module_wq,
2625 xenbus_read_driver_state(dev->otherend) ==
2626 XenbusStateClosed ||
2627 xenbus_read_driver_state(dev->otherend) ==
2633 static int xennet_remove(struct xenbus_device *dev)
2635 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2637 xennet_bus_close(dev);
2638 xennet_disconnect_backend(info);
2640 if (info->netdev->reg_state == NETREG_REGISTERED)
2641 unregister_netdev(info->netdev);
2645 xennet_destroy_queues(info);
2648 xennet_free_netdev(info->netdev);
2653 static const struct xenbus_device_id netfront_ids[] = {
2658 static struct xenbus_driver netfront_driver = {
2659 .ids = netfront_ids,
2660 .probe = netfront_probe,
2661 .remove = xennet_remove,
2662 .resume = netfront_resume,
2663 .otherend_changed = netback_changed,
2666 static int __init netif_init(void)
2671 if (!xen_has_pv_nic_devices())
2674 pr_info("Initialising Xen virtual ethernet driver\n");
2676 /* Allow as many queues as there are CPUs inut max. 8 if user has not
2677 * specified a value.
2679 if (xennet_max_queues == 0)
2680 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2683 return xenbus_register_frontend(&netfront_driver);
2685 module_init(netif_init);
2688 static void __exit netif_exit(void)
2690 xenbus_unregister_driver(&netfront_driver);
2692 module_exit(netif_exit);
2694 MODULE_DESCRIPTION("Xen virtual network device frontend");
2695 MODULE_LICENSE("GPL");
2696 MODULE_ALIAS("xen:vif");
2697 MODULE_ALIAS("xennet");