totemconfig.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 <qb/qbdefs.h>
  51. #include <corosync/totem/totem.h>
  52. #include <corosync/engine/objdb.h>
  53. #include <corosync/engine/config.h>
  54. #include <corosync/engine/logsys.h>
  55. #ifdef HAVE_LIBNSS
  56. #include <nss.h>
  57. #include <pk11pub.h>
  58. #include <pkcs11.h>
  59. #include <prerror.h>
  60. #endif
  61. #include "util.h"
  62. #include "totemconfig.h"
  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. objdb->object_find_create (
  284. object_totem_handle,
  285. "interface",
  286. strlen ("interface"),
  287. &object_find_interface_handle);
  288. while (objdb->object_find_next (
  289. object_find_interface_handle,
  290. &object_interface_handle) == 0) {
  291. member_count = 0;
  292. objdb_get_int (objdb, object_interface_handle, "ringnumber", &ringnumber);
  293. /*
  294. * Get interface multicast address
  295. */
  296. if (!objdb_get_string (objdb, object_interface_handle, "mcastaddr", &str)) {
  297. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  298. }
  299. totem_config->broadcast_use = 0;
  300. if (!objdb_get_string (objdb, object_interface_handle, "broadcast", &str)) {
  301. if (strcmp (str, "yes") == 0) {
  302. totem_config->broadcast_use = 1;
  303. totemip_parse (
  304. &totem_config->interfaces[ringnumber].mcast_addr,
  305. "255.255.255.255", 0);
  306. }
  307. }
  308. /*
  309. * Get mcast port
  310. */
  311. if (!objdb_get_string (objdb, object_interface_handle, "mcastport", &str)) {
  312. totem_config->interfaces[ringnumber].ip_port = atoi (str);
  313. }
  314. /*
  315. * Get the bind net address
  316. */
  317. if (!objdb_get_string (objdb, object_interface_handle, "bindnetaddr", &str)) {
  318. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  319. totem_config->interfaces[ringnumber].mcast_addr.family);
  320. }
  321. /*
  322. * Get the TTL
  323. */
  324. totem_config->interfaces[ringnumber].ttl = 1;
  325. if (!objdb_get_string (objdb, object_interface_handle, "ttl", &str)) {
  326. totem_config->interfaces[ringnumber].ttl = atoi (str);
  327. }
  328. objdb->object_find_create (
  329. object_interface_handle,
  330. "member",
  331. strlen ("member"),
  332. &object_find_member_handle);
  333. while (objdb->object_find_next (
  334. object_find_member_handle,
  335. &object_member_handle) == 0) {
  336. if (!objdb_get_string (objdb, object_member_handle, "memberaddr", &str)) {
  337. res = totemip_parse (&totem_config->interfaces[ringnumber].member_list[member_count++], str, 0);
  338. }
  339. }
  340. totem_config->interfaces[ringnumber].member_count = member_count;
  341. totem_config->interface_count++;
  342. objdb->object_find_destroy (object_find_member_handle);
  343. }
  344. objdb->object_find_destroy (object_find_interface_handle);
  345. add_totem_config_notification(objdb, totem_config, object_totem_handle);
  346. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  347. (void)objdb_get_string (objdb, object_totem_handle, "transport", &transport_type);
  348. if (transport_type) {
  349. if (strcmp (transport_type, "udpu") == 0) {
  350. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  351. }
  352. }
  353. if (transport_type) {
  354. if (strcmp (transport_type, "iba") == 0) {
  355. totem_config->transport_number = TOTEM_TRANSPORT_RDMA;
  356. }
  357. }
  358. return 0;
  359. }
  360. int totem_config_validate (
  361. struct totem_config *totem_config,
  362. const char **error_string)
  363. {
  364. static char local_error_reason[512];
  365. char parse_error[512];
  366. const char *error_reason = local_error_reason;
  367. int i;
  368. unsigned int interface_max = INTERFACE_MAX;
  369. if (totem_config->interface_count == 0) {
  370. error_reason = "No interfaces defined";
  371. goto parse_error;
  372. }
  373. for (i = 0; i < totem_config->interface_count; i++) {
  374. /*
  375. * Some error checking of parsed data to make sure its valid
  376. */
  377. struct totem_ip_address null_addr;
  378. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  379. if ((totem_config->transport_number == 0) &&
  380. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  381. sizeof (struct totem_ip_address)) == 0) {
  382. error_reason = "No multicast address specified";
  383. goto parse_error;
  384. }
  385. if (totem_config->interfaces[i].ip_port == 0) {
  386. error_reason = "No multicast port specified";
  387. goto parse_error;
  388. }
  389. if (totem_config->interfaces[i].ttl > 255) {
  390. error_reason = "Invalid TTL (should be 0..255)";
  391. goto parse_error;
  392. }
  393. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  394. totem_config->interfaces[i].ttl != 1) {
  395. error_reason = "Can only set ttl on multicast transport types";
  396. goto parse_error;
  397. }
  398. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  399. totem_config->node_id == 0) {
  400. error_reason = "An IPV6 network requires that a node ID be specified.";
  401. goto parse_error;
  402. }
  403. if (totem_config->broadcast_use == 0 && totem_config->transport_number == 0) {
  404. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  405. error_reason = "Multicast address family does not match bind address family";
  406. goto parse_error;
  407. }
  408. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  409. error_reason = "Not all bind address belong to the same IP family";
  410. goto parse_error;
  411. }
  412. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  413. error_reason = "mcastaddr is not a correct multicast address.";
  414. goto parse_error;
  415. }
  416. }
  417. }
  418. if (totem_config->version != 2) {
  419. error_reason = "This totem parser can only parse version 2 configurations.";
  420. goto parse_error;
  421. }
  422. if (totem_config->token_retransmits_before_loss_const == 0) {
  423. totem_config->token_retransmits_before_loss_const =
  424. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  425. }
  426. /*
  427. * Setup timeout values that are not setup by user
  428. */
  429. if (totem_config->token_timeout == 0) {
  430. totem_config->token_timeout = TOKEN_TIMEOUT;
  431. if (totem_config->token_retransmits_before_loss_const == 0) {
  432. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  433. }
  434. if (totem_config->token_retransmit_timeout == 0) {
  435. totem_config->token_retransmit_timeout =
  436. (int)(totem_config->token_timeout /
  437. (totem_config->token_retransmits_before_loss_const + 0.2));
  438. }
  439. if (totem_config->token_hold_timeout == 0) {
  440. totem_config->token_hold_timeout =
  441. (int)(totem_config->token_retransmit_timeout * 0.8 -
  442. (1000/HZ));
  443. }
  444. }
  445. if (totem_config->max_network_delay == 0) {
  446. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  447. }
  448. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  449. snprintf (local_error_reason, sizeof(local_error_reason),
  450. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  451. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  452. goto parse_error;
  453. }
  454. if (totem_config->window_size == 0) {
  455. totem_config->window_size = WINDOW_SIZE;
  456. }
  457. if (totem_config->max_messages == 0) {
  458. totem_config->max_messages = MAX_MESSAGES;
  459. }
  460. if (totem_config->miss_count_const == 0) {
  461. totem_config->miss_count_const = MISS_COUNT_CONST;
  462. }
  463. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  464. snprintf (local_error_reason, sizeof(local_error_reason),
  465. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  466. totem_config->token_timeout, MINIMUM_TIMEOUT);
  467. goto parse_error;
  468. }
  469. if (totem_config->token_retransmit_timeout == 0) {
  470. totem_config->token_retransmit_timeout =
  471. (int)(totem_config->token_timeout /
  472. (totem_config->token_retransmits_before_loss_const + 0.2));
  473. }
  474. if (totem_config->token_hold_timeout == 0) {
  475. totem_config->token_hold_timeout =
  476. (int)(totem_config->token_retransmit_timeout * 0.8 -
  477. (1000/HZ));
  478. }
  479. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  480. snprintf (local_error_reason, sizeof(local_error_reason),
  481. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  482. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  483. goto parse_error;
  484. }
  485. if (totem_config->token_hold_timeout == 0) {
  486. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  487. }
  488. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  489. snprintf (local_error_reason, sizeof(local_error_reason),
  490. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  491. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  492. goto parse_error;
  493. }
  494. if (totem_config->join_timeout == 0) {
  495. totem_config->join_timeout = JOIN_TIMEOUT;
  496. }
  497. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  498. snprintf (local_error_reason, sizeof(local_error_reason),
  499. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  500. totem_config->join_timeout, MINIMUM_TIMEOUT);
  501. goto parse_error;
  502. }
  503. if (totem_config->consensus_timeout == 0) {
  504. totem_config->consensus_timeout = (int)(float)(1.2 * totem_config->token_timeout);
  505. }
  506. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  507. snprintf (local_error_reason, sizeof(local_error_reason),
  508. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  509. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  510. goto parse_error;
  511. }
  512. if (totem_config->merge_timeout == 0) {
  513. totem_config->merge_timeout = MERGE_TIMEOUT;
  514. }
  515. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  516. snprintf (local_error_reason, sizeof(local_error_reason),
  517. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  518. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  519. goto parse_error;
  520. }
  521. if (totem_config->downcheck_timeout == 0) {
  522. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  523. }
  524. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  525. snprintf (local_error_reason, sizeof(local_error_reason),
  526. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  527. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  528. goto parse_error;
  529. }
  530. /*
  531. * RRP values validation
  532. */
  533. if (strcmp (totem_config->rrp_mode, "none") &&
  534. strcmp (totem_config->rrp_mode, "active") &&
  535. strcmp (totem_config->rrp_mode, "passive")) {
  536. snprintf (local_error_reason, sizeof(local_error_reason),
  537. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  538. goto parse_error;
  539. }
  540. if (totem_config->rrp_problem_count_timeout == 0) {
  541. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  542. }
  543. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  544. snprintf (local_error_reason, sizeof(local_error_reason),
  545. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  546. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  547. goto parse_error;
  548. }
  549. if (totem_config->rrp_problem_count_threshold == 0) {
  550. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  551. }
  552. if (totem_config->rrp_problem_count_mcast_threshold == 0) {
  553. totem_config->rrp_problem_count_mcast_threshold = totem_config->rrp_problem_count_threshold * 10;
  554. }
  555. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  556. snprintf (local_error_reason, sizeof(local_error_reason),
  557. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  558. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  559. goto parse_error;
  560. }
  561. if (totem_config->rrp_problem_count_mcast_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  562. snprintf (local_error_reason, sizeof(local_error_reason),
  563. "The RRP multicast problem count threshold (%d problem count) may not be less then (%d problem count).",
  564. totem_config->rrp_problem_count_mcast_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  565. goto parse_error;
  566. }
  567. if (totem_config->rrp_token_expired_timeout == 0) {
  568. totem_config->rrp_token_expired_timeout =
  569. totem_config->token_retransmit_timeout;
  570. }
  571. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  572. snprintf (local_error_reason, sizeof(local_error_reason),
  573. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  574. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  575. goto parse_error;
  576. }
  577. if (totem_config->rrp_autorecovery_check_timeout == 0) {
  578. totem_config->rrp_autorecovery_check_timeout = RRP_AUTORECOVERY_CHECK_TIMEOUT;
  579. }
  580. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  581. interface_max = 1;
  582. }
  583. if (interface_max < totem_config->interface_count) {
  584. snprintf (parse_error, sizeof(parse_error),
  585. "%d is too many configured interfaces for the rrp_mode setting %s.",
  586. totem_config->interface_count,
  587. totem_config->rrp_mode);
  588. error_reason = parse_error;
  589. goto parse_error;
  590. }
  591. if (totem_config->fail_to_recv_const == 0) {
  592. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  593. }
  594. if (totem_config->seqno_unchanged_const == 0) {
  595. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  596. }
  597. if (totem_config->net_mtu == 0) {
  598. totem_config->net_mtu = 1500;
  599. }
  600. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  601. snprintf (local_error_reason, sizeof(local_error_reason),
  602. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  603. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  604. goto parse_error;
  605. }
  606. if (totem_config->threads > SEND_THREADS_MAX) {
  607. totem_config->threads = SEND_THREADS_MAX;
  608. }
  609. if (totem_config->secauth == 0) {
  610. totem_config->threads = 0;
  611. }
  612. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  613. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  614. goto parse_error;
  615. }
  616. if (totem_config->vsf_type == NULL) {
  617. totem_config->vsf_type = "none";
  618. }
  619. return (0);
  620. parse_error:
  621. snprintf (error_string_response, sizeof(error_string_response),
  622. "parse error in config: %s\n", error_reason);
  623. *error_string = error_string_response;
  624. return (-1);
  625. }
  626. static int read_keyfile (
  627. const char *key_location,
  628. struct totem_config *totem_config,
  629. const char **error_string)
  630. {
  631. int fd;
  632. int res;
  633. ssize_t expected_key_len = sizeof (totem_config->private_key);
  634. int saved_errno;
  635. char error_str[100];
  636. const char *error_ptr;
  637. fd = open (key_location, O_RDONLY);
  638. if (fd == -1) {
  639. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  640. snprintf (error_string_response, sizeof(error_string_response),
  641. "Could not open %s: %s\n",
  642. key_location, error_ptr);
  643. goto parse_error;
  644. }
  645. res = read (fd, totem_config->private_key, expected_key_len);
  646. saved_errno = errno;
  647. close (fd);
  648. if (res == -1) {
  649. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  650. snprintf (error_string_response, sizeof(error_string_response),
  651. "Could not read %s: %s\n",
  652. key_location, error_ptr);
  653. goto parse_error;
  654. }
  655. totem_config->private_key_len = expected_key_len;
  656. if (res != expected_key_len) {
  657. snprintf (error_string_response, sizeof(error_string_response),
  658. "Could only read %d bits of 1024 bits from %s.\n",
  659. res * 8, key_location);
  660. goto parse_error;
  661. }
  662. return 0;
  663. parse_error:
  664. *error_string = error_string_response;
  665. return (-1);
  666. }
  667. int totem_config_keyread (
  668. struct objdb_iface_ver0 *objdb,
  669. struct totem_config *totem_config,
  670. const char **error_string)
  671. {
  672. int got_key = 0;
  673. const char *key_location = NULL;
  674. hdb_handle_t object_totem_handle;
  675. int res;
  676. memset (totem_config->private_key, 0, 128);
  677. totem_config->private_key_len = 128;
  678. if (totem_config->secauth == 0) {
  679. return (0);
  680. }
  681. res = totem_handle_find (objdb, &object_totem_handle);
  682. if (res == -1) {
  683. return (-1);
  684. }
  685. /* objdb may store the location of the key file */
  686. if (!objdb_get_string (objdb,object_totem_handle, "keyfile", &key_location)
  687. && key_location) {
  688. res = read_keyfile(key_location, totem_config, error_string);
  689. if (res) {
  690. goto key_error;
  691. }
  692. got_key = 1;
  693. } else { /* Or the key itself may be in the objdb */
  694. char *key = NULL;
  695. size_t key_len;
  696. res = objdb->object_key_get (object_totem_handle,
  697. "key",
  698. strlen ("key"),
  699. (void *)&key,
  700. &key_len);
  701. if (res == 0 && key) {
  702. if (key_len > sizeof (totem_config->private_key)) {
  703. goto key_error;
  704. }
  705. memcpy(totem_config->private_key, key, key_len);
  706. totem_config->private_key_len = key_len;
  707. got_key = 1;
  708. }
  709. }
  710. /* In desperation we read the default filename */
  711. if (!got_key) {
  712. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  713. if (!filename)
  714. filename = COROSYSCONFDIR "/authkey";
  715. res = read_keyfile(filename, totem_config, error_string);
  716. if (res)
  717. goto key_error;
  718. }
  719. return (0);
  720. key_error:
  721. *error_string = error_string_response;
  722. return (-1);
  723. }
  724. static void totem_key_change_notify(object_change_type_t change_type,
  725. hdb_handle_t parent_object_handle,
  726. hdb_handle_t object_handle,
  727. const void *object_name_pt, size_t object_name_len,
  728. const void *key_name_pt, size_t key_len,
  729. const void *key_value_pt, size_t key_value_len,
  730. void *priv_data_pt)
  731. {
  732. struct totem_config *totem_config = priv_data_pt;
  733. if (memcmp(object_name_pt, "totem", object_name_len) == 0)
  734. totem_volatile_config_read(global_objdb,
  735. totem_config,
  736. object_handle); // CHECK
  737. }
  738. static void totem_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  739. void *priv_data_pt)
  740. {
  741. struct totem_config *totem_config = priv_data_pt;
  742. hdb_handle_t totem_object_handle;
  743. if (totem_config == NULL)
  744. return;
  745. /*
  746. * A new totem {} key might exist, cancel the
  747. * existing notification at the start of reload,
  748. * and start a new one on the new object when
  749. * it's all settled.
  750. */
  751. if (type == OBJDB_RELOAD_NOTIFY_START) {
  752. global_objdb->object_track_stop(
  753. totem_key_change_notify,
  754. NULL,
  755. NULL,
  756. NULL,
  757. totem_config);
  758. }
  759. if (type == OBJDB_RELOAD_NOTIFY_END ||
  760. type == OBJDB_RELOAD_NOTIFY_FAILED) {
  761. if (!totem_handle_find(global_objdb,
  762. &totem_object_handle)) {
  763. global_objdb->object_track_start(totem_object_handle,
  764. 1,
  765. totem_key_change_notify,
  766. NULL, // object_create_notify,
  767. NULL, // object_destroy_notify,
  768. NULL, // object_reload_notify
  769. totem_config); // priv_data
  770. /*
  771. * Reload the configuration
  772. */
  773. totem_volatile_config_read(global_objdb,
  774. totem_config,
  775. totem_object_handle);
  776. }
  777. else {
  778. log_printf(LOGSYS_LEVEL_ERROR, "totem objdb tracking stopped, cannot find totem{} handle on objdb\n");
  779. }
  780. }
  781. }
  782. static void add_totem_config_notification(
  783. struct objdb_iface_ver0 *objdb,
  784. struct totem_config *totem_config,
  785. hdb_handle_t totem_object_handle)
  786. {
  787. global_objdb = objdb;
  788. objdb->object_track_start(totem_object_handle,
  789. 1,
  790. totem_key_change_notify,
  791. NULL, // object_create_notify,
  792. NULL, // object_destroy_notify,
  793. NULL, // object_reload_notify
  794. totem_config); // priv_data
  795. /*
  796. * Reload notify must be on the parent object
  797. */
  798. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  799. 1,
  800. NULL, // key_change_notify,
  801. NULL, // object_create_notify,
  802. NULL, // object_destroy_notify,
  803. totem_objdb_reload_notify, // object_reload_notify
  804. totem_config); // priv_data
  805. }