[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RFC][PATCH v2 14/17] guest agent: core marshal/dispatch in
From: |
Michael Roth |
Subject: |
[Qemu-devel] [RFC][PATCH v2 14/17] guest agent: core marshal/dispatch interfaces |
Date: |
Mon, 18 Apr 2011 10:02:30 -0500 |
These are basically a stripped-down, guest-side analogue to what's in
qmp_core.c: definitions used by qmp-gen.py-generated marshalling code to
handle dispatch and a registration of command->function mappings (minus
all the bits related to Qmp sessions/server/events).
As a result of that, there is a little bit of code duplication here.
It wouldn't be difficult to make the common bits shareable, but there
isn't much.
The guest agent will use these interfaces to handle dispatch/execution
of requests it gets from the proxy.
Signed-off-by: Michael Roth <address@hidden>
---
qga/guest-agent-core.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++
qga/guest-agent-core.h | 40 ++++++++++++++
2 files changed, 179 insertions(+), 0 deletions(-)
create mode 100644 qga/guest-agent-core.c
create mode 100644 qga/guest-agent-core.h
diff --git a/qga/guest-agent-core.c b/qga/guest-agent-core.c
new file mode 100644
index 0000000..55498b1
--- /dev/null
+++ b/qga/guest-agent-core.c
@@ -0,0 +1,139 @@
+/*
+ * QEMU Guest Agent core functions
+ *
+ * Copyright IBM Corp. 2011
+ *
+ * Authors:
+ * Adam Litke <address@hidden>
+ * Michael Roth <address@hidden>
+ *
+ * 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 "guest-agent-core.h"
+
+typedef enum QmpCommandType {
+ QCT_NORMAL,
+ QCT_ASYNC,
+} QmpCommandType;
+
+typedef struct QmpCommand {
+ const char *name;
+ QmpCommandType type;
+ QmpCommandFunc *fn;
+ QmpAsyncCommandFunc *afn;
+ QTAILQ_ENTRY(QmpCommand) node;
+} QmpCommand;
+
+static QTAILQ_HEAD(, QmpCommand) qmp_commands =
+ QTAILQ_HEAD_INITIALIZER(qmp_commands);
+
+QmpMarshalState *qmp_state_get_mstate(QmpState *sess)
+{
+ return NULL;
+}
+
+void qga_register_command(const char *name, QmpCommandFunc *fn)
+{
+ QmpCommand *cmd = qemu_mallocz(sizeof(*cmd));
+
+ cmd->name = name;
+ cmd->type = QCT_NORMAL;
+ cmd->fn = fn;
+ QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
+}
+
+static QmpCommand *qmp_find_command(const char *name)
+{
+ QmpCommand *i;
+
+ QTAILQ_FOREACH(i, &qmp_commands, node) {
+ if (strcmp(i->name, name) == 0) {
+ return i;
+ }
+ }
+ return NULL;
+}
+
+static QObject *qga_dispatch_err(QObject *request, Error **errp)
+{
+ const char *command;
+ QDict *args, *dict;
+ QmpCommand *cmd;
+ QObject *ret = NULL;
+
+ if (qobject_type(request) != QTYPE_QDICT) {
+ error_set(errp, QERR_JSON_PARSE_ERROR, "request is not a dictionary");
+ goto out;
+ }
+
+ dict = qobject_to_qdict(request);
+ if (!qdict_haskey(dict, "execute")) {
+ error_set(errp, QERR_JSON_PARSE_ERROR, "no execute key");
+ goto out;
+ }
+
+ command = qdict_get_str(dict, "execute");
+ cmd = qmp_find_command(command);
+ if (cmd == NULL) {
+ error_set(errp, QERR_COMMAND_NOT_FOUND, command);
+ goto out;
+ }
+
+ if (!qdict_haskey(dict, "arguments")) {
+ args = qdict_new();
+ } else {
+ args = qdict_get_qdict(dict, "arguments");
+ QINCREF(args);
+ }
+
+ switch (cmd->type) {
+ case QCT_NORMAL:
+ cmd->fn(NULL, args, &ret, errp);
+ if (ret == NULL) {
+ ret = QOBJECT(qdict_new());
+ }
+ break;
+ case QCT_ASYNC: {
+ QmpCommandState *s = qemu_mallocz(sizeof(*s));
+ /* FIXME save async commands and do something
+ * smart if disconnect occurs before completion
+ */
+ s->tag = NULL;
+ if (qdict_haskey(dict, "tag")) {
+ s->tag = qdict_get(dict, "tag");
+ qobject_incref(s->tag);
+ }
+ cmd->afn(args, errp, s);
+ ret = NULL;
+ }
+ break;
+ }
+
+ QDECREF(args);
+
+out:
+ return ret;
+}
+
+QObject *qga_dispatch(QObject *request, Error **errp)
+{
+ Error *err = NULL;
+ QObject *ret;
+ QDict *rsp;
+
+ ret = qga_dispatch_err(request, &err);
+
+ rsp = qdict_new();
+ if (err) {
+ qdict_put_obj(rsp, "error", error_get_qobject(err));
+ error_free(err);
+ } else if (ret) {
+ qdict_put_obj(rsp, "return", ret);
+ } else {
+ QDECREF(rsp);
+ return NULL;
+ }
+
+ return QOBJECT(rsp);
+}
diff --git a/qga/guest-agent-core.h b/qga/guest-agent-core.h
new file mode 100644
index 0000000..51ae798
--- /dev/null
+++ b/qga/guest-agent-core.h
@@ -0,0 +1,40 @@
+/*
+ * QEMU Guest Agent core declarations
+ *
+ * Copyright IBM Corp. 2011
+ *
+ * Authors:
+ * Adam Litke <address@hidden>
+ * Michael Roth <address@hidden>
+ *
+ * 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 "qmp-core.h"
+#include "qapi-types-core.h"
+#include "qmp-marshal-types-core.h"
+#include "qemu-common.h"
+#include "qdict.h"
+
+#define QGA_VERSION "1.0"
+
+typedef struct GAState GAState;
+typedef struct GACommandState GACommandState;
+typedef struct GAWorker GAWorker;
+typedef void (*ga_worker_func)(void *input, void *output, Error **errp);
+
+QObject *qga_dispatch(QObject *obj, Error **errp);
+void qga_register_command(const char *name, QmpCommandFunc *fn);
+void qga_init_marshal(void);
+void ga_command_state_init(GAState *s, GACommandState *cs);
+void ga_command_state_add(GACommandState *cs,
+ void (*init)(void),
+ void (*cleanup)(void));
+void ga_command_state_init_all(GACommandState *cs);
+void ga_command_state_cleanup_all(GACommandState *cs);
+GACommandState *ga_command_state_new(void);
+GAWorker *ga_worker_new(ga_worker_func func);
+void ga_worker_cleanup(GAWorker *worker);
+bool ga_worker_dispatch(GAWorker *worker, void *input, void *output,
+ int timeout, Error **errp);
+int ga_get_timeout(GAState *s);
--
1.7.0.4
- Re: [Qemu-devel] [RFC][PATCH v2 12/17] guest agent: worker thread class, (continued)
- [Qemu-devel] [RFC][PATCH v2 16/17] guest agent: add guest agent RPCs/commands, Michael Roth, 2011/04/18
- [Qemu-devel] [RFC][PATCH v2 15/17] guest agent: qemu-ga daemon, Michael Roth, 2011/04/18
- Re: [Qemu-devel] [RFC][PATCH v2 15/17] guest agent: qemu-ga daemon, Jes Sorensen, 2011/04/21
- Re: [Qemu-devel] [RFC][PATCH v2 15/17] guest agent: qemu-ga daemon, Michael Roth, 2011/04/22
- Re: [Qemu-devel] [RFC][PATCH v2 15/17] guest agent: qemu-ga daemon, Ian Molton, 2011/04/22
- Re: [Qemu-devel] [RFC][PATCH v2 15/17] guest agent: qemu-ga daemon, Jes Sorensen, 2011/04/22
- Re: [Qemu-devel] [RFC][PATCH v2 15/17] guest agent: qemu-ga daemon, Ian Molton, 2011/04/25
- Re: [Qemu-devel] [RFC][PATCH v2 15/17] guest agent: qemu-ga daemon, Jes Sorensen, 2011/04/26
[Qemu-devel] [RFC][PATCH v2 13/17] guest agent: command state class, Michael Roth, 2011/04/18
[Qemu-devel] [RFC][PATCH v2 14/17] guest agent: core marshal/dispatch interfaces,
Michael Roth <=
[Qemu-devel] [RFC][PATCH v2 17/17] guest agent: build qemu-ga, add QEMU-wide gio dep, Michael Roth, 2011/04/18
[Qemu-devel] [RFC][PATCH v2 11/17] qmp proxy: build QEMU with qmp proxy, Michael Roth, 2011/04/18
Re: [Qemu-devel] [RFC][PATCH v2 00/11] QEMU Guest Agent: QMP-based host/guest communication (virtagent), Jes Sorensen, 2011/04/21
Re: [Qemu-devel] [RFC][PATCH v2 00/11] QEMU Guest Agent: QMP-based host/guest communication (virtagent), Jes Sorensen, 2011/04/21