Commit 1c778ef7 authored by Daniel P. Berrangé's avatar Daniel P. Berrangé Committed by Paolo Bonzini
Browse files

nbd: convert to using I/O channels for actual socket I/O



Now that all callers are converted to use I/O channels for
initial connection setup, it is possible to switch the core
NBD protocol handling core over to use QIOChannel APIs for
actual sockets I/O.

Signed-off-by: default avatarDaniel P. Berrange <berrange@redhat.com>
Message-Id: <1455129674-17255-7-git-send-email-berrange@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent ae398278
Loading
Loading
Loading
Loading
+10 −9
Original line number Diff line number Diff line
@@ -80,7 +80,7 @@ static void nbd_reply_ready(void *opaque)
         * that another thread has done the same thing in parallel, so
         * the socket is not readable anymore.
         */
        ret = nbd_receive_reply(s->sioc->fd, &s->reply);
        ret = nbd_receive_reply(s->ioc, &s->reply);
        if (ret == -EAGAIN) {
            return;
        }
@@ -131,6 +131,7 @@ static int nbd_co_send_request(BlockDriverState *bs,
        }
    }

    g_assert(qemu_in_coroutine());
    assert(i < MAX_NBD_REQUESTS);
    request->handle = INDEX_TO_HANDLE(s, i);

@@ -146,17 +147,17 @@ static int nbd_co_send_request(BlockDriverState *bs,
                       nbd_reply_ready, nbd_restart_write, bs);
    if (qiov) {
        qio_channel_set_cork(s->ioc, true);
        rc = nbd_send_request(s->sioc->fd, request);
        rc = nbd_send_request(s->ioc, request);
        if (rc >= 0) {
            ret = qemu_co_sendv(s->sioc->fd, qiov->iov, qiov->niov,
                                offset, request->len);
            ret = nbd_wr_syncv(s->ioc, qiov->iov, qiov->niov,
                               offset, request->len, 0);
            if (ret != request->len) {
                rc = -EIO;
            }
        }
        qio_channel_set_cork(s->ioc, false);
    } else {
        rc = nbd_send_request(s->sioc->fd, request);
        rc = nbd_send_request(s->ioc, request);
    }
    aio_set_fd_handler(aio_context, s->sioc->fd, false,
                       nbd_reply_ready, NULL, bs);
@@ -180,8 +181,8 @@ static void nbd_co_receive_reply(NbdClientSession *s,
        reply->error = EIO;
    } else {
        if (qiov && reply->error == 0) {
            ret = qemu_co_recvv(s->sioc->fd, qiov->iov, qiov->niov,
                                offset, request->len);
            ret = nbd_wr_syncv(s->ioc, qiov->iov, qiov->niov,
                               offset, request->len, 1);
            if (ret != request->len) {
                reply->error = EIO;
            }
@@ -388,7 +389,7 @@ void nbd_client_close(BlockDriverState *bs)
        return;
    }

    nbd_send_request(client->sioc->fd, &request);
    nbd_send_request(client->ioc, &request);

    nbd_teardown_connection(bs);
}
@@ -403,7 +404,7 @@ int nbd_client_init(BlockDriverState *bs, QIOChannelSocket *sioc,
    logout("session init %s\n", export);
    qio_channel_set_blocking(QIO_CHANNEL(sioc), true, NULL);

    ret = nbd_receive_negotiate(sioc->fd, export,
    ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), export,
                                &client->nbdflags, &client->size, errp);
    if (ret < 0) {
        logout("Failed to negotiate with the NBD server\n");
+1 −5
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ static gboolean nbd_accept(QIOChannel *ioc, GIOCondition condition,
                           gpointer opaque)
{
    QIOChannelSocket *cioc;
    int fd;

    cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
                                     NULL);
@@ -35,10 +34,7 @@ static gboolean nbd_accept(QIOChannel *ioc, GIOCondition condition,
        return TRUE;
    }

    fd = dup(cioc->fd);
    if (fd >= 0) {
        nbd_client_new(NULL, fd, nbd_client_put);
    }
    nbd_client_new(NULL, cioc, nbd_client_put);
    object_unref(OBJECT(cioc));
    return TRUE;
}
+14 −6
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@

#include "qemu-common.h"
#include "qemu/option.h"
#include "io/channel-socket.h"

struct nbd_request {
    uint32_t magic;
@@ -73,12 +74,17 @@ enum {
/* Maximum size of a single READ/WRITE data buffer */
#define NBD_MAX_BUFFER_SIZE (32 * 1024 * 1024)

ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read);
int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
ssize_t nbd_wr_syncv(QIOChannel *ioc,
                     struct iovec *iov,
                     size_t niov,
                     size_t offset,
                     size_t length,
                     bool do_read);
int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
                          off_t *size, Error **errp);
int nbd_init(int fd, int csock, uint32_t flags, off_t size);
ssize_t nbd_send_request(int csock, struct nbd_request *request);
ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply);
int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size);
ssize_t nbd_send_request(QIOChannel *ioc, struct nbd_request *request);
ssize_t nbd_receive_reply(QIOChannel *ioc, struct nbd_reply *reply);
int nbd_client(int fd);
int nbd_disconnect(int fd);

@@ -98,7 +104,9 @@ NBDExport *nbd_export_find(const char *name);
void nbd_export_set_name(NBDExport *exp, const char *name);
void nbd_export_close_all(void);

void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *));
void nbd_client_new(NBDExport *exp,
                    QIOChannelSocket *sioc,
                    void (*close)(NBDClient *));
void nbd_client_get(NBDClient *client);
void nbd_client_put(NBDClient *client);

+20 −20
Original line number Diff line number Diff line
@@ -71,7 +71,7 @@ static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);

*/

int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
                          off_t *size, Error **errp)
{
    char buf[256];
@@ -83,7 +83,7 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,

    rc = -EINVAL;

    if (read_sync(csock, buf, 8) != 8) {
    if (read_sync(ioc, buf, 8) != 8) {
        error_setg(errp, "Failed to read data");
        goto fail;
    }
@@ -109,7 +109,7 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
        goto fail;
    }

    if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
    if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
        error_setg(errp, "Failed to read magic");
        goto fail;
    }
@@ -130,35 +130,35 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
            }
            goto fail;
        }
        if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
        if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) {
            error_setg(errp, "Failed to read server flags");
            goto fail;
        }
        *flags = be16_to_cpu(tmp) << 16;
        /* reserved for future use */
        if (write_sync(csock, &reserved, sizeof(reserved)) !=
        if (write_sync(ioc, &reserved, sizeof(reserved)) !=
            sizeof(reserved)) {
            error_setg(errp, "Failed to read reserved field");
            goto fail;
        }
        /* write the export name */
        magic = cpu_to_be64(magic);
        if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
        if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
            error_setg(errp, "Failed to send export name magic");
            goto fail;
        }
        opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
        if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
        if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
            error_setg(errp, "Failed to send export name option number");
            goto fail;
        }
        namesize = cpu_to_be32(strlen(name));
        if (write_sync(csock, &namesize, sizeof(namesize)) !=
        if (write_sync(ioc, &namesize, sizeof(namesize)) !=
            sizeof(namesize)) {
            error_setg(errp, "Failed to send export name length");
            goto fail;
        }
        if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
        if (write_sync(ioc, (char *)name, strlen(name)) != strlen(name)) {
            error_setg(errp, "Failed to send export name");
            goto fail;
        }
@@ -175,7 +175,7 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
        }
    }

    if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
    if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
        error_setg(errp, "Failed to read export length");
        goto fail;
    }
@@ -183,19 +183,19 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
    TRACE("Size is %" PRIu64, *size);

    if (!name) {
        if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
        if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) {
            error_setg(errp, "Failed to read export flags");
            goto fail;
        }
        *flags = be32_to_cpup(flags);
    } else {
        if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
        if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) {
            error_setg(errp, "Failed to read export flags");
            goto fail;
        }
        *flags |= be16_to_cpu(tmp);
    }
    if (read_sync(csock, &buf, 124) != 124) {
    if (read_sync(ioc, &buf, 124) != 124) {
        error_setg(errp, "Failed to read reserved block");
        goto fail;
    }
@@ -206,11 +206,11 @@ fail:
}

#ifdef __linux__
int nbd_init(int fd, int csock, uint32_t flags, off_t size)
int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size)
{
    TRACE("Setting NBD socket");

    if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
    if (ioctl(fd, NBD_SET_SOCK, sioc->fd) < 0) {
        int serrno = errno;
        LOG("Failed to set NBD socket");
        return -serrno;
@@ -283,7 +283,7 @@ int nbd_client(int fd)
    return ret;
}
#else
int nbd_init(int fd, int csock, uint32_t flags, off_t size)
int nbd_init(int fd, QIOChannelSocket *ioc, uint32_t flags, off_t size)
{
    return -ENOTSUP;
}
@@ -294,7 +294,7 @@ int nbd_client(int fd)
}
#endif

ssize_t nbd_send_request(int csock, struct nbd_request *request)
ssize_t nbd_send_request(QIOChannel *ioc, struct nbd_request *request)
{
    uint8_t buf[NBD_REQUEST_SIZE];
    ssize_t ret;
@@ -309,7 +309,7 @@ ssize_t nbd_send_request(int csock, struct nbd_request *request)
          "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
          request->from, request->len, request->handle, request->type);

    ret = write_sync(csock, buf, sizeof(buf));
    ret = write_sync(ioc, buf, sizeof(buf));
    if (ret < 0) {
        return ret;
    }
@@ -321,13 +321,13 @@ ssize_t nbd_send_request(int csock, struct nbd_request *request)
    return 0;
}

ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
ssize_t nbd_receive_reply(QIOChannel *ioc, struct nbd_reply *reply)
{
    uint8_t buf[NBD_REPLY_SIZE];
    uint32_t magic;
    ssize_t ret;

    ret = read_sync(csock, buf, sizeof(buf));
    ret = read_sync(ioc, buf, sizeof(buf));
    if (ret < 0) {
        return ret;
    }
+40 −28
Original line number Diff line number Diff line
@@ -19,47 +19,59 @@
#include "qemu/osdep.h"
#include "nbd-internal.h"

ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
ssize_t nbd_wr_syncv(QIOChannel *ioc,
                     struct iovec *iov,
                     size_t niov,
                     size_t offset,
                     size_t length,
                     bool do_read)
{
    size_t offset = 0;
    int err;
    ssize_t done = 0;
    Error *local_err = NULL;
    struct iovec *local_iov = g_new(struct iovec, niov);
    struct iovec *local_iov_head = local_iov;
    unsigned int nlocal_iov = niov;

    if (qemu_in_coroutine()) {
        if (do_read) {
            return qemu_co_recv(fd, buffer, size);
        } else {
            return qemu_co_send(fd, buffer, size);
        }
    }
    nlocal_iov = iov_copy(local_iov, nlocal_iov,
                          iov, niov,
                          offset, length);

    while (offset < size) {
    while (nlocal_iov > 0) {
        ssize_t len;

        if (do_read) {
            len = qemu_recv(fd, buffer + offset, size - offset, 0);
            len = qio_channel_readv(ioc, local_iov, nlocal_iov, &local_err);
        } else {
            len = send(fd, buffer + offset, size - offset, 0);
            len = qio_channel_writev(ioc, local_iov, nlocal_iov, &local_err);
        }
        if (len == QIO_CHANNEL_ERR_BLOCK) {
            if (qemu_in_coroutine()) {
                /* XXX figure out if we can create a variant on
                 * qio_channel_yield() that works with AIO contexts
                 * and consider using that in this branch */
                qemu_coroutine_yield();
            } else {
                qio_channel_wait(ioc,
                                 do_read ? G_IO_IN : G_IO_OUT);
            }

        if (len < 0) {
            err = socket_error();

            /* recoverable error */
            if (err == EINTR || (offset > 0 && (err == EAGAIN || err == EWOULDBLOCK))) {
            continue;
        }

            /* unrecoverable error */
            return -err;
        if (len < 0) {
            TRACE("I/O error: %s", error_get_pretty(local_err));
            error_free(local_err);
            /* XXX handle Error objects */
            done = -EIO;
            goto cleanup;
        }

        /* eof */
        if (len == 0) {
        if (do_read && len == 0) {
            break;
        }

        offset += len;
        iov_discard_front(&local_iov, &nlocal_iov, len);
        done += len;
    }

    return offset;
 cleanup:
    g_free(local_iov_head);
    return done;
}
Loading