qdevice-cmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * Copyright (c) 2015-2026 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 <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <err.h>
  37. #include <poll.h>
  38. #include <stdio.h>
  39. #include <stdint.h>
  40. #include <netdb.h>
  41. #include "log.h"
  42. #include "log-corosync.h"
  43. #include "qdevice-config.h"
  44. #include "qdevice-cmap.h"
  45. #include "qdevice-log.h"
  46. #include "log-common.h"
  47. #include "qdevice-model.h"
  48. #include "utils.h"
  49. static uint32_t
  50. qdevice_cmap_autogenerate_node_id(const char *addr, int clear_node_high_bit)
  51. {
  52. struct addrinfo *ainfo;
  53. struct addrinfo ahints;
  54. int ret, i;
  55. memset(&ahints, 0, sizeof(ahints));
  56. ahints.ai_socktype = SOCK_DGRAM;
  57. ahints.ai_protocol = IPPROTO_UDP;
  58. /*
  59. * Hardcoded AF_INET because autogenerated nodeid is valid only for ipv4
  60. */
  61. ahints.ai_family = AF_INET;
  62. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  63. if (ret != 0)
  64. return (0);
  65. if (ainfo->ai_family != AF_INET) {
  66. freeaddrinfo(ainfo);
  67. return (0);
  68. }
  69. memcpy(&i, &(((struct sockaddr_in *)((void *)ainfo->ai_addr))->sin_addr), sizeof(struct in_addr));
  70. freeaddrinfo(ainfo);
  71. ret = htonl(i);
  72. if (clear_node_high_bit) {
  73. ret &= 0x7FFFFFFF;
  74. }
  75. return (ret);
  76. }
  77. int
  78. qdevice_cmap_get_nodelist(cmap_handle_t cmap_handle, struct node_list *list)
  79. {
  80. cs_error_t cs_err;
  81. cmap_iter_handle_t iter_handle;
  82. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  83. char ring0_addr_key[CMAP_KEYNAME_MAXLEN + 1];
  84. char tmp_key[CMAP_KEYNAME_MAXLEN + 1];
  85. int res;
  86. int ret_value;
  87. unsigned int node_pos;
  88. uint32_t node_id;
  89. uint32_t data_center_id;
  90. char *tmp_str;
  91. char *addr0_str;
  92. int clear_node_high_bit;
  93. ret_value = 0;
  94. node_list_init(list);
  95. /*
  96. * Fill clear high node bit
  97. */
  98. clear_node_high_bit = 0;
  99. if (cmap_get_string(cmap_handle, "totem.clear_node_high_bit", &tmp_str) == CS_OK) {
  100. if (strcmp (tmp_str, "yes") == 0) {
  101. clear_node_high_bit = 1;
  102. }
  103. free(tmp_str);
  104. }
  105. /*
  106. * Iterate nodelist
  107. */
  108. cs_err = cmap_iter_init(cmap_handle, "nodelist.node.", &iter_handle);
  109. if (cs_err != CS_OK) {
  110. return (-1);
  111. }
  112. while ((cs_err = cmap_iter_next(cmap_handle, iter_handle, key_name, NULL, NULL)) == CS_OK) {
  113. res = sscanf(key_name, "nodelist.node.%u.%s", &node_pos, tmp_key);
  114. if (res != 2) {
  115. continue;
  116. }
  117. if (strcmp(tmp_key, "ring0_addr") != 0 && strcmp(tmp_key, "name") != 0) {
  118. continue;
  119. }
  120. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  121. cs_err = cmap_get_uint32(cmap_handle, tmp_key, &node_id);
  122. if (cs_err == CS_ERR_NOT_EXIST) {
  123. /*
  124. * Nodeid doesn't exists -> autogenerate node id
  125. */
  126. /*
  127. * New corosync supports configuration without ring0_addr but then
  128. * nodeid has to be specified (not needed for corosync but qdevice
  129. * needs information about all nodes) so fail when it's not possible to
  130. * read ring0_addr
  131. */
  132. snprintf(ring0_addr_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr",
  133. node_pos);
  134. if (cmap_get_string(cmap_handle, ring0_addr_key, &addr0_str) != CS_OK) {
  135. log(LOG_ERR, "Cant find nodeid or ring0_addr for a node. "
  136. "One of them needs to be defined for every node.");
  137. return (-1);
  138. }
  139. node_id = qdevice_cmap_autogenerate_node_id(addr0_str,
  140. clear_node_high_bit);
  141. free(addr0_str);
  142. } else if (cs_err != CS_OK) {
  143. ret_value = -1;
  144. goto iter_finalize;
  145. }
  146. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.datacenterid", node_pos);
  147. if (cmap_get_uint32(cmap_handle, tmp_key, &data_center_id) != CS_OK) {
  148. data_center_id = 0;
  149. }
  150. if (node_list_find_node_id(list, node_id) != NULL) {
  151. /*
  152. * Node is already in the list (both ring0_addr and name exists)
  153. */
  154. continue ;
  155. }
  156. if (node_list_add(list, node_id, data_center_id, TLV_NODE_STATE_NOT_SET) == NULL) {
  157. ret_value = -1;
  158. goto iter_finalize;
  159. }
  160. }
  161. if (node_list_is_empty(list)) {
  162. log(LOG_ERR, "No configured nodes found - configuration without node list is not supported");
  163. ret_value = -1;
  164. goto iter_finalize;
  165. } else {
  166. log(LOG_DEBUG, "Configuration node list:");
  167. log_common_debug_dump_node_list(list);
  168. }
  169. iter_finalize:
  170. cmap_iter_finalize(cmap_handle, iter_handle);
  171. if (ret_value != 0) {
  172. node_list_free(list);
  173. }
  174. return (ret_value);
  175. }
  176. int
  177. qdevice_cmap_get_config_version(cmap_handle_t cmap_handle, uint64_t *config_version)
  178. {
  179. int res;
  180. if (cmap_get_uint64(cmap_handle, "totem.config_version", config_version) == CS_OK) {
  181. res = 0;
  182. } else {
  183. *config_version = 0;
  184. res = -1;
  185. }
  186. return (res);
  187. }
  188. int
  189. qdevice_cmap_store_config_node_list(struct qdevice_instance *instance)
  190. {
  191. int res;
  192. node_list_free(&instance->config_node_list);
  193. if (qdevice_cmap_get_nodelist(instance->cmap_handle, &instance->config_node_list) != 0) {
  194. log(LOG_ERR, "Can't get configuration node list.");
  195. return (-1);
  196. }
  197. res = qdevice_cmap_get_config_version(instance->cmap_handle, &instance->config_node_list_version);
  198. instance->config_node_list_version_set = (res == 0);
  199. return (0);
  200. }
  201. void
  202. qdevice_cmap_init(struct qdevice_instance *instance)
  203. {
  204. cs_error_t res;
  205. int no_retries;
  206. no_retries = 0;
  207. while ((res = cmap_initialize(&instance->cmap_handle)) == CS_ERR_TRY_AGAIN &&
  208. no_retries++ < instance->advanced_settings->max_cs_try_again) {
  209. (void)poll(NULL, 0, 1000);
  210. }
  211. if (res != CS_OK) {
  212. log_corosync_errx(EXIT_FAILURE, res, "Failed to initialize the cmap API");
  213. }
  214. if ((res = cmap_context_set(instance->cmap_handle, (void *)instance)) != CS_OK) {
  215. log_corosync_errx(EXIT_FAILURE, res, "Can't set cmap context");
  216. }
  217. cmap_fd_get(instance->cmap_handle, &instance->cmap_poll_fd);
  218. }
  219. static void
  220. qdevice_cmap_node_list_event(struct qdevice_instance *instance)
  221. {
  222. struct node_list nlist;
  223. int config_version_set;
  224. uint64_t config_version;
  225. log(LOG_DEBUG, "Node list configuration possibly changed");
  226. if (qdevice_cmap_get_nodelist(instance->cmap_handle, &nlist) != 0) {
  227. log(LOG_ERR, "Can't get configuration node list.");
  228. if (qdevice_model_get_config_node_list_failed(instance) != 0) {
  229. log(LOG_DEBUG, "qdevice_model_get_config_node_list_failed returned error -> exit");
  230. exit(EXIT_FAILURE);
  231. }
  232. return ;
  233. }
  234. config_version_set = (qdevice_cmap_get_config_version(instance->cmap_handle,
  235. &config_version) == 0);
  236. if (node_list_eq(&instance->config_node_list, &nlist)) {
  237. return ;
  238. }
  239. log(LOG_DEBUG, "Node list changed");
  240. if (config_version_set) {
  241. log(LOG_DEBUG, " config_version = "UTILS_PRI_CONFIG_VERSION, config_version);
  242. }
  243. log_common_debug_dump_node_list(&nlist);
  244. if (qdevice_model_config_node_list_changed(instance, &nlist,
  245. config_version_set, config_version) != 0) {
  246. log(LOG_DEBUG, "qdevice_model_config_node_list_changed returned error -> exit");
  247. exit(EXIT_FAILURE);
  248. }
  249. node_list_free(&instance->config_node_list);
  250. if (node_list_clone(&instance->config_node_list, &nlist) != 0) {
  251. log(LOG_ERR, "Can't allocate instance->config_node_list clone");
  252. node_list_free(&nlist);
  253. if (qdevice_model_get_config_node_list_failed(instance) != 0) {
  254. log(LOG_DEBUG, "qdevice_model_get_config_node_list_failed returned error -> exit");
  255. exit(EXIT_FAILURE);
  256. }
  257. return ;
  258. }
  259. instance->config_node_list_version_set = config_version_set;
  260. if (config_version_set) {
  261. instance->config_node_list_version = config_version;
  262. }
  263. }
  264. static void
  265. qdevice_cmap_logging_event(struct qdevice_instance *instance)
  266. {
  267. log(LOG_DEBUG, "Logging configuration possibly changed");
  268. qdevice_log_configure(instance);
  269. }
  270. static void
  271. qdevice_cmap_heuristics_event(struct qdevice_instance *instance)
  272. {
  273. log(LOG_DEBUG, "Heuristics configuration possibly changed");
  274. if (qdevice_instance_configure_from_cmap_heuristics(instance) != 0) {
  275. log(LOG_DEBUG, "qdevice_instance_configure_from_cmap_heuristics returned error -> exit");
  276. exit(EXIT_FAILURE);
  277. }
  278. }
  279. static void
  280. qdevice_cmap_reload_cb(cmap_handle_t cmap_handle, cmap_track_handle_t cmap_track_handle,
  281. int32_t event, const char *key_name,
  282. struct cmap_notify_value new_value, struct cmap_notify_value old_value,
  283. void *user_data)
  284. {
  285. cs_error_t cs_res;
  286. uint8_t reload;
  287. struct qdevice_instance *instance;
  288. const char *node_list_prefix_str;
  289. const char *logging_prefix_str;
  290. const char *heuristics_prefix_str;
  291. struct qdevice_cmap_change_events events;
  292. memset(&events, 0, sizeof(events));
  293. node_list_prefix_str = "nodelist.";
  294. logging_prefix_str = "logging.";
  295. heuristics_prefix_str = "quorum.device.heuristics.";
  296. if ((cs_res = cmap_context_get(cmap_handle, (const void **)&instance)) != CS_OK) {
  297. log_corosync(LOG_ERR, cs_res, "Fatal error. Can't get cmap context");
  298. exit(EXIT_FAILURE);
  299. }
  300. /*
  301. * Wait for full reload
  302. */
  303. if (strcmp(key_name, "config.totemconfig_reload_in_progress") == 0 &&
  304. new_value.type == CMAP_VALUETYPE_UINT8 && new_value.len == sizeof(reload)) {
  305. reload = 1;
  306. if (memcmp(new_value.data, &reload, sizeof(reload)) == 0) {
  307. /*
  308. * Ignore nodelist changes
  309. */
  310. instance->cmap_reload_in_progress = 1;
  311. return ;
  312. } else {
  313. instance->cmap_reload_in_progress = 0;
  314. events.node_list = 1;
  315. events.logging = 1;
  316. events.heuristics = 1;
  317. }
  318. }
  319. if (instance->cmap_reload_in_progress) {
  320. return ;
  321. }
  322. if (((cs_res = cmap_get_uint8(cmap_handle, "config.totemconfig_reload_in_progress",
  323. &reload)) == CS_OK) && reload == 1) {
  324. return ;
  325. }
  326. if (strncmp(key_name, node_list_prefix_str, strlen(node_list_prefix_str)) == 0) {
  327. events.node_list = 1;
  328. }
  329. if (strncmp(key_name, logging_prefix_str, strlen(logging_prefix_str)) == 0) {
  330. events.logging = 1;
  331. }
  332. if (strncmp(key_name, heuristics_prefix_str, strlen(heuristics_prefix_str)) == 0) {
  333. events.heuristics = 1;
  334. }
  335. if (events.logging) {
  336. qdevice_cmap_logging_event(instance);
  337. }
  338. if (events.node_list) {
  339. qdevice_cmap_node_list_event(instance);
  340. }
  341. if (events.heuristics) {
  342. qdevice_cmap_heuristics_event(instance);
  343. }
  344. /*
  345. * Inform model about change
  346. */
  347. if (qdevice_model_cmap_changed(instance, &events) != 0) {
  348. log(LOG_DEBUG, "qdevice_model_cmap_changed returned error -> exit");
  349. exit(EXIT_FAILURE);
  350. }
  351. }
  352. int
  353. qdevice_cmap_add_track(struct qdevice_instance *instance)
  354. {
  355. cs_error_t res;
  356. res = cmap_track_add(instance->cmap_handle, "config.totemconfig_reload_in_progress",
  357. CMAP_TRACK_ADD | CMAP_TRACK_MODIFY, qdevice_cmap_reload_cb,
  358. NULL, &instance->cmap_reload_track_handle);
  359. if (res != CS_OK) {
  360. log_corosync(LOG_ERR, res, "Can't initialize cmap totemconfig_reload_in_progress tracking");
  361. return (-1);
  362. }
  363. res = cmap_track_add(instance->cmap_handle, "nodelist.",
  364. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  365. qdevice_cmap_reload_cb,
  366. NULL, &instance->cmap_nodelist_track_handle);
  367. if (res != CS_OK) {
  368. log_corosync(LOG_ERR, res, "Can't initialize cmap nodelist tracking");
  369. return (-1);
  370. }
  371. res = cmap_track_add(instance->cmap_handle, "logging.",
  372. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  373. qdevice_cmap_reload_cb,
  374. NULL, &instance->cmap_logging_track_handle);
  375. if (res != CS_OK) {
  376. log_corosync(LOG_ERR, res, "Can't initialize logging tracking");
  377. return (-1);
  378. }
  379. res = cmap_track_add(instance->cmap_handle, "quorum.device.heuristics.",
  380. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  381. qdevice_cmap_reload_cb,
  382. NULL, &instance->cmap_heuristics_track_handle);
  383. if (res != CS_OK) {
  384. log_corosync(LOG_ERR, res, "Can't initialize qdevice heuristics tracking");
  385. return (-1);
  386. }
  387. return (0);
  388. }
  389. int
  390. qdevice_cmap_del_track(struct qdevice_instance *instance)
  391. {
  392. cs_error_t res;
  393. res = cmap_track_delete(instance->cmap_handle, instance->cmap_reload_track_handle);
  394. if (res != CS_OK) {
  395. log_corosync(LOG_WARNING, res, "Can't delete cmap totemconfig_reload_in_progress tracking");
  396. }
  397. res = cmap_track_delete(instance->cmap_handle, instance->cmap_nodelist_track_handle);
  398. if (res != CS_OK) {
  399. log_corosync(LOG_WARNING, res, "Can't delete cmap nodelist tracking");
  400. }
  401. res = cmap_track_delete(instance->cmap_handle, instance->cmap_logging_track_handle);
  402. if (res != CS_OK) {
  403. log_corosync(LOG_WARNING, res, "Can't delete cmap logging tracking");
  404. }
  405. res = cmap_track_delete(instance->cmap_handle, instance->cmap_heuristics_track_handle);
  406. if (res != CS_OK) {
  407. log_corosync(LOG_WARNING, res, "Can't delete cmap heuristics tracking");
  408. }
  409. return (0);
  410. }
  411. void
  412. qdevice_cmap_destroy(struct qdevice_instance *instance)
  413. {
  414. cs_error_t res;
  415. res = cmap_finalize(instance->cmap_handle);
  416. if (res != CS_OK) {
  417. log_corosync(LOG_WARNING, res, "Can't finalize cmap");
  418. }
  419. }
  420. int
  421. qdevice_cmap_dispatch(struct qdevice_instance *instance)
  422. {
  423. cs_error_t res;
  424. /*
  425. * dispatch can block if corosync is during sync phase
  426. */
  427. if (instance->sync_in_progress) {
  428. return (0);
  429. }
  430. res = cmap_dispatch(instance->cmap_handle, CS_DISPATCH_ALL);
  431. if (res != CS_OK && res != CS_ERR_TRY_AGAIN) {
  432. log_corosync(LOG_ERR, res, "Can't dispatch cmap messages");
  433. return (-1);
  434. }
  435. return (0);
  436. }