2 * Virtual network driver for conversing with remote driver backends.
4 * Copyright (c) 2002-2005, K A Fraser
5 * Copyright (c) 2005, XenSource Ltd
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation; or, when distributed
10 * separately from the Linux kernel or incorporated into other
11 * software packages, subject to the following license:
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/ethtool.h>
40 #include <linux/if_ether.h>
42 #include <linux/udp.h>
43 #include <linux/moduleparam.h>
45 #include <linux/slab.h>
49 #include <xen/xenbus.h>
50 #include <xen/events.h>
52 #include <xen/platform_pci.h>
53 #include <xen/grant_table.h>
55 #include <xen/interface/io/netif.h>
56 #include <xen/interface/memory.h>
57 #include <xen/interface/grant_table.h>
59 /* Module parameters */
60 static unsigned int xennet_max_queues;
61 module_param_named(max_queues, xennet_max_queues, uint, 0644);
62 MODULE_PARM_DESC(max_queues,
63 "Maximum number of queues per virtual interface");
65 #define XENNET_TIMEOUT (5 * HZ)
67 static const struct ethtool_ops xennet_ethtool_ops;
73 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
75 #define RX_COPY_THRESHOLD 256
77 #define GRANT_INVALID_REF 0
79 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
80 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
82 /* Minimum number of Rx slots (includes slot for GSO metadata). */
83 #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
85 /* Queue name is interface name with "-qNNN" appended */
86 #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
88 /* IRQ name is queue name with "-tx" or "-rx" appended */
89 #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
91 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
93 struct netfront_stats {
96 struct u64_stats_sync syncp;
101 struct netfront_queue {
102 unsigned int id; /* Queue ID, 0-based */
103 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
104 struct netfront_info *info;
106 struct napi_struct napi;
108 /* Split event channels support, tx_* == rx_* when using
109 * single event channel.
111 unsigned int tx_evtchn, rx_evtchn;
112 unsigned int tx_irq, rx_irq;
113 /* Only used when split event channels support is enabled */
114 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
115 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
118 struct xen_netif_tx_front_ring tx;
122 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
123 * are linked from tx_skb_freelist through tx_link.
125 struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
126 unsigned short tx_link[NET_TX_RING_SIZE];
127 #define TX_LINK_NONE 0xffff
128 #define TX_PENDING 0xfffe
129 grant_ref_t gref_tx_head;
130 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
131 struct page *grant_tx_page[NET_TX_RING_SIZE];
132 unsigned tx_skb_freelist;
133 unsigned int tx_pend_queue;
135 spinlock_t rx_lock ____cacheline_aligned_in_smp;
136 struct xen_netif_rx_front_ring rx;
139 struct timer_list rx_refill_timer;
141 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
142 grant_ref_t gref_rx_head;
143 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
146 struct netfront_info {
147 struct list_head list;
148 struct net_device *netdev;
150 struct xenbus_device *xbdev;
152 /* Multi-queue support */
153 struct netfront_queue *queues;
156 struct netfront_stats __percpu *rx_stats;
157 struct netfront_stats __percpu *tx_stats;
159 /* Is device behaving sane? */
162 atomic_t rx_gso_checksum_fixup;
165 struct netfront_rx_info {
166 struct xen_netif_rx_response rx;
167 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
171 * Access macros for acquiring freeing slots in tx_skbs[].
174 static void add_id_to_list(unsigned *head, unsigned short *list,
181 static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
183 unsigned int id = *head;
185 if (id != TX_LINK_NONE) {
187 list[id] = TX_LINK_NONE;
192 static int xennet_rxidx(RING_IDX idx)
194 return idx & (NET_RX_RING_SIZE - 1);
197 static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
200 int i = xennet_rxidx(ri);
201 struct sk_buff *skb = queue->rx_skbs[i];
202 queue->rx_skbs[i] = NULL;
206 static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
209 int i = xennet_rxidx(ri);
210 grant_ref_t ref = queue->grant_rx_ref[i];
211 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
216 static const struct attribute_group xennet_dev_group;
219 static bool xennet_can_sg(struct net_device *dev)
221 return dev->features & NETIF_F_SG;
225 static void rx_refill_timeout(unsigned long data)
227 struct netfront_queue *queue = (struct netfront_queue *)data;
228 napi_schedule(&queue->napi);
231 static int netfront_tx_slot_available(struct netfront_queue *queue)
233 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
234 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
237 static void xennet_maybe_wake_tx(struct netfront_queue *queue)
239 struct net_device *dev = queue->info->netdev;
240 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
242 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
243 netfront_tx_slot_available(queue) &&
244 likely(netif_running(dev)))
245 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
249 static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
254 skb = __netdev_alloc_skb(queue->info->netdev,
255 RX_COPY_THRESHOLD + NET_IP_ALIGN,
256 GFP_ATOMIC | __GFP_NOWARN);
260 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
265 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
267 /* Align ip header to a 16 bytes boundary */
268 skb_reserve(skb, NET_IP_ALIGN);
269 skb->dev = queue->info->netdev;
275 static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
277 RING_IDX req_prod = queue->rx.req_prod_pvt;
281 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
284 for (req_prod = queue->rx.req_prod_pvt;
285 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
291 struct xen_netif_rx_request *req;
293 skb = xennet_alloc_one_rx_buffer(queue);
299 id = xennet_rxidx(req_prod);
301 BUG_ON(queue->rx_skbs[id]);
302 queue->rx_skbs[id] = skb;
304 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
305 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
306 queue->grant_rx_ref[id] = ref;
308 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
310 req = RING_GET_REQUEST(&queue->rx, req_prod);
311 gnttab_page_grant_foreign_access_ref_one(ref,
312 queue->info->xbdev->otherend_id,
319 queue->rx.req_prod_pvt = req_prod;
321 /* Try again later if there are not enough requests or skb allocation
323 * Enough requests is quantified as the sum of newly created slots and
324 * the unconsumed slots at the backend.
326 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
328 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
332 wmb(); /* barrier so backend seens requests */
334 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
336 notify_remote_via_irq(queue->rx_irq);
339 static int xennet_open(struct net_device *dev)
341 struct netfront_info *np = netdev_priv(dev);
342 unsigned int num_queues = dev->real_num_tx_queues;
344 struct netfront_queue *queue = NULL;
346 if (!np->queues || np->broken)
349 for (i = 0; i < num_queues; ++i) {
350 queue = &np->queues[i];
351 napi_enable(&queue->napi);
353 spin_lock_bh(&queue->rx_lock);
354 if (netif_carrier_ok(dev)) {
355 xennet_alloc_rx_buffers(queue);
356 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
357 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
358 napi_schedule(&queue->napi);
360 spin_unlock_bh(&queue->rx_lock);
363 netif_tx_start_all_queues(dev);
368 static void xennet_tx_buf_gc(struct netfront_queue *queue)
373 const struct device *dev = &queue->info->netdev->dev;
375 BUG_ON(!netif_carrier_ok(queue->info->netdev));
378 prod = queue->tx.sring->rsp_prod;
379 if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
380 dev_alert(dev, "Illegal number of responses %u\n",
381 prod - queue->tx.rsp_cons);
384 rmb(); /* Ensure we see responses up to 'rp'. */
386 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
387 struct xen_netif_tx_response txrsp;
389 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
390 if (txrsp.status == XEN_NETIF_RSP_NULL)
394 if (id >= RING_SIZE(&queue->tx)) {
396 "Response has incorrect id (%u)\n",
400 if (queue->tx_link[id] != TX_PENDING) {
402 "Response for inactive request\n");
406 queue->tx_link[id] = TX_LINK_NONE;
407 skb = queue->tx_skbs[id];
408 queue->tx_skbs[id] = NULL;
409 if (unlikely(gnttab_query_foreign_access(
410 queue->grant_tx_ref[id]) != 0)) {
412 "Grant still in use by backend domain\n");
415 gnttab_end_foreign_access_ref(
416 queue->grant_tx_ref[id], GNTMAP_readonly);
417 gnttab_release_grant_reference(
418 &queue->gref_tx_head, queue->grant_tx_ref[id]);
419 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
420 queue->grant_tx_page[id] = NULL;
421 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
422 dev_kfree_skb_irq(skb);
425 queue->tx.rsp_cons = prod;
428 * Set a new event, then check for race with update of tx_cons.
429 * Note that it is essential to schedule a callback, no matter
430 * how few buffers are pending. Even if there is space in the
431 * transmit ring, higher layers may be blocked because too much
432 * data is outstanding: in such cases notification from Xen is
433 * likely to be the only kick that we'll get.
435 queue->tx.sring->rsp_event =
436 prod + ((queue->tx.sring->req_prod - prod) >> 1) + 1;
437 mb(); /* update shared area */
438 } while ((cons == prod) && (prod != queue->tx.sring->rsp_prod));
440 xennet_maybe_wake_tx(queue);
445 queue->info->broken = true;
446 dev_alert(dev, "Disabled for further use\n");
449 struct xennet_gnttab_make_txreq {
450 struct netfront_queue *queue;
453 struct xen_netif_tx_request *tx; /* Last request on ring page */
454 struct xen_netif_tx_request tx_local; /* Last request local copy*/
458 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
459 unsigned int len, void *data)
461 struct xennet_gnttab_make_txreq *info = data;
463 struct xen_netif_tx_request *tx;
465 /* convenient aliases */
466 struct page *page = info->page;
467 struct netfront_queue *queue = info->queue;
468 struct sk_buff *skb = info->skb;
470 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
471 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
472 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
473 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
475 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
476 gfn, GNTMAP_readonly);
478 queue->tx_skbs[id] = skb;
479 queue->grant_tx_page[id] = page;
480 queue->grant_tx_ref[id] = ref;
482 info->tx_local.id = id;
483 info->tx_local.gref = ref;
484 info->tx_local.offset = offset;
485 info->tx_local.size = len;
486 info->tx_local.flags = 0;
488 *tx = info->tx_local;
491 * Put the request in the pending queue, it will be set to be pending
492 * when the producer index is about to be raised.
494 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
497 info->size += info->tx_local.size;
500 static struct xen_netif_tx_request *xennet_make_first_txreq(
501 struct xennet_gnttab_make_txreq *info,
502 unsigned int offset, unsigned int len)
506 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
511 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
512 unsigned int len, void *data)
514 struct xennet_gnttab_make_txreq *info = data;
516 info->tx->flags |= XEN_NETTXF_more_data;
518 xennet_tx_setup_grant(gfn, offset, len, data);
521 static void xennet_make_txreqs(
522 struct xennet_gnttab_make_txreq *info,
524 unsigned int offset, unsigned int len)
526 /* Skip unused frames from start of page */
527 page += offset >> PAGE_SHIFT;
528 offset &= ~PAGE_MASK;
534 gnttab_foreach_grant_in_range(page, offset, len,
535 xennet_make_one_txreq,
545 * Count how many ring slots are required to send this skb. Each frag
546 * might be a compound page.
548 static int xennet_count_skb_slots(struct sk_buff *skb)
550 int i, frags = skb_shinfo(skb)->nr_frags;
553 slots = gnttab_count_grant(offset_in_page(skb->data),
556 for (i = 0; i < frags; i++) {
557 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
558 unsigned long size = skb_frag_size(frag);
559 unsigned long offset = frag->page_offset;
561 /* Skip unused frames from start of page */
562 offset &= ~PAGE_MASK;
564 slots += gnttab_count_grant(offset, size);
570 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
571 void *accel_priv, select_queue_fallback_t fallback)
573 unsigned int num_queues = dev->real_num_tx_queues;
577 /* First, check if there is only one queue */
578 if (num_queues == 1) {
581 hash = skb_get_hash(skb);
582 queue_idx = hash % num_queues;
588 static void xennet_mark_tx_pending(struct netfront_queue *queue)
592 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
594 queue->tx_link[i] = TX_PENDING;
597 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
599 static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
601 struct netfront_info *np = netdev_priv(dev);
602 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
603 struct xen_netif_tx_request *first_tx;
611 struct netfront_queue *queue = NULL;
612 struct xennet_gnttab_make_txreq info = { };
613 unsigned int num_queues = dev->real_num_tx_queues;
616 /* Drop the packet if no queues are set up */
619 if (unlikely(np->broken))
621 /* Determine which queue to transmit this SKB on */
622 queue_index = skb_get_queue_mapping(skb);
623 queue = &np->queues[queue_index];
625 /* If skb->len is too big for wire format, drop skb and alert
626 * user about misconfiguration.
628 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
629 net_alert_ratelimited(
630 "xennet: skb->len = %u, too big for wire format\n",
635 slots = xennet_count_skb_slots(skb);
636 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
637 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
639 if (skb_linearize(skb))
643 page = virt_to_page(skb->data);
644 offset = offset_in_page(skb->data);
645 len = skb_headlen(skb);
647 spin_lock_irqsave(&queue->tx_lock, flags);
649 if (unlikely(!netif_carrier_ok(dev) ||
650 (slots > 1 && !xennet_can_sg(dev)) ||
651 netif_needs_gso(skb, netif_skb_features(skb)))) {
652 spin_unlock_irqrestore(&queue->tx_lock, flags);
656 /* First request for the linear area. */
660 first_tx = xennet_make_first_txreq(&info, offset, len);
661 offset += info.tx_local.size;
662 if (offset == PAGE_SIZE) {
666 len -= info.tx_local.size;
668 if (skb->ip_summed == CHECKSUM_PARTIAL)
670 first_tx->flags |= XEN_NETTXF_csum_blank |
671 XEN_NETTXF_data_validated;
672 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
673 /* remote but checksummed. */
674 first_tx->flags |= XEN_NETTXF_data_validated;
676 /* Optional extra info after the first request. */
677 if (skb_shinfo(skb)->gso_size) {
678 struct xen_netif_extra_info *gso;
680 gso = (struct xen_netif_extra_info *)
681 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
683 first_tx->flags |= XEN_NETTXF_extra_info;
685 gso->u.gso.size = skb_shinfo(skb)->gso_size;
686 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
687 XEN_NETIF_GSO_TYPE_TCPV6 :
688 XEN_NETIF_GSO_TYPE_TCPV4;
690 gso->u.gso.features = 0;
692 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
696 /* Requests for the rest of the linear area. */
697 xennet_make_txreqs(&info, page, offset, len);
699 /* Requests for all the frags. */
700 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
701 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
702 xennet_make_txreqs(&info, skb_frag_page(frag),
704 skb_frag_size(frag));
707 /* First request has the packet length. */
708 first_tx->size = skb->len;
710 xennet_mark_tx_pending(queue);
712 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
714 notify_remote_via_irq(queue->tx_irq);
716 u64_stats_update_begin(&tx_stats->syncp);
717 tx_stats->bytes += skb->len;
719 u64_stats_update_end(&tx_stats->syncp);
721 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
722 xennet_tx_buf_gc(queue);
724 if (!netfront_tx_slot_available(queue))
725 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
727 spin_unlock_irqrestore(&queue->tx_lock, flags);
732 dev->stats.tx_dropped++;
733 dev_kfree_skb_any(skb);
737 static int xennet_close(struct net_device *dev)
739 struct netfront_info *np = netdev_priv(dev);
740 unsigned int num_queues = dev->real_num_tx_queues;
742 struct netfront_queue *queue;
743 netif_tx_stop_all_queues(np->netdev);
744 for (i = 0; i < num_queues; ++i) {
745 queue = &np->queues[i];
746 napi_disable(&queue->napi);
751 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
754 int new = xennet_rxidx(queue->rx.req_prod_pvt);
756 BUG_ON(queue->rx_skbs[new]);
757 queue->rx_skbs[new] = skb;
758 queue->grant_rx_ref[new] = ref;
759 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
760 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
761 queue->rx.req_prod_pvt++;
764 static int xennet_get_extras(struct netfront_queue *queue,
765 struct xen_netif_extra_info *extras,
769 struct xen_netif_extra_info extra;
770 struct device *dev = &queue->info->netdev->dev;
771 RING_IDX cons = queue->rx.rsp_cons;
778 if (unlikely(cons + 1 == rp)) {
780 dev_warn(dev, "Missing extra info\n");
785 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
787 if (unlikely(!extra.type ||
788 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
790 dev_warn(dev, "Invalid extra type: %d\n",
794 extras[extra.type - 1] = extra;
797 skb = xennet_get_rx_skb(queue, cons);
798 ref = xennet_get_rx_ref(queue, cons);
799 xennet_move_rx_slot(queue, skb, ref);
800 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
802 queue->rx.rsp_cons = cons;
806 static int xennet_get_responses(struct netfront_queue *queue,
807 struct netfront_rx_info *rinfo, RING_IDX rp,
808 struct sk_buff_head *list)
810 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
811 struct xen_netif_extra_info *extras = rinfo->extras;
812 struct device *dev = &queue->info->netdev->dev;
813 RING_IDX cons = queue->rx.rsp_cons;
814 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
815 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
816 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
821 if (rx->flags & XEN_NETRXF_extra_info) {
822 err = xennet_get_extras(queue, extras, rp);
823 cons = queue->rx.rsp_cons;
827 if (unlikely(rx->status < 0 ||
828 rx->offset + rx->status > XEN_PAGE_SIZE)) {
830 dev_warn(dev, "rx->offset: %u, size: %d\n",
831 rx->offset, rx->status);
832 xennet_move_rx_slot(queue, skb, ref);
838 * This definitely indicates a bug, either in this driver or in
839 * the backend driver. In future this should flag the bad
840 * situation to the system controller to reboot the backend.
842 if (ref == GRANT_INVALID_REF) {
844 dev_warn(dev, "Bad rx response id %d.\n",
850 ret = gnttab_end_foreign_access_ref(ref, 0);
853 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
855 __skb_queue_tail(list, skb);
858 if (!(rx->flags & XEN_NETRXF_more_data))
861 if (cons + slots == rp) {
863 dev_warn(dev, "Need more slots\n");
868 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
870 skb = xennet_get_rx_skb(queue, cons + slots);
871 ref = xennet_get_rx_ref(queue, cons + slots);
875 if (unlikely(slots > max)) {
877 dev_warn(dev, "Too many slots\n");
882 queue->rx.rsp_cons = cons + slots;
887 static int xennet_set_skb_gso(struct sk_buff *skb,
888 struct xen_netif_extra_info *gso)
890 if (!gso->u.gso.size) {
892 pr_warn("GSO size must not be zero\n");
896 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
897 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
899 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
903 skb_shinfo(skb)->gso_size = gso->u.gso.size;
904 skb_shinfo(skb)->gso_type =
905 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
909 /* Header must be checked, and gso_segs computed. */
910 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
911 skb_shinfo(skb)->gso_segs = 0;
916 static int xennet_fill_frags(struct netfront_queue *queue,
918 struct sk_buff_head *list)
920 RING_IDX cons = queue->rx.rsp_cons;
921 struct sk_buff *nskb;
923 while ((nskb = __skb_dequeue(list))) {
924 struct xen_netif_rx_response rx;
925 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
927 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
929 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
930 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
932 BUG_ON(pull_to < skb_headlen(skb));
933 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
935 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
936 queue->rx.rsp_cons = ++cons + skb_queue_len(list);
941 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
942 skb_frag_page(nfrag),
943 rx.offset, rx.status, PAGE_SIZE);
945 skb_shinfo(nskb)->nr_frags = 0;
949 queue->rx.rsp_cons = cons;
954 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
956 bool recalculate_partial_csum = false;
959 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
960 * peers can fail to set NETRXF_csum_blank when sending a GSO
961 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
962 * recalculate the partial checksum.
964 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
965 struct netfront_info *np = netdev_priv(dev);
966 atomic_inc(&np->rx_gso_checksum_fixup);
967 skb->ip_summed = CHECKSUM_PARTIAL;
968 recalculate_partial_csum = true;
971 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
972 if (skb->ip_summed != CHECKSUM_PARTIAL)
975 return skb_checksum_setup(skb, recalculate_partial_csum);
978 static int handle_incoming_queue(struct netfront_queue *queue,
979 struct sk_buff_head *rxq)
981 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
982 int packets_dropped = 0;
985 while ((skb = __skb_dequeue(rxq)) != NULL) {
986 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
988 if (pull_to > skb_headlen(skb))
989 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
991 /* Ethernet work: Delayed to here as it peeks the header. */
992 skb->protocol = eth_type_trans(skb, queue->info->netdev);
993 skb_reset_network_header(skb);
995 if (checksum_setup(queue->info->netdev, skb)) {
998 queue->info->netdev->stats.rx_errors++;
1002 u64_stats_update_begin(&rx_stats->syncp);
1003 rx_stats->packets++;
1004 rx_stats->bytes += skb->len;
1005 u64_stats_update_end(&rx_stats->syncp);
1008 napi_gro_receive(&queue->napi, skb);
1011 return packets_dropped;
1014 static int xennet_poll(struct napi_struct *napi, int budget)
1016 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1017 struct net_device *dev = queue->info->netdev;
1018 struct sk_buff *skb;
1019 struct netfront_rx_info rinfo;
1020 struct xen_netif_rx_response *rx = &rinfo.rx;
1021 struct xen_netif_extra_info *extras = rinfo.extras;
1024 struct sk_buff_head rxq;
1025 struct sk_buff_head errq;
1026 struct sk_buff_head tmpq;
1029 spin_lock(&queue->rx_lock);
1031 skb_queue_head_init(&rxq);
1032 skb_queue_head_init(&errq);
1033 skb_queue_head_init(&tmpq);
1035 rp = queue->rx.sring->rsp_prod;
1036 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1037 dev_alert(&dev->dev, "Illegal number of responses %u\n",
1038 rp - queue->rx.rsp_cons);
1039 queue->info->broken = true;
1040 spin_unlock(&queue->rx_lock);
1043 rmb(); /* Ensure we see queued responses up to 'rp'. */
1045 i = queue->rx.rsp_cons;
1047 while ((i != rp) && (work_done < budget)) {
1048 RING_COPY_RESPONSE(&queue->rx, i, rx);
1049 memset(extras, 0, sizeof(rinfo.extras));
1051 err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
1053 if (unlikely(err)) {
1055 while ((skb = __skb_dequeue(&tmpq)))
1056 __skb_queue_tail(&errq, skb);
1057 dev->stats.rx_errors++;
1058 i = queue->rx.rsp_cons;
1062 skb = __skb_dequeue(&tmpq);
1064 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1065 struct xen_netif_extra_info *gso;
1066 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1068 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1069 __skb_queue_head(&tmpq, skb);
1070 queue->rx.rsp_cons += skb_queue_len(&tmpq);
1075 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1076 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1077 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
1079 skb_shinfo(skb)->frags[0].page_offset = rx->offset;
1080 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1081 skb->data_len = rx->status;
1082 skb->len += rx->status;
1084 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
1087 if (rx->flags & XEN_NETRXF_csum_blank)
1088 skb->ip_summed = CHECKSUM_PARTIAL;
1089 else if (rx->flags & XEN_NETRXF_data_validated)
1090 skb->ip_summed = CHECKSUM_UNNECESSARY;
1092 __skb_queue_tail(&rxq, skb);
1094 i = ++queue->rx.rsp_cons;
1098 __skb_queue_purge(&errq);
1100 work_done -= handle_incoming_queue(queue, &rxq);
1102 xennet_alloc_rx_buffers(queue);
1104 if (work_done < budget) {
1107 napi_complete(napi);
1109 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1111 napi_schedule(napi);
1114 spin_unlock(&queue->rx_lock);
1119 static int xennet_change_mtu(struct net_device *dev, int mtu)
1121 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
1129 static struct rtnl_link_stats64 *xennet_get_stats64(struct net_device *dev,
1130 struct rtnl_link_stats64 *tot)
1132 struct netfront_info *np = netdev_priv(dev);
1135 for_each_possible_cpu(cpu) {
1136 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1137 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
1138 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1142 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1143 tx_packets = tx_stats->packets;
1144 tx_bytes = tx_stats->bytes;
1145 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
1148 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1149 rx_packets = rx_stats->packets;
1150 rx_bytes = rx_stats->bytes;
1151 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
1153 tot->rx_packets += rx_packets;
1154 tot->tx_packets += tx_packets;
1155 tot->rx_bytes += rx_bytes;
1156 tot->tx_bytes += tx_bytes;
1159 tot->rx_errors = dev->stats.rx_errors;
1160 tot->tx_dropped = dev->stats.tx_dropped;
1165 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1167 struct sk_buff *skb;
1170 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1171 /* Skip over entries which are actually freelist references */
1172 if (!queue->tx_skbs[i])
1175 skb = queue->tx_skbs[i];
1176 queue->tx_skbs[i] = NULL;
1177 get_page(queue->grant_tx_page[i]);
1178 gnttab_end_foreign_access(queue->grant_tx_ref[i],
1180 (unsigned long)page_address(queue->grant_tx_page[i]));
1181 queue->grant_tx_page[i] = NULL;
1182 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1183 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
1184 dev_kfree_skb_irq(skb);
1188 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1192 spin_lock_bh(&queue->rx_lock);
1194 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1195 struct sk_buff *skb;
1198 skb = queue->rx_skbs[id];
1202 ref = queue->grant_rx_ref[id];
1203 if (ref == GRANT_INVALID_REF)
1206 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1208 /* gnttab_end_foreign_access() needs a page ref until
1209 * foreign access is ended (which may be deferred).
1212 gnttab_end_foreign_access(ref, 0,
1213 (unsigned long)page_address(page));
1214 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
1219 spin_unlock_bh(&queue->rx_lock);
1222 static netdev_features_t xennet_fix_features(struct net_device *dev,
1223 netdev_features_t features)
1225 struct netfront_info *np = netdev_priv(dev);
1228 if (features & NETIF_F_SG) {
1229 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1234 features &= ~NETIF_F_SG;
1237 if (features & NETIF_F_IPV6_CSUM) {
1238 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1239 "feature-ipv6-csum-offload", "%d", &val) < 0)
1243 features &= ~NETIF_F_IPV6_CSUM;
1246 if (features & NETIF_F_TSO) {
1247 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1248 "feature-gso-tcpv4", "%d", &val) < 0)
1252 features &= ~NETIF_F_TSO;
1255 if (features & NETIF_F_TSO6) {
1256 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1257 "feature-gso-tcpv6", "%d", &val) < 0)
1261 features &= ~NETIF_F_TSO6;
1267 static int xennet_set_features(struct net_device *dev,
1268 netdev_features_t features)
1270 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1271 netdev_info(dev, "Reducing MTU because no SG offload");
1272 dev->mtu = ETH_DATA_LEN;
1278 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1280 struct netfront_queue *queue = dev_id;
1281 unsigned long flags;
1283 if (queue->info->broken)
1286 spin_lock_irqsave(&queue->tx_lock, flags);
1287 xennet_tx_buf_gc(queue);
1288 spin_unlock_irqrestore(&queue->tx_lock, flags);
1293 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1295 struct netfront_queue *queue = dev_id;
1296 struct net_device *dev = queue->info->netdev;
1298 if (queue->info->broken)
1301 if (likely(netif_carrier_ok(dev) &&
1302 RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
1303 napi_schedule(&queue->napi);
1308 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1310 xennet_tx_interrupt(irq, dev_id);
1311 xennet_rx_interrupt(irq, dev_id);
1315 #ifdef CONFIG_NET_POLL_CONTROLLER
1316 static void xennet_poll_controller(struct net_device *dev)
1318 /* Poll each queue */
1319 struct netfront_info *info = netdev_priv(dev);
1320 unsigned int num_queues = dev->real_num_tx_queues;
1326 for (i = 0; i < num_queues; ++i)
1327 xennet_interrupt(0, &info->queues[i]);
1331 static const struct net_device_ops xennet_netdev_ops = {
1332 .ndo_open = xennet_open,
1333 .ndo_stop = xennet_close,
1334 .ndo_start_xmit = xennet_start_xmit,
1335 .ndo_change_mtu = xennet_change_mtu,
1336 .ndo_get_stats64 = xennet_get_stats64,
1337 .ndo_set_mac_address = eth_mac_addr,
1338 .ndo_validate_addr = eth_validate_addr,
1339 .ndo_fix_features = xennet_fix_features,
1340 .ndo_set_features = xennet_set_features,
1341 .ndo_select_queue = xennet_select_queue,
1342 #ifdef CONFIG_NET_POLL_CONTROLLER
1343 .ndo_poll_controller = xennet_poll_controller,
1347 static void xennet_free_netdev(struct net_device *netdev)
1349 struct netfront_info *np = netdev_priv(netdev);
1351 free_percpu(np->rx_stats);
1352 free_percpu(np->tx_stats);
1353 free_netdev(netdev);
1356 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1359 struct net_device *netdev;
1360 struct netfront_info *np;
1362 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1364 return ERR_PTR(-ENOMEM);
1366 np = netdev_priv(netdev);
1372 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1373 if (np->rx_stats == NULL)
1375 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1376 if (np->tx_stats == NULL)
1379 netdev->netdev_ops = &xennet_netdev_ops;
1381 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1383 netdev->hw_features = NETIF_F_SG |
1385 NETIF_F_TSO | NETIF_F_TSO6;
1388 * Assume that all hw features are available for now. This set
1389 * will be adjusted by the call to netdev_update_features() in
1390 * xennet_connect() which is the earliest point where we can
1391 * negotiate with the backend regarding supported features.
1393 netdev->features |= netdev->hw_features;
1395 netdev->ethtool_ops = &xennet_ethtool_ops;
1396 SET_NETDEV_DEV(netdev, &dev->dev);
1398 np->netdev = netdev;
1400 netif_carrier_off(netdev);
1403 xenbus_switch_state(dev, XenbusStateInitialising);
1404 err = wait_event_timeout(module_wq,
1405 xenbus_read_driver_state(dev->otherend) !=
1406 XenbusStateClosed &&
1407 xenbus_read_driver_state(dev->otherend) !=
1408 XenbusStateUnknown, XENNET_TIMEOUT);
1414 xennet_free_netdev(netdev);
1415 return ERR_PTR(err);
1419 * Entry point to this code when a new device is created. Allocate the basic
1420 * structures and the ring buffers for communication with the backend, and
1421 * inform the backend of the appropriate details for those.
1423 static int netfront_probe(struct xenbus_device *dev,
1424 const struct xenbus_device_id *id)
1427 struct net_device *netdev;
1428 struct netfront_info *info;
1430 netdev = xennet_create_dev(dev);
1431 if (IS_ERR(netdev)) {
1432 err = PTR_ERR(netdev);
1433 xenbus_dev_fatal(dev, err, "creating netdev");
1437 info = netdev_priv(netdev);
1438 dev_set_drvdata(&dev->dev, info);
1440 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1446 static void xennet_end_access(int ref, void *page)
1448 /* This frees the page as a side-effect */
1449 if (ref != GRANT_INVALID_REF)
1450 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1453 static void xennet_disconnect_backend(struct netfront_info *info)
1456 unsigned int num_queues = info->netdev->real_num_tx_queues;
1458 netif_carrier_off(info->netdev);
1460 for (i = 0; i < num_queues && info->queues; ++i) {
1461 struct netfront_queue *queue = &info->queues[i];
1463 del_timer_sync(&queue->rx_refill_timer);
1465 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1466 unbind_from_irqhandler(queue->tx_irq, queue);
1467 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1468 unbind_from_irqhandler(queue->tx_irq, queue);
1469 unbind_from_irqhandler(queue->rx_irq, queue);
1471 queue->tx_evtchn = queue->rx_evtchn = 0;
1472 queue->tx_irq = queue->rx_irq = 0;
1474 if (netif_running(info->netdev))
1475 napi_synchronize(&queue->napi);
1477 xennet_release_tx_bufs(queue);
1478 xennet_release_rx_bufs(queue);
1479 gnttab_free_grant_references(queue->gref_tx_head);
1480 gnttab_free_grant_references(queue->gref_rx_head);
1482 /* End access and free the pages */
1483 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1484 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1486 queue->tx_ring_ref = GRANT_INVALID_REF;
1487 queue->rx_ring_ref = GRANT_INVALID_REF;
1488 queue->tx.sring = NULL;
1489 queue->rx.sring = NULL;
1494 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1495 * driver restart. We tear down our netif structure and recreate it, but
1496 * leave the device-layer structures intact so that this is transparent to the
1497 * rest of the kernel.
1499 static int netfront_resume(struct xenbus_device *dev)
1501 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1503 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1505 netif_tx_lock_bh(info->netdev);
1506 netif_device_detach(info->netdev);
1507 netif_tx_unlock_bh(info->netdev);
1509 xennet_disconnect_backend(info);
1513 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1515 char *s, *e, *macstr;
1518 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1520 return PTR_ERR(macstr);
1522 for (i = 0; i < ETH_ALEN; i++) {
1523 mac[i] = simple_strtoul(s, &e, 16);
1524 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1535 static int setup_netfront_single(struct netfront_queue *queue)
1539 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1543 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
1545 0, queue->info->netdev->name, queue);
1548 queue->rx_evtchn = queue->tx_evtchn;
1549 queue->rx_irq = queue->tx_irq = err;
1554 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1555 queue->tx_evtchn = 0;
1560 static int setup_netfront_split(struct netfront_queue *queue)
1564 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1567 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1569 goto alloc_rx_evtchn_fail;
1571 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1572 "%s-tx", queue->name);
1573 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
1574 xennet_tx_interrupt,
1575 0, queue->tx_irq_name, queue);
1578 queue->tx_irq = err;
1580 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1581 "%s-rx", queue->name);
1582 err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
1583 xennet_rx_interrupt,
1584 0, queue->rx_irq_name, queue);
1587 queue->rx_irq = err;
1592 unbind_from_irqhandler(queue->tx_irq, queue);
1595 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1596 queue->rx_evtchn = 0;
1597 alloc_rx_evtchn_fail:
1598 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1599 queue->tx_evtchn = 0;
1604 static int setup_netfront(struct xenbus_device *dev,
1605 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1607 struct xen_netif_tx_sring *txs;
1608 struct xen_netif_rx_sring *rxs;
1612 queue->tx_ring_ref = GRANT_INVALID_REF;
1613 queue->rx_ring_ref = GRANT_INVALID_REF;
1614 queue->rx.sring = NULL;
1615 queue->tx.sring = NULL;
1617 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1620 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1623 SHARED_RING_INIT(txs);
1624 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1626 err = xenbus_grant_ring(dev, txs, 1, &gref);
1628 goto grant_tx_ring_fail;
1629 queue->tx_ring_ref = gref;
1631 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1634 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1635 goto alloc_rx_ring_fail;
1637 SHARED_RING_INIT(rxs);
1638 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1640 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1642 goto grant_rx_ring_fail;
1643 queue->rx_ring_ref = gref;
1645 if (feature_split_evtchn)
1646 err = setup_netfront_split(queue);
1647 /* setup single event channel if
1648 * a) feature-split-event-channels == 0
1649 * b) feature-split-event-channels == 1 but failed to setup
1651 if (!feature_split_evtchn || (feature_split_evtchn && err))
1652 err = setup_netfront_single(queue);
1655 goto alloc_evtchn_fail;
1659 /* If we fail to setup netfront, it is safe to just revoke access to
1660 * granted pages because backend is not accessing it at this point.
1663 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
1665 free_page((unsigned long)rxs);
1667 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
1669 free_page((unsigned long)txs);
1674 /* Queue-specific initialisation
1675 * This used to be done in xennet_create_dev() but must now
1678 static int xennet_init_queue(struct netfront_queue *queue)
1684 spin_lock_init(&queue->tx_lock);
1685 spin_lock_init(&queue->rx_lock);
1687 setup_timer(&queue->rx_refill_timer, rx_refill_timeout,
1688 (unsigned long)queue);
1690 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
1691 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
1694 /* Initialise tx_skb_freelist as a free chain containing every entry. */
1695 queue->tx_skb_freelist = 0;
1696 queue->tx_pend_queue = TX_LINK_NONE;
1697 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1698 queue->tx_link[i] = i + 1;
1699 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1700 queue->grant_tx_page[i] = NULL;
1702 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
1704 /* Clear out rx_skbs */
1705 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1706 queue->rx_skbs[i] = NULL;
1707 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1710 /* A grant for every tx ring slot */
1711 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
1712 &queue->gref_tx_head) < 0) {
1713 pr_alert("can't alloc tx grant refs\n");
1718 /* A grant for every rx ring slot */
1719 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
1720 &queue->gref_rx_head) < 0) {
1721 pr_alert("can't alloc rx grant refs\n");
1729 gnttab_free_grant_references(queue->gref_tx_head);
1734 static int write_queue_xenstore_keys(struct netfront_queue *queue,
1735 struct xenbus_transaction *xbt, int write_hierarchical)
1737 /* Write the queue-specific keys into XenStore in the traditional
1738 * way for a single queue, or in a queue subkeys for multiple
1741 struct xenbus_device *dev = queue->info->xbdev;
1743 const char *message;
1747 /* Choose the correct place to write the keys */
1748 if (write_hierarchical) {
1749 pathsize = strlen(dev->nodename) + 10;
1750 path = kzalloc(pathsize, GFP_KERNEL);
1753 message = "out of memory while writing ring references";
1756 snprintf(path, pathsize, "%s/queue-%u",
1757 dev->nodename, queue->id);
1759 path = (char *)dev->nodename;
1762 /* Write ring references */
1763 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1764 queue->tx_ring_ref);
1766 message = "writing tx-ring-ref";
1770 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1771 queue->rx_ring_ref);
1773 message = "writing rx-ring-ref";
1777 /* Write event channels; taking into account both shared
1778 * and split event channel scenarios.
1780 if (queue->tx_evtchn == queue->rx_evtchn) {
1781 /* Shared event channel */
1782 err = xenbus_printf(*xbt, path,
1783 "event-channel", "%u", queue->tx_evtchn);
1785 message = "writing event-channel";
1789 /* Split event channels */
1790 err = xenbus_printf(*xbt, path,
1791 "event-channel-tx", "%u", queue->tx_evtchn);
1793 message = "writing event-channel-tx";
1797 err = xenbus_printf(*xbt, path,
1798 "event-channel-rx", "%u", queue->rx_evtchn);
1800 message = "writing event-channel-rx";
1805 if (write_hierarchical)
1810 if (write_hierarchical)
1812 xenbus_dev_fatal(dev, err, "%s", message);
1816 static void xennet_destroy_queues(struct netfront_info *info)
1820 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
1821 struct netfront_queue *queue = &info->queues[i];
1823 if (netif_running(info->netdev))
1824 napi_disable(&queue->napi);
1825 netif_napi_del(&queue->napi);
1828 kfree(info->queues);
1829 info->queues = NULL;
1832 static int xennet_create_queues(struct netfront_info *info,
1833 unsigned int *num_queues)
1838 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
1843 for (i = 0; i < *num_queues; i++) {
1844 struct netfront_queue *queue = &info->queues[i];
1849 ret = xennet_init_queue(queue);
1851 dev_warn(&info->xbdev->dev,
1852 "only created %d queues\n", i);
1857 netif_napi_add(queue->info->netdev, &queue->napi,
1859 if (netif_running(info->netdev))
1860 napi_enable(&queue->napi);
1863 netif_set_real_num_tx_queues(info->netdev, *num_queues);
1865 if (*num_queues == 0) {
1866 dev_err(&info->xbdev->dev, "no queues\n");
1872 /* Common code used when first setting up, and when resuming. */
1873 static int talk_to_netback(struct xenbus_device *dev,
1874 struct netfront_info *info)
1876 const char *message;
1877 struct xenbus_transaction xbt;
1879 unsigned int feature_split_evtchn;
1881 unsigned int max_queues = 0;
1882 struct netfront_queue *queue = NULL;
1883 unsigned int num_queues = 1;
1885 info->netdev->irq = 0;
1887 /* Check if backend supports multiple queues */
1888 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1889 "multi-queue-max-queues", "%u", &max_queues);
1892 num_queues = min(max_queues, xennet_max_queues);
1894 /* Check feature-split-event-channels */
1895 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1896 "feature-split-event-channels", "%u",
1897 &feature_split_evtchn);
1899 feature_split_evtchn = 0;
1901 /* Read mac addr. */
1902 err = xen_net_read_mac(dev, info->netdev->dev_addr);
1904 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1910 xennet_destroy_queues(info);
1912 /* For the case of a reconnect reset the "broken" indicator. */
1913 info->broken = false;
1915 err = xennet_create_queues(info, &num_queues);
1917 xenbus_dev_fatal(dev, err, "creating queues");
1918 kfree(info->queues);
1919 info->queues = NULL;
1924 /* Create shared ring, alloc event channel -- for each queue */
1925 for (i = 0; i < num_queues; ++i) {
1926 queue = &info->queues[i];
1927 err = setup_netfront(dev, queue, feature_split_evtchn);
1933 err = xenbus_transaction_start(&xbt);
1935 xenbus_dev_fatal(dev, err, "starting transaction");
1939 if (xenbus_exists(XBT_NIL,
1940 info->xbdev->otherend, "multi-queue-max-queues")) {
1941 /* Write the number of queues */
1942 err = xenbus_printf(xbt, dev->nodename,
1943 "multi-queue-num-queues", "%u", num_queues);
1945 message = "writing multi-queue-num-queues";
1946 goto abort_transaction_no_dev_fatal;
1950 if (num_queues == 1) {
1951 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
1953 goto abort_transaction_no_dev_fatal;
1955 /* Write the keys for each queue */
1956 for (i = 0; i < num_queues; ++i) {
1957 queue = &info->queues[i];
1958 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
1960 goto abort_transaction_no_dev_fatal;
1964 /* The remaining keys are not queue-specific */
1965 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1968 message = "writing request-rx-copy";
1969 goto abort_transaction;
1972 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1974 message = "writing feature-rx-notify";
1975 goto abort_transaction;
1978 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1980 message = "writing feature-sg";
1981 goto abort_transaction;
1984 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1986 message = "writing feature-gso-tcpv4";
1987 goto abort_transaction;
1990 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
1992 message = "writing feature-gso-tcpv6";
1993 goto abort_transaction;
1996 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
1999 message = "writing feature-ipv6-csum-offload";
2000 goto abort_transaction;
2003 err = xenbus_transaction_end(xbt, 0);
2007 xenbus_dev_fatal(dev, err, "completing transaction");
2014 xenbus_dev_fatal(dev, err, "%s", message);
2015 abort_transaction_no_dev_fatal:
2016 xenbus_transaction_end(xbt, 1);
2018 xennet_disconnect_backend(info);
2020 xennet_destroy_queues(info);
2024 device_unregister(&dev->dev);
2028 static int xennet_connect(struct net_device *dev)
2030 struct netfront_info *np = netdev_priv(dev);
2031 unsigned int num_queues = 0;
2033 unsigned int feature_rx_copy;
2035 struct netfront_queue *queue = NULL;
2037 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
2038 "feature-rx-copy", "%u", &feature_rx_copy);
2040 feature_rx_copy = 0;
2042 if (!feature_rx_copy) {
2044 "backend does not support copying receive path\n");
2048 err = talk_to_netback(np->xbdev, np);
2052 /* talk_to_netback() sets the correct number of queues */
2053 num_queues = dev->real_num_tx_queues;
2055 if (dev->reg_state == NETREG_UNINITIALIZED) {
2056 err = register_netdev(dev);
2058 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2059 device_unregister(&np->xbdev->dev);
2065 netdev_update_features(dev);
2069 * All public and private state should now be sane. Get
2070 * ready to start sending and receiving packets and give the driver
2071 * domain a kick because we've probably just requeued some
2074 netif_tx_lock_bh(np->netdev);
2075 netif_device_attach(np->netdev);
2076 netif_tx_unlock_bh(np->netdev);
2078 netif_carrier_on(np->netdev);
2079 for (j = 0; j < num_queues; ++j) {
2080 queue = &np->queues[j];
2082 notify_remote_via_irq(queue->tx_irq);
2083 if (queue->tx_irq != queue->rx_irq)
2084 notify_remote_via_irq(queue->rx_irq);
2086 spin_lock_irq(&queue->tx_lock);
2087 xennet_tx_buf_gc(queue);
2088 spin_unlock_irq(&queue->tx_lock);
2090 spin_lock_bh(&queue->rx_lock);
2091 xennet_alloc_rx_buffers(queue);
2092 spin_unlock_bh(&queue->rx_lock);
2099 * Callback received when the backend's state changes.
2101 static void netback_changed(struct xenbus_device *dev,
2102 enum xenbus_state backend_state)
2104 struct netfront_info *np = dev_get_drvdata(&dev->dev);
2105 struct net_device *netdev = np->netdev;
2107 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2109 wake_up_all(&module_wq);
2111 switch (backend_state) {
2112 case XenbusStateInitialising:
2113 case XenbusStateInitialised:
2114 case XenbusStateReconfiguring:
2115 case XenbusStateReconfigured:
2116 case XenbusStateUnknown:
2119 case XenbusStateInitWait:
2120 if (dev->state != XenbusStateInitialising)
2122 if (xennet_connect(netdev) != 0)
2124 xenbus_switch_state(dev, XenbusStateConnected);
2127 case XenbusStateConnected:
2128 netdev_notify_peers(netdev);
2131 case XenbusStateClosed:
2132 if (dev->state == XenbusStateClosed)
2134 /* Missed the backend's CLOSING state -- fallthrough */
2135 case XenbusStateClosing:
2136 xenbus_frontend_closed(dev);
2141 static const struct xennet_stat {
2142 char name[ETH_GSTRING_LEN];
2144 } xennet_stats[] = {
2146 "rx_gso_checksum_fixup",
2147 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2151 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2153 switch (string_set) {
2155 return ARRAY_SIZE(xennet_stats);
2161 static void xennet_get_ethtool_stats(struct net_device *dev,
2162 struct ethtool_stats *stats, u64 * data)
2164 void *np = netdev_priv(dev);
2167 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2168 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2171 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2175 switch (stringset) {
2177 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2178 memcpy(data + i * ETH_GSTRING_LEN,
2179 xennet_stats[i].name, ETH_GSTRING_LEN);
2184 static const struct ethtool_ops xennet_ethtool_ops =
2186 .get_link = ethtool_op_get_link,
2188 .get_sset_count = xennet_get_sset_count,
2189 .get_ethtool_stats = xennet_get_ethtool_stats,
2190 .get_strings = xennet_get_strings,
2194 static ssize_t show_rxbuf(struct device *dev,
2195 struct device_attribute *attr, char *buf)
2197 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2200 static ssize_t store_rxbuf(struct device *dev,
2201 struct device_attribute *attr,
2202 const char *buf, size_t len)
2205 unsigned long target;
2207 if (!capable(CAP_NET_ADMIN))
2210 target = simple_strtoul(buf, &endp, 0);
2214 /* rxbuf_min and rxbuf_max are no longer configurable. */
2219 static DEVICE_ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
2220 static DEVICE_ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
2221 static DEVICE_ATTR(rxbuf_cur, S_IRUGO, show_rxbuf, NULL);
2223 static struct attribute *xennet_dev_attrs[] = {
2224 &dev_attr_rxbuf_min.attr,
2225 &dev_attr_rxbuf_max.attr,
2226 &dev_attr_rxbuf_cur.attr,
2230 static const struct attribute_group xennet_dev_group = {
2231 .attrs = xennet_dev_attrs
2233 #endif /* CONFIG_SYSFS */
2235 static void xennet_bus_close(struct xenbus_device *dev)
2239 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2242 xenbus_switch_state(dev, XenbusStateClosing);
2243 ret = wait_event_timeout(module_wq,
2244 xenbus_read_driver_state(dev->otherend) ==
2245 XenbusStateClosing ||
2246 xenbus_read_driver_state(dev->otherend) ==
2247 XenbusStateClosed ||
2248 xenbus_read_driver_state(dev->otherend) ==
2253 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2257 xenbus_switch_state(dev, XenbusStateClosed);
2258 ret = wait_event_timeout(module_wq,
2259 xenbus_read_driver_state(dev->otherend) ==
2260 XenbusStateClosed ||
2261 xenbus_read_driver_state(dev->otherend) ==
2267 static int xennet_remove(struct xenbus_device *dev)
2269 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2271 xennet_bus_close(dev);
2272 xennet_disconnect_backend(info);
2274 if (info->netdev->reg_state == NETREG_REGISTERED)
2275 unregister_netdev(info->netdev);
2279 xennet_destroy_queues(info);
2282 xennet_free_netdev(info->netdev);
2287 static const struct xenbus_device_id netfront_ids[] = {
2292 static struct xenbus_driver netfront_driver = {
2293 .ids = netfront_ids,
2294 .probe = netfront_probe,
2295 .remove = xennet_remove,
2296 .resume = netfront_resume,
2297 .otherend_changed = netback_changed,
2300 static int __init netif_init(void)
2305 if (!xen_has_pv_nic_devices())
2308 pr_info("Initialising Xen virtual ethernet driver\n");
2310 /* Allow as many queues as there are CPUs if user has not
2311 * specified a value.
2313 if (xennet_max_queues == 0)
2314 xennet_max_queues = num_online_cpus();
2316 return xenbus_register_frontend(&netfront_driver);
2318 module_init(netif_init);
2321 static void __exit netif_exit(void)
2323 xenbus_unregister_driver(&netfront_driver);
2325 module_exit(netif_exit);
2327 MODULE_DESCRIPTION("Xen virtual network device frontend");
2328 MODULE_LICENSE("GPL");
2329 MODULE_ALIAS("xen:vif");
2330 MODULE_ALIAS("xennet");