samples: bpf: Fix vmlinux.h generation for XDP samples
Kumar Kartikeya Dwivedi
maintainerPATCH 1/1 samples: bpf: Fix vmlinux.h generation for XDP samples
Generate vmlinux.h only from the in-tree vmlinux, and remove enum declarations that would cause a build failure in case of version mismatches.
There are now two options when building the samples:
1. Compile the kernel to use in-tree vmlinux for vmlinux.h
2. Override VMLINUX_BTF for samples using something like this:
make VMLINUX_BTF=/sys/kernel/btf/vmlinux -C samples/bpf
This change was tested with relative builds, e.g. cases like:
* make O=build -C samples/bpf
* make KBUILD_OUTPUT=build -C samples/bpf
* make -C samples/bpf
* cd samples/bpf && make
When a suitable VMLINUX_BTF is not found, the following message is printed:/home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux for VMLINUX_BTF at any of " ./vmlinux", build the kernel or set VMLINUX_BTF variable. Stop.
--- samples/bpf/Makefile | 13 ++++++------- samples/bpf/xdp_redirect_map_multi.bpf.c | 5 ----- 2 files changed, 6 insertions(+), 12 deletions(-)
samples/bpf/Makefile +6 -7
@@ -324,15 +324,9 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ - ../../../../vmlinux \ - /sys/kernel/btf/vmlinux \ - /boot/vmlinux-$(shell uname -r) + ./vmlinux VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) -ifeq ($(VMLINUX_BTF),) -$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)") -endif - $(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) ifeq ($(VMLINUX_H),) $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
@@ -340,6 +334,11 @@ else $(Q)cp "$(VMLINUX_H)" $@ endif +ifeq ($(VMLINUX_BTF),) + $(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)",\ + build the kernel or set VMLINUX_BTF variable) +endif + clean-files += vmlinux.h # Get Clang's default includes on this system, as opposed to those seen by
samples/bpf/xdp_redirect_map_multi.bpf.c +0 -5
@@ -5,11 +5,6 @@#include "xdp_sample.bpf.h" #include "xdp_sample_shared.h" -enum { -BPF_F_BROADCAST = (1ULL <<3 ), -BPF_F_EXCLUDE_INGRESS = (1ULL <<4 ), -}; -struct { __uint(type, BPF_MAP_TYPE_DEVMAP_HASH); __uint(key_size, sizeof(int));
--
2.33.0
Quoted 35 lines · Kumar Kartikeya Dwivedi
Generate
vmlinux.honly from the in-tree vmlinux, and remove enum declarations that would cause a build failure in case of version mismatches.There are now two options when building the samples:
1. Compile the kernel to use in-tree vmlinux forvmlinux.h
2. OverrideVMLINUX_BTFfor samples using something like this:
make VMLINUX_BTF=/sys/kernel/btf/vmlinux -Csamples/bpfThis change was tested with relative builds, e.g. cases like:
* make O=build -Csamples/bpf
* make KBUILD_OUTPUT=build -Csamples/bpf
* make -Csamples/bpf
* cdsamples/bpf&& makeWhen a suitable
VMLINUX_BTFis not found, the following message is printed:/home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux forVMLINUX_BTFat any of " ./vmlinux", build the kernel or setVMLINUX_BTFvariable. Stop.Cc Toke Høiland-Jørgensen <[email protected]>Fixes 384b6b3bbf0d (samples: bpf: Add vmlinux.h generation support)Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- samples/bpf/Makefile | 13 ++++++------- samples/bpf/xdp_redirect_map_multi.bpf.c | 5 ----- 2 files changed, 6 insertions(+), 12 deletions(-)samples/bpf/Makefile +0
-0
samples/bpf/Makefile $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h · line 327
Quoted 7 lines
samples/bpf/Makefile +1
-3@@ -324,15 +324,9 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ - ../../../../vmlinux \ - /sys/kernel/btf/vmlinux \ - /boot/vmlinux-$(shell uname -r) + ./vmlinux
isn't this relative to samples/bpf subdirectory, so shouldn't it be ../../vmlinux?
Quoted 6 lines
samples/bpf/Makefile +0
-4VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) -ifeq ($(VMLINUX_BTF),) -$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)") -endif -
[...]
Kumar Kartikeya Dwivedi
maintainerQuoted 38 lines · Andrii Nakryiko
Quoted 36 lines · Kumar Kartikeya Dwivedi
Generate
vmlinux.honly from the in-tree vmlinux, and remove enum declarations that would cause a build failure in case of version mismatches.There are now two options when building the samples:
1. Compile the kernel to use in-tree vmlinux forvmlinux.h
2. OverrideVMLINUX_BTFfor samples using something like this:
make VMLINUX_BTF=/sys/kernel/btf/vmlinux -Csamples/bpfThis change was tested with relative builds, e.g. cases like:
* make O=build -Csamples/bpf
* make KBUILD_OUTPUT=build -Csamples/bpf
* make -Csamples/bpf
* cdsamples/bpf&& makeWhen a suitable
VMLINUX_BTFis not found, the following message is printed:/home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux forVMLINUX_BTFat any of " ./vmlinux", build the kernel or setVMLINUX_BTFvariable. Stop.Cc Toke Høiland-Jørgensen <[email protected]>Fixes 384b6b3bbf0d (samples: bpf: Add vmlinux.h generation support)Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- samples/bpf/Makefile | 13 ++++++------- samples/bpf/xdp_redirect_map_multi.bpf.c | 5 ----- 2 files changed, 6 insertions(+), 12 deletions(-)samples/bpf/Makefile +0
-0@@ -324,15 +324,9 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
Quoted 10 lines
samples/bpf/Makefile $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h · line 327
Quoted 6 lines
samples/bpf/Makefile +1
-3VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ - ../../../../vmlinux \ - /sys/kernel/btf/vmlinux \ - /boot/vmlinux-$(shell uname -r) + ./vmlinuxisn't this relative to
samples/bpfsubdirectory, so shouldn't it be ../../vmlinux?
I was confused about this too; but it executes this when you call make:
# Trick to allow make to be run from this directory all:
$(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)
so it fails if the path isn't relative to kernel source root.
Quoted 8 lines
Quoted 6 lines
samples/bpf/Makefile +0
-4VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) -ifeq ($(VMLINUX_BTF),) -$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)") -endif -[...]
--
Kartikeya
Quoted 51 lines · Kumar Kartikeya Dwivedi
Quoted 38 lines · Andrii Nakryiko
Quoted 36 lines · Kumar Kartikeya Dwivedi
Generate
vmlinux.honly from the in-tree vmlinux, and remove enum declarations that would cause a build failure in case of version mismatches.There are now two options when building the samples:
1. Compile the kernel to use in-tree vmlinux forvmlinux.h
2. OverrideVMLINUX_BTFfor samples using something like this:
make VMLINUX_BTF=/sys/kernel/btf/vmlinux -Csamples/bpfThis change was tested with relative builds, e.g. cases like:
* make O=build -Csamples/bpf
* make KBUILD_OUTPUT=build -Csamples/bpf
* make -Csamples/bpf
* cdsamples/bpf&& makeWhen a suitable
VMLINUX_BTFis not found, the following message is printed:/home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux forVMLINUX_BTFat any of " ./vmlinux", build the kernel or setVMLINUX_BTFvariable. Stop.Cc Toke Høiland-Jørgensen <[email protected]>Fixes 384b6b3bbf0d (samples: bpf: Add vmlinux.h generation support)Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- samples/bpf/Makefile | 13 ++++++------- samples/bpf/xdp_redirect_map_multi.bpf.c | 5 ----- 2 files changed, 6 insertions(+), 12 deletions(-)samples/bpf/Makefile +0
-0@@ -324,15 +324,9 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.hQuoted 10 lines
samples/bpf/Makefile $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h · line 327
Quoted 6 lines
samples/bpf/Makefile +1
-3VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ - ../../../../vmlinux \ - /sys/kernel/btf/vmlinux \ - /boot/vmlinux-$(shell uname -r) + ./vmlinuxisn't this relative to
samples/bpfsubdirectory, so shouldn't it be ../../vmlinux?
Quoted 7 lines
I was confused about this too; but it executes this when you call make:
# Trick to allow make to be run from this directory all:
$(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)so it fails if the path isn't relative to kernel source root.
Hm... what if we do $(abspath ./vmlinux) here (and probably same for $(O) and $(KBUILD_OUTPUT) just in case those are relative as well, would that do the right thing here? The way it is right now it's extremely confusing (and obviously ../../../../vmlinux never worked and we never knew that).
Quoted 12 lines
Quoted 9 lines
Quoted 7 lines
samples/bpf/Makefile +0
-4VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) -ifeq ($(VMLINUX_BTF),) -$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)") -endif -[...]
--
Kartikeya
Kumar Kartikeya Dwivedi
maintainerQuoted 59 lines · Andrii Nakryiko
Quoted 57 lines · Kumar Kartikeya Dwivedi
Quoted 38 lines · Andrii Nakryiko
Quoted 36 lines · Kumar Kartikeya Dwivedi
Generate
vmlinux.honly from the in-tree vmlinux, and remove enum declarations that would cause a build failure in case of version mismatches.There are now two options when building the samples:
1. Compile the kernel to use in-tree vmlinux forvmlinux.h
2. OverrideVMLINUX_BTFfor samples using something like this:
make VMLINUX_BTF=/sys/kernel/btf/vmlinux -Csamples/bpfThis change was tested with relative builds, e.g. cases like:
* make O=build -Csamples/bpf
* make KBUILD_OUTPUT=build -Csamples/bpf
* make -Csamples/bpf
* cdsamples/bpf&& makeWhen a suitable
VMLINUX_BTFis not found, the following message is printed:/home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux forVMLINUX_BTFat any of " ./vmlinux", build the kernel or setVMLINUX_BTFvariable. Stop.Cc Toke Høiland-Jørgensen <[email protected]>Fixes 384b6b3bbf0d (samples: bpf: Add vmlinux.h generation support)Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- samples/bpf/Makefile | 13 ++++++------- samples/bpf/xdp_redirect_map_multi.bpf.c | 5 ----- 2 files changed, 6 insertions(+), 12 deletions(-)samples/bpf/Makefile +0
-0@@ -324,15 +324,9 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.hQuoted 10 lines
samples/bpf/Makefile $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h · line 327
Quoted 6 lines
samples/bpf/Makefile +1
-3VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ - ../../../../vmlinux \ - /sys/kernel/btf/vmlinux \ - /boot/vmlinux-$(shell uname -r) + ./vmlinuxisn't this relative to
samples/bpfsubdirectory, so shouldn't it be ../../vmlinux?I was confused about this too; but it executes this when you call make:
# Trick to allow make to be run from this directory all:
$(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)
Quoted 5 lines
Quoted 1 line
so it fails if the path isn't relative to kernel source root.
Hm... what if we do $(abspath ./vmlinux) here (and probably same for $(O) and $(KBUILD_OUTPUT) just in case those are relative as well, would that do the right thing here? The way it is right now it's
Yeah, that works. I'll send a v2.
Quoted 3 lines
extremely confusing (and obviously ../../../../vmlinux never worked and we never knew that).
Indeed.
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
samples/bpf/Makefile +0
-4VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) -ifeq ($(VMLINUX_BTF),) -$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)") -endif -[...]
Quoted 2 lines
Quoted 2 lines
--
Kartikeya
--
Kartikeya