Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf tools: Add bpf.c/h for common bpf operations

This patch introduces bpf.c and bpf.h, which hold common functions
issuing bpf syscall. The goal of these two files is to hide syscall
completely from user. Note that bpf.c and bpf.h deal with kernel
interface only. Things like structure of 'map' section in the ELF object
is not cared by of bpf.[ch].

We first introduce bpf_create_map().

Note that, since functions in bpf.[ch] are wrapper of sys_bpf, they
don't use OO style naming.

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-16-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Wang Nan and committed by
Arnaldo Carvalho de Melo
e3ed2fef 34090915

+68 -1
+1 -1
tools/lib/bpf/Build
··· 1 - libbpf-y := libbpf.o 1 + libbpf-y := libbpf.o bpf.o
+51
tools/lib/bpf/bpf.c
··· 1 + /* 2 + * common eBPF ELF operations. 3 + * 4 + * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 5 + * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 6 + * Copyright (C) 2015 Huawei Inc. 7 + */ 8 + 9 + #include <stdlib.h> 10 + #include <memory.h> 11 + #include <unistd.h> 12 + #include <asm/unistd.h> 13 + #include <linux/bpf.h> 14 + #include "bpf.h" 15 + 16 + /* 17 + * When building perf, unistd.h is override. Define __NR_bpf is 18 + * required to be defined. 19 + */ 20 + #ifndef __NR_bpf 21 + # if defined(__i386__) 22 + # define __NR_bpf 357 23 + # elif defined(__x86_64__) 24 + # define __NR_bpf 321 25 + # elif defined(__aarch64__) 26 + # define __NR_bpf 280 27 + # else 28 + # error __NR_bpf not defined. libbpf does not support your arch. 29 + # endif 30 + #endif 31 + 32 + static int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, 33 + unsigned int size) 34 + { 35 + return syscall(__NR_bpf, cmd, attr, size); 36 + } 37 + 38 + int bpf_create_map(enum bpf_map_type map_type, int key_size, 39 + int value_size, int max_entries) 40 + { 41 + union bpf_attr attr; 42 + 43 + memset(&attr, '\0', sizeof(attr)); 44 + 45 + attr.map_type = map_type; 46 + attr.key_size = key_size; 47 + attr.value_size = value_size; 48 + attr.max_entries = max_entries; 49 + 50 + return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); 51 + }
+16
tools/lib/bpf/bpf.h
··· 1 + /* 2 + * common eBPF ELF operations. 3 + * 4 + * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 5 + * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 6 + * Copyright (C) 2015 Huawei Inc. 7 + */ 8 + #ifndef __BPF_BPF_H 9 + #define __BPF_BPF_H 10 + 11 + #include <linux/bpf.h> 12 + 13 + int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, 14 + int max_entries); 15 + 16 + #endif