Skip to content

Commit db85fb6

Browse files
authored
fix: prevent CNS hanging on HNS restart
Signed-off-by: Evan Baker <[email protected]>
1 parent 1296fd7 commit db85fb6

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

cns/service/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,15 @@ func main() {
799799
}
800800

801801
// Setting the remote ARP MAC address to 12-34-56-78-9a-bc on windows for external traffic if HNS is enabled
802-
err = platform.SetSdnRemoteArpMacAddress(rootCtx)
802+
arpCtx, arpCtxCancel := context.WithTimeout(rootCtx, 30*time.Second)
803+
err = platform.SetSdnRemoteArpMacAddress(arpCtx)
803804
if err != nil {
804805
logger.Errorf("Failed to set remote ARP MAC address: %v", err)
806+
arpCtxCancel()
805807
return
806808
}
809+
arpCtxCancel()
810+
807811
// We are only setting the PriorityVLANTag in 'cns.Direct' mode, because it neatly maps today, to 'isUsingMultitenancy'
808812
// In the future, we would want to have a better CNS flag, to explicitly say, this CNS is using multitenancy
809813
if cnsconfig.ChannelMode == cns.Direct {

platform/os_windows.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,16 @@ func (p *execClient) ExecutePowershellCommandWithContext(ctx context.Context, co
247247

248248
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy if hns is enabled
249249
func SetSdnRemoteArpMacAddress(ctx context.Context) error {
250+
if err := setSDNRemoteARPRegKey(); err != nil {
251+
return err
252+
}
253+
log.Printf("SDNRemoteArpMacAddress regKey set successfully")
254+
restartHNS(ctx)
255+
log.Printf("HNS service restarted successfully")
256+
return nil
257+
}
258+
259+
func setSDNRemoteARPRegKey() error {
250260
log.Printf("Setting SDNRemoteArpMacAddress regKey")
251261
// open the registry key
252262
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\hns\State`, registry.READ|registry.SET_VALUE)
@@ -265,7 +275,10 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
265275
if err = k.SetStringValue("SDNRemoteArpMacAddress", SDNRemoteArpMacAddress); err != nil {
266276
return errors.Wrap(err, "could not set registry key")
267277
}
268-
log.Printf("SDNRemoteArpMacAddress regKey set successfully")
278+
return nil
279+
}
280+
281+
func restartHNS(ctx context.Context) error {
269282
log.Printf("Restarting HNS service")
270283
// connect to the service manager
271284
m, err := mgr.Connect()
@@ -279,24 +292,16 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
279292
return errors.Wrap(err, "could not access service")
280293
}
281294
defer service.Close()
282-
if err := restartService(ctx, service); err != nil {
283-
return errors.Wrap(err, "could not restart service")
284-
}
285-
log.Printf("HNS service restarted successfully")
286-
return nil
287-
}
288-
289-
func restartService(ctx context.Context, s *mgr.Service) error {
290295
// Stop the service
291-
_, err := s.Control(svc.Stop)
296+
_, err = service.Control(svc.Stop)
292297
if err != nil {
293298
return errors.Wrap(err, "could not stop service")
294299
}
295300
// Wait for the service to stop
296301
ticker := time.NewTicker(500 * time.Millisecond) //nolint:gomnd // 500ms
297302
defer ticker.Stop()
298303
for { // hacky cancellable do-while
299-
status, err := s.Query()
304+
status, err := service.Query()
300305
if err != nil {
301306
return errors.Wrap(err, "could not query service status")
302307
}
@@ -310,7 +315,7 @@ func restartService(ctx context.Context, s *mgr.Service) error {
310315
}
311316
}
312317
// Start the service again
313-
if err := s.Start(); err != nil {
318+
if err := service.Start(); err != nil {
314319
return errors.Wrap(err, "could not start service")
315320
}
316321
return nil

0 commit comments

Comments
 (0)