Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752796AbbH0Kms (ORCPT ); Thu, 27 Aug 2015 06:42:48 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:35823 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751559AbbH0Kmp (ORCPT ); Thu, 27 Aug 2015 06:42:45 -0400 From: Kaixu Xia To: , , , , , , , CC: , , , Subject: [RFC PATCH 1/4] bpf tools: Add bpf_update_elem() and perf_event_open() for common bpf operations Date: Thu, 27 Aug 2015 10:42:19 +0000 Message-ID: <1440672142-89311-2-git-send-email-xiakaixu@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1440672142-89311-1-git-send-email-xiakaixu@huawei.com> References: <1440672142-89311-1-git-send-email-xiakaixu@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.250] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2701 Lines: 95 This patch introduces bpf_update_elem() and perf_event_open() in bpf.c/h for common bpf operations. Signed-off-by: Kaixu Xia --- tools/lib/bpf/bpf.c | 34 ++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 4 ++++ 2 files changed, 38 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index a633105..5ff7f09 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "bpf.h" @@ -29,6 +30,18 @@ # endif #endif +#if defined(__i386__) +#ifndef __NR_perf_event_open +# define __NR_perf_event_open 336 +#endif +#endif + +#if defined(__x86_64__) +#ifndef __NR_perf_event_open +# define __NR_perf_event_open 298 +#endif +#endif + static __u64 ptr_to_u64(void *ptr) { return (__u64) (unsigned long) ptr; @@ -55,6 +68,20 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); } +int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags) +{ + union bpf_attr attr; + + memset(&attr, '\0', sizeof(attr)); + + attr.map_fd = fd; + attr.key = ptr_to_u64(key); + attr.value = ptr_to_u64(value); + attr.flags = flags; + + return sys_bpf(BPF_MAP_UPDATE_ELEM, &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) @@ -83,3 +110,10 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns, log_buf[0] = 0; return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); } + +int perf_event_open(struct perf_event_attr *attr, int pid, int cpu, + int group_fd, unsigned long flags) +{ + return syscall(__NR_perf_event_open, attr, pid, cpu, + group_fd, flags); +} diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 854b736..7f1283c 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -12,6 +12,10 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, int max_entries); +int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags); +struct perf_event_attr; +int perf_event_open(struct perf_event_attr *attr, int pid, int cpu, + int group_fd, unsigned long flags); /* Recommend log buffer size */ #define BPF_LOG_BUF_SIZE 65536 -- 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/