1 /******************************************************************************
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".
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'.
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).
21 * Copyright (c) 2005 K A Fraser
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:
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:
36 * The above copyright notice and this permission notice shall be included in
37 * all copies or substantial portions of the Software.
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
48 #include <linux/module.h>
49 #include <linux/netdevice.h>
50 #include <linux/inetdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/skbuff.h>
53 #include <linux/ethtool.h>
55 #include <net/xfrm.h> /* secpath_reset() */
56 #include <asm/hypervisor.h> /* is_initial_xendomain() */
57 #include <../net/core/kmap_skb.h> /* k{,un}map_skb_frag() */
59 static int nloopbacks = -1;
60 module_param(nloopbacks, int, 0);
61 MODULE_PARM_DESC(nloopbacks, "Number of netback-loopback devices to create");
64 struct net_device *loopback_dev;
68 static inline struct net_private *loopback_priv(struct net_device *dev)
70 return netdev_priv(dev);
73 static int loopback_open(struct net_device *dev)
75 memset(&dev->stats, 0, sizeof(dev->stats));
76 netif_start_queue(dev);
80 static int loopback_close(struct net_device *dev)
82 netif_stop_queue(dev);
87 static int is_foreign(unsigned long pfn)
89 /* NB. Play it safe for auto-translation mode. */
90 return (xen_feature(XENFEAT_auto_translated_physmap) ||
91 (phys_to_machine_mapping[pfn] & FOREIGN_FRAME_BIT));
94 /* How to detect a foreign mapping? Play it safe. */
95 #define is_foreign(pfn) (1)
98 static int skb_remove_foreign_references(struct sk_buff *skb)
105 BUG_ON(skb_shinfo(skb)->frag_list);
107 if (skb_cloned(skb) &&
108 unlikely(pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
111 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
112 pfn = page_to_pfn(skb_frag_page(&skb_shinfo(skb)->frags[i]));
113 if (!is_foreign(pfn))
116 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
120 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
121 off = skb_shinfo(skb)->frags[i].page_offset;
122 memcpy(page_address(page) + off,
124 skb_frag_size(&skb_shinfo(skb)->frags[i]));
125 kunmap_skb_frag(vaddr);
127 skb_frag_unref(skb, i);
128 skb_frag_set_page(skb, i, page);
134 static int loopback_start_xmit(struct sk_buff *skb, struct net_device *dev)
136 if (!skb_remove_foreign_references(skb)) {
137 dev->stats.tx_dropped++;
142 dst_release(skb_dst(skb));
143 skb_dst_set(skb, NULL);
147 dev->stats.tx_bytes += skb->len;
148 dev->stats.tx_packets++;
150 /* Switch to loopback context. */
151 dev = loopback_priv(dev)->loopback_dev;
153 dev->stats.rx_bytes += skb->len;
154 dev->stats.rx_packets++;
156 skb->pkt_type = PACKET_HOST; /* overridden by eth_type_trans() */
157 skb->protocol = eth_type_trans(skb, dev);
159 /* Flush netfilter context: rx'ed skbuffs not expected to have any. */
168 static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
170 strcpy(info->driver, "netloop");
171 snprintf(info->bus_info, ETHTOOL_BUSINFO_LEN, "vif-0-%d",
172 loopback_priv(dev)->loop_idx);
175 static const struct ethtool_ops network_ethtool_ops =
177 .get_drvinfo = get_drvinfo,
178 .get_link = ethtool_op_get_link,
181 static const struct net_device_ops loopback_netdev_ops = {
182 .ndo_open = loopback_open,
183 .ndo_stop = loopback_close,
184 .ndo_start_xmit = loopback_start_xmit,
185 .ndo_change_mtu = NULL, /* allow arbitrary mtu */
188 static void loopback_construct(struct net_device *dev, struct net_device *lo,
191 struct net_private *np = loopback_priv(dev);
193 np->loopback_dev = lo;
194 np->loop_idx = loop_idx;
196 dev->netdev_ops = &loopback_netdev_ops;
197 dev->tx_queue_len = 0;
199 dev->features = (NETIF_F_HIGHDMA |
205 SET_ETHTOOL_OPS(dev, &network_ethtool_ops);
208 * We do not set a jumbo MTU on the interface. Otherwise the network
209 * stack will try to send large packets that will get dropped by the
210 * Ethernet bridge (unless the physical Ethernet interface is
211 * configured to transfer jumbo packets). If a larger MTU is desired
212 * then the system administrator can specify it using the 'ifconfig'
215 /*dev->mtu = 16*1024;*/
218 static int __init make_loopback(int i)
220 struct net_device *dev1, *dev2;
221 char dev_name[IFNAMSIZ];
224 sprintf(dev_name, "vif0.%d", i);
225 dev1 = alloc_netdev(sizeof(struct net_private), dev_name, ether_setup);
229 sprintf(dev_name, "veth%d", i);
230 dev2 = alloc_netdev(sizeof(struct net_private), dev_name, ether_setup);
234 loopback_construct(dev1, dev2, i);
235 loopback_construct(dev2, dev1, i);
238 * Initialise a dummy MAC address for the 'dummy backend' interface. We
239 * choose the numerically largest non-broadcast address to prevent the
240 * address getting stolen by an Ethernet bridge for STP purposes.
242 memset(dev1->dev_addr, 0xFF, ETH_ALEN);
243 dev1->dev_addr[0] &= ~0x01;
245 if ((err = register_netdev(dev1)) != 0)
248 if ((err = register_netdev(dev2)) != 0) {
249 unregister_netdev(dev1);
262 static int __init loopback_init(void)
266 if (nloopbacks == -1)
267 nloopbacks = is_initial_xendomain() ? 4 : 0;
269 for (i = 0; i < nloopbacks; i++)
270 if ((err = make_loopback(i)) != 0)
276 module_init(loopback_init);
278 MODULE_LICENSE("Dual BSD/GPL");