nbd: add more constants

Avoid magic numbers and magic size computations; hide them behind
constants.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
nbd: pass NBDClient to nbd_send_negotiate

We will need the NBDClient in nbd_send_negotiate to store the
export requested by the client.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
nbd: do not close BlockDriverState in nbd_export_close

This is not desirable when embedding the NBD server inside QEMU.
Move the bdrv_close to qemu-nbd.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
nbd: make refcount interface public

After the next patch, the close callback will have to release its
reference.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
nbd: do not leak nbd_trip coroutines when a connection is torn down

Because 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.  Also call the
close callback, which will release the user's reference.  The coroutines
then will fail and exit cleanly, and release all remaining references,
until the last refcount finally triggers the closure of the client.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
nbd: add reference counting to NBDExport

We will use a similar two-phase destruction for NBDExport, so we need
each NBDClient to add a reference to NBDExport.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
nbd: track clients into NBDExport

Track the NBDClients of each NBDExport, and use it to implement
nbd_export_close.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
nbd: add notification for closing an NBDExport

In order to exit cleanly from qemu-nbd, add a callback that triggers
when an NBDExport is closed.  In the case of qemu-nbd it will exit the
main loop.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-nbd: rewrite termination conditions to use a state machine

Use a simple state machine with the following states:

- RUNNING     => accepting connections
- TERMINATE   => main loop must call nbd_export_close/put, and not accept
  connections anymore
- TERMINATING => waiting for pending requests to finish
- TERMINATED  => the NBDExport has been closed

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
nbd: register named exports

Add an API to register and find named exports.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
nbd: negotiate with named exports

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 <pbonzini@redhat.com>
nbd: add nbd_export_get_blockdev

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-timer: simplify qemu_run_timers

ptimer_head is an invariant pointer to clock->active_timers.
Remove it, and just reference clock->active_timers directly.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
monitor: use monitor_handle_fd_param for non-Error-friendly users of named fds

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
monitor: add Error * argument to monitor_get_fd

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qapi: do not protect enum values from namespace pollution

Enum values are always preceded by the uppercase name of the enum, so
they do not conflict with reserved words.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qapi: add "unix" to the set of reserved words

It is #defined to 1.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qapi: add socket address types

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-sockets: add error propagation to inet_parse

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-sockets: add error propagation to Unix socket functions

This patch starts harmonizing unix_* and inet_* functions.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>