Skip to contentSkip to conversation

selftests: net: extract BPF building logic from the Makefile

PATCH Applied Jakub Kicinski 2 participants 4 messages Apr 23, 2024 – Apr 25, 2024

Subsystems (4)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover selftests: net: extract BPF building logic from the Makefile

This has been sitting in my tree for a while. I will soon add YNL/libynl support for networking selftests. This prompted a small cleanup of the selftest makefile for net/. We don't want to be piling logic for each library in there. YNL will get its own .mk file which can be included. Do the same for the BPF building section, already.

No funcional changes here, just a code move and small rename.

Jakub Kicinski (2):
selftests: net: name bpf objects consistently and simplify Makefile
selftests: net: extract BPF building logic from the Makefile

 tools/testing/selftests/net/Makefile          | 60 +------------------
 tools/testing/selftests/net/bpf.mk            | 53 ++++++++++++++++
 .../net/{nat6to4.c => nat6to4.bpf.c}          |  0
 tools/testing/selftests/net/udpgro.sh         |  2 +-
 tools/testing/selftests/net/udpgro_bench.sh   |  2 +-
 tools/testing/selftests/net/udpgro_frglist.sh |  8 +--
 tools/testing/selftests/net/udpgro_fwd.sh     |  2 +-
 tools/testing/selftests/net/veth.sh           |  2 +-
 .../net/{xdp_dummy.c => xdp_dummy.bpf.c}      |  0
 9 files changed, 64 insertions(+), 65 deletions(-)
 create mode 100644 tools/testing/selftests/net/bpf.mk
 rename tools/testing/selftests/net/{nat6to4.c => nat6to4.bpf.c} (100%)
 rename tools/testing/selftests/net/{xdp_dummy.c => xdp_dummy.bpf.c} (100%)
Signature · 1 line
2.44.0
author maintainer
#1 lore raw

PATCH 1/2 selftests: net: name bpf objects consistently and simplify Makefile

The BPF sources moved with bpf_offload.py have a suffix of .bpf.c which seems to be useful convention. Rename the 2 other BPF sources we had. Use wildcard in the Makefile, since we can match all those files easily now.

---
 tools/testing/selftests/net/Makefile                     | 9 +++------
 tools/testing/selftests/net/{nat6to4.c => nat6to4.bpf.c} | 0
 tools/testing/selftests/net/udpgro.sh                    | 2 +-
 tools/testing/selftests/net/udpgro_bench.sh              | 2 +-
 tools/testing/selftests/net/udpgro_frglist.sh            | 8 ++++----
 tools/testing/selftests/net/udpgro_fwd.sh                | 2 +-
 tools/testing/selftests/net/veth.sh                      | 2 +-
 .../selftests/net/{xdp_dummy.c => xdp_dummy.bpf.c}       | 0
 8 files changed, 11 insertions(+), 14 deletions(-)
 rename tools/testing/selftests/net/{nat6to4.c => nat6to4.bpf.c} (100%)
 rename tools/testing/selftests/net/{xdp_dummy.c => xdp_dummy.bpf.c} (100%)
tools/testing/selftests/net/Makefile +3 -6
index 7e7f243d0ab2..c388308ff517 100644
@@ -82,10 +82,6 @@ TEST_GEN_PROGS += so_incoming_cpu
 TEST_PROGS += sctp_vrf.sh
 TEST_GEN_FILES += sctp_hello
 TEST_GEN_FILES += csum
-TEST_GEN_FILES += nat6to4.o
-TEST_GEN_FILES += xdp_dummy.o
-TEST_GEN_FILES += sample_ret0.bpf.o
-TEST_GEN_FILES += sample_map_ret0.bpf.o
 TEST_GEN_FILES += ip_local_port_range
 TEST_GEN_FILES += bind_wildcard
 TEST_PROGS += test_vxlan_mdb.sh
@@ -100,6 +96,8 @@ TEST_PROGS += bpf_offload.py
 TEST_FILES := settings
 TEST_FILES += in_netns.sh lib.sh net_helper.sh setup_loopback.sh setup_veth.sh
 
+TEST_GEN_FILES += $(patsubst %.c,%.o,$(wildcard *.bpf.c))
+
 TEST_INCLUDES := forwarding/lib.sh
 
 include ../lib.mk
@@ -146,8 +144,7 @@ endif
 
 CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
 
-BPF_PROG_OBJS := $(OUTPUT)/nat6to4.o $(OUTPUT)/xdp_dummy.o \
-	$(OUTPUT)/sample_map_ret0.bpf.o $(OUTPUT)/sample_ret0.bpf.o
+BPF_PROG_OBJS := $(patsubst %.c,$(OUTPUT)/%.o,$(wildcard *.bpf.c))
 
 $(BPF_PROG_OBJS): $(OUTPUT)/%.o : %.c $(BPFOBJ) | $(MAKE_DIRS)
 	$(call msg,BPF_PROG,,$@)
tools/testing/selftests/net/nat6to4.bpf.c renamed from tools/testing/selftests/net/nat6to4.c +0 -0
similarity index 100%
rename from tools/testing/selftests/net/nat6to4.c
rename to tools/testing/selftests/net/nat6to4.bpf.c
tools/testing/selftests/net/udpgro.sh +1 -1
index 8802604148dd..11a1ebda564f 100755
@@ -7,7 +7,7 @@ source net_helper.sh
 
 readonly PEER_NS="ns-peer-$(mktemp -u XXXXXX)"
 
-BPF_FILE="xdp_dummy.o"
+BPF_FILE="xdp_dummy.bpf.o"
 
 # set global exit status, but never reset nonzero one.
 check_err()
tools/testing/selftests/net/udpgro_bench.sh +1 -1
index 7080eae5312b..c51ea90a1395 100755
@@ -7,7 +7,7 @@ source net_helper.sh
 
 readonly PEER_NS="ns-peer-$(mktemp -u XXXXXX)"
 
-BPF_FILE="xdp_dummy.o"
+BPF_FILE="xdp_dummy.bpf.o"
 
 cleanup() {
 	local -r jobs="$(jobs -p)"
tools/testing/selftests/net/udpgro_frglist.sh +4 -4
index e1ff645bd3d1..17404f49cdb6 100755
@@ -7,7 +7,7 @@ source net_helper.sh
 
 readonly PEER_NS="ns-peer-$(mktemp -u XXXXXX)"
 
-BPF_FILE="xdp_dummy.o"
+BPF_FILE="xdp_dummy.bpf.o"
 
 cleanup() {
 	local -r jobs="$(jobs -p)"
@@ -42,8 +42,8 @@ run_one() {
 
 	ip -n "${PEER_NS}" link set veth1 xdp object ${BPF_FILE} section xdp
 	tc -n "${PEER_NS}" qdisc add dev veth1 clsact
-	tc -n "${PEER_NS}" filter add dev veth1 ingress prio 4 protocol ipv6 bpf object-file nat6to4.o section schedcls/ingress6/nat_6  direct-action
-	tc -n "${PEER_NS}" filter add dev veth1 egress prio 4 protocol ip bpf object-file nat6to4.o section schedcls/egress4/snat4 direct-action
+	tc -n "${PEER_NS}" filter add dev veth1 ingress prio 4 protocol ipv6 bpf object-file nat6to4.bpf.o section schedcls/ingress6/nat_6  direct-action
+	tc -n "${PEER_NS}" filter add dev veth1 egress prio 4 protocol ip bpf object-file nat6to4.bpf.o section schedcls/egress4/snat4 direct-action
         echo ${rx_args}
 	ip netns exec "${PEER_NS}" ./udpgso_bench_rx ${rx_args} -r &
 
@@ -89,7 +89,7 @@ if [ ! -f ${BPF_FILE} ]; then
 	exit -1
 fi
 
-if [ ! -f nat6to4.o ]; then
+if [ ! -f nat6to4.bpf.o ]; then
 	echo "Missing nat6to4 helper. Run 'make' first"
 	exit -1
 fi
tools/testing/selftests/net/udpgro_fwd.sh +1 -1
index 83ed987cff34..550d8eb3e224 100755
@@ -3,7 +3,7 @@
 
 source net_helper.sh
 
-BPF_FILE="xdp_dummy.o"
+BPF_FILE="xdp_dummy.bpf.o"
 readonly BASE="ns-$(mktemp -u XXXXXX)"
 readonly SRC=2
 readonly DST=1
tools/testing/selftests/net/veth.sh +1 -1
index 3a394b43e274..4f1edbafb946 100755
@@ -1,7 +1,7 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-2.0
 
-BPF_FILE="xdp_dummy.o"
+BPF_FILE="xdp_dummy.bpf.o"
 readonly STATS="$(mktemp -p /tmp ns-XXXXXX)"
 readonly BASE=`basename $STATS`
 readonly SRC=2
tools/testing/selftests/net/xdp_dummy.bpf.c renamed from tools/testing/selftests/net/xdp_dummy.c +0 -0
similarity index 100%
rename from tools/testing/selftests/net/xdp_dummy.c
rename to tools/testing/selftests/net/xdp_dummy.bpf.c
Signature · 1 line
2.44.0
author maintainer
#2 lore raw

PATCH 2/2 selftests: net: extract BPF building logic from the Makefile

In reply to Jakub Kicinski

The BPF sample building code looks a little bit spaghetti-ish so move it out to its own Makefile snippet. Similar in the spirit to how we include lib.mk. libynl will soon get a similar snippet.

There is a small change hiding in the move, the relative paths (../../.., ../.. etc) are replaced with variables from lib.mk such as top_srcdir and selfdir.

---
 tools/testing/selftests/net/Makefile | 53 +---------------------------
 tools/testing/selftests/net/bpf.mk   | 53 ++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 52 deletions(-)
 create mode 100644 tools/testing/selftests/net/bpf.mk
tools/testing/selftests/net/Makefile +1 -52
index c388308ff517..5befca249452 100644
@@ -108,55 +108,4 @@ $(OUTPUT)/tcp_inq: LDLIBS += -lpthread
 $(OUTPUT)/bind_bhash: LDLIBS += -lpthread
 $(OUTPUT)/io_uring_zerocopy_tx: CFLAGS += -I../../../include/
 
-# Rules to generate bpf objs
-CLANG ?= clang
-SCRATCH_DIR := $(OUTPUT)/tools
-BUILD_DIR := $(SCRATCH_DIR)/build
-BPFDIR := $(abspath ../../../lib/bpf)
-APIDIR := $(abspath ../../../include/uapi)
-
-CCINCLUDE += -I../bpf
-CCINCLUDE += -I../../../../usr/include/
-CCINCLUDE += -I$(SCRATCH_DIR)/include
-
-BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
-
-MAKE_DIRS := $(BUILD_DIR)/libbpf
-$(MAKE_DIRS):
-	$(call msg,MKDIR,,$@)
-	$(Q)mkdir -p $@
-
-# Get Clang's default includes on this system, as opposed to those seen by
-# '--target=bpf'. This fixes "missing" files on some architectures/distros,
-# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
-#
-# Use '-idirafter': Don't interfere with include mechanics except where the
-# build would have failed anyways.
-define get_sys_includes
-$(shell $(1) $(2) -v -E - </dev/null 2>&1 \
-	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
-$(shell $(1) $(2) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}')
-endef
-
-ifneq ($(CROSS_COMPILE),)
-CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
-endif
-
-CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
-
-BPF_PROG_OBJS := $(patsubst %.c,$(OUTPUT)/%.o,$(wildcard *.bpf.c))
-
-$(BPF_PROG_OBJS): $(OUTPUT)/%.o : %.c $(BPFOBJ) | $(MAKE_DIRS)
-	$(call msg,BPF_PROG,,$@)
-	$(Q)$(CLANG) -O2 -g --target=bpf $(CCINCLUDE) $(CLANG_SYS_INCLUDES) \
-	-c $< -o $@
-
-$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
-	   $(APIDIR)/linux/bpf.h					       \
-	   | $(BUILD_DIR)/libbpf
-	$(call msg,MAKE,,$@)
-	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
-		    EXTRA_CFLAGS='-g -O0'				       \
-		    DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
-
-EXTRA_CLEAN := $(SCRATCH_DIR)
+include bpf.mk
tools/testing/selftests/net/bpf.mk new file +53 -0

53 lines not on this page · raw message

Signature · 1 line
2.44.0
#3 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Tue, 23 Apr 2024 11:35:40 -0700 you wrote:

This has been sitting in my tree for a while. I will soon add YNL/libynl support for networking selftests. This prompted a small cleanup of the selftest makefile for net/. We don't want to be piling logic for each library in there. YNL will get its own .mk file which can be included. Do the same for the BPF building section, already.

No funcional changes here, just a code move and small rename.

[...]

Here is the summary with links:
- [net-next,1/2] selftests: net: name bpf objects consistently and simplify Makefile

    https://git.kernel.org/netdev/net-next/c/6b88ce902f0b
  - [net-next,2/2] selftests: net: extract BPF building logic from the Makefile
    https://git.kernel.org/netdev/net-next/c/3f584c211d8c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html