Commit 16de581c authored by Yonghong Song's avatar Yonghong Song

rewrite bcc_create_map with libbpf primitives

This patch demonstrated how to use libbpf function calls.
Specially, the bcc_map_create is changed to use libbpf
functions bpf_create_map_name and bpf_create_map.
Signed-off-by: default avatarYonghong Song <yhs@fb.com>
parent e60438db
...@@ -51,6 +51,8 @@ ...@@ -51,6 +51,8 @@
// TODO: Remove this when CentOS 6 support is not needed anymore // TODO: Remove this when CentOS 6 support is not needed anymore
#include "setns.h" #include "setns.h"
#include "libbpf/src/bpf.h"
// TODO: remove these defines when linux-libc-dev exports them properly // TODO: remove these defines when linux-libc-dev exports them properly
#ifndef __NR_bpf #ifndef __NR_bpf
...@@ -198,20 +200,14 @@ int bcc_create_map(enum bpf_map_type map_type, const char *name, ...@@ -198,20 +200,14 @@ int bcc_create_map(enum bpf_map_type map_type, const char *name,
int max_entries, int map_flags) int max_entries, int map_flags)
{ {
size_t name_len = name ? strlen(name) : 0; size_t name_len = name ? strlen(name) : 0;
union bpf_attr attr; char map_name[BPF_OBJ_NAME_LEN];
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;
attr.map_flags = map_flags;
memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
int ret = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
memcpy(map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
int ret = bpf_create_map_name(map_type, map_name, key_size, value_size,
max_entries, map_flags);
if (ret < 0 && name_len && (errno == E2BIG || errno == EINVAL)) { if (ret < 0 && name_len && (errno == E2BIG || errno == EINVAL)) {
memset(attr.map_name, 0, BPF_OBJ_NAME_LEN); ret = bpf_create_map(map_type, key_size, value_size,
ret = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); max_entries, map_flags);
} }
if (ret < 0 && errno == EPERM) { if (ret < 0 && errno == EPERM) {
...@@ -222,7 +218,8 @@ int bcc_create_map(enum bpf_map_type map_type, const char *name, ...@@ -222,7 +218,8 @@ int bcc_create_map(enum bpf_map_type map_type, const char *name,
rl.rlim_max = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY;
rl.rlim_cur = rl.rlim_max; rl.rlim_cur = rl.rlim_max;
if (setrlimit(RLIMIT_MEMLOCK, &rl) == 0) if (setrlimit(RLIMIT_MEMLOCK, &rl) == 0)
ret = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); ret = bpf_create_map(map_type, key_size, value_size,
max_entries, map_flags);
} }
} }
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment