From patchwork Sat Jul 6 14:45:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725852 X-Patchwork-Delegate: bpf@iogearbox.net 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 4A75629CA; Sat, 6 Jul 2024 14:46:44 +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=1720277205; cv=none; b=A3xYJus8AnDRGnB33FlSUAkvMjtvr240ZPLjw/9pq4Qe/tlm0yz1vrDdkcXiGjHQW1VS3Ufc2kSv1umYMqeOoGNWfDnJNyCPRLCL4NDtpgn0r3kJ4IwTt44vBA1BIQq14j7VFXDaLvB64HAGYJh+R1l8Mg9gmrXumVinDFxUFcs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277205; c=relaxed/simple; bh=bAX1OLxg4DkksZvjRAzn/L1GhmwINGXrUi0DgLiNpt4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jDS+Epn0j+PP7lxCZMkOdhgGz/4FkpJC9DeQvSHrymfcBbL5gIJLA5op3ED9PHaHZFYBbFMYN0c+U6LhFNxaGJRm8aqFG3a2HKnaSSmIdWC53cOQKbl2Of+Ze9+cEJBx8xVn3uwnxFxSdR6XZm7qRlSD92NIXPeiob3fBwDSDCk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oheQobmp; 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="oheQobmp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF558C4AF0F; Sat, 6 Jul 2024 14:46:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277204; bh=bAX1OLxg4DkksZvjRAzn/L1GhmwINGXrUi0DgLiNpt4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oheQobmpoP3PN4qc7sokXE0JBVmuRGJXwbs82JJ5YbqarGWU0OBlqdj44xYwOnK/p 6aFf4oK7iY+k36fRxhvhAgWWdJ2gnPo7S5ZAizeP6pGIusA5D/1o8tExv43Z0xw40i lKekhXXSN9/H5nsiRow/veu3K4Q3RhjhicbDEOxZIvwiaRa3UgdbQvRiIX1YBnFyxi DYoJK37WO6IXBuGusOt+O//QVeCDkhQo0Rowqj8qiuwLx+cLngUTbD4vsS/sCZa4wn rB3IlyYGOcbKdGM+7huaAt/E+tc2rBp2mewgOuNMqwkRD6drcNBeGKuaAWFFqrqda1 o+kroLrQTZCsw== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 01/11] selftests/bpf: Add backlog for network_helper_opts Date: Sat, 6 Jul 2024 22:45:46 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Some callers expect __start_server() helper to pass their own "backlog" value to listen() instead of the default of 1. So this patch adds struct member "backlog" for network_helper_opts to allow callers to set "backlog" value via start_server_str() helper. listen(fd, 0 /* backlog */) can be used to enforce syncookie. Meaning backlog 0 is a legit value. Using 0 as a default and changing it to 1 here is fine. It makes the test program easier to write for the common case. Enforcing syncookie mode by using backlog 0 is a niche use case but it should at least have a way for the caller to do that. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/network_helpers.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 44c2c8fa542a..e0cba4178e41 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -106,7 +106,7 @@ static int __start_server(int type, const struct sockaddr *addr, socklen_t addrl } if (type == SOCK_STREAM) { - if (listen(fd, 1) < 0) { + if (listen(fd, opts->backlog ? MAX(opts->backlog, 0) : 1) < 0) { log_err("Failed to listed on socket"); goto error_close; } diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 9ea36524b9db..4f26bfc2dbf5 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -25,6 +25,10 @@ struct network_helper_opts { int timeout_ms; bool must_fail; int proto; + /* The backlog argument for listen(), defines the maximum length to which + * the queue of pending connections for sockfd may grow. + */ + int backlog; int (*post_socket_cb)(int fd, void *opts); void *cb_opts; }; From patchwork Sat Jul 6 14:45:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725853 X-Patchwork-Delegate: bpf@iogearbox.net 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 932271292CE; Sat, 6 Jul 2024 14:46:52 +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=1720277212; cv=none; b=pbIW+x5PVdoHxqBpAzkHtDaGQRJkN113XelqWRonQqijYVm2jW2axoP5faBAmBRjH48nrcjevrbY1HTkICluuqjKL7WF0loplTQUDB3lcreqlfjzbyyH3AdnMxB++bDfHWqRZeiANxzwVMYhAai1kLhaX8aRqCYFQfuzFFBxMGI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277212; c=relaxed/simple; bh=00pdNC+8+bJCChvMSP7B2ks/Ry/OoNnOXIemGC5RruM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qQ/2hT0ink2KdSXZu+JR6xm4b2+ee7Ax6Uyt5Ekkf/0INW/AfzOAU84w0L6pqNvN6hjwcjKpcwcw/MSwmkn0bDuyUZJl6/WK6B1LsJ88Un99qOSAN479X79Xijzez7L7BgcEWyPKJVxBJ5P8CGRhVHqW+0EPjEIL24f1FrjqzUk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FQ0/RSN7; 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="FQ0/RSN7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BC5EC4AF0C; Sat, 6 Jul 2024 14:46:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277212; bh=00pdNC+8+bJCChvMSP7B2ks/Ry/OoNnOXIemGC5RruM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FQ0/RSN7B2OTca12hCrZpJQoTZ9bcr325dejXNrqghveVWKb3pJbO0XcSGZRvLOg0 uYjZJHQ3ECGV9lMyaUSHcDd87p9yxvACAp5HVY0eMdVEgkE3vRL8kd50q5OGKlEpVp V7zDGHLq834EJNL1UTsLfWeW5s9O1yePNUemZxdNPAZ3GFRMqpSJj55zz3PQ7oFIq/ BgQ4ZJk2TExwqnAdQwrr80wZRo+ZUpxrS+JYf+IPYZBgMDiTPlmhWoCD0d4D/3yVd5 HbwGX2mvpeXsuhqCcSHqU9goaODtpaKKt38Fu3PZyGzflzc1bJ3cPoVN4T7rTXkBkT YqKAld8KCUt3g== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 02/11] selftests/bpf: Use start_server_str in sockmap_ktls Date: Sat, 6 Jul 2024 22:45:47 +0800 Message-ID: <65e49479affc051c12a45b37be219af293095fe7.1720276189.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Include network_helpers.h in prog_tests/sockmap_ktls.c, use public network helper start_server_str() instead of local defined function tcp_server(). This can avoid duplicate code. Technically, this is not a one-for-one replacement, as start_server_str() also does bind(). But the difference does not seem to matter. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 2d0796314862..32be112967a5 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -6,25 +6,11 @@ #include #include "test_progs.h" +#include "network_helpers.h" #define MAX_TEST_NAME 80 #define TCP_ULP 31 -static int tcp_server(int family) -{ - int err, s; - - s = socket(family, SOCK_STREAM, 0); - if (!ASSERT_GE(s, 0, "socket")) - return -1; - - err = listen(s, SOMAXCONN); - if (!ASSERT_OK(err, "listen")) - return -1; - - return s; -} - static int disconnect(int fd) { struct sockaddr unspec = { AF_UNSPEC }; @@ -35,12 +21,15 @@ static int disconnect(int fd) /* Disconnect (unhash) a kTLS socket after removing it from sockmap. */ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) { + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + }; struct sockaddr_storage addr = {0}; socklen_t len = sizeof(addr); int err, cli, srv, zero = 0; - srv = tcp_server(family); - if (srv == -1) + srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); + if (!ASSERT_GE(srv, 0, "start_server_str")) return; err = getsockname(srv, (struct sockaddr *)&addr, &len); From patchwork Sat Jul 6 14:45:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725854 X-Patchwork-Delegate: bpf@iogearbox.net 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 EC649811E0; Sat, 6 Jul 2024 14:46:59 +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=1720277220; cv=none; b=VxHWEXd0pa1oFhAPCDiHjMCScMMMlItFsPcLBK8tBkIbzm5QJPqrCQjxqeNJ24lMdms7OXm4DF+64JJQumN53/xl96480JLbmO0EZb8XrluECMabCTpR6vtVvz9VSltAg8SdyPqEVDqCb886A+2oyRZRD43zIa9oqCHJ8WxRZVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277220; c=relaxed/simple; bh=Qq9hn27IvadosGSaFEY1IhNuXDt86aHiR/dUXu2AMTk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Wlg8A8TdH6/Jwr71DMtPdDKifRjeVL0RMUEGn+EEdOMGpHQYNldFdzOS6N+Yp4EtchK5PBPBTgw/EXIKgBmQXiJguF8z/zIGcl07CD68YSJ9WJnaNCzpzKMksTCe3OxhevPmVluU/6ZDP0MmelOS7gImogJloMnGNogHZmh40eM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=s8or9wjW; 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="s8or9wjW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22112C3277B; Sat, 6 Jul 2024 14:46:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277219; bh=Qq9hn27IvadosGSaFEY1IhNuXDt86aHiR/dUXu2AMTk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s8or9wjWAzS9UaS9BoQiTLQ0gJQ0lZ3FjvK2f4+tW03DDOyboHopvj3prnkbnRbvl jwPAtXBUNboTOJ2hCA0Mw1tYz45/pUP96EJWsBTB0bSCLSspK8dwPzO2X3/quEu4vA SdvlirgjMqe1MImySA+DmVJc6P7I5STtI+FnIESGx/s9AAKBckyY8nOlw1MrD7mo+y N0PzPXM5HOWhTGzcK9KUyPh49dU/rAHkJI9rcc4ALrlc12OG3lMszWuej6MtpoBmfG IJ8Jeewy6JLaaFV3Hr5PJpWtQAGkugB5H50w5QJHpg9IxqWkDR4TuBQ5Xp66+YX7M7 KbXamUIWmKaMw== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 03/11] selftests/bpf: Use connect_to_fd_opts in sockmap_ktls Date: Sat, 6 Jul 2024 22:45:48 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Use public network helper connect_to_fd_opts() instead of open-coding it in prog_tests/sockmap_ktls.c. This can avoid duplicate code. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 32be112967a5..a794aa688ab5 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -24,26 +24,16 @@ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) struct network_helper_opts opts = { .backlog = SOMAXCONN, }; - struct sockaddr_storage addr = {0}; - socklen_t len = sizeof(addr); int err, cli, srv, zero = 0; srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); if (!ASSERT_GE(srv, 0, "start_server_str")) return; - err = getsockname(srv, (struct sockaddr *)&addr, &len); - if (!ASSERT_OK(err, "getsockopt")) - goto close_srv; - - cli = socket(family, SOCK_STREAM, 0); - if (!ASSERT_GE(cli, 0, "socket")) + cli = connect_to_fd_opts(srv, SOCK_STREAM, NULL); + if (!ASSERT_GE(cli, 0, "connect_to_fd_opts")) goto close_srv; - err = connect(cli, (struct sockaddr *)&addr, len); - if (!ASSERT_OK(err, "connect")) - goto close_cli; - err = bpf_map_update_elem(map, &zero, &cli, 0); if (!ASSERT_OK(err, "bpf_map_update_elem")) goto close_cli; From patchwork Sat Jul 6 14:45:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725855 X-Patchwork-Delegate: bpf@iogearbox.net 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 78C5A29CA; Sat, 6 Jul 2024 14:47: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=1720277226; cv=none; b=CLAsPsW8JidvFRk/k7s6V4fWPfjb76IiptT4lA7ElVlBVt76TYGJcYjP0jZeSXZQdFRYTyfeAm4yjZxUgYSFxyyAbhId3sdyZnYJwQZJUtiW+SqRR8ZujqRo9bMg2Qf/sWLz5ftwZ9fnsxmaQCTZqTIVcYqpLKRiEqJB7tUfx34= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277226; c=relaxed/simple; bh=rRP0ZiPRY0W17MVOWxZwWV5aU8LBd+K/G2ydWK7Wq+o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=t/ikW+3XaxB4esG3GvoFLmMmAFhfKXZ0c62xTYMNUw48oqkR0967w5rma0zL1DDPbQraQCPDFbiYB8jFDBOkkeOft6uC6xLsXJh1fOEqDtD2AUlPSY+RF3dF8nWv7u3ZgPrAhgYf/+UvwhwQtSBxHTaOzp0ZmtVvCh1Il7862Xo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZsRDpVoK; 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="ZsRDpVoK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13F14C2BD10; Sat, 6 Jul 2024 14:46:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277226; bh=rRP0ZiPRY0W17MVOWxZwWV5aU8LBd+K/G2ydWK7Wq+o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZsRDpVoKRgDfUdrTaFNNlxrZBGRJn9d850X26fGxYJwmY99YFVzLJ9tjNgMP9Vstw TF4V2Hz4miROXglieVcPaxcAgoniDNiE/ziKX0T8TgDCqaT3C0xPFA8dexNOU/DTc5 686U8yCFVoANf5v81aBBmveVRp0vq24vI6q+8+7Cpn1ZfPAo6NjsShFN/sMhjcHEp6 YW4IfkQOtaWFAFhFiIuWbViQan6DUS0JQXFm1renRHu5QwkI+lqdaneEfNt56euvTG ut8omie6FfxuRdgr3TkM9SIxZoBNJ+9rwtMSZyTiN31ZZZVJcqeacC3rcsJvn3eG6Y zjGI7nE3xbpSw== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 04/11] selftests/bpf: Use make_sockaddr in sockmap_ktls Date: Sat, 6 Jul 2024 22:45:49 +0800 Message-ID: <7125742ccf22a673a9ea66fa11eed0be24301516.1720276189.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper make_sockaddr() exported in network_helpers.h instead of open-coding in sockmap_ktls.c. This can avoid duplicate code. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index a794aa688ab5..3c223601e8b6 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -59,23 +59,11 @@ static void test_sockmap_ktls_update_fails_when_sock_has_ulp(int family, int map { struct sockaddr_storage addr = {}; socklen_t len = sizeof(addr); - struct sockaddr_in6 *v6; - struct sockaddr_in *v4; int err, s, zero = 0; - switch (family) { - case AF_INET: - v4 = (struct sockaddr_in *)&addr; - v4->sin_family = AF_INET; - break; - case AF_INET6: - v6 = (struct sockaddr_in6 *)&addr; - v6->sin6_family = AF_INET6; - break; - default: - PRINT_FAIL("unsupported socket family %d", family); + err = make_sockaddr(family, NULL, 0, &addr, &len); + if (!ASSERT_OK(err, "make_sockaddr")) return; - } s = socket(family, SOCK_STREAM, 0); if (!ASSERT_GE(s, 0, "socket")) From patchwork Sat Jul 6 14:45:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725856 X-Patchwork-Delegate: bpf@iogearbox.net 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 6994413A88D; Sat, 6 Jul 2024 14:47:13 +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=1720277233; cv=none; b=NgeOj5HlrTUP8NtPS1VME1MsXznSO0AKk9nFD+5bwK6bpr1ZTj0ajWN1K5PICffAEFqk+dL3FpWrSIW5IPdB8MrApQ0BBxT8ZorqUNase+jF9kAEhKiCvce14/CsHjAMQZTU4G65+h6TBaHahqAuybeBLlt7FFOXF+LFqEYm5ZU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277233; c=relaxed/simple; bh=HmXOdU8AvXotdcvvc/YDpHs6DrXYIXQa6FQSAA6yhPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q/EPA9U15fCGV0xwNhnnlxk0tAgkcbU+nS15chUYvWb+Q4351RcKv/rEBUZGHBfH+SJ89Ajhq8bi89teGHyOAikn0+bjwT+TILLJlXm+HayxA4Iz1qyA7DZrX3mQnK6AYxJoVeBOBtbQ2I+q3D62wzd+MUwJmEEDcCXQj2wOSXo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NJrG/kMv; 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="NJrG/kMv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9BCEEC3277B; Sat, 6 Jul 2024 14:47:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277232; bh=HmXOdU8AvXotdcvvc/YDpHs6DrXYIXQa6FQSAA6yhPQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NJrG/kMvLKdxbp+KCk1dcjowGWZWVIpg+JYTiCx0/r1zSKmZ9vId/KsCS4DiO1UJu z2qWqYutpdjcZS7P23R1QPLvPfEeOuo/+S2MOatbPG3/LXvA61u1Jlc2ExG3ZPdLDn bCYEWZEpX24d7eQjq8/kYFTk9n6Zeutk8olJT35onVZlrZqyViHkKX+R/pFGqwZI3u mFysBNsMl30+2v36ueGQ7s+JXQflTVuOYzU8vWb8/M0e19hHSNkehchGhUUIkbvM1g osgxZvN4LfC/lu5Fmp9GV9jMmf+w8IyVkLpRhvT8CHj2ahbcV0Q4ujP2MMjZ9wlnS0 2r543GU3oDrKw== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 05/11] selftests/bpf: Add network_helper_opts for connect_fd_to_fd Date: Sat, 6 Jul 2024 22:45:50 +0800 Message-ID: <7b0985069599970cb45187a1590670372ed1579b.1720276189.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang To make connect_fd_to_fd() more flexible, use a struct network_helper_opts parameter "opts" instead of timeout_ms. With this change, other opts like "must_fail" can be used in connect_fd_to_fd() too. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 10 +++++++--- tools/testing/selftests/bpf/network_helpers.h | 3 ++- tools/testing/selftests/bpf/prog_tests/bpf_nf.c | 5 ++++- .../selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c | 5 ++++- .../testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c | 4 ++-- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index e0cba4178e41..475a5a04e61e 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -369,12 +369,16 @@ int connect_to_fd(int server_fd, int timeout_ms) return connect_to_fd_opts(server_fd, type, &opts); } -int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) +int connect_fd_to_fd(int client_fd, int server_fd, + const struct network_helper_opts *opts) { struct sockaddr_storage addr; socklen_t len = sizeof(addr); - if (settimeo(client_fd, timeout_ms)) + if (!opts) + opts = &default_opts; + + if (settimeo(client_fd, opts->timeout_ms)) return -1; if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) { @@ -382,7 +386,7 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) return -1; } - if (connect_fd_to_addr(client_fd, &addr, len, false)) + if (connect_fd_to_addr(client_fd, &addr, len, opts->must_fail)) return -1; return 0; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 4f26bfc2dbf5..fcda6b2333ad 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -66,7 +66,8 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len const struct network_helper_opts *opts); int connect_to_fd(int server_fd, int timeout_ms); int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts); -int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); +int connect_fd_to_fd(int client_fd, int server_fd, + const struct network_helper_opts *opts); int fastopen_connect(int server_fd, const char *data, unsigned int data_len, int timeout_ms); int make_sockaddr(int family, const char *addr_str, __u16 port, diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_nf.c b/tools/testing/selftests/bpf/prog_tests/bpf_nf.c index a4a1f93878d4..cd4dce5bd7f1 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_nf.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_nf.c @@ -33,13 +33,16 @@ enum { static int connect_to_server(int srv_fd) { + struct network_helper_opts opts = { + .timeout_ms = TIMEOUT_MS, + }; int fd = -1; fd = socket(AF_INET, SOCK_STREAM, 0); if (!ASSERT_GE(fd, 0, "socket")) goto out; - if (!ASSERT_EQ(connect_fd_to_fd(fd, srv_fd, TIMEOUT_MS), 0, "connect_fd_to_fd")) { + if (!ASSERT_EQ(connect_fd_to_fd(fd, srv_fd, &opts), 0, "connect_fd_to_fd")) { close(fd); fd = -1; } diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c index b9dc4ec655b5..bcf142779cc6 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c @@ -8,6 +8,9 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk) { + struct network_helper_opts opts = { + .timeout_ms = 1000, + }; int serv_sk = -1, in_sk = -1, serv_in_sk = -1, err; struct sockaddr_in6 addr = {}; socklen_t addr_len = sizeof(addr); @@ -24,7 +27,7 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk) *g_serv_port = addr.sin6_port; /* Client outside of test cgroup should fail to connect by timeout. */ - err = connect_fd_to_fd(out_sk, serv_sk, 1000); + err = connect_fd_to_fd(out_sk, serv_sk, &opts); if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd", "unexpected result err %d errno %d\n", err, errno)) goto cleanup; diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c b/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c index a1542faf7873..f35a9897db24 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c @@ -86,7 +86,7 @@ static int talk_to_cgroup(int *client_fd, int *listen_fd, int *service_fd, skel->bss->g_sock_port = ntohs(port); /* Connect client to server */ - err = connect_fd_to_fd(*client_fd, *listen_fd, 0); + err = connect_fd_to_fd(*client_fd, *listen_fd, NULL); if (!ASSERT_OK(err, "connect_fd_to_fd")) return -1; *service_fd = accept(*listen_fd, NULL, NULL); @@ -136,7 +136,7 @@ static int talk_to_outside(int *client_fd, int *listen_fd, int *service_fd, skel->bss->g_sock_port = ntohs(port); /* Connect client to server */ - err = connect_fd_to_fd(*client_fd, *listen_fd, 0); + err = connect_fd_to_fd(*client_fd, *listen_fd, NULL); if (!ASSERT_OK(err, "connect_fd_to_fd")) return -1; *service_fd = accept(*listen_fd, NULL, NULL); From patchwork Sat Jul 6 14:45:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725857 X-Patchwork-Delegate: bpf@iogearbox.net 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 487CE811E0; Sat, 6 Jul 2024 14:47:21 +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=1720277241; cv=none; b=QVZmekMfkD1TEZ2zcVXTDFBj1+Owpway2QkNseZqu2FrMeFH6C6uxbMn8bFh6cBKPmx/xjiSzkBHDk+ARImprwxISEQoISIe102AU3FMcde7oI/xy0+SUtGwtqpowLi8LMGg3Ev2ch0hiabxn+I+WPdyHO4w8XaX3trlfyyyo00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277241; c=relaxed/simple; bh=Dpr4rRsmbyid4BnC85GpsbxEUjgT4+s7MlZSkdOun7o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cY55u+aPgeAOEGy6esXnqwYqBfLI1KEkifHW0XTGjEvxbThRcTODL10BC7ZwAE5W1HFCtVQXUyqxJfsq4S4LvGCmmK+zgmuIj+7U2+jdrNf1+Q8o5s8oBl/ZO7Wl5CVPv0xdHiCeDf22FC1gLnslvovH78/r8L25YrDjW+5S0F0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FTtRr0oJ; 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="FTtRr0oJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5B9FC2BD10; Sat, 6 Jul 2024 14:47:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277241; bh=Dpr4rRsmbyid4BnC85GpsbxEUjgT4+s7MlZSkdOun7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FTtRr0oJ0e/FnYX5P6T6obOUjdz6cRucw3YEE0W5676c9l493BGEfD5lcI+vRCX1w jdYgPan9QUnelZr3quraw9GV7mpHhoGbeU+8MOyzGzRlXq5JTMjqoRKsU1D5NZRZyA M7SDNlMPPqHjHa9fQcQQs/DAEK40fQDWFmvh3AbC94pChGLAFxJzJrmccLK/nhw517 Y0oKTefYOmj/jV/tiTRijQxoB/FYQt9vdLt3T44szOeeI8Br3nBBE+6TCBG0DhQBzt kA6cht9l86isspHI4X48hBhvjhOpRbx6zzZeDz9V+uy6/21IWyl8ueKOzKGwp1nX0m e7f/7CnZi1RRg== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 06/11] selftests/bpf: Add expect_errno for network_helper_opts Date: Sat, 6 Jul 2024 22:45:51 +0800 Message-ID: <263fb97117e3bb62df8bef34c35b6501d1b10e9a.1720276189.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang The errno EPERM is skipped in connect_fd_to_addr() by cgroup_v1v2 tests. More generally, it makes sense to add a "expect_errno" struct member for network_helper_opts to identify the expect errno to be skipped. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 11 +++++++---- tools/testing/selftests/bpf/network_helpers.h | 1 + tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 475a5a04e61e..062170d6be1c 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -279,7 +279,8 @@ int client_socket(int family, int type, static int connect_fd_to_addr(int fd, const struct sockaddr_storage *addr, - socklen_t addrlen, const bool must_fail) + socklen_t addrlen, const bool must_fail, + const int expect_errno) { int ret; @@ -290,7 +291,7 @@ static int connect_fd_to_addr(int fd, log_err("Unexpected success to connect to server"); return -1; } - if (errno != EPERM) { + if (errno != expect_errno) { log_err("Unexpected error from connect to server"); return -1; } @@ -318,7 +319,8 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add return -1; } - if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail)) + if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail, + opts->expect_errno)) goto error_close; return fd; @@ -386,7 +388,8 @@ int connect_fd_to_fd(int client_fd, int server_fd, return -1; } - if (connect_fd_to_addr(client_fd, &addr, len, opts->must_fail)) + if (connect_fd_to_addr(client_fd, &addr, len, opts->must_fail, + opts->expect_errno)) return -1; return 0; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index fcda6b2333ad..14d161d35248 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -24,6 +24,7 @@ typedef __u16 __sum16; struct network_helper_opts { int timeout_ms; bool must_fail; + int expect_errno; int proto; /* The backlog argument for listen(), defines the maximum length to which * the queue of pending connections for sockfd may grow. diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c index 9709c8db7275..ff477163f0ea 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c @@ -11,6 +11,7 @@ static int run_test(int cgroup_fd, int server_fd, bool classid) { struct network_helper_opts opts = { .must_fail = true, + .expect_errno = EPERM, }; struct connect4_dropper *skel; int fd, err = 0; From patchwork Sat Jul 6 14:45:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725858 X-Patchwork-Delegate: bpf@iogearbox.net 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 F1A33811E0; Sat, 6 Jul 2024 14:47:28 +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=1720277249; cv=none; b=tWuMnyVMdHamax4k0kd4RiiXWKuI/HH0B5jttA7T2Zn2szodCoXsFPo/0OhjwIbdrZLczWyd9sqQOJHqCKWpdKxcWcakrDpM+ZajjbWegoomgLwTt0O1ntbHovIr1r1Ll2Z8u1ORd1fTulLMvjTSwyRSOrOky1qarC573U1ZT/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277249; c=relaxed/simple; bh=p/sQSZ6Ry7HCVEED0GCWFAhT5N2x1Y0pWGbUjY6YoeY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mmNhEkIWEdctOx3GSS1gXEbFdVTY5hF06I/Vpi1FzcqyOc+UuutRWrNWXgpdjA7Ah0dtZhhGtFzuxf/ydlfAtEkTSjzihHaGpRS8JReMxhYd5riM1i1OHDWFtYV10Y6LcccfzyWRzkMghgOJv6ie5g6qutGux1gz8rhbbo22qFs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BmoQHncS; 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="BmoQHncS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E193BC4AF0E; Sat, 6 Jul 2024 14:47:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277248; bh=p/sQSZ6Ry7HCVEED0GCWFAhT5N2x1Y0pWGbUjY6YoeY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BmoQHncS3G6eOOs6kMq9Qt3P9feoahzE27ebgcvyAEjzWctestQm1O8XdNbi5KW5M rBZxTn79+GluBXZZ//W1vcea7P8MPYJVfuWlgGRorZDbXFl0rlH0RMQvvInH7dd+SZ ZbljCkd0urBeUdfO7uZ+wSjM9D0CucMpGbY6hidsd3P6F/P3imN623lZK6JSrdzClL eTri2gAQJbagLhh/nTedkxIdhrlmF1QTx1drDzhhBusZ6gIEWDbyLcJptyWfNFdK+Y GVmBOTqJnTtKwaxys3r3TmiHnamDqMY4Jy/Q1Zuu2mOhJTvC/MOOoByn9HnkBVxvme FdY4zC2VSabmQ== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 07/11] selftests/bpf: Set expect_errno for cgroup_skb_sk_lookup Date: Sat, 6 Jul 2024 22:45:52 +0800 Message-ID: <739486ce0d05a999c317ad6b21d3ef02cdca2a5f.1720276189.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang EINPROGRESS is skipped in run_lookup_test() in cgroup_skb_sk_lookup tests, but it is still showed in the log: ./test_progs -t cgroup_skb_sk_lookup -v run_cgroup_bpf_test:PASS:skel_open_load 0 nsec run_cgroup_bpf_test:PASS:cgroup_join 0 nsec run_cgroup_bpf_test:PASS:cgroup_attach 0 nsec run_lookup_test:PASS:start_server 0 nsec run_lookup_test:PASS:getsockname 0 nsec (network_helpers.c:300: errno: Operation now in progress) Failed to \ connect to server run_lookup_test:PASS:connect_fd_to_fd 0 nsec run_lookup_test:PASS:connect_to_fd 0 nsec run_lookup_test:PASS:accept 0 nsec #51 cgroup_skb_sk_lookup:OK To fix this, set EINPROGRESS as the expect_errno of network_helper_opts and pass it to connect_fd_to_fd(). Skip this expect_errno when must_fail is false too in connect_fd_to_addr(). Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 062170d6be1c..1403487c5921 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -296,7 +296,7 @@ static int connect_fd_to_addr(int fd, return -1; } } else { - if (ret) { + if (ret && errno != expect_errno) { log_err("Failed to connect to server"); return -1; } diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c index bcf142779cc6..0ef2e9a6f47c 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c @@ -10,6 +10,7 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk) { struct network_helper_opts opts = { .timeout_ms = 1000, + .expect_errno = EINPROGRESS, }; int serv_sk = -1, in_sk = -1, serv_in_sk = -1, err; struct sockaddr_in6 addr = {}; @@ -28,7 +29,7 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk) /* Client outside of test cgroup should fail to connect by timeout. */ err = connect_fd_to_fd(out_sk, serv_sk, &opts); - if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd", + if (CHECK(err || errno != EINPROGRESS, "connect_fd_to_fd", "unexpected result err %d errno %d\n", err, errno)) goto cleanup; From patchwork Sat Jul 6 14:45:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725859 X-Patchwork-Delegate: bpf@iogearbox.net 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 82C4729CA; Sat, 6 Jul 2024 14:47:35 +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=1720277255; cv=none; b=RxCf9BAX1XwjlOOvjv643VmLpg3UZ3VmkLAVluCHrBfMAqVwRPAZagXXu0N/C0L97idLMdvg/AW7hZuXF0F+q/Dq8PT1MjRlympoCir2/e4nku3N7cBVQBR2no+TCf93jUDiRNTT6O/A8SYvtHuFOdyZo7G+uP53P6PmefgT6pU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277255; c=relaxed/simple; bh=JAWAcKR7zBXRC50szWd1XFpZsq7hHNsF3ZdFSJ3411U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Iq57szo1vC3ta2J6NhEt/Xf+6zjZ2sPoqDQ4NrMhzfL6wUzCBNYcsOPWGNgy3MnYX8Nqb5ESjqO71wKoOMlYL6wNUjqyBrn3dxMT0biSYooaVZ7OkMBF8ZV3yK/zWA5althWpeQDqgS/bQiDQyUwPImXO6OLieIJj2P5nSawRJY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=s6WLIpLE; 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="s6WLIpLE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A9C8C3277B; Sat, 6 Jul 2024 14:47:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277255; bh=JAWAcKR7zBXRC50szWd1XFpZsq7hHNsF3ZdFSJ3411U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s6WLIpLEf1my4HtXFesxjp42Am/DT/2k1aB46ArhgtyX5SwPIFbUrHJw9rFUVkaGf G2NJJZbZf1LcahfO53A2bJ62qk9/Jnp1kenBHiRil8qV7UW4G9agj3pejgLp/318FR bDU2cygtr9UyWgk9Yfrw74p8uy9tEr9I6oc0lgusnAsZR0lkdOkiMolty41alkHbWQ d17inRbZn4Bjy+tZi+1W1JAzrPB9YnxOEbEkWGtVT3gdhM1qAZYy9pGt7p81m6TEzk HEEDe5LEiR9SP/VSv+bKQtZgtsOU07xU0vcHWZOzoGISJuPehsoqyEtrnUjDR145bb J4/7U2zZSJFfQ== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 08/11] selftests/bpf: Close fd in error path in drop_on_reuseport Date: Sat, 6 Jul 2024 22:45:53 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Server 1 fd should be closed in the error path when update_lookup_map() fails. This patch fixes it by goto "close_srv1" instead of "detach" lable in that case. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 597d0467a926..de2466547efe 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -994,7 +994,7 @@ static void drop_on_reuseport(const struct test *t) err = update_lookup_map(t->sock_map, SERVER_A, server1); if (err) - goto detach; + goto close_srv1; /* second server on destination address we should never reach */ server2 = make_server(t->sotype, t->connect_to.ip, t->connect_to.port, From patchwork Sat Jul 6 14:45:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725860 X-Patchwork-Delegate: bpf@iogearbox.net 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 CEC6329CA; Sat, 6 Jul 2024 14:47:43 +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=1720277263; cv=none; b=qJMu75BomEcmGgi5iEgsDn9r5fAo5UT47vEq8lFAJcOnlIv06QnH2W6ZakhX6ngtl52PJjnkd/VxmclYdKnYBNSLxtp65c6atFuSJcfIovcVGNa+bAip63yK4CtzYoNiF6BBKnVzutzIbF98Q8JJfZ2goNef6BfAzhdmwXj0IW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277263; c=relaxed/simple; bh=3p/deRrkgPGTdGxSapLPunN1pNLrCJ3NVhjsTWR8QBc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CntbU6i+GBpmjdr0sUpmoxlsRM9n6Xo9TeAu23fladjKyR/tgo1vcCWQC2dv3xfY3S/D6ZmwkBMiVp02t5g5I17p10q9vFG6s3VdxKxdI4Wt0Q0cpeysFy7eUAEjU/awx73/dzcEa7h/OWtw+AvNSwy/9XRHvNjapm4ATdEDCmE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dO5R7EEU; 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="dO5R7EEU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F7D7C4AF0A; Sat, 6 Jul 2024 14:47:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277263; bh=3p/deRrkgPGTdGxSapLPunN1pNLrCJ3NVhjsTWR8QBc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dO5R7EEUbHGdt1K1i7Uv9unZRLFdgCFxaEMGpf24p0+OBBkB4bT3CCBbS7IcmjcF6 i+5yCxIvVfRuuAwiyeaVBOTmu6lIbEZ6Yo7NWkVX/8K43uBqH0v0L/G3Uth6nXe5JO OEqCCCxoAKqXCNNebyCYN1XNHQ8ug5QHWrkUe+efnvhubYOBJIaPTxGU06WsJgG06O BDynMH3Cv5rTzLXIaCArPUd3p/tSjoXbHFpgHdJ33cPuZMcrtwX1qvDdwJ1PGFulnp 1lZvntIuX4uvoGfSfck8SjdXMMnaN2us0YRxRdowgZw8M2vhLTdgQqcgCLWIyV8OR3 PgcEezNCpiWJw== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 09/11] selftests/bpf: Use start_server_str in sk_lookup Date: Sat, 6 Jul 2024 22:45:54 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper start_server_addr() instead of local defined function make_server() in prog_tests/sk_lookup.c to avoid duplicate code. Add a helper setsockopts() to set SOL_CUSTOM sockopt looply, set it to setsockopt pointer of struct network_helper_opts, and pass it to start_server_addr(). Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index de2466547efe..d03ea3e64a2e 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -77,6 +77,12 @@ struct test { bool reuseport_has_conns; /* Add a connected socket to reuseport group */ }; +struct cb_opts { + int family; + int sotype; + bool reuseport; +}; + static __u32 duration; /* for CHECK macro */ static bool is_ipv6(const char *ip) @@ -142,19 +148,14 @@ static int make_socket(int sotype, const char *ip, int port, return fd; } -static int make_server(int sotype, const char *ip, int port, - struct bpf_program *reuseport_prog) +static int setsockopts(int fd, void *opts) { - struct sockaddr_storage addr = {0}; + struct cb_opts *co = (struct cb_opts *)opts; const int one = 1; - int err, fd = -1; - - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) - return -1; + int err = 0; /* Enabled for UDPv6 sockets for IPv4-mapped IPv6 to work. */ - if (sotype == SOCK_DGRAM) { + if (co->sotype == SOCK_DGRAM) { err = setsockopt(fd, SOL_IP, IP_RECVORIGDSTADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(IP_RECVORIGDSTADDR)", "failed\n")) { @@ -163,7 +164,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (sotype == SOCK_DGRAM && addr.ss_family == AF_INET6) { + if (co->sotype == SOCK_DGRAM && co->family == AF_INET6) { err = setsockopt(fd, SOL_IPV6, IPV6_RECVORIGDSTADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(IPV6_RECVORIGDSTADDR)", "failed\n")) { @@ -172,7 +173,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (sotype == SOCK_STREAM) { + if (co->sotype == SOCK_STREAM) { err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(SO_REUSEADDR)", "failed\n")) { @@ -181,7 +182,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (reuseport_prog) { + if (co->reuseport) { err = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); if (CHECK(err, "setsockopt(SO_REUSEPORT)", "failed\n")) { @@ -190,19 +191,28 @@ static int make_server(int sotype, const char *ip, int port, } } - err = bind(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "bind", "failed\n")) { - log_err("failed to bind listen socket"); - goto fail; - } +fail: + return err; +} - if (sotype == SOCK_STREAM) { - err = listen(fd, SOMAXCONN); - if (CHECK(err, "make_server", "listen")) { - log_err("failed to listen on port %d", port); - goto fail; - } - } +static int make_server(int sotype, const char *ip, int port, + struct bpf_program *reuseport_prog) +{ + struct cb_opts cb_opts = { + .family = is_ipv6(ip) ? AF_INET6 : AF_INET, + .sotype = sotype, + .reuseport = reuseport_prog, + }; + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; + int err, fd; + + fd = start_server_str(cb_opts.family, sotype, ip, port, &opts); + if (!ASSERT_GE(fd, 0, "start_server_str")) + return -1; /* Late attach reuseport prog so we can have one init path */ if (reuseport_prog) { From patchwork Sat Jul 6 14:45:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725861 X-Patchwork-Delegate: bpf@iogearbox.net 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 BAEEE13A41D; Sat, 6 Jul 2024 14:47:50 +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=1720277270; cv=none; b=WDB2S5w6O1sS0u2tO1lYR7gAO6SoFeFaLZd8Ql2EykxzsQH1C0f46jkx4M6hmkD/0aJXMDfiTkLGardpuW0uJ2d9K/+/tLTIXXFBjaWBi+V07ZEVYkYS1nifmOgFLz39SkZVya7t9+D3pOGeesAL7xbaZK/vFlnqvgR9LWX7Vt4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277270; c=relaxed/simple; bh=UP3dewuIjoA5sjTgTdYbHvbUObaH3wdxvkdyENKJ+bI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OQlOkO8tovC3uQKVX8+BGOqWEz3L7Gg1NZ27sj4MLDBfPl6UoCo5jvNiviGwVUVHy/8Miek+5TiUics7bIWGxC7u/UpjjdFi+Q6NV/oGHWr6bUQSOrEhRB4gjvCV9CX4CfXVgMv2/T77uOqjI0J9ghtXZ2Q6Ov6T9VvDYq48j60= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tUc1WSHk; 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="tUc1WSHk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03DDAC2BD10; Sat, 6 Jul 2024 14:47:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277270; bh=UP3dewuIjoA5sjTgTdYbHvbUObaH3wdxvkdyENKJ+bI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tUc1WSHkTKX31/kYvIDMnDT2+uYXhmPba3j+LXv2ZKPIJAt8ryjN1GPZDb5o9g/6P kyEhz2PNLRojTBc3NW9NR5r33kQ6sYvRwlE3ILoDiHO+egawOtMvjwX5NqkV0NTHdg Zw+bUJ9A9ocUtBOIgYi0Gz299qXP17EzWs3M9wY+GCIrByvsJMW+d46aZubrNy4r8E t2S7gf0p6eibLdbrjrwLCo0kYKZWcP/5yCWNVQBBKa+k6m2XAgxAiMiSCpP0A1S3Lk QSWbuWGzDw6XR1uWlmaxghpxPC5zwkvYIQThnR26abphOuiSlTn0g/lXbC8blVxDqc NIq/lM1/v937g== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 10/11] selftests/bpf: Use connect_to_addr in sk_lookup Date: Sat, 6 Jul 2024 22:45:55 +0800 Message-ID: <96086c85a032f8846c1bd642dd48c4c952ab2168.1720276189.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Add a new parameter expect_errno for make_client(), and use public network helpers make_sockaddr() + connect_to_addr() in it instead of using local defined function make_socket() + connect(). This local function can be dropped latter. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index d03ea3e64a2e..f9b8415c16a6 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -229,25 +229,27 @@ static int make_server(int sotype, const char *ip, int port, return -1; } -static int make_client(int sotype, const char *ip, int port) +static int make_client(int sotype, const char *ip, int port, + const int expect_errno) { - struct sockaddr_storage addr = {0}; + int family = is_ipv6(ip) ? AF_INET6 : AF_INET; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + .expect_errno = expect_errno, + }; + struct sockaddr_storage addr; + socklen_t len; int err, fd; - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) + err = make_sockaddr(family, ip, port, &addr, &len); + if (!ASSERT_OK(err, "make_sockaddr")) return -1; - err = connect(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "make_client", "connect")) { - log_err("failed to connect client socket"); - goto fail; - } + fd = connect_to_addr(sotype, &addr, len, &opts); + if (!ASSERT_GE(fd, 0, "connect_to_addr")) + return -1; return fd; -fail: - close(fd); - return -1; } static __u64 socket_cookie(int fd) @@ -658,7 +660,7 @@ static void run_lookup_prog(const struct test *t) goto close; } - client_fd = make_client(t->sotype, t->connect_to.ip, t->connect_to.port); + client_fd = make_client(t->sotype, t->connect_to.ip, t->connect_to.port, 0); if (client_fd < 0) goto close; @@ -874,7 +876,6 @@ static void test_redirect_lookup(struct test_sk_lookup *skel) static void drop_on_lookup(const struct test *t) { - struct sockaddr_storage dst = {}; int client_fd, server_fd, err; struct bpf_link *lookup_link; ssize_t n; @@ -888,12 +889,12 @@ static void drop_on_lookup(const struct test *t) if (server_fd < 0) goto detach; - client_fd = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + client_fd = make_client(t->sotype, t->connect_to.ip, + t->connect_to.port, ECONNREFUSED); if (client_fd < 0) goto close_srv; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = -1; if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -988,7 +989,6 @@ static void test_drop_on_lookup(struct test_sk_lookup *skel) static void drop_on_reuseport(const struct test *t) { - struct sockaddr_storage dst = { 0 }; int client, server1, server2, err; struct bpf_link *lookup_link; ssize_t n; @@ -1012,12 +1012,12 @@ static void drop_on_reuseport(const struct test *t) if (server2 < 0) goto close_srv1; - client = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + client = make_client(t->sotype, t->connect_to.ip, + t->connect_to.port, ECONNREFUSED); if (client < 0) goto close_srv2; - err = connect(client, (void *)&dst, inetaddr_len(&dst)); + err = -1; if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1164,7 +1164,7 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, if (server_fd < 0) return; - connected_fd = make_client(sotype, EXT_IP4, EXT_PORT); + connected_fd = make_client(sotype, EXT_IP4, EXT_PORT, 0); if (connected_fd < 0) goto out_close_server; @@ -1178,7 +1178,7 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, goto out_close_connected; /* Try to redirect TCP SYN / UDP packet to a connected socket */ - client_fd = make_client(sotype, EXT_IP4, EXT_PORT); + client_fd = make_client(sotype, EXT_IP4, EXT_PORT, 0); if (client_fd < 0) goto out_unlink_prog; if (sotype == SOCK_DGRAM) { From patchwork Sat Jul 6 14:45:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13725862 X-Patchwork-Delegate: bpf@iogearbox.net 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 CC29513A41D; Sat, 6 Jul 2024 14:47:57 +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=1720277277; cv=none; b=JxqoE/NvqL7Bpx9xestuDrvLNEA4hv1ge6v3wpqNhloqSjLA26ZE+BFTPYDt4PB4iK8/b9Z8t6IkHIpok7UVVvQB+ew9Aj3kMscZAJjg3f+8acsyscpg9GjjSH7XSlpfg3w5Sh16vvFGpxWtjMyg0IXlS9Lk/cxGSZXE+dS+Aec= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720277277; c=relaxed/simple; bh=p/nFYb53W3oKJGhyYaKGXlD1rGfvyYSCXpNcmfni+hk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Pox2x2ObE2uP1Tv9MZ4bOVGwgbO76MfzbuusrfeTbb0phDxfVoEGQoII+6cAWaWO6G+V/O4QFC8NRRpngzN7GI64zhsNgowZtOElBHAjpEhAaa3cWYQWUDkGiIReshUD+OKMEV5cRyC9zINFCbelv4n2PjVJHDeJx5om4/vDGes= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UUY642Bu; 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="UUY642Bu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 45B76C4AF0F; Sat, 6 Jul 2024 14:47:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720277277; bh=p/nFYb53W3oKJGhyYaKGXlD1rGfvyYSCXpNcmfni+hk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UUY642Bu39wv09XN7NPXTd9Hf1HYMtyuE6h0E5oQZ5bbDYJH/FvLndGTONeWVwCbT S+chQxO77Q527c8BHYKiLHC/NKYxN9YhIdBFl3B6UcR5nVEWxhJGLt/fZPdUTRiUxj DxhQnmJOZ1B5zX1h2PPKs44bjC1ZMajcTGfalO7rtkH+3ThwxBVp2krVx1vdnE+zcq 1Cxz3TR6KywrLC4+LcCZYnxZfO6/DbAWoZm/7u24BoAab4zvfpNdPxW9IOUlzT+l6C zRGzUsghLuC/2HD8eVCghZzN0tQhy7VPljiNy2Ut3o+2OnAM8y/A1uqmTwicr62f1G baD0pupTHKo7Q== 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, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 11/11] selftests/bpf: Drop make_socket in sk_lookup Date: Sat, 6 Jul 2024 22:45:56 +0800 Message-ID: <271f65310f9651d0059377e1c2a873edad2b1a81.1720276189.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Use local helper make_client() in run_multi_prog_lookup() instead of using make_socket() + connect(). Then make_socket() and inetaddr_len() can be dropped now. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 46 ++----------------- 1 file changed, 3 insertions(+), 43 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index f9b8415c16a6..dc84a3f90701 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -108,46 +108,6 @@ static int attach_reuseport(int sock_fd, struct bpf_program *reuseport_prog) return 0; } -static socklen_t inetaddr_len(const struct sockaddr_storage *addr) -{ - return (addr->ss_family == AF_INET ? sizeof(struct sockaddr_in) : - addr->ss_family == AF_INET6 ? sizeof(struct sockaddr_in6) : 0); -} - -static int make_socket(int sotype, const char *ip, int port, - struct sockaddr_storage *addr) -{ - struct timeval timeo = { .tv_sec = IO_TIMEOUT_SEC }; - int err, family, fd; - - family = is_ipv6(ip) ? AF_INET6 : AF_INET; - err = make_sockaddr(family, ip, port, addr, NULL); - if (CHECK(err, "make_address", "failed\n")) - return -1; - - fd = socket(addr->ss_family, sotype, 0); - if (CHECK(fd < 0, "socket", "failed\n")) { - log_err("failed to make socket"); - return -1; - } - - err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); - if (CHECK(err, "setsockopt(SO_SNDTIMEO)", "failed\n")) { - log_err("failed to set SNDTIMEO"); - close(fd); - return -1; - } - - err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); - if (CHECK(err, "setsockopt(SO_RCVTIMEO)", "failed\n")) { - log_err("failed to set RCVTIMEO"); - close(fd); - return -1; - } - - return fd; -} - static int setsockopts(int fd, void *opts) { struct cb_opts *co = (struct cb_opts *)opts; @@ -1227,7 +1187,6 @@ struct test_multi_prog { static void run_multi_prog_lookup(const struct test_multi_prog *t) { - struct sockaddr_storage dst = {}; int map_fd, server_fd, client_fd; struct bpf_link *link1, *link2; int prog_idx, done, err; @@ -1260,11 +1219,12 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) if (err) goto out_close_server; - client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst); + client_fd = make_client(SOCK_STREAM, EXT_IP4, EXT_PORT, + t->expect_errno); if (client_fd < 0) goto out_close_server; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = t->expect_errno ? -1 : 0; if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;