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