replication.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Replication filter
  3. *
  4. * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
  5. * Copyright (c) 2016 Intel Corporation
  6. * Copyright (c) 2016 FUJITSU LIMITED
  7. *
  8. * Author:
  9. * Changlong Xie <xiecl.fnst@cn.fujitsu.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  12. * See the COPYING file in the top-level directory.
  13. */
  14. #ifndef REPLICATION_H
  15. #define REPLICATION_H
  16. #include "qapi/qapi-types-block-core.h"
  17. #include "qemu/queue.h"
  18. typedef struct ReplicationOps ReplicationOps;
  19. typedef struct ReplicationState ReplicationState;
  20. /**
  21. * SECTION:replication.h
  22. * @title:Base Replication System
  23. * @short_description: interfaces for handling replication
  24. *
  25. * The Replication Model provides a framework for handling Replication
  26. *
  27. * <example>
  28. * <title>How to use replication interfaces</title>
  29. * <programlisting>
  30. * #include "replication.h"
  31. *
  32. * typedef struct BDRVReplicationState {
  33. * ReplicationState *rs;
  34. * } BDRVReplicationState;
  35. *
  36. * static void replication_start(ReplicationState *rs, ReplicationMode mode,
  37. * Error **errp);
  38. * static void replication_do_checkpoint(ReplicationState *rs, Error **errp);
  39. * static void replication_get_error(ReplicationState *rs, Error **errp);
  40. * static void replication_stop(ReplicationState *rs, bool failover,
  41. * Error **errp);
  42. *
  43. * static ReplicationOps replication_ops = {
  44. * .start = replication_start,
  45. * .checkpoint = replication_do_checkpoint,
  46. * .get_error = replication_get_error,
  47. * .stop = replication_stop,
  48. * }
  49. *
  50. * static int replication_open(BlockDriverState *bs, QDict *options,
  51. * int flags, Error **errp)
  52. * {
  53. * BDRVReplicationState *s = bs->opaque;
  54. * s->rs = replication_new(bs, &replication_ops);
  55. * return 0;
  56. * }
  57. *
  58. * static void replication_close(BlockDriverState *bs)
  59. * {
  60. * BDRVReplicationState *s = bs->opaque;
  61. * replication_remove(s->rs);
  62. * }
  63. *
  64. * BlockDriver bdrv_replication = {
  65. * .format_name = "replication",
  66. * .instance_size = sizeof(BDRVReplicationState),
  67. *
  68. * .bdrv_open = replication_open,
  69. * .bdrv_close = replication_close,
  70. * };
  71. *
  72. * static void bdrv_replication_init(void)
  73. * {
  74. * bdrv_register(&bdrv_replication);
  75. * }
  76. *
  77. * block_init(bdrv_replication_init);
  78. * </programlisting>
  79. * </example>
  80. *
  81. * We create an example about how to use replication interfaces in above.
  82. * Then in migration, we can use replication_(start/stop/do_checkpoint/
  83. * get_error)_all to handle all replication operations.
  84. */
  85. /**
  86. * ReplicationState:
  87. * @opaque: opaque pointer value passed to this ReplicationState
  88. * @ops: replication operation of this ReplicationState
  89. * @node: node that we will insert into @replication_states QLIST
  90. */
  91. struct ReplicationState {
  92. void *opaque;
  93. ReplicationOps *ops;
  94. QLIST_ENTRY(ReplicationState) node;
  95. };
  96. /**
  97. * ReplicationOps:
  98. * @start: callback to start replication
  99. * @stop: callback to stop replication
  100. * @checkpoint: callback to do checkpoint
  101. * @get_error: callback to check if error occurred during replication
  102. */
  103. struct ReplicationOps {
  104. void (*start)(ReplicationState *rs, ReplicationMode mode, Error **errp);
  105. void (*stop)(ReplicationState *rs, bool failover, Error **errp);
  106. void (*checkpoint)(ReplicationState *rs, Error **errp);
  107. void (*get_error)(ReplicationState *rs, Error **errp);
  108. };
  109. /**
  110. * replication_new:
  111. * @opaque: opaque pointer value passed to ReplicationState
  112. * @ops: replication operation of the new relevant ReplicationState
  113. *
  114. * Called to create a new ReplicationState instance, and then insert it
  115. * into @replication_states QLIST
  116. *
  117. * Returns: the new ReplicationState instance
  118. */
  119. ReplicationState *replication_new(void *opaque, ReplicationOps *ops);
  120. /**
  121. * replication_remove:
  122. * @rs: the ReplicationState instance to remove
  123. *
  124. * Called to remove a ReplicationState instance, and then delete it from
  125. * @replication_states QLIST
  126. */
  127. void replication_remove(ReplicationState *rs);
  128. /**
  129. * replication_start_all:
  130. * @mode: replication mode that could be "primary" or "secondary"
  131. * @errp: returns an error if this function fails
  132. *
  133. * Start replication, called in migration/checkpoint thread
  134. *
  135. * Note: the caller of the function MUST make sure vm stopped
  136. */
  137. void replication_start_all(ReplicationMode mode, Error **errp);
  138. /**
  139. * replication_do_checkpoint_all:
  140. * @errp: returns an error if this function fails
  141. *
  142. * This interface is called after all VM state is transferred to Secondary QEMU
  143. */
  144. void replication_do_checkpoint_all(Error **errp);
  145. /**
  146. * replication_get_error_all:
  147. * @errp: returns an error if this function fails
  148. *
  149. * This interface is called to check if error occurred during replication
  150. */
  151. void replication_get_error_all(Error **errp);
  152. /**
  153. * replication_stop_all:
  154. * @failover: boolean value that indicates if we need do failover or not
  155. * @errp: returns an error if this function fails
  156. *
  157. * It is called on failover. The vm should be stopped before calling it, if you
  158. * use this API to shutdown the guest, or other things except failover
  159. */
  160. void replication_stop_all(bool failover, Error **errp);
  161. #endif /* REPLICATION_H */