Skip to content

Conversation

@kurtjd
Copy link
Contributor

@kurtjd kurtjd commented Jan 14, 2026

This PR demonstrates using zerocopy for serializing instead of doing it manually for thermal service messages (and can be used for all services if this approach seems okay).

Notes:

  • Zerocopy is tricky when it comes to alignment and representations. Talking with Billy and we agreed it's pretty hard to get it working on the ThermalRequest/ThermalResponse enums themselves, so I derived the appropriate traits for structs which the enums wrapped, then just pattern match over the enum.
  • Had to use the zerocopy::byteorder::U<N> types to ensure LE representation on the wire (in the very rare case this is running on BE-native hardware) and to force alignment of fields to 1 (since zerocopy does not allow padding and a #repr(packed) could cause issues when accessing the fields from Rust code).
  • Those U<N> types unfortunately don't impl defmt::Format so had to manually do it (might try to upstream defmt support)
  • The mctp_rs trait methods require serializing into a buffer, instead of just returning a &[u8], so because of that, we lose the zero-copy benefit from zerocopy (and are really just using it for its serialization abilities)

The goal is also eventually to have these service message crates be useable from other areas than just the EC (such as host-side secure services).

@kurtjd kurtjd self-assigned this Jan 14, 2026
@kurtjd kurtjd added the enhancement New feature or request label Jan 14, 2026
@kurtjd kurtjd changed the base branch from main to v0.2.0 January 14, 2026 23:21
@github-actions github-actions bot added the cargo vet PRs pending auditor review label Jan 14, 2026
@kurtjd kurtjd force-pushed the zerocopy-service-msg branch 2 times, most recently from 9476651 to f24cc62 Compare January 14, 2026 23:43
@kurtjd kurtjd removed the cargo vet PRs pending auditor review label Jan 14, 2026
@kurtjd kurtjd force-pushed the zerocopy-service-msg branch from f24cc62 to 1f42a1c Compare January 15, 2026 00:16
@kurtjd
Copy link
Contributor Author

kurtjd commented Jan 15, 2026

Though I'm not a huge fan of this approach because since we are copying bytes, we shouldn't need to worry about the underlying memory structure, so all this fiddling with alignment, padding, and endianness is just noise (which would be important if we were going for zero-cost serialization, but the mctp_rs crate currently requires us to copy). Though if we modify the mctp_rs crate in the future to not copy, then maybe this will be worthwhile

But for the time-being though, ideally there would be some simple crate that is a proc macro or something that does automatically what we were doing manually (copying each field of the struct in series, in LE format, into a packed &mut [u8]). It looks like there are a couple but haven't played around with them much.

Based on the EC tech forum discussion Friday, something like postcard might not be acceptable because all though we don't necessarily care about the format on the wire as long as both sides agree, not sure we want to make the guarantee that the other side will be using postcard as well, and the packed byte format is the simplest to write a serializer/deserializer for.

@kurtjd kurtjd force-pushed the zerocopy-service-msg branch from 1f42a1c to e5e4815 Compare January 15, 2026 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant