From f1c9c632e18fc027b0c6133d0046d624611465dc Mon Sep 17 00:00:00 2001 From: Stefan Junker Date: Thu, 24 Sep 2015 18:28:14 +0200 Subject: [PATCH 1/2] Revert "plugins/ptp: allow host veth to be UP" This reverts commit 231d2d5a27f3ba54219c3f0b1c8ef2c5dab4faaf. --- pkg/ip/link.go | 10 +++++++--- plugins/main/ptp/ptp.go | 4 ---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/ip/link.go b/pkg/ip/link.go index e97dcd28..3936ed9d 100644 --- a/pkg/ip/link.go +++ b/pkg/ip/link.go @@ -77,9 +77,8 @@ func RandomVethName() (string, error) { return fmt.Sprintf("veth%x", entropy), nil } -// SetupVeth creates the virtual ethernet pair and sets up the container's end in the container netns. -// Setting up the host end up has to be done in the host netns outside of this function. -// This is because moving the host veth end will cause it to be brought down automatically when it is moved to the host netns. +// SetupVeth sets up a virtual ethernet link. +// Should be in container netns. func SetupVeth(contVethName string, mtu int, hostNS *os.File) (hostVeth, contVeth netlink.Link, err error) { var hostVethName string hostVethName, contVeth, err = makeVeth(contVethName, mtu) @@ -98,6 +97,11 @@ func SetupVeth(contVethName string, mtu int, hostNS *os.File) (hostVeth, contVet return } + if err = netlink.LinkSetUp(hostVeth); err != nil { + err = fmt.Errorf("failed to set %q up: %v", contVethName, err) + return + } + if err = netlink.LinkSetNsFd(hostVeth, int(hostNS.Fd())); err != nil { err = fmt.Errorf("failed to move veth to host netns: %v", err) return diff --git a/plugins/main/ptp/ptp.go b/plugins/main/ptp/ptp.go index c91418aa..58d52ef9 100644 --- a/plugins/main/ptp/ptp.go +++ b/plugins/main/ptp/ptp.go @@ -128,10 +128,6 @@ func setupHostVeth(vethName string, ipConf *types.IPConfig) error { return fmt.Errorf("failed to lookup %q: %v", vethName, err) } - if err = netlink.LinkSetUp(veth); err != nil { - return fmt.Errorf("failed to set %q up: %v", vethName, err) - } - // TODO(eyakubovich): IPv6 ipn := &net.IPNet{ IP: ipConf.Gateway, From 5b7aa09e52da1c490ace2ec2c940fe5ef1575d27 Mon Sep 17 00:00:00 2001 From: Stefan Junker Date: Thu, 24 Sep 2015 18:27:41 +0200 Subject: [PATCH 2/2] link: switch to host netns to set up host veth end --- pkg/ip/link.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/pkg/ip/link.go b/pkg/ip/link.go index 3936ed9d..44e5d84d 100644 --- a/pkg/ip/link.go +++ b/pkg/ip/link.go @@ -20,6 +20,7 @@ import ( "net" "os" + "github.com/appc/cni/pkg/ns" "github.com/vishvananda/netlink" ) @@ -78,7 +79,8 @@ func RandomVethName() (string, error) { } // SetupVeth sets up a virtual ethernet link. -// Should be in container netns. +// Should be in container netns, and will switch back to hostNS to set the host +// veth end up. func SetupVeth(contVethName string, mtu int, hostNS *os.File) (hostVeth, contVeth netlink.Link, err error) { var hostVethName string hostVethName, contVeth, err = makeVeth(contVethName, mtu) @@ -97,16 +99,22 @@ func SetupVeth(contVethName string, mtu int, hostNS *os.File) (hostVeth, contVet return } - if err = netlink.LinkSetUp(hostVeth); err != nil { - err = fmt.Errorf("failed to set %q up: %v", contVethName, err) - return - } - if err = netlink.LinkSetNsFd(hostVeth, int(hostNS.Fd())); err != nil { err = fmt.Errorf("failed to move veth to host netns: %v", err) return } + err = ns.WithNetNS(hostNS, false, func(_ *os.File) error { + hostVeth, err := netlink.LinkByName(hostVethName) + if err != nil { + return fmt.Errorf("failed to lookup %q in %q: %v", hostVethName, hostNS.Name(), err) + } + + if err = netlink.LinkSetUp(hostVeth); err != nil { + return fmt.Errorf("failed to set %q up: %v", hostVethName, err) + } + return nil + }) return }