When one host sends data to another host, the data needs to be forwarded through multiple routing layers. The forwarding process of IP at the routing layer is relatively complicated. How to process the datagram sent by the destination host?
First we need to understand the format of the datagram:
IP forwarding and control are determined by the header of the IP datagram
The value of the 4-digit header length is based on 4 bytes. The minimum value is 5. means that the minimum header length is 4x5=20 bytes.
means without For the IP header of any option, the maximum value represented by the 4-bit table is 15, which means that the maximum header length is 60 bytes.
The 8-bit TOS field has 3 bits to specify the priority of the IP datagram (Currently obsolete), there are 4 bits indicating the optional
including the minimum delay, maximum throughput, maximum reliability and minimum cost of the service type, and the total bit is always 0.
The total length of 16 bits is the number of bytes of the entire datagram (including IP header and IP layer payload).
Every time an IP datagram is transmitted, the 16-bit identifier is increased by 1. can be used to fragment and reassemble the datagram.
3-bit flag and 13-bit slice offset are used for slicing.
TTL (Time to live) is used like this :The source host sets a survival time for the data packet, such as 64, and sends the packet every time it passes through a router
The value is reduced by 1. If it is reduced to 0, it means that the route has been too long and the destination host's network cannot be found, so the packet will be discarded. Therefore, the unit of
this survival time is not seconds, but hops. .
The protocol field indicates whether the upper layer protocol is TCP, UDP, ICMP or IGMP.
Then there is the checksum, which only verifies the IP header, and the higher-layer protocol is responsible for data verification.
The IP address length of IPv4 is 32 bits.
In the IP datagram, the total length of the field is 16 bits, and the maximum length of a datagram is 2^16-1. Although the longest possible datagram can improve transmission efficiency, it rarely exceeds 1500 Bytes; so as long as it exceeds 1500 bytes, the datagram is considered to be fragmented. After the IP datagram is fragmented, each fragment forms a packet with an IP header and is routed independently. After arriving at the destination host, the IP layer of the destination host will process all received fragments before transmitting them to the transport layer. The fragments are reassembled into a datagram.
1.IP fragmentation
1.IP fragmentation principle:
The process of fragmentation and reassembly is transparent to the transport layer because the IP datagram After fragmentation, it can only be reassembled when it reaches the next station, and it is completed by the IP layer of the destination. The fragmented datagram can be fragmented again as needed;
IP fragmentation Fragments and complete packets have almost the same IP header, and the ID field is consistent for American and British fragments, so that the same IP data packet fragment can be identified during assembly. In the IP header, the 16-bit identification number uniquely records the ID of an IP packet, and IP fragments with the same ID will be reassembled; while the 13-bit fragment offset records the position of an IP fragment relative to the entire packet. ; The 3-bit flag in the middle of these two tables marks whether there are new fragments behind this fragment. The receiver can reassemble the IP data using the information provided in these three fields, which are all the information that makes up the IP fragmentation.
(1) The role of the flag field
0 DF MF
The flag field has three digits in total, the highest bit is 0, this value must be copied to all groups
Do not Fragment (DF) values must be copied. DF=1 means that the receiving host cannot fragment the packet. If the packet cannot be fragmented but its length exceeds the MTU, the packet can only be discarded and an ICMP error message needs to be used to notify the source host. DF=0, indicating that fragmentation is possible
Fragmentation (MF) indicates whether the fragmentation is the last fragmentation, MF=1 indicates that the accepted fragmentation is not the last fragmentation, MF=0 indicates when the fragmentation is accepted It is the last fragment
Intentional sending of some IP fragments instead of all will cause the target host to always wait for fragment consumption and occupy system resources.
2. MTU (Maximum Transmission Unit) Principle
When two remote CPs are interconnected, their data must pass through many routers and various network media to reach the other party. End
, the MTU of different media in the network are different, just like a long section of water pipes, consisting of water pipes of different thicknesses (different MTU). The maximum amount of water passing through this section of water pipe will be determined by the thinnest water pipe in the middle. Decide.
For the upper-layer protocols of the network layer (here, take the TCP/IP protocol cluster as an example), they do not care about the thickness of the water pipe because they think it is a matter of the network. At the network layer, the IP protocol detects the size of each data packet transmitted from the upper layer protocol, and determines whether it needs to be fragmented based on the MTU size of the machine. The biggest disadvantage of sharding is that it reduces transmission performance and requires that tasks that can be completed once are divided into multiple processes. Therefore, the higher layers of the network (implementation of the transport layer) often pay attention to this, because some higher layers may require that the packets cannot be fragmented for some reasons, and the index will add a label to the header of the IP datagram. :DF When an IP data packet is transmitted over a large network segment, if the MTU is smaller than the data packet, the forwarding device will drop the data packet according to the situation. Then an error message is returned to the sender, which often causes communication problems, but fortunately most network link MTU is 1500 or greater than 1500
For UDP, the protocol itself is useless The connection protocol does not care whether the arrival order of data packets is correct, so generally UDP does not have much requirements for fragmentation
It is different for TCP. This protocol is a connection-oriented protocol. For TCP It is very concerned about the arrival order of data packets
and whether errors occur during the transmission process, so some TCP applications have requirements for fragmentation---cannot be fragmented
3.MSS (maximum (Message segment length) Principle
MSS is the maximum data segment that can be transmitted by a TCP packet each time. In order to achieve the best transmission performance, the TCP protocol usually negotiates the MSS value of both parties when establishing a connection. This The value is replaced by MTU when the TCP protocol is implemented (minus the 20bites header size of the IP packet and the 20bites header size of the TCP data segment), so the MSS size is often 1460. Both parties will determine the MSS of this connection based on the minimum MSS value provided. Maximum value
2. IP fragmentation steps
The fragmentation information field of an unfragmented datagram is all 0, that is, multiple fragmentation flags are 0, and the offset is 0, fragmenting a datagram requires the following steps
(1) Check the DF flag bit to see if fragmentation is allowed. If this bit is set, the datagram will be discarded and an ICMP error will be returned. To the source
(2) Based on the MTU value, divide the data field into two or pairs of parts. Except for the last data part, the length of all newly created data options must be a multiple of 8 bytes
(3) Each data is put into an IP datagram, and the headers of these datagrams are slightly changed from the original headers
(4) Except for the last datagram fragment, all Each fragment has multiple fragmentation flags set
(5) The fragment offset field in each fragment is set to the position occupied by this data part in the original datagram. This position is relative to The beginning of the original unfragmented datagram.
(6) If options are included in the original datagram, the high-order byte of the option type byte determines whether this information is copied to all fragmented datagrams or only to the first datagram.
(7) Set the header field and total length field of the new datagram.
(8) Recalculate the checksum field of the message header.
At this time, these fragmented datagrams are forwarded like a complete IP datagram. IP processes each datagram fragment independently. Datagram fragments can reach their destination through different routes. If they After passing those routes that specify a smaller MTU, they can be further fragmented
On the destination host, the data is reassembled into the original datagram, and the identification field set by the sending host is the same as that in the datagram. The Yuan IP address is used together with the destination IP address. The fragmentation process does not change this field
3. Reassembly
In order to reconstruct the fragments of these datagrams, the receiving host will A storage buffer is allocated when the slice arrives. This host will also start a timer. When subsequent fragments of the datagram arrive, the data is copied into the buffer memory at the location specified by the fragment offset, and when all fragments have arrived, the complete, unfragmented original datagram is restored.
If the timer expires and the shard remains in an unapproved state, the data will not be retained. The initial value of this timer is the lifetime value of the IP datagram. It is implementation dependent and some implementations allow it to be configured.
Reassembly step
On the receiving side, an original data IP packet sent by the sender reassembles all the fragments before they can be submitted to the upper layer protocol. Each one will be The reassembled IP datagrams are represented by an ipq structure
In order to effectively improve the assembly of fragments, the structure used to save fragments must achieve the following points
(1) Quick positioning A group of packets of a certain datagram
(2) Quickly insert new fragments into a group of fragments belonging to a certain datagram
(3) Effectively judge a datagram Whether all fragments have been received
(4) It has a reassembly timeout mechanism. If the timeout overflows before the reassembly is completed, all contents of the datagram will be deleted
The above is the detailed content of What is the principle of fragmentation and assembly of IP datagrams?. For more information, please follow other related articles on the PHP Chinese website!