From patchwork Fri Nov 3 17:55:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: f.storniolo95@gmail.com X-Patchwork-Id: 13444865 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 BF17521102; Fri, 3 Nov 2023 17:55:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZI6zIzb7" Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5416E1BD; Fri, 3 Nov 2023 10:55:56 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-9c603e235d1so344346966b.3; Fri, 03 Nov 2023 10:55:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699034155; x=1699638955; 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=hea/FOM7sQnJfBnxfbrgw+YUmgcHRKdwu0JrlIBvasA=; b=ZI6zIzb7oCZL7zq/Tnlpqh2aSgHgADTQ9MIvs5OJP5v/3o0HUHVHHu52PPY3I76lOr v1OA8Y9f+ELFFZrVQfZvD3i9lezN/dWpJQFzOhULDXc31wkONFcr/bABtAqFkbdQRSCx UT9emE2x4xmEl6PrtnkZScAs5MFkifT4cztI13y5UtMeMtu7VjdqsKZmgnKp4eS139lG ULFk3xoYk6dIR+shXsjnSU0vjwS2AQ4f+5IgSbG5j0AvfsdF1tQBN7J9aaTmQBJRq+gD 1d0cBaLHUeh55pE3aljZ8d/yhWpAV1Yr+x5n+XgD/7DPgt9aK3IyzadTvu+Krb6m8yzs xVgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699034155; x=1699638955; 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=hea/FOM7sQnJfBnxfbrgw+YUmgcHRKdwu0JrlIBvasA=; b=orwtO+pKcxZSXGQWZqgvw24WbIqfaorhFRI7XfTQHEPnsrto8L3AnvGvktJLbGgIrA vYhS/FwwTOxVPsz8FH0IGFxBZWXPRnzEXdDgbY1m26OG/MZDjzrlOBv0SFE7v69ipcLD KgaP8o3Z9cCxuorgfTrz3+lx500ZkhqTD6sp6k4DJu2In0j4KlH2ZLbzTJA/j9Rkgmul VECnppmReTfmEbBfi5MPlhWTJ2o9w5L+dXufvwhvpyjSHIXZ3FKyn0txG4kNK1r81tIL A3IlZP2iA8iP2sBuf0qHyGg+W+GV8SaC3v1iNwQmVNOa2qz5h2yktSKtVbHUnuc9+l2L 2kBA== X-Gm-Message-State: AOJu0YxkeUIV9nGSO0d0C4IVLSgSyRpmG4DMx8JR1Ki5zdSSpTAJXbds Lk5dskg9mcvkyebW2Bf7o/c= X-Google-Smtp-Source: AGHT+IEADU2nZsKc7kfsMDqJIYT+yDWS0IsOZHLiDICdhAGbne3xQZBqOzwweMUHvyoKBfTQ82wWPw== X-Received: by 2002:a17:907:2cc3:b0:9bd:7f40:caa5 with SMTP id hg3-20020a1709072cc300b009bd7f40caa5mr6519534ejc.77.1699034154588; Fri, 03 Nov 2023 10:55:54 -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.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:55:54 -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 , Daan De Meyer Subject: [PATCH net 1/4] vsock/virtio: remove socket from connected/bound list on shutdown Date: Fri, 3 Nov 2023 18:55:48 +0100 Message-ID: <20231103175551.41025-2-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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Filippo Storniolo If the same remote peer, using the same port, tries to connect to a server on a listening port more than once, the server will reject the connection, causing a "connection reset by peer" error on the remote peer. This is due to the presence of a dangling socket from a previous connection in both the connected and bound socket lists. The inconsistency of the above lists only occurs when the remote peer disconnects and the server remains active. This bug does not occur when the server socket is closed: virtio_transport_release() will eventually schedule a call to virtio_transport_do_close() and the latter will remove the socket from the bound and connected socket lists and clear the sk_buff. However, virtio_transport_do_close() will only perform the above actions if it has been scheduled, and this will not happen if the server is processing the shutdown message from a remote peer. To fix this, introduce a call to vsock_remove_sock() when the server is handling a client disconnect. This is to remove the socket from the bound and connected socket lists without clearing the sk_buff. Fixes: 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko") Reported-by: Daan De Meyer Tested-by: Daan De Meyer Co-developed-by: Luigi Leonardi Signed-off-by: Luigi Leonardi Signed-off-by: Filippo Storniolo Reviewed-by: Stefano Garzarella --- net/vmw_vsock/virtio_transport_common.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index e22c81435ef7..4c595dd1fd64 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -1369,11 +1369,17 @@ virtio_transport_recv_connected(struct sock *sk, vsk->peer_shutdown |= RCV_SHUTDOWN; if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SHUTDOWN_SEND) vsk->peer_shutdown |= SEND_SHUTDOWN; - if (vsk->peer_shutdown == SHUTDOWN_MASK && - vsock_stream_has_data(vsk) <= 0 && - !sock_flag(sk, SOCK_DONE)) { - (void)virtio_transport_reset(vsk, NULL); - virtio_transport_do_close(vsk, true); + if (vsk->peer_shutdown == SHUTDOWN_MASK) { + if (vsock_stream_has_data(vsk) <= 0 && !sock_flag(sk, SOCK_DONE)) { + (void)virtio_transport_reset(vsk, NULL); + virtio_transport_do_close(vsk, true); + } + /* Remove this socket anyway because the remote peer sent + * the shutdown. This way a new connection will succeed + * if the remote peer uses the same source port, + * even if the old socket is still unreleased, but now disconnected. + */ + vsock_remove_sock(vsk); } if (le32_to_cpu(virtio_vsock_hdr(skb)->flags)) sk->sk_state_change(sk); From patchwork Fri Nov 3 17:55:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: f.storniolo95@gmail.com X-Patchwork-Id: 13444866 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 3B54421357; Fri, 3 Nov 2023 17:55:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MCSzD4TB" Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF329136; Fri, 3 Nov 2023 10:55:57 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-9d2e6c8b542so346259066b.0; Fri, 03 Nov 2023 10:55:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699034156; x=1699638956; 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=10jqauWlqfvt/HR75hSWRIhJKNXu3uAakFmXumXKAxc=; b=MCSzD4TB7ZsG9U+wuhnzDVnohFihfVoB1INN+AAc3i6MoHdgWKIRtVVlMG+W0OVvUl 6CsyQZq5+inijcJvpDPyZBHW5Advns2hBw1baEF65eCNKM+Kgf1eOAou/4QfRY6gIO3A qZUan5fvsV69t3vj1san7KMv52nxewjtpulOxA2yo7Xj8To147rhyARoH4AVsum1KQoy qlNh6XKUzBxYQApB+5QPeS8xIEqBacS70ZnM+2z+6cQZBB9cw4AN8PlK31XTyCRED/U5 tFecs4SQbu7CBJJZ75rW8ukaWjBeyBDbO1IrVb1WpYSwuf69T1vmq+25E+8lQfTqpqqs gqSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699034156; x=1699638956; 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=10jqauWlqfvt/HR75hSWRIhJKNXu3uAakFmXumXKAxc=; b=Km4dOqGzzgpL4oHf7mhi4HNwhuaqJIf4Mkb9MDpgxJviZERjS7JHG0JkZ0zg8N25XD vzvfl4CmilzBO8xlisF/Q6+WLiVFVyz0eyyBGo94P1VsMNOVT+a9fmcb7SNmsbQcC8Ou GXVJwkpeGUbBanex2WkziYte3/n/8yhcJLu6z31zWHEQ/zC9QW18n/ueFwkvscqXz70P kX4yRScCMYmPaGI4d2+8BvfNEsda0AwG0aVekOUXh1E2RffFB9O8tGSuhw/3pOEHqInK TiXExxKEHpgohHa30fdQ9uZzP8u3xeDISB0b1E6zxzFr1bib31WOj4mY/KVzbH1Y8vNZ 1srQ== X-Gm-Message-State: AOJu0Yy/ECOrvi4SYWQs0vUDXHCHTV8sKqGATbBsPn27CGJBlOs8JHNj 0CBmY/qtTf6Zc8ufPv6TBPw= X-Google-Smtp-Source: AGHT+IG72hMdXwEyKY8sI7nP1i81XqfpbLd/QUN0kt/ZLHlnFBIb2P77LqijxxqZe/1/2hhQ60F2DA== X-Received: by 2002:a17:907:74b:b0:9ae:65a5:b6fa with SMTP id xc11-20020a170907074b00b009ae65a5b6famr7085261ejb.32.1699034155977; Fri, 03 Nov 2023 10:55:55 -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.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:55:55 -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 2/4] test/vsock fix: add missing check on socket creation Date: Fri, 3 Nov 2023 18:55:49 +0100 Message-ID: <20231103175551.41025-3-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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Filippo Storniolo Add check on socket() return value in vsock_listen() and vsock_connect() Co-developed-by: Luigi Leonardi Signed-off-by: Luigi Leonardi Signed-off-by: Filippo Storniolo Reviewed-by: Stefano Garzarella --- tools/testing/vsock/util.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 92336721321a..698b0b44a2ee 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -104,6 +104,10 @@ static int vsock_connect(unsigned int cid, unsigned int port, int type) control_expectln("LISTENING"); fd = socket(AF_VSOCK, type, 0); + if (fd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } timeout_begin(TIMEOUT); do { @@ -158,6 +162,10 @@ static int vsock_accept(unsigned int cid, unsigned int port, int old_errno; fd = socket(AF_VSOCK, type, 0); + if (fd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) { perror("bind"); From patchwork Fri Nov 3 17:55:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: f.storniolo95@gmail.com X-Patchwork-Id: 13444867 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 1A493224DE; Fri, 3 Nov 2023 17:56:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lZj/8UPf" Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64689D44; Fri, 3 Nov 2023 10:55:59 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-9dd98378a39so78814666b.0; Fri, 03 Nov 2023 10:55:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699034158; x=1699638958; 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=3sVtZKx35+w7Chro8MkeRQaBuJMC8Dm37tS6SVEgp8c=; b=lZj/8UPfdfCxBwFM2nTC6nxk34IYgqdtIUWLITIwsc3D803VwcJxxfj0brJGi2Cwmn LhrOK+d1DTdwXH+kHdGKDiUiqlXTa9SWaY4ZIXvKUyRtCk/PFS4ffX84RA1QuDjToKvF mNuLQbSw7Dc5Pcjz401nnGZbj+XApAsu4onuKuUK5yPxuC2Kl5CsSRLNd2ruuCxh2HAl 0FyuPUvzOiQFcTb6SOxgrUTJApxln7swsWE/fv1JfCx3tKvtHqo+fYrEMgO+jVNZ+THb 6Hmggg27LQDS5idsaR6STBMt6BErX2MX0aYfV7ne8vtsvV6deMfSQXd38fUZe3bjjOkk BAkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699034158; x=1699638958; 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=3sVtZKx35+w7Chro8MkeRQaBuJMC8Dm37tS6SVEgp8c=; b=gKrNFp14va9HvqjLmyUjQvn820Pp6HZsAmwsfjIRO9HxlNn6iqp0AhKBFMdZDfxCUY yua+JhWRhtfcJ3MNQQDdAxW+RacHHp/dVMZwfy9uDVslLPRiGye0H6ytdwEecuqApbOH DM9aIV4Qv+9Q1dUGEai9hT2VM2lZj9D0lq+xm61dEUw3XyGH8RsKJZEdnrdfAsxdphTS 4PKh5C51WHoIxfK5Z3o5+UTnAo5Lj3vNNIoo6yHG2KBgISO3rbFMVp+9FQgIbozZ5lqd iNNyt3MEDFnC5tMC1TOUg2cjbxk3/sKwVfLdF18z0MVqakJmk6elHF9SPzgkkrBWfCjQ sKFA== X-Gm-Message-State: AOJu0YwwqOQGB0yE7x170XmNxi3g7yHzyeFTaTmB4m2BYekz93AvN0dB IP8AbTQTm8aCJa0uzcgDgqs= X-Google-Smtp-Source: AGHT+IH8If8+KrUor2UBAKUl2OT2Ez6RViX0N4Y+caY1fkZ2T9AZtbgx2FFxCNFArBAhSaRfGzq+sA== X-Received: by 2002:a17:906:4f96:b0:9dc:2291:d384 with SMTP id o22-20020a1709064f9600b009dc2291d384mr3312326eju.22.1699034157567; Fri, 03 Nov 2023 10:55:57 -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.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:55:56 -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 3/4] test/vsock: refactor vsock_accept Date: Fri, 3 Nov 2023 18:55:50 +0100 Message-ID: <20231103175551.41025-4-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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Filippo Storniolo This is a preliminary patch to introduce SOCK_STREAM bind connect test. vsock_accept() is split into vsock_listen() and vsock_accept(). Co-developed-by: Luigi Leonardi Signed-off-by: Luigi Leonardi Signed-off-by: Filippo Storniolo Reviewed-by: Stefano Garzarella --- tools/testing/vsock/util.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 698b0b44a2ee..2fc96f29bdf2 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -136,11 +136,8 @@ int vsock_seqpacket_connect(unsigned int cid, unsigned int port) return vsock_connect(cid, port, SOCK_SEQPACKET); } -/* Listen on and return the first incoming connection. The remote - * address is stored to clientaddrp. clientaddrp may be NULL. - */ -static int vsock_accept(unsigned int cid, unsigned int port, - struct sockaddr_vm *clientaddrp, int type) +/* Listen on and return the file descriptor. */ +static int vsock_listen(unsigned int cid, unsigned int port, int type) { union { struct sockaddr sa; @@ -152,14 +149,7 @@ static int vsock_accept(unsigned int cid, unsigned int port, .svm_cid = cid, }, }; - union { - struct sockaddr sa; - struct sockaddr_vm svm; - } clientaddr; - socklen_t clientaddr_len = sizeof(clientaddr.svm); int fd; - int client_fd; - int old_errno; fd = socket(AF_VSOCK, type, 0); if (fd < 0) { @@ -177,6 +167,24 @@ static int vsock_accept(unsigned int cid, unsigned int port, exit(EXIT_FAILURE); } + return fd; +} + +/* Listen on and return the first incoming connection. The remote + * address is stored to clientaddrp. clientaddrp may be NULL. + */ +static int vsock_accept(unsigned int cid, unsigned int port, + struct sockaddr_vm *clientaddrp, int type) +{ + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } clientaddr; + socklen_t clientaddr_len = sizeof(clientaddr.svm); + int fd, client_fd, old_errno; + + fd = vsock_listen(cid, port, type); + control_writeln("LISTENING"); timeout_begin(TIMEOUT); From patchwork Fri Nov 3 17:55:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: f.storniolo95@gmail.com X-Patchwork-Id: 13444868 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 71D6F225A4; Fri, 3 Nov 2023 17:56:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FjeJPHgS" 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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org 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); } +/* Bind to , connect to and return the file descriptor. */ +int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type) +{ + struct sockaddr_vm sa_client = { + .svm_family = AF_VSOCK, + .svm_cid = VMADDR_CID_ANY, + .svm_port = bind_port, + }; + struct sockaddr_vm sa_server = { + .svm_family = AF_VSOCK, + .svm_cid = cid, + .svm_port = port, + }; + + int client_fd, ret; + + client_fd = 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 = connect(client_fd, (struct sockaddr *)&sa_server, sizeof(sa_server)); + timeout_check("connect"); + } while (ret < 0 && errno == 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); } +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_test.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 test_opts *opts) close(fd); } +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 = sizeof(sa_client); + + listen_fd = vsock_stream_listen(VMADDR_CID_ANY, 1234); + + for (i = 0; i < 2; i++) { + control_writeln("LISTENING"); + + timeout_begin(TIMEOUT); + do { + client_fd = accept(listen_fd, (struct sockaddr *)&sa_client, + &socklen_client); + timeout_check("accept"); + } while (client_fd < 0 && errno == 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 = 0; i < 2; i++) { + /* Wait until server is ready to accept a new connection */ + control_expectln("LISTENING"); + + client_fd = vsock_bind_connect(opts->peer_cid, 1234, 4321, SOCK_STREAM); + + close(client_fd); + } +} + static struct test_case test_cases[] = { { .name = "SOCK_STREAM connection reset", @@ -1285,6 +1330,11 @@ static struct test_case test_cases[] = { .run_client = test_stream_msgzcopy_empty_errq_client, .run_server = test_stream_msgzcopy_empty_errq_server, }, + { + .name = "SOCK_STREAM double bind connect", + .run_client = test_double_bind_connect_client, + .run_server = test_double_bind_connect_server, + }, {}, };