ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 6169:e59f04598620

Allow use of grant tables for netdev tx+rx rings. Code is kinda
ugly due at least in part to the fact that we still have separate
_TX and _RX grant config options - expect this to be unified soon.

Signed-off-by: Steven Hand <steven@xensource.com>
author smh22@firebug.cl.cam.ac.uk
date Sun Aug 14 20:34:13 2005 +0000 (2005-08-14)
parents f46bb706a38d
children d47c61f72572
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/ctrl_if.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, gref_tx_terminal;
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, gref_rx_terminal;
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 static domid_t rdomid = 0;
116 #define GRANT_INVALID_REF (0xFFFF)
117 #endif
119 static void network_tx_buf_gc(struct net_device *dev);
120 static void network_alloc_rx_buffers(struct net_device *dev);
122 static unsigned long rx_pfn_array[NETIF_RX_RING_SIZE];
123 static multicall_entry_t rx_mcl[NETIF_RX_RING_SIZE+1];
124 static mmu_update_t rx_mmu[NETIF_RX_RING_SIZE];
126 #ifdef CONFIG_PROC_FS
127 static int xennet_proc_init(void);
128 static int xennet_proc_addif(struct net_device *dev);
129 static void xennet_proc_delif(struct net_device *dev);
130 #else
131 #define xennet_proc_init() (0)
132 #define xennet_proc_addif(d) (0)
133 #define xennet_proc_delif(d) ((void)0)
134 #endif
136 static struct list_head dev_list;
138 struct net_private
139 {
140 struct list_head list;
141 struct net_device *dev;
143 struct net_device_stats stats;
144 NETIF_RING_IDX rx_resp_cons, tx_resp_cons;
145 unsigned int tx_full;
147 netif_tx_interface_t *tx;
148 netif_rx_interface_t *rx;
150 spinlock_t tx_lock;
151 spinlock_t rx_lock;
153 unsigned int handle;
154 unsigned int evtchn;
156 /* What is the status of our connection to the remote backend? */
157 #define BEST_CLOSED 0
158 #define BEST_DISCONNECTED 1
159 #define BEST_CONNECTED 2
160 unsigned int backend_state;
162 /* Is this interface open or closed (down or up)? */
163 #define UST_CLOSED 0
164 #define UST_OPEN 1
165 unsigned int user_state;
167 /* Receive-ring batched refills. */
168 #define RX_MIN_TARGET 8
169 #define RX_MAX_TARGET NETIF_RX_RING_SIZE
170 int rx_min_target, rx_max_target, rx_target;
171 struct sk_buff_head rx_batch;
173 /*
174 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
175 * array is an index into a chain of free entries.
176 */
177 struct sk_buff *tx_skbs[NETIF_TX_RING_SIZE+1];
178 struct sk_buff *rx_skbs[NETIF_RX_RING_SIZE+1];
179 };
181 /* Access macros for acquiring freeing slots in {tx,rx}_skbs[]. */
182 #define ADD_ID_TO_FREELIST(_list, _id) \
183 (_list)[(_id)] = (_list)[0]; \
184 (_list)[0] = (void *)(unsigned long)(_id);
185 #define GET_ID_FROM_FREELIST(_list) \
186 ({ unsigned long _id = (unsigned long)(_list)[0]; \
187 (_list)[0] = (_list)[_id]; \
188 (unsigned short)_id; })
190 static char *status_name[] = {
191 [NETIF_INTERFACE_STATUS_CLOSED] = "closed",
192 [NETIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected",
193 [NETIF_INTERFACE_STATUS_CONNECTED] = "connected",
194 [NETIF_INTERFACE_STATUS_CHANGED] = "changed",
195 };
197 static char *be_state_name[] = {
198 [BEST_CLOSED] = "closed",
199 [BEST_DISCONNECTED] = "disconnected",
200 [BEST_CONNECTED] = "connected",
201 };
203 #ifdef DEBUG
204 #define DPRINTK(fmt, args...) \
205 printk(KERN_ALERT "xen_net (%s:%d) " fmt, __FUNCTION__, __LINE__, ##args)
206 #else
207 #define DPRINTK(fmt, args...) ((void)0)
208 #endif
209 #define IPRINTK(fmt, args...) \
210 printk(KERN_INFO "xen_net: " fmt, ##args)
211 #define WPRINTK(fmt, args...) \
212 printk(KERN_WARNING "xen_net: " fmt, ##args)
214 static struct net_device *find_dev_by_handle(unsigned int handle)
215 {
216 struct list_head *ent;
217 struct net_private *np;
218 list_for_each (ent, &dev_list) {
219 np = list_entry(ent, struct net_private, list);
220 if (np->handle == handle)
221 return np->dev;
222 }
223 return NULL;
224 }
226 /** Network interface info. */
227 struct netif_ctrl {
228 /** Number of interfaces. */
229 int interface_n;
230 /** Number of connected interfaces. */
231 int connected_n;
232 /** Error code. */
233 int err;
234 int up;
235 };
237 static struct netif_ctrl netctrl;
239 static void netctrl_init(void)
240 {
241 memset(&netctrl, 0, sizeof(netctrl));
242 netctrl.up = NETIF_DRIVER_STATUS_DOWN;
243 }
245 /** Get or set a network interface error.
246 */
247 static int netctrl_err(int err)
248 {
249 if ((err < 0) && !netctrl.err)
250 netctrl.err = err;
251 return netctrl.err;
252 }
254 /** Test if all network interfaces are connected.
255 *
256 * @return 1 if all connected, 0 if not, negative error code otherwise
257 */
258 static int netctrl_connected(void)
259 {
260 int ok;
262 if (netctrl.err)
263 ok = netctrl.err;
264 else if (netctrl.up == NETIF_DRIVER_STATUS_UP)
265 ok = (netctrl.connected_n == netctrl.interface_n);
266 else
267 ok = 0;
269 return ok;
270 }
272 /** Count the connected network interfaces.
273 *
274 * @return connected count
275 */
276 static int netctrl_connected_count(void)
277 {
279 struct list_head *ent;
280 struct net_private *np;
281 unsigned int connected;
283 connected = 0;
285 list_for_each(ent, &dev_list) {
286 np = list_entry(ent, struct net_private, list);
287 if (np->backend_state == BEST_CONNECTED)
288 connected++;
289 }
291 netctrl.connected_n = connected;
292 DPRINTK("> connected_n=%d interface_n=%d\n",
293 netctrl.connected_n, netctrl.interface_n);
294 return connected;
295 }
297 /** Send a packet on a net device to encourage switches to learn the
298 * MAC. We send a fake ARP request.
299 *
300 * @param dev device
301 * @return 0 on success, error code otherwise
302 */
303 static int send_fake_arp(struct net_device *dev)
304 {
305 struct sk_buff *skb;
306 u32 src_ip, dst_ip;
308 dst_ip = INADDR_BROADCAST;
309 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
311 /* No IP? Then nothing to do. */
312 if (src_ip == 0)
313 return 0;
315 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
316 dst_ip, dev, src_ip,
317 /*dst_hw*/ NULL, /*src_hw*/ NULL,
318 /*target_hw*/ dev->dev_addr);
319 if (skb == NULL)
320 return -ENOMEM;
322 return dev_queue_xmit(skb);
323 }
325 static int network_open(struct net_device *dev)
326 {
327 struct net_private *np = netdev_priv(dev);
329 memset(&np->stats, 0, sizeof(np->stats));
331 np->user_state = UST_OPEN;
333 network_alloc_rx_buffers(dev);
334 np->rx->event = np->rx_resp_cons + 1;
336 netif_start_queue(dev);
338 return 0;
339 }
341 static void network_tx_buf_gc(struct net_device *dev)
342 {
343 NETIF_RING_IDX i, prod;
344 unsigned short id;
345 struct net_private *np = netdev_priv(dev);
346 struct sk_buff *skb;
348 if (np->backend_state != BEST_CONNECTED)
349 return;
351 do {
352 prod = np->tx->resp_prod;
353 rmb(); /* Ensure we see responses up to 'rp'. */
355 for (i = np->tx_resp_cons; i != prod; i++) {
356 id = np->tx->ring[MASK_NETIF_TX_IDX(i)].resp.id;
357 skb = np->tx_skbs[id];
358 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
359 if (unlikey(gnttab_query_foreign_access(grant_tx_ref[id]) != 0)) {
360 /* other domain is still using this grant - shouldn't happen
361 but if it does, we'll try to reclaim the grant later */
362 printk(KERN_ALERT "network_tx_buf_gc: warning -- grant "
363 "still in use by backend domain.\n");
364 goto out;
365 }
366 gnttab_end_foreign_access(grant_tx_ref[id], GNTMAP_readonly);
367 gnttab_release_grant_reference(&gref_tx_head, grant_tx_ref[id]);
368 grant_tx_ref[id] = GRANT_INVALID_REF;
369 #endif
370 ADD_ID_TO_FREELIST(np->tx_skbs, id);
371 dev_kfree_skb_irq(skb);
372 }
374 np->tx_resp_cons = prod;
376 /*
377 * Set a new event, then check for race with update of tx_cons. Note
378 * that it is essential to schedule a callback, no matter how few
379 * buffers are pending. Even if there is space in the transmit ring,
380 * higher layers may be blocked because too much data is outstanding:
381 * in such cases notification from Xen is likely to be the only kick
382 * that we'll get.
383 */
384 np->tx->event =
385 prod + ((np->tx->req_prod - prod) >> 1) + 1;
386 mb();
387 } while (prod != np->tx->resp_prod);
389 out:
390 if (np->tx_full && ((np->tx->req_prod - prod) < NETIF_TX_RING_SIZE)) {
391 np->tx_full = 0;
392 if (np->user_state == UST_OPEN)
393 netif_wake_queue(dev);
394 }
395 }
398 static void network_alloc_rx_buffers(struct net_device *dev)
399 {
400 unsigned short id;
401 struct net_private *np = netdev_priv(dev);
402 struct sk_buff *skb;
403 int i, batch_target;
404 NETIF_RING_IDX req_prod = np->rx->req_prod;
405 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
406 int ref;
407 #endif
409 if (unlikely(np->backend_state != BEST_CONNECTED))
410 return;
412 /*
413 * Allocate skbuffs greedily, even though we batch updates to the
414 * receive ring. This creates a less bursty demand on the memory allocator,
415 * so should reduce the chance of failed allocation requests both for
416 * ourself and for other kernel subsystems.
417 */
418 batch_target = np->rx_target - (req_prod - np->rx_resp_cons);
419 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
420 if (unlikely((skb = alloc_xen_skb(dev->mtu + RX_HEADROOM)) == NULL))
421 break;
422 __skb_queue_tail(&np->rx_batch, skb);
423 }
425 /* Is the batch large enough to be worthwhile? */
426 if (i < (np->rx_target/2))
427 return;
429 for (i = 0; ; i++) {
430 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
431 break;
433 skb->dev = dev;
435 id = GET_ID_FROM_FREELIST(np->rx_skbs);
437 np->rx_skbs[id] = skb;
439 np->rx->ring[MASK_NETIF_RX_IDX(req_prod + i)].req.id = id;
440 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
441 if (unlikely((ref = gnttab_claim_grant_reference(&gref_rx_head,
442 gref_rx_terminal)) < 0)) {
443 printk(KERN_ALERT "#### netfront can't claim rx reference\n");
444 BUG();
445 }
446 grant_rx_ref[id] = ref;
447 gnttab_grant_foreign_transfer_ref(ref, rdomid,
448 virt_to_machine(
449 skb->head) >> PAGE_SHIFT);
450 np->rx->ring[MASK_NETIF_RX_IDX(req_prod + i)].req.gref = ref;
451 #endif
452 rx_pfn_array[i] = virt_to_machine(skb->head) >> PAGE_SHIFT;
454 /* Remove this page from pseudo phys map before passing back to Xen. */
455 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT]
456 = INVALID_P2M_ENTRY;
458 MULTI_update_va_mapping(rx_mcl+i, (unsigned long)skb->head,
459 __pte(0), 0);
460 }
462 /* After all PTEs have been zapped we blow away stale TLB entries. */
463 rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] = UVMF_TLB_FLUSH|UVMF_ALL;
465 /* Give away a batch of pages. */
466 rx_mcl[i].op = __HYPERVISOR_dom_mem_op;
467 rx_mcl[i].args[0] = MEMOP_decrease_reservation;
468 rx_mcl[i].args[1] = (unsigned long)rx_pfn_array;
469 rx_mcl[i].args[2] = (unsigned long)i;
470 rx_mcl[i].args[3] = 0;
471 rx_mcl[i].args[4] = DOMID_SELF;
473 /* Tell the ballon driver what is going on. */
474 balloon_update_driver_allowance(i);
476 /* Zap PTEs and give away pages in one big multicall. */
477 (void)HYPERVISOR_multicall(rx_mcl, i+1);
479 /* Check return status of HYPERVISOR_dom_mem_op(). */
480 if (unlikely(rx_mcl[i].result != i))
481 panic("Unable to reduce memory reservation\n");
483 /* Above is a suitable barrier to ensure backend will see requests. */
484 np->rx->req_prod = req_prod + i;
486 /* Adjust our floating fill target if we risked running out of buffers. */
487 if (((req_prod - np->rx->resp_prod) < (np->rx_target / 4)) &&
488 ((np->rx_target *= 2) > np->rx_max_target))
489 np->rx_target = np->rx_max_target;
490 }
493 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
494 {
495 unsigned short id;
496 struct net_private *np = netdev_priv(dev);
497 netif_tx_request_t *tx;
498 NETIF_RING_IDX i;
499 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
500 unsigned int ref;
501 unsigned long mfn;
502 #endif
504 if (unlikely(np->tx_full)) {
505 printk(KERN_ALERT "%s: full queue wasn't stopped!\n", dev->name);
506 netif_stop_queue(dev);
507 goto drop;
508 }
510 if (unlikely((((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >=
511 PAGE_SIZE)) {
512 struct sk_buff *nskb;
513 if (unlikely((nskb = alloc_xen_skb(skb->len)) == NULL))
514 goto drop;
515 skb_put(nskb, skb->len);
516 memcpy(nskb->data, skb->data, skb->len);
517 nskb->dev = skb->dev;
518 dev_kfree_skb(skb);
519 skb = nskb;
520 }
522 spin_lock_irq(&np->tx_lock);
524 if (np->backend_state != BEST_CONNECTED) {
525 spin_unlock_irq(&np->tx_lock);
526 goto drop;
527 }
529 i = np->tx->req_prod;
531 id = GET_ID_FROM_FREELIST(np->tx_skbs);
532 np->tx_skbs[id] = skb;
534 tx = &np->tx->ring[MASK_NETIF_TX_IDX(i)].req;
536 tx->id = id;
537 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
538 if (unlikely((ref = gnttab_claim_grant_reference(&gref_tx_head,
539 gref_tx_terminal)) < 0)) {
540 printk(KERN_ALERT "#### netfront can't claim tx grant reference\n");
541 BUG();
542 }
543 mfn = virt_to_machine(skb->data) >> PAGE_SHIFT;
544 gnttab_grant_foreign_access_ref(ref, rdomid, mfn, GNTMAP_readonly);
545 tx->addr = (ref << PAGE_SHIFT) | ((unsigned long)skb->data & ~PAGE_MASK);
546 grant_tx_ref[id] = ref;
547 #else
548 tx->addr = virt_to_machine(skb->data);
549 #endif
550 tx->size = skb->len;
551 tx->csum_blank = (skb->ip_summed == CHECKSUM_HW);
553 wmb(); /* Ensure that backend will see the request. */
554 np->tx->req_prod = i + 1;
556 network_tx_buf_gc(dev);
558 if ((i - np->tx_resp_cons) == (NETIF_TX_RING_SIZE - 1)) {
559 np->tx_full = 1;
560 netif_stop_queue(dev);
561 }
563 spin_unlock_irq(&np->tx_lock);
565 np->stats.tx_bytes += skb->len;
566 np->stats.tx_packets++;
568 /* Only notify Xen if we really have to. */
569 mb();
570 if (np->tx->TX_TEST_IDX == i)
571 notify_via_evtchn(np->evtchn);
573 return 0;
575 drop:
576 np->stats.tx_dropped++;
577 dev_kfree_skb(skb);
578 return 0;
579 }
581 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
582 {
583 struct net_device *dev = dev_id;
584 struct net_private *np = netdev_priv(dev);
585 unsigned long flags;
587 spin_lock_irqsave(&np->tx_lock, flags);
588 network_tx_buf_gc(dev);
589 spin_unlock_irqrestore(&np->tx_lock, flags);
591 if ((np->rx_resp_cons != np->rx->resp_prod) && (np->user_state == UST_OPEN))
592 netif_rx_schedule(dev);
594 return IRQ_HANDLED;
595 }
598 static int netif_poll(struct net_device *dev, int *pbudget)
599 {
600 struct net_private *np = netdev_priv(dev);
601 struct sk_buff *skb, *nskb;
602 netif_rx_response_t *rx;
603 NETIF_RING_IDX i, rp;
604 mmu_update_t *mmu = rx_mmu;
605 multicall_entry_t *mcl = rx_mcl;
606 int work_done, budget, more_to_do = 1;
607 struct sk_buff_head rxq;
608 unsigned long flags;
609 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
610 unsigned long mfn;
611 grant_ref_t ref;
612 #endif
614 spin_lock(&np->rx_lock);
616 if (np->backend_state != BEST_CONNECTED) {
617 spin_unlock(&np->rx_lock);
618 return 0;
619 }
621 skb_queue_head_init(&rxq);
623 if ((budget = *pbudget) > dev->quota)
624 budget = dev->quota;
625 rp = np->rx->resp_prod;
626 rmb(); /* Ensure we see queued responses up to 'rp'. */
628 for (i = np->rx_resp_cons, work_done = 0;
629 (i != rp) && (work_done < budget);
630 i++, work_done++) {
631 rx = &np->rx->ring[MASK_NETIF_RX_IDX(i)].resp;
632 /*
633 * An error here is very odd. Usually indicates a backend bug,
634 * low-memory condition, or that we didn't have reservation headroom.
635 */
636 if (unlikely(rx->status <= 0)) {
637 if (net_ratelimit())
638 printk(KERN_WARNING "Bad rx buffer (memory squeeze?).\n");
639 np->rx->ring[MASK_NETIF_RX_IDX(np->rx->req_prod)].req.id = rx->id;
640 wmb();
641 np->rx->req_prod++;
642 work_done--;
643 continue;
644 }
646 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
647 ref = grant_rx_ref[rx->id];
648 grant_rx_ref[rx->id] = GRANT_INVALID_REF;
649 mfn = gnttab_end_foreign_transfer(ref);
650 gnttab_release_grant_reference(&gref_rx_head, ref);
651 #endif
653 skb = np->rx_skbs[rx->id];
654 ADD_ID_TO_FREELIST(np->rx_skbs, rx->id);
656 /* NB. We handle skb overflow later. */
657 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
658 skb->data = skb->head + rx->addr;
659 #else
660 skb->data = skb->head + (rx->addr & ~PAGE_MASK);
661 #endif
662 skb->len = rx->status;
663 skb->tail = skb->data + skb->len;
665 if ( rx->csum_valid )
666 skb->ip_summed = CHECKSUM_UNNECESSARY;
668 np->stats.rx_packets++;
669 np->stats.rx_bytes += rx->status;
671 /* Remap the page. */
672 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
673 mmu->ptr = mfn << PAGE_SHIFT | MMU_MACHPHYS_UPDATE;
674 #else
675 mmu->ptr = (rx->addr & PAGE_MASK) | MMU_MACHPHYS_UPDATE;
676 #endif
677 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
678 mmu++;
679 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
680 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
681 pfn_pte_ma(mfn, PAGE_KERNEL), 0);
682 #else
683 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
684 pfn_pte_ma(rx->addr >> PAGE_SHIFT,
685 PAGE_KERNEL), 0);
686 #endif
687 mcl++;
689 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
690 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] = mfn;
691 #else
692 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] =
693 rx->addr >> PAGE_SHIFT;
694 #endif
696 #ifdef GRANT_DEBUG
697 printk(KERN_ALERT "#### rx_poll enqueue vdata=%p mfn=%lu ref=%x\n",
698 skb->data, mfn, ref);
699 #endif
700 __skb_queue_tail(&rxq, skb);
701 }
703 /* Some pages are no longer absent... */
704 balloon_update_driver_allowance(-work_done);
706 /* Do all the remapping work, and M->P updates, in one big hypercall. */
707 if (likely((mcl - rx_mcl) != 0)) {
708 mcl->op = __HYPERVISOR_mmu_update;
709 mcl->args[0] = (unsigned long)rx_mmu;
710 mcl->args[1] = mmu - rx_mmu;
711 mcl->args[2] = 0;
712 mcl->args[3] = DOMID_SELF;
713 mcl++;
714 (void)HYPERVISOR_multicall(rx_mcl, mcl - rx_mcl);
715 }
717 while ((skb = __skb_dequeue(&rxq)) != NULL) {
718 #ifdef GRANT_DEBUG
719 printk(KERN_ALERT "#### rx_poll dequeue vdata=%p mfn=%lu\n",
720 skb->data, virt_to_machine(skb->data)>>PAGE_SHIFT);
721 dump_packet('d', skb->data, (unsigned long)skb->data);
722 #endif
723 /*
724 * Enough room in skbuff for the data we were passed? Also, Linux
725 * expects at least 16 bytes headroom in each receive buffer.
726 */
727 if (unlikely(skb->tail > skb->end) ||
728 unlikely((skb->data - skb->head) < 16)) {
729 nskb = NULL;
732 /* Only copy the packet if it fits in the current MTU. */
733 if (skb->len <= (dev->mtu + ETH_HLEN)) {
734 if ((skb->tail > skb->end) && net_ratelimit())
735 printk(KERN_INFO "Received packet needs %zd bytes more "
736 "headroom.\n", skb->tail - skb->end);
738 if ((nskb = alloc_xen_skb(skb->len + 2)) != NULL) {
739 skb_reserve(nskb, 2);
740 skb_put(nskb, skb->len);
741 memcpy(nskb->data, skb->data, skb->len);
742 nskb->dev = skb->dev;
743 }
744 }
745 else if (net_ratelimit())
746 printk(KERN_INFO "Received packet too big for MTU "
747 "(%d > %d)\n", skb->len - ETH_HLEN, dev->mtu);
749 /* Reinitialise and then destroy the old skbuff. */
750 skb->len = 0;
751 skb->tail = skb->data;
752 init_skb_shinfo(skb);
753 dev_kfree_skb(skb);
755 /* Switch old for new, if we copied the buffer. */
756 if ((skb = nskb) == NULL)
757 continue;
758 }
760 /* Set the shared-info area, which is hidden behind the real data. */
761 init_skb_shinfo(skb);
762 /* Ethernet-specific work. Delayed to here as it peeks the header. */
763 skb->protocol = eth_type_trans(skb, dev);
765 /* Pass it up. */
766 netif_receive_skb(skb);
767 dev->last_rx = jiffies;
768 }
770 np->rx_resp_cons = i;
772 /* If we get a callback with very few responses, reduce fill target. */
773 /* NB. Note exponential increase, linear decrease. */
774 if (((np->rx->req_prod - np->rx->resp_prod) > ((3*np->rx_target) / 4)) &&
775 (--np->rx_target < np->rx_min_target))
776 np->rx_target = np->rx_min_target;
778 network_alloc_rx_buffers(dev);
780 *pbudget -= work_done;
781 dev->quota -= work_done;
783 if (work_done < budget) {
784 local_irq_save(flags);
786 np->rx->event = i + 1;
788 /* Deal with hypervisor racing our resetting of rx_event. */
789 mb();
790 if (np->rx->resp_prod == i) {
791 __netif_rx_complete(dev);
792 more_to_do = 0;
793 }
795 local_irq_restore(flags);
796 }
798 spin_unlock(&np->rx_lock);
800 return more_to_do;
801 }
804 static int network_close(struct net_device *dev)
805 {
806 struct net_private *np = netdev_priv(dev);
807 np->user_state = UST_CLOSED;
808 netif_stop_queue(np->dev);
809 return 0;
810 }
813 static struct net_device_stats *network_get_stats(struct net_device *dev)
814 {
815 struct net_private *np = netdev_priv(dev);
816 return &np->stats;
817 }
820 static void network_connect(struct net_device *dev,
821 netif_fe_interface_status_t *status)
822 {
823 struct net_private *np;
824 int i, requeue_idx;
825 netif_tx_request_t *tx;
827 np = netdev_priv(dev);
828 spin_lock_irq(&np->tx_lock);
829 spin_lock(&np->rx_lock);
831 /* Recovery procedure: */
833 /* Step 1: Reinitialise variables. */
834 np->rx_resp_cons = np->tx_resp_cons = np->tx_full = 0;
835 np->rx->event = np->tx->event = 1;
837 /* Step 2: Rebuild the RX and TX ring contents.
838 * NB. We could just free the queued TX packets now but we hope
839 * that sending them out might do some good. We have to rebuild
840 * the RX ring because some of our pages are currently flipped out
841 * so we can't just free the RX skbs.
842 * NB2. Freelist index entries are always going to be less than
843 * __PAGE_OFFSET, whereas pointers to skbs will always be equal or
844 * greater than __PAGE_OFFSET: we use this property to distinguish
845 * them.
846 */
848 /* Rebuild the TX buffer freelist and the TX ring itself.
849 * NB. This reorders packets. We could keep more private state
850 * to avoid this but maybe it doesn't matter so much given the
851 * interface has been down.
852 */
853 for (requeue_idx = 0, i = 1; i <= NETIF_TX_RING_SIZE; i++) {
854 if ((unsigned long)np->tx_skbs[i] >= __PAGE_OFFSET) {
855 struct sk_buff *skb = np->tx_skbs[i];
857 tx = &np->tx->ring[requeue_idx++].req;
859 tx->id = i;
860 tx->addr = virt_to_machine(skb->data);
861 tx->size = skb->len;
863 np->stats.tx_bytes += skb->len;
864 np->stats.tx_packets++;
865 }
866 }
867 wmb();
868 np->tx->req_prod = requeue_idx;
870 /* Rebuild the RX buffer freelist and the RX ring itself. */
871 for (requeue_idx = 0, i = 1; i <= NETIF_RX_RING_SIZE; i++)
872 if ((unsigned long)np->rx_skbs[i] >= __PAGE_OFFSET)
873 np->rx->ring[requeue_idx++].req.id = i;
874 wmb();
875 np->rx->req_prod = requeue_idx;
877 /* Step 3: All public and private state should now be sane. Get
878 * ready to start sending and receiving packets and give the driver
879 * domain a kick because we've probably just requeued some
880 * packets.
881 */
882 np->backend_state = BEST_CONNECTED;
883 wmb();
884 notify_via_evtchn(status->evtchn);
885 network_tx_buf_gc(dev);
887 if (np->user_state == UST_OPEN)
888 netif_start_queue(dev);
890 spin_unlock(&np->rx_lock);
891 spin_unlock_irq(&np->tx_lock);
892 }
894 static void vif_show(struct net_private *np)
895 {
896 #ifdef DEBUG
897 if (np) {
898 IPRINTK("<vif handle=%u %s(%s) evtchn=%u tx=%p rx=%p>\n",
899 np->handle,
900 be_state_name[np->backend_state],
901 np->user_state ? "open" : "closed",
902 np->evtchn,
903 np->tx,
904 np->rx);
905 } else {
906 IPRINTK("<vif NULL>\n");
907 }
908 #endif
909 }
911 /* Send a connect message to xend to tell it to bring up the interface. */
912 static void send_interface_connect(struct net_private *np)
913 {
914 ctrl_msg_t cmsg = {
915 .type = CMSG_NETIF_FE,
916 .subtype = CMSG_NETIF_FE_INTERFACE_CONNECT,
917 .length = sizeof(netif_fe_interface_connect_t),
918 };
919 netif_fe_interface_connect_t *msg = (void*)cmsg.msg;
921 msg->handle = np->handle;
922 msg->tx_shmem_frame = (virt_to_machine(np->tx) >> PAGE_SHIFT);
923 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
924 msg->tx_shmem_ref = (u32)gnttab_claim_grant_reference(&gref_tx_head,
925 gref_tx_terminal);
926 if(msg->tx_shmem_ref < 0) {
927 printk(KERN_ALERT "#### netfront can't claim tx_shmem reference\n");
928 BUG();
929 }
930 gnttab_grant_foreign_access_ref (msg->tx_shmem_ref, rdomid,
931 msg->tx_shmem_frame, 0);
932 #endif
934 msg->rx_shmem_frame = (virt_to_machine(np->rx) >> PAGE_SHIFT);
935 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
936 msg->rx_shmem_ref = (u32)gnttab_claim_grant_reference(&gref_rx_head,
937 gref_rx_terminal);
938 if(msg->rx_shmem_ref < 0) {
939 printk(KERN_ALERT "#### netfront can't claim rx_shmem reference\n");
940 BUG();
941 }
942 gnttab_grant_foreign_access_ref (msg->rx_shmem_ref, rdomid,
943 msg->rx_shmem_frame, 0);
944 #endif
946 ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
947 }
949 /* Send a driver status notification to the domain controller. */
950 static int send_driver_status(int ok)
951 {
952 int err = 0;
953 ctrl_msg_t cmsg = {
954 .type = CMSG_NETIF_FE,
955 .subtype = CMSG_NETIF_FE_DRIVER_STATUS,
956 .length = sizeof(netif_fe_driver_status_t),
957 };
958 netif_fe_driver_status_t *msg = (void*)cmsg.msg;
960 msg->status = (ok ? NETIF_DRIVER_STATUS_UP : NETIF_DRIVER_STATUS_DOWN);
961 err = ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
962 return err;
963 }
965 /* Stop network device and free tx/rx queues and irq.
966 */
967 static void vif_release(struct net_private *np)
968 {
969 /* Stop old i/f to prevent errors whilst we rebuild the state. */
970 spin_lock_irq(&np->tx_lock);
971 spin_lock(&np->rx_lock);
972 netif_stop_queue(np->dev);
973 /* np->backend_state = BEST_DISCONNECTED; */
974 spin_unlock(&np->rx_lock);
975 spin_unlock_irq(&np->tx_lock);
977 /* Free resources. */
978 if ( np->tx != NULL )
979 {
980 unbind_evtchn_from_irqhandler(np->evtchn, np->dev);
981 free_page((unsigned long)np->tx);
982 free_page((unsigned long)np->rx);
983 np->evtchn = 0;
984 np->tx = NULL;
985 np->rx = NULL;
986 }
987 }
989 /* Release vif resources and close it down completely.
990 */
991 static void vif_close(struct net_private *np)
992 {
993 WPRINTK("Unexpected netif-CLOSED message in state %s\n",
994 be_state_name[np->backend_state]);
995 vif_release(np);
996 np->backend_state = BEST_CLOSED;
997 /* todo: take dev down and free. */
998 vif_show(np);
999 }
1001 /* Move the vif into disconnected state.
1002 * Allocates tx/rx pages.
1003 * Sends connect message to xend.
1004 */
1005 static void vif_disconnect(struct net_private *np)
1007 if(np->tx) free_page((unsigned long)np->tx);
1008 if(np->rx) free_page((unsigned long)np->rx);
1009 // Before this np->tx and np->rx had better be null.
1010 np->tx = (netif_tx_interface_t *)__get_free_page(GFP_KERNEL);
1011 np->rx = (netif_rx_interface_t *)__get_free_page(GFP_KERNEL);
1012 memset(np->tx, 0, PAGE_SIZE);
1013 memset(np->rx, 0, PAGE_SIZE);
1014 np->backend_state = BEST_DISCONNECTED;
1015 send_interface_connect(np);
1016 vif_show(np);
1019 /* Begin interface recovery.
1021 * NB. Whilst we're recovering, we turn the carrier state off. We
1022 * take measures to ensure that this device isn't used for
1023 * anything. We also stop the queue for this device. Various
1024 * different approaches (e.g. continuing to buffer packets) have
1025 * been tested but don't appear to improve the overall impact on
1026 * TCP connections.
1028 * TODO: (MAW) Change the Xend<->Guest protocol so that a recovery
1029 * is initiated by a special "RESET" message - disconnect could
1030 * just mean we're not allowed to use this interface any more.
1031 */
1032 static void vif_reset(struct net_private *np)
1034 IPRINTK("Attempting to reconnect network interface: handle=%u\n",
1035 np->handle);
1036 vif_release(np);
1037 vif_disconnect(np);
1038 vif_show(np);
1041 /* Move the vif into connected state.
1042 * Sets the mac and event channel from the message.
1043 * Binds the irq to the event channel.
1044 */
1045 static void
1046 vif_connect(struct net_private *np, netif_fe_interface_status_t *status)
1048 struct net_device *dev = np->dev;
1049 memcpy(dev->dev_addr, status->mac, ETH_ALEN);
1050 network_connect(dev, status);
1051 np->evtchn = status->evtchn;
1052 #if defined(CONFIG_XEN_NETDEV_GRANT_TX) || defined(CONFIG_XEN_NETDEV_GRANT_RX)
1053 rdomid = status->domid;
1054 #endif
1055 (void)bind_evtchn_to_irqhandler(
1056 np->evtchn, netif_int, SA_SAMPLE_RANDOM, dev->name, dev);
1057 netctrl_connected_count();
1058 (void)send_fake_arp(dev);
1059 vif_show(np);
1062 static struct ethtool_ops network_ethtool_ops =
1064 .get_tx_csum = ethtool_op_get_tx_csum,
1065 .set_tx_csum = ethtool_op_set_tx_csum,
1066 };
1068 /** Create a network device.
1069 * @param handle device handle
1070 * @param val return parameter for created device
1071 * @return 0 on success, error code otherwise
1072 */
1073 static int create_netdev(int handle, struct net_device **val)
1075 int i, err = 0;
1076 struct net_device *dev = NULL;
1077 struct net_private *np = NULL;
1079 if ((dev = alloc_etherdev(sizeof(struct net_private))) == NULL) {
1080 printk(KERN_WARNING "%s> alloc_etherdev failed.\n", __FUNCTION__);
1081 err = -ENOMEM;
1082 goto exit;
1085 np = netdev_priv(dev);
1086 np->backend_state = BEST_CLOSED;
1087 np->user_state = UST_CLOSED;
1088 np->handle = handle;
1090 spin_lock_init(&np->tx_lock);
1091 spin_lock_init(&np->rx_lock);
1093 skb_queue_head_init(&np->rx_batch);
1094 np->rx_target = RX_MIN_TARGET;
1095 np->rx_min_target = RX_MIN_TARGET;
1096 np->rx_max_target = RX_MAX_TARGET;
1098 /* Initialise {tx,rx}_skbs to be a free chain containing every entry. */
1099 for (i = 0; i <= NETIF_TX_RING_SIZE; i++) {
1100 np->tx_skbs[i] = (void *)((unsigned long) i+1);
1101 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
1102 grant_tx_ref[i] = GRANT_INVALID_REF;
1103 #endif
1105 for (i = 0; i <= NETIF_RX_RING_SIZE; i++) {
1106 np->rx_skbs[i] = (void *)((unsigned long) i+1);
1107 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
1108 grant_rx_ref[i] = GRANT_INVALID_REF;
1109 #endif
1112 dev->open = network_open;
1113 dev->hard_start_xmit = network_start_xmit;
1114 dev->stop = network_close;
1115 dev->get_stats = network_get_stats;
1116 dev->poll = netif_poll;
1117 dev->weight = 64;
1118 dev->features = NETIF_F_IP_CSUM;
1120 SET_ETHTOOL_OPS(dev, &network_ethtool_ops);
1122 if ((err = register_netdev(dev)) != 0) {
1123 printk(KERN_WARNING "%s> register_netdev err=%d\n", __FUNCTION__, err);
1124 goto exit;
1127 if ((err = xennet_proc_addif(dev)) != 0) {
1128 unregister_netdev(dev);
1129 goto exit;
1132 np->dev = dev;
1133 list_add(&np->list, &dev_list);
1135 exit:
1136 if ((err != 0) && (dev != NULL))
1137 kfree(dev);
1138 else if (val != NULL)
1139 *val = dev;
1140 return err;
1143 /* Get the target interface for a status message.
1144 * Creates the interface when it makes sense.
1145 * The returned interface may be null when there is no error.
1147 * @param status status message
1148 * @param np return parameter for interface state
1149 * @return 0 on success, error code otherwise
1150 */
1151 static int
1152 target_vif(netif_fe_interface_status_t *status, struct net_private **np)
1154 int err = 0;
1155 struct net_device *dev;
1157 DPRINTK("> handle=%d\n", status->handle);
1158 if (status->handle < 0) {
1159 err = -EINVAL;
1160 goto exit;
1163 if ((dev = find_dev_by_handle(status->handle)) != NULL)
1164 goto exit;
1166 if (status->status == NETIF_INTERFACE_STATUS_CLOSED)
1167 goto exit;
1168 if (status->status == NETIF_INTERFACE_STATUS_CHANGED)
1169 goto exit;
1171 /* It's a new interface in a good state - create it. */
1172 DPRINTK("> create device...\n");
1173 if ((err = create_netdev(status->handle, &dev)) != 0)
1174 goto exit;
1176 netctrl.interface_n++;
1178 exit:
1179 if (np != NULL)
1180 *np = ((dev && !err) ? netdev_priv(dev) : NULL);
1181 DPRINTK("< err=%d\n", err);
1182 return err;
1185 /* Handle an interface status message. */
1186 static void netif_interface_status(netif_fe_interface_status_t *status)
1188 int err = 0;
1189 struct net_private *np = NULL;
1191 DPRINTK("> status=%s handle=%d\n",
1192 status_name[status->status], status->handle);
1194 if ((err = target_vif(status, &np)) != 0) {
1195 WPRINTK("Invalid netif: handle=%u\n", status->handle);
1196 return;
1199 if (np == NULL) {
1200 DPRINTK("> no vif\n");
1201 return;
1204 switch (status->status) {
1205 case NETIF_INTERFACE_STATUS_CLOSED:
1206 switch (np->backend_state) {
1207 case BEST_CLOSED:
1208 case BEST_DISCONNECTED:
1209 case BEST_CONNECTED:
1210 vif_close(np);
1211 break;
1213 break;
1215 case NETIF_INTERFACE_STATUS_DISCONNECTED:
1216 switch (np->backend_state) {
1217 case BEST_CLOSED:
1218 vif_disconnect(np);
1219 break;
1220 case BEST_DISCONNECTED:
1221 case BEST_CONNECTED:
1222 vif_reset(np);
1223 break;
1225 break;
1227 case NETIF_INTERFACE_STATUS_CONNECTED:
1228 switch (np->backend_state) {
1229 case BEST_CLOSED:
1230 WPRINTK("Unexpected netif status %s in state %s\n",
1231 status_name[status->status],
1232 be_state_name[np->backend_state]);
1233 vif_disconnect(np);
1234 vif_connect(np, status);
1235 break;
1236 case BEST_DISCONNECTED:
1237 vif_connect(np, status);
1238 break;
1240 break;
1242 case NETIF_INTERFACE_STATUS_CHANGED:
1243 /*
1244 * The domain controller is notifying us that a device has been
1245 * added or removed.
1246 */
1247 break;
1249 default:
1250 WPRINTK("Invalid netif status code %d\n", status->status);
1251 break;
1254 vif_show(np);
1257 /*
1258 * Initialize the network control interface.
1259 */
1260 static void netif_driver_status(netif_fe_driver_status_t *status)
1262 netctrl.up = status->status;
1263 netctrl_connected_count();
1266 /* Receive handler for control messages. */
1267 static void netif_ctrlif_rx(ctrl_msg_t *msg, unsigned long id)
1270 switch (msg->subtype) {
1271 case CMSG_NETIF_FE_INTERFACE_STATUS:
1272 netif_interface_status((netif_fe_interface_status_t *) &msg->msg[0]);
1273 break;
1275 case CMSG_NETIF_FE_DRIVER_STATUS:
1276 netif_driver_status((netif_fe_driver_status_t *) &msg->msg[0]);
1277 break;
1279 default:
1280 msg->length = 0;
1281 break;
1284 ctrl_if_send_response(msg);
1288 #if 1
1289 /* Wait for all interfaces to be connected.
1291 * This works OK, but we'd like to use the probing mode (see below).
1292 */
1293 static int probe_interfaces(void)
1295 int err = 0, conn = 0;
1296 int wait_i, wait_n = 100;
1298 DPRINTK(">\n");
1300 for (wait_i = 0; wait_i < wait_n; wait_i++) {
1301 DPRINTK("> wait_i=%d\n", wait_i);
1302 conn = netctrl_connected();
1303 if(conn) break;
1304 DPRINTK("> schedule_timeout...\n");
1305 set_current_state(TASK_INTERRUPTIBLE);
1306 schedule_timeout(10);
1309 DPRINTK("> wait finished...\n");
1310 if (conn <= 0) {
1311 err = netctrl_err(-ENETDOWN);
1312 WPRINTK("Failed to connect all virtual interfaces: err=%d\n", err);
1315 DPRINTK("< err=%d\n", err);
1317 return err;
1319 #else
1320 /* Probe for interfaces until no more are found.
1322 * This is the mode we'd like to use, but at the moment it panics the kernel.
1323 */
1324 static int probe_interfaces(void)
1326 int err = 0;
1327 int wait_i, wait_n = 100;
1328 ctrl_msg_t cmsg = {
1329 .type = CMSG_NETIF_FE,
1330 .subtype = CMSG_NETIF_FE_INTERFACE_STATUS,
1331 .length = sizeof(netif_fe_interface_status_t),
1332 };
1333 netif_fe_interface_status_t msg = {};
1334 ctrl_msg_t rmsg = {};
1335 netif_fe_interface_status_t *reply = (void*)rmsg.msg;
1336 int state = TASK_UNINTERRUPTIBLE;
1337 u32 query = -1;
1339 DPRINTK(">\n");
1341 netctrl.interface_n = 0;
1342 for (wait_i = 0; wait_i < wait_n; wait_i++) {
1343 DPRINTK("> wait_i=%d query=%d\n", wait_i, query);
1344 msg.handle = query;
1345 memcpy(cmsg.msg, &msg, sizeof(msg));
1346 DPRINTK("> set_current_state...\n");
1347 set_current_state(state);
1348 DPRINTK("> rmsg=%p msg=%p, reply=%p\n", &rmsg, rmsg.msg, reply);
1349 DPRINTK("> sending...\n");
1350 err = ctrl_if_send_message_and_get_response(&cmsg, &rmsg, state);
1351 DPRINTK("> err=%d\n", err);
1352 if(err) goto exit;
1353 DPRINTK("> rmsg=%p msg=%p, reply=%p\n", &rmsg, rmsg.msg, reply);
1354 if((int)reply->handle < 0) {
1355 // No more interfaces.
1356 break;
1358 query = -reply->handle - 2;
1359 DPRINTK(">netif_interface_status ...\n");
1360 netif_interface_status(reply);
1363 exit:
1364 if (err) {
1365 err = netctrl_err(-ENETDOWN);
1366 WPRINTK("Connecting virtual network interfaces failed: err=%d\n", err);
1369 DPRINTK("< err=%d\n", err);
1370 return err;
1373 #endif
1375 /*
1376 * We use this notifier to send out a fake ARP reply to reset switches and
1377 * router ARP caches when an IP interface is brought up on a VIF.
1378 */
1379 static int
1380 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
1382 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
1383 struct net_device *dev = ifa->ifa_dev->dev;
1384 struct list_head *ent;
1385 struct net_private *np;
1387 if (event != NETDEV_UP)
1388 goto out;
1390 list_for_each (ent, &dev_list) {
1391 np = list_entry(ent, struct net_private, list);
1392 if (np->dev == dev)
1393 (void)send_fake_arp(dev);
1396 out:
1397 return NOTIFY_DONE;
1400 static struct notifier_block notifier_inetdev = {
1401 .notifier_call = inetdev_notify,
1402 .next = NULL,
1403 .priority = 0
1404 };
1406 static int __init netif_init(void)
1408 int err = 0;
1410 if (xen_start_info.flags & SIF_INITDOMAIN)
1411 return 0;
1412 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
1413 /* A grant for every ring slot, plus one for the ring itself */
1414 if (gnttab_alloc_grant_references(NETIF_TX_RING_SIZE + 1,
1415 &gref_tx_head, &gref_tx_terminal) < 0) {
1416 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1417 return 1;
1419 printk(KERN_ALERT "Netdev frontend (TX) is using grant tables.\n");
1420 #endif
1421 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
1422 /* A grant for every ring slot, plus one for the ring itself */
1423 if (gnttab_alloc_grant_references(NETIF_RX_RING_SIZE + 1,
1424 &gref_rx_head, &gref_rx_terminal) < 0) {
1425 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1426 return 1;
1428 printk(KERN_ALERT "Netdev frontend (RX) is using grant tables.\n");
1429 #endif
1431 if ((err = xennet_proc_init()) != 0)
1432 return err;
1434 IPRINTK("Initialising virtual ethernet driver.\n");
1435 INIT_LIST_HEAD(&dev_list);
1436 (void)register_inetaddr_notifier(&notifier_inetdev);
1437 netctrl_init();
1438 (void)ctrl_if_register_receiver(CMSG_NETIF_FE, netif_ctrlif_rx,
1439 CALLBACK_IN_BLOCKING_CONTEXT);
1440 send_driver_status(1);
1441 err = probe_interfaces();
1442 if (err)
1443 ctrl_if_unregister_receiver(CMSG_NETIF_FE, netif_ctrlif_rx);
1445 DPRINTK("< err=%d\n", err);
1446 return err;
1449 static void netif_exit(void)
1451 #ifdef CONFIG_XEN_NETDEV_GRANT_TX
1452 gnttab_free_grant_references(NETIF_TX_RING_SIZE, gref_tx_head);
1453 #endif
1454 #ifdef CONFIG_XEN_NETDEV_GRANT_RX
1455 gnttab_free_grant_references(NETIF_RX_RING_SIZE, gref_rx_head);
1456 #endif
1459 static void vif_suspend(struct net_private *np)
1461 /* Avoid having tx/rx stuff happen until we're ready. */
1462 unbind_evtchn_from_irqhandler(np->evtchn, np->dev);
1465 static void vif_resume(struct net_private *np)
1467 /*
1468 * Connect regardless of whether IFF_UP flag set.
1469 * Stop bad things from happening until we're back up.
1470 */
1471 np->backend_state = BEST_DISCONNECTED;
1472 memset(np->tx, 0, PAGE_SIZE);
1473 memset(np->rx, 0, PAGE_SIZE);
1475 send_interface_connect(np);
1478 void netif_suspend(void)
1480 struct list_head *ent;
1481 struct net_private *np;
1483 list_for_each (ent, &dev_list) {
1484 np = list_entry(ent, struct net_private, list);
1485 vif_suspend(np);
1489 void netif_resume(void)
1491 struct list_head *ent;
1492 struct net_private *np;
1494 list_for_each (ent, &dev_list) {
1495 np = list_entry(ent, struct net_private, list);
1496 vif_resume(np);
1500 #ifdef CONFIG_PROC_FS
1502 #define TARGET_MIN 0UL
1503 #define TARGET_MAX 1UL
1504 #define TARGET_CUR 2UL
1506 static int xennet_proc_read(
1507 char *page, char **start, off_t off, int count, int *eof, void *data)
1509 struct net_device *dev = (struct net_device *)((unsigned long)data & ~3UL);
1510 struct net_private *np = netdev_priv(dev);
1511 int len = 0, which_target = (long)data & 3;
1513 switch (which_target)
1515 case TARGET_MIN:
1516 len = sprintf(page, "%d\n", np->rx_min_target);
1517 break;
1518 case TARGET_MAX:
1519 len = sprintf(page, "%d\n", np->rx_max_target);
1520 break;
1521 case TARGET_CUR:
1522 len = sprintf(page, "%d\n", np->rx_target);
1523 break;
1526 *eof = 1;
1527 return len;
1530 static int xennet_proc_write(
1531 struct file *file, const char __user *buffer,
1532 unsigned long count, void *data)
1534 struct net_device *dev = (struct net_device *)((unsigned long)data & ~3UL);
1535 struct net_private *np = netdev_priv(dev);
1536 int which_target = (long)data & 3;
1537 char string[64];
1538 long target;
1540 if (!capable(CAP_SYS_ADMIN))
1541 return -EPERM;
1543 if (count <= 1)
1544 return -EBADMSG; /* runt */
1545 if (count > sizeof(string))
1546 return -EFBIG; /* too long */
1548 if (copy_from_user(string, buffer, count))
1549 return -EFAULT;
1550 string[sizeof(string)-1] = '\0';
1552 target = simple_strtol(string, NULL, 10);
1553 if (target < RX_MIN_TARGET)
1554 target = RX_MIN_TARGET;
1555 if (target > RX_MAX_TARGET)
1556 target = RX_MAX_TARGET;
1558 spin_lock(&np->rx_lock);
1560 switch (which_target)
1562 case TARGET_MIN:
1563 if (target > np->rx_max_target)
1564 np->rx_max_target = target;
1565 np->rx_min_target = target;
1566 if (target > np->rx_target)
1567 np->rx_target = target;
1568 break;
1569 case TARGET_MAX:
1570 if (target < np->rx_min_target)
1571 np->rx_min_target = target;
1572 np->rx_max_target = target;
1573 if (target < np->rx_target)
1574 np->rx_target = target;
1575 break;
1576 case TARGET_CUR:
1577 break;
1580 network_alloc_rx_buffers(dev);
1582 spin_unlock(&np->rx_lock);
1584 return count;
1587 static int xennet_proc_init(void)
1589 if (proc_mkdir("xen/net", NULL) == NULL)
1590 return -ENOMEM;
1591 return 0;
1594 static int xennet_proc_addif(struct net_device *dev)
1596 struct proc_dir_entry *dir, *min, *max, *cur;
1597 char name[30];
1599 sprintf(name, "xen/net/%s", dev->name);
1601 dir = proc_mkdir(name, NULL);
1602 if (!dir)
1603 goto nomem;
1605 min = create_proc_entry("rxbuf_min", 0644, dir);
1606 max = create_proc_entry("rxbuf_max", 0644, dir);
1607 cur = create_proc_entry("rxbuf_cur", 0444, dir);
1608 if (!min || !max || !cur)
1609 goto nomem;
1611 min->read_proc = xennet_proc_read;
1612 min->write_proc = xennet_proc_write;
1613 min->data = (void *)((unsigned long)dev | TARGET_MIN);
1615 max->read_proc = xennet_proc_read;
1616 max->write_proc = xennet_proc_write;
1617 max->data = (void *)((unsigned long)dev | TARGET_MAX);
1619 cur->read_proc = xennet_proc_read;
1620 cur->write_proc = xennet_proc_write;
1621 cur->data = (void *)((unsigned long)dev | TARGET_CUR);
1623 return 0;
1625 nomem:
1626 xennet_proc_delif(dev);
1627 return -ENOMEM;
1630 static void xennet_proc_delif(struct net_device *dev)
1632 char name[30];
1634 sprintf(name, "xen/net/%s/rxbuf_min", dev->name);
1635 remove_proc_entry(name, NULL);
1637 sprintf(name, "xen/net/%s/rxbuf_max", dev->name);
1638 remove_proc_entry(name, NULL);
1640 sprintf(name, "xen/net/%s/rxbuf_cur", dev->name);
1641 remove_proc_entry(name, NULL);
1643 sprintf(name, "xen/net/%s", dev->name);
1644 remove_proc_entry(name, NULL);
1647 #endif
1649 module_init(netif_init);
1650 module_exit(netif_exit);