Skip to content

Commit 18edf86

Browse files
authored
backport: fix: prevent CNS hanging on HNS restart (#3343) (#3361)
* fix: prevent CNS hanging on HNS restart (#3343) Signed-off-by: Evan Baker <[email protected]> * disable gomnd Signed-off-by: GitHub <[email protected]> --------- Signed-off-by: Evan Baker <[email protected]> Signed-off-by: GitHub <[email protected]>
1 parent 9367db2 commit 18edf86

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ linters:
1717
- gocritic
1818
- gocyclo
1919
- gofmt
20-
- gomnd
2120
- goprintffuncname
2221
- gosimple
2322
- lll

cns/service/main.go

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

800800
// Setting the remote ARP MAC address to 12-34-56-78-9a-bc on windows for external traffic if HNS is enabled
801-
err = platform.SetSdnRemoteArpMacAddress(rootCtx)
801+
arpCtx, arpCtxCancel := context.WithTimeout(rootCtx, 30*time.Second)
802+
err = platform.SetSdnRemoteArpMacAddress(arpCtx)
802803
if err != nil {
803804
logger.Errorf("Failed to set remote ARP MAC address: %v", err)
805+
arpCtxCancel()
804806
return
805807
}
808+
arpCtxCancel()
806809

807810
// We are only setting the PriorityVLANTag in 'cns.Direct' mode, because it neatly maps today, to 'isUsingMultitenancy'
808811
// 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
@@ -184,6 +184,18 @@ func (p *execClient) ExecutePowershellCommand(command string) (string, error) {
184184

185185
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy if hns is enabled
186186
func SetSdnRemoteArpMacAddress(ctx context.Context) error {
187+
if err := setSDNRemoteARPRegKey(); err != nil {
188+
return err
189+
}
190+
log.Printf("SDNRemoteArpMacAddress regKey set successfully")
191+
if err := restartHNS(ctx); err != nil {
192+
return err
193+
}
194+
log.Printf("HNS service restarted successfully")
195+
return nil
196+
}
197+
198+
func setSDNRemoteARPRegKey() error {
187199
log.Printf("Setting SDNRemoteArpMacAddress regKey")
188200
// open the registry key
189201
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\hns\State`, registry.READ|registry.SET_VALUE)
@@ -202,7 +214,10 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
202214
if err = k.SetStringValue("SDNRemoteArpMacAddress", SDNRemoteArpMacAddress); err != nil {
203215
return errors.Wrap(err, "could not set registry key")
204216
}
205-
log.Printf("SDNRemoteArpMacAddress regKey set successfully")
217+
return nil
218+
}
219+
220+
func restartHNS(ctx context.Context) error {
206221
log.Printf("Restarting HNS service")
207222
// connect to the service manager
208223
m, err := mgr.Connect()
@@ -216,24 +231,16 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
216231
return errors.Wrap(err, "could not access service")
217232
}
218233
defer service.Close()
219-
if err := restartService(ctx, service); err != nil {
220-
return errors.Wrap(err, "could not restart service")
221-
}
222-
log.Printf("HNS service restarted successfully")
223-
return nil
224-
}
225-
226-
func restartService(ctx context.Context, s *mgr.Service) error {
227234
// Stop the service
228-
_, err := s.Control(svc.Stop)
235+
_, err = service.Control(svc.Stop)
229236
if err != nil {
230237
return errors.Wrap(err, "could not stop service")
231238
}
232239
// Wait for the service to stop
233240
ticker := time.NewTicker(500 * time.Millisecond) //nolint:gomnd // 500ms
234241
defer ticker.Stop()
235242
for { // hacky cancellable do-while
236-
status, err := s.Query()
243+
status, err := service.Query()
237244
if err != nil {
238245
return errors.Wrap(err, "could not query service status")
239246
}
@@ -247,7 +254,7 @@ func restartService(ctx context.Context, s *mgr.Service) error {
247254
}
248255
}
249256
// Start the service again
250-
if err := s.Start(); err != nil {
257+
if err := service.Start(); err != nil {
251258
return errors.Wrap(err, "could not start service")
252259
}
253260
return nil

0 commit comments

Comments
 (0)