Fast calculation method of checksum

Source: Internet
Author: User

First put the code on

uint16_t Cal_checksum (uint32_t *pstart,uint16_t len) {uint64_t checksum; uint32_t *Pend     uint32_t v0,v1,v2,v3,v4;    checksum = 0; Pend = (uint32_t*) ((char*) Pstart + (Len & (~0xF)));while (Pstart <Pend) {v0 = *Pstart; V1 = * (pstart+1); V2 = * (pstart+2); V3 = * (pstart+3); Checksum + =V0; Checksum + =v1; Checksum + =v2; Checksum + =V3; Pstart + =4; } len = len &0xF; Pend = (uint32_t*) ((char*) Pstart + (Len & (0xF)));while (Pstart <Pend) {V0 = (uint32_t) (* (uint16_t*) pstart); V1 = (uint32_t) (* (uint16_t*) pstart+1)); Checksum + =V0; Checksum + =v1; Pstart + =1; }Switch (Len &0x3) {Case3: V0 = (uint32_t) (* (uint16_t*) pstart); V1 = ((uint32_t) (* ((uint8_t*) pstart+2)) <<8); Checksum + =V0; Checksum + =v1;Break;Case2: V0 = (uint32_t) (* (uint16_t*) pstart); Checksum + =V0;Break;Case1: V0 = ((uint32_t) (* ((uint8_t*) pstart+2)) <<8); Checksum + =V0;defaultbreak; } checksum = (Checksum>>32) + (Checksum& 0xffffffff); Checksum = (Checksum>>32) + (Checksum& 0xffffffff); Checksum = (Checksum>>16) + (Checksum&0xffff ); Checksum = (Checksum>>16) + (Checksum&0xffff ); return checksum ^ 0xffff             

The RFC-defined checksum is calculated as a number per two bytes, and the carry-over is added to the low.

The code optimization here has two points:

1 cross-use variables to save load latency leading to CPU waits

21 addition complete two adds 16 bytes of data, the low 16-bit carry is first added to the high 16 bits, and the final high 16 bits are added back to low 16 bits, in order to prevent high 16-bit overflow, the uint64_t type is used to record overflow

The second optimization draws on the code from Cavium, but unfortunately its code is severely low due to the use of non-unaligned and the use of the first optimization, and the judgment of too many conditions.

Optimized code performance is basically achieved, Cycle_num = LEN/2, that is, a 1500 of the packet can only need less than 800 cycles to complete the checksum calculation (CPU 1GHZ);

Of course, this code also has a limitation, that is, the Pstart pointer requires at least 4 byte alignment, which is why the type is written as uint32_t*. If not aligned, the light has a serious impact on efficiency, heavy CPU error.

Generally, the first address of the buff is at least 4 bytes aligned, when calculating checksum, the data should have been assembled in the buff, assuming that the start data in the buff is the MAC header, followed by the IP header, followed by the TCP header, then only the

Checksum=0

Replaced by

checksum=* (uint16_t*) Pstart;

Pstart = (uint32_t*) ((uint16_t*) pstart+1);

Len-=2;

The payload header address of the IP can be directly passed directly into the corrected function because the MAC headers defined by either Ethernet II or 802.3 satisfy the 4n+2 byte, and even the VLAN field data length is a multiple of 4.

Fast calculation method of checksum

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.