totemconfig.c 23 KB

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