Get link speed programmatically?

后端 未结 2 1288
一整个雨季
一整个雨季 2020-12-05 11:45

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 th

相关标签:
2条回答
  • 2020-12-05 12:17

    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);
    }
    
    0 讨论(0)
  • 2020-12-05 12:30

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

    cat /sys/class/net/eth0/speed
    1000
    
    0 讨论(0)
提交回复
热议问题