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.
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.
Given a
struct net *net
identifying the net namespace that you are interested in, you should grab thedev_base_lock
and usefor_each_netdev()
:(In newer kernels, you can use RCU instead, but that is probably an overcomplication in this case).
To obtain the
net
namespace to use, you should be registering yourproc
file withregister_pernet_subsys()
:In your
foostats_seq_open()
function, you take a reference on thenet
namespace, and drop it in the release function:The
foostats_seq_show()
function can then obtain thenet
, walk the devices, gather the statistics and produce the output:This ought to do the trick: