]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/vport-vxlan.c
appveyor: Build windows kernel datapath.
[mirror_ovs.git] / datapath / vport-vxlan.c
CommitLineData
79f827fa 1/*
a109c9fb
PS
2 * Copyright (c) 2013 Nicira, Inc.
3 * Copyright (c) 2013 Cisco Systems, Inc.
79f827fa
KM
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>
79f827fa
KM
23
24#include <linux/in.h>
25#include <linux/ip.h>
79f827fa 26#include <linux/net.h>
85c9de19 27#include <linux/rculist.h>
79f827fa
KM
28#include <linux/udp.h>
29
30#include <net/icmp.h>
31#include <net/ip.h>
32#include <net/udp.h>
1b7ee51f 33#include <net/ip_tunnels.h>
1b7ee51f
PS
34#include <net/rtnetlink.h>
35#include <net/route.h>
36#include <net/dsfield.h>
37#include <net/inet_ecn.h>
38#include <net/net_namespace.h>
39#include <net/netns/generic.h>
40#include <net/vxlan.h>
79f827fa
KM
41
42#include "datapath.h"
79f827fa 43#include "vport.h"
79f827fa 44
79f827fa
KM
45/**
46 * struct vxlan_port - Keeps track of open UDP ports
a109c9fb 47 * @vs: vxlan_sock created for the port.
c405d282 48 * @name: vport name.
79f827fa
KM
49 */
50struct vxlan_port {
a109c9fb 51 struct vxlan_sock *vs;
c405d282 52 char name[IFNAMSIZ];
79f827fa
KM
53};
54
c405d282
PS
55static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
56{
57 return vport_priv(vport);
58}
59
3174a818
TG
60static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
61 struct vxlan_metadata *md)
79f827fa 62{
f0cd669f 63 struct ovs_tunnel_info tun_info;
a109c9fb
PS
64 struct vport *vport = vs->data;
65 struct iphdr *iph;
79f827fa 66 __be64 key;
85c9de19 67
79f827fa 68 /* Save outer tunnel values */
85c9de19 69 iph = ip_hdr(skb);
3174a818 70 key = cpu_to_be64(ntohl(md->vni) >> 8);
8b7ea2d4
WZ
71 ovs_flow_tun_info_init(&tun_info, iph,
72 udp_hdr(skb)->source, udp_hdr(skb)->dest,
73 key, TUNNEL_KEY, NULL, 0);
79f827fa 74
f0cd669f 75 ovs_vport_receive(vport, skb, &tun_info);
79f827fa
KM
76}
77
c405d282 78static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
85c9de19 79{
c405d282 80 struct vxlan_port *vxlan_port = vxlan_vport(vport);
a109c9fb 81 __be16 dst_port = inet_sport(vxlan_port->vs->sock->sk);
85c9de19 82
1b7ee51f 83 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
c405d282
PS
84 return -EMSGSIZE;
85 return 0;
85c9de19
PS
86}
87
c405d282 88static void vxlan_tnl_destroy(struct vport *vport)
79f827fa 89{
c405d282 90 struct vxlan_port *vxlan_port = vxlan_vport(vport);
79f827fa 91
a109c9fb 92 vxlan_sock_release(vxlan_port->vs);
c405d282
PS
93
94 ovs_vport_deferred_free(vport);
79f827fa 95}
85c9de19 96
c405d282 97static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
79f827fa 98{
c405d282
PS
99 struct net *net = ovs_dp_get_net(parms->dp);
100 struct nlattr *options = parms->options;
85c9de19 101 struct vxlan_port *vxlan_port;
a109c9fb 102 struct vxlan_sock *vs;
c405d282 103 struct vport *vport;
79f827fa 104 struct nlattr *a;
79f827fa 105 u16 dst_port;
1b7ee51f 106 int err;
79f827fa
KM
107
108 if (!options) {
109 err = -EINVAL;
c405d282 110 goto error;
79f827fa 111 }
79f827fa
KM
112 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
113 if (a && nla_len(a) == sizeof(u16)) {
114 dst_port = nla_get_u16(a);
115 } else {
116 /* Require destination port from userspace. */
117 err = -EINVAL;
c405d282 118 goto error;
79f827fa
KM
119 }
120
c405d282
PS
121 vport = ovs_vport_alloc(sizeof(struct vxlan_port),
122 &ovs_vxlan_vport_ops, parms);
123 if (IS_ERR(vport))
124 return vport;
79f827fa 125
c405d282 126 vxlan_port = vxlan_vport(vport);
c405d282 127 strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
79f827fa 128
7ab1a0d8 129 vs = vxlan_sock_add(net, htons(dst_port), vxlan_rcv, vport, true, 0);
a109c9fb 130 if (IS_ERR(vs)) {
1b7ee51f 131 ovs_vport_free(vport);
a109c9fb 132 return (void *)vs;
1b7ee51f 133 }
a109c9fb 134 vxlan_port->vs = vs;
79f827fa 135
c405d282 136 return vport;
79f827fa
KM
137
138error:
c405d282 139 return ERR_PTR(err);
79f827fa
KM
140}
141
c405d282 142static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
79f827fa 143{
27914610 144 struct ovs_key_ipv4_tunnel *tun_key;
cb25142c 145 struct net *net = ovs_dp_get_net(vport->dp);
1b7ee51f 146 struct vxlan_port *vxlan_port = vxlan_vport(vport);
a109c9fb 147 __be16 dst_port = inet_sport(vxlan_port->vs->sock->sk);
3174a818 148 struct vxlan_metadata md = {0};
1b7ee51f
PS
149 struct rtable *rt;
150 __be16 src_port;
151 __be32 saddr;
152 __be16 df;
1b7ee51f
PS
153 int err;
154
fb66fbd1 155 if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
1b7ee51f
PS
156 err = -EINVAL;
157 goto error;
158 }
79f827fa 159
fb66fbd1 160 tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
27914610 161
1b7ee51f 162 /* Route lookup */
f0cd669f 163 saddr = tun_key->ipv4_src;
1b7ee51f 164 rt = find_route(ovs_dp_get_net(vport->dp),
f0cd669f
JG
165 &saddr, tun_key->ipv4_dst,
166 IPPROTO_UDP, tun_key->ipv4_tos,
3025a772 167 skb->mark);
1b7ee51f
PS
168 if (IS_ERR(rt)) {
169 err = PTR_ERR(rt);
170 goto error;
171 }
172
f0cd669f 173 df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
f6a0c895 174 skb->ignore_df = 1;
1b7ee51f 175
f6a0c895 176 src_port = udp_flow_src_port(net, skb, 0, 0, true);
3174a818 177 md.vni = htonl(be64_to_cpu(tun_key->tun_id) << 8);
1b7ee51f 178
13beaf62 179 err = vxlan_xmit_skb(vxlan_port->vs, rt, skb,
f0cd669f
JG
180 saddr, tun_key->ipv4_dst,
181 tun_key->ipv4_tos,
182 tun_key->ipv4_ttl, df,
1b7ee51f 183 src_port, dst_port,
2311260f 184 &md, false);
1b7ee51f
PS
185 if (err < 0)
186 ip_rt_put(rt);
93258bd7 187 return err;
1b7ee51f 188error:
93258bd7 189 kfree_skb(skb);
1b7ee51f 190 return err;
79f827fa
KM
191}
192
8b7ea2d4
WZ
193static int vxlan_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
194 struct ovs_tunnel_info *egress_tun_info)
195{
196 struct net *net = ovs_dp_get_net(vport->dp);
197 struct vxlan_port *vxlan_port = vxlan_vport(vport);
198 __be16 dst_port = inet_sport(vxlan_port->vs->sock->sk);
199 __be16 src_port;
8b7ea2d4 200
f6a0c895 201 src_port = udp_flow_src_port(net, skb, 0, 0, true);
8b7ea2d4
WZ
202
203 return ovs_tunnel_get_egress_info(egress_tun_info, net,
204 OVS_CB(skb)->egress_tun_info,
205 IPPROTO_UDP, skb->mark,
206 src_port, dst_port);
207}
208
c405d282 209static const char *vxlan_get_name(const struct vport *vport)
79f827fa 210{
c405d282
PS
211 struct vxlan_port *vxlan_port = vxlan_vport(vport);
212 return vxlan_port->name;
79f827fa
KM
213}
214
215const struct vport_ops ovs_vxlan_vport_ops = {
8b7ea2d4
WZ
216 .type = OVS_VPORT_TYPE_VXLAN,
217 .create = vxlan_tnl_create,
218 .destroy = vxlan_tnl_destroy,
219 .get_name = vxlan_get_name,
220 .get_options = vxlan_get_options,
221 .send = vxlan_tnl_send,
222 .get_egress_tun_info = vxlan_get_egress_tun_info,
79f827fa 223};