totemconfig.c 16 KB

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