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
];
147 struct netfront_info
{
148 struct list_head list
;
149 struct net_device
*netdev
;
151 struct xenbus_device
*xbdev
;
153 /* Multi-queue support */
154 struct netfront_queue
*queues
;
157 struct netfront_stats __percpu
*rx_stats
;
158 struct netfront_stats __percpu
*tx_stats
;
160 atomic_t rx_gso_checksum_fixup
;
163 struct netfront_rx_info
{
164 struct xen_netif_rx_response rx
;
165 struct xen_netif_extra_info extras
[XEN_NETIF_EXTRA_TYPE_MAX
- 1];
168 static void skb_entry_set_link(union skb_entry
*list
, unsigned short id
)
173 static int skb_entry_is_link(const union skb_entry
*list
)
175 BUILD_BUG_ON(sizeof(list
->skb
) != sizeof(list
->link
));
176 return (unsigned long)list
->skb
< PAGE_OFFSET
;
180 * Access macros for acquiring freeing slots in tx_skbs[].
183 static void add_id_to_freelist(unsigned *head
, union skb_entry
*list
,
186 skb_entry_set_link(&list
[id
], *head
);
190 static unsigned short get_id_from_freelist(unsigned *head
,
191 union skb_entry
*list
)
193 unsigned int id
= *head
;
194 *head
= list
[id
].link
;
198 static int xennet_rxidx(RING_IDX idx
)
200 return idx
& (NET_RX_RING_SIZE
- 1);
203 static struct sk_buff
*xennet_get_rx_skb(struct netfront_queue
*queue
,
206 int i
= xennet_rxidx(ri
);
207 struct sk_buff
*skb
= queue
->rx_skbs
[i
];
208 queue
->rx_skbs
[i
] = NULL
;
212 static grant_ref_t
xennet_get_rx_ref(struct netfront_queue
*queue
,
215 int i
= xennet_rxidx(ri
);
216 grant_ref_t ref
= queue
->grant_rx_ref
[i
];
217 queue
->grant_rx_ref
[i
] = GRANT_INVALID_REF
;
222 static int xennet_sysfs_addif(struct net_device
*netdev
);
223 static void xennet_sysfs_delif(struct net_device
*netdev
);
224 #else /* !CONFIG_SYSFS */
225 #define xennet_sysfs_addif(dev) (0)
226 #define xennet_sysfs_delif(dev) do { } while (0)
229 static bool xennet_can_sg(struct net_device
*dev
)
231 return dev
->features
& NETIF_F_SG
;
235 static void rx_refill_timeout(unsigned long data
)
237 struct netfront_queue
*queue
= (struct netfront_queue
*)data
;
238 napi_schedule(&queue
->napi
);
241 static int netfront_tx_slot_available(struct netfront_queue
*queue
)
243 return (queue
->tx
.req_prod_pvt
- queue
->tx
.rsp_cons
) <
244 (NET_TX_RING_SIZE
- MAX_SKB_FRAGS
- 2);
247 static void xennet_maybe_wake_tx(struct netfront_queue
*queue
)
249 struct net_device
*dev
= queue
->info
->netdev
;
250 struct netdev_queue
*dev_queue
= netdev_get_tx_queue(dev
, queue
->id
);
252 if (unlikely(netif_tx_queue_stopped(dev_queue
)) &&
253 netfront_tx_slot_available(queue
) &&
254 likely(netif_running(dev
)))
255 netif_tx_wake_queue(netdev_get_tx_queue(dev
, queue
->id
));
259 static struct sk_buff
*xennet_alloc_one_rx_buffer(struct netfront_queue
*queue
)
264 skb
= __netdev_alloc_skb(queue
->info
->netdev
,
265 RX_COPY_THRESHOLD
+ NET_IP_ALIGN
,
266 GFP_ATOMIC
| __GFP_NOWARN
);
270 page
= alloc_page(GFP_ATOMIC
| __GFP_NOWARN
);
275 skb_add_rx_frag(skb
, 0, page
, 0, 0, PAGE_SIZE
);
277 /* Align ip header to a 16 bytes boundary */
278 skb_reserve(skb
, NET_IP_ALIGN
);
279 skb
->dev
= queue
->info
->netdev
;
285 static void xennet_alloc_rx_buffers(struct netfront_queue
*queue
)
287 RING_IDX req_prod
= queue
->rx
.req_prod_pvt
;
290 if (unlikely(!netif_carrier_ok(queue
->info
->netdev
)))
293 for (req_prod
= queue
->rx
.req_prod_pvt
;
294 req_prod
- queue
->rx
.rsp_cons
< NET_RX_RING_SIZE
;
300 struct xen_netif_rx_request
*req
;
302 skb
= xennet_alloc_one_rx_buffer(queue
);
306 id
= xennet_rxidx(req_prod
);
308 BUG_ON(queue
->rx_skbs
[id
]);
309 queue
->rx_skbs
[id
] = skb
;
311 ref
= gnttab_claim_grant_reference(&queue
->gref_rx_head
);
312 BUG_ON((signed short)ref
< 0);
313 queue
->grant_rx_ref
[id
] = ref
;
315 pfn
= page_to_pfn(skb_frag_page(&skb_shinfo(skb
)->frags
[0]));
317 req
= RING_GET_REQUEST(&queue
->rx
, req_prod
);
318 gnttab_grant_foreign_access_ref(ref
,
319 queue
->info
->xbdev
->otherend_id
,
327 queue
->rx
.req_prod_pvt
= req_prod
;
329 /* Not enough requests? Try again later. */
330 if (req_prod
- queue
->rx
.rsp_cons
< NET_RX_SLOTS_MIN
) {
331 mod_timer(&queue
->rx_refill_timer
, jiffies
+ (HZ
/10));
335 wmb(); /* barrier so backend seens requests */
337 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue
->rx
, notify
);
339 notify_remote_via_irq(queue
->rx_irq
);
342 static int xennet_open(struct net_device
*dev
)
344 struct netfront_info
*np
= netdev_priv(dev
);
345 unsigned int num_queues
= dev
->real_num_tx_queues
;
347 struct netfront_queue
*queue
= NULL
;
349 for (i
= 0; i
< num_queues
; ++i
) {
350 queue
= &np
->queues
[i
];
351 napi_enable(&queue
->napi
);
353 spin_lock_bh(&queue
->rx_lock
);
354 if (netif_carrier_ok(dev
)) {
355 xennet_alloc_rx_buffers(queue
);
356 queue
->rx
.sring
->rsp_event
= queue
->rx
.rsp_cons
+ 1;
357 if (RING_HAS_UNCONSUMED_RESPONSES(&queue
->rx
))
358 napi_schedule(&queue
->napi
);
360 spin_unlock_bh(&queue
->rx_lock
);
363 netif_tx_start_all_queues(dev
);
368 static void xennet_tx_buf_gc(struct netfront_queue
*queue
)
374 BUG_ON(!netif_carrier_ok(queue
->info
->netdev
));
377 prod
= queue
->tx
.sring
->rsp_prod
;
378 rmb(); /* Ensure we see responses up to 'rp'. */
380 for (cons
= queue
->tx
.rsp_cons
; cons
!= prod
; cons
++) {
381 struct xen_netif_tx_response
*txrsp
;
383 txrsp
= RING_GET_RESPONSE(&queue
->tx
, cons
);
384 if (txrsp
->status
== XEN_NETIF_RSP_NULL
)
388 skb
= queue
->tx_skbs
[id
].skb
;
389 if (unlikely(gnttab_query_foreign_access(
390 queue
->grant_tx_ref
[id
]) != 0)) {
391 pr_alert("%s: warning -- grant still in use by backend domain\n",
395 gnttab_end_foreign_access_ref(
396 queue
->grant_tx_ref
[id
], GNTMAP_readonly
);
397 gnttab_release_grant_reference(
398 &queue
->gref_tx_head
, queue
->grant_tx_ref
[id
]);
399 queue
->grant_tx_ref
[id
] = GRANT_INVALID_REF
;
400 queue
->grant_tx_page
[id
] = NULL
;
401 add_id_to_freelist(&queue
->tx_skb_freelist
, queue
->tx_skbs
, id
);
402 dev_kfree_skb_irq(skb
);
405 queue
->tx
.rsp_cons
= prod
;
408 * Set a new event, then check for race with update of tx_cons.
409 * Note that it is essential to schedule a callback, no matter
410 * how few buffers are pending. Even if there is space in the
411 * transmit ring, higher layers may be blocked because too much
412 * data is outstanding: in such cases notification from Xen is
413 * likely to be the only kick that we'll get.
415 queue
->tx
.sring
->rsp_event
=
416 prod
+ ((queue
->tx
.sring
->req_prod
- prod
) >> 1) + 1;
417 mb(); /* update shared area */
418 } while ((cons
== prod
) && (prod
!= queue
->tx
.sring
->rsp_prod
));
420 xennet_maybe_wake_tx(queue
);
423 static struct xen_netif_tx_request
*xennet_make_one_txreq(
424 struct netfront_queue
*queue
, struct sk_buff
*skb
,
425 struct page
*page
, unsigned int offset
, unsigned int len
)
428 struct xen_netif_tx_request
*tx
;
431 len
= min_t(unsigned int, PAGE_SIZE
- offset
, len
);
433 id
= get_id_from_freelist(&queue
->tx_skb_freelist
, queue
->tx_skbs
);
434 tx
= RING_GET_REQUEST(&queue
->tx
, queue
->tx
.req_prod_pvt
++);
435 ref
= gnttab_claim_grant_reference(&queue
->gref_tx_head
);
436 BUG_ON((signed short)ref
< 0);
438 gnttab_grant_foreign_access_ref(ref
, queue
->info
->xbdev
->otherend_id
,
439 page_to_mfn(page
), GNTMAP_readonly
);
441 queue
->tx_skbs
[id
].skb
= skb
;
442 queue
->grant_tx_page
[id
] = page
;
443 queue
->grant_tx_ref
[id
] = ref
;
454 static struct xen_netif_tx_request
*xennet_make_txreqs(
455 struct netfront_queue
*queue
, struct xen_netif_tx_request
*tx
,
456 struct sk_buff
*skb
, struct page
*page
,
457 unsigned int offset
, unsigned int len
)
459 /* Skip unused frames from start of page */
460 page
+= offset
>> PAGE_SHIFT
;
461 offset
&= ~PAGE_MASK
;
464 tx
->flags
|= XEN_NETTXF_more_data
;
465 tx
= xennet_make_one_txreq(queue
, skb_get(skb
),
476 * Count how many ring slots are required to send this skb. Each frag
477 * might be a compound page.
479 static int xennet_count_skb_slots(struct sk_buff
*skb
)
481 int i
, frags
= skb_shinfo(skb
)->nr_frags
;
484 pages
= PFN_UP(offset_in_page(skb
->data
) + skb_headlen(skb
));
486 for (i
= 0; i
< frags
; i
++) {
487 skb_frag_t
*frag
= skb_shinfo(skb
)->frags
+ i
;
488 unsigned long size
= skb_frag_size(frag
);
489 unsigned long offset
= frag
->page_offset
;
491 /* Skip unused frames from start of page */
492 offset
&= ~PAGE_MASK
;
494 pages
+= PFN_UP(offset
+ size
);
500 static u16
xennet_select_queue(struct net_device
*dev
, struct sk_buff
*skb
,
501 void *accel_priv
, select_queue_fallback_t fallback
)
503 unsigned int num_queues
= dev
->real_num_tx_queues
;
507 /* First, check if there is only one queue */
508 if (num_queues
== 1) {
511 hash
= skb_get_hash(skb
);
512 queue_idx
= hash
% num_queues
;
518 static int xennet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
520 struct netfront_info
*np
= netdev_priv(dev
);
521 struct netfront_stats
*tx_stats
= this_cpu_ptr(np
->tx_stats
);
522 struct xen_netif_tx_request
*tx
, *first_tx
;
530 struct netfront_queue
*queue
= NULL
;
531 unsigned int num_queues
= dev
->real_num_tx_queues
;
534 /* Drop the packet if no queues are set up */
537 /* Determine which queue to transmit this SKB on */
538 queue_index
= skb_get_queue_mapping(skb
);
539 queue
= &np
->queues
[queue_index
];
541 /* If skb->len is too big for wire format, drop skb and alert
542 * user about misconfiguration.
544 if (unlikely(skb
->len
> XEN_NETIF_MAX_TX_SIZE
)) {
545 net_alert_ratelimited(
546 "xennet: skb->len = %u, too big for wire format\n",
551 slots
= xennet_count_skb_slots(skb
);
552 if (unlikely(slots
> MAX_SKB_FRAGS
+ 1)) {
553 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
555 if (skb_linearize(skb
))
559 page
= virt_to_page(skb
->data
);
560 offset
= offset_in_page(skb
->data
);
561 len
= skb_headlen(skb
);
563 spin_lock_irqsave(&queue
->tx_lock
, flags
);
565 if (unlikely(!netif_carrier_ok(dev
) ||
566 (slots
> 1 && !xennet_can_sg(dev
)) ||
567 netif_needs_gso(dev
, skb
, netif_skb_features(skb
)))) {
568 spin_unlock_irqrestore(&queue
->tx_lock
, flags
);
572 /* First request for the linear area. */
573 first_tx
= tx
= xennet_make_one_txreq(queue
, skb
,
579 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
581 tx
->flags
|= XEN_NETTXF_csum_blank
| XEN_NETTXF_data_validated
;
582 else if (skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
583 /* remote but checksummed. */
584 tx
->flags
|= XEN_NETTXF_data_validated
;
586 /* Optional extra info after the first request. */
587 if (skb_shinfo(skb
)->gso_size
) {
588 struct xen_netif_extra_info
*gso
;
590 gso
= (struct xen_netif_extra_info
*)
591 RING_GET_REQUEST(&queue
->tx
, queue
->tx
.req_prod_pvt
++);
593 tx
->flags
|= XEN_NETTXF_extra_info
;
595 gso
->u
.gso
.size
= skb_shinfo(skb
)->gso_size
;
596 gso
->u
.gso
.type
= (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV6
) ?
597 XEN_NETIF_GSO_TYPE_TCPV6
:
598 XEN_NETIF_GSO_TYPE_TCPV4
;
600 gso
->u
.gso
.features
= 0;
602 gso
->type
= XEN_NETIF_EXTRA_TYPE_GSO
;
606 /* Requests for the rest of the linear area. */
607 tx
= xennet_make_txreqs(queue
, tx
, skb
, page
, offset
, len
);
609 /* Requests for all the frags. */
610 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
611 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
612 tx
= xennet_make_txreqs(queue
, tx
, skb
,
613 skb_frag_page(frag
), frag
->page_offset
,
614 skb_frag_size(frag
));
617 /* First request has the packet length. */
618 first_tx
->size
= skb
->len
;
620 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue
->tx
, notify
);
622 notify_remote_via_irq(queue
->tx_irq
);
624 u64_stats_update_begin(&tx_stats
->syncp
);
625 tx_stats
->bytes
+= skb
->len
;
627 u64_stats_update_end(&tx_stats
->syncp
);
629 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
630 xennet_tx_buf_gc(queue
);
632 if (!netfront_tx_slot_available(queue
))
633 netif_tx_stop_queue(netdev_get_tx_queue(dev
, queue
->id
));
635 spin_unlock_irqrestore(&queue
->tx_lock
, flags
);
640 dev
->stats
.tx_dropped
++;
641 dev_kfree_skb_any(skb
);
645 static int xennet_close(struct net_device
*dev
)
647 struct netfront_info
*np
= netdev_priv(dev
);
648 unsigned int num_queues
= dev
->real_num_tx_queues
;
650 struct netfront_queue
*queue
;
651 netif_tx_stop_all_queues(np
->netdev
);
652 for (i
= 0; i
< num_queues
; ++i
) {
653 queue
= &np
->queues
[i
];
654 napi_disable(&queue
->napi
);
659 static void xennet_move_rx_slot(struct netfront_queue
*queue
, struct sk_buff
*skb
,
662 int new = xennet_rxidx(queue
->rx
.req_prod_pvt
);
664 BUG_ON(queue
->rx_skbs
[new]);
665 queue
->rx_skbs
[new] = skb
;
666 queue
->grant_rx_ref
[new] = ref
;
667 RING_GET_REQUEST(&queue
->rx
, queue
->rx
.req_prod_pvt
)->id
= new;
668 RING_GET_REQUEST(&queue
->rx
, queue
->rx
.req_prod_pvt
)->gref
= ref
;
669 queue
->rx
.req_prod_pvt
++;
672 static int xennet_get_extras(struct netfront_queue
*queue
,
673 struct xen_netif_extra_info
*extras
,
677 struct xen_netif_extra_info
*extra
;
678 struct device
*dev
= &queue
->info
->netdev
->dev
;
679 RING_IDX cons
= queue
->rx
.rsp_cons
;
686 if (unlikely(cons
+ 1 == rp
)) {
688 dev_warn(dev
, "Missing extra info\n");
693 extra
= (struct xen_netif_extra_info
*)
694 RING_GET_RESPONSE(&queue
->rx
, ++cons
);
696 if (unlikely(!extra
->type
||
697 extra
->type
>= XEN_NETIF_EXTRA_TYPE_MAX
)) {
699 dev_warn(dev
, "Invalid extra type: %d\n",
703 memcpy(&extras
[extra
->type
- 1], extra
,
707 skb
= xennet_get_rx_skb(queue
, cons
);
708 ref
= xennet_get_rx_ref(queue
, cons
);
709 xennet_move_rx_slot(queue
, skb
, ref
);
710 } while (extra
->flags
& XEN_NETIF_EXTRA_FLAG_MORE
);
712 queue
->rx
.rsp_cons
= cons
;
716 static int xennet_get_responses(struct netfront_queue
*queue
,
717 struct netfront_rx_info
*rinfo
, RING_IDX rp
,
718 struct sk_buff_head
*list
)
720 struct xen_netif_rx_response
*rx
= &rinfo
->rx
;
721 struct xen_netif_extra_info
*extras
= rinfo
->extras
;
722 struct device
*dev
= &queue
->info
->netdev
->dev
;
723 RING_IDX cons
= queue
->rx
.rsp_cons
;
724 struct sk_buff
*skb
= xennet_get_rx_skb(queue
, cons
);
725 grant_ref_t ref
= xennet_get_rx_ref(queue
, cons
);
726 int max
= MAX_SKB_FRAGS
+ (rx
->status
<= RX_COPY_THRESHOLD
);
731 if (rx
->flags
& XEN_NETRXF_extra_info
) {
732 err
= xennet_get_extras(queue
, extras
, rp
);
733 cons
= queue
->rx
.rsp_cons
;
737 if (unlikely(rx
->status
< 0 ||
738 rx
->offset
+ rx
->status
> PAGE_SIZE
)) {
740 dev_warn(dev
, "rx->offset: %x, size: %u\n",
741 rx
->offset
, rx
->status
);
742 xennet_move_rx_slot(queue
, skb
, ref
);
748 * This definitely indicates a bug, either in this driver or in
749 * the backend driver. In future this should flag the bad
750 * situation to the system controller to reboot the backend.
752 if (ref
== GRANT_INVALID_REF
) {
754 dev_warn(dev
, "Bad rx response id %d.\n",
760 ret
= gnttab_end_foreign_access_ref(ref
, 0);
763 gnttab_release_grant_reference(&queue
->gref_rx_head
, ref
);
765 __skb_queue_tail(list
, skb
);
768 if (!(rx
->flags
& XEN_NETRXF_more_data
))
771 if (cons
+ slots
== rp
) {
773 dev_warn(dev
, "Need more slots\n");
778 rx
= RING_GET_RESPONSE(&queue
->rx
, cons
+ slots
);
779 skb
= xennet_get_rx_skb(queue
, cons
+ slots
);
780 ref
= xennet_get_rx_ref(queue
, cons
+ slots
);
784 if (unlikely(slots
> max
)) {
786 dev_warn(dev
, "Too many slots\n");
791 queue
->rx
.rsp_cons
= cons
+ slots
;
796 static int xennet_set_skb_gso(struct sk_buff
*skb
,
797 struct xen_netif_extra_info
*gso
)
799 if (!gso
->u
.gso
.size
) {
801 pr_warn("GSO size must not be zero\n");
805 if (gso
->u
.gso
.type
!= XEN_NETIF_GSO_TYPE_TCPV4
&&
806 gso
->u
.gso
.type
!= XEN_NETIF_GSO_TYPE_TCPV6
) {
808 pr_warn("Bad GSO type %d\n", gso
->u
.gso
.type
);
812 skb_shinfo(skb
)->gso_size
= gso
->u
.gso
.size
;
813 skb_shinfo(skb
)->gso_type
=
814 (gso
->u
.gso
.type
== XEN_NETIF_GSO_TYPE_TCPV4
) ?
818 /* Header must be checked, and gso_segs computed. */
819 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
820 skb_shinfo(skb
)->gso_segs
= 0;
825 static RING_IDX
xennet_fill_frags(struct netfront_queue
*queue
,
827 struct sk_buff_head
*list
)
829 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
830 RING_IDX cons
= queue
->rx
.rsp_cons
;
831 struct sk_buff
*nskb
;
833 while ((nskb
= __skb_dequeue(list
))) {
834 struct xen_netif_rx_response
*rx
=
835 RING_GET_RESPONSE(&queue
->rx
, ++cons
);
836 skb_frag_t
*nfrag
= &skb_shinfo(nskb
)->frags
[0];
838 if (shinfo
->nr_frags
== MAX_SKB_FRAGS
) {
839 unsigned int pull_to
= NETFRONT_SKB_CB(skb
)->pull_to
;
841 BUG_ON(pull_to
<= skb_headlen(skb
));
842 __pskb_pull_tail(skb
, pull_to
- skb_headlen(skb
));
844 BUG_ON(shinfo
->nr_frags
>= MAX_SKB_FRAGS
);
846 skb_add_rx_frag(skb
, shinfo
->nr_frags
, skb_frag_page(nfrag
),
847 rx
->offset
, rx
->status
, PAGE_SIZE
);
849 skb_shinfo(nskb
)->nr_frags
= 0;
856 static int checksum_setup(struct net_device
*dev
, struct sk_buff
*skb
)
858 bool recalculate_partial_csum
= false;
861 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
862 * peers can fail to set NETRXF_csum_blank when sending a GSO
863 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
864 * recalculate the partial checksum.
866 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
&& skb_is_gso(skb
)) {
867 struct netfront_info
*np
= netdev_priv(dev
);
868 atomic_inc(&np
->rx_gso_checksum_fixup
);
869 skb
->ip_summed
= CHECKSUM_PARTIAL
;
870 recalculate_partial_csum
= true;
873 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
874 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
877 return skb_checksum_setup(skb
, recalculate_partial_csum
);
880 static int handle_incoming_queue(struct netfront_queue
*queue
,
881 struct sk_buff_head
*rxq
)
883 struct netfront_stats
*rx_stats
= this_cpu_ptr(queue
->info
->rx_stats
);
884 int packets_dropped
= 0;
887 while ((skb
= __skb_dequeue(rxq
)) != NULL
) {
888 int pull_to
= NETFRONT_SKB_CB(skb
)->pull_to
;
890 if (pull_to
> skb_headlen(skb
))
891 __pskb_pull_tail(skb
, pull_to
- skb_headlen(skb
));
893 /* Ethernet work: Delayed to here as it peeks the header. */
894 skb
->protocol
= eth_type_trans(skb
, queue
->info
->netdev
);
895 skb_reset_network_header(skb
);
897 if (checksum_setup(queue
->info
->netdev
, skb
)) {
900 queue
->info
->netdev
->stats
.rx_errors
++;
904 u64_stats_update_begin(&rx_stats
->syncp
);
906 rx_stats
->bytes
+= skb
->len
;
907 u64_stats_update_end(&rx_stats
->syncp
);
910 napi_gro_receive(&queue
->napi
, skb
);
913 return packets_dropped
;
916 static int xennet_poll(struct napi_struct
*napi
, int budget
)
918 struct netfront_queue
*queue
= container_of(napi
, struct netfront_queue
, napi
);
919 struct net_device
*dev
= queue
->info
->netdev
;
921 struct netfront_rx_info rinfo
;
922 struct xen_netif_rx_response
*rx
= &rinfo
.rx
;
923 struct xen_netif_extra_info
*extras
= rinfo
.extras
;
926 struct sk_buff_head rxq
;
927 struct sk_buff_head errq
;
928 struct sk_buff_head tmpq
;
931 spin_lock(&queue
->rx_lock
);
933 skb_queue_head_init(&rxq
);
934 skb_queue_head_init(&errq
);
935 skb_queue_head_init(&tmpq
);
937 rp
= queue
->rx
.sring
->rsp_prod
;
938 rmb(); /* Ensure we see queued responses up to 'rp'. */
940 i
= queue
->rx
.rsp_cons
;
942 while ((i
!= rp
) && (work_done
< budget
)) {
943 memcpy(rx
, RING_GET_RESPONSE(&queue
->rx
, i
), sizeof(*rx
));
944 memset(extras
, 0, sizeof(rinfo
.extras
));
946 err
= xennet_get_responses(queue
, &rinfo
, rp
, &tmpq
);
950 while ((skb
= __skb_dequeue(&tmpq
)))
951 __skb_queue_tail(&errq
, skb
);
952 dev
->stats
.rx_errors
++;
953 i
= queue
->rx
.rsp_cons
;
957 skb
= __skb_dequeue(&tmpq
);
959 if (extras
[XEN_NETIF_EXTRA_TYPE_GSO
- 1].type
) {
960 struct xen_netif_extra_info
*gso
;
961 gso
= &extras
[XEN_NETIF_EXTRA_TYPE_GSO
- 1];
963 if (unlikely(xennet_set_skb_gso(skb
, gso
))) {
964 __skb_queue_head(&tmpq
, skb
);
965 queue
->rx
.rsp_cons
+= skb_queue_len(&tmpq
);
970 NETFRONT_SKB_CB(skb
)->pull_to
= rx
->status
;
971 if (NETFRONT_SKB_CB(skb
)->pull_to
> RX_COPY_THRESHOLD
)
972 NETFRONT_SKB_CB(skb
)->pull_to
= RX_COPY_THRESHOLD
;
974 skb_shinfo(skb
)->frags
[0].page_offset
= rx
->offset
;
975 skb_frag_size_set(&skb_shinfo(skb
)->frags
[0], rx
->status
);
976 skb
->data_len
= rx
->status
;
977 skb
->len
+= rx
->status
;
979 i
= xennet_fill_frags(queue
, skb
, &tmpq
);
981 if (rx
->flags
& XEN_NETRXF_csum_blank
)
982 skb
->ip_summed
= CHECKSUM_PARTIAL
;
983 else if (rx
->flags
& XEN_NETRXF_data_validated
)
984 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
986 __skb_queue_tail(&rxq
, skb
);
988 queue
->rx
.rsp_cons
= ++i
;
992 __skb_queue_purge(&errq
);
994 work_done
-= handle_incoming_queue(queue
, &rxq
);
996 xennet_alloc_rx_buffers(queue
);
998 if (work_done
< budget
) {
1001 napi_complete(napi
);
1003 RING_FINAL_CHECK_FOR_RESPONSES(&queue
->rx
, more_to_do
);
1005 napi_schedule(napi
);
1008 spin_unlock(&queue
->rx_lock
);
1013 static int xennet_change_mtu(struct net_device
*dev
, int mtu
)
1015 int max
= xennet_can_sg(dev
) ?
1016 XEN_NETIF_MAX_TX_SIZE
- MAX_TCP_HEADER
: ETH_DATA_LEN
;
1024 static struct rtnl_link_stats64
*xennet_get_stats64(struct net_device
*dev
,
1025 struct rtnl_link_stats64
*tot
)
1027 struct netfront_info
*np
= netdev_priv(dev
);
1030 for_each_possible_cpu(cpu
) {
1031 struct netfront_stats
*rx_stats
= per_cpu_ptr(np
->rx_stats
, cpu
);
1032 struct netfront_stats
*tx_stats
= per_cpu_ptr(np
->tx_stats
, cpu
);
1033 u64 rx_packets
, rx_bytes
, tx_packets
, tx_bytes
;
1037 start
= u64_stats_fetch_begin_irq(&tx_stats
->syncp
);
1038 tx_packets
= tx_stats
->packets
;
1039 tx_bytes
= tx_stats
->bytes
;
1040 } while (u64_stats_fetch_retry_irq(&tx_stats
->syncp
, start
));
1043 start
= u64_stats_fetch_begin_irq(&rx_stats
->syncp
);
1044 rx_packets
= rx_stats
->packets
;
1045 rx_bytes
= rx_stats
->bytes
;
1046 } while (u64_stats_fetch_retry_irq(&rx_stats
->syncp
, start
));
1048 tot
->rx_packets
+= rx_packets
;
1049 tot
->tx_packets
+= tx_packets
;
1050 tot
->rx_bytes
+= rx_bytes
;
1051 tot
->tx_bytes
+= tx_bytes
;
1054 tot
->rx_errors
= dev
->stats
.rx_errors
;
1055 tot
->tx_dropped
= dev
->stats
.tx_dropped
;
1060 static void xennet_release_tx_bufs(struct netfront_queue
*queue
)
1062 struct sk_buff
*skb
;
1065 for (i
= 0; i
< NET_TX_RING_SIZE
; i
++) {
1066 /* Skip over entries which are actually freelist references */
1067 if (skb_entry_is_link(&queue
->tx_skbs
[i
]))
1070 skb
= queue
->tx_skbs
[i
].skb
;
1071 get_page(queue
->grant_tx_page
[i
]);
1072 gnttab_end_foreign_access(queue
->grant_tx_ref
[i
],
1074 (unsigned long)page_address(queue
->grant_tx_page
[i
]));
1075 queue
->grant_tx_page
[i
] = NULL
;
1076 queue
->grant_tx_ref
[i
] = GRANT_INVALID_REF
;
1077 add_id_to_freelist(&queue
->tx_skb_freelist
, queue
->tx_skbs
, i
);
1078 dev_kfree_skb_irq(skb
);
1082 static void xennet_release_rx_bufs(struct netfront_queue
*queue
)
1086 spin_lock_bh(&queue
->rx_lock
);
1088 for (id
= 0; id
< NET_RX_RING_SIZE
; id
++) {
1089 struct sk_buff
*skb
;
1092 skb
= queue
->rx_skbs
[id
];
1096 ref
= queue
->grant_rx_ref
[id
];
1097 if (ref
== GRANT_INVALID_REF
)
1100 page
= skb_frag_page(&skb_shinfo(skb
)->frags
[0]);
1102 /* gnttab_end_foreign_access() needs a page ref until
1103 * foreign access is ended (which may be deferred).
1106 gnttab_end_foreign_access(ref
, 0,
1107 (unsigned long)page_address(page
));
1108 queue
->grant_rx_ref
[id
] = GRANT_INVALID_REF
;
1113 spin_unlock_bh(&queue
->rx_lock
);
1116 static netdev_features_t
xennet_fix_features(struct net_device
*dev
,
1117 netdev_features_t features
)
1119 struct netfront_info
*np
= netdev_priv(dev
);
1122 if (features
& NETIF_F_SG
) {
1123 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
, "feature-sg",
1128 features
&= ~NETIF_F_SG
;
1131 if (features
& NETIF_F_IPV6_CSUM
) {
1132 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1133 "feature-ipv6-csum-offload", "%d", &val
) < 0)
1137 features
&= ~NETIF_F_IPV6_CSUM
;
1140 if (features
& NETIF_F_TSO
) {
1141 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1142 "feature-gso-tcpv4", "%d", &val
) < 0)
1146 features
&= ~NETIF_F_TSO
;
1149 if (features
& NETIF_F_TSO6
) {
1150 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1151 "feature-gso-tcpv6", "%d", &val
) < 0)
1155 features
&= ~NETIF_F_TSO6
;
1161 static int xennet_set_features(struct net_device
*dev
,
1162 netdev_features_t features
)
1164 if (!(features
& NETIF_F_SG
) && dev
->mtu
> ETH_DATA_LEN
) {
1165 netdev_info(dev
, "Reducing MTU because no SG offload");
1166 dev
->mtu
= ETH_DATA_LEN
;
1172 static irqreturn_t
xennet_tx_interrupt(int irq
, void *dev_id
)
1174 struct netfront_queue
*queue
= dev_id
;
1175 unsigned long flags
;
1177 spin_lock_irqsave(&queue
->tx_lock
, flags
);
1178 xennet_tx_buf_gc(queue
);
1179 spin_unlock_irqrestore(&queue
->tx_lock
, flags
);
1184 static irqreturn_t
xennet_rx_interrupt(int irq
, void *dev_id
)
1186 struct netfront_queue
*queue
= dev_id
;
1187 struct net_device
*dev
= queue
->info
->netdev
;
1189 if (likely(netif_carrier_ok(dev
) &&
1190 RING_HAS_UNCONSUMED_RESPONSES(&queue
->rx
)))
1191 napi_schedule(&queue
->napi
);
1196 static irqreturn_t
xennet_interrupt(int irq
, void *dev_id
)
1198 xennet_tx_interrupt(irq
, dev_id
);
1199 xennet_rx_interrupt(irq
, dev_id
);
1203 #ifdef CONFIG_NET_POLL_CONTROLLER
1204 static void xennet_poll_controller(struct net_device
*dev
)
1206 /* Poll each queue */
1207 struct netfront_info
*info
= netdev_priv(dev
);
1208 unsigned int num_queues
= dev
->real_num_tx_queues
;
1210 for (i
= 0; i
< num_queues
; ++i
)
1211 xennet_interrupt(0, &info
->queues
[i
]);
1215 static const struct net_device_ops xennet_netdev_ops
= {
1216 .ndo_open
= xennet_open
,
1217 .ndo_stop
= xennet_close
,
1218 .ndo_start_xmit
= xennet_start_xmit
,
1219 .ndo_change_mtu
= xennet_change_mtu
,
1220 .ndo_get_stats64
= xennet_get_stats64
,
1221 .ndo_set_mac_address
= eth_mac_addr
,
1222 .ndo_validate_addr
= eth_validate_addr
,
1223 .ndo_fix_features
= xennet_fix_features
,
1224 .ndo_set_features
= xennet_set_features
,
1225 .ndo_select_queue
= xennet_select_queue
,
1226 #ifdef CONFIG_NET_POLL_CONTROLLER
1227 .ndo_poll_controller
= xennet_poll_controller
,
1231 static void xennet_free_netdev(struct net_device
*netdev
)
1233 struct netfront_info
*np
= netdev_priv(netdev
);
1235 free_percpu(np
->rx_stats
);
1236 free_percpu(np
->tx_stats
);
1237 free_netdev(netdev
);
1240 static struct net_device
*xennet_create_dev(struct xenbus_device
*dev
)
1243 struct net_device
*netdev
;
1244 struct netfront_info
*np
;
1246 netdev
= alloc_etherdev_mq(sizeof(struct netfront_info
), xennet_max_queues
);
1248 return ERR_PTR(-ENOMEM
);
1250 np
= netdev_priv(netdev
);
1253 /* No need to use rtnl_lock() before the call below as it
1254 * happens before register_netdev().
1256 netif_set_real_num_tx_queues(netdev
, 0);
1260 np
->rx_stats
= netdev_alloc_pcpu_stats(struct netfront_stats
);
1261 if (np
->rx_stats
== NULL
)
1263 np
->tx_stats
= netdev_alloc_pcpu_stats(struct netfront_stats
);
1264 if (np
->tx_stats
== NULL
)
1267 netdev
->netdev_ops
= &xennet_netdev_ops
;
1269 netdev
->features
= NETIF_F_IP_CSUM
| NETIF_F_RXCSUM
|
1271 netdev
->hw_features
= NETIF_F_SG
|
1273 NETIF_F_TSO
| NETIF_F_TSO6
;
1276 * Assume that all hw features are available for now. This set
1277 * will be adjusted by the call to netdev_update_features() in
1278 * xennet_connect() which is the earliest point where we can
1279 * negotiate with the backend regarding supported features.
1281 netdev
->features
|= netdev
->hw_features
;
1283 netdev
->ethtool_ops
= &xennet_ethtool_ops
;
1284 SET_NETDEV_DEV(netdev
, &dev
->dev
);
1286 netif_set_gso_max_size(netdev
, XEN_NETIF_MAX_TX_SIZE
- MAX_TCP_HEADER
);
1288 np
->netdev
= netdev
;
1290 netif_carrier_off(netdev
);
1295 xennet_free_netdev(netdev
);
1296 return ERR_PTR(err
);
1300 * Entry point to this code when a new device is created. Allocate the basic
1301 * structures and the ring buffers for communication with the backend, and
1302 * inform the backend of the appropriate details for those.
1304 static int netfront_probe(struct xenbus_device
*dev
,
1305 const struct xenbus_device_id
*id
)
1308 struct net_device
*netdev
;
1309 struct netfront_info
*info
;
1311 netdev
= xennet_create_dev(dev
);
1312 if (IS_ERR(netdev
)) {
1313 err
= PTR_ERR(netdev
);
1314 xenbus_dev_fatal(dev
, err
, "creating netdev");
1318 info
= netdev_priv(netdev
);
1319 dev_set_drvdata(&dev
->dev
, info
);
1321 err
= register_netdev(info
->netdev
);
1323 pr_warn("%s: register_netdev err=%d\n", __func__
, err
);
1327 err
= xennet_sysfs_addif(info
->netdev
);
1329 unregister_netdev(info
->netdev
);
1330 pr_warn("%s: add sysfs failed err=%d\n", __func__
, err
);
1337 xennet_free_netdev(netdev
);
1338 dev_set_drvdata(&dev
->dev
, NULL
);
1342 static void xennet_end_access(int ref
, void *page
)
1344 /* This frees the page as a side-effect */
1345 if (ref
!= GRANT_INVALID_REF
)
1346 gnttab_end_foreign_access(ref
, 0, (unsigned long)page
);
1349 static void xennet_disconnect_backend(struct netfront_info
*info
)
1352 unsigned int num_queues
= info
->netdev
->real_num_tx_queues
;
1354 netif_carrier_off(info
->netdev
);
1356 for (i
= 0; i
< num_queues
; ++i
) {
1357 struct netfront_queue
*queue
= &info
->queues
[i
];
1359 if (queue
->tx_irq
&& (queue
->tx_irq
== queue
->rx_irq
))
1360 unbind_from_irqhandler(queue
->tx_irq
, queue
);
1361 if (queue
->tx_irq
&& (queue
->tx_irq
!= queue
->rx_irq
)) {
1362 unbind_from_irqhandler(queue
->tx_irq
, queue
);
1363 unbind_from_irqhandler(queue
->rx_irq
, queue
);
1365 queue
->tx_evtchn
= queue
->rx_evtchn
= 0;
1366 queue
->tx_irq
= queue
->rx_irq
= 0;
1368 napi_synchronize(&queue
->napi
);
1370 xennet_release_tx_bufs(queue
);
1371 xennet_release_rx_bufs(queue
);
1372 gnttab_free_grant_references(queue
->gref_tx_head
);
1373 gnttab_free_grant_references(queue
->gref_rx_head
);
1375 /* End access and free the pages */
1376 xennet_end_access(queue
->tx_ring_ref
, queue
->tx
.sring
);
1377 xennet_end_access(queue
->rx_ring_ref
, queue
->rx
.sring
);
1379 queue
->tx_ring_ref
= GRANT_INVALID_REF
;
1380 queue
->rx_ring_ref
= GRANT_INVALID_REF
;
1381 queue
->tx
.sring
= NULL
;
1382 queue
->rx
.sring
= NULL
;
1387 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1388 * driver restart. We tear down our netif structure and recreate it, but
1389 * leave the device-layer structures intact so that this is transparent to the
1390 * rest of the kernel.
1392 static int netfront_resume(struct xenbus_device
*dev
)
1394 struct netfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1396 dev_dbg(&dev
->dev
, "%s\n", dev
->nodename
);
1398 xennet_disconnect_backend(info
);
1402 static int xen_net_read_mac(struct xenbus_device
*dev
, u8 mac
[])
1404 char *s
, *e
, *macstr
;
1407 macstr
= s
= xenbus_read(XBT_NIL
, dev
->nodename
, "mac", NULL
);
1409 return PTR_ERR(macstr
);
1411 for (i
= 0; i
< ETH_ALEN
; i
++) {
1412 mac
[i
] = simple_strtoul(s
, &e
, 16);
1413 if ((s
== e
) || (*e
!= ((i
== ETH_ALEN
-1) ? '\0' : ':'))) {
1424 static int setup_netfront_single(struct netfront_queue
*queue
)
1428 err
= xenbus_alloc_evtchn(queue
->info
->xbdev
, &queue
->tx_evtchn
);
1432 err
= bind_evtchn_to_irqhandler(queue
->tx_evtchn
,
1434 0, queue
->info
->netdev
->name
, queue
);
1437 queue
->rx_evtchn
= queue
->tx_evtchn
;
1438 queue
->rx_irq
= queue
->tx_irq
= err
;
1443 xenbus_free_evtchn(queue
->info
->xbdev
, queue
->tx_evtchn
);
1444 queue
->tx_evtchn
= 0;
1449 static int setup_netfront_split(struct netfront_queue
*queue
)
1453 err
= xenbus_alloc_evtchn(queue
->info
->xbdev
, &queue
->tx_evtchn
);
1456 err
= xenbus_alloc_evtchn(queue
->info
->xbdev
, &queue
->rx_evtchn
);
1458 goto alloc_rx_evtchn_fail
;
1460 snprintf(queue
->tx_irq_name
, sizeof(queue
->tx_irq_name
),
1461 "%s-tx", queue
->name
);
1462 err
= bind_evtchn_to_irqhandler(queue
->tx_evtchn
,
1463 xennet_tx_interrupt
,
1464 0, queue
->tx_irq_name
, queue
);
1467 queue
->tx_irq
= err
;
1469 snprintf(queue
->rx_irq_name
, sizeof(queue
->rx_irq_name
),
1470 "%s-rx", queue
->name
);
1471 err
= bind_evtchn_to_irqhandler(queue
->rx_evtchn
,
1472 xennet_rx_interrupt
,
1473 0, queue
->rx_irq_name
, queue
);
1476 queue
->rx_irq
= err
;
1481 unbind_from_irqhandler(queue
->tx_irq
, queue
);
1484 xenbus_free_evtchn(queue
->info
->xbdev
, queue
->rx_evtchn
);
1485 queue
->rx_evtchn
= 0;
1486 alloc_rx_evtchn_fail
:
1487 xenbus_free_evtchn(queue
->info
->xbdev
, queue
->tx_evtchn
);
1488 queue
->tx_evtchn
= 0;
1493 static int setup_netfront(struct xenbus_device
*dev
,
1494 struct netfront_queue
*queue
, unsigned int feature_split_evtchn
)
1496 struct xen_netif_tx_sring
*txs
;
1497 struct xen_netif_rx_sring
*rxs
;
1500 queue
->tx_ring_ref
= GRANT_INVALID_REF
;
1501 queue
->rx_ring_ref
= GRANT_INVALID_REF
;
1502 queue
->rx
.sring
= NULL
;
1503 queue
->tx
.sring
= NULL
;
1505 txs
= (struct xen_netif_tx_sring
*)get_zeroed_page(GFP_NOIO
| __GFP_HIGH
);
1508 xenbus_dev_fatal(dev
, err
, "allocating tx ring page");
1511 SHARED_RING_INIT(txs
);
1512 FRONT_RING_INIT(&queue
->tx
, txs
, PAGE_SIZE
);
1514 err
= xenbus_grant_ring(dev
, virt_to_mfn(txs
));
1516 goto grant_tx_ring_fail
;
1517 queue
->tx_ring_ref
= err
;
1519 rxs
= (struct xen_netif_rx_sring
*)get_zeroed_page(GFP_NOIO
| __GFP_HIGH
);
1522 xenbus_dev_fatal(dev
, err
, "allocating rx ring page");
1523 goto alloc_rx_ring_fail
;
1525 SHARED_RING_INIT(rxs
);
1526 FRONT_RING_INIT(&queue
->rx
, rxs
, PAGE_SIZE
);
1528 err
= xenbus_grant_ring(dev
, virt_to_mfn(rxs
));
1530 goto grant_rx_ring_fail
;
1531 queue
->rx_ring_ref
= err
;
1533 if (feature_split_evtchn
)
1534 err
= setup_netfront_split(queue
);
1535 /* setup single event channel if
1536 * a) feature-split-event-channels == 0
1537 * b) feature-split-event-channels == 1 but failed to setup
1539 if (!feature_split_evtchn
|| (feature_split_evtchn
&& err
))
1540 err
= setup_netfront_single(queue
);
1543 goto alloc_evtchn_fail
;
1547 /* If we fail to setup netfront, it is safe to just revoke access to
1548 * granted pages because backend is not accessing it at this point.
1551 gnttab_end_foreign_access_ref(queue
->rx_ring_ref
, 0);
1553 free_page((unsigned long)rxs
);
1555 gnttab_end_foreign_access_ref(queue
->tx_ring_ref
, 0);
1557 free_page((unsigned long)txs
);
1562 /* Queue-specific initialisation
1563 * This used to be done in xennet_create_dev() but must now
1566 static int xennet_init_queue(struct netfront_queue
*queue
)
1571 spin_lock_init(&queue
->tx_lock
);
1572 spin_lock_init(&queue
->rx_lock
);
1574 init_timer(&queue
->rx_refill_timer
);
1575 queue
->rx_refill_timer
.data
= (unsigned long)queue
;
1576 queue
->rx_refill_timer
.function
= rx_refill_timeout
;
1578 snprintf(queue
->name
, sizeof(queue
->name
), "%s-q%u",
1579 queue
->info
->netdev
->name
, queue
->id
);
1581 /* Initialise tx_skbs as a free chain containing every entry. */
1582 queue
->tx_skb_freelist
= 0;
1583 for (i
= 0; i
< NET_TX_RING_SIZE
; i
++) {
1584 skb_entry_set_link(&queue
->tx_skbs
[i
], i
+1);
1585 queue
->grant_tx_ref
[i
] = GRANT_INVALID_REF
;
1586 queue
->grant_tx_page
[i
] = NULL
;
1589 /* Clear out rx_skbs */
1590 for (i
= 0; i
< NET_RX_RING_SIZE
; i
++) {
1591 queue
->rx_skbs
[i
] = NULL
;
1592 queue
->grant_rx_ref
[i
] = GRANT_INVALID_REF
;
1595 /* A grant for every tx ring slot */
1596 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE
,
1597 &queue
->gref_tx_head
) < 0) {
1598 pr_alert("can't alloc tx grant refs\n");
1603 /* A grant for every rx ring slot */
1604 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE
,
1605 &queue
->gref_rx_head
) < 0) {
1606 pr_alert("can't alloc rx grant refs\n");
1614 gnttab_free_grant_references(queue
->gref_tx_head
);
1619 static int write_queue_xenstore_keys(struct netfront_queue
*queue
,
1620 struct xenbus_transaction
*xbt
, int write_hierarchical
)
1622 /* Write the queue-specific keys into XenStore in the traditional
1623 * way for a single queue, or in a queue subkeys for multiple
1626 struct xenbus_device
*dev
= queue
->info
->xbdev
;
1628 const char *message
;
1632 /* Choose the correct place to write the keys */
1633 if (write_hierarchical
) {
1634 pathsize
= strlen(dev
->nodename
) + 10;
1635 path
= kzalloc(pathsize
, GFP_KERNEL
);
1638 message
= "out of memory while writing ring references";
1641 snprintf(path
, pathsize
, "%s/queue-%u",
1642 dev
->nodename
, queue
->id
);
1644 path
= (char *)dev
->nodename
;
1647 /* Write ring references */
1648 err
= xenbus_printf(*xbt
, path
, "tx-ring-ref", "%u",
1649 queue
->tx_ring_ref
);
1651 message
= "writing tx-ring-ref";
1655 err
= xenbus_printf(*xbt
, path
, "rx-ring-ref", "%u",
1656 queue
->rx_ring_ref
);
1658 message
= "writing rx-ring-ref";
1662 /* Write event channels; taking into account both shared
1663 * and split event channel scenarios.
1665 if (queue
->tx_evtchn
== queue
->rx_evtchn
) {
1666 /* Shared event channel */
1667 err
= xenbus_printf(*xbt
, path
,
1668 "event-channel", "%u", queue
->tx_evtchn
);
1670 message
= "writing event-channel";
1674 /* Split event channels */
1675 err
= xenbus_printf(*xbt
, path
,
1676 "event-channel-tx", "%u", queue
->tx_evtchn
);
1678 message
= "writing event-channel-tx";
1682 err
= xenbus_printf(*xbt
, path
,
1683 "event-channel-rx", "%u", queue
->rx_evtchn
);
1685 message
= "writing event-channel-rx";
1690 if (write_hierarchical
)
1695 if (write_hierarchical
)
1697 xenbus_dev_fatal(dev
, err
, "%s", message
);
1701 static void xennet_destroy_queues(struct netfront_info
*info
)
1707 for (i
= 0; i
< info
->netdev
->real_num_tx_queues
; i
++) {
1708 struct netfront_queue
*queue
= &info
->queues
[i
];
1710 if (netif_running(info
->netdev
))
1711 napi_disable(&queue
->napi
);
1712 netif_napi_del(&queue
->napi
);
1717 kfree(info
->queues
);
1718 info
->queues
= NULL
;
1721 static int xennet_create_queues(struct netfront_info
*info
,
1722 unsigned int num_queues
)
1727 info
->queues
= kcalloc(num_queues
, sizeof(struct netfront_queue
),
1734 for (i
= 0; i
< num_queues
; i
++) {
1735 struct netfront_queue
*queue
= &info
->queues
[i
];
1740 ret
= xennet_init_queue(queue
);
1742 dev_warn(&info
->netdev
->dev
,
1743 "only created %d queues\n", i
);
1748 netif_napi_add(queue
->info
->netdev
, &queue
->napi
,
1750 if (netif_running(info
->netdev
))
1751 napi_enable(&queue
->napi
);
1754 netif_set_real_num_tx_queues(info
->netdev
, num_queues
);
1758 if (num_queues
== 0) {
1759 dev_err(&info
->netdev
->dev
, "no queues\n");
1765 /* Common code used when first setting up, and when resuming. */
1766 static int talk_to_netback(struct xenbus_device
*dev
,
1767 struct netfront_info
*info
)
1769 const char *message
;
1770 struct xenbus_transaction xbt
;
1772 unsigned int feature_split_evtchn
;
1774 unsigned int max_queues
= 0;
1775 struct netfront_queue
*queue
= NULL
;
1776 unsigned int num_queues
= 1;
1778 info
->netdev
->irq
= 0;
1780 /* Check if backend supports multiple queues */
1781 err
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
1782 "multi-queue-max-queues", "%u", &max_queues
);
1785 num_queues
= min(max_queues
, xennet_max_queues
);
1787 /* Check feature-split-event-channels */
1788 err
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
1789 "feature-split-event-channels", "%u",
1790 &feature_split_evtchn
);
1792 feature_split_evtchn
= 0;
1794 /* Read mac addr. */
1795 err
= xen_net_read_mac(dev
, info
->netdev
->dev_addr
);
1797 xenbus_dev_fatal(dev
, err
, "parsing %s/mac", dev
->nodename
);
1802 xennet_destroy_queues(info
);
1804 err
= xennet_create_queues(info
, num_queues
);
1808 /* Create shared ring, alloc event channel -- for each queue */
1809 for (i
= 0; i
< num_queues
; ++i
) {
1810 queue
= &info
->queues
[i
];
1811 err
= setup_netfront(dev
, queue
, feature_split_evtchn
);
1813 /* setup_netfront() will tidy up the current
1814 * queue on error, but we need to clean up
1815 * those already allocated.
1819 netif_set_real_num_tx_queues(info
->netdev
, i
);
1829 err
= xenbus_transaction_start(&xbt
);
1831 xenbus_dev_fatal(dev
, err
, "starting transaction");
1835 if (num_queues
== 1) {
1836 err
= write_queue_xenstore_keys(&info
->queues
[0], &xbt
, 0); /* flat */
1838 goto abort_transaction_no_dev_fatal
;
1840 /* Write the number of queues */
1841 err
= xenbus_printf(xbt
, dev
->nodename
, "multi-queue-num-queues",
1844 message
= "writing multi-queue-num-queues";
1845 goto abort_transaction_no_dev_fatal
;
1848 /* Write the keys for each queue */
1849 for (i
= 0; i
< num_queues
; ++i
) {
1850 queue
= &info
->queues
[i
];
1851 err
= write_queue_xenstore_keys(queue
, &xbt
, 1); /* hierarchical */
1853 goto abort_transaction_no_dev_fatal
;
1857 /* The remaining keys are not queue-specific */
1858 err
= xenbus_printf(xbt
, dev
->nodename
, "request-rx-copy", "%u",
1861 message
= "writing request-rx-copy";
1862 goto abort_transaction
;
1865 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-rx-notify", "%d", 1);
1867 message
= "writing feature-rx-notify";
1868 goto abort_transaction
;
1871 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-sg", "%d", 1);
1873 message
= "writing feature-sg";
1874 goto abort_transaction
;
1877 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-gso-tcpv4", "%d", 1);
1879 message
= "writing feature-gso-tcpv4";
1880 goto abort_transaction
;
1883 err
= xenbus_write(xbt
, dev
->nodename
, "feature-gso-tcpv6", "1");
1885 message
= "writing feature-gso-tcpv6";
1886 goto abort_transaction
;
1889 err
= xenbus_write(xbt
, dev
->nodename
, "feature-ipv6-csum-offload",
1892 message
= "writing feature-ipv6-csum-offload";
1893 goto abort_transaction
;
1896 err
= xenbus_transaction_end(xbt
, 0);
1900 xenbus_dev_fatal(dev
, err
, "completing transaction");
1907 xenbus_dev_fatal(dev
, err
, "%s", message
);
1908 abort_transaction_no_dev_fatal
:
1909 xenbus_transaction_end(xbt
, 1);
1911 xennet_disconnect_backend(info
);
1912 kfree(info
->queues
);
1913 info
->queues
= NULL
;
1915 netif_set_real_num_tx_queues(info
->netdev
, 0);
1921 static int xennet_connect(struct net_device
*dev
)
1923 struct netfront_info
*np
= netdev_priv(dev
);
1924 unsigned int num_queues
= 0;
1926 unsigned int feature_rx_copy
;
1928 struct netfront_queue
*queue
= NULL
;
1930 err
= xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1931 "feature-rx-copy", "%u", &feature_rx_copy
);
1933 feature_rx_copy
= 0;
1935 if (!feature_rx_copy
) {
1937 "backend does not support copying receive path\n");
1941 err
= talk_to_netback(np
->xbdev
, np
);
1945 /* talk_to_netback() sets the correct number of queues */
1946 num_queues
= dev
->real_num_tx_queues
;
1949 netdev_update_features(dev
);
1953 * All public and private state should now be sane. Get
1954 * ready to start sending and receiving packets and give the driver
1955 * domain a kick because we've probably just requeued some
1958 netif_carrier_on(np
->netdev
);
1959 for (j
= 0; j
< num_queues
; ++j
) {
1960 queue
= &np
->queues
[j
];
1962 notify_remote_via_irq(queue
->tx_irq
);
1963 if (queue
->tx_irq
!= queue
->rx_irq
)
1964 notify_remote_via_irq(queue
->rx_irq
);
1966 spin_lock_irq(&queue
->tx_lock
);
1967 xennet_tx_buf_gc(queue
);
1968 spin_unlock_irq(&queue
->tx_lock
);
1970 spin_lock_bh(&queue
->rx_lock
);
1971 xennet_alloc_rx_buffers(queue
);
1972 spin_unlock_bh(&queue
->rx_lock
);
1979 * Callback received when the backend's state changes.
1981 static void netback_changed(struct xenbus_device
*dev
,
1982 enum xenbus_state backend_state
)
1984 struct netfront_info
*np
= dev_get_drvdata(&dev
->dev
);
1985 struct net_device
*netdev
= np
->netdev
;
1987 dev_dbg(&dev
->dev
, "%s\n", xenbus_strstate(backend_state
));
1989 switch (backend_state
) {
1990 case XenbusStateInitialising
:
1991 case XenbusStateInitialised
:
1992 case XenbusStateReconfiguring
:
1993 case XenbusStateReconfigured
:
1994 case XenbusStateUnknown
:
1997 case XenbusStateInitWait
:
1998 if (dev
->state
!= XenbusStateInitialising
)
2000 if (xennet_connect(netdev
) != 0)
2002 xenbus_switch_state(dev
, XenbusStateConnected
);
2005 case XenbusStateConnected
:
2006 netdev_notify_peers(netdev
);
2009 case XenbusStateClosed
:
2010 if (dev
->state
== XenbusStateClosed
)
2012 /* Missed the backend's CLOSING state -- fallthrough */
2013 case XenbusStateClosing
:
2014 xenbus_frontend_closed(dev
);
2019 static const struct xennet_stat
{
2020 char name
[ETH_GSTRING_LEN
];
2022 } xennet_stats
[] = {
2024 "rx_gso_checksum_fixup",
2025 offsetof(struct netfront_info
, rx_gso_checksum_fixup
)
2029 static int xennet_get_sset_count(struct net_device
*dev
, int string_set
)
2031 switch (string_set
) {
2033 return ARRAY_SIZE(xennet_stats
);
2039 static void xennet_get_ethtool_stats(struct net_device
*dev
,
2040 struct ethtool_stats
*stats
, u64
* data
)
2042 void *np
= netdev_priv(dev
);
2045 for (i
= 0; i
< ARRAY_SIZE(xennet_stats
); i
++)
2046 data
[i
] = atomic_read((atomic_t
*)(np
+ xennet_stats
[i
].offset
));
2049 static void xennet_get_strings(struct net_device
*dev
, u32 stringset
, u8
* data
)
2053 switch (stringset
) {
2055 for (i
= 0; i
< ARRAY_SIZE(xennet_stats
); i
++)
2056 memcpy(data
+ i
* ETH_GSTRING_LEN
,
2057 xennet_stats
[i
].name
, ETH_GSTRING_LEN
);
2062 static const struct ethtool_ops xennet_ethtool_ops
=
2064 .get_link
= ethtool_op_get_link
,
2066 .get_sset_count
= xennet_get_sset_count
,
2067 .get_ethtool_stats
= xennet_get_ethtool_stats
,
2068 .get_strings
= xennet_get_strings
,
2072 static ssize_t
show_rxbuf(struct device
*dev
,
2073 struct device_attribute
*attr
, char *buf
)
2075 return sprintf(buf
, "%lu\n", NET_RX_RING_SIZE
);
2078 static ssize_t
store_rxbuf(struct device
*dev
,
2079 struct device_attribute
*attr
,
2080 const char *buf
, size_t len
)
2083 unsigned long target
;
2085 if (!capable(CAP_NET_ADMIN
))
2088 target
= simple_strtoul(buf
, &endp
, 0);
2092 /* rxbuf_min and rxbuf_max are no longer configurable. */
2097 static struct device_attribute xennet_attrs
[] = {
2098 __ATTR(rxbuf_min
, S_IRUGO
|S_IWUSR
, show_rxbuf
, store_rxbuf
),
2099 __ATTR(rxbuf_max
, S_IRUGO
|S_IWUSR
, show_rxbuf
, store_rxbuf
),
2100 __ATTR(rxbuf_cur
, S_IRUGO
, show_rxbuf
, NULL
),
2103 static int xennet_sysfs_addif(struct net_device
*netdev
)
2108 for (i
= 0; i
< ARRAY_SIZE(xennet_attrs
); i
++) {
2109 err
= device_create_file(&netdev
->dev
,
2118 device_remove_file(&netdev
->dev
, &xennet_attrs
[i
]);
2122 static void xennet_sysfs_delif(struct net_device
*netdev
)
2126 for (i
= 0; i
< ARRAY_SIZE(xennet_attrs
); i
++)
2127 device_remove_file(&netdev
->dev
, &xennet_attrs
[i
]);
2130 #endif /* CONFIG_SYSFS */
2132 static int xennet_remove(struct xenbus_device
*dev
)
2134 struct netfront_info
*info
= dev_get_drvdata(&dev
->dev
);
2135 unsigned int num_queues
= info
->netdev
->real_num_tx_queues
;
2136 struct netfront_queue
*queue
= NULL
;
2139 dev_dbg(&dev
->dev
, "%s\n", dev
->nodename
);
2141 xennet_disconnect_backend(info
);
2143 xennet_sysfs_delif(info
->netdev
);
2145 unregister_netdev(info
->netdev
);
2147 for (i
= 0; i
< num_queues
; ++i
) {
2148 queue
= &info
->queues
[i
];
2149 del_timer_sync(&queue
->rx_refill_timer
);
2153 kfree(info
->queues
);
2154 info
->queues
= NULL
;
2157 xennet_free_netdev(info
->netdev
);
2162 static const struct xenbus_device_id netfront_ids
[] = {
2167 static struct xenbus_driver netfront_driver
= {
2168 .ids
= netfront_ids
,
2169 .probe
= netfront_probe
,
2170 .remove
= xennet_remove
,
2171 .resume
= netfront_resume
,
2172 .otherend_changed
= netback_changed
,
2175 static int __init
netif_init(void)
2180 if (!xen_has_pv_nic_devices())
2183 pr_info("Initialising Xen virtual ethernet driver\n");
2185 /* Allow as many queues as there are CPUs, by default */
2186 xennet_max_queues
= num_online_cpus();
2188 return xenbus_register_frontend(&netfront_driver
);
2190 module_init(netif_init
);
2193 static void __exit
netif_exit(void)
2195 xenbus_unregister_driver(&netfront_driver
);
2197 module_exit(netif_exit
);
2199 MODULE_DESCRIPTION("Xen virtual network device frontend");
2200 MODULE_LICENSE("GPL");
2201 MODULE_ALIAS("xen:vif");
2202 MODULE_ALIAS("xennet");