Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Fix NetNeighbor Powershell error handling #2905

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/agent/route/route_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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
}

tnqn marked this conversation as resolved.
Show resolved Hide resolved
parsed := parseGetNetCmdResult(neighborsStr, 5)
var neighbors []Neighbor
for _, items := range parsed {
Expand All @@ -685,6 +687,7 @@ func GetNetNeighbor(neighbor *Neighbor) ([]Neighbor, error) {
LinkIndex: idx,
IPAddress: dstIP,
LinkLayerAddress: mac,
State: items[3],
}
neighbors = append(neighbors, neighbor)
}
Expand All @@ -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
}
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: if the neighbor for the IP exists but the MAC or the stat doesn't match, can NewNetNeighbor override it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. No, it says Instance MSFT_NetNeighbor already exist. I added a RemoveNetNeighbor before it.

}