xref: /openbmc/qemu/migration/migration.h (revision 0b1183e3)
1 /*
2  * QEMU live migration
3  *
4  * Copyright IBM, Corp. 2008
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #ifndef QEMU_MIGRATION_H
15 #define QEMU_MIGRATION_H
16 
17 #include "qemu-common.h"
18 #include "qemu/thread.h"
19 #include "qapi-types.h"
20 #include "exec/cpu-common.h"
21 #include "qemu/coroutine_int.h"
22 #include "hw/qdev.h"
23 
24 /* State for the incoming migration */
25 struct MigrationIncomingState {
26     QEMUFile *from_src_file;
27 
28     /*
29      * Free at the start of the main state load, set as the main thread finishes
30      * loading state.
31      */
32     QemuEvent main_thread_load_event;
33 
34     size_t         largest_page_size;
35     bool           have_fault_thread;
36     QemuThread     fault_thread;
37     QemuSemaphore  fault_thread_sem;
38 
39     bool           have_listen_thread;
40     QemuThread     listen_thread;
41     QemuSemaphore  listen_thread_sem;
42 
43     /* For the kernel to send us notifications */
44     int       userfault_fd;
45     /* To tell the fault_thread to quit */
46     int       userfault_quit_fd;
47     QEMUFile *to_src_file;
48     QemuMutex rp_mutex;    /* We send replies from multiple threads */
49     void     *postcopy_tmp_page;
50     void     *postcopy_tmp_zero_page;
51 
52     QEMUBH *bh;
53 
54     int state;
55 
56     bool have_colo_incoming_thread;
57     QemuThread colo_incoming_thread;
58     /* The coroutine we should enter (back) after failover */
59     Coroutine *migration_incoming_co;
60     QemuSemaphore colo_incoming_sem;
61 };
62 
63 MigrationIncomingState *migration_incoming_get_current(void);
64 void migration_incoming_state_destroy(void);
65 
66 #define TYPE_MIGRATION "migration"
67 
68 #define MIGRATION_CLASS(klass) \
69     OBJECT_CLASS_CHECK(MigrationClass, (klass), TYPE_MIGRATION)
70 #define MIGRATION_OBJ(obj) \
71     OBJECT_CHECK(MigrationState, (obj), TYPE_MIGRATION)
72 #define MIGRATION_GET_CLASS(obj) \
73     OBJECT_GET_CLASS(MigrationClass, (obj), TYPE_MIGRATION)
74 
75 typedef struct MigrationClass {
76     /*< private >*/
77     DeviceClass parent_class;
78 } MigrationClass;
79 
80 struct MigrationState
81 {
82     /*< private >*/
83     DeviceState parent_obj;
84 
85     /*< public >*/
86     size_t bytes_xfer;
87     size_t xfer_limit;
88     QemuThread thread;
89     QEMUBH *cleanup_bh;
90     QEMUFile *to_dst_file;
91 
92     /* params from 'migrate-set-parameters' */
93     MigrationParameters parameters;
94 
95     int state;
96 
97     /* State related to return path */
98     struct {
99         QEMUFile     *from_dst_file;
100         QemuThread    rp_thread;
101         bool          error;
102     } rp_state;
103 
104     double mbps;
105     int64_t total_time;
106     int64_t downtime;
107     int64_t expected_downtime;
108     bool enabled_capabilities[MIGRATION_CAPABILITY__MAX];
109     int64_t xbzrle_cache_size;
110     int64_t setup_time;
111 
112     /* Flag set once the migration has been asked to enter postcopy */
113     bool start_postcopy;
114     /* Flag set after postcopy has sent the device state */
115     bool postcopy_after_devices;
116 
117     /* Flag set once the migration thread is running (and needs joining) */
118     bool migration_thread_running;
119 
120     /* Flag set once the migration thread called bdrv_inactivate_all */
121     bool block_inactive;
122 
123     /* The semaphore is used to notify COLO thread that failover is finished */
124     QemuSemaphore colo_exit_sem;
125 
126     /* The semaphore is used to notify COLO thread to do checkpoint */
127     QemuSemaphore colo_checkpoint_sem;
128     int64_t colo_checkpoint_time;
129     QEMUTimer *colo_delay_timer;
130 
131     /* The last error that occurred */
132     Error *error;
133     /* Do we have to clean up -b/-i from old migrate parameters */
134     /* This feature is deprecated and will be removed */
135     bool must_remove_block_options;
136 
137     /*
138      * Global switch on whether we need to store the global state
139      * during migration.
140      */
141     bool store_global_state;
142 
143     /* Whether the VM is only allowing for migratable devices */
144     bool only_migratable;
145 
146     /* Whether we send QEMU_VM_CONFIGURATION during migration */
147     bool send_configuration;
148     /* Whether we send section footer during migration */
149     bool send_section_footer;
150 };
151 
152 void migrate_set_state(int *state, int old_state, int new_state);
153 
154 void migration_fd_process_incoming(QEMUFile *f);
155 
156 uint64_t migrate_max_downtime(void);
157 
158 void migrate_fd_error(MigrationState *s, const Error *error);
159 
160 void migrate_fd_connect(MigrationState *s);
161 
162 MigrationState *migrate_init(void);
163 bool migration_is_blocked(Error **errp);
164 /* True if outgoing migration has entered postcopy phase */
165 bool migration_in_postcopy(void);
166 MigrationState *migrate_get_current(void);
167 
168 bool migrate_release_ram(void);
169 bool migrate_postcopy_ram(void);
170 bool migrate_zero_blocks(void);
171 
172 bool migrate_auto_converge(void);
173 
174 int migrate_use_xbzrle(void);
175 int64_t migrate_xbzrle_cache_size(void);
176 bool migrate_colo_enabled(void);
177 
178 bool migrate_use_block(void);
179 bool migrate_use_block_incremental(void);
180 bool migrate_use_return_path(void);
181 
182 bool migrate_use_compression(void);
183 int migrate_compress_level(void);
184 int migrate_compress_threads(void);
185 int migrate_decompress_threads(void);
186 bool migrate_use_events(void);
187 
188 /* Sending on the return path - generic and then for each message type */
189 void migrate_send_rp_shut(MigrationIncomingState *mis,
190                           uint32_t value);
191 void migrate_send_rp_pong(MigrationIncomingState *mis,
192                           uint32_t value);
193 void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char* rbname,
194                               ram_addr_t start, size_t len);
195 
196 #endif
197