tunneling: Add userspace tunnel support for Geneve.
[cascardo/ovs.git] / lib / netdev-vport.c
index 91acabb..ed407dc 100644 (file)
@@ -29,6 +29,7 @@
 #include "daemon.h"
 #include "dirs.h"
 #include "dpif.h"
+#include "dp-packet.h"
 #include "dynamic-string.h"
 #include "flow.h"
 #include "hash.h"
 #include "list.h"
 #include "netdev-provider.h"
 #include "odp-netlink.h"
-#include "ofpbuf.h"
+#include "dp-packet.h"
 #include "ovs-router.h"
 #include "packets.h"
-#include "packet-dpif.h"
 #include "poll-loop.h"
 #include "route-table.h"
 #include "shash.h"
@@ -61,6 +61,11 @@ static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(60, 5);
                       sizeof(struct udp_header) +         \
                       sizeof(struct vxlanhdr))
 
+#define GENEVE_BASE_HLEN   (sizeof(struct eth_header) +         \
+                            sizeof(struct ip_header)  +         \
+                            sizeof(struct udp_header) +         \
+                            sizeof(struct genevehdr))
+
 #define DEFAULT_TTL 64
 
 struct netdev_vport {
@@ -426,7 +431,8 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args)
     struct netdev_tunnel_config tnl_cfg;
     struct smap_node *node;
 
-    has_csum = strstr(type, "gre");
+    has_csum = strstr(type, "gre") || strstr(type, "geneve") ||
+               strstr(type, "vxlan");
     ipsec_mech_set = false;
     memset(&tnl_cfg, 0, sizeof tnl_cfg);
 
@@ -532,6 +538,24 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args)
                    !strcmp(node->key, "in_key") ||
                    !strcmp(node->key, "out_key")) {
             /* Handled separately below. */
+        } else if (!strcmp(node->key, "exts")) {
+            char *str = xstrdup(node->value);
+            char *ext, *save_ptr = NULL;
+
+            tnl_cfg.exts = 0;
+
+            ext = strtok_r(str, ",", &save_ptr);
+            while (ext) {
+                if (!strcmp(type, "vxlan") && !strcmp(ext, "gbp")) {
+                    tnl_cfg.exts |= (1 << OVS_VXLAN_EXT_GBP);
+                } else {
+                    VLOG_WARN("%s: unknown extension '%s'", name, ext);
+                }
+
+                ext = strtok_r(NULL, ",", &save_ptr);
+            }
+
+            free(str);
         } else {
             VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
         }
@@ -593,9 +617,11 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args)
                                &tnl_cfg.out_key_flow);
 
     ovs_mutex_lock(&dev->mutex);
-    dev->tnl_cfg = tnl_cfg;
-    tunnel_check_status_change__(dev);
-    netdev_change_seq_changed(dev_);
+    if (memcmp(&dev->tnl_cfg, &tnl_cfg, sizeof tnl_cfg)) {
+        dev->tnl_cfg = tnl_cfg;
+        tunnel_check_status_change__(dev);
+        netdev_change_seq_changed(dev_);
+    }
     ovs_mutex_unlock(&dev->mutex);
 
     return 0;
@@ -768,9 +794,11 @@ set_patch_config(struct netdev *dev_, const struct smap *args)
     }
 
     ovs_mutex_lock(&dev->mutex);
-    free(dev->peer);
-    dev->peer = xstrdup(peer);
-    netdev_change_seq_changed(dev_);
+    if (!dev->peer || strcmp(dev->peer, peer)) {
+        free(dev->peer);
+        dev->peer = xstrdup(peer);
+        netdev_change_seq_changed(dev_);
+    }
     ovs_mutex_unlock(&dev->mutex);
 
     return 0;
@@ -804,13 +832,13 @@ gre_hdr(struct ip_header *ip)
 }
 
 static void *
-ip_extract_tnl_md(struct ofpbuf *packet, struct flow_tnl *tnl)
+ip_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl)
 {
     struct ip_header *nh;
     void *l4;
 
-    nh = ofpbuf_l3(packet);
-    l4 = ofpbuf_l4(packet);
+    nh = dp_packet_l3(packet);
+    l4 = dp_packet_l4(packet);
 
     if (!nh || !l4) {
         return NULL;
@@ -819,6 +847,7 @@ ip_extract_tnl_md(struct ofpbuf *packet, struct flow_tnl *tnl)
     tnl->ip_src = get_16aligned_be32(&nh->ip_src);
     tnl->ip_dst = get_16aligned_be32(&nh->ip_dst);
     tnl->ip_tos = nh->ip_tos;
+    tnl->ip_ttl = nh->ip_ttl;
 
     return l4;
 }
@@ -833,14 +862,14 @@ ip_extract_tnl_md(struct ofpbuf *packet, struct flow_tnl *tnl)
  *
  * Return pointer to the L4 header added to 'packet'. */
 static void *
-push_ip_header(struct ofpbuf *packet,
+push_ip_header(struct dp_packet *packet,
                const void *header, int size, int *ip_tot_size)
 {
     struct eth_header *eth;
     struct ip_header *ip;
 
-    eth = ofpbuf_push_uninit(packet, size);
-    *ip_tot_size = ofpbuf_size(packet) - sizeof (struct eth_header);
+    eth = dp_packet_push_uninit(packet, size);
+    *ip_tot_size = dp_packet_size(packet) - sizeof (struct eth_header);
 
     memcpy(eth, header, size);
     ip = ip_hdr(eth);
@@ -852,6 +881,65 @@ push_ip_header(struct ofpbuf *packet,
     return ip + 1;
 }
 
+static void *
+udp_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl)
+{
+    struct udp_header *udp;
+
+    udp = ip_extract_tnl_md(packet, tnl);
+    if (!udp) {
+        return NULL;
+    }
+
+    tnl->tp_src = udp->udp_src;
+    tnl->tp_dst = udp->udp_dst;
+
+    return udp + 1;
+}
+
+static ovs_be16
+get_src_port(struct dp_packet *packet)
+{
+    uint32_t hash;
+
+    hash = dp_packet_get_dp_hash(packet);
+
+    return htons((((uint64_t) hash * (tnl_udp_port_max - tnl_udp_port_min)) >> 32) +
+                 tnl_udp_port_min);
+}
+
+static void *
+push_udp_header(struct dp_packet *packet, const void *header, int size)
+{
+    struct udp_header *udp;
+    int ip_tot_size;
+
+    udp = push_ip_header(packet, header, size, &ip_tot_size);
+
+    /* set udp src port */
+    udp->udp_src = get_src_port(packet);
+    udp->udp_len = htons(ip_tot_size - sizeof (struct ip_header));
+    /* udp_csum is zero */
+
+    return udp + 1;
+}
+
+static void *
+udp_build_header(struct netdev_tunnel_config *tnl_cfg,
+                 struct ovs_action_push_tnl *data)
+{
+    struct ip_header *ip;
+    struct udp_header *udp;
+
+    ip = ip_hdr(data->header);
+    ip->ip_proto = IPPROTO_UDP;
+
+    udp = (struct udp_header *) (ip + 1);
+    udp->udp_dst = tnl_cfg->dst_port;
+
+    return udp + 1;
+}
+
 static int
 gre_header_len(ovs_be16 flags)
 {
@@ -871,7 +959,7 @@ gre_header_len(ovs_be16 flags)
 }
 
 static int
-parse_gre_header(struct ofpbuf *packet,
+parse_gre_header(struct dp_packet *packet,
                  struct flow_tnl *tnl)
 {
     const struct gre_base_hdr *greh;
@@ -887,8 +975,12 @@ parse_gre_header(struct ofpbuf *packet,
         return -EINVAL;
     }
 
+    if (greh->protocol != htons(ETH_TYPE_TEB)) {
+        return -EINVAL;
+    }
+
     hlen = gre_header_len(greh->flags);
-    if (hlen > ofpbuf_size(packet)) {
+    if (hlen > dp_packet_size(packet)) {
         return -EINVAL;
     }
 
@@ -896,9 +988,9 @@ parse_gre_header(struct ofpbuf *packet,
     if (greh->flags & htons(GRE_CSUM)) {
         ovs_be16 pkt_csum;
 
-        pkt_csum = csum(greh, ofpbuf_size(packet) -
+        pkt_csum = csum(greh, dp_packet_size(packet) -
                               ((const unsigned char *)greh -
-                               (const unsigned char *)ofpbuf_l2(packet)));
+                               (const unsigned char *)dp_packet_l2(packet)));
         if (pkt_csum) {
             return -EINVAL;
         }
@@ -926,16 +1018,15 @@ reset_tnl_md(struct pkt_metadata *md)
 }
 
 static void
-gre_extract_md(struct dpif_packet *dpif_pkt)
+gre_extract_md(struct dp_packet *packet)
 {
-    struct ofpbuf *packet = &dpif_pkt->ofpbuf;
-    struct pkt_metadata *md = &dpif_pkt->md;
+    struct pkt_metadata *md = &packet->md;
     struct flow_tnl *tnl = &md->tunnel;
     int hlen = sizeof(struct eth_header) +
                sizeof(struct ip_header) + 4;
 
     memset(md, 0, sizeof *md);
-    if (hlen > ofpbuf_size(packet)) {
+    if (hlen > dp_packet_size(packet)) {
         return;
     }
 
@@ -944,12 +1035,12 @@ gre_extract_md(struct dpif_packet *dpif_pkt)
         reset_tnl_md(md);
     }
 
-    ofpbuf_reset_packet(packet, hlen);
+    dp_packet_reset_packet(packet, hlen);
 }
 
 static int
 netdev_gre_pop_header(struct netdev *netdev_ OVS_UNUSED,
-                      struct dpif_packet **pkt, int cnt)
+                      struct dp_packet **pkt, int cnt)
 {
     int i;
 
@@ -960,7 +1051,7 @@ netdev_gre_pop_header(struct netdev *netdev_ OVS_UNUSED,
 }
 
 static void
-netdev_gre_push_header__(struct ofpbuf *packet,
+netdev_gre_push_header__(struct dp_packet *packet,
                          const void *header, int size)
 {
     struct gre_base_hdr *greh;
@@ -969,23 +1060,20 @@ netdev_gre_push_header__(struct ofpbuf *packet,
     greh = push_ip_header(packet, header, size,  &ip_tot_size);
 
     if (greh->flags & htons(GRE_CSUM)) {
-        ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
-
-        put_16aligned_be32(options,
-                           (OVS_FORCE ovs_be32) csum(greh, ip_tot_size - sizeof (struct ip_header)));
+        ovs_be16 *csum_opt = (ovs_be16 *) (greh + 1);
+        *csum_opt = csum(greh, ip_tot_size - sizeof (struct ip_header));
     }
 }
 
 static int
 netdev_gre_push_header(const struct netdev *netdev OVS_UNUSED,
-                       struct dpif_packet **packets, int cnt,
+                       struct dp_packet **packets, int cnt,
                        const struct ovs_action_push_tnl *data)
 {
     int i;
 
     for (i = 0; i < cnt; i++) {
-        netdev_gre_push_header__(&packets[i]->ofpbuf,
-                                   data->header, data->header_len);
+        netdev_gre_push_header__(packets[i], data->header, data->header_len);
         packets[i]->md = PKT_METADATA_INITIALIZER(u32_to_odp(data->out_port));
     }
     return 0;
@@ -994,7 +1082,8 @@ netdev_gre_push_header(const struct netdev *netdev OVS_UNUSED,
 
 static int
 netdev_gre_build_header(const struct netdev *netdev,
-                        struct ovs_action_push_tnl *data)
+                        struct ovs_action_push_tnl *data,
+                        const struct flow *tnl_flow)
 {
     struct netdev_vport *dev = netdev_vport_cast(netdev);
     struct netdev_tunnel_config *tnl_cfg;
@@ -1015,7 +1104,7 @@ netdev_gre_build_header(const struct netdev *netdev,
     greh->flags = 0;
 
     options = (ovs_16aligned_be32 *) (greh + 1);
-    if (tnl_cfg->csum) {
+    if (tnl_flow->tunnel.flags & FLOW_TNL_F_CSUM) {
         greh->flags |= htons(GRE_CSUM);
         put_16aligned_be32(options, 0);
         options++;
@@ -1024,7 +1113,7 @@ netdev_gre_build_header(const struct netdev *netdev,
     if (tnl_cfg->out_key_present) {
         greh->flags |= htons(GRE_KEY);
         put_16aligned_be32(options, (OVS_FORCE ovs_be32)
-                                    ((OVS_FORCE uint64_t) tnl_cfg->out_key >> 32));
+                                    ((OVS_FORCE uint64_t) tnl_flow->tunnel.tun_id >> 32));
         options++;
     }
 
@@ -1039,24 +1128,21 @@ netdev_gre_build_header(const struct netdev *netdev,
 }
 
 static void
-vxlan_extract_md(struct dpif_packet *dpif_pkt)
+vxlan_extract_md(struct dp_packet *packet)
 {
-    struct ofpbuf *packet = &dpif_pkt->ofpbuf;
-    struct pkt_metadata *md = &dpif_pkt->md;
+    struct pkt_metadata *md = &packet->md;
     struct flow_tnl *tnl = &md->tunnel;
-    struct udp_header *udp;
     struct vxlanhdr *vxh;
 
     memset(md, 0, sizeof *md);
-    if (VXLAN_HLEN > ofpbuf_size(packet)) {
+    if (VXLAN_HLEN > dp_packet_size(packet)) {
         return;
     }
 
-    udp = ip_extract_tnl_md(packet, tnl);
-    if (!udp) {
+    vxh = udp_extract_tnl_md(packet, tnl);
+    if (!vxh) {
         return;
     }
-    vxh = (struct vxlanhdr *) (udp + 1);
 
     if (get_16aligned_be32(&vxh->vx_flags) != htonl(VXLAN_FLAGS) ||
        (get_16aligned_be32(&vxh->vx_vni) & htonl(0xff))) {
@@ -1066,16 +1152,15 @@ vxlan_extract_md(struct dpif_packet *dpif_pkt)
         reset_tnl_md(md);
         return;
     }
-    tnl->tp_src = udp->udp_src;
-    tnl->tp_dst = udp->udp_dst;
     tnl->tun_id = htonll(ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
+    tnl->flags |= FLOW_TNL_F_KEY;
 
-    ofpbuf_reset_packet(packet, VXLAN_HLEN);
+    dp_packet_reset_packet(packet, VXLAN_HLEN);
 }
 
 static int
 netdev_vxlan_pop_header(struct netdev *netdev_ OVS_UNUSED,
-                        struct dpif_packet **pkt, int cnt)
+                        struct dp_packet **pkt, int cnt)
 {
     int i;
 
@@ -1087,27 +1172,21 @@ netdev_vxlan_pop_header(struct netdev *netdev_ OVS_UNUSED,
 
 static int
 netdev_vxlan_build_header(const struct netdev *netdev,
-                          struct ovs_action_push_tnl *data)
+                          struct ovs_action_push_tnl *data,
+                          const struct flow *tnl_flow)
 {
     struct netdev_vport *dev = netdev_vport_cast(netdev);
     struct netdev_tunnel_config *tnl_cfg;
-    struct ip_header *ip;
-    struct udp_header *udp;
     struct vxlanhdr *vxh;
 
     /* XXX: RCUfy tnl_cfg. */
     ovs_mutex_lock(&dev->mutex);
     tnl_cfg = &dev->tnl_cfg;
 
-    ip = ip_hdr(data->header);
-    ip->ip_proto = IPPROTO_UDP;
-
-    udp = (struct udp_header *) (ip + 1);
-    udp->udp_dst = tnl_cfg->dst_port;
+    vxh = udp_build_header(tnl_cfg, data);
 
-    vxh = (struct vxlanhdr *) (udp + 1);
     put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS));
-    put_16aligned_be32(&vxh->vx_vni, htonl(ntohll(tnl_cfg->out_key) << 8));
+    put_16aligned_be32(&vxh->vx_vni, htonl(ntohll(tnl_flow->tunnel.tun_id) << 8));
 
     ovs_mutex_unlock(&dev->mutex);
     data->header_len = VXLAN_HLEN;
@@ -1115,42 +1194,121 @@ netdev_vxlan_build_header(const struct netdev *netdev,
     return 0;
 }
 
-static ovs_be16
-get_src_port(struct dpif_packet *packet)
+static int
+netdev_vxlan_push_header(const struct netdev *netdev OVS_UNUSED,
+                         struct dp_packet **packets, int cnt,
+                         const struct ovs_action_push_tnl *data)
 {
-    uint32_t hash;
-
-    hash = dpif_packet_get_dp_hash(packet);
+    int i;
 
-    return htons((((uint64_t) hash * (tnl_udp_port_max - tnl_udp_port_min)) >> 32) +
-                 tnl_udp_port_min);
+    for (i = 0; i < cnt; i++) {
+        push_udp_header(packets[i], data->header, VXLAN_HLEN);
+        packets[i]->md = PKT_METADATA_INITIALIZER(u32_to_odp(data->out_port));
+    }
+    return 0;
 }
 
 static void
-netdev_vxlan_push_header__(struct dpif_packet *packet,
-                           const void *header, int size)
+geneve_extract_md(struct dp_packet *packet)
 {
-    struct udp_header *udp;
-    int ip_tot_size;
+    struct pkt_metadata *md = &packet->md;
+    struct flow_tnl *tnl = &md->tunnel;
+    struct genevehdr *gnh;
+    unsigned int hlen;
 
-    udp = push_ip_header(&packet->ofpbuf, header, size, &ip_tot_size);
+    memset(md, 0, sizeof *md);
+    if (GENEVE_BASE_HLEN > dp_packet_size(packet)) {
+        VLOG_WARN_RL(&err_rl, "geneve packet too small: min header=%u packet size=%u\n",
+                     (unsigned int)GENEVE_BASE_HLEN, dp_packet_size(packet));
+        return;
+    }
 
-    /* set udp src port */
-    udp->udp_src = get_src_port(packet);
-    udp->udp_len = htons(ip_tot_size - sizeof (struct ip_header));
-    /* udp_csum is zero */
+    gnh = udp_extract_tnl_md(packet, tnl);
+    if (!gnh) {
+        return;
+    }
+
+    hlen = GENEVE_BASE_HLEN + gnh->opt_len * 4;
+    if (hlen > dp_packet_size(packet)) {
+        VLOG_WARN_RL(&err_rl, "geneve packet too small: header len=%u packet size=%u\n",
+                     hlen, dp_packet_size(packet));
+        reset_tnl_md(md);
+        return;
+    }
+
+    if (gnh->ver != 0) {
+        VLOG_WARN_RL(&err_rl, "unknown geneve version: %"PRIu8"\n", gnh->ver);
+        reset_tnl_md(md);
+        return;
+    }
+
+    if (gnh->opt_len && gnh->critical) {
+        VLOG_WARN_RL(&err_rl, "unknown geneve critical options: %"PRIu8" bytes\n",
+                     gnh->opt_len * 4);
+        reset_tnl_md(md);
+        return;
+    }
+
+    if (gnh->proto_type != htons(ETH_TYPE_TEB)) {
+        VLOG_WARN_RL(&err_rl, "unknown geneve encapsulated protocol: %#x\n",
+                     ntohs(gnh->proto_type));
+        reset_tnl_md(md);
+        return;
+    }
+
+    tnl->flags |= gnh->oam ? FLOW_TNL_F_OAM : 0;
+    tnl->tun_id = htonll(ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
+    tnl->flags |= FLOW_TNL_F_KEY;
+
+    dp_packet_reset_packet(packet, hlen);
 }
 
 static int
-netdev_vxlan_push_header(const struct netdev *netdev OVS_UNUSED,
-                         struct dpif_packet **packets, int cnt,
-                         const struct ovs_action_push_tnl *data)
+netdev_geneve_pop_header(struct netdev *netdev_ OVS_UNUSED,
+                         struct dp_packet **pkt, int cnt)
+{
+    int i;
+
+    for (i = 0; i < cnt; i++) {
+        geneve_extract_md(pkt[i]);
+    }
+    return 0;
+}
+
+static int
+netdev_geneve_build_header(const struct netdev *netdev,
+                           struct ovs_action_push_tnl *data,
+                           const struct flow *tnl_flow)
+{
+    struct netdev_vport *dev = netdev_vport_cast(netdev);
+    struct netdev_tunnel_config *tnl_cfg;
+    struct genevehdr *gnh;
+
+    /* XXX: RCUfy tnl_cfg. */
+    ovs_mutex_lock(&dev->mutex);
+    tnl_cfg = &dev->tnl_cfg;
+
+    gnh = udp_build_header(tnl_cfg, data);
+
+    gnh->oam = !!(tnl_flow->tunnel.flags & FLOW_TNL_F_OAM);
+    gnh->proto_type = htons(ETH_TYPE_TEB);
+    put_16aligned_be32(&gnh->vni, htonl(ntohll(tnl_flow->tunnel.tun_id) << 8));
+
+    ovs_mutex_unlock(&dev->mutex);
+    data->header_len = GENEVE_BASE_HLEN;
+    data->tnl_type = OVS_VPORT_TYPE_GENEVE;
+    return 0;
+}
+
+static int
+netdev_geneve_push_header(const struct netdev *netdev OVS_UNUSED,
+                          struct dp_packet **packets, int cnt,
+                          const struct ovs_action_push_tnl *data)
 {
     int i;
 
     for (i = 0; i < cnt; i++) {
-        netdev_vxlan_push_header__(packets[i],
-                                   data->header, VXLAN_HLEN);
+        push_udp_header(packets[i], data->header, data->header_len);
         packets[i]->md = PKT_METADATA_INITIALIZER(u32_to_odp(data->out_port));
     }
     return 0;
@@ -1285,7 +1443,9 @@ netdev_vport_tunnel_register(void)
     /* The name of the dpif_port should be short enough to accomodate adding
      * a port number to the end if one is necessary. */
     static const struct vport_class vport_classes[] = {
-        TUNNEL_CLASS("geneve", "genev_sys", NULL, NULL, NULL),
+        TUNNEL_CLASS("geneve", "genev_sys", netdev_geneve_build_header,
+                                            netdev_geneve_push_header,
+                                            netdev_geneve_pop_header),
         TUNNEL_CLASS("gre", "gre_sys", netdev_gre_build_header,
                                        netdev_gre_push_header,
                                        netdev_gre_pop_header),