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
{
93 struct u64_stats_sync syncp
;
98 struct netfront_queue
{
99 unsigned int id
; /* Queue ID, 0-based */
100 char name
[QUEUE_NAME_SIZE
]; /* DEVNAME-qN */
101 struct netfront_info
*info
;
103 struct napi_struct napi
;
105 /* Split event channels support, tx_* == rx_* when using
106 * single event channel.
108 unsigned int tx_evtchn
, rx_evtchn
;
109 unsigned int tx_irq
, rx_irq
;
110 /* Only used when split event channels support is enabled */
111 char tx_irq_name
[IRQ_NAME_SIZE
]; /* DEVNAME-qN-tx */
112 char rx_irq_name
[IRQ_NAME_SIZE
]; /* DEVNAME-qN-rx */
115 struct xen_netif_tx_front_ring tx
;
119 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
120 * are linked from tx_skb_freelist through skb_entry.link.
122 * NB. Freelist index entries are always going to be less than
123 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
124 * greater than PAGE_OFFSET: we use this property to distinguish
130 } tx_skbs
[NET_TX_RING_SIZE
];
131 grant_ref_t gref_tx_head
;
132 grant_ref_t grant_tx_ref
[NET_TX_RING_SIZE
];
133 struct page
*grant_tx_page
[NET_TX_RING_SIZE
];
134 unsigned tx_skb_freelist
;
136 spinlock_t rx_lock ____cacheline_aligned_in_smp
;
137 struct xen_netif_rx_front_ring rx
;
140 struct timer_list rx_refill_timer
;
142 struct sk_buff
*rx_skbs
[NET_RX_RING_SIZE
];
143 grant_ref_t gref_rx_head
;
144 grant_ref_t grant_rx_ref
[NET_RX_RING_SIZE
];
146 unsigned long rx_pfn_array
[NET_RX_RING_SIZE
];
147 struct multicall_entry rx_mcl
[NET_RX_RING_SIZE
+1];
148 struct mmu_update rx_mmu
[NET_RX_RING_SIZE
];
151 struct netfront_info
{
152 struct list_head list
;
153 struct net_device
*netdev
;
155 struct xenbus_device
*xbdev
;
157 /* Multi-queue support */
158 struct netfront_queue
*queues
;
161 struct netfront_stats __percpu
*rx_stats
;
162 struct netfront_stats __percpu
*tx_stats
;
164 atomic_t rx_gso_checksum_fixup
;
167 struct netfront_rx_info
{
168 struct xen_netif_rx_response rx
;
169 struct xen_netif_extra_info extras
[XEN_NETIF_EXTRA_TYPE_MAX
- 1];
172 static void skb_entry_set_link(union skb_entry
*list
, unsigned short id
)
177 static int skb_entry_is_link(const union skb_entry
*list
)
179 BUILD_BUG_ON(sizeof(list
->skb
) != sizeof(list
->link
));
180 return (unsigned long)list
->skb
< PAGE_OFFSET
;
184 * Access macros for acquiring freeing slots in tx_skbs[].
187 static void add_id_to_freelist(unsigned *head
, union skb_entry
*list
,
190 skb_entry_set_link(&list
[id
], *head
);
194 static unsigned short get_id_from_freelist(unsigned *head
,
195 union skb_entry
*list
)
197 unsigned int id
= *head
;
198 *head
= list
[id
].link
;
202 static int xennet_rxidx(RING_IDX idx
)
204 return idx
& (NET_RX_RING_SIZE
- 1);
207 static struct sk_buff
*xennet_get_rx_skb(struct netfront_queue
*queue
,
210 int i
= xennet_rxidx(ri
);
211 struct sk_buff
*skb
= queue
->rx_skbs
[i
];
212 queue
->rx_skbs
[i
] = NULL
;
216 static grant_ref_t
xennet_get_rx_ref(struct netfront_queue
*queue
,
219 int i
= xennet_rxidx(ri
);
220 grant_ref_t ref
= queue
->grant_rx_ref
[i
];
221 queue
->grant_rx_ref
[i
] = GRANT_INVALID_REF
;
226 static int xennet_sysfs_addif(struct net_device
*netdev
);
227 static void xennet_sysfs_delif(struct net_device
*netdev
);
228 #else /* !CONFIG_SYSFS */
229 #define xennet_sysfs_addif(dev) (0)
230 #define xennet_sysfs_delif(dev) do { } while (0)
233 static bool xennet_can_sg(struct net_device
*dev
)
235 return dev
->features
& NETIF_F_SG
;
239 static void rx_refill_timeout(unsigned long data
)
241 struct netfront_queue
*queue
= (struct netfront_queue
*)data
;
242 napi_schedule(&queue
->napi
);
245 static int netfront_tx_slot_available(struct netfront_queue
*queue
)
247 return (queue
->tx
.req_prod_pvt
- queue
->tx
.rsp_cons
) <
248 (NET_TX_RING_SIZE
- MAX_SKB_FRAGS
- 2);
251 static void xennet_maybe_wake_tx(struct netfront_queue
*queue
)
253 struct net_device
*dev
= queue
->info
->netdev
;
254 struct netdev_queue
*dev_queue
= netdev_get_tx_queue(dev
, queue
->id
);
256 if (unlikely(netif_tx_queue_stopped(dev_queue
)) &&
257 netfront_tx_slot_available(queue
) &&
258 likely(netif_running(dev
)))
259 netif_tx_wake_queue(netdev_get_tx_queue(dev
, queue
->id
));
263 static struct sk_buff
*xennet_alloc_one_rx_buffer(struct netfront_queue
*queue
)
268 skb
= __netdev_alloc_skb(queue
->info
->netdev
,
269 RX_COPY_THRESHOLD
+ NET_IP_ALIGN
,
270 GFP_ATOMIC
| __GFP_NOWARN
);
274 page
= alloc_page(GFP_ATOMIC
| __GFP_NOWARN
);
279 skb_add_rx_frag(skb
, 0, page
, 0, 0, PAGE_SIZE
);
281 /* Align ip header to a 16 bytes boundary */
282 skb_reserve(skb
, NET_IP_ALIGN
);
283 skb
->dev
= queue
->info
->netdev
;
289 static void xennet_alloc_rx_buffers(struct netfront_queue
*queue
)
291 RING_IDX req_prod
= queue
->rx
.req_prod_pvt
;
294 if (unlikely(!netif_carrier_ok(queue
->info
->netdev
)))
297 for (req_prod
= queue
->rx
.req_prod_pvt
;
298 req_prod
- queue
->rx
.rsp_cons
< NET_RX_RING_SIZE
;
304 struct xen_netif_rx_request
*req
;
306 skb
= xennet_alloc_one_rx_buffer(queue
);
310 id
= xennet_rxidx(req_prod
);
312 BUG_ON(queue
->rx_skbs
[id
]);
313 queue
->rx_skbs
[id
] = skb
;
315 ref
= gnttab_claim_grant_reference(&queue
->gref_rx_head
);
316 BUG_ON((signed short)ref
< 0);
317 queue
->grant_rx_ref
[id
] = ref
;
319 pfn
= page_to_pfn(skb_frag_page(&skb_shinfo(skb
)->frags
[0]));
321 req
= RING_GET_REQUEST(&queue
->rx
, req_prod
);
322 gnttab_grant_foreign_access_ref(ref
,
323 queue
->info
->xbdev
->otherend_id
,
331 queue
->rx
.req_prod_pvt
= req_prod
;
333 /* Not enough requests? Try again later. */
334 if (req_prod
- queue
->rx
.rsp_cons
< NET_RX_SLOTS_MIN
) {
335 mod_timer(&queue
->rx_refill_timer
, jiffies
+ (HZ
/10));
339 wmb(); /* barrier so backend seens requests */
341 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue
->rx
, notify
);
343 notify_remote_via_irq(queue
->rx_irq
);
346 static int xennet_open(struct net_device
*dev
)
348 struct netfront_info
*np
= netdev_priv(dev
);
349 unsigned int num_queues
= dev
->real_num_tx_queues
;
351 struct netfront_queue
*queue
= NULL
;
353 for (i
= 0; i
< num_queues
; ++i
) {
354 queue
= &np
->queues
[i
];
355 napi_enable(&queue
->napi
);
357 spin_lock_bh(&queue
->rx_lock
);
358 if (netif_carrier_ok(dev
)) {
359 xennet_alloc_rx_buffers(queue
);
360 queue
->rx
.sring
->rsp_event
= queue
->rx
.rsp_cons
+ 1;
361 if (RING_HAS_UNCONSUMED_RESPONSES(&queue
->rx
))
362 napi_schedule(&queue
->napi
);
364 spin_unlock_bh(&queue
->rx_lock
);
367 netif_tx_start_all_queues(dev
);
372 static void xennet_tx_buf_gc(struct netfront_queue
*queue
)
378 BUG_ON(!netif_carrier_ok(queue
->info
->netdev
));
381 prod
= queue
->tx
.sring
->rsp_prod
;
382 rmb(); /* Ensure we see responses up to 'rp'. */
384 for (cons
= queue
->tx
.rsp_cons
; cons
!= prod
; cons
++) {
385 struct xen_netif_tx_response
*txrsp
;
387 txrsp
= RING_GET_RESPONSE(&queue
->tx
, cons
);
388 if (txrsp
->status
== XEN_NETIF_RSP_NULL
)
392 skb
= queue
->tx_skbs
[id
].skb
;
393 if (unlikely(gnttab_query_foreign_access(
394 queue
->grant_tx_ref
[id
]) != 0)) {
395 pr_alert("%s: warning -- grant still in use by backend domain\n",
399 gnttab_end_foreign_access_ref(
400 queue
->grant_tx_ref
[id
], GNTMAP_readonly
);
401 gnttab_release_grant_reference(
402 &queue
->gref_tx_head
, queue
->grant_tx_ref
[id
]);
403 queue
->grant_tx_ref
[id
] = GRANT_INVALID_REF
;
404 queue
->grant_tx_page
[id
] = NULL
;
405 add_id_to_freelist(&queue
->tx_skb_freelist
, queue
->tx_skbs
, id
);
406 dev_kfree_skb_irq(skb
);
409 queue
->tx
.rsp_cons
= prod
;
412 * Set a new event, then check for race with update of tx_cons.
413 * Note that it is essential to schedule a callback, no matter
414 * how few buffers are pending. Even if there is space in the
415 * transmit ring, higher layers may be blocked because too much
416 * data is outstanding: in such cases notification from Xen is
417 * likely to be the only kick that we'll get.
419 queue
->tx
.sring
->rsp_event
=
420 prod
+ ((queue
->tx
.sring
->req_prod
- prod
) >> 1) + 1;
421 mb(); /* update shared area */
422 } while ((cons
== prod
) && (prod
!= queue
->tx
.sring
->rsp_prod
));
424 xennet_maybe_wake_tx(queue
);
427 static void xennet_make_frags(struct sk_buff
*skb
, struct netfront_queue
*queue
,
428 struct xen_netif_tx_request
*tx
)
430 char *data
= skb
->data
;
432 RING_IDX prod
= queue
->tx
.req_prod_pvt
;
433 int frags
= skb_shinfo(skb
)->nr_frags
;
434 unsigned int offset
= offset_in_page(data
);
435 unsigned int len
= skb_headlen(skb
);
440 /* While the header overlaps a page boundary (including being
441 larger than a page), split it it into page-sized chunks. */
442 while (len
> PAGE_SIZE
- offset
) {
443 tx
->size
= PAGE_SIZE
- offset
;
444 tx
->flags
|= XEN_NETTXF_more_data
;
449 id
= get_id_from_freelist(&queue
->tx_skb_freelist
, queue
->tx_skbs
);
450 queue
->tx_skbs
[id
].skb
= skb_get(skb
);
451 tx
= RING_GET_REQUEST(&queue
->tx
, prod
++);
453 ref
= gnttab_claim_grant_reference(&queue
->gref_tx_head
);
454 BUG_ON((signed short)ref
< 0);
456 mfn
= virt_to_mfn(data
);
457 gnttab_grant_foreign_access_ref(ref
, queue
->info
->xbdev
->otherend_id
,
458 mfn
, GNTMAP_readonly
);
460 queue
->grant_tx_page
[id
] = virt_to_page(data
);
461 tx
->gref
= queue
->grant_tx_ref
[id
] = ref
;
467 /* Grant backend access to each skb fragment page. */
468 for (i
= 0; i
< frags
; i
++) {
469 skb_frag_t
*frag
= skb_shinfo(skb
)->frags
+ i
;
470 struct page
*page
= skb_frag_page(frag
);
472 len
= skb_frag_size(frag
);
473 offset
= frag
->page_offset
;
475 /* Skip unused frames from start of page */
476 page
+= offset
>> PAGE_SHIFT
;
477 offset
&= ~PAGE_MASK
;
482 bytes
= PAGE_SIZE
- offset
;
486 tx
->flags
|= XEN_NETTXF_more_data
;
488 id
= get_id_from_freelist(&queue
->tx_skb_freelist
,
490 queue
->tx_skbs
[id
].skb
= skb_get(skb
);
491 tx
= RING_GET_REQUEST(&queue
->tx
, prod
++);
493 ref
= gnttab_claim_grant_reference(&queue
->gref_tx_head
);
494 BUG_ON((signed short)ref
< 0);
496 mfn
= pfn_to_mfn(page_to_pfn(page
));
497 gnttab_grant_foreign_access_ref(ref
,
498 queue
->info
->xbdev
->otherend_id
,
499 mfn
, GNTMAP_readonly
);
501 queue
->grant_tx_page
[id
] = page
;
502 tx
->gref
= queue
->grant_tx_ref
[id
] = ref
;
511 if (offset
== PAGE_SIZE
&& len
) {
512 BUG_ON(!PageCompound(page
));
519 queue
->tx
.req_prod_pvt
= prod
;
523 * Count how many ring slots are required to send the frags of this
524 * skb. Each frag might be a compound page.
526 static int xennet_count_skb_frag_slots(struct sk_buff
*skb
)
528 int i
, frags
= skb_shinfo(skb
)->nr_frags
;
531 for (i
= 0; i
< frags
; i
++) {
532 skb_frag_t
*frag
= skb_shinfo(skb
)->frags
+ i
;
533 unsigned long size
= skb_frag_size(frag
);
534 unsigned long offset
= frag
->page_offset
;
536 /* Skip unused frames from start of page */
537 offset
&= ~PAGE_MASK
;
539 pages
+= PFN_UP(offset
+ size
);
545 static u16
xennet_select_queue(struct net_device
*dev
, struct sk_buff
*skb
,
546 void *accel_priv
, select_queue_fallback_t fallback
)
548 unsigned int num_queues
= dev
->real_num_tx_queues
;
552 /* First, check if there is only one queue */
553 if (num_queues
== 1) {
556 hash
= skb_get_hash(skb
);
557 queue_idx
= hash
% num_queues
;
563 static int xennet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
566 struct netfront_info
*np
= netdev_priv(dev
);
567 struct netfront_stats
*tx_stats
= this_cpu_ptr(np
->tx_stats
);
568 struct xen_netif_tx_request
*tx
;
569 char *data
= skb
->data
;
575 unsigned int offset
= offset_in_page(data
);
576 unsigned int len
= skb_headlen(skb
);
578 struct netfront_queue
*queue
= NULL
;
579 unsigned int num_queues
= dev
->real_num_tx_queues
;
582 /* Drop the packet if no queues are set up */
585 /* Determine which queue to transmit this SKB on */
586 queue_index
= skb_get_queue_mapping(skb
);
587 queue
= &np
->queues
[queue_index
];
589 /* If skb->len is too big for wire format, drop skb and alert
590 * user about misconfiguration.
592 if (unlikely(skb
->len
> XEN_NETIF_MAX_TX_SIZE
)) {
593 net_alert_ratelimited(
594 "xennet: skb->len = %u, too big for wire format\n",
599 slots
= DIV_ROUND_UP(offset
+ len
, PAGE_SIZE
) +
600 xennet_count_skb_frag_slots(skb
);
601 if (unlikely(slots
> MAX_SKB_FRAGS
+ 1)) {
602 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
604 if (skb_linearize(skb
))
607 offset
= offset_in_page(data
);
608 len
= skb_headlen(skb
);
611 spin_lock_irqsave(&queue
->tx_lock
, flags
);
613 if (unlikely(!netif_carrier_ok(dev
) ||
614 (slots
> 1 && !xennet_can_sg(dev
)) ||
615 netif_needs_gso(dev
, skb
, netif_skb_features(skb
)))) {
616 spin_unlock_irqrestore(&queue
->tx_lock
, flags
);
620 i
= queue
->tx
.req_prod_pvt
;
622 id
= get_id_from_freelist(&queue
->tx_skb_freelist
, queue
->tx_skbs
);
623 queue
->tx_skbs
[id
].skb
= skb
;
625 tx
= RING_GET_REQUEST(&queue
->tx
, i
);
628 ref
= gnttab_claim_grant_reference(&queue
->gref_tx_head
);
629 BUG_ON((signed short)ref
< 0);
630 mfn
= virt_to_mfn(data
);
631 gnttab_grant_foreign_access_ref(
632 ref
, queue
->info
->xbdev
->otherend_id
, mfn
, GNTMAP_readonly
);
633 queue
->grant_tx_page
[id
] = virt_to_page(data
);
634 tx
->gref
= queue
->grant_tx_ref
[id
] = ref
;
639 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
641 tx
->flags
|= XEN_NETTXF_csum_blank
| XEN_NETTXF_data_validated
;
642 else if (skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
643 /* remote but checksummed. */
644 tx
->flags
|= XEN_NETTXF_data_validated
;
646 if (skb_shinfo(skb
)->gso_size
) {
647 struct xen_netif_extra_info
*gso
;
649 gso
= (struct xen_netif_extra_info
*)
650 RING_GET_REQUEST(&queue
->tx
, ++i
);
652 tx
->flags
|= XEN_NETTXF_extra_info
;
654 gso
->u
.gso
.size
= skb_shinfo(skb
)->gso_size
;
655 gso
->u
.gso
.type
= (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV6
) ?
656 XEN_NETIF_GSO_TYPE_TCPV6
:
657 XEN_NETIF_GSO_TYPE_TCPV4
;
659 gso
->u
.gso
.features
= 0;
661 gso
->type
= XEN_NETIF_EXTRA_TYPE_GSO
;
665 queue
->tx
.req_prod_pvt
= i
+ 1;
667 xennet_make_frags(skb
, queue
, tx
);
670 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue
->tx
, notify
);
672 notify_remote_via_irq(queue
->tx_irq
);
674 u64_stats_update_begin(&tx_stats
->syncp
);
675 tx_stats
->bytes
+= skb
->len
;
677 u64_stats_update_end(&tx_stats
->syncp
);
679 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
680 xennet_tx_buf_gc(queue
);
682 if (!netfront_tx_slot_available(queue
))
683 netif_tx_stop_queue(netdev_get_tx_queue(dev
, queue
->id
));
685 spin_unlock_irqrestore(&queue
->tx_lock
, flags
);
690 dev
->stats
.tx_dropped
++;
691 dev_kfree_skb_any(skb
);
695 static int xennet_close(struct net_device
*dev
)
697 struct netfront_info
*np
= netdev_priv(dev
);
698 unsigned int num_queues
= dev
->real_num_tx_queues
;
700 struct netfront_queue
*queue
;
701 netif_tx_stop_all_queues(np
->netdev
);
702 for (i
= 0; i
< num_queues
; ++i
) {
703 queue
= &np
->queues
[i
];
704 napi_disable(&queue
->napi
);
709 static void xennet_move_rx_slot(struct netfront_queue
*queue
, struct sk_buff
*skb
,
712 int new = xennet_rxidx(queue
->rx
.req_prod_pvt
);
714 BUG_ON(queue
->rx_skbs
[new]);
715 queue
->rx_skbs
[new] = skb
;
716 queue
->grant_rx_ref
[new] = ref
;
717 RING_GET_REQUEST(&queue
->rx
, queue
->rx
.req_prod_pvt
)->id
= new;
718 RING_GET_REQUEST(&queue
->rx
, queue
->rx
.req_prod_pvt
)->gref
= ref
;
719 queue
->rx
.req_prod_pvt
++;
722 static int xennet_get_extras(struct netfront_queue
*queue
,
723 struct xen_netif_extra_info
*extras
,
727 struct xen_netif_extra_info
*extra
;
728 struct device
*dev
= &queue
->info
->netdev
->dev
;
729 RING_IDX cons
= queue
->rx
.rsp_cons
;
736 if (unlikely(cons
+ 1 == rp
)) {
738 dev_warn(dev
, "Missing extra info\n");
743 extra
= (struct xen_netif_extra_info
*)
744 RING_GET_RESPONSE(&queue
->rx
, ++cons
);
746 if (unlikely(!extra
->type
||
747 extra
->type
>= XEN_NETIF_EXTRA_TYPE_MAX
)) {
749 dev_warn(dev
, "Invalid extra type: %d\n",
753 memcpy(&extras
[extra
->type
- 1], extra
,
757 skb
= xennet_get_rx_skb(queue
, cons
);
758 ref
= xennet_get_rx_ref(queue
, cons
);
759 xennet_move_rx_slot(queue
, skb
, ref
);
760 } while (extra
->flags
& XEN_NETIF_EXTRA_FLAG_MORE
);
762 queue
->rx
.rsp_cons
= cons
;
766 static int xennet_get_responses(struct netfront_queue
*queue
,
767 struct netfront_rx_info
*rinfo
, RING_IDX rp
,
768 struct sk_buff_head
*list
)
770 struct xen_netif_rx_response
*rx
= &rinfo
->rx
;
771 struct xen_netif_extra_info
*extras
= rinfo
->extras
;
772 struct device
*dev
= &queue
->info
->netdev
->dev
;
773 RING_IDX cons
= queue
->rx
.rsp_cons
;
774 struct sk_buff
*skb
= xennet_get_rx_skb(queue
, cons
);
775 grant_ref_t ref
= xennet_get_rx_ref(queue
, cons
);
776 int max
= MAX_SKB_FRAGS
+ (rx
->status
<= RX_COPY_THRESHOLD
);
781 if (rx
->flags
& XEN_NETRXF_extra_info
) {
782 err
= xennet_get_extras(queue
, extras
, rp
);
783 cons
= queue
->rx
.rsp_cons
;
787 if (unlikely(rx
->status
< 0 ||
788 rx
->offset
+ rx
->status
> PAGE_SIZE
)) {
790 dev_warn(dev
, "rx->offset: %x, size: %u\n",
791 rx
->offset
, rx
->status
);
792 xennet_move_rx_slot(queue
, skb
, ref
);
798 * This definitely indicates a bug, either in this driver or in
799 * the backend driver. In future this should flag the bad
800 * situation to the system controller to reboot the backend.
802 if (ref
== GRANT_INVALID_REF
) {
804 dev_warn(dev
, "Bad rx response id %d.\n",
810 ret
= gnttab_end_foreign_access_ref(ref
, 0);
813 gnttab_release_grant_reference(&queue
->gref_rx_head
, ref
);
815 __skb_queue_tail(list
, skb
);
818 if (!(rx
->flags
& XEN_NETRXF_more_data
))
821 if (cons
+ slots
== rp
) {
823 dev_warn(dev
, "Need more slots\n");
828 rx
= RING_GET_RESPONSE(&queue
->rx
, cons
+ slots
);
829 skb
= xennet_get_rx_skb(queue
, cons
+ slots
);
830 ref
= xennet_get_rx_ref(queue
, cons
+ slots
);
834 if (unlikely(slots
> max
)) {
836 dev_warn(dev
, "Too many slots\n");
841 queue
->rx
.rsp_cons
= cons
+ slots
;
846 static int xennet_set_skb_gso(struct sk_buff
*skb
,
847 struct xen_netif_extra_info
*gso
)
849 if (!gso
->u
.gso
.size
) {
851 pr_warn("GSO size must not be zero\n");
855 if (gso
->u
.gso
.type
!= XEN_NETIF_GSO_TYPE_TCPV4
&&
856 gso
->u
.gso
.type
!= XEN_NETIF_GSO_TYPE_TCPV6
) {
858 pr_warn("Bad GSO type %d\n", gso
->u
.gso
.type
);
862 skb_shinfo(skb
)->gso_size
= gso
->u
.gso
.size
;
863 skb_shinfo(skb
)->gso_type
=
864 (gso
->u
.gso
.type
== XEN_NETIF_GSO_TYPE_TCPV4
) ?
868 /* Header must be checked, and gso_segs computed. */
869 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
870 skb_shinfo(skb
)->gso_segs
= 0;
875 static RING_IDX
xennet_fill_frags(struct netfront_queue
*queue
,
877 struct sk_buff_head
*list
)
879 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
880 RING_IDX cons
= queue
->rx
.rsp_cons
;
881 struct sk_buff
*nskb
;
883 while ((nskb
= __skb_dequeue(list
))) {
884 struct xen_netif_rx_response
*rx
=
885 RING_GET_RESPONSE(&queue
->rx
, ++cons
);
886 skb_frag_t
*nfrag
= &skb_shinfo(nskb
)->frags
[0];
888 if (shinfo
->nr_frags
== MAX_SKB_FRAGS
) {
889 unsigned int pull_to
= NETFRONT_SKB_CB(skb
)->pull_to
;
891 BUG_ON(pull_to
<= skb_headlen(skb
));
892 __pskb_pull_tail(skb
, pull_to
- skb_headlen(skb
));
894 BUG_ON(shinfo
->nr_frags
>= MAX_SKB_FRAGS
);
896 skb_add_rx_frag(skb
, shinfo
->nr_frags
, skb_frag_page(nfrag
),
897 rx
->offset
, rx
->status
, PAGE_SIZE
);
899 skb_shinfo(nskb
)->nr_frags
= 0;
906 static int checksum_setup(struct net_device
*dev
, struct sk_buff
*skb
)
908 bool recalculate_partial_csum
= false;
911 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
912 * peers can fail to set NETRXF_csum_blank when sending a GSO
913 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
914 * recalculate the partial checksum.
916 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
&& skb_is_gso(skb
)) {
917 struct netfront_info
*np
= netdev_priv(dev
);
918 atomic_inc(&np
->rx_gso_checksum_fixup
);
919 skb
->ip_summed
= CHECKSUM_PARTIAL
;
920 recalculate_partial_csum
= true;
923 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
924 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
927 return skb_checksum_setup(skb
, recalculate_partial_csum
);
930 static int handle_incoming_queue(struct netfront_queue
*queue
,
931 struct sk_buff_head
*rxq
)
933 struct netfront_stats
*rx_stats
= this_cpu_ptr(queue
->info
->rx_stats
);
934 int packets_dropped
= 0;
937 while ((skb
= __skb_dequeue(rxq
)) != NULL
) {
938 int pull_to
= NETFRONT_SKB_CB(skb
)->pull_to
;
940 if (pull_to
> skb_headlen(skb
))
941 __pskb_pull_tail(skb
, pull_to
- skb_headlen(skb
));
943 /* Ethernet work: Delayed to here as it peeks the header. */
944 skb
->protocol
= eth_type_trans(skb
, queue
->info
->netdev
);
945 skb_reset_network_header(skb
);
947 if (checksum_setup(queue
->info
->netdev
, skb
)) {
950 queue
->info
->netdev
->stats
.rx_errors
++;
954 u64_stats_update_begin(&rx_stats
->syncp
);
956 rx_stats
->bytes
+= skb
->len
;
957 u64_stats_update_end(&rx_stats
->syncp
);
960 napi_gro_receive(&queue
->napi
, skb
);
963 return packets_dropped
;
966 static int xennet_poll(struct napi_struct
*napi
, int budget
)
968 struct netfront_queue
*queue
= container_of(napi
, struct netfront_queue
, napi
);
969 struct net_device
*dev
= queue
->info
->netdev
;
971 struct netfront_rx_info rinfo
;
972 struct xen_netif_rx_response
*rx
= &rinfo
.rx
;
973 struct xen_netif_extra_info
*extras
= rinfo
.extras
;
976 struct sk_buff_head rxq
;
977 struct sk_buff_head errq
;
978 struct sk_buff_head tmpq
;
981 spin_lock(&queue
->rx_lock
);
983 skb_queue_head_init(&rxq
);
984 skb_queue_head_init(&errq
);
985 skb_queue_head_init(&tmpq
);
987 rp
= queue
->rx
.sring
->rsp_prod
;
988 rmb(); /* Ensure we see queued responses up to 'rp'. */
990 i
= queue
->rx
.rsp_cons
;
992 while ((i
!= rp
) && (work_done
< budget
)) {
993 memcpy(rx
, RING_GET_RESPONSE(&queue
->rx
, i
), sizeof(*rx
));
994 memset(extras
, 0, sizeof(rinfo
.extras
));
996 err
= xennet_get_responses(queue
, &rinfo
, rp
, &tmpq
);
1000 while ((skb
= __skb_dequeue(&tmpq
)))
1001 __skb_queue_tail(&errq
, skb
);
1002 dev
->stats
.rx_errors
++;
1003 i
= queue
->rx
.rsp_cons
;
1007 skb
= __skb_dequeue(&tmpq
);
1009 if (extras
[XEN_NETIF_EXTRA_TYPE_GSO
- 1].type
) {
1010 struct xen_netif_extra_info
*gso
;
1011 gso
= &extras
[XEN_NETIF_EXTRA_TYPE_GSO
- 1];
1013 if (unlikely(xennet_set_skb_gso(skb
, gso
))) {
1014 __skb_queue_head(&tmpq
, skb
);
1015 queue
->rx
.rsp_cons
+= skb_queue_len(&tmpq
);
1020 NETFRONT_SKB_CB(skb
)->pull_to
= rx
->status
;
1021 if (NETFRONT_SKB_CB(skb
)->pull_to
> RX_COPY_THRESHOLD
)
1022 NETFRONT_SKB_CB(skb
)->pull_to
= RX_COPY_THRESHOLD
;
1024 skb_shinfo(skb
)->frags
[0].page_offset
= rx
->offset
;
1025 skb_frag_size_set(&skb_shinfo(skb
)->frags
[0], rx
->status
);
1026 skb
->data_len
= rx
->status
;
1027 skb
->len
+= rx
->status
;
1029 i
= xennet_fill_frags(queue
, skb
, &tmpq
);
1031 if (rx
->flags
& XEN_NETRXF_csum_blank
)
1032 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1033 else if (rx
->flags
& XEN_NETRXF_data_validated
)
1034 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1036 __skb_queue_tail(&rxq
, skb
);
1038 queue
->rx
.rsp_cons
= ++i
;
1042 __skb_queue_purge(&errq
);
1044 work_done
-= handle_incoming_queue(queue
, &rxq
);
1046 xennet_alloc_rx_buffers(queue
);
1048 if (work_done
< budget
) {
1051 napi_complete(napi
);
1053 RING_FINAL_CHECK_FOR_RESPONSES(&queue
->rx
, more_to_do
);
1055 napi_schedule(napi
);
1058 spin_unlock(&queue
->rx_lock
);
1063 static int xennet_change_mtu(struct net_device
*dev
, int mtu
)
1065 int max
= xennet_can_sg(dev
) ?
1066 XEN_NETIF_MAX_TX_SIZE
- MAX_TCP_HEADER
: ETH_DATA_LEN
;
1074 static struct rtnl_link_stats64
*xennet_get_stats64(struct net_device
*dev
,
1075 struct rtnl_link_stats64
*tot
)
1077 struct netfront_info
*np
= netdev_priv(dev
);
1080 for_each_possible_cpu(cpu
) {
1081 struct netfront_stats
*rx_stats
= per_cpu_ptr(np
->rx_stats
, cpu
);
1082 struct netfront_stats
*tx_stats
= per_cpu_ptr(np
->tx_stats
, cpu
);
1083 u64 rx_packets
, rx_bytes
, tx_packets
, tx_bytes
;
1087 start
= u64_stats_fetch_begin_irq(&tx_stats
->syncp
);
1088 tx_packets
= tx_stats
->packets
;
1089 tx_bytes
= tx_stats
->bytes
;
1090 } while (u64_stats_fetch_retry_irq(&tx_stats
->syncp
, start
));
1093 start
= u64_stats_fetch_begin_irq(&rx_stats
->syncp
);
1094 rx_packets
= rx_stats
->packets
;
1095 rx_bytes
= rx_stats
->bytes
;
1096 } while (u64_stats_fetch_retry_irq(&rx_stats
->syncp
, start
));
1098 tot
->rx_packets
+= rx_packets
;
1099 tot
->tx_packets
+= tx_packets
;
1100 tot
->rx_bytes
+= rx_bytes
;
1101 tot
->tx_bytes
+= tx_bytes
;
1104 tot
->rx_errors
= dev
->stats
.rx_errors
;
1105 tot
->tx_dropped
= dev
->stats
.tx_dropped
;
1110 static void xennet_release_tx_bufs(struct netfront_queue
*queue
)
1112 struct sk_buff
*skb
;
1115 for (i
= 0; i
< NET_TX_RING_SIZE
; i
++) {
1116 /* Skip over entries which are actually freelist references */
1117 if (skb_entry_is_link(&queue
->tx_skbs
[i
]))
1120 skb
= queue
->tx_skbs
[i
].skb
;
1121 get_page(queue
->grant_tx_page
[i
]);
1122 gnttab_end_foreign_access(queue
->grant_tx_ref
[i
],
1124 (unsigned long)page_address(queue
->grant_tx_page
[i
]));
1125 queue
->grant_tx_page
[i
] = NULL
;
1126 queue
->grant_tx_ref
[i
] = GRANT_INVALID_REF
;
1127 add_id_to_freelist(&queue
->tx_skb_freelist
, queue
->tx_skbs
, i
);
1128 dev_kfree_skb_irq(skb
);
1132 static void xennet_release_rx_bufs(struct netfront_queue
*queue
)
1136 spin_lock_bh(&queue
->rx_lock
);
1138 for (id
= 0; id
< NET_RX_RING_SIZE
; id
++) {
1139 struct sk_buff
*skb
;
1142 skb
= queue
->rx_skbs
[id
];
1146 ref
= queue
->grant_rx_ref
[id
];
1147 if (ref
== GRANT_INVALID_REF
)
1150 page
= skb_frag_page(&skb_shinfo(skb
)->frags
[0]);
1152 /* gnttab_end_foreign_access() needs a page ref until
1153 * foreign access is ended (which may be deferred).
1156 gnttab_end_foreign_access(ref
, 0,
1157 (unsigned long)page_address(page
));
1158 queue
->grant_rx_ref
[id
] = GRANT_INVALID_REF
;
1163 spin_unlock_bh(&queue
->rx_lock
);
1166 static netdev_features_t
xennet_fix_features(struct net_device
*dev
,
1167 netdev_features_t features
)
1169 struct netfront_info
*np
= netdev_priv(dev
);
1172 if (features
& NETIF_F_SG
) {
1173 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
, "feature-sg",
1178 features
&= ~NETIF_F_SG
;
1181 if (features
& NETIF_F_IPV6_CSUM
) {
1182 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1183 "feature-ipv6-csum-offload", "%d", &val
) < 0)
1187 features
&= ~NETIF_F_IPV6_CSUM
;
1190 if (features
& NETIF_F_TSO
) {
1191 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1192 "feature-gso-tcpv4", "%d", &val
) < 0)
1196 features
&= ~NETIF_F_TSO
;
1199 if (features
& NETIF_F_TSO6
) {
1200 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1201 "feature-gso-tcpv6", "%d", &val
) < 0)
1205 features
&= ~NETIF_F_TSO6
;
1211 static int xennet_set_features(struct net_device
*dev
,
1212 netdev_features_t features
)
1214 if (!(features
& NETIF_F_SG
) && dev
->mtu
> ETH_DATA_LEN
) {
1215 netdev_info(dev
, "Reducing MTU because no SG offload");
1216 dev
->mtu
= ETH_DATA_LEN
;
1222 static irqreturn_t
xennet_tx_interrupt(int irq
, void *dev_id
)
1224 struct netfront_queue
*queue
= dev_id
;
1225 unsigned long flags
;
1227 spin_lock_irqsave(&queue
->tx_lock
, flags
);
1228 xennet_tx_buf_gc(queue
);
1229 spin_unlock_irqrestore(&queue
->tx_lock
, flags
);
1234 static irqreturn_t
xennet_rx_interrupt(int irq
, void *dev_id
)
1236 struct netfront_queue
*queue
= dev_id
;
1237 struct net_device
*dev
= queue
->info
->netdev
;
1239 if (likely(netif_carrier_ok(dev
) &&
1240 RING_HAS_UNCONSUMED_RESPONSES(&queue
->rx
)))
1241 napi_schedule(&queue
->napi
);
1246 static irqreturn_t
xennet_interrupt(int irq
, void *dev_id
)
1248 xennet_tx_interrupt(irq
, dev_id
);
1249 xennet_rx_interrupt(irq
, dev_id
);
1253 #ifdef CONFIG_NET_POLL_CONTROLLER
1254 static void xennet_poll_controller(struct net_device
*dev
)
1256 /* Poll each queue */
1257 struct netfront_info
*info
= netdev_priv(dev
);
1258 unsigned int num_queues
= dev
->real_num_tx_queues
;
1260 for (i
= 0; i
< num_queues
; ++i
)
1261 xennet_interrupt(0, &info
->queues
[i
]);
1265 static const struct net_device_ops xennet_netdev_ops
= {
1266 .ndo_open
= xennet_open
,
1267 .ndo_stop
= xennet_close
,
1268 .ndo_start_xmit
= xennet_start_xmit
,
1269 .ndo_change_mtu
= xennet_change_mtu
,
1270 .ndo_get_stats64
= xennet_get_stats64
,
1271 .ndo_set_mac_address
= eth_mac_addr
,
1272 .ndo_validate_addr
= eth_validate_addr
,
1273 .ndo_fix_features
= xennet_fix_features
,
1274 .ndo_set_features
= xennet_set_features
,
1275 .ndo_select_queue
= xennet_select_queue
,
1276 #ifdef CONFIG_NET_POLL_CONTROLLER
1277 .ndo_poll_controller
= xennet_poll_controller
,
1281 static void xennet_free_netdev(struct net_device
*netdev
)
1283 struct netfront_info
*np
= netdev_priv(netdev
);
1285 free_percpu(np
->rx_stats
);
1286 free_percpu(np
->tx_stats
);
1287 free_netdev(netdev
);
1290 static struct net_device
*xennet_create_dev(struct xenbus_device
*dev
)
1293 struct net_device
*netdev
;
1294 struct netfront_info
*np
;
1296 netdev
= alloc_etherdev_mq(sizeof(struct netfront_info
), xennet_max_queues
);
1298 return ERR_PTR(-ENOMEM
);
1300 np
= netdev_priv(netdev
);
1303 /* No need to use rtnl_lock() before the call below as it
1304 * happens before register_netdev().
1306 netif_set_real_num_tx_queues(netdev
, 0);
1310 np
->rx_stats
= netdev_alloc_pcpu_stats(struct netfront_stats
);
1311 if (np
->rx_stats
== NULL
)
1313 np
->tx_stats
= netdev_alloc_pcpu_stats(struct netfront_stats
);
1314 if (np
->tx_stats
== NULL
)
1317 netdev
->netdev_ops
= &xennet_netdev_ops
;
1319 netdev
->features
= NETIF_F_IP_CSUM
| NETIF_F_RXCSUM
|
1321 netdev
->hw_features
= NETIF_F_SG
|
1323 NETIF_F_TSO
| NETIF_F_TSO6
;
1326 * Assume that all hw features are available for now. This set
1327 * will be adjusted by the call to netdev_update_features() in
1328 * xennet_connect() which is the earliest point where we can
1329 * negotiate with the backend regarding supported features.
1331 netdev
->features
|= netdev
->hw_features
;
1333 netdev
->ethtool_ops
= &xennet_ethtool_ops
;
1334 SET_NETDEV_DEV(netdev
, &dev
->dev
);
1336 netif_set_gso_max_size(netdev
, XEN_NETIF_MAX_TX_SIZE
- MAX_TCP_HEADER
);
1338 np
->netdev
= netdev
;
1340 netif_carrier_off(netdev
);
1345 xennet_free_netdev(netdev
);
1346 return ERR_PTR(err
);
1350 * Entry point to this code when a new device is created. Allocate the basic
1351 * structures and the ring buffers for communication with the backend, and
1352 * inform the backend of the appropriate details for those.
1354 static int netfront_probe(struct xenbus_device
*dev
,
1355 const struct xenbus_device_id
*id
)
1358 struct net_device
*netdev
;
1359 struct netfront_info
*info
;
1361 netdev
= xennet_create_dev(dev
);
1362 if (IS_ERR(netdev
)) {
1363 err
= PTR_ERR(netdev
);
1364 xenbus_dev_fatal(dev
, err
, "creating netdev");
1368 info
= netdev_priv(netdev
);
1369 dev_set_drvdata(&dev
->dev
, info
);
1371 err
= register_netdev(info
->netdev
);
1373 pr_warn("%s: register_netdev err=%d\n", __func__
, err
);
1377 err
= xennet_sysfs_addif(info
->netdev
);
1379 unregister_netdev(info
->netdev
);
1380 pr_warn("%s: add sysfs failed err=%d\n", __func__
, err
);
1387 xennet_free_netdev(netdev
);
1388 dev_set_drvdata(&dev
->dev
, NULL
);
1392 static void xennet_end_access(int ref
, void *page
)
1394 /* This frees the page as a side-effect */
1395 if (ref
!= GRANT_INVALID_REF
)
1396 gnttab_end_foreign_access(ref
, 0, (unsigned long)page
);
1399 static void xennet_disconnect_backend(struct netfront_info
*info
)
1402 unsigned int num_queues
= info
->netdev
->real_num_tx_queues
;
1404 netif_carrier_off(info
->netdev
);
1406 for (i
= 0; i
< num_queues
; ++i
) {
1407 struct netfront_queue
*queue
= &info
->queues
[i
];
1409 if (queue
->tx_irq
&& (queue
->tx_irq
== queue
->rx_irq
))
1410 unbind_from_irqhandler(queue
->tx_irq
, queue
);
1411 if (queue
->tx_irq
&& (queue
->tx_irq
!= queue
->rx_irq
)) {
1412 unbind_from_irqhandler(queue
->tx_irq
, queue
);
1413 unbind_from_irqhandler(queue
->rx_irq
, queue
);
1415 queue
->tx_evtchn
= queue
->rx_evtchn
= 0;
1416 queue
->tx_irq
= queue
->rx_irq
= 0;
1418 napi_synchronize(&queue
->napi
);
1420 xennet_release_tx_bufs(queue
);
1421 xennet_release_rx_bufs(queue
);
1422 gnttab_free_grant_references(queue
->gref_tx_head
);
1423 gnttab_free_grant_references(queue
->gref_rx_head
);
1425 /* End access and free the pages */
1426 xennet_end_access(queue
->tx_ring_ref
, queue
->tx
.sring
);
1427 xennet_end_access(queue
->rx_ring_ref
, queue
->rx
.sring
);
1429 queue
->tx_ring_ref
= GRANT_INVALID_REF
;
1430 queue
->rx_ring_ref
= GRANT_INVALID_REF
;
1431 queue
->tx
.sring
= NULL
;
1432 queue
->rx
.sring
= NULL
;
1437 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1438 * driver restart. We tear down our netif structure and recreate it, but
1439 * leave the device-layer structures intact so that this is transparent to the
1440 * rest of the kernel.
1442 static int netfront_resume(struct xenbus_device
*dev
)
1444 struct netfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1446 dev_dbg(&dev
->dev
, "%s\n", dev
->nodename
);
1448 xennet_disconnect_backend(info
);
1452 static int xen_net_read_mac(struct xenbus_device
*dev
, u8 mac
[])
1454 char *s
, *e
, *macstr
;
1457 macstr
= s
= xenbus_read(XBT_NIL
, dev
->nodename
, "mac", NULL
);
1459 return PTR_ERR(macstr
);
1461 for (i
= 0; i
< ETH_ALEN
; i
++) {
1462 mac
[i
] = simple_strtoul(s
, &e
, 16);
1463 if ((s
== e
) || (*e
!= ((i
== ETH_ALEN
-1) ? '\0' : ':'))) {
1474 static int setup_netfront_single(struct netfront_queue
*queue
)
1478 err
= xenbus_alloc_evtchn(queue
->info
->xbdev
, &queue
->tx_evtchn
);
1482 err
= bind_evtchn_to_irqhandler(queue
->tx_evtchn
,
1484 0, queue
->info
->netdev
->name
, queue
);
1487 queue
->rx_evtchn
= queue
->tx_evtchn
;
1488 queue
->rx_irq
= queue
->tx_irq
= err
;
1493 xenbus_free_evtchn(queue
->info
->xbdev
, queue
->tx_evtchn
);
1494 queue
->tx_evtchn
= 0;
1499 static int setup_netfront_split(struct netfront_queue
*queue
)
1503 err
= xenbus_alloc_evtchn(queue
->info
->xbdev
, &queue
->tx_evtchn
);
1506 err
= xenbus_alloc_evtchn(queue
->info
->xbdev
, &queue
->rx_evtchn
);
1508 goto alloc_rx_evtchn_fail
;
1510 snprintf(queue
->tx_irq_name
, sizeof(queue
->tx_irq_name
),
1511 "%s-tx", queue
->name
);
1512 err
= bind_evtchn_to_irqhandler(queue
->tx_evtchn
,
1513 xennet_tx_interrupt
,
1514 0, queue
->tx_irq_name
, queue
);
1517 queue
->tx_irq
= err
;
1519 snprintf(queue
->rx_irq_name
, sizeof(queue
->rx_irq_name
),
1520 "%s-rx", queue
->name
);
1521 err
= bind_evtchn_to_irqhandler(queue
->rx_evtchn
,
1522 xennet_rx_interrupt
,
1523 0, queue
->rx_irq_name
, queue
);
1526 queue
->rx_irq
= err
;
1531 unbind_from_irqhandler(queue
->tx_irq
, queue
);
1534 xenbus_free_evtchn(queue
->info
->xbdev
, queue
->rx_evtchn
);
1535 queue
->rx_evtchn
= 0;
1536 alloc_rx_evtchn_fail
:
1537 xenbus_free_evtchn(queue
->info
->xbdev
, queue
->tx_evtchn
);
1538 queue
->tx_evtchn
= 0;
1543 static int setup_netfront(struct xenbus_device
*dev
,
1544 struct netfront_queue
*queue
, unsigned int feature_split_evtchn
)
1546 struct xen_netif_tx_sring
*txs
;
1547 struct xen_netif_rx_sring
*rxs
;
1550 queue
->tx_ring_ref
= GRANT_INVALID_REF
;
1551 queue
->rx_ring_ref
= GRANT_INVALID_REF
;
1552 queue
->rx
.sring
= NULL
;
1553 queue
->tx
.sring
= NULL
;
1555 txs
= (struct xen_netif_tx_sring
*)get_zeroed_page(GFP_NOIO
| __GFP_HIGH
);
1558 xenbus_dev_fatal(dev
, err
, "allocating tx ring page");
1561 SHARED_RING_INIT(txs
);
1562 FRONT_RING_INIT(&queue
->tx
, txs
, PAGE_SIZE
);
1564 err
= xenbus_grant_ring(dev
, virt_to_mfn(txs
));
1566 goto grant_tx_ring_fail
;
1567 queue
->tx_ring_ref
= err
;
1569 rxs
= (struct xen_netif_rx_sring
*)get_zeroed_page(GFP_NOIO
| __GFP_HIGH
);
1572 xenbus_dev_fatal(dev
, err
, "allocating rx ring page");
1573 goto alloc_rx_ring_fail
;
1575 SHARED_RING_INIT(rxs
);
1576 FRONT_RING_INIT(&queue
->rx
, rxs
, PAGE_SIZE
);
1578 err
= xenbus_grant_ring(dev
, virt_to_mfn(rxs
));
1580 goto grant_rx_ring_fail
;
1581 queue
->rx_ring_ref
= err
;
1583 if (feature_split_evtchn
)
1584 err
= setup_netfront_split(queue
);
1585 /* setup single event channel if
1586 * a) feature-split-event-channels == 0
1587 * b) feature-split-event-channels == 1 but failed to setup
1589 if (!feature_split_evtchn
|| (feature_split_evtchn
&& err
))
1590 err
= setup_netfront_single(queue
);
1593 goto alloc_evtchn_fail
;
1597 /* If we fail to setup netfront, it is safe to just revoke access to
1598 * granted pages because backend is not accessing it at this point.
1601 gnttab_end_foreign_access_ref(queue
->rx_ring_ref
, 0);
1603 free_page((unsigned long)rxs
);
1605 gnttab_end_foreign_access_ref(queue
->tx_ring_ref
, 0);
1607 free_page((unsigned long)txs
);
1612 /* Queue-specific initialisation
1613 * This used to be done in xennet_create_dev() but must now
1616 static int xennet_init_queue(struct netfront_queue
*queue
)
1621 spin_lock_init(&queue
->tx_lock
);
1622 spin_lock_init(&queue
->rx_lock
);
1624 init_timer(&queue
->rx_refill_timer
);
1625 queue
->rx_refill_timer
.data
= (unsigned long)queue
;
1626 queue
->rx_refill_timer
.function
= rx_refill_timeout
;
1628 snprintf(queue
->name
, sizeof(queue
->name
), "%s-q%u",
1629 queue
->info
->netdev
->name
, queue
->id
);
1631 /* Initialise tx_skbs as a free chain containing every entry. */
1632 queue
->tx_skb_freelist
= 0;
1633 for (i
= 0; i
< NET_TX_RING_SIZE
; i
++) {
1634 skb_entry_set_link(&queue
->tx_skbs
[i
], i
+1);
1635 queue
->grant_tx_ref
[i
] = GRANT_INVALID_REF
;
1636 queue
->grant_tx_page
[i
] = NULL
;
1639 /* Clear out rx_skbs */
1640 for (i
= 0; i
< NET_RX_RING_SIZE
; i
++) {
1641 queue
->rx_skbs
[i
] = NULL
;
1642 queue
->grant_rx_ref
[i
] = GRANT_INVALID_REF
;
1645 /* A grant for every tx ring slot */
1646 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE
,
1647 &queue
->gref_tx_head
) < 0) {
1648 pr_alert("can't alloc tx grant refs\n");
1653 /* A grant for every rx ring slot */
1654 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE
,
1655 &queue
->gref_rx_head
) < 0) {
1656 pr_alert("can't alloc rx grant refs\n");
1664 gnttab_free_grant_references(queue
->gref_tx_head
);
1669 static int write_queue_xenstore_keys(struct netfront_queue
*queue
,
1670 struct xenbus_transaction
*xbt
, int write_hierarchical
)
1672 /* Write the queue-specific keys into XenStore in the traditional
1673 * way for a single queue, or in a queue subkeys for multiple
1676 struct xenbus_device
*dev
= queue
->info
->xbdev
;
1678 const char *message
;
1682 /* Choose the correct place to write the keys */
1683 if (write_hierarchical
) {
1684 pathsize
= strlen(dev
->nodename
) + 10;
1685 path
= kzalloc(pathsize
, GFP_KERNEL
);
1688 message
= "out of memory while writing ring references";
1691 snprintf(path
, pathsize
, "%s/queue-%u",
1692 dev
->nodename
, queue
->id
);
1694 path
= (char *)dev
->nodename
;
1697 /* Write ring references */
1698 err
= xenbus_printf(*xbt
, path
, "tx-ring-ref", "%u",
1699 queue
->tx_ring_ref
);
1701 message
= "writing tx-ring-ref";
1705 err
= xenbus_printf(*xbt
, path
, "rx-ring-ref", "%u",
1706 queue
->rx_ring_ref
);
1708 message
= "writing rx-ring-ref";
1712 /* Write event channels; taking into account both shared
1713 * and split event channel scenarios.
1715 if (queue
->tx_evtchn
== queue
->rx_evtchn
) {
1716 /* Shared event channel */
1717 err
= xenbus_printf(*xbt
, path
,
1718 "event-channel", "%u", queue
->tx_evtchn
);
1720 message
= "writing event-channel";
1724 /* Split event channels */
1725 err
= xenbus_printf(*xbt
, path
,
1726 "event-channel-tx", "%u", queue
->tx_evtchn
);
1728 message
= "writing event-channel-tx";
1732 err
= xenbus_printf(*xbt
, path
,
1733 "event-channel-rx", "%u", queue
->rx_evtchn
);
1735 message
= "writing event-channel-rx";
1740 if (write_hierarchical
)
1745 if (write_hierarchical
)
1747 xenbus_dev_fatal(dev
, err
, "%s", message
);
1751 static void xennet_destroy_queues(struct netfront_info
*info
)
1757 for (i
= 0; i
< info
->netdev
->real_num_tx_queues
; i
++) {
1758 struct netfront_queue
*queue
= &info
->queues
[i
];
1760 if (netif_running(info
->netdev
))
1761 napi_disable(&queue
->napi
);
1762 netif_napi_del(&queue
->napi
);
1767 kfree(info
->queues
);
1768 info
->queues
= NULL
;
1771 static int xennet_create_queues(struct netfront_info
*info
,
1772 unsigned int num_queues
)
1777 info
->queues
= kcalloc(num_queues
, sizeof(struct netfront_queue
),
1784 for (i
= 0; i
< num_queues
; i
++) {
1785 struct netfront_queue
*queue
= &info
->queues
[i
];
1790 ret
= xennet_init_queue(queue
);
1792 dev_warn(&info
->netdev
->dev
,
1793 "only created %d queues\n", i
);
1798 netif_napi_add(queue
->info
->netdev
, &queue
->napi
,
1800 if (netif_running(info
->netdev
))
1801 napi_enable(&queue
->napi
);
1804 netif_set_real_num_tx_queues(info
->netdev
, num_queues
);
1808 if (num_queues
== 0) {
1809 dev_err(&info
->netdev
->dev
, "no queues\n");
1815 /* Common code used when first setting up, and when resuming. */
1816 static int talk_to_netback(struct xenbus_device
*dev
,
1817 struct netfront_info
*info
)
1819 const char *message
;
1820 struct xenbus_transaction xbt
;
1822 unsigned int feature_split_evtchn
;
1824 unsigned int max_queues
= 0;
1825 struct netfront_queue
*queue
= NULL
;
1826 unsigned int num_queues
= 1;
1828 info
->netdev
->irq
= 0;
1830 /* Check if backend supports multiple queues */
1831 err
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
1832 "multi-queue-max-queues", "%u", &max_queues
);
1835 num_queues
= min(max_queues
, xennet_max_queues
);
1837 /* Check feature-split-event-channels */
1838 err
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
1839 "feature-split-event-channels", "%u",
1840 &feature_split_evtchn
);
1842 feature_split_evtchn
= 0;
1844 /* Read mac addr. */
1845 err
= xen_net_read_mac(dev
, info
->netdev
->dev_addr
);
1847 xenbus_dev_fatal(dev
, err
, "parsing %s/mac", dev
->nodename
);
1852 xennet_destroy_queues(info
);
1854 err
= xennet_create_queues(info
, num_queues
);
1858 /* Create shared ring, alloc event channel -- for each queue */
1859 for (i
= 0; i
< num_queues
; ++i
) {
1860 queue
= &info
->queues
[i
];
1861 err
= setup_netfront(dev
, queue
, feature_split_evtchn
);
1863 /* setup_netfront() will tidy up the current
1864 * queue on error, but we need to clean up
1865 * those already allocated.
1869 netif_set_real_num_tx_queues(info
->netdev
, i
);
1879 err
= xenbus_transaction_start(&xbt
);
1881 xenbus_dev_fatal(dev
, err
, "starting transaction");
1885 if (num_queues
== 1) {
1886 err
= write_queue_xenstore_keys(&info
->queues
[0], &xbt
, 0); /* flat */
1888 goto abort_transaction_no_dev_fatal
;
1890 /* Write the number of queues */
1891 err
= xenbus_printf(xbt
, dev
->nodename
, "multi-queue-num-queues",
1894 message
= "writing multi-queue-num-queues";
1895 goto abort_transaction_no_dev_fatal
;
1898 /* Write the keys for each queue */
1899 for (i
= 0; i
< num_queues
; ++i
) {
1900 queue
= &info
->queues
[i
];
1901 err
= write_queue_xenstore_keys(queue
, &xbt
, 1); /* hierarchical */
1903 goto abort_transaction_no_dev_fatal
;
1907 /* The remaining keys are not queue-specific */
1908 err
= xenbus_printf(xbt
, dev
->nodename
, "request-rx-copy", "%u",
1911 message
= "writing request-rx-copy";
1912 goto abort_transaction
;
1915 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-rx-notify", "%d", 1);
1917 message
= "writing feature-rx-notify";
1918 goto abort_transaction
;
1921 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-sg", "%d", 1);
1923 message
= "writing feature-sg";
1924 goto abort_transaction
;
1927 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-gso-tcpv4", "%d", 1);
1929 message
= "writing feature-gso-tcpv4";
1930 goto abort_transaction
;
1933 err
= xenbus_write(xbt
, dev
->nodename
, "feature-gso-tcpv6", "1");
1935 message
= "writing feature-gso-tcpv6";
1936 goto abort_transaction
;
1939 err
= xenbus_write(xbt
, dev
->nodename
, "feature-ipv6-csum-offload",
1942 message
= "writing feature-ipv6-csum-offload";
1943 goto abort_transaction
;
1946 err
= xenbus_transaction_end(xbt
, 0);
1950 xenbus_dev_fatal(dev
, err
, "completing transaction");
1957 xenbus_dev_fatal(dev
, err
, "%s", message
);
1958 abort_transaction_no_dev_fatal
:
1959 xenbus_transaction_end(xbt
, 1);
1961 xennet_disconnect_backend(info
);
1962 kfree(info
->queues
);
1963 info
->queues
= NULL
;
1965 netif_set_real_num_tx_queues(info
->netdev
, 0);
1971 static int xennet_connect(struct net_device
*dev
)
1973 struct netfront_info
*np
= netdev_priv(dev
);
1974 unsigned int num_queues
= 0;
1976 unsigned int feature_rx_copy
;
1978 struct netfront_queue
*queue
= NULL
;
1980 err
= xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1981 "feature-rx-copy", "%u", &feature_rx_copy
);
1983 feature_rx_copy
= 0;
1985 if (!feature_rx_copy
) {
1987 "backend does not support copying receive path\n");
1991 err
= talk_to_netback(np
->xbdev
, np
);
1995 /* talk_to_netback() sets the correct number of queues */
1996 num_queues
= dev
->real_num_tx_queues
;
1999 netdev_update_features(dev
);
2003 * All public and private state should now be sane. Get
2004 * ready to start sending and receiving packets and give the driver
2005 * domain a kick because we've probably just requeued some
2008 netif_carrier_on(np
->netdev
);
2009 for (j
= 0; j
< num_queues
; ++j
) {
2010 queue
= &np
->queues
[j
];
2012 notify_remote_via_irq(queue
->tx_irq
);
2013 if (queue
->tx_irq
!= queue
->rx_irq
)
2014 notify_remote_via_irq(queue
->rx_irq
);
2016 spin_lock_irq(&queue
->tx_lock
);
2017 xennet_tx_buf_gc(queue
);
2018 spin_unlock_irq(&queue
->tx_lock
);
2020 spin_lock_bh(&queue
->rx_lock
);
2021 xennet_alloc_rx_buffers(queue
);
2022 spin_unlock_bh(&queue
->rx_lock
);
2029 * Callback received when the backend's state changes.
2031 static void netback_changed(struct xenbus_device
*dev
,
2032 enum xenbus_state backend_state
)
2034 struct netfront_info
*np
= dev_get_drvdata(&dev
->dev
);
2035 struct net_device
*netdev
= np
->netdev
;
2037 dev_dbg(&dev
->dev
, "%s\n", xenbus_strstate(backend_state
));
2039 switch (backend_state
) {
2040 case XenbusStateInitialising
:
2041 case XenbusStateInitialised
:
2042 case XenbusStateReconfiguring
:
2043 case XenbusStateReconfigured
:
2044 case XenbusStateUnknown
:
2047 case XenbusStateInitWait
:
2048 if (dev
->state
!= XenbusStateInitialising
)
2050 if (xennet_connect(netdev
) != 0)
2052 xenbus_switch_state(dev
, XenbusStateConnected
);
2055 case XenbusStateConnected
:
2056 netdev_notify_peers(netdev
);
2059 case XenbusStateClosed
:
2060 if (dev
->state
== XenbusStateClosed
)
2062 /* Missed the backend's CLOSING state -- fallthrough */
2063 case XenbusStateClosing
:
2064 xenbus_frontend_closed(dev
);
2069 static const struct xennet_stat
{
2070 char name
[ETH_GSTRING_LEN
];
2072 } xennet_stats
[] = {
2074 "rx_gso_checksum_fixup",
2075 offsetof(struct netfront_info
, rx_gso_checksum_fixup
)
2079 static int xennet_get_sset_count(struct net_device
*dev
, int string_set
)
2081 switch (string_set
) {
2083 return ARRAY_SIZE(xennet_stats
);
2089 static void xennet_get_ethtool_stats(struct net_device
*dev
,
2090 struct ethtool_stats
*stats
, u64
* data
)
2092 void *np
= netdev_priv(dev
);
2095 for (i
= 0; i
< ARRAY_SIZE(xennet_stats
); i
++)
2096 data
[i
] = atomic_read((atomic_t
*)(np
+ xennet_stats
[i
].offset
));
2099 static void xennet_get_strings(struct net_device
*dev
, u32 stringset
, u8
* data
)
2103 switch (stringset
) {
2105 for (i
= 0; i
< ARRAY_SIZE(xennet_stats
); i
++)
2106 memcpy(data
+ i
* ETH_GSTRING_LEN
,
2107 xennet_stats
[i
].name
, ETH_GSTRING_LEN
);
2112 static const struct ethtool_ops xennet_ethtool_ops
=
2114 .get_link
= ethtool_op_get_link
,
2116 .get_sset_count
= xennet_get_sset_count
,
2117 .get_ethtool_stats
= xennet_get_ethtool_stats
,
2118 .get_strings
= xennet_get_strings
,
2122 static ssize_t
show_rxbuf(struct device
*dev
,
2123 struct device_attribute
*attr
, char *buf
)
2125 return sprintf(buf
, "%lu\n", NET_RX_RING_SIZE
);
2128 static ssize_t
store_rxbuf(struct device
*dev
,
2129 struct device_attribute
*attr
,
2130 const char *buf
, size_t len
)
2133 unsigned long target
;
2135 if (!capable(CAP_NET_ADMIN
))
2138 target
= simple_strtoul(buf
, &endp
, 0);
2142 /* rxbuf_min and rxbuf_max are no longer configurable. */
2147 static struct device_attribute xennet_attrs
[] = {
2148 __ATTR(rxbuf_min
, S_IRUGO
|S_IWUSR
, show_rxbuf
, store_rxbuf
),
2149 __ATTR(rxbuf_max
, S_IRUGO
|S_IWUSR
, show_rxbuf
, store_rxbuf
),
2150 __ATTR(rxbuf_cur
, S_IRUGO
, show_rxbuf
, NULL
),
2153 static int xennet_sysfs_addif(struct net_device
*netdev
)
2158 for (i
= 0; i
< ARRAY_SIZE(xennet_attrs
); i
++) {
2159 err
= device_create_file(&netdev
->dev
,
2168 device_remove_file(&netdev
->dev
, &xennet_attrs
[i
]);
2172 static void xennet_sysfs_delif(struct net_device
*netdev
)
2176 for (i
= 0; i
< ARRAY_SIZE(xennet_attrs
); i
++)
2177 device_remove_file(&netdev
->dev
, &xennet_attrs
[i
]);
2180 #endif /* CONFIG_SYSFS */
2182 static int xennet_remove(struct xenbus_device
*dev
)
2184 struct netfront_info
*info
= dev_get_drvdata(&dev
->dev
);
2185 unsigned int num_queues
= info
->netdev
->real_num_tx_queues
;
2186 struct netfront_queue
*queue
= NULL
;
2189 dev_dbg(&dev
->dev
, "%s\n", dev
->nodename
);
2191 xennet_disconnect_backend(info
);
2193 xennet_sysfs_delif(info
->netdev
);
2195 unregister_netdev(info
->netdev
);
2197 for (i
= 0; i
< num_queues
; ++i
) {
2198 queue
= &info
->queues
[i
];
2199 del_timer_sync(&queue
->rx_refill_timer
);
2203 kfree(info
->queues
);
2204 info
->queues
= NULL
;
2207 xennet_free_netdev(info
->netdev
);
2212 static const struct xenbus_device_id netfront_ids
[] = {
2217 static struct xenbus_driver netfront_driver
= {
2218 .ids
= netfront_ids
,
2219 .probe
= netfront_probe
,
2220 .remove
= xennet_remove
,
2221 .resume
= netfront_resume
,
2222 .otherend_changed
= netback_changed
,
2225 static int __init
netif_init(void)
2230 if (!xen_has_pv_nic_devices())
2233 pr_info("Initialising Xen virtual ethernet driver\n");
2235 /* Allow as many queues as there are CPUs, by default */
2236 xennet_max_queues
= num_online_cpus();
2238 return xenbus_register_frontend(&netfront_driver
);
2240 module_init(netif_init
);
2243 static void __exit
netif_exit(void)
2245 xenbus_unregister_driver(&netfront_driver
);
2247 module_exit(netif_exit
);
2249 MODULE_DESCRIPTION("Xen virtual network device frontend");
2250 MODULE_LICENSE("GPL");
2251 MODULE_ALIAS("xen:vif");
2252 MODULE_ALIAS("xennet");