From: Jakub Sitnicki <jakub@cloudflare.com>
To: Michal Luczaj <mhal@rbox.co>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@fomichev.me>,
Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>,
Mykola Lysenko <mykolal@fb.com>, Shuah Khan <shuah@kernel.org>,
bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH bpf-next 1/5] selftests/bpf: sockmap_redir: Simplify try_recv()
Date: Wed, 10 Sep 2025 11:43:20 +0200 [thread overview]
Message-ID: <877by663t3.fsf@cloudflare.com> (raw)
In-Reply-To: <35912d55-eb6e-403a-9a7a-05cae551ccf3@rbox.co> (Michal Luczaj's message of "Tue, 9 Sep 2025 23:25:16 +0200")
On Tue, Sep 09, 2025 at 11:25 PM +02, Michal Luczaj wrote:
> On 9/9/25 12:15, Jakub Sitnicki wrote:
>> On Tue, Sep 09, 2025 at 11:51 AM +02, Jakub Sitnicki wrote:
>>> 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?
>>>
>>> [...]
>>
>> Nevermind. It makes sense now. We do want to report a failure for 0-len
>> msg recv as well. You're effectively checking if the rcv queue is empty.
>>
>> I'd add MSG_PEEK, to signal that we're _just checking_ if the socket is
>> readable, and turn the check into the below to succeed only when
>> queue is empty:
>>
>> (n != -1 || (errno != EAGAIN && errno != EWOULDBLOCK))
>
> Well, looks like adding MSG_PEEK exposed a bug in the test. I'll fix that.
The gift that keeps on giving xD
Other alternatives that should also work, but who knows:
- select/poll/epoll readability check
- ioctl(SIOCINQ) but no way to tell if 0-len msg is pending
next prev parent reply other threads:[~2025-09-10 9:43 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-05 11:11 [PATCH bpf-next 0/5] selftests/bpf: Extend sockmap_redir to test no-redir SK_DROP/SK_PASS Michal Luczaj
2025-09-05 11:11 ` [PATCH bpf-next 1/5] selftests/bpf: sockmap_redir: Simplify try_recv() Michal Luczaj
2025-09-09 9:51 ` Jakub Sitnicki
2025-09-09 10:15 ` Jakub Sitnicki
2025-09-09 21:25 ` Michal Luczaj
2025-09-10 9:43 ` Jakub Sitnicki [this message]
2025-09-05 11:11 ` [PATCH bpf-next 2/5] selftests/bpf: sockmap_redir: Fix OOB handling Michal Luczaj
2025-09-05 11:19 ` Michal Luczaj
2025-09-19 11:29 ` Jakub Sitnicki
2025-09-19 9:49 ` Jakub Sitnicki
2025-09-05 11:11 ` [PATCH bpf-next 3/5] selftests/bpf: sockmap_redir: Rename functions Michal Luczaj
2025-09-19 9:54 ` Jakub Sitnicki
2025-09-05 11:11 ` [PATCH bpf-next 4/5] selftests/bpf: sockmap_redir: Let test specify skel's redirect_type Michal Luczaj
2025-09-19 9:55 ` Jakub Sitnicki
2025-09-05 11:11 ` [PATCH bpf-next 5/5] selftests/bpf: sockmap_redir: Support no-redirect SK_DROP/SK_PASS Michal Luczaj
2025-09-19 11:28 ` Jakub Sitnicki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=877by663t3.fsf@cloudflare.com \
--to=jakub@cloudflare.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mhal@rbox.co \
--cc=mykolal@fb.com \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox