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 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 82 | int tcp_start_outgoing_migration(MigrationState *s, const char *host_port, |
| 83 | Error **errp) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 84 | { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 85 | s->get_error = socket_errno; |
| 86 | s->write = socket_write; |
| 87 | s->close = tcp_close; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 88 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 89 | s->fd = inet_connect(host_port, false, errp); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 90 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 91 | if (!error_is_set(errp)) { |
| 92 | migrate_fd_connect(s); |
| 93 | } else if (error_is_type(*errp, QERR_SOCKET_CONNECT_IN_PROGRESS)) { |
| 94 | DPRINTF("connect in progress\n"); |
| 95 | qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s); |
| 96 | } else if (error_is_type(*errp, QERR_SOCKET_CREATE_FAILED)) { |
| 97 | DPRINTF("connect failed\n"); |
| 98 | return -1; |
| 99 | } else if (error_is_type(*errp, QERR_SOCKET_CONNECT_FAILED)) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 100 | DPRINTF("connect failed\n"); |
Yoshiaki Tamura | 304e3a7 | 2010-06-10 06:50:10 +0900 | [diff] [blame] | 101 | migrate_fd_error(s); |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 102 | return -1; |
| 103 | } else { |
| 104 | DPRINTF("unknown error\n"); |
| 105 | return -1; |
Juan Quintela | 8414ff3 | 2011-02-23 19:56:52 +0100 | [diff] [blame] | 106 | } |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 107 | |
Juan Quintela | 07af445 | 2010-05-11 22:27:45 +0200 | [diff] [blame] | 108 | return 0; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static void tcp_accept_incoming_migration(void *opaque) |
| 112 | { |
| 113 | struct sockaddr_in addr; |
| 114 | socklen_t addrlen = sizeof(addr); |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 115 | int s = (intptr_t)opaque; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 116 | QEMUFile *f; |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 117 | int c; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 118 | |
| 119 | do { |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 120 | c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen); |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 121 | } while (c == -1 && socket_error() == EINTR); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 122 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 123 | DPRINTF("accepted migration\n"); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 124 | |
| 125 | if (c == -1) { |
| 126 | fprintf(stderr, "could not accept migration connection\n"); |
Shahar Havivi | d092c10 | 2010-07-24 13:03:07 +0300 | [diff] [blame] | 127 | goto out2; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 128 | } |
| 129 | |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 130 | f = qemu_fopen_socket(c); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 131 | if (f == NULL) { |
| 132 | fprintf(stderr, "could not qemu_fopen socket\n"); |
| 133 | goto out; |
| 134 | } |
| 135 | |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 136 | process_incoming_migration(f); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 137 | qemu_fclose(f); |
| 138 | out: |
Shahar Havivi | d092c10 | 2010-07-24 13:03:07 +0300 | [diff] [blame] | 139 | close(c); |
| 140 | out2: |
Juan Quintela | cfaf6d3 | 2010-03-10 00:10:35 +0100 | [diff] [blame] | 141 | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); |
| 142 | close(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 145 | int tcp_start_incoming_migration(const char *host_port, Error **errp) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 146 | { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 147 | int s; |
| 148 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 149 | s = inet_listen(host_port, NULL, 256, SOCK_STREAM, 0, errp); |
Juan Quintela | ee86c61 | 2011-02-23 20:44:29 +0100 | [diff] [blame] | 150 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 151 | if (s < 0) { |
| 152 | return -1; |
Juan Quintela | ee86c61 | 2011-02-23 20:44:29 +0100 | [diff] [blame] | 153 | } |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 154 | |
| 155 | qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL, |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 156 | (void *)(intptr_t)s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 157 | |
| 158 | return 0; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 159 | } |