From patchwork Mon Apr 8 13:37:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luigi Leonardi X-Patchwork-Id: 13621187 Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04olkn2084.outbound.protection.outlook.com [40.92.74.84]) (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 5A8C17E111; Mon, 8 Apr 2024 13:38:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.74.84 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712583483; cv=fail; b=ESkQQpCD5U3tYm1EdV0qzjAlSrEVJSVQcG7eY3+W/1fdk1kw8C2NHVLKftnIRR91z570rsAgGsJBRKfYO/NJFpfKjOeKggVrahjEs60AcFnmZ/4eL6NYd+LlH3pTSj56Dm31EAl36YVc45Rj9SJT7t1vtMzuMdoFUObH5qXawMQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712583483; c=relaxed/simple; bh=bjs0waJJJzCAnHAplsfPeH63EhgESuijhn62premjRo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=kSJ31U9lsUIwUhmd+9052lox7LRuWKJ9EOKS9bxLRH1vHorJH+YC8tBl+hM2yFu17KGpvYj8tSl5UVa7/+3F8coplN/8gNp5lH2B+hg+0OvQuY1uHrLomgt15aOukT2bZ2HsSpfY0YIpkiMjeJ0C0XW5ml/0r/CdVqLQZhMuJVk= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=RwTMLVeJ; arc=fail smtp.client-ip=40.92.74.84 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="RwTMLVeJ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WvWzAJDKmMg0qYw4G8UyG/8aa/w5jDkRAq6kcJFT85/sqbuXV6hx9QJG+HDdQN11Ul2l4r7twrki/KNOVmbyT+rABjRJFulxNncO9fQi6+RVSt5X/EqdzDr36RmbR4z6wb9oG5CSWySwjrTbJUSx1E7D898j7ANB6HsX+V6ZMx5ipXO/OfnJDp0IiZGyyHwNtyAXj8tHgFZcekXRiCLGDSolnSahEfUtb8CEC8ri0BOgf3BF3zWaclFaeMn8sezSzqkOZh1Iai9eu5aaUU5EwTTlC+KCE1oPZ/LpLA0ZehvmYp3gcedgacvUbNjVYwan9Qr/O4b52QEFxFsWleJPBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=yrNlYd7P5EJrJNCMHnW3SyA0i93SxmOovuYl0g4DPgk=; b=dWCfasbUvBR6r0vnzQClLVIk2e5r/l6O0L3NOmPr6bR2p3hJ/11B76SusNvfoG/3ws4+6cjHMBugRFyQqfrl3yoEaAUUXOngo0QaPGo0/yGuOrStGqw8Y46fqNwXMmeyr1AT/U190Scvfp0ObkmBOPvhEnsnHb7IrFEqbToSnBGc2Oz92fxrydhv556gCCjNiamaML0ojRka8KPGdRuoWMCzO4EyyzDYE0Fw8x2oGI3H2FFi1HQ3I5ntPZAi+HecICrOM6jWCi2yxO7ypWTmUhcKTiM/16JfAoU74gbhV8xE8etKSljec5vI9clTXPbZ0I4JovfwG+TlfgxMLO1JbQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=yrNlYd7P5EJrJNCMHnW3SyA0i93SxmOovuYl0g4DPgk=; b=RwTMLVeJFyjBmEfg5fQcv48ua26GTrZoNOL7fX7eC5ElPJysDCYDPPds1yntqj5sHR0GKIOnRAawmET+APH3ScBdsZh+TdEtiBxI3045fbm5NqH4zyMnweT30kxUBQyEPTf6x+aMzZdUfjU9QvvXztBoL3ZrsEu7ZaFxqu1HsL59u9/BOSPDJrAeBT5Fm8slXX9yO5bHIc0tDj9UJWCEokJ2q1C3CZk5WiiQ39pPlMA3PkjUipHjHtppIEGDjZOy+IGiIGVUkwaAuk8O0G3ZkkYfe0TRr+LNGtRISLDm443+jhI3wsYVfwjXlqr5zqNsM8rFYXX5Z1qABkraqZJnbw== Received: from AS2P194MB2170.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:642::8) by AM8P194MB1662.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:321::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.47; Mon, 8 Apr 2024 13:37:57 +0000 Received: from AS2P194MB2170.EURP194.PROD.OUTLOOK.COM ([fe80::56f0:1717:f1a8:ea4e]) by AS2P194MB2170.EURP194.PROD.OUTLOOK.COM ([fe80::56f0:1717:f1a8:ea4e%2]) with mapi id 15.20.7409.042; Mon, 8 Apr 2024 13:37:57 +0000 From: Luigi Leonardi To: mst@redhat.com, xuanzhuo@linux.alibaba.com, virtualization@lists.linux.dev, sgarzare@redhat.com, netdev@vger.kernel.org, kuba@kernel.org, stefanha@redhat.com, davem@davemloft.net, pabeni@redhat.com, edumazet@google.com, kvm@vger.kernel.org, jasowang@redhat.com Cc: Luigi Leonardi , Daan De Meyer Subject: [PATCH net-next v2 1/3] vsock: add support for SIOCOUTQ ioctl for all vsock socket types. Date: Mon, 8 Apr 2024 15:37:47 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408133749.510520-1-luigi.leonardi@outlook.com> References: <20240408133749.510520-1-luigi.leonardi@outlook.com> X-TMN: [PhkoXylYMxcPjdqbpOwBRj77EFiJq/nQ] X-ClientProxiedBy: MI0P293CA0013.ITAP293.PROD.OUTLOOK.COM (2603:10a6:290:44::18) To AS2P194MB2170.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:642::8) X-Microsoft-Original-Message-ID: <20240408133749.510520-2-luigi.leonardi@outlook.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS2P194MB2170:EE_|AM8P194MB1662:EE_ X-MS-Office365-Filtering-Correlation-Id: 1eb9f2cf-ea77-4947-65c3-08dc57d119b2 X-MS-Exchange-SLBlob-MailProps: znQPCv1HvwW17eFZrekaPBZuDTSQRy8lVwRyxtiHsHNEkzM5ereoWqUmXLCJpiI2KdtWfnEJlRwiIi5mPKSVDXFI21jLyOYbybTTC8N6OfX+3e7gvbREtHrFEBZwoS5rODHri9ob9bPazOQhgiPw7yle1SUJOwMBp5l74+ANRyRbMGqJNJV79hQFrzlZZ1Ttz0agVhEHTVw5aJfIhMeFe8u91CA5vwd/zWPL14rFlD1Xp01AnW+ASg6QoiQ7dshBQf+/vhftA7mrBkANYmXUgudU2MV/MskvwD6bSh1fddLoeTqom2WfSoFdU9JYmOQ0UH2ncfAZ9Euk7V43Drzz5O/2hQYlyRdNo4/2KKSMS0prtrmBqzJyR26ZVnFit7l98dqVjpVPfhNshsqErKw9JxMwRFnm29c3zW8I/xUB7C7V5paPPoIpNveRg3O6VEnWqejDsFuWbEt5GPalnmCAYmdtHKF93GkqkoU4l42i6H0WMBaQFUkLo+qHMhubTa14cD+PHiP/HMCZiq5VKzR/I90vNxICQh34MjoHI5eVbejMQKN0IHdGsLLERbSUgiGFQL7AnCqMzq5kXuTtQFO9gBL3cdemgA/4rJERdYJgrSaBz0NdkdWpYhu6aNifCcwJtJctenOSrirKROGZyq2k6YX6/El8nhHeJwszjSbp3E4Iyj/vUUzh67BhilI7YiZhMlR/PefFzEYRw1Gx4MPh8fPXdmWY31db79EaC3put9N0nWg3bh5yaXlr5XddpXnTdkHt0UW/9/U= X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pK12T2p4NdFk7NsLT3N2Zd+arDOl1qJ2pNZgcvFpCYqFwaXc2u82ElautOWpR9nW4vmgG+PafLIPYcE7BAJyyw8hHCIOl/QV95eHQweNGttLSJV+g1nrGKs5x6jc33aOkZjrwY5e+zvtzozvJnaE7WGXBNN5xUiWehsVQlrDVIX8sp2L/SsliY616XJQRL9cM2o8odMX4vNwTjE8dNpNp8lwHBqgm0SPEsU3R5LaRFo4qMf8UfMEYhqtxUhRS2ByC4wOL9ISdKU/LVJ+j6TIwX4sido+S6qCIZUY5HKrzQL2kJ2uVffJnSyzLC036VtJgrwFJO16af6MjtiWmUaqAkgLc4jabL0AiDSO3ikNj+QreHsZAgfkT7rlpsDAqeltgqYqdwYWsI1NpYtU0JV304zhtGdx06W2e+ZvpAmt+bGgpqHaWGGMjr2TLniQaynm/8WOf/99iBTDNDtX49JLSf5kTwatBu+jIS/Az/mq1qU0/RwiziIqdINDPpJdFJoQSbxokBFmK7g+BMnxbhQVpTOMlFRscjnw0GPwikd5PU2ki4LvA9xsIih4qMKu/hLI4skH9bbaK6U/qTr/xR4Xp+u7rH49mjlYGFsjXNC+QJAjfupnIxdlaHB34kJUYsKz X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VjksOsJF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zQSwx4X1N2knaSvNbo08fTFo+mnWtcuFWVnvoBdA/YaN82GcyhMISWktK72R0qUbQrQ9C1A6BhavUjfnupxovNkmAxpL9TR1TfVJ0NSyOFMpxrvNsePs2JlQOQCIG6gwYRVZPFROfRzcSjkLy/oRroFNAAnob5e52aGtaRuDRy0Xnq1UidbkwoJCgQQj9N4lZ X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1eb9f2cf-ea77-4947-65c3-08dc57d119b2 X-MS-Exchange-CrossTenant-AuthSource: AS2P194MB2170.EURP194.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Apr 2024 13:37:57.2938 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8P194MB1662 This add support for ioctl(s) for SOCK_STREAM SOCK_SEQPACKET and SOCK_DGRAM in AF_VSOCK. The only ioctl available is SIOCOUTQ/TIOCOUTQ, which returns the number of unsent bytes in the socket. This information is transport-specific and is delegated to them using a callback. Suggested-by: Daan De Meyer Signed-off-by: Luigi Leonardi --- include/net/af_vsock.h | 3 +++ net/vmw_vsock/af_vsock.c | 51 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index 535701efc1e5..7d67faa7bbdb 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -169,6 +169,9 @@ struct vsock_transport { void (*notify_buffer_size)(struct vsock_sock *, u64 *); int (*notify_set_rcvlowat)(struct vsock_sock *vsk, int val); + /* SIOCOUTQ ioctl */ + int (*unsent_bytes)(struct vsock_sock *vsk); + /* Shutdown. */ int (*shutdown)(struct vsock_sock *, int); diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 54ba7316f808..fc108283409a 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -112,6 +112,7 @@ #include #include #include +#include static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr); static void vsock_sk_destruct(struct sock *sk); @@ -1292,6 +1293,50 @@ int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, } EXPORT_SYMBOL_GPL(vsock_dgram_recvmsg); +static int vsock_do_ioctl(struct socket *sock, unsigned int cmd, + int __user *arg) +{ + struct sock *sk = sock->sk; + struct vsock_sock *vsk; + int retval; + + vsk = vsock_sk(sk); + + switch (cmd) { + case SIOCOUTQ: { + int n_bytes; + + if (vsk->transport->unsent_bytes) { + if (sock_type_connectible(sk->sk_type) && sk->sk_state == TCP_LISTEN) { + retval = -EINVAL; + break; + } + + n_bytes = vsk->transport->unsent_bytes(vsk); + if (n_bytes < 0) { + retval = n_bytes; + break; + } + + retval = put_user(n_bytes, arg); + } else { + retval = -EOPNOTSUPP; + } + break; + } + default: + retval = -ENOIOCTLCMD; + } + + return retval; +} + +static int vsock_ioctl(struct socket *sock, unsigned int cmd, + unsigned long arg) +{ + return vsock_do_ioctl(sock, cmd, (int __user *)arg); +} + static const struct proto_ops vsock_dgram_ops = { .family = PF_VSOCK, .owner = THIS_MODULE, @@ -1302,7 +1347,7 @@ static const struct proto_ops vsock_dgram_ops = { .accept = sock_no_accept, .getname = vsock_getname, .poll = vsock_poll, - .ioctl = sock_no_ioctl, + .ioctl = vsock_ioctl, .listen = sock_no_listen, .shutdown = vsock_shutdown, .sendmsg = vsock_dgram_sendmsg, @@ -2286,7 +2331,7 @@ static const struct proto_ops vsock_stream_ops = { .accept = vsock_accept, .getname = vsock_getname, .poll = vsock_poll, - .ioctl = sock_no_ioctl, + .ioctl = vsock_ioctl, .listen = vsock_listen, .shutdown = vsock_shutdown, .setsockopt = vsock_connectible_setsockopt, @@ -2308,7 +2353,7 @@ static const struct proto_ops vsock_seqpacket_ops = { .accept = vsock_accept, .getname = vsock_getname, .poll = vsock_poll, - .ioctl = sock_no_ioctl, + .ioctl = vsock_ioctl, .listen = vsock_listen, .shutdown = vsock_shutdown, .setsockopt = vsock_connectible_setsockopt, From patchwork Mon Apr 8 13:37:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luigi Leonardi X-Patchwork-Id: 13621188 Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04olkn2084.outbound.protection.outlook.com [40.92.74.84]) (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 6451112D76E; Mon, 8 Apr 2024 13:38:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.74.84 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712583485; cv=fail; b=MlZdr/z1tT+xbOQcM9Pc2qUdv9vHIuOOSovl/35+ej+e1rhjz1HoeZdDoR1yTMKeCmqVLyiB3GuZjS3bukVS+RW/UxS0u9o2jaDUgxUMjBZK7YxPmjjniPUWaIvMpTbeKqaa1oQAwiFwjsbLNuyhusfGFmuh+mgASU6eHiFwMMk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712583485; c=relaxed/simple; bh=nd/KKLr3uEmUGgW3sCQOZFMpuDUs9Fvh8muPWGAXlDI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=oqEmb5juwUip/uczippOt/08lPuZEf1ONHV49H0eZWEXZt8YJzKKZtwc6+826Kd2n1z4jTw2xGhaBG06vFL2i56nM3aZmNU8bG1qBr9ux2pleNA5gsTrVkZ7fxGdB+8vfZAxiwFoSpRlCrd4Pa2ILT4yseDC0gsgbn9lOfEVDCQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=N7/YJds1; arc=fail smtp.client-ip=40.92.74.84 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="N7/YJds1" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l6c0aOpyww/Uoy6T1la0pBWe+SqGBZtizxzCKbGROvRAy/geBTGXlcYok/9AQYsMR8xDkWe4VksEwWYc3gsFaVyF0EiEADesOH2S3Ct7Xr1YNuq0W/avfh1J+K3tSgzOdm3PuBBDYf1RFLFmH0rrDATN8BtoCf3WnUs19X3VzGQoUnwdkhZ7ZM1bhPo0DmyCON+h7ZQglIQtJFCQLQaSuLQc2cQp+pxn8+tB2xX58FtLmm73emVq4uc+L/y/bmukHGmPhBKPAsyhdxgnFc5I6SfaJ4dKWT/5EVjDPCK3NdLX2dgFtMOdbEl8wJKcgxkIUhvBKg4FtBif6TrpEVv7TA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=/famEPvZr0J8tPUcehUv/adCdeNO8EXShja9tY7JAJs=; b=AokQgBS4gPKBzDVkTCdZgB7MR6VVg4Qi1ZzevZl6QGOrj00oSdwwUduTERgxN8aHhdE6KJBwrLCQWdJVkNOF4oWKuO2k4+ELiTCjxtDPbAAcCScPcdsVF5RYRr4rhA99OUZ6nRwESunCUzAyBICDTf68i1hpEXRQzeqioXVjmoNlJgZL0FJ63Sjp8YPuAYwGzWK5/liwt4YMpGXBAwqIL7xtplMMUgq3HhOdpBQll4lvAIRXBC/cSmQvCO2yorzJCq+DHCNf8Cvdxjv0W1AsQFQHsrTqEgtdzi19SjYztQHjRDJv8xRLiAacIiV9JGKD/JZdLmlu/LhLo5/z8zfPPw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/famEPvZr0J8tPUcehUv/adCdeNO8EXShja9tY7JAJs=; b=N7/YJds1ZQev8RUXJz3s86di9Cpi8rfwfI8PFCtC8XotUpUeQMrva+OJqTLeIt0GrcZQosSqKDs3gzAEGQGNARKoeY3hZF+WaxUlNT9dCOapAlIzI1TdTXp2AymUR8LVHM75Id1ew6uttFEEnZcFSh7x8tL276kUq+fCfqBXqkeqGHZGuS9EPY/UjXVs8/zZQjFGaYHfk7fmgQiBp1bJp8uqsas/vQpLCdi3lbF0bxaIGOYA/oo7jY1hmsDLAFYyREKkJIUZiA66uthHfnZbI7HKc+BtIX8zTLVpLE4Kw6tL7AFIVHnFOTVK8gdjvo211AYpywM36rtuflR1mYKFPg== Received: from AS2P194MB2170.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:642::8) by AM8P194MB1662.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:321::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.47; Mon, 8 Apr 2024 13:37:58 +0000 Received: from AS2P194MB2170.EURP194.PROD.OUTLOOK.COM ([fe80::56f0:1717:f1a8:ea4e]) by AS2P194MB2170.EURP194.PROD.OUTLOOK.COM ([fe80::56f0:1717:f1a8:ea4e%2]) with mapi id 15.20.7409.042; Mon, 8 Apr 2024 13:37:58 +0000 From: Luigi Leonardi To: mst@redhat.com, xuanzhuo@linux.alibaba.com, virtualization@lists.linux.dev, sgarzare@redhat.com, netdev@vger.kernel.org, kuba@kernel.org, stefanha@redhat.com, davem@davemloft.net, pabeni@redhat.com, edumazet@google.com, kvm@vger.kernel.org, jasowang@redhat.com Cc: Luigi Leonardi Subject: [PATCH net-next v2 2/3] vsock/virtio: add SIOCOUTQ support for all virtio based transports Date: Mon, 8 Apr 2024 15:37:48 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408133749.510520-1-luigi.leonardi@outlook.com> References: <20240408133749.510520-1-luigi.leonardi@outlook.com> X-TMN: [3QOD7PN58Cqrrege9F7rHYGM+xKZ387G] X-ClientProxiedBy: MI0P293CA0013.ITAP293.PROD.OUTLOOK.COM (2603:10a6:290:44::18) To AS2P194MB2170.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:642::8) X-Microsoft-Original-Message-ID: <20240408133749.510520-3-luigi.leonardi@outlook.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS2P194MB2170:EE_|AM8P194MB1662:EE_ X-MS-Office365-Filtering-Correlation-Id: 21362109-d51b-467e-354a-08dc57d11a2a X-MS-Exchange-SLBlob-MailProps: znQPCv1HvwW17eFZrekaPBZuDTSQRy8lFytrk4eN0aM1NtOQTBoPQWHBK7AluJIVBVnOr8rIn6OuTNB1OAyTiO0v9pJLa3n/QH5M/jiUuaT909n5QpsHgBYGeCBqnYOUs87d7hhqRLyHVf1TvIAJhI6qWw3OUP5DWvoa7B8lHNK83InGSWm+H+l6p2xEtxhzYIWp10kBYoO+w6QgXSUhO3e4b8WmRun4xDbPX9PAM+Ojea6LaMnkGE3BQ+fJcc6uLLyFhzk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C3FVdfQjo3dOtjkTuxFc5fKcXlFf06YB8MSYge37KGQWMciFYAvx/baHoJn2hqfsgkMzFmrac7oWXf25rSdqEFmdhSLwoQ5I8WtrLrJCrOhecnXdLF5cwQsdbabOYW3ms2oIw1rILwer1iaEtkWgfuL5pxycqVjbvW3TwpOOJtaemwZXAZKsdoYT3z96BZSXRJYPkWDfNiWOaQd4OwNdzQeYuOkNghJWuT/HqGrdR1+xEFze0NoZhu0cslpOEHuLxhgrONs44+2woNoJz4tyk0eVNnBkPG3kxGYVaI8LBKQwcy8h5V5Db9u4o6+0FqDjZVcPxNzzIdKe1rcX4v6N7+uhOmhB5TFxjefUygX0Luoj X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 21362109-d51b-467e-354a-08dc57d11a2a X-MS-Exchange-CrossTenant-AuthSource: AS2P194MB2170.EURP194.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Apr 2024 13:37:58.0331 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8P194MB1662 This patch introduce support for stream_bytes_unsent and seqpacket_bytes_unsent ioctl for virtio_transport, vhost_vsock and vsock_loopback. For all transports the unsent bytes counter is incremented in virtio_transport_send_pkt_info. In the virtio_transport (G2H) the counter is decremented each time the host notifies the guest that it consumed the skbuffs. In vhost-vsock (H2G) the counter is decremented after the skbuff is queued in the virtqueue. In vsock_loopback the counter is decremented after the skbuff is dequeued. Signed-off-by: Luigi Leonardi --- drivers/vhost/vsock.c | 4 ++- include/linux/virtio_vsock.h | 7 ++++++ net/vmw_vsock/virtio_transport.c | 4 ++- net/vmw_vsock/virtio_transport_common.c | 33 +++++++++++++++++++++++++ net/vmw_vsock/vsock_loopback.c | 7 ++++++ 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index ec20ecff85c7..dba8b3ea37bf 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -244,7 +244,7 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock, restart_tx = true; } - consume_skb(skb); + virtio_transport_consume_skb_sent(skb, true); } } while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len))); if (added) @@ -451,6 +451,8 @@ static struct virtio_transport vhost_transport = { .notify_buffer_size = virtio_transport_notify_buffer_size, .notify_set_rcvlowat = virtio_transport_notify_set_rcvlowat, + .unsent_bytes = virtio_transport_bytes_unsent, + .read_skb = virtio_transport_read_skb, }, diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index c82089dee0c8..dbb22d45d203 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -134,6 +134,8 @@ struct virtio_vsock_sock { u32 peer_fwd_cnt; u32 peer_buf_alloc; + atomic_t bytes_unsent; + /* Protected by rx_lock */ u32 fwd_cnt; u32 last_fwd_cnt; @@ -193,6 +195,11 @@ s64 virtio_transport_stream_has_data(struct vsock_sock *vsk); s64 virtio_transport_stream_has_space(struct vsock_sock *vsk); u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk); +int virtio_transport_bytes_unsent(struct vsock_sock *vsk); + +void virtio_transport_consume_skb_sent(struct sk_buff *skb, + bool consume); + int virtio_transport_do_socket_init(struct vsock_sock *vsk, struct vsock_sock *psk); int diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c index ee5d306a96d0..a4656d0a2567 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -311,7 +311,7 @@ static void virtio_transport_tx_work(struct work_struct *work) virtqueue_disable_cb(vq); while ((skb = virtqueue_get_buf(vq, &len)) != NULL) { - consume_skb(skb); + virtio_transport_consume_skb_sent(skb, true); added = true; } } while (!virtqueue_enable_cb(vq)); @@ -540,6 +540,8 @@ static struct virtio_transport virtio_transport = { .notify_buffer_size = virtio_transport_notify_buffer_size, .notify_set_rcvlowat = virtio_transport_notify_set_rcvlowat, + .unsent_bytes = virtio_transport_bytes_unsent, + .read_skb = virtio_transport_read_skb, }, diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index 16ff976a86e3..82a31a13dc32 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -419,6 +419,13 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk, */ rest_len -= ret; + /* Avoid to perform an atomic_add on 0 bytes. + * This is equivalent to check on VIRTIO_VSOCK_OP_RW + * as is the only packet type with payload. + */ + if (ret) + atomic_add(ret, &vvs->bytes_unsent); + if (WARN_ONCE(ret != skb_len, "'send_pkt()' returns %i, but %zu expected\n", ret, skb_len)) @@ -463,6 +470,23 @@ void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct sk_buff * } EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt); +void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume) +{ + struct sock *s = skb->sk; + + if (s && skb->len) { + struct vsock_sock *vs = vsock_sk(s); + struct virtio_vsock_sock *vvs; + + vvs = vs->trans; + atomic_sub(skb->len, &vvs->bytes_unsent); + } + + if (consume) + consume_skb(skb); +} +EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent); + u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit) { u32 ret; @@ -891,6 +915,7 @@ int virtio_transport_do_socket_init(struct vsock_sock *vsk, vsk->buffer_size = VIRTIO_VSOCK_MAX_BUF_SIZE; vvs->buf_alloc = vsk->buffer_size; + atomic_set(&vvs->bytes_unsent, 0); spin_lock_init(&vvs->rx_lock); spin_lock_init(&vvs->tx_lock); @@ -1090,6 +1115,14 @@ void virtio_transport_destruct(struct vsock_sock *vsk) } EXPORT_SYMBOL_GPL(virtio_transport_destruct); +int virtio_transport_bytes_unsent(struct vsock_sock *vsk) +{ + struct virtio_vsock_sock *vvs = vsk->trans; + + return atomic_read(&vvs->bytes_unsent); +} +EXPORT_SYMBOL_GPL(virtio_transport_bytes_unsent); + static int virtio_transport_reset(struct vsock_sock *vsk, struct sk_buff *skb) { diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c index 6dea6119f5b2..9098613561e3 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -98,6 +98,8 @@ static struct virtio_transport loopback_transport = { .notify_buffer_size = virtio_transport_notify_buffer_size, .notify_set_rcvlowat = virtio_transport_notify_set_rcvlowat, + .unsent_bytes = virtio_transport_bytes_unsent, + .read_skb = virtio_transport_read_skb, }, @@ -123,6 +125,11 @@ static void vsock_loopback_work(struct work_struct *work) spin_unlock_bh(&vsock->pkt_queue.lock); while ((skb = __skb_dequeue(&pkts))) { + /* Decrement the bytes_sent counter without deallocating skb + * It is freed by the receiver. + */ + virtio_transport_consume_skb_sent(skb, false); + virtio_transport_deliver_tap_pkt(skb); virtio_transport_recv_pkt(&loopback_transport, skb); } From patchwork Mon Apr 8 13:37:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luigi Leonardi X-Patchwork-Id: 13621189 Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04olkn2084.outbound.protection.outlook.com [40.92.74.84]) (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 7DAD512E1DB; Mon, 8 Apr 2024 13:38:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.74.84 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712583487; cv=fail; b=KcuVJL3uj1tO1MRd0rZx6lRbdIRtJtYOjWK+ONHMYuZla1TQfkwaQm/vZbGCXPXM57+l80qaHKwaa+6SNlI7Rgi+AwxwKNfSRCM3gSzvkKV650g9yR8yTZPBFAnx+KeFvR7tVh1KAGw1Ix6lEZ8QQ54hFfroBlNCNH4nIzDEeu4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712583487; c=relaxed/simple; bh=kMkbuSouMVyfx9e17I2jia19HrSAMQA6Vxiyr4jC7TY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=n4HlFrwUlB7rbJpJQ0kdP3foavb8kPJKDWzqSnbbiGxHB+uZTcuH7dlvPxH1/ygbJmatcxaLKsGib1V93er55tLnFNtvfsnpvD+bsnR2KSZh+MhBICGJEzdVq/Snp4S+aujJj59kIblalA93OiutMxifRiCALmK1odUnavRiOFY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=KXtFA0Ss; arc=fail smtp.client-ip=40.92.74.84 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="KXtFA0Ss" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Jt6uIzF/4h1kqkxkaJLVIb03TeuBpRLK0zlkGd1elnkDUyWhP1CnmXNgQP3KBjyCPuUfrKuoZBYXqpJh5tfinXuPcKLhFRmhXDOTIJmkriN1krxI2m0H3HSCujYf7KWskGN4lErJRVjcNaa/B2ybuRZyIMgSW+cKPtb5lkWocOBQeUaCaawj0OKSjF+IEwLhKD8VeibaQ7XoyaxywFj1BzNQvpn2QV8+ZpbrVBdE1YpzHVHphEr6M0UJUQsuk47kAqgwEx+hFl8pLkbP3MJPZfi+GgPOI/Zfmi7UBXxP1PexA5UYejMDZ2rPehcg5xbbUq6NscxvfDQ05ZWQZNCj9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=7WJsOAcjdVDt7Vxd6SdrBgUI2q3HCWMcb42jSXmBoAI=; b=WdLB1+cppQ/dDZVjGhmxb0yLziLBQoDRsZVpOOkieq650v2dBfI5bbTKs80H6F7CiGV0JdTHHuPeVYsM+S5oOwXwLVo62gyYl+1k72/xCJSs/REZSpkAUpVIA7iDaWWA7iCjgZygFhZUXP4G5nxJ79OgDBMrmmyd22WIKHOpVzlhc6PKRQAs4XBV3xpKHr+K7+SWOdrfF5nOoDMU+awBPp5NmZH9o/ipThWiqL6UZQ1aqAacDeOW6NxH9jOIzr3Ns4Zzeu/uzSFOt+yf+VYkv/DFAhlwEQJrUPbCHihiFjkEHN0YkYsx35OyxTgQ7aLKHDdWo33x6n13tLymWbXbNw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7WJsOAcjdVDt7Vxd6SdrBgUI2q3HCWMcb42jSXmBoAI=; b=KXtFA0SsGemNrSCqoNB4Ghd/tVY31F9erv1IYXfI7JV9L4E7UTSHYi5R2l//fgJmMo6VvDMkWbxQ3Uk2a9n4UCfQbXO8QRnxS/4/nfCpVsL44dPKxXefUAinu58M9zQBtDSOFRycYyo+JrqSjI8pmt4x/9xSq5HwRpQP/oJHXB8Mv3rRElJPVBHf4rNcXp8OuwzVQ/SgmqG9bj6uksTfdwHm70T9MntA0jB48efRD19OJ6Rf2ZeGuRYJfhHZCZoPZ4jIexUCunzJaoraJKO+eM9kfFf21b8FUmRdMFF3I8EuFtNfwcZ/4VrfB/KTgGIsj4O3tx0DVMpMxTlRiHkx5Q== Received: from AS2P194MB2170.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:642::8) by AM8P194MB1662.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:321::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.47; Mon, 8 Apr 2024 13:37:58 +0000 Received: from AS2P194MB2170.EURP194.PROD.OUTLOOK.COM ([fe80::56f0:1717:f1a8:ea4e]) by AS2P194MB2170.EURP194.PROD.OUTLOOK.COM ([fe80::56f0:1717:f1a8:ea4e%2]) with mapi id 15.20.7409.042; Mon, 8 Apr 2024 13:37:58 +0000 From: Luigi Leonardi To: mst@redhat.com, xuanzhuo@linux.alibaba.com, virtualization@lists.linux.dev, sgarzare@redhat.com, netdev@vger.kernel.org, kuba@kernel.org, stefanha@redhat.com, davem@davemloft.net, pabeni@redhat.com, edumazet@google.com, kvm@vger.kernel.org, jasowang@redhat.com Cc: Luigi Leonardi Subject: [PATCH net-next v2 3/3] test/vsock: add ioctl unsent bytes test Date: Mon, 8 Apr 2024 15:37:49 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408133749.510520-1-luigi.leonardi@outlook.com> References: <20240408133749.510520-1-luigi.leonardi@outlook.com> X-TMN: [dILONH51rufQGRaOkOyWZiPNRtnd5OWN] X-ClientProxiedBy: MI0P293CA0013.ITAP293.PROD.OUTLOOK.COM (2603:10a6:290:44::18) To AS2P194MB2170.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:642::8) X-Microsoft-Original-Message-ID: <20240408133749.510520-4-luigi.leonardi@outlook.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS2P194MB2170:EE_|AM8P194MB1662:EE_ X-MS-Office365-Filtering-Correlation-Id: cf91efde-5ae0-495c-9032-08dc57d11a9d X-MS-Exchange-SLBlob-MailProps: znQPCv1HvwW17eFZrekaPBZuDTSQRy8lIpg5mhFRKw0dzIef8ex5pMt4k4PMUxPPmFkwPMy3AtVzRP7WjFRwAkckTI3gHEUIXuIjgJqWtPyLF2zlL2K6qCydptc1rmAOf/gqw5QaHcwwXvE0kL/kybgxJLw0x1AIlOOfhHOmDwA/cV9lmbHQf50rfglG/Xk6oish/+4Mx7O4T0BLluXhM4ufip09AuDGsX7DUTWn9niRBDwckLNw5V7I6dJUYiPf6FO30zNe60wtYKa8SN6iPOar3azooJC7i8uZYZXO/LbvJyjvAX/wlOW6jcfHK8QbQxm0BXEIWgouYqAI+ksrepXEqpcslmb+EV4rGrBDRMZ6VsxQFHcLzZKoRFoh8Gxnl0sw/8iHcqfkh9vj0dTPuXkU6G7Mv13LP+fChuc3haj2PTjvkL/EyRd27/G/m0/d8qpzB0TyuQBAbkzJ5Dctt0sy7x/uztwMdA0MpGlJG8eDObpETSRclHsqZDaa5Z463pj0vrvZ18/T28O5wJNQljTakeMMqsn2PrWUhU4xyr9ymACDpmLTcdKgU6wtg7o1tjXr+t+gZFgm6oDCNVWsQPye2A61u5yNy3cCyTamvq4GeHELP/q+AQyDvqPvOPt4y8BOAjC/ZwlfSvG4C/hWR/A/mjcvXrpkEuzdoyRLJRoN/qiGzMnTEEWClAtoZW5fnkGI4e/40xIQ0g9MOaDOXrlceKin2JmrJ1F03PlH2IEWr7oU+7aH3Bc3cUmXQCiFYyoCg7cKF1o= X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DTkg/jpp6AQDWuR1wTaWJN/QIgJnaPHc4PCI9k9501z5hGr1+l152pLieJmQ7L+hrI+pmaYj16q4/MPb1a2G+9yV/SWvWqdNnpoZpboL8d/Bmcdof3s6qGUBGSW4QsaCs1KULjgZ+NNAsjdIUCWqDw/qwJj/PwlnT7cfZyDnnJfYwQFSDqrR7OLucko7Dkk7+uFMGxrnaBmniiN16nr8Kp3cvK7y2QR5I577m1yFz4IWk7pOi5h0/V9MvP6KbKetcbUl51xN/J46mH6kjxpcOncMOCrjr1fQLb2F5+fy0w94upNXaatoh5TiVbo1FXRcNLsvbXob7pRU51DncJbZLSrGIC3uWCvYJfSRoQldqxgKmV8Foq50Qo1/0m8ohWUp/vMK7fMzoQEvX5FRUCpQkQgtOLwfT51sd57CcRx7qk+JMEhDPF64ZiiB1YmXpYqrHWoCiOYwvs0N9YvtJCIVIEFV4q0RmXHxx7MQbemkhqSSPkwjLxHchCqGUaoPN4wzC0XSZ/Dn6j6yL1pUGIqqx1FknRRjLvUh1J8MA4RI3+mk89ffXd4KZtoqMAmFzuuAsjkT+eUKYMorw2cjNrgOBD+MItr0j1jvHiJ7W/8WQTovgqTp9E4N+x/tczyibpy4 X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: x4g4wC0NHtL006aM8irss2E4QcP3OsY4oFZrhLwt9xH4AFejyBX7mRaQ01rCMEzR877rFLQCUeOfGeoduUPA+gV4PcLzjW+CGHOncIe82k212yWaDFBp/g2OK+3cN68wNRMfCu8dumShB89ZdZ5JnZ/sn/tiOvT7PRCwO8cepimoAk092VKIG7lowW4SLsfX0ub0H0H+igV4L/TC2hYQ3Qcd2x2vD+yBfb869UwOSymAcucrS6dppYvROIYEDr3d8JIMMPoRie9D3KwsvJ4Vf6nxNtM/glLkQ5qQ03uA2St0YYmPid/AkLwdEZM/D33xGfbcqbF/cV8kAzJ4el9uHGLlyOC1HF1wz805VyCn3+k7GvSB9EqKHaGy7+UCdywj7PaN/9sJIK1iSB26523S7AqYcKFI2aX0EadyIw3YfqbAvT6XAt/9jcnxgUYzmRK4yn+Vm2iaq4kJZ6bBevU17UC2VOHO5JK6sC5nVPtW3AHZ/AHlaB5R0+dilfKE7ktcJFS2nzv49HZB1IsYX+Izd21ihWykjKazrUeTBqoRK02vMtWVUs/YB5TgL6zHhrwZral4f9cKoLmt+7t5Ody/ov/qIsWIUSfdpNfs6ejDbbPnrUi4qhdVZnIJJf+oc7wDOYJfWbGTFC7AE1ClZ3T6vIvvnwlxsUX5awBfv7Y4xOnSwq5bk8S3OQ3Mhqo4I6nSTcabRvoJGGYeln/X/yvod23Ag/yhGZBD1ZljqkAeztj2ujhdVh8UcFHHFYi4tuDP6aqDR6/4q/xoONP0BW/m97Kf7Z+wMfGgKT69bcKHiF394Q9JWW316optm+qNBvJY7rTF01soBW3XedBUrCKtSXGg2hSMb369wODoI1IVWN2ETax+elg4Um3CzIDmy/ViLgEehjH6G0sklz1g7VKZyJ19Rv3vdnOzLYzFv8+uJ3FCsZvMDW+/nCf+pKvInrV4fwjEmKJGIaMwRL2m/gM/+rAbdWH0jhLgKgRr6Zfx+cMG0sSwlR1OtiuZm+IXOMeQnnfTxOTMjrDZWIaQ8z4eetwDtGOsthY8O+84C875JA+TPPf6dAgEOWk6HWG28IWs7Ew3WuDC2NGmG899NH98f2+PuU5T5z5Vnb7pxfJHhEx5RircpgW34aq5V6iMbsmiddEAWpuSIdr2ov8a8mXsHr+OThXgwVQ8mlJKlywf0hhBNK3bpe9Q6FFBUIFDeIU2WIPwvp9bEq1ojIoczrsuDI3n+R/nyfCozLJQKzSRBUXticP2MCHSadPPhhZ9EP2C X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: cf91efde-5ae0-495c-9032-08dc57d11a9d X-MS-Exchange-CrossTenant-AuthSource: AS2P194MB2170.EURP194.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Apr 2024 13:37:58.8173 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8P194MB1662 This test that after a packet is delivered the number of unsent bytes is zero. Signed-off-by: Luigi Leonardi --- tools/testing/vsock/util.c | 6 +-- tools/testing/vsock/util.h | 3 ++ tools/testing/vsock/vsock_test.c | 85 ++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 554b290fefdc..a3d448a075e3 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -139,7 +139,7 @@ int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_po } /* Connect to and return the file descriptor. */ -static int vsock_connect(unsigned int cid, unsigned int port, int type) +int vsock_connect(unsigned int cid, unsigned int port, int type) { union { struct sockaddr sa; @@ -226,8 +226,8 @@ static int vsock_listen(unsigned int cid, unsigned int port, int type) /* 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) +int vsock_accept(unsigned int cid, unsigned int port, + struct sockaddr_vm *clientaddrp, int type) { union { struct sockaddr sa; diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h index e95e62485959..fff22d4a14c0 100644 --- a/tools/testing/vsock/util.h +++ b/tools/testing/vsock/util.h @@ -39,6 +39,9 @@ struct test_case { void init_signals(void); unsigned int parse_cid(const char *str); unsigned int parse_port(const char *str); +int vsock_connect(unsigned int cid, unsigned int port, int type); +int vsock_accept(unsigned int cid, unsigned int port, + struct sockaddr_vm *clientaddrp, int type); 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); diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c index f851f8961247..c19ffbcca9dd 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include "vsock_test_zerocopy.h" #include "timeout.h" @@ -1238,6 +1240,79 @@ static void test_double_bind_connect_client(const struct test_opts *opts) } } +#define MSG_BUF_IOCTL_LEN 64 +static void test_unsent_bytes_server(const struct test_opts *opts, int type) +{ + unsigned char buf[MSG_BUF_IOCTL_LEN]; + int client_fd; + + client_fd = vsock_accept(VMADDR_CID_ANY, 1234, NULL, type); + if (client_fd < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + recv_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf)); + control_writeln("RECEIVED"); + + close(client_fd); +} + +static void test_unsent_bytes_client(const struct test_opts *opts, int type) +{ + unsigned char buf[MSG_BUF_IOCTL_LEN]; + int ret, fd, sock_bytes_unsent; + + fd = vsock_connect(opts->peer_cid, 1234, type); + if (fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + for (int i = 0; i < sizeof(buf); i++) + buf[i] = rand() & 0xFF; + + send_buf(fd, buf, sizeof(buf), 0, sizeof(buf)); + control_expectln("RECEIVED"); + + ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent); + if (ret < 0) { + perror("ioctl"); + + if (errno != EOPNOTSUPP) + exit(EXIT_FAILURE); + + fprintf(stderr, "Test skipped\n"); + } else if (ret == 0 && sock_bytes_unsent != 0) { + fprintf(stderr, + "Unexpected 'SIOCOUTQ' value, expected 0, got %i\n", + sock_bytes_unsent); + exit(EXIT_FAILURE); + } + + close(fd); +} + +static void test_stream_unsent_bytes_client(const struct test_opts *opts) +{ + test_unsent_bytes_client(opts, SOCK_STREAM); +} + +static void test_stream_unsent_bytes_server(const struct test_opts *opts) +{ + test_unsent_bytes_server(opts, SOCK_STREAM); +} + +static void test_seqpacket_unsent_bytes_client(const struct test_opts *opts) +{ + test_unsent_bytes_client(opts, SOCK_SEQPACKET); +} + +static void test_seqpacket_unsent_bytes_server(const struct test_opts *opts) +{ + test_unsent_bytes_server(opts, SOCK_SEQPACKET); +} + #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 @@ -1523,6 +1598,16 @@ static struct test_case test_cases[] = { .run_client = test_stream_rcvlowat_def_cred_upd_client, .run_server = test_stream_cred_upd_on_low_rx_bytes, }, + { + .name = "SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes", + .run_client = test_stream_unsent_bytes_client, + .run_server = test_stream_unsent_bytes_server, + }, + { + .name = "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes", + .run_client = test_seqpacket_unsent_bytes_client, + .run_server = test_seqpacket_unsent_bytes_server, + }, {}, };