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