ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 9411:bd930874222e

Clean up netfront. Eliminate some macros in favour of inline functions.
Fix allocation of receive batching arrays (should be per interface).

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Mar 22 16:48:48 2006 +0100 (2006-03-22)
parents ecb99d1ce57d
children b89a155ecd2c
line source
1 /******************************************************************************
2 * Virtual network driver for conversing with remote driver backends.
3 *
4 * Copyright (c) 2002-2005, K A Fraser
5 * Copyright (c) 2005, XenSource Ltd
6 *
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:
12 *
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:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
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
29 * IN THE SOFTWARE.
30 */
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/netdevice.h>
41 #include <linux/inetdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/init.h>
45 #include <linux/bitops.h>
46 #include <linux/proc_fs.h>
47 #include <linux/ethtool.h>
48 #include <linux/in.h>
49 #include <net/sock.h>
50 #include <net/pkt_sched.h>
51 #include <net/arp.h>
52 #include <net/route.h>
53 #include <asm/io.h>
54 #include <asm/uaccess.h>
55 #include <xen/evtchn.h>
56 #include <xen/xenbus.h>
57 #include <xen/interface/io/netif.h>
58 #include <xen/interface/memory.h>
59 #include <xen/balloon.h>
60 #include <asm/page.h>
61 #include <asm/uaccess.h>
62 #include <xen/interface/grant_table.h>
63 #include <xen/gnttab.h>
64 #include <xen/net_driver_util.h>
66 #define GRANT_INVALID_REF 0
68 #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
69 #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
71 static inline void init_skb_shinfo(struct sk_buff *skb)
72 {
73 atomic_set(&(skb_shinfo(skb)->dataref), 1);
74 skb_shinfo(skb)->nr_frags = 0;
75 skb_shinfo(skb)->frag_list = NULL;
76 }
78 struct netfront_info
79 {
80 struct list_head list;
81 struct net_device *netdev;
83 struct net_device_stats stats;
84 unsigned int tx_full;
86 netif_tx_front_ring_t tx;
87 netif_rx_front_ring_t rx;
89 spinlock_t tx_lock;
90 spinlock_t rx_lock;
92 unsigned int handle;
93 unsigned int evtchn, irq;
95 /* What is the status of our connection to the remote backend? */
96 #define BEST_CLOSED 0
97 #define BEST_DISCONNECTED 1
98 #define BEST_CONNECTED 2
99 unsigned int backend_state;
101 /* Is this interface open or closed (down or up)? */
102 #define UST_CLOSED 0
103 #define UST_OPEN 1
104 unsigned int user_state;
106 /* Receive-ring batched refills. */
107 #define RX_MIN_TARGET 8
108 #define RX_DFL_MIN_TARGET 64
109 #define RX_MAX_TARGET NET_RX_RING_SIZE
110 int rx_min_target, rx_max_target, rx_target;
111 struct sk_buff_head rx_batch;
113 struct timer_list rx_refill_timer;
115 /*
116 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
117 * array is an index into a chain of free entries.
118 */
119 struct sk_buff *tx_skbs[NET_TX_RING_SIZE+1];
120 struct sk_buff *rx_skbs[NET_RX_RING_SIZE+1];
122 grant_ref_t gref_tx_head;
123 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
124 grant_ref_t gref_rx_head;
125 grant_ref_t grant_rx_ref[NET_TX_RING_SIZE + 1];
127 struct xenbus_device *xbdev;
128 int tx_ring_ref;
129 int rx_ring_ref;
130 u8 mac[ETH_ALEN];
132 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
133 multicall_entry_t rx_mcl[NET_RX_RING_SIZE+1];
134 mmu_update_t rx_mmu[NET_RX_RING_SIZE];
135 };
137 /*
138 * Access macros for acquiring freeing slots in {tx,rx}_skbs[].
139 */
141 static inline void add_id_to_freelist(struct sk_buff **list, unsigned short id)
142 {
143 list[id] = list[0];
144 list[0] = (void *)(unsigned long)id;
145 }
147 static inline unsigned short get_id_from_freelist(struct sk_buff **list)
148 {
149 unsigned int id = (unsigned int)(unsigned long)list[0];
150 list[0] = list[id];
151 return id;
152 }
154 #ifdef DEBUG
155 static char *be_state_name[] = {
156 [BEST_CLOSED] = "closed",
157 [BEST_DISCONNECTED] = "disconnected",
158 [BEST_CONNECTED] = "connected",
159 };
160 #endif
162 #define DPRINTK(fmt, args...) pr_debug("netfront (%s:%d) " fmt, \
163 __FUNCTION__, __LINE__, ##args)
164 #define IPRINTK(fmt, args...) \
165 printk(KERN_INFO "netfront: " fmt, ##args)
166 #define WPRINTK(fmt, args...) \
167 printk(KERN_WARNING "netfront: " fmt, ##args)
170 static int talk_to_backend(struct xenbus_device *, struct netfront_info *);
171 static int setup_device(struct xenbus_device *, struct netfront_info *);
172 static int create_netdev(int, struct xenbus_device *, struct net_device **);
174 static void netfront_closing(struct xenbus_device *);
176 static void end_access(int, void *);
177 static void netif_disconnect_backend(struct netfront_info *);
178 static void close_netdev(struct netfront_info *);
179 static void netif_free(struct netfront_info *);
181 static void show_device(struct netfront_info *);
183 static void network_connect(struct net_device *);
184 static void network_tx_buf_gc(struct net_device *);
185 static void network_alloc_rx_buffers(struct net_device *);
186 static int send_fake_arp(struct net_device *);
188 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs);
190 #ifdef CONFIG_PROC_FS
191 static int xennet_proc_init(void);
192 static int xennet_proc_addif(struct net_device *dev);
193 static void xennet_proc_delif(struct net_device *dev);
194 #else
195 #define xennet_proc_init() (0)
196 #define xennet_proc_addif(d) (0)
197 #define xennet_proc_delif(d) ((void)0)
198 #endif
201 /**
202 * Entry point to this code when a new device is created. Allocate the basic
203 * structures and the ring buffers for communication with the backend, and
204 * inform the backend of the appropriate details for those. Switch to
205 * Connected state.
206 */
207 static int netfront_probe(struct xenbus_device *dev,
208 const struct xenbus_device_id *id)
209 {
210 int err;
211 struct net_device *netdev;
212 struct netfront_info *info;
213 unsigned int handle;
215 err = xenbus_scanf(XBT_NULL, dev->nodename, "handle", "%u", &handle);
216 if (err != 1) {
217 xenbus_dev_fatal(dev, err, "reading handle");
218 return err;
219 }
221 err = create_netdev(handle, dev, &netdev);
222 if (err) {
223 xenbus_dev_fatal(dev, err, "creating netdev");
224 return err;
225 }
227 info = netdev_priv(netdev);
228 dev->data = info;
230 err = talk_to_backend(dev, info);
231 if (err) {
232 kfree(info);
233 dev->data = NULL;
234 return err;
235 }
237 return 0;
238 }
241 /**
242 * We are reconnecting to the backend, due to a suspend/resume, or a backend
243 * driver restart. We tear down our netif structure and recreate it, but
244 * leave the device-layer structures intact so that this is transparent to the
245 * rest of the kernel.
246 */
247 static int netfront_resume(struct xenbus_device *dev)
248 {
249 struct netfront_info *info = dev->data;
251 DPRINTK("%s\n", dev->nodename);
253 netif_disconnect_backend(info);
254 return talk_to_backend(dev, info);
255 }
258 /* Common code used when first setting up, and when resuming. */
259 static int talk_to_backend(struct xenbus_device *dev,
260 struct netfront_info *info)
261 {
262 const char *message;
263 xenbus_transaction_t xbt;
264 int err;
266 err = xen_net_read_mac(dev, info->mac);
267 if (err) {
268 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
269 goto out;
270 }
272 /* Create shared ring, alloc event channel. */
273 err = setup_device(dev, info);
274 if (err)
275 goto out;
277 again:
278 err = xenbus_transaction_start(&xbt);
279 if (err) {
280 xenbus_dev_fatal(dev, err, "starting transaction");
281 goto destroy_ring;
282 }
284 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
285 info->tx_ring_ref);
286 if (err) {
287 message = "writing tx ring-ref";
288 goto abort_transaction;
289 }
290 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
291 info->rx_ring_ref);
292 if (err) {
293 message = "writing rx ring-ref";
294 goto abort_transaction;
295 }
296 err = xenbus_printf(xbt, dev->nodename,
297 "event-channel", "%u", info->evtchn);
298 if (err) {
299 message = "writing event-channel";
300 goto abort_transaction;
301 }
303 err = xenbus_printf(xbt, dev->nodename,
304 "state", "%d", XenbusStateConnected);
305 if (err) {
306 message = "writing frontend XenbusStateConnected";
307 goto abort_transaction;
308 }
310 err = xenbus_transaction_end(xbt, 0);
311 if (err) {
312 if (err == -EAGAIN)
313 goto again;
314 xenbus_dev_fatal(dev, err, "completing transaction");
315 goto destroy_ring;
316 }
318 return 0;
320 abort_transaction:
321 xenbus_transaction_end(xbt, 1);
322 xenbus_dev_fatal(dev, err, "%s", message);
323 destroy_ring:
324 netif_free(info);
325 out:
326 return err;
327 }
330 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
331 {
332 netif_tx_sring_t *txs;
333 netif_rx_sring_t *rxs;
334 int err;
335 struct net_device *netdev = info->netdev;
337 info->tx_ring_ref = GRANT_INVALID_REF;
338 info->rx_ring_ref = GRANT_INVALID_REF;
339 info->rx.sring = NULL;
340 info->tx.sring = NULL;
341 info->irq = 0;
343 txs = (netif_tx_sring_t *)__get_free_page(GFP_KERNEL);
344 if (!txs) {
345 err = -ENOMEM;
346 xenbus_dev_fatal(dev, err, "allocating tx ring page");
347 goto fail;
348 }
349 rxs = (netif_rx_sring_t *)__get_free_page(GFP_KERNEL);
350 if (!rxs) {
351 err = -ENOMEM;
352 xenbus_dev_fatal(dev, err, "allocating rx ring page");
353 goto fail;
354 }
355 memset(txs, 0, PAGE_SIZE);
356 memset(rxs, 0, PAGE_SIZE);
357 info->backend_state = BEST_DISCONNECTED;
359 SHARED_RING_INIT(txs);
360 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
362 SHARED_RING_INIT(rxs);
363 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
365 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
366 if (err < 0)
367 goto fail;
368 info->tx_ring_ref = err;
370 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
371 if (err < 0)
372 goto fail;
373 info->rx_ring_ref = err;
375 err = xenbus_alloc_evtchn(dev, &info->evtchn);
376 if (err)
377 goto fail;
379 memcpy(netdev->dev_addr, info->mac, ETH_ALEN);
380 network_connect(netdev);
381 info->irq = bind_evtchn_to_irqhandler(
382 info->evtchn, netif_int, SA_SAMPLE_RANDOM, netdev->name,
383 netdev);
384 (void)send_fake_arp(netdev);
385 show_device(info);
387 return 0;
389 fail:
390 netif_free(info);
391 return err;
392 }
395 /**
396 * Callback received when the backend's state changes.
397 */
398 static void backend_changed(struct xenbus_device *dev,
399 XenbusState backend_state)
400 {
401 DPRINTK("\n");
403 switch (backend_state) {
404 case XenbusStateInitialising:
405 case XenbusStateInitWait:
406 case XenbusStateInitialised:
407 case XenbusStateConnected:
408 case XenbusStateUnknown:
409 case XenbusStateClosed:
410 break;
412 case XenbusStateClosing:
413 netfront_closing(dev);
414 break;
415 }
416 }
419 /** Send a packet on a net device to encourage switches to learn the
420 * MAC. We send a fake ARP request.
421 *
422 * @param dev device
423 * @return 0 on success, error code otherwise
424 */
425 static int send_fake_arp(struct net_device *dev)
426 {
427 struct sk_buff *skb;
428 u32 src_ip, dst_ip;
430 dst_ip = INADDR_BROADCAST;
431 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
433 /* No IP? Then nothing to do. */
434 if (src_ip == 0)
435 return 0;
437 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
438 dst_ip, dev, src_ip,
439 /*dst_hw*/ NULL, /*src_hw*/ NULL,
440 /*target_hw*/ dev->dev_addr);
441 if (skb == NULL)
442 return -ENOMEM;
444 return dev_queue_xmit(skb);
445 }
448 static int network_open(struct net_device *dev)
449 {
450 struct netfront_info *np = netdev_priv(dev);
452 memset(&np->stats, 0, sizeof(np->stats));
454 np->user_state = UST_OPEN;
456 network_alloc_rx_buffers(dev);
457 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
459 netif_start_queue(dev);
461 return 0;
462 }
464 static void network_tx_buf_gc(struct net_device *dev)
465 {
466 RING_IDX i, prod;
467 unsigned short id;
468 struct netfront_info *np = netdev_priv(dev);
469 struct sk_buff *skb;
471 if (np->backend_state != BEST_CONNECTED)
472 return;
474 do {
475 prod = np->tx.sring->rsp_prod;
476 rmb(); /* Ensure we see responses up to 'rp'. */
478 for (i = np->tx.rsp_cons; i != prod; i++) {
479 id = RING_GET_RESPONSE(&np->tx, i)->id;
480 skb = np->tx_skbs[id];
481 if (unlikely(gnttab_query_foreign_access(
482 np->grant_tx_ref[id]) != 0)) {
483 printk(KERN_ALERT "network_tx_buf_gc: warning "
484 "-- grant still in use by backend "
485 "domain.\n");
486 goto out;
487 }
488 gnttab_end_foreign_access_ref(
489 np->grant_tx_ref[id], GNTMAP_readonly);
490 gnttab_release_grant_reference(
491 &np->gref_tx_head, np->grant_tx_ref[id]);
492 np->grant_tx_ref[id] = GRANT_INVALID_REF;
493 add_id_to_freelist(np->tx_skbs, id);
494 dev_kfree_skb_irq(skb);
495 }
497 np->tx.rsp_cons = prod;
499 /*
500 * Set a new event, then check for race with update of tx_cons.
501 * Note that it is essential to schedule a callback, no matter
502 * how few buffers are pending. Even if there is space in the
503 * transmit ring, higher layers may be blocked because too much
504 * data is outstanding: in such cases notification from Xen is
505 * likely to be the only kick that we'll get.
506 */
507 np->tx.sring->rsp_event =
508 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
509 mb();
510 } while (prod != np->tx.sring->rsp_prod);
512 out:
513 if (np->tx_full &&
514 ((np->tx.sring->req_prod - prod) < NET_TX_RING_SIZE)) {
515 np->tx_full = 0;
516 if (np->user_state == UST_OPEN)
517 netif_wake_queue(dev);
518 }
519 }
522 static void rx_refill_timeout(unsigned long data)
523 {
524 struct net_device *dev = (struct net_device *)data;
525 netif_rx_schedule(dev);
526 }
529 static void network_alloc_rx_buffers(struct net_device *dev)
530 {
531 unsigned short id;
532 struct netfront_info *np = netdev_priv(dev);
533 struct sk_buff *skb;
534 int i, batch_target;
535 RING_IDX req_prod = np->rx.req_prod_pvt;
536 struct xen_memory_reservation reservation;
537 grant_ref_t ref;
539 if (unlikely(np->backend_state != BEST_CONNECTED))
540 return;
542 /*
543 * Allocate skbuffs greedily, even though we batch updates to the
544 * receive ring. This creates a less bursty demand on the memory
545 * allocator, so should reduce the chance of failed allocation requests
546 * both for ourself and for other kernel subsystems.
547 */
548 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
549 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
550 /*
551 * Subtract dev_alloc_skb headroom (16 bytes) and shared info
552 * tailroom then round down to SKB_DATA_ALIGN boundary.
553 */
554 skb = __dev_alloc_skb(
555 ((PAGE_SIZE - sizeof(struct skb_shared_info)) &
556 (-SKB_DATA_ALIGN(1))) - 16,
557 GFP_ATOMIC|__GFP_NOWARN);
558 if (skb == NULL) {
559 /* Any skbuffs queued for refill? Force them out. */
560 if (i != 0)
561 goto refill;
562 /* Could not allocate any skbuffs. Try again later. */
563 mod_timer(&np->rx_refill_timer,
564 jiffies + (HZ/10));
565 return;
566 }
567 __skb_queue_tail(&np->rx_batch, skb);
568 }
570 /* Is the batch large enough to be worthwhile? */
571 if (i < (np->rx_target/2))
572 return;
574 /* Adjust our fill target if we risked running out of buffers. */
575 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
576 ((np->rx_target *= 2) > np->rx_max_target))
577 np->rx_target = np->rx_max_target;
579 refill:
580 for (i = 0; ; i++) {
581 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
582 break;
584 skb->dev = dev;
586 id = get_id_from_freelist(np->rx_skbs);
588 np->rx_skbs[id] = skb;
590 RING_GET_REQUEST(&np->rx, req_prod + i)->id = id;
591 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
592 BUG_ON((signed short)ref < 0);
593 np->grant_rx_ref[id] = ref;
594 gnttab_grant_foreign_transfer_ref(ref,
595 np->xbdev->otherend_id,
596 __pa(skb->head) >> PAGE_SHIFT);
597 RING_GET_REQUEST(&np->rx, req_prod + i)->gref = ref;
598 np->rx_pfn_array[i] = virt_to_mfn(skb->head);
600 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
601 /* Remove this page before passing back to Xen. */
602 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
603 INVALID_P2M_ENTRY);
604 MULTI_update_va_mapping(np->rx_mcl+i,
605 (unsigned long)skb->head,
606 __pte(0), 0);
607 }
608 }
610 /* Tell the ballon driver what is going on. */
611 balloon_update_driver_allowance(i);
613 reservation.extent_start = np->rx_pfn_array;
614 reservation.nr_extents = i;
615 reservation.extent_order = 0;
616 reservation.address_bits = 0;
617 reservation.domid = DOMID_SELF;
619 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
620 /* After all PTEs have been zapped, flush the TLB. */
621 np->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
622 UVMF_TLB_FLUSH|UVMF_ALL;
624 /* Give away a batch of pages. */
625 np->rx_mcl[i].op = __HYPERVISOR_memory_op;
626 np->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
627 np->rx_mcl[i].args[1] = (unsigned long)&reservation;
629 /* Zap PTEs and give away pages in one big multicall. */
630 (void)HYPERVISOR_multicall(np->rx_mcl, i+1);
632 /* Check return status of HYPERVISOR_memory_op(). */
633 if (unlikely(np->rx_mcl[i].result != i))
634 panic("Unable to reduce memory reservation\n");
635 } else
636 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
637 &reservation) != i)
638 panic("Unable to reduce memory reservation\n");
640 /* Above is a suitable barrier to ensure backend will see requests. */
641 np->rx.req_prod_pvt = req_prod + i;
642 RING_PUSH_REQUESTS(&np->rx);
643 }
646 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
647 {
648 unsigned short id;
649 struct netfront_info *np = netdev_priv(dev);
650 netif_tx_request_t *tx;
651 RING_IDX i;
652 grant_ref_t ref;
653 unsigned long mfn;
654 int notify;
656 if (unlikely(np->tx_full)) {
657 printk(KERN_ALERT "%s: full queue wasn't stopped!\n",
658 dev->name);
659 netif_stop_queue(dev);
660 goto drop;
661 }
663 if (unlikely((((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >=
664 PAGE_SIZE)) {
665 struct sk_buff *nskb;
666 nskb = __dev_alloc_skb(skb->len, GFP_ATOMIC|__GFP_NOWARN);
667 if (unlikely(nskb == NULL))
668 goto drop;
669 skb_put(nskb, skb->len);
670 memcpy(nskb->data, skb->data, skb->len);
671 nskb->dev = skb->dev;
672 dev_kfree_skb(skb);
673 skb = nskb;
674 }
676 spin_lock_irq(&np->tx_lock);
678 if (np->backend_state != BEST_CONNECTED) {
679 spin_unlock_irq(&np->tx_lock);
680 goto drop;
681 }
683 i = np->tx.req_prod_pvt;
685 id = get_id_from_freelist(np->tx_skbs);
686 np->tx_skbs[id] = skb;
688 tx = RING_GET_REQUEST(&np->tx, i);
690 tx->id = id;
691 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
692 BUG_ON((signed short)ref < 0);
693 mfn = virt_to_mfn(skb->data);
694 gnttab_grant_foreign_access_ref(
695 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
696 tx->gref = np->grant_tx_ref[id] = ref;
697 tx->offset = (unsigned long)skb->data & ~PAGE_MASK;
698 tx->size = skb->len;
699 tx->flags = (skb->ip_summed == CHECKSUM_HW) ? NETTXF_csum_blank : 0;
701 np->tx.req_prod_pvt = i + 1;
702 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
703 if (notify)
704 notify_remote_via_irq(np->irq);
706 network_tx_buf_gc(dev);
708 if (RING_FULL(&np->tx)) {
709 np->tx_full = 1;
710 netif_stop_queue(dev);
711 }
713 spin_unlock_irq(&np->tx_lock);
715 np->stats.tx_bytes += skb->len;
716 np->stats.tx_packets++;
718 return 0;
720 drop:
721 np->stats.tx_dropped++;
722 dev_kfree_skb(skb);
723 return 0;
724 }
726 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
727 {
728 struct net_device *dev = dev_id;
729 struct netfront_info *np = netdev_priv(dev);
730 unsigned long flags;
732 spin_lock_irqsave(&np->tx_lock, flags);
733 network_tx_buf_gc(dev);
734 spin_unlock_irqrestore(&np->tx_lock, flags);
736 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx) &&
737 (np->user_state == UST_OPEN))
738 netif_rx_schedule(dev);
740 return IRQ_HANDLED;
741 }
744 static int netif_poll(struct net_device *dev, int *pbudget)
745 {
746 struct netfront_info *np = netdev_priv(dev);
747 struct sk_buff *skb, *nskb;
748 netif_rx_response_t *rx;
749 RING_IDX i, rp;
750 mmu_update_t *mmu = np->rx_mmu;
751 multicall_entry_t *mcl = np->rx_mcl;
752 int work_done, budget, more_to_do = 1;
753 struct sk_buff_head rxq;
754 unsigned long flags;
755 unsigned long mfn;
756 grant_ref_t ref;
758 spin_lock(&np->rx_lock);
760 if (np->backend_state != BEST_CONNECTED) {
761 spin_unlock(&np->rx_lock);
762 return 0;
763 }
765 skb_queue_head_init(&rxq);
767 if ((budget = *pbudget) > dev->quota)
768 budget = dev->quota;
769 rp = np->rx.sring->rsp_prod;
770 rmb(); /* Ensure we see queued responses up to 'rp'. */
772 for (i = np->rx.rsp_cons, work_done = 0;
773 (i != rp) && (work_done < budget);
774 i++, work_done++) {
775 rx = RING_GET_RESPONSE(&np->rx, i);
777 /*
778 * This definitely indicates a bug, either in this driver or
779 * in the backend driver. In future this should flag the bad
780 * situation to the system controller to reboot the backed.
781 */
782 if ((ref = np->grant_rx_ref[rx->id]) == GRANT_INVALID_REF) {
783 WPRINTK("Bad rx response id %d.\n", rx->id);
784 work_done--;
785 continue;
786 }
788 /* Memory pressure, insufficient buffer headroom, ... */
789 if ((mfn = gnttab_end_foreign_transfer_ref(ref)) == 0) {
790 if (net_ratelimit())
791 WPRINTK("Unfulfilled rx req (id=%d, st=%d).\n",
792 rx->id, rx->status);
793 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id =
794 rx->id;
795 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref =
796 ref;
797 np->rx.req_prod_pvt++;
798 RING_PUSH_REQUESTS(&np->rx);
799 work_done--;
800 continue;
801 }
803 gnttab_release_grant_reference(&np->gref_rx_head, ref);
804 np->grant_rx_ref[rx->id] = GRANT_INVALID_REF;
806 skb = np->rx_skbs[rx->id];
807 add_id_to_freelist(np->rx_skbs, rx->id);
809 /* NB. We handle skb overflow later. */
810 skb->data = skb->head + rx->offset;
811 skb->len = rx->status;
812 skb->tail = skb->data + skb->len;
814 if (rx->flags & NETRXF_csum_valid)
815 skb->ip_summed = CHECKSUM_UNNECESSARY;
817 np->stats.rx_packets++;
818 np->stats.rx_bytes += rx->status;
820 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
821 /* Remap the page. */
822 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
823 pfn_pte_ma(mfn, PAGE_KERNEL),
824 0);
825 mcl++;
826 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
827 | MMU_MACHPHYS_UPDATE;
828 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
829 mmu++;
831 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
832 mfn);
833 }
835 __skb_queue_tail(&rxq, skb);
836 }
838 /* Some pages are no longer absent... */
839 balloon_update_driver_allowance(-work_done);
841 /* Do all the remapping work, and M2P updates, in one big hypercall. */
842 if (likely((mcl - np->rx_mcl) != 0)) {
843 mcl->op = __HYPERVISOR_mmu_update;
844 mcl->args[0] = (unsigned long)np->rx_mmu;
845 mcl->args[1] = mmu - np->rx_mmu;
846 mcl->args[2] = 0;
847 mcl->args[3] = DOMID_SELF;
848 mcl++;
849 (void)HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
850 }
852 while ((skb = __skb_dequeue(&rxq)) != NULL) {
853 if (skb->len > (dev->mtu + ETH_HLEN + 4)) {
854 if (net_ratelimit())
855 printk(KERN_INFO "Received packet too big for "
856 "MTU (%d > %d)\n",
857 skb->len - ETH_HLEN - 4, dev->mtu);
858 skb->len = 0;
859 skb->tail = skb->data;
860 init_skb_shinfo(skb);
861 dev_kfree_skb(skb);
862 continue;
863 }
865 /*
866 * Enough room in skbuff for the data we were passed? Also,
867 * Linux expects at least 16 bytes headroom in each rx buffer.
868 */
869 if (unlikely(skb->tail > skb->end) ||
870 unlikely((skb->data - skb->head) < 16)) {
871 if (net_ratelimit()) {
872 if (skb->tail > skb->end)
873 printk(KERN_INFO "Received packet "
874 "is %zd bytes beyond tail.\n",
875 skb->tail - skb->end);
876 else
877 printk(KERN_INFO "Received packet "
878 "is %zd bytes before head.\n",
879 16 - (skb->data - skb->head));
880 }
882 nskb = __dev_alloc_skb(skb->len + 2,
883 GFP_ATOMIC|__GFP_NOWARN);
884 if (nskb != NULL) {
885 skb_reserve(nskb, 2);
886 skb_put(nskb, skb->len);
887 memcpy(nskb->data, skb->data, skb->len);
888 nskb->dev = skb->dev;
889 nskb->ip_summed = skb->ip_summed;
890 }
892 /* Reinitialise and then destroy the old skbuff. */
893 skb->len = 0;
894 skb->tail = skb->data;
895 init_skb_shinfo(skb);
896 dev_kfree_skb(skb);
898 /* Switch old for new, if we copied the buffer. */
899 if ((skb = nskb) == NULL)
900 continue;
901 }
903 /* Set the shinfo area, which is hidden behind the data. */
904 init_skb_shinfo(skb);
905 /* Ethernet work: Delayed to here as it peeks the header. */
906 skb->protocol = eth_type_trans(skb, dev);
908 /* Pass it up. */
909 netif_receive_skb(skb);
910 dev->last_rx = jiffies;
911 }
913 np->rx.rsp_cons = i;
915 /* If we get a callback with very few responses, reduce fill target. */
916 /* NB. Note exponential increase, linear decrease. */
917 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
918 ((3*np->rx_target) / 4)) &&
919 (--np->rx_target < np->rx_min_target))
920 np->rx_target = np->rx_min_target;
922 network_alloc_rx_buffers(dev);
924 *pbudget -= work_done;
925 dev->quota -= work_done;
927 if (work_done < budget) {
928 local_irq_save(flags);
930 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
931 if (!more_to_do)
932 __netif_rx_complete(dev);
934 local_irq_restore(flags);
935 }
937 spin_unlock(&np->rx_lock);
939 return more_to_do;
940 }
943 static int network_close(struct net_device *dev)
944 {
945 struct netfront_info *np = netdev_priv(dev);
946 np->user_state = UST_CLOSED;
947 netif_stop_queue(np->netdev);
948 return 0;
949 }
952 static struct net_device_stats *network_get_stats(struct net_device *dev)
953 {
954 struct netfront_info *np = netdev_priv(dev);
955 return &np->stats;
956 }
958 static void network_connect(struct net_device *dev)
959 {
960 struct netfront_info *np;
961 int i, requeue_idx;
962 netif_tx_request_t *tx;
963 struct sk_buff *skb;
965 np = netdev_priv(dev);
966 spin_lock_irq(&np->tx_lock);
967 spin_lock(&np->rx_lock);
969 /* Recovery procedure: */
971 /* Step 1: Reinitialise variables. */
972 np->tx_full = 0;
974 /*
975 * Step 2: Rebuild the RX and TX ring contents.
976 * NB. We could just free the queued TX packets now but we hope
977 * that sending them out might do some good. We have to rebuild
978 * the RX ring because some of our pages are currently flipped out
979 * so we can't just free the RX skbs.
980 * NB2. Freelist index entries are always going to be less than
981 * __PAGE_OFFSET, whereas pointers to skbs will always be equal or
982 * greater than __PAGE_OFFSET: we use this property to distinguish
983 * them.
984 */
986 /*
987 * Rebuild the TX buffer freelist and the TX ring itself.
988 * NB. This reorders packets. We could keep more private state
989 * to avoid this but maybe it doesn't matter so much given the
990 * interface has been down.
991 */
992 for (requeue_idx = 0, i = 1; i <= NET_TX_RING_SIZE; i++) {
993 if ((unsigned long)np->tx_skbs[i] < __PAGE_OFFSET)
994 continue;
996 skb = np->tx_skbs[i];
998 tx = RING_GET_REQUEST(&np->tx, requeue_idx);
999 requeue_idx++;
1001 tx->id = i;
1002 gnttab_grant_foreign_access_ref(
1003 np->grant_tx_ref[i], np->xbdev->otherend_id,
1004 virt_to_mfn(np->tx_skbs[i]->data),
1005 GNTMAP_readonly);
1006 tx->gref = np->grant_tx_ref[i];
1007 tx->offset = (unsigned long)skb->data & ~PAGE_MASK;
1008 tx->size = skb->len;
1009 tx->flags = (skb->ip_summed == CHECKSUM_HW) ?
1010 NETTXF_csum_blank : 0;
1012 np->stats.tx_bytes += skb->len;
1013 np->stats.tx_packets++;
1016 np->tx.req_prod_pvt = requeue_idx;
1017 RING_PUSH_REQUESTS(&np->tx);
1019 /* Rebuild the RX buffer freelist and the RX ring itself. */
1020 for (requeue_idx = 0, i = 1; i <= NET_RX_RING_SIZE; i++) {
1021 if ((unsigned long)np->rx_skbs[i] < __PAGE_OFFSET)
1022 continue;
1023 gnttab_grant_foreign_transfer_ref(
1024 np->grant_rx_ref[i], np->xbdev->otherend_id,
1025 __pa(np->rx_skbs[i]->data) >> PAGE_SHIFT);
1026 RING_GET_REQUEST(&np->rx, requeue_idx)->gref =
1027 np->grant_rx_ref[i];
1028 RING_GET_REQUEST(&np->rx, requeue_idx)->id = i;
1029 requeue_idx++;
1032 np->rx.req_prod_pvt = requeue_idx;
1033 RING_PUSH_REQUESTS(&np->rx);
1035 /*
1036 * Step 3: All public and private state should now be sane. Get
1037 * ready to start sending and receiving packets and give the driver
1038 * domain a kick because we've probably just requeued some
1039 * packets.
1040 */
1041 np->backend_state = BEST_CONNECTED;
1042 notify_remote_via_irq(np->irq);
1043 network_tx_buf_gc(dev);
1045 if (np->user_state == UST_OPEN)
1046 netif_start_queue(dev);
1048 spin_unlock(&np->rx_lock);
1049 spin_unlock_irq(&np->tx_lock);
1052 static void show_device(struct netfront_info *np)
1054 #ifdef DEBUG
1055 if (np) {
1056 IPRINTK("<vif handle=%u %s(%s) evtchn=%u tx=%p rx=%p>\n",
1057 np->handle,
1058 be_state_name[np->backend_state],
1059 np->user_state ? "open" : "closed",
1060 np->evtchn,
1061 np->tx,
1062 np->rx);
1063 } else
1064 IPRINTK("<vif NULL>\n");
1065 #endif
1068 static void netif_uninit(struct net_device *dev)
1070 struct netfront_info *np = netdev_priv(dev);
1071 gnttab_free_grant_references(np->gref_tx_head);
1072 gnttab_free_grant_references(np->gref_rx_head);
1075 static struct ethtool_ops network_ethtool_ops =
1077 .get_tx_csum = ethtool_op_get_tx_csum,
1078 .set_tx_csum = ethtool_op_set_tx_csum,
1079 };
1081 /** Create a network device.
1082 * @param handle device handle
1083 * @param val return parameter for created device
1084 * @return 0 on success, error code otherwise
1085 */
1086 static int create_netdev(int handle, struct xenbus_device *dev,
1087 struct net_device **val)
1089 int i, err = 0;
1090 struct net_device *netdev = NULL;
1091 struct netfront_info *np = NULL;
1093 if ((netdev = alloc_etherdev(sizeof(struct netfront_info))) == NULL) {
1094 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1095 __FUNCTION__);
1096 err = -ENOMEM;
1097 goto exit;
1100 np = netdev_priv(netdev);
1101 np->backend_state = BEST_CLOSED;
1102 np->user_state = UST_CLOSED;
1103 np->handle = handle;
1104 np->xbdev = dev;
1106 spin_lock_init(&np->tx_lock);
1107 spin_lock_init(&np->rx_lock);
1109 skb_queue_head_init(&np->rx_batch);
1110 np->rx_target = RX_DFL_MIN_TARGET;
1111 np->rx_min_target = RX_DFL_MIN_TARGET;
1112 np->rx_max_target = RX_MAX_TARGET;
1114 init_timer(&np->rx_refill_timer);
1115 np->rx_refill_timer.data = (unsigned long)netdev;
1116 np->rx_refill_timer.function = rx_refill_timeout;
1118 /* Initialise {tx,rx}_skbs as a free chain containing every entry. */
1119 for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1120 np->tx_skbs[i] = (void *)((unsigned long) i+1);
1121 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1124 for (i = 0; i <= NET_RX_RING_SIZE; i++) {
1125 np->rx_skbs[i] = (void *)((unsigned long) i+1);
1126 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1129 /* A grant for every tx ring slot */
1130 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
1131 &np->gref_tx_head) < 0) {
1132 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1133 err = -ENOMEM;
1134 goto exit;
1136 /* A grant for every rx ring slot */
1137 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
1138 &np->gref_rx_head) < 0) {
1139 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1140 gnttab_free_grant_references(np->gref_tx_head);
1141 err = -ENOMEM;
1142 goto exit;
1145 netdev->open = network_open;
1146 netdev->hard_start_xmit = network_start_xmit;
1147 netdev->stop = network_close;
1148 netdev->get_stats = network_get_stats;
1149 netdev->poll = netif_poll;
1150 netdev->uninit = netif_uninit;
1151 netdev->weight = 64;
1152 netdev->features = NETIF_F_IP_CSUM;
1154 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
1155 SET_MODULE_OWNER(netdev);
1156 SET_NETDEV_DEV(netdev, &dev->dev);
1158 if ((err = register_netdev(netdev)) != 0) {
1159 printk(KERN_WARNING "%s> register_netdev err=%d\n",
1160 __FUNCTION__, err);
1161 goto exit_free_grefs;
1164 if ((err = xennet_proc_addif(netdev)) != 0) {
1165 unregister_netdev(netdev);
1166 goto exit_free_grefs;
1169 np->netdev = netdev;
1171 exit:
1172 if (err != 0)
1173 kfree(netdev);
1174 else if (val != NULL)
1175 *val = netdev;
1176 return err;
1178 exit_free_grefs:
1179 gnttab_free_grant_references(np->gref_tx_head);
1180 gnttab_free_grant_references(np->gref_rx_head);
1181 goto exit;
1184 /*
1185 * We use this notifier to send out a fake ARP reply to reset switches and
1186 * router ARP caches when an IP interface is brought up on a VIF.
1187 */
1188 static int
1189 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
1191 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
1192 struct net_device *dev = ifa->ifa_dev->dev;
1194 /* UP event and is it one of our devices? */
1195 if (event == NETDEV_UP && dev->open == network_open)
1196 (void)send_fake_arp(dev);
1198 return NOTIFY_DONE;
1202 /* ** Close down ** */
1205 /**
1206 * Handle the change of state of the backend to Closing. We must delete our
1207 * device-layer structures now, to ensure that writes are flushed through to
1208 * the backend. Once is this done, we can switch to Closed in
1209 * acknowledgement.
1210 */
1211 static void netfront_closing(struct xenbus_device *dev)
1213 struct netfront_info *info = dev->data;
1215 DPRINTK("netfront_closing: %s removed\n", dev->nodename);
1217 close_netdev(info);
1219 xenbus_switch_state(dev, XBT_NULL, XenbusStateClosed);
1223 static int netfront_remove(struct xenbus_device *dev)
1225 struct netfront_info *info = dev->data;
1227 DPRINTK("%s\n", dev->nodename);
1229 netif_disconnect_backend(info);
1230 free_netdev(info->netdev);
1232 return 0;
1236 static void close_netdev(struct netfront_info *info)
1238 spin_lock_irq(&info->netdev->xmit_lock);
1239 netif_stop_queue(info->netdev);
1240 spin_unlock_irq(&info->netdev->xmit_lock);
1242 #ifdef CONFIG_PROC_FS
1243 xennet_proc_delif(info->netdev);
1244 #endif
1246 del_timer_sync(&info->rx_refill_timer);
1248 unregister_netdev(info->netdev);
1252 static void netif_disconnect_backend(struct netfront_info *info)
1254 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1255 spin_lock_irq(&info->tx_lock);
1256 spin_lock(&info->rx_lock);
1257 info->backend_state = BEST_DISCONNECTED;
1258 spin_unlock(&info->rx_lock);
1259 spin_unlock_irq(&info->tx_lock);
1261 if (info->irq)
1262 unbind_from_irqhandler(info->irq, info->netdev);
1263 info->evtchn = info->irq = 0;
1265 end_access(info->tx_ring_ref, info->tx.sring);
1266 end_access(info->rx_ring_ref, info->rx.sring);
1267 info->tx_ring_ref = GRANT_INVALID_REF;
1268 info->rx_ring_ref = GRANT_INVALID_REF;
1269 info->tx.sring = NULL;
1270 info->rx.sring = NULL;
1274 static void netif_free(struct netfront_info *info)
1276 close_netdev(info);
1277 netif_disconnect_backend(info);
1278 free_netdev(info->netdev);
1282 static void end_access(int ref, void *page)
1284 if (ref != GRANT_INVALID_REF)
1285 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1289 /* ** Driver registration ** */
1292 static struct xenbus_device_id netfront_ids[] = {
1293 { "vif" },
1294 { "" }
1295 };
1298 static struct xenbus_driver netfront = {
1299 .name = "vif",
1300 .owner = THIS_MODULE,
1301 .ids = netfront_ids,
1302 .probe = netfront_probe,
1303 .remove = netfront_remove,
1304 .resume = netfront_resume,
1305 .otherend_changed = backend_changed,
1306 };
1309 static struct notifier_block notifier_inetdev = {
1310 .notifier_call = inetdev_notify,
1311 .next = NULL,
1312 .priority = 0
1313 };
1315 static int __init netif_init(void)
1317 int err = 0;
1319 if (xen_start_info->flags & SIF_INITDOMAIN)
1320 return 0;
1322 if ((err = xennet_proc_init()) != 0)
1323 return err;
1325 IPRINTK("Initialising virtual ethernet driver.\n");
1327 (void)register_inetaddr_notifier(&notifier_inetdev);
1329 return xenbus_register_frontend(&netfront);
1331 module_init(netif_init);
1334 static void netif_exit(void)
1336 unregister_inetaddr_notifier(&notifier_inetdev);
1338 return xenbus_unregister_driver(&netfront);
1340 module_exit(netif_exit);
1342 MODULE_LICENSE("Dual BSD/GPL");
1345 /* ** /proc **/
1348 #ifdef CONFIG_PROC_FS
1350 #define TARGET_MIN 0UL
1351 #define TARGET_MAX 1UL
1352 #define TARGET_CUR 2UL
1354 static int xennet_proc_read(
1355 char *page, char **start, off_t off, int count, int *eof, void *data)
1357 struct net_device *dev =
1358 (struct net_device *)((unsigned long)data & ~3UL);
1359 struct netfront_info *np = netdev_priv(dev);
1360 int len = 0, which_target = (long)data & 3;
1362 switch (which_target) {
1363 case TARGET_MIN:
1364 len = sprintf(page, "%d\n", np->rx_min_target);
1365 break;
1366 case TARGET_MAX:
1367 len = sprintf(page, "%d\n", np->rx_max_target);
1368 break;
1369 case TARGET_CUR:
1370 len = sprintf(page, "%d\n", np->rx_target);
1371 break;
1374 *eof = 1;
1375 return len;
1378 static int xennet_proc_write(
1379 struct file *file, const char __user *buffer,
1380 unsigned long count, void *data)
1382 struct net_device *dev =
1383 (struct net_device *)((unsigned long)data & ~3UL);
1384 struct netfront_info *np = netdev_priv(dev);
1385 int which_target = (long)data & 3;
1386 char string[64];
1387 long target;
1389 if (!capable(CAP_SYS_ADMIN))
1390 return -EPERM;
1392 if (count <= 1)
1393 return -EBADMSG; /* runt */
1394 if (count > sizeof(string))
1395 return -EFBIG; /* too long */
1397 if (copy_from_user(string, buffer, count))
1398 return -EFAULT;
1399 string[sizeof(string)-1] = '\0';
1401 target = simple_strtol(string, NULL, 10);
1402 if (target < RX_MIN_TARGET)
1403 target = RX_MIN_TARGET;
1404 if (target > RX_MAX_TARGET)
1405 target = RX_MAX_TARGET;
1407 spin_lock(&np->rx_lock);
1409 switch (which_target) {
1410 case TARGET_MIN:
1411 if (target > np->rx_max_target)
1412 np->rx_max_target = target;
1413 np->rx_min_target = target;
1414 if (target > np->rx_target)
1415 np->rx_target = target;
1416 break;
1417 case TARGET_MAX:
1418 if (target < np->rx_min_target)
1419 np->rx_min_target = target;
1420 np->rx_max_target = target;
1421 if (target < np->rx_target)
1422 np->rx_target = target;
1423 break;
1424 case TARGET_CUR:
1425 break;
1428 network_alloc_rx_buffers(dev);
1430 spin_unlock(&np->rx_lock);
1432 return count;
1435 static int xennet_proc_init(void)
1437 if (proc_mkdir("xen/net", NULL) == NULL)
1438 return -ENOMEM;
1439 return 0;
1442 static int xennet_proc_addif(struct net_device *dev)
1444 struct proc_dir_entry *dir, *min, *max, *cur;
1445 char name[30];
1447 sprintf(name, "xen/net/%s", dev->name);
1449 dir = proc_mkdir(name, NULL);
1450 if (!dir)
1451 goto nomem;
1453 min = create_proc_entry("rxbuf_min", 0644, dir);
1454 max = create_proc_entry("rxbuf_max", 0644, dir);
1455 cur = create_proc_entry("rxbuf_cur", 0444, dir);
1456 if (!min || !max || !cur)
1457 goto nomem;
1459 min->read_proc = xennet_proc_read;
1460 min->write_proc = xennet_proc_write;
1461 min->data = (void *)((unsigned long)dev | TARGET_MIN);
1463 max->read_proc = xennet_proc_read;
1464 max->write_proc = xennet_proc_write;
1465 max->data = (void *)((unsigned long)dev | TARGET_MAX);
1467 cur->read_proc = xennet_proc_read;
1468 cur->write_proc = xennet_proc_write;
1469 cur->data = (void *)((unsigned long)dev | TARGET_CUR);
1471 return 0;
1473 nomem:
1474 xennet_proc_delif(dev);
1475 return -ENOMEM;
1478 static void xennet_proc_delif(struct net_device *dev)
1480 char name[30];
1482 sprintf(name, "xen/net/%s/rxbuf_min", dev->name);
1483 remove_proc_entry(name, NULL);
1485 sprintf(name, "xen/net/%s/rxbuf_max", dev->name);
1486 remove_proc_entry(name, NULL);
1488 sprintf(name, "xen/net/%s/rxbuf_cur", dev->name);
1489 remove_proc_entry(name, NULL);
1491 sprintf(name, "xen/net/%s", dev->name);
1492 remove_proc_entry(name, NULL);
1495 #endif
1498 /*
1499 * Local variables:
1500 * c-file-style: "linux"
1501 * indent-tabs-mode: t
1502 * c-indent-level: 8
1503 * c-basic-offset: 8
1504 * tab-width: 8
1505 * End:
1506 */