blob: 2c8c447239673df1d715c598def3124a3d1a61ba [file] [log] [blame]
Juan Quintela84a899d2017-04-24 18:53:30 +02001/*
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"
Peter Xu52722982017-06-27 12:10:14 +080017#include "migration.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020018#include "migration/global_state.h"
19#include "migration/vmstate.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020020#include "trace.h"
21
22typedef struct {
Juan Quintela84a899d2017-04-24 18:53:30 +020023 uint32_t size;
24 uint8_t runstate[100];
25 RunState state;
26 bool received;
27} GlobalState;
28
29static GlobalState global_state;
30
31int global_state_store(void)
32{
33 if (!runstate_store((char *)global_state.runstate,
34 sizeof(global_state.runstate))) {
35 error_report("runstate name too big: %s", global_state.runstate);
36 trace_migrate_state_too_big();
37 return -EINVAL;
38 }
39 return 0;
40}
41
42void global_state_store_running(void)
43{
Markus Armbruster977c7362017-08-24 10:46:08 +020044 const char *state = RunState_str(RUN_STATE_RUNNING);
Marc-André Lureau0a5526a2019-01-03 09:56:37 +010045 assert(strlen(state) < sizeof(global_state.runstate));
Juan Quintela84a899d2017-04-24 18:53:30 +020046 strncpy((char *)global_state.runstate,
47 state, sizeof(global_state.runstate));
48}
49
50bool global_state_received(void)
51{
52 return global_state.received;
53}
54
55RunState global_state_get_runstate(void)
56{
57 return global_state.state;
58}
59
Juan Quintela84a899d2017-04-24 18:53:30 +020060static bool global_state_needed(void *opaque)
61{
62 GlobalState *s = opaque;
63 char *runstate = (char *)s->runstate;
64
65 /* If it is not optional, it is mandatory */
66
Peter Xu52722982017-06-27 12:10:14 +080067 if (migrate_get_current()->store_global_state) {
Juan Quintela84a899d2017-04-24 18:53:30 +020068 return true;
69 }
70
71 /* If state is running or paused, it is not needed */
72
73 if (strcmp(runstate, "running") == 0 ||
74 strcmp(runstate, "paused") == 0) {
75 return false;
76 }
77
78 /* for any other state it is needed */
79 return true;
80}
81
82static int global_state_post_load(void *opaque, int version_id)
83{
84 GlobalState *s = opaque;
85 Error *local_err = NULL;
86 int r;
87 char *runstate = (char *)s->runstate;
88
89 s->received = true;
90 trace_migrate_global_state_post_load(runstate);
91
Philippe Mathieu-Daudéa346af92019-01-03 09:56:38 +010092 if (strnlen((char *)s->runstate,
93 sizeof(s->runstate)) == sizeof(s->runstate)) {
94 /*
95 * This condition should never happen during migration, because
96 * all runstate names are shorter than 100 bytes (the size of
97 * s->runstate). However, a malicious stream could overflow
98 * the qapi_enum_parse() call, so we force the last character
99 * to a NUL byte.
100 */
101 s->runstate[sizeof(s->runstate) - 1] = '\0';
102 }
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200103 r = qapi_enum_parse(&RunState_lookup, runstate, -1, &local_err);
Juan Quintela84a899d2017-04-24 18:53:30 +0200104
105 if (r == -1) {
106 if (local_err) {
107 error_report_err(local_err);
108 }
109 return -EINVAL;
110 }
111 s->state = r;
112
113 return 0;
114}
115
Dr. David Alan Gilbert44b1ff32017-09-25 12:29:12 +0100116static int global_state_pre_save(void *opaque)
Juan Quintela84a899d2017-04-24 18:53:30 +0200117{
118 GlobalState *s = opaque;
119
120 trace_migrate_global_state_pre_save((char *)s->runstate);
Philippe Mathieu-Daudéa346af92019-01-03 09:56:38 +0100121 s->size = strnlen((char *)s->runstate, sizeof(s->runstate)) + 1;
122 assert(s->size <= sizeof(s->runstate));
Dr. David Alan Gilbert44b1ff32017-09-25 12:29:12 +0100123
124 return 0;
Juan Quintela84a899d2017-04-24 18:53:30 +0200125}
126
127static const VMStateDescription vmstate_globalstate = {
128 .name = "globalstate",
129 .version_id = 1,
130 .minimum_version_id = 1,
131 .post_load = global_state_post_load,
132 .pre_save = global_state_pre_save,
133 .needed = global_state_needed,
134 .fields = (VMStateField[]) {
135 VMSTATE_UINT32(size, GlobalState),
136 VMSTATE_BUFFER(runstate, GlobalState),
137 VMSTATE_END_OF_LIST()
138 },
139};
140
141void register_global_state(void)
142{
143 /* We would use it independently that we receive it */
144 strcpy((char *)&global_state.runstate, "");
145 global_state.received = false;
146 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
147}