migration-tcp.c 2.3 KB

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