netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / datapath / vport-stt.c
1 /*
2  * Copyright (c) 2015 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/in.h>
13 #include <linux/ip.h>
14 #include <linux/net.h>
15 #include <linux/rculist.h>
16 #include <linux/udp.h>
17 #include <linux/if_vlan.h>
18 #include <linux/module.h>
19
20 #include <net/stt.h>
21 #include <net/icmp.h>
22 #include <net/ip.h>
23 #include <net/route.h>
24 #include <net/udp.h>
25 #include <net/xfrm.h>
26 #include <net/stt.h>
27
28 #include "datapath.h"
29 #include "vport.h"
30 #include "vport-netdev.h"
31
32 #ifdef OVS_STT
33 static struct vport_ops ovs_stt_vport_ops;
34 /**
35  * struct stt_port - Keeps track of open UDP ports
36  * @dst_port: destination port.
37  */
38 struct stt_port {
39         u16 port_no;
40 };
41
42 static inline struct stt_port *stt_vport(const struct vport *vport)
43 {
44         return vport_priv(vport);
45 }
46
47 static int stt_get_options(const struct vport *vport,
48                               struct sk_buff *skb)
49 {
50         struct stt_port *stt_port = stt_vport(vport);
51
52         if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, stt_port->port_no))
53                 return -EMSGSIZE;
54         return 0;
55 }
56
57 static int stt_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
58                                       struct dp_upcall_info *upcall)
59 {
60         struct stt_port *stt_port = stt_vport(vport);
61         struct net *net = ovs_dp_get_net(vport->dp);
62         __be16 dport = htons(stt_port->port_no);
63         __be16 sport = udp_flow_src_port(net, skb, 1, USHRT_MAX, true);
64
65         return ovs_tunnel_get_egress_info(upcall, ovs_dp_get_net(vport->dp),
66                                           skb, IPPROTO_UDP, sport, dport);
67 }
68
69 static struct vport *stt_tnl_create(const struct vport_parms *parms)
70 {
71         struct net *net = ovs_dp_get_net(parms->dp);
72         struct nlattr *options = parms->options;
73         struct stt_port *stt_port;
74         struct net_device *dev;
75         struct vport *vport;
76         struct nlattr *a;
77         u16 dst_port;
78         int err;
79
80         if (!options) {
81                 err = -EINVAL;
82                 goto error;
83         }
84
85         a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
86         if (a && nla_len(a) == sizeof(u16)) {
87                 dst_port = nla_get_u16(a);
88         } else {
89                 /* Require destination port from userspace. */
90                 err = -EINVAL;
91                 goto error;
92         }
93
94         vport = ovs_vport_alloc(sizeof(struct stt_port),
95                                 &ovs_stt_vport_ops, parms);
96         if (IS_ERR(vport))
97                 return vport;
98
99         stt_port = stt_vport(vport);
100         stt_port->port_no = dst_port;
101
102         rtnl_lock();
103         dev = stt_dev_create_fb(net, parms->name, NET_NAME_USER, dst_port);
104         if (IS_ERR(dev)) {
105                 rtnl_unlock();
106                 ovs_vport_free(vport);
107                 return ERR_CAST(dev);
108         }
109
110         dev_change_flags(dev, dev->flags | IFF_UP);
111         rtnl_unlock();
112         return vport;
113 error:
114         return ERR_PTR(err);
115 }
116
117 static struct vport *stt_create(const struct vport_parms *parms)
118 {
119         struct vport *vport;
120
121         vport = stt_tnl_create(parms);
122         if (IS_ERR(vport))
123                 return vport;
124
125         return ovs_netdev_link(vport, parms->name);
126 }
127
128 static struct vport_ops ovs_stt_vport_ops = {
129         .type           = OVS_VPORT_TYPE_STT,
130         .create         = stt_create,
131         .destroy        = ovs_netdev_tunnel_destroy,
132         .get_options    = stt_get_options,
133         .send           = ovs_stt_xmit,
134         .get_egress_tun_info    = stt_get_egress_tun_info,
135 };
136
137 static int __init ovs_stt_tnl_init(void)
138 {
139         return ovs_vport_ops_register(&ovs_stt_vport_ops);
140 }
141
142 static void __exit ovs_stt_tnl_exit(void)
143 {
144         ovs_vport_ops_unregister(&ovs_stt_vport_ops);
145 }
146
147 module_init(ovs_stt_tnl_init);
148 module_exit(ovs_stt_tnl_exit);
149
150 MODULE_DESCRIPTION("OVS: STT switching port");
151 MODULE_LICENSE("GPL");
152 MODULE_ALIAS("vport-type-106");
153 #endif