migration-tcp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include "qemu-common.h"
  14. #include "qemu_socket.h"
  15. #include "migration.h"
  16. #include "qemu-char.h"
  17. #include "buffered_file.h"
  18. #include "block.h"
  19. //#define DEBUG_MIGRATION_TCP
  20. #ifdef DEBUG_MIGRATION_TCP
  21. #define DPRINTF(fmt, ...) \
  22. do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
  23. #else
  24. #define DPRINTF(fmt, ...) \
  25. do { } while (0)
  26. #endif
  27. static int socket_errno(FdMigrationState *s)
  28. {
  29. return socket_error();
  30. }
  31. static int socket_write(FdMigrationState *s, const void * buf, size_t size)
  32. {
  33. return send(s->fd, buf, size, 0);
  34. }
  35. static int tcp_close(FdMigrationState *s)
  36. {
  37. DPRINTF("tcp_close\n");
  38. if (s->fd != -1) {
  39. close(s->fd);
  40. s->fd = -1;
  41. }
  42. return 0;
  43. }
  44. static void tcp_wait_for_connect(void *opaque)
  45. {
  46. FdMigrationState *s = opaque;
  47. int val, ret;
  48. socklen_t valsize = sizeof(val);
  49. DPRINTF("connect completed\n");
  50. do {
  51. ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
  52. } while (ret == -1 && (s->get_error(s)) == EINTR);
  53. if (ret < 0) {
  54. migrate_fd_error(s);
  55. return;
  56. }
  57. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  58. if (val == 0)
  59. migrate_fd_connect(s);
  60. else {
  61. DPRINTF("error connecting %d\n", val);
  62. migrate_fd_error(s);
  63. }
  64. }
  65. MigrationState *tcp_start_outgoing_migration(Monitor *mon,
  66. const char *host_port,
  67. int64_t bandwidth_limit,
  68. int detach,
  69. int blk,
  70. int inc)
  71. {
  72. struct sockaddr_in addr;
  73. FdMigrationState *s;
  74. int ret;
  75. if (parse_host_port(&addr, host_port) < 0)
  76. return NULL;
  77. s = qemu_mallocz(sizeof(*s));
  78. s->get_error = socket_errno;
  79. s->write = socket_write;
  80. s->close = tcp_close;
  81. s->mig_state.cancel = migrate_fd_cancel;
  82. s->mig_state.get_status = migrate_fd_get_status;
  83. s->mig_state.release = migrate_fd_release;
  84. s->mig_state.blk = blk;
  85. s->mig_state.shared = inc;
  86. s->state = MIG_STATE_ACTIVE;
  87. s->mon = NULL;
  88. s->bandwidth_limit = bandwidth_limit;
  89. s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
  90. if (s->fd == -1) {
  91. qemu_free(s);
  92. return NULL;
  93. }
  94. socket_set_nonblock(s->fd);
  95. if (!detach) {
  96. migrate_fd_monitor_suspend(s, mon);
  97. }
  98. do {
  99. ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
  100. if (ret == -1)
  101. ret = -(s->get_error(s));
  102. if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
  103. qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
  104. } while (ret == -EINTR);
  105. if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
  106. DPRINTF("connect failed\n");
  107. migrate_fd_error(s);
  108. } else if (ret >= 0)
  109. migrate_fd_connect(s);
  110. return &s->mig_state;
  111. }
  112. static void tcp_accept_incoming_migration(void *opaque)
  113. {
  114. struct sockaddr_in addr;
  115. socklen_t addrlen = sizeof(addr);
  116. int s = (intptr_t)opaque;
  117. QEMUFile *f;
  118. int c;
  119. do {
  120. c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
  121. } while (c == -1 && socket_error() == EINTR);
  122. DPRINTF("accepted migration\n");
  123. if (c == -1) {
  124. fprintf(stderr, "could not accept migration connection\n");
  125. goto out2;
  126. }
  127. f = qemu_fopen_socket(c);
  128. if (f == NULL) {
  129. fprintf(stderr, "could not qemu_fopen socket\n");
  130. goto out;
  131. }
  132. process_incoming_migration(f);
  133. qemu_fclose(f);
  134. out:
  135. close(c);
  136. out2:
  137. qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
  138. close(s);
  139. }
  140. int tcp_start_incoming_migration(const char *host_port)
  141. {
  142. struct sockaddr_in addr;
  143. int val;
  144. int s;
  145. if (parse_host_port(&addr, host_port) < 0) {
  146. fprintf(stderr, "invalid host/port combination: %s\n", host_port);
  147. return -EINVAL;
  148. }
  149. s = qemu_socket(PF_INET, SOCK_STREAM, 0);
  150. if (s == -1)
  151. return -socket_error();
  152. val = 1;
  153. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
  154. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
  155. goto err;
  156. if (listen(s, 1) == -1)
  157. goto err;
  158. qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
  159. (void *)(intptr_t)s);
  160. return 0;
  161. err:
  162. close(s);
  163. return -socket_error();
  164. }