VMware Cloud Community
jrallen9
Enthusiast
Enthusiast

Patch proposal for setting Linux OS ifstat->speed attribute

I'm hoping to get some feedback on code I wrote to retrieve the network interface link speed for the Linux OS.

in sigar build 1.6.4-0-g4b67f57 linux_sigar.c:1771
ifstat->speed = SIGAR_FIELD_NOTIMPL;

I'm proposing a function which can make a system call to ioctrl to retrieve this information from the device drivers. I'm hoping that if this code could be included in future builds if it proves reliable and consistent. Here is the code I have written and tested to retrieve this information:


/**
* Author: Justin Allen (jallen (AT)ucsd.edu)
* Date: 6/18/2010
* This code is provided on an "as is" basis, with no conditions or warranties, either expressed or implied.

* Prototype program to get the network link speed of a network interface by making a system call to ioctrl to query the driver
*/
#include < stdio.h >
#include < sys/socket.h >
#include < sys/ioctl.h >
#include < netinet/in.h >
#include < linux/sockios.h >
#include < linux/if.h >
#include < linux/ethtool.h >
#include < string.h >
#include < stdlib.h >


long get_link_speed(const char* interface)
{
int sock, rc;
struct ifreq ifr;
struct ethtool_cmd edata;

sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock<0) {
perror("socket");
return -1;
}

strncpy(ifr.ifr_name, interface , sizeof(ifr.ifr_name));
ifr.ifr_data = &edata;

edata.cmd = ETHTOOL_GSET;

rc = ioctl(sock, SIOCETHTOOL, &ifr);
if (rc<0) {
perror("ioctl");
return -1;
}
switch (edata.speed) {
case SPEED_10: return (edata.speed * 1000000); break; // 10Mbps
case SPEED_100: return (edata.speed * 1000000); break; //100Mbps
case SPEED_1000: return (edata.speed * 1000000000); break; // 1Gbps
case SPEED_2500: return (edata.speed * 1000000000); break; // 2.5Gbps
case SPEED_10000: return (edata.speed * 1000000000); break; // 10Gbps
default: return (edata.speed);
}

return (edata.speed);
}

int main (int argc, char **argv)
{
fprintf(stdout, "speed: %d\n", get_link_speed("eth0"));

return 0;
}

Message was edited by: jrallen9

Message was edited by: jrallen9

Message was edited by: jrallen9

Message was edited by: jrallen9
Reply
0 Kudos
7 Replies
jrallen9
Enthusiast
Enthusiast

case SPEED_100: return (edata.speed * 1000000); break; //100Mbps
case SPEED_1000: return (edata.speed * 1000000000); break; // 1Gbps
case SPEED_2500: return (edata.speed * 1000000000); break; // 2.5Gbps
case SPEED_10000: return (edata.speed * 1000000000); break; // 10Gbps
default: return (edata.speed);
}

return (edata.speed);
}

int main (int argc, char **argv)
{
fprintf(stdout, "speed: %d\n", get_link_speed("eth0"));

return 0;
}
Reply
0 Kudos
dougm_hyperic
VMware Employee
VMware Employee

Hi Justin,

Thanks for digging into this! I've opened a ticket to track this: http://jira.hyperic.com/browse/SIGAR-220

I had started looking into this a few years ago, one issue I believe was that the ioctl ETHTOOL interface requires root permissions, is this still the case? Also wondering if the interface speed is available somewhere in the /proc file system these days?

In any case, if you want to take a stab at implementing this within sigar, doing so as a fork on github would be ideal. Even just having your prototype program as a gist on github would make it easier to work with. Happy to help here and get this functionality folded into the next release.

Thanks,
-Doug
Reply
0 Kudos
jrallen9
Enthusiast
Enthusiast

Hey Doug,

I've taken a look at the proc files for any indication of link speed, but to this date that device info is not there, but this has only been on Linux Kernels 2.6.17, for some testing servers I have available to me. Also, the web searches I've performed aren't revealing any additional info for more recent kernels. There appears to be a gap on Linux for network device info outside of ethtool and mii-tool

As you have found in the past any function call to ioctrl requires that the exec environment running SIGAR has the function capable(CAP_NET_ADMIN) return true. Otherwise ioctrl returns -EPERM.

I can't say I'm a subject matter expert in OS programming (my background is a couple of University courses and a couple of small scale applications), but I've consulted a couple of experts on this matter and I've been assured that the link speed info doesn't exist in /proc.

So I guess from my perspective the philosophy of SIGAR is to provide user-mode system performance data only. I do see the value in this philosophy, but I could argue that the code should be written to acquire an attribute in all permission environments, and if the metric isn't available to a particular group, then fall back on SIGAR_NOTIMP. I'm advocating this solution using ioctrl to populate the network link speed in Linux when running the application under admin privileges.

I suppose the last alternative is to replicate ethtool code by inclusion of various headers from multiple device vendors. I don't suppose that solution would be clean or maintainable over the long run.

Thanks,
Justin
Reply
0 Kudos
dougm_hyperic
VMware Employee
VMware Employee

Hi Justin,

I have no problem with having sigar use the ioctl and setting the field to SIGAR_FIELD_NOTIMPL on EPERM. I recall another issue was that ethtool and it's header files don't exist in 2.2 kernels. The current 1.6.x releases still support 2.2 kernels and our x86-linux binary is built on 2.2. We were already considering dropping 2.2 support in the 1.7 release (in the binary version at least), so this could be another motivator. In any case, it would be most helpful if you made your code available on github or similar. We should be able to get this done in the 1.7 release.

Thanks,
-Doug
Reply
0 Kudos
stuart890
Contributor
Contributor

Thanks for such a terrific publish and the review, I am completely impressed! Maintain stuff like this coming.







L-Argenine
Reply
0 Kudos
JeanDagenais
Contributor
Contributor

Hello,

I was wondering if this fix is still planned for 1.7, and if this is possible to have an early version to test it on Red Hat 5.5 and Red Hat 6.0 - 64 bits,with Sun JDK 1.6 64 bits.

Thanks!
Jean
Reply
0 Kudos
VeresBogdan
Contributor
Contributor

patch against latest master

Reply
0 Kudos