blob: 26d7be5ec99e2d41c53bbad1d676104f2abeac7f [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"
Markus Armbruster6b673952018-02-01 12:18:35 +010019#include "qapi/qmp/qbool.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010020#include "qapi/qmp/qdict.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010021#include "qapi/qmp/qlist.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010022#include "qapi/qmp/qnull.h"
23#include "qapi/qmp/qnum.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010024#include "qapi/qmp/qstring.h"
Michael Rothe4e6aa12011-07-19 14:50:34 -050025
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010026typedef struct QStackEntry {
Michael Rothe4e6aa12011-07-19 14:50:34 -050027 QObject *value;
Eric Blake1158bb22016-06-09 10:48:34 -060028 void *qapi; /* sanity check that caller uses same pointer */
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020029 QSLIST_ENTRY(QStackEntry) node;
Michael Rothe4e6aa12011-07-19 14:50:34 -050030} QStackEntry;
31
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010032struct QObjectOutputVisitor {
Michael Rothe4e6aa12011-07-19 14:50:34 -050033 Visitor visitor;
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020034 QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */
Eric Blake455ba082016-01-29 06:49:01 -070035 QObject *root; /* Root of the output visit */
Eric Blake3b098d52016-06-09 10:48:43 -060036 QObject **result; /* User's storage location for result */
Michael Rothe4e6aa12011-07-19 14:50:34 -050037};
38
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010039#define qobject_output_add(qov, name, value) \
40 qobject_output_add_obj(qov, name, QOBJECT(value))
41#define qobject_output_push(qov, value, qapi) \
42 qobject_output_push_obj(qov, QOBJECT(value), qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050043
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010044static QObjectOutputVisitor *to_qov(Visitor *v)
Michael Rothe4e6aa12011-07-19 14:50:34 -050045{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010046 return container_of(v, QObjectOutputVisitor, visitor);
Michael Rothe4e6aa12011-07-19 14:50:34 -050047}
48
Eric Blakea8615642016-01-29 06:49:00 -070049/* Push @value onto the stack of current QObjects being built */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010050static void qobject_output_push_obj(QObjectOutputVisitor *qov, QObject *value,
51 void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050052{
Anthony Liguori7267c092011-08-20 22:09:37 -050053 QStackEntry *e = g_malloc0(sizeof(*e));
Michael Rothe4e6aa12011-07-19 14:50:34 -050054
Eric Blake455ba082016-01-29 06:49:01 -070055 assert(qov->root);
Eric Blakea8615642016-01-29 06:49:00 -070056 assert(value);
Michael Rothe4e6aa12011-07-19 14:50:34 -050057 e->value = value;
Eric Blake1158bb22016-06-09 10:48:34 -060058 e->qapi = qapi;
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020059 QSLIST_INSERT_HEAD(&qov->stack, e, node);
Michael Rothe4e6aa12011-07-19 14:50:34 -050060}
61
Eric Blakea8615642016-01-29 06:49:00 -070062/* Pop a value off the stack of QObjects being built, and return it. */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010063static QObject *qobject_output_pop(QObjectOutputVisitor *qov, void *qapi)
Michael Rothe4e6aa12011-07-19 14:50:34 -050064{
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020065 QStackEntry *e = QSLIST_FIRST(&qov->stack);
Michael Rothe4e6aa12011-07-19 14:50:34 -050066 QObject *value;
Eric Blakea8615642016-01-29 06:49:00 -070067
68 assert(e);
Eric Blake1158bb22016-06-09 10:48:34 -060069 assert(e->qapi == qapi);
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020070 QSLIST_REMOVE_HEAD(&qov->stack, node);
Michael Rothe4e6aa12011-07-19 14:50:34 -050071 value = e->value;
Eric Blakea8615642016-01-29 06:49:00 -070072 assert(value);
Anthony Liguori7267c092011-08-20 22:09:37 -050073 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -050074 return value;
75}
76
Eric Blakea8615642016-01-29 06:49:00 -070077/* Add @value to the current QObject being built.
78 * If the stack is visiting a dictionary or list, @value is now owned
79 * by that container. Otherwise, @value is now the root. */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010080static void qobject_output_add_obj(QObjectOutputVisitor *qov, const char *name,
81 QObject *value)
Michael Rothe4e6aa12011-07-19 14:50:34 -050082{
Paolo Bonzinifc76ae82016-07-07 17:53:17 +020083 QStackEntry *e = QSLIST_FIRST(&qov->stack);
Eric Blake455ba082016-01-29 06:49:01 -070084 QObject *cur = e ? e->value : NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -050085
Eric Blake455ba082016-01-29 06:49:01 -070086 if (!cur) {
Eric Blake56a6f022016-04-28 15:45:26 -060087 /* Don't allow reuse of visitor on more than one root */
88 assert(!qov->root);
Eric Blake455ba082016-01-29 06:49:01 -070089 qov->root = value;
90 } else {
91 switch (qobject_type(cur)) {
92 case QTYPE_QDICT:
93 assert(name);
Max Reitz7dc847e2018-02-24 16:40:29 +010094 qdict_put_obj(qobject_to(QDict, cur), name, value);
Eric Blake455ba082016-01-29 06:49:01 -070095 break;
96 case QTYPE_QLIST:
Eric Blake56a6f022016-04-28 15:45:26 -060097 assert(!name);
Max Reitz7dc847e2018-02-24 16:40:29 +010098 qlist_append_obj(qobject_to(QList, cur), value);
Eric Blake455ba082016-01-29 06:49:01 -070099 break;
100 default:
101 g_assert_not_reached();
102 }
Michael Rothe4e6aa12011-07-19 14:50:34 -0500103 }
104}
105
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100106static void qobject_output_start_struct(Visitor *v, const char *name,
107 void **obj, size_t unused, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500108{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100109 QObjectOutputVisitor *qov = to_qov(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500110 QDict *dict = qdict_new();
111
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100112 qobject_output_add(qov, name, dict);
113 qobject_output_push(qov, dict, obj);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500114}
115
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100116static void qobject_output_end_struct(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500117{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100118 QObjectOutputVisitor *qov = to_qov(v);
119 QObject *value = qobject_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600120 assert(qobject_type(value) == QTYPE_QDICT);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500121}
122
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100123static void qobject_output_start_list(Visitor *v, const char *name,
124 GenericList **listp, size_t size,
125 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500126{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100127 QObjectOutputVisitor *qov = to_qov(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500128 QList *list = qlist_new();
129
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100130 qobject_output_add(qov, name, list);
131 qobject_output_push(qov, list, listp);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500132}
133
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100134static GenericList *qobject_output_next_list(Visitor *v, GenericList *tail,
135 size_t size)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500136{
Eric Blaked9f62dd2016-04-28 15:45:31 -0600137 return tail->next;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500138}
139
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100140static void qobject_output_end_list(Visitor *v, void **obj)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500141{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100142 QObjectOutputVisitor *qov = to_qov(v);
143 QObject *value = qobject_output_pop(qov, obj);
Eric Blake56a6f022016-04-28 15:45:26 -0600144 assert(qobject_type(value) == QTYPE_QLIST);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500145}
146
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100147static void qobject_output_type_int64(Visitor *v, const char *name,
148 int64_t *obj, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500149{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100150 QObjectOutputVisitor *qov = to_qov(v);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400151 qobject_output_add(qov, name, qnum_from_int(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500152}
153
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100154static void qobject_output_type_uint64(Visitor *v, const char *name,
155 uint64_t *obj, Error **errp)
Eric Blakef755dea2016-01-29 06:48:50 -0700156{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100157 QObjectOutputVisitor *qov = to_qov(v);
Marc-André Lureau5923f852017-06-07 20:36:03 +0400158 qobject_output_add(qov, name, qnum_from_uint(*obj));
Eric Blakef755dea2016-01-29 06:48:50 -0700159}
160
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100161static void qobject_output_type_bool(Visitor *v, const char *name, bool *obj,
162 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500163{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100164 QObjectOutputVisitor *qov = to_qov(v);
165 qobject_output_add(qov, name, qbool_from_bool(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500166}
167
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100168static void qobject_output_type_str(Visitor *v, const char *name, char **obj,
169 Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500170{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100171 QObjectOutputVisitor *qov = to_qov(v);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500172 if (*obj) {
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100173 qobject_output_add(qov, name, qstring_from_str(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500174 } else {
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100175 qobject_output_add(qov, name, qstring_from_str(""));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500176 }
177}
178
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100179static void qobject_output_type_number(Visitor *v, const char *name,
180 double *obj, Error **errp)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500181{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100182 QObjectOutputVisitor *qov = to_qov(v);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400183 qobject_output_add(qov, name, qnum_from_double(*obj));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500184}
185
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100186static void qobject_output_type_any(Visitor *v, const char *name,
187 QObject **obj, Error **errp)
Markus Armbruster28770e02015-09-16 13:06:24 +0200188{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100189 QObjectOutputVisitor *qov = to_qov(v);
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200190
191 qobject_output_add_obj(qov, name, qobject_ref(*obj));
Markus Armbruster28770e02015-09-16 13:06:24 +0200192}
193
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200194static void qobject_output_type_null(Visitor *v, const char *name,
195 QNull **obj, Error **errp)
Eric Blake3bc97fd2016-04-28 15:45:22 -0600196{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100197 QObjectOutputVisitor *qov = to_qov(v);
Markus Armbruster006ca092017-06-26 13:52:24 +0200198 qobject_output_add(qov, name, qnull());
Eric Blake3bc97fd2016-04-28 15:45:22 -0600199}
200
Eric Blake56a6f022016-04-28 15:45:26 -0600201/* Finish building, and return the root object.
202 * The root object is never null. The caller becomes the object's
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200203 * owner, and should use qobject_unref() when done with it. */
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100204static void qobject_output_complete(Visitor *v, void *opaque)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500205{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100206 QObjectOutputVisitor *qov = to_qov(v);
Eric Blake3b098d52016-06-09 10:48:43 -0600207
Eric Blake56a6f022016-04-28 15:45:26 -0600208 /* A visit must have occurred, with each start paired with end. */
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200209 assert(qov->root && QSLIST_EMPTY(&qov->stack));
Eric Blake3b098d52016-06-09 10:48:43 -0600210 assert(opaque == qov->result);
Eric Blake56a6f022016-04-28 15:45:26 -0600211
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200212 *qov->result = qobject_ref(qov->root);
Eric Blake3b098d52016-06-09 10:48:43 -0600213 qov->result = NULL;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500214}
215
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100216static void qobject_output_free(Visitor *v)
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600217{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100218 QObjectOutputVisitor *qov = to_qov(v);
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200219 QStackEntry *e;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500220
Paolo Bonzinifc76ae82016-07-07 17:53:17 +0200221 while (!QSLIST_EMPTY(&qov->stack)) {
222 e = QSLIST_FIRST(&qov->stack);
223 QSLIST_REMOVE_HEAD(&qov->stack, node);
Anthony Liguori7267c092011-08-20 22:09:37 -0500224 g_free(e);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500225 }
226
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200227 qobject_unref(qov->root);
Eric Blake1830f222016-06-09 10:48:40 -0600228 g_free(qov);
Michael Rothe4e6aa12011-07-19 14:50:34 -0500229}
230
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100231Visitor *qobject_output_visitor_new(QObject **result)
Michael Rothe4e6aa12011-07-19 14:50:34 -0500232{
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100233 QObjectOutputVisitor *v;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500234
Anthony Liguori7267c092011-08-20 22:09:37 -0500235 v = g_malloc0(sizeof(*v));
Michael Rothe4e6aa12011-07-19 14:50:34 -0500236
Eric Blake983f52d2016-04-28 15:45:09 -0600237 v->visitor.type = VISITOR_OUTPUT;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100238 v->visitor.start_struct = qobject_output_start_struct;
239 v->visitor.end_struct = qobject_output_end_struct;
240 v->visitor.start_list = qobject_output_start_list;
241 v->visitor.next_list = qobject_output_next_list;
242 v->visitor.end_list = qobject_output_end_list;
243 v->visitor.type_int64 = qobject_output_type_int64;
244 v->visitor.type_uint64 = qobject_output_type_uint64;
245 v->visitor.type_bool = qobject_output_type_bool;
246 v->visitor.type_str = qobject_output_type_str;
247 v->visitor.type_number = qobject_output_type_number;
248 v->visitor.type_any = qobject_output_type_any;
249 v->visitor.type_null = qobject_output_type_null;
250 v->visitor.complete = qobject_output_complete;
251 v->visitor.free = qobject_output_free;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500252
Eric Blake3b098d52016-06-09 10:48:43 -0600253 *result = NULL;
254 v->result = result;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500255
Eric Blake3b098d52016-06-09 10:48:43 -0600256 return &v->visitor;
Michael Rothe4e6aa12011-07-19 14:50:34 -0500257}