Exemplo n.º 1
0
 public NetworkAdapterInfo GetNetworkAdapterInfo()
 {
     using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData())
     {
         using (IEthernetPortAllocationSettingData epasd = EthernetPortAllocationSettingData.GetRelated(systemSettingData).DisposingUnless <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>())
         {
             if (epasd == null)
             {
                 return new NetworkAdapterInfo()
                        {
                            SwitchPort = new SwitchPortInfo()
                            {
                                Name = string.Empty
                            },
                            VirtualSwitch = new VirtualSwitchInfo()
                            {
                                Label = string.Empty
                            }
                        }
             }
             ;
             string vsPath = epasd.HostResource == null || epasd.HostResource.Length == 0 ? string.Empty : epasd.HostResource[0];
             using (IEthernetSwitchPortVlanSettingData portVlanSettingData = EthernetSwitchPortVlanSettingData.GetRelated(epasd).FirstOrDefault <IEthernetSwitchPortVlanSettingData>())
             {
                 using (IEthernetSwitchPort ethernetSwitchPort = EthernetSwitchPort.GetRelated(epasd).FirstOrDefault <IEthernetSwitchPort>())
                 {
                     using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.GetAllVirtualEthernetSwitches(this._Host).DisposingUnless <IVirtualEthernetSwitch>((Func <IVirtualEthernetSwitch, bool>)(v => ((IWMICommon)v).Object.Path.Path == vsPath)).FirstOrDefault <IVirtualEthernetSwitch>())
                     {
                         NetworkAdapterInfo networkAdapterInfo = new NetworkAdapterInfo()
                         {
                             SwitchPort    = new SwitchPortInfo(),
                             VirtualSwitch = new VirtualSwitchInfo()
                         };
                         if (ethernetSwitchPort == null)
                         {
                             if (virtualEthernetSwitch != null)
                             {
                                 networkAdapterInfo.SwitchPort.Name = virtualEthernetSwitch.ElementName;
                                 networkAdapterInfo.SwitchPort.Guid = virtualEthernetSwitch.Name;
                             }
                         }
                         else
                         {
                             networkAdapterInfo.SwitchPort.Name = ethernetSwitchPort.SystemName;
                             networkAdapterInfo.SwitchPort.Guid = ethernetSwitchPort.Name;
                         }
                         networkAdapterInfo.SwitchPort.AccessVLanId = portVlanSettingData == null ? 0 : (int)portVlanSettingData.AccessVlanId;
                         if (virtualEthernetSwitch != null)
                         {
                             networkAdapterInfo.VirtualSwitch.Label      = virtualEthernetSwitch.ElementName;
                             networkAdapterInfo.VirtualSwitch.SwitchUuid = virtualEthernetSwitch.Name;
                         }
                         return(networkAdapterInfo);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 public void RemoveVLan()
 {
     using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
     {
         using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData())
         {
             using (IEthernetPortAllocationSettingData epasd = EthernetPortAllocationSettingData.GetRelated(systemSettingData).DisposingUnless <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>())
             {
                 if (epasd == null)
                 {
                     this._Logger.Information("Nic already not connected - no changes have been made to Vlan configuration.");
                 }
                 else if ((int)epasd.EnabledState == 3)
                 {
                     this._Logger.Information("Nic disabled - no changes have been made to Vlan configuration.");
                 }
                 else
                 {
                     using (IEthernetSwitchPortVlanSettingData portVlanSettingData = EthernetSwitchPortVlanSettingData.GetRelated(epasd).FirstOrDefault <IEthernetSwitchPortVlanSettingData>())
                     {
                         if (portVlanSettingData == null)
                         {
                             this._Logger.Information("No Vlan has been specified - no changes have been made to Vlan configuration.");
                         }
                         else
                         {
                             portVlanSettingData.AccessVlanId  = (ushort)0;
                             portVlanSettingData.OperationMode = 1U;
                             managmentService.ModifyFeatureSettings(new string[1]
                             {
                                 ((IWMICommon)portVlanSettingData).Object.GetText(TextFormat.WmiDtd20)
                             }).ForEach <IEthernetSwitchFeatureSettingData>((Action <IEthernetSwitchFeatureSettingData>)(f => f.TryDispose()));
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 3
0
 protected void AddNicToVm(IVm vm, VirtualNicType nicType, string vswitchName, NicConnectionStatus nicStatus, string nicGuid, int VLan)
 {
     using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
     {
         using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.Query(this._Host, "ElementName='{0}'", (object)vswitchName).FirstOrDefault <IVirtualEthernetSwitch>())
         {
             if (virtualEthernetSwitch == null)
             {
                 throw new HyperVException("Virtual Ethernet switch '" + vswitchName + "' not found");
             }
             using (IVirtualSystemSettingData systemSettingData = ((IVmInternals)vm).ComputerSystem.GetVirtualSystemSettingData())
             {
                 ICommonEthernetPortSettingData ethernetPortSettingData;
                 if (nicType != VirtualNicType.Legacy)
                 {
                     if (nicType != VirtualNicType.Synthetic)
                     {
                         throw new HyperVException("I don't know about the nic type " + nicType.ToString());
                     }
                     ethernetPortSettingData = (ICommonEthernetPortSettingData)SyntheticEthernetPortSettingData.GetDefaultSyntheticAdapter(this._Host);
                     ((ISyntheticEthernetPortSettingData)ethernetPortSettingData).VirtualSystemIdentifiers = new string[1]
                     {
                         nicGuid
                     };
                     ethernetPortSettingData.ElementName = "Synthetic Network Adapter";
                 }
                 else
                 {
                     ethernetPortSettingData             = (ICommonEthernetPortSettingData)EmulatedEthernetPortSettingData.GetDefaultLegacyAdapter(this._Host);
                     ethernetPortSettingData.ElementName = "Legacy Network Adapter";
                 }
                 using (ethernetPortSettingData)
                 {
                     ethernetPortSettingData.StaticMacAddress = false;
                     using (IResourceAllocationSettingData allocationSettingData1 = managmentService.AddResourceSettings(systemSettingData, new string[1]
                     {
                         ((IWMICommon)ethernetPortSettingData).Object.GetText(TextFormat.WmiDtd20)
                     }).FirstOrDefault <IResourceAllocationSettingData>())
                     {
                         if (nicStatus != NicConnectionStatus.Connected)
                         {
                             return;
                         }
                         using (IEthernetPortAllocationSettingData allocationSettingData2 = EthernetPortAllocationSettingData.GetDefaultEthernetPortAllocationSettingData(this._Host))
                         {
                             using (IVirtualSwitch virtualSwitchByName = this.GetVirtualSwitchByName(vswitchName))
                             {
                                 Invariant.ArgumentNotNull((object)allocationSettingData2, "Switch port not found");
                                 Invariant.ArgumentNotNull((object)virtualSwitchByName, "Virtual switch '" + vswitchName + "' not found");
                                 allocationSettingData2.Parent       = ((IWMICommon)allocationSettingData1).Object.Path.Path;
                                 allocationSettingData2.HostResource = new string[1]
                                 {
                                     virtualSwitchByName.Object.Path.Path
                                 };
                                 managmentService.AddResourceSettings(systemSettingData, new string[1]
                                 {
                                     ((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20)
                                 }).FirstOrDefault <IResourceAllocationSettingData>().TryDispose();
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 4
0
 public void Disable()
 {
     using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
     {
         using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData())
         {
             using (IEthernetPortAllocationSettingData allocationSettingData = EthernetPortAllocationSettingData.GetRelated(systemSettingData).DisposingUnless <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>())
             {
                 if (allocationSettingData == null)
                 {
                     this._Logger.Information("Adapter disconnected.");
                 }
                 else
                 {
                     allocationSettingData.EnabledState = (ushort)3;
                     string[] resourceSettings = new string[1]
                     {
                         ((IWMICommon)allocationSettingData).Object.GetText(TextFormat.WmiDtd20)
                     };
                     managmentService.ModifyResourceSettings(resourceSettings).ForEach <IResourceAllocationSettingData>((Action <IResourceAllocationSettingData>)(rasd => rasd.Dispose()));
                 }
             }
         }
     }
 }
Exemplo n.º 5
0
 public void Disconnect()
 {
     using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
     {
         using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData())
         {
             using (IEthernetPortAllocationSettingData allocationSettingData = EthernetPortAllocationSettingData.GetRelated(systemSettingData).Where <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>())
             {
                 if (allocationSettingData == null)
                 {
                     NIC.tracer.TraceInformation("Did not find connection to remove.");
                 }
                 else
                 {
                     using (IResourceAllocationSettingData fromObject = ResourceAllocationSettingData.CreateFromObject(((IWMICommon)allocationSettingData).Object))
                     {
                         NIC.tracer.TraceInformation("Found connection to remove.");
                         managmentService.RemoveResourceSettings(new IResourceAllocationSettingData[1]
                         {
                             fromObject
                         });
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 6
0
 public void Connect(string virtualSwitchName)
 {
     using (VirtualEthernetSwitchManagementService.GetVirtualEthernetSwitchManagementService(this._Host))
     {
         using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
         {
             using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData())
             {
                 using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.GetAllVirtualEthernetSwitches(this._Host).DisposingUnless <IVirtualEthernetSwitch>((Func <IVirtualEthernetSwitch, bool>)(v => v.ElementName == virtualSwitchName)).FirstOrDefault <IVirtualEthernetSwitch>())
                 {
                     using (IEthernetPortAllocationSettingData allocationSettingData1 = EthernetPortAllocationSettingData.GetRelated(systemSettingData).Where <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>())
                     {
                         if (allocationSettingData1 == null)
                         {
                             using (IEthernetPortAllocationSettingData allocationSettingData2 = EthernetPortAllocationSettingData.GetDefaultEthernetPortAllocationSettingData(this._Host))
                             {
                                 Invariant.ArgumentNotNull((object)allocationSettingData2, "Switch port not found");
                                 Invariant.ArgumentNotNull((object)virtualEthernetSwitch, "Virtual switch '" + virtualSwitchName + "' not found");
                                 allocationSettingData2.Parent       = ((IWMICommon)this._AdapterSettingData).Object.Path.Path;
                                 allocationSettingData2.HostResource = new string[1]
                                 {
                                     ((IWMICommon)virtualEthernetSwitch).Object.Path.Path
                                 };
                                 managmentService.AddResourceSettings(systemSettingData, new string[1]
                                 {
                                     ((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20)
                                 }).FirstOrDefault <IResourceAllocationSettingData>().TryDispose();
                             }
                         }
                         else
                         {
                             Invariant.ArgumentNotNull((object)virtualEthernetSwitch, "Virtual switch '" + virtualSwitchName + "' not found");
                             allocationSettingData1.EnabledState = (ushort)2;
                             allocationSettingData1.HostResource = new string[1]
                             {
                                 ((IWMICommon)virtualEthernetSwitch).Object.Path.Path
                             };
                             managmentService.ModifyResourceSettings(new string[1]
                             {
                                 ((IWMICommon)allocationSettingData1).Object.GetText(TextFormat.WmiDtd20)
                             }).ForEach <IResourceAllocationSettingData>((Action <IResourceAllocationSettingData>)(v => v.Dispose()));
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 7
0
 public void ConnectUsingID(string virtualSwitchID)
 {
     NIC.tracer.TraceInformation(string.Format("Connecting using ID {0}", (object)virtualSwitchID));
     using (VirtualEthernetSwitchManagementService.GetVirtualEthernetSwitchManagementService(this._Host))
     {
         using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
         {
             using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData())
             {
                 using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.GetAllVirtualEthernetSwitches(this._Host).DisposingUnless <IVirtualEthernetSwitch>((Func <IVirtualEthernetSwitch, bool>)(v => v.Name == virtualSwitchID)).FirstOrDefault <IVirtualEthernetSwitch>())
                 {
                     using (IEthernetPortAllocationSettingData allocationSettingData1 = EthernetPortAllocationSettingData.GetRelated(systemSettingData).Where <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>())
                     {
                         if (allocationSettingData1 == null)
                         {
                             NIC.tracer.TraceInformation(string.Format("Did not find existing Ethernet port allocation setting data for {0}", (object)virtualSwitchID));
                             using (IEthernetPortAllocationSettingData allocationSettingData2 = EthernetPortAllocationSettingData.GetDefaultEthernetPortAllocationSettingData(this._Host))
                             {
                                 Invariant.ArgumentNotNull((object)allocationSettingData2, "Switch port not found");
                                 Invariant.ArgumentNotNull((object)virtualEthernetSwitch, "Virtual switch '" + virtualSwitchID + "' not found");
                                 allocationSettingData2.Parent = ((IWMICommon)this._AdapterSettingData).Object.Path.Path;
                                 NIC.tracer.TraceInformation(string.Format("Switch port parent {0}", (object)allocationSettingData2.Parent));
                                 allocationSettingData2.HostResource = new string[1]
                                 {
                                     ((IWMICommon)virtualEthernetSwitch).Object.Path.Path
                                 };
                                 NIC.tracer.TraceInformation(string.Format("Switch port host resource {0}", (object[])allocationSettingData2.HostResource));
                                 NIC.tracer.TraceInformation(string.Format("Adding resource settings to VM: {0}", (object)((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20)));
                                 managmentService.AddResourceSettings(systemSettingData, new string[1]
                                 {
                                     ((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20)
                                 }).FirstOrDefault <IResourceAllocationSettingData>().TryDispose();
                             }
                         }
                         else
                         {
                             NIC.tracer.TraceInformation(string.Format("Found existing Ethernet port allocation setting data for {0}", (object)virtualSwitchID));
                             allocationSettingData1.EnabledState = (ushort)2;
                             allocationSettingData1.HostResource = new string[1]
                             {
                                 ((IWMICommon)virtualEthernetSwitch).Object.Path.Path
                             };
                             NIC.tracer.TraceInformation(string.Format("Ethernet port allocation setting data for {0} set to {1}", (object)virtualSwitchID, (object)((IEnumerable <string>)allocationSettingData1.HostResource).FirstOrDefault <string>()));
                             NIC.tracer.TraceInformation(string.Format("Modifying resource settings: {0}", (object)((IWMICommon)allocationSettingData1).Object.GetText(TextFormat.WmiDtd20)));
                             managmentService.ModifyResourceSettings(new string[1]
                             {
                                 ((IWMICommon)allocationSettingData1).Object.GetText(TextFormat.WmiDtd20)
                             }).ForEach <IResourceAllocationSettingData>((Action <IResourceAllocationSettingData>)(v => v.Dispose()));
                         }
                     }
                 }
             }
         }
     }
 }