2
0

migration-fd.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. */
  13. #include "qemu-common.h"
  14. #include "qemu_socket.h"
  15. #include "migration.h"
  16. #include "monitor.h"
  17. #include "qemu-char.h"
  18. #include "buffered_file.h"
  19. #include "block.h"
  20. #include "qemu_socket.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(FdMigrationState *s)
  30. {
  31. return errno;
  32. }
  33. static int fd_write(FdMigrationState *s, const void * buf, size_t size)
  34. {
  35. return write(s->fd, buf, size);
  36. }
  37. static int fd_close(FdMigrationState *s)
  38. {
  39. DPRINTF("fd_close\n");
  40. if (s->fd != -1) {
  41. close(s->fd);
  42. s->fd = -1;
  43. }
  44. return 0;
  45. }
  46. MigrationState *fd_start_outgoing_migration(Monitor *mon,
  47. const char *fdname,
  48. int64_t bandwidth_limit,
  49. int detach,
  50. int blk,
  51. int inc)
  52. {
  53. FdMigrationState *s;
  54. s = qemu_mallocz(sizeof(*s));
  55. s->fd = monitor_get_fd(mon, fdname);
  56. if (s->fd == -1) {
  57. DPRINTF("fd_migration: invalid file descriptor identifier\n");
  58. goto err_after_alloc;
  59. }
  60. if (fcntl(s->fd, F_SETFL, O_NONBLOCK) == -1) {
  61. DPRINTF("Unable to set nonblocking mode on file descriptor\n");
  62. goto err_after_open;
  63. }
  64. s->get_error = fd_errno;
  65. s->write = fd_write;
  66. s->close = fd_close;
  67. s->mig_state.cancel = migrate_fd_cancel;
  68. s->mig_state.get_status = migrate_fd_get_status;
  69. s->mig_state.release = migrate_fd_release;
  70. s->mig_state.blk = blk;
  71. s->mig_state.shared = inc;
  72. s->state = MIG_STATE_ACTIVE;
  73. s->mon = NULL;
  74. s->bandwidth_limit = bandwidth_limit;
  75. if (!detach) {
  76. migrate_fd_monitor_suspend(s, mon);
  77. }
  78. migrate_fd_connect(s);
  79. return &s->mig_state;
  80. err_after_open:
  81. close(s->fd);
  82. err_after_alloc:
  83. qemu_free(s);
  84. return NULL;
  85. }
  86. static void fd_accept_incoming_migration(void *opaque)
  87. {
  88. QEMUFile *f = opaque;
  89. process_incoming_migration(f);
  90. qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
  91. qemu_fclose(f);
  92. }
  93. int fd_start_incoming_migration(const char *infd)
  94. {
  95. int fd;
  96. QEMUFile *f;
  97. DPRINTF("Attempting to start an incoming migration via fd\n");
  98. fd = strtol(infd, NULL, 0);
  99. f = qemu_fdopen(fd, "rb");
  100. if(f == NULL) {
  101. DPRINTF("Unable to apply qemu wrapper to file descriptor\n");
  102. return -errno;
  103. }
  104. qemu_set_fd_handler2(fd, NULL, fd_accept_incoming_migration, NULL, f);
  105. return 0;
  106. }