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