[RFC PATCH 1/4] bpf tools: Add bpf_update_elem() and perf_event_open() for common bpf operations

From: Kaixu Xia
Date: Thu Aug 27 2015 - 06:43:09 EST


This patch introduces bpf_update_elem() and perf_event_open()
in bpf.c/h for common bpf operations.

Signed-off-by: Kaixu Xia <xiakaixu@xxxxxxxxxx>
---
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 <memory.h>
#include <unistd.h>
#include <asm/unistd.h>
+#include <sys/syscall.h>
#include <linux/bpf.h>
#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@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/