I created a child process to perform some operation on a bpf map object, and I need to get the btf information about the object. I can get the map_fd of that object, but I cannot modify the child process code, which means I cannot pin that object. Also, I cannot get the id.
Currently, I try to iterate all the ids by using bpf_map_get_next_id and get the corresponding fd by using bpf_map_get_fd_by_id, where fd is equal to the fd I got from the child process. But I don't know whether the same fd refers to the same bpf map object under different processes.
Is there any way to get id by fd in BPF? Are the file descriptors of BPF objects the same in different processes? Is BPF id the unique identifier for BPF object?
CodePudding user response:
Is there any way to get id by fd in BPF?
Yes, there is. You can use the int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
function of libbpf which wraps the BPF_OBJ_GET_INFO_BY_FD command. The info
pointer can be struct bpf_prog_info
, struct bpf_map_info
, struct bpf_btf_info
, or struct bpf_link_info
depending on which object you would like to know the id of(all of these structs have an id
field).
Are the file descriptors of BPF objects the same in different processes?
If the FD existed before your the child forked from the parent, they are the same. If you opened the FD in the parent and child separately(after the fork) they will be different.
Is BPF id the unique identifier for BPF object?
Yes, the BPF ids are unique for that object type(prog,map,btf,link)