totemrrp.c 43 KB

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