sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (c) 2005-2006 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.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 <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <sys/un.h>
  37. #include <sys/ioctl.h>
  38. #include <netinet/in.h>
  39. #include <sys/uio.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <errno.h>
  45. #include <signal.h>
  46. #include <time.h>
  47. #include <unistd.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include "../include/saAis.h"
  51. #include "main.h"
  52. #include "sync.h"
  53. #include "totempg.h"
  54. #include "totemip.h"
  55. #include "totem.h"
  56. #include "vsf.h"
  57. #include "swab.h"
  58. #include "../lcr/lcr_ifact.h"
  59. #define LOG_SERVICE LOG_SERVICE_SYNC
  60. #include "print.h"
  61. #define MESSAGE_REQ_SYNC_BARRIER 0
  62. struct barrier_data {
  63. struct totem_ip_address addr;
  64. int completed;
  65. };
  66. static struct memb_ring_id *sync_ring_id;
  67. static int vsf_none = 0;
  68. static int (*sync_callbacks_retrieve) (int sync_id, struct sync_callbacks *callack);
  69. static struct sync_callbacks sync_callbacks;
  70. static int sync_processing = 0;
  71. static int sync_callback_loaded = 0;
  72. static void (*sync_synchronization_completed) (void);
  73. static int sync_recovery_index = 0;
  74. static void *sync_callback_token_handle = 0;
  75. static struct barrier_data barrier_data_confchg[PROCESSOR_COUNT_MAX];
  76. static int barrier_data_confchg_entries;
  77. static struct barrier_data barrier_data_process[PROCESSOR_COUNT_MAX];
  78. static struct openais_vsf_iface_ver0 *vsf_iface;
  79. static int sync_barrier_send (struct memb_ring_id *ring_id);
  80. static int sync_start_process (enum totem_callback_token_type type, void *data);
  81. static void sync_service_init (struct memb_ring_id *ring_id);
  82. static int sync_service_process (enum totem_callback_token_type type, void *data);
  83. static void sync_deliver_fn (
  84. struct totem_ip_address *source_addr,
  85. struct iovec *iovec,
  86. int iov_len,
  87. int endian_conversion_required);
  88. static void sync_confchg_fn (
  89. enum totem_configuration_type configuration_type,
  90. struct totem_ip_address *member_list, int member_list_entries,
  91. struct totem_ip_address *left_list, int left_list_entries,
  92. struct totem_ip_address *joined_list, int joined_list_entries,
  93. struct memb_ring_id *ring_id);
  94. static void sync_primary_callback_fn (
  95. struct totem_ip_address *view_list,
  96. int view_list_entries,
  97. int primary_designated,
  98. struct memb_ring_id *ring_id);
  99. static struct totempg_group sync_group = {
  100. .group = "sync",
  101. .group_len = 4
  102. };
  103. static totempg_groups_handle sync_group_handle;
  104. struct req_exec_sync_barrier_start {
  105. struct req_header header;
  106. struct memb_ring_id ring_id;
  107. };
  108. /*
  109. * Send a barrier data structure
  110. */
  111. static int sync_barrier_send (struct memb_ring_id *ring_id)
  112. {
  113. struct req_exec_sync_barrier_start req_exec_sync_barrier_start;
  114. struct iovec iovec;
  115. int res;
  116. req_exec_sync_barrier_start.header.size = sizeof (struct req_exec_sync_barrier_start);
  117. req_exec_sync_barrier_start.header.id = MESSAGE_REQ_SYNC_BARRIER;
  118. memcpy (&req_exec_sync_barrier_start.ring_id, ring_id,
  119. sizeof (struct memb_ring_id));
  120. iovec.iov_base = (char *)&req_exec_sync_barrier_start;
  121. iovec.iov_len = sizeof (req_exec_sync_barrier_start);
  122. res = totempg_groups_mcast_joined (sync_group_handle, &iovec, 1, TOTEMPG_AGREED);
  123. return (res);
  124. }
  125. void sync_start_init (struct memb_ring_id *ring_id)
  126. {
  127. totempg_callback_token_create (
  128. &sync_callback_token_handle,
  129. TOTEM_CALLBACK_TOKEN_SENT,
  130. 0, /* don't delete after callback */
  131. sync_start_process,
  132. (void *)ring_id);
  133. }
  134. static void sync_service_init (struct memb_ring_id *ring_id)
  135. {
  136. sync_callbacks.sync_init ();
  137. totempg_callback_token_destroy (&sync_callback_token_handle);
  138. /*
  139. * Create the token callback for the processing
  140. */
  141. totempg_callback_token_create (
  142. &sync_callback_token_handle,
  143. TOTEM_CALLBACK_TOKEN_SENT,
  144. 0, /* don't delete after callback */
  145. sync_service_process,
  146. (void *)ring_id);
  147. }
  148. static int sync_start_process (enum totem_callback_token_type type, void *data)
  149. {
  150. int res;
  151. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  152. res = sync_barrier_send (ring_id);
  153. if (res == 0) {
  154. /*
  155. * Delete the token callback for the barrier
  156. */
  157. totempg_callback_token_destroy (&sync_callback_token_handle);
  158. }
  159. return (0);
  160. }
  161. void sync_callbacks_load (void)
  162. {
  163. int res;
  164. // TODO rewrite this to get rid of the for (;;)
  165. for (;;) {
  166. res = sync_callbacks_retrieve (sync_recovery_index, &sync_callbacks);
  167. /*
  168. * No more service handlers have sync callbacks at this tie
  169. ` */
  170. if (res == -1) {
  171. sync_processing = 0;
  172. break;
  173. }
  174. sync_recovery_index += 1;
  175. if (sync_callbacks.sync_init != NULL) {
  176. sync_callback_loaded = 1;
  177. break;
  178. }
  179. }
  180. }
  181. static int sync_service_process (enum totem_callback_token_type type, void *data)
  182. {
  183. int res;
  184. struct memb_ring_id *ring_id = (struct memb_ring_id *)data;
  185. /*
  186. * If process operation not from this ring id, then ignore it and stop
  187. * processing
  188. */
  189. if (memcmp (ring_id, sync_ring_id, sizeof (struct memb_ring_id)) != 0) {
  190. return (0);
  191. }
  192. /*
  193. * If process returns 0, then its time to activate
  194. * and start the next service's synchronization
  195. */
  196. res = sync_callbacks.sync_process ();
  197. if (res != 0) {
  198. return (0);
  199. }
  200. /*
  201. * This sync is complete so activate and start next service sync
  202. */
  203. sync_callbacks.sync_activate ();
  204. totempg_callback_token_destroy (&sync_callback_token_handle);
  205. log_printf (LOG_LEVEL_NOTICE,"Synchronization actions done for (%s)\n",sync_callbacks.name);
  206. sync_callbacks_load();
  207. /*
  208. * if sync service found, execute it
  209. */
  210. if (sync_processing && sync_callbacks.sync_init) {
  211. sync_start_init (ring_id);
  212. }
  213. else {
  214. sync_callback_loaded = 0;
  215. }
  216. return (0);
  217. }
  218. int sync_register (
  219. int (*callbacks_retrieve) (int sync_id, struct sync_callbacks *callack),
  220. void (*synchronization_completed) (void),
  221. char *vsf_type)
  222. {
  223. unsigned int res;
  224. unsigned int vsf_handle;
  225. void *vsf_iface_p;
  226. char openais_vsf_type[1024];
  227. log_init ("SYNC");
  228. res = totempg_groups_initialize (
  229. &sync_group_handle,
  230. sync_deliver_fn,
  231. sync_confchg_fn);
  232. if (res == -1) {
  233. log_printf (LOG_LEVEL_ERROR,
  234. "Couldn't initialize groups interface.\n");
  235. return (-1);
  236. }
  237. res = totempg_groups_join (
  238. sync_group_handle,
  239. &sync_group,
  240. 1);
  241. if (res == -1) {
  242. log_printf (LOG_LEVEL_ERROR, "Couldn't join group.\n");
  243. return (-1);
  244. }
  245. if (strcmp (vsf_type, "none") == 0) {
  246. log_printf (LOG_LEVEL_NOTICE,
  247. "Not using a virtual synchrony filter.\n");
  248. vsf_none = 1;
  249. } else {
  250. vsf_none = 0;
  251. sprintf (openais_vsf_type, "openais_vsf_%s", vsf_type);
  252. res = lcr_ifact_reference (
  253. &vsf_handle,
  254. openais_vsf_type,
  255. 0,
  256. &vsf_iface_p,
  257. 0);
  258. if (res == -1) {
  259. log_printf (LOG_LEVEL_NOTICE,
  260. "Couldn't load virtual synchrony filter %s\n",
  261. vsf_type);
  262. return (-1);
  263. }
  264. log_printf (LOG_LEVEL_NOTICE,
  265. "Using virtual synchrony filter %s\n", openais_vsf_type);
  266. vsf_iface = (struct openais_vsf_iface_ver0 *)vsf_iface_p;
  267. vsf_iface->init (sync_primary_callback_fn);
  268. }
  269. sync_callbacks_retrieve = callbacks_retrieve;
  270. sync_synchronization_completed = synchronization_completed;
  271. return (0);
  272. }
  273. static void sync_primary_callback_fn (
  274. struct totem_ip_address *view_list,
  275. int view_list_entries,
  276. int primary_designated,
  277. struct memb_ring_id *ring_id)
  278. {
  279. int i;
  280. if (primary_designated) {
  281. log_printf (LOG_LEVEL_NOTICE, "This node is within the primary component and will provide service.\n");
  282. } else {
  283. log_printf (LOG_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.\n");
  284. return;
  285. }
  286. /*
  287. * Execute configuration change for synchronization service
  288. */
  289. sync_processing = 1;
  290. totempg_callback_token_destroy (&sync_callback_token_handle);
  291. sync_recovery_index = 0;
  292. sync_callback_loaded = 0;
  293. memset (&barrier_data_confchg, 0, sizeof (barrier_data_confchg));
  294. for (i = 0; i < view_list_entries; i++) {
  295. totemip_copy(&barrier_data_confchg[i].addr, &view_list[i]);
  296. barrier_data_confchg[i].completed = 0;
  297. }
  298. memcpy (barrier_data_process, barrier_data_confchg,
  299. sizeof (barrier_data_confchg));
  300. barrier_data_confchg_entries = view_list_entries;
  301. sync_start_init (sync_ring_id);
  302. }
  303. static struct memb_ring_id deliver_ring_id;
  304. void sync_endian_convert (struct req_exec_sync_barrier_start *req_exec_sync_barrier_start)
  305. {
  306. totemip_copy_endian_convert(&req_exec_sync_barrier_start->ring_id.rep,
  307. &req_exec_sync_barrier_start->ring_id.rep);
  308. req_exec_sync_barrier_start->ring_id.seq = swab64 (req_exec_sync_barrier_start->ring_id.seq);
  309. }
  310. static void sync_deliver_fn (
  311. struct totem_ip_address *source_addr,
  312. struct iovec *iovec,
  313. int iov_len,
  314. int endian_conversion_required)
  315. {
  316. struct req_exec_sync_barrier_start *req_exec_sync_barrier_start =
  317. (struct req_exec_sync_barrier_start *)iovec[0].iov_base;
  318. int i;
  319. if (endian_conversion_required) {
  320. sync_endian_convert (req_exec_sync_barrier_start);
  321. }
  322. int barrier_completed = 1;
  323. memcpy (&deliver_ring_id, &req_exec_sync_barrier_start->ring_id,
  324. sizeof (struct memb_ring_id));
  325. /*
  326. * Is this barrier from this configuration, if not, ignore it
  327. */
  328. if (memcmp (&req_exec_sync_barrier_start->ring_id, sync_ring_id,
  329. sizeof (struct memb_ring_id)) != 0) {
  330. return;
  331. }
  332. /*
  333. * Set completion for source_addr's address
  334. */
  335. for (i = 0; i < barrier_data_confchg_entries; i++) {
  336. if (totemip_equal(source_addr, &barrier_data_process[i].addr)) {
  337. barrier_data_process[i].completed = 1;
  338. log_printf (LOG_LEVEL_DEBUG,"Barrier Start Recieved From %s\n", totemip_print(&barrier_data_process[i].addr));
  339. break;
  340. }
  341. }
  342. /*
  343. * Test if barrier is complete
  344. */
  345. for (i = 0; i < barrier_data_confchg_entries; i++) {
  346. log_printf (LOG_LEVEL_DEBUG,"Barrier completion status for %s = %d. \n",
  347. totemip_print(&barrier_data_process[i].addr),
  348. barrier_data_process[i].completed);
  349. if (barrier_data_process[i].completed == 0) {
  350. barrier_completed = 0;
  351. }
  352. }
  353. if (barrier_completed) {
  354. log_printf (LOG_LEVEL_NOTICE,
  355. "Synchronization barrier completed\n");
  356. }
  357. /*
  358. * Start synchronization if the barrier has completed
  359. */
  360. if (barrier_completed) {
  361. memcpy (barrier_data_process, barrier_data_confchg,
  362. sizeof (barrier_data_confchg));
  363. if ( sync_callback_loaded == 0 ) {
  364. sync_callbacks_load();
  365. }
  366. /*
  367. * if sync service found, execute it
  368. */
  369. if (sync_processing && sync_callbacks.sync_init) {
  370. log_printf (LOG_LEVEL_NOTICE,"Synchronization actions starting for (%s)\n",sync_callbacks.name);
  371. sync_service_init (&deliver_ring_id);
  372. }
  373. }
  374. return;
  375. }
  376. static void sync_confchg_fn (
  377. enum totem_configuration_type configuration_type,
  378. struct totem_ip_address *member_list, int member_list_entries,
  379. struct totem_ip_address *left_list, int left_list_entries,
  380. struct totem_ip_address *joined_list, int joined_list_entries,
  381. struct memb_ring_id *ring_id)
  382. {
  383. sync_ring_id = ring_id;
  384. /*
  385. * If no virtual synchrony filter configured, then start
  386. * synchronization process
  387. */
  388. if (vsf_none == 1) {
  389. sync_primary_callback_fn (
  390. member_list,
  391. member_list_entries,
  392. 1,
  393. ring_id);
  394. }
  395. }
  396. int sync_in_process (void)
  397. {
  398. return (sync_processing);
  399. }
  400. int sync_primary_designated (void)
  401. {
  402. if (vsf_none == 1) {
  403. return (1);
  404. } else {
  405. return (vsf_iface->primary());
  406. }
  407. }