migration-fd.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/sockets.h"
  17. #include "migration/migration.h"
  18. #include "monitor/monitor.h"
  19. #include "migration/qemu-file.h"
  20. #include "block/block.h"
  21. //#define DEBUG_MIGRATION_FD
  22. #ifdef DEBUG_MIGRATION_FD
  23. #define DPRINTF(fmt, ...) \
  24. do { printf("migration-fd: " fmt, ## __VA_ARGS__); } while (0)
  25. #else
  26. #define DPRINTF(fmt, ...) \
  27. do { } while (0)
  28. #endif
  29. static int fd_errno(MigrationState *s)
  30. {
  31. return errno;
  32. }
  33. static int fd_write(MigrationState *s, const void * buf, size_t size)
  34. {
  35. return write(s->fd, buf, size);
  36. }
  37. static int fd_close(MigrationState *s)
  38. {
  39. struct stat st;
  40. int ret;
  41. DPRINTF("fd_close\n");
  42. ret = fstat(s->fd, &st);
  43. if (ret == 0 && S_ISREG(st.st_mode)) {
  44. /*
  45. * If the file handle is a regular file make sure the
  46. * data is flushed to disk before signaling success.
  47. */
  48. ret = fsync(s->fd);
  49. if (ret != 0) {
  50. ret = -errno;
  51. perror("migration-fd: fsync");
  52. return ret;
  53. }
  54. }
  55. ret = close(s->fd);
  56. s->fd = -1;
  57. if (ret != 0) {
  58. ret = -errno;
  59. perror("migration-fd: close");
  60. }
  61. return ret;
  62. }
  63. void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
  64. {
  65. s->fd = monitor_get_fd(cur_mon, fdname, errp);
  66. if (s->fd == -1) {
  67. return;
  68. }
  69. s->get_error = fd_errno;
  70. s->write = fd_write;
  71. s->close = fd_close;
  72. migrate_fd_connect(s);
  73. }
  74. static void fd_accept_incoming_migration(void *opaque)
  75. {
  76. QEMUFile *f = opaque;
  77. qemu_set_fd_handler2(qemu_get_fd(f), NULL, NULL, NULL, NULL);
  78. process_incoming_migration(f);
  79. }
  80. void fd_start_incoming_migration(const char *infd, Error **errp)
  81. {
  82. int fd;
  83. QEMUFile *f;
  84. DPRINTF("Attempting to start an incoming migration via fd\n");
  85. fd = strtol(infd, NULL, 0);
  86. f = qemu_fdopen(fd, "rb");
  87. if(f == NULL) {
  88. error_setg_errno(errp, errno, "failed to open the source descriptor");
  89. return;
  90. }
  91. qemu_set_fd_handler2(fd, NULL, fd_accept_incoming_migration, NULL, f);
  92. }