syncv2.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2010 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld <asalkeld@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <sys/un.h>
  38. #include <sys/time.h>
  39. #include <sys/ioctl.h>
  40. #include <netinet/in.h>
  41. #include <sys/uio.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <time.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <string.h>
  51. #include <corosync/swab.h>
  52. #include <corosync/corotypes.h>
  53. #include <qb/qbipc_common.h>
  54. #include <corosync/corodefs.h>
  55. #include <corosync/lcr/lcr_comp.h>
  56. #include <corosync/mar_gen.h>
  57. #include <corosync/engine/coroapi.h>
  58. #include <corosync/list.h>
  59. #include <corosync/engine/logsys.h>
  60. LOGSYS_DECLARE_SUBSYS ("TST2");
  61. /*
  62. * Service Interfaces required by service_message_handler struct
  63. */
  64. static int tst_sv2_exec_init_fn (
  65. struct corosync_api_v1 *corosync_api);
  66. static void tst_sv2_confchg_fn (
  67. enum totem_configuration_type configuration_type,
  68. const unsigned int *member_list, size_t member_list_entries,
  69. const unsigned int *left_list, size_t left_list_entries,
  70. const unsigned int *joined_list, size_t joined_list_entries,
  71. const struct memb_ring_id *ring_id);
  72. static int tst_sv2_lib_init_fn (void *conn);
  73. static int tst_sv2_lib_exit_fn (void *conn);
  74. static struct corosync_api_v1 *api;
  75. static void tst_sv2_sync_init_v2 (
  76. const unsigned int *member_list,
  77. size_t member_list_entries,
  78. const struct memb_ring_id *ring_id);
  79. static int tst_sv2_sync_process (void);
  80. static void tst_sv2_sync_activate (void);
  81. static void tst_sv2_sync_abort (void);
  82. struct corosync_service_engine tst_sv2_service_engine = {
  83. .name = "corosync test synv2 service",
  84. .id = TST_SV2_SERVICE,
  85. .priority = 1,
  86. .private_data_size = 0,
  87. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  88. .lib_init_fn = tst_sv2_lib_init_fn,
  89. .lib_exit_fn = tst_sv2_lib_exit_fn,
  90. .lib_engine = NULL,
  91. .lib_engine_count = 0,
  92. .exec_engine = NULL,
  93. .exec_engine_count = 0,
  94. .confchg_fn = tst_sv2_confchg_fn,
  95. .exec_init_fn = tst_sv2_exec_init_fn,
  96. .exec_dump_fn = NULL,
  97. .sync_mode = CS_SYNC_V2,
  98. .sync_init = tst_sv2_sync_init_v2,
  99. .sync_process = tst_sv2_sync_process,
  100. .sync_activate = tst_sv2_sync_activate,
  101. .sync_abort = tst_sv2_sync_abort
  102. };
  103. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  104. static unsigned int my_member_list_entries;
  105. static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
  106. static unsigned int my_old_member_list_entries = 0;
  107. static int num_sync_processes = 0;
  108. static DECLARE_LIST_INIT (confchg_notify);
  109. /*
  110. * Dynamic loading descriptor
  111. */
  112. static struct corosync_service_engine *tst_sv2_get_service_engine_ver0 (void);
  113. static struct corosync_service_engine_iface_ver0 tst_sv2_service_engine_iface = {
  114. .corosync_get_service_engine_ver0 = tst_sv2_get_service_engine_ver0
  115. };
  116. static struct lcr_iface corosync_tst_sv2_ver0[1] = {
  117. {
  118. .name = "corosync_tst_sv2",
  119. .version = 0,
  120. .versions_replace = 0,
  121. .versions_replace_count = 0,
  122. .dependencies = 0,
  123. .dependency_count = 0,
  124. .constructor = NULL,
  125. .destructor = NULL,
  126. .interfaces = NULL,
  127. }
  128. };
  129. static struct lcr_comp tst_sv2_comp_ver0 = {
  130. .iface_count = 1,
  131. .ifaces = corosync_tst_sv2_ver0
  132. };
  133. static struct corosync_service_engine *tst_sv2_get_service_engine_ver0 (void)
  134. {
  135. return (&tst_sv2_service_engine);
  136. }
  137. #ifdef COROSYNC_SOLARIS
  138. void corosync_lcr_component_register (void);
  139. void corosync_lcr_component_register (void) {
  140. #else
  141. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  142. #endif
  143. lcr_interfaces_set (&corosync_tst_sv2_ver0[0], &tst_sv2_service_engine_iface);
  144. lcr_component_register (&tst_sv2_comp_ver0);
  145. }
  146. static int tst_sv2_exec_init_fn (
  147. struct corosync_api_v1 *corosync_api)
  148. {
  149. #ifdef COROSYNC_SOLARIS
  150. logsys_subsys_init();
  151. #endif
  152. api = corosync_api;
  153. return 0;
  154. }
  155. static void tst_sv2_confchg_fn (
  156. enum totem_configuration_type configuration_type,
  157. const unsigned int *member_list, size_t member_list_entries,
  158. const unsigned int *left_list, size_t left_list_entries,
  159. const unsigned int *joined_list, size_t joined_list_entries,
  160. const struct memb_ring_id *ring_id)
  161. {
  162. int j;
  163. for (j = 0; j < left_list_entries; j++) {
  164. log_printf (LOGSYS_LEVEL_INFO,
  165. "Member left: %s", api->totem_ifaces_print (left_list[j]));
  166. }
  167. for (j = 0; j < joined_list_entries; j++) {
  168. log_printf (LOGSYS_LEVEL_INFO,
  169. "Member joined: %s", api->totem_ifaces_print (joined_list[j]));
  170. }
  171. }
  172. static int tst_sv2_lib_init_fn (void *conn)
  173. {
  174. return (0);
  175. }
  176. static int tst_sv2_lib_exit_fn (void *conn)
  177. {
  178. return (0);
  179. }
  180. static void tst_sv2_sync_init_v2 (
  181. const unsigned int *member_list,
  182. size_t member_list_entries,
  183. const struct memb_ring_id *ring_id)
  184. {
  185. unsigned int lowest_nodeid = 0xffffffff;
  186. int i, j;
  187. int found;
  188. num_sync_processes = 0;
  189. memcpy (my_member_list, member_list, member_list_entries *
  190. sizeof (unsigned int));
  191. my_member_list_entries = member_list_entries;
  192. for (i = 0; i < my_member_list_entries; i++) {
  193. if (my_member_list[i] < lowest_nodeid) {
  194. lowest_nodeid = my_member_list[i];
  195. }
  196. }
  197. log_printf (LOGSYS_LEVEL_INFO,
  198. "tst_sv2_sync_init_v2 %s",
  199. api->totem_ifaces_print (lowest_nodeid));
  200. /* look for new (joined) nodes */
  201. for (j = 0; j < member_list_entries; j++) {
  202. found = 0;
  203. for (i = 0; i < my_old_member_list_entries; i++) {
  204. if (my_old_member_list[i] == member_list[j]) {
  205. found = 1;
  206. break;
  207. }
  208. }
  209. if (found == 0) {
  210. log_printf (LOGSYS_LEVEL_INFO,
  211. "sync: node joined %s",
  212. api->totem_ifaces_print (member_list[j]));
  213. }
  214. }
  215. /* look for old (left) nodes */
  216. for (i = 0; i < my_old_member_list_entries; i++) {
  217. found = 0;
  218. for (j = 0; j < member_list_entries; j++) {
  219. if (my_old_member_list[i] == member_list[j]) {
  220. found = 1;
  221. break;
  222. }
  223. }
  224. if (found == 0) {
  225. log_printf (LOGSYS_LEVEL_INFO,
  226. "sync: node left %s",
  227. api->totem_ifaces_print (my_old_member_list[i]));
  228. }
  229. }
  230. }
  231. static int tst_sv2_sync_process (void)
  232. {
  233. num_sync_processes++;
  234. if (num_sync_processes > 3) {
  235. return 0;
  236. } else {
  237. return 1;
  238. }
  239. }
  240. static void tst_sv2_sync_activate (void)
  241. {
  242. memcpy (my_old_member_list, my_member_list,
  243. my_member_list_entries * sizeof (unsigned int));
  244. my_old_member_list_entries = my_member_list_entries;
  245. if (num_sync_processes <= 3) {
  246. log_printf (LOGSYS_LEVEL_ERROR,
  247. "sync: activate called before process is done %d",
  248. num_sync_processes);
  249. } else {
  250. log_printf (LOGSYS_LEVEL_INFO,
  251. "sync: activate correctly %d",
  252. num_sync_processes);
  253. }
  254. num_sync_processes = 0;
  255. }
  256. static void tst_sv2_sync_abort (void)
  257. {
  258. log_printf (LOGSYS_LEVEL_INFO, "sync: abort");
  259. }