From patchwork Tue Nov 15 20:50:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arseniy Krasnov X-Patchwork-Id: 20560 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2935706wru; Tue, 15 Nov 2022 12:53:58 -0800 (PST) X-Google-Smtp-Source: AA0mqf75NwSs0w0mkXovgHZPKhKPSTyuZ5p1ai1wPPld2Gbw7/yXIPWd7Cgl6GATgeUexUXxSDsO X-Received: by 2002:a05:6402:43c7:b0:460:19c3:2992 with SMTP id p7-20020a05640243c700b0046019c32992mr16227320edc.1.1668545638626; Tue, 15 Nov 2022 12:53:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668545638; cv=none; d=google.com; s=arc-20160816; b=ZulWn1Hx4raRI/P1DBTJb6tpeKu8RX84cccoHTrvfI1qOmE6sqyoVWan/3Xh1QT+Ec LQQ0DNqzG5UzIiu+sqvxxG7wOdgi/JADcXWgC7jl+q7IF/TINx6InkMaocu7HihER7Pa iRNf//EoONCSSxQ+Py7WSzHODFUYhQYcQXzMb7/Jy8SAjXa8MbVPH6ppW/sYtUzcyHM2 oZtXNN/fjXdjPdjhxsdoLpRGa+MAd96GGUNMUPsqUi198czzTm+jxcCUE8QyahekzyfH iKMg8D94t7SVbmoJmAeQyC0YqQO2yiznyvrrJ9Kfpk9veYPr1R0EMg1pnmHvPr63a207 IE5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature; bh=JQL1ga6vP7uVnM/1sMjjuRBMWnCgT0aVBhk5ZnnWS9M=; b=NqrKzVEwh1VrtUgnOBdwSLY5mNavrn8HT4v7JxEqGFN4/h6OK7pwOC5SVMNq+ywTpb Eaz22uJvwyi3zMbcy/333S9WGjWP4faioLy0+p7oTEp9795/remLmU7EafLb8DwPs/wr B2tyFBkwr2kunpS/srH4NlOgyenRBfsLKIl1j4+KdWuDUs+gqHUDYvrWispBQqG74csF KeCUb7WVpR42JWTxIbz8hLcN8HXfmowBTuVPP4YHil78Js9r+a5El7fKqO/6VshHtwU/ sN+4cCSYeDvMNUOkgP8htM43A98KOOZjrQX+93ddxktAUc0FGnoYY8fJFn7wZAJvd5Aj eM3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=TzPG1RHK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z5-20020a056402274500b00461d6a4afd1si13769740edd.8.2022.11.15.12.53.34; Tue, 15 Nov 2022 12:53:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=TzPG1RHK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238575AbiKOUvI (ORCPT + 99 others); Tue, 15 Nov 2022 15:51:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238574AbiKOUul (ORCPT ); Tue, 15 Nov 2022 15:50:41 -0500 Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C95172D1DB; Tue, 15 Nov 2022 12:50:38 -0800 (PST) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mx.sberdevices.ru (Postfix) with ESMTP id 09F105FD07; Tue, 15 Nov 2022 23:50:37 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1668545437; bh=JQL1ga6vP7uVnM/1sMjjuRBMWnCgT0aVBhk5ZnnWS9M=; h=From:To:Subject:Date:Message-ID:Content-Type:MIME-Version; b=TzPG1RHKAgiskePC2sqheWt3Nxq2/yfFKOd9cvqk/T6vDIiyXilCgX4S6rCJLj/YG CAGbI4vvLJ9xUXXPLOlVUQQojI4GWvNgPtvSaUB+7XQyydNKGZRDd9EBp0UtsXHwWX 9WLGlIt56T4b6wbfHUpOBpai65PZEGDVcATwFpd9v21z+ukMMfheYa+8wsKk1TvQC/ ujzPsfXFYgBcfTJ2bzi1s4kRiPIhrn8ewYPQ2b1vi9xeoE5mZyJex0cHpI4eJ3SFRP ICGwvhmr7GoWrwPsSfJ172l2OtPJryVNNvI0C63pTo+UdsvlD4WzCQUAxDZV3PK3jb bF5knFYtqv2QQ== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mx.sberdevices.ru (Postfix) with ESMTP; Tue, 15 Nov 2022 23:50:36 +0300 (MSK) From: Arseniy Krasnov To: Stefano Garzarella CC: "linux-kernel@vger.kernel.org" , "netdev@vger.kernel.org" , "virtualization@lists.linux-foundation.org" , kernel , Bobby Eshleman , Krasnov Arseniy Subject: [RFC PATCH v1 1/3] test/vsock: rework message bound test Thread-Topic: [RFC PATCH v1 1/3] test/vsock: rework message bound test Thread-Index: AQHY+TPpot0teDu5AkucZunOQ/7DDw== Date: Tue, 15 Nov 2022 20:50:36 +0000 Message-ID: In-Reply-To: Accept-Language: en-US, ru-RU Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.16.1.12] Content-ID: <905A2B95B2474D41899DA843AF6EFA5E@sberdevices.ru> MIME-Version: 1.0 X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2022/11/15 16:23:00 #20571948 X-KSMG-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749596911513131073?= X-GMAIL-MSGID: =?utf-8?q?1749596911513131073?= This updates message bound test making it more complex. Instead of sending 1 bytes messages with one MSG_EOR bit, it sends messages of random length(one half of messages are smaller than page size, second half are bigger) with random number of MSG_EOR bits set. Receiver also don't know total number of messages. Signed-off-by: Arseniy Krasnov --- tools/testing/vsock/control.c | 34 +++++++++ tools/testing/vsock/control.h | 2 + tools/testing/vsock/util.c | 13 ++++ tools/testing/vsock/util.h | 1 + tools/testing/vsock/vsock_test.c | 115 +++++++++++++++++++++++++++---- 5 files changed, 152 insertions(+), 13 deletions(-) -- 2.25.1 diff --git a/tools/testing/vsock/control.c b/tools/testing/vsock/control.c index 4874872fc5a3..bed1649bdf3d 100644 --- a/tools/testing/vsock/control.c +++ b/tools/testing/vsock/control.c @@ -141,6 +141,40 @@ void control_writeln(const char *str) timeout_end(); } +void control_writeulong(unsigned long value) +{ + char str[32]; + + if (snprintf(str, sizeof(str), "%lu", value) >= sizeof(str)) { + perror("snprintf"); + exit(EXIT_FAILURE); + } + + control_writeln(str); +} + +unsigned long control_readulong(bool *ok) +{ + unsigned long value; + char *str; + + if (ok) + *ok = false; + + str = control_readln(); + + if (str == NULL) + return 0; + + value = strtoul(str, NULL, 10); + free(str); + + if (ok) + *ok = true; + + return value; +} + /* Return the next line from the control socket (without the trailing newline). * * The program terminates if a timeout occurs. diff --git a/tools/testing/vsock/control.h b/tools/testing/vsock/control.h index 51814b4f9ac1..cdd922dfea68 100644 --- a/tools/testing/vsock/control.h +++ b/tools/testing/vsock/control.h @@ -9,7 +9,9 @@ void control_init(const char *control_host, const char *control_port, void control_cleanup(void); void control_writeln(const char *str); char *control_readln(void); +unsigned long control_readulong(bool *ok); void control_expectln(const char *str); bool control_cmpln(char *line, const char *str, bool fail); +void control_writeulong(unsigned long value); #endif /* CONTROL_H */ diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 2acbb7703c6a..351903836774 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -395,3 +395,16 @@ void skip_test(struct test_case *test_cases, size_t test_cases_len, test_cases[test_id].skip = true; } + +unsigned long djb2(const void *data, size_t len) +{ + unsigned long hash = 5381; + int i = 0; + + while (i < len) { + hash = ((hash << 5) + hash) + ((unsigned char *)data)[i]; + i++; + } + + return hash; +} diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h index a3375ad2fb7f..988cc69a4642 100644 --- a/tools/testing/vsock/util.h +++ b/tools/testing/vsock/util.h @@ -49,4 +49,5 @@ void run_tests(const struct test_case *test_cases, void list_tests(const struct test_case *test_cases); void skip_test(struct test_case *test_cases, size_t test_cases_len, const char *test_id_str); +unsigned long djb2(const void *data, size_t len); #endif /* UTIL_H */ diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c index bb6d691cb30d..107c11165887 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -284,10 +284,14 @@ static void test_stream_msg_peek_server(const struct test_opts *opts) close(fd); } -#define MESSAGES_CNT 7 -#define MSG_EOR_IDX (MESSAGES_CNT / 2) +#define SOCK_BUF_SIZE (2 * 1024 * 1024) +#define MAX_MSG_SIZE (32 * 1024) + static void test_seqpacket_msg_bounds_client(const struct test_opts *opts) { + unsigned long curr_hash; + int page_size; + int msg_count; int fd; fd = vsock_seqpacket_connect(opts->peer_cid, 1234); @@ -296,18 +300,69 @@ static void test_seqpacket_msg_bounds_client(const struct test_opts *opts) exit(EXIT_FAILURE); } - /* Send several messages, one with MSG_EOR flag */ - for (int i = 0; i < MESSAGES_CNT; i++) - send_byte(fd, 1, (i == MSG_EOR_IDX) ? MSG_EOR : 0); + /* Wait, until receiver sets buffer size. */ + control_expectln("SRVREADY"); + + curr_hash = 0; + page_size = getpagesize(); + msg_count = SOCK_BUF_SIZE / MAX_MSG_SIZE; + + for (int i = 0; i < msg_count; i++) { + ssize_t send_size; + size_t buf_size; + int flags; + void *buf; + + /* Use "small" buffers and "big" buffers. */ + if (i & 1) + buf_size = page_size + + (rand() % (MAX_MSG_SIZE - page_size)); + else + buf_size = 1 + (rand() % page_size); + + buf = malloc(buf_size); + + if (!buf) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + /* Set at least one MSG_EOR + some random. */ + if (i == (msg_count / 2) || (rand() & 1)) { + flags = MSG_EOR; + curr_hash++; + } else { + flags = 0; + } + + send_size = send(fd, buf, buf_size, flags); + + if (send_size < 0) { + perror("send"); + exit(EXIT_FAILURE); + } + + if (send_size != buf_size) { + fprintf(stderr, "Invalid send size\n"); + exit(EXIT_FAILURE); + } + + curr_hash += send_size; + curr_hash = djb2(&curr_hash, sizeof(curr_hash)); + } control_writeln("SENDDONE"); + control_writeulong(curr_hash); close(fd); } static void test_seqpacket_msg_bounds_server(const struct test_opts *opts) { + unsigned long sock_buf_size; + unsigned long remote_hash; + unsigned long curr_hash; int fd; - char buf[16]; + char buf[MAX_MSG_SIZE]; struct msghdr msg = {0}; struct iovec iov = {0}; @@ -317,25 +372,58 @@ static void test_seqpacket_msg_bounds_server(const struct test_opts *opts) exit(EXIT_FAILURE); } + sock_buf_size = SOCK_BUF_SIZE; + + if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, + &sock_buf_size, sizeof(sock_buf_size))) { + perror("getsockopt"); + exit(EXIT_FAILURE); + } + + if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, + &sock_buf_size, sizeof(sock_buf_size))) { + perror("getsockopt"); + exit(EXIT_FAILURE); + } + + /* Ready to receive data. */ + control_writeln("SRVREADY"); + /* Wait, until peer sends whole data. */ control_expectln("SENDDONE"); iov.iov_base = buf; iov.iov_len = sizeof(buf); msg.msg_iov = &iov; msg.msg_iovlen = 1; - for (int i = 0; i < MESSAGES_CNT; i++) { - if (recvmsg(fd, &msg, 0) != 1) { - perror("message bound violated"); - exit(EXIT_FAILURE); - } + curr_hash = 0; - if ((i == MSG_EOR_IDX) ^ !!(msg.msg_flags & MSG_EOR)) { - perror("MSG_EOR"); + while (1) { + ssize_t recv_size; + + recv_size = recvmsg(fd, &msg, 0); + + if (!recv_size) + break; + + if (recv_size < 0) { + perror("recvmsg"); exit(EXIT_FAILURE); } + + if (msg.msg_flags & MSG_EOR) + curr_hash++; + + curr_hash += recv_size; + curr_hash = djb2(&curr_hash, sizeof(curr_hash)); } close(fd); + remote_hash = control_readulong(NULL); + + if (curr_hash != remote_hash) { + fprintf(stderr, "Message bounds broken\n"); + exit(EXIT_FAILURE); + } } #define MESSAGE_TRUNC_SZ 32 @@ -837,6 +925,7 @@ int main(int argc, char **argv) .peer_cid = VMADDR_CID_ANY, }; + srand(time(NULL)); init_signals(); for (;;) { From patchwork Tue Nov 15 20:52:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arseniy Krasnov X-Patchwork-Id: 20561 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2935921wru; Tue, 15 Nov 2022 12:54:36 -0800 (PST) X-Google-Smtp-Source: AA0mqf4E0TJCNz05arDYrHR+XnZDYjrehhD4MNOhN0CQGyGkNOC4o3Y75tnk6V3A9aOSsukadMx5 X-Received: by 2002:a17:906:5154:b0:782:7790:f132 with SMTP id jr20-20020a170906515400b007827790f132mr15367639ejc.649.1668545676434; Tue, 15 Nov 2022 12:54:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668545676; cv=none; d=google.com; s=arc-20160816; b=zcXN/SACobBqLo97KeMCSMsfEEptA+C5Vpck4foW8DaZzvYwyVGEAiulTSFjOIWa2O eikrCzXMulrVM9tEC19ML/4jjj2kft6uU1zVgfQsrEZuUqAjMIxrIWnn8jUOxgU0DmS8 +OUrKyBp5ahwWBEdgZXsFrxwR8R/6CZFFSA5k3ySpEUEVgTEiT0ECNbb9tHGaV3+log+ 5LvdcH94oC9DjKX/D/3DBW0R2PR1PHhnfZUY3LE3VjyU0Pf5fGiqSdFbtD70hv21xNaR rKpkv0egoXmSzd48pjon7sVohhjUhDY36XC36ObrvoSFfAVxoovwZPXV6Eu9NogPiRiL tKRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature; bh=W5hJi2NJ0bqPTdJKhkYCcnAEG+dDCh2qMXVB0CfPfa0=; b=i2HLqVKT4IL5dSRh6g8KM3xv/WYj/AezVLxwe9erYY0ZI58DwiWqwXDtCqz2RCxv4H 7CsalAnkJdDpldIMaoddJgwdXGhYGvDCfQBtolWpw41+b64nQIevpgcdRnZWiHRCuyny qqf63sMXaNs5mNC7Z0zIlms7noGRQPUbSQPy2EYdf0Uo+5iqR/A4rssqMWm+ZwxygPrG SIX1zpByT4vHgOlsTI8wGor54n6CEKUNEvymn6qfYQA//uxYHX8BKuF/DBlOIa/E4q0N PBsQwbFffvYaJOu9J4M/U7BtPKGgQ9DbFjAvLxAsRlwkG/rTpX9bcD+zgaw+zfjbtIaV 5YhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=XdVrPJHE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jz6-20020a170906bb0600b007a8c58b51a1si10990134ejb.179.2022.11.15.12.54.13; Tue, 15 Nov 2022 12:54:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=XdVrPJHE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231970AbiKOUw4 (ORCPT + 99 others); Tue, 15 Nov 2022 15:52:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238582AbiKOUwj (ORCPT ); Tue, 15 Nov 2022 15:52:39 -0500 Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 999B930F63; Tue, 15 Nov 2022 12:52:37 -0800 (PST) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mx.sberdevices.ru (Postfix) with ESMTP id 0A6025FD07; Tue, 15 Nov 2022 23:52:36 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1668545556; bh=W5hJi2NJ0bqPTdJKhkYCcnAEG+dDCh2qMXVB0CfPfa0=; h=From:To:Subject:Date:Message-ID:Content-Type:MIME-Version; b=XdVrPJHE+SfWL3SsZ40hp0Q6mfZHyev3hv9+o0WeSrNQwj4IcLwmIa5Hgy5I9PUaw 08eVwTHKZmJePvcZwJkMdVDqPm1enMMs6enQLHsc2z52AOMNqkEIw0mTWtiTcZBZQ2 dfLEFebLshv1Pb38dTrWEJ962ZatT5h5/b4kMzYbjUqJG8FBGc8jZe66exIg+6JccE fkbn54a2hoez2h6QLwNDUB5/XeIpe49YqQFiHjBgcTVN1NcidzIi7PWc+QlJ4Bz7nm xw6K0M4hQWoOTz7akNjN2Akyk/YqERufTz/cHqSzKsJ2dAY0vyC1rlusOhrRpCPEJh AwNG0CtiP/Gbw== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mx.sberdevices.ru (Postfix) with ESMTP; Tue, 15 Nov 2022 23:52:35 +0300 (MSK) From: Arseniy Krasnov To: Stefano Garzarella CC: "linux-kernel@vger.kernel.org" , "netdev@vger.kernel.org" , "virtualization@lists.linux-foundation.org" , kernel , Bobby Eshleman , Krasnov Arseniy Subject: [RFC PATCH v1 2/3] test/vsock: add big message test Thread-Topic: [RFC PATCH v1 2/3] test/vsock: add big message test Thread-Index: AQHY+TQwn9KcvFr2LUmtZTFuocc05g== Date: Tue, 15 Nov 2022 20:52:35 +0000 Message-ID: In-Reply-To: Accept-Language: en-US, ru-RU Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.16.1.12] Content-ID: <61AB262BC80AEC438B4F4C80B53C1C7A@sberdevices.ru> MIME-Version: 1.0 X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2022/11/15 16:23:00 #20571948 X-KSMG-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749596951446738593?= X-GMAIL-MSGID: =?utf-8?q?1749596951446738593?= This adds test for sending message, bigger than peer's buffer size. For SOCK_SEQPACKET socket it must fail, as this type of socket has message size limit. Signed-off-by: Arseniy Krasnov --- tools/testing/vsock/vsock_test.c | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) -- 2.25.1 diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c index 107c11165887..bb4e8657f1d6 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -560,6 +560,63 @@ static void test_seqpacket_timeout_server(const struct test_opts *opts) close(fd); } +static void test_seqpacket_bigmsg_client(const struct test_opts *opts) +{ + unsigned long sock_buf_size; + ssize_t send_size; + socklen_t len; + void *data; + int fd; + + len = sizeof(sock_buf_size); + + fd = vsock_seqpacket_connect(opts->peer_cid, 1234); + if (fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + if (getsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, + &sock_buf_size, &len)) { + perror("getsockopt"); + exit(EXIT_FAILURE); + } + + sock_buf_size++; + + data = malloc(sock_buf_size); + if (!data) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + send_size = send(fd, data, sock_buf_size, 0); + if (send_size != -1) { + fprintf(stderr, "expected 'send(2)' failure, got %zi\n", + send_size); + } + + control_writeln("CLISENT"); + + free(data); + close(fd); +} + +static void test_seqpacket_bigmsg_server(const struct test_opts *opts) +{ + int fd; + + fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL); + if (fd < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + control_expectln("CLISENT"); + + close(fd); +} + #define BUF_PATTERN_1 'a' #define BUF_PATTERN_2 'b' @@ -832,6 +889,11 @@ static struct test_case test_cases[] = { .run_client = test_seqpacket_timeout_client, .run_server = test_seqpacket_timeout_server, }, + { + .name = "SOCK_SEQPACKET big message", + .run_client = test_seqpacket_bigmsg_client, + .run_server = test_seqpacket_bigmsg_server, + }, { .name = "SOCK_SEQPACKET invalid receive buffer", .run_client = test_seqpacket_invalid_rec_buffer_client, From patchwork Tue Nov 15 20:54:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arseniy Krasnov X-Patchwork-Id: 20562 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2936368wru; Tue, 15 Nov 2022 12:56:05 -0800 (PST) X-Google-Smtp-Source: AA0mqf54ZNcW6NjrJddzmKPuzShUYPAGgxQirpsl7z2SdNUzidcz2HHlk2A63GzIkuDkafR2qC6D X-Received: by 2002:aa7:d38b:0:b0:467:71de:fe10 with SMTP id x11-20020aa7d38b000000b0046771defe10mr15198101edq.63.1668545765510; Tue, 15 Nov 2022 12:56:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668545765; cv=none; d=google.com; s=arc-20160816; b=Dwo36fV6GY7SzD27F9J5WE0HtnFEJOyDfavJjBxXhsp8BUdmznLWeiQMvVucZNcG91 3zszoeMVsw2c4/hWJlAZ1Uct5LNnJ123IYHnPUGs8mbyMwncgAhOhcfPozrWz2HXpi6e lG8QTiGGbS/dAgFK5YZk7iFvrhxTGRoPZZbSyhCy9KlSQDXNAvAHR3+BcmqPjApbG5HK +TSdGSlJa8W5Bg21gkA/Xa4CuoUOEIeGUwdzkr/vENsab8dNKEhxbeYodBEl/f0rcK6b JObAhQx1N0XBTv7ZDoO09Wmn9j1k/AEyIflMU19G+iurP0ye/gzrOAyc4zOvUGddi15A vYAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature; bh=A2E+v419f/ZwKB1qLLAkAqVwR/qEM49hQZoXrZ67bFs=; b=zauZdJAmtk0mDludctAnTLnUaof5c58v6CxU0WrnAAQsL0HvolchirlTp2No/Kmwl6 gzkkilLZMOzRwM9qYmF1h7u4O+M/OT1TNcT/rvMNRPjaaQPFq/NGJ2ENA+D4/Offw/tA hQsP8Ih4L/8qvOw6HqjQKi9tfukGzxKG2TqTgPm806ElODocoWBptFGHsqJq5qFamEo8 tXKIUKMtJdY/XHMpVdzwJHKVW6qSdxEiHOrimnAi5+ph8FxYgLsqTtvDii3rWehBVnCM 4aaamnzWYhPdIlBkX6XsqxPkrgmitj5JxK4cL2wC03lSdkAv487RAYT3vmI4uQWOCPZj 9SuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b="mLB/aaQ1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cb15-20020a0564020b6f00b00459dc4e4b7fsi10733731edb.143.2022.11.15.12.55.42; Tue, 15 Nov 2022 12:56:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b="mLB/aaQ1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238614AbiKOUyT (ORCPT + 99 others); Tue, 15 Nov 2022 15:54:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231345AbiKOUyK (ORCPT ); Tue, 15 Nov 2022 15:54:10 -0500 Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF31DD6F; Tue, 15 Nov 2022 12:54:07 -0800 (PST) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mx.sberdevices.ru (Postfix) with ESMTP id 2A2855FD07; Tue, 15 Nov 2022 23:54:06 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1668545646; bh=A2E+v419f/ZwKB1qLLAkAqVwR/qEM49hQZoXrZ67bFs=; h=From:To:Subject:Date:Message-ID:Content-Type:MIME-Version; b=mLB/aaQ19S1tlj6ZyYENgw0/Swx+AXL7frxrW4m1KPA9PGu/7PNcDPL8SrSJdd0oN Umhz5AFwLpRH/7waZnzi42fP5GDud1XhdwIZGsjbuEsVkfHgQG581Qpptx8gAbJleH WZTIKAUv4aTdurG1R0wfXtIHks8W6wf3+vURBixqaB71zH86YqQUgOeQGs+qpshByM 1vr7MJhYn3nP0+ObX1VV8U5SZudHZJIr5/IxUtcXvbk6f1Jz3cWGQIe2IESpxXlPZ8 8rERLrVriFnTBqdctRHoh5mktIBmbBqz/3PhRmqdLFrQQinhAfWfG6ljsKkzt5vnBh LqFLq3wLCpDDA== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mx.sberdevices.ru (Postfix) with ESMTP; Tue, 15 Nov 2022 23:54:06 +0300 (MSK) From: Arseniy Krasnov To: Stefano Garzarella CC: "linux-kernel@vger.kernel.org" , "netdev@vger.kernel.org" , "virtualization@lists.linux-foundation.org" , kernel , Bobby Eshleman , Krasnov Arseniy Subject: [RFC PATCH v1 3/3] test/vsock: vsock_perf utility Thread-Topic: [RFC PATCH v1 3/3] test/vsock: vsock_perf utility Thread-Index: AQHY+TRmd9Cts4oN90SS3C1IVbYAWA== Date: Tue, 15 Nov 2022 20:54:05 +0000 Message-ID: In-Reply-To: Accept-Language: en-US, ru-RU Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.16.1.12] Content-ID: <6045C729747AB74E80C46388BA10C4D0@sberdevices.ru> MIME-Version: 1.0 X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2022/11/15 16:23:00 #20571948 X-KSMG-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749597044756045812?= X-GMAIL-MSGID: =?utf-8?q?1749597044756045812?= This adds utility to check vsock receive performance. Signed-off-by: Arseniy Krasnov --- tools/testing/vsock/Makefile | 1 + tools/testing/vsock/vsock_perf.c | 386 +++++++++++++++++++++++++++++++ 2 files changed, 387 insertions(+) create mode 100644 tools/testing/vsock/vsock_perf.c -- 2.25.1 diff --git a/tools/testing/vsock/Makefile b/tools/testing/vsock/Makefile index f8293c6910c9..d36fdd59fe2e 100644 --- a/tools/testing/vsock/Makefile +++ b/tools/testing/vsock/Makefile @@ -3,6 +3,7 @@ all: test test: vsock_test vsock_diag_test vsock_test: vsock_test.o timeout.o control.o util.o vsock_diag_test: vsock_diag_test.o timeout.o control.o util.o +vsock_perf: vsock_perf.o CFLAGS += -g -O2 -Werror -Wall -I. -I../../include -I../../../usr/include -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE -D_GNU_SOURCE .PHONY: all test clean diff --git a/tools/testing/vsock/vsock_perf.c b/tools/testing/vsock/vsock_perf.c new file mode 100644 index 000000000000..ca7af08dca46 --- /dev/null +++ b/tools/testing/vsock/vsock_perf.c @@ -0,0 +1,386 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * vsock_perf - benchmark utility for vsock. + * + * Copyright (C) 2022 SberDevices. + * + * Author: Arseniy Krasnov + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEFAULT_BUF_SIZE_BYTES (128*1024) +#define DEFAULT_TO_SEND_BYTES (64*1024) +#define DEFAULT_VSOCK_BUF_BYTES (256*1024) +#define DEFAULT_RCVLOWAT_BYTES 1 +#define DEFAULT_PORT 1234 + +#define BYTES_PER_GB (1024 * 1024 * 1024ULL) +#define NSEC_PER_SEC (1000000000ULL) + +static unsigned int port = DEFAULT_PORT; +static unsigned long buf_size_bytes = DEFAULT_BUF_SIZE_BYTES; +static unsigned long vsock_buf_bytes = DEFAULT_VSOCK_BUF_BYTES; + +static inline time_t current_nsec(void) +{ + struct timespec ts; + + if (clock_gettime(CLOCK_REALTIME, &ts)) { + perror("clock_gettime"); + exit(EXIT_FAILURE); + } + + return (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec; +} + +/* From lib/cmdline.c. */ +static unsigned long memparse(const char *ptr) +{ + char *endptr; + + unsigned long long ret = strtoull(ptr, &endptr, 0); + + switch (*endptr) { + case 'E': + case 'e': + ret <<= 10; + case 'P': + case 'p': + ret <<= 10; + case 'T': + case 't': + ret <<= 10; + case 'G': + case 'g': + ret <<= 10; + case 'M': + case 'm': + ret <<= 10; + case 'K': + case 'k': + ret <<= 10; + endptr++; + default: + break; + } + + return ret; +} + +static void vsock_increase_buf_size(int fd) +{ + if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, + &vsock_buf_bytes, sizeof(vsock_buf_bytes))) { + perror("setsockopt"); + exit(EXIT_FAILURE); + } + + if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, + &vsock_buf_bytes, sizeof(vsock_buf_bytes))) { + perror("setsockopt"); + exit(EXIT_FAILURE); + } +} + +static int vsock_connect(unsigned int cid, unsigned int port) +{ + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } addr = { + .svm = { + .svm_family = AF_VSOCK, + .svm_port = port, + .svm_cid = cid, + }, + }; + int fd; + + fd = socket(AF_VSOCK, SOCK_STREAM, 0); + + if (fd < 0) + return -1; + + vsock_increase_buf_size(fd); + + if (connect(fd, &addr.sa, sizeof(addr.svm)) < 0) { + close(fd); + return -1; + } + + return fd; +} + +static float get_gbps(unsigned long bytes, time_t ns_delta) +{ + return ((float)bytes / BYTES_PER_GB) / + ((float)ns_delta / NSEC_PER_SEC); +} + +static void run_sender(unsigned long to_send_bytes) +{ + time_t tx_begin_ns; + size_t total_send; + int client_fd; + char *data; + int fd; + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } addr = { + .svm = { + .svm_family = AF_VSOCK, + .svm_port = port, + .svm_cid = VMADDR_CID_ANY, + }, + }; + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } clientaddr; + + socklen_t clientaddr_len = sizeof(clientaddr.svm); + + fprintf(stderr, "Run as sender\n"); + fprintf(stderr, "Listen port %u\n", port); + fprintf(stderr, "Send %lu bytes\n", to_send_bytes); + fprintf(stderr, "TX buffer %lu bytes\n", buf_size_bytes); + fprintf(stderr, "Peer buffer %lu bytes\n", vsock_buf_bytes); + + fd = socket(AF_VSOCK, SOCK_STREAM, 0); + + if (fd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } + + if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) { + perror("bind"); + exit(EXIT_FAILURE); + } + + if (listen(fd, 1) < 0) { + perror("listen"); + exit(EXIT_FAILURE); + } + + client_fd = accept(fd, &clientaddr.sa, &clientaddr_len); + + if (client_fd < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + vsock_increase_buf_size(client_fd); + + data = malloc(buf_size_bytes); + + if (!data) { + fprintf(stderr, "malloc failed\n"); + exit(EXIT_FAILURE); + } + + memset(data, 0, buf_size_bytes); + total_send = 0; + tx_begin_ns = current_nsec(); + + while (total_send < to_send_bytes) { + ssize_t sent; + + sent = write(client_fd, data, buf_size_bytes); + + if (sent <= 0) { + perror("write"); + exit(EXIT_FAILURE); + } + + total_send += sent; + } + + fprintf(stderr, "tx performance: %f Gb/s\n", + get_gbps(total_send, current_nsec() - tx_begin_ns)); + + close(client_fd); + close(fd); + + free(data); +} + +static void run_receiver(int peer_cid, unsigned long rcvlowat_bytes) +{ + unsigned int read_cnt; + time_t rx_begin_ns; + time_t in_read_ns; + size_t total_recv; + void *data; + int fd; + + fprintf(stderr, "Run as receiver\n"); + fprintf(stderr, "Connect to %i:%u\n", peer_cid, port); + fprintf(stderr, "RX buffer %lu bytes\n", buf_size_bytes); + fprintf(stderr, "Peer buffer %lu bytes\n", vsock_buf_bytes); + fprintf(stderr, "SO_RCVLOWAT %lu bytes\n", rcvlowat_bytes); + + fd = vsock_connect(peer_cid, port); + + if (fd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } + + if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT, + &rcvlowat_bytes, + sizeof(rcvlowat_bytes))) { + perror("setsockopt"); + exit(EXIT_FAILURE); + } + + data = mmap(NULL, buf_size_bytes, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE, -1, 0); + + if (data == MAP_FAILED) { + perror("mmap"); + exit(EXIT_FAILURE); + } + + read_cnt = 0; + in_read_ns = 0; + total_recv = 0; + rx_begin_ns = current_nsec(); + + while (1) { + struct pollfd fds = { 0 }; + + fds.fd = fd; + fds.events = POLLIN | POLLERR | POLLHUP | + POLLRDHUP | POLLNVAL; + + 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) { + ssize_t bytes_read; + time_t t; + + t = current_nsec(); + bytes_read = read(fd, data, buf_size_bytes); + in_read_ns += (current_nsec() - t); + read_cnt++; + + if (!bytes_read) + break; + + if (bytes_read < 0) { + perror("recv"); + exit(EXIT_FAILURE); + } + + total_recv += bytes_read; + } + + if (fds.revents & (POLLHUP | POLLRDHUP)) + break; + } + + fprintf(stderr, "rx performance %f Gb/s\n", + get_gbps(total_recv, current_nsec() - rx_begin_ns)); + fprintf(stderr, "total in 'read()' %f sec\n", (float)in_read_ns / NSEC_PER_SEC); + fprintf(stderr, "POLLIN wakeups: %i\n", read_cnt); + fprintf(stderr, "average in 'read()' %f ns\n", (float)in_read_ns / read_cnt); + + munmap(data, buf_size_bytes); + close(fd); +} + +static void usage(void) +{ + fprintf(stderr, "Help:\n" + "\n" + "This is benchmarking utility, to test vsock performance.\n" + "It runs in two modes: sender or receiver. In sender mode, it waits\n" + "connection from receiver, and when established, sender starts data\n" + "transmission. Total size of data to send is set by '-m' option.\n" + "\n" + "Meaning of '-b' depends of sender or receiver mode. In sender\n" + "mode, it is size of buffer, passed to 'write()'. In receiver mode,\n" + "it is size of rx buffer passed to 'read()'.\n" + "\n" + "Options:\n" + " -h This help message\n" + " -s Sender mode(receiver default)\n" + " -p Port\n" + " -c CID of the peer\n" + " -m Megabytes to send\n" + " -b Depends on sender/receiver mode\n" + " -v Peer buffer size\n" + " -r SO_RCVLOWAT\n" + "\n"); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) +{ + unsigned long to_send_bytes = DEFAULT_TO_SEND_BYTES; + unsigned long rcvlowat_bytes = DEFAULT_RCVLOWAT_BYTES; + bool receiver_mode = true; + int peer_cid = -1; + int c; + + while ((c = getopt(argc, argv, "v:r:c:p:m:b:sh")) != -1) { + switch (c) { + case 'v': /* Peer buffer size. */ + vsock_buf_bytes = memparse(optarg); + break; + case 'r': /* SO_RCVLOWAT value. */ + rcvlowat_bytes = memparse(optarg); + break; + case 'c': /* CID to connect to. */ + peer_cid = atoi(optarg); + break; + case 'p': /* Port to connect to. */ + port = atoi(optarg); + break; + case 'm': /* Bytes to send. */ + to_send_bytes = memparse(optarg); + break; + case 'b': /* Size of rx/tx buffer. */ + buf_size_bytes = memparse(optarg); + break; + case 's': /* Sender mode. */ + receiver_mode = false; + break; + case 'h': /* Help. */ + usage(); + break; + default: + usage(); + + } + } + + if (receiver_mode) + run_receiver(peer_cid, rcvlowat_bytes); + else + run_sender(to_send_bytes); + + return 0; +}