totemconfig.c 61 KB

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