Simple thread analysis of TCP/IP protocol stack under Linux __linux

Source: Internet
Author: User

Some of the things that have recently been written about network programming have encountered minor problems with transport. Because there is a simple look at some of the TCP/IP to explain some of the things, so simply find this "tracking LINUXTCP/IP code Run" book Read all morning, the results found that the first contact with these core aspects of things, harvest very little. And then on the Internet to find related categories of the Great God blog, to read, although still see is not too clear, absorption is not good enough, but I want to blog as a record of it, but also hope for me to learn these things later to open a good head bar 1.linux Network protocol stack main structure (1) Socket Layer

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

Socket structure Body

struct socket
{
    socket_stat state;          The state of the socket
    unsigned long flags;        The flag bit of the socket
    const struct PROTO_OPS *pos//socket function action table struct fasync_struct *fasync_list  // The asynchronous wake queue for the socket
    struct file *file;         The file pointer associated with the socket
    struct sock *sk;          Sock structure pointer representing specific protocol content
    wait_queue_head_t    wait//For queue short
    type;               Type of Socket
}
The sock structure encapsulates something that is related to a specific protocol, and its structure definition is a little bit large, so I'm not going to list it here, just to introduce you to a few of the main modules it defines.

(1) Defines the sock_common of the structure which is shared with Inet_timewait_sock, which includes the address family, the connection state, the Protocol function table and so on.
(2) defines the various data and queues associated with receiving the send queue
(3) for recording and providing control of the various received sent data (2) INET socket Layer

This layer is an interface that can be used for various network protocols when used in TCP/IP (3) TCP/UDP layer

The main processing transport layer operation, transport layer with struct inet_protocol and struct proto two structure representation (4) IP layer

Handling network layer operation, network layer using STUCT packet_type structure representation (5) Data link layer and driver

Each network device is a simple analysis of some code of the 2.TCP/IP protocol stack with struct net_device structure (1) main data structure

struct MSGHDR
struct sk_buf
struct socket
struct sock
struct proto_ops struct proto

It is self-evident that the socket layer corresponds to the operation of the socket, then how do we create a socket. Contact with the network programming probably will know boils a socket just call 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 be interpreted as the initialization of the socket structure), and create a socket descriptor, and then use the pointer to make the two connections. When the socket is initialized, different function action table POS is 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 associated operation in sock to initialize sock from the socket to the sock layer. Recvive_queue (packets received sk_buf linked list queues), Send_queue (a linked list queue to send packet sk_buf), Back_queue (TCP three handshake successful packet queue). There is also the initialization of the faimly to the inet type, type to stream, Sock_proto initialized to Tcp_prot.
When a write or send process is performed at one end, the data in the write buffer is first packaged into a MSGHDR data structure, and then the sock_sendmsg is invoked to send the MSGHDR data to the inet layer (the intermediate between the transport layer and the transmission layer, providing interfaces for various protocols), Then the data in the structure of the MSGHDR is encapsulated by the SK_BUF structure, at which point the packet is a complete body and can be hung on the routing system of (2) Linux in the sending queue.

There are three types of data related to routing, the first is a physical and local connection to the host address information table, the second is to save the network access to determine what a network address should be routed the data table, the third is the most recent use of the query routing address of the cached address data table

The most important routing rule is saved in the FIB structure by which the routing address method can be found. System in the middle of the method generally for the current routing cache lookup, if you can find the corresponding items directly as a routing rule, if not found, the solution according to the rules of FIB conversion
Data Link Layer

The Net_device hardware table mentioned above corresponds to each network interface device. This structure contains a lot of functions and variables that can directly obtain the information of the network card, as well as the function of the network card operation, this kind of function directly points to the entrance of the NIC driver, including sending and receiving frames to the buffer. After the work is done, the frames in the cache are composed of the NETIF_RX BUF to the system's receiving queue in the form of 2. Start up process

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

The TCP connection passes 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, which calls Socket_creat () to create a socket structure that satisfies the parameters of the socket function, and sock_creat () will invoke different functions according to the different protocol, for INET, it will call inet_ The CREATE function, and then it calls another series of functions
(2) When the system initialization work is done, connect will work, connect a socket descriptor associated with sockect and a remote machine, and call each protocol corresponding to the CONNECT function. TCP for Inet_stream_connect
(3) Inet_stream_connect will get a SK structure, this SK structure describes some of the socket properties and state, for TCP and then call the Tcp_v4_connect function
(4) Tcp_v4_connect () calls other functions to find the appropriate route 4. Close of connection

(1) Call the Sock_close function by close when closing a connection
(2) Sock_close () calls Sock_release () to free some space
(3) Inet_release calls the socket corresponding to the closed function of the protocol, the final release of SK 5. Flow chart for sending data

flowchart for receiving data


If you have finished reading my study notes, you may be able to poke them into the original connection.
http://blog.csdn.net/cz_hyf/article/details/602802

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.