totemconfig.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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(struct totem_config *totem_config)
  112. {
  113. char *str;
  114. const char *tmp_cipher;
  115. const char *tmp_hash;
  116. tmp_hash = "sha1";
  117. tmp_cipher = "aes256";
  118. if (icmap_get_string("totem.secauth", &str) == CS_OK) {
  119. if (strcmp (str, "off") == 0) {
  120. tmp_hash = "none";
  121. tmp_cipher = "none";
  122. }
  123. free(str);
  124. }
  125. if (icmap_get_string("totem.crypto_cipher", &str) == CS_OK) {
  126. if (strcmp(str, "none") == 0) {
  127. tmp_cipher = "none";
  128. }
  129. if (strcmp(str, "aes256") == 0) {
  130. tmp_cipher = "aes256";
  131. }
  132. free(str);
  133. }
  134. if (icmap_get_string("totem.crypto_hash", &str) == CS_OK) {
  135. if (strcmp(str, "none") == 0) {
  136. tmp_hash = "none";
  137. }
  138. if (strcmp(str, "sha1") == 0) {
  139. tmp_hash = "sha1";
  140. }
  141. free(str);
  142. }
  143. free(totem_config->crypto_cipher_type);
  144. free(totem_config->crypto_hash_type);
  145. totem_config->crypto_cipher_type = strdup(tmp_cipher);
  146. totem_config->crypto_hash_type = strdup(tmp_hash);
  147. }
  148. static uint16_t generate_cluster_id (const char *cluster_name)
  149. {
  150. int i;
  151. int value = 0;
  152. for (i = 0; i < strlen(cluster_name); i++) {
  153. value <<= 1;
  154. value += cluster_name[i];
  155. }
  156. return (value & 0xFFFF);
  157. }
  158. static int get_cluster_mcast_addr (
  159. const char *cluster_name,
  160. const struct totem_ip_address *bindnet,
  161. unsigned int ringnumber,
  162. struct totem_ip_address *res)
  163. {
  164. uint16_t clusterid;
  165. char addr[INET6_ADDRSTRLEN + 1];
  166. int err;
  167. if (cluster_name == NULL) {
  168. return (-1);
  169. }
  170. clusterid = generate_cluster_id(cluster_name) + ringnumber;
  171. memset (res, 0, sizeof(res));
  172. switch (bindnet->family) {
  173. case AF_INET:
  174. snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
  175. break;
  176. case AF_INET6:
  177. snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
  178. break;
  179. default:
  180. /*
  181. * Unknown family
  182. */
  183. return (-1);
  184. }
  185. err = totemip_parse (res, addr, 0);
  186. return (err);
  187. }
  188. static int find_local_node_in_nodelist(struct totem_config *totem_config)
  189. {
  190. icmap_iter_t iter;
  191. const char *iter_key;
  192. int res = 0;
  193. int node_pos;
  194. int local_node_pos = -1;
  195. struct totem_ip_address bind_addr;
  196. int interface_up, interface_num;
  197. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  198. char *node_addr_str;
  199. struct totem_ip_address node_addr;
  200. res = totemip_iface_check(&totem_config->interfaces[0].bindnet,
  201. &bind_addr, &interface_up, &interface_num,
  202. totem_config->clear_node_high_bit);
  203. if (res == -1) {
  204. return (-1);
  205. }
  206. iter = icmap_iter_init("nodelist.node.");
  207. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  208. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  209. if (res != 2) {
  210. continue;
  211. }
  212. if (strcmp(tmp_key, "ring0_addr") != 0) {
  213. continue;
  214. }
  215. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  216. if (icmap_get_string(tmp_key, &node_addr_str) != CS_OK) {
  217. continue;
  218. }
  219. res = totemip_parse (&node_addr, node_addr_str, 0);
  220. free(node_addr_str);
  221. if (res == -1) {
  222. continue ;
  223. }
  224. if (totemip_equal(&bind_addr, &node_addr)) {
  225. local_node_pos = node_pos;
  226. }
  227. }
  228. icmap_iter_finalize(iter);
  229. return (local_node_pos);
  230. }
  231. static void put_nodelist_members_to_config(struct totem_config *totem_config)
  232. {
  233. icmap_iter_t iter, iter2;
  234. const char *iter_key, *iter_key2;
  235. int res = 0;
  236. int node_pos;
  237. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  238. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  239. char *node_addr_str;
  240. int member_count;
  241. unsigned int ringnumber = 0;
  242. iter = icmap_iter_init("nodelist.node.");
  243. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  244. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  245. if (res != 2) {
  246. continue;
  247. }
  248. if (strcmp(tmp_key, "ring0_addr") != 0) {
  249. continue;
  250. }
  251. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  252. iter2 = icmap_iter_init(tmp_key);
  253. while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
  254. res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &ringnumber, tmp_key2);
  255. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  256. continue;
  257. }
  258. if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
  259. continue;
  260. }
  261. member_count = totem_config->interfaces[ringnumber].member_count;
  262. res = totemip_parse(&totem_config->interfaces[ringnumber].member_list[member_count],
  263. node_addr_str, 0);
  264. if (res != -1) {
  265. totem_config->interfaces[ringnumber].member_count++;
  266. }
  267. free(node_addr_str);
  268. }
  269. icmap_iter_finalize(iter2);
  270. }
  271. icmap_iter_finalize(iter);
  272. }
  273. static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
  274. {
  275. icmap_iter_t iter;
  276. const char *iter_key;
  277. int res = 0;
  278. int node_pos;
  279. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  280. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  281. char *node_addr_str;
  282. unsigned int ringnumber = 0;
  283. struct list_head addrs;
  284. struct list_head *list;
  285. struct totem_ip_if_address *if_addr;
  286. struct totem_ip_address node_addr;
  287. int node_found;
  288. if (totemip_getifaddrs(&addrs) == -1) {
  289. return ;
  290. }
  291. iter = icmap_iter_init("nodelist.node.");
  292. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  293. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  294. if (res != 2) {
  295. continue;
  296. }
  297. if (strcmp(tmp_key, "ring0_addr") != 0) {
  298. continue;
  299. }
  300. if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
  301. continue ;
  302. }
  303. if (totemip_parse(&node_addr, node_addr_str, 0) == -1) {
  304. free(node_addr_str);
  305. continue ;
  306. }
  307. free(node_addr_str);
  308. /*
  309. * Try to find node in if_addrs
  310. */
  311. node_found = 0;
  312. for (list = addrs.next; list != &addrs; list = list->next) {
  313. if_addr = list_entry(list, struct totem_ip_if_address, list);
  314. if (totemip_equal(&node_addr, &if_addr->ip_addr)) {
  315. node_found = 1;
  316. break;
  317. }
  318. }
  319. if (node_found) {
  320. break ;
  321. }
  322. }
  323. icmap_iter_finalize(iter);
  324. if (node_found) {
  325. /*
  326. * We found node, so create interface section
  327. */
  328. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  329. iter = icmap_iter_init(tmp_key);
  330. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  331. res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &ringnumber, tmp_key2);
  332. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  333. continue ;
  334. }
  335. if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
  336. continue;
  337. }
  338. snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", ringnumber);
  339. icmap_set_string(tmp_key2, node_addr_str);
  340. free(node_addr_str);
  341. }
  342. icmap_iter_finalize(iter);
  343. }
  344. }
  345. extern int totem_config_read (
  346. struct totem_config *totem_config,
  347. const char **error_string,
  348. uint64_t *warnings)
  349. {
  350. int res = 0;
  351. char *str;
  352. unsigned int ringnumber = 0;
  353. int member_count = 0;
  354. icmap_iter_t iter, member_iter;
  355. const char *iter_key;
  356. const char *member_iter_key;
  357. char ringnumber_key[ICMAP_KEYNAME_MAXLEN];
  358. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  359. uint8_t u8;
  360. uint16_t u16;
  361. char *cluster_name = NULL;
  362. int i;
  363. int local_node_pos;
  364. int nodeid_set;
  365. *warnings = 0;
  366. memset (totem_config, 0, sizeof (struct totem_config));
  367. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  368. if (totem_config->interfaces == 0) {
  369. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  370. return -1;
  371. }
  372. memset (totem_config->interfaces, 0,
  373. sizeof (struct totem_interface) * INTERFACE_MAX);
  374. strcpy (totem_config->rrp_mode, "none");
  375. icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
  376. totem_get_crypto(totem_config);
  377. if (icmap_get_string("totem.rrp_mode", &str) == CS_OK) {
  378. strcpy (totem_config->rrp_mode, str);
  379. free(str);
  380. }
  381. icmap_get_uint32("totem.nodeid", &totem_config->node_id);
  382. totem_config->clear_node_high_bit = 0;
  383. if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
  384. if (strcmp (str, "yes") == 0) {
  385. totem_config->clear_node_high_bit = 1;
  386. }
  387. free(str);
  388. }
  389. icmap_get_uint32("totem.threads", &totem_config->threads);
  390. icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
  391. icmap_get_string("totem.cluster_name", &cluster_name);
  392. /*
  393. * Get things that might change in the future
  394. */
  395. totem_volatile_config_read(totem_config);
  396. if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
  397. /*
  398. * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
  399. */
  400. config_convert_nodelist_to_interface(totem_config);
  401. } else {
  402. free(str);
  403. }
  404. iter = icmap_iter_init("totem.interface.");
  405. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  406. res = sscanf(iter_key, "totem.interface.%[^.].%s", ringnumber_key, tmp_key);
  407. if (res != 2) {
  408. continue;
  409. }
  410. if (strcmp(tmp_key, "bindnetaddr") != 0) {
  411. continue;
  412. }
  413. member_count = 0;
  414. ringnumber = atoi(ringnumber_key);
  415. /*
  416. * Get the bind net address
  417. */
  418. if (icmap_get_string(iter_key, &str) == CS_OK) {
  419. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  420. totem_config->interfaces[ringnumber].mcast_addr.family);
  421. free(str);
  422. }
  423. /*
  424. * Get interface multicast address
  425. */
  426. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", ringnumber);
  427. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  428. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  429. free(str);
  430. } else {
  431. /*
  432. * User not specified address -> autogenerate one from cluster_name key
  433. * (if available)
  434. */
  435. res = get_cluster_mcast_addr (cluster_name,
  436. &totem_config->interfaces[ringnumber].bindnet,
  437. ringnumber,
  438. &totem_config->interfaces[ringnumber].mcast_addr);
  439. }
  440. totem_config->broadcast_use = 0;
  441. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", ringnumber);
  442. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  443. if (strcmp (str, "yes") == 0) {
  444. totem_config->broadcast_use = 1;
  445. totemip_parse (
  446. &totem_config->interfaces[ringnumber].mcast_addr,
  447. "255.255.255.255", 0);
  448. }
  449. free(str);
  450. }
  451. /*
  452. * Get mcast port
  453. */
  454. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", ringnumber);
  455. if (icmap_get_uint16(tmp_key, &totem_config->interfaces[ringnumber].ip_port) != CS_OK) {
  456. if (totem_config->broadcast_use) {
  457. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT + (2 * ringnumber);
  458. } else {
  459. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT;
  460. }
  461. }
  462. /*
  463. * Get the TTL
  464. */
  465. totem_config->interfaces[ringnumber].ttl = 1;
  466. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", ringnumber);
  467. if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
  468. totem_config->interfaces[ringnumber].ttl = u8;
  469. }
  470. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", ringnumber);
  471. member_iter = icmap_iter_init(tmp_key);
  472. while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
  473. if (member_count == 0) {
  474. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  475. free(str);
  476. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
  477. break;
  478. } else {
  479. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
  480. }
  481. }
  482. if (icmap_get_string(member_iter_key, &str) == CS_OK) {
  483. res = totemip_parse (&totem_config->interfaces[ringnumber].member_list[member_count++],
  484. str, 0);
  485. }
  486. }
  487. icmap_iter_finalize(member_iter);
  488. totem_config->interfaces[ringnumber].member_count = member_count;
  489. totem_config->interface_count++;
  490. }
  491. icmap_iter_finalize(iter);
  492. /*
  493. * Store automatically generated items back to icmap
  494. */
  495. for (i = 0; i < totem_config->interface_count; i++) {
  496. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
  497. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  498. free(str);
  499. } else {
  500. str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
  501. icmap_set_string(tmp_key, str);
  502. }
  503. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
  504. if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
  505. icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
  506. }
  507. }
  508. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  509. if (icmap_get_string("totem.transport", &str) == CS_OK) {
  510. if (strcmp (str, "udpu") == 0) {
  511. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  512. }
  513. if (strcmp (str, "iba") == 0) {
  514. totem_config->transport_number = TOTEM_TRANSPORT_RDMA;
  515. }
  516. free(str);
  517. }
  518. free(cluster_name);
  519. /*
  520. * Check existence of nodelist
  521. */
  522. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  523. free(str);
  524. /*
  525. * find local node
  526. */
  527. local_node_pos = find_local_node_in_nodelist(totem_config);
  528. if (local_node_pos != -1) {
  529. icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
  530. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
  531. nodeid_set = (totem_config->node_id != 0);
  532. if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
  533. *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
  534. }
  535. /*
  536. * Make localnode ring0_addr read only, so we can be sure that local
  537. * node never changes. If rebinding to other IP would be in future
  538. * supported, this must be changed and handled properly!
  539. */
  540. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", local_node_pos);
  541. icmap_set_ro_access(tmp_key, 0, 1);
  542. icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
  543. }
  544. put_nodelist_members_to_config(totem_config);
  545. }
  546. add_totem_config_notification(totem_config);
  547. return 0;
  548. }
  549. int totem_config_validate (
  550. struct totem_config *totem_config,
  551. const char **error_string)
  552. {
  553. static char local_error_reason[512];
  554. char parse_error[512];
  555. const char *error_reason = local_error_reason;
  556. int i;
  557. unsigned int interface_max = INTERFACE_MAX;
  558. if (totem_config->interface_count == 0) {
  559. error_reason = "No interfaces defined";
  560. goto parse_error;
  561. }
  562. for (i = 0; i < totem_config->interface_count; i++) {
  563. /*
  564. * Some error checking of parsed data to make sure its valid
  565. */
  566. struct totem_ip_address null_addr;
  567. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  568. if ((totem_config->transport_number == 0) &&
  569. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  570. sizeof (struct totem_ip_address)) == 0) {
  571. error_reason = "No multicast address specified";
  572. goto parse_error;
  573. }
  574. if (totem_config->interfaces[i].ip_port == 0) {
  575. error_reason = "No multicast port specified";
  576. goto parse_error;
  577. }
  578. if (totem_config->interfaces[i].ttl > 255) {
  579. error_reason = "Invalid TTL (should be 0..255)";
  580. goto parse_error;
  581. }
  582. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  583. totem_config->interfaces[i].ttl != 1) {
  584. error_reason = "Can only set ttl on multicast transport types";
  585. goto parse_error;
  586. }
  587. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  588. totem_config->node_id == 0) {
  589. error_reason = "An IPV6 network requires that a node ID be specified.";
  590. goto parse_error;
  591. }
  592. if (totem_config->broadcast_use == 0 && totem_config->transport_number == 0) {
  593. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  594. error_reason = "Multicast address family does not match bind address family";
  595. goto parse_error;
  596. }
  597. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  598. error_reason = "Not all bind address belong to the same IP family";
  599. goto parse_error;
  600. }
  601. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  602. error_reason = "mcastaddr is not a correct multicast address.";
  603. goto parse_error;
  604. }
  605. }
  606. }
  607. if (totem_config->version != 2) {
  608. error_reason = "This totem parser can only parse version 2 configurations.";
  609. goto parse_error;
  610. }
  611. if (totem_config->token_retransmits_before_loss_const == 0) {
  612. totem_config->token_retransmits_before_loss_const =
  613. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  614. }
  615. /*
  616. * Setup timeout values that are not setup by user
  617. */
  618. if (totem_config->token_timeout == 0) {
  619. totem_config->token_timeout = TOKEN_TIMEOUT;
  620. if (totem_config->token_retransmits_before_loss_const == 0) {
  621. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  622. }
  623. if (totem_config->token_retransmit_timeout == 0) {
  624. totem_config->token_retransmit_timeout =
  625. (int)(totem_config->token_timeout /
  626. (totem_config->token_retransmits_before_loss_const + 0.2));
  627. }
  628. if (totem_config->token_hold_timeout == 0) {
  629. totem_config->token_hold_timeout =
  630. (int)(totem_config->token_retransmit_timeout * 0.8 -
  631. (1000/HZ));
  632. }
  633. }
  634. if (totem_config->max_network_delay == 0) {
  635. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  636. }
  637. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  638. snprintf (local_error_reason, sizeof(local_error_reason),
  639. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  640. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  641. goto parse_error;
  642. }
  643. if (totem_config->window_size == 0) {
  644. totem_config->window_size = WINDOW_SIZE;
  645. }
  646. if (totem_config->max_messages == 0) {
  647. totem_config->max_messages = MAX_MESSAGES;
  648. }
  649. if (totem_config->miss_count_const == 0) {
  650. totem_config->miss_count_const = MISS_COUNT_CONST;
  651. }
  652. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  653. snprintf (local_error_reason, sizeof(local_error_reason),
  654. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  655. totem_config->token_timeout, MINIMUM_TIMEOUT);
  656. goto parse_error;
  657. }
  658. if (totem_config->token_retransmit_timeout == 0) {
  659. totem_config->token_retransmit_timeout =
  660. (int)(totem_config->token_timeout /
  661. (totem_config->token_retransmits_before_loss_const + 0.2));
  662. }
  663. if (totem_config->token_hold_timeout == 0) {
  664. totem_config->token_hold_timeout =
  665. (int)(totem_config->token_retransmit_timeout * 0.8 -
  666. (1000/HZ));
  667. }
  668. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  669. snprintf (local_error_reason, sizeof(local_error_reason),
  670. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  671. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  672. goto parse_error;
  673. }
  674. if (totem_config->token_hold_timeout == 0) {
  675. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  676. }
  677. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  678. snprintf (local_error_reason, sizeof(local_error_reason),
  679. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  680. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  681. goto parse_error;
  682. }
  683. if (totem_config->join_timeout == 0) {
  684. totem_config->join_timeout = JOIN_TIMEOUT;
  685. }
  686. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  687. snprintf (local_error_reason, sizeof(local_error_reason),
  688. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  689. totem_config->join_timeout, MINIMUM_TIMEOUT);
  690. goto parse_error;
  691. }
  692. if (totem_config->consensus_timeout == 0) {
  693. totem_config->consensus_timeout = (int)(float)(1.2 * totem_config->token_timeout);
  694. }
  695. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  696. snprintf (local_error_reason, sizeof(local_error_reason),
  697. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  698. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  699. goto parse_error;
  700. }
  701. if (totem_config->merge_timeout == 0) {
  702. totem_config->merge_timeout = MERGE_TIMEOUT;
  703. }
  704. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  705. snprintf (local_error_reason, sizeof(local_error_reason),
  706. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  707. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  708. goto parse_error;
  709. }
  710. if (totem_config->downcheck_timeout == 0) {
  711. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  712. }
  713. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  714. snprintf (local_error_reason, sizeof(local_error_reason),
  715. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  716. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  717. goto parse_error;
  718. }
  719. /*
  720. * RRP values validation
  721. */
  722. if (strcmp (totem_config->rrp_mode, "none") &&
  723. strcmp (totem_config->rrp_mode, "active") &&
  724. strcmp (totem_config->rrp_mode, "passive")) {
  725. snprintf (local_error_reason, sizeof(local_error_reason),
  726. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  727. goto parse_error;
  728. }
  729. if (totem_config->rrp_problem_count_timeout == 0) {
  730. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  731. }
  732. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  733. snprintf (local_error_reason, sizeof(local_error_reason),
  734. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  735. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  736. goto parse_error;
  737. }
  738. if (totem_config->rrp_problem_count_threshold == 0) {
  739. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  740. }
  741. if (totem_config->rrp_problem_count_mcast_threshold == 0) {
  742. totem_config->rrp_problem_count_mcast_threshold = totem_config->rrp_problem_count_threshold * 10;
  743. }
  744. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  745. snprintf (local_error_reason, sizeof(local_error_reason),
  746. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  747. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  748. goto parse_error;
  749. }
  750. if (totem_config->rrp_problem_count_mcast_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  751. snprintf (local_error_reason, sizeof(local_error_reason),
  752. "The RRP multicast problem count threshold (%d problem count) may not be less then (%d problem count).",
  753. totem_config->rrp_problem_count_mcast_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  754. goto parse_error;
  755. }
  756. if (totem_config->rrp_token_expired_timeout == 0) {
  757. totem_config->rrp_token_expired_timeout =
  758. totem_config->token_retransmit_timeout;
  759. }
  760. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  761. snprintf (local_error_reason, sizeof(local_error_reason),
  762. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  763. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  764. goto parse_error;
  765. }
  766. if (totem_config->rrp_autorecovery_check_timeout == 0) {
  767. totem_config->rrp_autorecovery_check_timeout = RRP_AUTORECOVERY_CHECK_TIMEOUT;
  768. }
  769. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  770. interface_max = 1;
  771. }
  772. if (interface_max < totem_config->interface_count) {
  773. snprintf (parse_error, sizeof(parse_error),
  774. "%d is too many configured interfaces for the rrp_mode setting %s.",
  775. totem_config->interface_count,
  776. totem_config->rrp_mode);
  777. error_reason = parse_error;
  778. goto parse_error;
  779. }
  780. if (totem_config->fail_to_recv_const == 0) {
  781. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  782. }
  783. if (totem_config->seqno_unchanged_const == 0) {
  784. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  785. }
  786. if (totem_config->net_mtu == 0) {
  787. totem_config->net_mtu = 1500;
  788. }
  789. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  790. snprintf (local_error_reason, sizeof(local_error_reason),
  791. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  792. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  793. goto parse_error;
  794. }
  795. if (totem_config->threads > SEND_THREADS_MAX) {
  796. totem_config->threads = SEND_THREADS_MAX;
  797. }
  798. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  799. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  800. goto parse_error;
  801. }
  802. if (totem_config->vsf_type == NULL) {
  803. totem_config->vsf_type = "none";
  804. }
  805. return (0);
  806. parse_error:
  807. snprintf (error_string_response, sizeof(error_string_response),
  808. "parse error in config: %s\n", error_reason);
  809. *error_string = error_string_response;
  810. return (-1);
  811. }
  812. static int read_keyfile (
  813. const char *key_location,
  814. struct totem_config *totem_config,
  815. const char **error_string)
  816. {
  817. int fd;
  818. int res;
  819. ssize_t expected_key_len = sizeof (totem_config->private_key);
  820. int saved_errno;
  821. char error_str[100];
  822. const char *error_ptr;
  823. fd = open (key_location, O_RDONLY);
  824. if (fd == -1) {
  825. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  826. snprintf (error_string_response, sizeof(error_string_response),
  827. "Could not open %s: %s\n",
  828. key_location, error_ptr);
  829. goto parse_error;
  830. }
  831. res = read (fd, totem_config->private_key, expected_key_len);
  832. saved_errno = errno;
  833. close (fd);
  834. if (res == -1) {
  835. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  836. snprintf (error_string_response, sizeof(error_string_response),
  837. "Could not read %s: %s\n",
  838. key_location, error_ptr);
  839. goto parse_error;
  840. }
  841. totem_config->private_key_len = expected_key_len;
  842. if (res != expected_key_len) {
  843. snprintf (error_string_response, sizeof(error_string_response),
  844. "Could only read %d bits of 1024 bits from %s.\n",
  845. res * 8, key_location);
  846. goto parse_error;
  847. }
  848. return 0;
  849. parse_error:
  850. *error_string = error_string_response;
  851. return (-1);
  852. }
  853. int totem_config_keyread (
  854. struct totem_config *totem_config,
  855. const char **error_string)
  856. {
  857. int got_key = 0;
  858. char *key_location = NULL;
  859. int res;
  860. size_t key_len;
  861. memset (totem_config->private_key, 0, 128);
  862. totem_config->private_key_len = 128;
  863. if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
  864. strcmp(totem_config->crypto_hash_type, "none") == 0) {
  865. return (0);
  866. }
  867. /* cmap may store the location of the key file */
  868. if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
  869. res = read_keyfile(key_location, totem_config, error_string);
  870. free(key_location);
  871. if (res) {
  872. goto key_error;
  873. }
  874. got_key = 1;
  875. } else { /* Or the key itself may be in the cmap */
  876. if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
  877. if (key_len > sizeof (totem_config->private_key)) {
  878. sprintf(error_string_response, "key is too long");
  879. goto key_error;
  880. }
  881. if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
  882. totem_config->private_key_len = key_len;
  883. got_key = 1;
  884. } else {
  885. sprintf(error_string_response, "can't store private key");
  886. goto key_error;
  887. }
  888. }
  889. }
  890. /* In desperation we read the default filename */
  891. if (!got_key) {
  892. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  893. if (!filename)
  894. filename = COROSYSCONFDIR "/authkey";
  895. res = read_keyfile(filename, totem_config, error_string);
  896. if (res)
  897. goto key_error;
  898. }
  899. return (0);
  900. key_error:
  901. *error_string = error_string_response;
  902. return (-1);
  903. }
  904. static void totem_change_notify(
  905. int32_t event,
  906. const char *key_name,
  907. struct icmap_notify_value new_val,
  908. struct icmap_notify_value old_val,
  909. void *user_data)
  910. {
  911. totem_volatile_config_read((struct totem_config *)user_data);
  912. }
  913. static void add_totem_config_notification(struct totem_config *totem_config)
  914. {
  915. icmap_track_t icmap_track;
  916. icmap_track_add("totem.",
  917. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  918. totem_change_notify,
  919. totem_config,
  920. &icmap_track);
  921. }