VirtualBox

Changeset 44742 in vbox for trunk/src/VBox/Main


Ignore:
Timestamp:
Feb 18, 2013 5:26:05 PM (12 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
83847
Message:

Main/Metrics: Generic implentation of link state and stubs for line speed for all platforms (#6345)

Location:
trunk/src/VBox/Main
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/include/netif.h

    r44528 r44742  
    9292int NetIfGetConfig(HostNetworkInterface * pIf, NETIFINFO *);
    9393int NetIfGetConfigByName(PNETIFINFO pInfo);
     94int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState);
     95int NetIfGetLinkSpeed(const char *pcszIfName, uint32_t *puMbits);
    9496int NetIfDhcpRediscover(VirtualBox *pVbox, HostNetworkInterface * pIf);
    9597int NetIfAdpCtlOut(const char * pcszName, const char * pcszCmd, char *pszBuffer, size_t cBufSize);
  • trunk/src/VBox/Main/src-server/Performance.cpp

    r44548 r44742  
    689689static bool getLinkSpeed(const char *szShortName, uint32_t *pSpeed)
    690690{
    691     /*
    692      * Note that we do not need the full name in the info, so we do not
    693      * allocate the space for it and we rely on the fact that
    694      * NetIfGetConfigByName() never fills it.
    695      */
    696     NETIFINFO Info;
    697     memset(&Info, 0, sizeof(Info));
    698     strcpy(Info.szShortName, szShortName);
    699     int rc = NetIfGetConfigByName(&Info);
     691    NETIFSTATUS enmState = NETIF_S_UNKNOWN;
     692    int rc = NetIfGetState(szShortName, &enmState);
    700693    if (RT_FAILURE(rc))
    701694        return false;
    702     *pSpeed =  Info.enmStatus == NETIF_S_UP ? Info.uSpeedMbits : 0;
     695    if (enmState != NETIF_S_UP)
     696        *pSpeed = 0;
     697    else
     698    {
     699        rc = NetIfGetLinkSpeed(szShortName, pSpeed);
     700        if (RT_FAILURE(rc))
     701            return false;
     702    }
    703703    return true;
    704704}
  • trunk/src/VBox/Main/src-server/darwin/NetIf-darwin.cpp

    r44529 r44742  
    529529}
    530530
     531/**
     532 * Retrieve the physical link speed in megabits per second. If the interface is
     533 * not up or otherwise unavailable the zero speed is returned.
     534 *
     535 * @returns VBox status code.
     536 *
     537 * @param   pcszIfName  Interface name.
     538 * @param   puMbits     Where to store the link speed.
     539 */
     540int NetIfGetLinkSpeed(const char * /*pcszIfName*/, uint32_t * /*puMbits*/)
     541{
     542    return VERR_NOT_IMPLEMENTED;
     543}
    531544#endif
  • trunk/src/VBox/Main/src-server/freebsd/NetIf-freebsd.cpp

    r44529 r44742  
    406406}
    407407
     408/**
     409 * Retrieve the physical link speed in megabits per second. If the interface is
     410 * not up or otherwise unavailable the zero speed is returned.
     411 *
     412 * @returns VBox status code.
     413 *
     414 * @param   pcszIfName  Interface name.
     415 * @param   puMbits     Where to store the link speed.
     416 */
     417int NetIfGetLinkSpeed(const char * /*pcszIfName*/, uint32_t * /*puMbits*/)
     418{
     419    return VERR_NOT_IMPLEMENTED;
     420}
  • trunk/src/VBox/Main/src-server/generic/NetIf-generic.cpp

    r44528 r44742  
    2222#include <iprt/path.h>
    2323#include <iprt/param.h>
     24#include <sys/ioctl.h>
     25#include <netinet/in.h>
     26#include <net/if.h>
     27#include <errno.h>
     28
     29#if defined(RT_OS_SOLARIS)
     30# include <sys/sockio.h>
     31#endif
    2432
    2533#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
     
    358366}
    359367
     368/**
     369 * Obtain the current state of the interface.
     370 *
     371 * @returns VBox status code.
     372 *
     373 * @param   pcszIfName  Interface name.
     374 * @param   penmState   Where to store the retrieved state.
     375 */
     376int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState)
     377{
     378    int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
     379    if (sock < 0)
     380        return VERR_OUT_OF_RESOURCES;
     381    struct ifreq Req;
     382    memset(&Req, 0, sizeof(Req));
     383    strncpy(Req.ifr_name, pcszIfName, sizeof(Req.ifr_name) - 1);
     384    if (ioctl(sock, SIOCGIFFLAGS, &Req) < 0)
     385    {
     386        Log(("NetIfGetState: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
     387        *penmState = NETIF_S_UNKNOWN;
     388    }
     389    else
     390        *penmState = (Req.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
     391    return VINF_SUCCESS;
     392}
  • trunk/src/VBox/Main/src-server/linux/NetIf-linux.cpp

    r44529 r44742  
    7070    }
    7171    return VERR_INTERNAL_ERROR;
     72}
     73
     74static uint32_t getInterfaceSpeed(const char *pszName)
     75{
     76    /*
     77     * I wish I could do simple ioctl here, but older kernels require root
     78     * privileges for any ethtool commands.
     79     */
     80    char szBuf[256];
     81    uint32_t uSpeed = 0;
     82    /* First, we try to retrieve the speed via sysfs. */
     83    RTStrPrintf(szBuf, sizeof(szBuf), "/sys/class/net/%s/speed", pszName);
     84    FILE *fp = fopen(szBuf, "r");
     85    if (fp)
     86    {
     87        if (fscanf(fp, "%u", &uSpeed) != 1)
     88            uSpeed = 0;
     89        fclose(fp);
     90    }
     91    if (uSpeed == 10)
     92    {
     93        /* Check the cable is plugged in at all */
     94        unsigned uCarrier = 0;
     95        RTStrPrintf(szBuf, sizeof(szBuf), "/sys/class/net/%s/carrier", pszName);
     96        fp = fopen(szBuf, "r");
     97        if (fp)
     98        {
     99            if (fscanf(fp, "%u", &uCarrier) != 1 || uCarrier == 0)
     100                uSpeed = 0;
     101            fclose(fp);
     102        }
     103    }
     104   
     105    if (uSpeed == 0)
     106    {
     107        /* Failed to get speed via sysfs, go to plan B. */
     108        int rc = NetIfAdpCtlOut(pszName, "speed", szBuf, sizeof(szBuf));
     109        if (RT_SUCCESS(rc))
     110            uSpeed = RTStrToUInt32(szBuf);
     111    }
     112    return uSpeed;
    72113}
    73114
     
    153194         * produces errors.
    154195         */
    155         pInfo->uSpeedMbits = 0;
    156196        if (pInfo->enmMediumType == NETIF_T_ETHERNET)
    157         {
    158             /*
    159              * I wish I could do simple ioctl here, but older kernels require root
    160              * privileges for any ethtool commands.
    161              */
    162             char szBuf[256];
    163             /* First, we try to retrieve the speed via sysfs. */
    164             RTStrPrintf(szBuf, sizeof(szBuf), "/sys/class/net/%s/speed", pszName);
    165             fp = fopen(szBuf, "r");
    166             if (fp)
    167             {
    168                 if (fscanf(fp, "%u", &pInfo->uSpeedMbits) != 1)
    169                     pInfo->uSpeedMbits = 0;
    170                 fclose(fp);
    171             }
    172             if (pInfo->uSpeedMbits == 10)
    173             {
    174                 /* Check the cable is plugged in at all */
    175                 unsigned uCarrier = 0;
    176                 RTStrPrintf(szBuf, sizeof(szBuf), "/sys/class/net/%s/carrier", pszName);
    177                 fp = fopen(szBuf, "r");
    178                 if (fp)
    179                 {
    180                     if (fscanf(fp, "%u", &uCarrier) != 1 || uCarrier == 0)
    181                         pInfo->uSpeedMbits = 0;
    182                     fclose(fp);
    183                 }
    184             }
    185 
    186             if (pInfo->uSpeedMbits == 0)
    187             {
    188                 /* Failed to get speed via sysfs, go to plan B. */
    189                 int rc = NetIfAdpCtlOut(pszName, "speed", szBuf, sizeof(szBuf));
    190                 if (RT_SUCCESS(rc))
    191                     pInfo->uSpeedMbits = RTStrToUInt32(szBuf);
    192             }
    193         }
     197            pInfo->uSpeedMbits = getInterfaceSpeed(pszName);
     198        else
     199            pInfo->uSpeedMbits = 0;
    194200    }
    195201    return VINF_SUCCESS;
     
    266272    return rc;
    267273}
     274
     275/**
     276 * Retrieve the physical link speed in megabits per second. If the interface is
     277 * not up or otherwise unavailable the zero speed is returned.
     278 *
     279 * @returns VBox status code.
     280 *
     281 * @param   pcszIfName  Interface name.
     282 * @param   puMbits     Where to store the link speed.
     283 */
     284int NetIfGetLinkSpeed(const char *pcszIfName, uint32_t *puMbits)
     285{
     286    int sock = socket(AF_INET, SOCK_DGRAM, 0);
     287    if (sock < 0)
     288        return VERR_OUT_OF_RESOURCES;
     289    struct ifreq Req;
     290    memset(&Req, 0, sizeof(Req));
     291    strncpy(Req.ifr_name, pcszIfName, sizeof(Req.ifr_name) - 1);
     292    if (ioctl(sock, SIOCGIFHWADDR, &Req) >= 0)
     293    {
     294        if (ioctl(sock, SIOCGIFFLAGS, &Req) >= 0)
     295            if (Req.ifr_flags & IFF_UP)
     296            {
     297                *puMbits = getInterfaceSpeed(pcszIfName);
     298                return VINF_SUCCESS;
     299            }
     300    }
     301    *puMbits = 0;
     302    return VWRN_NOT_FOUND;
     303}
  • trunk/src/VBox/Main/src-server/solaris/NetIf-solaris.cpp

    r44726 r44742  
    493493}
    494494
     495/**
     496 * Retrieve the physical link speed in megabits per second. If the interface is
     497 * not up or otherwise unavailable the zero speed is returned.
     498 *
     499 * @returns VBox status code.
     500 *
     501 * @param   pcszIfName  Interface name.
     502 * @param   puMbits     Where to store the link speed.
     503 */
     504int NetIfGetLinkSpeed(const char *pcszIfName, uint32_t *puMbits)
     505{
     506    *puMbits =  kstatGet(pcszIfName);
     507    return VINF_SUCCESS;
     508}
  • trunk/src/VBox/Main/src-server/win/NetIf-win.cpp

    r44528 r44742  
    10791079
    10801080int NetIfGetConfigByName(PNETIFINFO)
     1081{
     1082    return VERR_NOT_IMPLEMENTED;
     1083}
     1084
     1085/**
     1086 * Obtain the current state of the interface.
     1087 *
     1088 * @returns VBox status code.
     1089 *
     1090 * @param   pcszIfName  Interface name.
     1091 * @param   penmState   Where to store the retrieved state.
     1092 */
     1093int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState)
     1094{
     1095    return VERR_NOT_IMPLEMENTED;
     1096}
     1097
     1098/**
     1099 * Retrieve the physical link speed in megabits per second. If the interface is
     1100 * not up or otherwise unavailable the zero speed is returned.
     1101 *
     1102 * @returns VBox status code.
     1103 *
     1104 * @param   pcszIfName  Interface name.
     1105 * @param   puMbits     Where to store the link speed.
     1106 */
     1107int NetIfGetLinkSpeed(const char * /*pcszIfName*/, uint32_t * /*puMbits*/)
    10811108{
    10821109    return VERR_NOT_IMPLEMENTED;
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