All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Marek Marczykowski-Górecki" <marmarek@invisiblethingslab.com>
To: Anthony PERARD <anthony.perard@citrix.com>
Cc: xen-devel@lists.xenproject.org,
	Ian Jackson <ian.jackson@eu.citrix.com>,
	Wei Liu <wei.liu2@citrix.com>
Subject: Re: [PATCH v6 05/11] libxl_qmp: Implementation of libxl__ev_qmp_*
Date: Thu, 22 Nov 2018 20:04:53 +0100	[thread overview]
Message-ID: <20181122190453.GA6260@mail-itl> (raw)
In-Reply-To: <20181112164930.25893-6-anthony.perard@citrix.com>


[-- Attachment #1.1: Type: text/plain, Size: 4514 bytes --]

On Mon, Nov 12, 2018 at 04:49:24PM +0000, Anthony PERARD wrote:
(...)
> +/* QMP FD callbacks */
> +
> +static void qmp_ev_fd_callback(libxl__egc *egc, libxl__ev_fd *ev_fd,
> +                               int fd, short events, short revents)
> +{
> +    EGC_GC;
> +    int rc;
> +    libxl__json_object *o = NULL;
> +    libxl__ev_qmp *ev = CONTAINER_OF(ev_fd, *ev, qmp_efd);
> +
> +    if (revents & (POLLHUP)) {
> +        LOGD(ERROR, ev->domid, "received POLLHUP from QMP socket");
> +        rc = ERROR_PROTOCOL_ERROR_QMP;
> +        goto out;
> +    }
> +    if (revents & ~(POLLIN|POLLOUT)) {
> +        LOGD(ERROR, ev->domid,
> +             "unexpected poll event 0x%x on QMP socket (expected POLLIN "
> +             "and/or POLLOUT)",
> +            revents);
> +        rc = ERROR_FAIL;
> +        goto out;
> +    }
> +
> +    if (revents & POLLOUT) {
> +        rc = qmp_ev_callback_writable(gc, ev, fd);
> +        if (rc)
> +            goto out;
> +    }
> +
> +    if (revents & POLLIN) {
> +        rc = qmp_ev_callback_readable(egc, ev, fd);
> +        if (rc)
> +            goto out;
> +
> +        /* parse input */
> +        while (1) {
> +            /* parse rx buffer to find one json object */
> +            rc = qmp_ev_get_next_msg(egc, ev, &o);
> +            if (rc == ERROR_NOTFOUND) {
> +                rc = 0;
> +                break;
> +            } else if (rc)
> +                goto out;
> +
> +            /* Must be last and return when the user callback is called */
> +            rc = qmp_ev_handle_message(egc, ev, o);
> +            if (rc < 0)
> +                goto out;
> +            if (rc == 1) {
> +                /* user callback has been called */
> +                return;
> +            }
> +        }
> +    }
> +
> +    qmp_ev_ensure_reading_writing(gc, ev);
> +
> +out:
> +    if (rc) {
> +        LOGD(ERROR, ev->domid,
> +             "Error happend with the QMP connection to QEMU");
> +
> +        /* On error, deallocate all private ressources */
> +        libxl__ev_qmp_dispose(gc, ev);
> +
> +        /* And tell libxl__ev_qmp user about the error */
> +        ev->callback(egc, ev, NULL, rc); /* must be last */
> +    }
> +}
> +
(...)
> +static int qmp_ev_callback_readable(libxl__egc *egc,
> +                                    libxl__ev_qmp *ev, int fd)
> +{
> +    EGC_GC;
> +
> +    while (1) {
> +        ssize_t r;
> +
> +        /* Check if the buffer still have space, or increase size */
> +        if (ev->rx_buf_size - ev->rx_buf_used < QMP_RECEIVE_BUFFER_SIZE) {
> +            ev->rx_buf_size = max(ev->rx_buf_size * 2,
> +                               (size_t)QMP_RECEIVE_BUFFER_SIZE * 2);
> +            assert(ev->rx_buf_size <= QMP_MAX_SIZE_RX_BUF);
> +            if (ev->rx_buf_size > QMP_MAX_SIZE_RX_BUF) {
> +                LOGD(ERROR, ev->domid,
> +                     "QMP receive buffer is too big (%ld > %lld)",
> +                     ev->rx_buf_size, QMP_MAX_SIZE_RX_BUF);
> +                return ERROR_BUFFERFULL;

What if you receive multiple messages (events?), but actually a single
message do fit in a buffer? I think it would be better to stop receiving
the data in the case of buffer full, but try to find a valid message
before erroring out.

> +            }
> +            ev->rx_buf = libxl__realloc(NOGC, ev->rx_buf, ev->rx_buf_size);
> +        }
> +
> +        r = read(fd, ev->rx_buf + ev->rx_buf_used,
> +                 ev->rx_buf_size - ev->rx_buf_used);
> +        if (r < 0) {
> +            if (errno == EINTR) {
> +                continue;
> +            }
> +            if (errno == EWOULDBLOCK) {
> +                break;
> +            }
> +            LOGED(ERROR, ev->domid, "error reading QMP socket");
> +            return ERROR_FAIL;
> +        }
> +
> +        if (r == 0) {
> +            LOGD(ERROR, ev->domid, "Unexpected EOF on QMP socket");
> +            return ERROR_PROTOCOL_ERROR_QMP;
> +        }
> +
> +        LOG_QMP("received %ldB: '%.*s'", r,
> +                (int)r, ev->rx_buf + ev->rx_buf_used);
> +
> +        ev->rx_buf_used += r;
> +        assert(ev->rx_buf_used <= ev->rx_buf_size);
> +    }
> +
> +    return 0;
> +}

(...)

-- 
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 157 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2018-11-22 19:05 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-12 16:49 [PATCH v6 00/11] libxl: Enable save/restore/migration of a restricted QEMU + libxl__ev_qmp_* Anthony PERARD
2018-11-12 16:49 ` [PATCH v6 01/11] libxl: Enhance libxl__sendmsg_fds to deal with EINTR and EWOULDBLOCK Anthony PERARD
2018-11-12 16:58   ` Ian Jackson
2018-11-12 16:49 ` [PATCH v6 02/11] libxl_qmp: Separate QMP message generation from qmp_send_prepare Anthony PERARD
2018-11-12 17:20   ` Ian Jackson
2018-11-13 10:59     ` Anthony PERARD
2018-11-13 12:04       ` Ian Jackson
2018-11-12 16:49 ` [PATCH v6 03/11] libxl_qmp: Change qmp_qemu_check_version to compare version Anthony PERARD
2018-11-12 17:22   ` Ian Jackson
2018-11-22 12:24     ` Anthony PERARD
2018-11-22 12:27       ` Anthony PERARD
2018-11-12 16:49 ` [PATCH v6 04/11] libxl: Design of an async API to issue QMP commands to QEMU Anthony PERARD
2018-11-12 17:29   ` Ian Jackson
2018-11-12 16:49 ` [PATCH v6 05/11] libxl_qmp: Implementation of libxl__ev_qmp_* Anthony PERARD
2018-11-13 11:33   ` Ian Jackson
2018-11-22 19:04   ` Marek Marczykowski-Górecki [this message]
2018-11-23 11:09     ` Anthony PERARD
2018-11-12 16:49 ` [PATCH v6 06/11] libxl_exec: Add libxl__spawn_initiate_failure Anthony PERARD
2018-11-12 17:34   ` Ian Jackson
2018-11-12 16:49 ` [PATCH v6 07/11] libxl_dm: Pre-open QMP socket for QEMU Anthony PERARD
2018-11-16 11:52   ` Ian Jackson
2018-11-21 17:14     ` Anthony PERARD
2018-11-12 16:49 ` [PATCH v6 08/11] libxl: QEMU startup sync based on QMP Anthony PERARD
2018-11-16 12:14   ` Ian Jackson
2018-11-21 16:49     ` Anthony PERARD
2018-11-22 14:37       ` Anthony PERARD
2018-11-22 15:48         ` Ian Jackson
2018-11-12 16:49 ` [PATCH v6 09/11] libxl_qmp: Store advertised QEMU version in libxl__ev_qmp Anthony PERARD
2018-11-16 12:17   ` Ian Jackson
2018-11-12 16:49 ` [PATCH v6 10/11] libxl: Change libxl__domain_suspend_device_model() to be async Anthony PERARD
2018-11-12 16:49 ` [PATCH v6 11/11] libxl: Re-implement domain_suspend_device_model using libxl__ev_qmp Anthony PERARD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181122190453.GA6260@mail-itl \
    --to=marmarek@invisiblethingslab.com \
    --cc=anthony.perard@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.