totemconfig.c 32 KB

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