Skip to content

Commit

Permalink
[Windows] Remove Hyper-V binding on network adapter in cleanup script
Browse files Browse the repository at this point in the history
Remove Hyper-V binding on the network adapter which is used as OVS uplink,
otherwise the adapter is not possible to recover IP address after HNSNetwork
cleanup.

Signed-off-by: wenyingd <wenyingd@vmware.com>
  • Loading branch information
wenyingd committed Aug 17, 2021
1 parent a9552a1 commit 53ff890
Showing 1 changed file with 104 additions and 18 deletions.
122 changes: 104 additions & 18 deletions hack/windows/Clean-AntreaNetwork.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,119 @@ function GetHnsnetworkId($NetName) {
}
return $null
}
$AntreaHnsNetworkName = "antrea-hnsnetwork"

Write-Host "Delete OVS bridge: br-int"
ovs-vsctl.exe --no-wait --if-exists del-br br-int
$MaxRetryCount = 10
$RetryCountRange = 1..$MaxRetryCount
$BrIntDeleted = $false
foreach ($RetryCount in $RetryCountRange) {
Write-Host "Waiting for OVS bridge deletion complete ($RetryCount/$MaxRetryCount)..."
$BrIntAdapter = $(Get-NetAdapter br-int -ErrorAction SilentlyContinue)
if ($BrIntAdapter -eq $null) {
$BrIntDeleted = $true
break
function ClearHyperVBinding($adapter) {
$status= $(Get-NetAdapterBinding -Name $adapter.Name -ComponentID vms_pp).Enabled
if ($status -EQ "False") {
Set-NetAdapterBinding -Name $adapter.Name -ComponentID vms_pp -Enabled $False
}
}

function ClearHyperVBindingOnAdapter($adapterName) {
if ($adapterName -NE "") {
$adapter = $(Get-NetAdapter -Name $adapterName)
if ($adapter -eq $null) {
return
}
ClearHyperVBinding($adapter)
} else {
$adapters= $(Get-NetAdapter | ? Virtual -EQ $false)
if ($adapters -eq $null) {
Write-Host "Physical network adapters not found"
return
}
foreach ($adapter in $adapters) {
ClearHyperVBinding($adapter)
}
}
}

function ResetOVSService() {
$ovsVswitchdSvc = Get-Service ovs-vswitchd -ErrorAction Ignore
if ($ovsVswitchdSvc -EQ $null) {
return
}
$ovsStatus = $(Get-Service ovs-vswitchd).Status
if ("$ovsStatus" -EQ "StartPending") {
sc.exe delete ovs-vswitchd
stop-service ovsdb-server

# $OVSInstallDir is where OVS is installed, it is the path argument when using Install-OVS.ps1
$OVSInstallDir="C:\openvswitch"
# Replace the path using the actual path where ovs-vswitchd.pid locates, it is always at $OVSInstallDir\var\run\openvswitch.
$OVS_PID_PATH = "$OVSInstallDir\var\run\openvswitch\ovs-vswitchd.pid"
Remove-Item -Path $OVS_PID_PATH -Force
# Replace the path using the actual path where OVS conf.db locates. It is always at OVSInstallDir\etc\openvswitch\
$OVS_DB_SCHEMA_PATH = "$OVSInstallDir\usr\share\openvswitch\vswitch.ovsschema"
$OVS_DB_PATH = "$OVSInstallDir\etc\openvswitch\conf.db"
Remove-Item -Path "$OVSInstallDir\etc\openvswitch\*"
ovsdb-tool create "$OVS_DB_PATH" "$OVS_DB_SCHEMA_PATH"

start-service ovsdb-server
sc.exe create ovs-vswitchd binpath="$OVSInstallDir\usr\sbin\ovs-vswitchd.exe --pidfile -vfile:info --log-file --service" start= auto depend= "ovsdb-server"
sc.exe failure ovs-vswitchd reset= 0 actions= restart/0/restart/0/restart/0
start-service ovs-vswitchd
}
if ($RetryCount -eq $MaxRetryCount) {
break
}

function RemoveNetworkAdapter($adapterName) {
$adapter = $(Get-NetAdapter $adapterName -ErrorAction Ignore)
if ($adapter -ne $null) {
Remove-NetIPAddress -IfAlias $adapterName -Confirm:$false
Write-Host "Network adapter $adapter.Name is left on the Windows host with status $adapter.Status, please remove it manually."
}
Start-Sleep -Seconds 5
}
if (!$BrIntDeleted) {
Write-Host "Failed to delete OVS Bridge, please retry the script or delete the bridge and HNS network manually."
return

function RemoveHiddenNetDevices() {
$Devs = $(Get-PnpDevice -Class net | ? Status -eq Unknown | Select InstanceId)
foreach ($Dev in $Devs) {
$RemoveKey = "HKLM:\SYSTEM\CurrentControlSet\Enum\$($Dev.InstanceId)"
Get-Item $RemoveKey | Select-Object -ExpandProperty Property | %{Remove-ItemProperty -Path $RemoveKey -Name $_ -Verbose }
}
}

function clearOVSBridge() {
$ovsStatus = $(Get-Service ovs-vswitchd).Status
if ("$ovsStatus" -EQ "running") {
Write-Host "Delete OVS bridge: br-int"
ovs-vsctl.exe --no-wait --if-exists del-br br-int
$MaxRetryCount = 10
$RetryCountRange = 1..$MaxRetryCount
$BrIntDeleted = $false
foreach ($RetryCount in $RetryCountRange) {
Write-Host "Waiting for OVS bridge deletion complete ($RetryCount/$MaxRetryCount)..."
$BrIntAdapter = $(Get-NetAdapter br-int -ErrorAction SilentlyContinue)
if ($BrIntAdapter -eq $null) {
$BrIntDeleted = $true
break
}
if ($RetryCount -eq $MaxRetryCount) {
break
}
Start-Sleep -Seconds 5
}
if (!$BrIntDeleted) {
Write-Host "Failed to delete OVS Bridge, please retry the script or delete the bridge and HNS network manually."
return
}
}
}

$BrIntDeleted = $(Get-NetAdapter br-int) -Eq $null
if ($BrIntDeleted -eq $false) {
clearOVSBridge
}
$uplink = ""
$AntreaHnsNetworkName = "antrea-hnsnetwork"
$NetId = GetHnsnetworkId($AntreaHnsNetworkName)
if ($NetId -ne $null) {
Write-Host "Remove HnsNetwork: $AntreaHnsNetworkName"
$uplink = $(Get-HnsNetwork -Id $NetId).NetworkAdapterName
Get-HnsNetwork -Id $NetId | Remove-HnsNetwork
}

ResetOVSService "ovs-vswitchd"
RemoveNetworkAdapter "br-int"
RemoveNetworkAdapter "antrea-gw0"
ClearHyperVBindingOnAdapter($uplink)
ipconfig /renew

0 comments on commit 53ff890

Please sign in to comment.