Home > PowerCLI, PowerShell, dvSwitch, vSphere > dvSwitch scripting – Part 4 – NIC teaming

dvSwitch scripting – Part 4 – NIC teaming

October 27th, 2009 LucD Leave a comment Go to comments

The previous parts (Part1, Part2 & Part 3) in the dvSwitch series showed how to create a dvSwitch, a portgroup for Virtual Machines and a Service Console & vmKernle portgroup. The test setup now looks something like this:

dvSw-part4-startThe double Service Consoles and vmKernel connection might look confusing at first. But when you select one these connections, the vSphere client will show you to which uplink a specific connection is going.

To increase the availability of the dvSwitch, I will show how to add two pNics and how to activate and configure NIC Teaming.

When I created the dvSwitch I configured it for two uplink ports (per host). Since I’m adding two pNics,  I will first have to change the maximum number of dvUplink ports.

function Get-dvSwitch{
param($dcName,$dvSwName)

$dcNetFolder = Get-View (Get-Datacenter $dcName | Get-View).NetworkFolder
$found = $null
foreach($net in $dcNetFolder.ChildEntity){
if($net.Type -eq "VmwareDistributedVirtualSwitch"){
$temp = Get-View $net
if($temp.Name -eq $dvSwName){
$found = $temp
}
}
}
$found
}

function Set-dvSwUplink{
param($dcName, $dvSwName, $baseUplink, $nrUplink)

$dvSw = Get-dvSwitch $dcName $dvSwitchName
$currentnrUplink = $dvSw.Config.UplinkPortPolicy.UplinkPortName.Count

if($nrUplink -ne $currentnrUplink){
$spec = New-Object VMware.Vim.DVSConfigSpec
$spec.configVersion = $dvSw.Config.ConfigVersion
$spec.uplinkPortPolicy = New-Object VMware.Vim.DVSNameArrayUplinkPortPolicy
for($i = 1; $i -le $nrUplink; $i++){
$spec.uplinkPortPolicy.uplinkPortName += ($baseUplink + $i)
}

$taskMoRef = $dvSw.ReconfigureDvs_Task($spec)

$task = Get-View $taskMoRef
while("running","queued" -contains $task.Info.State){
$task.UpdateViewData("Info")
}
$task.Info.Result
}
else{
$null
}
}

$datacenterName = "Home1"
$dvSwitchName = "dvSw1"
$baseUplink = "dvUpl"
$nrUplinkNew = 4
$dvPgName = "dvPg1"

$dvSw = Get-dvSwitch $datacenterName $dvSwitchName

Set-dvSwUplink $datacenterName $dvSw $baseUplink $nrUplinkNew

Annotation:

Line 23-29: simple algorithm to increase (or decrease) the number of Uplink ports on a dvSwitch.

dvSw-add-uplinks

There are now two free uplink ports available in the dvSwitch. The next step is to add two pNics to the dvSwitch. This has to be done on each host that is connected to the dvSwitch. I use the UpdateNetworkConfig method to accomplish this.

function Add-dvSwHostpNic{
param ($dvSw, $pnics)
$dvSw.Config.Host | % {Get-View $_.Config.Host} | where {$_.Runtime.ConnectionState -eq "connected"} | % {

$hostNetSys = Get-View (Get-View $_.Config.Host).configManager.networkSystem
$spec = New-Object VMware.Vim.HostNetworkConfig
$pSwitch = New-Object VMware.Vim.HostProxySwitchConfig
$pSwitch.changeOperation = "edit"
$pSwitch.spec = New-Object VMware.Vim.HostProxySwitchSpec

$pSwitch.spec.backing = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking

# Get available Uplinks

$hostNetSys.NetworkInfo.ProxySwitch | where {$_.DvsName -eq $dvSw.Name} | % {
$configuredUplinks = @{}
$_.UplinkPort | % {$configuredUplinks[$_.Key] = $_.Value}
$_.Spec.Backing.PnicSpec | % {
$configuredUplinks.Remove($_.UplinkPortKey)
$pSwitch.spec.backing.pnicSpec += $_
}
}

# Assign the free uplinks to the new pNics
$i = 0
$keysUplinks = $configuredUplinks.Keys | Sort-Object
$pnics | % {
$pnic = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
$pnic.pnicDevice = $_
$pnic.UplinkPortKey = $keysUplinks[$i]
$i++
$pSwitch.spec.backing.pnicSpec += $pnic
}
$pSwitch.uuid = $dvSw.Config.Uuid
$spec.ProxySwitch = $pSwitch
$result = $hostNetSys.UpdateNetworkConfig($spec, "modify")
}
}

$datacenterName = "Home1"
$dvSwitchName = "dvSw1"
$baseUplink = "dvUpl"
$nrUplinkNew = 4
$dvPgName = "dvPg1"

Add-dvSwHostpNic $dvSw ("vmnic6","vmnic7")

Anotation:

Line 4: I filter out the hosts that are not connected to avoid error messages from the UpdateNetworkConfig method

Line 16: the available uplinks are placed in a hash table.

Line 18: the uplinks that are in use are removed from the hash table

Line 24: the remaining uplinks, which be definition are free, are used to assign to the new pNICs

dvSw-add-pNic

All the required elements are now in place to set up Nic Teaming. In this case I will keep the two original pNics as the Active Nics and define the two new pNics as Standby Nics. As a failover criteria I will use beacon probing in this example.

function Set-dVSwPgTeam{
param($dvSw, $dvPgName, $actUpl, $sbyUpl)

# Find the portgroup
$dvSw.Portgroup | %{
$dvPgTemp = Get-View -Id $_
if($dvPgTemp.Name -eq $dvPgName){
$dvPg = $dvPgTemp
}
}

$spec = New-Object VMware.Vim.DVPortgroupConfigSpec
$spec.configVersion = $dvPg.Config.ConfigVersion
$spec.defaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting
$spec.defaultPortConfig.uplinkTeamingPolicy = New-Object VMware.Vim.VmwareUplinkPortTeamingPolicy
$spec.defaultPortConfig.uplinkTeamingPolicy.failureCriteria = New-Object VMware.Vim.DVSFailureCriteria
$spec.defaultPortConfig.uplinkTeamingPolicy.failureCriteria.checkBeacon = New-Object VMware.Vim.BoolPolicy
$spec.defaultPortConfig.uplinkTeamingPolicy.failureCriteria.checkBeacon.value = $true
$spec.defaultPortConfig.uplinkTeamingPolicy.notifySwitches = New-Object VMware.Vim.BoolPolicy
$spec.defaultPortConfig.uplinkTeamingPolicy.notifySwitches.value = $true
$spec.defaultPortConfig.uplinkTeamingPolicy.policy = New-Object VMware.Vim.StringPolicy
$spec.defaultPortConfig.uplinkTeamingPolicy.policy.value = "loadbalance_srcid"
$spec.defaultPortConfig.uplinkTeamingPolicy.reversePolicy = New-Object VMware.Vim.BoolPolicy
$spec.defaultPortConfig.uplinkTeamingPolicy.rollingOrder = New-Object VMware.Vim.BoolPolicy
$spec.defaultPortConfig.uplinkTeamingPolicy.rollingOrder.value = $false
$spec.defaultPortConfig.uplinkTeamingPolicy.uplinkPortOrder = New-Object VMware.Vim.VMwareUplinkPortOrderPolicy
$spec.defaultPortConfig.uplinkTeamingPolicy.uplinkPortOrder.activeUplinkPort = $actUpl
$spec.defaultPortConfig.uplinkTeamingPolicy.uplinkPortOrder.standbyUplinkPort = $sbyUpl

$taskMoRef = $dvPg.ReconfigureDVPortgroup_Task($spec)

$task = Get-View $taskMoRef
while("running","queued" -contains $task.Info.State){
$task.UpdateViewData("Info")
}
}

$datacenterName = "Home1"
$dvSwitchName = "dvSw1"
$baseUplink = "dvUpl"
$nrUplinkNew = 4
$dvPgName = "dvPg1"

Set-dvSwPgTeam $dvSw $dvPgName ("dvUpl1","dvUpl2") ("dvUpl3","dvUpl4")

Annotation:

Line13: you need to pass the current configversion to the method otherwise the method will fail

This is how the targeted setup ultimately looks like in the vCenter client.