Skip to content

Commit f314ab5

Browse files
committedApr 12, 2023
broadcase free arp when pod setup
1 parent e29fdc9 commit f314ab5

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed
 

‎pkg/daemon/ovs_linux.go

+6
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ func configureNic(link, ip string, macAddr net.HardwareAddr, mtu int, detectIPCo
544544
return fmt.Errorf("IP address %s has already been used by host with MAC %s", ip, mac)
545545
}
546546
}
547+
if addr.IP.To4() != nil && !detectIPConflict {
548+
// when detectIPConflict is true, free arp is already broadcast in the step of announcement
549+
if err := util.AnnounceArpAddress(link, addr.IP.String(), macAddr, 1, 1*time.Second); err != nil {
550+
klog.Warningf("failed to broadcast free arp with err %v ", err)
551+
}
552+
}
547553

548554
klog.Infof("add ip address %s to %s", ip, link)
549555
if err = netlink.AddrAdd(nodeLink, &addr); err != nil {

‎pkg/util/arp.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,45 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr
203203
// Announcement is identical to the ARP Probe described above,
204204
// except that now the sender and target IP addresses are both
205205
// set to the host's newly selected IPv4 address.
206-
if pkt, err = arp.NewPacket(arp.OperationRequest, mac, tpa, tha, tpa); err != nil {
206+
if err = AnnounceArpAddress(nic, ip, mac, announceNum, announceInterval); err != nil {
207207
return nil, err
208208
}
209209

210+
return nil, nil
211+
}
212+
213+
func AnnounceArpAddress(nic, ip string, mac net.HardwareAddr, announceNum int, announceInterval time.Duration) error {
214+
klog.Infof("announce arp address nic %s , ip %s, with mac %v ", nic, ip, mac)
215+
netInterface, err := net.InterfaceByName(nic)
216+
if err != nil {
217+
return err
218+
}
219+
220+
client, err := arp.Dial(netInterface)
221+
if err != nil {
222+
return err
223+
}
224+
defer client.Close()
225+
226+
tpa, err := netip.ParseAddr(ip)
227+
if err != nil {
228+
klog.Errorf("failed to parse IP address %s: %v ", ip, err)
229+
return err
230+
}
231+
tha := net.HardwareAddr{0, 0, 0, 0, 0, 0}
232+
pkt, err := arp.NewPacket(arp.OperationRequest, mac, tpa, tha, tpa)
233+
if err != nil {
234+
return err
235+
}
236+
237+
dstMac := net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
210238
for i := 0; i < announceNum; i++ {
211239
c := time.NewTimer(announceInterval)
212240
if err = client.SetDeadline(time.Now().Add(announceInterval)); err != nil {
213-
return nil, err
241+
return err
214242
}
215243
if err = client.WriteTo(pkt, dstMac); err != nil {
216-
return nil, err
244+
return err
217245
}
218246
if i == announceNum-1 {
219247
// the last one, no need to wait
@@ -223,5 +251,5 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr
223251
}
224252
}
225253

226-
return nil, nil
254+
return nil
227255
}

0 commit comments

Comments
 (0)
Please sign in to comment.