获取Linux内核中的网络设备列表 [英] Getting list of network devices inside the Linux kernel
问题描述
我一直在浏览net/core/dev.c和其他文件,以试图找到如何获取当前已配置的网络设备的列表,事实证明这很难找到.
I've been looking through net/core/dev.c and other files to try to find out how to get the list of network devices that are currently configured and it's proving to be a little difficult to find.
最终目标是能够使用dev.c中的dev_get_stats获取网络设备统计信息,但是我需要了解当前接口,以便可以抓取net_device结构以进行传递.我必须在内核,因为我正在编写一个模块,该模块添加了一个新的/proc/条目,该条目与当前网络设备的某些统计信息相关,因此,根据我的收集,这必须在内核内部完成.
The end goal is to be able to get network device statistics using dev_get_stats in dev.c, but I need to know the current interfaces so I can grab the net_device struct to pass in. I'm having to do this inside the kernel as I'm writing a module which adds in a new /proc/ entry which relates to some statistics from the current network devices so from what I can gather this must be done inside the kernel.
如果有人可以指出我如何获取界面,将不胜感激.
If someone could point me to how to get the interfaces it would be much appreciated.
推荐答案
这应该可以解决问题:
#include <linux/netdevice.h>
struct net_device *dev;
read_lock(&dev_base_lock);
dev = first_net_device(&init_net);
while (dev) {
printk(KERN_INFO "found [%s]\n", dev->name);
dev = next_net_device(dev);
}
read_unlock(&dev_base_lock);
这篇关于获取Linux内核中的网络设备列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!