Fixes for sock_fields selftests
Subsystems (5)
v3 cover Fixes for sock_fields selftests
I think we have reached a consensus [1] on how the test for the 4-byte load from bpf_sock->dst_port and bpf_sk_lookup->remote_port should look, so here goes v3.
I will submit a separate set of patches for bpf_sk_lookup->remote_port tests.
This series has been tested on x86_64 and s390 on top of recent bpf-next - ad13baf45691 ("selftests/bpf: Test subprog jit when toggle bpf_jit_harden repeatedly").
[1] https://lore.kernel.org/bpf/[email protected]/
v2 -> v3:
- Split what was previously patch 2 which was doing two things
- Use BPF_TCP_* constants (Martin)
- Treat the result of 4-byte load from dst_port as a 16-bit value (Martin)
- Typo fixup and some rewording in patch 4 description
v1 -> v2:
- Limit read_sk_dst_port only to client traffic (patch 2)
- Make read_sk_dst_port pass on litte- and big-endian (patch 3)
v1: https://lore.kernel.org/bpf/[email protected]/
v2: https://lore.kernel.org/bpf/[email protected]/
Jakub Sitnicki (4):
selftests/bpf: Fix error reporting from sock_fields programs selftests/bpf: Check dst_port only on the client socket selftests/bpf: Use constants for socket states in sock_fields test selftests/bpf: Fix test for 4-byte load from dst_port on big-endian
.../selftests/bpf/progs/test_sock_fields.c | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-)
Signature · 1 line
2.35.1
PATCH v3 1/4 selftests/bpf: Fix error reporting from sock_fields programs
The helper macro that records an error in BPF programs that exercise sock fields access has been inadvertently broken by adaptation work that happened in commit b18c1f0aa477 ("bpf: selftest: Adapt sock_fields test to use skel and global variables").
BPF_NOEXIST flag cannot be used to update BPF_MAP_TYPE_ARRAY. The operation always fails with -EEXIST, which in turn means the error never gets recorded, and the checks for errors always pass.
Revert the change in update flags.
--- tools/testing/selftests/bpf/progs/test_sock_fields.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/test_sock_fields.c +1 -1
@@ -114,7 +114,7 @@ static void tpcpy(struct bpf_tcp_sock *dst,#define RET_LOG () ({ \ linum = __LINE__; \ - bpf_map_update_elem(&linum_map, &linum_idx, &linum, BPF_NOEXIST); \ + bpf_map_update_elem(&linum_map, &linum_idx, &linum, BPF_ANY); \ return CG_OK; \ })
Signature · 1 line
2.35.1
PATCH v3 2/4 selftests/bpf: Check dst_port only on the client socket
In reply to Jakub Sitnickicgroup_skb/egress programs which sock_fields test installs process packets flying in both directions, from the client to the server, and in reverse direction.
Recently added dst_port check relies on the fact that destination port (remote peer port) of the socket which sends the packet is known ahead of time. This holds true only for the client socket, which connects to the known server port.
Filter out any traffic that is not egressing from the client socket in the BPF program that tests reading the dst_port.
--- tools/testing/selftests/bpf/progs/test_sock_fields.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/testing/selftests/bpf/progs/test_sock_fields.c +4 -0
@@ -281,6 +281,10 @@ int read_sk_dst_port(struct __sk_buff *skb)if (!sk)RET_LOG (); +/* Ignore everything but the SYN from the client socket */ +if (sk->state !=BPF_TCP_SYN_SENT ) +return CG_OK ; +if (!sk_dst_port__load_word (sk))RET_LOG ();if (!sk_dst_port__load_half (sk))
Signature · 1 line
2.35.1
PATCH v3 3/4 selftests/bpf: Use constants for socket states in sock_fields test
In reply to Jakub SitnickiReplace magic numbers in BPF code with constants from bpf.h, so that they don't require an explanation in the comments.
--- tools/testing/selftests/bpf/progs/test_sock_fields.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/progs/test_sock_fields.c +5 -5
@@ -134,11 +134,11 @@ int egress_read_sock_fields(struct __sk_buff *skb)if (!sk)RET_LOG (); -/* Not the testing egress traffic or -* TCP_LISTEN (10) socket will be copied at the ingress side. +/* Not testing the egress traffic or the listening socket, +* which are covered by the cgroup_skb/ingress test program. */ if (sk->family !=AF_INET6 || !is_loopback6 (sk->src_ip6) || - sk->state ==10 ) + sk->state ==BPF_TCP_LISTEN )return CG_OK ;if (sk->src_port ==bpf_ntohs (srv_sa6.sin6_port)) {
@@ -232,8 +232,8 @@ int ingress_read_sock_fields(struct __sk_buff *skb) sk->src_port != bpf_ntohs(srv_sa6.sin6_port))return CG_OK ; -/* Only interested in TCP_LISTEN */ -if (sk->state !=10 ) +/* Only interested in the listening socket */ +if (sk->state !=BPF_TCP_LISTEN )return CG_OK ;/* It must be a fullsock for cgroup_skb/ingress prog */
Signature · 1 line
2.35.1
PATCH v3 4/4 selftests/bpf: Fix test for 4-byte load from dst_port on big-endian
In reply to Jakub SitnickiThe check for 4-byte load from dst_port offset into bpf_sock is failing on big-endian architecture - s390. The bpf access converter rewrites the 4-byte load to a 2-byte load from sock_common at skc_dport offset, as shown below.
* s390 / llvm-objdump -S --no-show-raw-insn
00000000000002a0 <sk_dst_port__load_word>:
84: r1 = *(u32 *)(r1 + 48)
85: w0 = 1
86: if w1 == 51966 goto +1 <LBB5_2>
87: w0 = 0 00000000000002c0 <LBB5_2>:
88: exit
* s390 / bpftool prog dump xlated
_Bool sk_dst_port__load_word(struct bpf_sock * sk):
35: (69) r1 = *(u16 *)(r1 +12)
36: (bc) w1 = w1
37: (b4) w0 = 1
38: (16) if w1 == 0xcafe goto pc+1
39: (b4) w0 = 0
40: (95) exit * x86_64 / llvm-objdump -S --no-show-raw-insn
00000000000002a0 <sk_dst_port__load_word>:
84: r1 = *(u32 *)(r1 + 48)
85: w0 = 1
86: if w1 == 65226 goto +1 <LBB5_2>
87: w0 = 0 00000000000002c0 <LBB5_2>:
88: exit
* x86_64 / bpftool prog dump xlated
_Bool sk_dst_port__load_word(struct bpf_sock * sk):
33: (69) r1 = *(u16 *)(r1 +12)
34: (b4) w0 = 1
35: (16) if w1 == 0xfeca goto pc+1
36: (b4) w0 = 0
37: (95) exitThis leads to surprises if we treat the destination register contents as a 32-bit value, ignoring the fact that in reality it contains a 16-bit value.
On little-endian the register contents reflect the bpf_sock struct definition, where the lower 16-bits contain the port number:
struct bpf_sock {
...
__be16 dst_port; /* offset 48 */
__u16 :16;
...
};However, on big-endian the register contents suggest that field the layout of bpf_sock struct is as so:
struct bpf_sock {
...
__u16 :16; /* offset 48 */
__be16 dst_port;
...
};Account for this quirky access conversion in the test case exercising the 4-byte load by treating the result as 16-bit wide.
--- tools/testing/selftests/bpf/progs/test_sock_fields.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/test_sock_fields.c +7 -1
@@ -251,10 +251,16 @@ int ingress_read_sock_fields(struct __sk_buff *skb)return CG_OK ; } +/* +* NOTE: 4-byte load from bpf_sock at dst_port offset is quirky. It +* gets rewritten by the access converter to a 2-byte load for +* backward compatibility. Treating the load result as a be16 value +* makes the code portable across little- and big-endian platforms. +*/ static __noinline bool sk_dst_port__load_word(struct bpf_sock *sk) {__u32 *word = (__u32 *)&sk->dst_port; -return word[0 ] ==bpf_htonl (0xcafe0000 ); +return word[0 ] ==bpf_htons (0xcafe ); } static __noinline bool sk_dst_port__load_half(struct bpf_sock *sk)
Signature · 1 line
2.35.1
Quoted 10 lines · Jakub Sitnicki
I think we have reached a consensus [1] on how the test for the 4-byte load from
bpf_sock->dst_portandbpf_sk_lookup->remote_portshould look, so here goes v3.I will submit a separate set of patches for
bpf_sk_lookup->remote_porttests.This series has been tested on
x86_64and s390 on top of recent bpf-next -ad13baf45691("selftests/bpf: Test subprog jit when togglebpf_jit_hardenrepeatedly").
Quoted 7 lines
[1] https://lore.kernel.org/bpf/[email protected]/
v2 -> v3:
- Split what was previously patch 2 which was doing two things
- Use BPF_TCP_* constants (Martin)
- Treat the result of 4-byte load fromdst_portas a 16-bit value (Martin)
- Typo fixup and some rewording in patch 4 description
Thanks for your work on this and reached a resolution with the remote_port !
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Quoted 11 lines · On Thu, 17 Mar 2022 12:39:16 +0100 you wrote:
I think we have reached a consensus [1] on how the test for the 4-byte load from
bpf_sock->dst_portandbpf_sk_lookup->remote_portshould look, so here goes v3.I will submit a separate set of patches for
bpf_sk_lookup->remote_porttests.This series has been tested on
x86_64and s390 on top of recent bpf-next -ad13baf45691("selftests/bpf: Test subprog jit when togglebpf_jit_hardenrepeatedly").[...]
Here is the summary with links:
- [bpf-next,v3,1/4] selftests/bpf: Fix error reporting from sock_fields programs
https://git.kernel.org/bpf/bpf-next/c/a4c9fe0ed4a1 - [bpf-next,v3,2/4] selftests/bpf: Check dst_port only on the client socket https://git.kernel.org/bpf/bpf-next/c/2d2202ba858c - [bpf-next,v3,3/4] selftests/bpf: Use constants for socket states in sock_fields test https://git.kernel.org/bpf/bpf-next/c/e06b5bbcf3f1 - [bpf-next,v3,4/4] selftests/bpf: Fix test for 4-byte load from dst_port on big-endian https://git.kernel.org/bpf/bpf-next/c/deb594004644
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html