2 * Copyright (c) 2014 Nicira, Inc.
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.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/version.h>
16 #include <linux/net.h>
17 #include <linux/rculist.h>
18 #include <linux/udp.h>
19 #include <linux/if_vlan.h>
20 #include <linux/module.h>
22 #include <net/geneve.h>
25 #include <net/route.h>
32 static struct vport_ops ovs_geneve_vport_ops;
35 * struct geneve_port - Keeps track of open UDP ports
36 * @gs: The socket created for this port number.
40 struct geneve_sock *gs;
44 static LIST_HEAD(geneve_ports);
46 static inline struct geneve_port *geneve_vport(const struct vport *vport)
48 return vport_priv(vport);
51 static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
53 return (struct genevehdr *)(udp_hdr(skb) + 1);
56 /* Convert 64 bit tunnel ID to 24 bit VNI. */
57 static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
60 vni[0] = (__force __u8)(tun_id >> 16);
61 vni[1] = (__force __u8)(tun_id >> 8);
62 vni[2] = (__force __u8)tun_id;
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);
70 /* Convert 24 bit VNI to 64 bit tunnel ID. */
71 static __be64 vni_to_tunnel_id(__u8 *vni)
74 return (vni[0] << 16) | (vni[1] << 8) | vni[2];
76 return (__force __be64)(((__force u64)vni[0] << 40) |
77 ((__force u64)vni[1] << 48) |
78 ((__force u64)vni[2] << 56));
82 static void geneve_rcv(struct geneve_sock *gs, struct sk_buff *skb)
84 struct vport *vport = gs->rcv_data;
85 struct genevehdr *geneveh = geneve_hdr(skb);
87 struct ovs_tunnel_info tun_info;
91 opts_len = geneveh->opt_len * 4;
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);
98 key = vni_to_tunnel_id(geneveh->vni);
100 ovs_flow_tun_info_init(&tun_info, ip_hdr(skb), key, flags,
101 geneveh->options, opts_len);
103 ovs_vport_receive(vport, skb, &tun_info);
106 static int geneve_get_options(const struct vport *vport,
109 struct geneve_port *geneve_port = geneve_vport(vport);
110 struct inet_sock *sk = inet_sk(geneve_port->gs->sock->sk);
112 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(sk->inet_sport)))
117 static void geneve_tnl_destroy(struct vport *vport)
119 struct geneve_port *geneve_port = geneve_vport(vport);
121 geneve_sock_release(geneve_port->gs);
123 ovs_vport_deferred_free(vport);
126 static struct vport *geneve_tnl_create(const struct vport_parms *parms)
128 struct net *net = ovs_dp_get_net(parms->dp);
129 struct nlattr *options = parms->options;
130 struct geneve_port *geneve_port;
131 struct geneve_sock *gs;
142 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
143 if (a && nla_len(a) == sizeof(u16)) {
144 dst_port = nla_get_u16(a);
146 /* Require destination port from userspace. */
151 vport = ovs_vport_alloc(sizeof(struct geneve_port),
152 &ovs_geneve_vport_ops, parms);
156 geneve_port = geneve_vport(vport);
157 strncpy(geneve_port->name, parms->name, IFNAMSIZ);
159 gs = geneve_sock_add(net, htons(dst_port), geneve_rcv, vport, true, 0);
161 ovs_vport_free(vport);
164 geneve_port->gs = gs;
171 static int geneve_tnl_send(struct vport *vport, struct sk_buff *skb)
173 struct ovs_key_ipv4_tunnel *tun_key;
174 struct ovs_tunnel_info *tun_info;
175 struct net *net = ovs_dp_get_net(vport->dp);
176 struct geneve_port *geneve_port = geneve_vport(vport);
177 __be16 dport = inet_sk(geneve_port->gs->sock->sk)->inet_sport;
185 tun_info = OVS_CB(skb)->egress_tun_info;
186 if (unlikely(!tun_info)) {
191 tun_key = &tun_info->tunnel;
194 memset(&fl, 0, sizeof(fl));
195 fl.daddr = tun_key->ipv4_dst;
196 fl.saddr = tun_key->ipv4_src;
197 fl.flowi4_tos = RT_TOS(tun_key->ipv4_tos);
198 fl.flowi4_mark = skb->mark;
199 fl.flowi4_proto = IPPROTO_UDP;
201 rt = ip_route_output_key(net, &fl);
207 df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
208 sport = udp_flow_src_port(net, skb, 1, USHRT_MAX, true);
209 tunnel_id_to_vni(tun_key->tun_id, vni);
212 err = geneve_xmit_skb(geneve_port->gs, rt, skb, fl.saddr,
213 tun_key->ipv4_dst, tun_key->ipv4_tos,
214 tun_key->ipv4_ttl, df, sport, dport,
215 tun_key->tun_flags, vni,
216 tun_info->options_len, (u8 *)tun_info->options,
224 static const char *geneve_get_name(const struct vport *vport)
226 struct geneve_port *geneve_port = geneve_vport(vport);
228 return geneve_port->name;
231 static struct vport_ops ovs_geneve_vport_ops = {
232 .type = OVS_VPORT_TYPE_GENEVE,
233 .create = geneve_tnl_create,
234 .destroy = geneve_tnl_destroy,
235 .get_name = geneve_get_name,
236 .get_options = geneve_get_options,
237 .send = geneve_tnl_send,
238 .owner = THIS_MODULE,
241 static int __init ovs_geneve_tnl_init(void)
243 return ovs_vport_ops_register(&ovs_geneve_vport_ops);
246 static void __exit ovs_geneve_tnl_exit(void)
248 ovs_vport_ops_unregister(&ovs_geneve_vport_ops);
251 module_init(ovs_geneve_tnl_init);
252 module_exit(ovs_geneve_tnl_exit);
254 MODULE_DESCRIPTION("OVS: Geneve swiching port");
255 MODULE_LICENSE("GPL");
256 MODULE_ALIAS("vport-type-5");