diff --git a/pkg/agent/route/route_windows.go b/pkg/agent/route/route_windows.go index 6f6606c89bf..d721e089519 100644 --- a/pkg/agent/route/route_windows.go +++ b/pkg/agent/route/route_windows.go @@ -234,6 +234,7 @@ func (c *Client) addVirtualServiceIPRoute(isIPv6 bool) error { LinkIndex: linkIndex, IPAddress: config.VirtualServiceIPv4, LinkLayerAddress: openflow.GlobalVirtualMAC, + State: "Permanent", } if err := util.ReplaceNetNeighbor(vNeighbor); err != nil { return err diff --git a/pkg/agent/util/net_windows.go b/pkg/agent/util/net_windows.go index f9e99534c9c..54f453b4ab1 100644 --- a/pkg/agent/util/net_windows.go +++ b/pkg/agent/util/net_windows.go @@ -65,6 +65,7 @@ type Neighbor struct { LinkIndex int IPAddress net.IP LinkLayerAddress net.HardwareAddr + State string } func (n Neighbor) String() string { @@ -660,11 +661,12 @@ func CreateNetNatOnHost(subnetCIDR *net.IPNet) error { // GetNetNeighbor gets neighbor cache entries with Get-NetNeighbor. func GetNetNeighbor(neighbor *Neighbor) ([]Neighbor, error) { - cmd := fmt.Sprintf("Get-NetNeighbor -InterfaceIndex %d -IPAddress %s -ErrorAction Ignore | Format-Table -HideTableHeaders", neighbor.LinkIndex, neighbor.IPAddress.String()) + cmd := fmt.Sprintf("Get-NetNeighbor -InterfaceIndex %d -IPAddress %s | Format-Table -HideTableHeaders", neighbor.LinkIndex, neighbor.IPAddress.String()) neighborsStr, err := ps.RunCommand(cmd) - if err != nil { + if err != nil && !strings.Contains(err.Error(), "No matching MSFT_NetNeighbor objects") { return nil, err } + parsed := parseGetNetCmdResult(neighborsStr, 5) var neighbors []Neighbor for _, items := range parsed { @@ -685,6 +687,7 @@ func GetNetNeighbor(neighbor *Neighbor) ([]Neighbor, error) { LinkIndex: idx, IPAddress: dstIP, LinkLayerAddress: mac, + State: items[3], } neighbors = append(neighbors, neighbor) } @@ -699,10 +702,9 @@ func NewNetNeighbor(neighbor *Neighbor) error { return err } -// SetNetNeighbor modifies a neighbor cache entry with Set-NetNeighbor. -func SetNetNeighbor(neighbor *Neighbor) error { - cmd := fmt.Sprintf("Set-NetNeighbor -InterfaceIndex %d -IPAddress %s -LinkLayerAddress %s -State Permanent", - neighbor.LinkIndex, neighbor.IPAddress, neighbor.LinkLayerAddress) +func RemoveNetNeighbor(neighbor *Neighbor) error { + cmd := fmt.Sprintf("Remove-NetNeighbor -InterfaceIndex %d -IPAddress %s -Confirm:$false", + neighbor.LinkIndex, neighbor.IPAddress) _, err := ps.RunCommand(cmd) return err } @@ -719,18 +721,13 @@ func ReplaceNetNeighbor(neighbor *Neighbor) error { } return nil } - found := false for _, n := range neighbors { - if n.LinkLayerAddress.String() == neighbor.LinkLayerAddress.String() { - found = true - break + if n.LinkLayerAddress.String() == neighbor.LinkLayerAddress.String() && n.State == neighbor.State { + return nil } } - if found { - return nil - } - if err := SetNetNeighbor(neighbor); err != nil { + if err := RemoveNetNeighbor(neighbor); err != nil { return err } - return nil + return NewNetNeighbor(neighbor) }