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>
This commit is contained in:
Sebastiaan van Stijn 2023-08-21 10:18:39 +02:00
parent 338fc49060
commit 542fe0da40
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 49 additions and 49 deletions

View file

@ -115,8 +115,8 @@ func (i *Interface) Statistics() (*types.InterfaceStatistics, error) {
}
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 {
// 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 {
i.dstName = i.srcName
} else {
@ -164,7 +164,7 @@ func (n *Namespace) AddInterface(srcName, dstPrefix string, options ...IfaceOpti
isDefault := n.isDefault
nlh := n.nlHandle
nlhHost := ns.NlHandle()
n.Unlock()
n.mu.Unlock()
// 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
@ -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)
}
n.Lock()
n.mu.Lock()
n.iFaces = append(n.iFaces, i)
n.Unlock()
n.mu.Unlock()
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
// original name and moving it out of the sandbox.
func (n *Namespace) RemoveInterface(i *Interface) error {
n.Lock()
n.mu.Lock()
isDefault := n.isDefault
nlh := n.nlHandle
n.Unlock()
n.mu.Unlock()
// Find the network interface identified by the DstName attribute.
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 {
if intf == i {
i.ns.iFaces = append(i.ns.iFaces[:index], i.ns.iFaces[index+1:]...)
break
}
}
n.Unlock()
n.mu.Unlock()
n.checkLoV6()
return nil

View file

@ -328,7 +328,7 @@ type Namespace struct {
isDefault bool
nlHandle *netlink.Handle
loV6Enabled bool
sync.Mutex
mu sync.Mutex
}
// 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 {
n.Lock()
defer n.Unlock()
n.mu.Lock()
defer n.mu.Unlock()
return n.path
}
@ -547,33 +547,33 @@ func (n *Namespace) Restore(ifsopt map[Iface][]IfaceOption, routes []*types.Stat
}
}
index++
n.Lock()
n.mu.Lock()
if index > n.nextIfIndex[name.DstPrefix] {
n.nextIfIndex[name.DstPrefix] = index
}
n.iFaces = append(n.iFaces, i)
n.Unlock()
n.mu.Unlock()
}
}
// restore routes
for _, r := range routes {
n.Lock()
n.mu.Lock()
n.staticRoutes = append(n.staticRoutes, r)
n.Unlock()
n.mu.Unlock()
}
// restore gateway
if len(gw) > 0 {
n.Lock()
n.mu.Lock()
n.gw = gw
n.Unlock()
n.mu.Unlock()
}
if len(gw6) > 0 {
n.Lock()
n.mu.Lock()
n.gwv6 = gw6
n.Unlock()
n.mu.Unlock()
}
return nil
@ -586,7 +586,7 @@ func (n *Namespace) checkLoV6() {
action = "disable"
)
n.Lock()
n.mu.Lock()
for _, iface := range n.iFaces {
if iface.AddressIPv6() != nil {
enable = true
@ -594,7 +594,7 @@ func (n *Namespace) checkLoV6() {
break
}
}
n.Unlock()
n.mu.Unlock()
if n.loV6Enabled == enable {
return

View file

@ -32,8 +32,8 @@ type neigh struct {
}
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 {
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}
}
n.Lock()
n.mu.Lock()
nlh := n.nlHandle
n.Unlock()
n.mu.Unlock()
var linkIndex int
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 {
if neighbor.dstIP.Equal(dstIP) && bytes.Equal(neighbor.dstMac, dstMac) {
n.neighbors = append(n.neighbors[:i], n.neighbors[i+1:]...)
break
}
}
n.Unlock()
n.mu.Unlock()
log.G(context.TODO()).Debugf("Neighbor entry deleted for IP %v, mac %v", dstIP, dstMac)
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
n.Unlock()
n.mu.Unlock()
if 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
}
n.Lock()
n.mu.Lock()
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)
return nil

View file

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