totemconfig.c 31 KB

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