datapath: Add module parameter to allow TSO with vlans.
[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 = skb->dev->features & skb->dev->vlan_features;
281
282                 err = vswitch_skb_checksum_setup(skb);
283                 if (unlikely(err)) {
284                         kfree_skb(skb);
285                         return 0;
286                 }
287
288                 if (!vlan_tso)
289                         features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
290                                       NETIF_F_UFO | NETIF_F_FSO);
291
292                 if (skb_is_gso(skb) &&
293                     (!skb_gso_ok(skb, features) ||
294                      unlikely(skb->ip_summed != CHECKSUM_PARTIAL))) {
295                         struct sk_buff *nskb;
296
297                         nskb = skb_gso_segment(skb, features);
298                         if (!nskb) {
299                                 if (unlikely(skb_cloned(skb) &&
300                                     pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
301                                         kfree_skb(skb);
302                                         return 0;
303                                 }
304
305                                 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
306                                 goto tag;
307                         }
308
309                         kfree_skb(skb);
310                         skb = nskb;
311                         if (IS_ERR(skb))
312                                 return 0;
313
314                         len = 0;
315                         do {
316                                 nskb = skb->next;
317                                 skb->next = NULL;
318
319                                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
320                                 if (likely(skb)) {
321                                         len += skb->len;
322                                         vlan_set_tci(skb, 0);
323                                         dev_queue_xmit(skb);
324                                 }
325
326                                 skb = nskb;
327                         } while (skb);
328
329                         return len;
330                 }
331
332 tag:
333                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
334                 if (unlikely(!skb))
335                         return 0;
336                 vlan_set_tci(skb, 0);
337         }
338 #endif
339
340         len = skb->len;
341         dev_queue_xmit(skb);
342
343         return len;
344 }
345
346 /* Returns null if this device is not attached to a datapath. */
347 struct vport *netdev_get_vport(struct net_device *dev)
348 {
349 #ifdef IFF_BRIDGE_PORT
350 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
351         if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
352 #else
353         if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))   
354 #endif
355                 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
356         else
357                 return NULL;
358 #else
359         return (struct vport *)rcu_dereference_rtnl(dev->br_port);
360 #endif
361 }
362
363 const struct vport_ops netdev_vport_ops = {
364         .type           = ODP_VPORT_TYPE_NETDEV,
365         .flags          = (VPORT_F_REQUIRED |
366                           (USE_VPORT_STATS ? VPORT_F_GEN_STATS : 0)),
367         .init           = netdev_init,
368         .exit           = netdev_exit,
369         .create         = netdev_create,
370         .destroy        = netdev_destroy,
371         .set_mtu        = netdev_set_mtu,
372         .set_addr       = netdev_set_addr,
373         .get_name       = netdev_get_name,
374         .get_addr       = netdev_get_addr,
375         .get_kobj       = netdev_get_kobj,
376         .get_stats      = netdev_get_stats,
377         .get_dev_flags  = netdev_get_dev_flags,
378         .is_running     = netdev_is_running,
379         .get_operstate  = netdev_get_operstate,
380         .get_ifindex    = netdev_get_ifindex,
381         .get_iflink     = netdev_get_iflink,
382         .get_mtu        = netdev_get_mtu,
383         .send           = netdev_send,
384 };
385
386 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
387 /*
388  * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
389  * the Linux bridge module on any released version of Linux, because there
390  * is only a single bridge hook function and only a single br_port member
391  * in struct net_device.
392  *
393  * Declaring and exporting this symbol enforces mutual exclusion.  The bridge
394  * module also exports the same symbol, so the module loader will refuse to
395  * load both modules at the same time (e.g. "bridge: exports duplicate symbol
396  * br_should_route_hook (owned by openvswitch_mod)").
397  *
398  * The use of "typeof" here avoids the need to track changes in the type of
399  * br_should_route_hook over various kernel versions.
400  */
401 typeof(br_should_route_hook) br_should_route_hook;
402 EXPORT_SYMBOL(br_should_route_hook);
403 #endif