Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1946680AbbHHIPl (ORCPT ); Sat, 8 Aug 2015 04:15:41 -0400 Received: from terminus.zytor.com ([198.137.202.10]:53840 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932818AbbHHIPe (ORCPT ); Sat, 8 Aug 2015 04:15:34 -0400 Date: Sat, 8 Aug 2015 01:14:48 -0700 From: tip-bot for Wang Nan Message-ID: Cc: dsahern@gmail.com, masami.hiramatsu.pt@hitachi.com, a.p.zijlstra@chello.nl, lizefan@huawei.com, namhyung@kernel.org, wangnan0@huawei.com, brendan.d.gregg@gmail.com, tglx@linutronix.de, jolsa@kernel.org, ast@plumgrid.com, hpa@zytor.com, daniel@iogearbox.net, mingo@kernel.org, xiakaixu@huawei.com, hekuang@huawei.com, acme@redhat.com, linux-kernel@vger.kernel.org Reply-To: tglx@linutronix.de, brendan.d.gregg@gmail.com, lizefan@huawei.com, namhyung@kernel.org, wangnan0@huawei.com, a.p.zijlstra@chello.nl, masami.hiramatsu.pt@hitachi.com, dsahern@gmail.com, hekuang@huawei.com, linux-kernel@vger.kernel.org, acme@redhat.com, xiakaixu@huawei.com, mingo@kernel.org, daniel@iogearbox.net, hpa@zytor.com, jolsa@kernel.org, ast@plumgrid.com In-Reply-To: <1435716878-189507-19-git-send-email-wangnan0@huawei.com> References: <1435716878-189507-19-git-send-email-wangnan0@huawei.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] bpf tools: Introduce bpf_load_program() to bpf.c Git-Commit-ID: 7bf98369a73a9a84c56b3f2b247ea8f0eaf8668b X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3372 Lines: 103 Commit-ID: 7bf98369a73a9a84c56b3f2b247ea8f0eaf8668b Gitweb: http://git.kernel.org/tip/7bf98369a73a9a84c56b3f2b247ea8f0eaf8668b Author: Wang Nan AuthorDate: Wed, 1 Jul 2015 02:14:06 +0000 Committer: Arnaldo Carvalho de Melo CommitDate: Fri, 7 Aug 2015 10:16:58 -0300 bpf tools: Introduce bpf_load_program() to bpf.c bpf_load_program() can be used to load bpf program into kernel. To make loading faster, first try to load without logbuf. Try again with logbuf if the first try failed. Signed-off-by: Wang Nan Acked-by: Alexei Starovoitov Cc: Brendan Gregg Cc: Daniel Borkmann Cc: David Ahern Cc: He Kuang Cc: Jiri Olsa Cc: Kaixu Xia Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1435716878-189507-19-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/lib/bpf/bpf.c | 34 ++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 7 +++++++ 2 files changed, 41 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 208de7c3..a633105 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -29,6 +29,11 @@ # endif #endif +static __u64 ptr_to_u64(void *ptr) +{ + return (__u64) (unsigned long) ptr; +} + static int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size) { @@ -49,3 +54,32 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); } + +int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns, + size_t insns_cnt, char *license, + u32 kern_version, char *log_buf, size_t log_buf_sz) +{ + int fd; + union bpf_attr attr; + + bzero(&attr, sizeof(attr)); + attr.prog_type = type; + attr.insn_cnt = (__u32)insns_cnt; + attr.insns = ptr_to_u64(insns); + attr.license = ptr_to_u64(license); + attr.log_buf = ptr_to_u64(NULL); + attr.log_size = 0; + attr.log_level = 0; + attr.kern_version = kern_version; + + fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); + if (fd >= 0 || !log_buf || !log_buf_sz) + return fd; + + /* Try again with log */ + attr.log_buf = ptr_to_u64(log_buf); + attr.log_size = log_buf_sz; + attr.log_level = 1; + log_buf[0] = 0; + return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); +} diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 28f7942..854b736 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -13,4 +13,11 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, int max_entries); +/* Recommend log buffer size */ +#define BPF_LOG_BUF_SIZE 65536 +int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns, + size_t insns_cnt, char *license, + u32 kern_version, char *log_buf, + size_t log_buf_sz); + #endif -- 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/