Skip to content

Commit bd9e991

Browse files
ebblakeaxboe
authored andcommitted
block nbd: use req.cookie instead of req.handle
The NBD spec was recently changed [1] to refer to the opaque client identifier as a 'cookie' rather than a 'handle', but has for a much longer time listed it as a 64-bit value, and declares that all values in the NBD protocol are sent in network byte order (big-endian). Because the value is opaque to the server, it doesn't usually matter what endianness we send as the client - as long as we are consistent that either we byte-swap on both write and read, or on neither, then we can match server replies back to our requests. That said, our internal use of the cookie is as a 64-bit number (well, as two 32-bit numbers concatenated together), rather than as 8 individual bytes; so prior to this commit, we ARE leaking the native endianness of our internals as a client out to the server. We don't know of any server that will actually inspect the opaque value and behave differently depending on whether a little-endian or big-endian client is sending requests, but since we DO log the cookie value, a wireshark capture of the network traffic is easier to correlate back to the kernel traffic of a big-endian host (where the u64 and char[8] representations are the same) than of a little-endian host (where if wireshark honors the NBD spec and displays a u64 in network byte order, it is byte-swapped from what the kernel logged). The fix in this patch is thus two-part: it now consistently uses network byte order for the opaque value (no difference to a big-endian machine, but an extra byteswap on a little-endian machine; probably in the noise compared to the overhead of network traffic in general), and now uses a 64-bit integer instead of char[8] as its preferred access to the opaque value (direct assignment instead of memcpy()). Signed-off-by: Eric Blake <eblake@redhat.com> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Link: https://lore.kernel.org/r/20230410180611.1051618-4-eblake@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 2686eb8 commit bd9e991

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

drivers/block/nbd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
609609
request.len = htonl(size);
610610
}
611611
handle = nbd_cmd_handle(cmd);
612-
memcpy(request.handle, &handle, sizeof(handle));
612+
request.cookie = cpu_to_be64(handle);
613613

614614
trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
615615

@@ -621,7 +621,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
621621
trace_nbd_header_sent(req, handle);
622622
if (result < 0) {
623623
if (was_interrupted(result)) {
624-
/* If we havne't sent anything we can just return BUSY,
624+
/* If we haven't sent anything we can just return BUSY,
625625
* however if we have sent something we need to make
626626
* sure we only allow this req to be sent until we are
627627
* completely done.
@@ -735,7 +735,7 @@ static struct nbd_cmd *nbd_handle_reply(struct nbd_device *nbd, int index,
735735
u32 tag;
736736
int ret = 0;
737737

738-
memcpy(&handle, reply->handle, sizeof(handle));
738+
handle = be64_to_cpu(reply->cookie);
739739
tag = nbd_handle_to_tag(handle);
740740
hwq = blk_mq_unique_tag_to_hwq(tag);
741741
if (hwq < nbd->tag_set.nr_hw_queues)

0 commit comments

Comments
 (0)