blob: 484864dd0e689290dfca8a2c204e984de1b33184 [file] [log] [blame]
Jesse Grossf5796682014-10-03 15:35:33 -07001/*
2 * Copyright (c) 2014 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/version.h>
13
14#include <linux/in.h>
15#include <linux/ip.h>
16#include <linux/net.h>
17#include <linux/rculist.h>
18#include <linux/udp.h>
19#include <linux/if_vlan.h>
Thomas Graf62b9c8d2014-10-22 17:29:06 +020020#include <linux/module.h>
Jesse Grossf5796682014-10-03 15:35:33 -070021
22#include <net/geneve.h>
23#include <net/icmp.h>
24#include <net/ip.h>
25#include <net/route.h>
26#include <net/udp.h>
27#include <net/xfrm.h>
28
29#include "datapath.h"
30#include "vport.h"
31
Thomas Graf62b9c8d2014-10-22 17:29:06 +020032static struct vport_ops ovs_geneve_vport_ops;
33
Jesse Grossf5796682014-10-03 15:35:33 -070034/**
35 * struct geneve_port - Keeps track of open UDP ports
Fabian Frederick7e78cc42014-10-15 21:03:18 +020036 * @gs: The socket created for this port number.
Jesse Grossf5796682014-10-03 15:35:33 -070037 * @name: vport name.
38 */
39struct geneve_port {
40 struct geneve_sock *gs;
41 char name[IFNAMSIZ];
42};
43
44static LIST_HEAD(geneve_ports);
45
46static inline struct geneve_port *geneve_vport(const struct vport *vport)
47{
48 return vport_priv(vport);
49}
50
51static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
52{
53 return (struct genevehdr *)(udp_hdr(skb) + 1);
54}
55
56/* Convert 64 bit tunnel ID to 24 bit VNI. */
57static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
58{
59#ifdef __BIG_ENDIAN
60 vni[0] = (__force __u8)(tun_id >> 16);
61 vni[1] = (__force __u8)(tun_id >> 8);
62 vni[2] = (__force __u8)tun_id;
63#else
64 vni[0] = (__force __u8)((__force u64)tun_id >> 40);
65 vni[1] = (__force __u8)((__force u64)tun_id >> 48);
66 vni[2] = (__force __u8)((__force u64)tun_id >> 56);
67#endif
68}
69
70/* Convert 24 bit VNI to 64 bit tunnel ID. */
Thomas Graf12eb18f2014-11-06 06:58:52 -080071static __be64 vni_to_tunnel_id(const __u8 *vni)
Jesse Grossf5796682014-10-03 15:35:33 -070072{
73#ifdef __BIG_ENDIAN
74 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
75#else
76 return (__force __be64)(((__force u64)vni[0] << 40) |
77 ((__force u64)vni[1] << 48) |
78 ((__force u64)vni[2] << 56));
79#endif
80}
81
82static void geneve_rcv(struct geneve_sock *gs, struct sk_buff *skb)
83{
84 struct vport *vport = gs->rcv_data;
85 struct genevehdr *geneveh = geneve_hdr(skb);
86 int opts_len;
87 struct ovs_tunnel_info tun_info;
88 __be64 key;
89 __be16 flags;
90
91 opts_len = geneveh->opt_len * 4;
92
93 flags = TUNNEL_KEY | TUNNEL_OPTIONS_PRESENT |
94 (udp_hdr(skb)->check != 0 ? TUNNEL_CSUM : 0) |
95 (geneveh->oam ? TUNNEL_OAM : 0) |
96 (geneveh->critical ? TUNNEL_CRIT_OPT : 0);
97
98 key = vni_to_tunnel_id(geneveh->vni);
99
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800100 ovs_flow_tun_info_init(&tun_info, ip_hdr(skb),
101 udp_hdr(skb)->source, udp_hdr(skb)->dest,
102 key, flags,
Jesse Grossf5796682014-10-03 15:35:33 -0700103 geneveh->options, opts_len);
104
105 ovs_vport_receive(vport, skb, &tun_info);
106}
107
108static int geneve_get_options(const struct vport *vport,
109 struct sk_buff *skb)
110{
111 struct geneve_port *geneve_port = geneve_vport(vport);
Andy Zhou0a5d1c52014-10-06 13:22:51 -0700112 struct inet_sock *sk = inet_sk(geneve_port->gs->sock->sk);
Jesse Grossf5796682014-10-03 15:35:33 -0700113
Andy Zhou0a5d1c52014-10-06 13:22:51 -0700114 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(sk->inet_sport)))
Jesse Grossf5796682014-10-03 15:35:33 -0700115 return -EMSGSIZE;
116 return 0;
117}
118
119static void geneve_tnl_destroy(struct vport *vport)
120{
121 struct geneve_port *geneve_port = geneve_vport(vport);
122
123 geneve_sock_release(geneve_port->gs);
124
125 ovs_vport_deferred_free(vport);
126}
127
128static struct vport *geneve_tnl_create(const struct vport_parms *parms)
129{
130 struct net *net = ovs_dp_get_net(parms->dp);
131 struct nlattr *options = parms->options;
132 struct geneve_port *geneve_port;
133 struct geneve_sock *gs;
134 struct vport *vport;
135 struct nlattr *a;
136 int err;
137 u16 dst_port;
138
139 if (!options) {
140 err = -EINVAL;
141 goto error;
142 }
143
144 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
145 if (a && nla_len(a) == sizeof(u16)) {
146 dst_port = nla_get_u16(a);
147 } else {
148 /* Require destination port from userspace. */
149 err = -EINVAL;
150 goto error;
151 }
152
153 vport = ovs_vport_alloc(sizeof(struct geneve_port),
154 &ovs_geneve_vport_ops, parms);
155 if (IS_ERR(vport))
156 return vport;
157
158 geneve_port = geneve_vport(vport);
159 strncpy(geneve_port->name, parms->name, IFNAMSIZ);
160
161 gs = geneve_sock_add(net, htons(dst_port), geneve_rcv, vport, true, 0);
162 if (IS_ERR(gs)) {
163 ovs_vport_free(vport);
164 return (void *)gs;
165 }
166 geneve_port->gs = gs;
167
168 return vport;
169error:
170 return ERR_PTR(err);
171}
172
173static int geneve_tnl_send(struct vport *vport, struct sk_buff *skb)
174{
175 struct ovs_key_ipv4_tunnel *tun_key;
176 struct ovs_tunnel_info *tun_info;
177 struct net *net = ovs_dp_get_net(vport->dp);
178 struct geneve_port *geneve_port = geneve_vport(vport);
179 __be16 dport = inet_sk(geneve_port->gs->sock->sk)->inet_sport;
180 __be16 sport;
181 struct rtable *rt;
182 struct flowi4 fl;
183 u8 vni[3];
184 __be16 df;
185 int err;
186
187 tun_info = OVS_CB(skb)->egress_tun_info;
188 if (unlikely(!tun_info)) {
189 err = -EINVAL;
190 goto error;
191 }
192
193 tun_key = &tun_info->tunnel;
194
195 /* Route lookup */
196 memset(&fl, 0, sizeof(fl));
197 fl.daddr = tun_key->ipv4_dst;
198 fl.saddr = tun_key->ipv4_src;
199 fl.flowi4_tos = RT_TOS(tun_key->ipv4_tos);
200 fl.flowi4_mark = skb->mark;
201 fl.flowi4_proto = IPPROTO_UDP;
202
203 rt = ip_route_output_key(net, &fl);
204 if (IS_ERR(rt)) {
205 err = PTR_ERR(rt);
206 goto error;
207 }
208
209 df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
210 sport = udp_flow_src_port(net, skb, 1, USHRT_MAX, true);
211 tunnel_id_to_vni(tun_key->tun_id, vni);
212 skb->ignore_df = 1;
213
214 err = geneve_xmit_skb(geneve_port->gs, rt, skb, fl.saddr,
215 tun_key->ipv4_dst, tun_key->ipv4_tos,
216 tun_key->ipv4_ttl, df, sport, dport,
217 tun_key->tun_flags, vni,
218 tun_info->options_len, (u8 *)tun_info->options,
219 false);
220 if (err < 0)
221 ip_rt_put(rt);
Pravin B Shelar997e0682014-12-23 16:20:32 -0800222 return err;
223
Jesse Grossf5796682014-10-03 15:35:33 -0700224error:
Pravin B Shelar997e0682014-12-23 16:20:32 -0800225 kfree_skb(skb);
Jesse Grossf5796682014-10-03 15:35:33 -0700226 return err;
227}
228
229static const char *geneve_get_name(const struct vport *vport)
230{
231 struct geneve_port *geneve_port = geneve_vport(vport);
232
233 return geneve_port->name;
234}
235
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800236static int geneve_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
237 struct ovs_tunnel_info *egress_tun_info)
238{
239 struct geneve_port *geneve_port = geneve_vport(vport);
240 struct net *net = ovs_dp_get_net(vport->dp);
241 __be16 dport = inet_sk(geneve_port->gs->sock->sk)->inet_sport;
242 __be16 sport = udp_flow_src_port(net, skb, 1, USHRT_MAX, true);
243
244 /* Get tp_src and tp_dst, refert to geneve_build_header().
245 */
246 return ovs_tunnel_get_egress_info(egress_tun_info,
247 ovs_dp_get_net(vport->dp),
248 OVS_CB(skb)->egress_tun_info,
249 IPPROTO_UDP, skb->mark, sport, dport);
250}
251
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200252static struct vport_ops ovs_geneve_vport_ops = {
Jesse Grossf5796682014-10-03 15:35:33 -0700253 .type = OVS_VPORT_TYPE_GENEVE,
254 .create = geneve_tnl_create,
255 .destroy = geneve_tnl_destroy,
256 .get_name = geneve_get_name,
257 .get_options = geneve_get_options,
258 .send = geneve_tnl_send,
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200259 .owner = THIS_MODULE,
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800260 .get_egress_tun_info = geneve_get_egress_tun_info,
Jesse Grossf5796682014-10-03 15:35:33 -0700261};
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200262
263static int __init ovs_geneve_tnl_init(void)
264{
265 return ovs_vport_ops_register(&ovs_geneve_vport_ops);
266}
267
268static void __exit ovs_geneve_tnl_exit(void)
269{
270 ovs_vport_ops_unregister(&ovs_geneve_vport_ops);
271}
272
273module_init(ovs_geneve_tnl_init);
274module_exit(ovs_geneve_tnl_exit);
275
276MODULE_DESCRIPTION("OVS: Geneve swiching port");
277MODULE_LICENSE("GPL");
278MODULE_ALIAS("vport-type-5");