[RFC PATCH v8 38/49] bpf tools: Load instructions buffer using load_program()
From: Wang Nan
Date: Wed Jun 24 2015 - 08:41:54 EST
Extract code for loading a 'struct bpf_insn' array into kernel to
load_program() and makes bpf_program__load() to call it. Now we have
function loads instructions into kernel. It will be used by further
patches, which creates different instances from a program and load them
into kernel.
Signed-off-by: Wang Nan <wangnan0@xxxxxxxxxx>
---
tools/lib/bpf/libbpf.c | 51 ++++++++++++++++++++++++++++++--------------------
1 file changed, 31 insertions(+), 20 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 5147a98..a2f2991 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -745,48 +745,59 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
}
static int
-bpf_program__load(struct bpf_program *prog,
- char *license, u32 kern_version)
+load_program(struct bpf_insn *insns, int insns_cnt,
+ char *license, u32 kern_version, int *pfd)
{
- int fd, err;
+ int ret;
char *log_buf;
- if (!prog->insns || !prog->insns_cnt)
+ if (!insns || !insns_cnt)
return -EINVAL;
log_buf = malloc(BPF_LOG_BUF_SIZE);
if (!log_buf)
pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
- fd = bpf_load_program(BPF_PROG_TYPE_KPROBE, prog->insns,
- prog->insns_cnt, license,
- kern_version, log_buf,
- BPF_LOG_BUF_SIZE);
+ ret = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
+ insns_cnt, license, kern_version,
+ log_buf, BPF_LOG_BUF_SIZE);
- if (fd >= 0) {
- prog->fd = fd;
- pr_debug("load bpf program '%s': fd = %d\n",
- prog->section_name, prog->fd);
- err = 0;
+ if (ret >= 0) {
+ *pfd = ret;
+ ret = 0;
goto out;
}
- err = -EINVAL;
- pr_warning("load bpf program '%s' failed: %s\n",
- prog->section_name, strerror(errno));
+ ret = -EINVAL;
+ pr_warning("load bpf program failed: %s\n", strerror(errno));
if (log_buf) {
- pr_warning("bpf: load: failed to load program '%s':\n",
- prog->section_name);
pr_warning("-- BEGIN DUMP LOG ---\n");
- pr_warning("%s\n", log_buf);
+ pr_warning("\n%s\n", log_buf);
pr_warning("-- END LOG --\n");
}
out:
+ free(log_buf);
+ return ret;
+}
+
+static int
+bpf_program__load(struct bpf_program *prog,
+ char *license, u32 kern_version)
+{
+ int err, fd;
+
+ err = load_program(prog->insns, prog->insns_cnt,
+ license, kern_version, &fd);
+ if (!err)
+ prog->fd = fd;
+
+ if (err)
+ pr_warning("failed to load program '%s'\n",
+ prog->section_name);
zfree(&prog->insns);
prog->insns_cnt = 0;
- free(log_buf);
return err;
}
--
1.8.3.4
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/