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>
21 #include "vport-internal_dev.h"
22 #include "vport-netdev.h"
24 /* If the native device stats aren't 64 bit use the vport stats tracking instead. */
25 #define USE_VPORT_STATS (sizeof(((struct net_device_stats *)0)->rx_bytes) < sizeof(u64))
27 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
29 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
30 /* Called with rcu_read_lock and bottom-halves disabled. */
31 static struct sk_buff *netdev_frame_hook(struct sk_buff *skb)
35 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
38 vport = netdev_get_vport(skb->dev);
40 netdev_port_receive(vport, skb);
44 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
46 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
47 * different set of devices!)
49 /* Called with rcu_read_lock and bottom-halves disabled. */
50 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
53 netdev_port_receive((struct vport *)p, skb);
56 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
58 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
59 * different set of devices!)
61 /* Called with rcu_read_lock and bottom-halves disabled. */
62 static int netdev_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
64 netdev_port_receive((struct vport *)p, *pskb);
71 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
72 static int netdev_init(void) { return 0; }
73 static void netdev_exit(void) { }
75 static int netdev_init(void)
77 /* Hook into callback used by the bridge to intercept packets.
78 * Parasites we are. */
79 br_handle_frame_hook = netdev_frame_hook;
84 static void netdev_exit(void)
86 br_handle_frame_hook = NULL;
90 static struct vport *netdev_create(const struct vport_parms *parms)
93 struct netdev_vport *netdev_vport;
96 vport = vport_alloc(sizeof(struct netdev_vport), &netdev_vport_ops, parms);
102 netdev_vport = netdev_vport_priv(vport);
104 netdev_vport->dev = dev_get_by_name(&init_net, parms->name);
105 if (!netdev_vport->dev) {
107 goto error_free_vport;
110 if (netdev_vport->dev->flags & IFF_LOOPBACK ||
111 netdev_vport->dev->type != ARPHRD_ETHER ||
112 is_internal_dev(netdev_vport->dev)) {
117 /* If we are using the vport stats layer initialize it to the current
118 * values so we are roughly consistent with the device stats. */
119 if (USE_VPORT_STATS) {
120 struct rtnl_link_stats64 stats;
122 err = netdev_get_stats(vport, &stats);
124 vport_set_stats(vport, &stats);
127 err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
132 dev_set_promiscuity(netdev_vport->dev, 1);
133 dev_disable_lro(netdev_vport->dev);
134 netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
139 dev_put(netdev_vport->dev);
146 static int netdev_destroy(struct vport *vport)
148 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
150 netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
151 netdev_rx_handler_unregister(netdev_vport->dev);
152 dev_set_promiscuity(netdev_vport->dev, -1);
156 dev_put(netdev_vport->dev);
162 int netdev_set_mtu(struct vport *vport, int mtu)
164 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
165 return dev_set_mtu(netdev_vport->dev, mtu);
168 int netdev_set_addr(struct vport *vport, const unsigned char *addr)
170 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
173 sa.sa_family = ARPHRD_ETHER;
174 memcpy(sa.sa_data, addr, ETH_ALEN);
176 return dev_set_mac_address(netdev_vport->dev, &sa);
179 const char *netdev_get_name(const struct vport *vport)
181 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
182 return netdev_vport->dev->name;
185 const unsigned char *netdev_get_addr(const struct vport *vport)
187 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
188 return netdev_vport->dev->dev_addr;
191 struct kobject *netdev_get_kobj(const struct vport *vport)
193 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
194 return &netdev_vport->dev->NETDEV_DEV_MEMBER.kobj;
197 int netdev_get_stats(const struct vport *vport, struct rtnl_link_stats64 *stats)
199 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
200 dev_get_stats(netdev_vport->dev, stats);
204 unsigned netdev_get_dev_flags(const struct vport *vport)
206 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
207 return dev_get_flags(netdev_vport->dev);
210 int netdev_is_running(const struct vport *vport)
212 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
213 return netif_running(netdev_vport->dev);
216 unsigned char netdev_get_operstate(const struct vport *vport)
218 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
219 return netdev_vport->dev->operstate;
222 int netdev_get_ifindex(const struct vport *vport)
224 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
225 return netdev_vport->dev->ifindex;
228 int netdev_get_iflink(const struct vport *vport)
230 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
231 return netdev_vport->dev->iflink;
234 int netdev_get_mtu(const struct vport *vport)
236 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
237 return netdev_vport->dev->mtu;
240 /* Must be called with rcu_read_lock. */
241 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
243 /* Make our own copy of the packet. Otherwise we will mangle the
244 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
245 * (No one comes after us, since we tell handle_bridge() that we took
247 skb = skb_share_check(skb, GFP_ATOMIC);
251 skb_warn_if_lro(skb);
253 skb_push(skb, ETH_HLEN);
254 compute_ip_summed(skb, false);
256 vport_receive(vport, skb);
259 static int netdev_send(struct vport *vport, struct sk_buff *skb)
261 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
264 skb->dev = netdev_vport->dev;
265 forward_ip_summed(skb);
271 /* Returns null if this device is not attached to a datapath. */
272 struct vport *netdev_get_vport(struct net_device *dev)
274 #ifdef IFF_BRIDGE_PORT
275 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
276 if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
278 if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))
280 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
284 return (struct vport *)rcu_dereference_rtnl(dev->br_port);
288 const struct vport_ops netdev_vport_ops = {
289 .type = ODP_VPORT_TYPE_NETDEV,
290 .flags = (VPORT_F_REQUIRED |
291 (USE_VPORT_STATS ? VPORT_F_GEN_STATS : 0)),
294 .create = netdev_create,
295 .destroy = netdev_destroy,
296 .set_mtu = netdev_set_mtu,
297 .set_addr = netdev_set_addr,
298 .get_name = netdev_get_name,
299 .get_addr = netdev_get_addr,
300 .get_kobj = netdev_get_kobj,
301 .get_stats = netdev_get_stats,
302 .get_dev_flags = netdev_get_dev_flags,
303 .is_running = netdev_is_running,
304 .get_operstate = netdev_get_operstate,
305 .get_ifindex = netdev_get_ifindex,
306 .get_iflink = netdev_get_iflink,
307 .get_mtu = netdev_get_mtu,
311 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
313 * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
314 * the Linux bridge module on any released version of Linux, because there
315 * is only a single bridge hook function and only a single br_port member
316 * in struct net_device.
318 * Declaring and exporting this symbol enforces mutual exclusion. The bridge
319 * module also exports the same symbol, so the module loader will refuse to
320 * load both modules at the same time (e.g. "bridge: exports duplicate symbol
321 * br_should_route_hook (owned by openvswitch_mod)").
323 * The use of "typeof" here avoids the need to track changes in the type of
324 * br_should_route_hook over various kernel versions.
326 typeof(br_should_route_hook) br_should_route_hook;
327 EXPORT_SYMBOL(br_should_route_hook);