Linux network subsystem Kernel Analysis, linux subsystem Kernel

Source: Internet
Author: User

Linux network subsystem Kernel Analysis, linux subsystem Kernel

Select route

To send data packets to PC2, the linux system queries the route table to see that the 168.1.1.10 (Destination Address) gateway address is 192.168.1.1. In this case, the linux system selects Nic 1 to send data packets.

2. Neighbor subsystem (establish neighbor information through arp)

When network adapter 1 is selected to send data, the packet is first sent to the neighbor (GATEWAY), and then forwarded to the neighbor. To send data to the neighbor, you must know the MAC address of the neighbor, if you do not know the MAC address of the neighbor, you need to obtain the MAC address of the neighbor through the arp request packet.

 

The Linux network architecture consists of the following five parts: 1) System Call interface 2) protocol-independent interface 3) Network Protocol 4) device-independent interface 5 device driver. The following describes the five parts:

1) System Call Interface

The system call interface is the only valid way for applications in the user space to normally access the kernel (terminal and can also access the kernel ). For example:

Asmlingkage long sys_getpid (void)

{

Return current-> pid;

}

System calls generally start with sys, And the modifier is asmlingkage, indicating that the function gets parameters from the stack.

2) protocol-independent Interfaces

The protocol-independent interface is implemented by socket. It provides a set of common functions to support different protocols.

Socket operations are required for communication through the network stack. The socket Structure in Linux is struct sock, which is defined in linux/include/net/sock. h. This huge structure contains all the status information required by a specific socket, including the specific protocol used by the socket and some operations that can be performed on the socket.

The network subsystem can understand the available protocols by defining a special structure of its own functions. Each Protocol maintains a structure named proto (which can be found in linux/include/net/sock. h ). This structure defines the ability to perform specific socket operations from the socket layer to the transport layer.

3) network protocol

Linux supports multiple network protocols. You can find the supported network protocols in <linux/socket. h>:

# Define AF_UNIX 1/* Unix domain sockets */

# Define AF_LOCAL 1/* POSIX name for AF_UNIX */

# Define AF_INET 2/* Internet IP Protocol */

# Define AF_AX25 3/* Amateur Radio AX.25 */

# Define AF_IPX 4/* Novell IPX

... ...

Each Supported Protocol corresponds to one of the net_family [] arrays. net_family [] is a struct pointer array, and each of them is a struct pointer pointing to a net_proto_family structure.

Struct net_proto_family {

Int family;

Int (* create) (struct socket * sock, int protocol );

Short authentication;

Short encryption;

Short encrypt_net;

Struct module * owner;

}; Information about the protocol is registered in this struct.

4) device-independent Interfaces

The device-independent interface is implemented by net_device. Any device communicates with the upper layer through the net_device device-independent interface.

It connects the protocol to a hardware device with many different features. This layer provides a set of common functions for underlying network device drivers to operate on high-level protocol stacks.

First, the device driver may register or log out of the kernel by calling register_netdevice or unregister_netdevice. The caller first enters the net_device structure and then passes the structure for registration. The kernel calls its init function (if this function is defined), then executes a set of health checks, and creates a sysfs entry, then add the new device to the device list (the linked list of active devices in the kernel ). The net_device structure can be found in linux/include/linux/netdevice. h. These functions are implemented in linux/net/core/dev. c.

To send sk_buff to the device from the protocol layer, use the dev_queue_xmit function. This function can queue sk_buff for final transmission by the underlying Device Driver (using the network device defined by net_device or sk_buff-> dev referenced in sk_buff ). The dev structure contains a method named hard_start_xmit, which stores the driver functions used to initiate the sk_buff transmission.

The netif_rx is usually used to receive packets. When the underlying device driver receives a packet (included in the allocated sk_buff), it will upload the sk_buff to the network layer by calling netif_rx. Then, this function queues the sk_buff in the upper-layer protocol queue through netif_rx_schedule for later processing. You can find the dev_queue_xmit and netif_rx functions in linux/net/core/dev. c.

5) device drivers

The bottom of the network stack is the device driver responsible for managing physical network devices. For example, the SLIP driver used by the packet serial port and the Ethernet driver used by the Ethernet device are both devices at this layer.

During initialization, the device driver allocates a net_device structure and then initializes it using a required program. One of these programs is dev-> hard_start_xmit, which defines how the upper layer should transmit the sk_buff queue. The parameter of this program is sk_buff. The operation of this function depends on the underlying hardware, but the packets described by sk_buff are usually moved to the hardware ring or queue. As described in the device-independent layer, for NAPI-compatible network drivers, the netif_rx and netif_receive_skb interfaces are used for frame receiving. The NAPI driver limits the underlying hardware capabilities.

 

Analyze the kernel code implementation:

1. Send UDP data packets

 In the Application

Use the socket () function to create a socket, and then use the write () function to write data to the socket for sending.

  In the kernel:

A. In the system call layer and protocol-independent Layer

First, find the entry of the application in the kernel through the socket_file_ops structure. The function is sock_aio_write, and then call do_sock_write --- _ sock_sendmsg --- _ sock_sendmsg_nosec.

B. In the network protocol layer

Call udp_sendmsg --- route (select route) route --- route (IP protocol entry) --- ip_local_out --- dst_output --- ip_finish_output --- ip_finish_output2 --- arp_generic_ops-> neigh_resolve_output (create neighbor Information)

C. In the device-independent interface

Call dev_queue_xsce --- dev_hard_start_xmit

D. In the driver layer

Call ndo_start_xmit

 

2. Receive IP data packets

  In the Application

Obtain the package content by calling the recvmsg function.

  In the kernel:

When the network adapter receives a packet, it is interrupted. The netif_rx function is used to send the packet to the netif_rx_action (except the driver layer, the above layer of the total entry), continue to call netif_receive_skb (determine which Protocol to handle the package) --- deliver_skb, and then submit the packet to the IP protocol layer processing (ip_rcv-ip protocol stack layer processing entry ), then there is the udp protocol layer processing (udp_rcv-udp protocol stack layer processing portal), after the udp layer processing, submit to sock-> ops-> recvmsg, that is, the corresponding function called by the recvmsg system.

 

 

  

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.