Extend the libbpf API to provide support for getting the number of entries in a map. Co-developed-by: Nick Zavaritsky <mejedi@xxxxxxxxx> Signed-off-by: Nick Zavaritsky <mejedi@xxxxxxxxx> Signed-off-by: Charalampos Stylianopoulos <charalampos.stylianopoulos@xxxxxxxxx> --- tools/include/uapi/linux/bpf.h | 17 +++++++++++++++++ tools/lib/bpf/bpf.c | 16 ++++++++++++++++ tools/lib/bpf/bpf.h | 2 ++ tools/lib/bpf/libbpf.map | 1 + 4 files changed, 36 insertions(+) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 2acf9b336371..f5c7adea1387 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -903,6 +903,17 @@ union bpf_iter_link_info { * A new file descriptor (a nonnegative integer), or -1 if an * error occurred (in which case, *errno* is set appropriately). * + * BPF_MAP_GET_NUM_ENTRIES + * Description + * Get the number of entries currently present in the map. + * + * This operation is supported only for a subset of maps. If the + * map is not supported, the operation returns **EOPNOTSUPP**. + * + * Return + * Returns zero on success. On error, -1 is returned and *errno* + * is set appropriately. + * * NOTES * eBPF objects (maps and programs) can be shared between processes. * @@ -958,6 +969,7 @@ enum bpf_cmd { BPF_LINK_DETACH, BPF_PROG_BIND_MAP, BPF_TOKEN_CREATE, + BPF_MAP_GET_NUM_ENTRIES, __MAX_BPF_CMD, }; @@ -1837,6 +1849,11 @@ union bpf_attr { __u32 bpffs_fd; } token_create; + struct { /* struct used by BPF_MAP_GET_NUM_ENTRIES command*/ + __u32 map_fd; + __u32 num_entries; + } map_get_num_entries; + } __attribute__((aligned(8))); /* The description below is an attempt at providing documentation to eBPF diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 359f73ead613..c91f43690624 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -1330,3 +1330,19 @@ int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts) fd = sys_bpf_fd(BPF_TOKEN_CREATE, &attr, attr_sz); return libbpf_err_errno(fd); } + +int bpf_map_get_num_entries(int fd, unsigned int *num_entries) +{ + const size_t attr_sz = offsetofend(union bpf_attr, map_get_num_entries); + union bpf_attr attr; + int ret; + + memset(&attr, 0, attr_sz); + attr.map_get_num_entries.map_fd = fd; + + ret = sys_bpf(BPF_MAP_GET_NUM_ENTRIES, &attr, attr_sz); + if (!ret) + *num_entries = attr.map_get_num_entries.num_entries; + + return libbpf_err_errno(ret); +} diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 435da95d2058..efa5a092acc9 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -653,6 +653,8 @@ struct bpf_prog_bind_opts { LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd, const struct bpf_prog_bind_opts *opts); +LIBBPF_API int bpf_map_get_num_entries(int fd, unsigned int *num_entries); + struct bpf_test_run_opts { size_t sz; /* size of this struct for forward/backward compatibility */ const void *data_in; /* optional */ diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index a8b2936a1646..63dbad55cc2d 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -436,4 +436,5 @@ LIBBPF_1.6.0 { bpf_linker__add_buf; bpf_linker__add_fd; bpf_linker__new_fd; + bpf_map_get_num_entries; } LIBBPF_1.5.0; -- 2.43.0