debuggers.hg

view linux-2.6.10-rc2-xen-sparse/drivers/xen/netfront/netfront.c @ 3289:a169836882cb

bitkeeper revision 1.1159.170.59 (41b4c2fdJ2gj_BWy27Vj3ptayZp_yg)

sync w/ head.
author cl349@arcadians.cl.cam.ac.uk
date Mon Dec 06 20:37:17 2004 +0000 (2004-12-06)
parents f65b65977b19
children
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 <net/sock.h>
43 #include <net/pkt_sched.h>
44 #include <asm/io.h>
45 #include <asm-xen/evtchn.h>
46 #include <asm-xen/ctrl_if.h>
47 #include <asm-xen/xen-public/io/netif.h>
48 #include <asm-xen/balloon.h>
49 #include <asm/page.h>
51 #include <net/arp.h>
52 #include <net/route.h>
54 #define DEBUG 0
56 #ifndef __GFP_NOWARN
57 #define __GFP_NOWARN 0
58 #endif
59 #define alloc_xen_skb(_l) __dev_alloc_skb((_l), GFP_ATOMIC|__GFP_NOWARN)
61 #define init_skb_shinfo(_skb) \
62 do { \
63 atomic_set(&(skb_shinfo(_skb)->dataref), 1); \
64 skb_shinfo(_skb)->nr_frags = 0; \
65 skb_shinfo(_skb)->frag_list = NULL; \
66 } while ( 0 )
68 /* Allow headroom on each rx pkt for Ethernet header, alignment padding, ... */
69 #define RX_HEADROOM 200
71 /*
72 * If the backend driver is pipelining transmit requests then we can be very
73 * aggressive in avoiding new-packet notifications -- only need to send a
74 * notification if there are no outstanding unreceived responses.
75 * If the backend may be buffering our transmit buffers for any reason then we
76 * are rather more conservative.
77 */
78 #ifdef CONFIG_XEN_NETDEV_FRONTEND_PIPELINED_TRANSMITTER
79 #define TX_TEST_IDX resp_prod /* aggressive: any outstanding responses? */
80 #else
81 #define TX_TEST_IDX req_cons /* conservative: not seen all our requests? */
82 #endif
84 static void network_tx_buf_gc(struct net_device *dev);
85 static void network_alloc_rx_buffers(struct net_device *dev);
87 static unsigned long rx_pfn_array[NETIF_RX_RING_SIZE];
88 static multicall_entry_t rx_mcl[NETIF_RX_RING_SIZE+1];
89 static mmu_update_t rx_mmu[NETIF_RX_RING_SIZE];
91 static struct list_head dev_list;
93 struct net_private
94 {
95 struct list_head list;
96 struct net_device *dev;
98 struct net_device_stats stats;
99 NETIF_RING_IDX rx_resp_cons, tx_resp_cons;
100 unsigned int tx_full;
102 netif_tx_interface_t *tx;
103 netif_rx_interface_t *rx;
105 spinlock_t tx_lock;
106 spinlock_t rx_lock;
108 unsigned int handle;
109 unsigned int evtchn;
110 unsigned int irq;
112 /* What is the status of our connection to the remote backend? */
113 #define BEST_CLOSED 0
114 #define BEST_DISCONNECTED 1
115 #define BEST_CONNECTED 2
116 unsigned int backend_state;
118 /* Is this interface open or closed (down or up)? */
119 #define UST_CLOSED 0
120 #define UST_OPEN 1
121 unsigned int user_state;
123 /* Receive-ring batched refills. */
124 #define RX_MIN_TARGET 8
125 #define RX_MAX_TARGET NETIF_RX_RING_SIZE
126 int rx_target;
127 struct sk_buff_head rx_batch;
129 /*
130 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
131 * array is an index into a chain of free entries.
132 */
133 struct sk_buff *tx_skbs[NETIF_TX_RING_SIZE+1];
134 struct sk_buff *rx_skbs[NETIF_RX_RING_SIZE+1];
135 };
137 /* Access macros for acquiring freeing slots in {tx,rx}_skbs[]. */
138 #define ADD_ID_TO_FREELIST(_list, _id) \
139 (_list)[(_id)] = (_list)[0]; \
140 (_list)[0] = (void *)(unsigned long)(_id);
141 #define GET_ID_FROM_FREELIST(_list) \
142 ({ unsigned long _id = (unsigned long)(_list)[0]; \
143 (_list)[0] = (_list)[_id]; \
144 (unsigned short)_id; })
146 static char *status_name[] = {
147 [NETIF_INTERFACE_STATUS_CLOSED] = "closed",
148 [NETIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected",
149 [NETIF_INTERFACE_STATUS_CONNECTED] = "connected",
150 [NETIF_INTERFACE_STATUS_CHANGED] = "changed",
151 };
153 static char *be_state_name[] = {
154 [BEST_CLOSED] = "closed",
155 [BEST_DISCONNECTED] = "disconnected",
156 [BEST_CONNECTED] = "connected",
157 };
159 #if DEBUG
160 #define DPRINTK(fmt, args...) \
161 printk(KERN_ALERT "xen_net (%s:%d) " fmt, __FUNCTION__, __LINE__, ##args)
162 #else
163 #define DPRINTK(fmt, args...) ((void)0)
164 #endif
165 #define IPRINTK(fmt, args...) \
166 printk(KERN_INFO "xen_net: " fmt, ##args)
167 #define WPRINTK(fmt, args...) \
168 printk(KERN_WARNING "xen_net: " fmt, ##args)
170 static struct net_device *find_dev_by_handle(unsigned int handle)
171 {
172 struct list_head *ent;
173 struct net_private *np;
174 list_for_each ( ent, &dev_list )
175 {
176 np = list_entry(ent, struct net_private, list);
177 if ( np->handle == handle )
178 return np->dev;
179 }
180 return NULL;
181 }
183 /** Network interface info. */
184 struct netif_ctrl {
185 /** Number of interfaces. */
186 int interface_n;
187 /** Number of connected interfaces. */
188 int connected_n;
189 /** Error code. */
190 int err;
191 int up;
192 };
194 static struct netif_ctrl netctrl;
196 static void netctrl_init(void)
197 {
198 memset(&netctrl, 0, sizeof(netctrl));
199 netctrl.up = NETIF_DRIVER_STATUS_DOWN;
200 }
202 /** Get or set a network interface error.
203 */
204 static int netctrl_err(int err)
205 {
206 if ( (err < 0) && !netctrl.err )
207 netctrl.err = err;
208 return netctrl.err;
209 }
211 /** Test if all network interfaces are connected.
212 *
213 * @return 1 if all connected, 0 if not, negative error code otherwise
214 */
215 static int netctrl_connected(void)
216 {
217 int ok;
219 if ( netctrl.err )
220 ok = netctrl.err;
221 else if ( netctrl.up == NETIF_DRIVER_STATUS_UP )
222 ok = (netctrl.connected_n == netctrl.interface_n);
223 else
224 ok = 0;
226 return ok;
227 }
229 /** Count the connected network interfaces.
230 *
231 * @return connected count
232 */
233 static int netctrl_connected_count(void)
234 {
236 struct list_head *ent;
237 struct net_private *np;
238 unsigned int connected;
240 connected = 0;
242 list_for_each(ent, &dev_list) {
243 np = list_entry(ent, struct net_private, list);
244 if (np->backend_state == BEST_CONNECTED)
245 connected++;
246 }
248 netctrl.connected_n = connected;
249 DPRINTK("> connected_n=%d interface_n=%d\n",
250 netctrl.connected_n, netctrl.interface_n);
251 return connected;
252 }
254 /** Send a packet on a net device to encourage switches to learn the
255 * MAC. We send a fake ARP request.
256 *
257 * @param dev device
258 * @return 0 on success, error code otherwise
259 */
260 static int vif_wake(struct net_device *dev)
261 {
262 struct sk_buff *skb;
263 u32 src_ip, dst_ip;
265 dst_ip = INADDR_BROADCAST;
266 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
268 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
269 dst_ip, dev, src_ip,
270 /*dst_hw*/ NULL, /*src_hw*/ NULL,
271 /*target_hw*/ dev->dev_addr);
272 if ( skb == NULL )
273 return -ENOMEM;
275 return dev_queue_xmit(skb);
276 }
278 static int network_open(struct net_device *dev)
279 {
280 struct net_private *np = dev->priv;
282 memset(&np->stats, 0, sizeof(np->stats));
284 np->user_state = UST_OPEN;
286 network_alloc_rx_buffers(dev);
287 np->rx->event = np->rx_resp_cons + 1;
289 netif_start_queue(dev);
291 return 0;
292 }
294 static void network_tx_buf_gc(struct net_device *dev)
295 {
296 NETIF_RING_IDX i, prod;
297 unsigned short id;
298 struct net_private *np = dev->priv;
299 struct sk_buff *skb;
301 if ( np->backend_state != BEST_CONNECTED )
302 return;
304 do {
305 prod = np->tx->resp_prod;
306 rmb(); /* Ensure we see responses up to 'rp'. */
308 for ( i = np->tx_resp_cons; i != prod; i++ )
309 {
310 id = np->tx->ring[MASK_NETIF_TX_IDX(i)].resp.id;
311 skb = np->tx_skbs[id];
312 ADD_ID_TO_FREELIST(np->tx_skbs, id);
313 dev_kfree_skb_irq(skb);
314 }
316 np->tx_resp_cons = prod;
318 /*
319 * Set a new event, then check for race with update of tx_cons. Note
320 * that it is essential to schedule a callback, no matter how few
321 * buffers are pending. Even if there is space in the transmit ring,
322 * higher layers may be blocked because too much data is outstanding:
323 * in such cases notification from Xen is likely to be the only kick
324 * that we'll get.
325 */
326 np->tx->event =
327 prod + ((np->tx->req_prod - prod) >> 1) + 1;
328 mb();
329 }
330 while ( prod != np->tx->resp_prod );
332 if ( np->tx_full &&
333 ((np->tx->req_prod - prod) < NETIF_TX_RING_SIZE) )
334 {
335 np->tx_full = 0;
336 if ( np->user_state == UST_OPEN )
337 netif_wake_queue(dev);
338 }
339 }
342 static void network_alloc_rx_buffers(struct net_device *dev)
343 {
344 unsigned short id;
345 struct net_private *np = dev->priv;
346 struct sk_buff *skb;
347 int i, batch_target;
348 NETIF_RING_IDX req_prod = np->rx->req_prod;
350 if ( unlikely(np->backend_state != BEST_CONNECTED) )
351 return;
353 /*
354 * Allocate skbuffs greedily, even though we batch updates to the
355 * receive ring. This creates a less bursty demand on the memory allocator,
356 * so should reduce the chance of failed allocation requests both for
357 * ourself and for other kernel subsystems.
358 */
359 batch_target = np->rx_target - (req_prod - np->rx_resp_cons);
360 for ( i = skb_queue_len(&np->rx_batch); i < batch_target; i++ )
361 {
362 if ( unlikely((skb = alloc_xen_skb(dev->mtu + RX_HEADROOM)) == NULL) )
363 break;
364 __skb_queue_tail(&np->rx_batch, skb);
365 }
367 /* Is the batch large enough to be worthwhile? */
368 if ( i < (np->rx_target/2) )
369 return;
371 for ( i = 0; ; i++ )
372 {
373 if ( (skb = __skb_dequeue(&np->rx_batch)) == NULL )
374 break;
376 skb->dev = dev;
378 id = GET_ID_FROM_FREELIST(np->rx_skbs);
380 np->rx_skbs[id] = skb;
382 np->rx->ring[MASK_NETIF_RX_IDX(req_prod + i)].req.id = id;
384 rx_pfn_array[i] = virt_to_machine(skb->head) >> PAGE_SHIFT;
386 /* Remove this page from pseudo phys map before passing back to Xen. */
387 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT]
388 = INVALID_P2M_ENTRY;
390 rx_mcl[i].op = __HYPERVISOR_update_va_mapping;
391 rx_mcl[i].args[0] = (unsigned long)skb->head >> PAGE_SHIFT;
392 rx_mcl[i].args[1] = 0;
393 rx_mcl[i].args[2] = 0;
394 }
396 /*
397 * We may have allocated buffers which have entries outstanding in the page
398 * update queue -- make sure we flush those first!
399 */
400 flush_page_update_queue();
402 /* After all PTEs have been zapped we blow away stale TLB entries. */
403 rx_mcl[i-1].args[2] = UVMF_FLUSH_TLB;
405 /* Give away a batch of pages. */
406 rx_mcl[i].op = __HYPERVISOR_dom_mem_op;
407 rx_mcl[i].args[0] = MEMOP_decrease_reservation;
408 rx_mcl[i].args[1] = (unsigned long)rx_pfn_array;
409 rx_mcl[i].args[2] = (unsigned long)i;
410 rx_mcl[i].args[3] = 0;
411 rx_mcl[i].args[4] = DOMID_SELF;
413 /* Tell the ballon driver what is going on. */
414 balloon_update_driver_allowance(i);
416 /* Zap PTEs and give away pages in one big multicall. */
417 (void)HYPERVISOR_multicall(rx_mcl, i+1);
419 /* Check return status of HYPERVISOR_dom_mem_op(). */
420 if ( unlikely(rx_mcl[i].args[5] != i) )
421 panic("Unable to reduce memory reservation\n");
423 /* Above is a suitable barrier to ensure backend will see requests. */
424 np->rx->req_prod = req_prod + i;
426 /* Adjust our floating fill target if we risked running out of buffers. */
427 if ( ((req_prod - np->rx->resp_prod) < (np->rx_target / 4)) &&
428 ((np->rx_target *= 2) > RX_MAX_TARGET) )
429 np->rx_target = RX_MAX_TARGET;
430 }
433 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
434 {
435 unsigned short id;
436 struct net_private *np = (struct net_private *)dev->priv;
437 netif_tx_request_t *tx;
438 NETIF_RING_IDX i;
440 if ( unlikely(np->tx_full) )
441 {
442 printk(KERN_ALERT "%s: full queue wasn't stopped!\n", dev->name);
443 netif_stop_queue(dev);
444 goto drop;
445 }
447 if ( unlikely((((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >=
448 PAGE_SIZE) )
449 {
450 struct sk_buff *nskb;
451 if ( unlikely((nskb = alloc_xen_skb(skb->len)) == NULL) )
452 goto drop;
453 skb_put(nskb, skb->len);
454 memcpy(nskb->data, skb->data, skb->len);
455 nskb->dev = skb->dev;
456 dev_kfree_skb(skb);
457 skb = nskb;
458 }
460 spin_lock_irq(&np->tx_lock);
462 if ( np->backend_state != BEST_CONNECTED )
463 {
464 spin_unlock_irq(&np->tx_lock);
465 goto drop;
466 }
468 i = np->tx->req_prod;
470 id = GET_ID_FROM_FREELIST(np->tx_skbs);
471 np->tx_skbs[id] = skb;
473 tx = &np->tx->ring[MASK_NETIF_TX_IDX(i)].req;
475 tx->id = id;
476 tx->addr = virt_to_machine(skb->data);
477 tx->size = skb->len;
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 {
486 np->tx_full = 1;
487 netif_stop_queue(dev);
488 }
490 spin_unlock_irq(&np->tx_lock);
492 np->stats.tx_bytes += skb->len;
493 np->stats.tx_packets++;
495 /* Only notify Xen if we really have to. */
496 mb();
497 if ( np->tx->TX_TEST_IDX == i )
498 notify_via_evtchn(np->evtchn);
500 return 0;
502 drop:
503 np->stats.tx_dropped++;
504 dev_kfree_skb(skb);
505 return 0;
506 }
509 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
510 {
511 struct net_device *dev = dev_id;
512 struct net_private *np = dev->priv;
513 unsigned long flags;
515 spin_lock_irqsave(&np->tx_lock, flags);
516 network_tx_buf_gc(dev);
517 spin_unlock_irqrestore(&np->tx_lock, flags);
519 if ( (np->rx_resp_cons != np->rx->resp_prod) &&
520 (np->user_state == UST_OPEN) )
521 netif_rx_schedule(dev);
523 return IRQ_HANDLED;
524 }
527 static int netif_poll(struct net_device *dev, int *pbudget)
528 {
529 struct net_private *np = dev->priv;
530 struct sk_buff *skb, *nskb;
531 netif_rx_response_t *rx;
532 NETIF_RING_IDX i, rp;
533 mmu_update_t *mmu = rx_mmu;
534 multicall_entry_t *mcl = rx_mcl;
535 int work_done, budget, more_to_do = 1;
536 struct sk_buff_head rxq;
537 unsigned long flags;
539 spin_lock(&np->rx_lock);
541 if ( np->backend_state != BEST_CONNECTED )
542 {
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;
552 rp = np->rx->resp_prod;
553 rmb(); /* Ensure we see queued responses up to 'rp'. */
555 for ( i = np->rx_resp_cons, work_done = 0;
556 (i != rp) && (work_done < budget);
557 i++, work_done++ )
558 {
559 rx = &np->rx->ring[MASK_NETIF_RX_IDX(i)].resp;
561 /*
562 * An error here is very odd. Usually indicates a backend bug,
563 * low-memory condition, or that we didn't have reservation headroom.
564 */
565 if ( unlikely(rx->status <= 0) )
566 {
567 if ( net_ratelimit() )
568 printk(KERN_WARNING "Bad rx buffer (memory squeeze?).\n");
569 np->rx->ring[MASK_NETIF_RX_IDX(np->rx->req_prod)].req.id = rx->id;
570 wmb();
571 np->rx->req_prod++;
572 work_done--;
573 continue;
574 }
576 skb = np->rx_skbs[rx->id];
577 ADD_ID_TO_FREELIST(np->rx_skbs, rx->id);
579 /* NB. We handle skb overflow later. */
580 skb->data = skb->head + (rx->addr & ~PAGE_MASK);
581 skb->len = rx->status;
582 skb->tail = skb->data + skb->len;
584 np->stats.rx_packets++;
585 np->stats.rx_bytes += rx->status;
587 /* Remap the page. */
588 mmu->ptr = (rx->addr & PAGE_MASK) | MMU_MACHPHYS_UPDATE;
589 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
590 mmu++;
591 mcl->op = __HYPERVISOR_update_va_mapping;
592 mcl->args[0] = (unsigned long)skb->head >> PAGE_SHIFT;
593 mcl->args[1] = (rx->addr & PAGE_MASK) | __PAGE_KERNEL;
594 mcl->args[2] = 0;
595 mcl++;
597 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] =
598 rx->addr >> PAGE_SHIFT;
600 __skb_queue_tail(&rxq, skb);
601 }
603 /* Some pages are no longer absent... */
604 balloon_update_driver_allowance(-work_done);
606 /* Do all the remapping work, and M->P updates, in one big hypercall. */
607 if ( likely((mcl - rx_mcl) != 0) )
608 {
609 mcl->op = __HYPERVISOR_mmu_update;
610 mcl->args[0] = (unsigned long)rx_mmu;
611 mcl->args[1] = mmu - rx_mmu;
612 mcl->args[2] = 0;
613 mcl++;
614 (void)HYPERVISOR_multicall(rx_mcl, mcl - rx_mcl);
615 }
617 while ( (skb = __skb_dequeue(&rxq)) != NULL )
618 {
619 /*
620 * Enough room in skbuff for the data we were passed? Also, Linux
621 * expects at least 16 bytes headroom in each receive buffer.
622 */
623 if ( unlikely(skb->tail > skb->end) ||
624 unlikely((skb->data - skb->head) < 16) )
625 {
626 nskb = NULL;
628 /* Only copy the packet if it fits in the current MTU. */
629 if ( skb->len <= (dev->mtu + ETH_HLEN) )
630 {
631 if ( (skb->tail > skb->end) && net_ratelimit() )
632 printk(KERN_INFO "Received packet needs %d bytes more "
633 "headroom.\n", skb->tail - skb->end);
635 if ( (nskb = alloc_xen_skb(skb->len + 2)) != NULL )
636 {
637 skb_reserve(nskb, 2);
638 skb_put(nskb, skb->len);
639 memcpy(nskb->data, skb->data, skb->len);
640 nskb->dev = skb->dev;
641 }
642 }
643 else if ( net_ratelimit() )
644 printk(KERN_INFO "Received packet too big for MTU "
645 "(%d > %d)\n", skb->len - ETH_HLEN, dev->mtu);
647 /* Reinitialise and then destroy the old skbuff. */
648 skb->len = 0;
649 skb->tail = skb->data;
650 init_skb_shinfo(skb);
651 dev_kfree_skb(skb);
653 /* Switch old for new, if we copied the buffer. */
654 if ( (skb = nskb) == NULL )
655 continue;
656 }
658 /* Set the shared-info area, which is hidden behind the real data. */
659 init_skb_shinfo(skb);
661 /* Ethernet-specific work. Delayed to here as it peeks the header. */
662 skb->protocol = eth_type_trans(skb, dev);
664 /* Pass it up. */
665 netif_receive_skb(skb);
666 dev->last_rx = jiffies;
667 }
669 np->rx_resp_cons = i;
671 /* If we get a callback with very few responses, reduce fill target. */
672 /* NB. Note exponential increase, linear decrease. */
673 if ( ((np->rx->req_prod - np->rx->resp_prod) > ((3*np->rx_target) / 4)) &&
674 (--np->rx_target < RX_MIN_TARGET) )
675 np->rx_target = RX_MIN_TARGET;
677 network_alloc_rx_buffers(dev);
679 *pbudget -= work_done;
680 dev->quota -= work_done;
682 if ( work_done < budget )
683 {
684 local_irq_save(flags);
686 np->rx->event = i + 1;
688 /* Deal with hypervisor racing our resetting of rx_event. */
689 mb();
690 if ( np->rx->resp_prod == i )
691 {
692 __netif_rx_complete(dev);
693 more_to_do = 0;
694 }
696 local_irq_restore(flags);
697 }
699 spin_unlock(&np->rx_lock);
701 return more_to_do;
702 }
705 static int network_close(struct net_device *dev)
706 {
707 struct net_private *np = dev->priv;
708 np->user_state = UST_CLOSED;
709 netif_stop_queue(np->dev);
710 return 0;
711 }
714 static struct net_device_stats *network_get_stats(struct net_device *dev)
715 {
716 struct net_private *np = (struct net_private *)dev->priv;
717 return &np->stats;
718 }
721 static void network_connect(struct net_device *dev,
722 netif_fe_interface_status_t *status)
723 {
724 struct net_private *np;
725 int i, requeue_idx;
726 netif_tx_request_t *tx;
728 np = dev->priv;
729 spin_lock_irq(&np->tx_lock);
730 spin_lock(&np->rx_lock);
732 /* Recovery procedure: */
734 /* Step 1: Reinitialise variables. */
735 np->rx_resp_cons = np->tx_resp_cons = np->tx_full = 0;
736 np->rx->event = np->tx->event = 1;
738 /* Step 2: Rebuild the RX and TX ring contents.
739 * NB. We could just free the queued TX packets now but we hope
740 * that sending them out might do some good. We have to rebuild
741 * the RX ring because some of our pages are currently flipped out
742 * so we can't just free the RX skbs.
743 * NB2. Freelist index entries are always going to be less than
744 * __PAGE_OFFSET, whereas pointers to skbs will always be equal or
745 * greater than __PAGE_OFFSET: we use this property to distinguish
746 * them.
747 */
749 /* Rebuild the TX buffer freelist and the TX ring itself.
750 * NB. This reorders packets. We could keep more private state
751 * to avoid this but maybe it doesn't matter so much given the
752 * interface has been down.
753 */
754 for ( requeue_idx = 0, i = 1; i <= NETIF_TX_RING_SIZE; i++ )
755 {
756 if ( (unsigned long)np->tx_skbs[i] >= __PAGE_OFFSET )
757 {
758 struct sk_buff *skb = np->tx_skbs[i];
760 tx = &np->tx->ring[requeue_idx++].req;
762 tx->id = i;
763 tx->addr = virt_to_machine(skb->data);
764 tx->size = skb->len;
766 np->stats.tx_bytes += skb->len;
767 np->stats.tx_packets++;
768 }
769 }
770 wmb();
771 np->tx->req_prod = requeue_idx;
773 /* Rebuild the RX buffer freelist and the RX ring itself. */
774 for ( requeue_idx = 0, i = 1; i <= NETIF_RX_RING_SIZE; i++ )
775 if ( (unsigned long)np->rx_skbs[i] >= __PAGE_OFFSET )
776 np->rx->ring[requeue_idx++].req.id = i;
777 wmb();
778 np->rx->req_prod = requeue_idx;
780 /* Step 3: All public and private state should now be sane. Get
781 * ready to start sending and receiving packets and give the driver
782 * domain a kick because we've probably just requeued some
783 * packets.
784 */
785 np->backend_state = BEST_CONNECTED;
786 wmb();
787 notify_via_evtchn(status->evtchn);
788 network_tx_buf_gc(dev);
790 if ( np->user_state == UST_OPEN )
791 netif_start_queue(dev);
793 spin_unlock(&np->rx_lock);
794 spin_unlock_irq(&np->tx_lock);
795 }
797 static void vif_show(struct net_private *np)
798 {
799 #if DEBUG
800 if (np) {
801 IPRINTK("<vif handle=%u %s(%s) evtchn=%u irq=%u tx=%p rx=%p>\n",
802 np->handle,
803 be_state_name[np->backend_state],
804 np->user_state ? "open" : "closed",
805 np->evtchn,
806 np->irq,
807 np->tx,
808 np->rx);
809 } else {
810 IPRINTK("<vif NULL>\n");
811 }
812 #endif
813 }
815 /* Send a connect message to xend to tell it to bring up the interface. */
816 static void send_interface_connect(struct net_private *np)
817 {
818 ctrl_msg_t cmsg = {
819 .type = CMSG_NETIF_FE,
820 .subtype = CMSG_NETIF_FE_INTERFACE_CONNECT,
821 .length = sizeof(netif_fe_interface_connect_t),
822 };
823 netif_fe_interface_connect_t *msg = (void*)cmsg.msg;
825 DPRINTK(">\n"); vif_show(np);
826 msg->handle = np->handle;
827 msg->tx_shmem_frame = (virt_to_machine(np->tx) >> PAGE_SHIFT);
828 msg->rx_shmem_frame = (virt_to_machine(np->rx) >> PAGE_SHIFT);
830 ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
831 DPRINTK("<\n");
832 }
834 /* Send a driver status notification to the domain controller. */
835 static int send_driver_status(int ok)
836 {
837 int err = 0;
838 ctrl_msg_t cmsg = {
839 .type = CMSG_NETIF_FE,
840 .subtype = CMSG_NETIF_FE_DRIVER_STATUS,
841 .length = sizeof(netif_fe_driver_status_t),
842 };
843 netif_fe_driver_status_t *msg = (void*)cmsg.msg;
845 msg->status = (ok ? NETIF_DRIVER_STATUS_UP : NETIF_DRIVER_STATUS_DOWN);
846 err = ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
847 return err;
848 }
850 /* Stop network device and free tx/rx queues and irq.
851 */
852 static void vif_release(struct net_private *np)
853 {
854 /* Stop old i/f to prevent errors whilst we rebuild the state. */
855 spin_lock_irq(&np->tx_lock);
856 spin_lock(&np->rx_lock);
857 netif_stop_queue(np->dev);
858 /* np->backend_state = BEST_DISCONNECTED; */
859 spin_unlock(&np->rx_lock);
860 spin_unlock_irq(&np->tx_lock);
862 /* Free resources. */
863 if(np->tx != NULL){
864 free_irq(np->irq, np->dev);
865 unbind_evtchn_from_irq(np->evtchn);
866 free_page((unsigned long)np->tx);
867 free_page((unsigned long)np->rx);
868 np->irq = 0;
869 np->evtchn = 0;
870 np->tx = NULL;
871 np->rx = NULL;
872 }
873 }
875 /* Release vif resources and close it down completely.
876 */
877 static void vif_close(struct net_private *np)
878 {
879 DPRINTK(">\n"); vif_show(np);
880 WPRINTK("Unexpected netif-CLOSED message in state %s\n",
881 be_state_name[np->backend_state]);
882 vif_release(np);
883 np->backend_state = BEST_CLOSED;
884 /* todo: take dev down and free. */
885 vif_show(np); DPRINTK("<\n");
886 }
888 /* Move the vif into disconnected state.
889 * Allocates tx/rx pages.
890 * Sends connect message to xend.
891 */
892 static void vif_disconnect(struct net_private *np){
893 DPRINTK(">\n");
894 if(np->tx) free_page((unsigned long)np->tx);
895 if(np->rx) free_page((unsigned long)np->rx);
896 // Before this np->tx and np->rx had better be null.
897 np->tx = (netif_tx_interface_t *)__get_free_page(GFP_KERNEL);
898 np->rx = (netif_rx_interface_t *)__get_free_page(GFP_KERNEL);
899 memset(np->tx, 0, PAGE_SIZE);
900 memset(np->rx, 0, PAGE_SIZE);
901 np->backend_state = BEST_DISCONNECTED;
902 send_interface_connect(np);
903 vif_show(np); DPRINTK("<\n");
904 }
906 /* Begin interface recovery.
907 *
908 * NB. Whilst we're recovering, we turn the carrier state off. We
909 * take measures to ensure that this device isn't used for
910 * anything. We also stop the queue for this device. Various
911 * different approaches (e.g. continuing to buffer packets) have
912 * been tested but don't appear to improve the overall impact on
913 * TCP connections.
914 *
915 * TODO: (MAW) Change the Xend<->Guest protocol so that a recovery
916 * is initiated by a special "RESET" message - disconnect could
917 * just mean we're not allowed to use this interface any more.
918 */
919 static void
920 vif_reset(
921 struct net_private *np)
922 {
923 DPRINTK(">\n");
924 IPRINTK("Attempting to reconnect network interface: handle=%u\n",
925 np->handle);
926 vif_release(np);
927 vif_disconnect(np);
928 vif_show(np); DPRINTK("<\n");
929 }
931 /* Move the vif into connected state.
932 * Sets the mac and event channel from the message.
933 * Binds the irq to the event channel.
934 */
935 static void
936 vif_connect(
937 struct net_private *np, netif_fe_interface_status_t *status)
938 {
939 struct net_device *dev = np->dev;
940 DPRINTK(">\n");
941 memcpy(dev->dev_addr, status->mac, ETH_ALEN);
942 network_connect(dev, status);
943 np->evtchn = status->evtchn;
944 np->irq = bind_evtchn_to_irq(np->evtchn);
945 (void)request_irq(np->irq, netif_int, SA_SAMPLE_RANDOM, dev->name, dev);
946 netctrl_connected_count();
947 vif_wake(dev);
948 vif_show(np); DPRINTK("<\n");
949 }
952 /** Create a network device.
953 * @param handle device handle
954 * @param val return parameter for created device
955 * @return 0 on success, error code otherwise
956 */
957 static int create_netdev(int handle, struct net_device **val)
958 {
959 int i, err = 0;
960 struct net_device *dev = NULL;
961 struct net_private *np = NULL;
963 if ( (dev = alloc_etherdev(sizeof(struct net_private))) == NULL )
964 {
965 printk(KERN_WARNING "%s> alloc_etherdev failed.\n", __FUNCTION__);
966 err = -ENOMEM;
967 goto exit;
968 }
970 np = dev->priv;
971 np->backend_state = BEST_CLOSED;
972 np->user_state = UST_CLOSED;
973 np->handle = handle;
975 spin_lock_init(&np->tx_lock);
976 spin_lock_init(&np->rx_lock);
978 skb_queue_head_init(&np->rx_batch);
979 np->rx_target = RX_MIN_TARGET;
981 /* Initialise {tx,rx}_skbs to be a free chain containing every entry. */
982 for ( i = 0; i <= NETIF_TX_RING_SIZE; i++ )
983 np->tx_skbs[i] = (void *)(i+1);
984 for ( i = 0; i <= NETIF_RX_RING_SIZE; i++ )
985 np->rx_skbs[i] = (void *)(i+1);
987 dev->open = network_open;
988 dev->hard_start_xmit = network_start_xmit;
989 dev->stop = network_close;
990 dev->get_stats = network_get_stats;
991 dev->poll = netif_poll;
992 dev->weight = 64;
994 if ( (err = register_netdev(dev)) != 0 )
995 {
996 printk(KERN_WARNING "%s> register_netdev err=%d\n", __FUNCTION__, err);
997 goto exit;
998 }
999 np->dev = dev;
1000 list_add(&np->list, &dev_list);
1002 exit:
1003 if ( (err != 0) && (dev != NULL ) )
1004 kfree(dev);
1005 else if ( val != NULL )
1006 *val = dev;
1007 return err;
1010 /* Get the target interface for a status message.
1011 * Creates the interface when it makes sense.
1012 * The returned interface may be null when there is no error.
1014 * @param status status message
1015 * @param np return parameter for interface state
1016 * @return 0 on success, error code otherwise
1017 */
1018 static int
1019 target_vif(
1020 netif_fe_interface_status_t *status, struct net_private **np)
1022 int err = 0;
1023 struct net_device *dev;
1025 DPRINTK("> handle=%d\n", status->handle);
1026 if ( status->handle < 0 )
1028 err = -EINVAL;
1029 goto exit;
1032 if ( (dev = find_dev_by_handle(status->handle)) != NULL )
1033 goto exit;
1035 if ( status->status == NETIF_INTERFACE_STATUS_CLOSED )
1036 goto exit;
1037 if ( status->status == NETIF_INTERFACE_STATUS_CHANGED )
1038 goto exit;
1040 /* It's a new interface in a good state - create it. */
1041 DPRINTK("> create device...\n");
1042 if ( (err = create_netdev(status->handle, &dev)) != 0 )
1043 goto exit;
1045 netctrl.interface_n++;
1047 exit:
1048 if ( np != NULL )
1049 *np = ((dev && !err) ? dev->priv : NULL);
1050 DPRINTK("< err=%d\n", err);
1051 return err;
1054 /* Handle an interface status message. */
1055 static void netif_interface_status(netif_fe_interface_status_t *status)
1057 int err = 0;
1058 struct net_private *np = NULL;
1060 DPRINTK(">\n");
1061 DPRINTK("> status=%s handle=%d\n",
1062 status_name[status->status], status->handle);
1064 if ( (err = target_vif(status, &np)) != 0 )
1066 WPRINTK("Invalid netif: handle=%u\n", status->handle);
1067 return;
1070 if ( np == NULL )
1072 DPRINTK("> no vif\n");
1073 return;
1076 DPRINTK(">\n"); vif_show(np);
1078 switch ( status->status )
1080 case NETIF_INTERFACE_STATUS_CLOSED:
1081 switch ( np->backend_state )
1083 case BEST_CLOSED:
1084 case BEST_DISCONNECTED:
1085 case BEST_CONNECTED:
1086 vif_close(np);
1087 break;
1089 break;
1091 case NETIF_INTERFACE_STATUS_DISCONNECTED:
1092 switch ( np->backend_state )
1094 case BEST_CLOSED:
1095 vif_disconnect(np);
1096 break;
1097 case BEST_DISCONNECTED:
1098 case BEST_CONNECTED:
1099 vif_reset(np);
1100 break;
1102 break;
1104 case NETIF_INTERFACE_STATUS_CONNECTED:
1105 switch ( np->backend_state )
1107 case BEST_CLOSED:
1108 WPRINTK("Unexpected netif status %s in state %s\n",
1109 status_name[status->status],
1110 be_state_name[np->backend_state]);
1111 vif_disconnect(np);
1112 vif_connect(np, status);
1113 break;
1114 case BEST_DISCONNECTED:
1115 vif_connect(np, status);
1116 break;
1118 break;
1120 case NETIF_INTERFACE_STATUS_CHANGED:
1121 /*
1122 * The domain controller is notifying us that a device has been
1123 * added or removed.
1124 */
1125 break;
1127 default:
1128 WPRINTK("Invalid netif status code %d\n", status->status);
1129 break;
1131 vif_show(np);
1132 DPRINTK("<\n");
1135 /*
1136 * Initialize the network control interface.
1137 */
1138 static void netif_driver_status(netif_fe_driver_status_t *status)
1140 DPRINTK("> status=%d\n", status->status);
1141 netctrl.up = status->status;
1142 //netctrl.interface_n = status->max_handle;
1143 //netctrl.connected_n = 0;
1144 netctrl_connected_count();
1147 /* Receive handler for control messages. */
1148 static void netif_ctrlif_rx(ctrl_msg_t *msg, unsigned long id)
1151 switch ( msg->subtype )
1153 case CMSG_NETIF_FE_INTERFACE_STATUS:
1154 if ( msg->length != sizeof(netif_fe_interface_status_t) )
1155 goto error;
1156 netif_interface_status((netif_fe_interface_status_t *)
1157 &msg->msg[0]);
1158 break;
1160 case CMSG_NETIF_FE_DRIVER_STATUS:
1161 if ( msg->length != sizeof(netif_fe_driver_status_t) )
1162 goto error;
1163 netif_driver_status((netif_fe_driver_status_t *)
1164 &msg->msg[0]);
1165 break;
1167 error:
1168 default:
1169 msg->length = 0;
1170 break;
1173 ctrl_if_send_response(msg);
1177 #if 1
1178 /* Wait for all interfaces to be connected.
1180 * This works OK, but we'd like to use the probing mode (see below).
1181 */
1182 static int probe_interfaces(void)
1184 int err = 0, conn = 0;
1185 int wait_i, wait_n = 100;
1187 DPRINTK(">\n");
1189 for ( wait_i = 0; wait_i < wait_n; wait_i++)
1191 DPRINTK("> wait_i=%d\n", wait_i);
1192 conn = netctrl_connected();
1193 if(conn) break;
1194 DPRINTK("> schedule_timeout...\n");
1195 set_current_state(TASK_INTERRUPTIBLE);
1196 schedule_timeout(10);
1199 DPRINTK("> wait finished...\n");
1200 if ( conn <= 0 )
1202 err = netctrl_err(-ENETDOWN);
1203 WPRINTK("Failed to connect all virtual interfaces: err=%d\n", err);
1206 DPRINTK("< err=%d\n", err);
1208 return err;
1210 #else
1211 /* Probe for interfaces until no more are found.
1213 * This is the mode we'd like to use, but at the moment it panics the kernel.
1214 */
1215 static int probe_interfaces(void)
1217 int err = 0;
1218 int wait_i, wait_n = 100;
1219 ctrl_msg_t cmsg = {
1220 .type = CMSG_NETIF_FE,
1221 .subtype = CMSG_NETIF_FE_INTERFACE_STATUS,
1222 .length = sizeof(netif_fe_interface_status_t),
1223 };
1224 netif_fe_interface_status_t msg = {};
1225 ctrl_msg_t rmsg = {};
1226 netif_fe_interface_status_t *reply = (void*)rmsg.msg;
1227 int state = TASK_UNINTERRUPTIBLE;
1228 u32 query = -1;
1230 DPRINTK(">\n");
1232 netctrl.interface_n = 0;
1233 for ( wait_i = 0; wait_i < wait_n; wait_i++ )
1235 DPRINTK("> wait_i=%d query=%d\n", wait_i, query);
1236 msg.handle = query;
1237 memcpy(cmsg.msg, &msg, sizeof(msg));
1238 DPRINTK("> set_current_state...\n");
1239 set_current_state(state);
1240 DPRINTK("> rmsg=%p msg=%p, reply=%p\n", &rmsg, rmsg.msg, reply);
1241 DPRINTK("> sending...\n");
1242 err = ctrl_if_send_message_and_get_response(&cmsg, &rmsg, state);
1243 DPRINTK("> err=%d\n", err);
1244 if(err) goto exit;
1245 DPRINTK("> rmsg=%p msg=%p, reply=%p\n", &rmsg, rmsg.msg, reply);
1246 if((int)reply->handle < 0){
1247 // No more interfaces.
1248 break;
1250 query = -reply->handle - 2;
1251 DPRINTK(">netif_interface_status ...\n");
1252 netif_interface_status(reply);
1255 exit:
1256 if ( err )
1258 err = netctrl_err(-ENETDOWN);
1259 WPRINTK("Connecting virtual network interfaces failed: err=%d\n", err);
1262 DPRINTK("< err=%d\n", err);
1263 return err;
1266 #endif
1268 static int __init netif_init(void)
1270 int err = 0;
1272 if ( (xen_start_info.flags & SIF_INITDOMAIN) ||
1273 (xen_start_info.flags & SIF_NET_BE_DOMAIN) )
1274 return 0;
1276 IPRINTK("Initialising virtual ethernet driver.\n");
1277 INIT_LIST_HEAD(&dev_list);
1278 netctrl_init();
1279 (void)ctrl_if_register_receiver(CMSG_NETIF_FE, netif_ctrlif_rx,
1280 CALLBACK_IN_BLOCKING_CONTEXT);
1281 send_driver_status(1);
1282 err = probe_interfaces();
1283 if ( err )
1284 ctrl_if_unregister_receiver(CMSG_NETIF_FE, netif_ctrlif_rx);
1286 DPRINTK("< err=%d\n", err);
1287 return err;
1290 static void vif_suspend(struct net_private *np)
1292 // Avoid having tx/rx stuff happen until we're ready.
1293 DPRINTK(">\n");
1294 free_irq(np->irq, np->dev);
1295 unbind_evtchn_from_irq(np->evtchn);
1296 DPRINTK("<\n");
1299 static void vif_resume(struct net_private *np)
1301 // Connect regardless of whether IFF_UP flag set.
1302 // Stop bad things from happening until we're back up.
1303 DPRINTK(">\n");
1304 np->backend_state = BEST_DISCONNECTED;
1305 memset(np->tx, 0, PAGE_SIZE);
1306 memset(np->rx, 0, PAGE_SIZE);
1308 send_interface_connect(np);
1309 DPRINTK("<\n");
1312 void netif_suspend(void)
1314 #if 1 /* XXX THIS IS TEMPORARY */
1315 struct list_head *ent;
1316 struct net_private *np;
1318 DPRINTK(">\n");
1319 list_for_each(ent, &dev_list){
1320 np = list_entry(ent, struct net_private, list);
1321 vif_suspend(np);
1323 DPRINTK("<\n");
1324 #endif
1327 void netif_resume(void)
1329 #if 1
1330 /* XXX THIS IS TEMPORARY */
1331 struct list_head *ent;
1332 struct net_private *np;
1334 DPRINTK(">\n");
1335 list_for_each ( ent, &dev_list )
1337 np = list_entry(ent, struct net_private, list);
1338 vif_resume(np);
1340 DPRINTK("<\n");
1341 #endif
1345 __initcall(netif_init);