Commit 3b932976 authored by Jakub Kicinski's avatar Jakub Kicinski
Browse files

Merge branch 'vsock-introduce-siocinq-ioctl-support'

Xuewei Niu says:

====================
vsock: Introduce SIOCINQ ioctl support

Introduce SIOCINQ ioctl support for vsock, indicating the length of unread
bytes.

Similar with SIOCOUTQ ioctl, the information is transport-dependent.

The first patch adds SIOCINQ ioctl support in AF_VSOCK.

Thanks to @dexuan, the second patch is to fix the issue where hyper-v
`hvs_stream_has_data()` doesn't return the readable bytes.

The third patch wraps the ioctl into `ioctl_int()`, which implements a
retry mechanism to prevent immediate failure.

The last one adds two test cases to check the functionality. The changes
have been tested, and the results are as expected.
====================

Link: https://patch.msgid.link/20250708-siocinq-v6-0-3775f9a9e359@antgroup.com


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents 819802e2 61316568
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -1389,6 +1389,28 @@ static int vsock_do_ioctl(struct socket *sock, unsigned int cmd,
	vsk = vsock_sk(sk);

	switch (cmd) {
	case SIOCINQ: {
		ssize_t n_bytes;

		if (!vsk->transport) {
			ret = -EOPNOTSUPP;
			break;
		}

		if (sock_type_connectible(sk->sk_type) &&
		    sk->sk_state == TCP_LISTEN) {
			ret = -EINVAL;
			break;
		}

		n_bytes = vsock_stream_has_data(vsk);
		if (n_bytes < 0) {
			ret = n_bytes;
			break;
		}
		ret = put_user(n_bytes, arg);
		break;
	}
	case SIOCOUTQ: {
		ssize_t n_bytes;

+14 −3
Original line number Diff line number Diff line
@@ -694,15 +694,26 @@ static ssize_t hvs_stream_enqueue(struct vsock_sock *vsk, struct msghdr *msg,
static s64 hvs_stream_has_data(struct vsock_sock *vsk)
{
	struct hvsock *hvs = vsk->trans;
	bool need_refill;
	s64 ret;

	if (hvs->recv_data_len > 0)
		return 1;
		return hvs->recv_data_len;

	switch (hvs_channel_readable_payload(hvs->chan)) {
	case 1:
		ret = 1;
		break;
		need_refill = !hvs->recv_desc;
		if (!need_refill)
			return -EIO;

		hvs->recv_desc = hv_pkt_iter_first(hvs->chan);
		if (!hvs->recv_desc)
			return -ENOBUFS;

		ret = hvs_update_recv_data(hvs);
		if (ret)
			return ret;
		return hvs->recv_data_len;
	case 0:
		vsk->peer_shutdown |= SEND_SHUTDOWN;
		ret = 0;
+21 −9
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include <unistd.h>
#include <assert.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/sockios.h>

@@ -101,28 +102,39 @@ void vsock_wait_remote_close(int fd)
	close(epollfd);
}

/* Wait until transport reports no data left to be sent.
 * Return false if transport does not implement the unsent_bytes() callback.
/* Wait until ioctl gives an expected int value.
 * Return false if the op is not supported.
 */
bool vsock_wait_sent(int fd)
bool vsock_ioctl_int(int fd, unsigned long op, int expected)
{
	int ret, sock_bytes_unsent;
	int actual, ret;
	char name[32];

	snprintf(name, sizeof(name), "ioctl(%lu)", op);

	timeout_begin(TIMEOUT);
	do {
		ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
		ret = ioctl(fd, op, &actual);
		if (ret < 0) {
			if (errno == EOPNOTSUPP)
				break;

			perror("ioctl(SIOCOUTQ)");
			perror(name);
			exit(EXIT_FAILURE);
		}
		timeout_check("SIOCOUTQ");
	} while (sock_bytes_unsent != 0);
		timeout_check(name);
	} while (actual != expected);
	timeout_end();

	return !ret;
	return ret >= 0;
}

/* Wait until transport reports no data left to be sent.
 * Return false if transport does not implement the unsent_bytes() callback.
 */
bool vsock_wait_sent(int fd)
{
	return vsock_ioctl_int(fd, SIOCOUTQ, 0);
}

/* Create socket <type>, bind to <cid, port>.
+1 −0
Original line number Diff line number Diff line
@@ -87,6 +87,7 @@ int vsock_stream_listen(unsigned int cid, unsigned int port);
int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
			   struct sockaddr_vm *clientaddrp);
void vsock_wait_remote_close(int fd);
bool vsock_ioctl_int(int fd, unsigned long op, int expected);
bool vsock_wait_sent(int fd);
void send_buf(int fd, const void *buf, size_t len, int flags,
	      ssize_t expected_ret);
+79 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include <linux/time64.h>
#include <pthread.h>
#include <fcntl.h>
#include <linux/sockios.h>

#include "vsock_test_zerocopy.h"
#include "timeout.h"
@@ -1307,6 +1308,54 @@ static void test_unsent_bytes_client(const struct test_opts *opts, int type)
	close(fd);
}

static void test_unread_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, opts->peer_port, NULL, type);
	if (client_fd < 0) {
		perror("accept");
		exit(EXIT_FAILURE);
	}

	for (int i = 0; i < sizeof(buf); i++)
		buf[i] = rand() & 0xFF;

	send_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf));
	control_writeln("SENT");

	close(client_fd);
}

static void test_unread_bytes_client(const struct test_opts *opts, int type)
{
	unsigned char buf[MSG_BUF_IOCTL_LEN];
	int fd;

	fd = vsock_connect(opts->peer_cid, opts->peer_port, type);
	if (fd < 0) {
		perror("connect");
		exit(EXIT_FAILURE);
	}

	control_expectln("SENT");
	/* The data has arrived but has not been read. The expected is
	 * MSG_BUF_IOCTL_LEN.
	 */
	if (!vsock_ioctl_int(fd, SIOCINQ, MSG_BUF_IOCTL_LEN)) {
		fprintf(stderr, "Test skipped, SIOCINQ not supported.\n");
		goto out;
	}

	recv_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
	/* All data has been consumed, so the expected is 0. */
	vsock_ioctl_int(fd, SIOCINQ, 0);

out:
	close(fd);
}

static void test_stream_unsent_bytes_client(const struct test_opts *opts)
{
	test_unsent_bytes_client(opts, SOCK_STREAM);
@@ -1327,6 +1376,26 @@ static void test_seqpacket_unsent_bytes_server(const struct test_opts *opts)
	test_unsent_bytes_server(opts, SOCK_SEQPACKET);
}

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 *opts)
{
	test_unread_bytes_client(opts, SOCK_SEQPACKET);
}

static void test_seqpacket_unread_bytes_server(const struct test_opts *opts)
{
	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
@@ -2276,6 +2345,16 @@ static struct test_case test_cases[] = {
		.run_client = test_stream_transport_change_client,
		.run_server = test_stream_transport_change_server,
	},
	{
		.name = "SOCK_STREAM ioctl(SIOCINQ) functionality",
		.run_client = test_stream_unread_bytes_client,
		.run_server = test_stream_unread_bytes_server,
	},
	{
		.name = "SOCK_SEQPACKET ioctl(SIOCINQ) functionality",
		.run_client = test_seqpacket_unread_bytes_client,
		.run_server = test_seqpacket_unread_bytes_server,
	},
	{},
};