evil.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 unsigned int my_evt_checked_in = 0;
  76. static unsigned int my_member_list_entries;
  77. static struct corosync_api_v1 *api = NULL;
  78. static void clm_sync_init (
  79. const unsigned int *member_list,
  80. size_t member_list_entries,
  81. const struct memb_ring_id *ring_id);
  82. static int clm_sync_process (void);
  83. static void clm_sync_activate (void);
  84. static void clm_sync_abort (void);
  85. static void evt_sync_init (
  86. const unsigned int *member_list,
  87. size_t member_list_entries,
  88. const struct memb_ring_id *ring_id);
  89. static int evt_sync_process (void);
  90. static void evt_sync_activate (void);
  91. static void evt_sync_abort (void);
  92. static int clm_hack_init (void);
  93. static void deliver_fn_evt_compat (
  94. unsigned int nodeid,
  95. unsigned int service,
  96. unsigned int fn_id,
  97. const void *msg,
  98. unsigned int endian_conversion_required);
  99. static struct sync_callbacks clm_sync_operations = {
  100. .name = "dummy CLM service",
  101. .sync_init = clm_sync_init,
  102. .sync_process = clm_sync_process,
  103. .sync_activate = clm_sync_activate,
  104. .sync_abort = clm_sync_abort,
  105. };
  106. static struct sync_callbacks evt_sync_operations = {
  107. .name = "dummy EVT service",
  108. .sync_init = evt_sync_init,
  109. .sync_process = evt_sync_process,
  110. .sync_activate = evt_sync_activate,
  111. .sync_abort = evt_sync_abort,
  112. };
  113. static void sync_dummy_init (
  114. const unsigned int *member_list,
  115. size_t member_list_entries,
  116. const struct memb_ring_id *ring_id)
  117. {
  118. }
  119. static int sync_dummy_process (void)
  120. {
  121. return (0);
  122. }
  123. static void sync_dummy_activate (void)
  124. {
  125. }
  126. static void sync_dummy_abort (void)
  127. {
  128. }
  129. void evil_init (struct corosync_api_v1 *api_in)
  130. {
  131. api = api_in;
  132. clm_hack_init ();
  133. }
  134. extern int evil_callbacks_load (int sync_id,
  135. struct sync_callbacks *callbacks)
  136. {
  137. int sync_dummy_found = 1;
  138. int callbacks_init = 1;
  139. memset (callbacks, 0, sizeof (struct sync_callbacks));
  140. switch (sync_id) {
  141. case EVS_SERVICE:
  142. callbacks_init = 0;
  143. break;
  144. case CLM_SERVICE:
  145. /*
  146. * ugh
  147. */
  148. memcpy (callbacks, &clm_sync_operations, sizeof (struct sync_callbacks));
  149. callbacks_init = 0;
  150. break;
  151. case AMF_SERVICE:
  152. callbacks->name = "dummy AMF service";
  153. break;
  154. case CKPT_SERVICE:
  155. callbacks->name = "dummy CKPT service";
  156. break;
  157. case EVT_SERVICE:
  158. /*
  159. * double ugh
  160. */
  161. memcpy (callbacks, &evt_sync_operations, sizeof (struct sync_callbacks));
  162. callbacks_init = 0;
  163. break;
  164. case LCK_SERVICE:
  165. callbacks_init = 0;
  166. break;
  167. case MSG_SERVICE:
  168. callbacks_init = 0;
  169. break;
  170. case CFG_SERVICE:
  171. callbacks_init = 0;
  172. break;
  173. case CPG_SERVICE:
  174. callbacks->name = "dummy CPG service";
  175. break;
  176. case CONFDB_SERVICE:
  177. callbacks_init = 0;
  178. break;
  179. default:
  180. callbacks_init = 0;
  181. sync_dummy_found = 0;
  182. break;
  183. }
  184. if (callbacks_init) {
  185. callbacks->sync_init = sync_dummy_init;
  186. callbacks->sync_process = sync_dummy_process;
  187. callbacks->sync_activate = sync_dummy_activate;
  188. callbacks->sync_abort = sync_dummy_abort;
  189. }
  190. if (sync_dummy_found == 0) {
  191. return (-1);
  192. }
  193. return (0);
  194. }
  195. void evil_deliver_fn (
  196. unsigned int nodeid,
  197. unsigned int service,
  198. unsigned int fn_id,
  199. const void *msg,
  200. unsigned int endian_conversion_required)
  201. {
  202. if (service == EVT_SERVICE) {
  203. deliver_fn_evt_compat (
  204. nodeid,
  205. service,
  206. fn_id,
  207. msg,
  208. endian_conversion_required);
  209. }
  210. }
  211. /*
  212. * This sends the clm nodejoin message required by clm services
  213. * on whitetank as well as the event service
  214. */
  215. enum clm_message_req_types {
  216. MESSAGE_REQ_EXEC_CLM_NODEJOIN = 0
  217. };
  218. #define MAR_CLM_MAX_ADDRESS_LENGTH 64
  219. #define SA_MAX_NAME_LENGTH 256
  220. typedef struct {
  221. int size __attribute__((aligned(8)));
  222. int id __attribute__((aligned(8)));
  223. } mar_req_header_t __attribute__((aligned(8)));
  224. typedef enum {
  225. MAR_CLM_AF_INET = 1,
  226. MAR_CLM_AF_INET6 = 2
  227. } mar_clm_node_address_family_t;
  228. typedef struct {
  229. unsigned short length __attribute__((aligned(8)));
  230. mar_clm_node_address_family_t family __attribute__((aligned(8)));
  231. unsigned char value[MAR_CLM_MAX_ADDRESS_LENGTH] __attribute__((aligned(8)));
  232. } mar_clm_node_address_t;
  233. typedef uint8_t mar_uint8_t;
  234. typedef uint16_t mar_uint16_t;
  235. typedef uint32_t mar_uint32_t;
  236. typedef uint64_t mar_uint64_t;
  237. typedef struct {
  238. mar_uint16_t length __attribute__((aligned(8)));
  239. mar_uint8_t value[SA_MAX_NAME_LENGTH] __attribute__((aligned(8)));
  240. } mar_name_t;
  241. typedef struct {
  242. mar_uint32_t node_id __attribute__((aligned(8)));
  243. mar_clm_node_address_t node_address __attribute__((aligned(8)));
  244. mar_name_t node_name __attribute__((aligned(8)));
  245. mar_uint32_t member __attribute__((aligned(8)));
  246. mar_uint64_t boot_timestamp __attribute__((aligned(8)));
  247. mar_uint64_t initial_view_number __attribute__((aligned(8)));
  248. } mar_clm_cluster_node_t;
  249. mar_clm_cluster_node_t my_cluster_node;
  250. struct req_exec_clm_nodejoin {
  251. mar_req_header_t header __attribute__((aligned(8)));
  252. mar_clm_cluster_node_t cluster_node __attribute__((aligned(8)));
  253. };
  254. static void my_cluster_node_load (void)
  255. {
  256. struct totem_ip_address interfaces[INTERFACE_MAX];
  257. unsigned int iface_count;
  258. char **status;
  259. const char *iface_string;
  260. totempg_ifaces_get (
  261. totempg_my_nodeid_get (),
  262. interfaces,
  263. &status,
  264. &iface_count);
  265. iface_string = totemip_print (&interfaces[0]);
  266. sprintf ((char *)my_cluster_node.node_address.value, "%s",
  267. iface_string);
  268. my_cluster_node.node_address.length =
  269. strlen ((char *)my_cluster_node.node_address.value);
  270. if (totempg_my_family_get () == AF_INET) {
  271. my_cluster_node.node_address.family = MAR_CLM_AF_INET;
  272. } else
  273. if (totempg_my_family_get () == AF_INET6) {
  274. my_cluster_node.node_address.family = MAR_CLM_AF_INET6;
  275. } else {
  276. assert (0);
  277. }
  278. strcpy ((char *)my_cluster_node.node_name.value,
  279. (char *)my_cluster_node.node_address.value);
  280. my_cluster_node.node_name.length =
  281. my_cluster_node.node_address.length;
  282. my_cluster_node.node_id = totempg_my_nodeid_get ();
  283. my_cluster_node.member = 1;
  284. }
  285. static int clm_hack_init (void)
  286. {
  287. #if defined(COROSYNC_LINUX)
  288. struct sysinfo s_info;
  289. time_t current_time;
  290. sysinfo (&s_info);
  291. current_time = time (NULL);
  292. /* (currenttime (s) - uptime (s)) * 1 billion (ns) / 1 (s) */
  293. my_cluster_node.boot_timestamp = ((uint64_t)(current_time - s_info.uptime)) * 1000000000ULL;
  294. #elif defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  295. int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  296. struct timeval boot_time;
  297. size_t size = sizeof(boot_time);
  298. if ( sysctl(mib, 2, &boot_time, &size, NULL, 0) == -1 )
  299. boot_time.tv_sec = time (NULL);
  300. /* (currenttime (s) - uptime (s)) * 1 billion (ns) / 1 (s) */
  301. my_cluster_node.boot_timestamp = ((uint64_t)boot_time.tv_sec) * 1000000000ULL;
  302. #else /* defined(CTL_KERN) && defined(KERN_BOOTTIME) */
  303. #warning "no bootime support"
  304. #endif
  305. return (0);
  306. }
  307. static int clm_nodejoin_send (void)
  308. {
  309. struct req_exec_clm_nodejoin req_exec_clm_nodejoin;
  310. struct iovec req_exec_clm_iovec;
  311. int result;
  312. my_cluster_node_load ();
  313. req_exec_clm_nodejoin.header.size = sizeof (struct req_exec_clm_nodejoin);
  314. req_exec_clm_nodejoin.header.id =
  315. SERVICE_ID_MAKE (CLM_SERVICE, MESSAGE_REQ_EXEC_CLM_NODEJOIN);
  316. my_cluster_node.initial_view_number = 0;
  317. memcpy (&req_exec_clm_nodejoin.cluster_node, &my_cluster_node,
  318. sizeof (mar_clm_cluster_node_t));
  319. req_exec_clm_iovec.iov_base = (char *)&req_exec_clm_nodejoin;
  320. req_exec_clm_iovec.iov_len = sizeof (req_exec_clm_nodejoin);
  321. result = api->totem_mcast (&req_exec_clm_iovec, 1, TOTEMPG_AGREED);
  322. return (result);
  323. }
  324. /*
  325. * This is a noop for this service
  326. */
  327. static void clm_sync_init (
  328. const unsigned int *member_list,
  329. size_t member_list_entries,
  330. const struct memb_ring_id *ring_id)
  331. {
  332. return;
  333. }
  334. static int clm_sync_process (void)
  335. {
  336. /*
  337. * Send node information to other nodes
  338. */
  339. return (clm_nodejoin_send ());
  340. }
  341. /*
  342. * This is a noop for this service
  343. */
  344. static void clm_sync_activate (void)
  345. {
  346. return;
  347. }
  348. /*
  349. * This is a noop for this service
  350. */
  351. static void clm_sync_abort (void)
  352. {
  353. return;
  354. }
  355. enum evt_sync_states {
  356. EVT_SYNC_PART_ONE,
  357. EVT_SYNC_PART_TWO,
  358. EVT_SYNC_PART_THREE,
  359. EVT_SYNC_PART_FOUR,
  360. EVT_SYNC_DONE
  361. };
  362. static enum evt_sync_states evt_sync_state;
  363. enum evt_chan_ops {
  364. EVT_OPEN_CHAN_OP, /* chc_chan */
  365. EVT_CLOSE_CHAN_OP, /* chc_close_unlink_chan */
  366. EVT_UNLINK_CHAN_OP, /* chc_close_unlink_chan */
  367. EVT_CLEAR_RET_OP, /* chc_event_id */
  368. EVT_SET_ID_OP, /* chc_set_id */
  369. EVT_CONF_DONE, /* no data used */
  370. EVT_OPEN_COUNT, /* chc_set_opens */
  371. EVT_OPEN_COUNT_DONE /* no data used */
  372. };
  373. enum evt_message_req_types {
  374. MESSAGE_REQ_EXEC_EVT_EVENTDATA = 0,
  375. MESSAGE_REQ_EXEC_EVT_CHANCMD = 1,
  376. MESSAGE_REQ_EXEC_EVT_RECOVERY_EVENTDATA = 2
  377. };
  378. struct req_evt_chan_command {
  379. mar_req_header_t chc_head __attribute__((aligned(8)));
  380. mar_uint32_t chc_op __attribute__((aligned(8)));
  381. };
  382. static void evt_sync_init (
  383. const unsigned int *member_list,
  384. size_t member_list_entries,
  385. const struct memb_ring_id *ring_id)
  386. {
  387. my_member_list_entries = member_list_entries;
  388. my_evt_checked_in = 0;
  389. evt_sync_state = EVT_SYNC_PART_ONE;
  390. return;
  391. }
  392. static int evt_sync_process (void)
  393. {
  394. int res;
  395. struct req_evt_chan_command cpkt;
  396. struct iovec chn_iovec;
  397. memset(&cpkt, 0, sizeof(cpkt));
  398. cpkt.chc_head.id =
  399. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  400. cpkt.chc_head.size = sizeof(cpkt);
  401. chn_iovec.iov_base = &cpkt;
  402. chn_iovec.iov_len = cpkt.chc_head.size;
  403. if (evt_sync_state == EVT_SYNC_PART_ONE) {
  404. cpkt.chc_op = EVT_OPEN_COUNT_DONE;
  405. res = api->totem_mcast(&chn_iovec, 1,TOTEMPG_AGREED);
  406. if (res == -1) {
  407. return (res);
  408. }
  409. evt_sync_state = EVT_SYNC_PART_TWO;
  410. }
  411. if (evt_sync_state == EVT_SYNC_PART_THREE) {
  412. cpkt.chc_op = EVT_CONF_DONE;
  413. res = api->totem_mcast(&chn_iovec, 1,TOTEMPG_AGREED);
  414. if (res == -1) {
  415. return (res);
  416. }
  417. evt_sync_state = EVT_SYNC_PART_FOUR;
  418. }
  419. if (evt_sync_state == EVT_SYNC_DONE) {
  420. return (0);
  421. }
  422. return (-1);
  423. }
  424. static void evt_sync_activate (void)
  425. {
  426. return;
  427. }
  428. static void evt_sync_abort (void)
  429. {
  430. return;
  431. }
  432. static void deliver_fn_evt_compat (
  433. unsigned int nodeid,
  434. unsigned int service,
  435. unsigned int fn_id,
  436. const void *msg,
  437. unsigned int endian_conversion_required)
  438. {
  439. const struct req_evt_chan_command *cpkt = msg;
  440. unsigned int operation;
  441. if (fn_id != MESSAGE_REQ_EXEC_EVT_CHANCMD) {
  442. return;
  443. }
  444. operation = cpkt->chc_op;
  445. if (endian_conversion_required) {
  446. operation = swab32 (operation);
  447. }
  448. switch (operation) {
  449. case EVT_OPEN_COUNT_DONE:
  450. if (++my_evt_checked_in == my_member_list_entries) {
  451. evt_sync_state = EVT_SYNC_PART_THREE;
  452. my_evt_checked_in = 0;
  453. }
  454. break;
  455. case EVT_CONF_DONE:
  456. if (++my_evt_checked_in == my_member_list_entries) {
  457. evt_sync_state = EVT_SYNC_DONE;
  458. my_evt_checked_in = 0;
  459. }
  460. break;
  461. }
  462. }