Re: [PATCH v2 2/5] block nbd: send handle in network order

From: Ming Lei
Date: Mon Mar 20 2023 - 19:21:45 EST


On Fri, Mar 17, 2023 at 03:27:46PM -0500, Eric Blake wrote:
> The NBD spec says the client handle (or cookie) is opaque on the
> server, and therefore it really doesn't matter what endianness we use;
> to date, the use of memcpy() between u64 and a char[8] has exposed
> native endianness when treating the handle as a 64-bit number.

No, memcpy() works fine for char[8], which doesn't break endianness.

> However, since NBD protocol documents that everything else is in
> network order, and tools like Wireshark will dump even the contents of
> the handle as seen over the network, it's worth using a consistent
> ordering regardless of the native endianness.
>
> Plus, using a consistent endianness now allows an upcoming patch to
> simplify this to directly use integer assignment instead of memcpy().

It isn't necessary, given ->handle is actually u64, which is handled by
nbd client only.

>
> Signed-off-by: Eric Blake <eblake@xxxxxxxxxx>
>
> ---
> v2: new patch
> ---
> drivers/block/nbd.c | 10 +++++++---
> 1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> index 592cfa8b765a..8a9487e79f1c 100644
> --- a/drivers/block/nbd.c
> +++ b/drivers/block/nbd.c
> @@ -560,6 +560,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
> unsigned long size = blk_rq_bytes(req);
> struct bio *bio;
> u64 handle;
> + __be64 tmp;
> u32 type;
> u32 nbd_cmd_flags = 0;
> int sent = nsock->sent, skip = 0;
> @@ -606,7 +607,8 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
> request.len = htonl(size);
> }
> handle = nbd_cmd_handle(cmd);
> - memcpy(request.handle, &handle, sizeof(handle));
> + tmp = cpu_to_be64(handle);
> + memcpy(request.handle, &tmp, sizeof(tmp));

This way copies handle two times, really not fun.


thanks,
Ming