datapath: Use "OVS_*" as opposed to "ODP_*" for user<->kernel interactions.
[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,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
288         if (unlikely(compute_ip_summed(skb, false))) {
289                 kfree_skb(skb);
290                 return;
291         }
292         vlan_copy_skb_tci(skb);
293
294         vport_receive(vport, skb);
295 }
296
297 static bool dev_supports_vlan_tx(struct net_device *dev)
298 {
299 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
300         /* Software fallback means every device supports vlan_tci on TX. */
301         return true;
302 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
303         return dev->features & NETIF_F_HW_VLAN_TX;
304 #else
305         /* Assume that the driver is buggy. */
306         return false;
307 #endif
308 }
309
310 static int netdev_send(struct vport *vport, struct sk_buff *skb)
311 {
312         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
313         int len;
314
315         skb->dev = netdev_vport->dev;
316         forward_ip_summed(skb, true);
317
318         if (vlan_tx_tag_present(skb) && !dev_supports_vlan_tx(skb->dev)) {
319                 int features = 0;
320
321 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
322                 features = skb->dev->features & skb->dev->vlan_features;
323 #endif
324
325                 if (!vlan_tso)
326                         features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
327                                       NETIF_F_UFO | NETIF_F_FSO);
328
329                 if (skb_is_gso(skb) &&
330                     (!skb_gso_ok(skb, features) ||
331                      unlikely(skb->ip_summed != CHECKSUM_PARTIAL))) {
332                         struct sk_buff *nskb;
333
334                         nskb = skb_gso_segment(skb, features);
335                         if (!nskb) {
336                                 if (unlikely(skb_cloned(skb) &&
337                                     pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
338                                         kfree_skb(skb);
339                                         return 0;
340                                 }
341
342                                 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
343                                 goto tag;
344                         }
345
346                         if (IS_ERR(nskb)) {
347                                 kfree_skb(skb);
348                                 return 0;
349                         }
350                         consume_skb(skb);
351                         skb = nskb;
352
353                         len = 0;
354                         do {
355                                 nskb = skb->next;
356                                 skb->next = NULL;
357
358                                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
359                                 if (likely(skb)) {
360                                         len += skb->len;
361                                         vlan_set_tci(skb, 0);
362                                         dev_queue_xmit(skb);
363                                 }
364
365                                 skb = nskb;
366                         } while (skb);
367
368                         return len;
369                 }
370
371 tag:
372                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
373                 if (unlikely(!skb))
374                         return 0;
375                 vlan_set_tci(skb, 0);
376         }
377
378         len = skb->len;
379         dev_queue_xmit(skb);
380
381         return len;
382 }
383
384 /* Returns null if this device is not attached to a datapath. */
385 struct vport *netdev_get_vport(struct net_device *dev)
386 {
387 #ifdef IFF_BRIDGE_PORT
388 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
389         if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
390 #else
391         if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))   
392 #endif
393                 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
394         else
395                 return NULL;
396 #else
397         return (struct vport *)rcu_dereference_rtnl(dev->br_port);
398 #endif
399 }
400
401 const struct vport_ops netdev_vport_ops = {
402         .type           = OVS_VPORT_TYPE_NETDEV,
403         .flags          = (VPORT_F_REQUIRED |
404                           (USE_VPORT_STATS ? VPORT_F_GEN_STATS : 0)),
405         .init           = netdev_init,
406         .exit           = netdev_exit,
407         .create         = netdev_create,
408         .destroy        = netdev_destroy,
409         .set_mtu        = netdev_set_mtu,
410         .set_addr       = netdev_set_addr,
411         .get_name       = netdev_get_name,
412         .get_addr       = netdev_get_addr,
413         .get_kobj       = netdev_get_kobj,
414         .get_stats      = netdev_get_stats,
415         .get_dev_flags  = netdev_get_dev_flags,
416         .is_running     = netdev_is_running,
417         .get_operstate  = netdev_get_operstate,
418         .get_ifindex    = netdev_get_ifindex,
419         .get_iflink     = netdev_get_iflink,
420         .get_mtu        = netdev_get_mtu,
421         .send           = netdev_send,
422 };
423
424 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
425 /*
426  * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
427  * the Linux bridge module on any released version of Linux, because there
428  * is only a single bridge hook function and only a single br_port member
429  * in struct net_device.
430  *
431  * Declaring and exporting this symbol enforces mutual exclusion.  The bridge
432  * module also exports the same symbol, so the module loader will refuse to
433  * load both modules at the same time (e.g. "bridge: exports duplicate symbol
434  * br_should_route_hook (owned by openvswitch_mod)").
435  *
436  * The use of "typeof" here avoids the need to track changes in the type of
437  * br_should_route_hook over various kernel versions.
438  */
439 typeof(br_should_route_hook) br_should_route_hook;
440 EXPORT_SYMBOL(br_should_route_hook);
441 #endif