[ovs-dev] [PATCH v3] Introduce odp_flow_key_to_mask() API

Ben Pfaff blp at nicira.com
Wed Aug 21 00:05:21 UTC 2013


On Tue, Aug 20, 2013 at 10:40:50AM -0700, gyang at nicira.com wrote:
> From: Guolin Yang <gyang at nicira.com>
> 
>     With megaflow support, there is API to convert mask to nlattr key based
>     format. This change introduces API to do the reverse conversion. We
>     leverage the existing odp_flow_key_to_flow() API to resue the code.
> 
> Signed-off-by: Guolin Yang <gyang at nicira.com>

There were some warnings from Clang:

    ../lib/odp-util.c:2913:33: error: comparison of constant 255 with expression of type 'enum ovs_key_attr' is always true [-Werror,-Wtautological-constant-out-of-range-compare]
        if (is_mask && expected_bit != 0xff) {
                   ~~~~~~~~~~~~ ^  ~~~~

and from sparse:

    ../lib/odp-util.c:2696:17: warning: restricted __be16 degrades to integer
    ../lib/odp-util.c:2737:44: warning: restricted __be16 degrades to integer
    ../lib/odp-util.c:2822:58: warning: restricted __be16 degrades to integer
    ../lib/odp-util.c:2903:33: warning: restricted __be16 degrades to integer
    ../lib/odp-util.c:3095:24: warning: incorrect type in assignment (different base types)
    ../lib/odp-util.c:3095:24:    expected restricted __be16 [usertype] vlan_tci
    ../lib/odp-util.c:3095:24:    got int

I fixed these, and some style rules and simplifications, by folding in
the following incremental.  I applied the full patch at the end of
this email.

diff --git a/AUTHORS b/AUTHORS
index fc665b3..e94150f 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -36,6 +36,7 @@ FUJITA Tomonori         fujita.tomonori at lab.ntt.co.jp
 Gaetano Catalli         gaetano.catalli at gmail.com
 Giuseppe Lettieri       g.lettieri at iet.unipi.it
 Glen Gibb               grg at stanford.edu
+Guolin Yang             gyang at nicira.com
 Gurucharan Shetty       gshetty at nicira.com
 Henry Mai               hmai at nicira.com
 Hao Zheng               hzheng at nicira.com
diff --git a/lib/odp-util.c b/lib/odp-util.c
index 4373bbd..b351c71 100644
--- a/lib/odp-util.c
+++ b/lib/odp-util.c
@@ -2680,7 +2680,7 @@ check_expectations(uint64_t present_attrs, int out_of_range_attr,
 static bool
 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
                 uint64_t present_attrs, uint64_t *expected_attrs,
-                struct flow *flow, struct flow *src_flow)
+                struct flow *flow, const struct flow *src_flow)
 {
     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
     bool is_mask = flow != src_flow;
@@ -2692,8 +2692,8 @@ parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
                         ntohs(flow->dl_type));
             return false;
         }
-        if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN && 
-            flow->dl_type != 0xffff) {
+        if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
+            flow->dl_type != htons(0xffff)) {
             return false;
         }
         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
@@ -2701,11 +2701,8 @@ parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
         if (!is_mask) {
             flow->dl_type = htons(FLOW_DL_TYPE_NONE);
         } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
-            /*
-             * see comments odp_flow_key_from_flow__
-             */
-            VLOG_ERR_RL(&rl, "Alway expect mask for non ethernet II "
-                        "frame");
+            /* See comments in odp_flow_key_from_flow__(). */
+            VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
             return false;
         }
     }
@@ -2717,12 +2714,12 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
                   uint64_t present_attrs, int out_of_range_attr,
                   uint64_t expected_attrs, struct flow *flow,
                   const struct nlattr *key, size_t key_len,
-		  struct flow *src_flow)
+		  const struct flow *src_flow)
 {
     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
     bool is_mask = src_flow != flow;
-    const uint8_t *checkStart = NULL;
-    size_t checkLen = 0;
+    const void *check_start = NULL;
+    size_t check_len = 0;
     enum ovs_key_attr expected_bit = 0xff;
 
     if (eth_type_mpls(src_flow->dl_type)) {
@@ -2737,14 +2734,12 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
         } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
             flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
 
-            if (flow->mpls_lse != 0 && flow->dl_type != 0xffff) {
+            if (flow->mpls_lse != 0 && flow->dl_type != htons(0xffff)) {
                 return ODP_FIT_ERROR;
             }
             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
             if (flow->mpls_lse) {
-                /*
-                 * do we need to set the following? XXX
-                 */
+                /* XXX Is this needed? */
                 flow->mpls_depth = 0xffff;
             }
         }
@@ -2764,8 +2759,8 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
             flow->nw_ttl = ipv4_key->ipv4_ttl;
             if (is_mask) {
 	        flow->nw_frag = ipv4_key->ipv4_frag;
-                checkStart = (const uint8_t *)ipv4_key;
-                checkLen = sizeof (struct ovs_key_ipv4);
+                check_start = ipv4_key;
+                check_len = sizeof *ipv4_key;
                 expected_bit = OVS_KEY_ATTR_IPV4;
             } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
                 return ODP_FIT_ERROR;
@@ -2787,8 +2782,8 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
             flow->nw_ttl = ipv6_key->ipv6_hlimit;
             if (is_mask) {
 	        flow->nw_frag = ipv6_key->ipv6_frag;
-                checkStart = (const uint8_t *)ipv6_key;
-                checkLen = sizeof (struct ovs_key_ipv6);
+                check_start = ipv6_key;
+                check_len = sizeof *ipv6_key;
                 expected_bit = OVS_KEY_ATTR_IPV6;
             } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
                 return ODP_FIT_ERROR;
@@ -2815,8 +2810,8 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
 
             if (is_mask) {
-                checkStart = (const uint8_t *)arp_key;
-                checkLen = sizeof (struct ovs_key_arp);
+                check_start = arp_key;
+                check_len = sizeof *arp_key;
                 expected_bit = OVS_KEY_ATTR_ARP;
             }
         }
@@ -2824,14 +2819,15 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
         goto done;
     }
     if (is_mask) {
-        if (!is_all_zeros(checkStart, checkLen) && flow->dl_type != 0xffff) {
+        if (!is_all_zeros(check_start, check_len) &&
+            flow->dl_type != htons(0xffff)) {
             return ODP_FIT_ERROR;
         } else {
             expected_attrs |= UINT64_C(1) << expected_bit;
         }
     }
 
-    expected_bit = 0xff; /* initialize to invalid bit*/
+    expected_bit = OVS_KEY_ATTR_UNSPEC;
     if (src_flow->nw_proto == IPPROTO_TCP
         && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
             src_flow->dl_type == htons(ETH_TYPE_IPV6))
@@ -2903,9 +2899,9 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
                     if (is_mask) {
-                        if (!is_all_zeros((const uint8_t *)nd_key,
-                                          sizeof (struct ovs_key_nd)) &&
-                            flow->tp_src != 0xffff) {
+                        if (!is_all_zeros((const uint8_t *) nd_key,
+                                          sizeof *nd_key) &&
+                            flow->tp_src != htons(0xffff)) {
                             return ODP_FIT_ERROR;
                         } else {
                             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
@@ -2915,7 +2911,7 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
             }
         }
     }
-    if (is_mask && expected_bit != 0xff) {
+    if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
         if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
             return ODP_FIT_ERROR;
         } else {
@@ -2934,7 +2930,7 @@ parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
                    uint64_t present_attrs, int out_of_range_attr,
                    uint64_t expected_attrs, struct flow *flow,
                    const struct nlattr *key, size_t key_len,
-                   struct flow *src_flow)
+                   const struct flow *src_flow)
 {
     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
     bool is_mask = src_flow != flow;
@@ -3006,24 +3002,9 @@ parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
     return MAX(fitness, encap_fitness);
 }
 
-/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
- * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
- * 'key' fits our expectations for what a flow key should contain.
- *
- * The 'in_port' will be the datapath's understanding of the port.  The
- * caller will need to translate with odp_port_to_ofp_port() if the
- * OpenFlow port is needed.
- *
- * This function doesn't take the packet itself as an argument because none of
- * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
- * it is always possible to infer which additional attribute(s) should appear
- * by looking at the attributes for lower-level protocols, e.g. if the network
- * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
- * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
- * must be absent. */
 static enum odp_key_fitness
 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
-                       struct flow *flow, struct flow *src_flow)
+                       struct flow *flow, const struct flow *src_flow)
 {
     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
     uint64_t expected_attrs;
@@ -3097,7 +3078,7 @@ odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
                                   expected_attrs, flow, key, key_len, src_flow);
     }
     if (is_mask) {
-        flow->vlan_tci = 0xffff;
+        flow->vlan_tci = htons(0xffff);
         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
             flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
@@ -3107,6 +3088,21 @@ odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
                              expected_attrs, flow, key, key_len, src_flow);
 }
 
+/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
+ * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
+ * 'key' fits our expectations for what a flow key should contain.
+ *
+ * The 'in_port' will be the datapath's understanding of the port.  The
+ * caller will need to translate with odp_port_to_ofp_port() if the
+ * OpenFlow port is needed.
+ *
+ * This function doesn't take the packet itself as an argument because none of
+ * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
+ * it is always possible to infer which additional attribute(s) should appear
+ * by looking at the attributes for lower-level protocols, e.g. if the network
+ * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
+ * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
+ * must be absent. */
 enum odp_key_fitness
 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
                      struct flow *flow)
@@ -3114,9 +3110,13 @@ odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
    return odp_flow_key_to_flow__(key, key_len, flow, flow);
 }
 
+/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
+ * structure in 'mask'.  'flow' must be a previously translated flow
+ * corresponding to 'mask'.  Returns an ODP_FIT_* value that indicates how well
+ * 'key' fits our expectations for what a flow key should contain. */
 enum odp_key_fitness
 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
-                     struct flow *mask, struct flow *flow)
+                     struct flow *mask, const struct flow *flow)
 {
    return odp_flow_key_to_flow__(key, key_len, mask, flow);
 }
diff --git a/lib/odp-util.h b/lib/odp-util.h
index cc18dca..192cfa0 100644
--- a/lib/odp-util.h
+++ b/lib/odp-util.h
@@ -123,7 +123,8 @@ enum odp_key_fitness {
 enum odp_key_fitness odp_flow_key_to_flow(const struct nlattr *, size_t,
                                           struct flow *);
 enum odp_key_fitness odp_flow_key_to_mask(const struct nlattr *key, size_t len,
-                                          struct flow *mask, struct flow *flow);
+                                          struct flow *mask,
+                                          const struct flow *flow);
 const char *odp_key_fitness_to_string(enum odp_key_fitness);
 
 void commit_odp_tunnel_action(const struct flow *, struct flow *base,

--8<--------------------------cut here-------------------------->8--

From: Guolin Yang <gyang at nicira.com>
Date: Tue, 20 Aug 2013 10:40:50 -0700
Subject: [PATCH] odp-util: New function odp_flow_key_to_mask().

With megaflow support, there is API to convert mask to nlattr key based
format.  This change introduces API to do the reverse conversion.  We
leverage the existing odp_flow_key_to_flow() API to reuse the code.

Signed-off-by: Guolin Yang <gyang at nicira.com>
Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 AUTHORS        |    1 +
 lib/odp-util.c |  325 ++++++++++++++++++++++++++++++++++++++++----------------
 lib/odp-util.h |    3 +
 3 files changed, 238 insertions(+), 91 deletions(-)

diff --git a/AUTHORS b/AUTHORS
index fc665b3..e94150f 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -36,6 +36,7 @@ FUJITA Tomonori         fujita.tomonori at lab.ntt.co.jp
 Gaetano Catalli         gaetano.catalli at gmail.com
 Giuseppe Lettieri       g.lettieri at iet.unipi.it
 Glen Gibb               grg at stanford.edu
+Guolin Yang             gyang at nicira.com
 Gurucharan Shetty       gshetty at nicira.com
 Henry Mai               hmai at nicira.com
 Hao Zheng               hzheng at nicira.com
diff --git a/lib/odp-util.c b/lib/odp-util.c
index a09042e..b351c71 100644
--- a/lib/odp-util.c
+++ b/lib/odp-util.c
@@ -2680,20 +2680,31 @@ check_expectations(uint64_t present_attrs, int out_of_range_attr,
 static bool
 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
                 uint64_t present_attrs, uint64_t *expected_attrs,
-                struct flow *flow)
+                struct flow *flow, const struct flow *src_flow)
 {
     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+    bool is_mask = flow != src_flow;
 
     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
-        if (ntohs(flow->dl_type) < 1536) {
+        if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
                         ntohs(flow->dl_type));
             return false;
         }
+        if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
+            flow->dl_type != htons(0xffff)) {
+            return false;
+        }
         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
     } else {
-        flow->dl_type = htons(FLOW_DL_TYPE_NONE);
+        if (!is_mask) {
+            flow->dl_type = htons(FLOW_DL_TYPE_NONE);
+        } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
+            /* See comments in odp_flow_key_from_flow__(). */
+            VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
+            return false;
+        }
     }
     return true;
 }
@@ -2702,20 +2713,41 @@ static enum odp_key_fitness
 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
                   uint64_t present_attrs, int out_of_range_attr,
                   uint64_t expected_attrs, struct flow *flow,
-                  const struct nlattr *key, size_t key_len)
+                  const struct nlattr *key, size_t key_len,
+		  const struct flow *src_flow)
 {
     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
-
-    if (eth_type_mpls(flow->dl_type)) {
-        expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
-
-        if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
-            return ODP_FIT_TOO_LITTLE;
+    bool is_mask = src_flow != flow;
+    const void *check_start = NULL;
+    size_t check_len = 0;
+    enum ovs_key_attr expected_bit = 0xff;
+
+    if (eth_type_mpls(src_flow->dl_type)) {
+	if (!is_mask) {
+	    expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
+
+	    if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
+	        return ODP_FIT_TOO_LITTLE;
+	    }
+	    flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
+	    flow->mpls_depth++;
+        } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
+            flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
+
+            if (flow->mpls_lse != 0 && flow->dl_type != htons(0xffff)) {
+                return ODP_FIT_ERROR;
+            }
+            expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
+            if (flow->mpls_lse) {
+                /* XXX Is this needed? */
+                flow->mpls_depth = 0xffff;
+            }
+        }
+        goto done;
+    } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
+        if (!is_mask) {
+            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
         }
-        flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
-        flow->mpls_depth++;
-    } else if (flow->dl_type == htons(ETH_TYPE_IP)) {
-        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
             const struct ovs_key_ipv4 *ipv4_key;
 
@@ -2725,12 +2757,19 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
             flow->nw_proto = ipv4_key->ipv4_proto;
             flow->nw_tos = ipv4_key->ipv4_tos;
             flow->nw_ttl = ipv4_key->ipv4_ttl;
-            if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
+            if (is_mask) {
+	        flow->nw_frag = ipv4_key->ipv4_frag;
+                check_start = ipv4_key;
+                check_len = sizeof *ipv4_key;
+                expected_bit = OVS_KEY_ATTR_IPV4;
+            } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
                 return ODP_FIT_ERROR;
             }
         }
-    } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
-        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
+    } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
+        if (!is_mask) {
+            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
+        }
         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
             const struct ovs_key_ipv6 *ipv6_key;
 
@@ -2741,20 +2780,27 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
             flow->nw_proto = ipv6_key->ipv6_proto;
             flow->nw_tos = ipv6_key->ipv6_tclass;
             flow->nw_ttl = ipv6_key->ipv6_hlimit;
-            if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
+            if (is_mask) {
+	        flow->nw_frag = ipv6_key->ipv6_frag;
+                check_start = ipv6_key;
+                check_len = sizeof *ipv6_key;
+                expected_bit = OVS_KEY_ATTR_IPV6;
+            } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
                 return ODP_FIT_ERROR;
             }
         }
-    } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
-               flow->dl_type == htons(ETH_TYPE_RARP)) {
-        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
+    } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
+               src_flow->dl_type == htons(ETH_TYPE_RARP)) {
+        if (!is_mask) {
+            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
+        }
         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
             const struct ovs_key_arp *arp_key;
 
             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
             flow->nw_src = arp_key->arp_sip;
             flow->nw_dst = arp_key->arp_tip;
-            if (arp_key->arp_op & htons(0xff00)) {
+            if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
                             "key", ntohs(arp_key->arp_op));
                 return ODP_FIT_ERROR;
@@ -2762,58 +2808,88 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
             flow->nw_proto = ntohs(arp_key->arp_op);
             memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
+
+            if (is_mask) {
+                check_start = arp_key;
+                check_len = sizeof *arp_key;
+                expected_bit = OVS_KEY_ATTR_ARP;
+            }
+        }
+    } else {
+        goto done;
+    }
+    if (is_mask) {
+        if (!is_all_zeros(check_start, check_len) &&
+            flow->dl_type != htons(0xffff)) {
+            return ODP_FIT_ERROR;
+        } else {
+            expected_attrs |= UINT64_C(1) << expected_bit;
         }
     }
 
-    if (flow->nw_proto == IPPROTO_TCP
-        && (flow->dl_type == htons(ETH_TYPE_IP) ||
-            flow->dl_type == htons(ETH_TYPE_IPV6))
-        && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
-        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
+    expected_bit = OVS_KEY_ATTR_UNSPEC;
+    if (src_flow->nw_proto == IPPROTO_TCP
+        && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
+            src_flow->dl_type == htons(ETH_TYPE_IPV6))
+        && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
+        if (!is_mask) {
+            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
+        }
         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
             const struct ovs_key_tcp *tcp_key;
 
             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
             flow->tp_src = tcp_key->tcp_src;
             flow->tp_dst = tcp_key->tcp_dst;
+            expected_bit = OVS_KEY_ATTR_TCP;
+        }
+    } else if (src_flow->nw_proto == IPPROTO_UDP
+               && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
+                   src_flow->dl_type == htons(ETH_TYPE_IPV6))
+               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
+        if (!is_mask) {
+            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
         }
-    } else if (flow->nw_proto == IPPROTO_UDP
-               && (flow->dl_type == htons(ETH_TYPE_IP) ||
-                   flow->dl_type == htons(ETH_TYPE_IPV6))
-               && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
-        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
             const struct ovs_key_udp *udp_key;
 
             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
             flow->tp_src = udp_key->udp_src;
             flow->tp_dst = udp_key->udp_dst;
+            expected_bit = OVS_KEY_ATTR_UDP;
+        }
+    } else if (src_flow->nw_proto == IPPROTO_ICMP
+               && src_flow->dl_type == htons(ETH_TYPE_IP)
+               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
+        if (!is_mask) {
+            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
         }
-    } else if (flow->nw_proto == IPPROTO_ICMP
-               && flow->dl_type == htons(ETH_TYPE_IP)
-               && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
-        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
             const struct ovs_key_icmp *icmp_key;
 
             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
             flow->tp_src = htons(icmp_key->icmp_type);
             flow->tp_dst = htons(icmp_key->icmp_code);
+            expected_bit = OVS_KEY_ATTR_ICMP;
+        }
+    } else if (src_flow->nw_proto == IPPROTO_ICMPV6
+               && src_flow->dl_type == htons(ETH_TYPE_IPV6)
+               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
+        if (!is_mask) {
+            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
         }
-    } else if (flow->nw_proto == IPPROTO_ICMPV6
-               && flow->dl_type == htons(ETH_TYPE_IPV6)
-               && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
-        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
             const struct ovs_key_icmpv6 *icmpv6_key;
 
             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
             flow->tp_src = htons(icmpv6_key->icmpv6_type);
             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
-
-            if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
-                flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
-                expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
+            expected_bit = OVS_KEY_ATTR_ICMPV6;
+            if (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
+                src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
+                if (!is_mask) {
+                    expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
+                }
                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
                     const struct ovs_key_nd *nd_key;
 
@@ -2822,11 +2898,28 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
                            sizeof flow->nd_target);
                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
+                    if (is_mask) {
+                        if (!is_all_zeros((const uint8_t *) nd_key,
+                                          sizeof *nd_key) &&
+                            flow->tp_src != htons(0xffff)) {
+                            return ODP_FIT_ERROR;
+                        } else {
+                            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
+                        }
+                    }
                 }
             }
         }
     }
+    if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
+        if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
+            return ODP_FIT_ERROR;
+        } else {
+            expected_attrs |= UINT64_C(1) << expected_bit;
+        }
+    }
 
+done:
     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
                               key, key_len);
 }
@@ -2836,9 +2929,11 @@ static enum odp_key_fitness
 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
                    uint64_t present_attrs, int out_of_range_attr,
                    uint64_t expected_attrs, struct flow *flow,
-                   const struct nlattr *key, size_t key_len)
+                   const struct nlattr *key, size_t key_len,
+                   const struct flow *src_flow)
 {
     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+    bool is_mask = src_flow != flow;
 
     const struct nlattr *encap
         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
@@ -2848,33 +2943,47 @@ parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
     ovs_be16 tci;
 
     /* Calculate fitness of outer attributes. */
-    expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
-                       (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
+    if (!is_mask) {
+        expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
+                          (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
+    } else {
+        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
+            expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
+        }
+        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
+            expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
+        }
+    }
     fitness = check_expectations(present_attrs, out_of_range_attr,
                                  expected_attrs, key, key_len);
 
     /* Get the VLAN TCI value. */
-    if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
+    if (!is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
         return ODP_FIT_TOO_LITTLE;
-    }
-    tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
-    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;
-        }
+    } else {
+        tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
+        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;
+	    }
+        }
+        /* 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;
-    } 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;
     }
-
-    /* Set vlan_tci.
-     * Remove the TPID from dl_type since it's not the real Ethertype.  */
-    flow->vlan_tci = tci;
-    flow->dl_type = htons(0);
-
     /* 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)) {
@@ -2882,39 +2991,26 @@ parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
     }
     expected_attrs = 0;
 
-    if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
+    if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
         return ODP_FIT_ERROR;
     }
     encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
-                                      expected_attrs, flow, key, key_len);
+                                      expected_attrs, flow, key, key_len,
+                                      src_flow);
 
     /* The overall fitness is the worse of the outer and inner attributes. */
     return MAX(fitness, encap_fitness);
 }
 
-/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
- * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
- * 'key' fits our expectations for what a flow key should contain.
- *
- * The 'in_port' will be the datapath's understanding of the port.  The
- * caller will need to translate with odp_port_to_ofp_port() if the
- * OpenFlow port is needed.
- *
- * This function doesn't take the packet itself as an argument because none of
- * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
- * it is always possible to infer which additional attribute(s) should appear
- * by looking at the attributes for lower-level protocols, e.g. if the network
- * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
- * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
- * must be absent. */
-enum odp_key_fitness
-odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
-                     struct flow *flow)
+static enum odp_key_fitness
+odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
+                       struct flow *flow, const struct flow *src_flow)
 {
     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
     uint64_t expected_attrs;
     uint64_t present_attrs;
     int out_of_range_attr;
+    bool is_mask = src_flow != flow;
 
     memset(flow, 0, sizeof *flow);
 
@@ -2951,7 +3047,7 @@ odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
         flow->in_port.odp_port
             = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
-    } else {
+    } else if (!is_mask) {
         flow->in_port.odp_port = ODPP_NONE;
     }
 
@@ -2962,20 +3058,67 @@ odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
         memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
         memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
+        if (is_mask) {
+            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
+        }
+    }
+    if (!is_mask) {
+        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
     }
-    expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
 
     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
-    if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
+    if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
+        src_flow)) {
         return ODP_FIT_ERROR;
     }
 
-    if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
+    if ((is_mask && (src_flow->vlan_tci & htons(VLAN_CFI))) ||
+        (!is_mask && src_flow->dl_type == htons(ETH_TYPE_VLAN))) {
         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
-                                  expected_attrs, flow, key, key_len);
+                                  expected_attrs, flow, key, key_len, src_flow);
+    }
+    if (is_mask) {
+        flow->vlan_tci = htons(0xffff);
+        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
+            flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
+            expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
+        }
     }
     return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
-                             expected_attrs, flow, key, key_len);
+                             expected_attrs, flow, key, key_len, src_flow);
+}
+
+/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
+ * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
+ * 'key' fits our expectations for what a flow key should contain.
+ *
+ * The 'in_port' will be the datapath's understanding of the port.  The
+ * caller will need to translate with odp_port_to_ofp_port() if the
+ * OpenFlow port is needed.
+ *
+ * This function doesn't take the packet itself as an argument because none of
+ * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
+ * it is always possible to infer which additional attribute(s) should appear
+ * by looking at the attributes for lower-level protocols, e.g. if the network
+ * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
+ * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
+ * must be absent. */
+enum odp_key_fitness
+odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
+                     struct flow *flow)
+{
+   return odp_flow_key_to_flow__(key, key_len, flow, flow);
+}
+
+/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
+ * structure in 'mask'.  'flow' must be a previously translated flow
+ * corresponding to 'mask'.  Returns an ODP_FIT_* value that indicates how well
+ * 'key' fits our expectations for what a flow key should contain. */
+enum odp_key_fitness
+odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
+                     struct flow *mask, const struct flow *flow)
+{
+   return odp_flow_key_to_flow__(key, key_len, mask, flow);
 }
 
 /* Returns 'fitness' as a string, for use in debug messages. */
diff --git a/lib/odp-util.h b/lib/odp-util.h
index 0c40f38..192cfa0 100644
--- a/lib/odp-util.h
+++ b/lib/odp-util.h
@@ -122,6 +122,9 @@ enum odp_key_fitness {
 };
 enum odp_key_fitness odp_flow_key_to_flow(const struct nlattr *, size_t,
                                           struct flow *);
+enum odp_key_fitness odp_flow_key_to_mask(const struct nlattr *key, size_t len,
+                                          struct flow *mask,
+                                          const struct flow *flow);
 const char *odp_key_fitness_to_string(enum odp_key_fitness);
 
 void commit_odp_tunnel_action(const struct flow *, struct flow *base,
-- 
1.7.10.4




More information about the dev mailing list