4
0

totemconfig.c 77 KB

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