Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / netback / loopback.c
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  */
47
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>
54 #include <net/dst.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() */
58
59 static int nloopbacks = -1;
60 module_param(nloopbacks, int, 0);
61 MODULE_PARM_DESC(nloopbacks, "Number of netback-loopback devices to create");
62
63 struct net_private {
64         struct net_device *loopback_dev;
65         int loop_idx;
66 };
67
68 static inline struct net_private *loopback_priv(struct net_device *dev)
69 {
70         return netdev_priv(dev);
71 }
72
73 static int loopback_open(struct net_device *dev)
74 {
75         memset(&dev->stats, 0, sizeof(dev->stats));
76         netif_start_queue(dev);
77         return 0;
78 }
79
80 static int loopback_close(struct net_device *dev)
81 {
82         netif_stop_queue(dev);
83         return 0;
84 }
85
86 #ifdef CONFIG_X86
87 static int is_foreign(unsigned long pfn)
88 {
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));
92 }
93 #else
94 /* How to detect a foreign mapping? Play it safe. */
95 #define is_foreign(pfn) (1)
96 #endif
97
98 static int skb_remove_foreign_references(struct sk_buff *skb)
99 {
100         struct page *page;
101         unsigned long pfn;
102         int i, off;
103         char *vaddr;
104
105         BUG_ON(skb_shinfo(skb)->frag_list);
106
107         if (skb_cloned(skb) &&
108             unlikely(pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
109                 return 0;
110
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))
114                         continue;
115                 
116                 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
117                 if (unlikely(!page))
118                         return 0;
119
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,
123                        vaddr + off,
124                        skb_frag_size(&skb_shinfo(skb)->frags[i]));
125                 kunmap_skb_frag(vaddr);
126
127                 skb_frag_unref(skb, i);
128                 skb_frag_set_page(skb, i, page);
129         }
130
131         return 1;
132 }
133
134 static int loopback_start_xmit(struct sk_buff *skb, struct net_device *dev)
135 {
136         if (!skb_remove_foreign_references(skb)) {
137                 dev->stats.tx_dropped++;
138                 dev_kfree_skb(skb);
139                 return NETDEV_TX_OK;
140         }
141
142         dst_release(skb_dst(skb));
143         skb_dst_set(skb, NULL);
144
145         skb_orphan(skb);
146
147         dev->stats.tx_bytes += skb->len;
148         dev->stats.tx_packets++;
149
150         /* Switch to loopback context. */
151         dev = loopback_priv(dev)->loopback_dev;
152
153         dev->stats.rx_bytes += skb->len;
154         dev->stats.rx_packets++;
155
156         skb->pkt_type = PACKET_HOST; /* overridden by eth_type_trans() */
157         skb->protocol = eth_type_trans(skb, dev);
158
159         /* Flush netfilter context: rx'ed skbuffs not expected to have any. */
160         nf_reset(skb);
161         secpath_reset(skb);
162
163         netif_rx(skb);
164
165         return NETDEV_TX_OK;
166 }
167
168 static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
169 {
170         strcpy(info->driver, "netloop");
171         snprintf(info->bus_info, ETHTOOL_BUSINFO_LEN, "vif-0-%d",
172                  loopback_priv(dev)->loop_idx);
173 }
174
175 static const struct ethtool_ops network_ethtool_ops =
176 {
177         .get_drvinfo = get_drvinfo,
178         .get_link = ethtool_op_get_link,
179 };
180
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 */
186 };
187
188 static void loopback_construct(struct net_device *dev, struct net_device *lo,
189                                int loop_idx)
190 {
191         struct net_private *np = loopback_priv(dev);
192
193         np->loopback_dev     = lo;
194         np->loop_idx         = loop_idx;
195
196         dev->netdev_ops      = &loopback_netdev_ops;
197         dev->tx_queue_len    = 0;
198
199         dev->features        = (NETIF_F_HIGHDMA |
200                                 NETIF_F_LLTX |
201                                 NETIF_F_TSO |
202                                 NETIF_F_SG |
203                                 NETIF_F_IP_CSUM);
204
205         SET_ETHTOOL_OPS(dev, &network_ethtool_ops);
206
207         /*
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'
213          * command.
214          */
215         /*dev->mtu             = 16*1024;*/
216 }
217
218 static int __init make_loopback(int i)
219 {
220         struct net_device *dev1, *dev2;
221         char dev_name[IFNAMSIZ];
222         int err = -ENOMEM;
223
224         sprintf(dev_name, "vif0.%d", i);
225         dev1 = alloc_netdev(sizeof(struct net_private), dev_name, ether_setup);
226         if (!dev1)
227                 return err;
228
229         sprintf(dev_name, "veth%d", i);
230         dev2 = alloc_netdev(sizeof(struct net_private), dev_name, ether_setup);
231         if (!dev2)
232                 goto fail_netdev2;
233
234         loopback_construct(dev1, dev2, i);
235         loopback_construct(dev2, dev1, i);
236
237         /*
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.
241          */
242         memset(dev1->dev_addr, 0xFF, ETH_ALEN);
243         dev1->dev_addr[0] &= ~0x01;
244
245         if ((err = register_netdev(dev1)) != 0)
246                 goto fail;
247
248         if ((err = register_netdev(dev2)) != 0) {
249                 unregister_netdev(dev1);
250                 goto fail;
251         }
252
253         return 0;
254
255  fail:
256         free_netdev(dev2);
257  fail_netdev2:
258         free_netdev(dev1);
259         return err;
260 }
261
262 static int __init loopback_init(void)
263 {
264         int i, err = 0;
265
266         if (nloopbacks == -1)
267                 nloopbacks = is_initial_xendomain() ? 4 : 0;
268
269         for (i = 0; i < nloopbacks; i++)
270                 if ((err = make_loopback(i)) != 0)
271                         break;
272
273         return err;
274 }
275
276 module_init(loopback_init);
277
278 MODULE_LICENSE("Dual BSD/GPL");