netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / datapath / vport-internal_dev.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 #include <linux/hardirq.h>
20 #include <linux/if_vlan.h>
21 #include <linux/kernel.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/skbuff.h>
26 #include <linux/percpu.h>
27 #include <linux/u64_stats_sync.h>
28 #include <linux/netdev_features.h>
29
30 #include <net/dst.h>
31 #include <net/xfrm.h>
32 #include <net/rtnetlink.h>
33
34 #include "datapath.h"
35 #include "vport-internal_dev.h"
36 #include "vport-netdev.h"
37
38 struct internal_dev {
39         struct vport *vport;
40 };
41
42 static struct vport_ops ovs_internal_vport_ops;
43
44 static struct internal_dev *internal_dev_priv(struct net_device *netdev)
45 {
46         return netdev_priv(netdev);
47 }
48
49 /* Called with rcu_read_lock_bh. */
50 static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
51 {
52         int len, err;
53
54         len = skb->len;
55         rcu_read_lock();
56         err = ovs_vport_receive(internal_dev_priv(netdev)->vport, skb, NULL);
57         rcu_read_unlock();
58
59         if (likely(!err)) {
60 #ifdef HAVE_DEV_TSTATS
61                 struct pcpu_sw_netstats *tstats;
62
63                 tstats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)netdev->tstats);
64
65                 u64_stats_update_begin(&tstats->syncp);
66                 tstats->tx_bytes += len;
67                 tstats->tx_packets++;
68                 u64_stats_update_end(&tstats->syncp);
69 #endif
70         } else {
71                 netdev->stats.tx_errors++;
72         }
73         return 0;
74 }
75
76 static int internal_dev_open(struct net_device *netdev)
77 {
78         netif_start_queue(netdev);
79         return 0;
80 }
81
82 static int internal_dev_stop(struct net_device *netdev)
83 {
84         netif_stop_queue(netdev);
85         return 0;
86 }
87
88 static void internal_dev_getinfo(struct net_device *netdev,
89                                  struct ethtool_drvinfo *info)
90 {
91         strlcpy(info->driver, "openvswitch", sizeof(info->driver));
92 }
93
94 static const struct ethtool_ops internal_dev_ethtool_ops = {
95         .get_drvinfo    = internal_dev_getinfo,
96         .get_link       = ethtool_op_get_link,
97 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
98         .get_sg         = ethtool_op_get_sg,
99         .set_sg         = ethtool_op_set_sg,
100         .get_tx_csum    = ethtool_op_get_tx_csum,
101         .set_tx_csum    = ethtool_op_set_tx_hw_csum,
102         .get_tso        = ethtool_op_get_tso,
103         .set_tso        = ethtool_op_set_tso,
104 #endif
105 };
106
107 static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
108 {
109         if (new_mtu < 68)
110                 return -EINVAL;
111
112         netdev->mtu = new_mtu;
113         return 0;
114 }
115
116 static void internal_dev_destructor(struct net_device *dev)
117 {
118         struct vport *vport = ovs_internal_dev_get_vport(dev);
119
120         ovs_vport_free(vport);
121         free_netdev(dev);
122 }
123
124 #ifdef HAVE_DEV_TSTATS
125 static int internal_dev_init(struct net_device *dev)
126 {
127         dev->tstats = (typeof(dev->tstats)) netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
128         if (!dev->tstats)
129                 return -ENOMEM;
130         return 0;
131 }
132
133 static void internal_dev_uninit(struct net_device *dev)
134 {
135         free_percpu(dev->tstats);
136 }
137 #endif
138
139 static const struct net_device_ops internal_dev_netdev_ops = {
140 #ifdef HAVE_DEV_TSTATS
141         .ndo_init = internal_dev_init,
142         .ndo_uninit = internal_dev_uninit,
143         .ndo_get_stats64 = ip_tunnel_get_stats64,
144 #endif
145         .ndo_open = internal_dev_open,
146         .ndo_stop = internal_dev_stop,
147         .ndo_start_xmit = internal_dev_xmit,
148         .ndo_set_mac_address = eth_mac_addr,
149         .ndo_change_mtu = internal_dev_change_mtu,
150 };
151
152 static struct rtnl_link_ops internal_dev_link_ops __read_mostly = {
153         .kind = "openvswitch",
154 };
155
156 static void do_setup(struct net_device *netdev)
157 {
158         ether_setup(netdev);
159
160         netdev->netdev_ops = &internal_dev_netdev_ops;
161
162         netdev->priv_flags &= ~IFF_TX_SKB_SHARING;
163         netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_OPENVSWITCH;
164         netdev->destructor = internal_dev_destructor;
165         netdev->ethtool_ops = &internal_dev_ethtool_ops;
166         netdev->rtnl_link_ops = &internal_dev_link_ops;
167         netdev->tx_queue_len = 0;
168
169         netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
170                            NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
171                            NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL;
172
173         netdev->vlan_features = netdev->features;
174         netdev->features |= NETIF_F_HW_VLAN_CTAG_TX;
175
176 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
177         netdev->hw_enc_features = netdev->features;
178 #endif
179 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
180         netdev->hw_features = netdev->features & ~NETIF_F_LLTX;
181 #endif
182         eth_hw_addr_random(netdev);
183 }
184
185 static struct vport *internal_dev_create(const struct vport_parms *parms)
186 {
187         struct vport *vport;
188         struct internal_dev *internal_dev;
189         int err;
190
191         vport = ovs_vport_alloc(0, &ovs_internal_vport_ops, parms);
192         if (IS_ERR(vport)) {
193                 err = PTR_ERR(vport);
194                 goto error;
195         }
196
197         vport->dev = alloc_netdev(sizeof(struct internal_dev),
198                                   parms->name, NET_NAME_UNKNOWN, do_setup);
199         if (!vport->dev) {
200                 err = -ENOMEM;
201                 goto error_free_vport;
202         }
203
204         dev_net_set(vport->dev, ovs_dp_get_net(vport->dp));
205         internal_dev = internal_dev_priv(vport->dev);
206         internal_dev->vport = vport;
207
208         /* Restrict bridge port to current netns. */
209         if (vport->port_no == OVSP_LOCAL)
210                 vport->dev->features |= NETIF_F_NETNS_LOCAL;
211
212         rtnl_lock();
213         err = register_netdevice(vport->dev);
214         if (err)
215                 goto error_free_netdev;
216
217         dev_set_promiscuity(vport->dev, 1);
218         rtnl_unlock();
219         netif_start_queue(vport->dev);
220
221         return vport;
222
223 error_free_netdev:
224         rtnl_unlock();
225         free_netdev(vport->dev);
226 error_free_vport:
227         ovs_vport_free(vport);
228 error:
229         return ERR_PTR(err);
230 }
231
232 static void internal_dev_destroy(struct vport *vport)
233 {
234         netif_stop_queue(vport->dev);
235         rtnl_lock();
236         dev_set_promiscuity(vport->dev, -1);
237
238         /* unregister_netdevice() waits for an RCU grace period. */
239         unregister_netdevice(vport->dev);
240
241         rtnl_unlock();
242 }
243
244 static netdev_tx_t internal_dev_recv(struct sk_buff *skb)
245 {
246         struct net_device *netdev = skb->dev;
247 #ifdef HAVE_DEV_TSTATS
248         struct pcpu_sw_netstats *stats;
249 #endif
250
251         if (unlikely(!(netdev->flags & IFF_UP))) {
252                 kfree_skb(skb);
253                 netdev->stats.rx_dropped++;
254                 return NETDEV_TX_OK;
255         }
256
257 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
258         if (skb_vlan_tag_present(skb)) {
259                 if (unlikely(!vlan_insert_tag_set_proto(skb,
260                                                         skb->vlan_proto,
261                                                         skb_vlan_tag_get(skb))))
262                         return NETDEV_TX_OK;
263
264                 if (skb->ip_summed == CHECKSUM_COMPLETE)
265                         skb->csum = csum_add(skb->csum,
266                                              csum_partial(skb->data + (2 * ETH_ALEN),
267                                                           VLAN_HLEN, 0));
268
269                 vlan_set_tci(skb, 0);
270         }
271 #endif
272
273         skb_dst_drop(skb);
274         nf_reset(skb);
275         secpath_reset(skb);
276
277         skb->pkt_type = PACKET_HOST;
278         skb->protocol = eth_type_trans(skb, netdev);
279         skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
280
281 #ifdef HAVE_DEV_TSTATS
282         stats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)netdev->tstats);
283         u64_stats_update_begin(&stats->syncp);
284         stats->rx_packets++;
285         stats->rx_bytes += skb->len;
286         u64_stats_update_end(&stats->syncp);
287 #endif
288
289         netif_rx(skb);
290         return NETDEV_TX_OK;
291 }
292
293 static struct vport_ops ovs_internal_vport_ops = {
294         .type           = OVS_VPORT_TYPE_INTERNAL,
295         .create         = internal_dev_create,
296         .destroy        = internal_dev_destroy,
297         .send           = internal_dev_recv,
298 };
299
300 int ovs_is_internal_dev(const struct net_device *netdev)
301 {
302         return netdev->netdev_ops == &internal_dev_netdev_ops;
303 }
304
305 struct vport *ovs_internal_dev_get_vport(struct net_device *netdev)
306 {
307         if (!ovs_is_internal_dev(netdev))
308                 return NULL;
309
310         return internal_dev_priv(netdev)->vport;
311 }
312
313 int ovs_internal_dev_rtnl_link_register(void)
314 {
315         int err;
316
317         err = rtnl_link_register(&internal_dev_link_ops);
318         if (err < 0)
319                 return err;
320
321         err = ovs_vport_ops_register(&ovs_internal_vport_ops);
322         if (err < 0)
323                 rtnl_link_unregister(&internal_dev_link_ops);
324
325         return err;
326 }
327
328 void ovs_internal_dev_rtnl_link_unregister(void)
329 {
330         ovs_vport_ops_unregister(&ovs_internal_vport_ops);
331         rtnl_link_unregister(&internal_dev_link_ops);
332 }