ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 9291:a5ee1cb525bb

Make MTU rx check in netfront more permissive to allow for 8021q vlan tagging.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Mar 15 12:52:33 2006 +0100 (2006-03-15)
parents e9daf5307296
children ecb99d1ce57d
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 #ifndef __GFP_NOWARN
72 #define __GFP_NOWARN 0
73 #endif
74 #define alloc_xen_skb(_l) __dev_alloc_skb((_l), GFP_ATOMIC|__GFP_NOWARN)
76 #define init_skb_shinfo(_skb) \
77 do { \
78 atomic_set(&(skb_shinfo(_skb)->dataref), 1); \
79 skb_shinfo(_skb)->nr_frags = 0; \
80 skb_shinfo(_skb)->frag_list = NULL; \
81 } while (0)
83 static unsigned long rx_pfn_array[NET_RX_RING_SIZE];
84 static multicall_entry_t rx_mcl[NET_RX_RING_SIZE+1];
85 static mmu_update_t rx_mmu[NET_RX_RING_SIZE];
87 struct netfront_info
88 {
89 struct list_head list;
90 struct net_device *netdev;
92 struct net_device_stats stats;
93 unsigned int tx_full;
95 netif_tx_front_ring_t tx;
96 netif_rx_front_ring_t rx;
98 spinlock_t tx_lock;
99 spinlock_t rx_lock;
101 unsigned int handle;
102 unsigned int evtchn, irq;
104 /* What is the status of our connection to the remote backend? */
105 #define BEST_CLOSED 0
106 #define BEST_DISCONNECTED 1
107 #define BEST_CONNECTED 2
108 unsigned int backend_state;
110 /* Is this interface open or closed (down or up)? */
111 #define UST_CLOSED 0
112 #define UST_OPEN 1
113 unsigned int user_state;
115 /* Receive-ring batched refills. */
116 #define RX_MIN_TARGET 8
117 #define RX_DFL_MIN_TARGET 64
118 #define RX_MAX_TARGET NET_RX_RING_SIZE
119 int rx_min_target, rx_max_target, rx_target;
120 struct sk_buff_head rx_batch;
122 struct timer_list rx_refill_timer;
124 /*
125 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
126 * array is an index into a chain of free entries.
127 */
128 struct sk_buff *tx_skbs[NET_TX_RING_SIZE+1];
129 struct sk_buff *rx_skbs[NET_RX_RING_SIZE+1];
131 grant_ref_t gref_tx_head;
132 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
133 grant_ref_t gref_rx_head;
134 grant_ref_t grant_rx_ref[NET_TX_RING_SIZE + 1];
136 struct xenbus_device *xbdev;
137 int tx_ring_ref;
138 int rx_ring_ref;
139 u8 mac[ETH_ALEN];
140 };
142 /* Access macros for acquiring freeing slots in {tx,rx}_skbs[]. */
143 #define ADD_ID_TO_FREELIST(_list, _id) \
144 (_list)[(_id)] = (_list)[0]; \
145 (_list)[0] = (void *)(unsigned long)(_id);
146 #define GET_ID_FROM_FREELIST(_list) \
147 ({ unsigned long _id = (unsigned long)(_list)[0]; \
148 (_list)[0] = (_list)[_id]; \
149 (unsigned short)_id; })
151 #ifdef DEBUG
152 static char *be_state_name[] = {
153 [BEST_CLOSED] = "closed",
154 [BEST_DISCONNECTED] = "disconnected",
155 [BEST_CONNECTED] = "connected",
156 };
157 #endif
159 #define DPRINTK(fmt, args...) pr_debug("netfront (%s:%d) " fmt, \
160 __FUNCTION__, __LINE__, ##args)
161 #define IPRINTK(fmt, args...) \
162 printk(KERN_INFO "netfront: " fmt, ##args)
163 #define WPRINTK(fmt, args...) \
164 printk(KERN_WARNING "netfront: " fmt, ##args)
167 static int talk_to_backend(struct xenbus_device *, struct netfront_info *);
168 static int setup_device(struct xenbus_device *, struct netfront_info *);
169 static int create_netdev(int, struct xenbus_device *, struct net_device **);
171 static void netfront_closing(struct xenbus_device *);
173 static void end_access(int, void *);
174 static void netif_disconnect_backend(struct netfront_info *);
175 static void close_netdev(struct netfront_info *);
176 static void netif_free(struct netfront_info *);
178 static void show_device(struct netfront_info *);
180 static void network_connect(struct net_device *);
181 static void network_tx_buf_gc(struct net_device *);
182 static void network_alloc_rx_buffers(struct net_device *);
183 static int send_fake_arp(struct net_device *);
185 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs);
187 #ifdef CONFIG_PROC_FS
188 static int xennet_proc_init(void);
189 static int xennet_proc_addif(struct net_device *dev);
190 static void xennet_proc_delif(struct net_device *dev);
191 #else
192 #define xennet_proc_init() (0)
193 #define xennet_proc_addif(d) (0)
194 #define xennet_proc_delif(d) ((void)0)
195 #endif
198 /**
199 * Entry point to this code when a new device is created. Allocate the basic
200 * structures and the ring buffers for communication with the backend, and
201 * inform the backend of the appropriate details for those. Switch to
202 * Connected state.
203 */
204 static int netfront_probe(struct xenbus_device *dev,
205 const struct xenbus_device_id *id)
206 {
207 int err;
208 struct net_device *netdev;
209 struct netfront_info *info;
210 unsigned int handle;
212 err = xenbus_scanf(XBT_NULL, dev->nodename, "handle", "%u", &handle);
213 if (err != 1) {
214 xenbus_dev_fatal(dev, err, "reading handle");
215 return err;
216 }
218 err = create_netdev(handle, dev, &netdev);
219 if (err) {
220 xenbus_dev_fatal(dev, err, "creating netdev");
221 return err;
222 }
224 info = netdev_priv(netdev);
225 dev->data = info;
227 err = talk_to_backend(dev, info);
228 if (err) {
229 kfree(info);
230 dev->data = NULL;
231 return err;
232 }
234 return 0;
235 }
238 /**
239 * We are reconnecting to the backend, due to a suspend/resume, or a backend
240 * driver restart. We tear down our netif structure and recreate it, but
241 * leave the device-layer structures intact so that this is transparent to the
242 * rest of the kernel.
243 */
244 static int netfront_resume(struct xenbus_device *dev)
245 {
246 struct netfront_info *info = dev->data;
248 DPRINTK("%s\n", dev->nodename);
250 netif_disconnect_backend(info);
251 return talk_to_backend(dev, info);
252 }
255 /* Common code used when first setting up, and when resuming. */
256 static int talk_to_backend(struct xenbus_device *dev,
257 struct netfront_info *info)
258 {
259 const char *message;
260 xenbus_transaction_t xbt;
261 int err;
263 err = xen_net_read_mac(dev, info->mac);
264 if (err) {
265 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
266 goto out;
267 }
269 /* Create shared ring, alloc event channel. */
270 err = setup_device(dev, info);
271 if (err)
272 goto out;
274 again:
275 err = xenbus_transaction_start(&xbt);
276 if (err) {
277 xenbus_dev_fatal(dev, err, "starting transaction");
278 goto destroy_ring;
279 }
281 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
282 info->tx_ring_ref);
283 if (err) {
284 message = "writing tx ring-ref";
285 goto abort_transaction;
286 }
287 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
288 info->rx_ring_ref);
289 if (err) {
290 message = "writing rx ring-ref";
291 goto abort_transaction;
292 }
293 err = xenbus_printf(xbt, dev->nodename,
294 "event-channel", "%u", info->evtchn);
295 if (err) {
296 message = "writing event-channel";
297 goto abort_transaction;
298 }
300 err = xenbus_printf(xbt, dev->nodename,
301 "state", "%d", XenbusStateConnected);
302 if (err) {
303 message = "writing frontend XenbusStateConnected";
304 goto abort_transaction;
305 }
307 err = xenbus_transaction_end(xbt, 0);
308 if (err) {
309 if (err == -EAGAIN)
310 goto again;
311 xenbus_dev_fatal(dev, err, "completing transaction");
312 goto destroy_ring;
313 }
315 return 0;
317 abort_transaction:
318 xenbus_transaction_end(xbt, 1);
319 xenbus_dev_fatal(dev, err, "%s", message);
320 destroy_ring:
321 netif_free(info);
322 out:
323 return err;
324 }
327 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
328 {
329 netif_tx_sring_t *txs;
330 netif_rx_sring_t *rxs;
331 int err;
332 struct net_device *netdev = info->netdev;
334 info->tx_ring_ref = GRANT_INVALID_REF;
335 info->rx_ring_ref = GRANT_INVALID_REF;
336 info->rx.sring = NULL;
337 info->tx.sring = NULL;
338 info->irq = 0;
340 txs = (netif_tx_sring_t *)__get_free_page(GFP_KERNEL);
341 if (!txs) {
342 err = -ENOMEM;
343 xenbus_dev_fatal(dev, err, "allocating tx ring page");
344 goto fail;
345 }
346 rxs = (netif_rx_sring_t *)__get_free_page(GFP_KERNEL);
347 if (!rxs) {
348 err = -ENOMEM;
349 xenbus_dev_fatal(dev, err, "allocating rx ring page");
350 goto fail;
351 }
352 memset(txs, 0, PAGE_SIZE);
353 memset(rxs, 0, PAGE_SIZE);
354 info->backend_state = BEST_DISCONNECTED;
356 SHARED_RING_INIT(txs);
357 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
359 SHARED_RING_INIT(rxs);
360 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
362 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
363 if (err < 0)
364 goto fail;
365 info->tx_ring_ref = err;
367 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
368 if (err < 0)
369 goto fail;
370 info->rx_ring_ref = err;
372 err = xenbus_alloc_evtchn(dev, &info->evtchn);
373 if (err)
374 goto fail;
376 memcpy(netdev->dev_addr, info->mac, ETH_ALEN);
377 network_connect(netdev);
378 info->irq = bind_evtchn_to_irqhandler(
379 info->evtchn, netif_int, SA_SAMPLE_RANDOM, netdev->name,
380 netdev);
381 (void)send_fake_arp(netdev);
382 show_device(info);
384 return 0;
386 fail:
387 netif_free(info);
388 return err;
389 }
392 /**
393 * Callback received when the backend's state changes.
394 */
395 static void backend_changed(struct xenbus_device *dev,
396 XenbusState backend_state)
397 {
398 DPRINTK("\n");
400 switch (backend_state) {
401 case XenbusStateInitialising:
402 case XenbusStateInitWait:
403 case XenbusStateInitialised:
404 case XenbusStateConnected:
405 case XenbusStateUnknown:
406 case XenbusStateClosed:
407 break;
409 case XenbusStateClosing:
410 netfront_closing(dev);
411 break;
412 }
413 }
416 /** Send a packet on a net device to encourage switches to learn the
417 * MAC. We send a fake ARP request.
418 *
419 * @param dev device
420 * @return 0 on success, error code otherwise
421 */
422 static int send_fake_arp(struct net_device *dev)
423 {
424 struct sk_buff *skb;
425 u32 src_ip, dst_ip;
427 dst_ip = INADDR_BROADCAST;
428 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
430 /* No IP? Then nothing to do. */
431 if (src_ip == 0)
432 return 0;
434 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
435 dst_ip, dev, src_ip,
436 /*dst_hw*/ NULL, /*src_hw*/ NULL,
437 /*target_hw*/ dev->dev_addr);
438 if (skb == NULL)
439 return -ENOMEM;
441 return dev_queue_xmit(skb);
442 }
445 static int network_open(struct net_device *dev)
446 {
447 struct netfront_info *np = netdev_priv(dev);
449 memset(&np->stats, 0, sizeof(np->stats));
451 np->user_state = UST_OPEN;
453 network_alloc_rx_buffers(dev);
454 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
456 netif_start_queue(dev);
458 return 0;
459 }
461 static void network_tx_buf_gc(struct net_device *dev)
462 {
463 RING_IDX i, prod;
464 unsigned short id;
465 struct netfront_info *np = netdev_priv(dev);
466 struct sk_buff *skb;
468 if (np->backend_state != BEST_CONNECTED)
469 return;
471 do {
472 prod = np->tx.sring->rsp_prod;
473 rmb(); /* Ensure we see responses up to 'rp'. */
475 for (i = np->tx.rsp_cons; i != prod; i++) {
476 id = RING_GET_RESPONSE(&np->tx, i)->id;
477 skb = np->tx_skbs[id];
478 if (unlikely(gnttab_query_foreign_access(
479 np->grant_tx_ref[id]) != 0)) {
480 printk(KERN_ALERT "network_tx_buf_gc: warning "
481 "-- grant still in use by backend "
482 "domain.\n");
483 goto out;
484 }
485 gnttab_end_foreign_access_ref(
486 np->grant_tx_ref[id], GNTMAP_readonly);
487 gnttab_release_grant_reference(
488 &np->gref_tx_head, np->grant_tx_ref[id]);
489 np->grant_tx_ref[id] = GRANT_INVALID_REF;
490 ADD_ID_TO_FREELIST(np->tx_skbs, id);
491 dev_kfree_skb_irq(skb);
492 }
494 np->tx.rsp_cons = prod;
496 /*
497 * Set a new event, then check for race with update of tx_cons.
498 * Note that it is essential to schedule a callback, no matter
499 * how few buffers are pending. Even if there is space in the
500 * transmit ring, higher layers may be blocked because too much
501 * data is outstanding: in such cases notification from Xen is
502 * likely to be the only kick that we'll get.
503 */
504 np->tx.sring->rsp_event =
505 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
506 mb();
507 } while (prod != np->tx.sring->rsp_prod);
509 out:
510 if (np->tx_full &&
511 ((np->tx.sring->req_prod - prod) < NET_TX_RING_SIZE)) {
512 np->tx_full = 0;
513 if (np->user_state == UST_OPEN)
514 netif_wake_queue(dev);
515 }
516 }
519 static void rx_refill_timeout(unsigned long data)
520 {
521 struct net_device *dev = (struct net_device *)data;
522 netif_rx_schedule(dev);
523 }
526 static void network_alloc_rx_buffers(struct net_device *dev)
527 {
528 unsigned short id;
529 struct netfront_info *np = netdev_priv(dev);
530 struct sk_buff *skb;
531 int i, batch_target;
532 RING_IDX req_prod = np->rx.req_prod_pvt;
533 struct xen_memory_reservation reservation;
534 grant_ref_t ref;
536 if (unlikely(np->backend_state != BEST_CONNECTED))
537 return;
539 /*
540 * Allocate skbuffs greedily, even though we batch updates to the
541 * receive ring. This creates a less bursty demand on the memory
542 * allocator, so should reduce the chance of failed allocation requests
543 * both for ourself and for other kernel subsystems.
544 */
545 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
546 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
547 /*
548 * Subtract dev_alloc_skb headroom (16 bytes) and shared info
549 * tailroom then round down to SKB_DATA_ALIGN boundary.
550 */
551 skb = alloc_xen_skb(
552 ((PAGE_SIZE - sizeof(struct skb_shared_info)) &
553 (-SKB_DATA_ALIGN(1))) - 16);
554 if (skb == NULL) {
555 /* Any skbuffs queued for refill? Force them out. */
556 if (i != 0)
557 goto refill;
558 /* Could not allocate any skbuffs. Try again later. */
559 mod_timer(&np->rx_refill_timer,
560 jiffies + (HZ/10));
561 return;
562 }
563 __skb_queue_tail(&np->rx_batch, skb);
564 }
566 /* Is the batch large enough to be worthwhile? */
567 if (i < (np->rx_target/2))
568 return;
570 /* Adjust our fill target if we risked running out of buffers. */
571 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
572 ((np->rx_target *= 2) > np->rx_max_target))
573 np->rx_target = np->rx_max_target;
575 refill:
576 for (i = 0; ; i++) {
577 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
578 break;
580 skb->dev = dev;
582 id = GET_ID_FROM_FREELIST(np->rx_skbs);
584 np->rx_skbs[id] = skb;
586 RING_GET_REQUEST(&np->rx, req_prod + i)->id = id;
587 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
588 BUG_ON((signed short)ref < 0);
589 np->grant_rx_ref[id] = ref;
590 gnttab_grant_foreign_transfer_ref(ref,
591 np->xbdev->otherend_id,
592 __pa(skb->head) >> PAGE_SHIFT);
593 RING_GET_REQUEST(&np->rx, req_prod + i)->gref = ref;
594 rx_pfn_array[i] = virt_to_mfn(skb->head);
596 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
597 /* Remove this page before passing back to Xen. */
598 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
599 INVALID_P2M_ENTRY);
600 MULTI_update_va_mapping(rx_mcl+i,
601 (unsigned long)skb->head,
602 __pte(0), 0);
603 }
604 }
606 /* Tell the ballon driver what is going on. */
607 balloon_update_driver_allowance(i);
609 reservation.extent_start = rx_pfn_array;
610 reservation.nr_extents = i;
611 reservation.extent_order = 0;
612 reservation.address_bits = 0;
613 reservation.domid = DOMID_SELF;
615 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
616 /* After all PTEs have been zapped, flush the TLB. */
617 rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
618 UVMF_TLB_FLUSH|UVMF_ALL;
620 /* Give away a batch of pages. */
621 rx_mcl[i].op = __HYPERVISOR_memory_op;
622 rx_mcl[i].args[0] = XENMEM_decrease_reservation;
623 rx_mcl[i].args[1] = (unsigned long)&reservation;
625 /* Zap PTEs and give away pages in one big multicall. */
626 (void)HYPERVISOR_multicall(rx_mcl, i+1);
628 /* Check return status of HYPERVISOR_memory_op(). */
629 if (unlikely(rx_mcl[i].result != i))
630 panic("Unable to reduce memory reservation\n");
631 } else
632 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
633 &reservation) != i)
634 panic("Unable to reduce memory reservation\n");
636 /* Above is a suitable barrier to ensure backend will see requests. */
637 np->rx.req_prod_pvt = req_prod + i;
638 RING_PUSH_REQUESTS(&np->rx);
639 }
642 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
643 {
644 unsigned short id;
645 struct netfront_info *np = netdev_priv(dev);
646 netif_tx_request_t *tx;
647 RING_IDX i;
648 grant_ref_t ref;
649 unsigned long mfn;
650 int notify;
652 if (unlikely(np->tx_full)) {
653 printk(KERN_ALERT "%s: full queue wasn't stopped!\n",
654 dev->name);
655 netif_stop_queue(dev);
656 goto drop;
657 }
659 if (unlikely((((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >=
660 PAGE_SIZE)) {
661 struct sk_buff *nskb;
662 if (unlikely((nskb = alloc_xen_skb(skb->len)) == NULL))
663 goto drop;
664 skb_put(nskb, skb->len);
665 memcpy(nskb->data, skb->data, skb->len);
666 nskb->dev = skb->dev;
667 dev_kfree_skb(skb);
668 skb = nskb;
669 }
671 spin_lock_irq(&np->tx_lock);
673 if (np->backend_state != BEST_CONNECTED) {
674 spin_unlock_irq(&np->tx_lock);
675 goto drop;
676 }
678 i = np->tx.req_prod_pvt;
680 id = GET_ID_FROM_FREELIST(np->tx_skbs);
681 np->tx_skbs[id] = skb;
683 tx = RING_GET_REQUEST(&np->tx, i);
685 tx->id = id;
686 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
687 BUG_ON((signed short)ref < 0);
688 mfn = virt_to_mfn(skb->data);
689 gnttab_grant_foreign_access_ref(
690 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
691 tx->gref = np->grant_tx_ref[id] = ref;
692 tx->offset = (unsigned long)skb->data & ~PAGE_MASK;
693 tx->size = skb->len;
694 tx->flags = (skb->ip_summed == CHECKSUM_HW) ? NETTXF_csum_blank : 0;
696 np->tx.req_prod_pvt = i + 1;
697 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
698 if (notify)
699 notify_remote_via_irq(np->irq);
701 network_tx_buf_gc(dev);
703 if (RING_FULL(&np->tx)) {
704 np->tx_full = 1;
705 netif_stop_queue(dev);
706 }
708 spin_unlock_irq(&np->tx_lock);
710 np->stats.tx_bytes += skb->len;
711 np->stats.tx_packets++;
713 return 0;
715 drop:
716 np->stats.tx_dropped++;
717 dev_kfree_skb(skb);
718 return 0;
719 }
721 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
722 {
723 struct net_device *dev = dev_id;
724 struct netfront_info *np = netdev_priv(dev);
725 unsigned long flags;
727 spin_lock_irqsave(&np->tx_lock, flags);
728 network_tx_buf_gc(dev);
729 spin_unlock_irqrestore(&np->tx_lock, flags);
731 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx) &&
732 (np->user_state == UST_OPEN))
733 netif_rx_schedule(dev);
735 return IRQ_HANDLED;
736 }
739 static int netif_poll(struct net_device *dev, int *pbudget)
740 {
741 struct netfront_info *np = netdev_priv(dev);
742 struct sk_buff *skb, *nskb;
743 netif_rx_response_t *rx;
744 RING_IDX i, rp;
745 mmu_update_t *mmu = rx_mmu;
746 multicall_entry_t *mcl = rx_mcl;
747 int work_done, budget, more_to_do = 1;
748 struct sk_buff_head rxq;
749 unsigned long flags;
750 unsigned long mfn;
751 grant_ref_t ref;
753 spin_lock(&np->rx_lock);
755 if (np->backend_state != BEST_CONNECTED) {
756 spin_unlock(&np->rx_lock);
757 return 0;
758 }
760 skb_queue_head_init(&rxq);
762 if ((budget = *pbudget) > dev->quota)
763 budget = dev->quota;
764 rp = np->rx.sring->rsp_prod;
765 rmb(); /* Ensure we see queued responses up to 'rp'. */
767 for (i = np->rx.rsp_cons, work_done = 0;
768 (i != rp) && (work_done < budget);
769 i++, work_done++) {
770 rx = RING_GET_RESPONSE(&np->rx, i);
772 /*
773 * This definitely indicates a bug, either in this driver or
774 * in the backend driver. In future this should flag the bad
775 * situation to the system controller to reboot the backed.
776 */
777 if ((ref = np->grant_rx_ref[rx->id]) == GRANT_INVALID_REF) {
778 WPRINTK("Bad rx response id %d.\n", rx->id);
779 work_done--;
780 continue;
781 }
783 /* Memory pressure, insufficient buffer headroom, ... */
784 if ((mfn = gnttab_end_foreign_transfer_ref(ref)) == 0) {
785 if (net_ratelimit())
786 WPRINTK("Unfulfilled rx req (id=%d, st=%d).\n",
787 rx->id, rx->status);
788 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id =
789 rx->id;
790 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref =
791 ref;
792 np->rx.req_prod_pvt++;
793 RING_PUSH_REQUESTS(&np->rx);
794 work_done--;
795 continue;
796 }
798 gnttab_release_grant_reference(&np->gref_rx_head, ref);
799 np->grant_rx_ref[rx->id] = GRANT_INVALID_REF;
801 skb = np->rx_skbs[rx->id];
802 ADD_ID_TO_FREELIST(np->rx_skbs, rx->id);
804 /* NB. We handle skb overflow later. */
805 skb->data = skb->head + rx->offset;
806 skb->len = rx->status;
807 skb->tail = skb->data + skb->len;
809 if (rx->flags & NETRXF_csum_valid)
810 skb->ip_summed = CHECKSUM_UNNECESSARY;
812 np->stats.rx_packets++;
813 np->stats.rx_bytes += rx->status;
815 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
816 /* Remap the page. */
817 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
818 pfn_pte_ma(mfn, PAGE_KERNEL),
819 0);
820 mcl++;
821 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
822 | MMU_MACHPHYS_UPDATE;
823 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
824 mmu++;
826 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
827 mfn);
828 }
830 __skb_queue_tail(&rxq, skb);
831 }
833 /* Some pages are no longer absent... */
834 balloon_update_driver_allowance(-work_done);
836 /* Do all the remapping work, and M2P updates, in one big hypercall. */
837 if (likely((mcl - rx_mcl) != 0)) {
838 mcl->op = __HYPERVISOR_mmu_update;
839 mcl->args[0] = (unsigned long)rx_mmu;
840 mcl->args[1] = mmu - rx_mmu;
841 mcl->args[2] = 0;
842 mcl->args[3] = DOMID_SELF;
843 mcl++;
844 (void)HYPERVISOR_multicall(rx_mcl, mcl - rx_mcl);
845 }
847 while ((skb = __skb_dequeue(&rxq)) != NULL) {
848 if (skb->len > (dev->mtu + ETH_HLEN + 4)) {
849 if (net_ratelimit())
850 printk(KERN_INFO "Received packet too big for "
851 "MTU (%d > %d)\n",
852 skb->len - ETH_HLEN - 4, dev->mtu);
853 skb->len = 0;
854 skb->tail = skb->data;
855 init_skb_shinfo(skb);
856 dev_kfree_skb(skb);
857 continue;
858 }
860 /*
861 * Enough room in skbuff for the data we were passed? Also,
862 * Linux expects at least 16 bytes headroom in each rx buffer.
863 */
864 if (unlikely(skb->tail > skb->end) ||
865 unlikely((skb->data - skb->head) < 16)) {
866 if (net_ratelimit()) {
867 if (skb->tail > skb->end)
868 printk(KERN_INFO "Received packet "
869 "is %zd bytes beyond tail.\n",
870 skb->tail - skb->end);
871 else
872 printk(KERN_INFO "Received packet "
873 "is %zd bytes before head.\n",
874 16 - (skb->data - skb->head));
875 }
877 nskb = alloc_xen_skb(skb->len + 2);
878 if (nskb != NULL) {
879 skb_reserve(nskb, 2);
880 skb_put(nskb, skb->len);
881 memcpy(nskb->data, skb->data, skb->len);
882 nskb->dev = skb->dev;
883 nskb->ip_summed = skb->ip_summed;
884 }
886 /* Reinitialise and then destroy the old skbuff. */
887 skb->len = 0;
888 skb->tail = skb->data;
889 init_skb_shinfo(skb);
890 dev_kfree_skb(skb);
892 /* Switch old for new, if we copied the buffer. */
893 if ((skb = nskb) == NULL)
894 continue;
895 }
897 /* Set the shinfo area, which is hidden behind the data. */
898 init_skb_shinfo(skb);
899 /* Ethernet work: Delayed to here as it peeks the header. */
900 skb->protocol = eth_type_trans(skb, dev);
902 /* Pass it up. */
903 netif_receive_skb(skb);
904 dev->last_rx = jiffies;
905 }
907 np->rx.rsp_cons = i;
909 /* If we get a callback with very few responses, reduce fill target. */
910 /* NB. Note exponential increase, linear decrease. */
911 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
912 ((3*np->rx_target) / 4)) &&
913 (--np->rx_target < np->rx_min_target))
914 np->rx_target = np->rx_min_target;
916 network_alloc_rx_buffers(dev);
918 *pbudget -= work_done;
919 dev->quota -= work_done;
921 if (work_done < budget) {
922 local_irq_save(flags);
924 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
925 if (!more_to_do)
926 __netif_rx_complete(dev);
928 local_irq_restore(flags);
929 }
931 spin_unlock(&np->rx_lock);
933 return more_to_do;
934 }
937 static int network_close(struct net_device *dev)
938 {
939 struct netfront_info *np = netdev_priv(dev);
940 np->user_state = UST_CLOSED;
941 netif_stop_queue(np->netdev);
942 return 0;
943 }
946 static struct net_device_stats *network_get_stats(struct net_device *dev)
947 {
948 struct netfront_info *np = netdev_priv(dev);
949 return &np->stats;
950 }
952 static void network_connect(struct net_device *dev)
953 {
954 struct netfront_info *np;
955 int i, requeue_idx;
956 netif_tx_request_t *tx;
957 struct sk_buff *skb;
959 np = netdev_priv(dev);
960 spin_lock_irq(&np->tx_lock);
961 spin_lock(&np->rx_lock);
963 /* Recovery procedure: */
965 /* Step 1: Reinitialise variables. */
966 np->tx_full = 0;
968 /*
969 * Step 2: Rebuild the RX and TX ring contents.
970 * NB. We could just free the queued TX packets now but we hope
971 * that sending them out might do some good. We have to rebuild
972 * the RX ring because some of our pages are currently flipped out
973 * so we can't just free the RX skbs.
974 * NB2. Freelist index entries are always going to be less than
975 * __PAGE_OFFSET, whereas pointers to skbs will always be equal or
976 * greater than __PAGE_OFFSET: we use this property to distinguish
977 * them.
978 */
980 /*
981 * Rebuild the TX buffer freelist and the TX ring itself.
982 * NB. This reorders packets. We could keep more private state
983 * to avoid this but maybe it doesn't matter so much given the
984 * interface has been down.
985 */
986 for (requeue_idx = 0, i = 1; i <= NET_TX_RING_SIZE; i++) {
987 if ((unsigned long)np->tx_skbs[i] < __PAGE_OFFSET)
988 continue;
990 skb = np->tx_skbs[i];
992 tx = RING_GET_REQUEST(&np->tx, requeue_idx);
993 requeue_idx++;
995 tx->id = i;
996 gnttab_grant_foreign_access_ref(
997 np->grant_tx_ref[i], np->xbdev->otherend_id,
998 virt_to_mfn(np->tx_skbs[i]->data),
999 GNTMAP_readonly);
1000 tx->gref = np->grant_tx_ref[i];
1001 tx->offset = (unsigned long)skb->data & ~PAGE_MASK;
1002 tx->size = skb->len;
1003 tx->flags = (skb->ip_summed == CHECKSUM_HW) ?
1004 NETTXF_csum_blank : 0;
1006 np->stats.tx_bytes += skb->len;
1007 np->stats.tx_packets++;
1010 np->tx.req_prod_pvt = requeue_idx;
1011 RING_PUSH_REQUESTS(&np->tx);
1013 /* Rebuild the RX buffer freelist and the RX ring itself. */
1014 for (requeue_idx = 0, i = 1; i <= NET_RX_RING_SIZE; i++) {
1015 if ((unsigned long)np->rx_skbs[i] < __PAGE_OFFSET)
1016 continue;
1017 gnttab_grant_foreign_transfer_ref(
1018 np->grant_rx_ref[i], np->xbdev->otherend_id,
1019 __pa(np->rx_skbs[i]->data) >> PAGE_SHIFT);
1020 RING_GET_REQUEST(&np->rx, requeue_idx)->gref =
1021 np->grant_rx_ref[i];
1022 RING_GET_REQUEST(&np->rx, requeue_idx)->id = i;
1023 requeue_idx++;
1026 np->rx.req_prod_pvt = requeue_idx;
1027 RING_PUSH_REQUESTS(&np->rx);
1029 /*
1030 * Step 3: All public and private state should now be sane. Get
1031 * ready to start sending and receiving packets and give the driver
1032 * domain a kick because we've probably just requeued some
1033 * packets.
1034 */
1035 np->backend_state = BEST_CONNECTED;
1036 notify_remote_via_irq(np->irq);
1037 network_tx_buf_gc(dev);
1039 if (np->user_state == UST_OPEN)
1040 netif_start_queue(dev);
1042 spin_unlock(&np->rx_lock);
1043 spin_unlock_irq(&np->tx_lock);
1046 static void show_device(struct netfront_info *np)
1048 #ifdef DEBUG
1049 if (np) {
1050 IPRINTK("<vif handle=%u %s(%s) evtchn=%u tx=%p rx=%p>\n",
1051 np->handle,
1052 be_state_name[np->backend_state],
1053 np->user_state ? "open" : "closed",
1054 np->evtchn,
1055 np->tx,
1056 np->rx);
1057 } else
1058 IPRINTK("<vif NULL>\n");
1059 #endif
1062 static void netif_uninit(struct net_device *dev)
1064 struct netfront_info *np = netdev_priv(dev);
1065 gnttab_free_grant_references(np->gref_tx_head);
1066 gnttab_free_grant_references(np->gref_rx_head);
1069 static struct ethtool_ops network_ethtool_ops =
1071 .get_tx_csum = ethtool_op_get_tx_csum,
1072 .set_tx_csum = ethtool_op_set_tx_csum,
1073 };
1075 /** Create a network device.
1076 * @param handle device handle
1077 * @param val return parameter for created device
1078 * @return 0 on success, error code otherwise
1079 */
1080 static int create_netdev(int handle, struct xenbus_device *dev,
1081 struct net_device **val)
1083 int i, err = 0;
1084 struct net_device *netdev = NULL;
1085 struct netfront_info *np = NULL;
1087 if ((netdev = alloc_etherdev(sizeof(struct netfront_info))) == NULL) {
1088 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1089 __FUNCTION__);
1090 err = -ENOMEM;
1091 goto exit;
1094 np = netdev_priv(netdev);
1095 np->backend_state = BEST_CLOSED;
1096 np->user_state = UST_CLOSED;
1097 np->handle = handle;
1098 np->xbdev = dev;
1100 spin_lock_init(&np->tx_lock);
1101 spin_lock_init(&np->rx_lock);
1103 skb_queue_head_init(&np->rx_batch);
1104 np->rx_target = RX_DFL_MIN_TARGET;
1105 np->rx_min_target = RX_DFL_MIN_TARGET;
1106 np->rx_max_target = RX_MAX_TARGET;
1108 init_timer(&np->rx_refill_timer);
1109 np->rx_refill_timer.data = (unsigned long)netdev;
1110 np->rx_refill_timer.function = rx_refill_timeout;
1112 /* Initialise {tx,rx}_skbs as a free chain containing every entry. */
1113 for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1114 np->tx_skbs[i] = (void *)((unsigned long) i+1);
1115 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1118 for (i = 0; i <= NET_RX_RING_SIZE; i++) {
1119 np->rx_skbs[i] = (void *)((unsigned long) i+1);
1120 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1123 /* A grant for every tx ring slot */
1124 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
1125 &np->gref_tx_head) < 0) {
1126 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1127 err = -ENOMEM;
1128 goto exit;
1130 /* A grant for every rx ring slot */
1131 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
1132 &np->gref_rx_head) < 0) {
1133 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1134 gnttab_free_grant_references(np->gref_tx_head);
1135 err = -ENOMEM;
1136 goto exit;
1139 netdev->open = network_open;
1140 netdev->hard_start_xmit = network_start_xmit;
1141 netdev->stop = network_close;
1142 netdev->get_stats = network_get_stats;
1143 netdev->poll = netif_poll;
1144 netdev->uninit = netif_uninit;
1145 netdev->weight = 64;
1146 netdev->features = NETIF_F_IP_CSUM;
1148 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
1149 SET_MODULE_OWNER(netdev);
1150 SET_NETDEV_DEV(netdev, &dev->dev);
1152 if ((err = register_netdev(netdev)) != 0) {
1153 printk(KERN_WARNING "%s> register_netdev err=%d\n",
1154 __FUNCTION__, err);
1155 goto exit_free_grefs;
1158 if ((err = xennet_proc_addif(netdev)) != 0) {
1159 unregister_netdev(netdev);
1160 goto exit_free_grefs;
1163 np->netdev = netdev;
1165 exit:
1166 if (err != 0)
1167 kfree(netdev);
1168 else if (val != NULL)
1169 *val = netdev;
1170 return err;
1172 exit_free_grefs:
1173 gnttab_free_grant_references(np->gref_tx_head);
1174 gnttab_free_grant_references(np->gref_rx_head);
1175 goto exit;
1178 /*
1179 * We use this notifier to send out a fake ARP reply to reset switches and
1180 * router ARP caches when an IP interface is brought up on a VIF.
1181 */
1182 static int
1183 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
1185 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
1186 struct net_device *dev = ifa->ifa_dev->dev;
1188 /* UP event and is it one of our devices? */
1189 if (event == NETDEV_UP && dev->open == network_open)
1190 (void)send_fake_arp(dev);
1192 return NOTIFY_DONE;
1196 /* ** Close down ** */
1199 /**
1200 * Handle the change of state of the backend to Closing. We must delete our
1201 * device-layer structures now, to ensure that writes are flushed through to
1202 * the backend. Once is this done, we can switch to Closed in
1203 * acknowledgement.
1204 */
1205 static void netfront_closing(struct xenbus_device *dev)
1207 struct netfront_info *info = dev->data;
1209 DPRINTK("netfront_closing: %s removed\n", dev->nodename);
1211 close_netdev(info);
1213 xenbus_switch_state(dev, XBT_NULL, XenbusStateClosed);
1217 static int netfront_remove(struct xenbus_device *dev)
1219 struct netfront_info *info = dev->data;
1221 DPRINTK("%s\n", dev->nodename);
1223 netif_disconnect_backend(info);
1224 free_netdev(info->netdev);
1226 return 0;
1230 static void close_netdev(struct netfront_info *info)
1232 spin_lock_irq(&info->netdev->xmit_lock);
1233 netif_stop_queue(info->netdev);
1234 spin_unlock_irq(&info->netdev->xmit_lock);
1236 #ifdef CONFIG_PROC_FS
1237 xennet_proc_delif(info->netdev);
1238 #endif
1240 del_timer_sync(&info->rx_refill_timer);
1242 unregister_netdev(info->netdev);
1246 static void netif_disconnect_backend(struct netfront_info *info)
1248 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1249 spin_lock_irq(&info->tx_lock);
1250 spin_lock(&info->rx_lock);
1251 info->backend_state = BEST_DISCONNECTED;
1252 spin_unlock(&info->rx_lock);
1253 spin_unlock_irq(&info->tx_lock);
1255 if (info->irq)
1256 unbind_from_irqhandler(info->irq, info->netdev);
1257 info->evtchn = info->irq = 0;
1259 end_access(info->tx_ring_ref, info->tx.sring);
1260 end_access(info->rx_ring_ref, info->rx.sring);
1261 info->tx_ring_ref = GRANT_INVALID_REF;
1262 info->rx_ring_ref = GRANT_INVALID_REF;
1263 info->tx.sring = NULL;
1264 info->rx.sring = NULL;
1268 static void netif_free(struct netfront_info *info)
1270 close_netdev(info);
1271 netif_disconnect_backend(info);
1272 free_netdev(info->netdev);
1276 static void end_access(int ref, void *page)
1278 if (ref != GRANT_INVALID_REF)
1279 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1283 /* ** Driver registration ** */
1286 static struct xenbus_device_id netfront_ids[] = {
1287 { "vif" },
1288 { "" }
1289 };
1292 static struct xenbus_driver netfront = {
1293 .name = "vif",
1294 .owner = THIS_MODULE,
1295 .ids = netfront_ids,
1296 .probe = netfront_probe,
1297 .remove = netfront_remove,
1298 .resume = netfront_resume,
1299 .otherend_changed = backend_changed,
1300 };
1303 static struct notifier_block notifier_inetdev = {
1304 .notifier_call = inetdev_notify,
1305 .next = NULL,
1306 .priority = 0
1307 };
1309 static int __init netif_init(void)
1311 int err = 0;
1313 if (xen_start_info->flags & SIF_INITDOMAIN)
1314 return 0;
1316 if ((err = xennet_proc_init()) != 0)
1317 return err;
1319 IPRINTK("Initialising virtual ethernet driver.\n");
1321 (void)register_inetaddr_notifier(&notifier_inetdev);
1323 return xenbus_register_frontend(&netfront);
1325 module_init(netif_init);
1328 static void netif_exit(void)
1330 unregister_inetaddr_notifier(&notifier_inetdev);
1332 return xenbus_unregister_driver(&netfront);
1334 module_exit(netif_exit);
1336 MODULE_LICENSE("Dual BSD/GPL");
1339 /* ** /proc **/
1342 #ifdef CONFIG_PROC_FS
1344 #define TARGET_MIN 0UL
1345 #define TARGET_MAX 1UL
1346 #define TARGET_CUR 2UL
1348 static int xennet_proc_read(
1349 char *page, char **start, off_t off, int count, int *eof, void *data)
1351 struct net_device *dev =
1352 (struct net_device *)((unsigned long)data & ~3UL);
1353 struct netfront_info *np = netdev_priv(dev);
1354 int len = 0, which_target = (long)data & 3;
1356 switch (which_target) {
1357 case TARGET_MIN:
1358 len = sprintf(page, "%d\n", np->rx_min_target);
1359 break;
1360 case TARGET_MAX:
1361 len = sprintf(page, "%d\n", np->rx_max_target);
1362 break;
1363 case TARGET_CUR:
1364 len = sprintf(page, "%d\n", np->rx_target);
1365 break;
1368 *eof = 1;
1369 return len;
1372 static int xennet_proc_write(
1373 struct file *file, const char __user *buffer,
1374 unsigned long count, void *data)
1376 struct net_device *dev =
1377 (struct net_device *)((unsigned long)data & ~3UL);
1378 struct netfront_info *np = netdev_priv(dev);
1379 int which_target = (long)data & 3;
1380 char string[64];
1381 long target;
1383 if (!capable(CAP_SYS_ADMIN))
1384 return -EPERM;
1386 if (count <= 1)
1387 return -EBADMSG; /* runt */
1388 if (count > sizeof(string))
1389 return -EFBIG; /* too long */
1391 if (copy_from_user(string, buffer, count))
1392 return -EFAULT;
1393 string[sizeof(string)-1] = '\0';
1395 target = simple_strtol(string, NULL, 10);
1396 if (target < RX_MIN_TARGET)
1397 target = RX_MIN_TARGET;
1398 if (target > RX_MAX_TARGET)
1399 target = RX_MAX_TARGET;
1401 spin_lock(&np->rx_lock);
1403 switch (which_target) {
1404 case TARGET_MIN:
1405 if (target > np->rx_max_target)
1406 np->rx_max_target = target;
1407 np->rx_min_target = target;
1408 if (target > np->rx_target)
1409 np->rx_target = target;
1410 break;
1411 case TARGET_MAX:
1412 if (target < np->rx_min_target)
1413 np->rx_min_target = target;
1414 np->rx_max_target = target;
1415 if (target < np->rx_target)
1416 np->rx_target = target;
1417 break;
1418 case TARGET_CUR:
1419 break;
1422 network_alloc_rx_buffers(dev);
1424 spin_unlock(&np->rx_lock);
1426 return count;
1429 static int xennet_proc_init(void)
1431 if (proc_mkdir("xen/net", NULL) == NULL)
1432 return -ENOMEM;
1433 return 0;
1436 static int xennet_proc_addif(struct net_device *dev)
1438 struct proc_dir_entry *dir, *min, *max, *cur;
1439 char name[30];
1441 sprintf(name, "xen/net/%s", dev->name);
1443 dir = proc_mkdir(name, NULL);
1444 if (!dir)
1445 goto nomem;
1447 min = create_proc_entry("rxbuf_min", 0644, dir);
1448 max = create_proc_entry("rxbuf_max", 0644, dir);
1449 cur = create_proc_entry("rxbuf_cur", 0444, dir);
1450 if (!min || !max || !cur)
1451 goto nomem;
1453 min->read_proc = xennet_proc_read;
1454 min->write_proc = xennet_proc_write;
1455 min->data = (void *)((unsigned long)dev | TARGET_MIN);
1457 max->read_proc = xennet_proc_read;
1458 max->write_proc = xennet_proc_write;
1459 max->data = (void *)((unsigned long)dev | TARGET_MAX);
1461 cur->read_proc = xennet_proc_read;
1462 cur->write_proc = xennet_proc_write;
1463 cur->data = (void *)((unsigned long)dev | TARGET_CUR);
1465 return 0;
1467 nomem:
1468 xennet_proc_delif(dev);
1469 return -ENOMEM;
1472 static void xennet_proc_delif(struct net_device *dev)
1474 char name[30];
1476 sprintf(name, "xen/net/%s/rxbuf_min", dev->name);
1477 remove_proc_entry(name, NULL);
1479 sprintf(name, "xen/net/%s/rxbuf_max", dev->name);
1480 remove_proc_entry(name, NULL);
1482 sprintf(name, "xen/net/%s/rxbuf_cur", dev->name);
1483 remove_proc_entry(name, NULL);
1485 sprintf(name, "xen/net/%s", dev->name);
1486 remove_proc_entry(name, NULL);
1489 #endif
1492 /*
1493 * Local variables:
1494 * c-file-style: "linux"
1495 * indent-tabs-mode: t
1496 * c-indent-level: 8
1497 * c-basic-offset: 8
1498 * tab-width: 8
1499 * End:
1500 */