totemconfig.c 22 KB

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