ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 6742:ac6605bceb9d

remove pointless NULL check before calling kfree

Signed-off-by: Vincent Hanquez <vincent@xensource.com>
author vh249@arcadians.cl.cam.ac.uk
date Sat Sep 10 14:41:16 2005 +0000 (2005-09-10)
parents cdfa7dd00c44
children 9ead08216805
line source
1 /******************************************************************************
2 * Virtual network driver for conversing with remote driver backends.
3 *
4 * Copyright (c) 2002-2004, K A Fraser
5 *
6 * This file may be distributed separately from the Linux kernel, or
7 * incorporated into other software packages, subject to the following license:
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this source file (the "Software"), to deal in the Software without
11 * restriction, including without limitation the rights to use, copy, modify,
12 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
13 * and to permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/version.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/errno.h>
36 #include <linux/netdevice.h>
37 #include <linux/inetdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/skbuff.h>
40 #include <linux/init.h>
41 #include <linux/bitops.h>
42 #include <linux/proc_fs.h>
43 #include <linux/ethtool.h>
44 #include <net/sock.h>
45 #include <net/pkt_sched.h>
46 #include <net/arp.h>
47 #include <net/route.h>
48 #include <asm/io.h>
49 #include <asm/uaccess.h>
50 #include <asm-xen/evtchn.h>
51 #include <asm-xen/xenbus.h>
52 #include <asm-xen/xen-public/io/netif.h>
53 #include <asm-xen/xen-public/memory.h>
54 #include <asm-xen/balloon.h>
55 #include <asm/page.h>
56 #include <asm/uaccess.h>
58 #ifdef CONFIG_XEN_NETDEV_GRANT
59 #include <asm-xen/xen-public/grant_table.h>
60 #include <asm-xen/gnttab.h>
62 static grant_ref_t gref_tx_head;
63 static grant_ref_t grant_tx_ref[NETIF_TX_RING_SIZE + 1];
65 static grant_ref_t gref_rx_head;
66 static grant_ref_t grant_rx_ref[NETIF_RX_RING_SIZE + 1];
68 #define GRANT_INVALID_REF (0xFFFF)
70 #ifdef GRANT_DEBUG
71 static void
72 dump_packet(int tag, void *addr, u32 ap)
73 {
74 unsigned char *p = (unsigned char *)ap;
75 int i;
77 printk(KERN_ALERT "#### rx_poll %c %08x ", tag & 0xff, addr);
78 for (i = 0; i < 20; i++) {
79 printk("%02x", p[i]);
80 }
81 printk("\n");
82 }
84 #define GDPRINTK(_f, _a...) printk(KERN_ALERT "(file=%s, line=%d) " _f, \
85 __FILE__ , __LINE__ , ## _a )
86 #else
87 #define dump_packet(x,y,z) ((void)0)
88 #define GDPRINTK(_f, _a...) ((void)0)
89 #endif
91 #endif
95 #ifndef __GFP_NOWARN
96 #define __GFP_NOWARN 0
97 #endif
98 #define alloc_xen_skb(_l) __dev_alloc_skb((_l), GFP_ATOMIC|__GFP_NOWARN)
100 #define init_skb_shinfo(_skb) \
101 do { \
102 atomic_set(&(skb_shinfo(_skb)->dataref), 1); \
103 skb_shinfo(_skb)->nr_frags = 0; \
104 skb_shinfo(_skb)->frag_list = NULL; \
105 } while (0)
107 /* Allow headroom on each rx pkt for Ethernet header, alignment padding, ... */
108 #define RX_HEADROOM 200
110 /*
111 * If the backend driver is pipelining transmit requests then we can be very
112 * aggressive in avoiding new-packet notifications -- only need to send a
113 * notification if there are no outstanding unreceived responses.
114 * If the backend may be buffering our transmit buffers for any reason then we
115 * are rather more conservative.
116 */
117 #ifdef CONFIG_XEN_NETDEV_FRONTEND_PIPELINED_TRANSMITTER
118 #define TX_TEST_IDX resp_prod /* aggressive: any outstanding responses? */
119 #else
120 #define TX_TEST_IDX req_cons /* conservative: not seen all our requests? */
121 #endif
124 #define NETIF_STATE_DISCONNECTED 0
125 #define NETIF_STATE_CONNECTED 1
128 static unsigned int netif_state = NETIF_STATE_DISCONNECTED;
130 static void network_tx_buf_gc(struct net_device *dev);
131 static void network_alloc_rx_buffers(struct net_device *dev);
133 static unsigned long rx_pfn_array[NETIF_RX_RING_SIZE];
134 static multicall_entry_t rx_mcl[NETIF_RX_RING_SIZE+1];
135 static mmu_update_t rx_mmu[NETIF_RX_RING_SIZE];
137 #ifdef CONFIG_PROC_FS
138 static int xennet_proc_init(void);
139 static int xennet_proc_addif(struct net_device *dev);
140 static void xennet_proc_delif(struct net_device *dev);
141 #else
142 #define xennet_proc_init() (0)
143 #define xennet_proc_addif(d) (0)
144 #define xennet_proc_delif(d) ((void)0)
145 #endif
147 #define netfront_info net_private
148 struct net_private
149 {
150 struct list_head list;
151 struct net_device *netdev;
153 struct net_device_stats stats;
154 NETIF_RING_IDX rx_resp_cons, tx_resp_cons;
155 unsigned int tx_full;
157 netif_tx_interface_t *tx;
158 netif_rx_interface_t *rx;
160 spinlock_t tx_lock;
161 spinlock_t rx_lock;
163 unsigned int handle;
164 unsigned int evtchn;
166 /* What is the status of our connection to the remote backend? */
167 #define BEST_CLOSED 0
168 #define BEST_DISCONNECTED 1
169 #define BEST_CONNECTED 2
170 unsigned int backend_state;
172 /* Is this interface open or closed (down or up)? */
173 #define UST_CLOSED 0
174 #define UST_OPEN 1
175 unsigned int user_state;
177 /* Receive-ring batched refills. */
178 #define RX_MIN_TARGET 8
179 #define RX_MAX_TARGET NETIF_RX_RING_SIZE
180 int rx_min_target, rx_max_target, rx_target;
181 struct sk_buff_head rx_batch;
183 /*
184 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
185 * array is an index into a chain of free entries.
186 */
187 struct sk_buff *tx_skbs[NETIF_TX_RING_SIZE+1];
188 struct sk_buff *rx_skbs[NETIF_RX_RING_SIZE+1];
190 struct xenbus_device *xbdev;
191 char *backend;
192 int backend_id;
193 struct xenbus_watch watch;
194 int tx_ring_ref;
195 int rx_ring_ref;
196 u8 mac[ETH_ALEN];
197 };
199 /* Access macros for acquiring freeing slots in {tx,rx}_skbs[]. */
200 #define ADD_ID_TO_FREELIST(_list, _id) \
201 (_list)[(_id)] = (_list)[0]; \
202 (_list)[0] = (void *)(unsigned long)(_id);
203 #define GET_ID_FROM_FREELIST(_list) \
204 ({ unsigned long _id = (unsigned long)(_list)[0]; \
205 (_list)[0] = (_list)[_id]; \
206 (unsigned short)_id; })
208 #ifdef DEBUG
209 static char *be_state_name[] = {
210 [BEST_CLOSED] = "closed",
211 [BEST_DISCONNECTED] = "disconnected",
212 [BEST_CONNECTED] = "connected",
213 };
214 #endif
216 #ifdef DEBUG
217 #define DPRINTK(fmt, args...) \
218 printk(KERN_ALERT "xen_net (%s:%d) " fmt, __FUNCTION__, __LINE__, ##args)
219 #else
220 #define DPRINTK(fmt, args...) ((void)0)
221 #endif
222 #define IPRINTK(fmt, args...) \
223 printk(KERN_INFO "xen_net: " fmt, ##args)
224 #define WPRINTK(fmt, args...) \
225 printk(KERN_WARNING "xen_net: " fmt, ##args)
227 /** Send a packet on a net device to encourage switches to learn the
228 * MAC. We send a fake ARP request.
229 *
230 * @param dev device
231 * @return 0 on success, error code otherwise
232 */
233 static int send_fake_arp(struct net_device *dev)
234 {
235 struct sk_buff *skb;
236 u32 src_ip, dst_ip;
238 dst_ip = INADDR_BROADCAST;
239 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
241 /* No IP? Then nothing to do. */
242 if (src_ip == 0)
243 return 0;
245 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
246 dst_ip, dev, src_ip,
247 /*dst_hw*/ NULL, /*src_hw*/ NULL,
248 /*target_hw*/ dev->dev_addr);
249 if (skb == NULL)
250 return -ENOMEM;
252 return dev_queue_xmit(skb);
253 }
255 static int network_open(struct net_device *dev)
256 {
257 struct net_private *np = netdev_priv(dev);
259 memset(&np->stats, 0, sizeof(np->stats));
261 np->user_state = UST_OPEN;
263 network_alloc_rx_buffers(dev);
264 np->rx->event = np->rx_resp_cons + 1;
266 netif_start_queue(dev);
268 return 0;
269 }
271 static void network_tx_buf_gc(struct net_device *dev)
272 {
273 NETIF_RING_IDX i, prod;
274 unsigned short id;
275 struct net_private *np = netdev_priv(dev);
276 struct sk_buff *skb;
278 if (np->backend_state != BEST_CONNECTED)
279 return;
281 do {
282 prod = np->tx->resp_prod;
283 rmb(); /* Ensure we see responses up to 'rp'. */
285 for (i = np->tx_resp_cons; i != prod; i++) {
286 id = np->tx->ring[MASK_NETIF_TX_IDX(i)].resp.id;
287 skb = np->tx_skbs[id];
288 #ifdef CONFIG_XEN_NETDEV_GRANT
289 if (unlikely(gnttab_query_foreign_access(grant_tx_ref[id]) != 0)) {
290 /* other domain is still using this grant - shouldn't happen
291 but if it does, we'll try to reclaim the grant later */
292 printk(KERN_ALERT "network_tx_buf_gc: warning -- grant "
293 "still in use by backend domain.\n");
294 goto out;
295 }
296 gnttab_end_foreign_access_ref(grant_tx_ref[id], GNTMAP_readonly);
297 gnttab_release_grant_reference(&gref_tx_head, grant_tx_ref[id]);
298 grant_tx_ref[id] = GRANT_INVALID_REF;
299 #endif
300 ADD_ID_TO_FREELIST(np->tx_skbs, id);
301 dev_kfree_skb_irq(skb);
302 }
304 np->tx_resp_cons = prod;
306 /*
307 * Set a new event, then check for race with update of tx_cons. Note
308 * that it is essential to schedule a callback, no matter how few
309 * buffers are pending. Even if there is space in the transmit ring,
310 * higher layers may be blocked because too much data is outstanding:
311 * in such cases notification from Xen is likely to be the only kick
312 * that we'll get.
313 */
314 np->tx->event =
315 prod + ((np->tx->req_prod - prod) >> 1) + 1;
316 mb();
317 } while (prod != np->tx->resp_prod);
319 #ifdef CONFIG_XEN_NETDEV_GRANT
320 out:
321 #endif
323 if (np->tx_full && ((np->tx->req_prod - prod) < NETIF_TX_RING_SIZE)) {
324 np->tx_full = 0;
325 if (np->user_state == UST_OPEN)
326 netif_wake_queue(dev);
327 }
328 }
331 static void network_alloc_rx_buffers(struct net_device *dev)
332 {
333 unsigned short id;
334 struct net_private *np = netdev_priv(dev);
335 struct sk_buff *skb;
336 int i, batch_target;
337 NETIF_RING_IDX req_prod = np->rx->req_prod;
338 struct xen_memory_reservation reservation;
339 #ifdef CONFIG_XEN_NETDEV_GRANT
340 grant_ref_t ref;
341 #endif
343 if (unlikely(np->backend_state != BEST_CONNECTED))
344 return;
346 /*
347 * Allocate skbuffs greedily, even though we batch updates to the
348 * receive ring. This creates a less bursty demand on the memory allocator,
349 * so should reduce the chance of failed allocation requests both for
350 * ourself and for other kernel subsystems.
351 */
352 batch_target = np->rx_target - (req_prod - np->rx_resp_cons);
353 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
354 if (unlikely((skb = alloc_xen_skb(dev->mtu + RX_HEADROOM)) == NULL))
355 break;
356 __skb_queue_tail(&np->rx_batch, skb);
357 }
359 /* Is the batch large enough to be worthwhile? */
360 if (i < (np->rx_target/2))
361 return;
363 for (i = 0; ; i++) {
364 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
365 break;
367 skb->dev = dev;
369 id = GET_ID_FROM_FREELIST(np->rx_skbs);
371 np->rx_skbs[id] = skb;
373 np->rx->ring[MASK_NETIF_RX_IDX(req_prod + i)].req.id = id;
374 #ifdef CONFIG_XEN_NETDEV_GRANT
375 ref = gnttab_claim_grant_reference(&gref_rx_head);
376 if (unlikely((signed short)ref < 0)) {
377 printk(KERN_ALERT "#### netfront can't claim rx reference\n");
378 BUG();
379 }
380 grant_rx_ref[id] = ref;
381 gnttab_grant_foreign_transfer_ref(ref, np->backend_id,
382 virt_to_mfn(skb->head));
383 np->rx->ring[MASK_NETIF_RX_IDX(req_prod + i)].req.gref = ref;
384 #endif
385 rx_pfn_array[i] = virt_to_mfn(skb->head);
387 /* Remove this page from pseudo phys map before passing back to Xen. */
388 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT]
389 = INVALID_P2M_ENTRY;
391 MULTI_update_va_mapping(rx_mcl+i, (unsigned long)skb->head,
392 __pte(0), 0);
393 }
395 /* After all PTEs have been zapped we blow away stale TLB entries. */
396 rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] = UVMF_TLB_FLUSH|UVMF_ALL;
398 /* Give away a batch of pages. */
399 rx_mcl[i].op = __HYPERVISOR_memory_op;
400 rx_mcl[i].args[0] = XENMEM_decrease_reservation;
401 rx_mcl[i].args[1] = (unsigned long)&reservation;
403 reservation.extent_start = rx_pfn_array;
404 reservation.nr_extents = i;
405 reservation.extent_order = 0;
406 reservation.address_bits = 0;
407 reservation.domid = DOMID_SELF;
409 /* Tell the ballon driver what is going on. */
410 balloon_update_driver_allowance(i);
412 /* Zap PTEs and give away pages in one big multicall. */
413 (void)HYPERVISOR_multicall(rx_mcl, i+1);
415 /* Check return status of HYPERVISOR_memory_op(). */
416 if (unlikely(rx_mcl[i].result != i))
417 panic("Unable to reduce memory reservation\n");
419 /* Above is a suitable barrier to ensure backend will see requests. */
420 np->rx->req_prod = req_prod + i;
422 /* Adjust our floating fill target if we risked running out of buffers. */
423 if (((req_prod - np->rx->resp_prod) < (np->rx_target / 4)) &&
424 ((np->rx_target *= 2) > np->rx_max_target))
425 np->rx_target = np->rx_max_target;
426 }
429 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
430 {
431 unsigned short id;
432 struct net_private *np = netdev_priv(dev);
433 netif_tx_request_t *tx;
434 NETIF_RING_IDX i;
435 #ifdef CONFIG_XEN_NETDEV_GRANT
436 grant_ref_t ref;
437 unsigned long mfn;
438 #endif
440 if (unlikely(np->tx_full)) {
441 printk(KERN_ALERT "%s: full queue wasn't stopped!\n", dev->name);
442 netif_stop_queue(dev);
443 goto drop;
444 }
446 if (unlikely((((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >=
447 PAGE_SIZE)) {
448 struct sk_buff *nskb;
449 if (unlikely((nskb = alloc_xen_skb(skb->len)) == NULL))
450 goto drop;
451 skb_put(nskb, skb->len);
452 memcpy(nskb->data, skb->data, skb->len);
453 nskb->dev = skb->dev;
454 dev_kfree_skb(skb);
455 skb = nskb;
456 }
458 spin_lock_irq(&np->tx_lock);
460 if (np->backend_state != BEST_CONNECTED) {
461 spin_unlock_irq(&np->tx_lock);
462 goto drop;
463 }
465 i = np->tx->req_prod;
467 id = GET_ID_FROM_FREELIST(np->tx_skbs);
468 np->tx_skbs[id] = skb;
470 tx = &np->tx->ring[MASK_NETIF_TX_IDX(i)].req;
472 tx->id = id;
473 #ifdef CONFIG_XEN_NETDEV_GRANT
474 ref = gnttab_claim_grant_reference(&gref_tx_head);
475 if (unlikely((signed short)ref < 0)) {
476 printk(KERN_ALERT "#### netfront can't claim tx grant reference\n");
477 BUG();
478 }
479 mfn = virt_to_mfn(skb->data);
480 gnttab_grant_foreign_access_ref(ref, np->backend_id, mfn, GNTMAP_readonly);
481 tx->addr = ref << PAGE_SHIFT;
482 grant_tx_ref[id] = ref;
483 #else
484 tx->addr = virt_to_mfn(skb->data) << PAGE_SHIFT;
485 #endif
486 tx->addr |= (unsigned long)skb->data & ~PAGE_MASK;
487 tx->size = skb->len;
488 tx->csum_blank = (skb->ip_summed == CHECKSUM_HW);
490 wmb(); /* Ensure that backend will see the request. */
491 np->tx->req_prod = i + 1;
493 network_tx_buf_gc(dev);
495 if ((i - np->tx_resp_cons) == (NETIF_TX_RING_SIZE - 1)) {
496 np->tx_full = 1;
497 netif_stop_queue(dev);
498 }
500 spin_unlock_irq(&np->tx_lock);
502 np->stats.tx_bytes += skb->len;
503 np->stats.tx_packets++;
505 /* Only notify Xen if we really have to. */
506 mb();
507 if (np->tx->TX_TEST_IDX == i)
508 notify_via_evtchn(np->evtchn);
510 return 0;
512 drop:
513 np->stats.tx_dropped++;
514 dev_kfree_skb(skb);
515 return 0;
516 }
518 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
519 {
520 struct net_device *dev = dev_id;
521 struct net_private *np = netdev_priv(dev);
522 unsigned long flags;
524 spin_lock_irqsave(&np->tx_lock, flags);
525 network_tx_buf_gc(dev);
526 spin_unlock_irqrestore(&np->tx_lock, flags);
528 if((np->rx_resp_cons != np->rx->resp_prod) && (np->user_state == UST_OPEN))
529 netif_rx_schedule(dev);
531 return IRQ_HANDLED;
532 }
535 static int netif_poll(struct net_device *dev, int *pbudget)
536 {
537 struct net_private *np = netdev_priv(dev);
538 struct sk_buff *skb, *nskb;
539 netif_rx_response_t *rx;
540 NETIF_RING_IDX i, rp;
541 mmu_update_t *mmu = rx_mmu;
542 multicall_entry_t *mcl = rx_mcl;
543 int work_done, budget, more_to_do = 1;
544 struct sk_buff_head rxq;
545 unsigned long flags;
546 #ifdef CONFIG_XEN_NETDEV_GRANT
547 unsigned long mfn;
548 grant_ref_t ref;
549 #endif
551 spin_lock(&np->rx_lock);
553 if (np->backend_state != BEST_CONNECTED) {
554 spin_unlock(&np->rx_lock);
555 return 0;
556 }
558 skb_queue_head_init(&rxq);
560 if ((budget = *pbudget) > dev->quota)
561 budget = dev->quota;
562 rp = np->rx->resp_prod;
563 rmb(); /* Ensure we see queued responses up to 'rp'. */
565 for (i = np->rx_resp_cons, work_done = 0;
566 (i != rp) && (work_done < budget);
567 i++, work_done++) {
568 rx = &np->rx->ring[MASK_NETIF_RX_IDX(i)].resp;
569 /*
570 * An error here is very odd. Usually indicates a backend bug,
571 * low-memory condition, or that we didn't have reservation headroom.
572 */
573 if (unlikely(rx->status <= 0)) {
574 if (net_ratelimit())
575 printk(KERN_WARNING "Bad rx buffer (memory squeeze?).\n");
576 np->rx->ring[MASK_NETIF_RX_IDX(np->rx->req_prod)].req.id = rx->id;
577 wmb();
578 np->rx->req_prod++;
579 work_done--;
580 continue;
581 }
583 #ifdef CONFIG_XEN_NETDEV_GRANT
584 ref = grant_rx_ref[rx->id];
586 if(ref == GRANT_INVALID_REF) {
587 printk(KERN_WARNING "Bad rx grant reference %d from dom %d.\n",
588 ref, np->backend_id);
589 np->rx->ring[MASK_NETIF_RX_IDX(np->rx->req_prod)].req.id = rx->id;
590 wmb();
591 np->rx->req_prod++;
592 work_done--;
593 continue;
594 }
596 grant_rx_ref[rx->id] = GRANT_INVALID_REF;
597 mfn = gnttab_end_foreign_transfer_ref(ref);
598 gnttab_release_grant_reference(&gref_rx_head, ref);
599 #endif
601 skb = np->rx_skbs[rx->id];
602 ADD_ID_TO_FREELIST(np->rx_skbs, rx->id);
604 /* NB. We handle skb overflow later. */
605 #ifdef CONFIG_XEN_NETDEV_GRANT
606 skb->data = skb->head + rx->addr;
607 #else
608 skb->data = skb->head + (rx->addr & ~PAGE_MASK);
609 #endif
610 skb->len = rx->status;
611 skb->tail = skb->data + skb->len;
613 if ( rx->csum_valid )
614 skb->ip_summed = CHECKSUM_UNNECESSARY;
616 np->stats.rx_packets++;
617 np->stats.rx_bytes += rx->status;
619 /* Remap the page. */
620 #ifdef CONFIG_XEN_NETDEV_GRANT
621 mmu->ptr = ((unsigned long long)mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE;
622 #else
623 mmu->ptr = (rx->addr & PAGE_MASK) | MMU_MACHPHYS_UPDATE;
624 #endif
625 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
626 mmu++;
627 #ifdef CONFIG_XEN_NETDEV_GRANT
628 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
629 pfn_pte_ma(mfn, PAGE_KERNEL), 0);
630 #else
631 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
632 pfn_pte_ma(rx->addr >> PAGE_SHIFT,
633 PAGE_KERNEL), 0);
634 #endif
635 mcl++;
637 #ifdef CONFIG_XEN_NETDEV_GRANT
638 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] = mfn;
639 GDPRINTK("#### rx_poll enqueue vdata=%p mfn=%lu ref=%x\n",
640 skb->data, mfn, ref);
641 #else
642 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] =
643 rx->addr >> PAGE_SHIFT;
644 #endif
647 __skb_queue_tail(&rxq, skb);
648 }
651 /* Some pages are no longer absent... */
652 balloon_update_driver_allowance(-work_done);
654 /* Do all the remapping work, and M->P updates, in one big hypercall. */
655 if (likely((mcl - rx_mcl) != 0)) {
656 mcl->op = __HYPERVISOR_mmu_update;
657 mcl->args[0] = (unsigned long)rx_mmu;
658 mcl->args[1] = mmu - rx_mmu;
659 mcl->args[2] = 0;
660 mcl->args[3] = DOMID_SELF;
661 mcl++;
662 (void)HYPERVISOR_multicall(rx_mcl, mcl - rx_mcl);
663 }
665 while ((skb = __skb_dequeue(&rxq)) != NULL) {
666 #ifdef CONFIG_XEN_NETDEV_GRANT
667 GDPRINTK("#### rx_poll dequeue vdata=%p mfn=%lu\n",
668 skb->data, virt_to_mfn(skb->data));
669 dump_packet('d', skb->data, (unsigned long)skb->data);
670 #endif
671 /*
672 * Enough room in skbuff for the data we were passed? Also, Linux
673 * expects at least 16 bytes headroom in each receive buffer.
674 */
675 if (unlikely(skb->tail > skb->end) ||
676 unlikely((skb->data - skb->head) < 16)) {
677 nskb = NULL;
680 /* Only copy the packet if it fits in the current MTU. */
681 if (skb->len <= (dev->mtu + ETH_HLEN)) {
682 if ((skb->tail > skb->end) && net_ratelimit())
683 printk(KERN_INFO "Received packet needs %zd bytes more "
684 "headroom.\n", skb->tail - skb->end);
686 if ((nskb = alloc_xen_skb(skb->len + 2)) != NULL) {
687 skb_reserve(nskb, 2);
688 skb_put(nskb, skb->len);
689 memcpy(nskb->data, skb->data, skb->len);
690 nskb->dev = skb->dev;
691 }
692 }
693 else if (net_ratelimit())
694 printk(KERN_INFO "Received packet too big for MTU "
695 "(%d > %d)\n", skb->len - ETH_HLEN, dev->mtu);
697 /* Reinitialise and then destroy the old skbuff. */
698 skb->len = 0;
699 skb->tail = skb->data;
700 init_skb_shinfo(skb);
701 dev_kfree_skb(skb);
703 /* Switch old for new, if we copied the buffer. */
704 if ((skb = nskb) == NULL)
705 continue;
706 }
708 /* Set the shared-info area, which is hidden behind the real data. */
709 init_skb_shinfo(skb);
710 /* Ethernet-specific work. Delayed to here as it peeks the header. */
711 skb->protocol = eth_type_trans(skb, dev);
713 /* Pass it up. */
714 netif_receive_skb(skb);
715 dev->last_rx = jiffies;
716 }
718 np->rx_resp_cons = i;
720 /* If we get a callback with very few responses, reduce fill target. */
721 /* NB. Note exponential increase, linear decrease. */
722 if (((np->rx->req_prod - np->rx->resp_prod) > ((3*np->rx_target) / 4)) &&
723 (--np->rx_target < np->rx_min_target))
724 np->rx_target = np->rx_min_target;
726 network_alloc_rx_buffers(dev);
728 *pbudget -= work_done;
729 dev->quota -= work_done;
731 if (work_done < budget) {
732 local_irq_save(flags);
734 np->rx->event = i + 1;
736 /* Deal with hypervisor racing our resetting of rx_event. */
737 mb();
738 if (np->rx->resp_prod == i) {
739 __netif_rx_complete(dev);
740 more_to_do = 0;
741 }
743 local_irq_restore(flags);
744 }
746 spin_unlock(&np->rx_lock);
748 return more_to_do;
749 }
752 static int network_close(struct net_device *dev)
753 {
754 struct net_private *np = netdev_priv(dev);
755 np->user_state = UST_CLOSED;
756 netif_stop_queue(np->netdev);
757 return 0;
758 }
761 static struct net_device_stats *network_get_stats(struct net_device *dev)
762 {
763 struct net_private *np = netdev_priv(dev);
764 return &np->stats;
765 }
767 static void network_connect(struct net_device *dev)
768 {
769 struct net_private *np;
770 int i, requeue_idx;
771 netif_tx_request_t *tx;
773 np = netdev_priv(dev);
774 spin_lock_irq(&np->tx_lock);
775 spin_lock(&np->rx_lock);
777 /* Recovery procedure: */
779 /* Step 1: Reinitialise variables. */
780 np->rx_resp_cons = np->tx_resp_cons = np->tx_full = 0;
781 np->rx->event = np->tx->event = 1;
783 /* Step 2: Rebuild the RX and TX ring contents.
784 * NB. We could just free the queued TX packets now but we hope
785 * that sending them out might do some good. We have to rebuild
786 * the RX ring because some of our pages are currently flipped out
787 * so we can't just free the RX skbs.
788 * NB2. Freelist index entries are always going to be less than
789 * __PAGE_OFFSET, whereas pointers to skbs will always be equal or
790 * greater than __PAGE_OFFSET: we use this property to distinguish
791 * them.
792 */
794 /* Rebuild the TX buffer freelist and the TX ring itself.
795 * NB. This reorders packets. We could keep more private state
796 * to avoid this but maybe it doesn't matter so much given the
797 * interface has been down.
798 */
799 for (requeue_idx = 0, i = 1; i <= NETIF_TX_RING_SIZE; i++) {
800 if ((unsigned long)np->tx_skbs[i] >= __PAGE_OFFSET) {
801 struct sk_buff *skb = np->tx_skbs[i];
803 tx = &np->tx->ring[requeue_idx++].req;
805 tx->id = i;
806 #ifdef CONFIG_XEN_NETDEV_GRANT
807 gnttab_grant_foreign_access_ref(grant_tx_ref[i], np->backend_id,
808 virt_to_mfn(np->tx_skbs[i]->data),
809 GNTMAP_readonly);
810 tx->addr = grant_tx_ref[i] << PAGE_SHIFT;
811 #else
812 tx->addr = virt_to_mfn(skb->data) << PAGE_SHIFT;
813 #endif
814 tx->addr |= (unsigned long)skb->data & ~PAGE_MASK;
815 tx->size = skb->len;
817 np->stats.tx_bytes += skb->len;
818 np->stats.tx_packets++;
819 }
820 }
821 wmb();
822 np->tx->req_prod = requeue_idx;
824 /* Rebuild the RX buffer freelist and the RX ring itself. */
825 for (requeue_idx = 0, i = 1; i <= NETIF_RX_RING_SIZE; i++) {
826 if ((unsigned long)np->rx_skbs[i] >= __PAGE_OFFSET) {
827 #ifdef CONFIG_XEN_NETDEV_GRANT
828 /* Reinstate the grant ref so backend can 'donate' mfn to us. */
829 gnttab_grant_foreign_transfer_ref(grant_rx_ref[i], np->backend_id,
830 virt_to_mfn(np->rx_skbs[i]->head)
831 );
832 np->rx->ring[requeue_idx].req.gref = grant_rx_ref[i];
833 #endif
834 np->rx->ring[requeue_idx].req.id = i;
835 requeue_idx++;
836 }
837 }
839 wmb();
840 np->rx->req_prod = requeue_idx;
842 /* Step 3: All public and private state should now be sane. Get
843 * ready to start sending and receiving packets and give the driver
844 * domain a kick because we've probably just requeued some
845 * packets.
846 */
847 np->backend_state = BEST_CONNECTED;
848 wmb();
849 notify_via_evtchn(np->evtchn);
850 network_tx_buf_gc(dev);
852 if (np->user_state == UST_OPEN)
853 netif_start_queue(dev);
855 spin_unlock(&np->rx_lock);
856 spin_unlock_irq(&np->tx_lock);
857 }
859 static void show_device(struct net_private *np)
860 {
861 #ifdef DEBUG
862 if (np) {
863 IPRINTK("<vif handle=%u %s(%s) evtchn=%u tx=%p rx=%p>\n",
864 np->handle,
865 be_state_name[np->backend_state],
866 np->user_state ? "open" : "closed",
867 np->evtchn,
868 np->tx,
869 np->rx);
870 } else {
871 IPRINTK("<vif NULL>\n");
872 }
873 #endif
874 }
876 /* Move the vif into connected state.
877 * Sets the mac and event channel from the message.
878 * Binds the irq to the event channel.
879 */
880 static void
881 connect_device(struct net_private *np, unsigned int evtchn)
882 {
883 struct net_device *dev = np->netdev;
884 memcpy(dev->dev_addr, np->mac, ETH_ALEN);
885 np->evtchn = evtchn;
886 network_connect(dev);
887 (void)bind_evtchn_to_irqhandler(
888 np->evtchn, netif_int, SA_SAMPLE_RANDOM, dev->name, dev);
889 (void)send_fake_arp(dev);
890 show_device(np);
891 }
893 static struct ethtool_ops network_ethtool_ops =
894 {
895 .get_tx_csum = ethtool_op_get_tx_csum,
896 .set_tx_csum = ethtool_op_set_tx_csum,
897 };
899 /** Create a network device.
900 * @param handle device handle
901 * @param val return parameter for created device
902 * @return 0 on success, error code otherwise
903 */
904 static int create_netdev(int handle, struct xenbus_device *dev,
905 struct net_device **val)
906 {
907 int i, err = 0;
908 struct net_device *netdev = NULL;
909 struct net_private *np = NULL;
911 if ((netdev = alloc_etherdev(sizeof(struct net_private))) == NULL) {
912 printk(KERN_WARNING "%s> alloc_etherdev failed.\n", __FUNCTION__);
913 err = -ENOMEM;
914 goto exit;
915 }
917 np = netdev_priv(netdev);
918 np->backend_state = BEST_CLOSED;
919 np->user_state = UST_CLOSED;
920 np->handle = handle;
921 np->xbdev = dev;
923 spin_lock_init(&np->tx_lock);
924 spin_lock_init(&np->rx_lock);
926 skb_queue_head_init(&np->rx_batch);
927 np->rx_target = RX_MIN_TARGET;
928 np->rx_min_target = RX_MIN_TARGET;
929 np->rx_max_target = RX_MAX_TARGET;
931 /* Initialise {tx,rx}_skbs to be a free chain containing every entry. */
932 for (i = 0; i <= NETIF_TX_RING_SIZE; i++) {
933 np->tx_skbs[i] = (void *)((unsigned long) i+1);
934 #ifdef CONFIG_XEN_NETDEV_GRANT
935 grant_tx_ref[i] = GRANT_INVALID_REF;
936 #endif
937 }
939 for (i = 0; i <= NETIF_RX_RING_SIZE; i++) {
940 np->rx_skbs[i] = (void *)((unsigned long) i+1);
941 #ifdef CONFIG_XEN_NETDEV_GRANT
942 grant_rx_ref[i] = GRANT_INVALID_REF;
943 #endif
944 }
946 netdev->open = network_open;
947 netdev->hard_start_xmit = network_start_xmit;
948 netdev->stop = network_close;
949 netdev->get_stats = network_get_stats;
950 netdev->poll = netif_poll;
951 netdev->weight = 64;
952 netdev->features = NETIF_F_IP_CSUM;
954 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
956 if ((err = register_netdev(netdev)) != 0) {
957 printk(KERN_WARNING "%s> register_netdev err=%d\n", __FUNCTION__, err);
958 goto exit;
959 }
961 if ((err = xennet_proc_addif(netdev)) != 0) {
962 unregister_netdev(netdev);
963 goto exit;
964 }
966 np->netdev = netdev;
968 exit:
969 if ((err != 0) && (netdev != NULL))
970 kfree(netdev);
971 else if (val != NULL)
972 *val = netdev;
973 return err;
974 }
976 static int destroy_netdev(struct net_device *netdev)
977 {
979 #ifdef CONFIG_PROC_FS
980 xennet_proc_delif(netdev);
981 #endif
983 unregister_netdev(netdev);
985 return 0;
986 }
988 /*
989 * We use this notifier to send out a fake ARP reply to reset switches and
990 * router ARP caches when an IP interface is brought up on a VIF.
991 */
992 static int
993 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
994 {
995 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
996 struct net_device *dev = ifa->ifa_dev->dev;
998 /* UP event and is it one of our devices? */
999 if (event == NETDEV_UP && dev->open == network_open)
1000 (void)send_fake_arp(dev);
1002 return NOTIFY_DONE;
1005 static struct notifier_block notifier_inetdev = {
1006 .notifier_call = inetdev_notify,
1007 .next = NULL,
1008 .priority = 0
1009 };
1011 static struct xenbus_device_id netfront_ids[] = {
1012 { "vif" },
1013 { "" }
1014 };
1016 static void watch_for_status(struct xenbus_watch *watch, const char *node)
1020 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
1022 evtchn_op_t op = { .cmd = EVTCHNOP_alloc_unbound };
1023 int err;
1025 #ifdef CONFIG_XEN_NETDEV_GRANT
1026 info->tx_ring_ref = GRANT_INVALID_REF;
1027 info->rx_ring_ref = GRANT_INVALID_REF;
1028 #endif
1030 info->tx = (netif_tx_interface_t *)__get_free_page(GFP_KERNEL);
1031 if (info->tx == 0) {
1032 err = -ENOMEM;
1033 xenbus_dev_error(dev, err, "allocating tx ring page");
1034 goto out;
1036 info->rx = (netif_rx_interface_t *)__get_free_page(GFP_KERNEL);
1037 if (info->rx == 0) {
1038 err = -ENOMEM;
1039 xenbus_dev_error(dev, err, "allocating rx ring page");
1040 goto out;
1042 memset(info->tx, 0, PAGE_SIZE);
1043 memset(info->rx, 0, PAGE_SIZE);
1044 info->backend_state = BEST_DISCONNECTED;
1046 #ifdef CONFIG_XEN_NETDEV_GRANT
1047 err = gnttab_grant_foreign_access(info->backend_id,
1048 virt_to_mfn(info->tx), 0);
1049 if (err < 0) {
1050 xenbus_dev_error(dev, err, "granting access to tx ring page");
1051 goto out;
1053 info->tx_ring_ref = err;
1055 err = gnttab_grant_foreign_access(info->backend_id,
1056 virt_to_mfn(info->rx), 0);
1057 if (err < 0) {
1058 xenbus_dev_error(dev, err, "granting access to rx ring page");
1059 goto out;
1061 info->rx_ring_ref = err;
1063 #else
1064 info->tx_ring_ref = virt_to_mfn(info->tx);
1065 info->rx_ring_ref = virt_to_mfn(info->rx);
1066 #endif
1068 op.u.alloc_unbound.dom = info->backend_id;
1069 err = HYPERVISOR_event_channel_op(&op);
1070 if (err) {
1071 xenbus_dev_error(dev, err, "allocating event channel");
1072 goto out;
1074 connect_device(info, op.u.alloc_unbound.port);
1075 return 0;
1077 out:
1078 if (info->tx)
1079 free_page((unsigned long)info->tx);
1080 info->tx = 0;
1081 if (info->rx)
1082 free_page((unsigned long)info->rx);
1083 info->rx = 0;
1085 #ifdef CONFIG_XEN_NETDEV_GRANT
1086 if (info->tx_ring_ref != GRANT_INVALID_REF)
1087 gnttab_end_foreign_access(info->tx_ring_ref, 0);
1088 info->tx_ring_ref = GRANT_INVALID_REF;
1090 if (info->rx_ring_ref != GRANT_INVALID_REF)
1091 gnttab_end_foreign_access(info->rx_ring_ref, 0);
1092 info->rx_ring_ref = GRANT_INVALID_REF;
1093 #endif
1095 return err;
1098 static void netif_free(struct netfront_info *info)
1100 if (info->tx)
1101 free_page((unsigned long)info->tx);
1102 info->tx = 0;
1103 if (info->rx)
1104 free_page((unsigned long)info->rx);
1105 info->rx = 0;
1107 #ifdef CONFIG_XEN_NETDEV_GRANT
1108 if (info->tx_ring_ref != GRANT_INVALID_REF)
1109 gnttab_end_foreign_access(info->tx_ring_ref, 0);
1110 info->tx_ring_ref = GRANT_INVALID_REF;
1112 if (info->rx_ring_ref != GRANT_INVALID_REF)
1113 gnttab_end_foreign_access(info->rx_ring_ref, 0);
1114 info->rx_ring_ref = GRANT_INVALID_REF;
1115 #endif
1117 unbind_evtchn_from_irqhandler(info->evtchn, info->netdev);
1118 info->evtchn = 0;
1121 /* Stop network device and free tx/rx queues and irq.
1122 */
1123 static void shutdown_device(struct net_private *np)
1125 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1126 spin_lock_irq(&np->tx_lock);
1127 spin_lock(&np->rx_lock);
1128 netif_stop_queue(np->netdev);
1129 /* np->backend_state = BEST_DISCONNECTED; */
1130 spin_unlock(&np->rx_lock);
1131 spin_unlock_irq(&np->tx_lock);
1133 /* Free resources. */
1134 netif_free(np);
1137 /* Common code used when first setting up, and when resuming. */
1138 static int talk_to_backend(struct xenbus_device *dev,
1139 struct netfront_info *info)
1141 char *backend, *mac, *e, *s;
1142 const char *message;
1143 int err, i;
1145 backend = NULL;
1146 err = xenbus_gather(dev->nodename,
1147 "backend-id", "%i", &info->backend_id,
1148 "backend", NULL, &backend,
1149 NULL);
1150 if (XENBUS_EXIST_ERR(err))
1151 goto out;
1152 if (backend && strlen(backend) == 0) {
1153 err = -ENOENT;
1154 goto out;
1156 if (err < 0) {
1157 xenbus_dev_error(dev, err, "reading %s/backend or backend-id",
1158 dev->nodename);
1159 goto out;
1162 mac = xenbus_read(dev->nodename, "mac", NULL);
1163 if (IS_ERR(mac)) {
1164 err = PTR_ERR(mac);
1165 xenbus_dev_error(dev, err, "reading %s/mac",
1166 dev->nodename);
1167 goto out;
1169 s = mac;
1170 for (i = 0; i < ETH_ALEN; i++) {
1171 info->mac[i] = simple_strtoul(s, &e, 16);
1172 if (s == e || (e[0] != ':' && e[0] != 0)) {
1173 kfree(mac);
1174 err = -ENOENT;
1175 xenbus_dev_error(dev, err, "parsing %s/mac",
1176 dev->nodename);
1177 goto out;
1179 s = &e[1];
1181 kfree(mac);
1183 /* Create shared ring, alloc event channel. */
1184 err = setup_device(dev, info);
1185 if (err) {
1186 xenbus_dev_error(dev, err, "setting up ring");
1187 goto out;
1190 err = xenbus_transaction_start(dev->nodename);
1191 if (err) {
1192 xenbus_dev_error(dev, err, "starting transaction");
1193 goto destroy_ring;
1196 err = xenbus_printf(dev->nodename, "tx-ring-ref","%u",
1197 info->tx_ring_ref);
1198 if (err) {
1199 message = "writing tx ring-ref";
1200 goto abort_transaction;
1202 err = xenbus_printf(dev->nodename, "rx-ring-ref","%u",
1203 info->rx_ring_ref);
1204 if (err) {
1205 message = "writing rx ring-ref";
1206 goto abort_transaction;
1208 err = xenbus_printf(dev->nodename,
1209 "event-channel", "%u", info->evtchn);
1210 if (err) {
1211 message = "writing event-channel";
1212 goto abort_transaction;
1215 info->backend = backend;
1216 backend = NULL;
1218 info->watch.node = info->backend;
1219 info->watch.callback = watch_for_status;
1220 err = register_xenbus_watch(&info->watch);
1221 if (err) {
1222 message = "registering watch on backend";
1223 goto abort_transaction;
1226 err = xenbus_transaction_end(0);
1227 if (err) {
1228 xenbus_dev_error(dev, err, "completing transaction");
1229 goto destroy_ring;
1232 netif_state = NETIF_STATE_CONNECTED;
1234 out:
1235 kfree(backend);
1236 return err;
1238 abort_transaction:
1239 xenbus_transaction_end(1);
1240 /* Have to do this *outside* transaction. */
1241 xenbus_dev_error(dev, err, "%s", message);
1242 destroy_ring:
1243 shutdown_device(info);
1244 goto out;
1247 /* Setup supplies the backend dir, virtual device.
1249 We place an event channel and shared frame entries.
1250 We watch backend to wait if it's ok. */
1251 static int netfront_probe(struct xenbus_device *dev,
1252 const struct xenbus_device_id *id)
1254 int err;
1255 struct net_device *netdev;
1256 struct netfront_info *info;
1257 unsigned int handle;
1259 err = xenbus_scanf(dev->nodename, "handle", "%u", &handle);
1260 if (XENBUS_EXIST_ERR(err))
1261 return err;
1262 if (err < 0) {
1263 xenbus_dev_error(dev, err, "reading handle");
1264 return err;
1267 err = create_netdev(handle, dev, &netdev);
1268 if (err) {
1269 xenbus_dev_error(dev, err, "creating netdev");
1270 return err;
1273 info = netdev_priv(netdev);
1274 dev->data = info;
1276 err = talk_to_backend(dev, info);
1277 if (err) {
1278 destroy_netdev(netdev);
1279 kfree(netdev);
1280 dev->data = NULL;
1281 return err;
1285 /* Call once in case entries already there. */
1286 watch_for_status(&info->watch, info->watch.node);
1288 return 0;
1291 static int netfront_remove(struct xenbus_device *dev)
1293 struct netfront_info *info = dev->data;
1295 if (info->backend)
1296 unregister_xenbus_watch(&info->watch);
1298 netif_free(info);
1300 kfree(info->backend);
1301 kfree(info);
1303 return 0;
1306 static int netfront_suspend(struct xenbus_device *dev)
1308 struct netfront_info *info = dev->data;
1310 unregister_xenbus_watch(&info->watch);
1311 kfree(info->backend);
1312 info->backend = NULL;
1314 netif_free(info);
1316 return 0;
1319 static int netfront_resume(struct xenbus_device *dev)
1321 struct net_private *np = dev->data;
1322 int err;
1324 err = talk_to_backend(dev, np);
1326 return err;
1329 static struct xenbus_driver netfront = {
1330 .name = "vif",
1331 .owner = THIS_MODULE,
1332 .ids = netfront_ids,
1333 .probe = netfront_probe,
1334 .remove = netfront_remove,
1335 .resume = netfront_resume,
1336 .suspend = netfront_suspend,
1337 };
1339 static void __init init_net_xenbus(void)
1341 xenbus_register_device(&netfront);
1344 static int wait_for_netif(void)
1346 int err = 0;
1347 int i;
1349 /*
1350 * We should figure out how many and which devices we need to
1351 * proceed and only wait for those. For now, continue once the
1352 * first device is around.
1353 */
1354 for ( i=0; netif_state != NETIF_STATE_CONNECTED && (i < 10*HZ); i++ )
1356 set_current_state(TASK_INTERRUPTIBLE);
1357 schedule_timeout(1);
1360 if (netif_state != NETIF_STATE_CONNECTED) {
1361 WPRINTK("Timeout connecting to device!\n");
1362 err = -ENOSYS;
1364 return err;
1367 static int __init netif_init(void)
1369 int err = 0;
1371 if (xen_start_info->flags & SIF_INITDOMAIN)
1372 return 0;
1374 if ((err = xennet_proc_init()) != 0)
1375 return err;
1377 IPRINTK("Initialising virtual ethernet driver.\n");
1379 #ifdef CONFIG_XEN_NETDEV_GRANT
1380 IPRINTK("Using grant tables.\n");
1382 /* A grant for every tx ring slot */
1383 if (gnttab_alloc_grant_references(NETIF_TX_RING_SIZE,
1384 &gref_tx_head) < 0) {
1385 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1386 return 1;
1388 /* A grant for every rx ring slot */
1389 if (gnttab_alloc_grant_references(NETIF_RX_RING_SIZE,
1390 &gref_rx_head) < 0) {
1391 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1392 return 1;
1394 #endif
1397 (void)register_inetaddr_notifier(&notifier_inetdev);
1399 init_net_xenbus();
1401 wait_for_netif();
1403 return err;
1406 static void netif_exit(void)
1408 #ifdef CONFIG_XEN_NETDEV_GRANT
1409 gnttab_free_grant_references(gref_tx_head);
1410 gnttab_free_grant_references(gref_rx_head);
1411 #endif
1414 #ifdef CONFIG_PROC_FS
1416 #define TARGET_MIN 0UL
1417 #define TARGET_MAX 1UL
1418 #define TARGET_CUR 2UL
1420 static int xennet_proc_read(
1421 char *page, char **start, off_t off, int count, int *eof, void *data)
1423 struct net_device *dev = (struct net_device *)((unsigned long)data & ~3UL);
1424 struct net_private *np = netdev_priv(dev);
1425 int len = 0, which_target = (long)data & 3;
1427 switch (which_target)
1429 case TARGET_MIN:
1430 len = sprintf(page, "%d\n", np->rx_min_target);
1431 break;
1432 case TARGET_MAX:
1433 len = sprintf(page, "%d\n", np->rx_max_target);
1434 break;
1435 case TARGET_CUR:
1436 len = sprintf(page, "%d\n", np->rx_target);
1437 break;
1440 *eof = 1;
1441 return len;
1444 static int xennet_proc_write(
1445 struct file *file, const char __user *buffer,
1446 unsigned long count, void *data)
1448 struct net_device *dev = (struct net_device *)((unsigned long)data & ~3UL);
1449 struct net_private *np = netdev_priv(dev);
1450 int which_target = (long)data & 3;
1451 char string[64];
1452 long target;
1454 if (!capable(CAP_SYS_ADMIN))
1455 return -EPERM;
1457 if (count <= 1)
1458 return -EBADMSG; /* runt */
1459 if (count > sizeof(string))
1460 return -EFBIG; /* too long */
1462 if (copy_from_user(string, buffer, count))
1463 return -EFAULT;
1464 string[sizeof(string)-1] = '\0';
1466 target = simple_strtol(string, NULL, 10);
1467 if (target < RX_MIN_TARGET)
1468 target = RX_MIN_TARGET;
1469 if (target > RX_MAX_TARGET)
1470 target = RX_MAX_TARGET;
1472 spin_lock(&np->rx_lock);
1474 switch (which_target)
1476 case TARGET_MIN:
1477 if (target > np->rx_max_target)
1478 np->rx_max_target = target;
1479 np->rx_min_target = target;
1480 if (target > np->rx_target)
1481 np->rx_target = target;
1482 break;
1483 case TARGET_MAX:
1484 if (target < np->rx_min_target)
1485 np->rx_min_target = target;
1486 np->rx_max_target = target;
1487 if (target < np->rx_target)
1488 np->rx_target = target;
1489 break;
1490 case TARGET_CUR:
1491 break;
1494 network_alloc_rx_buffers(dev);
1496 spin_unlock(&np->rx_lock);
1498 return count;
1501 static int xennet_proc_init(void)
1503 if (proc_mkdir("xen/net", NULL) == NULL)
1504 return -ENOMEM;
1505 return 0;
1508 static int xennet_proc_addif(struct net_device *dev)
1510 struct proc_dir_entry *dir, *min, *max, *cur;
1511 char name[30];
1513 sprintf(name, "xen/net/%s", dev->name);
1515 dir = proc_mkdir(name, NULL);
1516 if (!dir)
1517 goto nomem;
1519 min = create_proc_entry("rxbuf_min", 0644, dir);
1520 max = create_proc_entry("rxbuf_max", 0644, dir);
1521 cur = create_proc_entry("rxbuf_cur", 0444, dir);
1522 if (!min || !max || !cur)
1523 goto nomem;
1525 min->read_proc = xennet_proc_read;
1526 min->write_proc = xennet_proc_write;
1527 min->data = (void *)((unsigned long)dev | TARGET_MIN);
1529 max->read_proc = xennet_proc_read;
1530 max->write_proc = xennet_proc_write;
1531 max->data = (void *)((unsigned long)dev | TARGET_MAX);
1533 cur->read_proc = xennet_proc_read;
1534 cur->write_proc = xennet_proc_write;
1535 cur->data = (void *)((unsigned long)dev | TARGET_CUR);
1537 return 0;
1539 nomem:
1540 xennet_proc_delif(dev);
1541 return -ENOMEM;
1544 static void xennet_proc_delif(struct net_device *dev)
1546 char name[30];
1548 sprintf(name, "xen/net/%s/rxbuf_min", dev->name);
1549 remove_proc_entry(name, NULL);
1551 sprintf(name, "xen/net/%s/rxbuf_max", dev->name);
1552 remove_proc_entry(name, NULL);
1554 sprintf(name, "xen/net/%s/rxbuf_cur", dev->name);
1555 remove_proc_entry(name, NULL);
1557 sprintf(name, "xen/net/%s", dev->name);
1558 remove_proc_entry(name, NULL);
1561 #endif
1563 module_init(netif_init);
1564 module_exit(netif_exit);