Skip to content

Commit a1f804a

Browse files
Set SDNRemoteArpMacAddress regKey for windows multitenancy (#330)
* set SDNRemoteArpMacAddress * Update the Comment * address review comment
1 parent 9b17569 commit a1f804a

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

cns/restserver/restserver.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,14 @@ func (service *HTTPRestService) getNetworkContainerByOrchestratorContext(w http.
10901090
return
10911091
}
10921092

1093+
// getNetworkContainerByOrchestratorContext gets called for multitenancy and
1094+
// setting the SDNRemoteArpMacAddress regKey is essential for the multitenancy
1095+
// to work correctly in case of windows platform. Return if there is an error
1096+
if err = platform.SetSdnRemoteArpMacAddress(); err != nil {
1097+
log.Printf("[Azure CNS] SetSdnRemoteArpMacAddress failed with error: %s", err.Error())
1098+
return
1099+
}
1100+
10931101
getNetworkContainerResponse := service.getNetworkContainerResponse(req)
10941102
returnCode := getNetworkContainerResponse.Response.ReturnCode
10951103
err = service.Listener.Encode(w, &getNetworkContainerResponse)

platform/os_linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,9 @@ func KillProcessByName(processName string) error {
105105
_, err := ExecuteCommand(cmd)
106106
return err
107107
}
108+
109+
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy
110+
// This operation is specific to windows OS
111+
func SetSdnRemoteArpMacAddress() error {
112+
return nil
113+
}

platform/os_windows.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,27 @@ const (
3434

3535
// DNCRuntimePath is the path where DNC state files are stored.
3636
DNCRuntimePath = ""
37+
38+
// SDNRemoteArpMacAddress is the registry key for the remote arp mac address.
39+
// This is set for multitenancy to get arp response from within VM
40+
// for vlan tagged arp requests
41+
SDNRemoteArpMacAddress = "12-34-56-78-9a-bc"
42+
43+
// Command to get SDNRemoteArpMacAddress registry key
44+
GetSdnRemoteArpMacAddressCommand = "(Get-ItemProperty " +
45+
"-Path HKLM:\\SYSTEM\\CurrentControlSet\\Services\\hns\\State -Name SDNRemoteArpMacAddress).SDNRemoteArpMacAddress"
46+
47+
// Command to set SDNRemoteArpMacAddress registry key
48+
SetSdnRemoteArpMacAddressCommand = "Set-ItemProperty " +
49+
"-Path HKLM:\\SYSTEM\\CurrentControlSet\\Services\\hns\\State -Name SDNRemoteArpMacAddress -Value \"12-34-56-78-9a-bc\""
50+
51+
// Command to restart HNS service
52+
RestartHnsServiceCommand = "Restart-Service -Name hns"
3753
)
3854

55+
// Flag to check if sdnRemoteArpMacAddress registry key is set
56+
var sdnRemoteArpMacAddressSet = false
57+
3958
// GetOSInfo returns OS version information.
4059
func GetOSInfo() string {
4160
return "windows"
@@ -118,3 +137,48 @@ func KillProcessByName(processName string) {
118137
cmd := fmt.Sprintf("taskkill /IM %v /F", processName)
119138
ExecuteCommand(cmd)
120139
}
140+
141+
// executePowershellCommand executes powershell command
142+
func executePowershellCommand(command string) (string, error) {
143+
ps, err := exec.LookPath("powershell.exe")
144+
if err != nil {
145+
return "", fmt.Errorf("Failed to find powershell executable")
146+
}
147+
148+
cmd := exec.Command(ps, command)
149+
var stdout bytes.Buffer
150+
var stderr bytes.Buffer
151+
cmd.Stdout = &stdout
152+
cmd.Stderr = &stderr
153+
cmd.Run()
154+
155+
return strings.TrimSpace(stdout.String()), nil
156+
}
157+
158+
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy
159+
func SetSdnRemoteArpMacAddress() error {
160+
if sdnRemoteArpMacAddressSet == false {
161+
result, err := executePowershellCommand(GetSdnRemoteArpMacAddressCommand)
162+
if err != nil {
163+
return err
164+
}
165+
166+
// Set the reg key if not already set or has incorrect value
167+
if result != SDNRemoteArpMacAddress {
168+
if _, err = executePowershellCommand(SetSdnRemoteArpMacAddressCommand); err != nil {
169+
log.Printf("Failed to set SDNRemoteArpMacAddress due to error %s", err.Error())
170+
return err
171+
}
172+
173+
log.Printf("[Azure CNS] SDNRemoteArpMacAddress regKey set successfully. Restarting hns service.")
174+
if _, err := executePowershellCommand(RestartHnsServiceCommand); err != nil {
175+
log.Printf("Failed to Restart HNS Service due to error %s", err.Error())
176+
return err
177+
}
178+
}
179+
180+
sdnRemoteArpMacAddressSet = true
181+
}
182+
183+
return nil
184+
}

0 commit comments

Comments
 (0)