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/list.h>
28 #include <linux/net.h>
29 #include <linux/udp.h>
39 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
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.
51 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
53 static inline int vxlan_hdr_len(const struct tnl_mutable_config *mutable,
54 const struct ovs_key_ipv4_tunnel *tun_key)
60 * struct vxlan_port - Keeps track of open UDP ports
61 * @list: list element.
62 * @port: The UDP port number in network byte order.
63 * @socket: The socket created for this port number.
64 * @count: How many ports are using this socket/port.
67 struct list_head list;
69 struct socket *vxlan_rcv_socket;
73 static LIST_HEAD(vxlan_ports);
75 static struct vxlan_port *vxlan_port_exists(struct net *net, __be16 port)
77 struct vxlan_port *vxlan_port;
79 list_for_each_entry(vxlan_port, &vxlan_ports, list) {
80 if (vxlan_port->port == port &&
81 net_eq(sock_net(vxlan_port->vxlan_rcv_socket->sk), net))
88 static inline struct vxlanhdr *vxlan_hdr(const struct sk_buff *skb)
90 return (struct vxlanhdr *)(udp_hdr(skb) + 1);
93 /* Compute source port for outgoing packet.
94 * Currently we use the flow hash.
96 static u16 get_src_port(struct sk_buff *skb)
101 u32 hash = OVS_CB(skb)->flow->hash;
103 inet_get_local_port_range(&low, &high);
104 range = (high - low) + 1;
105 return (((u64) hash * range) >> 32) + low;
108 static struct sk_buff *vxlan_build_header(const struct vport *vport,
109 const struct tnl_mutable_config *mutable,
110 struct dst_entry *dst,
114 struct udphdr *udph = udp_hdr(skb);
115 struct vxlanhdr *vxh = (struct vxlanhdr *)(udph + 1);
116 const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
120 tnl_get_param(mutable, tun_key, &flags, &out_key);
122 udph->dest = mutable->dst_port;
123 udph->source = htons(get_src_port(skb));
125 udph->len = htons(skb->len - skb_transport_offset(skb));
127 vxh->vx_flags = htonl(VXLAN_FLAGS);
128 vxh->vx_vni = htonl(be64_to_cpu(out_key) << 8);
131 * Allow our local IP stack to fragment the outer packet even if the
132 * DF bit is set as a last resort. We also need to force selection of
133 * an IP ID here because Linux will otherwise leave it at 0 if the
134 * packet originally had DF set.
137 __ip_select_ident(ip_hdr(skb), dst, 0);
142 /* Called with rcu_read_lock and BH disabled. */
143 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
146 struct vxlanhdr *vxh;
147 const struct tnl_mutable_config *mutable;
149 struct ovs_key_ipv4_tunnel tun_key;
151 u32 tunnel_flags = 0;
153 if (unlikely(!pskb_may_pull(skb, VXLAN_HLEN + ETH_HLEN)))
156 vxh = vxlan_hdr(skb);
157 if (unlikely(vxh->vx_flags != htonl(VXLAN_FLAGS) ||
158 vxh->vx_vni & htonl(0xff)))
161 __skb_pull(skb, VXLAN_HLEN);
162 skb_postpull_rcsum(skb, skb_transport_header(skb), VXLAN_HLEN + ETH_HLEN);
164 key = cpu_to_be64(ntohl(vxh->vx_vni) >> 8);
167 vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr,
168 key, TNL_T_PROTO_VXLAN, &mutable);
169 if (unlikely(!vport))
172 if (mutable->flags & TNL_F_IN_KEY_MATCH || !mutable->key.daddr)
173 tunnel_flags = OVS_TNL_F_KEY;
177 /* Save outer tunnel values */
178 tnl_tun_key_init(&tun_key, iph, key, tunnel_flags);
179 OVS_CB(skb)->tun_key = &tun_key;
181 ovs_tnl_rcv(vport, skb);
190 /* Random value. Irrelevant as long as it's not 0 since we set the handler. */
191 #define UDP_ENCAP_VXLAN 1
192 static int vxlan_socket_init(struct vxlan_port *vxlan_port, struct net *net)
195 struct sockaddr_in sin;
197 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
198 &vxlan_port->vxlan_rcv_socket);
202 /* release net ref. */
203 sk_change_net(vxlan_port->vxlan_rcv_socket->sk, net);
205 sin.sin_family = AF_INET;
206 sin.sin_addr.s_addr = htonl(INADDR_ANY);
207 sin.sin_port = vxlan_port->port;
209 err = kernel_bind(vxlan_port->vxlan_rcv_socket, (struct sockaddr *)&sin,
210 sizeof(struct sockaddr_in));
214 udp_sk(vxlan_port->vxlan_rcv_socket->sk)->encap_type = UDP_ENCAP_VXLAN;
215 udp_sk(vxlan_port->vxlan_rcv_socket->sk)->encap_rcv = vxlan_rcv;
222 sk_release_kernel(vxlan_port->vxlan_rcv_socket->sk);
224 pr_warn("cannot register vxlan protocol handler\n");
228 static void vxlan_tunnel_release(struct vxlan_port *vxlan_port)
232 if (vxlan_port->count == 0) {
233 /* Release old socket */
234 sk_release_kernel(vxlan_port->vxlan_rcv_socket->sk);
235 list_del(&vxlan_port->list);
239 static int vxlan_tunnel_setup(struct net *net, struct nlattr *options,
240 struct vxlan_port **vxport)
245 struct vxlan_port *vxlan_port = NULL;
254 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
255 if (a && nla_len(a) == sizeof(u16)) {
256 dst_port = nla_get_u16(a);
258 /* Require destination port from userspace. */
263 /* Verify if we already have a socket created for this port */
264 vxlan_port = vxlan_port_exists(net, htons(dst_port));
268 *vxport = vxlan_port;
272 /* Add a new socket for this port */
273 vxlan_port = kzalloc(sizeof(struct vxlan_port), GFP_KERNEL);
279 vxlan_port->port = htons(dst_port);
280 vxlan_port->count = 1;
281 list_add_tail(&vxlan_port->list, &vxlan_ports);
283 err = vxlan_socket_init(vxlan_port, net);
287 *vxport = vxlan_port;
291 list_del(&vxlan_port->list);
297 static int vxlan_set_options(struct vport *vport, struct nlattr *options)
300 struct net *net = ovs_dp_get_net(vport->dp);
301 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
302 struct tnl_mutable_config *config;
303 struct vxlan_port *old_port = NULL;
304 struct vxlan_port *vxlan_port = NULL;
306 config = rtnl_dereference(tnl_vport->mutable);
308 old_port = vxlan_port_exists(net, config->dst_port);
310 err = vxlan_tunnel_setup(net, options, &vxlan_port);
314 err = ovs_tnl_set_options(vport, options);
317 vxlan_tunnel_release(vxlan_port);
319 /* Release old socket */
320 vxlan_tunnel_release(old_port);
326 static const struct tnl_ops ovs_vxlan_tnl_ops = {
327 .tunnel_type = TNL_T_PROTO_VXLAN,
328 .ipproto = IPPROTO_UDP,
329 .hdr_len = vxlan_hdr_len,
330 .build_header = vxlan_build_header,
333 static void vxlan_tnl_destroy(struct vport *vport)
335 struct vxlan_port *vxlan_port;
336 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
337 struct tnl_mutable_config *config;
339 config = rtnl_dereference(tnl_vport->mutable);
341 vxlan_port = vxlan_port_exists(ovs_dp_get_net(vport->dp),
344 vxlan_tunnel_release(vxlan_port);
346 ovs_tnl_destroy(vport);
349 static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
353 struct vxlan_port *vxlan_port = NULL;
355 err = vxlan_tunnel_setup(ovs_dp_get_net(parms->dp), parms->options,
360 vport = ovs_tnl_create(parms, &ovs_vxlan_vport_ops, &ovs_vxlan_tnl_ops);
363 vxlan_tunnel_release(vxlan_port);
368 const struct vport_ops ovs_vxlan_vport_ops = {
369 .type = OVS_VPORT_TYPE_VXLAN,
370 .flags = VPORT_F_TUN_ID,
371 .create = vxlan_tnl_create,
372 .destroy = vxlan_tnl_destroy,
373 .get_name = ovs_tnl_get_name,
374 .get_options = ovs_tnl_get_options,
375 .set_options = vxlan_set_options,
376 .send = ovs_tnl_send,
379 #warning VXLAN tunneling will not be available on kernels before 2.6.26
380 #endif /* Linux kernel < 2.6.26 */