VirtualBox

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

Last change on this file since 43421 was 41884, checked in by vboxsync, 12 years ago

addendum to r78736

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1/* $Id: VBoxManageBandwidthControl.cpp 41884 2012-06-22 12:13:12Z 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 * Parses a string in the following format "n[k|m|g|K|M|G]". Stores the value
47 * of n expressed in bytes to *pLimit. k meas kilobit, while K means kilobyte.
48 *
49 * @returns Error message or NULL if successful.
50 * @param pcszLimit The string to parse.
51 * @param pLimit Where to store the result.
52 */
53static const char *parseLimit(const char *pcszLimit, int64_t *pLimit)
54{
55 int iMultiplier = _1M;
56 char *pszNext = NULL;
57 int rc = RTStrToInt64Ex(pcszLimit, &pszNext, 10, pLimit);
58
59 switch (rc)
60 {
61 case VINF_SUCCESS:
62 break;
63 case VWRN_NUMBER_TOO_BIG:
64 return "Limit is too big\n";
65 case VWRN_TRAILING_CHARS:
66 switch (*pszNext)
67 {
68 case 'G': iMultiplier = _1G; break;
69 case 'M': iMultiplier = _1M; break;
70 case 'K': iMultiplier = _1K; break;
71 case 'g': iMultiplier = 125000000; break;
72 case 'm': iMultiplier = 125000; break;
73 case 'k': iMultiplier = 125; break;
74 default: return "Invalid unit suffix. Valid suffixes are: k, m, g, K, M, G\n";
75 }
76 break;
77 case VWRN_TRAILING_SPACES:
78 return "Trailing spaces in limit!\n";
79 case VERR_NO_DIGITS:
80 return "No digits in limit specifier\n";
81 default:
82 return "Invalid limit specifier\n";
83 }
84 if (*pLimit < 0)
85 return "Limit cannot be negative\n";
86 if (*pLimit > INT64_MAX / iMultiplier)
87 return "Limit is too big\n";
88 *pLimit *= iMultiplier;
89
90 return NULL;
91}
92
93/**
94 * Handles the 'bandwidthctl myvm add' sub-command.
95 * @returns Exit code.
96 * @param a The handler argument package.
97 * @param bwCtrl Reference to the bandwidth control interface.
98 */
99static RTEXITCODE handleBandwidthControlAdd(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
100{
101 HRESULT rc = S_OK;
102 static const RTGETOPTDEF g_aBWCtlAddOptions[] =
103 {
104 { "--type", 't', RTGETOPT_REQ_STRING },
105 { "--limit", 'l', RTGETOPT_REQ_STRING }
106 };
107
108
109 Bstr name(a->argv[2]);
110 const char *pszType = NULL;
111 int64_t cMaxBytesPerSec = INT64_MAX;
112
113 int c;
114 RTGETOPTUNION ValueUnion;
115 RTGETOPTSTATE GetState;
116 RTGetOptInit(&GetState, a->argc, a->argv, g_aBWCtlAddOptions,
117 RT_ELEMENTS(g_aBWCtlAddOptions), 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
118
119 while ( SUCCEEDED(rc)
120 && (c = RTGetOpt(&GetState, &ValueUnion)))
121 {
122 switch (c)
123 {
124 case 't': // bandwidth group type
125 {
126 if (ValueUnion.psz)
127 pszType = ValueUnion.psz;
128 else
129 rc = E_FAIL;
130 break;
131 }
132
133 case 'l': // limit
134 {
135 if (ValueUnion.psz)
136 {
137 const char *pcszError = parseLimit(ValueUnion.psz, &cMaxBytesPerSec);
138 if (pcszError)
139 {
140 errorArgument(pcszError);
141 return RTEXITCODE_FAILURE;
142 }
143 }
144 else
145 rc = E_FAIL;
146 break;
147 }
148
149 default:
150 {
151 errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
152 rc = E_FAIL;
153 break;
154 }
155 }
156 }
157
158 BandwidthGroupType_T enmType;
159
160 if (!RTStrICmp(pszType, "disk"))
161 enmType = BandwidthGroupType_Disk;
162 else if (!RTStrICmp(pszType, "network"))
163 enmType = BandwidthGroupType_Network;
164 else
165 {
166 errorArgument("Invalid bandwidth group type\n");
167 return RTEXITCODE_FAILURE;
168 }
169
170 CHECK_ERROR2_RET(bwCtrl, CreateBandwidthGroup(name.raw(), enmType, (LONG64)cMaxBytesPerSec), RTEXITCODE_FAILURE);
171
172 return RTEXITCODE_SUCCESS;
173}
174
175/**
176 * Handles the 'bandwidthctl myvm set' sub-command.
177 * @returns Exit code.
178 * @param a The handler argument package.
179 * @param bwCtrl Reference to the bandwidth control interface.
180 */
181static RTEXITCODE handleBandwidthControlSet(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
182{
183 HRESULT rc = S_OK;
184 static const RTGETOPTDEF g_aBWCtlAddOptions[] =
185 {
186 { "--limit", 'l', RTGETOPT_REQ_STRING }
187 };
188
189
190 Bstr name(a->argv[2]);
191 int64_t cMaxBytesPerSec = INT64_MAX;
192
193 int c;
194 RTGETOPTUNION ValueUnion;
195 RTGETOPTSTATE GetState;
196 RTGetOptInit(&GetState, a->argc, a->argv, g_aBWCtlAddOptions,
197 RT_ELEMENTS(g_aBWCtlAddOptions), 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
198
199 while ( SUCCEEDED(rc)
200 && (c = RTGetOpt(&GetState, &ValueUnion)))
201 {
202 switch (c)
203 {
204 case 'l': // limit
205 {
206 if (ValueUnion.psz)
207 {
208 const char *pcszError = parseLimit(ValueUnion.psz, &cMaxBytesPerSec);
209 if (pcszError)
210 {
211 errorArgument(pcszError);
212 return RTEXITCODE_FAILURE;
213 }
214 }
215 else
216 rc = E_FAIL;
217 break;
218 }
219
220 default:
221 {
222 errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
223 rc = E_FAIL;
224 break;
225 }
226 }
227 }
228
229
230 if (cMaxBytesPerSec != INT64_MAX)
231 {
232 ComPtr<IBandwidthGroup> bwGroup;
233 CHECK_ERROR2_RET(bwCtrl, GetBandwidthGroup(name.raw(), bwGroup.asOutParam()), RTEXITCODE_FAILURE);
234 if (SUCCEEDED(rc))
235 CHECK_ERROR2_RET(bwGroup, COMSETTER(MaxBytesPerSec)((LONG64)cMaxBytesPerSec), RTEXITCODE_FAILURE);
236 }
237
238 return RTEXITCODE_SUCCESS;
239}
240
241/**
242 * Handles the 'bandwidthctl myvm remove' sub-command.
243 * @returns Exit code.
244 * @param a The handler argument package.
245 * @param bwCtrl Reference to the bandwidth control interface.
246 */
247static RTEXITCODE handleBandwidthControlRemove(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
248{
249 Bstr name(a->argv[2]);
250 CHECK_ERROR2_RET(bwCtrl, DeleteBandwidthGroup(name.raw()), RTEXITCODE_FAILURE);
251 return RTEXITCODE_SUCCESS;
252}
253
254/**
255 * Handles the 'bandwidthctl myvm list' sub-command.
256 * @returns Exit code.
257 * @param a The handler argument package.
258 * @param bwCtrl Reference to the bandwidth control interface.
259 */
260static RTEXITCODE handleBandwidthControlList(HandlerArg *pArgs, ComPtr<IBandwidthControl> &rptrBWControl)
261{
262 static const RTGETOPTDEF g_aOptions[] =
263 {
264 { "--machinereadable", 'M', RTGETOPT_REQ_NOTHING },
265 };
266
267 VMINFO_DETAILS enmDetails = VMINFO_STANDARD;
268
269 int c;
270 RTGETOPTUNION ValueUnion;
271 RTGETOPTSTATE GetState;
272 RTGetOptInit(&GetState, pArgs->argc, pArgs->argv, g_aOptions, RT_ELEMENTS(g_aOptions), 2 /*iArg*/, 0 /*fFlags*/);
273 while ((c = RTGetOpt(&GetState, &ValueUnion)))
274 {
275 switch (c)
276 {
277 case 'M': enmDetails = VMINFO_MACHINEREADABLE; break;
278 default: return errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
279 }
280 }
281
282 /* See showVMInfo. */
283 if (FAILED(showBandwidthGroups(rptrBWControl, enmDetails)))
284 return RTEXITCODE_FAILURE;
285
286 return RTEXITCODE_SUCCESS;
287}
288
289
290/**
291 * Handles the 'bandwidthctl' command.
292 * @returns Exit code.
293 * @param a The handler argument package.
294 */
295int handleBandwidthControl(HandlerArg *a)
296{
297 int c = VERR_INTERNAL_ERROR; /* initialized to shut up gcc */
298 HRESULT rc = S_OK;
299 ComPtr<IMachine> machine;
300 ComPtr<IBandwidthControl> bwCtrl;
301
302 if (a->argc < 2)
303 return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too few parameters");
304 else if (a->argc > 7)
305 return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too many parameters");
306
307 /* try to find the given machine */
308 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
309 machine.asOutParam()), 1);
310
311 /* open a session for the VM (new or shared) */
312 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
313 SessionType_T st;
314 CHECK_ERROR_RET(a->session, COMGETTER(Type)(&st), 1);
315 bool fRunTime = (st == SessionType_Shared);
316
317 /* get the mutable session machine */
318 a->session->COMGETTER(Machine)(machine.asOutParam());
319 rc = machine->COMGETTER(BandwidthControl)(bwCtrl.asOutParam());
320 if (FAILED(rc)) goto leave;
321
322 if (!strcmp(a->argv[1], "add"))
323 {
324 if (fRunTime)
325 {
326 errorArgument("Bandwidth groups cannot be created while the VM is running\n");
327 goto leave;
328 }
329 rc = handleBandwidthControlAdd(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
330 }
331 else if (!strcmp(a->argv[1], "remove"))
332 {
333 if (fRunTime)
334 {
335 errorArgument("Bandwidth groups cannot be deleted while the VM is running\n");
336 goto leave;
337 }
338 rc = handleBandwidthControlRemove(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
339 }
340 else if (!strcmp(a->argv[1], "set"))
341 rc = handleBandwidthControlSet(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
342 else if (!strcmp(a->argv[1], "list"))
343 rc = handleBandwidthControlList(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
344 else
345 {
346 errorSyntax(USAGE_BANDWIDTHCONTROL, "Invalid parameter '%s'", Utf8Str(a->argv[1]).c_str());
347 rc = E_FAIL;
348 }
349
350 /* commit changes */
351 if (SUCCEEDED(rc))
352 CHECK_ERROR(machine, SaveSettings());
353
354leave:
355 /* it's important to always close sessions */
356 a->session->UnlockMachine();
357
358 return SUCCEEDED(rc) ? 0 : 1;
359}
360
361#endif /* !VBOX_ONLY_DOCS */
362
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use