-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid bit fields in protocol message definitions #768
Comments
it's host-endian, not big-endian: that is, we don't do, or attempt to do, any handling of different endianness at all. We certainly wouldn't be against supporting this, but not something we're planning to work on ourselves. |
To clarify my environment: I ran a qemu vfio-user client against a qemu vfio-user server on a big-endian host. So, both client and server are in big-endian byte order and hence expected to interoperate. However, I found that server and client disagree on the bit field layout in the header flags field. The qemu client would treat flags as a single uint32_t value in host byte order and so the flags field of a reply message would be Then I researched what the net has to say about bit fields and byte order, and found that the standards leave this implementation defined. In practice, compilers are forced to implement whatever widespread conventions there are (e.g. Linux does https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/ip.h?h=03702d4d29be4e2510ec80b248dbbde4e57030d9#n88 for example). At the end of the day, it's IMO prudent to accept that bit field encoding is a mine field and rather rely on shifting and masking to pack/unpack the flags field. |
I see, because the client doesn't use bitfields, we get bitten by the different on big endian systems. |
@mnissler-rivos are you planning to submit a patch? |
It's not a priority for me given that none of my stuff is running on big-endian host systems (I found this after checking on big-ending when checking qemu behavior in context of a code review there). If you're all OK with replacing the bitfield with |
I'm not sure there's any other way around this other than using |
yup for sure thanks |
It turns out that the bit field will not yield the desired / specified bit layout on big-endian systems, see issue nutanix#768 for details. Thus, replace the bit field with constants for the individual fields and use bit masking when accessing the flags field. Signed-off-by: Mattias Nissler <[email protected]>
It turns out that the bit field will not yield the desired / specified bit layout on big-endian systems, see issue nutanix#768 for details. Thus, replace the bit field with constants for the individual fields and use bit masking when accessing the flags field. Signed-off-by: Mattias Nissler <[email protected]>
It turns out that the bit field will not yield the desired / specified bit layout on big-endian systems, see issue #768 for details. Thus, replace the bit field with constants for the individual fields and use bit masking when accessing the flags field. Signed-off-by: Mattias Nissler <[email protected]> Reviewed-by: John Levon <[email protected]> Reviewed-by: Thanos Makatos <[email protected]>
The
vfio_user_header.flags
field currently uses bit fields to specify various sub-fields:libvfio-user/include/vfio-user.h
Lines 80 to 93 in 149aa84
Bit field layout is up to the compiler, and thus may differ depending on machine byte order, optimization settings, etc.
I observed this as problematic when testing on a big-endian host. All VFIO-user protocol fields are in big-endian byte order then (as expected per #615 IIUC), but the flags subcomponents weren't compiled in reverse order, thus ending up in the wrong bit positions.
The text was updated successfully, but these errors were encountered: