我在内核源代码中构建了一个ebpf演示。演示代码如下:
cgroup_kern.c
#include <uapi/linux/bpf.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/if_packet.h>
#include <uapi/linux/ip.h>
#include "bpf_helpers.h"
#include <bpf_helpers.h>
#include <linux/bpf.h>
struct bpf_map_def SEC("maps") my_map = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(u32),
.value_size = sizeof(long),
.max_entries = 256,
};
SEC("cgroupskb/ingress/stats")
int bpf_cgroup_ingress(struct __sk_buff* skb) {
uint32_t sock_uid = bpf_get_socket_uid(skb);
return 0;
}cgroup_user.c
#include <stdio.h>
#include <assert.h>
#include <linux/bpf.h>
#include "libbpf.h"
#include "bpf_load.h"
#include "sock_example.h"
#include <unistd.h>
#include <arpa/inet.h>
int main(int ac, char **argv)
{
char filename[256];
FILE *f;
int i, sock;
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
if (load_bpf_file(filename)) {
printf("%s", bpf_log_buf);
return 1;
}
return 0;
}然后,我为我的cgroup ebpf演示添加了一些Makefile。当我做的时候,得到了一个警告:
/usr/src/linux-source-4.15.0/linux-source-4.15.0/samples/bpf/cgroup_kern.c:20:22: warning: implicit declaration of function 'bpf_get_socket_uid' is invalid in C99 [-Wimplicit-function-declaration]
uint32_t sock_uid = bpf_get_socket_uid(skb);当我想要附加我的ebpf演示:无效的insn.code 0x85的relo
任何建议都将不胜感激。
发布于 2020-11-01 01:07:05
我猜你是从相同的内核源代码(4.15)中检索到bpf_helpers.h的。不幸的是,在4.15中,该文件缺少几个助手声明。此问题已在5.1版本的commit f2bb538中修复
commit f2bb53887eb3e8f859ac7cfc09d1a3801492c009
Author: Willem de Bruijn <willemb@google.com>
Date: Wed Feb 27 11:08:06 2019 -0500
bpf: add missing entries to bpf_helpers.h
This header defines the BPF functions enumerated in uapi/linux.bpf.h
in a callable format. Expand to include all registered functions.
Signed-off-by: Willem de Bruijn <willemb@google.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>您应该能够简单地通过向bpf_helpers.h添加适当的定义来修复错误
static unsigned int (*bpf_get_socket_uid)(void *ctx) =
(void *) BPF_FUNC_get_socket_uid;https://stackoverflow.com/questions/64622620
复制相似问题