Skip to contentSkip to conversation

nbd: generalize usage of nbd_read

RFC In review Vladimir Sementsov-Ogievskiy 2 participants 2 messages Jan 16, 2019 – Jan 17, 2019 Block layer

lore
Share & tools
lore
#0 lore raw

RFC 1/1 nbd: generalize usage of nbd_read

We generally do very similar things around nbd_read: error_prepend, specifying, what we have tried to read and be_to_cpu conversion of integers.

So, it seems reasonable to move common things to helper functions, which:
1. simplify code a bit
2. generalize nbd_read error descriptions, all starting with "Failed to read"
3. make it more difficult to forget to convert things from BE

Possible TODO:
make a helper to read sized variable buffers, like

uint32_t len
@len bytes buffer follows

---

Hi Eric!

I have an idea of several helpers around nbd_read, what do you think about it?

It now based on your "nbd: add qemu-nbd --list", and if we like it, I'll fix iotests appropriately (if they fail, but I think, they should) and rebase onto final version of your series.

Based-on:<[email protected]>

 include/block/nbd.h | 33 ++++++++++++++++-
 block/nbd-client.c  |  5 +--
 nbd/client.c        | 89 ++++++++++++++-------------------------------
 nbd/common.c        |  2 +-
 nbd/server.c        | 27 +++++---------
 5 files changed, 71 insertions(+), 85 deletions(-)
include/block/nbd.h +31 -2
index 80ee3cc997..f3d3ffc749 100644
@@ -23,6 +23,7 @@
 #include "qapi/qapi-types-block.h"
 #include "io/channel-socket.h"
 #include "crypto/tlscreds.h"
+#include "qapi/error.h"
 
 /* Handshake phase structs - this struct is passed on the wire */
 
@@ -336,11 +337,39 @@ void nbd_server_start(SocketAddress *addr, const char *tls_creds,
  * Reads @size bytes from @ioc. Returns 0 on success.
  */
 static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,
-                           Error **errp)
+                           const char *desc, Error **errp)
 {
-    return qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
+    int ret = qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
+
+    if (ret < 0) {
+        if (desc) {
+            error_prepend(errp, "Failed to read %s: ", desc);
+        } else {
+            error_prepend(errp, "Failed to read from socket: ");
+        }
+        return -1;
+    }
+
+    return 0;
 }
 
+#define DEF_NBD_READ(bits) \
+static inline int nbd_read ## bits(QIOChannel *ioc, uint ## bits ## _t *val, \
+                                   const char *desc, Error **errp)           \
+{                                                                            \
+    if (nbd_read(ioc, val, sizeof(*val), desc, errp) < 0) {                  \
+        return -1;                                                           \
+    }                                                                        \
+    *val = be ## bits ## _to_cpu(*val);                                      \
+    return 0;                                                                \
+}
+
+DEF_NBD_READ(16)
+DEF_NBD_READ(32)
+DEF_NBD_READ(64)
+
+#undef DEF_NBD_READ
+
 static inline bool nbd_reply_is_simple(NBDReply *reply)
 {
     return reply->magic == NBD_SIMPLE_REPLY_MAGIC;
block/nbd-client.c +2 -3
index 813539676d..5c97052608 100644
@@ -337,10 +337,9 @@ static int nbd_co_receive_offset_data_payload(NBDClientSession *s,
         return -EINVAL;
     }
 
-    if (nbd_read(s->ioc, &offset, sizeof(offset), errp) < 0) {
+    if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) {
         return -EIO;
     }
-    be64_to_cpus(&offset);
 
     data_size = chunk->length - sizeof(offset);
     assert(data_size);
@@ -387,7 +386,7 @@ static coroutine_fn int nbd_co_receive_structured_payload(
     }
 
     *payload = g_new(char, len);
-    ret = nbd_read(s->ioc, *payload, len, errp);
+    ret = nbd_read(s->ioc, *payload, len, "structured payload", errp);
     if (ret < 0) {
         g_free(*payload);
         *payload = NULL;
nbd/client.c +28 -61

247 lines not on this page · raw message

nbd/common.c +1 -1
index 41f5ed8d9f..cc8b278e54 100644
@@ -31,7 +31,7 @@ int nbd_drop(QIOChannel *ioc, size_t size, Error **errp)
     buffer = sizeof(small) >= size ? small : g_malloc(MIN(65536, size));
     while (size > 0) {
         ssize_t count = MIN(65536, size);
-        ret = nbd_read(ioc, buffer, MIN(65536, size), errp);
+        ret = nbd_read(ioc, buffer, MIN(65536, size), NULL, errp);
 
         if (ret < 0) {
             goto cleanup;
nbd/server.c +9 -18
index 15357d40fd..97a38de40c 100644
@@ -438,8 +438,7 @@ static int nbd_negotiate_handle_export_name(NBDClient *client,
         error_setg(errp, "Bad length received");
         return -EINVAL;
     }
-    if (nbd_read(client->ioc, name, client->optlen, errp) < 0) {
-        error_prepend(errp, "read failed: ");
+    if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) {
         return -EIO;
     }
     name[client->optlen] = '\0';
@@ -1046,11 +1045,9 @@ static int nbd_negotiate_options(NBDClient *client, uint16_t myflags,
         ...           Rest of request
     */
 
-    if (nbd_read(client->ioc, &flags, sizeof(flags), errp) < 0) {
-        error_prepend(errp, "read failed: ");
+    if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) {
         return -EIO;
     }
-    flags = be32_to_cpu(flags);
     trace_nbd_negotiate_options_flags(flags);
     if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
         fixedNewstyle = true;
@@ -1070,30 +1067,23 @@ static int nbd_negotiate_options(NBDClient *client, uint16_t myflags,
         uint32_t option, length;
         uint64_t magic;
 
-        if (nbd_read(client->ioc, &magic, sizeof(magic), errp) < 0) {
-            error_prepend(errp, "read failed: ");
+        if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) {
             return -EINVAL;
         }
-        magic = be64_to_cpu(magic);
         trace_nbd_negotiate_options_check_magic(magic);
         if (magic != NBD_OPTS_MAGIC) {
             error_setg(errp, "Bad magic received");
             return -EINVAL;
         }
 
-        if (nbd_read(client->ioc, &option,
-                     sizeof(option), errp) < 0) {
-            error_prepend(errp, "read failed: ");
+        if (nbd_read32(client->ioc, &option, "option", errp) < 0) {
             return -EINVAL;
         }
-        option = be32_to_cpu(option);
         client->opt = option;
 
-        if (nbd_read(client->ioc, &length, sizeof(length), errp) < 0) {
-            error_prepend(errp, "read failed: ");
+        if (nbd_read32(client->ioc, &length, "option length", errp) < 0) {
             return -EINVAL;
         }
-        length = be32_to_cpu(length);
         assert(!client->optlen);
         client->optlen = length;
 
@@ -1306,7 +1296,7 @@ static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
     uint32_t magic;
     int ret;
 
-    ret = nbd_read(ioc, buf, sizeof(buf), errp);
+    ret = nbd_read(ioc, buf, sizeof(buf), "request", errp);
     if (ret < 0) {
         return ret;
     }
@@ -2110,8 +2100,9 @@ static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
         }
     }
     if (request->type == NBD_CMD_WRITE) {
-        if (nbd_read(client->ioc, req->data, request->len, errp) < 0) {
-            error_prepend(errp, "reading from socket failed: ");
+        if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data",
+                     errp) < 0)
+        {
             return -EIO;
         }
         req->complete = true;
Signature · 1 line
2.18.0
#1 lore raw
Quoted 56 lines · Vladimir Sementsov-Ogievskiy

We generally do very similar things around nbd_read: error_prepend, specifying, what we have tried to read and be_to_cpu conversion of integers.

So, it seems reasonable to move common things to helper functions, which:
1. simplify code a bit
2. generalize nbd_read error descriptions, all starting with "Failed to read"
3. make it more difficult to forget to convert things from BE

Possible TODO:
make a helper to read sized variable buffers, like

uint32_t len
@len bytes buffer follows

---

Hi Eric!

I have an idea of several helpers around nbd_read, what do you think about it?

It now based on your "nbd: add qemu-nbd --list", and if we like it, I'll fix iotests appropriately (if they fail, but I think, they should) and rebase onto final version of your series.

Based-on:<[email protected]>

 include/block/nbd.h | 33 ++++++++++++++++-
 block/nbd-client.c  |  5 +--
 nbd/client.c        | 89 ++++++++++++++-------------------------------
 nbd/common.c        |  2 +-
 nbd/server.c        | 27 +++++---------
 5 files changed, 71 insertions(+), 85 deletions(-)
include/block/nbd.h +2 -1
index 80ee3cc997..f3d3ffc749 100644
@@ -23,6 +23,7 @@
 #include "qapi/qapi-types-block.h"
 #include "io/channel-socket.h"
 #include "crypto/tlscreds.h"
+#include "qapi/error.h"
 
 /* Handshake phase structs - this struct is passed on the wire */
 
@@ -336,11 +337,39 @@ void nbd_server_start(SocketAddress *addr, const char *tls_creds,
  * Reads @size bytes from @ioc. Returns 0 on success.
  */
 static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,
-                           Error **errp)
+                           const char *desc, Error **errp)

include/block/nbd.h void nbd_server_start(SocketAddress *addr, const char *tls_creds, · line 349

Quoted 10 lines
include/block/nbd.h +8 -1
 {
-    return qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
+    int ret = qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
+
+    if (ret < 0) {
+        if (desc) {
+            error_prepend(errp, "Failed to read %s: ", desc);
+        } else {
+            error_prepend(errp, "Failed to read from socket: ");
+        }

Do we need the error_prepend() at all when there is no desc? For that matter, does prepending "Failed to read..." add any useful information to the error message in the first place?

Quoted 10 lines
include/block/nbd.h +8 -0
+        return -1;
+    }
+
+    return 0;
 }
 
+#define DEF_NBD_READ(bits) \
+static inline int nbd_read ## bits(QIOChannel *ioc, uint ## bits ## _t *val, \
+                                   const char *desc, Error **errp)           \
+{                                                                            \

include/block/nbd.h void nbd_server_start(SocketAddress *addr, const char *tls_creds, · line 369

Quoted 10 lines
include/block/nbd.h +10 -0
+    if (nbd_read(ioc, val, sizeof(*val), desc, errp) < 0) {                  \
+        return -1;                                                           \
+    }                                                                        \
+    *val = be ## bits ## _to_cpu(*val);                                      \
+    return 0;                                                                \
+}
+
+DEF_NBD_READ(16)
+DEF_NBD_READ(32)
+DEF_NBD_READ(64)

I'd write:

DEF_NBD_READ(16) /* nbd_read16 */

to aid grep-ability of the source code when looking where the function is defined.

Quoted 11 lines
include/block/nbd.h +3 -0
+
+#undef DEF_NBD_READ
+
 static inline bool nbd_reply_is_simple(NBDReply *reply)
 {
     return reply->magic == NBD_SIMPLE_REPLY_MAGIC;
block/nbd-client.c +0 -0
index 813539676d..5c97052608 100644

block/nbd-client.c static int nbd_co_receive_offset_data_payload(NBDClientSession *s, · line 343

Quoted 8 lines
block/nbd-client.c +1 -2
@@ -337,10 +337,9 @@ static int nbd_co_receive_offset_data_payload(NBDClientSession *s,
         return -EINVAL;
     }
 
-    if (nbd_read(s->ioc, &offset, sizeof(offset), errp) < 0) {
+    if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) {
         return -EIO;
     }
-    be64_to_cpus(&offset);

The idea seems to be worthwhile; the callsites are indeed shorter when using the new wrappers.

block/nbd-client.c static coroutine_fn int nbd_co_receive_structured_payload( · line 389

Quoted 9 lines
block/nbd-client.c +1 -1
     data_size = chunk->length - sizeof(offset);
     assert(data_size);
@@ -387,7 +386,7 @@ static coroutine_fn int nbd_co_receive_structured_payload(
     }
 
     *payload = g_new(char, len);
-    ret = nbd_read(s->ioc, *payload, len, errp);
+    ret = nbd_read(s->ioc, *payload, len, "structured payload", errp);

Okay, so maybe prepending DOES help, in letting you know which part of the message was being read.

block/nbd-client.c static int nbd_handle_reply_err(QIOChannel *ioc, NBDOptionReply *reply, · line 168

Quoted 9 lines
block/nbd-client.c +1 -4
@@ -166,10 +165,7 @@ static int nbd_handle_reply_err(QIOChannel *ioc, NBDOptionReply *reply,
             goto cleanup;
         }
         msg = g_malloc(reply->length + 1);
-        if (nbd_read(ioc, msg, reply->length, errp) < 0) {
-            error_prepend(errp, "failed to read option error %" PRIu32
-                          " (%s) message: ",
-                          reply->type, nbd_rep_lookup(reply->type));
+        if (nbd_read(ioc, msg, reply->length, "option error", errp) < 0) {

On the other hand, the text being prepended here loses some information compared to what it previously had. If none of the iotests are triggering this particular failure path, it's hard to argue that anyone is hitting it enough to notice the difference, at which point is it even worth prepending anything if the error is unlikely.

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