@@ -111,11 +111,6 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
111111
112112// AddNeighbor adds a neighbor entry into the sandbox.
113113func (n * Namespace ) AddNeighbor (dstIP net.IP , dstMac net.HardwareAddr , options ... NeighOption ) error {
114- var (
115- iface netlink.Link
116- err error
117- )
118-
119114 nh := n .findNeighbor (dstIP , dstMac )
120115 if nh != nil {
121116 log .G (context .TODO ()).Warnf ("Neighbor entry already present for IP %v, mac %v neighbor:%+v" , dstIP , dstMac , nh )
@@ -129,24 +124,10 @@ func (n *Namespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, options .
129124
130125 nh .processNeighOptions (options ... )
131126
132- if nh .linkName != "" {
133- nh .linkDst = n .findDst (nh .linkName , false )
134- if nh .linkDst == "" {
135- return fmt .Errorf ("could not find the interface with name %s" , nh .linkName )
136- }
137- }
138-
139127 n .mu .Lock ()
140128 nlh := n .nlHandle
141129 n .mu .Unlock ()
142130
143- if nh .linkDst != "" {
144- iface , err = nlh .LinkByName (nh .linkDst )
145- if err != nil {
146- return fmt .Errorf ("could not find interface with destination name %s: %v" , nh .linkDst , err )
147- }
148- }
149-
150131 nlnh := & netlink.Neigh {
151132 IP : dstIP ,
152133 HardwareAddr : dstMac ,
@@ -158,7 +139,16 @@ func (n *Namespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, options .
158139 nlnh .Flags = netlink .NTF_SELF
159140 }
160141
161- if nh .linkDst != "" {
142+ if nh .linkName != "" {
143+ nh .linkDst = n .findDst (nh .linkName , false )
144+ if nh .linkDst == "" {
145+ return fmt .Errorf ("could not find the interface with name %s" , nh .linkName )
146+ }
147+
148+ iface , err := nlh .LinkByName (nh .linkDst )
149+ if err != nil {
150+ return fmt .Errorf ("could not find interface with destination name %s: %v" , nh .linkDst , err )
151+ }
162152 nlnh .LinkIndex = iface .Attrs ().Index
163153 }
164154
0 commit comments