2 * Copyright (c) 2010 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>
20 #include "vport-internal_dev.h"
21 #include "vport-netdev.h"
25 /* If the native device stats aren't 64 bit use the vport stats tracking instead. */
26 #define USE_VPORT_STATS (sizeof(((struct net_device_stats *)0)->rx_bytes) < sizeof(u64))
28 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
30 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
31 /* Called with rcu_read_lock and bottom-halves disabled. */
32 static struct sk_buff *netdev_frame_hook(struct sk_buff *skb)
36 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
39 vport = netdev_get_vport(skb->dev);
41 netdev_port_receive(vport, skb);
45 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
47 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
48 * different set of devices!)
50 /* Called with rcu_read_lock and bottom-halves disabled. */
51 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
54 netdev_port_receive((struct vport *)p, skb);
57 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
59 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
60 * different set of devices!)
62 /* Called with rcu_read_lock and bottom-halves disabled. */
63 static int netdev_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
65 netdev_port_receive((struct vport *)p, *pskb);
72 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
73 static int netdev_init(void) { return 0; }
74 static void netdev_exit(void) { }
76 static int netdev_init(void)
78 /* Hook into callback used by the bridge to intercept packets.
79 * Parasites we are. */
80 br_handle_frame_hook = netdev_frame_hook;
85 static void netdev_exit(void)
87 br_handle_frame_hook = NULL;
91 static struct vport *netdev_create(const struct vport_parms *parms)
94 struct netdev_vport *netdev_vport;
97 vport = vport_alloc(sizeof(struct netdev_vport), &netdev_vport_ops, parms);
103 netdev_vport = netdev_vport_priv(vport);
105 netdev_vport->dev = dev_get_by_name(&init_net, parms->name);
106 if (!netdev_vport->dev) {
108 goto error_free_vport;
111 if (netdev_vport->dev->flags & IFF_LOOPBACK ||
112 netdev_vport->dev->type != ARPHRD_ETHER ||
113 is_internal_dev(netdev_vport->dev)) {
118 /* If we are using the vport stats layer initialize it to the current
119 * values so we are roughly consistent with the device stats. */
120 if (USE_VPORT_STATS) {
121 struct rtnl_link_stats64 stats;
123 err = netdev_get_stats(vport, &stats);
125 vport_set_stats(vport, &stats);
131 dev_put(netdev_vport->dev);
138 static int netdev_destroy(struct vport *vport)
140 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
142 dev_put(netdev_vport->dev);
148 static int netdev_attach(struct vport *vport)
150 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
153 err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
158 dev_set_promiscuity(netdev_vport->dev, 1);
159 dev_disable_lro(netdev_vport->dev);
160 netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
165 static int netdev_detach(struct vport *vport)
167 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
169 netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
170 netdev_rx_handler_unregister(netdev_vport->dev);
171 dev_set_promiscuity(netdev_vport->dev, -1);
176 int netdev_set_mtu(struct vport *vport, int mtu)
178 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
179 return dev_set_mtu(netdev_vport->dev, mtu);
182 int netdev_set_addr(struct vport *vport, const unsigned char *addr)
184 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
187 sa.sa_family = ARPHRD_ETHER;
188 memcpy(sa.sa_data, addr, ETH_ALEN);
190 return dev_set_mac_address(netdev_vport->dev, &sa);
193 const char *netdev_get_name(const struct vport *vport)
195 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
196 return netdev_vport->dev->name;
199 const unsigned char *netdev_get_addr(const struct vport *vport)
201 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
202 return netdev_vport->dev->dev_addr;
205 struct kobject *netdev_get_kobj(const struct vport *vport)
207 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
208 return &netdev_vport->dev->NETDEV_DEV_MEMBER.kobj;
211 int netdev_get_stats(const struct vport *vport, struct rtnl_link_stats64 *stats)
213 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
214 dev_get_stats(netdev_vport->dev, stats);
218 unsigned netdev_get_dev_flags(const struct vport *vport)
220 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
221 return dev_get_flags(netdev_vport->dev);
224 int netdev_is_running(const struct vport *vport)
226 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
227 return netif_running(netdev_vport->dev);
230 unsigned char netdev_get_operstate(const struct vport *vport)
232 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
233 return netdev_vport->dev->operstate;
236 int netdev_get_ifindex(const struct vport *vport)
238 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
239 return netdev_vport->dev->ifindex;
242 int netdev_get_iflink(const struct vport *vport)
244 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
245 return netdev_vport->dev->iflink;
248 int netdev_get_mtu(const struct vport *vport)
250 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
251 return netdev_vport->dev->mtu;
254 /* Must be called with rcu_read_lock. */
255 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
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
261 skb = skb_share_check(skb, GFP_ATOMIC);
265 skb_warn_if_lro(skb);
267 skb_push(skb, ETH_HLEN);
268 compute_ip_summed(skb, false);
270 vport_receive(vport, skb);
273 static int netdev_send(struct vport *vport, struct sk_buff *skb)
275 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
278 skb->dev = netdev_vport->dev;
279 forward_ip_summed(skb);
285 /* Returns null if this device is not attached to a datapath. */
286 struct vport *netdev_get_vport(struct net_device *dev)
288 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
289 /* XXX: The bridge code may have registered the data.
290 * So check that the handler pointer is the datapath's.
291 * Once the merge is done and IFF_OVS_DATAPATH stops
292 * being the same value as IFF_BRIDGE_PORT the check can
293 * simply be netdev_vport->dev->priv_flags & IFF_OVS_DATAPATH. */
294 if (rcu_dereference(dev->rx_handler) != netdev_frame_hook)
296 return (struct vport *)rcu_dereference(dev->rx_handler_data);
298 return (struct vport *)rcu_dereference(dev->br_port);
302 const struct vport_ops netdev_vport_ops = {
304 .flags = (VPORT_F_REQUIRED |
305 (USE_VPORT_STATS ? VPORT_F_GEN_STATS : 0)),
308 .create = netdev_create,
309 .destroy = netdev_destroy,
310 .attach = netdev_attach,
311 .detach = netdev_detach,
312 .set_mtu = netdev_set_mtu,
313 .set_addr = netdev_set_addr,
314 .get_name = netdev_get_name,
315 .get_addr = netdev_get_addr,
316 .get_kobj = netdev_get_kobj,
317 .get_stats = netdev_get_stats,
318 .get_dev_flags = netdev_get_dev_flags,
319 .is_running = netdev_is_running,
320 .get_operstate = netdev_get_operstate,
321 .get_ifindex = netdev_get_ifindex,
322 .get_iflink = netdev_get_iflink,
323 .get_mtu = netdev_get_mtu,
327 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
329 * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
330 * the Linux bridge module on any released version of Linux, because there
331 * is only a single bridge hook function and only a single br_port member
332 * in struct net_device.
334 * Declaring and exporting this symbol enforces mutual exclusion. The bridge
335 * module also exports the same symbol, so the module loader will refuse to
336 * load both modules at the same time (e.g. "bridge: exports duplicate symbol
337 * br_should_route_hook (owned by openvswitch_mod)").
339 * The use of "typeof" here avoids the need to track changes in the type of
340 * br_should_route_hook over various kernel versions.
342 typeof(br_should_route_hook) br_should_route_hook;
343 EXPORT_SYMBOL(br_should_route_hook);