Skip to content

Commit 907be81

Browse files
use siphash to replace hash_string (#472)
* use siphash to replace hash_string * add siphash algorithm
1 parent 309e7aa commit 907be81

File tree

5 files changed

+310
-17
lines changed

5 files changed

+310
-17
lines changed

include/xquic/xquic.h

+2
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,8 @@ typedef struct xqc_config_s {
10941094

10951095
/** bucket size of connection hash table in engine */
10961096
size_t conns_hash_bucket_size;
1097+
/* for warning when the number of elements in one bucket exceeds the value of hash_conflict_threshold*/
1098+
uint32_t hash_conflict_threshold;
10971099

10981100
/** capacity of connection priority queue in engine */
10991101
size_t conns_active_pq_capacity;

src/common/xqc_siphash.h

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/**
2+
* @copyright Copyright (c) 2025, Alibaba Group Holding Limited
3+
*/
4+
5+
#ifndef _XQC_SIPHASH_H_INCLUDED_
6+
#define _XQC_SIPHASH_H_INCLUDED_
7+
8+
#include <inttypes.h>
9+
#include <stdint.h>
10+
#include <string.h>
11+
#include "src/common/xqc_common.h"
12+
13+
#define XQC_SIPHASH_KEY_SIZE 16
14+
#define XQC_SIPHASH_C_ROUNDS 2
15+
#define XQC_SIPHASH_D_ROUNDS 4
16+
#define XQC_DEFAULT_HASH_SIZE 8
17+
18+
/* save siphash context */
19+
typedef struct xqc_siphash_ctx {
20+
/* v0 v1 v2 v3 */
21+
uint64_t v0;
22+
uint64_t v1;
23+
uint64_t v2;
24+
uint64_t v3;
25+
int hash_size; /* save sizeof(hash), only 8 or 16 */
26+
/* SipHash-2-4 */
27+
int crounds;
28+
int drounds;
29+
} xqc_siphash_ctx_t;
30+
31+
#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
32+
33+
#define U32TO8_LE(p, v) \
34+
(p)[0] = (uint8_t)((v)); \
35+
(p)[1] = (uint8_t)((v) >> 8); \
36+
(p)[2] = (uint8_t)((v) >> 16); \
37+
(p)[3] = (uint8_t)((v) >> 24);
38+
39+
#define U64TO8_LE(p, v) \
40+
U32TO8_LE((p), (uint32_t)((v))); \
41+
U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
42+
43+
#define U8TO64_LE(p) \
44+
(((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
45+
((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
46+
((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
47+
((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
48+
49+
#define SIPROUND \
50+
do { \
51+
v0 += v1; \
52+
v1 = ROTL(v1, 13); \
53+
v1 ^= v0; \
54+
v0 = ROTL(v0, 32); \
55+
v2 += v3; \
56+
v3 = ROTL(v3, 16); \
57+
v3 ^= v2; \
58+
v0 += v3; \
59+
v3 = ROTL(v3, 21); \
60+
v3 ^= v0; \
61+
v2 += v1; \
62+
v1 = ROTL(v1, 17); \
63+
v1 ^= v2; \
64+
v2 = ROTL(v2, 32); \
65+
} while (0)
66+
67+
static inline int
68+
xqc_siphash_init(xqc_siphash_ctx_t *ctx, const unsigned char *k, size_t key_len,
69+
size_t hash_size, int crounds, int drounds)
70+
{
71+
uint64_t k0, k1;
72+
if (key_len != XQC_SIPHASH_KEY_SIZE) {
73+
return XQC_ERROR;
74+
}
75+
/* hash_size must be 8 or 16 */
76+
if (hash_size != 8 && hash_size != 16) {
77+
return XQC_ERROR;
78+
}
79+
k0 = U8TO64_LE(k);
80+
k1 = U8TO64_LE(k + 8);
81+
82+
ctx->v0 = 0x736f6d6570736575ULL ^ k0;
83+
ctx->v1 = 0x646f72616e646f6dULL ^ k1;
84+
ctx->v2 = 0x6c7967656e657261ULL ^ k0;
85+
ctx->v3 = 0x7465646279746573ULL ^ k1;
86+
87+
ctx->hash_size = hash_size;
88+
if (hash_size == 16) {
89+
ctx->v1 ^= 0xee;
90+
}
91+
/* default: SipHash-2-4 */
92+
if (crounds == 0) {
93+
ctx->crounds = XQC_SIPHASH_C_ROUNDS;
94+
} else {
95+
ctx->crounds = crounds;
96+
}
97+
if (drounds == 0) {
98+
ctx->drounds = XQC_SIPHASH_D_ROUNDS;
99+
} else {
100+
ctx->drounds = drounds;
101+
}
102+
return XQC_OK;
103+
}
104+
105+
106+
/*
107+
Computes a SipHash value
108+
*ctx: point to siphash context
109+
*in: pointer to input data (read-only)
110+
inlen: input data length in bytes (any size_t value)
111+
*out: pointer to output data (write-only), outlen bytes must be allocated
112+
outlen: length of the output in bytes, must be 8 or 16
113+
*/
114+
115+
static inline int
116+
xqc_siphash(xqc_siphash_ctx_t *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen)
117+
{
118+
uint64_t b = (uint64_t)inlen << 56, m;
119+
const uint8_t *pi = in, *end = in + inlen - (inlen % sizeof(uint64_t));
120+
int left = inlen & 7;
121+
int i = 0;
122+
uint64_t v0 = ctx->v0;
123+
uint64_t v1 = ctx->v1;
124+
uint64_t v2 = ctx->v2;
125+
uint64_t v3 = ctx->v3;
126+
127+
if (outlen != ctx->hash_size) {
128+
return XQC_ERROR;
129+
}
130+
131+
for(; pi != end; pi += 8) {
132+
m = U8TO64_LE(pi);
133+
v3 ^= m;
134+
for (i = 0; i < ctx->crounds; i++) {
135+
SIPROUND;
136+
}
137+
v0 ^= m;
138+
}
139+
140+
switch (left) {
141+
case 7:
142+
b |= ((uint64_t)pi[6]) << 48;
143+
case 6:
144+
b |= ((uint64_t)pi[5]) << 40;
145+
case 5:
146+
b |= ((uint64_t)pi[4]) << 32;
147+
case 4:
148+
b |= ((uint64_t)pi[3]) << 24;
149+
case 3:
150+
b |= ((uint64_t)pi[2]) << 16;
151+
case 2:
152+
b |= ((uint64_t)pi[1]) << 8;
153+
case 1:
154+
b |= ((uint64_t)pi[0]);
155+
break;
156+
case 0:
157+
break;
158+
}
159+
160+
v3 ^= b;
161+
for (i = 0; i < ctx->crounds; ++i)
162+
SIPROUND;
163+
v0 ^= b;
164+
165+
if (outlen == 16) {
166+
v2 ^= 0xee;
167+
} else {
168+
v2 ^= 0xff;
169+
}
170+
171+
for (i = 0; i < ctx->drounds; ++i)
172+
SIPROUND;
173+
174+
b = v0 ^ v1 ^ v2 ^ v3;
175+
U64TO8_LE(out, b);
176+
if (outlen == 8) {
177+
return XQC_OK;
178+
}
179+
v1 ^= 0xdd;
180+
for (i = 0; i < ctx->drounds; ++i)
181+
SIPROUND;
182+
b = v0 ^ v1 ^ v2 ^ v3;
183+
U64TO8_LE(out + 8, b);
184+
return XQC_OK;
185+
}
186+
187+
188+
189+
190+
#endif /* _XQC_SIPHASH_H_INCLUDED_ */

src/common/xqc_str_hash.h

+87-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,21 @@
66
#define _XQC_STR_HASH_INCLUDED_
77

88
#include <stdint.h>
9+
#include <time.h>
910

1011
#include "src/common/xqc_str.h"
12+
#include "src/common/xqc_siphash.h"
13+
#include "src/common/xqc_log.h"
14+
15+
/*
16+
* default log threshold of number of element in on bucket
17+
* test result of max conflict (default 1024*1024 hash buckets) in one bucket:
18+
* 100000 connections, the max conflict is 5
19+
* 1000000 connections, the max conflict is 24
20+
*/
21+
#define XQC_HASH_DEFAULT_CONFLICT_THRESHOLD 50
22+
/*10 second, log interval must not less then 10 second*/
23+
#define XQC_HASH_CONFLICT_LOG_INTERVAL 10
1124

1225
typedef struct xqc_str_hash_element_s {
1326
uint64_t hash;
@@ -24,20 +37,79 @@ typedef struct xqc_str_hash_table_s {
2437
xqc_str_hash_node_t **list;
2538
size_t count;
2639
xqc_allocator_t allocator; /* memory allocator */
40+
uint8_t *conflict_stat; /* statistic the number of elements in every bucket */
41+
xqc_siphash_ctx_t siphash_ctx; /* siphash context */
42+
uint32_t conflict_thres; /* conflict threshold in one bucket, warning if exceeded */
43+
time_t last_log_time; /* last timestamp(second) for logging the max conflict value*/
44+
xqc_log_t *log; /* pointer to engine's log*/
2745
} xqc_str_hash_table_t;
2846

47+
/* calculate the hash value using the siphash algorithm */
48+
static inline uint64_t
49+
xqc_siphash_get_hash(xqc_siphash_ctx_t *ctx, const uint8_t *data, size_t len)
50+
{
51+
uint64_t hash_value;
52+
if (xqc_siphash(ctx, data, len, (uint8_t *)(&hash_value), sizeof(hash_value)) == XQC_OK) {
53+
return hash_value;
54+
}
55+
/*
56+
* impossible, we set hash_size value 8 when we call xqc_siphash_init, sizeof(hash_value) is always 8
57+
* xqc_siphash return XQC_ERROR only when hash_size not equal sizeof(hash_value), it is impossible here
58+
*/
59+
return 0;
60+
}
2961

3062
static inline int
31-
xqc_str_hash_init(xqc_str_hash_table_t *hash_tab, xqc_allocator_t allocator, size_t bucket_num)
63+
xqc_str_hash_init(xqc_str_hash_table_t *hash_tab,
64+
xqc_allocator_t allocator, size_t bucket_num,
65+
uint32_t conflict_thres, uint8_t *key,
66+
size_t key_len, xqc_log_t *log)
3267
{
68+
if (bucket_num == 0) { /* impossible */
69+
return XQC_ERROR;
70+
}
71+
if (key_len != XQC_SIPHASH_KEY_SIZE) { /* siphash key length must be 16 */
72+
return XQC_ERROR;
73+
}
74+
if (log == NULL) {
75+
return XQC_ERROR;
76+
}
77+
xqc_memzero(hash_tab, sizeof(xqc_str_hash_table_t));
3378
hash_tab->allocator = allocator;
3479
hash_tab->list = allocator.malloc(allocator.opaque, sizeof(xqc_str_hash_node_t *) * bucket_num);
3580
if (hash_tab->list == NULL) {
3681
return XQC_ERROR;
3782
}
3883
xqc_memzero(hash_tab->list, sizeof(xqc_str_hash_node_t *) * bucket_num);
3984
hash_tab->count = bucket_num;
85+
hash_tab->conflict_stat = allocator.malloc(allocator.opaque, sizeof(uint8_t) * bucket_num);
86+
if (hash_tab->conflict_stat == NULL) {
87+
goto fail;
88+
}
89+
xqc_memzero(hash_tab->conflict_stat, sizeof(uint8_t) * bucket_num);
90+
if (conflict_thres > 0) {
91+
hash_tab->conflict_thres = conflict_thres;
92+
} else {
93+
hash_tab->conflict_thres = XQC_HASH_DEFAULT_CONFLICT_THRESHOLD;
94+
}
95+
hash_tab->last_log_time = 0;
96+
hash_tab->log = log;
97+
if (xqc_siphash_init(&hash_tab->siphash_ctx, key, key_len,
98+
XQC_DEFAULT_HASH_SIZE, XQC_SIPHASH_C_ROUNDS,
99+
XQC_SIPHASH_D_ROUNDS) != XQC_OK)
100+
{
101+
goto fail;
102+
}
40103
return XQC_OK;
104+
105+
fail:
106+
if (hash_tab->list) {
107+
allocator.free(allocator.opaque, hash_tab->list);
108+
}
109+
if (hash_tab->conflict_stat) {
110+
allocator.free(allocator.opaque, hash_tab->conflict_stat);
111+
}
112+
return XQC_ERROR;
41113
}
42114

43115
static inline void
@@ -53,6 +125,7 @@ xqc_str_hash_release(xqc_str_hash_table_t *hash_tab)
53125
}
54126
}
55127
a->free(a->opaque, hash_tab->list);
128+
a->free(a->opaque, hash_tab->conflict_stat);
56129
}
57130

58131
static inline void *
@@ -90,6 +163,16 @@ xqc_str_hash_add(xqc_str_hash_table_t *hash_tab, xqc_str_hash_element_t e)
90163

91164
node->next = hash_tab->list[index];
92165
hash_tab->list[index] = node;
166+
hash_tab->conflict_stat[index] += 1;
167+
if (hash_tab->conflict_stat[index] > hash_tab->conflict_thres) {
168+
time_t now_sec = time(NULL);
169+
if (now_sec >= hash_tab->last_log_time + XQC_HASH_CONFLICT_LOG_INTERVAL) {
170+
xqc_log(hash_tab->log, XQC_LOG_WARN,
171+
"|xqc conn hash conflict exceed|index:%ui, number of elements:%d|",
172+
index, hash_tab->conflict_stat[index]);
173+
hash_tab->last_log_time = now_sec;
174+
}
175+
}
93176

94177
return XQC_OK;
95178
}
@@ -104,6 +187,9 @@ xqc_str_hash_delete(xqc_str_hash_table_t *hash_tab, uint64_t hash, xqc_str_t str
104187
while (node) {
105188
if (node->element.hash == hash && xqc_str_equal(str, node->element.str)) {
106189
*pp = node->next;
190+
if (hash_tab->conflict_stat[index] > 0) {
191+
hash_tab->conflict_stat[index] -= 1;
192+
}
107193
a->free(a->opaque, node->element.str.data);
108194
a->free(a->opaque, node);
109195
return XQC_OK;

0 commit comments

Comments
 (0)