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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
24 #include "vport-internal_dev.h"
25 #include "vport-netdev.h"
27 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) && \
28 !defined(HAVE_VLAN_BUG_WORKAROUND)
29 #include <linux/module.h>
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");
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))
41 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
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)
47 struct sk_buff *skb = *pskb;
50 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
51 return RX_HANDLER_PASS;
53 vport = netdev_get_vport(skb->dev);
55 netdev_port_receive(vport, skb);
57 return RX_HANDLER_CONSUMED;
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)
65 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
68 vport = netdev_get_vport(skb->dev);
70 netdev_port_receive(vport, skb);
74 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
76 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
77 * different set of devices!)
79 /* Called with rcu_read_lock and bottom-halves disabled. */
80 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
83 netdev_port_receive((struct vport *)p, skb);
86 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
88 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
89 * different set of devices!)
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)
94 netdev_port_receive((struct vport *)p, *pskb);
101 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
102 static int netdev_init(void) { return 0; }
103 static void netdev_exit(void) { }
105 static int netdev_init(void)
107 /* Hook into callback used by the bridge to intercept packets.
108 * Parasites we are. */
109 br_handle_frame_hook = netdev_frame_hook;
114 static void netdev_exit(void)
116 br_handle_frame_hook = NULL;
120 static struct vport *netdev_create(const struct vport_parms *parms)
123 struct netdev_vport *netdev_vport;
126 vport = vport_alloc(sizeof(struct netdev_vport), &netdev_vport_ops, parms);
128 err = PTR_ERR(vport);
132 netdev_vport = netdev_vport_priv(vport);
134 netdev_vport->dev = dev_get_by_name(&init_net, parms->name);
135 if (!netdev_vport->dev) {
137 goto error_free_vport;
140 if (netdev_vport->dev->flags & IFF_LOOPBACK ||
141 netdev_vport->dev->type != ARPHRD_ETHER ||
142 is_internal_dev(netdev_vport->dev)) {
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;
152 err = netdev_get_stats(vport, &stats);
154 vport_set_stats(vport, &stats);
157 err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
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);
166 netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
171 dev_put(netdev_vport->dev);
178 static int netdev_destroy(struct vport *vport)
180 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
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);
188 dev_put(netdev_vport->dev);
194 int netdev_set_mtu(struct vport *vport, int mtu)
196 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
197 return dev_set_mtu(netdev_vport->dev, mtu);
200 int netdev_set_addr(struct vport *vport, const unsigned char *addr)
202 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
205 sa.sa_family = ARPHRD_ETHER;
206 memcpy(sa.sa_data, addr, ETH_ALEN);
208 return dev_set_mac_address(netdev_vport->dev, &sa);
211 const char *netdev_get_name(const struct vport *vport)
213 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
214 return netdev_vport->dev->name;
217 const unsigned char *netdev_get_addr(const struct vport *vport)
219 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
220 return netdev_vport->dev->dev_addr;
223 struct kobject *netdev_get_kobj(const struct vport *vport)
225 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
226 return &netdev_vport->dev->NETDEV_DEV_MEMBER.kobj;
229 int netdev_get_stats(const struct vport *vport, struct rtnl_link_stats64 *stats)
231 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
232 dev_get_stats(netdev_vport->dev, stats);
236 unsigned netdev_get_dev_flags(const struct vport *vport)
238 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
239 return dev_get_flags(netdev_vport->dev);
242 int netdev_is_running(const struct vport *vport)
244 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
245 return netif_running(netdev_vport->dev);
248 unsigned char netdev_get_operstate(const struct vport *vport)
250 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
251 return netdev_vport->dev->operstate;
254 int netdev_get_ifindex(const struct vport *vport)
256 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
257 return netdev_vport->dev->ifindex;
260 int netdev_get_mtu(const struct vport *vport)
262 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
263 return netdev_vport->dev->mtu;
266 /* Must be called with rcu_read_lock. */
267 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
269 if (unlikely(!vport)) {
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
278 skb = skb_share_check(skb, GFP_ATOMIC);
282 skb_push(skb, ETH_HLEN);
284 if (unlikely(compute_ip_summed(skb, false))) {
288 vlan_copy_skb_tci(skb);
290 vport_receive(vport, skb);
293 static inline unsigned packet_length(const struct sk_buff *skb)
295 unsigned length = skb->len - ETH_HLEN;
297 if (skb->protocol == htons(ETH_P_8021Q))
303 static bool dev_supports_vlan_tx(struct net_device *dev)
305 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
306 /* Software fallback means every device supports vlan_tci on TX. */
308 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
309 return dev->features & NETIF_F_HW_VLAN_TX;
311 /* Assume that the driver is buggy. */
316 static int netdev_send(struct vport *vport, struct sk_buff *skb)
318 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
319 int mtu = netdev_vport->dev->mtu;
322 if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
324 pr_warn("%s: dropped over-mtu packet: %d > %d\n",
325 dp_name(vport->dp), packet_length(skb), mtu);
329 if (unlikely(skb_warn_if_lro(skb)))
332 skb->dev = netdev_vport->dev;
333 forward_ip_summed(skb, true);
335 if (vlan_tx_tag_present(skb) && !dev_supports_vlan_tx(skb->dev)) {
338 features = netif_skb_features(skb);
341 features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
342 NETIF_F_UFO | NETIF_F_FSO);
344 if (netif_needs_gso(skb, features)) {
345 struct sk_buff *nskb;
347 nskb = skb_gso_segment(skb, features);
349 if (unlikely(skb_cloned(skb) &&
350 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
355 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
371 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
374 vlan_set_tci(skb, 0);
385 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
388 vlan_set_tci(skb, 0);
398 vport_record_error(vport, VPORT_E_TX_DROPPED);
402 /* Returns null if this device is not attached to a datapath. */
403 struct vport *netdev_get_vport(struct net_device *dev)
405 #ifdef IFF_BRIDGE_PORT
406 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
407 if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
409 if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))
411 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
415 return (struct vport *)rcu_dereference_rtnl(dev->br_port);
419 const struct vport_ops netdev_vport_ops = {
420 .type = OVS_VPORT_TYPE_NETDEV,
421 .flags = (VPORT_F_REQUIRED |
422 (USE_VPORT_STATS ? VPORT_F_GEN_STATS : 0)),
425 .create = netdev_create,
426 .destroy = netdev_destroy,
427 .set_mtu = netdev_set_mtu,
428 .set_addr = netdev_set_addr,
429 .get_name = netdev_get_name,
430 .get_addr = netdev_get_addr,
431 .get_kobj = netdev_get_kobj,
432 .get_stats = netdev_get_stats,
433 .get_dev_flags = netdev_get_dev_flags,
434 .is_running = netdev_is_running,
435 .get_operstate = netdev_get_operstate,
436 .get_ifindex = netdev_get_ifindex,
437 .get_mtu = netdev_get_mtu,
441 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
443 * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
444 * the Linux bridge module on any released version of Linux, because there
445 * is only a single bridge hook function and only a single br_port member
446 * in struct net_device.
448 * Declaring and exporting this symbol enforces mutual exclusion. The bridge
449 * module also exports the same symbol, so the module loader will refuse to
450 * load both modules at the same time (e.g. "bridge: exports duplicate symbol
451 * br_should_route_hook (owned by openvswitch_mod)").
453 * The use of "typeof" here avoids the need to track changes in the type of
454 * br_should_route_hook over various kernel versions.
456 typeof(br_should_route_hook) br_should_route_hook;
457 EXPORT_SYMBOL(br_should_route_hook);