migration-tcp.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <string.h>
  16. #include "qemu-common.h"
  17. #include "qemu/error-report.h"
  18. #include "qemu/sockets.h"
  19. #include "migration/migration.h"
  20. #include "migration/qemu-file.h"
  21. #include "block/block.h"
  22. #include "qemu/main-loop.h"
  23. //#define DEBUG_MIGRATION_TCP
  24. #ifdef DEBUG_MIGRATION_TCP
  25. #define DPRINTF(fmt, ...) \
  26. do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
  27. #else
  28. #define DPRINTF(fmt, ...) \
  29. do { } while (0)
  30. #endif
  31. static void tcp_wait_for_connect(int fd, Error *err, void *opaque)
  32. {
  33. MigrationState *s = opaque;
  34. if (fd < 0) {
  35. DPRINTF("migrate connect error: %s\n", error_get_pretty(err));
  36. s->file = NULL;
  37. migrate_fd_error(s);
  38. } else {
  39. DPRINTF("migrate connect success\n");
  40. s->file = qemu_fopen_socket(fd, "wb");
  41. migrate_fd_connect(s);
  42. }
  43. }
  44. void tcp_start_outgoing_migration(MigrationState *s, const char *host_port, Error **errp)
  45. {
  46. inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, errp);
  47. }
  48. static void tcp_accept_incoming_migration(void *opaque)
  49. {
  50. struct sockaddr_in addr;
  51. socklen_t addrlen = sizeof(addr);
  52. int s = (intptr_t)opaque;
  53. QEMUFile *f;
  54. int c, err;
  55. do {
  56. c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
  57. err = socket_error();
  58. } while (c < 0 && err == EINTR);
  59. qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
  60. closesocket(s);
  61. DPRINTF("accepted migration\n");
  62. if (c < 0) {
  63. error_report("could not accept migration connection (%s)",
  64. strerror(err));
  65. return;
  66. }
  67. f = qemu_fopen_socket(c, "rb");
  68. if (f == NULL) {
  69. error_report("could not qemu_fopen socket");
  70. goto out;
  71. }
  72. process_incoming_migration(f);
  73. return;
  74. out:
  75. closesocket(c);
  76. }
  77. void tcp_start_incoming_migration(const char *host_port, Error **errp)
  78. {
  79. int s;
  80. s = inet_listen(host_port, NULL, 256, SOCK_STREAM, 0, errp);
  81. if (s < 0) {
  82. return;
  83. }
  84. qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
  85. (void *)(intptr_t)s);
  86. }