totemconfig.c 27 KB

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