totemconfig.c 32 KB

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