Search code examples
cebpf

eBPF BPF_ARRAY lookup


I am trying to build a packet counter with eBPF and 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. The problem is I can't seem to access the stored value using the provided lookup() function.

Here is how I create the 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.

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.

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);

Solution

  • I figured out how to fix my errors. I'm new to C so the pointers confused me a little bit.

    int cindex = 0;
    counter.increment(cindex);
    unsigned long long *current_count = counter.lookup(&cindex);
    
    if(current_count != NULL){          
        output.avg = *current_count;    
    }