Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp4443390rdh; Wed, 29 Nov 2023 01:26:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IERhH13K52OP08ttW1Jc7OVV98/SM3pmNk9KPu+q+7usAYbm4LUv2f/3BdPNeqgn3CG8W1g X-Received: by 2002:a05:6a00:4091:b0:6be:319:446b with SMTP id bw17-20020a056a00409100b006be0319446bmr25124490pfb.21.1701249987747; Wed, 29 Nov 2023 01:26:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701249987; cv=none; d=google.com; s=arc-20160816; b=oxSHGGlYB36owP2RwFaKV0ahY1zzSm+tm1+P11Awpa8NgWSfHQygrLSvrNO5HXDrZU hT9TPd5OwFv1A7ZavmmBrMiUG8fBCsTA7BBaTygKhOTKAVaNnLKF62Z3H4jhxDtcHaxs QCtulfF+8/IMb5/vPa8YnXSnKAlFWh1TkpokhPZXd4ioqDWrZq9lMappj6Rq0XtPTMKA 5Ce1UqZkdxo/Ysgk4bnmwwGSETkNIqFzfu6kqzjc3SaRqkOLsm2LRf3N773J9AA8j/ik 8Nm687kwA0D2ip4ZzOB+AH3agmmZuXx1+JwDJIY4tHskjUTOeyt1m8YxQxgMHrF2HGsd RrKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-filter; bh=eVwndGgq0W0UsSKxBhx+5YPBwXU+EeeXoPsonuNv8sE=; fh=l7xBQVGdpXDK+Z71lZkrtPT+DrTVUNscI0qNZEzUJW4=; b=u1oFT6YqTzmUNwOfWK5y2Ms56V6yzY/msRFH31uWl7UBaUQUAkD0XVvawfyUwCuIZ0 VKe8YyxFEjrCKVU0DhXKfjUcPy4nNFDanDPzHuL7RafNWGJZbZZqUxabGpfP26VxY0Zd FmnWv9GZTzXOV4FXWouCMUg8qJWS9orZq3/nVkLkoWZU268GXhlRdGB1OtJ6QuG0YQLE 8FfiZzJeVRyAAayClrYMTyhHXEHyMOtqV1WfDeG6x6WSnwA+N8OvT3aqzqDEHK53oKB3 Gs4jXocfZOsaZkjV+VGMemJsKiYfNGQlzQQgY4PSdEQ3VLiIzuOdEJOdWr/Viq0iwoJY FB7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@salutedevices.com header.s=mail header.b=SJrfKwT9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=salutedevices.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id u11-20020a056a00124b00b0068e3efffe2csi14361643pfi.243.2023.11.29.01.26.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 01:26:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@salutedevices.com header.s=mail header.b=SJrfKwT9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=salutedevices.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 41C258081CED; Wed, 29 Nov 2023 01:25:42 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229791AbjK2JY6 (ORCPT + 99 others); Wed, 29 Nov 2023 04:24:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229842AbjK2JYz (ORCPT ); Wed, 29 Nov 2023 04:24:55 -0500 Received: from mx1.sberdevices.ru (mx1.sberdevices.ru [37.18.73.165]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BC15D1; Wed, 29 Nov 2023 01:24:59 -0800 (PST) Received: from p-infra-ksmg-sc-msk01 (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id F3D79100019; Wed, 29 Nov 2023 12:24:55 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru F3D79100019 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1701249896; bh=eVwndGgq0W0UsSKxBhx+5YPBwXU+EeeXoPsonuNv8sE=; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type:From; b=SJrfKwT9bR/NZ/ynUGUOKSpVvGjZCplqJNTnMkVFpGqOvWvGUp3lxOvraoDaJBQ8d /p+9Ww/2LojwNx+jMcVknSBemkHQASMW2VzvIdYnfODtcrJSbzh1md+BEfsDyOL7MI A7iL5FyJ+229888p5lwvLRzPmoSl+bfPBG/aX+XNDkqjyC7uj4jVB/3Y2rOGAW4OSt 1SM07hKJYT1VQ7+Ykok8S9ftDS9vzatzSpAZNO6lBzGmI3Sj70Zz/+vpAeH4r5h3Lw 18DZp/VoeSDWK8k/V1XhJSbOiggS4baHWMSiZva3wtisqPOg+N6Tv1ynhP28QbBxH/ gDOqeHdMzDsWw== Received: from p-i-exch-sc-m01.sberdevices.ru (p-i-exch-sc-m01.sberdevices.ru [172.16.192.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.sberdevices.ru (Postfix) with ESMTPS; Wed, 29 Nov 2023 12:24:55 +0300 (MSK) Received: from [192.168.0.106] (100.64.160.123) by p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 29 Nov 2023 12:24:55 +0300 Message-ID: Date: Wed, 29 Nov 2023 12:16:54 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: [RFC PATCH v3 3/3] vsock/test: SO_RCVLOWAT + deferred credit update test Content-Language: en-US To: Stefano Garzarella CC: Stefan Hajnoczi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Jason Wang , Bobby Eshleman , , , , , , References: <20231122180510.2297075-1-avkrasnov@salutedevices.com> <20231122180510.2297075-4-avkrasnov@salutedevices.com> From: Arseniy Krasnov In-Reply-To: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [100.64.160.123] X-ClientProxiedBy: p-i-exch-sc-m02.sberdevices.ru (172.16.192.103) To p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) X-KSMG-Rule-ID: 10 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 181677 [Nov 29 2023] X-KSMG-AntiSpam-Version: 6.0.0.2 X-KSMG-AntiSpam-Envelope-From: avkrasnov@salutedevices.com X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 5 0.3.5 98d108ddd984cca1d7e65e595eac546a62b0144b, {Tracking_from_domain_doesnt_match_to}, 100.64.160.123:7.1.2;salutedevices.com:7.1.1;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;127.0.0.199:7.1.2;p-i-exch-sc-m01.sberdevices.ru:7.1.1,5.0.1, FromAlignment: s, ApMailHostAddress: 100.64.160.123 X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean X-KSMG-LinksScanning: Clean X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.0.1.6960, bases: 2023/11/29 05:52:00 #22570775 X-KSMG-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-3.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,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 pete.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 (pete.vger.email [0.0.0.0]); Wed, 29 Nov 2023 01:25:42 -0800 (PST) On 29.11.2023 12:16, Stefano Garzarella wrote: > On Wed, Nov 22, 2023 at 09:05:10PM +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. >> >> tools/testing/vsock/vsock_test.c | 142 +++++++++++++++++++++++++++++++ >> 1 file changed, 142 insertions(+) >> >> diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c >> index 5b0e93f9996c..773a71260fba 100644 >> --- a/tools/testing/vsock/vsock_test.c >> +++ b/tools/testing/vsock/vsock_test.c >> @@ -1225,6 +1225,143 @@ 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)) { >> +        recv_buf(fd, buf, recv_buf_size, 0, recv_buf_size); > > Should we set the socket non-blocking? > > Otherwise, here poll() might wake up even if there are not all the > expected bytes due to some bug and recv() block waiting for the > remaining bytes, so we might not notice the bug. Good point! or just use MSG_DONTWAIT flag for only this 'recv()'. Thanks, Arseniy > > 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", >> @@ -1335,6 +1472,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 >> >