Skip to contentSkip to conversation

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

PATCH In review Ondrej Mosnacek 1 participant 2 messages Feb 18, 2022 – Feb 21, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/bpf: use new API if version >= 0.6

In version 0.7 the old API is deprecated and without this patch the testsuite fails to build with -Werror with this version.

---
 tests/bpf/bpf_common.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
tests/bpf/bpf_common.c +23 -4
index f499034..88aae2f 100644
@@ -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",
Signature · 1 line
2.35.1
#1 lore raw
Quoted 48 lines · Ondrej Mosnacek

In version 0.7 the old API is deprecated and without this patch the testsuite fails to build with -Werror with this version.

---
 tests/bpf/bpf_common.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
tests/bpf/bpf_common.c +16 -3
index f499034..88aae2f 100644
@@ -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;
-

tests/bpf/bpf_common.c int create_bpf_map(void) · line 45

Quoted 10 lines
tests/bpf/bpf_common.c +4 -1
        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

Quoted 11 lines
tests/bpf/bpf_common.c +3 -1
+                               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

Signature · 3 lines
Ondrej Mosnacek
Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.