bpf_endian.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. #ifndef __BPF_ENDIAN__
  3. #define __BPF_ENDIAN__
  4. /*
  5. * Isolate byte #n and put it into byte #m, for __u##b type.
  6. * E.g., moving byte #6 (nnnnnnnn) into byte #1 (mmmmmmmm) for __u64:
  7. * 1) xxxxxxxx nnnnnnnn xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx mmmmmmmm xxxxxxxx
  8. * 2) nnnnnnnn xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx mmmmmmmm xxxxxxxx 00000000
  9. * 3) 00000000 00000000 00000000 00000000 00000000 00000000 00000000 nnnnnnnn
  10. * 4) 00000000 00000000 00000000 00000000 00000000 00000000 nnnnnnnn 00000000
  11. */
  12. #define ___bpf_mvb(x, b, n, m) ((__u##b)(x) << (b-(n+1)*8) >> (b-8) << (m*8))
  13. #define ___bpf_swab16(x) ((__u16)( \
  14. ___bpf_mvb(x, 16, 0, 1) | \
  15. ___bpf_mvb(x, 16, 1, 0)))
  16. #define ___bpf_swab32(x) ((__u32)( \
  17. ___bpf_mvb(x, 32, 0, 3) | \
  18. ___bpf_mvb(x, 32, 1, 2) | \
  19. ___bpf_mvb(x, 32, 2, 1) | \
  20. ___bpf_mvb(x, 32, 3, 0)))
  21. #define ___bpf_swab64(x) ((__u64)( \
  22. ___bpf_mvb(x, 64, 0, 7) | \
  23. ___bpf_mvb(x, 64, 1, 6) | \
  24. ___bpf_mvb(x, 64, 2, 5) | \
  25. ___bpf_mvb(x, 64, 3, 4) | \
  26. ___bpf_mvb(x, 64, 4, 3) | \
  27. ___bpf_mvb(x, 64, 5, 2) | \
  28. ___bpf_mvb(x, 64, 6, 1) | \
  29. ___bpf_mvb(x, 64, 7, 0)))
  30. /* LLVM's BPF target selects the endianness of the CPU
  31. * it compiles on, or the user specifies (bpfel/bpfeb),
  32. * respectively. The used __BYTE_ORDER__ is defined by
  33. * the compiler, we cannot rely on __BYTE_ORDER from
  34. * libc headers, since it doesn't reflect the actual
  35. * requested byte order.
  36. *
  37. * Note, LLVM's BPF target has different __builtin_bswapX()
  38. * semantics. It does map to BPF_ALU | BPF_END | BPF_TO_BE
  39. * in bpfel and bpfeb case, which means below, that we map
  40. * to cpu_to_be16(). We could use it unconditionally in BPF
  41. * case, but better not rely on it, so that this header here
  42. * can be used from application and BPF program side, which
  43. * use different targets.
  44. */
  45. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  46. # define __bpf_ntohs(x) __builtin_bswap16(x)
  47. # define __bpf_htons(x) __builtin_bswap16(x)
  48. # define __bpf_constant_ntohs(x) ___bpf_swab16(x)
  49. # define __bpf_constant_htons(x) ___bpf_swab16(x)
  50. # define __bpf_ntohl(x) __builtin_bswap32(x)
  51. # define __bpf_htonl(x) __builtin_bswap32(x)
  52. # define __bpf_constant_ntohl(x) ___bpf_swab32(x)
  53. # define __bpf_constant_htonl(x) ___bpf_swab32(x)
  54. # define __bpf_be64_to_cpu(x) __builtin_bswap64(x)
  55. # define __bpf_cpu_to_be64(x) __builtin_bswap64(x)
  56. # define __bpf_constant_be64_to_cpu(x) ___bpf_swab64(x)
  57. # define __bpf_constant_cpu_to_be64(x) ___bpf_swab64(x)
  58. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  59. # define __bpf_ntohs(x) (x)
  60. # define __bpf_htons(x) (x)
  61. # define __bpf_constant_ntohs(x) (x)
  62. # define __bpf_constant_htons(x) (x)
  63. # define __bpf_ntohl(x) (x)
  64. # define __bpf_htonl(x) (x)
  65. # define __bpf_constant_ntohl(x) (x)
  66. # define __bpf_constant_htonl(x) (x)
  67. # define __bpf_be64_to_cpu(x) (x)
  68. # define __bpf_cpu_to_be64(x) (x)
  69. # define __bpf_constant_be64_to_cpu(x) (x)
  70. # define __bpf_constant_cpu_to_be64(x) (x)
  71. #else
  72. # error "Fix your compiler's __BYTE_ORDER__?!"
  73. #endif
  74. #define bpf_htons(x) \
  75. (__builtin_constant_p(x) ? \
  76. __bpf_constant_htons(x) : __bpf_htons(x))
  77. #define bpf_ntohs(x) \
  78. (__builtin_constant_p(x) ? \
  79. __bpf_constant_ntohs(x) : __bpf_ntohs(x))
  80. #define bpf_htonl(x) \
  81. (__builtin_constant_p(x) ? \
  82. __bpf_constant_htonl(x) : __bpf_htonl(x))
  83. #define bpf_ntohl(x) \
  84. (__builtin_constant_p(x) ? \
  85. __bpf_constant_ntohl(x) : __bpf_ntohl(x))
  86. #define bpf_cpu_to_be64(x) \
  87. (__builtin_constant_p(x) ? \
  88. __bpf_constant_cpu_to_be64(x) : __bpf_cpu_to_be64(x))
  89. #define bpf_be64_to_cpu(x) \
  90. (__builtin_constant_p(x) ? \
  91. __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x))
  92. #endif /* __BPF_ENDIAN__ */