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 | |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 32 | static int socket_errno(MigrationState *s) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 33 | { |
aliguori | 8ad9fa5 | 2008-11-12 22:29:11 +0000 | [diff] [blame] | 34 | return socket_error(); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 37 | static int socket_write(MigrationState *s, const void * buf, size_t size) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 38 | { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 39 | return send(s->fd, buf, size, 0); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 42 | static int tcp_close(MigrationState *s) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 43 | { |
Eduardo Habkost | 61a5872 | 2011-11-10 10:41:47 -0200 | [diff] [blame] | 44 | int r = 0; |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 45 | DPRINTF("tcp_close\n"); |
Paolo Bonzini | 6c36013 | 2012-09-27 13:30:15 +0200 | [diff] [blame] | 46 | if (closesocket(s->fd) < 0) { |
| 47 | r = -socket_error(); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 48 | } |
Eduardo Habkost | 61a5872 | 2011-11-10 10:41:47 -0200 | [diff] [blame] | 49 | return r; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Orit Wasserman | 233aa5c | 2012-09-24 13:11:09 +0200 | [diff] [blame] | 52 | static void tcp_wait_for_connect(int fd, void *opaque) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 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 | |
Orit Wasserman | 233aa5c | 2012-09-24 13:11:09 +0200 | [diff] [blame] | 56 | if (fd < 0) { |
| 57 | DPRINTF("migrate connect error\n"); |
| 58 | s->fd = -1; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 59 | migrate_fd_error(s); |
Orit Wasserman | 233aa5c | 2012-09-24 13:11:09 +0200 | [diff] [blame] | 60 | } else { |
| 61 | DPRINTF("migrate connect success\n"); |
| 62 | s->fd = fd; |
Juan Quintela | dd217b8 | 2012-07-23 06:15:02 +0200 | [diff] [blame] | 63 | socket_set_block(s->fd); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 64 | migrate_fd_connect(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 68 | void tcp_start_outgoing_migration(MigrationState *s, const char *host_port, Error **errp) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 69 | { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 70 | s->get_error = socket_errno; |
| 71 | s->write = socket_write; |
| 72 | s->close = tcp_close; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 73 | |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 74 | s->fd = inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, errp); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void tcp_accept_incoming_migration(void *opaque) |
| 78 | { |
| 79 | struct sockaddr_in addr; |
| 80 | socklen_t addrlen = sizeof(addr); |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 81 | int s = (intptr_t)opaque; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 82 | QEMUFile *f; |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 83 | int c; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 84 | |
| 85 | do { |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 86 | c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen); |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 87 | } while (c == -1 && socket_error() == EINTR); |
Paolo Bonzini | a6ef290 | 2012-08-07 10:49:13 +0200 | [diff] [blame] | 88 | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); |
Paolo Bonzini | 09bac73 | 2012-09-27 13:33:08 +0200 | [diff] [blame] | 89 | closesocket(s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 90 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 91 | DPRINTF("accepted migration\n"); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 92 | |
| 93 | if (c == -1) { |
| 94 | fprintf(stderr, "could not accept migration connection\n"); |
Paolo Bonzini | a6ef290 | 2012-08-07 10:49:13 +0200 | [diff] [blame] | 95 | goto out; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 96 | } |
| 97 | |
aliguori | c1d3666 | 2008-10-24 21:55:17 +0000 | [diff] [blame] | 98 | f = qemu_fopen_socket(c); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 99 | if (f == NULL) { |
| 100 | fprintf(stderr, "could not qemu_fopen socket\n"); |
| 101 | goto out; |
| 102 | } |
| 103 | |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 104 | process_incoming_migration(f); |
Paolo Bonzini | ab52a82 | 2012-08-07 10:50:26 +0200 | [diff] [blame] | 105 | return; |
| 106 | |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 107 | out: |
Paolo Bonzini | 09bac73 | 2012-09-27 13:33:08 +0200 | [diff] [blame] | 108 | closesocket(c); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 111 | void tcp_start_incoming_migration(const char *host_port, Error **errp) |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 112 | { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 113 | int s; |
| 114 | |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 115 | s = inet_listen(host_port, NULL, 256, SOCK_STREAM, 0, errp); |
Amos Kong | d5c5dac | 2012-05-11 00:28:35 +0800 | [diff] [blame] | 116 | if (s < 0) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 117 | return; |
Juan Quintela | ee86c61 | 2011-02-23 20:44:29 +0100 | [diff] [blame] | 118 | } |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 119 | |
| 120 | qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL, |
Stefan Weil | e0efb99 | 2011-02-23 19:09:16 +0100 | [diff] [blame] | 121 | (void *)(intptr_t)s); |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 122 | } |