VBoxManageBandwidthControl.cpp revision e67a57a63d60b971a077d325eb0872a3ccde94a4
63b785c3291332a86a9bc473e68f08121368898bvboxsync * VBoxManage - The bandwidth control related commands.
63b785c3291332a86a9bc473e68f08121368898bvboxsync * Copyright (C) 2006-2010 Oracle Corporation
63b785c3291332a86a9bc473e68f08121368898bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
63b785c3291332a86a9bc473e68f08121368898bvboxsync * available from http://www.virtualbox.org. This file is free software;
63b785c3291332a86a9bc473e68f08121368898bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
63b785c3291332a86a9bc473e68f08121368898bvboxsync * General Public License (GPL) as published by the Free Software
63b785c3291332a86a9bc473e68f08121368898bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
63b785c3291332a86a9bc473e68f08121368898bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
63b785c3291332a86a9bc473e68f08121368898bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
63b785c3291332a86a9bc473e68f08121368898bvboxsync/*******************************************************************************
63b785c3291332a86a9bc473e68f08121368898bvboxsync* Header Files *
63b785c3291332a86a9bc473e68f08121368898bvboxsync*******************************************************************************/
63b785c3291332a86a9bc473e68f08121368898bvboxsyncusing namespace com;
63b785c3291332a86a9bc473e68f08121368898bvboxsync///////////////////////////////////////////////////////////////////////////////
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * Handles the 'bandwidthctl myvm add' sub-command.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @returns Exit code.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @param a The handler argument package.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @param bwCtrl Reference to the bandwidth control interface.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsyncstatic RTEXITCODE handleBandwidthControlAdd(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync RTGetOptInit(&GetState, a->argc, a->argv, g_aBWCtlAddOptions,
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync RT_ELEMENTS(g_aBWCtlAddOptions), 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync RTPrintf("Adding bwgroup: name=%ls type=%s limit=%u\n", name.raw(), pszType, cMaxMbPerSec);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR2_RET(bwCtrl, CreateBandwidthGroup(name.raw(), enmType, cMaxMbPerSec), RTEXITCODE_FAILURE);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * Handles the 'bandwidthctl myvm set' sub-command.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @returns Exit code.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @param a The handler argument package.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @param bwCtrl Reference to the bandwidth control interface.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsyncstatic RTEXITCODE handleBandwidthControlSet(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync RTGetOptInit(&GetState, a->argc, a->argv, g_aBWCtlAddOptions,
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync RT_ELEMENTS(g_aBWCtlAddOptions), 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
63b785c3291332a86a9bc473e68f08121368898bvboxsync errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync RTPrintf("Updating bwgroup: name=%ls limit=%u\n", name.raw(), cMaxMbPerSec);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR2_RET(bwCtrl, GetBandwidthGroup(name.raw(), bwGroup.asOutParam()), RTEXITCODE_FAILURE);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR2_RET(bwGroup, COMSETTER(MaxMbPerSec)(cMaxMbPerSec), RTEXITCODE_FAILURE);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * Handles the 'bandwidthctl myvm remove' sub-command.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @returns Exit code.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @param a The handler argument package.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @param bwCtrl Reference to the bandwidth control interface.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsyncstatic RTEXITCODE handleBandwidthControlRemove(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR2_RET(bwCtrl, DeleteBandwidthGroup(name.raw()), RTEXITCODE_FAILURE);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * Converts bandwidth group type to a string.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @returns String representation.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @param enmType Bandwidth control group type.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsyncinline const char * typeToString(BandwidthGroupType_T enmType)
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync return "unknown";
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * Handles the 'bandwidthctl myvm list' sub-command.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @returns Exit code.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @param a The handler argument package.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @param bwCtrl Reference to the bandwidth control interface.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsyncstatic RTEXITCODE handleBandwidthControlList(HandlerArg *pArgs, ComPtr<IBandwidthControl> &rptrBWControl)
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync { "--machinereadable", 'M', RTGETOPT_REQ_NOTHING },
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync RTGetOptInit(&GetState, pArgs->argc, pArgs->argv, g_aOptions, RT_ELEMENTS(g_aOptions), 2 /*iArg*/, 0 /*fFlags*/);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync case 'M': enmDetails = VMINFO_MACHINEREADABLE; break;
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync default: return errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync /* See showVMInfo. */
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR2_RET(rptrBWControl, GetAllBandwidthGroups(ComSafeArrayAsOutParam(bwGroups)), RTEXITCODE_FAILURE);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR2_RET(bwGroups[i], COMGETTER(Name)(strName.asOutParam()), RTEXITCODE_FAILURE);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR2_RET(bwGroups[i], COMGETTER(Type)(&enmType), RTEXITCODE_FAILURE);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR2_RET(bwGroups[i], COMGETTER(MaxMbPerSec)(&cMaxMbPerSec), RTEXITCODE_FAILURE);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync "Group=%ls\nType=%s\nLimit=%u\n\n" :
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync "%-30ls %-10s %u MBytes/sec\n",
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * Handles the 'bandwidthctl' command.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @returns Exit code.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync * @param a The handler argument package.
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync int c = VERR_INTERNAL_ERROR; /* initialized to shut up gcc */
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too few parameters");
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too many parameters");
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync /* try to find the given machine */
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync /* open a session for the VM (new or shared) */
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync CHECK_ERROR_RET(a->session, COMGETTER(Type)(&st), 1);
63b785c3291332a86a9bc473e68f08121368898bvboxsync /* get the mutable session machine */
63b785c3291332a86a9bc473e68f08121368898bvboxsync a->session->COMGETTER(Machine)(machine.asOutParam());
63b785c3291332a86a9bc473e68f08121368898bvboxsync rc = machine->COMGETTER(BandwidthControl)(bwCtrl.asOutParam());
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync errorArgument("Bandwidth groups cannot be created while the VM is running\n");
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync rc = handleBandwidthControlAdd(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync errorArgument("Bandwidth groups cannot be deleted while the VM is running\n");
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync rc = handleBandwidthControlRemove(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync rc = handleBandwidthControlSet(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync rc = handleBandwidthControlList(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
e67a57a63d60b971a077d325eb0872a3ccde94a4vboxsync errorSyntax(USAGE_BANDWIDTHCONTROL, "Invalid parameter '%s'", Utf8Str(a->argv[1]).c_str());
63b785c3291332a86a9bc473e68f08121368898bvboxsync /* commit changes */
63b785c3291332a86a9bc473e68f08121368898bvboxsync /* it's important to always close sessions */
63b785c3291332a86a9bc473e68f08121368898bvboxsync#endif /* !VBOX_ONLY_DOCS */