netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Ahern <dsahern@kernel.org>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, kuba@kernel.org, schoen@loyalty.org,
	David Ahern <dsahern@gmail.com>
Subject: [PATCH net-next v4 04/13] selftests: Add options to set network namespace to nettest
Date: Wed, 13 Jan 2021 20:09:40 -0700	[thread overview]
Message-ID: <20210114030949.54425-5-dsahern@kernel.org> (raw)
In-Reply-To: <20210114030949.54425-1-dsahern@kernel.org>

From: David Ahern <dsahern@gmail.com>

Add options to specify server and client network namespace to
use before running respective functions.

Signed-off-by: Seth David Schoen <schoen@loyalty.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
---
 tools/testing/selftests/net/nettest.c | 56 ++++++++++++++++++++++++++-
 1 file changed, 55 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/net/nettest.c b/tools/testing/selftests/net/nettest.c
index 3b083fad3577..cc9635b6461f 100644
--- a/tools/testing/selftests/net/nettest.c
+++ b/tools/testing/selftests/net/nettest.c
@@ -9,6 +9,7 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
+#include <sys/wait.h>
 #include <linux/tcp.h>
 #include <arpa/inet.h>
 #include <net/if.h>
@@ -17,6 +18,7 @@
 #include <fcntl.h>
 #include <libgen.h>
 #include <limits.h>
+#include <sched.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -34,6 +36,8 @@
 
 #define DEFAULT_PORT 12345
 
+#define NS_PREFIX "/run/netns/"
+
 #ifndef MAX
 #define MAX(a, b)  ((a) > (b) ? (a) : (b))
 #endif
@@ -77,6 +81,9 @@ struct sock_args {
 	const char *dev;
 	int ifindex;
 
+	const char *clientns;
+	const char *serverns;
+
 	const char *password;
 	/* prefix for MD5 password */
 	const char *md5_prefix_str;
@@ -213,6 +220,27 @@ static void log_address(const char *desc, struct sockaddr *sa)
 	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)
 {
 	int keylen = strlen(args->password);
@@ -1377,6 +1405,15 @@ static int do_server(struct sock_args *args)
 	fd_set rfds;
 	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))
 		return 1;
 
@@ -1565,6 +1602,15 @@ static int do_client(struct sock_args *args)
 		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))
 		return 1;
 
@@ -1642,7 +1688,7 @@ static char *random_msg(int len)
 	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)
 {
@@ -1656,6 +1702,8 @@ static void print_usage(char *prog)
 	"    -t            timeout seconds (default: none)\n"
 	"\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"
 	"    -6            IPv6 (default is IPv4)\n"
 	"    -P proto      protocol for socket: icmp, ospf (default: none)\n"
@@ -1757,6 +1805,12 @@ int main(int argc, char *argv[])
 		case 'n':
 			iter = atoi(optarg);
 			break;
+		case 'N':
+			args.clientns = optarg;
+			break;
+		case 'O':
+			args.serverns = optarg;
+			break;
 		case 'L':
 			msg = random_msg(atoi(optarg));
 			break;
-- 
2.24.3 (Apple Git-128)


  parent reply	other threads:[~2021-01-14  3:10 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-14  3:09 [PATCH net-next v4 00/13] selftests: Updates to allow single instance of nettest for client and server David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 01/13] selftests: Move device validation in nettest David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 02/13] selftests: Move convert_addr up " David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 03/13] selftests: Move address validation " David Ahern
2021-01-14  3:09 ` David Ahern [this message]
2021-01-14  3:09 ` [PATCH net-next v4 05/13] selftests: Add support to nettest to run both client and server David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 06/13] selftests: Use separate stdout and stderr buffers in nettest David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 07/13] selftests: Add missing newline in nettest error messages David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 08/13] selftests: Make address validation apply only to client mode David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 09/13] selftests: Consistently specify address for MD5 protection David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 10/13] selftests: Add new option for client-side passwords David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 11/13] selftests: Add separate options for server device bindings David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 12/13] selftests: Remove exraneous newline in nettest David Ahern
2021-01-14  3:09 ` [PATCH net-next v4 13/13] selftests: Add separate option to nettest for address binding David Ahern
2021-01-15  0:30 ` [PATCH net-next v4 00/13] selftests: Updates to allow single instance of nettest for client and server patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210114030949.54425-5-dsahern@kernel.org \
    --to=dsahern@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dsahern@gmail.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=schoen@loyalty.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).