I'd like to perform actions after a value is inserted to the specific map, using fentry/htab_map_update_elem
hook. How can I check if struct bpf_map* map
is the map that declared in BPF code?
For example,
// kernel BPF program
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 128);
__type(key, __u32);
__type(value, __u32);
} mymap SEC(".maps");
SEC("fentry/htab_map_update_elem")
int mymap_prog(struct bpf_map* map, void* key, void* value, u64 map_flags) {
// Here, how to check if map == mymap?
}
// user program
int main() {
struct mymap_bpf* skel;
skel = ...
// Update mymap and trigger map update hook
bpf_map__update_elem(skel->maps.mymap, ...);
(Skeleton header file is generated with bpftool)
Thank you in advance.
TL;DR. You can identify the map by checking its ID, with map->id
and bpf_map_get_info_by_fd
.
On the BPF side:
SEC("fentry/htab_map_update_elem")
int mymap_prog(struct bpf_map* map, void* key, void* value, u64 map_flags) {
if (map->id == MY_MAP_ID)
...
}
On the libbpf side:
struct bpf_map_info info_m = {};
ret = bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.mymap),
&info_m, sizeof(info_m));
// The map ID is at info_m.id.
Then you can either pass info_m.id
to the BPF program via a map or you can hardcode it as MY_MAP_ID
as I did above.