blob: 871127079d72a14f096586580a5888927f5314ee [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
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010022typedef struct QStackEntry {
Michael Rothe4e6aa12011-07-19 14:50:34 -050023 QObject *value;
Eric Blake1158bb22016-06-09 10:48:34 -060024 void *qapi; /* sanity check that caller uses same pointer */
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020025 QSLIST_ENTRY(QStackEntry) node;
Michael Rothe4e6aa12011-07-19 14:50:34 -050026} QStackEntry;
27
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010028struct QObjectOutputVisitor {
Michael Rothe4e6aa12011-07-19 14:50:34 -050029 Visitor visitor;
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020030 QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */
Eric Blake455ba082016-01-29 06:49:01 -070031 QObject *root; /* Root of the output visit */
Eric Blake3b098d52016-06-09 10:48:43 -060032 QObject **result; /* User's storage location for result */
Michael Rothe4e6aa12011-07-19 14:50:34 -050033};
34
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010035#define qobject_output_add(qov, name, value) \
36 qobject_output_add_obj(qov, name, QOBJECT(value))
37#define qobject_output_push(qov, value, qapi) \
38 qobject_output_push_obj(qov, QOBJECT(value), qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050039
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010040static QObjectOutputVisitor *to_qov(Visitor *v)
Michael Rothe4e6aa12011-07-19 14:50:34 -050041{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010042 return container_of(v, QObjectOutputVisitor, visitor);
Michael Rothe4e6aa12011-07-19 14:50:34 -050043}
44
Eric Blakea8615642016-01-29 06:49:00 -070045/* Push @value onto the stack of current QObjects being built */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010046static void qobject_output_push_obj(QObjectOutputVisitor *qov, QObject *value,
47 void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050048{
Anthony Liguori7267c092011-08-20 22:09:37 -050049 QStackEntry *e = g_malloc0(sizeof(*e));
Michael Rothe4e6aa12011-07-19 14:50:34 -050050
Eric Blake455ba082016-01-29 06:49:01 -070051 assert(qov->root);
Eric Blakea8615642016-01-29 06:49:00 -070052 assert(value);
Michael Rothe4e6aa12011-07-19 14:50:34 -050053 e->value = value;
Eric Blake1158bb22016-06-09 10:48:34 -060054 e->qapi = qapi;
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020055 QSLIST_INSERT_HEAD(&qov->stack, e, node);
Michael Rothe4e6aa12011-07-19 14:50:34 -050056}
57
Eric Blakea8615642016-01-29 06:49:00 -070058/* Pop a value off the stack of QObjects being built, and return it. */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010059static QObject *qobject_output_pop(QObjectOutputVisitor *qov, void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050060{
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020061 QStackEntry *e = QSLIST_FIRST(&qov->stack);
Michael Rothe4e6aa12011-07-19 14:50:34 -050062 QObject *value;
Eric Blakea8615642016-01-29 06:49:00 -070063
64 assert(e);
Eric Blake1158bb22016-06-09 10:48:34 -060065 assert(e->qapi == qapi);
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020066 QSLIST_REMOVE_HEAD(&qov->stack, node);
Michael Rothe4e6aa12011-07-19 14:50:34 -050067 value = e->value;
Eric Blakea8615642016-01-29 06:49:00 -070068 assert(value);
Anthony Liguori7267c092011-08-20 22:09:37 -050069 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -050070 return value;
71}
72
Eric Blakea8615642016-01-29 06:49:00 -070073/* Add @value to the current QObject being built.
74 * If the stack is visiting a dictionary or list, @value is now owned
75 * by that container. Otherwise, @value is now the root. */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010076static void qobject_output_add_obj(QObjectOutputVisitor *qov, const char *name,
77 QObject *value)
Michael Rothe4e6aa12011-07-19 14:50:34 -050078{
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020079 QStackEntry *e = QSLIST_FIRST(&qov->stack);
Eric Blake455ba082016-01-29 06:49:01 -070080 QObject *cur = e ? e->value : NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -050081
Eric Blake455ba082016-01-29 06:49:01 -070082 if (!cur) {
Eric Blake56a6f022016-04-28 15:45:26 -060083 /* Don't allow reuse of visitor on more than one root */
84 assert(!qov->root);
Eric Blake455ba082016-01-29 06:49:01 -070085 qov->root = value;
86 } else {
87 switch (qobject_type(cur)) {
88 case QTYPE_QDICT:
89 assert(name);
90 qdict_put_obj(qobject_to_qdict(cur), name, value);
91 break;
92 case QTYPE_QLIST:
Eric Blake56a6f022016-04-28 15:45:26 -060093 assert(!name);
Eric Blake455ba082016-01-29 06:49:01 -070094 qlist_append_obj(qobject_to_qlist(cur), value);
95 break;
96 default:
97 g_assert_not_reached();
98 }
Michael Rothe4e6aa12011-07-19 14:50:34 -050099 }
100}
101
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100102static void qobject_output_start_struct(Visitor *v, const char *name,
103 void **obj, size_t unused, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500104{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100105 QObjectOutputVisitor *qov = to_qov(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500106 QDict *dict = qdict_new();
107
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100108 qobject_output_add(qov, name, dict);
109 qobject_output_push(qov, dict, obj);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500110}
111
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100112static void qobject_output_end_struct(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500113{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100114 QObjectOutputVisitor *qov = to_qov(v);
115 QObject *value = qobject_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600116 assert(qobject_type(value) == QTYPE_QDICT);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500117}
118
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100119static void qobject_output_start_list(Visitor *v, const char *name,
120 GenericList **listp, size_t size,
121 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500122{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100123 QObjectOutputVisitor *qov = to_qov(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500124 QList *list = qlist_new();
125
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100126 qobject_output_add(qov, name, list);
127 qobject_output_push(qov, list, listp);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500128}
129
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100130static GenericList *qobject_output_next_list(Visitor *v, GenericList *tail,
131 size_t size)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500132{
Eric Blaked9f62dd2016-04-28 15:45:31 -0600133 return tail->next;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500134}
135
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100136static void qobject_output_end_list(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500137{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100138 QObjectOutputVisitor *qov = to_qov(v);
139 QObject *value = qobject_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600140 assert(qobject_type(value) == QTYPE_QLIST);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500141}
142
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100143static void qobject_output_type_int64(Visitor *v, const char *name,
144 int64_t *obj, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500145{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100146 QObjectOutputVisitor *qov = to_qov(v);
147 qobject_output_add(qov, name, qint_from_int(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500148}
149
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100150static void qobject_output_type_uint64(Visitor *v, const char *name,
151 uint64_t *obj, Error **errp)
Eric Blakef755dea2016-01-29 06:48:50 -0700152{
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100153 /* FIXME values larger than INT64_MAX become negative */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100154 QObjectOutputVisitor *qov = to_qov(v);
155 qobject_output_add(qov, name, qint_from_int(*obj));
Eric Blakef755dea2016-01-29 06:48:50 -0700156}
157
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100158static void qobject_output_type_bool(Visitor *v, const char *name, bool *obj,
159 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500160{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100161 QObjectOutputVisitor *qov = to_qov(v);
162 qobject_output_add(qov, name, qbool_from_bool(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500163}
164
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100165static void qobject_output_type_str(Visitor *v, const char *name, char **obj,
166 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500167{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100168 QObjectOutputVisitor *qov = to_qov(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500169 if (*obj) {
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100170 qobject_output_add(qov, name, qstring_from_str(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500171 } else {
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100172 qobject_output_add(qov, name, qstring_from_str(""));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500173 }
174}
175
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100176static void qobject_output_type_number(Visitor *v, const char *name,
177 double *obj, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500178{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100179 QObjectOutputVisitor *qov = to_qov(v);
180 qobject_output_add(qov, name, qfloat_from_double(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500181}
182
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100183static void qobject_output_type_any(Visitor *v, const char *name,
184 QObject **obj, Error **errp)
Markus Armbruster28770e02015-09-16 13:06:24 +0200185{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100186 QObjectOutputVisitor *qov = to_qov(v);
Markus Armbruster28770e02015-09-16 13:06:24 +0200187 qobject_incref(*obj);
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100188 qobject_output_add_obj(qov, name, *obj);
Markus Armbruster28770e02015-09-16 13:06:24 +0200189}
190
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100191static void qobject_output_type_null(Visitor *v, const char *name, Error **errp)
Eric Blake3bc97fd2016-04-28 15:45:22 -0600192{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100193 QObjectOutputVisitor *qov = to_qov(v);
194 qobject_output_add_obj(qov, name, qnull());
Eric Blake3bc97fd2016-04-28 15:45:22 -0600195}
196
Eric Blake56a6f022016-04-28 15:45:26 -0600197/* Finish building, and return the root object.
198 * The root object is never null. The caller becomes the object's
199 * owner, and should use qobject_decref() when done with it. */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100200static void qobject_output_complete(Visitor *v, void *opaque)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500201{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100202 QObjectOutputVisitor *qov = to_qov(v);
Eric Blake3b098d52016-06-09 10:48:43 -0600203
Eric Blake56a6f022016-04-28 15:45:26 -0600204 /* A visit must have occurred, with each start paired with end. */
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200205 assert(qov->root && QSLIST_EMPTY(&qov->stack));
Eric Blake3b098d52016-06-09 10:48:43 -0600206 assert(opaque == qov->result);
Eric Blake56a6f022016-04-28 15:45:26 -0600207
208 qobject_incref(qov->root);
Eric Blake3b098d52016-06-09 10:48:43 -0600209 *qov->result = qov->root;
210 qov->result = NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500211}
212
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100213static void qobject_output_free(Visitor *v)
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600214{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100215 QObjectOutputVisitor *qov = to_qov(v);
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200216 QStackEntry *e;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500217
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200218 while (!QSLIST_EMPTY(&qov->stack)) {
219 e = QSLIST_FIRST(&qov->stack);
220 QSLIST_REMOVE_HEAD(&qov->stack, node);
Anthony Liguori7267c092011-08-20 22:09:37 -0500221 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500222 }
223
Eric Blake1830f222016-06-09 10:48:40 -0600224 qobject_decref(qov->root);
225 g_free(qov);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500226}
227
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100228Visitor *qobject_output_visitor_new(QObject **result)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500229{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100230 QObjectOutputVisitor *v;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500231
Anthony Liguori7267c092011-08-20 22:09:37 -0500232 v = g_malloc0(sizeof(*v));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500233
Eric Blake983f52d2016-04-28 15:45:09 -0600234 v->visitor.type = VISITOR_OUTPUT;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100235 v->visitor.start_struct = qobject_output_start_struct;
236 v->visitor.end_struct = qobject_output_end_struct;
237 v->visitor.start_list = qobject_output_start_list;
238 v->visitor.next_list = qobject_output_next_list;
239 v->visitor.end_list = qobject_output_end_list;
240 v->visitor.type_int64 = qobject_output_type_int64;
241 v->visitor.type_uint64 = qobject_output_type_uint64;
242 v->visitor.type_bool = qobject_output_type_bool;
243 v->visitor.type_str = qobject_output_type_str;
244 v->visitor.type_number = qobject_output_type_number;
245 v->visitor.type_any = qobject_output_type_any;
246 v->visitor.type_null = qobject_output_type_null;
247 v->visitor.complete = qobject_output_complete;
248 v->visitor.free = qobject_output_free;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500249
Eric Blake3b098d52016-06-09 10:48:43 -0600250 *result = NULL;
251 v->result = result;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500252
Eric Blake3b098d52016-06-09 10:48:43 -0600253 return &v->visitor;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500254}