blob: 89ffd8a7bfcb6c02a22edeca734757f573e4be16 [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"
Markus Armbruster6b673952018-02-01 12:18:35 +010020#include "qapi/qmp/qbool.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010021#include "qapi/qmp/qdict.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010022#include "qapi/qmp/qlist.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010023#include "qapi/qmp/qnull.h"
24#include "qapi/qmp/qnum.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010025#include "qapi/qmp/qstring.h"
Michael Rothe4e6aa12011-07-19 14:50:34 -050026
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010027typedef struct QStackEntry {
Michael Rothe4e6aa12011-07-19 14:50:34 -050028 QObject *value;
Eric Blake1158bb22016-06-09 10:48:34 -060029 void *qapi; /* sanity check that caller uses same pointer */
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020030 QSLIST_ENTRY(QStackEntry) node;
Michael Rothe4e6aa12011-07-19 14:50:34 -050031} QStackEntry;
32
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010033struct QObjectOutputVisitor {
Michael Rothe4e6aa12011-07-19 14:50:34 -050034 Visitor visitor;
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020035 QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */
Eric Blake455ba082016-01-29 06:49:01 -070036 QObject *root; /* Root of the output visit */
Eric Blake3b098d52016-06-09 10:48:43 -060037 QObject **result; /* User's storage location for result */
Michael Rothe4e6aa12011-07-19 14:50:34 -050038};
39
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010040#define qobject_output_add(qov, name, value) \
41 qobject_output_add_obj(qov, name, QOBJECT(value))
42#define qobject_output_push(qov, value, qapi) \
43 qobject_output_push_obj(qov, QOBJECT(value), qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050044
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010045static QObjectOutputVisitor *to_qov(Visitor *v)
Michael Rothe4e6aa12011-07-19 14:50:34 -050046{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010047 return container_of(v, QObjectOutputVisitor, visitor);
Michael Rothe4e6aa12011-07-19 14:50:34 -050048}
49
Eric Blakea8615642016-01-29 06:49:00 -070050/* Push @value onto the stack of current QObjects being built */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010051static void qobject_output_push_obj(QObjectOutputVisitor *qov, QObject *value,
52 void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050053{
Anthony Liguori7267c092011-08-20 22:09:37 -050054 QStackEntry *e = g_malloc0(sizeof(*e));
Michael Rothe4e6aa12011-07-19 14:50:34 -050055
Eric Blake455ba082016-01-29 06:49:01 -070056 assert(qov->root);
Eric Blakea8615642016-01-29 06:49:00 -070057 assert(value);
Michael Rothe4e6aa12011-07-19 14:50:34 -050058 e->value = value;
Eric Blake1158bb22016-06-09 10:48:34 -060059 e->qapi = qapi;
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020060 QSLIST_INSERT_HEAD(&qov->stack, e, node);
Michael Rothe4e6aa12011-07-19 14:50:34 -050061}
62
Eric Blakea8615642016-01-29 06:49:00 -070063/* Pop a value off the stack of QObjects being built, and return it. */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010064static QObject *qobject_output_pop(QObjectOutputVisitor *qov, void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050065{
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020066 QStackEntry *e = QSLIST_FIRST(&qov->stack);
Michael Rothe4e6aa12011-07-19 14:50:34 -050067 QObject *value;
Eric Blakea8615642016-01-29 06:49:00 -070068
69 assert(e);
Eric Blake1158bb22016-06-09 10:48:34 -060070 assert(e->qapi == qapi);
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020071 QSLIST_REMOVE_HEAD(&qov->stack, node);
Michael Rothe4e6aa12011-07-19 14:50:34 -050072 value = e->value;
Eric Blakea8615642016-01-29 06:49:00 -070073 assert(value);
Anthony Liguori7267c092011-08-20 22:09:37 -050074 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -050075 return value;
76}
77
Eric Blakea8615642016-01-29 06:49:00 -070078/* Add @value to the current QObject being built.
79 * If the stack is visiting a dictionary or list, @value is now owned
80 * by that container. Otherwise, @value is now the root. */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010081static void qobject_output_add_obj(QObjectOutputVisitor *qov, const char *name,
82 QObject *value)
Michael Rothe4e6aa12011-07-19 14:50:34 -050083{
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020084 QStackEntry *e = QSLIST_FIRST(&qov->stack);
Eric Blake455ba082016-01-29 06:49:01 -070085 QObject *cur = e ? e->value : NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -050086
Eric Blake455ba082016-01-29 06:49:01 -070087 if (!cur) {
Eric Blake56a6f022016-04-28 15:45:26 -060088 /* Don't allow reuse of visitor on more than one root */
89 assert(!qov->root);
Eric Blake455ba082016-01-29 06:49:01 -070090 qov->root = value;
91 } else {
92 switch (qobject_type(cur)) {
93 case QTYPE_QDICT:
94 assert(name);
Max Reitz7dc847e2018-02-24 16:40:29 +010095 qdict_put_obj(qobject_to(QDict, cur), name, value);
Eric Blake455ba082016-01-29 06:49:01 -070096 break;
97 case QTYPE_QLIST:
Eric Blake56a6f022016-04-28 15:45:26 -060098 assert(!name);
Max Reitz7dc847e2018-02-24 16:40:29 +010099 qlist_append_obj(qobject_to(QList, cur), value);
Eric Blake455ba082016-01-29 06:49:01 -0700100 break;
101 default:
102 g_assert_not_reached();
103 }
Michael Rothe4e6aa12011-07-19 14:50:34 -0500104 }
105}
106
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100107static void qobject_output_start_struct(Visitor *v, const char *name,
108 void **obj, size_t unused, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500109{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100110 QObjectOutputVisitor *qov = to_qov(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500111 QDict *dict = qdict_new();
112
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100113 qobject_output_add(qov, name, dict);
114 qobject_output_push(qov, dict, obj);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500115}
116
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100117static void qobject_output_end_struct(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500118{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100119 QObjectOutputVisitor *qov = to_qov(v);
120 QObject *value = qobject_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600121 assert(qobject_type(value) == QTYPE_QDICT);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500122}
123
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100124static void qobject_output_start_list(Visitor *v, const char *name,
125 GenericList **listp, size_t size,
126 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500127{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100128 QObjectOutputVisitor *qov = to_qov(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500129 QList *list = qlist_new();
130
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100131 qobject_output_add(qov, name, list);
132 qobject_output_push(qov, list, listp);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500133}
134
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100135static GenericList *qobject_output_next_list(Visitor *v, GenericList *tail,
136 size_t size)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500137{
Eric Blaked9f62dd2016-04-28 15:45:31 -0600138 return tail->next;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500139}
140
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100141static void qobject_output_end_list(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500142{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100143 QObjectOutputVisitor *qov = to_qov(v);
144 QObject *value = qobject_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600145 assert(qobject_type(value) == QTYPE_QLIST);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500146}
147
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100148static void qobject_output_type_int64(Visitor *v, const char *name,
149 int64_t *obj, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500150{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100151 QObjectOutputVisitor *qov = to_qov(v);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400152 qobject_output_add(qov, name, qnum_from_int(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500153}
154
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100155static void qobject_output_type_uint64(Visitor *v, const char *name,
156 uint64_t *obj, Error **errp)
Eric Blakef755dea2016-01-29 06:48:50 -0700157{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100158 QObjectOutputVisitor *qov = to_qov(v);
Marc-André Lureau5923f852017-06-07 20:36:03 +0400159 qobject_output_add(qov, name, qnum_from_uint(*obj));
Eric Blakef755dea2016-01-29 06:48:50 -0700160}
161
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100162static void qobject_output_type_bool(Visitor *v, const char *name, bool *obj,
163 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500164{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100165 QObjectOutputVisitor *qov = to_qov(v);
166 qobject_output_add(qov, name, qbool_from_bool(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500167}
168
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100169static void qobject_output_type_str(Visitor *v, const char *name, char **obj,
170 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500171{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100172 QObjectOutputVisitor *qov = to_qov(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500173 if (*obj) {
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100174 qobject_output_add(qov, name, qstring_from_str(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500175 } else {
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100176 qobject_output_add(qov, name, qstring_from_str(""));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500177 }
178}
179
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100180static void qobject_output_type_number(Visitor *v, const char *name,
181 double *obj, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500182{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100183 QObjectOutputVisitor *qov = to_qov(v);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400184 qobject_output_add(qov, name, qnum_from_double(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500185}
186
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100187static void qobject_output_type_any(Visitor *v, const char *name,
188 QObject **obj, Error **errp)
Markus Armbruster28770e02015-09-16 13:06:24 +0200189{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100190 QObjectOutputVisitor *qov = to_qov(v);
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200191
192 qobject_output_add_obj(qov, name, qobject_ref(*obj));
Markus Armbruster28770e02015-09-16 13:06:24 +0200193}
194
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200195static void qobject_output_type_null(Visitor *v, const char *name,
196 QNull **obj, Error **errp)
Eric Blake3bc97fd2016-04-28 15:45:22 -0600197{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100198 QObjectOutputVisitor *qov = to_qov(v);
Markus Armbruster006ca092017-06-26 13:52:24 +0200199 qobject_output_add(qov, name, qnull());
Eric Blake3bc97fd2016-04-28 15:45:22 -0600200}
201
Eric Blake56a6f022016-04-28 15:45:26 -0600202/* Finish building, and return the root object.
203 * The root object is never null. The caller becomes the object's
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200204 * owner, and should use qobject_unref() when done with it. */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100205static void qobject_output_complete(Visitor *v, void *opaque)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500206{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100207 QObjectOutputVisitor *qov = to_qov(v);
Eric Blake3b098d52016-06-09 10:48:43 -0600208
Eric Blake56a6f022016-04-28 15:45:26 -0600209 /* A visit must have occurred, with each start paired with end. */
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200210 assert(qov->root && QSLIST_EMPTY(&qov->stack));
Eric Blake3b098d52016-06-09 10:48:43 -0600211 assert(opaque == qov->result);
Eric Blake56a6f022016-04-28 15:45:26 -0600212
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200213 *qov->result = qobject_ref(qov->root);
Eric Blake3b098d52016-06-09 10:48:43 -0600214 qov->result = NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500215}
216
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100217static void qobject_output_free(Visitor *v)
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600218{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100219 QObjectOutputVisitor *qov = to_qov(v);
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200220 QStackEntry *e;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500221
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200222 while (!QSLIST_EMPTY(&qov->stack)) {
223 e = QSLIST_FIRST(&qov->stack);
224 QSLIST_REMOVE_HEAD(&qov->stack, node);
Anthony Liguori7267c092011-08-20 22:09:37 -0500225 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500226 }
227
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200228 qobject_unref(qov->root);
Eric Blake1830f222016-06-09 10:48:40 -0600229 g_free(qov);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500230}
231
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100232Visitor *qobject_output_visitor_new(QObject **result)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500233{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100234 QObjectOutputVisitor *v;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500235
Anthony Liguori7267c092011-08-20 22:09:37 -0500236 v = g_malloc0(sizeof(*v));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500237
Eric Blake983f52d2016-04-28 15:45:09 -0600238 v->visitor.type = VISITOR_OUTPUT;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100239 v->visitor.start_struct = qobject_output_start_struct;
240 v->visitor.end_struct = qobject_output_end_struct;
241 v->visitor.start_list = qobject_output_start_list;
242 v->visitor.next_list = qobject_output_next_list;
243 v->visitor.end_list = qobject_output_end_list;
244 v->visitor.type_int64 = qobject_output_type_int64;
245 v->visitor.type_uint64 = qobject_output_type_uint64;
246 v->visitor.type_bool = qobject_output_type_bool;
247 v->visitor.type_str = qobject_output_type_str;
248 v->visitor.type_number = qobject_output_type_number;
249 v->visitor.type_any = qobject_output_type_any;
250 v->visitor.type_null = qobject_output_type_null;
251 v->visitor.complete = qobject_output_complete;
252 v->visitor.free = qobject_output_free;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500253
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}