datapath: Fix coding style issues.
[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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/if_arp.h>
12 #include <linux/if_bridge.h>
13 #include <linux/if_vlan.h>
14 #include <linux/kernel.h>
15 #include <linux/llc.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/skbuff.h>
18
19 #include <net/llc.h>
20
21 #include "checksum.h"
22 #include "datapath.h"
23 #include "vlan.h"
24 #include "vport-internal_dev.h"
25 #include "vport-netdev.h"
26
27 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) && \
28         !defined(HAVE_VLAN_BUG_WORKAROUND)
29 #include <linux/module.h>
30
31 static int vlan_tso __read_mostly;
32 module_param(vlan_tso, int, 0644);
33 MODULE_PARM_DESC(vlan_tso, "Enable TSO for VLAN packets");
34 #else
35 #define vlan_tso true
36 #endif
37
38 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
39
40 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
41 /* Called with rcu_read_lock and bottom-halves disabled. */
42 static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
43 {
44         struct sk_buff *skb = *pskb;
45         struct vport *vport;
46
47         if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
48                 return RX_HANDLER_PASS;
49
50         vport = netdev_get_vport(skb->dev);
51
52         netdev_port_receive(vport, skb);
53
54         return RX_HANDLER_CONSUMED;
55 }
56 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
57 /* Called with rcu_read_lock and bottom-halves disabled. */
58 static struct sk_buff *netdev_frame_hook(struct sk_buff *skb)
59 {
60         struct vport *vport;
61
62         if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
63                 return skb;
64
65         vport = netdev_get_vport(skb->dev);
66
67         netdev_port_receive(vport, skb);
68
69         return NULL;
70 }
71 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
72 /*
73  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
74  * different set of devices!)
75  */
76 /* Called with rcu_read_lock and bottom-halves disabled. */
77 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
78                                          struct sk_buff *skb)
79 {
80         netdev_port_receive((struct vport *)p, skb);
81         return NULL;
82 }
83 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
84 /*
85  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
86  * different set of devices!)
87  */
88 /* Called with rcu_read_lock and bottom-halves disabled. */
89 static int netdev_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
90 {
91         netdev_port_receive((struct vport *)p, *pskb);
92         return 1;
93 }
94 #else
95 #error
96 #endif
97
98 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
99 static int netdev_init(void) { return 0; }
100 static void netdev_exit(void) { }
101 #else
102 static int netdev_init(void)
103 {
104         /* Hook into callback used by the bridge to intercept packets.
105          * Parasites we are. */
106         br_handle_frame_hook = netdev_frame_hook;
107
108         return 0;
109 }
110
111 static void netdev_exit(void)
112 {
113         br_handle_frame_hook = NULL;
114 }
115 #endif
116
117 static struct vport *netdev_create(const struct vport_parms *parms)
118 {
119         struct vport *vport;
120         struct netdev_vport *netdev_vport;
121         int err;
122
123         vport = vport_alloc(sizeof(struct netdev_vport),
124                             &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         err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
146                                          vport);
147         if (err)
148                 goto error_put;
149
150         dev_set_promiscuity(netdev_vport->dev, 1);
151 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
152         dev_disable_lro(netdev_vport->dev);
153 #endif
154         netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
155
156         return vport;
157
158 error_put:
159         dev_put(netdev_vport->dev);
160 error_free_vport:
161         vport_free(vport);
162 error:
163         return ERR_PTR(err);
164 }
165
166 static void netdev_destroy(struct vport *vport)
167 {
168         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
169
170         netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
171         netdev_rx_handler_unregister(netdev_vport->dev);
172         dev_set_promiscuity(netdev_vport->dev, -1);
173
174         synchronize_rcu();
175
176         dev_put(netdev_vport->dev);
177         vport_free(vport);
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 unsigned netdev_get_dev_flags(const struct vport *vport)
210 {
211         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
212         return dev_get_flags(netdev_vport->dev);
213 }
214
215 int netdev_is_running(const struct vport *vport)
216 {
217         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
218         return netif_running(netdev_vport->dev);
219 }
220
221 unsigned char netdev_get_operstate(const struct vport *vport)
222 {
223         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
224         return netdev_vport->dev->operstate;
225 }
226
227 int netdev_get_ifindex(const struct vport *vport)
228 {
229         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
230         return netdev_vport->dev->ifindex;
231 }
232
233 int netdev_get_mtu(const struct vport *vport)
234 {
235         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
236         return netdev_vport->dev->mtu;
237 }
238
239 /* Must be called with rcu_read_lock. */
240 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
241 {
242         if (unlikely(!vport)) {
243                 kfree_skb(skb);
244                 return;
245         }
246
247         /* Make our own copy of the packet.  Otherwise we will mangle the
248          * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
249          * (No one comes after us, since we tell handle_bridge() that we took
250          * the packet.) */
251         skb = skb_share_check(skb, GFP_ATOMIC);
252         if (unlikely(!skb))
253                 return;
254
255         skb_push(skb, ETH_HLEN);
256
257         if (unlikely(compute_ip_summed(skb, false))) {
258                 kfree_skb(skb);
259                 return;
260         }
261         vlan_copy_skb_tci(skb);
262
263         vport_receive(vport, skb);
264 }
265
266 static unsigned packet_length(const struct sk_buff *skb)
267 {
268         unsigned length = skb->len - ETH_HLEN;
269
270         if (skb->protocol == htons(ETH_P_8021Q))
271                 length -= VLAN_HLEN;
272
273         return length;
274 }
275
276 static bool dev_supports_vlan_tx(struct net_device *dev)
277 {
278 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
279         /* Software fallback means every device supports vlan_tci on TX. */
280         return true;
281 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
282         return dev->features & NETIF_F_HW_VLAN_TX;
283 #else
284         /* Assume that the driver is buggy. */
285         return false;
286 #endif
287 }
288
289 static int netdev_send(struct vport *vport, struct sk_buff *skb)
290 {
291         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
292         int mtu = netdev_vport->dev->mtu;
293         int len;
294
295         if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
296                 if (net_ratelimit())
297                         pr_warn("%s: dropped over-mtu packet: %d > %d\n",
298                                 dp_name(vport->dp), packet_length(skb), mtu);
299                 goto error;
300         }
301
302         if (unlikely(skb_warn_if_lro(skb)))
303                 goto error;
304
305         skb->dev = netdev_vport->dev;
306         forward_ip_summed(skb, true);
307
308         if (vlan_tx_tag_present(skb) && !dev_supports_vlan_tx(skb->dev)) {
309                 int features;
310
311                 features = netif_skb_features(skb);
312
313                 if (!vlan_tso)
314                         features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
315                                       NETIF_F_UFO | NETIF_F_FSO);
316
317                 if (netif_needs_gso(skb, features)) {
318                         struct sk_buff *nskb;
319
320                         nskb = skb_gso_segment(skb, features);
321                         if (!nskb) {
322                                 if (unlikely(skb_cloned(skb) &&
323                                     pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
324                                         kfree_skb(skb);
325                                         return 0;
326                                 }
327
328                                 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
329                                 goto tag;
330                         }
331
332                         if (IS_ERR(nskb)) {
333                                 kfree_skb(skb);
334                                 return 0;
335                         }
336                         consume_skb(skb);
337                         skb = nskb;
338
339                         len = 0;
340                         do {
341                                 nskb = skb->next;
342                                 skb->next = NULL;
343
344                                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
345                                 if (likely(skb)) {
346                                         len += skb->len;
347                                         vlan_set_tci(skb, 0);
348                                         dev_queue_xmit(skb);
349                                 }
350
351                                 skb = nskb;
352                         } while (skb);
353
354                         return len;
355                 }
356
357 tag:
358                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
359                 if (unlikely(!skb))
360                         return 0;
361                 vlan_set_tci(skb, 0);
362         }
363
364         len = skb->len;
365         dev_queue_xmit(skb);
366
367         return len;
368
369 error:
370         kfree_skb(skb);
371         vport_record_error(vport, VPORT_E_TX_DROPPED);
372         return 0;
373 }
374
375 /* Returns null if this device is not attached to a datapath. */
376 struct vport *netdev_get_vport(struct net_device *dev)
377 {
378 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
379 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
380         if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
381 #else
382         if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))
383 #endif
384                 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
385         else
386                 return NULL;
387 #else
388         return (struct vport *)rcu_dereference_rtnl(dev->br_port);
389 #endif
390 }
391
392 const struct vport_ops netdev_vport_ops = {
393         .type           = OVS_VPORT_TYPE_NETDEV,
394         .flags          = VPORT_F_REQUIRED,
395         .init           = netdev_init,
396         .exit           = netdev_exit,
397         .create         = netdev_create,
398         .destroy        = netdev_destroy,
399         .set_addr       = netdev_set_addr,
400         .get_name       = netdev_get_name,
401         .get_addr       = netdev_get_addr,
402         .get_kobj       = netdev_get_kobj,
403         .get_dev_flags  = netdev_get_dev_flags,
404         .is_running     = netdev_is_running,
405         .get_operstate  = netdev_get_operstate,
406         .get_ifindex    = netdev_get_ifindex,
407         .get_mtu        = netdev_get_mtu,
408         .send           = netdev_send,
409 };
410
411 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
412 /*
413  * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
414  * the Linux bridge module on any released version of Linux, because there
415  * is only a single bridge hook function and only a single br_port member
416  * in struct net_device.
417  *
418  * Declaring and exporting this symbol enforces mutual exclusion.  The bridge
419  * module also exports the same symbol, so the module loader will refuse to
420  * load both modules at the same time (e.g. "bridge: exports duplicate symbol
421  * br_should_route_hook (owned by openvswitch_mod)").
422  *
423  * The use of "typeof" here avoids the need to track changes in the type of
424  * br_should_route_hook over various kernel versions.
425  */
426 typeof(br_should_route_hook) br_should_route_hook;
427 EXPORT_SYMBOL(br_should_route_hook);
428 #endif