totemconfig.c 31 KB

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