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 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
40 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
41 /* Called with rcu_read_lock and bottom-halves disabled. */
42 static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
44 struct sk_buff *skb = *pskb;
47 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
48 return RX_HANDLER_PASS;
50 vport = netdev_get_vport(skb->dev);
52 netdev_port_receive(vport, skb);
54 return RX_HANDLER_CONSUMED;
56 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
57 /* Called with rcu_read_lock and bottom-halves disabled. */
58 static struct sk_buff *netdev_frame_hook(struct sk_buff *skb)
62 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
65 vport = netdev_get_vport(skb->dev);
67 netdev_port_receive(vport, skb);
71 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
73 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
74 * different set of devices!)
76 /* Called with rcu_read_lock and bottom-halves disabled. */
77 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
80 netdev_port_receive((struct vport *)p, skb);
83 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
85 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
86 * different set of devices!)
88 /* Called with rcu_read_lock and bottom-halves disabled. */
89 static int netdev_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
91 netdev_port_receive((struct vport *)p, *pskb);
98 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
99 static int netdev_init(void) { return 0; }
100 static void netdev_exit(void) { }
102 static int netdev_init(void)
104 /* Hook into callback used by the bridge to intercept packets.
105 * Parasites we are. */
106 br_handle_frame_hook = netdev_frame_hook;
111 static void netdev_exit(void)
113 br_handle_frame_hook = NULL;
117 static struct vport *netdev_create(const struct vport_parms *parms)
120 struct netdev_vport *netdev_vport;
123 vport = vport_alloc(sizeof(struct netdev_vport), &netdev_vport_ops, parms);
125 err = PTR_ERR(vport);
129 netdev_vport = netdev_vport_priv(vport);
131 netdev_vport->dev = dev_get_by_name(&init_net, parms->name);
132 if (!netdev_vport->dev) {
134 goto error_free_vport;
137 if (netdev_vport->dev->flags & IFF_LOOPBACK ||
138 netdev_vport->dev->type != ARPHRD_ETHER ||
139 is_internal_dev(netdev_vport->dev)) {
144 err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
149 dev_set_promiscuity(netdev_vport->dev, 1);
150 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
151 dev_disable_lro(netdev_vport->dev);
153 netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
158 dev_put(netdev_vport->dev);
165 static void netdev_destroy(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);
175 dev_put(netdev_vport->dev);
179 int netdev_set_addr(struct vport *vport, const unsigned char *addr)
181 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
184 sa.sa_family = ARPHRD_ETHER;
185 memcpy(sa.sa_data, addr, ETH_ALEN);
187 return dev_set_mac_address(netdev_vport->dev, &sa);
190 const char *netdev_get_name(const struct vport *vport)
192 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
193 return netdev_vport->dev->name;
196 const unsigned char *netdev_get_addr(const struct vport *vport)
198 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
199 return netdev_vport->dev->dev_addr;
202 struct kobject *netdev_get_kobj(const struct vport *vport)
204 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
205 return &netdev_vport->dev->NETDEV_DEV_MEMBER.kobj;
208 unsigned netdev_get_dev_flags(const struct vport *vport)
210 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
211 return dev_get_flags(netdev_vport->dev);
214 int netdev_is_running(const struct vport *vport)
216 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
217 return netif_running(netdev_vport->dev);
220 unsigned char netdev_get_operstate(const struct vport *vport)
222 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
223 return netdev_vport->dev->operstate;
226 int netdev_get_ifindex(const struct vport *vport)
228 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
229 return netdev_vport->dev->ifindex;
232 int netdev_get_mtu(const struct vport *vport)
234 const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
235 return netdev_vport->dev->mtu;
238 /* Must be called with rcu_read_lock. */
239 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
241 if (unlikely(!vport)) {
246 /* Make our own copy of the packet. Otherwise we will mangle the
247 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
248 * (No one comes after us, since we tell handle_bridge() that we took
250 skb = skb_share_check(skb, GFP_ATOMIC);
254 skb_push(skb, ETH_HLEN);
256 if (unlikely(compute_ip_summed(skb, false))) {
260 vlan_copy_skb_tci(skb);
262 vport_receive(vport, skb);
265 static inline unsigned packet_length(const struct sk_buff *skb)
267 unsigned length = skb->len - ETH_HLEN;
269 if (skb->protocol == htons(ETH_P_8021Q))
275 static bool dev_supports_vlan_tx(struct net_device *dev)
277 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
278 /* Software fallback means every device supports vlan_tci on TX. */
280 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
281 return dev->features & NETIF_F_HW_VLAN_TX;
283 /* Assume that the driver is buggy. */
288 static int netdev_send(struct vport *vport, struct sk_buff *skb)
290 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
291 int mtu = netdev_vport->dev->mtu;
294 if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
296 pr_warn("%s: dropped over-mtu packet: %d > %d\n",
297 dp_name(vport->dp), packet_length(skb), mtu);
301 if (unlikely(skb_warn_if_lro(skb)))
304 skb->dev = netdev_vport->dev;
305 forward_ip_summed(skb, true);
307 if (vlan_tx_tag_present(skb) && !dev_supports_vlan_tx(skb->dev)) {
310 features = netif_skb_features(skb);
313 features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
314 NETIF_F_UFO | NETIF_F_FSO);
316 if (netif_needs_gso(skb, features)) {
317 struct sk_buff *nskb;
319 nskb = skb_gso_segment(skb, features);
321 if (unlikely(skb_cloned(skb) &&
322 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
327 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
343 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
346 vlan_set_tci(skb, 0);
357 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
360 vlan_set_tci(skb, 0);
370 vport_record_error(vport, VPORT_E_TX_DROPPED);
374 /* Returns null if this device is not attached to a datapath. */
375 struct vport *netdev_get_vport(struct net_device *dev)
377 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
378 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
379 if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
381 if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))
383 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
387 return (struct vport *)rcu_dereference_rtnl(dev->br_port);
391 const struct vport_ops netdev_vport_ops = {
392 .type = OVS_VPORT_TYPE_NETDEV,
393 .flags = VPORT_F_REQUIRED,
396 .create = netdev_create,
397 .destroy = netdev_destroy,
398 .set_addr = netdev_set_addr,
399 .get_name = netdev_get_name,
400 .get_addr = netdev_get_addr,
401 .get_kobj = netdev_get_kobj,
402 .get_dev_flags = netdev_get_dev_flags,
403 .is_running = netdev_is_running,
404 .get_operstate = netdev_get_operstate,
405 .get_ifindex = netdev_get_ifindex,
406 .get_mtu = netdev_get_mtu,
410 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
412 * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
413 * the Linux bridge module on any released version of Linux, because there
414 * is only a single bridge hook function and only a single br_port member
415 * in struct net_device.
417 * Declaring and exporting this symbol enforces mutual exclusion. The bridge
418 * module also exports the same symbol, so the module loader will refuse to
419 * load both modules at the same time (e.g. "bridge: exports duplicate symbol
420 * br_should_route_hook (owned by openvswitch_mod)").
422 * The use of "typeof" here avoids the need to track changes in the type of
423 * br_should_route_hook over various kernel versions.
425 typeof(br_should_route_hook) br_should_route_hook;
426 EXPORT_SYMBOL(br_should_route_hook);