mirror of
https://github.com/netdata/libbpf.git
synced 2026-04-01 22:29:06 +08:00
tools/libbpf: Add support for bpf map element iterator
Add map_fd to bpf_iter_attach_opts and flags to bpf_link_create_opts. Later on, bpftool or selftest will be able to create a bpf map element iterator by passing map_fd to the kernel during link creation time. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200723184117.590673-1-yhs@fb.com
This commit is contained in:
committed by
Andrii Nakryiko
parent
b1720407ff
commit
5efd8395ef
@@ -598,6 +598,7 @@ int bpf_link_create(int prog_fd, int target_fd,
|
|||||||
attr.link_create.prog_fd = prog_fd;
|
attr.link_create.prog_fd = prog_fd;
|
||||||
attr.link_create.target_fd = target_fd;
|
attr.link_create.target_fd = target_fd;
|
||||||
attr.link_create.attach_type = attach_type;
|
attr.link_create.attach_type = attach_type;
|
||||||
|
attr.link_create.flags = OPTS_GET(opts, flags, 0);
|
||||||
|
|
||||||
return sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
|
return sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -170,8 +170,9 @@ LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
|
|||||||
|
|
||||||
struct bpf_link_create_opts {
|
struct bpf_link_create_opts {
|
||||||
size_t sz; /* size of this struct for forward/backward compatibility */
|
size_t sz; /* size of this struct for forward/backward compatibility */
|
||||||
|
__u32 flags;
|
||||||
};
|
};
|
||||||
#define bpf_link_create_opts__last_field sz
|
#define bpf_link_create_opts__last_field flags
|
||||||
|
|
||||||
LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
|
LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
|
||||||
enum bpf_attach_type attach_type,
|
enum bpf_attach_type attach_type,
|
||||||
|
|||||||
10
src/libbpf.c
10
src/libbpf.c
@@ -8282,13 +8282,20 @@ struct bpf_link *
|
|||||||
bpf_program__attach_iter(struct bpf_program *prog,
|
bpf_program__attach_iter(struct bpf_program *prog,
|
||||||
const struct bpf_iter_attach_opts *opts)
|
const struct bpf_iter_attach_opts *opts)
|
||||||
{
|
{
|
||||||
|
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
|
||||||
char errmsg[STRERR_BUFSIZE];
|
char errmsg[STRERR_BUFSIZE];
|
||||||
struct bpf_link *link;
|
struct bpf_link *link;
|
||||||
int prog_fd, link_fd;
|
int prog_fd, link_fd;
|
||||||
|
__u32 target_fd = 0;
|
||||||
|
|
||||||
if (!OPTS_VALID(opts, bpf_iter_attach_opts))
|
if (!OPTS_VALID(opts, bpf_iter_attach_opts))
|
||||||
return ERR_PTR(-EINVAL);
|
return ERR_PTR(-EINVAL);
|
||||||
|
|
||||||
|
if (OPTS_HAS(opts, map_fd)) {
|
||||||
|
target_fd = opts->map_fd;
|
||||||
|
link_create_opts.flags = BPF_ITER_LINK_MAP_FD;
|
||||||
|
}
|
||||||
|
|
||||||
prog_fd = bpf_program__fd(prog);
|
prog_fd = bpf_program__fd(prog);
|
||||||
if (prog_fd < 0) {
|
if (prog_fd < 0) {
|
||||||
pr_warn("program '%s': can't attach before loaded\n",
|
pr_warn("program '%s': can't attach before loaded\n",
|
||||||
@@ -8301,7 +8308,8 @@ bpf_program__attach_iter(struct bpf_program *prog,
|
|||||||
return ERR_PTR(-ENOMEM);
|
return ERR_PTR(-ENOMEM);
|
||||||
link->detach = &bpf_link__detach_fd;
|
link->detach = &bpf_link__detach_fd;
|
||||||
|
|
||||||
link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_ITER, NULL);
|
link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER,
|
||||||
|
&link_create_opts);
|
||||||
if (link_fd < 0) {
|
if (link_fd < 0) {
|
||||||
link_fd = -errno;
|
link_fd = -errno;
|
||||||
free(link);
|
free(link);
|
||||||
|
|||||||
@@ -264,8 +264,9 @@ LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);
|
|||||||
|
|
||||||
struct bpf_iter_attach_opts {
|
struct bpf_iter_attach_opts {
|
||||||
size_t sz; /* size of this struct for forward/backward compatibility */
|
size_t sz; /* size of this struct for forward/backward compatibility */
|
||||||
|
__u32 map_fd;
|
||||||
};
|
};
|
||||||
#define bpf_iter_attach_opts__last_field sz
|
#define bpf_iter_attach_opts__last_field map_fd
|
||||||
|
|
||||||
LIBBPF_API struct bpf_link *
|
LIBBPF_API struct bpf_link *
|
||||||
bpf_program__attach_iter(struct bpf_program *prog,
|
bpf_program__attach_iter(struct bpf_program *prog,
|
||||||
|
|||||||
Reference in New Issue
Block a user