Skip to content

Commit 8b3f591

Browse files
authored
fix: prevent CNS hanging on HNS restart (#3343)
Signed-off-by: Evan Baker <[email protected]>
1 parent cf6c65a commit 8b3f591

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

cns/service/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,14 @@ func main() {
711711
}
712712

713713
// Setting the remote ARP MAC address to 12-34-56-78-9a-bc on windows for external traffic if HNS is enabled
714-
err = platform.SetSdnRemoteArpMacAddress(rootCtx)
714+
arpCtx, arpCtxCancel := context.WithTimeout(rootCtx, 30*time.Second)
715+
err = platform.SetSdnRemoteArpMacAddress(arpCtx)
715716
if err != nil {
716717
logger.Errorf("Failed to set remote ARP MAC address: %v", err)
718+
arpCtxCancel()
717719
return
718720
}
721+
arpCtxCancel()
719722

720723
// We are only setting the PriorityVLANTag in 'cns.Direct' mode, because it neatly maps today, to 'isUsingMultitenancy'
721724
// In the future, we would want to have a better CNS flag, to explicitly say, this CNS is using multitenancy

platform/os_windows.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ func ExecutePowershellCommand(command string) (string, error) {
166166

167167
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy if hns is enabled
168168
func SetSdnRemoteArpMacAddress(ctx context.Context) error {
169+
if err := setSDNRemoteARPRegKey(); err != nil {
170+
return err
171+
}
172+
log.Printf("SDNRemoteArpMacAddress regKey set successfully")
173+
if err := restartHNS(ctx); err != nil {
174+
return err
175+
}
176+
log.Printf("HNS service restarted successfully")
177+
return nil
178+
}
179+
180+
func setSDNRemoteARPRegKey() error {
169181
log.Printf("Setting SDNRemoteArpMacAddress regKey")
170182
// open the registry key
171183
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\hns\State`, registry.READ|registry.SET_VALUE)
@@ -184,7 +196,10 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
184196
if err = k.SetStringValue("SDNRemoteArpMacAddress", SDNRemoteArpMacAddress); err != nil {
185197
return errors.Wrap(err, "could not set registry key")
186198
}
187-
log.Printf("SDNRemoteArpMacAddress regKey set successfully")
199+
return nil
200+
}
201+
202+
func restartHNS(ctx context.Context) error {
188203
log.Printf("Restarting HNS service")
189204
// connect to the service manager
190205
m, err := mgr.Connect()
@@ -198,24 +213,16 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
198213
return errors.Wrap(err, "could not access service")
199214
}
200215
defer service.Close()
201-
if err := restartService(ctx, service); err != nil {
202-
return errors.Wrap(err, "could not restart service")
203-
}
204-
log.Printf("HNS service restarted successfully")
205-
return nil
206-
}
207-
208-
func restartService(ctx context.Context, s *mgr.Service) error {
209216
// Stop the service
210-
_, err := s.Control(svc.Stop)
217+
_, err = service.Control(svc.Stop)
211218
if err != nil {
212219
return errors.Wrap(err, "could not stop service")
213220
}
214221
// Wait for the service to stop
215222
ticker := time.NewTicker(500 * time.Millisecond) //nolint:gomnd // 500ms
216223
defer ticker.Stop()
217224
for { // hacky cancellable do-while
218-
status, err := s.Query()
225+
status, err := service.Query()
219226
if err != nil {
220227
return errors.Wrap(err, "could not query service status")
221228
}
@@ -229,7 +236,7 @@ func restartService(ctx context.Context, s *mgr.Service) error {
229236
}
230237
}
231238
// Start the service again
232-
if err := s.Start(); err != nil {
239+
if err := service.Start(); err != nil {
233240
return errors.Wrap(err, "could not start service")
234241
}
235242
return nil

0 commit comments

Comments
 (0)