diff mbox series

[testsuite] tests/bpf: use new API if version >= 0.6

Message ID 20220218210815.107961-1-omosnace@redhat.com (mailing list archive)
State Accepted
Headers show
Series [testsuite] tests/bpf: use new API if version >= 0.6 | expand

Commit Message

Ondrej Mosnacek Feb. 18, 2022, 9:08 p.m. UTC
In version 0.7 the old API is deprecated and without this patch the
testsuite fails to build with -Werror with this version.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
---
 tests/bpf/bpf_common.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

Comments

Ondrej Mosnacek Feb. 21, 2022, 6:01 p.m. UTC | #1
On Fri, Feb 18, 2022 at 10:08 PM Ondrej Mosnacek <omosnace@redhat.com> wrote:
> In version 0.7 the old API is deprecated and without this patch the
> testsuite fails to build with -Werror with this version.
>
> Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
> ---
>  tests/bpf/bpf_common.c | 27 +++++++++++++++++++++++----
>  1 file changed, 23 insertions(+), 4 deletions(-)
>
> diff --git a/tests/bpf/bpf_common.c b/tests/bpf/bpf_common.c
> index f499034..88aae2f 100644
> --- a/tests/bpf/bpf_common.c
> +++ b/tests/bpf/bpf_common.c
> @@ -1,12 +1,28 @@
>  #include "bpf_common.h"
>
> +/*
> + * v0.7 deprecates some functions in favor of a new API (introduced in v0.6).
> + * Make this work with both to satisfy -Werror (and older distros).
> + */
> +#if defined(LIBBPF_MAJOR_VERSION)
> +#if LIBBPF_MAJOR_VERSION > 0 || (LIBBPF_MAJOR_VERSION == 0 && \
> +       LIBBPF_MINOR_VERSION > 6)
> +#define USE_NEW_API
> +#endif
> +#endif
> +
>  int create_bpf_map(void)
>  {
>         int map_fd, key;
>         long long value = 0;
>
> +#ifdef USE_NEW_API
> +       map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(key),
> +                               sizeof(value), 256, NULL);
> +#else
>         map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key),
>                                 sizeof(value), 256, 0);
> +#endif
>         if (map_fd < 0) {
>                 fprintf(stderr, "Failed to create BPF map: %s\n",
>                         strerror(errno));
> @@ -18,17 +34,20 @@ int create_bpf_map(void)
>
>  int create_bpf_prog(void)
>  {
> -       int prog_fd;
> -       size_t insns_cnt;
> -
>         struct bpf_insn prog[] = {
>                 BPF_MOV64_IMM(BPF_REG_0, 1),
>                 BPF_EXIT_INSN(),
>         };
> -       insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
> +       size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
> +       int prog_fd;
>
> +#ifdef USE_NEW_API
> +       prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPF",

Now merged, with the "General Protection Fault license" typo fixed :)

https://github.com/SELinuxProject/selinux-testsuite/commit/92c29366dffad9e8b1ef621d4fc07816ed121efe

> +                               prog, insns_cnt, NULL);
> +#else
>         prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
>                                    insns_cnt, "GPL", 0, NULL, 0);
> +#endif
>
>         if (prog_fd < 0) {
>                 fprintf(stderr, "Failed to load BPF prog: %s\n",
> --
> 2.35.1
>
diff mbox series

Patch

diff --git a/tests/bpf/bpf_common.c b/tests/bpf/bpf_common.c
index f499034..88aae2f 100644
--- a/tests/bpf/bpf_common.c
+++ b/tests/bpf/bpf_common.c
@@ -1,12 +1,28 @@ 
 #include "bpf_common.h"
 
+/*
+ * v0.7 deprecates some functions in favor of a new API (introduced in v0.6).
+ * Make this work with both to satisfy -Werror (and older distros).
+ */
+#if defined(LIBBPF_MAJOR_VERSION)
+#if LIBBPF_MAJOR_VERSION > 0 || (LIBBPF_MAJOR_VERSION == 0 && \
+	LIBBPF_MINOR_VERSION > 6)
+#define USE_NEW_API
+#endif
+#endif
+
 int create_bpf_map(void)
 {
 	int map_fd, key;
 	long long value = 0;
 
+#ifdef USE_NEW_API
+	map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(key),
+				sizeof(value), 256, NULL);
+#else
 	map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key),
 				sizeof(value), 256, 0);
+#endif
 	if (map_fd < 0) {
 		fprintf(stderr, "Failed to create BPF map: %s\n",
 			strerror(errno));
@@ -18,17 +34,20 @@  int create_bpf_map(void)
 
 int create_bpf_prog(void)
 {
-	int prog_fd;
-	size_t insns_cnt;
-
 	struct bpf_insn prog[] = {
 		BPF_MOV64_IMM(BPF_REG_0, 1),
 		BPF_EXIT_INSN(),
 	};
-	insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	int prog_fd;
 
+#ifdef USE_NEW_API
+	prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPF",
+				prog, insns_cnt, NULL);
+#else
 	prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
 				   insns_cnt, "GPL", 0, NULL, 0);
+#endif
 
 	if (prog_fd < 0) {
 		fprintf(stderr, "Failed to load BPF prog: %s\n",