Changeset 93628 in vbox for trunk/src/VBox/VMM/VMMAll/PDMAllNetShaper.cpp
- Timestamp:
- Feb 6, 2022 11:44:05 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/VMM/VMMAll/PDMAllNetShaper.cpp
r93115 r93628 21 21 *********************************************************************************************************************************/ 22 22 #define LOG_GROUP LOG_GROUP_NET_SHAPER 23 #include <VBox/vmm/pdm.h> 23 #include <VBox/vmm/pdmnetshaper.h> 24 #include "PDMInternal.h" 25 #include <VBox/vmm/vmcc.h> 26 24 27 #include <VBox/log.h> 25 28 #include <iprt/time.h> 26 27 #include <VBox/vmm/pdmnetshaper.h>28 #include "PDMNetShaperInternal.h"29 29 30 30 … … 40 40 { 41 41 AssertPtrReturn(pFilter, true); 42 if (!RT_VALID_PTR(pFilter->CTX_SUFF(pBwGroup)))43 return true;44 42 45 PPDMNSBWGROUP pBwGroup = ASMAtomicReadPtrT(&pFilter->CTX_SUFF(pBwGroup), PPDMNSBWGROUP); 46 int rc = PDMCritSectEnter(pVM, &pBwGroup->Lock, VERR_SEM_BUSY); AssertRC(rc); 47 if (RT_SUCCESS(rc)) 48 { /* likely */ } 49 else 43 /* 44 * If we haven't got a valid bandwidth group, we always allow the traffic. 45 */ 46 bool fAllowed = true; 47 uint32_t iGroup = ASMAtomicUoReadU32(&pFilter->iGroup); 48 if (iGroup != 0) 50 49 { 51 if (rc == VERR_SEM_BUSY) 52 return true; 53 PDM_CRITSECT_RELEASE_ASSERT_RC(pVM, &pBwGroup->Lock, rc); 54 return false; 55 } 50 if (iGroup <= RT_MIN(pVM->pdm.s.cNsGroups, RT_ELEMENTS(pVM->pdm.s.aNsGroups))) 51 { 52 PPDMNSBWGROUP pGroup = &pVM->pdm.s.aNsGroups[iGroup - 1]; 53 int rc = PDMCritSectEnter(pVM, &pGroup->Lock, VINF_TRY_AGAIN); 54 if (rc == VINF_SUCCESS) 55 { 56 uint64_t const cbPerSecMax = pGroup->cbPerSecMax; 57 if (cbPerSecMax > 0) 58 { 59 /* 60 * Re-fill the bucket first 61 */ 62 uint64_t const tsNow = RTTimeSystemNanoTS(); 63 uint64_t const cNsDelta = tsNow - pGroup->tsUpdatedLast; 64 /** @todo r=bird: there might be an overflow issue here if the gap 65 * between two transfers is too large. */ 66 uint32_t cTokensAdded = cNsDelta * cbPerSecMax / RT_NS_1SEC; 56 67 57 bool fAllowed = true; 58 if (pBwGroup->cbPerSecMax) 59 { 60 /* Re-fill the bucket first */ 61 uint64_t tsNow = RTTimeSystemNanoTS(); 62 uint32_t uTokensAdded = (tsNow - pBwGroup->tsUpdatedLast) * pBwGroup->cbPerSecMax / (1000 * 1000 * 1000); 63 uint32_t uTokens = RT_MIN(pBwGroup->cbBucket, uTokensAdded + pBwGroup->cbTokensLast); 68 uint32_t const cbBucket = pGroup->cbBucket; 69 uint32_t const cbTokensLast = pGroup->cbTokensLast; 70 uint32_t const cTokens = RT_MIN(cbBucket, cTokensAdded + cbTokensLast); 64 71 65 if (cbTransfer > uTokens) 66 { 67 fAllowed = false; 68 ASMAtomicWriteBool(&pFilter->fChoked, true); 72 /* 73 * Allowed? 74 */ 75 if (cbTransfer <= cTokens) 76 { 77 pGroup->cbTokensLast = cTokens - (uint32_t)cbTransfer; 78 pGroup->tsUpdatedLast = tsNow; 79 Log2(("pdmNsAllocateBandwidth/%s: allowed - cbTransfer=%#zx cTokens=%#x cTokensAdded=%#x\n", 80 pGroup->szName, cbTransfer, cTokens, cTokensAdded)); 81 } 82 else 83 { 84 ASMAtomicWriteBool(&pFilter->fChoked, true); 85 Log2(("pdmNsAllocateBandwidth/%s: refused - cbTransfer=%#zx cTokens=%#x cTokensAdded=%#x\n", 86 pGroup->szName, cbTransfer, cTokens, cTokensAdded)); 87 fAllowed = false; 88 } 89 } 90 else 91 Log2(("pdmNsAllocateBandwidth/%s: disabled\n", pGroup->szName)); 92 93 rc = PDMCritSectLeave(pVM, &pGroup->Lock); 94 AssertRCSuccess(rc); 95 } 96 else if (rc == VINF_TRY_AGAIN) /* (accounted for by the critsect stats) */ 97 Log2(("pdmNsAllocateBandwidth/%s: allowed - lock contention\n", pGroup->szName)); 98 else 99 PDM_CRITSECT_RELEASE_ASSERT_RC(pVM, &pGroup->Lock, rc); 69 100 } 70 101 else 71 { 72 pBwGroup->tsUpdatedLast = tsNow; 73 pBwGroup->cbTokensLast = uTokens - (uint32_t)cbTransfer; 74 } 75 Log2(("pdmNsAllocateBandwidth: BwGroup=%#p{%s} cbTransfer=%u uTokens=%u uTokensAdded=%u fAllowed=%RTbool\n", 76 pBwGroup, R3STRING(pBwGroup->pszNameR3), cbTransfer, uTokens, uTokensAdded, fAllowed)); 102 AssertMsgFailed(("Invalid iGroup=%d\n", iGroup)); 77 103 } 78 else79 Log2(("pdmNsAllocateBandwidth: BwGroup=%#p{%s} disabled fAllowed=%RTbool\n",80 pBwGroup, R3STRING(pBwGroup->pszNameR3), fAllowed));81 82 rc = PDMCritSectLeave(pVM, &pBwGroup->Lock); AssertRC(rc);83 104 return fAllowed; 84 105 } 85
Note:
See TracChangeset
for help on using the changeset viewer.