检查网线是否插入给定的 NIC

Mar*_*ter 7 ethernet ifconfig

服务器有多个 NIC,其中只有一个插入了电缆。

如何测试给定端口是否ethX已插入?

我发现很多类似的问题,但既不ethtool也不cat /sys/class/net/eth1/carrier为我工作。

就我而言,电缆实际上已连接到eth2,但ethtool仍显示Link detected: no

Settings for eth2:
    Supported ports: [ FIBRE ]
    Supported link modes:   10000baseT/Full 
    Supported pause frame use: Symmetric
    Supports auto-negotiation: No
    Advertised link modes:  10000baseT/Full 
    Advertised pause frame use: Symmetric
    Advertised auto-negotiation: No
    Speed: Unknown!
    Duplex: Unknown! (255)
    Port: Direct Attach Copper
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: off
    Supports Wake-on: d
    Wake-on: d
    Current message level: 0x00000007 (7)
                   drv probe link
    Link detected: no
Run Code Online (Sandbox Code Playgroud)

中没有连接电缆eth3,但ethtool输出看起来几乎相同:

diff <(ethtool eth2) <(ethtool eth3)
1c1
< Settings for eth2:
---
> Settings for eth3:
11c11
<   Port: Direct Attach Copper
---
>   Port: Other
Run Code Online (Sandbox Code Playgroud)

首先当我打开界面eth2时,然后ethtool显示Link detected: yes. 之后,即使我关闭界面,也ethtool将链接报告为yes

总之,ethtool好像不是第一次工作,之前界面已经ifuped。

如何可靠地测试给定接口是否已插入电缆

ilk*_*chu 5

我假设您想要查找 NIC 的链接状态,而不是电缆插入插座的物理状况。(这可能是不可能找到的。)

通过快速搜索,我想您已经有了答案。打开界面,等待它找到一个链接,如果有的话(可能需要几秒钟),然后检查ethtool, orcarrier和/或operstatein的输出/sys/class/net/$NIC/

ifconfig somenic up似乎打了这两个ioctl电话:

ioctl(4, SIOCGIFFLAGS, {ifr_name="somenic", ifr_flags=IFF_BROADCAST|IFF_MULTICAST}) = 0
ioctl(4, SIOCSIFFLAGS, {ifr_name="somenic", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
Run Code Online (Sandbox Code Playgroud)

也就是说,它设置IFF_UP. 基于此处,设置实际上导致设备初始化:

然后,它通过(套接字 I/O 控制设置接口标志)设置该IFF_UP位以打开接口。dev->flagioctl(SIOCSIFFLAGS)

不过,后一个命令 ( ioctl(SIOCSIFFLAGS)) 会调用设备的 open 方法。

就实际代码而言,驱动程序必须执行许多与字符和块驱动程序相同的任务。open 请求它需要的任何系统资源并告诉界面出现;

e1000e驱动程序源中有类似效果的评论:

/**
 * e1000e_open - Called when a network interface is made active
 * @netdev: network interface device structure
 *
 * Returns 0 on success, negative value on failure
 *                                                                                                                                                                           * The open entry point is called when a network interface is made
 * active by the system (IFF_UP).  At this point all resources needed
 * for transmit and receive operations are allocated, the interrupt
 * handler is registered with the OS, the watchdog timer is started,
 * and the stack is notified that the interface is ready.
 **/
int e1000e_open(struct net_device *netdev)  
Run Code Online (Sandbox Code Playgroud)

这意味着无法有意义地找到未启动的 NIC 的链接状态,因为硬件甚至不会被初始化。


当然,至少在理论上,某些驱动程序的行为可能有所不同,并在任何人设置之前初始化硬件IFF_UP,但这在一般情况下仍然没有帮助。

在一台计算机(连接e1000e到 Cisco 交换机)上,关闭接口也会使交换机看到链路已关闭。

在另一台机器(具有某些嵌入式 Realtek NIC)上,从上到下的更改会使远程交换机短暂断开连接,但交换机会看到链路然后恢复正常。(ethtool不过,在 PC 端确实显示“无链接”。)这可能与准备 LAN 唤醒等相关,也可能无关,但我真的不知道。


Gnu*_*iff 0

我查看了ethertools源代码,但找不到描述链接状态的地方。

然后我发现这似乎与SO上的问题完全相同:/sf/ask/56599231/ -连接器

我尝试了那里列出的大多数答案(mii-tools、ethertool、cat the Carrier或operstate、dmesg),当 ifconfig 关闭时,它们都不能在链接上正常工作。

由于这个问题已有 6 年多了,我认为大多数可能的答案已经存在。

我的投票是,使用标准 Linux 工具,你无法检查运营商,除非你尝试提出它。之后,mii-tools似乎可以很好地进行链接检测并给出统一的答案。

我没有尝试 rtnetlink 和其他一些答案,它们处理链接状态变化的检测,我认为这不是您想要的。