I'm trying to create a hashmap in BPF in kernel code, as follows:
struct bpf_map_def SEC("maps") hash_map = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(__u32),
.value_size = sizeof(__u32),
.max_entries = 1000,
};
Then I just insert and retrieve from the map as follows:
__u32 key = src_ip;
__u32 init_val = dst_ip;
__u32 *value;
value = bpf_map_lookup_elem(&hash_map, &key);
if (value) {
trace_printk("value found=%lu\n", value);
}
Where src_ip and dst_ip are the source and destination IPs from the packet I'm processing:
__u32 src_ip = ip->saddr;
__u32 dst_ip = ip->daddr;
However when I print the value obtained from the map I get:
value found=18446637648727971896
Whereas the actual src and dst addresses in the print that I have just above shows:
src= 763730773, dst= 2818323142
I just don't understand how to retrieve/store in the map correctly. Any pointers on that?
Thanks.
The lookup function returns a pointer to your value:
void *bpf_map_lookup_elem(struct bpf_map *map, const void *key);
Have you tried to dereference it? Something like:
trace_printk("value found=%lu\n", *value);