From mboxrd@z Thu Jan 1 00:00:00 1970 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=-7.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS 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 77089C282C8 for ; Mon, 28 Jan 2019 16:38:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3B5A520811 for ; Mon, 28 Jan 2019 16:38:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="N950Pw1Y" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390019AbfA1QiY (ORCPT ); Mon, 28 Jan 2019 11:38:24 -0500 Received: from mail-pl1-f196.google.com ([209.85.214.196]:36878 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728354AbfA1QiU (ORCPT ); Mon, 28 Jan 2019 11:38:20 -0500 Received: by mail-pl1-f196.google.com with SMTP id b5so7967448plr.4 for ; Mon, 28 Jan 2019 08:38:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=PdHE3lpW+YlKWf37LyXa7QqiC6epqB+KlBE57O36Lpc=; b=N950Pw1Y5/hdsda4Fuz+ns36nVYsyoAnpySAwWeL7WnrIj2aq+Mc/5lloRh0BwMZIW Fg/eTFAqvvIGA2///qT80k14U8PxA3G3Kom9OPLe5mjp6UZQlevRfLTLLDGTVAjk2ovi Ee6VYPToWJUTlf/vf/yitffPUIz8272GLKeUvEXByi3Nkt1uQHEspTQzDzNqzdsL6yZe vku5W0A1djlPAV7xeUP4oHEFTZmYs5IrHScI560+rXipYn+wKaH4ydKKkMpV7ZGuKQ1U liS2BQOnAL6dpOiZaF58cYQ/DMx4nupzfvSU4jVjD48ZqV6+e4oz3dPvUzsCSj0dVj3g bNfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=PdHE3lpW+YlKWf37LyXa7QqiC6epqB+KlBE57O36Lpc=; b=Zby6zWOmdCxoTnpQmtCuoNRHEXYQhtpQlCv5DIqqgDgBpjuGPEThsimBPza0zataQA 60TY0epSEV0AM2sBSJMmCTBEOw23aUlyPkSYqbuR7ExA71h0qsvMQUr2jusTxfkKIYs2 TJUM4YVhN9wBwAe2FdqVLLR6Y9avTd2mJGhddIWIqP2hMIcUQJ4FcOIShGedq4Mdd553 p7OBtWz+6fbn5vf3Dc0Iw4WdoXZ5YiM7NygHnNe2nWjoItOt6XeK9nGqVqauzAV3P6XS otno0AKRzcUR0qKpSkgHcff07wG6Fw2V+lLswKqs0Xf4jKwFtu2Fo58KB1UKh5IbPjE7 GEqQ== X-Gm-Message-State: AJcUukeLUIJw3/z4mFdVnG77Gf9kuoO9eypEcACwPeA5TyQIGvSWL2wB WxZpXBgGSxZ7G/YoSJ7OCSU= X-Google-Smtp-Source: ALg8bN6cboYwlTT5+P4khlITbcY9ccnOmD38M+fPQaHUSJZo6wPC56CGwTBFj3oDxBKjNibsa4zt2A== X-Received: by 2002:a17:902:ba8b:: with SMTP id k11mr22371207pls.177.1548693499467; Mon, 28 Jan 2019 08:38:19 -0800 (PST) Received: from ?IPv6:2601:282:800:fd80:6cf4:7f15:5073:c792? ([2601:282:800:fd80:6cf4:7f15:5073:c792]) by smtp.googlemail.com with ESMTPSA id a195sm66359836pfa.7.2019.01.28.08.38.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jan 2019 08:38:18 -0800 (PST) Subject: Re: [PATCH iproute2-next] netns: add subcommand to attach an existing network namespace To: Matteo Croce , netdev@vger.kernel.org Cc: Stephen Hemminger References: <20190124155053.7795-1-mcroce@redhat.com> From: David Ahern Message-ID: Date: Mon, 28 Jan 2019 09:38:16 -0700 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <20190124155053.7795-1-mcroce@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On 1/24/19 8:50 AM, Matteo Croce wrote: > 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 > --- > ip/ipnetns.c | 84 +++++++++++++++++++++++++++++++++++++++++++++ > man/man8/ip-netns.8 | 10 ++++++ > 2 files changed, 94 insertions(+) > > diff --git a/ip/ipnetns.c b/ip/ipnetns.c > index 03879b49..86b1a36b 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"); > @@ -811,6 +812,86 @@ static int netns_monitor(int argc, char **argv) > return 0; > } > > +static int netns_attach(int argc, char **argv) > +{ > + /* This function bind mounts an existing network namespace to a > + * well known location in the filesystem based on the name provided. > + * If everything succeeds, the result is the same as netns_add. > + * > + * 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 users. > + */ > + char netns_path[PATH_MAX], proc_path[PATH_MAX]; > + const char *name; > + int fd; > + pid_t pid; > + int made_netns_run_dir_mount = 0; > + > + if (argc < 2) { > + fprintf(stderr, "No netns name and PID specified\n"); > + return -1; > + } > + name = argv[0]; > + > + if (get_s32(&pid, argv[1], 0) || !pid) { > + fprintf(stderr, "Invalid PID: %s\n", argv[1]); > + return -1; > + } > + > + snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name); > + > + if (create_netns_dir()) > + return -1; > + > + /* Make it possible for network namespace mounts to propagate between > + * mount namespaces. This makes it likely that a unmounting a network > + * namespace file in one namespace will unmount the network namespace > + * file in all namespaces allowing the network namespace to be freed > + * sooner. > + */ > + while (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL)) { > + /* Fail unless we need to make the mount point */ > + if (errno != EINVAL || made_netns_run_dir_mount) { > + fprintf(stderr, "mount --make-shared %s failed: %s\n", > + NETNS_RUN_DIR, strerror(errno)); > + return -1; > + } > + > + /* Upgrade NETNS_RUN_DIR to a mount point */ > + if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND | MS_REC, NULL)) { > + fprintf(stderr, "mount --bind %s %s failed: %s\n", > + NETNS_RUN_DIR, NETNS_RUN_DIR, strerror(errno)); > + return -1; > + } > + made_netns_run_dir_mount = 1; > + } > + > + /* Create the filesystem state */ > + fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0); > + if (fd < 0) { > + fprintf(stderr, "Cannot create namespace file \"%s\": %s\n", > + netns_path, strerror(errno)); > + return -1; > + } > + close(fd); > + > + snprintf(proc_path, sizeof(proc_path), "/proc/%d/ns/net", pid); > + > + /* Bind the netns last so I can watch for it */ > + 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: > + if (unlink(netns_path) < 0) > + fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n", > + netns_path, strerror(errno)); > + return -1; > +} > + Rather than duplicate netns_add, refactor it for use by both attach and add.