tunnels: Don't initialize unnecessary packet metadata.
[cascardo/ovs.git] / lib / netdev-vport.c
index 0e0d791..a3394dd 100644 (file)
@@ -55,12 +55,18 @@ static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(60, 5);
 #define GENEVE_DST_PORT 6081
 #define VXLAN_DST_PORT 4789
 #define LISP_DST_PORT 4341
+#define STT_DST_PORT 7471
 
 #define VXLAN_HLEN   (sizeof(struct eth_header) +         \
                       sizeof(struct ip_header)  +         \
                       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 {
@@ -153,7 +159,7 @@ netdev_vport_needs_dst_port(const struct netdev *dev)
 
     return (class->get_config == get_tunnel_config &&
             (!strcmp("geneve", type) || !strcmp("vxlan", type) ||
-             !strcmp("lisp", type)));
+             !strcmp("lisp", type) || !strcmp("stt", type)) );
 }
 
 const char *
@@ -252,8 +258,12 @@ netdev_vport_construct(struct netdev *netdev_)
         dev->tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
     } else if (!strcmp(type, "lisp")) {
         dev->tnl_cfg.dst_port = htons(LISP_DST_PORT);
+    } else if (!strcmp(type, "stt")) {
+        dev->tnl_cfg.dst_port = htons(STT_DST_PORT);
     }
 
+    dev->tnl_cfg.dont_fragment = true;
+    dev->tnl_cfg.ttl = DEFAULT_TTL;
     return 0;
 }
 
@@ -427,7 +437,7 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args)
     struct smap_node *node;
 
     has_csum = strstr(type, "gre") || strstr(type, "geneve") ||
-               strstr(type, "vxlan");
+               strstr(type, "stt") || strstr(type, "vxlan");
     ipsec_mech_set = false;
     memset(&tnl_cfg, 0, sizeof tnl_cfg);
 
@@ -444,6 +454,10 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args)
         tnl_cfg.dst_port = htons(LISP_DST_PORT);
     }
 
+    if (!strcmp(type, "stt")) {
+        tnl_cfg.dst_port = htons(STT_DST_PORT);
+    }
+
     needs_dst_port = netdev_vport_needs_dst_port(dev_);
     tnl_cfg.ipsec = strstr(type, "ipsec");
     tnl_cfg.dont_fragment = true;
@@ -683,7 +697,8 @@ get_tunnel_config(const struct netdev *dev, struct smap *args)
 
         if ((!strcmp("geneve", type) && dst_port != GENEVE_DST_PORT) ||
             (!strcmp("vxlan", type) && dst_port != VXLAN_DST_PORT) ||
-            (!strcmp("lisp", type) && dst_port != LISP_DST_PORT)) {
+            (!strcmp("lisp", type) && dst_port != LISP_DST_PORT) ||
+            (!strcmp("stt", type) && dst_port != STT_DST_PORT)) {
             smap_add_format(args, "dst_port", "%d", dst_port);
         }
     }
@@ -876,6 +891,95 @@ push_ip_header(struct dp_packet *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;
+    }
+
+    if (udp->udp_csum) {
+        uint32_t csum = packet_csum_pseudoheader(dp_packet_l3(packet));
+
+        csum = csum_continue(csum, udp, dp_packet_size(packet) -
+                             ((const unsigned char *)udp -
+                              (const unsigned char *)dp_packet_l2(packet)));
+        if (csum_finish(csum)) {
+            return NULL;
+        }
+        tnl->flags |= FLOW_TNL_F_CSUM;
+    }
+
+    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_rss_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 struct ovs_action_push_tnl *data)
+{
+    struct udp_header *udp;
+    int ip_tot_size;
+
+    udp = push_ip_header(packet, data->header, data->header_len, &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));
+
+    if (udp->udp_csum) {
+        uint32_t csum = packet_csum_pseudoheader(ip_hdr(dp_packet_data(packet)));
+
+        csum = csum_continue(csum, udp,
+                             ip_tot_size - sizeof (struct ip_header));
+        udp->udp_csum = csum_finish(csum);
+
+        if (!udp->udp_csum) {
+            udp->udp_csum = htons(0xffff);
+        }
+    }
+}
+
+static void *
+udp_build_header(struct netdev_tunnel_config *tnl_cfg,
+                 const struct flow *tnl_flow,
+                 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;
+
+    if (tnl_flow->tunnel.flags & FLOW_TNL_F_CSUM) {
+        /* Write a value in now to mark that we should compute the checksum
+         * later. 0xffff is handy because it is transparent to the
+         * calculation. */
+        udp->udp_csum = htons(0xffff);
+    }
+
+    return udp + 1;
+}
+
 static int
 gre_header_len(ovs_be16 flags)
 {
@@ -948,52 +1052,46 @@ parse_gre_header(struct dp_packet *packet,
 }
 
 static void
-reset_tnl_md(struct pkt_metadata *md)
+pkt_metadata_init_tnl(struct pkt_metadata *md)
 {
-    memset(&md->tunnel, 0, sizeof(md->tunnel));
+    memset(md, 0, offsetof(struct pkt_metadata, tunnel.metadata));
+
+    /* If 'opt_map' is zero then none of the rest of the tunnel metadata
+     * will be read, so we can skip clearing it. */
+    md->tunnel.metadata.opt_map = 0;
 }
 
-static void
-gre_extract_md(struct dp_packet *packet)
+static int
+netdev_gre_pop_header(struct dp_packet *packet)
 {
     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);
+    pkt_metadata_init_tnl(md);
     if (hlen > dp_packet_size(packet)) {
-        return;
+        return EINVAL;
     }
 
     hlen = parse_gre_header(packet, tnl);
     if (hlen < 0) {
-        reset_tnl_md(md);
+        return -hlen;
     }
 
     dp_packet_reset_packet(packet, hlen);
-}
 
-static int
-netdev_gre_pop_header(struct netdev *netdev_ OVS_UNUSED,
-                      struct dp_packet **pkt, int cnt)
-{
-    int i;
-
-    for (i = 0; i < cnt; i++) {
-        gre_extract_md(pkt[i]);
-    }
     return 0;
 }
 
 static void
-netdev_gre_push_header__(struct dp_packet *packet,
-                         const void *header, int size)
+netdev_gre_push_header(struct dp_packet *packet,
+                       const struct ovs_action_push_tnl *data)
 {
     struct gre_base_hdr *greh;
     int ip_tot_size;
 
-    greh = push_ip_header(packet, header, size,  &ip_tot_size);
+    greh = push_ip_header(packet, data->header, data->header_len, &ip_tot_size);
 
     if (greh->flags & htons(GRE_CSUM)) {
         ovs_be16 *csum_opt = (ovs_be16 *) (greh + 1);
@@ -1001,21 +1099,6 @@ netdev_gre_push_header__(struct dp_packet *packet,
     }
 }
 
-static int
-netdev_gre_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_gre_push_header__(packets[i], data->header, data->header_len);
-        packets[i]->md = PKT_METADATA_INITIALIZER(u32_to_odp(data->out_port));
-    }
-    return 0;
-}
-
-
 static int
 netdev_gre_build_header(const struct netdev *netdev,
                         struct ovs_action_push_tnl *data,
@@ -1063,49 +1146,35 @@ netdev_gre_build_header(const struct netdev *netdev,
     return 0;
 }
 
-static void
-vxlan_extract_md(struct dp_packet *packet)
+static int
+netdev_vxlan_pop_header(struct dp_packet *packet)
 {
     struct pkt_metadata *md = &packet->md;
     struct flow_tnl *tnl = &md->tunnel;
-    struct udp_header *udp;
     struct vxlanhdr *vxh;
 
-    memset(md, 0, sizeof *md);
+    pkt_metadata_init_tnl(md);
     if (VXLAN_HLEN > dp_packet_size(packet)) {
-        return;
+        return EINVAL;
     }
 
-    udp = ip_extract_tnl_md(packet, tnl);
-    if (!udp) {
-        return;
+    vxh = udp_extract_tnl_md(packet, tnl);
+    if (!vxh) {
+        return EINVAL;
     }
-    vxh = (struct vxlanhdr *) (udp + 1);
 
     if (get_16aligned_be32(&vxh->vx_flags) != htonl(VXLAN_FLAGS) ||
        (get_16aligned_be32(&vxh->vx_vni) & htonl(0xff))) {
         VLOG_WARN_RL(&err_rl, "invalid vxlan flags=%#x vni=%#x\n",
                      ntohl(get_16aligned_be32(&vxh->vx_flags)),
                      ntohl(get_16aligned_be32(&vxh->vx_vni)));
-        reset_tnl_md(md);
-        return;
+        return EINVAL;
     }
-    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;
 
     dp_packet_reset_packet(packet, VXLAN_HLEN);
-}
 
-static int
-netdev_vxlan_pop_header(struct netdev *netdev_ OVS_UNUSED,
-                        struct dp_packet **pkt, int cnt)
-{
-    int i;
-
-    for (i = 0; i < cnt; i++) {
-        vxlan_extract_md(pkt[i]);
-    }
     return 0;
 }
 
@@ -1116,21 +1185,14 @@ netdev_vxlan_build_header(const struct netdev *netdev,
 {
     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;
+    vxh = udp_build_header(tnl_cfg, tnl_flow, data);
 
-    udp = (struct udp_header *) (ip + 1);
-    udp->udp_dst = tnl_cfg->dst_port;
-
-    vxh = (struct vxlanhdr *) (udp + 1);
     put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS));
     put_16aligned_be32(&vxh->vx_vni, htonl(ntohll(tnl_flow->tunnel.tun_id) << 8));
 
@@ -1140,44 +1202,92 @@ netdev_vxlan_build_header(const struct netdev *netdev,
     return 0;
 }
 
-static ovs_be16
-get_src_port(struct dp_packet *packet)
+static int
+netdev_geneve_pop_header(struct dp_packet *packet)
 {
-    uint32_t hash;
+    struct pkt_metadata *md = &packet->md;
+    struct flow_tnl *tnl = &md->tunnel;
+    struct genevehdr *gnh;
+    unsigned int hlen;
+    int err;
+
+    pkt_metadata_init_tnl(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 EINVAL;
+    }
 
-    hash = dp_packet_get_dp_hash(packet);
+    gnh = udp_extract_tnl_md(packet, tnl);
+    if (!gnh) {
+        return EINVAL;
+    }
 
-    return htons((((uint64_t) hash * (tnl_udp_port_max - tnl_udp_port_min)) >> 32) +
-                 tnl_udp_port_min);
-}
+    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));
+        return EINVAL;
+    }
 
-static void
-netdev_vxlan_push_header__(struct dp_packet *packet,
-                           const void *header, int size)
-{
-    struct udp_header *udp;
-    int ip_tot_size;
+    if (gnh->ver != 0) {
+        VLOG_WARN_RL(&err_rl, "unknown geneve version: %"PRIu8"\n", gnh->ver);
+        return EINVAL;
+    }
 
-    udp = push_ip_header(packet, header, size, &ip_tot_size);
+    if (gnh->proto_type != htons(ETH_TYPE_TEB)) {
+        VLOG_WARN_RL(&err_rl, "unknown geneve encapsulated protocol: %#x\n",
+                     ntohs(gnh->proto_type));
+        return EINVAL;
+    }
 
-    /* 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 */
+    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;
+
+    err = tun_metadata_from_geneve_header(gnh->options, gnh->opt_len * 4,
+                                          &tnl->metadata);
+    if (err) {
+        VLOG_WARN_RL(&err_rl, "invalid geneve options");
+        return err;
+    }
+
+    dp_packet_reset_packet(packet, hlen);
+
+    return 0;
 }
 
 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)
+netdev_geneve_build_header(const struct netdev *netdev,
+                           struct ovs_action_push_tnl *data,
+                           const struct flow *tnl_flow)
 {
-    int i;
+    struct netdev_vport *dev = netdev_vport_cast(netdev);
+    struct netdev_tunnel_config *tnl_cfg;
+    struct genevehdr *gnh;
+    int opt_len;
+    bool crit_opt;
 
-    for (i = 0; i < cnt; i++) {
-        netdev_vxlan_push_header__(packets[i],
-                                   data->header, VXLAN_HLEN);
-        packets[i]->md = PKT_METADATA_INITIALIZER(u32_to_odp(data->out_port));
-    }
+    /* XXX: RCUfy tnl_cfg. */
+    ovs_mutex_lock(&dev->mutex);
+    tnl_cfg = &dev->tnl_cfg;
+
+    gnh = udp_build_header(tnl_cfg, tnl_flow, data);
+
+    put_16aligned_be32(&gnh->vni, htonl(ntohll(tnl_flow->tunnel.tun_id) << 8));
+
+    ovs_mutex_unlock(&dev->mutex);
+
+    opt_len = tun_metadata_to_geneve_header(&tnl_flow->tunnel.metadata,
+                                            gnh->options, &crit_opt);
+
+    gnh->opt_len = opt_len / 4;
+    gnh->oam = !!(tnl_flow->tunnel.flags & FLOW_TNL_F_OAM);
+    gnh->critical = crit_opt ? 1 : 0;
+    gnh->proto_type = htons(ETH_TYPE_TEB);
+
+    data->header_len = GENEVE_BASE_HLEN + opt_len;
+    data->tnl_type = OVS_VPORT_TYPE_GENEVE;
     return 0;
 }
 
@@ -1310,7 +1420,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,
+                                            push_udp_header,
+                                            netdev_geneve_pop_header),
         TUNNEL_CLASS("gre", "gre_sys", netdev_gre_build_header,
                                        netdev_gre_push_header,
                                        netdev_gre_pop_header),
@@ -1318,9 +1430,10 @@ netdev_vport_tunnel_register(void)
         TUNNEL_CLASS("gre64", "gre64_sys", NULL,  NULL, NULL),
         TUNNEL_CLASS("ipsec_gre64", "gre64_sys", NULL, NULL, NULL),
         TUNNEL_CLASS("vxlan", "vxlan_sys", netdev_vxlan_build_header,
-                                           netdev_vxlan_push_header,
+                                           push_udp_header,
                                            netdev_vxlan_pop_header),
-        TUNNEL_CLASS("lisp", "lisp_sys", NULL, NULL, NULL)
+        TUNNEL_CLASS("lisp", "lisp_sys", NULL, NULL, NULL),
+        TUNNEL_CLASS("stt", "stt_sys", NULL, NULL, NULL),
     };
     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;