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