4
0

totemconfig.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. if (ringnumber >= INTERFACE_MAX) {
  428. snprintf (error_string_response, sizeof(error_string_response),
  429. "parse error in config: interface ring number %u is bigger then allowed maximum %u\n",
  430. ringnumber, INTERFACE_MAX - 1);
  431. *error_string = error_string_response;
  432. return -1;
  433. }
  434. /*
  435. * Get the bind net address
  436. */
  437. if (icmap_get_string(iter_key, &str) == CS_OK) {
  438. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  439. totem_config->interfaces[ringnumber].mcast_addr.family);
  440. free(str);
  441. }
  442. /*
  443. * Get interface multicast address
  444. */
  445. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", ringnumber);
  446. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  447. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  448. free(str);
  449. } else {
  450. /*
  451. * User not specified address -> autogenerate one from cluster_name key
  452. * (if available)
  453. */
  454. res = get_cluster_mcast_addr (cluster_name,
  455. &totem_config->interfaces[ringnumber].bindnet,
  456. ringnumber,
  457. &totem_config->interfaces[ringnumber].mcast_addr);
  458. }
  459. totem_config->broadcast_use = 0;
  460. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", ringnumber);
  461. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  462. if (strcmp (str, "yes") == 0) {
  463. totem_config->broadcast_use = 1;
  464. totemip_parse (
  465. &totem_config->interfaces[ringnumber].mcast_addr,
  466. "255.255.255.255", 0);
  467. }
  468. free(str);
  469. }
  470. /*
  471. * Get mcast port
  472. */
  473. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", ringnumber);
  474. if (icmap_get_uint16(tmp_key, &totem_config->interfaces[ringnumber].ip_port) != CS_OK) {
  475. if (totem_config->broadcast_use) {
  476. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT + (2 * ringnumber);
  477. } else {
  478. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT;
  479. }
  480. }
  481. /*
  482. * Get the TTL
  483. */
  484. totem_config->interfaces[ringnumber].ttl = 1;
  485. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", ringnumber);
  486. if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
  487. totem_config->interfaces[ringnumber].ttl = u8;
  488. }
  489. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", ringnumber);
  490. member_iter = icmap_iter_init(tmp_key);
  491. while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
  492. if (member_count == 0) {
  493. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  494. free(str);
  495. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
  496. break;
  497. } else {
  498. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
  499. }
  500. }
  501. if (icmap_get_string(member_iter_key, &str) == CS_OK) {
  502. res = totemip_parse (&totem_config->interfaces[ringnumber].member_list[member_count++],
  503. str, 0);
  504. }
  505. }
  506. icmap_iter_finalize(member_iter);
  507. totem_config->interfaces[ringnumber].member_count = member_count;
  508. totem_config->interface_count++;
  509. }
  510. icmap_iter_finalize(iter);
  511. /*
  512. * Store automatically generated items back to icmap
  513. */
  514. for (i = 0; i < totem_config->interface_count; i++) {
  515. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
  516. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  517. free(str);
  518. } else {
  519. str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
  520. icmap_set_string(tmp_key, str);
  521. }
  522. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
  523. if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
  524. icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
  525. }
  526. }
  527. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  528. if (icmap_get_string("totem.transport", &str) == CS_OK) {
  529. if (strcmp (str, "udpu") == 0) {
  530. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  531. }
  532. if (strcmp (str, "iba") == 0) {
  533. totem_config->transport_number = TOTEM_TRANSPORT_RDMA;
  534. }
  535. free(str);
  536. }
  537. free(cluster_name);
  538. /*
  539. * Check existence of nodelist
  540. */
  541. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  542. free(str);
  543. /*
  544. * find local node
  545. */
  546. local_node_pos = find_local_node_in_nodelist(totem_config);
  547. if (local_node_pos != -1) {
  548. icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
  549. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
  550. nodeid_set = (totem_config->node_id != 0);
  551. if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
  552. *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
  553. }
  554. /*
  555. * Make localnode ring0_addr read only, so we can be sure that local
  556. * node never changes. If rebinding to other IP would be in future
  557. * supported, this must be changed and handled properly!
  558. */
  559. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", local_node_pos);
  560. icmap_set_ro_access(tmp_key, 0, 1);
  561. icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
  562. }
  563. put_nodelist_members_to_config(totem_config);
  564. }
  565. add_totem_config_notification(totem_config);
  566. return 0;
  567. }
  568. int totem_config_validate (
  569. struct totem_config *totem_config,
  570. const char **error_string)
  571. {
  572. static char local_error_reason[512];
  573. char parse_error[512];
  574. const char *error_reason = local_error_reason;
  575. int i;
  576. unsigned int interface_max = INTERFACE_MAX;
  577. if (totem_config->interface_count == 0) {
  578. error_reason = "No interfaces defined";
  579. goto parse_error;
  580. }
  581. for (i = 0; i < totem_config->interface_count; i++) {
  582. /*
  583. * Some error checking of parsed data to make sure its valid
  584. */
  585. struct totem_ip_address null_addr;
  586. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  587. if ((totem_config->transport_number == 0) &&
  588. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  589. sizeof (struct totem_ip_address)) == 0) {
  590. error_reason = "No multicast address specified";
  591. goto parse_error;
  592. }
  593. if (totem_config->interfaces[i].ip_port == 0) {
  594. error_reason = "No multicast port specified";
  595. goto parse_error;
  596. }
  597. if (totem_config->interfaces[i].ttl > 255) {
  598. error_reason = "Invalid TTL (should be 0..255)";
  599. goto parse_error;
  600. }
  601. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  602. totem_config->interfaces[i].ttl != 1) {
  603. error_reason = "Can only set ttl on multicast transport types";
  604. goto parse_error;
  605. }
  606. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  607. totem_config->node_id == 0) {
  608. error_reason = "An IPV6 network requires that a node ID be specified.";
  609. goto parse_error;
  610. }
  611. if (totem_config->broadcast_use == 0 && totem_config->transport_number == 0) {
  612. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  613. error_reason = "Multicast address family does not match bind address family";
  614. goto parse_error;
  615. }
  616. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  617. error_reason = "Not all bind address belong to the same IP family";
  618. goto parse_error;
  619. }
  620. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  621. error_reason = "mcastaddr is not a correct multicast address.";
  622. goto parse_error;
  623. }
  624. }
  625. }
  626. if (totem_config->version != 2) {
  627. error_reason = "This totem parser can only parse version 2 configurations.";
  628. goto parse_error;
  629. }
  630. if (totem_config->token_retransmits_before_loss_const == 0) {
  631. totem_config->token_retransmits_before_loss_const =
  632. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  633. }
  634. /*
  635. * Setup timeout values that are not setup by user
  636. */
  637. if (totem_config->token_timeout == 0) {
  638. totem_config->token_timeout = TOKEN_TIMEOUT;
  639. if (totem_config->token_retransmits_before_loss_const == 0) {
  640. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  641. }
  642. if (totem_config->token_retransmit_timeout == 0) {
  643. totem_config->token_retransmit_timeout =
  644. (int)(totem_config->token_timeout /
  645. (totem_config->token_retransmits_before_loss_const + 0.2));
  646. }
  647. if (totem_config->token_hold_timeout == 0) {
  648. totem_config->token_hold_timeout =
  649. (int)(totem_config->token_retransmit_timeout * 0.8 -
  650. (1000/HZ));
  651. }
  652. }
  653. if (totem_config->max_network_delay == 0) {
  654. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  655. }
  656. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  657. snprintf (local_error_reason, sizeof(local_error_reason),
  658. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  659. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  660. goto parse_error;
  661. }
  662. if (totem_config->window_size == 0) {
  663. totem_config->window_size = WINDOW_SIZE;
  664. }
  665. if (totem_config->max_messages == 0) {
  666. totem_config->max_messages = MAX_MESSAGES;
  667. }
  668. if (totem_config->miss_count_const == 0) {
  669. totem_config->miss_count_const = MISS_COUNT_CONST;
  670. }
  671. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  672. snprintf (local_error_reason, sizeof(local_error_reason),
  673. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  674. totem_config->token_timeout, MINIMUM_TIMEOUT);
  675. goto parse_error;
  676. }
  677. if (totem_config->token_retransmit_timeout == 0) {
  678. totem_config->token_retransmit_timeout =
  679. (int)(totem_config->token_timeout /
  680. (totem_config->token_retransmits_before_loss_const + 0.2));
  681. }
  682. if (totem_config->token_hold_timeout == 0) {
  683. totem_config->token_hold_timeout =
  684. (int)(totem_config->token_retransmit_timeout * 0.8 -
  685. (1000/HZ));
  686. }
  687. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  688. snprintf (local_error_reason, sizeof(local_error_reason),
  689. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  690. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  691. goto parse_error;
  692. }
  693. if (totem_config->token_hold_timeout == 0) {
  694. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  695. }
  696. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  697. snprintf (local_error_reason, sizeof(local_error_reason),
  698. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  699. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  700. goto parse_error;
  701. }
  702. if (totem_config->join_timeout == 0) {
  703. totem_config->join_timeout = JOIN_TIMEOUT;
  704. }
  705. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  706. snprintf (local_error_reason, sizeof(local_error_reason),
  707. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  708. totem_config->join_timeout, MINIMUM_TIMEOUT);
  709. goto parse_error;
  710. }
  711. if (totem_config->consensus_timeout == 0) {
  712. totem_config->consensus_timeout = (int)(float)(1.2 * totem_config->token_timeout);
  713. }
  714. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  715. snprintf (local_error_reason, sizeof(local_error_reason),
  716. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  717. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  718. goto parse_error;
  719. }
  720. if (totem_config->merge_timeout == 0) {
  721. totem_config->merge_timeout = MERGE_TIMEOUT;
  722. }
  723. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  724. snprintf (local_error_reason, sizeof(local_error_reason),
  725. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  726. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  727. goto parse_error;
  728. }
  729. if (totem_config->downcheck_timeout == 0) {
  730. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  731. }
  732. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  733. snprintf (local_error_reason, sizeof(local_error_reason),
  734. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  735. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  736. goto parse_error;
  737. }
  738. /*
  739. * RRP values validation
  740. */
  741. if (strcmp (totem_config->rrp_mode, "none") &&
  742. strcmp (totem_config->rrp_mode, "active") &&
  743. strcmp (totem_config->rrp_mode, "passive")) {
  744. snprintf (local_error_reason, sizeof(local_error_reason),
  745. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  746. goto parse_error;
  747. }
  748. if (totem_config->rrp_problem_count_timeout == 0) {
  749. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  750. }
  751. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  752. snprintf (local_error_reason, sizeof(local_error_reason),
  753. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  754. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  755. goto parse_error;
  756. }
  757. if (totem_config->rrp_problem_count_threshold == 0) {
  758. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  759. }
  760. if (totem_config->rrp_problem_count_mcast_threshold == 0) {
  761. totem_config->rrp_problem_count_mcast_threshold = totem_config->rrp_problem_count_threshold * 10;
  762. }
  763. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  764. snprintf (local_error_reason, sizeof(local_error_reason),
  765. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  766. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  767. goto parse_error;
  768. }
  769. if (totem_config->rrp_problem_count_mcast_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  770. snprintf (local_error_reason, sizeof(local_error_reason),
  771. "The RRP multicast problem count threshold (%d problem count) may not be less then (%d problem count).",
  772. totem_config->rrp_problem_count_mcast_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  773. goto parse_error;
  774. }
  775. if (totem_config->rrp_token_expired_timeout == 0) {
  776. totem_config->rrp_token_expired_timeout =
  777. totem_config->token_retransmit_timeout;
  778. }
  779. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  780. snprintf (local_error_reason, sizeof(local_error_reason),
  781. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  782. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  783. goto parse_error;
  784. }
  785. if (totem_config->rrp_autorecovery_check_timeout == 0) {
  786. totem_config->rrp_autorecovery_check_timeout = RRP_AUTORECOVERY_CHECK_TIMEOUT;
  787. }
  788. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  789. interface_max = 1;
  790. }
  791. if (interface_max < totem_config->interface_count) {
  792. snprintf (parse_error, sizeof(parse_error),
  793. "%d is too many configured interfaces for the rrp_mode setting %s.",
  794. totem_config->interface_count,
  795. totem_config->rrp_mode);
  796. error_reason = parse_error;
  797. goto parse_error;
  798. }
  799. if (totem_config->fail_to_recv_const == 0) {
  800. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  801. }
  802. if (totem_config->seqno_unchanged_const == 0) {
  803. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  804. }
  805. if (totem_config->net_mtu == 0) {
  806. totem_config->net_mtu = 1500;
  807. }
  808. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  809. snprintf (local_error_reason, sizeof(local_error_reason),
  810. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  811. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  812. goto parse_error;
  813. }
  814. if (totem_config->threads > SEND_THREADS_MAX) {
  815. totem_config->threads = SEND_THREADS_MAX;
  816. }
  817. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  818. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  819. goto parse_error;
  820. }
  821. if (totem_config->vsf_type == NULL) {
  822. totem_config->vsf_type = "none";
  823. }
  824. return (0);
  825. parse_error:
  826. snprintf (error_string_response, sizeof(error_string_response),
  827. "parse error in config: %s\n", error_reason);
  828. *error_string = error_string_response;
  829. return (-1);
  830. }
  831. static int read_keyfile (
  832. const char *key_location,
  833. struct totem_config *totem_config,
  834. const char **error_string)
  835. {
  836. int fd;
  837. int res;
  838. ssize_t expected_key_len = sizeof (totem_config->private_key);
  839. int saved_errno;
  840. char error_str[100];
  841. const char *error_ptr;
  842. fd = open (key_location, O_RDONLY);
  843. if (fd == -1) {
  844. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  845. snprintf (error_string_response, sizeof(error_string_response),
  846. "Could not open %s: %s\n",
  847. key_location, error_ptr);
  848. goto parse_error;
  849. }
  850. res = read (fd, totem_config->private_key, expected_key_len);
  851. saved_errno = errno;
  852. close (fd);
  853. if (res == -1) {
  854. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  855. snprintf (error_string_response, sizeof(error_string_response),
  856. "Could not read %s: %s\n",
  857. key_location, error_ptr);
  858. goto parse_error;
  859. }
  860. totem_config->private_key_len = expected_key_len;
  861. if (res != expected_key_len) {
  862. snprintf (error_string_response, sizeof(error_string_response),
  863. "Could only read %d bits of 1024 bits from %s.\n",
  864. res * 8, key_location);
  865. goto parse_error;
  866. }
  867. return 0;
  868. parse_error:
  869. *error_string = error_string_response;
  870. return (-1);
  871. }
  872. int totem_config_keyread (
  873. struct totem_config *totem_config,
  874. const char **error_string)
  875. {
  876. int got_key = 0;
  877. char *key_location = NULL;
  878. int res;
  879. size_t key_len;
  880. memset (totem_config->private_key, 0, 128);
  881. totem_config->private_key_len = 128;
  882. if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
  883. strcmp(totem_config->crypto_hash_type, "none") == 0) {
  884. return (0);
  885. }
  886. /* cmap may store the location of the key file */
  887. if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
  888. res = read_keyfile(key_location, totem_config, error_string);
  889. free(key_location);
  890. if (res) {
  891. goto key_error;
  892. }
  893. got_key = 1;
  894. } else { /* Or the key itself may be in the cmap */
  895. if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
  896. if (key_len > sizeof (totem_config->private_key)) {
  897. sprintf(error_string_response, "key is too long");
  898. goto key_error;
  899. }
  900. if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
  901. totem_config->private_key_len = key_len;
  902. got_key = 1;
  903. } else {
  904. sprintf(error_string_response, "can't store private key");
  905. goto key_error;
  906. }
  907. }
  908. }
  909. /* In desperation we read the default filename */
  910. if (!got_key) {
  911. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  912. if (!filename)
  913. filename = COROSYSCONFDIR "/authkey";
  914. res = read_keyfile(filename, totem_config, error_string);
  915. if (res)
  916. goto key_error;
  917. }
  918. return (0);
  919. key_error:
  920. *error_string = error_string_response;
  921. return (-1);
  922. }
  923. static void totem_change_notify(
  924. int32_t event,
  925. const char *key_name,
  926. struct icmap_notify_value new_val,
  927. struct icmap_notify_value old_val,
  928. void *user_data)
  929. {
  930. totem_volatile_config_read((struct totem_config *)user_data);
  931. }
  932. static void add_totem_config_notification(struct totem_config *totem_config)
  933. {
  934. icmap_track_t icmap_track;
  935. icmap_track_add("totem.",
  936. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  937. totem_change_notify,
  938. totem_config,
  939. &icmap_track);
  940. }