totemconfig.c 32 KB

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