totemrrp.c 43 KB

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