From 761c25788562368d25e811cba93d011393e81c99 Mon Sep 17 00:00:00 2001 From: Manuel Buil Date: Fri, 12 Jan 2024 16:06:33 +0100 Subject: [PATCH] Provide support to select the interface in Windows Signed-off-by: Manuel Buil --- pkg/backend/vxlan/device_windows.go | 31 +++++++++++++++++++++++++++++ pkg/backend/vxlan/vxlan_windows.go | 1 + pkg/ipmatch/match.go | 4 ++++ 3 files changed, 36 insertions(+) diff --git a/pkg/backend/vxlan/device_windows.go b/pkg/backend/vxlan/device_windows.go index bb48b6f9cf..732e060817 100644 --- a/pkg/backend/vxlan/device_windows.go +++ b/pkg/backend/vxlan/device_windows.go @@ -18,6 +18,7 @@ package vxlan import ( "encoding/json" + "fmt" "time" "github.com/Microsoft/hcsshim/hcn" @@ -32,6 +33,7 @@ type vxlanDeviceAttrs struct { name string gbp bool addressPrefix ip.IP4Net + interfaceName string } type vxlanDevice struct { @@ -40,6 +42,10 @@ type vxlanDevice struct { directRouting bool } +type NetAdapterNameSettings struct { + NetworkAdapterName string `json:"NetworkAdapterName"` +} + func newVXLANDevice(devAttrs *vxlanDeviceAttrs) (*vxlanDevice, error) { subnet := createSubnet(devAttrs.addressPrefix.String(), (devAttrs.addressPrefix.IP + 1).String(), "0.0.0.0/0") network := &hcn.HostComputeNetwork{ @@ -80,6 +86,10 @@ func newVXLANDevice(devAttrs *vxlanDeviceAttrs) (*vxlanDevice, error) { network.Ipams[0].Subnets[0].Policies = append(network.Ipams[0].Subnets[0].Policies, spJson) + if devAttrs.interfaceName != "" { + addNetAdapterName(network, devAttrs.interfaceName) + } + hnsNetwork, err := ensureNetwork(network, devAttrs.addressPrefix.String()) if err != nil { return nil, err @@ -205,3 +215,24 @@ func createSubnet(AddressPrefix string, NextHop string, DestPrefix string) *hcn. }, } } + +// addNetAdapterName adds a policy to the network to set the name of the network adapter +func addNetAdapterName(network *hcn.HostComputeNetwork, netAdapterName string) error { + settings := NetAdapterNameSettings{ + NetworkAdapterName: netAdapterName, + } + + settingsJson, err := json.Marshal(settings) + if err != nil { + return fmt.Errorf("Failed to marshal settings: %w", err) + } + + policySettings := hcn.NetworkPolicy{ + Type: hcn.NetAdapterName, + Settings: settingsJson, + } + + network.Policies = append(network.Policies, policySettings) + + return nil +} diff --git a/pkg/backend/vxlan/vxlan_windows.go b/pkg/backend/vxlan/vxlan_windows.go index 321c2f9d9d..763e6d9883 100644 --- a/pkg/backend/vxlan/vxlan_windows.go +++ b/pkg/backend/vxlan/vxlan_windows.go @@ -148,6 +148,7 @@ func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup, vni: uint32(cfg.VNI), name: cfg.Name, addressPrefix: lease.Subnet, + interfaceName: be.extIface.Iface.Name, } dev, err := newVXLANDevice(&devAttrs) diff --git a/pkg/ipmatch/match.go b/pkg/ipmatch/match.go index 91eca25885..a7904e56bf 100644 --- a/pkg/ipmatch/match.go +++ b/pkg/ipmatch/match.go @@ -19,6 +19,7 @@ import ( "fmt" "net" "regexp" + "runtime" "strings" "github.com/flannel-io/flannel/pkg/backend" @@ -198,6 +199,9 @@ func LookupExtIface(ifname string, ifregexS string, ifcanreach string, ipStack i return nil, fmt.Errorf("Could not match pattern %s to any of the available network interfaces (%s)", ifregexS, strings.Join(availableFaces, ", ")) } } else if len(ifcanreach) > 0 { + if runtime.GOOS == "windows" { + return nil, fmt.Errorf("ifcanreach is not supported on windows") + } log.Info("Determining interface to use based on given ifcanreach: ", ifcanreach) if iface, ifaceAddr, err = ip.GetInterfaceBySpecificIPRouting(net.ParseIP(ifcanreach)); err != nil { return nil, fmt.Errorf("failed to get ifcanreach based interface: %s", err)