]>
Commit | Line | Data |
---|---|---|
84a899de JQ |
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" | |
5272298c | 17 | #include "migration.h" |
84a899de JQ |
18 | #include "migration/global_state.h" |
19 | #include "migration/vmstate.h" | |
84a899de JQ |
20 | #include "trace.h" |
21 | ||
22 | typedef struct { | |
84a899de JQ |
23 | uint32_t size; |
24 | uint8_t runstate[100]; | |
25 | RunState state; | |
26 | bool received; | |
27 | } GlobalState; | |
28 | ||
29 | static GlobalState global_state; | |
30 | ||
31 | int 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 | ||
42 | void global_state_store_running(void) | |
43 | { | |
977c736f | 44 | const char *state = RunState_str(RUN_STATE_RUNNING); |
0a5526a1 | 45 | assert(strlen(state) < sizeof(global_state.runstate)); |
84a899de JQ |
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 | ||
84a899de JQ |
60 | static 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 | ||
5272298c | 67 | if (migrate_get_current()->store_global_state) { |
84a899de JQ |
68 | 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 | ||
82 | static 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 | ||
f7abe0ec | 92 | r = qapi_enum_parse(&RunState_lookup, runstate, -1, &local_err); |
84a899de JQ |
93 | |
94 | if (r == -1) { | |
95 | if (local_err) { | |
96 | error_report_err(local_err); | |
97 | } | |
98 | return -EINVAL; | |
99 | } | |
100 | s->state = r; | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
44b1ff31 | 105 | static int global_state_pre_save(void *opaque) |
84a899de JQ |
106 | { |
107 | GlobalState *s = opaque; | |
108 | ||
109 | trace_migrate_global_state_pre_save((char *)s->runstate); | |
110 | s->size = strlen((char *)s->runstate) + 1; | |
44b1ff31 DDAG |
111 | |
112 | return 0; | |
84a899de JQ |
113 | } |
114 | ||
115 | static const VMStateDescription vmstate_globalstate = { | |
116 | .name = "globalstate", | |
117 | .version_id = 1, | |
118 | .minimum_version_id = 1, | |
119 | .post_load = global_state_post_load, | |
120 | .pre_save = global_state_pre_save, | |
121 | .needed = global_state_needed, | |
122 | .fields = (VMStateField[]) { | |
123 | VMSTATE_UINT32(size, GlobalState), | |
124 | VMSTATE_BUFFER(runstate, GlobalState), | |
125 | VMSTATE_END_OF_LIST() | |
126 | }, | |
127 | }; | |
128 | ||
129 | void register_global_state(void) | |
130 | { | |
131 | /* We would use it independently that we receive it */ | |
132 | strcpy((char *)&global_state.runstate, ""); | |
133 | global_state.received = false; | |
134 | vmstate_register(NULL, 0, &vmstate_globalstate, &global_state); | |
135 | } |