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