Re: [PATCH bpf-next 1/5] selftests/bpf: sockmap_redir: Simplify try_recv()

Jakub Sitnicki
2025-09-09T09:51:16Z

On Fri, Sep 05, 2025 at 01:11 PM +02, Michal Luczaj wrote:
> try_recv() was meant to support both @expect_success cases, but all the
> callers use @expect_success=false anyway. Drop the unused logic and fold in
> MSG_DONTWAIT. Adapt callers.
>
> Subtle change here: recv() return value of 0 will also be considered (an
> unexpected) success.
>
> Signed-off-by: Michal Luczaj <mhal@rbox.co>
> ---
> .../selftests/bpf/prog_tests/sockmap_redir.c | 25 +++++++++-------------
> 1 file changed, 10 insertions(+), 15 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c b/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
> index 9c461d93113db20de65ac353f92dfdbe32ffbd3b..c1bf1076e8152b7d83c3e07e2dce746b5a39cf7e 100644
> --- a/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
> @@ -144,17 +144,14 @@ static void get_redir_params(struct redir_spec *redir,
> *redirect_flags = 0;
> }
>
> -static void try_recv(const char *prefix, int fd, int flags, bool expect_success)
> +static void fail_recv(const char *prefix, int fd, int more_flags)
> {
> ssize_t n;
> char buf;
>
> - errno = 0;
> - n = recv(fd, &buf, 1, flags);
> - if (n < 0 && expect_success)
> - FAIL_ERRNO("%s: unexpected failure: retval=%zd", prefix, n);
> - if (!n && !expect_success)
> - FAIL("%s: expected failure: retval=%zd", prefix, n);
> + n = recv(fd, &buf, 1, MSG_DONTWAIT | more_flags);
> + if (n >= 0)
> + FAIL("%s: unexpected success: retval=%zd", prefix, n);
> }

This bit, which you highlighted in the description, I don't get.

If we're expecting to receive exactly one byte, why treat a short read
as a succcess? Why not make it a strict "n != 1" check? …