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_query_foreign_access(
428 queue->grant_tx_ref[id]) != 0)) {
430 "Grant still in use by backend domain\n");
433 gnttab_end_foreign_access_ref(
434 queue->grant_tx_ref[id], GNTMAP_readonly);
435 gnttab_release_grant_reference(
436 &queue->gref_tx_head, queue->grant_tx_ref[id]);
437 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
438 queue->grant_tx_page[id] = NULL;
439 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
440 dev_kfree_skb_irq(skb);
443 queue->tx.rsp_cons = prod;
445 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
446 } while (more_to_do);
448 xennet_maybe_wake_tx(queue);
453 queue->info->broken = true;
454 dev_alert(dev, "Disabled for further use\n");
459 struct xennet_gnttab_make_txreq {
460 struct netfront_queue *queue;
463 struct xen_netif_tx_request *tx; /* Last request on ring page */
464 struct xen_netif_tx_request tx_local; /* Last request local copy*/
468 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
469 unsigned int len, void *data)
471 struct xennet_gnttab_make_txreq *info = data;
473 struct xen_netif_tx_request *tx;
475 /* convenient aliases */
476 struct page *page = info->page;
477 struct netfront_queue *queue = info->queue;
478 struct sk_buff *skb = info->skb;
480 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
481 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
482 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
483 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
485 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
486 gfn, GNTMAP_readonly);
488 queue->tx_skbs[id] = skb;
489 queue->grant_tx_page[id] = page;
490 queue->grant_tx_ref[id] = ref;
492 info->tx_local.id = id;
493 info->tx_local.gref = ref;
494 info->tx_local.offset = offset;
495 info->tx_local.size = len;
496 info->tx_local.flags = 0;
498 *tx = info->tx_local;
501 * Put the request in the pending queue, it will be set to be pending
502 * when the producer index is about to be raised.
504 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
507 info->size += info->tx_local.size;
510 static struct xen_netif_tx_request *xennet_make_first_txreq(
511 struct xennet_gnttab_make_txreq *info,
512 unsigned int offset, unsigned int len)
516 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
521 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
522 unsigned int len, void *data)
524 struct xennet_gnttab_make_txreq *info = data;
526 info->tx->flags |= XEN_NETTXF_more_data;
528 xennet_tx_setup_grant(gfn, offset, len, data);
531 static void xennet_make_txreqs(
532 struct xennet_gnttab_make_txreq *info,
534 unsigned int offset, unsigned int len)
536 /* Skip unused frames from start of page */
537 page += offset >> PAGE_SHIFT;
538 offset &= ~PAGE_MASK;
544 gnttab_foreach_grant_in_range(page, offset, len,
545 xennet_make_one_txreq,
555 * Count how many ring slots are required to send this skb. Each frag
556 * might be a compound page.
558 static int xennet_count_skb_slots(struct sk_buff *skb)
560 int i, frags = skb_shinfo(skb)->nr_frags;
563 slots = gnttab_count_grant(offset_in_page(skb->data),
566 for (i = 0; i < frags; i++) {
567 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
568 unsigned long size = skb_frag_size(frag);
569 unsigned long offset = skb_frag_off(frag);
571 /* Skip unused frames from start of page */
572 offset &= ~PAGE_MASK;
574 slots += gnttab_count_grant(offset, size);
580 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
581 struct net_device *sb_dev)
583 unsigned int num_queues = dev->real_num_tx_queues;
587 /* First, check if there is only one queue */
588 if (num_queues == 1) {
591 hash = skb_get_hash(skb);
592 queue_idx = hash % num_queues;
598 static void xennet_mark_tx_pending(struct netfront_queue *queue)
602 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
604 queue->tx_link[i] = TX_PENDING;
607 static int xennet_xdp_xmit_one(struct net_device *dev,
608 struct netfront_queue *queue,
609 struct xdp_frame *xdpf)
611 struct netfront_info *np = netdev_priv(dev);
612 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
613 struct xennet_gnttab_make_txreq info = {
616 .page = virt_to_page(xdpf->data),
620 xennet_make_first_txreq(&info,
621 offset_in_page(xdpf->data),
624 xennet_mark_tx_pending(queue);
626 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
628 notify_remote_via_irq(queue->tx_irq);
630 u64_stats_update_begin(&tx_stats->syncp);
631 tx_stats->bytes += xdpf->len;
633 u64_stats_update_end(&tx_stats->syncp);
635 xennet_tx_buf_gc(queue);
640 static int xennet_xdp_xmit(struct net_device *dev, int n,
641 struct xdp_frame **frames, u32 flags)
643 unsigned int num_queues = dev->real_num_tx_queues;
644 struct netfront_info *np = netdev_priv(dev);
645 struct netfront_queue *queue = NULL;
646 unsigned long irq_flags;
650 if (unlikely(np->broken))
652 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
655 queue = &np->queues[smp_processor_id() % num_queues];
657 spin_lock_irqsave(&queue->tx_lock, irq_flags);
658 for (i = 0; i < n; i++) {
659 struct xdp_frame *xdpf = frames[i];
663 if (xennet_xdp_xmit_one(dev, queue, xdpf))
667 spin_unlock_irqrestore(&queue->tx_lock, irq_flags);
673 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
675 static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
677 struct netfront_info *np = netdev_priv(dev);
678 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
679 struct xen_netif_tx_request *first_tx;
687 struct netfront_queue *queue = NULL;
688 struct xennet_gnttab_make_txreq info = { };
689 unsigned int num_queues = dev->real_num_tx_queues;
691 struct sk_buff *nskb;
693 /* Drop the packet if no queues are set up */
696 if (unlikely(np->broken))
698 /* Determine which queue to transmit this SKB on */
699 queue_index = skb_get_queue_mapping(skb);
700 queue = &np->queues[queue_index];
702 /* If skb->len is too big for wire format, drop skb and alert
703 * user about misconfiguration.
705 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
706 net_alert_ratelimited(
707 "xennet: skb->len = %u, too big for wire format\n",
712 slots = xennet_count_skb_slots(skb);
713 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
714 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
716 if (skb_linearize(skb))
720 page = virt_to_page(skb->data);
721 offset = offset_in_page(skb->data);
723 /* The first req should be at least ETH_HLEN size or the packet will be
724 * dropped by netback.
726 if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
727 nskb = skb_copy(skb, GFP_ATOMIC);
730 dev_consume_skb_any(skb);
732 page = virt_to_page(skb->data);
733 offset = offset_in_page(skb->data);
736 len = skb_headlen(skb);
738 spin_lock_irqsave(&queue->tx_lock, flags);
740 if (unlikely(!netif_carrier_ok(dev) ||
741 (slots > 1 && !xennet_can_sg(dev)) ||
742 netif_needs_gso(skb, netif_skb_features(skb)))) {
743 spin_unlock_irqrestore(&queue->tx_lock, flags);
747 /* First request for the linear area. */
751 first_tx = xennet_make_first_txreq(&info, offset, len);
752 offset += info.tx_local.size;
753 if (offset == PAGE_SIZE) {
757 len -= info.tx_local.size;
759 if (skb->ip_summed == CHECKSUM_PARTIAL)
761 first_tx->flags |= XEN_NETTXF_csum_blank |
762 XEN_NETTXF_data_validated;
763 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
764 /* remote but checksummed. */
765 first_tx->flags |= XEN_NETTXF_data_validated;
767 /* Optional extra info after the first request. */
768 if (skb_shinfo(skb)->gso_size) {
769 struct xen_netif_extra_info *gso;
771 gso = (struct xen_netif_extra_info *)
772 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
774 first_tx->flags |= XEN_NETTXF_extra_info;
776 gso->u.gso.size = skb_shinfo(skb)->gso_size;
777 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
778 XEN_NETIF_GSO_TYPE_TCPV6 :
779 XEN_NETIF_GSO_TYPE_TCPV4;
781 gso->u.gso.features = 0;
783 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
787 /* Requests for the rest of the linear area. */
788 xennet_make_txreqs(&info, page, offset, len);
790 /* Requests for all the frags. */
791 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
792 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
793 xennet_make_txreqs(&info, skb_frag_page(frag),
795 skb_frag_size(frag));
798 /* First request has the packet length. */
799 first_tx->size = skb->len;
801 /* timestamp packet in software */
802 skb_tx_timestamp(skb);
804 xennet_mark_tx_pending(queue);
806 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
808 notify_remote_via_irq(queue->tx_irq);
810 u64_stats_update_begin(&tx_stats->syncp);
811 tx_stats->bytes += skb->len;
813 u64_stats_update_end(&tx_stats->syncp);
815 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
816 xennet_tx_buf_gc(queue);
818 if (!netfront_tx_slot_available(queue))
819 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
821 spin_unlock_irqrestore(&queue->tx_lock, flags);
826 dev->stats.tx_dropped++;
827 dev_kfree_skb_any(skb);
831 static int xennet_close(struct net_device *dev)
833 struct netfront_info *np = netdev_priv(dev);
834 unsigned int num_queues = dev->real_num_tx_queues;
836 struct netfront_queue *queue;
837 netif_tx_stop_all_queues(np->netdev);
838 for (i = 0; i < num_queues; ++i) {
839 queue = &np->queues[i];
840 napi_disable(&queue->napi);
845 static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
849 spin_lock_irqsave(&queue->rx_cons_lock, flags);
850 queue->rx.rsp_cons = val;
851 queue->rx_rsp_unconsumed = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
852 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
855 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
858 int new = xennet_rxidx(queue->rx.req_prod_pvt);
860 BUG_ON(queue->rx_skbs[new]);
861 queue->rx_skbs[new] = skb;
862 queue->grant_rx_ref[new] = ref;
863 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
864 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
865 queue->rx.req_prod_pvt++;
868 static int xennet_get_extras(struct netfront_queue *queue,
869 struct xen_netif_extra_info *extras,
873 struct xen_netif_extra_info extra;
874 struct device *dev = &queue->info->netdev->dev;
875 RING_IDX cons = queue->rx.rsp_cons;
882 if (unlikely(cons + 1 == rp)) {
884 dev_warn(dev, "Missing extra info\n");
889 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
891 if (unlikely(!extra.type ||
892 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
894 dev_warn(dev, "Invalid extra type: %d\n",
898 extras[extra.type - 1] = extra;
901 skb = xennet_get_rx_skb(queue, cons);
902 ref = xennet_get_rx_ref(queue, cons);
903 xennet_move_rx_slot(queue, skb, ref);
904 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
906 xennet_set_rx_rsp_cons(queue, cons);
910 static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata,
911 struct xen_netif_rx_response *rx, struct bpf_prog *prog,
912 struct xdp_buff *xdp, bool *need_xdp_flush)
914 struct xdp_frame *xdpf;
915 u32 len = rx->status;
919 xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
921 xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM,
924 act = bpf_prog_run_xdp(prog, xdp);
928 xdpf = xdp_convert_buff_to_frame(xdp);
929 err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
931 xdp_return_frame_rx_napi(xdpf);
932 else if (unlikely(err < 0))
933 trace_xdp_exception(queue->info->netdev, prog, act);
937 err = xdp_do_redirect(queue->info->netdev, xdp, prog);
938 *need_xdp_flush = true;
940 trace_xdp_exception(queue->info->netdev, prog, act);
947 trace_xdp_exception(queue->info->netdev, prog, act);
951 bpf_warn_invalid_xdp_action(queue->info->netdev, prog, act);
957 static int xennet_get_responses(struct netfront_queue *queue,
958 struct netfront_rx_info *rinfo, RING_IDX rp,
959 struct sk_buff_head *list,
960 bool *need_xdp_flush)
962 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
963 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
964 RING_IDX cons = queue->rx.rsp_cons;
965 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
966 struct xen_netif_extra_info *extras = rinfo->extras;
967 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
968 struct device *dev = &queue->info->netdev->dev;
969 struct bpf_prog *xdp_prog;
976 if (rx->flags & XEN_NETRXF_extra_info) {
977 err = xennet_get_extras(queue, extras, rp);
979 if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) {
980 struct xen_netif_extra_info *xdp;
982 xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1];
983 rx->offset = xdp->u.xdp.headroom;
986 cons = queue->rx.rsp_cons;
990 if (unlikely(rx->status < 0 ||
991 rx->offset + rx->status > XEN_PAGE_SIZE)) {
993 dev_warn(dev, "rx->offset: %u, size: %d\n",
994 rx->offset, rx->status);
995 xennet_move_rx_slot(queue, skb, ref);
1001 * This definitely indicates a bug, either in this driver or in
1002 * the backend driver. In future this should flag the bad
1003 * situation to the system controller to reboot the backend.
1005 if (ref == GRANT_INVALID_REF) {
1006 if (net_ratelimit())
1007 dev_warn(dev, "Bad rx response id %d.\n",
1013 ret = gnttab_end_foreign_access_ref(ref, 0);
1016 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
1019 xdp_prog = rcu_dereference(queue->xdp_prog);
1021 if (!(rx->flags & XEN_NETRXF_more_data)) {
1022 /* currently only a single page contains data */
1023 verdict = xennet_run_xdp(queue,
1024 skb_frag_page(&skb_shinfo(skb)->frags[0]),
1025 rx, xdp_prog, &xdp, need_xdp_flush);
1026 if (verdict != XDP_PASS)
1029 /* drop the frame */
1035 __skb_queue_tail(list, skb);
1036 if (!(rx->flags & XEN_NETRXF_more_data))
1039 if (cons + slots == rp) {
1040 if (net_ratelimit())
1041 dev_warn(dev, "Need more slots\n");
1046 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
1048 skb = xennet_get_rx_skb(queue, cons + slots);
1049 ref = xennet_get_rx_ref(queue, cons + slots);
1053 if (unlikely(slots > max)) {
1054 if (net_ratelimit())
1055 dev_warn(dev, "Too many slots\n");
1060 xennet_set_rx_rsp_cons(queue, cons + slots);
1065 static int xennet_set_skb_gso(struct sk_buff *skb,
1066 struct xen_netif_extra_info *gso)
1068 if (!gso->u.gso.size) {
1069 if (net_ratelimit())
1070 pr_warn("GSO size must not be zero\n");
1074 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
1075 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
1076 if (net_ratelimit())
1077 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
1081 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1082 skb_shinfo(skb)->gso_type =
1083 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
1087 /* Header must be checked, and gso_segs computed. */
1088 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1089 skb_shinfo(skb)->gso_segs = 0;
1094 static int xennet_fill_frags(struct netfront_queue *queue,
1095 struct sk_buff *skb,
1096 struct sk_buff_head *list)
1098 RING_IDX cons = queue->rx.rsp_cons;
1099 struct sk_buff *nskb;
1101 while ((nskb = __skb_dequeue(list))) {
1102 struct xen_netif_rx_response rx;
1103 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
1105 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
1107 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
1108 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1110 BUG_ON(pull_to < skb_headlen(skb));
1111 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1113 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
1114 xennet_set_rx_rsp_cons(queue,
1115 ++cons + skb_queue_len(list));
1120 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1121 skb_frag_page(nfrag),
1122 rx.offset, rx.status, PAGE_SIZE);
1124 skb_shinfo(nskb)->nr_frags = 0;
1128 xennet_set_rx_rsp_cons(queue, cons);
1133 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
1135 bool recalculate_partial_csum = false;
1138 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1139 * peers can fail to set NETRXF_csum_blank when sending a GSO
1140 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1141 * recalculate the partial checksum.
1143 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1144 struct netfront_info *np = netdev_priv(dev);
1145 atomic_inc(&np->rx_gso_checksum_fixup);
1146 skb->ip_summed = CHECKSUM_PARTIAL;
1147 recalculate_partial_csum = true;
1150 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1151 if (skb->ip_summed != CHECKSUM_PARTIAL)
1154 return skb_checksum_setup(skb, recalculate_partial_csum);
1157 static int handle_incoming_queue(struct netfront_queue *queue,
1158 struct sk_buff_head *rxq)
1160 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
1161 int packets_dropped = 0;
1162 struct sk_buff *skb;
1164 while ((skb = __skb_dequeue(rxq)) != NULL) {
1165 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1167 if (pull_to > skb_headlen(skb))
1168 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1170 /* Ethernet work: Delayed to here as it peeks the header. */
1171 skb->protocol = eth_type_trans(skb, queue->info->netdev);
1172 skb_reset_network_header(skb);
1174 if (checksum_setup(queue->info->netdev, skb)) {
1177 queue->info->netdev->stats.rx_errors++;
1181 u64_stats_update_begin(&rx_stats->syncp);
1182 rx_stats->packets++;
1183 rx_stats->bytes += skb->len;
1184 u64_stats_update_end(&rx_stats->syncp);
1187 napi_gro_receive(&queue->napi, skb);
1190 return packets_dropped;
1193 static int xennet_poll(struct napi_struct *napi, int budget)
1195 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1196 struct net_device *dev = queue->info->netdev;
1197 struct sk_buff *skb;
1198 struct netfront_rx_info rinfo;
1199 struct xen_netif_rx_response *rx = &rinfo.rx;
1200 struct xen_netif_extra_info *extras = rinfo.extras;
1203 struct sk_buff_head rxq;
1204 struct sk_buff_head errq;
1205 struct sk_buff_head tmpq;
1207 bool need_xdp_flush = false;
1209 spin_lock(&queue->rx_lock);
1211 skb_queue_head_init(&rxq);
1212 skb_queue_head_init(&errq);
1213 skb_queue_head_init(&tmpq);
1215 rp = queue->rx.sring->rsp_prod;
1216 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1217 dev_alert(&dev->dev, "Illegal number of responses %u\n",
1218 rp - queue->rx.rsp_cons);
1219 queue->info->broken = true;
1220 spin_unlock(&queue->rx_lock);
1223 rmb(); /* Ensure we see queued responses up to 'rp'. */
1225 i = queue->rx.rsp_cons;
1227 while ((i != rp) && (work_done < budget)) {
1228 RING_COPY_RESPONSE(&queue->rx, i, rx);
1229 memset(extras, 0, sizeof(rinfo.extras));
1231 err = xennet_get_responses(queue, &rinfo, rp, &tmpq,
1234 if (unlikely(err)) {
1236 while ((skb = __skb_dequeue(&tmpq)))
1237 __skb_queue_tail(&errq, skb);
1238 dev->stats.rx_errors++;
1239 i = queue->rx.rsp_cons;
1243 skb = __skb_dequeue(&tmpq);
1245 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1246 struct xen_netif_extra_info *gso;
1247 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1249 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1250 __skb_queue_head(&tmpq, skb);
1251 xennet_set_rx_rsp_cons(queue,
1252 queue->rx.rsp_cons +
1253 skb_queue_len(&tmpq));
1258 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1259 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1260 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
1262 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
1263 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1264 skb->data_len = rx->status;
1265 skb->len += rx->status;
1267 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
1270 if (rx->flags & XEN_NETRXF_csum_blank)
1271 skb->ip_summed = CHECKSUM_PARTIAL;
1272 else if (rx->flags & XEN_NETRXF_data_validated)
1273 skb->ip_summed = CHECKSUM_UNNECESSARY;
1275 __skb_queue_tail(&rxq, skb);
1277 i = queue->rx.rsp_cons + 1;
1278 xennet_set_rx_rsp_cons(queue, i);
1284 __skb_queue_purge(&errq);
1286 work_done -= handle_incoming_queue(queue, &rxq);
1288 xennet_alloc_rx_buffers(queue);
1290 if (work_done < budget) {
1293 napi_complete_done(napi, work_done);
1295 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1297 napi_schedule(napi);
1300 spin_unlock(&queue->rx_lock);
1305 static int xennet_change_mtu(struct net_device *dev, int mtu)
1307 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
1315 static void xennet_get_stats64(struct net_device *dev,
1316 struct rtnl_link_stats64 *tot)
1318 struct netfront_info *np = netdev_priv(dev);
1321 for_each_possible_cpu(cpu) {
1322 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1323 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
1324 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1328 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1329 tx_packets = tx_stats->packets;
1330 tx_bytes = tx_stats->bytes;
1331 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
1334 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1335 rx_packets = rx_stats->packets;
1336 rx_bytes = rx_stats->bytes;
1337 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
1339 tot->rx_packets += rx_packets;
1340 tot->tx_packets += tx_packets;
1341 tot->rx_bytes += rx_bytes;
1342 tot->tx_bytes += tx_bytes;
1345 tot->rx_errors = dev->stats.rx_errors;
1346 tot->tx_dropped = dev->stats.tx_dropped;
1349 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1351 struct sk_buff *skb;
1354 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1355 /* Skip over entries which are actually freelist references */
1356 if (!queue->tx_skbs[i])
1359 skb = queue->tx_skbs[i];
1360 queue->tx_skbs[i] = NULL;
1361 get_page(queue->grant_tx_page[i]);
1362 gnttab_end_foreign_access(queue->grant_tx_ref[i],
1364 (unsigned long)page_address(queue->grant_tx_page[i]));
1365 queue->grant_tx_page[i] = NULL;
1366 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1367 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
1368 dev_kfree_skb_irq(skb);
1372 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1376 spin_lock_bh(&queue->rx_lock);
1378 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1379 struct sk_buff *skb;
1382 skb = queue->rx_skbs[id];
1386 ref = queue->grant_rx_ref[id];
1387 if (ref == GRANT_INVALID_REF)
1390 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1392 /* gnttab_end_foreign_access() needs a page ref until
1393 * foreign access is ended (which may be deferred).
1396 gnttab_end_foreign_access(ref, 0,
1397 (unsigned long)page_address(page));
1398 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
1403 spin_unlock_bh(&queue->rx_lock);
1406 static netdev_features_t xennet_fix_features(struct net_device *dev,
1407 netdev_features_t features)
1409 struct netfront_info *np = netdev_priv(dev);
1411 if (features & NETIF_F_SG &&
1412 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1413 features &= ~NETIF_F_SG;
1415 if (features & NETIF_F_IPV6_CSUM &&
1416 !xenbus_read_unsigned(np->xbdev->otherend,
1417 "feature-ipv6-csum-offload", 0))
1418 features &= ~NETIF_F_IPV6_CSUM;
1420 if (features & NETIF_F_TSO &&
1421 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1422 features &= ~NETIF_F_TSO;
1424 if (features & NETIF_F_TSO6 &&
1425 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1426 features &= ~NETIF_F_TSO6;
1431 static int xennet_set_features(struct net_device *dev,
1432 netdev_features_t features)
1434 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1435 netdev_info(dev, "Reducing MTU because no SG offload");
1436 dev->mtu = ETH_DATA_LEN;
1442 static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
1444 unsigned long flags;
1446 if (unlikely(queue->info->broken))
1449 spin_lock_irqsave(&queue->tx_lock, flags);
1450 if (xennet_tx_buf_gc(queue))
1452 spin_unlock_irqrestore(&queue->tx_lock, flags);
1457 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1459 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1461 if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1462 xen_irq_lateeoi(irq, eoiflag);
1467 static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
1469 unsigned int work_queued;
1470 unsigned long flags;
1472 if (unlikely(queue->info->broken))
1475 spin_lock_irqsave(&queue->rx_cons_lock, flags);
1476 work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
1477 if (work_queued > queue->rx_rsp_unconsumed) {
1478 queue->rx_rsp_unconsumed = work_queued;
1480 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1481 const struct device *dev = &queue->info->netdev->dev;
1483 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1484 dev_alert(dev, "RX producer index going backwards\n");
1485 dev_alert(dev, "Disabled for further use\n");
1486 queue->info->broken = true;
1489 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1491 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
1492 napi_schedule(&queue->napi);
1497 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1499 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1501 if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1502 xen_irq_lateeoi(irq, eoiflag);
1507 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1509 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1511 if (xennet_handle_tx(dev_id, &eoiflag) &&
1512 xennet_handle_rx(dev_id, &eoiflag))
1513 xen_irq_lateeoi(irq, eoiflag);
1518 #ifdef CONFIG_NET_POLL_CONTROLLER
1519 static void xennet_poll_controller(struct net_device *dev)
1521 /* Poll each queue */
1522 struct netfront_info *info = netdev_priv(dev);
1523 unsigned int num_queues = dev->real_num_tx_queues;
1529 for (i = 0; i < num_queues; ++i)
1530 xennet_interrupt(0, &info->queues[i]);
1534 #define NETBACK_XDP_HEADROOM_DISABLE 0
1535 #define NETBACK_XDP_HEADROOM_ENABLE 1
1537 static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
1540 unsigned short headroom;
1542 headroom = xdp ? XDP_PACKET_HEADROOM : 0;
1543 err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
1544 "xdp-headroom", "%hu",
1547 pr_warn("Error writing xdp-headroom\n");
1552 static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1553 struct netlink_ext_ack *extack)
1555 unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
1556 struct netfront_info *np = netdev_priv(dev);
1557 struct bpf_prog *old_prog;
1558 unsigned int i, err;
1560 if (dev->mtu > max_mtu) {
1561 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
1565 if (!np->netback_has_xdp_headroom)
1568 xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
1570 err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
1571 NETBACK_XDP_HEADROOM_DISABLE);
1575 /* avoid the race with XDP headroom adjustment */
1576 wait_event(module_wq,
1577 xenbus_read_driver_state(np->xbdev->otherend) ==
1578 XenbusStateReconfigured);
1579 np->netfront_xdp_enabled = true;
1581 old_prog = rtnl_dereference(np->queues[0].xdp_prog);
1584 bpf_prog_add(prog, dev->real_num_tx_queues);
1586 for (i = 0; i < dev->real_num_tx_queues; ++i)
1587 rcu_assign_pointer(np->queues[i].xdp_prog, prog);
1590 for (i = 0; i < dev->real_num_tx_queues; ++i)
1591 bpf_prog_put(old_prog);
1593 xenbus_switch_state(np->xbdev, XenbusStateConnected);
1598 static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1600 struct netfront_info *np = netdev_priv(dev);
1605 switch (xdp->command) {
1606 case XDP_SETUP_PROG:
1607 return xennet_xdp_set(dev, xdp->prog, xdp->extack);
1613 static const struct net_device_ops xennet_netdev_ops = {
1614 .ndo_open = xennet_open,
1615 .ndo_stop = xennet_close,
1616 .ndo_start_xmit = xennet_start_xmit,
1617 .ndo_change_mtu = xennet_change_mtu,
1618 .ndo_get_stats64 = xennet_get_stats64,
1619 .ndo_set_mac_address = eth_mac_addr,
1620 .ndo_validate_addr = eth_validate_addr,
1621 .ndo_fix_features = xennet_fix_features,
1622 .ndo_set_features = xennet_set_features,
1623 .ndo_select_queue = xennet_select_queue,
1624 .ndo_bpf = xennet_xdp,
1625 .ndo_xdp_xmit = xennet_xdp_xmit,
1626 #ifdef CONFIG_NET_POLL_CONTROLLER
1627 .ndo_poll_controller = xennet_poll_controller,
1631 static void xennet_free_netdev(struct net_device *netdev)
1633 struct netfront_info *np = netdev_priv(netdev);
1635 free_percpu(np->rx_stats);
1636 free_percpu(np->tx_stats);
1637 free_netdev(netdev);
1640 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1643 struct net_device *netdev;
1644 struct netfront_info *np;
1646 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1648 return ERR_PTR(-ENOMEM);
1650 np = netdev_priv(netdev);
1656 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1657 if (np->rx_stats == NULL)
1659 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1660 if (np->tx_stats == NULL)
1663 netdev->netdev_ops = &xennet_netdev_ops;
1665 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1667 netdev->hw_features = NETIF_F_SG |
1669 NETIF_F_TSO | NETIF_F_TSO6;
1672 * Assume that all hw features are available for now. This set
1673 * will be adjusted by the call to netdev_update_features() in
1674 * xennet_connect() which is the earliest point where we can
1675 * negotiate with the backend regarding supported features.
1677 netdev->features |= netdev->hw_features;
1679 netdev->ethtool_ops = &xennet_ethtool_ops;
1680 netdev->min_mtu = ETH_MIN_MTU;
1681 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
1682 SET_NETDEV_DEV(netdev, &dev->dev);
1684 np->netdev = netdev;
1685 np->netfront_xdp_enabled = false;
1687 netif_carrier_off(netdev);
1690 xenbus_switch_state(dev, XenbusStateInitialising);
1691 err = wait_event_timeout(module_wq,
1692 xenbus_read_driver_state(dev->otherend) !=
1693 XenbusStateClosed &&
1694 xenbus_read_driver_state(dev->otherend) !=
1695 XenbusStateUnknown, XENNET_TIMEOUT);
1701 xennet_free_netdev(netdev);
1702 return ERR_PTR(err);
1706 * Entry point to this code when a new device is created. Allocate the basic
1707 * structures and the ring buffers for communication with the backend, and
1708 * inform the backend of the appropriate details for those.
1710 static int netfront_probe(struct xenbus_device *dev,
1711 const struct xenbus_device_id *id)
1714 struct net_device *netdev;
1715 struct netfront_info *info;
1717 netdev = xennet_create_dev(dev);
1718 if (IS_ERR(netdev)) {
1719 err = PTR_ERR(netdev);
1720 xenbus_dev_fatal(dev, err, "creating netdev");
1724 info = netdev_priv(netdev);
1725 dev_set_drvdata(&dev->dev, info);
1727 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1733 static void xennet_end_access(int ref, void *page)
1735 /* This frees the page as a side-effect */
1736 if (ref != GRANT_INVALID_REF)
1737 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1740 static void xennet_disconnect_backend(struct netfront_info *info)
1743 unsigned int num_queues = info->netdev->real_num_tx_queues;
1745 netif_carrier_off(info->netdev);
1747 for (i = 0; i < num_queues && info->queues; ++i) {
1748 struct netfront_queue *queue = &info->queues[i];
1750 del_timer_sync(&queue->rx_refill_timer);
1752 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1753 unbind_from_irqhandler(queue->tx_irq, queue);
1754 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1755 unbind_from_irqhandler(queue->tx_irq, queue);
1756 unbind_from_irqhandler(queue->rx_irq, queue);
1758 queue->tx_evtchn = queue->rx_evtchn = 0;
1759 queue->tx_irq = queue->rx_irq = 0;
1761 if (netif_running(info->netdev))
1762 napi_synchronize(&queue->napi);
1764 xennet_release_tx_bufs(queue);
1765 xennet_release_rx_bufs(queue);
1766 gnttab_free_grant_references(queue->gref_tx_head);
1767 gnttab_free_grant_references(queue->gref_rx_head);
1769 /* End access and free the pages */
1770 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1771 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1773 queue->tx_ring_ref = GRANT_INVALID_REF;
1774 queue->rx_ring_ref = GRANT_INVALID_REF;
1775 queue->tx.sring = NULL;
1776 queue->rx.sring = NULL;
1778 page_pool_destroy(queue->page_pool);
1783 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1784 * driver restart. We tear down our netif structure and recreate it, but
1785 * leave the device-layer structures intact so that this is transparent to the
1786 * rest of the kernel.
1788 static int netfront_resume(struct xenbus_device *dev)
1790 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1792 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1794 netif_tx_lock_bh(info->netdev);
1795 netif_device_detach(info->netdev);
1796 netif_tx_unlock_bh(info->netdev);
1798 xennet_disconnect_backend(info);
1802 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1804 char *s, *e, *macstr;
1807 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1809 return PTR_ERR(macstr);
1811 for (i = 0; i < ETH_ALEN; i++) {
1812 mac[i] = simple_strtoul(s, &e, 16);
1813 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1824 static int setup_netfront_single(struct netfront_queue *queue)
1828 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1832 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1833 xennet_interrupt, 0,
1834 queue->info->netdev->name,
1838 queue->rx_evtchn = queue->tx_evtchn;
1839 queue->rx_irq = queue->tx_irq = err;
1844 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1845 queue->tx_evtchn = 0;
1850 static int setup_netfront_split(struct netfront_queue *queue)
1854 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1857 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1859 goto alloc_rx_evtchn_fail;
1861 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1862 "%s-tx", queue->name);
1863 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1864 xennet_tx_interrupt, 0,
1865 queue->tx_irq_name, queue);
1868 queue->tx_irq = err;
1870 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1871 "%s-rx", queue->name);
1872 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1873 xennet_rx_interrupt, 0,
1874 queue->rx_irq_name, queue);
1877 queue->rx_irq = err;
1882 unbind_from_irqhandler(queue->tx_irq, queue);
1885 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1886 queue->rx_evtchn = 0;
1887 alloc_rx_evtchn_fail:
1888 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1889 queue->tx_evtchn = 0;
1894 static int setup_netfront(struct xenbus_device *dev,
1895 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1897 struct xen_netif_tx_sring *txs;
1898 struct xen_netif_rx_sring *rxs;
1902 queue->tx_ring_ref = GRANT_INVALID_REF;
1903 queue->rx_ring_ref = GRANT_INVALID_REF;
1904 queue->rx.sring = NULL;
1905 queue->tx.sring = NULL;
1907 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1910 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1913 SHARED_RING_INIT(txs);
1914 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1916 err = xenbus_grant_ring(dev, txs, 1, &gref);
1918 goto grant_tx_ring_fail;
1919 queue->tx_ring_ref = gref;
1921 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1924 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1925 goto alloc_rx_ring_fail;
1927 SHARED_RING_INIT(rxs);
1928 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1930 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1932 goto grant_rx_ring_fail;
1933 queue->rx_ring_ref = gref;
1935 if (feature_split_evtchn)
1936 err = setup_netfront_split(queue);
1937 /* setup single event channel if
1938 * a) feature-split-event-channels == 0
1939 * b) feature-split-event-channels == 1 but failed to setup
1941 if (!feature_split_evtchn || err)
1942 err = setup_netfront_single(queue);
1945 goto alloc_evtchn_fail;
1949 /* If we fail to setup netfront, it is safe to just revoke access to
1950 * granted pages because backend is not accessing it at this point.
1953 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
1955 free_page((unsigned long)rxs);
1957 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
1959 free_page((unsigned long)txs);
1964 /* Queue-specific initialisation
1965 * This used to be done in xennet_create_dev() but must now
1968 static int xennet_init_queue(struct netfront_queue *queue)
1974 spin_lock_init(&queue->tx_lock);
1975 spin_lock_init(&queue->rx_lock);
1976 spin_lock_init(&queue->rx_cons_lock);
1978 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
1980 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
1981 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
1984 /* Initialise tx_skb_freelist as a free chain containing every entry. */
1985 queue->tx_skb_freelist = 0;
1986 queue->tx_pend_queue = TX_LINK_NONE;
1987 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1988 queue->tx_link[i] = i + 1;
1989 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1990 queue->grant_tx_page[i] = NULL;
1992 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
1994 /* Clear out rx_skbs */
1995 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1996 queue->rx_skbs[i] = NULL;
1997 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
2000 /* A grant for every tx ring slot */
2001 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
2002 &queue->gref_tx_head) < 0) {
2003 pr_alert("can't alloc tx grant refs\n");
2008 /* A grant for every rx ring slot */
2009 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
2010 &queue->gref_rx_head) < 0) {
2011 pr_alert("can't alloc rx grant refs\n");
2019 gnttab_free_grant_references(queue->gref_tx_head);
2024 static int write_queue_xenstore_keys(struct netfront_queue *queue,
2025 struct xenbus_transaction *xbt, int write_hierarchical)
2027 /* Write the queue-specific keys into XenStore in the traditional
2028 * way for a single queue, or in a queue subkeys for multiple
2031 struct xenbus_device *dev = queue->info->xbdev;
2033 const char *message;
2037 /* Choose the correct place to write the keys */
2038 if (write_hierarchical) {
2039 pathsize = strlen(dev->nodename) + 10;
2040 path = kzalloc(pathsize, GFP_KERNEL);
2043 message = "out of memory while writing ring references";
2046 snprintf(path, pathsize, "%s/queue-%u",
2047 dev->nodename, queue->id);
2049 path = (char *)dev->nodename;
2052 /* Write ring references */
2053 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
2054 queue->tx_ring_ref);
2056 message = "writing tx-ring-ref";
2060 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
2061 queue->rx_ring_ref);
2063 message = "writing rx-ring-ref";
2067 /* Write event channels; taking into account both shared
2068 * and split event channel scenarios.
2070 if (queue->tx_evtchn == queue->rx_evtchn) {
2071 /* Shared event channel */
2072 err = xenbus_printf(*xbt, path,
2073 "event-channel", "%u", queue->tx_evtchn);
2075 message = "writing event-channel";
2079 /* Split event channels */
2080 err = xenbus_printf(*xbt, path,
2081 "event-channel-tx", "%u", queue->tx_evtchn);
2083 message = "writing event-channel-tx";
2087 err = xenbus_printf(*xbt, path,
2088 "event-channel-rx", "%u", queue->rx_evtchn);
2090 message = "writing event-channel-rx";
2095 if (write_hierarchical)
2100 if (write_hierarchical)
2102 xenbus_dev_fatal(dev, err, "%s", message);
2106 static void xennet_destroy_queues(struct netfront_info *info)
2110 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
2111 struct netfront_queue *queue = &info->queues[i];
2113 if (netif_running(info->netdev))
2114 napi_disable(&queue->napi);
2115 netif_napi_del(&queue->napi);
2118 kfree(info->queues);
2119 info->queues = NULL;
2124 static int xennet_create_page_pool(struct netfront_queue *queue)
2127 struct page_pool_params pp_params = {
2130 .pool_size = NET_RX_RING_SIZE,
2131 .nid = NUMA_NO_NODE,
2132 .dev = &queue->info->netdev->dev,
2133 .offset = XDP_PACKET_HEADROOM,
2134 .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
2137 queue->page_pool = page_pool_create(&pp_params);
2138 if (IS_ERR(queue->page_pool)) {
2139 err = PTR_ERR(queue->page_pool);
2140 queue->page_pool = NULL;
2144 err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
2147 netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
2151 err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
2152 MEM_TYPE_PAGE_POOL, queue->page_pool);
2154 netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
2155 goto err_unregister_rxq;
2160 xdp_rxq_info_unreg(&queue->xdp_rxq);
2162 page_pool_destroy(queue->page_pool);
2163 queue->page_pool = NULL;
2167 static int xennet_create_queues(struct netfront_info *info,
2168 unsigned int *num_queues)
2173 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
2178 for (i = 0; i < *num_queues; i++) {
2179 struct netfront_queue *queue = &info->queues[i];
2184 ret = xennet_init_queue(queue);
2186 dev_warn(&info->xbdev->dev,
2187 "only created %d queues\n", i);
2192 /* use page pool recycling instead of buddy allocator */
2193 ret = xennet_create_page_pool(queue);
2195 dev_err(&info->xbdev->dev, "can't allocate page pool\n");
2200 netif_napi_add(queue->info->netdev, &queue->napi,
2202 if (netif_running(info->netdev))
2203 napi_enable(&queue->napi);
2206 netif_set_real_num_tx_queues(info->netdev, *num_queues);
2208 if (*num_queues == 0) {
2209 dev_err(&info->xbdev->dev, "no queues\n");
2215 /* Common code used when first setting up, and when resuming. */
2216 static int talk_to_netback(struct xenbus_device *dev,
2217 struct netfront_info *info)
2219 const char *message;
2220 struct xenbus_transaction xbt;
2222 unsigned int feature_split_evtchn;
2224 unsigned int max_queues = 0;
2225 struct netfront_queue *queue = NULL;
2226 unsigned int num_queues = 1;
2229 info->netdev->irq = 0;
2231 /* Check if backend supports multiple queues */
2232 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2233 "multi-queue-max-queues", 1);
2234 num_queues = min(max_queues, xennet_max_queues);
2236 /* Check feature-split-event-channels */
2237 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2238 "feature-split-event-channels", 0);
2240 /* Read mac addr. */
2241 err = xen_net_read_mac(dev, addr);
2243 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
2246 eth_hw_addr_set(info->netdev, addr);
2248 info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
2249 "feature-xdp-headroom", 0);
2250 if (info->netback_has_xdp_headroom) {
2251 /* set the current xen-netfront xdp state */
2252 err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
2253 NETBACK_XDP_HEADROOM_ENABLE :
2254 NETBACK_XDP_HEADROOM_DISABLE);
2261 xennet_destroy_queues(info);
2263 /* For the case of a reconnect reset the "broken" indicator. */
2264 info->broken = false;
2266 err = xennet_create_queues(info, &num_queues);
2268 xenbus_dev_fatal(dev, err, "creating queues");
2269 kfree(info->queues);
2270 info->queues = NULL;
2275 /* Create shared ring, alloc event channel -- for each queue */
2276 for (i = 0; i < num_queues; ++i) {
2277 queue = &info->queues[i];
2278 err = setup_netfront(dev, queue, feature_split_evtchn);
2284 err = xenbus_transaction_start(&xbt);
2286 xenbus_dev_fatal(dev, err, "starting transaction");
2290 if (xenbus_exists(XBT_NIL,
2291 info->xbdev->otherend, "multi-queue-max-queues")) {
2292 /* Write the number of queues */
2293 err = xenbus_printf(xbt, dev->nodename,
2294 "multi-queue-num-queues", "%u", num_queues);
2296 message = "writing multi-queue-num-queues";
2297 goto abort_transaction_no_dev_fatal;
2301 if (num_queues == 1) {
2302 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2304 goto abort_transaction_no_dev_fatal;
2306 /* Write the keys for each queue */
2307 for (i = 0; i < num_queues; ++i) {
2308 queue = &info->queues[i];
2309 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2311 goto abort_transaction_no_dev_fatal;
2315 /* The remaining keys are not queue-specific */
2316 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2319 message = "writing request-rx-copy";
2320 goto abort_transaction;
2323 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2325 message = "writing feature-rx-notify";
2326 goto abort_transaction;
2329 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2331 message = "writing feature-sg";
2332 goto abort_transaction;
2335 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2337 message = "writing feature-gso-tcpv4";
2338 goto abort_transaction;
2341 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2343 message = "writing feature-gso-tcpv6";
2344 goto abort_transaction;
2347 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2350 message = "writing feature-ipv6-csum-offload";
2351 goto abort_transaction;
2354 err = xenbus_transaction_end(xbt, 0);
2358 xenbus_dev_fatal(dev, err, "completing transaction");
2365 xenbus_dev_fatal(dev, err, "%s", message);
2366 abort_transaction_no_dev_fatal:
2367 xenbus_transaction_end(xbt, 1);
2369 xennet_disconnect_backend(info);
2371 xennet_destroy_queues(info);
2375 device_unregister(&dev->dev);
2379 static int xennet_connect(struct net_device *dev)
2381 struct netfront_info *np = netdev_priv(dev);
2382 unsigned int num_queues = 0;
2385 struct netfront_queue *queue = NULL;
2387 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
2389 "backend does not support copying receive path\n");
2393 err = talk_to_netback(np->xbdev, np);
2396 if (np->netback_has_xdp_headroom)
2397 pr_info("backend supports XDP headroom\n");
2399 /* talk_to_netback() sets the correct number of queues */
2400 num_queues = dev->real_num_tx_queues;
2402 if (dev->reg_state == NETREG_UNINITIALIZED) {
2403 err = register_netdev(dev);
2405 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2406 device_unregister(&np->xbdev->dev);
2412 netdev_update_features(dev);
2416 * All public and private state should now be sane. Get
2417 * ready to start sending and receiving packets and give the driver
2418 * domain a kick because we've probably just requeued some
2421 netif_tx_lock_bh(np->netdev);
2422 netif_device_attach(np->netdev);
2423 netif_tx_unlock_bh(np->netdev);
2425 netif_carrier_on(np->netdev);
2426 for (j = 0; j < num_queues; ++j) {
2427 queue = &np->queues[j];
2429 notify_remote_via_irq(queue->tx_irq);
2430 if (queue->tx_irq != queue->rx_irq)
2431 notify_remote_via_irq(queue->rx_irq);
2433 spin_lock_irq(&queue->tx_lock);
2434 xennet_tx_buf_gc(queue);
2435 spin_unlock_irq(&queue->tx_lock);
2437 spin_lock_bh(&queue->rx_lock);
2438 xennet_alloc_rx_buffers(queue);
2439 spin_unlock_bh(&queue->rx_lock);
2446 * Callback received when the backend's state changes.
2448 static void netback_changed(struct xenbus_device *dev,
2449 enum xenbus_state backend_state)
2451 struct netfront_info *np = dev_get_drvdata(&dev->dev);
2452 struct net_device *netdev = np->netdev;
2454 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2456 wake_up_all(&module_wq);
2458 switch (backend_state) {
2459 case XenbusStateInitialising:
2460 case XenbusStateInitialised:
2461 case XenbusStateReconfiguring:
2462 case XenbusStateReconfigured:
2463 case XenbusStateUnknown:
2466 case XenbusStateInitWait:
2467 if (dev->state != XenbusStateInitialising)
2469 if (xennet_connect(netdev) != 0)
2471 xenbus_switch_state(dev, XenbusStateConnected);
2474 case XenbusStateConnected:
2475 netdev_notify_peers(netdev);
2478 case XenbusStateClosed:
2479 if (dev->state == XenbusStateClosed)
2481 fallthrough; /* Missed the backend's CLOSING state */
2482 case XenbusStateClosing:
2483 xenbus_frontend_closed(dev);
2488 static const struct xennet_stat {
2489 char name[ETH_GSTRING_LEN];
2491 } xennet_stats[] = {
2493 "rx_gso_checksum_fixup",
2494 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2498 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2500 switch (string_set) {
2502 return ARRAY_SIZE(xennet_stats);
2508 static void xennet_get_ethtool_stats(struct net_device *dev,
2509 struct ethtool_stats *stats, u64 * data)
2511 void *np = netdev_priv(dev);
2514 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2515 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2518 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2522 switch (stringset) {
2524 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2525 memcpy(data + i * ETH_GSTRING_LEN,
2526 xennet_stats[i].name, ETH_GSTRING_LEN);
2531 static const struct ethtool_ops xennet_ethtool_ops =
2533 .get_link = ethtool_op_get_link,
2535 .get_sset_count = xennet_get_sset_count,
2536 .get_ethtool_stats = xennet_get_ethtool_stats,
2537 .get_strings = xennet_get_strings,
2538 .get_ts_info = ethtool_op_get_ts_info,
2542 static ssize_t show_rxbuf(struct device *dev,
2543 struct device_attribute *attr, char *buf)
2545 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2548 static ssize_t store_rxbuf(struct device *dev,
2549 struct device_attribute *attr,
2550 const char *buf, size_t len)
2554 if (!capable(CAP_NET_ADMIN))
2557 simple_strtoul(buf, &endp, 0);
2561 /* rxbuf_min and rxbuf_max are no longer configurable. */
2566 static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2567 static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2568 static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
2570 static struct attribute *xennet_dev_attrs[] = {
2571 &dev_attr_rxbuf_min.attr,
2572 &dev_attr_rxbuf_max.attr,
2573 &dev_attr_rxbuf_cur.attr,
2577 static const struct attribute_group xennet_dev_group = {
2578 .attrs = xennet_dev_attrs
2580 #endif /* CONFIG_SYSFS */
2582 static void xennet_bus_close(struct xenbus_device *dev)
2586 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2589 xenbus_switch_state(dev, XenbusStateClosing);
2590 ret = wait_event_timeout(module_wq,
2591 xenbus_read_driver_state(dev->otherend) ==
2592 XenbusStateClosing ||
2593 xenbus_read_driver_state(dev->otherend) ==
2594 XenbusStateClosed ||
2595 xenbus_read_driver_state(dev->otherend) ==
2600 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2604 xenbus_switch_state(dev, XenbusStateClosed);
2605 ret = wait_event_timeout(module_wq,
2606 xenbus_read_driver_state(dev->otherend) ==
2607 XenbusStateClosed ||
2608 xenbus_read_driver_state(dev->otherend) ==
2614 static int xennet_remove(struct xenbus_device *dev)
2616 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2618 xennet_bus_close(dev);
2619 xennet_disconnect_backend(info);
2621 if (info->netdev->reg_state == NETREG_REGISTERED)
2622 unregister_netdev(info->netdev);
2626 xennet_destroy_queues(info);
2629 xennet_free_netdev(info->netdev);
2634 static const struct xenbus_device_id netfront_ids[] = {
2639 static struct xenbus_driver netfront_driver = {
2640 .ids = netfront_ids,
2641 .probe = netfront_probe,
2642 .remove = xennet_remove,
2643 .resume = netfront_resume,
2644 .otherend_changed = netback_changed,
2647 static int __init netif_init(void)
2652 if (!xen_has_pv_nic_devices())
2655 pr_info("Initialising Xen virtual ethernet driver\n");
2657 /* Allow as many queues as there are CPUs inut max. 8 if user has not
2658 * specified a value.
2660 if (xennet_max_queues == 0)
2661 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2664 return xenbus_register_frontend(&netfront_driver);
2666 module_init(netif_init);
2669 static void __exit netif_exit(void)
2671 xenbus_unregister_driver(&netfront_driver);
2673 module_exit(netif_exit);
2675 MODULE_DESCRIPTION("Xen virtual network device frontend");
2676 MODULE_LICENSE("GPL");
2677 MODULE_ALIAS("xen:vif");
2678 MODULE_ALIAS("xennet");