4
0

totemrrp.c 43 KB

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