ethtool -p 点灯

本文详细解析了通过ethtool工具控制网口指示灯的工作流程。从内核层面介绍了dev_ethtool函数如何调用set_phys_id实现灯的状态变化,并以Hisilicon HNS驱动为例,展示了具体的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当通过ethtool -p eth0 20 时,网口eth0的指示灯会闪烁或者常亮。其在kernel中实现的flow为
int dev_ethtool(struct net *net, struct ifreq *ifr)
{
	case ETHTOOL_PHYS_ID:
		rc = ethtool_phys_id(dev, useraddr);
		break;

}
static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
{

#网口的驱动需要实现set_phys_id 回调函数

	rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
	if (rc < 0)
		return rc;

	/* Drop the RTNL lock while waiting, but prevent reentry or
	 * removal of the device.
	 */
	busy = true;
	dev_hold(dev);
	rtnl_unlock();
#set_phys_id 函数如果返回0的话,说明driver自己会控制灯是常亮还是闪烁。并行在灯常亮或者闪烁
#时,当前thread 处于sleep状态
	if (rc == 0) {
		/* Driver will handle this itself */
		schedule_timeout_interruptible(
			id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
	} else {
#rc 不等于零的话,说明driver 只执行灯的亮或者灭的动作,所以这个采用while 循环来让灯 执行亮和灭 从而实现闪烁
		/* Driver expects to be called at twice the frequency in rc */
		int n = rc * 2, i, interval = HZ / n;

		/* Count down seconds */
		do {
			/* Count down iterations per second */
			i = n;
			do {
				rtnl_lock();
#灯的亮和灭交替进行
				rc = ops->set_phys_id(dev,
				    (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
				rtnl_unlock();
				if (rc)
					break;
				schedule_timeout_interruptible(interval);
			} while (!signal_pending(current) && --i != 0);
		} while (!signal_pending(current) &&
			 (id.data == 0 || --id.data != 0));
	}

	rtnl_lock();
	dev_put(dev);
	busy = false;
#最后把这个灯灭掉
	(void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
	return rc;
}

我们以kernel-master\drivers\net\ethernet\hisilicon\hns 的实现的set_phys_id 来看看具体的实现
static const struct ethtool_ops hns_ethtool_ops = {
	.set_phys_id = hns_set_phys_id,
}
int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
{
	struct hns_nic_priv *priv = netdev_priv(netdev);
	struct hnae_handle *h = priv->ae_handle;
	struct phy_device *phy_dev = netdev->phydev;
	int ret;

	if (phy_dev)
		switch (state) {
#可以看到这里是由phy来实现等的亮和灭的
		case ETHTOOL_ID_ON:
			ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
			if (ret)
				return ret;
			break;
		case ETHTOOL_ID_OFF:
			ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
			if (ret)
				return ret;
			break;
		
}
在hns_phy_led_set 中通过寄存器来控制灯的亮和灭
int hns_phy_led_set(struct net_device *netdev, int value)
{
	int retval;
	struct phy_device *phy_dev = netdev->phydev;

	retval = phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
	retval |= phy_write(phy_dev, HNS_LED_FC_REG, value);
	retval |= phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
	if (retval) {
		netdev_err(netdev, "mdiobus_write fail !\n");
		return retval;
	}
	return 0;
}

### 使用 `ethtool` 测试 CentOS 系统中的网卡指示灯 在 CentOS 或其他基于 Linux 的系统中,可以利用 `ethtool` 工具来管理和调试网络设备。为了测试网卡的物理层状态以及指示灯的行为,通常会涉及以下几个方面: #### 查看当前网卡的状态 通过运行以下命令,可以获得有关网卡的速度、双工模式和其他硬件设置的信息: ```bash ethtool eth0 ``` 此命令将显示诸如速度(speed)、双工模式(duplex)、自动协商(autonegotiation)以及其他重要参数的信息[^4]。 #### 配置网卡并触发指示灯变化 如果希望手动配置某些参数以观察网卡指示灯的变化,则可以执行如下操作: - 设置固定的速度和双工模式: ```bash ethtool -s eth0 speed 1000 duplex full autoneg off ``` 此命令强制将以太网卡设置为千兆全双工模式,并关闭自动协商功能[^1]。 - 如果需要重新启用自动协商以便恢复默认行为,可使用以下命令: ```bash ethtool -s eth0 autoneg on ``` #### 让网卡指示灯闪烁 部分型号的网卡支持通过软件控制其 LED 指示灯进行短暂闪烁,这有助于识别具体的物理连接位置。以下是实现这一功能的方法之一: ```bash ethtool -p eth0 10 ``` 在此例子中,“eth0” 是目标网卡的名字,而 “10” 表示让指示灯持续闪烁的时间长度(单位为秒)。如果没有指定时间,默认情况下可能只闪烁几秒钟[^4]。 #### 调整更深层次的硬件属性 对于高级用途来说,还可以尝试修改网卡 EEPROM 中的内容,但这一步骤需谨慎对待以免损坏硬件或使其无法正常工作。例如下面这条指令展示了如何改变特定偏移处的数据值: ```bash ethtool -E eth0 magic 0x10798086 offset 0x10 value 0x1A ``` 这里需要注意的是不同厂商生产的适配器可能会有不同的魔法数 (magic number),所以在实际应用前最好查阅官方文档确认具体数值含义[^3]。 综上所述,在 CentOS 上借助于强大的 `ethtool` 实用程序不仅可以轻松完成对各种网络接口特性的定制化设定,而且还能方便快捷地验证相应链路质量状况乃至定位潜在故障源等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值