Skip to content

Commit

Permalink
re-write hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Bentley committed Dec 9, 2018
1 parent 45ee3e7 commit 01bef1a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dump_maps: dump_maps.c common.h cjson/cJSON.o
bpf_load.o: bpf_load.c bpf_load.h
clang $(CFLAGS) $(IFLAGS) -c bpf_load.c -o bpf_load.o

test-hash: test-hash.c xdp-flowradar.c
test-hash: test-hash.c xdp-flowradar_kern.c
clang $(CFLAGS) test-hash.c -o test-hash

load: xdp-flowradar.o unload
Expand Down
2 changes: 1 addition & 1 deletion test-hash.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "xdp-flowradar.c"
#include "xdp-flowradar_kern.c"
#include <stdio.h>
#include <stdint.h>
#include <linux/types.h>
Expand Down
33 changes: 14 additions & 19 deletions xdp-flowradar_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,26 @@ struct bpf_map_def SEC("maps") host_info_map = {
*/
static __always_inline
uint16_t hash(uint16_t host, uint8_t k, struct five_tuple *ft) {
unsigned long i;
uint32_t a = 63689;
uint32_t b = 378551;

uint16_t saddr1 = (uint16_t)(ft->saddr >> 16);
uint16_t saddr2 = (uint16_t)(ft->saddr & 0x0000ffff);
uint16_t daddr1 = (uint16_t)(ft->daddr >> 16);
uint16_t daddr2 = (uint16_t)(ft->daddr & 0x0000ffff);
char *ptr = (void *)ft;

uint32_t hash = k;
hash = hash * a + host;
a = a * b;
hash = hash * a + saddr1;
a = a * b;
hash = hash * a + saddr2;
a = a * b;
hash = hash * a + daddr1;
a = a * b;
hash = hash * a + daddr2;
a = a * b;
hash = hash * a + ft->sport;
a = a * b;
hash = hash * a + ft->dport;
a = a * b;
hash = hash * a + ft->proto;
#pragma unroll
for (i=0; i<sizeof(struct five_tuple); i++) {
ptr = (void *)ft + i;
hash = hash * a + *ptr;
a = a * b;
}

#pragma unroll
for (i=0; i<sizeof(uint16_t); i++) {
ptr = (void *)&host + i;
hash = hash * a + *ptr;
a = a * b;
}

// We've been doing math on a uint32, so xor the high and low bits together
uint16_t h1 = (uint16_t)(hash & 0x0000ffff);
Expand Down

0 comments on commit 01bef1a

Please sign in to comment.