blob: 0452056d427a671035a58519c64761dbf170a819 [file] [log] [blame]
Michael Rothe4e6aa12011-07-19 14:50:34 -05001/*
2 * Core Definitions for QAPI/QMP Command Registry
3 *
Eric Blake08f95412016-01-29 06:48:59 -07004 * Copyright (C) 2012-2016 Red Hat, Inc.
Michael Rothe4e6aa12011-07-19 14:50:34 -05005 * Copyright IBM, Corp. 2011
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
Peter Maydellcbf21152016-01-29 17:49:57 +000015#include "qemu/osdep.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010016#include "qapi/qmp-output-visitor.h"
17#include "qapi/visitor-impl.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/queue.h"
Michael Rothe4e6aa12011-07-19 14:50:34 -050019#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010020#include "qapi/qmp/types.h"
Michael Rothe4e6aa12011-07-19 14:50:34 -050021
22typedef struct QStackEntry
23{
24 QObject *value;
Eric Blake1158bb22016-06-09 10:48:34 -060025 void *qapi; /* sanity check that caller uses same pointer */
Michael Rothe4e6aa12011-07-19 14:50:34 -050026 QTAILQ_ENTRY(QStackEntry) node;
27} QStackEntry;
28
29typedef QTAILQ_HEAD(QStack, QStackEntry) QStack;
30
31struct QmpOutputVisitor
32{
33 Visitor visitor;
Eric Blake455ba082016-01-29 06:49:01 -070034 QStack stack; /* Stack of containers that haven't yet been finished */
35 QObject *root; /* Root of the output visit */
Eric Blake3b098d52016-06-09 10:48:43 -060036 QObject **result; /* User's storage location for result */
Michael Rothe4e6aa12011-07-19 14:50:34 -050037};
38
39#define qmp_output_add(qov, name, value) \
40 qmp_output_add_obj(qov, name, QOBJECT(value))
Eric Blake1158bb22016-06-09 10:48:34 -060041#define qmp_output_push(qov, value, qapi) \
42 qmp_output_push_obj(qov, QOBJECT(value), qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050043
44static QmpOutputVisitor *to_qov(Visitor *v)
45{
46 return container_of(v, QmpOutputVisitor, visitor);
47}
48
Eric Blakea8615642016-01-29 06:49:00 -070049/* Push @value onto the stack of current QObjects being built */
Eric Blake1158bb22016-06-09 10:48:34 -060050static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value,
51 void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050052{
Anthony Liguori7267c092011-08-20 22:09:37 -050053 QStackEntry *e = g_malloc0(sizeof(*e));
Michael Rothe4e6aa12011-07-19 14:50:34 -050054
Eric Blake455ba082016-01-29 06:49:01 -070055 assert(qov->root);
Eric Blakea8615642016-01-29 06:49:00 -070056 assert(value);
Michael Rothe4e6aa12011-07-19 14:50:34 -050057 e->value = value;
Eric Blake1158bb22016-06-09 10:48:34 -060058 e->qapi = qapi;
Michael Rothe4e6aa12011-07-19 14:50:34 -050059 QTAILQ_INSERT_HEAD(&qov->stack, e, node);
60}
61
Eric Blakea8615642016-01-29 06:49:00 -070062/* Pop a value off the stack of QObjects being built, and return it. */
Eric Blake1158bb22016-06-09 10:48:34 -060063static QObject *qmp_output_pop(QmpOutputVisitor *qov, void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050064{
65 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
66 QObject *value;
Eric Blakea8615642016-01-29 06:49:00 -070067
68 assert(e);
Eric Blake1158bb22016-06-09 10:48:34 -060069 assert(e->qapi == qapi);
Michael Rothe4e6aa12011-07-19 14:50:34 -050070 QTAILQ_REMOVE(&qov->stack, e, node);
71 value = e->value;
Eric Blakea8615642016-01-29 06:49:00 -070072 assert(value);
Anthony Liguori7267c092011-08-20 22:09:37 -050073 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -050074 return value;
75}
76
Eric Blakea8615642016-01-29 06:49:00 -070077/* Add @value to the current QObject being built.
78 * If the stack is visiting a dictionary or list, @value is now owned
79 * by that container. Otherwise, @value is now the root. */
Michael Rothe4e6aa12011-07-19 14:50:34 -050080static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
81 QObject *value)
82{
Eric Blake455ba082016-01-29 06:49:01 -070083 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
84 QObject *cur = e ? e->value : NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -050085
Eric Blake455ba082016-01-29 06:49:01 -070086 if (!cur) {
Eric Blake56a6f022016-04-28 15:45:26 -060087 /* Don't allow reuse of visitor on more than one root */
88 assert(!qov->root);
Eric Blake455ba082016-01-29 06:49:01 -070089 qov->root = value;
90 } else {
91 switch (qobject_type(cur)) {
92 case QTYPE_QDICT:
93 assert(name);
94 qdict_put_obj(qobject_to_qdict(cur), name, value);
95 break;
96 case QTYPE_QLIST:
Eric Blake56a6f022016-04-28 15:45:26 -060097 assert(!name);
Eric Blake455ba082016-01-29 06:49:01 -070098 qlist_append_obj(qobject_to_qlist(cur), value);
99 break;
100 default:
101 g_assert_not_reached();
102 }
Michael Rothe4e6aa12011-07-19 14:50:34 -0500103 }
104}
105
Eric Blake0b2a0d62016-01-29 06:48:56 -0700106static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
Eric Blake337283d2016-01-29 06:48:57 -0700107 size_t unused, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500108{
109 QmpOutputVisitor *qov = to_qov(v);
110 QDict *dict = qdict_new();
111
112 qmp_output_add(qov, name, dict);
Eric Blake1158bb22016-06-09 10:48:34 -0600113 qmp_output_push(qov, dict, obj);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500114}
115
Eric Blake1158bb22016-06-09 10:48:34 -0600116static void qmp_output_end_struct(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500117{
118 QmpOutputVisitor *qov = to_qov(v);
Eric Blake1158bb22016-06-09 10:48:34 -0600119 QObject *value = qmp_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600120 assert(qobject_type(value) == QTYPE_QDICT);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500121}
122
Eric Blaked9f62dd2016-04-28 15:45:31 -0600123static void qmp_output_start_list(Visitor *v, const char *name,
124 GenericList **listp, size_t size,
125 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500126{
127 QmpOutputVisitor *qov = to_qov(v);
128 QList *list = qlist_new();
129
130 qmp_output_add(qov, name, list);
Eric Blake1158bb22016-06-09 10:48:34 -0600131 qmp_output_push(qov, list, listp);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500132}
133
Eric Blaked9f62dd2016-04-28 15:45:31 -0600134static GenericList *qmp_output_next_list(Visitor *v, GenericList *tail,
Eric Blakee65d89b2016-02-17 23:48:23 -0700135 size_t size)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500136{
Eric Blaked9f62dd2016-04-28 15:45:31 -0600137 return tail->next;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500138}
139
Eric Blake1158bb22016-06-09 10:48:34 -0600140static void qmp_output_end_list(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500141{
142 QmpOutputVisitor *qov = to_qov(v);
Eric Blake1158bb22016-06-09 10:48:34 -0600143 QObject *value = qmp_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600144 assert(qobject_type(value) == QTYPE_QLIST);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500145}
146
Eric Blake0b2a0d62016-01-29 06:48:56 -0700147static void qmp_output_type_int64(Visitor *v, const char *name, int64_t *obj,
Eric Blake4c403142016-01-29 06:48:49 -0700148 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500149{
150 QmpOutputVisitor *qov = to_qov(v);
151 qmp_output_add(qov, name, qint_from_int(*obj));
152}
153
Eric Blake0b2a0d62016-01-29 06:48:56 -0700154static void qmp_output_type_uint64(Visitor *v, const char *name, uint64_t *obj,
Eric Blakef755dea2016-01-29 06:48:50 -0700155 Error **errp)
156{
157 /* FIXME: QMP outputs values larger than INT64_MAX as negative */
158 QmpOutputVisitor *qov = to_qov(v);
159 qmp_output_add(qov, name, qint_from_int(*obj));
160}
161
Eric Blake0b2a0d62016-01-29 06:48:56 -0700162static void qmp_output_type_bool(Visitor *v, const char *name, bool *obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500163 Error **errp)
164{
165 QmpOutputVisitor *qov = to_qov(v);
Eric Blakefc48ffc2015-05-15 16:24:59 -0600166 qmp_output_add(qov, name, qbool_from_bool(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500167}
168
Eric Blake0b2a0d62016-01-29 06:48:56 -0700169static void qmp_output_type_str(Visitor *v, const char *name, char **obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500170 Error **errp)
171{
172 QmpOutputVisitor *qov = to_qov(v);
173 if (*obj) {
174 qmp_output_add(qov, name, qstring_from_str(*obj));
175 } else {
176 qmp_output_add(qov, name, qstring_from_str(""));
177 }
178}
179
Eric Blake0b2a0d62016-01-29 06:48:56 -0700180static void qmp_output_type_number(Visitor *v, const char *name, double *obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500181 Error **errp)
182{
183 QmpOutputVisitor *qov = to_qov(v);
184 qmp_output_add(qov, name, qfloat_from_double(*obj));
185}
186
Eric Blake0b2a0d62016-01-29 06:48:56 -0700187static void qmp_output_type_any(Visitor *v, const char *name, QObject **obj,
Markus Armbruster28770e02015-09-16 13:06:24 +0200188 Error **errp)
189{
190 QmpOutputVisitor *qov = to_qov(v);
191 qobject_incref(*obj);
192 qmp_output_add_obj(qov, name, *obj);
193}
194
Eric Blake3bc97fd2016-04-28 15:45:22 -0600195static void qmp_output_type_null(Visitor *v, const char *name, Error **errp)
196{
Eric Blake3df016f2016-04-28 15:45:23 -0600197 QmpOutputVisitor *qov = to_qov(v);
198 qmp_output_add_obj(qov, name, qnull());
Eric Blake3bc97fd2016-04-28 15:45:22 -0600199}
200
Eric Blake56a6f022016-04-28 15:45:26 -0600201/* Finish building, and return the root object.
202 * The root object is never null. The caller becomes the object's
203 * owner, and should use qobject_decref() when done with it. */
Eric Blake3b098d52016-06-09 10:48:43 -0600204static void qmp_output_complete(Visitor *v, void *opaque)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500205{
Eric Blake3b098d52016-06-09 10:48:43 -0600206 QmpOutputVisitor *qov = to_qov(v);
207
Eric Blake56a6f022016-04-28 15:45:26 -0600208 /* A visit must have occurred, with each start paired with end. */
209 assert(qov->root && QTAILQ_EMPTY(&qov->stack));
Eric Blake3b098d52016-06-09 10:48:43 -0600210 assert(opaque == qov->result);
Eric Blake56a6f022016-04-28 15:45:26 -0600211
212 qobject_incref(qov->root);
Eric Blake3b098d52016-06-09 10:48:43 -0600213 *qov->result = qov->root;
214 qov->result = NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500215}
216
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600217static void qmp_output_free(Visitor *v)
218{
219 QmpOutputVisitor *qov = to_qov(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500220 QStackEntry *e, *tmp;
221
Eric Blake1830f222016-06-09 10:48:40 -0600222 QTAILQ_FOREACH_SAFE(e, &qov->stack, node, tmp) {
223 QTAILQ_REMOVE(&qov->stack, e, node);
Anthony Liguori7267c092011-08-20 22:09:37 -0500224 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500225 }
226
Eric Blake1830f222016-06-09 10:48:40 -0600227 qobject_decref(qov->root);
228 g_free(qov);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500229}
230
Eric Blake3b098d52016-06-09 10:48:43 -0600231Visitor *qmp_output_visitor_new(QObject **result)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500232{
233 QmpOutputVisitor *v;
234
Anthony Liguori7267c092011-08-20 22:09:37 -0500235 v = g_malloc0(sizeof(*v));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500236
Eric Blake983f52d2016-04-28 15:45:09 -0600237 v->visitor.type = VISITOR_OUTPUT;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500238 v->visitor.start_struct = qmp_output_start_struct;
239 v->visitor.end_struct = qmp_output_end_struct;
240 v->visitor.start_list = qmp_output_start_list;
241 v->visitor.next_list = qmp_output_next_list;
242 v->visitor.end_list = qmp_output_end_list;
Eric Blake4c403142016-01-29 06:48:49 -0700243 v->visitor.type_int64 = qmp_output_type_int64;
Eric Blakef755dea2016-01-29 06:48:50 -0700244 v->visitor.type_uint64 = qmp_output_type_uint64;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500245 v->visitor.type_bool = qmp_output_type_bool;
246 v->visitor.type_str = qmp_output_type_str;
247 v->visitor.type_number = qmp_output_type_number;
Markus Armbruster28770e02015-09-16 13:06:24 +0200248 v->visitor.type_any = qmp_output_type_any;
Eric Blake3bc97fd2016-04-28 15:45:22 -0600249 v->visitor.type_null = qmp_output_type_null;
Eric Blake3b098d52016-06-09 10:48:43 -0600250 v->visitor.complete = qmp_output_complete;
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600251 v->visitor.free = qmp_output_free;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500252
253 QTAILQ_INIT(&v->stack);
Eric Blake3b098d52016-06-09 10:48:43 -0600254 *result = NULL;
255 v->result = result;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500256
Eric Blake3b098d52016-06-09 10:48:43 -0600257 return &v->visitor;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500258}