1 /* 2 * QEMU dbus-vmstate 3 * 4 * Copyright (C) 2019 Red Hat Inc 5 * 6 * Authors: 7 * Marc-André Lureau <marcandre.lureau@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "qemu/units.h" 15 #include "qemu/dbus.h" 16 #include "qemu/error-report.h" 17 #include "qapi/error.h" 18 #include "qom/object_interfaces.h" 19 #include "qapi/qmp/qerror.h" 20 #include "migration/vmstate.h" 21 #include "trace.h" 22 #include "qom/object.h" 23 24 25 #define TYPE_DBUS_VMSTATE "dbus-vmstate" 26 OBJECT_DECLARE_TYPE(DBusVMState, DBusVMStateClass, 27 dbus_vmstate, DBUS_VMSTATE) 28 29 struct DBusVMStateClass { 30 ObjectClass parent_class; 31 }; 32 33 struct DBusVMState { 34 Object parent; 35 36 GDBusConnection *bus; 37 char *dbus_addr; 38 char *id_list; 39 40 uint32_t data_size; 41 uint8_t *data; 42 }; 43 44 static const GDBusPropertyInfo vmstate_property_info[] = { 45 { -1, (char *) "Id", (char *) "s", 46 G_DBUS_PROPERTY_INFO_FLAGS_READABLE, NULL }, 47 }; 48 49 static const GDBusPropertyInfo * const vmstate_property_info_pointers[] = { 50 &vmstate_property_info[0], 51 NULL 52 }; 53 54 static const GDBusInterfaceInfo vmstate1_interface_info = { 55 -1, 56 (char *) "org.qemu.VMState1", 57 (GDBusMethodInfo **) NULL, 58 (GDBusSignalInfo **) NULL, 59 (GDBusPropertyInfo **) &vmstate_property_info_pointers, 60 NULL, 61 }; 62 63 #define DBUS_VMSTATE_SIZE_LIMIT (1 * MiB) 64 65 static GHashTable * 66 get_id_list_set(DBusVMState *self) 67 { 68 g_auto(GStrv) ids = NULL; 69 g_autoptr(GHashTable) set = NULL; 70 int i; 71 72 if (!self->id_list) { 73 return NULL; 74 } 75 76 ids = g_strsplit(self->id_list, ",", -1); 77 set = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); 78 for (i = 0; ids[i]; i++) { 79 g_hash_table_add(set, ids[i]); 80 ids[i] = NULL; 81 } 82 83 return g_steal_pointer(&set); 84 } 85 86 static GHashTable * 87 dbus_get_proxies(DBusVMState *self, GError **err) 88 { 89 g_autoptr(GHashTable) proxies = NULL; 90 g_autoptr(GHashTable) ids = NULL; 91 g_auto(GStrv) names = NULL; 92 Error *error = NULL; 93 size_t i; 94 95 ids = get_id_list_set(self); 96 proxies = g_hash_table_new_full(g_str_hash, g_str_equal, 97 g_free, g_object_unref); 98 99 names = qemu_dbus_get_queued_owners(self->bus, "org.qemu.VMState1", &error); 100 if (!names) { 101 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", 102 error_get_pretty(error)); 103 error_free(error); 104 return NULL; 105 } 106 107 for (i = 0; names[i]; i++) { 108 g_autoptr(GDBusProxy) proxy = NULL; 109 g_autoptr(GVariant) result = NULL; 110 g_autofree char *id = NULL; 111 size_t size; 112 113 proxy = g_dbus_proxy_new_sync(self->bus, G_DBUS_PROXY_FLAGS_NONE, 114 (GDBusInterfaceInfo *) &vmstate1_interface_info, 115 names[i], 116 "/org/qemu/VMState1", 117 "org.qemu.VMState1", 118 NULL, err); 119 if (!proxy) { 120 return NULL; 121 } 122 123 result = g_dbus_proxy_get_cached_property(proxy, "Id"); 124 if (!result) { 125 g_set_error_literal(err, G_IO_ERROR, G_IO_ERROR_FAILED, 126 "VMState Id property is missing."); 127 return NULL; 128 } 129 130 id = g_variant_dup_string(result, &size); 131 if (ids && !g_hash_table_remove(ids, id)) { 132 g_clear_pointer(&id, g_free); 133 g_clear_object(&proxy); 134 continue; 135 } 136 if (size == 0 || size >= 256) { 137 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED, 138 "VMState Id '%s' is invalid.", id); 139 return NULL; 140 } 141 142 if (!g_hash_table_insert(proxies, id, proxy)) { 143 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED, 144 "Duplicated VMState Id '%s'", id); 145 return NULL; 146 } 147 id = NULL; 148 proxy = NULL; 149 150 g_clear_pointer(&result, g_variant_unref); 151 } 152 153 if (ids) { 154 g_autofree char **left = NULL; 155 156 left = (char **)g_hash_table_get_keys_as_array(ids, NULL); 157 if (*left) { 158 g_autofree char *leftids = g_strjoinv(",", left); 159 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED, 160 "Required VMState Id are missing: %s", leftids); 161 return NULL; 162 } 163 } 164 165 return g_steal_pointer(&proxies); 166 } 167 168 static int 169 dbus_load_state_proxy(GDBusProxy *proxy, const uint8_t *data, size_t size) 170 { 171 g_autoptr(GError) err = NULL; 172 g_autoptr(GVariant) result = NULL; 173 g_autoptr(GVariant) value = NULL; 174 175 value = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, 176 data, size, sizeof(char)); 177 result = g_dbus_proxy_call_sync(proxy, "Load", 178 g_variant_new("(@ay)", 179 g_steal_pointer(&value)), 180 G_DBUS_CALL_FLAGS_NO_AUTO_START, 181 -1, NULL, &err); 182 if (!result) { 183 error_report("%s: Failed to Load: %s", __func__, err->message); 184 return -1; 185 } 186 187 return 0; 188 } 189 190 static int dbus_vmstate_post_load(void *opaque, int version_id) 191 { 192 DBusVMState *self = DBUS_VMSTATE(opaque); 193 g_autoptr(GInputStream) m = NULL; 194 g_autoptr(GDataInputStream) s = NULL; 195 g_autoptr(GError) err = NULL; 196 g_autoptr(GHashTable) proxies = NULL; 197 uint32_t nelem; 198 199 trace_dbus_vmstate_post_load(version_id); 200 201 proxies = dbus_get_proxies(self, &err); 202 if (!proxies) { 203 error_report("%s: Failed to get proxies: %s", __func__, err->message); 204 return -1; 205 } 206 207 m = g_memory_input_stream_new_from_data(self->data, self->data_size, NULL); 208 s = g_data_input_stream_new(m); 209 g_data_input_stream_set_byte_order(s, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN); 210 211 nelem = g_data_input_stream_read_uint32(s, NULL, &err); 212 if (err) { 213 goto error; 214 } 215 216 while (nelem > 0) { 217 GDBusProxy *proxy = NULL; 218 uint32_t len; 219 gsize bytes_read, avail; 220 char id[256]; 221 222 len = g_data_input_stream_read_uint32(s, NULL, &err); 223 if (err) { 224 goto error; 225 } 226 if (len >= 256) { 227 error_report("%s: Invalid DBus vmstate proxy name %u", 228 __func__, len); 229 return -1; 230 } 231 if (!g_input_stream_read_all(G_INPUT_STREAM(s), id, len, 232 &bytes_read, NULL, &err)) { 233 goto error; 234 } 235 g_return_val_if_fail(bytes_read == len, -1); 236 id[len] = 0; 237 238 trace_dbus_vmstate_loading(id); 239 240 proxy = g_hash_table_lookup(proxies, id); 241 if (!proxy) { 242 error_report("%s: Failed to find proxy Id '%s'", __func__, id); 243 return -1; 244 } 245 246 len = g_data_input_stream_read_uint32(s, NULL, &err); 247 avail = g_buffered_input_stream_get_available( 248 G_BUFFERED_INPUT_STREAM(s)); 249 250 if (len > DBUS_VMSTATE_SIZE_LIMIT || len > avail) { 251 error_report("%s: Invalid vmstate size: %u", __func__, len); 252 return -1; 253 } 254 255 if (dbus_load_state_proxy(proxy, 256 g_buffered_input_stream_peek_buffer(G_BUFFERED_INPUT_STREAM(s), 257 NULL), 258 len) < 0) { 259 error_report("%s: Failed to restore Id '%s'", __func__, id); 260 return -1; 261 } 262 263 if (!g_seekable_seek(G_SEEKABLE(s), len, G_SEEK_CUR, NULL, &err)) { 264 goto error; 265 } 266 267 nelem -= 1; 268 } 269 270 return 0; 271 272 error: 273 error_report("%s: Failed to read from stream: %s", __func__, err->message); 274 return -1; 275 } 276 277 static void 278 dbus_save_state_proxy(gpointer key, 279 gpointer value, 280 gpointer user_data) 281 { 282 GDataOutputStream *s = user_data; 283 const char *id = key; 284 GDBusProxy *proxy = value; 285 g_autoptr(GVariant) result = NULL; 286 g_autoptr(GVariant) child = NULL; 287 g_autoptr(GError) err = NULL; 288 const uint8_t *data; 289 gsize size; 290 291 trace_dbus_vmstate_saving(id); 292 293 result = g_dbus_proxy_call_sync(proxy, "Save", 294 NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START, 295 -1, NULL, &err); 296 if (!result) { 297 error_report("%s: Failed to Save: %s", __func__, err->message); 298 return; 299 } 300 301 child = g_variant_get_child_value(result, 0); 302 data = g_variant_get_fixed_array(child, &size, sizeof(char)); 303 if (!data) { 304 error_report("%s: Failed to Save: not a byte array", __func__); 305 return; 306 } 307 if (size > DBUS_VMSTATE_SIZE_LIMIT) { 308 error_report("%s: Too large vmstate data to save: %zu", 309 __func__, (size_t)size); 310 return; 311 } 312 313 if (!g_data_output_stream_put_uint32(s, strlen(id), NULL, &err) || 314 !g_data_output_stream_put_string(s, id, NULL, &err) || 315 !g_data_output_stream_put_uint32(s, size, NULL, &err) || 316 !g_output_stream_write_all(G_OUTPUT_STREAM(s), 317 data, size, NULL, NULL, &err)) { 318 error_report("%s: Failed to write to stream: %s", 319 __func__, err->message); 320 } 321 } 322 323 static int dbus_vmstate_pre_save(void *opaque) 324 { 325 DBusVMState *self = DBUS_VMSTATE(opaque); 326 g_autoptr(GOutputStream) m = NULL; 327 g_autoptr(GDataOutputStream) s = NULL; 328 g_autoptr(GHashTable) proxies = NULL; 329 g_autoptr(GError) err = NULL; 330 331 trace_dbus_vmstate_pre_save(); 332 333 proxies = dbus_get_proxies(self, &err); 334 if (!proxies) { 335 error_report("%s: Failed to get proxies: %s", __func__, err->message); 336 return -1; 337 } 338 339 m = g_memory_output_stream_new_resizable(); 340 s = g_data_output_stream_new(m); 341 g_data_output_stream_set_byte_order(s, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN); 342 343 if (!g_data_output_stream_put_uint32(s, g_hash_table_size(proxies), 344 NULL, &err)) { 345 error_report("%s: Failed to write to stream: %s", 346 __func__, err->message); 347 return -1; 348 } 349 350 g_hash_table_foreach(proxies, dbus_save_state_proxy, s); 351 352 if (g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m)) 353 > UINT32_MAX) { 354 error_report("%s: DBus vmstate buffer is too large", __func__); 355 return -1; 356 } 357 358 if (!g_output_stream_close(G_OUTPUT_STREAM(m), NULL, &err)) { 359 error_report("%s: Failed to close stream: %s", __func__, err->message); 360 return -1; 361 } 362 363 g_free(self->data); 364 self->data_size = 365 g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m)); 366 self->data = 367 g_memory_output_stream_steal_data(G_MEMORY_OUTPUT_STREAM(m)); 368 369 return 0; 370 } 371 372 static const VMStateDescription dbus_vmstate = { 373 .name = TYPE_DBUS_VMSTATE, 374 .version_id = 0, 375 .pre_save = dbus_vmstate_pre_save, 376 .post_load = dbus_vmstate_post_load, 377 .fields = (VMStateField[]) { 378 VMSTATE_UINT32(data_size, DBusVMState), 379 VMSTATE_VBUFFER_ALLOC_UINT32(data, DBusVMState, 0, 0, data_size), 380 VMSTATE_END_OF_LIST() 381 } 382 }; 383 384 static void 385 dbus_vmstate_complete(UserCreatable *uc, Error **errp) 386 { 387 DBusVMState *self = DBUS_VMSTATE(uc); 388 g_autoptr(GError) err = NULL; 389 390 if (!object_resolve_path_type("", TYPE_DBUS_VMSTATE, NULL)) { 391 error_setg(errp, "There is already an instance of %s", 392 TYPE_DBUS_VMSTATE); 393 return; 394 } 395 396 if (!self->dbus_addr) { 397 error_setg(errp, QERR_MISSING_PARAMETER, "addr"); 398 return; 399 } 400 401 self->bus = g_dbus_connection_new_for_address_sync(self->dbus_addr, 402 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | 403 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION, 404 NULL, NULL, &err); 405 if (err) { 406 error_setg(errp, "failed to connect to DBus: '%s'", err->message); 407 return; 408 } 409 410 if (vmstate_register(VMSTATE_IF(self), VMSTATE_INSTANCE_ID_ANY, 411 &dbus_vmstate, self) < 0) { 412 error_setg(errp, "Failed to register vmstate"); 413 } 414 } 415 416 static void 417 dbus_vmstate_finalize(Object *o) 418 { 419 DBusVMState *self = DBUS_VMSTATE(o); 420 421 vmstate_unregister(VMSTATE_IF(self), &dbus_vmstate, self); 422 423 g_clear_object(&self->bus); 424 g_free(self->dbus_addr); 425 g_free(self->id_list); 426 g_free(self->data); 427 } 428 429 static char * 430 get_dbus_addr(Object *o, Error **errp) 431 { 432 DBusVMState *self = DBUS_VMSTATE(o); 433 434 return g_strdup(self->dbus_addr); 435 } 436 437 static void 438 set_dbus_addr(Object *o, const char *str, Error **errp) 439 { 440 DBusVMState *self = DBUS_VMSTATE(o); 441 442 g_free(self->dbus_addr); 443 self->dbus_addr = g_strdup(str); 444 } 445 446 static char * 447 get_id_list(Object *o, Error **errp) 448 { 449 DBusVMState *self = DBUS_VMSTATE(o); 450 451 return g_strdup(self->id_list); 452 } 453 454 static void 455 set_id_list(Object *o, const char *str, Error **errp) 456 { 457 DBusVMState *self = DBUS_VMSTATE(o); 458 459 g_free(self->id_list); 460 self->id_list = g_strdup(str); 461 } 462 463 static char * 464 dbus_vmstate_get_id(VMStateIf *vmif) 465 { 466 return g_strdup(TYPE_DBUS_VMSTATE); 467 } 468 469 static void 470 dbus_vmstate_class_init(ObjectClass *oc, void *data) 471 { 472 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); 473 VMStateIfClass *vc = VMSTATE_IF_CLASS(oc); 474 475 ucc->complete = dbus_vmstate_complete; 476 vc->get_id = dbus_vmstate_get_id; 477 478 object_class_property_add_str(oc, "addr", 479 get_dbus_addr, set_dbus_addr); 480 object_class_property_add_str(oc, "id-list", 481 get_id_list, set_id_list); 482 } 483 484 static const TypeInfo dbus_vmstate_info = { 485 .name = TYPE_DBUS_VMSTATE, 486 .parent = TYPE_OBJECT, 487 .instance_size = sizeof(DBusVMState), 488 .instance_finalize = dbus_vmstate_finalize, 489 .class_size = sizeof(DBusVMStateClass), 490 .class_init = dbus_vmstate_class_init, 491 .interfaces = (InterfaceInfo[]) { 492 { TYPE_USER_CREATABLE }, 493 { TYPE_VMSTATE_IF }, 494 { } 495 } 496 }; 497 498 static void 499 register_types(void) 500 { 501 type_register_static(&dbus_vmstate_info); 502 } 503 504 type_init(register_types); 505