Skip to content

src: stop using le32toh() and the like for better portability - #72482

Merged
idryomov merged 6 commits into
ceph:mainfrom
tchaikov:wip-boost-endian
Oct 12, 2026
Merged

idryomov merged 6 commits into
ceph:mainfrom
tchaikov:wip-boost-endian

Conversation

@tchaikov

@tchaikov tchaikov commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

le16toh() and its siblings come from <endian.h>, which macOS does not
have, and MinGW does not provide them either. boost::endian works on
all of these platforms.

native_to_little() and little_to_native() take their width from the
argument, but some callers pass a size_t, e.g.
HTOCEPH_16(this->obj_name.length()). So the macros become inline
functions with fixed width parameters, and the argument is still
narrowed before it is converted, as it was with htole16().

Signed-off-by: Kefu Chai <[email protected]>

@ronen-fr ronen-fr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. One comment

Comment thread src/common/crc32c_s390x.c
/* Pure C implementations of CRC, one byte at a time */
unsigned int crc32c_le(uint32_t crc, unsigned char const *buf, unsigned len){
crc = htole32(crc);
crc = __builtin_bswap32(crc);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest adding a comment based on the commit message (i.e. - "this only builds on ,,, " etc)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure. added.

@anrao19

anrao19 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

This PR has been added to main integration testing by anuchaithra started 2026-10-08-10:48.

b26a508 moved librbd/migration/QCOWFormat.cc to boost::endian, but
the test still builds its QCOW2 headers with htobe32() and htobe64().
QCOWFormat.cc is why the __MINGW32__ block in include/compat.h defines
these macros, and this test is now their only user in a Windows build.

native_to_big() takes its width from the argument, and some of these
calls pass an int or a size_t, e.g. htobe64(1<<30) or
htobe16(id.size()). Those calls name the width of the macro they
replace.

Signed-off-by: Kefu Chai <[email protected]>
Replace htole64() with boost::endian::native_to_little(), and drop the
<endian.h> include.

The length goes into a 64-bit field of the journald native protocol,
but size_t is only 32 bits wide on 32-bit hosts, so pass uint64_t
explicitly. LogEntryEncoder also converted le.msg.size() twice; store
the msg_len it already has instead.

Signed-off-by: Kefu Chai <[email protected]>
The allocator file that BlueStore writes on umount keeps its extents
and CRCs in little endian, using le32toh() and the like. Use
boost::endian::native_to_little() and little_to_native() instead. Each
argument is already a uint32_t or uint64_t matching the width of the
old macro, so the on-disk format does not change.

Also remove the "#if 1 ... #else" block that defined the macros. Its
#else branch, which wrote big endian to debug the encoder, was never
compiled.

Signed-off-by: Kefu Chai <[email protected]>
crc32c_le() converts the CRC with htole32() and le32toh(). This file is
C, so it cannot use boost::endian like the other callers.

It is only built when HAVE_S390X is set, and s390x is always big
endian, so both calls swap the bytes. Call __builtin_bswap32() directly
instead. GCC and Clang both provide it, and <endian.h> is no longer
needed.

Signed-off-by: Kefu Chai <[email protected]>
057660a defined htobe16() and the rest for MinGW, because
librbd/migration/QCOWFormat.cc used them and the rbd tool no longer
built on Windows. b26a508 moved QCOWFormat.cc to boost::endian,
and the earlier commits in this series moved everything else, so
nothing uses these macros now.

Signed-off-by: Kefu Chai <[email protected]>
@tchaikov

tchaikov commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

changelog:

  • dropped the explicit type parameter when appropriate.

@idryomov idryomov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack from the RBD perspective

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath7-testing-20261012.100201-main.

@anrao19

anrao19 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

This PR has been added to main integration testing by anuchaithra started 2026-10-08-10:48.

pr testing completed and approved by @ivancich, more details in https://tracker.ceph.com/issues/81545.
@tchaikov , if further is not needed then this can be merged

@anrao19 anrao19 added TESTED RGW-TESTED testing from rgw completed and removed wip-anrao11-testing labels Oct 12, 2026
@idryomov
idryomov merged commit fb58ec9 into ceph:main Oct 12, 2026
18 checks passed
@tchaikov
tchaikov deleted the wip-boost-endian branch October 14, 2026 04:01
@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been removed from wip-bharath7-testing-20261012.100201-main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants