blob: 92c5b5b7bb8e5baa0f87c82c98802c3ceed54eba [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"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010016#include "qapi/qobject-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010017#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 */
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020026 QSLIST_ENTRY(QStackEntry) node;
Michael Rothe4e6aa12011-07-19 14:50:34 -050027} QStackEntry;
28
Michael Rothe4e6aa12011-07-19 14:50:34 -050029struct QmpOutputVisitor
30{
31 Visitor visitor;
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020032 QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */
Eric Blake455ba082016-01-29 06:49:01 -070033 QObject *root; /* Root of the output visit */
Eric Blake3b098d52016-06-09 10:48:43 -060034 QObject **result; /* User's storage location for result */
Michael Rothe4e6aa12011-07-19 14:50:34 -050035};
36
37#define qmp_output_add(qov, name, value) \
38 qmp_output_add_obj(qov, name, QOBJECT(value))
Eric Blake1158bb22016-06-09 10:48:34 -060039#define qmp_output_push(qov, value, qapi) \
40 qmp_output_push_obj(qov, QOBJECT(value), qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050041
42static QmpOutputVisitor *to_qov(Visitor *v)
43{
44 return container_of(v, QmpOutputVisitor, visitor);
45}
46
Eric Blakea8615642016-01-29 06:49:00 -070047/* Push @value onto the stack of current QObjects being built */
Eric Blake1158bb22016-06-09 10:48:34 -060048static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value,
49 void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050050{
Anthony Liguori7267c092011-08-20 22:09:37 -050051 QStackEntry *e = g_malloc0(sizeof(*e));
Michael Rothe4e6aa12011-07-19 14:50:34 -050052
Eric Blake455ba082016-01-29 06:49:01 -070053 assert(qov->root);
Eric Blakea8615642016-01-29 06:49:00 -070054 assert(value);
Michael Rothe4e6aa12011-07-19 14:50:34 -050055 e->value = value;
Eric Blake1158bb22016-06-09 10:48:34 -060056 e->qapi = qapi;
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020057 QSLIST_INSERT_HEAD(&qov->stack, e, node);
Michael Rothe4e6aa12011-07-19 14:50:34 -050058}
59
Eric Blakea8615642016-01-29 06:49:00 -070060/* Pop a value off the stack of QObjects being built, and return it. */
Eric Blake1158bb22016-06-09 10:48:34 -060061static QObject *qmp_output_pop(QmpOutputVisitor *qov, void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050062{
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020063 QStackEntry *e = QSLIST_FIRST(&qov->stack);
Michael Rothe4e6aa12011-07-19 14:50:34 -050064 QObject *value;
Eric Blakea8615642016-01-29 06:49:00 -070065
66 assert(e);
Eric Blake1158bb22016-06-09 10:48:34 -060067 assert(e->qapi == qapi);
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020068 QSLIST_REMOVE_HEAD(&qov->stack, node);
Michael Rothe4e6aa12011-07-19 14:50:34 -050069 value = e->value;
Eric Blakea8615642016-01-29 06:49:00 -070070 assert(value);
Anthony Liguori7267c092011-08-20 22:09:37 -050071 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -050072 return value;
73}
74
Eric Blakea8615642016-01-29 06:49:00 -070075/* Add @value to the current QObject being built.
76 * If the stack is visiting a dictionary or list, @value is now owned
77 * by that container. Otherwise, @value is now the root. */
Michael Rothe4e6aa12011-07-19 14:50:34 -050078static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
79 QObject *value)
80{
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020081 QStackEntry *e = QSLIST_FIRST(&qov->stack);
Eric Blake455ba082016-01-29 06:49:01 -070082 QObject *cur = e ? e->value : NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -050083
Eric Blake455ba082016-01-29 06:49:01 -070084 if (!cur) {
Eric Blake56a6f022016-04-28 15:45:26 -060085 /* Don't allow reuse of visitor on more than one root */
86 assert(!qov->root);
Eric Blake455ba082016-01-29 06:49:01 -070087 qov->root = value;
88 } else {
89 switch (qobject_type(cur)) {
90 case QTYPE_QDICT:
91 assert(name);
92 qdict_put_obj(qobject_to_qdict(cur), name, value);
93 break;
94 case QTYPE_QLIST:
Eric Blake56a6f022016-04-28 15:45:26 -060095 assert(!name);
Eric Blake455ba082016-01-29 06:49:01 -070096 qlist_append_obj(qobject_to_qlist(cur), value);
97 break;
98 default:
99 g_assert_not_reached();
100 }
Michael Rothe4e6aa12011-07-19 14:50:34 -0500101 }
102}
103
Eric Blake0b2a0d62016-01-29 06:48:56 -0700104static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
Eric Blake337283d2016-01-29 06:48:57 -0700105 size_t unused, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500106{
107 QmpOutputVisitor *qov = to_qov(v);
108 QDict *dict = qdict_new();
109
110 qmp_output_add(qov, name, dict);
Eric Blake1158bb22016-06-09 10:48:34 -0600111 qmp_output_push(qov, dict, obj);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500112}
113
Eric Blake1158bb22016-06-09 10:48:34 -0600114static void qmp_output_end_struct(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500115{
116 QmpOutputVisitor *qov = to_qov(v);
Eric Blake1158bb22016-06-09 10:48:34 -0600117 QObject *value = qmp_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600118 assert(qobject_type(value) == QTYPE_QDICT);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500119}
120
Eric Blaked9f62dd2016-04-28 15:45:31 -0600121static void qmp_output_start_list(Visitor *v, const char *name,
122 GenericList **listp, size_t size,
123 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500124{
125 QmpOutputVisitor *qov = to_qov(v);
126 QList *list = qlist_new();
127
128 qmp_output_add(qov, name, list);
Eric Blake1158bb22016-06-09 10:48:34 -0600129 qmp_output_push(qov, list, listp);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500130}
131
Eric Blaked9f62dd2016-04-28 15:45:31 -0600132static GenericList *qmp_output_next_list(Visitor *v, GenericList *tail,
Eric Blakee65d89b2016-02-17 23:48:23 -0700133 size_t size)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500134{
Eric Blaked9f62dd2016-04-28 15:45:31 -0600135 return tail->next;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500136}
137
Eric Blake1158bb22016-06-09 10:48:34 -0600138static void qmp_output_end_list(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500139{
140 QmpOutputVisitor *qov = to_qov(v);
Eric Blake1158bb22016-06-09 10:48:34 -0600141 QObject *value = qmp_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600142 assert(qobject_type(value) == QTYPE_QLIST);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500143}
144
Eric Blake0b2a0d62016-01-29 06:48:56 -0700145static void qmp_output_type_int64(Visitor *v, const char *name, int64_t *obj,
Eric Blake4c403142016-01-29 06:48:49 -0700146 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500147{
148 QmpOutputVisitor *qov = to_qov(v);
149 qmp_output_add(qov, name, qint_from_int(*obj));
150}
151
Eric Blake0b2a0d62016-01-29 06:48:56 -0700152static void qmp_output_type_uint64(Visitor *v, const char *name, uint64_t *obj,
Eric Blakef755dea2016-01-29 06:48:50 -0700153 Error **errp)
154{
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100155 /* FIXME values larger than INT64_MAX become negative */
Eric Blakef755dea2016-01-29 06:48:50 -0700156 QmpOutputVisitor *qov = to_qov(v);
157 qmp_output_add(qov, name, qint_from_int(*obj));
158}
159
Eric Blake0b2a0d62016-01-29 06:48:56 -0700160static void qmp_output_type_bool(Visitor *v, const char *name, bool *obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500161 Error **errp)
162{
163 QmpOutputVisitor *qov = to_qov(v);
Eric Blakefc48ffc2015-05-15 16:24:59 -0600164 qmp_output_add(qov, name, qbool_from_bool(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500165}
166
Eric Blake0b2a0d62016-01-29 06:48:56 -0700167static void qmp_output_type_str(Visitor *v, const char *name, char **obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500168 Error **errp)
169{
170 QmpOutputVisitor *qov = to_qov(v);
171 if (*obj) {
172 qmp_output_add(qov, name, qstring_from_str(*obj));
173 } else {
174 qmp_output_add(qov, name, qstring_from_str(""));
175 }
176}
177
Eric Blake0b2a0d62016-01-29 06:48:56 -0700178static void qmp_output_type_number(Visitor *v, const char *name, double *obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500179 Error **errp)
180{
181 QmpOutputVisitor *qov = to_qov(v);
182 qmp_output_add(qov, name, qfloat_from_double(*obj));
183}
184
Eric Blake0b2a0d62016-01-29 06:48:56 -0700185static void qmp_output_type_any(Visitor *v, const char *name, QObject **obj,
Markus Armbruster28770e02015-09-16 13:06:24 +0200186 Error **errp)
187{
188 QmpOutputVisitor *qov = to_qov(v);
189 qobject_incref(*obj);
190 qmp_output_add_obj(qov, name, *obj);
191}
192
Eric Blake3bc97fd2016-04-28 15:45:22 -0600193static void qmp_output_type_null(Visitor *v, const char *name, Error **errp)
194{
Eric Blake3df016f2016-04-28 15:45:23 -0600195 QmpOutputVisitor *qov = to_qov(v);
196 qmp_output_add_obj(qov, name, qnull());
Eric Blake3bc97fd2016-04-28 15:45:22 -0600197}
198
Eric Blake56a6f022016-04-28 15:45:26 -0600199/* Finish building, and return the root object.
200 * The root object is never null. The caller becomes the object's
201 * owner, and should use qobject_decref() when done with it. */
Eric Blake3b098d52016-06-09 10:48:43 -0600202static void qmp_output_complete(Visitor *v, void *opaque)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500203{
Eric Blake3b098d52016-06-09 10:48:43 -0600204 QmpOutputVisitor *qov = to_qov(v);
205
Eric Blake56a6f022016-04-28 15:45:26 -0600206 /* A visit must have occurred, with each start paired with end. */
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200207 assert(qov->root && QSLIST_EMPTY(&qov->stack));
Eric Blake3b098d52016-06-09 10:48:43 -0600208 assert(opaque == qov->result);
Eric Blake56a6f022016-04-28 15:45:26 -0600209
210 qobject_incref(qov->root);
Eric Blake3b098d52016-06-09 10:48:43 -0600211 *qov->result = qov->root;
212 qov->result = NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500213}
214
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600215static void qmp_output_free(Visitor *v)
216{
217 QmpOutputVisitor *qov = to_qov(v);
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200218 QStackEntry *e;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500219
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200220 while (!QSLIST_EMPTY(&qov->stack)) {
221 e = QSLIST_FIRST(&qov->stack);
222 QSLIST_REMOVE_HEAD(&qov->stack, node);
Anthony Liguori7267c092011-08-20 22:09:37 -0500223 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500224 }
225
Eric Blake1830f222016-06-09 10:48:40 -0600226 qobject_decref(qov->root);
227 g_free(qov);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500228}
229
Eric Blake3b098d52016-06-09 10:48:43 -0600230Visitor *qmp_output_visitor_new(QObject **result)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500231{
232 QmpOutputVisitor *v;
233
Anthony Liguori7267c092011-08-20 22:09:37 -0500234 v = g_malloc0(sizeof(*v));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500235
Eric Blake983f52d2016-04-28 15:45:09 -0600236 v->visitor.type = VISITOR_OUTPUT;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500237 v->visitor.start_struct = qmp_output_start_struct;
238 v->visitor.end_struct = qmp_output_end_struct;
239 v->visitor.start_list = qmp_output_start_list;
240 v->visitor.next_list = qmp_output_next_list;
241 v->visitor.end_list = qmp_output_end_list;
Eric Blake4c403142016-01-29 06:48:49 -0700242 v->visitor.type_int64 = qmp_output_type_int64;
Eric Blakef755dea2016-01-29 06:48:50 -0700243 v->visitor.type_uint64 = qmp_output_type_uint64;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500244 v->visitor.type_bool = qmp_output_type_bool;
245 v->visitor.type_str = qmp_output_type_str;
246 v->visitor.type_number = qmp_output_type_number;
Markus Armbruster28770e02015-09-16 13:06:24 +0200247 v->visitor.type_any = qmp_output_type_any;
Eric Blake3bc97fd2016-04-28 15:45:22 -0600248 v->visitor.type_null = qmp_output_type_null;
Eric Blake3b098d52016-06-09 10:48:43 -0600249 v->visitor.complete = qmp_output_complete;
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600250 v->visitor.free = qmp_output_free;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500251
Eric Blake3b098d52016-06-09 10:48:43 -0600252 *result = NULL;
253 v->result = result;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500254
Eric Blake3b098d52016-06-09 10:48:43 -0600255 return &v->visitor;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500256}