2 * Copyright (c) 2010, 2011 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
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>
22 #include "vport-internal_dev.h"
23 #include "vport-netdev.h"
25 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) && \
26 !defined(HAVE_VLAN_BUG_WORKAROUND)
27 #include <linux/module.h>
29 static int vlan_tso __read_mostly = 0;
30 module_param(vlan_tso, int, 0644);
31 MODULE_PARM_DESC(vlan_tso, "Enable TSO for VLAN packets");
36 /* If the native device stats aren't 64 bit use the vport stats tracking instead. */
37 #define USE_VPORT_STATS (sizeof(((struct net_device_stats *)0)->rx_bytes) < sizeof(u64))
39 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
41 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
42 /* Called with rcu_read_lock and bottom-halves disabled. */
43 static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
45 struct sk_buff *skb = *pskb;
48 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
49 return RX_HANDLER_PASS;
51 vport = netdev_get_vport(skb->dev);
53 netdev_port_receive(vport, skb);
55 return RX_HANDLER_CONSUMED;
57 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
58 /* Called with rcu_read_lock and bottom-halves disabled. */
59 static struct sk_buff *netdev_frame_hook(struct sk_buff *skb)
63 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
66 vport = netdev_get_vport(skb->dev);
68 netdev_port_receive(vport, skb);
72 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
74 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
75 * different set of devices!)
77 /* Called with rcu_read_lock and bottom-halves disabled. */
78 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
81 netdev_port_receive((struct vport *)p, skb);
84 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
86 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
87 * different set of devices!)
89 /* Called with rcu_read_lock and bottom-halves disabled. */
90 static int netdev_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
92 netdev_port_receive((struct vport *)p, *pskb);
99 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
100 static int netdev_init(void) { return 0; }
101 static void netdev_exit(void) { }
103 static int netdev_init(void)
105 /* Hook into callback used by the bridge to intercept packets.
106 * Parasites we are. */
107 br_handle_frame_hook = netdev_frame_hook;
112 static void netdev_exit(void)
114 br_handle_frame_hook = NULL;
118 static struct vport *netdev_create(const struct vport_parms *parms)
121 struct netdev_vport *netdev_vport;
124 vport = vport_alloc(sizeof(struct netdev_vport), &netdev_vport_ops, parms);
126 err = PTR_ERR(vport);
130 netdev_vport = netdev_vport_priv(vport);
132 netdev_vport->dev = dev_get_by_name(&init_net, parms->name);
133 if (!netdev_vport->dev) {
135 goto error_free_vport;
138 if (netdev_vport->dev->flags & IFF_LOOPBACK ||
139 netdev_vport->dev->type != ARPHRD_ETHER ||
140 is_internal_dev(netdev_vport->dev)) {
145 /* If we are using the vport stats layer initialize it to the current
146 * values so we are roughly consistent with the device stats. */
147 if (USE_VPORT_STATS) {
148 struct rtnl_link_stats64 stats;
150 err = netdev_get_stats(vport, &stats);
152 vport_set_stats(vport, &stats);
155 err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
160 dev_set_promiscuity(netdev_vport->dev, 1);
161 dev_disable_lro(netdev_vport->dev);
162 netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
167 dev_put(netdev_vport->dev);
174 static int netdev_destroy(struct vport *vport)
176 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
178 netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
179 netdev_rx_handler_unregister(netdev_vport->dev);
180 dev_set_promiscuity(netdev_vport->dev, -1);
184 dev_put(netdev_vport->dev);
190 int netdev_set_mtu(struct vport *vport, int mtu)
192 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
193 return dev_set_mtu(netdev_vport->dev, mtu);
196 int netdev_set_addr(struct vport *vport, const unsigned char *addr)
198 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
201 sa.sa_family = ARPHRD_ETHER;
202 memcpy(sa.sa_data, addr, ETH_ALEN);
204 return dev_set_mac_address(netdev_vport->dev, &sa);
207 const char *netdev_get_name(const struct vport *vport)
209 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
210 return netdev_vport->dev->name;
213 const unsigned char *netdev_get_addr(const struct vport *vport)
215 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
216 return netdev_vport->dev->dev_addr;
219 struct kobject *netdev_get_kobj(const struct vport *vport)
221 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
222 return &netdev_vport->dev->NETDEV_DEV_MEMBER.kobj;
225 int netdev_get_stats(const struct vport *vport, struct rtnl_link_stats64 *stats)
227 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
228 dev_get_stats(netdev_vport->dev, stats);
232 unsigned netdev_get_dev_flags(const struct vport *vport)
234 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
235 return dev_get_flags(netdev_vport->dev);
238 int netdev_is_running(const struct vport *vport)
240 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
241 return netif_running(netdev_vport->dev);
244 unsigned char netdev_get_operstate(const struct vport *vport)
246 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
247 return netdev_vport->dev->operstate;
250 int netdev_get_ifindex(const struct vport *vport)
252 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
253 return netdev_vport->dev->ifindex;
256 int netdev_get_iflink(const struct vport *vport)
258 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
259 return netdev_vport->dev->iflink;
262 int netdev_get_mtu(const struct vport *vport)
264 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
265 return netdev_vport->dev->mtu;
268 /* Must be called with rcu_read_lock. */
269 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
271 if (unlikely(!vport)) {
276 /* Make our own copy of the packet. Otherwise we will mangle the
277 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
278 * (No one comes after us, since we tell handle_bridge() that we took
280 skb = skb_share_check(skb, GFP_ATOMIC);
284 skb_warn_if_lro(skb);
286 skb_push(skb, ETH_HLEN);
287 compute_ip_summed(skb, false);
288 vlan_copy_skb_tci(skb);
290 vport_receive(vport, skb);
293 static bool dev_supports_vlan_tx(struct net_device *dev)
295 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
296 /* Software fallback means every device supports vlan_tci on TX. */
298 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
299 return dev->features & NETIF_F_HW_VLAN_TX;
301 /* Assume that the driver is buggy. */
306 static int netdev_send(struct vport *vport, struct sk_buff *skb)
308 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
311 skb->dev = netdev_vport->dev;
312 forward_ip_summed(skb);
314 if (vlan_tx_tag_present(skb) && !dev_supports_vlan_tx(skb->dev)) {
318 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
319 features = skb->dev->features & skb->dev->vlan_features;
322 err = vswitch_skb_checksum_setup(skb);
329 features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
330 NETIF_F_UFO | NETIF_F_FSO);
332 if (skb_is_gso(skb) &&
333 (!skb_gso_ok(skb, features) ||
334 unlikely(skb->ip_summed != CHECKSUM_PARTIAL))) {
335 struct sk_buff *nskb;
337 nskb = skb_gso_segment(skb, features);
339 if (unlikely(skb_cloned(skb) &&
340 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
345 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
359 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
362 vlan_set_tci(skb, 0);
373 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
376 vlan_set_tci(skb, 0);
385 /* Returns null if this device is not attached to a datapath. */
386 struct vport *netdev_get_vport(struct net_device *dev)
388 #ifdef IFF_BRIDGE_PORT
389 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
390 if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
392 if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))
394 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
398 return (struct vport *)rcu_dereference_rtnl(dev->br_port);
402 const struct vport_ops netdev_vport_ops = {
403 .type = ODP_VPORT_TYPE_NETDEV,
404 .flags = (VPORT_F_REQUIRED |
405 (USE_VPORT_STATS ? VPORT_F_GEN_STATS : 0)),
408 .create = netdev_create,
409 .destroy = netdev_destroy,
410 .set_mtu = netdev_set_mtu,
411 .set_addr = netdev_set_addr,
412 .get_name = netdev_get_name,
413 .get_addr = netdev_get_addr,
414 .get_kobj = netdev_get_kobj,
415 .get_stats = netdev_get_stats,
416 .get_dev_flags = netdev_get_dev_flags,
417 .is_running = netdev_is_running,
418 .get_operstate = netdev_get_operstate,
419 .get_ifindex = netdev_get_ifindex,
420 .get_iflink = netdev_get_iflink,
421 .get_mtu = netdev_get_mtu,
425 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
427 * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
428 * the Linux bridge module on any released version of Linux, because there
429 * is only a single bridge hook function and only a single br_port member
430 * in struct net_device.
432 * Declaring and exporting this symbol enforces mutual exclusion. The bridge
433 * module also exports the same symbol, so the module loader will refuse to
434 * load both modules at the same time (e.g. "bridge: exports duplicate symbol
435 * br_should_route_hook (owned by openvswitch_mod)").
437 * The use of "typeof" here avoids the need to track changes in the type of
438 * br_should_route_hook over various kernel versions.
440 typeof(br_should_route_hook) br_should_route_hook;
441 EXPORT_SYMBOL(br_should_route_hook);