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];
145 unsigned int rx_rsp_unconsumed;
146 spinlock_t rx_cons_lock;
149 struct netfront_info {
150 struct list_head list;
151 struct net_device *netdev;
153 struct xenbus_device *xbdev;
155 /* Multi-queue support */
156 struct netfront_queue *queues;
159 struct netfront_stats __percpu *rx_stats;
160 struct netfront_stats __percpu *tx_stats;
162 /* Is device behaving sane? */
165 atomic_t rx_gso_checksum_fixup;
168 struct netfront_rx_info {
169 struct xen_netif_rx_response rx;
170 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
174 * Access macros for acquiring freeing slots in tx_skbs[].
177 static void add_id_to_list(unsigned *head, unsigned short *list,
184 static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
186 unsigned int id = *head;
188 if (id != TX_LINK_NONE) {
190 list[id] = TX_LINK_NONE;
195 static int xennet_rxidx(RING_IDX idx)
197 return idx & (NET_RX_RING_SIZE - 1);
200 static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
203 int i = xennet_rxidx(ri);
204 struct sk_buff *skb = queue->rx_skbs[i];
205 queue->rx_skbs[i] = NULL;
209 static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
212 int i = xennet_rxidx(ri);
213 grant_ref_t ref = queue->grant_rx_ref[i];
214 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
219 static const struct attribute_group xennet_dev_group;
222 static bool xennet_can_sg(struct net_device *dev)
224 return dev->features & NETIF_F_SG;
228 static void rx_refill_timeout(unsigned long data)
230 struct netfront_queue *queue = (struct netfront_queue *)data;
231 napi_schedule(&queue->napi);
234 static int netfront_tx_slot_available(struct netfront_queue *queue)
236 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
237 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
240 static void xennet_maybe_wake_tx(struct netfront_queue *queue)
242 struct net_device *dev = queue->info->netdev;
243 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
245 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
246 netfront_tx_slot_available(queue) &&
247 likely(netif_running(dev)))
248 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
252 static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
257 skb = __netdev_alloc_skb(queue->info->netdev,
258 RX_COPY_THRESHOLD + NET_IP_ALIGN,
259 GFP_ATOMIC | __GFP_NOWARN);
263 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
268 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
270 /* Align ip header to a 16 bytes boundary */
271 skb_reserve(skb, NET_IP_ALIGN);
272 skb->dev = queue->info->netdev;
278 static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
280 RING_IDX req_prod = queue->rx.req_prod_pvt;
284 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
287 for (req_prod = queue->rx.req_prod_pvt;
288 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
294 struct xen_netif_rx_request *req;
296 skb = xennet_alloc_one_rx_buffer(queue);
302 id = xennet_rxidx(req_prod);
304 BUG_ON(queue->rx_skbs[id]);
305 queue->rx_skbs[id] = skb;
307 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
308 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
309 queue->grant_rx_ref[id] = ref;
311 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
313 req = RING_GET_REQUEST(&queue->rx, req_prod);
314 gnttab_page_grant_foreign_access_ref_one(ref,
315 queue->info->xbdev->otherend_id,
322 queue->rx.req_prod_pvt = req_prod;
324 /* Try again later if there are not enough requests or skb allocation
326 * Enough requests is quantified as the sum of newly created slots and
327 * the unconsumed slots at the backend.
329 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
331 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
335 wmb(); /* barrier so backend seens requests */
337 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
339 notify_remote_via_irq(queue->rx_irq);
342 static int xennet_open(struct net_device *dev)
344 struct netfront_info *np = netdev_priv(dev);
345 unsigned int num_queues = dev->real_num_tx_queues;
347 struct netfront_queue *queue = NULL;
349 if (!np->queues || np->broken)
352 for (i = 0; i < num_queues; ++i) {
353 queue = &np->queues[i];
354 napi_enable(&queue->napi);
356 spin_lock_bh(&queue->rx_lock);
357 if (netif_carrier_ok(dev)) {
358 xennet_alloc_rx_buffers(queue);
359 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
360 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
361 napi_schedule(&queue->napi);
363 spin_unlock_bh(&queue->rx_lock);
366 netif_tx_start_all_queues(dev);
371 static bool xennet_tx_buf_gc(struct netfront_queue *queue)
376 bool work_done = false;
377 const struct device *dev = &queue->info->netdev->dev;
379 BUG_ON(!netif_carrier_ok(queue->info->netdev));
382 prod = queue->tx.sring->rsp_prod;
383 if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
384 dev_alert(dev, "Illegal number of responses %u\n",
385 prod - queue->tx.rsp_cons);
388 rmb(); /* Ensure we see responses up to 'rp'. */
390 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
391 struct xen_netif_tx_response txrsp;
395 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
396 if (txrsp.status == XEN_NETIF_RSP_NULL)
400 if (id >= RING_SIZE(&queue->tx)) {
402 "Response has incorrect id (%u)\n",
406 if (queue->tx_link[id] != TX_PENDING) {
408 "Response for inactive request\n");
412 queue->tx_link[id] = TX_LINK_NONE;
413 skb = queue->tx_skbs[id];
414 queue->tx_skbs[id] = NULL;
415 if (unlikely(gnttab_query_foreign_access(
416 queue->grant_tx_ref[id]) != 0)) {
418 "Grant still in use by backend domain\n");
421 gnttab_end_foreign_access_ref(
422 queue->grant_tx_ref[id], GNTMAP_readonly);
423 gnttab_release_grant_reference(
424 &queue->gref_tx_head, queue->grant_tx_ref[id]);
425 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
426 queue->grant_tx_page[id] = NULL;
427 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
428 dev_kfree_skb_irq(skb);
431 queue->tx.rsp_cons = prod;
434 * Set a new event, then check for race with update of tx_cons.
435 * Note that it is essential to schedule a callback, no matter
436 * how few buffers are pending. Even if there is space in the
437 * transmit ring, higher layers may be blocked because too much
438 * data is outstanding: in such cases notification from Xen is
439 * likely to be the only kick that we'll get.
441 queue->tx.sring->rsp_event =
442 prod + ((queue->tx.sring->req_prod - prod) >> 1) + 1;
443 mb(); /* update shared area */
444 } while ((cons == prod) && (prod != queue->tx.sring->rsp_prod));
446 xennet_maybe_wake_tx(queue);
451 queue->info->broken = true;
452 dev_alert(dev, "Disabled for further use\n");
457 struct xennet_gnttab_make_txreq {
458 struct netfront_queue *queue;
461 struct xen_netif_tx_request *tx; /* Last request on ring page */
462 struct xen_netif_tx_request tx_local; /* Last request local copy*/
466 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
467 unsigned int len, void *data)
469 struct xennet_gnttab_make_txreq *info = data;
471 struct xen_netif_tx_request *tx;
473 /* convenient aliases */
474 struct page *page = info->page;
475 struct netfront_queue *queue = info->queue;
476 struct sk_buff *skb = info->skb;
478 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
479 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
480 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
481 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
483 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
484 gfn, GNTMAP_readonly);
486 queue->tx_skbs[id] = skb;
487 queue->grant_tx_page[id] = page;
488 queue->grant_tx_ref[id] = ref;
490 info->tx_local.id = id;
491 info->tx_local.gref = ref;
492 info->tx_local.offset = offset;
493 info->tx_local.size = len;
494 info->tx_local.flags = 0;
496 *tx = info->tx_local;
499 * Put the request in the pending queue, it will be set to be pending
500 * when the producer index is about to be raised.
502 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
505 info->size += info->tx_local.size;
508 static struct xen_netif_tx_request *xennet_make_first_txreq(
509 struct xennet_gnttab_make_txreq *info,
510 unsigned int offset, unsigned int len)
514 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
519 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
520 unsigned int len, void *data)
522 struct xennet_gnttab_make_txreq *info = data;
524 info->tx->flags |= XEN_NETTXF_more_data;
526 xennet_tx_setup_grant(gfn, offset, len, data);
529 static void xennet_make_txreqs(
530 struct xennet_gnttab_make_txreq *info,
532 unsigned int offset, unsigned int len)
534 /* Skip unused frames from start of page */
535 page += offset >> PAGE_SHIFT;
536 offset &= ~PAGE_MASK;
542 gnttab_foreach_grant_in_range(page, offset, len,
543 xennet_make_one_txreq,
553 * Count how many ring slots are required to send this skb. Each frag
554 * might be a compound page.
556 static int xennet_count_skb_slots(struct sk_buff *skb)
558 int i, frags = skb_shinfo(skb)->nr_frags;
561 slots = gnttab_count_grant(offset_in_page(skb->data),
564 for (i = 0; i < frags; i++) {
565 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
566 unsigned long size = skb_frag_size(frag);
567 unsigned long offset = frag->page_offset;
569 /* Skip unused frames from start of page */
570 offset &= ~PAGE_MASK;
572 slots += gnttab_count_grant(offset, size);
578 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
579 void *accel_priv, select_queue_fallback_t fallback)
581 unsigned int num_queues = dev->real_num_tx_queues;
585 /* First, check if there is only one queue */
586 if (num_queues == 1) {
589 hash = skb_get_hash(skb);
590 queue_idx = hash % num_queues;
596 static void xennet_mark_tx_pending(struct netfront_queue *queue)
600 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
602 queue->tx_link[i] = TX_PENDING;
605 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
607 static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
609 struct netfront_info *np = netdev_priv(dev);
610 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
611 struct xen_netif_tx_request *first_tx;
619 struct netfront_queue *queue = NULL;
620 struct xennet_gnttab_make_txreq info = { };
621 unsigned int num_queues = dev->real_num_tx_queues;
624 /* Drop the packet if no queues are set up */
627 if (unlikely(np->broken))
629 /* Determine which queue to transmit this SKB on */
630 queue_index = skb_get_queue_mapping(skb);
631 queue = &np->queues[queue_index];
633 /* If skb->len is too big for wire format, drop skb and alert
634 * user about misconfiguration.
636 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
637 net_alert_ratelimited(
638 "xennet: skb->len = %u, too big for wire format\n",
643 slots = xennet_count_skb_slots(skb);
644 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
645 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
647 if (skb_linearize(skb))
651 page = virt_to_page(skb->data);
652 offset = offset_in_page(skb->data);
653 len = skb_headlen(skb);
655 spin_lock_irqsave(&queue->tx_lock, flags);
657 if (unlikely(!netif_carrier_ok(dev) ||
658 (slots > 1 && !xennet_can_sg(dev)) ||
659 netif_needs_gso(skb, netif_skb_features(skb)))) {
660 spin_unlock_irqrestore(&queue->tx_lock, flags);
664 /* First request for the linear area. */
668 first_tx = xennet_make_first_txreq(&info, offset, len);
669 offset += info.tx_local.size;
670 if (offset == PAGE_SIZE) {
674 len -= info.tx_local.size;
676 if (skb->ip_summed == CHECKSUM_PARTIAL)
678 first_tx->flags |= XEN_NETTXF_csum_blank |
679 XEN_NETTXF_data_validated;
680 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
681 /* remote but checksummed. */
682 first_tx->flags |= XEN_NETTXF_data_validated;
684 /* Optional extra info after the first request. */
685 if (skb_shinfo(skb)->gso_size) {
686 struct xen_netif_extra_info *gso;
688 gso = (struct xen_netif_extra_info *)
689 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
691 first_tx->flags |= XEN_NETTXF_extra_info;
693 gso->u.gso.size = skb_shinfo(skb)->gso_size;
694 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
695 XEN_NETIF_GSO_TYPE_TCPV6 :
696 XEN_NETIF_GSO_TYPE_TCPV4;
698 gso->u.gso.features = 0;
700 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
704 /* Requests for the rest of the linear area. */
705 xennet_make_txreqs(&info, page, offset, len);
707 /* Requests for all the frags. */
708 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
709 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
710 xennet_make_txreqs(&info, skb_frag_page(frag),
712 skb_frag_size(frag));
715 /* First request has the packet length. */
716 first_tx->size = skb->len;
718 xennet_mark_tx_pending(queue);
720 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
722 notify_remote_via_irq(queue->tx_irq);
724 u64_stats_update_begin(&tx_stats->syncp);
725 tx_stats->bytes += skb->len;
727 u64_stats_update_end(&tx_stats->syncp);
729 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
730 xennet_tx_buf_gc(queue);
732 if (!netfront_tx_slot_available(queue))
733 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
735 spin_unlock_irqrestore(&queue->tx_lock, flags);
740 dev->stats.tx_dropped++;
741 dev_kfree_skb_any(skb);
745 static int xennet_close(struct net_device *dev)
747 struct netfront_info *np = netdev_priv(dev);
748 unsigned int num_queues = dev->real_num_tx_queues;
750 struct netfront_queue *queue;
751 netif_tx_stop_all_queues(np->netdev);
752 for (i = 0; i < num_queues; ++i) {
753 queue = &np->queues[i];
754 napi_disable(&queue->napi);
759 static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
763 spin_lock_irqsave(&queue->rx_cons_lock, flags);
764 queue->rx.rsp_cons = val;
765 queue->rx_rsp_unconsumed = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
766 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
769 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
772 int new = xennet_rxidx(queue->rx.req_prod_pvt);
774 BUG_ON(queue->rx_skbs[new]);
775 queue->rx_skbs[new] = skb;
776 queue->grant_rx_ref[new] = ref;
777 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
778 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
779 queue->rx.req_prod_pvt++;
782 static int xennet_get_extras(struct netfront_queue *queue,
783 struct xen_netif_extra_info *extras,
787 struct xen_netif_extra_info extra;
788 struct device *dev = &queue->info->netdev->dev;
789 RING_IDX cons = queue->rx.rsp_cons;
796 if (unlikely(cons + 1 == rp)) {
798 dev_warn(dev, "Missing extra info\n");
803 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
805 if (unlikely(!extra.type ||
806 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
808 dev_warn(dev, "Invalid extra type: %d\n",
812 extras[extra.type - 1] = extra;
815 skb = xennet_get_rx_skb(queue, cons);
816 ref = xennet_get_rx_ref(queue, cons);
817 xennet_move_rx_slot(queue, skb, ref);
818 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
820 xennet_set_rx_rsp_cons(queue, cons);
824 static int xennet_get_responses(struct netfront_queue *queue,
825 struct netfront_rx_info *rinfo, RING_IDX rp,
826 struct sk_buff_head *list)
828 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
829 struct xen_netif_extra_info *extras = rinfo->extras;
830 struct device *dev = &queue->info->netdev->dev;
831 RING_IDX cons = queue->rx.rsp_cons;
832 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
833 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
834 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
839 if (rx->flags & XEN_NETRXF_extra_info) {
840 err = xennet_get_extras(queue, extras, rp);
841 cons = queue->rx.rsp_cons;
845 if (unlikely(rx->status < 0 ||
846 rx->offset + rx->status > XEN_PAGE_SIZE)) {
848 dev_warn(dev, "rx->offset: %u, size: %d\n",
849 rx->offset, rx->status);
850 xennet_move_rx_slot(queue, skb, ref);
856 * This definitely indicates a bug, either in this driver or in
857 * the backend driver. In future this should flag the bad
858 * situation to the system controller to reboot the backend.
860 if (ref == GRANT_INVALID_REF) {
862 dev_warn(dev, "Bad rx response id %d.\n",
868 ret = gnttab_end_foreign_access_ref(ref, 0);
871 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
873 __skb_queue_tail(list, skb);
876 if (!(rx->flags & XEN_NETRXF_more_data))
879 if (cons + slots == rp) {
881 dev_warn(dev, "Need more slots\n");
886 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
888 skb = xennet_get_rx_skb(queue, cons + slots);
889 ref = xennet_get_rx_ref(queue, cons + slots);
893 if (unlikely(slots > max)) {
895 dev_warn(dev, "Too many slots\n");
900 xennet_set_rx_rsp_cons(queue, cons + slots);
905 static int xennet_set_skb_gso(struct sk_buff *skb,
906 struct xen_netif_extra_info *gso)
908 if (!gso->u.gso.size) {
910 pr_warn("GSO size must not be zero\n");
914 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
915 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
917 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
921 skb_shinfo(skb)->gso_size = gso->u.gso.size;
922 skb_shinfo(skb)->gso_type =
923 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
927 /* Header must be checked, and gso_segs computed. */
928 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
929 skb_shinfo(skb)->gso_segs = 0;
934 static int xennet_fill_frags(struct netfront_queue *queue,
936 struct sk_buff_head *list)
938 RING_IDX cons = queue->rx.rsp_cons;
939 struct sk_buff *nskb;
941 while ((nskb = __skb_dequeue(list))) {
942 struct xen_netif_rx_response rx;
943 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
945 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
947 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
948 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
950 BUG_ON(pull_to < skb_headlen(skb));
951 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
953 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
954 xennet_set_rx_rsp_cons(queue,
955 ++cons + skb_queue_len(list));
960 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
961 skb_frag_page(nfrag),
962 rx.offset, rx.status, PAGE_SIZE);
964 skb_shinfo(nskb)->nr_frags = 0;
968 xennet_set_rx_rsp_cons(queue, cons);
973 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
975 bool recalculate_partial_csum = false;
978 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
979 * peers can fail to set NETRXF_csum_blank when sending a GSO
980 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
981 * recalculate the partial checksum.
983 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
984 struct netfront_info *np = netdev_priv(dev);
985 atomic_inc(&np->rx_gso_checksum_fixup);
986 skb->ip_summed = CHECKSUM_PARTIAL;
987 recalculate_partial_csum = true;
990 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
991 if (skb->ip_summed != CHECKSUM_PARTIAL)
994 return skb_checksum_setup(skb, recalculate_partial_csum);
997 static int handle_incoming_queue(struct netfront_queue *queue,
998 struct sk_buff_head *rxq)
1000 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
1001 int packets_dropped = 0;
1002 struct sk_buff *skb;
1004 while ((skb = __skb_dequeue(rxq)) != NULL) {
1005 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
1007 if (pull_to > skb_headlen(skb))
1008 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1010 /* Ethernet work: Delayed to here as it peeks the header. */
1011 skb->protocol = eth_type_trans(skb, queue->info->netdev);
1012 skb_reset_network_header(skb);
1014 if (checksum_setup(queue->info->netdev, skb)) {
1017 queue->info->netdev->stats.rx_errors++;
1021 u64_stats_update_begin(&rx_stats->syncp);
1022 rx_stats->packets++;
1023 rx_stats->bytes += skb->len;
1024 u64_stats_update_end(&rx_stats->syncp);
1027 napi_gro_receive(&queue->napi, skb);
1030 return packets_dropped;
1033 static int xennet_poll(struct napi_struct *napi, int budget)
1035 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1036 struct net_device *dev = queue->info->netdev;
1037 struct sk_buff *skb;
1038 struct netfront_rx_info rinfo;
1039 struct xen_netif_rx_response *rx = &rinfo.rx;
1040 struct xen_netif_extra_info *extras = rinfo.extras;
1043 struct sk_buff_head rxq;
1044 struct sk_buff_head errq;
1045 struct sk_buff_head tmpq;
1048 spin_lock(&queue->rx_lock);
1050 skb_queue_head_init(&rxq);
1051 skb_queue_head_init(&errq);
1052 skb_queue_head_init(&tmpq);
1054 rp = queue->rx.sring->rsp_prod;
1055 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
1056 dev_alert(&dev->dev, "Illegal number of responses %u\n",
1057 rp - queue->rx.rsp_cons);
1058 queue->info->broken = true;
1059 spin_unlock(&queue->rx_lock);
1062 rmb(); /* Ensure we see queued responses up to 'rp'. */
1064 i = queue->rx.rsp_cons;
1066 while ((i != rp) && (work_done < budget)) {
1067 RING_COPY_RESPONSE(&queue->rx, i, rx);
1068 memset(extras, 0, sizeof(rinfo.extras));
1070 err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
1072 if (unlikely(err)) {
1074 while ((skb = __skb_dequeue(&tmpq)))
1075 __skb_queue_tail(&errq, skb);
1076 dev->stats.rx_errors++;
1077 i = queue->rx.rsp_cons;
1081 skb = __skb_dequeue(&tmpq);
1083 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1084 struct xen_netif_extra_info *gso;
1085 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1087 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1088 __skb_queue_head(&tmpq, skb);
1089 xennet_set_rx_rsp_cons(queue,
1090 queue->rx.rsp_cons +
1091 skb_queue_len(&tmpq));
1096 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1097 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1098 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
1100 skb_shinfo(skb)->frags[0].page_offset = rx->offset;
1101 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1102 skb->data_len = rx->status;
1103 skb->len += rx->status;
1105 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
1108 if (rx->flags & XEN_NETRXF_csum_blank)
1109 skb->ip_summed = CHECKSUM_PARTIAL;
1110 else if (rx->flags & XEN_NETRXF_data_validated)
1111 skb->ip_summed = CHECKSUM_UNNECESSARY;
1113 __skb_queue_tail(&rxq, skb);
1115 i = queue->rx.rsp_cons + 1;
1116 xennet_set_rx_rsp_cons(queue, i);
1120 __skb_queue_purge(&errq);
1122 work_done -= handle_incoming_queue(queue, &rxq);
1124 xennet_alloc_rx_buffers(queue);
1126 if (work_done < budget) {
1129 napi_complete(napi);
1131 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1133 napi_schedule(napi);
1136 spin_unlock(&queue->rx_lock);
1141 static int xennet_change_mtu(struct net_device *dev, int mtu)
1143 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
1151 static struct rtnl_link_stats64 *xennet_get_stats64(struct net_device *dev,
1152 struct rtnl_link_stats64 *tot)
1154 struct netfront_info *np = netdev_priv(dev);
1157 for_each_possible_cpu(cpu) {
1158 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1159 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
1160 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1164 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1165 tx_packets = tx_stats->packets;
1166 tx_bytes = tx_stats->bytes;
1167 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
1170 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1171 rx_packets = rx_stats->packets;
1172 rx_bytes = rx_stats->bytes;
1173 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
1175 tot->rx_packets += rx_packets;
1176 tot->tx_packets += tx_packets;
1177 tot->rx_bytes += rx_bytes;
1178 tot->tx_bytes += tx_bytes;
1181 tot->rx_errors = dev->stats.rx_errors;
1182 tot->tx_dropped = dev->stats.tx_dropped;
1187 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1189 struct sk_buff *skb;
1192 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1193 /* Skip over entries which are actually freelist references */
1194 if (!queue->tx_skbs[i])
1197 skb = queue->tx_skbs[i];
1198 queue->tx_skbs[i] = NULL;
1199 get_page(queue->grant_tx_page[i]);
1200 gnttab_end_foreign_access(queue->grant_tx_ref[i],
1202 (unsigned long)page_address(queue->grant_tx_page[i]));
1203 queue->grant_tx_page[i] = NULL;
1204 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1205 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
1206 dev_kfree_skb_irq(skb);
1210 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1214 spin_lock_bh(&queue->rx_lock);
1216 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1217 struct sk_buff *skb;
1220 skb = queue->rx_skbs[id];
1224 ref = queue->grant_rx_ref[id];
1225 if (ref == GRANT_INVALID_REF)
1228 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1230 /* gnttab_end_foreign_access() needs a page ref until
1231 * foreign access is ended (which may be deferred).
1234 gnttab_end_foreign_access(ref, 0,
1235 (unsigned long)page_address(page));
1236 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
1241 spin_unlock_bh(&queue->rx_lock);
1244 static netdev_features_t xennet_fix_features(struct net_device *dev,
1245 netdev_features_t features)
1247 struct netfront_info *np = netdev_priv(dev);
1250 if (features & NETIF_F_SG) {
1251 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1256 features &= ~NETIF_F_SG;
1259 if (features & NETIF_F_IPV6_CSUM) {
1260 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1261 "feature-ipv6-csum-offload", "%d", &val) < 0)
1265 features &= ~NETIF_F_IPV6_CSUM;
1268 if (features & NETIF_F_TSO) {
1269 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1270 "feature-gso-tcpv4", "%d", &val) < 0)
1274 features &= ~NETIF_F_TSO;
1277 if (features & NETIF_F_TSO6) {
1278 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1279 "feature-gso-tcpv6", "%d", &val) < 0)
1283 features &= ~NETIF_F_TSO6;
1289 static int xennet_set_features(struct net_device *dev,
1290 netdev_features_t features)
1292 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1293 netdev_info(dev, "Reducing MTU because no SG offload");
1294 dev->mtu = ETH_DATA_LEN;
1300 static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
1302 unsigned long flags;
1304 if (unlikely(queue->info->broken))
1307 spin_lock_irqsave(&queue->tx_lock, flags);
1308 if (xennet_tx_buf_gc(queue))
1310 spin_unlock_irqrestore(&queue->tx_lock, flags);
1315 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1317 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1319 if (likely(xennet_handle_tx(dev_id, &eoiflag)))
1320 xen_irq_lateeoi(irq, eoiflag);
1325 static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
1327 unsigned int work_queued;
1328 unsigned long flags;
1330 if (unlikely(queue->info->broken))
1333 spin_lock_irqsave(&queue->rx_cons_lock, flags);
1334 work_queued = RING_HAS_UNCONSUMED_RESPONSES(&queue->rx);
1335 if (work_queued > queue->rx_rsp_unconsumed) {
1336 queue->rx_rsp_unconsumed = work_queued;
1338 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
1339 const struct device *dev = &queue->info->netdev->dev;
1341 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1342 dev_alert(dev, "RX producer index going backwards\n");
1343 dev_alert(dev, "Disabled for further use\n");
1344 queue->info->broken = true;
1347 spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
1349 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
1350 napi_schedule(&queue->napi);
1355 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1357 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1359 if (likely(xennet_handle_rx(dev_id, &eoiflag)))
1360 xen_irq_lateeoi(irq, eoiflag);
1365 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1367 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1369 if (xennet_handle_tx(dev_id, &eoiflag) &&
1370 xennet_handle_rx(dev_id, &eoiflag))
1371 xen_irq_lateeoi(irq, eoiflag);
1376 #ifdef CONFIG_NET_POLL_CONTROLLER
1377 static void xennet_poll_controller(struct net_device *dev)
1379 /* Poll each queue */
1380 struct netfront_info *info = netdev_priv(dev);
1381 unsigned int num_queues = dev->real_num_tx_queues;
1387 for (i = 0; i < num_queues; ++i)
1388 xennet_interrupt(0, &info->queues[i]);
1392 static const struct net_device_ops xennet_netdev_ops = {
1393 .ndo_open = xennet_open,
1394 .ndo_stop = xennet_close,
1395 .ndo_start_xmit = xennet_start_xmit,
1396 .ndo_change_mtu = xennet_change_mtu,
1397 .ndo_get_stats64 = xennet_get_stats64,
1398 .ndo_set_mac_address = eth_mac_addr,
1399 .ndo_validate_addr = eth_validate_addr,
1400 .ndo_fix_features = xennet_fix_features,
1401 .ndo_set_features = xennet_set_features,
1402 .ndo_select_queue = xennet_select_queue,
1403 #ifdef CONFIG_NET_POLL_CONTROLLER
1404 .ndo_poll_controller = xennet_poll_controller,
1408 static void xennet_free_netdev(struct net_device *netdev)
1410 struct netfront_info *np = netdev_priv(netdev);
1412 free_percpu(np->rx_stats);
1413 free_percpu(np->tx_stats);
1414 free_netdev(netdev);
1417 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1420 struct net_device *netdev;
1421 struct netfront_info *np;
1423 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1425 return ERR_PTR(-ENOMEM);
1427 np = netdev_priv(netdev);
1433 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1434 if (np->rx_stats == NULL)
1436 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1437 if (np->tx_stats == NULL)
1440 netdev->netdev_ops = &xennet_netdev_ops;
1442 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1444 netdev->hw_features = NETIF_F_SG |
1446 NETIF_F_TSO | NETIF_F_TSO6;
1449 * Assume that all hw features are available for now. This set
1450 * will be adjusted by the call to netdev_update_features() in
1451 * xennet_connect() which is the earliest point where we can
1452 * negotiate with the backend regarding supported features.
1454 netdev->features |= netdev->hw_features;
1456 netdev->ethtool_ops = &xennet_ethtool_ops;
1457 SET_NETDEV_DEV(netdev, &dev->dev);
1459 np->netdev = netdev;
1461 netif_carrier_off(netdev);
1464 xenbus_switch_state(dev, XenbusStateInitialising);
1465 err = wait_event_timeout(module_wq,
1466 xenbus_read_driver_state(dev->otherend) !=
1467 XenbusStateClosed &&
1468 xenbus_read_driver_state(dev->otherend) !=
1469 XenbusStateUnknown, XENNET_TIMEOUT);
1475 xennet_free_netdev(netdev);
1476 return ERR_PTR(err);
1480 * Entry point to this code when a new device is created. Allocate the basic
1481 * structures and the ring buffers for communication with the backend, and
1482 * inform the backend of the appropriate details for those.
1484 static int netfront_probe(struct xenbus_device *dev,
1485 const struct xenbus_device_id *id)
1488 struct net_device *netdev;
1489 struct netfront_info *info;
1491 netdev = xennet_create_dev(dev);
1492 if (IS_ERR(netdev)) {
1493 err = PTR_ERR(netdev);
1494 xenbus_dev_fatal(dev, err, "creating netdev");
1498 info = netdev_priv(netdev);
1499 dev_set_drvdata(&dev->dev, info);
1501 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1507 static void xennet_end_access(int ref, void *page)
1509 /* This frees the page as a side-effect */
1510 if (ref != GRANT_INVALID_REF)
1511 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1514 static void xennet_disconnect_backend(struct netfront_info *info)
1517 unsigned int num_queues = info->netdev->real_num_tx_queues;
1519 netif_carrier_off(info->netdev);
1521 for (i = 0; i < num_queues && info->queues; ++i) {
1522 struct netfront_queue *queue = &info->queues[i];
1524 del_timer_sync(&queue->rx_refill_timer);
1526 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1527 unbind_from_irqhandler(queue->tx_irq, queue);
1528 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1529 unbind_from_irqhandler(queue->tx_irq, queue);
1530 unbind_from_irqhandler(queue->rx_irq, queue);
1532 queue->tx_evtchn = queue->rx_evtchn = 0;
1533 queue->tx_irq = queue->rx_irq = 0;
1535 if (netif_running(info->netdev))
1536 napi_synchronize(&queue->napi);
1538 xennet_release_tx_bufs(queue);
1539 xennet_release_rx_bufs(queue);
1540 gnttab_free_grant_references(queue->gref_tx_head);
1541 gnttab_free_grant_references(queue->gref_rx_head);
1543 /* End access and free the pages */
1544 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1545 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1547 queue->tx_ring_ref = GRANT_INVALID_REF;
1548 queue->rx_ring_ref = GRANT_INVALID_REF;
1549 queue->tx.sring = NULL;
1550 queue->rx.sring = NULL;
1555 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1556 * driver restart. We tear down our netif structure and recreate it, but
1557 * leave the device-layer structures intact so that this is transparent to the
1558 * rest of the kernel.
1560 static int netfront_resume(struct xenbus_device *dev)
1562 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1564 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1566 netif_tx_lock_bh(info->netdev);
1567 netif_device_detach(info->netdev);
1568 netif_tx_unlock_bh(info->netdev);
1570 xennet_disconnect_backend(info);
1574 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1576 char *s, *e, *macstr;
1579 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1581 return PTR_ERR(macstr);
1583 for (i = 0; i < ETH_ALEN; i++) {
1584 mac[i] = simple_strtoul(s, &e, 16);
1585 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1596 static int setup_netfront_single(struct netfront_queue *queue)
1600 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1604 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1605 xennet_interrupt, 0,
1606 queue->info->netdev->name,
1610 queue->rx_evtchn = queue->tx_evtchn;
1611 queue->rx_irq = queue->tx_irq = err;
1616 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1617 queue->tx_evtchn = 0;
1622 static int setup_netfront_split(struct netfront_queue *queue)
1626 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1629 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1631 goto alloc_rx_evtchn_fail;
1633 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1634 "%s-tx", queue->name);
1635 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
1636 xennet_tx_interrupt, 0,
1637 queue->tx_irq_name, queue);
1640 queue->tx_irq = err;
1642 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1643 "%s-rx", queue->name);
1644 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
1645 xennet_rx_interrupt, 0,
1646 queue->rx_irq_name, queue);
1649 queue->rx_irq = err;
1654 unbind_from_irqhandler(queue->tx_irq, queue);
1657 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1658 queue->rx_evtchn = 0;
1659 alloc_rx_evtchn_fail:
1660 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1661 queue->tx_evtchn = 0;
1666 static int setup_netfront(struct xenbus_device *dev,
1667 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1669 struct xen_netif_tx_sring *txs;
1670 struct xen_netif_rx_sring *rxs;
1674 queue->tx_ring_ref = GRANT_INVALID_REF;
1675 queue->rx_ring_ref = GRANT_INVALID_REF;
1676 queue->rx.sring = NULL;
1677 queue->tx.sring = NULL;
1679 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1682 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1685 SHARED_RING_INIT(txs);
1686 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
1688 err = xenbus_grant_ring(dev, txs, 1, &gref);
1690 goto grant_tx_ring_fail;
1691 queue->tx_ring_ref = gref;
1693 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1696 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1697 goto alloc_rx_ring_fail;
1699 SHARED_RING_INIT(rxs);
1700 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
1702 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1704 goto grant_rx_ring_fail;
1705 queue->rx_ring_ref = gref;
1707 if (feature_split_evtchn)
1708 err = setup_netfront_split(queue);
1709 /* setup single event channel if
1710 * a) feature-split-event-channels == 0
1711 * b) feature-split-event-channels == 1 but failed to setup
1713 if (!feature_split_evtchn || (feature_split_evtchn && err))
1714 err = setup_netfront_single(queue);
1717 goto alloc_evtchn_fail;
1721 /* If we fail to setup netfront, it is safe to just revoke access to
1722 * granted pages because backend is not accessing it at this point.
1725 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
1727 free_page((unsigned long)rxs);
1729 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
1731 free_page((unsigned long)txs);
1736 /* Queue-specific initialisation
1737 * This used to be done in xennet_create_dev() but must now
1740 static int xennet_init_queue(struct netfront_queue *queue)
1746 spin_lock_init(&queue->tx_lock);
1747 spin_lock_init(&queue->rx_lock);
1748 spin_lock_init(&queue->rx_cons_lock);
1750 setup_timer(&queue->rx_refill_timer, rx_refill_timeout,
1751 (unsigned long)queue);
1753 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
1754 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
1757 /* Initialise tx_skb_freelist as a free chain containing every entry. */
1758 queue->tx_skb_freelist = 0;
1759 queue->tx_pend_queue = TX_LINK_NONE;
1760 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1761 queue->tx_link[i] = i + 1;
1762 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1763 queue->grant_tx_page[i] = NULL;
1765 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
1767 /* Clear out rx_skbs */
1768 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1769 queue->rx_skbs[i] = NULL;
1770 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1773 /* A grant for every tx ring slot */
1774 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
1775 &queue->gref_tx_head) < 0) {
1776 pr_alert("can't alloc tx grant refs\n");
1781 /* A grant for every rx ring slot */
1782 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
1783 &queue->gref_rx_head) < 0) {
1784 pr_alert("can't alloc rx grant refs\n");
1792 gnttab_free_grant_references(queue->gref_tx_head);
1797 static int write_queue_xenstore_keys(struct netfront_queue *queue,
1798 struct xenbus_transaction *xbt, int write_hierarchical)
1800 /* Write the queue-specific keys into XenStore in the traditional
1801 * way for a single queue, or in a queue subkeys for multiple
1804 struct xenbus_device *dev = queue->info->xbdev;
1806 const char *message;
1810 /* Choose the correct place to write the keys */
1811 if (write_hierarchical) {
1812 pathsize = strlen(dev->nodename) + 10;
1813 path = kzalloc(pathsize, GFP_KERNEL);
1816 message = "out of memory while writing ring references";
1819 snprintf(path, pathsize, "%s/queue-%u",
1820 dev->nodename, queue->id);
1822 path = (char *)dev->nodename;
1825 /* Write ring references */
1826 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1827 queue->tx_ring_ref);
1829 message = "writing tx-ring-ref";
1833 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1834 queue->rx_ring_ref);
1836 message = "writing rx-ring-ref";
1840 /* Write event channels; taking into account both shared
1841 * and split event channel scenarios.
1843 if (queue->tx_evtchn == queue->rx_evtchn) {
1844 /* Shared event channel */
1845 err = xenbus_printf(*xbt, path,
1846 "event-channel", "%u", queue->tx_evtchn);
1848 message = "writing event-channel";
1852 /* Split event channels */
1853 err = xenbus_printf(*xbt, path,
1854 "event-channel-tx", "%u", queue->tx_evtchn);
1856 message = "writing event-channel-tx";
1860 err = xenbus_printf(*xbt, path,
1861 "event-channel-rx", "%u", queue->rx_evtchn);
1863 message = "writing event-channel-rx";
1868 if (write_hierarchical)
1873 if (write_hierarchical)
1875 xenbus_dev_fatal(dev, err, "%s", message);
1879 static void xennet_destroy_queues(struct netfront_info *info)
1883 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
1884 struct netfront_queue *queue = &info->queues[i];
1886 if (netif_running(info->netdev))
1887 napi_disable(&queue->napi);
1888 netif_napi_del(&queue->napi);
1891 kfree(info->queues);
1892 info->queues = NULL;
1895 static int xennet_create_queues(struct netfront_info *info,
1896 unsigned int *num_queues)
1901 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
1906 for (i = 0; i < *num_queues; i++) {
1907 struct netfront_queue *queue = &info->queues[i];
1912 ret = xennet_init_queue(queue);
1914 dev_warn(&info->xbdev->dev,
1915 "only created %d queues\n", i);
1920 netif_napi_add(queue->info->netdev, &queue->napi,
1922 if (netif_running(info->netdev))
1923 napi_enable(&queue->napi);
1926 netif_set_real_num_tx_queues(info->netdev, *num_queues);
1928 if (*num_queues == 0) {
1929 dev_err(&info->xbdev->dev, "no queues\n");
1935 /* Common code used when first setting up, and when resuming. */
1936 static int talk_to_netback(struct xenbus_device *dev,
1937 struct netfront_info *info)
1939 const char *message;
1940 struct xenbus_transaction xbt;
1942 unsigned int feature_split_evtchn;
1944 unsigned int max_queues = 0;
1945 struct netfront_queue *queue = NULL;
1946 unsigned int num_queues = 1;
1948 info->netdev->irq = 0;
1950 /* Check if backend supports multiple queues */
1951 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1952 "multi-queue-max-queues", "%u", &max_queues);
1955 num_queues = min(max_queues, xennet_max_queues);
1957 /* Check feature-split-event-channels */
1958 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1959 "feature-split-event-channels", "%u",
1960 &feature_split_evtchn);
1962 feature_split_evtchn = 0;
1964 /* Read mac addr. */
1965 err = xen_net_read_mac(dev, info->netdev->dev_addr);
1967 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1973 xennet_destroy_queues(info);
1975 /* For the case of a reconnect reset the "broken" indicator. */
1976 info->broken = false;
1978 err = xennet_create_queues(info, &num_queues);
1980 xenbus_dev_fatal(dev, err, "creating queues");
1981 kfree(info->queues);
1982 info->queues = NULL;
1987 /* Create shared ring, alloc event channel -- for each queue */
1988 for (i = 0; i < num_queues; ++i) {
1989 queue = &info->queues[i];
1990 err = setup_netfront(dev, queue, feature_split_evtchn);
1996 err = xenbus_transaction_start(&xbt);
1998 xenbus_dev_fatal(dev, err, "starting transaction");
2002 if (xenbus_exists(XBT_NIL,
2003 info->xbdev->otherend, "multi-queue-max-queues")) {
2004 /* Write the number of queues */
2005 err = xenbus_printf(xbt, dev->nodename,
2006 "multi-queue-num-queues", "%u", num_queues);
2008 message = "writing multi-queue-num-queues";
2009 goto abort_transaction_no_dev_fatal;
2013 if (num_queues == 1) {
2014 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2016 goto abort_transaction_no_dev_fatal;
2018 /* Write the keys for each queue */
2019 for (i = 0; i < num_queues; ++i) {
2020 queue = &info->queues[i];
2021 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2023 goto abort_transaction_no_dev_fatal;
2027 /* The remaining keys are not queue-specific */
2028 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2031 message = "writing request-rx-copy";
2032 goto abort_transaction;
2035 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2037 message = "writing feature-rx-notify";
2038 goto abort_transaction;
2041 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2043 message = "writing feature-sg";
2044 goto abort_transaction;
2047 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2049 message = "writing feature-gso-tcpv4";
2050 goto abort_transaction;
2053 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2055 message = "writing feature-gso-tcpv6";
2056 goto abort_transaction;
2059 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2062 message = "writing feature-ipv6-csum-offload";
2063 goto abort_transaction;
2066 err = xenbus_transaction_end(xbt, 0);
2070 xenbus_dev_fatal(dev, err, "completing transaction");
2077 xenbus_dev_fatal(dev, err, "%s", message);
2078 abort_transaction_no_dev_fatal:
2079 xenbus_transaction_end(xbt, 1);
2081 xennet_disconnect_backend(info);
2083 xennet_destroy_queues(info);
2087 device_unregister(&dev->dev);
2091 static int xennet_connect(struct net_device *dev)
2093 struct netfront_info *np = netdev_priv(dev);
2094 unsigned int num_queues = 0;
2096 unsigned int feature_rx_copy;
2098 struct netfront_queue *queue = NULL;
2100 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
2101 "feature-rx-copy", "%u", &feature_rx_copy);
2103 feature_rx_copy = 0;
2105 if (!feature_rx_copy) {
2107 "backend does not support copying receive path\n");
2111 err = talk_to_netback(np->xbdev, np);
2115 /* talk_to_netback() sets the correct number of queues */
2116 num_queues = dev->real_num_tx_queues;
2118 if (dev->reg_state == NETREG_UNINITIALIZED) {
2119 err = register_netdev(dev);
2121 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2122 device_unregister(&np->xbdev->dev);
2128 netdev_update_features(dev);
2132 * All public and private state should now be sane. Get
2133 * ready to start sending and receiving packets and give the driver
2134 * domain a kick because we've probably just requeued some
2137 netif_tx_lock_bh(np->netdev);
2138 netif_device_attach(np->netdev);
2139 netif_tx_unlock_bh(np->netdev);
2141 netif_carrier_on(np->netdev);
2142 for (j = 0; j < num_queues; ++j) {
2143 queue = &np->queues[j];
2145 notify_remote_via_irq(queue->tx_irq);
2146 if (queue->tx_irq != queue->rx_irq)
2147 notify_remote_via_irq(queue->rx_irq);
2149 spin_lock_irq(&queue->tx_lock);
2150 xennet_tx_buf_gc(queue);
2151 spin_unlock_irq(&queue->tx_lock);
2153 spin_lock_bh(&queue->rx_lock);
2154 xennet_alloc_rx_buffers(queue);
2155 spin_unlock_bh(&queue->rx_lock);
2162 * Callback received when the backend's state changes.
2164 static void netback_changed(struct xenbus_device *dev,
2165 enum xenbus_state backend_state)
2167 struct netfront_info *np = dev_get_drvdata(&dev->dev);
2168 struct net_device *netdev = np->netdev;
2170 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2172 wake_up_all(&module_wq);
2174 switch (backend_state) {
2175 case XenbusStateInitialising:
2176 case XenbusStateInitialised:
2177 case XenbusStateReconfiguring:
2178 case XenbusStateReconfigured:
2179 case XenbusStateUnknown:
2182 case XenbusStateInitWait:
2183 if (dev->state != XenbusStateInitialising)
2185 if (xennet_connect(netdev) != 0)
2187 xenbus_switch_state(dev, XenbusStateConnected);
2190 case XenbusStateConnected:
2191 netdev_notify_peers(netdev);
2194 case XenbusStateClosed:
2195 if (dev->state == XenbusStateClosed)
2197 /* Missed the backend's CLOSING state -- fallthrough */
2198 case XenbusStateClosing:
2199 xenbus_frontend_closed(dev);
2204 static const struct xennet_stat {
2205 char name[ETH_GSTRING_LEN];
2207 } xennet_stats[] = {
2209 "rx_gso_checksum_fixup",
2210 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2214 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2216 switch (string_set) {
2218 return ARRAY_SIZE(xennet_stats);
2224 static void xennet_get_ethtool_stats(struct net_device *dev,
2225 struct ethtool_stats *stats, u64 * data)
2227 void *np = netdev_priv(dev);
2230 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2231 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2234 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2238 switch (stringset) {
2240 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2241 memcpy(data + i * ETH_GSTRING_LEN,
2242 xennet_stats[i].name, ETH_GSTRING_LEN);
2247 static const struct ethtool_ops xennet_ethtool_ops =
2249 .get_link = ethtool_op_get_link,
2251 .get_sset_count = xennet_get_sset_count,
2252 .get_ethtool_stats = xennet_get_ethtool_stats,
2253 .get_strings = xennet_get_strings,
2257 static ssize_t show_rxbuf(struct device *dev,
2258 struct device_attribute *attr, char *buf)
2260 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2263 static ssize_t store_rxbuf(struct device *dev,
2264 struct device_attribute *attr,
2265 const char *buf, size_t len)
2268 unsigned long target;
2270 if (!capable(CAP_NET_ADMIN))
2273 target = simple_strtoul(buf, &endp, 0);
2277 /* rxbuf_min and rxbuf_max are no longer configurable. */
2282 static DEVICE_ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
2283 static DEVICE_ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
2284 static DEVICE_ATTR(rxbuf_cur, S_IRUGO, show_rxbuf, NULL);
2286 static struct attribute *xennet_dev_attrs[] = {
2287 &dev_attr_rxbuf_min.attr,
2288 &dev_attr_rxbuf_max.attr,
2289 &dev_attr_rxbuf_cur.attr,
2293 static const struct attribute_group xennet_dev_group = {
2294 .attrs = xennet_dev_attrs
2296 #endif /* CONFIG_SYSFS */
2298 static void xennet_bus_close(struct xenbus_device *dev)
2302 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2305 xenbus_switch_state(dev, XenbusStateClosing);
2306 ret = wait_event_timeout(module_wq,
2307 xenbus_read_driver_state(dev->otherend) ==
2308 XenbusStateClosing ||
2309 xenbus_read_driver_state(dev->otherend) ==
2310 XenbusStateClosed ||
2311 xenbus_read_driver_state(dev->otherend) ==
2316 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2320 xenbus_switch_state(dev, XenbusStateClosed);
2321 ret = wait_event_timeout(module_wq,
2322 xenbus_read_driver_state(dev->otherend) ==
2323 XenbusStateClosed ||
2324 xenbus_read_driver_state(dev->otherend) ==
2330 static int xennet_remove(struct xenbus_device *dev)
2332 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2334 xennet_bus_close(dev);
2335 xennet_disconnect_backend(info);
2337 if (info->netdev->reg_state == NETREG_REGISTERED)
2338 unregister_netdev(info->netdev);
2342 xennet_destroy_queues(info);
2345 xennet_free_netdev(info->netdev);
2350 static const struct xenbus_device_id netfront_ids[] = {
2355 static struct xenbus_driver netfront_driver = {
2356 .ids = netfront_ids,
2357 .probe = netfront_probe,
2358 .remove = xennet_remove,
2359 .resume = netfront_resume,
2360 .otherend_changed = netback_changed,
2363 static int __init netif_init(void)
2368 if (!xen_has_pv_nic_devices())
2371 pr_info("Initialising Xen virtual ethernet driver\n");
2373 /* Allow as many queues as there are CPUs if user has not
2374 * specified a value.
2376 if (xennet_max_queues == 0)
2377 xennet_max_queues = num_online_cpus();
2379 return xenbus_register_frontend(&netfront_driver);
2381 module_init(netif_init);
2384 static void __exit netif_exit(void)
2386 xenbus_unregister_driver(&netfront_driver);
2388 module_exit(netif_exit);
2390 MODULE_DESCRIPTION("Xen virtual network device frontend");
2391 MODULE_LICENSE("GPL");
2392 MODULE_ALIAS("xen:vif");
2393 MODULE_ALIAS("xennet");