Browse Source

libnetwork/osl: Namespace: make mutex private

Make the mutex internal to the Namespace; locking/unlocking should not
be done externally, and this makes it easier to see where it's used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 1 year ago
parent
commit
542fe0da40

+ 10 - 10
libnetwork/osl/interface_linux.go

@@ -115,8 +115,8 @@ func (i *Interface) Statistics() (*types.InterfaceStatistics, error) {
 }
 }
 
 
 func (n *Namespace) findDst(srcName string, isBridge bool) string {
 func (n *Namespace) findDst(srcName string, isBridge bool) string {
-	n.Lock()
-	defer n.Unlock()
+	n.mu.Lock()
+	defer n.mu.Unlock()
 
 
 	for _, i := range n.iFaces {
 	for _, i := range n.iFaces {
 		// The master should match the srcname of the interface and the
 		// The master should match the srcname of the interface and the
@@ -152,7 +152,7 @@ func (n *Namespace) AddInterface(srcName, dstPrefix string, options ...IfaceOpti
 		}
 		}
 	}
 	}
 
 
-	n.Lock()
+	n.mu.Lock()
 	if n.isDefault {
 	if n.isDefault {
 		i.dstName = i.srcName
 		i.dstName = i.srcName
 	} else {
 	} else {
@@ -164,7 +164,7 @@ func (n *Namespace) AddInterface(srcName, dstPrefix string, options ...IfaceOpti
 	isDefault := n.isDefault
 	isDefault := n.isDefault
 	nlh := n.nlHandle
 	nlh := n.nlHandle
 	nlhHost := ns.NlHandle()
 	nlhHost := ns.NlHandle()
-	n.Unlock()
+	n.mu.Unlock()
 
 
 	// If it is a bridge interface we have to create the bridge inside
 	// If it is a bridge interface we have to create the bridge inside
 	// the namespace so don't try to lookup the interface using srcName
 	// the namespace so don't try to lookup the interface using srcName
@@ -240,9 +240,9 @@ func (n *Namespace) AddInterface(srcName, dstPrefix string, options ...IfaceOpti
 		return fmt.Errorf("error setting interface %q routes to %q: %v", iface.Attrs().Name, i.Routes(), err)
 		return fmt.Errorf("error setting interface %q routes to %q: %v", iface.Attrs().Name, i.Routes(), err)
 	}
 	}
 
 
-	n.Lock()
+	n.mu.Lock()
 	n.iFaces = append(n.iFaces, i)
 	n.iFaces = append(n.iFaces, i)
-	n.Unlock()
+	n.mu.Unlock()
 
 
 	n.checkLoV6()
 	n.checkLoV6()
 
 
@@ -252,10 +252,10 @@ func (n *Namespace) AddInterface(srcName, dstPrefix string, options ...IfaceOpti
 // RemoveInterface removes an interface from the namespace by renaming to
 // RemoveInterface removes an interface from the namespace by renaming to
 // original name and moving it out of the sandbox.
 // original name and moving it out of the sandbox.
 func (n *Namespace) RemoveInterface(i *Interface) error {
 func (n *Namespace) RemoveInterface(i *Interface) error {
-	n.Lock()
+	n.mu.Lock()
 	isDefault := n.isDefault
 	isDefault := n.isDefault
 	nlh := n.nlHandle
 	nlh := n.nlHandle
-	n.Unlock()
+	n.mu.Unlock()
 
 
 	// Find the network interface identified by the DstName attribute.
 	// Find the network interface identified by the DstName attribute.
 	iface, err := nlh.LinkByName(i.DstName())
 	iface, err := nlh.LinkByName(i.DstName())
@@ -287,14 +287,14 @@ func (n *Namespace) RemoveInterface(i *Interface) error {
 		}
 		}
 	}
 	}
 
 
-	n.Lock()
+	n.mu.Lock()
 	for index, intf := range i.ns.iFaces {
 	for index, intf := range i.ns.iFaces {
 		if intf == i {
 		if intf == i {
 			i.ns.iFaces = append(i.ns.iFaces[:index], i.ns.iFaces[index+1:]...)
 			i.ns.iFaces = append(i.ns.iFaces[:index], i.ns.iFaces[index+1:]...)
 			break
 			break
 		}
 		}
 	}
 	}
-	n.Unlock()
+	n.mu.Unlock()
 
 
 	n.checkLoV6()
 	n.checkLoV6()
 	return nil
 	return nil

+ 13 - 13
libnetwork/osl/namespace_linux.go

@@ -328,7 +328,7 @@ type Namespace struct {
 	isDefault    bool
 	isDefault    bool
 	nlHandle     *netlink.Handle
 	nlHandle     *netlink.Handle
 	loV6Enabled  bool
 	loV6Enabled  bool
-	sync.Mutex
+	mu           sync.Mutex
 }
 }
 
 
 // Interfaces returns the collection of Interface previously added with the AddInterface
 // Interfaces returns the collection of Interface previously added with the AddInterface
@@ -450,8 +450,8 @@ func (n *Namespace) InvokeFunc(f func()) error {
 }
 }
 
 
 func (n *Namespace) nsPath() string {
 func (n *Namespace) nsPath() string {
-	n.Lock()
-	defer n.Unlock()
+	n.mu.Lock()
+	defer n.mu.Unlock()
 
 
 	return n.path
 	return n.path
 }
 }
@@ -547,33 +547,33 @@ func (n *Namespace) Restore(ifsopt map[Iface][]IfaceOption, routes []*types.Stat
 				}
 				}
 			}
 			}
 			index++
 			index++
-			n.Lock()
+			n.mu.Lock()
 			if index > n.nextIfIndex[name.DstPrefix] {
 			if index > n.nextIfIndex[name.DstPrefix] {
 				n.nextIfIndex[name.DstPrefix] = index
 				n.nextIfIndex[name.DstPrefix] = index
 			}
 			}
 			n.iFaces = append(n.iFaces, i)
 			n.iFaces = append(n.iFaces, i)
-			n.Unlock()
+			n.mu.Unlock()
 		}
 		}
 	}
 	}
 
 
 	// restore routes
 	// restore routes
 	for _, r := range routes {
 	for _, r := range routes {
-		n.Lock()
+		n.mu.Lock()
 		n.staticRoutes = append(n.staticRoutes, r)
 		n.staticRoutes = append(n.staticRoutes, r)
-		n.Unlock()
+		n.mu.Unlock()
 	}
 	}
 
 
 	// restore gateway
 	// restore gateway
 	if len(gw) > 0 {
 	if len(gw) > 0 {
-		n.Lock()
+		n.mu.Lock()
 		n.gw = gw
 		n.gw = gw
-		n.Unlock()
+		n.mu.Unlock()
 	}
 	}
 
 
 	if len(gw6) > 0 {
 	if len(gw6) > 0 {
-		n.Lock()
+		n.mu.Lock()
 		n.gwv6 = gw6
 		n.gwv6 = gw6
-		n.Unlock()
+		n.mu.Unlock()
 	}
 	}
 
 
 	return nil
 	return nil
@@ -586,7 +586,7 @@ func (n *Namespace) checkLoV6() {
 		action = "disable"
 		action = "disable"
 	)
 	)
 
 
-	n.Lock()
+	n.mu.Lock()
 	for _, iface := range n.iFaces {
 	for _, iface := range n.iFaces {
 		if iface.AddressIPv6() != nil {
 		if iface.AddressIPv6() != nil {
 			enable = true
 			enable = true
@@ -594,7 +594,7 @@ func (n *Namespace) checkLoV6() {
 			break
 			break
 		}
 		}
 	}
 	}
-	n.Unlock()
+	n.mu.Unlock()
 
 
 	if n.loV6Enabled == enable {
 	if n.loV6Enabled == enable {
 		return
 		return

+ 10 - 10
libnetwork/osl/neigh_linux.go

@@ -32,8 +32,8 @@ type neigh struct {
 }
 }
 
 
 func (n *Namespace) findNeighbor(dstIP net.IP, dstMac net.HardwareAddr) *neigh {
 func (n *Namespace) findNeighbor(dstIP net.IP, dstMac net.HardwareAddr) *neigh {
-	n.Lock()
-	defer n.Unlock()
+	n.mu.Lock()
+	defer n.mu.Unlock()
 
 
 	for _, nh := range n.neighbors {
 	for _, nh := range n.neighbors {
 		if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.dstMac, dstMac) {
 		if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.dstMac, dstMac) {
@@ -51,9 +51,9 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
 		return NeighborSearchError{dstIP, dstMac, false}
 		return NeighborSearchError{dstIP, dstMac, false}
 	}
 	}
 
 
-	n.Lock()
+	n.mu.Lock()
 	nlh := n.nlHandle
 	nlh := n.nlHandle
-	n.Unlock()
+	n.mu.Unlock()
 
 
 	var linkIndex int
 	var linkIndex int
 	if nh.linkDst != "" {
 	if nh.linkDst != "" {
@@ -96,14 +96,14 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
 		}
 		}
 	}
 	}
 
 
-	n.Lock()
+	n.mu.Lock()
 	for i, neighbor := range n.neighbors {
 	for i, neighbor := range n.neighbors {
 		if neighbor.dstIP.Equal(dstIP) && bytes.Equal(neighbor.dstMac, dstMac) {
 		if neighbor.dstIP.Equal(dstIP) && bytes.Equal(neighbor.dstMac, dstMac) {
 			n.neighbors = append(n.neighbors[:i], n.neighbors[i+1:]...)
 			n.neighbors = append(n.neighbors[:i], n.neighbors[i+1:]...)
 			break
 			break
 		}
 		}
 	}
 	}
-	n.Unlock()
+	n.mu.Unlock()
 	log.G(context.TODO()).Debugf("Neighbor entry deleted for IP %v, mac %v", dstIP, dstMac)
 	log.G(context.TODO()).Debugf("Neighbor entry deleted for IP %v, mac %v", dstIP, dstMac)
 
 
 	return nil
 	return nil
@@ -142,9 +142,9 @@ func (n *Namespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, force boo
 		}
 		}
 	}
 	}
 
 
-	n.Lock()
+	n.mu.Lock()
 	nlh := n.nlHandle
 	nlh := n.nlHandle
-	n.Unlock()
+	n.mu.Unlock()
 
 
 	if nh.linkDst != "" {
 	if nh.linkDst != "" {
 		iface, err = nlh.LinkByName(nh.linkDst)
 		iface, err = nlh.LinkByName(nh.linkDst)
@@ -176,9 +176,9 @@ func (n *Namespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, force boo
 		return nil
 		return nil
 	}
 	}
 
 
-	n.Lock()
+	n.mu.Lock()
 	n.neighbors = append(n.neighbors, nh)
 	n.neighbors = append(n.neighbors, nh)
-	n.Unlock()
+	n.mu.Unlock()
 	log.G(context.TODO()).Debugf("Neighbor entry added for IP:%v, mac:%v on ifc:%s", dstIP, dstMac, nh.linkName)
 	log.G(context.TODO()).Debugf("Neighbor entry added for IP:%v, mac:%v on ifc:%s", dstIP, dstMac, nh.linkName)
 
 
 	return nil
 	return nil

+ 16 - 16
libnetwork/osl/route_linux.go

@@ -10,16 +10,16 @@ import (
 
 
 // Gateway returns the IPv4 gateway for the sandbox.
 // Gateway returns the IPv4 gateway for the sandbox.
 func (n *Namespace) Gateway() net.IP {
 func (n *Namespace) Gateway() net.IP {
-	n.Lock()
-	defer n.Unlock()
+	n.mu.Lock()
+	defer n.mu.Unlock()
 
 
 	return n.gw
 	return n.gw
 }
 }
 
 
 // GatewayIPv6 returns the IPv6 gateway for the sandbox.
 // GatewayIPv6 returns the IPv6 gateway for the sandbox.
 func (n *Namespace) GatewayIPv6() net.IP {
 func (n *Namespace) GatewayIPv6() net.IP {
-	n.Lock()
-	defer n.Unlock()
+	n.mu.Lock()
+	defer n.mu.Unlock()
 
 
 	return n.gwv6
 	return n.gwv6
 }
 }
@@ -28,8 +28,8 @@ func (n *Namespace) GatewayIPv6() net.IP {
 // directly connected routes are stored on the particular interface they
 // directly connected routes are stored on the particular interface they
 // refer to.
 // refer to.
 func (n *Namespace) StaticRoutes() []*types.StaticRoute {
 func (n *Namespace) StaticRoutes() []*types.StaticRoute {
-	n.Lock()
-	defer n.Unlock()
+	n.mu.Lock()
+	defer n.mu.Unlock()
 
 
 	routes := make([]*types.StaticRoute, len(n.staticRoutes))
 	routes := make([]*types.StaticRoute, len(n.staticRoutes))
 	for i, route := range n.staticRoutes {
 	for i, route := range n.staticRoutes {
@@ -41,15 +41,15 @@ func (n *Namespace) StaticRoutes() []*types.StaticRoute {
 }
 }
 
 
 func (n *Namespace) setGateway(gw net.IP) {
 func (n *Namespace) setGateway(gw net.IP) {
-	n.Lock()
+	n.mu.Lock()
 	n.gw = gw
 	n.gw = gw
-	n.Unlock()
+	n.mu.Unlock()
 }
 }
 
 
 func (n *Namespace) setGatewayIPv6(gwv6 net.IP) {
 func (n *Namespace) setGatewayIPv6(gwv6 net.IP) {
-	n.Lock()
+	n.mu.Lock()
 	n.gwv6 = gwv6
 	n.gwv6 = gwv6
-	n.Unlock()
+	n.mu.Unlock()
 }
 }
 
 
 // SetGateway sets the default IPv4 gateway for the sandbox.
 // SetGateway sets the default IPv4 gateway for the sandbox.
@@ -173,9 +173,9 @@ func (n *Namespace) UnsetGatewayIPv6() error {
 
 
 	err := n.programGateway(gwv6, false)
 	err := n.programGateway(gwv6, false)
 	if err == nil {
 	if err == nil {
-		n.Lock()
+		n.mu.Lock()
 		n.gwv6 = net.IP{}
 		n.gwv6 = net.IP{}
-		n.Unlock()
+		n.mu.Unlock()
 	}
 	}
 
 
 	return err
 	return err
@@ -185,9 +185,9 @@ func (n *Namespace) UnsetGatewayIPv6() error {
 func (n *Namespace) AddStaticRoute(r *types.StaticRoute) error {
 func (n *Namespace) AddStaticRoute(r *types.StaticRoute) error {
 	err := n.programRoute(n.nsPath(), r.Destination, r.NextHop)
 	err := n.programRoute(n.nsPath(), r.Destination, r.NextHop)
 	if err == nil {
 	if err == nil {
-		n.Lock()
+		n.mu.Lock()
 		n.staticRoutes = append(n.staticRoutes, r)
 		n.staticRoutes = append(n.staticRoutes, r)
-		n.Unlock()
+		n.mu.Unlock()
 	}
 	}
 	return err
 	return err
 }
 }
@@ -196,7 +196,7 @@ func (n *Namespace) AddStaticRoute(r *types.StaticRoute) error {
 func (n *Namespace) RemoveStaticRoute(r *types.StaticRoute) error {
 func (n *Namespace) RemoveStaticRoute(r *types.StaticRoute) error {
 	err := n.removeRoute(n.nsPath(), r.Destination, r.NextHop)
 	err := n.removeRoute(n.nsPath(), r.Destination, r.NextHop)
 	if err == nil {
 	if err == nil {
-		n.Lock()
+		n.mu.Lock()
 		lastIndex := len(n.staticRoutes) - 1
 		lastIndex := len(n.staticRoutes) - 1
 		for i, v := range n.staticRoutes {
 		for i, v := range n.staticRoutes {
 			if v == r {
 			if v == r {
@@ -207,7 +207,7 @@ func (n *Namespace) RemoveStaticRoute(r *types.StaticRoute) error {
 				break
 				break
 			}
 			}
 		}
 		}
-		n.Unlock()
+		n.mu.Unlock()
 	}
 	}
 	return err
 	return err
 }
 }