Skip to content

Commit

Permalink
Add a few introspection helpers
Browse files Browse the repository at this point in the history
This patch adds the following helpers to libbpf:
int bpf_prog_get_next_id(uint32_t start_id, uint32_t *next_id);
int bpf_prog_get_fd_by_id(uint32_t id);
int bpf_map_get_fd_by_id(uint32_t id);

It also changes the info_len arg of the existing bpf_obj_get_info()
from int to uint32_t.

Signed-off-by: Martin KaFai Lau <[email protected]>
  • Loading branch information
iamkafai committed Oct 24, 2017
1 parent df36816 commit 3c24ad9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
37 changes: 36 additions & 1 deletion src/cc/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ static void bpf_print_hints(char *log)
}
#define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))

int bpf_obj_get_info(int prog_map_fd, void *info, int *info_len)
int bpf_obj_get_info(int prog_map_fd, void *info, uint32_t *info_len)
{
union bpf_attr attr;
int err;
Expand Down Expand Up @@ -1046,3 +1046,38 @@ int bpf_obj_get(const char *pathname)

return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr));
}

int bpf_prog_get_next_id(uint32_t start_id, uint32_t *next_id)
{
union bpf_attr attr;
int err;

memset(&attr, 0, sizeof(attr));
attr.start_id = start_id;

err = syscall(__NR_bpf, BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr));
if (!err)
*next_id = attr.next_id;

return err;
}

int bpf_prog_get_fd_by_id(uint32_t id)
{
union bpf_attr attr;

memset(&attr, 0, sizeof(attr));
attr.prog_id = id;

return syscall(__NR_bpf, BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
}

int bpf_map_get_fd_by_id(uint32_t id)
{
union bpf_attr attr;

memset(&attr, 0, sizeof(attr));
attr.map_id = id;

return syscall(__NR_bpf, BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
}
5 changes: 4 additions & 1 deletion src/cc/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ int bpf_close_perf_event_fd(int fd);

int bpf_obj_pin(int fd, const char *pathname);
int bpf_obj_get(const char *pathname);
int bpf_obj_get_info(int prog_map_fd, void *info, int *info_len);
int bpf_obj_get_info(int prog_map_fd, void *info, uint32_t *info_len);
int bpf_prog_compute_tag(const struct bpf_insn *insns, int prog_len,
unsigned long long *tag);
int bpf_prog_get_tag(int fd, unsigned long long *tag);
int bpf_prog_get_next_id(uint32_t start_id, uint32_t *next_id);
int bpf_prog_get_fd_by_id(uint32_t id);
int bpf_map_get_fd_by_id(uint32_t id);

#define LOG_BUF_SIZE 65536

Expand Down

0 comments on commit 3c24ad9

Please sign in to comment.