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 !defined(HAVE_VLAN_BUG_WORKAROUND)
27 #include <linux/module.h>
29 static int vlan_tso __read_mostly
= 0;
30 module_param(vlan_tso
, int, 0644);
31 MODULE_PARM_DESC(vlan_tso
, "Enable TSO for VLAN packets");
36 /* If the native device stats aren't 64 bit use the vport stats tracking instead. */
37 #define USE_VPORT_STATS (sizeof(((struct net_device_stats *)0)->rx_bytes) < sizeof(u64))
39 static void netdev_port_receive(struct vport
*vport
, struct sk_buff
*skb
);
41 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
42 /* Called with rcu_read_lock and bottom-halves disabled. */
43 static struct sk_buff
*netdev_frame_hook(struct sk_buff
*skb
)
47 if (unlikely(skb
->pkt_type
== PACKET_LOOPBACK
))
50 vport
= netdev_get_vport(skb
->dev
);
52 netdev_port_receive(vport
, skb
);
56 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
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 struct sk_buff
*netdev_frame_hook(struct net_bridge_port
*p
,
65 netdev_port_receive((struct vport
*)p
, skb
);
68 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
70 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
71 * different set of devices!)
73 /* Called with rcu_read_lock and bottom-halves disabled. */
74 static int netdev_frame_hook(struct net_bridge_port
*p
, struct sk_buff
**pskb
)
76 netdev_port_receive((struct vport
*)p
, *pskb
);
83 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
84 static int netdev_init(void) { return 0; }
85 static void netdev_exit(void) { }
87 static int netdev_init(void)
89 /* Hook into callback used by the bridge to intercept packets.
90 * Parasites we are. */
91 br_handle_frame_hook
= netdev_frame_hook
;
96 static void netdev_exit(void)
98 br_handle_frame_hook
= NULL
;
102 static struct vport
*netdev_create(const struct vport_parms
*parms
)
105 struct netdev_vport
*netdev_vport
;
108 vport
= vport_alloc(sizeof(struct netdev_vport
), &netdev_vport_ops
, parms
);
110 err
= PTR_ERR(vport
);
114 netdev_vport
= netdev_vport_priv(vport
);
116 netdev_vport
->dev
= dev_get_by_name(&init_net
, parms
->name
);
117 if (!netdev_vport
->dev
) {
119 goto error_free_vport
;
122 if (netdev_vport
->dev
->flags
& IFF_LOOPBACK
||
123 netdev_vport
->dev
->type
!= ARPHRD_ETHER
||
124 is_internal_dev(netdev_vport
->dev
)) {
129 /* If we are using the vport stats layer initialize it to the current
130 * values so we are roughly consistent with the device stats. */
131 if (USE_VPORT_STATS
) {
132 struct rtnl_link_stats64 stats
;
134 err
= netdev_get_stats(vport
, &stats
);
136 vport_set_stats(vport
, &stats
);
139 err
= netdev_rx_handler_register(netdev_vport
->dev
, netdev_frame_hook
,
144 dev_set_promiscuity(netdev_vport
->dev
, 1);
145 dev_disable_lro(netdev_vport
->dev
);
146 netdev_vport
->dev
->priv_flags
|= IFF_OVS_DATAPATH
;
151 dev_put(netdev_vport
->dev
);
158 static int netdev_destroy(struct vport
*vport
)
160 struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
162 netdev_vport
->dev
->priv_flags
&= ~IFF_OVS_DATAPATH
;
163 netdev_rx_handler_unregister(netdev_vport
->dev
);
164 dev_set_promiscuity(netdev_vport
->dev
, -1);
168 dev_put(netdev_vport
->dev
);
174 int netdev_set_mtu(struct vport
*vport
, int mtu
)
176 struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
177 return dev_set_mtu(netdev_vport
->dev
, mtu
);
180 int netdev_set_addr(struct vport
*vport
, const unsigned char *addr
)
182 struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
185 sa
.sa_family
= ARPHRD_ETHER
;
186 memcpy(sa
.sa_data
, addr
, ETH_ALEN
);
188 return dev_set_mac_address(netdev_vport
->dev
, &sa
);
191 const char *netdev_get_name(const struct vport
*vport
)
193 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
194 return netdev_vport
->dev
->name
;
197 const unsigned char *netdev_get_addr(const struct vport
*vport
)
199 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
200 return netdev_vport
->dev
->dev_addr
;
203 struct kobject
*netdev_get_kobj(const struct vport
*vport
)
205 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
206 return &netdev_vport
->dev
->NETDEV_DEV_MEMBER
.kobj
;
209 int netdev_get_stats(const struct vport
*vport
, struct rtnl_link_stats64
*stats
)
211 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
212 dev_get_stats(netdev_vport
->dev
, stats
);
216 unsigned netdev_get_dev_flags(const struct vport
*vport
)
218 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
219 return dev_get_flags(netdev_vport
->dev
);
222 int netdev_is_running(const struct vport
*vport
)
224 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
225 return netif_running(netdev_vport
->dev
);
228 unsigned char netdev_get_operstate(const struct vport
*vport
)
230 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
231 return netdev_vport
->dev
->operstate
;
234 int netdev_get_ifindex(const struct vport
*vport
)
236 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
237 return netdev_vport
->dev
->ifindex
;
240 int netdev_get_iflink(const struct vport
*vport
)
242 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
243 return netdev_vport
->dev
->iflink
;
246 int netdev_get_mtu(const struct vport
*vport
)
248 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
249 return netdev_vport
->dev
->mtu
;
252 /* Must be called with rcu_read_lock. */
253 static void netdev_port_receive(struct vport
*vport
, struct sk_buff
*skb
)
255 /* Make our own copy of the packet. Otherwise we will mangle the
256 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
257 * (No one comes after us, since we tell handle_bridge() that we took
259 skb
= skb_share_check(skb
, GFP_ATOMIC
);
263 skb_warn_if_lro(skb
);
265 skb_push(skb
, ETH_HLEN
);
266 compute_ip_summed(skb
, false);
267 vlan_copy_skb_tci(skb
);
269 vport_receive(vport
, skb
);
272 static bool dev_supports_vlan_tx(struct net_device
*dev
)
274 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
275 /* Software fallback means every device supports vlan_tci on TX. */
277 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
278 return dev
->features
& NETIF_F_HW_VLAN_TX
;
280 /* Assume that the driver is buggy. */
285 static int netdev_send(struct vport
*vport
, struct sk_buff
*skb
)
287 struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
290 skb
->dev
= netdev_vport
->dev
;
291 forward_ip_summed(skb
);
293 if (vlan_tx_tag_present(skb
) && !dev_supports_vlan_tx(skb
->dev
)) {
297 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
298 features
= skb
->dev
->features
& skb
->dev
->vlan_features
;
301 err
= vswitch_skb_checksum_setup(skb
);
308 features
&= ~(NETIF_F_TSO
| NETIF_F_TSO6
|
309 NETIF_F_UFO
| NETIF_F_FSO
);
311 if (skb_is_gso(skb
) &&
312 (!skb_gso_ok(skb
, features
) ||
313 unlikely(skb
->ip_summed
!= CHECKSUM_PARTIAL
))) {
314 struct sk_buff
*nskb
;
316 nskb
= skb_gso_segment(skb
, features
);
318 if (unlikely(skb_cloned(skb
) &&
319 pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))) {
324 skb_shinfo(skb
)->gso_type
&= ~SKB_GSO_DODGY
;
338 skb
= __vlan_put_tag(skb
, vlan_tx_tag_get(skb
));
341 vlan_set_tci(skb
, 0);
352 skb
= __vlan_put_tag(skb
, vlan_tx_tag_get(skb
));
355 vlan_set_tci(skb
, 0);
364 /* Returns null if this device is not attached to a datapath. */
365 struct vport
*netdev_get_vport(struct net_device
*dev
)
367 #ifdef IFF_BRIDGE_PORT
368 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
369 if (likely(dev
->priv_flags
& IFF_OVS_DATAPATH
))
371 if (likely(rcu_access_pointer(dev
->rx_handler
) == netdev_frame_hook
))
373 return (struct vport
*)rcu_dereference_rtnl(dev
->rx_handler_data
);
377 return (struct vport
*)rcu_dereference_rtnl(dev
->br_port
);
381 const struct vport_ops netdev_vport_ops
= {
382 .type
= ODP_VPORT_TYPE_NETDEV
,
383 .flags
= (VPORT_F_REQUIRED
|
384 (USE_VPORT_STATS
? VPORT_F_GEN_STATS
: 0)),
387 .create
= netdev_create
,
388 .destroy
= netdev_destroy
,
389 .set_mtu
= netdev_set_mtu
,
390 .set_addr
= netdev_set_addr
,
391 .get_name
= netdev_get_name
,
392 .get_addr
= netdev_get_addr
,
393 .get_kobj
= netdev_get_kobj
,
394 .get_stats
= netdev_get_stats
,
395 .get_dev_flags
= netdev_get_dev_flags
,
396 .is_running
= netdev_is_running
,
397 .get_operstate
= netdev_get_operstate
,
398 .get_ifindex
= netdev_get_ifindex
,
399 .get_iflink
= netdev_get_iflink
,
400 .get_mtu
= netdev_get_mtu
,
404 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
406 * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
407 * the Linux bridge module on any released version of Linux, because there
408 * is only a single bridge hook function and only a single br_port member
409 * in struct net_device.
411 * Declaring and exporting this symbol enforces mutual exclusion. The bridge
412 * module also exports the same symbol, so the module loader will refuse to
413 * load both modules at the same time (e.g. "bridge: exports duplicate symbol
414 * br_should_route_hook (owned by openvswitch_mod)").
416 * The use of "typeof" here avoids the need to track changes in the type of
417 * br_should_route_hook over various kernel versions.
419 typeof(br_should_route_hook
) br_should_route_hook
;
420 EXPORT_SYMBOL(br_should_route_hook
);