totemconfig.c 66 KB

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