totemconfig.c 31 KB

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