@@ -6,14 +6,20 @@ import (
66 "time"
77
88 "github.com/Azure/azure-container-networking/cns"
9+ "github.com/Azure/azure-container-networking/cns/logger"
910 "github.com/Azure/azure-container-networking/cns/types"
1011 "github.com/Microsoft/hcsshim"
1112 "github.com/pkg/errors"
13+ "golang.org/x/sys/windows/registry"
1214)
1315
1416const (
1517 // timeout for powershell command to return the interfaces list
16- pwshTimeout = 120 * time .Second
18+ pwshTimeout = 120 * time .Second
19+ hnsRegistryPath = `SYSTEM\CurrentControlSet\Services\HNS\wcna_state\config`
20+ prefixOnNicRegistryPath = `SYSTEM\CurrentControlSet\Services\HNS\wcna_state\config\PrefixOnNic`
21+ infraNicIfName = "eth0"
22+ enableSNAT = false
1723)
1824
1925var errUnsupportedAPI = errors .New ("unsupported api" )
@@ -75,3 +81,91 @@ func (service *HTTPRestService) getPrimaryNICMACAddress() (string, error) {
7581 }
7682 return macAddress , nil
7783}
84+
85+ func (service * HTTPRestService ) enablePrefixOnNic (enabled bool ) error {
86+ return service .setRegistryValue (prefixOnNicRegistryPath , "enabled" , enabled )
87+ }
88+
89+ func (service * HTTPRestService ) setInfraNicMacAddress (macAddress string ) error {
90+ return service .setRegistryValue (prefixOnNicRegistryPath , "infra_nic_mac_address" , macAddress )
91+ }
92+
93+ func (service * HTTPRestService ) setInfraNicIfName (ifName string ) error {
94+ return service .setRegistryValue (prefixOnNicRegistryPath , "infra_nic_ifname" , ifName )
95+ }
96+
97+ func (service * HTTPRestService ) setEnableSNAT (enabled bool ) error {
98+ return service .setRegistryValue (hnsRegistryPath , "EnableSNAT" , enabled )
99+ }
100+
101+ func (service * HTTPRestService ) setPrefixOnNICRegistry (enablePrefixOnNic bool , infraNicMacAddress string ) error {
102+ if err := service .enablePrefixOnNic (enablePrefixOnNic ); err != nil {
103+ return fmt .Errorf ("failed to set enablePrefixOnNic key to windows registry: %w" , err )
104+ }
105+
106+ if err := service .setInfraNicMacAddress (infraNicMacAddress ); err != nil {
107+ return fmt .Errorf ("failed to set InfraNicMacAddress key to windows registry: %w" , err )
108+ }
109+
110+ if err := service .setInfraNicIfName (infraNicIfName ); err != nil {
111+ return fmt .Errorf ("failed to set InfraNicIfName key to windows registry: %w" , err )
112+ }
113+
114+ if err := service .setEnableSNAT (enableSNAT ); err != nil {
115+ return fmt .Errorf ("failed to set EnableSNAT key to windows registry: %w" , err )
116+ }
117+
118+ return nil
119+ }
120+
121+ func (service * HTTPRestService ) setRegistryValue (registryPath , keyName string , value interface {}) error {
122+ key , _ , err := registry .CreateKey (registry .LOCAL_MACHINE , registryPath , registry .SET_VALUE )
123+ if err != nil {
124+ return fmt .Errorf ("failed to create/open registry key %s: %w" , registryPath , err )
125+ }
126+ defer key .Close ()
127+
128+ switch v := value .(type ) {
129+ case string :
130+ err = key .SetStringValue (keyName , v )
131+ case bool :
132+ dwordValue := uint32 (0 )
133+ if v {
134+ dwordValue = 1
135+ }
136+ err = key .SetDWordValue (keyName , dwordValue )
137+ case uint32 :
138+ err = key .SetDWordValue (keyName , v )
139+ case int :
140+ err = key .SetDWordValue (keyName , uint32 (v ))
141+ default :
142+ return fmt .Errorf ("unsupported value type for registry key %s: %T" , keyName , value )
143+ }
144+
145+ if err != nil {
146+ return fmt .Errorf ("failed to set registry value '%s': %w" , keyName , err )
147+ }
148+
149+ logger .Printf ("[setRegistryValue] Set %s\\ %s = %v" , registryPath , keyName , value )
150+ // have to remove this log later
151+ // test, _ := service.getPrefixOnNicEnabled()
152+ // logger.Printf("winDebug: setRegistryValue getPrefixOnNicEnabled %v", test)
153+ return nil
154+ }
155+
156+ // for testing purpose, will remove it later
157+
158+ // func (service *HTTPRestService) getPrefixOnNicEnabled() (bool, error) {
159+ // key, err := registry.OpenKey(registry.LOCAL_MACHINE, prefixOnNicRegistryPath, registry.QUERY_VALUE)
160+ // if err != nil {
161+ // return false, nil // Key doesn't exist, default to false
162+ // }
163+ // defer key.Close()
164+
165+ // value, _, err := key.GetIntegerValue("enabled")
166+ // if err != nil {
167+ // return false, nil // Value doesn't exist, default to false
168+ // }
169+
170+ // return value == 1, nil
171+ // }
0 commit comments