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>
49 #include <xen/xenbus.h>
50 #include <xen/events.h>
52 #include <xen/platform_pci.h>
53 #include <xen/grant_table.h>
55 #include <xen/interface/io/netif.h>
56 #include <xen/interface/memory.h>
57 #include <xen/interface/grant_table.h>
59 /* Module parameters */
60 static unsigned int xennet_max_queues;
61 module_param_named(max_queues, xennet_max_queues, uint, 0644);
62 MODULE_PARM_DESC(max_queues,
63 "Maximum number of queues per virtual interface");
65 static bool __read_mostly xennet_trusted = true;
66 module_param_named(trusted, xennet_trusted, bool, 0644);
67 MODULE_PARM_DESC(trusted, "Is the backend trusted");
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 napi_struct napi;
112 /* Split event channels support, tx_* == rx_* when using
113 * single event channel.
115 unsigned int tx_evtchn, rx_evtchn;
116 unsigned int tx_irq, rx_irq;
117 /* Only used when split event channels support is enabled */
118 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
119 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
122 struct xen_netif_tx_front_ring tx;
126 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
127 * are linked from tx_skb_freelist through tx_link.
129 struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
130 unsigned short tx_link[NET_TX_RING_SIZE];
131 #define TX_LINK_NONE 0xffff
132 #define TX_PENDING 0xfffe
133 grant_ref_t gref_tx_head;
134 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
135 struct page *grant_tx_page[NET_TX_RING_SIZE];
136 unsigned tx_skb_freelist;
137 unsigned int tx_pend_queue;
139 spinlock_t rx_lock ____cacheline_aligned_in_smp;
140 struct xen_netif_rx_front_ring rx;
143 struct timer_list rx_refill_timer;
145 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
146 grant_ref_t gref_rx_head;
147 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
149 unsigned int rx_rsp_unconsumed;
150 spinlock_t rx_cons_lock;
153 struct netfront_info {
154 struct list_head list;
155 struct net_device *netdev;
157 struct xenbus_device *xbdev;
159 /* Multi-queue support */
160 struct netfront_queue *queues;
163 struct netfront_stats __percpu *rx_stats;
164 struct netfront_stats __percpu *tx_stats;
166 /* Is device behaving sane? */
169 /* Should skbs be bounced into a zeroed buffer? */
172 atomic_t rx_gso_checksum_fixup;
175 struct netfront_rx_info {
176 struct xen_netif_rx_response rx;
177 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
181 * Access macros for acquiring freeing slots in tx_skbs[].
184 static void add_id_to_list(unsigned *head, unsigned short *list,
191 static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
193 unsigned int id = *head;
195 if (id != TX_LINK_NONE) {
197 list[id] = TX_LINK_NONE;
202 static int xennet_rxidx(RING_IDX idx)
204 return idx & (NET_RX_RING_SIZE - 1);
207 static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
210 int i = xennet_rxidx(ri);
211 struct sk_buff *skb = queue->rx_skbs[i];
212 queue->rx_skbs[i] = NULL;
216 static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
219 int i = xennet_rxidx(ri);
220 grant_ref_t ref = queue->grant_rx_ref[i];
221 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
226 static const struct attribute_group xennet_dev_group;
229 static bool xennet_can_sg(struct net_device *dev)
231 return dev->features & NETIF_F_SG;
235 static void rx_refill_timeout(unsigned long data)
237 struct netfront_queue *queue = (struct netfront_queue *)data;
238 napi_schedule(&queue->napi);
241 static int netfront_tx_slot_available(struct netfront_queue *queue)
243 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
244 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
247 static void xennet_maybe_wake_tx(struct netfront_queue *queue)
249 struct net_device *dev = queue->info->netdev;
250 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
252 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
253 netfront_tx_slot_available(queue) &&
254 likely(netif_running(dev)))
255 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
259 static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
264 skb = __netdev_alloc_skb(queue->info->netdev,
265 RX_COPY_THRESHOLD + NET_IP_ALIGN,
266 GFP_ATOMIC | __GFP_NOWARN);
270 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN | __GFP_ZERO);
275 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
277 /* Align ip header to a 16 bytes boundary */
278 skb_reserve(skb, NET_IP_ALIGN);
279 skb->dev = queue->info->netdev;
285 static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
287 RING_IDX req_prod = queue->rx.req_prod_pvt;
291 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
294 for (req_prod = queue->rx.req_prod_pvt;
295 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
301 struct xen_netif_rx_request *req;
303 skb = xennet_alloc_one_rx_buffer(queue);
309 id = xennet_rxidx(req_prod);
311 BUG_ON(queue->rx_skbs[id]);
312 queue->rx_skbs[id] = skb;
314 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
315 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
316 queue->grant_rx_ref[id] = ref;
318 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
320 req = RING_GET_REQUEST(&queue->rx, req_prod);
321 gnttab_page_grant_foreign_access_ref_one(ref,
322 queue->info->xbdev->otherend_id,
329 queue->rx.req_prod_pvt = req_prod;
331 /* Try again later if there are not enough requests or skb allocation
333 * Enough requests is quantified as the sum of newly created slots and
334 * the unconsumed slots at the backend.
336 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
338 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
342 wmb(); /* barrier so backend seens requests */
344 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
346 notify_remote_via_irq(queue->rx_irq);
349 static int xennet_open(struct net_device *dev)
351 struct netfront_info *np = netdev_priv(dev);
352 unsigned int num_queues = dev->real_num_tx_queues;
354 struct netfront_queue *queue = NULL;
356 if (!np->queues || np->broken)
359 for (i = 0; i < num_queues; ++i) {
360 queue = &np->queues[i];
361 napi_enable(&queue->napi);
363 spin_lock_bh(&queue->rx_lock);
364 if (netif_carrier_ok(dev)) {
365 xennet_alloc_rx_buffers(queue);
366 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
367 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
368 napi_schedule(&queue->napi);
370 spin_unlock_bh(&queue->rx_lock);
373 netif_tx_start_all_queues(dev);
378 static bool xennet_tx_buf_gc(struct netfront_queue *queue)
384 bool work_done = false;
385 const struct device *dev = &queue->info->netdev->dev;
387 BUG_ON(!netif_carrier_ok(queue->info->netdev));
390 prod = queue->tx.sring->rsp_prod;
391 if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
392 dev_alert(dev, "Illegal number of responses %u\n",
393 prod - queue->tx.rsp_cons);
396 rmb(); /* Ensure we see responses up to 'rp'. */
398 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
399 struct xen_netif_tx_response txrsp;
403 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
404 if (txrsp.status == XEN_NETIF_RSP_NULL)
408 if (id >= RING_SIZE(&queue->tx)) {
410 "Response has incorrect id (%u)\n",
414 if (queue->tx_link[id] != TX_PENDING) {
416 "Response for inactive request\n");
420 queue->tx_link[id] = TX_LINK_NONE;
421 skb = queue->tx_skbs[id];
422 queue->tx_skbs[id] = NULL;
423 if (unlikely(!gnttab_end_foreign_access_ref(
424 queue->grant_tx_ref[id], GNTMAP_readonly))) {
426 "Grant still in use by backend domain\n");
429 gnttab_release_grant_reference(
430 &queue->gref_tx_head, queue->grant_tx_ref[id]);
431 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
432 queue->grant_tx_page[id] = NULL;
433 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
434 dev_kfree_skb_irq(skb);
437 queue->tx.rsp_cons = prod;
439 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
440 } while (more_to_do);
442 xennet_maybe_wake_tx(queue);
447 queue->info->broken = true;
448 dev_alert(dev, "Disabled for further use\n");
453 struct xennet_gnttab_make_txreq {
454 struct netfront_queue *queue;
457 struct xen_netif_tx_request *tx; /* Last request on ring page */
458 struct xen_netif_tx_request tx_local; /* Last request local copy*/
462 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
463 unsigned int len, void *data)
465 struct xennet_gnttab_make_txreq *info = data;
467 struct xen_netif_tx_request *tx;
469 /* convenient aliases */
470 struct page *page = info->page;
471 struct netfront_queue *queue = info->queue;
472 struct sk_buff *skb = info->skb;
474 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
475 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
476 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
477 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
479 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
480 gfn, GNTMAP_readonly);
482 queue->tx_skbs[id] = skb;
483 queue->grant_tx_page[id] = page;
484 queue->grant_tx_ref[id] = ref;
486 info->tx_local.id = id;
487 info->tx_local.gref = ref;
488 info->tx_local.offset = offset;
489 info->tx_local.size = len;
490 info->tx_local.flags = 0;
492 *tx = info->tx_local;
495 * Put the request in the pending queue, it will be set to be pending
496 * when the producer index is about to be raised.
498 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
501 info->size += info->tx_local.size;
504 static struct xen_netif_tx_request *xennet_make_first_txreq(
505 struct xennet_gnttab_make_txreq *info,
506 unsigned int offset, unsigned int len)
510 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
515 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
516 unsigned int len, void *data)
518 struct xennet_gnttab_make_txreq *info = data;
520 info->tx->flags |= XEN_NETTXF_more_data;
522 xennet_tx_setup_grant(gfn, offset, len, data);
525 static void xennet_make_txreqs(
526 struct xennet_gnttab_make_txreq *info,
528 unsigned int offset, unsigned int len)
530 /* Skip unused frames from start of page */
531 page += offset >> PAGE_SHIFT;
532 offset &= ~PAGE_MASK;
538 gnttab_foreach_grant_in_range(page, offset, len,
539 xennet_make_one_txreq,
549 * Count how many ring slots are required to send this skb. Each frag
550 * might be a compound page.
552 static int xennet_count_skb_slots(struct sk_buff *skb)
554 int i, frags = skb_shinfo(skb)->nr_frags;
557 slots = gnttab_count_grant(offset_in_page(skb->data),
560 for (i = 0; i < frags; i++) {
561 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
562 unsigned long size = skb_frag_size(frag);
563 unsigned long offset = frag->page_offset;
565 /* Skip unused frames from start of page */
566 offset &= ~PAGE_MASK;
568 slots += gnttab_count_grant(offset, size);
574 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
575 void *accel_priv, select_queue_fallback_t fallback)
577 unsigned int num_queues = dev->real_num_tx_queues;
581 /* First, check if there is only one queue */
582 if (num_queues == 1) {
585 hash = skb_get_hash(skb);
586 queue_idx = hash % num_queues;
592 static void xennet_mark_tx_pending(struct netfront_queue *queue)
596 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
598 queue->tx_link[i] = TX_PENDING;
601 struct sk_buff *bounce_skb(const struct sk_buff *skb)
603 unsigned int headerlen = skb_headroom(skb);
604 /* Align size to allocate full pages and avoid contiguous data leaks */
605 unsigned int size = ALIGN(skb_end_offset(skb) + skb->data_len,
607 struct sk_buff *n = alloc_skb(size, GFP_ATOMIC | __GFP_ZERO);
612 if (!IS_ALIGNED((uintptr_t)n->head, XEN_PAGE_SIZE)) {
613 WARN_ONCE(1, "misaligned skb allocated\n");
618 /* Set the data pointer */
619 skb_reserve(n, headerlen);
620 /* Set the tail pointer and length */
621 skb_put(n, skb->len);
623 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
625 skb_copy_header(n, skb);
629 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
631 static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
633 struct netfront_info *np = netdev_priv(dev);
634 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
635 struct xen_netif_tx_request *first_tx;
643 struct netfront_queue *queue = NULL;
644 struct xennet_gnttab_make_txreq info = { };
645 unsigned int num_queues = dev->real_num_tx_queues;
647 struct sk_buff *nskb;
649 /* Drop the packet if no queues are set up */
652 if (unlikely(np->broken))
654 /* Determine which queue to transmit this SKB on */
655 queue_index = skb_get_queue_mapping(skb);
656 queue = &np->queues[queue_index];
658 /* If skb->len is too big for wire format, drop skb and alert
659 * user about misconfiguration.
661 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
662 net_alert_ratelimited(
663 "xennet: skb->len = %u, too big for wire format\n",
668 slots = xennet_count_skb_slots(skb);
669 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
670 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
672 if (skb_linearize(skb))
676 page = virt_to_page(skb->data);
677 offset = offset_in_page(skb->data);
679 /* The first req should be at least ETH_HLEN size or the packet will be
680 * dropped by netback.
682 * If the backend is not trusted bounce all data to zeroed pages to
683 * avoid exposing contiguous data on the granted page not belonging to
686 if (np->bounce || unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
687 nskb = bounce_skb(skb);
690 dev_kfree_skb_any(skb);
692 page = virt_to_page(skb->data);
693 offset = offset_in_page(skb->data);
696 len = skb_headlen(skb);
698 spin_lock_irqsave(&queue->tx_lock, flags);
700 if (unlikely(!netif_carrier_ok(dev) ||
701 (slots > 1 && !xennet_can_sg(dev)) ||
702 netif_needs_gso(skb, netif_skb_features(skb)))) {
703 spin_unlock_irqrestore(&queue->tx_lock, flags);
707 /* First request for the linear area. */
711 first_tx = xennet_make_first_txreq(&info, offset, len);
712 offset += info.tx_local.size;
713 if (offset == PAGE_SIZE) {
717 len -= info.tx_local.size;
719 if (skb->ip_summed == CHECKSUM_PARTIAL)
721 first_tx->flags |= XEN_NETTXF_csum_blank |
722 XEN_NETTXF_data_validated;
723 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
724 /* remote but checksummed. */
725 first_tx->flags |= XEN_NETTXF_data_validated;
727 /* Optional extra info after the first request. */
728 if (skb_shinfo(skb)->gso_size) {
729 struct xen_netif_extra_info *gso;
731 gso = (struct xen_netif_extra_info *)
732 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
734 first_tx->flags |= XEN_NETTXF_extra_info;
736 gso->u.gso.size = skb_shinfo(skb)->gso_size;
737 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
738 XEN_NETIF_GSO_TYPE_TCPV6 :
739 XEN_NETIF_GSO_TYPE_TCPV4;
741 gso->u.gso.features = 0;
743 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
747 /* Requests for the rest of the linear area. */
748 xennet_make_txreqs(&info, page, offset, len);
750 /* Requests for all the frags. */
751 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
752 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
753 xennet_make_txreqs(&info, skb_frag_page(frag),
755 skb_frag_size(frag));
758 /* First request has the packet length. */
759 first_tx->size = skb->len;
761 xennet_mark_tx_pending(queue);
763 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
765 notify_remote_via_irq(queue->tx_irq);
767 u64_stats_update_begin(&tx_stats->syncp);
768 tx_stats->bytes += skb->len;
770 u64_stats_update_end(&tx_stats->syncp);
772 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
773 xennet_tx_buf_gc(queue);
775 if (!netfront_tx_slot_available(queue))
776 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
778 spin_unlock_irqrestore(&queue->tx_lock, flags);
783 dev->stats.tx_dropped++;
784 dev_kfree_skb_any(skb);
788 static int xennet_close(struct net_device *dev)
790 struct netfront_info *np = netdev_priv(dev);
791 unsigned int num_queues = dev->real_num_tx_queues;
793 struct netfront_queue *queue;
794 netif_tx_stop_all_queues(np->netdev);
795 for (i = 0; i < num_queues; ++i) {
796 queue = &np->queues[i];
797 napi_disable(&queue->napi);
802 static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
806 spin_lock_irqsave(&queue->rx_cons_lock, flags);
807 queue->rx.rsp_cons = val;
808 queue->rx_rsp_unconsumed = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
809 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
812 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
815 int new = xennet_rxidx(queue->rx.req_prod_pvt);
817 BUG_ON(queue->rx_skbs[new]);
818 queue->rx_skbs[new] = skb;
819 queue->grant_rx_ref[new] = ref;
820 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
821 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
822 queue->rx.req_prod_pvt++;
825 static int xennet_get_extras(struct netfront_queue *queue,
826 struct xen_netif_extra_info *extras,
830 struct xen_netif_extra_info extra;
831 struct device *dev = &queue->info->netdev->dev;
832 RING_IDX cons = queue->rx.rsp_cons;
839 if (unlikely(cons + 1 == rp)) {
841 dev_warn(dev, "Missing extra info\n");
846 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
848 if (unlikely(!extra.type ||
849 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
851 dev_warn(dev, "Invalid extra type: %d\n",
855 extras[extra.type - 1] = extra;
858 skb = xennet_get_rx_skb(queue, cons);
859 ref = xennet_get_rx_ref(queue, cons);
860 xennet_move_rx_slot(queue, skb, ref);
861 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
863 xennet_set_rx_rsp_cons(queue, cons);
867 static int xennet_get_responses(struct netfront_queue *queue,
868 struct netfront_rx_info *rinfo, RING_IDX rp,
869 struct sk_buff_head *list)
871 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
872 struct xen_netif_extra_info *extras = rinfo->extras;
873 struct device *dev = &queue->info->netdev->dev;
874 RING_IDX cons = queue->rx.rsp_cons;
875 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
876 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
877 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
881 if (rx->flags & XEN_NETRXF_extra_info) {
882 err = xennet_get_extras(queue, extras, rp);
883 cons = queue->rx.rsp_cons;
887 if (unlikely(rx->status < 0 ||
888 rx->offset + rx->status > XEN_PAGE_SIZE)) {
890 dev_warn(dev, "rx->offset: %u, size: %d\n",
891 rx->offset, rx->status);
892 xennet_move_rx_slot(queue, skb, ref);
898 * This definitely indicates a bug, either in this driver or in
899 * the backend driver. In future this should flag the bad
900 * situation to the system controller to reboot the backend.
902 if (ref == GRANT_INVALID_REF) {
904 dev_warn(dev, "Bad rx response id %d.\n",
910 if (!gnttab_end_foreign_access_ref(ref, 0)) {
912 "Grant still in use by backend domain\n");
913 queue->info->broken = true;
914 dev_alert(dev, "Disabled for further use\n");
918 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
920 __skb_queue_tail(list, skb);
923 if (!(rx->flags & XEN_NETRXF_more_data))
926 if (cons + slots == rp) {
928 dev_warn(dev, "Need more slots\n");
933 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
935 skb = xennet_get_rx_skb(queue, cons + slots);
936 ref = xennet_get_rx_ref(queue, cons + slots);
940 if (unlikely(slots > max)) {
942 dev_warn(dev, "Too many slots\n");
947 xennet_set_rx_rsp_cons(queue, cons + slots);
952 static int xennet_set_skb_gso(struct sk_buff *skb,
953 struct xen_netif_extra_info *gso)
955 if (!gso->u.gso.size) {
957 pr_warn("GSO size must not be zero\n");
961 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
962 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
964 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
968 skb_shinfo(skb)->gso_size = gso->u.gso.size;
969 skb_shinfo(skb)->gso_type =
970 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
974 /* Header must be checked, and gso_segs computed. */
975 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
976 skb_shinfo(skb)->gso_segs = 0;
981 static int xennet_fill_frags(struct netfront_queue *queue,
983 struct sk_buff_head *list)
985 RING_IDX cons = queue->rx.rsp_cons;
986 struct sk_buff *nskb;
988 while ((nskb = __skb_dequeue(list))) {
989 struct xen_netif_rx_response rx;
990 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
992 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
994 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
995 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
997 BUG_ON(pull_to < skb_headlen(skb));
998 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1000 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
1001 xennet_set_rx_rsp_cons(queue,
1002 ++cons + skb_queue_len(list));
1007 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1008 skb_frag_page(nfrag),
1009 rx.offset, rx.status, PAGE_SIZE);
1011 skb_shinfo(nskb)->nr_frags = 0;
1015 xennet_set_rx_rsp_cons(queue, cons);
1020 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
1022 bool recalculate_partial_csum = false;
1025 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1026 * peers can fail to set NETRXF_csum_blank when sending a GSO
1027 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1028 * recalculate the partial checksum.
1030 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1031 struct netfront_info *np = netdev_priv(dev);
1032 atomic_inc(&np->rx_gso_checksum_fixup);
1033 skb->ip_summed = CHECKSUM_PARTIAL;
1034 recalculate_partial_csum = true;
1037 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1038 if (skb->ip_summed != CHECKSUM_PARTIAL)
1041 return skb_checksum_setup(skb, recalculate_partial_csum);
1044 static int handle_incoming_queue(struct netfront_queue *queue,
1045 struct sk_buff_head *rxq)
1047 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
1048 int packets_dropped = 0;
1049 struct sk_buff *skb;
1051 while ((skb = __skb_dequeue(rxq)) != NULL) {
1052 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1054 if (pull_to > skb_headlen(skb))
1055 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1057 /* Ethernet work: Delayed to here as it peeks the header. */
1058 skb->protocol = eth_type_trans(skb, queue->info->netdev);
1059 skb_reset_network_header(skb);
1061 if (checksum_setup(queue->info->netdev, skb)) {
1064 queue->info->netdev->stats.rx_errors++;
1068 u64_stats_update_begin(&rx_stats->syncp);
1069 rx_stats->packets++;
1070 rx_stats->bytes += skb->len;
1071 u64_stats_update_end(&rx_stats->syncp);
1074 napi_gro_receive(&queue->napi, skb);
1077 return packets_dropped;
1080 static int xennet_poll(struct napi_struct *napi, int budget)
1082 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1083 struct net_device *dev = queue->info->netdev;
1084 struct sk_buff *skb;
1085 struct netfront_rx_info rinfo;
1086 struct xen_netif_rx_response *rx = &rinfo.rx;
1087 struct xen_netif_extra_info *extras = rinfo.extras;
1090 struct sk_buff_head rxq;
1091 struct sk_buff_head errq;
1092 struct sk_buff_head tmpq;
1095 spin_lock(&queue->rx_lock);
1097 skb_queue_head_init(&rxq);
1098 skb_queue_head_init(&errq);
1099 skb_queue_head_init(&tmpq);
1101 rp = queue->rx.sring->rsp_prod;
1102 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1103 dev_alert(&dev->dev, "Illegal number of responses %u\n",
1104 rp - queue->rx.rsp_cons);
1105 queue->info->broken = true;
1106 spin_unlock(&queue->rx_lock);
1109 rmb(); /* Ensure we see queued responses up to 'rp'. */
1111 i = queue->rx.rsp_cons;
1113 while ((i != rp) && (work_done < budget)) {
1114 RING_COPY_RESPONSE(&queue->rx, i, rx);
1115 memset(extras, 0, sizeof(rinfo.extras));
1117 err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
1119 if (unlikely(err)) {
1120 if (queue->info->broken) {
1121 spin_unlock(&queue->rx_lock);
1125 while ((skb = __skb_dequeue(&tmpq)))
1126 __skb_queue_tail(&errq, skb);
1127 dev->stats.rx_errors++;
1128 i = queue->rx.rsp_cons;
1132 skb = __skb_dequeue(&tmpq);
1134 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1135 struct xen_netif_extra_info *gso;
1136 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1138 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1139 __skb_queue_head(&tmpq, skb);
1140 xennet_set_rx_rsp_cons(queue,
1141 queue->rx.rsp_cons +
1142 skb_queue_len(&tmpq));
1147 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1148 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1149 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
1151 skb_shinfo(skb)->frags[0].page_offset = rx->offset;
1152 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1153 skb->data_len = rx->status;
1154 skb->len += rx->status;
1156 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
1159 if (rx->flags & XEN_NETRXF_csum_blank)
1160 skb->ip_summed = CHECKSUM_PARTIAL;
1161 else if (rx->flags & XEN_NETRXF_data_validated)
1162 skb->ip_summed = CHECKSUM_UNNECESSARY;
1164 __skb_queue_tail(&rxq, skb);
1166 i = queue->rx.rsp_cons + 1;
1167 xennet_set_rx_rsp_cons(queue, i);
1171 __skb_queue_purge(&errq);
1173 work_done -= handle_incoming_queue(queue, &rxq);
1175 xennet_alloc_rx_buffers(queue);
1177 if (work_done < budget) {
1180 napi_complete(napi);
1182 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1184 napi_schedule(napi);
1187 spin_unlock(&queue->rx_lock);
1192 static int xennet_change_mtu(struct net_device *dev, int mtu)
1194 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
1202 static struct rtnl_link_stats64 *xennet_get_stats64(struct net_device *dev,
1203 struct rtnl_link_stats64 *tot)
1205 struct netfront_info *np = netdev_priv(dev);
1208 for_each_possible_cpu(cpu) {
1209 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1210 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
1211 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1215 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1216 tx_packets = tx_stats->packets;
1217 tx_bytes = tx_stats->bytes;
1218 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
1221 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1222 rx_packets = rx_stats->packets;
1223 rx_bytes = rx_stats->bytes;
1224 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
1226 tot->rx_packets += rx_packets;
1227 tot->tx_packets += tx_packets;
1228 tot->rx_bytes += rx_bytes;
1229 tot->tx_bytes += tx_bytes;
1232 tot->rx_errors = dev->stats.rx_errors;
1233 tot->tx_dropped = dev->stats.tx_dropped;
1238 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1240 struct sk_buff *skb;
1243 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1244 /* Skip over entries which are actually freelist references */
1245 if (!queue->tx_skbs[i])
1248 skb = queue->tx_skbs[i];
1249 queue->tx_skbs[i] = NULL;
1250 get_page(queue->grant_tx_page[i]);
1251 gnttab_end_foreign_access(queue->grant_tx_ref[i],
1253 (unsigned long)page_address(queue->grant_tx_page[i]));
1254 queue->grant_tx_page[i] = NULL;
1255 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1256 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
1257 dev_kfree_skb_irq(skb);
1261 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1265 spin_lock_bh(&queue->rx_lock);
1267 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1268 struct sk_buff *skb;
1271 skb = queue->rx_skbs[id];
1275 ref = queue->grant_rx_ref[id];
1276 if (ref == GRANT_INVALID_REF)
1279 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1281 /* gnttab_end_foreign_access() needs a page ref until
1282 * foreign access is ended (which may be deferred).
1285 gnttab_end_foreign_access(ref, 0,
1286 (unsigned long)page_address(page));
1287 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
1292 spin_unlock_bh(&queue->rx_lock);
1295 static netdev_features_t xennet_fix_features(struct net_device *dev,
1296 netdev_features_t features)
1298 struct netfront_info *np = netdev_priv(dev);
1301 if (features & NETIF_F_SG) {
1302 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1307 features &= ~NETIF_F_SG;
1310 if (features & NETIF_F_IPV6_CSUM) {
1311 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1312 "feature-ipv6-csum-offload", "%d", &val) < 0)
1316 features &= ~NETIF_F_IPV6_CSUM;
1319 if (features & NETIF_F_TSO) {
1320 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1321 "feature-gso-tcpv4", "%d", &val) < 0)
1325 features &= ~NETIF_F_TSO;
1328 if (features & NETIF_F_TSO6) {
1329 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1330 "feature-gso-tcpv6", "%d", &val) < 0)
1334 features &= ~NETIF_F_TSO6;
1340 static int xennet_set_features(struct net_device *dev,
1341 netdev_features_t features)
1343 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1344 netdev_info(dev, "Reducing MTU because no SG offload");
1345 dev->mtu = ETH_DATA_LEN;
1351 static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
1353 unsigned long flags;
1355 if (unlikely(queue->info->broken))
1358 spin_lock_irqsave(&queue->tx_lock, flags);
1359 if (xennet_tx_buf_gc(queue))
1361 spin_unlock_irqrestore(&queue->tx_lock, flags);
1366 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1368 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1370 if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1371 xen_irq_lateeoi(irq, eoiflag);
1376 static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
1378 unsigned int work_queued;
1379 unsigned long flags;
1381 if (unlikely(queue->info->broken))
1384 spin_lock_irqsave(&queue->rx_cons_lock, flags);
1385 work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
1386 if (work_queued > queue->rx_rsp_unconsumed) {
1387 queue->rx_rsp_unconsumed = work_queued;
1389 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1390 const struct device *dev = &queue->info->netdev->dev;
1392 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1393 dev_alert(dev, "RX producer index going backwards\n");
1394 dev_alert(dev, "Disabled for further use\n");
1395 queue->info->broken = true;
1398 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1400 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
1401 napi_schedule(&queue->napi);
1406 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1408 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1410 if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1411 xen_irq_lateeoi(irq, eoiflag);
1416 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1418 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1420 if (xennet_handle_tx(dev_id, &eoiflag) &&
1421 xennet_handle_rx(dev_id, &eoiflag))
1422 xen_irq_lateeoi(irq, eoiflag);
1427 #ifdef CONFIG_NET_POLL_CONTROLLER
1428 static void xennet_poll_controller(struct net_device *dev)
1430 /* Poll each queue */
1431 struct netfront_info *info = netdev_priv(dev);
1432 unsigned int num_queues = dev->real_num_tx_queues;
1438 for (i = 0; i < num_queues; ++i)
1439 xennet_interrupt(0, &info->queues[i]);
1443 static const struct net_device_ops xennet_netdev_ops = {
1444 .ndo_open = xennet_open,
1445 .ndo_stop = xennet_close,
1446 .ndo_start_xmit = xennet_start_xmit,
1447 .ndo_change_mtu = xennet_change_mtu,
1448 .ndo_get_stats64 = xennet_get_stats64,
1449 .ndo_set_mac_address = eth_mac_addr,
1450 .ndo_validate_addr = eth_validate_addr,
1451 .ndo_fix_features = xennet_fix_features,
1452 .ndo_set_features = xennet_set_features,
1453 .ndo_select_queue = xennet_select_queue,
1454 #ifdef CONFIG_NET_POLL_CONTROLLER
1455 .ndo_poll_controller = xennet_poll_controller,
1459 static void xennet_free_netdev(struct net_device *netdev)
1461 struct netfront_info *np = netdev_priv(netdev);
1463 free_percpu(np->rx_stats);
1464 free_percpu(np->tx_stats);
1465 free_netdev(netdev);
1468 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1471 struct net_device *netdev;
1472 struct netfront_info *np;
1474 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1476 return ERR_PTR(-ENOMEM);
1478 np = netdev_priv(netdev);
1484 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1485 if (np->rx_stats == NULL)
1487 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1488 if (np->tx_stats == NULL)
1491 netdev->netdev_ops = &xennet_netdev_ops;
1493 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1495 netdev->hw_features = NETIF_F_SG |
1497 NETIF_F_TSO | NETIF_F_TSO6;
1500 * Assume that all hw features are available for now. This set
1501 * will be adjusted by the call to netdev_update_features() in
1502 * xennet_connect() which is the earliest point where we can
1503 * negotiate with the backend regarding supported features.
1505 netdev->features |= netdev->hw_features;
1507 netdev->ethtool_ops = &xennet_ethtool_ops;
1508 SET_NETDEV_DEV(netdev, &dev->dev);
1510 np->netdev = netdev;
1512 netif_carrier_off(netdev);
1515 xenbus_switch_state(dev, XenbusStateInitialising);
1516 err = wait_event_timeout(module_wq,
1517 xenbus_read_driver_state(dev->otherend) !=
1518 XenbusStateClosed &&
1519 xenbus_read_driver_state(dev->otherend) !=
1520 XenbusStateUnknown, XENNET_TIMEOUT);
1526 xennet_free_netdev(netdev);
1527 return ERR_PTR(err);
1531 * Entry point to this code when a new device is created. Allocate the basic
1532 * structures and the ring buffers for communication with the backend, and
1533 * inform the backend of the appropriate details for those.
1535 static int netfront_probe(struct xenbus_device *dev,
1536 const struct xenbus_device_id *id)
1539 struct net_device *netdev;
1540 struct netfront_info *info;
1542 netdev = xennet_create_dev(dev);
1543 if (IS_ERR(netdev)) {
1544 err = PTR_ERR(netdev);
1545 xenbus_dev_fatal(dev, err, "creating netdev");
1549 info = netdev_priv(netdev);
1550 dev_set_drvdata(&dev->dev, info);
1552 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1558 static void xennet_end_access(int ref, void *page)
1560 /* This frees the page as a side-effect */
1561 if (ref != GRANT_INVALID_REF)
1562 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1565 static void xennet_disconnect_backend(struct netfront_info *info)
1568 unsigned int num_queues = info->netdev->real_num_tx_queues;
1570 netif_carrier_off(info->netdev);
1572 for (i = 0; i < num_queues && info->queues; ++i) {
1573 struct netfront_queue *queue = &info->queues[i];
1575 del_timer_sync(&queue->rx_refill_timer);
1577 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1578 unbind_from_irqhandler(queue->tx_irq, queue);
1579 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1580 unbind_from_irqhandler(queue->tx_irq, queue);
1581 unbind_from_irqhandler(queue->rx_irq, queue);
1583 queue->tx_evtchn = queue->rx_evtchn = 0;
1584 queue->tx_irq = queue->rx_irq = 0;
1586 if (netif_running(info->netdev))
1587 napi_synchronize(&queue->napi);
1589 xennet_release_tx_bufs(queue);
1590 xennet_release_rx_bufs(queue);
1591 gnttab_free_grant_references(queue->gref_tx_head);
1592 gnttab_free_grant_references(queue->gref_rx_head);
1594 /* End access and free the pages */
1595 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1596 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1598 queue->tx_ring_ref = GRANT_INVALID_REF;
1599 queue->rx_ring_ref = GRANT_INVALID_REF;
1600 queue->tx.sring = NULL;
1601 queue->rx.sring = NULL;
1606 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1607 * driver restart. We tear down our netif structure and recreate it, but
1608 * leave the device-layer structures intact so that this is transparent to the
1609 * rest of the kernel.
1611 static int netfront_resume(struct xenbus_device *dev)
1613 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1615 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1617 netif_tx_lock_bh(info->netdev);
1618 netif_device_detach(info->netdev);
1619 netif_tx_unlock_bh(info->netdev);
1621 xennet_disconnect_backend(info);
1625 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1627 char *s, *e, *macstr;
1630 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1632 return PTR_ERR(macstr);
1634 for (i = 0; i < ETH_ALEN; i++) {
1635 mac[i] = simple_strtoul(s, &e, 16);
1636 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1647 static int setup_netfront_single(struct netfront_queue *queue)
1651 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1655 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1656 xennet_interrupt, 0,
1657 queue->info->netdev->name,
1661 queue->rx_evtchn = queue->tx_evtchn;
1662 queue->rx_irq = queue->tx_irq = err;
1667 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1668 queue->tx_evtchn = 0;
1673 static int setup_netfront_split(struct netfront_queue *queue)
1677 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1680 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1682 goto alloc_rx_evtchn_fail;
1684 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1685 "%s-tx", queue->name);
1686 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1687 xennet_tx_interrupt, 0,
1688 queue->tx_irq_name, queue);
1691 queue->tx_irq = err;
1693 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1694 "%s-rx", queue->name);
1695 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1696 xennet_rx_interrupt, 0,
1697 queue->rx_irq_name, queue);
1700 queue->rx_irq = err;
1705 unbind_from_irqhandler(queue->tx_irq, queue);
1708 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1709 queue->rx_evtchn = 0;
1710 alloc_rx_evtchn_fail:
1711 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1712 queue->tx_evtchn = 0;
1717 static int setup_netfront(struct xenbus_device *dev,
1718 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1720 struct xen_netif_tx_sring *txs;
1721 struct xen_netif_rx_sring *rxs = NULL;
1725 queue->tx_ring_ref = GRANT_INVALID_REF;
1726 queue->rx_ring_ref = GRANT_INVALID_REF;
1727 queue->rx.sring = NULL;
1728 queue->tx.sring = NULL;
1730 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1733 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1736 SHARED_RING_INIT(txs);
1737 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1739 err = xenbus_grant_ring(dev, txs, 1, &gref);
1742 queue->tx_ring_ref = gref;
1744 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1747 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1750 SHARED_RING_INIT(rxs);
1751 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1753 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1756 queue->rx_ring_ref = gref;
1758 if (feature_split_evtchn)
1759 err = setup_netfront_split(queue);
1760 /* setup single event channel if
1761 * a) feature-split-event-channels == 0
1762 * b) feature-split-event-channels == 1 but failed to setup
1764 if (!feature_split_evtchn || (feature_split_evtchn && err))
1765 err = setup_netfront_single(queue);
1772 /* If we fail to setup netfront, it is safe to just revoke access to
1773 * granted pages because backend is not accessing it at this point.
1776 if (queue->rx_ring_ref != GRANT_INVALID_REF) {
1777 gnttab_end_foreign_access(queue->rx_ring_ref, 0,
1778 (unsigned long)rxs);
1779 queue->rx_ring_ref = GRANT_INVALID_REF;
1781 free_page((unsigned long)rxs);
1783 if (queue->tx_ring_ref != GRANT_INVALID_REF) {
1784 gnttab_end_foreign_access(queue->tx_ring_ref, 0,
1785 (unsigned long)txs);
1786 queue->tx_ring_ref = GRANT_INVALID_REF;
1788 free_page((unsigned long)txs);
1793 /* Queue-specific initialisation
1794 * This used to be done in xennet_create_dev() but must now
1797 static int xennet_init_queue(struct netfront_queue *queue)
1803 spin_lock_init(&queue->tx_lock);
1804 spin_lock_init(&queue->rx_lock);
1805 spin_lock_init(&queue->rx_cons_lock);
1807 setup_timer(&queue->rx_refill_timer, rx_refill_timeout,
1808 (unsigned long)queue);
1810 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
1811 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
1814 /* Initialise tx_skb_freelist as a free chain containing every entry. */
1815 queue->tx_skb_freelist = 0;
1816 queue->tx_pend_queue = TX_LINK_NONE;
1817 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1818 queue->tx_link[i] = i + 1;
1819 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1820 queue->grant_tx_page[i] = NULL;
1822 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
1824 /* Clear out rx_skbs */
1825 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1826 queue->rx_skbs[i] = NULL;
1827 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1830 /* A grant for every tx ring slot */
1831 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
1832 &queue->gref_tx_head) < 0) {
1833 pr_alert("can't alloc tx grant refs\n");
1838 /* A grant for every rx ring slot */
1839 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
1840 &queue->gref_rx_head) < 0) {
1841 pr_alert("can't alloc rx grant refs\n");
1849 gnttab_free_grant_references(queue->gref_tx_head);
1854 static int write_queue_xenstore_keys(struct netfront_queue *queue,
1855 struct xenbus_transaction *xbt, int write_hierarchical)
1857 /* Write the queue-specific keys into XenStore in the traditional
1858 * way for a single queue, or in a queue subkeys for multiple
1861 struct xenbus_device *dev = queue->info->xbdev;
1863 const char *message;
1867 /* Choose the correct place to write the keys */
1868 if (write_hierarchical) {
1869 pathsize = strlen(dev->nodename) + 10;
1870 path = kzalloc(pathsize, GFP_KERNEL);
1873 message = "out of memory while writing ring references";
1876 snprintf(path, pathsize, "%s/queue-%u",
1877 dev->nodename, queue->id);
1879 path = (char *)dev->nodename;
1882 /* Write ring references */
1883 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1884 queue->tx_ring_ref);
1886 message = "writing tx-ring-ref";
1890 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1891 queue->rx_ring_ref);
1893 message = "writing rx-ring-ref";
1897 /* Write event channels; taking into account both shared
1898 * and split event channel scenarios.
1900 if (queue->tx_evtchn == queue->rx_evtchn) {
1901 /* Shared event channel */
1902 err = xenbus_printf(*xbt, path,
1903 "event-channel", "%u", queue->tx_evtchn);
1905 message = "writing event-channel";
1909 /* Split event channels */
1910 err = xenbus_printf(*xbt, path,
1911 "event-channel-tx", "%u", queue->tx_evtchn);
1913 message = "writing event-channel-tx";
1917 err = xenbus_printf(*xbt, path,
1918 "event-channel-rx", "%u", queue->rx_evtchn);
1920 message = "writing event-channel-rx";
1925 if (write_hierarchical)
1930 if (write_hierarchical)
1932 xenbus_dev_fatal(dev, err, "%s", message);
1936 static void xennet_destroy_queues(struct netfront_info *info)
1940 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
1941 struct netfront_queue *queue = &info->queues[i];
1943 if (netif_running(info->netdev))
1944 napi_disable(&queue->napi);
1945 netif_napi_del(&queue->napi);
1948 kfree(info->queues);
1949 info->queues = NULL;
1952 static int xennet_create_queues(struct netfront_info *info,
1953 unsigned int *num_queues)
1958 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
1963 for (i = 0; i < *num_queues; i++) {
1964 struct netfront_queue *queue = &info->queues[i];
1969 ret = xennet_init_queue(queue);
1971 dev_warn(&info->xbdev->dev,
1972 "only created %d queues\n", i);
1977 netif_napi_add(queue->info->netdev, &queue->napi,
1979 if (netif_running(info->netdev))
1980 napi_enable(&queue->napi);
1983 netif_set_real_num_tx_queues(info->netdev, *num_queues);
1985 if (*num_queues == 0) {
1986 dev_err(&info->xbdev->dev, "no queues\n");
1992 /* Common code used when first setting up, and when resuming. */
1993 static int talk_to_netback(struct xenbus_device *dev,
1994 struct netfront_info *info)
1996 const char *message;
1997 struct xenbus_transaction xbt;
1999 unsigned int feature_split_evtchn;
2001 unsigned int max_queues = 0;
2002 struct netfront_queue *queue = NULL;
2003 unsigned int num_queues = 1;
2004 unsigned int trusted;
2006 info->netdev->irq = 0;
2008 /* Check if backend is trusted. */
2009 err = xenbus_scanf(XBT_NIL, dev->nodename, "trusted", "%u", &trusted);
2012 info->bounce = !xennet_trusted || !trusted;
2014 /* Check if backend supports multiple queues */
2015 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2016 "multi-queue-max-queues", "%u", &max_queues);
2019 num_queues = min(max_queues, xennet_max_queues);
2021 /* Check feature-split-event-channels */
2022 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2023 "feature-split-event-channels", "%u",
2024 &feature_split_evtchn);
2026 feature_split_evtchn = 0;
2028 /* Read mac addr. */
2029 err = xen_net_read_mac(dev, info->netdev->dev_addr);
2031 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
2037 xennet_destroy_queues(info);
2039 /* For the case of a reconnect reset the "broken" indicator. */
2040 info->broken = false;
2042 err = xennet_create_queues(info, &num_queues);
2044 xenbus_dev_fatal(dev, err, "creating queues");
2045 kfree(info->queues);
2046 info->queues = NULL;
2051 /* Create shared ring, alloc event channel -- for each queue */
2052 for (i = 0; i < num_queues; ++i) {
2053 queue = &info->queues[i];
2054 err = setup_netfront(dev, queue, feature_split_evtchn);
2060 err = xenbus_transaction_start(&xbt);
2062 xenbus_dev_fatal(dev, err, "starting transaction");
2066 if (xenbus_exists(XBT_NIL,
2067 info->xbdev->otherend, "multi-queue-max-queues")) {
2068 /* Write the number of queues */
2069 err = xenbus_printf(xbt, dev->nodename,
2070 "multi-queue-num-queues", "%u", num_queues);
2072 message = "writing multi-queue-num-queues";
2073 goto abort_transaction_no_dev_fatal;
2077 if (num_queues == 1) {
2078 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2080 goto abort_transaction_no_dev_fatal;
2082 /* Write the keys for each queue */
2083 for (i = 0; i < num_queues; ++i) {
2084 queue = &info->queues[i];
2085 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2087 goto abort_transaction_no_dev_fatal;
2091 /* The remaining keys are not queue-specific */
2092 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2095 message = "writing request-rx-copy";
2096 goto abort_transaction;
2099 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2101 message = "writing feature-rx-notify";
2102 goto abort_transaction;
2105 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2107 message = "writing feature-sg";
2108 goto abort_transaction;
2111 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2113 message = "writing feature-gso-tcpv4";
2114 goto abort_transaction;
2117 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2119 message = "writing feature-gso-tcpv6";
2120 goto abort_transaction;
2123 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2126 message = "writing feature-ipv6-csum-offload";
2127 goto abort_transaction;
2130 err = xenbus_transaction_end(xbt, 0);
2134 xenbus_dev_fatal(dev, err, "completing transaction");
2141 xenbus_dev_fatal(dev, err, "%s", message);
2142 abort_transaction_no_dev_fatal:
2143 xenbus_transaction_end(xbt, 1);
2145 xennet_disconnect_backend(info);
2147 xennet_destroy_queues(info);
2151 device_unregister(&dev->dev);
2155 static int xennet_connect(struct net_device *dev)
2157 struct netfront_info *np = netdev_priv(dev);
2158 unsigned int num_queues = 0;
2160 unsigned int feature_rx_copy;
2162 struct netfront_queue *queue = NULL;
2164 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
2165 "feature-rx-copy", "%u", &feature_rx_copy);
2167 feature_rx_copy = 0;
2169 if (!feature_rx_copy) {
2171 "backend does not support copying receive path\n");
2175 err = talk_to_netback(np->xbdev, np);
2179 dev_info(&np->xbdev->dev,
2180 "bouncing transmitted data to zeroed pages\n");
2182 /* talk_to_netback() sets the correct number of queues */
2183 num_queues = dev->real_num_tx_queues;
2185 if (dev->reg_state == NETREG_UNINITIALIZED) {
2186 err = register_netdev(dev);
2188 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2189 device_unregister(&np->xbdev->dev);
2195 netdev_update_features(dev);
2199 * All public and private state should now be sane. Get
2200 * ready to start sending and receiving packets and give the driver
2201 * domain a kick because we've probably just requeued some
2204 netif_tx_lock_bh(np->netdev);
2205 netif_device_attach(np->netdev);
2206 netif_tx_unlock_bh(np->netdev);
2208 netif_carrier_on(np->netdev);
2209 for (j = 0; j < num_queues; ++j) {
2210 queue = &np->queues[j];
2212 notify_remote_via_irq(queue->tx_irq);
2213 if (queue->tx_irq != queue->rx_irq)
2214 notify_remote_via_irq(queue->rx_irq);
2216 spin_lock_irq(&queue->tx_lock);
2217 xennet_tx_buf_gc(queue);
2218 spin_unlock_irq(&queue->tx_lock);
2220 spin_lock_bh(&queue->rx_lock);
2221 xennet_alloc_rx_buffers(queue);
2222 spin_unlock_bh(&queue->rx_lock);
2229 * Callback received when the backend's state changes.
2231 static void netback_changed(struct xenbus_device *dev,
2232 enum xenbus_state backend_state)
2234 struct netfront_info *np = dev_get_drvdata(&dev->dev);
2235 struct net_device *netdev = np->netdev;
2237 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2239 wake_up_all(&module_wq);
2241 switch (backend_state) {
2242 case XenbusStateInitialising:
2243 case XenbusStateInitialised:
2244 case XenbusStateReconfiguring:
2245 case XenbusStateReconfigured:
2246 case XenbusStateUnknown:
2249 case XenbusStateInitWait:
2250 if (dev->state != XenbusStateInitialising)
2252 if (xennet_connect(netdev) != 0)
2254 xenbus_switch_state(dev, XenbusStateConnected);
2257 case XenbusStateConnected:
2258 netdev_notify_peers(netdev);
2261 case XenbusStateClosed:
2262 if (dev->state == XenbusStateClosed)
2264 /* Missed the backend's CLOSING state -- fallthrough */
2265 case XenbusStateClosing:
2266 xenbus_frontend_closed(dev);
2271 static const struct xennet_stat {
2272 char name[ETH_GSTRING_LEN];
2274 } xennet_stats[] = {
2276 "rx_gso_checksum_fixup",
2277 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2281 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2283 switch (string_set) {
2285 return ARRAY_SIZE(xennet_stats);
2291 static void xennet_get_ethtool_stats(struct net_device *dev,
2292 struct ethtool_stats *stats, u64 * data)
2294 void *np = netdev_priv(dev);
2297 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2298 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2301 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2305 switch (stringset) {
2307 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2308 memcpy(data + i * ETH_GSTRING_LEN,
2309 xennet_stats[i].name, ETH_GSTRING_LEN);
2314 static const struct ethtool_ops xennet_ethtool_ops =
2316 .get_link = ethtool_op_get_link,
2318 .get_sset_count = xennet_get_sset_count,
2319 .get_ethtool_stats = xennet_get_ethtool_stats,
2320 .get_strings = xennet_get_strings,
2324 static ssize_t show_rxbuf(struct device *dev,
2325 struct device_attribute *attr, char *buf)
2327 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2330 static ssize_t store_rxbuf(struct device *dev,
2331 struct device_attribute *attr,
2332 const char *buf, size_t len)
2335 unsigned long target;
2337 if (!capable(CAP_NET_ADMIN))
2340 target = simple_strtoul(buf, &endp, 0);
2344 /* rxbuf_min and rxbuf_max are no longer configurable. */
2349 static DEVICE_ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
2350 static DEVICE_ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
2351 static DEVICE_ATTR(rxbuf_cur, S_IRUGO, show_rxbuf, NULL);
2353 static struct attribute *xennet_dev_attrs[] = {
2354 &dev_attr_rxbuf_min.attr,
2355 &dev_attr_rxbuf_max.attr,
2356 &dev_attr_rxbuf_cur.attr,
2360 static const struct attribute_group xennet_dev_group = {
2361 .attrs = xennet_dev_attrs
2363 #endif /* CONFIG_SYSFS */
2365 static void xennet_bus_close(struct xenbus_device *dev)
2369 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2372 xenbus_switch_state(dev, XenbusStateClosing);
2373 ret = wait_event_timeout(module_wq,
2374 xenbus_read_driver_state(dev->otherend) ==
2375 XenbusStateClosing ||
2376 xenbus_read_driver_state(dev->otherend) ==
2377 XenbusStateClosed ||
2378 xenbus_read_driver_state(dev->otherend) ==
2383 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2387 xenbus_switch_state(dev, XenbusStateClosed);
2388 ret = wait_event_timeout(module_wq,
2389 xenbus_read_driver_state(dev->otherend) ==
2390 XenbusStateClosed ||
2391 xenbus_read_driver_state(dev->otherend) ==
2397 static int xennet_remove(struct xenbus_device *dev)
2399 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2401 xennet_bus_close(dev);
2402 xennet_disconnect_backend(info);
2404 if (info->netdev->reg_state == NETREG_REGISTERED)
2405 unregister_netdev(info->netdev);
2409 xennet_destroy_queues(info);
2412 xennet_free_netdev(info->netdev);
2417 static const struct xenbus_device_id netfront_ids[] = {
2422 static struct xenbus_driver netfront_driver = {
2423 .ids = netfront_ids,
2424 .probe = netfront_probe,
2425 .remove = xennet_remove,
2426 .resume = netfront_resume,
2427 .otherend_changed = netback_changed,
2430 static int __init netif_init(void)
2435 if (!xen_has_pv_nic_devices())
2438 pr_info("Initialising Xen virtual ethernet driver\n");
2440 /* Allow as many queues as there are CPUs if user has not
2441 * specified a value.
2443 if (xennet_max_queues == 0)
2444 xennet_max_queues = num_online_cpus();
2446 return xenbus_register_frontend(&netfront_driver);
2448 module_init(netif_init);
2451 static void __exit netif_exit(void)
2453 xenbus_unregister_driver(&netfront_driver);
2455 module_exit(netif_exit);
2457 MODULE_DESCRIPTION("Xen virtual network device frontend");
2458 MODULE_LICENSE("GPL");
2459 MODULE_ALIAS("xen:vif");
2460 MODULE_ALIAS("xennet");