totemconfig.c 62 KB

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