@@ -623,6 +623,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
623623 protected MemStat memStat = new MemStat (dom0MinMem , dom0OvercommitMem );
624624 private final LibvirtUtilitiesHelper libvirtUtilitiesHelper = new LibvirtUtilitiesHelper ();
625625 private LibvirtDomainListener libvirtDomainListener ;
626+ private final UsageExemptCounters usageExemptCounters = new UsageExemptCounters ();
626627
627628 protected Boolean enableManuallySettingCpuTopologyOnKvmVm = AgentPropertiesFileHandler .getPropertyValue (AgentProperties .ENABLE_MANUALLY_SETTING_CPU_TOPOLOGY_ON_KVM_VM );
628629
@@ -1790,10 +1791,19 @@ public boolean configureHostParams(final Map<String, String> params) {
17901791 isTungstenEnabled = Boolean .parseBoolean (params .get (NetworkOrchestrationService .TUNGSTEN_ENABLED .key ()));
17911792 }
17921793
1793- if (params .get (ReconcileCommandService .ReconcileCommandsEnabled .key ()) != null ) {
1794- isReconcileCommandsEnabled = Boolean .parseBoolean (params .get (ReconcileCommandService .ReconcileCommandsEnabled .key ()));
1794+ final String usageExemptCidrs = params .get (NetworkOrchestrationService .NetworkUsageExemptCidrs .key ());
1795+ if (usageExemptCidrs != null && hypervisorType == HypervisorType .KVM ) {
1796+ usageExemptCounters .setTapOwners (this ::getTapOwners );
1797+ usageExemptCounters .setCidrs (NetworkOrchestrationService .parseUsageExemptCidrs (usageExemptCidrs ));
17951798 }
1796- if (isReconcileCommandsEnabled ) {
1799+
1800+ final String reconcileCommandsEnabled = params .get (ReconcileCommandService .ReconcileCommandsEnabled .key ());
1801+ if (reconcileCommandsEnabled != null ) {
1802+ isReconcileCommandsEnabled = Boolean .parseBoolean (reconcileCommandsEnabled );
1803+ }
1804+ // Only on connect, which is the only time the server sends this setting. Settings pushed while the agent is
1805+ // running must leave the logs of commands still in progress alone.
1806+ if (isReconcileCommandsEnabled && reconcileCommandsEnabled != null ) {
17971807 File commandsLogPath = new File (COMMANDS_LOG_PATH );
17981808 if (!commandsLogPath .exists ()) {
17991809 commandsLogPath .mkdirs ();
@@ -5197,6 +5207,38 @@ private int executeBashScriptAndRetrieveExitValue(final String script) {
51975207 return command .getExitValue ();
51985208 }
51995209
5210+ public UsageExemptCounters getUsageExemptCounters () {
5211+ return usageExemptCounters ;
5212+ }
5213+
5214+ /**
5215+ * Maps each running VM's NIC device name to the VM's name, so the usage exemption counters can skip system VMs.
5216+ * Returns null if libvirt can't be asked.
5217+ */
5218+ protected Map <String , String > getTapOwners () {
5219+ try {
5220+ final Connect conn = LibvirtConnection .getConnection ();
5221+ final Map <String , String > owners = new HashMap <>();
5222+ for (final int id : conn .listDomains ()) {
5223+ final Domain dm = conn .domainLookupByID (id );
5224+ try {
5225+ final String vmName = dm .getName ();
5226+ for (final InterfaceDef nic : getInterfaces (conn , vmName )) {
5227+ if (nic .getDevName () != null ) {
5228+ owners .put (nic .getDevName (), vmName );
5229+ }
5230+ }
5231+ } finally {
5232+ dm .free ();
5233+ }
5234+ }
5235+ return owners ;
5236+ } catch (final LibvirtException e ) {
5237+ LOGGER .debug ("Failed to list the running VMs' NICs for the usage exemption counters" , e );
5238+ return null ;
5239+ }
5240+ }
5241+
52005242 public List <VmNetworkStatsEntry > getVmNetworkStat (Connect conn , String vmName ) throws LibvirtException {
52015243 Domain dm = null ;
52025244 try {
@@ -5209,7 +5251,8 @@ public List<VmNetworkStatsEntry> getVmNetworkStat(Connect conn, String vmName) t
52095251 for (InterfaceDef nic : nics ) {
52105252 DomainInterfaceStats nicStats = dm .interfaceStats (nic .getDevName ());
52115253 String macAddress = nic .getMacAddress ();
5212- VmNetworkStatsEntry stat = new VmNetworkStatsEntry (vmName , macAddress , nicStats .tx_bytes , nicStats .rx_bytes );
5254+ long [] usage = usageExemptCounters .subtractExempt (nic .getDevName (), nicStats .tx_bytes , nicStats .rx_bytes );
5255+ VmNetworkStatsEntry stat = new VmNetworkStatsEntry (vmName , macAddress , usage [0 ], usage [1 ]);
52135256 stats .add (stat );
52145257 }
52155258
0 commit comments