Skip to content

Commit 9bb8a29

Browse files
stefanhaRHdavem330
authored andcommitted
VSOCK: extract connect/accept functions from vsock_diag_test.c
Many test cases will need to connect to the server or accept incoming connections. This patch extracts these operations into utility functions that can be reused. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent df7e0e0 commit 9bb8a29

3 files changed

Lines changed: 119 additions & 76 deletions

File tree

‎tools/testing/vsock/util.c‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
#include <stdio.h>
1212
#include <stdlib.h>
1313
#include <signal.h>
14+
#include <unistd.h>
1415

1516
#include "timeout.h"
17+
#include "control.h"
1618
#include "util.h"
1719

1820
/* Install signal handlers */
@@ -41,6 +43,112 @@ unsigned int parse_cid(const char *str)
4143
return n;
4244
}
4345

46+
/* Connect to <cid, port> and return the file descriptor. */
47+
int vsock_stream_connect(unsigned int cid, unsigned int port)
48+
{
49+
union {
50+
struct sockaddr sa;
51+
struct sockaddr_vm svm;
52+
} addr = {
53+
.svm = {
54+
.svm_family = AF_VSOCK,
55+
.svm_port = port,
56+
.svm_cid = cid,
57+
},
58+
};
59+
int ret;
60+
int fd;
61+
62+
control_expectln("LISTENING");
63+
64+
fd = socket(AF_VSOCK, SOCK_STREAM, 0);
65+
66+
timeout_begin(TIMEOUT);
67+
do {
68+
ret = connect(fd, &addr.sa, sizeof(addr.svm));
69+
timeout_check("connect");
70+
} while (ret < 0 && errno == EINTR);
71+
timeout_end();
72+
73+
if (ret < 0) {
74+
int old_errno = errno;
75+
76+
close(fd);
77+
fd = -1;
78+
errno = old_errno;
79+
}
80+
return fd;
81+
}
82+
83+
/* Listen on <cid, port> and return the first incoming connection. The remote
84+
* address is stored to clientaddrp. clientaddrp may be NULL.
85+
*/
86+
int vsock_stream_accept(unsigned int cid, unsigned int port,
87+
struct sockaddr_vm *clientaddrp)
88+
{
89+
union {
90+
struct sockaddr sa;
91+
struct sockaddr_vm svm;
92+
} addr = {
93+
.svm = {
94+
.svm_family = AF_VSOCK,
95+
.svm_port = port,
96+
.svm_cid = cid,
97+
},
98+
};
99+
union {
100+
struct sockaddr sa;
101+
struct sockaddr_vm svm;
102+
} clientaddr;
103+
socklen_t clientaddr_len = sizeof(clientaddr.svm);
104+
int fd;
105+
int client_fd;
106+
int old_errno;
107+
108+
fd = socket(AF_VSOCK, SOCK_STREAM, 0);
109+
110+
if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
111+
perror("bind");
112+
exit(EXIT_FAILURE);
113+
}
114+
115+
if (listen(fd, 1) < 0) {
116+
perror("listen");
117+
exit(EXIT_FAILURE);
118+
}
119+
120+
control_writeln("LISTENING");
121+
122+
timeout_begin(TIMEOUT);
123+
do {
124+
client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
125+
timeout_check("accept");
126+
} while (client_fd < 0 && errno == EINTR);
127+
timeout_end();
128+
129+
old_errno = errno;
130+
close(fd);
131+
errno = old_errno;
132+
133+
if (client_fd < 0)
134+
return client_fd;
135+
136+
if (clientaddr_len != sizeof(clientaddr.svm)) {
137+
fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
138+
(size_t)clientaddr_len);
139+
exit(EXIT_FAILURE);
140+
}
141+
if (clientaddr.sa.sa_family != AF_VSOCK) {
142+
fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
143+
clientaddr.sa.sa_family);
144+
exit(EXIT_FAILURE);
145+
}
146+
147+
if (clientaddrp)
148+
*clientaddrp = clientaddr.svm;
149+
return client_fd;
150+
}
151+
44152
/* Run test cases. The program terminates if a failure occurs. */
45153
void run_tests(const struct test_case *test_cases,
46154
const struct test_opts *opts)

‎tools/testing/vsock/util.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#ifndef UTIL_H
33
#define UTIL_H
44

5+
#include <sys/socket.h>
6+
#include <linux/vm_sockets.h>
7+
58
/* Tests can either run as the client or the server */
69
enum test_mode {
710
TEST_MODE_UNSET,
@@ -30,6 +33,9 @@ struct test_case {
3033

3134
void init_signals(void);
3235
unsigned int parse_cid(const char *str);
36+
int vsock_stream_connect(unsigned int cid, unsigned int port);
37+
int vsock_stream_accept(unsigned int cid, unsigned int port,
38+
struct sockaddr_vm *clientaddrp);
3339
void run_tests(const struct test_case *test_cases,
3440
const struct test_opts *opts);
3541

‎tools/testing/vsock/vsock_diag_test.c‎

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
#include <string.h>
1414
#include <errno.h>
1515
#include <unistd.h>
16-
#include <sys/socket.h>
1716
#include <sys/stat.h>
1817
#include <sys/types.h>
1918
#include <linux/list.h>
2019
#include <linux/net.h>
2120
#include <linux/netlink.h>
22-
#include <linux/vm_sockets.h>
2321
#include <linux/sock_diag.h>
2422
#include <linux/vm_sockets_diag.h>
2523
#include <netinet/tcp.h>
@@ -378,33 +376,12 @@ static void test_listen_socket_server(const struct test_opts *opts)
378376

379377
static void test_connect_client(const struct test_opts *opts)
380378
{
381-
union {
382-
struct sockaddr sa;
383-
struct sockaddr_vm svm;
384-
} addr = {
385-
.svm = {
386-
.svm_family = AF_VSOCK,
387-
.svm_port = 1234,
388-
.svm_cid = opts->peer_cid,
389-
},
390-
};
391379
int fd;
392-
int ret;
393380
LIST_HEAD(sockets);
394381
struct vsock_stat *st;
395382

396-
control_expectln("LISTENING");
397-
398-
fd = socket(AF_VSOCK, SOCK_STREAM, 0);
399-
400-
timeout_begin(TIMEOUT);
401-
do {
402-
ret = connect(fd, &addr.sa, sizeof(addr.svm));
403-
timeout_check("connect");
404-
} while (ret < 0 && errno == EINTR);
405-
timeout_end();
406-
407-
if (ret < 0) {
383+
fd = vsock_stream_connect(opts->peer_cid, 1234);
384+
if (fd < 0) {
408385
perror("connect");
409386
exit(EXIT_FAILURE);
410387
}
@@ -424,74 +401,26 @@ static void test_connect_client(const struct test_opts *opts)
424401

425402
static void test_connect_server(const struct test_opts *opts)
426403
{
427-
union {
428-
struct sockaddr sa;
429-
struct sockaddr_vm svm;
430-
} addr = {
431-
.svm = {
432-
.svm_family = AF_VSOCK,
433-
.svm_port = 1234,
434-
.svm_cid = VMADDR_CID_ANY,
435-
},
436-
};
437-
union {
438-
struct sockaddr sa;
439-
struct sockaddr_vm svm;
440-
} clientaddr;
441-
socklen_t clientaddr_len = sizeof(clientaddr.svm);
442-
LIST_HEAD(sockets);
443404
struct vsock_stat *st;
444-
int fd;
405+
LIST_HEAD(sockets);
445406
int client_fd;
446407

447-
fd = socket(AF_VSOCK, SOCK_STREAM, 0);
448-
449-
if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
450-
perror("bind");
451-
exit(EXIT_FAILURE);
452-
}
453-
454-
if (listen(fd, 1) < 0) {
455-
perror("listen");
456-
exit(EXIT_FAILURE);
457-
}
458-
459-
control_writeln("LISTENING");
460-
461-
timeout_begin(TIMEOUT);
462-
do {
463-
client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
464-
timeout_check("accept");
465-
} while (client_fd < 0 && errno == EINTR);
466-
timeout_end();
467-
408+
client_fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
468409
if (client_fd < 0) {
469410
perror("accept");
470411
exit(EXIT_FAILURE);
471412
}
472-
if (clientaddr.sa.sa_family != AF_VSOCK) {
473-
fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
474-
clientaddr.sa.sa_family);
475-
exit(EXIT_FAILURE);
476-
}
477-
if (clientaddr.svm.svm_cid != opts->peer_cid) {
478-
fprintf(stderr, "expected peer CID %u from accept(2), got %u\n",
479-
opts->peer_cid, clientaddr.svm.svm_cid);
480-
exit(EXIT_FAILURE);
481-
}
482413

483414
read_vsock_stat(&sockets);
484415

485-
check_num_sockets(&sockets, 2);
486-
find_vsock_stat(&sockets, fd);
416+
check_num_sockets(&sockets, 1);
487417
st = find_vsock_stat(&sockets, client_fd);
488418
check_socket_state(st, TCP_ESTABLISHED);
489419

490420
control_writeln("DONE");
491421
control_expectln("DONE");
492422

493423
close(client_fd);
494-
close(fd);
495424
free_sock_stat(&sockets);
496425
}
497426

0 commit comments

Comments
 (0)