VirtualBox

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

Last change on this file since 56937 was 56422, checked in by vboxsync, 10 years ago

VBoxManage: gcc warning.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: VBoxManageBandwidthControl.cpp 56422 2015-06-15 09:27:47Z vboxsync $ */
2/** @file
3 * VBoxManage - The bandwidth control related commands.
4 */
5
6/*
7 * Copyright (C) 2006-2014 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 if (name.isEmpty())
111 {
112 errorArgument("Bandwidth group name must not be empty!\n");
113 return RTEXITCODE_FAILURE;
114 }
115
116 const char *pszType = NULL;
117 int64_t cMaxBytesPerSec = INT64_MAX;
118
119 int c;
120 RTGETOPTUNION ValueUnion;
121 RTGETOPTSTATE GetState;
122 RTGetOptInit(&GetState, a->argc, a->argv, g_aBWCtlAddOptions,
123 RT_ELEMENTS(g_aBWCtlAddOptions), 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
124
125 while ( SUCCEEDED(rc)
126 && (c = RTGetOpt(&GetState, &ValueUnion)))
127 {
128 switch (c)
129 {
130 case 't': // bandwidth group type
131 {
132 if (ValueUnion.psz)
133 pszType = ValueUnion.psz;
134 else
135 rc = E_FAIL;
136 break;
137 }
138
139 case 'l': // limit
140 {
141 if (ValueUnion.psz)
142 {
143 const char *pcszError = parseLimit(ValueUnion.psz, &cMaxBytesPerSec);
144 if (pcszError)
145 {
146 errorArgument(pcszError);
147 return RTEXITCODE_FAILURE;
148 }
149 }
150 else
151 rc = E_FAIL;
152 break;
153 }
154
155 default:
156 {
157 errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
158 rc = E_FAIL;
159 break;
160 }
161 }
162 }
163
164 BandwidthGroupType_T enmType;
165
166 if (!RTStrICmp(pszType, "disk"))
167 enmType = BandwidthGroupType_Disk;
168 else if (!RTStrICmp(pszType, "network"))
169 enmType = BandwidthGroupType_Network;
170 else
171 {
172 errorArgument("Invalid bandwidth group type\n");
173 return RTEXITCODE_FAILURE;
174 }
175
176 CHECK_ERROR2I_RET(bwCtrl, CreateBandwidthGroup(name.raw(), enmType, (LONG64)cMaxBytesPerSec), RTEXITCODE_FAILURE);
177
178 return RTEXITCODE_SUCCESS;
179}
180
181/**
182 * Handles the 'bandwidthctl myvm set' sub-command.
183 * @returns Exit code.
184 * @param a The handler argument package.
185 * @param bwCtrl Reference to the bandwidth control interface.
186 */
187static RTEXITCODE handleBandwidthControlSet(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
188{
189 HRESULT rc = S_OK;
190 static const RTGETOPTDEF g_aBWCtlAddOptions[] =
191 {
192 { "--limit", 'l', RTGETOPT_REQ_STRING }
193 };
194
195
196 Bstr name(a->argv[2]);
197 int64_t cMaxBytesPerSec = INT64_MAX;
198
199 int c;
200 RTGETOPTUNION ValueUnion;
201 RTGETOPTSTATE GetState;
202 RTGetOptInit(&GetState, a->argc, a->argv, g_aBWCtlAddOptions,
203 RT_ELEMENTS(g_aBWCtlAddOptions), 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
204
205 while ( SUCCEEDED(rc)
206 && (c = RTGetOpt(&GetState, &ValueUnion)))
207 {
208 switch (c)
209 {
210 case 'l': // limit
211 {
212 if (ValueUnion.psz)
213 {
214 const char *pcszError = parseLimit(ValueUnion.psz, &cMaxBytesPerSec);
215 if (pcszError)
216 {
217 errorArgument(pcszError);
218 return RTEXITCODE_FAILURE;
219 }
220 }
221 else
222 rc = E_FAIL;
223 break;
224 }
225
226 default:
227 {
228 errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
229 rc = E_FAIL;
230 break;
231 }
232 }
233 }
234
235
236 if (cMaxBytesPerSec != INT64_MAX)
237 {
238 ComPtr<IBandwidthGroup> bwGroup;
239 CHECK_ERROR2I_RET(bwCtrl, GetBandwidthGroup(name.raw(), bwGroup.asOutParam()), RTEXITCODE_FAILURE);
240 if (SUCCEEDED(rc))
241 {
242 CHECK_ERROR2I_RET(bwGroup, COMSETTER(MaxBytesPerSec)((LONG64)cMaxBytesPerSec), RTEXITCODE_FAILURE);
243 }
244 }
245
246 return RTEXITCODE_SUCCESS;
247}
248
249/**
250 * Handles the 'bandwidthctl myvm remove' sub-command.
251 * @returns Exit code.
252 * @param a The handler argument package.
253 * @param bwCtrl Reference to the bandwidth control interface.
254 */
255static RTEXITCODE handleBandwidthControlRemove(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
256{
257 Bstr name(a->argv[2]);
258 CHECK_ERROR2I_RET(bwCtrl, DeleteBandwidthGroup(name.raw()), RTEXITCODE_FAILURE);
259 return RTEXITCODE_SUCCESS;
260}
261
262/**
263 * Handles the 'bandwidthctl myvm list' sub-command.
264 * @returns Exit code.
265 * @param a The handler argument package.
266 * @param bwCtrl Reference to the bandwidth control interface.
267 */
268static RTEXITCODE handleBandwidthControlList(HandlerArg *pArgs, ComPtr<IBandwidthControl> &rptrBWControl)
269{
270 static const RTGETOPTDEF g_aOptions[] =
271 {
272 { "--machinereadable", 'M', RTGETOPT_REQ_NOTHING },
273 };
274
275 VMINFO_DETAILS enmDetails = VMINFO_STANDARD;
276
277 int c;
278 RTGETOPTUNION ValueUnion;
279 RTGETOPTSTATE GetState;
280 RTGetOptInit(&GetState, pArgs->argc, pArgs->argv, g_aOptions, RT_ELEMENTS(g_aOptions), 2 /*iArg*/, 0 /*fFlags*/);
281 while ((c = RTGetOpt(&GetState, &ValueUnion)))
282 {
283 switch (c)
284 {
285 case 'M':
286 enmDetails = VMINFO_MACHINEREADABLE;
287 break;
288 default:
289 return errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
290 }
291 }
292
293 if (FAILED(showBandwidthGroups(rptrBWControl, enmDetails)))
294 return RTEXITCODE_FAILURE;
295
296 return RTEXITCODE_SUCCESS;
297}
298
299
300/**
301 * Handles the 'bandwidthctl' command.
302 * @returns Exit code.
303 * @param a The handler argument package.
304 */
305RTEXITCODE handleBandwidthControl(HandlerArg *a)
306{
307 int c = VERR_INTERNAL_ERROR; /* initialized to shut up gcc */
308 HRESULT rc = S_OK;
309 ComPtr<IMachine> machine;
310 ComPtr<IBandwidthControl> bwCtrl;
311
312 if (a->argc < 2)
313 return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too few parameters");
314 else if (a->argc > 7)
315 return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too many parameters");
316
317 /* try to find the given machine */
318 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
319 machine.asOutParam()), RTEXITCODE_FAILURE);
320
321 /* open a session for the VM (new or shared) */
322 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), RTEXITCODE_FAILURE);
323 SessionType_T st;
324 CHECK_ERROR_RET(a->session, COMGETTER(Type)(&st), RTEXITCODE_FAILURE);
325 bool fRunTime = (st == SessionType_Shared);
326
327 /* get the mutable session machine */
328 a->session->COMGETTER(Machine)(machine.asOutParam());
329 rc = machine->COMGETTER(BandwidthControl)(bwCtrl.asOutParam());
330 if (FAILED(rc)) goto leave;
331
332 if (!strcmp(a->argv[1], "add"))
333 {
334 if (fRunTime)
335 {
336 errorArgument("Bandwidth groups cannot be created while the VM is running\n");
337 goto leave;
338 }
339 rc = handleBandwidthControlAdd(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
340 }
341 else if (!strcmp(a->argv[1], "remove"))
342 {
343 if (fRunTime)
344 {
345 errorArgument("Bandwidth groups cannot be deleted while the VM is running\n");
346 goto leave;
347 }
348 rc = handleBandwidthControlRemove(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
349 }
350 else if (!strcmp(a->argv[1], "set"))
351 rc = handleBandwidthControlSet(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
352 else if (!strcmp(a->argv[1], "list"))
353 rc = handleBandwidthControlList(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
354 else
355 {
356 errorSyntax(USAGE_BANDWIDTHCONTROL, "Invalid parameter '%s'", Utf8Str(a->argv[1]).c_str());
357 rc = E_FAIL;
358 }
359
360 /* commit changes */
361 if (SUCCEEDED(rc))
362 CHECK_ERROR(machine, SaveSettings());
363
364leave:
365 /* it's important to always close sessions */
366 a->session->UnlockMachine();
367
368 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
369}
370
371#endif /* !VBOX_ONLY_DOCS */
372
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