ipfix: Support tunnel information for Flow IPFIX.
[cascardo/ovs.git] / lib / odp-util.c
index 2937f3b..48c05f5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "byte-order.h"
 #include "coverage.h"
 #include "dpif.h"
-#include "dynamic-string.h"
+#include "openvswitch/dynamic-string.h"
 #include "flow.h"
 #include "netlink.h"
-#include "ofpbuf.h"
+#include "openvswitch/ofpbuf.h"
 #include "packets.h"
 #include "simap.h"
 #include "timeval.h"
@@ -311,11 +311,13 @@ format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
                 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
                               ",collector_set_id=%"PRIu32
                               ",obs_domain_id=%"PRIu32
-                              ",obs_point_id=%"PRIu32")",
+                              ",obs_point_id=%"PRIu32
+                              ",output_port=%"PRIu32")",
                               cookie.flow_sample.probability,
                               cookie.flow_sample.collector_set_id,
                               cookie.flow_sample.obs_domain_id,
-                              cookie.flow_sample.obs_point_id);
+                              cookie.flow_sample.obs_point_id,
+                              cookie.flow_sample.output_odp_port);
             } else if (userdata_len >= sizeof cookie.ipfix
                        && cookie.type == USER_ACTION_COOKIE_IPFIX) {
                 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
@@ -386,13 +388,13 @@ static void
 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
             const struct ovs_key_mpls *mpls_mask, int n)
 {
-    if (n == 1) {
-        ovs_be32 key = mpls_key->mpls_lse;
+    for (int i = 0; i < n; i++) {
+        ovs_be32 key = mpls_key[i].mpls_lse;
 
         if (mpls_mask == NULL) {
             format_mpls_lse(ds, key);
         } else {
-            ovs_be32 mask = mpls_mask->mpls_lse;
+            ovs_be32 mask = mpls_mask[i].mpls_lse;
 
             ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
                           mpls_lse_to_label(key), mpls_lse_to_label(mask),
@@ -400,19 +402,9 @@ format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
                           mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
                           mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
         }
-    } else {
-        int i;
-
-        for (i = 0; i < n; i++) {
-            ds_put_format(ds, "lse%d=%#"PRIx32,
-                          i, ntohl(mpls_key[i].mpls_lse));
-            if (mpls_mask) {
-                ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
-            }
-            ds_put_char(ds, ',');
-        }
-        ds_chomp(ds, ',');
+        ds_put_char(ds, ',');
     }
+    ds_chomp(ds, ',');
 }
 
 static void
@@ -475,7 +467,7 @@ format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
                       IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
                       ip->ip_proto, ip->ip_tos,
                       ip->ip_ttl,
-                      ip->ip_frag_off);
+                      ntohs(ip->ip_frag_off));
         l4 = (ip + 1);
     } else {
         const struct ip6_hdr *ip6;
@@ -909,11 +901,14 @@ parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
     void *user_data = NULL;
     size_t user_data_size = 0;
     bool include_actions = false;
+    int res;
 
     if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
         return -EINVAL;
     }
 
+    ofpbuf_init(&buf, 16);
+
     {
         uint32_t output;
         uint32_t probability;
@@ -940,8 +935,6 @@ parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
             user_data_size = sizeof cookie.sflow;
         } else if (ovs_scan(&s[n], ",slow_path(%n",
                             &n1)) {
-            int res;
-
             n += n1;
             cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
             cookie.slow_path.unused = 0;
@@ -951,7 +944,7 @@ parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
                                   &cookie.slow_path.reason,
                                   SLOW_PATH_REASON_MASK, NULL);
             if (res < 0 || s[n + res] != ')') {
-                return res;
+                goto out;
             }
             n += res + 1;
 
@@ -960,9 +953,11 @@ parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
         } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
                             "collector_set_id=%"SCNi32","
                             "obs_domain_id=%"SCNi32","
-                            "obs_point_id=%"SCNi32")%n",
+                            "obs_point_id=%"SCNi32","
+                            "output_port=%"SCNi32")%n",
                             &probability, &collector_set_id,
-                            &obs_domain_id, &obs_point_id, &n1)) {
+                            &obs_domain_id, &obs_point_id,
+                            &output, &n1)) {
             n += n1;
 
             cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
@@ -970,6 +965,7 @@ parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
             cookie.flow_sample.collector_set_id = collector_set_id;
             cookie.flow_sample.obs_domain_id = obs_domain_id;
             cookie.flow_sample.obs_point_id = obs_point_id;
+            cookie.flow_sample.output_odp_port = u32_to_odp(output);
             user_data = &cookie;
             user_data_size = sizeof cookie.flow_sample;
         } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
@@ -984,10 +980,10 @@ parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
             char *end;
 
             n += n1;
-            ofpbuf_init(&buf, 16);
             end = ofpbuf_put_hex(&buf, &s[n], NULL);
             if (end[0] != ')') {
-                return -EINVAL;
+                res = -EINVAL;
+                goto out;
             }
             user_data = buf.data;
             user_data_size = buf.size;
@@ -1009,15 +1005,18 @@ parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
                      &tunnel_out_port, &n1)) {
             odp_put_userspace_action(pid, user_data, user_data_size,
                                      tunnel_out_port, include_actions, actions);
-            return n + n1;
+            res = n + n1;
         } else if (s[n] == ')') {
             odp_put_userspace_action(pid, user_data, user_data_size,
                                      ODPP_NONE, include_actions, actions);
-            return n + 1;
+            res = n + 1;
+        } else {
+            res = -EINVAL;
         }
     }
-
-    return -EINVAL;
+out:
+    ofpbuf_uninit(&buf);
+    return res;
 }
 
 static int
@@ -1060,16 +1059,18 @@ ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
 
     if (eth->eth_type == htons(ETH_TYPE_IP)) {
         /* IPv4 */
+        uint16_t ip_frag_off;
         if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
                           ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
                           IP_SCAN_ARGS(&sip),
                           IP_SCAN_ARGS(&dip),
                           &ip->ip_proto, &ip->ip_tos,
-                          &ip->ip_ttl, &ip->ip_frag_off)) {
+                          &ip->ip_ttl, &ip_frag_off)) {
             return -EINVAL;
         }
         put_16aligned_be32(&ip->ip_src, sip);
         put_16aligned_be32(&ip->ip_dst, dip);
+        ip->ip_frag_off = htons(ip_frag_off);
         ip_len = sizeof *ip;
     } else {
         char sip6_s[IPV6_SCAN_LEN + 1];
@@ -1496,7 +1497,7 @@ find_end:
             nl_msg_put_unspec(actions, OVS_CT_ATTR_MARK, &ct_mark,
                               sizeof(ct_mark));
         }
-        if (!ovs_u128_is_zero(&ct_label.mask)) {
+        if (!ovs_u128_is_zero(ct_label.mask)) {
             nl_msg_put_unspec(actions, OVS_CT_ATTR_LABELS, &ct_label,
                               sizeof ct_label);
         }
@@ -2085,10 +2086,9 @@ odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
 void
 odp_portno_names_destroy(struct hmap *portno_names)
 {
-    struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
-    HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
-                        hmap_node, portno_names) {
-        hmap_remove(portno_names, &odp_portno_names->hmap_node);
+    struct odp_portno_names *odp_portno_names;
+
+    HMAP_FOR_EACH_POP (odp_portno_names, hmap_node, portno_names) {
         free(odp_portno_names->name);
         free(odp_portno_names);
     }
@@ -2370,7 +2370,7 @@ format_odp_tun_vxlan_opt(const struct nlattr *attr,
         case OVS_VXLAN_EXT_GBP: {
             uint32_t key = nl_attr_get_u32(a);
             ovs_be16 id, id_mask;
-            uint8_t flags, flags_mask;
+            uint8_t flags, flags_mask = 0;
 
             id = htons(key & 0xFFFF);
             flags = (key >> 16) & 0xFF;
@@ -2617,6 +2617,10 @@ odp_ct_state_to_string(uint32_t flag)
         return "rel";
     case OVS_CS_F_INVALID:
         return "inv";
+    case OVS_CS_F_SRC_NAT:
+        return "snat";
+    case OVS_CS_F_DST_NAT:
+        return "dnat";
     default:
         return NULL;
     }
@@ -2951,12 +2955,12 @@ static void
 format_u128(struct ds *ds, const ovs_u128 *key, const ovs_u128 *mask,
             bool verbose)
 {
-    if (verbose || (mask && !ovs_u128_is_zero(mask))) {
+    if (verbose || (mask && !ovs_u128_is_zero(*mask))) {
         ovs_be128 value;
 
         value = hton128(*key);
         ds_put_hex(ds, &value, sizeof value);
-        if (mask && !(ovs_u128_is_ones(mask))) {
+        if (mask && !(ovs_u128_is_ones(*mask))) {
             value = hton128(*mask);
             ds_put_char(ds, '/');
             ds_put_hex(ds, &value, sizeof value);
@@ -3387,6 +3391,12 @@ ovs_to_odp_ct_state(uint8_t state)
     if (state & CS_TRACKED) {
         odp |= OVS_CS_F_TRACKED;
     }
+    if (state & CS_SRC_NAT) {
+        odp |= OVS_CS_F_SRC_NAT;
+    }
+    if (state & CS_DST_NAT) {
+        odp |= OVS_CS_F_DST_NAT;
+    }
 
     return odp;
 }
@@ -3414,6 +3424,12 @@ odp_to_ovs_ct_state(uint32_t flags)
     if (flags & OVS_CS_F_TRACKED) {
         state |= CS_TRACKED;
     }
+    if (flags & OVS_CS_F_SRC_NAT) {
+        state |= CS_SRC_NAT;
+    }
+    if (flags & OVS_CS_F_DST_NAT) {
+        state |= CS_DST_NAT;
+    }
 
     return state;
 }
@@ -3903,12 +3919,9 @@ geneve_to_attr(struct ofpbuf *a, const void *data_)
         SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
 
 #define SCAN_PUT(ATTR, FUNC)                            \
-        if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
-            SCAN_PUT_ATTR(key, ATTR, skey, FUNC);       \
-            if (mask) {                                 \
-                SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
-            }                                           \
-        }
+        SCAN_PUT_ATTR(key, ATTR, skey, FUNC);           \
+        if (mask)                                       \
+            SCAN_PUT_ATTR(mask, ATTR, smask, FUNC);     \
 
 #define SCAN_END(ATTR)                                  \
         SCAN_FINISH();                                  \
@@ -3916,6 +3929,51 @@ geneve_to_attr(struct ofpbuf *a, const void *data_)
         return s - start;                               \
     }
 
+#define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT)       \
+    SCAN_IF(NAME);                              \
+        TYPE skey[CNT], smask[CNT];             \
+        memset(&skey, 0, sizeof skey);          \
+        memset(&smask, 0, sizeof smask);        \
+        int idx = 0, cnt = CNT;                 \
+        uint64_t fields = 0;                    \
+        do {                                    \
+            int field = 0;                      \
+            len = 0;
+
+/* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
+#define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD)                             \
+    if (strncmp(s, NAME, strlen(NAME)) == 0) {                          \
+        if (fields & (1UL << field)) {                                  \
+            fields = 0;                                                 \
+            if (++idx == cnt) {                                         \
+                break;                                                  \
+            }                                                           \
+        }                                                               \
+        s += strlen(NAME);                                              \
+        SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
+        fields |= 1UL << field;                                         \
+        continue;                                                       \
+    }                                                                   \
+    field++;
+
+#define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT)                    \
+    nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
+
+#define SCAN_PUT_ARRAY(ATTR, CNT)                        \
+    SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT);       \
+    if (mask) {                                      \
+        SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
+    }
+
+#define SCAN_END_ARRAY(ATTR)             \
+        SCAN_FINISH();                   \
+        if (idx == cnt) {                \
+            return -EINVAL;              \
+        }                                \
+        SCAN_PUT_ARRAY(ATTR, idx + 1);   \
+        return s - start;                \
+    }
+
 #define SCAN_END_SINGLE(ATTR)                           \
         SCAN_FINISH_SINGLE();                           \
         SCAN_PUT(ATTR, NULL);                           \
@@ -3998,12 +4056,12 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
 
     SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
 
-    SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
-        SCAN_FIELD("label=", mpls_label, mpls_lse);
-        SCAN_FIELD("tc=", mpls_tc, mpls_lse);
-        SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
-        SCAN_FIELD("bos=", mpls_bos, mpls_lse);
-    } SCAN_END(OVS_KEY_ATTR_MPLS);
+    SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls, FLOW_MAX_MPLS_LABELS) {
+        SCAN_FIELD_ARRAY("label=", mpls_label, mpls_lse);
+        SCAN_FIELD_ARRAY("tc=", mpls_tc, mpls_lse);
+        SCAN_FIELD_ARRAY("ttl=", mpls_ttl, mpls_lse);
+        SCAN_FIELD_ARRAY("bos=", mpls_bos, mpls_lse);
+    } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS);
 
     SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
         SCAN_FIELD("src=", ipv4, ipv4_src);
@@ -4221,10 +4279,10 @@ odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
         nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
     }
 
-    /* Add an ingress port attribute if this is a mask or 'odp_in_port'
+    /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
      * is not the magical value "ODPP_NONE". */
-    if (export_mask || parms->odp_in_port != ODPP_NONE) {
-        nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, parms->odp_in_port);
+    if (export_mask || flow->in_port.odp_port != ODPP_NONE) {
+        nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, data->in_port.odp_port);
     }
 
     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
@@ -4342,8 +4400,12 @@ odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
             if (flow->tp_dst == htons(0)
                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
                     || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
-                && (!export_mask || (data->tp_src == htons(0xffff)
-                                     && data->tp_dst == htons(0xffff)))) {
+                /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
+                 * type and code are 8 bits wide.  Therefore, an exact match
+                 * looks like htons(0xff), not htons(0xffff).  See
+                 * xlate_wc_finish() for details. */
+                && (!export_mask || (data->tp_src == htons(0xff)
+                                     && data->tp_dst == htons(0xff)))) {
 
                 struct ovs_key_nd *nd_key;
 
@@ -4407,7 +4469,7 @@ odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
         if (md->ct_mark) {
             nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark);
         }
-        if (!ovs_u128_is_zero(&md->ct_label)) {
+        if (!ovs_u128_is_zero(md->ct_label)) {
             nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label,
                               sizeof(md->ct_label));
         }
@@ -4508,8 +4570,7 @@ uint32_t
 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
 {
     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
-    return hash_words(ALIGNED_CAST(const uint32_t *, key),
-                      key_len / sizeof(uint32_t), 0);
+    return hash_bytes32(ALIGNED_CAST(const uint32_t *, key), key_len, 0);
 }
 
 static void
@@ -4888,9 +4949,14 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
                     flow->arp_sha = nd_key->nd_sll;
                     flow->arp_tha = nd_key->nd_tll;
                     if (is_mask) {
+                        /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
+                         * ICMP type and code are 8 bits wide.  Therefore, an
+                         * exact match looks like htons(0xff), not
+                         * htons(0xffff).  See xlate_wc_finish() for details.
+                         * */
                         if (!is_all_zeros(nd_key, sizeof *nd_key) &&
-                            (flow->tp_src != htons(0xffff) ||
-                             flow->tp_dst != htons(0xffff))) {
+                            (flow->tp_src != htons(0xff) ||
+                             flow->tp_dst != htons(0xff))) {
                             return ODP_FIT_ERROR;
                         } else {
                             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
@@ -5136,6 +5202,26 @@ odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
    return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow, false);
 }
 
+static enum odp_key_fitness
+odp_flow_key_to_mask__(const struct nlattr *mask_key, size_t mask_key_len,
+                       const struct nlattr *flow_key, size_t flow_key_len,
+                       struct flow_wildcards *mask,
+                       const struct flow *src_flow,
+                       bool udpif)
+{
+    if (mask_key_len) {
+        return odp_flow_key_to_flow__(mask_key, mask_key_len,
+                                      flow_key, flow_key_len,
+                                      &mask->masks, src_flow, udpif);
+
+    } else {
+        /* A missing mask means that the flow should be exact matched.
+         * Generate an appropriate exact wildcard for the flow. */
+        flow_wildcards_init_for_packet(mask, src_flow);
+
+        return ODP_FIT_PERFECT;
+    }
+}
 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
  * to a mask structure in 'mask'.  'flow' must be a previously translated flow
  * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
@@ -5144,10 +5230,11 @@ odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
 enum odp_key_fitness
 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
                      const struct nlattr *flow_key, size_t flow_key_len,
-                     struct flow *mask, const struct flow *flow)
+                     struct flow_wildcards *mask, const struct flow *flow)
 {
-   return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
-                                 mask, flow, false);
+    return odp_flow_key_to_mask__(mask_key, mask_key_len,
+                                  flow_key, flow_key_len,
+                                  mask, flow, false);
 }
 
 /* These functions are similar to their non-"_udpif" variants but output a
@@ -5169,10 +5256,12 @@ odp_flow_key_to_flow_udpif(const struct nlattr *key, size_t key_len,
 enum odp_key_fitness
 odp_flow_key_to_mask_udpif(const struct nlattr *mask_key, size_t mask_key_len,
                            const struct nlattr *flow_key, size_t flow_key_len,
-                           struct flow *mask, const struct flow *flow)
+                           struct flow_wildcards *mask,
+                           const struct flow *flow)
 {
-   return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
-                                 mask, flow, true);
+    return odp_flow_key_to_mask__(mask_key, mask_key_len,
+                                  flow_key, flow_key_len,
+                                  mask, flow, true);
 }
 
 /* Returns 'fitness' as a string, for use in debug messages. */
@@ -5635,12 +5724,18 @@ commit_set_icmp_action(const struct flow *flow, struct flow *base_flow,
     struct ovs_key_icmp key, mask, base;
     enum ovs_key_attr attr;
 
+    if (is_icmpv4(flow, NULL)) {
+        attr = OVS_KEY_ATTR_ICMP;
+    } else if (is_icmpv6(flow, NULL)) {
+        attr = OVS_KEY_ATTR_ICMPV6;
+    } else {
+        return 0;
+    }
+
     get_icmp_key(flow, &key);
     get_icmp_key(base_flow, &base);
     get_icmp_key(&wc->masks, &mask);
 
-    attr = flow->dl_type == htons(ETH_TYPE_IP) ? OVS_KEY_ATTR_ICMP
-                                               : OVS_KEY_ATTR_ICMPV6;
     if (commit(attr, false, &key, &base, &mask, sizeof key, odp_actions)) {
         put_icmp_key(&base, base_flow);
         put_icmp_key(&mask, &wc->masks);