Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753801AbbFXMlu (ORCPT ); Wed, 24 Jun 2015 08:41:50 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:37422 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752818AbbFXMdF (ORCPT ); Wed, 24 Jun 2015 08:33:05 -0400 From: Wang Nan To: , , , , , , , , , , CC: , , , , Subject: [RFC PATCH v8 38/49] bpf tools: Load instructions buffer using load_program() Date: Wed, 24 Jun 2015 12:31:42 +0000 Message-ID: <1435149113-51142-39-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1435149113-51142-1-git-send-email-wangnan0@huawei.com> References: <1435149113-51142-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.200] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2820 Lines: 103 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 --- 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@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/