2 * Copyright (c) 2012 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * vppinfra already includes tons of different hash tables.
18 * MagLev flow table is a bit different. It has to be very efficient
19 * for both writing and reading operations. But it does not need to
20 * be 100% reliable (write can fail). It also needs to recycle
21 * old entries in a lazy way.
23 * This hash table is the most dummy hash table you can do.
24 * Fixed total size, fixed bucket size.
25 * Advantage is that it could be very efficient (maybe).
29 #ifndef LB_PLUGIN_LB_LBHASH_H_
30 #define LB_PLUGIN_LB_LBHASH_H_
32 #include <vnet/vnet.h>
34 #define LBHASH_ENTRY_PER_BUCKET_LOG2 2
35 #define LBHASH_ENTRY_PER_BUCKET (1 << LBHASH_ENTRY_PER_BUCKET_LOG2)
36 #define LBHASH_ENTRY_PER_BUCKET_MASK (LBHASH_ENTRY_PER_BUCKET - 1)
47 lb_hash_entry_t entries[];
50 #define lb_hash_nbuckets(h) (((h)->buckets_mask >> LBHASH_ENTRY_PER_BUCKET_LOG2) + 1)
51 #define lb_hash_size(h) ((h)->buckets_mask + LBHASH_ENTRY_PER_BUCKET)
53 #define lb_hash_foreach_entry(h, e) \
54 for (e = (h)->entries; e < h->entries + lb_hash_size(h); e++)
56 #define lb_hash_foreach_valid_entry(h, e, now) \
57 lb_hash_foreach_entry(h, e) \
58 if (!clib_u32_loop_gt((now), (e)->last_seen + (h)->timeout))
61 lb_hash_t *lb_hash_alloc(u32 buckets, u32 timeout)
63 if ((!is_pow2(buckets)) ||
64 ((buckets << LBHASH_ENTRY_PER_BUCKET_LOG2) == 0))
67 // Allocate 1 more bucket for prefetch
68 u32 size = sizeof(lb_hash_t) + ((buckets << LBHASH_ENTRY_PER_BUCKET_LOG2) + 1)* sizeof(lb_hash_entry_t);
71 vec_alloc_aligned(mem, size, CLIB_CACHE_LINE_BYTES);
73 h->buckets_mask = (buckets - 1) << LBHASH_ENTRY_PER_BUCKET_LOG2;
79 void lb_hash_free(lb_hash_t *h)
86 u32 lb_hash_crc_u32(u32 data, u32 value)
88 __asm__ volatile( "crc32l %[data], %[value];"
89 : [value] "+r" (value)
90 : [data] "rm" (data));
95 u32 lb_hash_hash(u64 k[5])
100 value = lb_hash_crc_u32 (dp[0], value);
101 value = lb_hash_crc_u32 (dp[1], value);
102 value = lb_hash_crc_u32 (dp[2], value);
103 value = lb_hash_crc_u32 (dp[3], value);
104 value = lb_hash_crc_u32 (dp[4], value);
105 value = lb_hash_crc_u32 (dp[5], value);
106 value = lb_hash_crc_u32 (dp[6], value);
107 value = lb_hash_crc_u32 (dp[7], value);
108 value = lb_hash_crc_u32 (dp[8], value);
109 value = lb_hash_crc_u32 (dp[9], value);
114 u32 lb_hash_hash(u64 k[5])
116 u64 tmp = k[0] ^ k[1] ^ k[2] ^ k[3] ^ k[4];
117 return (u32)clib_xxhash (tmp);
124 void lb_hash_get(lb_hash_t *h, u64 k[5], u32 hash, u32 time_now, u32 *available_index, u32 *value)
126 lb_hash_entry_t *e = &h->entries[hash & h->buckets_mask];
129 *available_index = ~0;
130 CLIB_PREFETCH (&(e[1]), sizeof(lb_hash_entry_t), STORE);
131 for (i=0; i<LBHASH_ENTRY_PER_BUCKET; i++) {
132 CLIB_PREFETCH (&(e[i+2]), sizeof(lb_hash_entry_t), STORE); //+2 somehow performs best
134 (e[i].key[0] ^ k[0]) |
135 (e[i].key[1] ^ k[1]) |
136 (e[i].key[2] ^ k[2]) |
137 (e[i].key[3] ^ k[3]) |
138 (e[i].key[4] ^ k[4]);
140 u8 timeouted = clib_u32_loop_gt(time_now, e[i].last_seen + h->timeout);
142 *value = (cmp || timeouted)?*value:e[i].value;
143 e[i].last_seen = (cmp || timeouted)?e[i].last_seen:time_now;
144 *available_index = (timeouted && (*available_index == ~0))?(&e[i] - h->entries):*available_index;
152 u32 lb_hash_available_value(lb_hash_t *h, u32 available_index)
154 return h->entries[available_index].value;
158 u32 lb_hash_put(lb_hash_t *h, u64 k[5], u32 value, u32 available_index, u32 time_now)
160 lb_hash_entry_t *e = &h->entries[available_index];
167 e->last_seen = time_now;
172 u32 lb_hash_elts(lb_hash_t *h, u32 time_now)
176 lb_hash_foreach_valid_entry(h, e, time_now) {
182 #endif /* LB_PLUGIN_LB_LBHASH_H_ */