totemconfig.c 16 KB

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