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/types.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 static bool __read_mostly xennet_trusted = true;
70 module_param_named(trusted, xennet_trusted, bool, 0644);
71 MODULE_PARM_DESC(trusted, "Is the backend trusted");
73 #define XENNET_TIMEOUT (5 * HZ)
75 static const struct ethtool_ops xennet_ethtool_ops;
81 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
83 #define RX_COPY_THRESHOLD 256
85 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
86 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
88 /* Minimum number of Rx slots (includes slot for GSO metadata). */
89 #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
91 /* Queue name is interface name with "-qNNN" appended */
92 #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
94 /* IRQ name is queue name with "-tx" or "-rx" appended */
95 #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
97 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
99 struct netfront_stats {
102 struct u64_stats_sync syncp;
105 struct netfront_info;
107 struct netfront_queue {
108 unsigned int id; /* Queue ID, 0-based */
109 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
110 struct netfront_info *info;
112 struct bpf_prog __rcu *xdp_prog;
114 struct napi_struct napi;
116 /* Split event channels support, tx_* == rx_* when using
117 * single event channel.
119 unsigned int tx_evtchn, rx_evtchn;
120 unsigned int tx_irq, rx_irq;
121 /* Only used when split event channels support is enabled */
122 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
123 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
126 struct xen_netif_tx_front_ring tx;
130 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
131 * are linked from tx_skb_freelist through tx_link.
133 struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
134 unsigned short tx_link[NET_TX_RING_SIZE];
135 #define TX_LINK_NONE 0xffff
136 #define TX_PENDING 0xfffe
137 grant_ref_t gref_tx_head;
138 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
139 struct page *grant_tx_page[NET_TX_RING_SIZE];
140 unsigned tx_skb_freelist;
141 unsigned int tx_pend_queue;
143 spinlock_t rx_lock ____cacheline_aligned_in_smp;
144 struct xen_netif_rx_front_ring rx;
147 struct timer_list rx_refill_timer;
149 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
150 grant_ref_t gref_rx_head;
151 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
153 unsigned int rx_rsp_unconsumed;
154 spinlock_t rx_cons_lock;
156 struct page_pool *page_pool;
157 struct xdp_rxq_info xdp_rxq;
160 struct netfront_info {
161 struct list_head list;
162 struct net_device *netdev;
164 struct xenbus_device *xbdev;
166 /* Multi-queue support */
167 struct netfront_queue *queues;
170 struct netfront_stats __percpu *rx_stats;
171 struct netfront_stats __percpu *tx_stats;
174 bool netback_has_xdp_headroom;
175 bool netfront_xdp_enabled;
177 /* Is device behaving sane? */
180 /* Should skbs be bounced into a zeroed buffer? */
183 atomic_t rx_gso_checksum_fixup;
186 struct netfront_rx_info {
187 struct xen_netif_rx_response rx;
188 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
192 * Access macros for acquiring freeing slots in tx_skbs[].
195 static void add_id_to_list(unsigned *head, unsigned short *list,
202 static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
204 unsigned int id = *head;
206 if (id != TX_LINK_NONE) {
208 list[id] = TX_LINK_NONE;
213 static int xennet_rxidx(RING_IDX idx)
215 return idx & (NET_RX_RING_SIZE - 1);
218 static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
221 int i = xennet_rxidx(ri);
222 struct sk_buff *skb = queue->rx_skbs[i];
223 queue->rx_skbs[i] = NULL;
227 static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
230 int i = xennet_rxidx(ri);
231 grant_ref_t ref = queue->grant_rx_ref[i];
232 queue->grant_rx_ref[i] = INVALID_GRANT_REF;
237 static const struct attribute_group xennet_dev_group;
240 static bool xennet_can_sg(struct net_device *dev)
242 return dev->features & NETIF_F_SG;
246 static void rx_refill_timeout(struct timer_list *t)
248 struct netfront_queue *queue = timer_container_of(queue, t,
250 napi_schedule(&queue->napi);
253 static int netfront_tx_slot_available(struct netfront_queue *queue)
255 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
256 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
259 static void xennet_maybe_wake_tx(struct netfront_queue *queue)
261 struct net_device *dev = queue->info->netdev;
262 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
264 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
265 netfront_tx_slot_available(queue) &&
266 likely(netif_running(dev)))
267 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
271 static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
276 skb = __netdev_alloc_skb(queue->info->netdev,
277 RX_COPY_THRESHOLD + NET_IP_ALIGN,
278 GFP_ATOMIC | __GFP_NOWARN);
282 page = page_pool_alloc_pages(queue->page_pool,
283 GFP_ATOMIC | __GFP_NOWARN | __GFP_ZERO);
284 if (unlikely(!page)) {
288 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
289 skb_mark_for_recycle(skb);
291 /* Align ip header to a 16 bytes boundary */
292 skb_reserve(skb, NET_IP_ALIGN);
293 skb->dev = queue->info->netdev;
299 static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
301 RING_IDX req_prod = queue->rx.req_prod_pvt;
305 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
308 for (req_prod = queue->rx.req_prod_pvt;
309 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
315 struct xen_netif_rx_request *req;
317 skb = xennet_alloc_one_rx_buffer(queue);
323 id = xennet_rxidx(req_prod);
325 BUG_ON(queue->rx_skbs[id]);
326 queue->rx_skbs[id] = skb;
328 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
329 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
330 queue->grant_rx_ref[id] = ref;
332 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
334 req = RING_GET_REQUEST(&queue->rx, req_prod);
335 gnttab_page_grant_foreign_access_ref_one(ref,
336 queue->info->xbdev->otherend_id,
343 queue->rx.req_prod_pvt = req_prod;
345 /* Try again later if there are not enough requests or skb allocation
347 * Enough requests is quantified as the sum of newly created slots and
348 * the unconsumed slots at the backend.
350 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
352 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
356 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
358 notify_remote_via_irq(queue->rx_irq);
361 static int xennet_open(struct net_device *dev)
363 struct netfront_info *np = netdev_priv(dev);
364 unsigned int num_queues = dev->real_num_tx_queues;
366 struct netfront_queue *queue = NULL;
368 if (!np->queues || np->broken)
371 for (i = 0; i < num_queues; ++i) {
372 queue = &np->queues[i];
373 napi_enable(&queue->napi);
375 spin_lock_bh(&queue->rx_lock);
376 if (netif_carrier_ok(dev)) {
377 xennet_alloc_rx_buffers(queue);
378 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
379 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
380 napi_schedule(&queue->napi);
382 spin_unlock_bh(&queue->rx_lock);
385 netif_tx_start_all_queues(dev);
390 static bool xennet_tx_buf_gc(struct netfront_queue *queue)
396 bool work_done = false;
397 const struct device *dev = &queue->info->netdev->dev;
399 BUG_ON(!netif_carrier_ok(queue->info->netdev));
402 prod = queue->tx.sring->rsp_prod;
403 if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
404 dev_alert(dev, "Illegal number of responses %u\n",
405 prod - queue->tx.rsp_cons);
408 rmb(); /* Ensure we see responses up to 'rp'. */
410 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
411 struct xen_netif_tx_response txrsp;
415 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
416 if (txrsp.status == XEN_NETIF_RSP_NULL)
420 if (id >= RING_SIZE(&queue->tx)) {
422 "Response has incorrect id (%u)\n",
426 if (queue->tx_link[id] != TX_PENDING) {
428 "Response for inactive request\n");
432 queue->tx_link[id] = TX_LINK_NONE;
433 skb = queue->tx_skbs[id];
434 queue->tx_skbs[id] = NULL;
435 if (unlikely(!gnttab_end_foreign_access_ref(
436 queue->grant_tx_ref[id]))) {
438 "Grant still in use by backend domain\n");
441 gnttab_release_grant_reference(
442 &queue->gref_tx_head, queue->grant_tx_ref[id]);
443 queue->grant_tx_ref[id] = INVALID_GRANT_REF;
444 queue->grant_tx_page[id] = NULL;
445 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
446 dev_kfree_skb_irq(skb);
449 queue->tx.rsp_cons = prod;
451 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
452 } while (more_to_do);
454 xennet_maybe_wake_tx(queue);
459 queue->info->broken = true;
460 dev_alert(dev, "Disabled for further use\n");
465 struct xennet_gnttab_make_txreq {
466 struct netfront_queue *queue;
469 struct xen_netif_tx_request *tx; /* Last request on ring page */
470 struct xen_netif_tx_request tx_local; /* Last request local copy*/
474 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
475 unsigned int len, void *data)
477 struct xennet_gnttab_make_txreq *info = data;
479 struct xen_netif_tx_request *tx;
481 /* convenient aliases */
482 struct page *page = info->page;
483 struct netfront_queue *queue = info->queue;
484 struct sk_buff *skb = info->skb;
486 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
487 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
488 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
489 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
491 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
492 gfn, GNTMAP_readonly);
494 queue->tx_skbs[id] = skb;
495 queue->grant_tx_page[id] = page;
496 queue->grant_tx_ref[id] = ref;
498 info->tx_local.id = id;
499 info->tx_local.gref = ref;
500 info->tx_local.offset = offset;
501 info->tx_local.size = len;
502 info->tx_local.flags = 0;
504 *tx = info->tx_local;
507 * Put the request in the pending queue, it will be set to be pending
508 * when the producer index is about to be raised.
510 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
513 info->size += info->tx_local.size;
516 static struct xen_netif_tx_request *xennet_make_first_txreq(
517 struct xennet_gnttab_make_txreq *info,
518 unsigned int offset, unsigned int len)
522 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
527 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
528 unsigned int len, void *data)
530 struct xennet_gnttab_make_txreq *info = data;
532 info->tx->flags |= XEN_NETTXF_more_data;
534 xennet_tx_setup_grant(gfn, offset, len, data);
537 static void xennet_make_txreqs(
538 struct xennet_gnttab_make_txreq *info,
540 unsigned int offset, unsigned int len)
542 /* Skip unused frames from start of page */
543 page += offset >> PAGE_SHIFT;
544 offset &= ~PAGE_MASK;
550 gnttab_foreach_grant_in_range(page, offset, len,
551 xennet_make_one_txreq,
561 * Count how many ring slots are required to send this skb. Each frag
562 * might be a compound page.
564 static int xennet_count_skb_slots(struct sk_buff *skb)
566 int i, frags = skb_shinfo(skb)->nr_frags;
569 slots = gnttab_count_grant(offset_in_page(skb->data),
572 for (i = 0; i < frags; i++) {
573 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
574 unsigned long size = skb_frag_size(frag);
575 unsigned long offset = skb_frag_off(frag);
577 /* Skip unused frames from start of page */
578 offset &= ~PAGE_MASK;
580 slots += gnttab_count_grant(offset, size);
586 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
587 struct net_device *sb_dev)
589 unsigned int num_queues = dev->real_num_tx_queues;
593 /* First, check if there is only one queue */
594 if (num_queues == 1) {
597 hash = skb_get_hash(skb);
598 queue_idx = hash % num_queues;
604 static void xennet_mark_tx_pending(struct netfront_queue *queue)
608 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
610 queue->tx_link[i] = TX_PENDING;
613 static int xennet_xdp_xmit_one(struct net_device *dev,
614 struct netfront_queue *queue,
615 struct xdp_frame *xdpf)
617 struct netfront_info *np = netdev_priv(dev);
618 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
619 struct xennet_gnttab_make_txreq info = {
622 .page = virt_to_page(xdpf->data),
626 xennet_make_first_txreq(&info,
627 offset_in_page(xdpf->data),
630 xennet_mark_tx_pending(queue);
632 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
634 notify_remote_via_irq(queue->tx_irq);
636 u64_stats_update_begin(&tx_stats->syncp);
637 tx_stats->bytes += xdpf->len;
639 u64_stats_update_end(&tx_stats->syncp);
641 xennet_tx_buf_gc(queue);
646 static int xennet_xdp_xmit(struct net_device *dev, int n,
647 struct xdp_frame **frames, u32 flags)
649 unsigned int num_queues = dev->real_num_tx_queues;
650 struct netfront_info *np = netdev_priv(dev);
651 struct netfront_queue *queue = NULL;
652 unsigned long irq_flags;
656 if (unlikely(np->broken))
658 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
661 queue = &np->queues[smp_processor_id() % num_queues];
663 spin_lock_irqsave(&queue->tx_lock, irq_flags);
664 for (i = 0; i < n; i++) {
665 struct xdp_frame *xdpf = frames[i];
669 if (xennet_xdp_xmit_one(dev, queue, xdpf))
673 spin_unlock_irqrestore(&queue->tx_lock, irq_flags);
678 static struct sk_buff *bounce_skb(const struct sk_buff *skb)
680 unsigned int headerlen = skb_headroom(skb);
681 /* Align size to allocate full pages and avoid contiguous data leaks */
682 unsigned int size = ALIGN(skb_end_offset(skb) + skb->data_len,
684 struct sk_buff *n = alloc_skb(size, GFP_ATOMIC | __GFP_ZERO);
689 if (!IS_ALIGNED((uintptr_t)n->head, XEN_PAGE_SIZE)) {
690 WARN_ONCE(1, "misaligned skb allocated\n");
695 /* Set the data pointer */
696 skb_reserve(n, headerlen);
697 /* Set the tail pointer and length */
698 skb_put(n, skb->len);
700 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
702 skb_copy_header(n, skb);
706 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
708 static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
710 struct netfront_info *np = netdev_priv(dev);
711 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
712 struct xen_netif_tx_request *first_tx;
720 struct netfront_queue *queue = NULL;
721 struct xennet_gnttab_make_txreq info = { };
722 unsigned int num_queues = dev->real_num_tx_queues;
724 struct sk_buff *nskb;
726 /* Drop the packet if no queues are set up */
729 if (unlikely(np->broken))
731 /* Determine which queue to transmit this SKB on */
732 queue_index = skb_get_queue_mapping(skb);
733 queue = &np->queues[queue_index];
735 /* If skb->len is too big for wire format, drop skb and alert
736 * user about misconfiguration.
738 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
739 net_alert_ratelimited(
740 "xennet: skb->len = %u, too big for wire format\n",
745 slots = xennet_count_skb_slots(skb);
746 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
747 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
749 if (skb_linearize(skb))
753 page = virt_to_page(skb->data);
754 offset = offset_in_page(skb->data);
756 /* The first req should be at least ETH_HLEN size or the packet will be
757 * dropped by netback.
759 * If the backend is not trusted bounce all data to zeroed pages to
760 * avoid exposing contiguous data on the granted page not belonging to
763 if (np->bounce || unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
764 nskb = bounce_skb(skb);
767 dev_consume_skb_any(skb);
769 page = virt_to_page(skb->data);
770 offset = offset_in_page(skb->data);
773 len = skb_headlen(skb);
775 spin_lock_irqsave(&queue->tx_lock, flags);
777 if (unlikely(!netif_carrier_ok(dev) ||
778 (slots > 1 && !xennet_can_sg(dev)) ||
779 netif_needs_gso(skb, netif_skb_features(skb)))) {
780 spin_unlock_irqrestore(&queue->tx_lock, flags);
784 /* First request for the linear area. */
788 first_tx = xennet_make_first_txreq(&info, offset, len);
789 offset += info.tx_local.size;
790 if (offset == PAGE_SIZE) {
794 len -= info.tx_local.size;
796 if (skb->ip_summed == CHECKSUM_PARTIAL)
798 first_tx->flags |= XEN_NETTXF_csum_blank |
799 XEN_NETTXF_data_validated;
800 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
801 /* remote but checksummed. */
802 first_tx->flags |= XEN_NETTXF_data_validated;
804 /* Optional extra info after the first request. */
805 if (skb_shinfo(skb)->gso_size) {
806 struct xen_netif_extra_info *gso;
808 gso = (struct xen_netif_extra_info *)
809 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
811 first_tx->flags |= XEN_NETTXF_extra_info;
813 gso->u.gso.size = skb_shinfo(skb)->gso_size;
814 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
815 XEN_NETIF_GSO_TYPE_TCPV6 :
816 XEN_NETIF_GSO_TYPE_TCPV4;
818 gso->u.gso.features = 0;
820 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
824 /* Requests for the rest of the linear area. */
825 xennet_make_txreqs(&info, page, offset, len);
827 /* Requests for all the frags. */
828 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
829 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
830 xennet_make_txreqs(&info, skb_frag_page(frag),
832 skb_frag_size(frag));
835 /* First request has the packet length. */
836 first_tx->size = skb->len;
838 /* timestamp packet in software */
839 skb_tx_timestamp(skb);
841 xennet_mark_tx_pending(queue);
843 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
845 notify_remote_via_irq(queue->tx_irq);
847 u64_stats_update_begin(&tx_stats->syncp);
848 tx_stats->bytes += skb->len;
850 u64_stats_update_end(&tx_stats->syncp);
852 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
853 xennet_tx_buf_gc(queue);
855 if (!netfront_tx_slot_available(queue))
856 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
858 spin_unlock_irqrestore(&queue->tx_lock, flags);
863 dev->stats.tx_dropped++;
864 dev_kfree_skb_any(skb);
868 static int xennet_close(struct net_device *dev)
870 struct netfront_info *np = netdev_priv(dev);
871 unsigned int num_queues = np->queues ? dev->real_num_tx_queues : 0;
873 struct netfront_queue *queue;
874 netif_tx_stop_all_queues(np->netdev);
875 for (i = 0; i < num_queues; ++i) {
876 queue = &np->queues[i];
877 napi_disable(&queue->napi);
882 static void xennet_destroy_queues(struct netfront_info *info)
889 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
890 struct netfront_queue *queue = &info->queues[i];
892 if (netif_running(info->netdev))
893 napi_disable(&queue->napi);
894 netif_napi_del(&queue->napi);
901 static void xennet_uninit(struct net_device *dev)
903 struct netfront_info *np = netdev_priv(dev);
904 xennet_destroy_queues(np);
907 static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
911 spin_lock_irqsave(&queue->rx_cons_lock, flags);
912 queue->rx.rsp_cons = val;
913 queue->rx_rsp_unconsumed = XEN_RING_NR_UNCONSUMED_RESPONSES(&queue->rx);
914 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
917 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
920 int new = xennet_rxidx(queue->rx.req_prod_pvt);
922 BUG_ON(queue->rx_skbs[new]);
923 queue->rx_skbs[new] = skb;
924 queue->grant_rx_ref[new] = ref;
925 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
926 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
927 queue->rx.req_prod_pvt++;
930 static int xennet_get_extras(struct netfront_queue *queue,
931 struct xen_netif_extra_info *extras,
935 struct xen_netif_extra_info extra;
936 struct device *dev = &queue->info->netdev->dev;
937 RING_IDX cons = queue->rx.rsp_cons;
944 if (unlikely(cons + 1 == rp)) {
946 dev_warn(dev, "Missing extra info\n");
951 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
953 if (unlikely(!extra.type ||
954 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
956 dev_warn(dev, "Invalid extra type: %d\n",
960 extras[extra.type - 1] = extra;
963 skb = xennet_get_rx_skb(queue, cons);
964 ref = xennet_get_rx_ref(queue, cons);
965 xennet_move_rx_slot(queue, skb, ref);
966 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
968 xennet_set_rx_rsp_cons(queue, cons);
972 static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata,
973 struct xen_netif_rx_response *rx, struct bpf_prog *prog,
974 struct xdp_buff *xdp, bool *need_xdp_flush)
976 struct xdp_frame *xdpf;
977 u32 len = rx->status;
981 xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
983 xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM,
986 act = bpf_prog_run_xdp(prog, xdp);
989 xdpf = xdp_convert_buff_to_frame(xdp);
990 if (unlikely(!xdpf)) {
991 trace_xdp_exception(queue->info->netdev, prog, act);
995 err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
996 if (unlikely(err <= 0)) {
998 trace_xdp_exception(queue->info->netdev, prog, act);
999 xdp_return_frame_rx_napi(xdpf);
1004 err = xdp_do_redirect(queue->info->netdev, xdp, prog);
1005 *need_xdp_flush = true;
1006 if (unlikely(err)) {
1007 trace_xdp_exception(queue->info->netdev, prog, act);
1008 xdp_return_buff(xdp);
1016 trace_xdp_exception(queue->info->netdev, prog, act);
1020 bpf_warn_invalid_xdp_action(queue->info->netdev, prog, act);
1026 static int xennet_get_responses(struct netfront_queue *queue,
1027 struct netfront_rx_info *rinfo, RING_IDX rp,
1028 struct sk_buff_head *list,
1029 bool *need_xdp_flush)
1031 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
1032 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
1033 RING_IDX cons = queue->rx.rsp_cons;
1034 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
1035 struct xen_netif_extra_info *extras = rinfo->extras;
1036 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
1037 struct device *dev = &queue->info->netdev->dev;
1038 struct bpf_prog *xdp_prog;
1039 struct xdp_buff xdp;
1044 if (rx->flags & XEN_NETRXF_extra_info) {
1045 err = xennet_get_extras(queue, extras, rp);
1047 if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) {
1048 struct xen_netif_extra_info *xdp;
1050 xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1];
1051 rx->offset = xdp->u.xdp.headroom;
1054 cons = queue->rx.rsp_cons;
1059 * This definitely indicates a bug, either in this driver or in
1060 * the backend driver. In future this should flag the bad
1061 * situation to the system controller to reboot the backend.
1063 if (ref == INVALID_GRANT_REF) {
1064 if (net_ratelimit())
1065 dev_warn(dev, "Bad rx response id %d.\n",
1071 if (unlikely(rx->status < 0 ||
1072 rx->offset + rx->status > XEN_PAGE_SIZE)) {
1073 if (net_ratelimit())
1074 dev_warn(dev, "rx->offset: %u, size: %d\n",
1075 rx->offset, rx->status);
1076 xennet_move_rx_slot(queue, skb, ref);
1081 if (!gnttab_end_foreign_access_ref(ref)) {
1083 "Grant still in use by backend domain\n");
1084 queue->info->broken = true;
1085 dev_alert(dev, "Disabled for further use\n");
1089 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
1092 xdp_prog = rcu_dereference(queue->xdp_prog);
1094 if (!(rx->flags & XEN_NETRXF_more_data)) {
1095 /* currently only a single page contains data */
1096 verdict = xennet_run_xdp(queue,
1097 skb_frag_page(&skb_shinfo(skb)->frags[0]),
1098 rx, xdp_prog, &xdp, need_xdp_flush);
1099 if (verdict != XDP_PASS)
1102 /* drop the frame */
1108 __skb_queue_tail(list, skb);
1111 if (!(rx->flags & XEN_NETRXF_more_data))
1114 if (cons + slots == rp) {
1115 if (net_ratelimit())
1116 dev_warn(dev, "Need more slots\n");
1121 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
1123 skb = xennet_get_rx_skb(queue, cons + slots);
1124 ref = xennet_get_rx_ref(queue, cons + slots);
1128 if (unlikely(slots > max)) {
1129 if (net_ratelimit())
1130 dev_warn(dev, "Too many slots\n");
1135 xennet_set_rx_rsp_cons(queue, cons + slots);
1140 static int xennet_set_skb_gso(struct sk_buff *skb,
1141 struct xen_netif_extra_info *gso)
1143 if (!gso->u.gso.size) {
1144 if (net_ratelimit())
1145 pr_warn("GSO size must not be zero\n");
1149 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
1150 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
1151 if (net_ratelimit())
1152 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
1156 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1157 skb_shinfo(skb)->gso_type =
1158 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
1162 /* Header must be checked, and gso_segs computed. */
1163 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1164 skb_shinfo(skb)->gso_segs = 0;
1169 static int xennet_fill_frags(struct netfront_queue *queue,
1170 struct sk_buff *skb,
1171 struct sk_buff_head *list)
1173 RING_IDX cons = queue->rx.rsp_cons;
1174 struct sk_buff *nskb;
1176 while ((nskb = __skb_dequeue(list))) {
1177 struct xen_netif_rx_response rx;
1178 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
1180 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
1182 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
1183 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1185 BUG_ON(pull_to < skb_headlen(skb));
1186 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1188 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
1189 xennet_set_rx_rsp_cons(queue,
1190 ++cons + skb_queue_len(list));
1195 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1196 skb_frag_page(nfrag),
1197 rx.offset, rx.status, PAGE_SIZE);
1199 skb_shinfo(nskb)->nr_frags = 0;
1203 xennet_set_rx_rsp_cons(queue, cons);
1208 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
1210 bool recalculate_partial_csum = false;
1213 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1214 * peers can fail to set NETRXF_csum_blank when sending a GSO
1215 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1216 * recalculate the partial checksum.
1218 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1219 struct netfront_info *np = netdev_priv(dev);
1220 atomic_inc(&np->rx_gso_checksum_fixup);
1221 skb->ip_summed = CHECKSUM_PARTIAL;
1222 recalculate_partial_csum = true;
1225 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1226 if (skb->ip_summed != CHECKSUM_PARTIAL)
1229 return skb_checksum_setup(skb, recalculate_partial_csum);
1232 static int handle_incoming_queue(struct netfront_queue *queue,
1233 struct sk_buff_head *rxq)
1235 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
1236 int packets_dropped = 0;
1237 struct sk_buff *skb;
1239 while ((skb = __skb_dequeue(rxq)) != NULL) {
1240 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1242 if (pull_to > skb_headlen(skb))
1243 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1245 /* Ethernet work: Delayed to here as it peeks the header. */
1246 skb->protocol = eth_type_trans(skb, queue->info->netdev);
1247 skb_reset_network_header(skb);
1249 if (checksum_setup(queue->info->netdev, skb)) {
1252 queue->info->netdev->stats.rx_errors++;
1256 u64_stats_update_begin(&rx_stats->syncp);
1257 rx_stats->packets++;
1258 rx_stats->bytes += skb->len;
1259 u64_stats_update_end(&rx_stats->syncp);
1262 napi_gro_receive(&queue->napi, skb);
1265 return packets_dropped;
1268 static int xennet_poll(struct napi_struct *napi, int budget)
1270 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1271 struct net_device *dev = queue->info->netdev;
1272 struct sk_buff *skb;
1273 struct netfront_rx_info rinfo;
1274 struct xen_netif_rx_response *rx = &rinfo.rx;
1275 struct xen_netif_extra_info *extras = rinfo.extras;
1278 struct sk_buff_head rxq;
1279 struct sk_buff_head errq;
1280 struct sk_buff_head tmpq;
1282 bool need_xdp_flush = false;
1284 spin_lock(&queue->rx_lock);
1286 skb_queue_head_init(&rxq);
1287 skb_queue_head_init(&errq);
1288 skb_queue_head_init(&tmpq);
1290 rp = queue->rx.sring->rsp_prod;
1291 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1292 dev_alert(&dev->dev, "Illegal number of responses %u\n",
1293 rp - queue->rx.rsp_cons);
1294 queue->info->broken = true;
1295 spin_unlock(&queue->rx_lock);
1298 rmb(); /* Ensure we see queued responses up to 'rp'. */
1300 i = queue->rx.rsp_cons;
1302 while ((i != rp) && (work_done < budget)) {
1303 RING_COPY_RESPONSE(&queue->rx, i, rx);
1304 memset(extras, 0, sizeof(rinfo.extras));
1306 err = xennet_get_responses(queue, &rinfo, rp, &tmpq,
1309 if (unlikely(err)) {
1310 if (queue->info->broken) {
1311 spin_unlock(&queue->rx_lock);
1315 while ((skb = __skb_dequeue(&tmpq)))
1316 __skb_queue_tail(&errq, skb);
1317 dev->stats.rx_errors++;
1318 i = queue->rx.rsp_cons;
1322 skb = __skb_dequeue(&tmpq);
1324 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1325 struct xen_netif_extra_info *gso;
1326 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1328 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1329 __skb_queue_head(&tmpq, skb);
1330 xennet_set_rx_rsp_cons(queue,
1331 queue->rx.rsp_cons +
1332 skb_queue_len(&tmpq));
1337 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1338 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1339 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
1341 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
1342 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1343 skb->data_len = rx->status;
1344 skb->len += rx->status;
1346 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
1349 if (rx->flags & XEN_NETRXF_csum_blank)
1350 skb->ip_summed = CHECKSUM_PARTIAL;
1351 else if (rx->flags & XEN_NETRXF_data_validated)
1352 skb->ip_summed = CHECKSUM_UNNECESSARY;
1354 __skb_queue_tail(&rxq, skb);
1356 i = queue->rx.rsp_cons + 1;
1357 xennet_set_rx_rsp_cons(queue, i);
1363 __skb_queue_purge(&errq);
1365 work_done -= handle_incoming_queue(queue, &rxq);
1367 xennet_alloc_rx_buffers(queue);
1369 if (work_done < budget) {
1372 napi_complete_done(napi, work_done);
1374 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1376 napi_schedule(napi);
1379 spin_unlock(&queue->rx_lock);
1384 static int xennet_change_mtu(struct net_device *dev, int mtu)
1386 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
1390 WRITE_ONCE(dev->mtu, mtu);
1394 static void xennet_get_stats64(struct net_device *dev,
1395 struct rtnl_link_stats64 *tot)
1397 struct netfront_info *np = netdev_priv(dev);
1400 for_each_possible_cpu(cpu) {
1401 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1402 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
1403 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1407 start = u64_stats_fetch_begin(&tx_stats->syncp);
1408 tx_packets = tx_stats->packets;
1409 tx_bytes = tx_stats->bytes;
1410 } while (u64_stats_fetch_retry(&tx_stats->syncp, start));
1413 start = u64_stats_fetch_begin(&rx_stats->syncp);
1414 rx_packets = rx_stats->packets;
1415 rx_bytes = rx_stats->bytes;
1416 } while (u64_stats_fetch_retry(&rx_stats->syncp, start));
1418 tot->rx_packets += rx_packets;
1419 tot->tx_packets += tx_packets;
1420 tot->rx_bytes += rx_bytes;
1421 tot->tx_bytes += tx_bytes;
1424 tot->rx_errors = dev->stats.rx_errors;
1425 tot->tx_dropped = dev->stats.tx_dropped;
1428 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1430 struct sk_buff *skb;
1433 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1434 /* Skip over entries which are actually freelist references */
1435 if (!queue->tx_skbs[i])
1438 skb = queue->tx_skbs[i];
1439 queue->tx_skbs[i] = NULL;
1440 get_page(queue->grant_tx_page[i]);
1441 gnttab_end_foreign_access(queue->grant_tx_ref[i],
1442 queue->grant_tx_page[i]);
1443 queue->grant_tx_page[i] = NULL;
1444 queue->grant_tx_ref[i] = INVALID_GRANT_REF;
1445 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
1446 dev_kfree_skb_irq(skb);
1450 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1454 spin_lock_bh(&queue->rx_lock);
1456 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1457 struct sk_buff *skb;
1460 skb = queue->rx_skbs[id];
1464 ref = queue->grant_rx_ref[id];
1465 if (ref == INVALID_GRANT_REF)
1468 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1470 /* gnttab_end_foreign_access() needs a page ref until
1471 * foreign access is ended (which may be deferred).
1474 gnttab_end_foreign_access(ref, page);
1475 queue->grant_rx_ref[id] = INVALID_GRANT_REF;
1480 spin_unlock_bh(&queue->rx_lock);
1483 static netdev_features_t xennet_fix_features(struct net_device *dev,
1484 netdev_features_t features)
1486 struct netfront_info *np = netdev_priv(dev);
1488 if (features & NETIF_F_SG &&
1489 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1490 features &= ~NETIF_F_SG;
1492 if (features & NETIF_F_IPV6_CSUM &&
1493 !xenbus_read_unsigned(np->xbdev->otherend,
1494 "feature-ipv6-csum-offload", 0))
1495 features &= ~NETIF_F_IPV6_CSUM;
1497 if (features & NETIF_F_TSO &&
1498 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1499 features &= ~NETIF_F_TSO;
1501 if (features & NETIF_F_TSO6 &&
1502 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1503 features &= ~NETIF_F_TSO6;
1508 static int xennet_set_features(struct net_device *dev,
1509 netdev_features_t features)
1511 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1512 netdev_info(dev, "Reducing MTU because no SG offload");
1513 dev->mtu = ETH_DATA_LEN;
1519 static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
1521 unsigned long flags;
1523 if (unlikely(queue->info->broken))
1526 spin_lock_irqsave(&queue->tx_lock, flags);
1527 if (xennet_tx_buf_gc(queue))
1529 spin_unlock_irqrestore(&queue->tx_lock, flags);
1534 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1536 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1538 if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1539 xen_irq_lateeoi(irq, eoiflag);
1544 static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
1546 unsigned int work_queued;
1547 unsigned long flags;
1549 if (unlikely(queue->info->broken))
1552 spin_lock_irqsave(&queue->rx_cons_lock, flags);
1553 work_queued = XEN_RING_NR_UNCONSUMED_RESPONSES(&queue->rx);
1554 if (work_queued > queue->rx_rsp_unconsumed) {
1555 queue->rx_rsp_unconsumed = work_queued;
1557 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1558 const struct device *dev = &queue->info->netdev->dev;
1560 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1561 dev_alert(dev, "RX producer index going backwards\n");
1562 dev_alert(dev, "Disabled for further use\n");
1563 queue->info->broken = true;
1566 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1568 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
1569 napi_schedule(&queue->napi);
1574 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1576 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1578 if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1579 xen_irq_lateeoi(irq, eoiflag);
1584 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1586 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1588 if (xennet_handle_tx(dev_id, &eoiflag) &&
1589 xennet_handle_rx(dev_id, &eoiflag))
1590 xen_irq_lateeoi(irq, eoiflag);
1595 #ifdef CONFIG_NET_POLL_CONTROLLER
1596 static void xennet_poll_controller(struct net_device *dev)
1598 /* Poll each queue */
1599 struct netfront_info *info = netdev_priv(dev);
1600 unsigned int num_queues = dev->real_num_tx_queues;
1606 for (i = 0; i < num_queues; ++i)
1607 xennet_interrupt(0, &info->queues[i]);
1611 #define NETBACK_XDP_HEADROOM_DISABLE 0
1612 #define NETBACK_XDP_HEADROOM_ENABLE 1
1614 static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
1617 unsigned short headroom;
1619 headroom = xdp ? XDP_PACKET_HEADROOM : 0;
1620 err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
1621 "xdp-headroom", "%hu",
1624 pr_warn("Error writing xdp-headroom\n");
1629 static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1630 struct netlink_ext_ack *extack)
1632 unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
1633 struct netfront_info *np = netdev_priv(dev);
1634 struct bpf_prog *old_prog;
1635 unsigned int i, err;
1637 if (dev->mtu > max_mtu) {
1638 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
1642 if (!np->netback_has_xdp_headroom)
1645 xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
1647 err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
1648 NETBACK_XDP_HEADROOM_DISABLE);
1652 /* avoid the race with XDP headroom adjustment */
1653 wait_event(module_wq,
1654 xenbus_read_driver_state(np->xbdev->otherend) ==
1655 XenbusStateReconfigured);
1656 np->netfront_xdp_enabled = true;
1658 old_prog = rtnl_dereference(np->queues[0].xdp_prog);
1661 bpf_prog_add(prog, dev->real_num_tx_queues);
1663 for (i = 0; i < dev->real_num_tx_queues; ++i)
1664 rcu_assign_pointer(np->queues[i].xdp_prog, prog);
1667 for (i = 0; i < dev->real_num_tx_queues; ++i)
1668 bpf_prog_put(old_prog);
1670 xenbus_switch_state(np->xbdev, XenbusStateConnected);
1675 static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1677 struct netfront_info *np = netdev_priv(dev);
1682 switch (xdp->command) {
1683 case XDP_SETUP_PROG:
1684 return xennet_xdp_set(dev, xdp->prog, xdp->extack);
1690 static const struct net_device_ops xennet_netdev_ops = {
1691 .ndo_uninit = xennet_uninit,
1692 .ndo_open = xennet_open,
1693 .ndo_stop = xennet_close,
1694 .ndo_start_xmit = xennet_start_xmit,
1695 .ndo_change_mtu = xennet_change_mtu,
1696 .ndo_get_stats64 = xennet_get_stats64,
1697 .ndo_set_mac_address = eth_mac_addr,
1698 .ndo_validate_addr = eth_validate_addr,
1699 .ndo_fix_features = xennet_fix_features,
1700 .ndo_set_features = xennet_set_features,
1701 .ndo_select_queue = xennet_select_queue,
1702 .ndo_bpf = xennet_xdp,
1703 .ndo_xdp_xmit = xennet_xdp_xmit,
1704 #ifdef CONFIG_NET_POLL_CONTROLLER
1705 .ndo_poll_controller = xennet_poll_controller,
1709 static void xennet_free_netdev(struct net_device *netdev)
1711 struct netfront_info *np = netdev_priv(netdev);
1713 free_percpu(np->rx_stats);
1714 free_percpu(np->tx_stats);
1715 free_netdev(netdev);
1718 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1721 struct net_device *netdev;
1722 struct netfront_info *np;
1724 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1726 return ERR_PTR(-ENOMEM);
1728 np = netdev_priv(netdev);
1734 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1735 if (np->rx_stats == NULL)
1737 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1738 if (np->tx_stats == NULL)
1741 netdev->netdev_ops = &xennet_netdev_ops;
1743 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1745 netdev->hw_features = NETIF_F_SG |
1747 NETIF_F_TSO | NETIF_F_TSO6;
1750 * Assume that all hw features are available for now. This set
1751 * will be adjusted by the call to netdev_update_features() in
1752 * xennet_connect() which is the earliest point where we can
1753 * negotiate with the backend regarding supported features.
1755 netdev->features |= netdev->hw_features;
1756 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
1757 NETDEV_XDP_ACT_NDO_XMIT;
1759 netdev->ethtool_ops = &xennet_ethtool_ops;
1760 netdev->min_mtu = ETH_MIN_MTU;
1761 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
1762 SET_NETDEV_DEV(netdev, &dev->dev);
1764 np->netdev = netdev;
1765 np->netfront_xdp_enabled = false;
1767 netif_carrier_off(netdev);
1770 xenbus_switch_state(dev, XenbusStateInitialising);
1771 err = wait_event_timeout(module_wq,
1772 xenbus_read_driver_state(dev->otherend) !=
1773 XenbusStateClosed &&
1774 xenbus_read_driver_state(dev->otherend) !=
1775 XenbusStateUnknown, XENNET_TIMEOUT);
1781 xennet_free_netdev(netdev);
1782 return ERR_PTR(err);
1786 * Entry point to this code when a new device is created. Allocate the basic
1787 * structures and the ring buffers for communication with the backend, and
1788 * inform the backend of the appropriate details for those.
1790 static int netfront_probe(struct xenbus_device *dev,
1791 const struct xenbus_device_id *id)
1794 struct net_device *netdev;
1795 struct netfront_info *info;
1797 netdev = xennet_create_dev(dev);
1798 if (IS_ERR(netdev)) {
1799 err = PTR_ERR(netdev);
1800 xenbus_dev_fatal(dev, err, "creating netdev");
1804 info = netdev_priv(netdev);
1805 dev_set_drvdata(&dev->dev, info);
1807 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1813 static void xennet_end_access(int ref, void *page)
1815 /* This frees the page as a side-effect */
1816 if (ref != INVALID_GRANT_REF)
1817 gnttab_end_foreign_access(ref, virt_to_page(page));
1820 static void xennet_disconnect_backend(struct netfront_info *info)
1823 unsigned int num_queues = info->netdev->real_num_tx_queues;
1825 netif_carrier_off(info->netdev);
1827 for (i = 0; i < num_queues && info->queues; ++i) {
1828 struct netfront_queue *queue = &info->queues[i];
1830 timer_delete_sync(&queue->rx_refill_timer);
1832 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1833 unbind_from_irqhandler(queue->tx_irq, queue);
1834 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1835 unbind_from_irqhandler(queue->tx_irq, queue);
1836 unbind_from_irqhandler(queue->rx_irq, queue);
1838 queue->tx_evtchn = queue->rx_evtchn = 0;
1839 queue->tx_irq = queue->rx_irq = 0;
1841 if (netif_running(info->netdev))
1842 napi_synchronize(&queue->napi);
1844 xennet_release_tx_bufs(queue);
1845 xennet_release_rx_bufs(queue);
1846 gnttab_free_grant_references(queue->gref_tx_head);
1847 gnttab_free_grant_references(queue->gref_rx_head);
1849 /* End access and free the pages */
1850 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1851 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1853 queue->tx_ring_ref = INVALID_GRANT_REF;
1854 queue->rx_ring_ref = INVALID_GRANT_REF;
1855 queue->tx.sring = NULL;
1856 queue->rx.sring = NULL;
1858 page_pool_destroy(queue->page_pool);
1863 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1864 * driver restart. We tear down our netif structure and recreate it, but
1865 * leave the device-layer structures intact so that this is transparent to the
1866 * rest of the kernel.
1868 static int netfront_resume(struct xenbus_device *dev)
1870 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1872 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1874 netif_tx_lock_bh(info->netdev);
1875 netif_device_detach(info->netdev);
1876 netif_tx_unlock_bh(info->netdev);
1878 xennet_disconnect_backend(info);
1882 xennet_destroy_queues(info);
1888 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1890 char *s, *e, *macstr;
1893 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1895 return PTR_ERR(macstr);
1897 for (i = 0; i < ETH_ALEN; i++) {
1898 mac[i] = simple_strtoul(s, &e, 16);
1899 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1910 static int setup_netfront_single(struct netfront_queue *queue)
1914 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1918 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1919 xennet_interrupt, 0,
1920 queue->info->netdev->name,
1924 queue->rx_evtchn = queue->tx_evtchn;
1925 queue->rx_irq = queue->tx_irq = err;
1930 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1931 queue->tx_evtchn = 0;
1936 static int setup_netfront_split(struct netfront_queue *queue)
1940 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1943 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1945 goto alloc_rx_evtchn_fail;
1947 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1948 "%s-tx", queue->name);
1949 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1950 xennet_tx_interrupt, 0,
1951 queue->tx_irq_name, queue);
1954 queue->tx_irq = err;
1956 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1957 "%s-rx", queue->name);
1958 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1959 xennet_rx_interrupt, 0,
1960 queue->rx_irq_name, queue);
1963 queue->rx_irq = err;
1968 unbind_from_irqhandler(queue->tx_irq, queue);
1971 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1972 queue->rx_evtchn = 0;
1973 alloc_rx_evtchn_fail:
1974 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1975 queue->tx_evtchn = 0;
1980 static int setup_netfront(struct xenbus_device *dev,
1981 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1983 struct xen_netif_tx_sring *txs;
1984 struct xen_netif_rx_sring *rxs;
1987 queue->tx_ring_ref = INVALID_GRANT_REF;
1988 queue->rx_ring_ref = INVALID_GRANT_REF;
1989 queue->rx.sring = NULL;
1990 queue->tx.sring = NULL;
1992 err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH, (void **)&txs,
1993 1, &queue->tx_ring_ref);
1997 XEN_FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1999 err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH, (void **)&rxs,
2000 1, &queue->rx_ring_ref);
2004 XEN_FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
2006 if (feature_split_evtchn)
2007 err = setup_netfront_split(queue);
2008 /* setup single event channel if
2009 * a) feature-split-event-channels == 0
2010 * b) feature-split-event-channels == 1 but failed to setup
2012 if (!feature_split_evtchn || err)
2013 err = setup_netfront_single(queue);
2021 xenbus_teardown_ring((void **)&queue->rx.sring, 1, &queue->rx_ring_ref);
2022 xenbus_teardown_ring((void **)&queue->tx.sring, 1, &queue->tx_ring_ref);
2027 /* Queue-specific initialisation
2028 * This used to be done in xennet_create_dev() but must now
2031 static int xennet_init_queue(struct netfront_queue *queue)
2037 spin_lock_init(&queue->tx_lock);
2038 spin_lock_init(&queue->rx_lock);
2039 spin_lock_init(&queue->rx_cons_lock);
2041 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
2043 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
2044 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
2047 /* Initialise tx_skb_freelist as a free chain containing every entry. */
2048 queue->tx_skb_freelist = 0;
2049 queue->tx_pend_queue = TX_LINK_NONE;
2050 for (i = 0; i < NET_TX_RING_SIZE; i++) {
2051 queue->tx_link[i] = i + 1;
2052 queue->grant_tx_ref[i] = INVALID_GRANT_REF;
2053 queue->grant_tx_page[i] = NULL;
2055 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
2057 /* Clear out rx_skbs */
2058 for (i = 0; i < NET_RX_RING_SIZE; i++) {
2059 queue->rx_skbs[i] = NULL;
2060 queue->grant_rx_ref[i] = INVALID_GRANT_REF;
2063 /* A grant for every tx ring slot */
2064 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
2065 &queue->gref_tx_head) < 0) {
2066 pr_alert("can't alloc tx grant refs\n");
2071 /* A grant for every rx ring slot */
2072 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
2073 &queue->gref_rx_head) < 0) {
2074 pr_alert("can't alloc rx grant refs\n");
2082 gnttab_free_grant_references(queue->gref_tx_head);
2087 static int write_queue_xenstore_keys(struct netfront_queue *queue,
2088 struct xenbus_transaction *xbt, int write_hierarchical)
2090 /* Write the queue-specific keys into XenStore in the traditional
2091 * way for a single queue, or in a queue subkeys for multiple
2094 struct xenbus_device *dev = queue->info->xbdev;
2096 const char *message;
2100 /* Choose the correct place to write the keys */
2101 if (write_hierarchical) {
2102 pathsize = strlen(dev->nodename) + 10;
2103 path = kzalloc(pathsize, GFP_KERNEL);
2106 message = "out of memory while writing ring references";
2109 snprintf(path, pathsize, "%s/queue-%u",
2110 dev->nodename, queue->id);
2112 path = (char *)dev->nodename;
2115 /* Write ring references */
2116 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
2117 queue->tx_ring_ref);
2119 message = "writing tx-ring-ref";
2123 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
2124 queue->rx_ring_ref);
2126 message = "writing rx-ring-ref";
2130 /* Write event channels; taking into account both shared
2131 * and split event channel scenarios.
2133 if (queue->tx_evtchn == queue->rx_evtchn) {
2134 /* Shared event channel */
2135 err = xenbus_printf(*xbt, path,
2136 "event-channel", "%u", queue->tx_evtchn);
2138 message = "writing event-channel";
2142 /* Split event channels */
2143 err = xenbus_printf(*xbt, path,
2144 "event-channel-tx", "%u", queue->tx_evtchn);
2146 message = "writing event-channel-tx";
2150 err = xenbus_printf(*xbt, path,
2151 "event-channel-rx", "%u", queue->rx_evtchn);
2153 message = "writing event-channel-rx";
2158 if (write_hierarchical)
2163 if (write_hierarchical)
2165 xenbus_dev_fatal(dev, err, "%s", message);
2171 static int xennet_create_page_pool(struct netfront_queue *queue)
2174 struct page_pool_params pp_params = {
2177 .pool_size = NET_RX_RING_SIZE,
2178 .nid = NUMA_NO_NODE,
2179 .dev = &queue->info->netdev->dev,
2180 .offset = XDP_PACKET_HEADROOM,
2181 .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
2184 queue->page_pool = page_pool_create(&pp_params);
2185 if (IS_ERR(queue->page_pool)) {
2186 err = PTR_ERR(queue->page_pool);
2187 queue->page_pool = NULL;
2191 err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
2194 netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
2198 err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
2199 MEM_TYPE_PAGE_POOL, queue->page_pool);
2201 netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
2202 goto err_unregister_rxq;
2207 xdp_rxq_info_unreg(&queue->xdp_rxq);
2209 page_pool_destroy(queue->page_pool);
2210 queue->page_pool = NULL;
2214 static int xennet_create_queues(struct netfront_info *info,
2215 unsigned int *num_queues)
2220 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
2225 for (i = 0; i < *num_queues; i++) {
2226 struct netfront_queue *queue = &info->queues[i];
2231 ret = xennet_init_queue(queue);
2233 dev_warn(&info->xbdev->dev,
2234 "only created %d queues\n", i);
2239 /* use page pool recycling instead of buddy allocator */
2240 ret = xennet_create_page_pool(queue);
2242 dev_err(&info->xbdev->dev, "can't allocate page pool\n");
2247 netif_napi_add(queue->info->netdev, &queue->napi, xennet_poll);
2248 if (netif_running(info->netdev))
2249 napi_enable(&queue->napi);
2252 netif_set_real_num_tx_queues(info->netdev, *num_queues);
2254 if (*num_queues == 0) {
2255 dev_err(&info->xbdev->dev, "no queues\n");
2261 /* Common code used when first setting up, and when resuming. */
2262 static int talk_to_netback(struct xenbus_device *dev,
2263 struct netfront_info *info)
2265 const char *message;
2266 struct xenbus_transaction xbt;
2268 unsigned int feature_split_evtchn;
2270 unsigned int max_queues = 0;
2271 struct netfront_queue *queue = NULL;
2272 unsigned int num_queues = 1;
2275 info->netdev->irq = 0;
2277 /* Check if backend is trusted. */
2278 info->bounce = !xennet_trusted ||
2279 !xenbus_read_unsigned(dev->nodename, "trusted", 1);
2281 /* Check if backend supports multiple queues */
2282 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2283 "multi-queue-max-queues", 1);
2284 num_queues = min(max_queues, xennet_max_queues);
2286 /* Check feature-split-event-channels */
2287 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2288 "feature-split-event-channels", 0);
2290 /* Read mac addr. */
2291 err = xen_net_read_mac(dev, addr);
2293 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
2296 eth_hw_addr_set(info->netdev, addr);
2298 info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
2299 "feature-xdp-headroom", 0);
2300 if (info->netback_has_xdp_headroom) {
2301 /* set the current xen-netfront xdp state */
2302 err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
2303 NETBACK_XDP_HEADROOM_ENABLE :
2304 NETBACK_XDP_HEADROOM_DISABLE);
2311 xennet_destroy_queues(info);
2313 /* For the case of a reconnect reset the "broken" indicator. */
2314 info->broken = false;
2316 err = xennet_create_queues(info, &num_queues);
2318 xenbus_dev_fatal(dev, err, "creating queues");
2319 kfree(info->queues);
2320 info->queues = NULL;
2325 /* Create shared ring, alloc event channel -- for each queue */
2326 for (i = 0; i < num_queues; ++i) {
2327 queue = &info->queues[i];
2328 err = setup_netfront(dev, queue, feature_split_evtchn);
2334 err = xenbus_transaction_start(&xbt);
2336 xenbus_dev_fatal(dev, err, "starting transaction");
2340 if (xenbus_exists(XBT_NIL,
2341 info->xbdev->otherend, "multi-queue-max-queues")) {
2342 /* Write the number of queues */
2343 err = xenbus_printf(xbt, dev->nodename,
2344 "multi-queue-num-queues", "%u", num_queues);
2346 message = "writing multi-queue-num-queues";
2347 goto abort_transaction_no_dev_fatal;
2351 if (num_queues == 1) {
2352 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2354 goto abort_transaction_no_dev_fatal;
2356 /* Write the keys for each queue */
2357 for (i = 0; i < num_queues; ++i) {
2358 queue = &info->queues[i];
2359 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2361 goto abort_transaction_no_dev_fatal;
2365 /* The remaining keys are not queue-specific */
2366 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2369 message = "writing request-rx-copy";
2370 goto abort_transaction;
2373 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2375 message = "writing feature-rx-notify";
2376 goto abort_transaction;
2379 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2381 message = "writing feature-sg";
2382 goto abort_transaction;
2385 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2387 message = "writing feature-gso-tcpv4";
2388 goto abort_transaction;
2391 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2393 message = "writing feature-gso-tcpv6";
2394 goto abort_transaction;
2397 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2400 message = "writing feature-ipv6-csum-offload";
2401 goto abort_transaction;
2404 err = xenbus_transaction_end(xbt, 0);
2408 xenbus_dev_fatal(dev, err, "completing transaction");
2415 xenbus_dev_fatal(dev, err, "%s", message);
2416 abort_transaction_no_dev_fatal:
2417 xenbus_transaction_end(xbt, 1);
2419 xennet_disconnect_backend(info);
2421 xennet_destroy_queues(info);
2425 device_unregister(&dev->dev);
2429 static int xennet_connect(struct net_device *dev)
2431 struct netfront_info *np = netdev_priv(dev);
2432 unsigned int num_queues = 0;
2435 struct netfront_queue *queue = NULL;
2437 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
2439 "backend does not support copying receive path\n");
2443 err = talk_to_netback(np->xbdev, np);
2446 if (np->netback_has_xdp_headroom)
2447 pr_info("backend supports XDP headroom\n");
2449 dev_info(&np->xbdev->dev,
2450 "bouncing transmitted data to zeroed pages\n");
2452 /* talk_to_netback() sets the correct number of queues */
2453 num_queues = dev->real_num_tx_queues;
2455 if (dev->reg_state == NETREG_UNINITIALIZED) {
2456 err = register_netdev(dev);
2458 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2459 device_unregister(&np->xbdev->dev);
2465 netdev_update_features(dev);
2469 * All public and private state should now be sane. Get
2470 * ready to start sending and receiving packets and give the driver
2471 * domain a kick because we've probably just requeued some
2474 netif_tx_lock_bh(np->netdev);
2475 netif_device_attach(np->netdev);
2476 netif_tx_unlock_bh(np->netdev);
2478 netif_carrier_on(np->netdev);
2479 for (j = 0; j < num_queues; ++j) {
2480 queue = &np->queues[j];
2482 notify_remote_via_irq(queue->tx_irq);
2483 if (queue->tx_irq != queue->rx_irq)
2484 notify_remote_via_irq(queue->rx_irq);
2486 spin_lock_bh(&queue->rx_lock);
2487 xennet_alloc_rx_buffers(queue);
2488 spin_unlock_bh(&queue->rx_lock);
2495 * Callback received when the backend's state changes.
2497 static void netback_changed(struct xenbus_device *dev,
2498 enum xenbus_state backend_state)
2500 struct netfront_info *np = dev_get_drvdata(&dev->dev);
2501 struct net_device *netdev = np->netdev;
2503 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2505 wake_up_all(&module_wq);
2507 switch (backend_state) {
2508 case XenbusStateInitialising:
2509 case XenbusStateInitialised:
2510 case XenbusStateReconfiguring:
2511 case XenbusStateReconfigured:
2512 case XenbusStateUnknown:
2515 case XenbusStateInitWait:
2516 if (dev->state != XenbusStateInitialising)
2518 if (xennet_connect(netdev) != 0)
2520 xenbus_switch_state(dev, XenbusStateConnected);
2523 case XenbusStateConnected:
2524 netdev_notify_peers(netdev);
2527 case XenbusStateClosed:
2528 if (dev->state == XenbusStateClosed)
2530 fallthrough; /* Missed the backend's CLOSING state */
2531 case XenbusStateClosing:
2532 xenbus_frontend_closed(dev);
2537 static const struct xennet_stat {
2538 char name[ETH_GSTRING_LEN];
2540 } xennet_stats[] = {
2542 "rx_gso_checksum_fixup",
2543 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2547 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2549 switch (string_set) {
2551 return ARRAY_SIZE(xennet_stats);
2557 static void xennet_get_ethtool_stats(struct net_device *dev,
2558 struct ethtool_stats *stats, u64 * data)
2560 void *np = netdev_priv(dev);
2563 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2564 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2567 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2571 switch (stringset) {
2573 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2574 memcpy(data + i * ETH_GSTRING_LEN,
2575 xennet_stats[i].name, ETH_GSTRING_LEN);
2580 static const struct ethtool_ops xennet_ethtool_ops =
2582 .get_link = ethtool_op_get_link,
2584 .get_sset_count = xennet_get_sset_count,
2585 .get_ethtool_stats = xennet_get_ethtool_stats,
2586 .get_strings = xennet_get_strings,
2587 .get_ts_info = ethtool_op_get_ts_info,
2591 static ssize_t show_rxbuf(struct device *dev,
2592 struct device_attribute *attr, char *buf)
2594 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2597 static ssize_t store_rxbuf(struct device *dev,
2598 struct device_attribute *attr,
2599 const char *buf, size_t len)
2603 if (!capable(CAP_NET_ADMIN))
2606 simple_strtoul(buf, &endp, 0);
2610 /* rxbuf_min and rxbuf_max are no longer configurable. */
2615 static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2616 static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2617 static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
2619 static struct attribute *xennet_dev_attrs[] = {
2620 &dev_attr_rxbuf_min.attr,
2621 &dev_attr_rxbuf_max.attr,
2622 &dev_attr_rxbuf_cur.attr,
2626 static const struct attribute_group xennet_dev_group = {
2627 .attrs = xennet_dev_attrs
2629 #endif /* CONFIG_SYSFS */
2631 static void xennet_bus_close(struct xenbus_device *dev)
2635 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2638 xenbus_switch_state(dev, XenbusStateClosing);
2639 ret = wait_event_timeout(module_wq,
2640 xenbus_read_driver_state(dev->otherend) ==
2641 XenbusStateClosing ||
2642 xenbus_read_driver_state(dev->otherend) ==
2643 XenbusStateClosed ||
2644 xenbus_read_driver_state(dev->otherend) ==
2649 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2653 xenbus_switch_state(dev, XenbusStateClosed);
2654 ret = wait_event_timeout(module_wq,
2655 xenbus_read_driver_state(dev->otherend) ==
2656 XenbusStateClosed ||
2657 xenbus_read_driver_state(dev->otherend) ==
2663 static void xennet_remove(struct xenbus_device *dev)
2665 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2667 xennet_bus_close(dev);
2668 xennet_disconnect_backend(info);
2670 if (info->netdev->reg_state == NETREG_REGISTERED)
2671 unregister_netdev(info->netdev);
2675 xennet_destroy_queues(info);
2678 xennet_free_netdev(info->netdev);
2681 static const struct xenbus_device_id netfront_ids[] = {
2686 static struct xenbus_driver netfront_driver = {
2687 .ids = netfront_ids,
2688 .probe = netfront_probe,
2689 .remove = xennet_remove,
2690 .resume = netfront_resume,
2691 .otherend_changed = netback_changed,
2694 static int __init netif_init(void)
2699 if (!xen_has_pv_nic_devices())
2702 pr_info("Initialising Xen virtual ethernet driver\n");
2704 /* Allow as many queues as there are CPUs inut max. 8 if user has not
2705 * specified a value.
2707 if (xennet_max_queues == 0)
2708 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2711 return xenbus_register_frontend(&netfront_driver);
2713 module_init(netif_init);
2716 static void __exit netif_exit(void)
2718 xenbus_unregister_driver(&netfront_driver);
2720 module_exit(netif_exit);
2722 MODULE_DESCRIPTION("Xen virtual network device frontend");
2723 MODULE_LICENSE("GPL");
2724 MODULE_ALIAS("xen:vif");
2725 MODULE_ALIAS("xennet");