totemrrp.c 43 KB

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