migration-unix.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * QEMU live migration via Unix Domain Sockets
  3. *
  4. * Copyright Red Hat, Inc. 2009
  5. *
  6. * Authors:
  7. * Chris Lalancette <clalance@redhat.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_UNIX
  20. #ifdef DEBUG_MIGRATION_UNIX
  21. #define DPRINTF(fmt, ...) \
  22. do { printf("migration-unix: " fmt, ## __VA_ARGS__); } while (0)
  23. #else
  24. #define DPRINTF(fmt, ...) \
  25. do { } while (0)
  26. #endif
  27. static int unix_errno(MigrationState *s)
  28. {
  29. return errno;
  30. }
  31. static int unix_write(MigrationState *s, const void * buf, size_t size)
  32. {
  33. return write(s->fd, buf, size);
  34. }
  35. static int unix_close(MigrationState *s)
  36. {
  37. DPRINTF("unix_close\n");
  38. if (s->fd != -1) {
  39. close(s->fd);
  40. s->fd = -1;
  41. }
  42. return 0;
  43. }
  44. static void unix_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 && errno == 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 unix_start_outgoing_migration(MigrationState *s, const char *path)
  66. {
  67. struct sockaddr_un addr;
  68. int ret;
  69. addr.sun_family = AF_UNIX;
  70. snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
  71. s->get_error = unix_errno;
  72. s->write = unix_write;
  73. s->close = unix_close;
  74. s->fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
  75. if (s->fd == -1) {
  76. DPRINTF("Unable to open socket");
  77. return -errno;
  78. }
  79. socket_set_nonblock(s->fd);
  80. do {
  81. ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
  82. if (ret == -1) {
  83. ret = -errno;
  84. }
  85. if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
  86. qemu_set_fd_handler2(s->fd, NULL, NULL, unix_wait_for_connect, s);
  87. return 0;
  88. }
  89. } while (ret == -EINTR);
  90. if (ret < 0) {
  91. DPRINTF("connect failed\n");
  92. migrate_fd_error(s);
  93. return ret;
  94. }
  95. migrate_fd_connect(s);
  96. return 0;
  97. }
  98. static void unix_accept_incoming_migration(void *opaque)
  99. {
  100. struct sockaddr_un addr;
  101. socklen_t addrlen = sizeof(addr);
  102. int s = (intptr_t)opaque;
  103. QEMUFile *f;
  104. int c;
  105. do {
  106. c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
  107. } while (c == -1 && errno == EINTR);
  108. DPRINTF("accepted migration\n");
  109. if (c == -1) {
  110. fprintf(stderr, "could not accept migration connection\n");
  111. goto out2;
  112. }
  113. f = qemu_fopen_socket(c);
  114. if (f == NULL) {
  115. fprintf(stderr, "could not qemu_fopen socket\n");
  116. goto out;
  117. }
  118. process_incoming_migration(f);
  119. qemu_fclose(f);
  120. out:
  121. close(c);
  122. out2:
  123. qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
  124. close(s);
  125. }
  126. int unix_start_incoming_migration(const char *path)
  127. {
  128. struct sockaddr_un addr;
  129. int s;
  130. int ret;
  131. DPRINTF("Attempting to start an incoming migration\n");
  132. s = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
  133. if (s == -1) {
  134. fprintf(stderr, "Could not open unix socket: %s\n", strerror(errno));
  135. return -errno;
  136. }
  137. memset(&addr, 0, sizeof(addr));
  138. addr.sun_family = AF_UNIX;
  139. snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
  140. unlink(addr.sun_path);
  141. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  142. ret = -errno;
  143. fprintf(stderr, "bind(unix:%s): %s\n", addr.sun_path, strerror(errno));
  144. goto err;
  145. }
  146. if (listen(s, 1) == -1) {
  147. fprintf(stderr, "listen(unix:%s): %s\n", addr.sun_path,
  148. strerror(errno));
  149. ret = -errno;
  150. goto err;
  151. }
  152. qemu_set_fd_handler2(s, NULL, unix_accept_incoming_migration, NULL,
  153. (void *)(intptr_t)s);
  154. return 0;
  155. err:
  156. close(s);
  157. return ret;
  158. }