[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RFC PATCH 06/13] nbd: negotiate with named exports
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [RFC PATCH 06/13] nbd: negotiate with named exports |
Date: |
Mon, 27 Aug 2012 17:00:19 +0200 |
Allow negotiation to receive the name of the requested export from
the client. Passing a NULL export to nbd_client_new will cause
the server to send the extended negotiation header. The exp field
is then filled during negotiation.
Signed-off-by: Paolo Bonzini <address@hidden>
---
nbd.c | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file modificato, 140 inserzioni(+), 15 rimozioni(-)
diff --git a/nbd.c b/nbd.c
index 1249548..fe7551d 100644
--- a/nbd.c
+++ b/nbd.c
@@ -234,11 +234,23 @@ int unix_socket_outgoing(const char *path)
return unix_connect(path);
}
-/* Basic flow
+/* Basic flow for negotiation
Server Client
-
Negotiate
+
+ or
+
+ Server Client
+ Negotiate #1
+ Option
+ Negotiate #2
+
+ ----
+
+ followed by
+
+ Server Client
Request
Response
Request
@@ -246,20 +258,110 @@ int unix_socket_outgoing(const char *path)
...
...
Request (type == 2)
+
*/
+static int nbd_receive_options(NBDClient *client)
+{
+ int csock = client->sock;
+ char name[256];
+ uint32_t tmp, length;
+ uint64_t magic;
+ int rc;
+
+ /* Client sends:
+ [ 0 .. 3] reserved (0)
+ [ 4 .. 11] NBD_OPTS_MAGIC
+ [12 .. 15] NBD_OPT_EXPORT_NAME
+ [16 .. 19] length
+ [20 .. xx] export name (length bytes)
+ */
+
+ rc = -EINVAL;
+ if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
+ LOG("read failed");
+ goto fail;
+ }
+ TRACE("Checking reserved");
+ if (tmp != 0) {
+ LOG("Bad reserved received");
+ goto fail;
+ }
+
+ if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
+ LOG("read failed");
+ goto fail;
+ }
+ TRACE("Checking reserved");
+ if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
+ LOG("Bad magic received");
+ goto fail;
+ }
+
+ if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
+ LOG("read failed");
+ goto fail;
+ }
+ TRACE("Checking option");
+ if (tmp != be32_to_cpu(NBD_OPT_EXPORT_NAME)) {
+ LOG("Bad option received");
+ goto fail;
+ }
+
+ if (read_sync(csock, &length, sizeof(length)) != sizeof(length)) {
+ LOG("read failed");
+ goto fail;
+ }
+ TRACE("Checking length");
+ length = be32_to_cpu(length);
+ if (length > 255) {
+ LOG("Bad length received");
+ goto fail;
+ }
+ if (read_sync(csock, name, length) != length) {
+ LOG("read failed");
+ goto fail;
+ }
+ name[length] = '\0';
+
+ client->exp = nbd_export_find(name);
+ if (!client->exp) {
+ LOG("export not found");
+ goto fail;
+ }
+
+ QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
+ TRACE("Option negotiation succeeded.");
+ rc = 0;
+fail:
+ return rc;
+}
+
static int nbd_send_negotiate(NBDClient *client)
{
int csock = client->sock;
char buf[8 + 8 + 8 + 128];
int rc;
+ const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
+ NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
- /* Negotiate
- [ 0 .. 7] passwd ("NBDMAGIC")
- [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
+ /* Negotiation header without options:
+ [ 0 .. 7] passwd ("NBDMAGIC")
+ [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
[16 .. 23] size
- [24 .. 27] flags
- [28 .. 151] reserved (0)
+ [24 .. 25] server flags (0)
+ [24 .. 27] export flags
+ [28 .. 151] reserved (0)
+
+ Negotiation header with options, part 1:
+ [ 0 .. 7] passwd ("NBDMAGIC")
+ [ 8 .. 15] magic (NBD_OPTS_MAGIC)
+ [16 .. 17] server flags (0)
+
+ part 2 (after options are sent):
+ [18 .. 25] size
+ [26 .. 27] export flags
+ [28 .. 151] reserved (0)
*/
socket_set_block(csock);
@@ -267,16 +369,39 @@ static int nbd_send_negotiate(NBDClient *client)
TRACE("Beginning negotiation.");
memcpy(buf, "NBDMAGIC", 8);
- cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC);
- cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size);
- cpu_to_be32w((uint32_t*)(buf + 24),
- client->exp->nbdflags | NBD_FLAG_HAS_FLAGS |
NBD_FLAG_SEND_TRIM |
- NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
+ if (client->exp) {
+ assert ((client->exp->nbdflags & ~65535) == 0);
+ cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC);
+ cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size);
+ cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
+ } else {
+ cpu_to_be64w((uint64_t*)(buf + 8), NBD_OPTS_MAGIC);
+ }
memset(buf + 28, 0, 124);
- if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
- LOG("write failed");
- goto fail;
+ if (client->exp) {
+ if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
+ LOG("write failed");
+ goto fail;
+ }
+ } else {
+ if (write_sync(csock, buf, 18) != 18) {
+ LOG("write failed");
+ goto fail;
+ }
+ rc = nbd_receive_options(client);
+ if (rc < 0) {
+ LOG("option negotiation failed");
+ goto fail;
+ }
+
+ assert ((client->exp->nbdflags & ~65535) == 0);
+ cpu_to_be64w((uint64_t*)(buf + 18), client->exp->size);
+ cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
+ if (write_sync(csock, buf + 18, sizeof(buf) - 18) != sizeof(buf) - 18)
{
+ LOG("write failed");
+ goto fail;
+ }
}
TRACE("Negotiation succeeded.");
--
1.7.11.2
- [Qemu-devel] [RFC PATCH 00/13] Embedded NBD server, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 01/13] nbd: add more constants, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 03/13] nbd: do not leak nbd_trip coroutines when a connection is torn down, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 04/13] nbd: close all clients on deleting export, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 02/13] nbd: pass NBDClient to nbd_send_negotiate, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 05/13] nbd: register named exports, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 08/13] qemu-sockets: publish dummy_opts, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 07/13] nbd: do not close BlockDriverState in nbd_export_close, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 06/13] nbd: negotiate with named exports,
Paolo Bonzini <=
- [Qemu-devel] [RFC PATCH 10/13] qemu-sockets: make inet_parse public, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 09/13] qmp: add NBD server commands, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 11/13] hmp: add NBD server commands, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 13/13] nbd: add notifier to close exports when the image is closed, Paolo Bonzini, 2012/08/27
- [Qemu-devel] [RFC PATCH 12/13] block: add close notifiers, Paolo Bonzini, 2012/08/27