简体   繁体   English

在linux上获取NIC /以太网卡链接状态的事件

[英]Get event for NIC/ethernet card link status on linux

Is there any API(or special file) on Linux that provides an event on the change in link status of an ethernet card without polling ? Linux上是否有任何API(或特殊文件)在没有轮询的情况下提供有关以太网卡链路状态变化的事件? ie when you plug in/out the cable ? 即当你插入/拔出电缆?

如果此文件/sys/class/net/eth0/carrier为1,则接口eth0具有carrier。

You can get link up/down events via netlink. 您可以通过netlink获取链接上/下事件。 You would create a PF_NETLINK socket, bind to the RTMGRP_LINK group, and wait for RTM_NEWLINK / RTM_DELLINK messages. 您将创建PF_NETLINK套接字,绑定到RTMGRP_LINK组,并等待RTM_NEWLINK / RTM_DELLINK消息。 Link up events have IFF_RUNNING and IFF_LOWER_UP set in ifinfomsg.flags , while link down events have both flags cleared (funnily enough, I'm getting RTM_NEWLINK messages for both type of events). 链接事件在IFF_RUNNING设置了IFF_RUNNINGIFF_LOWER_UP ,而链接ifinfomsg.flags事件都清除了两个标志(有趣的是,我收到了两种类型事件的RTM_NEWLINK消息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM