[PATCH/RFC v2 bpf-next 11/19] libbpf: new global variable "libbpf_test_mode"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Enable BPF_F_TEST_RND_HI32 for all existing bpf selftests or other
independent tests could involve quite a few changes to make sure all bpf
prog load places has BPF_F_TEST_RND_HI32 set.

Given most of the tests are using libbpf, this patch introduces a new
global variable "libbpf_test_mode" into libbpf, once which is set, all bpf
prog load issued through libbpf will have BPF_F_TEST_RND_HI32 set
automatically, this could minimize changes required from testsuite.

The other way might be introducing new load function like
"bpf_prog_test_load", which will set BPF_F_TEST_RND_HI32. But there are
several prog load APIs, and we need minor changes on some parameters.

The global variable approach seems to be a proper first step for easy
testsuite porting.

Signed-off-by: Jiong Wang <jiong.wang@xxxxxxxxxxxxx>
---
 tools/lib/bpf/bpf.c      | 4 ++++
 tools/lib/bpf/libbpf.c   | 2 ++
 tools/lib/bpf/libbpf.h   | 2 ++
 tools/lib/bpf/libbpf.map | 1 +
 4 files changed, 9 insertions(+)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index dababce..0795a85 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -254,6 +254,8 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
 	if (load_attr->name)
 		memcpy(attr.prog_name, load_attr->name,
 		       min(strlen(load_attr->name), BPF_OBJ_NAME_LEN - 1));
+	if (libbpf_test_mode)
+		attr.prog_flags |= BPF_F_TEST_RND_HI32;
 
 	fd = sys_bpf_prog_load(&attr, sizeof(attr));
 	if (fd >= 0)
@@ -350,6 +352,8 @@ int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 	log_buf[0] = 0;
 	attr.kern_version = kern_version;
 	attr.prog_flags = prog_flags;
+	if (libbpf_test_mode)
+		attr.prog_flags |= BPF_F_TEST_RND_HI32;
 
 	return sys_bpf_prog_load(&attr, sizeof(attr));
 }
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index e2a457e..606643f 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -45,6 +45,8 @@
 #include "str_error.h"
 #include "libbpf_util.h"
 
+bool libbpf_test_mode;
+
 #ifndef EM_BPF
 #define EM_BPF 247
 #endif
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index c5ff005..b40de66 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -447,6 +447,8 @@ bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
 LIBBPF_API void
 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
 
+LIBBPF_API extern bool libbpf_test_mode;
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 6730017..5854e0b 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -156,6 +156,7 @@ LIBBPF_0.0.2 {
 		bpf_program__get_prog_info_linear;
 		bpf_program__bpil_addr_to_offs;
 		bpf_program__bpil_offs_to_addr;
+		libbpf_test_mode;
 } LIBBPF_0.0.1;
 
 LIBBPF_0.0.3 {
-- 
2.7.4




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux