#include <net/icmp.h>
#include <net/ip.h>
#include <net/udp.h>
+#include <net/ip_tunnels.h>
+#include <net/udp.h>
+#include <net/rtnetlink.h>
+#include <net/route.h>
+#include <net/dsfield.h>
+#include <net/inet_ecn.h>
+#include <net/net_namespace.h>
+#include <net/netns/generic.h>
+#include <net/vxlan.h>
#include "datapath.h"
-#include "tunnel.h"
#include "vport.h"
-#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
-
-/**
- * struct vxlanhdr - VXLAN header
- * @vx_flags: Must have the exact value %VXLAN_FLAGS.
- * @vx_vni: VXLAN Network Identifier (VNI) in top 24 bits, low 8 bits zeroed.
- */
-struct vxlanhdr {
- __be32 vx_flags;
- __be32 vx_vni;
-};
-
-#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
+#define OVS_VXLAN_RCV_PRIORITY 8
/**
* struct vxlan_port - Keeps track of open UDP ports
- * @dst_port: vxlan UDP port no.
- * @list: list element in @vxlan_ports.
- * @vxlan_rcv_socket: The socket created for this port number.
+ * @vh: vxlan_handler created for the port.
* @name: vport name.
*/
struct vxlan_port {
- __be16 dst_port;
- struct list_head list;
- struct socket *vxlan_rcv_socket;
+ struct vxlan_handler *vh;
char name[IFNAMSIZ];
};
-static LIST_HEAD(vxlan_ports);
-
static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
{
return vport_priv(vport);
}
-static struct vxlan_port *vxlan_find_port(struct net *net, __be16 port)
-{
- struct vxlan_port *vxlan_port;
-
- list_for_each_entry_rcu(vxlan_port, &vxlan_ports, list) {
-
- if (vxlan_port->dst_port == port &&
- net_eq(sock_net(vxlan_port->vxlan_rcv_socket->sk), net))
- return vxlan_port;
- }
-
- return NULL;
-}
-
-static inline struct vxlanhdr *vxlan_hdr(const struct sk_buff *skb)
-{
- return (struct vxlanhdr *)(udp_hdr(skb) + 1);
-}
-
-static void vxlan_build_header(const struct vport *vport,
- struct sk_buff *skb,
- int tunnel_hlen)
-{
- struct vxlan_port *vxlan_port = vxlan_vport(vport);
- struct udphdr *udph = udp_hdr(skb);
- struct vxlanhdr *vxh = (struct vxlanhdr *)(udph + 1);
- const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
-
- udph->dest = vxlan_port->dst_port;
- udph->source = htons(ovs_tnl_get_src_port(skb));
- udph->check = 0;
- udph->len = htons(skb->len - skb_transport_offset(skb));
-
- vxh->vx_flags = htonl(VXLAN_FLAGS);
- vxh->vx_vni = htonl(be64_to_cpu(tun_key->tun_id) << 8);
-}
-
/* Called with rcu_read_lock and BH disabled. */
-static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
+static int vxlan_rcv(struct vxlan_handler *vh, struct sk_buff *skb, __be32 vx_vni)
{
- struct vxlan_port *vxlan_vport;
- struct vxlanhdr *vxh;
+ struct vport *vport = vh->data;
struct iphdr *iph;
struct ovs_key_ipv4_tunnel tun_key;
__be64 key;
- vxlan_vport = vxlan_find_port(dev_net(skb->dev), udp_hdr(skb)->dest);
- if (unlikely(!vxlan_vport))
- goto error;
-
- if (unlikely(!pskb_may_pull(skb, VXLAN_HLEN + ETH_HLEN)))
- goto error;
-
- vxh = vxlan_hdr(skb);
- if (unlikely(vxh->vx_flags != htonl(VXLAN_FLAGS) ||
- vxh->vx_vni & htonl(0xff)))
- goto error;
-
- skb_pull_rcsum(skb, VXLAN_HLEN);
-
- key = cpu_to_be64(ntohl(vxh->vx_vni) >> 8);
-
/* Save outer tunnel values */
iph = ip_hdr(skb);
- tnl_tun_key_init(&tun_key, iph, key, OVS_TNL_F_KEY);
+ key = cpu_to_be64(ntohl(vx_vni) >> 8);
+ ovs_flow_tun_key_init(&tun_key, iph, key, TUNNEL_KEY);
- ovs_tnl_rcv(vport_from_priv(vxlan_vport), skb, &tun_key);
- goto out;
-
-error:
- kfree_skb(skb);
-out:
- return 0;
-}
-
-/* Random value. Irrelevant as long as it's not 0 since we set the handler. */
-#define UDP_ENCAP_VXLAN 1
-static int vxlan_socket_init(struct vxlan_port *vxlan_port, struct net *net)
-{
- struct sockaddr_in sin;
- int err;
-
- err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
- &vxlan_port->vxlan_rcv_socket);
- if (err)
- goto error;
-
- /* release net ref. */
- sk_change_net(vxlan_port->vxlan_rcv_socket->sk, net);
-
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = htonl(INADDR_ANY);
- sin.sin_port = vxlan_port->dst_port;
-
- err = kernel_bind(vxlan_port->vxlan_rcv_socket, (struct sockaddr *)&sin,
- sizeof(struct sockaddr_in));
- if (err)
- goto error_sock;
-
- udp_sk(vxlan_port->vxlan_rcv_socket->sk)->encap_type = UDP_ENCAP_VXLAN;
- udp_sk(vxlan_port->vxlan_rcv_socket->sk)->encap_rcv = vxlan_rcv;
-
- udp_encap_enable();
-
- return 0;
-
-error_sock:
- sk_release_kernel(vxlan_port->vxlan_rcv_socket->sk);
-error:
- pr_warn("cannot register vxlan protocol handler\n");
- return err;
+ ovs_vport_receive(vport, skb, &tun_key);
+ return PACKET_RCVD;
}
static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
{
struct vxlan_port *vxlan_port = vxlan_vport(vport);
+ __be16 dst_port = inet_sport(vxlan_port->vh->vs->sock->sk);
- if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(vxlan_port->dst_port)))
+ if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
return -EMSGSIZE;
return 0;
}
{
struct vxlan_port *vxlan_port = vxlan_vport(vport);
- list_del_rcu(&vxlan_port->list);
- /* Release socket */
- sk_release_kernel(vxlan_port->vxlan_rcv_socket->sk);
+ vxlan_handler_put(vxlan_port->vh);
ovs_vport_deferred_free(vport);
}
struct net *net = ovs_dp_get_net(parms->dp);
struct nlattr *options = parms->options;
struct vxlan_port *vxlan_port;
+ struct vxlan_handler *vh;
struct vport *vport;
struct nlattr *a;
- int err;
u16 dst_port;
+ int err;
if (!options) {
err = -EINVAL;
goto error;
}
- /* Verify if we already have a socket created for this port */
- if (vxlan_find_port(net, htons(dst_port))) {
- err = -EEXIST;
- goto error;
- }
-
vport = ovs_vport_alloc(sizeof(struct vxlan_port),
&ovs_vxlan_vport_ops, parms);
if (IS_ERR(vport))
return vport;
vxlan_port = vxlan_vport(vport);
- vxlan_port->dst_port = htons(dst_port);
strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
- err = vxlan_socket_init(vxlan_port, net);
- if (err)
- goto error_free;
+ vh = vxlan_handler_add(net, htons(dst_port), vxlan_rcv,
+ vport, OVS_VXLAN_RCV_PRIORITY, true);
+ if (IS_ERR(vh)) {
+ ovs_vport_free(vport);
+ return (void *)vh;
+ }
+ vxlan_port->vh = vh;
- list_add_tail_rcu(&vxlan_port->list, &vxlan_ports);
return vport;
-error_free:
- ovs_vport_free(vport);
error:
return ERR_PTR(err);
}
static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
{
- if (unlikely(!OVS_CB(skb)->tun_key))
- return -EINVAL;
+ struct vxlan_port *vxlan_port = vxlan_vport(vport);
+ __be16 dst_port = inet_sport(vxlan_port->vh->vs->sock->sk);
+ struct net *net = ovs_dp_get_net(vport->dp);
+ struct rtable *rt;
+ __be16 src_port;
+ __be32 saddr;
+ __be16 df;
+ int port_min;
+ int port_max;
+ int err;
+
+ if (unlikely(!OVS_CB(skb)->tun_key)) {
+ err = -EINVAL;
+ goto error;
+ }
- return ovs_tnl_send(vport, skb, IPPROTO_UDP,
- VXLAN_HLEN, vxlan_build_header);
+ forward_ip_summed(skb, true);
+
+ /* Route lookup */
+ saddr = OVS_CB(skb)->tun_key->ipv4_src;
+ rt = find_route(ovs_dp_get_net(vport->dp),
+ &saddr,
+ OVS_CB(skb)->tun_key->ipv4_dst,
+ IPPROTO_UDP,
+ OVS_CB(skb)->tun_key->ipv4_tos,
+ skb_get_mark(skb));
+ if (IS_ERR(rt)) {
+ err = PTR_ERR(rt);
+ goto error;
+ }
+
+ df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
+ htons(IP_DF) : 0;
+
+ skb->local_df = 1;
+
+ inet_get_local_port_range(&port_min, &port_max);
+ src_port = vxlan_src_port(port_min, port_max, skb);
+
+ err = vxlan_xmit_skb(net, vxlan_port->vh, rt, skb,
+ saddr, OVS_CB(skb)->tun_key->ipv4_dst,
+ OVS_CB(skb)->tun_key->ipv4_tos,
+ OVS_CB(skb)->tun_key->ipv4_ttl, df,
+ src_port, dst_port,
+ htonl(be64_to_cpu(OVS_CB(skb)->tun_key->tun_id) << 8));
+ if (err < 0)
+ ip_rt_put(rt);
+error:
+ return err;
}
static const char *vxlan_get_name(const struct vport *vport)