1 | /* $Id: VBoxManageBandwidthControl.cpp 34587 2010-12-01 20:30:02Z 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 | static const RTGETOPTDEF g_aBandwidthControlOptions[] =
|
---|
46 | {
|
---|
47 | { "--name", 'n', RTGETOPT_REQ_STRING },
|
---|
48 | { "--add", 'a', RTGETOPT_REQ_STRING },
|
---|
49 | { "--limit", 'l', RTGETOPT_REQ_UINT32 },
|
---|
50 | { "--delete", 'd', RTGETOPT_REQ_NOTHING },
|
---|
51 | };
|
---|
52 |
|
---|
53 | int handleBandwidthControl(HandlerArg *a)
|
---|
54 | {
|
---|
55 | int c = VERR_INTERNAL_ERROR; /* initialized to shut up gcc */
|
---|
56 | HRESULT rc = S_OK;
|
---|
57 | const char *pszName = NULL;
|
---|
58 | const char *pszType = NULL;
|
---|
59 | ULONG cMaxMbPerSec = UINT32_MAX;
|
---|
60 | bool fDelete = false;
|
---|
61 | RTGETOPTUNION ValueUnion;
|
---|
62 | RTGETOPTSTATE GetState;
|
---|
63 | ComPtr<IMachine> machine;
|
---|
64 | ComPtr<IBandwidthControl> bwCtrl;
|
---|
65 | ComPtr<IBandwidthGroup> bwGroup;
|
---|
66 |
|
---|
67 | if (a->argc < 4)
|
---|
68 | return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too few parameters");
|
---|
69 | else if (a->argc > 7)
|
---|
70 | return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too many parameters");
|
---|
71 |
|
---|
72 | RTGetOptInit(&GetState, a->argc, a->argv, g_aBandwidthControlOptions,
|
---|
73 | RT_ELEMENTS(g_aBandwidthControlOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
74 |
|
---|
75 | while ( SUCCEEDED(rc)
|
---|
76 | && (c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
77 | {
|
---|
78 | switch (c)
|
---|
79 | {
|
---|
80 | case 'n': // bandwidth group name
|
---|
81 | {
|
---|
82 | if (ValueUnion.psz)
|
---|
83 | pszName = ValueUnion.psz;
|
---|
84 | else
|
---|
85 | rc = E_FAIL;
|
---|
86 | break;
|
---|
87 | }
|
---|
88 |
|
---|
89 | case 'a': // add
|
---|
90 | {
|
---|
91 | if (ValueUnion.psz)
|
---|
92 | pszType = ValueUnion.psz;
|
---|
93 | else
|
---|
94 | rc = E_FAIL;
|
---|
95 | break;
|
---|
96 | }
|
---|
97 |
|
---|
98 | case 'l': // limit
|
---|
99 | {
|
---|
100 | cMaxMbPerSec = ValueUnion.u32;
|
---|
101 | break;
|
---|
102 | }
|
---|
103 |
|
---|
104 | case 'd': // delete
|
---|
105 | {
|
---|
106 | fDelete = true;
|
---|
107 | break;
|
---|
108 | }
|
---|
109 |
|
---|
110 | default:
|
---|
111 | {
|
---|
112 | errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
|
---|
113 | rc = E_FAIL;
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (FAILED(rc))
|
---|
120 | return 1;
|
---|
121 |
|
---|
122 | if (!pszName)
|
---|
123 | return errorSyntax(USAGE_BANDWIDTHCONTROL, "Bandwidth group name not specified");
|
---|
124 |
|
---|
125 | /* try to find the given machine */
|
---|
126 | CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
|
---|
127 | machine.asOutParam()), 1);
|
---|
128 |
|
---|
129 | /* open a session for the VM (new or shared) */
|
---|
130 | CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
|
---|
131 | SessionType_T st;
|
---|
132 | CHECK_ERROR_RET(a->session, COMGETTER(Type)(&st), 1);
|
---|
133 | bool fRunTime = (st == SessionType_Shared);
|
---|
134 |
|
---|
135 | if (fRunTime && pszType)
|
---|
136 | {
|
---|
137 | errorArgument("Bandwidth groups cannot be created while the VM is running\n");
|
---|
138 | goto leave;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (fRunTime && fDelete)
|
---|
142 | {
|
---|
143 | errorArgument("Bandwidth groups cannot be deleted while the VM is running\n");
|
---|
144 | goto leave;
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (fDelete && pszType)
|
---|
148 | {
|
---|
149 | errorArgument("Bandwidth groups cannot be created and deleted at the same time\n");
|
---|
150 | goto leave;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /* get the mutable session machine */
|
---|
154 | a->session->COMGETTER(Machine)(machine.asOutParam());
|
---|
155 | rc = machine->COMGETTER(BandwidthControl)(bwCtrl.asOutParam());
|
---|
156 | if (FAILED(rc)) goto leave;
|
---|
157 |
|
---|
158 | /* Add a new group if requested. */
|
---|
159 | if (pszType)
|
---|
160 | {
|
---|
161 | BandwidthGroupType_T enmType;
|
---|
162 |
|
---|
163 | if (!RTStrICmp(pszType, "disk"))
|
---|
164 | enmType = BandwidthGroupType_Disk;
|
---|
165 | else if (!RTStrICmp(pszType, "network"))
|
---|
166 | enmType = BandwidthGroupType_Network;
|
---|
167 | else
|
---|
168 | {
|
---|
169 | errorArgument("Invalid bandwidth group type\n");
|
---|
170 | goto leave;
|
---|
171 | }
|
---|
172 |
|
---|
173 | CHECK_ERROR(bwCtrl, CreateBandwidthGroup(Bstr(pszName).raw(), enmType, cMaxMbPerSec));
|
---|
174 | }
|
---|
175 | else if (fDelete)
|
---|
176 | {
|
---|
177 | CHECK_ERROR(bwCtrl, DeleteBandwidthGroup(Bstr(pszName).raw()));
|
---|
178 | }
|
---|
179 | else if (cMaxMbPerSec != UINT32_MAX)
|
---|
180 | {
|
---|
181 | CHECK_ERROR(bwCtrl, GetBandwidthGroup(Bstr(pszName).raw(), bwGroup.asOutParam()));
|
---|
182 | if (SUCCEEDED(rc))
|
---|
183 | CHECK_ERROR(bwGroup, COMSETTER(MaxMbPerSec)(cMaxMbPerSec));
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* commit changes */
|
---|
187 | if (SUCCEEDED(rc))
|
---|
188 | CHECK_ERROR(machine, SaveSettings());
|
---|
189 |
|
---|
190 | leave:
|
---|
191 | /* it's important to always close sessions */
|
---|
192 | a->session->UnlockMachine();
|
---|
193 |
|
---|
194 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
195 | }
|
---|
196 |
|
---|
197 | #endif /* !VBOX_ONLY_DOCS */
|
---|
198 |
|
---|