blob: 5f0035cbf6954727b38a82c9f1eb92197bc8a0a1 [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 */
Michael Rothe4e6aa12011-07-19 14:50:34 -050036};
37
38#define qmp_output_add(qov, name, value) \
39 qmp_output_add_obj(qov, name, QOBJECT(value))
Eric Blake1158bb22016-06-09 10:48:34 -060040#define qmp_output_push(qov, value, qapi) \
41 qmp_output_push_obj(qov, QOBJECT(value), qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050042
43static QmpOutputVisitor *to_qov(Visitor *v)
44{
45 return container_of(v, QmpOutputVisitor, visitor);
46}
47
Eric Blakea8615642016-01-29 06:49:00 -070048/* Push @value onto the stack of current QObjects being built */
Eric Blake1158bb22016-06-09 10:48:34 -060049static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value,
50 void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050051{
Anthony Liguori7267c092011-08-20 22:09:37 -050052 QStackEntry *e = g_malloc0(sizeof(*e));
Michael Rothe4e6aa12011-07-19 14:50:34 -050053
Eric Blake455ba082016-01-29 06:49:01 -070054 assert(qov->root);
Eric Blakea8615642016-01-29 06:49:00 -070055 assert(value);
Michael Rothe4e6aa12011-07-19 14:50:34 -050056 e->value = value;
Eric Blake1158bb22016-06-09 10:48:34 -060057 e->qapi = qapi;
Michael Rothe4e6aa12011-07-19 14:50:34 -050058 QTAILQ_INSERT_HEAD(&qov->stack, e, node);
59}
60
Eric Blakea8615642016-01-29 06:49:00 -070061/* Pop a value off the stack of QObjects being built, and return it. */
Eric Blake1158bb22016-06-09 10:48:34 -060062static QObject *qmp_output_pop(QmpOutputVisitor *qov, void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050063{
64 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
65 QObject *value;
Eric Blakea8615642016-01-29 06:49:00 -070066
67 assert(e);
Eric Blake1158bb22016-06-09 10:48:34 -060068 assert(e->qapi == qapi);
Michael Rothe4e6aa12011-07-19 14:50:34 -050069 QTAILQ_REMOVE(&qov->stack, e, node);
70 value = e->value;
Eric Blakea8615642016-01-29 06:49:00 -070071 assert(value);
Anthony Liguori7267c092011-08-20 22:09:37 -050072 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -050073 return value;
74}
75
Eric Blakea8615642016-01-29 06:49:00 -070076/* Add @value to the current QObject being built.
77 * If the stack is visiting a dictionary or list, @value is now owned
78 * by that container. Otherwise, @value is now the root. */
Michael Rothe4e6aa12011-07-19 14:50:34 -050079static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
80 QObject *value)
81{
Eric Blake455ba082016-01-29 06:49:01 -070082 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
83 QObject *cur = e ? e->value : NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -050084
Eric Blake455ba082016-01-29 06:49:01 -070085 if (!cur) {
Eric Blake56a6f022016-04-28 15:45:26 -060086 /* Don't allow reuse of visitor on more than one root */
87 assert(!qov->root);
Eric Blake455ba082016-01-29 06:49:01 -070088 qov->root = value;
89 } else {
90 switch (qobject_type(cur)) {
91 case QTYPE_QDICT:
92 assert(name);
93 qdict_put_obj(qobject_to_qdict(cur), name, value);
94 break;
95 case QTYPE_QLIST:
Eric Blake56a6f022016-04-28 15:45:26 -060096 assert(!name);
Eric Blake455ba082016-01-29 06:49:01 -070097 qlist_append_obj(qobject_to_qlist(cur), value);
98 break;
99 default:
100 g_assert_not_reached();
101 }
Michael Rothe4e6aa12011-07-19 14:50:34 -0500102 }
103}
104
Eric Blake0b2a0d62016-01-29 06:48:56 -0700105static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
Eric Blake337283d2016-01-29 06:48:57 -0700106 size_t unused, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500107{
108 QmpOutputVisitor *qov = to_qov(v);
109 QDict *dict = qdict_new();
110
111 qmp_output_add(qov, name, dict);
Eric Blake1158bb22016-06-09 10:48:34 -0600112 qmp_output_push(qov, dict, obj);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500113}
114
Eric Blake1158bb22016-06-09 10:48:34 -0600115static void qmp_output_end_struct(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500116{
117 QmpOutputVisitor *qov = to_qov(v);
Eric Blake1158bb22016-06-09 10:48:34 -0600118 QObject *value = qmp_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600119 assert(qobject_type(value) == QTYPE_QDICT);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500120}
121
Eric Blaked9f62dd2016-04-28 15:45:31 -0600122static void qmp_output_start_list(Visitor *v, const char *name,
123 GenericList **listp, size_t size,
124 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500125{
126 QmpOutputVisitor *qov = to_qov(v);
127 QList *list = qlist_new();
128
129 qmp_output_add(qov, name, list);
Eric Blake1158bb22016-06-09 10:48:34 -0600130 qmp_output_push(qov, list, listp);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500131}
132
Eric Blaked9f62dd2016-04-28 15:45:31 -0600133static GenericList *qmp_output_next_list(Visitor *v, GenericList *tail,
Eric Blakee65d89b2016-02-17 23:48:23 -0700134 size_t size)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500135{
Eric Blaked9f62dd2016-04-28 15:45:31 -0600136 return tail->next;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500137}
138
Eric Blake1158bb22016-06-09 10:48:34 -0600139static void qmp_output_end_list(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500140{
141 QmpOutputVisitor *qov = to_qov(v);
Eric Blake1158bb22016-06-09 10:48:34 -0600142 QObject *value = qmp_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600143 assert(qobject_type(value) == QTYPE_QLIST);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500144}
145
Eric Blake0b2a0d62016-01-29 06:48:56 -0700146static void qmp_output_type_int64(Visitor *v, const char *name, int64_t *obj,
Eric Blake4c403142016-01-29 06:48:49 -0700147 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500148{
149 QmpOutputVisitor *qov = to_qov(v);
150 qmp_output_add(qov, name, qint_from_int(*obj));
151}
152
Eric Blake0b2a0d62016-01-29 06:48:56 -0700153static void qmp_output_type_uint64(Visitor *v, const char *name, uint64_t *obj,
Eric Blakef755dea2016-01-29 06:48:50 -0700154 Error **errp)
155{
156 /* FIXME: QMP outputs values larger than INT64_MAX as negative */
157 QmpOutputVisitor *qov = to_qov(v);
158 qmp_output_add(qov, name, qint_from_int(*obj));
159}
160
Eric Blake0b2a0d62016-01-29 06:48:56 -0700161static void qmp_output_type_bool(Visitor *v, const char *name, bool *obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500162 Error **errp)
163{
164 QmpOutputVisitor *qov = to_qov(v);
Eric Blakefc48ffc2015-05-15 16:24:59 -0600165 qmp_output_add(qov, name, qbool_from_bool(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500166}
167
Eric Blake0b2a0d62016-01-29 06:48:56 -0700168static void qmp_output_type_str(Visitor *v, const char *name, char **obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500169 Error **errp)
170{
171 QmpOutputVisitor *qov = to_qov(v);
172 if (*obj) {
173 qmp_output_add(qov, name, qstring_from_str(*obj));
174 } else {
175 qmp_output_add(qov, name, qstring_from_str(""));
176 }
177}
178
Eric Blake0b2a0d62016-01-29 06:48:56 -0700179static void qmp_output_type_number(Visitor *v, const char *name, double *obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500180 Error **errp)
181{
182 QmpOutputVisitor *qov = to_qov(v);
183 qmp_output_add(qov, name, qfloat_from_double(*obj));
184}
185
Eric Blake0b2a0d62016-01-29 06:48:56 -0700186static void qmp_output_type_any(Visitor *v, const char *name, QObject **obj,
Markus Armbruster28770e02015-09-16 13:06:24 +0200187 Error **errp)
188{
189 QmpOutputVisitor *qov = to_qov(v);
190 qobject_incref(*obj);
191 qmp_output_add_obj(qov, name, *obj);
192}
193
Eric Blake3bc97fd2016-04-28 15:45:22 -0600194static void qmp_output_type_null(Visitor *v, const char *name, Error **errp)
195{
Eric Blake3df016f2016-04-28 15:45:23 -0600196 QmpOutputVisitor *qov = to_qov(v);
197 qmp_output_add_obj(qov, name, qnull());
Eric Blake3bc97fd2016-04-28 15:45:22 -0600198}
199
Eric Blake56a6f022016-04-28 15:45:26 -0600200/* Finish building, and return the root object.
201 * The root object is never null. The caller becomes the object's
202 * owner, and should use qobject_decref() when done with it. */
Michael Rothe4e6aa12011-07-19 14:50:34 -0500203QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
204{
Eric Blake56a6f022016-04-28 15:45:26 -0600205 /* A visit must have occurred, with each start paired with end. */
206 assert(qov->root && QTAILQ_EMPTY(&qov->stack));
207
208 qobject_incref(qov->root);
209 return qov->root;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500210}
211
212Visitor *qmp_output_get_visitor(QmpOutputVisitor *v)
213{
214 return &v->visitor;
215}
216
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600217static void qmp_output_free(Visitor *v)
218{
219 QmpOutputVisitor *qov = to_qov(v);
220
221 qmp_output_visitor_cleanup(qov);
222}
223
Michael Rothe4e6aa12011-07-19 14:50:34 -0500224void qmp_output_visitor_cleanup(QmpOutputVisitor *v)
225{
226 QStackEntry *e, *tmp;
227
228 QTAILQ_FOREACH_SAFE(e, &v->stack, node, tmp) {
229 QTAILQ_REMOVE(&v->stack, e, node);
Anthony Liguori7267c092011-08-20 22:09:37 -0500230 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500231 }
232
Eric Blake455ba082016-01-29 06:49:01 -0700233 qobject_decref(v->root);
Anthony Liguori7267c092011-08-20 22:09:37 -0500234 g_free(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500235}
236
237QmpOutputVisitor *qmp_output_visitor_new(void)
238{
239 QmpOutputVisitor *v;
240
Anthony Liguori7267c092011-08-20 22:09:37 -0500241 v = g_malloc0(sizeof(*v));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500242
Eric Blake983f52d2016-04-28 15:45:09 -0600243 v->visitor.type = VISITOR_OUTPUT;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500244 v->visitor.start_struct = qmp_output_start_struct;
245 v->visitor.end_struct = qmp_output_end_struct;
246 v->visitor.start_list = qmp_output_start_list;
247 v->visitor.next_list = qmp_output_next_list;
248 v->visitor.end_list = qmp_output_end_list;
Eric Blake4c403142016-01-29 06:48:49 -0700249 v->visitor.type_int64 = qmp_output_type_int64;
Eric Blakef755dea2016-01-29 06:48:50 -0700250 v->visitor.type_uint64 = qmp_output_type_uint64;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500251 v->visitor.type_bool = qmp_output_type_bool;
252 v->visitor.type_str = qmp_output_type_str;
253 v->visitor.type_number = qmp_output_type_number;
Markus Armbruster28770e02015-09-16 13:06:24 +0200254 v->visitor.type_any = qmp_output_type_any;
Eric Blake3bc97fd2016-04-28 15:45:22 -0600255 v->visitor.type_null = qmp_output_type_null;
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600256 v->visitor.free = qmp_output_free;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500257
258 QTAILQ_INIT(&v->stack);
259
260 return v;
261}