totemconfig.c 31 KB

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