2
我有一個需要查找給定網絡接口(即「eth0」)的速度的linux內核模塊。對於Linux 2.6.31,我將如何找到速度(配置/協商)?如何從linux內核模塊中確定網絡設備速度
我有一個需要查找給定網絡接口(即「eth0」)的速度的linux內核模塊。對於Linux 2.6.31,我將如何找到速度(配置/協商)?如何從linux內核模塊中確定網絡設備速度
每個網絡驅動程序都具有這些功能的「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;
}