All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests
@ 2022-03-17 11:39 Jakub Sitnicki
  2022-03-17 11:39 ` [PATCH bpf-next v3 1/4] selftests/bpf: Fix error reporting from sock_fields programs Jakub Sitnicki
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Jakub Sitnicki @ 2022-03-17 11:39 UTC (permalink / raw)
  To: bpf
  Cc: netdev, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	kernel-team, Ilya Leoshkevich, Martin KaFai Lau

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/87k0cwxkzs.fsf@cloudflare.com/

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/20220225184130.483208-1-jakub@cloudflare.com/
v2: https://lore.kernel.org/bpf/20220227202757.519015-1-jakub@cloudflare.com/

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(-)

-- 
2.35.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH bpf-next v3 1/4] selftests/bpf: Fix error reporting from sock_fields programs
  2022-03-17 11:39 [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests Jakub Sitnicki
@ 2022-03-17 11:39 ` Jakub Sitnicki
  2022-03-17 11:39 ` [PATCH bpf-next v3 2/4] selftests/bpf: Check dst_port only on the client socket Jakub Sitnicki
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Jakub Sitnicki @ 2022-03-17 11:39 UTC (permalink / raw)
  To: bpf
  Cc: netdev, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	kernel-team, Ilya Leoshkevich, Martin KaFai Lau

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.

Fixes: b18c1f0aa477 ("bpf: selftest: Adapt sock_fields test to use skel and global variables")
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 tools/testing/selftests/bpf/progs/test_sock_fields.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/progs/test_sock_fields.c b/tools/testing/selftests/bpf/progs/test_sock_fields.c
index 246f1f001813..3e2e3ee51cc9 100644
--- a/tools/testing/selftests/bpf/progs/test_sock_fields.c
+++ b/tools/testing/selftests/bpf/progs/test_sock_fields.c
@@ -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;						\
 })
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf-next v3 2/4] selftests/bpf: Check dst_port only on the client socket
  2022-03-17 11:39 [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests Jakub Sitnicki
  2022-03-17 11:39 ` [PATCH bpf-next v3 1/4] selftests/bpf: Fix error reporting from sock_fields programs Jakub Sitnicki
@ 2022-03-17 11:39 ` Jakub Sitnicki
  2022-03-17 11:39 ` [PATCH bpf-next v3 3/4] selftests/bpf: Use constants for socket states in sock_fields test Jakub Sitnicki
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Jakub Sitnicki @ 2022-03-17 11:39 UTC (permalink / raw)
  To: bpf
  Cc: netdev, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	kernel-team, Ilya Leoshkevich, Martin KaFai Lau

cgroup_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.

Fixes: 8f50f16ff39d ("selftests/bpf: Extend verifier and bpf_sock tests for dst_port loads")
Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 tools/testing/selftests/bpf/progs/test_sock_fields.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/test_sock_fields.c b/tools/testing/selftests/bpf/progs/test_sock_fields.c
index 3e2e3ee51cc9..43b31aa1fcf7 100644
--- a/tools/testing/selftests/bpf/progs/test_sock_fields.c
+++ b/tools/testing/selftests/bpf/progs/test_sock_fields.c
@@ -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))
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf-next v3 3/4] selftests/bpf: Use constants for socket states in sock_fields test
  2022-03-17 11:39 [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests Jakub Sitnicki
  2022-03-17 11:39 ` [PATCH bpf-next v3 1/4] selftests/bpf: Fix error reporting from sock_fields programs Jakub Sitnicki
  2022-03-17 11:39 ` [PATCH bpf-next v3 2/4] selftests/bpf: Check dst_port only on the client socket Jakub Sitnicki
@ 2022-03-17 11:39 ` Jakub Sitnicki
  2022-03-17 11:39 ` [PATCH bpf-next v3 4/4] selftests/bpf: Fix test for 4-byte load from dst_port on big-endian Jakub Sitnicki
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Jakub Sitnicki @ 2022-03-17 11:39 UTC (permalink / raw)
  To: bpf
  Cc: netdev, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	kernel-team, Ilya Leoshkevich, Martin KaFai Lau

Replace magic numbers in BPF code with constants from bpf.h, so that they
don't require an explanation in the comments.

Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 tools/testing/selftests/bpf/progs/test_sock_fields.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/bpf/progs/test_sock_fields.c b/tools/testing/selftests/bpf/progs/test_sock_fields.c
index 43b31aa1fcf7..43a17fdef226 100644
--- a/tools/testing/selftests/bpf/progs/test_sock_fields.c
+++ b/tools/testing/selftests/bpf/progs/test_sock_fields.c
@@ -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 */
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf-next v3 4/4] selftests/bpf: Fix test for 4-byte load from dst_port on big-endian
  2022-03-17 11:39 [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests Jakub Sitnicki
                   ` (2 preceding siblings ...)
  2022-03-17 11:39 ` [PATCH bpf-next v3 3/4] selftests/bpf: Use constants for socket states in sock_fields test Jakub Sitnicki
@ 2022-03-17 11:39 ` Jakub Sitnicki
  2022-03-18  1:01 ` [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests Martin KaFai Lau
  2022-03-18 14:50 ` patchwork-bot+netdevbpf
  5 siblings, 0 replies; 7+ messages in thread
From: Jakub Sitnicki @ 2022-03-17 11:39 UTC (permalink / raw)
  To: bpf
  Cc: netdev, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	kernel-team, Ilya Leoshkevich, Martin KaFai Lau

The 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) exit

This 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.

Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 tools/testing/selftests/bpf/progs/test_sock_fields.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/progs/test_sock_fields.c b/tools/testing/selftests/bpf/progs/test_sock_fields.c
index 43a17fdef226..9f4b8f9f1181 100644
--- a/tools/testing/selftests/bpf/progs/test_sock_fields.c
+++ b/tools/testing/selftests/bpf/progs/test_sock_fields.c
@@ -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)
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests
  2022-03-17 11:39 [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests Jakub Sitnicki
                   ` (3 preceding siblings ...)
  2022-03-17 11:39 ` [PATCH bpf-next v3 4/4] selftests/bpf: Fix test for 4-byte load from dst_port on big-endian Jakub Sitnicki
@ 2022-03-18  1:01 ` Martin KaFai Lau
  2022-03-18 14:50 ` patchwork-bot+netdevbpf
  5 siblings, 0 replies; 7+ messages in thread
From: Martin KaFai Lau @ 2022-03-18  1:01 UTC (permalink / raw)
  To: Jakub Sitnicki
  Cc: bpf, netdev, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, kernel-team, Ilya Leoshkevich

On Thu, Mar 17, 2022 at 12:39:16PM +0100, Jakub Sitnicki wrote:
> 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/87k0cwxkzs.fsf@cloudflare.com/
> 
> 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
Thanks for your work on this and reached a resolution with the remote_port !

Acked-by: Martin KaFai Lau <kafai@fb.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests
  2022-03-17 11:39 [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests Jakub Sitnicki
                   ` (4 preceding siblings ...)
  2022-03-18  1:01 ` [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests Martin KaFai Lau
@ 2022-03-18 14:50 ` patchwork-bot+netdevbpf
  5 siblings, 0 replies; 7+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-03-18 14:50 UTC (permalink / raw)
  To: Jakub Sitnicki; +Cc: bpf, netdev, ast, daniel, andrii, kernel-team, iii, kafai

Hello:

This series was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

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_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").
> 
> [...]

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-03-18 14:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-17 11:39 [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests Jakub Sitnicki
2022-03-17 11:39 ` [PATCH bpf-next v3 1/4] selftests/bpf: Fix error reporting from sock_fields programs Jakub Sitnicki
2022-03-17 11:39 ` [PATCH bpf-next v3 2/4] selftests/bpf: Check dst_port only on the client socket Jakub Sitnicki
2022-03-17 11:39 ` [PATCH bpf-next v3 3/4] selftests/bpf: Use constants for socket states in sock_fields test Jakub Sitnicki
2022-03-17 11:39 ` [PATCH bpf-next v3 4/4] selftests/bpf: Fix test for 4-byte load from dst_port on big-endian Jakub Sitnicki
2022-03-18  1:01 ` [PATCH bpf-next v3 0/4] Fixes for sock_fields selftests Martin KaFai Lau
2022-03-18 14:50 ` patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.