Skip to content

Commit 3b93297

Browse files
committed
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: Jakub Kicinski <kuba@kernel.org>
2 parents 819802e + 6131656 commit 3b93297

File tree

5 files changed

+137
-12
lines changed

5 files changed

+137
-12
lines changed

net/vmw_vsock/af_vsock.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,28 @@ static int vsock_do_ioctl(struct socket *sock, unsigned int cmd,
13891389
vsk = vsock_sk(sk);
13901390

13911391
switch (cmd) {
1392+
case SIOCINQ: {
1393+
ssize_t n_bytes;
1394+
1395+
if (!vsk->transport) {
1396+
ret = -EOPNOTSUPP;
1397+
break;
1398+
}
1399+
1400+
if (sock_type_connectible(sk->sk_type) &&
1401+
sk->sk_state == TCP_LISTEN) {
1402+
ret = -EINVAL;
1403+
break;
1404+
}
1405+
1406+
n_bytes = vsock_stream_has_data(vsk);
1407+
if (n_bytes < 0) {
1408+
ret = n_bytes;
1409+
break;
1410+
}
1411+
ret = put_user(n_bytes, arg);
1412+
break;
1413+
}
13921414
case SIOCOUTQ: {
13931415
ssize_t n_bytes;
13941416

net/vmw_vsock/hyperv_transport.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,26 @@ static ssize_t hvs_stream_enqueue(struct vsock_sock *vsk, struct msghdr *msg,
694694
static s64 hvs_stream_has_data(struct vsock_sock *vsk)
695695
{
696696
struct hvsock *hvs = vsk->trans;
697+
bool need_refill;
697698
s64 ret;
698699

699700
if (hvs->recv_data_len > 0)
700-
return 1;
701+
return hvs->recv_data_len;
701702

702703
switch (hvs_channel_readable_payload(hvs->chan)) {
703704
case 1:
704-
ret = 1;
705-
break;
705+
need_refill = !hvs->recv_desc;
706+
if (!need_refill)
707+
return -EIO;
708+
709+
hvs->recv_desc = hv_pkt_iter_first(hvs->chan);
710+
if (!hvs->recv_desc)
711+
return -ENOBUFS;
712+
713+
ret = hvs_update_recv_data(hvs);
714+
if (ret)
715+
return ret;
716+
return hvs->recv_data_len;
706717
case 0:
707718
vsk->peer_shutdown |= SEND_SHUTDOWN;
708719
ret = 0;

tools/testing/vsock/util.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <unistd.h>
1818
#include <assert.h>
1919
#include <sys/epoll.h>
20+
#include <sys/ioctl.h>
2021
#include <sys/mman.h>
2122
#include <linux/sockios.h>
2223

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

104-
/* Wait until transport reports no data left to be sent.
105-
* Return false if transport does not implement the unsent_bytes() callback.
105+
/* Wait until ioctl gives an expected int value.
106+
* Return false if the op is not supported.
106107
*/
107-
bool vsock_wait_sent(int fd)
108+
bool vsock_ioctl_int(int fd, unsigned long op, int expected)
108109
{
109-
int ret, sock_bytes_unsent;
110+
int actual, ret;
111+
char name[32];
112+
113+
snprintf(name, sizeof(name), "ioctl(%lu)", op);
110114

111115
timeout_begin(TIMEOUT);
112116
do {
113-
ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
117+
ret = ioctl(fd, op, &actual);
114118
if (ret < 0) {
115119
if (errno == EOPNOTSUPP)
116120
break;
117121

118-
perror("ioctl(SIOCOUTQ)");
122+
perror(name);
119123
exit(EXIT_FAILURE);
120124
}
121-
timeout_check("SIOCOUTQ");
122-
} while (sock_bytes_unsent != 0);
125+
timeout_check(name);
126+
} while (actual != expected);
123127
timeout_end();
124128

125-
return !ret;
129+
return ret >= 0;
130+
}
131+
132+
/* Wait until transport reports no data left to be sent.
133+
* Return false if transport does not implement the unsent_bytes() callback.
134+
*/
135+
bool vsock_wait_sent(int fd)
136+
{
137+
return vsock_ioctl_int(fd, SIOCOUTQ, 0);
126138
}
127139

128140
/* Create socket <type>, bind to <cid, port>.

tools/testing/vsock/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ int vsock_stream_listen(unsigned int cid, unsigned int port);
8787
int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
8888
struct sockaddr_vm *clientaddrp);
8989
void vsock_wait_remote_close(int fd);
90+
bool vsock_ioctl_int(int fd, unsigned long op, int expected);
9091
bool vsock_wait_sent(int fd);
9192
void send_buf(int fd, const void *buf, size_t len, int flags,
9293
ssize_t expected_ret);

tools/testing/vsock/vsock_test.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/time64.h>
2525
#include <pthread.h>
2626
#include <fcntl.h>
27+
#include <linux/sockios.h>
2728

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

1311+
static void test_unread_bytes_server(const struct test_opts *opts, int type)
1312+
{
1313+
unsigned char buf[MSG_BUF_IOCTL_LEN];
1314+
int client_fd;
1315+
1316+
client_fd = vsock_accept(VMADDR_CID_ANY, opts->peer_port, NULL, type);
1317+
if (client_fd < 0) {
1318+
perror("accept");
1319+
exit(EXIT_FAILURE);
1320+
}
1321+
1322+
for (int i = 0; i < sizeof(buf); i++)
1323+
buf[i] = rand() & 0xFF;
1324+
1325+
send_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf));
1326+
control_writeln("SENT");
1327+
1328+
close(client_fd);
1329+
}
1330+
1331+
static void test_unread_bytes_client(const struct test_opts *opts, int type)
1332+
{
1333+
unsigned char buf[MSG_BUF_IOCTL_LEN];
1334+
int fd;
1335+
1336+
fd = vsock_connect(opts->peer_cid, opts->peer_port, type);
1337+
if (fd < 0) {
1338+
perror("connect");
1339+
exit(EXIT_FAILURE);
1340+
}
1341+
1342+
control_expectln("SENT");
1343+
/* The data has arrived but has not been read. The expected is
1344+
* MSG_BUF_IOCTL_LEN.
1345+
*/
1346+
if (!vsock_ioctl_int(fd, SIOCINQ, MSG_BUF_IOCTL_LEN)) {
1347+
fprintf(stderr, "Test skipped, SIOCINQ not supported.\n");
1348+
goto out;
1349+
}
1350+
1351+
recv_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
1352+
/* All data has been consumed, so the expected is 0. */
1353+
vsock_ioctl_int(fd, SIOCINQ, 0);
1354+
1355+
out:
1356+
close(fd);
1357+
}
1358+
13101359
static void test_stream_unsent_bytes_client(const struct test_opts *opts)
13111360
{
13121361
test_unsent_bytes_client(opts, SOCK_STREAM);
@@ -1327,6 +1376,26 @@ static void test_seqpacket_unsent_bytes_server(const struct test_opts *opts)
13271376
test_unsent_bytes_server(opts, SOCK_SEQPACKET);
13281377
}
13291378

1379+
static void test_stream_unread_bytes_client(const struct test_opts *opts)
1380+
{
1381+
test_unread_bytes_client(opts, SOCK_STREAM);
1382+
}
1383+
1384+
static void test_stream_unread_bytes_server(const struct test_opts *opts)
1385+
{
1386+
test_unread_bytes_server(opts, SOCK_STREAM);
1387+
}
1388+
1389+
static void test_seqpacket_unread_bytes_client(const struct test_opts *opts)
1390+
{
1391+
test_unread_bytes_client(opts, SOCK_SEQPACKET);
1392+
}
1393+
1394+
static void test_seqpacket_unread_bytes_server(const struct test_opts *opts)
1395+
{
1396+
test_unread_bytes_server(opts, SOCK_SEQPACKET);
1397+
}
1398+
13301399
#define RCVLOWAT_CREDIT_UPD_BUF_SIZE (1024 * 128)
13311400
/* This define is the same as in 'include/linux/virtio_vsock.h':
13321401
* it is used to decide when to send credit update message during
@@ -2276,6 +2345,16 @@ static struct test_case test_cases[] = {
22762345
.run_client = test_stream_transport_change_client,
22772346
.run_server = test_stream_transport_change_server,
22782347
},
2348+
{
2349+
.name = "SOCK_STREAM ioctl(SIOCINQ) functionality",
2350+
.run_client = test_stream_unread_bytes_client,
2351+
.run_server = test_stream_unread_bytes_server,
2352+
},
2353+
{
2354+
.name = "SOCK_SEQPACKET ioctl(SIOCINQ) functionality",
2355+
.run_client = test_seqpacket_unread_bytes_client,
2356+
.run_server = test_seqpacket_unread_bytes_server,
2357+
},
22792358
{},
22802359
};
22812360

0 commit comments

Comments
 (0)