PDMNetShaper.cpp revision 404dd7b4bf7209363c3ab5b98944ff8405e0130b
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * PDM Network Shaper - Limit network traffic according to bandwidth
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * group settings.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Copyright (C) 2006-2012 Oracle Corporation
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * available from http://www.virtualbox.org. This file is free software;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * you can redistribute it and/or modify it under the terms of the GNU
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * General Public License (GPL) as published by the Free Software
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Header Files *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Structures and Typedefs *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Bandwidth group instance data
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsynctypedef struct PDMNSBWGROUP
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the next group in the list. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the shared UVM structure. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Critical section protecting all members below. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the first filter attached to this group. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Bandwidth group name. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Maximum number of bytes filters are allowed to transfer. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Number of bytes we are allowed to transfer in one burst. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Number of bytes we were allowed to transfer at the last update. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Timestamp of the last update */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Reference counter - How many filters are associated with this group. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** Pointer to a bandwidth group. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Network shaper data. One instance per VM.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsynctypedef struct PDMNETSHAPER
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the VM. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Critical section protecting all members below. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pending TX thread. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the first bandwidth group. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Internal Functions *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic PPDMNSBWGROUP pdmNsBwGroupFindById(PPDMNETSHAPER pShaper, const char *pcszId)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectEnter(&pShaper->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic void pdmNsBwGroupLink(PPDMNSBWGROUP pBwGroup)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectEnter(&pShaper->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic void pdmNsBwGroupUnlink(PPDMNSBWGROUP pBwGroup)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectEnter(&pShaper->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic void pdmNsBwGroupSetLimit(PPDMNSBWGROUP pBwGroup, uint32_t cbTransferPerSecMax)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pBwGroup->cbTransferPerSecMax = cbTransferPerSecMax;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pBwGroup->cbBucketSize = RT_MAX(PDM_NETSHAPER_MIN_BUCKET_SIZE,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync cbTransferPerSecMax * PDM_NETSHAPER_MAX_LATENCY / 1000);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlowFunc(("New rate limit is %d bytes per second, adjusted bucket size to %d bytes\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pBwGroup->cbTransferPerSecMax, pBwGroup->cbBucketSize));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int pdmNsBwGroupCreate(PPDMNETSHAPER pShaper, const char *pcszBwGroup, uint32_t cbTransferPerSecMax)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlowFunc(("pShaper=%#p pcszBwGroup=%#p{%s} cbTransferPerSecMax=%u\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pShaper, pcszBwGroup, pcszBwGroup, cbTransferPerSecMax));
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync AssertPtrReturn(pcszBwGroup, VERR_INVALID_POINTER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertReturn(*pcszBwGroup != '\0', VERR_INVALID_PARAMETER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMNSBWGROUP pBwGroup = pdmNsBwGroupFindById(pShaper, pcszBwGroup);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = MMR3HeapAllocZEx(pShaper->pVM, MM_TAG_PDM_NET_SHAPER,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pdmNsBwGroupSetLimit(pBwGroup, cbTransferPerSecMax);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic void pdmNsBwGroupTerminate(PPDMNSBWGROUP pBwGroup)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncDECLINLINE(void) pdmNsBwGroupRef(PPDMNSBWGROUP pBwGroup)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncDECLINLINE(void) pdmNsBwGroupUnref(PPDMNSBWGROUP pBwGroup)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic void pdmNsBwGroupXmitPending(PPDMNSBWGROUP pBwGroup)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * We don't need to hold the bandwidth group lock to iterate over the list
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * of filters since the filters are removed while the shaper lock is being
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync //int rc = RTCritSectEnter(&pBwGroup->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync bool fChoked = ASMAtomicXchgBool(&pFilter->fChoked, false);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync Log3((LOG_FN_FMT ": pFilter=%#p fChoked=%RTbool\n", __PRETTY_FUNCTION__, pFilter, fChoked));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlowFunc(("Calling pfnXmitPending for pFilter=%#p\n", pFilter));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pFilter->pIDrvNet->pfnXmitPending(pFilter->pIDrvNet);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync //rc = RTCritSectLeave(&pBwGroup->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectEnter(&pBwGroup->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * We need to make sure we hold the shaper lock since pdmNsBwGroupXmitPending()
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * does not hold the bandwidth group lock while iterating over the list
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * of group's filters.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectEnter(&pBwGroup->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncVMMR3DECL(int) PDMR3NsAttach(PVM pVM, PPDMDRVINS pDrvIns, const char *pcszBwGroup,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertReturn(pFilter->pBwGroupR3 == NULL, VERR_ALREADY_EXISTS);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectEnter(&pShaper->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pBwGroupNew = pdmNsBwGroupFindById(pShaper, pcszBwGroup);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pBwGroupOld = ASMAtomicXchgPtrT(&pFilter->pBwGroupR3, pBwGroupNew, PPDMNSBWGROUP);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc2 = RTCritSectLeave(&pShaper->cs); AssertRC(rc2);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncVMMR3DECL(int) PDMR3NsDetach(PVM pVM, PPDMDRVINS pDrvIns, PPDMNSFILTER pFilter)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertPtrReturn(pFilter->pBwGroupR3, VERR_INVALID_POINTER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectEnter(&pShaper->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pBwGroup = ASMAtomicXchgPtrT(&pFilter->pBwGroupR3, NULL, PPDMNSBWGROUP);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc2 = RTCritSectLeave(&pShaper->cs); AssertRC(rc2);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncVMMR3DECL(bool) PDMR3NsAllocateBandwidth(PPDMNSFILTER pFilter, uint32_t cbTransfer)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return true;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMNSBWGROUP pBwGroup = ASMAtomicReadPtrT(&pFilter->pBwGroupR3, PPDMNSBWGROUP);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectEnter(&pBwGroup->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync bool fAllowed = true;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Re-fill the bucket first */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint32_t uTokensAdded = (tsNow - pBwGroup->tsUpdatedLast)*pBwGroup->cbTransferPerSecMax/(1000*1000*1000);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint32_t uTokens = RT_MIN(pBwGroup->cbBucketSize, uTokensAdded + pBwGroup->cbTokensLast);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync Log2((LOG_FN_FMT "BwGroup=%#p{%s} cbTransfer=%u uTokens=%u uTokensAdded=%u fAllowed=%RTbool\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync __PRETTY_FUNCTION__, pBwGroup, pBwGroup->pszName, cbTransfer, uTokens, uTokensAdded, fAllowed));
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsyncVMMR3DECL(int) PDMR3NsBwGroupSetLimit(PVM pVM, const char *pcszBwGroup, uint32_t cbTransferPerSecMax)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectEnter(&pShaper->cs); AssertRC(rc);
a39ea3668b7019c23a68936259545f9b71bce1aavboxsync PPDMNSBWGROUP pBwGroup = pdmNsBwGroupFindById(pShaper, pcszBwGroup);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pdmNsBwGroupSetLimit(pBwGroup, cbTransferPerSecMax);
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync /* Drop extra tokens */
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync if (pBwGroup->cbTokensLast > pBwGroup->cbBucketSize)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * I/O thread for pending TX.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @returns VINF_SUCCESS (ignored).
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pVM Pointer to the VM.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pThread The PDM thread data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic DECLCALLBACK(int) pdmR3NsTxThread(PVM pVM, PPDMTHREAD pThread)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMNETSHAPER pShaper = (PPDMNETSHAPER)pThread->pvUser;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("pdmR3NsTxThread: pShaper=%p\n", pShaper));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync while (pThread->enmState == PDMTHREADSTATE_RUNNING)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Go over all bandwidth groups/filters calling pfnXmitPending */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectEnter(&pShaper->cs); AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @copydoc FNPDMTHREADWAKEUPINT
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic DECLCALLBACK(int) pdmR3NsTxWakeUp(PVM pVM, PPDMTHREAD pThread)
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync PPDMNETSHAPER pShaper = (PPDMNETSHAPER)pThread->pvUser;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("pdmR3NsTxWakeUp: pShaper=%p\n", pShaper));
5530d99d77afedf5676a976e5135c89478b4e8d0vboxsync /* Nothing to do */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Terminate the network shaper.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @returns VBox error code.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pVM Pointer to VM.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @remarks This method destroys all bandwidth group objects.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Destroy the bandwidth managers. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Initialize the network shaper.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @returns VBox status code
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pVM Pointer to the VM.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_NET_SHAPER,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PCFGMNODE pCfgNetShaper = CFGMR3GetChild(CFGMR3GetChild(pCfgRoot, "PDM"), "NetworkShaper");
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Create all bandwidth groups. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PCFGMNODE pCfgBwGrp = CFGMR3GetChild(pCfgNetShaper, "BwGroups");
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync for (PCFGMNODE pCur = CFGMR3GetFirstChild(pCfgBwGrp); pCur; pCur = CFGMR3GetNextChild(pCur))
cba6719bd64ec749967bbe931230452664109857vboxsync rc = pdmNsBwGroupCreate(pNetShaper, pszBwGrpId, cbMax);
static unsigned iThread;
return VINF_SUCCESS;
return rc;