Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp154713rdb; Thu, 30 Nov 2023 00:43:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IEUX6TGOYHc4bQMPehc5k8QWvxCMsmaXHVGRvIn3KhtDbo+7Wjl3MnNjAqxRYAWFCknbBXb X-Received: by 2002:a05:6870:6710:b0:1fa:2d7f:c33 with SMTP id gb16-20020a056870671000b001fa2d7f0c33mr21116699oab.29.1701333812731; Thu, 30 Nov 2023 00:43:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701333812; cv=none; d=google.com; s=arc-20160816; b=Hkiny18NLdWqyum2YsVoqBXI4TmuTNHJw8Ns3QKEte9syUobqG7V0CBVNG0GNxSG+t uY42lwZHiMpMH1qK91dAb9IK9i8CbhUvSkt1MZfaUTuEPMNonxJKO7LlQSb0oncGC3Mg EVB9OtbzT3f2QAPdv5SHYmtw4IBaM/3m8L0tFhuGI0F4yjvT/W4Z/yjtxvaTWujOvES0 pc5yT4s7rSIsT/F5Vrj3yP0dfYIQuQA7T1ibfqzZYVuIMgs9evAi+l8XUrCrJxKrlFWR 3Y9cWZV3x9/XvexK4fsiAURJ6PeJRvkTs/4dFeKgs9nQiHB2lSkv1I0aHdVVv83T8Bwx wLpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=7D3F6df4inV556FrwBxrc42REjXaDfM0mMQ3apZMcjc=; fh=JEq7ZyqHwnKMAbPjHbEHIOO1u1YxTWxtQlCbJmwG+xw=; b=tIxpU7/2HpOwMmduLSnKM1cScrzQvfw+sWTbiJ+rWcWBiNXYXndlXvsDwayyq4GCQr Il9SY0ugzTWSmOALL7Vx4y0iyaUb4I3pC+ysEh+OfXNp77SXc3j/MJ8IVW8si34yvbjh ZJT3pSNm5YhyIxwemU6jF5Fv7sGRnNzuIMsd3slAoPklwVQW35UzH0k6f4wbzaj02XFk jwJ1hn0omRX0+ijg5L10OO69VAZiQeK/iNlKHSoyJxXJKGPdmZ9JpDT9GsemMOsIVCjW lKy49RuRgldvuyqs1pfsNNlVjqrN8Ijr4c4y3cl0mTzgm+Uvhwtm5mgFCAVZYmUZJq0w FO3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aZAnSdUE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id q17-20020a656851000000b005ab3f1980f3si885324pgt.68.2023.11.30.00.43.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 00:43:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aZAnSdUE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 42AAB8037AA9; Thu, 30 Nov 2023 00:43:29 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344954AbjK3InI (ORCPT + 99 others); Thu, 30 Nov 2023 03:43:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231765AbjK3InH (ORCPT ); Thu, 30 Nov 2023 03:43:07 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54C9710DB for ; Thu, 30 Nov 2023 00:43:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701333792; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=7D3F6df4inV556FrwBxrc42REjXaDfM0mMQ3apZMcjc=; b=aZAnSdUEj29FU+c2TF5dvvHGWheA0dOqVVG1ima1oK6NncwQFkmZg5kUw+yYWsSQlqFX1v WwZsiDz2AnUNP0qU2GBvORspShOeAYgXDBnV7DhdKlSyXsl8So9lDFWGAv/d2yA0Oxvita kat33y5Z0PHWgZJVxLJ9GA4EJiFvuMg= Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-288-M78UHVluMX2ADmQhNGb0Iw-1; Thu, 30 Nov 2023 03:42:29 -0500 X-MC-Unique: M78UHVluMX2ADmQhNGb0Iw-1 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-50aa8fbf1e6so861252e87.2 for ; Thu, 30 Nov 2023 00:42:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701333748; x=1701938548; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=7D3F6df4inV556FrwBxrc42REjXaDfM0mMQ3apZMcjc=; b=Tp6CBuATeTE4hVVF6eyTpyiS9M8p8S5tN1jcLWMj61EX3DgASThW129C9tmIQ+SQ+O bnm14jz4elagJSgBN4JjnZFXQxOo5sFP6FQwcg/JnQ0JHlvNiUHn8bX36HcrleEif2xs CWCgIyqYhMcufD2/DiRHwam4/pFXkZQhsdvtZVJkw9AJnCE3IXq4LBwKrxdpcoLofJ+9 WODzg4+dUZwVRM7u2/oJVVVL9bZ+JZ8uuofBX+khO7z2OgWUKz3OM4371TMtE6s/fs8Q XyXGqq5R/CsdHXIXyeF93Ucg0lsukD6ZaafoH0a8I5YsP/JqsneefARfDVO35ILxwLSO r/dw== X-Gm-Message-State: AOJu0Yyjkui6+DaPHxqIMbyE0o31hhEAwxyU9dLpavaTNuY8+WViNbA6 wG+RxQ2blmvLillbKq8bsaoJSMSvpUepmrmPlAQhC+y4hIjXZsNR9rCYyAfgoDC8qSb2MdRE9MU DZTjtH1rjdU2hZVfJezQD+KVa X-Received: by 2002:a05:6512:3b9c:b0:50b:c699:b4f7 with SMTP id g28-20020a0565123b9c00b0050bc699b4f7mr3397314lfv.38.1701333747859; Thu, 30 Nov 2023 00:42:27 -0800 (PST) X-Received: by 2002:a05:6512:3b9c:b0:50b:c699:b4f7 with SMTP id g28-20020a0565123b9c00b0050bc699b4f7mr3397292lfv.38.1701333747517; Thu, 30 Nov 2023 00:42:27 -0800 (PST) Received: from sgarzare-redhat (host-79-46-200-199.retail.telecomitalia.it. [79.46.200.199]) by smtp.gmail.com with ESMTPSA id v9-20020a05600c470900b004063cd8105csm4856905wmo.22.2023.11.30.00.42.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 00:42:26 -0800 (PST) Date: Thu, 30 Nov 2023 09:42:21 +0100 From: Stefano Garzarella To: Arseniy Krasnov Cc: Stefan Hajnoczi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Jason Wang , Bobby Eshleman , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@sberdevices.ru, oxffffaa@gmail.com Subject: Re: [RFC PATCH v4 3/3] vsock/test: SO_RCVLOWAT + deferred credit update test Message-ID: References: <20231129212519.2938875-1-avkrasnov@salutedevices.com> <20231129212519.2938875-4-avkrasnov@salutedevices.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20231129212519.2938875-4-avkrasnov@salutedevices.com> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 30 Nov 2023 00:43:29 -0800 (PST) On Thu, Nov 30, 2023 at 12:25:19AM +0300, Arseniy Krasnov wrote: >Test which checks, that updating SO_RCVLOWAT value also sends credit >update message. Otherwise mutual hungup may happen when receiver didn't >send credit update and then calls 'poll()' with non default SO_RCVLOWAT >value (e.g. waiting enough bytes to read), while sender waits for free >space at receiver's side. Important thing is that this test relies on >kernel's define for maximum packet size for virtio transport and this >value is not exported to user: VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (this >define is used to control moment when to send credit update message). >If this value or its usage will be changed in kernel - this test may >become useless/broken. > >Signed-off-by: Arseniy Krasnov >--- > Changelog: > v1 -> v2: > * Update commit message by removing 'This patch adds XXX' manner. > * Update commit message by adding details about dependency for this > test from kernel internal define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE. > * Add comment for this dependency in 'vsock_test.c' where this define > is duplicated. > v2 -> v3: > * Replace synchronization based on control TCP socket with vsock > data socket - this is needed to allow sender transmit data only > when new buffer size of receiver is visible to sender. Otherwise > there is race and test fails sometimes. > v3 -> v4: > * Replace 'recv_buf()' to 'recv(MSG_DONTWAIT)' in last read operation > in server part. This is needed to ensure that 'poll()' wake up us > when number of bytes ready to read is equal to SO_RCVLOWAT value. > > tools/testing/vsock/vsock_test.c | 149 +++++++++++++++++++++++++++++++ > 1 file changed, 149 insertions(+) > >diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c >index 01fa816868bc..68f7037834db 100644 >--- a/tools/testing/vsock/vsock_test.c >+++ b/tools/testing/vsock/vsock_test.c >@@ -1232,6 +1232,150 @@ static void test_double_bind_connect_client(const struct test_opts *opts) > } > } > >+#define RCVLOWAT_CREDIT_UPD_BUF_SIZE (1024 * 128) >+/* This define is the same as in 'include/linux/virtio_vsock.h': >+ * it is used to decide when to send credit update message during >+ * reading from rx queue of a socket. Value and its usage in >+ * kernel is important for this test. >+ */ >+#define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (1024 * 64) >+ >+static void test_stream_rcvlowat_def_cred_upd_client(const struct test_opts *opts) >+{ >+ size_t buf_size; >+ void *buf; >+ int fd; >+ >+ fd = vsock_stream_connect(opts->peer_cid, 1234); >+ if (fd < 0) { >+ perror("connect"); >+ exit(EXIT_FAILURE); >+ } >+ >+ /* Send 1 byte more than peer's buffer size. */ >+ buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE + 1; >+ >+ buf = malloc(buf_size); >+ if (!buf) { >+ perror("malloc"); >+ exit(EXIT_FAILURE); >+ } >+ >+ /* Wait until peer sets needed buffer size. */ >+ recv_byte(fd, 1, 0); >+ >+ if (send(fd, buf, buf_size, 0) != buf_size) { >+ perror("send failed"); >+ exit(EXIT_FAILURE); >+ } >+ >+ free(buf); >+ close(fd); >+} >+ >+static void test_stream_rcvlowat_def_cred_upd_server(const struct test_opts *opts) >+{ >+ size_t recv_buf_size; >+ struct pollfd fds; >+ size_t buf_size; >+ void *buf; >+ int fd; >+ >+ fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL); >+ if (fd < 0) { >+ perror("accept"); >+ exit(EXIT_FAILURE); >+ } >+ >+ buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE; >+ >+ if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, >+ &buf_size, sizeof(buf_size))) { >+ perror("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)"); >+ exit(EXIT_FAILURE); >+ } >+ >+ /* Send one dummy byte here, because 'setsockopt()' above also >+ * sends special packet which tells sender to update our buffer >+ * size. This 'send_byte()' will serialize such packet with data >+ * reads in a loop below. Sender starts transmission only when >+ * it receives this single byte. >+ */ >+ send_byte(fd, 1, 0); >+ >+ buf = malloc(buf_size); >+ if (!buf) { >+ perror("malloc"); >+ exit(EXIT_FAILURE); >+ } >+ >+ /* Wait until there will be 128KB of data in rx queue. */ >+ while (1) { >+ ssize_t res; >+ >+ res = recv(fd, buf, buf_size, MSG_PEEK); >+ if (res == buf_size) >+ break; >+ >+ if (res <= 0) { >+ fprintf(stderr, "unexpected 'recv()' return: %zi\n", res); >+ exit(EXIT_FAILURE); >+ } >+ } >+ >+ /* There is 128KB of data in the socket's rx queue, >+ * dequeue first 64KB, credit update is not sent. >+ */ >+ recv_buf_size = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE; >+ recv_buf(fd, buf, recv_buf_size, 0, recv_buf_size); >+ recv_buf_size++; >+ >+ /* Updating SO_RCVLOWAT will send credit update. */ >+ if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT, >+ &recv_buf_size, sizeof(recv_buf_size))) { >+ perror("setsockopt(SO_RCVLOWAT)"); >+ exit(EXIT_FAILURE); >+ } >+ >+ memset(&fds, 0, sizeof(fds)); >+ fds.fd = fd; >+ fds.events = POLLIN | POLLRDNORM | POLLERR | >+ POLLRDHUP | POLLHUP; >+ >+ /* This 'poll()' will return once we receive last byte >+ * sent by client. >+ */ >+ if (poll(&fds, 1, -1) < 0) { >+ perror("poll"); >+ exit(EXIT_FAILURE); >+ } >+ >+ if (fds.revents & POLLERR) { >+ fprintf(stderr, "'poll()' error\n"); >+ exit(EXIT_FAILURE); >+ } >+ >+ if (fds.revents & (POLLIN | POLLRDNORM)) { >+ ssize_t res; >+ >+ res = recv(fd, buf, recv_buf_size, MSG_DONTWAIT); >+ if (res != recv_buf_size) { >+ fprintf(stderr, "recv(2), expected %zu, got %zu\n", >+ recv_buf_size, res); >+ exit(EXIT_FAILURE); >+ } Why not just passing MSG_DONTWAIT to recv_buf()? Thanks, Stefano >+ } else { >+ /* These flags must be set, as there is at >+ * least 64KB of data ready to read. >+ */ >+ fprintf(stderr, "POLLIN | POLLRDNORM expected\n"); >+ exit(EXIT_FAILURE); >+ } >+ >+ free(buf); >+ close(fd); >+} >+ > static struct test_case test_cases[] = { > { > .name = "SOCK_STREAM connection reset", >@@ -1342,6 +1486,11 @@ static struct test_case test_cases[] = { > .run_client = test_double_bind_connect_client, > .run_server = test_double_bind_connect_server, > }, >+ { >+ .name = "SOCK_STREAM virtio SO_RCVLOWAT + deferred cred update", >+ .run_client = test_stream_rcvlowat_def_cred_upd_client, >+ .run_server = test_stream_rcvlowat_def_cred_upd_server, >+ }, > {}, > }; > >-- >2.25.1 >