From nobody Fri Dec 19 15:51:20 2025 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB36D265CCC; Mon, 19 May 2025 07:07:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747638431; cv=none; b=YreT3omztG9RvmyFZfKOMT+nBwJxLz4cA1ruxND63Z5sUzOI04m1E6V7QtYUGjN6OraEnFu5Vh484hoIw/+0e8rrqrrBHL9nSem6wzSb5qpwvx7BzZpgTjHndw4cRl5pafGEgYJZjpQlpyb902pMGkcN7XYh/Q4NapF66baeGv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747638431; c=relaxed/simple; bh=eoGMF2Rt9uZ2gIcNe72NFY9+t/U8Awv10xG4k5ShIao=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UvBgdY/gk2kcRGCmnhBeFMUIX0y/jjVgIBLEYYhuHU0DW0DegaGG3Cvr/XuiPyAOmkayALU64djhOCqZoiICebG2KFs6ymPi1ZPUMic8aFMZt6DSAu6xhDMbUZDhdGl3yQk1We/ZJBXGXVyqGcCvAdOVRRcLswPcjpYtFqEhSSA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IeFwk7aO; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IeFwk7aO" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-742c27df0daso818801b3a.1; Mon, 19 May 2025 00:07:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747638429; x=1748243229; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XHAaWm/nwPVfz2tbZm/P8yaDDY/dUJ8pDKEX+ugzIbQ=; b=IeFwk7aO4wqUwyq9P67QZiI/r3wp4VOPcoMCeS82fqpZRDXwXUZy1jV/0LFJvG21MS IXS7mUWxYXKmaR1yS4u0cynlH24iBgmf15NqpHHhPbLnAat5Ar8TLcROcniuWzEQcHLc FMzUXPRBE5Fs4YCuPC92a8Plk3lNkvUdzxWn0buq5G1H6nB9t2ds9JDEmMWQB9Hnpl9G VI7Co1V9sKIIJbZ5xYMLQ5jXSqGI0cahkFCe0vI496PW6xGPd5/V+AhTe8sYQVr5EmuO JvvK/tHhYY5RFi6cTcQLR6/GgaxjCcgLdJjkR7mGo1TSG0RBNjiUrYnvdwmH9CbME2kw 1e7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747638429; x=1748243229; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XHAaWm/nwPVfz2tbZm/P8yaDDY/dUJ8pDKEX+ugzIbQ=; b=v1DqJl71IWVo6lIUwoSER8H7mtE38jwO6Z7EzBjN5S96gc7ZiSArAHM+g053alZVMS Wq1BzF5++Z0/zbqwJMumAWVhGKvItrla9z6yUGPhINiuzuojpymUWUk2yAjo+f/RiUBr npdFl2bvTr9ctgbEJ2F6tnyef7LbcpUmgKTNIyrWLPIhgCU7u29lgWX9c6C6qdtGrsVl gVzitT3UWQ0yKcwywCQ26c2O72tvnQu4p5AnHov57ALlc25N+Xi4rS1OBGnC5n9Y2Uhu BzrUWUdLZxcFO9YUOYRIHpQCEmdnyt9pIhTP2ezQMT6FL/xY0pZszUn5zK7x1F8S/kdt rmdg== X-Forwarded-Encrypted: i=1; AJvYcCVBxBm3bRgFgQPgMsHXdrowzBjGZZdzKk62HvfhaLuCyO1wRELqspI3nRqgClzjvZ3FScg=@vger.kernel.org, AJvYcCWRSUUlXRAfr/wcyVBNwnMfGkU01XethAKZ/XgZFlySQFCxrCggwlFY+pM9wpsPYxVgsNsPkD0XA1K4lWT5@vger.kernel.org X-Gm-Message-State: AOJu0YxLz6K3j5Jc7g6Uy+4PKTYfDYgV98Xd2a7ual1b4qvNVEq4wfH0 HCFl6hHcNfOsGjWfjNE5PacAKVCrvu3MHnu41HAcBB5BpV4cUxgiIh0I X-Gm-Gg: ASbGnct59Rp6+uXP6U/fmOD7Ok68jaE1sbp8J46ogqFp0NR/3PJKhb+zR8xAKSR9zK5 NF0GdHeyTPZoEJcRA1jFOLK5BgTjOREksmvjF3MZeK8pjJWkG0lJyCJKtnd/GOEJWHMh/+1M1Sx M8q/ftKpgRtEbKIA3ciTW2fUxtxpf7qm663KSYevzbGfsJFdFyYJzAMLO1urJvS4beGbOpRqwSN uIsliXDbHRH6FB2Bx2TY8G8q98dEs07JoPW/eevbBYaIWWM0XdNn+tLYwUdTk5gYYM4TviUNhfA dWlYGWaNrrRwy2ZEWHIUd+ofjvkT7SLKpQ+1zObrcF11kI5p71Q+08h/+TdapG9RQmc7d0Qjcg= = X-Google-Smtp-Source: AGHT+IENpOr+coy+nuUYSAQz1G13SXO8o5nY1ZaRb3QaUw56V4IvmR4mxX3+5YP0g6/vBT3U7sT4sw== X-Received: by 2002:a05:6a00:9455:b0:73d:ff02:8d83 with SMTP id d2e1a72fcca58-742a9786a85mr17865169b3a.3.1747638428863; Mon, 19 May 2025 00:07:08 -0700 (PDT) Received: from devant.antgroup-inc.local ([47.89.83.0]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b26eb0a0120sm5574336a12.63.2025.05.19.00.07.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 May 2025 00:07:08 -0700 (PDT) From: Xuewei Niu X-Google-Original-From: Xuewei Niu To: sgarzare@redhat.com, mst@redhat.com, fupan.lfp@antgroup.com, pabeni@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, stefanha@redhat.com Cc: virtualization@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xuewei Niu Subject: [PATCH 1/3] vsock: Add support for SIOCINQ ioctl Date: Mon, 19 May 2025 15:06:47 +0800 Message-Id: <20250519070649.3063874-2-niuxuewei.nxw@antgroup.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250519070649.3063874-1-niuxuewei.nxw@antgroup.com> References: <20250519070649.3063874-1-niuxuewei.nxw@antgroup.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch adds support for SIOCINQ ioctl, which returns the number of bytes unread in the socket. Signed-off-by: Xuewei Niu --- include/net/af_vsock.h | 2 ++ net/vmw_vsock/af_vsock.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index 9e85424c8343..6f32a2601a49 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -171,6 +171,8 @@ struct vsock_transport { =20 /* SIOCOUTQ ioctl */ ssize_t (*unsent_bytes)(struct vsock_sock *vsk); + /* SIOCINQ ioctl */ + ssize_t (*unread_bytes)(struct vsock_sock *vsk); =20 /* Shutdown. */ int (*shutdown)(struct vsock_sock *, int); diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index fc6afbc8d680..288bbfd43bc5 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -1356,6 +1356,28 @@ static int vsock_do_ioctl(struct socket *sock, unsig= ned int cmd, vsk =3D vsock_sk(sk); =20 switch (cmd) { + case SIOCINQ: { + ssize_t n_bytes; + + if (!vsk->transport || !vsk->transport->unread_bytes) { + ret =3D -EOPNOTSUPP; + break; + } + + if (sock_type_connectible(sk->sk_type) && + sk->sk_state =3D=3D TCP_LISTEN) { + ret =3D -EINVAL; + break; + } + + n_bytes =3D vsk->transport->unread_bytes(vsk); + if (n_bytes < 0) { + ret =3D n_bytes; + break; + } + ret =3D put_user(n_bytes, arg); + break; + } case SIOCOUTQ: { ssize_t n_bytes; =20 --=20 2.34.1 From nobody Fri Dec 19 15:51:20 2025 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A38E266565; Mon, 19 May 2025 07:07:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747638437; cv=none; b=c9eFrjYz3nAKn22tXVvdWnvf/kiaWAxuyy0vzvFjoUx3cmpyffExLVRJ6F61RBxY7sNpz8HH8jP5BtOTDWG4FslnTeVk26FrMzaKBO8B/6NKj38JUU5Qhw0Q7IeNQutx/We6ksUIQI8qDl+ZCuUQbieeSUrtXe0hBo/kOdkXo8s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747638437; c=relaxed/simple; bh=o4+xCFenhmp96OI75UNoIQhOcjTBx0Ax3r1tXJ88EXg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fvEAaGSRpuddFzGZfkhIfcYNoXforjyy/jE14ktBMk8ePhM2jF5HqlEzadpDxjwXSas+lvBmshpvmqcH0hPqhW9IaqkiBAbq/s1ehVAHBmOb+hkAPZ0ZAlo431Yyce7ECLfpvN2vujESUyG7E5HKO04MGI8/510G/Wr0fxKOMrU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=flZg9Klw; arc=none smtp.client-ip=209.85.216.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="flZg9Klw" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-30e57a37294so3585836a91.2; Mon, 19 May 2025 00:07:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747638434; x=1748243234; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RWPdfKtT2OXZLeQL6cLLp0+NSNfqA1Oi993as69qazA=; b=flZg9KlwllrTjV104sD42F2EwapOmoHiY5p455thg3WsMnT97TzpUuDpxFimsbVW8k CszzPG6EDiPTYO0I+InLqXpnmnfFDh1dZMlNP+30W+FwDQwMkx1lGVWqkrU6ZIwEKndv oN6ujoT1MTSFZwqb2GUEsYACAXL8DEkVQCVI6V5fB50WVpHDfqHQ3w/SAQq9RcKvIYie 8oKRGcg2Qd8X1/u/kvMKn9tIRKDgk50kOfKQI3obk1UH61N2crRFBhOKkQITCYmh47FO xYy7jC2u2J2E4oooU8OFd5nx72DQI9kM7a14jvS+bN07XxFIXa/smY1BE31C9I0yxqzz kHoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747638434; x=1748243234; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RWPdfKtT2OXZLeQL6cLLp0+NSNfqA1Oi993as69qazA=; b=roHIf8qyKVW9z0nt3y9Lz4OpaJAUgKpru91xPakhSWU0cLtN+yIt+Ez3P3EqkSc3nO nRTxuBhUMgLR/tNimUHyh2YMTufvLuSMQkbqlScLBlHWI2gRcg7nqciO1oaCH12CsKIh LkWhXru7Ts0bYTtQT9aJr/LvFtZVWujmW1lcHy9jaRIK5WR3K36HjsBrfmJPfR8aRMMF flw9ZLzhFJ3XGxEoeKayKsIzcHF6fhtLXY/F/+hQgQa1cyTqgnGrZTTD9D4gxnR9i8Pz gnGu1lS7R1C1ZPOnoMATTo7Z0HcRUpgOP9x7E5rOXL4SB80wq5Y2kDWIMUF2Y5x2y/7s cUTQ== X-Forwarded-Encrypted: i=1; AJvYcCWB4eiw2Nzq9HX6xVtLviT15fDUn3PxQwA1+zLM5rc3tk+ukk4zJVVV8uzeT6CUfbfiu3/80zDnwXpp6vjA@vger.kernel.org, AJvYcCXPk75+0bPZAJI2VeQXewHztsgs9SwteZPwpQMfbyx4iF72vKu+XLo1jGziDRmt3r4/nsI=@vger.kernel.org X-Gm-Message-State: AOJu0YwtJpwybiI2Yd25+TRdqdDWhOkJ6ckmAAXeC8066opjYAh+XPNp b4tsdrOF2CE4tnP7V22QLwrQM0soLgRoGWHUi6c19/TZJQSlpeU7EmKG X-Gm-Gg: ASbGncuG2jgMfNKgEYkEm9OMtV6IwOQJ841nkuF1ubffmJ1RecjeoSEPEx98Mu2Cr5+ p/ZqjS14zotz8+vlH82sA2fI0DABvy16GUalbcu3EHCHrv1O/1gIPeCwKQSHfKe+rLrwoq+wtV2 OvItMWwTmJEvZTbC+U9WRKilBLQwSrPvc66k/4dcdZSEMsI77XKw5DNKgmU0/wqf7kIYCSx4kjB dB3QiAXl0GYiMyMjtoC41lJI8FzefQOMNivrwfaHOJXgNBVFzLmldIRE58MvgpInOZKwCucX1l9 YWYs+PhFF5l+V/fIPqpWLnnvUheArOvAdstMmTApMJo15n1ylvOMFaOlpZLA12FROqotcUDa6g= = X-Google-Smtp-Source: AGHT+IGAGsvIArGOURC6jETHsXCGSSkCUmd1tqdQbpGNwcsLQuYAUGuTL9uAfECQEQuYsai0/RRvNA== X-Received: by 2002:a17:90b:55cc:b0:2ee:d63f:d73 with SMTP id 98e67ed59e1d1-30e830ebe05mr20262447a91.11.1747638434394; Mon, 19 May 2025 00:07:14 -0700 (PDT) Received: from devant.antgroup-inc.local ([47.89.83.0]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b26eb0a0120sm5574336a12.63.2025.05.19.00.07.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 May 2025 00:07:13 -0700 (PDT) From: Xuewei Niu X-Google-Original-From: Xuewei Niu To: sgarzare@redhat.com, mst@redhat.com, fupan.lfp@antgroup.com, pabeni@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, stefanha@redhat.com Cc: virtualization@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xuewei Niu Subject: [PATCH 2/3] vsock/virtio: Add SIOCINQ support for all virtio based transports Date: Mon, 19 May 2025 15:06:48 +0800 Message-Id: <20250519070649.3063874-3-niuxuewei.nxw@antgroup.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250519070649.3063874-1-niuxuewei.nxw@antgroup.com> References: <20250519070649.3063874-1-niuxuewei.nxw@antgroup.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The virtio_vsock_sock has a new field called bytes_unread as the return value of the SIOCINQ ioctl. Though the rx_bytes exists, we introduce a bytes_unread field to the virtio_vsock_sock struct. The reason is that it will not be updated until the skbuff is fully consumed, which causes inconsistency. The byte_unread is increased by the length of the skbuff when skbuff is enqueued, and it is decreased when dequeued. Signed-off-by: Xuewei Niu --- drivers/vhost/vsock.c | 1 + include/linux/virtio_vsock.h | 2 ++ net/vmw_vsock/virtio_transport.c | 1 + net/vmw_vsock/virtio_transport_common.c | 17 +++++++++++++++++ net/vmw_vsock/vsock_loopback.c | 1 + 5 files changed, 22 insertions(+) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 802153e23073..0f20af6e5036 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -452,6 +452,7 @@ static struct virtio_transport vhost_transport =3D { .notify_set_rcvlowat =3D virtio_transport_notify_set_rcvlowat, =20 .unsent_bytes =3D virtio_transport_unsent_bytes, + .unread_bytes =3D virtio_transport_unread_bytes, =20 .read_skb =3D virtio_transport_read_skb, }, diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 0387d64e2c66..0a7bd240113a 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -142,6 +142,7 @@ struct virtio_vsock_sock { u32 buf_alloc; struct sk_buff_head rx_queue; u32 msg_count; + size_t bytes_unread; }; =20 struct virtio_vsock_pkt_info { @@ -195,6 +196,7 @@ s64 virtio_transport_stream_has_space(struct vsock_sock= *vsk); u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk); =20 ssize_t virtio_transport_unsent_bytes(struct vsock_sock *vsk); +ssize_t virtio_transport_unread_bytes(struct vsock_sock *vsk); =20 void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume); diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index f0e48e6911fc..917881537b63 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -585,6 +585,7 @@ static struct virtio_transport virtio_transport =3D { .notify_set_rcvlowat =3D virtio_transport_notify_set_rcvlowat, =20 .unsent_bytes =3D virtio_transport_unsent_bytes, + .unread_bytes =3D virtio_transport_unread_bytes, =20 .read_skb =3D virtio_transport_read_skb, }, diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio= _transport_common.c index 7f7de6d88096..11eae88c60fc 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -632,6 +632,7 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *v= sk, free_space =3D vvs->buf_alloc - fwd_cnt_delta; low_rx_bytes =3D (vvs->rx_bytes < sock_rcvlowat(sk_vsock(vsk), 0, INT_MAX)); + vvs->bytes_unread -=3D total; =20 spin_unlock_bh(&vvs->rx_lock); =20 @@ -782,6 +783,7 @@ static int virtio_transport_seqpacket_do_dequeue(struct= vsock_sock *vsk, } =20 virtio_transport_dec_rx_pkt(vvs, pkt_len); + vvs->bytes_unread -=3D pkt_len; kfree_skb(skb); } =20 @@ -1132,6 +1134,19 @@ ssize_t virtio_transport_unsent_bytes(struct vsock_s= ock *vsk) } EXPORT_SYMBOL_GPL(virtio_transport_unsent_bytes); =20 +ssize_t virtio_transport_unread_bytes(struct vsock_sock *vsk) +{ + struct virtio_vsock_sock *vvs =3D vsk->trans; + size_t ret; + + spin_lock_bh(&vvs->rx_lock); + ret =3D vvs->bytes_unread; + spin_unlock_bh(&vvs->rx_lock); + + return ret; +} +EXPORT_SYMBOL_GPL(virtio_transport_unread_bytes); + static int virtio_transport_reset(struct vsock_sock *vsk, struct sk_buff *skb) { @@ -1365,6 +1380,8 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk, goto out; } =20 + vvs->bytes_unread +=3D len; + if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) vvs->msg_count++; =20 diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c index 6e78927a598e..13a77db2a76f 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -99,6 +99,7 @@ static struct virtio_transport loopback_transport =3D { .notify_set_rcvlowat =3D virtio_transport_notify_set_rcvlowat, =20 .unsent_bytes =3D virtio_transport_unsent_bytes, + .unread_bytes =3D virtio_transport_unread_bytes, =20 .read_skb =3D virtio_transport_read_skb, }, --=20 2.34.1 From nobody Fri Dec 19 15:51:20 2025 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3C792673BB; Mon, 19 May 2025 07:07:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747638440; cv=none; b=Eo0+6vv8tmfD6XppD/BDLEziQITAxe4ztGD2eEJoE3A+q8UW0foyugGtvA85oBJhHfBAvIZD8PMCEEuLLXtTAJ1MnrJi7ciwHWUOLW6yXQEVdu+SzG0u3RbFd5U7ay2WRkWtLQixInLXiUN4H6YBAOP1knWZ7Rxnq3LHLpYZijU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747638440; c=relaxed/simple; bh=lR0efES/TyHPp4CcTBk6Ai5txdkjK+8TK75tfxkp6xo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=caF4hgMZLxAD8L6MUzLF35R63JBevCU2D/eNEp8yfXg52RIex9fg4goiK0ziX+gZKEVPkTvEldvM59nW9xKeKntS+bZPvw2DEurz/ZIatMCFk3E90iOtwhtlSPvlwrpvvGpN8Qet8T3RXfEWMp5WGOFKeowxVfICRAztosBJqpQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=BRXnaKva; arc=none smtp.client-ip=209.85.216.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BRXnaKva" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-30ed99132abso856230a91.3; Mon, 19 May 2025 00:07:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747638438; x=1748243238; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JAhF0WSXalG9ecvsiVvvYCkMdMtzERZ5TCfWvYr6e80=; b=BRXnaKvau65pK8KVehl/e3XKszZoOs9OdD1WE0YsmllgsRngihTBJ8xrJfIiMPfnon R3Kv1qGeYrvVuAIrdCmptFHV9V0ILITaCxv/8Mhviu+SzUkkJPUMu3fQuFKg8MIoEu2y 4lM5Y+0I83Drz9lQD6wFJacd2GtSPOJtUQwIBxUlaUNStIpmvw/FwyhEBOsYoQgD+RjL 4pHZr3NFvoJdHrt7y8ibVdDImKtUUtlPwfUWRLQ/CgHNcd7QTMigBYP1COAurvIEaJPA siinFsn1opCgkUYH2yPsSqLuYQuGpWPCWJFoVMhg9vm/4v2JOfomI+EJXGjlGDbar97y xWLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747638438; x=1748243238; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JAhF0WSXalG9ecvsiVvvYCkMdMtzERZ5TCfWvYr6e80=; b=tbAPljk3JIasP2fnBwcGlFgFzilatdKdone+VDoIxieOS21vWVZabfIUvu2hX84k2o NPO4xYdrua3Ph+gCq2T1/ClNiG11uEw5shC38QMb4iLmMFuzAlzj6V4PycnwzedR4aSH VBLEVc7erblBnPRjKLfFxgRoaAMr/AqhjiQfwfpLlPwRLvBEac8m0WEbvDRt4MhqJ12V vPJ6sj1KkvpQ/UervQfBqz74Kvm77o2aPxRS0eKp/zdstgXRHZaAoDB4NXBtq6NdfE8X 2QYPgN7yO0figACroTi4Ru96VjboyBqbKhGeFcp3g8lwm2S7ovXgLNQf+rYrKORhFFVX D0Mg== X-Forwarded-Encrypted: i=1; AJvYcCU6LnNfjdZtCrJY+iPeal8w6jX+y4ehPHVFFk2UdAFfak6g95bGKswdahIlDIdGQ/prBBY=@vger.kernel.org, AJvYcCXLLjVMZX6rx6ABwBhzIljYXSmWRTkvUxRXW73czA9IYyMroA2XAtVnUsAqEiYXaI3bJP8qAT3a8MpWNew7@vger.kernel.org X-Gm-Message-State: AOJu0YxQV69teFFXmbBnroS/eom4Ovik082pCs8A6JNHT8AmC5c8j/lO hbVnlQ5QzCm62KiqSqM1Ydf+rD6DrQPmRF6FO4lRo7SLiJjK/oQe1Wil X-Gm-Gg: ASbGnctcLirESvzTuW0Sflguk3y3kQJIpwA36hEOTi9ZqH66x7ui26ZT4Z8iRbIziqH gwUPJ5AuCjEJBBsyJixq80yFcMf4PwOE19ZsHwJ44JkBgjtwhZQdhNPUkoee0IepXrCrrBfY9Wa /Ll1+dNw8yzC4h/n6ywGMUy6xr01nOoEhSEMprw6QI8iTNrqKjP5rnxfKLcxlO2pKMDiZtpFxyI lTMgMcRGr1UktQU1/43lJUArP4hpGLGvqMtQ+pDkxHw+7YqGONJoa0H6x0qywK4CmR9NrERE4KE JcoEd9bzXOmcBBy9OdUSKXANyL25Pig+uIgKtpz+ECjLRYNfTbPqvF5rWNdvEPVnm5mkJh0pug= = X-Google-Smtp-Source: AGHT+IFjhVjo/YrTw1poiwjhhCJqVvrXmUL2/CBsKqcYhLdcps3ekIFTWSzHoLUkzwFSdsZU0sLlMw== X-Received: by 2002:a17:90b:5251:b0:308:5273:4dee with SMTP id 98e67ed59e1d1-30e7d542b40mr17676836a91.15.1747638437921; Mon, 19 May 2025 00:07:17 -0700 (PDT) Received: from devant.antgroup-inc.local ([47.89.83.0]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b26eb0a0120sm5574336a12.63.2025.05.19.00.07.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 May 2025 00:07:17 -0700 (PDT) From: Xuewei Niu X-Google-Original-From: Xuewei Niu To: sgarzare@redhat.com, mst@redhat.com, fupan.lfp@antgroup.com, pabeni@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, stefanha@redhat.com Cc: virtualization@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xuewei Niu Subject: [PATCH 3/3] test/vsock: Add ioctl SIOCINQ tests Date: Mon, 19 May 2025 15:06:49 +0800 Message-Id: <20250519070649.3063874-4-niuxuewei.nxw@antgroup.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250519070649.3063874-1-niuxuewei.nxw@antgroup.com> References: <20250519070649.3063874-1-niuxuewei.nxw@antgroup.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch adds two tests for ioctl SIOCINQ for SOCK_STREAM and SOCK_SEQPACKET. The client waits for the server to send data, and checks if the return value of the SIOCINQ is the size of the data. Then, consumes the data and checks if the value is 0. Signed-off-by: Xuewei Niu --- tools/testing/vsock/vsock_test.c | 102 +++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_t= est.c index d0f6d253ac72..8b3fb88e2877 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -1282,6 +1282,78 @@ static void test_unsent_bytes_client(const struct te= st_opts *opts, int type) close(fd); } =20 +static void test_unread_bytes_server(const struct test_opts *opts, int typ= e) +{ + unsigned char buf[MSG_BUF_IOCTL_LEN]; + int client_fd; + + client_fd =3D vsock_accept(VMADDR_CID_ANY, opts->peer_port, NULL, type); + if (client_fd < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + for (int i =3D 0; i < sizeof(buf); i++) + buf[i] =3D rand() & 0xFF; + + send_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf)); + control_writeln("SENT"); + control_expectln("RECEIVED"); + + close(client_fd); +} + +static void test_unread_bytes_client(const struct test_opts *opts, int typ= e) +{ + unsigned char buf[MSG_BUF_IOCTL_LEN]; + int ret, fd; + int sock_bytes_unread; + + fd =3D vsock_connect(opts->peer_cid, opts->peer_port, type); + if (fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + control_expectln("SENT"); + // The data have come in but is not read, the expected value is + // MSG_BUF_IOCTL_LEN. + ret =3D ioctl(fd, SIOCINQ, &sock_bytes_unread); + if (ret < 0) { + if (errno =3D=3D EOPNOTSUPP) { + fprintf(stderr, + "Test skipped, SIOCINQ not supported.\n"); + goto out; + } else { + perror("ioctl"); + exit(EXIT_FAILURE); + } + } else if (ret =3D=3D 0 && sock_bytes_unread !=3D MSG_BUF_IOCTL_LEN) { + fprintf(stderr, + "Unexpected 'SIOCOUTQ' value, expected %d, got %i\n", + MSG_BUF_IOCTL_LEN, sock_bytes_unread); + exit(EXIT_FAILURE); + } + + recv_buf(fd, buf, sizeof(buf), 0, sizeof(buf)); + // The data is consumed, so the expected is 0. + ret =3D ioctl(fd, SIOCINQ, &sock_bytes_unread); + if (ret < 0) { + // Don't ignore EOPNOTSUPP since we have already checked it! + perror("ioctl"); + exit(EXIT_FAILURE); + } else if (ret =3D=3D 0 && sock_bytes_unread !=3D 0) { + fprintf(stderr, + "Unexpected 'SIOCOUTQ' value, expected 0, got %i\n", + sock_bytes_unread); + exit(EXIT_FAILURE); + } + control_writeln("RECEIVED"); + +out: + close(fd); +} + static void test_stream_unsent_bytes_client(const struct test_opts *opts) { test_unsent_bytes_client(opts, SOCK_STREAM); @@ -1302,6 +1374,26 @@ static void test_seqpacket_unsent_bytes_server(const= struct test_opts *opts) test_unsent_bytes_server(opts, SOCK_SEQPACKET); } =20 +static void test_stream_unread_bytes_client(const struct test_opts *opts) +{ + test_unread_bytes_client(opts, SOCK_STREAM); +} + +static void test_stream_unread_bytes_server(const struct test_opts *opts) +{ + test_unread_bytes_server(opts, SOCK_STREAM); +} + +static void test_seqpacket_unread_bytes_client(const struct test_opts *opt= s) +{ + test_unread_bytes_client(opts, SOCK_SEQPACKET); +} + +static void test_seqpacket_unread_bytes_server(const struct test_opts *opt= s) +{ + test_unread_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 @@ -1954,6 +2046,16 @@ static struct test_case test_cases[] =3D { .run_client =3D test_seqpacket_unsent_bytes_client, .run_server =3D test_seqpacket_unsent_bytes_server, }, + { + .name =3D "SOCK_STREAM ioctl(SIOCINQ) functionality", + .run_client =3D test_stream_unread_bytes_client, + .run_server =3D test_stream_unread_bytes_server, + }, + { + .name =3D "SOCK_SEQPACKET ioctl(SIOCINQ) functionality", + .run_client =3D test_seqpacket_unread_bytes_client, + .run_server =3D test_seqpacket_unread_bytes_server, + }, { .name =3D "SOCK_STREAM leak accept queue", .run_client =3D test_stream_leak_acceptq_client, --=20 2.34.1