netdev-dpdk: fix mbuf leaks
[cascardo/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         /* We can be invoked by both explicit vport deletion and
217          * underlying netdev deregistration; delete the link only
218          * if it's not already shutting down.
219          */
220
221         if (vport->dev->reg_state == NETREG_REGISTERED)
222                 rtnl_delete_link(vport->dev);
223
224         dev_put(vport->dev);
225         vport->dev = NULL;
226         rtnl_unlock();
227
228         call_rcu(&vport->rcu, vport_netdev_free);
229 }
230 EXPORT_SYMBOL_GPL(ovs_netdev_tunnel_destroy);
231
232 /* Returns null if this device is not attached to a datapath. */
233 struct vport *ovs_netdev_get_vport(struct net_device *dev)
234 {
235 #if defined HAVE_NETDEV_RX_HANDLER_REGISTER || \
236     defined HAVE_RHEL_OVS_HOOK
237 #ifdef HAVE_OVS_DATAPATH
238         if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
239 #else
240         if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))
241 #endif
242 #ifdef HAVE_RHEL_OVS_HOOK
243                 return (struct vport *)rcu_dereference_rtnl(dev->ax25_ptr);
244 #else
245 #ifdef HAVE_NET_DEVICE_EXTENDED
246                 return (struct vport *)
247                         rcu_dereference_rtnl(netdev_extended(dev)->rx_handler_data);
248 #else
249                 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
250 #endif
251 #endif
252         else
253                 return NULL;
254 #else
255         return (struct vport *)rcu_dereference_rtnl(dev->br_port);
256 #endif
257 }
258
259 static struct vport_ops ovs_netdev_vport_ops = {
260         .type           = OVS_VPORT_TYPE_NETDEV,
261         .create         = netdev_create,
262         .destroy        = netdev_destroy,
263         .send           = dev_queue_xmit,
264 };
265
266 int __init ovs_netdev_init(void)
267 {
268         return ovs_vport_ops_register(&ovs_netdev_vport_ops);
269 }
270
271 void ovs_netdev_exit(void)
272 {
273         ovs_vport_ops_unregister(&ovs_netdev_vport_ops);
274 }
275
276 #if !defined HAVE_NETDEV_RX_HANDLER_REGISTER && \
277     !defined HAVE_RHEL_OVS_HOOK
278 /*
279  * Enforces, mutual exclusion with the Linux bridge module, by declaring and
280  * exporting br_should_route_hook.  Because the bridge module also exports the
281  * same symbol, the module loader will refuse to load both modules at the same
282  * time (e.g. "bridge: exports duplicate symbol br_should_route_hook (owned by
283  * openvswitch)").
284  *
285  * Before Linux 2.6.36, Open vSwitch cannot safely coexist with the Linux
286  * bridge module, so openvswitch uses this macro in those versions.  In
287  * Linux 2.6.36 and later, Open vSwitch can coexist with the bridge module.
288  *
289  * The use of "typeof" here avoids the need to track changes in the type of
290  * br_should_route_hook over various kernel versions.
291  */
292 typeof(br_should_route_hook) br_should_route_hook;
293 EXPORT_SYMBOL(br_should_route_hook);
294 #endif