4
0

totemconfig.c 32 KB

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