From b7a194eb6536e536b116b193dae929bc3a8ec8bc Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Tue, 9 May 2017 14:41:41 +0800 Subject: [PATCH] byteorder: use gcc intrinsics for byteswap * use gcc intrinsics for byteswap * use template to wrap them. * add the modeline for emacs/vim * update the caller of the mswab/swab accordingly Signed-off-by: Kefu Chai --- src/auth/cephx/CephxProtocol.cc | 2 +- src/auth/cephx/CephxProtocol.h | 2 +- src/auth/cephx/CephxSessionHandler.cc | 6 +- src/include/byteorder.h | 85 ++++++++++++++++----------- src/os/kv.h | 10 ++-- src/rbd_replay/ActionTypes.cc | 18 +++--- src/tools/rbd_nbd/rbd-nbd.cc | 2 +- 7 files changed, 71 insertions(+), 54 deletions(-) diff --git a/src/auth/cephx/CephxProtocol.cc b/src/auth/cephx/CephxProtocol.cc index ae61d09dde9..5836a33bd53 100644 --- a/src/auth/cephx/CephxProtocol.cc +++ b/src/auth/cephx/CephxProtocol.cc @@ -38,7 +38,7 @@ void cephx_calc_client_server_challenge(CephContext *cct, CryptoKey& secret, uin uint64_t k = 0; const uint64_t *p = (const uint64_t *)enc.c_str(); for (int pos = 0; pos + sizeof(k) <= enc.length(); pos+=sizeof(k), p++) - k ^= mswab64(*p); + k ^= mswab(*p); *key = k; } diff --git a/src/auth/cephx/CephxProtocol.h b/src/auth/cephx/CephxProtocol.h index ebb25dc9be7..c8220698990 100644 --- a/src/auth/cephx/CephxProtocol.h +++ b/src/auth/cephx/CephxProtocol.h @@ -421,7 +421,7 @@ extern bool cephx_verify_authorizer(CephContext *cct, KeyStore *keys, /* * encode+encrypt macros */ -#define AUTH_ENC_MAGIC 0xff009cad8826aa55ull +static constexpr uint64_t AUTH_ENC_MAGIC = 0xff009cad8826aa55ull; template void decode_decrypt_enc_bl(CephContext *cct, T& t, CryptoKey key, bufferlist& bl_enc, diff --git a/src/auth/cephx/CephxSessionHandler.cc b/src/auth/cephx/CephxSessionHandler.cc index 943b80afbd3..087d6c54aa2 100644 --- a/src/auth/cephx/CephxSessionHandler.cc +++ b/src/auth/cephx/CephxSessionHandler.cc @@ -41,9 +41,9 @@ int CephxSessionHandler::_calc_signature(Message *m, uint64_t *psig) __le32 middle_crc; __le32 data_crc; } __attribute__ ((packed)) sigblock = { - 1, mswab64(AUTH_ENC_MAGIC), mswab32(4*4), - mswab32(header.crc), mswab32(footer.front_crc), - mswab32(footer.middle_crc), mswab32(footer.data_crc) + 1, mswab(AUTH_ENC_MAGIC), mswab(4*4), + mswab(header.crc), mswab(footer.front_crc), + mswab(footer.middle_crc), mswab(footer.data_crc) }; bufferlist bl_plaintext; bl_plaintext.append(buffer::create_static(sizeof(sigblock), (char*)&sigblock)); diff --git a/src/include/byteorder.h b/src/include/byteorder.h index a2262c01bde..bbc8a2ef444 100644 --- a/src/include/byteorder.h +++ b/src/include/byteorder.h @@ -1,29 +1,45 @@ -/* - * byteorder.h - * - * LGPL 2 - */ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- -#ifndef CEPH_BYTEORDER_H -#define CEPH_BYTEORDER_H +#pragma once +#include #include "acconfig.h" #include "int_types.h" -static __inline__ __u16 swab16(__u16 val) -{ +#ifdef __GNUC__ +template +inline typename std::enable_if::type +swab(T val) { + return __builtin_bswap16(val); +} +template +inline typename std::enable_if::type +swab(T val) { + return __builtin_bswap32(val); +} +template +inline typename std::enable_if::type +swab(T val) { + return __builtin_bswap64(val); +} +#else +template +inline typename std::enable_if::type +swab(T val) { return (val >> 8) | (val << 8); } -static __inline__ __u32 swab32(__u32 val) -{ +template +inline typename std::enable_if::type +swab(T val) { return (( val >> 24) | ((val >> 8) & 0xff00) | ((val << 8) & 0xff0000) | ((val << 24))); } -static __inline__ uint64_t swab64(uint64_t val) -{ +template +inline typename std::enable_if::type +swab(T val) { return (( val >> 56) | ((val >> 40) & 0xff00ull) | ((val >> 24) & 0xff0000ull) | @@ -33,32 +49,31 @@ static __inline__ uint64_t swab64(uint64_t val) ((val << 40) & 0xff000000000000ull) | ((val << 56))); } +#endif // mswab == maybe swab (if not LE) #ifdef CEPH_BIG_ENDIAN -# define mswab64(a) swab64(a) -# define mswab32(a) swab32(a) -# define mswab16(a) swab16(a) -#elif defined(CEPH_LITTLE_ENDIAN) -# define mswab64(a) (a) -# define mswab32(a) (a) -# define mswab16(a) (a) +template +inline T mswab(T val) { + return swab(val); +} #else -# error "Could not determine endianess" +template +inline T mswab(T val) { + return val; +} #endif -#ifdef __cplusplus - #define MAKE_LE_CLASS(bits) \ - struct ceph_le##bits { \ + struct ceph_le##bits { \ __u##bits v; \ ceph_le##bits &operator=(__u##bits nv) { \ - v = mswab##bits(nv); \ + v = mswab(nv); \ return *this; \ } \ - operator __u##bits() const { return mswab##bits(v); } \ + operator __u##bits() const { return mswab(v); } \ } __attribute__ ((packed)); \ - static inline bool operator==(ceph_le##bits a, ceph_le##bits b) { \ + static inline bool operator==(ceph_le##bits a, ceph_le##bits b) { \ return a.v == b.v; \ } @@ -67,11 +82,15 @@ MAKE_LE_CLASS(32) MAKE_LE_CLASS(16) #undef MAKE_LE_CLASS -#endif /* __cplusplus */ - -#define init_le64(x) { (__u64)mswab64(x) } -#define init_le32(x) { (__u32)mswab32(x) } -#define init_le16(x) { (__u16)mswab16(x) } +inline __u64 init_le64(__u64 x) { + return mswab<__u64>(x); +} +inline __u32 init_le32(__u32 x) { + return mswab<__u32>(x); +} +inline __u16 init_le16(__u16 x) { + return mswab<__u16>(x); +} /* #define cpu_to_le64(x) (x) @@ -81,5 +100,3 @@ MAKE_LE_CLASS(16) #define le64_to_cpu(x) ((uint64_t)x) #define le32_to_cpu(x) ((__u32)x) #define le16_to_cpu(x) ((__u16)x) - -#endif diff --git a/src/os/kv.h b/src/os/kv.h index c3d5bd13a71..64048b088e2 100644 --- a/src/os/kv.h +++ b/src/os/kv.h @@ -14,7 +14,7 @@ inline static void _key_encode_u32(uint32_t u, T *key) { #ifdef CEPH_BIG_ENDIAN bu = u; #elif defined(CEPH_LITTLE_ENDIAN) - bu = swab32(u); + bu = swab(u); #else # error wtf #endif @@ -27,7 +27,7 @@ inline static void _key_encode_u32(uint32_t u, size_t pos, T *key) { #ifdef CEPH_BIG_ENDIAN bu = u; #elif defined(CEPH_LITTLE_ENDIAN) - bu = swab32(u); + bu = swab(u); #else # error wtf #endif @@ -40,7 +40,7 @@ inline static const char *_key_decode_u32(const char *key, uint32_t *pu) { #ifdef CEPH_BIG_ENDIAN *pu = bu; #elif defined(CEPH_LITTLE_ENDIAN) - *pu = swab32(bu); + *pu = swab(bu); #else # error wtf #endif @@ -53,7 +53,7 @@ inline static void _key_encode_u64(uint64_t u, T *key) { #ifdef CEPH_BIG_ENDIAN bu = u; #elif defined(CEPH_LITTLE_ENDIAN) - bu = swab64(u); + bu = swab(u); #else # error wtf #endif @@ -66,7 +66,7 @@ inline static const char *_key_decode_u64(const char *key, uint64_t *pu) { #ifdef CEPH_BIG_ENDIAN *pu = bu; #elif defined(CEPH_LITTLE_ENDIAN) - *pu = swab64(bu); + *pu = swab(bu); #else # error wtf #endif diff --git a/src/rbd_replay/ActionTypes.cc b/src/rbd_replay/ActionTypes.cc index 4e7a297a6c7..eed19fcec1a 100644 --- a/src/rbd_replay/ActionTypes.cc +++ b/src/rbd_replay/ActionTypes.cc @@ -26,7 +26,7 @@ void decode_big_endian_string(std::string &str, bufferlist::iterator &it) { #if defined(CEPH_LITTLE_ENDIAN) uint32_t length; ::decode(length, it); - length = swab32(length); + length = swab(length); str.clear(); it.copy(length, str); #else @@ -92,8 +92,8 @@ void Dependency::decode(__u8 version, bufferlist::iterator &it) { ::decode(id, it); ::decode(time_delta, it); if (byte_swap_required(version)) { - id = swab32(id); - time_delta = swab64(time_delta); + id = swab(id); + time_delta = swab(time_delta); } } @@ -125,12 +125,12 @@ void ActionBase::decode(__u8 version, bufferlist::iterator &it) { } if (byte_swap_required(version)) { - id = swab32(id); - thread_id = swab64(thread_id); + id = swab(id); + thread_id = swab(thread_id); uint32_t dep_count; ::decode(dep_count, it); - dep_count = swab32(dep_count); + dep_count = swab(dep_count); dependencies.resize(dep_count); for (uint32_t i = 0; i < dep_count; ++i) { dependencies[i].decode(0, it); @@ -161,7 +161,7 @@ void ImageActionBase::decode(__u8 version, bufferlist::iterator &it) { ActionBase::decode(version, it); ::decode(imagectx_id, it); if (byte_swap_required(version)) { - imagectx_id = swab64(imagectx_id); + imagectx_id = swab(imagectx_id); } } @@ -181,8 +181,8 @@ void IoActionBase::decode(__u8 version, bufferlist::iterator &it) { ::decode(offset, it); ::decode(length, it); if (byte_swap_required(version)) { - offset = swab64(offset); - length = swab64(length); + offset = swab(offset); + length = swab(length); } } diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index f886e5f4f45..a8e4bf60395 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -86,7 +86,7 @@ static int nbd = -1; #ifdef CEPH_BIG_ENDIAN #define ntohll(a) (a) #elif defined(CEPH_LITTLE_ENDIAN) -#define ntohll(a) swab64(a) +#define ntohll(a) swab(a) #else #error "Could not determine endianess" #endif -- 2.39.5