totemconfig.c 32 KB

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