totemrrp.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <assert.h>
  37. #include <pthread.h>
  38. #include <sys/mman.h>
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <sys/socket.h>
  42. #include <netdb.h>
  43. #include <sys/un.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/param.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <unistd.h>
  49. #include <fcntl.h>
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <errno.h>
  53. #include <sched.h>
  54. #include <time.h>
  55. #include <sys/time.h>
  56. #include <sys/poll.h>
  57. #include <limits.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/list.h>
  60. #include <corosync/hdb.h>
  61. #include <corosync/swab.h>
  62. #include <corosync/totem/coropoll.h>
  63. #define LOGSYS_UTILS_ONLY 1
  64. #include <corosync/engine/logsys.h>
  65. #include "totemnet.h"
  66. #include "totemrrp.h"
  67. void rrp_deliver_fn (
  68. void *context,
  69. const void *msg,
  70. unsigned int msg_len);
  71. void rrp_iface_change_fn (
  72. void *context,
  73. const struct totem_ip_address *iface_addr);
  74. struct totemrrp_instance;
  75. struct passive_instance {
  76. struct totemrrp_instance *rrp_instance;
  77. unsigned int *faulty;
  78. unsigned int *token_recv_count;
  79. unsigned int *mcast_recv_count;
  80. unsigned char token[15000];
  81. unsigned int token_len;
  82. poll_timer_handle timer_expired_token;
  83. poll_timer_handle timer_problem_decrementer;
  84. void *totemrrp_context;
  85. unsigned int token_xmit_iface;
  86. unsigned int msg_xmit_iface;
  87. };
  88. struct active_instance {
  89. struct totemrrp_instance *rrp_instance;
  90. unsigned int *faulty;
  91. unsigned int *last_token_recv;
  92. unsigned int *counter_problems;
  93. unsigned char token[15000];
  94. unsigned int token_len;
  95. unsigned int last_token_seq;
  96. poll_timer_handle timer_expired_token;
  97. poll_timer_handle timer_problem_decrementer;
  98. void *totemrrp_context;
  99. };
  100. struct rrp_algo {
  101. const char *name;
  102. void * (*initialize) (
  103. struct totemrrp_instance *rrp_instance,
  104. int interface_count);
  105. void (*mcast_recv) (
  106. struct totemrrp_instance *instance,
  107. unsigned int iface_no,
  108. void *context,
  109. const void *msg,
  110. unsigned int msg_len);
  111. void (*mcast_noflush_send) (
  112. struct totemrrp_instance *instance,
  113. const void *msg,
  114. unsigned int msg_len);
  115. void (*mcast_flush_send) (
  116. struct totemrrp_instance *instance,
  117. const void *msg,
  118. unsigned int msg_len);
  119. void (*token_recv) (
  120. struct totemrrp_instance *instance,
  121. unsigned int iface_no,
  122. void *context,
  123. const void *msg,
  124. unsigned int msg_len,
  125. unsigned int token_seqid);
  126. void (*token_send) (
  127. struct totemrrp_instance *instance,
  128. const void *msg,
  129. unsigned int msg_len);
  130. void (*recv_flush) (
  131. struct totemrrp_instance *instance);
  132. void (*send_flush) (
  133. struct totemrrp_instance *instance);
  134. void (*iface_check) (
  135. struct totemrrp_instance *instance);
  136. void (*processor_count_set) (
  137. struct totemrrp_instance *instance,
  138. unsigned int processor_count);
  139. void (*token_target_set) (
  140. struct totemrrp_instance *instance,
  141. struct totem_ip_address *token_target,
  142. unsigned int iface_no);
  143. void (*ring_reenable) (
  144. struct totemrrp_instance *instance);
  145. };
  146. struct totemrrp_instance {
  147. hdb_handle_t totemrrp_poll_handle;
  148. struct totem_interface *interfaces;
  149. struct rrp_algo *rrp_algo;
  150. void *context;
  151. char *status[INTERFACE_MAX];
  152. void (*totemrrp_deliver_fn) (
  153. void *context,
  154. const void *msg,
  155. unsigned int msg_len);
  156. void (*totemrrp_iface_change_fn) (
  157. void *context,
  158. const struct totem_ip_address *iface_addr,
  159. unsigned int iface_no);
  160. void (*totemrrp_token_seqid_get) (
  161. const void *msg,
  162. unsigned int *seqid,
  163. unsigned int *token_is);
  164. unsigned int (*totemrrp_msgs_missing) (void);
  165. /*
  166. * Function and data used to log messages
  167. */
  168. int totemrrp_log_level_security;
  169. int totemrrp_log_level_error;
  170. int totemrrp_log_level_warning;
  171. int totemrrp_log_level_notice;
  172. int totemrrp_log_level_debug;
  173. int totemrrp_subsys_id;
  174. void (*totemrrp_log_printf) (
  175. unsigned int rec_ident,
  176. const char *function,
  177. const char *file,
  178. int line,
  179. const char *format, ...)__attribute__((format(printf, 5, 6)));
  180. hdb_handle_t handle;
  181. hdb_handle_t *net_handles;
  182. void *rrp_algo_instance;
  183. int interface_count;
  184. hdb_handle_t poll_handle;
  185. int processor_count;
  186. struct totem_config *totem_config;
  187. };
  188. /*
  189. * None Replication Forward Declerations
  190. */
  191. static void none_mcast_recv (
  192. struct totemrrp_instance *instance,
  193. unsigned int iface_no,
  194. void *context,
  195. const void *msg,
  196. unsigned int msg_len);
  197. static void none_mcast_noflush_send (
  198. struct totemrrp_instance *instance,
  199. const void *msg,
  200. unsigned int msg_len);
  201. static void none_mcast_flush_send (
  202. struct totemrrp_instance *instance,
  203. const void *msg,
  204. unsigned int msg_len);
  205. static void none_token_recv (
  206. struct totemrrp_instance *instance,
  207. unsigned int iface_no,
  208. void *context,
  209. const void *msg,
  210. unsigned int msg_len,
  211. unsigned int token_seqid);
  212. static void none_token_send (
  213. struct totemrrp_instance *instance,
  214. const void *msg,
  215. unsigned int msg_len);
  216. static void none_recv_flush (
  217. struct totemrrp_instance *instance);
  218. static void none_send_flush (
  219. struct totemrrp_instance *instance);
  220. static void none_iface_check (
  221. struct totemrrp_instance *instance);
  222. static void none_processor_count_set (
  223. struct totemrrp_instance *instance,
  224. unsigned int processor_count_set);
  225. static void none_token_target_set (
  226. struct totemrrp_instance *instance,
  227. struct totem_ip_address *token_target,
  228. unsigned int iface_no);
  229. static void none_ring_reenable (
  230. struct totemrrp_instance *instance);
  231. /*
  232. * Passive Replication Forward Declerations
  233. */
  234. static void *passive_instance_initialize (
  235. struct totemrrp_instance *rrp_instance,
  236. int interface_count);
  237. static void passive_mcast_recv (
  238. struct totemrrp_instance *instance,
  239. unsigned int iface_no,
  240. void *context,
  241. const void *msg,
  242. unsigned int msg_len);
  243. static void passive_mcast_noflush_send (
  244. struct totemrrp_instance *instance,
  245. const void *msg,
  246. unsigned int msg_len);
  247. static void passive_mcast_flush_send (
  248. struct totemrrp_instance *instance,
  249. const void *msg,
  250. unsigned int msg_len);
  251. static void passive_token_recv (
  252. struct totemrrp_instance *instance,
  253. unsigned int iface_no,
  254. void *context,
  255. const void *msg,
  256. unsigned int msg_len,
  257. unsigned int token_seqid);
  258. static void passive_token_send (
  259. struct totemrrp_instance *instance,
  260. const void *msg,
  261. unsigned int msg_len);
  262. static void passive_recv_flush (
  263. struct totemrrp_instance *instance);
  264. static void passive_send_flush (
  265. struct totemrrp_instance *instance);
  266. static void passive_iface_check (
  267. struct totemrrp_instance *instance);
  268. static void passive_processor_count_set (
  269. struct totemrrp_instance *instance,
  270. unsigned int processor_count_set);
  271. static void passive_token_target_set (
  272. struct totemrrp_instance *instance,
  273. struct totem_ip_address *token_target,
  274. unsigned int iface_no);
  275. static void passive_ring_reenable (
  276. struct totemrrp_instance *instance);
  277. /*
  278. * Active Replication Forward Definitions
  279. */
  280. static void *active_instance_initialize (
  281. struct totemrrp_instance *rrp_instance,
  282. int interface_count);
  283. static void active_mcast_recv (
  284. struct totemrrp_instance *instance,
  285. unsigned int iface_no,
  286. void *context,
  287. const void *msg,
  288. unsigned int msg_len);
  289. static void active_mcast_noflush_send (
  290. struct totemrrp_instance *instance,
  291. const void *msg,
  292. unsigned int msg_len);
  293. static void active_mcast_flush_send (
  294. struct totemrrp_instance *instance,
  295. const void *msg,
  296. unsigned int msg_len);
  297. static void active_token_recv (
  298. struct totemrrp_instance *instance,
  299. unsigned int iface_no,
  300. void *context,
  301. const void *msg,
  302. unsigned int msg_len,
  303. unsigned int token_seqid);
  304. static void active_token_send (
  305. struct totemrrp_instance *instance,
  306. const void *msg,
  307. unsigned int msg_len);
  308. static void active_recv_flush (
  309. struct totemrrp_instance *instance);
  310. static void active_send_flush (
  311. struct totemrrp_instance *instance);
  312. static void active_iface_check (
  313. struct totemrrp_instance *instance);
  314. static void active_processor_count_set (
  315. struct totemrrp_instance *instance,
  316. unsigned int processor_count_set);
  317. static void active_token_target_set (
  318. struct totemrrp_instance *instance,
  319. struct totem_ip_address *token_target,
  320. unsigned int iface_no);
  321. static void active_ring_reenable (
  322. struct totemrrp_instance *instance);
  323. static void active_timer_expired_token_start (
  324. struct active_instance *active_instance);
  325. static void active_timer_expired_token_cancel (
  326. struct active_instance *active_instance);
  327. static void active_timer_problem_decrementer_start (
  328. struct active_instance *active_instance);
  329. static void active_timer_problem_decrementer_cancel (
  330. struct active_instance *active_instance);
  331. struct rrp_algo none_algo = {
  332. .name = "none",
  333. .initialize = NULL,
  334. .mcast_recv = none_mcast_recv,
  335. .mcast_noflush_send = none_mcast_noflush_send,
  336. .mcast_flush_send = none_mcast_flush_send,
  337. .token_recv = none_token_recv,
  338. .token_send = none_token_send,
  339. .recv_flush = none_recv_flush,
  340. .send_flush = none_send_flush,
  341. .iface_check = none_iface_check,
  342. .processor_count_set = none_processor_count_set,
  343. .token_target_set = none_token_target_set,
  344. .ring_reenable = none_ring_reenable
  345. };
  346. struct rrp_algo passive_algo = {
  347. .name = "passive",
  348. .initialize = passive_instance_initialize,
  349. .mcast_recv = passive_mcast_recv,
  350. .mcast_noflush_send = passive_mcast_noflush_send,
  351. .mcast_flush_send = passive_mcast_flush_send,
  352. .token_recv = passive_token_recv,
  353. .token_send = passive_token_send,
  354. .recv_flush = passive_recv_flush,
  355. .send_flush = passive_send_flush,
  356. .iface_check = passive_iface_check,
  357. .processor_count_set = passive_processor_count_set,
  358. .token_target_set = passive_token_target_set,
  359. .ring_reenable = passive_ring_reenable
  360. };
  361. struct rrp_algo active_algo = {
  362. .name = "active",
  363. .initialize = active_instance_initialize,
  364. .mcast_recv = active_mcast_recv,
  365. .mcast_noflush_send = active_mcast_noflush_send,
  366. .mcast_flush_send = active_mcast_flush_send,
  367. .token_recv = active_token_recv,
  368. .token_send = active_token_send,
  369. .recv_flush = active_recv_flush,
  370. .send_flush = active_send_flush,
  371. .iface_check = active_iface_check,
  372. .processor_count_set = active_processor_count_set,
  373. .token_target_set = active_token_target_set,
  374. .ring_reenable = active_ring_reenable
  375. };
  376. struct rrp_algo *rrp_algos[] = {
  377. &none_algo,
  378. &passive_algo,
  379. &active_algo
  380. };
  381. #define RRP_ALGOS_COUNT 3
  382. /*
  383. * All instances in one database
  384. */
  385. DECLARE_HDB_DATABASE (totemrrp_instance_database,NULL);
  386. #define log_printf(level, format, args...) \
  387. do { \
  388. rrp_instance->totemrrp_log_printf ( \
  389. LOGSYS_ENCODE_RECID(level, \
  390. rrp_instance->totemrrp_subsys_id, \
  391. LOGSYS_RECID_LOG), \
  392. __FUNCTION__, __FILE__, __LINE__, \
  393. format, ##args); \
  394. } while (0);
  395. /*
  396. * None Replication Implementation
  397. */
  398. static void none_mcast_recv (
  399. struct totemrrp_instance *rrp_instance,
  400. unsigned int iface_no,
  401. void *context,
  402. const void *msg,
  403. unsigned int msg_len)
  404. {
  405. rrp_instance->totemrrp_deliver_fn (
  406. context,
  407. msg,
  408. msg_len);
  409. }
  410. static void none_mcast_flush_send (
  411. struct totemrrp_instance *instance,
  412. const void *msg,
  413. unsigned int msg_len)
  414. {
  415. totemnet_mcast_flush_send (instance->net_handles[0], msg, msg_len);
  416. }
  417. static void none_mcast_noflush_send (
  418. struct totemrrp_instance *instance,
  419. const void *msg,
  420. unsigned int msg_len)
  421. {
  422. totemnet_mcast_noflush_send (instance->net_handles[0], msg, msg_len);
  423. }
  424. static void none_token_recv (
  425. struct totemrrp_instance *rrp_instance,
  426. unsigned int iface_no,
  427. void *context,
  428. const void *msg,
  429. unsigned int msg_len,
  430. unsigned int token_seq)
  431. {
  432. rrp_instance->totemrrp_deliver_fn (
  433. context,
  434. msg,
  435. msg_len);
  436. }
  437. static void none_token_send (
  438. struct totemrrp_instance *instance,
  439. const void *msg,
  440. unsigned int msg_len)
  441. {
  442. totemnet_token_send (
  443. instance->net_handles[0],
  444. msg, msg_len);
  445. }
  446. static void none_recv_flush (struct totemrrp_instance *instance)
  447. {
  448. totemnet_recv_flush (instance->net_handles[0]);
  449. }
  450. static void none_send_flush (struct totemrrp_instance *instance)
  451. {
  452. totemnet_send_flush (instance->net_handles[0]);
  453. }
  454. static void none_iface_check (struct totemrrp_instance *instance)
  455. {
  456. totemnet_iface_check (instance->net_handles[0]);
  457. }
  458. static void none_processor_count_set (
  459. struct totemrrp_instance *instance,
  460. unsigned int processor_count)
  461. {
  462. totemnet_processor_count_set (instance->net_handles[0],
  463. processor_count);
  464. }
  465. static void none_token_target_set (
  466. struct totemrrp_instance *instance,
  467. struct totem_ip_address *token_target,
  468. unsigned int iface_no)
  469. {
  470. totemnet_token_target_set (instance->net_handles[0], token_target);
  471. }
  472. static void none_ring_reenable (
  473. struct totemrrp_instance *instance)
  474. {
  475. /*
  476. * No operation
  477. */
  478. }
  479. /*
  480. * Passive Replication Implementation
  481. */
  482. void *passive_instance_initialize (
  483. struct totemrrp_instance *rrp_instance,
  484. int interface_count)
  485. {
  486. struct passive_instance *instance;
  487. instance = malloc (sizeof (struct passive_instance));
  488. if (instance == 0) {
  489. goto error_exit;
  490. }
  491. memset (instance, 0, sizeof (struct passive_instance));
  492. instance->faulty = malloc (sizeof (int) * interface_count);
  493. if (instance->faulty == 0) {
  494. free (instance);
  495. instance = 0;
  496. goto error_exit;
  497. }
  498. memset (instance->faulty, 0, sizeof (int) * interface_count);
  499. instance->token_recv_count = malloc (sizeof (int) * interface_count);
  500. if (instance->token_recv_count == 0) {
  501. free (instance->faulty);
  502. free (instance);
  503. instance = 0;
  504. goto error_exit;
  505. }
  506. memset (instance->token_recv_count, 0, sizeof (int) * interface_count);
  507. instance->mcast_recv_count = malloc (sizeof (int) * interface_count);
  508. if (instance->mcast_recv_count == 0) {
  509. free (instance->token_recv_count);
  510. free (instance->faulty);
  511. free (instance);
  512. instance = 0;
  513. goto error_exit;
  514. }
  515. memset (instance->mcast_recv_count, 0, sizeof (int) * interface_count);
  516. error_exit:
  517. return ((void *)instance);
  518. }
  519. static void timer_function_passive_token_expired (void *context)
  520. {
  521. struct passive_instance *passive_instance = (struct passive_instance *)context;
  522. struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  523. rrp_instance->totemrrp_deliver_fn (
  524. passive_instance->totemrrp_context,
  525. passive_instance->token,
  526. passive_instance->token_len);
  527. }
  528. /* TODO
  529. static void timer_function_passive_problem_decrementer (void *context)
  530. {
  531. // struct passive_instance *passive_instance = (struct passive_instance *)context;
  532. // struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  533. }
  534. */
  535. static void passive_timer_expired_token_start (
  536. struct passive_instance *passive_instance)
  537. {
  538. poll_timer_add (
  539. passive_instance->rrp_instance->poll_handle,
  540. passive_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  541. (void *)passive_instance,
  542. timer_function_passive_token_expired,
  543. &passive_instance->timer_expired_token);
  544. }
  545. static void passive_timer_expired_token_cancel (
  546. struct passive_instance *passive_instance)
  547. {
  548. poll_timer_delete (
  549. passive_instance->rrp_instance->poll_handle,
  550. passive_instance->timer_expired_token);
  551. }
  552. /*
  553. static void passive_timer_problem_decrementer_start (
  554. struct passive_instance *passive_instance)
  555. {
  556. poll_timer_add (
  557. passive_instance->rrp_instance->poll_handle,
  558. passive_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  559. (void *)passive_instance,
  560. timer_function_passive_problem_decrementer,
  561. &passive_instance->timer_problem_decrementer);
  562. }
  563. static void passive_timer_problem_decrementer_cancel (
  564. struct passive_instance *passive_instance)
  565. {
  566. poll_timer_delete (
  567. passive_instance->rrp_instance->poll_handle,
  568. passive_instance->timer_problem_decrementer);
  569. }
  570. */
  571. static void passive_mcast_recv (
  572. struct totemrrp_instance *rrp_instance,
  573. unsigned int iface_no,
  574. void *context,
  575. const void *msg,
  576. unsigned int msg_len)
  577. {
  578. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  579. unsigned int max;
  580. unsigned int i;
  581. rrp_instance->totemrrp_deliver_fn (
  582. context,
  583. msg,
  584. msg_len);
  585. if (rrp_instance->totemrrp_msgs_missing() == 0 &&
  586. passive_instance->timer_expired_token) {
  587. /*
  588. * Delivers the last token
  589. */
  590. rrp_instance->totemrrp_deliver_fn (
  591. passive_instance->totemrrp_context,
  592. passive_instance->token,
  593. passive_instance->token_len);
  594. passive_timer_expired_token_cancel (passive_instance);
  595. }
  596. /*
  597. * Monitor for failures
  598. * TODO doesn't handle wrap-around of the mcast recv count
  599. */
  600. passive_instance->mcast_recv_count[iface_no] += 1;
  601. max = 0;
  602. for (i = 0; i < rrp_instance->interface_count; i++) {
  603. if (max < passive_instance->mcast_recv_count[i]) {
  604. max = passive_instance->mcast_recv_count[i];
  605. }
  606. }
  607. for (i = 0; i < rrp_instance->interface_count; i++) {
  608. if ((passive_instance->faulty[i] == 0) &&
  609. (max - passive_instance->mcast_recv_count[i] >
  610. rrp_instance->totem_config->rrp_problem_count_threshold)) {
  611. passive_instance->faulty[i] = 1;
  612. sprintf (rrp_instance->status[i],
  613. "Marking ringid %u interface %s FAULTY - adminisrtative intervention required.",
  614. i,
  615. totemnet_iface_print (rrp_instance->net_handles[i]));
  616. log_printf (
  617. rrp_instance->totemrrp_log_level_error,
  618. "%s",
  619. rrp_instance->status[i]);
  620. }
  621. }
  622. }
  623. static void passive_mcast_flush_send (
  624. struct totemrrp_instance *instance,
  625. const void *msg,
  626. unsigned int msg_len)
  627. {
  628. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  629. do {
  630. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  631. } while (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1);
  632. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  633. }
  634. static void passive_mcast_noflush_send (
  635. struct totemrrp_instance *instance,
  636. const void *msg,
  637. unsigned int msg_len)
  638. {
  639. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  640. do {
  641. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  642. } while (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1);
  643. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  644. }
  645. static void passive_token_recv (
  646. struct totemrrp_instance *rrp_instance,
  647. unsigned int iface_no,
  648. void *context,
  649. const void *msg,
  650. unsigned int msg_len,
  651. unsigned int token_seq)
  652. {
  653. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  654. unsigned int max;
  655. unsigned int i;
  656. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  657. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  658. rrp_instance->totemrrp_deliver_fn (
  659. context,
  660. msg,
  661. msg_len);
  662. } else {
  663. memcpy (passive_instance->token, msg, msg_len);
  664. passive_timer_expired_token_start (passive_instance);
  665. }
  666. /*
  667. * Monitor for failures
  668. * TODO doesn't handle wrap-around of the token
  669. */
  670. passive_instance->token_recv_count[iface_no] += 1;
  671. max = 0;
  672. for (i = 0; i < rrp_instance->interface_count; i++) {
  673. if (max < passive_instance->token_recv_count[i]) {
  674. max = passive_instance->token_recv_count[i];
  675. }
  676. }
  677. for (i = 0; i < rrp_instance->interface_count; i++) {
  678. if ((passive_instance->faulty[i] == 0) &&
  679. (max - passive_instance->token_recv_count[i] >
  680. rrp_instance->totem_config->rrp_problem_count_threshold)) {
  681. passive_instance->faulty[i] = 1;
  682. sprintf (rrp_instance->status[i],
  683. "Marking seqid %d ringid %u interface %s FAULTY - adminisrtative intervention required.",
  684. token_seq,
  685. i,
  686. totemnet_iface_print (rrp_instance->net_handles[i]));
  687. log_printf (
  688. rrp_instance->totemrrp_log_level_error,
  689. "%s",
  690. rrp_instance->status[i]);
  691. }
  692. }
  693. }
  694. static void passive_token_send (
  695. struct totemrrp_instance *instance,
  696. const void *msg,
  697. unsigned int msg_len)
  698. {
  699. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  700. do {
  701. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  702. } while (passive_instance->faulty[passive_instance->token_xmit_iface] == 1);
  703. totemnet_token_send (
  704. instance->net_handles[passive_instance->token_xmit_iface],
  705. msg, msg_len);
  706. }
  707. static void passive_recv_flush (struct totemrrp_instance *instance)
  708. {
  709. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  710. unsigned int i;
  711. for (i = 0; i < instance->interface_count; i++) {
  712. if (rrp_algo_instance->faulty[i] == 0) {
  713. totemnet_recv_flush (instance->net_handles[i]);
  714. }
  715. }
  716. }
  717. static void passive_send_flush (struct totemrrp_instance *instance)
  718. {
  719. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  720. unsigned int i;
  721. for (i = 0; i < instance->interface_count; i++) {
  722. if (rrp_algo_instance->faulty[i] == 0) {
  723. totemnet_send_flush (instance->net_handles[i]);
  724. }
  725. }
  726. }
  727. static void passive_iface_check (struct totemrrp_instance *instance)
  728. {
  729. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  730. unsigned int i;
  731. for (i = 0; i < instance->interface_count; i++) {
  732. if (rrp_algo_instance->faulty[i] == 0) {
  733. totemnet_iface_check (instance->net_handles[i]);
  734. }
  735. }
  736. }
  737. static void passive_processor_count_set (
  738. struct totemrrp_instance *instance,
  739. unsigned int processor_count)
  740. {
  741. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  742. unsigned int i;
  743. for (i = 0; i < instance->interface_count; i++) {
  744. if (rrp_algo_instance->faulty[i] == 0) {
  745. totemnet_processor_count_set (instance->net_handles[i],
  746. processor_count);
  747. }
  748. }
  749. }
  750. static void passive_token_target_set (
  751. struct totemrrp_instance *instance,
  752. struct totem_ip_address *token_target,
  753. unsigned int iface_no)
  754. {
  755. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  756. }
  757. static void passive_ring_reenable (
  758. struct totemrrp_instance *instance)
  759. {
  760. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  761. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  762. instance->interface_count);
  763. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  764. instance->interface_count);
  765. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  766. instance->interface_count);
  767. }
  768. /*
  769. * Active Replication Implementation
  770. */
  771. void *active_instance_initialize (
  772. struct totemrrp_instance *rrp_instance,
  773. int interface_count)
  774. {
  775. struct active_instance *instance;
  776. instance = malloc (sizeof (struct active_instance));
  777. if (instance == 0) {
  778. goto error_exit;
  779. }
  780. memset (instance, 0, sizeof (struct active_instance));
  781. instance->faulty = malloc (sizeof (int) * interface_count);
  782. if (instance->faulty == 0) {
  783. free (instance);
  784. instance = 0;
  785. goto error_exit;
  786. }
  787. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  788. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  789. if (instance->last_token_recv == 0) {
  790. free (instance->faulty);
  791. free (instance);
  792. instance = 0;
  793. goto error_exit;
  794. }
  795. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  796. instance->counter_problems = malloc (sizeof (int) * interface_count);
  797. if (instance->counter_problems == 0) {
  798. free (instance->last_token_recv);
  799. free (instance->faulty);
  800. free (instance);
  801. instance = 0;
  802. goto error_exit;
  803. }
  804. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  805. instance->timer_expired_token = 0;
  806. instance->timer_problem_decrementer = 0;
  807. instance->rrp_instance = rrp_instance;
  808. error_exit:
  809. return ((void *)instance);
  810. }
  811. static void timer_function_active_problem_decrementer (void *context)
  812. {
  813. struct active_instance *active_instance = (struct active_instance *)context;
  814. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  815. unsigned int problem_found = 0;
  816. unsigned int i;
  817. for (i = 0; i < rrp_instance->interface_count; i++) {
  818. if (active_instance->counter_problems[i] > 0) {
  819. problem_found = 1;
  820. active_instance->counter_problems[i] -= 1;
  821. if (active_instance->counter_problems[i] == 0) {
  822. sprintf (rrp_instance->status[i],
  823. "ring %d active with no faults", i);
  824. } else {
  825. sprintf (rrp_instance->status[i],
  826. "Decrementing problem counter for iface %s to [%d of %d]",
  827. totemnet_iface_print (rrp_instance->net_handles[i]),
  828. active_instance->counter_problems[i],
  829. rrp_instance->totem_config->rrp_problem_count_threshold);
  830. }
  831. log_printf (
  832. rrp_instance->totemrrp_log_level_warning,
  833. "%s",
  834. rrp_instance->status[i]);
  835. }
  836. }
  837. if (problem_found) {
  838. active_timer_problem_decrementer_start (active_instance);
  839. } else {
  840. active_instance->timer_problem_decrementer = 0;
  841. }
  842. }
  843. static void timer_function_active_token_expired (void *context)
  844. {
  845. struct active_instance *active_instance = (struct active_instance *)context;
  846. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  847. unsigned int i;
  848. for (i = 0; i < rrp_instance->interface_count; i++) {
  849. if (active_instance->last_token_recv[i] == 0) {
  850. active_instance->counter_problems[i] += 1;
  851. if (active_instance->timer_problem_decrementer == 0) {
  852. active_timer_problem_decrementer_start (active_instance);
  853. }
  854. sprintf (rrp_instance->status[i],
  855. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  856. active_instance->last_token_seq,
  857. totemnet_iface_print (rrp_instance->net_handles[i]),
  858. active_instance->counter_problems[i],
  859. rrp_instance->totem_config->rrp_problem_count_threshold);
  860. log_printf (
  861. rrp_instance->totemrrp_log_level_warning,
  862. "%s",
  863. rrp_instance->status[i]);
  864. }
  865. }
  866. for (i = 0; i < rrp_instance->interface_count; i++) {
  867. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold)
  868. {
  869. active_instance->faulty[i] = 1;
  870. sprintf (rrp_instance->status[i],
  871. "Marking seqid %d ringid %u interface %s FAULTY - adminisrtative intervention required.",
  872. active_instance->last_token_seq,
  873. i,
  874. totemnet_iface_print (rrp_instance->net_handles[i]));
  875. log_printf (
  876. rrp_instance->totemrrp_log_level_error,
  877. "%s",
  878. rrp_instance->status[i]);
  879. active_timer_problem_decrementer_cancel (active_instance);
  880. }
  881. }
  882. rrp_instance->totemrrp_deliver_fn (
  883. active_instance->totemrrp_context,
  884. active_instance->token,
  885. active_instance->token_len);
  886. }
  887. static void active_timer_expired_token_start (
  888. struct active_instance *active_instance)
  889. {
  890. poll_timer_add (
  891. active_instance->rrp_instance->poll_handle,
  892. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  893. (void *)active_instance,
  894. timer_function_active_token_expired,
  895. &active_instance->timer_expired_token);
  896. }
  897. static void active_timer_expired_token_cancel (
  898. struct active_instance *active_instance)
  899. {
  900. poll_timer_delete (
  901. active_instance->rrp_instance->poll_handle,
  902. active_instance->timer_expired_token);
  903. }
  904. static void active_timer_problem_decrementer_start (
  905. struct active_instance *active_instance)
  906. {
  907. poll_timer_add (
  908. active_instance->rrp_instance->poll_handle,
  909. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  910. (void *)active_instance,
  911. timer_function_active_problem_decrementer,
  912. &active_instance->timer_problem_decrementer);
  913. }
  914. static void active_timer_problem_decrementer_cancel (
  915. struct active_instance *active_instance)
  916. {
  917. poll_timer_delete (
  918. active_instance->rrp_instance->poll_handle,
  919. active_instance->timer_problem_decrementer);
  920. }
  921. /*
  922. * active replication
  923. */
  924. static void active_mcast_recv (
  925. struct totemrrp_instance *instance,
  926. unsigned int iface_no,
  927. void *context,
  928. const void *msg,
  929. unsigned int msg_len)
  930. {
  931. instance->totemrrp_deliver_fn (
  932. context,
  933. msg,
  934. msg_len);
  935. }
  936. static void active_mcast_flush_send (
  937. struct totemrrp_instance *instance,
  938. const void *msg,
  939. unsigned int msg_len)
  940. {
  941. int i;
  942. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  943. for (i = 0; i < instance->interface_count; i++) {
  944. if (rrp_algo_instance->faulty[i] == 0) {
  945. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  946. }
  947. }
  948. }
  949. static void active_mcast_noflush_send (
  950. struct totemrrp_instance *instance,
  951. const void *msg,
  952. unsigned int msg_len)
  953. {
  954. int i;
  955. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  956. for (i = 0; i < instance->interface_count; i++) {
  957. if (rrp_algo_instance->faulty[i] == 0) {
  958. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  959. }
  960. }
  961. }
  962. static void active_token_recv (
  963. struct totemrrp_instance *instance,
  964. unsigned int iface_no,
  965. void *context,
  966. const void *msg,
  967. unsigned int msg_len,
  968. unsigned int token_seq)
  969. {
  970. int i;
  971. struct active_instance *active_instance = (struct active_instance *)instance->rrp_algo_instance;
  972. active_instance->totemrrp_context = context; // this should be in totemrrp_instance ?
  973. if (token_seq > active_instance->last_token_seq) {
  974. memcpy (active_instance->token, msg, msg_len);
  975. active_instance->token_len = msg_len;
  976. for (i = 0; i < instance->interface_count; i++) {
  977. active_instance->last_token_recv[i] = 0;
  978. }
  979. active_instance->last_token_recv[iface_no] = 1;
  980. active_timer_expired_token_start (active_instance);
  981. }
  982. active_instance->last_token_seq = token_seq;
  983. if (token_seq == active_instance->last_token_seq) {
  984. active_instance->last_token_recv[iface_no] = 1;
  985. for (i = 0; i < instance->interface_count; i++) {
  986. if ((active_instance->last_token_recv[i] == 0) &&
  987. active_instance->faulty[i] == 0) {
  988. return; /* don't deliver token */
  989. }
  990. }
  991. active_timer_expired_token_cancel (active_instance);
  992. instance->totemrrp_deliver_fn (
  993. context,
  994. msg,
  995. msg_len);
  996. }
  997. }
  998. static void active_token_send (
  999. struct totemrrp_instance *instance,
  1000. const void *msg,
  1001. unsigned int msg_len)
  1002. {
  1003. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1004. int i;
  1005. for (i = 0; i < instance->interface_count; i++) {
  1006. if (rrp_algo_instance->faulty[i] == 0) {
  1007. totemnet_token_send (
  1008. instance->net_handles[i],
  1009. msg, msg_len);
  1010. }
  1011. }
  1012. }
  1013. static void active_recv_flush (struct totemrrp_instance *instance)
  1014. {
  1015. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1016. unsigned int i;
  1017. for (i = 0; i < instance->interface_count; i++) {
  1018. if (rrp_algo_instance->faulty[i] == 0) {
  1019. totemnet_recv_flush (instance->net_handles[i]);
  1020. }
  1021. }
  1022. }
  1023. static void active_send_flush (struct totemrrp_instance *instance)
  1024. {
  1025. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1026. unsigned int i;
  1027. for (i = 0; i < instance->interface_count; i++) {
  1028. if (rrp_algo_instance->faulty[i] == 0) {
  1029. totemnet_send_flush (instance->net_handles[i]);
  1030. }
  1031. }
  1032. }
  1033. static void active_iface_check (struct totemrrp_instance *instance)
  1034. {
  1035. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1036. unsigned int i;
  1037. for (i = 0; i < instance->interface_count; i++) {
  1038. if (rrp_algo_instance->faulty[i] == 0) {
  1039. totemnet_iface_check (instance->net_handles[i]);
  1040. }
  1041. }
  1042. }
  1043. static void active_processor_count_set (
  1044. struct totemrrp_instance *instance,
  1045. unsigned int processor_count)
  1046. {
  1047. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1048. unsigned int i;
  1049. for (i = 0; i < instance->interface_count; i++) {
  1050. if (rrp_algo_instance->faulty[i] == 0) {
  1051. totemnet_processor_count_set (instance->net_handles[i],
  1052. processor_count);
  1053. }
  1054. }
  1055. }
  1056. static void active_token_target_set (
  1057. struct totemrrp_instance *instance,
  1058. struct totem_ip_address *token_target,
  1059. unsigned int iface_no)
  1060. {
  1061. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1062. }
  1063. static void active_ring_reenable (
  1064. struct totemrrp_instance *instance)
  1065. {
  1066. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1067. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1068. instance->interface_count);
  1069. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1070. instance->interface_count);
  1071. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1072. instance->interface_count);
  1073. }
  1074. struct deliver_fn_context {
  1075. struct totemrrp_instance *instance;
  1076. void *context;
  1077. int iface_no;
  1078. };
  1079. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1080. {
  1081. memset (instance, 0, sizeof (struct totemrrp_instance));
  1082. }
  1083. static int totemrrp_algorithm_set (
  1084. struct totem_config *totem_config,
  1085. struct totemrrp_instance *instance)
  1086. {
  1087. unsigned int res = -1;
  1088. unsigned int i;
  1089. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1090. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1091. instance->rrp_algo = rrp_algos[i];
  1092. if (rrp_algos[i]->initialize) {
  1093. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1094. instance,
  1095. totem_config->interface_count);
  1096. }
  1097. res = 0;
  1098. break;
  1099. }
  1100. }
  1101. for (i = 0; i < totem_config->interface_count; i++) {
  1102. instance->status[i] = malloc (1024);
  1103. sprintf (instance->status[i], "ring %d active with no faults", i);
  1104. }
  1105. return (res);
  1106. }
  1107. void rrp_deliver_fn (
  1108. void *context,
  1109. const void *msg,
  1110. unsigned int msg_len)
  1111. {
  1112. unsigned int token_seqid;
  1113. unsigned int token_is;
  1114. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1115. deliver_fn_context->instance->totemrrp_token_seqid_get (
  1116. msg,
  1117. &token_seqid,
  1118. &token_is);
  1119. if (token_is) {
  1120. /*
  1121. * Deliver to the token receiver for this rrp algorithm
  1122. */
  1123. deliver_fn_context->instance->rrp_algo->token_recv (
  1124. deliver_fn_context->instance,
  1125. deliver_fn_context->iface_no,
  1126. deliver_fn_context->context,
  1127. msg,
  1128. msg_len,
  1129. token_seqid);
  1130. } else {
  1131. /*
  1132. * Deliver to the mcast receiver for this rrp algorithm
  1133. */
  1134. deliver_fn_context->instance->rrp_algo->mcast_recv (
  1135. deliver_fn_context->instance,
  1136. deliver_fn_context->iface_no,
  1137. deliver_fn_context->context,
  1138. msg,
  1139. msg_len);
  1140. }
  1141. }
  1142. void rrp_iface_change_fn (
  1143. void *context,
  1144. const struct totem_ip_address *iface_addr)
  1145. {
  1146. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1147. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1148. deliver_fn_context->context,
  1149. iface_addr,
  1150. deliver_fn_context->iface_no);
  1151. }
  1152. int totemrrp_finalize (
  1153. hdb_handle_t handle)
  1154. {
  1155. struct totemrrp_instance *instance;
  1156. int res = 0;
  1157. int i;
  1158. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1159. (void *)&instance);
  1160. if (res != 0) {
  1161. res = ENOENT;
  1162. goto error_exit;
  1163. }
  1164. for (i = 0; i < instance->interface_count; i++) {
  1165. totemnet_finalize (instance->net_handles[i]);
  1166. }
  1167. hdb_handle_put (&totemrrp_instance_database, handle);
  1168. error_exit:
  1169. return (res);
  1170. }
  1171. /*
  1172. * Totem Redundant Ring interface
  1173. * depends on poll abstraction, POSIX, IPV4
  1174. */
  1175. /*
  1176. * Create an instance
  1177. */
  1178. int totemrrp_initialize (
  1179. hdb_handle_t poll_handle,
  1180. hdb_handle_t *handle,
  1181. struct totem_config *totem_config,
  1182. void *context,
  1183. void (*deliver_fn) (
  1184. void *context,
  1185. const void *msg,
  1186. unsigned int msg_len),
  1187. void (*iface_change_fn) (
  1188. void *context,
  1189. const struct totem_ip_address *iface_addr,
  1190. unsigned int iface_no),
  1191. void (*token_seqid_get) (
  1192. const void *msg,
  1193. unsigned int *seqid,
  1194. unsigned int *token_is),
  1195. unsigned int (*msgs_missing) (void))
  1196. {
  1197. struct totemrrp_instance *instance;
  1198. unsigned int res;
  1199. int i;
  1200. res = hdb_handle_create (&totemrrp_instance_database,
  1201. sizeof (struct totemrrp_instance), handle);
  1202. if (res != 0) {
  1203. goto error_exit;
  1204. }
  1205. res = hdb_handle_get (&totemrrp_instance_database, *handle,
  1206. (void *)&instance);
  1207. if (res != 0) {
  1208. goto error_destroy;
  1209. }
  1210. totemrrp_instance_initialize (instance);
  1211. instance->totem_config = totem_config;
  1212. res = totemrrp_algorithm_set (
  1213. instance->totem_config,
  1214. instance);
  1215. if (res == -1) {
  1216. goto error_put;
  1217. }
  1218. /*
  1219. * Configure logging
  1220. */
  1221. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1222. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1223. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1224. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1225. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1226. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1227. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1228. instance->interfaces = totem_config->interfaces;
  1229. instance->totemrrp_poll_handle = poll_handle;
  1230. instance->totemrrp_deliver_fn = deliver_fn;
  1231. instance->totemrrp_iface_change_fn = iface_change_fn;
  1232. instance->totemrrp_token_seqid_get = token_seqid_get;
  1233. instance->totemrrp_msgs_missing = msgs_missing;
  1234. instance->interface_count = totem_config->interface_count;
  1235. instance->net_handles = malloc (sizeof (hdb_handle_t) * totem_config->interface_count);
  1236. instance->context = context;
  1237. instance->poll_handle = poll_handle;
  1238. for (i = 0; i < totem_config->interface_count; i++) {
  1239. struct deliver_fn_context *deliver_fn_context;
  1240. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1241. assert (deliver_fn_context);
  1242. deliver_fn_context->instance = instance;
  1243. deliver_fn_context->context = context;
  1244. deliver_fn_context->iface_no = i;
  1245. totemnet_initialize (
  1246. poll_handle,
  1247. &instance->net_handles[i],
  1248. totem_config,
  1249. i,
  1250. (void *)deliver_fn_context,
  1251. rrp_deliver_fn,
  1252. rrp_iface_change_fn);
  1253. }
  1254. totemnet_net_mtu_adjust (totem_config);
  1255. error_exit:
  1256. hdb_handle_put (&totemrrp_instance_database, *handle);
  1257. return (0);
  1258. error_put:
  1259. hdb_handle_put (&totemrrp_instance_database, *handle);
  1260. error_destroy:
  1261. hdb_handle_destroy (&totemrrp_instance_database, *handle);
  1262. return (res);
  1263. }
  1264. int totemrrp_processor_count_set (
  1265. hdb_handle_t handle,
  1266. unsigned int processor_count)
  1267. {
  1268. struct totemrrp_instance *instance;
  1269. int res = 0;
  1270. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1271. (void *)&instance);
  1272. if (res != 0) {
  1273. res = ENOENT;
  1274. goto error_exit;
  1275. }
  1276. instance->rrp_algo->processor_count_set (instance, processor_count);
  1277. instance->processor_count = processor_count;
  1278. hdb_handle_put (&totemrrp_instance_database, handle);
  1279. error_exit:
  1280. return (res);
  1281. }
  1282. int totemrrp_token_target_set (
  1283. hdb_handle_t handle,
  1284. struct totem_ip_address *addr,
  1285. unsigned int iface_no)
  1286. {
  1287. struct totemrrp_instance *instance;
  1288. int res = 0;
  1289. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1290. (void *)&instance);
  1291. if (res != 0) {
  1292. res = ENOENT;
  1293. goto error_exit;
  1294. }
  1295. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1296. hdb_handle_put (&totemrrp_instance_database, handle);
  1297. error_exit:
  1298. return (res);
  1299. }
  1300. int totemrrp_recv_flush (hdb_handle_t handle)
  1301. {
  1302. struct totemrrp_instance *instance;
  1303. int res = 0;
  1304. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1305. (void *)&instance);
  1306. if (res != 0) {
  1307. res = ENOENT;
  1308. goto error_exit;
  1309. }
  1310. instance->rrp_algo->recv_flush (instance);
  1311. hdb_handle_put (&totemrrp_instance_database, handle);
  1312. error_exit:
  1313. return (res);
  1314. }
  1315. int totemrrp_send_flush (hdb_handle_t handle)
  1316. {
  1317. struct totemrrp_instance *instance;
  1318. int res = 0;
  1319. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1320. (void *)&instance);
  1321. if (res != 0) {
  1322. res = ENOENT;
  1323. goto error_exit;
  1324. }
  1325. instance->rrp_algo->send_flush (instance);
  1326. hdb_handle_put (&totemrrp_instance_database, handle);
  1327. error_exit:
  1328. return (res);
  1329. }
  1330. int totemrrp_token_send (
  1331. hdb_handle_t handle,
  1332. const void *msg,
  1333. unsigned int msg_len)
  1334. {
  1335. struct totemrrp_instance *instance;
  1336. int res = 0;
  1337. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1338. (void *)&instance);
  1339. if (res != 0) {
  1340. res = ENOENT;
  1341. goto error_exit;
  1342. }
  1343. instance->rrp_algo->token_send (instance, msg, msg_len);
  1344. hdb_handle_put (&totemrrp_instance_database, handle);
  1345. error_exit:
  1346. return (res);
  1347. }
  1348. int totemrrp_mcast_flush_send (
  1349. hdb_handle_t handle,
  1350. const void *msg,
  1351. unsigned int msg_len)
  1352. {
  1353. struct totemrrp_instance *instance;
  1354. int res = 0;
  1355. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1356. (void *)&instance);
  1357. if (res != 0) {
  1358. res = ENOENT;
  1359. goto error_exit;
  1360. }
  1361. // TODO this needs to return the result
  1362. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1363. hdb_handle_put (&totemrrp_instance_database, handle);
  1364. error_exit:
  1365. return (res);
  1366. }
  1367. int totemrrp_mcast_noflush_send (
  1368. hdb_handle_t handle,
  1369. const void *msg,
  1370. unsigned int msg_len)
  1371. {
  1372. struct totemrrp_instance *instance;
  1373. int res = 0;
  1374. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1375. (void *)&instance);
  1376. if (res != 0) {
  1377. res = ENOENT;
  1378. goto error_exit;
  1379. }
  1380. /*
  1381. * merge detects go out through mcast_flush_send so it is safe to
  1382. * flush these messages if we are only one processor. This avoids
  1383. * an encryption/hmac and decryption/hmac
  1384. */
  1385. if (instance->processor_count > 1) {
  1386. // TODO this needs to return the result
  1387. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1388. }
  1389. hdb_handle_put (&totemrrp_instance_database, handle);
  1390. error_exit:
  1391. return (res);
  1392. }
  1393. int totemrrp_iface_check (hdb_handle_t handle)
  1394. {
  1395. struct totemrrp_instance *instance;
  1396. int res = 0;
  1397. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1398. (void *)&instance);
  1399. if (res != 0) {
  1400. res = ENOENT;
  1401. goto error_exit;
  1402. }
  1403. instance->rrp_algo->iface_check (instance);
  1404. hdb_handle_put (&totemrrp_instance_database, handle);
  1405. error_exit:
  1406. return (res);
  1407. }
  1408. int totemrrp_ifaces_get (
  1409. hdb_handle_t handle,
  1410. char ***status,
  1411. unsigned int *iface_count)
  1412. {
  1413. struct totemrrp_instance *instance;
  1414. int res = 0;
  1415. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1416. (void *)&instance);
  1417. if (res != 0) {
  1418. res = ENOENT;
  1419. goto error_exit;
  1420. }
  1421. *status = instance->status;
  1422. if (iface_count) {
  1423. *iface_count = instance->interface_count;
  1424. }
  1425. hdb_handle_put (&totemrrp_instance_database, handle);
  1426. error_exit:
  1427. return (res);
  1428. }
  1429. int totemrrp_crypto_set (
  1430. hdb_handle_t handle,
  1431. unsigned int type)
  1432. {
  1433. int res;
  1434. struct totemrrp_instance *instance;
  1435. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1436. (void *)&instance);
  1437. if (res != 0) {
  1438. return (0);
  1439. }
  1440. res = totemnet_crypto_set(instance->net_handles[0], type);
  1441. hdb_handle_put (&totemrrp_instance_database, handle);
  1442. return (res);
  1443. }
  1444. int totemrrp_ring_reenable (
  1445. hdb_handle_t handle)
  1446. {
  1447. struct totemrrp_instance *instance;
  1448. int res = 0;
  1449. unsigned int i;
  1450. printf ("totemrrp ring reenable\n");
  1451. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1452. (void *)&instance);
  1453. if (res != 0) {
  1454. res = ENOENT;
  1455. goto error_exit;
  1456. }
  1457. instance->rrp_algo->ring_reenable (instance);
  1458. for (i = 0; i < instance->interface_count; i++) {
  1459. sprintf (instance->status[i], "ring %d active with no faults", i);
  1460. }
  1461. hdb_handle_put (&totemrrp_instance_database, handle);
  1462. error_exit:
  1463. return (res);
  1464. }