migration-tcp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(MigrationState *s)
  28. {
  29. return socket_error();
  30. }
  31. static int socket_write(MigrationState *s, const void * buf, size_t size)
  32. {
  33. return send(s->fd, buf, size, 0);
  34. }
  35. static int tcp_close(MigrationState *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. MigrationState *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 && (socket_error()) == 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. int tcp_start_outgoing_migration(MigrationState *s, const char *host_port)
  66. {
  67. struct sockaddr_in addr;
  68. int ret;
  69. ret = parse_host_port(&addr, host_port);
  70. if (ret < 0) {
  71. return ret;
  72. }
  73. s->get_error = socket_errno;
  74. s->write = socket_write;
  75. s->close = tcp_close;
  76. s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
  77. if (s->fd == -1) {
  78. DPRINTF("Unable to open socket");
  79. return -socket_error();
  80. }
  81. socket_set_nonblock(s->fd);
  82. do {
  83. ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
  84. if (ret == -1) {
  85. ret = -socket_error();
  86. }
  87. if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
  88. qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
  89. return 0;
  90. }
  91. } while (ret == -EINTR);
  92. if (ret < 0) {
  93. DPRINTF("connect failed\n");
  94. migrate_fd_error(s);
  95. return ret;
  96. }
  97. migrate_fd_connect(s);
  98. return 0;
  99. }
  100. static void tcp_accept_incoming_migration(void *opaque)
  101. {
  102. struct sockaddr_in addr;
  103. socklen_t addrlen = sizeof(addr);
  104. int s = (intptr_t)opaque;
  105. QEMUFile *f;
  106. int c;
  107. do {
  108. c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
  109. } while (c == -1 && socket_error() == EINTR);
  110. DPRINTF("accepted migration\n");
  111. if (c == -1) {
  112. fprintf(stderr, "could not accept migration connection\n");
  113. goto out2;
  114. }
  115. f = qemu_fopen_socket(c);
  116. if (f == NULL) {
  117. fprintf(stderr, "could not qemu_fopen socket\n");
  118. goto out;
  119. }
  120. process_incoming_migration(f);
  121. qemu_fclose(f);
  122. out:
  123. close(c);
  124. out2:
  125. qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
  126. close(s);
  127. }
  128. int tcp_start_incoming_migration(const char *host_port)
  129. {
  130. struct sockaddr_in addr;
  131. int val;
  132. int s;
  133. DPRINTF("Attempting to start an incoming migration\n");
  134. if (parse_host_port(&addr, host_port) < 0) {
  135. fprintf(stderr, "invalid host/port combination: %s\n", host_port);
  136. return -EINVAL;
  137. }
  138. s = qemu_socket(PF_INET, SOCK_STREAM, 0);
  139. if (s == -1) {
  140. return -socket_error();
  141. }
  142. val = 1;
  143. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
  144. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
  145. goto err;
  146. }
  147. if (listen(s, 1) == -1) {
  148. goto err;
  149. }
  150. qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
  151. (void *)(intptr_t)s);
  152. return 0;
  153. err:
  154. close(s);
  155. return -socket_error();
  156. }