1 /* 2 * Global State configuration 3 * 4 * Copyright (c) 2014-2017 Red Hat Inc 5 * 6 * Authors: 7 * Juan Quintela <quintela@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/cutils.h" 15 #include "qemu/error-report.h" 16 #include "qapi/error.h" 17 #include "qapi/util.h" 18 #include "migration/global_state.h" 19 #include "migration/vmstate.h" 20 #include "trace.h" 21 22 typedef struct { 23 bool optional; 24 uint32_t size; 25 uint8_t runstate[100]; 26 RunState state; 27 bool received; 28 } GlobalState; 29 30 static GlobalState global_state; 31 32 int global_state_store(void) 33 { 34 if (!runstate_store((char *)global_state.runstate, 35 sizeof(global_state.runstate))) { 36 error_report("runstate name too big: %s", global_state.runstate); 37 trace_migrate_state_too_big(); 38 return -EINVAL; 39 } 40 return 0; 41 } 42 43 void global_state_store_running(void) 44 { 45 const char *state = RunState_lookup[RUN_STATE_RUNNING]; 46 strncpy((char *)global_state.runstate, 47 state, sizeof(global_state.runstate)); 48 } 49 50 bool global_state_received(void) 51 { 52 return global_state.received; 53 } 54 55 RunState global_state_get_runstate(void) 56 { 57 return global_state.state; 58 } 59 60 void global_state_set_optional(void) 61 { 62 global_state.optional = true; 63 } 64 65 static bool global_state_needed(void *opaque) 66 { 67 GlobalState *s = opaque; 68 char *runstate = (char *)s->runstate; 69 70 /* If it is not optional, it is mandatory */ 71 72 if (s->optional == false) { 73 return true; 74 } 75 76 /* If state is running or paused, it is not needed */ 77 78 if (strcmp(runstate, "running") == 0 || 79 strcmp(runstate, "paused") == 0) { 80 return false; 81 } 82 83 /* for any other state it is needed */ 84 return true; 85 } 86 87 static int global_state_post_load(void *opaque, int version_id) 88 { 89 GlobalState *s = opaque; 90 Error *local_err = NULL; 91 int r; 92 char *runstate = (char *)s->runstate; 93 94 s->received = true; 95 trace_migrate_global_state_post_load(runstate); 96 97 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX, 98 -1, &local_err); 99 100 if (r == -1) { 101 if (local_err) { 102 error_report_err(local_err); 103 } 104 return -EINVAL; 105 } 106 s->state = r; 107 108 return 0; 109 } 110 111 static void global_state_pre_save(void *opaque) 112 { 113 GlobalState *s = opaque; 114 115 trace_migrate_global_state_pre_save((char *)s->runstate); 116 s->size = strlen((char *)s->runstate) + 1; 117 } 118 119 static const VMStateDescription vmstate_globalstate = { 120 .name = "globalstate", 121 .version_id = 1, 122 .minimum_version_id = 1, 123 .post_load = global_state_post_load, 124 .pre_save = global_state_pre_save, 125 .needed = global_state_needed, 126 .fields = (VMStateField[]) { 127 VMSTATE_UINT32(size, GlobalState), 128 VMSTATE_BUFFER(runstate, GlobalState), 129 VMSTATE_END_OF_LIST() 130 }, 131 }; 132 133 void register_global_state(void) 134 { 135 /* We would use it independently that we receive it */ 136 strcpy((char *)&global_state.runstate, ""); 137 global_state.received = false; 138 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state); 139 } 140