"Drive" NIC drives Linux core network hierarchy

Source: Internet
Author: User

Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://liucw.blog.51cto.com/6751239/1221140

Preface

The Linux kernel uses a unified interface for network drivers and is designed for network devices using object-oriented thinking.

The Linux kernel uses a hierarchical structure to process network packets. Hierarchical structure matching with network protocol can simplify the process of packet processing and facilitate the expansion and maintenance.

Kernel Network Architecture

In the Linux kernel, the network part according to the network protocol layer , the network equipment layer , the device drive function layer and the network medium layer layered system design.

The Network driver function layer is mainly implemented through the network Driver program.

In the Linux kernel, all network devices are abstracted into an interface that provides all the network operations.

The Net_device structure represents the situation of the network device in the kernel, that is, the network device interface. The network device interface includes both a software virtual network device interface, such as a loop device, and a network hardware device, such as an Ethernet card.

The Linux kernel has a dev_base global Pointer to a list of devices, including all network devices within the system. The device lists each node as a network device.

A number of device methods for system access and protocol layer invocation are provided in the NET_DEVICE structure, including initialization, turning off devices, sending and receiving packets, and so on.

Network-related data structures

The kernel's processing of network packets is based on the SK_BUFF structure , which is the most important data structure in the kernel network part.

each layer protocol in the network protocol stack can be added or deleted by the operation of this layer protocol data. using the Sk_buff structure avoids the inefficiencies caused by copying data back and forth across the network protocol stack.

The Sk_buff structure can be divided into two parts, part of which is the storage of the packet cache, represented as Packertdata in the diagram, and a set of pointers for kernel management.

The most important pointers to Sk_buff management are the following 4:

      • head points to the core header of the data buffer (packertdata);

      • data points to the first address of the current packet;

      • tail points to the end address of the current packet;

      • end points to the kernel-end address of the data buffer.

The size of the packet changes during the processing of the kernel network protocol stack, so the data and tail pointers are constantly changing, and the head and tail pointers do not change.

For an example of a TCP packet,Sk_buff also provides several pointers directly to the layer protocol headers. the Mac pointer points to the MAC header of the data, the NH pointer points to the network protocol header, usually the IP protocol header, and H points to the Transport Layer protocol header, in this case the TCP protocol header.

It is convenient for the protocol stack to handle the packets by setting pointers to each layer.

Net_device structure

The most important data structure of network devices in the Linux kernel is the net_device structure , which is the most important part of the network driver.

Net_device structure is saved in the Include/linux/netdevices.h header file, understanding the structure is very helpful for understanding the network device driver.

The information and operations of all network devices in the kernel are used in the Net_device device, whether it is registering a network device or setting network device parameters.

The following are the primary data members.

      • Device Name

      • Bus parameters

      • Protocol parameters

      • Link Layer Variables

      • Interface Flags

Packet Receipt Process

In the Linux kernel, a network packet receives the user space from the network card, which needs to be processed by the link layer, the transport layer and the socket to reach the user space eventually.

Take the DM9000 Nic as an example, when the NIC receives the packet, the interrupt handler function Dm9000_interrupt ()is called, and the function checks the interrupt processing type and, if it is a packet interrupt, calls Dm9000_rx () The function receives the packet into the kernel space.

After the DM9000_RX () function receives the packet completion, the kernel continues to call the Netif_rx () function, which is the function of receiving the network card to submit the data to the protocol stack processing.

The protocol stack uses the net_rx_action () function to handle the receiving packet queue, which is processed by the ip_recv () function if the IP packet is handled after the packet is disposed of. The IP_RECV () function is primarily to check the legitimacy of a packet IP header, which is passed to the ip_local_deliver () and ip_local_deliver_finish () functions for processing, This is handled separately because firewall-related code in the kernel needs to be loaded dynamically here.

After the IP header processing, take the UDP packet as an example will be referred to the udp_recv () function, and IP_RECV () function class billion, the function checks the legitimacy of the UDP header, and then to the udp_queue_recv () function processing, Last submitted to the sock_queue_recv () function for processing.

The first function that the packet enters the socket section is Skb_recv_datagram (), which takes the packet out of the socket queue of the kernel and gives it to the udp_recvmsg () function in the socket section. The function is responsible for processing UDP data, and sock_recvmsg () processing is submitted to the Sock_read () function.

The Sock_read () function reads the received data buffers and returns the data to the Sys_read () system call. The Sys_read () function call eventually copies the data to the user space for the user to make.

Packet sending process

In the case of the UDP packet sending process, how to send a packet on the DM9000 NIC.

When a user-space application sends a UDP data through the socket function Sento () , it calls the Sock_writev () function of the kernel space and then passes the sock_sendmsg () function processing. The sock_sendmsg () function calls the inet_sendmsg () function, and the inet_sendmsg () function handles the data to be sent to the udp_sendmsg () function of the transport layer.

The Udp_sendmsg () function joins the UDP header before the data and then gives the data to the ip_build_xmit () function, which constructs the IP header based on the destination IP and port information provided by the socket, and then calls the Output_ Maybe_reroute () function processing. The Out_maybe_reroute () function checks whether the packet needs to be routed, and finally the ip_output () function is written to the send queue, which is processed by the ip_finish_output () function after the write is completed.

The dev_queue_xmit () function of the link layer processes the Send queue, calls the Send packet function of the DM9000 NIC dm9000_xmit () sends the packet, and after it is sent, calls Dm9000_xmit The _done function handles sending results.

Drive NIC drives Linux core network hierarchy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.