29 #ifndef LB_PLUGIN_LB_LBHASH_H_ 30 #define LB_PLUGIN_LB_LBHASH_H_ 34 #include <immintrin.h> 39 #define LBHASH_ENTRY_PER_BUCKET 4 41 #define LB_HASH_DO_NOT_USE_SSE_BUCKETS 0 61 #define lb_hash_nbuckets(h) (((h)->buckets_mask) + 1) 62 #define lb_hash_size(h) ((h)->buckets_mask + LBHASH_ENTRY_PER_BUCKET) 64 #define lb_hash_foreach_bucket(h, bucket) \ 65 for (bucket = (h)->buckets; \ 66 bucket < (h)->buckets + lb_hash_nbuckets(h); \ 69 #define lb_hash_foreach_entry(h, bucket, i) \ 70 lb_hash_foreach_bucket(h, bucket) \ 71 for (i = 0; i < LBHASH_ENTRY_PER_BUCKET; i++) 73 #define lb_hash_foreach_valid_entry(h, bucket, i, now) \ 74 lb_hash_foreach_entry(h, bucket, i) \ 75 if (!clib_u32_loop_gt((now), bucket->timeout[i])) 107 val = _mm_crc32_u64(val, k0);
108 val = _mm_crc32_u64(val, k1);
109 val = _mm_crc32_u64(val, k2);
110 val = _mm_crc32_u64(val, k3);
111 val = _mm_crc32_u64(val, k4);
118 u64 tmp = k0 ^ k1 ^ k2 ^ k3 ^ k4;
132 u32 *available_index,
u32 *found_value)
136 *available_index = ~0;
137 #if __SSE4_2__ && LB_HASH_DO_NOT_USE_SSE_BUCKETS == 0 138 u32 bitmask, found_index;
142 mask = _mm_cmpgt_epi32(_mm_loadu_si128 ((__m128i *) bucket->
timeout),
143 _mm_set1_epi32 (time_now));
145 bitmask = (~_mm_movemask_epi8(mask)) & 0xffff;
147 *available_index = (bitmask)?__builtin_ctz(bitmask)/4:*available_index;
150 mask = _mm_and_si128(mask,
152 _mm_loadu_si128 ((__m128i *) bucket->
hash),
153 _mm_set1_epi32 (hash)));
157 mask = _mm_and_si128(mask,
159 _mm_loadu_si128 ((__m128i *) bucket->
vip),
160 _mm_set1_epi32 (vip)));
163 bitmask = _mm_movemask_epi8(mask);
165 found_index = (bitmask)?__builtin_ctzll(bitmask)/4:0;
167 *found_value = (bitmask)?bucket->
value[found_index]:*found_value;
173 u8 cmp = (bucket->
hash[
i] == hash && bucket->
vip[
i] == vip);
175 *found_value = (cmp || timeouted)?*found_value:bucket->
value[i];
177 *available_index = (timeouted && (*available_index == ~0))?i:*available_index;
193 u32 available_index,
u32 time_now)
196 bucket->
hash[available_index] = hash;
197 bucket->
value[available_index] = value;
199 bucket->
vip[available_index] = vip;
#define lb_hash_foreach_valid_entry(h, bucket, i, now)
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
sll srl srl sll sra u16x4 i
#define LBHASH_ENTRY_PER_BUCKET
vppinfra already includes tons of different hash tables.
static_always_inline lb_hash_t * lb_hash_alloc(u32 buckets, u32 timeout)
#define clib_u32_loop_gt(a, b)
32 bits integer comparison for running values.
static u64 clib_xxhash(u64 key)
static_always_inline u32 lb_hash_available_value(lb_hash_t *h, u32 hash, u32 available_index)
u32 vip[LBHASH_ENTRY_PER_BUCKET]
#define static_always_inline
u32 timeout[LBHASH_ENTRY_PER_BUCKET]
u32 value[LBHASH_ENTRY_PER_BUCKET]
lb_hash_bucket_t buckets[]
#define vec_alloc_aligned(V, N, A)
Allocate space for N more elements (no header, given alignment)
static_always_inline void lb_hash_prefetch_bucket(lb_hash_t *ht, u32 hash)
#define CLIB_PREFETCH(addr, size, type)
#define vec_free(V)
Free vector's memory (no header).
static_always_inline void lb_hash_free(lb_hash_t *h)
static uword is_pow2(uword x)
u32 hash[LBHASH_ENTRY_PER_BUCKET]
#define CLIB_CACHE_LINE_BYTES
static_always_inline void lb_hash_get(lb_hash_t *ht, u32 hash, u32 vip, u32 time_now, u32 *available_index, u32 *found_value)
static_always_inline u32 lb_hash_hash(u64 k0, u64 k1, u64 k2, u64 k3, u64 k4)
static_always_inline void lb_hash_put(lb_hash_t *h, u32 hash, u32 value, u32 vip, u32 available_index, u32 time_now)
static_always_inline u32 lb_hash_elts(lb_hash_t *h, u32 time_now)