]> git.droids-corp.org - dpdk.git/commitdiff
hash: fix incorrect lookup if key is all zero
authorPablo de Lara <pablo.de.lara.guarch@intel.com>
Thu, 17 Sep 2015 10:30:48 +0000 (11:30 +0100)
committerThomas Monjalon <thomas.monjalon@6wind.com>
Wed, 4 Nov 2015 00:07:25 +0000 (01:07 +0100)
If user has not added an all zero key in the hash table,
and tries to look it up, it results in an incorrect hit,
as dummy slot in the key table has all zero as well.

Fixes: 48a399119619 ("hash: replace with cuckoo hash implementation")
Signed-off-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
doc/guides/rel_notes/release_2_2.rst
lib/librte_hash/rte_cuckoo_hash.c

index fa431714cfa8615d0dba7d3116bcdce2068d7321..ca8471b6b978d8e44f64b94a23e50c17acc5836a 100644 (file)
@@ -160,6 +160,12 @@ Libraries
   Fixed issue where an incorrect Cuckoo Hash key table size could be
   calculated limiting the size to 4GB.
 
+* **hash: Fixed incorrect lookup if key is all zero.**
+
+  Fixed issue in hash library that occurred if an all zero
+  key was not added in the table and the key was looked up,
+  resulting in an incorrect hit.
+
 
 Examples
 ~~~~~~~~
index f797aeba3fcc3ef78f57df6c97284d77c0291b1e..1e970de6f410868caa2112aee818ebd298a25aac 100644 (file)
@@ -966,15 +966,22 @@ lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
 static inline void
 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
-               void *data[], uint64_t *hits, const struct rte_hash *h)
+               const int32_t *positions, void *data[], uint64_t *hits,
+               const struct rte_hash *h)
 {
        unsigned hit;
+       unsigned key_idx;
 
        hit = !h->rte_hash_cmp_eq(key_slot->key, keys[idx], h->key_len);
        if (data != NULL)
                data[idx] = key_slot->pdata;
 
-       *hits |= (uint64_t)(hit) << idx;
+       key_idx = positions[idx] + 1;
+       /*
+        * If key index is 0, force hit to be 0, in case key to be looked up
+        * is all zero (as in the dummy slot), which would result in a wrong hit
+        */
+       *hits |= (uint64_t)(hit && !!key_idx)  << idx;
 }
 
 static inline void
@@ -1066,8 +1073,8 @@ __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
                lookup_stage2(idx21, primary_hash21, secondary_hash21,
                        primary_bkt21, secondary_bkt21, &k_slot21, positions,
                        &extra_hits_mask, key_store, h);
-               lookup_stage3(idx30, k_slot30, keys, data, &hits, h);
-               lookup_stage3(idx31, k_slot31, keys, data, &hits, h);
+               lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
+               lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
        }
 
        k_slot30 = k_slot20, k_slot31 = k_slot21;
@@ -1093,8 +1100,8 @@ __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
        lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
                secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
                key_store, h);
-       lookup_stage3(idx30, k_slot30, keys, data, &hits, h);
-       lookup_stage3(idx31, k_slot31, keys, data, &hits, h);
+       lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
+       lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
 
        k_slot30 = k_slot20, k_slot31 = k_slot21;
        idx30 = idx20, idx31 = idx21;
@@ -1114,14 +1121,14 @@ __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
        lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
                secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
                key_store, h);
-       lookup_stage3(idx30, k_slot30, keys, data, &hits, h);
-       lookup_stage3(idx31, k_slot31, keys, data, &hits, h);
+       lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
+       lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
 
        k_slot30 = k_slot20, k_slot31 = k_slot21;
        idx30 = idx20, idx31 = idx21;
 
-       lookup_stage3(idx30, k_slot30, keys, data, &hits, h);
-       lookup_stage3(idx31, k_slot31, keys, data, &hits, h);
+       lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
+       lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
 
        /* ignore any items we have already found */
        extra_hits_mask &= ~hits;