I'm new to eBPF, I want to insert elements to a BPF_ARRAY, so is there any way to do like C++ push_back()
size()
function?
1 Answers
Yes and no. You can change elements in an array map (through the system call bpf(BPF_MAP_UPDATE_ELEM, ...)
), but you cannot dynamically resize your map at this time (see also this answer), although this might be possible in the future as some work in that direction has been presented before.
So for now you have a fixed-size array that you can update either from your BPF program, or from user space. For the user space side, in C++, you probably want to use libbpf to update your map. You have a low-level wrapper around the syscall in bpf.h, bpf_map_update_elem()
, as well as a higher-level function that works on map objects managed by the library, in libbpf.h: bpf_map__update_elem()
.
To answer to the question from your title: Yes you can obtain the map size, again through the bpf()
system call. Libbpf provides a wrapper: bpf_obj_get_info_by_fd()
, which fills a struct bpf_map_info
from which you can retrieve the size of the map.

- 8,284
- 1
- 24
- 52