Skip to contentSkip to conversation

selftests/bpf: Remove xxd util dependency

PATCH Superseded Mykyta Yatsenko 2 participants 3 messages Jan 28

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests/bpf: Remove xxd util dependency

From: Mykyta Yatsenko <[email protected]>

The verification signature header generation requires converting a binary certificate to a C array. Previously this only worked with xxd (part of vim-common package).
As xxd may not be available on some systems building selftests, it makes sense to substitute it with more common utils: hexdump, wc, sed to generate equivalent C array output.

Tested by generating header with both xxd and hexdump and comparing them.

---
 tools/testing/selftests/bpf/Makefile | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/Makefile +5 -1
index 2c2f68a171ed..bbcd2101c3d7 100644
@@ -720,9 +720,13 @@ $(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP)
 	$(Q)mkdir -p $(BUILD_DIR)
 	$(Q)$(VERIFY_SIG_SETUP) genkey $(BUILD_DIR)
 
+# Generates a header with C array declaration, containing test_progs_verification_cert bytes
 $(VERIFY_SIG_HDR): $(VERIFICATION_CERT)
 	$(Q)ln -fs $< test_progs_verification_cert && \
-	xxd -i test_progs_verification_cert > $@
+	(echo "unsigned char test_progs_verification_cert[] = {"; \
+	 hexdump -v -e '12/1 "  0x%02x," "\n"' test_progs_verification_cert | sed 's/0x  ,//g'; \
+	 echo "};"; \
+	 echo "unsigned int test_progs_verification_cert_len = $$(wc -c < test_progs_verification_cert);") > $@
 
 # Define test_progs test runner.
 TRUNNER_TESTS_DIR := prog_tests
Signature · 1 line
2.52.0
Tested-by on v2 1/1
#1 lore raw
Quoted 22 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

The verification signature header generation requires converting a binary certificate to a C array. Previously this only worked with xxd (part of vim-common package).
As xxd may not be available on some systems building selftests, it makes sense to substitute it with more common utils: hexdump, wc, sed to generate equivalent C array output.

Tested by generating header with both xxd and hexdump and comparing them.

---
 tools/testing/selftests/bpf/Makefile | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/Makefile +0 -0
index 2c2f68a171ed..bbcd2101c3d7 100644

tools/testing/selftests/bpf/Makefile $(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP) · line 725

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1 -0
@@ -720,9 +720,13 @@ $(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP)
 	$(Q)mkdir -p $(BUILD_DIR)
 	$(Q)$(VERIFY_SIG_SETUP) genkey $(BUILD_DIR)
 
+# Generates a header with C array declaration, containing test_progs_verification_cert bytes
 $(VERIFY_SIG_HDR): $(VERIFICATION_CERT)
 	$(Q)ln -fs $< test_progs_verification_cert && \

I think we can remove the symlinking here too. It was a workaround for the fact that xxd derived the name of the variable to assign from the filename, so we could not use $< directly as a result. It would use underscores etc when representing the path; i.e. something like path_2_test_progs_verification_cert.

Newer xxd had an option to provide an explicit name, but we needed a workaround there since we couldn't rely on the fact xxd was new enough.

Now since we don't care about xxd we can dispense with the symlinking step.

tools/testing/selftests/bpf/Makefile $(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP) · line 730

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +4 -1
-	xxd -i test_progs_verification_cert > $@
+	(echo "unsigned char test_progs_verification_cert[] = {"; \
+	 hexdump -v -e '12/1 "  0x%02x," "\n"' test_progs_verification_cert | sed 's/0x  ,//g'; \
+	 echo "};"; \
+	 echo "unsigned int test_progs_verification_cert_len = $$(wc -c < test_progs_verification_cert);") > $@
 

After removing symlinking we would just need to change to using $< above instead of test_progs_verification_cert I think; following changes on top of your patch seem to do the trick in tidying this up:

tools/testing/selftests/bpf/.gitignore +0 -1
index b8bf51b7a0b0..a3ea98211ea6 100644
@@ -23,7 +23,6 @@ test_tcpnotify_user
 test_libbpf
 xdping
 test_cpp
-test_progs_verification_cert
 *.d
 *.subskel.h
 *.skel.h
tools/testing/selftests/bpf/Makefile +4 -6
index bbcd2101c3d7..f559f792c1be 100644
@@ -722,11 +722,10 @@ $(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP)
 
 # Generates a header with C array declaration, containing test_progs_verification_cert bytes
 $(VERIFY_SIG_HDR): $(VERIFICATION_CERT)
-       $(Q)ln -fs $< test_progs_verification_cert && \
-       (echo "unsigned char test_progs_verification_cert[] = {"; \
-        hexdump -v -e '12/1 "  0x%02x," "\n"' test_progs_verification_cert | sed 's/0x  ,//g'; \
+       $(Q)(echo "unsigned char test_progs_verification_cert[] = {"; \
+        hexdump -v -e '12/1 "  0x%02x," "\n"' $< | sed 's/0x  ,//g'; \
         echo "};"; \
-        echo "unsigned int test_progs_verification_cert_len = $$(wc -c < test_progs_verification_cert);") > $@
+        echo "unsigned int test_progs_verification_cert_len = $$(wc -c < $<);") > $@
 
 # Define test_progs test runner.
 TRUNNER_TESTS_DIR := prog_tests
@@ -902,8 +901,7 @@ EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)                   \
                               *.BTF *.BTF_ids *.BTF.base               \
                               no_alu32 cpuv4 bpf_gcc                   \
                               liburandom_read.so)                      \
-       $(OUTPUT)/FEATURE-DUMP.selftests                                \
-       test_progs_verification_cert
+       $(OUTPUT)/FEATURE-DUMP.selftests
 
 .PHONY: docs docs-clean
 

I'd suggest doing something like the above, but I tested both ways and it works well so

#2 lore raw
Quoted 93 lines · Alan Maguire
Quoted 22 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

The verification signature header generation requires converting a binary certificate to a C array. Previously this only worked with xxd (part of vim-common package).
As xxd may not be available on some systems building selftests, it makes sense to substitute it with more common utils: hexdump, wc, sed to generate equivalent C array output.

Tested by generating header with both xxd and hexdump and comparing them.

---
  tools/testing/selftests/bpf/Makefile | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/Makefile +0 -0
index 2c2f68a171ed..bbcd2101c3d7 100644

tools/testing/selftests/bpf/Makefile $(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP) · line 725

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1 -0
@@ -720,9 +720,13 @@ $(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP)
  	$(Q)mkdir -p $(BUILD_DIR)
  	$(Q)$(VERIFY_SIG_SETUP) genkey $(BUILD_DIR)
 
+# Generates a header with C array declaration, containing test_progs_verification_cert bytes
  $(VERIFY_SIG_HDR): $(VERIFICATION_CERT)
  	$(Q)ln -fs $< test_progs_verification_cert && \

I think we can remove the symlinking here too. It was a workaround for the fact that xxd derived the name of the variable to assign from the filename, so we could not use $< directly as a result. It would use underscores etc when representing the path; i.e. something like path_2_test_progs_verification_cert.

Newer xxd had an option to provide an explicit name, but we needed a workaround there since we couldn't rely on the fact xxd was new enough.

Now since we don't care about xxd we can dispense with the symlinking step.

tools/testing/selftests/bpf/Makefile $(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP) · line 730

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +4 -1
-	xxd -i test_progs_verification_cert > $@
+	(echo "unsigned char test_progs_verification_cert[] = {"; \
+	 hexdump -v -e '12/1 "  0x%02x," "\n"' test_progs_verification_cert | sed 's/0x  ,//g'; \
+	 echo "};"; \
+	 echo "unsigned int test_progs_verification_cert_len = $$(wc -c < test_progs_verification_cert);") > $@
 

After removing symlinking we would just need to change to using $< above instead of test_progs_verification_cert I think; following changes on top of your patch seem to do the trick in tidying this up:

tools/testing/selftests/bpf/.gitignore +0 -1
index b8bf51b7a0b0..a3ea98211ea6 100644
@@ -23,7 +23,6 @@ test_tcpnotify_user
  test_libbpf
  xdping
  test_cpp
-test_progs_verification_cert
  *.d
  *.subskel.h
  *.skel.h
tools/testing/selftests/bpf/Makefile +4 -6
index bbcd2101c3d7..f559f792c1be 100644
@@ -722,11 +722,10 @@ $(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP)
 
  # Generates a header with C array declaration, containing test_progs_verification_cert bytes
  $(VERIFY_SIG_HDR): $(VERIFICATION_CERT)
-       $(Q)ln -fs $< test_progs_verification_cert && \
-       (echo "unsigned char test_progs_verification_cert[] = {"; \
-        hexdump -v -e '12/1 "  0x%02x," "\n"' test_progs_verification_cert | sed 's/0x  ,//g'; \
+       $(Q)(echo "unsigned char test_progs_verification_cert[] = {"; \
+        hexdump -v -e '12/1 "  0x%02x," "\n"' $< | sed 's/0x  ,//g'; \
          echo "};"; \
-        echo "unsigned int test_progs_verification_cert_len = $$(wc -c < test_progs_verification_cert);") > $@
+        echo "unsigned int test_progs_verification_cert_len = $$(wc -c < $<);") > $@
 
  # Define test_progs test runner.
  TRUNNER_TESTS_DIR := prog_tests
@@ -902,8 +901,7 @@ EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)                   \
                                *.BTF *.BTF_ids *.BTF.base               \
                                no_alu32 cpuv4 bpf_gcc                   \
                                liburandom_read.so)                      \
-       $(OUTPUT)/FEATURE-DUMP.selftests                                \
-       test_progs_verification_cert
+       $(OUTPUT)/FEATURE-DUMP.selftests
 
  .PHONY: docs docs-clean
 

I'd suggest doing something like the above, but I tested both ways and it works well so

Quoted 1 line

Thanks for taking a look, you suggestions make sense, I'll apply them.