Reputation: 83
I am trying to write a code for XDP where a user space program will populate a bpf map and then later the Kernel will look into the map. For creating the map I have added a SEC(".maps") in the bpf_obj file that will get loaded into the kernel. Regarding this I have a bunch of questions:
Is the bpf_map_lookup_elem() somehow making the data persistent in the map after bpf_map_update_elem() in the Insert()?
Edit: (Here is a snippet of code)
Insert(struct my_key key, struct my_val value){
map_fd = find_map_fd(bpf_obj, "my_map");
if(map_fd < 0)
xdp_link_detach(config.ifindex, config.xdp_flags,0);
bpf_map_update_elem(map_fd, &key, &value, BPF_ANY);
}
Fetch(struct my_key key){
struct my_val value;
map_fd = find_map_fd(bpf_obj, "my_map");
if(map_fd < 0)
xdp_link_detach(config.ifindex, config.xdp_flags,0);
if(bpf_map_lookup_elem(map_fd, &key, &value) == 0)
printf("Found key");
else
printf("Key not found");
}
int main(){
// Defined key and value
Insert(key, value);
Fetch(key); // This call is failing to fetch
return 0;
}
However if I change my Insert() then above program works:
Insert(struct my_key key, struct my_val val){
map_fd = find_map_fd(bpf_obj, "my_map");
if(map_fd < 0)
xdp_link_detach(config.ifindex, config.xdp_flags,0);
bpf_map_update_elem(map_fd, &key, &val, BPF_ANY);
//Immediately fetch the value
struct my_val value;
bpf_map_lookup_elem(map_fd, &key, &value);
}
Upvotes: 2
Views: 1558
Reputation: 83
Actually, I found the issue. I had to change my key definition from:
struct Key {
uint32_t srcIP;
uint16_t srcPort;
};
to:
struct Key {
uint32_t IP;
uint32_t port;
};
This has something to do with how kernel creates boundaries. It was probably doing some padding to the port that I write. I used strace to look into the bpf calls, where I noticed that the key that I ask for and the key that is written in the bpf_map were off by 32 decimals.
Upvotes: 2