--- sys/i386/i386/in_cksum.c.back	2007-07-17 18:32:52.000000000 +0000
+++ sys/i386/i386/in_cksum.c	2007-07-22 00:36:56.000000000 +0000
@@ -43,7 +43,7 @@
 #include <netinet/in.h>
 #include <netinet/in_systm.h>
 #include <netinet/ip.h>
-
+#include <netinet/ip_var.h>
 #include <machine/in_cksum.h>
 
 /*
@@ -259,7 +259,7 @@
  * load the value into a register but will not use it.  Since modern CPUs
  * reorder operations, this will generally take place in parallel with
  * other calculations.
- */
+ *
 #define ADD(n)	__asm __volatile \
 		("addl %1, %0" : "+r" (sum) : \
 		"g" (((const u_int32_t *)w)[n / 4]))
@@ -270,7 +270,11 @@
 		("" : : "r" (((const u_int32_t *)w)[n / 4]))
 #define MOP	__asm __volatile \
 		("adcl         $0, %0" : "+r" (sum))
-
+ * remove those macros because gcc will insert add instructions 
+ * between __asm expressions
+ */
+u_int64_t
+in_cksum_range(struct mbuf *m, int nxt, int offset, int bytes);
 u_short
 in_cksum_skip(m, len, skip)
 	struct mbuf *m;
@@ -279,10 +283,14 @@
 {
 	register u_short *w;
 	register unsigned sum = 0;
+	u_int64_t rv,rv1,rv2;
 	register int mlen = 0;
 	int byte_swapped = 0;
 	union { char	c[2]; u_short	s; } su;
 
+	rv2 = in_cksum_range(m, 0, skip, len - skip);
+
+	__asm __volatile("rdtsc": "=A" (rv));	
 	len -= skip;
 	for (; skip && m; m = m->m_next) {
 		if (m->m_len > skip) {
@@ -341,15 +349,25 @@
 		 * Advance to a 486 cache line boundary.
 		 */
 		if (4 & (int) w && mlen >= 4) {
-			ADD(0);
-			MOP;
+		/*	ADD(0);
+			MOP;*/
+		  	__asm volatile ("addl %1, %0 \n"
+					"adcl $0, %0"
+	 				: "+r" (sum) 
+					: "g" (((const unsigned int *)w)[0 / 4]));
 			w += 2;
 			mlen -= 4;
 		}
 		if (8 & (int) w && mlen >= 8) {
-			ADD(0);
+		/*	ADD(0);
 			ADDC(4);
-			MOP;
+			MOP;*/
+			__asm volatile ("addl %1, %0 \n"
+					"adcl %2, %0 \n"
+					"adcl $0, %0"
+   					: "+r" (sum) : 
+					"g" (((const unsigned int *)w)[0 / 4]),
+   					"g" (((const unsigned int *)w)[4 / 4]));
 			w += 4;
 			mlen -= 8;
 		}
@@ -379,7 +397,7 @@
 			 * is initially 33 (not 32) to guaranteed that
 			 * the LOAD(32) is within bounds.
 			 */
-			ADD(16);
+			/* ADD(16);
 			ADDC(0);
 			ADDC(4);
 			ADDC(8);
@@ -388,12 +406,30 @@
 			ADDC(20);
 			ADDC(24);
 			ADDC(28);
-			MOP;
+			MOP; */
+			__asm volatile ("addl %1, %0 \n"
+					"adcl %2, %0 \n"
+					"adcl %3, %0 \n"
+					"adcl %4, %0 \n"
+					"adcl %5, %0 \n"
+					"adcl %7, %0 \n"
+					"adcl %8, %0 \n"
+					"adcl %9, %0 \n"
+					"adcl $0, %0" : "+r" (sum) :
+					"g" (((const unsigned int *)w)[16 / 4]),
+					"g" (((const unsigned int *)w)[0 / 4]),
+					"g" (((const unsigned int *)w)[4 / 4]),
+					"g" (((const unsigned int *)w)[8 / 4]),
+					"g" (((const unsigned int *)w)[12 / 4]),
+					"r" (((const unsigned int *)w)[32 / 4]),
+					"g" (((const unsigned int *)w)[20 / 4]),
+					"g" (((const unsigned int *)w)[24 / 4]),
+					"g" (((const unsigned int *)w)[28 / 4]));
 			w += 16;
 		}
 		mlen += 32 + 1;
 		if (mlen >= 32) {
-			ADD(16);
+			/* ADD(16);
 			ADDC(0);
 			ADDC(4);
 			ADDC(8);
@@ -401,23 +437,54 @@
 			ADDC(20);
 			ADDC(24);
 			ADDC(28);
-			MOP;
+			MOP;*/
+			__asm volatile ("addl %1, %0 \n"
+					"adcl %2, %0 \n"
+					"adcl %3, %0 \n"
+					"adcl %4, %0 \n"
+					"adcl %5, %0 \n"
+					"adcl %6, %0 \n"
+					"adcl %7, %0 \n"
+					"adcl %8, %0 \n"
+					"adcl $0, %0" : "+r" (sum) :
+					"g" (((const unsigned int *)w)[16 / 4]),
+					"g" (((const unsigned int *)w)[0 / 4]),
+					"g" (((const unsigned int *)w)[4 / 4]),
+					"g" (((const unsigned int *)w)[8 / 4]),
+					"g" (((const unsigned int *)w)[12 / 4]),
+					"g" (((const unsigned int *)w)[20 / 4]),
+					"g" (((const unsigned int *)w)[24 / 4]),
+					"g" (((const unsigned int *)w)[28 / 4]));
 			w += 16;
 			mlen -= 32;
 		}
 		if (mlen >= 16) {
-			ADD(0);
+			/* ADD(0);
 			ADDC(4);
 			ADDC(8);
 			ADDC(12);
-			MOP;
+			MOP;*/
+			__asm volatile ("addl %1, %0 \n"
+					"adcl %2, %0 \n"
+					"adcl %3, %0 \n"
+					"adcl %4, %0 \n"
+					"adcl $0, %0": "+r"(sum): 
+					"g" (((const unsigned int *)w)[0 / 4]),
+					"g" (((const unsigned int *)w)[4 / 4]),
+					"g" (((const unsigned int *)w)[8 / 4]),
+					"g" (((const unsigned int *)w)[12 / 4]));
 			w += 8;
 			mlen -= 16;
 		}
 		if (mlen >= 8) {
-			ADD(0);
+			/* ADD(0);
 			ADDC(4);
-			MOP;
+			MOP;*/
+			__asm volatile ("addl %1, %0 \n"
+					"adcl %2, %0 \n"
+					"adcl $0, %0": "+r"(sum): 
+					"g" (((const unsigned int *)w)[0 / 4]),
+					"g" (((const unsigned int *)w)[4 / 4]));
 			w += 4;
 			mlen -= 8;
 		}
@@ -455,6 +522,172 @@
 		sum += su.s;
 	}
 	REDUCE;
+	__asm __volatile("rdtsc": "=A" (rv1));	
+	printf("DFBSD with %16llx cycles while FreeBSD with %16llx cycles\n", rv2, rv1 - rv);
 	return (~sum & 0xffff);
 }
+__uint32_t asm_ones32(const void *buf, int count);      /* in 32 bit words */
+u_int64_t
+in_cksum_range(struct mbuf *m, int nxt, int offset, int bytes)
+{
+     __uint8_t *ptr;
+     __uint32_t sum0;
+     __uint32_t sum1;
+     int n;
+     int flip;
+     u_int64_t rv,rv1;
+     
+     __asm __volatile("rdtsc": "=A" (rv));	
+     sum0 = 0;
+     sum1 = 0;
+     flip = 0;
+ 
+     if (nxt != 0) {
+         uint32_t sum32;
+         struct ipovly ipov;
+ 
+         /* pseudo header */
+         if (offset < sizeof(struct ipovly))
+                 panic("in_cksum_range: offset too short");
+         if (m->m_len < sizeof(struct ip))
+                panic("in_cksum_range: bad mbuf chain");
+         bzero(&ipov, sizeof ipov);
+         ipov.ih_len = htons(bytes);
+         ipov.ih_pr = nxt;
+         ipov.ih_src = mtod(m, struct ip *)->ip_src;
+         ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
+         ptr = (uint8_t *)&ipov;
+ 
+         sum32 = asm_ones32(ptr, sizeof(ipov) / 4);
+         sum32 = (sum32 >> 16) + (sum32 & 0xffff);
+         if (flip)
+            sum1 += sum32;
+         else
+             sum0 += sum32;
+    }
+ 
+    /*
+     * Skip fully engulfed mbufs.  Branch predict optimal.
+     */
+    while (m && offset >= m->m_len) {
+        offset -= m->m_len;
+        m = m->m_next;
+    }
+ 
+     /*
+      * Process the checksum for each segment.  Note that the code below is
+      * branch-predict optimal, so it's faster then you might otherwise
+      * believe.  When we are buffer-aligned but also odd-byte-aligned from
+      * the point of view of the IP packet, we accumulate to sum1 instead of
+      * sum0.
+      *
+      * Initial offsets do not pre-set flip (assert that offset is even?)
+      */
+    while (bytes > 0 && m) {
+        /*
+         * Calculate pointer base and number of bytes to snarf, account
+         * for snarfed bytes.
+         */
+        ptr = mtod(m, __uint8_t *) + offset;
+        if ((n = m->m_len - offset) > bytes)
+            n = bytes;
+	    bytes -= n;
+ 
+        /*
+         * First 16-bit-align our buffer by eating a byte if necessary,
+         * then 32-bit-align our buffer by eating a word if necessary.
+         *
+         * We are endian-sensitive when chomping a byte.  WARNING!  Be
+         * careful optimizing this!  16 ane 32 bit words must be aligned
+         * for this to be generic code.
+         */
+        if (((intptr_t)ptr & 1) && n) {
+#if BYTE_ORDER == LITTLE_ENDIAN
+           if (flip)
+                 sum1 += ptr[0];
+             else
+                 sum0 += ptr[0];
+#else
+             if (flip)
+                 sum0 += ptr[0];
+             else
+                 sum1 += ptr[0];
+#endif
+             ++ptr;
+             --n;
+             flip = 1 - flip;
+        }
+        if (((intptr_t)ptr & 2) && n > 1) {
+             if (flip)
+                sum1 += *(__uint16_t *)ptr;
+             else
+                sum0 += *(__uint16_t *)ptr;
+             ptr += 2;
+             n -= 2;
+         }
+ 
+         /*
+          * Process a 32-bit aligned data buffer and accumulate the result
+          * in sum0 or sum1.  Allow only one 16 bit overflow carry.
+          */
+         if (n >= 4) {
+             __uint32_t sum32;
+ 
+             sum32 = asm_ones32((void *)ptr, n >> 2);
+             sum32 = (sum32 >> 16) + (sum32 & 0xffff);
+             if (flip)
+                 sum1 += sum32;
+             else
+                 sum0 += sum32;
+             ptr += n & ~3;
+            /* n &= 3; dontcare */
+         }
+ 
+         /*
+          * Handle oddly-sized buffers.  Handle word issues first while
+          * ptr is still aligned.
+          */
+         if (n & 2) {
+             if (flip)
+                 sum1 += *(__uint16_t *)ptr;
+             else
+                 sum0 += *(__uint16_t *)ptr;
+             ptr += 2;
+             /* n -= 2; dontcare */
+         }
+         if (n & 1) {
+#if BYTE_ORDER == LITTLE_ENDIAN
+             if (flip)
+                 sum1 += ptr[0];
+             else
+                 sum0 += ptr[0];
+#else
+             if (flip)
+                 sum0 += ptr[0];
+             else
+                 sum1 += ptr[0];
+#endif
+             /* ++ptr; dontcare */
+             /* --n; dontcare */
+             flip = 1 - flip;
+         }
+         m = m->m_next;
+         offset = 0;
+     }
+ 
+     /*
+      * Due to byte aligned or oddly-sized buffers we may have a checksum
+      * in sum1 which needs to be shifted and added to our main sum.  There
+      * is a presumption here that no more then 255 overflows occured which
+      * is 255/3 byte aligned mbufs in the worst case.
+      */
+     sum0 += sum1 << 8;
+     sum0 = (sum0 >> 16) + (sum0 & 0xffff);
+     if (sum0 > 0xffff)
+         ++sum0;
+     __asm __volatile("rdtsc": "=A" (rv1));	
+     return rv1 - rv;
+//     printf("DFBSD with %16llx cycles,",  rv1 - rv);
+//     return(~sum0 & 0xffff);
+}
 #endif
--- sys/i386/i386/support.s.back	2007-07-22 00:46:55.000000000 +0000
+++ sys/i386/i386/support.s	2007-07-22 00:47:07.000000000 +0000
@@ -1532,3 +1532,50 @@
 	movl	%edx,16(%eax)
 	movl	%eax,bbhead
 	NON_GPROF_RET
+
+	.text
+         /*
+          * asm_ones32(32bitalignedbuffer, numberof32bitwords)
+          *
+          * Returns the 32 bit one complement partial checksum.  This is
+          * basically a 1's complement checksum without the inversion (~)
+          * at the end.  A 32 bit value is returned.  If the caller is
+          * calculating a 16 bit 1's complement checksum the caller must
+          * collapse the 32 bit return value via:
+          *
+          *      result = (result >> 16) + (result & 0xFFFF)
+          *      if (result > 0xFFFF)
+          *          result -= 0xFFFF ;    <<< same as (result + 1) & 0xFFFF
+          *                                  within the range of result.
+          * Note that worst case 0xFFFFFFFF + 0xFFFFFFFF = 0xFFFFFFFE + CARRY,
+          * so no double-carry ever occurs.
+          */
+         .p2align 4
+ENTRY(asm_ones32)
+         movl    4(%esp),%edx    /* %edx = buffer pointer */
+         movl    8(%esp),%ecx    /* %ecx = counter */
+         subl    %eax,%eax       /* %eax = checksum */
+         cmpl    $5,%ecx
+         jl      2f
+1:
+         subl    $5,%ecx
+         addl    (%edx),%eax
+         adcl    4(%edx),%eax
+         adcl    8(%edx),%eax
+         adcl    12(%edx),%eax
+         adcl    16(%edx),%eax
+         adcl    $0,%eax
+         addl    $20,%edx
+         cmpl    $5,%ecx
+         jge     1b
+2:
+         testl   %ecx,%ecx
+         je      4f
+3:
+         addl    (%edx),%eax
+         adcl    $0,%eax
+         addl    $4,%edx
+         decl    %ecx
+         jnz     3b
+4:
+         ret
