4
0

totemconfig.c 26 KB

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