2
0

migration-tcp.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. //#define DEBUG_MIGRATION_TCP
  21. #ifdef DEBUG_MIGRATION_TCP
  22. #define DPRINTF(fmt, ...) \
  23. do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
  24. #else
  25. #define DPRINTF(fmt, ...) \
  26. do { } while (0)
  27. #endif
  28. static int socket_errno(MigrationState *s)
  29. {
  30. return socket_error();
  31. }
  32. static int socket_write(MigrationState *s, const void * buf, size_t size)
  33. {
  34. return send(s->fd, buf, size, 0);
  35. }
  36. static int tcp_close(MigrationState *s)
  37. {
  38. int r = 0;
  39. DPRINTF("tcp_close\n");
  40. if (closesocket(s->fd) < 0) {
  41. r = -socket_error();
  42. }
  43. return r;
  44. }
  45. static void tcp_wait_for_connect(int fd, void *opaque)
  46. {
  47. MigrationState *s = opaque;
  48. if (fd < 0) {
  49. DPRINTF("migrate connect error\n");
  50. s->fd = -1;
  51. migrate_fd_error(s);
  52. } else {
  53. DPRINTF("migrate connect success\n");
  54. s->fd = fd;
  55. qemu_set_block(s->fd);
  56. migrate_fd_connect(s);
  57. }
  58. }
  59. void tcp_start_outgoing_migration(MigrationState *s, const char *host_port, Error **errp)
  60. {
  61. s->get_error = socket_errno;
  62. s->write = socket_write;
  63. s->close = tcp_close;
  64. s->fd = inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, errp);
  65. }
  66. static void tcp_accept_incoming_migration(void *opaque)
  67. {
  68. struct sockaddr_in addr;
  69. socklen_t addrlen = sizeof(addr);
  70. int s = (intptr_t)opaque;
  71. QEMUFile *f;
  72. int c;
  73. do {
  74. c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
  75. } while (c == -1 && socket_error() == EINTR);
  76. qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
  77. closesocket(s);
  78. DPRINTF("accepted migration\n");
  79. if (c == -1) {
  80. fprintf(stderr, "could not accept migration connection\n");
  81. goto out;
  82. }
  83. f = qemu_fopen_socket(c);
  84. if (f == NULL) {
  85. fprintf(stderr, "could not qemu_fopen socket\n");
  86. goto out;
  87. }
  88. process_incoming_migration(f);
  89. return;
  90. out:
  91. closesocket(c);
  92. }
  93. void tcp_start_incoming_migration(const char *host_port, Error **errp)
  94. {
  95. int s;
  96. s = inet_listen(host_port, NULL, 256, SOCK_STREAM, 0, errp);
  97. if (s < 0) {
  98. return;
  99. }
  100. qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
  101. (void *)(intptr_t)s);
  102. }