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 struct netfront_rx_info
{
126 struct xen_netif_rx_response rx
;
127 struct xen_netif_extra_info extras
[XEN_NETIF_EXTRA_TYPE_MAX
- 1];
130 static void skb_entry_set_link(union skb_entry
*list
, unsigned short id
)
135 static int skb_entry_is_link(const union skb_entry
*list
)
137 BUILD_BUG_ON(sizeof(list
->skb
) != sizeof(list
->link
));
138 return (unsigned long)list
->skb
< PAGE_OFFSET
;
142 * Access macros for acquiring freeing slots in tx_skbs[].
145 static void add_id_to_freelist(unsigned *head
, union skb_entry
*list
,
148 skb_entry_set_link(&list
[id
], *head
);
152 static unsigned short get_id_from_freelist(unsigned *head
,
153 union skb_entry
*list
)
155 unsigned int id
= *head
;
156 *head
= list
[id
].link
;
160 static int xennet_rxidx(RING_IDX idx
)
162 return idx
& (NET_RX_RING_SIZE
- 1);
165 static struct sk_buff
*xennet_get_rx_skb(struct netfront_info
*np
,
168 int i
= xennet_rxidx(ri
);
169 struct sk_buff
*skb
= np
->rx_skbs
[i
];
170 np
->rx_skbs
[i
] = NULL
;
174 static grant_ref_t
xennet_get_rx_ref(struct netfront_info
*np
,
177 int i
= xennet_rxidx(ri
);
178 grant_ref_t ref
= np
->grant_rx_ref
[i
];
179 np
->grant_rx_ref
[i
] = GRANT_INVALID_REF
;
184 static int xennet_sysfs_addif(struct net_device
*netdev
);
185 static void xennet_sysfs_delif(struct net_device
*netdev
);
186 #else /* !CONFIG_SYSFS */
187 #define xennet_sysfs_addif(dev) (0)
188 #define xennet_sysfs_delif(dev) do { } while (0)
191 static int xennet_can_sg(struct net_device
*dev
)
193 return dev
->features
& NETIF_F_SG
;
197 static void rx_refill_timeout(unsigned long data
)
199 struct net_device
*dev
= (struct net_device
*)data
;
200 struct netfront_info
*np
= netdev_priv(dev
);
201 napi_schedule(&np
->napi
);
204 static int netfront_tx_slot_available(struct netfront_info
*np
)
206 return (np
->tx
.req_prod_pvt
- np
->tx
.rsp_cons
) <
207 (TX_MAX_TARGET
- MAX_SKB_FRAGS
- 2);
210 static void xennet_maybe_wake_tx(struct net_device
*dev
)
212 struct netfront_info
*np
= netdev_priv(dev
);
214 if (unlikely(netif_queue_stopped(dev
)) &&
215 netfront_tx_slot_available(np
) &&
216 likely(netif_running(dev
)))
217 netif_wake_queue(dev
);
220 static void xennet_alloc_rx_buffers(struct net_device
*dev
)
223 struct netfront_info
*np
= netdev_priv(dev
);
226 int i
, batch_target
, notify
;
227 RING_IDX req_prod
= np
->rx
.req_prod_pvt
;
231 struct xen_netif_rx_request
*req
;
233 if (unlikely(!netif_carrier_ok(dev
)))
237 * Allocate skbuffs greedily, even though we batch updates to the
238 * receive ring. This creates a less bursty demand on the memory
239 * allocator, so should reduce the chance of failed allocation requests
240 * both for ourself and for other kernel subsystems.
242 batch_target
= np
->rx_target
- (req_prod
- np
->rx
.rsp_cons
);
243 for (i
= skb_queue_len(&np
->rx_batch
); i
< batch_target
; i
++) {
244 skb
= __netdev_alloc_skb(dev
, RX_COPY_THRESHOLD
+ NET_IP_ALIGN
,
245 GFP_ATOMIC
| __GFP_NOWARN
);
249 /* Align ip header to a 16 bytes boundary */
250 skb_reserve(skb
, NET_IP_ALIGN
);
252 page
= alloc_page(GFP_ATOMIC
| __GFP_NOWARN
);
256 /* Any skbuffs queued for refill? Force them out. */
259 /* Could not allocate any skbuffs. Try again later. */
260 mod_timer(&np
->rx_refill_timer
,
265 skb_shinfo(skb
)->frags
[0].page
= page
;
266 skb_shinfo(skb
)->nr_frags
= 1;
267 __skb_queue_tail(&np
->rx_batch
, skb
);
270 /* Is the batch large enough to be worthwhile? */
271 if (i
< (np
->rx_target
/2)) {
272 if (req_prod
> np
->rx
.sring
->req_prod
)
277 /* Adjust our fill target if we risked running out of buffers. */
278 if (((req_prod
- np
->rx
.sring
->rsp_prod
) < (np
->rx_target
/ 4)) &&
279 ((np
->rx_target
*= 2) > np
->rx_max_target
))
280 np
->rx_target
= np
->rx_max_target
;
284 skb
= __skb_dequeue(&np
->rx_batch
);
290 id
= xennet_rxidx(req_prod
+ i
);
292 BUG_ON(np
->rx_skbs
[id
]);
293 np
->rx_skbs
[id
] = skb
;
295 ref
= gnttab_claim_grant_reference(&np
->gref_rx_head
);
296 BUG_ON((signed short)ref
< 0);
297 np
->grant_rx_ref
[id
] = ref
;
299 pfn
= page_to_pfn(skb_shinfo(skb
)->frags
[0].page
);
300 vaddr
= page_address(skb_shinfo(skb
)->frags
[0].page
);
302 req
= RING_GET_REQUEST(&np
->rx
, req_prod
+ i
);
303 gnttab_grant_foreign_access_ref(ref
,
304 np
->xbdev
->otherend_id
,
312 wmb(); /* barrier so backend seens requests */
314 /* Above is a suitable barrier to ensure backend will see requests. */
315 np
->rx
.req_prod_pvt
= req_prod
+ i
;
317 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np
->rx
, notify
);
319 notify_remote_via_irq(np
->netdev
->irq
);
322 static int xennet_open(struct net_device
*dev
)
324 struct netfront_info
*np
= netdev_priv(dev
);
326 napi_enable(&np
->napi
);
328 spin_lock_bh(&np
->rx_lock
);
329 if (netif_carrier_ok(dev
)) {
330 xennet_alloc_rx_buffers(dev
);
331 np
->rx
.sring
->rsp_event
= np
->rx
.rsp_cons
+ 1;
332 if (RING_HAS_UNCONSUMED_RESPONSES(&np
->rx
))
333 napi_schedule(&np
->napi
);
335 spin_unlock_bh(&np
->rx_lock
);
337 netif_start_queue(dev
);
342 static void xennet_tx_buf_gc(struct net_device
*dev
)
346 struct netfront_info
*np
= netdev_priv(dev
);
349 BUG_ON(!netif_carrier_ok(dev
));
352 prod
= np
->tx
.sring
->rsp_prod
;
353 rmb(); /* Ensure we see responses up to 'rp'. */
355 for (cons
= np
->tx
.rsp_cons
; cons
!= prod
; cons
++) {
356 struct xen_netif_tx_response
*txrsp
;
358 txrsp
= RING_GET_RESPONSE(&np
->tx
, cons
);
359 if (txrsp
->status
== NETIF_RSP_NULL
)
363 skb
= np
->tx_skbs
[id
].skb
;
364 if (unlikely(gnttab_query_foreign_access(
365 np
->grant_tx_ref
[id
]) != 0)) {
366 printk(KERN_ALERT
"xennet_tx_buf_gc: warning "
367 "-- grant still in use by backend "
371 gnttab_end_foreign_access_ref(
372 np
->grant_tx_ref
[id
], GNTMAP_readonly
);
373 gnttab_release_grant_reference(
374 &np
->gref_tx_head
, np
->grant_tx_ref
[id
]);
375 np
->grant_tx_ref
[id
] = GRANT_INVALID_REF
;
376 add_id_to_freelist(&np
->tx_skb_freelist
, np
->tx_skbs
, id
);
377 dev_kfree_skb_irq(skb
);
380 np
->tx
.rsp_cons
= prod
;
383 * Set a new event, then check for race with update of tx_cons.
384 * Note that it is essential to schedule a callback, no matter
385 * how few buffers are pending. Even if there is space in the
386 * transmit ring, higher layers may be blocked because too much
387 * data is outstanding: in such cases notification from Xen is
388 * likely to be the only kick that we'll get.
390 np
->tx
.sring
->rsp_event
=
391 prod
+ ((np
->tx
.sring
->req_prod
- prod
) >> 1) + 1;
392 mb(); /* update shared area */
393 } while ((cons
== prod
) && (prod
!= np
->tx
.sring
->rsp_prod
));
395 xennet_maybe_wake_tx(dev
);
398 static void xennet_make_frags(struct sk_buff
*skb
, struct net_device
*dev
,
399 struct xen_netif_tx_request
*tx
)
401 struct netfront_info
*np
= netdev_priv(dev
);
402 char *data
= skb
->data
;
404 RING_IDX prod
= np
->tx
.req_prod_pvt
;
405 int frags
= skb_shinfo(skb
)->nr_frags
;
406 unsigned int offset
= offset_in_page(data
);
407 unsigned int len
= skb_headlen(skb
);
412 /* While the header overlaps a page boundary (including being
413 larger than a page), split it it into page-sized chunks. */
414 while (len
> PAGE_SIZE
- offset
) {
415 tx
->size
= PAGE_SIZE
- offset
;
416 tx
->flags
|= NETTXF_more_data
;
421 id
= get_id_from_freelist(&np
->tx_skb_freelist
, np
->tx_skbs
);
422 np
->tx_skbs
[id
].skb
= skb_get(skb
);
423 tx
= RING_GET_REQUEST(&np
->tx
, prod
++);
425 ref
= gnttab_claim_grant_reference(&np
->gref_tx_head
);
426 BUG_ON((signed short)ref
< 0);
428 mfn
= virt_to_mfn(data
);
429 gnttab_grant_foreign_access_ref(ref
, np
->xbdev
->otherend_id
,
430 mfn
, GNTMAP_readonly
);
432 tx
->gref
= np
->grant_tx_ref
[id
] = ref
;
438 /* Grant backend access to each skb fragment page. */
439 for (i
= 0; i
< frags
; i
++) {
440 skb_frag_t
*frag
= skb_shinfo(skb
)->frags
+ i
;
442 tx
->flags
|= NETTXF_more_data
;
444 id
= get_id_from_freelist(&np
->tx_skb_freelist
, np
->tx_skbs
);
445 np
->tx_skbs
[id
].skb
= skb_get(skb
);
446 tx
= RING_GET_REQUEST(&np
->tx
, prod
++);
448 ref
= gnttab_claim_grant_reference(&np
->gref_tx_head
);
449 BUG_ON((signed short)ref
< 0);
451 mfn
= pfn_to_mfn(page_to_pfn(frag
->page
));
452 gnttab_grant_foreign_access_ref(ref
, np
->xbdev
->otherend_id
,
453 mfn
, GNTMAP_readonly
);
455 tx
->gref
= np
->grant_tx_ref
[id
] = ref
;
456 tx
->offset
= frag
->page_offset
;
457 tx
->size
= frag
->size
;
461 np
->tx
.req_prod_pvt
= prod
;
464 static int xennet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
467 struct netfront_info
*np
= netdev_priv(dev
);
468 struct xen_netif_tx_request
*tx
;
469 struct xen_netif_extra_info
*extra
;
470 char *data
= skb
->data
;
475 int frags
= skb_shinfo(skb
)->nr_frags
;
476 unsigned int offset
= offset_in_page(data
);
477 unsigned int len
= skb_headlen(skb
);
479 frags
+= DIV_ROUND_UP(offset
+ len
, PAGE_SIZE
);
480 if (unlikely(frags
> MAX_SKB_FRAGS
+ 1)) {
481 printk(KERN_ALERT
"xennet: skb rides the rocket: %d frags\n",
487 spin_lock_irq(&np
->tx_lock
);
489 if (unlikely(!netif_carrier_ok(dev
) ||
490 (frags
> 1 && !xennet_can_sg(dev
)) ||
491 netif_needs_gso(skb
, netif_skb_features(skb
)))) {
492 spin_unlock_irq(&np
->tx_lock
);
496 i
= np
->tx
.req_prod_pvt
;
498 id
= get_id_from_freelist(&np
->tx_skb_freelist
, np
->tx_skbs
);
499 np
->tx_skbs
[id
].skb
= skb
;
501 tx
= RING_GET_REQUEST(&np
->tx
, i
);
504 ref
= gnttab_claim_grant_reference(&np
->gref_tx_head
);
505 BUG_ON((signed short)ref
< 0);
506 mfn
= virt_to_mfn(data
);
507 gnttab_grant_foreign_access_ref(
508 ref
, np
->xbdev
->otherend_id
, mfn
, GNTMAP_readonly
);
509 tx
->gref
= np
->grant_tx_ref
[id
] = ref
;
515 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
517 tx
->flags
|= NETTXF_csum_blank
| NETTXF_data_validated
;
518 else if (skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
519 /* remote but checksummed. */
520 tx
->flags
|= NETTXF_data_validated
;
522 if (skb_shinfo(skb
)->gso_size
) {
523 struct xen_netif_extra_info
*gso
;
525 gso
= (struct xen_netif_extra_info
*)
526 RING_GET_REQUEST(&np
->tx
, ++i
);
529 extra
->flags
|= XEN_NETIF_EXTRA_FLAG_MORE
;
531 tx
->flags
|= NETTXF_extra_info
;
533 gso
->u
.gso
.size
= skb_shinfo(skb
)->gso_size
;
534 gso
->u
.gso
.type
= XEN_NETIF_GSO_TYPE_TCPV4
;
536 gso
->u
.gso
.features
= 0;
538 gso
->type
= XEN_NETIF_EXTRA_TYPE_GSO
;
543 np
->tx
.req_prod_pvt
= i
+ 1;
545 xennet_make_frags(skb
, dev
, tx
);
548 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np
->tx
, notify
);
550 notify_remote_via_irq(np
->netdev
->irq
);
552 dev
->stats
.tx_bytes
+= skb
->len
;
553 dev
->stats
.tx_packets
++;
555 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
556 xennet_tx_buf_gc(dev
);
558 if (!netfront_tx_slot_available(np
))
559 netif_stop_queue(dev
);
561 spin_unlock_irq(&np
->tx_lock
);
566 dev
->stats
.tx_dropped
++;
571 static int xennet_close(struct net_device
*dev
)
573 struct netfront_info
*np
= netdev_priv(dev
);
574 netif_stop_queue(np
->netdev
);
575 napi_disable(&np
->napi
);
579 static void xennet_move_rx_slot(struct netfront_info
*np
, struct sk_buff
*skb
,
582 int new = xennet_rxidx(np
->rx
.req_prod_pvt
);
584 BUG_ON(np
->rx_skbs
[new]);
585 np
->rx_skbs
[new] = skb
;
586 np
->grant_rx_ref
[new] = ref
;
587 RING_GET_REQUEST(&np
->rx
, np
->rx
.req_prod_pvt
)->id
= new;
588 RING_GET_REQUEST(&np
->rx
, np
->rx
.req_prod_pvt
)->gref
= ref
;
589 np
->rx
.req_prod_pvt
++;
592 static int xennet_get_extras(struct netfront_info
*np
,
593 struct xen_netif_extra_info
*extras
,
597 struct xen_netif_extra_info
*extra
;
598 struct device
*dev
= &np
->netdev
->dev
;
599 RING_IDX cons
= np
->rx
.rsp_cons
;
606 if (unlikely(cons
+ 1 == rp
)) {
608 dev_warn(dev
, "Missing extra info\n");
613 extra
= (struct xen_netif_extra_info
*)
614 RING_GET_RESPONSE(&np
->rx
, ++cons
);
616 if (unlikely(!extra
->type
||
617 extra
->type
>= XEN_NETIF_EXTRA_TYPE_MAX
)) {
619 dev_warn(dev
, "Invalid extra type: %d\n",
623 memcpy(&extras
[extra
->type
- 1], extra
,
627 skb
= xennet_get_rx_skb(np
, cons
);
628 ref
= xennet_get_rx_ref(np
, cons
);
629 xennet_move_rx_slot(np
, skb
, ref
);
630 } while (extra
->flags
& XEN_NETIF_EXTRA_FLAG_MORE
);
632 np
->rx
.rsp_cons
= cons
;
636 static int xennet_get_responses(struct netfront_info
*np
,
637 struct netfront_rx_info
*rinfo
, RING_IDX rp
,
638 struct sk_buff_head
*list
)
640 struct xen_netif_rx_response
*rx
= &rinfo
->rx
;
641 struct xen_netif_extra_info
*extras
= rinfo
->extras
;
642 struct device
*dev
= &np
->netdev
->dev
;
643 RING_IDX cons
= np
->rx
.rsp_cons
;
644 struct sk_buff
*skb
= xennet_get_rx_skb(np
, cons
);
645 grant_ref_t ref
= xennet_get_rx_ref(np
, cons
);
646 int max
= MAX_SKB_FRAGS
+ (rx
->status
<= RX_COPY_THRESHOLD
);
651 if (rx
->flags
& NETRXF_extra_info
) {
652 err
= xennet_get_extras(np
, extras
, rp
);
653 cons
= np
->rx
.rsp_cons
;
657 if (unlikely(rx
->status
< 0 ||
658 rx
->offset
+ rx
->status
> PAGE_SIZE
)) {
660 dev_warn(dev
, "rx->offset: %x, size: %u\n",
661 rx
->offset
, rx
->status
);
662 xennet_move_rx_slot(np
, skb
, ref
);
668 * This definitely indicates a bug, either in this driver or in
669 * the backend driver. In future this should flag the bad
670 * situation to the system controller to reboot the backed.
672 if (ref
== GRANT_INVALID_REF
) {
674 dev_warn(dev
, "Bad rx response id %d.\n",
680 ret
= gnttab_end_foreign_access_ref(ref
, 0);
683 gnttab_release_grant_reference(&np
->gref_rx_head
, ref
);
685 __skb_queue_tail(list
, skb
);
688 if (!(rx
->flags
& NETRXF_more_data
))
691 if (cons
+ frags
== rp
) {
693 dev_warn(dev
, "Need more frags\n");
698 rx
= RING_GET_RESPONSE(&np
->rx
, cons
+ frags
);
699 skb
= xennet_get_rx_skb(np
, cons
+ frags
);
700 ref
= xennet_get_rx_ref(np
, cons
+ frags
);
704 if (unlikely(frags
> max
)) {
706 dev_warn(dev
, "Too many frags\n");
711 np
->rx
.rsp_cons
= cons
+ frags
;
716 static int xennet_set_skb_gso(struct sk_buff
*skb
,
717 struct xen_netif_extra_info
*gso
)
719 if (!gso
->u
.gso
.size
) {
721 printk(KERN_WARNING
"GSO size must not be zero.\n");
725 /* Currently only TCPv4 S.O. is supported. */
726 if (gso
->u
.gso
.type
!= XEN_NETIF_GSO_TYPE_TCPV4
) {
728 printk(KERN_WARNING
"Bad GSO type %d.\n", gso
->u
.gso
.type
);
732 skb_shinfo(skb
)->gso_size
= gso
->u
.gso
.size
;
733 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
735 /* Header must be checked, and gso_segs computed. */
736 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
737 skb_shinfo(skb
)->gso_segs
= 0;
742 static RING_IDX
xennet_fill_frags(struct netfront_info
*np
,
744 struct sk_buff_head
*list
)
746 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
747 int nr_frags
= shinfo
->nr_frags
;
748 RING_IDX cons
= np
->rx
.rsp_cons
;
749 skb_frag_t
*frag
= shinfo
->frags
+ nr_frags
;
750 struct sk_buff
*nskb
;
752 while ((nskb
= __skb_dequeue(list
))) {
753 struct xen_netif_rx_response
*rx
=
754 RING_GET_RESPONSE(&np
->rx
, ++cons
);
756 frag
->page
= skb_shinfo(nskb
)->frags
[0].page
;
757 frag
->page_offset
= rx
->offset
;
758 frag
->size
= rx
->status
;
760 skb
->data_len
+= rx
->status
;
762 skb_shinfo(nskb
)->nr_frags
= 0;
769 shinfo
->nr_frags
= nr_frags
;
773 static int skb_checksum_setup(struct sk_buff
*skb
)
779 if (skb
->protocol
!= htons(ETH_P_IP
))
782 iph
= (void *)skb
->data
;
783 th
= skb
->data
+ 4 * iph
->ihl
;
784 if (th
>= skb_tail_pointer(skb
))
787 skb
->csum_start
= th
- skb
->head
;
788 switch (iph
->protocol
) {
790 skb
->csum_offset
= offsetof(struct tcphdr
, check
);
793 skb
->csum_offset
= offsetof(struct udphdr
, check
);
797 printk(KERN_ERR
"Attempting to checksum a non-"
798 "TCP/UDP packet, dropping a protocol"
799 " %d packet", iph
->protocol
);
803 if ((th
+ skb
->csum_offset
+ 2) > skb_tail_pointer(skb
))
812 static int handle_incoming_queue(struct net_device
*dev
,
813 struct sk_buff_head
*rxq
)
815 int packets_dropped
= 0;
818 while ((skb
= __skb_dequeue(rxq
)) != NULL
) {
819 struct page
*page
= NETFRONT_SKB_CB(skb
)->page
;
820 void *vaddr
= page_address(page
);
821 unsigned offset
= NETFRONT_SKB_CB(skb
)->offset
;
823 memcpy(skb
->data
, vaddr
+ offset
,
826 if (page
!= skb_shinfo(skb
)->frags
[0].page
)
829 /* Ethernet work: Delayed to here as it peeks the header. */
830 skb
->protocol
= eth_type_trans(skb
, dev
);
832 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
833 if (skb_checksum_setup(skb
)) {
836 dev
->stats
.rx_errors
++;
841 dev
->stats
.rx_packets
++;
842 dev
->stats
.rx_bytes
+= skb
->len
;
845 netif_receive_skb(skb
);
848 return packets_dropped
;
851 static int xennet_poll(struct napi_struct
*napi
, int budget
)
853 struct netfront_info
*np
= container_of(napi
, struct netfront_info
, napi
);
854 struct net_device
*dev
= np
->netdev
;
856 struct netfront_rx_info rinfo
;
857 struct xen_netif_rx_response
*rx
= &rinfo
.rx
;
858 struct xen_netif_extra_info
*extras
= rinfo
.extras
;
861 struct sk_buff_head rxq
;
862 struct sk_buff_head errq
;
863 struct sk_buff_head tmpq
;
868 spin_lock(&np
->rx_lock
);
870 skb_queue_head_init(&rxq
);
871 skb_queue_head_init(&errq
);
872 skb_queue_head_init(&tmpq
);
874 rp
= np
->rx
.sring
->rsp_prod
;
875 rmb(); /* Ensure we see queued responses up to 'rp'. */
879 while ((i
!= rp
) && (work_done
< budget
)) {
880 memcpy(rx
, RING_GET_RESPONSE(&np
->rx
, i
), sizeof(*rx
));
881 memset(extras
, 0, sizeof(rinfo
.extras
));
883 err
= xennet_get_responses(np
, &rinfo
, rp
, &tmpq
);
887 while ((skb
= __skb_dequeue(&tmpq
)))
888 __skb_queue_tail(&errq
, skb
);
889 dev
->stats
.rx_errors
++;
894 skb
= __skb_dequeue(&tmpq
);
896 if (extras
[XEN_NETIF_EXTRA_TYPE_GSO
- 1].type
) {
897 struct xen_netif_extra_info
*gso
;
898 gso
= &extras
[XEN_NETIF_EXTRA_TYPE_GSO
- 1];
900 if (unlikely(xennet_set_skb_gso(skb
, gso
))) {
901 __skb_queue_head(&tmpq
, skb
);
902 np
->rx
.rsp_cons
+= skb_queue_len(&tmpq
);
907 NETFRONT_SKB_CB(skb
)->page
= skb_shinfo(skb
)->frags
[0].page
;
908 NETFRONT_SKB_CB(skb
)->offset
= rx
->offset
;
911 if (len
> RX_COPY_THRESHOLD
)
912 len
= RX_COPY_THRESHOLD
;
915 if (rx
->status
> len
) {
916 skb_shinfo(skb
)->frags
[0].page_offset
=
918 skb_shinfo(skb
)->frags
[0].size
= rx
->status
- len
;
919 skb
->data_len
= rx
->status
- len
;
921 skb_shinfo(skb
)->frags
[0].page
= NULL
;
922 skb_shinfo(skb
)->nr_frags
= 0;
925 i
= xennet_fill_frags(np
, skb
, &tmpq
);
928 * Truesize approximates the size of true data plus
929 * any supervisor overheads. Adding hypervisor
930 * overheads has been shown to significantly reduce
931 * achievable bandwidth with the default receive
932 * buffer size. It is therefore not wise to account
935 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set
936 * to RX_COPY_THRESHOLD + the supervisor
937 * overheads. Here, we add the size of the data pulled
938 * in xennet_fill_frags().
940 * We also adjust for any unused space in the main
941 * data area by subtracting (RX_COPY_THRESHOLD -
942 * len). This is especially important with drivers
943 * which split incoming packets into header and data,
944 * using only 66 bytes of the main data area (see the
945 * e1000 driver for example.) On such systems,
946 * without this last adjustement, our achievable
947 * receive throughout using the standard receive
948 * buffer size was cut by 25%(!!!).
950 skb
->truesize
+= skb
->data_len
- (RX_COPY_THRESHOLD
- len
);
951 skb
->len
+= skb
->data_len
;
953 if (rx
->flags
& NETRXF_csum_blank
)
954 skb
->ip_summed
= CHECKSUM_PARTIAL
;
955 else if (rx
->flags
& NETRXF_data_validated
)
956 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
958 __skb_queue_tail(&rxq
, skb
);
960 np
->rx
.rsp_cons
= ++i
;
964 __skb_queue_purge(&errq
);
966 work_done
-= handle_incoming_queue(dev
, &rxq
);
968 /* If we get a callback with very few responses, reduce fill target. */
969 /* NB. Note exponential increase, linear decrease. */
970 if (((np
->rx
.req_prod_pvt
- np
->rx
.sring
->rsp_prod
) >
971 ((3*np
->rx_target
) / 4)) &&
972 (--np
->rx_target
< np
->rx_min_target
))
973 np
->rx_target
= np
->rx_min_target
;
975 xennet_alloc_rx_buffers(dev
);
977 if (work_done
< budget
) {
980 local_irq_save(flags
);
982 RING_FINAL_CHECK_FOR_RESPONSES(&np
->rx
, more_to_do
);
984 __napi_complete(napi
);
986 local_irq_restore(flags
);
989 spin_unlock(&np
->rx_lock
);
994 static int xennet_change_mtu(struct net_device
*dev
, int mtu
)
996 int max
= xennet_can_sg(dev
) ? 65535 - ETH_HLEN
: ETH_DATA_LEN
;
1004 static void xennet_release_tx_bufs(struct netfront_info
*np
)
1006 struct sk_buff
*skb
;
1009 for (i
= 0; i
< NET_TX_RING_SIZE
; i
++) {
1010 /* Skip over entries which are actually freelist references */
1011 if (skb_entry_is_link(&np
->tx_skbs
[i
]))
1014 skb
= np
->tx_skbs
[i
].skb
;
1015 gnttab_end_foreign_access_ref(np
->grant_tx_ref
[i
],
1017 gnttab_release_grant_reference(&np
->gref_tx_head
,
1018 np
->grant_tx_ref
[i
]);
1019 np
->grant_tx_ref
[i
] = GRANT_INVALID_REF
;
1020 add_id_to_freelist(&np
->tx_skb_freelist
, np
->tx_skbs
, i
);
1021 dev_kfree_skb_irq(skb
);
1025 static void xennet_release_rx_bufs(struct netfront_info
*np
)
1027 struct mmu_update
*mmu
= np
->rx_mmu
;
1028 struct multicall_entry
*mcl
= np
->rx_mcl
;
1029 struct sk_buff_head free_list
;
1030 struct sk_buff
*skb
;
1032 int xfer
= 0, noxfer
= 0, unused
= 0;
1035 dev_warn(&np
->netdev
->dev
, "%s: fix me for copying receiver.\n",
1039 skb_queue_head_init(&free_list
);
1041 spin_lock_bh(&np
->rx_lock
);
1043 for (id
= 0; id
< NET_RX_RING_SIZE
; id
++) {
1044 ref
= np
->grant_rx_ref
[id
];
1045 if (ref
== GRANT_INVALID_REF
) {
1050 skb
= np
->rx_skbs
[id
];
1051 mfn
= gnttab_end_foreign_transfer_ref(ref
);
1052 gnttab_release_grant_reference(&np
->gref_rx_head
, ref
);
1053 np
->grant_rx_ref
[id
] = GRANT_INVALID_REF
;
1056 skb_shinfo(skb
)->nr_frags
= 0;
1062 if (!xen_feature(XENFEAT_auto_translated_physmap
)) {
1063 /* Remap the page. */
1064 struct page
*page
= skb_shinfo(skb
)->frags
[0].page
;
1065 unsigned long pfn
= page_to_pfn(page
);
1066 void *vaddr
= page_address(page
);
1068 MULTI_update_va_mapping(mcl
, (unsigned long)vaddr
,
1069 mfn_pte(mfn
, PAGE_KERNEL
),
1072 mmu
->ptr
= ((u64
)mfn
<< PAGE_SHIFT
)
1073 | MMU_MACHPHYS_UPDATE
;
1077 set_phys_to_machine(pfn
, mfn
);
1079 __skb_queue_tail(&free_list
, skb
);
1083 dev_info(&np
->netdev
->dev
, "%s: %d xfer, %d noxfer, %d unused\n",
1084 __func__
, xfer
, noxfer
, unused
);
1087 if (!xen_feature(XENFEAT_auto_translated_physmap
)) {
1088 /* Do all the remapping work and M2P updates. */
1089 MULTI_mmu_update(mcl
, np
->rx_mmu
, mmu
- np
->rx_mmu
,
1092 HYPERVISOR_multicall(np
->rx_mcl
, mcl
- np
->rx_mcl
);
1096 __skb_queue_purge(&free_list
);
1098 spin_unlock_bh(&np
->rx_lock
);
1101 static void xennet_uninit(struct net_device
*dev
)
1103 struct netfront_info
*np
= netdev_priv(dev
);
1104 xennet_release_tx_bufs(np
);
1105 xennet_release_rx_bufs(np
);
1106 gnttab_free_grant_references(np
->gref_tx_head
);
1107 gnttab_free_grant_references(np
->gref_rx_head
);
1110 static const struct net_device_ops xennet_netdev_ops
= {
1111 .ndo_open
= xennet_open
,
1112 .ndo_uninit
= xennet_uninit
,
1113 .ndo_stop
= xennet_close
,
1114 .ndo_start_xmit
= xennet_start_xmit
,
1115 .ndo_change_mtu
= xennet_change_mtu
,
1116 .ndo_set_mac_address
= eth_mac_addr
,
1117 .ndo_validate_addr
= eth_validate_addr
,
1120 static struct net_device
* __devinit
xennet_create_dev(struct xenbus_device
*dev
)
1123 struct net_device
*netdev
;
1124 struct netfront_info
*np
;
1126 netdev
= alloc_etherdev(sizeof(struct netfront_info
));
1128 printk(KERN_WARNING
"%s> alloc_etherdev failed.\n",
1130 return ERR_PTR(-ENOMEM
);
1133 np
= netdev_priv(netdev
);
1136 spin_lock_init(&np
->tx_lock
);
1137 spin_lock_init(&np
->rx_lock
);
1139 skb_queue_head_init(&np
->rx_batch
);
1140 np
->rx_target
= RX_DFL_MIN_TARGET
;
1141 np
->rx_min_target
= RX_DFL_MIN_TARGET
;
1142 np
->rx_max_target
= RX_MAX_TARGET
;
1144 init_timer(&np
->rx_refill_timer
);
1145 np
->rx_refill_timer
.data
= (unsigned long)netdev
;
1146 np
->rx_refill_timer
.function
= rx_refill_timeout
;
1148 /* Initialise tx_skbs as a free chain containing every entry. */
1149 np
->tx_skb_freelist
= 0;
1150 for (i
= 0; i
< NET_TX_RING_SIZE
; i
++) {
1151 skb_entry_set_link(&np
->tx_skbs
[i
], i
+1);
1152 np
->grant_tx_ref
[i
] = GRANT_INVALID_REF
;
1155 /* Clear out rx_skbs */
1156 for (i
= 0; i
< NET_RX_RING_SIZE
; i
++) {
1157 np
->rx_skbs
[i
] = NULL
;
1158 np
->grant_rx_ref
[i
] = GRANT_INVALID_REF
;
1161 /* A grant for every tx ring slot */
1162 if (gnttab_alloc_grant_references(TX_MAX_TARGET
,
1163 &np
->gref_tx_head
) < 0) {
1164 printk(KERN_ALERT
"#### netfront can't alloc tx grant refs\n");
1168 /* A grant for every rx ring slot */
1169 if (gnttab_alloc_grant_references(RX_MAX_TARGET
,
1170 &np
->gref_rx_head
) < 0) {
1171 printk(KERN_ALERT
"#### netfront can't alloc rx grant refs\n");
1176 netdev
->netdev_ops
= &xennet_netdev_ops
;
1178 netif_napi_add(netdev
, &np
->napi
, xennet_poll
, 64);
1179 netdev
->features
= NETIF_F_IP_CSUM
;
1181 SET_ETHTOOL_OPS(netdev
, &xennet_ethtool_ops
);
1182 SET_NETDEV_DEV(netdev
, &dev
->dev
);
1184 np
->netdev
= netdev
;
1186 netif_carrier_off(netdev
);
1191 gnttab_free_grant_references(np
->gref_tx_head
);
1193 free_netdev(netdev
);
1194 return ERR_PTR(err
);
1198 * Entry point to this code when a new device is created. Allocate the basic
1199 * structures and the ring buffers for communication with the backend, and
1200 * inform the backend of the appropriate details for those.
1202 static int __devinit
netfront_probe(struct xenbus_device
*dev
,
1203 const struct xenbus_device_id
*id
)
1206 struct net_device
*netdev
;
1207 struct netfront_info
*info
;
1209 netdev
= xennet_create_dev(dev
);
1210 if (IS_ERR(netdev
)) {
1211 err
= PTR_ERR(netdev
);
1212 xenbus_dev_fatal(dev
, err
, "creating netdev");
1216 info
= netdev_priv(netdev
);
1217 dev_set_drvdata(&dev
->dev
, info
);
1219 err
= register_netdev(info
->netdev
);
1221 printk(KERN_WARNING
"%s: register_netdev err=%d\n",
1226 err
= xennet_sysfs_addif(info
->netdev
);
1228 unregister_netdev(info
->netdev
);
1229 printk(KERN_WARNING
"%s: add sysfs failed err=%d\n",
1237 free_netdev(netdev
);
1238 dev_set_drvdata(&dev
->dev
, NULL
);
1242 static void xennet_end_access(int ref
, void *page
)
1244 /* This frees the page as a side-effect */
1245 if (ref
!= GRANT_INVALID_REF
)
1246 gnttab_end_foreign_access(ref
, 0, (unsigned long)page
);
1249 static void xennet_disconnect_backend(struct netfront_info
*info
)
1251 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1252 spin_lock_bh(&info
->rx_lock
);
1253 spin_lock_irq(&info
->tx_lock
);
1254 netif_carrier_off(info
->netdev
);
1255 spin_unlock_irq(&info
->tx_lock
);
1256 spin_unlock_bh(&info
->rx_lock
);
1258 if (info
->netdev
->irq
)
1259 unbind_from_irqhandler(info
->netdev
->irq
, info
->netdev
);
1260 info
->evtchn
= info
->netdev
->irq
= 0;
1262 /* End access and free the pages */
1263 xennet_end_access(info
->tx_ring_ref
, info
->tx
.sring
);
1264 xennet_end_access(info
->rx_ring_ref
, info
->rx
.sring
);
1266 info
->tx_ring_ref
= GRANT_INVALID_REF
;
1267 info
->rx_ring_ref
= GRANT_INVALID_REF
;
1268 info
->tx
.sring
= NULL
;
1269 info
->rx
.sring
= NULL
;
1273 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1274 * driver restart. We tear down our netif structure and recreate it, but
1275 * leave the device-layer structures intact so that this is transparent to the
1276 * rest of the kernel.
1278 static int netfront_resume(struct xenbus_device
*dev
)
1280 struct netfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1282 dev_dbg(&dev
->dev
, "%s\n", dev
->nodename
);
1284 xennet_disconnect_backend(info
);
1288 static int xen_net_read_mac(struct xenbus_device
*dev
, u8 mac
[])
1290 char *s
, *e
, *macstr
;
1293 macstr
= s
= xenbus_read(XBT_NIL
, dev
->nodename
, "mac", NULL
);
1295 return PTR_ERR(macstr
);
1297 for (i
= 0; i
< ETH_ALEN
; i
++) {
1298 mac
[i
] = simple_strtoul(s
, &e
, 16);
1299 if ((s
== e
) || (*e
!= ((i
== ETH_ALEN
-1) ? '\0' : ':'))) {
1310 static irqreturn_t
xennet_interrupt(int irq
, void *dev_id
)
1312 struct net_device
*dev
= dev_id
;
1313 struct netfront_info
*np
= netdev_priv(dev
);
1314 unsigned long flags
;
1316 spin_lock_irqsave(&np
->tx_lock
, flags
);
1318 if (likely(netif_carrier_ok(dev
))) {
1319 xennet_tx_buf_gc(dev
);
1320 /* Under tx_lock: protects access to rx shared-ring indexes. */
1321 if (RING_HAS_UNCONSUMED_RESPONSES(&np
->rx
))
1322 napi_schedule(&np
->napi
);
1325 spin_unlock_irqrestore(&np
->tx_lock
, flags
);
1330 static int setup_netfront(struct xenbus_device
*dev
, struct netfront_info
*info
)
1332 struct xen_netif_tx_sring
*txs
;
1333 struct xen_netif_rx_sring
*rxs
;
1335 struct net_device
*netdev
= info
->netdev
;
1337 info
->tx_ring_ref
= GRANT_INVALID_REF
;
1338 info
->rx_ring_ref
= GRANT_INVALID_REF
;
1339 info
->rx
.sring
= NULL
;
1340 info
->tx
.sring
= NULL
;
1343 err
= xen_net_read_mac(dev
, netdev
->dev_addr
);
1345 xenbus_dev_fatal(dev
, err
, "parsing %s/mac", dev
->nodename
);
1349 txs
= (struct xen_netif_tx_sring
*)get_zeroed_page(GFP_NOIO
| __GFP_HIGH
);
1352 xenbus_dev_fatal(dev
, err
, "allocating tx ring page");
1355 SHARED_RING_INIT(txs
);
1356 FRONT_RING_INIT(&info
->tx
, txs
, PAGE_SIZE
);
1358 err
= xenbus_grant_ring(dev
, virt_to_mfn(txs
));
1360 free_page((unsigned long)txs
);
1364 info
->tx_ring_ref
= err
;
1365 rxs
= (struct xen_netif_rx_sring
*)get_zeroed_page(GFP_NOIO
| __GFP_HIGH
);
1368 xenbus_dev_fatal(dev
, err
, "allocating rx ring page");
1371 SHARED_RING_INIT(rxs
);
1372 FRONT_RING_INIT(&info
->rx
, rxs
, PAGE_SIZE
);
1374 err
= xenbus_grant_ring(dev
, virt_to_mfn(rxs
));
1376 free_page((unsigned long)rxs
);
1379 info
->rx_ring_ref
= err
;
1381 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
1385 err
= bind_evtchn_to_irqhandler(info
->evtchn
, xennet_interrupt
,
1386 IRQF_SAMPLE_RANDOM
, netdev
->name
,
1397 /* Common code used when first setting up, and when resuming. */
1398 static int talk_to_netback(struct xenbus_device
*dev
,
1399 struct netfront_info
*info
)
1401 const char *message
;
1402 struct xenbus_transaction xbt
;
1405 /* Create shared ring, alloc event channel. */
1406 err
= setup_netfront(dev
, info
);
1411 err
= xenbus_transaction_start(&xbt
);
1413 xenbus_dev_fatal(dev
, err
, "starting transaction");
1417 err
= xenbus_printf(xbt
, dev
->nodename
, "tx-ring-ref", "%u",
1420 message
= "writing tx ring-ref";
1421 goto abort_transaction
;
1423 err
= xenbus_printf(xbt
, dev
->nodename
, "rx-ring-ref", "%u",
1426 message
= "writing rx ring-ref";
1427 goto abort_transaction
;
1429 err
= xenbus_printf(xbt
, dev
->nodename
,
1430 "event-channel", "%u", info
->evtchn
);
1432 message
= "writing event-channel";
1433 goto abort_transaction
;
1436 err
= xenbus_printf(xbt
, dev
->nodename
, "request-rx-copy", "%u",
1439 message
= "writing request-rx-copy";
1440 goto abort_transaction
;
1443 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-rx-notify", "%d", 1);
1445 message
= "writing feature-rx-notify";
1446 goto abort_transaction
;
1449 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-sg", "%d", 1);
1451 message
= "writing feature-sg";
1452 goto abort_transaction
;
1455 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-gso-tcpv4", "%d", 1);
1457 message
= "writing feature-gso-tcpv4";
1458 goto abort_transaction
;
1461 err
= xenbus_transaction_end(xbt
, 0);
1465 xenbus_dev_fatal(dev
, err
, "completing transaction");
1472 xenbus_transaction_end(xbt
, 1);
1473 xenbus_dev_fatal(dev
, err
, "%s", message
);
1475 xennet_disconnect_backend(info
);
1480 static int xennet_set_sg(struct net_device
*dev
, u32 data
)
1483 struct netfront_info
*np
= netdev_priv(dev
);
1486 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
, "feature-sg",
1491 } else if (dev
->mtu
> ETH_DATA_LEN
)
1492 dev
->mtu
= ETH_DATA_LEN
;
1494 return ethtool_op_set_sg(dev
, data
);
1497 static int xennet_set_tso(struct net_device
*dev
, u32 data
)
1500 struct netfront_info
*np
= netdev_priv(dev
);
1503 if (xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1504 "feature-gso-tcpv4", "%d", &val
) < 0)
1510 return ethtool_op_set_tso(dev
, data
);
1513 static void xennet_set_features(struct net_device
*dev
)
1515 /* Turn off all GSO bits except ROBUST. */
1516 dev
->features
&= ~NETIF_F_GSO_MASK
;
1517 dev
->features
|= NETIF_F_GSO_ROBUST
;
1518 xennet_set_sg(dev
, 0);
1520 /* We need checksum offload to enable scatter/gather and TSO. */
1521 if (!(dev
->features
& NETIF_F_IP_CSUM
))
1524 if (!xennet_set_sg(dev
, 1))
1525 xennet_set_tso(dev
, 1);
1528 static int xennet_connect(struct net_device
*dev
)
1530 struct netfront_info
*np
= netdev_priv(dev
);
1531 int i
, requeue_idx
, err
;
1532 struct sk_buff
*skb
;
1534 struct xen_netif_rx_request
*req
;
1535 unsigned int feature_rx_copy
;
1537 err
= xenbus_scanf(XBT_NIL
, np
->xbdev
->otherend
,
1538 "feature-rx-copy", "%u", &feature_rx_copy
);
1540 feature_rx_copy
= 0;
1542 if (!feature_rx_copy
) {
1544 "backend does not support copying receive path\n");
1548 err
= talk_to_netback(np
->xbdev
, np
);
1552 xennet_set_features(dev
);
1554 spin_lock_bh(&np
->rx_lock
);
1555 spin_lock_irq(&np
->tx_lock
);
1557 /* Step 1: Discard all pending TX packet fragments. */
1558 xennet_release_tx_bufs(np
);
1560 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1561 for (requeue_idx
= 0, i
= 0; i
< NET_RX_RING_SIZE
; i
++) {
1562 if (!np
->rx_skbs
[i
])
1565 skb
= np
->rx_skbs
[requeue_idx
] = xennet_get_rx_skb(np
, i
);
1566 ref
= np
->grant_rx_ref
[requeue_idx
] = xennet_get_rx_ref(np
, i
);
1567 req
= RING_GET_REQUEST(&np
->rx
, requeue_idx
);
1569 gnttab_grant_foreign_access_ref(
1570 ref
, np
->xbdev
->otherend_id
,
1571 pfn_to_mfn(page_to_pfn(skb_shinfo(skb
)->
1575 req
->id
= requeue_idx
;
1580 np
->rx
.req_prod_pvt
= requeue_idx
;
1583 * Step 3: All public and private state should now be sane. Get
1584 * ready to start sending and receiving packets and give the driver
1585 * domain a kick because we've probably just requeued some
1588 netif_carrier_on(np
->netdev
);
1589 notify_remote_via_irq(np
->netdev
->irq
);
1590 xennet_tx_buf_gc(dev
);
1591 xennet_alloc_rx_buffers(dev
);
1593 spin_unlock_irq(&np
->tx_lock
);
1594 spin_unlock_bh(&np
->rx_lock
);
1600 * Callback received when the backend's state changes.
1602 static void netback_changed(struct xenbus_device
*dev
,
1603 enum xenbus_state backend_state
)
1605 struct netfront_info
*np
= dev_get_drvdata(&dev
->dev
);
1606 struct net_device
*netdev
= np
->netdev
;
1608 dev_dbg(&dev
->dev
, "%s\n", xenbus_strstate(backend_state
));
1610 switch (backend_state
) {
1611 case XenbusStateInitialising
:
1612 case XenbusStateInitialised
:
1613 case XenbusStateReconfiguring
:
1614 case XenbusStateReconfigured
:
1615 case XenbusStateConnected
:
1616 case XenbusStateUnknown
:
1617 case XenbusStateClosed
:
1620 case XenbusStateInitWait
:
1621 if (dev
->state
!= XenbusStateInitialising
)
1623 if (xennet_connect(netdev
) != 0)
1625 xenbus_switch_state(dev
, XenbusStateConnected
);
1626 netif_notify_peers(netdev
);
1629 case XenbusStateClosing
:
1630 xenbus_frontend_closed(dev
);
1635 static const struct ethtool_ops xennet_ethtool_ops
=
1637 .set_tx_csum
= ethtool_op_set_tx_csum
,
1638 .set_sg
= xennet_set_sg
,
1639 .set_tso
= xennet_set_tso
,
1640 .get_link
= ethtool_op_get_link
,
1644 static ssize_t
show_rxbuf_min(struct device
*dev
,
1645 struct device_attribute
*attr
, char *buf
)
1647 struct net_device
*netdev
= to_net_dev(dev
);
1648 struct netfront_info
*info
= netdev_priv(netdev
);
1650 return sprintf(buf
, "%u\n", info
->rx_min_target
);
1653 static ssize_t
store_rxbuf_min(struct device
*dev
,
1654 struct device_attribute
*attr
,
1655 const char *buf
, size_t len
)
1657 struct net_device
*netdev
= to_net_dev(dev
);
1658 struct netfront_info
*np
= netdev_priv(netdev
);
1660 unsigned long target
;
1662 if (!capable(CAP_NET_ADMIN
))
1665 target
= simple_strtoul(buf
, &endp
, 0);
1669 if (target
< RX_MIN_TARGET
)
1670 target
= RX_MIN_TARGET
;
1671 if (target
> RX_MAX_TARGET
)
1672 target
= RX_MAX_TARGET
;
1674 spin_lock_bh(&np
->rx_lock
);
1675 if (target
> np
->rx_max_target
)
1676 np
->rx_max_target
= target
;
1677 np
->rx_min_target
= target
;
1678 if (target
> np
->rx_target
)
1679 np
->rx_target
= target
;
1681 xennet_alloc_rx_buffers(netdev
);
1683 spin_unlock_bh(&np
->rx_lock
);
1687 static ssize_t
show_rxbuf_max(struct device
*dev
,
1688 struct device_attribute
*attr
, char *buf
)
1690 struct net_device
*netdev
= to_net_dev(dev
);
1691 struct netfront_info
*info
= netdev_priv(netdev
);
1693 return sprintf(buf
, "%u\n", info
->rx_max_target
);
1696 static ssize_t
store_rxbuf_max(struct device
*dev
,
1697 struct device_attribute
*attr
,
1698 const char *buf
, size_t len
)
1700 struct net_device
*netdev
= to_net_dev(dev
);
1701 struct netfront_info
*np
= netdev_priv(netdev
);
1703 unsigned long target
;
1705 if (!capable(CAP_NET_ADMIN
))
1708 target
= simple_strtoul(buf
, &endp
, 0);
1712 if (target
< RX_MIN_TARGET
)
1713 target
= RX_MIN_TARGET
;
1714 if (target
> RX_MAX_TARGET
)
1715 target
= RX_MAX_TARGET
;
1717 spin_lock_bh(&np
->rx_lock
);
1718 if (target
< np
->rx_min_target
)
1719 np
->rx_min_target
= target
;
1720 np
->rx_max_target
= target
;
1721 if (target
< np
->rx_target
)
1722 np
->rx_target
= target
;
1724 xennet_alloc_rx_buffers(netdev
);
1726 spin_unlock_bh(&np
->rx_lock
);
1730 static ssize_t
show_rxbuf_cur(struct device
*dev
,
1731 struct device_attribute
*attr
, char *buf
)
1733 struct net_device
*netdev
= to_net_dev(dev
);
1734 struct netfront_info
*info
= netdev_priv(netdev
);
1736 return sprintf(buf
, "%u\n", info
->rx_target
);
1739 static struct device_attribute xennet_attrs
[] = {
1740 __ATTR(rxbuf_min
, S_IRUGO
|S_IWUSR
, show_rxbuf_min
, store_rxbuf_min
),
1741 __ATTR(rxbuf_max
, S_IRUGO
|S_IWUSR
, show_rxbuf_max
, store_rxbuf_max
),
1742 __ATTR(rxbuf_cur
, S_IRUGO
, show_rxbuf_cur
, NULL
),
1745 static int xennet_sysfs_addif(struct net_device
*netdev
)
1750 for (i
= 0; i
< ARRAY_SIZE(xennet_attrs
); i
++) {
1751 err
= device_create_file(&netdev
->dev
,
1760 device_remove_file(&netdev
->dev
, &xennet_attrs
[i
]);
1764 static void xennet_sysfs_delif(struct net_device
*netdev
)
1768 for (i
= 0; i
< ARRAY_SIZE(xennet_attrs
); i
++)
1769 device_remove_file(&netdev
->dev
, &xennet_attrs
[i
]);
1772 #endif /* CONFIG_SYSFS */
1774 static struct xenbus_device_id netfront_ids
[] = {
1780 static int __devexit
xennet_remove(struct xenbus_device
*dev
)
1782 struct netfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1784 dev_dbg(&dev
->dev
, "%s\n", dev
->nodename
);
1786 unregister_netdev(info
->netdev
);
1788 xennet_disconnect_backend(info
);
1790 del_timer_sync(&info
->rx_refill_timer
);
1792 xennet_sysfs_delif(info
->netdev
);
1794 free_netdev(info
->netdev
);
1799 static struct xenbus_driver netfront_driver
= {
1801 .owner
= THIS_MODULE
,
1802 .ids
= netfront_ids
,
1803 .probe
= netfront_probe
,
1804 .remove
= __devexit_p(xennet_remove
),
1805 .resume
= netfront_resume
,
1806 .otherend_changed
= netback_changed
,
1809 static int __init
netif_init(void)
1814 if (xen_initial_domain())
1817 printk(KERN_INFO
"Initialising Xen virtual ethernet driver.\n");
1819 return xenbus_register_frontend(&netfront_driver
);
1821 module_init(netif_init
);
1824 static void __exit
netif_exit(void)
1826 if (xen_initial_domain())
1829 xenbus_unregister_driver(&netfront_driver
);
1831 module_exit(netif_exit
);
1833 MODULE_DESCRIPTION("Xen virtual network device frontend");
1834 MODULE_LICENSE("GPL");
1835 MODULE_ALIAS("xen:vif");
1836 MODULE_ALIAS("xennet");