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 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/ethtool.h>
38 #include <linux/if_ether.h>
39 #include <linux/tcp.h>
40 #include <linux/udp.h>
41 #include <linux/moduleparam.h>
43 #include <linux/slab.h>
47 #include <xen/xenbus.h>
48 #include <xen/events.h>
50 #include <xen/grant_table.h>
52 #include <xen/interface/io/netif.h>
53 #include <xen/interface/memory.h>
54 #include <xen/interface/grant_table.h>
56 static const struct ethtool_ops xennet_ethtool_ops
;
63 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
65 #define RX_COPY_THRESHOLD 256
67 #define GRANT_INVALID_REF 0
69 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)
70 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
71 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
73 struct netfront_info
{
74 struct list_head list
;
75 struct net_device
*netdev
;
77 struct napi_struct napi
;
80 struct xenbus_device
*xbdev
;
83 struct xen_netif_tx_front_ring tx
;
87 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
88 * are linked from tx_skb_freelist through skb_entry.link.
90 * NB. Freelist index entries are always going to be less than
91 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
92 * greater than PAGE_OFFSET: we use this property to distinguish
98 } tx_skbs
[NET_TX_RING_SIZE
];
99 grant_ref_t gref_tx_head
;
100 grant_ref_t grant_tx_ref
[NET_TX_RING_SIZE
];
101 unsigned tx_skb_freelist
;
103 spinlock_t rx_lock ____cacheline_aligned_in_smp
;
104 struct xen_netif_rx_front_ring rx
;
107 /* Receive-ring batched refills. */
108 #define RX_MIN_TARGET 8
109 #define RX_DFL_MIN_TARGET 64
110 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
111 unsigned rx_min_target
, rx_max_target
, rx_target
;
112 struct sk_buff_head rx_batch
;
114 struct timer_list rx_refill_timer
;
116 struct sk_buff
*rx_skbs
[NET_RX_RING_SIZE
];
117 grant_ref_t gref_rx_head
;
118 grant_ref_t grant_rx_ref
[NET_RX_RING_SIZE
];
120 unsigned long rx_pfn_array
[NET_RX_RING_SIZE
];
121 struct multicall_entry rx_mcl
[NET_RX_RING_SIZE
+1];
122 struct mmu_update rx_mmu
[NET_RX_RING_SIZE
];
125 unsigned long rx_gso_checksum_fixup
;
128 struct netfront_rx_info
{
129 struct xen_netif_rx_response rx
;
130 struct xen_netif_extra_info extras
[XEN_NETIF_EXTRA_TYPE_MAX
- 1];
133 static void skb_entry_set_link(union skb_entry
*list
, unsigned short id
)
138 static int skb_entry_is_link(const union skb_entry
*list
)
140 BUILD_BUG_ON(sizeof(list
->skb
) != sizeof(list
->link
));
141 return (unsigned long)list
->skb
< PAGE_OFFSET
;
145 * Access macros for acquiring freeing slots in tx_skbs[].
148 static void add_id_to_freelist(unsigned *head
, union skb_entry
*list
,
151 skb_entry_set_link(&list
[id
], *head
);
155 static unsigned short get_id_from_freelist(unsigned *head
,
156 union skb_entry
*list
)
158 unsigned int id
= *head
;
159 *head
= list
[id
].link
;
163 static int xennet_rxidx(RING_IDX idx
)
165 return idx
& (NET_RX_RING_SIZE
- 1);
168 static struct sk_buff
*xennet_get_rx_skb(struct netfront_info
*np
,
171 int i
= xennet_rxidx(ri
);
172 struct sk_buff
*skb
= np
->rx_skbs
[i
];
173 np
->rx_skbs
[i
] = NULL
;
177 static grant_ref_t
xennet_get_rx_ref(struct netfront_info
*np
,
180 int i
= xennet_rxidx(ri
);
181 grant_ref_t ref
= np
->grant_rx_ref
[i
];
182 np
->grant_rx_ref
[i
] = GRANT_INVALID_REF
;
187 static int xennet_sysfs_addif(struct net_device
*netdev
);
188 static void xennet_sysfs_delif(struct net_device
*netdev
);
189 #else /* !CONFIG_SYSFS */
190 #define xennet_sysfs_addif(dev) (0)
191 #define xennet_sysfs_delif(dev) do { } while (0)
194 static int xennet_can_sg(struct net_device
*dev
)
196 return dev
->features
& NETIF_F_SG
;
200 static void rx_refill_timeout(unsigned long data
)
202 struct net_device
*dev
= (struct net_device
*)data
;
203 struct netfront_info
*np
= netdev_priv(dev
);
204 napi_schedule(&np
->napi
);
207 static int netfront_tx_slot_available(struct netfront_info
*np
)
209 return (np
->tx
.req_prod_pvt
- np
->tx
.rsp_cons
) <
210 (TX_MAX_TARGET
- MAX_SKB_FRAGS
- 2);
213 static void xennet_maybe_wake_tx(struct net_device
*dev
)
215 struct netfront_info
*np
= netdev_priv(dev
);
217 if (unlikely(netif_queue_stopped(dev
)) &&
218 netfront_tx_slot_available(np
) &&
219 likely(netif_running(dev
)))
220 netif_wake_queue(dev
);
223 static void xennet_alloc_rx_buffers(struct net_device
*dev
)
226 struct netfront_info
*np
= netdev_priv(dev
);
229 int i
, batch_target
, notify
;
230 RING_IDX req_prod
= np
->rx
.req_prod_pvt
;
234 struct xen_netif_rx_request
*req
;
236 if (unlikely(!netif_carrier_ok(dev
)))
240 * Allocate skbuffs greedily, even though we batch updates to the
241 * receive ring. This creates a less bursty demand on the memory
242 * allocator, so should reduce the chance of failed allocation requests
243 * both for ourself and for other kernel subsystems.
245 batch_target
= np
->rx_target
- (req_prod
- np
->rx
.rsp_cons
);
246 for (i
= skb_queue_len(&np
->rx_batch
); i
< batch_target
; i
++) {
247 skb
= __netdev_alloc_skb(dev
, RX_COPY_THRESHOLD
+ NET_IP_ALIGN
,
248 GFP_ATOMIC
| __GFP_NOWARN
);
252 /* Align ip header to a 16 bytes boundary */
253 skb_reserve(skb
, NET_IP_ALIGN
);
255 page
= alloc_page(GFP_ATOMIC
| __GFP_NOWARN
);
259 /* Any skbuffs queued for refill? Force them out. */
262 /* Could not allocate any skbuffs. Try again later. */
263 mod_timer(&np
->rx_refill_timer
,
268 skb_shinfo(skb
)->frags
[0].page
= page
;
269 skb_shinfo(skb
)->nr_frags
= 1;
270 __skb_queue_tail(&np
->rx_batch
, skb
);
273 /* Is the batch large enough to be worthwhile? */
274 if (i
< (np
->rx_target
/2)) {
275 if (req_prod
> np
->rx
.sring
->req_prod
)
280 /* Adjust our fill target if we risked running out of buffers. */
281 if (((req_prod
- np
->rx
.sring
->rsp_prod
) < (np
->rx_target
/ 4)) &&
282 ((np
->rx_target
*= 2) > np
->rx_max_target
))
283 np
->rx_target
= np
->rx_max_target
;
287 skb
= __skb_dequeue(&np
->rx_batch
);
293 id
= xennet_rxidx(req_prod
+ i
);
295 BUG_ON(np
->rx_skbs
[id
]);
296 np
->rx_skbs
[id
] = skb
;
298 ref
= gnttab_claim_grant_reference(&np
->gref_rx_head
);
299 BUG_ON((signed short)ref
< 0);
300 np
->grant_rx_ref
[id
] = ref
;
302 pfn
= page_to_pfn(skb_shinfo(skb
)->frags
[0].page
);
303 vaddr
= page_address(skb_shinfo(skb
)->frags
[0].page
);
305 req
= RING_GET_REQUEST(&np
->rx
, req_prod
+ i
);
306 gnttab_grant_foreign_access_ref(ref
,
307 np
->xbdev
->otherend_id
,
315 wmb(); /* barrier so backend seens requests */
317 /* Above is a suitable barrier to ensure backend will see requests. */
318 np
->rx
.req_prod_pvt
= req_prod
+ i
;
320 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np
->rx
, notify
);
322 notify_remote_via_irq(np
->netdev
->irq
);
325 static int xennet_open(struct net_device
*dev
)
327 struct netfront_info
*np
= netdev_priv(dev
);
329 napi_enable(&np
->napi
);
331 spin_lock_bh(&np
->rx_lock
);
332 if (netif_carrier_ok(dev
)) {
333 xennet_alloc_rx_buffers(dev
);
334 np
->rx
.sring
->rsp_event
= np
->rx
.rsp_cons
+ 1;
335 if (RING_HAS_UNCONSUMED_RESPONSES(&np
->rx
))
336 napi_schedule(&np
->napi
);
338 spin_unlock_bh(&np
->rx_lock
);
340 netif_start_queue(dev
);
345 static void xennet_tx_buf_gc(struct net_device
*dev
)
349 struct netfront_info
*np
= netdev_priv(dev
);
352 BUG_ON(!netif_carrier_ok(dev
));
355 prod
= np
->tx
.sring
->rsp_prod
;
356 rmb(); /* Ensure we see responses up to 'rp'. */
358 for (cons
= np
->tx
.rsp_cons
; cons
!= prod
; cons
++) {
359 struct xen_netif_tx_response
*txrsp
;
361 txrsp
= RING_GET_RESPONSE(&np
->tx
, cons
);
362 if (txrsp
->status
== XEN_NETIF_RSP_NULL
)
366 skb
= np
->tx_skbs
[id
].skb
;
367 if (unlikely(gnttab_query_foreign_access(
368 np
->grant_tx_ref
[id
]) != 0)) {
369 printk(KERN_ALERT
"xennet_tx_buf_gc: warning "
370 "-- grant still in use by backend "
374 gnttab_end_foreign_access_ref(
375 np
->grant_tx_ref
[id
], GNTMAP_readonly
);
376 gnttab_release_grant_reference(
377 &np
->gref_tx_head
, np
->grant_tx_ref
[id
]);
378 np
->grant_tx_ref
[id
] = GRANT_INVALID_REF
;
379 add_id_to_freelist(&np
->tx_skb_freelist
, np
->tx_skbs
, id
);
380 dev_kfree_skb_irq(skb
);
383 np
->tx
.rsp_cons
= prod
;
386 * Set a new event, then check for race with update of tx_cons.
387 * Note that it is essential to schedule a callback, no matter
388 * how few buffers are pending. Even if there is space in the
389 * transmit ring, higher layers may be blocked because too much
390 * data is outstanding: in such cases notification from Xen is
391 * likely to be the only kick that we'll get.
393 np
->tx
.sring
->rsp_event
=
394 prod
+ ((np
->tx
.sring
->req_prod
- prod
) >> 1) + 1;
395 mb(); /* update shared area */
396 } while ((cons
== prod
) && (prod
!= np
->tx
.sring
->rsp_prod
));
398 xennet_maybe_wake_tx(dev
);
401 static void xennet_make_frags(struct sk_buff
*skb
, struct net_device
*dev
,
402 struct xen_netif_tx_request
*tx
)
404 struct netfront_info
*np
= netdev_priv(dev
);
405 char *data
= skb
->data
;
407 RING_IDX prod
= np
->tx
.req_prod_pvt
;
408 int frags
= skb_shinfo(skb
)->nr_frags
;
409 unsigned int offset
= offset_in_page(data
);
410 unsigned int len
= skb_headlen(skb
);
415 /* While the header overlaps a page boundary (including being
416 larger than a page), split it it into page-sized chunks. */
417 while (len
> PAGE_SIZE
- offset
) {
418 tx
->size
= PAGE_SIZE
- offset
;
419 tx
->flags
|= XEN_NETTXF_more_data
;
424 id
= get_id_from_freelist(&np
->tx_skb_freelist
, np
->tx_skbs
);
425 np
->tx_skbs
[id
].skb
= skb_get(skb
);
426 tx
= RING_GET_REQUEST(&np
->tx
, prod
++);
428 ref
= gnttab_claim_grant_reference(&np
->gref_tx_head
);
429 BUG_ON((signed short)ref
< 0);
431 mfn
= virt_to_mfn(data
);
432 gnttab_grant_foreign_access_ref(ref
, np
->xbdev
->otherend_id
,
433 mfn
, GNTMAP_readonly
);
435 tx
->gref
= np
->grant_tx_ref
[id
] = ref
;
441 /* Grant backend access to each skb fragment page. */
442 for (i
= 0; i
< frags
; i
++) {
443 skb_frag_t
*frag
= skb_shinfo(skb
)->frags
+ i
;
445 tx
->flags
|= XEN_NETTXF_more_data
;
447 id
= get_id_from_freelist(&np
->tx_skb_freelist
, np
->tx_skbs
);
448 np
->tx_skbs
[id
].skb
= skb_get(skb
);
449 tx
= RING_GET_REQUEST(&np
->tx
, prod
++);
451 ref
= gnttab_claim_grant_reference(&np
->gref_tx_head
);
452 BUG_ON((signed short)ref
< 0);
454 mfn
= pfn_to_mfn(page_to_pfn(frag
->page
));
455 gnttab_grant_foreign_access_ref(ref
, np
->xbdev
->otherend_id
,
456 mfn
, GNTMAP_readonly
);
458 tx
->gref
= np
->grant_tx_ref
[id
] = ref
;
459 tx
->offset
= frag
->page_offset
;
460 tx
->size
= frag
->size
;
464 np
->tx
.req_prod_pvt
= prod
;
467 static int xennet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
470 struct netfront_info
*np
= netdev_priv(dev
);
471 struct xen_netif_tx_request
*tx
;
472 struct xen_netif_extra_info
*extra
;
473 char *data
= skb
->data
;
478 int frags
= skb_shinfo(skb
)->nr_frags
;
479 unsigned int offset
= offset_in_page(data
);
480 unsigned int len
= skb_headlen(skb
);
482 frags
+= DIV_ROUND_UP(offset
+ len
, PAGE_SIZE
);
483 if (unlikely(frags
> MAX_SKB_FRAGS
+ 1)) {
484 printk(KERN_ALERT
"xennet: skb rides the rocket: %d frags\n",
490 spin_lock_irq(&np
->tx_lock
);
492 if (unlikely(!netif_carrier_ok(dev
) ||
493 (frags
> 1 && !xennet_can_sg(dev
)) ||
494 netif_needs_gso(skb
, netif_skb_features(skb
)))) {
495 spin_unlock_irq(&np
->tx_lock
);
499 i
= np
->tx
.req_prod_pvt
;
501 id
= get_id_from_freelist(&np
->tx_skb_freelist
, np
->tx_skbs
);
502 np
->tx_skbs
[id
].skb
= skb
;
504 tx
= RING_GET_REQUEST(&np
->tx
, i
);
507 ref
= gnttab_claim_grant_reference(&np
->gref_tx_head
);
508 BUG_ON((signed short)ref
< 0);
509 mfn
= virt_to_mfn(data
);
510 gnttab_grant_foreign_access_ref(
511 ref
, np
->xbdev
->otherend_id
, mfn
, GNTMAP_readonly
);
512 tx
->gref
= np
->grant_tx_ref
[id
] = ref
;
518 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
520 tx
->flags
|= XEN_NETTXF_csum_blank
| XEN_NETTXF_data_validated
;
521 else if (skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
522 /* remote but checksummed. */
523 tx
->flags
|= XEN_NETTXF_data_validated
;
525 if (skb_shinfo(skb
)->gso_size
) {
526 struct xen_netif_extra_info
*gso
;
528 gso
= (struct xen_netif_extra_info
*)
529 RING_GET_REQUEST(&np
->tx
, ++i
);
532 extra
->flags
|= XEN_NETIF_EXTRA_FLAG_MORE
;
534 tx
->flags
|= XEN_NETTXF_extra_info
;
536 gso
->u
.gso
.size
= skb_shinfo(skb
)->gso_size
;
537 gso
->u
.gso
.type
= XEN_NETIF_GSO_TYPE_TCPV4
;
539 gso
->u
.gso
.features
= 0;
541 gso
->type
= XEN_NETIF_EXTRA_TYPE_GSO
;
546 np
->tx
.req_prod_pvt
= i
+ 1;
548 xennet_make_frags(skb
, dev
, tx
);
551 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np
->tx
, notify
);
553 notify_remote_via_irq(np
->netdev
->irq
);
555 dev
->stats
.tx_bytes
+= skb
->len
;
556 dev
->stats
.tx_packets
++;
558 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
559 xennet_tx_buf_gc(dev
);
561 if (!netfront_tx_slot_available(np
))
562 netif_stop_queue(dev
);
564 spin_unlock_irq(&np
->tx_lock
);
569 dev
->stats
.tx_dropped
++;
574 static int xennet_close(struct net_device
*dev
)
576 struct netfront_info
*np
= netdev_priv(dev
);
577 netif_stop_queue(np
->netdev
);
578 napi_disable(&np
->napi
);
582 static void xennet_move_rx_slot(struct netfront_info
*np
, struct sk_buff
*skb
,
585 int new = xennet_rxidx(np
->rx
.req_prod_pvt
);
587 BUG_ON(np
->rx_skbs
[new]);
588 np
->rx_skbs
[new] = skb
;
589 np
->grant_rx_ref
[new] = ref
;
590 RING_GET_REQUEST(&np
->rx
, np
->rx
.req_prod_pvt
)->id
= new;
591 RING_GET_REQUEST(&np
->rx
, np
->rx
.req_prod_pvt
)->gref
= ref
;
592 np
->rx
.req_prod_pvt
++;
595 static int xennet_get_extras(struct netfront_info
*np
,
596 struct xen_netif_extra_info
*extras
,
600 struct xen_netif_extra_info
*extra
;
601 struct device
*dev
= &np
->netdev
->dev
;
602 RING_IDX cons
= np
->rx
.rsp_cons
;
609 if (unlikely(cons
+ 1 == rp
)) {
611 dev_warn(dev
, "Missing extra info\n");
616 extra
= (struct xen_netif_extra_info
*)
617 RING_GET_RESPONSE(&np
->rx
, ++cons
);
619 if (unlikely(!extra
->type
||
620 extra
->type
>= XEN_NETIF_EXTRA_TYPE_MAX
)) {
622 dev_warn(dev
, "Invalid extra type: %d\n",
626 memcpy(&extras
[extra
->type
- 1], extra
,
630 skb
= xennet_get_rx_skb(np
, cons
);
631 ref
= xennet_get_rx_ref(np
, cons
);
632 xennet_move_rx_slot(np
, skb
, ref
);
633 } while (extra
->flags
& XEN_NETIF_EXTRA_FLAG_MORE
);
635 np
->rx
.rsp_cons
= cons
;
639 static int xennet_get_responses(struct netfront_info
*np
,
640 struct netfront_rx_info
*rinfo
, RING_IDX rp
,
641 struct sk_buff_head
*list
)
643 struct xen_netif_rx_response
*rx
= &rinfo
->rx
;
644 struct xen_netif_extra_info
*extras
= rinfo
->extras
;
645 struct device
*dev
= &np
->netdev
->dev
;
646 RING_IDX cons
= np
->rx
.rsp_cons
;
647 struct sk_buff
*skb
= xennet_get_rx_skb(np
, cons
);
648 grant_ref_t ref
= xennet_get_rx_ref(np
, cons
);
649 int max
= MAX_SKB_FRAGS
+ (rx
->status
<= RX_COPY_THRESHOLD
);
654 if (rx
->flags
& XEN_NETRXF_extra_info
) {
655 err
= xennet_get_extras(np
, extras
, rp
);
656 cons
= np
->rx
.rsp_cons
;
660 if (unlikely(rx
->status
< 0 ||
661 rx
->offset
+ rx
->status
> PAGE_SIZE
)) {
663 dev_warn(dev
, "rx->offset: %x, size: %u\n",
664 rx
->offset
, rx
->status
);
665 xennet_move_rx_slot(np
, skb
, ref
);
671 * This definitely indicates a bug, either in this driver or in
672 * the backend driver. In future this should flag the bad
673 * situation to the system controller to reboot the backed.
675 if (ref
== GRANT_INVALID_REF
) {
677 dev_warn(dev
, "Bad rx response id %d.\n",
683 ret
= gnttab_end_foreign_access_ref(ref
, 0);
686 gnttab_release_grant_reference(&np
->gref_rx_head
, ref
);
688 __skb_queue_tail(list
, skb
);
691 if (!(rx
->flags
& XEN_NETRXF_more_data
))
694 if (cons
+ frags
== rp
) {
696 dev_warn(dev
, "Need more frags\n");
701 rx
= RING_GET_RESPONSE(&np
->rx
, cons
+ frags
);
702 skb
= xennet_get_rx_skb(np
, cons
+ frags
);
703 ref
= xennet_get_rx_ref(np
, cons
+ frags
);
707 if (unlikely(frags
> max
)) {
709 dev_warn(dev
, "Too many frags\n");
714 np
->rx
.rsp_cons
= cons
+ frags
;
719 static int xennet_set_skb_gso(struct sk_buff
*skb
,
720 struct xen_netif_extra_info
*gso
)
722 if (!gso
->u
.gso
.size
) {
724 printk(KERN_WARNING
"GSO size must not be zero.\n");
728 /* Currently only TCPv4 S.O. is supported. */
729 if (gso
->u
.gso
.type
!= XEN_NETIF_GSO_TYPE_TCPV4
) {
731 printk(KERN_WARNING
"Bad GSO type %d.\n", gso
->u
.gso
.type
);
735 skb_shinfo(skb
)->gso_size
= gso
->u
.gso
.size
;
736 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
738 /* Header must be checked, and gso_segs computed. */
739 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
740 skb_shinfo(skb
)->gso_segs
= 0;
745 static RING_IDX
xennet_fill_frags(struct netfront_info
*np
,
747 struct sk_buff_head
*list
)
749 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
750 int nr_frags
= shinfo
->nr_frags
;
751 RING_IDX cons
= np
->rx
.rsp_cons
;
752 skb_frag_t
*frag
= shinfo
->frags
+ nr_frags
;
753 struct sk_buff
*nskb
;
755 while ((nskb
= __skb_dequeue(list
))) {
756 struct xen_netif_rx_response
*rx
=
757 RING_GET_RESPONSE(&np
->rx
, ++cons
);
759 frag
->page
= skb_shinfo(nskb
)->frags
[0].page
;
760 frag
->page_offset
= rx
->offset
;
761 frag
->size
= rx
->status
;
763 skb
->data_len
+= rx
->status
;
765 skb_shinfo(nskb
)->nr_frags
= 0;
772 shinfo
->nr_frags
= nr_frags
;
776 static int checksum_setup(struct net_device
*dev
, struct sk_buff
*skb
)
781 int recalculate_partial_csum
= 0;
784 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
785 * peers can fail to set NETRXF_csum_blank when sending a GSO
786 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
787 * recalculate the partial checksum.
789 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
&& skb_is_gso(skb
)) {
790 struct netfront_info
*np
= netdev_priv(dev
);
791 np
->rx_gso_checksum_fixup
++;
792 skb
->ip_summed
= CHECKSUM_PARTIAL
;
793 recalculate_partial_csum
= 1;
796 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
797 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
800 if (skb
->protocol
!= htons(ETH_P_IP
))
803 iph
= (void *)skb
->data
;
804 th
= skb
->data
+ 4 * iph
->ihl
;
805 if (th
>= skb_tail_pointer(skb
))
808 skb
->csum_start
= th
- skb
->head
;
809 switch (iph
->protocol
) {
811 skb
->csum_offset
= offsetof(struct tcphdr
, check
);
813 if (recalculate_partial_csum
) {
814 struct tcphdr
*tcph
= (struct tcphdr
*)th
;
815 tcph
->check
= ~csum_tcpudp_magic(iph
->saddr
, iph
->daddr
,
816 skb
->len
- iph
->ihl
*4,
821 skb
->csum_offset
= offsetof(struct udphdr
, check
);
823 if (recalculate_partial_csum
) {
824 struct udphdr
*udph
= (struct udphdr
*)th
;
825 udph
->check
= ~csum_tcpudp_magic(iph
->saddr
, iph
->daddr
,
826 skb
->len
- iph
->ihl
*4,
832 printk(KERN_ERR
"Attempting to checksum a non-"
833 "TCP/UDP packet, dropping a protocol"
834 " %d packet", iph
->protocol
);
838 if ((th
+ skb
->csum_offset
+ 2) > skb_tail_pointer(skb
))
847 static int handle_incoming_queue(struct net_device
*dev
,
848 struct sk_buff_head
*rxq
)
850 int packets_dropped
= 0;
853 while ((skb
= __skb_dequeue(rxq
)) != NULL
) {
854 struct page
*page
= NETFRONT_SKB_CB(skb
)->page
;
855 void *vaddr
= page_address(page
);
856 unsigned offset
= NETFRONT_SKB_CB(skb
)->offset
;
858 memcpy(skb
->data
, vaddr
+ offset
,
861 if (page
!= skb_shinfo(skb
)->frags
[0].page
)
864 /* Ethernet work: Delayed to here as it peeks the header. */
865 skb
->protocol
= eth_type_trans(skb
, dev
);
867 if (checksum_setup(dev
, skb
)) {
870 dev
->stats
.rx_errors
++;
874 dev
->stats
.rx_packets
++;
875 dev
->stats
.rx_bytes
+= skb
->len
;
878 netif_receive_skb(skb
);
881 return packets_dropped
;
884 static int xennet_poll(struct napi_struct
*napi
, int budget
)
886 struct netfront_info
*np
= container_of(napi
, struct netfront_info
, napi
);
887 struct net_device
*dev
= np
->netdev
;
889 struct netfront_rx_info rinfo
;
890 struct xen_netif_rx_response
*rx
= &rinfo
.rx
;
891 struct xen_netif_extra_info
*extras
= rinfo
.extras
;
894 struct sk_buff_head rxq
;
895 struct sk_buff_head errq
;
896 struct sk_buff_head tmpq
;
901 spin_lock(&np
->rx_lock
);
903 skb_queue_head_init(&rxq
);
904 skb_queue_head_init(&errq
);
905 skb_queue_head_init(&tmpq
);
907 rp
= np
->rx
.sring
->rsp_prod
;
908 rmb(); /* Ensure we see queued responses up to 'rp'. */
912 while ((i
!= rp
) && (work_done
< budget
)) {
913 memcpy(rx
, RING_GET_RESPONSE(&np
->rx
, i
), sizeof(*rx
));
914 memset(extras
, 0, sizeof(rinfo
.extras
));
916 err
= xennet_get_responses(np
, &rinfo
, rp
, &tmpq
);
920 while ((skb
= __skb_dequeue(&tmpq
)))
921 __skb_queue_tail(&errq
, skb
);
922 dev
->stats
.rx_errors
++;
927 skb
= __skb_dequeue(&tmpq
);
929 if (extras
[XEN_NETIF_EXTRA_TYPE_GSO
- 1].type
) {
930 struct xen_netif_extra_info
*gso
;
931 gso
= &extras
[XEN_NETIF_EXTRA_TYPE_GSO
- 1];
933 if (unlikely(xennet_set_skb_gso(skb
, gso
))) {
934 __skb_queue_head(&tmpq
, skb
);
935 np
->rx
.rsp_cons
+= skb_queue_len(&tmpq
);
940 NETFRONT_SKB_CB(skb
)->page
= skb_shinfo(skb
)->frags
[0].page
;
941 NETFRONT_SKB_CB(skb
)->offset
= rx
->offset
;
944 if (len
> RX_COPY_THRESHOLD
)
945 len
= RX_COPY_THRESHOLD
;
948 if (rx
->status
> len
) {
949 skb_shinfo(skb
)->frags
[0].page_offset
=
951 skb_shinfo(skb
)->frags
[0].size
= rx
->status
- len
;
952 skb
->data_len
= rx
->status
- len
;
954 skb_shinfo(skb
)->frags
[0].page
= NULL
;
955 skb_shinfo(skb
)->nr_frags
= 0;
958 i
= xennet_fill_frags(np
, skb
, &tmpq
);
961 * Truesize approximates the size of true data plus
962 * any supervisor overheads. Adding hypervisor
963 * overheads has been shown to significantly reduce
964 * achievable bandwidth with the default receive
965 * buffer size. It is therefore not wise to account
968 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set
969 * to RX_COPY_THRESHOLD + the supervisor
970 * overheads. Here, we add the size of the data pulled
971 * in xennet_fill_frags().
973 * We also adjust for any unused space in the main
974 * data area by subtracting (RX_COPY_THRESHOLD -
975 * len). This is especially important with drivers
976 * which split incoming packets into header and data,
977 * using only 66 bytes of the main data area (see the
978 * e1000 driver for example.) On such systems,
979 * without this last adjustement, our achievable
980 * receive throughout using the standard receive
981 * buffer size was cut by 25%(!!!).
983 skb
->truesize
+= skb
->data_len
- (RX_COPY_THRESHOLD
- len
);
984 skb
->len
+= skb
->data_len
;
986 if (rx
->flags
& XEN_NETRXF_csum_blank
)
987 skb
->ip_summed
= CHECKSUM_PARTIAL
;
988 else if (rx
->flags
& XEN_NETRXF_data_validated
)
989 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
991 __skb_queue_tail(&rxq
, skb
);
993 np
->rx
.rsp_cons
= ++i
;
997 __skb_queue_purge(&errq
);
999 work_done
-= handle_incoming_queue(dev
, &rxq
);
1001 /* If we get a callback with very few responses, reduce fill target. */
1002 /* NB. Note exponential increase, linear decrease. */
1003 if (((np
->rx
.req_prod_pvt
- np
->rx
.sring
->rsp_prod
) >
1004 ((3*np
->rx_target
) / 4)) &&
1005 (--np
->rx_target
< np
->rx_min_target
))
1006 np
->rx_target
= np
->rx_min_target
;
1008 xennet_alloc_rx_buffers(dev
);
1010 if (work_done
< budget
) {
1013 local_irq_save(flags
);
1015 RING_FINAL_CHECK_FOR_RESPONSES(&np
->rx
, more_to_do
);
1017 __napi_complete(napi
);
1019 local_irq_restore(flags
);
1022 spin_unlock(&np
->rx_lock
);
1027 static int xennet_change_mtu(struct net_device
*dev
, int mtu
)
1029 int max
= xennet_can_sg(dev
) ? 65535 - ETH_HLEN
: ETH_DATA_LEN
;
1037 static void xennet_release_tx_bufs(struct netfront_info
*np
)
1039 struct sk_buff
*skb
;
1042 for (i
= 0; i
< NET_TX_RING_SIZE
; i
++) {
1043 /* Skip over entries which are actually freelist references */
1044 if (skb_entry_is_link(&np
->tx_skbs
[i
]))
1047 skb
= np
->tx_skbs
[i
].skb
;
1048 gnttab_end_foreign_access_ref(np
->grant_tx_ref
[i
],
1050 gnttab_release_grant_reference(&np
->gref_tx_head
,
1051 np
->grant_tx_ref
[i
]);
1052 np
->grant_tx_ref
[i
] = GRANT_INVALID_REF
;
1053 add_id_to_freelist(&np
->tx_skb_freelist
, np
->tx_skbs
, i
);
1054 dev_kfree_skb_irq(skb
);
1058 static void xennet_release_rx_bufs(struct netfront_info
*np
)
1060 struct mmu_update
*mmu
= np
->rx_mmu
;
1061 struct multicall_entry
*mcl
= np
->rx_mcl
;
1062 struct sk_buff_head free_list
;
1063 struct sk_buff
*skb
;
1065 int xfer
= 0, noxfer
= 0, unused
= 0;
1068 dev_warn(&np
->netdev
->dev
, "%s: fix me for copying receiver.\n",
1072 skb_queue_head_init(&free_list
);
1074 spin_lock_bh(&np
->rx_lock
);
1076 for (id
= 0; id
< NET_RX_RING_SIZE
; id
++) {
1077 ref
= np
->grant_rx_ref
[id
];
1078 if (ref
== GRANT_INVALID_REF
) {
1083 skb
= np
->rx_skbs
[id
];
1084 mfn
= gnttab_end_foreign_transfer_ref(ref
);
1085 gnttab_release_grant_reference(&np
->gref_rx_head
, ref
);
1086 np
->grant_rx_ref
[id
] = GRANT_INVALID_REF
;
1089 skb_shinfo(skb
)->nr_frags
= 0;
1095 if (!xen_feature(XENFEAT_auto_translated_physmap
)) {
1096 /* Remap the page. */
1097 struct page
*page
= skb_shinfo(skb
)->frags
[0].page
;
1098 unsigned long pfn
= page_to_pfn(page
);
1099 void *vaddr
= page_address(page
);
1101 MULTI_update_va_mapping(mcl
, (unsigned long)vaddr
,
1102 mfn_pte(mfn
, PAGE_KERNEL
),
1105 mmu
->ptr
= ((u64
)mfn
<< PAGE_SHIFT
)
1106 | MMU_MACHPHYS_UPDATE
;
1110 set_phys_to_machine(pfn
, mfn
);
1112 __skb_queue_tail(&free_list
, skb
);
1116 dev_info(&np
->netdev
->dev
, "%s: %d xfer, %d noxfer, %d unused\n",
1117 __func__
, xfer
, noxfer
, unused
);
1120 if (!xen_feature(XENFEAT_auto_translated_physmap
)) {
1121 /* Do all the remapping work and M2P updates. */
1122 MULTI_mmu_update(mcl
, np
->rx_mmu
, mmu
- np
->rx_mmu
,
1125 HYPERVISOR_multicall(np
->rx_mcl
, mcl
- np
->rx_mcl
);
1129 __skb_queue_purge(&free_list
);
1131 spin_unlock_bh(&np
->rx_lock
);
1134 static void xennet_uninit(struct net_device
*dev
)
1136 struct netfront_info
*np
= netdev_priv(dev
);
1137 xennet_release_tx_bufs(np
);
1138 xennet_release_rx_bufs(np
);
1139 gnttab_free_grant_references(np
->gref_tx_head
);
1140 gnttab_free_grant_references(np
->gref_rx_head
);
1143 static u32
xennet_fix_features(struct net_device
*dev
, u32 features
)
1145 struct netfront_info
*np
= netdev_priv(dev
);
1148 if (features
& NETIF_F_SG
) {
1149 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
, "feature-sg",
1154 features
&= ~NETIF_F_SG
;
1157 if (features
& NETIF_F_TSO
) {
1158 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1159 "feature-gso-tcpv4", "%d", &val
) < 0)
1163 features
&= ~NETIF_F_TSO
;
1169 static int xennet_set_features(struct net_device
*dev
, u32 features
)
1171 if (!(features
& NETIF_F_SG
) && dev
->mtu
> ETH_DATA_LEN
) {
1172 netdev_info(dev
, "Reducing MTU because no SG offload");
1173 dev
->mtu
= ETH_DATA_LEN
;
1179 static const struct net_device_ops xennet_netdev_ops
= {
1180 .ndo_open
= xennet_open
,
1181 .ndo_uninit
= xennet_uninit
,
1182 .ndo_stop
= xennet_close
,
1183 .ndo_start_xmit
= xennet_start_xmit
,
1184 .ndo_change_mtu
= xennet_change_mtu
,
1185 .ndo_set_mac_address
= eth_mac_addr
,
1186 .ndo_validate_addr
= eth_validate_addr
,
1187 .ndo_fix_features
= xennet_fix_features
,
1188 .ndo_set_features
= xennet_set_features
,
1191 static struct net_device
* __devinit
xennet_create_dev(struct xenbus_device
*dev
)
1194 struct net_device
*netdev
;
1195 struct netfront_info
*np
;
1197 netdev
= alloc_etherdev(sizeof(struct netfront_info
));
1199 printk(KERN_WARNING
"%s> alloc_etherdev failed.\n",
1201 return ERR_PTR(-ENOMEM
);
1204 np
= netdev_priv(netdev
);
1207 spin_lock_init(&np
->tx_lock
);
1208 spin_lock_init(&np
->rx_lock
);
1210 skb_queue_head_init(&np
->rx_batch
);
1211 np
->rx_target
= RX_DFL_MIN_TARGET
;
1212 np
->rx_min_target
= RX_DFL_MIN_TARGET
;
1213 np
->rx_max_target
= RX_MAX_TARGET
;
1215 init_timer(&np
->rx_refill_timer
);
1216 np
->rx_refill_timer
.data
= (unsigned long)netdev
;
1217 np
->rx_refill_timer
.function
= rx_refill_timeout
;
1219 /* Initialise tx_skbs as a free chain containing every entry. */
1220 np
->tx_skb_freelist
= 0;
1221 for (i
= 0; i
< NET_TX_RING_SIZE
; i
++) {
1222 skb_entry_set_link(&np
->tx_skbs
[i
], i
+1);
1223 np
->grant_tx_ref
[i
] = GRANT_INVALID_REF
;
1226 /* Clear out rx_skbs */
1227 for (i
= 0; i
< NET_RX_RING_SIZE
; i
++) {
1228 np
->rx_skbs
[i
] = NULL
;
1229 np
->grant_rx_ref
[i
] = GRANT_INVALID_REF
;
1232 /* A grant for every tx ring slot */
1233 if (gnttab_alloc_grant_references(TX_MAX_TARGET
,
1234 &np
->gref_tx_head
) < 0) {
1235 printk(KERN_ALERT
"#### netfront can't alloc tx grant refs\n");
1239 /* A grant for every rx ring slot */
1240 if (gnttab_alloc_grant_references(RX_MAX_TARGET
,
1241 &np
->gref_rx_head
) < 0) {
1242 printk(KERN_ALERT
"#### netfront can't alloc rx grant refs\n");
1247 netdev
->netdev_ops
= &xennet_netdev_ops
;
1249 netif_napi_add(netdev
, &np
->napi
, xennet_poll
, 64);
1250 netdev
->features
= NETIF_F_IP_CSUM
| NETIF_F_RXCSUM
|
1252 netdev
->hw_features
= NETIF_F_IP_CSUM
| NETIF_F_SG
| NETIF_F_TSO
;
1255 * Assume that all hw features are available for now. This set
1256 * will be adjusted by the call to netdev_update_features() in
1257 * xennet_connect() which is the earliest point where we can
1258 * negotiate with the backend regarding supported features.
1260 netdev
->features
|= netdev
->hw_features
;
1262 SET_ETHTOOL_OPS(netdev
, &xennet_ethtool_ops
);
1263 SET_NETDEV_DEV(netdev
, &dev
->dev
);
1265 np
->netdev
= netdev
;
1267 netif_carrier_off(netdev
);
1272 gnttab_free_grant_references(np
->gref_tx_head
);
1274 free_netdev(netdev
);
1275 return ERR_PTR(err
);
1279 * Entry point to this code when a new device is created. Allocate the basic
1280 * structures and the ring buffers for communication with the backend, and
1281 * inform the backend of the appropriate details for those.
1283 static int __devinit
netfront_probe(struct xenbus_device
*dev
,
1284 const struct xenbus_device_id
*id
)
1287 struct net_device
*netdev
;
1288 struct netfront_info
*info
;
1290 netdev
= xennet_create_dev(dev
);
1291 if (IS_ERR(netdev
)) {
1292 err
= PTR_ERR(netdev
);
1293 xenbus_dev_fatal(dev
, err
, "creating netdev");
1297 info
= netdev_priv(netdev
);
1298 dev_set_drvdata(&dev
->dev
, info
);
1300 err
= register_netdev(info
->netdev
);
1302 printk(KERN_WARNING
"%s: register_netdev err=%d\n",
1307 err
= xennet_sysfs_addif(info
->netdev
);
1309 unregister_netdev(info
->netdev
);
1310 printk(KERN_WARNING
"%s: add sysfs failed err=%d\n",
1318 free_netdev(netdev
);
1319 dev_set_drvdata(&dev
->dev
, NULL
);
1323 static void xennet_end_access(int ref
, void *page
)
1325 /* This frees the page as a side-effect */
1326 if (ref
!= GRANT_INVALID_REF
)
1327 gnttab_end_foreign_access(ref
, 0, (unsigned long)page
);
1330 static void xennet_disconnect_backend(struct netfront_info
*info
)
1332 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1333 spin_lock_bh(&info
->rx_lock
);
1334 spin_lock_irq(&info
->tx_lock
);
1335 netif_carrier_off(info
->netdev
);
1336 spin_unlock_irq(&info
->tx_lock
);
1337 spin_unlock_bh(&info
->rx_lock
);
1339 if (info
->netdev
->irq
)
1340 unbind_from_irqhandler(info
->netdev
->irq
, info
->netdev
);
1341 info
->evtchn
= info
->netdev
->irq
= 0;
1343 /* End access and free the pages */
1344 xennet_end_access(info
->tx_ring_ref
, info
->tx
.sring
);
1345 xennet_end_access(info
->rx_ring_ref
, info
->rx
.sring
);
1347 info
->tx_ring_ref
= GRANT_INVALID_REF
;
1348 info
->rx_ring_ref
= GRANT_INVALID_REF
;
1349 info
->tx
.sring
= NULL
;
1350 info
->rx
.sring
= NULL
;
1354 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1355 * driver restart. We tear down our netif structure and recreate it, but
1356 * leave the device-layer structures intact so that this is transparent to the
1357 * rest of the kernel.
1359 static int netfront_resume(struct xenbus_device
*dev
)
1361 struct netfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1363 dev_dbg(&dev
->dev
, "%s\n", dev
->nodename
);
1365 xennet_disconnect_backend(info
);
1369 static int xen_net_read_mac(struct xenbus_device
*dev
, u8 mac
[])
1371 char *s
, *e
, *macstr
;
1374 macstr
= s
= xenbus_read(XBT_NIL
, dev
->nodename
, "mac", NULL
);
1376 return PTR_ERR(macstr
);
1378 for (i
= 0; i
< ETH_ALEN
; i
++) {
1379 mac
[i
] = simple_strtoul(s
, &e
, 16);
1380 if ((s
== e
) || (*e
!= ((i
== ETH_ALEN
-1) ? '\0' : ':'))) {
1391 static irqreturn_t
xennet_interrupt(int irq
, void *dev_id
)
1393 struct net_device
*dev
= dev_id
;
1394 struct netfront_info
*np
= netdev_priv(dev
);
1395 unsigned long flags
;
1397 spin_lock_irqsave(&np
->tx_lock
, flags
);
1399 if (likely(netif_carrier_ok(dev
))) {
1400 xennet_tx_buf_gc(dev
);
1401 /* Under tx_lock: protects access to rx shared-ring indexes. */
1402 if (RING_HAS_UNCONSUMED_RESPONSES(&np
->rx
))
1403 napi_schedule(&np
->napi
);
1406 spin_unlock_irqrestore(&np
->tx_lock
, flags
);
1411 static int setup_netfront(struct xenbus_device
*dev
, struct netfront_info
*info
)
1413 struct xen_netif_tx_sring
*txs
;
1414 struct xen_netif_rx_sring
*rxs
;
1416 struct net_device
*netdev
= info
->netdev
;
1418 info
->tx_ring_ref
= GRANT_INVALID_REF
;
1419 info
->rx_ring_ref
= GRANT_INVALID_REF
;
1420 info
->rx
.sring
= NULL
;
1421 info
->tx
.sring
= NULL
;
1424 err
= xen_net_read_mac(dev
, netdev
->dev_addr
);
1426 xenbus_dev_fatal(dev
, err
, "parsing %s/mac", dev
->nodename
);
1430 txs
= (struct xen_netif_tx_sring
*)get_zeroed_page(GFP_NOIO
| __GFP_HIGH
);
1433 xenbus_dev_fatal(dev
, err
, "allocating tx ring page");
1436 SHARED_RING_INIT(txs
);
1437 FRONT_RING_INIT(&info
->tx
, txs
, PAGE_SIZE
);
1439 err
= xenbus_grant_ring(dev
, virt_to_mfn(txs
));
1441 free_page((unsigned long)txs
);
1445 info
->tx_ring_ref
= err
;
1446 rxs
= (struct xen_netif_rx_sring
*)get_zeroed_page(GFP_NOIO
| __GFP_HIGH
);
1449 xenbus_dev_fatal(dev
, err
, "allocating rx ring page");
1452 SHARED_RING_INIT(rxs
);
1453 FRONT_RING_INIT(&info
->rx
, rxs
, PAGE_SIZE
);
1455 err
= xenbus_grant_ring(dev
, virt_to_mfn(rxs
));
1457 free_page((unsigned long)rxs
);
1460 info
->rx_ring_ref
= err
;
1462 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
1466 err
= bind_evtchn_to_irqhandler(info
->evtchn
, xennet_interrupt
,
1467 0, netdev
->name
, netdev
);
1477 /* Common code used when first setting up, and when resuming. */
1478 static int talk_to_netback(struct xenbus_device
*dev
,
1479 struct netfront_info
*info
)
1481 const char *message
;
1482 struct xenbus_transaction xbt
;
1485 /* Create shared ring, alloc event channel. */
1486 err
= setup_netfront(dev
, info
);
1491 err
= xenbus_transaction_start(&xbt
);
1493 xenbus_dev_fatal(dev
, err
, "starting transaction");
1497 err
= xenbus_printf(xbt
, dev
->nodename
, "tx-ring-ref", "%u",
1500 message
= "writing tx ring-ref";
1501 goto abort_transaction
;
1503 err
= xenbus_printf(xbt
, dev
->nodename
, "rx-ring-ref", "%u",
1506 message
= "writing rx ring-ref";
1507 goto abort_transaction
;
1509 err
= xenbus_printf(xbt
, dev
->nodename
,
1510 "event-channel", "%u", info
->evtchn
);
1512 message
= "writing event-channel";
1513 goto abort_transaction
;
1516 err
= xenbus_printf(xbt
, dev
->nodename
, "request-rx-copy", "%u",
1519 message
= "writing request-rx-copy";
1520 goto abort_transaction
;
1523 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-rx-notify", "%d", 1);
1525 message
= "writing feature-rx-notify";
1526 goto abort_transaction
;
1529 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-sg", "%d", 1);
1531 message
= "writing feature-sg";
1532 goto abort_transaction
;
1535 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-gso-tcpv4", "%d", 1);
1537 message
= "writing feature-gso-tcpv4";
1538 goto abort_transaction
;
1541 err
= xenbus_transaction_end(xbt
, 0);
1545 xenbus_dev_fatal(dev
, err
, "completing transaction");
1552 xenbus_transaction_end(xbt
, 1);
1553 xenbus_dev_fatal(dev
, err
, "%s", message
);
1555 xennet_disconnect_backend(info
);
1560 static int xennet_connect(struct net_device
*dev
)
1562 struct netfront_info
*np
= netdev_priv(dev
);
1563 int i
, requeue_idx
, err
;
1564 struct sk_buff
*skb
;
1566 struct xen_netif_rx_request
*req
;
1567 unsigned int feature_rx_copy
;
1569 err
= xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1570 "feature-rx-copy", "%u", &feature_rx_copy
);
1572 feature_rx_copy
= 0;
1574 if (!feature_rx_copy
) {
1576 "backend does not support copying receive path\n");
1580 err
= talk_to_netback(np
->xbdev
, np
);
1585 netdev_update_features(dev
);
1588 spin_lock_bh(&np
->rx_lock
);
1589 spin_lock_irq(&np
->tx_lock
);
1591 /* Step 1: Discard all pending TX packet fragments. */
1592 xennet_release_tx_bufs(np
);
1594 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1595 for (requeue_idx
= 0, i
= 0; i
< NET_RX_RING_SIZE
; i
++) {
1596 if (!np
->rx_skbs
[i
])
1599 skb
= np
->rx_skbs
[requeue_idx
] = xennet_get_rx_skb(np
, i
);
1600 ref
= np
->grant_rx_ref
[requeue_idx
] = xennet_get_rx_ref(np
, i
);
1601 req
= RING_GET_REQUEST(&np
->rx
, requeue_idx
);
1603 gnttab_grant_foreign_access_ref(
1604 ref
, np
->xbdev
->otherend_id
,
1605 pfn_to_mfn(page_to_pfn(skb_shinfo(skb
)->
1609 req
->id
= requeue_idx
;
1614 np
->rx
.req_prod_pvt
= requeue_idx
;
1617 * Step 3: All public and private state should now be sane. Get
1618 * ready to start sending and receiving packets and give the driver
1619 * domain a kick because we've probably just requeued some
1622 netif_carrier_on(np
->netdev
);
1623 notify_remote_via_irq(np
->netdev
->irq
);
1624 xennet_tx_buf_gc(dev
);
1625 xennet_alloc_rx_buffers(dev
);
1627 spin_unlock_irq(&np
->tx_lock
);
1628 spin_unlock_bh(&np
->rx_lock
);
1634 * Callback received when the backend's state changes.
1636 static void netback_changed(struct xenbus_device
*dev
,
1637 enum xenbus_state backend_state
)
1639 struct netfront_info
*np
= dev_get_drvdata(&dev
->dev
);
1640 struct net_device
*netdev
= np
->netdev
;
1642 dev_dbg(&dev
->dev
, "%s\n", xenbus_strstate(backend_state
));
1644 switch (backend_state
) {
1645 case XenbusStateInitialising
:
1646 case XenbusStateInitialised
:
1647 case XenbusStateReconfiguring
:
1648 case XenbusStateReconfigured
:
1649 case XenbusStateConnected
:
1650 case XenbusStateUnknown
:
1651 case XenbusStateClosed
:
1654 case XenbusStateInitWait
:
1655 if (dev
->state
!= XenbusStateInitialising
)
1657 if (xennet_connect(netdev
) != 0)
1659 xenbus_switch_state(dev
, XenbusStateConnected
);
1660 netif_notify_peers(netdev
);
1663 case XenbusStateClosing
:
1664 xenbus_frontend_closed(dev
);
1669 static const struct xennet_stat
{
1670 char name
[ETH_GSTRING_LEN
];
1672 } xennet_stats
[] = {
1674 "rx_gso_checksum_fixup",
1675 offsetof(struct netfront_info
, rx_gso_checksum_fixup
)
1679 static int xennet_get_sset_count(struct net_device
*dev
, int string_set
)
1681 switch (string_set
) {
1683 return ARRAY_SIZE(xennet_stats
);
1689 static void xennet_get_ethtool_stats(struct net_device
*dev
,
1690 struct ethtool_stats
*stats
, u64
* data
)
1692 void *np
= netdev_priv(dev
);
1695 for (i
= 0; i
< ARRAY_SIZE(xennet_stats
); i
++)
1696 data
[i
] = *(unsigned long *)(np
+ xennet_stats
[i
].offset
);
1699 static void xennet_get_strings(struct net_device
*dev
, u32 stringset
, u8
* data
)
1703 switch (stringset
) {
1705 for (i
= 0; i
< ARRAY_SIZE(xennet_stats
); i
++)
1706 memcpy(data
+ i
* ETH_GSTRING_LEN
,
1707 xennet_stats
[i
].name
, ETH_GSTRING_LEN
);
1712 static const struct ethtool_ops xennet_ethtool_ops
=
1714 .get_link
= ethtool_op_get_link
,
1716 .get_sset_count
= xennet_get_sset_count
,
1717 .get_ethtool_stats
= xennet_get_ethtool_stats
,
1718 .get_strings
= xennet_get_strings
,
1722 static ssize_t
show_rxbuf_min(struct device
*dev
,
1723 struct device_attribute
*attr
, char *buf
)
1725 struct net_device
*netdev
= to_net_dev(dev
);
1726 struct netfront_info
*info
= netdev_priv(netdev
);
1728 return sprintf(buf
, "%u\n", info
->rx_min_target
);
1731 static ssize_t
store_rxbuf_min(struct device
*dev
,
1732 struct device_attribute
*attr
,
1733 const char *buf
, size_t len
)
1735 struct net_device
*netdev
= to_net_dev(dev
);
1736 struct netfront_info
*np
= netdev_priv(netdev
);
1738 unsigned long target
;
1740 if (!capable(CAP_NET_ADMIN
))
1743 target
= simple_strtoul(buf
, &endp
, 0);
1747 if (target
< RX_MIN_TARGET
)
1748 target
= RX_MIN_TARGET
;
1749 if (target
> RX_MAX_TARGET
)
1750 target
= RX_MAX_TARGET
;
1752 spin_lock_bh(&np
->rx_lock
);
1753 if (target
> np
->rx_max_target
)
1754 np
->rx_max_target
= target
;
1755 np
->rx_min_target
= target
;
1756 if (target
> np
->rx_target
)
1757 np
->rx_target
= target
;
1759 xennet_alloc_rx_buffers(netdev
);
1761 spin_unlock_bh(&np
->rx_lock
);
1765 static ssize_t
show_rxbuf_max(struct device
*dev
,
1766 struct device_attribute
*attr
, char *buf
)
1768 struct net_device
*netdev
= to_net_dev(dev
);
1769 struct netfront_info
*info
= netdev_priv(netdev
);
1771 return sprintf(buf
, "%u\n", info
->rx_max_target
);
1774 static ssize_t
store_rxbuf_max(struct device
*dev
,
1775 struct device_attribute
*attr
,
1776 const char *buf
, size_t len
)
1778 struct net_device
*netdev
= to_net_dev(dev
);
1779 struct netfront_info
*np
= netdev_priv(netdev
);
1781 unsigned long target
;
1783 if (!capable(CAP_NET_ADMIN
))
1786 target
= simple_strtoul(buf
, &endp
, 0);
1790 if (target
< RX_MIN_TARGET
)
1791 target
= RX_MIN_TARGET
;
1792 if (target
> RX_MAX_TARGET
)
1793 target
= RX_MAX_TARGET
;
1795 spin_lock_bh(&np
->rx_lock
);
1796 if (target
< np
->rx_min_target
)
1797 np
->rx_min_target
= target
;
1798 np
->rx_max_target
= target
;
1799 if (target
< np
->rx_target
)
1800 np
->rx_target
= target
;
1802 xennet_alloc_rx_buffers(netdev
);
1804 spin_unlock_bh(&np
->rx_lock
);
1808 static ssize_t
show_rxbuf_cur(struct device
*dev
,
1809 struct device_attribute
*attr
, char *buf
)
1811 struct net_device
*netdev
= to_net_dev(dev
);
1812 struct netfront_info
*info
= netdev_priv(netdev
);
1814 return sprintf(buf
, "%u\n", info
->rx_target
);
1817 static struct device_attribute xennet_attrs
[] = {
1818 __ATTR(rxbuf_min
, S_IRUGO
|S_IWUSR
, show_rxbuf_min
, store_rxbuf_min
),
1819 __ATTR(rxbuf_max
, S_IRUGO
|S_IWUSR
, show_rxbuf_max
, store_rxbuf_max
),
1820 __ATTR(rxbuf_cur
, S_IRUGO
, show_rxbuf_cur
, NULL
),
1823 static int xennet_sysfs_addif(struct net_device
*netdev
)
1828 for (i
= 0; i
< ARRAY_SIZE(xennet_attrs
); i
++) {
1829 err
= device_create_file(&netdev
->dev
,
1838 device_remove_file(&netdev
->dev
, &xennet_attrs
[i
]);
1842 static void xennet_sysfs_delif(struct net_device
*netdev
)
1846 for (i
= 0; i
< ARRAY_SIZE(xennet_attrs
); i
++)
1847 device_remove_file(&netdev
->dev
, &xennet_attrs
[i
]);
1850 #endif /* CONFIG_SYSFS */
1852 static struct xenbus_device_id netfront_ids
[] = {
1858 static int __devexit
xennet_remove(struct xenbus_device
*dev
)
1860 struct netfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1862 dev_dbg(&dev
->dev
, "%s\n", dev
->nodename
);
1864 unregister_netdev(info
->netdev
);
1866 xennet_disconnect_backend(info
);
1868 del_timer_sync(&info
->rx_refill_timer
);
1870 xennet_sysfs_delif(info
->netdev
);
1872 free_netdev(info
->netdev
);
1877 static struct xenbus_driver netfront_driver
= {
1879 .owner
= THIS_MODULE
,
1880 .ids
= netfront_ids
,
1881 .probe
= netfront_probe
,
1882 .remove
= __devexit_p(xennet_remove
),
1883 .resume
= netfront_resume
,
1884 .otherend_changed
= netback_changed
,
1887 static int __init
netif_init(void)
1892 if (xen_initial_domain())
1895 printk(KERN_INFO
"Initialising Xen virtual ethernet driver.\n");
1897 return xenbus_register_frontend(&netfront_driver
);
1899 module_init(netif_init
);
1902 static void __exit
netif_exit(void)
1904 if (xen_initial_domain())
1907 xenbus_unregister_driver(&netfront_driver
);
1909 module_exit(netif_exit
);
1911 MODULE_DESCRIPTION("Xen virtual network device frontend");
1912 MODULE_LICENSE("GPL");
1913 MODULE_ALIAS("xen:vif");
1914 MODULE_ALIAS("xennet");