totemrrp.c 44 KB

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