Skip to content

Commit e78e059

Browse files
mmhalPaolo Abeni
authored andcommitted
vsock/test: Introduce vsock_wait_sent() helper
Distill the virtio_vsock_sock::bytes_unsent checking loop (ioctl SIOCOUTQ) and move it to utils. Tweak the comment. Signed-off-by: Michal Luczaj <[email protected]> Reviewed-by: Stefano Garzarella <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent 5ec4086 commit e78e059

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

tools/testing/vsock/util.c

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

2122
#include "timeout.h"
2223
#include "control.h"
@@ -96,6 +97,30 @@ void vsock_wait_remote_close(int fd)
9697
close(epollfd);
9798
}
9899

100+
/* Wait until transport reports no data left to be sent.
101+
* Return false if transport does not implement the unsent_bytes() callback.
102+
*/
103+
bool vsock_wait_sent(int fd)
104+
{
105+
int ret, sock_bytes_unsent;
106+
107+
timeout_begin(TIMEOUT);
108+
do {
109+
ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
110+
if (ret < 0) {
111+
if (errno == EOPNOTSUPP)
112+
break;
113+
114+
perror("ioctl(SIOCOUTQ)");
115+
exit(EXIT_FAILURE);
116+
}
117+
timeout_check("SIOCOUTQ");
118+
} while (sock_bytes_unsent != 0);
119+
timeout_end();
120+
121+
return !ret;
122+
}
123+
99124
/* Create socket <type>, bind to <cid, port> and return the file descriptor. */
100125
int vsock_bind(unsigned int cid, unsigned int port, int type)
101126
{

tools/testing/vsock/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ int vsock_stream_listen(unsigned int cid, unsigned int port);
5454
int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
5555
struct sockaddr_vm *clientaddrp);
5656
void vsock_wait_remote_close(int fd);
57+
bool vsock_wait_sent(int fd);
5758
void send_buf(int fd, const void *buf, size_t len, int flags,
5859
ssize_t expected_ret);
5960
void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret);

tools/testing/vsock/vsock_test.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <poll.h>
2222
#include <signal.h>
2323
#include <sys/ioctl.h>
24-
#include <linux/sockios.h>
2524
#include <linux/time64.h>
2625

2726
#include "vsock_test_zerocopy.h"
@@ -1280,7 +1279,7 @@ static void test_unsent_bytes_server(const struct test_opts *opts, int type)
12801279
static void test_unsent_bytes_client(const struct test_opts *opts, int type)
12811280
{
12821281
unsigned char buf[MSG_BUF_IOCTL_LEN];
1283-
int ret, fd, sock_bytes_unsent;
1282+
int fd;
12841283

12851284
fd = vsock_connect(opts->peer_cid, opts->peer_port, type);
12861285
if (fd < 0) {
@@ -1297,22 +1296,12 @@ static void test_unsent_bytes_client(const struct test_opts *opts, int type)
12971296
/* SIOCOUTQ isn't guaranteed to instantly track sent data. Even though
12981297
* the "RECEIVED" message means that the other side has received the
12991298
* data, there can be a delay in our kernel before updating the "unsent
1300-
* bytes" counter. Repeat SIOCOUTQ until it returns 0.
1299+
* bytes" counter. vsock_wait_sent() will repeat SIOCOUTQ until it
1300+
* returns 0.
13011301
*/
1302-
timeout_begin(TIMEOUT);
1303-
do {
1304-
ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
1305-
if (ret < 0) {
1306-
if (errno == EOPNOTSUPP) {
1307-
fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
1308-
break;
1309-
}
1310-
perror("ioctl");
1311-
exit(EXIT_FAILURE);
1312-
}
1313-
timeout_check("SIOCOUTQ");
1314-
} while (sock_bytes_unsent != 0);
1315-
timeout_end();
1302+
if (!vsock_wait_sent(fd))
1303+
fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
1304+
13161305
close(fd);
13171306
}
13181307

0 commit comments

Comments
 (0)