cmap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. /*
  106. * Library Handler Definition
  107. */
  108. static struct corosync_lib_handler cmap_lib_engine[] =
  109. {
  110. { /* 0 */
  111. .lib_handler_fn = message_handler_req_lib_cmap_set,
  112. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  113. },
  114. { /* 1 */
  115. .lib_handler_fn = message_handler_req_lib_cmap_delete,
  116. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  117. },
  118. { /* 2 */
  119. .lib_handler_fn = message_handler_req_lib_cmap_get,
  120. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  121. },
  122. { /* 3 */
  123. .lib_handler_fn = message_handler_req_lib_cmap_adjust_int,
  124. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  125. },
  126. { /* 4 */
  127. .lib_handler_fn = message_handler_req_lib_cmap_iter_init,
  128. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  129. },
  130. { /* 5 */
  131. .lib_handler_fn = message_handler_req_lib_cmap_iter_next,
  132. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  133. },
  134. { /* 6 */
  135. .lib_handler_fn = message_handler_req_lib_cmap_iter_finalize,
  136. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  137. },
  138. { /* 7 */
  139. .lib_handler_fn = message_handler_req_lib_cmap_track_add,
  140. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  141. },
  142. { /* 8 */
  143. .lib_handler_fn = message_handler_req_lib_cmap_track_delete,
  144. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  145. },
  146. };
  147. static struct corosync_exec_handler cmap_exec_engine[] =
  148. {
  149. { /* 0 - MESSAGE_REQ_EXEC_CMAP_MCAST */
  150. .exec_handler_fn = message_handler_req_exec_cmap_mcast,
  151. .exec_endian_convert_fn = exec_cmap_mcast_endian_convert
  152. },
  153. };
  154. struct corosync_service_engine cmap_service_engine = {
  155. .name = "corosync configuration map access",
  156. .id = CMAP_SERVICE,
  157. .priority = 1,
  158. .private_data_size = sizeof(struct cmap_conn_info),
  159. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  160. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  161. .lib_init_fn = cmap_lib_init_fn,
  162. .lib_exit_fn = cmap_lib_exit_fn,
  163. .lib_engine = cmap_lib_engine,
  164. .lib_engine_count = sizeof (cmap_lib_engine) / sizeof (struct corosync_lib_handler),
  165. .exec_init_fn = cmap_exec_init_fn,
  166. .exec_exit_fn = cmap_exec_exit_fn,
  167. .exec_engine = cmap_exec_engine,
  168. .exec_engine_count = sizeof (cmap_exec_engine) / sizeof (struct corosync_exec_handler),
  169. };
  170. struct corosync_service_engine *cmap_get_service_engine_ver0 (void)
  171. {
  172. return (&cmap_service_engine);
  173. }
  174. struct req_exec_cmap_mcast_item {
  175. mar_name_t key_name __attribute__((aligned(8)));
  176. mar_uint8_t value_type __attribute__((aligned(8)));
  177. mar_size_t value_len __attribute__((aligned(8)));
  178. uint8_t value[] __attribute__((aligned(8)));
  179. };
  180. struct req_exec_cmap_mcast {
  181. struct qb_ipc_request_header header __attribute__((aligned(8)));
  182. mar_uint8_t reason __attribute__((aligned(8)));
  183. mar_uint8_t no_items __attribute__((aligned(8)));
  184. mar_uint8_t reserved1 __attribute__((aligned(8)));
  185. mar_uint8_t reserver2 __attribute__((aligned(8)));
  186. /*
  187. * Following are array of req_exec_cmap_mcast_item alligned to 8 bytes
  188. */
  189. };
  190. static int cmap_exec_exit_fn(void)
  191. {
  192. return 0;
  193. }
  194. static char *cmap_exec_init_fn (
  195. struct corosync_api_v1 *corosync_api)
  196. {
  197. api = corosync_api;
  198. return (NULL);
  199. }
  200. static int cmap_lib_init_fn (void *conn)
  201. {
  202. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  203. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p", conn);
  204. api->ipc_refcnt_inc(conn);
  205. memset(conn_info, 0, sizeof(*conn_info));
  206. hdb_create(&conn_info->iter_db);
  207. hdb_create(&conn_info->track_db);
  208. return (0);
  209. }
  210. static int cmap_lib_exit_fn (void *conn)
  211. {
  212. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  213. hdb_handle_t iter_handle = 0;
  214. icmap_iter_t *iter;
  215. hdb_handle_t track_handle = 0;
  216. icmap_track_t *track;
  217. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
  218. hdb_iterator_reset(&conn_info->iter_db);
  219. while (hdb_iterator_next(&conn_info->iter_db,
  220. (void*)&iter, &iter_handle) == 0) {
  221. icmap_iter_finalize(*iter);
  222. (void)hdb_handle_put (&conn_info->iter_db, iter_handle);
  223. }
  224. hdb_destroy(&conn_info->iter_db);
  225. hdb_iterator_reset(&conn_info->track_db);
  226. while (hdb_iterator_next(&conn_info->track_db,
  227. (void*)&track, &track_handle) == 0) {
  228. free(icmap_track_get_user_data(*track));
  229. icmap_track_delete(*track);
  230. (void)hdb_handle_put (&conn_info->track_db, track_handle);
  231. }
  232. hdb_destroy(&conn_info->track_db);
  233. api->ipc_refcnt_dec(conn);
  234. return (0);
  235. }
  236. static void message_handler_req_lib_cmap_set(void *conn, const void *message)
  237. {
  238. const struct req_lib_cmap_set *req_lib_cmap_set = message;
  239. struct res_lib_cmap_set res_lib_cmap_set;
  240. cs_error_t ret;
  241. if (icmap_is_key_ro((char *)req_lib_cmap_set->key_name.value)) {
  242. ret = CS_ERR_ACCESS;
  243. } else {
  244. ret = icmap_set((char *)req_lib_cmap_set->key_name.value, &req_lib_cmap_set->value,
  245. req_lib_cmap_set->value_len, req_lib_cmap_set->type);
  246. }
  247. memset(&res_lib_cmap_set, 0, sizeof(res_lib_cmap_set));
  248. res_lib_cmap_set.header.size = sizeof(res_lib_cmap_set);
  249. res_lib_cmap_set.header.id = MESSAGE_RES_CMAP_SET;
  250. res_lib_cmap_set.header.error = ret;
  251. api->ipc_response_send(conn, &res_lib_cmap_set, sizeof(res_lib_cmap_set));
  252. }
  253. static void message_handler_req_lib_cmap_delete(void *conn, const void *message)
  254. {
  255. const struct req_lib_cmap_set *req_lib_cmap_set = message;
  256. struct res_lib_cmap_delete res_lib_cmap_delete;
  257. cs_error_t ret;
  258. if (icmap_is_key_ro((char *)req_lib_cmap_set->key_name.value)) {
  259. ret = CS_ERR_ACCESS;
  260. } else {
  261. ret = icmap_delete((char *)req_lib_cmap_set->key_name.value);
  262. }
  263. memset(&res_lib_cmap_delete, 0, sizeof(res_lib_cmap_delete));
  264. res_lib_cmap_delete.header.size = sizeof(res_lib_cmap_delete);
  265. res_lib_cmap_delete.header.id = MESSAGE_RES_CMAP_DELETE;
  266. res_lib_cmap_delete.header.error = ret;
  267. api->ipc_response_send(conn, &res_lib_cmap_delete, sizeof(res_lib_cmap_delete));
  268. }
  269. static void message_handler_req_lib_cmap_get(void *conn, const void *message)
  270. {
  271. const struct req_lib_cmap_get *req_lib_cmap_get = message;
  272. struct res_lib_cmap_get *res_lib_cmap_get;
  273. struct res_lib_cmap_get error_res_lib_cmap_get;
  274. cs_error_t ret;
  275. size_t value_len;
  276. size_t res_lib_cmap_get_size;
  277. icmap_value_types_t type;
  278. void *value;
  279. value_len = req_lib_cmap_get->value_len;
  280. res_lib_cmap_get_size = sizeof(*res_lib_cmap_get) + value_len;
  281. res_lib_cmap_get = malloc(res_lib_cmap_get_size);
  282. if (res_lib_cmap_get == NULL) {
  283. ret = CS_ERR_NO_MEMORY;
  284. goto error_exit;
  285. }
  286. memset(res_lib_cmap_get, 0, res_lib_cmap_get_size);
  287. if (value_len > 0) {
  288. value = res_lib_cmap_get->value;
  289. } else {
  290. value = NULL;
  291. }
  292. ret = icmap_get((char *)req_lib_cmap_get->key_name.value,
  293. value,
  294. &value_len,
  295. &type);
  296. if (ret != CS_OK) {
  297. free(res_lib_cmap_get);
  298. goto error_exit;
  299. }
  300. res_lib_cmap_get->header.size = res_lib_cmap_get_size;
  301. res_lib_cmap_get->header.id = MESSAGE_RES_CMAP_GET;
  302. res_lib_cmap_get->header.error = ret;
  303. res_lib_cmap_get->type = type;
  304. res_lib_cmap_get->value_len = value_len;
  305. api->ipc_response_send(conn, res_lib_cmap_get, res_lib_cmap_get_size);
  306. free(res_lib_cmap_get);
  307. return ;
  308. error_exit:
  309. memset(&error_res_lib_cmap_get, 0, sizeof(error_res_lib_cmap_get));
  310. error_res_lib_cmap_get.header.size = sizeof(error_res_lib_cmap_get);
  311. error_res_lib_cmap_get.header.id = MESSAGE_RES_CMAP_GET;
  312. error_res_lib_cmap_get.header.error = ret;
  313. api->ipc_response_send(conn, &error_res_lib_cmap_get, sizeof(error_res_lib_cmap_get));
  314. }
  315. static void message_handler_req_lib_cmap_adjust_int(void *conn, const void *message)
  316. {
  317. const struct req_lib_cmap_adjust_int *req_lib_cmap_adjust_int = message;
  318. struct res_lib_cmap_adjust_int res_lib_cmap_adjust_int;
  319. cs_error_t ret;
  320. if (icmap_is_key_ro((char *)req_lib_cmap_adjust_int->key_name.value)) {
  321. ret = CS_ERR_ACCESS;
  322. } else {
  323. ret = icmap_adjust_int((char *)req_lib_cmap_adjust_int->key_name.value,
  324. req_lib_cmap_adjust_int->step);
  325. }
  326. memset(&res_lib_cmap_adjust_int, 0, sizeof(res_lib_cmap_adjust_int));
  327. res_lib_cmap_adjust_int.header.size = sizeof(res_lib_cmap_adjust_int);
  328. res_lib_cmap_adjust_int.header.id = MESSAGE_RES_CMAP_ADJUST_INT;
  329. res_lib_cmap_adjust_int.header.error = ret;
  330. api->ipc_response_send(conn, &res_lib_cmap_adjust_int, sizeof(res_lib_cmap_adjust_int));
  331. }
  332. static void message_handler_req_lib_cmap_iter_init(void *conn, const void *message)
  333. {
  334. const struct req_lib_cmap_iter_init *req_lib_cmap_iter_init = message;
  335. struct res_lib_cmap_iter_init res_lib_cmap_iter_init;
  336. cs_error_t ret;
  337. icmap_iter_t iter;
  338. icmap_iter_t *hdb_iter;
  339. cmap_iter_handle_t handle = 0ULL;
  340. const char *prefix;
  341. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  342. if (req_lib_cmap_iter_init->prefix.length > 0) {
  343. prefix = (char *)req_lib_cmap_iter_init->prefix.value;
  344. } else {
  345. prefix = NULL;
  346. }
  347. iter = icmap_iter_init(prefix);
  348. if (iter == NULL) {
  349. ret = CS_ERR_NO_SECTIONS;
  350. goto reply_send;
  351. }
  352. ret = hdb_error_to_cs(hdb_handle_create(&conn_info->iter_db, sizeof(iter), &handle));
  353. if (ret != CS_OK) {
  354. goto reply_send;
  355. }
  356. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db, handle, (void *)&hdb_iter));
  357. if (ret != CS_OK) {
  358. goto reply_send;
  359. }
  360. *hdb_iter = iter;
  361. (void)hdb_handle_put (&conn_info->iter_db, handle);
  362. reply_send:
  363. memset(&res_lib_cmap_iter_init, 0, sizeof(res_lib_cmap_iter_init));
  364. res_lib_cmap_iter_init.header.size = sizeof(res_lib_cmap_iter_init);
  365. res_lib_cmap_iter_init.header.id = MESSAGE_RES_CMAP_ITER_INIT;
  366. res_lib_cmap_iter_init.header.error = ret;
  367. res_lib_cmap_iter_init.iter_handle = handle;
  368. api->ipc_response_send(conn, &res_lib_cmap_iter_init, sizeof(res_lib_cmap_iter_init));
  369. }
  370. static void message_handler_req_lib_cmap_iter_next(void *conn, const void *message)
  371. {
  372. const struct req_lib_cmap_iter_next *req_lib_cmap_iter_next = message;
  373. struct res_lib_cmap_iter_next res_lib_cmap_iter_next;
  374. cs_error_t ret;
  375. icmap_iter_t *iter;
  376. size_t value_len;
  377. icmap_value_types_t type;
  378. const char *res = NULL;
  379. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  380. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db,
  381. req_lib_cmap_iter_next->iter_handle, (void *)&iter));
  382. if (ret != CS_OK) {
  383. goto reply_send;
  384. }
  385. res = icmap_iter_next(*iter, &value_len, &type);
  386. if (res == NULL) {
  387. ret = CS_ERR_NO_SECTIONS;
  388. }
  389. (void)hdb_handle_put (&conn_info->iter_db, req_lib_cmap_iter_next->iter_handle);
  390. reply_send:
  391. memset(&res_lib_cmap_iter_next, 0, sizeof(res_lib_cmap_iter_next));
  392. res_lib_cmap_iter_next.header.size = sizeof(res_lib_cmap_iter_next);
  393. res_lib_cmap_iter_next.header.id = MESSAGE_RES_CMAP_ITER_NEXT;
  394. res_lib_cmap_iter_next.header.error = ret;
  395. if (res != NULL) {
  396. res_lib_cmap_iter_next.value_len = value_len;
  397. res_lib_cmap_iter_next.type = type;
  398. memcpy(res_lib_cmap_iter_next.key_name.value, res, strlen(res));
  399. res_lib_cmap_iter_next.key_name.length = strlen(res);
  400. }
  401. api->ipc_response_send(conn, &res_lib_cmap_iter_next, sizeof(res_lib_cmap_iter_next));
  402. }
  403. static void message_handler_req_lib_cmap_iter_finalize(void *conn, const void *message)
  404. {
  405. const struct req_lib_cmap_iter_finalize *req_lib_cmap_iter_finalize = message;
  406. struct res_lib_cmap_iter_finalize res_lib_cmap_iter_finalize;
  407. cs_error_t ret;
  408. icmap_iter_t *iter;
  409. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  410. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db,
  411. req_lib_cmap_iter_finalize->iter_handle, (void *)&iter));
  412. if (ret != CS_OK) {
  413. goto reply_send;
  414. }
  415. icmap_iter_finalize(*iter);
  416. (void)hdb_handle_destroy(&conn_info->iter_db, req_lib_cmap_iter_finalize->iter_handle);
  417. (void)hdb_handle_put (&conn_info->iter_db, req_lib_cmap_iter_finalize->iter_handle);
  418. reply_send:
  419. memset(&res_lib_cmap_iter_finalize, 0, sizeof(res_lib_cmap_iter_finalize));
  420. res_lib_cmap_iter_finalize.header.size = sizeof(res_lib_cmap_iter_finalize);
  421. res_lib_cmap_iter_finalize.header.id = MESSAGE_RES_CMAP_ITER_FINALIZE;
  422. res_lib_cmap_iter_finalize.header.error = ret;
  423. api->ipc_response_send(conn, &res_lib_cmap_iter_finalize, sizeof(res_lib_cmap_iter_finalize));
  424. }
  425. static void cmap_notify_fn(int32_t event,
  426. const char *key_name,
  427. struct icmap_notify_value new_val,
  428. struct icmap_notify_value old_val,
  429. void *user_data)
  430. {
  431. struct cmap_track_user_data *cmap_track_user_data = (struct cmap_track_user_data *)user_data;
  432. struct res_lib_cmap_notify_callback res_lib_cmap_notify_callback;
  433. struct iovec iov[3];
  434. memset(&res_lib_cmap_notify_callback, 0, sizeof(res_lib_cmap_notify_callback));
  435. res_lib_cmap_notify_callback.header.size = sizeof(res_lib_cmap_notify_callback) + new_val.len + old_val.len;
  436. res_lib_cmap_notify_callback.header.id = MESSAGE_RES_CMAP_NOTIFY_CALLBACK;
  437. res_lib_cmap_notify_callback.header.error = CS_OK;
  438. res_lib_cmap_notify_callback.new_value_type = new_val.type;
  439. res_lib_cmap_notify_callback.old_value_type = old_val.type;
  440. res_lib_cmap_notify_callback.new_value_len = new_val.len;
  441. res_lib_cmap_notify_callback.old_value_len = old_val.len;
  442. res_lib_cmap_notify_callback.event = event;
  443. res_lib_cmap_notify_callback.key_name.length = strlen(key_name);
  444. res_lib_cmap_notify_callback.track_inst_handle = cmap_track_user_data->track_inst_handle;
  445. memcpy(res_lib_cmap_notify_callback.key_name.value, key_name, strlen(key_name));
  446. iov[0].iov_base = (char *)&res_lib_cmap_notify_callback;
  447. iov[0].iov_len = sizeof(res_lib_cmap_notify_callback);
  448. iov[1].iov_base = (char *)new_val.data;
  449. iov[1].iov_len = new_val.len;
  450. iov[2].iov_base = (char *)old_val.data;
  451. iov[2].iov_len = old_val.len;
  452. api->ipc_dispatch_iov_send(cmap_track_user_data->conn, iov, 3);
  453. }
  454. static void message_handler_req_lib_cmap_track_add(void *conn, const void *message)
  455. {
  456. const struct req_lib_cmap_track_add *req_lib_cmap_track_add = message;
  457. struct res_lib_cmap_track_add res_lib_cmap_track_add;
  458. cs_error_t ret;
  459. cmap_track_handle_t handle = 0;
  460. icmap_track_t track = NULL;
  461. icmap_track_t *hdb_track;
  462. struct cmap_track_user_data *cmap_track_user_data;
  463. const char *key_name;
  464. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  465. cmap_track_user_data = malloc(sizeof(*cmap_track_user_data));
  466. if (cmap_track_user_data == NULL) {
  467. ret = CS_ERR_NO_MEMORY;
  468. goto reply_send;
  469. }
  470. memset(cmap_track_user_data, 0, sizeof(*cmap_track_user_data));
  471. if (req_lib_cmap_track_add->key_name.length > 0) {
  472. key_name = (char *)req_lib_cmap_track_add->key_name.value;
  473. } else {
  474. key_name = NULL;
  475. }
  476. ret = icmap_track_add(key_name,
  477. req_lib_cmap_track_add->track_type,
  478. cmap_notify_fn,
  479. cmap_track_user_data,
  480. &track);
  481. if (ret != CS_OK) {
  482. free(cmap_track_user_data);
  483. goto reply_send;
  484. }
  485. ret = hdb_error_to_cs(hdb_handle_create(&conn_info->track_db, sizeof(track), &handle));
  486. if (ret != CS_OK) {
  487. free(cmap_track_user_data);
  488. goto reply_send;
  489. }
  490. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->track_db, handle, (void *)&hdb_track));
  491. if (ret != CS_OK) {
  492. free(cmap_track_user_data);
  493. goto reply_send;
  494. }
  495. *hdb_track = track;
  496. cmap_track_user_data->conn = conn;
  497. cmap_track_user_data->track_handle = handle;
  498. cmap_track_user_data->track_inst_handle = req_lib_cmap_track_add->track_inst_handle;
  499. (void)hdb_handle_put (&conn_info->track_db, handle);
  500. reply_send:
  501. memset(&res_lib_cmap_track_add, 0, sizeof(res_lib_cmap_track_add));
  502. res_lib_cmap_track_add.header.size = sizeof(res_lib_cmap_track_add);
  503. res_lib_cmap_track_add.header.id = MESSAGE_RES_CMAP_TRACK_ADD;
  504. res_lib_cmap_track_add.header.error = ret;
  505. res_lib_cmap_track_add.track_handle = handle;
  506. api->ipc_response_send(conn, &res_lib_cmap_track_add, sizeof(res_lib_cmap_track_add));
  507. }
  508. static void message_handler_req_lib_cmap_track_delete(void *conn, const void *message)
  509. {
  510. const struct req_lib_cmap_track_delete *req_lib_cmap_track_delete = message;
  511. struct res_lib_cmap_track_delete res_lib_cmap_track_delete;
  512. cs_error_t ret;
  513. icmap_track_t *track;
  514. struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
  515. uint64_t track_inst_handle = 0;
  516. ret = hdb_error_to_cs(hdb_handle_get(&conn_info->track_db,
  517. req_lib_cmap_track_delete->track_handle, (void *)&track));
  518. if (ret != CS_OK) {
  519. goto reply_send;
  520. }
  521. track_inst_handle = ((struct cmap_track_user_data *)icmap_track_get_user_data(*track))->track_inst_handle;
  522. free(icmap_track_get_user_data(*track));
  523. ret = icmap_track_delete(*track);
  524. (void)hdb_handle_put (&conn_info->track_db, req_lib_cmap_track_delete->track_handle);
  525. (void)hdb_handle_destroy(&conn_info->track_db, req_lib_cmap_track_delete->track_handle);
  526. reply_send:
  527. memset(&res_lib_cmap_track_delete, 0, sizeof(res_lib_cmap_track_delete));
  528. res_lib_cmap_track_delete.header.size = sizeof(res_lib_cmap_track_delete);
  529. res_lib_cmap_track_delete.header.id = MESSAGE_RES_CMAP_TRACK_DELETE;
  530. res_lib_cmap_track_delete.header.error = ret;
  531. res_lib_cmap_track_delete.track_inst_handle = track_inst_handle;
  532. api->ipc_response_send(conn, &res_lib_cmap_track_delete, sizeof(res_lib_cmap_track_delete));
  533. }
  534. static cs_error_t cmap_mcast_send(enum cmap_mcast_reason reason, int argc, char *argv[])
  535. {
  536. int i;
  537. size_t value_len;
  538. icmap_value_types_t value_type;
  539. cs_error_t err;
  540. size_t item_len;
  541. size_t msg_len = 0;
  542. struct req_exec_cmap_mcast req_exec_cmap_mcast;
  543. struct req_exec_cmap_mcast_item *item;
  544. struct iovec req_exec_cmap_iovec[MAX_REQ_EXEC_CMAP_MCAST_ITEMS + 1];
  545. ENTER();
  546. if (argc > MAX_REQ_EXEC_CMAP_MCAST_ITEMS) {
  547. return (CS_ERR_TOO_MANY_GROUPS);
  548. }
  549. memset(req_exec_cmap_iovec, 0, sizeof(req_exec_cmap_iovec));
  550. for (i = 0; i < argc; i++) {
  551. err = icmap_get(argv[i], NULL, &value_len, &value_type);
  552. if (err != CS_OK && err != CS_ERR_NOT_EXIST) {
  553. goto free_mem;
  554. }
  555. if (err == CS_ERR_NOT_EXIST) {
  556. value_type = ICMAP_VALUETYPE_NOT_EXIST;
  557. value_len = 0;
  558. }
  559. item_len = MAR_ALIGN_UP(sizeof(*item) + value_len, 8);
  560. item = malloc(item_len);
  561. if (item == NULL) {
  562. goto free_mem;
  563. }
  564. memset(item, 0, sizeof(item_len));
  565. item->value_type = value_type;
  566. item->value_len = value_len;
  567. item->key_name.length = strlen(argv[i]);
  568. strcpy((char *)item->key_name.value, argv[i]);
  569. if (value_type != ICMAP_VALUETYPE_NOT_EXIST) {
  570. err = icmap_get(argv[i], item->value, &value_len, &value_type);
  571. if (err != CS_OK) {
  572. goto free_mem;
  573. }
  574. }
  575. req_exec_cmap_iovec[i + 1].iov_base = item;
  576. req_exec_cmap_iovec[i + 1].iov_len = item_len;
  577. msg_len += item_len;
  578. qb_log(LOG_TRACE, "Item %u - type %u, len %zu", i, item->value_type, item->value_len);
  579. item = NULL;
  580. }
  581. memset(&req_exec_cmap_mcast, 0, sizeof(req_exec_cmap_mcast));
  582. req_exec_cmap_mcast.header.size = sizeof(req_exec_cmap_mcast) + msg_len;
  583. req_exec_cmap_mcast.reason = reason;
  584. req_exec_cmap_mcast.no_items = argc;
  585. req_exec_cmap_iovec[0].iov_base = &req_exec_cmap_mcast;
  586. req_exec_cmap_iovec[0].iov_len = sizeof(req_exec_cmap_mcast);
  587. qb_log(LOG_TRACE, "Sending %u items (%u iovec) for reason %u", argc, argc + 1, reason);
  588. err = (api->totem_mcast(req_exec_cmap_iovec, argc + 1, TOTEM_AGREED) == 0 ? CS_OK : CS_ERR_MESSAGE_ERROR);
  589. free_mem:
  590. for (i = 0; i < argc; i++) {
  591. free(req_exec_cmap_iovec[i + 1].iov_base);
  592. }
  593. free(item);
  594. LEAVE();
  595. return (err);
  596. }
  597. static struct req_exec_cmap_mcast_item *cmap_mcast_item_find(
  598. const void *message,
  599. char *key)
  600. {
  601. const struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
  602. int i;
  603. const char *p;
  604. struct req_exec_cmap_mcast_item *item;
  605. mar_uint16_t key_name_len;
  606. p = (const char *)message + sizeof(*req_exec_cmap_mcast);
  607. for (i = 0; i < req_exec_cmap_mcast->no_items; i++) {
  608. item = (struct req_exec_cmap_mcast_item *)p;
  609. key_name_len = item->key_name.length;
  610. if (strlen(key) == key_name_len && strcmp((char *)item->key_name.value, key) == 0) {
  611. return (item);
  612. }
  613. p += MAR_ALIGN_UP(sizeof(*item) + item->value_len, 8);
  614. }
  615. return (NULL);
  616. }
  617. static void message_handler_req_exec_cmap_mcast_reason_sync(
  618. const void *message,
  619. unsigned int nodeid)
  620. {
  621. uint64_t config_version = 0;
  622. struct req_exec_cmap_mcast_item *item;
  623. mar_size_t value_len;
  624. ENTER();
  625. item = cmap_mcast_item_find(message, (char *)"totem.config_version");
  626. if (item != NULL) {
  627. value_len = item->value_len;
  628. if (item->value_type == ICMAP_VALUETYPE_NOT_EXIST) {
  629. config_version = 0;
  630. }
  631. if (item->value_type == ICMAP_VALUETYPE_UINT64) {
  632. memcpy(&config_version, item->value, value_len);
  633. }
  634. }
  635. qb_log(LOG_TRACE, "Received config version %"PRIu64" from node %x", config_version, nodeid);
  636. LEAVE();
  637. }
  638. static void message_handler_req_exec_cmap_mcast(
  639. const void *message,
  640. unsigned int nodeid)
  641. {
  642. const struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
  643. ENTER();
  644. switch (req_exec_cmap_mcast->reason) {
  645. case CMAP_MCAST_REASON_SYNC:
  646. message_handler_req_exec_cmap_mcast_reason_sync(message, nodeid);
  647. break;
  648. default:
  649. qb_log(LOG_TRACE, "Received mcast with unknown reason %u", req_exec_cmap_mcast->reason);
  650. };
  651. LEAVE();
  652. }
  653. static void exec_cmap_mcast_endian_convert(void *message)
  654. {
  655. struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
  656. const char *p;
  657. int i;
  658. struct req_exec_cmap_mcast_item *item;
  659. uint16_t u16;
  660. uint32_t u32;
  661. uint64_t u64;
  662. float flt;
  663. double dbl;
  664. swab_coroipc_request_header_t(&req_exec_cmap_mcast->header);
  665. p = (const char *)message + sizeof(*req_exec_cmap_mcast);
  666. for (i = 0; i < req_exec_cmap_mcast->no_items; i++) {
  667. item = (struct req_exec_cmap_mcast_item *)p;
  668. swab_mar_uint16_t(&item->key_name.length);
  669. swab_mar_size_t(&item->value_len);
  670. switch (item->value_type) {
  671. case ICMAP_VALUETYPE_INT16:
  672. case ICMAP_VALUETYPE_UINT16:
  673. memcpy(&u16, item->value, sizeof(u16));
  674. u16 = swab16(u16);
  675. memcpy(item->value, &u16, sizeof(u16));
  676. break;
  677. case ICMAP_VALUETYPE_INT32:
  678. case ICMAP_VALUETYPE_UINT32:
  679. memcpy(&u32, item->value, sizeof(u32));
  680. u32 = swab32(u32);
  681. memcpy(item->value, &u32, sizeof(u32));
  682. break;
  683. case ICMAP_VALUETYPE_INT64:
  684. case ICMAP_VALUETYPE_UINT64:
  685. memcpy(&u64, item->value, sizeof(u64));
  686. u64 = swab64(u64);
  687. memcpy(item->value, &u64, sizeof(u64));
  688. break;
  689. case ICMAP_VALUETYPE_FLOAT:
  690. memcpy(&flt, item->value, sizeof(flt));
  691. swabflt(&flt);
  692. memcpy(item->value, &flt, sizeof(flt));
  693. break;
  694. case ICMAP_VALUETYPE_DOUBLE:
  695. memcpy(&dbl, item->value, sizeof(dbl));
  696. swabdbl(&dbl);
  697. memcpy(item->value, &dbl, sizeof(dbl));
  698. break;
  699. }
  700. p += MAR_ALIGN_UP(sizeof(*item) + item->value_len, 8);
  701. }
  702. }