Skip to contentSkip to conversation

selftests/bpf: fix endianness issues in test_sysctl

PATCH Superseded Ilya Leoshkevich 3 participants 3 messages Aug 19, 2019 – Aug 21, 2019

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests/bpf: fix endianness issues in test_sysctl

A lot of test_sysctl sub-tests fail due to handling strings as a bunch of immediate values in a little-endian-specific manner.

Fix by wrapping all immediates in bpf_ntohl and the new bpf_be64_to_cpu.

Also, sometimes tests fail because sysctl() unexpectedly succeeds with an inappropriate "Unexpected failure" message and a random errno. Zero out errno before calling sysctl() and replace the message with "Unexpected success".

Fixes 1f5fa9ab6e2e ("selftests/bpf: Test BPF_CGROUP_SYSCTL")
Fixes 9a1027e52535 ("selftests/bpf: Test file_pos field in bpf_sysctl ctx")
Fixes 6041c67f28d8 ("selftests/bpf: Test bpf_sysctl_get_name helper")
Fixes 11ff34f74e32 ("selftests/bpf: Test sysctl_get_current_value helper")
Fixes 786047dd08de ("selftests/bpf: Test bpf_sysctl_{get,set}_new_value helpers")
Fixes 8549ddc832d6 ("selftests/bpf: Test bpf_strtol and bpf_strtoul helpers")

---
v1->v2: Use bpf_ntohl and bpf_be64_to_cpu, drop __bpf_le64_to_cpu.

 tools/testing/selftests/bpf/bpf_endian.h  |   7 ++
 tools/testing/selftests/bpf/test_sysctl.c | 130 ++++++++++++++--------
 2 files changed, 92 insertions(+), 45 deletions(-)
tools/testing/selftests/bpf/bpf_endian.h +7 -0
index 05f036df8a4c..f3be9322e89c 100644
@@ -29,6 +29,8 @@
 # define __bpf_htonl(x)			__builtin_bswap32(x)
 # define __bpf_constant_ntohl(x)	___constant_swab32(x)
 # define __bpf_constant_htonl(x)	___constant_swab32(x)
+# define __bpf_be64_to_cpu(x)		__builtin_bswap64(x)
+# define __bpf_constant_be64_to_cpu(x)	___constant_swab64(x)
 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
 # define __bpf_ntohs(x)			(x)
 # define __bpf_htons(x)			(x)
@@ -38,6 +40,8 @@
 # define __bpf_htonl(x)			(x)
 # define __bpf_constant_ntohl(x)	(x)
 # define __bpf_constant_htonl(x)	(x)
+# define __bpf_be64_to_cpu(x)		(x)
+# define __bpf_constant_be64_to_cpu(x)  (x)
 #else
 # error "Fix your compiler's __BYTE_ORDER__?!"
 #endif
@@ -54,5 +58,8 @@
 #define bpf_ntohl(x)				\
 	(__builtin_constant_p(x) ?		\
 	 __bpf_constant_ntohl(x) : __bpf_ntohl(x))
+#define bpf_be64_to_cpu(x)			\
+	(__builtin_constant_p(x) ?		\
+	 __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x))
 
 #endif /* __BPF_ENDIAN__ */
tools/testing/selftests/bpf/test_sysctl.c +85 -45

331 lines not on this page · raw message

Signature · 1 line
2.21.0
Acked-by on v2 1/1
#1 lore raw
Quoted 10 lines · Ilya Leoshkevich

A lot of test_sysctl sub-tests fail due to handling strings as a bunch of immediate values in a little-endian-specific manner.

Fix by wrapping all immediates in bpf_ntohl and the new bpf_be64_to_cpu.

Also, sometimes tests fail because sysctl() unexpectedly succeeds with an inappropriate "Unexpected failure" message and a random errno. Zero out errno before calling sysctl() and replace the message with "Unexpected success".

Quoted 7 lines
Fixes 1f5fa9ab6e2e ("selftests/bpf: Test BPF_CGROUP_SYSCTL")
Fixes 9a1027e52535 ("selftests/bpf: Test file_pos field in bpf_sysctl ctx")
Fixes 6041c67f28d8 ("selftests/bpf: Test bpf_sysctl_get_name helper")
Fixes 11ff34f74e32 ("selftests/bpf: Test sysctl_get_current_value helper")
Fixes 786047dd08de ("selftests/bpf: Test bpf_sysctl_{get,set}_new_value helpers")
Fixes 8549ddc832d6 ("selftests/bpf: Test bpf_strtol and bpf_strtoul helpers")
maintainer
#2 lore raw
In reply to Ilya Leoshkevich
Quoted 47 lines · Ilya Leoshkevich

A lot of test_sysctl sub-tests fail due to handling strings as a bunch of immediate values in a little-endian-specific manner.

Fix by wrapping all immediates in bpf_ntohl and the new bpf_be64_to_cpu.

Also, sometimes tests fail because sysctl() unexpectedly succeeds with an inappropriate "Unexpected failure" message and a random errno. Zero out errno before calling sysctl() and replace the message with "Unexpected success".

Fixes 1f5fa9ab6e2e ("selftests/bpf: Test BPF_CGROUP_SYSCTL")
Fixes 9a1027e52535 ("selftests/bpf: Test file_pos field in bpf_sysctl ctx")
Fixes 6041c67f28d8 ("selftests/bpf: Test bpf_sysctl_get_name helper")
Fixes 11ff34f74e32 ("selftests/bpf: Test sysctl_get_current_value helper")
Fixes 786047dd08de ("selftests/bpf: Test bpf_sysctl_{get,set}_new_value helpers")
Fixes 8549ddc832d6 ("selftests/bpf: Test bpf_strtol and bpf_strtoul helpers")

---
v1->v2: Use bpf_ntohl and bpf_be64_to_cpu, drop __bpf_le64_to_cpu.

  tools/testing/selftests/bpf/bpf_endian.h  |   7 ++
  tools/testing/selftests/bpf/test_sysctl.c | 130 ++++++++++++++--------
  2 files changed, 92 insertions(+), 45 deletions(-)
tools/testing/selftests/bpf/bpf_endian.h +4 -0
index 05f036df8a4c..f3be9322e89c 100644
@@ -29,6 +29,8 @@
  # define __bpf_htonl(x)			__builtin_bswap32(x)
  # define __bpf_constant_ntohl(x)	___constant_swab32(x)
  # define __bpf_constant_htonl(x)	___constant_swab32(x)
+# define __bpf_be64_to_cpu(x)		__builtin_bswap64(x)
+# define __bpf_constant_be64_to_cpu(x)	___constant_swab64(x)
  #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  # define __bpf_ntohs(x)			(x)
  # define __bpf_htons(x)			(x)
@@ -38,6 +40,8 @@
  # define __bpf_htonl(x)			(x)
  # define __bpf_constant_ntohl(x)	(x)
  # define __bpf_constant_htonl(x)	(x)
+# define __bpf_be64_to_cpu(x)		(x)
+# define __bpf_constant_be64_to_cpu(x)  (x)
  #else
  # error "Fix your compiler's __BYTE_ORDER__?!"
  #endif

tools/testing/selftests/bpf/bpf_endian.h line 65

Quoted 8 lines
tools/testing/selftests/bpf/bpf_endian.h +3 -0
@@ -54,5 +58,8 @@
  #define bpf_ntohl(x)				\
  	(__builtin_constant_p(x) ?		\
  	 __bpf_constant_ntohl(x) : __bpf_ntohl(x))
+#define bpf_be64_to_cpu(x)			\
+	(__builtin_constant_p(x) ?		\
+	 __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x))
 
  #endif /* __BPF_ENDIAN__ */

By the way, looks like progs/test_lwt_seg6local.c defines its own ntohll()/htonll(), could we make this two patches: i) consolidating the former by adding proper variants to bpf_endian.h (I mean generically) and ii) fixing test_sysctl.c by using them? Thinking addition of i) as plan for bpf-next would be to move bpf_endian.h as proper API into libbpf in near future such that BPF progs don't need to redefine them.

Thanks,
Daniel