Skip to contentSkip to conversation

nbd: s/handle/cookie/

PATCH Superseded Eric Blake 3 participants 12 messages Mar 17, 2023 – Mar 22, 2023

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

v2 cover nbd: s/handle/cookie/

v1 was here: https://lkml.org/lkml/2023/3/10/1162
since then:
- split original 1/3 into 1/5 and 5/5
- new patch 2/5
- reorder members of anon union
- always send cookie in network order

Eric Blake (5):
uapi nbd: improve doc links to userspace spec block nbd: send handle in network order
uapi nbd: add cookie alias to handle
block nbd: use req.cookie instead of req.handle docs nbd: userspace NBD now favors github over sourceforge

 Documentation/admin-guide/blockdev/nbd.rst |  2 +-
 drivers/block/nbd.c                        |  6 +++---
 include/uapi/linux/nbd.h                   | 25 +++++++++++++++++-----
 3 files changed, 24 insertions(+), 9 deletions(-)

base-commit: 8d3c682a5e3d9dfc2448ecbb22f4cd48359b9e21

Signature · 1 line
2.39.2
author
#1 lore raw

PATCH v2 1/5 uapi nbd: improve doc links to userspace spec

The uapi <linux/nbd.h> header intentionally documents only the NBD server features that the kernel module will utilize as a client. But while it already had one mention of skipped bits due to userspace extensions, it did not actually direct the reader to the canonical source to learn about those extensions.

While touching comments, fix an outdated reference that listed only READ and WRITE as commands.

---
v2: Split change to sourceforge link to separate patch

---
 include/uapi/linux/nbd.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
include/uapi/linux/nbd.h +12 -3
index 20d6cc91435d..8797387caaf7 100644
@@ -11,6 +11,8 @@
  *            Cleanup PARANOIA usage & code.
  * 2004/02/19 Paul Clements
  *            Removed PARANOIA, plus various cleanup and comments
+ * 2023 Copyright Red Hat
+ *            Link to userspace extensions.
  */
 
 #ifndef _UAPILINUX_NBD_H
@@ -30,12 +32,18 @@
 #define NBD_SET_TIMEOUT _IO( 0xab, 9 )
 #define NBD_SET_FLAGS   _IO( 0xab, 10)
 
+/*
+ * See also https://github.com/NetworkBlockDevice/nbd/blob/master/doc/proto.md
+ * for additional userspace extensions not yet utilized in the kernel module.
+ */
+
 enum {
 	NBD_CMD_READ = 0,
 	NBD_CMD_WRITE = 1,
 	NBD_CMD_DISC = 2,
 	NBD_CMD_FLUSH = 3,
 	NBD_CMD_TRIM = 4
+	/* userspace defines additional extension commands */
 };
 
 /* values for flags field, these are server interaction specific. */
@@ -64,14 +72,15 @@ enum {
 #define NBD_REQUEST_MAGIC 0x25609513
 #define NBD_REPLY_MAGIC 0x67446698
 /* Do *not* use magics: 0x12560953 0x96744668. */
+/* magic 0x668e33ef for structured reply not supported by kernel yet */
 
 /*
  * This is the packet used for communication between client and
  * server. All data are in network byte order.
  */
 struct nbd_request {
-	__be32 magic;
-	__be32 type;	/* == READ || == WRITE 	*/
+	__be32 magic;	/* NBD_REQUEST_MAGIC	*/
+	__be32 type;	/* See NBD_CMD_*	*/
 	char handle[8];
 	__be64 from;
 	__be32 len;
@@ -82,7 +91,7 @@ struct nbd_request {
  * it has completed an I/O request (or an error occurs).
  */
 struct nbd_reply {
-	__be32 magic;
+	__be32 magic;		/* NBD_REPLY_MAGIC	*/
 	__be32 error;		/* 0 = ok, else error	*/
 	char handle[8];		/* handle you got from request	*/
 };
Signature · 1 line
2.39.2
author
#2 lore raw

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

In reply to Eric Blake

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. 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().

---
v2: new patch

---
 drivers/block/nbd.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/block/nbd.c +7 -3
index 592cfa8b765a..8a9487e79f1c 100644
@@ -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));
 
 	trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
 
@@ -618,7 +620,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
 	trace_nbd_header_sent(req, handle);
 	if (result < 0) {
 		if (was_interrupted(result)) {
-			/* If we havne't sent anything we can just return BUSY,
+			/* If we haven't sent anything we can just return BUSY,
 			 * however if we have sent something we need to make
 			 * sure we only allow this req to be sent until we are
 			 * completely done.
@@ -727,12 +729,14 @@ static struct nbd_cmd *nbd_handle_reply(struct nbd_device *nbd, int index,
 	int result;
 	struct nbd_cmd *cmd;
 	struct request *req = NULL;
+	__be64 tmp;
 	u64 handle;
 	u16 hwq;
 	u32 tag;
 	int ret = 0;
 
-	memcpy(&handle, reply->handle, sizeof(handle));
+	memcpy(&tmp, reply->handle, sizeof(tmp));
+	handle = be64_to_cpu(tmp);
 	tag = nbd_handle_to_tag(handle);
 	hwq = blk_mq_unique_tag_to_hwq(tag);
 	if (hwq < nbd->tag_set.nr_hw_queues)
Signature · 1 line
2.39.2
#3 lore raw

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

Quoted 4 lines · Eric Blake

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.

Quoted 7 lines

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.

Quoted 22 lines

---
v2: new patch

---
 drivers/block/nbd.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/block/nbd.c +1 -0
index 592cfa8b765a..8a9487e79f1c 100644
@@ -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;

drivers/block/nbd.c static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) · line 611

Quoted 6 lines
drivers/block/nbd.c +2 -1
@@ -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

author
#4 lore raw

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

Quoted 7 lines · Ming Lei
Quoted 4 lines · Eric Blake

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.

I didn't say memcpy() breaks endianness, I said it preserves it. By using memcpy(), you are exposing native endianness over the wire. Thus, even though a server should not be making any decisions based on the content of the handle (it is an opaque value handed back to the client unchanged), the current kernel client code DOES leak through information about whether the client is big- or little-endian; in contrast to the NBD protocol saying that ALL data is network-byte-order.

Quoted 11 lines
Quoted 8 lines

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.

No, re-read the whole series. ->handle is actually char[8]. Later in the series adds ->cookie as __be64 as an alias to ->handle, precisely so that we are converting the u64 'handle' in kernel code into a big-endian value on the wire, regardless of the host type, and making it impossible for a server to inspect the wire data and learn the kernel's endianness.

Quoted 23 lines
Quoted 23 lines

---
v2: new patch

---
 drivers/block/nbd.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/block/nbd.c +1 -0
index 592cfa8b765a..8a9487e79f1c 100644
@@ -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)
Quoted 8 lines

drivers/block/nbd.c static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) · line 611

Quoted 6 lines
drivers/block/nbd.c +2 -1
 		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.

Indeed. And as mentioned in the commit message, it is temporary; the second copy goes away later in the series once we can use direct integer assignment.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#5 lore raw

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

Quoted 7 lines · Eric Blake
Quoted 6 lines · Ming Lei
Quoted 4 lines · Eric Blake

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.

Quoted 8 lines
Quoted 1 line

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

I didn't say memcpy() breaks endianness, I said it preserves it. By using memcpy(), you are exposing native endianness over the wire. Thus, even though a server should not be making any decisions based on the content of the handle (it is an opaque value handed back to the client unchanged), the current kernel client code DOES leak through information about whether the client is big- or little-endian;

How is the client cpu endianness leaked with handle defined as char[8]?

Suppose it is leaked, is it really one issue? Cause most of CPUs in the world is little-endian.

Quoted 2 lines

contrast to the NBD protocol saying that ALL data is network-byte-order.

That doesn't make sense for any data defined as char[] or byte which needn't to be little or big endian.

Quoted 13 lines
Quoted 7 lines
Quoted 7 lines

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.

Quoted 5 lines
Quoted 2 lines

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.

Quoted 6 lines

No, re-read the whole series. ->handle is actually char[8]. Later in the series adds ->cookie as __be64 as an alias to ->handle, precisely so that we are converting the u64 'handle' in kernel code into a big-endian value on the wire, regardless of the host type, and making it impossible for a server to inspect the wire data and learn the kernel's endianness.

How does server learn the client cpu endianness in this way? Is it really one issue?

Quoted 33 lines
Quoted 24 lines
Quoted 24 lines

---
v2: new patch

---
 drivers/block/nbd.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/block/nbd.c +1 -0
index 592cfa8b765a..8a9487e79f1c 100644
@@ -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)
Quoted 8 lines

drivers/block/nbd.c static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) · line 611

Quoted 6 lines
drivers/block/nbd.c +2 -1
 		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.

Quoted 3 lines

Indeed. And as mentioned in the commit message, it is temporary; the second copy goes away later in the series once we can use direct integer assignment.

Then please merge with following patch, given it is hard to review temporary change.

thanks,
Ming

author
#6 lore raw

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

Quoted 19 lines · Ming Lei
Quoted 7 lines · Eric Blake
Quoted 6 lines · Ming Lei
Quoted 4 lines · Eric Blake

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.

Quoted 8 lines
Quoted 1 line

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

I didn't say memcpy() breaks endianness, I said it preserves it. By using memcpy(), you are exposing native endianness over the wire. Thus, even though a server should not be making any decisions based on the content of the handle (it is an opaque value handed back to the client unchanged), the current kernel client code DOES leak through information about whether the client is big- or little-endian;

How is the client cpu endianness leaked with handle defined as char[8]?

Quoted 8 lines

Suppose it is leaked, is it really one issue? Cause most of CPUs in the world is little-endian.

Quoted 2 lines

contrast to the NBD protocol saying that ALL data is network-byte-order.

That doesn't make sense for any data defined as char[] or byte which needn't to be little or big endian.

The NBD spec defines it as a 64-bit opaque quantity - that does not indicate whether it is a single integer or 8 characters, but because it is opaque, we don't have to care. However, if we DO treat it as an integer (and the kernel client code DOES do that: internally, it is building up a u64 integer), it is wise to consider network endianness.

Quoted 21 lines
Quoted 14 lines
Quoted 8 lines
Quoted 8 lines

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.

Quoted 5 lines
Quoted 2 lines

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.

Quoted 6 lines

No, re-read the whole series. ->handle is actually char[8]. Later in the series adds ->cookie as __be64 as an alias to ->handle, precisely so that we are converting the u64 'handle' in kernel code into a big-endian value on the wire, regardless of the host type, and making it impossible for a server to inspect the wire data and learn the kernel's endianness.

Quoted 2 lines

How does server learn the client cpu endianness in this way? Is it really one issue?

Not a security issue, merely a consistency one. A server that inspects the handles being sent by the client, and checks whether they are sequential when treated as a big- or little-endian number, can infer whether the client is little-endian. But there is nothing useful it can do with that knowledge. Rather, the consistency factor is that if you have a wireshark plugin reading network traffic, and are trying to correlate it back to kernel traces, it is NICE if the wireshark plugin can display the SAME u64 number as the kernel was sticking into the field - and the way to do that is to have a fixed endianness of the u64 value over the wire.

Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 25 lines

---
v2: new patch

---
 drivers/block/nbd.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/block/nbd.c +1 -0
index 592cfa8b765a..8a9487e79f1c 100644
@@ -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;

drivers/block/nbd.c static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) · line 611

Quoted 6 lines
drivers/block/nbd.c +2 -1
@@ -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.

Quoted 6 lines
Quoted 3 lines

Indeed. And as mentioned in the commit message, it is temporary; the second copy goes away later in the series once we can use direct integer assignment.

Then please merge with following patch, given it is hard to review temporary change.

The underlying reason I split this patch out is that in v1 I got complaints that I was not taking endianness into account. The patch series DOES cause an observable change (namely, a little-endian client now sends a value in big-endian order that it used to send in little-endian order) - but the change is harmless. But if you want me to squash this patch back with 4/5 in v3, I'm happy to do that.

Are there any other comments on this series that I should consider before spending time putting out a v3?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#7 lore raw

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

Quoted 26 lines · Eric Blake
Quoted 25 lines · Ming Lei
Quoted 7 lines · Eric Blake
Quoted 6 lines · Ming Lei
Quoted 4 lines · Eric Blake

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.

Quoted 8 lines
Quoted 1 line

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

I didn't say memcpy() breaks endianness, I said it preserves it. By using memcpy(), you are exposing native endianness over the wire. Thus, even though a server should not be making any decisions based on the content of the handle (it is an opaque value handed back to the client unchanged), the current kernel client code DOES leak through information about whether the client is big- or little-endian;

How is the client cpu endianness leaked with handle defined as char[8]?

Suppose it is leaked, is it really one issue? Cause most of CPUs in the world is little-endian.

Quoted 2 lines

contrast to the NBD protocol saying that ALL data is network-byte-order.

Quoted 8 lines
Quoted 2 lines

That doesn't make sense for any data defined as char[] or byte which needn't to be little or big endian.

The NBD spec defines it as a 64-bit opaque quantity - that does not indicate whether it is a single integer or 8 characters, but because it is opaque, we don't have to care. However, if we DO treat it as an integer (and the kernel client code DOES do that: internally, it is building up a u64 integer), it is wise to consider network endianness.

That depends on if it is reasonable to convert to int.

Quoted 25 lines
Quoted 22 lines
Quoted 15 lines
Quoted 9 lines
Quoted 9 lines

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.

Quoted 5 lines
Quoted 2 lines

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.

Quoted 6 lines

No, re-read the whole series. ->handle is actually char[8]. Later in the series adds ->cookie as __be64 as an alias to ->handle, precisely so that we are converting the u64 'handle' in kernel code into a big-endian value on the wire, regardless of the host type, and making it impossible for a server to inspect the wire data and learn the kernel's endianness.

Quoted 2 lines

How does server learn the client cpu endianness in this way? Is it really one issue?

Quoted 10 lines

Not a security issue, merely a consistency one. A server that inspects the handles being sent by the client, and checks whether they are sequential when treated as a big- or little-endian number, can infer whether the client is little-endian. But there is nothing useful it can do with that knowledge. Rather, the consistency factor is that if you have a wireshark plugin reading network traffic, and are trying to correlate it back to kernel traces, it is NICE if the wireshark plugin can display the SAME u64 number as the kernel was sticking into the field - and the way to do that is to have a fixed endianness of the u64 value over the wire.

OK, so the real motivation is only for aligning wireshark output with nbd trace event. If yes, please add it in comment log.

BTW, the nbd trace event can be converted to any format by bcc or bpftrace script, then you still can associate one with another.

Quoted 49 lines
Quoted 35 lines
Quoted 35 lines
Quoted 34 lines
Quoted 26 lines

---
v2: new patch

---
 drivers/block/nbd.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/block/nbd.c +1 -0
index 592cfa8b765a..8a9487e79f1c 100644
@@ -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;

drivers/block/nbd.c static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) · line 611

Quoted 6 lines
drivers/block/nbd.c +2 -1
@@ -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.

Quoted 6 lines
Quoted 3 lines

Indeed. And as mentioned in the commit message, it is temporary; the second copy goes away later in the series once we can use direct integer assignment.

Then please merge with following patch, given it is hard to review temporary change.

The underlying reason I split this patch out is that in v1 I got complaints that I was not taking endianness into account. The patch series DOES cause an observable change (namely, a little-endian client now sends a value in big-endian order that it used to send in little-endian order) - but the change is harmless. But if you want me to squash this patch back with 4/5 in v3, I'm happy to do that.

Quoted 2 lines

Are there any other comments on this series that I should consider before spending time putting out a v3?

I think 2~4 should be merged to single patch.

Thanks,
Ming

author
#8 lore raw

PATCH v2 3/5 uapi nbd: add cookie alias to handle

In reply to Eric Blake

The uapi <linux/nbd.h> header declares a 'char handle[8]' per request; which is overloaded in English (are you referring to "handle" the verb, such as handling a signal or writing a callback handler, or "handle" the noun, the value used in a lookup table to correlate a response back to the request). Many client-side NBD implementations (both servers and clients) have instead used 'uint64_t cookie' or similar, as it is easier to directly assign an integer than to futz around with memcpy. In fact, upstream documentation is now encouraging this shift in terminology:
https://lists.debian.org/nbd/2023/03/msg00031.html

Accomplish this by use of an anonymous union to provide the alias for anyone getting the definition from the uapi; this does not break existing clients, while exposing the nicer name for those who prefer it. Note that block/nbd.c still uses the term handle (in fact, it actually combines a 32-bit cookie and a 32-bit tag into the 64-bit handle), but that internal usage is not changed the public uapi, since no compliant NBD server has any reason to inspect or alter the 64 bits sent over the socket.

---
v2: swap order of anonymous union and add comments to list favored name

---
 include/uapi/linux/nbd.h | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
include/uapi/linux/nbd.h +9 -3
index 8797387caaf7..80ce0ef43afd 100644
@@ -12,7 +12,7 @@
  * 2004/02/19 Paul Clements
  *            Removed PARANOIA, plus various cleanup and comments
  * 2023 Copyright Red Hat
- *            Link to userspace extensions.
+ *            Link to userspace extensions, favor cookie over handle.
  */
 
 #ifndef _UAPILINUX_NBD_H
@@ -81,7 +81,10 @@ enum {
 struct nbd_request {
 	__be32 magic;	/* NBD_REQUEST_MAGIC	*/
 	__be32 type;	/* See NBD_CMD_*	*/
-	char handle[8];
+	union {
+		__be64 cookie;	/* Opaque identifier for request	*/
+		char handle[8];	/* older spelling of cookie		*/
+	};
 	__be64 from;
 	__be32 len;
 } __attribute__((packed));
@@ -93,6 +96,9 @@ struct nbd_request {
 struct nbd_reply {
 	__be32 magic;		/* NBD_REPLY_MAGIC	*/
 	__be32 error;		/* 0 = ok, else error	*/
-	char handle[8];		/* handle you got from request	*/
+	union {
+		__be64 cookie;	/* Opaque identifier from request	*/
+		char handle[8];	/* older spelling of cookie		*/
+	};
 };
 #endif /* _UAPILINUX_NBD_H */
Signature · 1 line
2.39.2
author
#9 lore raw

PATCH v2 4/5 block nbd: use req.cookie instead of req.handle

In reply to Eric Blake

A good optimizing compiler should not compile this any differently, but it is nicer to work directly with integers instead of memcpy().

---
v2: Fix kernel test robot complaint about wrong endianness on loongarch:
https://lore.kernel.org/oe-kbuild-all/[email protected]/

---
 drivers/block/nbd.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/block/nbd.c +2 -6
index 8a9487e79f1c..94ae85400b46 100644
@@ -560,7 +560,6 @@ 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;
@@ -607,8 +606,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
 		request.len = htonl(size);
 	}
 	handle = nbd_cmd_handle(cmd);
-	tmp = cpu_to_be64(handle);
-	memcpy(request.handle, &tmp, sizeof(tmp));
+	request.cookie = cpu_to_be64(handle);
 
 	trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
 
@@ -729,14 +727,12 @@ static struct nbd_cmd *nbd_handle_reply(struct nbd_device *nbd, int index,
 	int result;
 	struct nbd_cmd *cmd;
 	struct request *req = NULL;
-	__be64 tmp;
 	u64 handle;
 	u16 hwq;
 	u32 tag;
 	int ret = 0;
 
-	memcpy(&tmp, reply->handle, sizeof(tmp));
-	handle = be64_to_cpu(tmp);
+	handle = be64_to_cpu(reply->cookie);
 	tag = nbd_handle_to_tag(handle);
 	hwq = blk_mq_unique_tag_to_hwq(tag);
 	if (hwq < nbd->tag_set.nr_hw_queues)
Signature · 1 line
2.39.2
author
#10 lore raw

PATCH v2 5/5 docs nbd: userspace NBD now favors github over sourceforge

In reply to Eric Blake

While the sourceforge site for userspace NBD still exists, the code repository moved to github several years ago. Then with a recent patch[1], the github landing page contains just as much information as the sourceforge page, so we might as well point to a single location that also provides the code.

[1] https://lists.debian.org/nbd/2023/03/msg00051.html

---
v2: split into its own patch

---
 Documentation/admin-guide/blockdev/nbd.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/admin-guide/blockdev/nbd.rst +1 -1
index d78dfe559dcf..faf2ac4b1509 100644
@@ -14,7 +14,7 @@ to borrow disk space from another computer.
 Unlike NFS, it is possible to put any filesystem on it, etc.
 
 For more information, or to download the nbd-client and nbd-server
-tools, go to http://nbd.sf.net/.
+tools, go to https://github.com/NetworkBlockDevice/nbd.
 
 The nbd kernel module need only be installed on the client
 system, as the nbd-server is completely in userspace. In fact,
Signature · 1 line
2.39.2
maintainer Reviewed-by on the v2 series
#11 lore raw
In reply to Eric Blake
Quoted 14 lines · Eric Blake

v1 was here: https://lkml.org/lkml/2023/3/10/1162
since then:
- split original 1/3 into 1/5 and 5/5
- new patch 2/5
- reorder members of anon union
- always send cookie in network order

Eric Blake (5):
uapi nbd: improve doc links to userspace spec block nbd: send handle in network order
uapi nbd: add cookie alias to handle
block nbd: use req.cookie instead of req.handle docs nbd: userspace NBD now favors github over sourceforge

Quoted 7 lines
 Documentation/admin-guide/blockdev/nbd.rst |  2 +-
 drivers/block/nbd.c                        |  6 +++---
 include/uapi/linux/nbd.h                   | 25 +++++++++++++++++-----
 3 files changed, 24 insertions(+), 9 deletions(-)

base-commit: 8d3c682a5e3d9dfc2448ecbb22f4cd48359b9e21

Thanks,

Josef