cmap.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * Copyright (c) 2011-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <sys/types.h>
  36. #include <unistd.h>
  37. #include <fcntl.h>
  38. #include <stdlib.h>
  39. #include <errno.h>
  40. #include <unistd.h>
  41. #include <poll.h>
  42. #include <assert.h>
  43. #include <qb/qbloop.h>
  44. #include <qb/qbipc_common.h>
  45. #include <corosync/corotypes.h>
  46. #include <corosync/corodefs.h>
  47. #include <corosync/list.h>
  48. #include <corosync/mar_gen.h>
  49. #include <corosync/ipc_cmap.h>
  50. #include <corosync/logsys.h>
  51. #include <corosync/coroapi.h>
  52. #include <corosync/icmap.h>
  53. #include "service.h"
  54. LOGSYS_DECLARE_SUBSYS ("CMAP");
  55. #define MAX_REQ_EXEC_CMAP_MCAST_ITEMS 32
  56. #define ICMAP_VALUETYPE_NOT_EXIST 0
  57. struct cmap_conn_info {
  58. struct hdb_handle_database iter_db;
  59. struct hdb_handle_database track_db;
  60. };
  61. typedef uint64_t cmap_iter_handle_t;
  62. typedef uint64_t cmap_track_handle_t;
  63. struct cmap_track_user_data {
  64. void *conn;
  65. cmap_track_handle_t track_handle;
  66. uint64_t track_inst_handle;
  67. };
  68. enum cmap_message_req_types {
  69. MESSAGE_REQ_EXEC_CMAP_MCAST = 0,
  70. };
  71. enum cmap_mcast_reason {
  72. CMAP_MCAST_REASON_SYNC = 0,
  73. };
  74. static struct corosync_api_v1 *api;
  75. static char *cmap_exec_init_fn (struct corosync_api_v1 *corosync_api);
  76. static int cmap_exec_exit_fn(void);
  77. static int cmap_lib_init_fn (void *conn);
  78. static int cmap_lib_exit_fn (void *conn);
  79. static void message_handler_req_lib_cmap_set(void *conn, const void *message);
  80. static void message_handler_req_lib_cmap_delete(void *conn, const void *message);
  81. static void message_handler_req_lib_cmap_get(void *conn, const void *message);
  82. static void message_handler_req_lib_cmap_adjust_int(void *conn, const void *message);
  83. static void message_handler_req_lib_cmap_iter_init(void *conn, const void *message);
  84. static void message_handler_req_lib_cmap_iter_next(void *conn, const void *message);
  85. static void message_handler_req_lib_cmap_iter_finalize(void *conn, const void *message);
  86. static void message_handler_req_lib_cmap_track_add(void *conn, const void *message);
  87. static void message_handler_req_lib_cmap_track_delete(void *conn, const void *message);
  88. static void cmap_notify_fn(int32_t event,
  89. const char *key_name,
  90. struct icmap_notify_value new_val,
  91. struct icmap_notify_value old_val,
  92. void *user_data);
  93. static void message_handler_req_exec_cmap_mcast(
  94. const void *message,
  95. unsigned int nodeid);
  96. static void exec_cmap_mcast_endian_convert(void *message);
  97. /*
  98. * Reson is subtype of message. argc is number of items in argv array. Argv is array
  99. * of strings (key names) which will be send to wire. There can be maximum
  100. * MAX_REQ_EXEC_CMAP_MCAST_ITEMS items (for more items, CS_ERR_TOO_MANY_GROUPS
  101. * error is returned). If key is not found, item has type ICMAP_VALUETYPE_NOT_EXIST
  102. * and length zero.
  103. */
  104. static cs_error_t cmap_mcast_send(enum cmap_mcast_reason reason, int argc, char *argv[]);
  105. static void cmap_sync_init (
  106. const unsigned int *trans_list,
  107. size_t trans_list_entries,
  108. const unsigned int *member_list,
  109. size_t member_list_entries,
  110. const struct memb_ring_id *ring_id);
  111. static int cmap_sync_process (void);
  112. static void cmap_sync_activate (void);
  113. static void cmap_sync_abort (void);
  114. /*
  115. * Library Handler Definition
  116. */
  117. static struct corosync_lib_handler cmap_lib_engine[] =
  118. {
  119. { /* 0 */
  120. .lib_handler_fn = message_handler_req_lib_cmap_set,
  121. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  122. },
  123. { /* 1 */
  124. .lib_handler_fn = message_handler_req_lib_cmap_delete,
  125. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  126. },
  127. { /* 2 */
  128. .lib_handler_fn = message_handler_req_lib_cmap_get,
  129. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  130. },
  131. { /* 3 */
  132. .lib_handler_fn = message_handler_req_lib_cmap_adjust_int,
  133. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  134. },
  135. { /* 4 */
  136. .lib_handler_fn = message_handler_req_lib_cmap_iter_init,
  137. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  138. },
  139. { /* 5 */
  140. .lib_handler_fn = message_handler_req_lib_cmap_iter_next,
  141. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  142. },
  143. { /* 6 */
  144. .lib_handler_fn = message_handler_req_lib_cmap_iter_finalize,
  145. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  146. },
  147. { /* 7 */
  148. .lib_handler_fn = message_handler_req_lib_cmap_track_add,
  149. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  150. },
  151. { /* 8 */
  152. .lib_handler_fn = message_handler_req_lib_cmap_track_delete,
  153. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  154. },
  155. };
  156. static struct corosync_exec_handler cmap_exec_engine[] =
  157. {
  158. { /* 0 - MESSAGE_REQ_EXEC_CMAP_MCAST */
  159. .exec_handler_fn = message_handler_req_exec_cmap_mcast,
  160. .exec_endian_convert_fn = exec_cmap_mcast_endian_convert
  161. },
  162. };
  163. struct corosync_service_engine cmap_service_engine = {
  164. .name = "corosync configuration map access",
  165. .id = CMAP_SERVICE,
  166. .priority = 1,
  167. .private_data_size = sizeof(struct cmap_conn_info),
  168. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  169. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  170. .lib_init_fn = cmap_lib_init_fn,
  171. .lib_exit_fn = cmap_lib_exit_fn,
  172. .lib_engine = cmap_lib_engine,
  173. .lib_engine_count = sizeof (cmap_lib_engine) / sizeof (struct corosync_lib_handler),
  174. .exec_init_fn = cmap_exec_init_fn,
  175. .exec_exit_fn = cmap_exec_exit_fn,
  176. .exec_engine = cmap_exec_engine,
  177. .exec_engine_count = sizeof (cmap_exec_engine) / sizeof (struct corosync_exec_handler),
  178. .sync_init = cmap_sync_init,
  179. .sync_process = cmap_sync_process,
  180. .sync_activate = cmap_sync_activate,
  181. .sync_abort = cmap_sync_abort
  182. };
  183. struct corosync_service_engine *cmap_get_service_engine_ver0 (void)
  184. {
  185. return (&cmap_service_engine);
  186. }
  187. struct req_exec_cmap_mcast_item {
  188. mar_name_t key_name __attribute__((aligned(8)));
  189. mar_uint8_t value_type __attribute__((aligned(8)));
  190. mar_size_t value_len __attribute__((aligned(8)));
  191. uint8_t value[] __attribute__((aligned(8)));
  192. };
  193. struct req_exec_cmap_mcast {
  194. struct qb_ipc_request_header header __attribute__((aligned(8)));
  195. mar_uint8_t reason __attribute__((aligned(8)));
  196. mar_uint8_t no_items __attribute__((aligned(8)));
  197. mar_uint8_t reserved1 __attribute__((aligned(8)));
  198. mar_uint8_t reserver2 __attribute__((aligned(8)));
  199. /*
  200. * Following are array of req_exec_cmap_mcast_item alligned to 8 bytes
  201. */
  202. };
  203. static size_t cmap_sync_trans_list_entries = 0;
  204. static size_t cmap_sync_member_list_entries = 0;
  205. static uint64_t cmap_highest_config_version_received = 0;
  206. static uint64_t cmap_my_config_version = 0;
  207. static int cmap_first_sync = 1;
  208. static int cmap_exec_exit_fn(void)
  209. {
  210. return 0;
  211. }
  212. static char *cmap_exec_init_fn (
  213. struct corosync_api_v1 *corosync_api)
  214. {
  215. api = corosync_api;
  216. return (NULL);
  217. }
  218. static int cmap_lib_init_fn (void *conn)
  219. {
  220. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  221. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p", conn);
  222. api->ipc_refcnt_inc(conn);
  223. memset(conn_info, 0, sizeof(*conn_info));
  224. hdb_create(&conn_info->iter_db);
  225. hdb_create(&conn_info->track_db);
  226. return (0);
  227. }
  228. static int cmap_lib_exit_fn (void *conn)
  229. {
  230. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  231. hdb_handle_t iter_handle = 0;
  232. icmap_iter_t *iter;
  233. hdb_handle_t track_handle = 0;
  234. icmap_track_t *track;
  235. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
  236. hdb_iterator_reset(&conn_info->iter_db);
  237. while (hdb_iterator_next(&conn_info->iter_db,
  238. (void*)&iter, &iter_handle) == 0) {
  239. icmap_iter_finalize(*iter);
  240. (void)hdb_handle_put (&conn_info->iter_db, iter_handle);
  241. }
  242. hdb_destroy(&conn_info->iter_db);
  243. hdb_iterator_reset(&conn_info->track_db);
  244. while (hdb_iterator_next(&conn_info->track_db,
  245. (void*)&track, &track_handle) == 0) {
  246. free(icmap_track_get_user_data(*track));
  247. icmap_track_delete(*track);
  248. (void)hdb_handle_put (&conn_info->track_db, track_handle);
  249. }
  250. hdb_destroy(&conn_info->track_db);
  251. api->ipc_refcnt_dec(conn);
  252. return (0);
  253. }
  254. static void cmap_sync_init (
  255. const unsigned int *trans_list,
  256. size_t trans_list_entries,
  257. const unsigned int *member_list,
  258. size_t member_list_entries,
  259. const struct memb_ring_id *ring_id)
  260. {
  261. cmap_sync_trans_list_entries = trans_list_entries;
  262. cmap_sync_member_list_entries = member_list_entries;
  263. cmap_highest_config_version_received = 0;
  264. if (icmap_get_uint64("totem.config_version", &cmap_my_config_version) != CS_OK) {
  265. cmap_my_config_version = 0;
  266. }
  267. }
  268. static int cmap_sync_process (void)
  269. {
  270. const char *key = "totem.config_version";
  271. cs_error_t ret;
  272. ret = cmap_mcast_send(CMAP_MCAST_REASON_SYNC, 1, (char **)&key);
  273. return (ret == CS_OK ? 0 : -1);
  274. }
  275. static void cmap_sync_activate (void)
  276. {
  277. if (cmap_sync_trans_list_entries == 0) {
  278. log_printf(LOGSYS_LEVEL_DEBUG, "Single node sync -> no action");
  279. return ;
  280. }
  281. if (cmap_first_sync == 1) {
  282. cmap_first_sync = 0;
  283. } else {
  284. log_printf(LOGSYS_LEVEL_DEBUG, "Not first sync -> no action");
  285. return ;
  286. }
  287. if (cmap_my_config_version == 0) {
  288. log_printf(LOGSYS_LEVEL_DEBUG, "My config version is 0 -> no action");
  289. return ;
  290. }
  291. if (cmap_highest_config_version_received == 0) {
  292. log_printf(LOGSYS_LEVEL_DEBUG, "Other nodes version is 0 -> no action");
  293. return ;
  294. }
  295. if (cmap_highest_config_version_received != cmap_my_config_version) {
  296. log_printf(LOGSYS_LEVEL_ERROR,
  297. "Received config version (%"PRIu64") is different then my config version (%"PRIu64")! Exiting",
  298. cmap_highest_config_version_received, cmap_my_config_version);
  299. api->shutdown_request();
  300. return ;
  301. }
  302. }
  303. static void cmap_sync_abort (void)
  304. {
  305. }
  306. static void message_handler_req_lib_cmap_set(void *conn, const void *message)
  307. {
  308. const struct req_lib_cmap_set *req_lib_cmap_set = message;
  309. struct res_lib_cmap_set res_lib_cmap_set;
  310. cs_error_t ret;
  311. if (icmap_is_key_ro((char *)req_lib_cmap_set->key_name.value)) {
  312. ret = CS_ERR_ACCESS;
  313. } else {
  314. ret = icmap_set((char *)req_lib_cmap_set->key_name.value, &req_lib_cmap_set->value,
  315. req_lib_cmap_set->value_len, req_lib_cmap_set->type);
  316. }
  317. memset(&res_lib_cmap_set, 0, sizeof(res_lib_cmap_set));
  318. res_lib_cmap_set.header.size = sizeof(res_lib_cmap_set);
  319. res_lib_cmap_set.header.id = MESSAGE_RES_CMAP_SET;
  320. res_lib_cmap_set.header.error = ret;
  321. api->ipc_response_send(conn, &res_lib_cmap_set, sizeof(res_lib_cmap_set));
  322. }
  323. static void message_handler_req_lib_cmap_delete(void *conn, const void *message)
  324. {
  325. const struct req_lib_cmap_set *req_lib_cmap_set = message;
  326. struct res_lib_cmap_delete res_lib_cmap_delete;
  327. cs_error_t ret;
  328. if (icmap_is_key_ro((char *)req_lib_cmap_set->key_name.value)) {
  329. ret = CS_ERR_ACCESS;
  330. } else {
  331. ret = icmap_delete((char *)req_lib_cmap_set->key_name.value);
  332. }
  333. memset(&res_lib_cmap_delete, 0, sizeof(res_lib_cmap_delete));
  334. res_lib_cmap_delete.header.size = sizeof(res_lib_cmap_delete);
  335. res_lib_cmap_delete.header.id = MESSAGE_RES_CMAP_DELETE;
  336. res_lib_cmap_delete.header.error = ret;
  337. api->ipc_response_send(conn, &res_lib_cmap_delete, sizeof(res_lib_cmap_delete));
  338. }
  339. static void message_handler_req_lib_cmap_get(void *conn, const void *message)
  340. {
  341. const struct req_lib_cmap_get *req_lib_cmap_get = message;
  342. struct res_lib_cmap_get *res_lib_cmap_get;
  343. struct res_lib_cmap_get error_res_lib_cmap_get;
  344. cs_error_t ret;
  345. size_t value_len;
  346. size_t res_lib_cmap_get_size;
  347. icmap_value_types_t type;
  348. void *value;
  349. value_len = req_lib_cmap_get->value_len;
  350. res_lib_cmap_get_size = sizeof(*res_lib_cmap_get) + value_len;
  351. res_lib_cmap_get = malloc(res_lib_cmap_get_size);
  352. if (res_lib_cmap_get == NULL) {
  353. ret = CS_ERR_NO_MEMORY;
  354. goto error_exit;
  355. }
  356. memset(res_lib_cmap_get, 0, res_lib_cmap_get_size);
  357. if (value_len > 0) {
  358. value = res_lib_cmap_get->value;
  359. } else {
  360. value = NULL;
  361. }
  362. ret = icmap_get((char *)req_lib_cmap_get->key_name.value,
  363. value,
  364. &value_len,
  365. &type);
  366. if (ret != CS_OK) {
  367. free(res_lib_cmap_get);
  368. goto error_exit;
  369. }
  370. res_lib_cmap_get->header.size = res_lib_cmap_get_size;
  371. res_lib_cmap_get->header.id = MESSAGE_RES_CMAP_GET;
  372. res_lib_cmap_get->header.error = ret;
  373. res_lib_cmap_get->type = type;
  374. res_lib_cmap_get->value_len = value_len;
  375. api->ipc_response_send(conn, res_lib_cmap_get, res_lib_cmap_get_size);
  376. free(res_lib_cmap_get);
  377. return ;
  378. error_exit:
  379. memset(&error_res_lib_cmap_get, 0, sizeof(error_res_lib_cmap_get));
  380. error_res_lib_cmap_get.header.size = sizeof(error_res_lib_cmap_get);
  381. error_res_lib_cmap_get.header.id = MESSAGE_RES_CMAP_GET;
  382. error_res_lib_cmap_get.header.error = ret;
  383. api->ipc_response_send(conn, &error_res_lib_cmap_get, sizeof(error_res_lib_cmap_get));
  384. }
  385. static void message_handler_req_lib_cmap_adjust_int(void *conn, const void *message)
  386. {
  387. const struct req_lib_cmap_adjust_int *req_lib_cmap_adjust_int = message;
  388. struct res_lib_cmap_adjust_int res_lib_cmap_adjust_int;
  389. cs_error_t ret;
  390. if (icmap_is_key_ro((char *)req_lib_cmap_adjust_int->key_name.value)) {
  391. ret = CS_ERR_ACCESS;
  392. } else {
  393. ret = icmap_adjust_int((char *)req_lib_cmap_adjust_int->key_name.value,
  394. req_lib_cmap_adjust_int->step);
  395. }
  396. memset(&res_lib_cmap_adjust_int, 0, sizeof(res_lib_cmap_adjust_int));
  397. res_lib_cmap_adjust_int.header.size = sizeof(res_lib_cmap_adjust_int);
  398. res_lib_cmap_adjust_int.header.id = MESSAGE_RES_CMAP_ADJUST_INT;
  399. res_lib_cmap_adjust_int.header.error = ret;
  400. api->ipc_response_send(conn, &res_lib_cmap_adjust_int, sizeof(res_lib_cmap_adjust_int));
  401. }
  402. static void message_handler_req_lib_cmap_iter_init(void *conn, const void *message)
  403. {
  404. const struct req_lib_cmap_iter_init *req_lib_cmap_iter_init = message;
  405. struct res_lib_cmap_iter_init res_lib_cmap_iter_init;
  406. cs_error_t ret;
  407. icmap_iter_t iter;
  408. icmap_iter_t *hdb_iter;
  409. cmap_iter_handle_t handle = 0ULL;
  410. const char *prefix;
  411. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  412. if (req_lib_cmap_iter_init->prefix.length > 0) {
  413. prefix = (char *)req_lib_cmap_iter_init->prefix.value;
  414. } else {
  415. prefix = NULL;
  416. }
  417. iter = icmap_iter_init(prefix);
  418. if (iter == NULL) {
  419. ret = CS_ERR_NO_SECTIONS;
  420. goto reply_send;
  421. }
  422. ret = hdb_error_to_cs(hdb_handle_create(&conn_info->iter_db, sizeof(iter), &handle));
  423. if (ret != CS_OK) {
  424. goto reply_send;
  425. }
  426. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db, handle, (void *)&hdb_iter));
  427. if (ret != CS_OK) {
  428. goto reply_send;
  429. }
  430. *hdb_iter = iter;
  431. (void)hdb_handle_put (&conn_info->iter_db, handle);
  432. reply_send:
  433. memset(&res_lib_cmap_iter_init, 0, sizeof(res_lib_cmap_iter_init));
  434. res_lib_cmap_iter_init.header.size = sizeof(res_lib_cmap_iter_init);
  435. res_lib_cmap_iter_init.header.id = MESSAGE_RES_CMAP_ITER_INIT;
  436. res_lib_cmap_iter_init.header.error = ret;
  437. res_lib_cmap_iter_init.iter_handle = handle;
  438. api->ipc_response_send(conn, &res_lib_cmap_iter_init, sizeof(res_lib_cmap_iter_init));
  439. }
  440. static void message_handler_req_lib_cmap_iter_next(void *conn, const void *message)
  441. {
  442. const struct req_lib_cmap_iter_next *req_lib_cmap_iter_next = message;
  443. struct res_lib_cmap_iter_next res_lib_cmap_iter_next;
  444. cs_error_t ret;
  445. icmap_iter_t *iter;
  446. size_t value_len;
  447. icmap_value_types_t type;
  448. const char *res = NULL;
  449. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  450. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db,
  451. req_lib_cmap_iter_next->iter_handle, (void *)&iter));
  452. if (ret != CS_OK) {
  453. goto reply_send;
  454. }
  455. res = icmap_iter_next(*iter, &value_len, &type);
  456. if (res == NULL) {
  457. ret = CS_ERR_NO_SECTIONS;
  458. }
  459. (void)hdb_handle_put (&conn_info->iter_db, req_lib_cmap_iter_next->iter_handle);
  460. reply_send:
  461. memset(&res_lib_cmap_iter_next, 0, sizeof(res_lib_cmap_iter_next));
  462. res_lib_cmap_iter_next.header.size = sizeof(res_lib_cmap_iter_next);
  463. res_lib_cmap_iter_next.header.id = MESSAGE_RES_CMAP_ITER_NEXT;
  464. res_lib_cmap_iter_next.header.error = ret;
  465. if (res != NULL) {
  466. res_lib_cmap_iter_next.value_len = value_len;
  467. res_lib_cmap_iter_next.type = type;
  468. memcpy(res_lib_cmap_iter_next.key_name.value, res, strlen(res));
  469. res_lib_cmap_iter_next.key_name.length = strlen(res);
  470. }
  471. api->ipc_response_send(conn, &res_lib_cmap_iter_next, sizeof(res_lib_cmap_iter_next));
  472. }
  473. static void message_handler_req_lib_cmap_iter_finalize(void *conn, const void *message)
  474. {
  475. const struct req_lib_cmap_iter_finalize *req_lib_cmap_iter_finalize = message;
  476. struct res_lib_cmap_iter_finalize res_lib_cmap_iter_finalize;
  477. cs_error_t ret;
  478. icmap_iter_t *iter;
  479. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  480. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db,
  481. req_lib_cmap_iter_finalize->iter_handle, (void *)&iter));
  482. if (ret != CS_OK) {
  483. goto reply_send;
  484. }
  485. icmap_iter_finalize(*iter);
  486. (void)hdb_handle_destroy(&conn_info->iter_db, req_lib_cmap_iter_finalize->iter_handle);
  487. (void)hdb_handle_put (&conn_info->iter_db, req_lib_cmap_iter_finalize->iter_handle);
  488. reply_send:
  489. memset(&res_lib_cmap_iter_finalize, 0, sizeof(res_lib_cmap_iter_finalize));
  490. res_lib_cmap_iter_finalize.header.size = sizeof(res_lib_cmap_iter_finalize);
  491. res_lib_cmap_iter_finalize.header.id = MESSAGE_RES_CMAP_ITER_FINALIZE;
  492. res_lib_cmap_iter_finalize.header.error = ret;
  493. api->ipc_response_send(conn, &res_lib_cmap_iter_finalize, sizeof(res_lib_cmap_iter_finalize));
  494. }
  495. static void cmap_notify_fn(int32_t event,
  496. const char *key_name,
  497. struct icmap_notify_value new_val,
  498. struct icmap_notify_value old_val,
  499. void *user_data)
  500. {
  501. struct cmap_track_user_data *cmap_track_user_data = (struct cmap_track_user_data *)user_data;
  502. struct res_lib_cmap_notify_callback res_lib_cmap_notify_callback;
  503. struct iovec iov[3];
  504. memset(&res_lib_cmap_notify_callback, 0, sizeof(res_lib_cmap_notify_callback));
  505. res_lib_cmap_notify_callback.header.size = sizeof(res_lib_cmap_notify_callback) + new_val.len + old_val.len;
  506. res_lib_cmap_notify_callback.header.id = MESSAGE_RES_CMAP_NOTIFY_CALLBACK;
  507. res_lib_cmap_notify_callback.header.error = CS_OK;
  508. res_lib_cmap_notify_callback.new_value_type = new_val.type;
  509. res_lib_cmap_notify_callback.old_value_type = old_val.type;
  510. res_lib_cmap_notify_callback.new_value_len = new_val.len;
  511. res_lib_cmap_notify_callback.old_value_len = old_val.len;
  512. res_lib_cmap_notify_callback.event = event;
  513. res_lib_cmap_notify_callback.key_name.length = strlen(key_name);
  514. res_lib_cmap_notify_callback.track_inst_handle = cmap_track_user_data->track_inst_handle;
  515. memcpy(res_lib_cmap_notify_callback.key_name.value, key_name, strlen(key_name));
  516. iov[0].iov_base = (char *)&res_lib_cmap_notify_callback;
  517. iov[0].iov_len = sizeof(res_lib_cmap_notify_callback);
  518. iov[1].iov_base = (char *)new_val.data;
  519. iov[1].iov_len = new_val.len;
  520. iov[2].iov_base = (char *)old_val.data;
  521. iov[2].iov_len = old_val.len;
  522. api->ipc_dispatch_iov_send(cmap_track_user_data->conn, iov, 3);
  523. }
  524. static void message_handler_req_lib_cmap_track_add(void *conn, const void *message)
  525. {
  526. const struct req_lib_cmap_track_add *req_lib_cmap_track_add = message;
  527. struct res_lib_cmap_track_add res_lib_cmap_track_add;
  528. cs_error_t ret;
  529. cmap_track_handle_t handle = 0;
  530. icmap_track_t track = NULL;
  531. icmap_track_t *hdb_track;
  532. struct cmap_track_user_data *cmap_track_user_data;
  533. const char *key_name;
  534. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  535. cmap_track_user_data = malloc(sizeof(*cmap_track_user_data));
  536. if (cmap_track_user_data == NULL) {
  537. ret = CS_ERR_NO_MEMORY;
  538. goto reply_send;
  539. }
  540. memset(cmap_track_user_data, 0, sizeof(*cmap_track_user_data));
  541. if (req_lib_cmap_track_add->key_name.length > 0) {
  542. key_name = (char *)req_lib_cmap_track_add->key_name.value;
  543. } else {
  544. key_name = NULL;
  545. }
  546. ret = icmap_track_add(key_name,
  547. req_lib_cmap_track_add->track_type,
  548. cmap_notify_fn,
  549. cmap_track_user_data,
  550. &track);
  551. if (ret != CS_OK) {
  552. free(cmap_track_user_data);
  553. goto reply_send;
  554. }
  555. ret = hdb_error_to_cs(hdb_handle_create(&conn_info->track_db, sizeof(track), &handle));
  556. if (ret != CS_OK) {
  557. free(cmap_track_user_data);
  558. goto reply_send;
  559. }
  560. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->track_db, handle, (void *)&hdb_track));
  561. if (ret != CS_OK) {
  562. free(cmap_track_user_data);
  563. goto reply_send;
  564. }
  565. *hdb_track = track;
  566. cmap_track_user_data->conn = conn;
  567. cmap_track_user_data->track_handle = handle;
  568. cmap_track_user_data->track_inst_handle = req_lib_cmap_track_add->track_inst_handle;
  569. (void)hdb_handle_put (&conn_info->track_db, handle);
  570. reply_send:
  571. memset(&res_lib_cmap_track_add, 0, sizeof(res_lib_cmap_track_add));
  572. res_lib_cmap_track_add.header.size = sizeof(res_lib_cmap_track_add);
  573. res_lib_cmap_track_add.header.id = MESSAGE_RES_CMAP_TRACK_ADD;
  574. res_lib_cmap_track_add.header.error = ret;
  575. res_lib_cmap_track_add.track_handle = handle;
  576. api->ipc_response_send(conn, &res_lib_cmap_track_add, sizeof(res_lib_cmap_track_add));
  577. }
  578. static void message_handler_req_lib_cmap_track_delete(void *conn, const void *message)
  579. {
  580. const struct req_lib_cmap_track_delete *req_lib_cmap_track_delete = message;
  581. struct res_lib_cmap_track_delete res_lib_cmap_track_delete;
  582. cs_error_t ret;
  583. icmap_track_t *track;
  584. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  585. uint64_t track_inst_handle = 0;
  586. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->track_db,
  587. req_lib_cmap_track_delete->track_handle, (void *)&track));
  588. if (ret != CS_OK) {
  589. goto reply_send;
  590. }
  591. track_inst_handle = ((struct cmap_track_user_data *)icmap_track_get_user_data(*track))->track_inst_handle;
  592. free(icmap_track_get_user_data(*track));
  593. ret = icmap_track_delete(*track);
  594. (void)hdb_handle_put (&conn_info->track_db, req_lib_cmap_track_delete->track_handle);
  595. (void)hdb_handle_destroy(&conn_info->track_db, req_lib_cmap_track_delete->track_handle);
  596. reply_send:
  597. memset(&res_lib_cmap_track_delete, 0, sizeof(res_lib_cmap_track_delete));
  598. res_lib_cmap_track_delete.header.size = sizeof(res_lib_cmap_track_delete);
  599. res_lib_cmap_track_delete.header.id = MESSAGE_RES_CMAP_TRACK_DELETE;
  600. res_lib_cmap_track_delete.header.error = ret;
  601. res_lib_cmap_track_delete.track_inst_handle = track_inst_handle;
  602. api->ipc_response_send(conn, &res_lib_cmap_track_delete, sizeof(res_lib_cmap_track_delete));
  603. }
  604. static cs_error_t cmap_mcast_send(enum cmap_mcast_reason reason, int argc, char *argv[])
  605. {
  606. int i;
  607. size_t value_len;
  608. icmap_value_types_t value_type;
  609. cs_error_t err;
  610. size_t item_len;
  611. size_t msg_len = 0;
  612. struct req_exec_cmap_mcast req_exec_cmap_mcast;
  613. struct req_exec_cmap_mcast_item *item;
  614. struct iovec req_exec_cmap_iovec[MAX_REQ_EXEC_CMAP_MCAST_ITEMS + 1];
  615. ENTER();
  616. if (argc > MAX_REQ_EXEC_CMAP_MCAST_ITEMS) {
  617. return (CS_ERR_TOO_MANY_GROUPS);
  618. }
  619. memset(req_exec_cmap_iovec, 0, sizeof(req_exec_cmap_iovec));
  620. for (i = 0; i < argc; i++) {
  621. err = icmap_get(argv[i], NULL, &value_len, &value_type);
  622. if (err != CS_OK && err != CS_ERR_NOT_EXIST) {
  623. goto free_mem;
  624. }
  625. if (err == CS_ERR_NOT_EXIST) {
  626. value_type = ICMAP_VALUETYPE_NOT_EXIST;
  627. value_len = 0;
  628. }
  629. item_len = MAR_ALIGN_UP(sizeof(*item) + value_len, 8);
  630. item = malloc(item_len);
  631. if (item == NULL) {
  632. goto free_mem;
  633. }
  634. memset(item, 0, sizeof(item_len));
  635. item->value_type = value_type;
  636. item->value_len = value_len;
  637. item->key_name.length = strlen(argv[i]);
  638. strcpy((char *)item->key_name.value, argv[i]);
  639. if (value_type != ICMAP_VALUETYPE_NOT_EXIST) {
  640. err = icmap_get(argv[i], item->value, &value_len, &value_type);
  641. if (err != CS_OK) {
  642. goto free_mem;
  643. }
  644. }
  645. req_exec_cmap_iovec[i + 1].iov_base = item;
  646. req_exec_cmap_iovec[i + 1].iov_len = item_len;
  647. msg_len += item_len;
  648. qb_log(LOG_TRACE, "Item %u - type %u, len %zu", i, item->value_type, item->value_len);
  649. item = NULL;
  650. }
  651. memset(&req_exec_cmap_mcast, 0, sizeof(req_exec_cmap_mcast));
  652. req_exec_cmap_mcast.header.size = sizeof(req_exec_cmap_mcast) + msg_len;
  653. req_exec_cmap_mcast.reason = reason;
  654. req_exec_cmap_mcast.no_items = argc;
  655. req_exec_cmap_iovec[0].iov_base = &req_exec_cmap_mcast;
  656. req_exec_cmap_iovec[0].iov_len = sizeof(req_exec_cmap_mcast);
  657. qb_log(LOG_TRACE, "Sending %u items (%u iovec) for reason %u", argc, argc + 1, reason);
  658. err = (api->totem_mcast(req_exec_cmap_iovec, argc + 1, TOTEM_AGREED) == 0 ? CS_OK : CS_ERR_MESSAGE_ERROR);
  659. free_mem:
  660. for (i = 0; i < argc; i++) {
  661. free(req_exec_cmap_iovec[i + 1].iov_base);
  662. }
  663. free(item);
  664. LEAVE();
  665. return (err);
  666. }
  667. static struct req_exec_cmap_mcast_item *cmap_mcast_item_find(
  668. const void *message,
  669. char *key)
  670. {
  671. const struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
  672. int i;
  673. const char *p;
  674. struct req_exec_cmap_mcast_item *item;
  675. mar_uint16_t key_name_len;
  676. p = (const char *)message + sizeof(*req_exec_cmap_mcast);
  677. for (i = 0; i < req_exec_cmap_mcast->no_items; i++) {
  678. item = (struct req_exec_cmap_mcast_item *)p;
  679. key_name_len = item->key_name.length;
  680. if (strlen(key) == key_name_len && strcmp((char *)item->key_name.value, key) == 0) {
  681. return (item);
  682. }
  683. p += MAR_ALIGN_UP(sizeof(*item) + item->value_len, 8);
  684. }
  685. return (NULL);
  686. }
  687. static void message_handler_req_exec_cmap_mcast_reason_sync(
  688. const void *message,
  689. unsigned int nodeid)
  690. {
  691. uint64_t config_version = 0;
  692. struct req_exec_cmap_mcast_item *item;
  693. mar_size_t value_len;
  694. ENTER();
  695. item = cmap_mcast_item_find(message, (char *)"totem.config_version");
  696. if (item != NULL) {
  697. value_len = item->value_len;
  698. if (item->value_type == ICMAP_VALUETYPE_NOT_EXIST) {
  699. config_version = 0;
  700. }
  701. if (item->value_type == ICMAP_VALUETYPE_UINT64) {
  702. memcpy(&config_version, item->value, value_len);
  703. }
  704. }
  705. qb_log(LOG_TRACE, "Received config version %"PRIu64" from node %x", config_version, nodeid);
  706. if (nodeid != api->totem_nodeid_get() &&
  707. config_version > cmap_highest_config_version_received) {
  708. cmap_highest_config_version_received = config_version;
  709. }
  710. LEAVE();
  711. }
  712. static void message_handler_req_exec_cmap_mcast(
  713. const void *message,
  714. unsigned int nodeid)
  715. {
  716. const struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
  717. ENTER();
  718. switch (req_exec_cmap_mcast->reason) {
  719. case CMAP_MCAST_REASON_SYNC:
  720. message_handler_req_exec_cmap_mcast_reason_sync(message, nodeid);
  721. break;
  722. default:
  723. qb_log(LOG_TRACE, "Received mcast with unknown reason %u", req_exec_cmap_mcast->reason);
  724. };
  725. LEAVE();
  726. }
  727. static void exec_cmap_mcast_endian_convert(void *message)
  728. {
  729. struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
  730. const char *p;
  731. int i;
  732. struct req_exec_cmap_mcast_item *item;
  733. uint16_t u16;
  734. uint32_t u32;
  735. uint64_t u64;
  736. float flt;
  737. double dbl;
  738. swab_coroipc_request_header_t(&req_exec_cmap_mcast->header);
  739. p = (const char *)message + sizeof(*req_exec_cmap_mcast);
  740. for (i = 0; i < req_exec_cmap_mcast->no_items; i++) {
  741. item = (struct req_exec_cmap_mcast_item *)p;
  742. swab_mar_uint16_t(&item->key_name.length);
  743. swab_mar_size_t(&item->value_len);
  744. switch (item->value_type) {
  745. case ICMAP_VALUETYPE_INT16:
  746. case ICMAP_VALUETYPE_UINT16:
  747. memcpy(&u16, item->value, sizeof(u16));
  748. u16 = swab16(u16);
  749. memcpy(item->value, &u16, sizeof(u16));
  750. break;
  751. case ICMAP_VALUETYPE_INT32:
  752. case ICMAP_VALUETYPE_UINT32:
  753. memcpy(&u32, item->value, sizeof(u32));
  754. u32 = swab32(u32);
  755. memcpy(item->value, &u32, sizeof(u32));
  756. break;
  757. case ICMAP_VALUETYPE_INT64:
  758. case ICMAP_VALUETYPE_UINT64:
  759. memcpy(&u64, item->value, sizeof(u64));
  760. u64 = swab64(u64);
  761. memcpy(item->value, &u64, sizeof(u64));
  762. break;
  763. case ICMAP_VALUETYPE_FLOAT:
  764. memcpy(&flt, item->value, sizeof(flt));
  765. swabflt(&flt);
  766. memcpy(item->value, &flt, sizeof(flt));
  767. break;
  768. case ICMAP_VALUETYPE_DOUBLE:
  769. memcpy(&dbl, item->value, sizeof(dbl));
  770. swabdbl(&dbl);
  771. memcpy(item->value, &dbl, sizeof(dbl));
  772. break;
  773. }
  774. p += MAR_ALIGN_UP(sizeof(*item) + item->value_len, 8);
  775. }
  776. }