2 * Copyright (c) 2007-2011 Nicira Networks.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/if_arp.h>
22 #include <linux/if_bridge.h>
23 #include <linux/if_vlan.h>
24 #include <linux/kernel.h>
25 #include <linux/llc.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/skbuff.h>
34 #include "vport-internal_dev.h"
35 #include "vport-netdev.h"
37 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) && \
38 !defined(HAVE_VLAN_BUG_WORKAROUND)
39 #include <linux/module.h>
41 static int vlan_tso __read_mostly
;
42 module_param(vlan_tso
, int, 0644);
43 MODULE_PARM_DESC(vlan_tso
, "Enable TSO for VLAN packets");
48 static void netdev_port_receive(struct vport
*vport
, struct sk_buff
*skb
);
50 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
51 /* Called with rcu_read_lock and bottom-halves disabled. */
52 static rx_handler_result_t
netdev_frame_hook(struct sk_buff
**pskb
)
54 struct sk_buff
*skb
= *pskb
;
57 if (unlikely(skb
->pkt_type
== PACKET_LOOPBACK
))
58 return RX_HANDLER_PASS
;
60 vport
= netdev_get_vport(skb
->dev
);
62 netdev_port_receive(vport
, skb
);
64 return RX_HANDLER_CONSUMED
;
66 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
67 /* Called with rcu_read_lock and bottom-halves disabled. */
68 static struct sk_buff
*netdev_frame_hook(struct sk_buff
*skb
)
72 if (unlikely(skb
->pkt_type
== PACKET_LOOPBACK
))
75 vport
= netdev_get_vport(skb
->dev
);
77 netdev_port_receive(vport
, skb
);
81 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
83 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
84 * different set of devices!)
86 /* Called with rcu_read_lock and bottom-halves disabled. */
87 static struct sk_buff
*netdev_frame_hook(struct net_bridge_port
*p
,
90 netdev_port_receive((struct vport
*)p
, skb
);
93 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
95 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
96 * different set of devices!)
98 /* Called with rcu_read_lock and bottom-halves disabled. */
99 static int netdev_frame_hook(struct net_bridge_port
*p
, struct sk_buff
**pskb
)
101 netdev_port_receive((struct vport
*)p
, *pskb
);
108 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
109 static int netdev_init(void) { return 0; }
110 static void netdev_exit(void) { }
112 static int netdev_init(void)
114 /* Hook into callback used by the bridge to intercept packets.
115 * Parasites we are. */
116 br_handle_frame_hook
= netdev_frame_hook
;
121 static void netdev_exit(void)
123 br_handle_frame_hook
= NULL
;
127 static struct vport
*netdev_create(const struct vport_parms
*parms
)
130 struct netdev_vport
*netdev_vport
;
133 vport
= vport_alloc(sizeof(struct netdev_vport
),
134 &netdev_vport_ops
, parms
);
136 err
= PTR_ERR(vport
);
140 netdev_vport
= netdev_vport_priv(vport
);
142 netdev_vport
->dev
= dev_get_by_name(&init_net
, parms
->name
);
143 if (!netdev_vport
->dev
) {
145 goto error_free_vport
;
148 if (netdev_vport
->dev
->flags
& IFF_LOOPBACK
||
149 netdev_vport
->dev
->type
!= ARPHRD_ETHER
||
150 is_internal_dev(netdev_vport
->dev
)) {
155 err
= netdev_rx_handler_register(netdev_vport
->dev
, netdev_frame_hook
,
160 dev_set_promiscuity(netdev_vport
->dev
, 1);
161 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
162 dev_disable_lro(netdev_vport
->dev
);
164 netdev_vport
->dev
->priv_flags
|= IFF_OVS_DATAPATH
;
169 dev_put(netdev_vport
->dev
);
176 static void netdev_destroy(struct vport
*vport
)
178 struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
180 netdev_vport
->dev
->priv_flags
&= ~IFF_OVS_DATAPATH
;
181 netdev_rx_handler_unregister(netdev_vport
->dev
);
182 dev_set_promiscuity(netdev_vport
->dev
, -1);
186 dev_put(netdev_vport
->dev
);
190 int netdev_set_addr(struct vport
*vport
, const unsigned char *addr
)
192 struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
195 sa
.sa_family
= ARPHRD_ETHER
;
196 memcpy(sa
.sa_data
, addr
, ETH_ALEN
);
198 return dev_set_mac_address(netdev_vport
->dev
, &sa
);
201 const char *netdev_get_name(const struct vport
*vport
)
203 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
204 return netdev_vport
->dev
->name
;
207 const unsigned char *netdev_get_addr(const struct vport
*vport
)
209 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
210 return netdev_vport
->dev
->dev_addr
;
213 struct kobject
*netdev_get_kobj(const struct vport
*vport
)
215 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
216 return &netdev_vport
->dev
->NETDEV_DEV_MEMBER
.kobj
;
219 unsigned netdev_get_dev_flags(const struct vport
*vport
)
221 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
222 return dev_get_flags(netdev_vport
->dev
);
225 int netdev_is_running(const struct vport
*vport
)
227 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
228 return netif_running(netdev_vport
->dev
);
231 unsigned char netdev_get_operstate(const struct vport
*vport
)
233 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
234 return netdev_vport
->dev
->operstate
;
237 int netdev_get_ifindex(const struct vport
*vport
)
239 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
240 return netdev_vport
->dev
->ifindex
;
243 int netdev_get_mtu(const struct vport
*vport
)
245 const struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
246 return netdev_vport
->dev
->mtu
;
249 /* Must be called with rcu_read_lock. */
250 static void netdev_port_receive(struct vport
*vport
, struct sk_buff
*skb
)
252 if (unlikely(!vport
)) {
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_push(skb
, ETH_HLEN
);
267 if (unlikely(compute_ip_summed(skb
, false))) {
271 vlan_copy_skb_tci(skb
);
273 vport_receive(vport
, skb
);
276 static unsigned packet_length(const struct sk_buff
*skb
)
278 unsigned length
= skb
->len
- ETH_HLEN
;
280 if (skb
->protocol
== htons(ETH_P_8021Q
))
286 static bool dev_supports_vlan_tx(struct net_device
*dev
)
288 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
289 /* Software fallback means every device supports vlan_tci on TX. */
291 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
292 return dev
->features
& NETIF_F_HW_VLAN_TX
;
294 /* Assume that the driver is buggy. */
299 static int netdev_send(struct vport
*vport
, struct sk_buff
*skb
)
301 struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
302 int mtu
= netdev_vport
->dev
->mtu
;
305 if (unlikely(packet_length(skb
) > mtu
&& !skb_is_gso(skb
))) {
307 pr_warn("%s: dropped over-mtu packet: %d > %d\n",
308 dp_name(vport
->dp
), packet_length(skb
), mtu
);
312 if (unlikely(skb_warn_if_lro(skb
)))
315 skb
->dev
= netdev_vport
->dev
;
316 forward_ip_summed(skb
, true);
318 if (vlan_tx_tag_present(skb
) && !dev_supports_vlan_tx(skb
->dev
)) {
321 features
= netif_skb_features(skb
);
324 features
&= ~(NETIF_F_TSO
| NETIF_F_TSO6
|
325 NETIF_F_UFO
| NETIF_F_FSO
);
327 if (netif_needs_gso(skb
, features
)) {
328 struct sk_buff
*nskb
;
330 nskb
= skb_gso_segment(skb
, features
);
332 if (unlikely(skb_cloned(skb
) &&
333 pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))) {
338 skb_shinfo(skb
)->gso_type
&= ~SKB_GSO_DODGY
;
354 skb
= __vlan_put_tag(skb
, vlan_tx_tag_get(skb
));
357 vlan_set_tci(skb
, 0);
368 skb
= __vlan_put_tag(skb
, vlan_tx_tag_get(skb
));
371 vlan_set_tci(skb
, 0);
381 vport_record_error(vport
, VPORT_E_TX_DROPPED
);
385 /* Returns null if this device is not attached to a datapath. */
386 struct vport
*netdev_get_vport(struct net_device
*dev
)
388 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
389 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
390 if (likely(dev
->priv_flags
& IFF_OVS_DATAPATH
))
392 if (likely(rcu_access_pointer(dev
->rx_handler
) == netdev_frame_hook
))
394 return (struct vport
*)rcu_dereference_rtnl(dev
->rx_handler_data
);
398 return (struct vport
*)rcu_dereference_rtnl(dev
->br_port
);
402 const struct vport_ops netdev_vport_ops
= {
403 .type
= OVS_VPORT_TYPE_NETDEV
,
404 .flags
= VPORT_F_REQUIRED
,
407 .create
= netdev_create
,
408 .destroy
= netdev_destroy
,
409 .set_addr
= netdev_set_addr
,
410 .get_name
= netdev_get_name
,
411 .get_addr
= netdev_get_addr
,
412 .get_kobj
= netdev_get_kobj
,
413 .get_dev_flags
= netdev_get_dev_flags
,
414 .is_running
= netdev_is_running
,
415 .get_operstate
= netdev_get_operstate
,
416 .get_ifindex
= netdev_get_ifindex
,
417 .get_mtu
= netdev_get_mtu
,
421 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
423 * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
424 * the Linux bridge module on any released version of Linux, because there
425 * is only a single bridge hook function and only a single br_port member
426 * in struct net_device.
428 * Declaring and exporting this symbol enforces mutual exclusion. The bridge
429 * module also exports the same symbol, so the module loader will refuse to
430 * load both modules at the same time (e.g. "bridge: exports duplicate symbol
431 * br_should_route_hook (owned by openvswitch_mod)").
433 * The use of "typeof" here avoids the need to track changes in the type of
434 * br_should_route_hook over various kernel versions.
436 typeof(br_should_route_hook
) br_should_route_hook
;
437 EXPORT_SYMBOL(br_should_route_hook
);