]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
byteorder: use gcc intrinsics for byteswap
authorKefu Chai <kchai@redhat.com>
Tue, 9 May 2017 06:41:41 +0000 (14:41 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 10 May 2017 15:12:30 +0000 (23:12 +0800)
* 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 <kchai@redhat.com>
src/auth/cephx/CephxProtocol.cc
src/auth/cephx/CephxProtocol.h
src/auth/cephx/CephxSessionHandler.cc
src/include/byteorder.h
src/os/kv.h
src/rbd_replay/ActionTypes.cc
src/tools/rbd_nbd/rbd-nbd.cc

index ae61d09dde9add46d5902e66999b23b4aae1c7eb..5836a33bd531e7a31e662d6d93a5098150a4625b 100644 (file)
@@ -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;
 }
 
index ebb25dc9be75e72ebcf0ca8e68400c7009791f50..c82206989906155ba8400187ee87535dbc2ff627 100644 (file)
@@ -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 <typename T>
 void decode_decrypt_enc_bl(CephContext *cct, T& t, CryptoKey key, bufferlist& bl_enc, 
index 943b80afbd3ca6120d5f131c760183821fad2f0c..087d6c54aa2eb07d25815efd702834ce0a3c5453 100644 (file)
@@ -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<uint32_t>(4*4),
+    mswab<uint32_t>(header.crc), mswab<uint32_t>(footer.front_crc),
+    mswab<uint32_t>(footer.middle_crc), mswab<uint32_t>(footer.data_crc)
   };
   bufferlist bl_plaintext;
   bl_plaintext.append(buffer::create_static(sizeof(sigblock), (char*)&sigblock));
index a2262c01bde2d35446607370c019726093c67acd..bbc8a2ef444393603b290525bfb945cd134a0b6a 100644 (file)
@@ -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 <type_traits>
 #include "acconfig.h"
 #include "int_types.h"
 
 
-static __inline__ __u16 swab16(__u16 val) 
-{
+#ifdef __GNUC__
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint16_t), T>::type
+swab(T val) {
+  return __builtin_bswap16(val);
+}
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint32_t), T>::type
+swab(T val) {
+  return __builtin_bswap32(val);
+}
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint64_t), T>::type
+swab(T val) {
+  return __builtin_bswap64(val);
+}
+#else
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint16_t), T>::type
+swab(T val) {
   return (val >> 8) | (val << 8);
 }
-static __inline__ __u32 swab32(__u32 val) 
-{
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint32_t), T>::type
+swab(T val) {
   return (( val >> 24) |
          ((val >> 8)  & 0xff00) |
          ((val << 8)  & 0xff0000) | 
          ((val << 24)));
 }
-static __inline__ uint64_t swab64(uint64_t val) 
-{
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint64_t), T>::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<typename T>
+inline T mswab(T val) {
+  return swab(val);
+}
 #else
-# error "Could not determine endianess"
+template<typename T>
+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
index c3d5bd13a71059e184e45625e5cb9a397347deaf..64048b088e250f01d839549515c372738546bd20 100644 (file)
@@ -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
index 4e7a297a6c7cd6f0b28d49bc384d56780e772dce..eed19fcec1ae370e71a19c36587cf9ad4d93bdc7 100644 (file)
@@ -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);
   }
 }
 
index f886e5f4f4513d9511a0cd9da721ad891361653f..a8e4bf60395836b30f301cc1378d62ca839a416c 100644 (file)
@@ -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