0

I'm trying to see if I can use stdio.h with a simple XDP code that blocks every packet.

    #include <linux/bpf.h>
    #include <stdio.h>

    int main()
    {
        return XDP_DROP;
    }

This code works without stdio.h but it wouldn't compile when I try to compile it using :

clang -target bpf -O2 -c xdp.c -o xdp.o

I was thinking maybe the compiler cannot find the system headers' directory so I also tried :

clang -target bpf -O2 -c xdp.c -o xdp.o -I /usr/include/

which I believe should specify where the system header files are.

I don't know what is wrong :(

Qeole
  • 8,284
  • 1
  • 24
  • 52
Rosè
  • 345
  • 2
  • 13
  • 2
    What the error message? The issue is likely that `stdio.h` contains some code that cannot be compiled to BPF bytecode. In general, you won't be able to use standard libraries in BPF programs. – pchaigno Dec 27 '19 at 09:11
  • @pchaigno It gives me an error that types such as s32 or u8 are not defined. It also says more than 20 fatal errors have occurred. I have been thinking about it and I do think it does contain some code that can't be compiled into BPF bytecode. Thanks for the insight! – Rosè Dec 29 '19 at 12:09
  • 1
    it compiled for me. even without -I flag. whats your clang version? mine is 7.0.1. – Devidas Dec 31 '19 at 05:19
  • @Rose can you please paste exact error code? so that we will be able to help you better. – Devidas Dec 31 '19 at 05:25
  • @Devidas I have uploaded clang to version 9.0.0 just in case but still I get the same error :( – Rosè Jan 02 '20 at 11:12
  • @Devidas the error is : "in file includedf rom xdp.c:2: /usr/include/sxtdio.h:28:1: error: unknown type name 'p' p ^ In file included from xdp.c:2: In file included from /usr/include/stdio.h:37: /usr/include/x86_64-linux-gnu/bits/types.h:30:1: error: expected identifier or '(' typedef unsigned char __u_char; ^ 2 errors generated" – Rosè Jan 02 '20 at 11:18
  • What do you need `stdio.h` for in your program? – Qeole Jan 02 '20 at 15:24
  • @Qeole I was just curious if I would be able to add headers I used to use in the user space. Furthermore, I'm trying to learn how I would be able to distinguish between the headers I can use in the eBPF kernel space program and the ones that I can't! – Rosè Jan 02 '20 at 16:27
  • 1
    I don't know if there is a real rule that could be used to filter what headers would work and what would fail. `` works, obviously, because it contains BPF-related definitions. Some other Linux headers can be used to access things like Ethernet or IP packet structure, for example. But I would not expect standard libraries to work in general, because they define specific functions that may not translate into BPF programs, as pchaigno said. If you consider `stdio.h`: it is about standard input/output, which are quite different for regular programs when compared to BPF ones. – Qeole Jan 02 '20 at 16:48
  • @Qeole Yeah I was vaguely thinking that some headers probably can't be used in the kernel but now you make it more crystal clear and that in fact I am using BPF programs after all to be able to do something in the kernel space. Thank you for your insight! – Rosè Jan 02 '20 at 17:05

0 Answers0