Kernel version: 2.6.34
Message IP checksum, ICMP checksum, TCP/UDP checksum using the same algorithm, defined in the RFC1071, online data and examples are many, do not explain the algorithm flow, but focus on the implementation of changes and techniques.
The checksum algorithm is simply to add up all 16-bit words in one's complement and then to take the one ' s complement of the sum.
The calculation of the checksum can be divided into two steps: accumulation and reverse. This division is important, and it greatly reduces the cost of checksum computation. The first thing to check and calculate is to be clear: checksums are time-consuming! The reason is not the complexity of the algorithm, but the large input data, imagine the transfer of 500M files, the kernel to verify 500M bytes of data, and for each message, is to be checksum. So the verification and implementation of the protocol stack is not straightforward, and many methods are used to circumvent this overhead.
First: Delay the checksum calculation
In accordance with the provisions of the Agreement, the message to each layer, first verify that the checksum is correct, discard the incorrect message, before the subsequent operation. For the protocol under the Transport layer, the kernel does this because the IP only needs to verify the IP header, up to 60 bytes, and for the Protocol on the network layer, the kernel does not do so, icmp/tcp/udp all need to check the contents of the message, and this part of the consumption is very large.
Taking UDP as an example, when the message passes to the UDP processing, it does not verify the correctness of the checksum, but inserts the message SKB into the receiving queue sk_receive_queue of the corresponding socket directly. The kernel does not compute checksums until the actual program receives the message and takes it out of the receiving queue. Consider this approach, due to the postponement of checksum calculation, so many of the wrong messages are received, they will occupy the processing of the message process, until the message is ready to enter user space, this time to calculate the checksum, found errors and discarded. In this way it seems that there is no need to increase the cost, must check and the calculation is to be carried out. But in doing so, the binding of the two operations is critical when officers transferred Guevara and computations are deferred to a copy message to user space. Originally, checksum calculation to traverse a message, and the copy to traverse a message, so that is two traversal operations, after the merge with a traversal, it saves much more than the extra pay.
Second: Separating the checksum calculation steps
The beginning of the calculation of the checksum is divided into two steps: Add, reverse, the two steps apart, you will find that the checksum can be part of the calculation, and finally use the value of each part of the sum of the inverse. This feature, on the other hand, provides support for simultaneous copying and checksum computations. Moreover, because the message may be fragmented, the message content is not stored together in the linear address space, but the fragment is hung on the first fragment SKB frag_list, which is stored in the Non-linear address space. Therefore, the copy will be a fragment of a fragment, due to the partition of calibration and calculation, it can also be a fragment of a piecewise calculation. Csum_partial () and Csum_fold () are born for this, the former calculates the cumulative, the latter calculates the inverse.
So the general checksum can be computed, skb_checksum () calculates the sum of the SKB and adds to the cumulative and skb->csum previously calculated, and then Csum_fold () to the final result, the resulting checksum. S
Um = Csum_fold (skb_checksum (SKB, 0, Len, skb->csum));
Third: Improve calibration and calculation
RFC1071 the test and calculation is 16bit per unit, but the actual accumulation of this step can be adjusted, the kernel calculation is every 32bit calculation, the larger the unit, the less the cycle, the efficiency will naturally be high. The following is to show that the 32bit cumulative and 16bit cumulative results are consistent.
Suppose you want to compute 8-byte checksums, which are divided into 4 parts per 16bit: 1,2,3,4. The left side is a 16bit cumulative process, the right side is a 32bit cumulative process:
Where there is confusion is the cumulative carry problem, the left 16bit additive is added to sum, and the right 32bit additive is added to sum, as the 2,4 addition produces the same result as the 16bit additive. The following is the 32bit cumulative code snippet, W>result to determine whether to produce a carry, assuming that x+y=z produced a carry overflow, then x< Z and Y<z, otherwise z>x and z>y.
unsigned int
carry = 0;
do {
unsigned int w = * (unsigned int *) buff;
count--;
Buff + 4;
result = carry;
result = W;
Carry = (W > result);
} while (count);
result = carry;
result = [Result & 0xFFFF] + (Result >> 16);
Part Four: Calibration and calculation techniques
The best way to save checksums is to not compute checksums, which can be useful in some cases, such as large-volume contracts or local-area networks, where efficiency is more important than correctness. Skb->ip_summed parameter is for this purpose, checksum_unnecessary skips checksum calculations. Or the user sets the checksum field checksum=0 when the contract is awarded, and the checksum calculation is skipped.
skb->ip_summed = checksum_unnecessary;
In addition, in order to speed up checksum calculation, many network cards provide hardware calculation checksum, in particular, Linux uses skb->ip_summed and skb->csum to use hardware computing power to help verify TCP/UDP messages. Checksum_complete indicates that the hardware is calculated and the results are stored in skb->csum.
skb->ip_summed = = Checksum_complete;
In many chip implementations, the checksum calculation code is implemented by the Assembly, which is also due to the efficiency of the calibration and calculation.
Finally, a simple analysis of the two core functions of the checksum calculation.
Do_csum () checksum additive
The body portion of the checksum calculation is calculated in 32bit and assumes that the Buff start address is aligned and the length is aligned. As a result, incoming buff are processed to satisfy the assumptions.