简体   繁体   English

eBPF BPF_ARRAY 查找

[英]eBPF BPF_ARRAY lookup

I am trying to build a packet counter with eBPF and XDP.我正在尝试使用 eBPF 和 XDP 构建数据包计数器。 I need a way to keep track of the total number of packets received.我需要一种方法来跟踪收到的数据包总数。 Since I'm using XDP I use a BPF_ARRAY and increment it every time a packet is received.由于我使用的是 XDP,因此我使用 BPF_ARRAY 并在每次收到数据包时递增它。 The problem is I can't seem to access the stored value using the provided lookup() function.问题是我似乎无法使用提供的lookup() function 访问存储的值。

Here is how I create the BPF_ARRAY.这是我创建 BPF_ARRAY 的方法。

BPF_ARRAY(counter, u64, 1);

Here is how I try to access and use the stored value.这是我尝试访问和使用存储值的方式。 The type of output.avg is u64. output.avg 的类型是 u64。

int cindex = 0;
counter.increment(&cindex);
long current_count = counter.lookup(&cindex);
output.avg = current_count;

BPF gives me this warning and fails to compile. BPF 给了我这个警告并且无法编译。

warning: incompatible pointer to integer conversion initializing 'long' with
      an expression of type 'u64 *' (aka 'unsigned long long *') [-Wint-conversion]
                        long current_count = counter.lookup(&cindex);

I figured out how to fix my errors.我想出了如何解决我的错误。 I'm new to C so the pointers confused me a little bit.我是 C 的新手,所以指针让我有点困惑。

int cindex = 0;
counter.increment(cindex);
unsigned long long *current_count = counter.lookup(&cindex);

if(current_count != NULL){          
    output.avg = *current_count;    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM