[PATCH RFC bpf-next 49/52] libbpf: add LE <--> CPU conversion helpers

Alexander Lobakin
2022-06-28T19:53:49Z

From: Larysa Zaremba <larysa.zaremba@intel.com>

XDP Generic metadata structure has fields of the explicit
Endianness, all 16, 32 and 64-bit wide.
To make it easier to access them, define __le{16,32,64} <--> cpu
helpers the same way it's done for the BEs.

Signed-off-by: Larysa Zaremba <larysa.zaremba@intel.com>
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
---
tools/lib/bpf/bpf_endian.h | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)

diff --git a/tools/lib/bpf/bpf_endian.h b/tools/lib/bpf/bpf_endian.h
index b03db6aa3f14..35941e6f1d99 100644
--- a/tools/lib/bpf/bpf_endian.h
+++ b/tools/lib/bpf/bpf_endian.h
@@ -60,6 +60,18 @@
# define __bpf_cpu_to_be64(x) __builtin_bswap64(x)
# define __bpf_constant_be64_to_cpu(x) ___bpf_swab64(x)
# define __bpf_constant_cpu_to_be64(x) ___bpf_swab64(x)
+# define __bpf_le16_to_cpu(x) (x)
+# define __bpf_cpu_to_le16(x) (x)
+# define __bpf_constant_le16_to_cpu(x) (x)
+# define __bpf_constant_cpu_to_le16(x) (x)
+# define __bpf_le32_to_cpu(x) (x)
+# define __bpf_cpu_to_le32(x) (x)
+# define __bpf_constant_le32_to_cpu(x) (x)
+# define __bpf_constant_cpu_to_le32(x) (x)
+# define __bpf_le64_to_cpu(x) (x)
+# define __bpf_cpu_to_le64(x) (x)
+# define __bpf_constant_le64_to_cpu(x) (x)
+# define __bpf_constant_cpu_to_le64(x) (x)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define __bpf_ntohs(x) (x)
# define __bpf_htons(x) (x)
@@ -73,6 +85,18 @@
# define __bpf_cpu_to_be64(x) (x)
# define __bpf_constant_be64_to_cpu(x) (x)
# define __bpf_constant_cpu_to_be64(x) (x)
+# define __bpf_le16_to_cpu(x) __builtin_bswap16(x)
+# define __bpf_cpu_to_le16(x) __builtin_bswap16(x)
+# define __bpf_constant_le16_to_cpu(x) ___bpf_swab16(x)
+# define __bpf_constant_cpu_to_le16(x) ___bpf_swab16(x)
+# define __bpf_le32_to_cpu(x) __builtin_bswap32(x)
+# define __bpf_cpu_to_le32(x) __builtin_bswap32(x)
+# define __bpf_constant_le32_to_cpu(x) ___bpf_swab32(x)
+# define __bpf_constant_cpu_to_le32(x) ___bpf_swab32(x)
+# define __bpf_le64_to_cpu(x) __builtin_bswap64(x)
+# define __bpf_cpu_to_le64(x) __builtin_bswap64(x)
+# define __bpf_constant_le64_to_cpu(x) ___bpf_swab64(x)
+# define __bpf_constant_cpu_to_le64(x) ___bpf_swab64(x)
#else
# error "Fix your compiler's __BYTE_ORDER__?!"
#endif
@@ -87,5 +111,11 @@
#define bpf_ntohl(x) __bpf_endop(ntohl, x)
#define bpf_cpu_to_be64(x) __bpf_endop(cpu_to_be64, x)
#define bpf_be64_to_cpu(x) __bpf_endop(be64_to_cpu, x)
+#define bpf_cpu_to_le16(x) __bpf_endop(cpu_to_le16, x)
+#define bpf_le16_to_cpu(x) __bpf_endop(le16_to_cpu, x)
+#define bpf_cpu_to_le32(x) __bpf_endop(cpu_to_le32, x)
+#define bpf_le32_to_cpu(x) __bpf_endop(le32_to_cpu, x)
+#define bpf_cpu_to_le64(x) __bpf_endop(cpu_to_le64, x)
+#define bpf_le64_to_cpu(x) __bpf_endop(le64_to_cpu, x)

#endif /* __BPF_ENDIAN__ */
--
2.36.1