开发者

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

I have a linux kernel module that开发者_运维技巧 needs to find the speed of a given network interface (i.e. "eth0"). For linux 2.6.31 how would I find the speed (configured/negotiated)?


Every network driver has a "ethtool" implementation for such features. But you probably need a generic function that can give you the speed for a generic netdev struct. You can have a look at net/core/net-sysfs.c and see how it implements the /sys/class/net interface. For example :

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜