LoadBal: Use intrinsics and use protocol number
[vpp.git] / plugins / lb-plugin / lb / lbhash.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 /**
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.
22  *
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).
26  *
27  */
28
29 #ifndef LB_PLUGIN_LB_LBHASH_H_
30 #define LB_PLUGIN_LB_LBHASH_H_
31
32 #include <vnet/vnet.h>
33
34 #include <immintrin.h>
35
36 /*
37  * @brief Number of entries per bucket.
38  */
39 #define LBHASH_ENTRY_PER_BUCKET 4
40
41 #define LB_HASH_DO_NOT_USE_SSE_BUCKETS 0
42
43 /*
44  * @brief One bucket contains 4 entries.
45  * Each bucket takes one 64B cache line in memory.
46  */
47 typedef struct {
48   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
49   u32 hash[LBHASH_ENTRY_PER_BUCKET];
50   u32 timeout[LBHASH_ENTRY_PER_BUCKET];
51   u32 vip[LBHASH_ENTRY_PER_BUCKET];
52   u32 value[LBHASH_ENTRY_PER_BUCKET];
53 } lb_hash_bucket_t;
54
55 typedef struct {
56   u32 buckets_mask;
57   u32 timeout;
58   lb_hash_bucket_t buckets[];
59 } lb_hash_t;
60
61 #define lb_hash_nbuckets(h) (((h)->buckets_mask) + 1)
62 #define lb_hash_size(h) ((h)->buckets_mask + LBHASH_ENTRY_PER_BUCKET)
63
64 #define lb_hash_foreach_bucket(h, bucket) \
65   for (bucket = (h)->buckets; \
66         bucket < (h)->buckets + lb_hash_nbuckets(h); \
67         bucket++)
68
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++)
72
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]))
76
77 static_always_inline
78 lb_hash_t *lb_hash_alloc(u32 buckets, u32 timeout)
79 {
80   if (!is_pow2(buckets))
81     return NULL;
82
83   // Allocate 1 more bucket for prefetch
84   u32 size = ((u64)&((lb_hash_t *)(0))->buckets[0]) +
85       sizeof(lb_hash_bucket_t) * (buckets + 1);
86   u8 *mem = 0;
87   lb_hash_t *h;
88   vec_alloc_aligned(mem, size, CLIB_CACHE_LINE_BYTES);
89   h = (lb_hash_t *)mem;
90   h->buckets_mask = (buckets - 1);
91   h->timeout = timeout;
92   return h;
93 }
94
95 static_always_inline
96 void lb_hash_free(lb_hash_t *h)
97 {
98   u8 *mem = (u8 *)h;
99   vec_free(mem);
100 }
101
102 #if __SSE4_2__
103 static_always_inline
104 u32 lb_hash_hash(u64 k0, u64 k1, u64 k2, u64 k3, u64 k4)
105 {
106   u64 val = 0;
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);
112   return (u32) val;
113 }
114 #else
115 static_always_inline
116 u32 lb_hash_hash(u64 k0, u64 k1, u64 k2, u64 k3, u64 k4)
117 {
118   u64 tmp = k0 ^ k1 ^ k2 ^ k3 ^ k4;
119   return (u32)clib_xxhash (tmp);
120 }
121 #endif
122
123 static_always_inline
124 void lb_hash_prefetch_bucket(lb_hash_t *ht, u32 hash)
125 {
126   lb_hash_bucket_t *bucket = &ht->buckets[hash & ht->buckets_mask];
127   CLIB_PREFETCH(bucket, sizeof(*bucket), READ);
128 }
129
130 static_always_inline
131 void lb_hash_get(lb_hash_t *ht, u32 hash, u32 vip, u32 time_now,
132                  u32 *available_index, u32 *found_value)
133 {
134   lb_hash_bucket_t *bucket = &ht->buckets[hash & ht->buckets_mask];
135   *found_value = ~0;
136   *available_index = ~0;
137 #if __SSE4_2__ && LB_HASH_DO_NOT_USE_SSE_BUCKETS == 0
138   u32 bitmask, found_index;
139   __m128i mask;
140
141   // mask[*] = timeout[*] > now
142   mask = _mm_cmpgt_epi32(_mm_loadu_si128 ((__m128i *) bucket->timeout),
143                          _mm_set1_epi32 (time_now));
144   // bitmask[*] = now <= timeout[*/4]
145   bitmask = (~_mm_movemask_epi8(mask)) & 0xffff;
146   // Get first index with now <= timeout[*], if any.
147   *available_index = (bitmask)?__builtin_ctz(bitmask)/4:*available_index;
148
149   // mask[*] = (timeout[*] > now) && (hash[*] == hash)
150   mask = _mm_and_si128(mask,
151                        _mm_cmpeq_epi32(
152                            _mm_loadu_si128 ((__m128i *) bucket->hash),
153                            _mm_set1_epi32 (hash)));
154
155   // Load the array of vip values
156   // mask[*] = (timeout[*] > now) && (hash[*] == hash) && (vip[*] == vip)
157   mask = _mm_and_si128(mask,
158                        _mm_cmpeq_epi32(
159                            _mm_loadu_si128 ((__m128i *) bucket->vip),
160                            _mm_set1_epi32 (vip)));
161
162   // mask[*] = (timeout[*x4] > now) && (hash[*x4] == hash) && (vip[*x4] == vip)
163   bitmask = _mm_movemask_epi8(mask);
164   // Get first index, if any
165   found_index = (bitmask)?__builtin_ctzll(bitmask)/4:0;
166   ASSERT(found_index < 4);
167   *found_value = (bitmask)?bucket->value[found_index]:*found_value;
168   bucket->timeout[found_index] =
169       (bitmask)?time_now + ht->timeout:bucket->timeout[found_index];
170 #else
171   u32 i;
172   for (i = 0; i < LBHASH_ENTRY_PER_BUCKET; i++) {
173       u8 cmp = (bucket->hash[i] == hash && bucket->vip[i] == vip);
174       u8 timeouted = clib_u32_loop_gt(time_now, bucket->timeout[i]);
175       *found_value = (cmp || timeouted)?*found_value:bucket->value[i];
176       bucket->timeout[i] = (cmp || timeouted)?time_now + ht->timeout:bucket->timeout[i];
177       *available_index = (timeouted && (*available_index == ~0))?i:*available_index;
178
179       if (!cmp)
180         return;
181   }
182 #endif
183 }
184
185 static_always_inline
186 u32 lb_hash_available_value(lb_hash_t *h, u32 hash, u32 available_index)
187 {
188   return h->buckets[hash & h->buckets_mask].value[available_index];
189 }
190
191 static_always_inline
192 void lb_hash_put(lb_hash_t *h, u32 hash, u32 value, u32 vip,
193                  u32 available_index, u32 time_now)
194 {
195   lb_hash_bucket_t *bucket = &h->buckets[hash & h->buckets_mask];
196   bucket->hash[available_index] = hash;
197   bucket->value[available_index] = value;
198   bucket->timeout[available_index] = time_now + h->timeout;
199   bucket->vip[available_index] = vip;
200 }
201
202 static_always_inline
203 u32 lb_hash_elts(lb_hash_t *h, u32 time_now)
204 {
205   u32 tot = 0;
206   lb_hash_bucket_t *bucket;
207   u32 i;
208   lb_hash_foreach_valid_entry(h, bucket, i, time_now) {
209     tot++;
210   }
211   return tot;
212 }
213
214 #endif /* LB_PLUGIN_LB_LBHASH_H_ */