Skip to content

Commit

Permalink
[Windows] Fix NetNeighbor Powershell error handling (antrea-io#2905)
Browse files Browse the repository at this point in the history
Stop ignoring Get-NetNeighbor's error and check if the error
message includes "No matching MSFT_NetNeighbor objects"

Signed-off-by: Zhecheng Li <[email protected]>
  • Loading branch information
lzhecheng authored Oct 25, 2021
1 parent 12f8395 commit 7f9fb00
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
1 change: 1 addition & 0 deletions pkg/agent/route/route_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,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
Expand Down
27 changes: 12 additions & 15 deletions pkg/agent/util/net_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Neighbor struct {
LinkIndex int
IPAddress net.IP
LinkLayerAddress net.HardwareAddr
State string
}

func (n Neighbor) String() string {
Expand Down Expand Up @@ -728,11 +729,12 @@ func RemoveNetNatStaticMappingByID(netNatName string, id int) 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 {
Expand All @@ -753,6 +755,7 @@ func GetNetNeighbor(neighbor *Neighbor) ([]Neighbor, error) {
LinkIndex: idx,
IPAddress: dstIP,
LinkLayerAddress: mac,
State: items[3],
}
neighbors = append(neighbors, neighbor)
}
Expand All @@ -767,10 +770,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
}
Expand All @@ -787,18 +789,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)
}

0 comments on commit 7f9fb00

Please sign in to comment.