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