1
2 #ifndef __BPF_ENDIAN__
3 #define __BPF_ENDIAN__
4
5 #include <linux/stddef.h>
6 #include <linux/swab.h>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
24 # define __bpf_ntohs(x) __builtin_bswap16(x)
25 # define __bpf_htons(x) __builtin_bswap16(x)
26 # define __bpf_constant_ntohs(x) ___constant_swab16(x)
27 # define __bpf_constant_htons(x) ___constant_swab16(x)
28 # define __bpf_ntohl(x) __builtin_bswap32(x)
29 # define __bpf_htonl(x) __builtin_bswap32(x)
30 # define __bpf_constant_ntohl(x) ___constant_swab32(x)
31 # define __bpf_constant_htonl(x) ___constant_swab32(x)
32 # define __bpf_be64_to_cpu(x) __builtin_bswap64(x)
33 # define __bpf_cpu_to_be64(x) __builtin_bswap64(x)
34 # define __bpf_constant_be64_to_cpu(x) ___constant_swab64(x)
35 # define __bpf_constant_cpu_to_be64(x) ___constant_swab64(x)
36 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
37 # define __bpf_ntohs(x) (x)
38 # define __bpf_htons(x) (x)
39 # define __bpf_constant_ntohs(x) (x)
40 # define __bpf_constant_htons(x) (x)
41 # define __bpf_ntohl(x) (x)
42 # define __bpf_htonl(x) (x)
43 # define __bpf_constant_ntohl(x) (x)
44 # define __bpf_constant_htonl(x) (x)
45 # define __bpf_be64_to_cpu(x) (x)
46 # define __bpf_cpu_to_be64(x) (x)
47 # define __bpf_constant_be64_to_cpu(x) (x)
48 # define __bpf_constant_cpu_to_be64(x) (x)
49 #else
50 # error "Fix your compiler's __BYTE_ORDER__?!"
51 #endif
52
53 #define bpf_htons(x) \
54 (__builtin_constant_p(x) ? \
55 __bpf_constant_htons(x) : __bpf_htons(x))
56 #define bpf_ntohs(x) \
57 (__builtin_constant_p(x) ? \
58 __bpf_constant_ntohs(x) : __bpf_ntohs(x))
59 #define bpf_htonl(x) \
60 (__builtin_constant_p(x) ? \
61 __bpf_constant_htonl(x) : __bpf_htonl(x))
62 #define bpf_ntohl(x) \
63 (__builtin_constant_p(x) ? \
64 __bpf_constant_ntohl(x) : __bpf_ntohl(x))
65 #define bpf_cpu_to_be64(x) \
66 (__builtin_constant_p(x) ? \
67 __bpf_constant_cpu_to_be64(x) : __bpf_cpu_to_be64(x))
68 #define bpf_be64_to_cpu(x) \
69 (__builtin_constant_p(x) ? \
70 __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x))
71
72 #endif