diff --git a/slurm.c b/slurm.c index db040d4..ed05fe7 100644 --- a/slurm.c +++ b/slurm.c @@ -1299,7 +1299,7 @@ int main(int argc, char *argv[]) * if the speed could not determined due to errors or lack of * this feature on the host operating system */ -#if defined(_HAVE_BSD) || defined(__HPUX__) || defined(__Solaris__) || defined(__APPLE__) +#if defined(_HAVE_BSD) || defined(__HPUX__) || defined(__Solaris__) || defined(__APPLE__) || defined(__linux__) ifdata.if_speed = get_if_speed(ifdata.if_name); /* if ERR_IFACE_NO_SPEED we could not determine the interface speed diff --git a/src/if_media.c b/src/if_media.c index 9f5b51d..063881c 100644 --- a/src/if_media.c +++ b/src/if_media.c @@ -26,6 +26,10 @@ #endif #endif +#if defined(__linux__) +#include +#include +#endif /****************************************************************************** @@ -296,6 +300,39 @@ int get_if_speed(char *ifstring) return speed; } +#elif defined(__linux__) +int get_if_speed(char *ifstring) +{ + int sock; + struct ifreq ifr; + struct ethtool_cmd edata; + int rc; + + sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + if (sock < 0) { + fprintf(stderr, "cannot create socket to guess interface speed"); + return -1; + } + + strncpy(ifr.ifr_name, ifstring, sizeof(ifr.ifr_name)); + ifr.ifr_data = (void *)&edata; + + edata.cmd = ETHTOOL_GSET; + + rc = ioctl(sock, SIOCETHTOOL, &ifr); + if (rc < 0) { + fprintf(stderr, "failed to guess interface speed"); + return -1; + } + switch (ethtool_cmd_speed(&edata)) { + case SPEED_10: return 10*1000; + case SPEED_100: return 100*1000; + case SPEED_1000: return 1000*1000; + case SPEED_2500: return 2500*1000; + case SPEED_10000: return 10000*1000; + default: return edata.speed*1000; + } +} #else int get_if_speed(char *ifstring) {