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