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