eBPF 的 perf_submit() 也可以在 socket_filter 程序中使用吗?

Ros*_*osè 2 bpf ebpf bcc-bpf

所以我试图使用 perf_submit 将一些数据从内核空间程序发送到用户空间程序。

我做了一些研究,在这里(https://github.com/iovisor/bcc/issues/2423),yonghong-song回答(最后一条评论),socket_filter程序无法访问bpf_perf_event_output helper,因此它只能是用于跟踪程序类型。

但是,在 BCC 参考站点(https://github.com/iovisor/bcc/blob/master/docs/reference_guide.md#2-bpf_perf_output)上,如果您 ctrl+f 并搜索: 3. perf_submit() ,它第五行说“对于 SOCKET_FILTER 程序,必须使用 struct __sk_buff *skb”。我相信这表明 perf_submit() 也可以用于 socket_filter 程序?

所以我很难弄清楚 perf_submit() 是否确实可以用于套接字过滤程序。自从宋永红回答了上面的问题之后,也许已经添加了一些功能?

我正在检查 perf_submit() 是否可以与套接字过滤器一起使用,并且实际上没有一行代码可以获取 perf_submit 输出的数据,因为内核程序中的 addint perf_submit() 已经忽略了错误。

这是我的程序的代码:

from bcc import BPF

# Network interface to be monoitored
INTERFACE = "br-netrome"

bpf_text = """

#include <uapi/linux/ptrace.h>
#include <net/sock.h>
#include <bcc/proto.h>
#include <linux/bpf.h>

#define IP_TCP 6
#define IP_UDP 17
#define IP_ICMP 1
#define ETH_HLEN 14

BPF_PERF_OUTPUT(events);    // has to be delcared outside any function

int packet_monitor(struct __sk_buff *skb) {
    u8 *cursor = 0;
    u64 saddr;
    u64 daddr;
    u64 ttl;
    u64 hchecksum;

    struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
    if (!(ethernet -> type == 0x0800)) {
        return 0; // drop
    }

    struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
    /*
    if (ip->nextp != IP_TCP) 
    {
        if (ip -> nextp != IP_UDP) 
        {
            if (ip -> nextp != IP_ICMP) 
                return 0; 
        }
    }
    */

    saddr = ip -> src;
    daddr = ip -> dst;
    ttl = ip -> ttl;
    hchecksum = ip -> hchecksum;

    events.perf_submit(skb, &saddr, sizeof(saddr));

//    bpf_trace_printk("saddr = %llu, daddr = %llu, ttl = %llu", saddr, daddr, ttl); // only three arguments can be passed using printk

//    bpf_trace_printk("Incoming packet!!\\n");
    return -1;
}
Run Code Online (Sandbox Code Playgroud)

这是错误代码:

 R0=inv2048 R6=ctx(id=0,off=0,imm=0) R7=inv0 R10=fp0,call_-1
4: (20) r0 = *(u32 *)skb[26]
5: (7b) *(u64 *)(r10 -8) = r0
6: (18) r2 = 0xffff9bde204ffa00
8: (18) r7 = 0xffffffff
10: (bf) r4 = r10
11: (07) r4 += -8
12: (bf) r1 = r6
13: (18) r3 = 0xffffffff
15: (b7) r5 = 8
16: (85) call bpf_perf_event_output#25
unknown func bpf_perf_event_output#25

Traceback (most recent call last):
  File "packet_monitor.py", line 68, in <module>
    function_skb_matching = bpf.load_func("packet_monitor", BPF.SOCKET_FILTER)
  File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 397, in load_func
    (func_name, errstr))
Run Code Online (Sandbox Code Playgroud)

pch*_*gno 6

TL;博士。BPF类型的程序只能从Linux 5.4开始BPF_PROG_TYPE_SOCKET_FILTER使用。bpf_perf_event_output


给定的 BPF 程序可以访问哪些助手是由get_func_proto对象的成员定义的struct bpf_verifier_ops。通过读取函数和文件可以找到哪个bpf_verifier_ops对象对应哪个程序类型。的情况下,对应的函数为find_prog_type()bpf_types.hBPF_PROG_TYPE_SOCKET_FILTERsk_filter_func_proto()

如果您git blame在最新的内核源代码上运行该函数,您将得到类似以下内容的内容(您可以使用 GitHub 的blame 功能执行相同的操作):

$ git blame net/core/filter.c
[...]
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6080) static const struct bpf_func_proto *
5e43f899b03a3 (Andrey Ignatov           2018-03-30 15:08:00 -0700 6081) sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6082) {
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6083)         switch (func_id) {
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6084)         case BPF_FUNC_skb_load_bytes:
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6085)                 return &bpf_skb_load_bytes_proto;
4e1ec56cdc597 (Daniel Borkmann          2018-05-04 01:08:15 +0200 6086)         case BPF_FUNC_skb_load_bytes_relative:
4e1ec56cdc597 (Daniel Borkmann          2018-05-04 01:08:15 +0200 6087)                 return &bpf_skb_load_bytes_relative_proto;
91b8270f2a4d1 (Chenbo Feng              2017-03-22 17:27:34 -0700 6088)         case BPF_FUNC_get_socket_cookie:
91b8270f2a4d1 (Chenbo Feng              2017-03-22 17:27:34 -0700 6089)                 return &bpf_get_socket_cookie_proto;
6acc5c2910689 (Chenbo Feng              2017-03-22 17:27:35 -0700 6090)         case BPF_FUNC_get_socket_uid:
6acc5c2910689 (Chenbo Feng              2017-03-22 17:27:35 -0700 6091)                 return &bpf_get_socket_uid_proto;
7c4b90d79d0f4 (Allan Zhang              2019-07-23 17:07:24 -0700 6092)         case BPF_FUNC_perf_event_output:
7c4b90d79d0f4 (Allan Zhang              2019-07-23 17:07:24 -0700 6093)                 return &bpf_skb_event_output_proto;
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6094)         default:
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6095)                 return bpf_base_func_proto(func_id);
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6096)         }
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6097) }
[...]
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,BPF_FUNC_perf_event_output最近才添加到这些 BPF 程序可以调用的帮助程序列表中。添加此支持的提交7c4b90d79d0f4已合并到 Linux v5.4 中:

$ git describe --contains 7c4b90d79d0f4
v5.4-rc1~131^2~248^2~20
Run Code Online (Sandbox Code Playgroud)