如何在不轮询内核的情况下在C程序中监视NIC状态(上/下)? [英] How can I monitor the NIC status(up/down) in a C program without polling the kernel?

查看:146
本文介绍了如何在不轮询内核的情况下在C程序中监视NIC状态(上/下)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我需要实时获取NIC的状态(启动或关闭).这意味着当NIC在阻塞循环中上下时,我必须捕获内核中断.

Now I need to get the status of the NIC(up or down) in the real time. That means I have to catch the kernel interrupt when the NIC up or down in a blocked loop.

我的第一个愚蠢方法是检查 /sys/class/net/eth0/operstate 或使用 ioctl 即可在循环中每100毫秒获取一次ifflag.但是100ms太长了,无法使应用重新路由流量,并且每100ms轮询一次内核不是一个好主意.

The first stupid method from mine is that check on the /sys/class/net/eth0/operstate or use ioctl to get the ifflag every 100ms in a loop. But 100ms is too long for the app to reroute the traffic and also polling kernel every 100ms is not good idea.

一旦我注意到 inotify 功能,该功能可以以阻止模式监视文件.但不幸的是,由于/sys位于RAM而非磁盘中,因此它无法监视/sys/class/net/eth0/operstate文件.

Once I notice the inotify function that can monitor the files in a block mode. But unfortunately, it can't monitor the /sys/class/net/eth0/operstate file since /sys is located in the RAM not in the disk.

那么,除了编写内核模块以块模式捕获C程序中的NIC中断(上/下)以外,还有什么方法可用?

So, is there any methods except writing a kernel module to catch the NIC interrupt(up/down) in the C program with a block mode?

推荐答案

是的,打开一个netlink套接字并侦听RTMGRP_LINK(网络接口创建/删除/上/下事件)多播组.

Yes, open a netlink socket and listen to the RTMGRP_LINK (network interface create/delete/up/down events) multicast groups.

netlink手册页此处有一个具体示例为此.

The netlink man page here has a specific example to do this.

这篇关于如何在不轮询内核的情况下在C程序中监视NIC状态(上/下)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆