Skip to content

Commit

Permalink
fetch primary mac for hns accelnet (#2857)
Browse files Browse the repository at this point in the history
* fetch primary mac for hns accelnet

* downgrade winio module

* revert winio to 0.6.2

* remove replace winio old version & separate hns call into windows-only file

* fix lint errors

* rename files

* go fmt locally

* add build tags

* moving method to platform package

* refactor files to restserver
fix fmt issues, linter

* minor comment edit

* undo azure-ipam go.mod, edit minor comments

* add log line & format better main.go

* address review comments, change ctx todo

* undo temp pipeline changes

* fix: gofumpt the file linter error
  • Loading branch information
kmurudi authored Jul 31, 2024
1 parent 8fd638c commit 6e3d2ff
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cns/middlewares/k8sSwiftV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodI
PrefixLength: uint8(prefixSize),
},
MacAddress: interfaceInfo.MacAddress,
NICType: cns.DelegatedVMNIC,
NICType: nicType,
SkipDefaultRoutes: false,
// InterfaceName is empty for DelegatedVMNIC
// InterfaceName is empty for DelegatedVMNIC and AccelnetFrontendNIC
}
// for windows scenario, it is required to add additional fields with the exact subnetAddressSpace
// received from MTPNC, this function assigns them for windows while linux is a no-op
Expand Down
4 changes: 4 additions & 0 deletions cns/restserver/internalapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,7 @@ func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req *cns.

return returnCode
}

func (service *HTTPRestService) SetVFForAccelnetNICs() error {
return service.setVFForAccelnetNICs()
}
5 changes: 5 additions & 0 deletions cns/restserver/internalapi_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ func (service *HTTPRestService) programSNATRules(req *cns.CreateNetworkContainer
}
return types.Success, ""
}

// no-op for linux
func (service *HTTPRestService) setVFForAccelnetNICs() error {
return nil
}
54 changes: 54 additions & 0 deletions cns/restserver/internalapi_windows.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,65 @@
package restserver

import (
"context"
"fmt"
"time"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/types"
"github.com/Microsoft/hcsshim"
"github.com/pkg/errors"
)

const (
// timeout for powershell command to return the interfaces list
pwshTimeout = 120 * time.Second
)

// nolint
func (service *HTTPRestService) programSNATRules(req *cns.CreateNetworkContainerRequest) (types.ResponseCode, string) {
return types.Success, ""
}

// setVFForAccelnetNICs is used in SWIFTV2 mode to set VF on accelnet nics
func (service *HTTPRestService) setVFForAccelnetNICs() error {
// supply the primary MAC address to HNS api
macAddress, err := service.getPrimaryNICMACAddress()
if err != nil {
return err
}
macAddresses := []string{macAddress}
if _, err := hcsshim.SetNnvManagementMacAddresses(macAddresses); err != nil {
return errors.Wrap(err, "Failed to set primary NIC MAC address")
}
return nil
}

// getPrimaryNICMacAddress fetches the MAC address of the primary NIC on the node.
func (service *HTTPRestService) getPrimaryNICMACAddress() (string, error) {
// Create a new context and add a timeout to it
ctx, cancel := context.WithTimeout(context.Background(), pwshTimeout)
defer cancel() // The cancel should be deferred so resources are cleaned up

res, err := service.wscli.GetInterfaces(ctx)
if err != nil {
return "", fmt.Errorf("failed to find primary interface info: %w", err)
}
var macAddress string
for _, i := range res.Interface {
// skip if not primary
if !i.IsPrimary {
continue
}
// skip if no subnets
if len(i.IPSubnet) == 0 {
continue
}
macAddress = i.MacAddress
}

if macAddress == "" {
return "", errors.New("MAC address not found(empty) from wireserver")
}
return macAddress, nil
}
6 changes: 6 additions & 0 deletions cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,12 @@ func main() {
if err = httpRemoteRestService.SavePnpIDMacaddressMapping(rootCtx); err != nil {
logger.Errorf("Failed to fetch PnpIDMacaddress mapping: %v", err)
}

// No-op for linux, setting primary macaddress if VF is enabled on the nics for aks swiftv2 windows
logger.Printf("Setting VF for accelnet nics if feature is enabled (only on windows VM & swiftV2 scenario)")
if err = httpRemoteRestService.SetVFForAccelnetNICs(); err != nil {
logger.Errorf("Failed to set VF for accelnet NICs: %v", err)
}
}
}

Expand Down

0 comments on commit 6e3d2ff

Please sign in to comment.