github.com/cilium/cilium@v1.16.2/bpf/lib/endian.h (about)

     1  /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
     2  /* Copyright Authors of Cilium */
     3  
     4  #pragma once
     5  
     6  #include <bpf/ctx/ctx.h>
     7  #include <bpf/api.h>
     8  
     9  #define ___bpf_mvb(x, b, n, m) ((__u##b)(x) << (b-(n+1)*8) >> (b-8) << (m*8))
    10  
    11  #define ___bpf_swab16(x) ((__u16)(			\
    12  			  ___bpf_mvb(x, 16, 0, 1) |	\
    13  			  ___bpf_mvb(x, 16, 1, 0)))
    14  
    15  #define ___bpf_swab32(x) ((__u32)(			\
    16  			  ___bpf_mvb(x, 32, 0, 3) |	\
    17  			  ___bpf_mvb(x, 32, 1, 2) |	\
    18  			  ___bpf_mvb(x, 32, 2, 1) |	\
    19  			  ___bpf_mvb(x, 32, 3, 0)))
    20  
    21  #define ___bpf_swab64(x) ((__u64)(			\
    22  			  ___bpf_mvb(x, 64, 0, 7) |	\
    23  			  ___bpf_mvb(x, 64, 1, 6) |	\
    24  			  ___bpf_mvb(x, 64, 2, 5) |	\
    25  			  ___bpf_mvb(x, 64, 3, 4) |	\
    26  			  ___bpf_mvb(x, 64, 4, 3) |	\
    27  			  ___bpf_mvb(x, 64, 5, 2) |	\
    28  			  ___bpf_mvb(x, 64, 6, 1) |	\
    29  			  ___bpf_mvb(x, 64, 7, 0)))
    30  
    31  #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
    32  # define __bpf_ntohs(x)			__builtin_bswap16(x)
    33  # define __bpf_htons(x)			__builtin_bswap16(x)
    34  # define __bpf_constant_ntohs(x)	___bpf_swab16(x)
    35  # define __bpf_constant_htons(x)	___bpf_swab16(x)
    36  # define __bpf_ntohl(x)			__builtin_bswap32(x)
    37  # define __bpf_htonl(x)			__builtin_bswap32(x)
    38  # define __bpf_constant_ntohl(x)	___bpf_swab32(x)
    39  # define __bpf_constant_htonl(x)	___bpf_swab32(x)
    40  # define __bpf_be64_to_cpu(x)		__builtin_bswap64(x)
    41  # define __bpf_cpu_to_be64(x)		__builtin_bswap64(x)
    42  # define __bpf_constant_be64_to_cpu(x)	___bpf_swab64(x)
    43  # define __bpf_constant_cpu_to_be64(x)	___bpf_swab64(x)
    44  #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
    45  # define __bpf_ntohs(x)			(x)
    46  # define __bpf_htons(x)			(x)
    47  # define __bpf_constant_ntohs(x)	(x)
    48  # define __bpf_constant_htons(x)	(x)
    49  # define __bpf_ntohl(x)			(x)
    50  # define __bpf_htonl(x)			(x)
    51  # define __bpf_constant_ntohl(x)	(x)
    52  # define __bpf_constant_htonl(x)	(x)
    53  # define __bpf_be64_to_cpu(x)		(x)
    54  # define __bpf_cpu_to_be64(x)		(x)
    55  # define __bpf_constant_be64_to_cpu(x)  (x)
    56  # define __bpf_constant_cpu_to_be64(x)  (x)
    57  #else
    58  # error "Fix your compiler's __BYTE_ORDER__?!"
    59  #endif
    60  
    61  #define bpf_htons(x)				\
    62  	(__builtin_constant_p(x) ?		\
    63  	 __bpf_constant_htons(x) : __bpf_htons(x))
    64  #define bpf_u8_to_be16(x) bpf_htons((__u16)x)
    65  #define bpf_ntohs(x)				\
    66  	(__builtin_constant_p(x) ?		\
    67  	 __bpf_constant_ntohs(x) : __bpf_ntohs(x))
    68  #define bpf_htonl(x)				\
    69  	(__builtin_constant_p(x) ?		\
    70  	 __bpf_constant_htonl(x) : __bpf_htonl(x))
    71  #define bpf_ntohl(x)				\
    72  	(__builtin_constant_p(x) ?		\
    73  	 __bpf_constant_ntohl(x) : __bpf_ntohl(x))
    74  #define bpf_cpu_to_be64(x)			\
    75  	(__builtin_constant_p(x) ?		\
    76  	 __bpf_constant_cpu_to_be64(x) : __bpf_cpu_to_be64(x))
    77  #define bpf_be64_to_cpu(x)			\
    78  	(__builtin_constant_p(x) ?		\
    79  	 __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x))