Skip to contentSkip to conversation

libbpf: Deprecate bpf_{map,program}__{prev,next} APIs since v0.7

PATCH Applied Hengqi Chen 3 participants 7 messages Oct 3, 2021 – Oct 7, 2021

Subsystems (5)
Kselftest BPF BPF selftests libbpf +1 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover libbpf: Deprecate bpf_{map,program}__{prev,next} APIs since v0.7

bpf_{map,program}__{prev,next} don't follow the libbpf API naming convention. Deprecate them and replace them with a new set of APIs named bpf_object__{prev,next}_{program,map}.

v1->v2: [0]
* Addressed Andrii's comments

[0]: https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Hengqi Chen (2):
libbpf: Deprecate bpf_{map,program}__{prev,next} APIs since v0.7 selftests/bpf: Switch to new bpf_object__next_{map,program} APIs

 tools/lib/bpf/libbpf.c                        | 24 +++++++++++++
 tools/lib/bpf/libbpf.h                        | 35 ++++++++++++-------
 tools/lib/bpf/libbpf.map                      |  4 +++
 tools/testing/selftests/bpf/prog_tests/btf.c  |  2 +-
 .../selftests/bpf/prog_tests/fexit_bpf2bpf.c  |  6 ++--
 .../bpf/prog_tests/select_reuseport.c         |  2 +-
 .../selftests/bpf/prog_tests/tcp_rtt.c        |  2 +-
 tools/testing/selftests/bpf/xdping.c          |  2 +-
 8 files changed, 58 insertions(+), 19 deletions(-)

--
2.25.1

#1 lore raw

PATCH v2 1/2 libbpf: Deprecate bpf_{map,program}__{prev,next} APIs since v0.7

Deprecate bpf_{map,program}__{prev,next} APIs. Replace them with a new set of APIs named bpf_object__{prev,next}_{program,map} which follow the libbpf API naming convention.[0] No functionality changes.

---
 tools/lib/bpf/libbpf.c   | 24 ++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   | 35 +++++++++++++++++++++++------------
 tools/lib/bpf/libbpf.map |  4 ++++
 3 files changed, 51 insertions(+), 12 deletions(-)
tools/lib/bpf/libbpf.c +24 -0
index e23f1b6b9402..ecd5284c705d 100644
@@ -7798,6 +7798,12 @@ __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
 
 struct bpf_program *
 bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj)
+{
+	return bpf_object__next_program(obj, prev);
+}
+
+struct bpf_program *
+bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev)
 {
 	struct bpf_program *prog = prev;
 
@@ -7810,6 +7816,12 @@ bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj)
 
 struct bpf_program *
 bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj)
+{
+	return bpf_object__prev_program(obj, next);
+}
+
+struct bpf_program *
+bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *next)
 {
 	struct bpf_program *prog = next;
 
@@ -8742,6 +8754,12 @@ __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
 
 struct bpf_map *
 bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
+{
+	return bpf_object__next_map(obj, prev);
+}
+
+struct bpf_map *
+bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev)
 {
 	if (prev == NULL)
 		return obj->maps;
@@ -8751,6 +8769,12 @@ bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
 
 struct bpf_map *
 bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj)
+{
+	return bpf_object__prev_map(obj, next);
+}
+
+struct bpf_map *
+bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next)
 {
 	if (next == NULL) {
 		if (!obj->nr_maps)
tools/lib/bpf/libbpf.h +23 -12
index e35490c54eb3..8f5144bfa321 100644
@@ -189,16 +189,22 @@ LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
 
 /* Accessors of bpf_program */
 struct bpf_program;
-LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
-						 const struct bpf_object *obj);
+LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_program() instead")
+struct bpf_program *bpf_program__next(struct bpf_program *prog,
+				      const struct bpf_object *obj);
+LIBBPF_API struct bpf_program *
+bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
 
-#define bpf_object__for_each_program(pos, obj)		\
-	for ((pos) = bpf_program__next(NULL, (obj));	\
-	     (pos) != NULL;				\
-	     (pos) = bpf_program__next((pos), (obj)))
+#define bpf_object__for_each_program(pos, obj)			\
+	for ((pos) = bpf_object__next_program((obj), NULL);	\
+	     (pos) != NULL;					\
+	     (pos) = bpf_object__next_program((obj), (pos)))
 
-LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
-						 const struct bpf_object *obj);
+LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_program() instead")
+struct bpf_program *bpf_program__prev(struct bpf_program *prog,
+				      const struct bpf_object *obj);
+LIBBPF_API struct bpf_program *
+bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
 
 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
 
@@ -502,16 +508,21 @@ bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
 LIBBPF_API struct bpf_map *
 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
 
+LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead")
+struct bpf_map *bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
 LIBBPF_API struct bpf_map *
-bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
+bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
+
 #define bpf_object__for_each_map(pos, obj)		\
-	for ((pos) = bpf_map__next(NULL, (obj));	\
+	for ((pos) = bpf_object__next_map((obj), NULL);	\
 	     (pos) != NULL;				\
-	     (pos) = bpf_map__next((pos), (obj)))
+	     (pos) = bpf_object__next_map((obj), (pos)))
 #define bpf_map__for_each bpf_object__for_each_map
 
+LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_map() instead")
+struct bpf_map *bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
 LIBBPF_API struct bpf_map *
-bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
+bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
 
 /**
  * @brief **bpf_map__fd()** gets the file descriptor of the passed
tools/lib/bpf/libbpf.map +4 -0
index 9e649cf9e771..b9f711ca475f 100644
@@ -389,5 +389,9 @@ LIBBPF_0.5.0 {
 
 LIBBPF_0.6.0 {
 	global:
+		bpf_object__next_map;
+		bpf_object__next_program;
+		bpf_object__prev_map;
+		bpf_object__prev_program;
 		btf__add_tag;
 } LIBBPF_0.5.0;

--
2.25.1

reviewer Acked-by on v2 1/2
#2 lore raw
Quoted 6 lines · Hengqi Chen

Deprecate bpf_{map,program}__{prev,next} APIs. Replace them with a new set of APIs named bpf_object__{prev,next}_{program,map} which follow the libbpf API naming convention.[0] No functionality changes.

^^^ I guess we need "[0]" here?

Quoted 3 lines

---

Other than the nitpick above

#3 lore raw
Quoted 7 lines · Song Liu
Quoted 5 lines · Hengqi Chen

Deprecate bpf_{map,program}__{prev,next} APIs. Replace them with a new set of APIs named bpf_object__{prev,next}_{program,map} which follow the libbpf API naming convention.[0] No functionality changes.

Quoted 8 lines
Quoted 1 line

^^^ I guess we need "[0]" here?

Quoted 3 lines

---

Other than the nitpick above

Fixed that up while applying. There were also 4 more uses of bpf_program__next and bpf_map__next in bpftool and samples/bpf, I've fixed them up as well to not come back to this again. Hengqi, for future deprecations, please do grep for the entire Linux source code, not just selftests, there are a bunch of other tools and parts of the kernel that use libbpf APIs.

Applied to bpf-next, thanks.

Quoted 2 lines
#4 lore raw
Quoted 15 lines · Andrii Nakryiko
Quoted 14 lines · Song Liu
Quoted 6 lines · Hengqi Chen

Deprecate bpf_{map,program}__{prev,next} APIs. Replace them with a new set of APIs named bpf_object__{prev,next}_{program,map} which follow the libbpf API naming convention.[0] No functionality changes.

^^^ I guess we need "[0]" here?

Quoted 3 lines

---

Quoted 9 lines
Quoted 1 line

Other than the nitpick above

Fixed that up while applying. There were also 4 more uses of bpf_program__next and bpf_map__next in bpftool and samples/bpf, I've fixed them up as well to not come back to this again. Hengqi, for future deprecations, please do grep for the entire Linux source code, not just selftests, there are a bunch of other tools and parts of the kernel that use libbpf APIs.

Thanks. Will keep this in mind. :)

Quoted 4 lines

Applied to bpf-next, thanks.

Quoted 2 lines
#5 lore raw

PATCH v2 2/2 selftests/bpf: Switch to new bpf_object__next_{map,program} APIs

In reply to Hengqi Chen

Replace deprecated bpf_{map,program}__next APIs with newly added bpf_object__next_{map,program} APIs, so that no compilation warnings emit.

---
 tools/testing/selftests/bpf/prog_tests/btf.c              | 2 +-
 tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c    | 6 +++---
 tools/testing/selftests/bpf/prog_tests/select_reuseport.c | 2 +-
 tools/testing/selftests/bpf/prog_tests/tcp_rtt.c          | 2 +-
 tools/testing/selftests/bpf/xdping.c                      | 2 +-
 5 files changed, 7 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf.c +1 -1
index 9c85d7d27409..acd33d0cd5d9 100644
@@ -4511,7 +4511,7 @@ static void do_test_file(unsigned int test_num)
 	if (CHECK(err, "obj: %d", err))
 		return;
 
-	prog = bpf_program__next(NULL, obj);
+	prog = bpf_object__next_program(obj, NULL);
 	if (CHECK(!prog, "Cannot find bpf_prog")) {
 		err = -1;
 		goto done;
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +3 -3
index c7c1816899bf..2839f4270a26 100644
@@ -285,7 +285,7 @@ static void test_fmod_ret_freplace(void)
 	if (!ASSERT_OK_PTR(freplace_obj, "freplace_obj_open"))
 		goto out;
 
-	prog = bpf_program__next(NULL, freplace_obj);
+	prog = bpf_object__next_program(freplace_obj, NULL);
 	err = bpf_program__set_attach_target(prog, pkt_fd, NULL);
 	ASSERT_OK(err, "freplace__set_attach_target");
 
@@ -302,7 +302,7 @@ static void test_fmod_ret_freplace(void)
 		goto out;
 
 	attach_prog_fd = bpf_program__fd(prog);
-	prog = bpf_program__next(NULL, fmod_obj);
+	prog = bpf_object__next_program(fmod_obj, NULL);
 	err = bpf_program__set_attach_target(prog, attach_prog_fd, NULL);
 	ASSERT_OK(err, "fmod_ret_set_attach_target");
 
@@ -352,7 +352,7 @@ static void test_obj_load_failure_common(const char *obj_file,
 	if (!ASSERT_OK_PTR(obj, "obj_open"))
 		goto close_prog;
 
-	prog = bpf_program__next(NULL, obj);
+	prog = bpf_object__next_program(obj, NULL);
 	err = bpf_program__set_attach_target(prog, pkt_fd, NULL);
 	ASSERT_OK(err, "set_attach_target");
 
tools/testing/selftests/bpf/prog_tests/select_reuseport.c +1 -1
index 4efd337d6a3c..d40e9156c48d 100644
@@ -114,7 +114,7 @@ static int prepare_bpf_obj(void)
 	err = bpf_object__load(obj);
 	RET_ERR(err, "load bpf_object", "err:%d\n", err);
 
-	prog = bpf_program__next(NULL, obj);
+	prog = bpf_object__next_program(obj, NULL);
 	RET_ERR(!prog, "get first bpf_program", "!prog\n");
 	select_by_skb_data_prog = bpf_program__fd(prog);
 	RET_ERR(select_by_skb_data_prog < 0, "get prog fd",
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +1 -1
index d207e968e6b1..265b4fe33ec3 100644
@@ -109,7 +109,7 @@ static int run_test(int cgroup_fd, int server_fd)
 		return -1;
 	}
 
-	map = bpf_map__next(NULL, obj);
+	map = bpf_object__next_map(obj, NULL);
 	map_fd = bpf_map__fd(map);
 
 	err = bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS, 0);
tools/testing/selftests/bpf/xdping.c +1 -1
index 79a3453dab25..30f12637f4e4 100644
@@ -187,7 +187,7 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	map = bpf_map__next(NULL, obj);
+	map = bpf_object__next_map(obj, NULL);
 	if (map)
 		map_fd = bpf_map__fd(map);
 	if (!map || map_fd < 0) {

--
2.25.1

reviewer Acked-by on v2 2/2
#6 lore raw

Re: [PATCH bpf-next 2/2 v2] selftests/bpf: Switch to new bpf_object__next_{map,program} APIs

Quoted 6 lines · Hengqi Chen

Replace deprecated bpf_{map,program}__next APIs with newly added bpf_object__next_{map,program} APIs, so that no compilation warnings emit.

Quoted 93 lines
---
 tools/testing/selftests/bpf/prog_tests/btf.c              | 2 +-
 tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c    | 6 +++---
 tools/testing/selftests/bpf/prog_tests/select_reuseport.c | 2 +-
 tools/testing/selftests/bpf/prog_tests/tcp_rtt.c          | 2 +-
 tools/testing/selftests/bpf/xdping.c                      | 2 +-
 5 files changed, 7 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf.c +1 -1
index 9c85d7d27409..acd33d0cd5d9 100644
@@ -4511,7 +4511,7 @@ static void do_test_file(unsigned int test_num)
        if (CHECK(err, "obj: %d", err))
                return;
 
-       prog = bpf_program__next(NULL, obj);
+       prog = bpf_object__next_program(obj, NULL);
        if (CHECK(!prog, "Cannot find bpf_prog")) {
                err = -1;
                goto done;
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +3 -3
index c7c1816899bf..2839f4270a26 100644
@@ -285,7 +285,7 @@ static void test_fmod_ret_freplace(void)
        if (!ASSERT_OK_PTR(freplace_obj, "freplace_obj_open"))
                goto out;
 
-       prog = bpf_program__next(NULL, freplace_obj);
+       prog = bpf_object__next_program(freplace_obj, NULL);
        err = bpf_program__set_attach_target(prog, pkt_fd, NULL);
        ASSERT_OK(err, "freplace__set_attach_target");
 
@@ -302,7 +302,7 @@ static void test_fmod_ret_freplace(void)
                goto out;
 
        attach_prog_fd = bpf_program__fd(prog);
-       prog = bpf_program__next(NULL, fmod_obj);
+       prog = bpf_object__next_program(fmod_obj, NULL);
        err = bpf_program__set_attach_target(prog, attach_prog_fd, NULL);
        ASSERT_OK(err, "fmod_ret_set_attach_target");
 
@@ -352,7 +352,7 @@ static void test_obj_load_failure_common(const char *obj_file,
        if (!ASSERT_OK_PTR(obj, "obj_open"))
                goto close_prog;
 
-       prog = bpf_program__next(NULL, obj);
+       prog = bpf_object__next_program(obj, NULL);
        err = bpf_program__set_attach_target(prog, pkt_fd, NULL);
        ASSERT_OK(err, "set_attach_target");
 
tools/testing/selftests/bpf/prog_tests/select_reuseport.c +1 -1
index 4efd337d6a3c..d40e9156c48d 100644
@@ -114,7 +114,7 @@ static int prepare_bpf_obj(void)
        err = bpf_object__load(obj);
        RET_ERR(err, "load bpf_object", "err:%d\n", err);
 
-       prog = bpf_program__next(NULL, obj);
+       prog = bpf_object__next_program(obj, NULL);
        RET_ERR(!prog, "get first bpf_program", "!prog\n");
        select_by_skb_data_prog = bpf_program__fd(prog);
        RET_ERR(select_by_skb_data_prog < 0, "get prog fd",
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +1 -1
index d207e968e6b1..265b4fe33ec3 100644
@@ -109,7 +109,7 @@ static int run_test(int cgroup_fd, int server_fd)
                return -1;
        }
 
-       map = bpf_map__next(NULL, obj);
+       map = bpf_object__next_map(obj, NULL);
        map_fd = bpf_map__fd(map);
 
        err = bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS, 0);
tools/testing/selftests/bpf/xdping.c +1 -2
index 79a3453dab25..30f12637f4e4 100644
@@ -187,7 +187,7 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       map = bpf_map__next(NULL, obj);
+       map = bpf_object__next_map(obj, NULL);
        if (map)
                map_fd = bpf_map__fd(map);
        if (!map || map_fd < 0) {
--

2.25.1