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