Skip to content

Commit

Permalink
Remove vNICs created by OVS from Windows VMSwitch
Browse files Browse the repository at this point in the history
Signed-off-by: wenyingd <wenyingd@vmware.com>
  • Loading branch information
wenyingd committed May 8, 2023
1 parent b963cc8 commit ece97c5
Showing 1 changed file with 26 additions and 97 deletions.
123 changes: 26 additions & 97 deletions hack/windows/Clean-AntreaNetwork.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
.PARAMETER RenewIPConfig
Renew the ipconfig on the host. The default value is $false.
.PARAMETER RemoveOVS
Remove ovsdb-server and ovs-vswitchd services on the host. The default value is $false. If this argument is set as
true, this script would remove the two Windows services of ovsdb-server and ovs-vswitchd from the host. Otherwise,
these services are thought to be supposed to be running on the host, so the script would try to recover them if their
statuses are as expected.
Remove ovsdb-server and ovs-vswitchd services fom the host. The default value is $false. If this argument is set
as true, this script would remove the two Windows services from the host. Otherwise, we consider that these
services are supposed to be running on the host, so the script would try to recover them if their statuses are
not as expected.
#>
Param(
[parameter(Mandatory = $false)] [string] $OVSInstallDir = "C:\openvswitch",
Expand All @@ -25,45 +25,7 @@ $OVS_DB_SCHEMA_PATH = "$OVSInstallDir\usr\share\openvswitch\vswitch.ovsschema"
$OVSDB_CONF_DIR = "$OVSInstallDir\etc\openvswitch"
$OVS_DB_PATH = "$OVSDB_CONF_DIR\conf.db"
$OVS_BR_ADAPTER = "br-int"

function GetHnsnetworkId($NetName) {
$NetList= $(Get-HnsNetwork -ErrorAction SilentlyContinue)
if ($NetList -eq $null) {
return $null
}
foreach ($Net in $NetList) {
if ($Net.Name -eq $NetName) {
return $Net.Id
}
}
return $null
}

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)
}
}
}
$AntreaHnsNetworkName = "antrea-hnsnetwork"

function RemoveOVSService() {
stop-service ovs-vswitchd
Expand Down Expand Up @@ -116,72 +78,39 @@ function CreateStartOVSvSwitchd() {
ovs-vsctl --no-wait set Open_vSwitch . ovs_version=$OVS_VERSION
}

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."
}
}

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() {
$ovsdbService = Get-Service ovsdb-server -ErrorAction SilentlyContinue
if ( $ovsdbService -ne $null -and $ovsdbService.Status -eq "running") {
Write-Host "Delete OVS bridge from OVSDB: br-int"
ovs-vsctl.exe --no-wait --if-exists del-br $OVS_BR_ADAPTER
}
}

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 "$OVS_BR_ADAPTER" -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
function ClearHnsNetwork() {
$vmSwitch = Get-VMSwitch -Name $AntreaHnsNetworkName -ErrorAction SilentlyContinue
if ($vmSwitch -ne $null) {
Write-Host "Remove vNICs"
Remove-VMNetworkAdapter -SwitchName $AntreaHnsNetworkName -ManagementOS -Confirm:$false -ErrorAction SilentlyContinue
$hnsNetwork = Get-HnsNetwork | Where-Object {$_.Name -eq $AntreaHnsNetworkName}
if ($hnsNetwork -ne $null) {
Write-Host "Remove HnsNetwork: $AntreaHnsNetworkName"
$uplink = $hnsNetwork.NetworkAdapterName
Get-HnsNetwork -Id $hnsNetwork.Id | Remove-HnsNetwork -ErrorAction Continue
Set-NetAdapterBinding -Name $uplink -ComponentID vms_pp -Enabled $false
}
Remove-VMSwitch -Name $AntreaHnsNetworkName -Force -ErrorAction SilentlyContinue
}
}

$BrIntDeleted = $(Get-NetAdapter "$OVS_BR_ADAPTER") -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
}

RemoveNetworkAdapter $OVS_BR_ADAPTER
RemoveNetworkAdapter "antrea-gw0"
ClearHyperVBindingOnAdapter($uplink)
clearOVSBridge
ClearHnsNetwork
if ($RemoveOVS) {
RemoveOVSService
} else {
# ResetOVSService is called to recover Windows Services "ovsdb-server" and "ovs-vswitchd" if they are removed
# unexpectedly or their status is not correct, e.g., ovs-vswitchd fails to go into Running.
# This might happen after the Windows host is restarted abnormally, in which case some stale configurations block
# ovs-vswitchd running, like the pid file and the misconfigurations in OVSDB.
# This might happen after the Windows host is restarted abnormally, in which case some stale configurations
# can prevent ovs-vswitchd from running, like a stale pid file or misconfigurations in OVSDB.
ResetOVSService
}
if ($RenewIPConfig) {
Expand Down

0 comments on commit ece97c5

Please sign in to comment.