From nobody Wed Dec 31 13:07:40 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8B58C4332F for ; Fri, 3 Nov 2023 17:56:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233821AbjKCR4N (ORCPT ); Fri, 3 Nov 2023 13:56:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232817AbjKCR4D (ORCPT ); Fri, 3 Nov 2023 13:56:03 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC09B1BD; Fri, 3 Nov 2023 10:56:00 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-9d224dca585so354913066b.1; Fri, 03 Nov 2023 10:56:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699034159; x=1699638959; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ANnfbv1oGkDpEz/e45pUqxbq4vbbI9kV1Jy9Nq/Lv90=; b=FjeJPHgSk67T50NWxh5D7atfWqnCvkgvtcY+QMr54VoYRV4tu44oDD4kVvzXWV/PCH HcabX/RFYUe1Uiye4O/Ki1hsOoyCIEGUziDt2Y3NW+oAaY1N2i7WeU4so88gZGpmvXgF Uu9/y/XmPLxG5uMLVxC9X4SzxBCQNeOL/Bo6b8JSJ7o3ON8fHnhWuFgWiuP1rYR5kLVm sze6i0vI79Lf4+xNHFUE9Sten0Et/pQoue3bHo6PUhULDY1RKpCbvRpJDwIXhzV4u8rQ IXjkiD2uK8q7rR+fjmImY4OjfwYMCz3ny/E15mnV/kiaXREvUAlkONxj9lqk/AbNIDuq oq9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699034159; x=1699638959; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ANnfbv1oGkDpEz/e45pUqxbq4vbbI9kV1Jy9Nq/Lv90=; b=L6bw9ebbSWSX42T3KHT9LgoxrStV0bbJk/e7rCo5YxriySh2NNPSwP6IDQDJJ/H7Ja M/IrJ9OjhOFqvc8LMGAEv1ojD7PcutQXU7MvBOtG8XNhgcU2ZXDcXPrwodghRldszvtB fHXso4kn0/TIM58Z1Xot8sFtSI+snZqxnyikWKAtHhUJBCrWTNXfDSsM3umeygFUaXQF 8ZZpzdXzQwZhcWC8/lWOYNPrulgTThxek90MsDlxQZyuncmIlVwrM0eHBeocBQDKjeqj XCombZlvgsP/mmUIZdRZ2fLJRwaNaM7QclplKcg2qhf2riqCgieDgww5Eyx+r9h0PhqB ZV+Q== X-Gm-Message-State: AOJu0YwO6CCTYBHfpg3Q3nHmzKN/FqBliwM59qq4FFdeeGZNxek1c75K pzja79DiMmyxp/H7L/1n6LY= X-Google-Smtp-Source: AGHT+IG+Ba5CGONVICNPRhtchYsMjNYT5WbIR+O63L72F5/7sEGvOVu44gBRrmbNu6Mhg9TmbQSthA== X-Received: by 2002:a17:907:72c2:b0:9ad:c763:bc7a with SMTP id du2-20020a17090772c200b009adc763bc7amr8200301ejc.23.1699034159044; Fri, 03 Nov 2023 10:55:59 -0700 (PDT) Received: from fedora.. (host-62-211-113-16.retail.telecomitalia.it. [62.211.113.16]) by smtp.gmail.com with ESMTPSA id wj6-20020a170907050600b009ddf1a84379sm80306ejb.51.2023.11.03.10.55.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:55:58 -0700 (PDT) From: f.storniolo95@gmail.com To: luigi.leonardi@outlook.com, kvm@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, mst@redhat.com, imbrenda@linux.vnet.ibm.com, kuba@kernel.org, asias@redhat.com, stefanha@redhat.com, pabeni@redhat.com, sgarzare@redhat.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, Filippo Storniolo Subject: [PATCH net 4/4] test/vsock: add dobule bind connect test Date: Fri, 3 Nov 2023 18:55:51 +0100 Message-ID: <20231103175551.41025-5-f.storniolo95@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231103175551.41025-1-f.storniolo95@gmail.com> References: <20231103175551.41025-1-f.storniolo95@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Filippo Storniolo This add bind connect test which creates a listening server socket and tries to connect a client with a bound local port to it twice. Co-developed-by: Luigi Leonardi Signed-off-by: Luigi Leonardi Signed-off-by: Filippo Storniolo Reviewed-by: Stefano Garzarella --- tools/testing/vsock/util.c | 47 ++++++++++++++++++++++++++++++ tools/testing/vsock/util.h | 3 ++ tools/testing/vsock/vsock_test.c | 50 ++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 2fc96f29bdf2..ae2b33c21c45 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -85,6 +85,48 @@ void vsock_wait_remote_close(int fd) close(epollfd); } =20 +/* Bind to , connect to and return the file descrip= tor. */ +int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int b= ind_port, int type) +{ + struct sockaddr_vm sa_client =3D { + .svm_family =3D AF_VSOCK, + .svm_cid =3D VMADDR_CID_ANY, + .svm_port =3D bind_port, + }; + struct sockaddr_vm sa_server =3D { + .svm_family =3D AF_VSOCK, + .svm_cid =3D cid, + .svm_port =3D port, + }; + + int client_fd, ret; + + client_fd =3D socket(AF_VSOCK, type, 0); + if (client_fd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } + + if (bind(client_fd, (struct sockaddr *)&sa_client, sizeof(sa_client))) { + perror("bind"); + exit(EXIT_FAILURE); + } + + timeout_begin(TIMEOUT); + do { + ret =3D connect(client_fd, (struct sockaddr *)&sa_server, sizeof(sa_serv= er)); + timeout_check("connect"); + } while (ret < 0 && errno =3D=3D EINTR); + timeout_end(); + + if (ret < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + return client_fd; +} + /* Connect to and return the file descriptor. */ static int vsock_connect(unsigned int cid, unsigned int port, int type) { @@ -223,6 +265,11 @@ int vsock_stream_accept(unsigned int cid, unsigned int= port, return vsock_accept(cid, port, clientaddrp, SOCK_STREAM); } =20 +int vsock_stream_listen(unsigned int cid, unsigned int port) +{ + return vsock_listen(cid, port, SOCK_STREAM); +} + int vsock_seqpacket_accept(unsigned int cid, unsigned int port, struct sockaddr_vm *clientaddrp) { diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h index a77175d25864..03c88d0cb861 100644 --- a/tools/testing/vsock/util.h +++ b/tools/testing/vsock/util.h @@ -36,9 +36,12 @@ struct test_case { void init_signals(void); unsigned int parse_cid(const char *str); int vsock_stream_connect(unsigned int cid, unsigned int port); +int vsock_bind_connect(unsigned int cid, unsigned int port, + unsigned int bind_port, int type); int vsock_seqpacket_connect(unsigned int cid, unsigned int port); int vsock_stream_accept(unsigned int cid, unsigned int port, struct sockaddr_vm *clientaddrp); +int vsock_stream_listen(unsigned int cid, unsigned int port); int vsock_seqpacket_accept(unsigned int cid, unsigned int port, struct sockaddr_vm *clientaddrp); void vsock_wait_remote_close(int fd); diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_t= est.c index c1f7bc9abd22..5b0e93f9996c 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -1180,6 +1180,51 @@ static void test_stream_shutrd_server(const struct t= est_opts *opts) close(fd); } =20 +static void test_double_bind_connect_server(const struct test_opts *opts) +{ + int listen_fd, client_fd, i; + struct sockaddr_vm sa_client; + socklen_t socklen_client =3D sizeof(sa_client); + + listen_fd =3D vsock_stream_listen(VMADDR_CID_ANY, 1234); + + for (i =3D 0; i < 2; i++) { + control_writeln("LISTENING"); + + timeout_begin(TIMEOUT); + do { + client_fd =3D accept(listen_fd, (struct sockaddr *)&sa_client, + &socklen_client); + timeout_check("accept"); + } while (client_fd < 0 && errno =3D=3D EINTR); + timeout_end(); + + if (client_fd < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + /* Waiting for remote peer to close connection */ + vsock_wait_remote_close(client_fd); + } + + close(listen_fd); +} + +static void test_double_bind_connect_client(const struct test_opts *opts) +{ + int i, client_fd; + + for (i =3D 0; i < 2; i++) { + /* Wait until server is ready to accept a new connection */ + control_expectln("LISTENING"); + + client_fd =3D vsock_bind_connect(opts->peer_cid, 1234, 4321, SOCK_STREAM= ); + + close(client_fd); + } +} + static struct test_case test_cases[] =3D { { .name =3D "SOCK_STREAM connection reset", @@ -1285,6 +1330,11 @@ static struct test_case test_cases[] =3D { .run_client =3D test_stream_msgzcopy_empty_errq_client, .run_server =3D test_stream_msgzcopy_empty_errq_server, }, + { + .name =3D "SOCK_STREAM double bind connect", + .run_client =3D test_double_bind_connect_client, + .run_server =3D test_double_bind_connect_server, + }, {}, }; =20 --=20 2.41.0