Skip to content

Commit 74fc097

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
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: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20200805055058.1457623-1-yhs@fb.com
1 parent 5e7b302 commit 74fc097

7 files changed

Lines changed: 58 additions & 25 deletions

File tree

‎tools/bpf/bpftool/iter.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
static int do_pin(int argc, char **argv)
1212
{
1313
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, iter_opts);
14+
union bpf_iter_link_info linfo;
1415
const char *objfile, *path;
1516
struct bpf_program *prog;
1617
struct bpf_object *obj;
@@ -36,6 +37,11 @@ static int do_pin(int argc, char **argv)
3637
map_fd = map_parse_fd(&argc, &argv);
3738
if (map_fd < 0)
3839
return -1;
40+
41+
memset(&linfo, 0, sizeof(linfo));
42+
linfo.map.map_fd = map_fd;
43+
iter_opts.link_info = &linfo;
44+
iter_opts.link_info_len = sizeof(linfo);
3945
}
4046
}
4147

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

60-
if (map_fd >= 0)
61-
iter_opts.map_fd = map_fd;
62-
6366
link = bpf_program__attach_iter(prog, &iter_opts);
6467
if (IS_ERR(link)) {
6568
err = PTR_ERR(link);

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ struct bpf_cgroup_storage_key {
8181
__u32 attach_type; /* program attach type */
8282
};
8383

84+
union bpf_iter_link_info {
85+
struct {
86+
__u32 map_fd;
87+
} map;
88+
};
89+
8490
/* BPF syscall commands, see bpf(2) man-page for details. */
8591
enum bpf_cmd {
8692
BPF_MAP_CREATE,
@@ -249,13 +255,6 @@ enum bpf_link_type {
249255
MAX_BPF_LINK_TYPE,
250256
};
251257

252-
enum bpf_iter_link_info {
253-
BPF_ITER_LINK_UNSPEC = 0,
254-
BPF_ITER_LINK_MAP_FD = 1,
255-
256-
MAX_BPF_ITER_LINK_INFO,
257-
};
258-
259258
/* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
260259
*
261260
* NONE(default): No further bpf programs allowed in the subtree.
@@ -623,6 +622,8 @@ union bpf_attr {
623622
};
624623
__u32 attach_type; /* attach type */
625624
__u32 flags; /* extra flags */
625+
__aligned_u64 iter_info; /* extra bpf_iter_link_info */
626+
__u32 iter_info_len; /* iter_info length */
626627
} link_create;
627628

628629
struct { /* struct used by BPF_LINK_UPDATE command */

‎tools/lib/bpf/bpf.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,9 @@ int bpf_link_create(int prog_fd, int target_fd,
599599
attr.link_create.target_fd = target_fd;
600600
attr.link_create.attach_type = attach_type;
601601
attr.link_create.flags = OPTS_GET(opts, flags, 0);
602+
attr.link_create.iter_info =
603+
ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
604+
attr.link_create.iter_info_len = OPTS_GET(opts, iter_info_len, 0);
602605

603606
return sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
604607
}

‎tools/lib/bpf/bpf.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,14 @@ LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
168168
LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
169169
enum bpf_attach_type type);
170170

171+
union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
171172
struct bpf_link_create_opts {
172173
size_t sz; /* size of this struct for forward/backward compatibility */
173174
__u32 flags;
175+
union bpf_iter_link_info *iter_info;
176+
__u32 iter_info_len;
174177
};
175-
#define bpf_link_create_opts__last_field flags
178+
#define bpf_link_create_opts__last_field iter_info_len
176179

177180
LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
178181
enum bpf_attach_type attach_type,

‎tools/lib/bpf/libbpf.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8306,10 +8306,8 @@ bpf_program__attach_iter(struct bpf_program *prog,
83068306
if (!OPTS_VALID(opts, bpf_iter_attach_opts))
83078307
return ERR_PTR(-EINVAL);
83088308

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

83148312
prog_fd = bpf_program__fd(prog);
83158313
if (prog_fd < 0) {

‎tools/lib/bpf/libbpf.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,10 @@ LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);
267267

268268
struct bpf_iter_attach_opts {
269269
size_t sz; /* size of this struct for forward/backward compatibility */
270-
__u32 map_fd;
270+
union bpf_iter_link_info *link_info;
271+
__u32 link_info_len;
271272
};
272-
#define bpf_iter_attach_opts__last_field map_fd
273+
#define bpf_iter_attach_opts__last_field link_info_len
273274

274275
LIBBPF_API struct bpf_link *
275276
bpf_program__attach_iter(struct bpf_program *prog,

‎tools/testing/selftests/bpf/prog_tests/bpf_iter.c‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ static void test_bpf_hash_map(void)
468468
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
469469
struct bpf_iter_bpf_hash_map *skel;
470470
int err, i, len, map_fd, iter_fd;
471+
union bpf_iter_link_info linfo;
471472
__u64 val, expected_val = 0;
472473
struct bpf_link *link;
473474
struct key_t {
@@ -490,13 +491,16 @@ static void test_bpf_hash_map(void)
490491
goto out;
491492

492493
/* iterator with hashmap2 and hashmap3 should fail */
493-
opts.map_fd = bpf_map__fd(skel->maps.hashmap2);
494+
memset(&linfo, 0, sizeof(linfo));
495+
linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap2);
496+
opts.link_info = &linfo;
497+
opts.link_info_len = sizeof(linfo);
494498
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
495499
if (CHECK(!IS_ERR(link), "attach_iter",
496500
"attach_iter for hashmap2 unexpected succeeded\n"))
497501
goto out;
498502

499-
opts.map_fd = bpf_map__fd(skel->maps.hashmap3);
503+
linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap3);
500504
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
501505
if (CHECK(!IS_ERR(link), "attach_iter",
502506
"attach_iter for hashmap3 unexpected succeeded\n"))
@@ -519,7 +523,7 @@ static void test_bpf_hash_map(void)
519523
goto out;
520524
}
521525

522-
opts.map_fd = map_fd;
526+
linfo.map.map_fd = map_fd;
523527
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
524528
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n"))
525529
goto out;
@@ -562,6 +566,7 @@ static void test_bpf_percpu_hash_map(void)
562566
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
563567
struct bpf_iter_bpf_percpu_hash_map *skel;
564568
int err, i, j, len, map_fd, iter_fd;
569+
union bpf_iter_link_info linfo;
565570
__u32 expected_val = 0;
566571
struct bpf_link *link;
567572
struct key_t {
@@ -606,7 +611,10 @@ static void test_bpf_percpu_hash_map(void)
606611
goto out;
607612
}
608613

609-
opts.map_fd = map_fd;
614+
memset(&linfo, 0, sizeof(linfo));
615+
linfo.map.map_fd = map_fd;
616+
opts.link_info = &linfo;
617+
opts.link_info_len = sizeof(linfo);
610618
link = bpf_program__attach_iter(skel->progs.dump_bpf_percpu_hash_map, &opts);
611619
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n"))
612620
goto out;
@@ -649,6 +657,7 @@ static void test_bpf_array_map(void)
649657
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
650658
__u32 expected_key = 0, res_first_key;
651659
struct bpf_iter_bpf_array_map *skel;
660+
union bpf_iter_link_info linfo;
652661
int err, i, map_fd, iter_fd;
653662
struct bpf_link *link;
654663
char buf[64] = {};
@@ -673,7 +682,10 @@ static void test_bpf_array_map(void)
673682
goto out;
674683
}
675684

676-
opts.map_fd = map_fd;
685+
memset(&linfo, 0, sizeof(linfo));
686+
linfo.map.map_fd = map_fd;
687+
opts.link_info = &linfo;
688+
opts.link_info_len = sizeof(linfo);
677689
link = bpf_program__attach_iter(skel->progs.dump_bpf_array_map, &opts);
678690
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n"))
679691
goto out;
@@ -730,6 +742,7 @@ static void test_bpf_percpu_array_map(void)
730742
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
731743
struct bpf_iter_bpf_percpu_array_map *skel;
732744
__u32 expected_key = 0, expected_val = 0;
745+
union bpf_iter_link_info linfo;
733746
int err, i, j, map_fd, iter_fd;
734747
struct bpf_link *link;
735748
char buf[64];
@@ -765,7 +778,10 @@ static void test_bpf_percpu_array_map(void)
765778
goto out;
766779
}
767780

768-
opts.map_fd = map_fd;
781+
memset(&linfo, 0, sizeof(linfo));
782+
linfo.map.map_fd = map_fd;
783+
opts.link_info = &linfo;
784+
opts.link_info_len = sizeof(linfo);
769785
link = bpf_program__attach_iter(skel->progs.dump_bpf_percpu_array_map, &opts);
770786
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n"))
771787
goto out;
@@ -803,6 +819,7 @@ static void test_bpf_sk_storage_map(void)
803819
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
804820
int err, i, len, map_fd, iter_fd, num_sockets;
805821
struct bpf_iter_bpf_sk_storage_map *skel;
822+
union bpf_iter_link_info linfo;
806823
int sock_fd[3] = {-1, -1, -1};
807824
__u32 val, expected_val = 0;
808825
struct bpf_link *link;
@@ -829,7 +846,10 @@ static void test_bpf_sk_storage_map(void)
829846
goto out;
830847
}
831848

832-
opts.map_fd = map_fd;
849+
memset(&linfo, 0, sizeof(linfo));
850+
linfo.map.map_fd = map_fd;
851+
opts.link_info = &linfo;
852+
opts.link_info_len = sizeof(linfo);
833853
link = bpf_program__attach_iter(skel->progs.dump_bpf_sk_storage_map, &opts);
834854
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n"))
835855
goto out;
@@ -871,14 +891,18 @@ static void test_rdonly_buf_out_of_bound(void)
871891
{
872892
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
873893
struct bpf_iter_test_kern5 *skel;
894+
union bpf_iter_link_info linfo;
874895
struct bpf_link *link;
875896

876897
skel = bpf_iter_test_kern5__open_and_load();
877898
if (CHECK(!skel, "bpf_iter_test_kern5__open_and_load",
878899
"skeleton open_and_load failed\n"))
879900
return;
880901

881-
opts.map_fd = bpf_map__fd(skel->maps.hashmap1);
902+
memset(&linfo, 0, sizeof(linfo));
903+
linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap1);
904+
opts.link_info = &linfo;
905+
opts.link_info_len = sizeof(linfo);
882906
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
883907
if (CHECK(!IS_ERR(link), "attach_iter", "unexpected success\n"))
884908
bpf_link__destroy(link);

0 commit comments

Comments
 (0)