Changeset 44742 in vbox for trunk/src/VBox/Main
- Timestamp:
- Feb 18, 2013 5:26:05 PM (12 years ago)
- svn:sync-xref-src-repo-rev:
- 83847
- Location:
- trunk/src/VBox/Main
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/include/netif.h
r44528 r44742 92 92 int NetIfGetConfig(HostNetworkInterface * pIf, NETIFINFO *); 93 93 int NetIfGetConfigByName(PNETIFINFO pInfo); 94 int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState); 95 int NetIfGetLinkSpeed(const char *pcszIfName, uint32_t *puMbits); 94 96 int NetIfDhcpRediscover(VirtualBox *pVbox, HostNetworkInterface * pIf); 95 97 int NetIfAdpCtlOut(const char * pcszName, const char * pcszCmd, char *pszBuffer, size_t cBufSize); -
trunk/src/VBox/Main/src-server/Performance.cpp
r44548 r44742 689 689 static bool getLinkSpeed(const char *szShortName, uint32_t *pSpeed) 690 690 { 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); 700 693 if (RT_FAILURE(rc)) 701 694 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 } 703 703 return true; 704 704 } -
trunk/src/VBox/Main/src-server/darwin/NetIf-darwin.cpp
r44529 r44742 529 529 } 530 530 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 */ 540 int NetIfGetLinkSpeed(const char * /*pcszIfName*/, uint32_t * /*puMbits*/) 541 { 542 return VERR_NOT_IMPLEMENTED; 543 } 531 544 #endif -
trunk/src/VBox/Main/src-server/freebsd/NetIf-freebsd.cpp
r44529 r44742 406 406 } 407 407 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 */ 417 int 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 22 22 #include <iprt/path.h> 23 23 #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 24 32 25 33 #if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) … … 358 366 } 359 367 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 */ 376 int 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 70 70 } 71 71 return VERR_INTERNAL_ERROR; 72 } 73 74 static 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; 72 113 } 73 114 … … 153 194 * produces errors. 154 195 */ 155 pInfo->uSpeedMbits = 0;156 196 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; 194 200 } 195 201 return VINF_SUCCESS; … … 266 272 return rc; 267 273 } 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 */ 284 int 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 493 493 } 494 494 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 */ 504 int 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 1079 1079 1080 1080 int 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 */ 1093 int 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 */ 1107 int NetIfGetLinkSpeed(const char * /*pcszIfName*/, uint32_t * /*puMbits*/) 1081 1108 { 1082 1109 return VERR_NOT_IMPLEMENTED;
Note:
See TracChangeset
for help on using the changeset viewer.