Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / lib / odp-util.c
index 5cc2757..af464a0 100644 (file)
@@ -79,6 +79,8 @@ odp_action_len(uint16_t type)
     case OVS_ACTION_ATTR_POP_VLAN: return 0;
     case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
     case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
+    case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
+    case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
     case OVS_ACTION_ATTR_SET: return -2;
     case OVS_ACTION_ATTR_SAMPLE: return -2;
 
@@ -118,6 +120,8 @@ ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
     case OVS_KEY_ATTR_ARP: return "arp";
     case OVS_KEY_ATTR_ND: return "nd";
     case OVS_KEY_ATTR_MPLS: return "mpls";
+    case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
+    case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
 
     case __OVS_KEY_ATTR_MAX:
     default:
@@ -383,6 +387,26 @@ format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
     }
 }
 
+static void
+format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
+{
+    ds_put_format(ds, "recirc(%"PRIu32")", recirc_id);
+}
+
+static void
+format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
+{
+    ds_put_format(ds, "hash(");
+
+    if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
+        ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
+    } else {
+        ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
+                      hash_act->hash_alg);
+    }
+    ds_put_format(ds, ")");
+}
+
 static void
 format_odp_action(struct ds *ds, const struct nlattr *a)
 {
@@ -405,6 +429,12 @@ format_odp_action(struct ds *ds, const struct nlattr *a)
     case OVS_ACTION_ATTR_USERSPACE:
         format_odp_userspace_action(ds, a);
         break;
+    case OVS_ACTION_ATTR_RECIRC:
+        format_odp_recirc_action(ds, nl_attr_get_u32(a));
+        break;
+    case OVS_ACTION_ATTR_HASH:
+        format_odp_hash_action(ds, nl_attr_get(a));
+        break;
     case OVS_ACTION_ATTR_SET:
         ds_put_cstr(ds, "set(");
         format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
@@ -586,7 +616,7 @@ parse_odp_action(const char *s, const struct simap *port_names,
             ofpbuf_init(&buf, 16);
             end = ofpbuf_put_hex(&buf, &s[n], NULL);
             if (end[0] == ')' && end[1] == ')') {
-                odp_put_userspace_action(pid, buf.data, buf.size, actions);
+                odp_put_userspace_action(pid, ofpbuf_data(&buf), ofpbuf_size(&buf), actions);
                 ofpbuf_uninit(&buf);
                 return (end + 2) - s;
             }
@@ -696,7 +726,7 @@ odp_actions_from_string(const char *s, const struct simap *port_names,
         return 0;
     }
 
-    old_size = actions->size;
+    old_size = ofpbuf_size(actions);
     for (;;) {
         int retval;
 
@@ -707,7 +737,7 @@ odp_actions_from_string(const char *s, const struct simap *port_names,
 
         retval = parse_odp_action(s, port_names, actions);
         if (retval < 0 || !strchr(delimiters, s[retval])) {
-            actions->size = old_size;
+            ofpbuf_set_size(actions, old_size);
             return -retval;
         }
         s += retval;
@@ -730,6 +760,8 @@ odp_flow_key_attr_len(uint16_t type)
     case OVS_KEY_ATTR_ENCAP: return -2;
     case OVS_KEY_ATTR_PRIORITY: return 4;
     case OVS_KEY_ATTR_SKB_MARK: return 4;
+    case OVS_KEY_ATTR_DP_HASH: return 4;
+    case OVS_KEY_ATTR_RECIRC_ID: return 4;
     case OVS_KEY_ATTR_TUNNEL: return -2;
     case OVS_KEY_ATTR_IN_PORT: return 4;
     case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
@@ -988,22 +1020,22 @@ format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
         expected_len = odp_flow_key_attr_len(nl_attr_type(a));
         if (expected_len != -2) {
             bool bad_key_len = nl_attr_get_size(a) != expected_len;
-            bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len;
+            bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
 
             if (bad_key_len || bad_mask_len) {
                 if (bad_key_len) {
                     ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
-                                  nl_attr_get_size(a),
-                                  odp_flow_key_attr_len(nl_attr_type(a)));
+                                  nl_attr_get_size(a), expected_len);
                 }
                 format_generic_odp_key(a, ds);
-                if (bad_mask_len) {
+                if (ma) {
                     ds_put_char(ds, '/');
-                    ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
-                                  nl_attr_get_size(ma),
-                                  odp_flow_key_attr_len(nl_attr_type(ma)));
+                    if (bad_mask_len) {
+                        ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
+                                      nl_attr_get_size(ma), expected_len);
+                    }
+                    format_generic_odp_key(ma, ds);
                 }
-                format_generic_odp_key(ma, ds);
                 ds_put_char(ds, ')');
                 return;
             }
@@ -1025,6 +1057,8 @@ format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
 
     case OVS_KEY_ATTR_PRIORITY:
     case OVS_KEY_ATTR_SKB_MARK:
+    case OVS_KEY_ATTR_DP_HASH:
+    case OVS_KEY_ATTR_RECIRC_ID:
         ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
         if (!is_exact) {
             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
@@ -1285,7 +1319,7 @@ format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
         } else {
             const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
 
-            ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
+            ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
                           ntohs(sctp_key->sctp_src), ntohs(sctp_key->sctp_dst));
         }
         break;
@@ -1386,7 +1420,6 @@ format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
         }
         break;
     }
-
     case OVS_KEY_ATTR_UNSPEC:
     case __OVS_KEY_ATTR_MAX:
     default:
@@ -1420,7 +1453,7 @@ generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
         nl_msg_end_nested(ofp, nested_mask);
     }
 
-    return ofp->base;
+    return ofpbuf_base(ofp);
 }
 
 /* Appends to 'ds' a string representation of the 'key_len' bytes of
@@ -1618,6 +1651,36 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
         }
     }
 
+    {
+        uint32_t recirc_id;
+        int n = -1;
+
+        if (ovs_scan(s, "recirc_id(%"SCNi32")%n", &recirc_id, &n)) {
+            nl_msg_put_u32(key, OVS_KEY_ATTR_RECIRC_ID, recirc_id);
+            nl_msg_put_u32(mask, OVS_KEY_ATTR_RECIRC_ID, UINT32_MAX);
+            return n;
+        }
+    }
+
+    {
+        uint32_t dp_hash;
+        uint32_t dp_hash_mask;
+        int n = -1;
+
+        if (mask && ovs_scan(s, "dp_hash(%"SCNi32"/%"SCNi32")%n", &dp_hash,
+                             &dp_hash_mask, &n)) {
+            nl_msg_put_u32(key, OVS_KEY_ATTR_DP_HASH, dp_hash);
+            nl_msg_put_u32(mask, OVS_KEY_ATTR_DP_HASH, dp_hash_mask);
+            return n;
+        } else if (ovs_scan(s, "dp_hash(%"SCNi32")%n", &dp_hash, &n)) {
+            nl_msg_put_u32(key, OVS_KEY_ATTR_DP_HASH, dp_hash);
+            if (mask) {
+                nl_msg_put_u32(mask, OVS_KEY_ATTR_DP_HASH, UINT32_MAX);
+            }
+            return n;
+        }
+    }
+
     {
         uint64_t tun_id, tun_id_mask;
         struct flow_tnl tun_key, tun_key_mask;
@@ -2378,7 +2441,7 @@ int
 odp_flow_from_string(const char *s, const struct simap *port_names,
                      struct ofpbuf *key, struct ofpbuf *mask)
 {
-    const size_t old_size = key->size;
+    const size_t old_size = ofpbuf_size(key);
     for (;;) {
         int retval;
 
@@ -2389,7 +2452,7 @@ odp_flow_from_string(const char *s, const struct simap *port_names,
 
         retval = parse_odp_key_mask_attr(s, port_names, key, mask);
         if (retval < 0) {
-            key->size = old_size;
+            ofpbuf_set_size(key, old_size);
             return -retval;
         }
         s += retval;
@@ -2418,29 +2481,33 @@ ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
 }
 
 static void
-odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
-                         const struct flow *flow, odp_port_t odp_in_port,
-                         size_t max_mpls_depth)
+odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
+                         const struct flow *mask, odp_port_t odp_in_port,
+                         size_t max_mpls_depth, bool export_mask)
 {
-    bool is_mask;
     struct ovs_key_ethernet *eth_key;
     size_t encap;
-
-    /* We assume that if 'data' and 'flow' are not the same, we should
-     * treat 'data' as a mask. */
-    is_mask = (data != flow);
+    const struct flow *data = export_mask ? mask : flow;
 
     nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
 
-    if (flow->tunnel.ip_dst || is_mask) {
+    if (flow->tunnel.ip_dst || export_mask) {
         tun_key_to_attr(buf, &data->tunnel);
     }
 
     nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
 
+    if (data->recirc_id || (mask && mask->recirc_id)) {
+        nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
+    }
+
+    if (data->dp_hash || (mask && mask->dp_hash)) {
+        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'
      * is not the magical value "ODPP_NONE". */
-    if (is_mask || odp_in_port != ODPP_NONE) {
+    if (export_mask || odp_in_port != ODPP_NONE) {
         nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
     }
 
@@ -2450,7 +2517,7 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
     memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
 
     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
-        if (is_mask) {
+        if (export_mask) {
             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
         } else {
             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
@@ -2476,7 +2543,7 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
          *  <none>   0xffff   Any non-Ethernet II frame (except valid
          *                    802.3 SNAP packet with valid eth_type).
          */
-        if (is_mask) {
+        if (export_mask) {
             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
         }
         goto unencap;
@@ -2494,7 +2561,7 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
         ipv4_key->ipv4_proto = data->nw_proto;
         ipv4_key->ipv4_tos = data->nw_tos;
         ipv4_key->ipv4_ttl = data->nw_ttl;
-        ipv4_key->ipv4_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
+        ipv4_key->ipv4_frag = export_mask ? ovs_to_odp_frag_mask(data->nw_frag)
                                       : ovs_to_odp_frag(data->nw_frag);
     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
         struct ovs_key_ipv6 *ipv6_key;
@@ -2507,7 +2574,7 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
         ipv6_key->ipv6_proto = data->nw_proto;
         ipv6_key->ipv6_tclass = data->nw_tos;
         ipv6_key->ipv6_hlimit = data->nw_ttl;
-        ipv6_key->ipv6_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
+        ipv6_key->ipv6_frag = export_mask ? ovs_to_odp_frag_mask(data->nw_frag)
                                       : ovs_to_odp_frag(data->nw_frag);
     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
                flow->dl_type == htons(ETH_TYPE_RARP)) {
@@ -2579,7 +2646,7 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
             if (flow->tp_dst == htons(0) &&
                 (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
                  flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) &&
-                (!is_mask || (data->tp_src == htons(0xffff) &&
+                (!export_mask || (data->tp_src == htons(0xffff) &&
                               data->tp_dst == htons(0xffff)))) {
 
                 struct ovs_key_nd *nd_key;
@@ -2610,9 +2677,9 @@ unencap:
  * capable of being expanded to allow for that much space. */
 void
 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
-                       odp_port_t odp_in_port)
+                       const struct flow *mask, odp_port_t odp_in_port)
 {
-    odp_flow_key_from_flow__(buf, flow, flow, odp_in_port, SIZE_MAX);
+    odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, false);
 }
 
 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
@@ -2628,8 +2695,8 @@ odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
                        const struct flow *flow, uint32_t odp_in_port_mask,
                        size_t max_mpls_depth)
 {
-    odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask),
-                             max_mpls_depth);
+    odp_flow_key_from_flow__(buf, flow, mask,
+                             u32_to_odp(odp_in_port_mask), max_mpls_depth, true);
 }
 
 /* Generate ODP flow key from the given packet metadata */
@@ -2673,13 +2740,24 @@ odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
             continue;
         }
 
-        if (type == OVS_KEY_ATTR_PRIORITY) {
+        switch (type) {
+        case OVS_KEY_ATTR_RECIRC_ID:
+            md->recirc_id = nl_attr_get_u32(nla);
+            wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
+            break;
+        case OVS_KEY_ATTR_DP_HASH:
+            md->dp_hash = nl_attr_get_u32(nla);
+            wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
+            break;
+        case OVS_KEY_ATTR_PRIORITY:
             md->skb_priority = nl_attr_get_u32(nla);
             wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
-        } else if (type == OVS_KEY_ATTR_SKB_MARK) {
+            break;
+        case OVS_KEY_ATTR_SKB_MARK:
             md->pkt_mark = nl_attr_get_u32(nla);
             wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
-        } else if (type == OVS_KEY_ATTR_TUNNEL) {
+            break;
+        case OVS_KEY_ATTR_TUNNEL: {
             enum odp_key_fitness res;
 
             res = odp_tun_key_from_attr(nla, &md->tunnel);
@@ -2688,9 +2766,14 @@ odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
             } else if (res == ODP_FIT_PERFECT) {
                 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
             }
-        } else if (type == OVS_KEY_ATTR_IN_PORT) {
+            break;
+        }
+        case OVS_KEY_ATTR_IN_PORT:
             md->in_port.odp_port = nl_attr_get_odp_port(nla);
             wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
+            break;
+        default:
+            break;
         }
 
         if (!wanted_attrs) {
@@ -3146,7 +3229,6 @@ parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
     enum odp_key_fitness encap_fitness;
     enum odp_key_fitness fitness;
-    ovs_be16 tci;
 
     /* Calculate fitness of outer attributes. */
     if (!is_mask) {
@@ -3163,35 +3245,32 @@ parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
     fitness = check_expectations(present_attrs, out_of_range_attr,
                                  expected_attrs, key, key_len);
 
-    /* Get the VLAN TCI value. */
-    if (!is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
-        return ODP_FIT_TOO_LITTLE;
+    /* Set vlan_tci.
+     * Remove the TPID from dl_type since it's not the real Ethertype.  */
+    flow->dl_type = htons(0);
+    flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
+                      ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
+                      : htons(0));
+    if (!is_mask) {
+        if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
+            return ODP_FIT_TOO_LITTLE;
+        } else if (flow->vlan_tci == htons(0)) {
+            /* Corner case for a truncated 802.1Q header. */
+            if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
+                return ODP_FIT_TOO_MUCH;
+            }
+            return fitness;
+        } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
+            VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
+                        "but CFI bit is not set", ntohs(flow->vlan_tci));
+            return ODP_FIT_ERROR;
+        }
     } else {
-        tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
-               ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
-               : htons(0));
-        if (!is_mask) {
-            if (tci == htons(0)) {
-                /* Corner case for a truncated 802.1Q header. */
-                if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
-                    return ODP_FIT_TOO_MUCH;
-                }
-                return fitness;
-            } else if (!(tci & htons(VLAN_CFI))) {
-                VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
-                            "but CFI bit is not set", ntohs(tci));
-                return ODP_FIT_ERROR;
-            }
+        if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
+            return fitness;
         }
-        /* Set vlan_tci.
-         * Remove the TPID from dl_type since it's not the real Ethertype.  */
-        flow->dl_type = htons(0);
-        flow->vlan_tci = tci;
     }
 
-    if (is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
-        return fitness;
-    }
     /* Now parse the encapsulated attributes. */
     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
                             attrs, &present_attrs, &out_of_range_attr)) {
@@ -3230,6 +3309,18 @@ odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
     expected_attrs = 0;
 
     /* Metadata. */
+    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
+        flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
+        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
+    } else if (is_mask) {
+        /* Always exact match recirc_id when datapath does not sepcify it. */
+        flow->recirc_id = UINT32_MAX;
+    }
+
+    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
+        flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
+        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
+    }
     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
@@ -3364,7 +3455,7 @@ odp_put_userspace_action(uint32_t pid,
     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
     if (userdata) {
-        userdata_ofs = odp_actions->size + NLA_HDRLEN;
+        userdata_ofs = ofpbuf_size(odp_actions) + NLA_HDRLEN;
 
         /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
          * module before Linux 3.10 required the userdata to be exactly 8 bytes