totemconfig.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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 50
  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 5
  80. static char error_string_response[512];
  81. static struct objdb_iface_ver0 *global_objdb;
  82. static void add_totem_config_notification(
  83. struct objdb_iface_ver0 *objdb,
  84. struct totem_config *totem_config,
  85. hdb_handle_t totem_object_handle);
  86. /* These just makes the code below a little neater */
  87. static inline int objdb_get_string (
  88. const struct objdb_iface_ver0 *objdb,
  89. hdb_handle_t object_service_handle,
  90. const char *key, const char **value)
  91. {
  92. int res;
  93. *value = NULL;
  94. if ( !(res = objdb->object_key_get (object_service_handle,
  95. key,
  96. strlen (key),
  97. (void *)value,
  98. NULL))) {
  99. if (*value) {
  100. return 0;
  101. }
  102. }
  103. return -1;
  104. }
  105. static inline void objdb_get_int (
  106. const struct objdb_iface_ver0 *objdb,
  107. hdb_handle_t object_service_handle,
  108. const char *key, unsigned int *intvalue)
  109. {
  110. char *value = NULL;
  111. if (!objdb->object_key_get (object_service_handle,
  112. key,
  113. strlen (key),
  114. (void *)&value,
  115. NULL)) {
  116. if (value) {
  117. *intvalue = atoi(value);
  118. }
  119. }
  120. }
  121. static unsigned int totem_handle_find (
  122. struct objdb_iface_ver0 *objdb,
  123. hdb_handle_t *totem_find_handle) {
  124. hdb_handle_t object_find_handle;
  125. unsigned int res;
  126. /*
  127. * Find a network section
  128. */
  129. objdb->object_find_create (
  130. OBJECT_PARENT_HANDLE,
  131. "network",
  132. strlen ("network"),
  133. &object_find_handle);
  134. res = objdb->object_find_next (
  135. object_find_handle,
  136. totem_find_handle);
  137. objdb->object_find_destroy (object_find_handle);
  138. /*
  139. * Network section not found in configuration, checking for totem
  140. */
  141. if (res == -1) {
  142. objdb->object_find_create (
  143. OBJECT_PARENT_HANDLE,
  144. "totem",
  145. strlen ("totem"),
  146. &object_find_handle);
  147. res = objdb->object_find_next (
  148. object_find_handle,
  149. totem_find_handle);
  150. objdb->object_find_destroy (object_find_handle);
  151. }
  152. if (res == -1) {
  153. return (-1);
  154. }
  155. return (0);
  156. }
  157. static void totem_volatile_config_read (
  158. struct objdb_iface_ver0 *objdb,
  159. struct totem_config *totem_config,
  160. hdb_handle_t object_totem_handle)
  161. {
  162. objdb_get_int (objdb,object_totem_handle, "token", &totem_config->token_timeout);
  163. objdb_get_int (objdb,object_totem_handle, "token_retransmit", &totem_config->token_retransmit_timeout);
  164. objdb_get_int (objdb,object_totem_handle, "hold", &totem_config->token_hold_timeout);
  165. objdb_get_int (objdb,object_totem_handle, "token_retransmits_before_loss_const", &totem_config->token_retransmits_before_loss_const);
  166. objdb_get_int (objdb,object_totem_handle, "join", &totem_config->join_timeout);
  167. objdb_get_int (objdb,object_totem_handle, "send_join", &totem_config->send_join_timeout);
  168. objdb_get_int (objdb,object_totem_handle, "consensus", &totem_config->consensus_timeout);
  169. objdb_get_int (objdb,object_totem_handle, "merge", &totem_config->merge_timeout);
  170. objdb_get_int (objdb,object_totem_handle, "downcheck", &totem_config->downcheck_timeout);
  171. objdb_get_int (objdb,object_totem_handle, "fail_recv_const", &totem_config->fail_to_recv_const);
  172. objdb_get_int (objdb,object_totem_handle, "seqno_unchanged_const", &totem_config->seqno_unchanged_const);
  173. objdb_get_int (objdb,object_totem_handle, "rrp_token_expired_timeout", &totem_config->rrp_token_expired_timeout);
  174. objdb_get_int (objdb,object_totem_handle, "rrp_problem_count_timeout", &totem_config->rrp_problem_count_timeout);
  175. objdb_get_int (objdb,object_totem_handle, "rrp_problem_count_threshold", &totem_config->rrp_problem_count_threshold);
  176. objdb_get_int (objdb,object_totem_handle, "heartbeat_failures_allowed", &totem_config->heartbeat_failures_allowed);
  177. objdb_get_int (objdb,object_totem_handle, "max_network_delay", &totem_config->max_network_delay);
  178. objdb_get_int (objdb,object_totem_handle, "window_size", &totem_config->window_size);
  179. (void)objdb_get_string (objdb, object_totem_handle, "vsftype", &totem_config->vsf_type);
  180. objdb_get_int (objdb,object_totem_handle, "max_messages", &totem_config->max_messages);
  181. objdb_get_int (objdb,object_totem_handle, "miss_count_const", &totem_config->miss_count_const);
  182. }
  183. static void totem_get_crypto_type(
  184. const struct objdb_iface_ver0 *objdb,
  185. hdb_handle_t object_totem_handle,
  186. struct totem_config *totem_config)
  187. {
  188. const char *str;
  189. totem_config->crypto_accept = TOTEM_CRYPTO_ACCEPT_OLD;
  190. if (!objdb_get_string (objdb, object_totem_handle, "crypto_accept", &str)) {
  191. if (strcmp(str, "new") == 0) {
  192. totem_config->crypto_accept = TOTEM_CRYPTO_ACCEPT_NEW;
  193. }
  194. }
  195. totem_config->crypto_type = TOTEM_CRYPTO_SOBER;
  196. #ifdef HAVE_LIBNSS
  197. /*
  198. * We must set these even if the key does not exist.
  199. * Encryption type can be set on-the-fly using CFG
  200. */
  201. totem_config->crypto_crypt_type = CKM_AES_CBC_PAD;
  202. totem_config->crypto_sign_type = CKM_SHA256_RSA_PKCS;
  203. #endif
  204. if (!objdb_get_string (objdb, object_totem_handle, "crypto_type", &str)) {
  205. if (strcmp(str, "sober") == 0) {
  206. return;
  207. }
  208. #ifdef HAVE_LIBNSS
  209. if (strcmp(str, "nss") == 0) {
  210. totem_config->crypto_type = TOTEM_CRYPTO_NSS;
  211. }
  212. #endif
  213. }
  214. }
  215. extern int totem_config_read (
  216. struct objdb_iface_ver0 *objdb,
  217. struct totem_config *totem_config,
  218. const char **error_string)
  219. {
  220. int res = 0;
  221. hdb_handle_t object_totem_handle;
  222. hdb_handle_t object_interface_handle;
  223. hdb_handle_t object_member_handle;
  224. const char *str;
  225. unsigned int ringnumber = 0;
  226. hdb_handle_t object_find_interface_handle;
  227. hdb_handle_t object_find_member_handle;
  228. const char *transport_type;
  229. int member_count = 0;
  230. res = totem_handle_find (objdb, &object_totem_handle);
  231. if (res == -1) {
  232. printf ("couldn't find totem handle\n");
  233. return (-1);
  234. }
  235. memset (totem_config, 0, sizeof (struct totem_config));
  236. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  237. if (totem_config->interfaces == 0) {
  238. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  239. return -1;
  240. }
  241. memset (totem_config->interfaces, 0,
  242. sizeof (struct totem_interface) * INTERFACE_MAX);
  243. totem_config->secauth = 1;
  244. strcpy (totem_config->rrp_mode, "none");
  245. if (!objdb_get_string (objdb, object_totem_handle, "version", &str)) {
  246. if (strcmp (str, "2") == 0) {
  247. totem_config->version = 2;
  248. }
  249. }
  250. if (!objdb_get_string (objdb, object_totem_handle, "secauth", &str)) {
  251. if (strcmp (str, "on") == 0) {
  252. totem_config->secauth = 1;
  253. }
  254. if (strcmp (str, "off") == 0) {
  255. totem_config->secauth = 0;
  256. }
  257. }
  258. if (totem_config->secauth == 1) {
  259. totem_get_crypto_type(objdb, object_totem_handle, totem_config);
  260. }
  261. if (!objdb_get_string (objdb, object_totem_handle, "rrp_mode", &str)) {
  262. strcpy (totem_config->rrp_mode, str);
  263. }
  264. /*
  265. * Get interface node id
  266. */
  267. objdb_get_int (objdb, object_totem_handle, "nodeid", &totem_config->node_id);
  268. totem_config->clear_node_high_bit = 0;
  269. if (!objdb_get_string (objdb,object_totem_handle, "clear_node_high_bit", &str)) {
  270. if (strcmp (str, "yes") == 0) {
  271. totem_config->clear_node_high_bit = 1;
  272. }
  273. }
  274. objdb_get_int (objdb,object_totem_handle, "threads", &totem_config->threads);
  275. objdb_get_int (objdb,object_totem_handle, "netmtu", &totem_config->net_mtu);
  276. /*
  277. * Get things that might change in the future
  278. */
  279. totem_volatile_config_read (objdb, totem_config, object_totem_handle);
  280. objdb->object_find_create (
  281. object_totem_handle,
  282. "interface",
  283. strlen ("interface"),
  284. &object_find_interface_handle);
  285. while (objdb->object_find_next (
  286. object_find_interface_handle,
  287. &object_interface_handle) == 0) {
  288. member_count = 0;
  289. objdb_get_int (objdb, object_interface_handle, "ringnumber", &ringnumber);
  290. /*
  291. * Get interface multicast address
  292. */
  293. if (!objdb_get_string (objdb, object_interface_handle, "mcastaddr", &str)) {
  294. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  295. }
  296. totem_config->broadcast_use = 0;
  297. if (!objdb_get_string (objdb, object_interface_handle, "broadcast", &str)) {
  298. if (strcmp (str, "yes") == 0) {
  299. totem_config->broadcast_use = 1;
  300. totemip_parse (
  301. &totem_config->interfaces[ringnumber].mcast_addr,
  302. "255.255.255.255", 0);
  303. }
  304. }
  305. /*
  306. * Get mcast port
  307. */
  308. if (!objdb_get_string (objdb, object_interface_handle, "mcastport", &str)) {
  309. totem_config->interfaces[ringnumber].ip_port = atoi (str);
  310. }
  311. /*
  312. * Get the bind net address
  313. */
  314. if (!objdb_get_string (objdb, object_interface_handle, "bindnetaddr", &str)) {
  315. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  316. totem_config->interfaces[ringnumber].mcast_addr.family);
  317. }
  318. objdb->object_find_create (
  319. object_interface_handle,
  320. "member",
  321. strlen ("member"),
  322. &object_find_member_handle);
  323. while (objdb->object_find_next (
  324. object_find_member_handle,
  325. &object_member_handle) == 0) {
  326. if (!objdb_get_string (objdb, object_member_handle, "memberaddr", &str)) {
  327. res = totemip_parse (&totem_config->interfaces[ringnumber].member_list[member_count++], str, 0);
  328. }
  329. }
  330. totem_config->interfaces[ringnumber].member_count = member_count;
  331. totem_config->interface_count++;
  332. }
  333. objdb->object_find_destroy (object_find_interface_handle);
  334. add_totem_config_notification(objdb, totem_config, object_totem_handle);
  335. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  336. (void)objdb_get_string (objdb, object_totem_handle, "transport", &transport_type);
  337. if (transport_type) {
  338. if (strcmp (transport_type, "udpu") == 0) {
  339. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  340. }
  341. }
  342. if (transport_type) {
  343. if (strcmp (transport_type, "iba") == 0) {
  344. totem_config->transport_number = TOTEM_TRANSPORT_RDMA;
  345. }
  346. }
  347. return 0;
  348. }
  349. int totem_config_validate (
  350. struct totem_config *totem_config,
  351. const char **error_string)
  352. {
  353. static char local_error_reason[512];
  354. char parse_error[512];
  355. const char *error_reason = local_error_reason;
  356. int i;
  357. unsigned int interface_max = INTERFACE_MAX;
  358. if (totem_config->interface_count == 0) {
  359. error_reason = "No interfaces defined";
  360. goto parse_error;
  361. }
  362. for (i = 0; i < totem_config->interface_count; i++) {
  363. /*
  364. * Some error checking of parsed data to make sure its valid
  365. */
  366. struct totem_ip_address null_addr;
  367. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  368. if ((totem_config->transport_number == 0) &&
  369. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  370. sizeof (struct totem_ip_address)) == 0) {
  371. error_reason = "No multicast address specified";
  372. goto parse_error;
  373. }
  374. if (totem_config->interfaces[i].ip_port == 0) {
  375. error_reason = "No multicast port specified";
  376. goto parse_error;
  377. }
  378. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  379. totem_config->node_id == 0) {
  380. error_reason = "An IPV6 network requires that a node ID be specified.";
  381. goto parse_error;
  382. }
  383. if (totem_config->broadcast_use == 0 && totem_config->transport_number == 0) {
  384. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  385. error_reason = "Multicast address family does not match bind address family";
  386. goto parse_error;
  387. }
  388. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  389. error_reason = "Not all bind address belong to the same IP family";
  390. goto parse_error;
  391. }
  392. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  393. error_reason = "mcastaddr is not a correct multicast address.";
  394. goto parse_error;
  395. }
  396. }
  397. }
  398. if (totem_config->version != 2) {
  399. error_reason = "This totem parser can only parse version 2 configurations.";
  400. goto parse_error;
  401. }
  402. if (totem_config->token_retransmits_before_loss_const == 0) {
  403. totem_config->token_retransmits_before_loss_const =
  404. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  405. }
  406. /*
  407. * Setup timeout values that are not setup by user
  408. */
  409. if (totem_config->token_timeout == 0) {
  410. totem_config->token_timeout = TOKEN_TIMEOUT;
  411. if (totem_config->token_retransmits_before_loss_const == 0) {
  412. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  413. }
  414. if (totem_config->token_retransmit_timeout == 0) {
  415. totem_config->token_retransmit_timeout =
  416. (int)(totem_config->token_timeout /
  417. (totem_config->token_retransmits_before_loss_const + 0.2));
  418. }
  419. if (totem_config->token_hold_timeout == 0) {
  420. totem_config->token_hold_timeout =
  421. (int)(totem_config->token_retransmit_timeout * 0.8 -
  422. (1000/HZ));
  423. }
  424. }
  425. if (totem_config->max_network_delay == 0) {
  426. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  427. }
  428. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  429. snprintf (local_error_reason, sizeof(local_error_reason),
  430. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  431. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  432. goto parse_error;
  433. }
  434. if (totem_config->window_size == 0) {
  435. totem_config->window_size = WINDOW_SIZE;
  436. }
  437. if (totem_config->max_messages == 0) {
  438. totem_config->max_messages = MAX_MESSAGES;
  439. }
  440. if (totem_config->miss_count_const == 0) {
  441. totem_config->miss_count_const = MISS_COUNT_CONST;
  442. }
  443. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  444. snprintf (local_error_reason, sizeof(local_error_reason),
  445. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  446. totem_config->token_timeout, MINIMUM_TIMEOUT);
  447. goto parse_error;
  448. }
  449. if (totem_config->token_retransmit_timeout == 0) {
  450. totem_config->token_retransmit_timeout =
  451. (int)(totem_config->token_timeout /
  452. (totem_config->token_retransmits_before_loss_const + 0.2));
  453. }
  454. if (totem_config->token_hold_timeout == 0) {
  455. totem_config->token_hold_timeout =
  456. (int)(totem_config->token_retransmit_timeout * 0.8 -
  457. (1000/HZ));
  458. }
  459. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  460. snprintf (local_error_reason, sizeof(local_error_reason),
  461. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  462. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  463. goto parse_error;
  464. }
  465. if (totem_config->token_hold_timeout == 0) {
  466. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  467. }
  468. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  469. snprintf (local_error_reason, sizeof(local_error_reason),
  470. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  471. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  472. goto parse_error;
  473. }
  474. if (totem_config->join_timeout == 0) {
  475. totem_config->join_timeout = JOIN_TIMEOUT;
  476. }
  477. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  478. snprintf (local_error_reason, sizeof(local_error_reason),
  479. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  480. totem_config->join_timeout, MINIMUM_TIMEOUT);
  481. goto parse_error;
  482. }
  483. if (totem_config->consensus_timeout == 0) {
  484. totem_config->consensus_timeout = (int)(float)(1.2 * totem_config->token_timeout);
  485. }
  486. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  487. snprintf (local_error_reason, sizeof(local_error_reason),
  488. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  489. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  490. goto parse_error;
  491. }
  492. if (totem_config->merge_timeout == 0) {
  493. totem_config->merge_timeout = MERGE_TIMEOUT;
  494. }
  495. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  496. snprintf (local_error_reason, sizeof(local_error_reason),
  497. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  498. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  499. goto parse_error;
  500. }
  501. if (totem_config->downcheck_timeout == 0) {
  502. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  503. }
  504. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  505. snprintf (local_error_reason, sizeof(local_error_reason),
  506. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  507. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  508. goto parse_error;
  509. }
  510. /*
  511. * RRP values validation
  512. */
  513. if (strcmp (totem_config->rrp_mode, "none") &&
  514. strcmp (totem_config->rrp_mode, "active") &&
  515. strcmp (totem_config->rrp_mode, "passive")) {
  516. snprintf (local_error_reason, sizeof(local_error_reason),
  517. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  518. goto parse_error;
  519. }
  520. if (totem_config->rrp_problem_count_timeout == 0) {
  521. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  522. }
  523. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  524. snprintf (local_error_reason, sizeof(local_error_reason),
  525. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  526. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  527. goto parse_error;
  528. }
  529. if (totem_config->rrp_problem_count_threshold == 0) {
  530. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  531. }
  532. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  533. snprintf (local_error_reason, sizeof(local_error_reason),
  534. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  535. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  536. goto parse_error;
  537. }
  538. if (totem_config->rrp_token_expired_timeout == 0) {
  539. totem_config->rrp_token_expired_timeout =
  540. totem_config->token_retransmit_timeout;
  541. }
  542. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  543. snprintf (local_error_reason, sizeof(local_error_reason),
  544. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  545. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  546. goto parse_error;
  547. }
  548. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  549. interface_max = 1;
  550. }
  551. if (interface_max < totem_config->interface_count) {
  552. snprintf (parse_error, sizeof(parse_error),
  553. "%d is too many configured interfaces for the rrp_mode setting %s.",
  554. totem_config->interface_count,
  555. totem_config->rrp_mode);
  556. error_reason = parse_error;
  557. goto parse_error;
  558. }
  559. if (totem_config->fail_to_recv_const == 0) {
  560. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  561. }
  562. if (totem_config->seqno_unchanged_const == 0) {
  563. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  564. }
  565. if (totem_config->net_mtu == 0) {
  566. totem_config->net_mtu = 1500;
  567. }
  568. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  569. snprintf (local_error_reason, sizeof(local_error_reason),
  570. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  571. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  572. goto parse_error;
  573. }
  574. if (totem_config->threads > SEND_THREADS_MAX) {
  575. totem_config->threads = SEND_THREADS_MAX;
  576. }
  577. if (totem_config->secauth == 0) {
  578. totem_config->threads = 0;
  579. }
  580. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  581. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  582. goto parse_error;
  583. }
  584. if (totem_config->vsf_type == NULL) {
  585. totem_config->vsf_type = "none";
  586. }
  587. return (0);
  588. parse_error:
  589. snprintf (error_string_response, sizeof(error_string_response),
  590. "parse error in config: %s\n", error_reason);
  591. *error_string = error_string_response;
  592. return (-1);
  593. }
  594. static int read_keyfile (
  595. const char *key_location,
  596. struct totem_config *totem_config,
  597. const char **error_string)
  598. {
  599. int fd;
  600. int res;
  601. ssize_t expected_key_len = sizeof (totem_config->private_key);
  602. int saved_errno;
  603. char error_str[100];
  604. const char *error_ptr;
  605. fd = open (key_location, O_RDONLY);
  606. if (fd == -1) {
  607. LOGSYS_STRERROR_R (error_ptr, errno, error_str, sizeof(error_str));
  608. snprintf (error_string_response, sizeof(error_string_response),
  609. "Could not open %s: %s\n",
  610. key_location, error_ptr);
  611. goto parse_error;
  612. }
  613. res = read (fd, totem_config->private_key, expected_key_len);
  614. saved_errno = errno;
  615. close (fd);
  616. if (res == -1) {
  617. LOGSYS_STRERROR_R (error_ptr, saved_errno, error_str, sizeof(error_str));
  618. snprintf (error_string_response, sizeof(error_string_response),
  619. "Could not read %s: %s\n",
  620. key_location, error_ptr);
  621. goto parse_error;
  622. }
  623. totem_config->private_key_len = expected_key_len;
  624. if (res != expected_key_len) {
  625. snprintf (error_string_response, sizeof(error_string_response),
  626. "Could only read %d bits of 1024 bits from %s.\n",
  627. res * 8, key_location);
  628. goto parse_error;
  629. }
  630. return 0;
  631. parse_error:
  632. *error_string = error_string_response;
  633. return (-1);
  634. }
  635. int totem_config_keyread (
  636. struct objdb_iface_ver0 *objdb,
  637. struct totem_config *totem_config,
  638. const char **error_string)
  639. {
  640. int got_key = 0;
  641. const char *key_location = NULL;
  642. hdb_handle_t object_totem_handle;
  643. int res;
  644. memset (totem_config->private_key, 0, 128);
  645. totem_config->private_key_len = 128;
  646. if (totem_config->secauth == 0) {
  647. return (0);
  648. }
  649. res = totem_handle_find (objdb, &object_totem_handle);
  650. if (res == -1) {
  651. return (-1);
  652. }
  653. /* objdb may store the location of the key file */
  654. if (!objdb_get_string (objdb,object_totem_handle, "keyfile", &key_location)
  655. && key_location) {
  656. res = read_keyfile(key_location, totem_config, error_string);
  657. if (res) {
  658. goto key_error;
  659. }
  660. got_key = 1;
  661. } else { /* Or the key itself may be in the objdb */
  662. char *key = NULL;
  663. size_t key_len;
  664. res = objdb->object_key_get (object_totem_handle,
  665. "key",
  666. strlen ("key"),
  667. (void *)&key,
  668. &key_len);
  669. if (res == 0 && key) {
  670. if (key_len > sizeof (totem_config->private_key)) {
  671. goto key_error;
  672. }
  673. memcpy(totem_config->private_key, key, key_len);
  674. totem_config->private_key_len = key_len;
  675. got_key = 1;
  676. }
  677. }
  678. /* In desperation we read the default filename */
  679. if (!got_key) {
  680. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  681. if (!filename)
  682. filename = COROSYSCONFDIR "/authkey";
  683. res = read_keyfile(filename, totem_config, error_string);
  684. if (res)
  685. goto key_error;
  686. }
  687. return (0);
  688. key_error:
  689. *error_string = error_string_response;
  690. return (-1);
  691. }
  692. static void totem_key_change_notify(object_change_type_t change_type,
  693. hdb_handle_t parent_object_handle,
  694. hdb_handle_t object_handle,
  695. const void *object_name_pt, size_t object_name_len,
  696. const void *key_name_pt, size_t key_len,
  697. const void *key_value_pt, size_t key_value_len,
  698. void *priv_data_pt)
  699. {
  700. struct totem_config *totem_config = priv_data_pt;
  701. if (memcmp(object_name_pt, "totem", object_name_len) == 0)
  702. totem_volatile_config_read(global_objdb,
  703. totem_config,
  704. object_handle); // CHECK
  705. }
  706. static void totem_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  707. void *priv_data_pt)
  708. {
  709. struct totem_config *totem_config = priv_data_pt;
  710. hdb_handle_t totem_object_handle;
  711. if (totem_config == NULL)
  712. return;
  713. /*
  714. * A new totem {} key might exist, cancel the
  715. * existing notification at the start of reload,
  716. * and start a new one on the new object when
  717. * it's all settled.
  718. */
  719. if (type == OBJDB_RELOAD_NOTIFY_START) {
  720. global_objdb->object_track_stop(
  721. totem_key_change_notify,
  722. NULL,
  723. NULL,
  724. NULL,
  725. totem_config);
  726. }
  727. if (type == OBJDB_RELOAD_NOTIFY_END ||
  728. type == OBJDB_RELOAD_NOTIFY_FAILED) {
  729. if (!totem_handle_find(global_objdb,
  730. &totem_object_handle)) {
  731. global_objdb->object_track_start(totem_object_handle,
  732. 1,
  733. totem_key_change_notify,
  734. NULL, // object_create_notify,
  735. NULL, // object_destroy_notify,
  736. NULL, // object_reload_notify
  737. totem_config); // priv_data
  738. /*
  739. * Reload the configuration
  740. */
  741. totem_volatile_config_read(global_objdb,
  742. totem_config,
  743. totem_object_handle);
  744. }
  745. else {
  746. log_printf(LOGSYS_LEVEL_ERROR, "totem objdb tracking stopped, cannot find totem{} handle on objdb\n");
  747. }
  748. }
  749. }
  750. static void add_totem_config_notification(
  751. struct objdb_iface_ver0 *objdb,
  752. struct totem_config *totem_config,
  753. hdb_handle_t totem_object_handle)
  754. {
  755. global_objdb = objdb;
  756. objdb->object_track_start(totem_object_handle,
  757. 1,
  758. totem_key_change_notify,
  759. NULL, // object_create_notify,
  760. NULL, // object_destroy_notify,
  761. NULL, // object_reload_notify
  762. totem_config); // priv_data
  763. /*
  764. * Reload notify must be on the parent object
  765. */
  766. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  767. 1,
  768. NULL, // key_change_notify,
  769. NULL, // object_create_notify,
  770. NULL, // object_destroy_notify,
  771. totem_objdb_reload_notify, // object_reload_notify
  772. totem_config); // priv_data
  773. }