migration-unix.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * 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_UNIX
  22. #ifdef DEBUG_MIGRATION_UNIX
  23. #define DPRINTF(fmt, ...) \
  24. do { printf("migration-unix: " fmt, ## __VA_ARGS__); } while (0)
  25. #else
  26. #define DPRINTF(fmt, ...) \
  27. do { } while (0)
  28. #endif
  29. static int unix_errno(MigrationState *s)
  30. {
  31. return errno;
  32. }
  33. static int unix_write(MigrationState *s, const void * buf, size_t size)
  34. {
  35. return write(s->fd, buf, size);
  36. }
  37. static int unix_close(MigrationState *s)
  38. {
  39. int r = 0;
  40. DPRINTF("unix_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 unix_wait_for_connect(void *opaque)
  50. {
  51. MigrationState *s = opaque;
  52. int val, ret;
  53. socklen_t valsize = sizeof(val);
  54. DPRINTF("connect completed\n");
  55. do {
  56. ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
  57. } while (ret == -1 && errno == EINTR);
  58. if (ret < 0) {
  59. migrate_fd_error(s);
  60. return;
  61. }
  62. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  63. if (val == 0)
  64. migrate_fd_connect(s);
  65. else {
  66. DPRINTF("error connecting %d\n", val);
  67. migrate_fd_error(s);
  68. }
  69. }
  70. int unix_start_outgoing_migration(MigrationState *s, const char *path)
  71. {
  72. struct sockaddr_un addr;
  73. int ret;
  74. addr.sun_family = AF_UNIX;
  75. snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
  76. s->get_error = unix_errno;
  77. s->write = unix_write;
  78. s->close = unix_close;
  79. s->fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
  80. if (s->fd == -1) {
  81. DPRINTF("Unable to open socket");
  82. return -errno;
  83. }
  84. socket_set_nonblock(s->fd);
  85. do {
  86. ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
  87. if (ret == -1) {
  88. ret = -errno;
  89. }
  90. if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
  91. qemu_set_fd_handler2(s->fd, NULL, NULL, unix_wait_for_connect, s);
  92. return 0;
  93. }
  94. } while (ret == -EINTR);
  95. if (ret < 0) {
  96. DPRINTF("connect failed\n");
  97. migrate_fd_error(s);
  98. return ret;
  99. }
  100. migrate_fd_connect(s);
  101. return 0;
  102. }
  103. static void unix_accept_incoming_migration(void *opaque)
  104. {
  105. struct sockaddr_un addr;
  106. socklen_t addrlen = sizeof(addr);
  107. int s = (intptr_t)opaque;
  108. QEMUFile *f;
  109. int c;
  110. do {
  111. c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
  112. } while (c == -1 && errno == EINTR);
  113. DPRINTF("accepted migration\n");
  114. if (c == -1) {
  115. fprintf(stderr, "could not accept migration connection\n");
  116. goto out2;
  117. }
  118. f = qemu_fopen_socket(c);
  119. if (f == NULL) {
  120. fprintf(stderr, "could not qemu_fopen socket\n");
  121. goto out;
  122. }
  123. process_incoming_migration(f);
  124. qemu_fclose(f);
  125. out:
  126. close(c);
  127. out2:
  128. qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
  129. close(s);
  130. }
  131. int unix_start_incoming_migration(const char *path)
  132. {
  133. struct sockaddr_un addr;
  134. int s;
  135. int ret;
  136. DPRINTF("Attempting to start an incoming migration\n");
  137. s = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
  138. if (s == -1) {
  139. fprintf(stderr, "Could not open unix socket: %s\n", strerror(errno));
  140. return -errno;
  141. }
  142. memset(&addr, 0, sizeof(addr));
  143. addr.sun_family = AF_UNIX;
  144. snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
  145. unlink(addr.sun_path);
  146. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  147. ret = -errno;
  148. fprintf(stderr, "bind(unix:%s): %s\n", addr.sun_path, strerror(errno));
  149. goto err;
  150. }
  151. if (listen(s, 1) == -1) {
  152. fprintf(stderr, "listen(unix:%s): %s\n", addr.sun_path,
  153. strerror(errno));
  154. ret = -errno;
  155. goto err;
  156. }
  157. qemu_set_fd_handler2(s, NULL, unix_accept_incoming_migration, NULL,
  158. (void *)(intptr_t)s);
  159. return 0;
  160. err:
  161. close(s);
  162. return ret;
  163. }