Commit e18c7092 authored by Arseniy Krasnov's avatar Arseniy Krasnov Committed by Jakub Kicinski

vsock/test: add '--peer-port' input argument

Implement port for given CID as input argument instead of using
hardcoded value '1234'. This allows to run different test instances
on a single CID. Port argument is not required parameter and if it is
not set, then default value will be '1234' - thus we preserve previous
behaviour.
Signed-off-by: default avatarArseniy Krasnov <avkrasnov@salutedevices.com>
Reviewed-by: default avatarStefano Garzarella <sgarzare@redhat.com>
Acked-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Link: https://lore.kernel.org/r/20240123072750.4084181-1-avkrasnov@salutedevices.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 16c595a5
...@@ -33,8 +33,7 @@ void init_signals(void) ...@@ -33,8 +33,7 @@ void init_signals(void)
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
} }
/* Parse a CID in string representation */ static unsigned int parse_uint(const char *str, const char *err_str)
unsigned int parse_cid(const char *str)
{ {
char *endptr = NULL; char *endptr = NULL;
unsigned long n; unsigned long n;
...@@ -42,12 +41,24 @@ unsigned int parse_cid(const char *str) ...@@ -42,12 +41,24 @@ unsigned int parse_cid(const char *str)
errno = 0; errno = 0;
n = strtoul(str, &endptr, 10); n = strtoul(str, &endptr, 10);
if (errno || *endptr != '\0') { if (errno || *endptr != '\0') {
fprintf(stderr, "malformed CID \"%s\"\n", str); fprintf(stderr, "malformed %s \"%s\"\n", err_str, str);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return n; return n;
} }
/* Parse a CID in string representation */
unsigned int parse_cid(const char *str)
{
return parse_uint(str, "CID");
}
/* Parse a port in string representation */
unsigned int parse_port(const char *str)
{
return parse_uint(str, "port");
}
/* Wait for the remote to close the connection */ /* Wait for the remote to close the connection */
void vsock_wait_remote_close(int fd) void vsock_wait_remote_close(int fd)
{ {
......
...@@ -12,10 +12,13 @@ enum test_mode { ...@@ -12,10 +12,13 @@ enum test_mode {
TEST_MODE_SERVER TEST_MODE_SERVER
}; };
#define DEFAULT_PEER_PORT 1234
/* Test runner options */ /* Test runner options */
struct test_opts { struct test_opts {
enum test_mode mode; enum test_mode mode;
unsigned int peer_cid; unsigned int peer_cid;
unsigned int peer_port;
}; };
/* A test case definition. Test functions must print failures to stderr and /* A test case definition. Test functions must print failures to stderr and
...@@ -35,6 +38,7 @@ struct test_case { ...@@ -35,6 +38,7 @@ struct test_case {
void init_signals(void); void init_signals(void);
unsigned int parse_cid(const char *str); unsigned int parse_cid(const char *str);
unsigned int parse_port(const char *str);
int vsock_stream_connect(unsigned int cid, unsigned int port); int vsock_stream_connect(unsigned int cid, unsigned int port);
int vsock_bind_connect(unsigned int cid, unsigned int port, int vsock_bind_connect(unsigned int cid, unsigned int port,
unsigned int bind_port, int type); unsigned int bind_port, int type);
......
...@@ -342,7 +342,7 @@ static void test_listen_socket_server(const struct test_opts *opts) ...@@ -342,7 +342,7 @@ static void test_listen_socket_server(const struct test_opts *opts)
} addr = { } addr = {
.svm = { .svm = {
.svm_family = AF_VSOCK, .svm_family = AF_VSOCK,
.svm_port = 1234, .svm_port = opts->peer_port,
.svm_cid = VMADDR_CID_ANY, .svm_cid = VMADDR_CID_ANY,
}, },
}; };
...@@ -378,7 +378,7 @@ static void test_connect_client(const struct test_opts *opts) ...@@ -378,7 +378,7 @@ static void test_connect_client(const struct test_opts *opts)
LIST_HEAD(sockets); LIST_HEAD(sockets);
struct vsock_stat *st; struct vsock_stat *st;
fd = vsock_stream_connect(opts->peer_cid, 1234); fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
if (fd < 0) { if (fd < 0) {
perror("connect"); perror("connect");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -403,7 +403,7 @@ static void test_connect_server(const struct test_opts *opts) ...@@ -403,7 +403,7 @@ static void test_connect_server(const struct test_opts *opts)
LIST_HEAD(sockets); LIST_HEAD(sockets);
int client_fd; int client_fd;
client_fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL); client_fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
if (client_fd < 0) { if (client_fd < 0) {
perror("accept"); perror("accept");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -461,6 +461,11 @@ static const struct option longopts[] = { ...@@ -461,6 +461,11 @@ static const struct option longopts[] = {
.has_arg = required_argument, .has_arg = required_argument,
.val = 'p', .val = 'p',
}, },
{
.name = "peer-port",
.has_arg = required_argument,
.val = 'q',
},
{ {
.name = "list", .name = "list",
.has_arg = no_argument, .has_arg = no_argument,
...@@ -481,7 +486,7 @@ static const struct option longopts[] = { ...@@ -481,7 +486,7 @@ static const struct option longopts[] = {
static void usage(void) static void usage(void)
{ {
fprintf(stderr, "Usage: vsock_diag_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--list] [--skip=<test_id>]\n" fprintf(stderr, "Usage: vsock_diag_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--peer-port=<port>] [--list] [--skip=<test_id>]\n"
"\n" "\n"
" Server: vsock_diag_test --control-port=1234 --mode=server --peer-cid=3\n" " Server: vsock_diag_test --control-port=1234 --mode=server --peer-cid=3\n"
" Client: vsock_diag_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n" " Client: vsock_diag_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
...@@ -503,9 +508,11 @@ static void usage(void) ...@@ -503,9 +508,11 @@ static void usage(void)
" --control-port <port> Server port to listen on/connect to\n" " --control-port <port> Server port to listen on/connect to\n"
" --mode client|server Server or client mode\n" " --mode client|server Server or client mode\n"
" --peer-cid <cid> CID of the other side\n" " --peer-cid <cid> CID of the other side\n"
" --peer-port <port> AF_VSOCK port used for the test [default: %d]\n"
" --list List of tests that will be executed\n" " --list List of tests that will be executed\n"
" --skip <test_id> Test ID to skip;\n" " --skip <test_id> Test ID to skip;\n"
" use multiple --skip options to skip more tests\n" " use multiple --skip options to skip more tests\n",
DEFAULT_PEER_PORT
); );
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
...@@ -517,6 +524,7 @@ int main(int argc, char **argv) ...@@ -517,6 +524,7 @@ int main(int argc, char **argv)
struct test_opts opts = { struct test_opts opts = {
.mode = TEST_MODE_UNSET, .mode = TEST_MODE_UNSET,
.peer_cid = VMADDR_CID_ANY, .peer_cid = VMADDR_CID_ANY,
.peer_port = DEFAULT_PEER_PORT,
}; };
init_signals(); init_signals();
...@@ -544,6 +552,9 @@ int main(int argc, char **argv) ...@@ -544,6 +552,9 @@ int main(int argc, char **argv)
case 'p': case 'p':
opts.peer_cid = parse_cid(optarg); opts.peer_cid = parse_cid(optarg);
break; break;
case 'q':
opts.peer_port = parse_port(optarg);
break;
case 'P': case 'P':
control_port = optarg; control_port = optarg;
break; break;
......
This diff is collapsed.
...@@ -152,9 +152,9 @@ static void test_client(const struct test_opts *opts, ...@@ -152,9 +152,9 @@ static void test_client(const struct test_opts *opts,
int fd; int fd;
if (sock_seqpacket) if (sock_seqpacket)
fd = vsock_seqpacket_connect(opts->peer_cid, 1234); fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
else else
fd = vsock_stream_connect(opts->peer_cid, 1234); fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
if (fd < 0) { if (fd < 0) {
perror("connect"); perror("connect");
...@@ -248,9 +248,9 @@ static void test_server(const struct test_opts *opts, ...@@ -248,9 +248,9 @@ static void test_server(const struct test_opts *opts,
int fd; int fd;
if (sock_seqpacket) if (sock_seqpacket)
fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL); fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
else else
fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL); fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
if (fd < 0) { if (fd < 0) {
perror("accept"); perror("accept");
...@@ -323,7 +323,7 @@ void test_stream_msgzcopy_empty_errq_client(const struct test_opts *opts) ...@@ -323,7 +323,7 @@ void test_stream_msgzcopy_empty_errq_client(const struct test_opts *opts)
ssize_t res; ssize_t res;
int fd; int fd;
fd = vsock_stream_connect(opts->peer_cid, 1234); fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
if (fd < 0) { if (fd < 0) {
perror("connect"); perror("connect");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -347,7 +347,7 @@ void test_stream_msgzcopy_empty_errq_server(const struct test_opts *opts) ...@@ -347,7 +347,7 @@ void test_stream_msgzcopy_empty_errq_server(const struct test_opts *opts)
{ {
int fd; int fd;
fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL); fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
if (fd < 0) { if (fd < 0) {
perror("accept"); perror("accept");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
......
...@@ -66,7 +66,7 @@ static void vsock_io_uring_client(const struct test_opts *opts, ...@@ -66,7 +66,7 @@ static void vsock_io_uring_client(const struct test_opts *opts,
struct msghdr msg; struct msghdr msg;
int fd; int fd;
fd = vsock_stream_connect(opts->peer_cid, 1234); fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
if (fd < 0) { if (fd < 0) {
perror("connect"); perror("connect");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -120,7 +120,7 @@ static void vsock_io_uring_server(const struct test_opts *opts, ...@@ -120,7 +120,7 @@ static void vsock_io_uring_server(const struct test_opts *opts,
void *data; void *data;
int fd; int fd;
fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL); fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
if (fd < 0) { if (fd < 0) {
perror("accept"); perror("accept");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -247,6 +247,11 @@ static const struct option longopts[] = { ...@@ -247,6 +247,11 @@ static const struct option longopts[] = {
.has_arg = required_argument, .has_arg = required_argument,
.val = 'p', .val = 'p',
}, },
{
.name = "peer-port",
.has_arg = required_argument,
.val = 'q',
},
{ {
.name = "help", .name = "help",
.has_arg = no_argument, .has_arg = no_argument,
...@@ -257,7 +262,7 @@ static const struct option longopts[] = { ...@@ -257,7 +262,7 @@ static const struct option longopts[] = {
static void usage(void) static void usage(void)
{ {
fprintf(stderr, "Usage: vsock_uring_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid>\n" fprintf(stderr, "Usage: vsock_uring_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--peer-port=<port>]\n"
"\n" "\n"
" Server: vsock_uring_test --control-port=1234 --mode=server --peer-cid=3\n" " Server: vsock_uring_test --control-port=1234 --mode=server --peer-cid=3\n"
" Client: vsock_uring_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n" " Client: vsock_uring_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
...@@ -271,6 +276,8 @@ static void usage(void) ...@@ -271,6 +276,8 @@ static void usage(void)
" --control-port <port> Server port to listen on/connect to\n" " --control-port <port> Server port to listen on/connect to\n"
" --mode client|server Server or client mode\n" " --mode client|server Server or client mode\n"
" --peer-cid <cid> CID of the other side\n" " --peer-cid <cid> CID of the other side\n"
" --peer-port <port> AF_VSOCK port used for the test [default: %d]\n",
DEFAULT_PEER_PORT
); );
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
...@@ -282,6 +289,7 @@ int main(int argc, char **argv) ...@@ -282,6 +289,7 @@ int main(int argc, char **argv)
struct test_opts opts = { struct test_opts opts = {
.mode = TEST_MODE_UNSET, .mode = TEST_MODE_UNSET,
.peer_cid = VMADDR_CID_ANY, .peer_cid = VMADDR_CID_ANY,
.peer_port = DEFAULT_PEER_PORT,
}; };
init_signals(); init_signals();
...@@ -309,6 +317,9 @@ int main(int argc, char **argv) ...@@ -309,6 +317,9 @@ int main(int argc, char **argv)
case 'p': case 'p':
opts.peer_cid = parse_cid(optarg); opts.peer_cid = parse_cid(optarg);
break; break;
case 'q':
opts.peer_port = parse_port(optarg);
break;
case 'P': case 'P':
control_port = optarg; control_port = optarg;
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment