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" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 17 | #include "qemu/sockets.h" |
Paolo Bonzini | caf71f8 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 18 | #include "migration/migration.h" |
Juan Quintela | 557ec5a | 2012-10-03 14:07:31 +0200 | [diff] [blame] | 19 | #include "migration/qemu-file.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 20 | #include "block/block.h" |
Alex Bligh | 6a1751b | 2013-08-21 16:02:47 +0100 | [diff] [blame] | 21 | #include "qemu/main-loop.h" |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 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 | |
Orit Wasserman | 233aa5c | 2012-09-24 13:11:09 +0200 | [diff] [blame] | 33 | static void tcp_wait_for_connect(int fd, void *opaque) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 34 | { |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 35 | MigrationState *s = opaque; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 36 | |
Orit Wasserman | 233aa5c | 2012-09-24 13:11:09 +0200 | [diff] [blame] | 37 | if (fd < 0) { |
| 38 | DPRINTF("migrate connect error\n"); |
Paolo Bonzini | b352365 | 2013-02-22 17:36:47 +0100 | [diff] [blame] | 39 | s->file = NULL; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 40 | migrate_fd_error(s); |
Orit Wasserman | 233aa5c | 2012-09-24 13:11:09 +0200 | [diff] [blame] | 41 | } else { |
| 42 | DPRINTF("migrate connect success\n"); |
Paolo Bonzini | b352365 | 2013-02-22 17:36:47 +0100 | [diff] [blame] | 43 | s->file = qemu_fopen_socket(fd, "wb"); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 44 | migrate_fd_connect(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 48 | void tcp_start_outgoing_migration(MigrationState *s, const char *host_port, Error **errp) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 49 | { |
Paolo Bonzini | f8bbc12 | 2013-02-22 17:36:41 +0100 | [diff] [blame] | 50 | inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, errp); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static void tcp_accept_incoming_migration(void *opaque) |
| 54 | { |
| 55 | struct sockaddr_in addr; |
| 56 | socklen_t addrlen = sizeof(addr); |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 57 | int s = (intptr_t)opaque; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 58 | QEMUFile *f; |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 59 | int c; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 60 | |
| 61 | do { |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 62 | c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen); |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 63 | } while (c == -1 && socket_error() == EINTR); |
Paolo Bonzini | a6ef290 | 2012-08-07 10:49:13 +0200 | [diff] [blame] | 64 | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); |
Paolo Bonzini | 09bac73 | 2012-09-27 13:33:08 +0200 | [diff] [blame] | 65 | closesocket(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 66 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 67 | DPRINTF("accepted migration\n"); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 68 | |
| 69 | if (c == -1) { |
| 70 | fprintf(stderr, "could not accept migration connection\n"); |
Paolo Bonzini | a6ef290 | 2012-08-07 10:49:13 +0200 | [diff] [blame] | 71 | goto out; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Paolo Bonzini | 0cc3f3c | 2013-02-22 17:36:39 +0100 | [diff] [blame] | 74 | f = qemu_fopen_socket(c, "rb"); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 75 | if (f == NULL) { |
| 76 | fprintf(stderr, "could not qemu_fopen socket\n"); |
| 77 | goto out; |
| 78 | } |
| 79 | |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 80 | process_incoming_migration(f); |
Paolo Bonzini | ab52a82 | 2012-08-07 10:50:26 +0200 | [diff] [blame] | 81 | return; |
| 82 | |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 83 | out: |
Paolo Bonzini | 09bac73 | 2012-09-27 13:33:08 +0200 | [diff] [blame] | 84 | closesocket(c); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 87 | void tcp_start_incoming_migration(const char *host_port, Error **errp) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 88 | { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 89 | int s; |
| 90 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 91 | s = inet_listen(host_port, NULL, 256, SOCK_STREAM, 0, errp); |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 92 | if (s < 0) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 93 | return; |
Juan Quintela | ee86c61 | 2011-02-23 20:44:29 +0100 | [diff] [blame] | 94 | } |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 95 | |
| 96 | qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL, |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 97 | (void *)(intptr_t)s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 98 | } |