checksum_64.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #ifndef _ASM_X86_CHECKSUM_64_H
  2. #define _ASM_X86_CHECKSUM_64_H
  3. /*
  4. * Checksums for x86-64
  5. * Copyright 2002 by Andi Kleen, SuSE Labs
  6. * with some code from asm-x86/checksum.h
  7. */
  8. #include <linux/compiler.h>
  9. #include <asm/uaccess.h>
  10. #include <asm/byteorder.h>
  11. /**
  12. * csum_fold - Fold and invert a 32bit checksum.
  13. * sum: 32bit unfolded sum
  14. *
  15. * Fold a 32bit running checksum to 16bit and invert it. This is usually
  16. * the last step before putting a checksum into a packet.
  17. * Make sure not to mix with 64bit checksums.
  18. */
  19. static inline __sum16 csum_fold(__wsum sum)
  20. {
  21. asm(" addl %1,%0\n"
  22. " adcl $0xffff,%0"
  23. : "=r" (sum)
  24. : "r" ((__force u32)sum << 16),
  25. "0" ((__force u32)sum & 0xffff0000));
  26. return (__force __sum16)(~(__force u32)sum >> 16);
  27. }
  28. /*
  29. * This is a version of ip_compute_csum() optimized for IP headers,
  30. * which always checksum on 4 octet boundaries.
  31. *
  32. * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
  33. * Arnt Gulbrandsen.
  34. */
  35. /**
  36. * ip_fast_csum - Compute the IPv4 header checksum efficiently.
  37. * iph: ipv4 header
  38. * ihl: length of header / 4
  39. */
  40. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  41. {
  42. unsigned int sum;
  43. asm(" movl (%1), %0\n"
  44. " subl $4, %2\n"
  45. " jbe 2f\n"
  46. " addl 4(%1), %0\n"
  47. " adcl 8(%1), %0\n"
  48. " adcl 12(%1), %0\n"
  49. "1: adcl 16(%1), %0\n"
  50. " lea 4(%1), %1\n"
  51. " decl %2\n"
  52. " jne 1b\n"
  53. " adcl $0, %0\n"
  54. " movl %0, %2\n"
  55. " shrl $16, %0\n"
  56. " addw %w2, %w0\n"
  57. " adcl $0, %0\n"
  58. " notl %0\n"
  59. "2:"
  60. /* Since the input registers which are loaded with iph and ihl
  61. are modified, we must also specify them as outputs, or gcc
  62. will assume they contain their original values. */
  63. : "=r" (sum), "=r" (iph), "=r" (ihl)
  64. : "1" (iph), "2" (ihl)
  65. : "memory");
  66. return (__force __sum16)sum;
  67. }
  68. /**
  69. * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
  70. * @saddr: source address
  71. * @daddr: destination address
  72. * @len: length of packet
  73. * @proto: ip protocol of packet
  74. * @sum: initial sum to be added in (32bit unfolded)
  75. *
  76. * Returns the pseudo header checksum the input data. Result is
  77. * 32bit unfolded.
  78. */
  79. static inline __wsum
  80. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
  81. unsigned short proto, __wsum sum)
  82. {
  83. asm(" addl %1, %0\n"
  84. " adcl %2, %0\n"
  85. " adcl %3, %0\n"
  86. " adcl $0, %0\n"
  87. : "=r" (sum)
  88. : "g" (daddr), "g" (saddr),
  89. "g" ((len + proto)<<8), "0" (sum));
  90. return sum;
  91. }
  92. /**
  93. * csum_tcpup_magic - Compute an IPv4 pseudo header checksum.
  94. * @saddr: source address
  95. * @daddr: destination address
  96. * @len: length of packet
  97. * @proto: ip protocol of packet
  98. * @sum: initial sum to be added in (32bit unfolded)
  99. *
  100. * Returns the 16bit pseudo header checksum the input data already
  101. * complemented and ready to be filled in.
  102. */
  103. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  104. unsigned short len,
  105. unsigned short proto, __wsum sum)
  106. {
  107. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  108. }
  109. /**
  110. * csum_partial - Compute an internet checksum.
  111. * @buff: buffer to be checksummed
  112. * @len: length of buffer.
  113. * @sum: initial sum to be added in (32bit unfolded)
  114. *
  115. * Returns the 32bit unfolded internet checksum of the buffer.
  116. * Before filling it in it needs to be csum_fold()'ed.
  117. * buff should be aligned to a 64bit boundary if possible.
  118. */
  119. extern __wsum csum_partial(const void *buff, int len, __wsum sum);
  120. #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER 1
  121. #define HAVE_CSUM_COPY_USER 1
  122. /* Do not call this directly. Use the wrappers below */
  123. extern __wsum csum_partial_copy_generic(const void *src, const void *dst,
  124. int len, __wsum sum,
  125. int *src_err_ptr, int *dst_err_ptr);
  126. extern __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
  127. int len, __wsum isum, int *errp);
  128. extern __wsum csum_partial_copy_to_user(const void *src, void __user *dst,
  129. int len, __wsum isum, int *errp);
  130. extern __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  131. int len, __wsum sum);
  132. /* Old names. To be removed. */
  133. #define csum_and_copy_to_user csum_partial_copy_to_user
  134. #define csum_and_copy_from_user csum_partial_copy_from_user
  135. /**
  136. * ip_compute_csum - Compute an 16bit IP checksum.
  137. * @buff: buffer address.
  138. * @len: length of buffer.
  139. *
  140. * Returns the 16bit folded/inverted checksum of the passed buffer.
  141. * Ready to fill in.
  142. */
  143. extern __sum16 ip_compute_csum(const void *buff, int len);
  144. /**
  145. * csum_ipv6_magic - Compute checksum of an IPv6 pseudo header.
  146. * @saddr: source address
  147. * @daddr: destination address
  148. * @len: length of packet
  149. * @proto: protocol of packet
  150. * @sum: initial sum (32bit unfolded) to be added in
  151. *
  152. * Computes an IPv6 pseudo header checksum. This sum is added the checksum
  153. * into UDP/TCP packets and contains some link layer information.
  154. * Returns the unfolded 32bit checksum.
  155. */
  156. struct in6_addr;
  157. #define _HAVE_ARCH_IPV6_CSUM 1
  158. extern __sum16
  159. csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
  160. __u32 len, unsigned short proto, __wsum sum);
  161. static inline unsigned add32_with_carry(unsigned a, unsigned b)
  162. {
  163. asm("addl %2,%0\n\t"
  164. "adcl $0,%0"
  165. : "=r" (a)
  166. : "0" (a), "r" (b));
  167. return a;
  168. }
  169. #endif /* _ASM_X86_CHECKSUM_64_H */