162306a36Sopenharmony_ci/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 262306a36Sopenharmony_ci#ifndef __BPF_ENDIAN__ 362306a36Sopenharmony_ci#define __BPF_ENDIAN__ 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci/* 662306a36Sopenharmony_ci * Isolate byte #n and put it into byte #m, for __u##b type. 762306a36Sopenharmony_ci * E.g., moving byte #6 (nnnnnnnn) into byte #1 (mmmmmmmm) for __u64: 862306a36Sopenharmony_ci * 1) xxxxxxxx nnnnnnnn xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx mmmmmmmm xxxxxxxx 962306a36Sopenharmony_ci * 2) nnnnnnnn xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx mmmmmmmm xxxxxxxx 00000000 1062306a36Sopenharmony_ci * 3) 00000000 00000000 00000000 00000000 00000000 00000000 00000000 nnnnnnnn 1162306a36Sopenharmony_ci * 4) 00000000 00000000 00000000 00000000 00000000 00000000 nnnnnnnn 00000000 1262306a36Sopenharmony_ci */ 1362306a36Sopenharmony_ci#define ___bpf_mvb(x, b, n, m) ((__u##b)(x) << (b-(n+1)*8) >> (b-8) << (m*8)) 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#define ___bpf_swab16(x) ((__u16)( \ 1662306a36Sopenharmony_ci ___bpf_mvb(x, 16, 0, 1) | \ 1762306a36Sopenharmony_ci ___bpf_mvb(x, 16, 1, 0))) 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#define ___bpf_swab32(x) ((__u32)( \ 2062306a36Sopenharmony_ci ___bpf_mvb(x, 32, 0, 3) | \ 2162306a36Sopenharmony_ci ___bpf_mvb(x, 32, 1, 2) | \ 2262306a36Sopenharmony_ci ___bpf_mvb(x, 32, 2, 1) | \ 2362306a36Sopenharmony_ci ___bpf_mvb(x, 32, 3, 0))) 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#define ___bpf_swab64(x) ((__u64)( \ 2662306a36Sopenharmony_ci ___bpf_mvb(x, 64, 0, 7) | \ 2762306a36Sopenharmony_ci ___bpf_mvb(x, 64, 1, 6) | \ 2862306a36Sopenharmony_ci ___bpf_mvb(x, 64, 2, 5) | \ 2962306a36Sopenharmony_ci ___bpf_mvb(x, 64, 3, 4) | \ 3062306a36Sopenharmony_ci ___bpf_mvb(x, 64, 4, 3) | \ 3162306a36Sopenharmony_ci ___bpf_mvb(x, 64, 5, 2) | \ 3262306a36Sopenharmony_ci ___bpf_mvb(x, 64, 6, 1) | \ 3362306a36Sopenharmony_ci ___bpf_mvb(x, 64, 7, 0))) 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci/* LLVM's BPF target selects the endianness of the CPU 3662306a36Sopenharmony_ci * it compiles on, or the user specifies (bpfel/bpfeb), 3762306a36Sopenharmony_ci * respectively. The used __BYTE_ORDER__ is defined by 3862306a36Sopenharmony_ci * the compiler, we cannot rely on __BYTE_ORDER from 3962306a36Sopenharmony_ci * libc headers, since it doesn't reflect the actual 4062306a36Sopenharmony_ci * requested byte order. 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * Note, LLVM's BPF target has different __builtin_bswapX() 4362306a36Sopenharmony_ci * semantics. It does map to BPF_ALU | BPF_END | BPF_TO_BE 4462306a36Sopenharmony_ci * in bpfel and bpfeb case, which means below, that we map 4562306a36Sopenharmony_ci * to cpu_to_be16(). We could use it unconditionally in BPF 4662306a36Sopenharmony_ci * case, but better not rely on it, so that this header here 4762306a36Sopenharmony_ci * can be used from application and BPF program side, which 4862306a36Sopenharmony_ci * use different targets. 4962306a36Sopenharmony_ci */ 5062306a36Sopenharmony_ci#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 5162306a36Sopenharmony_ci# define __bpf_ntohs(x) __builtin_bswap16(x) 5262306a36Sopenharmony_ci# define __bpf_htons(x) __builtin_bswap16(x) 5362306a36Sopenharmony_ci# define __bpf_constant_ntohs(x) ___bpf_swab16(x) 5462306a36Sopenharmony_ci# define __bpf_constant_htons(x) ___bpf_swab16(x) 5562306a36Sopenharmony_ci# define __bpf_ntohl(x) __builtin_bswap32(x) 5662306a36Sopenharmony_ci# define __bpf_htonl(x) __builtin_bswap32(x) 5762306a36Sopenharmony_ci# define __bpf_constant_ntohl(x) ___bpf_swab32(x) 5862306a36Sopenharmony_ci# define __bpf_constant_htonl(x) ___bpf_swab32(x) 5962306a36Sopenharmony_ci# define __bpf_be64_to_cpu(x) __builtin_bswap64(x) 6062306a36Sopenharmony_ci# define __bpf_cpu_to_be64(x) __builtin_bswap64(x) 6162306a36Sopenharmony_ci# define __bpf_constant_be64_to_cpu(x) ___bpf_swab64(x) 6262306a36Sopenharmony_ci# define __bpf_constant_cpu_to_be64(x) ___bpf_swab64(x) 6362306a36Sopenharmony_ci#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 6462306a36Sopenharmony_ci# define __bpf_ntohs(x) (x) 6562306a36Sopenharmony_ci# define __bpf_htons(x) (x) 6662306a36Sopenharmony_ci# define __bpf_constant_ntohs(x) (x) 6762306a36Sopenharmony_ci# define __bpf_constant_htons(x) (x) 6862306a36Sopenharmony_ci# define __bpf_ntohl(x) (x) 6962306a36Sopenharmony_ci# define __bpf_htonl(x) (x) 7062306a36Sopenharmony_ci# define __bpf_constant_ntohl(x) (x) 7162306a36Sopenharmony_ci# define __bpf_constant_htonl(x) (x) 7262306a36Sopenharmony_ci# define __bpf_be64_to_cpu(x) (x) 7362306a36Sopenharmony_ci# define __bpf_cpu_to_be64(x) (x) 7462306a36Sopenharmony_ci# define __bpf_constant_be64_to_cpu(x) (x) 7562306a36Sopenharmony_ci# define __bpf_constant_cpu_to_be64(x) (x) 7662306a36Sopenharmony_ci#else 7762306a36Sopenharmony_ci# error "Fix your compiler's __BYTE_ORDER__?!" 7862306a36Sopenharmony_ci#endif 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci#define bpf_htons(x) \ 8162306a36Sopenharmony_ci (__builtin_constant_p(x) ? \ 8262306a36Sopenharmony_ci __bpf_constant_htons(x) : __bpf_htons(x)) 8362306a36Sopenharmony_ci#define bpf_ntohs(x) \ 8462306a36Sopenharmony_ci (__builtin_constant_p(x) ? \ 8562306a36Sopenharmony_ci __bpf_constant_ntohs(x) : __bpf_ntohs(x)) 8662306a36Sopenharmony_ci#define bpf_htonl(x) \ 8762306a36Sopenharmony_ci (__builtin_constant_p(x) ? \ 8862306a36Sopenharmony_ci __bpf_constant_htonl(x) : __bpf_htonl(x)) 8962306a36Sopenharmony_ci#define bpf_ntohl(x) \ 9062306a36Sopenharmony_ci (__builtin_constant_p(x) ? \ 9162306a36Sopenharmony_ci __bpf_constant_ntohl(x) : __bpf_ntohl(x)) 9262306a36Sopenharmony_ci#define bpf_cpu_to_be64(x) \ 9362306a36Sopenharmony_ci (__builtin_constant_p(x) ? \ 9462306a36Sopenharmony_ci __bpf_constant_cpu_to_be64(x) : __bpf_cpu_to_be64(x)) 9562306a36Sopenharmony_ci#define bpf_be64_to_cpu(x) \ 9662306a36Sopenharmony_ci (__builtin_constant_p(x) ? \ 9762306a36Sopenharmony_ci __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x)) 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci#endif /* __BPF_ENDIAN__ */ 100