4
0

evil.c 13 KB

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