Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 12 | #include <linux/in.h> |
| 13 | #include <linux/ip.h> |
| 14 | #include <linux/net.h> |
| 15 | #include <linux/rculist.h> |
| 16 | #include <linux/udp.h> |
| 17 | #include <linux/if_vlan.h> |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 18 | #include <linux/module.h> |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 19 | |
| 20 | #include <net/geneve.h> |
| 21 | #include <net/icmp.h> |
| 22 | #include <net/ip.h> |
| 23 | #include <net/route.h> |
| 24 | #include <net/udp.h> |
| 25 | #include <net/xfrm.h> |
| 26 | |
| 27 | #include "datapath.h" |
| 28 | #include "vport.h" |
| 29 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 30 | static struct vport_ops ovs_geneve_vport_ops; |
| 31 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 32 | /** |
| 33 | * struct geneve_port - Keeps track of open UDP ports |
Fabian Frederick | 7e78cc4 | 2014-10-15 21:03:18 +0200 | [diff] [blame] | 34 | * @gs: The socket created for this port number. |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 35 | * @name: vport name. |
| 36 | */ |
| 37 | struct geneve_port { |
| 38 | struct geneve_sock *gs; |
| 39 | char name[IFNAMSIZ]; |
| 40 | }; |
| 41 | |
| 42 | static LIST_HEAD(geneve_ports); |
| 43 | |
| 44 | static inline struct geneve_port *geneve_vport(const struct vport *vport) |
| 45 | { |
| 46 | return vport_priv(vport); |
| 47 | } |
| 48 | |
| 49 | static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb) |
| 50 | { |
| 51 | return (struct genevehdr *)(udp_hdr(skb) + 1); |
| 52 | } |
| 53 | |
| 54 | /* Convert 64 bit tunnel ID to 24 bit VNI. */ |
| 55 | static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni) |
| 56 | { |
| 57 | #ifdef __BIG_ENDIAN |
| 58 | vni[0] = (__force __u8)(tun_id >> 16); |
| 59 | vni[1] = (__force __u8)(tun_id >> 8); |
| 60 | vni[2] = (__force __u8)tun_id; |
| 61 | #else |
| 62 | vni[0] = (__force __u8)((__force u64)tun_id >> 40); |
| 63 | vni[1] = (__force __u8)((__force u64)tun_id >> 48); |
| 64 | vni[2] = (__force __u8)((__force u64)tun_id >> 56); |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | /* Convert 24 bit VNI to 64 bit tunnel ID. */ |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 69 | static __be64 vni_to_tunnel_id(const __u8 *vni) |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 70 | { |
| 71 | #ifdef __BIG_ENDIAN |
| 72 | return (vni[0] << 16) | (vni[1] << 8) | vni[2]; |
| 73 | #else |
| 74 | return (__force __be64)(((__force u64)vni[0] << 40) | |
| 75 | ((__force u64)vni[1] << 48) | |
| 76 | ((__force u64)vni[2] << 56)); |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | static void geneve_rcv(struct geneve_sock *gs, struct sk_buff *skb) |
| 81 | { |
| 82 | struct vport *vport = gs->rcv_data; |
| 83 | struct genevehdr *geneveh = geneve_hdr(skb); |
| 84 | int opts_len; |
| 85 | struct ovs_tunnel_info tun_info; |
| 86 | __be64 key; |
| 87 | __be16 flags; |
| 88 | |
| 89 | opts_len = geneveh->opt_len * 4; |
| 90 | |
Thomas Graf | 1dd144c | 2015-01-15 03:53:59 +0100 | [diff] [blame] | 91 | flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 92 | (udp_hdr(skb)->check != 0 ? TUNNEL_CSUM : 0) | |
| 93 | (geneveh->oam ? TUNNEL_OAM : 0) | |
| 94 | (geneveh->critical ? TUNNEL_CRIT_OPT : 0); |
| 95 | |
| 96 | key = vni_to_tunnel_id(geneveh->vni); |
| 97 | |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 98 | ovs_flow_tun_info_init(&tun_info, ip_hdr(skb), |
| 99 | udp_hdr(skb)->source, udp_hdr(skb)->dest, |
| 100 | key, flags, |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 101 | geneveh->options, opts_len); |
| 102 | |
| 103 | ovs_vport_receive(vport, skb, &tun_info); |
| 104 | } |
| 105 | |
| 106 | static int geneve_get_options(const struct vport *vport, |
| 107 | struct sk_buff *skb) |
| 108 | { |
| 109 | struct geneve_port *geneve_port = geneve_vport(vport); |
Andy Zhou | 0a5d1c5 | 2014-10-06 13:22:51 -0700 | [diff] [blame] | 110 | struct inet_sock *sk = inet_sk(geneve_port->gs->sock->sk); |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 111 | |
Andy Zhou | 0a5d1c5 | 2014-10-06 13:22:51 -0700 | [diff] [blame] | 112 | if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(sk->inet_sport))) |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 113 | return -EMSGSIZE; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static void geneve_tnl_destroy(struct vport *vport) |
| 118 | { |
| 119 | struct geneve_port *geneve_port = geneve_vport(vport); |
| 120 | |
| 121 | geneve_sock_release(geneve_port->gs); |
| 122 | |
| 123 | ovs_vport_deferred_free(vport); |
| 124 | } |
| 125 | |
| 126 | static struct vport *geneve_tnl_create(const struct vport_parms *parms) |
| 127 | { |
| 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; |
| 132 | struct vport *vport; |
| 133 | struct nlattr *a; |
| 134 | int err; |
| 135 | u16 dst_port; |
| 136 | |
| 137 | if (!options) { |
| 138 | err = -EINVAL; |
| 139 | goto error; |
| 140 | } |
| 141 | |
| 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); |
| 145 | } else { |
| 146 | /* Require destination port from userspace. */ |
| 147 | err = -EINVAL; |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | vport = ovs_vport_alloc(sizeof(struct geneve_port), |
| 152 | &ovs_geneve_vport_ops, parms); |
| 153 | if (IS_ERR(vport)) |
| 154 | return vport; |
| 155 | |
| 156 | geneve_port = geneve_vport(vport); |
| 157 | strncpy(geneve_port->name, parms->name, IFNAMSIZ); |
| 158 | |
| 159 | gs = geneve_sock_add(net, htons(dst_port), geneve_rcv, vport, true, 0); |
| 160 | if (IS_ERR(gs)) { |
| 161 | ovs_vport_free(vport); |
| 162 | return (void *)gs; |
| 163 | } |
| 164 | geneve_port->gs = gs; |
| 165 | |
| 166 | return vport; |
| 167 | error: |
| 168 | return ERR_PTR(err); |
| 169 | } |
| 170 | |
| 171 | static int geneve_tnl_send(struct vport *vport, struct sk_buff *skb) |
| 172 | { |
Fan Du | 3f4c1d8 | 2015-01-14 13:10:35 +0800 | [diff] [blame] | 173 | const struct ovs_key_ipv4_tunnel *tun_key; |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 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; |
| 178 | __be16 sport; |
| 179 | struct rtable *rt; |
| 180 | struct flowi4 fl; |
Thomas Graf | 1dd144c | 2015-01-15 03:53:59 +0100 | [diff] [blame] | 181 | u8 vni[3], opts_len, *opts; |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 182 | __be16 df; |
| 183 | int err; |
| 184 | |
| 185 | tun_info = OVS_CB(skb)->egress_tun_info; |
| 186 | if (unlikely(!tun_info)) { |
| 187 | err = -EINVAL; |
| 188 | goto error; |
| 189 | } |
| 190 | |
| 191 | tun_key = &tun_info->tunnel; |
Fan Du | 3f4c1d8 | 2015-01-14 13:10:35 +0800 | [diff] [blame] | 192 | rt = ovs_tunnel_route_lookup(net, tun_key, skb->mark, &fl, IPPROTO_UDP); |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 193 | if (IS_ERR(rt)) { |
| 194 | err = PTR_ERR(rt); |
| 195 | goto error; |
| 196 | } |
| 197 | |
| 198 | df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; |
| 199 | sport = udp_flow_src_port(net, skb, 1, USHRT_MAX, true); |
| 200 | tunnel_id_to_vni(tun_key->tun_id, vni); |
| 201 | skb->ignore_df = 1; |
| 202 | |
Thomas Graf | 1dd144c | 2015-01-15 03:53:59 +0100 | [diff] [blame] | 203 | if (tun_key->tun_flags & TUNNEL_GENEVE_OPT) { |
| 204 | opts = (u8 *)tun_info->options; |
| 205 | opts_len = tun_info->options_len; |
| 206 | } else { |
| 207 | opts = NULL; |
| 208 | opts_len = 0; |
| 209 | } |
| 210 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 211 | err = geneve_xmit_skb(geneve_port->gs, rt, skb, fl.saddr, |
| 212 | tun_key->ipv4_dst, tun_key->ipv4_tos, |
| 213 | tun_key->ipv4_ttl, df, sport, dport, |
Thomas Graf | 1dd144c | 2015-01-15 03:53:59 +0100 | [diff] [blame] | 214 | tun_key->tun_flags, vni, opts_len, opts, |
Jesse Gross | b869387 | 2015-01-28 16:32:46 -0800 | [diff] [blame^] | 215 | !!(tun_key->tun_flags & TUNNEL_CSUM), false); |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 216 | if (err < 0) |
| 217 | ip_rt_put(rt); |
Pravin B Shelar | 997e068 | 2014-12-23 16:20:32 -0800 | [diff] [blame] | 218 | return err; |
| 219 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 220 | error: |
Pravin B Shelar | 997e068 | 2014-12-23 16:20:32 -0800 | [diff] [blame] | 221 | kfree_skb(skb); |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 222 | return err; |
| 223 | } |
| 224 | |
| 225 | static const char *geneve_get_name(const struct vport *vport) |
| 226 | { |
| 227 | struct geneve_port *geneve_port = geneve_vport(vport); |
| 228 | |
| 229 | return geneve_port->name; |
| 230 | } |
| 231 | |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 232 | static int geneve_get_egress_tun_info(struct vport *vport, struct sk_buff *skb, |
| 233 | struct ovs_tunnel_info *egress_tun_info) |
| 234 | { |
| 235 | struct geneve_port *geneve_port = geneve_vport(vport); |
| 236 | struct net *net = ovs_dp_get_net(vport->dp); |
| 237 | __be16 dport = inet_sk(geneve_port->gs->sock->sk)->inet_sport; |
| 238 | __be16 sport = udp_flow_src_port(net, skb, 1, USHRT_MAX, true); |
| 239 | |
| 240 | /* Get tp_src and tp_dst, refert to geneve_build_header(). |
| 241 | */ |
| 242 | return ovs_tunnel_get_egress_info(egress_tun_info, |
| 243 | ovs_dp_get_net(vport->dp), |
| 244 | OVS_CB(skb)->egress_tun_info, |
| 245 | IPPROTO_UDP, skb->mark, sport, dport); |
| 246 | } |
| 247 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 248 | static struct vport_ops ovs_geneve_vport_ops = { |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 249 | .type = OVS_VPORT_TYPE_GENEVE, |
| 250 | .create = geneve_tnl_create, |
| 251 | .destroy = geneve_tnl_destroy, |
| 252 | .get_name = geneve_get_name, |
| 253 | .get_options = geneve_get_options, |
| 254 | .send = geneve_tnl_send, |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 255 | .owner = THIS_MODULE, |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 256 | .get_egress_tun_info = geneve_get_egress_tun_info, |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 257 | }; |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 258 | |
| 259 | static int __init ovs_geneve_tnl_init(void) |
| 260 | { |
| 261 | return ovs_vport_ops_register(&ovs_geneve_vport_ops); |
| 262 | } |
| 263 | |
| 264 | static void __exit ovs_geneve_tnl_exit(void) |
| 265 | { |
| 266 | ovs_vport_ops_unregister(&ovs_geneve_vport_ops); |
| 267 | } |
| 268 | |
| 269 | module_init(ovs_geneve_tnl_init); |
| 270 | module_exit(ovs_geneve_tnl_exit); |
| 271 | |
| 272 | MODULE_DESCRIPTION("OVS: Geneve swiching port"); |
| 273 | MODULE_LICENSE("GPL"); |
| 274 | MODULE_ALIAS("vport-type-5"); |