From: Wang Nan <wangnan0@huawei.com>
To: <acme@kernel.org>, <ast@plumgrid.com>
Cc: <linux-kernel@vger.kernel.org>, <lizefan@huawei.com>,
<hekuang@huawei.com>, <xiakaixu@huawei.com>, <pi3orama@163.com>
Subject: [PATCH v11 39/39] bpf tools: Load a program with different instance using preprocessor
Date: Wed, 8 Jul 2015 13:14:28 +0000 [thread overview]
Message-ID: <1436361268-234530-40-git-send-email-wangnan0@huawei.com> (raw)
In-Reply-To: <1436361268-234530-1-git-send-email-wangnan0@huawei.com>
In this patch, caller of libbpf is able to control the loaded programs
by installing a preprocessor callback for a BPF program. With
preprocessor, different instances can be created from one BPF program.
This patch will be used by perf to generate different prologue for
different 'struct probe_trace_event' instances matched by one
'struct perf_probe_event'.
bpf_program__set_prep() is added to support this function. Caller
should pass libbpf the number of instance should be created and a
preprocessor function which will be called when doing real loading.
The callback should return instructions arrays for each instances.
nr_instance and instance_fds are appended into bpf_programs to support
multiple instances. bpf_program__get_nth_fd() is introduced for
read fd of instances. Old interface bpf_program__get_fd() won't work
for program which has preprocessor hooked.
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Signed-off-by: He Kuang <hekuang@huawei.com>
---
tools/lib/bpf/libbpf.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++--
tools/lib/bpf/libbpf.h | 22 ++++++++
2 files changed, 152 insertions(+), 5 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 83a3b06..d467fcb 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -100,6 +100,10 @@ struct bpf_program {
int fd;
+ int nr_instance;
+ int *instance_fds;
+ bpf_program_prep_t preprocessor;
+
struct bpf_object *obj;
void *priv;
bpf_program_clear_priv_t clear_priv;
@@ -156,6 +160,19 @@ static void bpf_program__unload(struct bpf_program *prog)
return;
zclose(prog->fd);
+
+ if (prog->preprocessor) {
+ int i;
+
+ if (prog->nr_instance <= 0)
+ pr_warning("Internal error when unloading: instance is %d\n",
+ prog->nr_instance);
+ else
+ for (i = 0; i < prog->nr_instance; i++)
+ zclose(prog->instance_fds[i]);
+ prog->nr_instance = -1;
+ zfree(&prog->instance_fds);
+ }
}
static void bpf_program__clear(struct bpf_program *prog)
@@ -207,6 +224,8 @@ __bpf_program__new(void *data, size_t size, char *name, int idx,
prog->insns_cnt * sizeof(struct bpf_insn));
prog->idx = idx;
prog->fd = -1;
+ prog->nr_instance = -1;
+ prog->instance_fds = NULL;
return 0;
errout:
@@ -798,13 +817,57 @@ static int
bpf_program__load(struct bpf_program *prog,
char *license, u32 kern_version)
{
- int err, fd;
+ int err = 0, fd, i;
+
+ if (!prog->preprocessor) {
+ err = load_program(prog->insns, prog->insns_cnt,
+ license, kern_version, &fd);
+ if (!err)
+ prog->fd = fd;
+ goto out;
+ }
- err = load_program(prog->insns, prog->insns_cnt,
- license, kern_version, &fd);
- if (!err)
- prog->fd = fd;
+ if (prog->nr_instance <= 0 || !prog->instance_fds) {
+ pr_warning("Internal error when loading '%s'\n",
+ prog->section_name);
+ return -EINVAL;
+ }
+
+ for (i = 0; i < prog->nr_instance; i++) {
+ struct bpf_prog_prep_result result;
+ bpf_program_prep_t preprocessor = prog->preprocessor;
+
+ bzero(&result, sizeof(result));
+ err = preprocessor(prog, i, prog->insns,
+ prog->insns_cnt, &result);
+ if (err) {
+ pr_warning("Preprocessing %dth instance of program '%s' failed\n",
+ i, prog->section_name);
+ goto out;
+ }
+
+ if (!result.new_insn_ptr || !result.new_insn_cnt) {
+ pr_debug("Skip loading %dth instance of program '%s'\n",
+ i, prog->section_name);
+ prog->instance_fds[i] = -1;
+ continue;
+ }
+ err = load_program(result.new_insn_ptr,
+ result.new_insn_cnt,
+ license, kern_version, &fd);
+
+ if (err) {
+ pr_warning("Loading %dth instance of program '%s' failed\n",
+ i, prog->section_name);
+ goto out;
+ }
+
+ if (result.pfd)
+ *result.pfd = fd;
+ prog->instance_fds[i] = fd;
+ }
+out:
if (err)
pr_warning("failed to load program '%s'\n",
prog->section_name);
@@ -1054,6 +1117,68 @@ int bpf_program__get_fd(struct bpf_program *prog, int *pfd)
if (!pfd)
return -EINVAL;
+ if (prog->preprocessor) {
+ pr_warning("Invalid using bpf_program__get_fd() on program %s\n",
+ prog->section_name);
+ return -EINVAL;
+ }
+
*pfd = prog->fd;
return 0;
}
+
+int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
+ bpf_program_prep_t prep)
+{
+ int *instance_fds;
+
+ if (nr_instance <= 0 || !prep)
+ return -EINVAL;
+
+ instance_fds = malloc(sizeof(int) * nr_instance);
+ if (!instance_fds) {
+ pr_warning("alloc memory failed for instance of fds\n");
+ return -ENOMEM;
+ }
+
+ /* fill all fd with -1 */
+ memset(instance_fds, 0xff, sizeof(int) * nr_instance);
+
+ prog->nr_instance = nr_instance;
+ prog->instance_fds = instance_fds;
+ prog->preprocessor = prep;
+ return 0;
+}
+
+int bpf_program__get_nth_fd(struct bpf_program *prog, int n, int *pfd)
+{
+ int fd;
+
+ if (!pfd)
+ return -EINVAL;
+
+ if (!prog->preprocessor ||
+ prog->nr_instance <= 0 ||
+ !prog->instance_fds) {
+
+ pr_warning("Invalid using bpf_program__get_nth_fd() on program %s\n",
+ prog->section_name);
+ return -EINVAL;
+ }
+
+ if (n >= prog->nr_instance || n < 0) {
+ pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
+ n, prog->section_name, prog->nr_instance);
+ return -EINVAL;
+ }
+
+ fd = prog->instance_fds[n];
+ if (fd < 0) {
+ pr_warning("%dth instance of program '%s' is invalid\n",
+ n, prog->section_name);
+ return -EEXIST;
+ }
+
+ *pfd = fd;
+ return 0;
+}
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 1643b57..8c2dc74 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -69,6 +69,28 @@ int bpf_program__get_title(struct bpf_program *prog,
int bpf_program__get_fd(struct bpf_program *prog, int *pfd);
+struct bpf_insn;
+struct bpf_prog_prep_result {
+ /*
+ * If not NULL, load new instruction array.
+ * If set to NULL, don't load this instance.
+ */
+ struct bpf_insn *new_insn_ptr;
+ int new_insn_cnt;
+
+ /* If not NULL, result fd is set to it */
+ int *pfd;
+};
+
+typedef int (*bpf_program_prep_t)(struct bpf_program *, int n,
+ struct bpf_insn *, int insn_cnt,
+ struct bpf_prog_prep_result *res);
+
+int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
+ bpf_program_prep_t prep);
+
+int bpf_program__get_nth_fd(struct bpf_program *prog, int n, int *pfd);
+
/*
* We don't need __attribute__((packed)) now since it is
* unnecessary for 'bpf_map_def' because they are all aligned.
--
1.8.3.4
next prev parent reply other threads:[~2015-07-08 13:20 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-08 13:13 [PATCH v11 00/39] perf tools: filtering events using eBPF programs - part1 Wang Nan
2015-07-08 13:13 ` [PATCH v11 01/39] bpf: Use correct #ifdef controller for trace_call_bpf() Wang Nan
2015-07-08 13:13 ` [PATCH v11 02/39] tracing, perf: Implement BPF programs attached to uprobes Wang Nan
2015-07-08 13:13 ` [PATCH v11 03/39] bpf tools: Introduce 'bpf' library and add bpf feature check Wang Nan
2015-07-08 13:13 ` [PATCH v11 04/39] bpf tools: Allow caller to set printing function Wang Nan
2015-07-08 13:13 ` [PATCH v11 05/39] bpf tools: Open eBPF object file and do basic validation Wang Nan
2015-07-08 13:13 ` [PATCH v11 06/39] bpf tools: Read eBPF object from buffer Wang Nan
2015-07-08 13:13 ` [PATCH v11 07/39] bpf tools: Check endianness and make libbpf fail early Wang Nan
2015-07-08 13:13 ` [PATCH v11 08/39] bpf tools: Iterate over ELF sections to collect information Wang Nan
2015-07-08 13:13 ` [PATCH v11 09/39] bpf tools: Collect version and license from ELF sections Wang Nan
2015-07-08 13:13 ` [PATCH v11 10/39] bpf tools: Collect map definitions from 'maps' section Wang Nan
2015-07-08 13:14 ` [PATCH v11 11/39] bpf tools: Collect symbol table from SHT_SYMTAB section Wang Nan
2015-07-08 13:14 ` [PATCH v11 12/39] bpf tools: Collect eBPF programs from their own sections Wang Nan
2015-07-08 13:14 ` [PATCH v11 13/39] bpf tools: Collect relocation sections from SHT_REL sections Wang Nan
2015-07-08 13:14 ` [PATCH v11 14/39] bpf tools: Record map accessing instructions for each program Wang Nan
2015-07-08 13:14 ` [PATCH v11 15/39] bpf tools: Add bpf.c/h for common bpf operations Wang Nan
2015-07-08 13:14 ` [PATCH v11 16/39] bpf tools: Create eBPF maps defined in an object file Wang Nan
2015-07-08 13:14 ` [PATCH v11 17/39] bpf tools: Relocate eBPF programs Wang Nan
2015-07-08 13:14 ` [PATCH v11 18/39] bpf tools: Introduce bpf_load_program() to bpf.c Wang Nan
2015-07-08 13:14 ` [PATCH v11 19/39] bpf tools: Load eBPF programs in object files into kernel Wang Nan
2015-07-08 13:14 ` [PATCH v11 20/39] bpf tools: Introduce accessors for struct bpf_program Wang Nan
2015-07-08 13:14 ` [PATCH v11 21/39] bpf tools: Introduce accessors for struct bpf_object Wang Nan
2015-07-08 13:14 ` [PATCH v11 22/39] bpf tools: Link all bpf objects onto a list Wang Nan
2015-07-08 13:14 ` [PATCH v11 23/39] perf tools: Introduce llvm config options Wang Nan
2015-07-08 13:14 ` [PATCH v11 24/39] perf tools: Call clang to compile C source to object code Wang Nan
2015-07-08 13:14 ` [PATCH v11 25/39] perf tools: Auto detecting kernel build directory Wang Nan
2015-07-08 13:14 ` [PATCH v11 26/39] perf tools: Auto detecting kernel include options Wang Nan
2015-07-08 13:14 ` [PATCH v11 27/39] perf tests: Add LLVM test for eBPF on-the-fly compiling Wang Nan
2015-07-08 13:14 ` [PATCH v11 28/39] perf tools: Make perf depend on libbpf Wang Nan
2015-07-08 19:44 ` Arnaldo Carvalho de Melo
2015-07-08 13:14 ` [PATCH v11 29/39] perf record: Enable passing bpf object file to --event Wang Nan
2015-07-08 13:14 ` [PATCH v11 30/39] perf record: Compile scriptlets if pass '.c' " Wang Nan
2015-07-08 13:14 ` [PATCH v11 31/39] perf tools: Parse probe points of eBPF programs during preparation Wang Nan
2015-07-08 13:14 ` [PATCH v11 32/39] perf probe: Attach trace_probe_event with perf_probe_event Wang Nan
2015-07-08 13:14 ` [PATCH v11 33/39] perf record: Probe at kprobe points Wang Nan
2015-07-08 13:14 ` [PATCH v11 34/39] perf record: Load all eBPF object into kernel Wang Nan
2015-07-08 13:14 ` [PATCH v11 35/39] perf tools: Add bpf_fd field to evsel and config it Wang Nan
2015-07-08 13:14 ` [PATCH v11 36/39] perf tools: Attach eBPF program to perf event Wang Nan
2015-07-08 13:14 ` [PATCH v11 37/39] perf tools: Suppress probing messages when probing by BPF loading Wang Nan
2015-07-08 13:14 ` [PATCH v11 38/39] perf record: Add clang options for compiling BPF scripts Wang Nan
2015-07-08 13:14 ` Wang Nan [this message]
2015-07-08 14:03 ` [PATCH v11 00/39] perf tools: filtering events using eBPF programs - part1 Arnaldo Carvalho de Melo
2015-07-08 15:57 ` pi3orama
2015-07-08 19:12 ` Arnaldo Carvalho de Melo
2015-07-08 20:53 ` Arnaldo Carvalho de Melo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1436361268-234530-40-git-send-email-wangnan0@huawei.com \
--to=wangnan0@huawei.com \
--cc=acme@kernel.org \
--cc=ast@plumgrid.com \
--cc=hekuang@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan@huawei.com \
--cc=pi3orama@163.com \
--cc=xiakaixu@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).