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