]> git.proxmox.com Git - qemu.git/blob - migration-fd.c
Merge commit 'linux-user/linux-user-for-upstream' into staging
[qemu.git] / migration-fd.c
1 /*
2 * QEMU live migration via generic fd
3 *
4 * Copyright Red Hat, Inc. 2009
5 *
6 * Authors:
7 * Chris Lalancette <clalance@redhat.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 "qemu_socket.h"
16 #include "migration.h"
17 #include "monitor.h"
18 #include "qemu-char.h"
19 #include "sysemu.h"
20 #include "buffered_file.h"
21 #include "block.h"
22 #include "qemu_socket.h"
23
24 //#define DEBUG_MIGRATION_FD
25
26 #ifdef DEBUG_MIGRATION_FD
27 #define dprintf(fmt, ...) \
28 do { printf("migration-fd: " fmt, ## __VA_ARGS__); } while (0)
29 #else
30 #define dprintf(fmt, ...) \
31 do { } while (0)
32 #endif
33
34 static int fd_errno(FdMigrationState *s)
35 {
36 return errno;
37 }
38
39 static int fd_write(FdMigrationState *s, const void * buf, size_t size)
40 {
41 return write(s->fd, buf, size);
42 }
43
44 static int fd_close(FdMigrationState *s)
45 {
46 dprintf("fd_close\n");
47 if (s->fd != -1) {
48 close(s->fd);
49 s->fd = -1;
50 }
51 return 0;
52 }
53
54 MigrationState *fd_start_outgoing_migration(Monitor *mon,
55 const char *fdname,
56 int64_t bandwidth_limit,
57 int detach)
58 {
59 FdMigrationState *s;
60
61 s = qemu_mallocz(sizeof(*s));
62
63 s->fd = monitor_get_fd(mon, fdname);
64 if (s->fd == -1) {
65 dprintf("fd_migration: invalid file descriptor identifier\n");
66 goto err_after_alloc;
67 }
68
69 if (fcntl(s->fd, F_SETFL, O_NONBLOCK) == -1) {
70 dprintf("Unable to set nonblocking mode on file descriptor\n");
71 goto err_after_open;
72 }
73
74 s->get_error = fd_errno;
75 s->write = fd_write;
76 s->close = fd_close;
77 s->mig_state.cancel = migrate_fd_cancel;
78 s->mig_state.get_status = migrate_fd_get_status;
79 s->mig_state.release = migrate_fd_release;
80
81 s->state = MIG_STATE_ACTIVE;
82 s->mon_resume = NULL;
83 s->bandwidth_limit = bandwidth_limit;
84
85 if (!detach)
86 migrate_fd_monitor_suspend(s);
87
88 migrate_fd_connect(s);
89 return &s->mig_state;
90
91 err_after_open:
92 close(s->fd);
93 err_after_alloc:
94 qemu_free(s);
95 return NULL;
96 }
97
98 static void fd_accept_incoming_migration(void *opaque)
99 {
100 QEMUFile *f = opaque;
101 int ret;
102
103 ret = qemu_loadvm_state(f);
104 if (ret < 0) {
105 fprintf(stderr, "load of migration failed\n");
106 goto err;
107 }
108 qemu_announce_self();
109 dprintf("successfully loaded vm state\n");
110 /* we've successfully migrated, close the fd */
111 qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
112 if (autostart)
113 vm_start();
114
115 err:
116 qemu_fclose(f);
117 }
118
119 int fd_start_incoming_migration(const char *infd)
120 {
121 int fd;
122 QEMUFile *f;
123
124 dprintf("Attempting to start an incoming migration via fd\n");
125
126 fd = strtol(infd, NULL, 0);
127 f = qemu_fdopen(fd, "rb");
128 if(f == NULL) {
129 dprintf("Unable to apply qemu wrapper to file descriptor\n");
130 return -errno;
131 }
132
133 qemu_set_fd_handler2(fd, NULL, fd_accept_incoming_migration, NULL,
134 (void *)(unsigned long)f);
135
136 return 0;
137 }