VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageBandwidthControl.cpp@ 40673

Last change on this file since 40673 was 40541, checked in by vboxsync, 13 years ago

VBoxManage: removed debug printouts in bandwidthctl (#5582)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 KB
Line 
1/* $Id: VBoxManageBandwidthControl.cpp 40541 2012-03-19 14:36:29Z vboxsync $ */
2/** @file
3 * VBoxManage - The bandwidth control related commands.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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#ifndef VBOX_ONLY_DOCS
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include <VBox/com/com.h>
24#include <VBox/com/array.h>
25#include <VBox/com/ErrorInfo.h>
26#include <VBox/com/errorprint.h>
27#include <VBox/com/VirtualBox.h>
28
29#include <iprt/path.h>
30#include <iprt/param.h>
31#include <iprt/string.h>
32#include <iprt/ctype.h>
33#include <iprt/stream.h>
34#include <iprt/getopt.h>
35#include <VBox/log.h>
36
37#include "VBoxManage.h"
38using namespace com;
39
40
41// funcs
42///////////////////////////////////////////////////////////////////////////////
43
44
45/**
46 * Handles the 'bandwidthctl myvm add' sub-command.
47 * @returns Exit code.
48 * @param a The handler argument package.
49 * @param bwCtrl Reference to the bandwidth control interface.
50 */
51static RTEXITCODE handleBandwidthControlAdd(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
52{
53 HRESULT rc = S_OK;
54 static const RTGETOPTDEF g_aBWCtlAddOptions[] =
55 {
56 { "--type", 't', RTGETOPT_REQ_STRING },
57 { "--limit", 'l', RTGETOPT_REQ_UINT32 }
58 };
59
60
61 Bstr name(a->argv[2]);
62 const char *pszType = NULL;
63 ULONG cMaxMbPerSec = UINT32_MAX;
64
65 int c;
66 RTGETOPTUNION ValueUnion;
67 RTGETOPTSTATE GetState;
68 RTGetOptInit(&GetState, a->argc, a->argv, g_aBWCtlAddOptions,
69 RT_ELEMENTS(g_aBWCtlAddOptions), 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
70
71 while ( SUCCEEDED(rc)
72 && (c = RTGetOpt(&GetState, &ValueUnion)))
73 {
74 switch (c)
75 {
76 case 't': // bandwidth group type
77 {
78 if (ValueUnion.psz)
79 pszType = ValueUnion.psz;
80 else
81 rc = E_FAIL;
82 break;
83 }
84
85 case 'l': // limit
86 {
87 cMaxMbPerSec = ValueUnion.u32;
88 break;
89 }
90
91 default:
92 {
93 errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
94 rc = E_FAIL;
95 break;
96 }
97 }
98 }
99
100 BandwidthGroupType_T enmType;
101
102 if (!RTStrICmp(pszType, "disk"))
103 enmType = BandwidthGroupType_Disk;
104 else if (!RTStrICmp(pszType, "network"))
105 enmType = BandwidthGroupType_Network;
106 else
107 {
108 errorArgument("Invalid bandwidth group type\n");
109 return RTEXITCODE_FAILURE;
110 }
111
112 CHECK_ERROR2_RET(bwCtrl, CreateBandwidthGroup(name.raw(), enmType, cMaxMbPerSec), RTEXITCODE_FAILURE);
113
114 return RTEXITCODE_SUCCESS;
115}
116
117/**
118 * Handles the 'bandwidthctl myvm set' sub-command.
119 * @returns Exit code.
120 * @param a The handler argument package.
121 * @param bwCtrl Reference to the bandwidth control interface.
122 */
123static RTEXITCODE handleBandwidthControlSet(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
124{
125 HRESULT rc = S_OK;
126 static const RTGETOPTDEF g_aBWCtlAddOptions[] =
127 {
128 { "--limit", 'l', RTGETOPT_REQ_UINT32 }
129 };
130
131
132 Bstr name(a->argv[2]);
133 ULONG cMaxMbPerSec = UINT32_MAX;
134
135 int c;
136 RTGETOPTUNION ValueUnion;
137 RTGETOPTSTATE GetState;
138 RTGetOptInit(&GetState, a->argc, a->argv, g_aBWCtlAddOptions,
139 RT_ELEMENTS(g_aBWCtlAddOptions), 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
140
141 while ( SUCCEEDED(rc)
142 && (c = RTGetOpt(&GetState, &ValueUnion)))
143 {
144 switch (c)
145 {
146 case 'l': // limit
147 {
148 cMaxMbPerSec = ValueUnion.u32;
149 break;
150 }
151
152 default:
153 {
154 errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
155 rc = E_FAIL;
156 break;
157 }
158 }
159 }
160
161
162 if (cMaxMbPerSec != UINT32_MAX)
163 {
164 ComPtr<IBandwidthGroup> bwGroup;
165 CHECK_ERROR2_RET(bwCtrl, GetBandwidthGroup(name.raw(), bwGroup.asOutParam()), RTEXITCODE_FAILURE);
166 if (SUCCEEDED(rc))
167 CHECK_ERROR2_RET(bwGroup, COMSETTER(MaxMbPerSec)(cMaxMbPerSec), RTEXITCODE_FAILURE);
168 }
169
170 return RTEXITCODE_SUCCESS;
171}
172
173/**
174 * Handles the 'bandwidthctl myvm remove' sub-command.
175 * @returns Exit code.
176 * @param a The handler argument package.
177 * @param bwCtrl Reference to the bandwidth control interface.
178 */
179static RTEXITCODE handleBandwidthControlRemove(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
180{
181 Bstr name(a->argv[2]);
182 CHECK_ERROR2_RET(bwCtrl, DeleteBandwidthGroup(name.raw()), RTEXITCODE_FAILURE);
183 return RTEXITCODE_SUCCESS;
184}
185
186/**
187 * Handles the 'bandwidthctl myvm list' sub-command.
188 * @returns Exit code.
189 * @param a The handler argument package.
190 * @param bwCtrl Reference to the bandwidth control interface.
191 */
192static RTEXITCODE handleBandwidthControlList(HandlerArg *pArgs, ComPtr<IBandwidthControl> &rptrBWControl)
193{
194 static const RTGETOPTDEF g_aOptions[] =
195 {
196 { "--machinereadable", 'M', RTGETOPT_REQ_NOTHING },
197 };
198
199 VMINFO_DETAILS enmDetails = VMINFO_STANDARD;
200
201 int c;
202 RTGETOPTUNION ValueUnion;
203 RTGETOPTSTATE GetState;
204 RTGetOptInit(&GetState, pArgs->argc, pArgs->argv, g_aOptions, RT_ELEMENTS(g_aOptions), 2 /*iArg*/, 0 /*fFlags*/);
205 while ((c = RTGetOpt(&GetState, &ValueUnion)))
206 {
207 switch (c)
208 {
209 case 'M': enmDetails = VMINFO_MACHINEREADABLE; break;
210 default: return errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
211 }
212 }
213
214 /* See showVMInfo. */
215 if (FAILED(showBandwidthGroups(rptrBWControl, enmDetails)))
216 return RTEXITCODE_FAILURE;
217
218 return RTEXITCODE_SUCCESS;
219}
220
221
222/**
223 * Handles the 'bandwidthctl' command.
224 * @returns Exit code.
225 * @param a The handler argument package.
226 */
227int handleBandwidthControl(HandlerArg *a)
228{
229 int c = VERR_INTERNAL_ERROR; /* initialized to shut up gcc */
230 HRESULT rc = S_OK;
231 ComPtr<IMachine> machine;
232 ComPtr<IBandwidthControl> bwCtrl;
233
234 if (a->argc < 2)
235 return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too few parameters");
236 else if (a->argc > 7)
237 return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too many parameters");
238
239 /* try to find the given machine */
240 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
241 machine.asOutParam()), 1);
242
243 /* open a session for the VM (new or shared) */
244 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
245 SessionType_T st;
246 CHECK_ERROR_RET(a->session, COMGETTER(Type)(&st), 1);
247 bool fRunTime = (st == SessionType_Shared);
248
249 /* get the mutable session machine */
250 a->session->COMGETTER(Machine)(machine.asOutParam());
251 rc = machine->COMGETTER(BandwidthControl)(bwCtrl.asOutParam());
252 if (FAILED(rc)) goto leave;
253
254 if (!strcmp(a->argv[1], "add"))
255 {
256 if (fRunTime)
257 {
258 errorArgument("Bandwidth groups cannot be created while the VM is running\n");
259 goto leave;
260 }
261 rc = handleBandwidthControlAdd(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
262 }
263 else if (!strcmp(a->argv[1], "remove"))
264 {
265 if (fRunTime)
266 {
267 errorArgument("Bandwidth groups cannot be deleted while the VM is running\n");
268 goto leave;
269 }
270 rc = handleBandwidthControlRemove(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
271 }
272 else if (!strcmp(a->argv[1], "set"))
273 rc = handleBandwidthControlSet(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
274 else if (!strcmp(a->argv[1], "list"))
275 rc = handleBandwidthControlList(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
276 else
277 {
278 errorSyntax(USAGE_BANDWIDTHCONTROL, "Invalid parameter '%s'", Utf8Str(a->argv[1]).c_str());
279 rc = E_FAIL;
280 }
281
282 /* commit changes */
283 if (SUCCEEDED(rc))
284 CHECK_ERROR(machine, SaveSettings());
285
286leave:
287 /* it's important to always close sessions */
288 a->session->UnlockMachine();
289
290 return SUCCEEDED(rc) ? 0 : 1;
291}
292
293#endif /* !VBOX_ONLY_DOCS */
294
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