direct-io.hg

view linux-2.6.11-xen-sparse/drivers/xen/netback/loopback.c @ 4959:2c0074c64c33

bitkeeper revision 1.1159.258.134 (428a2efbVjatsyWzpCT8mcdj0YbvXw)

Do not build netfront/netback loopback by default.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Tue May 17 17:50:51 2005 +0000 (2005-05-17)
parents 328fbee2128c
children f849286c8a79 47166e28f279
line source
1 /******************************************************************************
2 * netback/loopback.c
3 *
4 * A two-interface loopback device to emulate a local netfront-netback
5 * connection. This ensures that local packet delivery looks identical
6 * to inter-domain delivery. Most importantly, packets delivered locally
7 * originating from other domains will get *copied* when they traverse this
8 * driver. This prevents unbounded delays in socket-buffer queues from
9 * causing the netback driver to "seize up".
10 *
11 * This driver creates a symmetric pair of loopback interfaces with names
12 * vif0.0 and veth0. The intention is that 'vif0.0' is bound to an Ethernet
13 * bridge, just like a proper netback interface, while a local IP interface
14 * is configured on 'veth0'.
15 *
16 * As with a real netback interface, vif0.0 is configured with a suitable
17 * dummy MAC address. No default is provided for veth0: a reasonable strategy
18 * is to transfer eth0's MAC address to veth0, and give eth0 a dummy address
19 * (to avoid confusing the Etherbridge).
20 *
21 * Copyright (c) 2005 K A Fraser
22 */
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/netdevice.h>
27 #include <linux/inetdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <net/dst.h>
32 struct net_private {
33 struct net_device *loopback_dev;
34 struct net_device_stats stats;
35 };
37 static int loopback_open(struct net_device *dev)
38 {
39 struct net_private *np = netdev_priv(dev);
40 memset(&np->stats, 0, sizeof(np->stats));
41 netif_start_queue(dev);
42 return 0;
43 }
45 static int loopback_close(struct net_device *dev)
46 {
47 netif_stop_queue(dev);
48 return 0;
49 }
51 static int loopback_start_xmit(struct sk_buff *skb, struct net_device *dev)
52 {
53 struct net_private *np = netdev_priv(dev);
55 dst_release(skb->dst);
56 skb->dst = NULL;
58 skb_orphan(skb);
60 np->stats.tx_bytes += skb->len;
61 np->stats.tx_packets++;
63 /* Switch to loopback context. */
64 dev = np->loopback_dev;
65 np = netdev_priv(dev);
67 np->stats.rx_bytes += skb->len;
68 np->stats.rx_packets++;
70 skb->pkt_type = PACKET_HOST; /* overridden by eth_type_trans() */
71 skb->protocol = eth_type_trans(skb, dev);
72 skb->dev = dev;
73 dev->last_rx = jiffies;
74 netif_rx(skb);
76 return 0;
77 }
79 static struct net_device_stats *loopback_get_stats(struct net_device *dev)
80 {
81 struct net_private *np = netdev_priv(dev);
82 return &np->stats;
83 }
85 static void loopback_construct(struct net_device *dev, struct net_device *lo)
86 {
87 struct net_private *np = netdev_priv(dev);
89 np->loopback_dev = lo;
91 dev->open = loopback_open;
92 dev->stop = loopback_close;
93 dev->hard_start_xmit = loopback_start_xmit;
94 dev->get_stats = loopback_get_stats;
96 dev->tx_queue_len = 0;
97 dev->mtu = 16*1024;
98 }
100 static int __init loopback_init(void)
101 {
102 struct net_device *dev1, *dev2;
103 int err = -ENOMEM;
105 dev1 = alloc_netdev(sizeof(struct net_private), "vif0.0", ether_setup);
106 dev2 = alloc_netdev(sizeof(struct net_private), "veth0", ether_setup);
107 if ( (dev1 == NULL) || (dev2 == NULL) )
108 goto fail;
110 loopback_construct(dev1, dev2);
111 loopback_construct(dev2, dev1);
113 /*
114 * Initialise a dummy MAC address for the 'dummy backend' interface. We
115 * choose the numerically largest non-broadcast address to prevent the
116 * address getting stolen by an Ethernet bridge for STP purposes.
117 */
118 memset(dev1->dev_addr, 0xFF, ETH_ALEN);
119 dev1->dev_addr[0] &= ~0x01;
121 if ( (err = register_netdev(dev1)) != 0 )
122 goto fail;
124 if ( (err = register_netdev(dev2)) != 0 )
125 {
126 unregister_netdev(dev1);
127 goto fail;
128 }
130 return 0;
132 fail:
133 if ( dev1 != NULL )
134 kfree(dev1);
135 if ( dev2 != NULL )
136 kfree(dev2);
137 return err;
138 }
140 module_init(loopback_init);