VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PDMAllNetShaper.cpp@ 93628

最後變更 在這個檔案從93628是 93628,由 vboxsync 提交於 3 年 前

VMM/PDMNetShaper,Main,DrvNetShaper: Moved the network shaper data off the hyper heap and into the VM structure. bugref:10093 bugref:5582

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 4.4 KB
 
1/* $Id: PDMAllNetShaper.cpp 93628 2022-02-06 23:44:05Z vboxsync $ */
2/** @file
3 * PDM Network Shaper - Limit network traffic according to bandwidth group settings.
4 */
5
6/*
7 * Copyright (C) 2011-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_NET_SHAPER
23#include <VBox/vmm/pdmnetshaper.h>
24#include "PDMInternal.h"
25#include <VBox/vmm/vmcc.h>
26
27#include <VBox/log.h>
28#include <iprt/time.h>
29
30
31/**
32 * Obtain bandwidth in a bandwidth group.
33 *
34 * @returns True if bandwidth was allocated, false if not.
35 * @param pVM The cross context VM structure.
36 * @param pFilter Pointer to the filter that allocates bandwidth.
37 * @param cbTransfer Number of bytes to allocate.
38 */
39VMM_INT_DECL(bool) PDMNetShaperAllocateBandwidth(PVMCC pVM, PPDMNSFILTER pFilter, size_t cbTransfer)
40{
41 AssertPtrReturn(pFilter, true);
42
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)
49 {
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;
67
68 uint32_t const cbBucket = pGroup->cbBucket;
69 uint32_t const cbTokensLast = pGroup->cbTokensLast;
70 uint32_t const cTokens = RT_MIN(cbBucket, cTokensAdded + cbTokensLast);
71
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);
100 }
101 else
102 AssertMsgFailed(("Invalid iGroup=%d\n", iGroup));
103 }
104 return fAllowed;
105}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette