Skip to content

Commit

Permalink
Merge pull request #29 from anarcat/linux-ifspeed
Browse files Browse the repository at this point in the history
implement iface speed detection on Linux (closes #12)
  • Loading branch information
mattthias authored Mar 20, 2019
2 parents fd6aa6a + bc08479 commit 88ac46b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion slurm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/if_media.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#endif
#endif

#if defined(__linux__)
#include <linux/sockios.h>
#include <linux/ethtool.h>
#endif


/******************************************************************************
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 88ac46b

Please sign in to comment.