totemconfig.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 ("/etc/ais/openais.conf", "r");
  139. if (fp == 0) {
  140. parse_done = 1;
  141. sprintf (error_reason, "Can't read file reason = (%s)\n", strerror (errno));
  142. *error_string = error_reason;
  143. return -1;
  144. }
  145. while (fgets (line, 255, fp)) {
  146. line_number += 1;
  147. line[strlen(line) - 1] = '\0';
  148. /*
  149. * Clear out white space and tabs
  150. */
  151. for (i = strlen (line) - 1; i > -1; i--) {
  152. if (line[i] == '\t' || line[i] == ' ') {
  153. line[i] = '\0';
  154. } else {
  155. break;
  156. }
  157. }
  158. /*
  159. * Clear out comments and empty lines
  160. */
  161. if (line[0] == '#' || line[0] == '\0') {
  162. continue;
  163. }
  164. line_number += 1;
  165. switch (parse) {
  166. case MAIN_HEAD:
  167. if (totem_parsed == 0 && strstr_rs (line, "network{")) {
  168. totem_parsed = 1;
  169. parse = MAIN_TOTEM;
  170. } else
  171. if (totem_parsed == 0 && strstr_rs (line, "totem{")) {
  172. totem_parsed = 1;
  173. parse = MAIN_TOTEM;
  174. } else {
  175. continue;
  176. }
  177. break;
  178. case MAIN_TOTEM:
  179. if ((loc = strstr_rs (line, "version:"))) {
  180. if (strcmp (loc, "1") == 0) {
  181. totem_config->version = 1;
  182. }
  183. } else
  184. if ((loc = strstr_rs (line, "secauth:"))) {
  185. if (strcmp (loc, "on") == 0) {
  186. totem_config->secauth = 1;
  187. } else
  188. if (strcmp (loc, "off") == 0) {
  189. totem_config->secauth = 0;
  190. }
  191. } else
  192. if ((loc = strstr_rs (line, "threads:"))) {
  193. totem_config->threads = atoi (loc);
  194. } else
  195. if ((loc = strstr_rs (line, "nodeid:"))) {
  196. res = totem_config->node_id = atoi (loc);
  197. } else
  198. if ((loc = strstr_rs (line, "netmtu:"))) {
  199. totem_config->net_mtu = atoi (loc);
  200. } else
  201. if ((loc = strstr_rs (line, "mcastaddr:"))) {
  202. res = totemip_parse (&totem_config->mcast_addr, loc);
  203. } else
  204. if ((loc = strstr_rs (line, "mcastport:"))) {
  205. res = totem_config->ip_port = htons (atoi (loc));
  206. } else
  207. if ((loc = strstr_rs (line, "bindnetaddr:"))) {
  208. if (interface_max == totem_config->interface_count) {
  209. sprintf (error_reason,
  210. "%d is too many interfaces in /etc/ais/network.conf",
  211. totem_config->interface_count);
  212. goto parse_error;
  213. }
  214. res = totemip_parse (&totem_config->interfaces[totem_config->interface_count].bindnet, loc);
  215. totem_config->interface_count += 1;
  216. } else
  217. if ((loc = strstr_rs (line, "token:"))) {
  218. totem_config->token_timeout = atoi(loc);
  219. } else if ((loc = strstr_rs (line, "token_retransmit:"))) {
  220. totem_config->token_retransmit_timeout = atoi(loc);
  221. } else if ((loc = strstr_rs (line, "hold:"))) {
  222. totem_config->token_hold_timeout = atoi(loc);
  223. } else if ((loc = strstr_rs (line, "token_retransmits_before_loss_const:"))) {
  224. totem_config->token_retransmits_before_loss_const = atoi(loc);
  225. } else if ((loc = strstr_rs (line, "join:"))) {
  226. totem_config->join_timeout = atoi(loc);
  227. } else if ((loc = strstr_rs (line, "consensus:"))) {
  228. totem_config->consensus_timeout = atoi(loc);
  229. } else if ((loc = strstr_rs (line, "merge:"))) {
  230. totem_config->merge_timeout = atoi(loc);
  231. } else if ((loc = strstr_rs (line, "downcheck:"))) {
  232. totem_config->downcheck_timeout = atoi(loc);
  233. } else if ((loc = strstr_rs (line, "fail_recv_const:"))) {
  234. totem_config->fail_to_recv_const = atoi(loc);
  235. } else if ((loc = strstr_rs (line, "seqno_unchanged_const:"))) {
  236. totem_config->seqno_unchanged_const = atoi(loc);
  237. } else if ((loc = strstr_rs (line, "heartbeat_failures_allowed:"))) {
  238. totem_config->heartbeat_failures_allowed = atoi(loc);
  239. } else if ((loc = strstr_rs (line, "max_network_delay:"))) {
  240. totem_config->max_network_delay = atoi(loc);
  241. } else if ((loc = strstr_rs (line, "}"))) {
  242. parse = MAIN_HEAD;
  243. } else {
  244. goto parse_error;
  245. }
  246. break;
  247. default:
  248. assert (0 == 1); /* SHOULDN'T HAPPEN */
  249. break;
  250. }
  251. }
  252. if (parse == MAIN_HEAD) {
  253. fclose (fp);
  254. return (0);
  255. } else {
  256. error_reason = "Missing closing brace";
  257. goto parse_error;
  258. }
  259. parse_error:
  260. if (parse_done) {
  261. sprintf (error_string_response,
  262. "parse error in /etc/ais/openais.conf: %s.\n", error_reason);
  263. } else {
  264. sprintf (error_string_response,
  265. "parse error in /etc/ais/openais.conf: %s (line %d).\n",
  266. error_reason, line_number);
  267. }
  268. *error_string = error_string_response;
  269. fclose (fp);
  270. return (-1);
  271. }
  272. int totem_config_validate (
  273. struct totem_config *totem_config,
  274. char **error_string)
  275. {
  276. static char local_error_reason[512];
  277. char *error_reason = local_error_reason;
  278. /*
  279. * Some error checking of parsed data to make sure its valid
  280. */
  281. if ((int *)&totem_config->mcast_addr.addr == 0) {
  282. error_reason = "No multicast address specified";
  283. goto parse_error;
  284. }
  285. if (totem_config->ip_port == 0) {
  286. error_reason = "No multicast port specified";
  287. goto parse_error;
  288. }
  289. if (totem_config->mcast_addr.family == AF_INET6 &&
  290. totem_config->node_id == 0) {
  291. error_reason = "An IPV6 network requires that a node ID be specified.";
  292. goto parse_error;
  293. }
  294. if (totem_config->interface_count == 0) {
  295. error_reason = "No bindnet specified";
  296. goto parse_error;
  297. }
  298. if (totem_config->version != 1) {
  299. error_reason = "This totem parser can only parse version 1 configuration files.";
  300. goto parse_error;
  301. }
  302. if (totem_config->token_retransmits_before_loss_const == 0) {
  303. totem_config->token_retransmits_before_loss_const =
  304. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  305. }
  306. /*
  307. * Setup timeout values that are not setup by user
  308. */
  309. if (totem_config->token_timeout == 0) {
  310. totem_config->token_timeout = TOKEN_TIMEOUT;
  311. if (totem_config->token_retransmits_before_loss_const == 0) {
  312. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  313. }
  314. if (totem_config->token_retransmit_timeout == 0) {
  315. totem_config->token_retransmit_timeout =
  316. (int)(totem_config->token_timeout /
  317. (totem_config->token_retransmits_before_loss_const + 0.2));
  318. }
  319. if (totem_config->token_hold_timeout == 0) {
  320. totem_config->token_hold_timeout =
  321. (int)(totem_config->token_retransmit_timeout * 0.8 -
  322. (1000/HZ));
  323. }
  324. }
  325. if (totem_config->max_network_delay == 0) {
  326. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  327. }
  328. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  329. sprintf (local_error_reason, "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  330. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  331. goto parse_error;
  332. }
  333. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  334. sprintf (local_error_reason, "The token timeout parameter (%d ms) may not be less then (%d ms).",
  335. totem_config->token_timeout, MINIMUM_TIMEOUT);
  336. goto parse_error;
  337. }
  338. if (totem_config->token_retransmit_timeout == 0) {
  339. totem_config->token_retransmit_timeout =
  340. (int)(totem_config->token_timeout /
  341. (totem_config->token_retransmits_before_loss_const + 0.2));
  342. }
  343. if (totem_config->token_hold_timeout == 0) {
  344. totem_config->token_hold_timeout =
  345. (int)(totem_config->token_retransmit_timeout * 0.8 -
  346. (1000/HZ));
  347. }
  348. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  349. sprintf (local_error_reason, "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  350. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  351. goto parse_error;
  352. }
  353. if (totem_config->token_hold_timeout == 0) {
  354. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  355. }
  356. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  357. sprintf (local_error_reason, "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  358. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  359. goto parse_error;
  360. }
  361. if (totem_config->join_timeout == 0) {
  362. totem_config->join_timeout = JOIN_TIMEOUT;
  363. }
  364. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  365. sprintf (local_error_reason, "The join timeout parameter (%d ms) may not be less then (%d ms).",
  366. totem_config->join_timeout, MINIMUM_TIMEOUT);
  367. goto parse_error;
  368. }
  369. if (totem_config->consensus_timeout == 0) {
  370. totem_config->consensus_timeout = CONSENSUS_TIMEOUT;
  371. }
  372. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  373. sprintf (local_error_reason, "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  374. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  375. goto parse_error;
  376. }
  377. if (totem_config->merge_timeout == 0) {
  378. totem_config->merge_timeout = MERGE_TIMEOUT;
  379. }
  380. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  381. sprintf (local_error_reason, "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  382. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  383. goto parse_error;
  384. }
  385. if (totem_config->downcheck_timeout == 0) {
  386. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  387. }
  388. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  389. sprintf (local_error_reason, "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  390. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  391. goto parse_error;
  392. }
  393. if (totem_config->fail_to_recv_const == 0) {
  394. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  395. }
  396. if (totem_config->seqno_unchanged_const == 0) {
  397. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  398. }
  399. if (totem_config->net_mtu == 0) {
  400. totem_config->net_mtu = 1500;
  401. }
  402. if (totem_config->threads > SEND_THREADS_MAX) {
  403. totem_config->threads = SEND_THREADS_MAX;
  404. }
  405. if (totem_config->secauth == 0) {
  406. totem_config->threads = 0;
  407. }
  408. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  409. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  410. goto parse_error;
  411. }
  412. return (0);
  413. parse_error:
  414. sprintf (error_string_response,
  415. "parse error in /etc/ais/openais.conf: %s\n", error_reason);
  416. *error_string = error_string_response;
  417. return (-1);
  418. }
  419. int totem_config_keyread (
  420. char *key_location,
  421. struct totem_config *totem_config,
  422. char **error_string)
  423. {
  424. int fd;
  425. int res;
  426. int i;
  427. if (totem_config->secauth == 0) {
  428. return (0);
  429. }
  430. fd = open (key_location, O_RDONLY);
  431. if (fd == -1) {
  432. sprintf (error_string_response, "Could not open %s: %s\n",
  433. key_location, strerror (errno));
  434. goto parse_error;
  435. }
  436. res = read (fd, totem_config->private_key, 128);
  437. if (res == -1) {
  438. close (fd);
  439. sprintf (error_string_response, "Could not read %s: %s\n",
  440. key_location, strerror (errno));
  441. goto parse_error;
  442. }
  443. totem_config->private_key_len = 128;
  444. if (res != 128) {
  445. close (fd);
  446. sprintf (error_string_response, "Could only read %d bits of 1024 bits from %s.\n",
  447. res * 8, key_location);
  448. goto parse_error;
  449. }
  450. if (totem_config->mcast_addr.family != totem_config->interfaces[0].bindnet.family) {
  451. strcpy (error_string_response, "Multicast address family does not match bind address family");
  452. goto parse_error;
  453. }
  454. for (i = 0; i < totem_config->interface_count; i++) {
  455. if (totem_config->mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  456. strcpy (error_string_response, "Not all bind address belong to the same IP family");
  457. goto parse_error;
  458. }
  459. }
  460. return (0);
  461. parse_error:
  462. *error_string = error_string_response;
  463. return (-1);
  464. }