Simple context analysis of TCP/IP protocol stack under Linux

Source: Internet
Author: User

Recently in the writing of some things in the network programming, and then encountered on the transmission of small problems. As a result of a simple look at some of the TCP/IP details of some things, so simply looking for this "tracking LINUXTCP/IP code Run" book read a morning, the results found that the first contact these core aspects of things, harvest very little. So on the Internet to find related to the Big God blog, take to read, although still not too understand, absorption is not good enough, but I want to blog in the form of record it, but also hope to learn these things for me to open a good head

The main structure of the 1.linux Network protocol stack (1) Socket Layer

This layer mainly deals with socket related things, such as the initialization of its various structures. Each socket exists in the kernel as a socket structure, and its structure is defined as follows

Socket structure Body

struct  socket{socket_stat State; //socket status  unsigned  long  flags; //socket the flag bit  const  struct  proto_ops *pos //socket function action table  struct  fasync_struct *fasync_list //socket asynchronous wake-up queue  Span class= "Hljs-keyword" >struct file *file; //the file pointer associated with the socket  struct  sock *sk; //represents the sock structure pointer for the content of the specific agreement  wait_queue_head_t wait //used to wait for the queue /span> short  type; //socket type } 
其中sock结构体封装了与具体协议相关的东西,它的结构体定义稍微有点大,所以这里我就不一一列出,只是简单的给大家介绍一下它定义的主要几个模块

(1) Defines the structure body Sock_common which is common with inet_timewait_sock, which mainly contains the address family, the connection state, the Protocol function table and so on.
(2) defines various data and queues related to receiving send queues
(3) A variety of incoming and outgoing data for recording and providing control

(2) INET Socket Layer

This layer is an interface that can be used for a variety of network protocols when used with TCP/IP

(3) TCP/UDP Layer

The operation of the transport layer is mainly handled, and the transport layer is represented by struct inet_protocol and struct proto two structures.

(4) IP layer

To handle the operation of the network layer, the network layer is represented by the STUCT packet_type structure.

(5) Data link layer and driver

Each network device is represented as a struct net_device struct

Simple analysis of some code in the 2.TCP/IP protocol stack (1) Main data structure
struct msghdrstruct sk_bufstruct socketstruct sockstruct proto_opsstruct proto

It goes without saying that the socket layer corresponds to the socket, so how do we create a socket? have been exposed to network programming probably will know the sore boils a socket only calls the function socket (), in fact, the function of the internal implementation is very complex, it will be based on the parameters of the above function to create a socket instance (should be able to understand the initialization of the socket structure), and create a socket descriptor, and then use pointers to connect the two. When the socket is initialized, different function table Pos are initialized according to different protocols (see structure information above). If TCP is Inet_stream_ops, UDP is inet_dgram_ops, and the socket also initializes 3 queues by invoking the related operation in sock to initialize sock from the socket to the sock layer. Recvive_queue (Received packet sk_buf list queue), Send_queue (the linked list queue that needs to send packet sk_buf), Back_queue (TCP three handshake successful packet queue). There are also faimly initialized to the inet type, type is stream, and Sock_proto is initialized to Tcp_prot.
At one end of the write or send process, the data in the write buffer is first packaged into a MSGHDR data structure, and then called Sock_sendmsg to send MSGHDR data to the inet layer (should be shipped layer and the middle of the transport layer, to provide the interface of various protocols), Then the data in the MSGHDR structure is encapsulated by the SK_BUF structure, and the packet is a complete body and can be hung in the send queue.

(2) Linux routing system

The main storage of three kinds of data related to the route, the first is the host address information table connected to the physical and local, the second is to save the network access to determine what a network address should be routed data table, the third is the latest used query routing address cache address data table

FIB结构中保存了最重要的路由规则,通过其可以找到路由地址方法。系统中路由的手段一般为现在路由缓存中查找,如果能找到直接将对应的项作为路由规则,如果找不到,解根据FIB中的规则换算出来
Data Link Layer

The Net_device hardware table mentioned earlier corresponds to each network interface device. This structure contains a lot of functions and variables that can get the network card information directly, as well as the function of the network card operation, which directly points to the entrance of the network card driver, including sending and receiving frames to the buffer area. After the work is done, the frames in the buffer are composed by Netif_rx Sk_ BUF to the system's receive queue in the form of a

2. Start-Up process

Initialization process main: Start->kernel->do_basic_setup->sock_init-->do_initcalls
the basic flow of do_initcalls initialization can be summarized as follows
(1) Initialization of the Protocol
(2) Initialization of the route
(3) Initialization of the network interface device

3. Network connection

The TCP connection is via the following function
SOCKFD = socket (af_inet,sock_stream,0);
Connect (sockfd,&adress,sizeof (address))
The following are the main things that occur during the invocation of this function
(1) Sys_socket system call, and it calls Socket_creat () to create a socket function to meet the parameters of the sockets structure, and sock_creat () will be different from the protocol to call different functions, for inet, it will call inet_ Create function, and then it calls the other series of functions
(2) After the initialization of the system is done, connect will be connected, connect a socket descriptor associated with sockect with a remote machine, and invoke each protocol's own corresponding connect function. TCP is Inet_stream_connect
(3) Inet_stream_connect will get an SK structure that describes some of the properties and states of the socket, and then calls the Tcp_v4_connect function for TCP.
(4) Tcp_v4_connect () also calls other functions to find the appropriate route

4. Closing of the connection

(1) The Sock_close function is called by close when the connection is closed
(2) Sock_close () call Sock_release () to free up some space
(3) Inet_release call the socket corresponding to the closing function of the protocol, and finally release the SK

5. Flowchart for sending data

Flowchart for receiving data


If you have read my study notes, you can poke them into the original text.
http://blog.csdn.net/cz_hyf/article/details/602802

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Simple context analysis of TCP/IP protocol stack under Linux

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.