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