Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Linux kernel network subsystem analysis     - 11 examples in Linux df command (Linux)

- Sudo and Root Account in Ubuntu related (Linux)

- To control based on IP address routing policy under Linux (Linux)

- How to install Ubuntu California - the calendar application (Linux)

- Puppet 3.5 Source package Installation and Configuration (Server)

- Upgrading to MySQL 5.7 partitioning problem solving (Database)

- The Java Properties class action (Programming)

- When a software RAID data recovery and reconstruction of how failure (Linux)

- Merge Sort (Programming)

- Infinispan 8 new Redis cache storage implementation (Linux)

- JavaScript function part (Programming)

- Iptables in Ubuntu (Linux)

- The principle Httpclient4.4 (HttpClient Interface) (Programming)

- Json Applications of FastJson (Programming)

- How VirtualBox and VMware virtual machine conversion (Linux)

- Detailed PHP code optimization [section] (explain reasons) (Programming)

- C ++ handling text input (Programming)

- Linux System Getting Started Tutorial: Installing Git on Linux (Linux)

- Ubuntu use three methods to install Ruby (Linux)

- CentOS boot image to achieve semi-automatic installation (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 168.1.1.10 (the destination address) of the gateway address is 192.168.1.1, 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.
     
         
         
         
  More:      
 
- Linux operation and maintenance of the actual file system, file links (Linux)
- B-tree - ideas and implementation of C language code (Programming)
- Java NIO1: I / O model overview (Programming)
- How to identify memory leaks in Java (Programming)
- Installed in the desktop version of Ubuntu Unity Tweak Tool (Linux)
- Shared directory settings between Linux and Linux (Linux)
- On Android running ClojureScript (Linux)
- Ubuntu How to install and upgrade Linux Kernel 3.15 (Linux)
- Installed FFmpeg 2.6.3 on Ubuntu / Debian / Fedora system (Linux)
- Unix / Linux commonly used to clean up disk space command (Linux)
- Java implementation chain store binary tree (Programming)
- Nodejs command-line program development tutorial (Programming)
- Installation Eduspec university management systems 17 Linux Mint (Server)
- grep search from files and display the file name (Linux)
- Linux uses a screen recording under RecordMyDesktop (Linux)
- Install Redis 2.6 5.5 32 position CentOS error resolved (Linux)
- sed command (Linux)
- Linux Shell debugging (Programming)
- NAT (network address translation) Realization (Linux)
- Use Hexo quickly build and deploy a blog to Github (Server)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.