Skip to content

Commit 852a00c

Browse files
mmhalkuba-moo
authored andcommitted
vsock/test: Introduce vsock_bind()
Add a helper for socket()+bind(). Adapt callers. Reviewed-by: Stefano Garzarella <[email protected]> Reviewed-by: Luigi Leonardi <[email protected]> Signed-off-by: Michal Luczaj <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent aa388c7 commit 852a00c

File tree

3 files changed

+26
-49
lines changed

3 files changed

+26
-49
lines changed

tools/testing/vsock/util.c

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,33 +96,43 @@ void vsock_wait_remote_close(int fd)
9696
close(epollfd);
9797
}
9898

99-
/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
100-
int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
99+
/* Create socket <type>, bind to <cid, port> and return the file descriptor. */
100+
int vsock_bind(unsigned int cid, unsigned int port, int type)
101101
{
102-
struct sockaddr_vm sa_client = {
103-
.svm_family = AF_VSOCK,
104-
.svm_cid = VMADDR_CID_ANY,
105-
.svm_port = bind_port,
106-
};
107-
struct sockaddr_vm sa_server = {
102+
struct sockaddr_vm sa = {
108103
.svm_family = AF_VSOCK,
109104
.svm_cid = cid,
110105
.svm_port = port,
111106
};
107+
int fd;
112108

113-
int client_fd, ret;
114-
115-
client_fd = socket(AF_VSOCK, type, 0);
116-
if (client_fd < 0) {
109+
fd = socket(AF_VSOCK, type, 0);
110+
if (fd < 0) {
117111
perror("socket");
118112
exit(EXIT_FAILURE);
119113
}
120114

121-
if (bind(client_fd, (struct sockaddr *)&sa_client, sizeof(sa_client))) {
115+
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa))) {
122116
perror("bind");
123117
exit(EXIT_FAILURE);
124118
}
125119

120+
return fd;
121+
}
122+
123+
/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
124+
int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
125+
{
126+
struct sockaddr_vm sa_server = {
127+
.svm_family = AF_VSOCK,
128+
.svm_cid = cid,
129+
.svm_port = port,
130+
};
131+
132+
int client_fd, ret;
133+
134+
client_fd = vsock_bind(VMADDR_CID_ANY, bind_port, type);
135+
126136
timeout_begin(TIMEOUT);
127137
do {
128138
ret = connect(client_fd, (struct sockaddr *)&sa_server, sizeof(sa_server));
@@ -192,28 +202,9 @@ int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
192202
/* Listen on <cid, port> and return the file descriptor. */
193203
static int vsock_listen(unsigned int cid, unsigned int port, int type)
194204
{
195-
union {
196-
struct sockaddr sa;
197-
struct sockaddr_vm svm;
198-
} addr = {
199-
.svm = {
200-
.svm_family = AF_VSOCK,
201-
.svm_port = port,
202-
.svm_cid = cid,
203-
},
204-
};
205205
int fd;
206206

207-
fd = socket(AF_VSOCK, type, 0);
208-
if (fd < 0) {
209-
perror("socket");
210-
exit(EXIT_FAILURE);
211-
}
212-
213-
if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
214-
perror("bind");
215-
exit(EXIT_FAILURE);
216-
}
207+
fd = vsock_bind(cid, port, type);
217208

218209
if (listen(fd, 1) < 0) {
219210
perror("listen");

tools/testing/vsock/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ int vsock_connect(unsigned int cid, unsigned int port, int type);
4343
int vsock_accept(unsigned int cid, unsigned int port,
4444
struct sockaddr_vm *clientaddrp, int type);
4545
int vsock_stream_connect(unsigned int cid, unsigned int port);
46+
int vsock_bind(unsigned int cid, unsigned int port, int type);
4647
int vsock_bind_connect(unsigned int cid, unsigned int port,
4748
unsigned int bind_port, int type);
4849
int vsock_seqpacket_connect(unsigned int cid, unsigned int port);

tools/testing/vsock/vsock_test.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,9 @@ static void test_stream_bind_only_client(const struct test_opts *opts)
113113

114114
static void test_stream_bind_only_server(const struct test_opts *opts)
115115
{
116-
union {
117-
struct sockaddr sa;
118-
struct sockaddr_vm svm;
119-
} addr = {
120-
.svm = {
121-
.svm_family = AF_VSOCK,
122-
.svm_port = opts->peer_port,
123-
.svm_cid = VMADDR_CID_ANY,
124-
},
125-
};
126116
int fd;
127117

128-
fd = socket(AF_VSOCK, SOCK_STREAM, 0);
129-
130-
if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
131-
perror("bind");
132-
exit(EXIT_FAILURE);
133-
}
118+
fd = vsock_bind(VMADDR_CID_ANY, opts->peer_port, SOCK_STREAM);
134119

135120
/* Notify the client that the server is ready */
136121
control_writeln("BIND");

0 commit comments

Comments
 (0)