2 * Copyright (c) 2011 Nicira, Inc.
3 * Copyright (c) 2012 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>
23 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
27 #include <linux/net.h>
28 #include <linux/rculist.h>
29 #include <linux/udp.h>
34 #include <net/ip_tunnels.h>
36 #include <net/rtnetlink.h>
37 #include <net/route.h>
38 #include <net/dsfield.h>
39 #include <net/inet_ecn.h>
40 #include <net/net_namespace.h>
41 #include <net/netns/generic.h>
42 #include <net/vxlan.h>
48 #define OVS_VXLAN_RCV_PRIORITY 8
51 * struct vxlan_port - Keeps track of open UDP ports
52 * @vh: vxlan_handler created for the port.
56 struct vxlan_handler
*vh
;
60 static inline struct vxlan_port
*vxlan_vport(const struct vport
*vport
)
62 return vport_priv(vport
);
65 /* Called with rcu_read_lock and BH disabled. */
66 static int vxlan_rcv(struct vxlan_handler
*vh
, struct sk_buff
*skb
, __be32 vx_vni
)
68 struct vport
*vport
= vh
->data
;
70 struct ovs_key_ipv4_tunnel tun_key
;
73 /* Save outer tunnel values */
75 key
= cpu_to_be64(ntohl(vx_vni
) >> 8);
76 tnl_tun_key_init(&tun_key
, iph
, key
, TUNNEL_KEY
);
78 ovs_vport_receive(vport
, skb
, &tun_key
);
82 static int vxlan_get_options(const struct vport
*vport
, struct sk_buff
*skb
)
84 struct vxlan_port
*vxlan_port
= vxlan_vport(vport
);
85 __be16 dst_port
= inet_sport(vxlan_port
->vh
->vs
->sock
->sk
);
87 if (nla_put_u16(skb
, OVS_TUNNEL_ATTR_DST_PORT
, ntohs(dst_port
)))
92 static void vxlan_tnl_destroy(struct vport
*vport
)
94 struct vxlan_port
*vxlan_port
= vxlan_vport(vport
);
96 vxlan_handler_put(vxlan_port
->vh
);
98 ovs_vport_deferred_free(vport
);
101 static struct vport
*vxlan_tnl_create(const struct vport_parms
*parms
)
103 struct net
*net
= ovs_dp_get_net(parms
->dp
);
104 struct nlattr
*options
= parms
->options
;
105 struct vxlan_port
*vxlan_port
;
106 struct vxlan_handler
*vh
;
116 a
= nla_find_nested(options
, OVS_TUNNEL_ATTR_DST_PORT
);
117 if (a
&& nla_len(a
) == sizeof(u16
)) {
118 dst_port
= nla_get_u16(a
);
120 /* Require destination port from userspace. */
125 vport
= ovs_vport_alloc(sizeof(struct vxlan_port
),
126 &ovs_vxlan_vport_ops
, parms
);
130 vxlan_port
= vxlan_vport(vport
);
131 strncpy(vxlan_port
->name
, parms
->name
, IFNAMSIZ
);
133 vh
= vxlan_handler_add(net
, htons(dst_port
), vxlan_rcv
,
134 vport
, OVS_VXLAN_RCV_PRIORITY
, true);
136 ovs_vport_free(vport
);
147 static int vxlan_tnl_send(struct vport
*vport
, struct sk_buff
*skb
)
149 struct vxlan_port
*vxlan_port
= vxlan_vport(vport
);
150 __be16 dst_port
= inet_sport(vxlan_port
->vh
->vs
->sock
->sk
);
151 struct net
*net
= ovs_dp_get_net(vport
->dp
);
160 if (unlikely(!OVS_CB(skb
)->tun_key
)) {
165 forward_ip_summed(skb
, true);
168 saddr
= OVS_CB(skb
)->tun_key
->ipv4_src
;
169 rt
= find_route(ovs_dp_get_net(vport
->dp
),
171 OVS_CB(skb
)->tun_key
->ipv4_dst
,
173 OVS_CB(skb
)->tun_key
->ipv4_tos
,
180 df
= OVS_CB(skb
)->tun_key
->tun_flags
& TUNNEL_DONT_FRAGMENT
?
185 inet_get_local_port_range(&port_min
, &port_max
);
186 src_port
= vxlan_src_port(port_min
, port_max
, skb
);
188 err
= vxlan_xmit_skb(net
, vxlan_port
->vh
, rt
, skb
,
189 saddr
, OVS_CB(skb
)->tun_key
->ipv4_dst
,
190 OVS_CB(skb
)->tun_key
->ipv4_tos
,
191 OVS_CB(skb
)->tun_key
->ipv4_ttl
, df
,
193 htonl(be64_to_cpu(OVS_CB(skb
)->tun_key
->tun_id
) << 8));
200 static const char *vxlan_get_name(const struct vport
*vport
)
202 struct vxlan_port
*vxlan_port
= vxlan_vport(vport
);
203 return vxlan_port
->name
;
206 const struct vport_ops ovs_vxlan_vport_ops
= {
207 .type
= OVS_VPORT_TYPE_VXLAN
,
208 .create
= vxlan_tnl_create
,
209 .destroy
= vxlan_tnl_destroy
,
210 .get_name
= vxlan_get_name
,
211 .get_options
= vxlan_get_options
,
212 .send
= vxlan_tnl_send
,
215 #warning VXLAN tunneling will not be available on kernels before 2.6.26
216 #endif /* Linux kernel < 2.6.26 */