Skip to content

Commit 26d3540

Browse files
rbtrsivakami
authored andcommitted
fix: prevent CNS hanging on HNS restart (#3343)
Signed-off-by: Evan Baker <[email protected]>
1 parent da8be31 commit 26d3540

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-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: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ 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+
if err := restartHNS(ctx); err != nil {
255+
return err
256+
}
257+
log.Printf("HNS service restarted successfully")
258+
return nil
259+
}
260+
261+
func setSDNRemoteARPRegKey() error {
250262
log.Printf("Setting SDNRemoteArpMacAddress regKey")
251263
// open the registry key
252264
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\hns\State`, registry.READ|registry.SET_VALUE)
@@ -265,7 +277,10 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
265277
if err = k.SetStringValue("SDNRemoteArpMacAddress", SDNRemoteArpMacAddress); err != nil {
266278
return errors.Wrap(err, "could not set registry key")
267279
}
268-
log.Printf("SDNRemoteArpMacAddress regKey set successfully")
280+
return nil
281+
}
282+
283+
func restartHNS(ctx context.Context) error {
269284
log.Printf("Restarting HNS service")
270285
// connect to the service manager
271286
m, err := mgr.Connect()
@@ -279,24 +294,16 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
279294
return errors.Wrap(err, "could not access service")
280295
}
281296
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 {
290297
// Stop the service
291-
_, err := s.Control(svc.Stop)
298+
_, err = service.Control(svc.Stop)
292299
if err != nil {
293300
return errors.Wrap(err, "could not stop service")
294301
}
295302
// Wait for the service to stop
296303
ticker := time.NewTicker(500 * time.Millisecond) //nolint:gomnd // 500ms
297304
defer ticker.Stop()
298305
for { // hacky cancellable do-while
299-
status, err := s.Query()
306+
status, err := service.Query()
300307
if err != nil {
301308
return errors.Wrap(err, "could not query service status")
302309
}
@@ -310,7 +317,7 @@ func restartService(ctx context.Context, s *mgr.Service) error {
310317
}
311318
}
312319
// Start the service again
313-
if err := s.Start(); err != nil {
320+
if err := service.Start(); err != nil {
314321
return errors.Wrap(err, "could not start service")
315322
}
316323
return nil

0 commit comments

Comments
 (0)