1 /* 2 * Core Definitions for QAPI Visitor Classes 3 * 4 * Copyright (C) 2012-2016 Red Hat, Inc. 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 15 #include "qemu/osdep.h" 16 #include "qapi/error.h" 17 #include "qemu-common.h" 18 #include "qapi/qmp/qobject.h" 19 #include "qapi/qmp/qerror.h" 20 #include "qapi/visitor.h" 21 #include "qapi/visitor-impl.h" 22 #include "trace.h" 23 24 void visit_complete(Visitor *v, void *opaque) 25 { 26 assert(v->type != VISITOR_OUTPUT || v->complete); 27 trace_visit_complete(v, opaque); 28 if (v->complete) { 29 v->complete(v, opaque); 30 } 31 } 32 33 void visit_free(Visitor *v) 34 { 35 trace_visit_free(v); 36 if (v) { 37 v->free(v); 38 } 39 } 40 41 void visit_start_struct(Visitor *v, const char *name, void **obj, 42 size_t size, Error **errp) 43 { 44 Error *err = NULL; 45 46 trace_visit_start_struct(v, name, obj, size); 47 if (obj) { 48 assert(size); 49 assert(!(v->type & VISITOR_OUTPUT) || *obj); 50 } 51 v->start_struct(v, name, obj, size, &err); 52 if (obj && (v->type & VISITOR_INPUT)) { 53 assert(!err != !*obj); 54 } 55 error_propagate(errp, err); 56 } 57 58 void visit_check_struct(Visitor *v, Error **errp) 59 { 60 trace_visit_check_struct(v); 61 if (v->check_struct) { 62 v->check_struct(v, errp); 63 } 64 } 65 66 void visit_end_struct(Visitor *v, void **obj) 67 { 68 trace_visit_end_struct(v, obj); 69 v->end_struct(v, obj); 70 } 71 72 void visit_start_list(Visitor *v, const char *name, GenericList **list, 73 size_t size, Error **errp) 74 { 75 Error *err = NULL; 76 77 assert(!list || size >= sizeof(GenericList)); 78 trace_visit_start_list(v, name, list, size); 79 v->start_list(v, name, list, size, &err); 80 if (list && (v->type & VISITOR_INPUT)) { 81 assert(!(err && *list)); 82 } 83 error_propagate(errp, err); 84 } 85 86 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size) 87 { 88 assert(tail && size >= sizeof(GenericList)); 89 trace_visit_next_list(v, tail, size); 90 return v->next_list(v, tail, size); 91 } 92 93 void visit_check_list(Visitor *v, Error **errp) 94 { 95 trace_visit_check_list(v); 96 if (v->check_list) { 97 v->check_list(v, errp); 98 } 99 } 100 101 void visit_end_list(Visitor *v, void **obj) 102 { 103 trace_visit_end_list(v, obj); 104 v->end_list(v, obj); 105 } 106 107 void visit_start_alternate(Visitor *v, const char *name, 108 GenericAlternate **obj, size_t size, 109 Error **errp) 110 { 111 Error *err = NULL; 112 113 assert(obj && size >= sizeof(GenericAlternate)); 114 assert(!(v->type & VISITOR_OUTPUT) || *obj); 115 trace_visit_start_alternate(v, name, obj, size); 116 if (v->start_alternate) { 117 v->start_alternate(v, name, obj, size, &err); 118 } 119 if (v->type & VISITOR_INPUT) { 120 assert(v->start_alternate && !err != !*obj); 121 } 122 error_propagate(errp, err); 123 } 124 125 void visit_end_alternate(Visitor *v, void **obj) 126 { 127 trace_visit_end_alternate(v, obj); 128 if (v->end_alternate) { 129 v->end_alternate(v, obj); 130 } 131 } 132 133 bool visit_optional(Visitor *v, const char *name, bool *present) 134 { 135 trace_visit_optional(v, name, present); 136 if (v->optional) { 137 v->optional(v, name, present); 138 } 139 return *present; 140 } 141 142 bool visit_is_input(Visitor *v) 143 { 144 return v->type == VISITOR_INPUT; 145 } 146 147 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp) 148 { 149 assert(obj); 150 trace_visit_type_int(v, name, obj); 151 v->type_int64(v, name, obj, errp); 152 } 153 154 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name, 155 uint64_t max, const char *type, Error **errp) 156 { 157 Error *err = NULL; 158 uint64_t value = *obj; 159 160 v->type_uint64(v, name, &value, &err); 161 if (err) { 162 error_propagate(errp, err); 163 } else if (value > max) { 164 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 165 name ? name : "null", type); 166 } else { 167 *obj = value; 168 } 169 } 170 171 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj, 172 Error **errp) 173 { 174 uint64_t value; 175 176 trace_visit_type_uint8(v, name, obj); 177 value = *obj; 178 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp); 179 *obj = value; 180 } 181 182 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj, 183 Error **errp) 184 { 185 uint64_t value; 186 187 trace_visit_type_uint16(v, name, obj); 188 value = *obj; 189 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp); 190 *obj = value; 191 } 192 193 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj, 194 Error **errp) 195 { 196 uint64_t value; 197 198 trace_visit_type_uint32(v, name, obj); 199 value = *obj; 200 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp); 201 *obj = value; 202 } 203 204 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj, 205 Error **errp) 206 { 207 assert(obj); 208 trace_visit_type_uint64(v, name, obj); 209 v->type_uint64(v, name, obj, errp); 210 } 211 212 static void visit_type_intN(Visitor *v, int64_t *obj, const char *name, 213 int64_t min, int64_t max, const char *type, 214 Error **errp) 215 { 216 Error *err = NULL; 217 int64_t value = *obj; 218 219 v->type_int64(v, name, &value, &err); 220 if (err) { 221 error_propagate(errp, err); 222 } else if (value < min || value > max) { 223 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 224 name ? name : "null", type); 225 } else { 226 *obj = value; 227 } 228 } 229 230 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp) 231 { 232 int64_t value; 233 234 trace_visit_type_int8(v, name, obj); 235 value = *obj; 236 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp); 237 *obj = value; 238 } 239 240 void visit_type_int16(Visitor *v, const char *name, int16_t *obj, 241 Error **errp) 242 { 243 int64_t value; 244 245 trace_visit_type_int16(v, name, obj); 246 value = *obj; 247 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp); 248 *obj = value; 249 } 250 251 void visit_type_int32(Visitor *v, const char *name, int32_t *obj, 252 Error **errp) 253 { 254 int64_t value; 255 256 trace_visit_type_int32(v, name, obj); 257 value = *obj; 258 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp); 259 *obj = value; 260 } 261 262 void visit_type_int64(Visitor *v, const char *name, int64_t *obj, 263 Error **errp) 264 { 265 assert(obj); 266 trace_visit_type_int64(v, name, obj); 267 v->type_int64(v, name, obj, errp); 268 } 269 270 void visit_type_size(Visitor *v, const char *name, uint64_t *obj, 271 Error **errp) 272 { 273 assert(obj); 274 trace_visit_type_size(v, name, obj); 275 if (v->type_size) { 276 v->type_size(v, name, obj, errp); 277 } else { 278 v->type_uint64(v, name, obj, errp); 279 } 280 } 281 282 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) 283 { 284 assert(obj); 285 trace_visit_type_bool(v, name, obj); 286 v->type_bool(v, name, obj, errp); 287 } 288 289 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp) 290 { 291 Error *err = NULL; 292 293 assert(obj); 294 /* TODO: Fix callers to not pass NULL when they mean "", so that we 295 * can enable: 296 assert(!(v->type & VISITOR_OUTPUT) || *obj); 297 */ 298 trace_visit_type_str(v, name, obj); 299 v->type_str(v, name, obj, &err); 300 if (v->type & VISITOR_INPUT) { 301 assert(!err != !*obj); 302 } 303 error_propagate(errp, err); 304 } 305 306 void visit_type_number(Visitor *v, const char *name, double *obj, 307 Error **errp) 308 { 309 assert(obj); 310 trace_visit_type_number(v, name, obj); 311 v->type_number(v, name, obj, errp); 312 } 313 314 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp) 315 { 316 Error *err = NULL; 317 318 assert(obj); 319 assert(v->type != VISITOR_OUTPUT || *obj); 320 trace_visit_type_any(v, name, obj); 321 v->type_any(v, name, obj, &err); 322 if (v->type == VISITOR_INPUT) { 323 assert(!err != !*obj); 324 } 325 error_propagate(errp, err); 326 } 327 328 void visit_type_null(Visitor *v, const char *name, QNull **obj, 329 Error **errp) 330 { 331 trace_visit_type_null(v, name, obj); 332 v->type_null(v, name, obj, errp); 333 } 334 335 static void output_type_enum(Visitor *v, const char *name, int *obj, 336 const char *const strings[], Error **errp) 337 { 338 int i = 0; 339 int value = *obj; 340 char *enum_str; 341 342 while (strings[i++] != NULL); 343 if (value < 0 || value >= i - 1) { 344 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null"); 345 return; 346 } 347 348 enum_str = (char *)strings[value]; 349 visit_type_str(v, name, &enum_str, errp); 350 } 351 352 static void input_type_enum(Visitor *v, const char *name, int *obj, 353 const char *const strings[], Error **errp) 354 { 355 Error *local_err = NULL; 356 int64_t value = 0; 357 char *enum_str; 358 359 visit_type_str(v, name, &enum_str, &local_err); 360 if (local_err) { 361 error_propagate(errp, local_err); 362 return; 363 } 364 365 while (strings[value] != NULL) { 366 if (strcmp(strings[value], enum_str) == 0) { 367 break; 368 } 369 value++; 370 } 371 372 if (strings[value] == NULL) { 373 error_setg(errp, QERR_INVALID_PARAMETER, enum_str); 374 g_free(enum_str); 375 return; 376 } 377 378 g_free(enum_str); 379 *obj = value; 380 } 381 382 void visit_type_enum(Visitor *v, const char *name, int *obj, 383 const char *const strings[], Error **errp) 384 { 385 assert(obj && strings); 386 trace_visit_type_enum(v, name, obj); 387 switch (v->type) { 388 case VISITOR_INPUT: 389 input_type_enum(v, name, obj, strings, errp); 390 break; 391 case VISITOR_OUTPUT: 392 output_type_enum(v, name, obj, strings, errp); 393 break; 394 case VISITOR_CLONE: 395 /* nothing further to do, scalar value was already copied by 396 * g_memdup() during visit_start_*() */ 397 break; 398 case VISITOR_DEALLOC: 399 /* nothing to deallocate for a scalar */ 400 break; 401 } 402 } 403