VirtualBox

Changeset 40469 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Mar 14, 2012 6:14:52 PM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
76837
Message:

VBoxManage: bandwidthctl uses sub-commands instead of options (#5582)

Location:
trunk/src/VBox/Frontends/VBoxManage
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageBandwidthControl.cpp

    r34587 r40469  
    4343
    4444
    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 */
     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{
    5653    HRESULT rc = S_OK;
    57     const char *pszName  = NULL;
     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]);
    5862    const char *pszType  = NULL;
    5963    ULONG cMaxMbPerSec   = UINT32_MAX;
    60     bool fDelete = false;
     64
     65    int c;
    6166    RTGETOPTUNION ValueUnion;
    6267    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);
     68    RTGetOptInit(&GetState, a->argc, a->argv, g_aBWCtlAddOptions,
     69                 RT_ELEMENTS(g_aBWCtlAddOptions), 3, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
    7470
    7571    while (   SUCCEEDED(rc)
     
    7874        switch (c)
    7975        {
    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
     76            case 't':   // bandwidth group type
    9077            {
    9178                if (ValueUnion.psz)
     
    10289            }
    10390
    104             case 'd':   // delete
    105             {
    106                 fDelete = true;
    107                 break;
    108             }
    109 
    11091            default:
    11192            {
     
    11697        }
    11798    }
    118 
    119     if (FAILED(rc))
    120         return 1;
    121 
    122     if (!pszName)
    123         return errorSyntax(USAGE_BANDWIDTHCONTROL, "Bandwidth group name not specified");
     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 */
     124static 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 */
     181static 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 * Converts bandwidth group type to a string.
     190 * @returns String representation.
     191 * @param   enmType         Bandwidth control group type.
     192 */
     193inline const char * typeToString(BandwidthGroupType_T enmType)
     194{
     195    switch (enmType)
     196    {
     197        case BandwidthGroupType_Disk:    return "Disk";
     198        case BandwidthGroupType_Network: return "Network";
     199    }
     200    return "unknown";
     201}
     202
     203/**
     204 * Handles the 'bandwidthctl myvm list' sub-command.
     205 * @returns Exit code.
     206 * @param   a               The handler argument package.
     207 * @param   bwCtrl          Reference to the bandwidth control interface.
     208 */
     209static RTEXITCODE handleBandwidthControlList(HandlerArg *pArgs, ComPtr<IBandwidthControl> &rptrBWControl)
     210{
     211    static const RTGETOPTDEF g_aOptions[] =
     212    {
     213        { "--machinereadable",  'M', RTGETOPT_REQ_NOTHING },
     214    };
     215
     216    VMINFO_DETAILS enmDetails = VMINFO_STANDARD;
     217
     218    int c;
     219    RTGETOPTUNION ValueUnion;
     220    RTGETOPTSTATE GetState;
     221    RTGetOptInit(&GetState, pArgs->argc, pArgs->argv, g_aOptions, RT_ELEMENTS(g_aOptions), 2 /*iArg*/, 0 /*fFlags*/);
     222    while ((c = RTGetOpt(&GetState, &ValueUnion)))
     223    {
     224        switch (c)
     225        {
     226            case 'M':   enmDetails = VMINFO_MACHINEREADABLE; break;
     227            default:    return errorGetOpt(USAGE_BANDWIDTHCONTROL, c, &ValueUnion);
     228        }
     229    }
     230
     231    /* See showVMInfo. */
     232    com::SafeIfaceArray<IBandwidthGroup> bwGroups;
     233    CHECK_ERROR2_RET(rptrBWControl, GetAllBandwidthGroups(ComSafeArrayAsOutParam(bwGroups)), RTEXITCODE_FAILURE);
     234    for (size_t i = 0; i < bwGroups.size(); i++)
     235    {
     236        Bstr strName;
     237        ULONG cMaxMbPerSec;
     238        BandwidthGroupType_T enmType;
     239
     240        CHECK_ERROR2_RET(bwGroups[i], COMGETTER(Name)(strName.asOutParam()), RTEXITCODE_FAILURE);
     241        CHECK_ERROR2_RET(bwGroups[i], COMGETTER(Type)(&enmType), RTEXITCODE_FAILURE);
     242        CHECK_ERROR2_RET(bwGroups[i], COMGETTER(MaxMbPerSec)(&cMaxMbPerSec), RTEXITCODE_FAILURE);
     243
     244        RTPrintf(enmDetails == VMINFO_MACHINEREADABLE ?
     245                 "Group=%ls\nType=%s\nLimit=%u\n\n" :
     246                 "%-30ls %-10s %u MBytes/sec\n",
     247                 strName.raw(),
     248                 typeToString(enmType),
     249                 cMaxMbPerSec);
     250    }
     251
     252    return RTEXITCODE_SUCCESS;
     253}
     254
     255
     256/**
     257 * Handles the 'bandwidthctl' command.
     258 * @returns Exit code.
     259 * @param   a               The handler argument package.
     260 */
     261int handleBandwidthControl(HandlerArg *a)
     262{
     263    int c = VERR_INTERNAL_ERROR;        /* initialized to shut up gcc */
     264    HRESULT rc = S_OK;
     265    ComPtr<IMachine> machine;
     266    ComPtr<IBandwidthControl> bwCtrl;
     267
     268    if (a->argc < 2)
     269        return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too few parameters");
     270    else if (a->argc > 7)
     271        return errorSyntax(USAGE_BANDWIDTHCONTROL, "Too many parameters");
    124272
    125273    /* try to find the given machine */
     
    133281    bool fRunTime = (st == SessionType_Shared);
    134282
    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 
    153283    /* get the mutable session machine */
    154284    a->session->COMGETTER(Machine)(machine.asOutParam());
     
    156286    if (FAILED(rc)) goto leave;
    157287
    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");
     288    if (!strcmp(a->argv[1], "add"))
     289    {
     290        if (fRunTime)
     291        {
     292            errorArgument("Bandwidth groups cannot be created while the VM is running\n");
    170293            goto leave;
    171294        }
    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));
     295        rc = handleBandwidthControlAdd(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
     296    }
     297    else if (!strcmp(a->argv[1], "remove"))
     298    {
     299        if (fRunTime)
     300        {
     301            errorArgument("Bandwidth groups cannot be deleted while the VM is running\n");
     302            goto leave;
     303        }
     304        rc = handleBandwidthControlRemove(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
     305    }
     306    else if (!strcmp(a->argv[1], "set"))
     307        rc = handleBandwidthControlSet(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
     308    else if (!strcmp(a->argv[1], "list"))
     309        rc = handleBandwidthControlList(a, bwCtrl) == RTEXITCODE_SUCCESS ? S_OK : E_FAIL;
     310    else
     311    {
     312        errorSyntax(USAGE_BANDWIDTHCONTROL, "Invalid parameter '%s'", Utf8Str(a->argv[1]).c_str());
     313        rc = E_FAIL;
    184314    }
    185315
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageHelp.cpp

    r40418 r40469  
    510510        RTStrmPrintf(pStrm,
    511511                     "VBoxManage bandwidthctl     <uuid|vmname>\n"
    512                      "                            --name <name>\n"
    513                      "                            [--add disk|network]\n"
    514                      "                            [--limit <megabytes per second>\n"
    515                      "                            [--delete]\n"
     512                     "                            add <name> --type <disk|network> --limit <megabytes per second> |\n"
     513                     "                            set <name> --limit <megabytes per second> |\n"
     514                     "                            remove <name> |\n"
     515                     "                            list [--machinereadable]\n"
    516516                     "\n");
    517517
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageInfo.cpp

    r40419 r40469  
    10231023                nic->COMGETTER(BootPriority)(&ulBootPriority);
    10241024
     1025                /* bandwidth group */
     1026                ComObjPtr<IBandwidthGroup> pBwGroup;
     1027                Bstr strBwGroup;
     1028                nic->COMGETTER(BandwidthGroup)(pBwGroup.asOutParam());
     1029                if (!pBwGroup.isNull())
     1030                    pBwGroup->COMGETTER(Name)(strBwGroup.asOutParam());
     1031
    10251032                if (details == VMINFO_MACHINEREADABLE)
    10261033                {
     
    10301037                }
    10311038                else
    1032                     RTPrintf("NIC %u:           MAC: %ls, Attachment: %s, Cable connected: %s, Trace: %s (file: %ls), Type: %s, Reported speed: %d Mbps, Boot priority: %d, Promisc Policy: %s\n",
     1039                    RTPrintf("NIC %u:           MAC: %ls, Attachment: %s, Cable connected: %s, Trace: %s (file: %ls), Type: %s, Reported speed: %d Mbps, Boot priority: %d, Promisc Policy: %s, Bandwidth group: %ls\n",
    10331040                             currentNIC + 1, strMACAddress.raw(), strAttachment.c_str(),
    10341041                             fConnected ? "on" : "off",
     
    10381045                             ulLineSpeed / 1000,
    10391046                             (int)ulBootPriority,
    1040                              pszPromiscuousGuestPolicy);
     1047                             pszPromiscuousGuestPolicy,
     1048                             strBwGroup.isEmpty() ? Bstr("none").raw() : strBwGroup.raw());
    10411049                if (strNatSettings.length())
    10421050                    RTPrintf(strNatSettings.c_str());
     
    18231831
    18241832    /*
     1833     * Bandwidth groups
     1834     */
     1835    if (details != VMINFO_MACHINEREADABLE)
     1836        RTPrintf("Bandwidth groups:\n\n");
     1837    {
     1838        ComPtr<IBandwidthControl> bwCtrl;
     1839        SafeIfaceArray<IBandwidthGroup> bwGroups;
     1840
     1841        CHECK_ERROR_RET(machine, COMGETTER(BandwidthControl)(bwCtrl.asOutParam()), rc);
     1842
     1843        CHECK_ERROR_RET(bwCtrl, GetAllBandwidthGroups(ComSafeArrayAsOutParam(bwGroups)), rc);
     1844
     1845        for (size_t i = 0; i < bwGroups.size(); i++)
     1846        {
     1847            Bstr strName;
     1848            ULONG cMaxMbPerSec;
     1849            BandwidthGroupType_T enmType;
     1850
     1851            CHECK_ERROR_RET(bwGroups[i], COMGETTER(Name)(strName.asOutParam()), rc);
     1852            CHECK_ERROR_RET(bwGroups[i], COMGETTER(Type)(&enmType), rc);
     1853            CHECK_ERROR_RET(bwGroups[i], COMGETTER(MaxMbPerSec)(&cMaxMbPerSec), rc);
     1854
     1855            const char *pszType = "unknown";
     1856            switch (enmType)
     1857            {
     1858                case BandwidthGroupType_Disk:    pszType = "disk";    break;
     1859                case BandwidthGroupType_Network: pszType = "network"; break;
     1860            }
     1861            if (details == VMINFO_MACHINEREADABLE)
     1862                RTPrintf("BandwidthGroup%zu=%ls,%s,%d\n", strName.raw(), pszType, cMaxMbPerSec);
     1863            else
     1864                RTPrintf("Name: '%ls', Type: %s, Limit: %d Mbytes/sec\n", strName.raw(), pszType, cMaxMbPerSec);
     1865        }
     1866        if (details != VMINFO_MACHINEREADABLE && bwGroups.size() != 0)
     1867            RTPrintf("\n");
     1868    }
     1869
     1870
     1871    /*
    18251872     * Shared folders
    18261873     */
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette