Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Core Definitions for QAPI/QMP Command Registry |
| 3 | * |
Eric Blake | 08f9541 | 2016-01-29 06:48:59 -0700 | [diff] [blame] | 4 | * Copyright (C) 2012-2016 Red Hat, Inc. |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 5 | * 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 Maydell | cbf2115 | 2016-01-29 17:49:57 +0000 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 16 | #include "qapi/qmp-output-visitor.h" |
| 17 | #include "qapi/visitor-impl.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 18 | #include "qemu/queue.h" |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 19 | #include "qemu-common.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 20 | #include "qapi/qmp/types.h" |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 21 | |
| 22 | typedef struct QStackEntry |
| 23 | { |
| 24 | QObject *value; |
Michael Roth | e1bc2f7 | 2011-09-19 19:03:11 -0500 | [diff] [blame] | 25 | bool is_list_head; |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 26 | QTAILQ_ENTRY(QStackEntry) node; |
| 27 | } QStackEntry; |
| 28 | |
| 29 | typedef QTAILQ_HEAD(QStack, QStackEntry) QStack; |
| 30 | |
| 31 | struct QmpOutputVisitor |
| 32 | { |
| 33 | Visitor visitor; |
Eric Blake | 455ba08 | 2016-01-29 06:49:01 -0700 | [diff] [blame] | 34 | QStack stack; /* Stack of containers that haven't yet been finished */ |
| 35 | QObject *root; /* Root of the output visit */ |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 36 | }; |
| 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 | |
| 42 | static QmpOutputVisitor *to_qov(Visitor *v) |
| 43 | { |
| 44 | return container_of(v, QmpOutputVisitor, visitor); |
| 45 | } |
| 46 | |
Eric Blake | a861564 | 2016-01-29 06:49:00 -0700 | [diff] [blame] | 47 | /* Push @value onto the stack of current QObjects being built */ |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 48 | static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value) |
| 49 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 50 | QStackEntry *e = g_malloc0(sizeof(*e)); |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 51 | |
Eric Blake | 455ba08 | 2016-01-29 06:49:01 -0700 | [diff] [blame] | 52 | assert(qov->root); |
Eric Blake | a861564 | 2016-01-29 06:49:00 -0700 | [diff] [blame] | 53 | assert(value); |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 54 | e->value = value; |
Michael Roth | e1bc2f7 | 2011-09-19 19:03:11 -0500 | [diff] [blame] | 55 | if (qobject_type(e->value) == QTYPE_QLIST) { |
| 56 | e->is_list_head = true; |
| 57 | } |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 58 | QTAILQ_INSERT_HEAD(&qov->stack, e, node); |
| 59 | } |
| 60 | |
Eric Blake | a861564 | 2016-01-29 06:49:00 -0700 | [diff] [blame] | 61 | /* Pop a value off the stack of QObjects being built, and return it. */ |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 62 | static QObject *qmp_output_pop(QmpOutputVisitor *qov) |
| 63 | { |
| 64 | QStackEntry *e = QTAILQ_FIRST(&qov->stack); |
| 65 | QObject *value; |
Eric Blake | a861564 | 2016-01-29 06:49:00 -0700 | [diff] [blame] | 66 | |
| 67 | assert(e); |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 68 | QTAILQ_REMOVE(&qov->stack, e, node); |
| 69 | value = e->value; |
Eric Blake | a861564 | 2016-01-29 06:49:00 -0700 | [diff] [blame] | 70 | assert(value); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 71 | g_free(e); |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 72 | return value; |
| 73 | } |
| 74 | |
Eric Blake | a861564 | 2016-01-29 06:49:00 -0700 | [diff] [blame] | 75 | /* 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 Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 78 | static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name, |
| 79 | QObject *value) |
| 80 | { |
Eric Blake | 455ba08 | 2016-01-29 06:49:01 -0700 | [diff] [blame] | 81 | QStackEntry *e = QTAILQ_FIRST(&qov->stack); |
| 82 | QObject *cur = e ? e->value : NULL; |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 83 | |
Eric Blake | 455ba08 | 2016-01-29 06:49:01 -0700 | [diff] [blame] | 84 | if (!cur) { |
Eric Blake | 56a6f02 | 2016-04-28 15:45:26 -0600 | [diff] [blame^] | 85 | /* Don't allow reuse of visitor on more than one root */ |
| 86 | assert(!qov->root); |
Eric Blake | 455ba08 | 2016-01-29 06:49:01 -0700 | [diff] [blame] | 87 | 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 Blake | 56a6f02 | 2016-04-28 15:45:26 -0600 | [diff] [blame^] | 95 | assert(!name); |
Eric Blake | 455ba08 | 2016-01-29 06:49:01 -0700 | [diff] [blame] | 96 | qlist_append_obj(qobject_to_qlist(cur), value); |
| 97 | break; |
| 98 | default: |
| 99 | g_assert_not_reached(); |
| 100 | } |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 104 | static void qmp_output_start_struct(Visitor *v, const char *name, void **obj, |
Eric Blake | 337283d | 2016-01-29 06:48:57 -0700 | [diff] [blame] | 105 | size_t unused, Error **errp) |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 106 | { |
| 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 | |
| 114 | static void qmp_output_end_struct(Visitor *v, Error **errp) |
| 115 | { |
| 116 | QmpOutputVisitor *qov = to_qov(v); |
Eric Blake | 56a6f02 | 2016-04-28 15:45:26 -0600 | [diff] [blame^] | 117 | QObject *value = qmp_output_pop(qov); |
| 118 | assert(qobject_type(value) == QTYPE_QDICT); |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void qmp_output_start_list(Visitor *v, const char *name, Error **errp) |
| 122 | { |
| 123 | QmpOutputVisitor *qov = to_qov(v); |
| 124 | QList *list = qlist_new(); |
| 125 | |
| 126 | qmp_output_add(qov, name, list); |
| 127 | qmp_output_push(qov, list); |
| 128 | } |
| 129 | |
Eric Blake | e65d89b | 2016-02-17 23:48:23 -0700 | [diff] [blame] | 130 | static GenericList *qmp_output_next_list(Visitor *v, GenericList **listp, |
| 131 | size_t size) |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 132 | { |
Michael Roth | e1bc2f7 | 2011-09-19 19:03:11 -0500 | [diff] [blame] | 133 | GenericList *list = *listp; |
| 134 | QmpOutputVisitor *qov = to_qov(v); |
| 135 | QStackEntry *e = QTAILQ_FIRST(&qov->stack); |
| 136 | |
| 137 | assert(e); |
| 138 | if (e->is_list_head) { |
| 139 | e->is_list_head = false; |
| 140 | return list; |
| 141 | } |
| 142 | |
| 143 | return list ? list->next : NULL; |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Eric Blake | 08f9541 | 2016-01-29 06:48:59 -0700 | [diff] [blame] | 146 | static void qmp_output_end_list(Visitor *v) |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 147 | { |
| 148 | QmpOutputVisitor *qov = to_qov(v); |
Eric Blake | 56a6f02 | 2016-04-28 15:45:26 -0600 | [diff] [blame^] | 149 | QObject *value = qmp_output_pop(qov); |
| 150 | assert(qobject_type(value) == QTYPE_QLIST); |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 153 | static void qmp_output_type_int64(Visitor *v, const char *name, int64_t *obj, |
Eric Blake | 4c40314 | 2016-01-29 06:48:49 -0700 | [diff] [blame] | 154 | Error **errp) |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 155 | { |
| 156 | QmpOutputVisitor *qov = to_qov(v); |
| 157 | qmp_output_add(qov, name, qint_from_int(*obj)); |
| 158 | } |
| 159 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 160 | static void qmp_output_type_uint64(Visitor *v, const char *name, uint64_t *obj, |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 161 | Error **errp) |
| 162 | { |
| 163 | /* FIXME: QMP outputs values larger than INT64_MAX as negative */ |
| 164 | QmpOutputVisitor *qov = to_qov(v); |
| 165 | qmp_output_add(qov, name, qint_from_int(*obj)); |
| 166 | } |
| 167 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 168 | static void qmp_output_type_bool(Visitor *v, const char *name, bool *obj, |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 169 | Error **errp) |
| 170 | { |
| 171 | QmpOutputVisitor *qov = to_qov(v); |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 172 | qmp_output_add(qov, name, qbool_from_bool(*obj)); |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 175 | static void qmp_output_type_str(Visitor *v, const char *name, char **obj, |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 176 | Error **errp) |
| 177 | { |
| 178 | QmpOutputVisitor *qov = to_qov(v); |
| 179 | if (*obj) { |
| 180 | qmp_output_add(qov, name, qstring_from_str(*obj)); |
| 181 | } else { |
| 182 | qmp_output_add(qov, name, qstring_from_str("")); |
| 183 | } |
| 184 | } |
| 185 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 186 | static void qmp_output_type_number(Visitor *v, const char *name, double *obj, |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 187 | Error **errp) |
| 188 | { |
| 189 | QmpOutputVisitor *qov = to_qov(v); |
| 190 | qmp_output_add(qov, name, qfloat_from_double(*obj)); |
| 191 | } |
| 192 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 193 | static void qmp_output_type_any(Visitor *v, const char *name, QObject **obj, |
Markus Armbruster | 28770e0 | 2015-09-16 13:06:24 +0200 | [diff] [blame] | 194 | Error **errp) |
| 195 | { |
| 196 | QmpOutputVisitor *qov = to_qov(v); |
| 197 | qobject_incref(*obj); |
| 198 | qmp_output_add_obj(qov, name, *obj); |
| 199 | } |
| 200 | |
Eric Blake | 3bc97fd | 2016-04-28 15:45:22 -0600 | [diff] [blame] | 201 | static void qmp_output_type_null(Visitor *v, const char *name, Error **errp) |
| 202 | { |
Eric Blake | 3df016f | 2016-04-28 15:45:23 -0600 | [diff] [blame] | 203 | QmpOutputVisitor *qov = to_qov(v); |
| 204 | qmp_output_add_obj(qov, name, qnull()); |
Eric Blake | 3bc97fd | 2016-04-28 15:45:22 -0600 | [diff] [blame] | 205 | } |
| 206 | |
Eric Blake | 56a6f02 | 2016-04-28 15:45:26 -0600 | [diff] [blame^] | 207 | /* Finish building, and return the root object. |
| 208 | * The root object is never null. The caller becomes the object's |
| 209 | * owner, and should use qobject_decref() when done with it. */ |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 210 | QObject *qmp_output_get_qobject(QmpOutputVisitor *qov) |
| 211 | { |
Eric Blake | 56a6f02 | 2016-04-28 15:45:26 -0600 | [diff] [blame^] | 212 | /* A visit must have occurred, with each start paired with end. */ |
| 213 | assert(qov->root && QTAILQ_EMPTY(&qov->stack)); |
| 214 | |
| 215 | qobject_incref(qov->root); |
| 216 | return qov->root; |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | Visitor *qmp_output_get_visitor(QmpOutputVisitor *v) |
| 220 | { |
| 221 | return &v->visitor; |
| 222 | } |
| 223 | |
| 224 | void qmp_output_visitor_cleanup(QmpOutputVisitor *v) |
| 225 | { |
| 226 | QStackEntry *e, *tmp; |
| 227 | |
| 228 | QTAILQ_FOREACH_SAFE(e, &v->stack, node, tmp) { |
| 229 | QTAILQ_REMOVE(&v->stack, e, node); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 230 | g_free(e); |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 231 | } |
| 232 | |
Eric Blake | 455ba08 | 2016-01-29 06:49:01 -0700 | [diff] [blame] | 233 | qobject_decref(v->root); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 234 | g_free(v); |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | QmpOutputVisitor *qmp_output_visitor_new(void) |
| 238 | { |
| 239 | QmpOutputVisitor *v; |
| 240 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 241 | v = g_malloc0(sizeof(*v)); |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 242 | |
Eric Blake | 983f52d | 2016-04-28 15:45:09 -0600 | [diff] [blame] | 243 | v->visitor.type = VISITOR_OUTPUT; |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 244 | v->visitor.start_struct = qmp_output_start_struct; |
| 245 | v->visitor.end_struct = qmp_output_end_struct; |
| 246 | v->visitor.start_list = qmp_output_start_list; |
| 247 | v->visitor.next_list = qmp_output_next_list; |
| 248 | v->visitor.end_list = qmp_output_end_list; |
Eric Blake | 4c40314 | 2016-01-29 06:48:49 -0700 | [diff] [blame] | 249 | v->visitor.type_int64 = qmp_output_type_int64; |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 250 | v->visitor.type_uint64 = qmp_output_type_uint64; |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 251 | v->visitor.type_bool = qmp_output_type_bool; |
| 252 | v->visitor.type_str = qmp_output_type_str; |
| 253 | v->visitor.type_number = qmp_output_type_number; |
Markus Armbruster | 28770e0 | 2015-09-16 13:06:24 +0200 | [diff] [blame] | 254 | v->visitor.type_any = qmp_output_type_any; |
Eric Blake | 3bc97fd | 2016-04-28 15:45:22 -0600 | [diff] [blame] | 255 | v->visitor.type_null = qmp_output_type_null; |
Michael Roth | e4e6aa1 | 2011-07-19 14:50:34 -0500 | [diff] [blame] | 256 | |
| 257 | QTAILQ_INIT(&v->stack); |
| 258 | |
| 259 | return v; |
| 260 | } |