migration-tcp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu-common.h"
  16. #include "qemu_socket.h"
  17. #include "migration.h"
  18. #include "qemu-char.h"
  19. #include "buffered_file.h"
  20. #include "block.h"
  21. //#define DEBUG_MIGRATION_TCP
  22. #ifdef DEBUG_MIGRATION_TCP
  23. #define DPRINTF(fmt, ...) \
  24. do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
  25. #else
  26. #define DPRINTF(fmt, ...) \
  27. do { } while (0)
  28. #endif
  29. static int socket_errno(MigrationState *s)
  30. {
  31. return socket_error();
  32. }
  33. static int socket_write(MigrationState *s, const void * buf, size_t size)
  34. {
  35. return send(s->fd, buf, size, 0);
  36. }
  37. static int tcp_close(MigrationState *s)
  38. {
  39. int r = 0;
  40. DPRINTF("tcp_close\n");
  41. if (s->fd != -1) {
  42. if (close(s->fd) < 0) {
  43. r = -errno;
  44. }
  45. s->fd = -1;
  46. }
  47. return r;
  48. }
  49. static void tcp_wait_for_connect(int fd, void *opaque)
  50. {
  51. MigrationState *s = opaque;
  52. if (fd < 0) {
  53. DPRINTF("migrate connect error\n");
  54. s->fd = -1;
  55. migrate_fd_error(s);
  56. } else {
  57. DPRINTF("migrate connect success\n");
  58. s->fd = fd;
  59. migrate_fd_connect(s);
  60. }
  61. }
  62. int tcp_start_outgoing_migration(MigrationState *s, const char *host_port,
  63. Error **errp)
  64. {
  65. s->get_error = socket_errno;
  66. s->write = socket_write;
  67. s->close = tcp_close;
  68. s->fd = inet_nonblocking_connect(host_port, tcp_wait_for_connect, s,
  69. errp);
  70. if (error_is_set(errp)) {
  71. migrate_fd_error(s);
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. static void tcp_accept_incoming_migration(void *opaque)
  77. {
  78. struct sockaddr_in addr;
  79. socklen_t addrlen = sizeof(addr);
  80. int s = (intptr_t)opaque;
  81. QEMUFile *f;
  82. int c;
  83. do {
  84. c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
  85. } while (c == -1 && socket_error() == EINTR);
  86. DPRINTF("accepted migration\n");
  87. if (c == -1) {
  88. fprintf(stderr, "could not accept migration connection\n");
  89. goto out2;
  90. }
  91. f = qemu_fopen_socket(c);
  92. if (f == NULL) {
  93. fprintf(stderr, "could not qemu_fopen socket\n");
  94. goto out;
  95. }
  96. process_incoming_migration(f);
  97. qemu_fclose(f);
  98. out:
  99. close(c);
  100. out2:
  101. qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
  102. close(s);
  103. }
  104. int tcp_start_incoming_migration(const char *host_port, Error **errp)
  105. {
  106. int s;
  107. s = inet_listen(host_port, NULL, 256, SOCK_STREAM, 0, errp);
  108. if (s < 0) {
  109. return -1;
  110. }
  111. qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
  112. (void *)(intptr_t)s);
  113. return 0;
  114. }