refactor: supply /proc path to CreateAndAttachVeth()

Move the assumptions about where we're running up towards the caller.
This commit is contained in:
Bryan Boreham committed 2020-05-14 10:21:09 +00:00
1 parent 2f235daa1c
commit 0f3864968e
3 files changed
+20 -18

No files matched your search

+11 -11
View File
@@ -63,10 +63,10 @@ const (
) )
type Bridge interface { type Bridge interface {
init(config *BridgeConfig) error // create and initialise bridge device(s) init(procPath string, config *BridgeConfig) error // create and initialise bridge device(s)
attach(veth *netlink.Veth) error // attach veth to bridge attach(veth *netlink.Veth) error // attach veth to bridge
IsFastdp() bool // does this bridge use fastdp? IsFastdp() bool // does this bridge use fastdp?
String() string // human-readable type string String() string // human-readable type string
} }
// Used to indicate a fallback to the Bridge type // Used to indicate a fallback to the Bridge type
@@ -250,7 +250,7 @@ func EnsureBridge(procPath string, config *BridgeConfig, log *logrus.Logger, ips
} }
for { for {
if err := bridgeType.init(config); err != nil { if err := bridgeType.init(procPath, config); err != nil {
if errors.Cause(err) == errBridgeNotSupported { if errors.Cause(err) == errBridgeNotSupported {
log.Warnf("Skipping bridge creation of %q due to: %s", bridgeType, err) log.Warnf("Skipping bridge creation of %q due to: %s", bridgeType, err)
bridgeType = bridgeImpl{} bridgeType = bridgeImpl{}
@@ -345,11 +345,11 @@ func (b bridgeImpl) initPrep(config *BridgeConfig) error {
return nil 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 { if err := b.initPrep(config); err != nil {
return err 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) return netlink.LinkSetUp(veth)
}); err != nil { }); err != nil {
return errors.Wrap(err, "creating pcap veth pair") return errors.Wrap(err, "creating pcap veth pair")
@@ -361,7 +361,7 @@ func (b bridgeImpl) init(config *BridgeConfig) error {
return nil return nil
} }
func (f fastdpImpl) init(config *BridgeConfig) error { func (f fastdpImpl) init(procPath string, config *BridgeConfig) error {
odpSupported, err := odp.CreateDatapath(f.datapathName) odpSupported, err := odp.CreateDatapath(f.datapathName)
if !odpSupported { if !odpSupported {
msg := "" msg := ""
@@ -392,14 +392,14 @@ func (f fastdpImpl) init(config *BridgeConfig) error {
return nil return nil
} }
func (bf bridgedFastdpImpl) init(config *BridgeConfig) error { func (bf bridgedFastdpImpl) init(procPath string, config *BridgeConfig) error {
if err := bf.fastdpImpl.init(config); err != nil { if err := bf.fastdpImpl.init(procPath, config); err != nil {
return err return err
} }
if err := bf.bridgeImpl.initPrep(config); err != nil { if err := bf.bridgeImpl.initPrep(config); err != nil {
return err 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 { if err := netlink.LinkSetUp(veth); err != nil {
return errors.Wrapf(err, "setting link up on %q", veth.Attrs().Name) return errors.Wrapf(err, "setting link up on %q", veth.Attrs().Name)
} }
+8 -6
View File
@@ -13,7 +13,7 @@ import (
) )
// create and attach a veth to the Weave bridge // 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) bridge, err := netlink.LinkByName(bridgeName)
if err != nil { if err != nil {
return nil, fmt.Errorf(`bridge "%s" not present; did you launch weave?`, bridgeName) 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 { 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) ns, err := netns.GetFromPath(netNSPath)
if err != nil { if err != nil {
return err 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 id = id[:maxIDLen] // trim passed ID if too long
} }
name, peerName := vethPrefix+"pl"+id, vethPrefix+"pg"+id 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 { if err := netlink.LinkSetNsFd(veth, int(ns)); err != nil {
return fmt.Errorf("failed to move veth to container netns: %s", err) return fmt.Errorf("failed to move veth to container netns: %s", err)
} }
if err := WithNetNS(ns, func() error { if err := WithNetNS(ns, func() error {
return setupIface(peerName, ifName) return setupIface(procPath, peerName, ifName)
}); err != nil { }); err != nil {
return fmt.Errorf("error setting up interface: %s", err) 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 // 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() ipt, err := iptables.New()
if err != nil { if err != nil {
return err return err
@@ -219,8 +222,7 @@ func setupIface(ifaceName, newIfName string) error {
if err := netlink.LinkSetName(link, newIfName); err != nil { if err := netlink.LinkSetName(link, newIfName); err != nil {
return err return err
} }
// This is only called by AttachContainer which is only called in host pid namespace if err := configureARPCache(procPath, newIfName); err != nil {
if err := configureARPCache("/proc", newIfName); err != nil {
return err return err
} }
return ipt.Append("filter", "INPUT", "-i", newIfName, "-d", "224.0.0.0/4", "-j", "DROP") return ipt.Append("filter", "INPUT", "-i", newIfName, "-d", "224.0.0.0/4", "-j", "DROP")
+1 -1
View File
@@ -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 // create veths. note we assume endpoint IDs are unique in the first 9 chars
name, peerName := vethPair(create.EndpointID) 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) return nil, driver.error("JoinEndpoint", "%s", err)
} }