From 0f3864968eb3ca9f2b1018dff044ae8259499f2d Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 13 May 2020 14:44:24 +0000 Subject: [PATCH] refactor: supply /proc path to CreateAndAttachVeth() Move the assumptions about where we're running up towards the caller. --- net/bridge.go | 22 +++++++++++----------- net/veth.go | 14 ++++++++------ plugin/net/driver.go | 2 +- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/net/bridge.go b/net/bridge.go index f35da524..d36bad11 100644 --- a/net/bridge.go +++ b/net/bridge.go @@ -63,10 +63,10 @@ const ( ) type Bridge interface { - init(config *BridgeConfig) error // create and initialise bridge device(s) - attach(veth *netlink.Veth) error // attach veth to bridge - IsFastdp() bool // does this bridge use fastdp? - String() string // human-readable type string + init(procPath string, config *BridgeConfig) error // create and initialise bridge device(s) + attach(veth *netlink.Veth) error // attach veth to bridge + IsFastdp() bool // does this bridge use fastdp? + String() string // human-readable type string } // Used to indicate a fallback to the Bridge type @@ -250,7 +250,7 @@ func EnsureBridge(procPath string, config *BridgeConfig, log *logrus.Logger, ips } for { - if err := bridgeType.init(config); err != nil { + if err := bridgeType.init(procPath, config); err != nil { if errors.Cause(err) == errBridgeNotSupported { log.Warnf("Skipping bridge creation of %q due to: %s", bridgeType, err) bridgeType = bridgeImpl{} @@ -345,11 +345,11 @@ func (b bridgeImpl) initPrep(config *BridgeConfig) error { return nil } -func (b bridgeImpl) init(config *BridgeConfig) error { +func (b bridgeImpl) init(procPath string, config *BridgeConfig) error { if err := b.initPrep(config); err != nil { return err } - if _, err := CreateAndAttachVeth(BridgeIfName, PcapIfName, config.WeaveBridgeName, config.MTU, true, false, func(veth netlink.Link) error { + if _, err := CreateAndAttachVeth(procPath, BridgeIfName, PcapIfName, config.WeaveBridgeName, config.MTU, true, false, func(veth netlink.Link) error { return netlink.LinkSetUp(veth) }); err != nil { return errors.Wrap(err, "creating pcap veth pair") @@ -361,7 +361,7 @@ func (b bridgeImpl) init(config *BridgeConfig) error { return nil } -func (f fastdpImpl) init(config *BridgeConfig) error { +func (f fastdpImpl) init(procPath string, config *BridgeConfig) error { odpSupported, err := odp.CreateDatapath(f.datapathName) if !odpSupported { msg := "" @@ -392,14 +392,14 @@ func (f fastdpImpl) init(config *BridgeConfig) error { return nil } -func (bf bridgedFastdpImpl) init(config *BridgeConfig) error { - if err := bf.fastdpImpl.init(config); err != nil { +func (bf bridgedFastdpImpl) init(procPath string, config *BridgeConfig) error { + if err := bf.fastdpImpl.init(procPath, config); err != nil { return err } if err := bf.bridgeImpl.initPrep(config); err != nil { return err } - if _, err := CreateAndAttachVeth(BridgeIfName, DatapathIfName, config.WeaveBridgeName, config.MTU, true, false, func(veth netlink.Link) error { + if _, err := CreateAndAttachVeth(procPath, BridgeIfName, DatapathIfName, config.WeaveBridgeName, config.MTU, true, false, func(veth netlink.Link) error { if err := netlink.LinkSetUp(veth); err != nil { return errors.Wrapf(err, "setting link up on %q", veth.Attrs().Name) } diff --git a/net/veth.go b/net/veth.go index dd82abef..a022292f 100644 --- a/net/veth.go +++ b/net/veth.go @@ -13,7 +13,7 @@ import ( ) // create and attach a veth to the Weave bridge -func CreateAndAttachVeth(name, peerName, bridgeName string, mtu int, keepTXOn bool, errIfLinkExist bool, init func(peer netlink.Link) error) (*netlink.Veth, error) { +func CreateAndAttachVeth(procPath, name, peerName, bridgeName string, mtu int, keepTXOn bool, errIfLinkExist bool, init func(peer netlink.Link) error) (*netlink.Veth, error) { bridge, err := netlink.LinkByName(bridgeName) if err != nil { return nil, fmt.Errorf(`bridge "%s" not present; did you launch weave?`, bridgeName) @@ -112,6 +112,9 @@ func interfaceExistsInNamespace(netNSPath string, ifName string) bool { } func AttachContainer(netNSPath, id, ifName, bridgeName string, mtu int, withMulticastRoute bool, cidrs []*net.IPNet, keepTXOn bool, hairpinMode bool) error { + // AttachContainer expects to be called in host pid namespace + const procPath = "/proc" + ns, err := netns.GetFromPath(netNSPath) if err != nil { return err @@ -124,12 +127,12 @@ func AttachContainer(netNSPath, id, ifName, bridgeName string, mtu int, withMult id = id[:maxIDLen] // trim passed ID if too long } name, peerName := vethPrefix+"pl"+id, vethPrefix+"pg"+id - veth, err := CreateAndAttachVeth(name, peerName, bridgeName, mtu, keepTXOn, true, func(veth netlink.Link) error { + veth, err := CreateAndAttachVeth(procPath, name, peerName, bridgeName, mtu, keepTXOn, true, func(veth netlink.Link) error { if err := netlink.LinkSetNsFd(veth, int(ns)); err != nil { return fmt.Errorf("failed to move veth to container netns: %s", err) } if err := WithNetNS(ns, func() error { - return setupIface(peerName, ifName) + return setupIface(procPath, peerName, ifName) }); err != nil { return fmt.Errorf("error setting up interface: %s", err) } @@ -206,7 +209,7 @@ func setupIfaceAddrs(veth netlink.Link, withMulticastRoute bool, cidrs []*net.IP } // setupIface expects to be called in the container's netns -func setupIface(ifaceName, newIfName string) error { +func setupIface(procPath, ifaceName, newIfName string) error { ipt, err := iptables.New() if err != nil { return err @@ -219,8 +222,7 @@ func setupIface(ifaceName, newIfName string) error { 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 { + if err := configureARPCache(procPath, newIfName); err != nil { return err } return ipt.Append("filter", "INPUT", "-i", newIfName, "-d", "224.0.0.0/4", "-j", "DROP") diff --git a/plugin/net/driver.go b/plugin/net/driver.go index 70de3efa..cea245b7 100644 --- a/plugin/net/driver.go +++ b/plugin/net/driver.go @@ -134,7 +134,7 @@ func (driver *driver) CreateEndpoint(create *api.CreateEndpointRequest) (*api.Cr // create veths. note we assume endpoint IDs are unique in the first 9 chars name, peerName := vethPair(create.EndpointID) - if _, err := weavenet.CreateAndAttachVeth(name, peerName, weavenet.WeaveBridgeName, 0, false, true, nil); err != nil { + if _, err := weavenet.CreateAndAttachVeth("/proc", name, peerName, weavenet.WeaveBridgeName, 0, false, true, nil); err != nil { return nil, driver.error("JoinEndpoint", "%s", err) }