Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753240AbbEQLCY (ORCPT ); Sun, 17 May 2015 07:02:24 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:26292 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752789AbbEQK56 (ORCPT ); Sun, 17 May 2015 06:57:58 -0400 From: Wang Nan To: , , , , , , , , , , CC: , , Subject: [RFC PATCH v3 20/37] bpf tools: Add bpf.c/h for common bpf operations Date: Sun, 17 May 2015 10:56:45 +0000 Message-ID: <1431860222-61636-21-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1431860222-61636-1-git-send-email-wangnan0@huawei.com> References: <1431860222-61636-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.200] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.5558741F.004E,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 77e21b1059a3c688893c85eaeb99b1d6 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2984 Lines: 114 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(). Signed-off-by: Wang Nan --- tools/lib/bpf/Build | 2 +- tools/lib/bpf/bpf.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 17 +++++++++++++++++ 3 files changed, 71 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..3dbe30d --- /dev/null +++ b/tools/lib/bpf/bpf.c @@ -0,0 +1,53 @@ +/* + * common eBPF ELF operations. + * + * Copyright (C) 2015, Wang Nan + * Copyright (C) 2015, Huawei Inc. + * + * Released under the GPL v2. (and only v2, not any later version) + */ + +#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 + +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..1e9a53b --- /dev/null +++ b/tools/lib/bpf/bpf.h @@ -0,0 +1,17 @@ +/* + * common eBPF ELF operations. + * + * Copyright (C) 2015, Wang Nan + * Copyright (C) 2015, Huawei Inc. + * + * Released under the GPL v2. (and only v2, not any later version) + */ +#ifndef __LIBBPF_BPF_H +#define __LIBBPF_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 -- 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/