cmap.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. CMAP_MCAST_REASON_NEW_CONFIG_VERSION = 1,
  74. };
  75. static struct corosync_api_v1 *api;
  76. static char *cmap_exec_init_fn (struct corosync_api_v1 *corosync_api);
  77. static int cmap_exec_exit_fn(void);
  78. static int cmap_lib_init_fn (void *conn);
  79. static int cmap_lib_exit_fn (void *conn);
  80. static void message_handler_req_lib_cmap_set(void *conn, const void *message);
  81. static void message_handler_req_lib_cmap_delete(void *conn, const void *message);
  82. static void message_handler_req_lib_cmap_get(void *conn, const void *message);
  83. static void message_handler_req_lib_cmap_adjust_int(void *conn, const void *message);
  84. static void message_handler_req_lib_cmap_iter_init(void *conn, const void *message);
  85. static void message_handler_req_lib_cmap_iter_next(void *conn, const void *message);
  86. static void message_handler_req_lib_cmap_iter_finalize(void *conn, const void *message);
  87. static void message_handler_req_lib_cmap_track_add(void *conn, const void *message);
  88. static void message_handler_req_lib_cmap_track_delete(void *conn, const void *message);
  89. static void cmap_notify_fn(int32_t event,
  90. const char *key_name,
  91. struct icmap_notify_value new_val,
  92. struct icmap_notify_value old_val,
  93. void *user_data);
  94. static void message_handler_req_exec_cmap_mcast(
  95. const void *message,
  96. unsigned int nodeid);
  97. static void exec_cmap_mcast_endian_convert(void *message);
  98. /*
  99. * Reson is subtype of message. argc is number of items in argv array. Argv is array
  100. * of strings (key names) which will be send to wire. There can be maximum
  101. * MAX_REQ_EXEC_CMAP_MCAST_ITEMS items (for more items, CS_ERR_TOO_MANY_GROUPS
  102. * error is returned). If key is not found, item has type ICMAP_VALUETYPE_NOT_EXIST
  103. * and length zero.
  104. */
  105. static cs_error_t cmap_mcast_send(enum cmap_mcast_reason reason, int argc, char *argv[]);
  106. static void cmap_sync_init (
  107. const unsigned int *trans_list,
  108. size_t trans_list_entries,
  109. const unsigned int *member_list,
  110. size_t member_list_entries,
  111. const struct memb_ring_id *ring_id);
  112. static int cmap_sync_process (void);
  113. static void cmap_sync_activate (void);
  114. static void cmap_sync_abort (void);
  115. static void cmap_config_version_track_cb(
  116. int32_t event,
  117. const char *key_name,
  118. struct icmap_notify_value new_value,
  119. struct icmap_notify_value old_value,
  120. void *user_data);
  121. /*
  122. * Library Handler Definition
  123. */
  124. static struct corosync_lib_handler cmap_lib_engine[] =
  125. {
  126. { /* 0 */
  127. .lib_handler_fn = message_handler_req_lib_cmap_set,
  128. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  129. },
  130. { /* 1 */
  131. .lib_handler_fn = message_handler_req_lib_cmap_delete,
  132. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  133. },
  134. { /* 2 */
  135. .lib_handler_fn = message_handler_req_lib_cmap_get,
  136. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  137. },
  138. { /* 3 */
  139. .lib_handler_fn = message_handler_req_lib_cmap_adjust_int,
  140. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  141. },
  142. { /* 4 */
  143. .lib_handler_fn = message_handler_req_lib_cmap_iter_init,
  144. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  145. },
  146. { /* 5 */
  147. .lib_handler_fn = message_handler_req_lib_cmap_iter_next,
  148. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  149. },
  150. { /* 6 */
  151. .lib_handler_fn = message_handler_req_lib_cmap_iter_finalize,
  152. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  153. },
  154. { /* 7 */
  155. .lib_handler_fn = message_handler_req_lib_cmap_track_add,
  156. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  157. },
  158. { /* 8 */
  159. .lib_handler_fn = message_handler_req_lib_cmap_track_delete,
  160. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  161. },
  162. };
  163. static struct corosync_exec_handler cmap_exec_engine[] =
  164. {
  165. { /* 0 - MESSAGE_REQ_EXEC_CMAP_MCAST */
  166. .exec_handler_fn = message_handler_req_exec_cmap_mcast,
  167. .exec_endian_convert_fn = exec_cmap_mcast_endian_convert
  168. },
  169. };
  170. struct corosync_service_engine cmap_service_engine = {
  171. .name = "corosync configuration map access",
  172. .id = CMAP_SERVICE,
  173. .priority = 1,
  174. .private_data_size = sizeof(struct cmap_conn_info),
  175. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  176. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  177. .lib_init_fn = cmap_lib_init_fn,
  178. .lib_exit_fn = cmap_lib_exit_fn,
  179. .lib_engine = cmap_lib_engine,
  180. .lib_engine_count = sizeof (cmap_lib_engine) / sizeof (struct corosync_lib_handler),
  181. .exec_init_fn = cmap_exec_init_fn,
  182. .exec_exit_fn = cmap_exec_exit_fn,
  183. .exec_engine = cmap_exec_engine,
  184. .exec_engine_count = sizeof (cmap_exec_engine) / sizeof (struct corosync_exec_handler),
  185. .sync_init = cmap_sync_init,
  186. .sync_process = cmap_sync_process,
  187. .sync_activate = cmap_sync_activate,
  188. .sync_abort = cmap_sync_abort
  189. };
  190. struct corosync_service_engine *cmap_get_service_engine_ver0 (void)
  191. {
  192. return (&cmap_service_engine);
  193. }
  194. struct req_exec_cmap_mcast_item {
  195. mar_name_t key_name __attribute__((aligned(8)));
  196. mar_uint8_t value_type __attribute__((aligned(8)));
  197. mar_size_t value_len __attribute__((aligned(8)));
  198. uint8_t value[] __attribute__((aligned(8)));
  199. };
  200. struct req_exec_cmap_mcast {
  201. struct qb_ipc_request_header header __attribute__((aligned(8)));
  202. mar_uint8_t reason __attribute__((aligned(8)));
  203. mar_uint8_t no_items __attribute__((aligned(8)));
  204. mar_uint8_t reserved1 __attribute__((aligned(8)));
  205. mar_uint8_t reserver2 __attribute__((aligned(8)));
  206. /*
  207. * Following are array of req_exec_cmap_mcast_item alligned to 8 bytes
  208. */
  209. };
  210. static size_t cmap_sync_trans_list_entries = 0;
  211. static size_t cmap_sync_member_list_entries = 0;
  212. static uint64_t cmap_highest_config_version_received = 0;
  213. static uint64_t cmap_my_config_version = 0;
  214. static int cmap_first_sync = 1;
  215. static icmap_track_t cmap_config_version_track;
  216. static void cmap_config_version_track_cb(
  217. int32_t event,
  218. const char *key_name,
  219. struct icmap_notify_value new_value,
  220. struct icmap_notify_value old_value,
  221. void *user_data)
  222. {
  223. const char *key = "totem.config_version";
  224. cs_error_t ret;
  225. ENTER();
  226. if (icmap_get_uint64("totem.config_version", &cmap_my_config_version) != CS_OK) {
  227. cmap_my_config_version = 0;
  228. }
  229. ret = cmap_mcast_send(CMAP_MCAST_REASON_NEW_CONFIG_VERSION, 1, (char **)&key);
  230. if (ret != CS_OK) {
  231. log_printf(LOGSYS_LEVEL_ERROR, "Can't inform other nodes about new config version");
  232. }
  233. LEAVE();
  234. }
  235. static int cmap_exec_exit_fn(void)
  236. {
  237. if (icmap_track_delete(cmap_config_version_track) != CS_OK) {
  238. log_printf(LOGSYS_LEVEL_ERROR, "Can't delete config_version icmap tracker");
  239. }
  240. return 0;
  241. }
  242. static char *cmap_exec_init_fn (
  243. struct corosync_api_v1 *corosync_api)
  244. {
  245. cs_error_t ret;
  246. api = corosync_api;
  247. ret = icmap_track_add("totem.config_version",
  248. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY,
  249. cmap_config_version_track_cb,
  250. NULL,
  251. &cmap_config_version_track);
  252. if (ret != CS_OK) {
  253. return ((char *)"Can't add config_version icmap tracker");
  254. }
  255. return (NULL);
  256. }
  257. static int cmap_lib_init_fn (void *conn)
  258. {
  259. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  260. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p", conn);
  261. api->ipc_refcnt_inc(conn);
  262. memset(conn_info, 0, sizeof(*conn_info));
  263. hdb_create(&conn_info->iter_db);
  264. hdb_create(&conn_info->track_db);
  265. return (0);
  266. }
  267. static int cmap_lib_exit_fn (void *conn)
  268. {
  269. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  270. hdb_handle_t iter_handle = 0;
  271. icmap_iter_t *iter;
  272. hdb_handle_t track_handle = 0;
  273. icmap_track_t *track;
  274. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
  275. hdb_iterator_reset(&conn_info->iter_db);
  276. while (hdb_iterator_next(&conn_info->iter_db,
  277. (void*)&iter, &iter_handle) == 0) {
  278. icmap_iter_finalize(*iter);
  279. (void)hdb_handle_put (&conn_info->iter_db, iter_handle);
  280. }
  281. hdb_destroy(&conn_info->iter_db);
  282. hdb_iterator_reset(&conn_info->track_db);
  283. while (hdb_iterator_next(&conn_info->track_db,
  284. (void*)&track, &track_handle) == 0) {
  285. free(icmap_track_get_user_data(*track));
  286. icmap_track_delete(*track);
  287. (void)hdb_handle_put (&conn_info->track_db, track_handle);
  288. }
  289. hdb_destroy(&conn_info->track_db);
  290. api->ipc_refcnt_dec(conn);
  291. return (0);
  292. }
  293. static void cmap_sync_init (
  294. const unsigned int *trans_list,
  295. size_t trans_list_entries,
  296. const unsigned int *member_list,
  297. size_t member_list_entries,
  298. const struct memb_ring_id *ring_id)
  299. {
  300. cmap_sync_trans_list_entries = trans_list_entries;
  301. cmap_sync_member_list_entries = member_list_entries;
  302. cmap_highest_config_version_received = 0;
  303. if (icmap_get_uint64("totem.config_version", &cmap_my_config_version) != CS_OK) {
  304. cmap_my_config_version = 0;
  305. }
  306. }
  307. static int cmap_sync_process (void)
  308. {
  309. const char *key = "totem.config_version";
  310. cs_error_t ret;
  311. ret = cmap_mcast_send(CMAP_MCAST_REASON_SYNC, 1, (char **)&key);
  312. return (ret == CS_OK ? 0 : -1);
  313. }
  314. static void cmap_sync_activate (void)
  315. {
  316. if (cmap_sync_trans_list_entries == 0) {
  317. log_printf(LOGSYS_LEVEL_DEBUG, "Single node sync -> no action");
  318. return ;
  319. }
  320. if (cmap_first_sync == 1) {
  321. cmap_first_sync = 0;
  322. } else {
  323. log_printf(LOGSYS_LEVEL_DEBUG, "Not first sync -> no action");
  324. return ;
  325. }
  326. if (cmap_my_config_version == 0) {
  327. log_printf(LOGSYS_LEVEL_DEBUG, "My config version is 0 -> no action");
  328. return ;
  329. }
  330. if (cmap_highest_config_version_received == 0) {
  331. log_printf(LOGSYS_LEVEL_DEBUG, "Other nodes version is 0 -> no action");
  332. return ;
  333. }
  334. if (cmap_highest_config_version_received != cmap_my_config_version) {
  335. log_printf(LOGSYS_LEVEL_ERROR,
  336. "Received config version (%"PRIu64") is different then my config version (%"PRIu64")! Exiting",
  337. cmap_highest_config_version_received, cmap_my_config_version);
  338. api->shutdown_request();
  339. return ;
  340. }
  341. }
  342. static void cmap_sync_abort (void)
  343. {
  344. }
  345. static void message_handler_req_lib_cmap_set(void *conn, const void *message)
  346. {
  347. const struct req_lib_cmap_set *req_lib_cmap_set = message;
  348. struct res_lib_cmap_set res_lib_cmap_set;
  349. cs_error_t ret;
  350. if (icmap_is_key_ro((char *)req_lib_cmap_set->key_name.value)) {
  351. ret = CS_ERR_ACCESS;
  352. } else {
  353. ret = icmap_set((char *)req_lib_cmap_set->key_name.value, &req_lib_cmap_set->value,
  354. req_lib_cmap_set->value_len, req_lib_cmap_set->type);
  355. }
  356. memset(&res_lib_cmap_set, 0, sizeof(res_lib_cmap_set));
  357. res_lib_cmap_set.header.size = sizeof(res_lib_cmap_set);
  358. res_lib_cmap_set.header.id = MESSAGE_RES_CMAP_SET;
  359. res_lib_cmap_set.header.error = ret;
  360. api->ipc_response_send(conn, &res_lib_cmap_set, sizeof(res_lib_cmap_set));
  361. }
  362. static void message_handler_req_lib_cmap_delete(void *conn, const void *message)
  363. {
  364. const struct req_lib_cmap_set *req_lib_cmap_set = message;
  365. struct res_lib_cmap_delete res_lib_cmap_delete;
  366. cs_error_t ret;
  367. if (icmap_is_key_ro((char *)req_lib_cmap_set->key_name.value)) {
  368. ret = CS_ERR_ACCESS;
  369. } else {
  370. ret = icmap_delete((char *)req_lib_cmap_set->key_name.value);
  371. }
  372. memset(&res_lib_cmap_delete, 0, sizeof(res_lib_cmap_delete));
  373. res_lib_cmap_delete.header.size = sizeof(res_lib_cmap_delete);
  374. res_lib_cmap_delete.header.id = MESSAGE_RES_CMAP_DELETE;
  375. res_lib_cmap_delete.header.error = ret;
  376. api->ipc_response_send(conn, &res_lib_cmap_delete, sizeof(res_lib_cmap_delete));
  377. }
  378. static void message_handler_req_lib_cmap_get(void *conn, const void *message)
  379. {
  380. const struct req_lib_cmap_get *req_lib_cmap_get = message;
  381. struct res_lib_cmap_get *res_lib_cmap_get;
  382. struct res_lib_cmap_get error_res_lib_cmap_get;
  383. cs_error_t ret;
  384. size_t value_len;
  385. size_t res_lib_cmap_get_size;
  386. icmap_value_types_t type;
  387. void *value;
  388. value_len = req_lib_cmap_get->value_len;
  389. res_lib_cmap_get_size = sizeof(*res_lib_cmap_get) + value_len;
  390. res_lib_cmap_get = malloc(res_lib_cmap_get_size);
  391. if (res_lib_cmap_get == NULL) {
  392. ret = CS_ERR_NO_MEMORY;
  393. goto error_exit;
  394. }
  395. memset(res_lib_cmap_get, 0, res_lib_cmap_get_size);
  396. if (value_len > 0) {
  397. value = res_lib_cmap_get->value;
  398. } else {
  399. value = NULL;
  400. }
  401. ret = icmap_get((char *)req_lib_cmap_get->key_name.value,
  402. value,
  403. &value_len,
  404. &type);
  405. if (ret != CS_OK) {
  406. free(res_lib_cmap_get);
  407. goto error_exit;
  408. }
  409. res_lib_cmap_get->header.size = res_lib_cmap_get_size;
  410. res_lib_cmap_get->header.id = MESSAGE_RES_CMAP_GET;
  411. res_lib_cmap_get->header.error = ret;
  412. res_lib_cmap_get->type = type;
  413. res_lib_cmap_get->value_len = value_len;
  414. api->ipc_response_send(conn, res_lib_cmap_get, res_lib_cmap_get_size);
  415. free(res_lib_cmap_get);
  416. return ;
  417. error_exit:
  418. memset(&error_res_lib_cmap_get, 0, sizeof(error_res_lib_cmap_get));
  419. error_res_lib_cmap_get.header.size = sizeof(error_res_lib_cmap_get);
  420. error_res_lib_cmap_get.header.id = MESSAGE_RES_CMAP_GET;
  421. error_res_lib_cmap_get.header.error = ret;
  422. api->ipc_response_send(conn, &error_res_lib_cmap_get, sizeof(error_res_lib_cmap_get));
  423. }
  424. static void message_handler_req_lib_cmap_adjust_int(void *conn, const void *message)
  425. {
  426. const struct req_lib_cmap_adjust_int *req_lib_cmap_adjust_int = message;
  427. struct res_lib_cmap_adjust_int res_lib_cmap_adjust_int;
  428. cs_error_t ret;
  429. if (icmap_is_key_ro((char *)req_lib_cmap_adjust_int->key_name.value)) {
  430. ret = CS_ERR_ACCESS;
  431. } else {
  432. ret = icmap_adjust_int((char *)req_lib_cmap_adjust_int->key_name.value,
  433. req_lib_cmap_adjust_int->step);
  434. }
  435. memset(&res_lib_cmap_adjust_int, 0, sizeof(res_lib_cmap_adjust_int));
  436. res_lib_cmap_adjust_int.header.size = sizeof(res_lib_cmap_adjust_int);
  437. res_lib_cmap_adjust_int.header.id = MESSAGE_RES_CMAP_ADJUST_INT;
  438. res_lib_cmap_adjust_int.header.error = ret;
  439. api->ipc_response_send(conn, &res_lib_cmap_adjust_int, sizeof(res_lib_cmap_adjust_int));
  440. }
  441. static void message_handler_req_lib_cmap_iter_init(void *conn, const void *message)
  442. {
  443. const struct req_lib_cmap_iter_init *req_lib_cmap_iter_init = message;
  444. struct res_lib_cmap_iter_init res_lib_cmap_iter_init;
  445. cs_error_t ret;
  446. icmap_iter_t iter;
  447. icmap_iter_t *hdb_iter;
  448. cmap_iter_handle_t handle = 0ULL;
  449. const char *prefix;
  450. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  451. if (req_lib_cmap_iter_init->prefix.length > 0) {
  452. prefix = (char *)req_lib_cmap_iter_init->prefix.value;
  453. } else {
  454. prefix = NULL;
  455. }
  456. iter = icmap_iter_init(prefix);
  457. if (iter == NULL) {
  458. ret = CS_ERR_NO_SECTIONS;
  459. goto reply_send;
  460. }
  461. ret = hdb_error_to_cs(hdb_handle_create(&conn_info->iter_db, sizeof(iter), &handle));
  462. if (ret != CS_OK) {
  463. goto reply_send;
  464. }
  465. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db, handle, (void *)&hdb_iter));
  466. if (ret != CS_OK) {
  467. goto reply_send;
  468. }
  469. *hdb_iter = iter;
  470. (void)hdb_handle_put (&conn_info->iter_db, handle);
  471. reply_send:
  472. memset(&res_lib_cmap_iter_init, 0, sizeof(res_lib_cmap_iter_init));
  473. res_lib_cmap_iter_init.header.size = sizeof(res_lib_cmap_iter_init);
  474. res_lib_cmap_iter_init.header.id = MESSAGE_RES_CMAP_ITER_INIT;
  475. res_lib_cmap_iter_init.header.error = ret;
  476. res_lib_cmap_iter_init.iter_handle = handle;
  477. api->ipc_response_send(conn, &res_lib_cmap_iter_init, sizeof(res_lib_cmap_iter_init));
  478. }
  479. static void message_handler_req_lib_cmap_iter_next(void *conn, const void *message)
  480. {
  481. const struct req_lib_cmap_iter_next *req_lib_cmap_iter_next = message;
  482. struct res_lib_cmap_iter_next res_lib_cmap_iter_next;
  483. cs_error_t ret;
  484. icmap_iter_t *iter;
  485. size_t value_len;
  486. icmap_value_types_t type;
  487. const char *res = NULL;
  488. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  489. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db,
  490. req_lib_cmap_iter_next->iter_handle, (void *)&iter));
  491. if (ret != CS_OK) {
  492. goto reply_send;
  493. }
  494. res = icmap_iter_next(*iter, &value_len, &type);
  495. if (res == NULL) {
  496. ret = CS_ERR_NO_SECTIONS;
  497. }
  498. (void)hdb_handle_put (&conn_info->iter_db, req_lib_cmap_iter_next->iter_handle);
  499. reply_send:
  500. memset(&res_lib_cmap_iter_next, 0, sizeof(res_lib_cmap_iter_next));
  501. res_lib_cmap_iter_next.header.size = sizeof(res_lib_cmap_iter_next);
  502. res_lib_cmap_iter_next.header.id = MESSAGE_RES_CMAP_ITER_NEXT;
  503. res_lib_cmap_iter_next.header.error = ret;
  504. if (res != NULL) {
  505. res_lib_cmap_iter_next.value_len = value_len;
  506. res_lib_cmap_iter_next.type = type;
  507. memcpy(res_lib_cmap_iter_next.key_name.value, res, strlen(res));
  508. res_lib_cmap_iter_next.key_name.length = strlen(res);
  509. }
  510. api->ipc_response_send(conn, &res_lib_cmap_iter_next, sizeof(res_lib_cmap_iter_next));
  511. }
  512. static void message_handler_req_lib_cmap_iter_finalize(void *conn, const void *message)
  513. {
  514. const struct req_lib_cmap_iter_finalize *req_lib_cmap_iter_finalize = message;
  515. struct res_lib_cmap_iter_finalize res_lib_cmap_iter_finalize;
  516. cs_error_t ret;
  517. icmap_iter_t *iter;
  518. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  519. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db,
  520. req_lib_cmap_iter_finalize->iter_handle, (void *)&iter));
  521. if (ret != CS_OK) {
  522. goto reply_send;
  523. }
  524. icmap_iter_finalize(*iter);
  525. (void)hdb_handle_destroy(&conn_info->iter_db, req_lib_cmap_iter_finalize->iter_handle);
  526. (void)hdb_handle_put (&conn_info->iter_db, req_lib_cmap_iter_finalize->iter_handle);
  527. reply_send:
  528. memset(&res_lib_cmap_iter_finalize, 0, sizeof(res_lib_cmap_iter_finalize));
  529. res_lib_cmap_iter_finalize.header.size = sizeof(res_lib_cmap_iter_finalize);
  530. res_lib_cmap_iter_finalize.header.id = MESSAGE_RES_CMAP_ITER_FINALIZE;
  531. res_lib_cmap_iter_finalize.header.error = ret;
  532. api->ipc_response_send(conn, &res_lib_cmap_iter_finalize, sizeof(res_lib_cmap_iter_finalize));
  533. }
  534. static void cmap_notify_fn(int32_t event,
  535. const char *key_name,
  536. struct icmap_notify_value new_val,
  537. struct icmap_notify_value old_val,
  538. void *user_data)
  539. {
  540. struct cmap_track_user_data *cmap_track_user_data = (struct cmap_track_user_data *)user_data;
  541. struct res_lib_cmap_notify_callback res_lib_cmap_notify_callback;
  542. struct iovec iov[3];
  543. memset(&res_lib_cmap_notify_callback, 0, sizeof(res_lib_cmap_notify_callback));
  544. res_lib_cmap_notify_callback.header.size = sizeof(res_lib_cmap_notify_callback) + new_val.len + old_val.len;
  545. res_lib_cmap_notify_callback.header.id = MESSAGE_RES_CMAP_NOTIFY_CALLBACK;
  546. res_lib_cmap_notify_callback.header.error = CS_OK;
  547. res_lib_cmap_notify_callback.new_value_type = new_val.type;
  548. res_lib_cmap_notify_callback.old_value_type = old_val.type;
  549. res_lib_cmap_notify_callback.new_value_len = new_val.len;
  550. res_lib_cmap_notify_callback.old_value_len = old_val.len;
  551. res_lib_cmap_notify_callback.event = event;
  552. res_lib_cmap_notify_callback.key_name.length = strlen(key_name);
  553. res_lib_cmap_notify_callback.track_inst_handle = cmap_track_user_data->track_inst_handle;
  554. memcpy(res_lib_cmap_notify_callback.key_name.value, key_name, strlen(key_name));
  555. iov[0].iov_base = (char *)&res_lib_cmap_notify_callback;
  556. iov[0].iov_len = sizeof(res_lib_cmap_notify_callback);
  557. iov[1].iov_base = (char *)new_val.data;
  558. iov[1].iov_len = new_val.len;
  559. iov[2].iov_base = (char *)old_val.data;
  560. iov[2].iov_len = old_val.len;
  561. api->ipc_dispatch_iov_send(cmap_track_user_data->conn, iov, 3);
  562. }
  563. static void message_handler_req_lib_cmap_track_add(void *conn, const void *message)
  564. {
  565. const struct req_lib_cmap_track_add *req_lib_cmap_track_add = message;
  566. struct res_lib_cmap_track_add res_lib_cmap_track_add;
  567. cs_error_t ret;
  568. cmap_track_handle_t handle = 0;
  569. icmap_track_t track = NULL;
  570. icmap_track_t *hdb_track;
  571. struct cmap_track_user_data *cmap_track_user_data;
  572. const char *key_name;
  573. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  574. cmap_track_user_data = malloc(sizeof(*cmap_track_user_data));
  575. if (cmap_track_user_data == NULL) {
  576. ret = CS_ERR_NO_MEMORY;
  577. goto reply_send;
  578. }
  579. memset(cmap_track_user_data, 0, sizeof(*cmap_track_user_data));
  580. if (req_lib_cmap_track_add->key_name.length > 0) {
  581. key_name = (char *)req_lib_cmap_track_add->key_name.value;
  582. } else {
  583. key_name = NULL;
  584. }
  585. ret = icmap_track_add(key_name,
  586. req_lib_cmap_track_add->track_type,
  587. cmap_notify_fn,
  588. cmap_track_user_data,
  589. &track);
  590. if (ret != CS_OK) {
  591. free(cmap_track_user_data);
  592. goto reply_send;
  593. }
  594. ret = hdb_error_to_cs(hdb_handle_create(&conn_info->track_db, sizeof(track), &handle));
  595. if (ret != CS_OK) {
  596. free(cmap_track_user_data);
  597. goto reply_send;
  598. }
  599. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->track_db, handle, (void *)&hdb_track));
  600. if (ret != CS_OK) {
  601. free(cmap_track_user_data);
  602. goto reply_send;
  603. }
  604. *hdb_track = track;
  605. cmap_track_user_data->conn = conn;
  606. cmap_track_user_data->track_handle = handle;
  607. cmap_track_user_data->track_inst_handle = req_lib_cmap_track_add->track_inst_handle;
  608. (void)hdb_handle_put (&conn_info->track_db, handle);
  609. reply_send:
  610. memset(&res_lib_cmap_track_add, 0, sizeof(res_lib_cmap_track_add));
  611. res_lib_cmap_track_add.header.size = sizeof(res_lib_cmap_track_add);
  612. res_lib_cmap_track_add.header.id = MESSAGE_RES_CMAP_TRACK_ADD;
  613. res_lib_cmap_track_add.header.error = ret;
  614. res_lib_cmap_track_add.track_handle = handle;
  615. api->ipc_response_send(conn, &res_lib_cmap_track_add, sizeof(res_lib_cmap_track_add));
  616. }
  617. static void message_handler_req_lib_cmap_track_delete(void *conn, const void *message)
  618. {
  619. const struct req_lib_cmap_track_delete *req_lib_cmap_track_delete = message;
  620. struct res_lib_cmap_track_delete res_lib_cmap_track_delete;
  621. cs_error_t ret;
  622. icmap_track_t *track;
  623. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  624. uint64_t track_inst_handle = 0;
  625. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->track_db,
  626. req_lib_cmap_track_delete->track_handle, (void *)&track));
  627. if (ret != CS_OK) {
  628. goto reply_send;
  629. }
  630. track_inst_handle = ((struct cmap_track_user_data *)icmap_track_get_user_data(*track))->track_inst_handle;
  631. free(icmap_track_get_user_data(*track));
  632. ret = icmap_track_delete(*track);
  633. (void)hdb_handle_put (&conn_info->track_db, req_lib_cmap_track_delete->track_handle);
  634. (void)hdb_handle_destroy(&conn_info->track_db, req_lib_cmap_track_delete->track_handle);
  635. reply_send:
  636. memset(&res_lib_cmap_track_delete, 0, sizeof(res_lib_cmap_track_delete));
  637. res_lib_cmap_track_delete.header.size = sizeof(res_lib_cmap_track_delete);
  638. res_lib_cmap_track_delete.header.id = MESSAGE_RES_CMAP_TRACK_DELETE;
  639. res_lib_cmap_track_delete.header.error = ret;
  640. res_lib_cmap_track_delete.track_inst_handle = track_inst_handle;
  641. api->ipc_response_send(conn, &res_lib_cmap_track_delete, sizeof(res_lib_cmap_track_delete));
  642. }
  643. static cs_error_t cmap_mcast_send(enum cmap_mcast_reason reason, int argc, char *argv[])
  644. {
  645. int i;
  646. size_t value_len;
  647. icmap_value_types_t value_type;
  648. cs_error_t err;
  649. size_t item_len;
  650. size_t msg_len = 0;
  651. struct req_exec_cmap_mcast req_exec_cmap_mcast;
  652. struct req_exec_cmap_mcast_item *item;
  653. struct iovec req_exec_cmap_iovec[MAX_REQ_EXEC_CMAP_MCAST_ITEMS + 1];
  654. ENTER();
  655. if (argc > MAX_REQ_EXEC_CMAP_MCAST_ITEMS) {
  656. return (CS_ERR_TOO_MANY_GROUPS);
  657. }
  658. memset(req_exec_cmap_iovec, 0, sizeof(req_exec_cmap_iovec));
  659. for (i = 0; i < argc; i++) {
  660. err = icmap_get(argv[i], NULL, &value_len, &value_type);
  661. if (err != CS_OK && err != CS_ERR_NOT_EXIST) {
  662. goto free_mem;
  663. }
  664. if (err == CS_ERR_NOT_EXIST) {
  665. value_type = ICMAP_VALUETYPE_NOT_EXIST;
  666. value_len = 0;
  667. }
  668. item_len = MAR_ALIGN_UP(sizeof(*item) + value_len, 8);
  669. item = malloc(item_len);
  670. if (item == NULL) {
  671. goto free_mem;
  672. }
  673. memset(item, 0, sizeof(item_len));
  674. item->value_type = value_type;
  675. item->value_len = value_len;
  676. item->key_name.length = strlen(argv[i]);
  677. strcpy((char *)item->key_name.value, argv[i]);
  678. if (value_type != ICMAP_VALUETYPE_NOT_EXIST) {
  679. err = icmap_get(argv[i], item->value, &value_len, &value_type);
  680. if (err != CS_OK) {
  681. goto free_mem;
  682. }
  683. }
  684. req_exec_cmap_iovec[i + 1].iov_base = item;
  685. req_exec_cmap_iovec[i + 1].iov_len = item_len;
  686. msg_len += item_len;
  687. qb_log(LOG_TRACE, "Item %u - type %u, len %zu", i, item->value_type, item->value_len);
  688. item = NULL;
  689. }
  690. memset(&req_exec_cmap_mcast, 0, sizeof(req_exec_cmap_mcast));
  691. req_exec_cmap_mcast.header.size = sizeof(req_exec_cmap_mcast) + msg_len;
  692. req_exec_cmap_mcast.reason = reason;
  693. req_exec_cmap_mcast.no_items = argc;
  694. req_exec_cmap_iovec[0].iov_base = &req_exec_cmap_mcast;
  695. req_exec_cmap_iovec[0].iov_len = sizeof(req_exec_cmap_mcast);
  696. qb_log(LOG_TRACE, "Sending %u items (%u iovec) for reason %u", argc, argc + 1, reason);
  697. err = (api->totem_mcast(req_exec_cmap_iovec, argc + 1, TOTEM_AGREED) == 0 ? CS_OK : CS_ERR_MESSAGE_ERROR);
  698. free_mem:
  699. for (i = 0; i < argc; i++) {
  700. free(req_exec_cmap_iovec[i + 1].iov_base);
  701. }
  702. free(item);
  703. LEAVE();
  704. return (err);
  705. }
  706. static struct req_exec_cmap_mcast_item *cmap_mcast_item_find(
  707. const void *message,
  708. char *key)
  709. {
  710. const struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
  711. int i;
  712. const char *p;
  713. struct req_exec_cmap_mcast_item *item;
  714. mar_uint16_t key_name_len;
  715. p = (const char *)message + sizeof(*req_exec_cmap_mcast);
  716. for (i = 0; i < req_exec_cmap_mcast->no_items; i++) {
  717. item = (struct req_exec_cmap_mcast_item *)p;
  718. key_name_len = item->key_name.length;
  719. if (strlen(key) == key_name_len && strcmp((char *)item->key_name.value, key) == 0) {
  720. return (item);
  721. }
  722. p += MAR_ALIGN_UP(sizeof(*item) + item->value_len, 8);
  723. }
  724. return (NULL);
  725. }
  726. static void message_handler_req_exec_cmap_mcast_reason_sync_nv(
  727. enum cmap_mcast_reason reason,
  728. const void *message,
  729. unsigned int nodeid)
  730. {
  731. char member_config_version[ICMAP_KEYNAME_MAXLEN];
  732. uint64_t config_version = 0;
  733. struct req_exec_cmap_mcast_item *item;
  734. mar_size_t value_len;
  735. ENTER();
  736. item = cmap_mcast_item_find(message, (char *)"totem.config_version");
  737. if (item != NULL) {
  738. value_len = item->value_len;
  739. if (item->value_type == ICMAP_VALUETYPE_NOT_EXIST) {
  740. config_version = 0;
  741. }
  742. if (item->value_type == ICMAP_VALUETYPE_UINT64) {
  743. memcpy(&config_version, item->value, value_len);
  744. }
  745. }
  746. qb_log(LOG_TRACE, "Received config version %"PRIu64" from node %x", config_version, nodeid);
  747. if (nodeid != api->totem_nodeid_get() &&
  748. config_version > cmap_highest_config_version_received) {
  749. cmap_highest_config_version_received = config_version;
  750. }
  751. snprintf(member_config_version, ICMAP_KEYNAME_MAXLEN,
  752. "runtime.totem.pg.mrp.srp.members.%u.config_version", nodeid);
  753. icmap_set_uint64(member_config_version, config_version);
  754. LEAVE();
  755. }
  756. static void message_handler_req_exec_cmap_mcast(
  757. const void *message,
  758. unsigned int nodeid)
  759. {
  760. const struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
  761. ENTER();
  762. switch (req_exec_cmap_mcast->reason) {
  763. case CMAP_MCAST_REASON_SYNC:
  764. message_handler_req_exec_cmap_mcast_reason_sync_nv(req_exec_cmap_mcast->reason,
  765. message, nodeid);
  766. break;
  767. case CMAP_MCAST_REASON_NEW_CONFIG_VERSION:
  768. message_handler_req_exec_cmap_mcast_reason_sync_nv(req_exec_cmap_mcast->reason,
  769. message, nodeid);
  770. break;
  771. default:
  772. qb_log(LOG_TRACE, "Received mcast with unknown reason %u", req_exec_cmap_mcast->reason);
  773. };
  774. LEAVE();
  775. }
  776. static void exec_cmap_mcast_endian_convert(void *message)
  777. {
  778. struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
  779. const char *p;
  780. int i;
  781. struct req_exec_cmap_mcast_item *item;
  782. uint16_t u16;
  783. uint32_t u32;
  784. uint64_t u64;
  785. float flt;
  786. double dbl;
  787. swab_coroipc_request_header_t(&req_exec_cmap_mcast->header);
  788. p = (const char *)message + sizeof(*req_exec_cmap_mcast);
  789. for (i = 0; i < req_exec_cmap_mcast->no_items; i++) {
  790. item = (struct req_exec_cmap_mcast_item *)p;
  791. swab_mar_uint16_t(&item->key_name.length);
  792. swab_mar_size_t(&item->value_len);
  793. switch (item->value_type) {
  794. case ICMAP_VALUETYPE_INT16:
  795. case ICMAP_VALUETYPE_UINT16:
  796. memcpy(&u16, item->value, sizeof(u16));
  797. u16 = swab16(u16);
  798. memcpy(item->value, &u16, sizeof(u16));
  799. break;
  800. case ICMAP_VALUETYPE_INT32:
  801. case ICMAP_VALUETYPE_UINT32:
  802. memcpy(&u32, item->value, sizeof(u32));
  803. u32 = swab32(u32);
  804. memcpy(item->value, &u32, sizeof(u32));
  805. break;
  806. case ICMAP_VALUETYPE_INT64:
  807. case ICMAP_VALUETYPE_UINT64:
  808. memcpy(&u64, item->value, sizeof(u64));
  809. u64 = swab64(u64);
  810. memcpy(item->value, &u64, sizeof(u64));
  811. break;
  812. case ICMAP_VALUETYPE_FLOAT:
  813. memcpy(&flt, item->value, sizeof(flt));
  814. swabflt(&flt);
  815. memcpy(item->value, &flt, sizeof(flt));
  816. break;
  817. case ICMAP_VALUETYPE_DOUBLE:
  818. memcpy(&dbl, item->value, sizeof(dbl));
  819. swabdbl(&dbl);
  820. memcpy(item->value, &dbl, sizeof(dbl));
  821. break;
  822. }
  823. p += MAR_ALIGN_UP(sizeof(*item) + item->value_len, 8);
  824. }
  825. }