syncv2.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <corosync/coroipc_types.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. #include "../exec/tlist.h"
  61. LOGSYS_DECLARE_SUBSYS ("TST2");
  62. /*
  63. * Service Interfaces required by service_message_handler struct
  64. */
  65. static int tst_sv2_exec_init_fn (
  66. struct corosync_api_v1 *corosync_api);
  67. static void tst_sv2_confchg_fn (
  68. enum totem_configuration_type configuration_type,
  69. const unsigned int *member_list, size_t member_list_entries,
  70. const unsigned int *left_list, size_t left_list_entries,
  71. const unsigned int *joined_list, size_t joined_list_entries,
  72. const struct memb_ring_id *ring_id);
  73. static int tst_sv2_lib_init_fn (void *conn);
  74. static int tst_sv2_lib_exit_fn (void *conn);
  75. static struct corosync_api_v1 *api;
  76. static void tst_sv2_sync_init_v2 (
  77. const unsigned int *member_list,
  78. size_t member_list_entries,
  79. const struct memb_ring_id *ring_id);
  80. static int tst_sv2_sync_process (void);
  81. static void tst_sv2_sync_activate (void);
  82. static void tst_sv2_sync_abort (void);
  83. struct corosync_service_engine tst_sv2_service_engine = {
  84. .name = "corosync test synv2 service",
  85. .id = TST_SV2_SERVICE,
  86. .priority = 1,
  87. .private_data_size = 0,
  88. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  89. .lib_init_fn = tst_sv2_lib_init_fn,
  90. .lib_exit_fn = tst_sv2_lib_exit_fn,
  91. .lib_engine = NULL,
  92. .lib_engine_count = 0,
  93. .exec_engine = NULL,
  94. .exec_engine_count = 0,
  95. .confchg_fn = tst_sv2_confchg_fn,
  96. .exec_init_fn = tst_sv2_exec_init_fn,
  97. .exec_dump_fn = NULL,
  98. .sync_mode = CS_SYNC_V2,
  99. .sync_init = tst_sv2_sync_init_v2,
  100. .sync_process = tst_sv2_sync_process,
  101. .sync_activate = tst_sv2_sync_activate,
  102. .sync_abort = tst_sv2_sync_abort
  103. };
  104. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  105. static unsigned int my_member_list_entries;
  106. static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
  107. static unsigned int my_old_member_list_entries = 0;
  108. static int num_sync_processes = 0;
  109. static DECLARE_LIST_INIT (confchg_notify);
  110. /*
  111. * Dynamic loading descriptor
  112. */
  113. static struct corosync_service_engine *tst_sv2_get_service_engine_ver0 (void);
  114. static struct corosync_service_engine_iface_ver0 tst_sv2_service_engine_iface = {
  115. .corosync_get_service_engine_ver0 = tst_sv2_get_service_engine_ver0
  116. };
  117. static struct lcr_iface corosync_tst_sv2_ver0[1] = {
  118. {
  119. .name = "corosync_tst_sv2",
  120. .version = 0,
  121. .versions_replace = 0,
  122. .versions_replace_count = 0,
  123. .dependencies = 0,
  124. .dependency_count = 0,
  125. .constructor = NULL,
  126. .destructor = NULL,
  127. .interfaces = NULL,
  128. }
  129. };
  130. static struct lcr_comp tst_sv2_comp_ver0 = {
  131. .iface_count = 1,
  132. .ifaces = corosync_tst_sv2_ver0
  133. };
  134. static struct corosync_service_engine *tst_sv2_get_service_engine_ver0 (void)
  135. {
  136. return (&tst_sv2_service_engine);
  137. }
  138. #ifdef COROSYNC_SOLARIS
  139. void corosync_lcr_component_register (void);
  140. void corosync_lcr_component_register (void) {
  141. #else
  142. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  143. #endif
  144. lcr_interfaces_set (&corosync_tst_sv2_ver0[0], &tst_sv2_service_engine_iface);
  145. lcr_component_register (&tst_sv2_comp_ver0);
  146. }
  147. static int tst_sv2_exec_init_fn (
  148. struct corosync_api_v1 *corosync_api)
  149. {
  150. #ifdef COROSYNC_SOLARIS
  151. logsys_subsys_init();
  152. #endif
  153. api = corosync_api;
  154. return 0;
  155. }
  156. static void tst_sv2_confchg_fn (
  157. enum totem_configuration_type configuration_type,
  158. const unsigned int *member_list, size_t member_list_entries,
  159. const unsigned int *left_list, size_t left_list_entries,
  160. const unsigned int *joined_list, size_t joined_list_entries,
  161. const struct memb_ring_id *ring_id)
  162. {
  163. int j;
  164. for (j = 0; j < left_list_entries; j++) {
  165. log_printf (LOGSYS_LEVEL_INFO,
  166. "Member left: %s", api->totem_ifaces_print (left_list[j]));
  167. }
  168. for (j = 0; j < joined_list_entries; j++) {
  169. log_printf (LOGSYS_LEVEL_INFO,
  170. "Member joined: %s", api->totem_ifaces_print (joined_list[j]));
  171. }
  172. }
  173. static int tst_sv2_lib_init_fn (void *conn)
  174. {
  175. return (0);
  176. }
  177. static int tst_sv2_lib_exit_fn (void *conn)
  178. {
  179. return (0);
  180. }
  181. static void tst_sv2_sync_init_v2 (
  182. const unsigned int *member_list,
  183. size_t member_list_entries,
  184. const struct memb_ring_id *ring_id)
  185. {
  186. unsigned int lowest_nodeid = 0xffffffff;
  187. int i, j;
  188. int found;
  189. num_sync_processes = 0;
  190. memcpy (my_member_list, member_list, member_list_entries *
  191. sizeof (unsigned int));
  192. my_member_list_entries = member_list_entries;
  193. for (i = 0; i < my_member_list_entries; i++) {
  194. if (my_member_list[i] < lowest_nodeid) {
  195. lowest_nodeid = my_member_list[i];
  196. }
  197. }
  198. log_printf (LOGSYS_LEVEL_INFO,
  199. "tst_sv2_sync_init_v2 %s",
  200. api->totem_ifaces_print (lowest_nodeid));
  201. /* look for new (joined) nodes */
  202. for (j = 0; j < member_list_entries; j++) {
  203. found = 0;
  204. for (i = 0; i < my_old_member_list_entries; i++) {
  205. if (my_old_member_list[i] == member_list[j]) {
  206. found = 1;
  207. break;
  208. }
  209. }
  210. if (found == 0) {
  211. log_printf (LOGSYS_LEVEL_INFO,
  212. "sync: node joined %s",
  213. api->totem_ifaces_print (member_list[j]));
  214. }
  215. }
  216. /* look for old (left) nodes */
  217. for (i = 0; i < my_old_member_list_entries; i++) {
  218. found = 0;
  219. for (j = 0; j < member_list_entries; j++) {
  220. if (my_old_member_list[i] == member_list[j]) {
  221. found = 1;
  222. break;
  223. }
  224. }
  225. if (found == 0) {
  226. log_printf (LOGSYS_LEVEL_INFO,
  227. "sync: node left %s",
  228. api->totem_ifaces_print (my_old_member_list[i]));
  229. }
  230. }
  231. }
  232. static int tst_sv2_sync_process (void)
  233. {
  234. num_sync_processes++;
  235. if (num_sync_processes > 3) {
  236. return 0;
  237. } else {
  238. return 1;
  239. }
  240. }
  241. static void tst_sv2_sync_activate (void)
  242. {
  243. memcpy (my_old_member_list, my_member_list,
  244. my_member_list_entries * sizeof (unsigned int));
  245. my_old_member_list_entries = my_member_list_entries;
  246. if (num_sync_processes <= 3) {
  247. log_printf (LOGSYS_LEVEL_ERROR,
  248. "sync: activate called before process is done %d",
  249. num_sync_processes);
  250. } else {
  251. log_printf (LOGSYS_LEVEL_INFO,
  252. "sync: activate correctly %d",
  253. num_sync_processes);
  254. }
  255. num_sync_processes = 0;
  256. }
  257. static void tst_sv2_sync_abort (void)
  258. {
  259. log_printf (LOGSYS_LEVEL_INFO, "sync: abort");
  260. }