Search code examples
bpfebpf

Printing Hello World using uBPF?


I am messing around with uBPF recently and have noticed that I can't seem to implement any print functions within uBPF. I tried adding my code directly to test.c within the VM folder but and have it as a registered function but I am left with relocation errors, leaving me without a solution. I have seen an implementation with OKO but that does not seem to fix my problem either.

Here is my registered code:

static void
register_functions(struct ubpf_vm *vm)
{
    ubpf_register(vm, 0, "gather_bytes", gather_bytes);
    ubpf_register(vm, 1, "memfrob", memfrob);
    ubpf_register(vm, 2, "trash_registers", trash_registers);
    ubpf_register(vm, 3, "ubpf_printf", ubpf_printf);
}

And here is the print function itself:

static void
ubpf_printf(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    char str[MAX_PRINTF_LENGTH];
    if (vsnprintf(str, MAX_PRINTF_LENGTH, fmt, args) >= 0)
        VLOG_ERR("%s", str);
    va_end(args);
}

My question is how do I implement a print function that can print whatever I want?

Edit: Adding the error itself, after implementing my test function within test.c. The vlog.h file is there but I still seem to get the error.

make -C vm
make: Entering directory '/home/pi/Desktop/ubpfModified/vm'
cc -Wall -Werror -Iinc -O2 -g -Wunused-parameter   -c -o test.o test.c
test.c:32:10: fatal error: /home/pi/Desktop/ubpf/vm/vlog.h: No such file or directory
   32 | #include "/home/pi/Desktop/ubpf/vm/vlog.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [<builtin>: test.o] Error 1
make: Leaving directory '/home/pi/Desktop/ubpfModified/vm'

Solution

  • The error message tell you everything you need. This is not about your implementation, but about a header that you attempt to include, but that the compiler cannot find.

    You have apparently copied some code from Oko, it seems that VLOG_ERR() for example is defined in oko/include/openvswitch/vlog.h. You probably added an #include "vlog.h", but did not add the relevant file to your repo?