totemconfig.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006 RedHat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.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 "../include/list.h"
  49. #include "util.h"
  50. #include "totem.h"
  51. #include "totemconfig.h"
  52. #include "print.h"
  53. #include "objdb.h"
  54. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  55. #define HZ 100 /* 10ms */
  56. #endif
  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 100
  62. #define CONSENSUS_TIMEOUT 200
  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. static char error_string_response[512];
  72. /* These just makes the code below a little neater */
  73. static inline int objdb_get_string (
  74. struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
  75. char *key, char **value)
  76. {
  77. int res;
  78. *value = NULL;
  79. if ( !(res = objdb->object_key_get (object_service_handle,
  80. key,
  81. strlen (key),
  82. (void *)value,
  83. NULL))) {
  84. if (*value)
  85. return 0;
  86. }
  87. return -1;
  88. }
  89. static inline void objdb_get_int (
  90. struct objdb_iface_ver0 *objdb, unsigned int object_service_handle,
  91. char *key, unsigned int *intvalue)
  92. {
  93. char *value = NULL;
  94. if (!objdb->object_key_get (object_service_handle,
  95. key,
  96. strlen (key),
  97. (void *)&value,
  98. NULL)) {
  99. if (value) {
  100. *intvalue = atoi(value);
  101. }
  102. }
  103. }
  104. extern int totem_config_read (
  105. struct objdb_iface_ver0 *objdb,
  106. struct totem_config *totem_config,
  107. char **error_string,
  108. int interface_max)
  109. {
  110. int res = 0;
  111. int parse_done = 0;
  112. unsigned int object_totem_handle;
  113. unsigned int object_interface_handle;
  114. char *str;
  115. char *error_reason = error_string_response;
  116. unsigned int ringnumber = 0;
  117. memset (totem_config, 0, sizeof (struct totem_config));
  118. totem_config->interfaces = malloc (sizeof (struct totem_interface) * interface_max);
  119. if (totem_config->interfaces == 0) {
  120. parse_done = 1;
  121. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  122. return -1;
  123. }
  124. memset (totem_config->interfaces, 0,
  125. sizeof (struct totem_interface) * interface_max);
  126. totem_config->secauth = 1;
  127. objdb->object_find_reset (OBJECT_PARENT_HANDLE);
  128. if (objdb->object_find (
  129. OBJECT_PARENT_HANDLE,
  130. "network",
  131. strlen ("network"),
  132. &object_totem_handle) == 0 ||
  133. objdb->object_find (
  134. OBJECT_PARENT_HANDLE,
  135. "totem",
  136. strlen ("totem"),
  137. &object_totem_handle) == 0) {
  138. if (!objdb_get_string (objdb,object_totem_handle, "version", &str)) {
  139. if (strcmp (str, "2") == 0) {
  140. totem_config->version = 2;
  141. }
  142. }
  143. if (!objdb_get_string (objdb,object_totem_handle, "secauth", &str)) {
  144. if (strcmp (str, "on") == 0) {
  145. totem_config->secauth = 1;
  146. }
  147. if (strcmp (str, "off") == 0) {
  148. totem_config->secauth = 0;
  149. }
  150. }
  151. /*
  152. * Get interface node id
  153. */
  154. objdb_get_int (objdb, object_totem_handle, "nodeid", &totem_config->node_id);
  155. objdb_get_int (objdb,object_totem_handle, "threads", &totem_config->threads);
  156. objdb_get_int (objdb,object_totem_handle, "netmtu", &totem_config->net_mtu);
  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, "consensus", &totem_config->consensus_timeout);
  163. objdb_get_int (objdb,object_totem_handle, "merge", &totem_config->merge_timeout);
  164. objdb_get_int (objdb,object_totem_handle, "downcheck", &totem_config->downcheck_timeout);
  165. objdb_get_int (objdb,object_totem_handle, "fail_recv_const", &totem_config->fail_to_recv_const);
  166. objdb_get_int (objdb,object_totem_handle, "seqno_unchanged_const", &totem_config->seqno_unchanged_const);
  167. objdb_get_int (objdb,object_totem_handle, "heartbeat_failures_allowed", &totem_config->heartbeat_failures_allowed);
  168. objdb_get_int (objdb,object_totem_handle, "max_network_delay", &totem_config->max_network_delay);
  169. objdb_get_int (objdb,object_totem_handle, "window_size", &totem_config->window_size);
  170. objdb_get_string (objdb, object_totem_handle, "vsftype", &totem_config->vsf_type);
  171. objdb_get_int (objdb,object_totem_handle, "max_messages", &totem_config->max_messages);
  172. }
  173. while (objdb->object_find (
  174. object_totem_handle,
  175. "interface",
  176. strlen ("interface"),
  177. &object_interface_handle) == 0) {
  178. if (interface_max == totem_config->interface_count) {
  179. sprintf (error_reason,
  180. "%d is too many interfaces in %s/network.conf",
  181. totem_config->interface_count, OPENAIS_CONFDIR);
  182. goto parse_error;
  183. }
  184. objdb_get_int (objdb, object_interface_handle, "ringnumber", &ringnumber);
  185. /*
  186. * Get interface multicast address
  187. */
  188. if (!objdb_get_string (objdb, object_interface_handle, "mcastaddr", &str)) {
  189. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str);
  190. }
  191. /*
  192. * Get mcast port
  193. */
  194. if (!objdb_get_string (objdb, object_interface_handle, "mcastport", &str)) {
  195. totem_config->interfaces[ringnumber].ip_port = htons (atoi (str));
  196. }
  197. /*
  198. * Get the bind net address
  199. */
  200. if (!objdb_get_string (objdb, object_interface_handle, "bindnetaddr", &str)) {
  201. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str);
  202. }
  203. totem_config->interface_count++;
  204. }
  205. return 0;
  206. parse_error:
  207. sprintf (error_string_response,
  208. "parse error in config %s.\n",
  209. error_reason);
  210. *error_string = error_string_response;
  211. return (-1);
  212. }
  213. int totem_config_validate (
  214. struct totem_config *totem_config,
  215. char **error_string)
  216. {
  217. static char local_error_reason[512];
  218. char *error_reason = local_error_reason;
  219. int i;
  220. if (totem_config->interface_count == 0) {
  221. error_reason = "No interfaces defined";
  222. goto parse_error;
  223. }
  224. for (i = 0; i < totem_config->interface_count; i++) {
  225. /*
  226. * Some error checking of parsed data to make sure its valid
  227. */
  228. if ((int *)&totem_config->interfaces[i].mcast_addr.addr == 0) {
  229. error_reason = "No multicast address specified";
  230. goto parse_error;
  231. }
  232. if (totem_config->interfaces[i].ip_port == 0) {
  233. error_reason = "No multicast port specified";
  234. goto parse_error;
  235. }
  236. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  237. totem_config->node_id == 0) {
  238. error_reason = "An IPV6 network requires that a node ID be specified.";
  239. goto parse_error;
  240. }
  241. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  242. error_reason = "Multicast address family does not match bind address family";
  243. goto parse_error;
  244. }
  245. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  246. error_reason = "Not all bind address belong to the same IP family";
  247. goto parse_error;
  248. }
  249. }
  250. if (totem_config->version != 2) {
  251. error_reason = "This totem parser can only parse version 2 configurations.";
  252. goto parse_error;
  253. }
  254. if (totem_config->token_retransmits_before_loss_const == 0) {
  255. totem_config->token_retransmits_before_loss_const =
  256. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  257. }
  258. /*
  259. * Setup timeout values that are not setup by user
  260. */
  261. if (totem_config->token_timeout == 0) {
  262. totem_config->token_timeout = TOKEN_TIMEOUT;
  263. if (totem_config->token_retransmits_before_loss_const == 0) {
  264. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  265. }
  266. if (totem_config->token_retransmit_timeout == 0) {
  267. totem_config->token_retransmit_timeout =
  268. (int)(totem_config->token_timeout /
  269. (totem_config->token_retransmits_before_loss_const + 0.2));
  270. }
  271. if (totem_config->token_hold_timeout == 0) {
  272. totem_config->token_hold_timeout =
  273. (int)(totem_config->token_retransmit_timeout * 0.8 -
  274. (1000/HZ));
  275. }
  276. }
  277. if (totem_config->max_network_delay == 0) {
  278. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  279. }
  280. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  281. sprintf (local_error_reason, "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  282. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  283. goto parse_error;
  284. }
  285. if (totem_config->window_size == 0) {
  286. totem_config->window_size = WINDOW_SIZE;
  287. }
  288. if (totem_config->max_messages == 0) {
  289. totem_config->max_messages = MAX_MESSAGES;
  290. }
  291. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  292. sprintf (local_error_reason, "The token timeout parameter (%d ms) may not be less then (%d ms).",
  293. totem_config->token_timeout, MINIMUM_TIMEOUT);
  294. goto parse_error;
  295. }
  296. if (totem_config->token_retransmit_timeout == 0) {
  297. totem_config->token_retransmit_timeout =
  298. (int)(totem_config->token_timeout /
  299. (totem_config->token_retransmits_before_loss_const + 0.2));
  300. }
  301. if (totem_config->token_hold_timeout == 0) {
  302. totem_config->token_hold_timeout =
  303. (int)(totem_config->token_retransmit_timeout * 0.8 -
  304. (1000/HZ));
  305. }
  306. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  307. sprintf (local_error_reason, "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  308. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  309. goto parse_error;
  310. }
  311. if (totem_config->token_hold_timeout == 0) {
  312. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  313. }
  314. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  315. sprintf (local_error_reason, "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  316. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  317. goto parse_error;
  318. }
  319. if (totem_config->join_timeout == 0) {
  320. totem_config->join_timeout = JOIN_TIMEOUT;
  321. }
  322. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  323. sprintf (local_error_reason, "The join timeout parameter (%d ms) may not be less then (%d ms).",
  324. totem_config->join_timeout, MINIMUM_TIMEOUT);
  325. goto parse_error;
  326. }
  327. if (totem_config->consensus_timeout == 0) {
  328. totem_config->consensus_timeout = CONSENSUS_TIMEOUT;
  329. }
  330. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  331. sprintf (local_error_reason, "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  332. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  333. goto parse_error;
  334. }
  335. if (totem_config->merge_timeout == 0) {
  336. totem_config->merge_timeout = MERGE_TIMEOUT;
  337. }
  338. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  339. sprintf (local_error_reason, "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  340. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  341. goto parse_error;
  342. }
  343. if (totem_config->downcheck_timeout == 0) {
  344. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  345. }
  346. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  347. sprintf (local_error_reason, "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  348. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  349. goto parse_error;
  350. }
  351. if (totem_config->fail_to_recv_const == 0) {
  352. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  353. }
  354. if (totem_config->seqno_unchanged_const == 0) {
  355. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  356. }
  357. if (totem_config->net_mtu == 0) {
  358. totem_config->net_mtu = 1500;
  359. }
  360. if ((256000 / totem_config->net_mtu) < totem_config->max_messages) {
  361. sprintf (local_error_reason, "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  362. totem_config->max_messages, 256000/totem_config->net_mtu);
  363. goto parse_error;
  364. }
  365. if (totem_config->threads > SEND_THREADS_MAX) {
  366. totem_config->threads = SEND_THREADS_MAX;
  367. }
  368. if (totem_config->secauth == 0) {
  369. totem_config->threads = 0;
  370. }
  371. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  372. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  373. goto parse_error;
  374. }
  375. if (totem_config->vsf_type == NULL) {
  376. totem_config->vsf_type = "ykd";
  377. }
  378. return (0);
  379. parse_error:
  380. sprintf (error_string_response,
  381. "parse error in config: %s\n", error_reason);
  382. *error_string = error_string_response;
  383. return (-1);
  384. }
  385. static int read_keyfile (
  386. char *key_location,
  387. struct totem_config *totem_config,
  388. char **error_string)
  389. {
  390. int fd;
  391. int res;
  392. fd = open (key_location, O_RDONLY);
  393. if (fd == -1) {
  394. sprintf (error_string_response, "Could not open %s: %s\n",
  395. key_location, strerror (errno));
  396. goto parse_error;
  397. }
  398. res = read (fd, totem_config->private_key, 128);
  399. if (res == -1) {
  400. close (fd);
  401. sprintf (error_string_response, "Could not read %s: %s\n",
  402. key_location, strerror (errno));
  403. goto parse_error;
  404. }
  405. totem_config->private_key_len = 128;
  406. if (res != 128) {
  407. close (fd);
  408. sprintf (error_string_response, "Could only read %d bits of 1024 bits from %s.\n",
  409. res * 8, key_location);
  410. goto parse_error;
  411. }
  412. return 0;
  413. parse_error:
  414. *error_string = error_string_response;
  415. return (-1);
  416. }
  417. int totem_config_keyread (
  418. struct objdb_iface_ver0 *objdb,
  419. struct totem_config *totem_config,
  420. char **error_string)
  421. {
  422. int got_key = 0;
  423. char *key_location = NULL;
  424. unsigned int object_service_handle;
  425. int res;
  426. memset (totem_config->private_key, 0, 128);
  427. totem_config->private_key_len = 128;
  428. if (totem_config->secauth == 0) {
  429. return (0);
  430. }
  431. if (objdb->object_find (
  432. OBJECT_PARENT_HANDLE,
  433. "network",
  434. strlen ("network"),
  435. &object_service_handle) == 0 ||
  436. objdb->object_find (
  437. OBJECT_PARENT_HANDLE,
  438. "totem",
  439. strlen ("totem"),
  440. &object_service_handle) == 0) {
  441. /* objdb may store the location of the key file */
  442. if (!objdb_get_string (objdb,object_service_handle, "keyfile", &key_location)
  443. && key_location) {
  444. res = read_keyfile(key_location, totem_config, error_string);
  445. if (res)
  446. goto key_error;
  447. got_key = 1;
  448. }
  449. else { /* Or the key itself may be in the objdb */
  450. char *key = NULL;
  451. int key_len;
  452. res = objdb->object_key_get (object_service_handle,
  453. "key",
  454. strlen ("key"),
  455. (void *)&key,
  456. &key_len);
  457. if (res == 0 && key) {
  458. memcpy(totem_config->private_key, key, key_len);
  459. totem_config->private_key_len = key_len;
  460. got_key = 1;
  461. }
  462. }
  463. }
  464. /* In desperation we read the default filename */
  465. if (!got_key) {
  466. res = read_keyfile(OPENAIS_CONFDIR "/authkey", totem_config, error_string);
  467. if (res)
  468. goto key_error;
  469. }
  470. return (0);
  471. *error_string = error_string_response;
  472. key_error:
  473. return (-1);
  474. }