@@ -388,6 +388,7 @@ public ComputerSystem DeployVirtualMachine(dynamic jsonObj, string systemVmIso)
388388 {
389389
390390 int nicid = nic . deviceId ;
391+ Int32 networkRateMbps = nic . networkRateMbps ;
391392 string mac = nic . mac ;
392393 string vlan = null ;
393394 string isolationUri = nic . isolationUri ;
@@ -447,6 +448,11 @@ public ComputerSystem DeployVirtualMachine(dynamic jsonObj, string systemVmIso)
447448 SetPortVlan ( vlan , portSettings ) ;
448449 }
449450
451+ if ( networkRateMbps > 0 )
452+ {
453+ SetBandWidthLimit ( ( ulong ) networkRateMbps , portSettings ) ;
454+ }
455+
450456 logger . DebugFormat ( "Created adapter {0} on port {1}, {2}" ,
451457 newAdapter . Path , portSettings . Path , ( vlan == null ? "No VLAN" : "VLAN " + vlan ) ) ;
452458 }
@@ -1503,6 +1509,46 @@ private ManagementPath SetPortVlan(string vlan, EthernetPortAllocationSettingDat
15031509 return newResourcePaths [ 0 ] ;
15041510 }
15051511
1512+ private void SetBandWidthLimit ( ulong limit , EthernetPortAllocationSettingData portPath )
1513+ {
1514+ logger . DebugFormat ( "Setting network rate limit to {0}" , limit ) ;
1515+
1516+ var vmVirtMgmtSvc = GetVirtualisationSystemManagementService ( ) ;
1517+ // EthernetSwitchPortBandwidthSettingData.GetInstances();
1518+
1519+ // Create NIC resource by cloning the default NIC
1520+ var bandwidthSettings = EthernetSwitchPortBandwidthSettingData . GetInstances ( vmVirtMgmtSvc . Scope , "InstanceID LIKE \" %Default\" " ) ;
1521+
1522+ // Assert
1523+ if ( bandwidthSettings . Count != 1 )
1524+ {
1525+ var errMsg = string . Format ( "Internal error, could not find default EthernetSwitchPortBandwidthSettingData instance" ) ;
1526+ var ex = new WmiException ( errMsg ) ;
1527+ logger . Error ( errMsg , ex ) ;
1528+ throw ex ;
1529+ }
1530+ var defaultBandwidthSettings = bandwidthSettings . OfType < EthernetSwitchPortBandwidthSettingData > ( ) . First ( ) ;
1531+
1532+ var newBandwidthSettings = new EthernetSwitchPortBandwidthSettingData ( ( ManagementBaseObject ) defaultBandwidthSettings . LateBoundObject . Clone ( ) ) ;
1533+ newBandwidthSettings . Limit = limit * 1000000 ;
1534+
1535+ // Insert bandwidth settings to nic
1536+ string [ ] newResources = new string [ ] { newBandwidthSettings . LateBoundObject . GetText ( System . Management . TextFormat . CimDtd20 ) } ;
1537+ ManagementPath [ ] newResourcePaths = AddFeatureSettings ( newResources , portPath . Path ) ;
1538+
1539+ // assert
1540+ if ( newResourcePaths . Length != 1 )
1541+ {
1542+ var errMsg = string . Format (
1543+ "Failed to properly apply network rate limit {0} for NIC on port {1}" ,
1544+ limit ,
1545+ portPath . Path ) ;
1546+ var ex = new WmiException ( errMsg ) ;
1547+ logger . Error ( errMsg , ex ) ;
1548+ throw ex ;
1549+ }
1550+ }
1551+
15061552
15071553 /// <summary>
15081554 /// External VSwitch has an external NIC, and we assume there is only one external NIC and one external vswitch.
0 commit comments