9f8874adf59872ff939893ecdbac554401d84560
[sliver-openvswitch.git] / datapath / vport-vxlan.c
1 /*
2  * Copyright (c) 2011 Nicira, Inc.
3  * Copyright (c) 2012 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 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
24
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/net.h>
28 #include <linux/rculist.h>
29 #include <linux/udp.h>
30
31 #include <net/icmp.h>
32 #include <net/ip.h>
33 #include <net/udp.h>
34
35 #include "datapath.h"
36 #include "tunnel.h"
37 #include "vport.h"
38
39 #define VXLAN_FLAGS 0x08000000  /* struct vxlanhdr.vx_flags required value. */
40
41 /**
42  * struct vxlanhdr - VXLAN header
43  * @vx_flags: Must have the exact value %VXLAN_FLAGS.
44  * @vx_vni: VXLAN Network Identifier (VNI) in top 24 bits, low 8 bits zeroed.
45  */
46 struct vxlanhdr {
47         __be32 vx_flags;
48         __be32 vx_vni;
49 };
50
51 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
52
53 static inline int vxlan_hdr_len(const struct ovs_key_ipv4_tunnel *tun_key)
54 {
55         return VXLAN_HLEN;
56 }
57
58 /**
59  * struct vxlan_port - Keeps track of open UDP ports
60  * @list: list element.
61  * @vport: vport for the tunnel.
62  * @socket: The socket created for this port number.
63  */
64 struct vxlan_port {
65         struct list_head list;
66         struct vport *vport;
67         struct socket *vxlan_rcv_socket;
68         struct rcu_head rcu;
69 };
70
71 static LIST_HEAD(vxlan_ports);
72
73 static struct vxlan_port *vxlan_find_port(struct net *net, __be16 port)
74 {
75         struct vxlan_port *vxlan_port;
76
77         list_for_each_entry_rcu(vxlan_port, &vxlan_ports, list) {
78                 struct tnl_vport *tnl_vport = tnl_vport_priv(vxlan_port->vport);
79
80                 if (tnl_vport->dst_port == port &&
81                         net_eq(sock_net(vxlan_port->vxlan_rcv_socket->sk), net))
82                         return vxlan_port;
83         }
84
85         return NULL;
86 }
87
88 static inline struct vxlanhdr *vxlan_hdr(const struct sk_buff *skb)
89 {
90         return (struct vxlanhdr *)(udp_hdr(skb) + 1);
91 }
92
93 static void vxlan_build_header(const struct vport *vport,
94                                struct sk_buff *skb,
95                                int tunnel_hlen)
96 {
97         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
98         struct udphdr *udph = udp_hdr(skb);
99         struct vxlanhdr *vxh = (struct vxlanhdr *)(udph + 1);
100         const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
101
102         udph->dest = tnl_vport->dst_port;
103         udph->source = htons(ovs_tnl_get_src_port(skb));
104         udph->check = 0;
105         udph->len = htons(skb->len - skb_transport_offset(skb));
106
107         vxh->vx_flags = htonl(VXLAN_FLAGS);
108         vxh->vx_vni = htonl(be64_to_cpu(tun_key->tun_id) << 8);
109
110         /*
111          * Allow our local IP stack to fragment the outer packet even if the
112          * DF bit is set as a last resort.  We also need to force selection of
113          * an IP ID here because Linux will otherwise leave it at 0 if the
114          * packet originally had DF set.
115          */
116         skb->local_df = 1;
117         __ip_select_ident(ip_hdr(skb), skb_dst(skb), 0);
118 }
119
120 /* Called with rcu_read_lock and BH disabled. */
121 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
122 {
123         struct vxlan_port *vxlan_vport;
124         struct vxlanhdr *vxh;
125         struct iphdr *iph;
126         struct ovs_key_ipv4_tunnel tun_key;
127         __be64 key;
128
129         vxlan_vport = vxlan_find_port(dev_net(skb->dev), udp_hdr(skb)->dest);
130         if (unlikely(!vxlan_vport))
131                 goto error;
132
133         if (unlikely(!pskb_may_pull(skb, VXLAN_HLEN + ETH_HLEN)))
134                 goto error;
135
136         vxh = vxlan_hdr(skb);
137         if (unlikely(vxh->vx_flags != htonl(VXLAN_FLAGS) ||
138                      vxh->vx_vni & htonl(0xff)))
139                 goto error;
140
141         __skb_pull(skb, VXLAN_HLEN);
142         skb_postpull_rcsum(skb, skb_transport_header(skb), VXLAN_HLEN + ETH_HLEN);
143
144         key = cpu_to_be64(ntohl(vxh->vx_vni) >> 8);
145
146         /* Save outer tunnel values */
147         iph = ip_hdr(skb);
148         tnl_tun_key_init(&tun_key, iph, key, OVS_TNL_F_KEY);
149         OVS_CB(skb)->tun_key = &tun_key;
150
151         ovs_tnl_rcv(vxlan_vport->vport, skb);
152         goto out;
153
154 error:
155         kfree_skb(skb);
156 out:
157         return 0;
158 }
159
160 /* Random value.  Irrelevant as long as it's not 0 since we set the handler. */
161 #define UDP_ENCAP_VXLAN 1
162 static int vxlan_socket_init(struct vxlan_port *vxlan_port, struct net *net)
163 {
164         int err;
165         struct sockaddr_in sin;
166         struct tnl_vport *tnl_vport = tnl_vport_priv(vxlan_port->vport);
167
168         err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
169                                &vxlan_port->vxlan_rcv_socket);
170         if (err)
171                 goto error;
172
173         /* release net ref. */
174         sk_change_net(vxlan_port->vxlan_rcv_socket->sk, net);
175
176         sin.sin_family = AF_INET;
177         sin.sin_addr.s_addr = htonl(INADDR_ANY);
178         sin.sin_port = tnl_vport->dst_port;
179
180         err = kernel_bind(vxlan_port->vxlan_rcv_socket, (struct sockaddr *)&sin,
181                           sizeof(struct sockaddr_in));
182         if (err)
183                 goto error_sock;
184
185         udp_sk(vxlan_port->vxlan_rcv_socket->sk)->encap_type = UDP_ENCAP_VXLAN;
186         udp_sk(vxlan_port->vxlan_rcv_socket->sk)->encap_rcv = vxlan_rcv;
187
188         udp_encap_enable();
189
190         return 0;
191
192 error_sock:
193         sk_release_kernel(vxlan_port->vxlan_rcv_socket->sk);
194 error:
195         pr_warn("cannot register vxlan protocol handler\n");
196         return err;
197 }
198
199 static void free_port_rcu(struct rcu_head *rcu)
200 {
201         struct vxlan_port *vxlan_port = container_of(rcu,
202                         struct vxlan_port, rcu);
203
204         kfree(vxlan_port);
205 }
206
207 static void vxlan_tunnel_release(struct vxlan_port *vxlan_port)
208 {
209         if (!vxlan_port)
210                 return;
211
212         list_del_rcu(&vxlan_port->list);
213         /* Release socket */
214         sk_release_kernel(vxlan_port->vxlan_rcv_socket->sk);
215         call_rcu(&vxlan_port->rcu, free_port_rcu);
216 }
217
218 static int vxlan_tunnel_setup(struct net *net, struct vport *vport,
219                               struct nlattr *options)
220 {
221         struct vxlan_port *vxlan_port;
222         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
223         struct nlattr *a;
224         int err;
225         u16 dst_port;
226
227         if (!options) {
228                 err = -EINVAL;
229                 goto out;
230         }
231
232         a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
233         if (a && nla_len(a) == sizeof(u16)) {
234                 dst_port = nla_get_u16(a);
235         } else {
236                 /* Require destination port from userspace. */
237                 err = -EINVAL;
238                 goto out;
239         }
240
241         /* Verify if we already have a socket created for this port */
242         vxlan_port = vxlan_find_port(net, htons(dst_port));
243         if (vxlan_port) {
244                 err = -EEXIST;
245                 goto out;
246         }
247
248         /* Add a new socket for this port */
249         vxlan_port = kzalloc(sizeof(struct vxlan_port), GFP_KERNEL);
250         if (!vxlan_port) {
251                 err = -ENOMEM;
252                 goto out;
253         }
254
255         tnl_vport->dst_port = htons(dst_port);
256         vxlan_port->vport = vport;
257         list_add_tail_rcu(&vxlan_port->list, &vxlan_ports);
258
259         err = vxlan_socket_init(vxlan_port, net);
260         if (err)
261                 goto error;
262
263         return 0;
264
265 error:
266         list_del_rcu(&vxlan_port->list);
267         kfree(vxlan_port);
268 out:
269         return err;
270 }
271
272 static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
273 {
274         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
275
276         if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(tnl_vport->dst_port)))
277                 return -EMSGSIZE;
278         return 0;
279 }
280
281 static const struct tnl_ops ovs_vxlan_tnl_ops = {
282         .ipproto        = IPPROTO_UDP,
283         .hdr_len        = vxlan_hdr_len,
284         .build_header   = vxlan_build_header,
285 };
286
287 static void vxlan_tnl_destroy(struct vport *vport)
288 {
289         struct vxlan_port *vxlan_port;
290         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
291
292         vxlan_port = vxlan_find_port(ovs_dp_get_net(vport->dp),
293                                          tnl_vport->dst_port);
294
295         vxlan_tunnel_release(vxlan_port);
296         ovs_tnl_destroy(vport);
297 }
298
299 static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
300 {
301         int err;
302         struct vport *vport;
303
304         vport = ovs_tnl_create(parms, &ovs_vxlan_vport_ops, &ovs_vxlan_tnl_ops);
305         if (IS_ERR(vport))
306                 return vport;
307
308         err = vxlan_tunnel_setup(ovs_dp_get_net(parms->dp), vport,
309                                  parms->options);
310         if (err) {
311                 ovs_tnl_destroy(vport);
312                 return ERR_PTR(err);
313         }
314
315         return vport;
316 }
317
318 const struct vport_ops ovs_vxlan_vport_ops = {
319         .type           = OVS_VPORT_TYPE_VXLAN,
320         .flags          = VPORT_F_TUN_ID,
321         .create         = vxlan_tnl_create,
322         .destroy        = vxlan_tnl_destroy,
323         .get_name       = ovs_tnl_get_name,
324         .get_options    = vxlan_get_options,
325         .send           = ovs_tnl_send,
326 };
327 #else
328 #warning VXLAN tunneling will not be available on kernels before 2.6.26
329 #endif /* Linux kernel < 2.6.26 */