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 #define MAX_QUEUES_DEFAULT 8
61 static unsigned int xennet_max_queues;
62 module_param_named(max_queues, xennet_max_queues, uint, 0644);
63 MODULE_PARM_DESC(max_queues,
64 "Maximum number of queues per virtual interface");
66 static bool __read_mostly xennet_trusted = true;
67 module_param_named(trusted, xennet_trusted, bool, 0644);
68 MODULE_PARM_DESC(trusted, "Is the backend trusted");
70 #define XENNET_TIMEOUT (5 * HZ)
72 static const struct ethtool_ops xennet_ethtool_ops;
78 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
80 #define RX_COPY_THRESHOLD 256
82 #define GRANT_INVALID_REF 0
84 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
85 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
87 /* Minimum number of Rx slots (includes slot for GSO metadata). */
88 #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
90 /* Queue name is interface name with "-qNNN" appended */
91 #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
93 /* IRQ name is queue name with "-tx" or "-rx" appended */
94 #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
96 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
98 struct netfront_stats {
101 struct u64_stats_sync syncp;
104 struct netfront_info;
106 struct netfront_queue {
107 unsigned int id; /* Queue ID, 0-based */
108 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
109 struct netfront_info *info;
111 struct napi_struct napi;
113 /* Split event channels support, tx_* == rx_* when using
114 * single event channel.
116 unsigned int tx_evtchn, rx_evtchn;
117 unsigned int tx_irq, rx_irq;
118 /* Only used when split event channels support is enabled */
119 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
120 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
123 struct xen_netif_tx_front_ring tx;
127 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
128 * are linked from tx_skb_freelist through tx_link.
130 struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
131 unsigned short tx_link[NET_TX_RING_SIZE];
132 #define TX_LINK_NONE 0xffff
133 #define TX_PENDING 0xfffe
134 grant_ref_t gref_tx_head;
135 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
136 struct page *grant_tx_page[NET_TX_RING_SIZE];
137 unsigned tx_skb_freelist;
138 unsigned int tx_pend_queue;
140 spinlock_t rx_lock ____cacheline_aligned_in_smp;
141 struct xen_netif_rx_front_ring rx;
144 struct timer_list rx_refill_timer;
146 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
147 grant_ref_t gref_rx_head;
148 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
150 unsigned int rx_rsp_unconsumed;
151 spinlock_t rx_cons_lock;
154 struct netfront_info {
155 struct list_head list;
156 struct net_device *netdev;
158 struct xenbus_device *xbdev;
160 /* Multi-queue support */
161 struct netfront_queue *queues;
164 struct netfront_stats __percpu *rx_stats;
165 struct netfront_stats __percpu *tx_stats;
167 /* Is device behaving sane? */
170 /* Should skbs be bounced into a zeroed buffer? */
173 atomic_t rx_gso_checksum_fixup;
176 struct netfront_rx_info {
177 struct xen_netif_rx_response rx;
178 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
182 * Access macros for acquiring freeing slots in tx_skbs[].
185 static void add_id_to_list(unsigned *head, unsigned short *list,
192 static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
194 unsigned int id = *head;
196 if (id != TX_LINK_NONE) {
198 list[id] = TX_LINK_NONE;
203 static int xennet_rxidx(RING_IDX idx)
205 return idx & (NET_RX_RING_SIZE - 1);
208 static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
211 int i = xennet_rxidx(ri);
212 struct sk_buff *skb = queue->rx_skbs[i];
213 queue->rx_skbs[i] = NULL;
217 static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
220 int i = xennet_rxidx(ri);
221 grant_ref_t ref = queue->grant_rx_ref[i];
222 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
227 static const struct attribute_group xennet_dev_group;
230 static bool xennet_can_sg(struct net_device *dev)
232 return dev->features & NETIF_F_SG;
236 static void rx_refill_timeout(struct timer_list *t)
238 struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
239 napi_schedule(&queue->napi);
242 static int netfront_tx_slot_available(struct netfront_queue *queue)
244 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
245 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
248 static void xennet_maybe_wake_tx(struct netfront_queue *queue)
250 struct net_device *dev = queue->info->netdev;
251 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
253 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
254 netfront_tx_slot_available(queue) &&
255 likely(netif_running(dev)))
256 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
260 static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
265 skb = __netdev_alloc_skb(queue->info->netdev,
266 RX_COPY_THRESHOLD + NET_IP_ALIGN,
267 GFP_ATOMIC | __GFP_NOWARN);
271 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN | __GFP_ZERO);
276 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
278 /* Align ip header to a 16 bytes boundary */
279 skb_reserve(skb, NET_IP_ALIGN);
280 skb->dev = queue->info->netdev;
286 static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
288 RING_IDX req_prod = queue->rx.req_prod_pvt;
292 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
295 for (req_prod = queue->rx.req_prod_pvt;
296 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
302 struct xen_netif_rx_request *req;
304 skb = xennet_alloc_one_rx_buffer(queue);
310 id = xennet_rxidx(req_prod);
312 BUG_ON(queue->rx_skbs[id]);
313 queue->rx_skbs[id] = skb;
315 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
316 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
317 queue->grant_rx_ref[id] = ref;
319 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
321 req = RING_GET_REQUEST(&queue->rx, req_prod);
322 gnttab_page_grant_foreign_access_ref_one(ref,
323 queue->info->xbdev->otherend_id,
330 queue->rx.req_prod_pvt = req_prod;
332 /* Try again later if there are not enough requests or skb allocation
334 * Enough requests is quantified as the sum of newly created slots and
335 * the unconsumed slots at the backend.
337 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
339 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
343 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
345 notify_remote_via_irq(queue->rx_irq);
348 static int xennet_open(struct net_device *dev)
350 struct netfront_info *np = netdev_priv(dev);
351 unsigned int num_queues = dev->real_num_tx_queues;
353 struct netfront_queue *queue = NULL;
355 if (!np->queues || np->broken)
358 for (i = 0; i < num_queues; ++i) {
359 queue = &np->queues[i];
360 napi_enable(&queue->napi);
362 spin_lock_bh(&queue->rx_lock);
363 if (netif_carrier_ok(dev)) {
364 xennet_alloc_rx_buffers(queue);
365 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
366 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
367 napi_schedule(&queue->napi);
369 spin_unlock_bh(&queue->rx_lock);
372 netif_tx_start_all_queues(dev);
377 static bool xennet_tx_buf_gc(struct netfront_queue *queue)
383 bool work_done = false;
384 const struct device *dev = &queue->info->netdev->dev;
386 BUG_ON(!netif_carrier_ok(queue->info->netdev));
389 prod = queue->tx.sring->rsp_prod;
390 if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
391 dev_alert(dev, "Illegal number of responses %u\n",
392 prod - queue->tx.rsp_cons);
395 rmb(); /* Ensure we see responses up to 'rp'. */
397 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
398 struct xen_netif_tx_response txrsp;
402 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
403 if (txrsp.status == XEN_NETIF_RSP_NULL)
407 if (id >= RING_SIZE(&queue->tx)) {
409 "Response has incorrect id (%u)\n",
413 if (queue->tx_link[id] != TX_PENDING) {
415 "Response for inactive request\n");
419 queue->tx_link[id] = TX_LINK_NONE;
420 skb = queue->tx_skbs[id];
421 queue->tx_skbs[id] = NULL;
422 if (unlikely(!gnttab_end_foreign_access_ref(
423 queue->grant_tx_ref[id], GNTMAP_readonly))) {
425 "Grant still in use by backend domain\n");
428 gnttab_release_grant_reference(
429 &queue->gref_tx_head, queue->grant_tx_ref[id]);
430 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
431 queue->grant_tx_page[id] = NULL;
432 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
433 dev_kfree_skb_irq(skb);
436 queue->tx.rsp_cons = prod;
438 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
439 } while (more_to_do);
441 xennet_maybe_wake_tx(queue);
446 queue->info->broken = true;
447 dev_alert(dev, "Disabled for further use\n");
452 struct xennet_gnttab_make_txreq {
453 struct netfront_queue *queue;
456 struct xen_netif_tx_request *tx; /* Last request on ring page */
457 struct xen_netif_tx_request tx_local; /* Last request local copy*/
461 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
462 unsigned int len, void *data)
464 struct xennet_gnttab_make_txreq *info = data;
466 struct xen_netif_tx_request *tx;
468 /* convenient aliases */
469 struct page *page = info->page;
470 struct netfront_queue *queue = info->queue;
471 struct sk_buff *skb = info->skb;
473 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
474 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
475 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
476 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
478 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
479 gfn, GNTMAP_readonly);
481 queue->tx_skbs[id] = skb;
482 queue->grant_tx_page[id] = page;
483 queue->grant_tx_ref[id] = ref;
485 info->tx_local.id = id;
486 info->tx_local.gref = ref;
487 info->tx_local.offset = offset;
488 info->tx_local.size = len;
489 info->tx_local.flags = 0;
491 *tx = info->tx_local;
494 * Put the request in the pending queue, it will be set to be pending
495 * when the producer index is about to be raised.
497 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
500 info->size += info->tx_local.size;
503 static struct xen_netif_tx_request *xennet_make_first_txreq(
504 struct xennet_gnttab_make_txreq *info,
505 unsigned int offset, unsigned int len)
509 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
514 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
515 unsigned int len, void *data)
517 struct xennet_gnttab_make_txreq *info = data;
519 info->tx->flags |= XEN_NETTXF_more_data;
521 xennet_tx_setup_grant(gfn, offset, len, data);
524 static void xennet_make_txreqs(
525 struct xennet_gnttab_make_txreq *info,
527 unsigned int offset, unsigned int len)
529 /* Skip unused frames from start of page */
530 page += offset >> PAGE_SHIFT;
531 offset &= ~PAGE_MASK;
537 gnttab_foreach_grant_in_range(page, offset, len,
538 xennet_make_one_txreq,
548 * Count how many ring slots are required to send this skb. Each frag
549 * might be a compound page.
551 static int xennet_count_skb_slots(struct sk_buff *skb)
553 int i, frags = skb_shinfo(skb)->nr_frags;
556 slots = gnttab_count_grant(offset_in_page(skb->data),
559 for (i = 0; i < frags; i++) {
560 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
561 unsigned long size = skb_frag_size(frag);
562 unsigned long offset = skb_frag_off(frag);
564 /* Skip unused frames from start of page */
565 offset &= ~PAGE_MASK;
567 slots += gnttab_count_grant(offset, size);
573 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
574 struct net_device *sb_dev)
576 unsigned int num_queues = dev->real_num_tx_queues;
580 /* First, check if there is only one queue */
581 if (num_queues == 1) {
584 hash = skb_get_hash(skb);
585 queue_idx = hash % num_queues;
591 static void xennet_mark_tx_pending(struct netfront_queue *queue)
595 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
597 queue->tx_link[i] = TX_PENDING;
600 struct sk_buff *bounce_skb(const struct sk_buff *skb)
602 unsigned int headerlen = skb_headroom(skb);
603 /* Align size to allocate full pages and avoid contiguous data leaks */
604 unsigned int size = ALIGN(skb_end_offset(skb) + skb->data_len,
606 struct sk_buff *n = alloc_skb(size, GFP_ATOMIC | __GFP_ZERO);
611 if (!IS_ALIGNED((uintptr_t)n->head, XEN_PAGE_SIZE)) {
612 WARN_ONCE(1, "misaligned skb allocated\n");
617 /* Set the data pointer */
618 skb_reserve(n, headerlen);
619 /* Set the tail pointer and length */
620 skb_put(n, skb->len);
622 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
624 skb_copy_header(n, skb);
628 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
630 static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
632 struct netfront_info *np = netdev_priv(dev);
633 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
634 struct xen_netif_tx_request *first_tx;
642 struct netfront_queue *queue = NULL;
643 struct xennet_gnttab_make_txreq info = { };
644 unsigned int num_queues = dev->real_num_tx_queues;
646 struct sk_buff *nskb;
648 /* Drop the packet if no queues are set up */
651 if (unlikely(np->broken))
653 /* Determine which queue to transmit this SKB on */
654 queue_index = skb_get_queue_mapping(skb);
655 queue = &np->queues[queue_index];
657 /* If skb->len is too big for wire format, drop skb and alert
658 * user about misconfiguration.
660 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
661 net_alert_ratelimited(
662 "xennet: skb->len = %u, too big for wire format\n",
667 slots = xennet_count_skb_slots(skb);
668 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
669 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
671 if (skb_linearize(skb))
675 page = virt_to_page(skb->data);
676 offset = offset_in_page(skb->data);
678 /* The first req should be at least ETH_HLEN size or the packet will be
679 * dropped by netback.
681 * If the backend is not trusted bounce all data to zeroed pages to
682 * avoid exposing contiguous data on the granted page not belonging to
685 if (np->bounce || unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
686 nskb = bounce_skb(skb);
689 dev_consume_skb_any(skb);
691 page = virt_to_page(skb->data);
692 offset = offset_in_page(skb->data);
695 len = skb_headlen(skb);
697 spin_lock_irqsave(&queue->tx_lock, flags);
699 if (unlikely(!netif_carrier_ok(dev) ||
700 (slots > 1 && !xennet_can_sg(dev)) ||
701 netif_needs_gso(skb, netif_skb_features(skb)))) {
702 spin_unlock_irqrestore(&queue->tx_lock, flags);
706 /* First request for the linear area. */
710 first_tx = xennet_make_first_txreq(&info, offset, len);
711 offset += info.tx_local.size;
712 if (offset == PAGE_SIZE) {
716 len -= info.tx_local.size;
718 if (skb->ip_summed == CHECKSUM_PARTIAL)
720 first_tx->flags |= XEN_NETTXF_csum_blank |
721 XEN_NETTXF_data_validated;
722 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
723 /* remote but checksummed. */
724 first_tx->flags |= XEN_NETTXF_data_validated;
726 /* Optional extra info after the first request. */
727 if (skb_shinfo(skb)->gso_size) {
728 struct xen_netif_extra_info *gso;
730 gso = (struct xen_netif_extra_info *)
731 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
733 first_tx->flags |= XEN_NETTXF_extra_info;
735 gso->u.gso.size = skb_shinfo(skb)->gso_size;
736 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
737 XEN_NETIF_GSO_TYPE_TCPV6 :
738 XEN_NETIF_GSO_TYPE_TCPV4;
740 gso->u.gso.features = 0;
742 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
746 /* Requests for the rest of the linear area. */
747 xennet_make_txreqs(&info, page, offset, len);
749 /* Requests for all the frags. */
750 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
751 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
752 xennet_make_txreqs(&info, skb_frag_page(frag),
754 skb_frag_size(frag));
757 /* First request has the packet length. */
758 first_tx->size = skb->len;
760 xennet_mark_tx_pending(queue);
762 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
764 notify_remote_via_irq(queue->tx_irq);
766 u64_stats_update_begin(&tx_stats->syncp);
767 tx_stats->bytes += skb->len;
769 u64_stats_update_end(&tx_stats->syncp);
771 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
772 xennet_tx_buf_gc(queue);
774 if (!netfront_tx_slot_available(queue))
775 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
777 spin_unlock_irqrestore(&queue->tx_lock, flags);
782 dev->stats.tx_dropped++;
783 dev_kfree_skb_any(skb);
787 static int xennet_close(struct net_device *dev)
789 struct netfront_info *np = netdev_priv(dev);
790 unsigned int num_queues = dev->real_num_tx_queues;
792 struct netfront_queue *queue;
793 netif_tx_stop_all_queues(np->netdev);
794 for (i = 0; i < num_queues; ++i) {
795 queue = &np->queues[i];
796 napi_disable(&queue->napi);
801 static void xennet_destroy_queues(struct netfront_info *info)
805 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
806 struct netfront_queue *queue = &info->queues[i];
808 if (netif_running(info->netdev))
809 napi_disable(&queue->napi);
810 netif_napi_del(&queue->napi);
817 static void xennet_uninit(struct net_device *dev)
819 struct netfront_info *np = netdev_priv(dev);
820 xennet_destroy_queues(np);
823 static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
827 spin_lock_irqsave(&queue->rx_cons_lock, flags);
828 queue->rx.rsp_cons = val;
829 queue->rx_rsp_unconsumed = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
830 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
833 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
836 int new = xennet_rxidx(queue->rx.req_prod_pvt);
838 BUG_ON(queue->rx_skbs[new]);
839 queue->rx_skbs[new] = skb;
840 queue->grant_rx_ref[new] = ref;
841 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
842 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
843 queue->rx.req_prod_pvt++;
846 static int xennet_get_extras(struct netfront_queue *queue,
847 struct xen_netif_extra_info *extras,
851 struct xen_netif_extra_info extra;
852 struct device *dev = &queue->info->netdev->dev;
853 RING_IDX cons = queue->rx.rsp_cons;
860 if (unlikely(cons + 1 == rp)) {
862 dev_warn(dev, "Missing extra info\n");
867 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
869 if (unlikely(!extra.type ||
870 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
872 dev_warn(dev, "Invalid extra type: %d\n",
876 extras[extra.type - 1] = extra;
879 skb = xennet_get_rx_skb(queue, cons);
880 ref = xennet_get_rx_ref(queue, cons);
881 xennet_move_rx_slot(queue, skb, ref);
882 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
884 xennet_set_rx_rsp_cons(queue, cons);
888 static int xennet_get_responses(struct netfront_queue *queue,
889 struct netfront_rx_info *rinfo, RING_IDX rp,
890 struct sk_buff_head *list)
892 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
893 struct xen_netif_extra_info *extras = rinfo->extras;
894 struct device *dev = &queue->info->netdev->dev;
895 RING_IDX cons = queue->rx.rsp_cons;
896 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
897 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
898 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
902 if (rx->flags & XEN_NETRXF_extra_info) {
903 err = xennet_get_extras(queue, extras, rp);
904 cons = queue->rx.rsp_cons;
908 if (unlikely(rx->status < 0 ||
909 rx->offset + rx->status > XEN_PAGE_SIZE)) {
911 dev_warn(dev, "rx->offset: %u, size: %d\n",
912 rx->offset, rx->status);
913 xennet_move_rx_slot(queue, skb, ref);
919 * This definitely indicates a bug, either in this driver or in
920 * the backend driver. In future this should flag the bad
921 * situation to the system controller to reboot the backend.
923 if (ref == GRANT_INVALID_REF) {
925 dev_warn(dev, "Bad rx response id %d.\n",
931 if (!gnttab_end_foreign_access_ref(ref, 0)) {
933 "Grant still in use by backend domain\n");
934 queue->info->broken = true;
935 dev_alert(dev, "Disabled for further use\n");
939 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
941 __skb_queue_tail(list, skb);
944 if (!(rx->flags & XEN_NETRXF_more_data))
947 if (cons + slots == rp) {
949 dev_warn(dev, "Need more slots\n");
954 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
956 skb = xennet_get_rx_skb(queue, cons + slots);
957 ref = xennet_get_rx_ref(queue, cons + slots);
961 if (unlikely(slots > max)) {
963 dev_warn(dev, "Too many slots\n");
968 xennet_set_rx_rsp_cons(queue, cons + slots);
973 static int xennet_set_skb_gso(struct sk_buff *skb,
974 struct xen_netif_extra_info *gso)
976 if (!gso->u.gso.size) {
978 pr_warn("GSO size must not be zero\n");
982 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
983 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
985 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
989 skb_shinfo(skb)->gso_size = gso->u.gso.size;
990 skb_shinfo(skb)->gso_type =
991 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
995 /* Header must be checked, and gso_segs computed. */
996 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
997 skb_shinfo(skb)->gso_segs = 0;
1002 static int xennet_fill_frags(struct netfront_queue *queue,
1003 struct sk_buff *skb,
1004 struct sk_buff_head *list)
1006 RING_IDX cons = queue->rx.rsp_cons;
1007 struct sk_buff *nskb;
1009 while ((nskb = __skb_dequeue(list))) {
1010 struct xen_netif_rx_response rx;
1011 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
1013 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
1015 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
1016 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1018 BUG_ON(pull_to < skb_headlen(skb));
1019 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1021 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
1022 xennet_set_rx_rsp_cons(queue,
1023 ++cons + skb_queue_len(list));
1028 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1029 skb_frag_page(nfrag),
1030 rx.offset, rx.status, PAGE_SIZE);
1032 skb_shinfo(nskb)->nr_frags = 0;
1036 xennet_set_rx_rsp_cons(queue, cons);
1041 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
1043 bool recalculate_partial_csum = false;
1046 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1047 * peers can fail to set NETRXF_csum_blank when sending a GSO
1048 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1049 * recalculate the partial checksum.
1051 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1052 struct netfront_info *np = netdev_priv(dev);
1053 atomic_inc(&np->rx_gso_checksum_fixup);
1054 skb->ip_summed = CHECKSUM_PARTIAL;
1055 recalculate_partial_csum = true;
1058 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1059 if (skb->ip_summed != CHECKSUM_PARTIAL)
1062 return skb_checksum_setup(skb, recalculate_partial_csum);
1065 static int handle_incoming_queue(struct netfront_queue *queue,
1066 struct sk_buff_head *rxq)
1068 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
1069 int packets_dropped = 0;
1070 struct sk_buff *skb;
1072 while ((skb = __skb_dequeue(rxq)) != NULL) {
1073 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1075 if (pull_to > skb_headlen(skb))
1076 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1078 /* Ethernet work: Delayed to here as it peeks the header. */
1079 skb->protocol = eth_type_trans(skb, queue->info->netdev);
1080 skb_reset_network_header(skb);
1082 if (checksum_setup(queue->info->netdev, skb)) {
1085 queue->info->netdev->stats.rx_errors++;
1089 u64_stats_update_begin(&rx_stats->syncp);
1090 rx_stats->packets++;
1091 rx_stats->bytes += skb->len;
1092 u64_stats_update_end(&rx_stats->syncp);
1095 napi_gro_receive(&queue->napi, skb);
1098 return packets_dropped;
1101 static int xennet_poll(struct napi_struct *napi, int budget)
1103 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1104 struct net_device *dev = queue->info->netdev;
1105 struct sk_buff *skb;
1106 struct netfront_rx_info rinfo;
1107 struct xen_netif_rx_response *rx = &rinfo.rx;
1108 struct xen_netif_extra_info *extras = rinfo.extras;
1111 struct sk_buff_head rxq;
1112 struct sk_buff_head errq;
1113 struct sk_buff_head tmpq;
1116 spin_lock(&queue->rx_lock);
1118 skb_queue_head_init(&rxq);
1119 skb_queue_head_init(&errq);
1120 skb_queue_head_init(&tmpq);
1122 rp = queue->rx.sring->rsp_prod;
1123 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1124 dev_alert(&dev->dev, "Illegal number of responses %u\n",
1125 rp - queue->rx.rsp_cons);
1126 queue->info->broken = true;
1127 spin_unlock(&queue->rx_lock);
1130 rmb(); /* Ensure we see queued responses up to 'rp'. */
1132 i = queue->rx.rsp_cons;
1134 while ((i != rp) && (work_done < budget)) {
1135 RING_COPY_RESPONSE(&queue->rx, i, rx);
1136 memset(extras, 0, sizeof(rinfo.extras));
1138 err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
1140 if (unlikely(err)) {
1141 if (queue->info->broken) {
1142 spin_unlock(&queue->rx_lock);
1146 while ((skb = __skb_dequeue(&tmpq)))
1147 __skb_queue_tail(&errq, skb);
1148 dev->stats.rx_errors++;
1149 i = queue->rx.rsp_cons;
1153 skb = __skb_dequeue(&tmpq);
1155 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1156 struct xen_netif_extra_info *gso;
1157 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1159 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1160 __skb_queue_head(&tmpq, skb);
1161 xennet_set_rx_rsp_cons(queue,
1162 queue->rx.rsp_cons +
1163 skb_queue_len(&tmpq));
1168 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1169 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1170 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
1172 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
1173 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1174 skb->data_len = rx->status;
1175 skb->len += rx->status;
1177 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
1180 if (rx->flags & XEN_NETRXF_csum_blank)
1181 skb->ip_summed = CHECKSUM_PARTIAL;
1182 else if (rx->flags & XEN_NETRXF_data_validated)
1183 skb->ip_summed = CHECKSUM_UNNECESSARY;
1185 __skb_queue_tail(&rxq, skb);
1187 i = queue->rx.rsp_cons + 1;
1188 xennet_set_rx_rsp_cons(queue, i);
1192 __skb_queue_purge(&errq);
1194 work_done -= handle_incoming_queue(queue, &rxq);
1196 xennet_alloc_rx_buffers(queue);
1198 if (work_done < budget) {
1201 napi_complete_done(napi, work_done);
1203 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1205 napi_schedule(napi);
1208 spin_unlock(&queue->rx_lock);
1213 static int xennet_change_mtu(struct net_device *dev, int mtu)
1215 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
1223 static void xennet_get_stats64(struct net_device *dev,
1224 struct rtnl_link_stats64 *tot)
1226 struct netfront_info *np = netdev_priv(dev);
1229 for_each_possible_cpu(cpu) {
1230 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1231 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
1232 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1236 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1237 tx_packets = tx_stats->packets;
1238 tx_bytes = tx_stats->bytes;
1239 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
1242 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1243 rx_packets = rx_stats->packets;
1244 rx_bytes = rx_stats->bytes;
1245 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
1247 tot->rx_packets += rx_packets;
1248 tot->tx_packets += tx_packets;
1249 tot->rx_bytes += rx_bytes;
1250 tot->tx_bytes += tx_bytes;
1253 tot->rx_errors = dev->stats.rx_errors;
1254 tot->tx_dropped = dev->stats.tx_dropped;
1257 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1259 struct sk_buff *skb;
1262 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1263 /* Skip over entries which are actually freelist references */
1264 if (!queue->tx_skbs[i])
1267 skb = queue->tx_skbs[i];
1268 queue->tx_skbs[i] = NULL;
1269 get_page(queue->grant_tx_page[i]);
1270 gnttab_end_foreign_access(queue->grant_tx_ref[i],
1272 (unsigned long)page_address(queue->grant_tx_page[i]));
1273 queue->grant_tx_page[i] = NULL;
1274 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1275 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
1276 dev_kfree_skb_irq(skb);
1280 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1284 spin_lock_bh(&queue->rx_lock);
1286 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1287 struct sk_buff *skb;
1290 skb = queue->rx_skbs[id];
1294 ref = queue->grant_rx_ref[id];
1295 if (ref == GRANT_INVALID_REF)
1298 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1300 /* gnttab_end_foreign_access() needs a page ref until
1301 * foreign access is ended (which may be deferred).
1304 gnttab_end_foreign_access(ref, 0,
1305 (unsigned long)page_address(page));
1306 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
1311 spin_unlock_bh(&queue->rx_lock);
1314 static netdev_features_t xennet_fix_features(struct net_device *dev,
1315 netdev_features_t features)
1317 struct netfront_info *np = netdev_priv(dev);
1319 if (features & NETIF_F_SG &&
1320 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1321 features &= ~NETIF_F_SG;
1323 if (features & NETIF_F_IPV6_CSUM &&
1324 !xenbus_read_unsigned(np->xbdev->otherend,
1325 "feature-ipv6-csum-offload", 0))
1326 features &= ~NETIF_F_IPV6_CSUM;
1328 if (features & NETIF_F_TSO &&
1329 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1330 features &= ~NETIF_F_TSO;
1332 if (features & NETIF_F_TSO6 &&
1333 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1334 features &= ~NETIF_F_TSO6;
1339 static int xennet_set_features(struct net_device *dev,
1340 netdev_features_t features)
1342 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1343 netdev_info(dev, "Reducing MTU because no SG offload");
1344 dev->mtu = ETH_DATA_LEN;
1350 static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
1352 unsigned long flags;
1354 if (unlikely(queue->info->broken))
1357 spin_lock_irqsave(&queue->tx_lock, flags);
1358 if (xennet_tx_buf_gc(queue))
1360 spin_unlock_irqrestore(&queue->tx_lock, flags);
1365 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1367 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1369 if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1370 xen_irq_lateeoi(irq, eoiflag);
1375 static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
1377 unsigned int work_queued;
1378 unsigned long flags;
1380 if (unlikely(queue->info->broken))
1383 spin_lock_irqsave(&queue->rx_cons_lock, flags);
1384 work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
1385 if (work_queued > queue->rx_rsp_unconsumed) {
1386 queue->rx_rsp_unconsumed = work_queued;
1388 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1389 const struct device *dev = &queue->info->netdev->dev;
1391 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1392 dev_alert(dev, "RX producer index going backwards\n");
1393 dev_alert(dev, "Disabled for further use\n");
1394 queue->info->broken = true;
1397 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1399 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
1400 napi_schedule(&queue->napi);
1405 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1407 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1409 if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1410 xen_irq_lateeoi(irq, eoiflag);
1415 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1417 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1419 if (xennet_handle_tx(dev_id, &eoiflag) &&
1420 xennet_handle_rx(dev_id, &eoiflag))
1421 xen_irq_lateeoi(irq, eoiflag);
1426 #ifdef CONFIG_NET_POLL_CONTROLLER
1427 static void xennet_poll_controller(struct net_device *dev)
1429 /* Poll each queue */
1430 struct netfront_info *info = netdev_priv(dev);
1431 unsigned int num_queues = dev->real_num_tx_queues;
1437 for (i = 0; i < num_queues; ++i)
1438 xennet_interrupt(0, &info->queues[i]);
1442 static const struct net_device_ops xennet_netdev_ops = {
1443 .ndo_uninit = xennet_uninit,
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 netdev->min_mtu = ETH_MIN_MTU;
1509 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
1510 SET_NETDEV_DEV(netdev, &dev->dev);
1512 np->netdev = netdev;
1514 netif_carrier_off(netdev);
1517 xenbus_switch_state(dev, XenbusStateInitialising);
1518 err = wait_event_timeout(module_wq,
1519 xenbus_read_driver_state(dev->otherend) !=
1520 XenbusStateClosed &&
1521 xenbus_read_driver_state(dev->otherend) !=
1522 XenbusStateUnknown, XENNET_TIMEOUT);
1528 xennet_free_netdev(netdev);
1529 return ERR_PTR(err);
1533 * Entry point to this code when a new device is created. Allocate the basic
1534 * structures and the ring buffers for communication with the backend, and
1535 * inform the backend of the appropriate details for those.
1537 static int netfront_probe(struct xenbus_device *dev,
1538 const struct xenbus_device_id *id)
1541 struct net_device *netdev;
1542 struct netfront_info *info;
1544 netdev = xennet_create_dev(dev);
1545 if (IS_ERR(netdev)) {
1546 err = PTR_ERR(netdev);
1547 xenbus_dev_fatal(dev, err, "creating netdev");
1551 info = netdev_priv(netdev);
1552 dev_set_drvdata(&dev->dev, info);
1554 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1560 static void xennet_end_access(int ref, void *page)
1562 /* This frees the page as a side-effect */
1563 if (ref != GRANT_INVALID_REF)
1564 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1567 static void xennet_disconnect_backend(struct netfront_info *info)
1570 unsigned int num_queues = info->netdev->real_num_tx_queues;
1572 netif_carrier_off(info->netdev);
1574 for (i = 0; i < num_queues && info->queues; ++i) {
1575 struct netfront_queue *queue = &info->queues[i];
1577 del_timer_sync(&queue->rx_refill_timer);
1579 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1580 unbind_from_irqhandler(queue->tx_irq, queue);
1581 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1582 unbind_from_irqhandler(queue->tx_irq, queue);
1583 unbind_from_irqhandler(queue->rx_irq, queue);
1585 queue->tx_evtchn = queue->rx_evtchn = 0;
1586 queue->tx_irq = queue->rx_irq = 0;
1588 if (netif_running(info->netdev))
1589 napi_synchronize(&queue->napi);
1591 xennet_release_tx_bufs(queue);
1592 xennet_release_rx_bufs(queue);
1593 gnttab_free_grant_references(queue->gref_tx_head);
1594 gnttab_free_grant_references(queue->gref_rx_head);
1596 /* End access and free the pages */
1597 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1598 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1600 queue->tx_ring_ref = GRANT_INVALID_REF;
1601 queue->rx_ring_ref = GRANT_INVALID_REF;
1602 queue->tx.sring = NULL;
1603 queue->rx.sring = NULL;
1608 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1609 * driver restart. We tear down our netif structure and recreate it, but
1610 * leave the device-layer structures intact so that this is transparent to the
1611 * rest of the kernel.
1613 static int netfront_resume(struct xenbus_device *dev)
1615 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1617 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1619 netif_tx_lock_bh(info->netdev);
1620 netif_device_detach(info->netdev);
1621 netif_tx_unlock_bh(info->netdev);
1623 xennet_disconnect_backend(info);
1627 xennet_destroy_queues(info);
1633 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1635 char *s, *e, *macstr;
1638 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1640 return PTR_ERR(macstr);
1642 for (i = 0; i < ETH_ALEN; i++) {
1643 mac[i] = simple_strtoul(s, &e, 16);
1644 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1655 static int setup_netfront_single(struct netfront_queue *queue)
1659 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1663 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1664 xennet_interrupt, 0,
1665 queue->info->netdev->name,
1669 queue->rx_evtchn = queue->tx_evtchn;
1670 queue->rx_irq = queue->tx_irq = err;
1675 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1676 queue->tx_evtchn = 0;
1681 static int setup_netfront_split(struct netfront_queue *queue)
1685 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1688 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1690 goto alloc_rx_evtchn_fail;
1692 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1693 "%s-tx", queue->name);
1694 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1695 xennet_tx_interrupt, 0,
1696 queue->tx_irq_name, queue);
1699 queue->tx_irq = err;
1701 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1702 "%s-rx", queue->name);
1703 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1704 xennet_rx_interrupt, 0,
1705 queue->rx_irq_name, queue);
1708 queue->rx_irq = err;
1713 unbind_from_irqhandler(queue->tx_irq, queue);
1716 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1717 queue->rx_evtchn = 0;
1718 alloc_rx_evtchn_fail:
1719 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1720 queue->tx_evtchn = 0;
1725 static int setup_netfront(struct xenbus_device *dev,
1726 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1728 struct xen_netif_tx_sring *txs;
1729 struct xen_netif_rx_sring *rxs = NULL;
1733 queue->tx_ring_ref = GRANT_INVALID_REF;
1734 queue->rx_ring_ref = GRANT_INVALID_REF;
1735 queue->rx.sring = NULL;
1736 queue->tx.sring = NULL;
1738 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1741 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1744 SHARED_RING_INIT(txs);
1745 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1747 err = xenbus_grant_ring(dev, txs, 1, &gref);
1750 queue->tx_ring_ref = gref;
1752 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1755 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1758 SHARED_RING_INIT(rxs);
1759 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1761 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1764 queue->rx_ring_ref = gref;
1766 if (feature_split_evtchn)
1767 err = setup_netfront_split(queue);
1768 /* setup single event channel if
1769 * a) feature-split-event-channels == 0
1770 * b) feature-split-event-channels == 1 but failed to setup
1772 if (!feature_split_evtchn || (feature_split_evtchn && err))
1773 err = setup_netfront_single(queue);
1780 /* If we fail to setup netfront, it is safe to just revoke access to
1781 * granted pages because backend is not accessing it at this point.
1784 if (queue->rx_ring_ref != GRANT_INVALID_REF) {
1785 gnttab_end_foreign_access(queue->rx_ring_ref, 0,
1786 (unsigned long)rxs);
1787 queue->rx_ring_ref = GRANT_INVALID_REF;
1789 free_page((unsigned long)rxs);
1791 if (queue->tx_ring_ref != GRANT_INVALID_REF) {
1792 gnttab_end_foreign_access(queue->tx_ring_ref, 0,
1793 (unsigned long)txs);
1794 queue->tx_ring_ref = GRANT_INVALID_REF;
1796 free_page((unsigned long)txs);
1801 /* Queue-specific initialisation
1802 * This used to be done in xennet_create_dev() but must now
1805 static int xennet_init_queue(struct netfront_queue *queue)
1811 spin_lock_init(&queue->tx_lock);
1812 spin_lock_init(&queue->rx_lock);
1813 spin_lock_init(&queue->rx_cons_lock);
1815 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
1817 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
1818 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
1821 /* Initialise tx_skb_freelist as a free chain containing every entry. */
1822 queue->tx_skb_freelist = 0;
1823 queue->tx_pend_queue = TX_LINK_NONE;
1824 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1825 queue->tx_link[i] = i + 1;
1826 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1827 queue->grant_tx_page[i] = NULL;
1829 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
1831 /* Clear out rx_skbs */
1832 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1833 queue->rx_skbs[i] = NULL;
1834 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1837 /* A grant for every tx ring slot */
1838 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
1839 &queue->gref_tx_head) < 0) {
1840 pr_alert("can't alloc tx grant refs\n");
1845 /* A grant for every rx ring slot */
1846 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
1847 &queue->gref_rx_head) < 0) {
1848 pr_alert("can't alloc rx grant refs\n");
1856 gnttab_free_grant_references(queue->gref_tx_head);
1861 static int write_queue_xenstore_keys(struct netfront_queue *queue,
1862 struct xenbus_transaction *xbt, int write_hierarchical)
1864 /* Write the queue-specific keys into XenStore in the traditional
1865 * way for a single queue, or in a queue subkeys for multiple
1868 struct xenbus_device *dev = queue->info->xbdev;
1870 const char *message;
1874 /* Choose the correct place to write the keys */
1875 if (write_hierarchical) {
1876 pathsize = strlen(dev->nodename) + 10;
1877 path = kzalloc(pathsize, GFP_KERNEL);
1880 message = "out of memory while writing ring references";
1883 snprintf(path, pathsize, "%s/queue-%u",
1884 dev->nodename, queue->id);
1886 path = (char *)dev->nodename;
1889 /* Write ring references */
1890 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1891 queue->tx_ring_ref);
1893 message = "writing tx-ring-ref";
1897 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1898 queue->rx_ring_ref);
1900 message = "writing rx-ring-ref";
1904 /* Write event channels; taking into account both shared
1905 * and split event channel scenarios.
1907 if (queue->tx_evtchn == queue->rx_evtchn) {
1908 /* Shared event channel */
1909 err = xenbus_printf(*xbt, path,
1910 "event-channel", "%u", queue->tx_evtchn);
1912 message = "writing event-channel";
1916 /* Split event channels */
1917 err = xenbus_printf(*xbt, path,
1918 "event-channel-tx", "%u", queue->tx_evtchn);
1920 message = "writing event-channel-tx";
1924 err = xenbus_printf(*xbt, path,
1925 "event-channel-rx", "%u", queue->rx_evtchn);
1927 message = "writing event-channel-rx";
1932 if (write_hierarchical)
1937 if (write_hierarchical)
1939 xenbus_dev_fatal(dev, err, "%s", message);
1943 static int xennet_create_queues(struct netfront_info *info,
1944 unsigned int *num_queues)
1949 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
1954 for (i = 0; i < *num_queues; i++) {
1955 struct netfront_queue *queue = &info->queues[i];
1960 ret = xennet_init_queue(queue);
1962 dev_warn(&info->xbdev->dev,
1963 "only created %d queues\n", i);
1968 netif_napi_add(queue->info->netdev, &queue->napi,
1970 if (netif_running(info->netdev))
1971 napi_enable(&queue->napi);
1974 netif_set_real_num_tx_queues(info->netdev, *num_queues);
1976 if (*num_queues == 0) {
1977 dev_err(&info->xbdev->dev, "no queues\n");
1983 /* Common code used when first setting up, and when resuming. */
1984 static int talk_to_netback(struct xenbus_device *dev,
1985 struct netfront_info *info)
1987 const char *message;
1988 struct xenbus_transaction xbt;
1990 unsigned int feature_split_evtchn;
1992 unsigned int max_queues = 0;
1993 struct netfront_queue *queue = NULL;
1994 unsigned int num_queues = 1;
1996 info->netdev->irq = 0;
1998 /* Check if backend is trusted. */
1999 info->bounce = !xennet_trusted ||
2000 !xenbus_read_unsigned(dev->nodename, "trusted", 1);
2002 /* Check if backend supports multiple queues */
2003 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2004 "multi-queue-max-queues", 1);
2005 num_queues = min(max_queues, xennet_max_queues);
2007 /* Check feature-split-event-channels */
2008 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2009 "feature-split-event-channels", 0);
2011 /* Read mac addr. */
2012 err = xen_net_read_mac(dev, info->netdev->dev_addr);
2014 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
2020 xennet_destroy_queues(info);
2022 /* For the case of a reconnect reset the "broken" indicator. */
2023 info->broken = false;
2025 err = xennet_create_queues(info, &num_queues);
2027 xenbus_dev_fatal(dev, err, "creating queues");
2028 kfree(info->queues);
2029 info->queues = NULL;
2034 /* Create shared ring, alloc event channel -- for each queue */
2035 for (i = 0; i < num_queues; ++i) {
2036 queue = &info->queues[i];
2037 err = setup_netfront(dev, queue, feature_split_evtchn);
2043 err = xenbus_transaction_start(&xbt);
2045 xenbus_dev_fatal(dev, err, "starting transaction");
2049 if (xenbus_exists(XBT_NIL,
2050 info->xbdev->otherend, "multi-queue-max-queues")) {
2051 /* Write the number of queues */
2052 err = xenbus_printf(xbt, dev->nodename,
2053 "multi-queue-num-queues", "%u", num_queues);
2055 message = "writing multi-queue-num-queues";
2056 goto abort_transaction_no_dev_fatal;
2060 if (num_queues == 1) {
2061 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2063 goto abort_transaction_no_dev_fatal;
2065 /* Write the keys for each queue */
2066 for (i = 0; i < num_queues; ++i) {
2067 queue = &info->queues[i];
2068 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2070 goto abort_transaction_no_dev_fatal;
2074 /* The remaining keys are not queue-specific */
2075 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2078 message = "writing request-rx-copy";
2079 goto abort_transaction;
2082 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2084 message = "writing feature-rx-notify";
2085 goto abort_transaction;
2088 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2090 message = "writing feature-sg";
2091 goto abort_transaction;
2094 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2096 message = "writing feature-gso-tcpv4";
2097 goto abort_transaction;
2100 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2102 message = "writing feature-gso-tcpv6";
2103 goto abort_transaction;
2106 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2109 message = "writing feature-ipv6-csum-offload";
2110 goto abort_transaction;
2113 err = xenbus_transaction_end(xbt, 0);
2117 xenbus_dev_fatal(dev, err, "completing transaction");
2124 xenbus_dev_fatal(dev, err, "%s", message);
2125 abort_transaction_no_dev_fatal:
2126 xenbus_transaction_end(xbt, 1);
2128 xennet_disconnect_backend(info);
2130 xennet_destroy_queues(info);
2134 device_unregister(&dev->dev);
2138 static int xennet_connect(struct net_device *dev)
2140 struct netfront_info *np = netdev_priv(dev);
2141 unsigned int num_queues = 0;
2144 struct netfront_queue *queue = NULL;
2146 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
2148 "backend does not support copying receive path\n");
2152 err = talk_to_netback(np->xbdev, np);
2156 dev_info(&np->xbdev->dev,
2157 "bouncing transmitted data to zeroed pages\n");
2159 /* talk_to_netback() sets the correct number of queues */
2160 num_queues = dev->real_num_tx_queues;
2162 if (dev->reg_state == NETREG_UNINITIALIZED) {
2163 err = register_netdev(dev);
2165 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2166 device_unregister(&np->xbdev->dev);
2172 netdev_update_features(dev);
2176 * All public and private state should now be sane. Get
2177 * ready to start sending and receiving packets and give the driver
2178 * domain a kick because we've probably just requeued some
2181 netif_tx_lock_bh(np->netdev);
2182 netif_device_attach(np->netdev);
2183 netif_tx_unlock_bh(np->netdev);
2185 netif_carrier_on(np->netdev);
2186 for (j = 0; j < num_queues; ++j) {
2187 queue = &np->queues[j];
2189 notify_remote_via_irq(queue->tx_irq);
2190 if (queue->tx_irq != queue->rx_irq)
2191 notify_remote_via_irq(queue->rx_irq);
2193 spin_lock_irq(&queue->tx_lock);
2194 xennet_tx_buf_gc(queue);
2195 spin_unlock_irq(&queue->tx_lock);
2197 spin_lock_bh(&queue->rx_lock);
2198 xennet_alloc_rx_buffers(queue);
2199 spin_unlock_bh(&queue->rx_lock);
2206 * Callback received when the backend's state changes.
2208 static void netback_changed(struct xenbus_device *dev,
2209 enum xenbus_state backend_state)
2211 struct netfront_info *np = dev_get_drvdata(&dev->dev);
2212 struct net_device *netdev = np->netdev;
2214 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2216 wake_up_all(&module_wq);
2218 switch (backend_state) {
2219 case XenbusStateInitialising:
2220 case XenbusStateInitialised:
2221 case XenbusStateReconfiguring:
2222 case XenbusStateReconfigured:
2223 case XenbusStateUnknown:
2226 case XenbusStateInitWait:
2227 if (dev->state != XenbusStateInitialising)
2229 if (xennet_connect(netdev) != 0)
2231 xenbus_switch_state(dev, XenbusStateConnected);
2234 case XenbusStateConnected:
2235 netdev_notify_peers(netdev);
2238 case XenbusStateClosed:
2239 if (dev->state == XenbusStateClosed)
2241 /* Fall through - Missed the backend's CLOSING state. */
2242 case XenbusStateClosing:
2243 xenbus_frontend_closed(dev);
2248 static const struct xennet_stat {
2249 char name[ETH_GSTRING_LEN];
2251 } xennet_stats[] = {
2253 "rx_gso_checksum_fixup",
2254 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2258 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2260 switch (string_set) {
2262 return ARRAY_SIZE(xennet_stats);
2268 static void xennet_get_ethtool_stats(struct net_device *dev,
2269 struct ethtool_stats *stats, u64 * data)
2271 void *np = netdev_priv(dev);
2274 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2275 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2278 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2282 switch (stringset) {
2284 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2285 memcpy(data + i * ETH_GSTRING_LEN,
2286 xennet_stats[i].name, ETH_GSTRING_LEN);
2291 static const struct ethtool_ops xennet_ethtool_ops =
2293 .get_link = ethtool_op_get_link,
2295 .get_sset_count = xennet_get_sset_count,
2296 .get_ethtool_stats = xennet_get_ethtool_stats,
2297 .get_strings = xennet_get_strings,
2301 static ssize_t show_rxbuf(struct device *dev,
2302 struct device_attribute *attr, char *buf)
2304 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2307 static ssize_t store_rxbuf(struct device *dev,
2308 struct device_attribute *attr,
2309 const char *buf, size_t len)
2312 unsigned long target;
2314 if (!capable(CAP_NET_ADMIN))
2317 target = simple_strtoul(buf, &endp, 0);
2321 /* rxbuf_min and rxbuf_max are no longer configurable. */
2326 static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2327 static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2328 static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
2330 static struct attribute *xennet_dev_attrs[] = {
2331 &dev_attr_rxbuf_min.attr,
2332 &dev_attr_rxbuf_max.attr,
2333 &dev_attr_rxbuf_cur.attr,
2337 static const struct attribute_group xennet_dev_group = {
2338 .attrs = xennet_dev_attrs
2340 #endif /* CONFIG_SYSFS */
2342 static void xennet_bus_close(struct xenbus_device *dev)
2346 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2349 xenbus_switch_state(dev, XenbusStateClosing);
2350 ret = wait_event_timeout(module_wq,
2351 xenbus_read_driver_state(dev->otherend) ==
2352 XenbusStateClosing ||
2353 xenbus_read_driver_state(dev->otherend) ==
2354 XenbusStateClosed ||
2355 xenbus_read_driver_state(dev->otherend) ==
2360 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2364 xenbus_switch_state(dev, XenbusStateClosed);
2365 ret = wait_event_timeout(module_wq,
2366 xenbus_read_driver_state(dev->otherend) ==
2367 XenbusStateClosed ||
2368 xenbus_read_driver_state(dev->otherend) ==
2374 static int xennet_remove(struct xenbus_device *dev)
2376 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2378 xennet_bus_close(dev);
2379 xennet_disconnect_backend(info);
2381 if (info->netdev->reg_state == NETREG_REGISTERED)
2382 unregister_netdev(info->netdev);
2386 xennet_destroy_queues(info);
2389 xennet_free_netdev(info->netdev);
2394 static const struct xenbus_device_id netfront_ids[] = {
2399 static struct xenbus_driver netfront_driver = {
2400 .ids = netfront_ids,
2401 .probe = netfront_probe,
2402 .remove = xennet_remove,
2403 .resume = netfront_resume,
2404 .otherend_changed = netback_changed,
2407 static int __init netif_init(void)
2412 if (!xen_has_pv_nic_devices())
2415 pr_info("Initialising Xen virtual ethernet driver\n");
2417 /* Allow as many queues as there are CPUs inut max. 8 if user has not
2418 * specified a value.
2420 if (xennet_max_queues == 0)
2421 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2424 return xenbus_register_frontend(&netfront_driver);
2426 module_init(netif_init);
2429 static void __exit netif_exit(void)
2431 xenbus_unregister_driver(&netfront_driver);
2433 module_exit(netif_exit);
2435 MODULE_DESCRIPTION("Xen virtual network device frontend");
2436 MODULE_LICENSE("GPL");
2437 MODULE_ALIAS("xen:vif");
2438 MODULE_ALIAS("xennet");