totemconfig.c 32 KB

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