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 napi_struct napi;
78 struct xenbus_device *xbdev;
81 struct xen_netif_tx_front_ring tx;
85 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
86 * are linked from tx_skb_freelist through skb_entry.link.
88 * NB. Freelist index entries are always going to be less than
89 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
90 * greater than PAGE_OFFSET: we use this property to distinguish
96 } tx_skbs[NET_TX_RING_SIZE];
97 grant_ref_t gref_tx_head;
98 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
99 unsigned tx_skb_freelist;
101 spinlock_t rx_lock ____cacheline_aligned_in_smp;
102 struct xen_netif_rx_front_ring rx;
105 /* Receive-ring batched refills. */
106 #define RX_MIN_TARGET 8
107 #define RX_DFL_MIN_TARGET 64
108 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
109 unsigned rx_min_target, rx_max_target, rx_target;
110 struct sk_buff_head rx_batch;
112 struct timer_list rx_refill_timer;
114 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
115 grant_ref_t gref_rx_head;
116 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
118 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
119 struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
120 struct mmu_update rx_mmu[NET_RX_RING_SIZE];
123 struct netfront_rx_info {
124 struct xen_netif_rx_response rx;
125 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
129 * Access macros for acquiring freeing slots in tx_skbs[].
132 static void add_id_to_freelist(unsigned *head, union skb_entry *list,
135 list[id].link = *head;
139 static unsigned short get_id_from_freelist(unsigned *head,
140 union skb_entry *list)
142 unsigned int id = *head;
143 *head = list[id].link;
147 static int xennet_rxidx(RING_IDX idx)
149 return idx & (NET_RX_RING_SIZE - 1);
152 static struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
155 int i = xennet_rxidx(ri);
156 struct sk_buff *skb = np->rx_skbs[i];
157 np->rx_skbs[i] = NULL;
161 static grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
164 int i = xennet_rxidx(ri);
165 grant_ref_t ref = np->grant_rx_ref[i];
166 np->grant_rx_ref[i] = GRANT_INVALID_REF;
171 static int xennet_sysfs_addif(struct net_device *netdev);
172 static void xennet_sysfs_delif(struct net_device *netdev);
173 #else /* !CONFIG_SYSFS */
174 #define xennet_sysfs_addif(dev) (0)
175 #define xennet_sysfs_delif(dev) do { } while (0)
178 static int xennet_can_sg(struct net_device *dev)
180 return dev->features & NETIF_F_SG;
184 static void rx_refill_timeout(unsigned long data)
186 struct net_device *dev = (struct net_device *)data;
187 struct netfront_info *np = netdev_priv(dev);
188 netif_rx_schedule(dev, &np->napi);
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;
218 struct xen_netif_rx_request *req;
220 if (unlikely(!netif_carrier_ok(dev)))
224 * Allocate skbuffs greedily, even though we batch updates to the
225 * receive ring. This creates a less bursty demand on the memory
226 * allocator, so should reduce the chance of failed allocation requests
227 * both for ourself and for other kernel subsystems.
229 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
230 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
231 skb = __netdev_alloc_skb(dev, RX_COPY_THRESHOLD,
232 GFP_ATOMIC | __GFP_NOWARN);
236 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
240 /* Any skbuffs queued for refill? Force them out. */
243 /* Could not allocate any skbuffs. Try again later. */
244 mod_timer(&np->rx_refill_timer,
249 skb_shinfo(skb)->frags[0].page = page;
250 skb_shinfo(skb)->nr_frags = 1;
251 __skb_queue_tail(&np->rx_batch, skb);
254 /* Is the batch large enough to be worthwhile? */
255 if (i < (np->rx_target/2)) {
256 if (req_prod > np->rx.sring->req_prod)
261 /* Adjust our fill target if we risked running out of buffers. */
262 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
263 ((np->rx_target *= 2) > np->rx_max_target))
264 np->rx_target = np->rx_max_target;
268 skb = __skb_dequeue(&np->rx_batch);
274 id = xennet_rxidx(req_prod + i);
276 BUG_ON(np->rx_skbs[id]);
277 np->rx_skbs[id] = skb;
279 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
280 BUG_ON((signed short)ref < 0);
281 np->grant_rx_ref[id] = ref;
283 pfn = page_to_pfn(skb_shinfo(skb)->frags[0].page);
284 vaddr = page_address(skb_shinfo(skb)->frags[0].page);
286 req = RING_GET_REQUEST(&np->rx, req_prod + i);
287 gnttab_grant_foreign_access_ref(ref,
288 np->xbdev->otherend_id,
296 wmb(); /* barrier so backend seens requests */
298 /* Above is a suitable barrier to ensure backend will see requests. */
299 np->rx.req_prod_pvt = req_prod + i;
301 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
303 notify_remote_via_irq(np->netdev->irq);
306 static int xennet_open(struct net_device *dev)
308 struct netfront_info *np = netdev_priv(dev);
310 napi_enable(&np->napi);
312 spin_lock_bh(&np->rx_lock);
313 if (netif_carrier_ok(dev)) {
314 xennet_alloc_rx_buffers(dev);
315 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
316 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
317 netif_rx_schedule(dev, &np->napi);
319 spin_unlock_bh(&np->rx_lock);
321 xennet_maybe_wake_tx(dev);
326 static void xennet_tx_buf_gc(struct net_device *dev)
330 struct netfront_info *np = netdev_priv(dev);
333 BUG_ON(!netif_carrier_ok(dev));
336 prod = np->tx.sring->rsp_prod;
337 rmb(); /* Ensure we see responses up to 'rp'. */
339 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
340 struct xen_netif_tx_response *txrsp;
342 txrsp = RING_GET_RESPONSE(&np->tx, cons);
343 if (txrsp->status == NETIF_RSP_NULL)
347 skb = np->tx_skbs[id].skb;
348 if (unlikely(gnttab_query_foreign_access(
349 np->grant_tx_ref[id]) != 0)) {
350 printk(KERN_ALERT "xennet_tx_buf_gc: warning "
351 "-- grant still in use by backend "
355 gnttab_end_foreign_access_ref(
356 np->grant_tx_ref[id], GNTMAP_readonly);
357 gnttab_release_grant_reference(
358 &np->gref_tx_head, np->grant_tx_ref[id]);
359 np->grant_tx_ref[id] = GRANT_INVALID_REF;
360 add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, id);
361 dev_kfree_skb_irq(skb);
364 np->tx.rsp_cons = prod;
367 * Set a new event, then check for race with update of tx_cons.
368 * Note that it is essential to schedule a callback, no matter
369 * how few buffers are pending. Even if there is space in the
370 * transmit ring, higher layers may be blocked because too much
371 * data is outstanding: in such cases notification from Xen is
372 * likely to be the only kick that we'll get.
374 np->tx.sring->rsp_event =
375 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
376 mb(); /* update shared area */
377 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
379 xennet_maybe_wake_tx(dev);
382 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
383 struct xen_netif_tx_request *tx)
385 struct netfront_info *np = netdev_priv(dev);
386 char *data = skb->data;
388 RING_IDX prod = np->tx.req_prod_pvt;
389 int frags = skb_shinfo(skb)->nr_frags;
390 unsigned int offset = offset_in_page(data);
391 unsigned int len = skb_headlen(skb);
396 /* While the header overlaps a page boundary (including being
397 larger than a page), split it it into page-sized chunks. */
398 while (len > PAGE_SIZE - offset) {
399 tx->size = PAGE_SIZE - offset;
400 tx->flags |= NETTXF_more_data;
405 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
406 np->tx_skbs[id].skb = skb_get(skb);
407 tx = RING_GET_REQUEST(&np->tx, prod++);
409 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
410 BUG_ON((signed short)ref < 0);
412 mfn = virt_to_mfn(data);
413 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
414 mfn, GNTMAP_readonly);
416 tx->gref = np->grant_tx_ref[id] = ref;
422 /* Grant backend access to each skb fragment page. */
423 for (i = 0; i < frags; i++) {
424 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
426 tx->flags |= NETTXF_more_data;
428 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
429 np->tx_skbs[id].skb = skb_get(skb);
430 tx = RING_GET_REQUEST(&np->tx, prod++);
432 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
433 BUG_ON((signed short)ref < 0);
435 mfn = pfn_to_mfn(page_to_pfn(frag->page));
436 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
437 mfn, GNTMAP_readonly);
439 tx->gref = np->grant_tx_ref[id] = ref;
440 tx->offset = frag->page_offset;
441 tx->size = frag->size;
445 np->tx.req_prod_pvt = prod;
448 static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
451 struct netfront_info *np = netdev_priv(dev);
452 struct xen_netif_tx_request *tx;
453 struct xen_netif_extra_info *extra;
454 char *data = skb->data;
459 int frags = skb_shinfo(skb)->nr_frags;
460 unsigned int offset = offset_in_page(data);
461 unsigned int len = skb_headlen(skb);
463 frags += (offset + len + PAGE_SIZE - 1) / PAGE_SIZE;
464 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
465 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
471 spin_lock_irq(&np->tx_lock);
473 if (unlikely(!netif_carrier_ok(dev) ||
474 (frags > 1 && !xennet_can_sg(dev)) ||
475 netif_needs_gso(dev, skb))) {
476 spin_unlock_irq(&np->tx_lock);
480 i = np->tx.req_prod_pvt;
482 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
483 np->tx_skbs[id].skb = skb;
485 tx = RING_GET_REQUEST(&np->tx, i);
488 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
489 BUG_ON((signed short)ref < 0);
490 mfn = virt_to_mfn(data);
491 gnttab_grant_foreign_access_ref(
492 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
493 tx->gref = np->grant_tx_ref[id] = ref;
499 if (skb->ip_summed == CHECKSUM_PARTIAL)
501 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
502 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
503 /* remote but checksummed. */
504 tx->flags |= NETTXF_data_validated;
506 if (skb_shinfo(skb)->gso_size) {
507 struct xen_netif_extra_info *gso;
509 gso = (struct xen_netif_extra_info *)
510 RING_GET_REQUEST(&np->tx, ++i);
513 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
515 tx->flags |= NETTXF_extra_info;
517 gso->u.gso.size = skb_shinfo(skb)->gso_size;
518 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
520 gso->u.gso.features = 0;
522 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
527 np->tx.req_prod_pvt = i + 1;
529 xennet_make_frags(skb, dev, tx);
532 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
534 notify_remote_via_irq(np->netdev->irq);
536 dev->stats.tx_bytes += skb->len;
537 dev->stats.tx_packets++;
539 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
540 xennet_tx_buf_gc(dev);
542 if (!netfront_tx_slot_available(np))
543 netif_stop_queue(dev);
545 spin_unlock_irq(&np->tx_lock);
550 dev->stats.tx_dropped++;
555 static int xennet_close(struct net_device *dev)
557 struct netfront_info *np = netdev_priv(dev);
558 netif_stop_queue(np->netdev);
559 napi_disable(&np->napi);
563 static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
566 int new = xennet_rxidx(np->rx.req_prod_pvt);
568 BUG_ON(np->rx_skbs[new]);
569 np->rx_skbs[new] = skb;
570 np->grant_rx_ref[new] = ref;
571 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
572 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
573 np->rx.req_prod_pvt++;
576 static int xennet_get_extras(struct netfront_info *np,
577 struct xen_netif_extra_info *extras,
581 struct xen_netif_extra_info *extra;
582 struct device *dev = &np->netdev->dev;
583 RING_IDX cons = np->rx.rsp_cons;
590 if (unlikely(cons + 1 == rp)) {
592 dev_warn(dev, "Missing extra info\n");
597 extra = (struct xen_netif_extra_info *)
598 RING_GET_RESPONSE(&np->rx, ++cons);
600 if (unlikely(!extra->type ||
601 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
603 dev_warn(dev, "Invalid extra type: %d\n",
607 memcpy(&extras[extra->type - 1], extra,
611 skb = xennet_get_rx_skb(np, cons);
612 ref = xennet_get_rx_ref(np, cons);
613 xennet_move_rx_slot(np, skb, ref);
614 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
616 np->rx.rsp_cons = cons;
620 static int xennet_get_responses(struct netfront_info *np,
621 struct netfront_rx_info *rinfo, RING_IDX rp,
622 struct sk_buff_head *list)
624 struct xen_netif_rx_response *rx = &rinfo->rx;
625 struct xen_netif_extra_info *extras = rinfo->extras;
626 struct device *dev = &np->netdev->dev;
627 RING_IDX cons = np->rx.rsp_cons;
628 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
629 grant_ref_t ref = xennet_get_rx_ref(np, cons);
630 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
635 if (rx->flags & NETRXF_extra_info) {
636 err = xennet_get_extras(np, extras, rp);
637 cons = np->rx.rsp_cons;
641 if (unlikely(rx->status < 0 ||
642 rx->offset + rx->status > PAGE_SIZE)) {
644 dev_warn(dev, "rx->offset: %x, size: %u\n",
645 rx->offset, rx->status);
646 xennet_move_rx_slot(np, skb, ref);
652 * This definitely indicates a bug, either in this driver or in
653 * the backend driver. In future this should flag the bad
654 * situation to the system controller to reboot the backed.
656 if (ref == GRANT_INVALID_REF) {
658 dev_warn(dev, "Bad rx response id %d.\n",
664 ret = gnttab_end_foreign_access_ref(ref, 0);
667 gnttab_release_grant_reference(&np->gref_rx_head, ref);
669 __skb_queue_tail(list, skb);
672 if (!(rx->flags & NETRXF_more_data))
675 if (cons + frags == rp) {
677 dev_warn(dev, "Need more frags\n");
682 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
683 skb = xennet_get_rx_skb(np, cons + frags);
684 ref = xennet_get_rx_ref(np, cons + frags);
688 if (unlikely(frags > max)) {
690 dev_warn(dev, "Too many frags\n");
695 np->rx.rsp_cons = cons + frags;
700 static int xennet_set_skb_gso(struct sk_buff *skb,
701 struct xen_netif_extra_info *gso)
703 if (!gso->u.gso.size) {
705 printk(KERN_WARNING "GSO size must not be zero.\n");
709 /* Currently only TCPv4 S.O. is supported. */
710 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
712 printk(KERN_WARNING "Bad GSO type %d.\n", gso->u.gso.type);
716 skb_shinfo(skb)->gso_size = gso->u.gso.size;
717 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
719 /* Header must be checked, and gso_segs computed. */
720 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
721 skb_shinfo(skb)->gso_segs = 0;
726 static RING_IDX xennet_fill_frags(struct netfront_info *np,
728 struct sk_buff_head *list)
730 struct skb_shared_info *shinfo = skb_shinfo(skb);
731 int nr_frags = shinfo->nr_frags;
732 RING_IDX cons = np->rx.rsp_cons;
733 skb_frag_t *frag = shinfo->frags + nr_frags;
734 struct sk_buff *nskb;
736 while ((nskb = __skb_dequeue(list))) {
737 struct xen_netif_rx_response *rx =
738 RING_GET_RESPONSE(&np->rx, ++cons);
740 frag->page = skb_shinfo(nskb)->frags[0].page;
741 frag->page_offset = rx->offset;
742 frag->size = rx->status;
744 skb->data_len += rx->status;
746 skb_shinfo(nskb)->nr_frags = 0;
753 shinfo->nr_frags = nr_frags;
757 static int skb_checksum_setup(struct sk_buff *skb)
763 if (skb->protocol != htons(ETH_P_IP))
766 iph = (void *)skb->data;
767 th = skb->data + 4 * iph->ihl;
768 if (th >= skb_tail_pointer(skb))
771 skb->csum_start = th - skb->head;
772 switch (iph->protocol) {
774 skb->csum_offset = offsetof(struct tcphdr, check);
777 skb->csum_offset = offsetof(struct udphdr, check);
781 printk(KERN_ERR "Attempting to checksum a non-"
782 "TCP/UDP packet, dropping a protocol"
783 " %d packet", iph->protocol);
787 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
796 static int handle_incoming_queue(struct net_device *dev,
797 struct sk_buff_head *rxq)
799 int packets_dropped = 0;
802 while ((skb = __skb_dequeue(rxq)) != NULL) {
803 struct page *page = NETFRONT_SKB_CB(skb)->page;
804 void *vaddr = page_address(page);
805 unsigned offset = NETFRONT_SKB_CB(skb)->offset;
807 memcpy(skb->data, vaddr + offset,
810 if (page != skb_shinfo(skb)->frags[0].page)
813 /* Ethernet work: Delayed to here as it peeks the header. */
814 skb->protocol = eth_type_trans(skb, dev);
816 if (skb->ip_summed == CHECKSUM_PARTIAL) {
817 if (skb_checksum_setup(skb)) {
820 dev->stats.rx_errors++;
825 dev->stats.rx_packets++;
826 dev->stats.rx_bytes += skb->len;
829 netif_receive_skb(skb);
830 dev->last_rx = jiffies;
833 return packets_dropped;
836 static int xennet_poll(struct napi_struct *napi, int budget)
838 struct netfront_info *np = container_of(napi, struct netfront_info, napi);
839 struct net_device *dev = np->netdev;
841 struct netfront_rx_info rinfo;
842 struct xen_netif_rx_response *rx = &rinfo.rx;
843 struct xen_netif_extra_info *extras = rinfo.extras;
846 struct sk_buff_head rxq;
847 struct sk_buff_head errq;
848 struct sk_buff_head tmpq;
853 spin_lock(&np->rx_lock);
855 skb_queue_head_init(&rxq);
856 skb_queue_head_init(&errq);
857 skb_queue_head_init(&tmpq);
859 rp = np->rx.sring->rsp_prod;
860 rmb(); /* Ensure we see queued responses up to 'rp'. */
864 while ((i != rp) && (work_done < budget)) {
865 memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
866 memset(extras, 0, sizeof(rinfo.extras));
868 err = xennet_get_responses(np, &rinfo, rp, &tmpq);
872 while ((skb = __skb_dequeue(&tmpq)))
873 __skb_queue_tail(&errq, skb);
874 dev->stats.rx_errors++;
879 skb = __skb_dequeue(&tmpq);
881 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
882 struct xen_netif_extra_info *gso;
883 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
885 if (unlikely(xennet_set_skb_gso(skb, gso))) {
886 __skb_queue_head(&tmpq, skb);
887 np->rx.rsp_cons += skb_queue_len(&tmpq);
892 NETFRONT_SKB_CB(skb)->page = skb_shinfo(skb)->frags[0].page;
893 NETFRONT_SKB_CB(skb)->offset = rx->offset;
896 if (len > RX_COPY_THRESHOLD)
897 len = RX_COPY_THRESHOLD;
900 if (rx->status > len) {
901 skb_shinfo(skb)->frags[0].page_offset =
903 skb_shinfo(skb)->frags[0].size = rx->status - len;
904 skb->data_len = rx->status - len;
906 skb_shinfo(skb)->frags[0].page = NULL;
907 skb_shinfo(skb)->nr_frags = 0;
910 i = xennet_fill_frags(np, skb, &tmpq);
913 * Truesize approximates the size of true data plus
914 * any supervisor overheads. Adding hypervisor
915 * overheads has been shown to significantly reduce
916 * achievable bandwidth with the default receive
917 * buffer size. It is therefore not wise to account
920 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set
921 * to RX_COPY_THRESHOLD + the supervisor
922 * overheads. Here, we add the size of the data pulled
923 * in xennet_fill_frags().
925 * We also adjust for any unused space in the main
926 * data area by subtracting (RX_COPY_THRESHOLD -
927 * len). This is especially important with drivers
928 * which split incoming packets into header and data,
929 * using only 66 bytes of the main data area (see the
930 * e1000 driver for example.) On such systems,
931 * without this last adjustement, our achievable
932 * receive throughout using the standard receive
933 * buffer size was cut by 25%(!!!).
935 skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
936 skb->len += skb->data_len;
938 if (rx->flags & NETRXF_csum_blank)
939 skb->ip_summed = CHECKSUM_PARTIAL;
940 else if (rx->flags & NETRXF_data_validated)
941 skb->ip_summed = CHECKSUM_UNNECESSARY;
943 __skb_queue_tail(&rxq, skb);
945 np->rx.rsp_cons = ++i;
949 while ((skb = __skb_dequeue(&errq)))
952 work_done -= handle_incoming_queue(dev, &rxq);
954 /* If we get a callback with very few responses, reduce fill target. */
955 /* NB. Note exponential increase, linear decrease. */
956 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
957 ((3*np->rx_target) / 4)) &&
958 (--np->rx_target < np->rx_min_target))
959 np->rx_target = np->rx_min_target;
961 xennet_alloc_rx_buffers(dev);
963 if (work_done < budget) {
966 local_irq_save(flags);
968 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
970 __netif_rx_complete(dev, napi);
972 local_irq_restore(flags);
975 spin_unlock(&np->rx_lock);
980 static int xennet_change_mtu(struct net_device *dev, int mtu)
982 int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
990 static void xennet_release_tx_bufs(struct netfront_info *np)
995 for (i = 0; i < NET_TX_RING_SIZE; i++) {
996 /* Skip over entries which are actually freelist references */
997 if ((unsigned long)np->tx_skbs[i].skb < PAGE_OFFSET)
1000 skb = np->tx_skbs[i].skb;
1001 gnttab_end_foreign_access_ref(np->grant_tx_ref[i],
1003 gnttab_release_grant_reference(&np->gref_tx_head,
1004 np->grant_tx_ref[i]);
1005 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1006 add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, i);
1007 dev_kfree_skb_irq(skb);
1011 static void xennet_release_rx_bufs(struct netfront_info *np)
1013 struct mmu_update *mmu = np->rx_mmu;
1014 struct multicall_entry *mcl = np->rx_mcl;
1015 struct sk_buff_head free_list;
1016 struct sk_buff *skb;
1018 int xfer = 0, noxfer = 0, unused = 0;
1021 dev_warn(&np->netdev->dev, "%s: fix me for copying receiver.\n",
1025 skb_queue_head_init(&free_list);
1027 spin_lock_bh(&np->rx_lock);
1029 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1030 ref = np->grant_rx_ref[id];
1031 if (ref == GRANT_INVALID_REF) {
1036 skb = np->rx_skbs[id];
1037 mfn = gnttab_end_foreign_transfer_ref(ref);
1038 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1039 np->grant_rx_ref[id] = GRANT_INVALID_REF;
1042 skb_shinfo(skb)->nr_frags = 0;
1048 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1049 /* Remap the page. */
1050 struct page *page = skb_shinfo(skb)->frags[0].page;
1051 unsigned long pfn = page_to_pfn(page);
1052 void *vaddr = page_address(page);
1054 MULTI_update_va_mapping(mcl, (unsigned long)vaddr,
1055 mfn_pte(mfn, PAGE_KERNEL),
1058 mmu->ptr = ((u64)mfn << PAGE_SHIFT)
1059 | MMU_MACHPHYS_UPDATE;
1063 set_phys_to_machine(pfn, mfn);
1065 __skb_queue_tail(&free_list, skb);
1069 dev_info(&np->netdev->dev, "%s: %d xfer, %d noxfer, %d unused\n",
1070 __func__, xfer, noxfer, unused);
1073 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1074 /* Do all the remapping work and M2P updates. */
1075 MULTI_mmu_update(mcl, np->rx_mmu, mmu - np->rx_mmu,
1078 HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
1082 while ((skb = __skb_dequeue(&free_list)) != NULL)
1085 spin_unlock_bh(&np->rx_lock);
1088 static void xennet_uninit(struct net_device *dev)
1090 struct netfront_info *np = netdev_priv(dev);
1091 xennet_release_tx_bufs(np);
1092 xennet_release_rx_bufs(np);
1093 gnttab_free_grant_references(np->gref_tx_head);
1094 gnttab_free_grant_references(np->gref_rx_head);
1097 static struct net_device * __devinit xennet_create_dev(struct xenbus_device *dev)
1100 struct net_device *netdev;
1101 struct netfront_info *np;
1103 netdev = alloc_etherdev(sizeof(struct netfront_info));
1105 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1107 return ERR_PTR(-ENOMEM);
1110 np = netdev_priv(netdev);
1113 spin_lock_init(&np->tx_lock);
1114 spin_lock_init(&np->rx_lock);
1116 skb_queue_head_init(&np->rx_batch);
1117 np->rx_target = RX_DFL_MIN_TARGET;
1118 np->rx_min_target = RX_DFL_MIN_TARGET;
1119 np->rx_max_target = RX_MAX_TARGET;
1121 init_timer(&np->rx_refill_timer);
1122 np->rx_refill_timer.data = (unsigned long)netdev;
1123 np->rx_refill_timer.function = rx_refill_timeout;
1125 /* Initialise tx_skbs as a free chain containing every entry. */
1126 np->tx_skb_freelist = 0;
1127 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1128 np->tx_skbs[i].link = i+1;
1129 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1132 /* Clear out rx_skbs */
1133 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1134 np->rx_skbs[i] = NULL;
1135 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1138 /* A grant for every tx ring slot */
1139 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1140 &np->gref_tx_head) < 0) {
1141 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1145 /* A grant for every rx ring slot */
1146 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1147 &np->gref_rx_head) < 0) {
1148 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1153 netdev->open = xennet_open;
1154 netdev->hard_start_xmit = xennet_start_xmit;
1155 netdev->stop = xennet_close;
1156 netif_napi_add(netdev, &np->napi, xennet_poll, 64);
1157 netdev->uninit = xennet_uninit;
1158 netdev->change_mtu = xennet_change_mtu;
1159 netdev->features = NETIF_F_IP_CSUM;
1161 SET_ETHTOOL_OPS(netdev, &xennet_ethtool_ops);
1162 SET_NETDEV_DEV(netdev, &dev->dev);
1164 np->netdev = netdev;
1166 netif_carrier_off(netdev);
1171 gnttab_free_grant_references(np->gref_tx_head);
1173 free_netdev(netdev);
1174 return ERR_PTR(err);
1178 * Entry point to this code when a new device is created. Allocate the basic
1179 * structures and the ring buffers for communication with the backend, and
1180 * inform the backend of the appropriate details for those.
1182 static int __devinit netfront_probe(struct xenbus_device *dev,
1183 const struct xenbus_device_id *id)
1186 struct net_device *netdev;
1187 struct netfront_info *info;
1189 netdev = xennet_create_dev(dev);
1190 if (IS_ERR(netdev)) {
1191 err = PTR_ERR(netdev);
1192 xenbus_dev_fatal(dev, err, "creating netdev");
1196 info = netdev_priv(netdev);
1197 dev->dev.driver_data = info;
1199 err = register_netdev(info->netdev);
1201 printk(KERN_WARNING "%s: register_netdev err=%d\n",
1206 err = xennet_sysfs_addif(info->netdev);
1208 unregister_netdev(info->netdev);
1209 printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
1217 free_netdev(netdev);
1218 dev->dev.driver_data = NULL;
1222 static void xennet_end_access(int ref, void *page)
1224 /* This frees the page as a side-effect */
1225 if (ref != GRANT_INVALID_REF)
1226 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1229 static void xennet_disconnect_backend(struct netfront_info *info)
1231 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1232 spin_lock_bh(&info->rx_lock);
1233 spin_lock_irq(&info->tx_lock);
1234 netif_carrier_off(info->netdev);
1235 spin_unlock_irq(&info->tx_lock);
1236 spin_unlock_bh(&info->rx_lock);
1238 if (info->netdev->irq)
1239 unbind_from_irqhandler(info->netdev->irq, info->netdev);
1240 info->evtchn = info->netdev->irq = 0;
1242 /* End access and free the pages */
1243 xennet_end_access(info->tx_ring_ref, info->tx.sring);
1244 xennet_end_access(info->rx_ring_ref, info->rx.sring);
1246 info->tx_ring_ref = GRANT_INVALID_REF;
1247 info->rx_ring_ref = GRANT_INVALID_REF;
1248 info->tx.sring = NULL;
1249 info->rx.sring = NULL;
1253 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1254 * driver restart. We tear down our netif structure and recreate it, but
1255 * leave the device-layer structures intact so that this is transparent to the
1256 * rest of the kernel.
1258 static int netfront_resume(struct xenbus_device *dev)
1260 struct netfront_info *info = dev->dev.driver_data;
1262 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1264 xennet_disconnect_backend(info);
1268 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1270 char *s, *e, *macstr;
1273 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1275 return PTR_ERR(macstr);
1277 for (i = 0; i < ETH_ALEN; i++) {
1278 mac[i] = simple_strtoul(s, &e, 16);
1279 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1290 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1292 struct net_device *dev = dev_id;
1293 struct netfront_info *np = netdev_priv(dev);
1294 unsigned long flags;
1296 spin_lock_irqsave(&np->tx_lock, flags);
1298 if (likely(netif_carrier_ok(dev))) {
1299 xennet_tx_buf_gc(dev);
1300 /* Under tx_lock: protects access to rx shared-ring indexes. */
1301 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
1302 netif_rx_schedule(dev, &np->napi);
1305 spin_unlock_irqrestore(&np->tx_lock, flags);
1310 static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)
1312 struct xen_netif_tx_sring *txs;
1313 struct xen_netif_rx_sring *rxs;
1315 struct net_device *netdev = info->netdev;
1317 info->tx_ring_ref = GRANT_INVALID_REF;
1318 info->rx_ring_ref = GRANT_INVALID_REF;
1319 info->rx.sring = NULL;
1320 info->tx.sring = NULL;
1323 err = xen_net_read_mac(dev, netdev->dev_addr);
1325 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1329 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_KERNEL);
1332 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1335 SHARED_RING_INIT(txs);
1336 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
1338 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
1340 free_page((unsigned long)txs);
1344 info->tx_ring_ref = err;
1345 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_KERNEL);
1348 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1351 SHARED_RING_INIT(rxs);
1352 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
1354 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
1356 free_page((unsigned long)rxs);
1359 info->rx_ring_ref = err;
1361 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1365 err = bind_evtchn_to_irqhandler(info->evtchn, xennet_interrupt,
1366 IRQF_SAMPLE_RANDOM, netdev->name,
1377 /* Common code used when first setting up, and when resuming. */
1378 static int talk_to_backend(struct xenbus_device *dev,
1379 struct netfront_info *info)
1381 const char *message;
1382 struct xenbus_transaction xbt;
1385 /* Create shared ring, alloc event channel. */
1386 err = setup_netfront(dev, info);
1391 err = xenbus_transaction_start(&xbt);
1393 xenbus_dev_fatal(dev, err, "starting transaction");
1397 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref", "%u",
1400 message = "writing tx ring-ref";
1401 goto abort_transaction;
1403 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref", "%u",
1406 message = "writing rx ring-ref";
1407 goto abort_transaction;
1409 err = xenbus_printf(xbt, dev->nodename,
1410 "event-channel", "%u", info->evtchn);
1412 message = "writing event-channel";
1413 goto abort_transaction;
1416 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1419 message = "writing request-rx-copy";
1420 goto abort_transaction;
1423 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1425 message = "writing feature-rx-notify";
1426 goto abort_transaction;
1429 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1431 message = "writing feature-sg";
1432 goto abort_transaction;
1435 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1437 message = "writing feature-gso-tcpv4";
1438 goto abort_transaction;
1441 err = xenbus_transaction_end(xbt, 0);
1445 xenbus_dev_fatal(dev, err, "completing transaction");
1452 xenbus_transaction_end(xbt, 1);
1453 xenbus_dev_fatal(dev, err, "%s", message);
1455 xennet_disconnect_backend(info);
1460 static int xennet_set_sg(struct net_device *dev, u32 data)
1463 struct netfront_info *np = netdev_priv(dev);
1466 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1471 } else if (dev->mtu > ETH_DATA_LEN)
1472 dev->mtu = ETH_DATA_LEN;
1474 return ethtool_op_set_sg(dev, data);
1477 static int xennet_set_tso(struct net_device *dev, u32 data)
1480 struct netfront_info *np = netdev_priv(dev);
1483 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1484 "feature-gso-tcpv4", "%d", &val) < 0)
1490 return ethtool_op_set_tso(dev, data);
1493 static void xennet_set_features(struct net_device *dev)
1495 /* Turn off all GSO bits except ROBUST. */
1496 dev->features &= (1 << NETIF_F_GSO_SHIFT) - 1;
1497 dev->features |= NETIF_F_GSO_ROBUST;
1498 xennet_set_sg(dev, 0);
1500 /* We need checksum offload to enable scatter/gather and TSO. */
1501 if (!(dev->features & NETIF_F_IP_CSUM))
1504 if (!xennet_set_sg(dev, 1))
1505 xennet_set_tso(dev, 1);
1508 static int xennet_connect(struct net_device *dev)
1510 struct netfront_info *np = netdev_priv(dev);
1511 int i, requeue_idx, err;
1512 struct sk_buff *skb;
1514 struct xen_netif_rx_request *req;
1515 unsigned int feature_rx_copy;
1517 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1518 "feature-rx-copy", "%u", &feature_rx_copy);
1520 feature_rx_copy = 0;
1522 if (!feature_rx_copy) {
1524 "backend does not support copying receive path\n");
1528 err = talk_to_backend(np->xbdev, np);
1532 xennet_set_features(dev);
1534 spin_lock_bh(&np->rx_lock);
1535 spin_lock_irq(&np->tx_lock);
1537 /* Step 1: Discard all pending TX packet fragments. */
1538 xennet_release_tx_bufs(np);
1540 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1541 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1542 if (!np->rx_skbs[i])
1545 skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1546 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1547 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1549 gnttab_grant_foreign_access_ref(
1550 ref, np->xbdev->otherend_id,
1551 pfn_to_mfn(page_to_pfn(skb_shinfo(skb)->
1555 req->id = requeue_idx;
1560 np->rx.req_prod_pvt = requeue_idx;
1563 * Step 3: All public and private state should now be sane. Get
1564 * ready to start sending and receiving packets and give the driver
1565 * domain a kick because we've probably just requeued some
1568 netif_carrier_on(np->netdev);
1569 notify_remote_via_irq(np->netdev->irq);
1570 xennet_tx_buf_gc(dev);
1571 xennet_alloc_rx_buffers(dev);
1573 spin_unlock_irq(&np->tx_lock);
1574 spin_unlock_bh(&np->rx_lock);
1580 * Callback received when the backend's state changes.
1582 static void backend_changed(struct xenbus_device *dev,
1583 enum xenbus_state backend_state)
1585 struct netfront_info *np = dev->dev.driver_data;
1586 struct net_device *netdev = np->netdev;
1588 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
1590 switch (backend_state) {
1591 case XenbusStateInitialising:
1592 case XenbusStateInitialised:
1593 case XenbusStateConnected:
1594 case XenbusStateUnknown:
1595 case XenbusStateClosed:
1598 case XenbusStateInitWait:
1599 if (dev->state != XenbusStateInitialising)
1601 if (xennet_connect(netdev) != 0)
1603 xenbus_switch_state(dev, XenbusStateConnected);
1606 case XenbusStateClosing:
1607 xenbus_frontend_closed(dev);
1612 static struct ethtool_ops xennet_ethtool_ops =
1614 .set_tx_csum = ethtool_op_set_tx_csum,
1615 .set_sg = xennet_set_sg,
1616 .set_tso = xennet_set_tso,
1617 .get_link = ethtool_op_get_link,
1621 static ssize_t show_rxbuf_min(struct device *dev,
1622 struct device_attribute *attr, char *buf)
1624 struct net_device *netdev = to_net_dev(dev);
1625 struct netfront_info *info = netdev_priv(netdev);
1627 return sprintf(buf, "%u\n", info->rx_min_target);
1630 static ssize_t store_rxbuf_min(struct device *dev,
1631 struct device_attribute *attr,
1632 const char *buf, size_t len)
1634 struct net_device *netdev = to_net_dev(dev);
1635 struct netfront_info *np = netdev_priv(netdev);
1637 unsigned long target;
1639 if (!capable(CAP_NET_ADMIN))
1642 target = simple_strtoul(buf, &endp, 0);
1646 if (target < RX_MIN_TARGET)
1647 target = RX_MIN_TARGET;
1648 if (target > RX_MAX_TARGET)
1649 target = RX_MAX_TARGET;
1651 spin_lock_bh(&np->rx_lock);
1652 if (target > np->rx_max_target)
1653 np->rx_max_target = target;
1654 np->rx_min_target = target;
1655 if (target > np->rx_target)
1656 np->rx_target = target;
1658 xennet_alloc_rx_buffers(netdev);
1660 spin_unlock_bh(&np->rx_lock);
1664 static ssize_t show_rxbuf_max(struct device *dev,
1665 struct device_attribute *attr, char *buf)
1667 struct net_device *netdev = to_net_dev(dev);
1668 struct netfront_info *info = netdev_priv(netdev);
1670 return sprintf(buf, "%u\n", info->rx_max_target);
1673 static ssize_t store_rxbuf_max(struct device *dev,
1674 struct device_attribute *attr,
1675 const char *buf, size_t len)
1677 struct net_device *netdev = to_net_dev(dev);
1678 struct netfront_info *np = netdev_priv(netdev);
1680 unsigned long target;
1682 if (!capable(CAP_NET_ADMIN))
1685 target = simple_strtoul(buf, &endp, 0);
1689 if (target < RX_MIN_TARGET)
1690 target = RX_MIN_TARGET;
1691 if (target > RX_MAX_TARGET)
1692 target = RX_MAX_TARGET;
1694 spin_lock_bh(&np->rx_lock);
1695 if (target < np->rx_min_target)
1696 np->rx_min_target = target;
1697 np->rx_max_target = target;
1698 if (target < np->rx_target)
1699 np->rx_target = target;
1701 xennet_alloc_rx_buffers(netdev);
1703 spin_unlock_bh(&np->rx_lock);
1707 static ssize_t show_rxbuf_cur(struct device *dev,
1708 struct device_attribute *attr, char *buf)
1710 struct net_device *netdev = to_net_dev(dev);
1711 struct netfront_info *info = netdev_priv(netdev);
1713 return sprintf(buf, "%u\n", info->rx_target);
1716 static struct device_attribute xennet_attrs[] = {
1717 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1718 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1719 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1722 static int xennet_sysfs_addif(struct net_device *netdev)
1727 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1728 err = device_create_file(&netdev->dev,
1737 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1741 static void xennet_sysfs_delif(struct net_device *netdev)
1745 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
1746 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1749 #endif /* CONFIG_SYSFS */
1751 static struct xenbus_device_id netfront_ids[] = {
1757 static int __devexit xennet_remove(struct xenbus_device *dev)
1759 struct netfront_info *info = dev->dev.driver_data;
1761 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1763 unregister_netdev(info->netdev);
1765 xennet_disconnect_backend(info);
1767 del_timer_sync(&info->rx_refill_timer);
1769 xennet_sysfs_delif(info->netdev);
1771 free_netdev(info->netdev);
1776 static struct xenbus_driver netfront = {
1778 .owner = THIS_MODULE,
1779 .ids = netfront_ids,
1780 .probe = netfront_probe,
1781 .remove = __devexit_p(xennet_remove),
1782 .resume = netfront_resume,
1783 .otherend_changed = backend_changed,
1786 static int __init netif_init(void)
1788 if (!is_running_on_xen())
1791 if (is_initial_xendomain())
1794 printk(KERN_INFO "Initialising Xen virtual ethernet driver.\n");
1796 return xenbus_register_frontend(&netfront);
1798 module_init(netif_init);
1801 static void __exit netif_exit(void)
1803 if (is_initial_xendomain())
1806 return xenbus_unregister_driver(&netfront);
1808 module_exit(netif_exit);
1810 MODULE_DESCRIPTION("Xen virtual network device frontend");
1811 MODULE_LICENSE("GPL");