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 #define XENNET_TIMEOUT (5 * HZ)
68 static const struct ethtool_ops xennet_ethtool_ops;
74 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
76 #define RX_COPY_THRESHOLD 256
78 #define GRANT_INVALID_REF 0
80 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
81 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
83 /* Minimum number of Rx slots (includes slot for GSO metadata). */
84 #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
86 /* Queue name is interface name with "-qNNN" appended */
87 #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
89 /* IRQ name is queue name with "-tx" or "-rx" appended */
90 #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
92 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
94 struct netfront_stats {
97 struct u64_stats_sync syncp;
100 struct netfront_info;
102 struct netfront_queue {
103 unsigned int id; /* Queue ID, 0-based */
104 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
105 struct netfront_info *info;
107 struct napi_struct napi;
109 /* Split event channels support, tx_* == rx_* when using
110 * single event channel.
112 unsigned int tx_evtchn, rx_evtchn;
113 unsigned int tx_irq, rx_irq;
114 /* Only used when split event channels support is enabled */
115 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
116 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
119 struct xen_netif_tx_front_ring tx;
123 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
124 * are linked from tx_skb_freelist through skb_entry.link.
126 * NB. Freelist index entries are always going to be less than
127 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
128 * greater than PAGE_OFFSET: we use this property to distinguish
134 } tx_skbs[NET_TX_RING_SIZE];
135 grant_ref_t gref_tx_head;
136 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
137 struct page *grant_tx_page[NET_TX_RING_SIZE];
138 unsigned tx_skb_freelist;
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];
151 struct netfront_info {
152 struct list_head list;
153 struct net_device *netdev;
155 struct xenbus_device *xbdev;
157 /* Multi-queue support */
158 struct netfront_queue *queues;
161 struct netfront_stats __percpu *rx_stats;
162 struct netfront_stats __percpu *tx_stats;
164 atomic_t rx_gso_checksum_fixup;
167 struct netfront_rx_info {
168 struct xen_netif_rx_response rx;
169 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
172 static void skb_entry_set_link(union skb_entry *list, unsigned short id)
177 static int skb_entry_is_link(const union skb_entry *list)
179 BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
180 return (unsigned long)list->skb < PAGE_OFFSET;
184 * Access macros for acquiring freeing slots in tx_skbs[].
187 static void add_id_to_freelist(unsigned *head, union skb_entry *list,
190 skb_entry_set_link(&list[id], *head);
194 static unsigned short get_id_from_freelist(unsigned *head,
195 union skb_entry *list)
197 unsigned int id = *head;
198 *head = list[id].link;
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(struct timer_list *t)
237 struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
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);
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 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
344 notify_remote_via_irq(queue->rx_irq);
347 static int xennet_open(struct net_device *dev)
349 struct netfront_info *np = netdev_priv(dev);
350 unsigned int num_queues = dev->real_num_tx_queues;
352 struct netfront_queue *queue = NULL;
357 for (i = 0; i < num_queues; ++i) {
358 queue = &np->queues[i];
359 napi_enable(&queue->napi);
361 spin_lock_bh(&queue->rx_lock);
362 if (netif_carrier_ok(dev)) {
363 xennet_alloc_rx_buffers(queue);
364 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
365 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
366 napi_schedule(&queue->napi);
368 spin_unlock_bh(&queue->rx_lock);
371 netif_tx_start_all_queues(dev);
376 static void xennet_tx_buf_gc(struct netfront_queue *queue)
383 BUG_ON(!netif_carrier_ok(queue->info->netdev));
386 prod = queue->tx.sring->rsp_prod;
387 rmb(); /* Ensure we see responses up to 'rp'. */
389 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
390 struct xen_netif_tx_response *txrsp;
392 txrsp = RING_GET_RESPONSE(&queue->tx, cons);
393 if (txrsp->status == XEN_NETIF_RSP_NULL)
397 skb = queue->tx_skbs[id].skb;
398 if (unlikely(gnttab_query_foreign_access(
399 queue->grant_tx_ref[id]) != 0)) {
400 pr_alert("%s: warning -- grant still in use by backend domain\n",
404 gnttab_end_foreign_access_ref(
405 queue->grant_tx_ref[id], GNTMAP_readonly);
406 gnttab_release_grant_reference(
407 &queue->gref_tx_head, queue->grant_tx_ref[id]);
408 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
409 queue->grant_tx_page[id] = NULL;
410 add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, id);
411 dev_kfree_skb_irq(skb);
414 queue->tx.rsp_cons = prod;
416 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
417 } while (more_to_do);
419 xennet_maybe_wake_tx(queue);
422 struct xennet_gnttab_make_txreq {
423 struct netfront_queue *queue;
426 struct xen_netif_tx_request *tx; /* Last request */
430 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
431 unsigned int len, void *data)
433 struct xennet_gnttab_make_txreq *info = data;
435 struct xen_netif_tx_request *tx;
437 /* convenient aliases */
438 struct page *page = info->page;
439 struct netfront_queue *queue = info->queue;
440 struct sk_buff *skb = info->skb;
442 id = get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs);
443 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
444 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
445 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
447 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
448 gfn, GNTMAP_readonly);
450 queue->tx_skbs[id].skb = skb;
451 queue->grant_tx_page[id] = page;
452 queue->grant_tx_ref[id] = ref;
461 info->size += tx->size;
464 static struct xen_netif_tx_request *xennet_make_first_txreq(
465 struct netfront_queue *queue, struct sk_buff *skb,
466 struct page *page, unsigned int offset, unsigned int len)
468 struct xennet_gnttab_make_txreq info = {
475 gnttab_for_one_grant(page, offset, len, xennet_tx_setup_grant, &info);
480 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
481 unsigned int len, void *data)
483 struct xennet_gnttab_make_txreq *info = data;
485 info->tx->flags |= XEN_NETTXF_more_data;
487 xennet_tx_setup_grant(gfn, offset, len, data);
490 static struct xen_netif_tx_request *xennet_make_txreqs(
491 struct netfront_queue *queue, struct xen_netif_tx_request *tx,
492 struct sk_buff *skb, struct page *page,
493 unsigned int offset, unsigned int len)
495 struct xennet_gnttab_make_txreq info = {
501 /* Skip unused frames from start of page */
502 page += offset >> PAGE_SHIFT;
503 offset &= ~PAGE_MASK;
509 gnttab_foreach_grant_in_range(page, offset, len,
510 xennet_make_one_txreq,
522 * Count how many ring slots are required to send this skb. Each frag
523 * might be a compound page.
525 static int xennet_count_skb_slots(struct sk_buff *skb)
527 int i, frags = skb_shinfo(skb)->nr_frags;
530 slots = gnttab_count_grant(offset_in_page(skb->data),
533 for (i = 0; i < frags; i++) {
534 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
535 unsigned long size = skb_frag_size(frag);
536 unsigned long offset = skb_frag_off(frag);
538 /* Skip unused frames from start of page */
539 offset &= ~PAGE_MASK;
541 slots += gnttab_count_grant(offset, size);
547 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
548 struct net_device *sb_dev)
550 unsigned int num_queues = dev->real_num_tx_queues;
554 /* First, check if there is only one queue */
555 if (num_queues == 1) {
558 hash = skb_get_hash(skb);
559 queue_idx = hash % num_queues;
565 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
567 static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
569 struct netfront_info *np = netdev_priv(dev);
570 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
571 struct xen_netif_tx_request *tx, *first_tx;
579 struct netfront_queue *queue = NULL;
580 unsigned int num_queues = dev->real_num_tx_queues;
582 struct sk_buff *nskb;
584 /* Drop the packet if no queues are set up */
587 /* Determine which queue to transmit this SKB on */
588 queue_index = skb_get_queue_mapping(skb);
589 queue = &np->queues[queue_index];
591 /* If skb->len is too big for wire format, drop skb and alert
592 * user about misconfiguration.
594 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
595 net_alert_ratelimited(
596 "xennet: skb->len = %u, too big for wire format\n",
601 slots = xennet_count_skb_slots(skb);
602 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
603 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
605 if (skb_linearize(skb))
609 page = virt_to_page(skb->data);
610 offset = offset_in_page(skb->data);
612 /* The first req should be at least ETH_HLEN size or the packet will be
613 * dropped by netback.
615 if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
616 nskb = skb_copy(skb, GFP_ATOMIC);
619 dev_consume_skb_any(skb);
621 page = virt_to_page(skb->data);
622 offset = offset_in_page(skb->data);
625 len = skb_headlen(skb);
627 spin_lock_irqsave(&queue->tx_lock, flags);
629 if (unlikely(!netif_carrier_ok(dev) ||
630 (slots > 1 && !xennet_can_sg(dev)) ||
631 netif_needs_gso(skb, netif_skb_features(skb)))) {
632 spin_unlock_irqrestore(&queue->tx_lock, flags);
636 /* First request for the linear area. */
637 first_tx = tx = xennet_make_first_txreq(queue, skb,
640 if (offset == PAGE_SIZE) {
646 if (skb->ip_summed == CHECKSUM_PARTIAL)
648 tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
649 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
650 /* remote but checksummed. */
651 tx->flags |= XEN_NETTXF_data_validated;
653 /* Optional extra info after the first request. */
654 if (skb_shinfo(skb)->gso_size) {
655 struct xen_netif_extra_info *gso;
657 gso = (struct xen_netif_extra_info *)
658 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
660 tx->flags |= XEN_NETTXF_extra_info;
662 gso->u.gso.size = skb_shinfo(skb)->gso_size;
663 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
664 XEN_NETIF_GSO_TYPE_TCPV6 :
665 XEN_NETIF_GSO_TYPE_TCPV4;
667 gso->u.gso.features = 0;
669 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
673 /* Requests for the rest of the linear area. */
674 tx = xennet_make_txreqs(queue, tx, skb, page, offset, len);
676 /* Requests for all the frags. */
677 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
678 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
679 tx = xennet_make_txreqs(queue, tx, skb, skb_frag_page(frag),
681 skb_frag_size(frag));
684 /* First request has the packet length. */
685 first_tx->size = skb->len;
687 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
689 notify_remote_via_irq(queue->tx_irq);
691 u64_stats_update_begin(&tx_stats->syncp);
692 tx_stats->bytes += skb->len;
694 u64_stats_update_end(&tx_stats->syncp);
696 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
697 xennet_tx_buf_gc(queue);
699 if (!netfront_tx_slot_available(queue))
700 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
702 spin_unlock_irqrestore(&queue->tx_lock, flags);
707 dev->stats.tx_dropped++;
708 dev_kfree_skb_any(skb);
712 static int xennet_close(struct net_device *dev)
714 struct netfront_info *np = netdev_priv(dev);
715 unsigned int num_queues = dev->real_num_tx_queues;
717 struct netfront_queue *queue;
718 netif_tx_stop_all_queues(np->netdev);
719 for (i = 0; i < num_queues; ++i) {
720 queue = &np->queues[i];
721 napi_disable(&queue->napi);
726 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
729 int new = xennet_rxidx(queue->rx.req_prod_pvt);
731 BUG_ON(queue->rx_skbs[new]);
732 queue->rx_skbs[new] = skb;
733 queue->grant_rx_ref[new] = ref;
734 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
735 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
736 queue->rx.req_prod_pvt++;
739 static int xennet_get_extras(struct netfront_queue *queue,
740 struct xen_netif_extra_info *extras,
744 struct xen_netif_extra_info *extra;
745 struct device *dev = &queue->info->netdev->dev;
746 RING_IDX cons = queue->rx.rsp_cons;
753 if (unlikely(cons + 1 == rp)) {
755 dev_warn(dev, "Missing extra info\n");
760 extra = (struct xen_netif_extra_info *)
761 RING_GET_RESPONSE(&queue->rx, ++cons);
763 if (unlikely(!extra->type ||
764 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
766 dev_warn(dev, "Invalid extra type: %d\n",
770 memcpy(&extras[extra->type - 1], extra,
774 skb = xennet_get_rx_skb(queue, cons);
775 ref = xennet_get_rx_ref(queue, cons);
776 xennet_move_rx_slot(queue, skb, ref);
777 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
779 queue->rx.rsp_cons = cons;
783 static int xennet_get_responses(struct netfront_queue *queue,
784 struct netfront_rx_info *rinfo, RING_IDX rp,
785 struct sk_buff_head *list)
787 struct xen_netif_rx_response *rx = &rinfo->rx;
788 struct xen_netif_extra_info *extras = rinfo->extras;
789 struct device *dev = &queue->info->netdev->dev;
790 RING_IDX cons = queue->rx.rsp_cons;
791 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
792 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
793 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
798 if (rx->flags & XEN_NETRXF_extra_info) {
799 err = xennet_get_extras(queue, extras, rp);
800 cons = queue->rx.rsp_cons;
804 if (unlikely(rx->status < 0 ||
805 rx->offset + rx->status > XEN_PAGE_SIZE)) {
807 dev_warn(dev, "rx->offset: %u, size: %d\n",
808 rx->offset, rx->status);
809 xennet_move_rx_slot(queue, skb, ref);
815 * This definitely indicates a bug, either in this driver or in
816 * the backend driver. In future this should flag the bad
817 * situation to the system controller to reboot the backend.
819 if (ref == GRANT_INVALID_REF) {
821 dev_warn(dev, "Bad rx response id %d.\n",
827 ret = gnttab_end_foreign_access_ref(ref, 0);
830 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
832 __skb_queue_tail(list, skb);
835 if (!(rx->flags & XEN_NETRXF_more_data))
838 if (cons + slots == rp) {
840 dev_warn(dev, "Need more slots\n");
845 rx = RING_GET_RESPONSE(&queue->rx, cons + slots);
846 skb = xennet_get_rx_skb(queue, cons + slots);
847 ref = xennet_get_rx_ref(queue, cons + slots);
851 if (unlikely(slots > max)) {
853 dev_warn(dev, "Too many slots\n");
858 queue->rx.rsp_cons = cons + slots;
863 static int xennet_set_skb_gso(struct sk_buff *skb,
864 struct xen_netif_extra_info *gso)
866 if (!gso->u.gso.size) {
868 pr_warn("GSO size must not be zero\n");
872 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
873 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
875 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
879 skb_shinfo(skb)->gso_size = gso->u.gso.size;
880 skb_shinfo(skb)->gso_type =
881 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
885 /* Header must be checked, and gso_segs computed. */
886 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
887 skb_shinfo(skb)->gso_segs = 0;
892 static int xennet_fill_frags(struct netfront_queue *queue,
894 struct sk_buff_head *list)
896 RING_IDX cons = queue->rx.rsp_cons;
897 struct sk_buff *nskb;
899 while ((nskb = __skb_dequeue(list))) {
900 struct xen_netif_rx_response *rx =
901 RING_GET_RESPONSE(&queue->rx, ++cons);
902 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
904 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
905 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
907 BUG_ON(pull_to < skb_headlen(skb));
908 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
910 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
911 queue->rx.rsp_cons = ++cons + skb_queue_len(list);
916 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
917 skb_frag_page(nfrag),
918 rx->offset, rx->status, PAGE_SIZE);
920 skb_shinfo(nskb)->nr_frags = 0;
924 queue->rx.rsp_cons = cons;
929 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
931 bool recalculate_partial_csum = false;
934 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
935 * peers can fail to set NETRXF_csum_blank when sending a GSO
936 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
937 * recalculate the partial checksum.
939 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
940 struct netfront_info *np = netdev_priv(dev);
941 atomic_inc(&np->rx_gso_checksum_fixup);
942 skb->ip_summed = CHECKSUM_PARTIAL;
943 recalculate_partial_csum = true;
946 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
947 if (skb->ip_summed != CHECKSUM_PARTIAL)
950 return skb_checksum_setup(skb, recalculate_partial_csum);
953 static int handle_incoming_queue(struct netfront_queue *queue,
954 struct sk_buff_head *rxq)
956 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
957 int packets_dropped = 0;
960 while ((skb = __skb_dequeue(rxq)) != NULL) {
961 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
963 if (pull_to > skb_headlen(skb))
964 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
966 /* Ethernet work: Delayed to here as it peeks the header. */
967 skb->protocol = eth_type_trans(skb, queue->info->netdev);
968 skb_reset_network_header(skb);
970 if (checksum_setup(queue->info->netdev, skb)) {
973 queue->info->netdev->stats.rx_errors++;
977 u64_stats_update_begin(&rx_stats->syncp);
979 rx_stats->bytes += skb->len;
980 u64_stats_update_end(&rx_stats->syncp);
983 napi_gro_receive(&queue->napi, skb);
986 return packets_dropped;
989 static int xennet_poll(struct napi_struct *napi, int budget)
991 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
992 struct net_device *dev = queue->info->netdev;
994 struct netfront_rx_info rinfo;
995 struct xen_netif_rx_response *rx = &rinfo.rx;
996 struct xen_netif_extra_info *extras = rinfo.extras;
999 struct sk_buff_head rxq;
1000 struct sk_buff_head errq;
1001 struct sk_buff_head tmpq;
1004 spin_lock(&queue->rx_lock);
1006 skb_queue_head_init(&rxq);
1007 skb_queue_head_init(&errq);
1008 skb_queue_head_init(&tmpq);
1010 rp = queue->rx.sring->rsp_prod;
1011 rmb(); /* Ensure we see queued responses up to 'rp'. */
1013 i = queue->rx.rsp_cons;
1015 while ((i != rp) && (work_done < budget)) {
1016 memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx));
1017 memset(extras, 0, sizeof(rinfo.extras));
1019 err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
1021 if (unlikely(err)) {
1023 while ((skb = __skb_dequeue(&tmpq)))
1024 __skb_queue_tail(&errq, skb);
1025 dev->stats.rx_errors++;
1026 i = queue->rx.rsp_cons;
1030 skb = __skb_dequeue(&tmpq);
1032 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1033 struct xen_netif_extra_info *gso;
1034 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1036 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1037 __skb_queue_head(&tmpq, skb);
1038 queue->rx.rsp_cons += skb_queue_len(&tmpq);
1043 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1044 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1045 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
1047 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
1048 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1049 skb->data_len = rx->status;
1050 skb->len += rx->status;
1052 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
1055 if (rx->flags & XEN_NETRXF_csum_blank)
1056 skb->ip_summed = CHECKSUM_PARTIAL;
1057 else if (rx->flags & XEN_NETRXF_data_validated)
1058 skb->ip_summed = CHECKSUM_UNNECESSARY;
1060 __skb_queue_tail(&rxq, skb);
1062 i = ++queue->rx.rsp_cons;
1066 __skb_queue_purge(&errq);
1068 work_done -= handle_incoming_queue(queue, &rxq);
1070 xennet_alloc_rx_buffers(queue);
1072 if (work_done < budget) {
1075 napi_complete_done(napi, work_done);
1077 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1079 napi_schedule(napi);
1082 spin_unlock(&queue->rx_lock);
1087 static int xennet_change_mtu(struct net_device *dev, int mtu)
1089 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
1097 static void xennet_get_stats64(struct net_device *dev,
1098 struct rtnl_link_stats64 *tot)
1100 struct netfront_info *np = netdev_priv(dev);
1103 for_each_possible_cpu(cpu) {
1104 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1105 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
1106 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1110 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1111 tx_packets = tx_stats->packets;
1112 tx_bytes = tx_stats->bytes;
1113 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
1116 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1117 rx_packets = rx_stats->packets;
1118 rx_bytes = rx_stats->bytes;
1119 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
1121 tot->rx_packets += rx_packets;
1122 tot->tx_packets += tx_packets;
1123 tot->rx_bytes += rx_bytes;
1124 tot->tx_bytes += tx_bytes;
1127 tot->rx_errors = dev->stats.rx_errors;
1128 tot->tx_dropped = dev->stats.tx_dropped;
1131 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1133 struct sk_buff *skb;
1136 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1137 /* Skip over entries which are actually freelist references */
1138 if (skb_entry_is_link(&queue->tx_skbs[i]))
1141 skb = queue->tx_skbs[i].skb;
1142 get_page(queue->grant_tx_page[i]);
1143 gnttab_end_foreign_access(queue->grant_tx_ref[i],
1145 (unsigned long)page_address(queue->grant_tx_page[i]));
1146 queue->grant_tx_page[i] = NULL;
1147 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1148 add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, i);
1149 dev_kfree_skb_irq(skb);
1153 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1157 spin_lock_bh(&queue->rx_lock);
1159 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1160 struct sk_buff *skb;
1163 skb = queue->rx_skbs[id];
1167 ref = queue->grant_rx_ref[id];
1168 if (ref == GRANT_INVALID_REF)
1171 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1173 /* gnttab_end_foreign_access() needs a page ref until
1174 * foreign access is ended (which may be deferred).
1177 gnttab_end_foreign_access(ref, 0,
1178 (unsigned long)page_address(page));
1179 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
1184 spin_unlock_bh(&queue->rx_lock);
1187 static netdev_features_t xennet_fix_features(struct net_device *dev,
1188 netdev_features_t features)
1190 struct netfront_info *np = netdev_priv(dev);
1192 if (features & NETIF_F_SG &&
1193 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1194 features &= ~NETIF_F_SG;
1196 if (features & NETIF_F_IPV6_CSUM &&
1197 !xenbus_read_unsigned(np->xbdev->otherend,
1198 "feature-ipv6-csum-offload", 0))
1199 features &= ~NETIF_F_IPV6_CSUM;
1201 if (features & NETIF_F_TSO &&
1202 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1203 features &= ~NETIF_F_TSO;
1205 if (features & NETIF_F_TSO6 &&
1206 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1207 features &= ~NETIF_F_TSO6;
1212 static int xennet_set_features(struct net_device *dev,
1213 netdev_features_t features)
1215 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1216 netdev_info(dev, "Reducing MTU because no SG offload");
1217 dev->mtu = ETH_DATA_LEN;
1223 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1225 struct netfront_queue *queue = dev_id;
1226 unsigned long flags;
1228 spin_lock_irqsave(&queue->tx_lock, flags);
1229 xennet_tx_buf_gc(queue);
1230 spin_unlock_irqrestore(&queue->tx_lock, flags);
1235 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1237 struct netfront_queue *queue = dev_id;
1238 struct net_device *dev = queue->info->netdev;
1240 if (likely(netif_carrier_ok(dev) &&
1241 RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
1242 napi_schedule(&queue->napi);
1247 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1249 xennet_tx_interrupt(irq, dev_id);
1250 xennet_rx_interrupt(irq, dev_id);
1254 #ifdef CONFIG_NET_POLL_CONTROLLER
1255 static void xennet_poll_controller(struct net_device *dev)
1257 /* Poll each queue */
1258 struct netfront_info *info = netdev_priv(dev);
1259 unsigned int num_queues = dev->real_num_tx_queues;
1261 for (i = 0; i < num_queues; ++i)
1262 xennet_interrupt(0, &info->queues[i]);
1266 static const struct net_device_ops xennet_netdev_ops = {
1267 .ndo_open = xennet_open,
1268 .ndo_stop = xennet_close,
1269 .ndo_start_xmit = xennet_start_xmit,
1270 .ndo_change_mtu = xennet_change_mtu,
1271 .ndo_get_stats64 = xennet_get_stats64,
1272 .ndo_set_mac_address = eth_mac_addr,
1273 .ndo_validate_addr = eth_validate_addr,
1274 .ndo_fix_features = xennet_fix_features,
1275 .ndo_set_features = xennet_set_features,
1276 .ndo_select_queue = xennet_select_queue,
1277 #ifdef CONFIG_NET_POLL_CONTROLLER
1278 .ndo_poll_controller = xennet_poll_controller,
1282 static void xennet_free_netdev(struct net_device *netdev)
1284 struct netfront_info *np = netdev_priv(netdev);
1286 free_percpu(np->rx_stats);
1287 free_percpu(np->tx_stats);
1288 free_netdev(netdev);
1291 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1294 struct net_device *netdev;
1295 struct netfront_info *np;
1297 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1299 return ERR_PTR(-ENOMEM);
1301 np = netdev_priv(netdev);
1307 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1308 if (np->rx_stats == NULL)
1310 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1311 if (np->tx_stats == NULL)
1314 netdev->netdev_ops = &xennet_netdev_ops;
1316 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1318 netdev->hw_features = NETIF_F_SG |
1320 NETIF_F_TSO | NETIF_F_TSO6;
1323 * Assume that all hw features are available for now. This set
1324 * will be adjusted by the call to netdev_update_features() in
1325 * xennet_connect() which is the earliest point where we can
1326 * negotiate with the backend regarding supported features.
1328 netdev->features |= netdev->hw_features;
1330 netdev->ethtool_ops = &xennet_ethtool_ops;
1331 netdev->min_mtu = ETH_MIN_MTU;
1332 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
1333 SET_NETDEV_DEV(netdev, &dev->dev);
1335 np->netdev = netdev;
1337 netif_carrier_off(netdev);
1340 xenbus_switch_state(dev, XenbusStateInitialising);
1341 err = wait_event_timeout(module_wq,
1342 xenbus_read_driver_state(dev->otherend) !=
1343 XenbusStateClosed &&
1344 xenbus_read_driver_state(dev->otherend) !=
1345 XenbusStateUnknown, XENNET_TIMEOUT);
1351 xennet_free_netdev(netdev);
1352 return ERR_PTR(err);
1356 * Entry point to this code when a new device is created. Allocate the basic
1357 * structures and the ring buffers for communication with the backend, and
1358 * inform the backend of the appropriate details for those.
1360 static int netfront_probe(struct xenbus_device *dev,
1361 const struct xenbus_device_id *id)
1364 struct net_device *netdev;
1365 struct netfront_info *info;
1367 netdev = xennet_create_dev(dev);
1368 if (IS_ERR(netdev)) {
1369 err = PTR_ERR(netdev);
1370 xenbus_dev_fatal(dev, err, "creating netdev");
1374 info = netdev_priv(netdev);
1375 dev_set_drvdata(&dev->dev, info);
1377 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1383 static void xennet_end_access(int ref, void *page)
1385 /* This frees the page as a side-effect */
1386 if (ref != GRANT_INVALID_REF)
1387 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1390 static void xennet_disconnect_backend(struct netfront_info *info)
1393 unsigned int num_queues = info->netdev->real_num_tx_queues;
1395 netif_carrier_off(info->netdev);
1397 for (i = 0; i < num_queues && info->queues; ++i) {
1398 struct netfront_queue *queue = &info->queues[i];
1400 del_timer_sync(&queue->rx_refill_timer);
1402 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1403 unbind_from_irqhandler(queue->tx_irq, queue);
1404 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1405 unbind_from_irqhandler(queue->tx_irq, queue);
1406 unbind_from_irqhandler(queue->rx_irq, queue);
1408 queue->tx_evtchn = queue->rx_evtchn = 0;
1409 queue->tx_irq = queue->rx_irq = 0;
1411 if (netif_running(info->netdev))
1412 napi_synchronize(&queue->napi);
1414 xennet_release_tx_bufs(queue);
1415 xennet_release_rx_bufs(queue);
1416 gnttab_free_grant_references(queue->gref_tx_head);
1417 gnttab_free_grant_references(queue->gref_rx_head);
1419 /* End access and free the pages */
1420 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1421 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1423 queue->tx_ring_ref = GRANT_INVALID_REF;
1424 queue->rx_ring_ref = GRANT_INVALID_REF;
1425 queue->tx.sring = NULL;
1426 queue->rx.sring = NULL;
1431 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1432 * driver restart. We tear down our netif structure and recreate it, but
1433 * leave the device-layer structures intact so that this is transparent to the
1434 * rest of the kernel.
1436 static int netfront_resume(struct xenbus_device *dev)
1438 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1440 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1442 xennet_disconnect_backend(info);
1446 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1448 char *s, *e, *macstr;
1451 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1453 return PTR_ERR(macstr);
1455 for (i = 0; i < ETH_ALEN; i++) {
1456 mac[i] = simple_strtoul(s, &e, 16);
1457 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1468 static int setup_netfront_single(struct netfront_queue *queue)
1472 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1476 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
1478 0, queue->info->netdev->name, queue);
1481 queue->rx_evtchn = queue->tx_evtchn;
1482 queue->rx_irq = queue->tx_irq = err;
1487 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1488 queue->tx_evtchn = 0;
1493 static int setup_netfront_split(struct netfront_queue *queue)
1497 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1500 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1502 goto alloc_rx_evtchn_fail;
1504 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1505 "%s-tx", queue->name);
1506 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
1507 xennet_tx_interrupt,
1508 0, queue->tx_irq_name, queue);
1511 queue->tx_irq = err;
1513 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1514 "%s-rx", queue->name);
1515 err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
1516 xennet_rx_interrupt,
1517 0, queue->rx_irq_name, queue);
1520 queue->rx_irq = err;
1525 unbind_from_irqhandler(queue->tx_irq, queue);
1528 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1529 queue->rx_evtchn = 0;
1530 alloc_rx_evtchn_fail:
1531 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1532 queue->tx_evtchn = 0;
1537 static int setup_netfront(struct xenbus_device *dev,
1538 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1540 struct xen_netif_tx_sring *txs;
1541 struct xen_netif_rx_sring *rxs;
1545 queue->tx_ring_ref = GRANT_INVALID_REF;
1546 queue->rx_ring_ref = GRANT_INVALID_REF;
1547 queue->rx.sring = NULL;
1548 queue->tx.sring = NULL;
1550 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1553 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1556 SHARED_RING_INIT(txs);
1557 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1559 err = xenbus_grant_ring(dev, txs, 1, &gref);
1561 goto grant_tx_ring_fail;
1562 queue->tx_ring_ref = gref;
1564 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1567 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1568 goto alloc_rx_ring_fail;
1570 SHARED_RING_INIT(rxs);
1571 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1573 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1575 goto grant_rx_ring_fail;
1576 queue->rx_ring_ref = gref;
1578 if (feature_split_evtchn)
1579 err = setup_netfront_split(queue);
1580 /* setup single event channel if
1581 * a) feature-split-event-channels == 0
1582 * b) feature-split-event-channels == 1 but failed to setup
1584 if (!feature_split_evtchn || (feature_split_evtchn && err))
1585 err = setup_netfront_single(queue);
1588 goto alloc_evtchn_fail;
1592 /* If we fail to setup netfront, it is safe to just revoke access to
1593 * granted pages because backend is not accessing it at this point.
1596 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
1598 free_page((unsigned long)rxs);
1600 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
1602 free_page((unsigned long)txs);
1607 /* Queue-specific initialisation
1608 * This used to be done in xennet_create_dev() but must now
1611 static int xennet_init_queue(struct netfront_queue *queue)
1617 spin_lock_init(&queue->tx_lock);
1618 spin_lock_init(&queue->rx_lock);
1620 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
1622 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
1623 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
1626 /* Initialise tx_skbs as a free chain containing every entry. */
1627 queue->tx_skb_freelist = 0;
1628 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1629 skb_entry_set_link(&queue->tx_skbs[i], i+1);
1630 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1631 queue->grant_tx_page[i] = NULL;
1634 /* Clear out rx_skbs */
1635 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1636 queue->rx_skbs[i] = NULL;
1637 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1640 /* A grant for every tx ring slot */
1641 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
1642 &queue->gref_tx_head) < 0) {
1643 pr_alert("can't alloc tx grant refs\n");
1648 /* A grant for every rx ring slot */
1649 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
1650 &queue->gref_rx_head) < 0) {
1651 pr_alert("can't alloc rx grant refs\n");
1659 gnttab_free_grant_references(queue->gref_tx_head);
1664 static int write_queue_xenstore_keys(struct netfront_queue *queue,
1665 struct xenbus_transaction *xbt, int write_hierarchical)
1667 /* Write the queue-specific keys into XenStore in the traditional
1668 * way for a single queue, or in a queue subkeys for multiple
1671 struct xenbus_device *dev = queue->info->xbdev;
1673 const char *message;
1677 /* Choose the correct place to write the keys */
1678 if (write_hierarchical) {
1679 pathsize = strlen(dev->nodename) + 10;
1680 path = kzalloc(pathsize, GFP_KERNEL);
1683 message = "out of memory while writing ring references";
1686 snprintf(path, pathsize, "%s/queue-%u",
1687 dev->nodename, queue->id);
1689 path = (char *)dev->nodename;
1692 /* Write ring references */
1693 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1694 queue->tx_ring_ref);
1696 message = "writing tx-ring-ref";
1700 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1701 queue->rx_ring_ref);
1703 message = "writing rx-ring-ref";
1707 /* Write event channels; taking into account both shared
1708 * and split event channel scenarios.
1710 if (queue->tx_evtchn == queue->rx_evtchn) {
1711 /* Shared event channel */
1712 err = xenbus_printf(*xbt, path,
1713 "event-channel", "%u", queue->tx_evtchn);
1715 message = "writing event-channel";
1719 /* Split event channels */
1720 err = xenbus_printf(*xbt, path,
1721 "event-channel-tx", "%u", queue->tx_evtchn);
1723 message = "writing event-channel-tx";
1727 err = xenbus_printf(*xbt, path,
1728 "event-channel-rx", "%u", queue->rx_evtchn);
1730 message = "writing event-channel-rx";
1735 if (write_hierarchical)
1740 if (write_hierarchical)
1742 xenbus_dev_fatal(dev, err, "%s", message);
1746 static void xennet_destroy_queues(struct netfront_info *info)
1750 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
1751 struct netfront_queue *queue = &info->queues[i];
1753 if (netif_running(info->netdev))
1754 napi_disable(&queue->napi);
1755 netif_napi_del(&queue->napi);
1758 kfree(info->queues);
1759 info->queues = NULL;
1762 static int xennet_create_queues(struct netfront_info *info,
1763 unsigned int *num_queues)
1768 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
1773 for (i = 0; i < *num_queues; i++) {
1774 struct netfront_queue *queue = &info->queues[i];
1779 ret = xennet_init_queue(queue);
1781 dev_warn(&info->xbdev->dev,
1782 "only created %d queues\n", i);
1787 netif_napi_add(queue->info->netdev, &queue->napi,
1789 if (netif_running(info->netdev))
1790 napi_enable(&queue->napi);
1793 netif_set_real_num_tx_queues(info->netdev, *num_queues);
1795 if (*num_queues == 0) {
1796 dev_err(&info->xbdev->dev, "no queues\n");
1802 /* Common code used when first setting up, and when resuming. */
1803 static int talk_to_netback(struct xenbus_device *dev,
1804 struct netfront_info *info)
1806 const char *message;
1807 struct xenbus_transaction xbt;
1809 unsigned int feature_split_evtchn;
1811 unsigned int max_queues = 0;
1812 struct netfront_queue *queue = NULL;
1813 unsigned int num_queues = 1;
1815 info->netdev->irq = 0;
1817 /* Check if backend supports multiple queues */
1818 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
1819 "multi-queue-max-queues", 1);
1820 num_queues = min(max_queues, xennet_max_queues);
1822 /* Check feature-split-event-channels */
1823 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
1824 "feature-split-event-channels", 0);
1826 /* Read mac addr. */
1827 err = xen_net_read_mac(dev, info->netdev->dev_addr);
1829 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1835 xennet_destroy_queues(info);
1837 err = xennet_create_queues(info, &num_queues);
1839 xenbus_dev_fatal(dev, err, "creating queues");
1840 kfree(info->queues);
1841 info->queues = NULL;
1846 /* Create shared ring, alloc event channel -- for each queue */
1847 for (i = 0; i < num_queues; ++i) {
1848 queue = &info->queues[i];
1849 err = setup_netfront(dev, queue, feature_split_evtchn);
1855 err = xenbus_transaction_start(&xbt);
1857 xenbus_dev_fatal(dev, err, "starting transaction");
1861 if (xenbus_exists(XBT_NIL,
1862 info->xbdev->otherend, "multi-queue-max-queues")) {
1863 /* Write the number of queues */
1864 err = xenbus_printf(xbt, dev->nodename,
1865 "multi-queue-num-queues", "%u", num_queues);
1867 message = "writing multi-queue-num-queues";
1868 goto abort_transaction_no_dev_fatal;
1872 if (num_queues == 1) {
1873 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
1875 goto abort_transaction_no_dev_fatal;
1877 /* Write the keys for each queue */
1878 for (i = 0; i < num_queues; ++i) {
1879 queue = &info->queues[i];
1880 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
1882 goto abort_transaction_no_dev_fatal;
1886 /* The remaining keys are not queue-specific */
1887 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1890 message = "writing request-rx-copy";
1891 goto abort_transaction;
1894 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1896 message = "writing feature-rx-notify";
1897 goto abort_transaction;
1900 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1902 message = "writing feature-sg";
1903 goto abort_transaction;
1906 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1908 message = "writing feature-gso-tcpv4";
1909 goto abort_transaction;
1912 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
1914 message = "writing feature-gso-tcpv6";
1915 goto abort_transaction;
1918 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
1921 message = "writing feature-ipv6-csum-offload";
1922 goto abort_transaction;
1925 err = xenbus_transaction_end(xbt, 0);
1929 xenbus_dev_fatal(dev, err, "completing transaction");
1936 xenbus_dev_fatal(dev, err, "%s", message);
1937 abort_transaction_no_dev_fatal:
1938 xenbus_transaction_end(xbt, 1);
1940 xennet_disconnect_backend(info);
1942 xennet_destroy_queues(info);
1946 device_unregister(&dev->dev);
1950 static int xennet_connect(struct net_device *dev)
1952 struct netfront_info *np = netdev_priv(dev);
1953 unsigned int num_queues = 0;
1956 struct netfront_queue *queue = NULL;
1958 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
1960 "backend does not support copying receive path\n");
1964 err = talk_to_netback(np->xbdev, np);
1968 /* talk_to_netback() sets the correct number of queues */
1969 num_queues = dev->real_num_tx_queues;
1971 if (dev->reg_state == NETREG_UNINITIALIZED) {
1972 err = register_netdev(dev);
1974 pr_warn("%s: register_netdev err=%d\n", __func__, err);
1975 device_unregister(&np->xbdev->dev);
1981 netdev_update_features(dev);
1985 * All public and private state should now be sane. Get
1986 * ready to start sending and receiving packets and give the driver
1987 * domain a kick because we've probably just requeued some
1990 netif_carrier_on(np->netdev);
1991 for (j = 0; j < num_queues; ++j) {
1992 queue = &np->queues[j];
1994 notify_remote_via_irq(queue->tx_irq);
1995 if (queue->tx_irq != queue->rx_irq)
1996 notify_remote_via_irq(queue->rx_irq);
1998 spin_lock_irq(&queue->tx_lock);
1999 xennet_tx_buf_gc(queue);
2000 spin_unlock_irq(&queue->tx_lock);
2002 spin_lock_bh(&queue->rx_lock);
2003 xennet_alloc_rx_buffers(queue);
2004 spin_unlock_bh(&queue->rx_lock);
2011 * Callback received when the backend's state changes.
2013 static void netback_changed(struct xenbus_device *dev,
2014 enum xenbus_state backend_state)
2016 struct netfront_info *np = dev_get_drvdata(&dev->dev);
2017 struct net_device *netdev = np->netdev;
2019 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2021 wake_up_all(&module_wq);
2023 switch (backend_state) {
2024 case XenbusStateInitialising:
2025 case XenbusStateInitialised:
2026 case XenbusStateReconfiguring:
2027 case XenbusStateReconfigured:
2028 case XenbusStateUnknown:
2031 case XenbusStateInitWait:
2032 if (dev->state != XenbusStateInitialising)
2034 if (xennet_connect(netdev) != 0)
2036 xenbus_switch_state(dev, XenbusStateConnected);
2039 case XenbusStateConnected:
2040 netdev_notify_peers(netdev);
2043 case XenbusStateClosed:
2044 if (dev->state == XenbusStateClosed)
2046 /* Fall through - Missed the backend's CLOSING state. */
2047 case XenbusStateClosing:
2048 xenbus_frontend_closed(dev);
2053 static const struct xennet_stat {
2054 char name[ETH_GSTRING_LEN];
2056 } xennet_stats[] = {
2058 "rx_gso_checksum_fixup",
2059 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2063 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2065 switch (string_set) {
2067 return ARRAY_SIZE(xennet_stats);
2073 static void xennet_get_ethtool_stats(struct net_device *dev,
2074 struct ethtool_stats *stats, u64 * data)
2076 void *np = netdev_priv(dev);
2079 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2080 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2083 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2087 switch (stringset) {
2089 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2090 memcpy(data + i * ETH_GSTRING_LEN,
2091 xennet_stats[i].name, ETH_GSTRING_LEN);
2096 static const struct ethtool_ops xennet_ethtool_ops =
2098 .get_link = ethtool_op_get_link,
2100 .get_sset_count = xennet_get_sset_count,
2101 .get_ethtool_stats = xennet_get_ethtool_stats,
2102 .get_strings = xennet_get_strings,
2106 static ssize_t show_rxbuf(struct device *dev,
2107 struct device_attribute *attr, char *buf)
2109 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2112 static ssize_t store_rxbuf(struct device *dev,
2113 struct device_attribute *attr,
2114 const char *buf, size_t len)
2117 unsigned long target;
2119 if (!capable(CAP_NET_ADMIN))
2122 target = simple_strtoul(buf, &endp, 0);
2126 /* rxbuf_min and rxbuf_max are no longer configurable. */
2131 static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2132 static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2133 static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
2135 static struct attribute *xennet_dev_attrs[] = {
2136 &dev_attr_rxbuf_min.attr,
2137 &dev_attr_rxbuf_max.attr,
2138 &dev_attr_rxbuf_cur.attr,
2142 static const struct attribute_group xennet_dev_group = {
2143 .attrs = xennet_dev_attrs
2145 #endif /* CONFIG_SYSFS */
2147 static void xennet_bus_close(struct xenbus_device *dev)
2151 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2154 xenbus_switch_state(dev, XenbusStateClosing);
2155 ret = wait_event_timeout(module_wq,
2156 xenbus_read_driver_state(dev->otherend) ==
2157 XenbusStateClosing ||
2158 xenbus_read_driver_state(dev->otherend) ==
2159 XenbusStateClosed ||
2160 xenbus_read_driver_state(dev->otherend) ==
2165 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2169 xenbus_switch_state(dev, XenbusStateClosed);
2170 ret = wait_event_timeout(module_wq,
2171 xenbus_read_driver_state(dev->otherend) ==
2172 XenbusStateClosed ||
2173 xenbus_read_driver_state(dev->otherend) ==
2179 static int xennet_remove(struct xenbus_device *dev)
2181 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2183 xennet_bus_close(dev);
2184 xennet_disconnect_backend(info);
2186 if (info->netdev->reg_state == NETREG_REGISTERED)
2187 unregister_netdev(info->netdev);
2191 xennet_destroy_queues(info);
2194 xennet_free_netdev(info->netdev);
2199 static const struct xenbus_device_id netfront_ids[] = {
2204 static struct xenbus_driver netfront_driver = {
2205 .ids = netfront_ids,
2206 .probe = netfront_probe,
2207 .remove = xennet_remove,
2208 .resume = netfront_resume,
2209 .otherend_changed = netback_changed,
2212 static int __init netif_init(void)
2217 if (!xen_has_pv_nic_devices())
2220 pr_info("Initialising Xen virtual ethernet driver\n");
2222 /* Allow as many queues as there are CPUs inut max. 8 if user has not
2223 * specified a value.
2225 if (xennet_max_queues == 0)
2226 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2229 return xenbus_register_frontend(&netfront_driver);
2231 module_init(netif_init);
2234 static void __exit netif_exit(void)
2236 xenbus_unregister_driver(&netfront_driver);
2238 module_exit(netif_exit);
2240 MODULE_DESCRIPTION("Xen virtual network device frontend");
2241 MODULE_LICENSE("GPL");
2242 MODULE_ALIAS("xen:vif");
2243 MODULE_ALIAS("xennet");