totemrrp.c 45 KB

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