[RFC PATCH 09/13] bpf tools: Register extern functions for ubpf programs

From: Wang Nan
Date: Wed Apr 20 2016 - 14:02:38 EST


Introduce libbpf_register_ubpf_func(), allow caller (perf) to
register extern functions. Registered functions are stored into
an array and registered to each ubpf_vm when loading.

Signed-off-by: Wang Nan <wangnan0@xxxxxxxxxx>
Cc: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
Cc: Brendan Gregg <brendan.d.gregg@xxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Li Zefan <lizefan@xxxxxxxxxx>
---
tools/lib/bpf/libbpf.c | 38 +++++++++++++++++++++++++++++++++++++-
tools/lib/bpf/libbpf.h | 4 ++++
2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 4045a7e..f6df743 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -951,19 +951,48 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
}

#ifdef HAVE_UBPF_SUPPORT
+static struct {
+ const char *name;
+ void *func;
+} ubpf_funcs[MAX_UBPF_FUNC];
+
+int libbpf_register_ubpf_func(unsigned int idx, const char *name, void *func)
+{
+ if (idx >= MAX_UBPF_FUNC)
+ return -E2BIG;
+ if (!func)
+ return -EINVAL;
+
+ ubpf_funcs[idx].name = name;
+ ubpf_funcs[idx].func = func;
+ return 0;
+}
+
static int
load_ubpf_program(struct bpf_insn *insns, int insns_cnt,
struct ubpf_vm **pvm)
{
struct ubpf_vm *vm = ubpf_create();
char *message;
- int err;
+ int i, err;

if (!vm) {
pr_warning("Failed to create ubpf vm\n");
return -LIBBPF_ERRNO__LOADUBPF;
}

+ for (i = 0; i < MAX_UBPF_FUNC; i++) {
+ if (ubpf_funcs[i].func) {
+ err = ubpf_register(vm, i, ubpf_funcs[i].name,
+ ubpf_funcs[i].func);
+ if (err) {
+ pr_warning("Failed to register %s\n",
+ ubpf_funcs[i].name);
+ return -LIBBPF_ERRNO__LOADUBPF;
+ }
+ }
+ }
+
err = ubpf_load(vm, insns, insns_cnt * sizeof(insns[0]), &message);
if (err < 0) {
pr_warning("Failed to load ubpf program: %s\n", message);
@@ -973,6 +1002,13 @@ load_ubpf_program(struct bpf_insn *insns, int insns_cnt,
*pvm = vm;
return 0;
}
+#else
+int libbpf_register_ubpf_func(unsigned int idx __maybe_unused,
+ const char *name __maybe_unused,
+ void *func __maybe_unused)
+{
+ return -LIBBPF_ERRNO__NOUBPF;
+}
#endif

static int
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 41c35fd..a764dee 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -45,6 +45,10 @@ void libbpf_set_print(libbpf_print_fn_t warn,
libbpf_print_fn_t info,
libbpf_print_fn_t debug);

+#define MAX_UBPF_FUNC 64
+
+int libbpf_register_ubpf_func(unsigned int idx, const char *name, void *func);
+
/* Hide internal to user */
struct bpf_object;

--
1.8.3.4