VirtualBox

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

Last change on this file since 90802 was 90784, checked in by vboxsync, 3 years ago

VMM: VALID_PTR -> RT_VALID_PTR.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1/* $Id: PDMAllNetShaper.cpp 90784 2021-08-23 09:42:32Z vboxsync $ */
2/** @file
3 * PDM Network Shaper - Limit network traffic according to bandwidth group settings.
4 */
5
6/*
7 * Copyright (C) 2011-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.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/pdm.h>
24#include <VBox/log.h>
25#include <iprt/time.h>
26
27#include <VBox/vmm/pdmnetshaper.h>
28#include "PDMNetShaperInternal.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 if (!RT_VALID_PTR(pFilter->CTX_SUFF(pBwGroup)))
43 return true;
44
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
50 {
51 if (rc == VERR_SEM_BUSY)
52 return true;
53 PDM_CRITSECT_RELEASE_ASSERT_RC(pVM, &pBwGroup->Lock, rc);
54 return false;
55 }
56
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);
64
65 if (cbTransfer > uTokens)
66 {
67 fAllowed = false;
68 ASMAtomicWriteBool(&pFilter->fChoked, true);
69 }
70 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));
77 }
78 else
79 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 return fAllowed;
84}
85
Note: See TracBrowser for help on using the repository browser.

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