Skip to content

Commit 9866738

Browse files
committed
libnetwork/osl: remove superfluous locks in Namespace
The isDefault and nlHandle fields are immutable once the Namespace is constructed. Signed-off-by: Cory Snider <[email protected]>
1 parent b6d76eb commit 9866738

2 files changed

Lines changed: 11 additions & 23 deletions

File tree

libnetwork/osl/interface_linux.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -804,38 +804,34 @@ func (n *Namespace) prepAdvertiseAddrs(ctx context.Context, i *Interface, ifInde
804804
// original name and moving it out of the sandbox.
805805
func (n *Namespace) RemoveInterface(i *Interface) error {
806806
close(i.stopCh)
807-
n.mu.Lock()
808-
isDefault := n.isDefault
809-
nlh := n.nlHandle
810-
n.mu.Unlock()
811807

812808
// Find the network interface identified by the DstName attribute.
813-
iface, err := nlh.LinkByName(i.DstName())
809+
iface, err := n.nlHandle.LinkByName(i.DstName())
814810
if err != nil {
815811
return err
816812
}
817813

818814
// Down the interface before configuring
819-
if err := nlh.LinkSetDown(iface); err != nil {
815+
if err := n.nlHandle.LinkSetDown(iface); err != nil {
820816
return err
821817
}
822818

823819
// TODO(aker): Why are we doing this? This would fail if the initial interface set up failed before the "dest interface" was moved into its own namespace; see https://github.com/moby/moby/pull/46315/commits/108595c2fe852a5264b78e96f9e63cda284990a6#r1331253578
824-
err = nlh.LinkSetName(iface, i.SrcName())
820+
err = n.nlHandle.LinkSetName(iface, i.SrcName())
825821
if err != nil {
826822
log.G(context.TODO()).Debugf("LinkSetName failed for interface %s: %v", i.SrcName(), err)
827823
return err
828824
}
829825

830826
// if it is a bridge just delete it.
831827
if i.Bridge() {
832-
if err := nlh.LinkDel(iface); err != nil {
828+
if err := n.nlHandle.LinkDel(iface); err != nil {
833829
return fmt.Errorf("failed deleting bridge %q: %v", i.SrcName(), err)
834830
}
835-
} else if !isDefault {
831+
} else if !n.isDefault {
836832
// Move the network interface to caller namespace.
837833
// TODO(aker): What's this really doing? There are no calls to LinkDel in this package: is this code really used? (Interface.Remove() has 3 callers); see https://github.com/moby/moby/pull/46315/commits/108595c2fe852a5264b78e96f9e63cda284990a6#r1331265335
838-
if err := nlh.LinkSetNsFd(iface, ns.ParseHandlerInt()); err != nil {
834+
if err := n.nlHandle.LinkSetNsFd(iface, ns.ParseHandlerInt()); err != nil {
839835
log.G(context.TODO()).Debugf("LinkSetNsFd failed for interface %s: %v", i.SrcName(), err)
840836
return err
841837
}

libnetwork/osl/neigh_linux.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,9 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
5151
return NeighborSearchError{dstIP, dstMac, false}
5252
}
5353

54-
n.mu.Lock()
55-
nlh := n.nlHandle
56-
n.mu.Unlock()
57-
5854
var linkIndex int
5955
if nh.linkDst != "" {
60-
iface, err := nlh.LinkByName(nh.linkDst)
56+
iface, err := n.nlHandle.LinkByName(nh.linkDst)
6157
if err != nil {
6258
return fmt.Errorf("could not find interface with destination name %s: %v", nh.linkDst, err)
6359
}
@@ -79,13 +75,13 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
7975
// If the kernel deletion fails for the neighbor entry still remove it
8076
// from the namespace cache, otherwise kernel update can fail if the
8177
// neighbor moves back to the same host again.
82-
if err := nlh.NeighDel(nlnh); err != nil && !errors.Is(err, os.ErrNotExist) {
78+
if err := n.nlHandle.NeighDel(nlnh); err != nil && !errors.Is(err, os.ErrNotExist) {
8379
log.G(context.TODO()).Warnf("Deleting neighbor IP %s, mac %s failed, %v", dstIP, dstMac, err)
8480
}
8581

8682
// Delete the dynamic entry in the bridge
8783
if nh.family > 0 {
88-
if err := nlh.NeighDel(&netlink.Neigh{
84+
if err := n.nlHandle.NeighDel(&netlink.Neigh{
8985
LinkIndex: linkIndex,
9086
IP: dstIP,
9187
Family: nh.family,
@@ -124,10 +120,6 @@ func (n *Namespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, options .
124120

125121
nh.processNeighOptions(options...)
126122

127-
n.mu.Lock()
128-
nlh := n.nlHandle
129-
n.mu.Unlock()
130-
131123
nlnh := &netlink.Neigh{
132124
IP: dstIP,
133125
HardwareAddr: dstMac,
@@ -145,14 +137,14 @@ func (n *Namespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, options .
145137
return fmt.Errorf("could not find the interface with name %s", nh.linkName)
146138
}
147139

148-
iface, err := nlh.LinkByName(nh.linkDst)
140+
iface, err := n.nlHandle.LinkByName(nh.linkDst)
149141
if err != nil {
150142
return fmt.Errorf("could not find interface with destination name %s: %v", nh.linkDst, err)
151143
}
152144
nlnh.LinkIndex = iface.Attrs().Index
153145
}
154146

155-
if err := nlh.NeighSet(nlnh); err != nil {
147+
if err := n.nlHandle.NeighSet(nlnh); err != nil {
156148
return fmt.Errorf("could not add neighbor entry:%+v error:%v", nlnh, err)
157149
}
158150

0 commit comments

Comments
 (0)