cmap.c 26 KB

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