totemconfig.c 32 KB

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