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 #include <linux/module.h>
28 static int vlan_tso __read_mostly = 0;
29 module_param(vlan_tso, int, 0644);
30 MODULE_PARM_DESC(vlan_tso, "Enable TSO for VLAN packets");
33 /* If the native device stats aren't 64 bit use the vport stats tracking instead. */
34 #define USE_VPORT_STATS (sizeof(((struct net_device_stats *)0)->rx_bytes) < sizeof(u64))
36 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
38 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
39 /* Called with rcu_read_lock and bottom-halves disabled. */
40 static struct sk_buff *netdev_frame_hook(struct sk_buff *skb)
44 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
47 vport = netdev_get_vport(skb->dev);
49 netdev_port_receive(vport, skb);
53 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
55 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
56 * different set of devices!)
58 /* Called with rcu_read_lock and bottom-halves disabled. */
59 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
62 netdev_port_receive((struct vport *)p, skb);
65 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
67 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
68 * different set of devices!)
70 /* Called with rcu_read_lock and bottom-halves disabled. */
71 static int netdev_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
73 netdev_port_receive((struct vport *)p, *pskb);
80 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
81 static int netdev_init(void) { return 0; }
82 static void netdev_exit(void) { }
84 static int netdev_init(void)
86 /* Hook into callback used by the bridge to intercept packets.
87 * Parasites we are. */
88 br_handle_frame_hook = netdev_frame_hook;
93 static void netdev_exit(void)
95 br_handle_frame_hook = NULL;
99 static struct vport *netdev_create(const struct vport_parms *parms)
102 struct netdev_vport *netdev_vport;
105 vport = vport_alloc(sizeof(struct netdev_vport), &netdev_vport_ops, parms);
107 err = PTR_ERR(vport);
111 netdev_vport = netdev_vport_priv(vport);
113 netdev_vport->dev = dev_get_by_name(&init_net, parms->name);
114 if (!netdev_vport->dev) {
116 goto error_free_vport;
119 if (netdev_vport->dev->flags & IFF_LOOPBACK ||
120 netdev_vport->dev->type != ARPHRD_ETHER ||
121 is_internal_dev(netdev_vport->dev)) {
126 /* If we are using the vport stats layer initialize it to the current
127 * values so we are roughly consistent with the device stats. */
128 if (USE_VPORT_STATS) {
129 struct rtnl_link_stats64 stats;
131 err = netdev_get_stats(vport, &stats);
133 vport_set_stats(vport, &stats);
136 err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
141 dev_set_promiscuity(netdev_vport->dev, 1);
142 dev_disable_lro(netdev_vport->dev);
143 netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
148 dev_put(netdev_vport->dev);
155 static int netdev_destroy(struct vport *vport)
157 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
159 netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
160 netdev_rx_handler_unregister(netdev_vport->dev);
161 dev_set_promiscuity(netdev_vport->dev, -1);
165 dev_put(netdev_vport->dev);
171 int netdev_set_mtu(struct vport *vport, int mtu)
173 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
174 return dev_set_mtu(netdev_vport->dev, mtu);
177 int netdev_set_addr(struct vport *vport, const unsigned char *addr)
179 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
182 sa.sa_family = ARPHRD_ETHER;
183 memcpy(sa.sa_data, addr, ETH_ALEN);
185 return dev_set_mac_address(netdev_vport->dev, &sa);
188 const char *netdev_get_name(const struct vport *vport)
190 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
191 return netdev_vport->dev->name;
194 const unsigned char *netdev_get_addr(const struct vport *vport)
196 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
197 return netdev_vport->dev->dev_addr;
200 struct kobject *netdev_get_kobj(const struct vport *vport)
202 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
203 return &netdev_vport->dev->NETDEV_DEV_MEMBER.kobj;
206 int netdev_get_stats(const struct vport *vport, struct rtnl_link_stats64 *stats)
208 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
209 dev_get_stats(netdev_vport->dev, stats);
213 unsigned netdev_get_dev_flags(const struct vport *vport)
215 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
216 return dev_get_flags(netdev_vport->dev);
219 int netdev_is_running(const struct vport *vport)
221 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
222 return netif_running(netdev_vport->dev);
225 unsigned char netdev_get_operstate(const struct vport *vport)
227 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
228 return netdev_vport->dev->operstate;
231 int netdev_get_ifindex(const struct vport *vport)
233 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
234 return netdev_vport->dev->ifindex;
237 int netdev_get_iflink(const struct vport *vport)
239 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
240 return netdev_vport->dev->iflink;
243 int netdev_get_mtu(const struct vport *vport)
245 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
246 return netdev_vport->dev->mtu;
249 /* Must be called with rcu_read_lock. */
250 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
252 /* Make our own copy of the packet. Otherwise we will mangle the
253 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
254 * (No one comes after us, since we tell handle_bridge() that we took
256 skb = skb_share_check(skb, GFP_ATOMIC);
260 skb_warn_if_lro(skb);
262 skb_push(skb, ETH_HLEN);
263 compute_ip_summed(skb, false);
264 vlan_copy_skb_tci(skb);
266 vport_receive(vport, skb);
269 static int netdev_send(struct vport *vport, struct sk_buff *skb)
271 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
274 skb->dev = netdev_vport->dev;
275 forward_ip_summed(skb);
277 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
278 if (vlan_tx_tag_present(skb)) {
282 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
283 features = skb->dev->features & skb->dev->vlan_features;
286 err = vswitch_skb_checksum_setup(skb);
293 features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
294 NETIF_F_UFO | NETIF_F_FSO);
296 if (skb_is_gso(skb) &&
297 (!skb_gso_ok(skb, features) ||
298 unlikely(skb->ip_summed != CHECKSUM_PARTIAL))) {
299 struct sk_buff *nskb;
301 nskb = skb_gso_segment(skb, features);
303 if (unlikely(skb_cloned(skb) &&
304 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
309 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
323 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
326 vlan_set_tci(skb, 0);
337 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
340 vlan_set_tci(skb, 0);
350 /* Returns null if this device is not attached to a datapath. */
351 struct vport *netdev_get_vport(struct net_device *dev)
353 #ifdef IFF_BRIDGE_PORT
354 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
355 if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
357 if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))
359 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
363 return (struct vport *)rcu_dereference_rtnl(dev->br_port);
367 const struct vport_ops netdev_vport_ops = {
368 .type = ODP_VPORT_TYPE_NETDEV,
369 .flags = (VPORT_F_REQUIRED |
370 (USE_VPORT_STATS ? VPORT_F_GEN_STATS : 0)),
373 .create = netdev_create,
374 .destroy = netdev_destroy,
375 .set_mtu = netdev_set_mtu,
376 .set_addr = netdev_set_addr,
377 .get_name = netdev_get_name,
378 .get_addr = netdev_get_addr,
379 .get_kobj = netdev_get_kobj,
380 .get_stats = netdev_get_stats,
381 .get_dev_flags = netdev_get_dev_flags,
382 .is_running = netdev_is_running,
383 .get_operstate = netdev_get_operstate,
384 .get_ifindex = netdev_get_ifindex,
385 .get_iflink = netdev_get_iflink,
386 .get_mtu = netdev_get_mtu,
390 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
392 * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
393 * the Linux bridge module on any released version of Linux, because there
394 * is only a single bridge hook function and only a single br_port member
395 * in struct net_device.
397 * Declaring and exporting this symbol enforces mutual exclusion. The bridge
398 * module also exports the same symbol, so the module loader will refuse to
399 * load both modules at the same time (e.g. "bridge: exports duplicate symbol
400 * br_should_route_hook (owned by openvswitch_mod)").
402 * The use of "typeof" here avoids the need to track changes in the type of
403 * br_should_route_hook over various kernel versions.
405 typeof(br_should_route_hook) br_should_route_hook;
406 EXPORT_SYMBOL(br_should_route_hook);