totemconfig.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2013 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * Jan Friesse (jfriesse@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <config.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <errno.h>
  41. #include <unistd.h>
  42. #include <sys/socket.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <sys/param.h>
  49. #include <corosync/swab.h>
  50. #include <qb/qblist.h>
  51. #include <qb/qbdefs.h>
  52. #include <libknet.h>
  53. #include <corosync/totem/totem.h>
  54. #include <corosync/config.h>
  55. #include <corosync/logsys.h>
  56. #include <corosync/icmap.h>
  57. #include "util.h"
  58. #include "totemconfig.h"
  59. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  60. #define TOKEN_TIMEOUT 1000
  61. #define TOKEN_COEFFICIENT 650
  62. #define JOIN_TIMEOUT 50
  63. #define MERGE_TIMEOUT 200
  64. #define DOWNCHECK_TIMEOUT 1000
  65. #define FAIL_TO_RECV_CONST 2500
  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. #define MISS_COUNT_CONST 5
  72. /* These currently match the defaults in libknet.h */
  73. #define KNET_PING_INTERVAL 1000
  74. #define KNET_PING_TIMEOUT 2000
  75. #define KNET_PING_PRECISION 2048
  76. #define KNET_PONG_COUNT 2
  77. #define KNET_PMTUD_INTERVAL 30
  78. #define KNET_DEFAULT_TRANSPORT KNET_TRANSPORT_UDP
  79. #define DEFAULT_PORT 5405
  80. static char error_string_response[512];
  81. static void add_totem_config_notification(struct totem_config *totem_config);
  82. static void *totem_get_param_by_name(struct totem_config *totem_config, const char *param_name)
  83. {
  84. if (strcmp(param_name, "totem.token") == 0)
  85. return &totem_config->token_timeout;
  86. if (strcmp(param_name, "totem.token_retransmit") == 0)
  87. return &totem_config->token_retransmit_timeout;
  88. if (strcmp(param_name, "totem.hold") == 0)
  89. return &totem_config->token_hold_timeout;
  90. if (strcmp(param_name, "totem.token_retransmits_before_loss_const") == 0)
  91. return &totem_config->token_retransmits_before_loss_const;
  92. if (strcmp(param_name, "totem.join") == 0)
  93. return &totem_config->join_timeout;
  94. if (strcmp(param_name, "totem.send_join") == 0)
  95. return &totem_config->send_join_timeout;
  96. if (strcmp(param_name, "totem.consensus") == 0)
  97. return &totem_config->consensus_timeout;
  98. if (strcmp(param_name, "totem.merge") == 0)
  99. return &totem_config->merge_timeout;
  100. if (strcmp(param_name, "totem.downcheck") == 0)
  101. return &totem_config->downcheck_timeout;
  102. if (strcmp(param_name, "totem.fail_recv_const") == 0)
  103. return &totem_config->fail_to_recv_const;
  104. if (strcmp(param_name, "totem.seqno_unchanged_const") == 0)
  105. return &totem_config->seqno_unchanged_const;
  106. if (strcmp(param_name, "totem.heartbeat_failures_allowed") == 0)
  107. return &totem_config->heartbeat_failures_allowed;
  108. if (strcmp(param_name, "totem.max_network_delay") == 0)
  109. return &totem_config->max_network_delay;
  110. if (strcmp(param_name, "totem.window_size") == 0)
  111. return &totem_config->window_size;
  112. if (strcmp(param_name, "totem.max_messages") == 0)
  113. return &totem_config->max_messages;
  114. if (strcmp(param_name, "totem.miss_count_const") == 0)
  115. return &totem_config->miss_count_const;
  116. if (strcmp(param_name, "totem.knet_pmtud_interval") == 0)
  117. return &totem_config->knet_pmtud_interval;
  118. if (strcmp(param_name, "totem.knet_compression_threshold") == 0)
  119. return &totem_config->knet_compression_threshold;
  120. if (strcmp(param_name, "totem.knet_compression_level") == 0)
  121. return &totem_config->knet_compression_level;
  122. if (strcmp(param_name, "totem.knet_compression_model") == 0)
  123. return &totem_config->knet_compression_model;
  124. return NULL;
  125. }
  126. /*
  127. * Read key_name from icmap. If key is not found or key_name == delete_key or if allow_zero is false
  128. * and readed value is zero, default value is used and stored into totem_config.
  129. */
  130. static void totem_volatile_config_set_uint32_value (struct totem_config *totem_config,
  131. const char *key_name, const char *deleted_key, unsigned int default_value,
  132. int allow_zero_value)
  133. {
  134. char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
  135. if (icmap_get_uint32(key_name, totem_get_param_by_name(totem_config, key_name)) != CS_OK ||
  136. (deleted_key != NULL && strcmp(deleted_key, key_name) == 0) ||
  137. (!allow_zero_value && *(uint32_t *)totem_get_param_by_name(totem_config, key_name) == 0)) {
  138. *(uint32_t *)totem_get_param_by_name(totem_config, key_name) = default_value;
  139. }
  140. /*
  141. * Store totem_config value to cmap runtime section
  142. */
  143. if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
  144. /*
  145. * This shouldn't happen
  146. */
  147. return ;
  148. }
  149. strcpy(runtime_key_name, "runtime.config.");
  150. strcat(runtime_key_name, key_name);
  151. icmap_set_uint32(runtime_key_name, *(uint32_t *)totem_get_param_by_name(totem_config, key_name));
  152. }
  153. static void totem_volatile_config_set_int32_value (struct totem_config *totem_config,
  154. const char *key_name, const char *deleted_key, int default_value,
  155. int allow_zero_value)
  156. {
  157. char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
  158. if (icmap_get_int32(key_name, totem_get_param_by_name(totem_config, key_name)) != CS_OK ||
  159. (deleted_key != NULL && strcmp(deleted_key, key_name) == 0) ||
  160. (!allow_zero_value && *(int32_t *)totem_get_param_by_name(totem_config, key_name) == 0)) {
  161. *(int32_t *)totem_get_param_by_name(totem_config, key_name) = default_value;
  162. }
  163. /*
  164. * Store totem_config value to cmap runtime section
  165. */
  166. if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
  167. /*
  168. * This shouldn't happen
  169. */
  170. return ;
  171. }
  172. strcpy(runtime_key_name, "runtime.config.");
  173. strcat(runtime_key_name, key_name);
  174. icmap_set_int32(runtime_key_name, *(int32_t *)totem_get_param_by_name(totem_config, key_name));
  175. }
  176. static void totem_volatile_config_set_string_value (struct totem_config *totem_config,
  177. const char *key_name, const char *deleted_key, const char *default_value)
  178. {
  179. char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
  180. void **config_value;
  181. void *old_config_ptr;
  182. config_value = totem_get_param_by_name(totem_config, key_name);
  183. old_config_ptr = *config_value;
  184. if (icmap_get_string(key_name, totem_get_param_by_name(totem_config, key_name)) != CS_OK ||
  185. (deleted_key != NULL && strcmp(deleted_key, key_name) == 0)) {
  186. /* Need to strdup() here so that the free() below works for a default and a configured value */
  187. *config_value = strdup(default_value);
  188. }
  189. free(old_config_ptr);
  190. /*
  191. * Store totem_config value to cmap runtime section
  192. */
  193. if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
  194. /*
  195. * This shouldn't happen
  196. */
  197. return ;
  198. }
  199. strcpy(runtime_key_name, "runtime.config.");
  200. strcat(runtime_key_name, key_name);
  201. icmap_set_string(runtime_key_name, (char *)*config_value);
  202. }
  203. /*
  204. * Read and validate config values from cmap and store them into totem_config. If key doesn't exists,
  205. * default value is stored. deleted_key is name of key beeing processed by delete operation
  206. * from cmap. It is considered as non existing even if it can be read. Can be NULL.
  207. */
  208. static void totem_volatile_config_read (struct totem_config *totem_config, const char *deleted_key)
  209. {
  210. uint32_t u32;
  211. totem_volatile_config_set_uint32_value(totem_config, "totem.token_retransmits_before_loss_const", deleted_key,
  212. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST, 0);
  213. totem_volatile_config_set_uint32_value(totem_config, "totem.token", deleted_key, TOKEN_TIMEOUT, 0);
  214. if (totem_config->interfaces[0].member_count > 2) {
  215. u32 = TOKEN_COEFFICIENT;
  216. icmap_get_uint32("totem.token_coefficient", &u32);
  217. totem_config->token_timeout += (totem_config->interfaces[0].member_count - 2) * u32;
  218. /*
  219. * Store totem_config value to cmap runtime section
  220. */
  221. icmap_set_uint32("runtime.config.totem.token", totem_config->token_timeout);
  222. }
  223. totem_volatile_config_set_uint32_value(totem_config, "totem.max_network_delay", deleted_key, MAX_NETWORK_DELAY, 0);
  224. totem_volatile_config_set_uint32_value(totem_config, "totem.window_size", deleted_key, WINDOW_SIZE, 0);
  225. totem_volatile_config_set_uint32_value(totem_config, "totem.max_messages", deleted_key, MAX_MESSAGES, 0);
  226. totem_volatile_config_set_uint32_value(totem_config, "totem.miss_count_const", deleted_key, MISS_COUNT_CONST, 0);
  227. totem_volatile_config_set_uint32_value(totem_config, "totem.knet_pmtud_interval", deleted_key, KNET_PMTUD_INTERVAL, 0);
  228. totem_volatile_config_set_uint32_value(totem_config, "totem.token_retransmit", deleted_key,
  229. (int)(totem_config->token_timeout / (totem_config->token_retransmits_before_loss_const + 0.2)), 0);
  230. totem_volatile_config_set_uint32_value(totem_config, "totem.hold", deleted_key,
  231. (int)(totem_config->token_retransmit_timeout * 0.8 - (1000/HZ)), 0);
  232. totem_volatile_config_set_uint32_value(totem_config, "totem.join", deleted_key, JOIN_TIMEOUT, 0);
  233. totem_volatile_config_set_uint32_value(totem_config, "totem.consensus", deleted_key,
  234. (int)(float)(1.2 * totem_config->token_timeout), 0);
  235. totem_volatile_config_set_uint32_value(totem_config, "totem.merge", deleted_key, MERGE_TIMEOUT, 0);
  236. totem_volatile_config_set_uint32_value(totem_config, "totem.downcheck", deleted_key, DOWNCHECK_TIMEOUT, 0);
  237. totem_volatile_config_set_uint32_value(totem_config, "totem.fail_recv_const", deleted_key, FAIL_TO_RECV_CONST, 0);
  238. totem_volatile_config_set_uint32_value(totem_config, "totem.seqno_unchanged_const", deleted_key,
  239. SEQNO_UNCHANGED_CONST, 0);
  240. totem_volatile_config_set_uint32_value(totem_config, "totem.send_join", deleted_key, 0, 1);
  241. totem_volatile_config_set_uint32_value(totem_config, "totem.heartbeat_failures_allowed", deleted_key, 0, 1);
  242. totem_volatile_config_set_uint32_value(totem_config, "totem.knet_compression_threshold", deleted_key, 0, 1);
  243. totem_volatile_config_set_int32_value(totem_config, "totem.knet_compression_level", deleted_key, 0, 1);
  244. totem_volatile_config_set_string_value(totem_config, "totem.knet_compression_model", deleted_key, "none");
  245. }
  246. static int totem_volatile_config_validate (
  247. struct totem_config *totem_config,
  248. const char **error_string)
  249. {
  250. static char local_error_reason[512];
  251. const char *error_reason = local_error_reason;
  252. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  253. snprintf (local_error_reason, sizeof(local_error_reason),
  254. "The max_network_delay parameter (%d ms) may not be less than (%d ms).",
  255. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  256. goto parse_error;
  257. }
  258. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  259. snprintf (local_error_reason, sizeof(local_error_reason),
  260. "The token timeout parameter (%d ms) may not be less than (%d ms).",
  261. totem_config->token_timeout, MINIMUM_TIMEOUT);
  262. goto parse_error;
  263. }
  264. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  265. snprintf (local_error_reason, sizeof(local_error_reason),
  266. "The token retransmit timeout parameter (%d ms) may not be less than (%d ms).",
  267. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  268. goto parse_error;
  269. }
  270. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  271. snprintf (local_error_reason, sizeof(local_error_reason),
  272. "The token hold timeout parameter (%d ms) may not be less than (%d ms).",
  273. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  274. goto parse_error;
  275. }
  276. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  277. snprintf (local_error_reason, sizeof(local_error_reason),
  278. "The join timeout parameter (%d ms) may not be less than (%d ms).",
  279. totem_config->join_timeout, MINIMUM_TIMEOUT);
  280. goto parse_error;
  281. }
  282. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  283. snprintf (local_error_reason, sizeof(local_error_reason),
  284. "The consensus timeout parameter (%d ms) may not be less than (%d ms).",
  285. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  286. goto parse_error;
  287. }
  288. if (totem_config->consensus_timeout < totem_config->join_timeout) {
  289. snprintf (local_error_reason, sizeof(local_error_reason),
  290. "The consensus timeout parameter (%d ms) may not be less than join timeout (%d ms).",
  291. totem_config->consensus_timeout, totem_config->join_timeout);
  292. goto parse_error;
  293. }
  294. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  295. snprintf (local_error_reason, sizeof(local_error_reason),
  296. "The merge timeout parameter (%d ms) may not be less than (%d ms).",
  297. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  298. goto parse_error;
  299. }
  300. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  301. snprintf (local_error_reason, sizeof(local_error_reason),
  302. "The downcheck timeout parameter (%d ms) may not be less than (%d ms).",
  303. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  304. goto parse_error;
  305. }
  306. return 0;
  307. parse_error:
  308. snprintf (error_string_response, sizeof(error_string_response),
  309. "parse error in config: %s\n", error_reason);
  310. *error_string = error_string_response;
  311. return (-1);
  312. }
  313. static int totem_get_crypto(struct totem_config *totem_config, const char **error_string)
  314. {
  315. char *str;
  316. const char *tmp_cipher;
  317. const char *tmp_hash;
  318. const char *tmp_model;
  319. tmp_hash = "none";
  320. tmp_cipher = "none";
  321. tmp_model = "none";
  322. if (icmap_get_string("totem.crypto_model", &str) == CS_OK) {
  323. if (strcmp(str, "nss") == 0) {
  324. tmp_model = "nss";
  325. }
  326. if (strcmp(str, "openssl") == 0) {
  327. tmp_model = "openssl";
  328. }
  329. free(str);
  330. } else {
  331. tmp_model = "nss";
  332. }
  333. if (icmap_get_string("totem.crypto_cipher", &str) == CS_OK) {
  334. if (strcmp(str, "none") == 0) {
  335. tmp_cipher = "none";
  336. }
  337. if (strcmp(str, "aes256") == 0) {
  338. tmp_cipher = "aes256";
  339. }
  340. if (strcmp(str, "aes192") == 0) {
  341. tmp_cipher = "aes192";
  342. }
  343. if (strcmp(str, "aes128") == 0) {
  344. tmp_cipher = "aes128";
  345. }
  346. if (strcmp(str, "3des") == 0) {
  347. tmp_cipher = "3des";
  348. }
  349. free(str);
  350. }
  351. if (icmap_get_string("totem.crypto_hash", &str) == CS_OK) {
  352. if (strcmp(str, "none") == 0) {
  353. tmp_hash = "none";
  354. }
  355. if (strcmp(str, "md5") == 0) {
  356. tmp_hash = "md5";
  357. }
  358. if (strcmp(str, "sha1") == 0) {
  359. tmp_hash = "sha1";
  360. }
  361. if (strcmp(str, "sha256") == 0) {
  362. tmp_hash = "sha256";
  363. }
  364. if (strcmp(str, "sha384") == 0) {
  365. tmp_hash = "sha384";
  366. }
  367. if (strcmp(str, "sha512") == 0) {
  368. tmp_hash = "sha512";
  369. }
  370. free(str);
  371. }
  372. if ((strcmp(tmp_cipher, "none") != 0) &&
  373. (strcmp(tmp_hash, "none") == 0)) {
  374. *error_string = "crypto_cipher requires crypto_hash with value other than none";
  375. return -1;
  376. }
  377. if (strcmp(tmp_model, "none") == 0) {
  378. *error_string = "crypto_model should be 'nss' or 'openssl'";
  379. return -1;
  380. }
  381. free(totem_config->crypto_cipher_type);
  382. free(totem_config->crypto_hash_type);
  383. free(totem_config->crypto_model);
  384. totem_config->crypto_cipher_type = strdup(tmp_cipher);
  385. totem_config->crypto_hash_type = strdup(tmp_hash);
  386. totem_config->crypto_model = strdup(tmp_model);
  387. return 0;
  388. }
  389. static int totem_config_get_ip_version(struct totem_config *totem_config)
  390. {
  391. int res;
  392. char *str;
  393. res = AF_INET;
  394. if (totem_config->transport_number == TOTEM_TRANSPORT_KNET) {
  395. res = AF_UNSPEC;
  396. } else {
  397. if (icmap_get_string("totem.ip_version", &str) == CS_OK) {
  398. if (strcmp(str, "ipv4") == 0) {
  399. res = AF_INET;
  400. }
  401. if (strcmp(str, "ipv6") == 0) {
  402. res = AF_INET6;
  403. }
  404. free(str);
  405. }
  406. }
  407. return (res);
  408. }
  409. static uint16_t generate_cluster_id (const char *cluster_name)
  410. {
  411. int i;
  412. int value = 0;
  413. for (i = 0; i < strlen(cluster_name); i++) {
  414. value <<= 1;
  415. value += cluster_name[i];
  416. }
  417. return (value & 0xFFFF);
  418. }
  419. static int get_cluster_mcast_addr (
  420. const char *cluster_name,
  421. unsigned int linknumber,
  422. int ip_version,
  423. struct totem_ip_address *res)
  424. {
  425. uint16_t clusterid;
  426. char addr[INET6_ADDRSTRLEN + 1];
  427. int err;
  428. if (cluster_name == NULL) {
  429. return (-1);
  430. }
  431. clusterid = generate_cluster_id(cluster_name) + linknumber;
  432. memset (res, 0, sizeof(*res));
  433. switch (ip_version) {
  434. case AF_INET:
  435. snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
  436. break;
  437. case AF_INET6:
  438. snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
  439. break;
  440. default:
  441. /*
  442. * Unknown family
  443. */
  444. return (-1);
  445. }
  446. err = totemip_parse (res, addr, ip_version);
  447. return (err);
  448. }
  449. static unsigned int generate_nodeid_for_duplicate_test(
  450. struct totem_config *totem_config,
  451. char *addr)
  452. {
  453. unsigned int nodeid;
  454. struct totem_ip_address totemip;
  455. /* AF_INET hard-coded here because auto-generated nodeids
  456. are only for IPv4 */
  457. if (totemip_parse(&totemip, addr, AF_INET) != 0)
  458. return -1;
  459. memcpy (&nodeid, &totemip.addr, sizeof (unsigned int));
  460. #if __BYTE_ORDER == __LITTLE_ENDIAN
  461. nodeid = swab32 (nodeid);
  462. #endif
  463. if (totem_config->clear_node_high_bit) {
  464. nodeid &= 0x7FFFFFFF;
  465. }
  466. return nodeid;
  467. }
  468. static int check_for_duplicate_nodeids(
  469. struct totem_config *totem_config,
  470. const char **error_string)
  471. {
  472. icmap_iter_t iter;
  473. icmap_iter_t subiter;
  474. const char *iter_key;
  475. int res = 0;
  476. int retval = 0;
  477. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  478. char *ring0_addr=NULL;
  479. char *ring0_addr1=NULL;
  480. unsigned int node_pos;
  481. unsigned int node_pos1;
  482. unsigned int nodeid;
  483. unsigned int nodeid1;
  484. int autogenerated;
  485. iter = icmap_iter_init("nodelist.node.");
  486. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  487. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  488. if (res != 2) {
  489. continue;
  490. }
  491. if (strcmp(tmp_key, "ring0_addr") != 0) {
  492. continue;
  493. }
  494. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  495. autogenerated = 0;
  496. if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
  497. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  498. if (icmap_get_string(tmp_key, &ring0_addr) != CS_OK) {
  499. continue;
  500. }
  501. /* Generate nodeid so we can check that auto-generated nodeids don't clash either */
  502. nodeid = generate_nodeid_for_duplicate_test(totem_config, ring0_addr);
  503. if (nodeid == -1) {
  504. continue;
  505. }
  506. autogenerated = 1;
  507. }
  508. node_pos1 = 0;
  509. subiter = icmap_iter_init("nodelist.node.");
  510. while (((iter_key = icmap_iter_next(subiter, NULL, NULL)) != NULL) && (node_pos1 < node_pos)) {
  511. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos1, tmp_key);
  512. if ((res != 2) || (node_pos1 >= node_pos)) {
  513. continue;
  514. }
  515. if (strcmp(tmp_key, "ring0_addr") != 0) {
  516. continue;
  517. }
  518. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos1);
  519. if (icmap_get_uint32(tmp_key, &nodeid1) != CS_OK) {
  520. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos1);
  521. if (icmap_get_string(tmp_key, &ring0_addr1) != CS_OK) {
  522. continue;
  523. }
  524. nodeid1 = generate_nodeid_for_duplicate_test(totem_config, ring0_addr1);
  525. if (nodeid1 == -1) {
  526. continue;
  527. }
  528. }
  529. if (nodeid == nodeid1) {
  530. retval = -1;
  531. snprintf (error_string_response, sizeof(error_string_response),
  532. "Nodeid %u%s%s%s appears twice in corosync.conf", nodeid,
  533. autogenerated?"(autogenerated from ":"",
  534. autogenerated?ring0_addr:"",
  535. autogenerated?")":"");
  536. log_printf (LOGSYS_LEVEL_ERROR, error_string_response);
  537. *error_string = error_string_response;
  538. break;
  539. }
  540. }
  541. icmap_iter_finalize(subiter);
  542. }
  543. icmap_iter_finalize(iter);
  544. return retval;
  545. }
  546. static int find_local_node_in_nodelist(struct totem_config *totem_config)
  547. {
  548. icmap_iter_t iter;
  549. const char *iter_key;
  550. int res = 0;
  551. unsigned int node_pos;
  552. int local_node_pos = -1;
  553. struct totem_ip_address bind_addr;
  554. int interface_up, interface_num;
  555. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  556. char *node_addr_str;
  557. struct totem_ip_address node_addr;
  558. res = totemip_iface_check(&totem_config->interfaces[0].bindnet,
  559. &bind_addr, &interface_up, &interface_num,
  560. totem_config->clear_node_high_bit);
  561. if (res == -1) {
  562. return (-1);
  563. }
  564. iter = icmap_iter_init("nodelist.node.");
  565. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  566. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  567. if (res != 2) {
  568. continue;
  569. }
  570. if (strcmp(tmp_key, "ring0_addr") != 0) {
  571. continue;
  572. }
  573. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  574. if (icmap_get_string(tmp_key, &node_addr_str) != CS_OK) {
  575. continue;
  576. }
  577. res = totemip_parse (&node_addr, node_addr_str, totem_config->ip_version);
  578. free(node_addr_str);
  579. if (res == -1) {
  580. continue ;
  581. }
  582. if (totemip_equal(&bind_addr, &node_addr)) {
  583. local_node_pos = node_pos;
  584. }
  585. }
  586. icmap_iter_finalize(iter);
  587. return (local_node_pos);
  588. }
  589. /*
  590. * This needs to be done last of all. It would be nice to do it when reading the
  591. * interface params, but the totem params need to have them to be read first. We
  592. * need both, so this is a way round that circular dependancy.
  593. */
  594. static void calc_knet_ping_timers(struct totem_config *totem_config)
  595. {
  596. char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
  597. int interface;
  598. for (interface = 0; interface < INTERFACE_MAX; interface++) {
  599. if (totem_config->interfaces[interface].configured) {
  600. if (!totem_config->interfaces[interface].knet_ping_timeout) {
  601. totem_config->interfaces[interface].knet_ping_timeout =
  602. totem_config->token_timeout / totem_config->interfaces[interface].knet_pong_count;
  603. }
  604. snprintf(runtime_key_name, sizeof(runtime_key_name),
  605. "runtime.config.totem.interface.%d.knet_ping_timeout", interface);
  606. icmap_set_uint32(runtime_key_name, totem_config->interfaces[interface].knet_ping_timeout);
  607. if (!totem_config->interfaces[interface].knet_ping_interval) {
  608. totem_config->interfaces[interface].knet_ping_interval =
  609. totem_config->token_timeout / (totem_config->interfaces[interface].knet_pong_count * 2);
  610. }
  611. snprintf(runtime_key_name, sizeof(runtime_key_name),
  612. "runtime.config.totem.interface.%d.knet_ping_interval", interface);
  613. icmap_set_uint32(runtime_key_name, totem_config->interfaces[interface].knet_ping_interval);
  614. }
  615. }
  616. }
  617. /*
  618. * Compute difference between two set of totem interface arrays. set1 and set2
  619. * are changed so for same ring, ip existing in both set1 and set2 are cleared
  620. * (set to 0), and ips which are only in set1 or set2 remains untouched.
  621. * totempg_node_add/remove is called.
  622. */
  623. static void compute_interfaces_diff(struct totem_interface *set1,
  624. struct totem_interface *set2)
  625. {
  626. int ring_no, set1_pos, set2_pos;
  627. struct totem_ip_address empty_ip_address;
  628. memset(&empty_ip_address, 0, sizeof(empty_ip_address));
  629. for (ring_no = 0; ring_no < INTERFACE_MAX; ring_no++) {
  630. if (!set1[ring_no].configured && !set2[ring_no].configured) {
  631. continue;
  632. }
  633. for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
  634. for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
  635. /*
  636. * For current ring_no remove all set1 items existing
  637. * in set2
  638. */
  639. if (memcmp(&set1[ring_no].member_list[set1_pos],
  640. &set2[ring_no].member_list[set2_pos],
  641. sizeof(struct totem_ip_address)) == 0) {
  642. memset(&set1[ring_no].member_list[set1_pos], 0,
  643. sizeof(struct totem_ip_address));
  644. memset(&set2[ring_no].member_list[set2_pos], 0,
  645. sizeof(struct totem_ip_address));
  646. }
  647. }
  648. }
  649. }
  650. for (ring_no = 0; ring_no < INTERFACE_MAX; ring_no++) {
  651. for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
  652. /*
  653. * All items which remained in set1 doesn't exists in set2 any longer so
  654. * node has to be removed.
  655. */
  656. if (memcmp(&set1[ring_no].member_list[set1_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
  657. log_printf(LOGSYS_LEVEL_DEBUG,
  658. "removing dynamic member %s for ring %u",
  659. totemip_print(&set1[ring_no].member_list[set1_pos]),
  660. ring_no);
  661. totempg_member_remove(&set1[ring_no].member_list[set1_pos], ring_no);
  662. }
  663. }
  664. if (!set2[ring_no].configured) {
  665. continue;
  666. }
  667. for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
  668. /*
  669. * All items which remained in set2 doesn't existed in set1 so this is no node
  670. * and has to be added.
  671. */
  672. if (memcmp(&set2[ring_no].member_list[set2_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
  673. log_printf(LOGSYS_LEVEL_DEBUG,
  674. "adding dynamic member %s for ring %u",
  675. totemip_print(&set2[ring_no].member_list[set2_pos]),
  676. ring_no);
  677. totempg_member_add(&set2[ring_no].member_list[set2_pos], ring_no);
  678. }
  679. }
  680. }
  681. }
  682. /*
  683. * Reconfigure links in totempg. Sets new local IP address and adds params for new links.
  684. */
  685. static void reconfigure_links(struct totem_config *totem_config)
  686. {
  687. int i;
  688. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  689. char *addr_string;
  690. struct totem_ip_address local_ip;
  691. int err;
  692. unsigned int local_node_pos = find_local_node_in_nodelist(totem_config);
  693. for (i = 0; i<INTERFACE_MAX; i++) {
  694. if (!totem_config->interfaces[i].configured) {
  695. continue;
  696. }
  697. log_printf(LOGSYS_LEVEL_INFO, "Configuring link %d\n", i);
  698. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring%u_addr", local_node_pos, i);
  699. if (icmap_get_string(tmp_key, &addr_string) != CS_OK) {
  700. continue;
  701. }
  702. err = totemip_parse(&local_ip, addr_string, AF_UNSPEC);
  703. if (err != 0) {
  704. continue;
  705. }
  706. local_ip.nodeid = totem_config->node_id;
  707. /* In case this is a new link, fill in the defaults if there was no interface{} section for it */
  708. if (!totem_config->interfaces[i].knet_link_priority)
  709. totem_config->interfaces[i].knet_link_priority = 1;
  710. /* knet_ping_interval & knet_ping_timeout are set later once we know all the other params */
  711. if (!totem_config->interfaces[i].knet_ping_precision)
  712. totem_config->interfaces[i].knet_ping_precision = KNET_PING_PRECISION;
  713. if (!totem_config->interfaces[i].knet_pong_count)
  714. totem_config->interfaces[i].knet_pong_count = KNET_PONG_COUNT;
  715. if (!totem_config->interfaces[i].knet_transport)
  716. totem_config->interfaces[i].knet_transport = KNET_TRANSPORT_UDP;
  717. if (!totem_config->interfaces[i].ip_port)
  718. totem_config->interfaces[i].ip_port = DEFAULT_PORT + i;
  719. totempg_iface_set(&local_ip, totem_config->interfaces[i].ip_port, i);
  720. }
  721. }
  722. static void put_nodelist_members_to_config(struct totem_config *totem_config, int reload)
  723. {
  724. icmap_iter_t iter, iter2;
  725. const char *iter_key, *iter_key2;
  726. int res = 0;
  727. unsigned int node_pos;
  728. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  729. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  730. char *node_addr_str;
  731. int member_count;
  732. unsigned int linknumber = 0;
  733. int i, j;
  734. struct totem_interface *new_interfaces = NULL;
  735. if (reload) {
  736. /*
  737. * We need to compute diff only for reload. Also for initial configuration
  738. * not all totem structures are initialized so corosync will crash during
  739. * member_add/remove
  740. */
  741. new_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  742. assert(new_interfaces != NULL);
  743. }
  744. /* Clear out nodelist so we can put the new one in if needed */
  745. for (i = 0; i < INTERFACE_MAX; i++) {
  746. for (j = 0; j < PROCESSOR_COUNT_MAX; j++) {
  747. memset(&totem_config->interfaces[i].member_list[j], 0, sizeof(struct totem_ip_address));
  748. }
  749. totem_config->interfaces[i].member_count = 0;
  750. }
  751. iter = icmap_iter_init("nodelist.node.");
  752. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  753. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  754. if (res != 2) {
  755. continue;
  756. }
  757. if (strcmp(tmp_key, "ring0_addr") != 0) {
  758. continue;
  759. }
  760. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  761. iter2 = icmap_iter_init(tmp_key);
  762. while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
  763. unsigned int nodeid;
  764. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  765. if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
  766. }
  767. res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
  768. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  769. continue;
  770. }
  771. if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
  772. continue;
  773. }
  774. member_count = totem_config->interfaces[linknumber].member_count;
  775. res = totemip_parse(&totem_config->interfaces[linknumber].member_list[member_count],
  776. node_addr_str, totem_config->ip_version);
  777. if (res != -1) {
  778. totem_config->interfaces[linknumber].member_list[member_count].nodeid = nodeid;
  779. totem_config->interfaces[linknumber].member_count++;
  780. }
  781. totem_config->interfaces[linknumber].configured = 1;
  782. free(node_addr_str);
  783. }
  784. icmap_iter_finalize(iter2);
  785. }
  786. icmap_iter_finalize(iter);
  787. if (reload) {
  788. log_printf(LOGSYS_LEVEL_DEBUG, "About to reconfigure links from nodelist.\n");
  789. reconfigure_links(totem_config);
  790. memcpy(new_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
  791. compute_interfaces_diff(totem_config->orig_interfaces, new_interfaces);
  792. free(new_interfaces);
  793. }
  794. }
  795. static void nodelist_dynamic_notify(
  796. int32_t event,
  797. const char *key_name,
  798. struct icmap_notify_value new_val,
  799. struct icmap_notify_value old_val,
  800. void *user_data)
  801. {
  802. int res;
  803. unsigned int ring_no;
  804. unsigned int member_no;
  805. char tmp_str[ICMAP_KEYNAME_MAXLEN];
  806. uint8_t reloading;
  807. struct totem_config *totem_config = (struct totem_config *)user_data;
  808. /*
  809. * If a full reload is in progress then don't do anything until it's done and
  810. * can reconfigure it all atomically
  811. */
  812. if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
  813. return ;
  814. }
  815. res = sscanf(key_name, "nodelist.node.%u.ring%u%s", &member_no, &ring_no, tmp_str);
  816. if (res != 3)
  817. return ;
  818. if (strcmp(tmp_str, "_addr") != 0) {
  819. return;
  820. }
  821. put_nodelist_members_to_config(totem_config, 1);
  822. }
  823. /*
  824. * Tries to find node (node_pos) in config nodelist which address matches any
  825. * local interface. Address can be stored in ring0_addr or if ipaddr_key_prefix is not NULL
  826. * key with prefix ipaddr_key is used (there can be multiuple of them)
  827. * This function differs * from find_local_node_in_nodelist because it doesn't need bindnetaddr,
  828. * but doesn't work when bind addr is network address (so IP must be exact
  829. * match).
  830. *
  831. * Returns 1 on success (address was found, node_pos is then correctly set) or 0 on failure.
  832. */
  833. int totem_config_find_local_addr_in_nodelist(struct totem_config *totem_config, const char *ipaddr_key_prefix, unsigned int *node_pos)
  834. {
  835. struct qb_list_head addrs;
  836. struct totem_ip_if_address *if_addr;
  837. icmap_iter_t iter, iter2;
  838. const char *iter_key, *iter_key2;
  839. struct qb_list_head *list;
  840. const char *ipaddr_key;
  841. int ip_version;
  842. struct totem_ip_address node_addr;
  843. char *node_addr_str;
  844. int node_found = 0;
  845. int res = 0;
  846. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  847. if (totemip_getifaddrs(&addrs) == -1) {
  848. return 0;
  849. }
  850. ip_version = totem_config_get_ip_version(totem_config);
  851. iter = icmap_iter_init("nodelist.node.");
  852. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  853. res = sscanf(iter_key, "nodelist.node.%u.%s", node_pos, tmp_key);
  854. if (res != 2) {
  855. continue;
  856. }
  857. if (strcmp(tmp_key, "ring0_addr") != 0) {
  858. continue;
  859. }
  860. if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
  861. continue ;
  862. }
  863. free(node_addr_str);
  864. /*
  865. * ring0_addr found -> let's iterate thru ipaddr_key_prefix
  866. */
  867. snprintf(tmp_key, sizeof(tmp_key), "nodelist.node.%u.%s", *node_pos,
  868. (ipaddr_key_prefix != NULL ? ipaddr_key_prefix : "ring0_addr"));
  869. iter2 = icmap_iter_init(tmp_key);
  870. while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
  871. /*
  872. * ring0_addr must be exact match, not prefix
  873. */
  874. ipaddr_key = (ipaddr_key_prefix != NULL ? iter_key2 : tmp_key);
  875. if (icmap_get_string(ipaddr_key, &node_addr_str) != CS_OK) {
  876. continue ;
  877. }
  878. if (totemip_parse(&node_addr, node_addr_str, ip_version) == -1) {
  879. free(node_addr_str);
  880. continue ;
  881. }
  882. free(node_addr_str);
  883. /*
  884. * Try to match ip with if_addrs
  885. */
  886. node_found = 0;
  887. qb_list_for_each(list, &(addrs)) {
  888. if_addr = qb_list_entry(list, struct totem_ip_if_address, list);
  889. if (totemip_equal(&node_addr, &if_addr->ip_addr)) {
  890. node_found = 1;
  891. break;
  892. }
  893. }
  894. if (node_found) {
  895. break ;
  896. }
  897. }
  898. icmap_iter_finalize(iter2);
  899. if (node_found) {
  900. break ;
  901. }
  902. }
  903. icmap_iter_finalize(iter);
  904. totemip_freeifaddrs(&addrs);
  905. return (node_found);
  906. }
  907. static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
  908. {
  909. int res = 0;
  910. unsigned int node_pos;
  911. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  912. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  913. char *node_addr_str;
  914. unsigned int linknumber = 0;
  915. icmap_iter_t iter;
  916. const char *iter_key;
  917. if (totem_config_find_local_addr_in_nodelist(totem_config, NULL, &node_pos)) {
  918. /*
  919. * We found node, so create interface section
  920. */
  921. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  922. iter = icmap_iter_init(tmp_key);
  923. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  924. res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
  925. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  926. continue ;
  927. }
  928. if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
  929. continue;
  930. }
  931. snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
  932. icmap_set_string(tmp_key2, node_addr_str);
  933. free(node_addr_str);
  934. }
  935. icmap_iter_finalize(iter);
  936. }
  937. }
  938. static int get_interface_params(struct totem_config *totem_config,
  939. const char **error_string, uint64_t *warnings,
  940. int reload)
  941. {
  942. int res = 0;
  943. unsigned int linknumber = 0;
  944. int member_count = 0;
  945. int i;
  946. icmap_iter_t iter, member_iter;
  947. const char *iter_key;
  948. const char *member_iter_key;
  949. char linknumber_key[ICMAP_KEYNAME_MAXLEN];
  950. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  951. uint8_t u8;
  952. uint32_t u32;
  953. char *str;
  954. char *cluster_name = NULL;
  955. if (reload) {
  956. for (i=0; i<INTERFACE_MAX; i++) {
  957. /*
  958. * Set back to defaults things that might have been configured and
  959. * now have been taken out of corosync.conf. These won't be caught by the
  960. * code below which only looks at interface{} sections that actually exist.
  961. */
  962. totem_config->interfaces[i].configured = 0;
  963. totem_config->interfaces[i].knet_ping_timeout = 0;
  964. totem_config->interfaces[i].knet_ping_interval = 0;
  965. totem_config->interfaces[i].knet_ping_precision = KNET_PING_PRECISION;
  966. totem_config->interfaces[i].knet_pong_count = KNET_PONG_COUNT;
  967. }
  968. }
  969. if (icmap_get_string("totem.cluster_name", &cluster_name) != CS_OK) {
  970. cluster_name = NULL;
  971. }
  972. iter = icmap_iter_init("totem.interface.");
  973. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  974. res = sscanf(iter_key, "totem.interface.%[^.].%s", linknumber_key, tmp_key);
  975. if (res != 2) {
  976. continue;
  977. }
  978. if (strcmp(tmp_key, "bindnetaddr") != 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
  979. continue;
  980. }
  981. member_count = 0;
  982. linknumber = atoi(linknumber_key);
  983. if (linknumber >= INTERFACE_MAX) {
  984. free(cluster_name);
  985. snprintf (error_string_response, sizeof(error_string_response),
  986. "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
  987. linknumber, INTERFACE_MAX - 1);
  988. *error_string = error_string_response;
  989. return -1;
  990. }
  991. /* These things are only valid for the initial read */
  992. if (!reload) {
  993. /*
  994. * Get the bind net address
  995. */
  996. if (icmap_get_string(iter_key, &str) == CS_OK) {
  997. res = totemip_parse (&totem_config->interfaces[linknumber].bindnet, str,
  998. totem_config->ip_version);
  999. free(str);
  1000. }
  1001. /*
  1002. * Get interface multicast address
  1003. */
  1004. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", linknumber);
  1005. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  1006. res = totemip_parse (&totem_config->interfaces[linknumber].mcast_addr, str, totem_config->ip_version);
  1007. free(str);
  1008. } else {
  1009. /*
  1010. * User not specified address -> autogenerate one from cluster_name key
  1011. * (if available). Return code is intentionally ignored, because
  1012. * udpu doesn't need mcastaddr and validity of mcastaddr for udp is
  1013. * checked later anyway.
  1014. */
  1015. (void)get_cluster_mcast_addr (cluster_name,
  1016. linknumber,
  1017. totem_config->ip_version,
  1018. &totem_config->interfaces[linknumber].mcast_addr);
  1019. }
  1020. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", linknumber);
  1021. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  1022. if (strcmp (str, "yes") == 0) {
  1023. totem_config->broadcast_use = 1;
  1024. }
  1025. free(str);
  1026. }
  1027. }
  1028. /* These things are only valid for the initial read OR a newly-defined link */
  1029. if (!reload || (totem_config->interfaces[linknumber].configured == 0)) {
  1030. /*
  1031. * Get mcast port
  1032. */
  1033. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", linknumber);
  1034. if (icmap_get_uint16(tmp_key, &totem_config->interfaces[linknumber].ip_port) != CS_OK) {
  1035. if (totem_config->broadcast_use) {
  1036. totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + (2 * linknumber);
  1037. } else {
  1038. totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + linknumber;
  1039. }
  1040. }
  1041. /*
  1042. * Get the TTL
  1043. */
  1044. totem_config->interfaces[linknumber].ttl = 1;
  1045. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", linknumber);
  1046. if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
  1047. totem_config->interfaces[linknumber].ttl = u8;
  1048. }
  1049. totem_config->interfaces[linknumber].knet_transport = KNET_DEFAULT_TRANSPORT;
  1050. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_transport", linknumber);
  1051. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  1052. if (strcmp(str, "sctp") == 0) {
  1053. totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_SCTP;
  1054. }
  1055. else if (strcmp(str, "udp") == 0) {
  1056. totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_UDP;
  1057. }
  1058. else {
  1059. *error_string = "Unrecognised knet_transport. expected 'udp' or 'sctp'";
  1060. return -1;
  1061. }
  1062. }
  1063. }
  1064. totem_config->interfaces[linknumber].configured = 1;
  1065. /*
  1066. * Get the knet link params
  1067. */
  1068. totem_config->interfaces[linknumber].knet_link_priority = 1;
  1069. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_link_priority", linknumber);
  1070. if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
  1071. totem_config->interfaces[linknumber].knet_link_priority = u8;
  1072. }
  1073. totem_config->interfaces[linknumber].knet_ping_interval = 0; /* real default applied later */
  1074. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_interval", linknumber);
  1075. if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
  1076. totem_config->interfaces[linknumber].knet_ping_interval = u32;
  1077. }
  1078. totem_config->interfaces[linknumber].knet_ping_timeout = 0; /* real default applied later */
  1079. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_timeout", linknumber);
  1080. if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
  1081. totem_config->interfaces[linknumber].knet_ping_timeout = u32;
  1082. }
  1083. totem_config->interfaces[linknumber].knet_ping_precision = KNET_PING_PRECISION;
  1084. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_precision", linknumber);
  1085. if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
  1086. totem_config->interfaces[linknumber].knet_ping_precision = u32;
  1087. }
  1088. totem_config->interfaces[linknumber].knet_pong_count = KNET_PONG_COUNT;
  1089. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_pong_count", linknumber);
  1090. if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
  1091. totem_config->interfaces[linknumber].knet_pong_count = u32;
  1092. }
  1093. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", linknumber);
  1094. member_iter = icmap_iter_init(tmp_key);
  1095. while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
  1096. if (member_count == 0) {
  1097. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  1098. free(str);
  1099. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
  1100. break;
  1101. } else {
  1102. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
  1103. }
  1104. }
  1105. if (icmap_get_string(member_iter_key, &str) == CS_OK) {
  1106. res = totemip_parse (&totem_config->interfaces[linknumber].member_list[member_count++],
  1107. str, totem_config->ip_version);
  1108. }
  1109. }
  1110. icmap_iter_finalize(member_iter);
  1111. totem_config->interfaces[linknumber].member_count = member_count;
  1112. }
  1113. icmap_iter_finalize(iter);
  1114. return 0;
  1115. }
  1116. extern int totem_config_read (
  1117. struct totem_config *totem_config,
  1118. const char **error_string,
  1119. uint64_t *warnings)
  1120. {
  1121. int res = 0;
  1122. char *str, *ring0_addr_str;
  1123. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  1124. uint16_t u16;
  1125. int i;
  1126. int local_node_pos;
  1127. int nodeid_set;
  1128. *warnings = 0;
  1129. memset (totem_config, 0, sizeof (struct totem_config));
  1130. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  1131. if (totem_config->interfaces == 0) {
  1132. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  1133. return -1;
  1134. }
  1135. totem_config->transport_number = TOTEM_TRANSPORT_KNET;
  1136. if (icmap_get_string("totem.transport", &str) == CS_OK) {
  1137. if (strcmp (str, "udpu") == 0) {
  1138. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  1139. }
  1140. if (strcmp (str, "udp") == 0) {
  1141. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  1142. }
  1143. if (strcmp (str, "knet") == 0) {
  1144. totem_config->transport_number = TOTEM_TRANSPORT_KNET;
  1145. }
  1146. free(str);
  1147. }
  1148. memset (totem_config->interfaces, 0,
  1149. sizeof (struct totem_interface) * INTERFACE_MAX);
  1150. strcpy (totem_config->link_mode, "passive");
  1151. icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
  1152. if (totem_get_crypto(totem_config, error_string) != 0) {
  1153. return -1;
  1154. }
  1155. if (icmap_get_string("totem.link_mode", &str) == CS_OK) {
  1156. if (strlen(str) >= TOTEM_LINK_MODE_BYTES) {
  1157. *error_string = "totem.link_mode is too long";
  1158. free(str);
  1159. return -1;
  1160. }
  1161. strcpy (totem_config->link_mode, str);
  1162. free(str);
  1163. }
  1164. icmap_get_uint32("totem.nodeid", &totem_config->node_id);
  1165. totem_config->clear_node_high_bit = 0;
  1166. if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
  1167. if (strcmp (str, "yes") == 0) {
  1168. totem_config->clear_node_high_bit = 1;
  1169. }
  1170. free(str);
  1171. }
  1172. icmap_get_uint32("totem.threads", &totem_config->threads);
  1173. icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
  1174. totem_config->ip_version = totem_config_get_ip_version(totem_config);
  1175. if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
  1176. /*
  1177. * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
  1178. */
  1179. config_convert_nodelist_to_interface(totem_config);
  1180. } else {
  1181. if (icmap_get_string("nodelist.node.0.ring0_addr", &ring0_addr_str) == CS_OK) {
  1182. /*
  1183. * Both bindnetaddr and ring0_addr are set.
  1184. * Log warning information, and use nodelist instead
  1185. */
  1186. *warnings |= TOTEM_CONFIG_BINDNETADDR_NODELIST_SET;
  1187. config_convert_nodelist_to_interface(totem_config);
  1188. free(ring0_addr_str);
  1189. }
  1190. free(str);
  1191. }
  1192. /*
  1193. * Broadcast option is global but set in interface section,
  1194. * so reset before processing interfaces.
  1195. */
  1196. totem_config->broadcast_use = 0;
  1197. res = get_interface_params(totem_config, error_string, warnings, 0);
  1198. if (res < 0) {
  1199. return res;
  1200. }
  1201. /*
  1202. * Use broadcast is global, so if set, make sure to fill mcast addr correctly
  1203. * broadcast is only supported for UDP so just do interface 0;
  1204. */
  1205. if (totem_config->broadcast_use) {
  1206. totemip_parse (&totem_config->interfaces[0].mcast_addr,
  1207. "255.255.255.255", 0);
  1208. }
  1209. /*
  1210. * Store automatically generated items back to icmap only for UDP
  1211. */
  1212. if (totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
  1213. for (i = 0; i < INTERFACE_MAX; i++) {
  1214. if (!totem_config->interfaces[i].configured) {
  1215. continue;
  1216. }
  1217. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
  1218. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  1219. free(str);
  1220. } else {
  1221. str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
  1222. icmap_set_string(tmp_key, str);
  1223. }
  1224. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
  1225. if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
  1226. icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
  1227. }
  1228. }
  1229. }
  1230. /*
  1231. * Check existence of nodelist
  1232. */
  1233. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  1234. free(str);
  1235. /*
  1236. * find local node
  1237. */
  1238. local_node_pos = find_local_node_in_nodelist(totem_config);
  1239. if (local_node_pos != -1) {
  1240. icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
  1241. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
  1242. nodeid_set = (totem_config->node_id != 0);
  1243. if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
  1244. *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
  1245. }
  1246. if ((totem_config->transport_number == TOTEM_TRANSPORT_KNET) && (!totem_config->node_id)) {
  1247. *error_string = "With knet, you must specify nodeid for current node";
  1248. return -1;
  1249. }
  1250. /*
  1251. * Make localnode ring0_addr read only, so we can be sure that local
  1252. * node never changes. If rebinding to other IP would be in future
  1253. * supported, this must be changed and handled properly!
  1254. */
  1255. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", local_node_pos);
  1256. icmap_set_ro_access(tmp_key, 0, 1);
  1257. icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
  1258. }
  1259. put_nodelist_members_to_config(totem_config, 0);
  1260. }
  1261. /*
  1262. * Get things that might change in the future (and can depend on totem_config->interfaces);
  1263. */
  1264. totem_volatile_config_read(totem_config, NULL);
  1265. calc_knet_ping_timers(totem_config);
  1266. icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
  1267. add_totem_config_notification(totem_config);
  1268. return 0;
  1269. }
  1270. int totem_config_validate (
  1271. struct totem_config *totem_config,
  1272. const char **error_string)
  1273. {
  1274. static char local_error_reason[512];
  1275. char parse_error[512];
  1276. const char *error_reason = local_error_reason;
  1277. int i,j;
  1278. uint32_t u32;
  1279. int num_configured = 0;
  1280. unsigned int interface_max = INTERFACE_MAX;
  1281. for (i = 0; i < INTERFACE_MAX; i++) {
  1282. if (totem_config->interfaces[i].configured) {
  1283. num_configured++;
  1284. }
  1285. }
  1286. if (num_configured == 0) {
  1287. error_reason = "No interfaces defined";
  1288. goto parse_error;
  1289. }
  1290. /* Check we found a local node address */
  1291. if (icmap_get_uint32("nodelist.local_node_pos", &u32) != CS_OK) {
  1292. error_reason = "No valid address found for local host";
  1293. goto parse_error;
  1294. }
  1295. for (i = 0; i < INTERFACE_MAX; i++) {
  1296. /*
  1297. * Some error checking of parsed data to make sure its valid
  1298. */
  1299. struct totem_ip_address null_addr;
  1300. if (!totem_config->interfaces[i].configured) {
  1301. continue;
  1302. }
  1303. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  1304. if ((totem_config->transport_number == TOTEM_TRANSPORT_UDP) &&
  1305. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  1306. sizeof (struct totem_ip_address)) == 0) {
  1307. error_reason = "No multicast address specified";
  1308. goto parse_error;
  1309. }
  1310. if (totem_config->interfaces[i].ip_port == 0) {
  1311. error_reason = "No multicast port specified";
  1312. goto parse_error;
  1313. }
  1314. if (totem_config->interfaces[i].ttl > 255) {
  1315. error_reason = "Invalid TTL (should be 0..255)";
  1316. goto parse_error;
  1317. }
  1318. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  1319. totem_config->interfaces[i].ttl != 1) {
  1320. error_reason = "Can only set ttl on multicast transport types";
  1321. goto parse_error;
  1322. }
  1323. if (totem_config->interfaces[i].knet_link_priority > 255) {
  1324. error_reason = "Invalid link priority (should be 0..255)";
  1325. goto parse_error;
  1326. }
  1327. if (totem_config->transport_number != TOTEM_TRANSPORT_KNET &&
  1328. totem_config->interfaces[i].knet_link_priority != 1) {
  1329. error_reason = "Can only set link priority on knet transport type";
  1330. goto parse_error;
  1331. }
  1332. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  1333. totem_config->node_id == 0) {
  1334. error_reason = "An IPV6 network requires that a node ID be specified.";
  1335. goto parse_error;
  1336. }
  1337. if (totem_config->broadcast_use == 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
  1338. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  1339. error_reason = "Multicast address family does not match bind address family";
  1340. goto parse_error;
  1341. }
  1342. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  1343. error_reason = "mcastaddr is not a correct multicast address.";
  1344. goto parse_error;
  1345. }
  1346. }
  1347. /* Verify that all nodes on the same knet link have the same IP family */
  1348. for (j=1; j<totem_config->interfaces[i].member_count; j++) {
  1349. if (totem_config->interfaces[i].configured) {
  1350. if (totem_config->interfaces[i].member_list[j].family !=
  1351. totem_config->interfaces[i].member_list[0].family) {
  1352. snprintf (local_error_reason, sizeof(local_error_reason),
  1353. "Nodes for link %d have different IP families", i);
  1354. goto parse_error;
  1355. }
  1356. }
  1357. }
  1358. }
  1359. if (totem_config->version != 2) {
  1360. error_reason = "This totem parser can only parse version 2 configurations.";
  1361. goto parse_error;
  1362. }
  1363. if (totem_volatile_config_validate(totem_config, error_string) == -1) {
  1364. return (-1);
  1365. }
  1366. if (check_for_duplicate_nodeids(totem_config, error_string) == -1) {
  1367. return (-1);
  1368. }
  1369. /*
  1370. * KNET Link values validation
  1371. */
  1372. if (strcmp (totem_config->link_mode, "active") &&
  1373. strcmp (totem_config->link_mode, "rr") &&
  1374. strcmp (totem_config->link_mode, "passive")) {
  1375. snprintf (local_error_reason, sizeof(local_error_reason),
  1376. "The Knet link mode \"%s\" specified is invalid. It must be active, passive or rr.\n", totem_config->link_mode);
  1377. goto parse_error;
  1378. }
  1379. /* Only Knet does multiple interfaces */
  1380. if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
  1381. interface_max = 1;
  1382. }
  1383. if (interface_max < num_configured) {
  1384. snprintf (parse_error, sizeof(parse_error),
  1385. "%d is too many configured interfaces for non-Knet transport.",
  1386. num_configured);
  1387. error_reason = parse_error;
  1388. goto parse_error;
  1389. }
  1390. /* Only knet allows crypto */
  1391. if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
  1392. if ((strcmp(totem_config->crypto_cipher_type, "none") != 0) ||
  1393. (strcmp(totem_config->crypto_hash_type, "none") != 0)) {
  1394. snprintf (parse_error, sizeof(parse_error),
  1395. "crypto_cipher & crypto_hash are only valid for the Knet transport.");
  1396. error_reason = parse_error;
  1397. goto parse_error;
  1398. }
  1399. }
  1400. if (totem_config->net_mtu == 0) {
  1401. if (totem_config->transport_number == TOTEM_TRANSPORT_KNET) {
  1402. totem_config->net_mtu = KNET_MAX_PACKET_SIZE;
  1403. }
  1404. else {
  1405. totem_config->net_mtu = 1500;
  1406. }
  1407. }
  1408. return 0;
  1409. parse_error:
  1410. snprintf (error_string_response, sizeof(error_string_response),
  1411. "parse error in config: %s\n", error_reason);
  1412. *error_string = error_string_response;
  1413. return (-1);
  1414. }
  1415. static int read_keyfile (
  1416. const char *key_location,
  1417. struct totem_config *totem_config,
  1418. const char **error_string)
  1419. {
  1420. int fd;
  1421. int res;
  1422. int saved_errno;
  1423. char error_str[100];
  1424. const char *error_ptr;
  1425. fd = open (key_location, O_RDONLY);
  1426. if (fd == -1) {
  1427. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  1428. snprintf (error_string_response, sizeof(error_string_response),
  1429. "Could not open %s: %s\n",
  1430. key_location, error_ptr);
  1431. goto parse_error;
  1432. }
  1433. res = read (fd, totem_config->private_key, TOTEM_PRIVATE_KEY_LEN_MAX);
  1434. saved_errno = errno;
  1435. close (fd);
  1436. if (res == -1) {
  1437. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  1438. snprintf (error_string_response, sizeof(error_string_response),
  1439. "Could not read %s: %s\n",
  1440. key_location, error_ptr);
  1441. goto parse_error;
  1442. }
  1443. if (res < TOTEM_PRIVATE_KEY_LEN_MIN) {
  1444. snprintf (error_string_response, sizeof(error_string_response),
  1445. "Could only read %d bits of minimum %u bits from %s.\n",
  1446. res * 8, TOTEM_PRIVATE_KEY_LEN_MIN * 8, key_location);
  1447. goto parse_error;
  1448. }
  1449. totem_config->private_key_len = res;
  1450. return 0;
  1451. parse_error:
  1452. *error_string = error_string_response;
  1453. return (-1);
  1454. }
  1455. int totem_config_keyread (
  1456. struct totem_config *totem_config,
  1457. const char **error_string)
  1458. {
  1459. int got_key = 0;
  1460. char *key_location = NULL;
  1461. int res;
  1462. size_t key_len;
  1463. memset (totem_config->private_key, 0, sizeof(totem_config->private_key));
  1464. totem_config->private_key_len = 0;
  1465. if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
  1466. strcmp(totem_config->crypto_hash_type, "none") == 0) {
  1467. return (0);
  1468. }
  1469. /* cmap may store the location of the key file */
  1470. if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
  1471. res = read_keyfile(key_location, totem_config, error_string);
  1472. free(key_location);
  1473. if (res) {
  1474. goto key_error;
  1475. }
  1476. got_key = 1;
  1477. } else { /* Or the key itself may be in the cmap */
  1478. if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
  1479. if (key_len > sizeof(totem_config->private_key)) {
  1480. sprintf(error_string_response, "key is too long");
  1481. goto key_error;
  1482. }
  1483. if (key_len < TOTEM_PRIVATE_KEY_LEN_MIN) {
  1484. sprintf(error_string_response, "key is too short");
  1485. goto key_error;
  1486. }
  1487. if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
  1488. totem_config->private_key_len = key_len;
  1489. got_key = 1;
  1490. } else {
  1491. sprintf(error_string_response, "can't load private key");
  1492. goto key_error;
  1493. }
  1494. }
  1495. }
  1496. /* In desperation we read the default filename */
  1497. if (!got_key) {
  1498. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  1499. if (!filename)
  1500. filename = COROSYSCONFDIR "/authkey";
  1501. res = read_keyfile(filename, totem_config, error_string);
  1502. if (res)
  1503. goto key_error;
  1504. }
  1505. return (0);
  1506. key_error:
  1507. *error_string = error_string_response;
  1508. return (-1);
  1509. }
  1510. static void debug_dump_totem_config(const struct totem_config *totem_config)
  1511. {
  1512. log_printf(LOGSYS_LEVEL_DEBUG, "Token Timeout (%d ms) retransmit timeout (%d ms)",
  1513. totem_config->token_timeout, totem_config->token_retransmit_timeout);
  1514. log_printf(LOGSYS_LEVEL_DEBUG, "token hold (%d ms) retransmits before loss (%d retrans)",
  1515. totem_config->token_hold_timeout, totem_config->token_retransmits_before_loss_const);
  1516. log_printf(LOGSYS_LEVEL_DEBUG, "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)",
  1517. totem_config->join_timeout, totem_config->send_join_timeout, totem_config->consensus_timeout,
  1518. totem_config->merge_timeout);
  1519. log_printf(LOGSYS_LEVEL_DEBUG, "downcheck (%d ms) fail to recv const (%d msgs)",
  1520. totem_config->downcheck_timeout, totem_config->fail_to_recv_const);
  1521. log_printf(LOGSYS_LEVEL_DEBUG,
  1522. "seqno unchanged const (%d rotations) Maximum network MTU %d",
  1523. totem_config->seqno_unchanged_const, totem_config->net_mtu);
  1524. log_printf(LOGSYS_LEVEL_DEBUG,
  1525. "window size per rotation (%d messages) maximum messages per rotation (%d messages)",
  1526. totem_config->window_size, totem_config->max_messages);
  1527. log_printf(LOGSYS_LEVEL_DEBUG, "missed count const (%d messages)", totem_config->miss_count_const);
  1528. log_printf(LOGSYS_LEVEL_DEBUG, "heartbeat_failures_allowed (%d)",
  1529. totem_config->heartbeat_failures_allowed);
  1530. log_printf(LOGSYS_LEVEL_DEBUG, "max_network_delay (%d ms)", totem_config->max_network_delay);
  1531. }
  1532. static void totem_change_notify(
  1533. int32_t event,
  1534. const char *key_name,
  1535. struct icmap_notify_value new_val,
  1536. struct icmap_notify_value old_val,
  1537. void *user_data)
  1538. {
  1539. struct totem_config *totem_config = (struct totem_config *)user_data;
  1540. uint32_t *param;
  1541. uint8_t reloading;
  1542. const char *deleted_key = NULL;
  1543. const char *error_string;
  1544. /*
  1545. * If a full reload is in progress then don't do anything until it's done and
  1546. * can reconfigure it all atomically
  1547. */
  1548. if (icmap_get_uint8("config.reload_in_progress", &reloading) == CS_OK && reloading)
  1549. return;
  1550. param = totem_get_param_by_name((struct totem_config *)user_data, key_name);
  1551. /*
  1552. * Process change only if changed key is found in totem_config (-> param is not NULL)
  1553. * or for special key token_coefficient. token_coefficient key is not stored in
  1554. * totem_config, but it is used for computation of token timeout.
  1555. */
  1556. if (!param && strcmp(key_name, "totem.token_coefficient") != 0)
  1557. return;
  1558. /*
  1559. * Values other than UINT32 are not supported, or needed (yet)
  1560. */
  1561. switch (event) {
  1562. case ICMAP_TRACK_DELETE:
  1563. deleted_key = key_name;
  1564. break;
  1565. case ICMAP_TRACK_ADD:
  1566. case ICMAP_TRACK_MODIFY:
  1567. deleted_key = NULL;
  1568. break;
  1569. default:
  1570. break;
  1571. }
  1572. totem_volatile_config_read (totem_config, deleted_key);
  1573. log_printf(LOGSYS_LEVEL_DEBUG, "Totem related config key changed. Dumping actual totem config.");
  1574. debug_dump_totem_config(totem_config);
  1575. if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
  1576. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1577. /*
  1578. * TODO: Consider corosync exit and/or load defaults for volatile
  1579. * values. For now, log error seems to be enough
  1580. */
  1581. }
  1582. }
  1583. static void totem_reload_notify(
  1584. int32_t event,
  1585. const char *key_name,
  1586. struct icmap_notify_value new_val,
  1587. struct icmap_notify_value old_val,
  1588. void *user_data)
  1589. {
  1590. struct totem_config *totem_config = (struct totem_config *)user_data;
  1591. uint32_t local_node_pos;
  1592. const char *error_string;
  1593. uint64_t warnings;
  1594. /* Reload has completed */
  1595. if (*(uint8_t *)new_val.data == 0) {
  1596. totem_config->orig_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  1597. assert(totem_config->orig_interfaces != NULL);
  1598. memcpy(totem_config->orig_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
  1599. get_interface_params(totem_config, &error_string, &warnings, 1);
  1600. put_nodelist_members_to_config (totem_config, 1);
  1601. totem_volatile_config_read (totem_config, NULL);
  1602. calc_knet_ping_timers(totem_config);
  1603. log_printf(LOGSYS_LEVEL_DEBUG, "Configuration reloaded. Dumping actual totem config.");
  1604. debug_dump_totem_config(totem_config);
  1605. if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
  1606. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1607. /*
  1608. * TODO: Consider corosync exit and/or load defaults for volatile
  1609. * values. For now, log error seems to be enough
  1610. */
  1611. }
  1612. /* Reinstate the local_node_pos */
  1613. local_node_pos = find_local_node_in_nodelist(totem_config);
  1614. if (local_node_pos != -1) {
  1615. icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
  1616. }
  1617. /* Reconfigure network params as appropriate */
  1618. totempg_reconfigure();
  1619. free(totem_config->orig_interfaces);
  1620. icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
  1621. } else {
  1622. icmap_set_uint8("config.totemconfig_reload_in_progress", 1);
  1623. }
  1624. }
  1625. static void add_totem_config_notification(struct totem_config *totem_config)
  1626. {
  1627. icmap_track_t icmap_track;
  1628. icmap_track_add("totem.",
  1629. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  1630. totem_change_notify,
  1631. totem_config,
  1632. &icmap_track);
  1633. icmap_track_add("config.reload_in_progress",
  1634. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  1635. totem_reload_notify,
  1636. totem_config,
  1637. &icmap_track);
  1638. icmap_track_add("nodelist.node.",
  1639. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  1640. nodelist_dynamic_notify,
  1641. (void *)totem_config,
  1642. &icmap_track);
  1643. }