Commit 3a70a645 authored by David Ahern's avatar David Ahern Committed by Jakub Kicinski

selftests: Move device validation in nettest

Later patch adds support for switching network namespaces before
running client, server or both. Device validations need to be
done after the network namespace switch, so add a helper to do it
and invoke in server and client code versus inline with argument
parsing. Move related argument checks as well.
Signed-off-by: default avatarDavid Ahern <dsahern@gmail.com>
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent c0103729
...@@ -84,6 +84,7 @@ struct sock_args { ...@@ -84,6 +84,7 @@ struct sock_args {
unsigned int prefix_len; unsigned int prefix_len;
/* expected addresses and device index for connection */ /* expected addresses and device index for connection */
const char *expected_dev;
int expected_ifindex; int expected_ifindex;
/* local address */ /* local address */
...@@ -522,6 +523,33 @@ static int str_to_uint(const char *str, int min, int max, unsigned int *value) ...@@ -522,6 +523,33 @@ static int str_to_uint(const char *str, int min, int max, unsigned int *value)
return -1; return -1;
} }
static int resolve_devices(struct sock_args *args)
{
if (args->dev) {
args->ifindex = get_ifidx(args->dev);
if (args->ifindex < 0) {
log_error("Invalid device name\n");
return 1;
}
}
if (args->expected_dev) {
unsigned int tmp;
if (str_to_uint(args->expected_dev, 0, INT_MAX, &tmp) == 0) {
args->expected_ifindex = (int)tmp;
} else {
args->expected_ifindex = get_ifidx(args->expected_dev);
if (args->expected_ifindex < 0) {
fprintf(stderr, "Invalid expected device\n");
return 1;
}
}
}
return 0;
}
static int expected_addr_match(struct sockaddr *sa, void *expected, static int expected_addr_match(struct sockaddr *sa, void *expected,
const char *desc) const char *desc)
{ {
...@@ -1190,6 +1218,9 @@ static int do_server(struct sock_args *args) ...@@ -1190,6 +1218,9 @@ static int do_server(struct sock_args *args)
fd_set rfds; fd_set rfds;
int rc; int rc;
if (resolve_devices(args))
return 1;
if (prog_timeout) if (prog_timeout)
ptval = &timeout; ptval = &timeout;
...@@ -1375,6 +1406,16 @@ static int do_client(struct sock_args *args) ...@@ -1375,6 +1406,16 @@ static int do_client(struct sock_args *args)
return 1; return 1;
} }
if (resolve_devices(args))
return 1;
if ((args->use_setsockopt || args->use_cmsg) && !args->ifindex) {
fprintf(stderr, "Device binding not specified\n");
return 1;
}
if (args->use_setsockopt || args->use_cmsg)
args->dev = NULL;
switch (args->version) { switch (args->version) {
case AF_INET: case AF_INET:
sin.sin_port = htons(args->port); sin.sin_port = htons(args->port);
...@@ -1703,11 +1744,6 @@ int main(int argc, char *argv[]) ...@@ -1703,11 +1744,6 @@ int main(int argc, char *argv[])
break; break;
case 'd': case 'd':
args.dev = optarg; args.dev = optarg;
args.ifindex = get_ifidx(optarg);
if (args.ifindex < 0) {
fprintf(stderr, "Invalid device name\n");
return 1;
}
break; break;
case 'i': case 'i':
interactive = 1; interactive = 1;
...@@ -1738,16 +1774,7 @@ int main(int argc, char *argv[]) ...@@ -1738,16 +1774,7 @@ int main(int argc, char *argv[])
break; break;
case '2': case '2':
if (str_to_uint(optarg, 0, INT_MAX, &tmp) == 0) { args.expected_dev = optarg;
args.expected_ifindex = (int)tmp;
} else {
args.expected_ifindex = get_ifidx(optarg);
if (args.expected_ifindex < 0) {
fprintf(stderr,
"Invalid expected device\n");
return 1;
}
}
break; break;
case 'q': case 'q':
quiet = 1; quiet = 1;
...@@ -1769,13 +1796,6 @@ int main(int argc, char *argv[]) ...@@ -1769,13 +1796,6 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if ((args.use_setsockopt || args.use_cmsg) && !args.ifindex) {
fprintf(stderr, "Device binding not specified\n");
return 1;
}
if (args.use_setsockopt || args.use_cmsg)
args.dev = NULL;
if (iter == 0) { if (iter == 0) {
fprintf(stderr, "Invalid number of messages to send\n"); fprintf(stderr, "Invalid number of messages to send\n");
return 1; return 1;
......
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