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 const struct net_device_ops xennet_netdev_ops
= {
1144 .ndo_open
= xennet_open
,
1145 .ndo_uninit
= xennet_uninit
,
1146 .ndo_stop
= xennet_close
,
1147 .ndo_start_xmit
= xennet_start_xmit
,
1148 .ndo_change_mtu
= xennet_change_mtu
,
1149 .ndo_set_mac_address
= eth_mac_addr
,
1150 .ndo_validate_addr
= eth_validate_addr
,
1153 static struct net_device
* __devinit
xennet_create_dev(struct xenbus_device
*dev
)
1156 struct net_device
*netdev
;
1157 struct netfront_info
*np
;
1159 netdev
= alloc_etherdev(sizeof(struct netfront_info
));
1161 printk(KERN_WARNING
"%s> alloc_etherdev failed.\n",
1163 return ERR_PTR(-ENOMEM
);
1166 np
= netdev_priv(netdev
);
1169 spin_lock_init(&np
->tx_lock
);
1170 spin_lock_init(&np
->rx_lock
);
1172 skb_queue_head_init(&np
->rx_batch
);
1173 np
->rx_target
= RX_DFL_MIN_TARGET
;
1174 np
->rx_min_target
= RX_DFL_MIN_TARGET
;
1175 np
->rx_max_target
= RX_MAX_TARGET
;
1177 init_timer(&np
->rx_refill_timer
);
1178 np
->rx_refill_timer
.data
= (unsigned long)netdev
;
1179 np
->rx_refill_timer
.function
= rx_refill_timeout
;
1181 /* Initialise tx_skbs as a free chain containing every entry. */
1182 np
->tx_skb_freelist
= 0;
1183 for (i
= 0; i
< NET_TX_RING_SIZE
; i
++) {
1184 skb_entry_set_link(&np
->tx_skbs
[i
], i
+1);
1185 np
->grant_tx_ref
[i
] = GRANT_INVALID_REF
;
1188 /* Clear out rx_skbs */
1189 for (i
= 0; i
< NET_RX_RING_SIZE
; i
++) {
1190 np
->rx_skbs
[i
] = NULL
;
1191 np
->grant_rx_ref
[i
] = GRANT_INVALID_REF
;
1194 /* A grant for every tx ring slot */
1195 if (gnttab_alloc_grant_references(TX_MAX_TARGET
,
1196 &np
->gref_tx_head
) < 0) {
1197 printk(KERN_ALERT
"#### netfront can't alloc tx grant refs\n");
1201 /* A grant for every rx ring slot */
1202 if (gnttab_alloc_grant_references(RX_MAX_TARGET
,
1203 &np
->gref_rx_head
) < 0) {
1204 printk(KERN_ALERT
"#### netfront can't alloc rx grant refs\n");
1209 netdev
->netdev_ops
= &xennet_netdev_ops
;
1211 netif_napi_add(netdev
, &np
->napi
, xennet_poll
, 64);
1212 netdev
->features
= NETIF_F_IP_CSUM
;
1214 SET_ETHTOOL_OPS(netdev
, &xennet_ethtool_ops
);
1215 SET_NETDEV_DEV(netdev
, &dev
->dev
);
1217 np
->netdev
= netdev
;
1219 netif_carrier_off(netdev
);
1224 gnttab_free_grant_references(np
->gref_tx_head
);
1226 free_netdev(netdev
);
1227 return ERR_PTR(err
);
1231 * Entry point to this code when a new device is created. Allocate the basic
1232 * structures and the ring buffers for communication with the backend, and
1233 * inform the backend of the appropriate details for those.
1235 static int __devinit
netfront_probe(struct xenbus_device
*dev
,
1236 const struct xenbus_device_id
*id
)
1239 struct net_device
*netdev
;
1240 struct netfront_info
*info
;
1242 netdev
= xennet_create_dev(dev
);
1243 if (IS_ERR(netdev
)) {
1244 err
= PTR_ERR(netdev
);
1245 xenbus_dev_fatal(dev
, err
, "creating netdev");
1249 info
= netdev_priv(netdev
);
1250 dev_set_drvdata(&dev
->dev
, info
);
1252 err
= register_netdev(info
->netdev
);
1254 printk(KERN_WARNING
"%s: register_netdev err=%d\n",
1259 err
= xennet_sysfs_addif(info
->netdev
);
1261 unregister_netdev(info
->netdev
);
1262 printk(KERN_WARNING
"%s: add sysfs failed err=%d\n",
1270 free_netdev(netdev
);
1271 dev_set_drvdata(&dev
->dev
, NULL
);
1275 static void xennet_end_access(int ref
, void *page
)
1277 /* This frees the page as a side-effect */
1278 if (ref
!= GRANT_INVALID_REF
)
1279 gnttab_end_foreign_access(ref
, 0, (unsigned long)page
);
1282 static void xennet_disconnect_backend(struct netfront_info
*info
)
1284 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1285 spin_lock_bh(&info
->rx_lock
);
1286 spin_lock_irq(&info
->tx_lock
);
1287 netif_carrier_off(info
->netdev
);
1288 spin_unlock_irq(&info
->tx_lock
);
1289 spin_unlock_bh(&info
->rx_lock
);
1291 if (info
->netdev
->irq
)
1292 unbind_from_irqhandler(info
->netdev
->irq
, info
->netdev
);
1293 info
->evtchn
= info
->netdev
->irq
= 0;
1295 /* End access and free the pages */
1296 xennet_end_access(info
->tx_ring_ref
, info
->tx
.sring
);
1297 xennet_end_access(info
->rx_ring_ref
, info
->rx
.sring
);
1299 info
->tx_ring_ref
= GRANT_INVALID_REF
;
1300 info
->rx_ring_ref
= GRANT_INVALID_REF
;
1301 info
->tx
.sring
= NULL
;
1302 info
->rx
.sring
= NULL
;
1306 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1307 * driver restart. We tear down our netif structure and recreate it, but
1308 * leave the device-layer structures intact so that this is transparent to the
1309 * rest of the kernel.
1311 static int netfront_resume(struct xenbus_device
*dev
)
1313 struct netfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1315 dev_dbg(&dev
->dev
, "%s\n", dev
->nodename
);
1317 xennet_disconnect_backend(info
);
1321 static int xen_net_read_mac(struct xenbus_device
*dev
, u8 mac
[])
1323 char *s
, *e
, *macstr
;
1326 macstr
= s
= xenbus_read(XBT_NIL
, dev
->nodename
, "mac", NULL
);
1328 return PTR_ERR(macstr
);
1330 for (i
= 0; i
< ETH_ALEN
; i
++) {
1331 mac
[i
] = simple_strtoul(s
, &e
, 16);
1332 if ((s
== e
) || (*e
!= ((i
== ETH_ALEN
-1) ? '\0' : ':'))) {
1343 static irqreturn_t
xennet_interrupt(int irq
, void *dev_id
)
1345 struct net_device
*dev
= dev_id
;
1346 struct netfront_info
*np
= netdev_priv(dev
);
1347 unsigned long flags
;
1349 spin_lock_irqsave(&np
->tx_lock
, flags
);
1351 if (likely(netif_carrier_ok(dev
))) {
1352 xennet_tx_buf_gc(dev
);
1353 /* Under tx_lock: protects access to rx shared-ring indexes. */
1354 if (RING_HAS_UNCONSUMED_RESPONSES(&np
->rx
))
1355 napi_schedule(&np
->napi
);
1358 spin_unlock_irqrestore(&np
->tx_lock
, flags
);
1363 static int setup_netfront(struct xenbus_device
*dev
, struct netfront_info
*info
)
1365 struct xen_netif_tx_sring
*txs
;
1366 struct xen_netif_rx_sring
*rxs
;
1368 struct net_device
*netdev
= info
->netdev
;
1370 info
->tx_ring_ref
= GRANT_INVALID_REF
;
1371 info
->rx_ring_ref
= GRANT_INVALID_REF
;
1372 info
->rx
.sring
= NULL
;
1373 info
->tx
.sring
= NULL
;
1376 err
= xen_net_read_mac(dev
, netdev
->dev_addr
);
1378 xenbus_dev_fatal(dev
, err
, "parsing %s/mac", dev
->nodename
);
1382 txs
= (struct xen_netif_tx_sring
*)get_zeroed_page(GFP_NOIO
| __GFP_HIGH
);
1385 xenbus_dev_fatal(dev
, err
, "allocating tx ring page");
1388 SHARED_RING_INIT(txs
);
1389 FRONT_RING_INIT(&info
->tx
, txs
, PAGE_SIZE
);
1391 err
= xenbus_grant_ring(dev
, virt_to_mfn(txs
));
1393 free_page((unsigned long)txs
);
1397 info
->tx_ring_ref
= err
;
1398 rxs
= (struct xen_netif_rx_sring
*)get_zeroed_page(GFP_NOIO
| __GFP_HIGH
);
1401 xenbus_dev_fatal(dev
, err
, "allocating rx ring page");
1404 SHARED_RING_INIT(rxs
);
1405 FRONT_RING_INIT(&info
->rx
, rxs
, PAGE_SIZE
);
1407 err
= xenbus_grant_ring(dev
, virt_to_mfn(rxs
));
1409 free_page((unsigned long)rxs
);
1412 info
->rx_ring_ref
= err
;
1414 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
1418 err
= bind_evtchn_to_irqhandler(info
->evtchn
, xennet_interrupt
,
1419 IRQF_SAMPLE_RANDOM
, netdev
->name
,
1430 /* Common code used when first setting up, and when resuming. */
1431 static int talk_to_netback(struct xenbus_device
*dev
,
1432 struct netfront_info
*info
)
1434 const char *message
;
1435 struct xenbus_transaction xbt
;
1438 /* Create shared ring, alloc event channel. */
1439 err
= setup_netfront(dev
, info
);
1444 err
= xenbus_transaction_start(&xbt
);
1446 xenbus_dev_fatal(dev
, err
, "starting transaction");
1450 err
= xenbus_printf(xbt
, dev
->nodename
, "tx-ring-ref", "%u",
1453 message
= "writing tx ring-ref";
1454 goto abort_transaction
;
1456 err
= xenbus_printf(xbt
, dev
->nodename
, "rx-ring-ref", "%u",
1459 message
= "writing rx ring-ref";
1460 goto abort_transaction
;
1462 err
= xenbus_printf(xbt
, dev
->nodename
,
1463 "event-channel", "%u", info
->evtchn
);
1465 message
= "writing event-channel";
1466 goto abort_transaction
;
1469 err
= xenbus_printf(xbt
, dev
->nodename
, "request-rx-copy", "%u",
1472 message
= "writing request-rx-copy";
1473 goto abort_transaction
;
1476 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-rx-notify", "%d", 1);
1478 message
= "writing feature-rx-notify";
1479 goto abort_transaction
;
1482 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-sg", "%d", 1);
1484 message
= "writing feature-sg";
1485 goto abort_transaction
;
1488 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-gso-tcpv4", "%d", 1);
1490 message
= "writing feature-gso-tcpv4";
1491 goto abort_transaction
;
1494 err
= xenbus_transaction_end(xbt
, 0);
1498 xenbus_dev_fatal(dev
, err
, "completing transaction");
1505 xenbus_transaction_end(xbt
, 1);
1506 xenbus_dev_fatal(dev
, err
, "%s", message
);
1508 xennet_disconnect_backend(info
);
1513 static int xennet_set_sg(struct net_device
*dev
, u32 data
)
1516 struct netfront_info
*np
= netdev_priv(dev
);
1519 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
, "feature-sg",
1524 } else if (dev
->mtu
> ETH_DATA_LEN
)
1525 dev
->mtu
= ETH_DATA_LEN
;
1527 return ethtool_op_set_sg(dev
, data
);
1530 static int xennet_set_tso(struct net_device
*dev
, u32 data
)
1533 struct netfront_info
*np
= netdev_priv(dev
);
1536 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1537 "feature-gso-tcpv4", "%d", &val
) < 0)
1543 return ethtool_op_set_tso(dev
, data
);
1546 static void xennet_set_features(struct net_device
*dev
)
1548 /* Turn off all GSO bits except ROBUST. */
1549 dev
->features
&= ~NETIF_F_GSO_MASK
;
1550 dev
->features
|= NETIF_F_GSO_ROBUST
;
1551 xennet_set_sg(dev
, 0);
1553 /* We need checksum offload to enable scatter/gather and TSO. */
1554 if (!(dev
->features
& NETIF_F_IP_CSUM
))
1557 if (!xennet_set_sg(dev
, 1))
1558 xennet_set_tso(dev
, 1);
1561 static int xennet_connect(struct net_device
*dev
)
1563 struct netfront_info
*np
= netdev_priv(dev
);
1564 int i
, requeue_idx
, err
;
1565 struct sk_buff
*skb
;
1567 struct xen_netif_rx_request
*req
;
1568 unsigned int feature_rx_copy
;
1570 err
= xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1571 "feature-rx-copy", "%u", &feature_rx_copy
);
1573 feature_rx_copy
= 0;
1575 if (!feature_rx_copy
) {
1577 "backend does not support copying receive path\n");
1581 err
= talk_to_netback(np
->xbdev
, np
);
1585 xennet_set_features(dev
);
1587 spin_lock_bh(&np
->rx_lock
);
1588 spin_lock_irq(&np
->tx_lock
);
1590 /* Step 1: Discard all pending TX packet fragments. */
1591 xennet_release_tx_bufs(np
);
1593 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1594 for (requeue_idx
= 0, i
= 0; i
< NET_RX_RING_SIZE
; i
++) {
1595 if (!np
->rx_skbs
[i
])
1598 skb
= np
->rx_skbs
[requeue_idx
] = xennet_get_rx_skb(np
, i
);
1599 ref
= np
->grant_rx_ref
[requeue_idx
] = xennet_get_rx_ref(np
, i
);
1600 req
= RING_GET_REQUEST(&np
->rx
, requeue_idx
);
1602 gnttab_grant_foreign_access_ref(
1603 ref
, np
->xbdev
->otherend_id
,
1604 pfn_to_mfn(page_to_pfn(skb_shinfo(skb
)->
1608 req
->id
= requeue_idx
;
1613 np
->rx
.req_prod_pvt
= requeue_idx
;
1616 * Step 3: All public and private state should now be sane. Get
1617 * ready to start sending and receiving packets and give the driver
1618 * domain a kick because we've probably just requeued some
1621 netif_carrier_on(np
->netdev
);
1622 notify_remote_via_irq(np
->netdev
->irq
);
1623 xennet_tx_buf_gc(dev
);
1624 xennet_alloc_rx_buffers(dev
);
1626 spin_unlock_irq(&np
->tx_lock
);
1627 spin_unlock_bh(&np
->rx_lock
);
1633 * Callback received when the backend's state changes.
1635 static void netback_changed(struct xenbus_device
*dev
,
1636 enum xenbus_state backend_state
)
1638 struct netfront_info
*np
= dev_get_drvdata(&dev
->dev
);
1639 struct net_device
*netdev
= np
->netdev
;
1641 dev_dbg(&dev
->dev
, "%s\n", xenbus_strstate(backend_state
));
1643 switch (backend_state
) {
1644 case XenbusStateInitialising
:
1645 case XenbusStateInitialised
:
1646 case XenbusStateReconfiguring
:
1647 case XenbusStateReconfigured
:
1648 case XenbusStateConnected
:
1649 case XenbusStateUnknown
:
1650 case XenbusStateClosed
:
1653 case XenbusStateInitWait
:
1654 if (dev
->state
!= XenbusStateInitialising
)
1656 if (xennet_connect(netdev
) != 0)
1658 xenbus_switch_state(dev
, XenbusStateConnected
);
1659 netif_notify_peers(netdev
);
1662 case XenbusStateClosing
:
1663 xenbus_frontend_closed(dev
);
1668 static const struct xennet_stat
{
1669 char name
[ETH_GSTRING_LEN
];
1671 } xennet_stats
[] = {
1673 "rx_gso_checksum_fixup",
1674 offsetof(struct netfront_info
, rx_gso_checksum_fixup
)
1678 static int xennet_get_sset_count(struct net_device
*dev
, int string_set
)
1680 switch (string_set
) {
1682 return ARRAY_SIZE(xennet_stats
);
1688 static void xennet_get_ethtool_stats(struct net_device
*dev
,
1689 struct ethtool_stats
*stats
, u64
* data
)
1691 void *np
= netdev_priv(dev
);
1694 for (i
= 0; i
< ARRAY_SIZE(xennet_stats
); i
++)
1695 data
[i
] = *(unsigned long *)(np
+ xennet_stats
[i
].offset
);
1698 static void xennet_get_strings(struct net_device
*dev
, u32 stringset
, u8
* data
)
1702 switch (stringset
) {
1704 for (i
= 0; i
< ARRAY_SIZE(xennet_stats
); i
++)
1705 memcpy(data
+ i
* ETH_GSTRING_LEN
,
1706 xennet_stats
[i
].name
, ETH_GSTRING_LEN
);
1711 static const struct ethtool_ops xennet_ethtool_ops
=
1713 .set_tx_csum
= ethtool_op_set_tx_csum
,
1714 .set_sg
= xennet_set_sg
,
1715 .set_tso
= xennet_set_tso
,
1716 .get_link
= ethtool_op_get_link
,
1718 .get_sset_count
= xennet_get_sset_count
,
1719 .get_ethtool_stats
= xennet_get_ethtool_stats
,
1720 .get_strings
= xennet_get_strings
,
1724 static ssize_t
show_rxbuf_min(struct device
*dev
,
1725 struct device_attribute
*attr
, char *buf
)
1727 struct net_device
*netdev
= to_net_dev(dev
);
1728 struct netfront_info
*info
= netdev_priv(netdev
);
1730 return sprintf(buf
, "%u\n", info
->rx_min_target
);
1733 static ssize_t
store_rxbuf_min(struct device
*dev
,
1734 struct device_attribute
*attr
,
1735 const char *buf
, size_t len
)
1737 struct net_device
*netdev
= to_net_dev(dev
);
1738 struct netfront_info
*np
= netdev_priv(netdev
);
1740 unsigned long target
;
1742 if (!capable(CAP_NET_ADMIN
))
1745 target
= simple_strtoul(buf
, &endp
, 0);
1749 if (target
< RX_MIN_TARGET
)
1750 target
= RX_MIN_TARGET
;
1751 if (target
> RX_MAX_TARGET
)
1752 target
= RX_MAX_TARGET
;
1754 spin_lock_bh(&np
->rx_lock
);
1755 if (target
> np
->rx_max_target
)
1756 np
->rx_max_target
= target
;
1757 np
->rx_min_target
= target
;
1758 if (target
> np
->rx_target
)
1759 np
->rx_target
= target
;
1761 xennet_alloc_rx_buffers(netdev
);
1763 spin_unlock_bh(&np
->rx_lock
);
1767 static ssize_t
show_rxbuf_max(struct device
*dev
,
1768 struct device_attribute
*attr
, char *buf
)
1770 struct net_device
*netdev
= to_net_dev(dev
);
1771 struct netfront_info
*info
= netdev_priv(netdev
);
1773 return sprintf(buf
, "%u\n", info
->rx_max_target
);
1776 static ssize_t
store_rxbuf_max(struct device
*dev
,
1777 struct device_attribute
*attr
,
1778 const char *buf
, size_t len
)
1780 struct net_device
*netdev
= to_net_dev(dev
);
1781 struct netfront_info
*np
= netdev_priv(netdev
);
1783 unsigned long target
;
1785 if (!capable(CAP_NET_ADMIN
))
1788 target
= simple_strtoul(buf
, &endp
, 0);
1792 if (target
< RX_MIN_TARGET
)
1793 target
= RX_MIN_TARGET
;
1794 if (target
> RX_MAX_TARGET
)
1795 target
= RX_MAX_TARGET
;
1797 spin_lock_bh(&np
->rx_lock
);
1798 if (target
< np
->rx_min_target
)
1799 np
->rx_min_target
= target
;
1800 np
->rx_max_target
= target
;
1801 if (target
< np
->rx_target
)
1802 np
->rx_target
= target
;
1804 xennet_alloc_rx_buffers(netdev
);
1806 spin_unlock_bh(&np
->rx_lock
);
1810 static ssize_t
show_rxbuf_cur(struct device
*dev
,
1811 struct device_attribute
*attr
, char *buf
)
1813 struct net_device
*netdev
= to_net_dev(dev
);
1814 struct netfront_info
*info
= netdev_priv(netdev
);
1816 return sprintf(buf
, "%u\n", info
->rx_target
);
1819 static struct device_attribute xennet_attrs
[] = {
1820 __ATTR(rxbuf_min
, S_IRUGO
|S_IWUSR
, show_rxbuf_min
, store_rxbuf_min
),
1821 __ATTR(rxbuf_max
, S_IRUGO
|S_IWUSR
, show_rxbuf_max
, store_rxbuf_max
),
1822 __ATTR(rxbuf_cur
, S_IRUGO
, show_rxbuf_cur
, NULL
),
1825 static int xennet_sysfs_addif(struct net_device
*netdev
)
1830 for (i
= 0; i
< ARRAY_SIZE(xennet_attrs
); i
++) {
1831 err
= device_create_file(&netdev
->dev
,
1840 device_remove_file(&netdev
->dev
, &xennet_attrs
[i
]);
1844 static void xennet_sysfs_delif(struct net_device
*netdev
)
1848 for (i
= 0; i
< ARRAY_SIZE(xennet_attrs
); i
++)
1849 device_remove_file(&netdev
->dev
, &xennet_attrs
[i
]);
1852 #endif /* CONFIG_SYSFS */
1854 static struct xenbus_device_id netfront_ids
[] = {
1860 static int __devexit
xennet_remove(struct xenbus_device
*dev
)
1862 struct netfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1864 dev_dbg(&dev
->dev
, "%s\n", dev
->nodename
);
1866 unregister_netdev(info
->netdev
);
1868 xennet_disconnect_backend(info
);
1870 del_timer_sync(&info
->rx_refill_timer
);
1872 xennet_sysfs_delif(info
->netdev
);
1874 free_netdev(info
->netdev
);
1879 static struct xenbus_driver netfront_driver
= {
1881 .owner
= THIS_MODULE
,
1882 .ids
= netfront_ids
,
1883 .probe
= netfront_probe
,
1884 .remove
= __devexit_p(xennet_remove
),
1885 .resume
= netfront_resume
,
1886 .otherend_changed
= netback_changed
,
1889 static int __init
netif_init(void)
1894 if (xen_initial_domain())
1897 printk(KERN_INFO
"Initialising Xen virtual ethernet driver.\n");
1899 return xenbus_register_frontend(&netfront_driver
);
1901 module_init(netif_init
);
1904 static void __exit
netif_exit(void)
1906 if (xen_initial_domain())
1909 xenbus_unregister_driver(&netfront_driver
);
1911 module_exit(netif_exit
);
1913 MODULE_DESCRIPTION("Xen virtual network device frontend");
1914 MODULE_LICENSE("GPL");
1915 MODULE_ALIAS("xen:vif");
1916 MODULE_ALIAS("xennet");