libnetwork/osl: Namespace: inline setGateway and setGatewayIPv6

They were not consistently used, and the locations where they were
used were already "setters", so we may as well inline the code.

Also updating Namespace.Restore to keep the lock slightly longer,
instead of locking/unlocking for each property individually, although
we should consider to keep the long for the duration of the whole
function to make it more atomic.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-08-21 11:08:59 +02:00
parent bd17d27658
commit 7b96663082
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 13 additions and 29 deletions

View file

@ -557,26 +557,16 @@ func (n *Namespace) Restore(ifsopt map[Iface][]IfaceOption, routes []*types.Stat
}
}
// restore routes
for _, r := range routes {
n.mu.Lock()
n.staticRoutes = append(n.staticRoutes, r)
n.mu.Unlock()
}
// restore gateway
// restore routes and gateways
n.mu.Lock()
n.staticRoutes = append(n.staticRoutes, routes...)
if len(gw) > 0 {
n.mu.Lock()
n.gw = gw
n.mu.Unlock()
}
if len(gw6) > 0 {
n.mu.Lock()
n.gwv6 = gw6
n.mu.Unlock()
}
n.mu.Unlock()
return nil
}

View file

@ -40,18 +40,6 @@ func (n *Namespace) StaticRoutes() []*types.StaticRoute {
return routes
}
func (n *Namespace) setGateway(gw net.IP) {
n.mu.Lock()
n.gw = gw
n.mu.Unlock()
}
func (n *Namespace) setGatewayIPv6(gwv6 net.IP) {
n.mu.Lock()
n.gwv6 = gwv6
n.mu.Unlock()
}
// SetGateway sets the default IPv4 gateway for the sandbox. It is a no-op
// if the given gateway is empty.
func (n *Namespace) SetGateway(gw net.IP) error {
@ -62,7 +50,9 @@ func (n *Namespace) SetGateway(gw net.IP) error {
if err := n.programGateway(gw, true); err != nil {
return err
}
n.setGateway(gw)
n.mu.Lock()
n.gw = gw
n.mu.Unlock()
return nil
}
@ -77,7 +67,9 @@ func (n *Namespace) UnsetGateway() error {
if err := n.programGateway(gw, false); err != nil {
return err
}
n.setGateway(net.IP{})
n.mu.Lock()
n.gw = net.IP{}
n.mu.Unlock()
return nil
}
@ -155,7 +147,9 @@ func (n *Namespace) SetGatewayIPv6(gwv6 net.IP) error {
return err
}
n.setGatewayIPv6(gwv6)
n.mu.Lock()
n.gwv6 = gwv6
n.mu.Unlock()
return nil
}