totemrrp.c 43 KB

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