Error compiling the c code output by net_monitor.py
- Dominant language
- C
- Stars
- 22.7k
- Forks
- 4.1k
- Avg merge
- 5d 13h
- Merged PRs (30d)
- 3
Description
I want to learn the ebpf program compilation process
It is normal to run net_monitor.py directly
[root@localhost]# ./net_monitor.py eth0
Running from kernel directory at: /lib/modules/5.11.6-1.el7.elrepo.x86_64/source
clang -cc1 -triple x86_64-unknown-linux-gnu -emit-llvm-bc -emit-llvm-uselists -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model static -mthread-model posix -fno-jump-tables -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -nostdsysteminc -nobuiltininc -resource-dir lib/clang/10.0.0 -isystem /virtual/lib/clang/include -include ./include/linux/kconfig.h -include /virtual/include/bcc/bpf.h -include /virtual/include/bcc/helpers.h -isystem /virtual/include -I /root/bcc/examples/networking -D __BPF_TRACING__ -I arch/x86/include/ -I /lib/modules/5.11.6-1.el7.elrepo.x86_64/build/arch/x86/include/generated -I include -I /lib/modules/5.11.6-1.el7.elrepo.x86_64/build/include -I arch/x86/include/uapi -I /lib/modules/5.11.6-1.el7.elrepo.x86_64/build/arch/x86/include/generated/uapi -I include/uapi -I /lib/modules/5.11.6-1.el7.elrepo.x86_64/build/include/generated/uapi -D __KERNEL__ -D __HAVE_BUILTIN_BSWAP16__ -D __HAVE_BUILTIN_BSWAP32__ -D __HAVE_BUILTIN_BSWAP64__ -D KBUILD_MODNAME="bcc" -O2 -Wno-deprecated-declarations -Wno-gnu-variable-sized-type-not-at-end -Wno-pragma-once-outside-header -Wno-address-of-packed-member -Wno-unknown-warning-option -Wno-unused-value -Wno-pointer-sign -fdebug-compilation-dir /usr/src/kernels/5.11.6-1.el7.elrepo.x86_64 -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -faddrsig -o main.bc -x c /virtual/main.c
```c
#if defined(BPF_LICENSE)
#error BPF_LICENSE cannot be specified through cflags
#endif
#if !defined(CONFIG_CC_STACKPROTECTOR)
#if defined(CONFIG_CC_STACKPROTECTOR_AUTO) \
|| defined(CONFIG_CC_STACKPROTECTOR_REGULAR) \
|| defined(CONFIG_CC_STACKPROTECTOR_STRONG)
#define CONFIG_CC_STACKPROTECTOR
#endif
#endif
#include
#include
#include
#include
#define IP_TCP 6
#define IP_UDP 17
#define IP_ICMP 1
#define ETH_HLEN 14
BPF_PERF_OUTPUT(skb_events);
BPF_HASH(packet_cnt, u64, long, 256);
__attribute__((section(".bpf.fn.packet_monitor")))
int packet_monitor(struct __sk_buff *skb) {
u8 *cursor = 0;
u32 saddr, daddr;
long* count = 0;
long one = 1;
u64 pass_value = 0;
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
if (bpf_dext_pkt(skb, (u64)ip+9, 0, 8) != IP_TCP)
{
if (bpf_dext_pkt(skb, (u64)ip+9, 0, 8) != IP_UDP)
{
if (bpf_dext_pkt(skb, (u64)ip+9, 0, 8) != IP_ICMP)
return 0;
}
}
saddr = bpf_dext_pkt(skb, (u64)ip+12, 0, 32);
daddr = bpf_dext_pkt(skb, (u64)ip+16, 0, 32);
pass_value = saddr;
pass_value = pass_value << 32;
pass_value = pass_value + daddr;
count = bpf_map_lookup_elem((void *)bpf_pseudo_fd(1, -2), &pass_value);
if (count) // check if this map exists
*count += 1;
else // if the map for the key doesn't exist, create one
{
bpf_map_update_elem((void *)bpf_pseudo_fd(1, -2), &pass_value, &one, BPF_ANY);
}
return -1;
}
#include
```
source address : 172.25.1.55 destination address : 172.25.1.56 47 time : 2021;03;23;15;12;46
source address : 172.25.1.56 destination address : 10.0.0.110 3 time : 2021;03;23;15;12;46
source address : 172.25.1.57 destination address : 10.0.0.197 5 time : 2021;03;23;15;12;46
source address : 172.25.1.55 destination address : 172.25.1.57 54 time : 2021;03;23;15;12;46
source address : 172.25.1.57 destination address : 172.25.1.55 58 time : 2021;03;23;15;12;46
source address : 172.25.1.56 destination address : 172.25.1.55 50 time : 2021;03;23;15;12;46
source address : 172.25.1.56 destination address : 10.0.0.197 3 time : 2021;03;23;15;12;47
source address : 172.25.1.26 destination address : 172.25.1.55 3 time : 2021;03;23;15;12;47
source address : 172.25.1.55 destination address : 10.0.1.246 12 time : 2021;03;23;15;12;47
source address : 172.25.1.55 destination address : 172.25.1.56 113 time : 2021;03;23;15;12;47
source address : 172.25.1.57 destination address : 10.0.0.197 5 time : 2021;03;23;15;12;47
source address : 172.25.1.55 destination address : 172.25.1.57 104 time : 2021;03;23;15;12;47
source address : 172.25.1.57 destination address : 172.25.1.55 105 time : 2021;03;23;15;12;47
source address : 172.25.1.56 destination address : 172.25.1.55 117 time : 2021;03;23;15;12;47
source address : 172.20.158.64 destination address : 172.25.1.55 1 time : 2021;03;23;15;12;47
source address : 172.25.1.55 destination address : 172.25.1.26 2 time : 2021;03;23;15;12;47
Save the c code to main.c
The file directory structure is as follows
[root@localhost]#pwd
/virtual
[root@localhost]# tree
.
├── include
│ └── bcc
│ ├── bpf.h
│ ├── footer.h
│ ├── helpers.h
│ └── proto.h
├── lib
│ └── clang
│ └── include
│ └── stdarg.h
└── main.c
5 directories, 6 files
error compiling the c code output by net_monitor.py
[root@localhost]# pwd
/lib/modules/5.11.6-1.el7.elrepo.x86_64/source
[root@localhost]# clang -cc1 -triple x86_64-unknown-linux-gnu -emit-llvm-bc -emit-llvm-uselists -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model static -mthread-model posix -fno-jump-tables -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -nostdsysteminc -nobuiltininc -resource-dir lib/clang/10.0.0 -isystem /virtual/lib/clang/include -include ./include/linux/kconfig.h -include /virtual/include/bcc/bpf.h -include /virtual/include/bcc/helpers.h -isystem /virtual/include -I /root/bcc/examples/networking -D __BPF_TRACING__ -I arch/x86/include/ -I /lib/modules/5.11.6-1.el7.elrepo.x86_64/build/arch/x86/include/generated -I include -I /lib/modules/5.11.6-1.el7.elrepo.x86_64/build/include -I arch/x86/include/uapi -I /lib/modules/5.11.6-1.el7.elrepo.x86_64/build/arch/x86/include/generated/uapi -I include/uapi -I /lib/modules/5.11.6-1.el7.elrepo.x86_64/build/include/generated/uapi -D __KERNEL__ -D __HAVE_BUILTIN_BSWAP16__ -D __HAVE_BUILTIN_BSWAP32__ -D __HAVE_BUILTIN_BSWAP64__ -D KBUILD_MODNAME="bcc" -O2 -Wno-deprecated-declarations -Wno-gnu-variable-sized-type-not-at-end -Wno-pragma-once-outside-header -Wno-address-of-packed-member -Wno-unknown-warning-option -Wno-unused-value -Wno-pointer-sign -fdebug-compilation-dir /usr/src/kernels/5.11.6-1.el7.elrepo.x86_64 -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -faddrsig -o main.bc -x c /virtual/main.c
In file included from /virtual/main.c:14:
In file included from include/net/sock.h:59:
In file included from include/linux/filter.h:26:
In file included from include/net/sch_generic.h:21:
include/net/flow_offload.h:317:4: error: use of undeclared identifier 'bcc'
NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported");
^
include/linux/netlink.h:98:27: note: expanded from macro 'NL_SET_ERR_MSG_MOD'
NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
^
:6:24: note: expanded from here
#define KBUILD_MODNAME bcc
^
In file included from /virtual/main.c:14:
In file included from include/net/sock.h:59:
In file included from include/linux/filter.h:26:
In file included from include/net/sch_generic.h:21:
include/net/flow_offload.h:317:4: error: expected ';' at end of declaration
include/linux/netlink.h:98:42: note: expanded from macro 'NL_SET_ERR_MSG_MOD'
NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
^
In file included from /virtual/main.c:14:
In file included from include/net/sock.h:59:
In file included from include/linux/filter.h:26:
In file included from include/net/sch_generic.h:21:
include/net/flow_offload.h:352:3: error: use of undeclared identifier 'bcc'
NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\"");
^
include/linux/netlink.h:98:27: note: expanded from macro 'NL_SET_ERR_MSG_MOD'
NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
^
:6:24: note: expanded from here
#define KBUILD_MODNAME bcc
^
In file included from /virtual/main.c:14:
In file included from include/net/sock.h:59:
In file included from include/linux/filter.h:26:
In file included from include/net/sch_generic.h:21:
include/net/flow_offload.h:352:3: error: expected ';' at end of declaration
include/linux/netlink.h:98:42: note: expanded from macro 'NL_SET_ERR_MSG_MOD'
NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
^
In file included from /virtual/main.c:14:
In file included from include/net/sock.h:59:
In file included from include/linux/filter.h:26:
In file included from include/net/sch_generic.h:21:
include/net/flow_offload.h:356:3: error: use of undeclared identifier 'bcc'
NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type");
^
include/linux/netlink.h:98:27: note: expanded from macro 'NL_SET_ERR_MSG_MOD'
NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
^
:6:24: note: expanded from here
#define KBUILD_MODNAME bcc
^
In file included from /virtual/main.c:14:
In file included from include/net/sock.h:59:
In file included from include/linux/filter.h:26:
In file included from include/net/sch_generic.h:21:
include/net/flow_offload.h:356:3: error: expected ';' at end of declaration
include/linux/netlink.h:98:42: note: expanded from macro 'NL_SET_ERR_MSG_MOD'
NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
^
6 errors generated.
How can I solve this error?thanks
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with net_monitor.py and the generated /virtual/main.c, then trace the reported macro expansion through include/net/flow_offload.h and include/linux/netlink.h. Compare the compiler command's KBUILD_MODNAME definition with the headers' expectations; done means the generated C compiles without the six reported errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, linux, python
- Domain
- networking, operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100