ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netback/loopback.c @ 11662:8cffe84d8f51

[NET] back: Create 0 loopbacks by default for non-domain-0.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Thu Sep 28 13:20:58 2006 +0100 (2006-09-28)
parents f8c53e937b30
children 14111cab38fd
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 *
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License version 2
25 * as published by the Free Software Foundation; or, when distributed
26 * separately from the Linux kernel or incorporated into other
27 * software packages, subject to the following license:
28 *
29 * Permission is hereby granted, free of charge, to any person obtaining a copy
30 * of this source file (the "Software"), to deal in the Software without
31 * restriction, including without limitation the rights to use, copy, modify,
32 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
33 * and to permit persons to whom the Software is furnished to do so, subject to
34 * the following conditions:
35 *
36 * The above copyright notice and this permission notice shall be included in
37 * all copies or substantial portions of the Software.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
44 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
45 * IN THE SOFTWARE.
46 */
48 #include <linux/config.h>
49 #include <linux/module.h>
50 #include <linux/netdevice.h>
51 #include <linux/inetdevice.h>
52 #include <linux/etherdevice.h>
53 #include <linux/skbuff.h>
54 #include <linux/ethtool.h>
55 #include <net/dst.h>
56 #include <asm/hypervisor.h> /* is_initial_xendomain() */
58 static int nloopbacks = -1;
59 module_param(nloopbacks, int, 0);
60 MODULE_PARM_DESC(nloopbacks, "Number of netback-loopback devices to create");
62 struct net_private {
63 struct net_device *loopback_dev;
64 struct net_device_stats stats;
65 };
67 static int loopback_open(struct net_device *dev)
68 {
69 struct net_private *np = netdev_priv(dev);
70 memset(&np->stats, 0, sizeof(np->stats));
71 netif_start_queue(dev);
72 return 0;
73 }
75 static int loopback_close(struct net_device *dev)
76 {
77 netif_stop_queue(dev);
78 return 0;
79 }
81 static int loopback_start_xmit(struct sk_buff *skb, struct net_device *dev)
82 {
83 struct net_private *np = netdev_priv(dev);
85 dst_release(skb->dst);
86 skb->dst = NULL;
88 skb_orphan(skb);
90 np->stats.tx_bytes += skb->len;
91 np->stats.tx_packets++;
93 /* Switch to loopback context. */
94 dev = np->loopback_dev;
95 np = netdev_priv(dev);
97 np->stats.rx_bytes += skb->len;
98 np->stats.rx_packets++;
100 if (skb->ip_summed == CHECKSUM_HW) {
101 /* Defer checksum calculation. */
102 skb->proto_csum_blank = 1;
103 /* Must be a local packet: assert its integrity. */
104 skb->proto_data_valid = 1;
105 }
107 skb->ip_summed = skb->proto_data_valid ?
108 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
110 skb->pkt_type = PACKET_HOST; /* overridden by eth_type_trans() */
111 skb->protocol = eth_type_trans(skb, dev);
112 skb->dev = dev;
113 dev->last_rx = jiffies;
114 netif_rx(skb);
116 return 0;
117 }
119 static struct net_device_stats *loopback_get_stats(struct net_device *dev)
120 {
121 struct net_private *np = netdev_priv(dev);
122 return &np->stats;
123 }
125 static struct ethtool_ops network_ethtool_ops =
126 {
127 .get_tx_csum = ethtool_op_get_tx_csum,
128 .set_tx_csum = ethtool_op_set_tx_csum,
129 .get_sg = ethtool_op_get_sg,
130 .set_sg = ethtool_op_set_sg,
131 .get_tso = ethtool_op_get_tso,
132 .set_tso = ethtool_op_set_tso,
133 .get_link = ethtool_op_get_link,
134 };
136 /*
137 * Nothing to do here. Virtual interface is point-to-point and the
138 * physical interface is probably promiscuous anyway.
139 */
140 static void loopback_set_multicast_list(struct net_device *dev)
141 {
142 }
144 static void loopback_construct(struct net_device *dev, struct net_device *lo)
145 {
146 struct net_private *np = netdev_priv(dev);
148 np->loopback_dev = lo;
150 dev->open = loopback_open;
151 dev->stop = loopback_close;
152 dev->hard_start_xmit = loopback_start_xmit;
153 dev->get_stats = loopback_get_stats;
154 dev->set_multicast_list = loopback_set_multicast_list;
155 dev->change_mtu = NULL; /* allow arbitrary mtu */
157 dev->tx_queue_len = 0;
159 dev->features = (NETIF_F_HIGHDMA |
160 NETIF_F_LLTX |
161 NETIF_F_TSO |
162 NETIF_F_SG |
163 NETIF_F_IP_CSUM);
165 SET_ETHTOOL_OPS(dev, &network_ethtool_ops);
167 /*
168 * We do not set a jumbo MTU on the interface. Otherwise the network
169 * stack will try to send large packets that will get dropped by the
170 * Ethernet bridge (unless the physical Ethernet interface is
171 * configured to transfer jumbo packets). If a larger MTU is desired
172 * then the system administrator can specify it using the 'ifconfig'
173 * command.
174 */
175 /*dev->mtu = 16*1024;*/
176 }
178 static int __init make_loopback(int i)
179 {
180 struct net_device *dev1, *dev2;
181 char dev_name[IFNAMSIZ];
182 int err = -ENOMEM;
184 sprintf(dev_name, "vif0.%d", i);
185 dev1 = alloc_netdev(sizeof(struct net_private), dev_name, ether_setup);
186 if (!dev1)
187 return err;
189 sprintf(dev_name, "veth%d", i);
190 dev2 = alloc_netdev(sizeof(struct net_private), dev_name, ether_setup);
191 if (!dev2)
192 goto fail_netdev2;
194 loopback_construct(dev1, dev2);
195 loopback_construct(dev2, dev1);
197 /*
198 * Initialise a dummy MAC address for the 'dummy backend' interface. We
199 * choose the numerically largest non-broadcast address to prevent the
200 * address getting stolen by an Ethernet bridge for STP purposes.
201 */
202 memset(dev1->dev_addr, 0xFF, ETH_ALEN);
203 dev1->dev_addr[0] &= ~0x01;
205 if ((err = register_netdev(dev1)) != 0)
206 goto fail;
208 if ((err = register_netdev(dev2)) != 0) {
209 unregister_netdev(dev1);
210 goto fail;
211 }
213 return 0;
215 fail:
216 free_netdev(dev2);
217 fail_netdev2:
218 free_netdev(dev1);
219 return err;
220 }
222 static void __exit clean_loopback(int i)
223 {
224 struct net_device *dev1, *dev2;
225 char dev_name[IFNAMSIZ];
227 sprintf(dev_name, "vif0.%d", i);
228 dev1 = dev_get_by_name(dev_name);
229 sprintf(dev_name, "veth%d", i);
230 dev2 = dev_get_by_name(dev_name);
231 if (dev1 && dev2) {
232 unregister_netdev(dev2);
233 unregister_netdev(dev1);
234 free_netdev(dev2);
235 free_netdev(dev1);
236 }
237 }
239 static int __init loopback_init(void)
240 {
241 int i, err = 0;
243 if (nloopbacks == -1)
244 nloopbacks = is_initial_xendomain() ? 8 : 0;
246 for (i = 0; i < nloopbacks; i++)
247 if ((err = make_loopback(i)) != 0)
248 break;
250 return err;
251 }
253 module_init(loopback_init);
255 static void __exit loopback_exit(void)
256 {
257 int i;
259 for (i = nloopbacks; i-- > 0; )
260 clean_loopback(i);
261 }
263 module_exit(loopback_exit);
265 MODULE_LICENSE("Dual BSD/GPL");