为什么 samples/bpf 中的 ebpf 程序不起作用?
Why ebpf program inside samples/bpf doesn't work?
目标:在4.18.0内核源码树的samples/bpf
目录下写一个新的ebpf
例子,编译并执行。
问题: 当我 运行 sudo ./mine
编译它后它就终止了。
mine_kern.c
#include <uapi/linux/bpf.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/ip.h>
#include <linux/in.h>
#include <linux/if_packet.h>
#include "bpf_helpers.h"
int icmp_filter(struct __sk_buff *skb){
int proto = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
if(proto == IPPROTO_ICMP && skb->pkt_type == PACKET_OUTGOING){
return -1;
} else {
return 0;
}
}
char _license[] SEC("license") = "GPL";
mine_user.c
#include <stdio.h>
#include <assert.h>
#include <linux/bpf.h>
#include <bpf/bpf.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;
}
sock = open_raw_sock("lo");
assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd,
sizeof(prog_fd[0])) == 0);
f = popen("ping -c5 localhost", "r");
(void) f;
char buf[65535];
for(i=0; i<20; i++){
int res = recvfrom(sock, buf, sizeof(buf), 0, NULL, 0);
printf("res=%d\n", res);
}
return 0;
}
我还修改了 samples/bpf
中的 Makefile
,在需要的地方添加了 mine_user.c
和 mine_kern.c
。
问题:这段代码有什么问题?
由于load_bpf_file()
加载函数的方式,您需要将您的BPF程序函数放在一个单独的ELF部分。例如,我可以加载程序:
SEC("socket")
int icmp_filter(struct __sk_buff *skb){
...
}
之后,我看到一连串res=-1
当运行的程序。这是因为你的套接字被open_raw_sock()
设置为非阻塞,从sock_example.h
:
sock = socket(PF_PACKET, SOCK_RAW|SOCK_NONBLOCK|SOCK_CLOEXEC, htons(ETH_P_ALL));
所以当没有数据包接收时,recvfrom()
只需 returns 和 -1
(并将 errno
设置为 -EGAIN
-- 你应该考虑顺便打印 strerror(errno)
)而不是等待数据包。所以你可能也想改变它。
目标:在4.18.0内核源码树的samples/bpf
目录下写一个新的ebpf
例子,编译并执行。
问题: 当我 运行 sudo ./mine
编译它后它就终止了。
mine_kern.c
#include <uapi/linux/bpf.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/ip.h>
#include <linux/in.h>
#include <linux/if_packet.h>
#include "bpf_helpers.h"
int icmp_filter(struct __sk_buff *skb){
int proto = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
if(proto == IPPROTO_ICMP && skb->pkt_type == PACKET_OUTGOING){
return -1;
} else {
return 0;
}
}
char _license[] SEC("license") = "GPL";
mine_user.c
#include <stdio.h>
#include <assert.h>
#include <linux/bpf.h>
#include <bpf/bpf.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;
}
sock = open_raw_sock("lo");
assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd,
sizeof(prog_fd[0])) == 0);
f = popen("ping -c5 localhost", "r");
(void) f;
char buf[65535];
for(i=0; i<20; i++){
int res = recvfrom(sock, buf, sizeof(buf), 0, NULL, 0);
printf("res=%d\n", res);
}
return 0;
}
我还修改了 samples/bpf
中的 Makefile
,在需要的地方添加了 mine_user.c
和 mine_kern.c
。
问题:这段代码有什么问题?
由于load_bpf_file()
加载函数的方式,您需要将您的BPF程序函数放在一个单独的ELF部分。例如,我可以加载程序:
SEC("socket")
int icmp_filter(struct __sk_buff *skb){
...
}
之后,我看到一连串res=-1
当运行的程序。这是因为你的套接字被open_raw_sock()
设置为非阻塞,从sock_example.h
:
sock = socket(PF_PACKET, SOCK_RAW|SOCK_NONBLOCK|SOCK_CLOEXEC, htons(ETH_P_ALL));
所以当没有数据包接收时,recvfrom()
只需 returns 和 -1
(并将 errno
设置为 -EGAIN
-- 你应该考虑顺便打印 strerror(errno)
)而不是等待数据包。所以你可能也想改变它。