totemconfig.c 16 KB

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