]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/vport-netdev.c
ofp-util: Make ofputil_encode_set_protocol() able to return failure.
[mirror_ovs.git] / datapath / vport-netdev.c
CommitLineData
f2459fe7 1/*
e0edde6f 2 * Copyright (c) 2007-2012 Nicira, Inc.
f2459fe7 3 *
a9a29d22
JG
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
f2459fe7
JG
17 */
18
24b019f8
JP
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
f2459fe7
JG
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
dd8d6b8c 31#include "checksum.h"
f2459fe7 32#include "datapath.h"
6e0ce48e 33#include "vlan.h"
f2459fe7
JG
34#include "vport-internal_dev.h"
35#include "vport-netdev.h"
36
431488e6 37#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) && \
6455100f 38 !defined(HAVE_VLAN_BUG_WORKAROUND)
3f6256af
JG
39#include <linux/module.h>
40
6455100f 41static int vlan_tso __read_mostly;
3f6256af
JG
42module_param(vlan_tso, int, 0644);
43MODULE_PARM_DESC(vlan_tso, "Enable TSO for VLAN packets");
431488e6
BP
44#else
45#define vlan_tso true
3f6256af
JG
46#endif
47
43158509 48static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
f2459fe7 49
898ba403
AE
50#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
51/* Called with rcu_read_lock and bottom-halves disabled. */
52static 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
850b6b3b 60 vport = ovs_netdev_get_vport(skb->dev);
898ba403
AE
61
62 netdev_port_receive(vport, skb);
63
64 return RX_HANDLER_CONSUMED;
65}
66#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
43158509
SH
67/* Called with rcu_read_lock and bottom-halves disabled. */
68static 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
0145d7ed 75 vport = ovs_netdev_get_vport(skb->dev);
43158509
SH
76
77 netdev_port_receive(vport, skb);
78
79 return NULL;
80}
81#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
f2459fe7
JG
82/*
83 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
84 * different set of devices!)
85 */
f2459fe7 86/* Called with rcu_read_lock and bottom-halves disabled. */
fceb2a5b
JG
87static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
88 struct sk_buff *skb)
f2459fe7 89{
43158509 90 netdev_port_receive((struct vport *)p, skb);
f2459fe7
JG
91 return NULL;
92}
93#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
43158509
SH
94/*
95 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
96 * different set of devices!)
97 */
f2459fe7 98/* Called with rcu_read_lock and bottom-halves disabled. */
fceb2a5b 99static int netdev_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
f2459fe7 100{
43158509 101 netdev_port_receive((struct vport *)p, *pskb);
f2459fe7
JG
102 return 1;
103}
104#else
105#error
106#endif
107
43158509
SH
108#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
109static int netdev_init(void) { return 0; }
110static void netdev_exit(void) { }
111#else
fceb2a5b 112static int netdev_init(void)
f2459fe7 113{
f2459fe7
JG
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
fbb29a54 121static void netdev_exit(void)
f2459fe7
JG
122{
123 br_handle_frame_hook = NULL;
f2459fe7 124}
43158509 125#endif
f2459fe7 126
94903c98 127static struct vport *netdev_create(const struct vport_parms *parms)
f2459fe7
JG
128{
129 struct vport *vport;
130 struct netdev_vport *netdev_vport;
131 int err;
132
850b6b3b
JG
133 vport = ovs_vport_alloc(sizeof(struct netdev_vport),
134 &ovs_netdev_vport_ops, parms);
f2459fe7
JG
135 if (IS_ERR(vport)) {
136 err = PTR_ERR(vport);
137 goto error;
138 }
139
140 netdev_vport = netdev_vport_priv(vport);
141
2a4999f3 142 netdev_vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), parms->name);
f2459fe7
JG
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 ||
850b6b3b 150 ovs_is_internal_dev(netdev_vport->dev)) {
f2459fe7
JG
151 err = -EINVAL;
152 goto error_put;
153 }
154
7237e4f4
BP
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);
24b019f8 161#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
7237e4f4 162 dev_disable_lro(netdev_vport->dev);
24b019f8 163#endif
7237e4f4
BP
164 netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
165
f2459fe7
JG
166 return vport;
167
168error_put:
169 dev_put(netdev_vport->dev);
170error_free_vport:
850b6b3b 171 ovs_vport_free(vport);
f2459fe7
JG
172error:
173 return ERR_PTR(err);
174}
175
3544358a 176static void netdev_destroy(struct vport *vport)
f2459fe7
JG
177{
178 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
179
37f055c7 180 netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
43158509 181 netdev_rx_handler_unregister(netdev_vport->dev);
f2459fe7
JG
182 dev_set_promiscuity(netdev_vport->dev, -1);
183
7237e4f4
BP
184 synchronize_rcu();
185
186 dev_put(netdev_vport->dev);
850b6b3b 187 ovs_vport_free(vport);
f2459fe7
JG
188}
189
850b6b3b 190int ovs_netdev_set_addr(struct vport *vport, const unsigned char *addr)
f2459fe7
JG
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
850b6b3b 201const char *ovs_netdev_get_name(const struct vport *vport)
f2459fe7
JG
202{
203 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
204 return netdev_vport->dev->name;
205}
206
850b6b3b 207const unsigned char *ovs_netdev_get_addr(const struct vport *vport)
f2459fe7
JG
208{
209 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
210 return netdev_vport->dev->dev_addr;
211}
212
850b6b3b 213struct kobject *ovs_netdev_get_kobj(const struct vport *vport)
f2459fe7
JG
214{
215 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
216 return &netdev_vport->dev->NETDEV_DEV_MEMBER.kobj;
217}
218
850b6b3b 219unsigned ovs_netdev_get_dev_flags(const struct vport *vport)
f2459fe7
JG
220{
221 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
222 return dev_get_flags(netdev_vport->dev);
223}
224
850b6b3b 225int ovs_netdev_is_running(const struct vport *vport)
f2459fe7
JG
226{
227 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
228 return netif_running(netdev_vport->dev);
229}
230
850b6b3b 231unsigned char ovs_netdev_get_operstate(const struct vport *vport)
f2459fe7
JG
232{
233 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
234 return netdev_vport->dev->operstate;
235}
236
850b6b3b 237int ovs_netdev_get_ifindex(const struct vport *vport)
f2459fe7
JG
238{
239 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
240 return netdev_vport->dev->ifindex;
241}
242
850b6b3b 243int ovs_netdev_get_mtu(const struct vport *vport)
f2459fe7
JG
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. */
43158509 250static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
f2459fe7 251{
d825e2a5
JG
252 if (unlikely(!vport)) {
253 kfree_skb(skb);
254 return;
255 }
256
f2459fe7
JG
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);
67c74f75 262 if (unlikely(!skb))
f2459fe7
JG
263 return;
264
f2459fe7 265 skb_push(skb, ETH_HLEN);
c3729ee4
JG
266
267 if (unlikely(compute_ip_summed(skb, false))) {
268 kfree_skb(skb);
269 return;
270 }
6e0ce48e 271 vlan_copy_skb_tci(skb);
f2459fe7 272
850b6b3b 273 ovs_vport_receive(vport, skb);
f2459fe7
JG
274}
275
f1b8a922 276static unsigned int packet_length(const struct sk_buff *skb)
24b019f8 277{
f1b8a922 278 unsigned int length = skb->len - ETH_HLEN;
24b019f8
JP
279
280 if (skb->protocol == htons(ETH_P_8021Q))
281 length -= VLAN_HLEN;
282
283 return length;
284}
285
431488e6
BP
286static 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
fceb2a5b 299static int netdev_send(struct vport *vport, struct sk_buff *skb)
f2459fe7
JG
300{
301 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
24b019f8 302 int mtu = netdev_vport->dev->mtu;
6ce39213 303 int len;
f2459fe7 304
24b019f8 305 if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
51885213 306 net_warn_ratelimited("%s: dropped over-mtu packet: %d > %d\n",
40a5a25d 307 netdev_vport->dev->name,
51885213 308 packet_length(skb), mtu);
24b019f8
JP
309 goto error;
310 }
311
312 if (unlikely(skb_warn_if_lro(skb)))
313 goto error;
314
f2459fe7 315 skb->dev = netdev_vport->dev;
c3729ee4 316 forward_ip_summed(skb, true);
6ce39213 317
431488e6 318 if (vlan_tx_tag_present(skb) && !dev_supports_vlan_tx(skb->dev)) {
d9065a90 319 int features;
2d8916e5 320
d9065a90 321 features = netif_skb_features(skb);
6ce39213 322
3f6256af
JG
323 if (!vlan_tso)
324 features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
325 NETIF_F_UFO | NETIF_F_FSO);
326
d9065a90 327 if (netif_needs_gso(skb, features)) {
6ce39213
JG
328 struct sk_buff *nskb;
329
3f6256af
JG
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
5b95ab0e
JG
342 if (IS_ERR(nskb)) {
343 kfree_skb(skb);
6ce39213 344 return 0;
5b95ab0e
JG
345 }
346 consume_skb(skb);
347 skb = nskb;
6ce39213
JG
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;
6ce39213 365 }
3f6256af
JG
366
367tag:
368 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
369 if (unlikely(!skb))
370 return 0;
371 vlan_set_tci(skb, 0);
6ce39213 372 }
6ce39213
JG
373
374 len = skb->len;
f2459fe7
JG
375 dev_queue_xmit(skb);
376
377 return len;
24b019f8
JP
378
379error:
380 kfree_skb(skb);
850b6b3b 381 ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
24b019f8 382 return 0;
f2459fe7
JG
383}
384
385/* Returns null if this device is not attached to a datapath. */
850b6b3b 386struct vport *ovs_netdev_get_vport(struct net_device *dev)
f2459fe7 387{
2fb2cb98 388#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
e25c55d2 389#if IFF_OVS_DATAPATH != 0
055dae4f
JG
390 if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
391#else
6455100f 392 if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))
055dae4f 393#endif
e33adfd0 394 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
055dae4f 395 else
37f055c7 396 return NULL;
37f055c7 397#else
e33adfd0 398 return (struct vport *)rcu_dereference_rtnl(dev->br_port);
37f055c7 399#endif
f2459fe7
JG
400}
401
850b6b3b 402const struct vport_ops ovs_netdev_vport_ops = {
df2c07f4 403 .type = OVS_VPORT_TYPE_NETDEV,
f613a0d7 404 .flags = VPORT_F_REQUIRED,
f2459fe7
JG
405 .init = netdev_init,
406 .exit = netdev_exit,
407 .create = netdev_create,
408 .destroy = netdev_destroy,
850b6b3b
JG
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,
f2459fe7
JG
418 .send = netdev_send,
419};
106eab5d 420
37f055c7 421#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
106eab5d 422/*
a647150f
BP
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
9b80f761 426 * both bridge and openvswitch at the same time.
106eab5d 427 */
a647150f 428BRIDGE_MUTUAL_EXCLUSION;
37f055c7 429#endif