Refer:
Linux Kernel Source:
first_net_device() ↗
netif_running() ↗
Here is the copy paste of netif_running() API implementation (include/linux/netdevice.h) from the Kernel-source version 6.6 for quick reference:
/**
* netif_running - test if up
* @dev: network device
*
* Test if the device has been brought up.
*/
static inline bool netif_running(const struct net_device *dev)
{
return test_bit(__LINK_STATE_START, &dev->state);
}
Download this episode my entire kernel module sample code, make file, clean script HERE.
And here is the same source code for a quick reference.
/*---------- The Linux Channel -----------------*/
/* Author: Kiran Kankipati
* Contact: [email protected]
* Website: https://thelinuxchannel.org
* Date: 16-sep-2016
----------------------------------------------*/
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>
#include <linux/udp.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/inet.h>
#include <linux/inetdevice.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/if_arp.h>
#include <linux/icmp.h>
#include <linux/netlink.h>
#include <linux/mroute.h>
#include <net/checksum.h>
#include <net/inet_ecn.h>
#include <net/xfrm.h>
#include <net/route.h>
#include <net/sock.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/arp.h>
#include <net/udp.h>
#include <net/icmp.h>
#include <net/inetpeer.h>
#include <net/protocol.h>
#include <net/flow.h>
#include <asm/types.h>
//REFER: http://lxr.free-electrons.com/source/include/linux/netdevice.h?v=3.13
static int mymodule_init(void)
{
struct net_device *dev = first_net_device(&init_net);
while(dev)
{ printk("Dev: name: %s - ifindex: %d\n", dev->name, dev->ifindex);
if(netif_running(dev)) { printk("Device is up\n"); } else { printk("Device is down\n"); }
if(test_bit(__LINK_STATE_NOCARRIER, &dev->state)) { printk("NOCARRIER set\n"); } else { printk("NOCARRIER NOT-set\n"); }
dev = next_net_device(dev);
}
return 0;
}
static void mymodule_exit(void)
{
}
module_init(mymodule_init);
module_exit(mymodule_exit);
Its makefile:
obj-m += Kernel_module.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules