2 * Copyright (c) 2013 Nicira, Inc.
3 * Copyright (c) 2013 Cisco Systems, Inc.
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.
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.
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
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/version.h>
26 #include <linux/net.h>
27 #include <linux/rculist.h>
28 #include <linux/udp.h>
29 #include <linux/module.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>
45 #include "vport-vxlan.h"
48 * struct vxlan_port - Keeps track of open UDP ports
49 * @vs: vxlan_sock created for the port.
53 struct vxlan_sock
*vs
;
55 u32 exts
; /* VXLAN_F_* in <net/vxlan.h> */
58 static struct vport_ops ovs_vxlan_vport_ops
;
60 static inline struct vxlan_port
*vxlan_vport(const struct vport
*vport
)
62 return vport_priv(vport
);
65 static void vxlan_rcv(struct vxlan_sock
*vs
, struct sk_buff
*skb
,
66 struct vxlan_metadata
*md
)
68 struct ovs_tunnel_info tun_info
;
69 struct vxlan_port
*vxlan_port
;
70 struct vport
*vport
= vs
->data
;
72 struct ovs_vxlan_opts opts
= {
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
;
83 /* Save outer tunnel values */
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
));
90 ovs_vport_receive(vport
, skb
, &tun_info
);
93 static int vxlan_get_options(const struct vport
*vport
, struct sk_buff
*skb
)
95 struct vxlan_port
*vxlan_port
= vxlan_vport(vport
);
96 __be16 dst_port
= inet_sport(vxlan_port
->vs
->sock
->sk
);
98 if (nla_put_u16(skb
, OVS_TUNNEL_ATTR_DST_PORT
, ntohs(dst_port
)))
101 if (vxlan_port
->exts
) {
104 exts
= nla_nest_start(skb
, OVS_TUNNEL_ATTR_EXTENSION
);
108 if (vxlan_port
->exts
& VXLAN_F_GBP
&&
109 nla_put_flag(skb
, OVS_VXLAN_EXT_GBP
))
112 nla_nest_end(skb
, exts
);
118 static void vxlan_tnl_destroy(struct vport
*vport
)
120 struct vxlan_port
*vxlan_port
= vxlan_vport(vport
);
122 vxlan_sock_release(vxlan_port
->vs
);
124 ovs_vport_deferred_free(vport
);
127 static const struct nla_policy exts_policy
[OVS_VXLAN_EXT_MAX
+1] = {
128 [OVS_VXLAN_EXT_GBP
] = { .type
= NLA_FLAG
, },
131 static int vxlan_configure_exts(struct vport
*vport
, struct nlattr
*attr
)
133 struct nlattr
*exts
[OVS_VXLAN_EXT_MAX
+1];
134 struct vxlan_port
*vxlan_port
;
137 if (nla_len(attr
) < sizeof(struct nlattr
))
140 err
= nla_parse_nested(exts
, OVS_VXLAN_EXT_MAX
, attr
, exts_policy
);
144 vxlan_port
= vxlan_vport(vport
);
146 if (exts
[OVS_VXLAN_EXT_GBP
])
147 vxlan_port
->exts
|= VXLAN_F_GBP
;
152 static struct vport
*vxlan_tnl_create(const struct vport_parms
*parms
)
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
;
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
);
171 /* Require destination port from userspace. */
176 vport
= ovs_vport_alloc(sizeof(struct vxlan_port
),
177 &ovs_vxlan_vport_ops
, parms
);
181 vxlan_port
= vxlan_vport(vport
);
182 strncpy(vxlan_port
->name
, parms
->name
, IFNAMSIZ
);
184 a
= nla_find_nested(options
, OVS_TUNNEL_ATTR_EXTENSION
);
186 err
= vxlan_configure_exts(vport
, a
);
188 ovs_vport_free(vport
);
193 vs
= vxlan_sock_add(net
, htons(dst_port
), vxlan_rcv
, vport
, true,
196 ovs_vport_free(vport
);
207 static int vxlan_ext_gbp(struct sk_buff
*skb
)
209 const struct ovs_tunnel_info
*tun_info
;
210 const struct ovs_vxlan_opts
*opts
;
212 tun_info
= OVS_CB(skb
)->egress_tun_info
;
213 opts
= tun_info
->options
;
215 if (tun_info
->tunnel
.tun_flags
& TUNNEL_VXLAN_OPT
&&
216 tun_info
->options_len
>= sizeof(*opts
))
222 static int vxlan_tnl_send(struct vport
*vport
, struct sk_buff
*skb
)
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};
236 if (unlikely(!OVS_CB(skb
)->egress_tun_info
)) {
241 tun_key
= &OVS_CB(skb
)->egress_tun_info
->tunnel
;
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
,
254 df
= tun_key
->tun_flags
& TUNNEL_DONT_FRAGMENT
? htons(IP_DF
) : 0;
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);
263 err
= vxlan_xmit_skb(vxlan_port
->vs
, rt
, skb
,
264 saddr
, tun_key
->ipv4_dst
,
266 tun_key
->ipv4_ttl
, df
,
268 &md
, false, vxflags
);
277 static int vxlan_get_egress_tun_info(struct vport
*vport
, struct sk_buff
*skb
,
278 struct ovs_tunnel_info
*egress_tun_info
)
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
);
285 src_port
= udp_flow_src_port(net
, skb
, 0, 0, true);
287 return ovs_tunnel_get_egress_info(egress_tun_info
, net
,
288 OVS_CB(skb
)->egress_tun_info
,
289 IPPROTO_UDP
, skb
->mark
,
293 static const char *vxlan_get_name(const struct vport
*vport
)
295 struct vxlan_port
*vxlan_port
= vxlan_vport(vport
);
296 return vxlan_port
->name
;
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
,
310 static int __init
ovs_vxlan_tnl_init(void)
312 return ovs_vport_ops_register(&ovs_vxlan_vport_ops
);
315 static void __exit
ovs_vxlan_tnl_exit(void)
317 ovs_vport_ops_unregister(&ovs_vxlan_vport_ops
);
320 module_init(ovs_vxlan_tnl_init
);
321 module_exit(ovs_vxlan_tnl_exit
);
323 MODULE_DESCRIPTION("OVS: VXLAN switching port");
324 MODULE_LICENSE("GPL");
325 MODULE_ALIAS("vport-type-4");