totemconfig.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * Jan Friesse (jfriesse@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <config.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <errno.h>
  41. #include <unistd.h>
  42. #include <sys/socket.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <sys/param.h>
  49. #include <corosync/swab.h>
  50. #include <corosync/list.h>
  51. #include <qb/qbdefs.h>
  52. #include <corosync/totem/totem.h>
  53. #include <corosync/config.h>
  54. #include <corosync/logsys.h>
  55. #include <corosync/icmap.h>
  56. #include <nss.h>
  57. #include <pk11pub.h>
  58. #include <pkcs11.h>
  59. #include <prerror.h>
  60. #include "util.h"
  61. #include "totemconfig.h"
  62. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  63. #define TOKEN_TIMEOUT 1000
  64. #define TOKEN_RETRANSMIT_TIMEOUT (int)(TOKEN_TIMEOUT / (TOKEN_RETRANSMITS_BEFORE_LOSS_CONST + 0.2))
  65. #define TOKEN_HOLD_TIMEOUT (int)(TOKEN_RETRANSMIT_TIMEOUT * 0.8 - (1000/(int)HZ))
  66. #define JOIN_TIMEOUT 50
  67. #define MERGE_TIMEOUT 200
  68. #define DOWNCHECK_TIMEOUT 1000
  69. #define FAIL_TO_RECV_CONST 2500
  70. #define SEQNO_UNCHANGED_CONST 30
  71. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  72. #define MAX_NETWORK_DELAY 50
  73. #define WINDOW_SIZE 50
  74. #define MAX_MESSAGES 17
  75. #define MISS_COUNT_CONST 5
  76. #define RRP_PROBLEM_COUNT_TIMEOUT 2000
  77. #define RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT 10
  78. #define RRP_PROBLEM_COUNT_THRESHOLD_MIN 2
  79. #define RRP_AUTORECOVERY_CHECK_TIMEOUT 1000
  80. #define DEFAULT_PORT 5405
  81. static char error_string_response[512];
  82. static void add_totem_config_notification(struct totem_config *totem_config);
  83. static void totem_volatile_config_read (struct totem_config *totem_config)
  84. {
  85. char *str;
  86. icmap_get_uint32("totem.token", &totem_config->token_timeout);
  87. icmap_get_uint32("totem.token_retransmit", &totem_config->token_retransmit_timeout);
  88. icmap_get_uint32("totem.hold", &totem_config->token_hold_timeout);
  89. icmap_get_uint32("totem.token_retransmits_before_loss_const", &totem_config->token_retransmits_before_loss_const);
  90. icmap_get_uint32("totem.join", &totem_config->join_timeout);
  91. icmap_get_uint32("totem.send_join", &totem_config->send_join_timeout);
  92. icmap_get_uint32("totem.consensus", &totem_config->consensus_timeout);
  93. icmap_get_uint32("totem.merge", &totem_config->merge_timeout);
  94. icmap_get_uint32("totem.downcheck", &totem_config->downcheck_timeout);
  95. icmap_get_uint32("totem.fail_recv_const", &totem_config->fail_to_recv_const);
  96. icmap_get_uint32("totem.seqno_unchanged_const", &totem_config->seqno_unchanged_const);
  97. icmap_get_uint32("totem.rrp_token_expired_timeout", &totem_config->rrp_token_expired_timeout);
  98. icmap_get_uint32("totem.rrp_problem_count_timeout", &totem_config->rrp_problem_count_timeout);
  99. icmap_get_uint32("totem.rrp_problem_count_threshold", &totem_config->rrp_problem_count_threshold);
  100. icmap_get_uint32("totem.rrp_problem_count_mcast_threshold", &totem_config->rrp_problem_count_mcast_threshold);
  101. icmap_get_uint32("totem.rrp_autorecovery_check_timeout", &totem_config->rrp_autorecovery_check_timeout);
  102. icmap_get_uint32("totem.heartbeat_failures_allowed", &totem_config->heartbeat_failures_allowed);
  103. icmap_get_uint32("totem.max_network_delay", &totem_config->max_network_delay);
  104. icmap_get_uint32("totem.window_size", &totem_config->window_size);
  105. icmap_get_uint32("totem.max_messages", &totem_config->max_messages);
  106. icmap_get_uint32("totem.miss_count_const", &totem_config->miss_count_const);
  107. if (icmap_get_string("totem.vsftype", &str) == CS_OK) {
  108. totem_config->vsf_type = str;
  109. }
  110. }
  111. static void totem_get_crypto_type(struct totem_config *totem_config)
  112. {
  113. char *str;
  114. /*
  115. * We must set these even if the key does not exist.
  116. * Encryption type can be set on-the-fly using CFG
  117. */
  118. totem_config->crypto_crypt_type = CKM_AES_CBC_PAD;
  119. totem_config->crypto_sign_type = CKM_SHA256_RSA_PKCS;
  120. if (icmap_get_string("totem.crypto_type", &str) == CS_OK) {
  121. if (strcmp(str, "nss") == 0 || strcmp(str, "aes256") == 0) {
  122. totem_config->crypto_type = TOTEM_CRYPTO_AES256;
  123. }
  124. free(str);
  125. }
  126. }
  127. static uint16_t generate_cluster_id (const char *cluster_name)
  128. {
  129. int i;
  130. int value = 0;
  131. for (i = 0; i < strlen(cluster_name); i++) {
  132. value <<= 1;
  133. value += cluster_name[i];
  134. }
  135. return (value & 0xFFFF);
  136. }
  137. static int get_cluster_mcast_addr (
  138. const char *cluster_name,
  139. const struct totem_ip_address *bindnet,
  140. unsigned int ringnumber,
  141. struct totem_ip_address *res)
  142. {
  143. uint16_t clusterid;
  144. char addr[INET6_ADDRSTRLEN + 1];
  145. int err;
  146. if (cluster_name == NULL) {
  147. return (-1);
  148. }
  149. clusterid = generate_cluster_id(cluster_name) + ringnumber;
  150. memset (res, 0, sizeof(res));
  151. switch (bindnet->family) {
  152. case AF_INET:
  153. snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
  154. break;
  155. case AF_INET6:
  156. snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
  157. break;
  158. default:
  159. /*
  160. * Unknown family
  161. */
  162. return (-1);
  163. }
  164. err = totemip_parse (res, addr, 0);
  165. return (err);
  166. }
  167. static int find_local_node_in_nodelist(struct totem_config *totem_config)
  168. {
  169. icmap_iter_t iter;
  170. const char *iter_key;
  171. int res = 0;
  172. int node_pos;
  173. int local_node_pos = -1;
  174. struct totem_ip_address bind_addr;
  175. int interface_up, interface_num;
  176. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  177. char *node_addr_str;
  178. struct totem_ip_address node_addr;
  179. res = totemip_iface_check(&totem_config->interfaces[0].bindnet,
  180. &bind_addr, &interface_up, &interface_num,
  181. totem_config->clear_node_high_bit);
  182. if (res == -1) {
  183. return (-1);
  184. }
  185. iter = icmap_iter_init("nodelist.node.");
  186. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  187. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  188. if (res != 2) {
  189. continue;
  190. }
  191. if (strcmp(tmp_key, "ring0_addr") != 0) {
  192. continue;
  193. }
  194. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  195. if (icmap_get_string(tmp_key, &node_addr_str) != CS_OK) {
  196. continue;
  197. }
  198. res = totemip_parse (&node_addr, node_addr_str, 0);
  199. free(node_addr_str);
  200. if (res == -1) {
  201. continue ;
  202. }
  203. if (totemip_equal(&bind_addr, &node_addr)) {
  204. local_node_pos = node_pos;
  205. }
  206. }
  207. icmap_iter_finalize(iter);
  208. return (local_node_pos);
  209. }
  210. static void put_nodelist_members_to_config(struct totem_config *totem_config)
  211. {
  212. icmap_iter_t iter, iter2;
  213. const char *iter_key, *iter_key2;
  214. int res = 0;
  215. int node_pos;
  216. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  217. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  218. char *node_addr_str;
  219. int member_count;
  220. unsigned int ringnumber = 0;
  221. iter = icmap_iter_init("nodelist.node.");
  222. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  223. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  224. if (res != 2) {
  225. continue;
  226. }
  227. if (strcmp(tmp_key, "ring0_addr") != 0) {
  228. continue;
  229. }
  230. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  231. iter2 = icmap_iter_init(tmp_key);
  232. while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
  233. res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &ringnumber, tmp_key2);
  234. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  235. continue;
  236. }
  237. if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
  238. continue;
  239. }
  240. member_count = totem_config->interfaces[ringnumber].member_count;
  241. res = totemip_parse(&totem_config->interfaces[ringnumber].member_list[member_count],
  242. node_addr_str, 0);
  243. if (res != -1) {
  244. totem_config->interfaces[ringnumber].member_count++;
  245. }
  246. free(node_addr_str);
  247. }
  248. icmap_iter_finalize(iter2);
  249. }
  250. icmap_iter_finalize(iter);
  251. }
  252. static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
  253. {
  254. icmap_iter_t iter;
  255. const char *iter_key;
  256. int res = 0;
  257. int node_pos;
  258. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  259. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  260. char *node_addr_str;
  261. unsigned int ringnumber = 0;
  262. struct list_head addrs;
  263. struct list_head *list;
  264. struct totem_ip_if_address *if_addr;
  265. struct totem_ip_address node_addr;
  266. int node_found;
  267. if (totemip_getifaddrs(&addrs) == -1) {
  268. return ;
  269. }
  270. iter = icmap_iter_init("nodelist.node.");
  271. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  272. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  273. if (res != 2) {
  274. continue;
  275. }
  276. if (strcmp(tmp_key, "ring0_addr") != 0) {
  277. continue;
  278. }
  279. if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
  280. continue ;
  281. }
  282. if (totemip_parse(&node_addr, node_addr_str, 0) == -1) {
  283. free(node_addr_str);
  284. continue ;
  285. }
  286. free(node_addr_str);
  287. /*
  288. * Try to find node in if_addrs
  289. */
  290. node_found = 0;
  291. for (list = addrs.next; list != &addrs; list = list->next) {
  292. if_addr = list_entry(list, struct totem_ip_if_address, list);
  293. if (totemip_equal(&node_addr, &if_addr->ip_addr)) {
  294. node_found = 1;
  295. break;
  296. }
  297. }
  298. if (node_found) {
  299. break ;
  300. }
  301. }
  302. icmap_iter_finalize(iter);
  303. if (node_found) {
  304. /*
  305. * We found node, so create interface section
  306. */
  307. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  308. iter = icmap_iter_init(tmp_key);
  309. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  310. res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &ringnumber, tmp_key2);
  311. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  312. continue ;
  313. }
  314. if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
  315. continue;
  316. }
  317. snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", ringnumber);
  318. icmap_set_string(tmp_key2, node_addr_str);
  319. free(node_addr_str);
  320. }
  321. icmap_iter_finalize(iter);
  322. }
  323. }
  324. extern int totem_config_read (
  325. struct totem_config *totem_config,
  326. const char **error_string,
  327. uint64_t *warnings)
  328. {
  329. int res = 0;
  330. char *str;
  331. unsigned int ringnumber = 0;
  332. int member_count = 0;
  333. icmap_iter_t iter, member_iter;
  334. const char *iter_key;
  335. const char *member_iter_key;
  336. char ringnumber_key[ICMAP_KEYNAME_MAXLEN];
  337. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  338. uint8_t u8;
  339. uint16_t u16;
  340. char *cluster_name = NULL;
  341. int i;
  342. int local_node_pos;
  343. int nodeid_set;
  344. *warnings = 0;
  345. memset (totem_config, 0, sizeof (struct totem_config));
  346. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  347. if (totem_config->interfaces == 0) {
  348. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  349. return -1;
  350. }
  351. memset (totem_config->interfaces, 0,
  352. sizeof (struct totem_interface) * INTERFACE_MAX);
  353. totem_config->secauth = 1;
  354. strcpy (totem_config->rrp_mode, "none");
  355. icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
  356. if (icmap_get_string("totem.secauth", &str) == CS_OK) {
  357. if (strcmp (str, "on") == 0) {
  358. totem_config->secauth = 1;
  359. }
  360. if (strcmp (str, "off") == 0) {
  361. totem_config->secauth = 0;
  362. }
  363. free(str);
  364. }
  365. if (totem_config->secauth == 1) {
  366. totem_get_crypto_type(totem_config);
  367. }
  368. if (icmap_get_string("totem.rrp_mode", &str) == CS_OK) {
  369. strcpy (totem_config->rrp_mode, str);
  370. free(str);
  371. }
  372. icmap_get_uint32("totem.nodeid", &totem_config->node_id);
  373. totem_config->clear_node_high_bit = 0;
  374. if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
  375. if (strcmp (str, "yes") == 0) {
  376. totem_config->clear_node_high_bit = 1;
  377. }
  378. free(str);
  379. }
  380. icmap_get_uint32("totem.threads", &totem_config->threads);
  381. icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
  382. icmap_get_string("totem.cluster_name", &cluster_name);
  383. /*
  384. * Get things that might change in the future
  385. */
  386. totem_volatile_config_read(totem_config);
  387. if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
  388. /*
  389. * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
  390. */
  391. config_convert_nodelist_to_interface(totem_config);
  392. } else {
  393. free(str);
  394. }
  395. iter = icmap_iter_init("totem.interface.");
  396. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  397. res = sscanf(iter_key, "totem.interface.%[^.].%s", ringnumber_key, tmp_key);
  398. if (res != 2) {
  399. continue;
  400. }
  401. if (strcmp(tmp_key, "bindnetaddr") != 0) {
  402. continue;
  403. }
  404. member_count = 0;
  405. ringnumber = atoi(ringnumber_key);
  406. /*
  407. * Get the bind net address
  408. */
  409. if (icmap_get_string(iter_key, &str) == CS_OK) {
  410. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  411. totem_config->interfaces[ringnumber].mcast_addr.family);
  412. free(str);
  413. }
  414. /*
  415. * Get interface multicast address
  416. */
  417. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", ringnumber);
  418. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  419. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  420. free(str);
  421. } else {
  422. /*
  423. * User not specified address -> autogenerate one from cluster_name key
  424. * (if available)
  425. */
  426. res = get_cluster_mcast_addr (cluster_name,
  427. &totem_config->interfaces[ringnumber].bindnet,
  428. ringnumber,
  429. &totem_config->interfaces[ringnumber].mcast_addr);
  430. }
  431. totem_config->broadcast_use = 0;
  432. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", ringnumber);
  433. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  434. if (strcmp (str, "yes") == 0) {
  435. totem_config->broadcast_use = 1;
  436. totemip_parse (
  437. &totem_config->interfaces[ringnumber].mcast_addr,
  438. "255.255.255.255", 0);
  439. }
  440. free(str);
  441. }
  442. /*
  443. * Get mcast port
  444. */
  445. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", ringnumber);
  446. if (icmap_get_uint16(tmp_key, &totem_config->interfaces[ringnumber].ip_port) != CS_OK) {
  447. if (totem_config->broadcast_use) {
  448. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT + (2 * ringnumber);
  449. } else {
  450. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT;
  451. }
  452. }
  453. /*
  454. * Get the TTL
  455. */
  456. totem_config->interfaces[ringnumber].ttl = 1;
  457. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", ringnumber);
  458. if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
  459. totem_config->interfaces[ringnumber].ttl = u8;
  460. }
  461. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", ringnumber);
  462. member_iter = icmap_iter_init(tmp_key);
  463. while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
  464. if (member_count == 0) {
  465. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  466. free(str);
  467. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
  468. break;
  469. } else {
  470. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
  471. }
  472. }
  473. if (icmap_get_string(member_iter_key, &str) == CS_OK) {
  474. res = totemip_parse (&totem_config->interfaces[ringnumber].member_list[member_count++],
  475. str, 0);
  476. }
  477. }
  478. icmap_iter_finalize(member_iter);
  479. totem_config->interfaces[ringnumber].member_count = member_count;
  480. totem_config->interface_count++;
  481. }
  482. icmap_iter_finalize(iter);
  483. /*
  484. * Store automatically generated items back to icmap
  485. */
  486. for (i = 0; i < totem_config->interface_count; i++) {
  487. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
  488. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  489. free(str);
  490. } else {
  491. str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
  492. icmap_set_string(tmp_key, str);
  493. }
  494. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
  495. if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
  496. icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
  497. }
  498. }
  499. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  500. if (icmap_get_string("totem.transport", &str) == CS_OK) {
  501. if (strcmp (str, "udpu") == 0) {
  502. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  503. }
  504. if (strcmp (str, "iba") == 0) {
  505. totem_config->transport_number = TOTEM_TRANSPORT_RDMA;
  506. }
  507. free(str);
  508. }
  509. free(cluster_name);
  510. /*
  511. * Check existence of nodelist
  512. */
  513. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  514. free(str);
  515. /*
  516. * find local node
  517. */
  518. local_node_pos = find_local_node_in_nodelist(totem_config);
  519. if (local_node_pos != -1) {
  520. icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
  521. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
  522. nodeid_set = (totem_config->node_id != 0);
  523. if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
  524. *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
  525. }
  526. /*
  527. * Make localnode ring0_addr read only, so we can be sure that local
  528. * node never changes. If rebinding to other IP would be in future
  529. * supported, this must be changed and handled properly!
  530. */
  531. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", local_node_pos);
  532. icmap_set_ro_access(tmp_key, 0, 1);
  533. icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
  534. }
  535. put_nodelist_members_to_config(totem_config);
  536. }
  537. add_totem_config_notification(totem_config);
  538. return 0;
  539. }
  540. int totem_config_validate (
  541. struct totem_config *totem_config,
  542. const char **error_string)
  543. {
  544. static char local_error_reason[512];
  545. char parse_error[512];
  546. const char *error_reason = local_error_reason;
  547. int i;
  548. unsigned int interface_max = INTERFACE_MAX;
  549. if (totem_config->interface_count == 0) {
  550. error_reason = "No interfaces defined";
  551. goto parse_error;
  552. }
  553. for (i = 0; i < totem_config->interface_count; i++) {
  554. /*
  555. * Some error checking of parsed data to make sure its valid
  556. */
  557. struct totem_ip_address null_addr;
  558. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  559. if ((totem_config->transport_number == 0) &&
  560. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  561. sizeof (struct totem_ip_address)) == 0) {
  562. error_reason = "No multicast address specified";
  563. goto parse_error;
  564. }
  565. if (totem_config->interfaces[i].ip_port == 0) {
  566. error_reason = "No multicast port specified";
  567. goto parse_error;
  568. }
  569. if (totem_config->interfaces[i].ttl > 255) {
  570. error_reason = "Invalid TTL (should be 0..255)";
  571. goto parse_error;
  572. }
  573. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  574. totem_config->interfaces[i].ttl != 1) {
  575. error_reason = "Can only set ttl on multicast transport types";
  576. goto parse_error;
  577. }
  578. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  579. totem_config->node_id == 0) {
  580. error_reason = "An IPV6 network requires that a node ID be specified.";
  581. goto parse_error;
  582. }
  583. if (totem_config->broadcast_use == 0 && totem_config->transport_number == 0) {
  584. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  585. error_reason = "Multicast address family does not match bind address family";
  586. goto parse_error;
  587. }
  588. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  589. error_reason = "Not all bind address belong to the same IP family";
  590. goto parse_error;
  591. }
  592. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  593. error_reason = "mcastaddr is not a correct multicast address.";
  594. goto parse_error;
  595. }
  596. }
  597. }
  598. if (totem_config->version != 2) {
  599. error_reason = "This totem parser can only parse version 2 configurations.";
  600. goto parse_error;
  601. }
  602. if (totem_config->token_retransmits_before_loss_const == 0) {
  603. totem_config->token_retransmits_before_loss_const =
  604. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  605. }
  606. /*
  607. * Setup timeout values that are not setup by user
  608. */
  609. if (totem_config->token_timeout == 0) {
  610. totem_config->token_timeout = TOKEN_TIMEOUT;
  611. if (totem_config->token_retransmits_before_loss_const == 0) {
  612. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  613. }
  614. if (totem_config->token_retransmit_timeout == 0) {
  615. totem_config->token_retransmit_timeout =
  616. (int)(totem_config->token_timeout /
  617. (totem_config->token_retransmits_before_loss_const + 0.2));
  618. }
  619. if (totem_config->token_hold_timeout == 0) {
  620. totem_config->token_hold_timeout =
  621. (int)(totem_config->token_retransmit_timeout * 0.8 -
  622. (1000/HZ));
  623. }
  624. }
  625. if (totem_config->max_network_delay == 0) {
  626. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  627. }
  628. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  629. snprintf (local_error_reason, sizeof(local_error_reason),
  630. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  631. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  632. goto parse_error;
  633. }
  634. if (totem_config->window_size == 0) {
  635. totem_config->window_size = WINDOW_SIZE;
  636. }
  637. if (totem_config->max_messages == 0) {
  638. totem_config->max_messages = MAX_MESSAGES;
  639. }
  640. if (totem_config->miss_count_const == 0) {
  641. totem_config->miss_count_const = MISS_COUNT_CONST;
  642. }
  643. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  644. snprintf (local_error_reason, sizeof(local_error_reason),
  645. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  646. totem_config->token_timeout, MINIMUM_TIMEOUT);
  647. goto parse_error;
  648. }
  649. if (totem_config->token_retransmit_timeout == 0) {
  650. totem_config->token_retransmit_timeout =
  651. (int)(totem_config->token_timeout /
  652. (totem_config->token_retransmits_before_loss_const + 0.2));
  653. }
  654. if (totem_config->token_hold_timeout == 0) {
  655. totem_config->token_hold_timeout =
  656. (int)(totem_config->token_retransmit_timeout * 0.8 -
  657. (1000/HZ));
  658. }
  659. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  660. snprintf (local_error_reason, sizeof(local_error_reason),
  661. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  662. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  663. goto parse_error;
  664. }
  665. if (totem_config->token_hold_timeout == 0) {
  666. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  667. }
  668. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  669. snprintf (local_error_reason, sizeof(local_error_reason),
  670. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  671. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  672. goto parse_error;
  673. }
  674. if (totem_config->join_timeout == 0) {
  675. totem_config->join_timeout = JOIN_TIMEOUT;
  676. }
  677. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  678. snprintf (local_error_reason, sizeof(local_error_reason),
  679. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  680. totem_config->join_timeout, MINIMUM_TIMEOUT);
  681. goto parse_error;
  682. }
  683. if (totem_config->consensus_timeout == 0) {
  684. totem_config->consensus_timeout = (int)(float)(1.2 * totem_config->token_timeout);
  685. }
  686. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  687. snprintf (local_error_reason, sizeof(local_error_reason),
  688. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  689. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  690. goto parse_error;
  691. }
  692. if (totem_config->merge_timeout == 0) {
  693. totem_config->merge_timeout = MERGE_TIMEOUT;
  694. }
  695. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  696. snprintf (local_error_reason, sizeof(local_error_reason),
  697. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  698. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  699. goto parse_error;
  700. }
  701. if (totem_config->downcheck_timeout == 0) {
  702. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  703. }
  704. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  705. snprintf (local_error_reason, sizeof(local_error_reason),
  706. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  707. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  708. goto parse_error;
  709. }
  710. /*
  711. * RRP values validation
  712. */
  713. if (strcmp (totem_config->rrp_mode, "none") &&
  714. strcmp (totem_config->rrp_mode, "active") &&
  715. strcmp (totem_config->rrp_mode, "passive")) {
  716. snprintf (local_error_reason, sizeof(local_error_reason),
  717. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  718. goto parse_error;
  719. }
  720. if (totem_config->rrp_problem_count_timeout == 0) {
  721. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  722. }
  723. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  724. snprintf (local_error_reason, sizeof(local_error_reason),
  725. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  726. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  727. goto parse_error;
  728. }
  729. if (totem_config->rrp_problem_count_threshold == 0) {
  730. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  731. }
  732. if (totem_config->rrp_problem_count_mcast_threshold == 0) {
  733. totem_config->rrp_problem_count_mcast_threshold = totem_config->rrp_problem_count_threshold * 10;
  734. }
  735. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  736. snprintf (local_error_reason, sizeof(local_error_reason),
  737. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  738. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  739. goto parse_error;
  740. }
  741. if (totem_config->rrp_problem_count_mcast_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  742. snprintf (local_error_reason, sizeof(local_error_reason),
  743. "The RRP multicast problem count threshold (%d problem count) may not be less then (%d problem count).",
  744. totem_config->rrp_problem_count_mcast_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  745. goto parse_error;
  746. }
  747. if (totem_config->rrp_token_expired_timeout == 0) {
  748. totem_config->rrp_token_expired_timeout =
  749. totem_config->token_retransmit_timeout;
  750. }
  751. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  752. snprintf (local_error_reason, sizeof(local_error_reason),
  753. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  754. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  755. goto parse_error;
  756. }
  757. if (totem_config->rrp_autorecovery_check_timeout == 0) {
  758. totem_config->rrp_autorecovery_check_timeout = RRP_AUTORECOVERY_CHECK_TIMEOUT;
  759. }
  760. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  761. interface_max = 1;
  762. }
  763. if (interface_max < totem_config->interface_count) {
  764. snprintf (parse_error, sizeof(parse_error),
  765. "%d is too many configured interfaces for the rrp_mode setting %s.",
  766. totem_config->interface_count,
  767. totem_config->rrp_mode);
  768. error_reason = parse_error;
  769. goto parse_error;
  770. }
  771. if (totem_config->fail_to_recv_const == 0) {
  772. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  773. }
  774. if (totem_config->seqno_unchanged_const == 0) {
  775. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  776. }
  777. if (totem_config->net_mtu == 0) {
  778. totem_config->net_mtu = 1500;
  779. }
  780. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  781. snprintf (local_error_reason, sizeof(local_error_reason),
  782. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  783. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  784. goto parse_error;
  785. }
  786. if (totem_config->threads > SEND_THREADS_MAX) {
  787. totem_config->threads = SEND_THREADS_MAX;
  788. }
  789. if (totem_config->secauth == 0) {
  790. totem_config->threads = 0;
  791. }
  792. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  793. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  794. goto parse_error;
  795. }
  796. if (totem_config->vsf_type == NULL) {
  797. totem_config->vsf_type = "none";
  798. }
  799. return (0);
  800. parse_error:
  801. snprintf (error_string_response, sizeof(error_string_response),
  802. "parse error in config: %s\n", error_reason);
  803. *error_string = error_string_response;
  804. return (-1);
  805. }
  806. static int read_keyfile (
  807. const char *key_location,
  808. struct totem_config *totem_config,
  809. const char **error_string)
  810. {
  811. int fd;
  812. int res;
  813. ssize_t expected_key_len = sizeof (totem_config->private_key);
  814. int saved_errno;
  815. char error_str[100];
  816. const char *error_ptr;
  817. fd = open (key_location, O_RDONLY);
  818. if (fd == -1) {
  819. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  820. snprintf (error_string_response, sizeof(error_string_response),
  821. "Could not open %s: %s\n",
  822. key_location, error_ptr);
  823. goto parse_error;
  824. }
  825. res = read (fd, totem_config->private_key, expected_key_len);
  826. saved_errno = errno;
  827. close (fd);
  828. if (res == -1) {
  829. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  830. snprintf (error_string_response, sizeof(error_string_response),
  831. "Could not read %s: %s\n",
  832. key_location, error_ptr);
  833. goto parse_error;
  834. }
  835. totem_config->private_key_len = expected_key_len;
  836. if (res != expected_key_len) {
  837. snprintf (error_string_response, sizeof(error_string_response),
  838. "Could only read %d bits of 1024 bits from %s.\n",
  839. res * 8, key_location);
  840. goto parse_error;
  841. }
  842. return 0;
  843. parse_error:
  844. *error_string = error_string_response;
  845. return (-1);
  846. }
  847. int totem_config_keyread (
  848. struct totem_config *totem_config,
  849. const char **error_string)
  850. {
  851. int got_key = 0;
  852. char *key_location = NULL;
  853. int res;
  854. size_t key_len;
  855. memset (totem_config->private_key, 0, 128);
  856. totem_config->private_key_len = 128;
  857. if (totem_config->secauth == 0) {
  858. return (0);
  859. }
  860. /* cmap may store the location of the key file */
  861. if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
  862. res = read_keyfile(key_location, totem_config, error_string);
  863. free(key_location);
  864. if (res) {
  865. goto key_error;
  866. }
  867. got_key = 1;
  868. } else { /* Or the key itself may be in the cmap */
  869. if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
  870. if (key_len > sizeof (totem_config->private_key)) {
  871. sprintf(error_string_response, "key is too long");
  872. goto key_error;
  873. }
  874. if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
  875. totem_config->private_key_len = key_len;
  876. got_key = 1;
  877. } else {
  878. sprintf(error_string_response, "can't store private key");
  879. goto key_error;
  880. }
  881. }
  882. }
  883. /* In desperation we read the default filename */
  884. if (!got_key) {
  885. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  886. if (!filename)
  887. filename = COROSYSCONFDIR "/authkey";
  888. res = read_keyfile(filename, totem_config, error_string);
  889. if (res)
  890. goto key_error;
  891. }
  892. return (0);
  893. key_error:
  894. *error_string = error_string_response;
  895. return (-1);
  896. }
  897. static void totem_change_notify(
  898. int32_t event,
  899. const char *key_name,
  900. struct icmap_notify_value new_val,
  901. struct icmap_notify_value old_val,
  902. void *user_data)
  903. {
  904. totem_volatile_config_read((struct totem_config *)user_data);
  905. }
  906. static void add_totem_config_notification(struct totem_config *totem_config)
  907. {
  908. icmap_track_t icmap_track;
  909. icmap_track_add("totem.",
  910. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  911. totem_change_notify,
  912. totem_config,
  913. &icmap_track);
  914. }