Skip to content

Commit

Permalink
Use asm to do boundary checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sebymiano committed Jun 1, 2024
1 parent 5b759b9 commit e7fe1e1
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/ebpf/lib/cuckoo_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ typedef enum {
#define CUCKOO_MAX_KICK_ATTEMPTS 10
#endif

#ifndef bpf_clamp_uminmax
#define bpf_clamp_uminmax(VAR, UMIN, UMAX) \
asm volatile("if %0 >= %[min] goto +2\n" \
"%0 = %[min]\n" \
"goto +2\n" \
"if %0 <= %[max] goto +1\n" \
"%0 = %[max]\n" \
: "+r"(VAR) \
: [min] "i"(UMIN), [max] "i"(UMAX))
#endif

#define BPF_CUCKOO_FILTER(_name, _key_type, _max_entries) \
\
typedef struct _name##_cuckoo_filter_cell_s { \
Expand Down Expand Up @@ -160,7 +171,7 @@ typedef enum {
fingerprint &= _name##_cuckoo_filter_cfg.MASK; \
fingerprint += !fingerprint; \
\
volatile uint32_t h2 = \
uint32_t h2 = \
((h1 ^ hash(&fingerprint, sizeof(fingerprint), _name##_cuckoo_filter_cfg.BUCKET_COUNT, \
900, _name##_cuckoo_filter_cfg.HASH_SEED)) % \
_name##_cuckoo_filter_cfg.BUCKET_COUNT); \
Expand All @@ -182,15 +193,15 @@ typedef enum {
size_t ii; \
for (ii = 0; ii < _name##_cuckoo_filter_cfg.CUCKOO_NESTS_PER_BUCKET; ++ii) { \
uint32_t idx = (h1 * _name##_cuckoo_filter_cfg.CUCKOO_NESTS_PER_BUCKET) + ii; \
idx = idx % CUCKOO_MAX_BUCKET_COUNT; \
bpf_clamp_uminmax(idx, 0, CUCKOO_MAX_BUCKET_COUNT - 1); \
uint16_t elem = filter->buckets[idx].fingerprint; \
if (elem == fingerprint) { \
result->was_found = true; \
break; \
} \
\
idx = (h2 * _name##_cuckoo_filter_cfg.CUCKOO_NESTS_PER_BUCKET) + ii; \
idx = idx % (CUCKOO_MAX_BUCKET_COUNT); \
bpf_clamp_uminmax(idx, 0, CUCKOO_MAX_BUCKET_COUNT - 1); \
elem = filter->buckets[idx].fingerprint; \
if (elem == fingerprint) { \
result->was_found = true; \
Expand All @@ -212,7 +223,7 @@ typedef enum {
\
for (ii = 0; ii < _name##_cuckoo_filter_cfg.CUCKOO_NESTS_PER_BUCKET; ++ii) { \
uint32_t idx = (h * _name##_cuckoo_filter_cfg.CUCKOO_NESTS_PER_BUCKET) + ii; \
idx = idx % CUCKOO_MAX_BUCKET_COUNT; \
bpf_clamp_uminmax(idx, 0, CUCKOO_MAX_BUCKET_COUNT - 1); \
\
_name##_cuckoo_filter_cell_t *nest = &filter->buckets[idx]; \
if (0 == nest->fingerprint) { \
Expand All @@ -230,7 +241,7 @@ typedef enum {
\
for (ii = 0; ii < _name##_cuckoo_filter_cfg.CUCKOO_NESTS_PER_BUCKET; ++ii) { \
uint32_t idx = (h * _name##_cuckoo_filter_cfg.CUCKOO_NESTS_PER_BUCKET) + ii; \
idx = idx % CUCKOO_MAX_BUCKET_COUNT; \
bpf_clamp_uminmax(idx, 0, CUCKOO_MAX_BUCKET_COUNT - 1); \
\
_name##_cuckoo_filter_cell_t *nest = &filter->buckets[idx]; \
if (fp == nest->fingerprint) { \
Expand Down Expand Up @@ -286,10 +297,7 @@ typedef enum {
(bpf_get_prandom_u32() % _name##_cuckoo_filter_cfg.CUCKOO_NESTS_PER_BUCKET); \
uint32_t idx = (row * _name##_cuckoo_filter_cfg.CUCKOO_NESTS_PER_BUCKET) + col; \
\
if (idx < 0 || idx >= CUCKOO_MAX_BUCKET_COUNT) { \
return CUCKOO_FILTER_ALLOCATION_FAILED; \
} \
idx = idx % CUCKOO_MAX_BUCKET_COUNT; \
bpf_clamp_uminmax(idx, 0, CUCKOO_MAX_BUCKET_COUNT - 1); \
\
size_t elem = loop_ctx.map->buckets[idx].fingerprint; \
loop_ctx.map->buckets[idx].fingerprint = loop_ctx.fingerprint; \
Expand Down

0 comments on commit e7fe1e1

Please sign in to comment.