如何正确利用掩码将索引信息发送到 perf 事件输出?

问题描述 投票:0回答:1

根据这里的bpf_perf_event_output文档。http:/man7.orglinuxman-pagesman7bpf-helpers.7.html。

"标志用于指示map中必须放值的索引,用BPF_F_INDEX_MASK屏蔽。"

在下面的代码中。

SEC("xdp_sniffer")
int xdp_sniffer_prog(struct xdp_md *ctx)
{

    void *data_end = (void *)(long)ctx->data_end;
    void *data = (void *)(long)ctx->data;

    if (data < data_end) {
        /* If we have reached here, that means this
        * is a useful packet for us. Pass on-the-wire
        * size and our cookie via metadata.
        */
        /* If we have reached here, that means this
        * is a useful packet for us. Pass on-the-wire
        * size and our cookie via metadata.
        */
        __u64 flags = BPF_F_INDEX_MASK;
        __u16 sample_size;
        int ret;
        struct S metadata;

        metadata.cookie = 0xdead;
        metadata.pkt_len = (__u16)(data_end - data);

        /* To minimize writes to disk, only
        * pass necessary information to userspace;
        * that is just the header info.
        */
        sample_size = min(metadata.pkt_len, SAMPLE_SIZE);
        flags |= (__u64)sample_size << 32;

        ret = bpf_perf_event_output(ctx, &my_map, flags,
                        &metadata, sizeof(metadata));
        if (ret)
            bpf_printk("perf_event_output failed: %d\n", ret);
    }
    return XDP_PASS;
}

它的工作原理就像你所期望的那样,它存储了给定的CPU号的信息。 然而,假设我想让所有的数据包都被发送到索引1。

我交换了

__u64 flags = BPF_F_INDEX_MASK;

对于

__u64 flags = 0x1ULL;

代码正确编译,没有抛出任何错误,但是再也没有数据包被保存。如果我想让所有的数据包都被发送到索引1,我做错了什么?

c linux bit-manipulation bitmask xdp-bpf
1个回答
0
投票

部分答案。我看不出为什么数据包不会被发送到 perf 缓冲区,但我怀疑错误是在用户空间代码上(没有提供)。可能是你在尝试从缓冲区读取数据时,没有为所有CPU "打开" perf 事件。请看一下man page for perf_event_open(2):检查 pidcpu 允许你读取为CPU 1写入的数据。

补充说明一下,这。

__u64 flags = BPF_F_INDEX_MASK;

是有误导性的。掩码应该用来掩盖索引,而不是设置其值。BPF_F_CURRENT_CPU 而应该使用:,前者只是因为两个枚举属性的值相同才发生作用。

© www.soinside.com 2019 - 2024. All rights reserved.