migration-unix.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/sockets.h"
  17. #include "migration/migration.h"
  18. #include "migration/qemu-file.h"
  19. #include "block/block.h"
  20. //#define DEBUG_MIGRATION_UNIX
  21. #ifdef DEBUG_MIGRATION_UNIX
  22. #define DPRINTF(fmt, ...) \
  23. do { printf("migration-unix: " fmt, ## __VA_ARGS__); } while (0)
  24. #else
  25. #define DPRINTF(fmt, ...) \
  26. do { } while (0)
  27. #endif
  28. static int unix_errno(MigrationState *s)
  29. {
  30. return errno;
  31. }
  32. static int unix_write(MigrationState *s, const void * buf, size_t size)
  33. {
  34. return write(s->fd, buf, size);
  35. }
  36. static int unix_close(MigrationState *s)
  37. {
  38. int r = 0;
  39. DPRINTF("unix_close\n");
  40. if (close(s->fd) < 0) {
  41. r = -errno;
  42. }
  43. return r;
  44. }
  45. static void unix_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 unix_start_outgoing_migration(MigrationState *s, const char *path, Error **errp)
  60. {
  61. s->get_error = unix_errno;
  62. s->write = unix_write;
  63. s->close = unix_close;
  64. s->fd = unix_nonblocking_connect(path, unix_wait_for_connect, s, errp);
  65. }
  66. static void unix_accept_incoming_migration(void *opaque)
  67. {
  68. struct sockaddr_un 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 && errno == EINTR);
  76. qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
  77. close(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. close(c);
  92. }
  93. void unix_start_incoming_migration(const char *path, Error **errp)
  94. {
  95. int s;
  96. s = unix_listen(path, NULL, 0, errp);
  97. if (s < 0) {
  98. return;
  99. }
  100. qemu_set_fd_handler2(s, NULL, unix_accept_incoming_migration, NULL,
  101. (void *)(intptr_t)s);
  102. }