More nbd-protocol.h improvements Eric Blake (2): common/protocol: Switch nbdmagic to uint64_t common/protocol: Declare additional constants common/protocol/nbd-protocol.h | 16 ++++++++++------ server/protocol-handshake-newstyle.c | 2 +- server/protocol-handshake-oldstyle.c | 2 +- plugins/nbd/nbd-standalone.c | 2 +- tests/test-layers.c | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) -- 2.21.0
Eric Blake
2019-Sep-25 14:49 UTC
[Libguestfs] [nbdkit PATCH 1/2] common/protocol: Switch nbdmagic to uint64_t
Stating that our magic is the C string "NBDMAGIC" is ambiguous, since not all the world uses ASCII. Switch to something that is wire-compatible even if compiled in an EBCDIC environment. Signed-off-by: Eric Blake <eblake@redhat.com> --- common/protocol/nbd-protocol.h | 9 +++++---- server/protocol-handshake-newstyle.c | 2 +- server/protocol-handshake-oldstyle.c | 2 +- plugins/nbd/nbd-standalone.c | 2 +- tests/test-layers.c | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/common/protocol/nbd-protocol.h b/common/protocol/nbd-protocol.h index a7a70116..9bf7171e 100644 --- a/common/protocol/nbd-protocol.h +++ b/common/protocol/nbd-protocol.h @@ -50,7 +50,7 @@ /* Old-style handshake. */ struct nbd_old_handshake { - char nbdmagic[8]; /* "NBDMAGIC" */ + uint64_t nbdmagic; /* NBD_MAGIC */ uint64_t version; /* NBD_OLD_VERSION */ uint64_t exportsize; uint16_t gflags; /* global flags */ @@ -58,20 +58,21 @@ struct nbd_old_handshake { char zeroes[124]; /* must be sent as zero bytes */ } NBD_ATTRIBUTE_PACKED; +#define NBD_MAGIC UINT64_C(0x4e42444d41474943) /* ASCII "NBDMAGIC" */ #define NBD_OLD_VERSION UINT64_C(0x420281861253) /* New-style handshake. */ struct nbd_new_handshake { - char nbdmagic[8]; /* "NBDMAGIC" */ + uint64_t nbdmagic; /* NBD_MAGIC */ uint64_t version; /* NBD_NEW_VERSION */ uint16_t gflags; /* global flags */ } NBD_ATTRIBUTE_PACKED; -#define NBD_NEW_VERSION UINT64_C(0x49484156454F5054) +#define NBD_NEW_VERSION UINT64_C(0x49484156454F5054) /* ASCII "IHAVEOPT" */ /* New-style handshake option (sent by the client to us). */ struct nbd_new_option { - uint64_t version; /* NEW_VERSION */ + uint64_t version; /* NBD_NEW_VERSION */ uint32_t option; /* NBD_OPT_* */ uint32_t optlen; /* option data length */ /* option data follows */ diff --git a/server/protocol-handshake-newstyle.c b/server/protocol-handshake-newstyle.c index e46b8255..d0fb4dd7 100644 --- a/server/protocol-handshake-newstyle.c +++ b/server/protocol-handshake-newstyle.c @@ -677,7 +677,7 @@ protocol_handshake_newstyle (struct connection *conn) debug ("newstyle negotiation: flags: global 0x%x", gflags); - memcpy (handshake.nbdmagic, "NBDMAGIC", 8); + handshake.nbdmagic = htobe64 (NBD_MAGIC); handshake.version = htobe64 (NBD_NEW_VERSION); handshake.gflags = htobe16 (gflags); diff --git a/server/protocol-handshake-oldstyle.c b/server/protocol-handshake-oldstyle.c index 8faa4f03..4efc668b 100644 --- a/server/protocol-handshake-oldstyle.c +++ b/server/protocol-handshake-oldstyle.c @@ -60,7 +60,7 @@ protocol_handshake_oldstyle (struct connection *conn) gflags, eflags); memset (&handshake, 0, sizeof handshake); - memcpy (handshake.nbdmagic, "NBDMAGIC", 8); + handshake.nbdmagic = htobe64 (NBD_MAGIC); handshake.version = htobe64 (NBD_OLD_VERSION); handshake.exportsize = htobe64 (exportsize); handshake.gflags = htobe16 (gflags); diff --git a/plugins/nbd/nbd-standalone.c b/plugins/nbd/nbd-standalone.c index 1789e39c..ab74e1e6 100644 --- a/plugins/nbd/nbd-standalone.c +++ b/plugins/nbd/nbd-standalone.c @@ -1018,7 +1018,7 @@ nbd_open_handle (int readonly) nbdkit_error ("unable to read magic: %m"); goto err; } - if (strncmp (old.nbdmagic, "NBDMAGIC", sizeof old.nbdmagic)) { + if (be64toh (old.nbdmagic) != NBD_MAGIC) { nbdkit_error ("wrong magic, %s is not an NBD server", servname); goto err; } diff --git a/tests/test-layers.c b/tests/test-layers.c index 9bc6532c..93b7770c 100644 --- a/tests/test-layers.c +++ b/tests/test-layers.c @@ -174,7 +174,7 @@ main (int argc, char *argv[]) perror ("recv: handshake"); exit (EXIT_FAILURE); } - if (memcmp (handshake.nbdmagic, "NBDMAGIC", 8) != 0 || + if (be64toh (handshake.nbdmagic) != NBD_MAGIC || be64toh (handshake.version) != NBD_NEW_VERSION) { fprintf (stderr, "%s: unexpected NBDMAGIC or version\n", program_name); -- 2.21.0
Eric Blake
2019-Sep-25 14:49 UTC
[Libguestfs] [nbdkit PATCH 2/2] common/protocol: Declare additional constants
We aren't using them yet, but as we are now publishing this file, it's worth letting other clients have a chance to use named bits reserved by the protocol. This does not expose anything related to resize, as that is still experimental. Signed-off-by: Eric Blake <eblake@redhat.com> --- common/protocol/nbd-protocol.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/common/protocol/nbd-protocol.h b/common/protocol/nbd-protocol.h index 9bf7171e..bdd1ef21 100644 --- a/common/protocol/nbd-protocol.h +++ b/common/protocol/nbd-protocol.h @@ -98,8 +98,8 @@ struct nbd_fixed_new_option_reply { #define NBD_REP_MAGIC UINT64_C(0x3e889045565a9) /* Global flags. */ -#define NBD_FLAG_FIXED_NEWSTYLE 1 -#define NBD_FLAG_NO_ZEROES 2 +#define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) +#define NBD_FLAG_NO_ZEROES (1 << 1) /* Per-export flags. */ #define NBD_FLAG_HAS_FLAGS (1 << 0) @@ -143,6 +143,9 @@ struct nbd_fixed_new_option_reply { #define NBD_REP_ERR_TOO_BIG NBD_REP_ERR (9) #define NBD_INFO_EXPORT 0 +#define NBD_INFO_NAME 1 +#define NBD_INFO_DESCRIPTION 2 +#define NBD_INFO_BLOCK_SIZE 3 /* NBD_INFO_EXPORT reply (follows fixed_new_option_reply). */ struct nbd_fixed_new_option_reply_info_export { -- 2.21.0
Richard W.M. Jones
2019-Sep-25 15:41 UTC
Re: [Libguestfs] [nbdkit PATCH 2/2] common/protocol: Declare additional constants
ACK this series and the associated libnbd series, thanks. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Apparently Analagous Threads
- [PATCH nbdkit 0/4] common/protocol: Unify public <nbd-protocol.h>
- [PATCH] Add support for newstyle NBD protocol (RHBZ#1297100).
- [libnbd PATCH] lib: Synchronize nbd-protocol.h with nbdkit, again
- [PATCH nbdkit 3/4] common/protocol: Update nbd-protocol.h so it matches libnbd’s copy.
- [libnbd PATCH] API: Add nbd_set_opt_mode to expose NEGOTIATING state