Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Linux kernel network subsystem analysis     - rsync server set up (Server)

- Cacti installation deployment under CentOS 6.6 (Server)

- Linux / proc directory Comments (Linux)

- MySQL various log summary (Database)

- CentOS7 installed VMware 10 (Linux)

- Ora-14450: attempt to access ah transactional temptable already in use (Database)

- Linux install deploy Ansible (Linux)

- How to properly set up a Linux swap partition (Linux)

- OpenGL shadow map (Programming)

- Safety testing Unix and Linux server entry succinctly (Linux)

- Upgrade to Node V4 seven reasons (Programming)

- Examples 14 grep command (Linux)

- The official release method to upgrade to Ubuntu 15.04 (Linux)

- Oracle database, some basic grammatical structures (Database)

- Linux System Getting Started Learning: install software packages on Ubuntu and Fedora (Linux)

- Python configuration tortuous road of third-party libraries Numpy and matplotlib (Programming)

- Linux common network tools: hping Advanced Host Scan (Linux)

- ORA-01000 Solution (Database)

- jobs command example (Linux)

- State and Linux nf_conntrack TCP disconnect time (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.
- How to use systemd timer (Linux)
- Linux argument references and command substitution (Linux)
- Installation Experience open source car Automotive Grade Linux system (Linux)
- VPS xen openvz kvm (Server)
- Why you can have JavaScript string method (Programming)
- CentOS7 virtual machine starts appear Permission denied (Linux)
- How to download apk file from the Google Play store on Linux (Linux)
- Zabbix configuration external network mail alarm (Server)
- Use Swift remove the spaces in the string (Programming)
- PHP interview questions of design patterns (Programming)
- Debian 8.1 (amd64) deployed Memcached (Server)
- CRF ++ Linux use (Linux)
- Ubuntu 14.04.02 LTS startup items erroneous writing / dev / sda1 (win 7 loader) Repair (Linux)
- DELL D630 Wireless LAN Driver Installation CentOS6 (Linux)
- The security administrator network analysis tools SATAN Introduction under Linux (Linux)
- How to allow users to access only a specific database (MSSQL) (Database)
- NET Developers need to know some Linux commands (Linux)
- Flask deploy applications using Nginx on Ubuntu (Server)
- Digital jQuery scrolling effect (Programming)
- IO reference Docker container (Server)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.