Skip to contentSkip to conversation

qemu-nbd: only send a limited number of errno codes on the wire

PATCH In review Paolo Bonzini 5 participants 10 messages May 8, 2015 – May 9, 2015

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 qemu-nbd: only send a limited number of errno codes on the wire

Right now, NBD includes potentially platform-specific error values in the wire protocol.

Luckily, most common error values are more or less universal: in particular, of all errno values <= 34 (up to ERANGE), they are all the same on supported platforms except for 11 (which is EAGAIN on Windows and Linux, but EDEADLK on Darwin and the *BSDs). So, in order to guarantee some portability, only keep a handful of possible error codes and squash everything else to EINVAL.

This patch defines a limited set of errno values that are valid for the NBD protocol, and specifies recommendations for what error to return in specific corner cases. The set of errno values is roughly based on the errors listed in the read(2) and write(2) man pages, with some exceptions:

- ENOMEM is added for servers that implement copy-on-write or other formats that require dynamic allocation.

- EDQUOT is not part of the universal set of errors; it can be changed to ENOSPC on the wire format.

- EFBIG is part of the universal set of errors, but it is also changed to ENOSPC because it is pretty similar to ENOSPC or EDQUOT.

Incoming values will in general match system errno values, but not on the Hurd which has different errno values (they have a "subsystem code" equal to 0x10 in bits 24-31). The Hurd is probably not something to which QEMU has been ported, but still do the right thing and reverse-map the NBD errno values to the system errno values.

The corresponding patch to the NBD protocol description can be found at
http://article.gmane.org/gmane.linux.drivers.nbd.general/3154.

---
v1->v2: use #defines and do not expect errnos to match
NBD errnos for the sake of Hurd. Reduced the list of errno values even more. Better commit message explaining how the list was obtained.

 nbd.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
nbd.c +52 -0
index cb1b9bb..57d71b2 100644
@@ -86,6 +86,54 @@
 #define NBD_OPT_ABORT           (2)
 #define NBD_OPT_LIST            (3)
 
+/* NBD errors are based on errno numbers, so there is a 1:1 mapping,
+ * but only a limited set of errno values is specified in the protocol.
+ * Everything else is squashed to EINVAL.
+ */
+#define NBD_EPERM      1
+#define NBD_EIO        5
+#define NBD_ENOMEM     12
+#define NBD_EINVAL     22
+#define NBD_ENOSPC     28
+
+static int system_errno_to_nbd_errno(int err)
+{
+    switch (err) {
+    case EPERM:
+        return NBD_EPERM;
+    case EIO:
+        return NBD_EIO;
+    case ENOMEM:
+        return NBD_ENOMEM;
+#ifdef EDQUOT
+    case EDQUOT:
+#endif
+    case EFBIG:
+    case ENOSPC:
+        return NBD_ENOSPC;
+    case EINVAL:
+    default:
+        return NBD_EINVAL;
+    }
+}
+
+static int nbd_errno_to_system_errno(int err)
+{
+    switch (err) {
+    case NBD_EPERM:
+        return EPERM;
+    case NBD_EIO:
+        return EIO;
+    case NBD_ENOMEM:
+        return ENOMEM;
+    case NBD_ENOSPC:
+        return ENOSPC;
+    case NBD_EINVAL:
+    default:
+        return EINVAL;
+    }
+}
+
 /* Definitions for opaque data types */
 
 typedef struct NBDRequest NBDRequest;
@@ -856,6 +904,8 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
     reply->error  = be32_to_cpup((uint32_t*)(buf + 4));
     reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
 
+    reply->error = nbd_errno_to_system_errno(reply->error);
+
     TRACE("Got reply: "
           "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
           magic, reply->error, reply->handle);
@@ -872,6 +922,8 @@ static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
     uint8_t buf[NBD_REPLY_SIZE];
     ssize_t ret;
 
+    reply->error = system_errno_to_nbd_errno(reply->error);
+
     /* Reply
        [ 0 ..  3]    magic   (NBD_REPLY_MAGIC)
        [ 4 ..  7]    error   (0 == no error)
Signature · 1 line
2.3.5
Reviewed-by on v2 1/1
#1 lore raw
Quoted 16 lines · Paolo Bonzini

Right now, NBD includes potentially platform-specific error values in the wire protocol.

Luckily, most common error values are more or less universal: in particular, of all errno values <= 34 (up to ERANGE), they are all the same on supported platforms except for 11 (which is EAGAIN on Windows and Linux, but EDEADLK on Darwin and the *BSDs). So, in order to guarantee some portability, only keep a handful of possible error codes and squash everything else to EINVAL.

This patch defines a limited set of errno values that are valid for the NBD protocol, and specifies recommendations for what error to return in specific corner cases. The set of errno values is roughly based on the errors listed in the read(2) and write(2) man pages, with some exceptions:

Quoted 5 lines

- ENOMEM is added for servers that implement copy-on-write or other formats that require dynamic allocation.

- EDQUOT is not part of the universal set of errors; it can be changed to ENOSPC on the wire format.

Makes sense.

Quoted 2 lines

- EFBIG is part of the universal set of errors, but it is also changed to ENOSPC because it is pretty similar to ENOSPC or EDQUOT.

Perhaps debatable, but I defer to your judgement.

Quoted 65 lines

Incoming values will in general match system errno values, but not on the Hurd which has different errno values (they have a "subsystem code" equal to 0x10 in bits 24-31). The Hurd is probably not something to which QEMU has been ported, but still do the right thing and reverse-map the NBD errno values to the system errno values.

The corresponding patch to the NBD protocol description can be found at
http://article.gmane.org/gmane.linux.drivers.nbd.general/3154.

---
v1->v2: use #defines and do not expect errnos to match
NBD errnos for the sake of Hurd. Reduced the list of errno values even more. Better commit message explaining how the list was obtained.

 nbd.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
nbd.c +38 -0
index cb1b9bb..57d71b2 100644
@@ -86,6 +86,54 @@
 #define NBD_OPT_ABORT           (2)
 #define NBD_OPT_LIST            (3)
 
+/* NBD errors are based on errno numbers, so there is a 1:1 mapping,
+ * but only a limited set of errno values is specified in the protocol.
+ * Everything else is squashed to EINVAL.
+ */
+#define NBD_EPERM      1
+#define NBD_EIO        5
+#define NBD_ENOMEM     12
+#define NBD_EINVAL     22
+#define NBD_ENOSPC     28
+
+static int system_errno_to_nbd_errno(int err)
+{
+    switch (err) {
+    case EPERM:
+        return NBD_EPERM;
+    case EIO:
+        return NBD_EIO;
+    case ENOMEM:
+        return NBD_ENOMEM;
+#ifdef EDQUOT
+    case EDQUOT:
+#endif
+    case EFBIG:
+    case ENOSPC:
+        return NBD_ENOSPC;
+    case EINVAL:
+    default:
+        return NBD_EINVAL;
+    }
+}
+
+static int nbd_errno_to_system_errno(int err)
+{
+    switch (err) {
+    case NBD_EPERM:
+        return EPERM;
+    case NBD_EIO:
+        return EIO;

nbd.c line 136

Quoted 10 lines
nbd.c +10 -0
+    case NBD_ENOMEM:
+        return ENOMEM;
+    case NBD_ENOSPC:
+        return ENOSPC;
+    case NBD_EINVAL:
+    default:
+        return EINVAL;
+    }
+}
+

If we reach default, something's amiss, isn't it? Worth logging something then?

Quoted 13 lines
nbd.c +2 -0
 /* Definitions for opaque data types */
 
 typedef struct NBDRequest NBDRequest;
@@ -856,6 +904,8 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
     reply->error  = be32_to_cpup((uint32_t*)(buf + 4));
     reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
 
+    reply->error = nbd_errno_to_system_errno(reply->error);
+
     TRACE("Got reply: "
           "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
           magic, reply->error, reply->handle);

nbd.c static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply) · line 929

Quoted 8 lines
nbd.c +2 -0
@@ -872,6 +922,8 @@ static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
     uint8_t buf[NBD_REPLY_SIZE];
     ssize_t ret;
 
+    reply->error = system_errno_to_nbd_errno(reply->error);
+
     /* Reply
        [ 0 ..  3]    magic   (NBD_REPLY_MAGIC)
        [ 4 ..  7]    error   (0 == no error)
#2 lore raw
Quoted 2 lines · Markus Armbruster
Quoted 2 lines

The corresponding patch to the NBD protocol description can be found at
http://article.gmane.org/gmane.linux.drivers.nbd.general/3154.

[...]

Quoted 4 lines
Quoted 2 lines

- EFBIG is part of the universal set of errors, but it is also changed to ENOSPC because it is pretty similar to ENOSPC or EDQUOT.

Perhaps debatable, but I defer to your judgement.

EFBIG is weird anyway, and requires you (or your parents) to ignore SIGXFSZ. A simpler protocol puts fewer requirements on the client.

(In fact, we probably should ignore SIGXFSZ in QEMU and treat EFBIG like ENOSPC everywhere. Should doesn't mean that it will get on anyone's todo list or priority list...).

Quoted 12 lines
Quoted 12 lines
nbd.c +12 -0
+static int nbd_errno_to_system_errno(int err)
+{
+    switch (err) {
+    case NBD_EPERM:
+        return EPERM;
+    case NBD_EIO:
+        return EIO;
+    case NBD_ENOMEM:
+        return ENOMEM;
+    case NBD_ENOSPC:
+        return ENOSPC;
+    case NBD_EINVAL:
Quoted 8 lines

nbd.c line 136

Quoted 5 lines
nbd.c +5 -0
+    default:
+        return EINVAL;
+    }
+}
+

If we reach default, something's amiss, isn't it? Worth logging something then?

Not necessarily. You could have an older NBD server on the other side, and then the errno code might only be valid on another platform!

In fact, if the NBD server is qemu-nbd, the producer of the error could be any driver in block/ for any old version of QEMU. I would not be surprised if an EPIPE or ENOTCONN sneaked in somehow.

And there are probably other NBD servers around. CCing Rich Jones so that he can fix nbdkit.

Paolo

#3 lore raw
Quoted 2 lines · Paolo Bonzini

And there are probably other NBD servers around. CCing Rich Jones so that he can fix nbdkit.

Thanks. FWIW currently nbdkit will send any arbitrary errno returned by a system call, and it doesn't do any platform-specific encoding.

I've added this to the to-do list.

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v
#4 lore raw
In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini
Quoted 2 lines · Markus Armbruster
Quoted 2 lines

The corresponding patch to the NBD protocol description can be found at
http://article.gmane.org/gmane.linux.drivers.nbd.general/3154.

[...]

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

- EFBIG is part of the universal set of errors, but it is also changed to ENOSPC because it is pretty similar to ENOSPC or EDQUOT.

Perhaps debatable, but I defer to your judgement.

EFBIG is weird anyway, and requires you (or your parents) to ignore SIGXFSZ. A simpler protocol puts fewer requirements on the client.

Not only that, but if I understand correctly, the only way to trigger SIGXFSZ or EFBIG is to compile your application to use a smaller off_t than the maximum the system supports. That is, it is impossible on 64-bit Linux, and on 32-bit Linux is it possible only when you use 32-bit off_t instead of 64-bit off_t. But we are dealing with guest disk images, and practically require 64-bit off_t to make it useful for all but the oldest of guests.

Quoted 4 lines

(In fact, we probably should ignore SIGXFSZ in QEMU and treat EFBIG like ENOSPC everywhere. Should doesn't mean that it will get on anyone's todo list or priority list...).

and if I'm right above, it wouldn't make a difference anyway.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v2 1/1
#5 lore raw
In reply to Paolo Bonzini
Quoted 20 lines · Paolo Bonzini

Right now, NBD includes potentially platform-specific error values in the wire protocol.

Luckily, most common error values are more or less universal: in particular, of all errno values <= 34 (up to ERANGE), they are all the same on supported platforms except for 11 (which is EAGAIN on Windows and Linux, but EDEADLK on Darwin and the *BSDs). So, in order to guarantee some portability, only keep a handful of possible error codes and squash everything else to EINVAL.

nbd.c +9 -0
+static int system_errno_to_nbd_errno(int err)
+{
+    switch (err) {
+    case EPERM:
+        return NBD_EPERM;
+    case EIO:
+        return NBD_EIO;
+    case ENOMEM:
+        return NBD_ENOMEM;

nbd.c line 117

Quoted 10 lines
nbd.c +10 -0
+#ifdef EDQUOT
+    case EDQUOT:
+#endif
+    case EFBIG:
+    case ENOSPC:
+        return NBD_ENOSPC;
+    case EINVAL:
+    default:
+        return NBD_EINVAL;
+    }

Do we also want to handle "case 0: return 0;" on either conversion, or even "case 0: abort();" to ensure that callers are using these helpers correctly?

But I can also live with it as is;

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw
Quoted 27 lines · Eric Blake
Quoted 26 lines · Paolo Bonzini

Right now, NBD includes potentially platform-specific error values in the wire protocol.

Luckily, most common error values are more or less universal: in particular, of all errno values <= 34 (up to ERANGE), they are all the same on supported platforms except for 11 (which is EAGAIN on Windows and Linux, but EDEADLK on Darwin and the *BSDs). So, in order to guarantee some portability, only keep a handful of possible error codes and squash everything else to EINVAL.

nbd.c +15 -0
+static int system_errno_to_nbd_errno(int err)
+{
+    switch (err) {
+    case EPERM:
+        return NBD_EPERM;
+    case EIO:
+        return NBD_EIO;
+    case ENOMEM:
+        return NBD_ENOMEM;
+#ifdef EDQUOT
+    case EDQUOT:
+#endif
+    case EFBIG:
+    case ENOSPC:
+        return NBD_ENOSPC;
Quoted 8 lines

nbd.c line 117

Quoted 4 lines
nbd.c +4 -0
+    case EINVAL:
+    default:
+        return NBD_EINVAL;
+    }

Do we also want to handle "case 0: return 0;" on either conversion, or even "case 0: abort();" to ensure that callers are using these helpers correctly?

Yes, it's much better that way.

Paolo

#7 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Eric Blake
Quoted 3 lines · Paolo Bonzini

Right now, NBD includes potentially platform-specific error values in the wire protocol.

Quoted 5 lines
Quoted 3 lines

Do we also want to handle "case 0: return 0;" on either conversion, or even "case 0: abort();" to ensure that callers are using these helpers correctly?

Yes, it's much better that way.

Thinking about it a bit more: abort() is fine on the sending side, to ensure we aren't putting garbage on the wire; but abort() on the receiving side is a bit risky (we should be handling a corrupted incoming stream gracefully - a malicious sender should not be able to crash us). Of course, once we've detected a corrupted incoming stream, we can't do much for the block device the stream was supposed to represent (perhaps treat it as EIO and declare the device dead), but that's still better than aborting.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#8 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Quoted 5 lines · Eric Blake
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Do we also want to handle "case 0: return 0;" on either conversion, or even "case 0: abort();" to ensure that callers are using these helpers correctly?

Yes, it's much better that way.

Quoted 8 lines

Thinking about it a bit more: abort() is fine on the sending side, to ensure we aren't putting garbage on the wire; but abort() on the receiving side is a bit risky (we should be handling a corrupted incoming stream gracefully - a malicious sender should not be able to crash us). Of course, once we've detected a corrupted incoming stream, we can't do much for the block device the stream was supposed to represent (perhaps treat it as EIO and declare the device dead), but that's still better than aborting.

I've included "case 0: return 0;" in the pull request.

Paolo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBCAAGBQJVTLRxAAoJEL/70l94x66DiU0IAIc9c7XNRGkOzvx8Dr/euTZh urx9OVG4Mwpust8KDmquuBJUv18Xu+omh9AxWOkF5ChZUGBwVxyt0N4sKPreVXK2 zJgObt+5cV4o1FXMb3QcFn1CD7s6+8V8T2QukGTviCsIwRaovwpBQAMuT4N5aJWY BTkCE8GiGTVYiWhV+Uz3UML33j5mXJ4WM2LP11ndsKZpFNMvezwo9iyvQe28EqrI Cj6xz+pLbnOtTu3/Kdf1SMA4FK9loH7/y6fth833TIBE/OIyY+PEtNKoq7TJzc3i /S58dVjXNZAez1Bf0Hb9rMCKsGg/MSv7mGPU5IsLQSKgZ0i154ZzJFxifkXnzYU= =fZNP
-----END PGP SIGNATURE-----

#9 lore raw
In reply to Paolo Bonzini

Hi!

With my GNU Hurd hat on:

Quoted 5 lines · Paolo Bonzini

Right now, NBD includes potentially platform-specific error values in the wire protocol.

This patch [...]

Quoted 8 lines

Incoming values will in general match system errno values, but not on the Hurd which has different errno values (they have a "subsystem code" equal to 0x10 in bits 24-31). The Hurd is probably not something to which QEMU has been ported, but still do the right thing and reverse-map the NBD errno values to the system errno values.

The corresponding patch to the NBD protocol description can be found at
http://article.gmane.org/gmane.linux.drivers.nbd.general/3154.

Thank you! I added this information/links to
<http://darnassus.sceen.net/~hurd-web/hurd/libstore/nbd_store/>.

Grüße,
Thomas