1 | /* $Id: pdmnetshaperint.h 42066 2012-07-09 16:39:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM Network Shaper - Internal data structures and functions common for both
|
---|
4 | * R0 and R3 parts.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2011-2012 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Bandwidth group instance data
|
---|
21 | */
|
---|
22 | typedef struct PDMNSBWGROUP
|
---|
23 | {
|
---|
24 | /** Pointer to the next group in the list. */
|
---|
25 | struct PDMNSBWGROUP *pNext;
|
---|
26 | /** Pointer to the shared UVM structure. */
|
---|
27 | struct PDMNETSHAPER *pShaper;
|
---|
28 | /** Critical section protecting all members below. */
|
---|
29 | PDMCRITSECT cs;
|
---|
30 | /** Pointer to the first filter attached to this group. */
|
---|
31 | struct PDMNSFILTER *pFiltersHead;
|
---|
32 | /** Bandwidth group name. */
|
---|
33 | char *pszName;
|
---|
34 | /** Maximum number of bytes filters are allowed to transfer. */
|
---|
35 | volatile uint64_t cbTransferPerSecMax;
|
---|
36 | /** Number of bytes we are allowed to transfer in one burst. */
|
---|
37 | volatile uint32_t cbBucketSize;
|
---|
38 | /** Number of bytes we were allowed to transfer at the last update. */
|
---|
39 | volatile uint32_t cbTokensLast;
|
---|
40 | /** Timestamp of the last update */
|
---|
41 | volatile uint64_t tsUpdatedLast;
|
---|
42 | /** Reference counter - How many filters are associated with this group. */
|
---|
43 | volatile uint32_t cRefs;
|
---|
44 | } PDMNSBWGROUP;
|
---|
45 | /** Pointer to a bandwidth group. */
|
---|
46 | typedef PDMNSBWGROUP *PPDMNSBWGROUP;
|
---|
47 |
|
---|
48 | DECLINLINE(bool) pdmNsAllocateBandwidth(PPDMNSFILTER pFilter, size_t cbTransfer)
|
---|
49 | {
|
---|
50 | AssertPtrReturn(pFilter, true);
|
---|
51 | if (!VALID_PTR(pFilter->CTX_SUFF(pBwGroup)))
|
---|
52 | return true;
|
---|
53 |
|
---|
54 | PPDMNSBWGROUP pBwGroup = ASMAtomicReadPtrT(&pFilter->CTX_SUFF(pBwGroup), PPDMNSBWGROUP);
|
---|
55 | int rc = PDMCritSectEnter(&pBwGroup->cs, VERR_SEM_BUSY); AssertRC(rc);
|
---|
56 | if (RT_UNLIKELY(rc == VERR_SEM_BUSY))
|
---|
57 | return true;
|
---|
58 | bool fAllowed = true;
|
---|
59 | if (pBwGroup->cbTransferPerSecMax)
|
---|
60 | {
|
---|
61 | /* Re-fill the bucket first */
|
---|
62 | uint64_t tsNow = RTTimeSystemNanoTS();
|
---|
63 | uint32_t uTokensAdded = (tsNow - pBwGroup->tsUpdatedLast)*pBwGroup->cbTransferPerSecMax/(1000*1000*1000);
|
---|
64 | uint32_t uTokens = RT_MIN(pBwGroup->cbBucketSize, uTokensAdded + pBwGroup->cbTokensLast);
|
---|
65 |
|
---|
66 | if (cbTransfer > uTokens)
|
---|
67 | {
|
---|
68 | fAllowed = false;
|
---|
69 | ASMAtomicWriteBool(&pFilter->fChoked, true);
|
---|
70 | }
|
---|
71 | else
|
---|
72 | {
|
---|
73 | pBwGroup->tsUpdatedLast = tsNow;
|
---|
74 | pBwGroup->cbTokensLast = uTokens - (uint32_t)cbTransfer;
|
---|
75 | }
|
---|
76 | Log2((LOG_FN_FMT "BwGroup=%#p{%s} cbTransfer=%u uTokens=%u uTokensAdded=%u fAllowed=%RTbool\n",
|
---|
77 | __PRETTY_FUNCTION__, pBwGroup, pBwGroup->pszName, cbTransfer, uTokens, uTokensAdded, fAllowed));
|
---|
78 | }
|
---|
79 | else
|
---|
80 | Log2((LOG_FN_FMT "BwGroup=%#p{%s} disabled fAllowed=%RTbool\n",
|
---|
81 | __PRETTY_FUNCTION__, pBwGroup, pBwGroup->pszName, fAllowed));
|
---|
82 |
|
---|
83 | rc = PDMCritSectLeave(&pBwGroup->cs); AssertRC(rc);
|
---|
84 | return fAllowed;
|
---|
85 | }
|
---|