From patchwork Tue Jul 9 09:16:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13727526 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 0155615443C; Tue, 9 Jul 2024 09:17:41 +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=1720516662; cv=none; b=RNVrkYGhgDdCjFU4UB+vd3Z8AVQ/cHFAWBwrayrzZl50jboWnK+qQCxnJXboQwKGAYuFcriDLPsE2wDsNN/mMfkZNQo42vEJqIdE/MPj6MxEi09JN2WxRk6kf8wteWp+k5EsmS+ogvsIuahFUMpTB/xyoHAKtHekgXLBuNIgDug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720516662; c=relaxed/simple; bh=cs6yIxGVVGrcoFSCRxBNO4LhFqM+0ikFcaOOvwvx0uI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RaSdVZgnBd+TLaACx6l+Jisxg6CmawXVf9Mmc2oqQZFydLNIyHJrdXTUJkNb19VvGwwAYU9zfjpwL2QJi8PUOMsS/syh04pABeh7R4PUGjIsdrIfWu9rU8qMXP+aBgh+yyIzTxMbeMcyZJQAO0d4TQmiNRz0UcEGL6h4w2f31xc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UNEjF1ob; 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="UNEjF1ob" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ACB82C4AF13; Tue, 9 Jul 2024 09:17:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720516661; bh=cs6yIxGVVGrcoFSCRxBNO4LhFqM+0ikFcaOOvwvx0uI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UNEjF1ob0RvI9pRsD5NRKl45G/F2/p9l1SoQRTlOrcwe428S3HSYVdEKhKAUNu0Z3 7y4qF/yEJvGojZJ+2z4HtcREV78HDusaZCuYqgtoNmpQKdmUk4tyscM9yYh5vGrZAm G98RDkuyxzkBTNZRyPnl2kHv7cUpYrH32qUEbqPbPqSJ/h83yyqVJNxaj2aJptQuXq aeZY/J5hNFA9DoJm3Qg7ypkESIsdqpiliPgnuSrAZol57RA9lrS59UV0ngvisQ/koP dQGjEW+eAVcrnp/Mg0FsWoKmCeqF7wqtn8wdknNJsqQT2oAxlnTbGLkCmTCLI4b3HX /NGuBeEl0/e6g== 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 v11 9/9] selftests/bpf: Drop make_socket in sk_lookup Date: Tue, 9 Jul 2024 17:16:25 +0800 Message-ID: <2c7bf233c4f657719ad7a62c301a60fe744916f5.1720515893.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 drop_on_lookup(), drop_on_reuseport() and run_multi_prog_lookup() instead of using make_socket() + connect(). Then make_socket() and inetaddr_len() can be dropped now. make_client() returns normal "fd" instead of "-1" when connect() fails but "expect_errno" matched in it. So "err = -errno" is needed to get the real error number before checking value of "err". Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 59 +++---------------- 1 file changed, 8 insertions(+), 51 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 26a1c339492e..f4bc6f968789 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; @@ -861,7 +821,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; @@ -875,12 +834,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 = -errno; if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -975,7 +934,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; @@ -999,12 +957,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 = -errno; if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1214,7 +1172,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; @@ -1247,11 +1204,11 @@ 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 ? -errno : 0; if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;