2 * Copyright (c) 2007-2015 Nicira, Inc.
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>
28 #include <linux/openvswitch.h>
30 #include <net/ip_tunnels.h>
31 #include <net/rtnetlink.h>
36 #include "vport-internal_dev.h"
37 #include "vport-netdev.h"
39 static struct vport_ops ovs_netdev_vport_ops
;
41 /* Must be called with rcu_read_lock. */
42 void netdev_port_receive(struct sk_buff
*skb
, struct ip_tunnel_info
*tun_info
)
46 vport
= ovs_netdev_get_vport(skb
->dev
);
50 if (unlikely(skb_warn_if_lro(skb
)))
53 /* Make our own copy of the packet. Otherwise we will mangle the
54 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
56 skb
= skb_share_check(skb
, GFP_ATOMIC
);
60 skb_push(skb
, ETH_HLEN
);
61 ovs_skb_postpush_rcsum(skb
, skb
->data
, ETH_HLEN
);
62 ovs_vport_receive(vport
, skb
, tun_info
);
68 #ifndef HAVE_METADATA_DST
69 #define port_receive(skb) netdev_port_receive(skb, NULL)
71 #define port_receive(skb) netdev_port_receive(skb, skb_tunnel_info(skb))
74 #if defined HAVE_RX_HANDLER_PSKB /* 2.6.39 and above or backports */
75 /* Called with rcu_read_lock and bottom-halves disabled. */
76 static rx_handler_result_t
netdev_frame_hook(struct sk_buff
**pskb
)
78 struct sk_buff
*skb
= *pskb
;
80 if (unlikely(skb
->pkt_type
== PACKET_LOOPBACK
))
81 return RX_HANDLER_PASS
;
84 return RX_HANDLER_CONSUMED
;
86 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36) || \
87 defined HAVE_RHEL_OVS_HOOK
88 /* Called with rcu_read_lock and bottom-halves disabled. */
89 static struct sk_buff
*netdev_frame_hook(struct sk_buff
*skb
)
91 if (unlikely(skb
->pkt_type
== PACKET_LOOPBACK
))
97 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
99 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
100 * different set of devices!)
102 /* Called with rcu_read_lock and bottom-halves disabled. */
103 static struct sk_buff
*netdev_frame_hook(struct net_bridge_port
*p
,
113 static struct net_device
*get_dpdev(const struct datapath
*dp
)
117 local
= ovs_vport_ovsl(dp
, OVSP_LOCAL
);
122 struct vport
*ovs_netdev_link(struct vport
*vport
, const char *name
)
126 vport
->dev
= dev_get_by_name(ovs_dp_get_net(vport
->dp
), name
);
129 goto error_free_vport
;
132 if (vport
->dev
->flags
& IFF_LOOPBACK
||
133 vport
->dev
->type
!= ARPHRD_ETHER
||
134 ovs_is_internal_dev(vport
->dev
)) {
140 err
= netdev_master_upper_dev_link(vport
->dev
,
141 get_dpdev(vport
->dp
));
145 err
= netdev_rx_handler_register(vport
->dev
, netdev_frame_hook
,
148 goto error_master_upper_dev_unlink
;
150 dev_disable_lro(vport
->dev
);
151 dev_set_promiscuity(vport
->dev
, 1);
152 vport
->dev
->priv_flags
|= IFF_OVS_DATAPATH
;
157 error_master_upper_dev_unlink
:
158 netdev_upper_dev_unlink(vport
->dev
, get_dpdev(vport
->dp
));
164 ovs_vport_free(vport
);
167 EXPORT_SYMBOL_GPL(ovs_netdev_link
);
169 static struct vport
*netdev_create(const struct vport_parms
*parms
)
173 vport
= ovs_vport_alloc(0, &ovs_netdev_vport_ops
, parms
);
177 return ovs_netdev_link(vport
, parms
->name
);
180 static void vport_netdev_free(struct rcu_head
*rcu
)
182 struct vport
*vport
= container_of(rcu
, struct vport
, rcu
);
186 ovs_vport_free(vport
);
189 void ovs_netdev_detach_dev(struct vport
*vport
)
192 vport
->dev
->priv_flags
&= ~IFF_OVS_DATAPATH
;
193 netdev_rx_handler_unregister(vport
->dev
);
194 netdev_upper_dev_unlink(vport
->dev
,
195 netdev_master_upper_dev_get(vport
->dev
));
196 dev_set_promiscuity(vport
->dev
, -1);
198 EXPORT_SYMBOL_GPL(ovs_netdev_detach_dev
);
200 static void netdev_destroy(struct vport
*vport
)
203 if (vport
->dev
->priv_flags
& IFF_OVS_DATAPATH
)
204 ovs_netdev_detach_dev(vport
);
207 call_rcu(&vport
->rcu
, vport_netdev_free
);
210 void ovs_netdev_tunnel_destroy(struct vport
*vport
)
213 if (vport
->dev
->priv_flags
& IFF_OVS_DATAPATH
)
214 ovs_netdev_detach_dev(vport
);
216 /* Early release so we can unregister the device */
218 rtnl_delete_link(vport
->dev
);
222 call_rcu(&vport
->rcu
, vport_netdev_free
);
224 EXPORT_SYMBOL_GPL(ovs_netdev_tunnel_destroy
);
226 /* Returns null if this device is not attached to a datapath. */
227 struct vport
*ovs_netdev_get_vport(struct net_device
*dev
)
229 #if defined HAVE_NETDEV_RX_HANDLER_REGISTER || \
230 defined HAVE_RHEL_OVS_HOOK
231 #ifdef HAVE_OVS_DATAPATH
232 if (likely(dev
->priv_flags
& IFF_OVS_DATAPATH
))
234 if (likely(rcu_access_pointer(dev
->rx_handler
) == netdev_frame_hook
))
236 #ifdef HAVE_RHEL_OVS_HOOK
237 return (struct vport
*)rcu_dereference_rtnl(dev
->ax25_ptr
);
239 #ifdef HAVE_NET_DEVICE_EXTENDED
240 return (struct vport
*)
241 rcu_dereference_rtnl(netdev_extended(dev
)->rx_handler_data
);
243 return (struct vport
*)rcu_dereference_rtnl(dev
->rx_handler_data
);
249 return (struct vport
*)rcu_dereference_rtnl(dev
->br_port
);
253 static struct vport_ops ovs_netdev_vport_ops
= {
254 .type
= OVS_VPORT_TYPE_NETDEV
,
255 .create
= netdev_create
,
256 .destroy
= netdev_destroy
,
257 .send
= dev_queue_xmit
,
260 int __init
ovs_netdev_init(void)
262 return ovs_vport_ops_register(&ovs_netdev_vport_ops
);
265 void ovs_netdev_exit(void)
267 ovs_vport_ops_unregister(&ovs_netdev_vport_ops
);
270 #if !defined HAVE_NETDEV_RX_HANDLER_REGISTER && \
271 !defined HAVE_RHEL_OVS_HOOK
273 * Enforces, mutual exclusion with the Linux bridge module, by declaring and
274 * exporting br_should_route_hook. Because the bridge module also exports the
275 * same symbol, the module loader will refuse to load both modules at the same
276 * time (e.g. "bridge: exports duplicate symbol br_should_route_hook (owned by
279 * Before Linux 2.6.36, Open vSwitch cannot safely coexist with the Linux
280 * bridge module, so openvswitch uses this macro in those versions. In
281 * Linux 2.6.36 and later, Open vSwitch can coexist with the bridge module.
283 * The use of "typeof" here avoids the need to track changes in the type of
284 * br_should_route_hook over various kernel versions.
286 typeof(br_should_route_hook
) br_should_route_hook
;
287 EXPORT_SYMBOL(br_should_route_hook
);