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 | |
Orit Wasserman | 233aa5c | 2012-09-24 13:11:09 +0200 | [diff] [blame] | 56 | static void tcp_wait_for_connect(int fd, void *opaque) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 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 | |
Orit Wasserman | 233aa5c | 2012-09-24 13:11:09 +0200 | [diff] [blame] | 60 | if (fd < 0) { |
| 61 | DPRINTF("migrate connect error\n"); |
| 62 | s->fd = -1; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 63 | migrate_fd_error(s); |
Orit Wasserman | 233aa5c | 2012-09-24 13:11:09 +0200 | [diff] [blame] | 64 | } else { |
| 65 | DPRINTF("migrate connect success\n"); |
| 66 | s->fd = fd; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 67 | migrate_fd_connect(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 71 | int tcp_start_outgoing_migration(MigrationState *s, const char *host_port, |
| 72 | Error **errp) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 73 | { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 74 | s->get_error = socket_errno; |
| 75 | s->write = socket_write; |
| 76 | s->close = tcp_close; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 77 | |
Orit Wasserman | 233aa5c | 2012-09-24 13:11:09 +0200 | [diff] [blame] | 78 | s->fd = inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, |
| 79 | errp); |
Luiz Capitulino | 540c79f | 2012-08-06 16:26:47 -0300 | [diff] [blame] | 80 | if (error_is_set(errp)) { |
Yoshiaki Tamura | 304e3a7 | 2010-06-10 06:50:10 +0900 | [diff] [blame] | 81 | migrate_fd_error(s); |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 82 | return -1; |
Luiz Capitulino | 540c79f | 2012-08-06 16:26:47 -0300 | [diff] [blame] | 83 | } |
| 84 | |
Juan Quintela | 07af445 | 2010-05-11 22:27:45 +0200 | [diff] [blame] | 85 | return 0; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void tcp_accept_incoming_migration(void *opaque) |
| 89 | { |
| 90 | struct sockaddr_in addr; |
| 91 | socklen_t addrlen = sizeof(addr); |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 92 | int s = (intptr_t)opaque; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 93 | QEMUFile *f; |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 94 | int c; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 95 | |
| 96 | do { |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 97 | c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen); |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 98 | } while (c == -1 && socket_error() == EINTR); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 99 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 100 | DPRINTF("accepted migration\n"); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 101 | |
| 102 | if (c == -1) { |
| 103 | fprintf(stderr, "could not accept migration connection\n"); |
Shahar Havivi | d092c10 | 2010-07-24 13:03:07 +0300 | [diff] [blame] | 104 | goto out2; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 105 | } |
| 106 | |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 107 | f = qemu_fopen_socket(c); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 108 | if (f == NULL) { |
| 109 | fprintf(stderr, "could not qemu_fopen socket\n"); |
| 110 | goto out; |
| 111 | } |
| 112 | |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 113 | process_incoming_migration(f); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 114 | qemu_fclose(f); |
| 115 | out: |
Shahar Havivi | d092c10 | 2010-07-24 13:03:07 +0300 | [diff] [blame] | 116 | close(c); |
| 117 | out2: |
Juan Quintela | cfaf6d3 | 2010-03-10 00:10:35 +0100 | [diff] [blame] | 118 | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); |
| 119 | close(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 122 | int tcp_start_incoming_migration(const char *host_port, Error **errp) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 123 | { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 124 | int s; |
| 125 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 126 | s = inet_listen(host_port, NULL, 256, SOCK_STREAM, 0, errp); |
Juan Quintela | ee86c61 | 2011-02-23 20:44:29 +0100 | [diff] [blame] | 127 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 128 | if (s < 0) { |
| 129 | return -1; |
Juan Quintela | ee86c61 | 2011-02-23 20:44:29 +0100 | [diff] [blame] | 130 | } |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 131 | |
| 132 | qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL, |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 133 | (void *)(intptr_t)s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 134 | |
| 135 | return 0; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 136 | } |