Skip to content

Commit 6131656

Browse files
justxueweikuba-moo
authored andcommitted
test/vsock: Add ioctl SIOCINQ tests
Add SIOCINQ ioctl tests for both SOCK_STREAM and SOCK_SEQPACKET. The client waits for the server to send data, and checks if the SIOCINQ ioctl value matches the data size. After consuming the data, the client checks if the SIOCINQ value is 0. Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com> Reviewed-by: Stefano Garzarella <sgarzare@redhat.com> Tested-by: Luigi Leonardi <leonardi@redhat.com> Reviewed-by: Luigi Leonardi <leonardi@redhat.com> Link: https://patch.msgid.link/20250708-siocinq-v6-4-3775f9a9e359@antgroup.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 53548d6 commit 6131656

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

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)