Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Linux kernel network subsystem analysis     - CentOS 6.4 install and configure Squid Proxy Server (Linux)

- Linux Getting Started tutorial: Experience QEMU virtual machine articles (Linux)

- MySQL 5.5 on master-slave copy filter (Database)

- Java regular expressions examples (Programming)

- Ubuntu 10.10 install Oracle 10g Installation Guide (Database)

- The PostgreSQL database pg_dump command line does not enter a password method (Database)

- MySQL use benchmarking tool sysbench (Database)

- Ubuntu 14.04 compile and install Apache (Server)

- Mac OS X command line to submit the local project to Git (Server)

- TNS-03505 name could not be resolved (Database)

- After VMware CentOS full VM clone the network card is unavailable Solutions (Linux)

- Eclipse, Tomcat configuration JNDI connection Oracle data source example (Server)

- High-performance Linux system firewall detailed analysis of double-effect (Linux)

- SQLite3 simple operation (Database)

- Linux environment Duplex (multi-machine) automatic mutual backup scheme (Linux)

- Neo4j map data processing tab (Database)

- Android media library of analysis: MediaProvider (Programming)

- Computer security perimeter recommendations (Linux)

- Upgrade installation manual CentOS6.5 GCC4.8.2 (Linux)

- Ubuntu install VMware Workstation 11 tutorials at 14.04 / 14.10 (Linux)

  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.
- Linux Bash share tips for getting started (Linux)
- RedHat / CentOS ext4 partition can not be formatted large supplementary ext4 formatting (Linux)
- Analysis of potential problems through custom Orabbix monitoring Oracle (Database)
- JavaScript closures and the scope chain (Programming)
- Ubuntu 12.04 LTS installation configuration JDK1.6.0_45 (Linux)
- JavaScript subarray Deduplication (Programming)
- Share and show your code on GitHub (Linux)
- Linux kernel update error, update-initramfs: failed Solution (Linux)
- Linux novice common commands (Linux)
- Linux network security probing tools Hping2 (Linux)
- Workspace Go language and environment variables GOPATH (Linux)
- CentOS 7 x64 compiler installation Tengine 2.0.3 Comments (Server)
- Eight sorting algorithm implemented in Python (Programming)
- Memcached and Redis (Linux)
- Linux Network Programming - raw socket instance: MAC header message analysis (Programming)
- 10 tips daily Docker (Server)
- Large computer network security policy Experience (Linux)
- VMware 8 installation configuration Win7, CentOS-7 Wizard (Linux)
- Linux package manager - yum (Linux)
- iOS GCD multithreading simple to use (Programming)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.