4
0

evil.c 13 KB

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