Skip to main content

7. Enhanced Checksum Algorithm

7. Enhanced checksum algorithm.​

7.1. Effect of checksum on transport performance.​

Performance experiments with Class 4 transport at the NBS have revealed that straightforward implementation of the Fletcher checksum using the algorithm recommended in the ISO transport standard leads to severe reduction of transport throughput. Early modeling indicated throughput drops of as much as 66% when using the checksum. Work by Anastase Nakassis [NAK85] of the NBS led to several improved implementations. The performance degradation due to checksum is now in the range of 40-55%, when using the improved implementations.

It is possible that transport may be used over a network that does not provide error detection. In such a case the transport checksum is necessary to ensure data integrity. In many instances, the underlying subnetwork provides some error checking mechanism. The HDLC frame check sequence as used by X.25, IEEE 802.3 and 802.4 rely on a 32 bit cyclic redundancy check and satellite link hardware frequently provides the HDLC frame check sequence. However, these are all link or physical layer error detection mechanisms which operate only point-to-point and not end-to-end as the transport checksum does. Some links provide error recovery while other links simply discard damaged messages. If adequate error recovery is provided, then the transport checksum is extra overhead, since transport will detect when the link mechanism has discarded a message and will retransmit the message. Even when the IP fragments the TPDU, the receiving IP will discover a hole in the reassembly buffer and discard the partially assembled datagram (i.e., TPDU). Transport will detect this missing TPDU and recover by means of the retransmission mechanism.

7.2. Enhanced algorithm.​

The Fletcher checksum algorithm given in an annex to IS 8073 is not part of the standard, and is included in the annex as a suggestion to implementors. This was done so that as improvements or new algorithms came along, they could be incorporated without the necessity to change the standard.

Nakassis has provided three ways of coding the algorithm, shown below, to provide implementors with insight rather than universally transportable code. One version uses a high order language (C). A second version uses C and VAX assembler, while a third uses only VAX assembler. In all the versions, the constant MODX appears. This represents the maximum number of sums that can be taken without experiencing overflow. This constant depends on the processor's word size and the arithmetic mode, as follows:

McCoy [Page 47]

RFC 1008 June 1987

Choose n such that

(n+1)*(254 + 255*n/2) <= 2**N - 1

where N is the number of usable bits for signed (unsigned) arithmetic. Nakassis shows [NAK85] that it is sufficient to take

 n <= sqrt( 2*(2**N - 1)/255 )

and that n = sqrt( 2*(2**N - 1)/255 ) - 2 generally yields usable values. The constant MODX then is taken to be n.

Some typical values for MODX are given in the following table.

BITS/WORD                MODX          ARITHMETIC
15 14 signed
16 21 unsigned
31 4102 signed
32 5802 unsigned

This constant is used to reduce the number of times mod 255 addition is invoked, by way of speeding up the algorithm.

It should be noted that it is also possible to implement the checksum in separate hardware. However, because of the placement of the checksum within the TPDU header rather than at the end of the TPDU, implementing this with registers and an adder will require significant associated logic to access and process each octet of the TPDU and to move the checksum octets in to the proper positions in the TPDU. An alternative to designing this supporting logic is to use a fast, microcoded 8-bit CPU to handle this access and the computation. Although there is some speed penalty over separate logic, savings may be realized through a reduced chip count and development time.

7.2.1. C language algorithm.​

#define MODX 4102

 encodecc( mess,len,k )
unsigned char mess[] ; /* the TPDU to be checksummed */
int len,
k; /* position of first checksum octet
as an offset from mess[0] */

McCoy [Page 48]

RFC 1008 June 1987

 { int ip,
iq,
ir,
c0,
c1;
unsigned char *p,*p1,*p2,*p3 ;

p = mess ; p3 = mess + len ;

if ( k > 0) { mess[k-1] = 0x00 ; mess[k] = 0x00 ; }
/* insert zeros for checksum octets */

c0 = 0 ; c1 = 0 ; p1 = mess ;
while (p1 < p3) /* outer sum accumulation loop */
{
p2 = p1 + MODX ; if (p2 > p3) p2 = p3 ;
for (p = p1 ; p < p2 ; p++) /* inner sum accumulation loop */
{ c0 = c0 + (*p) ; c1 = c1 + c0 ;
}
c0 = c0%255 ; c1 = c1%255 ; p1 = p2 ;
/* adjust accumulated sums to mod 255 */
}
ip = (c1 << 8) + c0 ; /* concatenate c1 and c0 */

if (k > 0)
{ /* compute and insert checksum octets */

iq = ((len-k)*c0 - c1)%255 ; if (iq <= 0) iq = iq + 255 ;
mess[k-1] = iq ;
ir = (510 - c0 - iq) ;
if (ir > 255) ir = ir - 255 ; mess[k] = ir ;
}
return(ip) ;
}

7.2.2. C/assembler algorithm.​

#include

 encodecm(mess,len,k)
unsigned char *mess ;
int len,k ;
{
int i,ip,c0,c1 ;

if (k > 0) { mess[k-1] = 0x00 ; mess[k] = 0x00 ; }
ip = optm1(mess,len,&c0,&c1) ;
if (k > 0)
{ i = ( (len-k)*c0 - c1)%255 ; if (i <= 0) i = i + 255 ;
mess[k-1] = i ;
i = (510 - c0 - i) ; if (i > 255) i = i - 255 ;

McCoy [Page 49]

RFC 1008 June 1987

     mess[k] = i ;
}
return(ip) ;
}
; calling sequence optm(message,length,&c0,&c1) where
; message is an array of bytes
; length is the length of the array
; &c0 and &c1 are the addresses of the counters to hold the
; remainder of; the first and second order partial sums
; mod(255).

.ENTRY optm1,^M<r2,r3,r4,r5,r6,r7,r8,r9,r10,r11>
movl 4(ap),r8 ; r8---> message
movl 8(ap),r9 ; r9=length
clrq r4 ; r5=r4=0
clrq r6 ; r7=r6=0
clrl r3 ; clear high order bytes of r3
movl #255,r10 ; r10 holds the value 255
movl #4102,r11 ; r11= MODX
xloop: movl r11,r7 ; if r7=MODX
cmpl r9,r7 ; is r9>=r7 ?
bgeq yloop ; if yes, go and execute the inner
; loop MODX times.
movl r9,r7 ; otherwise set r7, the inner loop
; counter,
yloop: movb (r8)+,r3 ;
addl2 r3,r4 ; sum1=sum1+byte
addl2 r4,r6 ; sum2=sum2+sum1
sobgtr r7,yloop ; while r7>0 return to iloop
; for mod 255 addition
ediv r10,r6,r0,r6 ; r6=remainder
ediv r10,r4,r0,r4 ;
subl2 r11,r9 ; adjust r9
bgtr xloop ; go for another loop if necessary
movl r4,@12(ap) ; first argument
movl r6,@16(ap) ; second argument
ashl #8,r6,r0 ;
addl2 r4,r0 ;
ret

7.2.3. Assembler algorithm.​

buff0: .blkb 3 ; allocate 3 bytes so that aloop is ; optimally aligned ; macro implementation of Fletcher's algorithm. ; calling sequence ip=encodemm(message,length,k) where ; message is an array of bytes ; length is the length of the array ; k is the location of the check octets if >0, ; an indication not to encode if 0. ;

McCoy [Page 50]

RFC 1008 June 1987

movl 4(ap),r8 ; r8---> message movl 8(ap),r9 ; r9=length clrq r4 ; r5=r4=0 clrq r6 ; r7=r6=0 clrl r3 ; clear high order bytes of r3 movl #255,r10 ; r10 holds the value 255 movl 12(ap),r2 ; r2=k bleq bloop ; if r2<=0, we do not encode subl3 r2,r9,r11 ; set r11=L-k addl2 r8,r2 ; r2---> octet k+1 clrb (r2) ; clear check octet k+1 clrb -(r2) ; clear check octet k, r2---> octet k. bloop: movw #4102,r7 ; set r7 (inner loop counter) = to MODX cmpl r9,r7 ; if r9>=MODX, then go directly to adjust r9 bgeq aloop ; and execute the inner loop MODX times. movl r9,r7 ; otherwise set r7, the inner loop counter, ; equal to r9, the number of the ; unprocessed characters aloop: movb (r8)+,r3 ; addl2 r3,r4 ; c0=c0+byte addl2 r4,r6 ; sum2=sum2+sum1 sobgtr r7,aloop ; while r7>0 return to iloop ; for mod 255 addition ediv r10,r6,r0,r6 ; r6=remainder ediv r10,r4,r0,r4 ; subl2 #4102,r9 ; bgtr bloop ; go for another loop if necessary ashl #8,r6,r0 ; r0=256r6 addl2 r4,r0 ; r0=256r6+r4 cmpl r2,r7 ; since r7=0, we are checking if r2 is bleq exit ; zero or less: if yes we bypass ; the encoding. movl r6,r8 ; r8=c1 mull3 r11,r4,r6 ; r6=(L-k)*c0 ediv r10,r6,r7,r6 ; r6 = (L-k)*c0 mod(255) subl2 r8,r6 ; r6= ((L-k)*c0)%255 -c1 and if negative, bgtr byte1 ; we must addl2 r10,r6 ; add 255 byte1: movb r6,(r2)+ ; save the octet and let r2---> octet k+1 addl2 r6,r4 ; r4=r4+r6=(x+c0) subl3 r4,r10,r4 ; r4=255-(x+c0) bgtr byte2 ; if >0 r4=octet (k+1) addl2 r10,r4 ; r4=255+r4 byte2: movb r4,(r2) ; save y in octet k+1 exit: ret