在C中找出eth设备链接速度功能

时间:2014-05-16 02:02:35

标签: c linux networking network-programming

我希望获得未在C程序中以编程方式插入的eth设备的链接速度。

我有一个连接eth6且eth8未连接的系统。

# ethtool  eth6 | egrep "modes|Speed|Link"
    Supported link modes:   10000baseT/Full
    Advertised link modes:  10000baseT/Full
    Speed: 10000Mb/s
    Link detected: yes
# /u/aalmehdi/bin/ethtool  eth8 | egrep "modes|Speed|Link"
    Supported link modes:   10000baseT/Full
    Advertised link modes:  10000baseT/Full
    Speed: Unknown!
    Link detected: no

我有以下C程序:

#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/ethtool.h>
#include <string.h>

int main(int argc, char *argv[])
{
    struct ifaddrs *ifaddr, *ifa;
    int family, n;
    int sock;
    struct ifreq ifr;
    struct ethtool_cmd edata;
    int rc;

    if (getifaddrs(&ifaddr) == -1) {
        perror("getifaddrs");
        exit(EXIT_FAILURE);
    }

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

    for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
        if (ifa->ifa_addr == NULL)
            continue;

        family = ifa->ifa_addr->sa_family;

        /* For an AF_PACKET (and physical eth) interface, display the speed */

        if (family == AF_PACKET && ifa->ifa_data != NULL &&
            ((ifa->ifa_flags & (IFF_BROADCAST | IFF_MULTICAST)) ==
                              (IFF_BROADCAST | IFF_MULTICAST))) {

            strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
            ifr.ifr_data = (char *)&edata;
            edata.cmd = ETHTOOL_GSET;

            rc = ioctl(sock, SIOCETHTOOL, &ifr);
            if (rc < 0) {
                perror("ioctl");
                exit(1);
            }

            printf("%-8s (ifa_flags = %x)\n",
                   ifa->ifa_name, ifa->ifa_flags);

            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 0x%x\n", edata.speed);
            }
        }
    }

    freeifaddrs(ifaddr);
    exit(EXIT_SUCCESS);
}

当我运行程序时,对于eth6,我得到了正确的答案,但eth8的值无效。我猜这是因为没有插入eth8。

# ./get_10g_dev
eth6     (ifa_flags = 11043)
10Gbps
eth8     (ifa_flags = 1003)
Speed returned is 0xffff

我真正想要的是访问ethtool显示的“支持的链接模式”或“广告链接模式”。

有人可以帮忙。

谢谢你, 艾哈迈德。

1 个答案:

答案 0 :(得分:3)

通过查看ethtool源代码,我找到了答案。检查字段中设置的特定位 - struct ethtool_cmd.supportedstruct ethtool_cmd.advertising如果端口(可以操作)以10G速度/模式传送。

        if (edata.supported & SUPPORTED_10000baseT_Full)
            printf("-> 10G supported\n");
        else
            printf("-> 10G NOT supported\n");
        if (edata.advertising & ADVERTISED_10000baseT_Full)
            printf("-> 10G advertised\n");
        else
            printf("-> 10G NOT advertised\n");