totemconfig.c 32 KB

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