lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [lwip-users] Checksum problem with lwip_chksum()


From: Paul C
Subject: RE: [lwip-users] Checksum problem with lwip_chksum()
Date: Fri, 10 Sep 2004 18:25:49 +0930

Heres my fix, it uses word access so it should be faster than the byte
access code.

Paul


static u16_t
lwip_chksum(void *dataptr, int len)
{
  u32_t acc;
  u8_t  OddStart;

  OddStart = (u8_t)dataptr & 0x01;
  if (OddStart)
  {
    acc =  *(u8_t *)dataptr;
    dataptr = (void *)((u8_t *)dataptr + 1);
    len--;
  } else
  {
    acc = 0;
  }
  
  LWIP_DEBUGF(INET_DEBUG, ("lwip_chksum(%p, %d)\n", (void *)dataptr,
  len));
  for( ; len > 1; len -= 2) {
      /*    acc = acc + *((u16_t *)dataptr)++;*/
    acc += *(u16_t *)dataptr;
    dataptr = (void *)((u16_t *)dataptr + 1);
  }

  /* add up any odd byte */
  if (len == 1) {
    acc += htons((u16_t)((*(u8_t *)dataptr) & 0xff) << 8);
    LWIP_DEBUGF(INET_DEBUG, ("inet: chksum: odd byte %d\n", (unsigned
    int)(*(u8_t *)dataptr)));
  } else {
    LWIP_DEBUGF(INET_DEBUG, ("inet: chksum: no odd byte\n"));
  }
  acc = (acc >> 16) + (acc & 0xffffUL);

  if ((acc & 0xffff0000) != 0) {
    acc = (acc >> 16) + (acc & 0xffffUL);
  }
  
  if (OddStart) {
    acc = ((acc & 0xff00)>>8) | ((acc & 0x00ff)<<8);
  }
  
  return (u16_t)acc;
}
On Thu, 2 Sep 2004 07:48:34 -0400, "John Taylor"
<address@hidden> said:
> > Can you post your code?
> Here is may hack.  I do not how efficient it is with respect to a true
> byte oriented checksum - but it was a quick fix.
> 
> In lwip_chksum(), in the for loop - replace: 
> 
>       acc = acc + *((u16_t *)dataptr)++;
> 
> with
>       /* !srt: read buffer byte-wise to resolve alignment issues! */
>      *((u8_t*)(&temp)+0) = *((u8_t*)dataptr);
>      *((u8_t*)(&temp)+1) = *((u8_t*)dataptr+1);
>       acc += temp;
> 
> where 'temp' is a local variable declared as:
> 
>       volatile u16_t temp; /* !srt: work var for intermediate value */
> 
> 
> -----Original Message-----
> From: address@hidden
> [mailto:address@hidden On Behalf
> Of Paul C
> Sent: Thursday, September 02, 2004 7:34 AM
> To: Mailing list for lwIP users; Mailing list for lwIP users
> Subject: Re: [lwip-users] Checksum problem with lwip_chksum()
> 
> I have always used tcp_write( ...,1);
> Now I look back at lwip_chksum() I remember finding 
> the same issue. As the copy function aligns the 
> data I decided that was easiest to copy all data.
> 
> Can you post your code?
> 
> Paul
> 
> 
> 
> On Thu, 2 Sep 2004 11:33:12 +0100, "Chris WIlliams"
> <address@hidden> said:
> > 
> > Has anybody else seen this problem? Is it a particular problem of the
> H8
> > (Paul C, did you have any problem here?). Would anybody like me to
> post
> > my new code?
> > 
> > Chris.
> 
> 
> _______________________________________________
> lwip-users mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/lwip-users
> 
> 
> 
> 
> _______________________________________________
> lwip-users mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/lwip-users




reply via email to

[Prev in Thread] Current Thread [Next in Thread]