[RFC PATCH 03/22] perf: add bpf common operations.

Wang Nan
2015-04-30T11:02:37Z

Add bpf syscall and related structure to perf for bpf loader use.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
---
tools/perf/perf-sys.h | 6 ++++++
tools/perf/util/Build | 1 +
tools/perf/util/bpf.c | 39 +++++++++++++++++++++++++++++++++++++++
tools/perf/util/bpf.h | 22 ++++++++++++++++++++++
4 files changed, 68 insertions(+)
create mode 100644 tools/perf/util/bpf.c
create mode 100644 tools/perf/util/bpf.h

diff --git a/tools/perf/perf-sys.h b/tools/perf/perf-sys.h
index 6ef6816..b38ca8b 100644
--- a/tools/perf/perf-sys.h
+++ b/tools/perf/perf-sys.h
@@ -22,6 +22,9 @@
#ifndef __NR_gettid
# define __NR_gettid 224
#endif
+#ifndef __NR_bpf
+# define __NR_bpf 357
+#endif
#endif

#if defined(__x86_64__)
@@ -39,6 +42,9 @@
#ifndef __NR_gettid
# define __NR_gettid 186
#endif
+#ifndef __NR_bpf
+# define __NR_bpf 321
+#endif
#endif

#ifdef __powerpc__
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 797490a..dfba2f0 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -74,6 +74,7 @@ libperf-y += data.o
libperf-$(CONFIG_X86) += tsc.o
libperf-y += cloexec.o
libperf-y += thread-stack.o
+libperf-y += bpf.o

libperf-$(CONFIG_LIBELF) += symbol-elf.o
libperf-$(CONFIG_LIBELF) += probe-event.o
diff --git a/tools/perf/util/bpf.c b/tools/perf/util/bpf.c
new file mode 100644
index 0000000..f752723
--- /dev/null
+++ b/tools/perf/util/bpf.c
@@ -0,0 +1,39 @@
+/*
+ * common BPF operations.
+ *
+ * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
+ * Copyright (C) 2015, Huawei Inc.
+ *
+ * Released under the GPL v2. (and only v2, not any later version)
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <linux/unistd.h>
+#include <unistd.h>
+#include <linux/bpf.h>
+#include <errno.h>
+#include "perf.h"
+#include "bpf.h"
+
+int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, size_t size)
+{
+ return syscall(__NR_bpf, cmd, attr, size);
+}
+
+int bpf_create_map(struct bpf_map_def *map_def)
+{
+ union bpf_attr attr;
+
+ if (!map_def)
+ return -EFAULT;
+
+ bzero(&attr, sizeof(attr));
+
+ attr.map_type = map_def->type;
+ attr.key_size = map_def->key_size;
+ attr.value_size = map_def->value_size;
+ attr.max_entries = map_def->max_entries;
+
+ return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
+}
diff --git a/tools/perf/util/bpf.h b/tools/perf/util/bpf.h
new file mode 100644
index 0000000..be106b0
--- /dev/null
+++ b/tools/perf/util/bpf.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
+ * Copyright (C) 2015, Huawei Inc.
+ *
+ * Released under the GPL v2. (and only v2, not any later version)
+ */
+#ifndef __PERF_BPF_H
+#define __PERF_BPF_H
+
+#include <linux/bpf.h>
+
+struct bpf_map_def {
+ unsigned int type;
+ unsigned int key_size;
+ unsigned int value_size;
+ unsigned int max_entries;
+};
+
+int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, size_t size);
+
+int bpf_create_map(struct bpf_map_def *map_def);
+#endif
--
1.8.3.4