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