Changeset 14964 in vbox for trunk/src/VBox/Devices/Network/slirp/cksum.c
- Timestamp:
- Dec 3, 2008 9:34:37 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/Network/slirp/cksum.c
r14470 r14964 47 47 48 48 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x) 49 #define REDUCE { l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}49 #define REDUCE { l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum); } 50 50 51 51 int cksum(struct mbuf *m, int len) 52 52 { 53 54 55 56 53 register u_int16_t *w; 54 register int sum = 0; 55 register int mlen = 0; 56 int byte_swapped = 0; 57 57 58 union { 59 u_int8_t c[2]; 60 u_int16_t s; 61 } s_util; 62 union { 63 u_int16_t s[2]; 64 u_int32_t l; 65 } l_util; 58 union 59 { 60 u_int8_t c[2]; 61 u_int16_t s; 62 } s_util; 63 union 64 { 65 u_int16_t s[2]; 66 u_int32_t l; 67 } l_util; 66 68 67 if (m->m_len == 0) 68 goto cont; 69 w = mtod(m, u_int16_t *); 69 if (m->m_len == 0) 70 goto cont; 71 w = mtod(m, u_int16_t *); 72 73 mlen = m->m_len; 70 74 71 mlen = m->m_len; 75 if (len < mlen) 76 mlen = len; 77 len -= mlen; 78 /* 79 * Force to even boundary. 80 */ 81 if ((1 & (long) w) && (mlen > 0)) 82 { 83 REDUCE; 84 sum <<= 8; 85 s_util.c[0] = *(u_int8_t *)w; 86 w = (u_int16_t *)((int8_t *)w + 1); 87 mlen--; 88 byte_swapped = 1; 89 } 90 /* 91 * Unroll the loop to make overhead from 92 * branches &c small. 93 */ 94 while ((mlen -= 32) >= 0) 95 { 96 sum += w[ 0]; sum += w[ 1]; sum += w[ 2]; sum += w[ 3]; 97 sum += w[ 4]; sum += w[ 5]; sum += w[ 6]; sum += w[ 7]; 98 sum += w[ 8]; sum += w[ 9]; sum += w[10]; sum += w[11]; 99 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15]; 100 w += 16; 101 } 102 mlen += 32; 103 while ((mlen -= 8) >= 0) 104 { 105 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 106 w += 4; 107 } 108 mlen += 8; 109 if (mlen == 0 && byte_swapped == 0) 110 goto cont; 111 REDUCE; 112 while ((mlen -= 2) >= 0) 113 { 114 sum += *w++; 115 } 72 116 73 if (len < mlen) 74 mlen = len; 75 len -= mlen; 76 /* 77 * Force to even boundary. 78 */ 79 if ((1 & (long) w) && (mlen > 0)) { 80 REDUCE; 81 sum <<= 8; 82 s_util.c[0] = *(u_int8_t *)w; 83 w = (u_int16_t *)((int8_t *)w + 1); 84 mlen--; 85 byte_swapped = 1; 117 if (byte_swapped) 118 { 119 REDUCE; 120 sum <<= 8; 121 byte_swapped = 0; 122 if (mlen == -1) 123 { 124 s_util.c[1] = *(u_int8_t *)w; 125 sum += s_util.s; 126 mlen = 0; 86 127 } 87 /* 88 * Unroll the loop to make overhead from 89 * branches &c small. 90 */ 91 while ((mlen -= 32) >= 0) { 92 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 93 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7]; 94 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11]; 95 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15]; 96 w += 16; 97 } 98 mlen += 32; 99 while ((mlen -= 8) >= 0) { 100 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 101 w += 4; 102 } 103 mlen += 8; 104 if (mlen == 0 && byte_swapped == 0) 105 goto cont; 106 REDUCE; 107 while ((mlen -= 2) >= 0) { 108 sum += *w++; 109 } 110 111 if (byte_swapped) { 112 REDUCE; 113 sum <<= 8; 114 byte_swapped = 0; 115 if (mlen == -1) { 116 s_util.c[1] = *(u_int8_t *)w; 117 sum += s_util.s; 118 mlen = 0; 119 } else 120 121 mlen = -1; 122 } else if (mlen == -1) 123 s_util.c[0] = *(u_int8_t *)w; 128 else 129 mlen = -1; 130 } 131 else if (mlen == -1) 132 s_util.c[0] = *(u_int8_t *)w; 124 133 125 134 cont: 126 135 #ifdef DEBUG 127 if (len) { 128 DEBUG_ERROR((dfd, "cksum: out of data\n")); 129 DEBUG_ERROR((dfd, " len = %d\n", len)); 130 } 136 if (len) 137 { 138 DEBUG_ERROR((dfd, "cksum: out of data\n")); 139 DEBUG_ERROR((dfd, " len = %d\n", len)); 140 } 131 141 #endif 132 if (mlen == -1) { 133 /* The last mbuf has odd # of bytes. Follow the 134 standard (the odd byte may be shifted left by 8 bits 135 or not as determined by endian-ness of the machine) */ 136 s_util.c[1] = 0; 137 sum += s_util.s; 138 } 139 REDUCE; 140 return (~sum & 0xffff); 142 if (mlen == -1) 143 { 144 /* The last mbuf has odd # of bytes. Follow the 145 standard (the odd byte may be shifted left by 8 bits 146 or not as determined by endian-ness of the machine) */ 147 s_util.c[1] = 0; 148 sum += s_util.s; 149 } 150 REDUCE; 151 return (~sum & 0xffff); 141 152 }
Note:
See TracChangeset
for help on using the changeset viewer.