[RFC PATCH v2 18/26] perf bpf: Add ubpf helper function slots and set/get methods

From: He Kuang
Date: Sun Jun 26 2016 - 07:23:20 EST


Userspace bpf program has the ability to invoke helper functions
provided externally, this patch stores the helper functions in array
and adds methods for set/get functions in it. Theoretically the
function index can be up to UINT32_MAX, here we simply use an array
contains 64 function pointer slots.

Signed-off-by: Wang Nan <wangnan0@xxxxxxxxxx>
Signed-off-by: He Kuang <hekuang@xxxxxxxxxx>
---
tools/lib/bpf/engine-ubpf.c | 25 +++++++++++++++++++++++++
tools/lib/bpf/libbpf.h | 16 ++++++++++++++++
2 files changed, 41 insertions(+)

diff --git a/tools/lib/bpf/engine-ubpf.c b/tools/lib/bpf/engine-ubpf.c
index 9a0b425..e63a267 100644
--- a/tools/lib/bpf/engine-ubpf.c
+++ b/tools/lib/bpf/engine-ubpf.c
@@ -7,6 +7,31 @@
#include "bpf.h"
#include "libbpf-internal.h"

+static struct {
+ const char *name;
+ void *func;
+} ubpf_funcs[MAX_UBPF_FUNC];
+
+const void *libbpf_get_ubpf_func(unsigned int func_id)
+{
+ if (func_id >= MAX_UBPF_FUNC)
+ return NULL;
+
+ return ubpf_funcs[func_id].func;
+}
+
+int libbpf_set_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 engine__init(struct bpf_program *prog)
{
struct ubpf_entry *instances_entries;
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index e01ab8e..19c92bd 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -188,6 +188,8 @@ int bpf_map__set_priv(struct bpf_map *map, void *priv,
bpf_map_clear_priv_t clear_priv);
void *bpf_map__priv(struct bpf_map *map);

+#define MAX_UBPF_FUNC 64
+
/* The entity of ubpf program */
struct ubpf_entry {
struct bpf_insn *insns;
@@ -196,6 +198,8 @@ struct ubpf_entry {
#ifdef HAVE_UBPF_SUPPORT
int bpf_program__set_ubpf(struct bpf_program *prog);
bool bpf_program__is_ubpf(struct bpf_program *prog);
+const void *libbpf_get_ubpf_func(unsigned int func_id);
+int libbpf_set_ubpf_func(unsigned int idx, const char *name, void *func);
#else
static inline int
bpf_program__set_ubpf(struct bpf_program *prog __maybe_unused)
@@ -207,6 +211,18 @@ bpf_program__is_ubpf(struct bpf_program *prog __maybe_unused)
{
return false;
}
+static inline
+const void *libbpf_get_ubpf_func(unsigned int func_id __maybe_unused)
+{
+ return NULL;
+}
+static inline int
+libbpf_set_ubpf_func(unsigned int idx __maybe_unused,
+ const char *name __maybe_unused,
+ void *func __maybe_unused)
+{
+ return -LIBBPF_ERRNO__NOUBPF;
+}
#endif

#endif
--
1.8.5.2