Starting from "how many connections can be established on a machine", 65535?

Some common sense

  • Computers use binary coding, and enter every two.
  • One byte is 8 bits (8bit).
  • The port number specified in the tcp protocol is represented by 2 bytes.
  • The maximum number that can be represented by two bytes is 2^16=65536.

Common misunderstandings

Based on the above common sense, many students have this idea:

One connection occupies one port number, so how many connections a machine can establish depends on how many ports the machine can open.
In the TCP protocol, 2 bytes are used to represent the port. Considering that port 0 is unavailable, a machine can only open 2^16-1=65535 ports at most.
Therefore, a machine can only establish 65535 connections at the same time.

对吗?以上描述有哪些问题。


problem analysis

How to define a connection first?

A connection corresponding to the code implementation is ultimately a data structure.
The lower-level structure of a socket in the Linux kernel is shown below

struct inet_sock {
    
    
	/* sk and pinet6 has to be the first two members of inet_sock */
	struct sock		sk;
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	struct ipv6_pinfo	*pinet6;
#endif
	/* Socket demultiplex comparisons on incoming packets. */
	__be32			daddr;         // IPv4的目的地址
	__be32			rcv_saddr;     // IPv4的本地地址
	__be16			dport;         // 目的端口
	__u16			num;           // 本地端口
	__be32			saddr;        

	……
}; 

The more important thing is 本地IP、本地端口、远端IP、远端端口.

These four fields form a four-tuple, and the four-tuple identifies a connection. The
bottom layer knows which socket handle to hand the packet to for processing based on the four items in the packet,
so all the four-tuples The combination method is the number of connections that a machine can establish.

That is, the theoretical value is
2^32 x 2^16 x 2^32 x 2^16 = 2^(32+16+32+16) = 2^96 = 79228162514264337593543950336L

important point:

  • The local IP depends on how many network cards the machine has. Generally, one network card corresponds to a local address, and most machines have only one or two network cards.
  • In fact, it should also include a protocol field (tcp or udp) to form a five-tuple. Here we mainly discuss tcp, and udp does not have the concept of connection.
  • The bottom layer locates the specific socket processing handle based on the four-tuple, not just the IP port, which is very important for subsequent understanding.

Client perspective

The number of connections that the client can establish is not any combination of the above four-tuples. The main limiting factor here is that the same port of the client cannot be reused in the four-tuple combination.

For example, the client wants to establish a connection with the address addr1 (1.1.1.1:80), assuming that the local port allocated by the operating system for the connection is 2333.
At this time, if the client establishes a connection with the address addr2 (2.2.2.2:80), the operating system can no longer allocate port 2333 to the new connection. It can only find an unoccupied port from 1-65535. If If everything is occupied, the operating system will report an error and will not allow the connection to continue.

So why does the operating system report an error, because if two connections use the same port, when the bottom layer receives a data packet, it cannot distinguish whether the packet should be for connection 1 or connection 2.

and so作为客户端能建立的连接数理论值只有65535

Insert picture description here

Server perspective

The biggest question on the server side here is that every time a connection is established, does it consume a port on the server side, so is there a limit of 65535 local ports for the number of server connections?

In fact, it is not. The server does not need to allocate a port for each client connection.

This is a note mentioned above. When the server receives a data packet, yes 根据四元组来查找具体的处理句柄的, so the local port in the four-tuple does not need to be changed, and there is no need to allocate a new local port, so the theoretical upper limit is four yuan A combination of the other three items in the group.

and so作为服务端能建立的连接数理论值远远超过65536

other

So far, let’s talk about the problem of the problem itself, "How many connections can a machine establish".
There are several prerequisites that are not explained, and these will lead to changes in the combination of quadruples.

  • Does the machine refer to the server or the client
  • The connection is connected with several addresses (ip:port)
  • Is the connection referring to ipv4 or ipv6
  • This machine has several network cards (meaning there are several local addresses)

In addition, do you want to consider the hardware configuration of the machine, such as how large the memory is, after all, the structure corresponding to each connection needs to apply for memory resources (a connection occupies about 3KB of memory).

Moreover, a connection also occupies a file descriptor of the operating system, and the operating system also has its own limit on file descriptors. This can generally be checked through ulimit -a, and the details are not discussed here.

If you have any questions or errors in the above, please leave a message.

Reference

Wikipedia-Internet Protocol Suite
Wikipedia-Network Socket

Guess you like

Origin blog.csdn.net/weixin_52777294/article/details/113073781