Learning about calibration algorithms (when learning Ethernet)

Source: Internet
Author: User
Tags crc32 rfc

About the verification algorithm learning (learning Ethernet encountered) Ethernet header or data will have a check, the implementation of the IP core on the FPGA, with CRC Check, and then learn the ICMP protocol, also see a check algorithm, in order to learn the rationale behind the analysis.

First, look at the ICMP packet format, mainly the ping command is used (there will be a specially ping C language implementation), so just to show the ICMP rules behind the ping command.

(from wiki)
The study of calibration does not need to understand the meaning of other data at the moment, and Wiki's interpretation of checksum is
Checksum
Error checking data, calculated from the ICMP header and data, with value 0 substituted for this field. The Internet Checksum is used, specified in RFC 1071.
The first check is Header+data, and then if you omit 0, the rule looks at RFC 1071 to see this document.
Second, before looking at this document to understand a few concepts: 2 ' s complement and 1 ' s complement
In fact, this is the representation of two kinds of numbers,
First look at 2 ' s complement: this is what we use daily, called the 2 complement system.
Look at the explanation of the great god of Wikipedia:
"This system was the most common method of representing signed integers on computers. [2] An n-bit-s-complement numeral system can represent every integer in the range? (2N??? 1) to + (2N??? 1? 1) "
So how do you encode the data and see:
For example, to find the 4-bit representation of 5 (subscripts denote the base of the representation):
x = 510 therefore x = 01012
Hence, with N = 4:
x* = 2N? x = 24? 510 = 100002? 01012 = 10112
The calculation can be do entirely in base, converting to base 2 at the end:
x* = 2N? x = 24? 510 = 1110 = 10112
is the positive number itself, such as 5 is 0101, negative number of the highest position one, and then reverse the other low, plus one.
This is the common method of computer coding.
This is the code diagram.

And look at 1 ' s complement.
Positive numbers is the same simple, binary system used by and complement. Negative values is the bit complement of the corresponding positive value. The largest positive value is characterized by the sign (high-order) bit being off (0) and all other bits being on (1). The smallest negative value is characterized by the sign bit being 1, and all other bits being 0. The table below shows all possible values in a 4-bit system, from? 7 to +7. "Wiki"
This is another set of coding systems, positive and the highest is 0, the other bits represent the value, the largest is the other bits are all 1. The representation of a negative number is reversed directly on a positive number. So here is the concept of +0 and-0, the following coding diagram

Third, RFC1071 documents
directly see
To generate a checksum, the checksum field itself are cleared,the 16-bit 1 ' s complement sum is computed over the octets con Cerned, and the 1 ' s complement of this sum are placed in thechecksum field.
First, the calibration method here is a 1-complement system, not a 2-complement system used by the computer.
The ICMP protocol uses a checksum algorithm for IP, ICMP, UDP, and TCP headers that have checksum fields, but IP, TCP, and UDP only compute checksums for headers, while ICMP calculates checksums along with header and message data. Tests and algorithms can be implemented in two steps. First on the sending side, there are three steps:
1. Place the checksum field at 0.
2. The data that needs to be verified is counted as a number in 16bit, followed by a binary summation.
3. Reverse the summation of the previous step and deposit the checksum field.
Second, at the receiving end, there are three corresponding steps:
1. The data that needs to be verified is made up of a number in 16bit, followed by a binary summation, including a checksum field.
2. Reverse the summing result of the previous step.
3. Determine whether the final result is 0. If 0, the checksum is correct. If it is not 0, the protocol stack discards the received data.

Iv. CRC
When writing Ethernet frame data on an FPGA, the checksum uses a CRC, is it not the same as the verification algorithm used for each layer of Ethernet?
First look at the data format of the Ethernet frame

Look at the interpretation of FCS by the great god of Wikipedia
Frame Check Sequence

The frame check sequence (FCS) is a four-octet cyclic redundancy check (CRC) that allows detection of corrupted data withi n the entire frame as received on the receiver side. The FCS value is computed as a function of the protected MAC frame Fields:source and Destination address, Length/type fie LD, MAC client data and padding (that's, all fields except the FCS).
Running the CRC algorithm over the received frame data including the CRC code would always result with a zero value for error -free received data, because the CRC is a remainder of the data divided by the polynomial. However, this technique can result in "false negative", where a data with trailing zeroes would also result in the same zer o remainder. To avoid this scenario, the FCS are complemented (reversed for each bit) by the sender before it's attached to the end of The payload data. This is the algorithm result would always be a magic number or CRC32 residue of 0xc704dd7b when data have been received CO rrectly. This allows for receiving a frame and validating the FCS without knowing where the FCS field actually starts.
The validation algorithm specified here is CRC32, and the checksum data does not include the FCS itself. So let's see what a CRC32 is.
Wiki's two introduction to CRC
Https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Introduction
Https://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checks
The CRC Learning website abroad
Http://www.repairfaq.org/filipg/LINK/F_crc_v31.html
CRC, known as the basic idea of redundancy check, is to treat any information as a large volume of binary number, in order to use a fixed bit to represent the check code (easy to unify), put forward a polynomial divisor concept, if a redundant polynomial removal with a fixed polynomial, the remainder as a check code to use, The data can be verified. The essence of division is subtraction, and XOR is substituted for division in binary in order to reduce the computational amount of division.
For example, to transfer information in decimal 219, convert it to binary 11011011b,
The polynomial defined for the calibration is 1010; an XOR occurs

Since the remainder must be at least one less than the divisor, take 011 as the checksum, which sends the data to 2193. The receiver also uses 219 in addition to 1010, and if the remainder is the same, it indicates that the data is correct.
Five, why in the network Transmission Commander Test code needs, different algorithms
In the ICMP checksum is used in the checksum check, the Ethernet frame is used at the end of the CRC check, such as the other TCP header check code is what?
IP, ICMP, UDP, and TCP headers are defined in the TCP/IP protocol with checksum fields and are based on the checksum algorithm. But there are different,
"The following summary comes from the network"
(1) IP checksum:
The checksum of the IP datagram only examines the header of the IP datagram.
(2) UDP checksum:
The method of calculating checksum for UDP datagram is similar to that of IP datagram checksum, but the checksum of UDP is verifying the header and the data part together.
And before calculating the UDP checksum, we need to encapsulate a pseudo-header, the pseudo-header structure is as follows (the concrete structure definition is shown in the following code section):

Source IP Address | Destination IP Address | All 0 | protocol | UDP length

(3) TCP checksum:
The TCP checksum method is the same as UDP, the same as a pseudo-head, the difference is that the pseudo-head protocol code is 0x06, length is the length of the entire TCP message (including the TCP header).
(4) ICMP checksum:
The ICMP checksum is computed just like the entire ICMP packet checksum, without a pseudo-header or IP packet header.

It is summarized as follows: When calculating the checksum of an IP datagram and ICMP there is no need to encapsulate the pseudo-header, the difference is that the IP datagram only examines the header of the IP datagram, and the ICMP datagram checksum includes the ICMP header and the data portion (the entire ICMP length), the TCP and UDP checksum methods and the IP, ICMP checksum method, but TCP and UDP checksums require encapsulation of pseudo-headers. It is also important to note that the order of validation is from the top (layer) to the bottom (layer), such as verifying ICMP, first verifying the ICMP after verifying the IP (ICMP packet structure =ether + IP + ICMP), and before the checksum needs to first officer the value initialized to 0.

That is to say, only Ethernet uses CRC, other Baotou or data compared to the use of checksum algorithm.
For the time being the more essential reason, but one explanation is, because the CRC itself is a large amount of data validation, sum (and the capacity of only 16bit) for small data volume verification,

Vi. completion of CRC and checksum implementation
First C implementation
Checksum on the background of ICPM. Look at the data format that ICMP uses for information echoing:
Information Request or information Reply Message

Code for

#include <stdio.h>structtimeval{Long intTv_sec;Long intTv_usec;};structicmp{unsigned CharTypeunsigned CharCodeunsigned  ShortChecksumunsigned  ShortIdunsigned  ShortNumstructTimeval t;};unsigned  ShortIcmp_checksum (unsigned  Short* S,intLen) {unsigned intsum=0;unsigned  ShortCheckintCnt=len>>1;//cnt is size of 16bit     while(cnt--)    {sum+=*s++; }if((len&0x1)==0x1) sum+=* (unsigned Char*) s; while((sum>> -)!=0) sum= (sum&0xFFFF) + (sum>> -);return~check=sum&0xFFFF;}intMain () {structICMP i; I.type=8; I.code=0; i.checksum=0; I.id=Ten; i.num= -; I.t.tv_sec= -; I.t.tv_usec= $; I.checksum=icmp_checksum (&i,sizeof(i));printf("Checksum is%x\n", i.checksum);return 0;}

Learning about calibration algorithms (when learning Ethernet)

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.