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