From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752645AbcFZLWR (ORCPT ); Sun, 26 Jun 2016 07:22:17 -0400 Received: from szxga04-in.huawei.com ([58.251.152.52]:21083 "EHLO szxga04-in.huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752422AbcFZLWE (ORCPT ); Sun, 26 Jun 2016 07:22:04 -0400 From: He Kuang To: , , , , , , , , CC: Subject: [RFC PATCH v2 18/26] perf bpf: Add ubpf helper function slots and set/get methods Date: Sun, 26 Jun 2016 11:21:10 +0000 Message-ID: <1466940078-65581-19-git-send-email-hekuang@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1466940078-65581-1-git-send-email-hekuang@huawei.com> References: <1466940078-65581-1-git-send-email-hekuang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.250] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090206.576FBAD0.001D,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 5fb02ae2ac22a2a10e5a9b738a33c95b Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Signed-off-by: He Kuang --- 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