Get link speed programmatically?
Asked Answered
A

2

13

I am writing an application that reports attributes of network devices on the local machine. I need the mac address, mtu, link speed and a few others. I'm using udev for this. I've already figured out how to get the mac address and mtu, but not how to get the link speed. I can get it with ethtool from the terminal, but I need a way to get it programmatically.

Does anyone know how I can get the link speed attribute with udev or another library?

Aigrette answered 20/5, 2010 at 8:23 Comment(2)
With udev? How did you pull that off?Greenhouse
Using the udev_device_get_sysattr_value call with attributes "address" for the Mac address and "mtu" for the MTU.Aigrette
E
34

You need to use the SIOCETHTOOL ioctl() call. There's a nice introduction to ioctl/SIOCETHTOOL call on LinuxJournal, and the code below (which is not intended to be an example of good C practices!) should show you how to use it to get the speed.

#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>

int main (int argc, char **argv)
{
    int sock;
    struct ifreq ifr;
    struct ethtool_cmd edata;
    int rc;

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

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

    edata.cmd = ETHTOOL_GSET;

    rc = ioctl(sock, SIOCETHTOOL, &ifr);
    if (rc < 0) {
        perror("ioctl");
        exit(1);
    }
    switch (ethtool_cmd_speed(&edata)) {
        case SPEED_10: printf("10Mbps\n"); break;
        case SPEED_100: printf("100Mbps\n"); break;
        case SPEED_1000: printf("1Gbps\n"); break;
        case SPEED_2500: printf("2.5Gbps\n"); break;
        case SPEED_10000: printf("10Gbps\n"); break;
        default: printf("Speed returned is %d\n", edata.speed);
    }

    return (0);
}
Expound answered 20/5, 2010 at 18:13 Comment(4)
Thanks! I'll test it tomorrow. (I actually hacked together a working solution by using code snippits from ethtool's source code. It's very similar to this.)Aigrette
This gives an "Operation not permitted" error when running as a non-privileged used on RHEL 5/6. Is there a way to get this information without needing root access?Etherealize
You should use ethtool_cmd_speed(&edata) to get speed response from edata, as there is some high bits that you ignore here.Virus
@MappaM: Good point - I've updated the answer. (When I originally posted this answer, back in 2010, ethtool_cmd_speed() didn't even exist, at least in the distros I was using :) )Expound
U
9

You can get the link speed in bit per second using the sysfs interface:

cat /sys/class/net/eth0/speed
1000
Undeviating answered 28/6, 2018 at 12:6 Comment(1)
Works for me in Linux Mint 18.3 without root. This is exactly what I was looking for.Proscenium

© 2022 - 2024 — McMap. All rights reserved.