datapath: Move generic tunnel functions to lisp module.
[sliver-openvswitch.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 "vport.h"
46
47 #define OVS_VXLAN_RCV_PRIORITY          8
48
49 /**
50  * struct vxlan_port - Keeps track of open UDP ports
51  * @vh: vxlan_handler created for the port.
52  * @name: vport name.
53  */
54 struct vxlan_port {
55         struct vxlan_handler *vh;
56         char name[IFNAMSIZ];
57 };
58
59 static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
60 {
61         return vport_priv(vport);
62 }
63
64 /* Called with rcu_read_lock and BH disabled. */
65 static int vxlan_rcv(struct vxlan_handler *vh, struct sk_buff *skb, __be32 vx_vni)
66 {
67         struct vport *vport = vh->data;
68         struct iphdr *iph;
69         struct ovs_key_ipv4_tunnel tun_key;
70         __be64 key;
71
72         /* Save outer tunnel values */
73         iph = ip_hdr(skb);
74         key = cpu_to_be64(ntohl(vx_vni) >> 8);
75         ovs_flow_tun_key_init(&tun_key, iph, key, TUNNEL_KEY);
76
77         ovs_vport_receive(vport, skb, &tun_key);
78         return PACKET_RCVD;
79 }
80
81 static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
82 {
83         struct vxlan_port *vxlan_port = vxlan_vport(vport);
84         __be16 dst_port = inet_sport(vxlan_port->vh->vs->sock->sk);
85
86         if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
87                 return -EMSGSIZE;
88         return 0;
89 }
90
91 static void vxlan_tnl_destroy(struct vport *vport)
92 {
93         struct vxlan_port *vxlan_port = vxlan_vport(vport);
94
95         vxlan_handler_put(vxlan_port->vh);
96
97         ovs_vport_deferred_free(vport);
98 }
99
100 static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
101 {
102         struct net *net = ovs_dp_get_net(parms->dp);
103         struct nlattr *options = parms->options;
104         struct vxlan_port *vxlan_port;
105         struct vxlan_handler *vh;
106         struct vport *vport;
107         struct nlattr *a;
108         u16 dst_port;
109         int err;
110
111         if (!options) {
112                 err = -EINVAL;
113                 goto error;
114         }
115         a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
116         if (a && nla_len(a) == sizeof(u16)) {
117                 dst_port = nla_get_u16(a);
118         } else {
119                 /* Require destination port from userspace. */
120                 err = -EINVAL;
121                 goto error;
122         }
123
124         vport = ovs_vport_alloc(sizeof(struct vxlan_port),
125                                 &ovs_vxlan_vport_ops, parms);
126         if (IS_ERR(vport))
127                 return vport;
128
129         vxlan_port = vxlan_vport(vport);
130         strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
131
132         vh = vxlan_handler_add(net, htons(dst_port), vxlan_rcv,
133                                vport, OVS_VXLAN_RCV_PRIORITY, true);
134         if (IS_ERR(vh)) {
135                 ovs_vport_free(vport);
136                 return (void *)vh;
137         }
138         vxlan_port->vh = vh;
139
140         return vport;
141
142 error:
143         return ERR_PTR(err);
144 }
145
146 static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
147 {
148         struct vxlan_port *vxlan_port = vxlan_vport(vport);
149         __be16 dst_port = inet_sport(vxlan_port->vh->vs->sock->sk);
150         struct net *net = ovs_dp_get_net(vport->dp);
151         struct rtable *rt;
152         __be16 src_port;
153         __be32 saddr;
154         __be16 df;
155         int port_min;
156         int port_max;
157         int err;
158
159         if (unlikely(!OVS_CB(skb)->tun_key)) {
160                 err = -EINVAL;
161                 goto error;
162         }
163
164         forward_ip_summed(skb, true);
165
166         /* Route lookup */
167         saddr = OVS_CB(skb)->tun_key->ipv4_src;
168         rt = find_route(ovs_dp_get_net(vport->dp),
169                         &saddr,
170                         OVS_CB(skb)->tun_key->ipv4_dst,
171                         IPPROTO_UDP,
172                         OVS_CB(skb)->tun_key->ipv4_tos,
173                         skb_get_mark(skb));
174         if (IS_ERR(rt)) {
175                 err = PTR_ERR(rt);
176                 goto error;
177         }
178
179         df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
180                 htons(IP_DF) : 0;
181
182         skb->local_df = 1;
183
184         inet_get_local_port_range(&port_min, &port_max);
185         src_port = vxlan_src_port(port_min, port_max, skb);
186
187         err = vxlan_xmit_skb(net, vxlan_port->vh, rt, skb,
188                              saddr, OVS_CB(skb)->tun_key->ipv4_dst,
189                              OVS_CB(skb)->tun_key->ipv4_tos,
190                              OVS_CB(skb)->tun_key->ipv4_ttl, df,
191                              src_port, dst_port,
192                              htonl(be64_to_cpu(OVS_CB(skb)->tun_key->tun_id) << 8));
193         if (err < 0)
194                 ip_rt_put(rt);
195 error:
196         return err;
197 }
198
199 static const char *vxlan_get_name(const struct vport *vport)
200 {
201         struct vxlan_port *vxlan_port = vxlan_vport(vport);
202         return vxlan_port->name;
203 }
204
205 const struct vport_ops ovs_vxlan_vport_ops = {
206         .type           = OVS_VPORT_TYPE_VXLAN,
207         .create         = vxlan_tnl_create,
208         .destroy        = vxlan_tnl_destroy,
209         .get_name       = vxlan_get_name,
210         .get_options    = vxlan_get_options,
211         .send           = vxlan_tnl_send,
212 };
213 #else
214 #warning VXLAN tunneling will not be available on kernels before 2.6.26
215 #endif /* Linux kernel < 2.6.26 */