2
0

migration-unix.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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(FdMigrationState *s)
  28. {
  29. return errno;
  30. }
  31. static int unix_write(FdMigrationState *s, const void * buf, size_t size)
  32. {
  33. return write(s->fd, buf, size);
  34. }
  35. static int unix_close(FdMigrationState *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. FdMigrationState *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 && (s->get_error(s)) == 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. MigrationState *unix_start_outgoing_migration(Monitor *mon,
  66. const char *path,
  67. int64_t bandwidth_limit,
  68. int detach,
  69. int blk,
  70. int inc)
  71. {
  72. FdMigrationState *s;
  73. struct sockaddr_un addr;
  74. int ret;
  75. addr.sun_family = AF_UNIX;
  76. snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
  77. s = g_malloc0(sizeof(*s));
  78. s->get_error = unix_errno;
  79. s->write = unix_write;
  80. s->close = unix_close;
  81. s->mig_state.cancel = migrate_fd_cancel;
  82. s->mig_state.get_status = migrate_fd_get_status;
  83. s->mig_state.release = migrate_fd_release;
  84. s->mig_state.blk = blk;
  85. s->mig_state.shared = inc;
  86. s->state = MIG_STATE_ACTIVE;
  87. s->mon = NULL;
  88. s->bandwidth_limit = bandwidth_limit;
  89. s->fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
  90. if (s->fd < 0) {
  91. DPRINTF("Unable to open socket");
  92. goto err_after_alloc;
  93. }
  94. socket_set_nonblock(s->fd);
  95. do {
  96. ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
  97. if (ret == -1)
  98. ret = -(s->get_error(s));
  99. if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
  100. qemu_set_fd_handler2(s->fd, NULL, NULL, unix_wait_for_connect, s);
  101. } while (ret == -EINTR);
  102. if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
  103. DPRINTF("connect failed\n");
  104. goto err_after_open;
  105. }
  106. if (!detach) {
  107. migrate_fd_monitor_suspend(s, mon);
  108. }
  109. if (ret >= 0)
  110. migrate_fd_connect(s);
  111. return &s->mig_state;
  112. err_after_open:
  113. close(s->fd);
  114. err_after_alloc:
  115. g_free(s);
  116. return NULL;
  117. }
  118. static void unix_accept_incoming_migration(void *opaque)
  119. {
  120. struct sockaddr_un addr;
  121. socklen_t addrlen = sizeof(addr);
  122. int s = (intptr_t)opaque;
  123. QEMUFile *f;
  124. int c;
  125. do {
  126. c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
  127. } while (c == -1 && socket_error() == EINTR);
  128. DPRINTF("accepted migration\n");
  129. if (c == -1) {
  130. fprintf(stderr, "could not accept migration connection\n");
  131. return;
  132. }
  133. f = qemu_fopen_socket(c);
  134. if (f == NULL) {
  135. fprintf(stderr, "could not qemu_fopen socket\n");
  136. goto out;
  137. }
  138. process_incoming_migration(f);
  139. qemu_fclose(f);
  140. out:
  141. qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
  142. close(s);
  143. close(c);
  144. }
  145. int unix_start_incoming_migration(const char *path)
  146. {
  147. struct sockaddr_un un;
  148. int sock;
  149. DPRINTF("Attempting to start an incoming migration\n");
  150. sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
  151. if (sock < 0) {
  152. fprintf(stderr, "Could not open unix socket: %s\n", strerror(errno));
  153. return -EINVAL;
  154. }
  155. memset(&un, 0, sizeof(un));
  156. un.sun_family = AF_UNIX;
  157. snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
  158. unlink(un.sun_path);
  159. if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
  160. fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
  161. goto err;
  162. }
  163. if (listen(sock, 1) < 0) {
  164. fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
  165. goto err;
  166. }
  167. qemu_set_fd_handler2(sock, NULL, unix_accept_incoming_migration, NULL,
  168. (void *)(intptr_t)sock);
  169. return 0;
  170. err:
  171. close(sock);
  172. return -EINVAL;
  173. }