The realization principle of Posix API and network protocol stack

What are the frequently asked questions about the protocol stack in the interview?

  1. TCP three-way handshake process?
  2. TCP waved four times?
  3. Why does it take three handshake to establish a connection, and four waves to disconnect it?
  4. How long does the time_wait state last and why?
  5. A lot of time_wait and close_wait?
  6. Timeout retransmission and fast retransmission?
  7. What fields does the TCP header length have?
  8. What is the meaning of the parameter backlog of TCP in listen?
  9. At which step did Accept happen in the three-way handshake?
  10. What are the insecurity in the three-way handshake process?
  11. What is the difference between TCP and UDP?
    Detailed explanation of TCP three-way handshake and four waved hands

Pool structure

  • Thread Pool
  • Memory pool
  • connection pool
  • Request pool

Server Posix API

  • fd = socket();
  • bind(); fd —>ip, port
  • listen(fd, backlog);
  • clientfd = accept(fd, addr, addrlen);
  • recv();
  • send();
  • close();

Client Posix API

  • connect();

epoll Posix API

  • epoll_create();
  • epoll_ctl();
  • epoll_wait();

other

  • shutdown(); (not commonly used)
  • setsockopt(); (UDP)
  • getsockopt(); (UDP)
  • fctnl(); (Control whether io is blocked or not)

The three stages of TCP

  • establish connection
  • Transmission process
  • Disconnect

Where is UDP mainly used?

  • Mass data transmission and downloading, such as Xunlei VIP download, removes congestion control and makes it free from flow control
  • Real-time scenes, games
  • DNS protocol, for example, when the browser requests the domain name IP, the UDP protocol is adopted, and the data synchronization between the node of the domain name server and the node adopts the TCP protocol

What are the disadvantages of UDP?
Unstable
UDP connect does not transmit specific data, it just tries to see if the link is smooth

The TCP data packet header forms the
tcp packet header
TCP three-way handshake
TCP three-way handshake

  • The client connect() makes a connection request
  • The server implements the first handshake after listen(), and listen() sets listenfd to the state of listen, which can perform a three-way handshake
  • After the third handshake, the server runs the function accept()
  • How to find the node corresponding to the data packet in the syn queue? Answer: Confirm through five tuples (sip, dip, sport, dport, proto)
  • The life cycle of a node? The tcp state machine (11 states) is stored in the node, and the life cycle is until the server calls close()
  • The node is also called tcp control block, tcp block, tcb for short

UDP concurrency (underlying processing)

  1. The server recvfrom(&addr) gets the ip and port of a client
  2. The server creates a new fd, sendto(fd,) sends the ip and port to the client
  3. If the first frame is mixed, the data will be returned directly, and the data will be retransmitted again after timeout
  4. KCP just completed a layer of application protocol based on this framework

TCP data transmission

  1. How does tcp guarantee the order? Answer: Timeout retransmission mechanism. The protocol stack starts a 200ms timer when a packet is received, and then resets the 200ms timer when it receives a packet, and so on. Once the timeout occurs in the middle, it will check which packets are not available from the 1st packet. Arrived, assuming that the 3rd packet did not arrive, re-send sequentially from the 3rd packet
  2. Disadvantages of tcp: ack confirmation time period is long, and the number of retransmissions of received packets is large
  3. First exponential growth (slow start), and then linear growth after a certain amount of packages are issued, then halve the number of existing packages before continuing to increase (congestion control)Slow start and congestion control
  4. The success of the send() of the client application does not mean that the data packet is successfully sent. This can mean that the data of the client application is successfully copied to the corresponding protocol stack, and copy_from_user is called to copy the data from the user space to the sendbuffer of the protocol stack. , Write() is the same

TCP waved four times
TCP waved four times

  • What should I do when there is a lot of time_wait in the protocol stack? Answer: The active party (the end that actively calls close first) will generate time_wait 1. Check your own logic at this time; 2. Set it to reuse through setsockopt(), so that tcb will not be released but reused, which is reduced to a certain extent time_wait
  • The reason for the large number of close_wait in the protocol stack? Answer: When the server does not close in time after 0 = recv(), there is a delay (maybe there is business code to process after recv = 0), you can throw the business code to another thread for processing, 0 = recv, immediately close
  • Since the server did not close in time after 0 = recv(), the client was in the state of fin_wait_2. Is there any way to end this state? Answer: No, if it is forcibly prohibited, only kill and the operating system can be used to recover
  • The reason for the large number of fin_wait_1 in the protocol stack? Answer: The client did not receive the ack packet, resulting in a long-term state of fin_wait_1
  • What should I do if there is a lot of fin_wait_1 in the protocol stack? Answer: No solution
  • It is generally not recommended to call shutdown(). If it is a one-way function, consider calling

11 states of TCP (associated with fd)11 states of TCP

Guess you like

Origin blog.csdn.net/CSJIN123/article/details/114962533