Statistics
| Branch: | Revision:

root / migration.c @ 1302425d

History | View | Annotate | Download (9.4 kB)

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
#include "qemu-common.h"
15
#include "migration.h"
16
#include "monitor.h"
17
#include "buffered_file.h"
18
#include "sysemu.h"
19
#include "block.h"
20
#include "qemu_socket.h"
21

    
22
//#define DEBUG_MIGRATION
23

    
24
#ifdef DEBUG_MIGRATION
25
#define dprintf(fmt, ...) \
26
    do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
27
#else
28
#define dprintf(fmt, ...) \
29
    do { } while (0)
30
#endif
31

    
32
/* Migration speed throttling */
33
static uint32_t max_throttle = (32 << 20);
34

    
35
static MigrationState *current_migration;
36

    
37
void qemu_start_incoming_migration(const char *uri)
38
{
39
    const char *p;
40

    
41
    if (strstart(uri, "tcp:", &p))
42
        tcp_start_incoming_migration(p);
43
#if !defined(WIN32)
44
    else if (strstart(uri, "exec:", &p))
45
        exec_start_incoming_migration(p);
46
    else if (strstart(uri, "unix:", &p))
47
        unix_start_incoming_migration(p);
48
    else if (strstart(uri, "fd:", &p))
49
        fd_start_incoming_migration(p);
50
#endif
51
    else
52
        fprintf(stderr, "unknown migration protocol: %s\n", uri);
53
}
54

    
55
void do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
56
{
57
    MigrationState *s = NULL;
58
    const char *p;
59
    int detach = qdict_get_int(qdict, "detach");
60
    const char *uri = qdict_get_str(qdict, "uri");
61

    
62
    if (current_migration &&
63
        current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
64
        monitor_printf(mon, "migration already in progress\n");
65
        return;
66
    }
67

    
68
    if (strstart(uri, "tcp:", &p))
69
        s = tcp_start_outgoing_migration(p, max_throttle, detach, 
70
                                         (int)qdict_get_int(qdict, "blk"), 
71
                                         (int)qdict_get_int(qdict, "inc"));
72
#if !defined(WIN32)
73
    else if (strstart(uri, "exec:", &p))
74
        s = exec_start_outgoing_migration(p, max_throttle, detach, 
75
                                          (int)qdict_get_int(qdict, "blk"), 
76
                                          (int)qdict_get_int(qdict, "inc"));
77
    else if (strstart(uri, "unix:", &p))
78
        s = unix_start_outgoing_migration(p, max_throttle, detach, 
79
                                          (int)qdict_get_int(qdict, "blk"), 
80
                                          (int)qdict_get_int(qdict, "inc"));
81
    else if (strstart(uri, "fd:", &p))
82
        s = fd_start_outgoing_migration(mon, p, max_throttle, detach, 
83
                                        (int)qdict_get_int(qdict, "blk"), 
84
                                        (int)qdict_get_int(qdict, "inc"));
85
#endif
86
    else
87
        monitor_printf(mon, "unknown migration protocol: %s\n", uri);
88

    
89
    if (s == NULL)
90
        monitor_printf(mon, "migration failed\n");
91
    else {
92
        if (current_migration)
93
            current_migration->release(current_migration);
94

    
95
        current_migration = s;
96
    }
97
}
98

    
99
void do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
100
{
101
    MigrationState *s = current_migration;
102

    
103
    if (s)
104
        s->cancel(s);
105
}
106

    
107
void do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
108
{
109
    double d;
110
    char *ptr;
111
    FdMigrationState *s;
112
    const char *value = qdict_get_str(qdict, "value");
113

    
114
    d = strtod(value, &ptr);
115
    switch (*ptr) {
116
    case 'G': case 'g':
117
        d *= 1024;
118
    case 'M': case 'm':
119
        d *= 1024;
120
    case 'K': case 'k':
121
        d *= 1024;
122
    default:
123
        break;
124
    }
125

    
126
    max_throttle = (uint32_t)d;
127

    
128
    s = migrate_to_fms(current_migration);
129
    if (s && s->file) {
130
        qemu_file_set_rate_limit(s->file, max_throttle);
131
    }
132
}
133

    
134
/* amount of nanoseconds we are willing to wait for migration to be down.
135
 * the choice of nanoseconds is because it is the maximum resolution that
136
 * get_clock() can achieve. It is an internal measure. All user-visible
137
 * units must be in seconds */
138
static uint64_t max_downtime = 30000000;
139

    
140
uint64_t migrate_max_downtime(void)
141
{
142
    return max_downtime;
143
}
144

    
145
void do_migrate_set_downtime(Monitor *mon, const QDict *qdict)
146
{
147
    char *ptr;
148
    double d;
149
    const char *value = qdict_get_str(qdict, "value");
150

    
151
    d = strtod(value, &ptr);
152
    if (!strcmp(ptr,"ms")) {
153
        d *= 1000000;
154
    } else if (!strcmp(ptr,"us")) {
155
        d *= 1000;
156
    } else if (!strcmp(ptr,"ns")) {
157
    } else {
158
        /* all else considered to be seconds */
159
        d *= 1000000000;
160
    }
161

    
162
    max_downtime = (uint64_t)d;
163
}
164

    
165
void do_info_migrate(Monitor *mon)
166
{
167
    MigrationState *s = current_migration;
168

    
169
    if (s) {
170
        monitor_printf(mon, "Migration status: ");
171
        switch (s->get_status(s)) {
172
        case MIG_STATE_ACTIVE:
173
            monitor_printf(mon, "active\n");
174
            monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n", ram_bytes_transferred() >> 10);
175
            monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n", ram_bytes_remaining() >> 10);
176
            monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n", ram_bytes_total() >> 10);
177
            break;
178
        case MIG_STATE_COMPLETED:
179
            monitor_printf(mon, "completed\n");
180
            break;
181
        case MIG_STATE_ERROR:
182
            monitor_printf(mon, "failed\n");
183
            break;
184
        case MIG_STATE_CANCELLED:
185
            monitor_printf(mon, "cancelled\n");
186
            break;
187
        }
188
    }
189
}
190

    
191
/* shared migration helpers */
192

    
193
void migrate_fd_monitor_suspend(FdMigrationState *s)
194
{
195
    s->mon_resume = cur_mon;
196
    if (monitor_suspend(cur_mon) == 0)
197
        dprintf("suspending monitor\n");
198
    else
199
        monitor_printf(cur_mon, "terminal does not allow synchronous "
200
                       "migration, continuing detached\n");
201
}
202

    
203
void migrate_fd_error(FdMigrationState *s)
204
{
205
    dprintf("setting error state\n");
206
    s->state = MIG_STATE_ERROR;
207
    migrate_fd_cleanup(s);
208
}
209

    
210
void migrate_fd_cleanup(FdMigrationState *s)
211
{
212
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
213

    
214
    if (s->file) {
215
        dprintf("closing file\n");
216
        qemu_fclose(s->file);
217
        s->file = NULL;
218
    }
219

    
220
    if (s->fd != -1)
221
        close(s->fd);
222

    
223
    /* Don't resume monitor until we've flushed all of the buffers */
224
    if (s->mon_resume)
225
        monitor_resume(s->mon_resume);
226

    
227
    s->fd = -1;
228
}
229

    
230
void migrate_fd_put_notify(void *opaque)
231
{
232
    FdMigrationState *s = opaque;
233

    
234
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
235
    qemu_file_put_notify(s->file);
236
}
237

    
238
ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
239
{
240
    FdMigrationState *s = opaque;
241
    ssize_t ret;
242

    
243
    do {
244
        ret = s->write(s, data, size);
245
    } while (ret == -1 && ((s->get_error(s)) == EINTR));
246

    
247
    if (ret == -1)
248
        ret = -(s->get_error(s));
249

    
250
    if (ret == -EAGAIN)
251
        qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
252

    
253
    return ret;
254
}
255

    
256
void migrate_fd_connect(FdMigrationState *s)
257
{
258
    int ret;
259

    
260
    s->file = qemu_fopen_ops_buffered(s,
261
                                      s->bandwidth_limit,
262
                                      migrate_fd_put_buffer,
263
                                      migrate_fd_put_ready,
264
                                      migrate_fd_wait_for_unfreeze,
265
                                      migrate_fd_close);
266

    
267
    dprintf("beginning savevm\n");
268
    ret = qemu_savevm_state_begin(s->file, s->mig_state.blk, 
269
                                  s->mig_state.shared);
270
    if (ret < 0) {
271
        dprintf("failed, %d\n", ret);
272
        migrate_fd_error(s);
273
        return;
274
    }
275
    
276
    migrate_fd_put_ready(s);
277
}
278

    
279
void migrate_fd_put_ready(void *opaque)
280
{
281
    FdMigrationState *s = opaque;
282

    
283
    if (s->state != MIG_STATE_ACTIVE) {
284
        dprintf("put_ready returning because of non-active state\n");
285
        return;
286
    }
287

    
288
    dprintf("iterate\n");
289
    if (qemu_savevm_state_iterate(s->file) == 1) {
290
        int state;
291
        int old_vm_running = vm_running;
292

    
293
        dprintf("done iterating\n");
294
        vm_stop(0);
295

    
296
        qemu_aio_flush();
297
        bdrv_flush_all();
298
        if ((qemu_savevm_state_complete(s->file)) < 0) {
299
            if (old_vm_running) {
300
                vm_start();
301
            }
302
            state = MIG_STATE_ERROR;
303
        } else {
304
            state = MIG_STATE_COMPLETED;
305
        }
306
        migrate_fd_cleanup(s);
307
        s->state = state;
308
    }
309
}
310

    
311
int migrate_fd_get_status(MigrationState *mig_state)
312
{
313
    FdMigrationState *s = migrate_to_fms(mig_state);
314
    return s->state;
315
}
316

    
317
void migrate_fd_cancel(MigrationState *mig_state)
318
{
319
    FdMigrationState *s = migrate_to_fms(mig_state);
320

    
321
    if (s->state != MIG_STATE_ACTIVE)
322
        return;
323

    
324
    dprintf("cancelling migration\n");
325

    
326
    s->state = MIG_STATE_CANCELLED;
327

    
328
    migrate_fd_cleanup(s);
329
}
330

    
331
void migrate_fd_release(MigrationState *mig_state)
332
{
333
    FdMigrationState *s = migrate_to_fms(mig_state);
334

    
335
    dprintf("releasing state\n");
336
   
337
    if (s->state == MIG_STATE_ACTIVE) {
338
        s->state = MIG_STATE_CANCELLED;
339
        migrate_fd_cleanup(s);
340
    }
341
    free(s);
342
}
343

    
344
void migrate_fd_wait_for_unfreeze(void *opaque)
345
{
346
    FdMigrationState *s = opaque;
347
    int ret;
348

    
349
    dprintf("wait for unfreeze\n");
350
    if (s->state != MIG_STATE_ACTIVE)
351
        return;
352

    
353
    do {
354
        fd_set wfds;
355

    
356
        FD_ZERO(&wfds);
357
        FD_SET(s->fd, &wfds);
358

    
359
        ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
360
    } while (ret == -1 && (s->get_error(s)) == EINTR);
361
}
362

    
363
int migrate_fd_close(void *opaque)
364
{
365
    FdMigrationState *s = opaque;
366

    
367
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
368
    return s->close(s);
369
}