ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 6432:b54144915ae6

merge?
author cl349@firebug.cl.cam.ac.uk
date Thu Aug 25 16:26:30 2005 +0000 (2005-08-25)
parents 4f73a7bde7b1 56a2232377f1
children 0610add7c3fe
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/balloon.h>
54 #include <asm/page.h>
55 #include <asm/uaccess.h>
57 #if defined(CONFIG_XEN_NETDEV_GRANT_TX) || defined(CONFIG_XEN_NETDEV_GRANT_RX)
58 #include <asm-xen/xen-public/grant_table.h>
59 #include <asm-xen/gnttab.h>
60 #ifdef GRANT_DEBUG
61 static void
62 dump_packet(int tag, void *addr, u32 ap)
63 {
64 unsigned char *p = (unsigned char *)ap;
65 int i;
67 printk(KERN_ALERT "#### rx_poll %c %08x ", tag & 0xff, addr);
68 for (i = 0; i < 20; i++) {
69 printk("%02x", p[i]);
70 }
71 printk("\n");
72 }
73 #endif
74 #endif
76 #ifndef __GFP_NOWARN
77 #define __GFP_NOWARN 0
78 #endif
79 #define alloc_xen_skb(_l) __dev_alloc_skb((_l), GFP_ATOMIC|__GFP_NOWARN)
81 #define init_skb_shinfo(_skb) \
82 do { \
83 atomic_set(&(skb_shinfo(_skb)->dataref), 1); \
84 skb_shinfo(_skb)->nr_frags = 0; \
85 skb_shinfo(_skb)->frag_list = NULL; \
86 } while (0)
88 /* Allow headroom on each rx pkt for Ethernet header, alignment padding, ... */
89 #define RX_HEADROOM 200
91 /*
92 * If the backend driver is pipelining transmit requests then we can be very
93 * aggressive in avoiding new-packet notifications -- only need to send a
94 * notification if there are no outstanding unreceived responses.
95 * If the backend may be buffering our transmit buffers for any reason then we
96 * are rather more conservative.
97 */
98 #ifdef CONFIG_XEN_NETDEV_FRONTEND_PIPELINED_TRANSMITTER
99 #define TX_TEST_IDX resp_prod /* aggressive: any outstanding responses? */
100 #else
101 #define TX_TEST_IDX req_cons /* conservative: not seen all our requests? */
102 #endif
104 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
105 static grant_ref_t gref_tx_head;
106 static grant_ref_t grant_tx_ref[NETIF_TX_RING_SIZE + 1];
107 #endif
109 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
110 static grant_ref_t gref_rx_head;
111 static grant_ref_t grant_rx_ref[NETIF_RX_RING_SIZE + 1];
112 #endif
114 #if defined(CONFIG_XEN_NETDEV_GRANT_TX) || defined(CONFIG_XEN_NETDEV_GRANT_RX)
115 #define GRANT_INVALID_REF (0xFFFF)
116 #endif
118 #define NETIF_STATE_DISCONNECTED 0
119 #define NETIF_STATE_CONNECTED 1
121 static unsigned int netif_state = NETIF_STATE_DISCONNECTED;
123 static void network_tx_buf_gc(struct net_device *dev);
124 static void network_alloc_rx_buffers(struct net_device *dev);
126 static unsigned long rx_pfn_array[NETIF_RX_RING_SIZE];
127 static multicall_entry_t rx_mcl[NETIF_RX_RING_SIZE+1];
128 static mmu_update_t rx_mmu[NETIF_RX_RING_SIZE];
130 #ifdef CONFIG_PROC_FS
131 static int xennet_proc_init(void);
132 static int xennet_proc_addif(struct net_device *dev);
133 static void xennet_proc_delif(struct net_device *dev);
134 #else
135 #define xennet_proc_init() (0)
136 #define xennet_proc_addif(d) (0)
137 #define xennet_proc_delif(d) ((void)0)
138 #endif
140 #define netfront_info net_private
141 struct net_private
142 {
143 struct list_head list;
144 struct net_device *netdev;
146 struct net_device_stats stats;
147 NETIF_RING_IDX rx_resp_cons, tx_resp_cons;
148 unsigned int tx_full;
150 netif_tx_interface_t *tx;
151 netif_rx_interface_t *rx;
153 spinlock_t tx_lock;
154 spinlock_t rx_lock;
156 unsigned int handle;
157 unsigned int evtchn;
159 /* What is the status of our connection to the remote backend? */
160 #define BEST_CLOSED 0
161 #define BEST_DISCONNECTED 1
162 #define BEST_CONNECTED 2
163 unsigned int backend_state;
165 /* Is this interface open or closed (down or up)? */
166 #define UST_CLOSED 0
167 #define UST_OPEN 1
168 unsigned int user_state;
170 /* Receive-ring batched refills. */
171 #define RX_MIN_TARGET 8
172 #define RX_MAX_TARGET NETIF_RX_RING_SIZE
173 int rx_min_target, rx_max_target, rx_target;
174 struct sk_buff_head rx_batch;
176 /*
177 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
178 * array is an index into a chain of free entries.
179 */
180 struct sk_buff *tx_skbs[NETIF_TX_RING_SIZE+1];
181 struct sk_buff *rx_skbs[NETIF_RX_RING_SIZE+1];
183 struct xenbus_device *xbdev;
184 char *backend;
185 int backend_id;
186 struct xenbus_watch watch;
187 int tx_ring_ref;
188 int rx_ring_ref;
189 u8 mac[ETH_ALEN];
190 };
192 /* Access macros for acquiring freeing slots in {tx,rx}_skbs[]. */
193 #define ADD_ID_TO_FREELIST(_list, _id) \
194 (_list)[(_id)] = (_list)[0]; \
195 (_list)[0] = (void *)(unsigned long)(_id);
196 #define GET_ID_FROM_FREELIST(_list) \
197 ({ unsigned long _id = (unsigned long)(_list)[0]; \
198 (_list)[0] = (_list)[_id]; \
199 (unsigned short)_id; })
201 #ifdef DEBUG
202 static char *be_state_name[] = {
203 [BEST_CLOSED] = "closed",
204 [BEST_DISCONNECTED] = "disconnected",
205 [BEST_CONNECTED] = "connected",
206 };
207 #endif
209 #ifdef DEBUG
210 #define DPRINTK(fmt, args...) \
211 printk(KERN_ALERT "xen_net (%s:%d) " fmt, __FUNCTION__, __LINE__, ##args)
212 #else
213 #define DPRINTK(fmt, args...) ((void)0)
214 #endif
215 #define IPRINTK(fmt, args...) \
216 printk(KERN_INFO "xen_net: " fmt, ##args)
217 #define WPRINTK(fmt, args...) \
218 printk(KERN_WARNING "xen_net: " fmt, ##args)
220 /** Send a packet on a net device to encourage switches to learn the
221 * MAC. We send a fake ARP request.
222 *
223 * @param dev device
224 * @return 0 on success, error code otherwise
225 */
226 static int send_fake_arp(struct net_device *dev)
227 {
228 struct sk_buff *skb;
229 u32 src_ip, dst_ip;
231 dst_ip = INADDR_BROADCAST;
232 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
234 /* No IP? Then nothing to do. */
235 if (src_ip == 0)
236 return 0;
238 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
239 dst_ip, dev, src_ip,
240 /*dst_hw*/ NULL, /*src_hw*/ NULL,
241 /*target_hw*/ dev->dev_addr);
242 if (skb == NULL)
243 return -ENOMEM;
245 return dev_queue_xmit(skb);
246 }
248 static int network_open(struct net_device *dev)
249 {
250 struct net_private *np = netdev_priv(dev);
252 memset(&np->stats, 0, sizeof(np->stats));
254 np->user_state = UST_OPEN;
256 network_alloc_rx_buffers(dev);
257 np->rx->event = np->rx_resp_cons + 1;
259 netif_start_queue(dev);
261 return 0;
262 }
264 static void network_tx_buf_gc(struct net_device *dev)
265 {
266 NETIF_RING_IDX i, prod;
267 unsigned short id;
268 struct net_private *np = netdev_priv(dev);
269 struct sk_buff *skb;
271 if (np->backend_state != BEST_CONNECTED)
272 return;
274 do {
275 prod = np->tx->resp_prod;
276 rmb(); /* Ensure we see responses up to 'rp'. */
278 for (i = np->tx_resp_cons; i != prod; i++) {
279 id = np->tx->ring[MASK_NETIF_TX_IDX(i)].resp.id;
280 skb = np->tx_skbs[id];
281 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
282 if (unlikely(gnttab_query_foreign_access(grant_tx_ref[id]) != 0)) {
283 /* other domain is still using this grant - shouldn't happen
284 but if it does, we'll try to reclaim the grant later */
285 printk(KERN_ALERT "network_tx_buf_gc: warning -- grant "
286 "still in use by backend domain.\n");
287 goto out;
288 }
289 gnttab_end_foreign_access_ref(grant_tx_ref[id], GNTMAP_readonly);
290 gnttab_release_grant_reference(&gref_tx_head, grant_tx_ref[id]);
291 grant_tx_ref[id] = GRANT_INVALID_REF;
292 #endif
293 ADD_ID_TO_FREELIST(np->tx_skbs, id);
294 dev_kfree_skb_irq(skb);
295 }
297 np->tx_resp_cons = prod;
299 /*
300 * Set a new event, then check for race with update of tx_cons. Note
301 * that it is essential to schedule a callback, no matter how few
302 * buffers are pending. Even if there is space in the transmit ring,
303 * higher layers may be blocked because too much data is outstanding:
304 * in such cases notification from Xen is likely to be the only kick
305 * that we'll get.
306 */
307 np->tx->event =
308 prod + ((np->tx->req_prod - prod) >> 1) + 1;
309 mb();
310 } while (prod != np->tx->resp_prod);
312 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
313 out:
314 #endif
316 if (np->tx_full && ((np->tx->req_prod - prod) < NETIF_TX_RING_SIZE)) {
317 np->tx_full = 0;
318 if (np->user_state == UST_OPEN)
319 netif_wake_queue(dev);
320 }
321 }
324 static void network_alloc_rx_buffers(struct net_device *dev)
325 {
326 unsigned short id;
327 struct net_private *np = netdev_priv(dev);
328 struct sk_buff *skb;
329 int i, batch_target;
330 NETIF_RING_IDX req_prod = np->rx->req_prod;
331 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
332 int ref;
333 #endif
335 if (unlikely(np->backend_state != BEST_CONNECTED))
336 return;
338 /*
339 * Allocate skbuffs greedily, even though we batch updates to the
340 * receive ring. This creates a less bursty demand on the memory allocator,
341 * so should reduce the chance of failed allocation requests both for
342 * ourself and for other kernel subsystems.
343 */
344 batch_target = np->rx_target - (req_prod - np->rx_resp_cons);
345 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
346 if (unlikely((skb = alloc_xen_skb(dev->mtu + RX_HEADROOM)) == NULL))
347 break;
348 __skb_queue_tail(&np->rx_batch, skb);
349 }
351 /* Is the batch large enough to be worthwhile? */
352 if (i < (np->rx_target/2))
353 return;
355 for (i = 0; ; i++) {
356 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
357 break;
359 skb->dev = dev;
361 id = GET_ID_FROM_FREELIST(np->rx_skbs);
363 np->rx_skbs[id] = skb;
365 np->rx->ring[MASK_NETIF_RX_IDX(req_prod + i)].req.id = id;
366 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
367 ref = gnttab_claim_grant_reference(&gref_rx_head);
368 if (unlikely(ref < 0)) {
369 printk(KERN_ALERT "#### netfront can't claim rx reference\n");
370 BUG();
371 }
372 grant_rx_ref[id] = ref;
373 gnttab_grant_foreign_transfer_ref(ref, np->backend_id,
374 virt_to_mfn(skb->head));
375 np->rx->ring[MASK_NETIF_RX_IDX(req_prod + i)].req.gref = ref;
376 #endif
377 rx_pfn_array[i] = virt_to_mfn(skb->head);
379 /* Remove this page from pseudo phys map before passing back to Xen. */
380 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT]
381 = INVALID_P2M_ENTRY;
383 MULTI_update_va_mapping(rx_mcl+i, (unsigned long)skb->head,
384 __pte(0), 0);
385 }
387 /* After all PTEs have been zapped we blow away stale TLB entries. */
388 rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] = UVMF_TLB_FLUSH|UVMF_ALL;
390 /* Give away a batch of pages. */
391 rx_mcl[i].op = __HYPERVISOR_dom_mem_op;
392 rx_mcl[i].args[0] = MEMOP_decrease_reservation;
393 rx_mcl[i].args[1] = (unsigned long)rx_pfn_array;
394 rx_mcl[i].args[2] = (unsigned long)i;
395 rx_mcl[i].args[3] = 0;
396 rx_mcl[i].args[4] = DOMID_SELF;
398 /* Tell the ballon driver what is going on. */
399 balloon_update_driver_allowance(i);
401 /* Zap PTEs and give away pages in one big multicall. */
402 (void)HYPERVISOR_multicall(rx_mcl, i+1);
404 /* Check return status of HYPERVISOR_dom_mem_op(). */
405 if (unlikely(rx_mcl[i].result != i))
406 panic("Unable to reduce memory reservation\n");
408 /* Above is a suitable barrier to ensure backend will see requests. */
409 np->rx->req_prod = req_prod + i;
411 /* Adjust our floating fill target if we risked running out of buffers. */
412 if (((req_prod - np->rx->resp_prod) < (np->rx_target / 4)) &&
413 ((np->rx_target *= 2) > np->rx_max_target))
414 np->rx_target = np->rx_max_target;
415 }
418 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
419 {
420 unsigned short id;
421 struct net_private *np = netdev_priv(dev);
422 netif_tx_request_t *tx;
423 NETIF_RING_IDX i;
424 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
425 unsigned int ref;
426 unsigned long mfn;
427 #endif
429 if (unlikely(np->tx_full)) {
430 printk(KERN_ALERT "%s: full queue wasn't stopped!\n", dev->name);
431 netif_stop_queue(dev);
432 goto drop;
433 }
435 if (unlikely((((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >=
436 PAGE_SIZE)) {
437 struct sk_buff *nskb;
438 if (unlikely((nskb = alloc_xen_skb(skb->len)) == NULL))
439 goto drop;
440 skb_put(nskb, skb->len);
441 memcpy(nskb->data, skb->data, skb->len);
442 nskb->dev = skb->dev;
443 dev_kfree_skb(skb);
444 skb = nskb;
445 }
447 spin_lock_irq(&np->tx_lock);
449 if (np->backend_state != BEST_CONNECTED) {
450 spin_unlock_irq(&np->tx_lock);
451 goto drop;
452 }
454 i = np->tx->req_prod;
456 id = GET_ID_FROM_FREELIST(np->tx_skbs);
457 np->tx_skbs[id] = skb;
459 tx = &np->tx->ring[MASK_NETIF_TX_IDX(i)].req;
461 tx->id = id;
462 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
463 ref = gnttab_claim_grant_reference(&gref_tx_head);
464 if (unlikely(ref < 0)) {
465 printk(KERN_ALERT "#### netfront can't claim tx grant reference\n");
466 BUG();
467 }
468 mfn = virt_to_mfn(skb->data);
469 gnttab_grant_foreign_access_ref(ref, np->backend_id, mfn, GNTMAP_readonly);
470 tx->addr = ref << PAGE_SHIFT;
471 grant_tx_ref[id] = ref;
472 #else
473 tx->addr = virt_to_mfn(skb->data) << PAGE_SHIFT;
474 #endif
475 tx->addr |= (unsigned long)skb->data & ~PAGE_MASK;
476 tx->size = skb->len;
477 tx->csum_blank = (skb->ip_summed == CHECKSUM_HW);
479 wmb(); /* Ensure that backend will see the request. */
480 np->tx->req_prod = i + 1;
482 network_tx_buf_gc(dev);
484 if ((i - np->tx_resp_cons) == (NETIF_TX_RING_SIZE - 1)) {
485 np->tx_full = 1;
486 netif_stop_queue(dev);
487 }
489 spin_unlock_irq(&np->tx_lock);
491 np->stats.tx_bytes += skb->len;
492 np->stats.tx_packets++;
494 /* Only notify Xen if we really have to. */
495 mb();
496 if (np->tx->TX_TEST_IDX == i)
497 notify_via_evtchn(np->evtchn);
499 return 0;
501 drop:
502 np->stats.tx_dropped++;
503 dev_kfree_skb(skb);
504 return 0;
505 }
507 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
508 {
509 struct net_device *dev = dev_id;
510 struct net_private *np = netdev_priv(dev);
511 unsigned long flags;
513 spin_lock_irqsave(&np->tx_lock, flags);
514 network_tx_buf_gc(dev);
515 spin_unlock_irqrestore(&np->tx_lock, flags);
517 if ((np->rx_resp_cons != np->rx->resp_prod) && (np->user_state == UST_OPEN))
518 netif_rx_schedule(dev);
520 return IRQ_HANDLED;
521 }
524 static int netif_poll(struct net_device *dev, int *pbudget)
525 {
526 struct net_private *np = netdev_priv(dev);
527 struct sk_buff *skb, *nskb;
528 netif_rx_response_t *rx;
529 NETIF_RING_IDX i, rp;
530 mmu_update_t *mmu = rx_mmu;
531 multicall_entry_t *mcl = rx_mcl;
532 int work_done, budget, more_to_do = 1;
533 struct sk_buff_head rxq;
534 unsigned long flags;
535 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
536 unsigned long mfn;
537 grant_ref_t ref;
538 #endif
540 spin_lock(&np->rx_lock);
542 if (np->backend_state != BEST_CONNECTED) {
543 spin_unlock(&np->rx_lock);
544 return 0;
545 }
547 skb_queue_head_init(&rxq);
549 if ((budget = *pbudget) > dev->quota)
550 budget = dev->quota;
551 rp = np->rx->resp_prod;
552 rmb(); /* Ensure we see queued responses up to 'rp'. */
554 for (i = np->rx_resp_cons, work_done = 0;
555 (i != rp) && (work_done < budget);
556 i++, work_done++) {
557 rx = &np->rx->ring[MASK_NETIF_RX_IDX(i)].resp;
558 /*
559 * An error here is very odd. Usually indicates a backend bug,
560 * low-memory condition, or that we didn't have reservation headroom.
561 */
562 if (unlikely(rx->status <= 0)) {
563 if (net_ratelimit())
564 printk(KERN_WARNING "Bad rx buffer (memory squeeze?).\n");
565 np->rx->ring[MASK_NETIF_RX_IDX(np->rx->req_prod)].req.id = rx->id;
566 wmb();
567 np->rx->req_prod++;
568 work_done--;
569 continue;
570 }
572 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
573 ref = grant_rx_ref[rx->id];
574 grant_rx_ref[rx->id] = GRANT_INVALID_REF;
575 mfn = gnttab_end_foreign_transfer_ref(ref);
576 gnttab_release_grant_reference(&gref_rx_head, ref);
577 #endif
579 skb = np->rx_skbs[rx->id];
580 ADD_ID_TO_FREELIST(np->rx_skbs, rx->id);
582 /* NB. We handle skb overflow later. */
583 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
584 skb->data = skb->head + rx->addr;
585 #else
586 skb->data = skb->head + (rx->addr & ~PAGE_MASK);
587 #endif
588 skb->len = rx->status;
589 skb->tail = skb->data + skb->len;
591 if ( rx->csum_valid )
592 skb->ip_summed = CHECKSUM_UNNECESSARY;
594 np->stats.rx_packets++;
595 np->stats.rx_bytes += rx->status;
597 /* Remap the page. */
598 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
599 mmu->ptr = mfn << PAGE_SHIFT | MMU_MACHPHYS_UPDATE;
600 #else
601 mmu->ptr = (rx->addr & PAGE_MASK) | MMU_MACHPHYS_UPDATE;
602 #endif
603 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
604 mmu++;
605 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
606 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
607 pfn_pte_ma(mfn, PAGE_KERNEL), 0);
608 #else
609 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
610 pfn_pte_ma(rx->addr >> PAGE_SHIFT,
611 PAGE_KERNEL), 0);
612 #endif
613 mcl++;
615 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
616 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] = mfn;
617 #else
618 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] =
619 rx->addr >> PAGE_SHIFT;
620 #endif
622 #ifdef GRANT_DEBUG
623 printk(KERN_ALERT "#### rx_poll enqueue vdata=%p mfn=%lu ref=%x\n",
624 skb->data, mfn, ref);
625 #endif
626 __skb_queue_tail(&rxq, skb);
627 }
629 /* Some pages are no longer absent... */
630 balloon_update_driver_allowance(-work_done);
632 /* Do all the remapping work, and M->P updates, in one big hypercall. */
633 if (likely((mcl - rx_mcl) != 0)) {
634 mcl->op = __HYPERVISOR_mmu_update;
635 mcl->args[0] = (unsigned long)rx_mmu;
636 mcl->args[1] = mmu - rx_mmu;
637 mcl->args[2] = 0;
638 mcl->args[3] = DOMID_SELF;
639 mcl++;
640 (void)HYPERVISOR_multicall(rx_mcl, mcl - rx_mcl);
641 }
643 while ((skb = __skb_dequeue(&rxq)) != NULL) {
644 #ifdef GRANT_DEBUG
645 printk(KERN_ALERT "#### rx_poll dequeue vdata=%p mfn=%lu\n",
646 skb->data, virt_to_mfn(skb->data));
647 dump_packet('d', skb->data, (unsigned long)skb->data);
648 #endif
649 /*
650 * Enough room in skbuff for the data we were passed? Also, Linux
651 * expects at least 16 bytes headroom in each receive buffer.
652 */
653 if (unlikely(skb->tail > skb->end) ||
654 unlikely((skb->data - skb->head) < 16)) {
655 nskb = NULL;
658 /* Only copy the packet if it fits in the current MTU. */
659 if (skb->len <= (dev->mtu + ETH_HLEN)) {
660 if ((skb->tail > skb->end) && net_ratelimit())
661 printk(KERN_INFO "Received packet needs %zd bytes more "
662 "headroom.\n", skb->tail - skb->end);
664 if ((nskb = alloc_xen_skb(skb->len + 2)) != NULL) {
665 skb_reserve(nskb, 2);
666 skb_put(nskb, skb->len);
667 memcpy(nskb->data, skb->data, skb->len);
668 nskb->dev = skb->dev;
669 }
670 }
671 else if (net_ratelimit())
672 printk(KERN_INFO "Received packet too big for MTU "
673 "(%d > %d)\n", skb->len - ETH_HLEN, dev->mtu);
675 /* Reinitialise and then destroy the old skbuff. */
676 skb->len = 0;
677 skb->tail = skb->data;
678 init_skb_shinfo(skb);
679 dev_kfree_skb(skb);
681 /* Switch old for new, if we copied the buffer. */
682 if ((skb = nskb) == NULL)
683 continue;
684 }
686 /* Set the shared-info area, which is hidden behind the real data. */
687 init_skb_shinfo(skb);
688 /* Ethernet-specific work. Delayed to here as it peeks the header. */
689 skb->protocol = eth_type_trans(skb, dev);
691 /* Pass it up. */
692 netif_receive_skb(skb);
693 dev->last_rx = jiffies;
694 }
696 np->rx_resp_cons = i;
698 /* If we get a callback with very few responses, reduce fill target. */
699 /* NB. Note exponential increase, linear decrease. */
700 if (((np->rx->req_prod - np->rx->resp_prod) > ((3*np->rx_target) / 4)) &&
701 (--np->rx_target < np->rx_min_target))
702 np->rx_target = np->rx_min_target;
704 network_alloc_rx_buffers(dev);
706 *pbudget -= work_done;
707 dev->quota -= work_done;
709 if (work_done < budget) {
710 local_irq_save(flags);
712 np->rx->event = i + 1;
714 /* Deal with hypervisor racing our resetting of rx_event. */
715 mb();
716 if (np->rx->resp_prod == i) {
717 __netif_rx_complete(dev);
718 more_to_do = 0;
719 }
721 local_irq_restore(flags);
722 }
724 spin_unlock(&np->rx_lock);
726 return more_to_do;
727 }
730 static int network_close(struct net_device *dev)
731 {
732 struct net_private *np = netdev_priv(dev);
733 np->user_state = UST_CLOSED;
734 netif_stop_queue(np->netdev);
735 return 0;
736 }
739 static struct net_device_stats *network_get_stats(struct net_device *dev)
740 {
741 struct net_private *np = netdev_priv(dev);
742 return &np->stats;
743 }
746 static void network_connect(struct net_device *dev)
747 {
748 struct net_private *np;
749 int i, requeue_idx;
750 netif_tx_request_t *tx;
752 np = netdev_priv(dev);
753 spin_lock_irq(&np->tx_lock);
754 spin_lock(&np->rx_lock);
756 /* Recovery procedure: */
758 /* Step 1: Reinitialise variables. */
759 np->rx_resp_cons = np->tx_resp_cons = np->tx_full = 0;
760 np->rx->event = np->tx->event = 1;
762 /* Step 2: Rebuild the RX and TX ring contents.
763 * NB. We could just free the queued TX packets now but we hope
764 * that sending them out might do some good. We have to rebuild
765 * the RX ring because some of our pages are currently flipped out
766 * so we can't just free the RX skbs.
767 * NB2. Freelist index entries are always going to be less than
768 * __PAGE_OFFSET, whereas pointers to skbs will always be equal or
769 * greater than __PAGE_OFFSET: we use this property to distinguish
770 * them.
771 */
773 /* Rebuild the TX buffer freelist and the TX ring itself.
774 * NB. This reorders packets. We could keep more private state
775 * to avoid this but maybe it doesn't matter so much given the
776 * interface has been down.
777 */
778 for (requeue_idx = 0, i = 1; i <= NETIF_TX_RING_SIZE; i++) {
779 if ((unsigned long)np->tx_skbs[i] >= __PAGE_OFFSET) {
780 struct sk_buff *skb = np->tx_skbs[i];
782 tx = &np->tx->ring[requeue_idx++].req;
784 tx->id = i;
785 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
786 tx->addr = 0; /*(ref << PAGE_SHIFT) |*/
787 #else
788 tx->addr = virt_to_mfn(skb->data) << PAGE_SHIFT;
789 #endif
790 tx->addr |= (unsigned long)skb->data & ~PAGE_MASK;
791 tx->size = skb->len;
793 np->stats.tx_bytes += skb->len;
794 np->stats.tx_packets++;
795 }
796 }
797 wmb();
798 np->tx->req_prod = requeue_idx;
800 /* Rebuild the RX buffer freelist and the RX ring itself. */
801 for (requeue_idx = 0, i = 1; i <= NETIF_RX_RING_SIZE; i++)
802 if ((unsigned long)np->rx_skbs[i] >= __PAGE_OFFSET)
803 np->rx->ring[requeue_idx++].req.id = i;
804 wmb();
805 np->rx->req_prod = requeue_idx;
807 /* Step 3: All public and private state should now be sane. Get
808 * ready to start sending and receiving packets and give the driver
809 * domain a kick because we've probably just requeued some
810 * packets.
811 */
812 np->backend_state = BEST_CONNECTED;
813 wmb();
814 notify_via_evtchn(np->evtchn);
815 network_tx_buf_gc(dev);
817 if (np->user_state == UST_OPEN)
818 netif_start_queue(dev);
820 spin_unlock(&np->rx_lock);
821 spin_unlock_irq(&np->tx_lock);
822 }
824 static void show_device(struct net_private *np)
825 {
826 #ifdef DEBUG
827 if (np) {
828 IPRINTK("<vif handle=%u %s(%s) evtchn=%u tx=%p rx=%p>\n",
829 np->handle,
830 be_state_name[np->backend_state],
831 np->user_state ? "open" : "closed",
832 np->evtchn,
833 np->tx,
834 np->rx);
835 } else {
836 IPRINTK("<vif NULL>\n");
837 }
838 #endif
839 }
841 /* Move the vif into connected state.
842 * Sets the mac and event channel from the message.
843 * Binds the irq to the event channel.
844 */
845 static void
846 connect_device(struct net_private *np, unsigned int evtchn)
847 {
848 struct net_device *dev = np->netdev;
849 memcpy(dev->dev_addr, np->mac, ETH_ALEN);
850 np->evtchn = evtchn;
851 network_connect(dev);
852 (void)bind_evtchn_to_irqhandler(
853 np->evtchn, netif_int, SA_SAMPLE_RANDOM, dev->name, dev);
854 (void)send_fake_arp(dev);
855 show_device(np);
856 }
858 static struct ethtool_ops network_ethtool_ops =
859 {
860 .get_tx_csum = ethtool_op_get_tx_csum,
861 .set_tx_csum = ethtool_op_set_tx_csum,
862 };
864 /** Create a network device.
865 * @param handle device handle
866 * @param val return parameter for created device
867 * @return 0 on success, error code otherwise
868 */
869 static int create_netdev(int handle, struct xenbus_device *dev,
870 struct net_device **val)
871 {
872 int i, err = 0;
873 struct net_device *netdev = NULL;
874 struct net_private *np = NULL;
876 if ((netdev = alloc_etherdev(sizeof(struct net_private))) == NULL) {
877 printk(KERN_WARNING "%s> alloc_etherdev failed.\n", __FUNCTION__);
878 err = -ENOMEM;
879 goto exit;
880 }
882 np = netdev_priv(netdev);
883 np->backend_state = BEST_CLOSED;
884 np->user_state = UST_CLOSED;
885 np->handle = handle;
886 np->xbdev = dev;
888 spin_lock_init(&np->tx_lock);
889 spin_lock_init(&np->rx_lock);
891 skb_queue_head_init(&np->rx_batch);
892 np->rx_target = RX_MIN_TARGET;
893 np->rx_min_target = RX_MIN_TARGET;
894 np->rx_max_target = RX_MAX_TARGET;
896 /* Initialise {tx,rx}_skbs to be a free chain containing every entry. */
897 for (i = 0; i <= NETIF_TX_RING_SIZE; i++) {
898 np->tx_skbs[i] = (void *)((unsigned long) i+1);
899 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
900 grant_tx_ref[i] = GRANT_INVALID_REF;
901 #endif
902 }
903 for (i = 0; i <= NETIF_RX_RING_SIZE; i++) {
904 np->rx_skbs[i] = (void *)((unsigned long) i+1);
905 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
906 grant_rx_ref[i] = GRANT_INVALID_REF;
907 #endif
908 }
910 netdev->open = network_open;
911 netdev->hard_start_xmit = network_start_xmit;
912 netdev->stop = network_close;
913 netdev->get_stats = network_get_stats;
914 netdev->poll = netif_poll;
915 netdev->weight = 64;
916 netdev->features = NETIF_F_IP_CSUM;
918 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
920 if ((err = register_netdev(netdev)) != 0) {
921 printk(KERN_WARNING "%s> register_netdev err=%d\n", __FUNCTION__, err);
922 goto exit;
923 }
925 if ((err = xennet_proc_addif(netdev)) != 0) {
926 unregister_netdev(netdev);
927 goto exit;
928 }
930 np->netdev = netdev;
932 exit:
933 if ((err != 0) && (netdev != NULL))
934 kfree(netdev);
935 else if (val != NULL)
936 *val = netdev;
937 return err;
938 }
940 static int destroy_netdev(struct net_device *netdev)
941 {
942 struct net_private *np = NULL;
944 #ifdef CONFIG_PROC_FS
945 xennet_proc_delif(netdev);
946 #endif
948 unregister_netdev(netdev);
950 np = netdev_priv(netdev);
951 list_del(&np->list);
953 kfree(netdev);
955 return 0;
956 }
958 /*
959 * We use this notifier to send out a fake ARP reply to reset switches and
960 * router ARP caches when an IP interface is brought up on a VIF.
961 */
962 static int
963 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
964 {
965 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
966 struct net_device *dev = ifa->ifa_dev->dev;
968 /* UP event and is it one of our devices? */
969 if (event == NETDEV_UP && dev->open == network_open)
970 (void)send_fake_arp(dev);
972 return NOTIFY_DONE;
973 }
975 static struct notifier_block notifier_inetdev = {
976 .notifier_call = inetdev_notify,
977 .next = NULL,
978 .priority = 0
979 };
981 static struct xenbus_device_id netfront_ids[] = {
982 { "vif" },
983 { "" }
984 };
986 static void watch_for_status(struct xenbus_watch *watch, const char *node)
987 {
988 }
990 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
991 {
992 evtchn_op_t op = { .cmd = EVTCHNOP_alloc_unbound };
993 int err;
995 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
996 info->tx_ring_ref = GRANT_INVALID_REF;
997 #endif
998 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
999 info->rx_ring_ref = GRANT_INVALID_REF;
1000 #endif
1002 info->tx = (netif_tx_interface_t *)__get_free_page(GFP_KERNEL);
1003 if (info->tx == 0) {
1004 err = -ENOMEM;
1005 xenbus_dev_error(dev, err, "allocating tx ring page");
1006 goto out;
1008 info->rx = (netif_rx_interface_t *)__get_free_page(GFP_KERNEL);
1009 if (info->rx == 0) {
1010 err = -ENOMEM;
1011 xenbus_dev_error(dev, err, "allocating rx ring page");
1012 goto out;
1014 memset(info->tx, 0, PAGE_SIZE);
1015 memset(info->rx, 0, PAGE_SIZE);
1016 info->backend_state = BEST_DISCONNECTED;
1018 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
1019 err = gnttab_grant_foreign_access(info->backend_id,
1020 virt_to_mfn(info->tx), 0);
1021 if (err < 0) {
1022 xenbus_dev_error(dev, err, "granting access to tx ring page");
1023 goto out;
1025 info->tx_ring_ref = err;
1026 #else
1027 info->tx_ring_ref = virt_to_mfn(info->tx);
1028 #endif
1030 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
1031 err = gnttab_grant_foreign_access(info->backend_id,
1032 virt_to_mfn(info->rx), 0);
1033 if (err < 0) {
1034 xenbus_dev_error(dev, err, "granting access to rx ring page");
1035 goto out;
1037 info->rx_ring_ref = err;
1038 #else
1039 info->rx_ring_ref = virt_to_mfn(info->rx);
1040 #endif
1042 op.u.alloc_unbound.dom = info->backend_id;
1043 err = HYPERVISOR_event_channel_op(&op);
1044 if (err) {
1045 xenbus_dev_error(dev, err, "allocating event channel");
1046 goto out;
1048 connect_device(info, op.u.alloc_unbound.port);
1049 return 0;
1051 out:
1052 if (info->tx)
1053 free_page((unsigned long)info->tx);
1054 info->tx = 0;
1055 if (info->rx)
1056 free_page((unsigned long)info->rx);
1057 info->rx = 0;
1058 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
1059 if (info->tx_ring_ref != GRANT_INVALID_REF)
1060 gnttab_end_foreign_access(info->tx_ring_ref, 0);
1061 info->tx_ring_ref = GRANT_INVALID_REF;
1062 #endif
1063 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
1064 if (info->rx_ring_ref != GRANT_INVALID_REF)
1065 gnttab_end_foreign_access(info->rx_ring_ref, 0);
1066 info->rx_ring_ref = GRANT_INVALID_REF;
1067 #endif
1068 return err;
1071 static void netif_free(struct netfront_info *info)
1073 if (info->tx)
1074 free_page((unsigned long)info->tx);
1075 info->tx = 0;
1076 if (info->rx)
1077 free_page((unsigned long)info->rx);
1078 info->rx = 0;
1079 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
1080 if (info->tx_ring_ref != GRANT_INVALID_REF)
1081 gnttab_end_foreign_access(info->tx_ring_ref, 0);
1082 info->tx_ring_ref = GRANT_INVALID_REF;
1083 #endif
1084 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
1085 if (info->rx_ring_ref != GRANT_INVALID_REF)
1086 gnttab_end_foreign_access(info->rx_ring_ref, 0);
1087 info->rx_ring_ref = GRANT_INVALID_REF;
1088 #endif
1089 unbind_evtchn_from_irqhandler(info->evtchn, info->netdev);
1090 info->evtchn = 0;
1093 /* Stop network device and free tx/rx queues and irq.
1094 */
1095 static void shutdown_device(struct net_private *np)
1097 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1098 spin_lock_irq(&np->tx_lock);
1099 spin_lock(&np->rx_lock);
1100 netif_stop_queue(np->netdev);
1101 /* np->backend_state = BEST_DISCONNECTED; */
1102 spin_unlock(&np->rx_lock);
1103 spin_unlock_irq(&np->tx_lock);
1105 /* Free resources. */
1106 netif_free(np);
1109 /* Common code used when first setting up, and when resuming. */
1110 static int talk_to_backend(struct xenbus_device *dev,
1111 struct netfront_info *info)
1113 char *backend, *mac, *e, *s;
1114 const char *message;
1115 int err, i;
1117 backend = NULL;
1118 err = xenbus_gather(dev->nodename,
1119 "backend-id", "%i", &info->backend_id,
1120 "backend", NULL, &backend,
1121 NULL);
1122 if (XENBUS_EXIST_ERR(err))
1123 goto out;
1124 if (backend && strlen(backend) == 0) {
1125 err = -ENOENT;
1126 goto out;
1128 if (err < 0) {
1129 xenbus_dev_error(dev, err, "reading %s/backend or backend-id",
1130 dev->nodename);
1131 goto out;
1134 mac = xenbus_read(dev->nodename, "mac", NULL);
1135 if (IS_ERR(mac)) {
1136 err = PTR_ERR(mac);
1137 xenbus_dev_error(dev, err, "reading %s/mac",
1138 dev->nodename);
1139 goto out;
1141 s = mac;
1142 for (i = 0; i < ETH_ALEN; i++) {
1143 info->mac[i] = simple_strtoul(s, &e, 16);
1144 if (s == e || (e[0] != ':' && e[0] != 0)) {
1145 kfree(mac);
1146 err = -ENOENT;
1147 xenbus_dev_error(dev, err, "parsing %s/mac",
1148 dev->nodename);
1149 goto out;
1151 s = &e[1];
1153 kfree(mac);
1155 /* Create shared ring, alloc event channel. */
1156 err = setup_device(dev, info);
1157 if (err) {
1158 xenbus_dev_error(dev, err, "setting up ring");
1159 goto out;
1162 err = xenbus_transaction_start(dev->nodename);
1163 if (err) {
1164 xenbus_dev_error(dev, err, "starting transaction");
1165 goto destroy_ring;
1168 err = xenbus_printf(dev->nodename, "tx-ring-ref","%u",
1169 info->tx_ring_ref);
1170 if (err) {
1171 message = "writing tx ring-ref";
1172 goto abort_transaction;
1174 err = xenbus_printf(dev->nodename, "rx-ring-ref","%u",
1175 info->rx_ring_ref);
1176 if (err) {
1177 message = "writing rx ring-ref";
1178 goto abort_transaction;
1180 err = xenbus_printf(dev->nodename,
1181 "event-channel", "%u", info->evtchn);
1182 if (err) {
1183 message = "writing event-channel";
1184 goto abort_transaction;
1187 info->backend = backend;
1188 backend = NULL;
1190 info->watch.node = info->backend;
1191 info->watch.callback = watch_for_status;
1192 err = register_xenbus_watch(&info->watch);
1193 if (err) {
1194 message = "registering watch on backend";
1195 goto abort_transaction;
1198 err = xenbus_transaction_end(0);
1199 if (err) {
1200 xenbus_dev_error(dev, err, "completing transaction");
1201 goto destroy_ring;
1204 netif_state = NETIF_STATE_CONNECTED;
1206 out:
1207 if (backend)
1208 kfree(backend);
1209 return err;
1211 abort_transaction:
1212 xenbus_transaction_end(1);
1213 /* Have to do this *outside* transaction. */
1214 xenbus_dev_error(dev, err, "%s", message);
1215 destroy_ring:
1216 shutdown_device(info);
1217 goto out;
1220 /* Setup supplies the backend dir, virtual device.
1222 We place an event channel and shared frame entries.
1223 We watch backend to wait if it's ok. */
1224 static int netfront_probe(struct xenbus_device *dev,
1225 const struct xenbus_device_id *id)
1227 int err;
1228 struct net_device *netdev;
1229 struct netfront_info *info;
1230 unsigned int handle;
1232 err = xenbus_scanf(dev->nodename, "handle", "%u", &handle);
1233 if (XENBUS_EXIST_ERR(err))
1234 return err;
1235 if (err < 0) {
1236 xenbus_dev_error(dev, err, "reading handle");
1237 return err;
1240 err = create_netdev(handle, dev, &netdev);
1241 if (err) {
1242 xenbus_dev_error(dev, err, "creating netdev");
1243 return err;
1246 info = netdev_priv(netdev);
1247 err = talk_to_backend(dev, info);
1248 if (err) {
1249 destroy_netdev(netdev);
1250 return err;
1253 /* Call once in case entries already there. */
1254 watch_for_status(&info->watch, info->watch.node);
1256 return 0;
1259 static int netfront_remove(struct xenbus_device *dev)
1261 struct netfront_info *info = dev->data;
1263 if (info->backend)
1264 unregister_xenbus_watch(&info->watch);
1266 netif_free(info);
1268 kfree(info->backend);
1269 kfree(info);
1271 return 0;
1274 static int netfront_suspend(struct xenbus_device *dev)
1276 struct net_private *np = dev->data;
1277 /* Avoid having tx/rx stuff happen until we're ready. */
1278 unbind_evtchn_from_irqhandler(np->evtchn, np->netdev);
1279 return 0;
1282 static int netfront_resume(struct xenbus_device *dev)
1284 struct net_private *np = dev->data;
1285 /*
1286 * Connect regardless of whether IFF_UP flag set.
1287 * Stop bad things from happening until we're back up.
1288 */
1289 np->backend_state = BEST_DISCONNECTED;
1290 memset(np->tx, 0, PAGE_SIZE);
1291 memset(np->rx, 0, PAGE_SIZE);
1293 // send_interface_connect(np);
1294 return 0;
1297 static struct xenbus_driver netfront = {
1298 .name = "vif",
1299 .owner = THIS_MODULE,
1300 .ids = netfront_ids,
1301 .probe = netfront_probe,
1302 .remove = netfront_remove,
1303 .resume = netfront_resume,
1304 .suspend = netfront_suspend,
1305 };
1307 static void __init init_net_xenbus(void)
1309 xenbus_register_device(&netfront);
1312 static int wait_for_netif(void)
1314 int err = 0;
1315 int i;
1317 /*
1318 * We should figure out how many and which devices we need to
1319 * proceed and only wait for those. For now, continue once the
1320 * first device is around.
1321 */
1322 for ( i=0; netif_state != NETIF_STATE_CONNECTED && (i < 10*HZ); i++ )
1324 set_current_state(TASK_INTERRUPTIBLE);
1325 schedule_timeout(1);
1328 if (netif_state != NETIF_STATE_CONNECTED) {
1329 WPRINTK("Timeout connecting to device!\n");
1330 err = -ENOSYS;
1332 return err;
1335 static int __init netif_init(void)
1337 int err = 0;
1339 if (xen_start_info.flags & SIF_INITDOMAIN)
1340 return 0;
1342 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
1343 /* A grant for every ring slot */
1344 if (gnttab_alloc_grant_references(NETIF_TX_RING_SIZE,
1345 &gref_tx_head) < 0) {
1346 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1347 return 1;
1349 printk(KERN_ALERT "Netdev frontend (TX) is using grant tables.\n");
1350 #endif
1351 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
1352 /* A grant for every ring slot */
1353 if (gnttab_alloc_grant_references(NETIF_RX_RING_SIZE,
1354 &gref_rx_head) < 0) {
1355 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1356 return 1;
1358 printk(KERN_ALERT "Netdev frontend (RX) is using grant tables.\n");
1359 #endif
1361 if ((err = xennet_proc_init()) != 0)
1362 return err;
1364 IPRINTK("Initialising virtual ethernet driver.\n");
1366 (void)register_inetaddr_notifier(&notifier_inetdev);
1368 init_net_xenbus();
1370 wait_for_netif();
1372 return err;
1375 static void netif_exit(void)
1377 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
1378 gnttab_free_grant_references(gref_tx_head);
1379 #endif
1380 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
1381 gnttab_free_grant_references(gref_rx_head);
1382 #endif
1385 #ifdef CONFIG_PROC_FS
1387 #define TARGET_MIN 0UL
1388 #define TARGET_MAX 1UL
1389 #define TARGET_CUR 2UL
1391 static int xennet_proc_read(
1392 char *page, char **start, off_t off, int count, int *eof, void *data)
1394 struct net_device *dev = (struct net_device *)((unsigned long)data & ~3UL);
1395 struct net_private *np = netdev_priv(dev);
1396 int len = 0, which_target = (long)data & 3;
1398 switch (which_target)
1400 case TARGET_MIN:
1401 len = sprintf(page, "%d\n", np->rx_min_target);
1402 break;
1403 case TARGET_MAX:
1404 len = sprintf(page, "%d\n", np->rx_max_target);
1405 break;
1406 case TARGET_CUR:
1407 len = sprintf(page, "%d\n", np->rx_target);
1408 break;
1411 *eof = 1;
1412 return len;
1415 static int xennet_proc_write(
1416 struct file *file, const char __user *buffer,
1417 unsigned long count, void *data)
1419 struct net_device *dev = (struct net_device *)((unsigned long)data & ~3UL);
1420 struct net_private *np = netdev_priv(dev);
1421 int which_target = (long)data & 3;
1422 char string[64];
1423 long target;
1425 if (!capable(CAP_SYS_ADMIN))
1426 return -EPERM;
1428 if (count <= 1)
1429 return -EBADMSG; /* runt */
1430 if (count > sizeof(string))
1431 return -EFBIG; /* too long */
1433 if (copy_from_user(string, buffer, count))
1434 return -EFAULT;
1435 string[sizeof(string)-1] = '\0';
1437 target = simple_strtol(string, NULL, 10);
1438 if (target < RX_MIN_TARGET)
1439 target = RX_MIN_TARGET;
1440 if (target > RX_MAX_TARGET)
1441 target = RX_MAX_TARGET;
1443 spin_lock(&np->rx_lock);
1445 switch (which_target)
1447 case TARGET_MIN:
1448 if (target > np->rx_max_target)
1449 np->rx_max_target = target;
1450 np->rx_min_target = target;
1451 if (target > np->rx_target)
1452 np->rx_target = target;
1453 break;
1454 case TARGET_MAX:
1455 if (target < np->rx_min_target)
1456 np->rx_min_target = target;
1457 np->rx_max_target = target;
1458 if (target < np->rx_target)
1459 np->rx_target = target;
1460 break;
1461 case TARGET_CUR:
1462 break;
1465 network_alloc_rx_buffers(dev);
1467 spin_unlock(&np->rx_lock);
1469 return count;
1472 static int xennet_proc_init(void)
1474 if (proc_mkdir("xen/net", NULL) == NULL)
1475 return -ENOMEM;
1476 return 0;
1479 static int xennet_proc_addif(struct net_device *dev)
1481 struct proc_dir_entry *dir, *min, *max, *cur;
1482 char name[30];
1484 sprintf(name, "xen/net/%s", dev->name);
1486 dir = proc_mkdir(name, NULL);
1487 if (!dir)
1488 goto nomem;
1490 min = create_proc_entry("rxbuf_min", 0644, dir);
1491 max = create_proc_entry("rxbuf_max", 0644, dir);
1492 cur = create_proc_entry("rxbuf_cur", 0444, dir);
1493 if (!min || !max || !cur)
1494 goto nomem;
1496 min->read_proc = xennet_proc_read;
1497 min->write_proc = xennet_proc_write;
1498 min->data = (void *)((unsigned long)dev | TARGET_MIN);
1500 max->read_proc = xennet_proc_read;
1501 max->write_proc = xennet_proc_write;
1502 max->data = (void *)((unsigned long)dev | TARGET_MAX);
1504 cur->read_proc = xennet_proc_read;
1505 cur->write_proc = xennet_proc_write;
1506 cur->data = (void *)((unsigned long)dev | TARGET_CUR);
1508 return 0;
1510 nomem:
1511 xennet_proc_delif(dev);
1512 return -ENOMEM;
1515 static void xennet_proc_delif(struct net_device *dev)
1517 char name[30];
1519 sprintf(name, "xen/net/%s/rxbuf_min", dev->name);
1520 remove_proc_entry(name, NULL);
1522 sprintf(name, "xen/net/%s/rxbuf_max", dev->name);
1523 remove_proc_entry(name, NULL);
1525 sprintf(name, "xen/net/%s/rxbuf_cur", dev->name);
1526 remove_proc_entry(name, NULL);
1528 sprintf(name, "xen/net/%s", dev->name);
1529 remove_proc_entry(name, NULL);
1532 #endif
1534 module_init(netif_init);
1535 module_exit(netif_exit);