OSI Layer in Linux Kernel


Socket Buffer


Network Namespace

struct net {
	atomic_t count; /* To decided when the network
	* namespace should be freed.
	struct list_head list; /* list of network namespaces */
	struct proc_dir_entry *proc_net;
	struct proc_dir_entry *proc_net_stat;
	struct proc_dir_entry *proc_net_root;
	struct net_device *loopback_dev; /* The loopback */
	struct list_head dev_base_head; /* all devices associated with a specific namespace
	struct hlist_head *dev_name_head;
	struct hlist_head *dev_index_head;

The kernel employs the following structure to keep track of all required initialization/cleanup tuples:

struct pernet_operations {
	struct list_head list;
	int (*init)(struct net *net);
	void (*exit)(struct net *net);

pernet_list is a list of All available pernet_operation instances. The auxiliary functions register_pernet_subsys and unregister_pernet_subsys add and remove elements to and from the list, respectively.

Network Device

Each network device is registered in a two-step process:

  1. alloc_netdev allocates a new instance of struct net_device, and a protocol-specific function fills the structure with typical values.
  2. Once struct net_device is completely filled in, it needs to be registered with register_netdev or register_netdevice.

Receving Packets


netif_rx is not a device specific function, Its call marks the transition between the card-specific part and the universal interface of the network layer.

The purpose of this function is to place the received packet on a CPU-specific wait queue and to exit the interrupt context so that the CPU can perform other activities

The kernel manages the wait queues of incoming and outgoing packets in the globally defined softnet_data array, which contains entries of type softnet_data.

struct softnet_data
	struct sk_buff_head input_pkt_queue;

input_pkt_queue: a linked list of all incoming packets.


All network layer functions used to receive data from the underlying network access layer are registered in a hash table implemented by the global array ptype_base.

New protocols are added by means of dev_add_pack.


NAPI uses a combination of IRQs and polling to support high speed network device.

  1. The first packet causes the network adapter to issue an IRQ. To prevent further packets from causing more IRQs, the driver turns off Rx IRQs for the adapter. Additionally, the adapter is placed on a poll list.
  2. The kernel then polls the device on the poll list as long as no further packets wait to be processed on the adapter.
  3. Rx interrupts are re-enabled again.


Network Layer

the layer is responsible not only for sending and receiving data, but also for forwarding and routing packets between systems not directly connected with each other.