0

I run an up to date debian testing (with kernel 4.19).

Helpers are not found on my system (but they exist in the header, Qt jumps to them)

#include "bpf/bpf.h"

int main (){
        int r = bpf_create_map(BPF_MAP_TYPE_ARRAY,1,1,1,0);
        return 0;
}

Compilation results in

undefined reference to `bpf_create_map(bpf_map_type, int, int, int, unsigned int)'

compiled with

g++ -c -pipe -g -std=gnu++1z -Wall -W -fPIC -DQT_QML_DEBUG -I. -I../../Qt/5.13.0/gcc_64/mkspecs/linux-g++ -o main.o main.cpp
g++ -lbpf -o server main.o  

Same result with

g++ main.cpp -lbpf -o out

I have the libbpf-dev installed as well and i have the associated libraries (a and so).

What is wrong?

Update

even the following code won't work

#include <linux/bpf.h>

int main (){

        //int r = bpf_create_map(BPF_MAP_TYPE_ARRAY,1,1,1,0);
        bpf_attr attr = {};
        attr.map_type    = BPF_MAP_TYPE_ARRAY;
        attr.key_size    = 1;
        attr.value_size  = 1;
        attr.max_entries = 1;

        bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
        return 0;
}

results in

error: 'bpf' was not declared in this scope

Update2:

BTW, key size is mandated to be 4 and not 1; but it is a point aside, that was unrelated to my problem here.

Larry
  • 1,735
  • 1
  • 18
  • 46
  • Are you sure `bpf_create_map` is part of the helpers? Because I can't find it in the man page. – Federico klez Culloca Jul 26 '19 at 07:38
  • 1
    With GCC and the standard Linux linker, the order you have your object files and libraries on the command-line when linking matters. If object file `A.o` depends on library `B`, then `A.o` must come *before* the library `B`. So the last command you show should work ***if*** the function actually exists in that library. – Some programmer dude Jul 26 '19 at 07:40
  • Besides, reading [the BPF manual page](http://man7.org/linux/man-pages/man2/bpf.2.html) it doesn't mention anything about a library to link with, or the header file you include. – Some programmer dude Jul 26 '19 at 07:41

1 Answers1

0

Namespace issue due to compiling in C++, you probably want:

extern "C" {
#include "bpf/bpf.h"
}

int main()...

Regarding your second error (error: 'bpf' was not declared in this scope), this is not directly related to libbpf, this is because there is no function simply called bpf() to actually perform the syscall. Instead you have to use the syscall number. For example, libbpf defines the following:

static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
                          unsigned int size)
{
    return syscall(__NR_bpf, cmd, attr, size);
}

... and uses sys_bpf() after that, the same way you try to call bpf() in your sample.

For the record, “BPF helpers” often designates BPF functions that you call from within a BPF program, which is not the case here. Hence some confusion in the comments, I believe.

Qeole
  • 8,284
  • 1
  • 24
  • 52
  • Ah you were right, adding extern C{] solved the problem. My first code compiles fine now. – Larry Jul 26 '19 at 11:27