bpf tools: Introduce bpf_load_program() to bpf.c
authorWang Nan <wangnan0@huawei.com>
Wed, 1 Jul 2015 02:14:06 +0000 (02:14 +0000)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 7 Aug 2015 13:16:58 +0000 (10:16 -0300)
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 <wangnan0@huawei.com>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
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 <acme@redhat.com>
tools/lib/bpf/bpf.c
tools/lib/bpf/bpf.h

index 208de7c31e60ef15501fe417394e69a55b1f0aa8..a6331050ab79dbe7e32e38435ee5436c2867c3e1 100644 (file)
 # 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));
+}
index 28f79426834d33f9a8ce75298d9869f0b3ea5be3..854b7361b784380fdff7741a55deeb86c370ccca 100644 (file)
 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