checksum.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (C) 2016 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __ASM_CHECKSUM_H
  17. #define __ASM_CHECKSUM_H
  18. #include <linux/types.h>
  19. static inline __sum16 csum_fold(__wsum csum)
  20. {
  21. u32 sum = (__force u32)csum;
  22. sum += (sum >> 16) | (sum << 16);
  23. return ~(__force __sum16)(sum >> 16);
  24. }
  25. #define csum_fold csum_fold
  26. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  27. {
  28. __uint128_t tmp;
  29. u64 sum;
  30. tmp = *(const __uint128_t *)iph;
  31. iph += 16;
  32. ihl -= 4;
  33. tmp += ((tmp >> 64) | (tmp << 64));
  34. sum = tmp >> 64;
  35. do {
  36. sum += *(const u32 *)iph;
  37. iph += 4;
  38. } while (--ihl);
  39. sum += ((sum >> 32) | (sum << 32));
  40. return csum_fold(sum >> 32);
  41. }
  42. #define ip_fast_csum ip_fast_csum
  43. #include <asm-generic/checksum.h>
  44. #endif /* __ASM_CHECKSUM_H */