Commit 74fc097d authored by Yonghong Song's avatar Yonghong Song Committed by Alexei Starovoitov
Browse files

tools/bpf: Support new uapi for map element bpf iterator



Previous commit adjusted kernel uapi for map
element bpf iterator. This patch adjusted libbpf API
due to uapi change. bpftool and bpf_iter selftests
are also changed accordingly.

Signed-off-by: default avatarYonghong Song <yhs@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200805055058.1457623-1-yhs@fb.com
parent 5e7b3020
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
static int do_pin(int argc, char **argv)
{
	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, iter_opts);
	union bpf_iter_link_info linfo;
	const char *objfile, *path;
	struct bpf_program *prog;
	struct bpf_object *obj;
@@ -36,6 +37,11 @@ static int do_pin(int argc, char **argv)
			map_fd = map_parse_fd(&argc, &argv);
			if (map_fd < 0)
				return -1;

			memset(&linfo, 0, sizeof(linfo));
			linfo.map.map_fd = map_fd;
			iter_opts.link_info = &linfo;
			iter_opts.link_info_len = sizeof(linfo);
		}
	}

@@ -57,9 +63,6 @@ static int do_pin(int argc, char **argv)
		goto close_obj;
	}

	if (map_fd >= 0)
		iter_opts.map_fd = map_fd;

	link = bpf_program__attach_iter(prog, &iter_opts);
	if (IS_ERR(link)) {
		err = PTR_ERR(link);
+8 −7
Original line number Diff line number Diff line
@@ -81,6 +81,12 @@ struct bpf_cgroup_storage_key {
	__u32	attach_type;		/* program attach type */
};

union bpf_iter_link_info {
	struct {
		__u32	map_fd;
	} map;
};

/* BPF syscall commands, see bpf(2) man-page for details. */
enum bpf_cmd {
	BPF_MAP_CREATE,
@@ -249,13 +255,6 @@ enum bpf_link_type {
	MAX_BPF_LINK_TYPE,
};

enum bpf_iter_link_info {
	BPF_ITER_LINK_UNSPEC = 0,
	BPF_ITER_LINK_MAP_FD = 1,

	MAX_BPF_ITER_LINK_INFO,
};

/* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
 *
 * NONE(default): No further bpf programs allowed in the subtree.
@@ -623,6 +622,8 @@ union bpf_attr {
		};
		__u32		attach_type;	/* attach type */
		__u32		flags;		/* extra flags */
		__aligned_u64	iter_info;	/* extra bpf_iter_link_info */
		__u32		iter_info_len;	/* iter_info length */
	} link_create;

	struct { /* struct used by BPF_LINK_UPDATE command */
+3 −0
Original line number Diff line number Diff line
@@ -599,6 +599,9 @@ int bpf_link_create(int prog_fd, int target_fd,
	attr.link_create.target_fd = target_fd;
	attr.link_create.attach_type = attach_type;
	attr.link_create.flags = OPTS_GET(opts, flags, 0);
	attr.link_create.iter_info =
		ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
	attr.link_create.iter_info_len = OPTS_GET(opts, iter_info_len, 0);

	return sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
}
+4 −1
Original line number Diff line number Diff line
@@ -168,11 +168,14 @@ LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
				enum bpf_attach_type type);

union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
struct bpf_link_create_opts {
	size_t sz; /* size of this struct for forward/backward compatibility */
	__u32 flags;
	union bpf_iter_link_info *iter_info;
	__u32 iter_info_len;
};
#define bpf_link_create_opts__last_field flags
#define bpf_link_create_opts__last_field iter_info_len

LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
			       enum bpf_attach_type attach_type,
+2 −4
Original line number Diff line number Diff line
@@ -8306,10 +8306,8 @@ bpf_program__attach_iter(struct bpf_program *prog,
	if (!OPTS_VALID(opts, bpf_iter_attach_opts))
		return ERR_PTR(-EINVAL);

	if (OPTS_HAS(opts, map_fd)) {
		target_fd = opts->map_fd;
		link_create_opts.flags = BPF_ITER_LINK_MAP_FD;
	}
	link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0);
	link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0);

	prog_fd = bpf_program__fd(prog);
	if (prog_fd < 0) {
Loading