TOFFEE

VIDEOS :: Linux Kernel net_device data-structure

Written by: Kiran Kankipati
Published: 05-Jul-2016


Linux Kernel net_device data-structure - netif_running() API and state variable - Episode6

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: kiran.kankipati@gmail.com
* Website: www.the-toffee-project.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);

Refer:
first_net_device() - http://lxr.free-electrons.com/source/include/linux/netdevice.h#L2388
netif_running() - http://lxr.free-electrons.com/source/include/linux/netdevice.h#L3054

Linux Kernel net_device data-structure - Traverse net_device list - Episode5

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.

#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>



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);
		dev = next_net_device(dev);
  	}

	return 0;	
}

static void mymodule_exit(void)
{

}

module_init(mymodule_init);
module_exit(mymodule_exit);

Refer:
first_net_device() - http://lxr.free-electrons.com/source/include/linux/netdevice.h#L2388
next_net_device() - http://lxr.free-electrons.com/source/include/linux/netdevice.h#L2368

Linux Kernel net_device data-structure - dev_get_by_index API - Episode4

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.

#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>



static int mymodule_init(void)
{	
	struct net_device *dev=dev_get_by_index(&init_net, 1);
	
	if(dev!=NULL)
	{	printk("Dev: name: %s - ifindex: %d\n", dev->name, dev->ifindex); }

	return 0;	
}

static void mymodule_exit(void)
{

}

module_init(mymodule_init);
module_exit(mymodule_exit);

Refer:
dev_get_by_index() prototype - http://lxr.free-electrons.com/source/include/linux/netdevice.h#L2450
dev_get_by_index() implementation - http://lxr.free-electrons.com/source/net/core/dev.c#L850

Linux Kernel net_device data-structure - dev_get_by_name API - Episode3

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.

#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>



static int mymodule_init(void)
{	
	struct net_device *dev=dev_get_by_name(&init_net, "eno1");
	
	if(dev!=NULL)
	{	printk("Dev: name: %s - ifindex: %d\n", dev->name, dev->ifindex); }

	return 0;	
}

static void mymodule_exit(void)
{

}

module_init(mymodule_init);
module_exit(mymodule_exit);

Refer:
dev_get_by_name() prototype - http://lxr.free-electrons.com/source/net/core/dev.c#L776
dev_get_by_name() implementation - http://lxr.free-electrons.com/source/include/linux/netdevice.h#L2417

Linux Kernel netdevice data-structure video series - part2 - Code-walk, Example

Refer:
struct net_device - http://lxr.free-electrons.com/source/include/linux/netdevice.h#L1645

Linux Kernel netdevice data-structure video series - part1 - Introduction


Thank you, Kiran




Back to Top