totemconfig.c 30 KB

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