]>
Commit | Line | Data |
---|---|---|
79f827fa | 1 | /* |
a109c9fb PS |
2 | * Copyright (c) 2013 Nicira, Inc. |
3 | * Copyright (c) 2013 Cisco Systems, Inc. | |
79f827fa KM |
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> | |
79f827fa KM |
23 | |
24 | #include <linux/in.h> | |
25 | #include <linux/ip.h> | |
79f827fa | 26 | #include <linux/net.h> |
85c9de19 | 27 | #include <linux/rculist.h> |
79f827fa | 28 | #include <linux/udp.h> |
5a38795f | 29 | #include <linux/module.h> |
79f827fa KM |
30 | |
31 | #include <net/icmp.h> | |
32 | #include <net/ip.h> | |
33 | #include <net/udp.h> | |
1b7ee51f | 34 | #include <net/ip_tunnels.h> |
1b7ee51f PS |
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> | |
79f827fa KM |
42 | |
43 | #include "datapath.h" | |
79f827fa | 44 | #include "vport.h" |
0c7930a3 | 45 | #include "vport-vxlan.h" |
79f827fa | 46 | |
79f827fa KM |
47 | /** |
48 | * struct vxlan_port - Keeps track of open UDP ports | |
a109c9fb | 49 | * @vs: vxlan_sock created for the port. |
c405d282 | 50 | * @name: vport name. |
79f827fa KM |
51 | */ |
52 | struct vxlan_port { | |
a109c9fb | 53 | struct vxlan_sock *vs; |
c405d282 | 54 | char name[IFNAMSIZ]; |
0c7930a3 | 55 | u32 exts; /* VXLAN_F_* in <net/vxlan.h> */ |
79f827fa KM |
56 | }; |
57 | ||
5a38795f TG |
58 | static struct vport_ops ovs_vxlan_vport_ops; |
59 | ||
c405d282 PS |
60 | static inline struct vxlan_port *vxlan_vport(const struct vport *vport) |
61 | { | |
62 | return vport_priv(vport); | |
63 | } | |
64 | ||
3174a818 TG |
65 | static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb, |
66 | struct vxlan_metadata *md) | |
79f827fa | 67 | { |
f0cd669f | 68 | struct ovs_tunnel_info tun_info; |
0c7930a3 | 69 | struct vxlan_port *vxlan_port; |
a109c9fb PS |
70 | struct vport *vport = vs->data; |
71 | struct iphdr *iph; | |
0c7930a3 TG |
72 | struct ovs_vxlan_opts opts = { |
73 | .gbp = md->gbp, | |
74 | }; | |
79f827fa | 75 | __be64 key; |
0c7930a3 TG |
76 | __be16 flags; |
77 | ||
2d79a600 | 78 | flags = TUNNEL_KEY | (udp_hdr(skb)->check != 0 ? TUNNEL_CSUM : 0); |
0c7930a3 TG |
79 | vxlan_port = vxlan_vport(vport); |
80 | if (vxlan_port->exts & VXLAN_F_GBP && md->gbp) | |
81 | flags |= TUNNEL_VXLAN_OPT; | |
85c9de19 | 82 | |
79f827fa | 83 | /* Save outer tunnel values */ |
85c9de19 | 84 | iph = ip_hdr(skb); |
3174a818 | 85 | key = cpu_to_be64(ntohl(md->vni) >> 8); |
8b7ea2d4 WZ |
86 | ovs_flow_tun_info_init(&tun_info, iph, |
87 | udp_hdr(skb)->source, udp_hdr(skb)->dest, | |
0c7930a3 | 88 | key, flags, &opts, sizeof(opts)); |
79f827fa | 89 | |
f0cd669f | 90 | ovs_vport_receive(vport, skb, &tun_info); |
79f827fa KM |
91 | } |
92 | ||
c405d282 | 93 | static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb) |
85c9de19 | 94 | { |
c405d282 | 95 | struct vxlan_port *vxlan_port = vxlan_vport(vport); |
a109c9fb | 96 | __be16 dst_port = inet_sport(vxlan_port->vs->sock->sk); |
85c9de19 | 97 | |
1b7ee51f | 98 | if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port))) |
c405d282 | 99 | return -EMSGSIZE; |
0c7930a3 TG |
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 | ||
c405d282 | 115 | return 0; |
85c9de19 PS |
116 | } |
117 | ||
c405d282 | 118 | static void vxlan_tnl_destroy(struct vport *vport) |
79f827fa | 119 | { |
c405d282 | 120 | struct vxlan_port *vxlan_port = vxlan_vport(vport); |
79f827fa | 121 | |
a109c9fb | 122 | vxlan_sock_release(vxlan_port->vs); |
c405d282 PS |
123 | |
124 | ovs_vport_deferred_free(vport); | |
79f827fa | 125 | } |
85c9de19 | 126 | |
0c7930a3 TG |
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 | ||
c405d282 | 152 | static struct vport *vxlan_tnl_create(const struct vport_parms *parms) |
79f827fa | 153 | { |
c405d282 PS |
154 | struct net *net = ovs_dp_get_net(parms->dp); |
155 | struct nlattr *options = parms->options; | |
85c9de19 | 156 | struct vxlan_port *vxlan_port; |
a109c9fb | 157 | struct vxlan_sock *vs; |
c405d282 | 158 | struct vport *vport; |
79f827fa | 159 | struct nlattr *a; |
79f827fa | 160 | u16 dst_port; |
1b7ee51f | 161 | int err; |
79f827fa KM |
162 | |
163 | if (!options) { | |
164 | err = -EINVAL; | |
c405d282 | 165 | goto error; |
79f827fa | 166 | } |
79f827fa KM |
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; | |
c405d282 | 173 | goto error; |
79f827fa KM |
174 | } |
175 | ||
c405d282 PS |
176 | vport = ovs_vport_alloc(sizeof(struct vxlan_port), |
177 | &ovs_vxlan_vport_ops, parms); | |
178 | if (IS_ERR(vport)) | |
179 | return vport; | |
79f827fa | 180 | |
c405d282 | 181 | vxlan_port = vxlan_vport(vport); |
c405d282 | 182 | strncpy(vxlan_port->name, parms->name, IFNAMSIZ); |
79f827fa | 183 | |
0c7930a3 TG |
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); | |
a109c9fb | 195 | if (IS_ERR(vs)) { |
1b7ee51f | 196 | ovs_vport_free(vport); |
a109c9fb | 197 | return (void *)vs; |
1b7ee51f | 198 | } |
a109c9fb | 199 | vxlan_port->vs = vs; |
79f827fa | 200 | |
c405d282 | 201 | return vport; |
79f827fa KM |
202 | |
203 | error: | |
c405d282 | 204 | return ERR_PTR(err); |
79f827fa KM |
205 | } |
206 | ||
0c7930a3 TG |
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 | ||
c405d282 | 222 | static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb) |
79f827fa | 223 | { |
27914610 | 224 | struct ovs_key_ipv4_tunnel *tun_key; |
cb25142c | 225 | struct net *net = ovs_dp_get_net(vport->dp); |
1b7ee51f | 226 | struct vxlan_port *vxlan_port = vxlan_vport(vport); |
a109c9fb | 227 | __be16 dst_port = inet_sport(vxlan_port->vs->sock->sk); |
3174a818 | 228 | struct vxlan_metadata md = {0}; |
1b7ee51f PS |
229 | struct rtable *rt; |
230 | __be16 src_port; | |
231 | __be32 saddr; | |
232 | __be16 df; | |
1b7ee51f | 233 | int err; |
2d79a600 | 234 | u32 vxflags; |
1b7ee51f | 235 | |
fb66fbd1 | 236 | if (unlikely(!OVS_CB(skb)->egress_tun_info)) { |
1b7ee51f PS |
237 | err = -EINVAL; |
238 | goto error; | |
239 | } | |
79f827fa | 240 | |
fb66fbd1 | 241 | tun_key = &OVS_CB(skb)->egress_tun_info->tunnel; |
27914610 | 242 | |
1b7ee51f | 243 | /* Route lookup */ |
f0cd669f | 244 | saddr = tun_key->ipv4_src; |
1b7ee51f | 245 | rt = find_route(ovs_dp_get_net(vport->dp), |
f0cd669f JG |
246 | &saddr, tun_key->ipv4_dst, |
247 | IPPROTO_UDP, tun_key->ipv4_tos, | |
3025a772 | 248 | skb->mark); |
1b7ee51f PS |
249 | if (IS_ERR(rt)) { |
250 | err = PTR_ERR(rt); | |
251 | goto error; | |
252 | } | |
253 | ||
f0cd669f | 254 | df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; |
f6a0c895 | 255 | skb->ignore_df = 1; |
1b7ee51f | 256 | |
f6a0c895 | 257 | src_port = udp_flow_src_port(net, skb, 0, 0, true); |
3174a818 | 258 | md.vni = htonl(be64_to_cpu(tun_key->tun_id) << 8); |
0c7930a3 | 259 | md.gbp = vxlan_ext_gbp(skb); |
2d79a600 JG |
260 | vxflags = vxlan_port->exts | |
261 | (tun_key->tun_flags & TUNNEL_CSUM ? VXLAN_F_UDP_CSUM : 0); | |
1b7ee51f | 262 | |
c0cddcec | 263 | err = vxlan_xmit_skb(rt, vxlan_port->vs->sock->sk, skb, |
f0cd669f JG |
264 | saddr, tun_key->ipv4_dst, |
265 | tun_key->ipv4_tos, | |
266 | tun_key->ipv4_ttl, df, | |
1b7ee51f | 267 | src_port, dst_port, |
2d79a600 | 268 | &md, false, vxflags); |
1b7ee51f PS |
269 | if (err < 0) |
270 | ip_rt_put(rt); | |
93258bd7 | 271 | return err; |
1b7ee51f | 272 | error: |
93258bd7 | 273 | kfree_skb(skb); |
1b7ee51f | 274 | return err; |
79f827fa KM |
275 | } |
276 | ||
8b7ea2d4 WZ |
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; | |
8b7ea2d4 | 284 | |
f6a0c895 | 285 | src_port = udp_flow_src_port(net, skb, 0, 0, true); |
8b7ea2d4 WZ |
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 | ||
c405d282 | 293 | static const char *vxlan_get_name(const struct vport *vport) |
79f827fa | 294 | { |
c405d282 PS |
295 | struct vxlan_port *vxlan_port = vxlan_vport(vport); |
296 | return vxlan_port->name; | |
79f827fa KM |
297 | } |
298 | ||
5a38795f | 299 | static struct vport_ops ovs_vxlan_vport_ops = { |
8b7ea2d4 WZ |
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, | |
5a38795f | 307 | .owner = THIS_MODULE, |
79f827fa | 308 | }; |
5a38795f TG |
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"); |