migration-fd.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * QEMU live migration via generic fd
  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 "monitor.h"
  19. #include "qemu-char.h"
  20. #include "buffered_file.h"
  21. #include "block.h"
  22. #include "qemu_socket.h"
  23. //#define DEBUG_MIGRATION_FD
  24. #ifdef DEBUG_MIGRATION_FD
  25. #define DPRINTF(fmt, ...) \
  26. do { printf("migration-fd: " fmt, ## __VA_ARGS__); } while (0)
  27. #else
  28. #define DPRINTF(fmt, ...) \
  29. do { } while (0)
  30. #endif
  31. static int fd_errno(MigrationState *s)
  32. {
  33. return errno;
  34. }
  35. static int fd_write(MigrationState *s, const void * buf, size_t size)
  36. {
  37. return write(s->fd, buf, size);
  38. }
  39. static int fd_close(MigrationState *s)
  40. {
  41. struct stat st;
  42. int ret;
  43. DPRINTF("fd_close\n");
  44. if (s->fd != -1) {
  45. ret = fstat(s->fd, &st);
  46. if (ret == 0 && S_ISREG(st.st_mode)) {
  47. /*
  48. * If the file handle is a regular file make sure the
  49. * data is flushed to disk before signaling success.
  50. */
  51. ret = fsync(s->fd);
  52. if (ret != 0) {
  53. ret = -errno;
  54. perror("migration-fd: fsync");
  55. return ret;
  56. }
  57. }
  58. ret = close(s->fd);
  59. s->fd = -1;
  60. if (ret != 0) {
  61. ret = -errno;
  62. perror("migration-fd: close");
  63. return ret;
  64. }
  65. }
  66. return 0;
  67. }
  68. int fd_start_outgoing_migration(MigrationState *s, const char *fdname)
  69. {
  70. s->fd = monitor_get_fd(cur_mon, fdname);
  71. if (s->fd == -1) {
  72. DPRINTF("fd_migration: invalid file descriptor identifier\n");
  73. goto err_after_get_fd;
  74. }
  75. if (fcntl(s->fd, F_SETFL, O_NONBLOCK) == -1) {
  76. DPRINTF("Unable to set nonblocking mode on file descriptor\n");
  77. goto err_after_open;
  78. }
  79. s->get_error = fd_errno;
  80. s->write = fd_write;
  81. s->close = fd_close;
  82. migrate_fd_connect(s);
  83. return 0;
  84. err_after_open:
  85. close(s->fd);
  86. err_after_get_fd:
  87. return -1;
  88. }
  89. static void fd_accept_incoming_migration(void *opaque)
  90. {
  91. QEMUFile *f = opaque;
  92. process_incoming_migration(f);
  93. qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
  94. qemu_fclose(f);
  95. }
  96. int fd_start_incoming_migration(const char *infd)
  97. {
  98. int fd;
  99. QEMUFile *f;
  100. DPRINTF("Attempting to start an incoming migration via fd\n");
  101. fd = strtol(infd, NULL, 0);
  102. f = qemu_fdopen(fd, "rb");
  103. if(f == NULL) {
  104. DPRINTF("Unable to apply qemu wrapper to file descriptor\n");
  105. return -errno;
  106. }
  107. qemu_set_fd_handler2(fd, NULL, fd_accept_incoming_migration, NULL, f);
  108. return 0;
  109. }