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>
48 #include <asm/xen/page.h>
50 #include <xen/xenbus.h>
51 #include <xen/events.h>
53 #include <xen/platform_pci.h>
54 #include <xen/grant_table.h>
56 #include <xen/interface/io/netif.h>
57 #include <xen/interface/memory.h>
58 #include <xen/interface/grant_table.h>
60 /* Module parameters */
61 static unsigned int xennet_max_queues;
62 module_param_named(max_queues, xennet_max_queues, uint, 0644);
63 MODULE_PARM_DESC(max_queues,
64 "Maximum number of queues per virtual interface");
66 static const struct ethtool_ops xennet_ethtool_ops;
72 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
74 #define RX_COPY_THRESHOLD 256
76 #define GRANT_INVALID_REF 0
78 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)
79 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
81 /* Minimum number of Rx slots (includes slot for GSO metadata). */
82 #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
84 /* Queue name is interface name with "-qNNN" appended */
85 #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
87 /* IRQ name is queue name with "-tx" or "-rx" appended */
88 #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
90 struct netfront_stats {
95 struct u64_stats_sync syncp;
100 struct netfront_queue {
101 unsigned int id; /* Queue ID, 0-based */
102 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
103 struct netfront_info *info;
105 struct napi_struct napi;
107 /* Split event channels support, tx_* == rx_* when using
108 * single event channel.
110 unsigned int tx_evtchn, rx_evtchn;
111 unsigned int tx_irq, rx_irq;
112 /* Only used when split event channels support is enabled */
113 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
114 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
117 struct xen_netif_tx_front_ring tx;
121 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
122 * are linked from tx_skb_freelist through skb_entry.link.
124 * NB. Freelist index entries are always going to be less than
125 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
126 * greater than PAGE_OFFSET: we use this property to distinguish
132 } tx_skbs[NET_TX_RING_SIZE];
133 grant_ref_t gref_tx_head;
134 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
135 struct page *grant_tx_page[NET_TX_RING_SIZE];
136 unsigned tx_skb_freelist;
138 spinlock_t rx_lock ____cacheline_aligned_in_smp;
139 struct xen_netif_rx_front_ring rx;
142 struct timer_list rx_refill_timer;
144 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
145 grant_ref_t gref_rx_head;
146 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
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 *stats;
161 atomic_t rx_gso_checksum_fixup;
164 struct netfront_rx_info {
165 struct xen_netif_rx_response rx;
166 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
169 static void skb_entry_set_link(union skb_entry *list, unsigned short id)
174 static int skb_entry_is_link(const union skb_entry *list)
176 BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
177 return (unsigned long)list->skb < PAGE_OFFSET;
181 * Access macros for acquiring freeing slots in tx_skbs[].
184 static void add_id_to_freelist(unsigned *head, union skb_entry *list,
187 skb_entry_set_link(&list[id], *head);
191 static unsigned short get_id_from_freelist(unsigned *head,
192 union skb_entry *list)
194 unsigned int id = *head;
195 *head = list[id].link;
199 static int xennet_rxidx(RING_IDX idx)
201 return idx & (NET_RX_RING_SIZE - 1);
204 static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
207 int i = xennet_rxidx(ri);
208 struct sk_buff *skb = queue->rx_skbs[i];
209 queue->rx_skbs[i] = NULL;
213 static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
216 int i = xennet_rxidx(ri);
217 grant_ref_t ref = queue->grant_rx_ref[i];
218 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
223 static int xennet_sysfs_addif(struct net_device *netdev);
224 static void xennet_sysfs_delif(struct net_device *netdev);
225 #else /* !CONFIG_SYSFS */
226 #define xennet_sysfs_addif(dev) (0)
227 #define xennet_sysfs_delif(dev) do { } while (0)
230 static bool xennet_can_sg(struct net_device *dev)
232 return dev->features & NETIF_F_SG;
236 static void rx_refill_timeout(unsigned long data)
238 struct netfront_queue *queue = (struct netfront_queue *)data;
239 napi_schedule(&queue->napi);
242 static int netfront_tx_slot_available(struct netfront_queue *queue)
244 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
245 (NET_TX_RING_SIZE - MAX_SKB_FRAGS - 2);
248 static void xennet_maybe_wake_tx(struct netfront_queue *queue)
250 struct net_device *dev = queue->info->netdev;
251 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
253 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
254 netfront_tx_slot_available(queue) &&
255 likely(netif_running(dev)))
256 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
260 static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
265 skb = __netdev_alloc_skb(queue->info->netdev,
266 RX_COPY_THRESHOLD + NET_IP_ALIGN,
267 GFP_ATOMIC | __GFP_NOWARN);
271 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
276 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
278 /* Align ip header to a 16 bytes boundary */
279 skb_reserve(skb, NET_IP_ALIGN);
280 skb->dev = queue->info->netdev;
286 static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
288 RING_IDX req_prod = queue->rx.req_prod_pvt;
291 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
294 for (req_prod = queue->rx.req_prod_pvt;
295 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
301 struct xen_netif_rx_request *req;
303 skb = xennet_alloc_one_rx_buffer(queue);
307 id = xennet_rxidx(req_prod);
309 BUG_ON(queue->rx_skbs[id]);
310 queue->rx_skbs[id] = skb;
312 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
313 BUG_ON((signed short)ref < 0);
314 queue->grant_rx_ref[id] = ref;
316 pfn = page_to_pfn(skb_frag_page(&skb_shinfo(skb)->frags[0]));
318 req = RING_GET_REQUEST(&queue->rx, req_prod);
319 gnttab_grant_foreign_access_ref(ref,
320 queue->info->xbdev->otherend_id,
328 queue->rx.req_prod_pvt = req_prod;
330 /* Not enough requests? Try again later. */
331 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN) {
332 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
336 wmb(); /* barrier so backend seens requests */
338 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
340 notify_remote_via_irq(queue->rx_irq);
343 static int xennet_open(struct net_device *dev)
345 struct netfront_info *np = netdev_priv(dev);
346 unsigned int num_queues = dev->real_num_tx_queues;
348 struct netfront_queue *queue = NULL;
350 for (i = 0; i < num_queues; ++i) {
351 queue = &np->queues[i];
352 napi_enable(&queue->napi);
354 spin_lock_bh(&queue->rx_lock);
355 if (netif_carrier_ok(dev)) {
356 xennet_alloc_rx_buffers(queue);
357 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
358 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
359 napi_schedule(&queue->napi);
361 spin_unlock_bh(&queue->rx_lock);
364 netif_tx_start_all_queues(dev);
369 static void xennet_tx_buf_gc(struct netfront_queue *queue)
375 BUG_ON(!netif_carrier_ok(queue->info->netdev));
378 prod = queue->tx.sring->rsp_prod;
379 rmb(); /* Ensure we see responses up to 'rp'. */
381 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
382 struct xen_netif_tx_response *txrsp;
384 txrsp = RING_GET_RESPONSE(&queue->tx, cons);
385 if (txrsp->status == XEN_NETIF_RSP_NULL)
389 skb = queue->tx_skbs[id].skb;
390 if (unlikely(gnttab_query_foreign_access(
391 queue->grant_tx_ref[id]) != 0)) {
392 pr_alert("%s: warning -- grant still in use by backend domain\n",
396 gnttab_end_foreign_access_ref(
397 queue->grant_tx_ref[id], GNTMAP_readonly);
398 gnttab_release_grant_reference(
399 &queue->gref_tx_head, queue->grant_tx_ref[id]);
400 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
401 queue->grant_tx_page[id] = NULL;
402 add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, id);
403 dev_kfree_skb_irq(skb);
406 queue->tx.rsp_cons = prod;
409 * Set a new event, then check for race with update of tx_cons.
410 * Note that it is essential to schedule a callback, no matter
411 * how few buffers are pending. Even if there is space in the
412 * transmit ring, higher layers may be blocked because too much
413 * data is outstanding: in such cases notification from Xen is
414 * likely to be the only kick that we'll get.
416 queue->tx.sring->rsp_event =
417 prod + ((queue->tx.sring->req_prod - prod) >> 1) + 1;
418 mb(); /* update shared area */
419 } while ((cons == prod) && (prod != queue->tx.sring->rsp_prod));
421 xennet_maybe_wake_tx(queue);
424 static struct xen_netif_tx_request *xennet_make_one_txreq(
425 struct netfront_queue *queue, struct sk_buff *skb,
426 struct page *page, unsigned int offset, unsigned int len)
429 struct xen_netif_tx_request *tx;
432 len = min_t(unsigned int, PAGE_SIZE - offset, len);
434 id = get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs);
435 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
436 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
437 BUG_ON((signed short)ref < 0);
439 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
440 page_to_mfn(page), GNTMAP_readonly);
442 queue->tx_skbs[id].skb = skb;
443 queue->grant_tx_page[id] = page;
444 queue->grant_tx_ref[id] = ref;
455 static struct xen_netif_tx_request *xennet_make_txreqs(
456 struct netfront_queue *queue, struct xen_netif_tx_request *tx,
457 struct sk_buff *skb, struct page *page,
458 unsigned int offset, unsigned int len)
460 /* Skip unused frames from start of page */
461 page += offset >> PAGE_SHIFT;
462 offset &= ~PAGE_MASK;
465 tx->flags |= XEN_NETTXF_more_data;
466 tx = xennet_make_one_txreq(queue, skb_get(skb),
477 * Count how many ring slots are required to send this skb. Each frag
478 * might be a compound page.
480 static int xennet_count_skb_slots(struct sk_buff *skb)
482 int i, frags = skb_shinfo(skb)->nr_frags;
485 pages = PFN_UP(offset_in_page(skb->data) + skb_headlen(skb));
487 for (i = 0; i < frags; i++) {
488 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
489 unsigned long size = skb_frag_size(frag);
490 unsigned long offset = frag->page_offset;
492 /* Skip unused frames from start of page */
493 offset &= ~PAGE_MASK;
495 pages += PFN_UP(offset + size);
501 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
502 void *accel_priv, select_queue_fallback_t fallback)
504 unsigned int num_queues = dev->real_num_tx_queues;
508 /* First, check if there is only one queue */
509 if (num_queues == 1) {
512 hash = skb_get_hash(skb);
513 queue_idx = hash % num_queues;
519 static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
521 struct netfront_info *np = netdev_priv(dev);
522 struct netfront_stats *stats = this_cpu_ptr(np->stats);
523 struct xen_netif_tx_request *tx, *first_tx;
531 struct netfront_queue *queue = NULL;
532 unsigned int num_queues = dev->real_num_tx_queues;
535 /* Drop the packet if no queues are set up */
538 /* Determine which queue to transmit this SKB on */
539 queue_index = skb_get_queue_mapping(skb);
540 queue = &np->queues[queue_index];
542 /* If skb->len is too big for wire format, drop skb and alert
543 * user about misconfiguration.
545 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
546 net_alert_ratelimited(
547 "xennet: skb->len = %u, too big for wire format\n",
552 slots = xennet_count_skb_slots(skb);
553 if (unlikely(slots > MAX_SKB_FRAGS + 1)) {
554 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
556 if (skb_linearize(skb))
560 page = virt_to_page(skb->data);
561 offset = offset_in_page(skb->data);
562 len = skb_headlen(skb);
564 spin_lock_irqsave(&queue->tx_lock, flags);
566 if (unlikely(!netif_carrier_ok(dev) ||
567 (slots > 1 && !xennet_can_sg(dev)) ||
568 netif_needs_gso(dev, skb, netif_skb_features(skb)))) {
569 spin_unlock_irqrestore(&queue->tx_lock, flags);
573 /* First request for the linear area. */
574 first_tx = tx = xennet_make_one_txreq(queue, skb,
580 if (skb->ip_summed == CHECKSUM_PARTIAL)
582 tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
583 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
584 /* remote but checksummed. */
585 tx->flags |= XEN_NETTXF_data_validated;
587 /* Optional extra info after the first request. */
588 if (skb_shinfo(skb)->gso_size) {
589 struct xen_netif_extra_info *gso;
591 gso = (struct xen_netif_extra_info *)
592 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
594 tx->flags |= XEN_NETTXF_extra_info;
596 gso->u.gso.size = skb_shinfo(skb)->gso_size;
597 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
598 XEN_NETIF_GSO_TYPE_TCPV6 :
599 XEN_NETIF_GSO_TYPE_TCPV4;
601 gso->u.gso.features = 0;
603 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
607 /* Requests for the rest of the linear area. */
608 tx = xennet_make_txreqs(queue, tx, skb, page, offset, len);
610 /* Requests for all the frags. */
611 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
612 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
613 tx = xennet_make_txreqs(queue, tx, skb,
614 skb_frag_page(frag), frag->page_offset,
615 skb_frag_size(frag));
618 /* First request has the packet length. */
619 first_tx->size = skb->len;
621 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
623 notify_remote_via_irq(queue->tx_irq);
625 u64_stats_update_begin(&stats->syncp);
626 stats->tx_bytes += skb->len;
628 u64_stats_update_end(&stats->syncp);
630 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
631 xennet_tx_buf_gc(queue);
633 if (!netfront_tx_slot_available(queue))
634 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
636 spin_unlock_irqrestore(&queue->tx_lock, flags);
641 dev->stats.tx_dropped++;
642 dev_kfree_skb_any(skb);
646 static int xennet_close(struct net_device *dev)
648 struct netfront_info *np = netdev_priv(dev);
649 unsigned int num_queues = dev->real_num_tx_queues;
651 struct netfront_queue *queue;
652 netif_tx_stop_all_queues(np->netdev);
653 for (i = 0; i < num_queues; ++i) {
654 queue = &np->queues[i];
655 napi_disable(&queue->napi);
660 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
663 int new = xennet_rxidx(queue->rx.req_prod_pvt);
665 BUG_ON(queue->rx_skbs[new]);
666 queue->rx_skbs[new] = skb;
667 queue->grant_rx_ref[new] = ref;
668 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
669 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
670 queue->rx.req_prod_pvt++;
673 static int xennet_get_extras(struct netfront_queue *queue,
674 struct xen_netif_extra_info *extras,
678 struct xen_netif_extra_info *extra;
679 struct device *dev = &queue->info->netdev->dev;
680 RING_IDX cons = queue->rx.rsp_cons;
687 if (unlikely(cons + 1 == rp)) {
689 dev_warn(dev, "Missing extra info\n");
694 extra = (struct xen_netif_extra_info *)
695 RING_GET_RESPONSE(&queue->rx, ++cons);
697 if (unlikely(!extra->type ||
698 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
700 dev_warn(dev, "Invalid extra type: %d\n",
704 memcpy(&extras[extra->type - 1], extra,
708 skb = xennet_get_rx_skb(queue, cons);
709 ref = xennet_get_rx_ref(queue, cons);
710 xennet_move_rx_slot(queue, skb, ref);
711 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
713 queue->rx.rsp_cons = cons;
717 static int xennet_get_responses(struct netfront_queue *queue,
718 struct netfront_rx_info *rinfo, RING_IDX rp,
719 struct sk_buff_head *list)
721 struct xen_netif_rx_response *rx = &rinfo->rx;
722 struct xen_netif_extra_info *extras = rinfo->extras;
723 struct device *dev = &queue->info->netdev->dev;
724 RING_IDX cons = queue->rx.rsp_cons;
725 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
726 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
727 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
732 if (rx->flags & XEN_NETRXF_extra_info) {
733 err = xennet_get_extras(queue, extras, rp);
734 cons = queue->rx.rsp_cons;
738 if (unlikely(rx->status < 0 ||
739 rx->offset + rx->status > PAGE_SIZE)) {
741 dev_warn(dev, "rx->offset: %x, size: %u\n",
742 rx->offset, rx->status);
743 xennet_move_rx_slot(queue, skb, ref);
749 * This definitely indicates a bug, either in this driver or in
750 * the backend driver. In future this should flag the bad
751 * situation to the system controller to reboot the backend.
753 if (ref == GRANT_INVALID_REF) {
755 dev_warn(dev, "Bad rx response id %d.\n",
761 ret = gnttab_end_foreign_access_ref(ref, 0);
764 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
766 __skb_queue_tail(list, skb);
769 if (!(rx->flags & XEN_NETRXF_more_data))
772 if (cons + slots == rp) {
774 dev_warn(dev, "Need more slots\n");
779 rx = RING_GET_RESPONSE(&queue->rx, cons + slots);
780 skb = xennet_get_rx_skb(queue, cons + slots);
781 ref = xennet_get_rx_ref(queue, cons + slots);
785 if (unlikely(slots > max)) {
787 dev_warn(dev, "Too many slots\n");
792 queue->rx.rsp_cons = cons + slots;
797 static int xennet_set_skb_gso(struct sk_buff *skb,
798 struct xen_netif_extra_info *gso)
800 if (!gso->u.gso.size) {
802 pr_warn("GSO size must not be zero\n");
806 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
807 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
809 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
813 skb_shinfo(skb)->gso_size = gso->u.gso.size;
814 skb_shinfo(skb)->gso_type =
815 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
819 /* Header must be checked, and gso_segs computed. */
820 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
821 skb_shinfo(skb)->gso_segs = 0;
826 static RING_IDX xennet_fill_frags(struct netfront_queue *queue,
828 struct sk_buff_head *list)
830 struct skb_shared_info *shinfo = skb_shinfo(skb);
831 RING_IDX cons = queue->rx.rsp_cons;
832 struct sk_buff *nskb;
834 while ((nskb = __skb_dequeue(list))) {
835 struct xen_netif_rx_response *rx =
836 RING_GET_RESPONSE(&queue->rx, ++cons);
837 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
839 if (shinfo->nr_frags == MAX_SKB_FRAGS) {
840 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
842 BUG_ON(pull_to <= skb_headlen(skb));
843 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
845 BUG_ON(shinfo->nr_frags >= MAX_SKB_FRAGS);
847 skb_add_rx_frag(skb, shinfo->nr_frags, skb_frag_page(nfrag),
848 rx->offset, rx->status, PAGE_SIZE);
850 skb_shinfo(nskb)->nr_frags = 0;
857 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
859 bool recalculate_partial_csum = false;
862 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
863 * peers can fail to set NETRXF_csum_blank when sending a GSO
864 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
865 * recalculate the partial checksum.
867 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
868 struct netfront_info *np = netdev_priv(dev);
869 atomic_inc(&np->rx_gso_checksum_fixup);
870 skb->ip_summed = CHECKSUM_PARTIAL;
871 recalculate_partial_csum = true;
874 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
875 if (skb->ip_summed != CHECKSUM_PARTIAL)
878 return skb_checksum_setup(skb, recalculate_partial_csum);
881 static int handle_incoming_queue(struct netfront_queue *queue,
882 struct sk_buff_head *rxq)
884 struct netfront_stats *stats = this_cpu_ptr(queue->info->stats);
885 int packets_dropped = 0;
888 while ((skb = __skb_dequeue(rxq)) != NULL) {
889 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
891 if (pull_to > skb_headlen(skb))
892 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
894 /* Ethernet work: Delayed to here as it peeks the header. */
895 skb->protocol = eth_type_trans(skb, queue->info->netdev);
896 skb_reset_network_header(skb);
898 if (checksum_setup(queue->info->netdev, skb)) {
901 queue->info->netdev->stats.rx_errors++;
905 u64_stats_update_begin(&stats->syncp);
907 stats->rx_bytes += skb->len;
908 u64_stats_update_end(&stats->syncp);
911 napi_gro_receive(&queue->napi, skb);
914 return packets_dropped;
917 static int xennet_poll(struct napi_struct *napi, int budget)
919 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
920 struct net_device *dev = queue->info->netdev;
922 struct netfront_rx_info rinfo;
923 struct xen_netif_rx_response *rx = &rinfo.rx;
924 struct xen_netif_extra_info *extras = rinfo.extras;
927 struct sk_buff_head rxq;
928 struct sk_buff_head errq;
929 struct sk_buff_head tmpq;
932 spin_lock(&queue->rx_lock);
934 skb_queue_head_init(&rxq);
935 skb_queue_head_init(&errq);
936 skb_queue_head_init(&tmpq);
938 rp = queue->rx.sring->rsp_prod;
939 rmb(); /* Ensure we see queued responses up to 'rp'. */
941 i = queue->rx.rsp_cons;
943 while ((i != rp) && (work_done < budget)) {
944 memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx));
945 memset(extras, 0, sizeof(rinfo.extras));
947 err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
951 while ((skb = __skb_dequeue(&tmpq)))
952 __skb_queue_tail(&errq, skb);
953 dev->stats.rx_errors++;
954 i = queue->rx.rsp_cons;
958 skb = __skb_dequeue(&tmpq);
960 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
961 struct xen_netif_extra_info *gso;
962 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
964 if (unlikely(xennet_set_skb_gso(skb, gso))) {
965 __skb_queue_head(&tmpq, skb);
966 queue->rx.rsp_cons += skb_queue_len(&tmpq);
971 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
972 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
973 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
975 skb_shinfo(skb)->frags[0].page_offset = rx->offset;
976 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
977 skb->data_len = rx->status;
978 skb->len += rx->status;
980 i = xennet_fill_frags(queue, skb, &tmpq);
982 if (rx->flags & XEN_NETRXF_csum_blank)
983 skb->ip_summed = CHECKSUM_PARTIAL;
984 else if (rx->flags & XEN_NETRXF_data_validated)
985 skb->ip_summed = CHECKSUM_UNNECESSARY;
987 __skb_queue_tail(&rxq, skb);
989 queue->rx.rsp_cons = ++i;
993 __skb_queue_purge(&errq);
995 work_done -= handle_incoming_queue(queue, &rxq);
997 xennet_alloc_rx_buffers(queue);
999 if (work_done < budget) {
1002 napi_complete(napi);
1004 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
1006 napi_schedule(napi);
1009 spin_unlock(&queue->rx_lock);
1014 static int xennet_change_mtu(struct net_device *dev, int mtu)
1016 int max = xennet_can_sg(dev) ?
1017 XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER : ETH_DATA_LEN;
1025 static struct rtnl_link_stats64 *xennet_get_stats64(struct net_device *dev,
1026 struct rtnl_link_stats64 *tot)
1028 struct netfront_info *np = netdev_priv(dev);
1031 for_each_possible_cpu(cpu) {
1032 struct netfront_stats *stats = per_cpu_ptr(np->stats, cpu);
1033 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1037 start = u64_stats_fetch_begin_irq(&stats->syncp);
1039 rx_packets = stats->rx_packets;
1040 tx_packets = stats->tx_packets;
1041 rx_bytes = stats->rx_bytes;
1042 tx_bytes = stats->tx_bytes;
1043 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1045 tot->rx_packets += rx_packets;
1046 tot->tx_packets += tx_packets;
1047 tot->rx_bytes += rx_bytes;
1048 tot->tx_bytes += tx_bytes;
1051 tot->rx_errors = dev->stats.rx_errors;
1052 tot->tx_dropped = dev->stats.tx_dropped;
1057 static void xennet_release_tx_bufs(struct netfront_queue *queue)
1059 struct sk_buff *skb;
1062 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1063 /* Skip over entries which are actually freelist references */
1064 if (skb_entry_is_link(&queue->tx_skbs[i]))
1067 skb = queue->tx_skbs[i].skb;
1068 get_page(queue->grant_tx_page[i]);
1069 gnttab_end_foreign_access(queue->grant_tx_ref[i],
1071 (unsigned long)page_address(queue->grant_tx_page[i]));
1072 queue->grant_tx_page[i] = NULL;
1073 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1074 add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, i);
1075 dev_kfree_skb_irq(skb);
1079 static void xennet_release_rx_bufs(struct netfront_queue *queue)
1083 spin_lock_bh(&queue->rx_lock);
1085 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1086 struct sk_buff *skb;
1089 skb = queue->rx_skbs[id];
1093 ref = queue->grant_rx_ref[id];
1094 if (ref == GRANT_INVALID_REF)
1097 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1099 /* gnttab_end_foreign_access() needs a page ref until
1100 * foreign access is ended (which may be deferred).
1103 gnttab_end_foreign_access(ref, 0,
1104 (unsigned long)page_address(page));
1105 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
1110 spin_unlock_bh(&queue->rx_lock);
1113 static netdev_features_t xennet_fix_features(struct net_device *dev,
1114 netdev_features_t features)
1116 struct netfront_info *np = netdev_priv(dev);
1119 if (features & NETIF_F_SG) {
1120 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1125 features &= ~NETIF_F_SG;
1128 if (features & NETIF_F_IPV6_CSUM) {
1129 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1130 "feature-ipv6-csum-offload", "%d", &val) < 0)
1134 features &= ~NETIF_F_IPV6_CSUM;
1137 if (features & NETIF_F_TSO) {
1138 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1139 "feature-gso-tcpv4", "%d", &val) < 0)
1143 features &= ~NETIF_F_TSO;
1146 if (features & NETIF_F_TSO6) {
1147 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1148 "feature-gso-tcpv6", "%d", &val) < 0)
1152 features &= ~NETIF_F_TSO6;
1158 static int xennet_set_features(struct net_device *dev,
1159 netdev_features_t features)
1161 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1162 netdev_info(dev, "Reducing MTU because no SG offload");
1163 dev->mtu = ETH_DATA_LEN;
1169 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
1171 struct netfront_queue *queue = dev_id;
1172 unsigned long flags;
1174 spin_lock_irqsave(&queue->tx_lock, flags);
1175 xennet_tx_buf_gc(queue);
1176 spin_unlock_irqrestore(&queue->tx_lock, flags);
1181 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1183 struct netfront_queue *queue = dev_id;
1184 struct net_device *dev = queue->info->netdev;
1186 if (likely(netif_carrier_ok(dev) &&
1187 RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
1188 napi_schedule(&queue->napi);
1193 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1195 xennet_tx_interrupt(irq, dev_id);
1196 xennet_rx_interrupt(irq, dev_id);
1200 #ifdef CONFIG_NET_POLL_CONTROLLER
1201 static void xennet_poll_controller(struct net_device *dev)
1203 /* Poll each queue */
1204 struct netfront_info *info = netdev_priv(dev);
1205 unsigned int num_queues = dev->real_num_tx_queues;
1207 for (i = 0; i < num_queues; ++i)
1208 xennet_interrupt(0, &info->queues[i]);
1212 static const struct net_device_ops xennet_netdev_ops = {
1213 .ndo_open = xennet_open,
1214 .ndo_stop = xennet_close,
1215 .ndo_start_xmit = xennet_start_xmit,
1216 .ndo_change_mtu = xennet_change_mtu,
1217 .ndo_get_stats64 = xennet_get_stats64,
1218 .ndo_set_mac_address = eth_mac_addr,
1219 .ndo_validate_addr = eth_validate_addr,
1220 .ndo_fix_features = xennet_fix_features,
1221 .ndo_set_features = xennet_set_features,
1222 .ndo_select_queue = xennet_select_queue,
1223 #ifdef CONFIG_NET_POLL_CONTROLLER
1224 .ndo_poll_controller = xennet_poll_controller,
1228 static struct net_device *xennet_create_dev(struct xenbus_device *dev)
1231 struct net_device *netdev;
1232 struct netfront_info *np;
1234 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
1236 return ERR_PTR(-ENOMEM);
1238 np = netdev_priv(netdev);
1241 /* No need to use rtnl_lock() before the call below as it
1242 * happens before register_netdev().
1244 netif_set_real_num_tx_queues(netdev, 0);
1248 np->stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1249 if (np->stats == NULL)
1252 netdev->netdev_ops = &xennet_netdev_ops;
1254 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1256 netdev->hw_features = NETIF_F_SG |
1258 NETIF_F_TSO | NETIF_F_TSO6;
1261 * Assume that all hw features are available for now. This set
1262 * will be adjusted by the call to netdev_update_features() in
1263 * xennet_connect() which is the earliest point where we can
1264 * negotiate with the backend regarding supported features.
1266 netdev->features |= netdev->hw_features;
1268 netdev->ethtool_ops = &xennet_ethtool_ops;
1269 SET_NETDEV_DEV(netdev, &dev->dev);
1271 netif_set_gso_max_size(netdev, XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER);
1273 np->netdev = netdev;
1275 netif_carrier_off(netdev);
1280 free_netdev(netdev);
1281 return ERR_PTR(err);
1285 * Entry point to this code when a new device is created. Allocate the basic
1286 * structures and the ring buffers for communication with the backend, and
1287 * inform the backend of the appropriate details for those.
1289 static int netfront_probe(struct xenbus_device *dev,
1290 const struct xenbus_device_id *id)
1293 struct net_device *netdev;
1294 struct netfront_info *info;
1296 netdev = xennet_create_dev(dev);
1297 if (IS_ERR(netdev)) {
1298 err = PTR_ERR(netdev);
1299 xenbus_dev_fatal(dev, err, "creating netdev");
1303 info = netdev_priv(netdev);
1304 dev_set_drvdata(&dev->dev, info);
1306 err = register_netdev(info->netdev);
1308 pr_warn("%s: register_netdev err=%d\n", __func__, err);
1312 err = xennet_sysfs_addif(info->netdev);
1314 unregister_netdev(info->netdev);
1315 pr_warn("%s: add sysfs failed err=%d\n", __func__, err);
1322 free_netdev(netdev);
1323 dev_set_drvdata(&dev->dev, NULL);
1327 static void xennet_end_access(int ref, void *page)
1329 /* This frees the page as a side-effect */
1330 if (ref != GRANT_INVALID_REF)
1331 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1334 static void xennet_disconnect_backend(struct netfront_info *info)
1337 unsigned int num_queues = info->netdev->real_num_tx_queues;
1339 netif_carrier_off(info->netdev);
1341 for (i = 0; i < num_queues; ++i) {
1342 struct netfront_queue *queue = &info->queues[i];
1344 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1345 unbind_from_irqhandler(queue->tx_irq, queue);
1346 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1347 unbind_from_irqhandler(queue->tx_irq, queue);
1348 unbind_from_irqhandler(queue->rx_irq, queue);
1350 queue->tx_evtchn = queue->rx_evtchn = 0;
1351 queue->tx_irq = queue->rx_irq = 0;
1353 napi_synchronize(&queue->napi);
1355 xennet_release_tx_bufs(queue);
1356 xennet_release_rx_bufs(queue);
1357 gnttab_free_grant_references(queue->gref_tx_head);
1358 gnttab_free_grant_references(queue->gref_rx_head);
1360 /* End access and free the pages */
1361 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1362 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1364 queue->tx_ring_ref = GRANT_INVALID_REF;
1365 queue->rx_ring_ref = GRANT_INVALID_REF;
1366 queue->tx.sring = NULL;
1367 queue->rx.sring = NULL;
1372 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1373 * driver restart. We tear down our netif structure and recreate it, but
1374 * leave the device-layer structures intact so that this is transparent to the
1375 * rest of the kernel.
1377 static int netfront_resume(struct xenbus_device *dev)
1379 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1381 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1383 xennet_disconnect_backend(info);
1387 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1389 char *s, *e, *macstr;
1392 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1394 return PTR_ERR(macstr);
1396 for (i = 0; i < ETH_ALEN; i++) {
1397 mac[i] = simple_strtoul(s, &e, 16);
1398 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1409 static int setup_netfront_single(struct netfront_queue *queue)
1413 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1417 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
1419 0, queue->info->netdev->name, queue);
1422 queue->rx_evtchn = queue->tx_evtchn;
1423 queue->rx_irq = queue->tx_irq = err;
1428 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1429 queue->tx_evtchn = 0;
1434 static int setup_netfront_split(struct netfront_queue *queue)
1438 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
1441 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
1443 goto alloc_rx_evtchn_fail;
1445 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1446 "%s-tx", queue->name);
1447 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
1448 xennet_tx_interrupt,
1449 0, queue->tx_irq_name, queue);
1452 queue->tx_irq = err;
1454 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1455 "%s-rx", queue->name);
1456 err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
1457 xennet_rx_interrupt,
1458 0, queue->rx_irq_name, queue);
1461 queue->rx_irq = err;
1466 unbind_from_irqhandler(queue->tx_irq, queue);
1469 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1470 queue->rx_evtchn = 0;
1471 alloc_rx_evtchn_fail:
1472 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1473 queue->tx_evtchn = 0;
1478 static int setup_netfront(struct xenbus_device *dev,
1479 struct netfront_queue *queue, unsigned int feature_split_evtchn)
1481 struct xen_netif_tx_sring *txs;
1482 struct xen_netif_rx_sring *rxs;
1485 queue->tx_ring_ref = GRANT_INVALID_REF;
1486 queue->rx_ring_ref = GRANT_INVALID_REF;
1487 queue->rx.sring = NULL;
1488 queue->tx.sring = NULL;
1490 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1493 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1496 SHARED_RING_INIT(txs);
1497 FRONT_RING_INIT(&queue->tx, txs, PAGE_SIZE);
1499 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
1501 goto grant_tx_ring_fail;
1502 queue->tx_ring_ref = err;
1504 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1507 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1508 goto alloc_rx_ring_fail;
1510 SHARED_RING_INIT(rxs);
1511 FRONT_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
1513 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
1515 goto grant_rx_ring_fail;
1516 queue->rx_ring_ref = err;
1518 if (feature_split_evtchn)
1519 err = setup_netfront_split(queue);
1520 /* setup single event channel if
1521 * a) feature-split-event-channels == 0
1522 * b) feature-split-event-channels == 1 but failed to setup
1524 if (!feature_split_evtchn || (feature_split_evtchn && err))
1525 err = setup_netfront_single(queue);
1528 goto alloc_evtchn_fail;
1532 /* If we fail to setup netfront, it is safe to just revoke access to
1533 * granted pages because backend is not accessing it at this point.
1536 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
1538 free_page((unsigned long)rxs);
1540 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
1542 free_page((unsigned long)txs);
1547 /* Queue-specific initialisation
1548 * This used to be done in xennet_create_dev() but must now
1551 static int xennet_init_queue(struct netfront_queue *queue)
1556 spin_lock_init(&queue->tx_lock);
1557 spin_lock_init(&queue->rx_lock);
1559 init_timer(&queue->rx_refill_timer);
1560 queue->rx_refill_timer.data = (unsigned long)queue;
1561 queue->rx_refill_timer.function = rx_refill_timeout;
1563 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
1564 queue->info->netdev->name, queue->id);
1566 /* Initialise tx_skbs as a free chain containing every entry. */
1567 queue->tx_skb_freelist = 0;
1568 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1569 skb_entry_set_link(&queue->tx_skbs[i], i+1);
1570 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1571 queue->grant_tx_page[i] = NULL;
1574 /* Clear out rx_skbs */
1575 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1576 queue->rx_skbs[i] = NULL;
1577 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1580 /* A grant for every tx ring slot */
1581 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
1582 &queue->gref_tx_head) < 0) {
1583 pr_alert("can't alloc tx grant refs\n");
1588 /* A grant for every rx ring slot */
1589 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
1590 &queue->gref_rx_head) < 0) {
1591 pr_alert("can't alloc rx grant refs\n");
1599 gnttab_free_grant_references(queue->gref_tx_head);
1604 static int write_queue_xenstore_keys(struct netfront_queue *queue,
1605 struct xenbus_transaction *xbt, int write_hierarchical)
1607 /* Write the queue-specific keys into XenStore in the traditional
1608 * way for a single queue, or in a queue subkeys for multiple
1611 struct xenbus_device *dev = queue->info->xbdev;
1613 const char *message;
1617 /* Choose the correct place to write the keys */
1618 if (write_hierarchical) {
1619 pathsize = strlen(dev->nodename) + 10;
1620 path = kzalloc(pathsize, GFP_KERNEL);
1623 message = "out of memory while writing ring references";
1626 snprintf(path, pathsize, "%s/queue-%u",
1627 dev->nodename, queue->id);
1629 path = (char *)dev->nodename;
1632 /* Write ring references */
1633 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1634 queue->tx_ring_ref);
1636 message = "writing tx-ring-ref";
1640 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1641 queue->rx_ring_ref);
1643 message = "writing rx-ring-ref";
1647 /* Write event channels; taking into account both shared
1648 * and split event channel scenarios.
1650 if (queue->tx_evtchn == queue->rx_evtchn) {
1651 /* Shared event channel */
1652 err = xenbus_printf(*xbt, path,
1653 "event-channel", "%u", queue->tx_evtchn);
1655 message = "writing event-channel";
1659 /* Split event channels */
1660 err = xenbus_printf(*xbt, path,
1661 "event-channel-tx", "%u", queue->tx_evtchn);
1663 message = "writing event-channel-tx";
1667 err = xenbus_printf(*xbt, path,
1668 "event-channel-rx", "%u", queue->rx_evtchn);
1670 message = "writing event-channel-rx";
1675 if (write_hierarchical)
1680 if (write_hierarchical)
1682 xenbus_dev_fatal(dev, err, "%s", message);
1686 static void xennet_destroy_queues(struct netfront_info *info)
1692 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
1693 struct netfront_queue *queue = &info->queues[i];
1695 if (netif_running(info->netdev))
1696 napi_disable(&queue->napi);
1697 netif_napi_del(&queue->napi);
1702 kfree(info->queues);
1703 info->queues = NULL;
1706 static int xennet_create_queues(struct netfront_info *info,
1707 unsigned int num_queues)
1712 info->queues = kcalloc(num_queues, sizeof(struct netfront_queue),
1719 for (i = 0; i < num_queues; i++) {
1720 struct netfront_queue *queue = &info->queues[i];
1725 ret = xennet_init_queue(queue);
1727 dev_warn(&info->netdev->dev,
1728 "only created %d queues\n", i);
1733 netif_napi_add(queue->info->netdev, &queue->napi,
1735 if (netif_running(info->netdev))
1736 napi_enable(&queue->napi);
1739 netif_set_real_num_tx_queues(info->netdev, num_queues);
1743 if (num_queues == 0) {
1744 dev_err(&info->netdev->dev, "no queues\n");
1750 /* Common code used when first setting up, and when resuming. */
1751 static int talk_to_netback(struct xenbus_device *dev,
1752 struct netfront_info *info)
1754 const char *message;
1755 struct xenbus_transaction xbt;
1757 unsigned int feature_split_evtchn;
1759 unsigned int max_queues = 0;
1760 struct netfront_queue *queue = NULL;
1761 unsigned int num_queues = 1;
1763 info->netdev->irq = 0;
1765 /* Check if backend supports multiple queues */
1766 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1767 "multi-queue-max-queues", "%u", &max_queues);
1770 num_queues = min(max_queues, xennet_max_queues);
1772 /* Check feature-split-event-channels */
1773 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1774 "feature-split-event-channels", "%u",
1775 &feature_split_evtchn);
1777 feature_split_evtchn = 0;
1779 /* Read mac addr. */
1780 err = xen_net_read_mac(dev, info->netdev->dev_addr);
1782 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1787 xennet_destroy_queues(info);
1789 err = xennet_create_queues(info, num_queues);
1793 /* Create shared ring, alloc event channel -- for each queue */
1794 for (i = 0; i < num_queues; ++i) {
1795 queue = &info->queues[i];
1796 err = setup_netfront(dev, queue, feature_split_evtchn);
1798 /* setup_netfront() will tidy up the current
1799 * queue on error, but we need to clean up
1800 * those already allocated.
1804 netif_set_real_num_tx_queues(info->netdev, i);
1814 err = xenbus_transaction_start(&xbt);
1816 xenbus_dev_fatal(dev, err, "starting transaction");
1820 if (num_queues == 1) {
1821 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
1823 goto abort_transaction_no_dev_fatal;
1825 /* Write the number of queues */
1826 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues",
1829 message = "writing multi-queue-num-queues";
1830 goto abort_transaction_no_dev_fatal;
1833 /* Write the keys for each queue */
1834 for (i = 0; i < num_queues; ++i) {
1835 queue = &info->queues[i];
1836 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
1838 goto abort_transaction_no_dev_fatal;
1842 /* The remaining keys are not queue-specific */
1843 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1846 message = "writing request-rx-copy";
1847 goto abort_transaction;
1850 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1852 message = "writing feature-rx-notify";
1853 goto abort_transaction;
1856 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1858 message = "writing feature-sg";
1859 goto abort_transaction;
1862 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1864 message = "writing feature-gso-tcpv4";
1865 goto abort_transaction;
1868 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
1870 message = "writing feature-gso-tcpv6";
1871 goto abort_transaction;
1874 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
1877 message = "writing feature-ipv6-csum-offload";
1878 goto abort_transaction;
1881 err = xenbus_transaction_end(xbt, 0);
1885 xenbus_dev_fatal(dev, err, "completing transaction");
1892 xenbus_dev_fatal(dev, err, "%s", message);
1893 abort_transaction_no_dev_fatal:
1894 xenbus_transaction_end(xbt, 1);
1896 xennet_disconnect_backend(info);
1897 kfree(info->queues);
1898 info->queues = NULL;
1900 netif_set_real_num_tx_queues(info->netdev, 0);
1906 static int xennet_connect(struct net_device *dev)
1908 struct netfront_info *np = netdev_priv(dev);
1909 unsigned int num_queues = 0;
1911 unsigned int feature_rx_copy;
1913 struct netfront_queue *queue = NULL;
1915 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1916 "feature-rx-copy", "%u", &feature_rx_copy);
1918 feature_rx_copy = 0;
1920 if (!feature_rx_copy) {
1922 "backend does not support copying receive path\n");
1926 err = talk_to_netback(np->xbdev, np);
1930 /* talk_to_netback() sets the correct number of queues */
1931 num_queues = dev->real_num_tx_queues;
1934 netdev_update_features(dev);
1938 * All public and private state should now be sane. Get
1939 * ready to start sending and receiving packets and give the driver
1940 * domain a kick because we've probably just requeued some
1943 netif_carrier_on(np->netdev);
1944 for (j = 0; j < num_queues; ++j) {
1945 queue = &np->queues[j];
1947 notify_remote_via_irq(queue->tx_irq);
1948 if (queue->tx_irq != queue->rx_irq)
1949 notify_remote_via_irq(queue->rx_irq);
1951 spin_lock_irq(&queue->tx_lock);
1952 xennet_tx_buf_gc(queue);
1953 spin_unlock_irq(&queue->tx_lock);
1955 spin_lock_bh(&queue->rx_lock);
1956 xennet_alloc_rx_buffers(queue);
1957 spin_unlock_bh(&queue->rx_lock);
1964 * Callback received when the backend's state changes.
1966 static void netback_changed(struct xenbus_device *dev,
1967 enum xenbus_state backend_state)
1969 struct netfront_info *np = dev_get_drvdata(&dev->dev);
1970 struct net_device *netdev = np->netdev;
1972 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
1974 switch (backend_state) {
1975 case XenbusStateInitialising:
1976 case XenbusStateInitialised:
1977 case XenbusStateReconfiguring:
1978 case XenbusStateReconfigured:
1979 case XenbusStateUnknown:
1982 case XenbusStateInitWait:
1983 if (dev->state != XenbusStateInitialising)
1985 if (xennet_connect(netdev) != 0)
1987 xenbus_switch_state(dev, XenbusStateConnected);
1990 case XenbusStateConnected:
1991 netdev_notify_peers(netdev);
1994 case XenbusStateClosed:
1995 if (dev->state == XenbusStateClosed)
1997 /* Missed the backend's CLOSING state -- fallthrough */
1998 case XenbusStateClosing:
1999 xenbus_frontend_closed(dev);
2004 static const struct xennet_stat {
2005 char name[ETH_GSTRING_LEN];
2007 } xennet_stats[] = {
2009 "rx_gso_checksum_fixup",
2010 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2014 static int xennet_get_sset_count(struct net_device *dev, int string_set)
2016 switch (string_set) {
2018 return ARRAY_SIZE(xennet_stats);
2024 static void xennet_get_ethtool_stats(struct net_device *dev,
2025 struct ethtool_stats *stats, u64 * data)
2027 void *np = netdev_priv(dev);
2030 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2031 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
2034 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2038 switch (stringset) {
2040 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2041 memcpy(data + i * ETH_GSTRING_LEN,
2042 xennet_stats[i].name, ETH_GSTRING_LEN);
2047 static const struct ethtool_ops xennet_ethtool_ops =
2049 .get_link = ethtool_op_get_link,
2051 .get_sset_count = xennet_get_sset_count,
2052 .get_ethtool_stats = xennet_get_ethtool_stats,
2053 .get_strings = xennet_get_strings,
2057 static ssize_t show_rxbuf(struct device *dev,
2058 struct device_attribute *attr, char *buf)
2060 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
2063 static ssize_t store_rxbuf(struct device *dev,
2064 struct device_attribute *attr,
2065 const char *buf, size_t len)
2068 unsigned long target;
2070 if (!capable(CAP_NET_ADMIN))
2073 target = simple_strtoul(buf, &endp, 0);
2077 /* rxbuf_min and rxbuf_max are no longer configurable. */
2082 static struct device_attribute xennet_attrs[] = {
2083 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf),
2084 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf),
2085 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf, NULL),
2088 static int xennet_sysfs_addif(struct net_device *netdev)
2093 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
2094 err = device_create_file(&netdev->dev,
2103 device_remove_file(&netdev->dev, &xennet_attrs[i]);
2107 static void xennet_sysfs_delif(struct net_device *netdev)
2111 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
2112 device_remove_file(&netdev->dev, &xennet_attrs[i]);
2115 #endif /* CONFIG_SYSFS */
2117 static int xennet_remove(struct xenbus_device *dev)
2119 struct netfront_info *info = dev_get_drvdata(&dev->dev);
2120 unsigned int num_queues = info->netdev->real_num_tx_queues;
2121 struct netfront_queue *queue = NULL;
2124 dev_dbg(&dev->dev, "%s\n", dev->nodename);
2126 xennet_disconnect_backend(info);
2128 xennet_sysfs_delif(info->netdev);
2130 unregister_netdev(info->netdev);
2132 for (i = 0; i < num_queues; ++i) {
2133 queue = &info->queues[i];
2134 del_timer_sync(&queue->rx_refill_timer);
2138 kfree(info->queues);
2139 info->queues = NULL;
2142 free_percpu(info->stats);
2144 free_netdev(info->netdev);
2149 static const struct xenbus_device_id netfront_ids[] = {
2154 static struct xenbus_driver netfront_driver = {
2155 .ids = netfront_ids,
2156 .probe = netfront_probe,
2157 .remove = xennet_remove,
2158 .resume = netfront_resume,
2159 .otherend_changed = netback_changed,
2162 static int __init netif_init(void)
2167 if (!xen_has_pv_nic_devices())
2170 pr_info("Initialising Xen virtual ethernet driver\n");
2172 /* Allow as many queues as there are CPUs, by default */
2173 xennet_max_queues = num_online_cpus();
2175 return xenbus_register_frontend(&netfront_driver);
2177 module_init(netif_init);
2180 static void __exit netif_exit(void)
2182 xenbus_unregister_driver(&netfront_driver);
2184 module_exit(netif_exit);
2186 MODULE_DESCRIPTION("Xen virtual network device frontend");
2187 MODULE_LICENSE("GPL");
2188 MODULE_ALIAS("xen:vif");
2189 MODULE_ALIAS("xennet");