2010-05-15 73 views

回答

4

每个网络驱动程序都具有这些功能的“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; 
}