Global replace of Nicira Networks.
[sliver-openvswitch.git] / datapath / vport-netdev.c
1 /*
2  * Copyright (c) 2007-2012 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/if_arp.h>
22 #include <linux/if_bridge.h>
23 #include <linux/if_vlan.h>
24 #include <linux/kernel.h>
25 #include <linux/llc.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/skbuff.h>
28
29 #include <net/llc.h>
30
31 #include "checksum.h"
32 #include "datapath.h"
33 #include "vlan.h"
34 #include "vport-internal_dev.h"
35 #include "vport-netdev.h"
36
37 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) && \
38         !defined(HAVE_VLAN_BUG_WORKAROUND)
39 #include <linux/module.h>
40
41 static int vlan_tso __read_mostly;
42 module_param(vlan_tso, int, 0644);
43 MODULE_PARM_DESC(vlan_tso, "Enable TSO for VLAN packets");
44 #else
45 #define vlan_tso true
46 #endif
47
48 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
49
50 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
51 /* Called with rcu_read_lock and bottom-halves disabled. */
52 static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
53 {
54         struct sk_buff *skb = *pskb;
55         struct vport *vport;
56
57         if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
58                 return RX_HANDLER_PASS;
59
60         vport = ovs_netdev_get_vport(skb->dev);
61
62         netdev_port_receive(vport, skb);
63
64         return RX_HANDLER_CONSUMED;
65 }
66 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
67 /* Called with rcu_read_lock and bottom-halves disabled. */
68 static struct sk_buff *netdev_frame_hook(struct sk_buff *skb)
69 {
70         struct vport *vport;
71
72         if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
73                 return skb;
74
75         vport = ovs_netdev_get_vport(skb->dev);
76
77         netdev_port_receive(vport, skb);
78
79         return NULL;
80 }
81 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
82 /*
83  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
84  * different set of devices!)
85  */
86 /* Called with rcu_read_lock and bottom-halves disabled. */
87 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
88                                          struct sk_buff *skb)
89 {
90         netdev_port_receive((struct vport *)p, skb);
91         return NULL;
92 }
93 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
94 /*
95  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
96  * different set of devices!)
97  */
98 /* Called with rcu_read_lock and bottom-halves disabled. */
99 static int netdev_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
100 {
101         netdev_port_receive((struct vport *)p, *pskb);
102         return 1;
103 }
104 #else
105 #error
106 #endif
107
108 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
109 static int netdev_init(void) { return 0; }
110 static void netdev_exit(void) { }
111 #else
112 static int netdev_init(void)
113 {
114         /* Hook into callback used by the bridge to intercept packets.
115          * Parasites we are. */
116         br_handle_frame_hook = netdev_frame_hook;
117
118         return 0;
119 }
120
121 static void netdev_exit(void)
122 {
123         br_handle_frame_hook = NULL;
124 }
125 #endif
126
127 static struct vport *netdev_create(const struct vport_parms *parms)
128 {
129         struct vport *vport;
130         struct netdev_vport *netdev_vport;
131         int err;
132
133         vport = ovs_vport_alloc(sizeof(struct netdev_vport),
134                                 &ovs_netdev_vport_ops, parms);
135         if (IS_ERR(vport)) {
136                 err = PTR_ERR(vport);
137                 goto error;
138         }
139
140         netdev_vport = netdev_vport_priv(vport);
141
142         netdev_vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), parms->name);
143         if (!netdev_vport->dev) {
144                 err = -ENODEV;
145                 goto error_free_vport;
146         }
147
148         if (netdev_vport->dev->flags & IFF_LOOPBACK ||
149             netdev_vport->dev->type != ARPHRD_ETHER ||
150             ovs_is_internal_dev(netdev_vport->dev)) {
151                 err = -EINVAL;
152                 goto error_put;
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 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
162         dev_disable_lro(netdev_vport->dev);
163 #endif
164         netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
165
166         return vport;
167
168 error_put:
169         dev_put(netdev_vport->dev);
170 error_free_vport:
171         ovs_vport_free(vport);
172 error:
173         return ERR_PTR(err);
174 }
175
176 static void netdev_destroy(struct vport *vport)
177 {
178         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
179
180         netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
181         netdev_rx_handler_unregister(netdev_vport->dev);
182         dev_set_promiscuity(netdev_vport->dev, -1);
183
184         synchronize_rcu();
185
186         dev_put(netdev_vport->dev);
187         ovs_vport_free(vport);
188 }
189
190 int ovs_netdev_set_addr(struct vport *vport, const unsigned char *addr)
191 {
192         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
193         struct sockaddr sa;
194
195         sa.sa_family = ARPHRD_ETHER;
196         memcpy(sa.sa_data, addr, ETH_ALEN);
197
198         return dev_set_mac_address(netdev_vport->dev, &sa);
199 }
200
201 const char *ovs_netdev_get_name(const struct vport *vport)
202 {
203         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
204         return netdev_vport->dev->name;
205 }
206
207 const unsigned char *ovs_netdev_get_addr(const struct vport *vport)
208 {
209         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
210         return netdev_vport->dev->dev_addr;
211 }
212
213 struct kobject *ovs_netdev_get_kobj(const struct vport *vport)
214 {
215         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
216         return &netdev_vport->dev->NETDEV_DEV_MEMBER.kobj;
217 }
218
219 unsigned ovs_netdev_get_dev_flags(const struct vport *vport)
220 {
221         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
222         return dev_get_flags(netdev_vport->dev);
223 }
224
225 int ovs_netdev_is_running(const struct vport *vport)
226 {
227         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
228         return netif_running(netdev_vport->dev);
229 }
230
231 unsigned char ovs_netdev_get_operstate(const struct vport *vport)
232 {
233         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
234         return netdev_vport->dev->operstate;
235 }
236
237 int ovs_netdev_get_ifindex(const struct vport *vport)
238 {
239         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
240         return netdev_vport->dev->ifindex;
241 }
242
243 int ovs_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         if (unlikely(!vport)) {
253                 kfree_skb(skb);
254                 return;
255         }
256
257         /* Make our own copy of the packet.  Otherwise we will mangle the
258          * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
259          * (No one comes after us, since we tell handle_bridge() that we took
260          * the packet.) */
261         skb = skb_share_check(skb, GFP_ATOMIC);
262         if (unlikely(!skb))
263                 return;
264
265         skb_push(skb, ETH_HLEN);
266
267         if (unlikely(compute_ip_summed(skb, false))) {
268                 kfree_skb(skb);
269                 return;
270         }
271         vlan_copy_skb_tci(skb);
272
273         ovs_vport_receive(vport, skb);
274 }
275
276 static unsigned packet_length(const struct sk_buff *skb)
277 {
278         unsigned length = skb->len - ETH_HLEN;
279
280         if (skb->protocol == htons(ETH_P_8021Q))
281                 length -= VLAN_HLEN;
282
283         return length;
284 }
285
286 static bool dev_supports_vlan_tx(struct net_device *dev)
287 {
288 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
289         /* Software fallback means every device supports vlan_tci on TX. */
290         return true;
291 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
292         return dev->features & NETIF_F_HW_VLAN_TX;
293 #else
294         /* Assume that the driver is buggy. */
295         return false;
296 #endif
297 }
298
299 static int netdev_send(struct vport *vport, struct sk_buff *skb)
300 {
301         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
302         int mtu = netdev_vport->dev->mtu;
303         int len;
304
305         if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
306                 if (net_ratelimit())
307                         pr_warn("%s: dropped over-mtu packet: %d > %d\n",
308                                 ovs_dp_name(vport->dp), packet_length(skb), mtu);
309                 goto error;
310         }
311
312         if (unlikely(skb_warn_if_lro(skb)))
313                 goto error;
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;
320
321                 features = netif_skb_features(skb);
322
323                 if (!vlan_tso)
324                         features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
325                                       NETIF_F_UFO | NETIF_F_FSO);
326
327                 if (netif_needs_gso(skb, features)) {
328                         struct sk_buff *nskb;
329
330                         nskb = skb_gso_segment(skb, features);
331                         if (!nskb) {
332                                 if (unlikely(skb_cloned(skb) &&
333                                     pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
334                                         kfree_skb(skb);
335                                         return 0;
336                                 }
337
338                                 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
339                                 goto tag;
340                         }
341
342                         if (IS_ERR(nskb)) {
343                                 kfree_skb(skb);
344                                 return 0;
345                         }
346                         consume_skb(skb);
347                         skb = nskb;
348
349                         len = 0;
350                         do {
351                                 nskb = skb->next;
352                                 skb->next = NULL;
353
354                                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
355                                 if (likely(skb)) {
356                                         len += skb->len;
357                                         vlan_set_tci(skb, 0);
358                                         dev_queue_xmit(skb);
359                                 }
360
361                                 skb = nskb;
362                         } while (skb);
363
364                         return len;
365                 }
366
367 tag:
368                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
369                 if (unlikely(!skb))
370                         return 0;
371                 vlan_set_tci(skb, 0);
372         }
373
374         len = skb->len;
375         dev_queue_xmit(skb);
376
377         return len;
378
379 error:
380         kfree_skb(skb);
381         ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
382         return 0;
383 }
384
385 /* Returns null if this device is not attached to a datapath. */
386 struct vport *ovs_netdev_get_vport(struct net_device *dev)
387 {
388 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
389 #if IFF_OVS_DATAPATH != 0
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 ovs_netdev_vport_ops = {
403         .type           = OVS_VPORT_TYPE_NETDEV,
404         .flags          = VPORT_F_REQUIRED,
405         .init           = netdev_init,
406         .exit           = netdev_exit,
407         .create         = netdev_create,
408         .destroy        = netdev_destroy,
409         .set_addr       = ovs_netdev_set_addr,
410         .get_name       = ovs_netdev_get_name,
411         .get_addr       = ovs_netdev_get_addr,
412         .get_kobj       = ovs_netdev_get_kobj,
413         .get_dev_flags  = ovs_netdev_get_dev_flags,
414         .is_running     = ovs_netdev_is_running,
415         .get_operstate  = ovs_netdev_get_operstate,
416         .get_ifindex    = ovs_netdev_get_ifindex,
417         .get_mtu        = ovs_netdev_get_mtu,
418         .send           = netdev_send,
419 };
420
421 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
422 /*
423  * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with the
424  * Linux bridge module, because there is only a single bridge hook function and
425  * only a single br_port member in struct net_device, so this prevents loading
426  * both bridge and openvswitch at the same time.
427  */
428 BRIDGE_MUTUAL_EXCLUSION;
429 #endif