xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Anthony PERARD <anthony.perard@citrix.com>
To: Ian Jackson <ian.jackson@citrix.com>
Cc: xen-devel@lists.xenproject.org, Wei Liu <wei.liu2@citrix.com>
Subject: Re: [PATCH v3.1] libxl: Design of an async API to issue QMP commands to QEMU
Date: Mon, 16 Jul 2018 16:27:38 +0100	[thread overview]
Message-ID: <20180716152738.GA2296@perard.uk.xensource.com> (raw)
In-Reply-To: <23367.33648.582216.233380@mariner.uk.xensource.com>

On Thu, Jul 12, 2018 at 05:36:00PM +0100, Ian Jackson wrote:
> Anthony PERARD writes ("Re: [PATCH v3.1] libxl: Design of an async API to issue QMP commands to QEMU"):
> > I don't think the state of a qmp connection can fit into
> > libxl__qmp_cmd_state. The "Active" state doesn't mean that a qmp
> > connection is open or that the command have been sent. It merely mean
> > that the syscall socket(3) and connect(3) have return successfully, and
> > there will be an attempt later to sent the command to qemu.
> 
> I think you haven't quite understood my point.
> 
> My understanding of your API is that it gives the user of
> libl__qmp_cmd a certain amount of visibility of the existence or
> otherwise of the qmp connection.
> 
> You say:
> 
>   + * When called from within a callback, the same QMP connection will be
>   + * reused to execute the new command. This is important in the case
>   + * where the first command is "add-fd" and the second command use the
>   + * fdset created by QEMU.
> 
> This implies that at the top of the callback, the qmp connection is
> actually in some kind of extra state which is not covered by any of
> your Undefined, Idle or Active.
> 
> It is not Undefined, obviously.
> 
> It is not Active because no response is being awaited any longer.
> (If a response were being awaited, then it would not be sensible for
> the callback to issue another command, because your rule is one
> command at once.)
> 
> And it is not Idle because it contains references to allocated
> resources - namely, the qmp connection fd.
> 
> So your documented state model is too poor to express what is going
> on.  You need to write down at least one additional state, which you
> might call `Connected'.
> 
> 
> Also, I have just noticed that you say "from within a callback".  That
> suggests that the code which makes the callback does something to the
> libxl__qmp_state after after the callback returns.
> 
> That is contrary to the way that everything else works in libxl.  In
> libxl, such a callback is made as the last thing before returning back
> to the event loop.
> 
> The reason is that the state struct (in this case the libxl__qmp_cmd)
> may be part of a larger struct, which is completed and deallocated
> somewhere in the series of (nested) callback functions.  So the memory
> may not be valid any more.
> 
> 
> Another way to look at this is that you actually have a fourth state
> which I will call WithinCallback.  On entry to the callback, the cmd
> is in WithinCallback state.
> 
> In WithinCallback state, it is allowed to request that another command
> is sent (putting the cmd back into Active).
> 
> But in the WithinCallback state, the libxl__qmp_cmd contains
> references to resources and may not be freed.  Furthermore, as I read
> your commentary, the WithinCallback state cannot be exited other than
> to Active, or by returning from the callback.
> 
> That would make it very awkward for the user of this interface to ever
> free the cmd.  After all, they can only free the memory for it when
> the state is Idle or Undefined.  So they need to get it into Idle
> which they can only do by returning, but then they have lost
> control...
> 
> 
> So this is why I say you should have a proper fourth state, which we
> can call Connected or something.  On entry to the callback, the cmd is
> in state Connected.  The cmd should stay Connected until it is
> explicitly disposed of.
> 
> The code which makes the callback then does not need to do anything
> after making the callback: the user has sent another command; or is
> going to send another command; or has called _dispose.  In any case,
> the caller has ownership.

I'll reply to that with a new patch which I hope will anwser your
comments.


> > > Also, I don't think this description of the semantics is right.  The
> > > caller must always somehow arrange to initialise this value.
> > > Presumably _init clears it ?  Certainly this as a parameter to the
> > > operation, this should be up with domid and callback.
> > > 
> > > Maybe you want comments like the ones in libxl__datacopier_state etc.,
> > > which say /* caller must fill these in */.
> > > 
> > > And, you probably want to make it clear that the fd remains open in
> > > the libxl process.  (I assume it does.)
> > 
> > Well I was closing the fd once it was sent to QEMU. But we can have the
> > callbacks takes care of closing it instead.
> 
> I don't mind what the semantics are, but they should be clear, and all
> the error cases need to work correctly.  Eg if asking to send a fd
> causes the fd to become owned by the qmp machiner, then if the attempt
> to send the qmp command fails (maybe becaue the qmp connection fails)
> then it must be closed.
> 
> The semantics should probably be whichever are more convenient.
> Personally i would lean towards qmp_cmd not taking ownership, because
> if you do then someone who wants to keep a copy of the fd has to dup
> it and duping a carefd is quite a faff.

Sounds good, I'll do it this way (the caller will keep ownership).

> > > > +libxl__qmp_error_class = Enumeration("qmp_error_class", [
> > > > +    # No error
> > > > +    (0, "NONE"),
> > > > +    # Error generated by libxl (e.g. socket closed unexpectedly, no mem, ...)
> > > > +    (1, "libxl_error"),
> > > > +    # QMP error classes described in QEMU sources code (QapiErrorClass)
> > > > +    (2, "GenericError"),
> > > > +    (3, "CommandNotFound"),
> > > > +    (4, "DeviceNotActive"),
> > > > +    (5, "DeviceNotFound"),
> > > > +    # Unrecognized QMP error class
> > > > +    (6, "Unknown"),
> > > 
> > > Are these numbers from qmp ?  Why not assign a bunch of libxl error
> > > values instead ?
> > 
> > No, these are strings from QEMU, numbers doesn't matter. Also I don't
> > know how well those are going to fit into libxl errors.
> 
> I meant, invent a libxl error number (and corresponding ERROR_QMP_BLAH
> or whatever) for each one of these qmp errors.

That sounds good, I'll invent new libxl error numbers.

> > (qemu.git:qapi/common.json)
> > # @QapiErrorClass:
> > #
> > # QEMU error classes
> > #
> > # @GenericError: this is used for errors that don't require a specific error
> > #                class. This should be the default case for most errors
> > #
> > # @CommandNotFound: the requested command has not been found
> > #
> > # @DeviceNotActive: a device has failed to be become active
> > #
> > # @DeviceNotFound: the requested device has not been found
> > 
> > QEMU always associate an error messages when it send an error, so the
> > only thing t odo with GenericError is to log that message.
> 
> I guess you should alwyas log the error anyway.  But discriminating
> the different qmp errors will probably be useful ?

I do think both DeviceNotActive and DeviceNotFound can be usefull to
know to a caller, and in anycase, I will keep logging error messages
from qemu.

Thanks,

-- 
Anthony PERARD

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

  reply	other threads:[~2018-07-16 15:27 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-01 14:36 [PATCH v3 00/31] libxl: Enable save/restore/migration of a restricted QEMU + libxl__ev_qmp_* Anthony PERARD
2018-06-01 14:36 ` [PATCH v3 01/31] libxl_event: Fix DEBUG prints Anthony PERARD
2018-06-27 14:19   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 02/31] libxl_qmp: Documentation of the logic of the QMP client Anthony PERARD
2018-06-27 14:20   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 03/31] libxl_qmp: Fix use of DEBUG_RECEIVED Anthony PERARD
2018-06-27 14:20   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 04/31] libxl_json: fix build with DEBUG_ANSWER Anthony PERARD
2018-06-27 14:22   ` Ian Jackson
2018-07-17 14:35     ` Anthony PERARD
2018-06-01 14:36 ` [PATCH v3 05/31] libxl_qmp: Move the buffer realloc to the same scope level as read Anthony PERARD
2018-06-27 14:25   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 06/31] libxl_qmp: Add a warning to not trust QEMU Anthony PERARD
2018-06-27 14:25   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 07/31] libxl_qmp: Learned to send FD through QMP to QEMU Anthony PERARD
2018-06-27 14:26   ` Ian Jackson
2018-06-27 16:50     ` Anthony PERARD
2018-06-28  9:56       ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 08/31] libxl_qmp: Have QEMU save its state to a file descriptor Anthony PERARD
2018-06-27 14:29   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 09/31] libxl_qmp: Move struct sockaddr_un variable to qmp_open() Anthony PERARD
2018-06-27 14:31   ` Ian Jackson
2018-06-27 16:54     ` Anthony PERARD
2018-06-01 14:36 ` [PATCH v3 10/31] libxl_qmp: Move buffers to the stack of qmp_next Anthony PERARD
2018-06-27 14:32   ` Ian Jackson
2018-06-27 16:58     ` Anthony PERARD
2018-06-28  9:57       ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 11/31] libxl_qmp: Remove unused yajl_ctx form handler Anthony PERARD
2018-06-27 14:32   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 12/31] libxl_json: constify libxl__json_object_to_yajl_gen arguments Anthony PERARD
2018-06-27 14:32   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 13/31] libxl_qmp: Separate QMP message generation from qmp_send_prepare Anthony PERARD
2018-06-27 14:45   ` Ian Jackson
2018-06-27 17:12     ` Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 14/31] libxl_qmp_ev: Introduce libxl__ev_qmp_start() to connect to QMP Anthony PERARD
2018-06-27 15:07   ` Ian Jackson
2018-06-28 11:28     ` Anthony PERARD
2018-06-28 11:44       ` Ian Jackson
2018-06-28 13:01         ` Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 15/31] libxl_qmp_ev: Implement fd callback and read data Anthony PERARD
2018-06-27 15:10   ` Ian Jackson
2018-06-28 11:33     ` Anthony PERARD
2018-06-28 11:37       ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 16/31] libxl_json: Allow partial parsing Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 17/31] libxl_json: Enable yajl_allow_trailing_garbage Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 18/31] libxl_json: libxl__json_object_to_json Anthony PERARD
2018-06-27 15:51   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 19/31] libxl_qmp_ev: Parse JSON input from QMP Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 20/31] libxl_qmp: Introduce libxl__ev_qmp functions Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 21/31] libxl_qmp_ev: Handle write to socket Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 22/31] libxl_qmp: Simplify qmp_response_type() prototype Anthony PERARD
2018-06-27 16:03   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 23/31] libxl_qmp_ev: Handle messages from QEMU Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 24/31] libxl_qmp_ev: Respond to QMP greeting Anthony PERARD
2018-06-27 16:07   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 25/31] libxl_qmp_ev: Disconnect QMP when no more events Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 26/31] libxl_qmp: Disable beautify for QMP generated cmd Anthony PERARD
2018-06-27 16:07   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 27/31] libxl_qmp: Implement libxl__qmp_insert_cdrom_ev Anthony PERARD
2018-06-27 16:10   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 28/31] libxl_disk: Cut libxl_cdrom_insert into step Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 29/31] libxl_disk: Have libxl_cdrom_insert use libxl__ev_qmp Anthony PERARD
2018-06-27 16:12   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 30/31] libxl_dm: Pre-open QMP socket for QEMU Anthony PERARD
2018-06-27 16:14   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 31/31] libxl: QEMU startup sync based on QMP Anthony PERARD
2018-06-27 16:16   ` Ian Jackson
2018-07-26 14:59     ` Anthony PERARD
2018-06-01 14:47 ` [PATCH v3 00/31] libxl: Enable save/restore/migration of a restricted QEMU + libxl__ev_qmp_* Anthony PERARD
2018-07-03  9:47 ` [PATCH v3.1] libxl: Design of an async API to issue QMP commands to QEMU Anthony PERARD
2018-07-03 14:48   ` Ian Jackson
2018-07-04 11:11     ` Anthony PERARD
2018-07-12 16:36       ` Ian Jackson
2018-07-16 15:27         ` Anthony PERARD [this message]
2018-07-16 15:28           ` [PATCH v3.2] " Anthony PERARD
2018-07-16 16:33             ` Anthony PERARD
2018-07-16 17:04               ` [PATCH v3.2] libxl: Design of an async API to issue QMP commands to QEMU [and 1 more messages] Ian Jackson
2018-07-18 10:30                 ` 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=20180716152738.GA2296@perard.uk.xensource.com \
    --to=anthony.perard@citrix.com \
    --cc=ian.jackson@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).