When I try to print the maximal throughput of my network interface, I receive an error:

$ cat /sys/class/net/<interface>/speed
cat: /sys/class/net/<interface>/speed: Invalid argument

response. According to my investigation, this attribute is only valid for interfaces that implement ethtool get_link_ksettings method (source).

My goal is to calculate network utilization for each interface by fetching the sent and received number of bytes each second (parsing the /proc/net/dev pseudo-file) and then using throughput to calculate percentage of utilization. I am doing this in C, so max throughput is obtained via ioctl() function that is unable to succeed for interfaces with unknown speed.

Is there a way how I can obtain the speed attribute in some other way? The simplest solution I was able to come up with is to say that the default speed of an interface is standard 10Mbps and if the utilization is over 100%, I can step the max speed up to the next standard speed of 1000Mbps and so on for 1Gbps, 2.5Gbps and 10Gbps. This could somehow work, but it may get broken with bundled interfaces utilizing non-standard speeds, e.g., two 1Gbps cards bundled into one 2Gbps. Bundled interfaces are not a big concern for me, but having general solution would be awesome.

  • I can give some java code to enumerate the adapters, if it can be any help – Joe Apr 9 at 7:09
  • @Joe Whatever information/help I can get might be useful! Thanks. – Samo Poláček Apr 9 at 7:13
  • I think the closest you can get to that information is by doing a test connection with a sustained stream of data towards a well-known server whose backbone speed is well established and known in advance, and also assuming that your unknown interface’s speed is at most not bigger than that backbone’s speed – LL3 Apr 9 at 9:31
  • @LL3 Thanks for the suggestion, but I'm not allowed to stream data to internet to test the connection speed. – Samo Poláček Apr 9 at 9:45
  • Not to internet, no, but to a local server perhaps ? I assumed you were concerning a LAN setup, what with possible 1-2-10Gbps speeds. Besides, if you’re measuring throughput to/from internet then also the /sys/class/net/<intf>/speed info is usually of no real use even when present and accurate. If your interface in question is of direct WAN type (e.g. an xDSL, or 4G interface) then there should be some link layer protocol (e.g. PPP) running over it, whose daemon you might try to query ? – LL3 Apr 9 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.