Support vlan_group workaround implemented in XenServer kernels.
[sliver-openvswitch.git] / datapath / vport-netdev.c
1 /*
2  * Copyright (c) 2010, 2011 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include <linux/if_arp.h>
10 #include <linux/if_bridge.h>
11 #include <linux/if_vlan.h>
12 #include <linux/kernel.h>
13 #include <linux/llc.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/skbuff.h>
16
17 #include <net/llc.h>
18
19 #include "checksum.h"
20 #include "datapath.h"
21 #include "vlan.h"
22 #include "vport-internal_dev.h"
23 #include "vport-netdev.h"
24
25 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) && \
26     !defined(HAVE_VLAN_BUG_WORKAROUND)
27 #include <linux/module.h>
28
29 static int vlan_tso __read_mostly = 0;
30 module_param(vlan_tso, int, 0644);
31 MODULE_PARM_DESC(vlan_tso, "Enable TSO for VLAN packets");
32 #else
33 #define vlan_tso true
34 #endif
35
36 /* If the native device stats aren't 64 bit use the vport stats tracking instead. */
37 #define USE_VPORT_STATS (sizeof(((struct net_device_stats *)0)->rx_bytes) < sizeof(u64))
38
39 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
40
41 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
42 /* Called with rcu_read_lock and bottom-halves disabled. */
43 static struct sk_buff *netdev_frame_hook(struct sk_buff *skb)
44 {
45         struct vport *vport;
46
47         if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
48                 return skb;
49
50         vport = netdev_get_vport(skb->dev);
51
52         netdev_port_receive(vport, skb);
53
54         return NULL;
55 }
56 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
57 /*
58  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
59  * different set of devices!)
60  */
61 /* Called with rcu_read_lock and bottom-halves disabled. */
62 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
63                                          struct sk_buff *skb)
64 {
65         netdev_port_receive((struct vport *)p, skb);
66         return NULL;
67 }
68 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
69 /*
70  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
71  * different set of devices!)
72  */
73 /* Called with rcu_read_lock and bottom-halves disabled. */
74 static int netdev_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
75 {
76         netdev_port_receive((struct vport *)p, *pskb);
77         return 1;
78 }
79 #else
80 #error
81 #endif
82
83 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
84 static int netdev_init(void) { return 0; }
85 static void netdev_exit(void) { }
86 #else
87 static int netdev_init(void)
88 {
89         /* Hook into callback used by the bridge to intercept packets.
90          * Parasites we are. */
91         br_handle_frame_hook = netdev_frame_hook;
92
93         return 0;
94 }
95
96 static void netdev_exit(void)
97 {
98         br_handle_frame_hook = NULL;
99 }
100 #endif
101
102 static struct vport *netdev_create(const struct vport_parms *parms)
103 {
104         struct vport *vport;
105         struct netdev_vport *netdev_vport;
106         int err;
107
108         vport = vport_alloc(sizeof(struct netdev_vport), &netdev_vport_ops, parms);
109         if (IS_ERR(vport)) {
110                 err = PTR_ERR(vport);
111                 goto error;
112         }
113
114         netdev_vport = netdev_vport_priv(vport);
115
116         netdev_vport->dev = dev_get_by_name(&init_net, parms->name);
117         if (!netdev_vport->dev) {
118                 err = -ENODEV;
119                 goto error_free_vport;
120         }
121
122         if (netdev_vport->dev->flags & IFF_LOOPBACK ||
123             netdev_vport->dev->type != ARPHRD_ETHER ||
124             is_internal_dev(netdev_vport->dev)) {
125                 err = -EINVAL;
126                 goto error_put;
127         }
128
129         /* If we are using the vport stats layer initialize it to the current
130          * values so we are roughly consistent with the device stats. */
131         if (USE_VPORT_STATS) {
132                 struct rtnl_link_stats64 stats;
133
134                 err = netdev_get_stats(vport, &stats);
135                 if (!err)
136                         vport_set_stats(vport, &stats);
137         }
138
139         err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
140                                          vport);
141         if (err)
142                 goto error_put;
143
144         dev_set_promiscuity(netdev_vport->dev, 1);
145         dev_disable_lro(netdev_vport->dev);
146         netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
147
148         return vport;
149
150 error_put:
151         dev_put(netdev_vport->dev);
152 error_free_vport:
153         vport_free(vport);
154 error:
155         return ERR_PTR(err);
156 }
157
158 static int netdev_destroy(struct vport *vport)
159 {
160         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
161
162         netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
163         netdev_rx_handler_unregister(netdev_vport->dev);
164         dev_set_promiscuity(netdev_vport->dev, -1);
165
166         synchronize_rcu();
167
168         dev_put(netdev_vport->dev);
169         vport_free(vport);
170
171         return 0;
172 }
173
174 int netdev_set_mtu(struct vport *vport, int mtu)
175 {
176         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
177         return dev_set_mtu(netdev_vport->dev, mtu);
178 }
179
180 int netdev_set_addr(struct vport *vport, const unsigned char *addr)
181 {
182         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
183         struct sockaddr sa;
184
185         sa.sa_family = ARPHRD_ETHER;
186         memcpy(sa.sa_data, addr, ETH_ALEN);
187
188         return dev_set_mac_address(netdev_vport->dev, &sa);
189 }
190
191 const char *netdev_get_name(const struct vport *vport)
192 {
193         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
194         return netdev_vport->dev->name;
195 }
196
197 const unsigned char *netdev_get_addr(const struct vport *vport)
198 {
199         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
200         return netdev_vport->dev->dev_addr;
201 }
202
203 struct kobject *netdev_get_kobj(const struct vport *vport)
204 {
205         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
206         return &netdev_vport->dev->NETDEV_DEV_MEMBER.kobj;
207 }
208
209 int netdev_get_stats(const struct vport *vport, struct rtnl_link_stats64 *stats)
210 {
211         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
212         dev_get_stats(netdev_vport->dev, stats);
213         return 0;
214 }
215
216 unsigned netdev_get_dev_flags(const struct vport *vport)
217 {
218         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
219         return dev_get_flags(netdev_vport->dev);
220 }
221
222 int netdev_is_running(const struct vport *vport)
223 {
224         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
225         return netif_running(netdev_vport->dev);
226 }
227
228 unsigned char netdev_get_operstate(const struct vport *vport)
229 {
230         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
231         return netdev_vport->dev->operstate;
232 }
233
234 int netdev_get_ifindex(const struct vport *vport)
235 {
236         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
237         return netdev_vport->dev->ifindex;
238 }
239
240 int netdev_get_iflink(const struct vport *vport)
241 {
242         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
243         return netdev_vport->dev->iflink;
244 }
245
246 int netdev_get_mtu(const struct vport *vport)
247 {
248         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
249         return netdev_vport->dev->mtu;
250 }
251
252 /* Must be called with rcu_read_lock. */
253 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
254 {
255         /* Make our own copy of the packet.  Otherwise we will mangle the
256          * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
257          * (No one comes after us, since we tell handle_bridge() that we took
258          * the packet.) */
259         skb = skb_share_check(skb, GFP_ATOMIC);
260         if (unlikely(!skb))
261                 return;
262
263         skb_warn_if_lro(skb);
264
265         skb_push(skb, ETH_HLEN);
266         compute_ip_summed(skb, false);
267         vlan_copy_skb_tci(skb);
268
269         vport_receive(vport, skb);
270 }
271
272 static bool dev_supports_vlan_tx(struct net_device *dev)
273 {
274 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
275         /* Software fallback means every device supports vlan_tci on TX. */
276         return true;
277 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
278         return dev->features & NETIF_F_HW_VLAN_TX;
279 #else
280         /* Assume that the driver is buggy. */
281         return false;
282 #endif
283 }
284
285 static int netdev_send(struct vport *vport, struct sk_buff *skb)
286 {
287         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
288         int len;
289
290         skb->dev = netdev_vport->dev;
291         forward_ip_summed(skb);
292
293         if (vlan_tx_tag_present(skb) && !dev_supports_vlan_tx(skb->dev)) {
294                 int err;
295                 int features = 0;
296
297 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
298                 features = skb->dev->features & skb->dev->vlan_features;
299 #endif
300
301                 err = vswitch_skb_checksum_setup(skb);
302                 if (unlikely(err)) {
303                         kfree_skb(skb);
304                         return 0;
305                 }
306
307                 if (!vlan_tso)
308                         features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
309                                       NETIF_F_UFO | NETIF_F_FSO);
310
311                 if (skb_is_gso(skb) &&
312                     (!skb_gso_ok(skb, features) ||
313                      unlikely(skb->ip_summed != CHECKSUM_PARTIAL))) {
314                         struct sk_buff *nskb;
315
316                         nskb = skb_gso_segment(skb, features);
317                         if (!nskb) {
318                                 if (unlikely(skb_cloned(skb) &&
319                                     pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
320                                         kfree_skb(skb);
321                                         return 0;
322                                 }
323
324                                 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
325                                 goto tag;
326                         }
327
328                         kfree_skb(skb);
329                         skb = nskb;
330                         if (IS_ERR(skb))
331                                 return 0;
332
333                         len = 0;
334                         do {
335                                 nskb = skb->next;
336                                 skb->next = NULL;
337
338                                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
339                                 if (likely(skb)) {
340                                         len += skb->len;
341                                         vlan_set_tci(skb, 0);
342                                         dev_queue_xmit(skb);
343                                 }
344
345                                 skb = nskb;
346                         } while (skb);
347
348                         return len;
349                 }
350
351 tag:
352                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
353                 if (unlikely(!skb))
354                         return 0;
355                 vlan_set_tci(skb, 0);
356         }
357
358         len = skb->len;
359         dev_queue_xmit(skb);
360
361         return len;
362 }
363
364 /* Returns null if this device is not attached to a datapath. */
365 struct vport *netdev_get_vport(struct net_device *dev)
366 {
367 #ifdef IFF_BRIDGE_PORT
368 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
369         if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
370 #else
371         if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))   
372 #endif
373                 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
374         else
375                 return NULL;
376 #else
377         return (struct vport *)rcu_dereference_rtnl(dev->br_port);
378 #endif
379 }
380
381 const struct vport_ops netdev_vport_ops = {
382         .type           = ODP_VPORT_TYPE_NETDEV,
383         .flags          = (VPORT_F_REQUIRED |
384                           (USE_VPORT_STATS ? VPORT_F_GEN_STATS : 0)),
385         .init           = netdev_init,
386         .exit           = netdev_exit,
387         .create         = netdev_create,
388         .destroy        = netdev_destroy,
389         .set_mtu        = netdev_set_mtu,
390         .set_addr       = netdev_set_addr,
391         .get_name       = netdev_get_name,
392         .get_addr       = netdev_get_addr,
393         .get_kobj       = netdev_get_kobj,
394         .get_stats      = netdev_get_stats,
395         .get_dev_flags  = netdev_get_dev_flags,
396         .is_running     = netdev_is_running,
397         .get_operstate  = netdev_get_operstate,
398         .get_ifindex    = netdev_get_ifindex,
399         .get_iflink     = netdev_get_iflink,
400         .get_mtu        = netdev_get_mtu,
401         .send           = netdev_send,
402 };
403
404 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
405 /*
406  * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
407  * the Linux bridge module on any released version of Linux, because there
408  * is only a single bridge hook function and only a single br_port member
409  * in struct net_device.
410  *
411  * Declaring and exporting this symbol enforces mutual exclusion.  The bridge
412  * module also exports the same symbol, so the module loader will refuse to
413  * load both modules at the same time (e.g. "bridge: exports duplicate symbol
414  * br_should_route_hook (owned by openvswitch_mod)").
415  *
416  * The use of "typeof" here avoids the need to track changes in the type of
417  * br_should_route_hook over various kernel versions.
418  */
419 typeof(br_should_route_hook) br_should_route_hook;
420 EXPORT_SYMBOL(br_should_route_hook);
421 #endif