From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756291AbbE2Opn (ORCPT ); Fri, 29 May 2015 10:45:43 -0400 Received: from mail-pd0-f176.google.com ([209.85.192.176]:34602 "EHLO mail-pd0-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751264AbbE2Ope (ORCPT ); Fri, 29 May 2015 10:45:34 -0400 Date: Fri, 29 May 2015 23:44:05 +0900 From: Namhyung Kim To: Wang Nan 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 Message-ID: <20150529144405.GD10935@danjae.kornet> References: <1432704004-171454-1-git-send-email-wangnan0@huawei.com> <1432704004-171454-16-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1432704004-171454-16-git-send-email-wangnan0@huawei.com> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > --- > 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 > + * Copyright (C) 2015 Wang Nan > + * Copyright (C) 2015 Huawei Inc. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#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 > + * Copyright (C) 2015 Wang Nan > + * Copyright (C) 2015 Huawei Inc. > + */ > +#ifndef __BPF_BPF_H > +#define __BPF_BPF_H > + > +#include > + > +int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, > + int max_entries); > + > +#endif > -- > 1.8.3.4 >