Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751933AbcDTSCf (ORCPT ); Wed, 20 Apr 2016 14:02:35 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:26295 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752281AbcDTSCb (ORCPT ); Wed, 20 Apr 2016 14:02:31 -0400 From: Wang Nan To: , , CC: , , Wang Nan , Arnaldo Carvalho de Melo , "Alexei Starovoitov" , Jiri Olsa , Li Zefan Subject: [RFC PATCH 09/13] bpf tools: Register extern functions for ubpf programs Date: Wed, 20 Apr 2016 18:01:49 +0000 Message-ID: <1461175313-38310-10-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1461175313-38310-1-git-send-email-wangnan0@huawei.com> References: <1461175313-38310-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090201.5717C426.00F9,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: dcb055ccb0b9d7c6274c9eeb4ad239e2 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2668 Lines: 100 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 Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Brendan Gregg Cc: Jiri Olsa Cc: Li Zefan --- 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