I'd create a map to store just one element (a port number) and it should be read/written both from userspace and kernelspace. Which map type should I use? Which size for key and value is appropriate and how can I write/read from both sides?
_user.c
/* create array map with one element */
map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 1, 0);
...
/* update map */
ret = bpf_map_update_elem(map_fd, &key, &i, BPF_ANY);
_kern.c
How can I refer to map_fd and operate on the same map?
EDIT:
I could successfully create and interact with the map only in one way:
defining the map within _kern.c file, as follow:
struct bpf_map_def SEC("maps") my_map = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(uint32_t),
.value_size = sizeof(uint32_t),
.max_entries = 1,
};
That definition allows to operate directly on the map using bpf helpers like bpf_map_lookup_elem.
Instead within _user.c after loading the _kern.o ebpf program into the kernel through bpf_prog_load I used
map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
to retrieve the file descriptor associated with the map (I was missing this point). Once you get the file descriptor to perform, for instance, a map update you can call
ret = bpf_map_update_elem(map_fd, &key, &value, BPF_ANY);
QUESTION: in this case I retrieve the fd from user space using libbpf, but if I create a map from _user.c with bpf_create_map then how can I retrieve the fd from ebpf program ?