blob: d44c676317d033cc5b264a36921c1a75ceaa61f8 [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;
Michael Rothe1bc2f72011-09-19 19:03:11 -050025 bool is_list_head;
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))
40#define qmp_output_push(qov, value) qmp_output_push_obj(qov, QOBJECT(value))
41
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 */
Michael Rothe4e6aa12011-07-19 14:50:34 -050048static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value)
49{
Anthony Liguori7267c092011-08-20 22:09:37 -050050 QStackEntry *e = g_malloc0(sizeof(*e));
Michael Rothe4e6aa12011-07-19 14:50:34 -050051
Eric Blake455ba082016-01-29 06:49:01 -070052 assert(qov->root);
Eric Blakea8615642016-01-29 06:49:00 -070053 assert(value);
Michael Rothe4e6aa12011-07-19 14:50:34 -050054 e->value = value;
Michael Rothe1bc2f72011-09-19 19:03:11 -050055 if (qobject_type(e->value) == QTYPE_QLIST) {
56 e->is_list_head = true;
57 }
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. */
Michael Rothe4e6aa12011-07-19 14:50:34 -050062static QObject *qmp_output_pop(QmpOutputVisitor *qov)
63{
64 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
65 QObject *value;
Eric Blakea8615642016-01-29 06:49:00 -070066
67 assert(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -050068 QTAILQ_REMOVE(&qov->stack, e, node);
69 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{
Eric Blake455ba082016-01-29 06:49:01 -070081 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
82 QObject *cur = e ? e->value : NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -050083
Eric Blake455ba082016-01-29 06:49:01 -070084 if (!cur) {
85 /* FIXME we should require the user to reset the visitor, rather
86 * than throwing away the previous root */
87 qobject_decref(qov->root);
88 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:
96 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);
111 qmp_output_push(qov, dict);
112}
113
114static void qmp_output_end_struct(Visitor *v, Error **errp)
115{
116 QmpOutputVisitor *qov = to_qov(v);
117 qmp_output_pop(qov);
118}
119
120static void qmp_output_start_list(Visitor *v, const char *name, Error **errp)
121{
122 QmpOutputVisitor *qov = to_qov(v);
123 QList *list = qlist_new();
124
125 qmp_output_add(qov, name, list);
126 qmp_output_push(qov, list);
127}
128
Eric Blakee65d89b2016-02-17 23:48:23 -0700129static GenericList *qmp_output_next_list(Visitor *v, GenericList **listp,
130 size_t size)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500131{
Michael Rothe1bc2f72011-09-19 19:03:11 -0500132 GenericList *list = *listp;
133 QmpOutputVisitor *qov = to_qov(v);
134 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
135
136 assert(e);
137 if (e->is_list_head) {
138 e->is_list_head = false;
139 return list;
140 }
141
142 return list ? list->next : NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500143}
144
Eric Blake08f95412016-01-29 06:48:59 -0700145static void qmp_output_end_list(Visitor *v)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500146{
147 QmpOutputVisitor *qov = to_qov(v);
148 qmp_output_pop(qov);
149}
150
Eric Blake0b2a0d62016-01-29 06:48:56 -0700151static void qmp_output_type_int64(Visitor *v, const char *name, int64_t *obj,
Eric Blake4c403142016-01-29 06:48:49 -0700152 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500153{
154 QmpOutputVisitor *qov = to_qov(v);
155 qmp_output_add(qov, name, qint_from_int(*obj));
156}
157
Eric Blake0b2a0d62016-01-29 06:48:56 -0700158static void qmp_output_type_uint64(Visitor *v, const char *name, uint64_t *obj,
Eric Blakef755dea2016-01-29 06:48:50 -0700159 Error **errp)
160{
161 /* FIXME: QMP outputs values larger than INT64_MAX as negative */
162 QmpOutputVisitor *qov = to_qov(v);
163 qmp_output_add(qov, name, qint_from_int(*obj));
164}
165
Eric Blake0b2a0d62016-01-29 06:48:56 -0700166static void qmp_output_type_bool(Visitor *v, const char *name, bool *obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500167 Error **errp)
168{
169 QmpOutputVisitor *qov = to_qov(v);
Eric Blakefc48ffc2015-05-15 16:24:59 -0600170 qmp_output_add(qov, name, qbool_from_bool(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500171}
172
Eric Blake0b2a0d62016-01-29 06:48:56 -0700173static void qmp_output_type_str(Visitor *v, const char *name, char **obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500174 Error **errp)
175{
176 QmpOutputVisitor *qov = to_qov(v);
177 if (*obj) {
178 qmp_output_add(qov, name, qstring_from_str(*obj));
179 } else {
180 qmp_output_add(qov, name, qstring_from_str(""));
181 }
182}
183
Eric Blake0b2a0d62016-01-29 06:48:56 -0700184static void qmp_output_type_number(Visitor *v, const char *name, double *obj,
Michael Rothe4e6aa12011-07-19 14:50:34 -0500185 Error **errp)
186{
187 QmpOutputVisitor *qov = to_qov(v);
188 qmp_output_add(qov, name, qfloat_from_double(*obj));
189}
190
Eric Blake0b2a0d62016-01-29 06:48:56 -0700191static void qmp_output_type_any(Visitor *v, const char *name, QObject **obj,
Markus Armbruster28770e02015-09-16 13:06:24 +0200192 Error **errp)
193{
194 QmpOutputVisitor *qov = to_qov(v);
195 qobject_incref(*obj);
196 qmp_output_add_obj(qov, name, *obj);
197}
198
Eric Blakea8615642016-01-29 06:49:00 -0700199/* Finish building, and return the root object. Will not be NULL. */
Michael Rothe4e6aa12011-07-19 14:50:34 -0500200QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
201{
Eric Blake455ba082016-01-29 06:49:01 -0700202 /* FIXME: we should require that a visit occurred, and that it is
203 * complete (no starts without a matching end) */
204 QObject *obj = qov->root;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500205 if (obj) {
206 qobject_incref(obj);
Eric Blakea8615642016-01-29 06:49:00 -0700207 } else {
208 obj = qnull();
Michael Rothe4e6aa12011-07-19 14:50:34 -0500209 }
210 return obj;
211}
212
213Visitor *qmp_output_get_visitor(QmpOutputVisitor *v)
214{
215 return &v->visitor;
216}
217
218void qmp_output_visitor_cleanup(QmpOutputVisitor *v)
219{
220 QStackEntry *e, *tmp;
221
222 QTAILQ_FOREACH_SAFE(e, &v->stack, node, tmp) {
223 QTAILQ_REMOVE(&v->stack, e, node);
Anthony Liguori7267c092011-08-20 22:09:37 -0500224 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500225 }
226
Eric Blake455ba082016-01-29 06:49:01 -0700227 qobject_decref(v->root);
Anthony Liguori7267c092011-08-20 22:09:37 -0500228 g_free(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500229}
230
231QmpOutputVisitor *qmp_output_visitor_new(void)
232{
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
237 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;
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100242 v->visitor.type_enum = output_type_enum;
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;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500249
250 QTAILQ_INIT(&v->stack);
251
252 return v;
253}