From patchwork Wed Apr 10 06:13:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623599 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADBF120309; Wed, 10 Apr 2024 06:13:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712729633; cv=none; b=gwWLLGippcd/7Quba0f2EjgQ36cMho+9ZTR2l1xBoWrG6IY5+WAtCO/Qi+31FdF0yHPPar61IyC1Kicv7OLoz+cK26V+mt1QM3kbtGurIYZtWOST41dhczoNwrR0EqOB1QARmcA38WEzMeKmI0QPgXN5QI/bd8YEORf1jr5CNuM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712729633; c=relaxed/simple; bh=ZS6Ft5i8Dr7yUuNXwy5mu3ymuNubjt/vAswNcv6k4D0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AoKwpelCROQJEJY28jl7X6DBcU1UtypDBMIzF6a09or30z8IRmkNJ+6Q7BJ5yAfdiIVeu35I0qR8/ac2nFSovUh2oWvhV5SiPubhRXC1Xd9CA7UWmod5Tw0SxdTuMXC+NfaShe4jWEHzWnU6O2+C0ftr3KIUVjZPECvEpkVMCkQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XPgzG6Eu; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="XPgzG6Eu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5F1B2C43390; Wed, 10 Apr 2024 06:13:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712729633; bh=ZS6Ft5i8Dr7yUuNXwy5mu3ymuNubjt/vAswNcv6k4D0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XPgzG6Eumlrsop2gn+9RrP6Qy/XYnqaVlEUIZQki2mU9O1I3pZfmM2vo97iXatQIS WrFBs4QgBZymHSHyWkb1wC055ZU1q4oSA5zrHrxy/KS3cJe6Ff5yAQmsDU+JcK0x/K VMSuN5ep9q+IvMDnQ9v5jewHtbP8Wrt+lUGo7z5hzhQ6FTp7I8NkoDHtVOHVhai/r6 IZbz6jMg4DMeLulfoTpTdyHEmZd0EW6f5sxIss5HJwZaFqEmVoK0bqwU4/KPYH13Ku TYAvtmXvGUJkuM+1BwqqI4kNa7bBt2FUqYQWkFj19WrK/Osa6YKcaAotecDBm0a39O uF5FuOkvKIn0w== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 1/3] selftests/bpf: Add struct send_recv_arg Date: Wed, 10 Apr 2024 14:13:29 +0800 Message-Id: <70e9f036964554db59de08bd8b4273e8274d4fce.1712729342.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Avoid setting total_bytes and stop as global variables, this patch adds a new struct named send_recv_arg to pass arguments between threads. Put these two variables together with fd into this struct and pass it to server thread, so that server thread can access these two variables without setting them as global ones. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c index 077b107130f6..64f172f02a9a 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c @@ -21,7 +21,6 @@ static const unsigned int total_bytes = 10 * 1024 * 1024; static int expected_stg = 0xeB9F; -static int stop; static int settcpca(int fd, const char *tcp_ca) { @@ -34,13 +33,20 @@ static int settcpca(int fd, const char *tcp_ca) return 0; } +struct send_recv_arg { + int fd; + uint32_t bytes; + int stop; +}; + static void *server(void *arg) { - int lfd = (int)(long)arg, err = 0, fd; + struct send_recv_arg *a = (struct send_recv_arg *)arg; ssize_t nr_sent = 0, bytes = 0; char batch[1500]; + int err = 0, fd; - fd = accept(lfd, NULL, NULL); + fd = accept(a->fd, NULL, NULL); while (fd == -1) { if (errno == EINTR) continue; @@ -53,9 +59,9 @@ static void *server(void *arg) goto done; } - while (bytes < total_bytes && !READ_ONCE(stop)) { + while (bytes < a->bytes && !READ_ONCE(a->stop)) { nr_sent = send(fd, &batch, - MIN(total_bytes - bytes, sizeof(batch)), 0); + MIN(a->bytes - bytes, sizeof(batch)), 0); if (nr_sent == -1 && errno == EINTR) continue; if (nr_sent == -1) { @@ -65,13 +71,13 @@ static void *server(void *arg) bytes += nr_sent; } - ASSERT_EQ(bytes, total_bytes, "send"); + ASSERT_EQ(bytes, a->bytes, "send"); done: if (fd >= 0) close(fd); if (err) { - WRITE_ONCE(stop, 1); + WRITE_ONCE(a->stop, 1); return ERR_PTR(err); } return NULL; @@ -80,18 +86,22 @@ static void *server(void *arg) static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) { ssize_t nr_recv = 0, bytes = 0; + struct send_recv_arg arg = { + .bytes = total_bytes, + .stop = 0, + }; int lfd = -1, fd = -1; pthread_t srv_thread; void *thread_ret; char batch[1500]; int err; - WRITE_ONCE(stop, 0); - lfd = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0); if (!ASSERT_NEQ(lfd, -1, "socket")) return; + arg.fd = lfd; + fd = socket(AF_INET6, SOCK_STREAM, 0); if (!ASSERT_NEQ(fd, -1, "socket")) { close(lfd); @@ -123,12 +133,12 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) goto done; } - err = pthread_create(&srv_thread, NULL, server, (void *)(long)lfd); + err = pthread_create(&srv_thread, NULL, server, (void *)&arg); if (!ASSERT_OK(err, "pthread_create")) goto done; /* recv total_bytes */ - while (bytes < total_bytes && !READ_ONCE(stop)) { + while (bytes < total_bytes && !READ_ONCE(arg.stop)) { nr_recv = recv(fd, &batch, MIN(total_bytes - bytes, sizeof(batch)), 0); if (nr_recv == -1 && errno == EINTR) @@ -140,7 +150,7 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) ASSERT_EQ(bytes, total_bytes, "recv"); - WRITE_ONCE(stop, 1); + WRITE_ONCE(arg.stop, 1); pthread_join(srv_thread, &thread_ret); ASSERT_OK(IS_ERR(thread_ret), "thread_ret"); From patchwork Wed Apr 10 06:13:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623600 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77AE436B08; Wed, 10 Apr 2024 06:14:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712729640; cv=none; b=a34vHk6AkuRQMAY4i8IhNcISsEivv/4Uzc/DLzSjQCKdEdytXlxhUt6K/+iluw0WVQELbyJhh32uZnlu8tHbFtebR+sxdpQpGcn6VIRqgZ0vyFft+PqMl7Im57zliGChzTrm2jJWj9+k+09hDpEULZx1wogmgnBQFfc1i/eXfUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712729640; c=relaxed/simple; bh=w+zC3cBptcMJBy/TE8m7jMBOOt1knNP/NZbw9OQbEOs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CEB3kE3wm7fF6lCYH/Y0RcBzp/QOrY5qlzRptysxq09K+U4nXazoZNCCNJ+Luh9it7nv7NjSR0UDn6GLkTTtgIdZ0qgRK7VUO7C44DAKH5L3iMH0o8wdRX2+2RbYwE4oxYZN45pseShQ7z17s1qJA8g1zm9C2ni8k3kqMHzTsgo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GT0Ic6nc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GT0Ic6nc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9310C43390; Wed, 10 Apr 2024 06:13:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712729640; bh=w+zC3cBptcMJBy/TE8m7jMBOOt1knNP/NZbw9OQbEOs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GT0Ic6ncgx4nL60r1eoiv7RFw8ZGDeT4ougP2+Aw7QljY/zhemB4pK/bhMGaMkR2P 3Ko/eApElSdk+XLA7/eSv0kVix86w4CWr3vwO9RxVDfiWnoBhdJoD2GSntYE6Z0s85 sgSgOx8Yy5oxPA2+Qss3HqVsX9fbPsg4PR07pasp3PxZ/GrqH4coymuaQ8XIybF+y5 RWBq4LGrjMB91VSbMebkZ3Tl2NVcAn+8IVg9pHxR1K15xiopHIJP3WI0RVb+JxPv2I JQ9g8WgICkS8QiCe1TW2wteIuV9RCv7C19TM/73kjPV3z+dzGjAacmnIq+oby3L4eH SfI+MW0VrNrIA== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 2/3] selftests/bpf: Export send_recv_data helper Date: Wed, 10 Apr 2024 14:13:30 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang This patch extracts the code to send and receive data into a new helper named send_recv_data() in network_helpers.c and export it in network_helpers.h. This helper will be used for MPTCP BPF selftests. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 96 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 81 +--------------- 3 files changed, 98 insertions(+), 80 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 04175e16195a..137cd18ef3f2 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -545,3 +545,99 @@ int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param) close(sockfd); return 0; } + +struct send_recv_arg { + int fd; + uint32_t bytes; + int stop; +}; + +static void *send_recv_server(void *arg) +{ + struct send_recv_arg *a = (struct send_recv_arg *)arg; + ssize_t nr_sent = 0, bytes = 0; + char batch[1500]; + int err = 0, fd; + + fd = accept(a->fd, NULL, NULL); + while (fd == -1) { + if (errno == EINTR) + continue; + err = -errno; + goto done; + } + + if (settimeo(fd, 0)) { + err = -errno; + goto done; + } + + while (bytes < a->bytes && !READ_ONCE(a->stop)) { + nr_sent = send(fd, &batch, + MIN(a->bytes - bytes, sizeof(batch)), 0); + if (nr_sent == -1 && errno == EINTR) + continue; + if (nr_sent == -1) { + err = -errno; + break; + } + bytes += nr_sent; + } + + if (bytes != a->bytes) + log_err("send"); + +done: + if (fd >= 0) + close(fd); + if (err) { + WRITE_ONCE(a->stop, 1); + return ERR_PTR(err); + } + return NULL; +} + +int send_recv_data(int lfd, int fd, uint32_t total_bytes) +{ + ssize_t nr_recv = 0, bytes = 0; + struct send_recv_arg arg = { + .fd = lfd, + .bytes = total_bytes, + .stop = 0, + }; + pthread_t srv_thread; + void *thread_ret; + char batch[1500]; + int err; + + err = pthread_create(&srv_thread, NULL, send_recv_server, (void *)&arg); + if (err) { + log_err("pthread_create"); + return err; + } + + /* recv total_bytes */ + while (bytes < total_bytes && !READ_ONCE(arg.stop)) { + nr_recv = recv(fd, &batch, + MIN(total_bytes - bytes, sizeof(batch)), 0); + if (nr_recv == -1 && errno == EINTR) + continue; + if (nr_recv == -1) + break; + bytes += nr_recv; + } + + if (bytes != total_bytes) { + log_err("recv"); + return -1; + } + + WRITE_ONCE(arg.stop, 1); + pthread_join(srv_thread, &thread_ret); + if (IS_ERR(thread_ret)) { + log_err("thread_ret"); + return -1; + } + + return 0; +} diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 6457445cc6e2..70f4e4c92733 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -76,6 +76,7 @@ struct nstoken; */ struct nstoken *open_netns(const char *name); void close_netns(struct nstoken *token); +int send_recv_data(int lfd, int fd, uint32_t total_bytes); static __u16 csum_fold(__u32 csum) { diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c index 64f172f02a9a..907bac46c774 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c @@ -33,75 +33,15 @@ static int settcpca(int fd, const char *tcp_ca) return 0; } -struct send_recv_arg { - int fd; - uint32_t bytes; - int stop; -}; - -static void *server(void *arg) -{ - struct send_recv_arg *a = (struct send_recv_arg *)arg; - ssize_t nr_sent = 0, bytes = 0; - char batch[1500]; - int err = 0, fd; - - fd = accept(a->fd, NULL, NULL); - while (fd == -1) { - if (errno == EINTR) - continue; - err = -errno; - goto done; - } - - if (settimeo(fd, 0)) { - err = -errno; - goto done; - } - - while (bytes < a->bytes && !READ_ONCE(a->stop)) { - nr_sent = send(fd, &batch, - MIN(a->bytes - bytes, sizeof(batch)), 0); - if (nr_sent == -1 && errno == EINTR) - continue; - if (nr_sent == -1) { - err = -errno; - break; - } - bytes += nr_sent; - } - - ASSERT_EQ(bytes, a->bytes, "send"); - -done: - if (fd >= 0) - close(fd); - if (err) { - WRITE_ONCE(a->stop, 1); - return ERR_PTR(err); - } - return NULL; -} - static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) { - ssize_t nr_recv = 0, bytes = 0; - struct send_recv_arg arg = { - .bytes = total_bytes, - .stop = 0, - }; int lfd = -1, fd = -1; - pthread_t srv_thread; - void *thread_ret; - char batch[1500]; int err; lfd = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0); if (!ASSERT_NEQ(lfd, -1, "socket")) return; - arg.fd = lfd; - fd = socket(AF_INET6, SOCK_STREAM, 0); if (!ASSERT_NEQ(fd, -1, "socket")) { close(lfd); @@ -133,26 +73,7 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) goto done; } - err = pthread_create(&srv_thread, NULL, server, (void *)&arg); - if (!ASSERT_OK(err, "pthread_create")) - goto done; - - /* recv total_bytes */ - while (bytes < total_bytes && !READ_ONCE(arg.stop)) { - nr_recv = recv(fd, &batch, - MIN(total_bytes - bytes, sizeof(batch)), 0); - if (nr_recv == -1 && errno == EINTR) - continue; - if (nr_recv == -1) - break; - bytes += nr_recv; - } - - ASSERT_EQ(bytes, total_bytes, "recv"); - - WRITE_ONCE(arg.stop, 1); - pthread_join(srv_thread, &thread_ret); - ASSERT_OK(IS_ERR(thread_ret), "thread_ret"); + ASSERT_OK(send_recv_data(lfd, fd, total_bytes), "send_recv_data"); done: close(lfd); From patchwork Wed Apr 10 06:13:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623601 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8828A2206C; Wed, 10 Apr 2024 06:14:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712729646; cv=none; b=NXYNmFWyfc90TMrVgd5TwSItTYijjT1N1NP62aGmv6wsN839a4IrMvaN4ZoC8el3eTPyngiMWaXaYRA3RxCz9dYSXhkj/YYrv05I9WWtLVpXKbgCmMr9dTE7p0npKPAn1CoJ2+kPT728kj2CdCRXMRgQ7e4McIM+wJW0dVOCSS4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712729646; c=relaxed/simple; bh=bDa3Vcz6I4Pm6HBOcI4duaKWHOWwh5YyRDPmIvGlPB0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YGqciXWVDpkcAtmMvrnSmZ/yOftMuq6LplP3pT/XUV+qWwRtPPolxoIsITrh53ZwHRkBr3L9V1l00h4fxAtRHHzelt4Wf6gCco0tBOj509egAkyQtqCdi1GBnzKZwBCowvioUKjMsfKqcd6YeLWsXN6pHflCpZ6/g8PotTn4U38= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kDWUu3zu; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kDWUu3zu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ACCBBC43399; Wed, 10 Apr 2024 06:14:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712729646; bh=bDa3Vcz6I4Pm6HBOcI4duaKWHOWwh5YyRDPmIvGlPB0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kDWUu3zu/P9veaO8fBdmD4bNQ1ncUzrnVcixhzEkRH+8cgVQvI9eZgYuy5yR3nf0x d+cCq7Ov9hb5SY1/deaf4y2oJ9pemdVdhVqmCEzNbeQpZgqabhs3TnNTpIBX/BnrLd yXsqLiAVGN7opF10Ks1OxWxU6polwoIJmNcjxlLTYyiMkYUSDztfVp4TsZcz67RE1e +zzO8XHDXUn2A35FdF+1o2nswHmAgaSbt5wd3kHeFAZ4RmRN6Hdyh+YrP9Pi9pwPsN iQMkPMCpX4jLbnEldqT2IWplTJ3/NO86GWJfauMwu/lTalAZBE/AOSJ4g4fFpBTZa5 TsJK1EUo4EC9g== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 3/3] selftests/bpf: Support nonblock for send_recv_data Date: Wed, 10 Apr 2024 14:13:31 +0800 Message-Id: <9cd358958245f8ec87c4f553779aa4243f967a2f.1712729342.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Some tests, such as the MPTCP bpf tests, require send_recv_data helper to run in nonblock mode. This patch adds nonblock support for send_recv_data(). Check if it is currently in nonblock mode, and if so, ignore EWOULDBLOCK to continue sending and receiving. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 137cd18ef3f2..ca16ef2b648e 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -555,6 +555,7 @@ struct send_recv_arg { static void *send_recv_server(void *arg) { struct send_recv_arg *a = (struct send_recv_arg *)arg; + int flags = fcntl(a->fd, F_GETFL); ssize_t nr_sent = 0, bytes = 0; char batch[1500]; int err = 0, fd; @@ -578,6 +579,8 @@ static void *send_recv_server(void *arg) if (nr_sent == -1 && errno == EINTR) continue; if (nr_sent == -1) { + if (flags & O_NONBLOCK && errno == EWOULDBLOCK) + continue; err = -errno; break; } @@ -599,6 +602,7 @@ static void *send_recv_server(void *arg) int send_recv_data(int lfd, int fd, uint32_t total_bytes) { + int flags = fcntl(lfd, F_GETFL); ssize_t nr_recv = 0, bytes = 0; struct send_recv_arg arg = { .fd = lfd, @@ -622,8 +626,11 @@ int send_recv_data(int lfd, int fd, uint32_t total_bytes) MIN(total_bytes - bytes, sizeof(batch)), 0); if (nr_recv == -1 && errno == EINTR) continue; - if (nr_recv == -1) + if (nr_recv == -1) { + if (flags & O_NONBLOCK && errno == EWOULDBLOCK) + continue; break; + } bytes += nr_recv; }