From patchwork Mon Feb 6 07:06:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arseniy Krasnov X-Patchwork-Id: 53039 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2094885wrn; Sun, 5 Feb 2023 23:15:57 -0800 (PST) X-Google-Smtp-Source: AK7set8LB8h1weQKzD129sZS1kFhPpoIpObcChZvZDab0nKcaO8zeR1pKcsoKHjTZBkhTNb3wcmV X-Received: by 2002:a50:d4dc:0:b0:4aa:a51a:7380 with SMTP id e28-20020a50d4dc000000b004aaa51a7380mr6196430edj.0.1675667757235; Sun, 05 Feb 2023 23:15:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675667757; cv=none; d=google.com; s=arc-20160816; b=lOQnBmpn13hecLyigineiskMfkxMgfMae28sD4uMlFj/KsR31VrCb8YwCIUwO2UrTy XUP1N1UmQbNxcNPdkkVBXypJA9NKn9ZTPabTTzz3J8ADK1jg3c4xYZB5iGt5AWKBhbnc 1MKCOzujuhbcyBe1Mpt0dI4Z8oUT9FAZaqGZw4XtXzeD40x/eo7k9SvWIzRei/Btqgny sCL3TsnHm3pfpmqy6gEE7XIExq5kDYAgYX6E3VYlgPBXAUfICb6dD9OOKK6mLj1TKa2d VjhC0OFI53h5iZiOUuGiO6rCoYXoULWBFoxgwKzJ/BHCQ2D9QC1qEyuF1dL9iigW+ye2 IfBw== 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=6pMqrPyHGPQW1VgQ8Akh86QM+ETIOMVEMR7fJ2NeMG8=; b=yjboIERPVg6svPzab4nb5lCWdpl9igZcP82qOWYJLivCWYuaUo6bi3IzfAZl6qK4gi QmEs5kZQMN1UbhLoBj5KC6HmO8UsC4GOMaWMsW170y/egSu9eitvPbKWTZk0b74Z2K2y 1O/WjZ4HpmlOAZqvo8raWmu5MZtx0GjF+0n0ulk2z+CQOAaMoIavvEgxai/KklUk2g1z nTj4UGhcQYx8Sltq68VbWJhN2bO6SsjrN2COPJN7SXk/SZonWt9CQz3avt8H7YnUEc/8 4BJ2WyQsECJr9cWrcTs3WmOC8ZWDsqv9PuyCGUKcyidLrPJD4gULxz+dZ8ZZIVYMAuTY QqSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=IGO63hiB; 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 29-20020a17090600dd00b00880575fb20esi14653937eji.481.2023.02.05.23.15.32; Sun, 05 Feb 2023 23:15:57 -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=IGO63hiB; 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 S229899AbjBFHGm (ORCPT + 99 others); Mon, 6 Feb 2023 02:06:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229864AbjBFHGk (ORCPT ); Mon, 6 Feb 2023 02:06:40 -0500 Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E6361ABC3; Sun, 5 Feb 2023 23:06:34 -0800 (PST) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mx.sberdevices.ru (Postfix) with ESMTP id 0B0025FD03; Mon, 6 Feb 2023 10:06:33 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1675667193; bh=6pMqrPyHGPQW1VgQ8Akh86QM+ETIOMVEMR7fJ2NeMG8=; h=From:To:Subject:Date:Message-ID:Content-Type:MIME-Version; b=IGO63hiBq7CW+U4nicOdwQ0xr1f/HcwilSpOFPbuMFJ/sZGQEMyzvz3Blh72Mexz1 9Mb6QcD4SPSQdhnvIl0NaeBT7++gaG/ZOkTuxrLv/sVIroJfl0QcppQTMw/X5IajvL aueaHKIeYE+L0RARt5or1Jqk7G8NJHCzXV1gSg+9ogb0n6/jwOdGhEZ4wIm4GPG0xW T3hJhbs68KjhurIknbSfklgLiEiLzwsAm7E1OtmFvwXa4l5P1z4oQk3ZhThlVvKRWp zwATTx+GyuxEZqYrlb/czAJRol2CX5zbbC9i0JaSZWEjV+7qCSrVWYxhFWncXGwu8Y ofKoMfgqI+UUA== Received: from S-MS-EXCH02.sberdevices.ru (S-MS-EXCH02.sberdevices.ru [172.16.1.5]) by mx.sberdevices.ru (Postfix) with ESMTP; Mon, 6 Feb 2023 10:06:32 +0300 (MSK) From: Arseniy Krasnov To: Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Arseniy Krasnov , "Krasnov Arseniy" CC: "linux-kernel@vger.kernel.org" , "kvm@vger.kernel.org" , "virtualization@lists.linux-foundation.org" , "netdev@vger.kernel.org" , kernel Subject: [RFC PATCH v1 12/12] test/vsock: MSG_ZEROCOPY support for vsock_perf Thread-Topic: [RFC PATCH v1 12/12] test/vsock: MSG_ZEROCOPY support for vsock_perf Thread-Index: AQHZOfmKbgKypo6Z8EmH8tGoyYFWVw== Date: Mon, 6 Feb 2023 07:06:32 +0000 Message-ID: <03570f48-f56a-2af4-9579-15a685127aeb@sberdevices.ru> In-Reply-To: <0e7c6fc4-b4a6-a27b-36e9-359597bba2b5@sberdevices.ru> 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: <26536C37BB13B643B0FDC846754C9890@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: 2023/02/06 01:18:00 #20834045 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?1757064994059763328?= X-GMAIL-MSGID: =?utf-8?q?1757064994059763328?= To use this option pass '--zc' parameter: ./vsock_perf --zc --sender --port --bytes With this option MSG_ZEROCOPY flag will be passed to the 'send()' call. Signed-off-by: Arseniy Krasnov --- tools/testing/vsock/vsock_perf.c | 127 +++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 7 deletions(-) -- 2.25.1 diff --git a/tools/testing/vsock/vsock_perf.c b/tools/testing/vsock/vsock_perf.c index a72520338f84..1d435be9b48e 100644 --- a/tools/testing/vsock/vsock_perf.c +++ b/tools/testing/vsock/vsock_perf.c @@ -18,6 +18,8 @@ #include #include #include +#include +#include #define DEFAULT_BUF_SIZE_BYTES (128 * 1024) #define DEFAULT_TO_SEND_BYTES (64 * 1024) @@ -28,9 +30,14 @@ #define BYTES_PER_GB (1024 * 1024 * 1024ULL) #define NSEC_PER_SEC (1000000000ULL) +#ifndef SOL_VSOCK +#define SOL_VSOCK 287 +#endif + 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 bool zerocopy; static void error(const char *s) { @@ -247,15 +254,74 @@ static void run_receiver(unsigned long rcvlowat_bytes) close(fd); } +static void recv_completion(int fd) +{ + struct sock_extended_err *serr; + char cmsg_data[128]; + struct cmsghdr *cm; + struct msghdr msg; + int ret; + + msg.msg_control = cmsg_data; + msg.msg_controllen = sizeof(cmsg_data); + + ret = recvmsg(fd, &msg, MSG_ERRQUEUE); + if (ret == -1) + return; + + cm = CMSG_FIRSTHDR(&msg); + if (!cm) { + fprintf(stderr, "cmsg: no cmsg\n"); + return; + } + + if (cm->cmsg_level != SOL_VSOCK) { + fprintf(stderr, "cmsg: unexpected 'cmsg_level'\n"); + return; + } + + if (cm->cmsg_type) { + fprintf(stderr, "cmsg: unexpected 'cmsg_type'\n"); + return; + } + + serr = (void *)CMSG_DATA(cm); + if (serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) { + fprintf(stderr, "serr: wrong origin\n"); + return; + } + + if (serr->ee_errno) { + fprintf(stderr, "serr: wrong error code\n"); + return; + } + + if (zerocopy && (serr->ee_code & SO_EE_CODE_ZEROCOPY_COPIED)) + fprintf(stderr, "warning: copy instead of zerocopy\n"); +} + +static void enable_so_zerocopy(int fd) +{ + int val = 1; + + if (setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val))) + error("setsockopt(SO_ZEROCOPY)"); +} + static void run_sender(int peer_cid, unsigned long to_send_bytes) { time_t tx_begin_ns; time_t tx_total_ns; size_t total_send; + time_t time_in_send; void *data; int fd; - printf("Run as sender\n"); + if (zerocopy) + printf("Run as sender MSG_ZEROCOPY\n"); + else + printf("Run as sender\n"); + printf("Connect to %i:%u\n", peer_cid, port); printf("Send %lu bytes\n", to_send_bytes); printf("TX buffer %lu bytes\n", buf_size_bytes); @@ -265,25 +331,58 @@ static void run_sender(int peer_cid, unsigned long to_send_bytes) if (fd < 0) exit(EXIT_FAILURE); - data = malloc(buf_size_bytes); + if (zerocopy) { + enable_so_zerocopy(fd); - if (!data) { - fprintf(stderr, "'malloc()' failed\n"); - exit(EXIT_FAILURE); + data = mmap(NULL, buf_size_bytes, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (data == MAP_FAILED) { + perror("mmap"); + exit(EXIT_FAILURE); + } + } else { + data = malloc(buf_size_bytes); + + if (!data) { + fprintf(stderr, "'malloc()' failed\n"); + exit(EXIT_FAILURE); + } } memset(data, 0, buf_size_bytes); total_send = 0; + time_in_send = 0; tx_begin_ns = current_nsec(); while (total_send < to_send_bytes) { ssize_t sent; + size_t rest_bytes; + time_t before; + + rest_bytes = to_send_bytes - total_send; - sent = write(fd, data, buf_size_bytes); + before = current_nsec(); + sent = send(fd, data, (rest_bytes > buf_size_bytes) ? + buf_size_bytes : rest_bytes, + zerocopy ? MSG_ZEROCOPY : 0); + time_in_send += (current_nsec() - before); if (sent <= 0) error("write"); + if (zerocopy) { + struct pollfd fds = { 0 }; + + fds.fd = fd; + + if (poll(&fds, 1, -1) < 0) { + perror("poll"); + exit(EXIT_FAILURE); + } + + recv_completion(fd); + } + total_send += sent; } @@ -294,9 +393,14 @@ static void run_sender(int peer_cid, unsigned long to_send_bytes) get_gbps(total_send * 8, tx_total_ns)); printf("total time in 'write()': %f sec\n", (float)tx_total_ns / NSEC_PER_SEC); + printf("time in send %f\n", (float)time_in_send / NSEC_PER_SEC); close(fd); - free(data); + + if (zerocopy) + munmap(data, buf_size_bytes); + else + free(data); } static const char optstring[] = ""; @@ -336,6 +440,11 @@ static const struct option longopts[] = { .has_arg = required_argument, .val = 'R', }, + { + .name = "zc", + .has_arg = no_argument, + .val = 'Z', + }, {}, }; @@ -351,6 +460,7 @@ static void usage(void) " --help This message\n" " --sender Sender mode (receiver default)\n" " of the receiver to connect to\n" + " --zc Enable zerocopy\n" " --port Port (default %d)\n" " --bytes KMG Bytes to send (default %d)\n" " --buf-size KMG Data buffer size (default %d). In sender mode\n" @@ -413,6 +523,9 @@ int main(int argc, char **argv) case 'H': /* Help. */ usage(); break; + case 'Z': /* Zerocopy. */ + zerocopy = true; + break; default: usage(); }