Skip to content

Commit

Permalink
Provide support to select the interface in Windows
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Buil <[email protected]>
  • Loading branch information
manuelbuil committed Jan 16, 2024
1 parent c25af46 commit 761c257
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/backend/vxlan/device_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package vxlan

import (
"encoding/json"
"fmt"
"time"

"github.com/Microsoft/hcsshim/hcn"
Expand All @@ -32,6 +33,7 @@ type vxlanDeviceAttrs struct {
name string
gbp bool
addressPrefix ip.IP4Net
interfaceName string
}

type vxlanDevice struct {
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions pkg/backend/vxlan/vxlan_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pkg/ipmatch/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"net"
"regexp"
"runtime"
"strings"

"github.com/flannel-io/flannel/pkg/backend"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 761c257

Please sign in to comment.