datapath: Fix STT protocol field for sampling packet.
[cascardo/ovs.git] / datapath / vport-vxlan.c
1 /*
2  * Copyright (c) 2013 Nicira, Inc.
3  * Copyright (c) 2013 Cisco Systems, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA
18  */
19
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/version.h>
23
24 #include <linux/in.h>
25 #include <linux/ip.h>
26 #include <linux/net.h>
27 #include <linux/rculist.h>
28 #include <linux/udp.h>
29 #include <linux/module.h>
30
31 #include <net/icmp.h>
32 #include <net/ip.h>
33 #include <net/udp.h>
34 #include <net/ip_tunnels.h>
35 #include <net/rtnetlink.h>
36 #include <net/route.h>
37 #include <net/dsfield.h>
38 #include <net/inet_ecn.h>
39 #include <net/net_namespace.h>
40 #include <net/netns/generic.h>
41 #include <net/vxlan.h>
42
43 #include "datapath.h"
44 #include "vport.h"
45 #include "vport-vxlan.h"
46
47 /**
48  * struct vxlan_port - Keeps track of open UDP ports
49  * @vs: vxlan_sock created for the port.
50  * @name: vport name.
51  */
52 struct vxlan_port {
53         struct vxlan_sock *vs;
54         char name[IFNAMSIZ];
55         u32 exts; /* VXLAN_F_* in <net/vxlan.h> */
56 };
57
58 static struct vport_ops ovs_vxlan_vport_ops;
59
60 static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
61 {
62         return vport_priv(vport);
63 }
64
65 static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
66                       struct vxlan_metadata *md)
67 {
68         struct ovs_tunnel_info tun_info;
69         struct vxlan_port *vxlan_port;
70         struct vport *vport = vs->data;
71         struct iphdr *iph;
72         struct ovs_vxlan_opts opts = {
73                 .gbp = md->gbp,
74         };
75         __be64 key;
76         __be16 flags;
77
78         flags = TUNNEL_KEY | (udp_hdr(skb)->check != 0 ? TUNNEL_CSUM : 0);
79         vxlan_port = vxlan_vport(vport);
80         if (vxlan_port->exts & VXLAN_F_GBP && md->gbp)
81                 flags |= TUNNEL_VXLAN_OPT;
82
83         /* Save outer tunnel values */
84         iph = ip_hdr(skb);
85         key = cpu_to_be64(ntohl(md->vni) >> 8);
86         ovs_flow_tun_info_init(&tun_info, iph,
87                                udp_hdr(skb)->source, udp_hdr(skb)->dest,
88                                key, flags, &opts, sizeof(opts));
89
90         ovs_vport_receive(vport, skb, &tun_info);
91 }
92
93 static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
94 {
95         struct vxlan_port *vxlan_port = vxlan_vport(vport);
96         __be16 dst_port = inet_sport(vxlan_port->vs->sock->sk);
97
98         if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
99                 return -EMSGSIZE;
100
101         if (vxlan_port->exts) {
102                 struct nlattr *exts;
103
104                 exts = nla_nest_start(skb, OVS_TUNNEL_ATTR_EXTENSION);
105                 if (!exts)
106                         return -EMSGSIZE;
107
108                 if (vxlan_port->exts & VXLAN_F_GBP &&
109                     nla_put_flag(skb, OVS_VXLAN_EXT_GBP))
110                         return -EMSGSIZE;
111
112                 nla_nest_end(skb, exts);
113         }
114
115         return 0;
116 }
117
118 static void vxlan_tnl_destroy(struct vport *vport)
119 {
120         struct vxlan_port *vxlan_port = vxlan_vport(vport);
121
122         vxlan_sock_release(vxlan_port->vs);
123
124         ovs_vport_deferred_free(vport);
125 }
126
127 static const struct nla_policy exts_policy[OVS_VXLAN_EXT_MAX+1] = {
128         [OVS_VXLAN_EXT_GBP]     = { .type = NLA_FLAG, },
129 };
130
131 static int vxlan_configure_exts(struct vport *vport, struct nlattr *attr)
132 {
133         struct nlattr *exts[OVS_VXLAN_EXT_MAX+1];
134         struct vxlan_port *vxlan_port;
135         int err;
136
137         if (nla_len(attr) < sizeof(struct nlattr))
138                 return -EINVAL;
139
140         err = nla_parse_nested(exts, OVS_VXLAN_EXT_MAX, attr, exts_policy);
141         if (err < 0)
142                 return err;
143
144         vxlan_port = vxlan_vport(vport);
145
146         if (exts[OVS_VXLAN_EXT_GBP])
147                 vxlan_port->exts |= VXLAN_F_GBP;
148
149         return 0;
150 }
151
152 static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
153 {
154         struct net *net = ovs_dp_get_net(parms->dp);
155         struct nlattr *options = parms->options;
156         struct vxlan_port *vxlan_port;
157         struct vxlan_sock *vs;
158         struct vport *vport;
159         struct nlattr *a;
160         u16 dst_port;
161         int err;
162
163         if (!options) {
164                 err = -EINVAL;
165                 goto error;
166         }
167         a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
168         if (a && nla_len(a) == sizeof(u16)) {
169                 dst_port = nla_get_u16(a);
170         } else {
171                 /* Require destination port from userspace. */
172                 err = -EINVAL;
173                 goto error;
174         }
175
176         vport = ovs_vport_alloc(sizeof(struct vxlan_port),
177                                 &ovs_vxlan_vport_ops, parms);
178         if (IS_ERR(vport))
179                 return vport;
180
181         vxlan_port = vxlan_vport(vport);
182         strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
183
184         a = nla_find_nested(options, OVS_TUNNEL_ATTR_EXTENSION);
185         if (a) {
186                 err = vxlan_configure_exts(vport, a);
187                 if (err) {
188                         ovs_vport_free(vport);
189                         goto error;
190                 }
191         }
192
193         vs = vxlan_sock_add(net, htons(dst_port), vxlan_rcv, vport, true,
194                             vxlan_port->exts);
195         if (IS_ERR(vs)) {
196                 ovs_vport_free(vport);
197                 return (void *)vs;
198         }
199         vxlan_port->vs = vs;
200
201         return vport;
202
203 error:
204         return ERR_PTR(err);
205 }
206
207 static int vxlan_ext_gbp(struct sk_buff *skb)
208 {
209         const struct ovs_tunnel_info *tun_info;
210         const struct ovs_vxlan_opts *opts;
211
212         tun_info = OVS_CB(skb)->egress_tun_info;
213         opts = tun_info->options;
214
215         if (tun_info->tunnel.tun_flags & TUNNEL_VXLAN_OPT &&
216             tun_info->options_len >= sizeof(*opts))
217                 return opts->gbp;
218         else
219                 return 0;
220 }
221
222 static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
223 {
224         struct ovs_key_ipv4_tunnel *tun_key;
225         struct net *net = ovs_dp_get_net(vport->dp);
226         struct vxlan_port *vxlan_port = vxlan_vport(vport);
227         __be16 dst_port = inet_sport(vxlan_port->vs->sock->sk);
228         struct vxlan_metadata md = {0};
229         struct rtable *rt;
230         __be16 src_port;
231         __be32 saddr;
232         __be16 df;
233         int err;
234         u32 vxflags;
235
236         if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
237                 err = -EINVAL;
238                 goto error;
239         }
240
241         tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
242
243         /* Route lookup */
244         saddr = tun_key->ipv4_src;
245         rt = find_route(ovs_dp_get_net(vport->dp),
246                         &saddr, tun_key->ipv4_dst,
247                         IPPROTO_UDP, tun_key->ipv4_tos,
248                         skb->mark);
249         if (IS_ERR(rt)) {
250                 err = PTR_ERR(rt);
251                 goto error;
252         }
253
254         df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
255         skb->ignore_df = 1;
256
257         src_port = udp_flow_src_port(net, skb, 0, 0, true);
258         md.vni = htonl(be64_to_cpu(tun_key->tun_id) << 8);
259         md.gbp = vxlan_ext_gbp(skb);
260         vxflags = vxlan_port->exts |
261                       (tun_key->tun_flags & TUNNEL_CSUM ? VXLAN_F_UDP_CSUM : 0);
262
263         err = vxlan_xmit_skb(vxlan_port->vs, rt, skb,
264                              saddr, tun_key->ipv4_dst,
265                              tun_key->ipv4_tos,
266                              tun_key->ipv4_ttl, df,
267                              src_port, dst_port,
268                              &md, false, vxflags);
269         if (err < 0)
270                 ip_rt_put(rt);
271         return err;
272 error:
273         kfree_skb(skb);
274         return err;
275 }
276
277 static int vxlan_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
278                                      struct ovs_tunnel_info *egress_tun_info)
279 {
280         struct net *net = ovs_dp_get_net(vport->dp);
281         struct vxlan_port *vxlan_port = vxlan_vport(vport);
282         __be16 dst_port = inet_sport(vxlan_port->vs->sock->sk);
283         __be16 src_port;
284
285         src_port = udp_flow_src_port(net, skb, 0, 0, true);
286
287         return ovs_tunnel_get_egress_info(egress_tun_info, net,
288                                           OVS_CB(skb)->egress_tun_info,
289                                           IPPROTO_UDP, skb->mark,
290                                           src_port, dst_port);
291 }
292
293 static const char *vxlan_get_name(const struct vport *vport)
294 {
295         struct vxlan_port *vxlan_port = vxlan_vport(vport);
296         return vxlan_port->name;
297 }
298
299 static struct vport_ops ovs_vxlan_vport_ops = {
300         .type                   = OVS_VPORT_TYPE_VXLAN,
301         .create                 = vxlan_tnl_create,
302         .destroy                = vxlan_tnl_destroy,
303         .get_name               = vxlan_get_name,
304         .get_options            = vxlan_get_options,
305         .send                   = vxlan_tnl_send,
306         .get_egress_tun_info    = vxlan_get_egress_tun_info,
307         .owner                  = THIS_MODULE,
308 };
309
310 static int __init ovs_vxlan_tnl_init(void)
311 {
312         return ovs_vport_ops_register(&ovs_vxlan_vport_ops);
313 }
314
315 static void __exit ovs_vxlan_tnl_exit(void)
316 {
317         ovs_vport_ops_unregister(&ovs_vxlan_vport_ops);
318 }
319
320 module_init(ovs_vxlan_tnl_init);
321 module_exit(ovs_vxlan_tnl_exit);
322
323 MODULE_DESCRIPTION("OVS: VXLAN switching port");
324 MODULE_LICENSE("GPL");
325 MODULE_ALIAS("vport-type-4");