From patchwork Wed Jan 13 04:00:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Ahern X-Patchwork-Id: 12015713 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82B1DC4332B for ; Wed, 13 Jan 2021 04:01:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 50FB62312F for ; Wed, 13 Jan 2021 04:01:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726386AbhAMEBk (ORCPT ); Tue, 12 Jan 2021 23:01:40 -0500 Received: from mail.kernel.org ([198.145.29.99]:48570 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726152AbhAMEBj (ORCPT ); Tue, 12 Jan 2021 23:01:39 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2108223139; Wed, 13 Jan 2021 04:00:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1610510458; bh=XSNvpuXX/43Px+JSR9m1SXDrGyusm6n3pqBeVNn5AWo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OiSCO6G+Kih2uJH/nUxkhXsronjEyXhs9U43rcf9pDPOER1Gx8Z5x5rXJgTq5JoyG FtbfnxKxcwgSN74Rz+wZ04jsi+xMpaPBo4wsNTub85qb3fR7AkSc0oso64IDzvZ3RE sRlJLPl/dF1dfiuZ429vHtGTwong5LTbwgkb3yP7bS93iU9hxTWSgg85n3k2j+Ha1w 07NmGkXSuKqv5rX+mCx3xs1Y+HiwIThb/vU6RbAJpzVp1qNuSqxTSA9LhV078TXHqn xT2To779WFXyyD2EiRHmsWJ759QnoRSxhExD58g8gw2zQLNSPpUr2ZUYjc8C4yOyrH ya8/Se357RBLQ== From: David Ahern To: netdev@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, schoen@loyalty.org, David Ahern Subject: [PATCH net-next v3 04/13] selftests: Add options to set network namespace to nettest Date: Tue, 12 Jan 2021 21:00:31 -0700 Message-Id: <20210113040040.50813-5-dsahern@kernel.org> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210113040040.50813-1-dsahern@kernel.org> References: <20210113040040.50813-1-dsahern@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: David Ahern Add options to specify server and client network namespace to use before running respective functions. Signed-off-by: Seth David Schoen Signed-off-by: David Ahern --- 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 #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -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;