Embedded NBD server
cover Embedded NBD server
Hi all,
this is an RFC series implementing an NBD server embedded inside QEMU. This can be used in various cases, including migration with non-shared storage.
Three new commands are introduced at the QMP level
{ 'command': 'nbd-server-start', 'data': { 'addr': 'IPSocketAddress' } } { 'command': 'nbd-server-add', 'data': {'device': 'str', '*writable': 'bool'} } { 'command': 'nbd-server-stop' }
At the HMP level there is no nbd_server_add command. nbd_server_start automatically exposes all of the VM's block devices, and an option -w makes them writable.
The NBD server exports multiple devices on a single port; they can be accessed using "nbd:HOST:PART:exportname=NAME".
The patches are mostly boring touching nbd.c. The part where I need a second opinion and/or ack is patch 12 and 13. They fix the case of a disk being unplugged while NBD export is active. To do this I add a NotifierList to a BlockDriverState. Does this look okay, or is it too ad hoc?
Paolo Bonzini (13):
nbd: add more constants
nbd: pass NBDClient to nbd_send_negotiate
nbd: do not leak nbd_trip coroutines when a connection is torn down
nbd: close all clients on deleting export
nbd: register named exports
nbd: negotiate with named exports
nbd: do not close BlockDriverState in nbd_export_close
qemu-sockets: publish dummy_opts
qmp: add NBD server commands
qemu-sockets: make inet_parse public
hmp: add NBD server commands
block: add close notifiers
nbd: add notifier to close exports when the image is closed
Makefile.objs | 5 +- block.c | 19 +++- block.h | 1 + block_int.h | 2 + blockdev-nbd.c | 131 ++++++++++++++++++++++ hmp-commands.hx | 29 +++++ hmp.c | 66 +++++++++++ hmp.h | 2 + nbd.c | 311 +++++++++++++++++++++++++++++++++++++++++----------- nbd.h | 6 + qapi-schema.json | 69 ++++++++++++ qapi/opts-visitor.c | 48 ++++---- qemu-nbd.c | 1 + qemu-sockets.c | 16 +-- qemu_socket.h | 3 + qmp-commands.hx | 16 +++ 16 file modificati, 621 inserzioni(+), 104 rimozioni(-) create mode 100644 blockdev-nbd.c
Signature · 1 line
1.7.11.2
RFC PATCH 1/13 nbd: add more constants
Avoid magic numbers and magic size computations; hide them behind #defines.
--- nbd.c | 17 ++++++++++-------
1 file modificato, 10 inserzioni(+), 7 rimozioni(-)
nbd.c +10 -7
@@ -57,9 +57,12 @@/* This is all part of the "official" NBD API */ +#define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)#define NBD_REPLY_SIZE (4 + 4 + 8)#define NBD_REQUEST_MAGIC 0x25609513#define NBD_REPLY_MAGIC 0x67446698 +#define NBD_OPTS_MAGIC 0x49484156454F5054LL +#define NBD_CLIENT_MAGIC 0x0000420281861253LL#define NBD_SET_SOCK _IO(0xab, 0)#define NBD_SET_BLKSIZE _IO(0xab, 1)
@@ -213,7 +216,7 @@ static int nbd_send_negotiate(int csock, off_t size, uint32_t flags)/* Negotiate [ 0 .. 7] passwd ("NBDMAGIC") -[ 8 .. 15] magic (0x00420281861253) +[ 8 .. 15] magic (NBD_CLIENT_MAGIC) [16 .. 23] size [24 .. 27] flags [28 .. 151] reserved (0)
@@ -224,7 +227,7 @@ static int nbd_send_negotiate(int csock, off_t size, uint32_t flags)TRACE ("Beginning negotiation." );memcpy (buf,"NBDMAGIC" ,8 ); -cpu_to_be64w ((uint64_t *)(buf +8 ),0x00420281861253LL ); +cpu_to_be64w ((uint64_t *)(buf +8 ),NBD_CLIENT_MAGIC );cpu_to_be64w ((uint64_t *)(buf +16 ), size); cpu_to_be32w((uint32_t*)(buf + 24), flags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
@@ -295,7 +298,7 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,uint32_t namesize;TRACE ("Checking magic (opts_magic)" ); -if (magic !=0x49484156454F5054LL ) { +if (magic !=NBD_OPTS_MAGIC ) {LOG ("Bad magic received" ); goto fail; }
@@ -334,7 +337,7 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags, }else {TRACE ("Checking magic (cli_magic)" ); -if (magic !=0x00420281861253LL ) { +if (magic !=NBD_CLIENT_MAGIC ) {LOG ("Bad magic received" ); goto fail; }
@@ -477,7 +480,7 @@ int nbd_client(int fd)ssize_t nbd_send_request (int csock,struct nbd_request *request) { -uint8_t buf[4 +4 +8 +8 +4 ]; +uint8_t buf[NBD_REQUEST_SIZE ];ssize_t ret;cpu_to_be32w ((uint32_t *)buf,NBD_REQUEST_MAGIC );
@@ -504,7 +507,7 @@ ssize_t nbd_send_request(int csock, struct nbd_request *request)static ssize_t nbd_receive_request (int csock,struct nbd_request *request) { -uint8_t buf[4 +4 +8 +8 +4 ]; +uint8_t buf[NBD_REQUEST_SIZE ];uint32_t magic;ssize_t ret;
@@ -582,7 +585,7 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)static ssize_t nbd_send_reply (int csock,struct nbd_reply *reply) { -uint8_t buf[4 +4 +8 ]; +uint8_t buf[NBD_REPLY_SIZE ];ssize_t ret;/* Reply
Signature · 1 line
1.7.11.2
RFC PATCH 2/13 nbd: pass NBDClient to nbd_send_negotiate
In reply to Paolo BonziniWe will need the NBDClient in nbd_send_negotiate to store the export requested by the client.
--- nbd.c | 76 ++++++++++++++++++++++++++++++++++++-------------------------------
1 file modificato, 41 inserzioni(+), 35 rimozioni(-)
nbd.c +41 -35
@@ -78,6 +78,39 @@#define NBD_OPT_EXPORT_NAME (1 << 0) +/* Definitions for opaque data types */ + +typedef struct NBDRequest NBDRequest ; + +struct NBDRequest { +QSIMPLEQ_ENTRY (NBDRequest) entry; +NBDClient *client; +uint8_t *data; +}; + +struct NBDExport { +BlockDriverState *bs; +off_t dev_offset; +off_t size; +uint32_t nbdflags; + QSIMPLEQ_HEAD(, NBDRequest) requests; +}; + +struct NBDClient { +int refcount; +void (*close)(NBDClient *client); + +NBDExport *exp; +int sock; + +Coroutine *recv_coroutine; + +CoMutex send_lock; +Coroutine *send_coroutine; + +int nb_requests; +}; +/* That's all folks */ ssize_t nbd_wr_sync (int fd,void *buffer,size_t size,bool do_read)
@@ -209,8 +242,9 @@ int unix_socket_outgoing(const char *path)Request (type == 2) */ -static int nbd_send_negotiate (int csock,off_t size,uint32_t flags) +static int nbd_send_negotiate (NBDClient *client) { +int csock = client->sock;char buf[8 +8 +8 +128 ];int rc;
@@ -228,9 +262,9 @@ static int nbd_send_negotiate(int csock, off_t size, uint32_t flags)TRACE ("Beginning negotiation." );memcpy (buf,"NBDMAGIC" ,8 );cpu_to_be64w ((uint64_t *)(buf +8 ),NBD_CLIENT_MAGIC ); -cpu_to_be64w ((uint64_t *)(buf +16 ), size); +cpu_to_be64w ((uint64_t *)(buf +16 ), client->exp->size);cpu_to_be32w ((uint32_t *)(buf +24 ), - flags |NBD_FLAG_HAS_FLAGS |NBD_FLAG_SEND_TRIM | + client->exp->nbdflags |NBD_FLAG_HAS_FLAGS |NBD_FLAG_SEND_TRIM |NBD_FLAG_SEND_FLUSH |NBD_FLAG_SEND_FUA );memset (buf +28 ,0 ,124 );
@@ -615,35 +649,6 @@ static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)typedef struct NBDRequest NBDRequest ; -struct NBDRequest { -QSIMPLEQ_ENTRY (NBDRequest) entry; -NBDClient *client; -uint8_t *data; -}; - -struct NBDExport { -BlockDriverState *bs; -off_t dev_offset; -off_t size; -uint32_t nbdflags; - QSIMPLEQ_HEAD(, NBDRequest) requests; -}; - -struct NBDClient { -int refcount; -void (*close)(NBDClient *client); - -NBDExport *exp; -int sock; - -Coroutine *recv_coroutine; - -CoMutex send_lock; -Coroutine *send_coroutine; - -int nb_requests; -}; -static void nbd_client_get (NBDClient *client) { client->refcount++;
@@ -977,13 +982,14 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock, void (*close)(NBDClient *)) {NBDClient *client; -if (nbd_send_negotiate (csock, exp->size, exp->nbdflags) <0 ) { -return NULL ; - } client =g_malloc0 (sizeof (NBDClient)); client->refcount =1 ; client->exp = exp; client->sock = csock; +if (nbd_send_negotiate (client) <0 ) { +g_free (client); +return NULL ; + } client->close = close;qemu_co_mutex_init (&client->send_lock);qemu_set_fd_handler2 (csock, nbd_can_read, nbd_read,NULL , client);
Signature · 1 line
1.7.11.2
RFC PATCH 3/13 nbd: do not leak nbd_trip coroutines when a connection is torn down
In reply to Paolo BonziniBecause nbd_client_close removes the I/O handlers for the client socket, there is no way that any suspended coroutines are restarted. This will be a problem with the QEMU embedded NBD server, because we will have a QMP command to forcibly close all connections with the clients.
Instead, we can exploit the reference counting of NBDClients; shutdown the client socket, which will make it readable and writeable. The coroutines then will fail and exit cleanly. The last refcount finally triggers the closure of the client.
--- nbd.c | 18 +++++++++++------- nbd.h | 3 +++
2 file modificati, 14 inserzioni(+), 7 rimozioni(-)
nbd.c +11 -7
@@ -657,18 +657,22 @@ static void nbd_client_get(NBDClient *client)static void nbd_client_put (NBDClient *client) {if (--client->refcount ==0 ) { +qemu_set_fd_handler2 (client->sock,NULL ,NULL ,NULL ,NULL ); +close (client->sock); + client->sock =-1 ; +if (client->close) { + client->close (client); + }g_free (client); } } -static void nbd_client_close (NBDClient *client) +void nbd_client_close (NBDClient *client) { -qemu_set_fd_handler2 (client->sock,NULL ,NULL ,NULL ,NULL ); -close (client->sock); - client->sock =-1 ; -if (client->close) { - client->close (client); - } +/* Force requests to finish. They will drop their own references, +* then we'll close the socket and free the NBDClient. +*/ +shutdown (client->sock,2 );nbd_client_put (client); }
nbd.h +3 -0
@@ -81,7 +81,10 @@ typedef struct NBDClient NBDClient;NBDExport *nbd_export_new (BlockDriverState *bs,off_t dev_offset,off_t size,uint32_t nbdflags);void nbd_export_close (NBDExport *exp); +NBDClient *nbd_client_new (NBDExport *exp,int csock,void (*close)(NBDClient *)); +void nbd_client_close (NBDClient *client); +#endif
Signature · 1 line
1.7.11.2
RFC PATCH 4/13 nbd: close all clients on deleting export
In reply to Paolo BonziniClients have a pointer to the NBDExport that they serve. Do not let a dangling pointer escape. Also flush all pending I/O so that coroutines are forced to exit.
--- nbd.c | 14 ++++++++++++++
1 file modificato, 14 inserzioni(+)
nbd.c +14 -0
@@ -93,6 +93,7 @@ struct NBDExport {off_t dev_offset;off_t size;uint32_t nbdflags; + QTAILQ_HEAD(, NBDClient) clients; QSIMPLEQ_HEAD(, NBDRequest) requests; };
@@ -108,6 +109,7 @@ struct NBDClient {CoMutex send_lock;Coroutine *send_coroutine; +QTAILQ_ENTRY (NBDClient) next;int nb_requests; };
@@ -658,6 +660,9 @@ static void nbd_client_put(NBDClient *client) {if (--client->refcount ==0 ) {qemu_set_fd_handler2 (client->sock,NULL ,NULL ,NULL ,NULL ); +if (client->exp) { +QTAILQ_REMOVE (&client->exp->clients, client, next); + }close (client->sock); client->sock =-1 ;if (client->close) {
@@ -711,6 +716,7 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, {NBDExport *exp =g_malloc0 (sizeof (NBDExport));QSIMPLEQ_INIT (&exp->requests); +QTAILQ_INIT (&exp->clients); exp->bs = bs; exp->dev_offset = dev_offset; exp->nbdflags = nbdflags;
@@ -720,6 +726,11 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,void nbd_export_close (NBDExport *exp) { +while (!QTAILQ_EMPTY (&exp->clients)) { +NBDClient *client =QTAILQ_FIRST (&exp->clients); +nbd_client_close (client); + } +while (!QSIMPLEQ_EMPTY (&exp->requests)) {NBDRequest *first =QSIMPLEQ_FIRST (&exp->requests);QSIMPLEQ_REMOVE_HEAD (&exp->requests, entry);
@@ -995,6 +1006,9 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock,return NULL ; } client->close = close; +if (exp) { +QTAILQ_INSERT_TAIL (&exp->clients, client, next); + }qemu_co_mutex_init (&client->send_lock);qemu_set_fd_handler2 (csock, nbd_can_read, nbd_read,NULL , client);return client;
Signature · 1 line
1.7.11.2
RFC PATCH 5/13 nbd: register named exports
In reply to Paolo BonziniAdd an API to register and find named exports.
--- nbd.c | 38 ++++++++++++++++++++++++++++++++++++++ nbd.h | 3 +++
2 file modificati, 41 inserzioni(+)
nbd.c +38 -0
@@ -90,13 +90,17 @@ struct NBDRequest {struct NBDExport {BlockDriverState *bs; +char *name;off_t dev_offset;off_t size;uint32_t nbdflags; QTAILQ_HEAD(, NBDClient) clients; QSIMPLEQ_HEAD(, NBDRequest) requests; +QTAILQ_ENTRY (NBDExport) next; }; +static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports); +struct NBDClient {int refcount;void (*close)(NBDClient *client);
@@ -724,6 +728,27 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,return exp; } +NBDExport *nbd_export_find (const char *name) +{ +NBDExport *exp; + QTAILQ_FOREACH(exp, &exports, next) { +if (strcmp (name, exp->name) ==0 ) { +return exp; + } + } + +return NULL ; +} + +void nbd_export_set_name (NBDExport *exp,const char *name) +{ +assert (exp->name ==NULL ); +assert (!nbd_export_find (name)); + + exp->name =g_strdup (name); +QTAILQ_INSERT_TAIL (&exports, exp, next); +} +void nbd_export_close (NBDExport *exp) {while (!QTAILQ_EMPTY (&exp->clients)) {
@@ -738,10 +763,23 @@ void nbd_export_close(NBDExport *exp)g_free (first); } +if (exp->name) { +QTAILQ_REMOVE (&exports, exp, next); +g_free (exp->name); + } +bdrv_close (exp->bs);g_free (exp); } +void nbd_export_close_all (void ) +{ +while (!QTAILQ_EMPTY (&exports)) { +NBDExport *export =QTAILQ_FIRST (&exports); +nbd_export_close (export); + } +} +static int nbd_can_read (void *opaque);static void nbd_read (void *opaque);static void nbd_restart_write (void *opaque);
nbd.h +3 -0
@@ -80,7 +80,10 @@ typedef struct NBDClient NBDClient;NBDExport *nbd_export_new (BlockDriverState *bs,off_t dev_offset,off_t size,uint32_t nbdflags); +NBDExport *nbd_export_find (const char *name); +void nbd_export_set_name (NBDExport *exp,const char *name);void nbd_export_close (NBDExport *exp); +void nbd_export_close_all (void );NBDClient *nbd_client_new (NBDExport *exp,int csock,void (*close)(NBDClient *));
Signature · 1 line
1.7.11.2
RFC PATCH 6/13 nbd: negotiate with named exports
In reply to Paolo BonziniAllow 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.
--- nbd.c | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file modificato, 140 inserzioni(+), 15 rimozioni(-)
nbd.c +140 -15
@@ -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." );
Signature · 1 line
1.7.11.2
RFC PATCH 7/13 nbd: do not close BlockDriverState in nbd_export_close
In reply to Paolo BonziniThis is not desirable when embedding the NBD server inside QEMU. Move the bdrv_close to qemu-nbd.
--- nbd.c | 1 - qemu-nbd.c | 1 +
2 file modificati, 1 inserzione(+). 1 rimozione(-)
nbd.c +0 -1
@@ -893,7 +893,6 @@ void nbd_export_close(NBDExport *exp)g_free (exp->name); } -bdrv_close (exp->bs);g_free (exp); }
qemu-nbd.c +1 -0
@@ -586,6 +586,7 @@ int main(int argc, char **argv) }while (!sigterm_reported && (persistent || !nbd_started || nb_fds >0 ));nbd_export_close (exp); +bdrv_close (bs);if (sockpath) {unlink (sockpath); }
Signature · 1 line
1.7.11.2
RFC PATCH 8/13 qemu-sockets: publish dummy_opts
In reply to Paolo BonziniThis is needed so that we can set up a QemuOpts instance from QMP parameters. The way to go here is to and move qemu-sockets.c away from QemuOpts and use Laszlo's QemuOptsVisitor whenever *_opts functions are called now. This can be done later, however.
--- qemu-sockets.c | 14 +++++++------- qemu_socket.h | 2 ++
2 file modificati, 9 inserzioni(+), 7 rimozioni(-)
qemu-sockets.c +7 -7
@@ -32,9 +32,9 @@static const int on=1 , off=0 ;/* used temporarely until all users are converted to QemuOpts */ -static QemuOptsList dummy_opts = { - .name ="dummy" , - .head =QTAILQ_HEAD_INITIALIZER (dummy_opts.head), +QemuOptsList socket_opts = { + .name ="socket" , + .head =QTAILQ_HEAD_INITIALIZER (socket_opts.head), .desc = { { .name ="path" ,
@@ -469,7 +469,7 @@ int inet_listen(const char *str, char *ostr, int olen,char *optstr;int sock =-1 ; - opts =qemu_opts_create (&dummy_opts,NULL ,0 ,NULL ); + opts =qemu_opts_create (&socket_opts,NULL ,0 ,NULL );if (inet_parse (opts, str) ==0 ) { sock =inet_listen_opts (opts, port_offset, errp);if (sock !=-1 && ostr) {
@@ -498,7 +498,7 @@ int inet_connect(const char *str, bool block, bool *in_progress, Error **errp)QemuOpts *opts;int sock =-1 ; - opts =qemu_opts_create (&dummy_opts,NULL ,0 ,NULL ); + opts =qemu_opts_create (&socket_opts,NULL ,0 ,NULL );if (inet_parse (opts, str) ==0 ) {if (block) {qemu_opt_set (opts,"block" ,"on" );
@@ -597,7 +597,7 @@ int unix_listen(const char *str, char *ostr, int olen)char *path, *optstr;int sock, len; - opts =qemu_opts_create (&dummy_opts,NULL ,0 ,NULL ); + opts =qemu_opts_create (&socket_opts,NULL ,0 ,NULL ); optstr =strchr (str,',' );if (optstr) {
@@ -625,7 +625,7 @@ int unix_connect(const char *path)QemuOpts *opts;int sock; - opts =qemu_opts_create (&dummy_opts,NULL ,0 ,NULL ); + opts =qemu_opts_create (&socket_opts,NULL ,0 ,NULL );qemu_opt_set (opts,"path" , path); sock =unix_connect_opts (opts);qemu_opts_del (opts);
qemu_socket.h +2 -0
@@ -30,6 +30,8 @@ int inet_aton(const char *cp, struct in_addr *ia);#include "error.h" #include "qerror.h" +extern QemuOptsList socket_opts; +/* misc helpers */ int qemu_socket (int domain,int type,int protocol);int qemu_accept (int s,struct sockaddr *addr,socklen_t *addrlen);
Signature · 1 line
1.7.11.2
RFC PATCH 9/13 qmp: add NBD server commands
In reply to Paolo BonziniAdding an NBD server inside QEMU is trivial, since all the logic is in nbd.c and can be shared easily between qemu-nbd and QEMU itself. The main difference is that qemu-nbd serves a single unnamed export, while QEMU serves named exports.
--- Makefile.objs | 2 +- blockdev-nbd.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qapi-schema.json | 69 +++++++++++++++++++++++++++++++++++++++++ qmp-commands.hx | 16 ++++++++++ 4 file modificati, 179 inserzioni(+). 1 rimozione(-) create mode 100644 blockdev-nbd.c
Makefile.objs +1 -1
@@ -59,7 +59,7 @@ endif # suppress *all* target specific code in case of system emulation, i.e. a # single QEMU executable should support all CPUs and machines. -common-obj-y = $(block-obj-y) blockdev.o +common-obj-y = $(block-obj-y) blockdev.o blockdev-nbd.o common-obj-y += net.o net/ common-obj-y += qom/ common-obj-y += readline.o console.o cursor.o
blockdev-nbd.c new file +93 -0
93 lines not on this page · raw message
qapi-schema.json +67 -0
76 lines not on this page · raw message
qmp-commands.hx +16 -0
@@ -2481,6 +2481,22 @@ EQMP }, { + .name = "nbd-server-start", + .args_type = "addr:q", + .mhandler.cmd_new = qmp_marshal_input_nbd_server_start, + }, + { + .name = "nbd-server-add", + .args_type = "device:B,writable:b?", + .mhandler.cmd_new = qmp_marshal_input_nbd_server_add, + }, + { + .name = "nbd-server-stop", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_nbd_server_stop, + }, + + { .name = "change-vnc-password", .args_type = "password:s", .mhandler.cmd_new = qmp_marshal_input_change_vnc_password,
Signature · 1 line
1.7.11.2
Re: [Qemu-devel] [RFC PATCH 09/13] qmp: add NBD server commands
Quoted 4 lines · On Mon, 27 Aug 2012 17:00:22 +0200 Paolo Bonzini <[email protected]> wrote:
Adding an NBD server inside QEMU is trivial, since all the logic is in
nbd.cand can be shared easily between qemu-nbd and QEMU itself. The main difference is that qemu-nbd serves a single unnamed export, while QEMU serves named exports.
Quickly reviewed the qmp part and it looks good to me (apart from TODOs of course, specially on error handling).
I've only a few minor comments to the schema.
Quoted 129 lines
Signed-off-by Paolo Bonzini <[email protected]>--- Makefile.objs | 2 +- blockdev-nbd.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qapi-schema.json | 69 +++++++++++++++++++++++++++++++++++++++++ qmp-commands.hx | 16 ++++++++++ 4 file modificati, 179 inserzioni(+). 1 rimozione(-) create mode 100644 blockdev-nbd.cMakefile.objs +1
-1@@ -59,7 +59,7 @@ endif # suppress *all* target specific code in case of system emulation, i.e. a # single QEMU executable should support all CPUs and machines. -common-obj-y = $(block-obj-y) blockdev.o +common-obj-y = $(block-obj-y) blockdev.o blockdev-nbd.o common-obj-y += net.o net/ common-obj-y += qom/ common-obj-y += readline.o console.o cursor.oblockdev-nbd.c new file +93
-0@@ -0,0 +1,93 @@ +/* +* QEMU host block devices +* +* Copyright (c) 2003-2008 Fabrice Bellard +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "blockdev.h" +#include "hw/block-common.h" +#include "monitor.h" +#include "qerror.h" +#include "sysemu.h" +#include "qmp-commands.h" +#include "trace.h" +#include "nbd.h" +#include "qemu_socket.h" + +static int server_fd =-1 ; + +static void nbd_accept (void *opaque) +{ +struct sockaddr_in addr; +socklen_t addr_len =sizeof (addr); + +int fd =accept (server_fd, (struct sockaddr *)&addr, &addr_len); +if (fd >=0 ) { +nbd_client_new (NULL , fd,NULL ); + } +} + +static void nbd_server_start (QemuOpts *opts,Error **errp) +{ +if (server_fd !=-1 ) { +/* TODO: error */ +return ; + } + + server_fd =inet_listen_opts (opts,0 , errp); +if (server_fd !=-1 ) { +qemu_set_fd_handler2 (server_fd,NULL , nbd_accept,NULL ,NULL ); + } +} + +void qmp_nbd_server_start (IPSocketAddress *addr,Error **errp) +{ +QemuOpts *opts; + + opts =qemu_opts_create (&socket_opts,NULL ,0 ,NULL ); +qemu_opt_set (opts,"host" , addr->host); +qemu_opt_set (opts,"port" , addr->port); + + addr->ipv4 |= !addr->has_ipv4; + addr->ipv6 |= !addr->has_ipv6; +if (!addr->ipv4 || !addr->ipv6) { +qemu_opt_set_bool (opts,"ipv4" , addr->ipv4); +qemu_opt_set_bool (opts,"ipv6" , addr->ipv6); + } + +nbd_server_start (opts, errp); +qemu_opts_del (opts); +} + + +void qmp_nbd_server_add (const char *device,bool has_writable,bool writable, +Error **errp) +{ +BlockDriverState *bs; +NBDExport *exp; + + bs =bdrv_find (device); +if (!bs) { +error_set (errp,QERR_DEVICE_NOT_FOUND , device); +return ; + } + +if (nbd_export_find (bdrv_get_device_name (bs))) { +/* TODO: error */ +return ; + } + + exp =nbd_export_new (bs,0 ,-1 , writable ?0 :NBD_FLAG_READ_ONLY ); +nbd_export_set_name (exp, device); +} + +void qmp_nbd_server_stop (Error **errp) +{ +nbd_export_close_all (); +qemu_set_fd_handler2 (server_fd,NULL ,NULL ,NULL ,NULL ); +close (server_fd); + server_fd =-1 ; +}qapi-schema.json +0
-0@@ -2275,6 +2275,30 @@ 'opts': 'NetClientOptions' } }
qapi-schema.json line 2286
Quoted 10 lines
qapi-schema.json +9
-0## +# @IPSocketAddress +# +# Captures the destination address of an IP socket +# +# @host: host part of the address +# +# @port: port part of the address, or lowest port if @to is present +# +# @to: highest port to try
Doesn't exist in data. If you add, please use a more descriptive name.
qapi-schema.json line 2290
Quoted 4 lines
qapi-schema.json +4
-0+# +# @ipv4: whether to accept IPv4 addresses, default try both IPv4 and IPv6 +# +# @ipv6: whether to accept IPv6 addresses, default try both IPv4 and IPv6
Both are missing #optional.
Quoted 31 lines
qapi-schema.json +24
-0+# +# Since 1.3 +## +{ 'type': 'IPSocketAddress', + 'data': { + 'host': 'str', + 'port': 'str', + '*ipv4': 'bool', + '*ipv6': 'bool' } } + +## # @getfd: # # Receive a file descriptor via SCM rights and assign it a name@@ -2454,3 +2478,46 @@ # ## { 'command': 'query-fdsets', 'returns': ['FdsetInfo'] } + +## +# @nbd-server-start: +# +# Start an NBD server listening on the given host and port. +# +# @addr: Interface on which to listen, nothing for all interfaces. +# +# Since: 1.3.0 +# +## +{ 'command': 'nbd-server-start', + 'data': { 'addr': 'IPSocketAddress' } }
qapi-schema.json line 2503
Quoted 10 lines
qapi-schema.json +10
-0+ +## +# @nbd-server-add: +# +# Export a device to QEMU's embedded NBD server. +# +# @device: Block device to be exported +# +# @writable: Whether clients should be able to write to the device via the +# NBD connection (default false).
Missing #optional.
Quoted 5 lines
qapi-schema.json +5
-0+# +# Returns: error if the device is already marked for export. +# +# Since: 1.3.0 +#
qapi-schema.json line 2518
Quoted 10 lines
qapi-schema.json +10
-0+## +{ 'command': 'nbd-server-add', 'data': {'device': 'str', '*writable': 'bool'} } + +## +# @nbd-server-stop: +# +# Stop QEMU's embedded NBD server, and unregister all devices previously +# added via nbd-server-add +# +# Returns: error if the server is already running.
That error pertains to nbd-start-start.
Quoted 32 lines
qapi-schema.json +5
-0+# +# Since: 1.3.0 +# +## +{ 'command': 'nbd-server-stop' }qmp-commands.hx +16
-0@@ -2481,6 +2481,22 @@ EQMP }, { + .name = "nbd-server-start", + .args_type = "addr:q", + .mhandler.cmd_new = qmp_marshal_input_nbd_server_start, + }, + { + .name = "nbd-server-add", + .args_type = "device:B,writable:b?", + .mhandler.cmd_new = qmp_marshal_input_nbd_server_add, + }, + { + .name = "nbd-server-stop", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_nbd_server_stop, + }, + + { .name = "change-vnc-password", .args_type = "password:s", .mhandler.cmd_new = qmp_marshal_input_change_vnc_password,
Re: [Qemu-devel] [RFC PATCH 09/13] qmp: add NBD server commands
Il 18/09/2012 22:11, Luiz Capitulino ha scritto:
Quoted 1 line
(apart from TODOs of course, specially on error handling).
Yes, those were waiting for error_setg to get in the tree. :)
Paolo
RFC PATCH 10/13 qemu-sockets: make inet_parse public
In reply to Paolo Bonzini--- qemu-sockets.c | 2 +- qemu_socket.h | 1 +
2 file modificati, 2 inserzioni(+). 1 rimozione(-)
qemu-sockets.c +1 -1
@@ -407,7 +407,7 @@ err: }/* compatibility wrapper */ -static int inet_parse (QemuOpts *opts,const char *str) +int inet_parse (QemuOpts *opts,const char *str) {const char *optstr, *h;char addr[64 ];
qemu_socket.h +1 -0
@@ -41,6 +41,7 @@ void socket_set_nonblock(int fd);int send_all (int fd,const void *buf,int len1);/* New, ipv6-ready socket helper functions, see qemu-sockets.c */ +int inet_parse (QemuOpts *opts,const char *str);int inet_listen_opts (QemuOpts *opts,int port_offset,Error **errp);int inet_listen (const char *str,char *ostr,int olen,int socktype,int port_offset,Error **errp);
Signature · 1 line
1.7.11.2
RFC PATCH 11/13 hmp: add NBD server commands
In reply to Paolo BonziniAt the HMP level there is no nbd_server_add command. nbd_server_start automatically exposes all of the VM's block devices, and an option -w makes them writable.
--- hmp-commands.hx | 29 +++++++++++++++++++++++++ hmp.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hmp.h | 2 ++
3 file modificati, 97 inserzioni(+)
hmp-commands.hx +29 -0
@@ -1248,6 +1248,35 @@ Remove all matches from the access control list, and set the default policy back to @code{deny}. ETEXI + { + .name = "nbd_server_start", + .args_type = "writable:-w,uri:s", + .params = "nbd_server_start [-w] host:port", + .help = "serve block devices on the given host and port", + .mhandler.cmd = hmp_nbd_server_start, + }, +STEXI +@item nbd_server_start @var{host}:@var{port} +@findex nbd_server_start +Start an NBD server on the given host and/or port, and serve all of the +virtual machine's block devices that have an inserted media on it. +The @option{-w} option makes the devices writable. +ETEXI + + { + .name = "nbd_server_stop", + .args_type = "", + .params = "nbd_server_stop", + .help = "stop serving block devices using the NBD protocol", + .mhandler.cmd = hmp_nbd_server_stop, + }, +STEXI +@item nbd_server_stop +@findex nbd_server_stop +Stop the QEMU embedded NBD server. +ETEXI + + #if defined(TARGET_I386) {
hmp.c +66 -0
75 lines not on this page · raw message
hmp.h +2 -0
@@ -71,5 +71,7 @@ void hmp_netdev_add(Monitor *mon, const QDict *qdict);void hmp_netdev_del (Monitor *mon,const QDict *qdict);void hmp_getfd (Monitor *mon,const QDict *qdict);void hmp_closefd (Monitor *mon,const QDict *qdict); +void hmp_nbd_server_start (Monitor *mon,const QDict *qdict); +void hmp_nbd_server_stop (Monitor *mon,const QDict *qdict);#endif
Signature · 1 line
1.7.11.2
Re: [Qemu-devel] [RFC PATCH 11/13] hmp: add NBD server commands
Quoted 83 lines · On Mon, 27 Aug 2012 17:00:24 +0200 Paolo Bonzini <[email protected]> wrote:
At the HMP level there is no
nbd_server_addcommand.nbd_server_startautomatically exposes all of the VM's block devices, and an option -w makes them writable.Signed-off-by Paolo Bonzini <[email protected]>--- hmp-commands.hx | 29 +++++++++++++++++++++++++ hmp.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hmp.h | 2 ++3 file modificati, 97 inserzioni(+)
hmp-commands.hx +29
-0@@ -1248,6 +1248,35 @@ Remove all matches from the access control list, and set the default policy back to @code{deny}. ETEXI + { + .name = "nbd_server_start", + .args_type = "writable:-w,uri:s", + .params = "nbd_server_start [-w] host:port", + .help = "serve block devices on the given host and port", + .mhandler.cmd = hmp_nbd_server_start, + }, +STEXI +@item nbd_server_start @var{host}:@var{port} +@findex nbd_server_start +Start an NBD server on the given host and/or port, and serve all of the +virtual machine's block devices that have an inserted media on it. +The @option{-w} option makes the devices writable. +ETEXI + + { + .name = "nbd_server_stop", + .args_type = "", + .params = "nbd_server_stop", + .help = "stop serving block devices using the NBD protocol", + .mhandler.cmd = hmp_nbd_server_stop, + }, +STEXI +@item nbd_server_stop +@findex nbd_server_stop +Stop the QEMU embedded NBD server. +ETEXI + + #if defined(TARGET_I386) {hmp.c +17
-0@@ -18,6 +18,7 @@#include "qemu-option.h" #include "qemu-timer.h" #include "qmp-commands.h" +#include "qemu_socket.h" #include "monitor.h" static void hmp_handle_error (Monitor *mon,Error **errp)@@ -1102,3 +1103,68 @@ void hmp_closefd(Monitor *mon, const QDict *qdict)qmp_closefd (fdname, &errp);hmp_handle_error (mon, &errp); } + +void hmp_nbd_server_start (Monitor *mon,const QDict *qdict) +{ +const char *uri =qdict_get_str (qdict,"uri" ); +int writable =qdict_get_try_bool (qdict,"writable" ,0 ); +Error *errp =NULL ; +QemuOpts *opts; +BlockDriverState *bs; +IPSocketAddress addr; + +/* First check if the address is available and start the server. */ + opts =qemu_opts_create (&socket_opts,NULL ,0 ,NULL ); +if (inet_parse (opts, uri) !=0 ) { +error_set (&errp,QERR_SOCKET_CREATE_FAILED ); + goto exit; + }
hmp.c void hmp_closefd(Monitor *mon, const QDict *qdict) · line 1131
Quoted 10 lines
hmp.c +10
-0+ +memset (&addr,0 ,sizeof (addr)); + addr.host = (char *)qemu_opt_get (opts,"host" ); + addr.port = (char *)qemu_opt_get (opts,"port" ); + addr.ipv4 =qemu_opt_get_bool (opts,"ipv4" ,0 ); + addr.ipv6 =qemu_opt_get_bool (opts,"ipv6" ,0 ); + addr.has_ipv4 = addr.has_ipv6 = true; + +if (addr.host ==NULL || addr.port ==NULL ) { +error_set (&errp,QERR_SOCKET_CREATE_FAILED );
You can and should use monitor_printf() directly.
hmp.c void hmp_closefd(Monitor *mon, const QDict *qdict) · line 1136
Quoted 5 lines
hmp.c +5
-0+ goto exit; + } + +qmp_nbd_server_start (&addr, &errp); +if (errp !=NULL ) {
Please, use error_is_set() instead.
hmp.c void hmp_closefd(Monitor *mon, const QDict *qdict) · line 1144
Quoted 8 lines
hmp.c +8
-0+ goto exit; + } + +/* Then try adding all block devices. If one fails, close all and +* exit. +*/ + bs =NULL ; +while ((bs =bdrv_next (bs))) {
To keep hmp.c an honest qmp client, you should use qmp_query_block() instead of looping through BSs directly. You'll find other examples if you find for qmp_query_block() in this file.
Quoted 38 lines
hmp.c +26
-0+if (!bdrv_is_inserted (bs)) { +continue ; + } + +qmp_nbd_server_add (bdrv_get_device_name (bs), + true, !bdrv_is_read_only (bs) && writable, + &errp); + +if (errp !=NULL ) { +qmp_nbd_server_stop (NULL ); +break ; + } + } + +exit: +qemu_opts_del (opts); +hmp_handle_error (mon, &errp); +} + +void hmp_nbd_server_stop (Monitor *mon,const QDict *qdict) +{ +Error *errp =NULL ; + +qmp_nbd_server_stop (&errp); +hmp_handle_error (mon, &errp); +}hmp.h +2
-0@@ -71,5 +71,7 @@ void hmp_netdev_add(Monitor *mon, const QDict *qdict);void hmp_netdev_del (Monitor *mon,const QDict *qdict);void hmp_getfd (Monitor *mon,const QDict *qdict);void hmp_closefd (Monitor *mon,const QDict *qdict); +void hmp_nbd_server_start (Monitor *mon,const QDict *qdict); +void hmp_nbd_server_stop (Monitor *mon,const QDict *qdict);#endif
Re: [Qemu-devel] [RFC PATCH 11/13] hmp: add NBD server commands
Il 18/09/2012 22:22, Luiz Capitulino ha scritto:
Quoted 3 lines
Quoted 2 lines
hmp.c void hmp_closefd(Monitor *mon, const QDict *qdict) · line 1131
Quoted 2 lines
hmp.c +2
-0+if (addr.host ==NULL || addr.port ==NULL ) { +error_set (&errp,QERR_SOCKET_CREATE_FAILED );You can and should use
monitor_printf()directly.
Hmm, why? Then I would have two error paths, one with monitor_printf and one with hmp_handle_error. But I will change this to add an Error* to inet_parse.
Quoted 6 lines
Quoted 5 lines
hmp.c void hmp_closefd(Monitor *mon, const QDict *qdict) · line 1136
Quoted 5 lines
hmp.c +5
-0+ goto exit; + } + +qmp_nbd_server_start (&addr, &errp); +if (errp !=NULL ) {Please, use
error_is_set()instead.
I'll just rename errp to local_err, which is the usual convention.
Quoted 11 lines
Quoted 8 lines
hmp.c void hmp_closefd(Monitor *mon, const QDict *qdict) · line 1144
Quoted 8 lines
hmp.c +8
-0+ goto exit; + } + +/* Then try adding all block devices. If one fails, close all and +* exit. +*/ + bs =NULL ; +while ((bs =bdrv_next (bs))) {To keep
hmp.can honest qmp client, you should useqmp_query_block()instead of looping through BSs directly. You'll find other examples if you find forqmp_query_block()in this file.
Right, thanks!
Paolo
RFC PATCH 12/13 block: add close notifiers
In reply to Paolo Bonzini--- Makefile.objs | 3 ++- block.c | 19 ++++++++++++++----- block.h | 1 + block_int.h | 2 ++
4 file modificati, 19 inserzioni(+), 6 rimozioni(-)
Makefile.objs +2 -1
@@ -43,6 +43,7 @@ coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o block-obj-y = cutils.o iov.o cache-utils.o qemu-option.o module.o async.o block-obj-y += nbd.o block.o aio.o aes.o qemu-config.o qemu-progress.o qemu-sockets.o +block-obj-y += notify.o block-obj-y += $(coroutine-obj-y) $(qobject-obj-y) $(version-obj-y) block-obj-$(CONFIG_POSIX) += posix-aio-compat.o block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
@@ -91,7 +92,7 @@ common-obj-y += bt-host.o bt-vhci.o common-obj-y += iov.o acl.o common-obj-$(CONFIG_POSIX) += compatfd.o -common-obj-y += notify.o event_notifier.o +common-obj-y += event_notifier.o common-obj-y += qemu-timer.o qemu-timer-common.o common-obj-$(CONFIG_SLIRP) += slirp/
block.c +14 -5
@@ -28,6 +28,7 @@#include "block_int.h" #include "module.h" #include "qjson.h" +#include "notify.h" #include "qemu-coroutine.h" #include "qmp-commands.h" #include "qemu-timer.h"
@@ -310,9 +311,16 @@ BlockDriverState *bdrv_new(const char *device_name)QTAILQ_INSERT_TAIL (&bdrv_states, bs, list); }bdrv_iostatus_disable (bs); +notifier_list_init (&bs->close_notifiers); +return bs; } +void bdrv_add_close_notifier (BlockDriverState *bs,Notifier *notify) +{ +notifier_list_add (&bs->close_notifiers, notify); +} +BlockDriver *bdrv_find_format (const char *format_name) {BlockDriver *drv1;
@@ -862,12 +870,13 @@ unlink_and_fail:void bdrv_close (BlockDriverState *bs) {bdrv_flush (bs); -if (bs->drv) { -if (bs->job) { -block_job_cancel_sync (bs->job); - } -bdrv_drain_all (); +if (bs->job) { +block_job_cancel_sync (bs->job); + } +bdrv_drain_all (); +notifier_list_notify (&bs->close_notifiers, bs); +if (bs->drv) {if (bs == bs_snapshots) { bs_snapshots =NULL ; }
block.h +1 -0
@@ -131,6 +131,7 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags);int bdrv_open (BlockDriverState *bs,const char *filename,int flags,BlockDriver *drv);void bdrv_close (BlockDriverState *bs); +void bdrv_add_close_notifier (BlockDriverState *bs,Notifier *notify);int bdrv_attach_dev (BlockDriverState *bs,void *dev);void bdrv_attach_dev_nofail (BlockDriverState *bs,void *dev);void bdrv_detach_dev (BlockDriverState *bs,void *dev);
block_int.h +2 -0
@@ -293,6 +293,8 @@ struct BlockDriverState {BlockDriverState *backing_hd;BlockDriverState *file; +NotifierList close_notifiers; +/* number of in-flight copy-on-read requests */ unsigned int copy_on_read_in_flight;
Signature · 1 line
1.7.11.2
RFC PATCH 13/13 nbd: add notifier to close exports when the image is closed
In reply to Paolo Bonzini--- blockdev-nbd.c | 38 ++++++++++++++++++++++++++++++++++++++ qapi/opts-visitor.c | 48 ++++++++++++++++++++----------------------------
2 file modificati, 58 inserzioni(+), 28 rimozioni(-)
blockdev-nbd.c +38 -0
@@ -62,12 +62,39 @@ void qmp_nbd_server_start(IPSocketAddress *addr, Error **errp)qemu_opts_del (opts); } +/* Hook into the BlockDriverState notifiers to close the export when +* the file is closed. +*/ +typedef struct NBDCloseNotifier { +Notifier n; +NBDExport *exp; +QTAILQ_ENTRY (NBDCloseNotifier) next; +}NBDCloseNotifier ; + +static QTAILQ_HEAD(, NBDCloseNotifier) close_notifiers = +QTAILQ_HEAD_INITIALIZER (close_notifiers); + +static void nbd_close_notifier_remove (NBDCloseNotifier *cn) +{ +notifier_remove (&cn->n); +QTAILQ_REMOVE (&close_notifiers, cn, next); +g_free (cn); +} + +static void nbd_close_notifier (Notifier *n,void *data) +{ +NBDCloseNotifier *cn =DO_UPCAST (NBDCloseNotifier, n, n); + +nbd_export_close (cn->exp); +nbd_close_notifier_remove (cn); +}void qmp_nbd_server_add (const char *device,bool has_writable,bool writable,Error **errp) {BlockDriverState *bs;NBDExport *exp; +NBDCloseNotifier *n; bs =bdrv_find (device);if (!bs) {
@@ -82,10 +109,21 @@ void qmp_nbd_server_add(const char *device, bool has_writable, bool writable, exp =nbd_export_new (bs,0 ,-1 , writable ?0 :NBD_FLAG_READ_ONLY );nbd_export_set_name (exp, device); + + n =g_malloc0 (sizeof (NBDCloseNotifier)); + n->n.notify = nbd_close_notifier; + n->exp = exp; +bdrv_add_close_notifier (bs, &n->n); +QTAILQ_INSERT_TAIL (&close_notifiers, n, next); }void qmp_nbd_server_stop (Error **errp) { +while (!QTAILQ_EMPTY (&close_notifiers)) { +NBDCloseNotifier *cn =QTAILQ_FIRST (&close_notifiers); +nbd_close_notifier_remove (cn); + } +nbd_export_close_all ();qemu_set_fd_handler2 (server_fd,NULL ,NULL ,NULL ,NULL );close (server_fd);
qapi/opts-visitor.c +20 -28
81 lines not on this page · raw message
Signature · 1 line
1.7.11.2
[Qemu-devel] ping Re: [RFC PATCH 00/13] Embedded NBD server
In reply to Paolo BonziniIl 27/08/2012 17:00, Paolo Bonzini ha scritto:
Quoted 5 lines
The part where I need
a second opinion and/or ack is patch 12 and 13. They fix the case of a disk being unplugged while NBD export is active. To do this I add a NotifierList to a BlockDriverState. Does this look okay, or is it too ad hoc?
Ping... Kevin/Stefan, could you look at just these two patches:
http://permalink.gmane.org/gmane.comp.emulators.qemu/167411
[12/13] block: add close notifiers
http://permalink.gmane.org/gmane.comp.emulators.qemu/167410
[13/13] nbd: add notifier to close exports when the image is closed
and if you need some context:
http://permalink.gmane.org/gmane.comp.emulators.qemu/167400
[09/13] qmp: add NBD server commands
Everything else is totally uninteresting.
Paolo
Re: [Qemu-devel] ping Re: [RFC PATCH 00/13] Embedded NBD server
Am 07.09.2012 17:50, schrieb Paolo Bonzini:
Quoted 15 lines
Il 27/08/2012 17:00, Paolo Bonzini ha scritto:
Quoted 5 lines
The part where I need
a second opinion and/or ack is patch 12 and 13. They fix the case of a disk being unplugged while NBD export is active. To do this I add a NotifierList to a BlockDriverState. Does this look okay, or is it too ad hoc?Ping... Kevin/Stefan, could you look at just these two patches:
http://permalink.gmane.org/gmane.comp.emulators.qemu/167411
[12/13] block: add close notifiershttp://permalink.gmane.org/gmane.comp.emulators.qemu/167410
[13/13] nbd: add notifier to close exports when the image is closed
Quoted 7 lines
and if you need some context:
http://permalink.gmane.org/gmane.comp.emulators.qemu/167400
[09/13] qmp: add NBD server commandsEverything else is totally uninteresting.
I was planning to review it in more detail next week, but I just had a quick look. I'm not sure if automatically shutting down the NBD server when the guest stops using it is always right (for removable media it could even be an eject from the guest), but introducing a notifier list doesn't look too bad. We can probably use it for other things that are currently hardcoded in bdrv_close() with some if statements, like disabling I/O throttling, cancelling a block job, etc.
Kevin
Re: [Qemu-devel] ping Re: [RFC PATCH 00/13] Embedded NBD server
Il 07/09/2012 18:11, Kevin Wolf ha scritto:
Quoted 4 lines
I was planning to review it in more detail next week, but I just had a quick look. I'm not sure if automatically shutting down the NBD server when the guest stops using it is always right (for removable media it could even be an eject from the guest),
Yes, the removable media case could be a bit too eager. Note however that a guest-triggered eject doesn't do bdrv_close, only a user-triggered eject does, and that's blocked by bdrv_in_use.
Luckily removable media are usually not too interesting, so a slightly suboptimal behavior is okay as long as it does not break the important use cases---mostly migration without shared storage, where also uninteresting images have to be mirrored or exposed via NBD. Those should be covered by bdrv_in_use.
Quoted 4 lines
but introducing a notifier list
doesn't look too bad. We can probably use it for other things that are currently hardcoded inbdrv_close()with some if statements, like disabling I/O throttling, cancelling a block job, etc.
Yes, though a lot of these could be moved to "filters" and use whatever filter-specific method is there (e.g. a filter bdrv_close). This circles back to the question of whether bdrv_close kills filters or only the base image...
Paolo
Re: [Qemu-devel] ping Re: [RFC PATCH 00/13] Embedded NBD server
Am 17.09.2012 18:43, schrieb Paolo Bonzini:
Quoted 10 lines
Il 07/09/2012 18:11, Kevin Wolf ha scritto:
Quoted 4 lines
I was planning to review it in more detail next week, but I just had a quick look. I'm not sure if automatically shutting down the NBD server when the guest stops using it is always right (for removable media it could even be an eject from the guest),
Yes, the removable media case could be a bit too eager. Note however that a guest-triggered eject doesn't do
bdrv_close, only a user-triggered eject does, and that's blocked bybdrv_in_use.
Quoted 5 lines
Luckily removable media are usually not too interesting, so a slightly suboptimal behavior is okay as long as it does not break the important use cases---mostly migration without shared storage, where also uninteresting images have to be mirrored or exposed via NBD. Those should be covered by
bdrv_in_use.
It sounds like it could be acceptable, yes. But what's even the motivation to close the server on bdrv_close? The commit message is a bit... well, not just terse, but even empty.
The standard case for closing images is that qemu exits. In this case, the NBD server would automatically exit as well. An interesting case for the NBD server would be when the migration has completed; but do we even get a bdrv_close there?
Quoted 9 lines
Quoted 4 lines
but introducing a notifier list
doesn't look too bad. We can probably use it for other things that are currently hardcoded inbdrv_close()with some if statements, like disabling I/O throttling, cancelling a block job, etc.Yes, though a lot of these could be moved to "filters" and use whatever filter-specific method is there (e.g. a filter
bdrv_close). This circles back to the question of whetherbdrv_closekills filters or only the base image...
Note that after completing the refactoring, we'll only have one combined bdrv_close/delete function and so there won't be BlockDriverStates that are closed. In this case, I think it's quite obvious that not closing the filters wouldn't make any sense.
Kevin
Re: [Qemu-devel] ping Re: [RFC PATCH 00/13] Embedded NBD server
Quoted 10 lines
Quoted 6 lines
Luckily removable media are usually not too interesting, so a slightly suboptimal behavior is okay as long as it does not break the important use cases---mostly migration without shared storage, where also uninteresting images have to be mirrored or exposed via NBD. Those should be covered by
bdrv_in_use.It sounds like it could be acceptable, yes. But what's even the motivation to close the server on
bdrv_close? The commit message is a bit... well, not just terse, but even empty.
The motivation is two-fold:
1) for device hot-unplug, not closing the server would impede removal of the blockdev until after all clients have closed their connections.
2) for the removable media case, clients risk reading data from two different images and merging it somehow.
In either case (hot-unplug and eject) after bdrv_close I/O requests would return ENOMEDIUM, so there is not much benefit in leaving the connection open. Clients can reconnect with the understanding that the medium has changed (medium change is not part of the NBD specification, but we can retrofit it this way).
Quoted 4 lines
The standard case for closing images is that qemu exits. In this case, the NBD server would automatically exit as well. An interesting case for the NBD server would be when the migration has completed; but do we even get a
bdrv_closethere?
No, you don't.
Quoted 9 lines
Quoted 4 lines
Yes, though a lot of these could be moved to "filters" and use whatever filter-specific method is there (e.g. a filter
bdrv_close). This circles back to the question of whetherbdrv_closekills filters or only the base image...Note that after completing the refactoring, we'll only have one combined bdrv_close/delete function and so there won't be BlockDriverStates that are closed. In this case, I think it's quite obvious that not closing the filters wouldn't make any sense.
Does that mean that any I/O throttling must be applied again on every medium change? That would be a behavioral change.
Paolo
Re: [Qemu-devel] ping Re: [RFC PATCH 00/13] Embedded NBD server
Am 18.09.2012 11:09, schrieb Paolo Bonzini:
Quoted 15 lines
Quoted 9 lines
Quoted 5 lines
Luckily removable media are usually not too interesting, so a slightly suboptimal behavior is okay as long as it does not break the important use cases---mostly migration without shared storage, where also uninteresting images have to be mirrored or exposed via NBD. Those should be covered by
bdrv_in_use.It sounds like it could be acceptable, yes. But what's even the motivation to close the server on
bdrv_close? The commit message is a bit... well, not just terse, but even empty.The motivation is two-fold:
1) for device hot-unplug, not closing the server would impede removal of the blockdev until after all clients have closed their connections.
Quoted 8 lines
2) for the removable media case, clients risk reading data from two different images and merging it somehow.
In either case (hot-unplug and eject) after
bdrv_closeI/O requests would return ENOMEDIUM, so there is not much benefit in leaving the connection open. Clients can reconnect with the understanding that the medium has changed (medium change is not part of the NBD specification, but we can retrofit it this way).
I think I can buy this, but please add it to the commit message.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Yes, though a lot of these could be moved to "filters" and use whatever filter-specific method is there (e.g. a filter
bdrv_close). This circles back to the question of whetherbdrv_closekills filters or only the base image...
Quoted 7 lines
Quoted 4 lines
Note that after completing the refactoring, we'll only have one combined bdrv_close/delete function and so there won't be BlockDriverStates that are closed. In this case, I think it's quite obvious that not closing the filters wouldn't make any sense.
Does that mean that any I/O throttling must be applied again on every medium change? That would be a behavioral change.
Hm, I guess so, at least on the lowest level. The only thing I know for certain is that maintaining compatibility for the old commands will be fun, but if possible at all we shouldn't let that compromise our design.
Kevin
Re: [Qemu-devel] ping Re: [RFC PATCH 00/13] Embedded NBD server
Il 18/09/2012 11:40, Kevin Wolf ha scritto:
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Note that after completing the refactoring, we'll only have one combined bdrv_close/delete function and so there won't be BlockDriverStates that are closed. In this case, I think it's quite obvious that not closing the filters wouldn't make any sense.
Does that mean that any I/O throttling must be applied again on every medium change? That would be a behavioral change.
Hm, I guess so, at least on the lowest level. The only thing I know for certain is that maintaining compatibility for the old commands will be fun, but if possible at all we shouldn't let that compromise our design.
Yeah, originally we had the idea of a "proxy" driver where you could stack all your persistent filters. The proxy driver would be needed of course for removable media, but it could also subsume things like bdrv_swap and bdrv_append.
Paolo
Re: [Qemu-devel] ping Re: [RFC PATCH 00/13] Embedded NBD server
Am 18.09.2012 11:48, schrieb Paolo Bonzini:
Quoted 8 lines
Il 18/09/2012 11:40, Kevin Wolf ha scritto:
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Note that after completing the refactoring, we'll only have one combined bdrv_close/delete function and so there won't be BlockDriverStates that are closed. In this case, I think it's quite obvious that not closing the filters wouldn't make any sense.
Does that mean that any I/O throttling must be applied again on every medium change? That would be a behavioral change.
Quoted 8 lines
Quoted 3 lines
Hm, I guess so, at least on the lowest level. The only thing I know for certain is that maintaining compatibility for the old commands will be fun, but if possible at all we shouldn't let that compromise our design.
Yeah, originally we had the idea of a "proxy" driver where you could stack all your persistent filters. The proxy driver would be needed of course for removable media, but it could also subsume things like
bdrv_swapandbdrv_append.
That would be filters directly for a BlockBackend, which we need in order to support filter that stay on top of snapshots. However, they only help in this specific case if we leave the same BlockBackend around across media change, which wasn't part of the plan, I think. But maybe we'll have to.
Kevin
Quoted 9 lines · Paolo Bonzini
Hi all,
this is an RFC series implementing an NBD server embedded inside QEMU. This can be used in various cases, including migration with non-shared storage.
Three new commands are introduced at the QMP level
{ 'command': 'nbd-server-start', 'data': { 'addr': 'IPSocketAddress' } }
It is probably desirable/required to also have a command where libvirt can pass in a pre-opened listening socket, since I think SELinux policy will not want to allow QEMU to create listening sockets itself.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Il 19/09/2012 12:16, Daniel P. Berrange ha scritto:
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Three new commands are introduced at the QMP level
{ 'command': 'nbd-server-start', 'data': { 'addr': 'IPSocketAddress' } }
It is probably desirable/required to also have a command where libvirt can pass in a pre-opened listening socket, since I think SELinux policy will not want to allow QEMU to create listening sockets itself.
Ok, I'll make it like this:
{ 'type': 'UnixSocketAddress',
'data': {
'path': 'str' } }{ 'type': 'String',
'data': {
'str': 'str' } }{'union': 'SocketAddress',
'data': {
'inet': 'IPSocketAddress',
'unix': 'UnixSocketAddress',
'fd': 'String',} }
Paolo