檢查網路電纜是否已插入給定的 NIC

檢查網路電纜是否已插入給定的 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

中沒有連接電纜eth3,但ethtool輸出看起來幾乎相同:

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

首先,當我調出介面時eth2,然後ethtool顯示Link detected: yes。之後,即使我關閉介面,也會ethtool將連結報告為。yes

簡而言之,ethtool在介面被 ifuped 之前,第一次似乎不起作用。

如何可靠地測試給定介面是否已插入電纜

答案1

我假設您想要查找 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

也就是說,它設定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)  

這意味著沒有辦法有意義地找到不存在的 NIC 的連結狀態。向上,因為硬體甚至不會被初始化。


當然,至少在理論上,某些驅動程式的行為可能有所不同,並在任何人設置之前初始化硬件IFF_UP,但這在一般情況下仍然沒有幫助。

在一台電腦(連接e1000e至 Cisco 交換器)上,關閉介面也會使交換器看到連結已關閉。

在另一台電腦(具有某些嵌入式 Realtek NIC)上,從開啟到關閉的變更使遠端交換器短暫斷開連接,但交換器看到連結然後又恢復正常。 (ethtool不過,在 PC 端確實顯示「無連結」。)這可能與準備 LAN 喚醒等相關,也可能無關,但我真的不知道。

答案2

嘗試ifplugstatus包中的命令如果插件

$ ifplugstatus net0
net0: unplugged

$ ifplugstatus wlnet0
wlnet0: link beat detected

除了螢幕輸出之外,您還可以透過命令的退出狀態來判斷。

(取自線上說明頁)

傳回值

  0 Success

  1 Failure

  2 Link beat detected (only available when an interface is specified)

  3 Unplugged (same here)

答案3

我查看了ethertools原始碼,但找不到描述連結狀態的地方。

然後我發現這似乎與 SO 上的問題完全相同:https://stackoverflow.com/questions/808560/how-to-detect-the-physical-connected-state-of-a-network-cable-connector

我嘗試了那裡列出的大多數答案(mii-tools、ethertool、cat the Carrier或operstate、dmesg),當 ifconfig 關閉時,它們都不能在連結上正常工作。

由於SO問題已有6年多了,我認為大多數可能的答案已經在那裡了。

我的投票是,使用標準 Linux 工具,你無法檢查運營商,除非你嘗試提出它。之後,mii-tools似乎可以很好地進行連結檢測並給出統一的答案。

我沒有嘗試 rtnetlink 和其他一些答案,它們涉及檢測變化處於鏈接狀態,我認為這不是您想要的。

相關內容