]> git.proxmox.com Git - ovs.git/blob - datapath/vport-vxlan.c
datapath: Restructure vxlan tunneling.
[ovs.git] / datapath / vport-vxlan.c
1 /*
2 * Copyright (c) 2011 Nicira, Inc.
3 * Copyright (c) 2012 Cisco Systems, Inc.
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>
23 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
24
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/net.h>
28 #include <linux/rculist.h>
29 #include <linux/udp.h>
30
31 #include <net/icmp.h>
32 #include <net/ip.h>
33 #include <net/udp.h>
34 #include <net/ip_tunnels.h>
35 #include <net/udp.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>
43
44 #include "datapath.h"
45 #include "tunnel.h"
46 #include "vport.h"
47
48 #define OVS_VXLAN_RCV_PRIORITY 8
49
50 /**
51 * struct vxlan_port - Keeps track of open UDP ports
52 * @vh: vxlan_handler created for the port.
53 * @name: vport name.
54 */
55 struct vxlan_port {
56 struct vxlan_handler *vh;
57 char name[IFNAMSIZ];
58 };
59
60 static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
61 {
62 return vport_priv(vport);
63 }
64
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)
67 {
68 struct vport *vport = vh->data;
69 struct iphdr *iph;
70 struct ovs_key_ipv4_tunnel tun_key;
71 __be64 key;
72
73 /* Save outer tunnel values */
74 iph = ip_hdr(skb);
75 key = cpu_to_be64(ntohl(vx_vni) >> 8);
76 tnl_tun_key_init(&tun_key, iph, key, TUNNEL_KEY);
77
78 ovs_vport_receive(vport, skb, &tun_key);
79 return PACKET_RCVD;
80 }
81
82 static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
83 {
84 struct vxlan_port *vxlan_port = vxlan_vport(vport);
85 __be16 dst_port = inet_sport(vxlan_port->vh->vs->sock->sk);
86
87 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
88 return -EMSGSIZE;
89 return 0;
90 }
91
92 static void vxlan_tnl_destroy(struct vport *vport)
93 {
94 struct vxlan_port *vxlan_port = vxlan_vport(vport);
95
96 vxlan_handler_put(vxlan_port->vh);
97
98 ovs_vport_deferred_free(vport);
99 }
100
101 static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
102 {
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;
107 struct vport *vport;
108 struct nlattr *a;
109 u16 dst_port;
110 int err;
111
112 if (!options) {
113 err = -EINVAL;
114 goto error;
115 }
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);
119 } else {
120 /* Require destination port from userspace. */
121 err = -EINVAL;
122 goto error;
123 }
124
125 vport = ovs_vport_alloc(sizeof(struct vxlan_port),
126 &ovs_vxlan_vport_ops, parms);
127 if (IS_ERR(vport))
128 return vport;
129
130 vxlan_port = vxlan_vport(vport);
131 strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
132
133 vh = vxlan_handler_add(net, htons(dst_port), vxlan_rcv,
134 vport, OVS_VXLAN_RCV_PRIORITY, true);
135 if (IS_ERR(vh)) {
136 ovs_vport_free(vport);
137 return (void *)vh;
138 }
139 vxlan_port->vh = vh;
140
141 return vport;
142
143 error:
144 return ERR_PTR(err);
145 }
146
147 static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
148 {
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);
152 struct rtable *rt;
153 __be16 src_port;
154 __be32 saddr;
155 __be16 df;
156 int port_min;
157 int port_max;
158 int err;
159
160 if (unlikely(!OVS_CB(skb)->tun_key)) {
161 err = -EINVAL;
162 goto error;
163 }
164
165 forward_ip_summed(skb, true);
166
167 /* Route lookup */
168 saddr = OVS_CB(skb)->tun_key->ipv4_src;
169 rt = find_route(ovs_dp_get_net(vport->dp),
170 &saddr,
171 OVS_CB(skb)->tun_key->ipv4_dst,
172 IPPROTO_UDP,
173 OVS_CB(skb)->tun_key->ipv4_tos,
174 skb_get_mark(skb));
175 if (IS_ERR(rt)) {
176 err = PTR_ERR(rt);
177 goto error;
178 }
179
180 df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
181 htons(IP_DF) : 0;
182
183 skb->local_df = 1;
184
185 inet_get_local_port_range(&port_min, &port_max);
186 src_port = vxlan_src_port(port_min, port_max, skb);
187
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,
192 src_port, dst_port,
193 htonl(be64_to_cpu(OVS_CB(skb)->tun_key->tun_id) << 8));
194 if (err < 0)
195 ip_rt_put(rt);
196 error:
197 return err;
198 }
199
200 static const char *vxlan_get_name(const struct vport *vport)
201 {
202 struct vxlan_port *vxlan_port = vxlan_vport(vport);
203 return vxlan_port->name;
204 }
205
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,
213 };
214 #else
215 #warning VXLAN tunneling will not be available on kernels before 2.6.26
216 #endif /* Linux kernel < 2.6.26 */