From patchwork Tue Jan 29 15:01:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 1032813 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43pqSx5HYlz9sDL for ; Wed, 30 Jan 2019 02:01:21 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726852AbfA2PBU (ORCPT ); Tue, 29 Jan 2019 10:01:20 -0500 Received: from mail-wm1-f54.google.com ([209.85.128.54]:56047 "EHLO mail-wm1-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725730AbfA2PBU (ORCPT ); Tue, 29 Jan 2019 10:01:20 -0500 Received: by mail-wm1-f54.google.com with SMTP id y139so18044687wmc.5 for ; Tue, 29 Jan 2019 07:01:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=shqngYpwqPJEQb2+9fdj8QItkjNVzths6K4FGgT7qfc=; b=EVA4wETubW1SCHbyG+Fhi4MYP5moEHiCS0qWmqBU6fcbzLPCaZnqhK13F7OSgq0xy/ wZw5mW6xvQBNK2kEKfOI0IieQTUZnrk42Smi8JSQVr1jNn3Xr3TAqD3Ka2UUAu9ifaAr bgPAZHUZoMvxCWb+VKOOaQ4yvUlf3Tm6RNU0Xh1J1vkoqc3CF3aT7/WZF6G/DU5fxh8U qMHdkUAkodfxgcexfGNRgI158e/kc9E3w873lrfFFfmK3SeJEFo/sA/7HDZYtlIFzKLP eOa6sFT0s7dXSxL+DbENn9VNY4b1bvezVZ5YCHI/RCkBdYjfqDkK74DLsf+ps0grhxAF LC4A== X-Gm-Message-State: AJcUuket/JkVQelFppepTRYRbX/ixuBag6cSto0mvUGM9ShbXIeI6Niw ouhSG9vrmDGT0K5C0ySkmNxWYj0unAg= X-Google-Smtp-Source: ALg8bN6UjfIgxdapbmBpwLXwMljHwIfWxLA2Ou5LR3h6Y1fY++U2SF0iwHmSrg1O6zz0ffRq6eELzA== X-Received: by 2002:a1c:3d44:: with SMTP id k65mr21337692wma.76.1548774077629; Tue, 29 Jan 2019 07:01:17 -0800 (PST) Received: from mcroce-redhat.mxp.redhat.com (nat-pool-mxp-t.redhat.com. [149.6.153.186]) by smtp.gmail.com with ESMTPSA id y14sm17348001wro.92.2019.01.29.07.01.16 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 29 Jan 2019 07:01:16 -0800 (PST) From: Matteo Croce To: netdev@vger.kernel.org Cc: David Ahern , Stephen Hemminger , Andrea Claudi Subject: [PATCH iproute2-next v2] netns: add subcommand to attach an existing network namespace Date: Tue, 29 Jan 2019 16:01:15 +0100 Message-Id: <20190129150115.19965-1-mcroce@redhat.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org ip tracks namespaces with dummy files in /var/run/netns/, but can't see namespaces created with other tools. Creating the dummy file and bind mounting the correct procfs entry will make ip aware of that namespace. Add an ip netns subcommand to automate this task. Signed-off-by: Matteo Croce Reviewed-by: Andrea Claudi Tested-by: Andrea Claudi --- ip/ipnetns.c | 62 ++++++++++++++++++++++++++++++++++----------- man/man8/ip-netns.8 | 10 ++++++++ 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/ip/ipnetns.c b/ip/ipnetns.c index 03879b49..430d8844 100644 --- a/ip/ipnetns.c +++ b/ip/ipnetns.c @@ -28,6 +28,7 @@ static int usage(void) { fprintf(stderr, "Usage: ip netns list\n"); fprintf(stderr, " ip netns add NAME\n"); + fprintf(stderr, " ip netns attach NAME PID\n"); fprintf(stderr, " ip netns set NAME NETNSID\n"); fprintf(stderr, " ip [-all] netns delete [NAME]\n"); fprintf(stderr, " ip netns identify [PID]\n"); @@ -632,24 +633,40 @@ static int create_netns_dir(void) return 0; } -static int netns_add(int argc, char **argv) +static int netns_add(int argc, char **argv, bool create) { /* This function creates a new network namespace and * a new mount namespace and bind them into a well known * location in the filesystem based on the name provided. * + * If create is true, a new namespace will be created, + * otherwise an existing one will be attached to the file. + * * The mount namespace is created so that any necessary * userspace tweaks like remounting /sys, or bind mounting - * a new /etc/resolv.conf can be shared between uers. + * a new /etc/resolv.conf can be shared between users. */ - char netns_path[PATH_MAX]; + char netns_path[PATH_MAX], proc_path[PATH_MAX]; const char *name; + pid_t pid; int fd; int made_netns_run_dir_mount = 0; - if (argc < 1) { - fprintf(stderr, "No netns name specified\n"); - return -1; + if (create) { + if (argc < 1) { + fprintf(stderr, "No netns name specified\n"); + return -1; + } + } else { + if (argc < 2) { + fprintf(stderr, "No netns name and PID specified\n"); + return -1; + } + + if (get_s32(&pid, argv[1], 0) || !pid) { + fprintf(stderr, "Invalid PID: %s\n", argv[1]); + return -1; + } } name = argv[0]; @@ -689,21 +706,33 @@ static int netns_add(int argc, char **argv) return -1; } close(fd); - if (unshare(CLONE_NEWNET) < 0) { - fprintf(stderr, "Failed to create a new network namespace \"%s\": %s\n", - name, strerror(errno)); - goto out_delete; + + if (create) { + if (unshare(CLONE_NEWNET) < 0) { + fprintf(stderr, "Failed to create a new network namespace \"%s\": %s\n", + name, strerror(errno)); + goto out_delete; + } + + strcpy(proc_path, "/proc/self/ns/net"); + } else { + snprintf(proc_path, sizeof(proc_path), "/proc/%d/ns/net", pid); } /* Bind the netns last so I can watch for it */ - if (mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL) < 0) { - fprintf(stderr, "Bind /proc/self/ns/net -> %s failed: %s\n", - netns_path, strerror(errno)); + if (mount(proc_path, netns_path, "none", MS_BIND, NULL) < 0) { + fprintf(stderr, "Bind %s -> %s failed: %s\n", + proc_path, netns_path, strerror(errno)); goto out_delete; } return 0; out_delete: - netns_delete(argc, argv); + if (create) { + netns_delete(argc, argv); + } else if (unlink(netns_path) < 0) { + fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n", + netns_path, strerror(errno)); + } return -1; } @@ -846,7 +875,7 @@ int do_netns(int argc, char **argv) return usage(); if (matches(*argv, "add") == 0) - return netns_add(argc-1, argv+1); + return netns_add(argc-1, argv+1, true); if (matches(*argv, "set") == 0) return netns_set(argc-1, argv+1); @@ -866,6 +895,9 @@ int do_netns(int argc, char **argv) if (matches(*argv, "monitor") == 0) return netns_monitor(argc-1, argv+1); + if (matches(*argv, "attach") == 0) + return netns_add(argc-1, argv+1, false); + fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv); exit(-1); } diff --git a/man/man8/ip-netns.8 b/man/man8/ip-netns.8 index d539f18b..39a10e76 100644 --- a/man/man8/ip-netns.8 +++ b/man/man8/ip-netns.8 @@ -19,6 +19,10 @@ ip-netns \- process network namespace management .B ip netns add .I NETNSNAME +.ti -8 +.B ip netns attach +.I NETNSNAME PID + .ti -8 .B ip [-all] netns del .RI "[ " NETNSNAME " ]" @@ -89,6 +93,12 @@ This command displays all of the network namespaces in /var/run/netns If NAME is available in /var/run/netns/ this command creates a new network namespace and assigns NAME. +.TP +.B ip netns attach NAME PID - create a new named network namespace +.sp +If NAME is available in /var/run/netns/ this command attaches the network +namespace of the process PID to NAME as if it were created with ip netns. + .TP .B ip [-all] netns delete [ NAME ] - delete the name of a network namespace(s) .sp