evil.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Don't look, you will be disappointed
  37. */
  38. #include <config.h>
  39. #include <pthread.h>
  40. #include <assert.h>
  41. #include <sys/types.h>
  42. #include <sys/poll.h>
  43. #include <sys/uio.h>
  44. #include <sys/mman.h>
  45. #include <sys/socket.h>
  46. #include <sys/un.h>
  47. #include <sys/time.h>
  48. #include <sys/resource.h>
  49. #include <sys/stat.h>
  50. #include <sys/sysinfo.h>
  51. #include <netinet/in.h>
  52. #include <arpa/inet.h>
  53. #include <unistd.h>
  54. #include <fcntl.h>
  55. #include <stdlib.h>
  56. #include <stdio.h>
  57. #include <errno.h>
  58. #include <signal.h>
  59. #include <sched.h>
  60. #include <time.h>
  61. #include <corosync/totem/totempg.h>
  62. #include <corosync/swab.h>
  63. #include <corosync/corotypes.h>
  64. #include <corosync/coroipc_types.h>
  65. #include <corosync/corodefs.h>
  66. #include <corosync/list.h>
  67. #include <corosync/lcr/lcr_ifact.h>
  68. #include <corosync/engine/objdb.h>
  69. #include <corosync/engine/config.h>
  70. #include <corosync/engine/coroapi.h>
  71. #include <corosync/engine/logsys.h>
  72. #include <corosync/coroipcs.h>
  73. #include "sync.h"
  74. #include "evil.h"
  75. static void clm_sync_init (
  76. const unsigned int *member_list,
  77. size_t member_list_entries,
  78. const struct memb_ring_id *ring_id);
  79. static int clm_sync_process (void);
  80. static void clm_sync_activate (void);
  81. static void clm_sync_abort (void);
  82. static void evt_sync_init (
  83. const unsigned int *member_list,
  84. size_t member_list_entries,
  85. const struct memb_ring_id *ring_id);
  86. static int evt_sync_process (void);
  87. static void evt_sync_activate (void);
  88. static void evt_sync_abort (void);
  89. static int clm_hack_init (void);
  90. static struct sync_callbacks clm_sync_operations = {
  91. .name = "dummy CLM service",
  92. .sync_init = clm_sync_init,
  93. .sync_process = clm_sync_process,
  94. .sync_activate = clm_sync_activate,
  95. .sync_abort = clm_sync_abort,
  96. };
  97. static struct sync_callbacks evt_sync_operations = {
  98. .name = "dummy EVT service",
  99. .sync_init = evt_sync_init,
  100. .sync_process = evt_sync_process,
  101. .sync_activate = evt_sync_activate,
  102. .sync_abort = evt_sync_abort,
  103. };
  104. static struct corosync_api_v1 *api = NULL;
  105. static void sync_dummy_init (
  106. const unsigned int *member_list,
  107. size_t member_list_entries,
  108. const struct memb_ring_id *ring_id)
  109. {
  110. }
  111. static int sync_dummy_process (void)
  112. {
  113. return (0);
  114. }
  115. static void sync_dummy_activate (void)
  116. {
  117. }
  118. static void sync_dummy_abort (void)
  119. {
  120. }
  121. void evil_init (struct corosync_api_v1 *api_in)
  122. {
  123. api = api_in;
  124. clm_hack_init ();
  125. }
  126. extern int evil_callbacks_load (int sync_id,
  127. struct sync_callbacks *callbacks)
  128. {
  129. int sync_dummy_found = 1;
  130. int callbacks_init = 1;
  131. memset (callbacks, 0, sizeof (struct sync_callbacks));
  132. switch (sync_id) {
  133. case EVS_SERVICE:
  134. callbacks_init = 0;
  135. break;
  136. case CLM_SERVICE:
  137. /*
  138. * ugh
  139. */
  140. memcpy (callbacks, &clm_sync_operations, sizeof (struct sync_callbacks));
  141. callbacks_init = 0;
  142. break;
  143. case AMF_SERVICE:
  144. callbacks->name = "dummy AMF service";
  145. break;
  146. case CKPT_SERVICE:
  147. callbacks->name = "dummy CKPT service";
  148. break;
  149. case EVT_SERVICE:
  150. memcpy (callbacks, &evt_sync_operations, sizeof (struct sync_callbacks));
  151. callbacks_init = 0;
  152. break;
  153. case LCK_SERVICE:
  154. callbacks_init = 0;
  155. break;
  156. case MSG_SERVICE:
  157. callbacks_init = 0;
  158. break;
  159. case CFG_SERVICE:
  160. callbacks_init = 0;
  161. break;
  162. case CPG_SERVICE:
  163. callbacks->name = "dummy CPG service";
  164. break;
  165. case CONFDB_SERVICE:
  166. callbacks_init = 0;
  167. break;
  168. default:
  169. callbacks_init = 0;
  170. sync_dummy_found = 0;
  171. break;
  172. }
  173. if (callbacks_init) {
  174. callbacks->sync_init = sync_dummy_init;
  175. callbacks->sync_process = sync_dummy_process;
  176. callbacks->sync_activate = sync_dummy_activate;
  177. callbacks->sync_abort = sync_dummy_abort;
  178. }
  179. if (sync_dummy_found == 0) {
  180. return (-1);
  181. }
  182. return (0);
  183. }
  184. /*
  185. * This sends the clm nodejoin message required by clm services
  186. * on whitetank as well as the event service
  187. */
  188. enum clm_message_req_types {
  189. MESSAGE_REQ_EXEC_CLM_NODEJOIN = 0
  190. };
  191. #define MAR_CLM_MAX_ADDRESS_LENGTH 64
  192. #define SA_MAX_NAME_LENGTH 256
  193. typedef struct {
  194. int size __attribute__((aligned(8)));
  195. int id __attribute__((aligned(8)));
  196. } mar_req_header_t __attribute__((aligned(8)));
  197. typedef enum {
  198. MAR_CLM_AF_INET = 1,
  199. MAR_CLM_AF_INET6 = 2
  200. } mar_clm_node_address_family_t;
  201. typedef struct {
  202. unsigned short length __attribute__((aligned(8)));
  203. mar_clm_node_address_family_t family __attribute__((aligned(8)));
  204. unsigned char value[MAR_CLM_MAX_ADDRESS_LENGTH] __attribute__((aligned(8)));
  205. } mar_clm_node_address_t;
  206. typedef uint8_t mar_uint8_t;
  207. typedef uint16_t mar_uint16_t;
  208. typedef uint32_t mar_uint32_t;
  209. typedef uint64_t mar_uint64_t;
  210. typedef struct {
  211. mar_uint16_t length __attribute__((aligned(8)));
  212. mar_uint8_t value[SA_MAX_NAME_LENGTH] __attribute__((aligned(8)));
  213. } mar_name_t;
  214. typedef struct {
  215. mar_uint32_t node_id __attribute__((aligned(8)));
  216. mar_clm_node_address_t node_address __attribute__((aligned(8)));
  217. mar_name_t node_name __attribute__((aligned(8)));
  218. mar_uint32_t member __attribute__((aligned(8)));
  219. mar_uint64_t boot_timestamp __attribute__((aligned(8)));
  220. mar_uint64_t initial_view_number __attribute__((aligned(8)));
  221. } mar_clm_cluster_node_t;
  222. mar_clm_cluster_node_t my_cluster_node;
  223. struct req_exec_clm_nodejoin {
  224. mar_req_header_t header __attribute__((aligned(8)));
  225. mar_clm_cluster_node_t cluster_node __attribute__((aligned(8)));
  226. };
  227. static void my_cluster_node_load (void)
  228. {
  229. struct totem_ip_address interfaces[INTERFACE_MAX];
  230. unsigned int iface_count;
  231. char **status;
  232. const char *iface_string;
  233. totempg_ifaces_get (
  234. totempg_my_nodeid_get (),
  235. interfaces,
  236. &status,
  237. &iface_count);
  238. iface_string = totemip_print (&interfaces[0]);
  239. sprintf ((char *)my_cluster_node.node_address.value, "%s",
  240. iface_string);
  241. my_cluster_node.node_address.length =
  242. strlen ((char *)my_cluster_node.node_address.value);
  243. if (totempg_my_family_get () == AF_INET) {
  244. my_cluster_node.node_address.family = MAR_CLM_AF_INET;
  245. } else
  246. if (totempg_my_family_get () == AF_INET6) {
  247. my_cluster_node.node_address.family = MAR_CLM_AF_INET6;
  248. } else {
  249. assert (0);
  250. }
  251. strcpy ((char *)my_cluster_node.node_name.value,
  252. (char *)my_cluster_node.node_address.value);
  253. my_cluster_node.node_name.length =
  254. my_cluster_node.node_address.length;
  255. my_cluster_node.node_id = totempg_my_nodeid_get ();
  256. my_cluster_node.member = 1;
  257. }
  258. static int clm_hack_init (void)
  259. {
  260. #if defined(COROSYNC_LINUX)
  261. struct sysinfo s_info;
  262. time_t current_time;
  263. sysinfo (&s_info);
  264. current_time = time (NULL);
  265. /* (currenttime (s) - uptime (s)) * 1 billion (ns) / 1 (s) */
  266. my_cluster_node.boot_timestamp = ((uint64_t)(current_time - s_info.uptime)) * 1000000000ULL;
  267. #elif defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  268. int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  269. struct timeval boot_time;
  270. size_t size = sizeof(boot_time);
  271. if ( sysctl(mib, 2, &boot_time, &size, NULL, 0) == -1 )
  272. boot_time.tv_sec = time (NULL);
  273. /* (currenttime (s) - uptime (s)) * 1 billion (ns) / 1 (s) */
  274. my_cluster_node.boot_timestamp = ((uint64_t)boot_time.tv_sec) * 1000000000ULL;
  275. #else /* defined(CTL_KERN) && defined(KERN_BOOTTIME) */
  276. #warning "no bootime support"
  277. #endif
  278. return (0);
  279. }
  280. static int clm_nodejoin_send (void)
  281. {
  282. struct req_exec_clm_nodejoin req_exec_clm_nodejoin;
  283. struct iovec req_exec_clm_iovec;
  284. int result;
  285. my_cluster_node_load ();
  286. req_exec_clm_nodejoin.header.size = sizeof (struct req_exec_clm_nodejoin);
  287. req_exec_clm_nodejoin.header.id =
  288. SERVICE_ID_MAKE (CLM_SERVICE, MESSAGE_REQ_EXEC_CLM_NODEJOIN);
  289. my_cluster_node.initial_view_number = 0;
  290. memcpy (&req_exec_clm_nodejoin.cluster_node, &my_cluster_node,
  291. sizeof (mar_clm_cluster_node_t));
  292. req_exec_clm_iovec.iov_base = (char *)&req_exec_clm_nodejoin;
  293. req_exec_clm_iovec.iov_len = sizeof (req_exec_clm_nodejoin);
  294. result = api->totem_mcast (&req_exec_clm_iovec, 1, TOTEMPG_AGREED);
  295. return (result);
  296. }
  297. /*
  298. * This is a noop for this service
  299. */
  300. static void clm_sync_init (
  301. const unsigned int *member_list,
  302. size_t member_list_entries,
  303. const struct memb_ring_id *ring_id)
  304. {
  305. return;
  306. }
  307. static int clm_sync_process (void)
  308. {
  309. /*
  310. * Send node information to other nodes
  311. */
  312. return (clm_nodejoin_send ());
  313. }
  314. /*
  315. * This is a noop for this service
  316. */
  317. static void clm_sync_activate (void)
  318. {
  319. return;
  320. }
  321. /*
  322. * This is a noop for this service
  323. */
  324. static void clm_sync_abort (void)
  325. {
  326. return;
  327. }
  328. enum evt_sync_states {
  329. EVT_SYNC_PART_ONE,
  330. EVT_SYNC_PART_TWO
  331. };
  332. static enum evt_sync_states evt_sync_state;
  333. enum evt_chan_ops {
  334. EVT_OPEN_CHAN_OP, /* chc_chan */
  335. EVT_CLOSE_CHAN_OP, /* chc_close_unlink_chan */
  336. EVT_UNLINK_CHAN_OP, /* chc_close_unlink_chan */
  337. EVT_CLEAR_RET_OP, /* chc_event_id */
  338. EVT_SET_ID_OP, /* chc_set_id */
  339. EVT_CONF_DONE, /* no data used */
  340. EVT_OPEN_COUNT, /* chc_set_opens */
  341. EVT_OPEN_COUNT_DONE /* no data used */
  342. };
  343. enum evt_message_req_types {
  344. MESSAGE_REQ_EXEC_EVT_EVENTDATA = 0,
  345. MESSAGE_REQ_EXEC_EVT_CHANCMD = 1,
  346. MESSAGE_REQ_EXEC_EVT_RECOVERY_EVENTDATA = 2
  347. };
  348. struct req_evt_chan_command {
  349. mar_req_header_t chc_head __attribute__((aligned(8)));
  350. mar_uint32_t chc_op __attribute__((aligned(8)));
  351. };
  352. static void evt_sync_init (
  353. const unsigned int *member_list,
  354. size_t member_list_entries,
  355. const struct memb_ring_id *ring_id)
  356. {
  357. evt_sync_state = EVT_SYNC_PART_ONE;
  358. return;
  359. }
  360. static int evt_sync_process (void)
  361. {
  362. int res;
  363. struct req_evt_chan_command cpkt;
  364. struct iovec chn_iovec;
  365. memset(&cpkt, 0, sizeof(cpkt));
  366. cpkt.chc_head.id =
  367. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  368. cpkt.chc_head.size = sizeof(cpkt);
  369. chn_iovec.iov_base = &cpkt;
  370. chn_iovec.iov_len = cpkt.chc_head.size;
  371. if (evt_sync_state == EVT_SYNC_PART_ONE) {
  372. cpkt.chc_op = EVT_OPEN_COUNT_DONE;
  373. res = api->totem_mcast(&chn_iovec, 1,TOTEMPG_AGREED);
  374. if (res == -1) {
  375. return (res);
  376. }
  377. evt_sync_state = EVT_SYNC_PART_TWO;
  378. }
  379. if (evt_sync_state == EVT_SYNC_PART_TWO) {
  380. cpkt.chc_op = EVT_CONF_DONE;
  381. res = api->totem_mcast(&chn_iovec, 1,TOTEMPG_AGREED);
  382. if (res == -1) {
  383. return (res);
  384. }
  385. }
  386. return (0);
  387. }
  388. static void evt_sync_activate (void)
  389. {
  390. return;
  391. }
  392. static void evt_sync_abort (void)
  393. {
  394. return;
  395. }