From: Kefu Chai Date: Tue, 27 Apr 2021 12:26:40 +0000 (+0800) Subject: librbd, tools/rbd_nbd: use boost::endian for endian conversion X-Git-Tag: v17.1.0~2097^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b26a50827d22f43328e6bae764dcd5c5e32ff7be;p=ceph.git librbd, tools/rbd_nbd: use boost::endian for endian conversion be32toh() and friends are available on BSD and from glibc, but they are not part of any standard. let's use boost::endian for endian conversions. Signed-off-by: Kefu Chai --- diff --git a/src/librbd/migration/QCOWFormat.cc b/src/librbd/migration/QCOWFormat.cc index 7bd4a5ef748e..cedf9aa20f02 100644 --- a/src/librbd/migration/QCOWFormat.cc +++ b/src/librbd/migration/QCOWFormat.cc @@ -18,6 +18,7 @@ #include "librbd/migration/Utils.h" #include #include +#include #include #include #include @@ -32,6 +33,8 @@ namespace migration { #define dout_prefix *_dout << "librbd::migration::QCOWFormat: " \ << __func__ << ": " +using boost::endian::big_to_native; + namespace qcow_format { struct ClusterExtent { @@ -69,7 +72,7 @@ void LookupTable::decode() { // translate the lookup table (big-endian -> CPU endianess) for (auto idx = 0UL; idx < size; ++idx) { - cluster_offsets[idx] = be64toh(cluster_offsets[idx]); + cluster_offsets[idx] = big_to_native(cluster_offsets[idx]); } decoded = true; @@ -385,7 +388,7 @@ private: auto cluster_offset = l2_table->cluster_offsets[request.l2_table_index]; if (!l2_table->decoded) { // table hasn't been byte-swapped - cluster_offset = be64toh(cluster_offset); + cluster_offset = big_to_native(cluster_offset); } *request.cluster_offset = cluster_offset & qcow_format->m_cluster_mask; @@ -890,8 +893,8 @@ void QCOWFormat::handle_probe(int r, Context* on_finish) { auto header_probe = *reinterpret_cast( m_bl.c_str()); - header_probe.magic = be32toh(header_probe.magic); - header_probe.version = be32toh(header_probe.version); + header_probe.magic = big_to_native(header_probe.magic); + header_probe.version = big_to_native(header_probe.version); if (header_probe.magic != QCOW_MAGIC) { lderr(cct) << "invalid QCOW header magic" << dendl; @@ -946,13 +949,13 @@ void QCOWFormat::handle_read_v1_header(int r, Context* on_finish) { auto header = *reinterpret_cast(m_bl.c_str()); // byte-swap important fields - header.magic = be32toh(header.magic); - header.version = be32toh(header.version); - header.backing_file_offset = be64toh(header.backing_file_offset); - header.backing_file_size = be32toh(header.backing_file_size); - header.size = be64toh(header.size); - header.crypt_method = be32toh(header.crypt_method); - header.l1_table_offset = be64toh(header.l1_table_offset); + header.magic = big_to_native(header.magic); + header.version = big_to_native(header.version); + header.backing_file_offset = big_to_native(header.backing_file_offset); + header.backing_file_size = big_to_native(header.backing_file_size); + header.size = big_to_native(header.size); + header.crypt_method = big_to_native(header.crypt_method); + header.l1_table_offset = big_to_native(header.l1_table_offset); if (header.magic != QCOW_MAGIC || header.version != 1) { // honestly shouldn't happen since we've already validated it @@ -1048,17 +1051,17 @@ void QCOWFormat::handle_read_v2_header(int r, Context* on_finish) { auto header = *reinterpret_cast(m_bl.c_str()); // byte-swap important fields - header.magic = be32toh(header.magic); - header.version = be32toh(header.version); - header.backing_file_offset = be64toh(header.backing_file_offset); - header.backing_file_size = be32toh(header.backing_file_size); - header.cluster_bits = be32toh(header.cluster_bits); - header.size = be64toh(header.size); - header.crypt_method = be32toh(header.crypt_method); - header.l1_size = be32toh(header.l1_size); - header.l1_table_offset = be64toh(header.l1_table_offset); - header.nb_snapshots = be32toh(header.nb_snapshots); - header.snapshots_offset = be64toh(header.snapshots_offset); + header.magic = big_to_native(header.magic); + header.version = big_to_native(header.version); + header.backing_file_offset = big_to_native(header.backing_file_offset); + header.backing_file_size = big_to_native(header.backing_file_size); + header.cluster_bits = big_to_native(header.cluster_bits); + header.size = big_to_native(header.size); + header.crypt_method = big_to_native(header.crypt_method); + header.l1_size = big_to_native(header.l1_size); + header.l1_table_offset = big_to_native(header.l1_table_offset); + header.nb_snapshots = big_to_native(header.nb_snapshots); + header.snapshots_offset = big_to_native(header.snapshots_offset); if (header.version == 2) { // valid only for version >= 3 @@ -1068,10 +1071,10 @@ void QCOWFormat::handle_read_v2_header(int r, Context* on_finish) { header.header_length = 72; header.compression_type = 0; } else { - header.incompatible_features = be64toh(header.incompatible_features); - header.compatible_features = be64toh(header.compatible_features); - header.autoclear_features = be64toh(header.autoclear_features); - header.header_length = be32toh(header.header_length); + header.incompatible_features = big_to_native(header.incompatible_features); + header.compatible_features = big_to_native(header.compatible_features); + header.autoclear_features = big_to_native(header.autoclear_features); + header.header_length = big_to_native(header.header_length); } if (header.magic != QCOW_MAGIC || header.version < 2 || header.version > 3) { @@ -1212,13 +1215,13 @@ void QCOWFormat::handle_read_snapshot(int r, Context* on_finish) { auto header = *reinterpret_cast(m_bl.c_str()); auto& snapshot = m_snapshots[m_snapshots.size() + 1]; - snapshot.id.resize(be16toh(header.id_str_size)); - snapshot.name.resize(be16toh(header.name_size)); - snapshot.l1_table_offset = be64toh(header.l1_table_offset); - snapshot.l1_table.size = be32toh(header.l1_size); - snapshot.timestamp.sec_ref() = be32toh(header.date_sec); - snapshot.timestamp.nsec_ref() = be32toh(header.date_nsec); - snapshot.extra_data_size = be32toh(header.extra_data_size); + snapshot.id.resize(big_to_native(header.id_str_size)); + snapshot.name.resize(big_to_native(header.name_size)); + snapshot.l1_table_offset = big_to_native(header.l1_table_offset); + snapshot.l1_table.size = big_to_native(header.l1_size); + snapshot.timestamp.sec_ref() = big_to_native(header.date_sec); + snapshot.timestamp.nsec_ref() = big_to_native(header.date_nsec); + snapshot.extra_data_size = big_to_native(header.extra_data_size); ldout(cct, 10) << "snap_id=" << m_snapshots.size() << ", " << "id_str_len=" << snapshot.id.size() << ", " @@ -1280,7 +1283,7 @@ void QCOWFormat::handle_read_snapshot_extra(int r, Context* on_finish) { if (snapshot.extra_data_size >= offsetof(QCowSnapshotExtraData, disk_size) + sizeof(uint64_t)) { auto extra = reinterpret_cast(m_bl.c_str()); - snapshot.size = be64toh(extra->disk_size); + snapshot.size = big_to_native(extra->disk_size); } else { snapshot.size = m_size; } diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index 044808830084..6187dbebaa46 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -20,6 +20,8 @@ #include "include/int_types.h" #include "include/scope_guard.h" +#include + #include #include #include @@ -84,6 +86,9 @@ namespace fs = std::experimental::filesystem; #undef dout_prefix #define dout_prefix *_dout << "rbd-nbd: " +using boost::endian::big_to_native; +using boost::endian::native_to_big; + enum Command { None, Map, @@ -177,15 +182,6 @@ static EventSocket terminate_event_sock; #define HELP_INFO 1 #define VERSION_INFO 2 -#ifdef CEPH_BIG_ENDIAN -#define ntohll(a) (a) -#elif defined(CEPH_LITTLE_ENDIAN) -#define ntohll(a) swab(a) -#else -#error "Could not determine endianess" -#endif -#define htonll(a) ntohll(a) - static int parse_args(vector& args, std::ostream *err_msg, Config *cfg); static int netlink_disconnect(int index); @@ -331,16 +327,16 @@ private: } if (ret < 0) { - ctx->reply.error = htonl(-ret); + ctx->reply.error = native_to_big(-ret); } else if ((ctx->command == NBD_CMD_READ) && ret < static_cast(ctx->request.len)) { int pad_byte_count = static_cast (ctx->request.len) - ret; ctx->data.append_zero(pad_byte_count); dout(20) << __func__ << ": " << *ctx << ": Pad byte count: " << pad_byte_count << dendl; - ctx->reply.error = htonl(0); + ctx->reply.error = native_to_big(0); } else { - ctx->reply.error = htonl(0); + ctx->reply.error = native_to_big(0); } ctx->server->io_finish(ctx); @@ -394,11 +390,11 @@ private: goto signal; } - ctx->request.from = ntohll(ctx->request.from); - ctx->request.type = ntohl(ctx->request.type); - ctx->request.len = ntohl(ctx->request.len); + ctx->request.from = big_to_native(ctx->request.from); + ctx->request.type = big_to_native(ctx->request.type); + ctx->request.len = big_to_native(ctx->request.len); - ctx->reply.magic = htonl(NBD_REPLY_MAGIC); + ctx->reply.magic = native_to_big(NBD_REPLY_MAGIC); memcpy(ctx->reply.handle, ctx->request.handle, sizeof(ctx->reply.handle)); ctx->command = ctx->request.type & 0x0000ffff; @@ -675,7 +671,7 @@ public: std::ostream &operator<<(std::ostream &os, const NBDServer::IOContext &ctx) { - os << "[" << std::hex << ntohll(*((uint64_t *)ctx.request.handle)); + os << "[" << std::hex << big_to_native(*((uint64_t *)ctx.request.handle)); switch (ctx.command) { @@ -700,7 +696,7 @@ std::ostream &operator<<(std::ostream &os, const NBDServer::IOContext &ctx) { } os << ctx.request.from << "~" << ctx.request.len << " " - << std::dec << ntohl(ctx.reply.error) << "]"; + << std::dec << big_to_native(ctx.reply.error) << "]"; return os; }