totemrrp.c 43 KB

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