checksum.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * arch/alpha/lib/checksum.c
  3. *
  4. * This file contains network checksum routines that are better done
  5. * in an architecture-specific manner due to speed..
  6. * Comments in other versions indicate that the algorithms are from RFC1071
  7. *
  8. * accelerated versions (and 21264 assembly versions ) contributed by
  9. * Rick Gorton <rick.gorton@alpha-processor.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/string.h>
  13. #include <asm/byteorder.h>
  14. static inline unsigned short from64to16(unsigned long x)
  15. {
  16. /* Using extract instructions is a bit more efficient
  17. than the original shift/bitmask version. */
  18. union {
  19. unsigned long ul;
  20. unsigned int ui[2];
  21. unsigned short us[4];
  22. } in_v, tmp_v, out_v;
  23. in_v.ul = x;
  24. tmp_v.ul = (unsigned long) in_v.ui[0] + (unsigned long) in_v.ui[1];
  25. /* Since the bits of tmp_v.sh[3] are going to always be zero,
  26. we don't have to bother to add that in. */
  27. out_v.ul = (unsigned long) tmp_v.us[0] + (unsigned long) tmp_v.us[1]
  28. + (unsigned long) tmp_v.us[2];
  29. /* Similarly, out_v.us[2] is always zero for the final add. */
  30. return out_v.us[0] + out_v.us[1];
  31. }
  32. /*
  33. * computes the checksum of the TCP/UDP pseudo-header
  34. * returns a 16-bit checksum, already complemented.
  35. */
  36. __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  37. __u32 len, __u8 proto, __wsum sum)
  38. {
  39. return (__force __sum16)~from64to16(
  40. (__force u64)saddr + (__force u64)daddr +
  41. (__force u64)sum + ((len + proto) << 8));
  42. }
  43. EXPORT_SYMBOL(csum_tcpudp_magic);
  44. __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  45. __u32 len, __u8 proto, __wsum sum)
  46. {
  47. unsigned long result;
  48. result = (__force u64)saddr + (__force u64)daddr +
  49. (__force u64)sum + ((len + proto) << 8);
  50. /* Fold down to 32-bits so we don't lose in the typedef-less
  51. network stack. */
  52. /* 64 to 33 */
  53. result = (result & 0xffffffff) + (result >> 32);
  54. /* 33 to 32 */
  55. result = (result & 0xffffffff) + (result >> 32);
  56. return (__force __wsum)result;
  57. }
  58. EXPORT_SYMBOL(csum_tcpudp_nofold);
  59. /*
  60. * Do a 64-bit checksum on an arbitrary memory area..
  61. *
  62. * This isn't a great routine, but it's not _horrible_ either. The
  63. * inner loop could be unrolled a bit further, and there are better
  64. * ways to do the carry, but this is reasonable.
  65. */
  66. static inline unsigned long do_csum(const unsigned char * buff, int len)
  67. {
  68. int odd, count;
  69. unsigned long result = 0;
  70. if (len <= 0)
  71. goto out;
  72. odd = 1 & (unsigned long) buff;
  73. if (odd) {
  74. result = *buff << 8;
  75. len--;
  76. buff++;
  77. }
  78. count = len >> 1; /* nr of 16-bit words.. */
  79. if (count) {
  80. if (2 & (unsigned long) buff) {
  81. result += *(unsigned short *) buff;
  82. count--;
  83. len -= 2;
  84. buff += 2;
  85. }
  86. count >>= 1; /* nr of 32-bit words.. */
  87. if (count) {
  88. if (4 & (unsigned long) buff) {
  89. result += *(unsigned int *) buff;
  90. count--;
  91. len -= 4;
  92. buff += 4;
  93. }
  94. count >>= 1; /* nr of 64-bit words.. */
  95. if (count) {
  96. unsigned long carry = 0;
  97. do {
  98. unsigned long w = *(unsigned long *) buff;
  99. count--;
  100. buff += 8;
  101. result += carry;
  102. result += w;
  103. carry = (w > result);
  104. } while (count);
  105. result += carry;
  106. result = (result & 0xffffffff) + (result >> 32);
  107. }
  108. if (len & 4) {
  109. result += *(unsigned int *) buff;
  110. buff += 4;
  111. }
  112. }
  113. if (len & 2) {
  114. result += *(unsigned short *) buff;
  115. buff += 2;
  116. }
  117. }
  118. if (len & 1)
  119. result += *buff;
  120. result = from64to16(result);
  121. if (odd)
  122. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  123. out:
  124. return result;
  125. }
  126. /*
  127. * This is a version of ip_compute_csum() optimized for IP headers,
  128. * which always checksum on 4 octet boundaries.
  129. */
  130. __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  131. {
  132. return (__force __sum16)~do_csum(iph,ihl*4);
  133. }
  134. EXPORT_SYMBOL(ip_fast_csum);
  135. /*
  136. * computes the checksum of a memory block at buff, length len,
  137. * and adds in "sum" (32-bit)
  138. *
  139. * returns a 32-bit number suitable for feeding into itself
  140. * or csum_tcpudp_magic
  141. *
  142. * this function must be called with even lengths, except
  143. * for the last fragment, which may be odd
  144. *
  145. * it's best to have buff aligned on a 32-bit boundary
  146. */
  147. __wsum csum_partial(const void *buff, int len, __wsum sum)
  148. {
  149. unsigned long result = do_csum(buff, len);
  150. /* add in old sum, and carry.. */
  151. result += (__force u32)sum;
  152. /* 32+c bits -> 32 bits */
  153. result = (result & 0xffffffff) + (result >> 32);
  154. return (__force __wsum)result;
  155. }
  156. EXPORT_SYMBOL(csum_partial);
  157. /*
  158. * this routine is used for miscellaneous IP-like checksums, mainly
  159. * in icmp.c
  160. */
  161. __sum16 ip_compute_csum(const void *buff, int len)
  162. {
  163. return (__force __sum16)~from64to16(do_csum(buff,len));
  164. }
  165. EXPORT_SYMBOL(ip_compute_csum);