totemrrp.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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 (*recv_flush) (
  131. struct totemrrp_instance *instance);
  132. void (*send_flush) (
  133. struct totemrrp_instance *instance);
  134. void (*iface_check) (
  135. struct totemrrp_instance *instance);
  136. void (*processor_count_set) (
  137. struct totemrrp_instance *instance,
  138. unsigned int processor_count);
  139. void (*token_target_set) (
  140. struct totemrrp_instance *instance,
  141. struct totem_ip_address *token_target,
  142. unsigned int iface_no);
  143. void (*ring_reenable) (
  144. struct totemrrp_instance *instance,
  145. unsigned int iface_no);
  146. int (*mcast_recv_empty) (
  147. struct totemrrp_instance *instance);
  148. int (*member_add) (
  149. struct totemrrp_instance *instance,
  150. const struct totem_ip_address *member,
  151. unsigned int iface_no);
  152. int (*member_remove) (
  153. struct totemrrp_instance *instance,
  154. const struct totem_ip_address *member,
  155. unsigned int iface_no);
  156. void (*membership_changed) (
  157. struct totemrrp_instance *instance,
  158. enum totem_configuration_type configuration_type,
  159. const struct srp_addr *member_list, size_t member_list_entries,
  160. const struct srp_addr *left_list, size_t left_list_entries,
  161. const struct srp_addr *joined_list, size_t joined_list_entries,
  162. const struct memb_ring_id *ring_id);
  163. };
  164. struct totemrrp_instance {
  165. hdb_handle_t poll_handle;
  166. struct totem_interface *interfaces;
  167. struct rrp_algo *rrp_algo;
  168. void *context;
  169. char *status[INTERFACE_MAX];
  170. void (*totemrrp_deliver_fn) (
  171. void *context,
  172. const void *msg,
  173. unsigned int msg_len);
  174. void (*totemrrp_iface_change_fn) (
  175. void *context,
  176. const struct totem_ip_address *iface_addr,
  177. unsigned int iface_no);
  178. void (*totemrrp_token_seqid_get) (
  179. const void *msg,
  180. unsigned int *seqid,
  181. unsigned int *token_is);
  182. void (*totemrrp_target_set_completed) (
  183. void *context);
  184. unsigned int (*totemrrp_msgs_missing) (void);
  185. /*
  186. * Function and data used to log messages
  187. */
  188. int totemrrp_log_level_security;
  189. int totemrrp_log_level_error;
  190. int totemrrp_log_level_warning;
  191. int totemrrp_log_level_notice;
  192. int totemrrp_log_level_debug;
  193. int totemrrp_subsys_id;
  194. void (*totemrrp_log_printf) (
  195. unsigned int rec_ident,
  196. const char *function,
  197. const char *file,
  198. int line,
  199. const char *format, ...)__attribute__((format(printf, 5, 6)));
  200. void **net_handles;
  201. void *rrp_algo_instance;
  202. int interface_count;
  203. int processor_count;
  204. int my_nodeid;
  205. struct totem_config *totem_config;
  206. void *deliver_fn_context[INTERFACE_MAX];
  207. poll_timer_handle timer_active_test_ring_timeout[INTERFACE_MAX];
  208. };
  209. /*
  210. * None Replication Forward Declerations
  211. */
  212. static void none_mcast_recv (
  213. struct totemrrp_instance *instance,
  214. unsigned int iface_no,
  215. void *context,
  216. const void *msg,
  217. unsigned int msg_len);
  218. static void none_mcast_noflush_send (
  219. struct totemrrp_instance *instance,
  220. const void *msg,
  221. unsigned int msg_len);
  222. static void none_mcast_flush_send (
  223. struct totemrrp_instance *instance,
  224. const void *msg,
  225. unsigned int msg_len);
  226. static void none_token_recv (
  227. struct totemrrp_instance *instance,
  228. unsigned int iface_no,
  229. void *context,
  230. const void *msg,
  231. unsigned int msg_len,
  232. unsigned int token_seqid);
  233. static void none_token_send (
  234. struct totemrrp_instance *instance,
  235. const void *msg,
  236. unsigned int msg_len);
  237. static void none_recv_flush (
  238. struct totemrrp_instance *instance);
  239. static void none_send_flush (
  240. struct totemrrp_instance *instance);
  241. static void none_iface_check (
  242. struct totemrrp_instance *instance);
  243. static void none_processor_count_set (
  244. struct totemrrp_instance *instance,
  245. unsigned int processor_count_set);
  246. static void none_token_target_set (
  247. struct totemrrp_instance *instance,
  248. struct totem_ip_address *token_target,
  249. unsigned int iface_no);
  250. static void none_ring_reenable (
  251. struct totemrrp_instance *instance,
  252. unsigned int iface_no);
  253. static int none_mcast_recv_empty (
  254. struct totemrrp_instance *instance);
  255. static int none_member_add (
  256. struct totemrrp_instance *instance,
  257. const struct totem_ip_address *member,
  258. unsigned int iface_no);
  259. static int none_member_remove (
  260. struct totemrrp_instance *instance,
  261. const struct totem_ip_address *member,
  262. unsigned int iface_no);
  263. static void none_membership_changed (
  264. struct totemrrp_instance *instance,
  265. enum totem_configuration_type configuration_type,
  266. const struct srp_addr *member_list, size_t member_list_entries,
  267. const struct srp_addr *left_list, size_t left_list_entries,
  268. const struct srp_addr *joined_list, size_t joined_list_entries,
  269. const struct memb_ring_id *ring_id);
  270. /*
  271. * Passive Replication Forward Declerations
  272. */
  273. static void *passive_instance_initialize (
  274. struct totemrrp_instance *rrp_instance,
  275. int interface_count);
  276. static void passive_mcast_recv (
  277. struct totemrrp_instance *instance,
  278. unsigned int iface_no,
  279. void *context,
  280. const void *msg,
  281. unsigned int msg_len);
  282. static void passive_mcast_noflush_send (
  283. struct totemrrp_instance *instance,
  284. const void *msg,
  285. unsigned int msg_len);
  286. static void passive_mcast_flush_send (
  287. struct totemrrp_instance *instance,
  288. const void *msg,
  289. unsigned int msg_len);
  290. static void passive_monitor (
  291. struct totemrrp_instance *rrp_instance,
  292. unsigned int iface_no,
  293. int is_token_recv_count);
  294. static void passive_token_recv (
  295. struct totemrrp_instance *instance,
  296. unsigned int iface_no,
  297. void *context,
  298. const void *msg,
  299. unsigned int msg_len,
  300. unsigned int token_seqid);
  301. static void passive_token_send (
  302. struct totemrrp_instance *instance,
  303. const void *msg,
  304. unsigned int msg_len);
  305. static void passive_recv_flush (
  306. struct totemrrp_instance *instance);
  307. static void passive_send_flush (
  308. struct totemrrp_instance *instance);
  309. static void passive_iface_check (
  310. struct totemrrp_instance *instance);
  311. static void passive_processor_count_set (
  312. struct totemrrp_instance *instance,
  313. unsigned int processor_count_set);
  314. static void passive_token_target_set (
  315. struct totemrrp_instance *instance,
  316. struct totem_ip_address *token_target,
  317. unsigned int iface_no);
  318. static void passive_ring_reenable (
  319. struct totemrrp_instance *instance,
  320. unsigned int iface_no);
  321. static int passive_mcast_recv_empty (
  322. struct totemrrp_instance *instance);
  323. static int passive_member_add (
  324. struct totemrrp_instance *instance,
  325. const struct totem_ip_address *member,
  326. unsigned int iface_no);
  327. static int passive_member_remove (
  328. struct totemrrp_instance *instance,
  329. const struct totem_ip_address *member,
  330. unsigned int iface_no);
  331. static void passive_membership_changed (
  332. struct totemrrp_instance *instance,
  333. enum totem_configuration_type configuration_type,
  334. const struct srp_addr *member_list, size_t member_list_entries,
  335. const struct srp_addr *left_list, size_t left_list_entries,
  336. const struct srp_addr *joined_list, size_t joined_list_entries,
  337. const struct memb_ring_id *ring_id);
  338. /*
  339. * Active Replication Forward Definitions
  340. */
  341. static void *active_instance_initialize (
  342. struct totemrrp_instance *rrp_instance,
  343. int interface_count);
  344. static void active_mcast_recv (
  345. struct totemrrp_instance *instance,
  346. unsigned int iface_no,
  347. void *context,
  348. const void *msg,
  349. unsigned int msg_len);
  350. static void active_mcast_noflush_send (
  351. struct totemrrp_instance *instance,
  352. const void *msg,
  353. unsigned int msg_len);
  354. static void active_mcast_flush_send (
  355. struct totemrrp_instance *instance,
  356. const void *msg,
  357. unsigned int msg_len);
  358. static void active_token_recv (
  359. struct totemrrp_instance *instance,
  360. unsigned int iface_no,
  361. void *context,
  362. const void *msg,
  363. unsigned int msg_len,
  364. unsigned int token_seqid);
  365. static void active_token_send (
  366. struct totemrrp_instance *instance,
  367. const void *msg,
  368. unsigned int msg_len);
  369. static void active_recv_flush (
  370. struct totemrrp_instance *instance);
  371. static void active_send_flush (
  372. struct totemrrp_instance *instance);
  373. static void active_iface_check (
  374. struct totemrrp_instance *instance);
  375. static void active_processor_count_set (
  376. struct totemrrp_instance *instance,
  377. unsigned int processor_count_set);
  378. static void active_token_target_set (
  379. struct totemrrp_instance *instance,
  380. struct totem_ip_address *token_target,
  381. unsigned int iface_no);
  382. static void active_ring_reenable (
  383. struct totemrrp_instance *instance,
  384. unsigned int iface_no);
  385. static int active_mcast_recv_empty (
  386. struct totemrrp_instance *instance);
  387. static int active_member_add (
  388. struct totemrrp_instance *instance,
  389. const struct totem_ip_address *member,
  390. unsigned int iface_no);
  391. static int active_member_remove (
  392. struct totemrrp_instance *instance,
  393. const struct totem_ip_address *member,
  394. unsigned int iface_no);
  395. static void active_membership_changed (
  396. struct totemrrp_instance *instance,
  397. enum totem_configuration_type configuration_type,
  398. const struct srp_addr *member_list, size_t member_list_entries,
  399. const struct srp_addr *left_list, size_t left_list_entries,
  400. const struct srp_addr *joined_list, size_t joined_list_entries,
  401. const struct memb_ring_id *ring_id);
  402. static void active_timer_expired_token_start (
  403. struct active_instance *active_instance);
  404. static void active_timer_expired_token_cancel (
  405. struct active_instance *active_instance);
  406. static void active_timer_problem_decrementer_start (
  407. struct active_instance *active_instance);
  408. static void active_timer_problem_decrementer_cancel (
  409. struct active_instance *active_instance);
  410. /*
  411. * 0-5 reserved for totemsrp.c
  412. */
  413. #define MESSAGE_TYPE_RING_TEST_ACTIVE 6
  414. #define MESSAGE_TYPE_RING_TEST_ACTIVATE 7
  415. #define ENDIAN_LOCAL 0xff22
  416. /*
  417. * Rollover handling:
  418. *
  419. * ARR_SEQNO_START_TOKEN is the starting sequence number of last seen sequence
  420. * for a token for active redundand ring. This should remain zero, unless testing
  421. * overflow in which case 07fffff00 or 0xffffff00 are good starting values.
  422. * It should be same as on defined in totemsrp.c
  423. */
  424. #define ARR_SEQNO_START_TOKEN 0x0
  425. /*
  426. * These can be used ot test different rollover points
  427. * #define ARR_SEQNO_START_MSG 0xfffffe00
  428. */
  429. /*
  430. * Threshold value when recv_count for passive rrp should be adjusted.
  431. * Set this value to some smaller for testing of adjusting proper
  432. * functionality. Also keep in mind that this value must be smaller
  433. * than rrp_problem_count_threshold
  434. */
  435. #define PASSIVE_RECV_COUNT_THRESHOLD (INT_MAX / 2)
  436. struct message_header {
  437. char type;
  438. char encapsulated;
  439. unsigned short endian_detector;
  440. int ring_number;
  441. int nodeid_activator;
  442. } __attribute__((packed));
  443. struct deliver_fn_context {
  444. struct totemrrp_instance *instance;
  445. void *context;
  446. int iface_no;
  447. };
  448. struct rrp_algo none_algo = {
  449. .name = "none",
  450. .initialize = NULL,
  451. .mcast_recv = none_mcast_recv,
  452. .mcast_noflush_send = none_mcast_noflush_send,
  453. .mcast_flush_send = none_mcast_flush_send,
  454. .token_recv = none_token_recv,
  455. .token_send = none_token_send,
  456. .recv_flush = none_recv_flush,
  457. .send_flush = none_send_flush,
  458. .iface_check = none_iface_check,
  459. .processor_count_set = none_processor_count_set,
  460. .token_target_set = none_token_target_set,
  461. .ring_reenable = none_ring_reenable,
  462. .mcast_recv_empty = none_mcast_recv_empty,
  463. .member_add = none_member_add,
  464. .member_remove = none_member_remove,
  465. .membership_changed = none_membership_changed
  466. };
  467. struct rrp_algo passive_algo = {
  468. .name = "passive",
  469. .initialize = passive_instance_initialize,
  470. .mcast_recv = passive_mcast_recv,
  471. .mcast_noflush_send = passive_mcast_noflush_send,
  472. .mcast_flush_send = passive_mcast_flush_send,
  473. .token_recv = passive_token_recv,
  474. .token_send = passive_token_send,
  475. .recv_flush = passive_recv_flush,
  476. .send_flush = passive_send_flush,
  477. .iface_check = passive_iface_check,
  478. .processor_count_set = passive_processor_count_set,
  479. .token_target_set = passive_token_target_set,
  480. .ring_reenable = passive_ring_reenable,
  481. .mcast_recv_empty = passive_mcast_recv_empty,
  482. .member_add = passive_member_add,
  483. .member_remove = passive_member_remove,
  484. .membership_changed = passive_membership_changed
  485. };
  486. struct rrp_algo active_algo = {
  487. .name = "active",
  488. .initialize = active_instance_initialize,
  489. .mcast_recv = active_mcast_recv,
  490. .mcast_noflush_send = active_mcast_noflush_send,
  491. .mcast_flush_send = active_mcast_flush_send,
  492. .token_recv = active_token_recv,
  493. .token_send = active_token_send,
  494. .recv_flush = active_recv_flush,
  495. .send_flush = active_send_flush,
  496. .iface_check = active_iface_check,
  497. .processor_count_set = active_processor_count_set,
  498. .token_target_set = active_token_target_set,
  499. .ring_reenable = active_ring_reenable,
  500. .mcast_recv_empty = active_mcast_recv_empty,
  501. .member_add = active_member_add,
  502. .member_remove = active_member_remove,
  503. .membership_changed = active_membership_changed
  504. };
  505. struct rrp_algo *rrp_algos[] = {
  506. &none_algo,
  507. &passive_algo,
  508. &active_algo
  509. };
  510. #define RRP_ALGOS_COUNT 3
  511. #define log_printf(level, format, args...) \
  512. do { \
  513. rrp_instance->totemrrp_log_printf ( \
  514. LOGSYS_ENCODE_RECID(level, \
  515. rrp_instance->totemrrp_subsys_id, \
  516. LOGSYS_RECID_LOG), \
  517. __FUNCTION__, __FILE__, __LINE__, \
  518. format, ##args); \
  519. } while (0);
  520. static void test_active_msg_endian_convert(const struct message_header *in, struct message_header *out)
  521. {
  522. out->type = in->type;
  523. out->encapsulated = in->encapsulated;
  524. out->endian_detector = ENDIAN_LOCAL;
  525. out->ring_number = swab32 (in->ring_number);
  526. out->nodeid_activator = swab32(in->nodeid_activator);
  527. }
  528. static void timer_function_test_ring_timeout (void *context)
  529. {
  530. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  531. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  532. unsigned int *faulty = NULL;
  533. int iface_no = deliver_fn_context->iface_no;
  534. struct message_header msg = {
  535. .type = MESSAGE_TYPE_RING_TEST_ACTIVE,
  536. .endian_detector = ENDIAN_LOCAL,
  537. };
  538. if (strcmp(rrp_instance->totem_config->rrp_mode, "active") == 0)
  539. faulty = ((struct active_instance *)(rrp_instance->rrp_algo_instance))->faulty;
  540. if (strcmp(rrp_instance->totem_config->rrp_mode, "passive") == 0)
  541. faulty = ((struct passive_instance *)(rrp_instance->rrp_algo_instance))->faulty;
  542. assert (faulty != NULL);
  543. if (faulty[iface_no] == 1) {
  544. msg.ring_number = iface_no;
  545. msg.nodeid_activator = rrp_instance->my_nodeid;
  546. totemnet_token_send (
  547. rrp_instance->net_handles[iface_no],
  548. &msg, sizeof (struct message_header));
  549. poll_timer_add (rrp_instance->poll_handle,
  550. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  551. (void *)deliver_fn_context,
  552. timer_function_test_ring_timeout,
  553. &rrp_instance->timer_active_test_ring_timeout[iface_no]);
  554. }
  555. }
  556. /*
  557. * None Replication Implementation
  558. */
  559. static void none_mcast_recv (
  560. struct totemrrp_instance *rrp_instance,
  561. unsigned int iface_no,
  562. void *context,
  563. const void *msg,
  564. unsigned int msg_len)
  565. {
  566. rrp_instance->totemrrp_deliver_fn (
  567. context,
  568. msg,
  569. msg_len);
  570. }
  571. static void none_mcast_flush_send (
  572. struct totemrrp_instance *instance,
  573. const void *msg,
  574. unsigned int msg_len)
  575. {
  576. totemnet_mcast_flush_send (instance->net_handles[0], msg, msg_len);
  577. }
  578. static void none_mcast_noflush_send (
  579. struct totemrrp_instance *instance,
  580. const void *msg,
  581. unsigned int msg_len)
  582. {
  583. totemnet_mcast_noflush_send (instance->net_handles[0], msg, msg_len);
  584. }
  585. static void none_token_recv (
  586. struct totemrrp_instance *rrp_instance,
  587. unsigned int iface_no,
  588. void *context,
  589. const void *msg,
  590. unsigned int msg_len,
  591. unsigned int token_seq)
  592. {
  593. rrp_instance->totemrrp_deliver_fn (
  594. context,
  595. msg,
  596. msg_len);
  597. }
  598. static void none_token_send (
  599. struct totemrrp_instance *instance,
  600. const void *msg,
  601. unsigned int msg_len)
  602. {
  603. totemnet_token_send (
  604. instance->net_handles[0],
  605. msg, msg_len);
  606. }
  607. static void none_recv_flush (struct totemrrp_instance *instance)
  608. {
  609. totemnet_recv_flush (instance->net_handles[0]);
  610. }
  611. static void none_send_flush (struct totemrrp_instance *instance)
  612. {
  613. totemnet_send_flush (instance->net_handles[0]);
  614. }
  615. static void none_iface_check (struct totemrrp_instance *instance)
  616. {
  617. totemnet_iface_check (instance->net_handles[0]);
  618. }
  619. static void none_processor_count_set (
  620. struct totemrrp_instance *instance,
  621. unsigned int processor_count)
  622. {
  623. totemnet_processor_count_set (instance->net_handles[0],
  624. processor_count);
  625. }
  626. static void none_token_target_set (
  627. struct totemrrp_instance *instance,
  628. struct totem_ip_address *token_target,
  629. unsigned int iface_no)
  630. {
  631. totemnet_token_target_set (instance->net_handles[0], token_target);
  632. }
  633. static void none_ring_reenable (
  634. struct totemrrp_instance *instance,
  635. unsigned int iface_no)
  636. {
  637. /*
  638. * No operation
  639. */
  640. }
  641. static int none_mcast_recv_empty (
  642. struct totemrrp_instance *instance)
  643. {
  644. int res;
  645. res = totemnet_recv_mcast_empty (instance->net_handles[0]);
  646. return (res);
  647. }
  648. static int none_member_add (
  649. struct totemrrp_instance *instance,
  650. const struct totem_ip_address *member,
  651. unsigned int iface_no)
  652. {
  653. int res;
  654. res = totemnet_member_add (instance->net_handles[0], member);
  655. return (res);
  656. }
  657. static int none_member_remove (
  658. struct totemrrp_instance *instance,
  659. const struct totem_ip_address *member,
  660. unsigned int iface_no)
  661. {
  662. int res;
  663. res = totemnet_member_remove (instance->net_handles[0], member);
  664. return (res);
  665. }
  666. static void none_membership_changed (
  667. struct totemrrp_instance *rrp_instance,
  668. enum totem_configuration_type configuration_type,
  669. const struct srp_addr *member_list, size_t member_list_entries,
  670. const struct srp_addr *left_list, size_t left_list_entries,
  671. const struct srp_addr *joined_list, size_t joined_list_entries,
  672. const struct memb_ring_id *ring_id)
  673. {
  674. int i;
  675. for (i = 0; i < left_list_entries; i++) {
  676. if (left_list[i].addr[0].family != AF_INET && left_list[i].addr[0].family != AF_INET6) {
  677. log_printf(rrp_instance->totemrrp_log_level_error,
  678. "Membership left list contains incorrect address. "
  679. "This is sign of misconfiguration between nodes!");
  680. } else {
  681. totemnet_member_set_active(rrp_instance->net_handles[0],
  682. &left_list[i].addr[0], 0);
  683. }
  684. }
  685. for (i = 0; i < joined_list_entries; i++) {
  686. if (joined_list[i].addr[0].family != AF_INET && joined_list[i].addr[0].family != AF_INET6) {
  687. log_printf(rrp_instance->totemrrp_log_level_error,
  688. "Membership join list contains incorrect address. "
  689. "This is sign of misconfiguration between nodes!");
  690. } else {
  691. totemnet_member_set_active(rrp_instance->net_handles[0],
  692. &joined_list[i].addr[0], 1);
  693. }
  694. }
  695. }
  696. /*
  697. * Passive Replication Implementation
  698. */
  699. void *passive_instance_initialize (
  700. struct totemrrp_instance *rrp_instance,
  701. int interface_count)
  702. {
  703. struct passive_instance *instance;
  704. instance = malloc (sizeof (struct passive_instance));
  705. if (instance == 0) {
  706. goto error_exit;
  707. }
  708. memset (instance, 0, sizeof (struct passive_instance));
  709. instance->faulty = malloc (sizeof (int) * interface_count);
  710. if (instance->faulty == 0) {
  711. free (instance);
  712. instance = 0;
  713. goto error_exit;
  714. }
  715. memset (instance->faulty, 0, sizeof (int) * interface_count);
  716. instance->token_recv_count = malloc (sizeof (int) * interface_count);
  717. if (instance->token_recv_count == 0) {
  718. free (instance->faulty);
  719. free (instance);
  720. instance = 0;
  721. goto error_exit;
  722. }
  723. memset (instance->token_recv_count, 0, sizeof (int) * interface_count);
  724. instance->mcast_recv_count = malloc (sizeof (int) * interface_count);
  725. if (instance->mcast_recv_count == 0) {
  726. free (instance->token_recv_count);
  727. free (instance->faulty);
  728. free (instance);
  729. instance = 0;
  730. goto error_exit;
  731. }
  732. memset (instance->mcast_recv_count, 0, sizeof (int) * interface_count);
  733. error_exit:
  734. return ((void *)instance);
  735. }
  736. static void timer_function_passive_token_expired (void *context)
  737. {
  738. struct passive_instance *passive_instance = (struct passive_instance *)context;
  739. struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  740. rrp_instance->totemrrp_deliver_fn (
  741. passive_instance->totemrrp_context,
  742. passive_instance->token,
  743. passive_instance->token_len);
  744. }
  745. /* TODO
  746. static void timer_function_passive_problem_decrementer (void *context)
  747. {
  748. // struct passive_instance *passive_instance = (struct passive_instance *)context;
  749. // struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  750. }
  751. */
  752. static void passive_timer_expired_token_start (
  753. struct passive_instance *passive_instance)
  754. {
  755. poll_timer_add (
  756. passive_instance->rrp_instance->poll_handle,
  757. passive_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  758. (void *)passive_instance,
  759. timer_function_passive_token_expired,
  760. &passive_instance->timer_expired_token);
  761. }
  762. static void passive_timer_expired_token_cancel (
  763. struct passive_instance *passive_instance)
  764. {
  765. poll_timer_delete (
  766. passive_instance->rrp_instance->poll_handle,
  767. passive_instance->timer_expired_token);
  768. }
  769. /*
  770. static void passive_timer_problem_decrementer_start (
  771. struct passive_instance *passive_instance)
  772. {
  773. poll_timer_add (
  774. passive_instance->rrp_instance->poll_handle,
  775. passive_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  776. (void *)passive_instance,
  777. timer_function_passive_problem_decrementer,
  778. &passive_instance->timer_problem_decrementer);
  779. }
  780. static void passive_timer_problem_decrementer_cancel (
  781. struct passive_instance *passive_instance)
  782. {
  783. poll_timer_delete (
  784. passive_instance->rrp_instance->poll_handle,
  785. passive_instance->timer_problem_decrementer);
  786. }
  787. */
  788. /*
  789. * Monitor function implementation from rrp paper.
  790. * rrp_instance is passive rrp instance, iface_no is interface with received messgae/token and
  791. * is_token_recv_count is boolean variable which donates if message is token (>1) or regular
  792. * message (= 0)
  793. */
  794. static void passive_monitor (
  795. struct totemrrp_instance *rrp_instance,
  796. unsigned int iface_no,
  797. int is_token_recv_count)
  798. {
  799. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  800. unsigned int *recv_count;
  801. unsigned int max;
  802. unsigned int i;
  803. unsigned int min_all, min_active;
  804. unsigned int threshold;
  805. /*
  806. * Monitor for failures
  807. */
  808. if (is_token_recv_count) {
  809. recv_count = passive_instance->token_recv_count;
  810. threshold = rrp_instance->totem_config->rrp_problem_count_threshold;
  811. } else {
  812. recv_count = passive_instance->mcast_recv_count;
  813. threshold = rrp_instance->totem_config->rrp_problem_count_mcast_threshold;
  814. }
  815. recv_count[iface_no] += 1;
  816. max = 0;
  817. for (i = 0; i < rrp_instance->interface_count; i++) {
  818. if (max < recv_count[i]) {
  819. max = recv_count[i];
  820. }
  821. }
  822. /*
  823. * Max is larger than threshold -> start adjusting process
  824. */
  825. if (max > PASSIVE_RECV_COUNT_THRESHOLD) {
  826. min_all = min_active = recv_count[iface_no];
  827. for (i = 0; i < rrp_instance->interface_count; i++) {
  828. if (recv_count[i] < min_all) {
  829. min_all = recv_count[i];
  830. }
  831. if (passive_instance->faulty[i] == 0 &&
  832. recv_count[i] < min_active) {
  833. min_active = recv_count[i];
  834. }
  835. }
  836. if (min_all > 0) {
  837. /*
  838. * There is one or more faulty device with recv_count > 0
  839. */
  840. for (i = 0; i < rrp_instance->interface_count; i++) {
  841. recv_count[i] -= min_all;
  842. }
  843. } else {
  844. /*
  845. * No faulty device with recv_count > 0, adjust only active
  846. * devices
  847. */
  848. for (i = 0; i < rrp_instance->interface_count; i++) {
  849. if (passive_instance->faulty[i] == 0) {
  850. recv_count[i] -= min_active;
  851. }
  852. }
  853. }
  854. /*
  855. * Find again max
  856. */
  857. max = 0;
  858. for (i = 0; i < rrp_instance->interface_count; i++) {
  859. if (max < recv_count[i]) {
  860. max = recv_count[i];
  861. }
  862. }
  863. }
  864. for (i = 0; i < rrp_instance->interface_count; i++) {
  865. if ((passive_instance->faulty[i] == 0) &&
  866. (max - recv_count[i] > threshold)) {
  867. passive_instance->faulty[i] = 1;
  868. poll_timer_add (rrp_instance->poll_handle,
  869. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  870. rrp_instance->deliver_fn_context[i],
  871. timer_function_test_ring_timeout,
  872. &rrp_instance->timer_active_test_ring_timeout[i]);
  873. sprintf (rrp_instance->status[i],
  874. "Marking ringid %u interface %s FAULTY",
  875. i,
  876. totemnet_iface_print (rrp_instance->net_handles[i]));
  877. log_printf (
  878. rrp_instance->totemrrp_log_level_error,
  879. "%s",
  880. rrp_instance->status[i]);
  881. }
  882. }
  883. }
  884. static void passive_mcast_recv (
  885. struct totemrrp_instance *rrp_instance,
  886. unsigned int iface_no,
  887. void *context,
  888. const void *msg,
  889. unsigned int msg_len)
  890. {
  891. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  892. rrp_instance->totemrrp_deliver_fn (
  893. context,
  894. msg,
  895. msg_len);
  896. if (rrp_instance->totemrrp_msgs_missing() == 0 &&
  897. passive_instance->timer_expired_token) {
  898. /*
  899. * Delivers the last token
  900. */
  901. rrp_instance->totemrrp_deliver_fn (
  902. passive_instance->totemrrp_context,
  903. passive_instance->token,
  904. passive_instance->token_len);
  905. passive_timer_expired_token_cancel (passive_instance);
  906. }
  907. passive_monitor (rrp_instance, iface_no, 0);
  908. }
  909. static void passive_mcast_flush_send (
  910. struct totemrrp_instance *instance,
  911. const void *msg,
  912. unsigned int msg_len)
  913. {
  914. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  915. int i = 0;
  916. do {
  917. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  918. i++;
  919. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1));
  920. if (i <= instance->interface_count) {
  921. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  922. }
  923. }
  924. static void passive_mcast_noflush_send (
  925. struct totemrrp_instance *instance,
  926. const void *msg,
  927. unsigned int msg_len)
  928. {
  929. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  930. int i = 0;
  931. do {
  932. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  933. i++;
  934. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1));
  935. if (i <= instance->interface_count) {
  936. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  937. }
  938. }
  939. static void passive_token_recv (
  940. struct totemrrp_instance *rrp_instance,
  941. unsigned int iface_no,
  942. void *context,
  943. const void *msg,
  944. unsigned int msg_len,
  945. unsigned int token_seq)
  946. {
  947. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  948. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  949. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  950. rrp_instance->totemrrp_deliver_fn (
  951. context,
  952. msg,
  953. msg_len);
  954. } else {
  955. memcpy (passive_instance->token, msg, msg_len);
  956. passive_timer_expired_token_start (passive_instance);
  957. }
  958. passive_monitor (rrp_instance, iface_no, 1);
  959. }
  960. static void passive_token_send (
  961. struct totemrrp_instance *instance,
  962. const void *msg,
  963. unsigned int msg_len)
  964. {
  965. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  966. int i = 0;
  967. do {
  968. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  969. i++;
  970. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->token_xmit_iface] == 1));
  971. if (i <= instance->interface_count) {
  972. totemnet_token_send (
  973. instance->net_handles[passive_instance->token_xmit_iface],
  974. msg, msg_len);
  975. }
  976. }
  977. static void passive_recv_flush (struct totemrrp_instance *instance)
  978. {
  979. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  980. unsigned int i;
  981. for (i = 0; i < instance->interface_count; i++) {
  982. if (rrp_algo_instance->faulty[i] == 0) {
  983. totemnet_recv_flush (instance->net_handles[i]);
  984. }
  985. }
  986. }
  987. static void passive_send_flush (struct totemrrp_instance *instance)
  988. {
  989. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  990. unsigned int i;
  991. for (i = 0; i < instance->interface_count; i++) {
  992. if (rrp_algo_instance->faulty[i] == 0) {
  993. totemnet_send_flush (instance->net_handles[i]);
  994. }
  995. }
  996. }
  997. static void passive_iface_check (struct totemrrp_instance *instance)
  998. {
  999. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1000. unsigned int i;
  1001. for (i = 0; i < instance->interface_count; i++) {
  1002. if (rrp_algo_instance->faulty[i] == 0) {
  1003. totemnet_iface_check (instance->net_handles[i]);
  1004. }
  1005. }
  1006. }
  1007. static void passive_processor_count_set (
  1008. struct totemrrp_instance *instance,
  1009. unsigned int processor_count)
  1010. {
  1011. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1012. unsigned int i;
  1013. for (i = 0; i < instance->interface_count; i++) {
  1014. if (rrp_algo_instance->faulty[i] == 0) {
  1015. totemnet_processor_count_set (instance->net_handles[i],
  1016. processor_count);
  1017. }
  1018. }
  1019. }
  1020. static void passive_token_target_set (
  1021. struct totemrrp_instance *instance,
  1022. struct totem_ip_address *token_target,
  1023. unsigned int iface_no)
  1024. {
  1025. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1026. }
  1027. static int passive_mcast_recv_empty (
  1028. struct totemrrp_instance *instance)
  1029. {
  1030. int res;
  1031. int msgs_emptied = 0;
  1032. int i;
  1033. for (i = 0; i < instance->interface_count; i++) {
  1034. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1035. if (res == -1) {
  1036. return (-1);
  1037. }
  1038. if (res == 1) {
  1039. msgs_emptied = 1;
  1040. }
  1041. }
  1042. return (msgs_emptied);
  1043. }
  1044. static int passive_member_add (
  1045. struct totemrrp_instance *instance,
  1046. const struct totem_ip_address *member,
  1047. unsigned int iface_no)
  1048. {
  1049. int res;
  1050. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1051. return (res);
  1052. }
  1053. static int passive_member_remove (
  1054. struct totemrrp_instance *instance,
  1055. const struct totem_ip_address *member,
  1056. unsigned int iface_no)
  1057. {
  1058. int res;
  1059. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1060. return (res);
  1061. }
  1062. static void passive_membership_changed (
  1063. struct totemrrp_instance *rrp_instance,
  1064. enum totem_configuration_type configuration_type,
  1065. const struct srp_addr *member_list, size_t member_list_entries,
  1066. const struct srp_addr *left_list, size_t left_list_entries,
  1067. const struct srp_addr *joined_list, size_t joined_list_entries,
  1068. const struct memb_ring_id *ring_id)
  1069. {
  1070. int i;
  1071. int interface;
  1072. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1073. for (i = 0; i < left_list_entries; i++) {
  1074. if (left_list[i].addr[interface].family != AF_INET &&
  1075. left_list[i].addr[interface].family != AF_INET6) {
  1076. log_printf(rrp_instance->totemrrp_log_level_error,
  1077. "Membership left list contains incorrect address. "
  1078. "This is sign of misconfiguration between nodes!");
  1079. } else {
  1080. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1081. &left_list[i].addr[interface], 0);
  1082. }
  1083. }
  1084. for (i = 0; i < joined_list_entries; i++) {
  1085. if (joined_list[i].addr[interface].family != AF_INET &&
  1086. joined_list[i].addr[interface].family != AF_INET6) {
  1087. log_printf(rrp_instance->totemrrp_log_level_error,
  1088. "Membership join list contains incorrect address. "
  1089. "This is sign of misconfiguration between nodes!");
  1090. } else {
  1091. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1092. &joined_list[i].addr[interface], 1);
  1093. }
  1094. }
  1095. }
  1096. }
  1097. static void passive_ring_reenable (
  1098. struct totemrrp_instance *instance,
  1099. unsigned int iface_no)
  1100. {
  1101. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1102. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  1103. instance->interface_count);
  1104. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  1105. instance->interface_count);
  1106. if (iface_no == instance->interface_count) {
  1107. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1108. instance->interface_count);
  1109. } else {
  1110. rrp_algo_instance->faulty[iface_no] = 0;
  1111. }
  1112. }
  1113. /*
  1114. * Active Replication Implementation
  1115. */
  1116. void *active_instance_initialize (
  1117. struct totemrrp_instance *rrp_instance,
  1118. int interface_count)
  1119. {
  1120. struct active_instance *instance;
  1121. instance = malloc (sizeof (struct active_instance));
  1122. if (instance == 0) {
  1123. goto error_exit;
  1124. }
  1125. memset (instance, 0, sizeof (struct active_instance));
  1126. instance->faulty = malloc (sizeof (int) * interface_count);
  1127. if (instance->faulty == 0) {
  1128. free (instance);
  1129. instance = 0;
  1130. goto error_exit;
  1131. }
  1132. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  1133. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  1134. if (instance->last_token_recv == 0) {
  1135. free (instance->faulty);
  1136. free (instance);
  1137. instance = 0;
  1138. goto error_exit;
  1139. }
  1140. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  1141. instance->counter_problems = malloc (sizeof (int) * interface_count);
  1142. if (instance->counter_problems == 0) {
  1143. free (instance->last_token_recv);
  1144. free (instance->faulty);
  1145. free (instance);
  1146. instance = 0;
  1147. goto error_exit;
  1148. }
  1149. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  1150. instance->timer_expired_token = 0;
  1151. instance->timer_problem_decrementer = 0;
  1152. instance->rrp_instance = rrp_instance;
  1153. instance->last_token_seq = ARR_SEQNO_START_TOKEN - 1;
  1154. error_exit:
  1155. return ((void *)instance);
  1156. }
  1157. static void timer_function_active_problem_decrementer (void *context)
  1158. {
  1159. struct active_instance *active_instance = (struct active_instance *)context;
  1160. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1161. unsigned int problem_found = 0;
  1162. unsigned int i;
  1163. for (i = 0; i < rrp_instance->interface_count; i++) {
  1164. if (active_instance->counter_problems[i] > 0) {
  1165. problem_found = 1;
  1166. active_instance->counter_problems[i] -= 1;
  1167. if (active_instance->counter_problems[i] == 0) {
  1168. sprintf (rrp_instance->status[i],
  1169. "ring %d active with no faults", i);
  1170. } else {
  1171. sprintf (rrp_instance->status[i],
  1172. "Decrementing problem counter for iface %s to [%d of %d]",
  1173. totemnet_iface_print (rrp_instance->net_handles[i]),
  1174. active_instance->counter_problems[i],
  1175. rrp_instance->totem_config->rrp_problem_count_threshold);
  1176. }
  1177. log_printf (
  1178. rrp_instance->totemrrp_log_level_warning,
  1179. "%s",
  1180. rrp_instance->status[i]);
  1181. }
  1182. }
  1183. if (problem_found) {
  1184. active_timer_problem_decrementer_start (active_instance);
  1185. } else {
  1186. active_instance->timer_problem_decrementer = 0;
  1187. }
  1188. }
  1189. static void timer_function_active_token_expired (void *context)
  1190. {
  1191. struct active_instance *active_instance = (struct active_instance *)context;
  1192. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1193. unsigned int i;
  1194. for (i = 0; i < rrp_instance->interface_count; i++) {
  1195. if (active_instance->last_token_recv[i] == 0) {
  1196. active_instance->counter_problems[i] += 1;
  1197. if (active_instance->timer_problem_decrementer == 0) {
  1198. active_timer_problem_decrementer_start (active_instance);
  1199. }
  1200. sprintf (rrp_instance->status[i],
  1201. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  1202. active_instance->last_token_seq,
  1203. totemnet_iface_print (rrp_instance->net_handles[i]),
  1204. active_instance->counter_problems[i],
  1205. rrp_instance->totem_config->rrp_problem_count_threshold);
  1206. log_printf (
  1207. rrp_instance->totemrrp_log_level_warning,
  1208. "%s",
  1209. rrp_instance->status[i]);
  1210. }
  1211. }
  1212. for (i = 0; i < rrp_instance->interface_count; i++) {
  1213. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold)
  1214. {
  1215. active_instance->faulty[i] = 1;
  1216. poll_timer_add (rrp_instance->poll_handle,
  1217. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  1218. rrp_instance->deliver_fn_context[i],
  1219. timer_function_test_ring_timeout,
  1220. &rrp_instance->timer_active_test_ring_timeout[i]);
  1221. sprintf (rrp_instance->status[i],
  1222. "Marking seqid %d ringid %u interface %s FAULTY",
  1223. active_instance->last_token_seq,
  1224. i,
  1225. totemnet_iface_print (rrp_instance->net_handles[i]));
  1226. log_printf (
  1227. rrp_instance->totemrrp_log_level_error,
  1228. "%s",
  1229. rrp_instance->status[i]);
  1230. active_timer_problem_decrementer_cancel (active_instance);
  1231. }
  1232. }
  1233. rrp_instance->totemrrp_deliver_fn (
  1234. active_instance->totemrrp_context,
  1235. active_instance->token,
  1236. active_instance->token_len);
  1237. }
  1238. static void active_timer_expired_token_start (
  1239. struct active_instance *active_instance)
  1240. {
  1241. poll_timer_add (
  1242. active_instance->rrp_instance->poll_handle,
  1243. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  1244. (void *)active_instance,
  1245. timer_function_active_token_expired,
  1246. &active_instance->timer_expired_token);
  1247. }
  1248. static void active_timer_expired_token_cancel (
  1249. struct active_instance *active_instance)
  1250. {
  1251. poll_timer_delete (
  1252. active_instance->rrp_instance->poll_handle,
  1253. active_instance->timer_expired_token);
  1254. }
  1255. static void active_timer_problem_decrementer_start (
  1256. struct active_instance *active_instance)
  1257. {
  1258. poll_timer_add (
  1259. active_instance->rrp_instance->poll_handle,
  1260. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  1261. (void *)active_instance,
  1262. timer_function_active_problem_decrementer,
  1263. &active_instance->timer_problem_decrementer);
  1264. }
  1265. static void active_timer_problem_decrementer_cancel (
  1266. struct active_instance *active_instance)
  1267. {
  1268. poll_timer_delete (
  1269. active_instance->rrp_instance->poll_handle,
  1270. active_instance->timer_problem_decrementer);
  1271. }
  1272. /*
  1273. * active replication
  1274. */
  1275. static void active_mcast_recv (
  1276. struct totemrrp_instance *instance,
  1277. unsigned int iface_no,
  1278. void *context,
  1279. const void *msg,
  1280. unsigned int msg_len)
  1281. {
  1282. instance->totemrrp_deliver_fn (
  1283. context,
  1284. msg,
  1285. msg_len);
  1286. }
  1287. static void active_mcast_flush_send (
  1288. struct totemrrp_instance *instance,
  1289. const void *msg,
  1290. unsigned int msg_len)
  1291. {
  1292. int i;
  1293. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1294. for (i = 0; i < instance->interface_count; i++) {
  1295. if (rrp_algo_instance->faulty[i] == 0) {
  1296. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  1297. }
  1298. }
  1299. }
  1300. static void active_mcast_noflush_send (
  1301. struct totemrrp_instance *instance,
  1302. const void *msg,
  1303. unsigned int msg_len)
  1304. {
  1305. int i;
  1306. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1307. for (i = 0; i < instance->interface_count; i++) {
  1308. if (rrp_algo_instance->faulty[i] == 0) {
  1309. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  1310. }
  1311. }
  1312. }
  1313. static void active_token_recv (
  1314. struct totemrrp_instance *rrp_instance,
  1315. unsigned int iface_no,
  1316. void *context,
  1317. const void *msg,
  1318. unsigned int msg_len,
  1319. unsigned int token_seq)
  1320. {
  1321. int i;
  1322. struct active_instance *active_instance = (struct active_instance *)rrp_instance->rrp_algo_instance;
  1323. active_instance->totemrrp_context = context;
  1324. if (sq_lt_compare (active_instance->last_token_seq, token_seq)) {
  1325. memcpy (active_instance->token, msg, msg_len);
  1326. active_instance->token_len = msg_len;
  1327. for (i = 0; i < rrp_instance->interface_count; i++) {
  1328. active_instance->last_token_recv[i] = 0;
  1329. }
  1330. active_instance->last_token_recv[iface_no] = 1;
  1331. active_timer_expired_token_start (active_instance);
  1332. }
  1333. /*
  1334. * This doesn't follow spec because the spec assumes we will know
  1335. * when token resets occur.
  1336. */
  1337. active_instance->last_token_seq = token_seq;
  1338. if (token_seq == active_instance->last_token_seq) {
  1339. active_instance->last_token_recv[iface_no] = 1;
  1340. for (i = 0; i < rrp_instance->interface_count; i++) {
  1341. if ((active_instance->last_token_recv[i] == 0) &&
  1342. active_instance->faulty[i] == 0) {
  1343. return; /* don't deliver token */
  1344. }
  1345. }
  1346. active_timer_expired_token_cancel (active_instance);
  1347. rrp_instance->totemrrp_deliver_fn (
  1348. context,
  1349. msg,
  1350. msg_len);
  1351. }
  1352. }
  1353. static void active_token_send (
  1354. struct totemrrp_instance *instance,
  1355. const void *msg,
  1356. unsigned int msg_len)
  1357. {
  1358. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1359. int i;
  1360. for (i = 0; i < instance->interface_count; i++) {
  1361. if (rrp_algo_instance->faulty[i] == 0) {
  1362. totemnet_token_send (
  1363. instance->net_handles[i],
  1364. msg, msg_len);
  1365. }
  1366. }
  1367. }
  1368. static void active_recv_flush (struct totemrrp_instance *instance)
  1369. {
  1370. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1371. unsigned int i;
  1372. for (i = 0; i < instance->interface_count; i++) {
  1373. if (rrp_algo_instance->faulty[i] == 0) {
  1374. totemnet_recv_flush (instance->net_handles[i]);
  1375. }
  1376. }
  1377. }
  1378. static void active_send_flush (struct totemrrp_instance *instance)
  1379. {
  1380. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1381. unsigned int i;
  1382. for (i = 0; i < instance->interface_count; i++) {
  1383. if (rrp_algo_instance->faulty[i] == 0) {
  1384. totemnet_send_flush (instance->net_handles[i]);
  1385. }
  1386. }
  1387. }
  1388. static int active_member_add (
  1389. struct totemrrp_instance *instance,
  1390. const struct totem_ip_address *member,
  1391. unsigned int iface_no)
  1392. {
  1393. int res;
  1394. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1395. return (res);
  1396. }
  1397. static int active_member_remove (
  1398. struct totemrrp_instance *instance,
  1399. const struct totem_ip_address *member,
  1400. unsigned int iface_no)
  1401. {
  1402. int res;
  1403. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1404. return (res);
  1405. }
  1406. static void active_membership_changed (
  1407. struct totemrrp_instance *rrp_instance,
  1408. enum totem_configuration_type configuration_type,
  1409. const struct srp_addr *member_list, size_t member_list_entries,
  1410. const struct srp_addr *left_list, size_t left_list_entries,
  1411. const struct srp_addr *joined_list, size_t joined_list_entries,
  1412. const struct memb_ring_id *ring_id)
  1413. {
  1414. int i;
  1415. int interface;
  1416. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1417. for (i = 0; i < left_list_entries; i++) {
  1418. if (left_list[i].addr[interface].family != AF_INET &&
  1419. left_list[i].addr[interface].family != AF_INET6) {
  1420. log_printf(rrp_instance->totemrrp_log_level_error,
  1421. "Membership left list contains incorrect address. "
  1422. "This is sign of misconfiguration between nodes!");
  1423. } else {
  1424. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1425. &left_list[i].addr[interface], 0);
  1426. }
  1427. }
  1428. for (i = 0; i < joined_list_entries; i++) {
  1429. if (joined_list[i].addr[interface].family != AF_INET &&
  1430. joined_list[i].addr[interface].family != AF_INET6) {
  1431. log_printf(rrp_instance->totemrrp_log_level_error,
  1432. "Membership join list contains incorrect address. "
  1433. "This is sign of misconfiguration between nodes!");
  1434. } else {
  1435. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1436. &joined_list[i].addr[interface], 1);
  1437. }
  1438. }
  1439. }
  1440. }
  1441. static void active_iface_check (struct totemrrp_instance *instance)
  1442. {
  1443. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1444. unsigned int i;
  1445. for (i = 0; i < instance->interface_count; i++) {
  1446. if (rrp_algo_instance->faulty[i] == 0) {
  1447. totemnet_iface_check (instance->net_handles[i]);
  1448. }
  1449. }
  1450. }
  1451. static void active_processor_count_set (
  1452. struct totemrrp_instance *instance,
  1453. unsigned int processor_count)
  1454. {
  1455. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1456. unsigned int i;
  1457. for (i = 0; i < instance->interface_count; i++) {
  1458. if (rrp_algo_instance->faulty[i] == 0) {
  1459. totemnet_processor_count_set (instance->net_handles[i],
  1460. processor_count);
  1461. }
  1462. }
  1463. }
  1464. static void active_token_target_set (
  1465. struct totemrrp_instance *instance,
  1466. struct totem_ip_address *token_target,
  1467. unsigned int iface_no)
  1468. {
  1469. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1470. }
  1471. static int active_mcast_recv_empty (
  1472. struct totemrrp_instance *instance)
  1473. {
  1474. int res;
  1475. int msgs_emptied = 0;
  1476. int i;
  1477. for (i = 0; i < instance->interface_count; i++) {
  1478. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1479. if (res == -1) {
  1480. return (-1);
  1481. }
  1482. if (res == 1) {
  1483. msgs_emptied = 1;
  1484. }
  1485. }
  1486. return (msgs_emptied);
  1487. }
  1488. static void active_ring_reenable (
  1489. struct totemrrp_instance *instance,
  1490. unsigned int iface_no)
  1491. {
  1492. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1493. if (iface_no == instance->interface_count) {
  1494. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1495. instance->interface_count);
  1496. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1497. instance->interface_count);
  1498. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1499. instance->interface_count);
  1500. } else {
  1501. rrp_algo_instance->last_token_recv[iface_no] = 0;
  1502. rrp_algo_instance->faulty[iface_no] = 0;
  1503. rrp_algo_instance->counter_problems[iface_no] = 0;
  1504. }
  1505. }
  1506. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1507. {
  1508. memset (instance, 0, sizeof (struct totemrrp_instance));
  1509. }
  1510. static int totemrrp_algorithm_set (
  1511. struct totem_config *totem_config,
  1512. struct totemrrp_instance *instance)
  1513. {
  1514. unsigned int res = -1;
  1515. unsigned int i;
  1516. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1517. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1518. instance->rrp_algo = rrp_algos[i];
  1519. if (rrp_algos[i]->initialize) {
  1520. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1521. instance,
  1522. totem_config->interface_count);
  1523. }
  1524. res = 0;
  1525. break;
  1526. }
  1527. }
  1528. for (i = 0; i < totem_config->interface_count; i++) {
  1529. instance->status[i] = malloc (1024);
  1530. sprintf (instance->status[i], "ring %d active with no faults", i);
  1531. }
  1532. return (res);
  1533. }
  1534. void rrp_deliver_fn (
  1535. void *context,
  1536. const void *msg,
  1537. unsigned int msg_len)
  1538. {
  1539. unsigned int token_seqid;
  1540. unsigned int token_is;
  1541. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1542. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  1543. const struct message_header *hdr = msg;
  1544. struct message_header tmp_msg, activate_msg;
  1545. memset(&tmp_msg, 0, sizeof(struct message_header));
  1546. memset(&activate_msg, 0, sizeof(struct message_header));
  1547. rrp_instance->totemrrp_token_seqid_get (
  1548. msg,
  1549. &token_seqid,
  1550. &token_is);
  1551. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVE) {
  1552. log_printf (
  1553. rrp_instance->totemrrp_log_level_debug,
  1554. "received message requesting test of ring now active\n");
  1555. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1556. test_active_msg_endian_convert(hdr, &tmp_msg);
  1557. hdr = &tmp_msg;
  1558. }
  1559. if (hdr->nodeid_activator == rrp_instance->my_nodeid) {
  1560. /*
  1561. * Send an activate message
  1562. */
  1563. activate_msg.type = MESSAGE_TYPE_RING_TEST_ACTIVATE;
  1564. activate_msg.endian_detector = ENDIAN_LOCAL;
  1565. activate_msg.ring_number = hdr->ring_number;
  1566. activate_msg.nodeid_activator = rrp_instance->my_nodeid;
  1567. totemnet_token_send (
  1568. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1569. &activate_msg, sizeof (struct message_header));
  1570. } else {
  1571. /*
  1572. * Send a ring test message
  1573. */
  1574. totemnet_token_send (
  1575. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1576. msg, msg_len);
  1577. }
  1578. } else
  1579. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVATE) {
  1580. log_printf (
  1581. rrp_instance->totemrrp_log_level_notice,
  1582. "Automatically recovered ring %d\n", hdr->ring_number);
  1583. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1584. test_active_msg_endian_convert(hdr, &tmp_msg);
  1585. hdr = &tmp_msg;
  1586. }
  1587. totemrrp_ring_reenable (rrp_instance, deliver_fn_context->iface_no);
  1588. if (hdr->nodeid_activator != rrp_instance->my_nodeid) {
  1589. totemnet_token_send (
  1590. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1591. msg, msg_len);
  1592. }
  1593. } else
  1594. if (token_is) {
  1595. /*
  1596. * Deliver to the token receiver for this rrp algorithm
  1597. */
  1598. rrp_instance->rrp_algo->token_recv (
  1599. rrp_instance,
  1600. deliver_fn_context->iface_no,
  1601. deliver_fn_context->context,
  1602. msg,
  1603. msg_len,
  1604. token_seqid);
  1605. } else {
  1606. /*
  1607. * Deliver to the mcast receiver for this rrp algorithm
  1608. */
  1609. rrp_instance->rrp_algo->mcast_recv (
  1610. rrp_instance,
  1611. deliver_fn_context->iface_no,
  1612. deliver_fn_context->context,
  1613. msg,
  1614. msg_len);
  1615. }
  1616. }
  1617. void rrp_iface_change_fn (
  1618. void *context,
  1619. const struct totem_ip_address *iface_addr)
  1620. {
  1621. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1622. deliver_fn_context->instance->my_nodeid = iface_addr->nodeid;
  1623. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1624. deliver_fn_context->context,
  1625. iface_addr,
  1626. deliver_fn_context->iface_no);
  1627. }
  1628. int totemrrp_finalize (
  1629. void *rrp_context)
  1630. {
  1631. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1632. int i;
  1633. for (i = 0; i < instance->interface_count; i++) {
  1634. totemnet_finalize (instance->net_handles[i]);
  1635. }
  1636. return (0);
  1637. }
  1638. static void rrp_target_set_completed (void *context)
  1639. {
  1640. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1641. deliver_fn_context->instance->totemrrp_target_set_completed (deliver_fn_context->context);
  1642. }
  1643. /*
  1644. * Totem Redundant Ring interface
  1645. * depends on poll abstraction, POSIX, IPV4
  1646. */
  1647. /*
  1648. * Create an instance
  1649. */
  1650. int totemrrp_initialize (
  1651. hdb_handle_t poll_handle,
  1652. void **rrp_context,
  1653. struct totem_config *totem_config,
  1654. totemsrp_stats_t *stats,
  1655. void *context,
  1656. void (*deliver_fn) (
  1657. void *context,
  1658. const void *msg,
  1659. unsigned int msg_len),
  1660. void (*iface_change_fn) (
  1661. void *context,
  1662. const struct totem_ip_address *iface_addr,
  1663. unsigned int iface_no),
  1664. void (*token_seqid_get) (
  1665. const void *msg,
  1666. unsigned int *seqid,
  1667. unsigned int *token_is),
  1668. unsigned int (*msgs_missing) (void),
  1669. void (*target_set_completed) (void *context))
  1670. {
  1671. struct totemrrp_instance *instance;
  1672. unsigned int res;
  1673. int i;
  1674. instance = malloc (sizeof (struct totemrrp_instance));
  1675. if (instance == 0) {
  1676. return (-1);
  1677. }
  1678. totemrrp_instance_initialize (instance);
  1679. instance->totem_config = totem_config;
  1680. res = totemrrp_algorithm_set (
  1681. instance->totem_config,
  1682. instance);
  1683. if (res == -1) {
  1684. goto error_destroy;
  1685. }
  1686. /*
  1687. * Configure logging
  1688. */
  1689. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1690. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1691. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1692. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1693. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1694. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1695. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1696. instance->interfaces = totem_config->interfaces;
  1697. instance->poll_handle = poll_handle;
  1698. instance->totemrrp_deliver_fn = deliver_fn;
  1699. instance->totemrrp_iface_change_fn = iface_change_fn;
  1700. instance->totemrrp_token_seqid_get = token_seqid_get;
  1701. instance->totemrrp_target_set_completed = target_set_completed;
  1702. instance->totemrrp_msgs_missing = msgs_missing;
  1703. instance->interface_count = totem_config->interface_count;
  1704. instance->net_handles = malloc (sizeof (void *) * totem_config->interface_count);
  1705. instance->context = context;
  1706. instance->poll_handle = poll_handle;
  1707. for (i = 0; i < totem_config->interface_count; i++) {
  1708. struct deliver_fn_context *deliver_fn_context;
  1709. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1710. assert (deliver_fn_context);
  1711. deliver_fn_context->instance = instance;
  1712. deliver_fn_context->context = context;
  1713. deliver_fn_context->iface_no = i;
  1714. instance->deliver_fn_context[i] = (void *)deliver_fn_context;
  1715. totemnet_initialize (
  1716. poll_handle,
  1717. &instance->net_handles[i],
  1718. totem_config,
  1719. stats,
  1720. i,
  1721. (void *)deliver_fn_context,
  1722. rrp_deliver_fn,
  1723. rrp_iface_change_fn,
  1724. rrp_target_set_completed);
  1725. totemnet_net_mtu_adjust (instance->net_handles[i], totem_config);
  1726. }
  1727. *rrp_context = instance;
  1728. return (0);
  1729. error_destroy:
  1730. free (instance);
  1731. return (res);
  1732. }
  1733. int totemrrp_processor_count_set (
  1734. void *rrp_context,
  1735. unsigned int processor_count)
  1736. {
  1737. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1738. instance->rrp_algo->processor_count_set (instance, processor_count);
  1739. instance->processor_count = processor_count;
  1740. return (0);
  1741. }
  1742. int totemrrp_token_target_set (
  1743. void *rrp_context,
  1744. struct totem_ip_address *addr,
  1745. unsigned int iface_no)
  1746. {
  1747. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1748. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1749. return (0);
  1750. }
  1751. int totemrrp_recv_flush (void *rrp_context)
  1752. {
  1753. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1754. instance->rrp_algo->recv_flush (instance);
  1755. return (0);
  1756. }
  1757. int totemrrp_send_flush (void *rrp_context)
  1758. {
  1759. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1760. instance->rrp_algo->send_flush (instance);
  1761. return (0);
  1762. }
  1763. int totemrrp_token_send (
  1764. void *rrp_context,
  1765. const void *msg,
  1766. unsigned int msg_len)
  1767. {
  1768. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1769. instance->rrp_algo->token_send (instance, msg, msg_len);
  1770. return (0);
  1771. }
  1772. int totemrrp_mcast_flush_send (
  1773. void *rrp_context,
  1774. const void *msg,
  1775. unsigned int msg_len)
  1776. {
  1777. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1778. int res = 0;
  1779. // TODO this needs to return the result
  1780. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1781. return (res);
  1782. }
  1783. int totemrrp_mcast_noflush_send (
  1784. void *rrp_context,
  1785. const void *msg,
  1786. unsigned int msg_len)
  1787. {
  1788. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1789. /*
  1790. * merge detects go out through mcast_flush_send so it is safe to
  1791. * flush these messages if we are only one processor. This avoids
  1792. * an encryption/hmac and decryption/hmac
  1793. */
  1794. if (instance->processor_count > 1) {
  1795. // TODO this needs to return the result
  1796. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1797. }
  1798. return (0);
  1799. }
  1800. int totemrrp_iface_check (void *rrp_context)
  1801. {
  1802. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1803. instance->rrp_algo->iface_check (instance);
  1804. return (0);
  1805. }
  1806. int totemrrp_ifaces_get (
  1807. void *rrp_context,
  1808. char ***status,
  1809. unsigned int *iface_count)
  1810. {
  1811. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1812. *status = instance->status;
  1813. if (iface_count) {
  1814. *iface_count = instance->interface_count;
  1815. }
  1816. return (0);
  1817. }
  1818. int totemrrp_crypto_set (
  1819. void *rrp_context,
  1820. unsigned int type)
  1821. {
  1822. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1823. int res;
  1824. res = totemnet_crypto_set(instance->net_handles[0], type);
  1825. return (res);
  1826. }
  1827. /*
  1828. * iface_no indicates the interface number [0, ..., interface_count-1] of the
  1829. * specific ring which will be reenabled. We specify iface_no == interface_count
  1830. * means reenabling all the rings.
  1831. */
  1832. int totemrrp_ring_reenable (
  1833. void *rrp_context,
  1834. unsigned int iface_no)
  1835. {
  1836. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1837. int res = 0;
  1838. unsigned int i;
  1839. instance->rrp_algo->ring_reenable (instance, iface_no);
  1840. if (iface_no == instance->interface_count) {
  1841. for (i = 0; i < instance->interface_count; i++) {
  1842. sprintf (instance->status[i], "ring %d active with no faults", i);
  1843. }
  1844. } else {
  1845. sprintf (instance->status[iface_no], "ring %d active with no faults", iface_no);
  1846. }
  1847. return (res);
  1848. }
  1849. extern int totemrrp_mcast_recv_empty (
  1850. void *rrp_context)
  1851. {
  1852. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1853. int res;
  1854. res = instance->rrp_algo->mcast_recv_empty (instance);
  1855. return (res);
  1856. }
  1857. int totemrrp_member_add (
  1858. void *rrp_context,
  1859. const struct totem_ip_address *member,
  1860. int iface_no)
  1861. {
  1862. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1863. int res;
  1864. res = instance->rrp_algo->member_add (instance, member, iface_no);
  1865. return (res);
  1866. }
  1867. int totemrrp_member_remove (
  1868. void *rrp_context,
  1869. const struct totem_ip_address *member,
  1870. int iface_no)
  1871. {
  1872. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1873. int res;
  1874. res = instance->rrp_algo->member_remove (instance, member, iface_no);
  1875. return (res);
  1876. }
  1877. void totemrrp_membership_changed (
  1878. void *rrp_context,
  1879. enum totem_configuration_type configuration_type,
  1880. const struct srp_addr *member_list, size_t member_list_entries,
  1881. const struct srp_addr *left_list, size_t left_list_entries,
  1882. const struct srp_addr *joined_list, size_t joined_list_entries,
  1883. const struct memb_ring_id *ring_id)
  1884. {
  1885. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1886. instance->rrp_algo->membership_changed (instance,
  1887. configuration_type,
  1888. member_list, member_list_entries,
  1889. left_list, left_list_entries,
  1890. joined_list, joined_list_entries,
  1891. ring_id);
  1892. }