[ovs-dev] [PATCH v3 6/7] classifier: Simplify minimask_hash().

Jarno Rajahalme jrajahalme at nicira.com
Fri Aug 7 00:38:00 UTC 2015


minimask_hash() can be simplified as each value is known to be non-zero.

Move miniflow_hash() into test-classifier.c as miniflow_hash__() as it
is no longer needed elsewhere.

Signed-off-by: Jarno Rajahalme <jrajahalme at nicira.com>
---
 lib/classifier-private.h | 38 +++++++++-----------------------------
 tests/test-classifier.c  | 34 +++++++++++++++++++++++++++++++++-
 2 files changed, 42 insertions(+), 30 deletions(-)

diff --git a/lib/classifier-private.h b/lib/classifier-private.h
index 0fd58e4..a7a5132 100644
--- a/lib/classifier-private.h
+++ b/lib/classifier-private.h
@@ -341,41 +341,21 @@ flow_wildcards_fold_minimask_in_map(struct flow_wildcards *wc,
     }
 }
 
-/* Returns a hash value for 'flow', given 'basis'. */
+/* Returns a hash value for 'mask', given 'basis'. */
 static inline uint32_t
-miniflow_hash(const struct miniflow *flow, uint32_t basis)
+minimask_hash(const struct minimask *mask, uint32_t basis)
 {
-    const uint64_t *values = miniflow_get_values(flow);
-    const uint64_t *p = values;
+    const uint64_t *p = miniflow_get_values(&mask->masks);
+    size_t n_values = miniflow_n_values(&mask->masks);
     uint32_t hash = basis;
-    uint64_t hash_tnl_map = 0, hash_pkt_map = 0;
-    uint64_t map;
 
-    for (map = flow->tnl_map; map; map = zero_rightmost_1bit(map)) {
-        if (*p) {
-            hash = hash_add64(hash, *p);
-            hash_tnl_map |= rightmost_1bit(map);
-        }
-        p++;
-    }
-    for (map = flow->pkt_map; map; map = zero_rightmost_1bit(map)) {
-        if (*p) {
-            hash = hash_add64(hash, *p);
-            hash_pkt_map |= rightmost_1bit(map);
-        }
-        p++;
+    for (size_t i = 0; i < n_values; i++) {
+        hash = hash_add64(hash, *p++);
     }
-    hash = hash_add64(hash, hash_tnl_map);
-    hash = hash_add64(hash, hash_pkt_map);
+    hash = hash_add64(hash, mask->masks.tnl_map);
+    hash = hash_add64(hash, mask->masks.pkt_map);
 
-    return hash_finish(hash, p - values);
-}
-
-/* Returns a hash value for 'mask', given 'basis'. */
-static inline uint32_t
-minimask_hash(const struct minimask *mask, uint32_t basis)
-{
-    return miniflow_hash(&mask->masks, basis);
+    return hash_finish(hash, n_values);
 }
 
 /* Returns a hash value for the bits of range [start, end) in 'minimatch',
diff --git a/tests/test-classifier.c b/tests/test-classifier.c
index 2462aed..54b595f 100644
--- a/tests/test-classifier.c
+++ b/tests/test-classifier.c
@@ -1633,6 +1633,36 @@ miniflow_clone__(const struct miniflow *src)
     return dst;
 }
 
+/* Returns a hash value for 'flow', given 'basis'. */
+static inline uint32_t
+miniflow_hash__(const struct miniflow *flow, uint32_t basis)
+{
+    const uint64_t *values = miniflow_get_values(flow);
+    const uint64_t *p = values;
+    uint32_t hash = basis;
+    uint64_t hash_tnl_map = 0, hash_pkt_map = 0;
+    uint64_t map;
+
+    for (map = flow->tnl_map; map; map = zero_rightmost_1bit(map)) {
+        if (*p) {
+            hash = hash_add64(hash, *p);
+            hash_tnl_map |= rightmost_1bit(map);
+        }
+        p++;
+    }
+    for (map = flow->pkt_map; map; map = zero_rightmost_1bit(map)) {
+        if (*p) {
+            hash = hash_add64(hash, *p);
+            hash_pkt_map |= rightmost_1bit(map);
+        }
+        p++;
+    }
+    hash = hash_add64(hash, hash_tnl_map);
+    hash = hash_add64(hash, hash_pkt_map);
+
+    return hash_finish(hash, p - values);
+}
+
 static void
 test_miniflow(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
@@ -1667,7 +1697,7 @@ test_miniflow(struct ovs_cmdl_context *ctx OVS_UNUSED)
         /* Check that copying a miniflow works properly. */
         miniflow2 = miniflow_clone__(miniflow);
         assert(miniflow_equal(miniflow, miniflow2));
-        assert(miniflow_hash(miniflow, 0) == miniflow_hash(miniflow2, 0));
+        assert(miniflow_hash__(miniflow, 0) == miniflow_hash__(miniflow2, 0));
         miniflow_expand(miniflow2, &flow3);
         assert(flow_equal(&flow, &flow3));
 
@@ -1683,6 +1713,8 @@ test_miniflow(struct ovs_cmdl_context *ctx OVS_UNUSED)
         assert(miniflow_equal_flow_in_minimask(miniflow, &flow2, minimask));
         assert(miniflow_hash_in_minimask(miniflow, minimask, 0x12345678) ==
                flow_hash_in_minimask(&flow, minimask, 0x12345678));
+        assert(minimask_hash(minimask, 0) ==
+               miniflow_hash__(&minimask->masks, 0));
 
         /* Check that masked matches work as expected for differing flows and
          * miniflows. */
-- 
2.1.4




More information about the dev mailing list