datapath-windows: Add WMI to set Hyper-V port "friendly name" from NOVA.
[cascardo/ovs.git] / datapath-windows / misc / OVS.psm1
1 <#
2 Copyright 2014 Cloudbase Solutions Srl
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 #>
16
17 $WMI_JOB_STATUS_STARTED = 4096
18 $WMI_JOB_STATE_RUNNING = 4
19 $WMI_JOB_STATE_COMPLETED = 7
20
21 $hvassembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.HyperV.PowerShell")
22
23 function Set-VMNetworkAdapterOVSPort
24 {
25     [CmdletBinding()]
26     param
27     (
28         [parameter(Mandatory=$true, ValueFromPipeline=$true)]
29         [Microsoft.HyperV.PowerShell.VMNetworkAdapter]$VMNetworkAdapter,
30
31         [parameter(Mandatory=$true)]
32         [ValidateLength(1, 48)]
33         [string]$OVSPortName
34     )
35     process
36     {
37         $ns = "root\virtualization\v2"
38         $EscapedId = $VMNetworkAdapter.Id.Replace('\', '\\')
39
40         $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "ElementName = '$OVSPortName'"
41         if($sd)
42         {
43             if($sd.InstanceId.Contains($VMNetworkAdapter.Id)){
44                 throw "The OVS port name '$OVSPortName' is already assigned to this port."
45             }
46             throw "Cannot assign the OVS port name '$OVSPortName' as it is already assigned to an other port."
47         }
48
49         $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "InstanceId like '$EscapedId%'"
50
51         if($sd)
52         {
53             $sd.ElementName = $OVSPortName
54
55             $vsms = gwmi -namespace $ns -class Msvm_VirtualSystemManagementService
56             $retVal = $vsms.ModifyResourceSettings(@($sd.GetText(1)))
57             try
58             {
59                 Check-WMIReturnValue $retVal
60             }
61             catch
62             {
63                 throw "Assigning OVS port '$OVSPortName' failed"
64             }
65         }
66     }
67 }
68
69 function Get-VMNetworkAdapterByOVSPort
70 {
71     [CmdletBinding()]
72     param
73     (
74
75         [parameter(Mandatory=$true)]
76         [ValidateLength(1, 48)]
77         [string]$OVSPortName
78     )
79     process
80     {
81         $ns = "root\virtualization\v2"
82
83         $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "ElementName = '$OVSPortName'"
84         if($sd)
85         {
86             return $sd
87         }
88     }
89 }
90
91 function Get-VMByOVSPort
92 {
93     [CmdletBinding()]
94     param
95     (
96         [parameter(Mandatory=$true)]
97         [ValidateLength(1, 48)]
98         [string]$OVSPortName
99     )
100     process
101     {
102         $ns = "root\virtualization\v2"
103
104         $vms = gwmi -namespace $ns -class Msvm_VirtualSystemSettingData
105         ForEach($vm in $vms)
106         {
107             $ports = gwmi -Namespace $ns -Query "
108                 Associators of {$vm} Where
109                 ResultClass = Msvm_EthernetPortAllocationSettingData"
110             if ($ports.ElementName -eq $OVSPortName){
111                 return $vm
112             }
113         }
114     }
115 }
116
117 function Check-WMIReturnValue($retVal)
118 {
119     if ($retVal.ReturnValue -ne 0)
120     {
121         if ($retVal.ReturnValue -eq $WMI_JOB_STATUS_STARTED)
122         {
123             do
124             {
125                 $job = [wmi]$retVal.Job
126             }
127             while ($job.JobState -eq $WMI_JOB_STATE_RUNNING)
128
129             if ($job.JobState -ne $WMI_JOB_STATE_COMPLETED)
130             {
131                 echo $job.ReturnValue
132                 $errorString = "Job Failed. Job State: " + $job.JobState.ToString()
133                 if ($job.__CLASS -eq "Msvm_ConcreteJob")
134                 {
135                     $errorString += " Error Code: " + $job.ErrorCode.ToString()
136                     $errorString += " Error Details: " + $job.ErrorDescription
137                 }
138                 else
139                 {
140                     $error = $job.GetError()
141                     if ($error.Error)
142                     {
143                         $errorString += " Error:" + $error.Error
144                     }
145                 }
146                 throw $errorString
147             }
148         }
149         else
150         {
151             throw "Job Failed. Return Value: {0}" -f $job.ReturnValue
152         }
153     }
154 }
155
156 function Set-VMNetworkAdapterOVSPortDirect
157 {
158     [CmdletBinding()]
159     param
160     (
161         [parameter(Mandatory=$true)]
162         [ValidateLength(1, 1024)]
163         [string]$vmName,
164
165         [parameter(Mandatory=$true)]
166         [ValidateLength(1, 48)]
167         [string]$OVSPortName
168     )
169     process
170     {
171         $vnic = 0
172
173         if ($vmName)
174         {
175             $vnic = Get-VMNetworkAdapter -VMName $vmName
176         }
177         # XXX the vnic index should be provided by the caller
178         $vnic[0] | Set-VMNetworkAdapterOVSPort -OVSPortName $OVSPortName
179     }
180 }