totemconfig.c 32 KB

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