totemconfig.c 67 KB

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