totemconfig.c 32 KB

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