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