bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCHv2 bpf-next 1/3] bpf, tests: tweak endianness selection
@ 2019-03-20 12:53 Sergey Senozhatsky
  2019-03-20 12:53 ` [PATCHv2 bpf-next 2/3] bpf, tests: drop unused __bpf_constant_foo defines Sergey Senozhatsky
                   ` (3 more replies)
  0 siblings, 4 replies; 19+ messages in thread
From: Sergey Senozhatsky @ 2019-03-20 12:53 UTC (permalink / raw)
  To: Shuah Khan, Alexei Starovoitov
  Cc: Stanislav Fomichev, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, linux-kselftest, netdev, bpf, Sergey Senozhatsky

Not all compilers have __builtin_bswap16() and __builtin_bswap32(),
thus not all compilers are able to compile the following code:

        (__builtin_constant_p(x) ? \
                ___constant_swab16(x) : __builtin_bswap16(x))

That's the reason why bpf_ntohl() doesn't work on GCC < 4.8, for
instance:

        error: implicit declaration of function '__builtin_bswap16'

We can use __builtin_bswap16() only if compiler has this built-in,
that is, only if __HAVE_BUILTIN_BSWAP16__ is defined. Standard UAPI
__swab16()/__swab32() take care of that, and, additionally, handle
__builtin_constant_p() cases as well:

 #ifdef __HAVE_BUILTIN_BSWAP16__
 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
 #else
 #define __swab16(x)                             \
         (__builtin_constant_p((__u16)(x)) ?     \
         ___constant_swab16(x) :                 \
         __fswab16(x))
 #endif

So we can tweak selftests/bpf/bpf_endian.h and use UAPI
__swab16()/__swab32().

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---

v2: fixed build error, reshuffled patches (Stanislav Fomichev)

 tools/testing/selftests/bpf/bpf_endian.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/bpf_endian.h b/tools/testing/selftests/bpf/bpf_endian.h
index b25595ea4a78..1ed268b2002b 100644
--- a/tools/testing/selftests/bpf/bpf_endian.h
+++ b/tools/testing/selftests/bpf/bpf_endian.h
@@ -20,12 +20,12 @@
  * use different targets.
  */
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-# define __bpf_ntohs(x)			__builtin_bswap16(x)
-# define __bpf_htons(x)			__builtin_bswap16(x)
+# define __bpf_ntohs(x)			__swab16(x)
+# define __bpf_htons(x)			__swab16(x)
 # define __bpf_constant_ntohs(x)	___constant_swab16(x)
 # define __bpf_constant_htons(x)	___constant_swab16(x)
-# define __bpf_ntohl(x)			__builtin_bswap32(x)
-# define __bpf_htonl(x)			__builtin_bswap32(x)
+# define __bpf_ntohl(x)			__swab32(x)
+# define __bpf_htonl(x)			__swab32(x)
 # define __bpf_constant_ntohl(x)	___constant_swab32(x)
 # define __bpf_constant_htonl(x)	___constant_swab32(x)
 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2019-03-22  3:12 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-20 12:53 [PATCHv2 bpf-next 1/3] bpf, tests: tweak endianness selection Sergey Senozhatsky
2019-03-20 12:53 ` [PATCHv2 bpf-next 2/3] bpf, tests: drop unused __bpf_constant_foo defines Sergey Senozhatsky
2019-03-20 12:56   ` Sergey Senozhatsky
2019-03-20 12:53 ` [PATCHv2 bpf-next 3/3] bpf, tests: don't use __bpf_constant_htons() Sergey Senozhatsky
2019-03-20 12:55   ` Sergey Senozhatsky
2019-03-20 17:13 ` [PATCHv2 bpf-next 1/3] bpf, tests: tweak endianness selection Stanislav Fomichev
2019-03-20 22:20   ` Yonghong Song
2019-03-20 22:27     ` Stanislav Fomichev
2019-03-20 22:45       ` Yonghong Song
2019-03-20 23:08         ` Daniel Borkmann
2019-03-21  0:03           ` Stanislav Fomichev
2019-03-21  0:23             ` Yonghong Song
2019-03-21  0:34       ` Sergey Senozhatsky
2019-03-21  0:22   ` Sergey Senozhatsky
2019-03-21  3:24 ` Alexei Starovoitov
2019-03-21  5:08   ` Sergey Senozhatsky
2019-03-21 15:49     ` Stanislav Fomichev
2019-03-22  2:46       ` Sergey Senozhatsky
2019-03-22  3:12         ` Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).