Commit 092e0ceb authored by David Ahern's avatar David Ahern Committed by Jakub Kicinski

selftests: Add options to set network namespace to nettest

Add options to specify server and client network namespace to
use before running respective functions.
Signed-off-by: default avatarSeth David Schoen <schoen@loyalty.org>
Signed-off-by: default avatarDavid Ahern <dsahern@gmail.com>
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent f2f57584
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/wait.h>
#include <linux/tcp.h> #include <linux/tcp.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <net/if.h> #include <net/if.h>
...@@ -17,6 +18,7 @@ ...@@ -17,6 +18,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h> #include <libgen.h>
#include <limits.h> #include <limits.h>
#include <sched.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -34,6 +36,8 @@ ...@@ -34,6 +36,8 @@
#define DEFAULT_PORT 12345 #define DEFAULT_PORT 12345
#define NS_PREFIX "/run/netns/"
#ifndef MAX #ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif #endif
...@@ -77,6 +81,9 @@ struct sock_args { ...@@ -77,6 +81,9 @@ struct sock_args {
const char *dev; const char *dev;
int ifindex; int ifindex;
const char *clientns;
const char *serverns;
const char *password; const char *password;
/* prefix for MD5 password */ /* prefix for MD5 password */
const char *md5_prefix_str; const char *md5_prefix_str;
...@@ -213,6 +220,27 @@ static void log_address(const char *desc, struct sockaddr *sa) ...@@ -213,6 +220,27 @@ static void log_address(const char *desc, struct sockaddr *sa)
fflush(stdout); fflush(stdout);
} }
static int switch_ns(const char *ns)
{
char path[PATH_MAX];
int fd, ret;
if (geteuid())
log_error("warning: likely need root to set netns %s!\n", ns);
snprintf(path, sizeof(path), "%s%s", NS_PREFIX, ns);
fd = open(path, 0);
if (fd < 0) {
log_err_errno("Failed to open netns path; can not switch netns");
return 1;
}
ret = setns(fd, CLONE_NEWNET);
close(fd);
return ret;
}
static int tcp_md5sig(int sd, void *addr, socklen_t alen, struct sock_args *args) static int tcp_md5sig(int sd, void *addr, socklen_t alen, struct sock_args *args)
{ {
int keylen = strlen(args->password); int keylen = strlen(args->password);
...@@ -1377,6 +1405,15 @@ static int do_server(struct sock_args *args) ...@@ -1377,6 +1405,15 @@ static int do_server(struct sock_args *args)
fd_set rfds; fd_set rfds;
int rc; int rc;
if (args->serverns) {
if (switch_ns(args->serverns)) {
log_error("Could not set server netns to %s\n",
args->serverns);
return 1;
}
log_msg("Switched server netns\n");
}
if (resolve_devices(args) || validate_addresses(args)) if (resolve_devices(args) || validate_addresses(args))
return 1; return 1;
...@@ -1565,6 +1602,15 @@ static int do_client(struct sock_args *args) ...@@ -1565,6 +1602,15 @@ static int do_client(struct sock_args *args)
return 1; return 1;
} }
if (args->clientns) {
if (switch_ns(args->clientns)) {
log_error("Could not set client netns to %s\n",
args->clientns);
return 1;
}
log_msg("Switched client netns\n");
}
if (resolve_devices(args) || validate_addresses(args)) if (resolve_devices(args) || validate_addresses(args))
return 1; return 1;
...@@ -1642,7 +1688,7 @@ static char *random_msg(int len) ...@@ -1642,7 +1688,7 @@ static char *random_msg(int len)
return m; return m;
} }
#define GETOPT_STR "sr:l:p:t:g:P:DRn:M:m:d:SCi6L:0:1:2:Fbq" #define GETOPT_STR "sr:l:p:t:g:P:DRn:M:m:d:N:O:SCi6L:0:1:2:Fbq"
static void print_usage(char *prog) static void print_usage(char *prog)
{ {
...@@ -1656,6 +1702,8 @@ static void print_usage(char *prog) ...@@ -1656,6 +1702,8 @@ static void print_usage(char *prog)
" -t timeout seconds (default: none)\n" " -t timeout seconds (default: none)\n"
"\n" "\n"
"Optional:\n" "Optional:\n"
" -N ns set client to network namespace ns (requires root)\n"
" -O ns set server to network namespace ns (requires root)\n"
" -F Restart server loop\n" " -F Restart server loop\n"
" -6 IPv6 (default is IPv4)\n" " -6 IPv6 (default is IPv4)\n"
" -P proto protocol for socket: icmp, ospf (default: none)\n" " -P proto protocol for socket: icmp, ospf (default: none)\n"
...@@ -1757,6 +1805,12 @@ int main(int argc, char *argv[]) ...@@ -1757,6 +1805,12 @@ int main(int argc, char *argv[])
case 'n': case 'n':
iter = atoi(optarg); iter = atoi(optarg);
break; break;
case 'N':
args.clientns = optarg;
break;
case 'O':
args.serverns = optarg;
break;
case 'L': case 'L':
msg = random_msg(atoi(optarg)); msg = random_msg(atoi(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