From: Namhyung Kim <namhyung@kernel.org>
To: Wang Nan <wangnan0@huawei.com>
Cc: paulus@samba.org, a.p.zijlstra@chello.nl, mingo@redhat.com,
acme@kernel.org, jolsa@kernel.org, dsahern@gmail.com,
daniel@iogearbox.net, brendan.d.gregg@gmail.com,
masami.hiramatsu.pt@hitachi.com, lizefan@huawei.com,
linux-kernel@vger.kernel.org, pi3orama@163.com
Subject: Re: [RFC PATCH v4 15/29] bpf tools: Add bpf.c/h for common bpf operations
Date: Fri, 29 May 2015 23:44:05 +0900 [thread overview]
Message-ID: <20150529144405.GD10935@danjae.kornet> (raw)
In-Reply-To: <1432704004-171454-16-git-send-email-wangnan0@huawei.com>
On Wed, May 27, 2015 at 05:19:50AM +0000, Wang Nan wrote:
> 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
> completly from user. Note that bpf.c and bpf.h only deal with kernel
> interface. 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>
> ---
> tools/lib/bpf/Build | 2 +-
> tools/lib/bpf/bpf.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
> tools/lib/bpf/bpf.h | 16 +++++++++++++++
> 3 files changed, 73 insertions(+), 1 deletion(-)
> create mode 100644 tools/lib/bpf/bpf.c
> create mode 100644 tools/lib/bpf/bpf.h
>
> diff --git a/tools/lib/bpf/Build b/tools/lib/bpf/Build
> index a316484..d874975 100644
> --- a/tools/lib/bpf/Build
> +++ b/tools/lib/bpf/Build
> @@ -1 +1 @@
> -libbpf-y := libbpf.o
> +libbpf-y := libbpf.o bpf.o
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> new file mode 100644
> index 0000000..7481923
> --- /dev/null
> +++ b/tools/lib/bpf/bpf.c
> @@ -0,0 +1,56 @@
> +/*
> + * common eBPF ELF operations.
> + *
> + * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
> + * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
> + * Copyright (C) 2015 Huawei Inc.
> + */
> +
> +#include <stdlib.h>
> +#include <memory.h>
> +#include <unistd.h>
> +#include <asm/unistd.h>
> +#include <linux/bpf.h>
> +#include "bpf.h"
> +
> +/* When building perf, unistd.h is override. __NR_bpf by ourself. */
> +#if defined(__i386__)
> +#ifndef __NR_bpf
> +# define __NR_bpf 357
> +#endif
> +#endif
> +
> +#if defined(__x86_64__)
> +#ifndef __NR_bpf
> +# define __NR_bpf 321
> +#endif
> +#endif
> +
> +#if defined(__aarch64__)
> +#ifndef __NR_bpf
> +# define __NR_bpf 280
> +#endif
> +#endif
> +
> +#ifndef __NR_bpf
> +# error __NR_bpf not defined. libbpf does not support your arch.
> +#endif
Why not doing this way?
#ifndef __NR_bpf
# if defined(__i386__)
# define __NR_bpf 357
# elif defined(__x86_64)
# define __NR_bpf 321
# elif defined(__aarch64__)
# define __NR_bpf 280
# else
# error __NR_bpf not defined. libbpf does not support your arch.
# endif
#endif
Thanks,
Namhyung
> +
> +static int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size)
> +{
> + return syscall(__NR_bpf, cmd, attr, size);
> +}
> +
> +int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
> + int max_entries)
> +{
> + union bpf_attr attr;
> + memset(&attr, '\0', sizeof(attr));
> +
> + attr.map_type = map_type;
> + attr.key_size = key_size;
> + attr.value_size = value_size;
> + attr.max_entries = max_entries;
> +
> + return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
> +}
> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
> new file mode 100644
> index 0000000..28f7942
> --- /dev/null
> +++ b/tools/lib/bpf/bpf.h
> @@ -0,0 +1,16 @@
> +/*
> + * common eBPF ELF operations.
> + *
> + * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
> + * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
> + * Copyright (C) 2015 Huawei Inc.
> + */
> +#ifndef __BPF_BPF_H
> +#define __BPF_BPF_H
> +
> +#include <linux/bpf.h>
> +
> +int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
> + int max_entries);
> +
> +#endif
> --
> 1.8.3.4
>
next prev parent reply other threads:[~2015-05-29 14:45 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-27 5:19 [RFC PATCH v4 00/29] perf tools: filtering events using eBPF programs Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 01/29] tools: Add __aligned_u64 to types.h Wang Nan
2015-05-27 13:00 ` Arnaldo Carvalho de Melo
2015-05-28 0:28 ` Wangnan (F)
2015-05-28 0:31 ` Arnaldo Carvalho de Melo
2015-05-27 5:19 ` [RFC PATCH v4 02/29] perf tools: Move linux/kernel.h to tools/include Wang Nan
2015-05-27 13:03 ` Arnaldo Carvalho de Melo
2015-05-27 5:19 ` [RFC PATCH v4 03/29] perf tools: Move linux/{list.h,poison.h} " Wang Nan
2015-05-27 13:15 ` Arnaldo Carvalho de Melo
2015-05-27 13:21 ` Arnaldo Carvalho de Melo
2015-05-27 15:30 ` Arnaldo Carvalho de Melo
2015-05-27 5:19 ` [RFC PATCH v4 04/29] bpf tools: Introduce 'bpf' library to tools Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 05/29] bpf tools: Allow caller to set printing function Wang Nan
2015-05-29 13:35 ` Namhyung Kim
2015-05-27 5:19 ` [RFC PATCH v4 06/29] bpf tools: Open eBPF object file and do basic validation Wang Nan
2015-05-28 1:44 ` Alexei Starovoitov
2015-05-27 5:19 ` [RFC PATCH v4 07/29] bpf tools: Check endianess and make libbpf fail early Wang Nan
2015-05-28 1:45 ` Alexei Starovoitov
2015-05-27 5:19 ` [RFC PATCH v4 08/29] bpf tools: Iterate over ELF sections to collect information Wang Nan
2015-05-28 1:46 ` Alexei Starovoitov
2015-05-27 5:19 ` [RFC PATCH v4 09/29] bpf tools: Collect version and license from ELF sections Wang Nan
2015-05-28 1:48 ` Alexei Starovoitov
2015-05-28 3:34 ` Wangnan (F)
2015-05-28 5:51 ` Alexei Starovoitov
2015-05-27 5:19 ` [RFC PATCH v4 10/29] bpf tools: Collect map definitions from 'maps' section Wang Nan
2015-05-28 1:53 ` Alexei Starovoitov
2015-05-28 2:03 ` Wangnan (F)
2015-05-28 2:28 ` Alexei Starovoitov
2015-05-28 3:09 ` Wangnan (F)
2015-05-28 6:09 ` Alexei Starovoitov
2015-05-28 7:14 ` Wangnan (F)
2015-05-29 3:35 ` Alexei Starovoitov
2015-05-29 3:59 ` Wangnan (F)
2015-06-01 2:12 ` Namhyung Kim
2015-06-01 5:19 ` Wangnan (F)
2015-06-01 6:03 ` Namhyung Kim
2015-06-01 13:01 ` Arnaldo Carvalho de Melo
2015-05-27 5:19 ` [RFC PATCH v4 11/29] bpf tools: Collect symbol table from SHT_SYMTAB section Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 12/29] bpf tools: Collect eBPF programs from their own sections Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 13/29] bpf tools: Collect relocation sections from SHT_REL sections Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 14/29] bpf tools: Record map accessing instructions for each program Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 15/29] bpf tools: Add bpf.c/h for common bpf operations Wang Nan
2015-05-28 1:55 ` Alexei Starovoitov
2015-05-29 14:44 ` Namhyung Kim [this message]
2015-05-27 5:19 ` [RFC PATCH v4 16/29] bpf tools: Create eBPF maps defined in an object file Wang Nan
2015-05-28 1:57 ` Alexei Starovoitov
2015-05-27 5:19 ` [RFC PATCH v4 17/29] bpf tools: Relocate eBPF programs Wang Nan
2015-06-01 5:32 ` Namhyung Kim
2015-06-01 6:36 ` Wangnan (F)
2015-05-27 5:19 ` [RFC PATCH v4 18/29] bpf tools: Introduce bpf_load_program() to bpf.c Wang Nan
2015-05-28 2:14 ` Alexei Starovoitov
2015-05-27 5:19 ` [RFC PATCH v4 19/29] bpf tools: Load eBPF programs in object files into kernel Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 20/29] bpf tools: Introduce accessors for struct bpf_program Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 21/29] bpf tools: Introduce accessors for struct bpf_object Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 22/29] bpf tools: Link all bpf objects onto a list Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 23/29] perf tools: Make perf depend on libbpf Wang Nan
2015-05-27 5:19 ` [RFC PATCH v4 24/29] perf record: Enable passing bpf object file to --event Wang Nan
2015-05-27 5:20 ` [RFC PATCH v4 25/29] perf tools: Parse probe points of eBPF programs during preparation Wang Nan
2015-05-27 5:20 ` [RFC PATCH v4 26/29] perf record: Probe at kprobe points Wang Nan
2015-05-27 5:20 ` [RFC PATCH v4 27/29] perf record: Load all eBPF object into kernel Wang Nan
2015-05-27 5:20 ` [RFC PATCH v4 28/29] perf tools: Add bpf_fd field to evsel and config it Wang Nan
2015-05-27 5:20 ` [RFC PATCH v4 29/29] perf tools: Attach eBPF program to perf event Wang Nan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150529144405.GD10935@danjae.kornet \
--to=namhyung@kernel.org \
--cc=a.p.zijlstra@chello.nl \
--cc=acme@kernel.org \
--cc=brendan.d.gregg@gmail.com \
--cc=daniel@iogearbox.net \
--cc=dsahern@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan@huawei.com \
--cc=masami.hiramatsu.pt@hitachi.com \
--cc=mingo@redhat.com \
--cc=paulus@samba.org \
--cc=pi3orama@163.com \
--cc=wangnan0@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox