totemconfig.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <assert.h>
  39. #include <unistd.h>
  40. #include <sys/socket.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <fcntl.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <sys/param.h>
  47. #include "../include/list.h"
  48. #include "util.h"
  49. #include "totem.h"
  50. #include "totemconfig.h"
  51. #include "print.h"
  52. #define LOG_SERVICE LOG_SERVICE_GMI
  53. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  54. #define HZ 100 /* 10ms */
  55. #endif
  56. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  57. #define TOKEN_TIMEOUT 1000
  58. #define TOKEN_RETRANSMIT_TIMEOUT (int)(TOKEN_TIMEOUT / (TOKEN_RETRANSMITS_BEFORE_LOSS_CONST + 0.2))
  59. #define TOKEN_HOLD_TIMEOUT (int)(TOKEN_RETRANSMIT_TIMEOUT * 0.8 - (1000/(int)HZ))
  60. #define JOIN_TIMEOUT 100
  61. #define CONSENSUS_TIMEOUT 200
  62. #define MERGE_TIMEOUT 200
  63. #define DOWNCHECK_TIMEOUT 1000
  64. #define FAIL_TO_RECV_CONST 50
  65. #define SEQNO_UNCHANGED_CONST 30
  66. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  67. #define MAX_NETWORK_DELAY 50 /*In milliseconds*/
  68. static char error_string_response[512];
  69. typedef enum {
  70. MAIN_HEAD,
  71. MAIN_TOTEM,
  72. } main_parse_t;
  73. static inline char *
  74. strstr_rs (const char *haystack, const char *needle)
  75. {
  76. char *end_address;
  77. char *new_needle;
  78. char *token = (char *)(needle + strlen (needle) - 1); /* last char is always the token */
  79. new_needle = (char *)strdup (needle);
  80. new_needle[strlen(new_needle) - 1] = '\0'; /* remove token */
  81. end_address = strstr (haystack, new_needle);
  82. if (end_address == 0) {
  83. goto not_found;
  84. }
  85. end_address += strlen (new_needle);
  86. /*
  87. * Parse all tabs and spaces until token is found
  88. * if other character found besides token, its not a match
  89. */
  90. do {
  91. if (*end_address == '\t' || *end_address == ' ') {
  92. end_address++;
  93. } else {
  94. break;
  95. }
  96. } while (*end_address != '\0' && *end_address != token[0]);
  97. if (*end_address != token[0]) {
  98. end_address = 0;
  99. goto not_found;
  100. }
  101. end_address++; /* skip past token */
  102. do {
  103. if (*end_address == '\t' || *end_address == ' ') {
  104. end_address++;
  105. } else {
  106. break;
  107. }
  108. } while (*end_address != '\0');
  109. not_found:
  110. free (new_needle);
  111. return (end_address);
  112. }
  113. extern int totem_config_read (
  114. struct totem_config *totem_config,
  115. char **error_string,
  116. int interface_max)
  117. {
  118. FILE *fp;
  119. int res = 0;
  120. int line_number = 0;
  121. main_parse_t parse = MAIN_HEAD;
  122. int totem_parsed = 0;
  123. char *loc;
  124. int i;
  125. int parse_done = 0;
  126. char line[512];
  127. char *error_reason = error_string_response;
  128. memset (totem_config, 0, sizeof (struct totem_config));
  129. totem_config->interfaces = malloc (sizeof (struct totem_interface) * interface_max);
  130. if (totem_config->interfaces == 0) {
  131. parse_done = 1;
  132. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  133. return -1;
  134. }
  135. memset (totem_config->interfaces, 0,
  136. sizeof (struct totem_interface) * interface_max);
  137. totem_config->secauth = 1;
  138. fp = fopen (OPENAIS_CONFDIR "/openais.conf", "r");
  139. if (fp == 0) {
  140. parse_done = 1;
  141. sprintf (error_reason, "Can't read file %s reason = (%s)\n",
  142. OPENAIS_CONFDIR, strerror (errno));
  143. *error_string = error_reason;
  144. return -1;
  145. }
  146. while (fgets (line, 255, fp)) {
  147. line_number += 1;
  148. line[strlen(line) - 1] = '\0';
  149. /*
  150. * Clear out white space and tabs
  151. */
  152. for (i = strlen (line) - 1; i > -1; i--) {
  153. if (line[i] == '\t' || line[i] == ' ') {
  154. line[i] = '\0';
  155. } else {
  156. break;
  157. }
  158. }
  159. /*
  160. * Clear out comments and empty lines
  161. */
  162. if (line[0] == '#' || line[0] == '\0') {
  163. continue;
  164. }
  165. line_number += 1;
  166. switch (parse) {
  167. case MAIN_HEAD:
  168. if (totem_parsed == 0 && strstr_rs (line, "network{")) {
  169. totem_parsed = 1;
  170. parse = MAIN_TOTEM;
  171. } else
  172. if (totem_parsed == 0 && strstr_rs (line, "totem{")) {
  173. totem_parsed = 1;
  174. parse = MAIN_TOTEM;
  175. } else {
  176. continue;
  177. }
  178. break;
  179. case MAIN_TOTEM:
  180. if ((loc = strstr_rs (line, "version:"))) {
  181. if (strcmp (loc, "1") == 0) {
  182. totem_config->version = 1;
  183. }
  184. } else
  185. if ((loc = strstr_rs (line, "secauth:"))) {
  186. if (strcmp (loc, "on") == 0) {
  187. totem_config->secauth = 1;
  188. } else
  189. if (strcmp (loc, "off") == 0) {
  190. totem_config->secauth = 0;
  191. }
  192. } else
  193. if ((loc = strstr_rs (line, "threads:"))) {
  194. totem_config->threads = atoi (loc);
  195. } else
  196. if ((loc = strstr_rs (line, "nodeid:"))) {
  197. res = totem_config->node_id = atoi (loc);
  198. } else
  199. if ((loc = strstr_rs (line, "netmtu:"))) {
  200. totem_config->net_mtu = atoi (loc);
  201. } else
  202. if ((loc = strstr_rs (line, "mcastaddr:"))) {
  203. res = totemip_parse (&totem_config->mcast_addr, loc);
  204. } else
  205. if ((loc = strstr_rs (line, "mcastport:"))) {
  206. res = totem_config->ip_port = htons (atoi (loc));
  207. } else
  208. if ((loc = strstr_rs (line, "bindnetaddr:"))) {
  209. if (interface_max == totem_config->interface_count) {
  210. sprintf (error_reason,
  211. "%d is too many interfaces in %s/network.conf",
  212. totem_config->interface_count, OPENAIS_CONFDIR);
  213. goto parse_error;
  214. }
  215. res = totemip_parse (&totem_config->interfaces[totem_config->interface_count].bindnet, loc);
  216. totem_config->interface_count += 1;
  217. } else
  218. if ((loc = strstr_rs (line, "token:"))) {
  219. totem_config->token_timeout = atoi(loc);
  220. } else if ((loc = strstr_rs (line, "token_retransmit:"))) {
  221. totem_config->token_retransmit_timeout = atoi(loc);
  222. } else if ((loc = strstr_rs (line, "hold:"))) {
  223. totem_config->token_hold_timeout = atoi(loc);
  224. } else if ((loc = strstr_rs (line, "token_retransmits_before_loss_const:"))) {
  225. totem_config->token_retransmits_before_loss_const = atoi(loc);
  226. } else if ((loc = strstr_rs (line, "join:"))) {
  227. totem_config->join_timeout = atoi(loc);
  228. } else if ((loc = strstr_rs (line, "consensus:"))) {
  229. totem_config->consensus_timeout = atoi(loc);
  230. } else if ((loc = strstr_rs (line, "merge:"))) {
  231. totem_config->merge_timeout = atoi(loc);
  232. } else if ((loc = strstr_rs (line, "downcheck:"))) {
  233. totem_config->downcheck_timeout = atoi(loc);
  234. } else if ((loc = strstr_rs (line, "fail_recv_const:"))) {
  235. totem_config->fail_to_recv_const = atoi(loc);
  236. } else if ((loc = strstr_rs (line, "seqno_unchanged_const:"))) {
  237. totem_config->seqno_unchanged_const = atoi(loc);
  238. } else if ((loc = strstr_rs (line, "heartbeat_failures_allowed:"))) {
  239. totem_config->heartbeat_failures_allowed = atoi(loc);
  240. } else if ((loc = strstr_rs (line, "max_network_delay:"))) {
  241. totem_config->max_network_delay = atoi(loc);
  242. } else if ((loc = strstr_rs (line, "}"))) {
  243. parse = MAIN_HEAD;
  244. } else {
  245. goto parse_error;
  246. }
  247. break;
  248. default:
  249. assert (0 == 1); /* SHOULDN'T HAPPEN */
  250. break;
  251. }
  252. }
  253. if (parse == MAIN_HEAD) {
  254. fclose (fp);
  255. return (0);
  256. } else {
  257. error_reason = "Missing closing brace";
  258. goto parse_error;
  259. }
  260. parse_error:
  261. if (parse_done) {
  262. sprintf (error_string_response,
  263. "parse error in %s/openais.conf: %s.\n",
  264. OPENAIS_CONFDIR, error_reason);
  265. } else {
  266. sprintf (error_string_response,
  267. "parse error in %s/openais.conf: %s (line %d).\n",
  268. OPENAIS_CONFDIR, error_reason, line_number);
  269. }
  270. *error_string = error_string_response;
  271. fclose (fp);
  272. return (-1);
  273. }
  274. int totem_config_validate (
  275. struct totem_config *totem_config,
  276. char **error_string)
  277. {
  278. static char local_error_reason[512];
  279. char *error_reason = local_error_reason;
  280. /*
  281. * Some error checking of parsed data to make sure its valid
  282. */
  283. if ((int *)&totem_config->mcast_addr.addr == 0) {
  284. error_reason = "No multicast address specified";
  285. goto parse_error;
  286. }
  287. if (totem_config->ip_port == 0) {
  288. error_reason = "No multicast port specified";
  289. goto parse_error;
  290. }
  291. if (totem_config->mcast_addr.family == AF_INET6 &&
  292. totem_config->node_id == 0) {
  293. error_reason = "An IPV6 network requires that a node ID be specified.";
  294. goto parse_error;
  295. }
  296. if (totem_config->interface_count == 0) {
  297. error_reason = "No bindnet specified";
  298. goto parse_error;
  299. }
  300. if (totem_config->version != 1) {
  301. error_reason = "This totem parser can only parse version 1 configuration files.";
  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->token_timeout < MINIMUM_TIMEOUT) {
  336. sprintf (local_error_reason, "The token timeout parameter (%d ms) may not be less then (%d ms).",
  337. totem_config->token_timeout, MINIMUM_TIMEOUT);
  338. goto parse_error;
  339. }
  340. if (totem_config->token_retransmit_timeout == 0) {
  341. totem_config->token_retransmit_timeout =
  342. (int)(totem_config->token_timeout /
  343. (totem_config->token_retransmits_before_loss_const + 0.2));
  344. }
  345. if (totem_config->token_hold_timeout == 0) {
  346. totem_config->token_hold_timeout =
  347. (int)(totem_config->token_retransmit_timeout * 0.8 -
  348. (1000/HZ));
  349. }
  350. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  351. sprintf (local_error_reason, "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  352. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  353. goto parse_error;
  354. }
  355. if (totem_config->token_hold_timeout == 0) {
  356. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  357. }
  358. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  359. sprintf (local_error_reason, "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  360. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  361. goto parse_error;
  362. }
  363. if (totem_config->join_timeout == 0) {
  364. totem_config->join_timeout = JOIN_TIMEOUT;
  365. }
  366. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  367. sprintf (local_error_reason, "The join timeout parameter (%d ms) may not be less then (%d ms).",
  368. totem_config->join_timeout, MINIMUM_TIMEOUT);
  369. goto parse_error;
  370. }
  371. if (totem_config->consensus_timeout == 0) {
  372. totem_config->consensus_timeout = CONSENSUS_TIMEOUT;
  373. }
  374. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  375. sprintf (local_error_reason, "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  376. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  377. goto parse_error;
  378. }
  379. if (totem_config->merge_timeout == 0) {
  380. totem_config->merge_timeout = MERGE_TIMEOUT;
  381. }
  382. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  383. sprintf (local_error_reason, "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  384. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  385. goto parse_error;
  386. }
  387. if (totem_config->downcheck_timeout == 0) {
  388. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  389. }
  390. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  391. sprintf (local_error_reason, "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  392. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  393. goto parse_error;
  394. }
  395. if (totem_config->fail_to_recv_const == 0) {
  396. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  397. }
  398. if (totem_config->seqno_unchanged_const == 0) {
  399. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  400. }
  401. if (totem_config->net_mtu == 0) {
  402. totem_config->net_mtu = 1500;
  403. }
  404. if (totem_config->threads > SEND_THREADS_MAX) {
  405. totem_config->threads = SEND_THREADS_MAX;
  406. }
  407. if (totem_config->secauth == 0) {
  408. totem_config->threads = 0;
  409. }
  410. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  411. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  412. goto parse_error;
  413. }
  414. return (0);
  415. parse_error:
  416. sprintf (error_string_response,
  417. "parse error in %s/openais.conf: %s\n", OPENAIS_CONFDIR, error_reason);
  418. *error_string = error_string_response;
  419. return (-1);
  420. }
  421. int totem_config_keyread (
  422. char *key_location,
  423. struct totem_config *totem_config,
  424. char **error_string)
  425. {
  426. int fd;
  427. int res;
  428. int i;
  429. if (totem_config->secauth == 0) {
  430. return (0);
  431. }
  432. fd = open (key_location, O_RDONLY);
  433. if (fd == -1) {
  434. sprintf (error_string_response, "Could not open %s: %s\n",
  435. key_location, strerror (errno));
  436. goto parse_error;
  437. }
  438. res = read (fd, totem_config->private_key, 128);
  439. if (res == -1) {
  440. close (fd);
  441. sprintf (error_string_response, "Could not read %s: %s\n",
  442. key_location, strerror (errno));
  443. goto parse_error;
  444. }
  445. totem_config->private_key_len = 128;
  446. if (res != 128) {
  447. close (fd);
  448. sprintf (error_string_response, "Could only read %d bits of 1024 bits from %s.\n",
  449. res * 8, key_location);
  450. goto parse_error;
  451. }
  452. if (totem_config->mcast_addr.family != totem_config->interfaces[0].bindnet.family) {
  453. strcpy (error_string_response, "Multicast address family does not match bind address family");
  454. goto parse_error;
  455. }
  456. for (i = 0; i < totem_config->interface_count; i++) {
  457. if (totem_config->mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  458. strcpy (error_string_response, "Not all bind address belong to the same IP family");
  459. goto parse_error;
  460. }
  461. }
  462. return (0);
  463. parse_error:
  464. *error_string = error_string_response;
  465. return (-1);
  466. }