获取 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]
", dev->name);
dev = next_net_device(dev);
}
read_unlock(&dev_base_lock);
这篇关于获取 Linux 内核中的网络设备列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!