1 | /* $Id: VBoxManageBandwidthControl.cpp 40471 2012-03-15 06:22:17Z 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"
|
---|
38 | using 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 | */
|
---|
51 | static 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 | RTPrintf("Adding bwgroup: name=%ls type=%s limit=%u\n", name.raw(), pszType, cMaxMbPerSec);
|
---|
100 |
|
---|
101 | BandwidthGroupType_T enmType;
|
---|
102 |
|
---|
103 | if (!RTStrICmp(pszType, "disk"))
|
---|
104 | enmType = BandwidthGroupType_Disk;
|
---|
105 | else if (!RTStrICmp(pszType, "network"))
|
---|
106 | enmType = BandwidthGroupType_Network;
|
---|
107 | else
|
---|
108 | {
|
---|
109 | errorArgument("Invalid bandwidth group type\n");
|
---|
110 | return RTEXITCODE_FAILURE;
|
---|
111 | }
|
---|
112 |
|
---|
113 | CHECK_ERROR2_RET(bwCtrl, CreateBandwidthGroup(name.raw(), enmType, cMaxMbPerSec), RTEXITCODE_FAILURE);
|
---|
114 |
|
---|
115 | return RTEXITCODE_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Handles the 'bandwidthctl myvm set' sub-command.
|
---|
120 | * @returns Exit code.
|
---|
121 | * @param a The handler argument package.
|
---|
122 | * @param bwCtrl Reference to the bandwidth control interface.
|
---|
123 | */
|
---|
124 | static RTEXITCODE handleBandwidthControlSet(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
|
---|
125 | {
|
---|
126 | HRESULT rc = S_OK;
|
---|
127 | static const RTGETOPTDEF g_aBWCtlAddOptions[] =
|
---|
128 | {
|
---|
129 | { "--limit", 'l', RTGETOPT_REQ_UINT32 }
|
---|
130 | };
|
---|
131 |
|
---|
132 |
|
---|
133 | Bstr name(a->argv[2]);
|
---|
134 | ULONG cMaxMbPerSec = UINT32_MAX;
|
---|
135 |
|
---|
136 | int c;
|
---|
137 | RTGETOPTUNION ValueUnion;
|
---|
138 | RTGETOPTSTATE GetState;
|
---|
139 | RTGetOptInit(&GetState, a->argc, a->argv, g_aBWCtlAddOptions,
|
---|
140 | RT_ELEMENTS(g_aBWCtlAddOptions), 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
141 |
|
---|
142 | while ( SUCCEEDED(rc)
|
---|
143 | && (c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
144 | {
|
---|
145 | switch (c)
|
---|
146 | {
|
---|
147 | case 'l': // limit
|
---|
148 | {
|
---|
149 | cMaxMbPerSec = ValueUnion.u32;
|
---|
150 | break;
|
---|
151 | }
|
---|
152 |
|
---|
153 | default:
|
---|
154 | {
|
---|
155 | errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
|
---|
156 | rc = E_FAIL;
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 | RTPrintf("Updating bwgroup: name=%ls limit=%u\n", name.raw(), cMaxMbPerSec);
|
---|
162 |
|
---|
163 |
|
---|
164 | if (cMaxMbPerSec != UINT32_MAX)
|
---|
165 | {
|
---|
166 | ComPtr<IBandwidthGroup> bwGroup;
|
---|
167 | CHECK_ERROR2_RET(bwCtrl, GetBandwidthGroup(name.raw(), bwGroup.asOutParam()), RTEXITCODE_FAILURE);
|
---|
168 | if (SUCCEEDED(rc))
|
---|
169 | CHECK_ERROR2_RET(bwGroup, COMSETTER(MaxMbPerSec)(cMaxMbPerSec), RTEXITCODE_FAILURE);
|
---|
170 | }
|
---|
171 |
|
---|
172 | return RTEXITCODE_SUCCESS;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Handles the 'bandwidthctl myvm remove' sub-command.
|
---|
177 | * @returns Exit code.
|
---|
178 | * @param a The handler argument package.
|
---|
179 | * @param bwCtrl Reference to the bandwidth control interface.
|
---|
180 | */
|
---|
181 | static RTEXITCODE handleBandwidthControlRemove(HandlerArg *a, ComPtr<IBandwidthControl> &bwCtrl)
|
---|
182 | {
|
---|
183 | Bstr name(a->argv[2]);
|
---|
184 | CHECK_ERROR2_RET(bwCtrl, DeleteBandwidthGroup(name.raw()), RTEXITCODE_FAILURE);
|
---|
185 | return RTEXITCODE_SUCCESS;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Handles the 'bandwidthctl myvm list' sub-command.
|
---|
190 | * @returns Exit code.
|
---|
191 | * @param a The handler argument package.
|
---|
192 | * @param bwCtrl Reference to the bandwidth control interface.
|
---|
193 | */
|
---|
194 | static RTEXITCODE handleBandwidthControlList(HandlerArg *pArgs, ComPtr<IBandwidthControl> &rptrBWControl)
|
---|
195 | {
|
---|
196 | static const RTGETOPTDEF g_aOptions[] =
|
---|
197 | {
|
---|
198 | { "--machinereadable", 'M', RTGETOPT_REQ_NOTHING },
|
---|
199 | };
|
---|
200 |
|
---|
201 | VMINFO_DETAILS enmDetails = VMINFO_STANDARD;
|
---|
202 |
|
---|
203 | int c;
|
---|
204 | RTGETOPTUNION ValueUnion;
|
---|
205 | RTGETOPTSTATE GetState;
|
---|
206 | RTGetOptInit(&GetState, pArgs->argc, pArgs->argv, g_aOptions, RT_ELEMENTS(g_aOptions), 2 /*iArg*/, 0 /*fFlags*/);
|
---|
207 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
208 | {
|
---|
209 | switch (c)
|
---|
210 | {
|
---|
211 | case 'M': enmDetails = VMINFO_MACHINEREADABLE; break;
|
---|
212 | default: return errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | /* See showVMInfo. */
|
---|
217 | if (FAILED(showBandwidthGroups(rptrBWControl, enmDetails)))
|
---|
218 | return RTEXITCODE_FAILURE;
|
---|
219 |
|
---|
220 | return RTEXITCODE_SUCCESS;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Handles the 'bandwidthctl' command.
|
---|
226 | * @returns Exit code.
|
---|
227 | * @param a The handler argument package.
|
---|
228 | */
|
---|
229 | int handleBandwidthControl(HandlerArg *a)
|
---|
230 | {
|
---|
231 | int c = VERR_INTERNAL_ERROR; /* initialized to shut up gcc */
|
---|
232 | HRESULT rc = S_OK;
|
---|
233 | ComPtr<IMachine> machine;
|
---|
234 | ComPtr<IBandwidthControl> bwCtrl;
|
---|
235 |
|
---|
236 | if (a->argc < 2)
|
---|
237 | return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too few parameters");
|
---|
238 | else if (a->argc > 7)
|
---|
239 | return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too many parameters");
|
---|
240 |
|
---|
241 | /* try to find the given machine */
|
---|
242 | CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
|
---|
243 | machine.asOutParam()), 1);
|
---|
244 |
|
---|
245 | /* open a session for the VM (new or shared) */
|
---|
246 | CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
|
---|
247 | SessionType_T st;
|
---|
248 | CHECK_ERROR_RET(a->session, COMGETTER(Type)(&st), 1);
|
---|
249 | bool fRunTime = (st == SessionType_Shared);
|
---|
250 |
|
---|
251 | /* get the mutable session machine */
|
---|
252 | a->session->COMGETTER(Machine)(machine.asOutParam());
|
---|
253 | rc = machine->COMGETTER(BandwidthControl)(bwCtrl.asOutParam());
|
---|
254 | if (FAILED(rc)) goto leave;
|
---|
255 |
|
---|
256 | if (!strcmp(a->argv[1], "add"))
|
---|
257 | {
|
---|
258 | if (fRunTime)
|
---|
259 | {
|
---|
260 | errorArgument("Bandwidth groups cannot be created while the VM is running\n");
|
---|
261 | goto leave;
|
---|
262 | }
|
---|
263 | rc = handleBandwidthControlAdd(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
|
---|
264 | }
|
---|
265 | else if (!strcmp(a->argv[1], "remove"))
|
---|
266 | {
|
---|
267 | if (fRunTime)
|
---|
268 | {
|
---|
269 | errorArgument("Bandwidth groups cannot be deleted while the VM is running\n");
|
---|
270 | goto leave;
|
---|
271 | }
|
---|
272 | rc = handleBandwidthControlRemove(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
|
---|
273 | }
|
---|
274 | else if (!strcmp(a->argv[1], "set"))
|
---|
275 | rc = handleBandwidthControlSet(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
|
---|
276 | else if (!strcmp(a->argv[1], "list"))
|
---|
277 | rc = handleBandwidthControlList(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
|
---|
278 | else
|
---|
279 | {
|
---|
280 | errorSyntax(USAGE_BANDWIDTHCONTROL, "Invalid parameter '%s'", Utf8Str(a->argv[1]).c_str());
|
---|
281 | rc = E_FAIL;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /* commit changes */
|
---|
285 | if (SUCCEEDED(rc))
|
---|
286 | CHECK_ERROR(machine, SaveSettings());
|
---|
287 |
|
---|
288 | leave:
|
---|
289 | /* it's important to always close sessions */
|
---|
290 | a->session->UnlockMachine();
|
---|
291 |
|
---|
292 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
293 | }
|
---|
294 |
|
---|
295 | #endif /* !VBOX_ONLY_DOCS */
|
---|
296 |
|
---|