qdevice-cmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Copyright (c) 2015-2016 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 "qdevice-config.h"
  42. #include "qdevice-cmap.h"
  43. #include "qdevice-log.h"
  44. #include "qdevice-log-debug.h"
  45. #include "qdevice-model.h"
  46. static uint32_t
  47. qdevice_cmap_autogenerate_node_id(const char *addr, int clear_node_high_byte)
  48. {
  49. struct addrinfo *ainfo;
  50. struct addrinfo ahints;
  51. int ret, i;
  52. memset(&ahints, 0, sizeof(ahints));
  53. ahints.ai_socktype = SOCK_DGRAM;
  54. ahints.ai_protocol = IPPROTO_UDP;
  55. /*
  56. * Hardcoded AF_INET because autogenerated nodeid is valid only for ipv4
  57. */
  58. ahints.ai_family = AF_INET;
  59. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  60. if (ret != 0)
  61. return (0);
  62. if (ainfo->ai_family != AF_INET) {
  63. freeaddrinfo(ainfo);
  64. return (0);
  65. }
  66. memcpy(&i, &((struct sockaddr_in *)ainfo->ai_addr)->sin_addr, sizeof(struct in_addr));
  67. freeaddrinfo(ainfo);
  68. ret = htonl(i);
  69. if (clear_node_high_byte) {
  70. ret &= 0x7FFFFFFF;
  71. }
  72. return (ret);
  73. }
  74. int
  75. qdevice_cmap_get_nodelist(cmap_handle_t cmap_handle, struct node_list *list)
  76. {
  77. cs_error_t cs_err;
  78. cmap_iter_handle_t iter_handle;
  79. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  80. char tmp_key[CMAP_KEYNAME_MAXLEN + 1];
  81. int res;
  82. int ret_value;
  83. unsigned int node_pos;
  84. uint32_t node_id;
  85. uint32_t data_center_id;
  86. char *tmp_str;
  87. char *addr0_str;
  88. int clear_node_high_byte;
  89. ret_value = 0;
  90. node_list_init(list);
  91. cs_err = cmap_iter_init(cmap_handle, "nodelist.node.", &iter_handle);
  92. if (cs_err != CS_OK) {
  93. return (-1);
  94. }
  95. while ((cs_err = cmap_iter_next(cmap_handle, iter_handle, key_name, NULL, NULL)) == CS_OK) {
  96. res = sscanf(key_name, "nodelist.node.%u.%s", &node_pos, tmp_key);
  97. if (res != 2) {
  98. continue;
  99. }
  100. if (strcmp(tmp_key, "ring0_addr") != 0) {
  101. continue;
  102. }
  103. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  104. cs_err = cmap_get_uint32(cmap_handle, tmp_key, &node_id);
  105. if (cs_err == CS_ERR_NOT_EXIST) {
  106. /*
  107. * Nodeid doesn't exists -> autogenerate node id
  108. */
  109. clear_node_high_byte = 0;
  110. if (cmap_get_string(cmap_handle, "totem.clear_node_high_bit",
  111. &tmp_str) == CS_OK) {
  112. if (strcmp (tmp_str, "yes") == 0) {
  113. clear_node_high_byte = 1;
  114. }
  115. free(tmp_str);
  116. }
  117. if (cmap_get_string(cmap_handle, key_name, &addr0_str) != CS_OK) {
  118. return (-1);
  119. }
  120. node_id = qdevice_cmap_autogenerate_node_id(addr0_str,
  121. clear_node_high_byte);
  122. free(addr0_str);
  123. } else if (cs_err != CS_OK) {
  124. ret_value = -1;
  125. goto iter_finalize;
  126. }
  127. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.datacenterid", node_pos);
  128. if (cmap_get_uint32(cmap_handle, tmp_key, &data_center_id) != CS_OK) {
  129. data_center_id = 0;
  130. }
  131. if (node_list_add(list, node_id, data_center_id, TLV_NODE_STATE_NOT_SET) == NULL) {
  132. ret_value = -1;
  133. goto iter_finalize;
  134. }
  135. }
  136. iter_finalize:
  137. cmap_iter_finalize(cmap_handle, iter_handle);
  138. if (ret_value != 0) {
  139. node_list_free(list);
  140. }
  141. return (ret_value);
  142. }
  143. int
  144. qdevice_cmap_get_config_version(cmap_handle_t cmap_handle, uint64_t *config_version)
  145. {
  146. int res;
  147. if (cmap_get_uint64(cmap_handle, "totem.config_version", config_version) == CS_OK) {
  148. res = 0;
  149. } else {
  150. *config_version = 0;
  151. res = -1;
  152. }
  153. return (res);
  154. }
  155. int
  156. qdevice_cmap_store_config_node_list(struct qdevice_instance *instance)
  157. {
  158. int res;
  159. node_list_free(&instance->config_node_list);
  160. if (qdevice_cmap_get_nodelist(instance->cmap_handle, &instance->config_node_list) != 0) {
  161. qdevice_log(LOG_ERR, "Can't get configuration node list.");
  162. return (-1);
  163. }
  164. res = qdevice_cmap_get_config_version(instance->cmap_handle, &instance->config_node_list_version);
  165. instance->config_node_list_version_set = (res == 0);
  166. return (0);
  167. }
  168. void
  169. qdevice_cmap_init(struct qdevice_instance *instance)
  170. {
  171. cs_error_t res;
  172. int no_retries;
  173. no_retries = 0;
  174. while ((res = cmap_initialize(&instance->cmap_handle)) == CS_ERR_TRY_AGAIN &&
  175. no_retries++ < QDEVICE_MAX_CS_TRY_AGAIN) {
  176. (void)poll(NULL, 0, 1000);
  177. }
  178. if (res != CS_OK) {
  179. errx(1, "Failed to initialize the cmap API. Error %s", cs_strerror(res));
  180. }
  181. if ((res = cmap_context_set(instance->cmap_handle, (void *)instance)) != CS_OK) {
  182. errx(1, "Can't set cmap context. Error %s", cs_strerror(res));
  183. }
  184. cmap_fd_get(instance->cmap_handle, &instance->cmap_poll_fd);
  185. }
  186. static void
  187. qdevice_cmap_node_list_event(struct qdevice_instance *instance)
  188. {
  189. struct node_list nlist;
  190. int config_version_set;
  191. uint64_t config_version;
  192. qdevice_log(LOG_DEBUG, "Node list configuration possibly changed");
  193. if (qdevice_cmap_get_nodelist(instance->cmap_handle, &nlist) != 0) {
  194. qdevice_log(LOG_ERR, "Can't get configuration node list.");
  195. if (qdevice_model_get_config_node_list_failed(instance) != 0) {
  196. qdevice_log(LOG_DEBUG, "qdevice_model_get_config_node_list_failed returned error -> exit");
  197. exit(2);
  198. }
  199. return ;
  200. }
  201. config_version_set = (qdevice_cmap_get_config_version(instance->cmap_handle,
  202. &config_version) == 0);
  203. if (node_list_eq(&instance->config_node_list, &nlist)) {
  204. return ;
  205. }
  206. qdevice_log(LOG_DEBUG, "Node list changed");
  207. if (config_version_set) {
  208. qdevice_log(LOG_DEBUG, " config_version = %"PRIu64, config_version);
  209. }
  210. qdevice_log_debug_dump_node_list(&nlist);
  211. if (qdevice_model_config_node_list_changed(instance, &nlist,
  212. config_version_set, config_version) != 0) {
  213. qdevice_log(LOG_DEBUG, "qdevice_model_config_node_list_changed returned error -> exit");
  214. exit(2);
  215. }
  216. node_list_free(&instance->config_node_list);
  217. if (node_list_clone(&instance->config_node_list, &nlist) != 0) {
  218. qdevice_log(LOG_ERR, "Can't allocate instance->config_node_list clone");
  219. node_list_free(&nlist);
  220. if (qdevice_model_get_config_node_list_failed(instance) != 0) {
  221. qdevice_log(LOG_DEBUG, "qdevice_model_get_config_node_list_failed returned error -> exit");
  222. exit(2);
  223. }
  224. return ;
  225. }
  226. instance->config_node_list_version_set = config_version_set;
  227. if (config_version_set) {
  228. instance->config_node_list_version = config_version;
  229. }
  230. }
  231. static void
  232. qdevice_cmap_logging_event(struct qdevice_instance *instance)
  233. {
  234. qdevice_log(LOG_DEBUG, "Logging configuration possibly changed");
  235. qdevice_log_configure(instance);
  236. }
  237. static void
  238. qdevice_cmap_reload_cb(cmap_handle_t cmap_handle, cmap_track_handle_t cmap_track_handle,
  239. int32_t event, const char *key_name,
  240. struct cmap_notify_value new_value, struct cmap_notify_value old_value,
  241. void *user_data)
  242. {
  243. cs_error_t cs_res;
  244. uint8_t reload;
  245. struct qdevice_instance *instance;
  246. int node_list_event;
  247. int logging_event;
  248. const char *node_list_prefix_str;
  249. const char *logging_prefix_str;
  250. node_list_event = 0;
  251. logging_event = 0;
  252. node_list_prefix_str = "nodelist.";
  253. logging_prefix_str = "logging.";
  254. if (cmap_context_get(cmap_handle, (const void **)&instance) != CS_OK) {
  255. qdevice_log(LOG_ERR, "Fatal error. Can't get cmap context");
  256. exit(1);
  257. }
  258. /*
  259. * Wait for full reload
  260. */
  261. if (strcmp(key_name, "config.totemconfig_reload_in_progress") == 0 &&
  262. new_value.type == CMAP_VALUETYPE_UINT8 && new_value.len == sizeof(reload)) {
  263. reload = 1;
  264. if (memcmp(new_value.data, &reload, sizeof(reload)) == 0) {
  265. /*
  266. * Ignore nodelist changes
  267. */
  268. instance->cmap_reload_in_progress = 1;
  269. return ;
  270. } else {
  271. instance->cmap_reload_in_progress = 0;
  272. node_list_event = 1;
  273. logging_event = 1;
  274. }
  275. }
  276. if (instance->cmap_reload_in_progress) {
  277. return ;
  278. }
  279. if (((cs_res = cmap_get_uint8(cmap_handle, "config.totemconfig_reload_in_progress",
  280. &reload)) == CS_OK) && reload == 1) {
  281. return ;
  282. }
  283. if (strncmp(key_name, node_list_prefix_str, strlen(node_list_prefix_str)) == 0) {
  284. node_list_event = 1;
  285. }
  286. if (strncmp(key_name, logging_prefix_str, strlen(logging_prefix_str)) == 0) {
  287. logging_event = 1;
  288. }
  289. if (logging_event) {
  290. qdevice_cmap_logging_event(instance);
  291. }
  292. if (node_list_event) {
  293. qdevice_cmap_node_list_event(instance);
  294. }
  295. }
  296. int
  297. qdevice_cmap_add_track(struct qdevice_instance *instance)
  298. {
  299. cs_error_t res;
  300. res = cmap_track_add(instance->cmap_handle, "config.totemconfig_reload_in_progress",
  301. CMAP_TRACK_ADD | CMAP_TRACK_MODIFY, qdevice_cmap_reload_cb,
  302. NULL, &instance->cmap_reload_track_handle);
  303. if (res != CS_OK) {
  304. qdevice_log(LOG_ERR, "Can't initialize cmap totemconfig_reload_in_progress tracking");
  305. return (-1);
  306. }
  307. res = cmap_track_add(instance->cmap_handle, "nodelist.",
  308. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  309. qdevice_cmap_reload_cb,
  310. NULL, &instance->cmap_nodelist_track_handle);
  311. if (res != CS_OK) {
  312. qdevice_log(LOG_ERR, "Can't initialize cmap nodelist tracking");
  313. return (-1);
  314. }
  315. res = cmap_track_add(instance->cmap_handle, "logging.",
  316. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  317. qdevice_cmap_reload_cb,
  318. NULL, &instance->cmap_logging_track_handle);
  319. if (res != CS_OK) {
  320. qdevice_log(LOG_ERR, "Can't initialize logging tracking");
  321. return (-1);
  322. }
  323. return (0);
  324. }
  325. int
  326. qdevice_cmap_del_track(struct qdevice_instance *instance)
  327. {
  328. cs_error_t res;
  329. res = cmap_track_delete(instance->cmap_handle, instance->cmap_reload_track_handle);
  330. if (res != CS_OK) {
  331. qdevice_log(LOG_WARNING, "Can't delete cmap totemconfig_reload_in_progress tracking");
  332. }
  333. res = cmap_track_delete(instance->cmap_handle, instance->cmap_nodelist_track_handle);
  334. if (res != CS_OK) {
  335. qdevice_log(LOG_WARNING, "Can't delete cmap nodelist tracking");
  336. }
  337. res = cmap_track_delete(instance->cmap_handle, instance->cmap_logging_track_handle);
  338. if (res != CS_OK) {
  339. qdevice_log(LOG_WARNING, "Can't delete cmap logging tracking");
  340. }
  341. return (0);
  342. }
  343. void
  344. qdevice_cmap_destroy(struct qdevice_instance *instance)
  345. {
  346. cs_error_t res;
  347. res = cmap_finalize(instance->cmap_handle);
  348. if (res != CS_OK) {
  349. qdevice_log(LOG_WARNING, "Can't finalize cmap. Error %s", cs_strerror(res));
  350. }
  351. }
  352. int
  353. qdevice_cmap_dispatch(struct qdevice_instance *instance)
  354. {
  355. cs_error_t res;
  356. res = cmap_dispatch(instance->cmap_handle, CS_DISPATCH_ALL);
  357. if (res != CS_OK && res != CS_ERR_TRY_AGAIN) {
  358. qdevice_log(LOG_ERR, "Can't dispatch cmap messages");
  359. return (-1);
  360. }
  361. return (0);
  362. }