totemrrp.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  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. active_instance->timer_problem_decrementer = 0;
  1272. }
  1273. /*
  1274. * active replication
  1275. */
  1276. static void active_mcast_recv (
  1277. struct totemrrp_instance *instance,
  1278. unsigned int iface_no,
  1279. void *context,
  1280. const void *msg,
  1281. unsigned int msg_len)
  1282. {
  1283. instance->totemrrp_deliver_fn (
  1284. context,
  1285. msg,
  1286. msg_len);
  1287. }
  1288. static void active_mcast_flush_send (
  1289. struct totemrrp_instance *instance,
  1290. const void *msg,
  1291. unsigned int msg_len)
  1292. {
  1293. int i;
  1294. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1295. for (i = 0; i < instance->interface_count; i++) {
  1296. if (rrp_algo_instance->faulty[i] == 0) {
  1297. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  1298. }
  1299. }
  1300. }
  1301. static void active_mcast_noflush_send (
  1302. struct totemrrp_instance *instance,
  1303. const void *msg,
  1304. unsigned int msg_len)
  1305. {
  1306. int i;
  1307. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1308. for (i = 0; i < instance->interface_count; i++) {
  1309. if (rrp_algo_instance->faulty[i] == 0) {
  1310. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  1311. }
  1312. }
  1313. }
  1314. static void active_token_recv (
  1315. struct totemrrp_instance *rrp_instance,
  1316. unsigned int iface_no,
  1317. void *context,
  1318. const void *msg,
  1319. unsigned int msg_len,
  1320. unsigned int token_seq)
  1321. {
  1322. int i;
  1323. struct active_instance *active_instance = (struct active_instance *)rrp_instance->rrp_algo_instance;
  1324. active_instance->totemrrp_context = context;
  1325. if (sq_lt_compare (active_instance->last_token_seq, token_seq)) {
  1326. memcpy (active_instance->token, msg, msg_len);
  1327. active_instance->token_len = msg_len;
  1328. for (i = 0; i < rrp_instance->interface_count; i++) {
  1329. active_instance->last_token_recv[i] = 0;
  1330. }
  1331. active_instance->last_token_recv[iface_no] = 1;
  1332. active_timer_expired_token_start (active_instance);
  1333. }
  1334. /*
  1335. * This doesn't follow spec because the spec assumes we will know
  1336. * when token resets occur.
  1337. */
  1338. active_instance->last_token_seq = token_seq;
  1339. if (token_seq == active_instance->last_token_seq) {
  1340. active_instance->last_token_recv[iface_no] = 1;
  1341. for (i = 0; i < rrp_instance->interface_count; i++) {
  1342. if ((active_instance->last_token_recv[i] == 0) &&
  1343. active_instance->faulty[i] == 0) {
  1344. return; /* don't deliver token */
  1345. }
  1346. }
  1347. active_timer_expired_token_cancel (active_instance);
  1348. rrp_instance->totemrrp_deliver_fn (
  1349. context,
  1350. msg,
  1351. msg_len);
  1352. }
  1353. }
  1354. static void active_token_send (
  1355. struct totemrrp_instance *instance,
  1356. const void *msg,
  1357. unsigned int msg_len)
  1358. {
  1359. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1360. int i;
  1361. for (i = 0; i < instance->interface_count; i++) {
  1362. if (rrp_algo_instance->faulty[i] == 0) {
  1363. totemnet_token_send (
  1364. instance->net_handles[i],
  1365. msg, msg_len);
  1366. }
  1367. }
  1368. }
  1369. static void active_recv_flush (struct totemrrp_instance *instance)
  1370. {
  1371. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1372. unsigned int i;
  1373. for (i = 0; i < instance->interface_count; i++) {
  1374. if (rrp_algo_instance->faulty[i] == 0) {
  1375. totemnet_recv_flush (instance->net_handles[i]);
  1376. }
  1377. }
  1378. }
  1379. static void active_send_flush (struct totemrrp_instance *instance)
  1380. {
  1381. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1382. unsigned int i;
  1383. for (i = 0; i < instance->interface_count; i++) {
  1384. if (rrp_algo_instance->faulty[i] == 0) {
  1385. totemnet_send_flush (instance->net_handles[i]);
  1386. }
  1387. }
  1388. }
  1389. static int active_member_add (
  1390. struct totemrrp_instance *instance,
  1391. const struct totem_ip_address *member,
  1392. unsigned int iface_no)
  1393. {
  1394. int res;
  1395. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1396. return (res);
  1397. }
  1398. static int active_member_remove (
  1399. struct totemrrp_instance *instance,
  1400. const struct totem_ip_address *member,
  1401. unsigned int iface_no)
  1402. {
  1403. int res;
  1404. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1405. return (res);
  1406. }
  1407. static void active_membership_changed (
  1408. struct totemrrp_instance *rrp_instance,
  1409. enum totem_configuration_type configuration_type,
  1410. const struct srp_addr *member_list, size_t member_list_entries,
  1411. const struct srp_addr *left_list, size_t left_list_entries,
  1412. const struct srp_addr *joined_list, size_t joined_list_entries,
  1413. const struct memb_ring_id *ring_id)
  1414. {
  1415. int i;
  1416. int interface;
  1417. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1418. for (i = 0; i < left_list_entries; i++) {
  1419. if (left_list[i].addr[interface].family != AF_INET &&
  1420. left_list[i].addr[interface].family != AF_INET6) {
  1421. log_printf(rrp_instance->totemrrp_log_level_error,
  1422. "Membership left list contains incorrect address. "
  1423. "This is sign of misconfiguration between nodes!");
  1424. } else {
  1425. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1426. &left_list[i].addr[interface], 0);
  1427. }
  1428. }
  1429. for (i = 0; i < joined_list_entries; i++) {
  1430. if (joined_list[i].addr[interface].family != AF_INET &&
  1431. joined_list[i].addr[interface].family != AF_INET6) {
  1432. log_printf(rrp_instance->totemrrp_log_level_error,
  1433. "Membership join list contains incorrect address. "
  1434. "This is sign of misconfiguration between nodes!");
  1435. } else {
  1436. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1437. &joined_list[i].addr[interface], 1);
  1438. }
  1439. }
  1440. }
  1441. }
  1442. static void active_iface_check (struct totemrrp_instance *instance)
  1443. {
  1444. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1445. unsigned int i;
  1446. for (i = 0; i < instance->interface_count; i++) {
  1447. if (rrp_algo_instance->faulty[i] == 0) {
  1448. totemnet_iface_check (instance->net_handles[i]);
  1449. }
  1450. }
  1451. }
  1452. static void active_processor_count_set (
  1453. struct totemrrp_instance *instance,
  1454. unsigned int processor_count)
  1455. {
  1456. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1457. unsigned int i;
  1458. for (i = 0; i < instance->interface_count; i++) {
  1459. if (rrp_algo_instance->faulty[i] == 0) {
  1460. totemnet_processor_count_set (instance->net_handles[i],
  1461. processor_count);
  1462. }
  1463. }
  1464. }
  1465. static void active_token_target_set (
  1466. struct totemrrp_instance *instance,
  1467. struct totem_ip_address *token_target,
  1468. unsigned int iface_no)
  1469. {
  1470. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1471. }
  1472. static int active_mcast_recv_empty (
  1473. struct totemrrp_instance *instance)
  1474. {
  1475. int res;
  1476. int msgs_emptied = 0;
  1477. int i;
  1478. for (i = 0; i < instance->interface_count; i++) {
  1479. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1480. if (res == -1) {
  1481. return (-1);
  1482. }
  1483. if (res == 1) {
  1484. msgs_emptied = 1;
  1485. }
  1486. }
  1487. return (msgs_emptied);
  1488. }
  1489. static void active_ring_reenable (
  1490. struct totemrrp_instance *instance,
  1491. unsigned int iface_no)
  1492. {
  1493. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1494. if (iface_no == instance->interface_count) {
  1495. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1496. instance->interface_count);
  1497. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1498. instance->interface_count);
  1499. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1500. instance->interface_count);
  1501. } else {
  1502. rrp_algo_instance->last_token_recv[iface_no] = 0;
  1503. rrp_algo_instance->faulty[iface_no] = 0;
  1504. rrp_algo_instance->counter_problems[iface_no] = 0;
  1505. }
  1506. }
  1507. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1508. {
  1509. memset (instance, 0, sizeof (struct totemrrp_instance));
  1510. }
  1511. static int totemrrp_algorithm_set (
  1512. struct totem_config *totem_config,
  1513. struct totemrrp_instance *instance)
  1514. {
  1515. unsigned int res = -1;
  1516. unsigned int i;
  1517. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1518. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1519. instance->rrp_algo = rrp_algos[i];
  1520. if (rrp_algos[i]->initialize) {
  1521. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1522. instance,
  1523. totem_config->interface_count);
  1524. }
  1525. res = 0;
  1526. break;
  1527. }
  1528. }
  1529. for (i = 0; i < totem_config->interface_count; i++) {
  1530. instance->status[i] = malloc (1024);
  1531. sprintf (instance->status[i], "ring %d active with no faults", i);
  1532. }
  1533. return (res);
  1534. }
  1535. void rrp_deliver_fn (
  1536. void *context,
  1537. const void *msg,
  1538. unsigned int msg_len)
  1539. {
  1540. unsigned int token_seqid;
  1541. unsigned int token_is;
  1542. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1543. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  1544. const struct message_header *hdr = msg;
  1545. struct message_header tmp_msg, activate_msg;
  1546. memset(&tmp_msg, 0, sizeof(struct message_header));
  1547. memset(&activate_msg, 0, sizeof(struct message_header));
  1548. rrp_instance->totemrrp_token_seqid_get (
  1549. msg,
  1550. &token_seqid,
  1551. &token_is);
  1552. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVE) {
  1553. log_printf (
  1554. rrp_instance->totemrrp_log_level_debug,
  1555. "received message requesting test of ring now active\n");
  1556. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1557. test_active_msg_endian_convert(hdr, &tmp_msg);
  1558. hdr = &tmp_msg;
  1559. }
  1560. if (hdr->nodeid_activator == rrp_instance->my_nodeid) {
  1561. /*
  1562. * Send an activate message
  1563. */
  1564. activate_msg.type = MESSAGE_TYPE_RING_TEST_ACTIVATE;
  1565. activate_msg.endian_detector = ENDIAN_LOCAL;
  1566. activate_msg.ring_number = hdr->ring_number;
  1567. activate_msg.nodeid_activator = rrp_instance->my_nodeid;
  1568. totemnet_token_send (
  1569. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1570. &activate_msg, sizeof (struct message_header));
  1571. } else {
  1572. /*
  1573. * Send a ring test message
  1574. */
  1575. totemnet_token_send (
  1576. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1577. msg, msg_len);
  1578. }
  1579. } else
  1580. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVATE) {
  1581. log_printf (
  1582. rrp_instance->totemrrp_log_level_notice,
  1583. "Automatically recovered ring %d\n", hdr->ring_number);
  1584. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1585. test_active_msg_endian_convert(hdr, &tmp_msg);
  1586. hdr = &tmp_msg;
  1587. }
  1588. totemrrp_ring_reenable (rrp_instance, deliver_fn_context->iface_no);
  1589. if (hdr->nodeid_activator != rrp_instance->my_nodeid) {
  1590. totemnet_token_send (
  1591. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1592. msg, msg_len);
  1593. }
  1594. } else
  1595. if (token_is) {
  1596. /*
  1597. * Deliver to the token receiver for this rrp algorithm
  1598. */
  1599. rrp_instance->rrp_algo->token_recv (
  1600. rrp_instance,
  1601. deliver_fn_context->iface_no,
  1602. deliver_fn_context->context,
  1603. msg,
  1604. msg_len,
  1605. token_seqid);
  1606. } else {
  1607. /*
  1608. * Deliver to the mcast receiver for this rrp algorithm
  1609. */
  1610. rrp_instance->rrp_algo->mcast_recv (
  1611. rrp_instance,
  1612. deliver_fn_context->iface_no,
  1613. deliver_fn_context->context,
  1614. msg,
  1615. msg_len);
  1616. }
  1617. }
  1618. void rrp_iface_change_fn (
  1619. void *context,
  1620. const struct totem_ip_address *iface_addr)
  1621. {
  1622. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1623. deliver_fn_context->instance->my_nodeid = iface_addr->nodeid;
  1624. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1625. deliver_fn_context->context,
  1626. iface_addr,
  1627. deliver_fn_context->iface_no);
  1628. }
  1629. int totemrrp_finalize (
  1630. void *rrp_context)
  1631. {
  1632. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1633. int i;
  1634. for (i = 0; i < instance->interface_count; i++) {
  1635. totemnet_finalize (instance->net_handles[i]);
  1636. }
  1637. return (0);
  1638. }
  1639. static void rrp_target_set_completed (void *context)
  1640. {
  1641. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1642. deliver_fn_context->instance->totemrrp_target_set_completed (deliver_fn_context->context);
  1643. }
  1644. /*
  1645. * Totem Redundant Ring interface
  1646. * depends on poll abstraction, POSIX, IPV4
  1647. */
  1648. /*
  1649. * Create an instance
  1650. */
  1651. int totemrrp_initialize (
  1652. hdb_handle_t poll_handle,
  1653. void **rrp_context,
  1654. struct totem_config *totem_config,
  1655. totemsrp_stats_t *stats,
  1656. void *context,
  1657. void (*deliver_fn) (
  1658. void *context,
  1659. const void *msg,
  1660. unsigned int msg_len),
  1661. void (*iface_change_fn) (
  1662. void *context,
  1663. const struct totem_ip_address *iface_addr,
  1664. unsigned int iface_no),
  1665. void (*token_seqid_get) (
  1666. const void *msg,
  1667. unsigned int *seqid,
  1668. unsigned int *token_is),
  1669. unsigned int (*msgs_missing) (void),
  1670. void (*target_set_completed) (void *context))
  1671. {
  1672. struct totemrrp_instance *instance;
  1673. unsigned int res;
  1674. int i;
  1675. instance = malloc (sizeof (struct totemrrp_instance));
  1676. if (instance == 0) {
  1677. return (-1);
  1678. }
  1679. totemrrp_instance_initialize (instance);
  1680. instance->totem_config = totem_config;
  1681. res = totemrrp_algorithm_set (
  1682. instance->totem_config,
  1683. instance);
  1684. if (res == -1) {
  1685. goto error_destroy;
  1686. }
  1687. /*
  1688. * Configure logging
  1689. */
  1690. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1691. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1692. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1693. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1694. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1695. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1696. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1697. instance->interfaces = totem_config->interfaces;
  1698. instance->poll_handle = poll_handle;
  1699. instance->totemrrp_deliver_fn = deliver_fn;
  1700. instance->totemrrp_iface_change_fn = iface_change_fn;
  1701. instance->totemrrp_token_seqid_get = token_seqid_get;
  1702. instance->totemrrp_target_set_completed = target_set_completed;
  1703. instance->totemrrp_msgs_missing = msgs_missing;
  1704. instance->interface_count = totem_config->interface_count;
  1705. instance->net_handles = malloc (sizeof (void *) * totem_config->interface_count);
  1706. instance->context = context;
  1707. instance->poll_handle = poll_handle;
  1708. for (i = 0; i < totem_config->interface_count; i++) {
  1709. struct deliver_fn_context *deliver_fn_context;
  1710. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1711. assert (deliver_fn_context);
  1712. deliver_fn_context->instance = instance;
  1713. deliver_fn_context->context = context;
  1714. deliver_fn_context->iface_no = i;
  1715. instance->deliver_fn_context[i] = (void *)deliver_fn_context;
  1716. totemnet_initialize (
  1717. poll_handle,
  1718. &instance->net_handles[i],
  1719. totem_config,
  1720. stats,
  1721. i,
  1722. (void *)deliver_fn_context,
  1723. rrp_deliver_fn,
  1724. rrp_iface_change_fn,
  1725. rrp_target_set_completed);
  1726. totemnet_net_mtu_adjust (instance->net_handles[i], totem_config);
  1727. }
  1728. *rrp_context = instance;
  1729. return (0);
  1730. error_destroy:
  1731. free (instance);
  1732. return (res);
  1733. }
  1734. int totemrrp_processor_count_set (
  1735. void *rrp_context,
  1736. unsigned int processor_count)
  1737. {
  1738. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1739. instance->rrp_algo->processor_count_set (instance, processor_count);
  1740. instance->processor_count = processor_count;
  1741. return (0);
  1742. }
  1743. int totemrrp_token_target_set (
  1744. void *rrp_context,
  1745. struct totem_ip_address *addr,
  1746. unsigned int iface_no)
  1747. {
  1748. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1749. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1750. return (0);
  1751. }
  1752. int totemrrp_recv_flush (void *rrp_context)
  1753. {
  1754. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1755. instance->rrp_algo->recv_flush (instance);
  1756. return (0);
  1757. }
  1758. int totemrrp_send_flush (void *rrp_context)
  1759. {
  1760. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1761. instance->rrp_algo->send_flush (instance);
  1762. return (0);
  1763. }
  1764. int totemrrp_token_send (
  1765. void *rrp_context,
  1766. const void *msg,
  1767. unsigned int msg_len)
  1768. {
  1769. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1770. instance->rrp_algo->token_send (instance, msg, msg_len);
  1771. return (0);
  1772. }
  1773. int totemrrp_mcast_flush_send (
  1774. void *rrp_context,
  1775. const void *msg,
  1776. unsigned int msg_len)
  1777. {
  1778. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1779. int res = 0;
  1780. // TODO this needs to return the result
  1781. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1782. return (res);
  1783. }
  1784. int totemrrp_mcast_noflush_send (
  1785. void *rrp_context,
  1786. const void *msg,
  1787. unsigned int msg_len)
  1788. {
  1789. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1790. /*
  1791. * merge detects go out through mcast_flush_send so it is safe to
  1792. * flush these messages if we are only one processor. This avoids
  1793. * an encryption/hmac and decryption/hmac
  1794. */
  1795. if (instance->processor_count > 1) {
  1796. // TODO this needs to return the result
  1797. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1798. }
  1799. return (0);
  1800. }
  1801. int totemrrp_iface_check (void *rrp_context)
  1802. {
  1803. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1804. instance->rrp_algo->iface_check (instance);
  1805. return (0);
  1806. }
  1807. int totemrrp_ifaces_get (
  1808. void *rrp_context,
  1809. char ***status,
  1810. unsigned int *iface_count)
  1811. {
  1812. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1813. *status = instance->status;
  1814. if (iface_count) {
  1815. *iface_count = instance->interface_count;
  1816. }
  1817. return (0);
  1818. }
  1819. int totemrrp_crypto_set (
  1820. void *rrp_context,
  1821. unsigned int type)
  1822. {
  1823. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1824. int res;
  1825. res = totemnet_crypto_set(instance->net_handles[0], type);
  1826. return (res);
  1827. }
  1828. /*
  1829. * iface_no indicates the interface number [0, ..., interface_count-1] of the
  1830. * specific ring which will be reenabled. We specify iface_no == interface_count
  1831. * means reenabling all the rings.
  1832. */
  1833. int totemrrp_ring_reenable (
  1834. void *rrp_context,
  1835. unsigned int iface_no)
  1836. {
  1837. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1838. int res = 0;
  1839. unsigned int i;
  1840. instance->rrp_algo->ring_reenable (instance, iface_no);
  1841. if (iface_no == instance->interface_count) {
  1842. for (i = 0; i < instance->interface_count; i++) {
  1843. sprintf (instance->status[i], "ring %d active with no faults", i);
  1844. }
  1845. } else {
  1846. sprintf (instance->status[iface_no], "ring %d active with no faults", iface_no);
  1847. }
  1848. return (res);
  1849. }
  1850. extern int totemrrp_mcast_recv_empty (
  1851. void *rrp_context)
  1852. {
  1853. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1854. int res;
  1855. res = instance->rrp_algo->mcast_recv_empty (instance);
  1856. return (res);
  1857. }
  1858. int totemrrp_member_add (
  1859. void *rrp_context,
  1860. const struct totem_ip_address *member,
  1861. int iface_no)
  1862. {
  1863. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1864. int res;
  1865. res = instance->rrp_algo->member_add (instance, member, iface_no);
  1866. return (res);
  1867. }
  1868. int totemrrp_member_remove (
  1869. void *rrp_context,
  1870. const struct totem_ip_address *member,
  1871. int iface_no)
  1872. {
  1873. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1874. int res;
  1875. res = instance->rrp_algo->member_remove (instance, member, iface_no);
  1876. return (res);
  1877. }
  1878. void totemrrp_membership_changed (
  1879. void *rrp_context,
  1880. enum totem_configuration_type configuration_type,
  1881. const struct srp_addr *member_list, size_t member_list_entries,
  1882. const struct srp_addr *left_list, size_t left_list_entries,
  1883. const struct srp_addr *joined_list, size_t joined_list_entries,
  1884. const struct memb_ring_id *ring_id)
  1885. {
  1886. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1887. instance->rrp_algo->membership_changed (instance,
  1888. configuration_type,
  1889. member_list, member_list_entries,
  1890. left_list, left_list_entries,
  1891. joined_list, joined_list_entries,
  1892. ring_id);
  1893. }