]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/vport-netdev.c
datapath: Serialize acts with original netlink len
[mirror_ovs.git] / datapath / vport-netdev.c
1 /*
2 * Copyright (c) 2007-2015 Nicira, Inc.
3 *
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.
7 *
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.
12 *
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
16 * 02110-1301, USA
17 */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
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>
29
30 #include <net/ip_tunnels.h>
31 #include <net/rtnetlink.h>
32
33 #include "datapath.h"
34 #include "gso.h"
35 #include "vport.h"
36 #include "vport-internal_dev.h"
37 #include "vport-netdev.h"
38
39 static struct vport_ops ovs_netdev_vport_ops;
40
41 /* Must be called with rcu_read_lock. */
42 void netdev_port_receive(struct sk_buff *skb, struct ip_tunnel_info *tun_info)
43 {
44 struct vport *vport;
45
46 vport = ovs_netdev_get_vport(skb->dev);
47 if (unlikely(!vport))
48 goto error;
49
50 if (unlikely(skb_warn_if_lro(skb)))
51 goto error;
52
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).
55 */
56 skb = skb_share_check(skb, GFP_ATOMIC);
57 if (unlikely(!skb))
58 return;
59
60 skb_push(skb, ETH_HLEN);
61 ovs_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
62 ovs_vport_receive(vport, skb, tun_info);
63 return;
64 error:
65 kfree_skb(skb);
66 }
67
68 #ifndef HAVE_METADATA_DST
69 #define port_receive(skb) netdev_port_receive(skb, NULL)
70 #else
71 #define port_receive(skb) netdev_port_receive(skb, skb_tunnel_info(skb))
72 #endif
73
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)
77 {
78 struct sk_buff *skb = *pskb;
79
80 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
81 return RX_HANDLER_PASS;
82
83 port_receive(skb);
84 return RX_HANDLER_CONSUMED;
85 }
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)
90 {
91 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
92 return skb;
93
94 port_receive(skb);
95 return NULL;
96 }
97 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
98 /*
99 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
100 * different set of devices!)
101 */
102 /* Called with rcu_read_lock and bottom-halves disabled. */
103 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
104 struct sk_buff *skb)
105 {
106 port_receive(skb);
107 return NULL;
108 }
109 #else
110 #error
111 #endif
112
113 static struct net_device *get_dpdev(const struct datapath *dp)
114 {
115 struct vport *local;
116
117 local = ovs_vport_ovsl(dp, OVSP_LOCAL);
118 BUG_ON(!local);
119 return local->dev;
120 }
121
122 struct vport *ovs_netdev_link(struct vport *vport, const char *name)
123 {
124 int err;
125
126 vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), name);
127 if (!vport->dev) {
128 err = -ENODEV;
129 goto error_free_vport;
130 }
131
132 if (vport->dev->flags & IFF_LOOPBACK ||
133 vport->dev->type != ARPHRD_ETHER ||
134 ovs_is_internal_dev(vport->dev)) {
135 err = -EINVAL;
136 goto error_put;
137 }
138
139 rtnl_lock();
140 err = netdev_master_upper_dev_link(vport->dev,
141 get_dpdev(vport->dp));
142 if (err)
143 goto error_unlock;
144
145 err = netdev_rx_handler_register(vport->dev, netdev_frame_hook,
146 vport);
147 if (err)
148 goto error_master_upper_dev_unlink;
149
150 dev_disable_lro(vport->dev);
151 dev_set_promiscuity(vport->dev, 1);
152 vport->dev->priv_flags |= IFF_OVS_DATAPATH;
153 rtnl_unlock();
154
155 return vport;
156
157 error_master_upper_dev_unlink:
158 netdev_upper_dev_unlink(vport->dev, get_dpdev(vport->dp));
159 error_unlock:
160 rtnl_unlock();
161 error_put:
162 dev_put(vport->dev);
163 error_free_vport:
164 ovs_vport_free(vport);
165 return ERR_PTR(err);
166 }
167 EXPORT_SYMBOL_GPL(ovs_netdev_link);
168
169 static struct vport *netdev_create(const struct vport_parms *parms)
170 {
171 struct vport *vport;
172
173 vport = ovs_vport_alloc(0, &ovs_netdev_vport_ops, parms);
174 if (IS_ERR(vport))
175 return vport;
176
177 return ovs_netdev_link(vport, parms->name);
178 }
179
180 static void vport_netdev_free(struct rcu_head *rcu)
181 {
182 struct vport *vport = container_of(rcu, struct vport, rcu);
183
184 if (vport->dev)
185 dev_put(vport->dev);
186 ovs_vport_free(vport);
187 }
188
189 void ovs_netdev_detach_dev(struct vport *vport)
190 {
191 ASSERT_RTNL();
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);
197 }
198 EXPORT_SYMBOL_GPL(ovs_netdev_detach_dev);
199
200 static void netdev_destroy(struct vport *vport)
201 {
202 rtnl_lock();
203 if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
204 ovs_netdev_detach_dev(vport);
205 rtnl_unlock();
206
207 call_rcu(&vport->rcu, vport_netdev_free);
208 }
209
210 void ovs_netdev_tunnel_destroy(struct vport *vport)
211 {
212 rtnl_lock();
213 if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
214 ovs_netdev_detach_dev(vport);
215
216 /* Early release so we can unregister the device */
217 dev_put(vport->dev);
218 rtnl_delete_link(vport->dev);
219 vport->dev = NULL;
220 rtnl_unlock();
221
222 call_rcu(&vport->rcu, vport_netdev_free);
223 }
224 EXPORT_SYMBOL_GPL(ovs_netdev_tunnel_destroy);
225
226 /* Returns null if this device is not attached to a datapath. */
227 struct vport *ovs_netdev_get_vport(struct net_device *dev)
228 {
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))
233 #else
234 if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))
235 #endif
236 #ifdef HAVE_RHEL_OVS_HOOK
237 return (struct vport *)rcu_dereference_rtnl(dev->ax25_ptr);
238 #else
239 #ifdef HAVE_NET_DEVICE_EXTENDED
240 return (struct vport *)
241 rcu_dereference_rtnl(netdev_extended(dev)->rx_handler_data);
242 #else
243 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
244 #endif
245 #endif
246 else
247 return NULL;
248 #else
249 return (struct vport *)rcu_dereference_rtnl(dev->br_port);
250 #endif
251 }
252
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,
258 };
259
260 int __init ovs_netdev_init(void)
261 {
262 return ovs_vport_ops_register(&ovs_netdev_vport_ops);
263 }
264
265 void ovs_netdev_exit(void)
266 {
267 ovs_vport_ops_unregister(&ovs_netdev_vport_ops);
268 }
269
270 #if !defined HAVE_NETDEV_RX_HANDLER_REGISTER && \
271 !defined HAVE_RHEL_OVS_HOOK
272 /*
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
277 * openvswitch)").
278 *
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.
282 *
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.
285 */
286 typeof(br_should_route_hook) br_should_route_hook;
287 EXPORT_SYMBOL(br_should_route_hook);
288 #endif