[PATCHv2] networking: consolidate the IP checksum code

Baruch Siach baruch at tkos.co.il
Wed Sep 7 04:29:32 UTC 2011


Hi Joakim,

On Mon, Sep 05, 2011 at 10:09:14PM +0200, Joakim Tjernlund wrote:
> > From: Baruch Siach <baruch at tkos.co.il>
> >
> > Use a single IP checksum routine for ping, traceroute and udhcp.
> >
> > Signed-off-by: Baruch Siach <baruch at tkos.co.il>
> > ---
> >
> > Changes from v1:
> >    Fix inet_cksum() with odd length on big endian systems
> >    Remove declaration of removed udhcp_checksum()
> 
> This looks like it is from quagga. I cleaned that up/optimized it
> some years ago into this(you might want to fix the types):

What is the advantage of this implementation over the one I suggested (which I 
took from the traceroute code)?

> int			/* return checksum in low-order 16 bits */
> in_cksum(void *parg, int nbytes)
> {
> 	u_short *ptr = parg;
> 	register long		sum;		/* assumes long == 32 bits */
> 	register u_short	answer;		/* assumes u_short == 16 bits */
> 	register int count;
> 	/*
> 	 * Our algorithm is simple, using a 32-bit accumulator (sum),
> 	 * we add sequential 16-bit words to it, and at the end, fold back
> 	 * all the carry bits from the top 16 bits into the lower 16 bits.
> 	 */
> 	sum = 0;
> 	count = nbytes >> 1; /* div by 2 */
> 	for(ptr--; count; --count)
> 	  sum += *++ptr;
> 
> 	if (nbytes & 1) /* Odd */
> 	  sum += *(u_char *)(++ptr);   /* one byte only */
> 
> 	/*
> 	 * Add back carry outs from top 16 bits to low 16 bits.
> 	 */
> 
> 	sum  = (sum >> 16) + (sum & 0xffff);	/* add high-16 to low-16 */
> 	sum += (sum >> 16);			/* add carry */
> 	answer = ~sum;		/* ones-complement, then truncate to 16 bits */
> 	return(answer);
> }

baruch

-- 
                                                     ~. .~   Tk Open Systems
=}------------------------------------------------ooO--U--Ooo------------{=
   - baruch at tkos.co.il - tel: +972.2.679.5364, http://www.tkos.co.il -


More information about the busybox mailing list