Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Linux kernel network subsystem analysis     - MongoDB Learning the notes (Database)

- Necessity in Java packages (Programming)

- C ++ input and output summary _ Input (Programming)

- Linux systems use IP masquerading anti-hacker (Linux)

- netfilter- in kernel mode network packet operation (Linux)

- To install Gitolite in Ubuntu / Fedora / CentOS (Linux)

- Linux bash: scp: command not found the problem (Linux)

- How to compile and install wxWidgets in Ubuntu / Debian / Linux Mint (Linux)

- Git Installation and Configuration (Network Agent settings) (Linux)

- How to install OpenOffice Ubuntu or Linux Mint (Linux)

- Gnu Linux - Ubuntu System Clean-term consolidation (Linux)

- Use backup-manager system backup on Linux (Linux)

- Nagios (centreon) monitoring LVS (Server)

- Linux fast set ip bond (Linux)

- Quickly build and install Linux KVM system (Linux)

- Java input and output common class Scanner (Programming)

- Eclipse-ee Tomcat browser can not start Tomcat, and Web project service deployment (Server)

- Go build the locale under Windows (Linux)

- C ++ 11 smart pointers (Programming)

- Use Python automatically cleared Android Engineering excess resources (Programming)

  Linux kernel network subsystem analysis
  Add Date : 2018-11-21      
  1. Select a route

To send a packet to PC2, then the Linux system through the routing table found (the destination address) of the gateway address is, this time Linux system selects the card 1 transmits packets.

2. neighborhood subsystem (up the neighbor information through arp protocol)

When you select the card transmits data 1, the first packet to the neighbor (the gateway), and then forwarded by neighbors to the back, to be sent to a neighbor, you must know the MAC address of the neighbor, the neighbor does not know the MAC address, you need to arp request packet to obtain the MAC address of the neighbor.

Linux network architecture consists of the following five parts 1) system call interface 2) protocol-independent interfaces 3) Network Protocol 4) Device-independent device driver interface 5. The following are brief five parts:

    1) system call interface

    System call interface is user-space applications access the kernel only legal way (the terminal and can also be accessed into the kernel). Such as:

    asmlingkage long sys_getpid (void)


    return current-> pid;


    System calls are generally made at the beginning of sys, front modifier is asmlingkage, showing the function parameters obtained by the stack.

    2) protocol independent interface

    Protocol-independent interfaces are implemented by the socket. It provides a common set of functions to support a variety of protocols.

    Communication via the network stack needs to socket to operate. Linux in the socket structure is struct sock, this structure is in linux / include / net / sock.h defined. This huge structure contains all the state information required for a particular socket, including the particular protocol used by the socket on the socket and some of the operations that can be performed.

    Network subsystem is defined by a unique structure of its own functions to understand the available protocols. Each protocol maintains a structure called proto (found in linux / include / net / sock.h in). This structure defines the socket can perform specific actions from the socket layer to the transport layer

    3) Network Protocol

    Linux supports multiple network protocols, can be found in the supported network protocols in in:

    #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

    ... ...

    Wherein each of the protocols supported by the corresponding net_family [] an array, net_family [] is a pointer to an array of structures, each of which is a structure pointer to a structure net_proto_family

    struct net_proto_family {

    int family;

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

    short authentication;

    short encryption;

    short encrypt_net;

    struct module * owner;

    }; This structure registered information about the protocol.

    4) Device independent interface

    Device-independent interface is implemented by net_device implemented. Any device and is independent interface overlay communication through net_device equipment.

    It has a number of agreements with various functions of hardware devices connected together. This layer provides a common set of functions for the underlying network device driver to use, so they can operate on the high-level protocol stack.

    First, the device driver may or by calling register_netdevice unregister_netdevice register or log in the kernel. The caller first fill net_device structure and then passes this structure to register. Kernel calls its init function (if the definition of this function), and then perform a sanity check, and creates a sysfs entry, and then add a new device to the device list (list of active devices in the kernel). In linux / include / linux / netdevice.h net_device can be found in this structure. These functions are in linux / net / core / dev.c implemented.

    Sk_buff sent from the protocol layer to the device, you need to use dev_queue_xmit function. This function can be queued for sk_buff, thereby performing the low-level device drivers, a final transmission (use sk_buff referenced net_device or sk_buff-> dev defined network equipment). dev structure contains a method called hard_start_xmit, wherein the preservation of driver functions initiated sk_buff transport used.

    The received message is usually performed using netif_rx. When the underlying device driver receives a packet (contained in the allocated sk_buff in), by calling netif_rx will sk_buff will be uploaded to the network layer. Then, this function will sk_buff by netif_rx_schedule queued in the upper layer protocol queue for later processing. You can find dev_queue_xmit and netif_rx function in linux / net / core / dev.c in.

    5) Device Drivers

    The bottom of the network stack is responsible for managing the physical network device driver device. For example, SLIP driver package using serial and Ethernet drivers for the Ethernet device is this layer of the device.

    During initialization, the device driver will be assigned a net_device structure, and then use the program must be initialized. These programs are a dev-> hard_start_xmit, which defines how to sk_buff upper queued for transmission. The parameters of this program is sk_buff. The operation of this function depends on the underlying hardware, but usually sk_buff described packets are moved to hardware ring or queue. Is the same as the device-independent layer as described, for NAPI-compliant network driver, the frame is received and used netif_rx netif_receive_skb interface. NAPI driver will have the ability to make some of the underlying hardware limitations.

Analysis of the kernel code:

1. Send UDP packets

In the application

() Function to create a socket by socket, then write () function writes data socket sent to speak

In the kernel:

a. In the system call level and protocol-independent layer

First, find the application in the kernel entry through socket_file_ops structure, its function is sock_aio_write, immediately call do_sock_write ---__ sock_sendmsg ---__ sock_sendmsg_nosec

b. at the network protocol layer

Call udp_sendmsg --- ip_route_output_flow (routing) udp_flush_pending_frames --- ip_flush_pending_frames (ip protocol entrance) --- ip_local_out --- dst_output --- ip_finish_output --- ip_finish_output2 --- arp_generic_ops-> neigh_resolve_output (established neighbor information)

c. In the device-independent interface

Call dev_queue_ximt --- dev_hard_start_xmit

d. in the driver layer

Call ndo_start_xmit

2. Receive IP packets

In the application

Recvmsg function call through the system to obtain the contents of the package

In the kernel:

After the card receives a packet interruption by netif_rx (here trigger a soft interrupt, when the processor is idle reprocessing) function package handed upper netif_rx_action (except for the driver layer, above the entrance to four total), continue to call netif_receive_skb (which is determined by the processing of the protocol packet) --- deliver_skb, then the packet submitted to the ip protocol layer processing (ip_rcv-ip protocol stack layer processing entrance), there followed threw udp protocol layer processing (udp_rcv- udp protocol stack layer processing entrance), after udp layer processed, submitted to the sock-> ops-> recvmsg, namely recvmsg system calls the corresponding function.
- Hadoop virtualization performance comparison and tuning experience (Server)
- Create a project using Android Studio LinearLayout (Programming)
- Experience PHP 7.0 on CentOS 7.x / Fedora 21 (Server)
- PostgreSQL with the C Completing the storage process instances (Database)
- Under CentOS yum install Nginx smooth switch mounted to Tengine (Server)
- Swift string common method (Programming)
- On the PC goes heavy security watch your startup items (Linux)
- C # socket udp broadcast (Programming)
- Oracle PL / SQL selective basis (IF CASE), (LOOP WHILE FOR) (Database)
- Oracle Incident Packaging Service (Database)
- Ubuntu install Eclipse can not find JAVA_HOME problem (Linux)
- Linux host dual LAN transceiver package ARP problem (Linux)
- CentOS 6.4 installation environment to build Scrapy 0.22 (Linux)
- Tab set to four spaces in Vim (Linux)
- Ubuntu 12.04 configure NVIDIA CUDA 5.5 Record (Linux)
- Ansible installation configuration and simple to use (Server)
- Oracle create user authorization and in PLSQL (Database)
- Introduction and use of the Raspberry Pi (Linux)
- Vim useful plugin: YouCompleteMe (Linux)
- Use SecureCRT to transfer files between Linux and Windows (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.