totemrrp.c 44 KB

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