繁体   English   中英

如何从Linux内核模块确定网络设备的速度

[英]how can I determine a network device speed from a linux kernel module

我有一个Linux内核模块,需要查找给定网络接口(即“ eth0”)的速度。 对于linux 2.6.31,我如何找到速度(已配置/协商)?

每个网络驱动程序都有一个用于此类功能的“ ethtool”实现。 但是您可能需要一个通用函数,该函数可以为您提供通用netdev结构的速度。 您可以查看net / core / net-sysfs.c并查看其如何实现/ sys / class / net接口。 例如 :

static ssize_t show_speed(struct device *dev,
          struct device_attribute *attr, char *buf)
{
    struct net_device *netdev = to_net_dev(dev);
    int ret = -EINVAL;

    if (!rtnl_trylock())
        return restart_syscall();

    if (netif_running(netdev) &&
        netdev->ethtool_ops &&
        netdev->ethtool_ops->get_settings) {
        struct ethtool_cmd cmd = { ETHTOOL_GSET };

        if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
            ret = sprintf(buf, fmt_dec, ethtool_cmd_speed(&cmd));
    }
    rtnl_unlock();
    return ret;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM