aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 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 "qemu_socket.h" |
| 16 | #include "migration.h" |
| 17 | #include "qemu-char.h" |
| 18 | #include "sysemu.h" |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 19 | #include "buffered_file.h" |
| 20 | #include "block.h" |
| 21 | |
| 22 | //#define DEBUG_MIGRATION_TCP |
| 23 | |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 24 | #ifdef DEBUG_MIGRATION_TCP |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 25 | #define DPRINTF(fmt, ...) \ |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 26 | do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0) |
| 27 | #else |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 28 | #define DPRINTF(fmt, ...) \ |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 29 | do { } while (0) |
| 30 | #endif |
| 31 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 32 | static int socket_errno(FdMigrationState *s) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 33 | { |
aliguori | 8ad9fa5 | 2008-11-12 22:29:11 +0000 | [diff] [blame] | 34 | return socket_error(); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 35 | } |
| 36 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 37 | static int socket_write(FdMigrationState *s, const void * buf, size_t size) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 38 | { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 39 | return send(s->fd, buf, size, 0); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 40 | } |
| 41 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 42 | static int tcp_close(FdMigrationState *s) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 43 | { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 44 | DPRINTF("tcp_close\n"); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 45 | if (s->fd != -1) { |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 46 | close(s->fd); |
| 47 | s->fd = -1; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 52 | |
| 53 | static void tcp_wait_for_connect(void *opaque) |
| 54 | { |
| 55 | FdMigrationState *s = opaque; |
| 56 | int val, ret; |
blueswir1 | 4761a48 | 2008-10-25 11:25:48 +0000 | [diff] [blame] | 57 | socklen_t valsize = sizeof(val); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 58 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 59 | DPRINTF("connect completed\n"); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 60 | do { |
malc | 0a656f5 | 2009-05-21 05:26:23 +0400 | [diff] [blame] | 61 | ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 62 | } while (ret == -1 && (s->get_error(s)) == EINTR); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 63 | |
| 64 | if (ret < 0) { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 65 | migrate_fd_error(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 66 | return; |
| 67 | } |
| 68 | |
| 69 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
| 70 | |
| 71 | if (val == 0) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 72 | migrate_fd_connect(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 73 | else { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 74 | DPRINTF("error connecting %d\n", val); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 75 | migrate_fd_error(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 79 | MigrationState *tcp_start_outgoing_migration(Monitor *mon, |
| 80 | const char *host_port, |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 81 | int64_t bandwidth_limit, |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 82 | int detach, |
| 83 | int blk, |
| 84 | int inc) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 85 | { |
| 86 | struct sockaddr_in addr; |
| 87 | FdMigrationState *s; |
| 88 | int ret; |
| 89 | |
| 90 | if (parse_host_port(&addr, host_port) < 0) |
| 91 | return NULL; |
| 92 | |
| 93 | s = qemu_mallocz(sizeof(*s)); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 94 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 95 | s->get_error = socket_errno; |
| 96 | s->write = socket_write; |
| 97 | s->close = tcp_close; |
| 98 | s->mig_state.cancel = migrate_fd_cancel; |
| 99 | s->mig_state.get_status = migrate_fd_get_status; |
| 100 | s->mig_state.release = migrate_fd_release; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 101 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 102 | s->mig_state.blk = blk; |
| 103 | s->mig_state.shared = inc; |
| 104 | |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 105 | s->state = MIG_STATE_ACTIVE; |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 106 | s->mon = NULL; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 107 | s->bandwidth_limit = bandwidth_limit; |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 108 | s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 109 | if (s->fd == -1) { |
| 110 | qemu_free(s); |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 111 | return NULL; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 112 | } |
| 113 | |
aliguori | 17e9097 | 2008-10-24 14:11:41 +0000 | [diff] [blame] | 114 | socket_set_nonblock(s->fd); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 115 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 116 | if (!detach) { |
| 117 | migrate_fd_monitor_suspend(s, mon); |
| 118 | } |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 119 | |
| 120 | do { |
| 121 | ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 122 | if (ret == -1) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 123 | ret = -(s->get_error(s)); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 124 | |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 125 | if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 126 | qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s); |
| 127 | } while (ret == -EINTR); |
| 128 | |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 129 | if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 130 | DPRINTF("connect failed\n"); |
Yoshiaki Tamura | 304e3a7 | 2010-06-10 06:50:10 +0900 | [diff] [blame] | 131 | migrate_fd_error(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 132 | } else if (ret >= 0) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 133 | migrate_fd_connect(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 134 | |
| 135 | return &s->mig_state; |
| 136 | } |
| 137 | |
| 138 | static void tcp_accept_incoming_migration(void *opaque) |
| 139 | { |
| 140 | struct sockaddr_in addr; |
| 141 | socklen_t addrlen = sizeof(addr); |
| 142 | int s = (unsigned long)opaque; |
| 143 | QEMUFile *f; |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 144 | int c; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 145 | |
| 146 | do { |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 147 | c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen); |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 148 | } while (c == -1 && socket_error() == EINTR); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 149 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 150 | DPRINTF("accepted migration\n"); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 151 | |
| 152 | if (c == -1) { |
| 153 | fprintf(stderr, "could not accept migration connection\n"); |
Shahar Havivi | d092c10 | 2010-07-24 13:03:07 +0300 | [diff] [blame] | 154 | goto out2; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 155 | } |
| 156 | |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 157 | f = qemu_fopen_socket(c); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 158 | if (f == NULL) { |
| 159 | fprintf(stderr, "could not qemu_fopen socket\n"); |
| 160 | goto out; |
| 161 | } |
| 162 | |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 163 | process_incoming_migration(f); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 164 | qemu_fclose(f); |
| 165 | out: |
Shahar Havivi | d092c10 | 2010-07-24 13:03:07 +0300 | [diff] [blame] | 166 | close(c); |
| 167 | out2: |
Juan Quintela | cfaf6d3 | 2010-03-10 00:10:35 +0100 | [diff] [blame] | 168 | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); |
| 169 | close(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | int tcp_start_incoming_migration(const char *host_port) |
| 173 | { |
| 174 | struct sockaddr_in addr; |
| 175 | int val; |
| 176 | int s; |
| 177 | |
| 178 | if (parse_host_port(&addr, host_port) < 0) { |
| 179 | fprintf(stderr, "invalid host/port combination: %s\n", host_port); |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 183 | s = qemu_socket(PF_INET, SOCK_STREAM, 0); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 184 | if (s == -1) |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 185 | return -socket_error(); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 186 | |
| 187 | val = 1; |
| 188 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
| 189 | |
| 190 | if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) |
| 191 | goto err; |
| 192 | |
| 193 | if (listen(s, 1) == -1) |
| 194 | goto err; |
| 195 | |
| 196 | qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL, |
| 197 | (void *)(unsigned long)s); |
| 198 | |
| 199 | return 0; |
| 200 | |
| 201 | err: |
| 202 | close(s); |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 203 | return -socket_error(); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 204 | } |