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