evil.c 13 KB

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