flow: Remove flow_to/from_match() in favor of cls_rule_to/from_match().
[openvswitch] / lib / flow.c
index 72202106b356d8925d32cd2b1986236971037073..7c3ad5118fb5c21d02d3988f2af6447dc5cbfb9d 100644 (file)
@@ -24,6 +24,7 @@
 #include "coverage.h"
 #include "dynamic-string.h"
 #include "hash.h"
+#include "ofp-util.h"
 #include "ofpbuf.h"
 #include "openflow/openflow.h"
 #include "openvswitch/datapath-protocol.h"
@@ -249,65 +250,6 @@ flow_extract_stats(const struct flow *flow, struct ofpbuf *packet,
     stats->n_packets = 1;
 }
 
-/* Extract 'flow' with 'wildcards' into the OpenFlow match structure
- * 'match'.  'flow_format' should be one of NXFF_*. */
-void
-flow_to_match(const struct flow *flow, uint32_t wildcards,
-              int flow_format, struct ofp_match *match)
-{
-    wildcards &= (flow_format == NXFF_TUN_ID_FROM_COOKIE ? OVSFW_ALL
-                  : OFPFW_ALL);
-    match->wildcards = htonl(wildcards);
-
-    match->in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
-                           : flow->in_port);
-    match->dl_vlan = flow->dl_vlan;
-    match->dl_vlan_pcp = flow->dl_vlan_pcp;
-    memcpy(match->dl_src, flow->dl_src, ETH_ADDR_LEN);
-    memcpy(match->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
-    match->dl_type = flow->dl_type;
-    match->nw_src = flow->nw_src;
-    match->nw_dst = flow->nw_dst;
-    match->nw_tos = flow->nw_tos;
-    match->nw_proto = flow->nw_proto;
-    match->tp_src = flow->tp_src;
-    match->tp_dst = flow->tp_dst;
-    memset(match->pad1, '\0', sizeof match->pad1);
-    memset(match->pad2, '\0', sizeof match->pad2);
-}
-
-void
-flow_from_match(const struct ofp_match *match, int flow_format,
-                ovs_be64 cookie, struct flow *flow,
-                struct flow_wildcards *wc)
-{
-    uint32_t wildcards = ntohl(match->wildcards) & OVSFW_ALL;
-
-    flow->tun_id = 0;
-    if (flow_format != NXFF_TUN_ID_FROM_COOKIE) {
-        wildcards |= NXFW_TUN_ID;
-    } else {
-        if (!(wildcards & NXFW_TUN_ID)) {
-            flow->tun_id = htonl(ntohll(cookie) >> 32);
-        }
-    }
-    flow_wildcards_init(wc, wildcards);
-
-    flow->nw_src = match->nw_src;
-    flow->nw_dst = match->nw_dst;
-    flow->in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
-                     : ntohs(match->in_port));
-    flow->dl_vlan = match->dl_vlan;
-    flow->dl_vlan_pcp = match->dl_vlan_pcp;
-    flow->dl_type = match->dl_type;
-    flow->tp_src = match->tp_src;
-    flow->tp_dst = match->tp_dst;
-    memcpy(flow->dl_src, match->dl_src, ETH_ADDR_LEN);
-    memcpy(flow->dl_dst, match->dl_dst, ETH_ADDR_LEN);
-    flow->nw_tos = match->nw_tos;
-    flow->nw_proto = match->nw_proto;
-}
-
 char *
 flow_to_string(const struct flow *flow)
 {
@@ -352,22 +294,6 @@ flow_print(FILE *stream, const struct flow *flow)
 \f
 /* flow_wildcards functions. */
 
-/* Given the wildcard bit count in bits 'shift' through 'shift + 5' (inclusive)
- * of 'wildcards', returns a 32-bit bit mask with a 1 in each bit that must
- * match and a 0 in each bit that is wildcarded.
- *
- * The bits in 'wildcards' are in the format used in enum ofp_flow_wildcards: 0
- * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
- * ..., 32 and higher wildcard the entire field.  This is the *opposite* of the
- * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
- * wildcarded. */
-ovs_be32
-flow_nw_bits_to_mask(uint32_t wildcards, int shift)
-{
-    wildcards = (wildcards >> shift) & 0x3f;
-    return wildcards < 32 ? htonl(~((1u << wildcards) - 1)) : 0;
-}
-
 /* Return 'wildcards' in "normal form":
  *
  *   - Forces unknown bits to 0.
@@ -377,7 +303,7 @@ flow_nw_bits_to_mask(uint32_t wildcards, int shift)
 static inline uint32_t
 flow_wildcards_normalize(uint32_t wildcards)
 {
-    wildcards &= wildcards & OVSFW_ALL;
+    wildcards &= wildcards & (OVSFW_ALL | FWW_ALL);
     if (wildcards & (0x20 << OFPFW_NW_SRC_SHIFT)) {
         wildcards &= ~(0x1f << OFPFW_NW_SRC_SHIFT);
     }
@@ -398,8 +324,8 @@ void
 flow_wildcards_init(struct flow_wildcards *wc, uint32_t wildcards)
 {
     wc->wildcards = flow_wildcards_normalize(wildcards) | FWW_REGS;
-    wc->nw_src_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_SRC_SHIFT);
-    wc->nw_dst_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_DST_SHIFT);
+    wc->nw_src_mask = ofputil_wcbits_to_netmask(wildcards >> OFPFW_NW_SRC_SHIFT);
+    wc->nw_dst_mask = ofputil_wcbits_to_netmask(wildcards >> OFPFW_NW_DST_SHIFT);
     memset(wc->reg_masks, 0, sizeof wc->reg_masks);
 }
 
@@ -414,6 +340,14 @@ flow_wildcards_init_exact(struct flow_wildcards *wc)
     memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
 }
 
+/* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
+ * fields. */
+bool
+flow_wildcards_is_exact(const struct flow_wildcards *wc)
+{
+    return !wc->wildcards;
+}
+
 static inline uint32_t
 combine_nw_bits(uint32_t wb1, uint32_t wb2, int shift)
 {
@@ -498,30 +432,13 @@ flow_wildcards_has_extra(const struct flow_wildcards *a,
             || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask);
 }
 
-static int
-count_ones(ovs_be32 mask)
-{
-#if __GNUC__ >= 4
-    return __builtin_popcount(mask);
-#else
-    int bits;
-
-    for (bits = 0; mask; bits++) {
-        mask &= mask - 1;
-    }
-
-    return bits;
-#endif
-}
-
 static bool
 set_nw_mask(struct flow_wildcards *wc, ovs_be32 mask,
             ovs_be32 *maskp, int shift)
 {
-    int wcbits = 32 - count_ones(mask);
-    if (flow_nw_bits_to_mask(wcbits, 0) == mask) {
+    if (ip_is_cidr(mask)) {
         wc->wildcards &= ~(0x3f << shift);
-        wc->wildcards |= wcbits << shift;
+        wc->wildcards |= ofputil_netmask_to_wcbits(mask) << shift;
         *maskp = mask;
         return true;
     } else {