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>
45 #include <xen/xenbus.h>
46 #include <xen/events.h>
48 #include <xen/grant_table.h>
50 #include <xen/interface/io/netif.h>
51 #include <xen/interface/memory.h>
52 #include <xen/interface/grant_table.h>
54 static struct ethtool_ops xennet_ethtool_ops;
61 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
63 #define RX_COPY_THRESHOLD 256
65 #define GRANT_INVALID_REF 0
67 #define NET_TX_RING_SIZE __RING_SIZE((struct xen_netif_tx_sring *)0, PAGE_SIZE)
68 #define NET_RX_RING_SIZE __RING_SIZE((struct xen_netif_rx_sring *)0, PAGE_SIZE)
69 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
71 struct netfront_info {
72 struct list_head list;
73 struct net_device *netdev;
75 struct net_device_stats stats;
77 struct xen_netif_tx_front_ring tx;
78 struct xen_netif_rx_front_ring rx;
85 /* Receive-ring batched refills. */
86 #define RX_MIN_TARGET 8
87 #define RX_DFL_MIN_TARGET 64
88 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
89 unsigned rx_min_target, rx_max_target, rx_target;
90 struct sk_buff_head rx_batch;
92 struct timer_list rx_refill_timer;
95 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
96 * are linked from tx_skb_freelist through skb_entry.link.
98 * NB. Freelist index entries are always going to be less than
99 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
100 * greater than PAGE_OFFSET: we use this property to distinguish
106 } tx_skbs[NET_TX_RING_SIZE];
107 grant_ref_t gref_tx_head;
108 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
109 unsigned tx_skb_freelist;
111 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
112 grant_ref_t gref_rx_head;
113 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
115 struct xenbus_device *xbdev;
119 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
120 struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
121 struct mmu_update rx_mmu[NET_RX_RING_SIZE];
124 struct netfront_rx_info {
125 struct xen_netif_rx_response rx;
126 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
130 * Access macros for acquiring freeing slots in tx_skbs[].
133 static void add_id_to_freelist(unsigned *head, union skb_entry *list,
136 list[id].link = *head;
140 static unsigned short get_id_from_freelist(unsigned *head,
141 union skb_entry *list)
143 unsigned int id = *head;
144 *head = list[id].link;
148 static int xennet_rxidx(RING_IDX idx)
150 return idx & (NET_RX_RING_SIZE - 1);
153 static struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
156 int i = xennet_rxidx(ri);
157 struct sk_buff *skb = np->rx_skbs[i];
158 np->rx_skbs[i] = NULL;
162 static grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
165 int i = xennet_rxidx(ri);
166 grant_ref_t ref = np->grant_rx_ref[i];
167 np->grant_rx_ref[i] = GRANT_INVALID_REF;
172 static int xennet_sysfs_addif(struct net_device *netdev);
173 static void xennet_sysfs_delif(struct net_device *netdev);
174 #else /* !CONFIG_SYSFS */
175 #define xennet_sysfs_addif(dev) (0)
176 #define xennet_sysfs_delif(dev) do { } while (0)
179 static int xennet_can_sg(struct net_device *dev)
181 return dev->features & NETIF_F_SG;
185 static void rx_refill_timeout(unsigned long data)
187 struct net_device *dev = (struct net_device *)data;
188 netif_rx_schedule(dev);
191 static int netfront_tx_slot_available(struct netfront_info *np)
193 return ((np->tx.req_prod_pvt - np->tx.rsp_cons) <
194 (TX_MAX_TARGET - MAX_SKB_FRAGS - 2));
197 static void xennet_maybe_wake_tx(struct net_device *dev)
199 struct netfront_info *np = netdev_priv(dev);
201 if (unlikely(netif_queue_stopped(dev)) &&
202 netfront_tx_slot_available(np) &&
203 likely(netif_running(dev)))
204 netif_wake_queue(dev);
207 static void xennet_alloc_rx_buffers(struct net_device *dev)
210 struct netfront_info *np = netdev_priv(dev);
213 int i, batch_target, notify;
214 RING_IDX req_prod = np->rx.req_prod_pvt;
215 struct xen_memory_reservation reservation;
220 struct xen_netif_rx_request *req;
222 if (unlikely(!netif_carrier_ok(dev)))
226 * Allocate skbuffs greedily, even though we batch updates to the
227 * receive ring. This creates a less bursty demand on the memory
228 * allocator, so should reduce the chance of failed allocation requests
229 * both for ourself and for other kernel subsystems.
231 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
232 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
233 skb = __netdev_alloc_skb(dev, RX_COPY_THRESHOLD,
234 GFP_ATOMIC | __GFP_NOWARN);
238 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
242 /* Any skbuffs queued for refill? Force them out. */
245 /* Could not allocate any skbuffs. Try again later. */
246 mod_timer(&np->rx_refill_timer,
251 skb_shinfo(skb)->frags[0].page = page;
252 skb_shinfo(skb)->nr_frags = 1;
253 __skb_queue_tail(&np->rx_batch, skb);
256 /* Is the batch large enough to be worthwhile? */
257 if (i < (np->rx_target/2)) {
258 if (req_prod > np->rx.sring->req_prod)
263 /* Adjust our fill target if we risked running out of buffers. */
264 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
265 ((np->rx_target *= 2) > np->rx_max_target))
266 np->rx_target = np->rx_max_target;
269 for (nr_flips = i = 0; ; i++) {
270 skb = __skb_dequeue(&np->rx_batch);
276 id = xennet_rxidx(req_prod + i);
278 BUG_ON(np->rx_skbs[id]);
279 np->rx_skbs[id] = skb;
281 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
282 BUG_ON((signed short)ref < 0);
283 np->grant_rx_ref[id] = ref;
285 pfn = page_to_pfn(skb_shinfo(skb)->frags[0].page);
286 vaddr = page_address(skb_shinfo(skb)->frags[0].page);
288 req = RING_GET_REQUEST(&np->rx, req_prod + i);
289 gnttab_grant_foreign_access_ref(ref,
290 np->xbdev->otherend_id,
299 reservation.extent_start = np->rx_pfn_array;
300 reservation.nr_extents = nr_flips;
301 reservation.extent_order = 0;
302 reservation.address_bits = 0;
303 reservation.domid = DOMID_SELF;
305 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
306 /* After all PTEs have been zapped, flush the TLB. */
307 np->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
308 UVMF_TLB_FLUSH|UVMF_ALL;
310 /* Give away a batch of pages. */
311 np->rx_mcl[i].op = __HYPERVISOR_memory_op;
312 np->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
313 np->rx_mcl[i].args[1] = (unsigned long)&reservation;
315 /* Zap PTEs and give away pages in one big
317 (void)HYPERVISOR_multicall(np->rx_mcl, i+1);
319 /* Check return status of HYPERVISOR_memory_op(). */
320 if (unlikely(np->rx_mcl[i].result != i))
321 panic("Unable to reduce memory reservation\n");
323 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
325 panic("Unable to reduce memory reservation\n");
328 wmb(); /* barrier so backend seens requests */
331 /* Above is a suitable barrier to ensure backend will see requests. */
332 np->rx.req_prod_pvt = req_prod + i;
334 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
336 notify_remote_via_irq(np->netdev->irq);
339 static int xennet_open(struct net_device *dev)
341 struct netfront_info *np = netdev_priv(dev);
343 memset(&np->stats, 0, sizeof(np->stats));
345 spin_lock_bh(&np->rx_lock);
346 if (netif_carrier_ok(dev)) {
347 xennet_alloc_rx_buffers(dev);
348 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
349 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
350 netif_rx_schedule(dev);
352 spin_unlock_bh(&np->rx_lock);
354 xennet_maybe_wake_tx(dev);
359 static void xennet_tx_buf_gc(struct net_device *dev)
363 struct netfront_info *np = netdev_priv(dev);
366 BUG_ON(!netif_carrier_ok(dev));
369 prod = np->tx.sring->rsp_prod;
370 rmb(); /* Ensure we see responses up to 'rp'. */
372 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
373 struct xen_netif_tx_response *txrsp;
375 txrsp = RING_GET_RESPONSE(&np->tx, cons);
376 if (txrsp->status == NETIF_RSP_NULL)
380 skb = np->tx_skbs[id].skb;
381 if (unlikely(gnttab_query_foreign_access(
382 np->grant_tx_ref[id]) != 0)) {
383 printk(KERN_ALERT "xennet_tx_buf_gc: warning "
384 "-- grant still in use by backend "
388 gnttab_end_foreign_access_ref(
389 np->grant_tx_ref[id], GNTMAP_readonly);
390 gnttab_release_grant_reference(
391 &np->gref_tx_head, np->grant_tx_ref[id]);
392 np->grant_tx_ref[id] = GRANT_INVALID_REF;
393 add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, id);
394 dev_kfree_skb_irq(skb);
397 np->tx.rsp_cons = prod;
400 * Set a new event, then check for race with update of tx_cons.
401 * Note that it is essential to schedule a callback, no matter
402 * how few buffers are pending. Even if there is space in the
403 * transmit ring, higher layers may be blocked because too much
404 * data is outstanding: in such cases notification from Xen is
405 * likely to be the only kick that we'll get.
407 np->tx.sring->rsp_event =
408 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
409 mb(); /* update shared area */
410 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
412 xennet_maybe_wake_tx(dev);
415 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
416 struct xen_netif_tx_request *tx)
418 struct netfront_info *np = netdev_priv(dev);
419 char *data = skb->data;
421 RING_IDX prod = np->tx.req_prod_pvt;
422 int frags = skb_shinfo(skb)->nr_frags;
423 unsigned int offset = offset_in_page(data);
424 unsigned int len = skb_headlen(skb);
429 /* While the header overlaps a page boundary (including being
430 larger than a page), split it it into page-sized chunks. */
431 while (len > PAGE_SIZE - offset) {
432 tx->size = PAGE_SIZE - offset;
433 tx->flags |= NETTXF_more_data;
438 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
439 np->tx_skbs[id].skb = skb_get(skb);
440 tx = RING_GET_REQUEST(&np->tx, prod++);
442 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
443 BUG_ON((signed short)ref < 0);
445 mfn = virt_to_mfn(data);
446 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
447 mfn, GNTMAP_readonly);
449 tx->gref = np->grant_tx_ref[id] = ref;
455 /* Grant backend access to each skb fragment page. */
456 for (i = 0; i < frags; i++) {
457 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
459 tx->flags |= NETTXF_more_data;
461 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
462 np->tx_skbs[id].skb = skb_get(skb);
463 tx = RING_GET_REQUEST(&np->tx, prod++);
465 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
466 BUG_ON((signed short)ref < 0);
468 mfn = pfn_to_mfn(page_to_pfn(frag->page));
469 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
470 mfn, GNTMAP_readonly);
472 tx->gref = np->grant_tx_ref[id] = ref;
473 tx->offset = frag->page_offset;
474 tx->size = frag->size;
478 np->tx.req_prod_pvt = prod;
481 static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
484 struct netfront_info *np = netdev_priv(dev);
485 struct xen_netif_tx_request *tx;
486 struct xen_netif_extra_info *extra;
487 char *data = skb->data;
492 int frags = skb_shinfo(skb)->nr_frags;
493 unsigned int offset = offset_in_page(data);
494 unsigned int len = skb_headlen(skb);
496 frags += (offset + len + PAGE_SIZE - 1) / PAGE_SIZE;
497 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
498 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
504 spin_lock_irq(&np->tx_lock);
506 if (unlikely(!netif_carrier_ok(dev) ||
507 (frags > 1 && !xennet_can_sg(dev)) ||
508 netif_needs_gso(dev, skb))) {
509 spin_unlock_irq(&np->tx_lock);
513 i = np->tx.req_prod_pvt;
515 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
516 np->tx_skbs[id].skb = skb;
518 tx = RING_GET_REQUEST(&np->tx, i);
521 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
522 BUG_ON((signed short)ref < 0);
523 mfn = virt_to_mfn(data);
524 gnttab_grant_foreign_access_ref(
525 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
526 tx->gref = np->grant_tx_ref[id] = ref;
532 if (skb->ip_summed == CHECKSUM_PARTIAL)
534 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
535 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
536 /* remote but checksummed. */
537 tx->flags |= NETTXF_data_validated;
539 if (skb_shinfo(skb)->gso_size) {
540 struct xen_netif_extra_info *gso;
542 gso = (struct xen_netif_extra_info *)
543 RING_GET_REQUEST(&np->tx, ++i);
546 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
548 tx->flags |= NETTXF_extra_info;
550 gso->u.gso.size = skb_shinfo(skb)->gso_size;
551 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
553 gso->u.gso.features = 0;
555 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
560 np->tx.req_prod_pvt = i + 1;
562 xennet_make_frags(skb, dev, tx);
565 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
567 notify_remote_via_irq(np->netdev->irq);
569 np->stats.tx_bytes += skb->len;
570 np->stats.tx_packets++;
572 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
573 xennet_tx_buf_gc(dev);
575 if (!netfront_tx_slot_available(np))
576 netif_stop_queue(dev);
578 spin_unlock_irq(&np->tx_lock);
583 np->stats.tx_dropped++;
588 static int xennet_close(struct net_device *dev)
590 struct netfront_info *np = netdev_priv(dev);
591 netif_stop_queue(np->netdev);
595 static struct net_device_stats *xennet_get_stats(struct net_device *dev)
597 struct netfront_info *np = netdev_priv(dev);
601 static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
604 int new = xennet_rxidx(np->rx.req_prod_pvt);
606 BUG_ON(np->rx_skbs[new]);
607 np->rx_skbs[new] = skb;
608 np->grant_rx_ref[new] = ref;
609 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
610 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
611 np->rx.req_prod_pvt++;
614 static int xennet_get_extras(struct netfront_info *np,
615 struct xen_netif_extra_info *extras,
619 struct xen_netif_extra_info *extra;
620 struct device *dev = &np->netdev->dev;
621 RING_IDX cons = np->rx.rsp_cons;
628 if (unlikely(cons + 1 == rp)) {
630 dev_warn(dev, "Missing extra info\n");
635 extra = (struct xen_netif_extra_info *)
636 RING_GET_RESPONSE(&np->rx, ++cons);
638 if (unlikely(!extra->type ||
639 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
641 dev_warn(dev, "Invalid extra type: %d\n",
645 memcpy(&extras[extra->type - 1], extra,
649 skb = xennet_get_rx_skb(np, cons);
650 ref = xennet_get_rx_ref(np, cons);
651 xennet_move_rx_slot(np, skb, ref);
652 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
654 np->rx.rsp_cons = cons;
658 static int xennet_get_responses(struct netfront_info *np,
659 struct netfront_rx_info *rinfo, RING_IDX rp,
660 struct sk_buff_head *list)
662 struct xen_netif_rx_response *rx = &rinfo->rx;
663 struct xen_netif_extra_info *extras = rinfo->extras;
664 struct device *dev = &np->netdev->dev;
665 RING_IDX cons = np->rx.rsp_cons;
666 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
667 grant_ref_t ref = xennet_get_rx_ref(np, cons);
668 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
673 if (rx->flags & NETRXF_extra_info) {
674 err = xennet_get_extras(np, extras, rp);
675 cons = np->rx.rsp_cons;
679 if (unlikely(rx->status < 0 ||
680 rx->offset + rx->status > PAGE_SIZE)) {
682 dev_warn(dev, "rx->offset: %x, size: %u\n",
683 rx->offset, rx->status);
684 xennet_move_rx_slot(np, skb, ref);
690 * This definitely indicates a bug, either in this driver or in
691 * the backend driver. In future this should flag the bad
692 * situation to the system controller to reboot the backed.
694 if (ref == GRANT_INVALID_REF) {
696 dev_warn(dev, "Bad rx response id %d.\n",
702 ret = gnttab_end_foreign_access_ref(ref, 0);
705 gnttab_release_grant_reference(&np->gref_rx_head, ref);
707 __skb_queue_tail(list, skb);
710 if (!(rx->flags & NETRXF_more_data))
713 if (cons + frags == rp) {
715 dev_warn(dev, "Need more frags\n");
720 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
721 skb = xennet_get_rx_skb(np, cons + frags);
722 ref = xennet_get_rx_ref(np, cons + frags);
726 if (unlikely(frags > max)) {
728 dev_warn(dev, "Too many frags\n");
733 np->rx.rsp_cons = cons + frags;
738 static int xennet_set_skb_gso(struct sk_buff *skb,
739 struct xen_netif_extra_info *gso)
741 if (!gso->u.gso.size) {
743 printk(KERN_WARNING "GSO size must not be zero.\n");
747 /* Currently only TCPv4 S.O. is supported. */
748 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
750 printk(KERN_WARNING "Bad GSO type %d.\n", gso->u.gso.type);
754 skb_shinfo(skb)->gso_size = gso->u.gso.size;
755 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
757 /* Header must be checked, and gso_segs computed. */
758 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
759 skb_shinfo(skb)->gso_segs = 0;
764 static RING_IDX xennet_fill_frags(struct netfront_info *np,
766 struct sk_buff_head *list)
768 struct skb_shared_info *shinfo = skb_shinfo(skb);
769 int nr_frags = shinfo->nr_frags;
770 RING_IDX cons = np->rx.rsp_cons;
771 skb_frag_t *frag = shinfo->frags + nr_frags;
772 struct sk_buff *nskb;
774 while ((nskb = __skb_dequeue(list))) {
775 struct xen_netif_rx_response *rx =
776 RING_GET_RESPONSE(&np->rx, ++cons);
778 frag->page = skb_shinfo(nskb)->frags[0].page;
779 frag->page_offset = rx->offset;
780 frag->size = rx->status;
782 skb->data_len += rx->status;
784 skb_shinfo(nskb)->nr_frags = 0;
791 shinfo->nr_frags = nr_frags;
795 static int skb_checksum_setup(struct sk_buff *skb)
801 if (skb->protocol != htons(ETH_P_IP))
804 iph = (void *)skb->data;
805 th = skb->data + 4 * iph->ihl;
806 if (th >= skb_tail_pointer(skb))
809 skb->csum_start = th - skb->head;
810 switch (iph->protocol) {
812 skb->csum_offset = offsetof(struct tcphdr, check);
815 skb->csum_offset = offsetof(struct udphdr, check);
819 printk(KERN_ERR "Attempting to checksum a non-"
820 "TCP/UDP packet, dropping a protocol"
821 " %d packet", iph->protocol);
825 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
834 static int handle_incoming_queue(struct net_device *dev,
835 struct sk_buff_head *rxq)
837 struct netfront_info *np = netdev_priv(dev);
838 int packets_dropped = 0;
841 while ((skb = __skb_dequeue(rxq)) != NULL) {
842 struct page *page = NETFRONT_SKB_CB(skb)->page;
843 void *vaddr = page_address(page);
844 unsigned offset = NETFRONT_SKB_CB(skb)->offset;
846 memcpy(skb->data, vaddr + offset,
849 if (page != skb_shinfo(skb)->frags[0].page)
852 /* Ethernet work: Delayed to here as it peeks the header. */
853 skb->protocol = eth_type_trans(skb, dev);
855 if (skb->ip_summed == CHECKSUM_PARTIAL) {
856 if (skb_checksum_setup(skb)) {
859 np->stats.rx_errors++;
864 np->stats.rx_packets++;
865 np->stats.rx_bytes += skb->len;
868 netif_receive_skb(skb);
869 dev->last_rx = jiffies;
872 return packets_dropped;
875 static int xennet_poll(struct net_device *dev, int *pbudget)
877 struct netfront_info *np = netdev_priv(dev);
879 struct netfront_rx_info rinfo;
880 struct xen_netif_rx_response *rx = &rinfo.rx;
881 struct xen_netif_extra_info *extras = rinfo.extras;
883 int work_done, budget, more_to_do = 1;
884 struct sk_buff_head rxq;
885 struct sk_buff_head errq;
886 struct sk_buff_head tmpq;
891 spin_lock(&np->rx_lock);
893 if (unlikely(!netif_carrier_ok(dev))) {
894 spin_unlock(&np->rx_lock);
898 skb_queue_head_init(&rxq);
899 skb_queue_head_init(&errq);
900 skb_queue_head_init(&tmpq);
903 if (budget > dev->quota)
905 rp = np->rx.sring->rsp_prod;
906 rmb(); /* Ensure we see queued responses up to 'rp'. */
910 while ((i != rp) && (work_done < budget)) {
911 memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
912 memset(extras, 0, sizeof(rinfo.extras));
914 err = xennet_get_responses(np, &rinfo, rp, &tmpq);
918 while ((skb = __skb_dequeue(&tmpq)))
919 __skb_queue_tail(&errq, skb);
920 np->stats.rx_errors++;
925 skb = __skb_dequeue(&tmpq);
927 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
928 struct xen_netif_extra_info *gso;
929 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
931 if (unlikely(xennet_set_skb_gso(skb, gso))) {
932 __skb_queue_head(&tmpq, skb);
933 np->rx.rsp_cons += skb_queue_len(&tmpq);
938 NETFRONT_SKB_CB(skb)->page = skb_shinfo(skb)->frags[0].page;
939 NETFRONT_SKB_CB(skb)->offset = rx->offset;
942 if (len > RX_COPY_THRESHOLD)
943 len = RX_COPY_THRESHOLD;
946 if (rx->status > len) {
947 skb_shinfo(skb)->frags[0].page_offset =
949 skb_shinfo(skb)->frags[0].size = rx->status - len;
950 skb->data_len = rx->status - len;
952 skb_shinfo(skb)->frags[0].page = NULL;
953 skb_shinfo(skb)->nr_frags = 0;
956 i = xennet_fill_frags(np, skb, &tmpq);
959 * Truesize approximates the size of true data plus
960 * any supervisor overheads. Adding hypervisor
961 * overheads has been shown to significantly reduce
962 * achievable bandwidth with the default receive
963 * buffer size. It is therefore not wise to account
966 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set
967 * to RX_COPY_THRESHOLD + the supervisor
968 * overheads. Here, we add the size of the data pulled
969 * in xennet_fill_frags().
971 * We also adjust for any unused space in the main
972 * data area by subtracting (RX_COPY_THRESHOLD -
973 * len). This is especially important with drivers
974 * which split incoming packets into header and data,
975 * using only 66 bytes of the main data area (see the
976 * e1000 driver for example.) On such systems,
977 * without this last adjustement, our achievable
978 * receive throughout using the standard receive
979 * buffer size was cut by 25%(!!!).
981 skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
982 skb->len += skb->data_len;
984 if (rx->flags & NETRXF_csum_blank)
985 skb->ip_summed = CHECKSUM_PARTIAL;
986 else if (rx->flags & NETRXF_data_validated)
987 skb->ip_summed = CHECKSUM_UNNECESSARY;
989 __skb_queue_tail(&rxq, skb);
991 np->rx.rsp_cons = ++i;
995 while ((skb = __skb_dequeue(&errq)))
998 work_done -= handle_incoming_queue(dev, &rxq);
1000 /* If we get a callback with very few responses, reduce fill target. */
1001 /* NB. Note exponential increase, linear decrease. */
1002 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
1003 ((3*np->rx_target) / 4)) &&
1004 (--np->rx_target < np->rx_min_target))
1005 np->rx_target = np->rx_min_target;
1007 xennet_alloc_rx_buffers(dev);
1009 *pbudget -= work_done;
1010 dev->quota -= work_done;
1012 if (work_done < budget) {
1013 local_irq_save(flags);
1015 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
1017 __netif_rx_complete(dev);
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 ((unsigned long)np->tx_skbs[i].skb < PAGE_OFFSET)
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 while ((skb = __skb_dequeue(&free_list)) != NULL)
1132 spin_unlock_bh(&np->rx_lock);
1135 static void xennet_uninit(struct net_device *dev)
1137 struct netfront_info *np = netdev_priv(dev);
1138 xennet_release_tx_bufs(np);
1139 xennet_release_rx_bufs(np);
1140 gnttab_free_grant_references(np->gref_tx_head);
1141 gnttab_free_grant_references(np->gref_rx_head);
1144 static struct net_device * __devinit xennet_create_dev(struct xenbus_device *dev)
1147 struct net_device *netdev;
1148 struct netfront_info *np;
1150 netdev = alloc_etherdev(sizeof(struct netfront_info));
1152 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1154 return ERR_PTR(-ENOMEM);
1157 np = netdev_priv(netdev);
1160 spin_lock_init(&np->tx_lock);
1161 spin_lock_init(&np->rx_lock);
1163 skb_queue_head_init(&np->rx_batch);
1164 np->rx_target = RX_DFL_MIN_TARGET;
1165 np->rx_min_target = RX_DFL_MIN_TARGET;
1166 np->rx_max_target = RX_MAX_TARGET;
1168 init_timer(&np->rx_refill_timer);
1169 np->rx_refill_timer.data = (unsigned long)netdev;
1170 np->rx_refill_timer.function = rx_refill_timeout;
1172 /* Initialise tx_skbs as a free chain containing every entry. */
1173 np->tx_skb_freelist = 0;
1174 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1175 np->tx_skbs[i].link = i+1;
1176 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1179 /* Clear out rx_skbs */
1180 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1181 np->rx_skbs[i] = NULL;
1182 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1185 /* A grant for every tx ring slot */
1186 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1187 &np->gref_tx_head) < 0) {
1188 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1192 /* A grant for every rx ring slot */
1193 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1194 &np->gref_rx_head) < 0) {
1195 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1200 netdev->open = xennet_open;
1201 netdev->hard_start_xmit = xennet_start_xmit;
1202 netdev->stop = xennet_close;
1203 netdev->get_stats = xennet_get_stats;
1204 netdev->poll = xennet_poll;
1205 netdev->uninit = xennet_uninit;
1206 netdev->change_mtu = xennet_change_mtu;
1207 netdev->weight = 64;
1208 netdev->features = NETIF_F_IP_CSUM;
1210 SET_ETHTOOL_OPS(netdev, &xennet_ethtool_ops);
1211 SET_MODULE_OWNER(netdev);
1212 SET_NETDEV_DEV(netdev, &dev->dev);
1214 np->netdev = netdev;
1216 netif_carrier_off(netdev);
1221 gnttab_free_grant_references(np->gref_tx_head);
1223 free_netdev(netdev);
1224 return ERR_PTR(err);
1228 * Entry point to this code when a new device is created. Allocate the basic
1229 * structures and the ring buffers for communication with the backend, and
1230 * inform the backend of the appropriate details for those.
1232 static int __devinit netfront_probe(struct xenbus_device *dev,
1233 const struct xenbus_device_id *id)
1236 struct net_device *netdev;
1237 struct netfront_info *info;
1239 netdev = xennet_create_dev(dev);
1240 if (IS_ERR(netdev)) {
1241 err = PTR_ERR(netdev);
1242 xenbus_dev_fatal(dev, err, "creating netdev");
1246 info = netdev_priv(netdev);
1247 dev->dev.driver_data = info;
1249 err = register_netdev(info->netdev);
1251 printk(KERN_WARNING "%s: register_netdev err=%d\n",
1256 err = xennet_sysfs_addif(info->netdev);
1258 unregister_netdev(info->netdev);
1259 printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
1267 free_netdev(netdev);
1268 dev->dev.driver_data = NULL;
1272 static void xennet_end_access(int ref, void *page)
1274 /* This frees the page as a side-effect */
1275 if (ref != GRANT_INVALID_REF)
1276 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1279 static void xennet_disconnect_backend(struct netfront_info *info)
1281 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1282 spin_lock_bh(&info->rx_lock);
1283 spin_lock_irq(&info->tx_lock);
1284 netif_carrier_off(info->netdev);
1285 spin_unlock_irq(&info->tx_lock);
1286 spin_unlock_bh(&info->rx_lock);
1288 if (info->netdev->irq)
1289 unbind_from_irqhandler(info->netdev->irq, info->netdev);
1290 info->evtchn = info->netdev->irq = 0;
1292 /* End access and free the pages */
1293 xennet_end_access(info->tx_ring_ref, info->tx.sring);
1294 xennet_end_access(info->rx_ring_ref, info->rx.sring);
1296 info->tx_ring_ref = GRANT_INVALID_REF;
1297 info->rx_ring_ref = GRANT_INVALID_REF;
1298 info->tx.sring = NULL;
1299 info->rx.sring = NULL;
1303 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1304 * driver restart. We tear down our netif structure and recreate it, but
1305 * leave the device-layer structures intact so that this is transparent to the
1306 * rest of the kernel.
1308 static int netfront_resume(struct xenbus_device *dev)
1310 struct netfront_info *info = dev->dev.driver_data;
1312 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1314 xennet_disconnect_backend(info);
1318 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1320 char *s, *e, *macstr;
1323 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1325 return PTR_ERR(macstr);
1327 for (i = 0; i < ETH_ALEN; i++) {
1328 mac[i] = simple_strtoul(s, &e, 16);
1329 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1340 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1342 struct net_device *dev = dev_id;
1343 struct netfront_info *np = netdev_priv(dev);
1344 unsigned long flags;
1346 spin_lock_irqsave(&np->tx_lock, flags);
1348 if (likely(netif_carrier_ok(dev))) {
1349 xennet_tx_buf_gc(dev);
1350 /* Under tx_lock: protects access to rx shared-ring indexes. */
1351 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
1352 netif_rx_schedule(dev);
1355 spin_unlock_irqrestore(&np->tx_lock, flags);
1360 static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)
1362 struct xen_netif_tx_sring *txs;
1363 struct xen_netif_rx_sring *rxs;
1365 struct net_device *netdev = info->netdev;
1367 info->tx_ring_ref = GRANT_INVALID_REF;
1368 info->rx_ring_ref = GRANT_INVALID_REF;
1369 info->rx.sring = NULL;
1370 info->tx.sring = NULL;
1373 err = xen_net_read_mac(dev, netdev->dev_addr);
1375 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1379 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_KERNEL);
1382 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1385 SHARED_RING_INIT(txs);
1386 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
1388 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
1390 free_page((unsigned long)txs);
1394 info->tx_ring_ref = err;
1395 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_KERNEL);
1398 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1401 SHARED_RING_INIT(rxs);
1402 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
1404 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
1406 free_page((unsigned long)rxs);
1409 info->rx_ring_ref = err;
1411 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1415 err = bind_evtchn_to_irqhandler(info->evtchn, xennet_interrupt,
1416 IRQF_SAMPLE_RANDOM, netdev->name,
1427 /* Common code used when first setting up, and when resuming. */
1428 static int talk_to_backend(struct xenbus_device *dev,
1429 struct netfront_info *info)
1431 const char *message;
1432 struct xenbus_transaction xbt;
1435 /* Create shared ring, alloc event channel. */
1436 err = setup_netfront(dev, info);
1441 err = xenbus_transaction_start(&xbt);
1443 xenbus_dev_fatal(dev, err, "starting transaction");
1447 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref", "%u",
1450 message = "writing tx ring-ref";
1451 goto abort_transaction;
1453 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref", "%u",
1456 message = "writing rx ring-ref";
1457 goto abort_transaction;
1459 err = xenbus_printf(xbt, dev->nodename,
1460 "event-channel", "%u", info->evtchn);
1462 message = "writing event-channel";
1463 goto abort_transaction;
1466 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1469 message = "writing request-rx-copy";
1470 goto abort_transaction;
1473 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1475 message = "writing feature-rx-notify";
1476 goto abort_transaction;
1479 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1481 message = "writing feature-sg";
1482 goto abort_transaction;
1485 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1487 message = "writing feature-gso-tcpv4";
1488 goto abort_transaction;
1491 err = xenbus_transaction_end(xbt, 0);
1495 xenbus_dev_fatal(dev, err, "completing transaction");
1502 xenbus_transaction_end(xbt, 1);
1503 xenbus_dev_fatal(dev, err, "%s", message);
1505 xennet_disconnect_backend(info);
1510 static int xennet_set_sg(struct net_device *dev, u32 data)
1513 struct netfront_info *np = netdev_priv(dev);
1516 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1521 } else if (dev->mtu > ETH_DATA_LEN)
1522 dev->mtu = ETH_DATA_LEN;
1524 return ethtool_op_set_sg(dev, data);
1527 static int xennet_set_tso(struct net_device *dev, u32 data)
1530 struct netfront_info *np = netdev_priv(dev);
1533 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1534 "feature-gso-tcpv4", "%d", &val) < 0)
1540 return ethtool_op_set_tso(dev, data);
1543 static void xennet_set_features(struct net_device *dev)
1545 /* Turn off all GSO bits except ROBUST. */
1546 dev->features &= (1 << NETIF_F_GSO_SHIFT) - 1;
1547 dev->features |= NETIF_F_GSO_ROBUST;
1548 xennet_set_sg(dev, 0);
1550 /* We need checksum offload to enable scatter/gather and TSO. */
1551 if (!(dev->features & NETIF_F_IP_CSUM))
1554 if (!xennet_set_sg(dev, 1))
1555 xennet_set_tso(dev, 1);
1558 static int xennet_connect(struct net_device *dev)
1560 struct netfront_info *np = netdev_priv(dev);
1561 int i, requeue_idx, err;
1562 struct sk_buff *skb;
1564 struct xen_netif_rx_request *req;
1565 unsigned int feature_rx_copy;
1567 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1568 "feature-rx-copy", "%u", &feature_rx_copy);
1570 feature_rx_copy = 0;
1572 if (!feature_rx_copy) {
1574 "backend does not support copying recieve path");
1578 err = talk_to_backend(np->xbdev, np);
1582 xennet_set_features(dev);
1584 spin_lock_bh(&np->rx_lock);
1585 spin_lock_irq(&np->tx_lock);
1587 /* Step 1: Discard all pending TX packet fragments. */
1588 xennet_release_tx_bufs(np);
1590 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1591 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1592 if (!np->rx_skbs[i])
1595 skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1596 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1597 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1599 gnttab_grant_foreign_access_ref(
1600 ref, np->xbdev->otherend_id,
1601 pfn_to_mfn(page_to_pfn(skb_shinfo(skb)->
1605 req->id = requeue_idx;
1610 np->rx.req_prod_pvt = requeue_idx;
1613 * Step 3: All public and private state should now be sane. Get
1614 * ready to start sending and receiving packets and give the driver
1615 * domain a kick because we've probably just requeued some
1618 netif_carrier_on(np->netdev);
1619 notify_remote_via_irq(np->netdev->irq);
1620 xennet_tx_buf_gc(dev);
1621 xennet_alloc_rx_buffers(dev);
1623 spin_unlock_irq(&np->tx_lock);
1624 spin_unlock_bh(&np->rx_lock);
1630 * Callback received when the backend's state changes.
1632 static void backend_changed(struct xenbus_device *dev,
1633 enum xenbus_state backend_state)
1635 struct netfront_info *np = dev->dev.driver_data;
1636 struct net_device *netdev = np->netdev;
1638 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
1640 switch (backend_state) {
1641 case XenbusStateInitialising:
1642 case XenbusStateInitialised:
1643 case XenbusStateConnected:
1644 case XenbusStateUnknown:
1645 case XenbusStateClosed:
1648 case XenbusStateInitWait:
1649 if (dev->state != XenbusStateInitialising)
1651 if (xennet_connect(netdev) != 0)
1653 xenbus_switch_state(dev, XenbusStateConnected);
1656 case XenbusStateClosing:
1657 xenbus_frontend_closed(dev);
1662 static struct ethtool_ops xennet_ethtool_ops =
1664 .get_tx_csum = ethtool_op_get_tx_csum,
1665 .set_tx_csum = ethtool_op_set_tx_csum,
1666 .get_sg = ethtool_op_get_sg,
1667 .set_sg = xennet_set_sg,
1668 .get_tso = ethtool_op_get_tso,
1669 .set_tso = xennet_set_tso,
1670 .get_link = ethtool_op_get_link,
1674 static ssize_t show_rxbuf_min(struct device *dev,
1675 struct device_attribute *attr, char *buf)
1677 struct net_device *netdev = to_net_dev(dev);
1678 struct netfront_info *info = netdev_priv(netdev);
1680 return sprintf(buf, "%u\n", info->rx_min_target);
1683 static ssize_t store_rxbuf_min(struct device *dev,
1684 struct device_attribute *attr,
1685 const char *buf, size_t len)
1687 struct net_device *netdev = to_net_dev(dev);
1688 struct netfront_info *np = netdev_priv(netdev);
1690 unsigned long target;
1692 if (!capable(CAP_NET_ADMIN))
1695 target = simple_strtoul(buf, &endp, 0);
1699 if (target < RX_MIN_TARGET)
1700 target = RX_MIN_TARGET;
1701 if (target > RX_MAX_TARGET)
1702 target = RX_MAX_TARGET;
1704 spin_lock_bh(&np->rx_lock);
1705 if (target > np->rx_max_target)
1706 np->rx_max_target = target;
1707 np->rx_min_target = target;
1708 if (target > np->rx_target)
1709 np->rx_target = target;
1711 xennet_alloc_rx_buffers(netdev);
1713 spin_unlock_bh(&np->rx_lock);
1717 static ssize_t show_rxbuf_max(struct device *dev,
1718 struct device_attribute *attr, char *buf)
1720 struct net_device *netdev = to_net_dev(dev);
1721 struct netfront_info *info = netdev_priv(netdev);
1723 return sprintf(buf, "%u\n", info->rx_max_target);
1726 static ssize_t store_rxbuf_max(struct device *dev,
1727 struct device_attribute *attr,
1728 const char *buf, size_t len)
1730 struct net_device *netdev = to_net_dev(dev);
1731 struct netfront_info *np = netdev_priv(netdev);
1733 unsigned long target;
1735 if (!capable(CAP_NET_ADMIN))
1738 target = simple_strtoul(buf, &endp, 0);
1742 if (target < RX_MIN_TARGET)
1743 target = RX_MIN_TARGET;
1744 if (target > RX_MAX_TARGET)
1745 target = RX_MAX_TARGET;
1747 spin_lock_bh(&np->rx_lock);
1748 if (target < np->rx_min_target)
1749 np->rx_min_target = target;
1750 np->rx_max_target = target;
1751 if (target < np->rx_target)
1752 np->rx_target = target;
1754 xennet_alloc_rx_buffers(netdev);
1756 spin_unlock_bh(&np->rx_lock);
1760 static ssize_t show_rxbuf_cur(struct device *dev,
1761 struct device_attribute *attr, char *buf)
1763 struct net_device *netdev = to_net_dev(dev);
1764 struct netfront_info *info = netdev_priv(netdev);
1766 return sprintf(buf, "%u\n", info->rx_target);
1769 static struct device_attribute xennet_attrs[] = {
1770 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1771 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1772 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1775 static int xennet_sysfs_addif(struct net_device *netdev)
1780 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1781 err = device_create_file(&netdev->dev,
1790 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1794 static void xennet_sysfs_delif(struct net_device *netdev)
1798 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
1799 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1802 #endif /* CONFIG_SYSFS */
1804 static struct xenbus_device_id netfront_ids[] = {
1810 static int __devexit xennet_remove(struct xenbus_device *dev)
1812 struct netfront_info *info = dev->dev.driver_data;
1814 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1816 unregister_netdev(info->netdev);
1818 xennet_disconnect_backend(info);
1820 del_timer_sync(&info->rx_refill_timer);
1822 xennet_sysfs_delif(info->netdev);
1824 free_netdev(info->netdev);
1829 static struct xenbus_driver netfront = {
1831 .owner = THIS_MODULE,
1832 .ids = netfront_ids,
1833 .probe = netfront_probe,
1834 .remove = __devexit_p(xennet_remove),
1835 .resume = netfront_resume,
1836 .otherend_changed = backend_changed,
1839 static int __init netif_init(void)
1841 if (!is_running_on_xen())
1844 if (is_initial_xendomain())
1847 printk(KERN_INFO "Initialising Xen virtual ethernet driver.\n");
1849 return xenbus_register_frontend(&netfront);
1851 module_init(netif_init);
1854 static void __exit netif_exit(void)
1856 if (is_initial_xendomain())
1859 return xenbus_unregister_driver(&netfront);
1861 module_exit(netif_exit);
1863 MODULE_DESCRIPTION("Xen virtual network device frontend");
1864 MODULE_LICENSE("GPL");