libnetwork/osl: Namespace.DeleteNeighbor: remove intermediate vars

- store linkIndex in a local variable so that it can be reused
- remove / rename some intermediate vars that shadowed existing declaration

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-09-04 13:30:47 +02:00
parent 4215a1542b
commit cd204f1118
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -46,11 +46,6 @@ func (n *Namespace) findNeighbor(dstIP net.IP, dstMac net.HardwareAddr) *neigh {
// DeleteNeighbor deletes neighbor entry from the sandbox. // DeleteNeighbor deletes neighbor entry from the sandbox.
func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error { func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error {
var (
iface netlink.Link
err error
)
nh := n.findNeighbor(dstIP, dstMac) nh := n.findNeighbor(dstIP, dstMac)
if nh == nil { if nh == nil {
return NeighborSearchError{dstIP, dstMac, false} return NeighborSearchError{dstIP, dstMac, false}
@ -60,28 +55,27 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
nlh := n.nlHandle nlh := n.nlHandle
n.Unlock() n.Unlock()
var linkIndex int
if nh.linkDst != "" { if nh.linkDst != "" {
iface, err = nlh.LinkByName(nh.linkDst) iface, err := nlh.LinkByName(nh.linkDst)
if err != nil { if err != nil {
return fmt.Errorf("could not find interface with destination name %s: %v", nh.linkDst, err) return fmt.Errorf("could not find interface with destination name %s: %v", nh.linkDst, err)
} }
linkIndex = iface.Attrs().Index
} }
nlnh := &netlink.Neigh{ nlnh := &netlink.Neigh{
IP: dstIP, LinkIndex: linkIndex,
State: netlink.NUD_PERMANENT, IP: dstIP,
Family: nh.family, State: netlink.NUD_PERMANENT,
Family: nh.family,
} }
if nlnh.Family > 0 { if nh.family > 0 {
nlnh.HardwareAddr = dstMac nlnh.HardwareAddr = dstMac
nlnh.Flags = netlink.NTF_SELF nlnh.Flags = netlink.NTF_SELF
} }
if nh.linkDst != "" {
nlnh.LinkIndex = iface.Attrs().Index
}
// If the kernel deletion fails for the neighbor entry still remove it // If the kernel deletion fails for the neighbor entry still remove it
// from the namespace cache, otherwise kernel update can fail if the // from the namespace cache, otherwise kernel update can fail if the
// neighbor moves back to the same host again. // neighbor moves back to the same host again.
@ -90,25 +84,21 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
} }
// Delete the dynamic entry in the bridge // Delete the dynamic entry in the bridge
if nlnh.Family > 0 { if nh.family > 0 {
nlnh := &netlink.Neigh{ if err := nlh.NeighDel(&netlink.Neigh{
IP: dstIP, LinkIndex: linkIndex,
Family: nh.family, IP: dstIP,
} Family: nh.family,
HardwareAddr: dstMac,
nlnh.HardwareAddr = dstMac Flags: netlink.NTF_MASTER,
nlnh.Flags = netlink.NTF_MASTER }); err != nil && !errors.Is(err, os.ErrNotExist) {
if nh.linkDst != "" {
nlnh.LinkIndex = iface.Attrs().Index
}
if err := nlh.NeighDel(nlnh); err != nil && !errors.Is(err, os.ErrNotExist) {
log.G(context.TODO()).WithError(err).Warn("error while deleting neighbor entry") log.G(context.TODO()).WithError(err).Warn("error while deleting neighbor entry")
} }
} }
n.Lock() n.Lock()
for i, nh := range n.neighbors { for i, neighbor := range n.neighbors {
if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.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
} }