From 97e0fcb765525ba4b7963ffab3c745433be68427 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sat, 27 May 2017 07:21:28 +0000 Subject: [PATCH] Refactor: move SetupIface code from weaveutil to net package --- net/veth.go | 26 ++++++++++++++++++++++++++ prog/weaveutil/netdev.go | 23 +---------------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/net/veth.go b/net/veth.go index 76c0932e..0b78f07f 100644 --- a/net/veth.go +++ b/net/veth.go @@ -193,6 +193,32 @@ func AttachContainer(netNSPath, id, ifName, bridgeName string, mtu int, withMult return nil } +// SetupIface is the implementation of the 'setup-iface' call above, +// running in another process in the container's netns +func SetupIface(ifaceName, newIfName string) error { + ipt, err := iptables.New() + if err != nil { + return err + } + + link, err := netlink.LinkByName(ifaceName) + if err != nil { + return err + } + if err := netlink.LinkSetName(link, newIfName); err != nil { + return err + } + // This is only called by AttachContainer which is only called in host pid namespace + if err := ConfigureARPCache("/proc", newIfName); err != nil { + return err + } + if err := ipt.Append("filter", "INPUT", "-i", newIfName, "-d", "224.0.0.0/4", "-j", "DROP"); err != nil { + return err + } + + return nil +} + // NB: This function can be used only by a process that terminates immediately // after calling the function as it changes netns via WithNetNSLinkUnsafe. func DetachContainer(netNSPath, id, ifName string, cidrs []*net.IPNet) error { diff --git a/prog/weaveutil/netdev.go b/prog/weaveutil/netdev.go index 457e30e3..c6caa38d 100644 --- a/prog/weaveutil/netdev.go +++ b/prog/weaveutil/netdev.go @@ -6,7 +6,6 @@ import ( "strconv" "strings" - "github.com/coreos/go-iptables/iptables" "github.com/j-keck/arping" "github.com/vishvananda/netlink" @@ -48,27 +47,7 @@ func setupIface(args []string) error { ifaceName := args[0] newIfName := args[1] - ipt, err := iptables.New() - if err != nil { - return err - } - - link, err := netlink.LinkByName(ifaceName) - if err != nil { - return err - } - if err := netlink.LinkSetName(link, newIfName); err != nil { - return err - } - // This is only called by AttachContainer which is only called in host pid namespace - if err := weavenet.ConfigureARPCache("/proc", newIfName); err != nil { - return err - } - if err := ipt.Append("filter", "INPUT", "-i", newIfName, "-d", "224.0.0.0/4", "-j", "DROP"); err != nil { - return err - } - - return nil + return weavenet.SetupIface(ifaceName, newIfName) } func configureARP(args []string) error {