totemsrp.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753
  1. /*
  2. * Copyright (c) 2003-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006 RedHat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.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. /*
  36. * The first version of this code was based upon Yair Amir's PhD thesis:
  37. * http://www.cs.jhu.edu/~yairamir/phd.ps) (ch4,5).
  38. *
  39. * The current version of totemsrp implements the Totem protocol specified in:
  40. * http://citeseer.ist.psu.edu/amir95totem.html
  41. *
  42. * The deviations from the above published protocols are:
  43. * - encryption of message contents with SOBER128
  44. * - authentication of meessage contents with SHA1/HMAC
  45. * - token hold mode where token doesn't rotate on unused ring - reduces cpu
  46. * usage on 1.6ghz xeon from 35% to less then .1 % as measured by top
  47. */
  48. #include <assert.h>
  49. #include <pthread.h>
  50. #include <sys/mman.h>
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #include <sys/socket.h>
  54. #include <netdb.h>
  55. #include <sys/un.h>
  56. #include <sys/ioctl.h>
  57. #include <sys/param.h>
  58. #include <netinet/in.h>
  59. #include <arpa/inet.h>
  60. #include <unistd.h>
  61. #include <fcntl.h>
  62. #include <stdlib.h>
  63. #include <stdio.h>
  64. #include <errno.h>
  65. #include <signal.h>
  66. #include <sched.h>
  67. #include <time.h>
  68. #include <sys/time.h>
  69. #include <sys/poll.h>
  70. #include "aispoll.h"
  71. #include "totemsrp.h"
  72. #include "totemrrp.h"
  73. #include "wthread.h"
  74. #include "../include/queue.h"
  75. #include "../include/sq.h"
  76. #include "../include/list.h"
  77. #include "../include/hdb.h"
  78. #include "swab.h"
  79. #include "crypto.h"
  80. #define LOCALHOST_IP inet_addr("127.0.0.1")
  81. #define QUEUE_RTR_ITEMS_SIZE_MAX 256 /* allow 256 retransmit items */
  82. #define RETRANS_MESSAGE_QUEUE_SIZE_MAX 500 /* allow 500 messages to be queued */
  83. #define RECEIVED_MESSAGE_QUEUE_SIZE_MAX 500 /* allow 500 messages to be queued */
  84. #define MAXIOVS 5
  85. #define RETRANSMIT_ENTRIES_MAX 30
  86. #define MISSING_MCAST_WINDOW 128
  87. /*
  88. * Rollover handling:
  89. * SEQNO_START_MSG is the starting sequence number after a new configuration
  90. * This should remain zero, unless testing overflow in which case
  91. * 0x7ffff000 and 0xfffff000 are good starting values.
  92. *
  93. * SEQNO_START_TOKEN is the starting sequence number after a new configuration
  94. * for a token. This should remain zero, unless testing overflow in which
  95. * case 07fffff00 or 0xffffff00 are good starting values.
  96. *
  97. * SEQNO_START_MSG is the starting sequence number after a new configuration
  98. * This should remain zero, unless testing overflow in which case
  99. * 0x7ffff000 and 0xfffff000 are good values to start with
  100. */
  101. #define SEQNO_START_MSG 0x0
  102. #define SEQNO_START_TOKEN 0x0
  103. /*
  104. * These can be used ot test different rollover points
  105. * #define SEQNO_START_MSG 0xfffffe00
  106. * #define SEQNO_START_TOKEN 0xfffffe00
  107. */
  108. /*
  109. * These can be used to test the error recovery algorithms
  110. * #define TEST_DROP_ORF_TOKEN_PERCENTAGE 30
  111. * #define TEST_DROP_COMMIT_TOKEN_PERCENTAGE 30
  112. * #define TEST_DROP_MCAST_PERCENTAGE 50
  113. * #define TEST_RECOVERY_MSG_COUNT 300
  114. */
  115. /*
  116. * we compare incoming messages to determine if their endian is
  117. * different - if so convert them
  118. *
  119. * do not change
  120. */
  121. #define ENDIAN_LOCAL 0xff22
  122. enum message_type {
  123. MESSAGE_TYPE_ORF_TOKEN = 0, /* Ordering, Reliability, Flow (ORF) control Token */
  124. MESSAGE_TYPE_MCAST = 1, /* ring ordered multicast message */
  125. MESSAGE_TYPE_MEMB_MERGE_DETECT = 2, /* merge rings if there are available rings */
  126. MESSAGE_TYPE_MEMB_JOIN = 3, /* membership join message */
  127. MESSAGE_TYPE_MEMB_COMMIT_TOKEN = 4, /* membership commit token */
  128. MESSAGE_TYPE_TOKEN_HOLD_CANCEL = 5, /* cancel the holding of the token */
  129. };
  130. /*
  131. * New membership algorithm local variables
  132. */
  133. struct consensus_list_item {
  134. struct totem_ip_address addr;
  135. int set;
  136. };
  137. struct token_callback_instance {
  138. struct list_head list;
  139. int (*callback_fn) (enum totem_callback_token_type type, void *);
  140. enum totem_callback_token_type callback_type;
  141. int delete;
  142. void *data;
  143. };
  144. struct totemsrp_socket {
  145. int mcast;
  146. int token;
  147. };
  148. struct message_header {
  149. char type;
  150. char encapsulated;
  151. unsigned short endian_detector;
  152. unsigned int nodeid;
  153. } __attribute__((packed));
  154. struct mcast {
  155. struct message_header header;
  156. unsigned int seq;
  157. int this_seqno;
  158. struct memb_ring_id ring_id;
  159. struct totem_ip_address source;
  160. int guarantee;
  161. } __attribute__((packed));
  162. /*
  163. * MTU - multicast message header - IP header - UDP header
  164. *
  165. * On lossy switches, making use of the DF UDP flag can lead to loss of
  166. * forward progress. So the packets must be fragmented by a higher layer
  167. *
  168. * This layer can only handle packets of MTU size.
  169. */
  170. #define FRAGMENT_SIZE (FRAME_SIZE_MAX - sizeof (struct mcast) - 20 - 8)
  171. struct rtr_item {
  172. struct memb_ring_id ring_id;
  173. unsigned int seq;
  174. }__attribute__((packed));
  175. struct orf_token {
  176. struct message_header header;
  177. unsigned int seq;
  178. unsigned int token_seq;
  179. unsigned int aru;
  180. struct totem_ip_address aru_addr;
  181. struct memb_ring_id ring_id;
  182. unsigned int backlog;
  183. unsigned int fcc;
  184. int retrans_flg;
  185. int rtr_list_entries;
  186. struct rtr_item rtr_list[0];
  187. }__attribute__((packed));
  188. struct memb_join {
  189. struct message_header header;
  190. struct totem_ip_address proc_list[PROCESSOR_COUNT_MAX];
  191. int proc_list_entries;
  192. struct totem_ip_address failed_list[PROCESSOR_COUNT_MAX];
  193. int failed_list_entries;
  194. unsigned long long ring_seq;
  195. } __attribute__((packed));
  196. struct memb_merge_detect {
  197. struct message_header header;
  198. struct memb_ring_id ring_id;
  199. } __attribute__((packed));
  200. struct token_hold_cancel {
  201. struct message_header header;
  202. struct memb_ring_id ring_id;
  203. } __attribute__((packed));
  204. struct memb_commit_token_memb_entry {
  205. struct memb_ring_id ring_id;
  206. unsigned int aru;
  207. unsigned int high_delivered;
  208. unsigned int received_flg;
  209. }__attribute__((packed));
  210. struct memb_commit_token {
  211. struct message_header header;
  212. unsigned int token_seq;
  213. struct memb_ring_id ring_id;
  214. unsigned int retrans_flg;
  215. int memb_index;
  216. int addr_entries;
  217. struct totem_ip_address addr[PROCESSOR_COUNT_MAX];
  218. struct memb_commit_token_memb_entry memb_list[PROCESSOR_COUNT_MAX];
  219. }__attribute__((packed));
  220. struct message_item {
  221. struct mcast *mcast;
  222. struct iovec iovec[MAXIOVS];
  223. int iov_len;
  224. };
  225. struct sort_queue_item {
  226. struct iovec iovec[MAXIOVS];
  227. int iov_len;
  228. };
  229. struct orf_token_mcast_thread_state {
  230. char iobuf[9000];
  231. prng_state prng_state;
  232. };
  233. enum memb_state {
  234. MEMB_STATE_OPERATIONAL = 1,
  235. MEMB_STATE_GATHER = 2,
  236. MEMB_STATE_COMMIT = 3,
  237. MEMB_STATE_RECOVERY = 4
  238. };
  239. struct totemsrp_instance {
  240. int first_run;
  241. /*
  242. * Flow control mcasts and remcasts on last and current orf_token
  243. */
  244. int fcc_remcast_last;
  245. int fcc_mcast_last;
  246. int fcc_mcast_current;
  247. int fcc_remcast_current;
  248. struct consensus_list_item consensus_list[PROCESSOR_COUNT_MAX];
  249. int consensus_list_entries;
  250. struct totem_ip_address my_proc_list[PROCESSOR_COUNT_MAX];
  251. struct totem_ip_address my_failed_list[PROCESSOR_COUNT_MAX];
  252. struct totem_ip_address my_new_memb_list[PROCESSOR_COUNT_MAX];
  253. struct totem_ip_address my_trans_memb_list[PROCESSOR_COUNT_MAX];
  254. struct totem_ip_address my_memb_list[PROCESSOR_COUNT_MAX];
  255. struct totem_ip_address my_deliver_memb_list[PROCESSOR_COUNT_MAX];
  256. struct totem_ip_address my_nodeid_lookup_list[PROCESSOR_COUNT_MAX];
  257. int my_proc_list_entries;
  258. int my_failed_list_entries;
  259. int my_new_memb_entries;
  260. int my_trans_memb_entries;
  261. int my_memb_entries;
  262. int my_deliver_memb_entries;
  263. int my_nodeid_lookup_entries;
  264. struct memb_ring_id my_ring_id;
  265. struct memb_ring_id my_old_ring_id;
  266. int my_aru_count;
  267. int my_merge_detect_timeout_outstanding;
  268. unsigned int my_last_aru;
  269. int my_seq_unchanged;
  270. int my_received_flg;
  271. unsigned int my_high_seq_received;
  272. unsigned int my_install_seq;
  273. int my_rotation_counter;
  274. int my_set_retrans_flg;
  275. int my_retrans_flg_count;
  276. unsigned int my_high_ring_delivered;
  277. int heartbeat_timeout;
  278. /*
  279. * Queues used to order, deliver, and recover messages
  280. */
  281. struct queue new_message_queue;
  282. struct queue retrans_message_queue;
  283. struct sq regular_sort_queue;
  284. struct sq recovery_sort_queue;
  285. /*
  286. * Received up to and including
  287. */
  288. unsigned int my_aru;
  289. unsigned int my_high_delivered;
  290. struct list_head token_callback_received_listhead;
  291. struct list_head token_callback_sent_listhead;
  292. char *orf_token_retransmit; // sizeof (struct orf_token) + sizeof (struct rtr_item) * RETRANSMIT_ENTRIES_MAX];
  293. int orf_token_retransmit_size;
  294. unsigned int my_token_seq;
  295. /*
  296. * Timers
  297. */
  298. poll_timer_handle timer_orf_token_timeout;
  299. poll_timer_handle timer_orf_token_retransmit_timeout;
  300. poll_timer_handle timer_orf_token_hold_retransmit_timeout;
  301. poll_timer_handle timer_merge_detect_timeout;
  302. poll_timer_handle memb_timer_state_gather_join_timeout;
  303. poll_timer_handle memb_timer_state_gather_consensus_timeout;
  304. poll_timer_handle memb_timer_state_commit_timeout;
  305. poll_timer_handle timer_heartbeat_timeout;
  306. /*
  307. * Function and data used to log messages
  308. */
  309. int totemsrp_log_level_security;
  310. int totemsrp_log_level_error;
  311. int totemsrp_log_level_warning;
  312. int totemsrp_log_level_notice;
  313. int totemsrp_log_level_debug;
  314. void (*totemsrp_log_printf) (int level, char *format, ...);
  315. enum memb_state memb_state;
  316. struct totem_ip_address my_id;
  317. struct totem_ip_address next_memb;
  318. char iov_buffer[FRAME_SIZE_MAX];
  319. struct iovec totemsrp_iov_recv;
  320. poll_handle totemsrp_poll_handle;
  321. /*
  322. * Function called when new message received
  323. */
  324. int (*totemsrp_recv) (char *group, struct iovec *iovec, int iov_len);
  325. struct totem_ip_address mcast_address;
  326. void (*totemsrp_deliver_fn) (
  327. struct totem_ip_address *source_addr,
  328. struct iovec *iovec,
  329. int iov_len,
  330. int endian_conversion_required);
  331. void (*totemsrp_confchg_fn) (
  332. enum totem_configuration_type configuration_type,
  333. struct totem_ip_address *member_list, int member_list_entries,
  334. struct totem_ip_address *left_list, int left_list_entries,
  335. struct totem_ip_address *joined_list, int joined_list_entries,
  336. struct memb_ring_id *ring_id);
  337. int global_seqno;
  338. int my_token_held;
  339. unsigned long long token_ring_id_seq;
  340. unsigned int last_released;
  341. unsigned int set_aru;
  342. int old_ring_state_saved;
  343. int old_ring_state_aru;
  344. unsigned int old_ring_state_high_seq_received;
  345. int ring_saved;
  346. unsigned int my_last_seq;
  347. struct timeval tv_old;
  348. totemrrp_handle totemrrp_handle;
  349. struct totem_config *totem_config;
  350. unsigned int use_heartbeat;
  351. unsigned int my_trc;
  352. unsigned int my_pbl;
  353. };
  354. struct message_handlers {
  355. int count;
  356. int (*handler_functions[6]) (
  357. struct totemsrp_instance *instance,
  358. struct totem_ip_address *system_from,
  359. void *msg,
  360. int msg_len,
  361. int endian_conversion_needed);
  362. };
  363. /*
  364. * forward decls
  365. */
  366. static int message_handler_orf_token (
  367. struct totemsrp_instance *instance,
  368. struct totem_ip_address *system_from,
  369. void *msg,
  370. int msg_len,
  371. int endian_conversion_needed);
  372. static int message_handler_mcast (
  373. struct totemsrp_instance *instance,
  374. struct totem_ip_address *system_from,
  375. void *msg,
  376. int msg_len,
  377. int endian_conversion_needed);
  378. static int message_handler_memb_merge_detect (
  379. struct totemsrp_instance *instance,
  380. struct totem_ip_address *system_from,
  381. void *msg,
  382. int msg_len,
  383. int endian_conversion_needed);
  384. static int message_handler_memb_join (
  385. struct totemsrp_instance *instance,
  386. struct totem_ip_address *system_from,
  387. void *msg,
  388. int msg_len,
  389. int endian_conversion_needed);
  390. static int message_handler_memb_commit_token (
  391. struct totemsrp_instance *instance,
  392. struct totem_ip_address *system_from,
  393. void *msg,
  394. int msg_len,
  395. int endian_conversion_needed);
  396. static int message_handler_token_hold_cancel (
  397. struct totemsrp_instance *instance,
  398. struct totem_ip_address *system_from,
  399. void *msg,
  400. int msg_len,
  401. int endian_conversion_needed);
  402. static void memb_ring_id_create_or_load (struct totemsrp_instance *, struct memb_ring_id *);
  403. static void token_callbacks_execute (struct totemsrp_instance *instance, enum totem_callback_token_type type);
  404. static void memb_state_gather_enter (struct totemsrp_instance *instance);
  405. static void messages_deliver_to_app (struct totemsrp_instance *instance, int skip, unsigned int end_point);
  406. static int orf_token_mcast (struct totemsrp_instance *instance, struct orf_token *oken,
  407. int fcc_mcasts_allowed, struct totem_ip_address *system_from);
  408. static void messages_free (struct totemsrp_instance *instance, unsigned int token_aru);
  409. static void memb_ring_id_store (struct totemsrp_instance *instance, struct memb_commit_token *commit_token);
  410. static void memb_state_commit_token_update (struct totemsrp_instance *instance, struct memb_commit_token *memb_commit_token);
  411. static int memb_state_commit_token_send (struct totemsrp_instance *instance, struct memb_commit_token *memb_commit_token);
  412. static void memb_state_commit_token_create (struct totemsrp_instance *instance, struct memb_commit_token *commit_token);
  413. static int token_hold_cancel_send (struct totemsrp_instance *instance);
  414. static void orf_token_endian_convert (struct orf_token *in, struct orf_token *out);
  415. static void memb_commit_token_endian_convert (struct memb_commit_token *in, struct memb_commit_token *out);
  416. static void memb_join_endian_convert (struct memb_join *in, struct memb_join *out);
  417. static void mcast_endian_convert (struct mcast *in, struct mcast *out);
  418. static void memb_merge_detect_endian_convert (
  419. struct memb_merge_detect *in,
  420. struct memb_merge_detect *out);
  421. static void timer_function_orf_token_timeout (void *data);
  422. static void timer_function_heartbeat_timeout (void *data);
  423. static void timer_function_token_retransmit_timeout (void *data);
  424. static void timer_function_token_hold_retransmit_timeout (void *data);
  425. static void timer_function_merge_detect_timeout (void *data);
  426. void main_deliver_fn (
  427. void *context,
  428. struct totem_ip_address *system_from,
  429. void *msg,
  430. int msg_len);
  431. void main_iface_change_fn (
  432. void *context,
  433. struct totem_ip_address *iface_address);
  434. /*
  435. * All instances in one database
  436. */
  437. static struct hdb_handle_database totemsrp_instance_database = {
  438. .handle_count = 0,
  439. .handles = 0,
  440. .iterator = 0
  441. };
  442. struct message_handlers totemsrp_message_handlers = {
  443. 6,
  444. {
  445. message_handler_orf_token,
  446. message_handler_mcast,
  447. message_handler_memb_merge_detect,
  448. message_handler_memb_join,
  449. message_handler_memb_commit_token,
  450. message_handler_token_hold_cancel
  451. }
  452. };
  453. void totemsrp_instance_initialize (struct totemsrp_instance *instance)
  454. {
  455. memset (instance, 0, sizeof (struct totemsrp_instance));
  456. list_init (&instance->token_callback_received_listhead);
  457. list_init (&instance->token_callback_sent_listhead);
  458. instance->my_received_flg = 0;
  459. instance->my_token_seq = SEQNO_START_TOKEN - 1;
  460. instance->orf_token_retransmit = malloc (15000);
  461. instance->memb_state = MEMB_STATE_OPERATIONAL;
  462. instance->set_aru = -1;
  463. instance->my_aru = SEQNO_START_MSG;
  464. instance->my_high_seq_received = SEQNO_START_MSG;
  465. instance->my_high_delivered = SEQNO_START_MSG;
  466. }
  467. void main_token_seqid_get (
  468. void *msg,
  469. unsigned int *seqid,
  470. unsigned int *token_is)
  471. {
  472. struct orf_token *token = (struct orf_token *)msg;
  473. *seqid = 0;
  474. *token_is = 0;
  475. if (token->header.type == MESSAGE_TYPE_ORF_TOKEN) {
  476. *seqid = token->seq;
  477. *token_is = 1;
  478. }
  479. }
  480. /*
  481. * Exported interfaces
  482. */
  483. int totemsrp_initialize (
  484. poll_handle poll_handle,
  485. totemsrp_handle *handle,
  486. struct totem_config *totem_config,
  487. void (*deliver_fn) (
  488. struct totem_ip_address *source_addr,
  489. struct iovec *iovec,
  490. int iov_len,
  491. int endian_conversion_required),
  492. void (*confchg_fn) (
  493. enum totem_configuration_type configuration_type,
  494. struct totem_ip_address *member_list, int member_list_entries,
  495. struct totem_ip_address *left_list, int left_list_entries,
  496. struct totem_ip_address *joined_list, int joined_list_entries,
  497. struct memb_ring_id *ring_id))
  498. {
  499. struct totemsrp_instance *instance;
  500. unsigned int res;
  501. res = hdb_handle_create (&totemsrp_instance_database,
  502. sizeof (struct totemsrp_instance), handle);
  503. if (res != 0) {
  504. goto error_exit;
  505. }
  506. res = hdb_handle_get (&totemsrp_instance_database, *handle,
  507. (void *)&instance);
  508. if (res != 0) {
  509. goto error_destroy;
  510. }
  511. totemsrp_instance_initialize (instance);
  512. instance->totem_config = totem_config;
  513. /*
  514. * Configure logging
  515. */
  516. instance->totemsrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  517. instance->totemsrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  518. instance->totemsrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  519. instance->totemsrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  520. instance->totemsrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  521. instance->totemsrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  522. /*
  523. * Initialize local variables for totemsrp
  524. */
  525. totemip_copy (&instance->mcast_address, &totem_config->mcast_addr);
  526. memset (&instance->next_memb, 0, sizeof (struct totem_ip_address));
  527. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  528. /*
  529. * Display totem configuration
  530. */
  531. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  532. "Token Timeout (%d ms) retransmit timeout (%d ms)\n",
  533. totem_config->token_timeout, totem_config->token_retransmit_timeout);
  534. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  535. "token hold (%d ms) retransmits before loss (%d retrans)\n",
  536. totem_config->token_hold_timeout, totem_config->token_retransmits_before_loss_const);
  537. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  538. "join (%d ms) consensus (%d ms) merge (%d ms)\n",
  539. totem_config->join_timeout, totem_config->consensus_timeout,
  540. totem_config->merge_timeout);
  541. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  542. "downcheck (%d ms) fail to recv const (%d msgs)\n",
  543. totem_config->downcheck_timeout, totem_config->fail_to_recv_const);
  544. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  545. "seqno unchanged const (%d rotations) Maximum network MTU %d\n", totem_config->seqno_unchanged_const, totem_config->net_mtu);
  546. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  547. "window size per rotation (%d messages) maximum messages per rotation (%d messages)\n",
  548. totem_config->window_size, totem_config->max_messages);
  549. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  550. "send threads (%d threads)\n", totem_config->threads);
  551. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  552. "heartbeat_failures_allowed (%d)\n", totem_config->heartbeat_failures_allowed);
  553. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  554. "max_network_delay (%d ms)\n", totem_config->max_network_delay);
  555. queue_init (&instance->retrans_message_queue, RETRANS_MESSAGE_QUEUE_SIZE_MAX,
  556. sizeof (struct message_item));
  557. sq_init (&instance->regular_sort_queue,
  558. QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct sort_queue_item), 0);
  559. sq_init (&instance->recovery_sort_queue,
  560. QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct sort_queue_item), 0);
  561. instance->totemsrp_poll_handle = poll_handle;
  562. instance->totemsrp_deliver_fn = deliver_fn;
  563. instance->totemsrp_confchg_fn = confchg_fn;
  564. instance->use_heartbeat = 1;
  565. if ( totem_config->heartbeat_failures_allowed == 0 ) {
  566. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  567. "HeartBeat is Disabled. To enable set heartbeat_failures_allowed > 0\n");
  568. instance->use_heartbeat = 0;
  569. }
  570. if (instance->use_heartbeat) {
  571. instance->heartbeat_timeout
  572. = (totem_config->heartbeat_failures_allowed) * totem_config->token_retransmit_timeout
  573. + totem_config->max_network_delay;
  574. if (instance->heartbeat_timeout >= totem_config->token_timeout) {
  575. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  576. "total heartbeat_timeout (%d ms) is not less than token timeout (%d ms)\n",
  577. instance->heartbeat_timeout,
  578. totem_config->token_timeout);
  579. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  580. "heartbeat_timeout = heartbeat_failures_allowed * token_retransmit_timeout + max_network_delay\n");
  581. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  582. "heartbeat timeout should be less than the token timeout. HeartBeat is Diabled !!\n");
  583. instance->use_heartbeat = 0;
  584. }
  585. else {
  586. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  587. "total heartbeat_timeout (%d ms)\n", instance->heartbeat_timeout);
  588. }
  589. }
  590. totemrrp_initialize (
  591. poll_handle,
  592. &instance->totemrrp_handle,
  593. totem_config,
  594. instance,
  595. main_deliver_fn,
  596. main_iface_change_fn,
  597. main_token_seqid_get);
  598. /*
  599. * Must have net_mtu adjusted by totemrrp_initialize first
  600. */
  601. queue_init (&instance->new_message_queue,
  602. (MESSAGE_SIZE_MAX / (totem_config->net_mtu - 25) /* for totempg_mcat header */),
  603. sizeof (struct message_item));
  604. return (0);
  605. error_destroy:
  606. hdb_handle_destroy (&totemsrp_instance_database, *handle);
  607. error_exit:
  608. return (-1);
  609. }
  610. void totemsrp_finalize (
  611. totemsrp_handle handle)
  612. {
  613. struct totemsrp_instance *instance;
  614. unsigned int res;
  615. res = hdb_handle_get (&totemsrp_instance_database, handle,
  616. (void *)&instance);
  617. if (res != 0) {
  618. return;
  619. }
  620. hdb_handle_put (&totemsrp_instance_database, handle);
  621. }
  622. /*
  623. * Set operations for use by the membership algorithm
  624. */
  625. static void memb_consensus_reset (struct totemsrp_instance *instance)
  626. {
  627. instance->consensus_list_entries = 0;
  628. }
  629. static void memb_set_subtract (
  630. struct totem_ip_address *out_list, int *out_list_entries,
  631. struct totem_ip_address *one_list, int one_list_entries,
  632. struct totem_ip_address *two_list, int two_list_entries)
  633. {
  634. int found = 0;
  635. int i;
  636. int j;
  637. *out_list_entries = 0;
  638. for (i = 0; i < one_list_entries; i++) {
  639. for (j = 0; j < two_list_entries; j++) {
  640. if (totemip_equal(&one_list[i], &two_list[j])) {
  641. found = 1;
  642. break;
  643. }
  644. }
  645. if (found == 0) {
  646. totemip_copy(&out_list[*out_list_entries], &one_list[i]);
  647. *out_list_entries = *out_list_entries + 1;
  648. }
  649. found = 0;
  650. }
  651. }
  652. /*
  653. * Set consensus for a specific processor
  654. */
  655. static void memb_consensus_set (
  656. struct totemsrp_instance *instance,
  657. struct totem_ip_address *addr)
  658. {
  659. int found = 0;
  660. int i;
  661. for (i = 0; i < instance->consensus_list_entries; i++) {
  662. if (totemip_equal(addr, &instance->consensus_list[i].addr)) {
  663. found = 1;
  664. break; /* found entry */
  665. }
  666. }
  667. totemip_copy(&instance->consensus_list[i].addr, addr);
  668. instance->consensus_list[i].set = 1;
  669. if (found == 0) {
  670. instance->consensus_list_entries++;
  671. }
  672. return;
  673. }
  674. /*
  675. * Is consensus set for a specific processor
  676. */
  677. static int memb_consensus_isset (
  678. struct totemsrp_instance *instance,
  679. struct totem_ip_address *addr)
  680. {
  681. int i;
  682. for (i = 0; i < instance->consensus_list_entries; i++) {
  683. if (totemip_equal(addr, &instance->consensus_list[i].addr)) {
  684. return (instance->consensus_list[i].set);
  685. }
  686. }
  687. return (0);
  688. }
  689. /*
  690. * Is consensus agreed upon based upon consensus database
  691. */
  692. static int memb_consensus_agreed (
  693. struct totemsrp_instance *instance)
  694. {
  695. struct totem_ip_address token_memb[PROCESSOR_COUNT_MAX];
  696. int token_memb_entries = 0;
  697. int agreed = 1;
  698. int i;
  699. memb_set_subtract (token_memb, &token_memb_entries,
  700. instance->my_proc_list, instance->my_proc_list_entries,
  701. instance->my_failed_list, instance->my_failed_list_entries);
  702. for (i = 0; i < token_memb_entries; i++) {
  703. if (memb_consensus_isset (instance, &token_memb[i]) == 0) {
  704. agreed = 0;
  705. break;
  706. }
  707. }
  708. assert (token_memb_entries >= 1);
  709. return (agreed);
  710. }
  711. static void memb_consensus_notset (
  712. struct totemsrp_instance *instance,
  713. struct totem_ip_address *no_consensus_list,
  714. int *no_consensus_list_entries,
  715. struct totem_ip_address *comparison_list,
  716. int comparison_list_entries)
  717. {
  718. int i;
  719. *no_consensus_list_entries = 0;
  720. for (i = 0; i < instance->my_proc_list_entries; i++) {
  721. if (memb_consensus_isset (instance, &instance->my_proc_list[i]) == 0) {
  722. totemip_copy(&no_consensus_list[*no_consensus_list_entries], &instance->my_proc_list[i]);
  723. *no_consensus_list_entries = *no_consensus_list_entries + 1;
  724. }
  725. }
  726. }
  727. /*
  728. * Is set1 equal to set2 Entries can be in different orders
  729. */
  730. static int memb_set_equal (struct totem_ip_address *set1, int set1_entries,
  731. struct totem_ip_address *set2, int set2_entries)
  732. {
  733. int i;
  734. int j;
  735. int found = 0;
  736. if (set1_entries != set2_entries) {
  737. return (0);
  738. }
  739. for (i = 0; i < set2_entries; i++) {
  740. for (j = 0; j < set1_entries; j++) {
  741. if (totemip_equal(&set1[j], &set2[i])) {
  742. found = 1;
  743. break;
  744. }
  745. }
  746. if (found == 0) {
  747. return (0);
  748. }
  749. found = 0;
  750. }
  751. return (1);
  752. }
  753. /*
  754. * Is subset fully contained in fullset
  755. */
  756. static int memb_set_subset (
  757. struct totem_ip_address *subset, int subset_entries,
  758. struct totem_ip_address *fullset, int fullset_entries)
  759. {
  760. int i;
  761. int j;
  762. int found = 0;
  763. if (subset_entries > fullset_entries) {
  764. return (0);
  765. }
  766. for (i = 0; i < subset_entries; i++) {
  767. for (j = 0; j < fullset_entries; j++) {
  768. if (totemip_equal(&subset[i], &fullset[j])) {
  769. found = 1;
  770. }
  771. }
  772. if (found == 0) {
  773. return (0);
  774. }
  775. found = 1;
  776. }
  777. return (1);
  778. }
  779. /*
  780. * merge subset into fullset taking care not to add duplicates
  781. */
  782. static void memb_set_merge (
  783. struct totem_ip_address *subset, int subset_entries,
  784. struct totem_ip_address *fullset, int *fullset_entries)
  785. {
  786. int found = 0;
  787. int i;
  788. int j;
  789. for (i = 0; i < subset_entries; i++) {
  790. for (j = 0; j < *fullset_entries; j++) {
  791. if (totemip_equal(&fullset[j], &subset[i])) {
  792. found = 1;
  793. break;
  794. }
  795. }
  796. if (found == 0) {
  797. totemip_copy(&fullset[j], &subset[i]);
  798. *fullset_entries = *fullset_entries + 1;
  799. }
  800. found = 0;
  801. }
  802. return;
  803. }
  804. static void memb_set_and (
  805. struct totem_ip_address *set1, int set1_entries,
  806. struct totem_ip_address *set2, int set2_entries,
  807. struct totem_ip_address *and, int *and_entries)
  808. {
  809. int i;
  810. int j;
  811. int found = 0;
  812. *and_entries = 0;
  813. for (i = 0; i < set2_entries; i++) {
  814. for (j = 0; j < set1_entries; j++) {
  815. if (totemip_equal(&set1[j], &set2[i])) {
  816. found = 1;
  817. break;
  818. }
  819. }
  820. if (found) {
  821. totemip_copy(&and[*and_entries], &set1[j]);
  822. *and_entries = *and_entries + 1;
  823. }
  824. found = 0;
  825. }
  826. return;
  827. }
  828. #ifdef CODE_COVERAGE
  829. static void memb_set_print (
  830. char *string,
  831. struct totem_ip_address *list,
  832. int list_entries)
  833. {
  834. int i;
  835. printf ("List '%s' contains %d entries:\n", string, list_entries);
  836. for (i = 0; i < list_entries; i++) {
  837. printf ("addr %s\n", totemip_print (&list[i]));
  838. }
  839. }
  840. #endif
  841. static void reset_token_retransmit_timeout (struct totemsrp_instance *instance)
  842. {
  843. poll_timer_delete (instance->totemsrp_poll_handle,
  844. instance->timer_orf_token_retransmit_timeout);
  845. poll_timer_add (instance->totemsrp_poll_handle,
  846. instance->totem_config->token_retransmit_timeout,
  847. (void *)instance,
  848. timer_function_token_retransmit_timeout,
  849. &instance->timer_orf_token_retransmit_timeout);
  850. }
  851. static void start_merge_detect_timeout (struct totemsrp_instance *instance)
  852. {
  853. if (instance->my_merge_detect_timeout_outstanding == 0) {
  854. poll_timer_add (instance->totemsrp_poll_handle,
  855. instance->totem_config->merge_timeout,
  856. (void *)instance,
  857. timer_function_merge_detect_timeout,
  858. &instance->timer_merge_detect_timeout);
  859. instance->my_merge_detect_timeout_outstanding = 1;
  860. }
  861. }
  862. static void cancel_merge_detect_timeout (struct totemsrp_instance *instance)
  863. {
  864. poll_timer_delete (instance->totemsrp_poll_handle, instance->timer_merge_detect_timeout);
  865. instance->my_merge_detect_timeout_outstanding = 0;
  866. }
  867. /*
  868. * ring_state_* is used to save and restore the sort queue
  869. * state when a recovery operation fails (and enters gather)
  870. */
  871. static void old_ring_state_save (struct totemsrp_instance *instance)
  872. {
  873. if (instance->old_ring_state_saved == 0) {
  874. instance->old_ring_state_saved = 1;
  875. instance->old_ring_state_aru = instance->my_aru;
  876. instance->old_ring_state_high_seq_received = instance->my_high_seq_received;
  877. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  878. "Saving state aru %x high seq received %x\n",
  879. instance->my_aru, instance->my_high_seq_received);
  880. }
  881. }
  882. static void ring_save (struct totemsrp_instance *instance)
  883. {
  884. if (instance->ring_saved == 0) {
  885. instance->ring_saved = 1;
  886. memcpy (&instance->my_old_ring_id, &instance->my_ring_id,
  887. sizeof (struct memb_ring_id));
  888. }
  889. }
  890. static void ring_reset (struct totemsrp_instance *instance)
  891. {
  892. instance->ring_saved = 0;
  893. }
  894. static void ring_state_restore (struct totemsrp_instance *instance)
  895. {
  896. if (instance->old_ring_state_saved) {
  897. totemip_zero_set(&instance->my_ring_id.rep);
  898. instance->my_aru = instance->old_ring_state_aru;
  899. instance->my_high_seq_received = instance->old_ring_state_high_seq_received;
  900. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  901. "Restoring instance->my_aru %x my high seq received %x\n",
  902. instance->my_aru, instance->my_high_seq_received);
  903. }
  904. }
  905. static void old_ring_state_reset (struct totemsrp_instance *instance)
  906. {
  907. instance->old_ring_state_saved = 0;
  908. }
  909. static void reset_token_timeout (struct totemsrp_instance *instance) {
  910. poll_timer_delete (instance->totemsrp_poll_handle, instance->timer_orf_token_timeout);
  911. poll_timer_add (instance->totemsrp_poll_handle,
  912. instance->totem_config->token_timeout,
  913. (void *)instance,
  914. timer_function_orf_token_timeout,
  915. &instance->timer_orf_token_timeout);
  916. }
  917. static void reset_heartbeat_timeout (struct totemsrp_instance *instance) {
  918. poll_timer_delete (instance->totemsrp_poll_handle, instance->timer_heartbeat_timeout);
  919. poll_timer_add (instance->totemsrp_poll_handle,
  920. instance->heartbeat_timeout,
  921. (void *)instance,
  922. timer_function_heartbeat_timeout,
  923. &instance->timer_heartbeat_timeout);
  924. }
  925. static void cancel_token_timeout (struct totemsrp_instance *instance) {
  926. poll_timer_delete (instance->totemsrp_poll_handle, instance->timer_orf_token_timeout);
  927. }
  928. static void cancel_heartbeat_timeout (struct totemsrp_instance *instance) {
  929. poll_timer_delete (instance->totemsrp_poll_handle, instance->timer_heartbeat_timeout);
  930. }
  931. static void cancel_token_retransmit_timeout (struct totemsrp_instance *instance)
  932. {
  933. poll_timer_delete (instance->totemsrp_poll_handle, instance->timer_orf_token_retransmit_timeout);
  934. }
  935. static void start_token_hold_retransmit_timeout (struct totemsrp_instance *instance)
  936. {
  937. poll_timer_add (instance->totemsrp_poll_handle,
  938. instance->totem_config->token_hold_timeout,
  939. (void *)instance,
  940. timer_function_token_hold_retransmit_timeout,
  941. &instance->timer_orf_token_hold_retransmit_timeout);
  942. }
  943. static void cancel_token_hold_retransmit_timeout (struct totemsrp_instance *instance)
  944. {
  945. poll_timer_delete (instance->totemsrp_poll_handle,
  946. instance->timer_orf_token_hold_retransmit_timeout);
  947. }
  948. static void memb_state_consensus_timeout_expired (
  949. struct totemsrp_instance *instance)
  950. {
  951. struct totem_ip_address no_consensus_list[PROCESSOR_COUNT_MAX];
  952. int no_consensus_list_entries;
  953. if (memb_consensus_agreed (instance)) {
  954. memb_consensus_reset (instance);
  955. memb_consensus_set (instance, &instance->my_id);
  956. reset_token_timeout (instance); // REVIEWED
  957. } else {
  958. memb_consensus_notset (instance, no_consensus_list,
  959. &no_consensus_list_entries,
  960. instance->my_proc_list, instance->my_proc_list_entries);
  961. memb_set_merge (no_consensus_list, no_consensus_list_entries,
  962. instance->my_failed_list, &instance->my_failed_list_entries);
  963. memb_state_gather_enter (instance);
  964. }
  965. }
  966. static void memb_join_message_send (struct totemsrp_instance *instance);
  967. static void memb_merge_detect_transmit (struct totemsrp_instance *instance);
  968. /*
  969. * Timers used for various states of the membership algorithm
  970. */
  971. static void timer_function_orf_token_timeout (void *data)
  972. {
  973. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  974. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  975. "The token was lost in state %d from timer %x\n", instance->memb_state, data);
  976. switch (instance->memb_state) {
  977. case MEMB_STATE_OPERATIONAL:
  978. totemrrp_iface_check (instance->totemrrp_handle);
  979. memb_state_gather_enter (instance);
  980. break;
  981. case MEMB_STATE_GATHER:
  982. memb_state_consensus_timeout_expired (instance);
  983. memb_state_gather_enter (instance);
  984. break;
  985. case MEMB_STATE_COMMIT:
  986. memb_state_gather_enter (instance);
  987. break;
  988. case MEMB_STATE_RECOVERY:
  989. ring_state_restore (instance);
  990. memb_state_gather_enter (instance);
  991. break;
  992. }
  993. }
  994. static void timer_function_heartbeat_timeout (void *data)
  995. {
  996. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  997. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  998. "HeartBeat Timer expired Invoking token loss mechanism in state %d \n", instance->memb_state);
  999. timer_function_orf_token_timeout(data);
  1000. }
  1001. static void memb_timer_function_state_gather (void *data)
  1002. {
  1003. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1004. switch (instance->memb_state) {
  1005. case MEMB_STATE_OPERATIONAL:
  1006. case MEMB_STATE_RECOVERY:
  1007. assert (0); /* this should never happen */
  1008. break;
  1009. case MEMB_STATE_GATHER:
  1010. case MEMB_STATE_COMMIT:
  1011. memb_join_message_send (instance);
  1012. /*
  1013. * Restart the join timeout
  1014. `*/
  1015. poll_timer_delete (instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1016. poll_timer_add (instance->totemsrp_poll_handle,
  1017. instance->totem_config->join_timeout,
  1018. (void *)instance,
  1019. memb_timer_function_state_gather,
  1020. &instance->memb_timer_state_gather_join_timeout);
  1021. break;
  1022. }
  1023. }
  1024. static void memb_timer_function_gather_consensus_timeout (void *data)
  1025. {
  1026. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1027. memb_state_consensus_timeout_expired (instance);
  1028. }
  1029. static void deliver_messages_from_recovery_to_regular (struct totemsrp_instance *instance)
  1030. {
  1031. unsigned int i;
  1032. struct sort_queue_item *recovery_message_item;
  1033. struct sort_queue_item regular_message_item;
  1034. unsigned int range = 0;
  1035. int res;
  1036. void *ptr;
  1037. struct mcast *mcast;
  1038. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1039. "recovery to regular %x-%x\n", SEQNO_START_MSG + 1, instance->my_aru);
  1040. range = instance->my_aru - SEQNO_START_MSG;
  1041. /*
  1042. * Move messages from recovery to regular sort queue
  1043. */
  1044. // todo should i be initialized to 0 or 1 ?
  1045. for (i = 1; i <= range; i++) {
  1046. res = sq_item_get (&instance->recovery_sort_queue,
  1047. i + SEQNO_START_MSG, &ptr);
  1048. if (res != 0) {
  1049. continue;
  1050. }
  1051. recovery_message_item = (struct sort_queue_item *)ptr;
  1052. /*
  1053. * Convert recovery message into regular message
  1054. */
  1055. if (recovery_message_item->iov_len > 1) {
  1056. mcast = recovery_message_item->iovec[1].iov_base;
  1057. memcpy (&regular_message_item.iovec[0],
  1058. &recovery_message_item->iovec[1],
  1059. sizeof (struct iovec) * recovery_message_item->iov_len);
  1060. } else {
  1061. mcast = recovery_message_item->iovec[0].iov_base;
  1062. if (mcast->header.encapsulated == 1) {
  1063. /*
  1064. * Message is a recovery message encapsulated
  1065. * in a new ring message
  1066. */
  1067. regular_message_item.iovec[0].iov_base =
  1068. recovery_message_item->iovec[0].iov_base + sizeof (struct mcast);
  1069. regular_message_item.iovec[0].iov_len =
  1070. recovery_message_item->iovec[0].iov_len - sizeof (struct mcast);
  1071. regular_message_item.iov_len = 1;
  1072. mcast = regular_message_item.iovec[0].iov_base;
  1073. } else {
  1074. continue; /* TODO this case shouldn't happen */
  1075. /*
  1076. * Message is originated on new ring and not
  1077. * encapsulated
  1078. */
  1079. regular_message_item.iovec[0].iov_base =
  1080. recovery_message_item->iovec[0].iov_base;
  1081. regular_message_item.iovec[0].iov_len =
  1082. recovery_message_item->iovec[0].iov_len;
  1083. }
  1084. }
  1085. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1086. "comparing if ring id is for this processors old ring seqno %d\n",
  1087. mcast->seq);
  1088. /*
  1089. * Only add this message to the regular sort
  1090. * queue if it was originated with the same ring
  1091. * id as the previous ring
  1092. */
  1093. if (memcmp (&instance->my_old_ring_id, &mcast->ring_id,
  1094. sizeof (struct memb_ring_id)) == 0) {
  1095. regular_message_item.iov_len = recovery_message_item->iov_len;
  1096. res = sq_item_inuse (&instance->regular_sort_queue, mcast->seq);
  1097. if (res == 0) {
  1098. sq_item_add (&instance->regular_sort_queue,
  1099. &regular_message_item, mcast->seq);
  1100. if (sq_lt_compare (instance->old_ring_state_high_seq_received, mcast->seq)) {
  1101. instance->old_ring_state_high_seq_received = mcast->seq;
  1102. }
  1103. }
  1104. } else {
  1105. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1106. "-not adding msg with seq no %x\n", mcast->seq);
  1107. }
  1108. }
  1109. }
  1110. /*
  1111. * Change states in the state machine of the membership algorithm
  1112. */
  1113. static void memb_state_operational_enter (struct totemsrp_instance *instance)
  1114. {
  1115. struct totem_ip_address joined_list[PROCESSOR_COUNT_MAX];
  1116. int joined_list_entries = 0;
  1117. struct totem_ip_address left_list[PROCESSOR_COUNT_MAX];
  1118. int left_list_entries = 0;
  1119. unsigned int aru_save;
  1120. old_ring_state_reset (instance);
  1121. ring_reset (instance);
  1122. deliver_messages_from_recovery_to_regular (instance);
  1123. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1124. "Delivering to app %x to %x\n",
  1125. instance->my_high_delivered + 1, instance->old_ring_state_high_seq_received);
  1126. aru_save = instance->my_aru;
  1127. instance->my_aru = instance->old_ring_state_aru;
  1128. messages_deliver_to_app (instance, 0, instance->old_ring_state_high_seq_received);
  1129. /*
  1130. * Calculate joined and left list
  1131. */
  1132. memb_set_subtract (left_list, &left_list_entries,
  1133. instance->my_memb_list, instance->my_memb_entries,
  1134. instance->my_trans_memb_list, instance->my_trans_memb_entries);
  1135. memb_set_subtract (joined_list, &joined_list_entries,
  1136. instance->my_new_memb_list, instance->my_new_memb_entries,
  1137. instance->my_trans_memb_list, instance->my_trans_memb_entries);
  1138. /*
  1139. * Deliver transitional configuration to application
  1140. */
  1141. instance->totemsrp_confchg_fn (TOTEM_CONFIGURATION_TRANSITIONAL,
  1142. instance->my_trans_memb_list, instance->my_trans_memb_entries,
  1143. left_list, left_list_entries,
  1144. 0, 0, &instance->my_ring_id);
  1145. // TODO we need to filter to ensure we only deliver those
  1146. // messages which are part of instance->my_deliver_memb
  1147. messages_deliver_to_app (instance, 1, instance->old_ring_state_high_seq_received);
  1148. instance->my_aru = aru_save;
  1149. /*
  1150. * Deliver regular configuration to application
  1151. */
  1152. instance->totemsrp_confchg_fn (TOTEM_CONFIGURATION_REGULAR,
  1153. instance->my_new_memb_list, instance->my_new_memb_entries,
  1154. 0, 0,
  1155. joined_list, joined_list_entries, &instance->my_ring_id);
  1156. /*
  1157. * Install new membership
  1158. */
  1159. instance->my_memb_entries = instance->my_new_memb_entries;
  1160. memcpy (instance->my_memb_list, instance->my_new_memb_list,
  1161. sizeof (struct totem_ip_address) * instance->my_memb_entries);
  1162. instance->last_released = 0;
  1163. instance->my_set_retrans_flg = 0;
  1164. /*
  1165. * The recovery sort queue now becomes the regular
  1166. * sort queue. It is necessary to copy the state
  1167. * into the regular sort queue.
  1168. */
  1169. sq_copy (&instance->regular_sort_queue, &instance->recovery_sort_queue);
  1170. instance->my_last_aru = SEQNO_START_MSG;
  1171. sq_items_release (&instance->regular_sort_queue, SEQNO_START_MSG - 1);
  1172. instance->my_proc_list_entries = instance->my_new_memb_entries;
  1173. memcpy (instance->my_proc_list, instance->my_new_memb_list,
  1174. sizeof (struct totem_ip_address) * instance->my_memb_entries);
  1175. instance->my_failed_list_entries = 0;
  1176. instance->my_high_delivered = instance->my_aru;
  1177. // TODO the recovery messages are leaked
  1178. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1179. "entering OPERATIONAL state.\n");
  1180. instance->memb_state = MEMB_STATE_OPERATIONAL;
  1181. instance->my_received_flg = 0;
  1182. return;
  1183. }
  1184. static void memb_state_gather_enter (struct totemsrp_instance *instance)
  1185. {
  1186. memb_set_merge (&instance->my_id, 1,
  1187. instance->my_proc_list, &instance->my_proc_list_entries);
  1188. memb_join_message_send (instance);
  1189. /*
  1190. * Restart the join timeout
  1191. */
  1192. poll_timer_delete (instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1193. poll_timer_add (instance->totemsrp_poll_handle,
  1194. instance->totem_config->join_timeout,
  1195. (void *)instance,
  1196. memb_timer_function_state_gather,
  1197. &instance->memb_timer_state_gather_join_timeout);
  1198. /*
  1199. * Restart the consensus timeout
  1200. */
  1201. poll_timer_delete (instance->totemsrp_poll_handle,
  1202. instance->memb_timer_state_gather_consensus_timeout);
  1203. poll_timer_add (instance->totemsrp_poll_handle,
  1204. instance->totem_config->consensus_timeout,
  1205. (void *)instance,
  1206. memb_timer_function_gather_consensus_timeout,
  1207. &instance->memb_timer_state_gather_consensus_timeout);
  1208. /*
  1209. * Cancel the token loss and token retransmission timeouts
  1210. */
  1211. cancel_token_retransmit_timeout (instance); // REVIEWED
  1212. cancel_token_timeout (instance); // REVIEWED
  1213. cancel_merge_detect_timeout (instance);
  1214. memb_consensus_reset (instance);
  1215. memb_consensus_set (instance, &instance->my_id);
  1216. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1217. "entering GATHER state.\n");
  1218. instance->memb_state = MEMB_STATE_GATHER;
  1219. return;
  1220. }
  1221. static void timer_function_token_retransmit_timeout (void *data);
  1222. static void memb_state_commit_enter (
  1223. struct totemsrp_instance *instance,
  1224. struct memb_commit_token *commit_token)
  1225. {
  1226. ring_save (instance);
  1227. old_ring_state_save (instance);
  1228. memb_state_commit_token_update (instance, commit_token);
  1229. memb_state_commit_token_send (instance, commit_token);
  1230. memb_ring_id_store (instance, commit_token);
  1231. poll_timer_delete (instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1232. instance->memb_timer_state_gather_join_timeout = 0;
  1233. poll_timer_delete (instance->totemsrp_poll_handle, instance->memb_timer_state_gather_consensus_timeout);
  1234. instance->memb_timer_state_gather_consensus_timeout = 0;
  1235. reset_token_timeout (instance); // REVIEWED
  1236. reset_token_retransmit_timeout (instance); // REVIEWED
  1237. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1238. "entering COMMIT state.\n");
  1239. instance->memb_state = MEMB_STATE_COMMIT;
  1240. /*
  1241. * reset all flow control variables since we are starting a new ring
  1242. */
  1243. instance->my_trc = 0;
  1244. instance->my_pbl = 0;
  1245. return;
  1246. }
  1247. static void memb_state_recovery_enter (
  1248. struct totemsrp_instance *instance,
  1249. struct memb_commit_token *commit_token)
  1250. {
  1251. int i;
  1252. int local_received_flg = 1;
  1253. unsigned int low_ring_aru;
  1254. unsigned int range = 0;
  1255. unsigned int messages_originated = 0;
  1256. char is_originated[4096];
  1257. char not_originated[4096];
  1258. char seqno_string_hex[10];
  1259. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1260. "entering RECOVERY state.\n");
  1261. instance->my_high_ring_delivered = 0;
  1262. sq_reinit (&instance->recovery_sort_queue, SEQNO_START_MSG);
  1263. queue_reinit (&instance->retrans_message_queue);
  1264. low_ring_aru = instance->old_ring_state_high_seq_received;
  1265. memb_state_commit_token_send (instance, commit_token);
  1266. instance->my_token_seq = SEQNO_START_TOKEN - 1;
  1267. /*
  1268. * Build regular configuration
  1269. */
  1270. instance->my_new_memb_entries = commit_token->addr_entries;
  1271. totemrrp_processor_count_set (
  1272. instance->totemrrp_handle,
  1273. commit_token->addr_entries);
  1274. memcpy (instance->my_new_memb_list, commit_token->addr,
  1275. sizeof (struct totem_ip_address) * instance->my_new_memb_entries);
  1276. /*
  1277. * Build transitional configuration
  1278. */
  1279. memb_set_and (instance->my_new_memb_list, instance->my_new_memb_entries,
  1280. instance->my_memb_list, instance->my_memb_entries,
  1281. instance->my_trans_memb_list, &instance->my_trans_memb_entries);
  1282. for (i = 0; i < instance->my_new_memb_entries; i++) {
  1283. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1284. "position [%d] member %s:\n", i, totemip_print (&commit_token->addr[i]));
  1285. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1286. "previous ring seq %lld rep %s\n",
  1287. commit_token->memb_list[i].ring_id.seq,
  1288. totemip_print (&commit_token->memb_list[i].ring_id.rep));
  1289. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1290. "aru %x high delivered %x received flag %d\n",
  1291. commit_token->memb_list[i].aru,
  1292. commit_token->memb_list[i].high_delivered,
  1293. commit_token->memb_list[i].received_flg);
  1294. assert (!totemip_zero_check(&commit_token->memb_list[i].ring_id.rep));
  1295. }
  1296. /*
  1297. * Determine if any received flag is false
  1298. */
  1299. for (i = 0; i < commit_token->addr_entries; i++) {
  1300. if (memb_set_subset (&instance->my_new_memb_list[i], 1,
  1301. instance->my_trans_memb_list, instance->my_trans_memb_entries) &&
  1302. commit_token->memb_list[i].received_flg == 0) {
  1303. instance->my_deliver_memb_entries = instance->my_trans_memb_entries;
  1304. memcpy (instance->my_deliver_memb_list, instance->my_trans_memb_list,
  1305. sizeof (struct totem_ip_address) * instance->my_trans_memb_entries);
  1306. local_received_flg = 0;
  1307. break;
  1308. }
  1309. }
  1310. if (local_received_flg == 0) {
  1311. /*
  1312. * Calculate my_low_ring_aru, instance->my_high_ring_delivered for the transitional membership
  1313. */
  1314. for (i = 0; i < commit_token->addr_entries; i++) {
  1315. if (memb_set_subset (&instance->my_new_memb_list[i], 1,
  1316. instance->my_deliver_memb_list,
  1317. instance->my_deliver_memb_entries) &&
  1318. memcmp (&instance->my_old_ring_id,
  1319. &commit_token->memb_list[i].ring_id,
  1320. sizeof (struct memb_ring_id)) == 0) {
  1321. if (sq_lt_compare (commit_token->memb_list[i].aru, low_ring_aru)) {
  1322. low_ring_aru = commit_token->memb_list[i].aru;
  1323. }
  1324. if (sq_lt_compare (instance->my_high_ring_delivered, commit_token->memb_list[i].high_delivered)) {
  1325. instance->my_high_ring_delivered = commit_token->memb_list[i].high_delivered;
  1326. }
  1327. }
  1328. }
  1329. /*
  1330. * Copy all old ring messages to instance->retrans_message_queue
  1331. */
  1332. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1333. "copying all old ring messages from %x-%x.\n",
  1334. low_ring_aru + 1, instance->old_ring_state_high_seq_received);
  1335. strcpy (not_originated, "Not Originated for recovery: ");
  1336. strcpy (is_originated, "Originated for recovery: ");
  1337. range = instance->old_ring_state_high_seq_received - low_ring_aru;
  1338. assert (range < 1024);
  1339. for (i = 1; i <= range; i++) {
  1340. struct sort_queue_item *sort_queue_item;
  1341. struct message_item message_item;
  1342. void *ptr;
  1343. int res;
  1344. sprintf (seqno_string_hex, "%x ", low_ring_aru + i);
  1345. res = sq_item_get (&instance->regular_sort_queue,
  1346. low_ring_aru + i, &ptr);
  1347. if (res != 0) {
  1348. strcat (not_originated, seqno_string_hex);
  1349. continue;
  1350. }
  1351. strcat (is_originated, seqno_string_hex);
  1352. sort_queue_item = ptr;
  1353. assert (sort_queue_item->iov_len > 0);
  1354. assert (sort_queue_item->iov_len <= MAXIOVS);
  1355. messages_originated++;
  1356. memset (&message_item, 0, sizeof (struct message_item));
  1357. // TODO LEAK
  1358. message_item.mcast = malloc (sizeof (struct mcast));
  1359. assert (message_item.mcast);
  1360. memcpy (message_item.mcast, sort_queue_item->iovec[0].iov_base,
  1361. sizeof (struct mcast));
  1362. memcpy (&message_item.mcast->ring_id, &instance->my_ring_id,
  1363. sizeof (struct memb_ring_id));
  1364. message_item.mcast->header.encapsulated = 1;
  1365. message_item.mcast->header.nodeid = instance->my_id.nodeid;
  1366. assert (message_item.mcast->header.nodeid);
  1367. message_item.iov_len = sort_queue_item->iov_len;
  1368. memcpy (&message_item.iovec, &sort_queue_item->iovec, sizeof (struct iovec) *
  1369. sort_queue_item->iov_len);
  1370. queue_item_add (&instance->retrans_message_queue, &message_item);
  1371. }
  1372. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1373. "Originated %d messages in RECOVERY.\n", messages_originated);
  1374. strcat (not_originated, "\n");
  1375. strcat (is_originated, "\n");
  1376. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, is_originated);
  1377. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, not_originated);
  1378. } else {
  1379. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1380. "Did not need to originate any messages in recovery.\n");
  1381. }
  1382. instance->my_aru = SEQNO_START_MSG;
  1383. instance->my_aru_count = 0;
  1384. instance->my_seq_unchanged = 0;
  1385. instance->my_high_seq_received = SEQNO_START_MSG;
  1386. instance->my_install_seq = SEQNO_START_MSG;
  1387. instance->last_released = SEQNO_START_MSG;
  1388. reset_token_timeout (instance); // REVIEWED
  1389. reset_token_retransmit_timeout (instance); // REVIEWED
  1390. instance->memb_state = MEMB_STATE_RECOVERY;
  1391. return;
  1392. }
  1393. int totemsrp_new_msg_signal (totemsrp_handle handle)
  1394. {
  1395. struct totemsrp_instance *instance;
  1396. unsigned int res;
  1397. res = hdb_handle_get (&totemsrp_instance_database, handle,
  1398. (void *)&instance);
  1399. if (res != 0) {
  1400. goto error_exit;
  1401. }
  1402. token_hold_cancel_send (instance);
  1403. hdb_handle_put (&totemsrp_instance_database, handle);
  1404. return (0);
  1405. error_exit:
  1406. return (-1);
  1407. }
  1408. int totemsrp_mcast (
  1409. totemsrp_handle handle,
  1410. struct iovec *iovec,
  1411. int iov_len,
  1412. int guarantee)
  1413. {
  1414. int i;
  1415. int j;
  1416. struct message_item message_item;
  1417. struct totemsrp_instance *instance;
  1418. unsigned int res;
  1419. res = hdb_handle_get (&totemsrp_instance_database, handle,
  1420. (void *)&instance);
  1421. if (res != 0) {
  1422. goto error_exit;
  1423. }
  1424. if (queue_is_full (&instance->new_message_queue)) {
  1425. instance->totemsrp_log_printf (instance->totemsrp_log_level_warning, "queue full\n");
  1426. return (-1);
  1427. }
  1428. for (j = 0, i = 0; i < iov_len; i++) {
  1429. j+= iovec[i].iov_len;
  1430. }
  1431. memset (&message_item, 0, sizeof (struct message_item));
  1432. /*
  1433. * Allocate pending item
  1434. */
  1435. // TODO LEAK
  1436. message_item.mcast = malloc (sizeof (struct mcast));
  1437. if (message_item.mcast == 0) {
  1438. goto error_mcast;
  1439. }
  1440. /*
  1441. * Set mcast header
  1442. */
  1443. message_item.mcast->header.type = MESSAGE_TYPE_MCAST;
  1444. message_item.mcast->header.endian_detector = ENDIAN_LOCAL;
  1445. message_item.mcast->header.encapsulated = 2;
  1446. message_item.mcast->header.nodeid = instance->my_id.nodeid;
  1447. assert (message_item.mcast->header.nodeid);
  1448. message_item.mcast->guarantee = guarantee;
  1449. totemip_copy(&message_item.mcast->source, &instance->my_id);
  1450. for (i = 0; i < iov_len; i++) {
  1451. // TODO LEAK
  1452. message_item.iovec[i].iov_base = malloc (iovec[i].iov_len);
  1453. if (message_item.iovec[i].iov_base == 0) {
  1454. goto error_iovec;
  1455. }
  1456. memcpy (message_item.iovec[i].iov_base, iovec[i].iov_base,
  1457. iovec[i].iov_len);
  1458. message_item.iovec[i].iov_len = iovec[i].iov_len;
  1459. }
  1460. message_item.iov_len = iov_len;
  1461. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug, "mcasted message added to pending queue\n");
  1462. queue_item_add (&instance->new_message_queue, &message_item);
  1463. hdb_handle_put (&totemsrp_instance_database, handle);
  1464. return (0);
  1465. error_iovec:
  1466. for (j = 0; j < i; j++) {
  1467. free (message_item.iovec[j].iov_base);
  1468. }
  1469. error_mcast:
  1470. hdb_handle_put (&totemsrp_instance_database, handle);
  1471. error_exit:
  1472. return (-1);
  1473. }
  1474. /*
  1475. * Determine if there is room to queue a new message
  1476. */
  1477. int totemsrp_avail (totemsrp_handle handle)
  1478. {
  1479. int avail;
  1480. struct totemsrp_instance *instance;
  1481. unsigned int res;
  1482. res = hdb_handle_get (&totemsrp_instance_database, handle,
  1483. (void *)&instance);
  1484. if (res != 0) {
  1485. goto error_exit;
  1486. }
  1487. queue_avail (&instance->new_message_queue, &avail);
  1488. hdb_handle_put (&totemsrp_instance_database, handle);
  1489. return (avail);
  1490. error_exit:
  1491. return (0);
  1492. }
  1493. /*
  1494. * ORF Token Management
  1495. */
  1496. /*
  1497. * Recast message to mcast group if it is available
  1498. */
  1499. static int orf_token_remcast (
  1500. struct totemsrp_instance *instance,
  1501. int seq)
  1502. {
  1503. struct sort_queue_item *sort_queue_item;
  1504. int res;
  1505. void *ptr;
  1506. struct sq *sort_queue;
  1507. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  1508. sort_queue = &instance->recovery_sort_queue;
  1509. } else {
  1510. sort_queue = &instance->regular_sort_queue;
  1511. }
  1512. res = sq_in_range (sort_queue, seq);
  1513. if (res == 0) {
  1514. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug, "sq not in range\n");
  1515. return (-1);
  1516. }
  1517. /*
  1518. * Get RTR item at seq, if not available, return
  1519. */
  1520. res = sq_item_get (sort_queue, seq, &ptr);
  1521. if (res != 0) {
  1522. return -1;
  1523. }
  1524. sort_queue_item = ptr;
  1525. totemrrp_mcast_noflush_send (instance->totemrrp_handle,
  1526. sort_queue_item->iovec,
  1527. sort_queue_item->iov_len);
  1528. return (0);
  1529. }
  1530. /*
  1531. * Free all freeable messages from ring
  1532. */
  1533. static void messages_free (
  1534. struct totemsrp_instance *instance,
  1535. unsigned int token_aru)
  1536. {
  1537. struct sort_queue_item *regular_message;
  1538. unsigned int i, j;
  1539. int res;
  1540. int log_release = 0;
  1541. unsigned int release_to;
  1542. unsigned int range = 0;
  1543. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1544. "aru %x last aru %x my high delivered %x last released %x\n",
  1545. token_aru, instance->my_last_aru, instance->my_high_delivered, instance->last_released);
  1546. release_to = token_aru;
  1547. if (sq_lt_compare (instance->my_last_aru, release_to)) {
  1548. release_to = instance->my_last_aru;
  1549. }
  1550. if (sq_lt_compare (instance->my_high_delivered, release_to)) {
  1551. release_to = instance->my_high_delivered;
  1552. }
  1553. /*
  1554. * Ensure we dont try release before an already released point
  1555. */
  1556. if (sq_lt_compare (release_to, instance->last_released)) {
  1557. return;
  1558. }
  1559. range = release_to - instance->last_released;
  1560. assert (range < 1024);
  1561. /*
  1562. * Release retransmit list items if group aru indicates they are transmitted
  1563. */
  1564. for (i = 1; i <= range; i++) {
  1565. void *ptr;
  1566. res = sq_item_get (&instance->regular_sort_queue,
  1567. instance->last_released + i, &ptr);
  1568. if (res == 0) {
  1569. regular_message = ptr;
  1570. for (j = 0; j < regular_message->iov_len; j++) {
  1571. free (regular_message->iovec[j].iov_base);
  1572. }
  1573. }
  1574. sq_items_release (&instance->regular_sort_queue,
  1575. instance->last_released + i);
  1576. log_release = 1;
  1577. }
  1578. instance->last_released += range;
  1579. if (log_release) {
  1580. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1581. "releasing messages up to and including %x\n", release_to);
  1582. }
  1583. }
  1584. static void update_aru (
  1585. struct totemsrp_instance *instance)
  1586. {
  1587. unsigned int i;
  1588. int res;
  1589. struct sq *sort_queue;
  1590. unsigned int range;
  1591. unsigned int my_aru_saved = 0;
  1592. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  1593. sort_queue = &instance->recovery_sort_queue;
  1594. } else {
  1595. sort_queue = &instance->regular_sort_queue;
  1596. }
  1597. range = instance->my_high_seq_received - instance->my_aru;
  1598. if (range > 1024) {
  1599. return;
  1600. }
  1601. my_aru_saved = instance->my_aru;
  1602. for (i = 1; i <= range; i++) {
  1603. void *ptr;
  1604. res = sq_item_get (sort_queue, my_aru_saved + i, &ptr);
  1605. /*
  1606. * If hole, stop updating aru
  1607. */
  1608. if (res != 0) {
  1609. break;
  1610. }
  1611. }
  1612. instance->my_aru += i - 1;
  1613. }
  1614. /*
  1615. * Multicasts pending messages onto the ring (requires orf_token possession)
  1616. */
  1617. static int orf_token_mcast (
  1618. struct totemsrp_instance *instance,
  1619. struct orf_token *token,
  1620. int fcc_mcasts_allowed,
  1621. struct totem_ip_address *system_from)
  1622. {
  1623. struct message_item *message_item = 0;
  1624. struct queue *mcast_queue;
  1625. struct sq *sort_queue;
  1626. struct sort_queue_item sort_queue_item;
  1627. struct sort_queue_item *sort_queue_item_ptr;
  1628. struct mcast *mcast;
  1629. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  1630. mcast_queue = &instance->retrans_message_queue;
  1631. sort_queue = &instance->recovery_sort_queue;
  1632. reset_token_retransmit_timeout (instance); // REVIEWED
  1633. } else {
  1634. mcast_queue = &instance->new_message_queue;
  1635. sort_queue = &instance->regular_sort_queue;
  1636. }
  1637. for (instance->fcc_mcast_current = 0; instance->fcc_mcast_current <= fcc_mcasts_allowed; instance->fcc_mcast_current++) {
  1638. if (queue_is_empty (mcast_queue)) {
  1639. break;
  1640. }
  1641. message_item = (struct message_item *)queue_item_get (mcast_queue);
  1642. /* preincrement required by algo */
  1643. if (instance->old_ring_state_saved &&
  1644. (instance->memb_state == MEMB_STATE_GATHER ||
  1645. instance->memb_state == MEMB_STATE_COMMIT)) {
  1646. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1647. "not multicasting at seqno is %d\n",
  1648. token->seq);
  1649. return (0);
  1650. }
  1651. message_item->mcast->seq = ++token->seq;
  1652. message_item->mcast->this_seqno = instance->global_seqno++;
  1653. /*
  1654. * Build IO vector
  1655. */
  1656. memset (&sort_queue_item, 0, sizeof (struct sort_queue_item));
  1657. sort_queue_item.iovec[0].iov_base = message_item->mcast;
  1658. sort_queue_item.iovec[0].iov_len = sizeof (struct mcast);
  1659. mcast = sort_queue_item.iovec[0].iov_base;
  1660. memcpy (&sort_queue_item.iovec[1], message_item->iovec,
  1661. message_item->iov_len * sizeof (struct iovec));
  1662. memcpy (&mcast->ring_id, &instance->my_ring_id, sizeof (struct memb_ring_id));
  1663. sort_queue_item.iov_len = message_item->iov_len + 1;
  1664. assert (sort_queue_item.iov_len < 16);
  1665. /*
  1666. * Add message to retransmit queue
  1667. */
  1668. sort_queue_item_ptr = sq_item_add (sort_queue,
  1669. &sort_queue_item, message_item->mcast->seq);
  1670. totemrrp_mcast_noflush_send (instance->totemrrp_handle,
  1671. sort_queue_item_ptr->iovec,
  1672. sort_queue_item_ptr->iov_len);
  1673. /*
  1674. * Delete item from pending queue
  1675. */
  1676. queue_item_remove (mcast_queue);
  1677. }
  1678. assert (instance->fcc_mcast_current < 100);
  1679. /*
  1680. * If messages mcasted, deliver any new messages to totempg
  1681. */
  1682. instance->my_high_seq_received = token->seq;
  1683. update_aru (instance);
  1684. /*
  1685. * Return 1 if more messages are available for single node clusters
  1686. */
  1687. return (instance->fcc_mcast_current);
  1688. }
  1689. /*
  1690. * Remulticasts messages in orf_token's retransmit list (requires orf_token)
  1691. * Modify's orf_token's rtr to include retransmits required by this process
  1692. */
  1693. static int orf_token_rtr (
  1694. struct totemsrp_instance *instance,
  1695. struct orf_token *orf_token,
  1696. int *fcc_allowed)
  1697. {
  1698. unsigned int res;
  1699. unsigned int i, j;
  1700. unsigned int found;
  1701. unsigned int total_entries;
  1702. struct sq *sort_queue;
  1703. struct rtr_item *rtr_list;
  1704. unsigned int range = 0;
  1705. char retransmit_msg[1024];
  1706. char value[64];
  1707. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  1708. sort_queue = &instance->recovery_sort_queue;
  1709. } else {
  1710. sort_queue = &instance->regular_sort_queue;
  1711. }
  1712. rtr_list = &orf_token->rtr_list[0];
  1713. strcpy (retransmit_msg, "Retransmit List: ");
  1714. if (orf_token->rtr_list_entries) {
  1715. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1716. "Retransmit List %d\n", orf_token->rtr_list_entries);
  1717. for (i = 0; i < orf_token->rtr_list_entries; i++) {
  1718. sprintf (value, "%x ", rtr_list[i].seq);
  1719. strcat (retransmit_msg, value);
  1720. }
  1721. strcat (retransmit_msg, "\n");
  1722. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1723. "%s", retransmit_msg);
  1724. }
  1725. total_entries = orf_token->rtr_list_entries;
  1726. /*
  1727. * Retransmit messages on orf_token's RTR list from RTR queue
  1728. */
  1729. for (instance->fcc_remcast_current = 0, i = 0;
  1730. instance->fcc_remcast_current <= *fcc_allowed && i < orf_token->rtr_list_entries;) {
  1731. /*
  1732. * If this retransmit request isn't from this configuration,
  1733. * try next rtr entry
  1734. */
  1735. if (memcmp (&rtr_list[i].ring_id, &instance->my_ring_id,
  1736. sizeof (struct memb_ring_id)) != 0) {
  1737. i += 1;
  1738. continue;
  1739. }
  1740. res = orf_token_remcast (instance, rtr_list[i].seq);
  1741. if (res == 0) {
  1742. /*
  1743. * Multicasted message, so no need to copy to new retransmit list
  1744. */
  1745. orf_token->rtr_list_entries -= 1;
  1746. assert (orf_token->rtr_list_entries >= 0);
  1747. memmove (&rtr_list[i], &rtr_list[i + 1],
  1748. sizeof (struct rtr_item) * (orf_token->rtr_list_entries));
  1749. instance->fcc_remcast_current++;
  1750. } else {
  1751. i += 1;
  1752. }
  1753. }
  1754. *fcc_allowed = *fcc_allowed - instance->fcc_remcast_current - 1;
  1755. /*
  1756. * Add messages to retransmit to RTR list
  1757. * but only retry if there is room in the retransmit list
  1758. */
  1759. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1760. "high seq %x aru %x\n", instance->my_high_seq_received, instance->my_aru);
  1761. range = instance->my_high_seq_received - instance->my_aru;
  1762. assert (range < 100000);
  1763. for (i = 1; (orf_token->rtr_list_entries < RETRANSMIT_ENTRIES_MAX) &&
  1764. (i <= range); i++) {
  1765. /*
  1766. * Ensure message is within the sort queue range
  1767. */
  1768. res = sq_in_range (sort_queue, instance->my_aru + i);
  1769. if (res == 0) {
  1770. break;
  1771. }
  1772. /*
  1773. * Find if a message is missing from this processor
  1774. */
  1775. res = sq_item_inuse (sort_queue, instance->my_aru + i);
  1776. if (res == 0) {
  1777. /*
  1778. * Determine if missing message is already in retransmit list
  1779. */
  1780. found = 0;
  1781. for (j = 0; j < orf_token->rtr_list_entries; j++) {
  1782. if (instance->my_aru + i == rtr_list[j].seq) {
  1783. found = 1;
  1784. }
  1785. }
  1786. if (found == 0) {
  1787. /*
  1788. * Missing message not found in current retransmit list so add it
  1789. */
  1790. memcpy (&rtr_list[orf_token->rtr_list_entries].ring_id,
  1791. &instance->my_ring_id, sizeof (struct memb_ring_id));
  1792. rtr_list[orf_token->rtr_list_entries].seq = instance->my_aru + i;
  1793. orf_token->rtr_list_entries++;
  1794. }
  1795. }
  1796. }
  1797. return (instance->fcc_remcast_current);
  1798. }
  1799. static void token_retransmit (struct totemsrp_instance *instance) {
  1800. totemrrp_token_send (instance->totemrrp_handle,
  1801. &instance->next_memb,
  1802. instance->orf_token_retransmit,
  1803. instance->orf_token_retransmit_size);
  1804. }
  1805. /*
  1806. * Retransmit the regular token if no mcast or token has
  1807. * been received in retransmit token period retransmit
  1808. * the token to the next processor
  1809. */
  1810. static void timer_function_token_retransmit_timeout (void *data)
  1811. {
  1812. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1813. switch (instance->memb_state) {
  1814. case MEMB_STATE_GATHER:
  1815. break;
  1816. case MEMB_STATE_COMMIT:
  1817. break;
  1818. case MEMB_STATE_OPERATIONAL:
  1819. case MEMB_STATE_RECOVERY:
  1820. token_retransmit (instance);
  1821. reset_token_retransmit_timeout (instance); // REVIEWED
  1822. break;
  1823. }
  1824. }
  1825. static void timer_function_token_hold_retransmit_timeout (void *data)
  1826. {
  1827. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1828. switch (instance->memb_state) {
  1829. case MEMB_STATE_GATHER:
  1830. break;
  1831. case MEMB_STATE_COMMIT:
  1832. break;
  1833. case MEMB_STATE_OPERATIONAL:
  1834. case MEMB_STATE_RECOVERY:
  1835. token_retransmit (instance);
  1836. break;
  1837. }
  1838. }
  1839. static void timer_function_merge_detect_timeout(void *data)
  1840. {
  1841. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1842. instance->my_merge_detect_timeout_outstanding = 0;
  1843. switch (instance->memb_state) {
  1844. case MEMB_STATE_OPERATIONAL:
  1845. if (totemip_equal(&instance->my_ring_id.rep, &instance->my_id)) {
  1846. memb_merge_detect_transmit (instance);
  1847. }
  1848. break;
  1849. case MEMB_STATE_GATHER:
  1850. case MEMB_STATE_COMMIT:
  1851. case MEMB_STATE_RECOVERY:
  1852. break;
  1853. }
  1854. }
  1855. /*
  1856. * Send orf_token to next member (requires orf_token)
  1857. */
  1858. static int token_send (
  1859. struct totemsrp_instance *instance,
  1860. struct orf_token *orf_token,
  1861. int forward_token)
  1862. {
  1863. int res = 0;
  1864. int iov_len = sizeof (struct orf_token) +
  1865. (orf_token->rtr_list_entries * sizeof (struct rtr_item));
  1866. memcpy (instance->orf_token_retransmit, orf_token, iov_len);
  1867. instance->orf_token_retransmit_size = iov_len;
  1868. orf_token->header.nodeid = instance->my_id.nodeid;
  1869. assert (orf_token->header.nodeid);
  1870. if (forward_token == 0) {
  1871. return (0);
  1872. }
  1873. totemrrp_token_send (instance->totemrrp_handle,
  1874. &instance->next_memb,
  1875. orf_token,
  1876. iov_len);
  1877. return (res);
  1878. }
  1879. static int token_hold_cancel_send (struct totemsrp_instance *instance)
  1880. {
  1881. struct token_hold_cancel token_hold_cancel;
  1882. /*
  1883. * Only cancel if the token is currently held
  1884. */
  1885. if (instance->my_token_held == 0) {
  1886. return (0);
  1887. }
  1888. instance->my_token_held = 0;
  1889. /*
  1890. * Build message
  1891. */
  1892. token_hold_cancel.header.type = MESSAGE_TYPE_TOKEN_HOLD_CANCEL;
  1893. token_hold_cancel.header.endian_detector = ENDIAN_LOCAL;
  1894. token_hold_cancel.header.nodeid = instance->my_id.nodeid;
  1895. assert (token_hold_cancel.header.nodeid);
  1896. memcpy (&token_hold_cancel.ring_id, &instance->my_ring_id,
  1897. sizeof (struct memb_ring_id));
  1898. totemrrp_mcast_flush_send (
  1899. instance->totemrrp_handle,
  1900. &token_hold_cancel,
  1901. sizeof (struct token_hold_cancel));
  1902. return (0);
  1903. }
  1904. //AAA
  1905. static int orf_token_send_initial (struct totemsrp_instance *instance)
  1906. {
  1907. struct orf_token orf_token;
  1908. int res;
  1909. orf_token.header.type = MESSAGE_TYPE_ORF_TOKEN;
  1910. orf_token.header.endian_detector = ENDIAN_LOCAL;
  1911. orf_token.header.encapsulated = 0;
  1912. orf_token.header.nodeid = instance->my_id.nodeid;
  1913. assert (orf_token.header.nodeid);
  1914. orf_token.seq = 0;
  1915. orf_token.seq = SEQNO_START_MSG;
  1916. orf_token.token_seq = SEQNO_START_TOKEN;
  1917. orf_token.retrans_flg = 1;
  1918. instance->my_set_retrans_flg = 1;
  1919. /*
  1920. if (queue_is_empty (&instance->retrans_message_queue) == 1) {
  1921. orf_token.retrans_flg = 0;
  1922. } else {
  1923. orf_token.retrans_flg = 1;
  1924. instance->my_set_retrans_flg = 1;
  1925. }
  1926. */
  1927. orf_token.aru = 0;
  1928. orf_token.aru = SEQNO_START_MSG - 1;
  1929. totemip_copy(&orf_token.aru_addr, &instance->my_id);
  1930. memcpy (&orf_token.ring_id, &instance->my_ring_id, sizeof (struct memb_ring_id));
  1931. orf_token.fcc = 0;
  1932. orf_token.backlog = 0;
  1933. orf_token.rtr_list_entries = 0;
  1934. res = token_send (instance, &orf_token, 1);
  1935. return (res);
  1936. }
  1937. static void memb_state_commit_token_update (
  1938. struct totemsrp_instance *instance,
  1939. struct memb_commit_token *memb_commit_token)
  1940. {
  1941. int memb_index_this;
  1942. memb_index_this = (memb_commit_token->memb_index + 1) % memb_commit_token->addr_entries;
  1943. memcpy (&memb_commit_token->memb_list[memb_index_this].ring_id,
  1944. &instance->my_old_ring_id, sizeof (struct memb_ring_id));
  1945. assert (!totemip_zero_check(&instance->my_old_ring_id.rep));
  1946. memb_commit_token->memb_list[memb_index_this].aru = instance->old_ring_state_aru;
  1947. /*
  1948. * TODO high delivered is really instance->my_aru, but with safe this
  1949. * could change?
  1950. */
  1951. memb_commit_token->memb_list[memb_index_this].high_delivered = instance->my_high_delivered;
  1952. memb_commit_token->memb_list[memb_index_this].received_flg = instance->my_received_flg;
  1953. memb_commit_token->header.nodeid = instance->my_id.nodeid;
  1954. assert (memb_commit_token->header.nodeid);
  1955. }
  1956. static int memb_state_commit_token_send (struct totemsrp_instance *instance,
  1957. struct memb_commit_token *memb_commit_token)
  1958. {
  1959. struct iovec iovec;
  1960. int memb_index_this;
  1961. int memb_index_next;
  1962. memb_commit_token->token_seq++;
  1963. memb_index_this = (memb_commit_token->memb_index + 1) % memb_commit_token->addr_entries;
  1964. memb_index_next = (memb_index_this + 1) % memb_commit_token->addr_entries;
  1965. memb_commit_token->memb_index = memb_index_this;
  1966. iovec.iov_base = memb_commit_token;
  1967. iovec.iov_len = sizeof (struct memb_commit_token);
  1968. totemip_copy(&instance->next_memb, &memb_commit_token->addr[memb_index_next]);
  1969. assert (instance->next_memb.nodeid != 0);
  1970. totemrrp_token_send (instance->totemrrp_handle,
  1971. &instance->next_memb,
  1972. memb_commit_token,
  1973. sizeof (struct memb_commit_token));
  1974. return (0);
  1975. }
  1976. static int memb_lowest_in_config (struct totemsrp_instance *instance)
  1977. {
  1978. struct totem_ip_address token_memb[PROCESSOR_COUNT_MAX];
  1979. int token_memb_entries = 0;
  1980. struct totem_ip_address lowest_addr;
  1981. int i;
  1982. memset(&lowest_addr, 0xff, sizeof(lowest_addr));
  1983. lowest_addr.family = instance->mcast_address.family;
  1984. memb_set_subtract (token_memb, &token_memb_entries,
  1985. instance->my_proc_list, instance->my_proc_list_entries,
  1986. instance->my_failed_list, instance->my_failed_list_entries);
  1987. /*
  1988. * find representative by searching for smallest identifier
  1989. */
  1990. for (i = 0; i < token_memb_entries; i++) {
  1991. if (totemip_compare(&lowest_addr, &token_memb[i]) > 0) {
  1992. totemip_copy(&lowest_addr, &token_memb[i]);
  1993. }
  1994. }
  1995. return (totemip_equal(&instance->my_id, &lowest_addr));
  1996. }
  1997. static void memb_state_commit_token_create (
  1998. struct totemsrp_instance *instance,
  1999. struct memb_commit_token *commit_token)
  2000. {
  2001. struct totem_ip_address token_memb[PROCESSOR_COUNT_MAX];
  2002. int token_memb_entries = 0;
  2003. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2004. "Creating commit token because I am the rep.\n");
  2005. memb_set_subtract (token_memb, &token_memb_entries,
  2006. instance->my_proc_list, instance->my_proc_list_entries,
  2007. instance->my_failed_list, instance->my_failed_list_entries);
  2008. memset (commit_token, 0, sizeof (struct memb_commit_token));
  2009. commit_token->header.type = MESSAGE_TYPE_MEMB_COMMIT_TOKEN;
  2010. commit_token->header.endian_detector = ENDIAN_LOCAL;
  2011. commit_token->header.encapsulated = 0;
  2012. commit_token->header.nodeid = instance->my_id.nodeid;
  2013. assert (commit_token->header.nodeid);
  2014. totemip_copy(&commit_token->ring_id.rep, &instance->my_id);
  2015. commit_token->ring_id.seq = instance->token_ring_id_seq + 4;
  2016. qsort (token_memb, token_memb_entries,
  2017. sizeof (struct totem_ip_address), totemip_compare);
  2018. memcpy (commit_token->addr, token_memb,
  2019. token_memb_entries * sizeof (struct totem_ip_address));
  2020. memset (commit_token->memb_list, 0,
  2021. sizeof (struct memb_commit_token_memb_entry) * PROCESSOR_COUNT_MAX);
  2022. commit_token->memb_index = token_memb_entries - 1;
  2023. commit_token->addr_entries = token_memb_entries;
  2024. }
  2025. static void memb_join_message_send (struct totemsrp_instance *instance)
  2026. {
  2027. struct memb_join memb_join;
  2028. memb_join.header.type = MESSAGE_TYPE_MEMB_JOIN;
  2029. memb_join.header.endian_detector = ENDIAN_LOCAL;
  2030. memb_join.header.encapsulated = 0;
  2031. memb_join.header.nodeid = instance->my_id.nodeid;
  2032. assert (memb_join.header.nodeid);
  2033. memb_join.ring_seq = instance->my_ring_id.seq;
  2034. memcpy (memb_join.proc_list, instance->my_proc_list,
  2035. instance->my_proc_list_entries * sizeof (struct totem_ip_address));
  2036. memb_join.proc_list_entries = instance->my_proc_list_entries;
  2037. memcpy (memb_join.failed_list, instance->my_failed_list,
  2038. instance->my_failed_list_entries * sizeof (struct totem_ip_address));
  2039. memb_join.failed_list_entries = instance->my_failed_list_entries;
  2040. totemrrp_mcast_flush_send (
  2041. instance->totemrrp_handle,
  2042. &memb_join,
  2043. sizeof (struct memb_join));
  2044. }
  2045. static void memb_merge_detect_transmit (struct totemsrp_instance *instance)
  2046. {
  2047. struct memb_merge_detect memb_merge_detect;
  2048. memb_merge_detect.header.type = MESSAGE_TYPE_MEMB_MERGE_DETECT;
  2049. memb_merge_detect.header.endian_detector = ENDIAN_LOCAL;
  2050. memb_merge_detect.header.encapsulated = 0;
  2051. memb_merge_detect.header.nodeid = instance->my_id.nodeid;
  2052. assert (memb_merge_detect.header.nodeid);
  2053. memcpy (&memb_merge_detect.ring_id, &instance->my_ring_id,
  2054. sizeof (struct memb_ring_id));
  2055. totemrrp_mcast_flush_send (
  2056. instance->totemrrp_handle,
  2057. &memb_merge_detect,
  2058. sizeof (struct memb_merge_detect));
  2059. }
  2060. static void memb_ring_id_create_or_load (
  2061. struct totemsrp_instance *instance,
  2062. struct memb_ring_id *memb_ring_id)
  2063. {
  2064. int fd;
  2065. int res;
  2066. char filename[256];
  2067. sprintf (filename, "/tmp/ringid_%s",
  2068. totemip_print (&instance->my_id));
  2069. fd = open (filename, O_RDONLY, 0777);
  2070. if (fd > 0) {
  2071. res = read (fd, &memb_ring_id->seq, sizeof (unsigned long long));
  2072. assert (res == sizeof (unsigned long long));
  2073. close (fd);
  2074. } else
  2075. if (fd == -1 && errno == ENOENT) {
  2076. memb_ring_id->seq = 0;
  2077. umask(0);
  2078. fd = open (filename, O_CREAT|O_RDWR, 0777);
  2079. if (fd == -1) {
  2080. instance->totemsrp_log_printf (instance->totemsrp_log_level_warning,
  2081. "Couldn't create %s %s\n", filename, strerror (errno));
  2082. }
  2083. res = write (fd, &memb_ring_id->seq, sizeof (unsigned long long));
  2084. assert (res == sizeof (unsigned long long));
  2085. close (fd);
  2086. } else {
  2087. instance->totemsrp_log_printf (instance->totemsrp_log_level_warning,
  2088. "Couldn't open %s %s\n", filename, strerror (errno));
  2089. }
  2090. totemip_copy(&memb_ring_id->rep, &instance->my_id);
  2091. assert (!totemip_zero_check(&memb_ring_id->rep));
  2092. instance->token_ring_id_seq = memb_ring_id->seq;
  2093. }
  2094. static void memb_ring_id_store (
  2095. struct totemsrp_instance *instance,
  2096. struct memb_commit_token *commit_token)
  2097. {
  2098. char filename[256];
  2099. int fd;
  2100. int res;
  2101. sprintf (filename, "/tmp/ringid_%s",
  2102. totemip_print (&instance->my_id));
  2103. fd = open (filename, O_WRONLY, 0777);
  2104. if (fd == -1) {
  2105. fd = open (filename, O_CREAT|O_RDWR, 0777);
  2106. }
  2107. if (fd == -1) {
  2108. instance->totemsrp_log_printf (instance->totemsrp_log_level_warning,
  2109. "Couldn't store new ring id %llx to stable storage (%s)\n",
  2110. commit_token->ring_id.seq, strerror (errno));
  2111. assert (0);
  2112. return;
  2113. }
  2114. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2115. "Storing new sequence id for ring %d\n", commit_token->ring_id.seq);
  2116. assert (fd > 0);
  2117. res = write (fd, &commit_token->ring_id.seq, sizeof (unsigned long long));
  2118. assert (res == sizeof (unsigned long long));
  2119. close (fd);
  2120. memcpy (&instance->my_ring_id, &commit_token->ring_id, sizeof (struct memb_ring_id));
  2121. instance->token_ring_id_seq = instance->my_ring_id.seq;
  2122. }
  2123. int totemsrp_callback_token_create (
  2124. totemsrp_handle handle,
  2125. void **handle_out,
  2126. enum totem_callback_token_type type,
  2127. int delete,
  2128. int (*callback_fn) (enum totem_callback_token_type type, void *),
  2129. void *data)
  2130. {
  2131. struct token_callback_instance *callback_handle;
  2132. struct totemsrp_instance *instance;
  2133. unsigned int res;
  2134. res = hdb_handle_get (&totemsrp_instance_database, handle,
  2135. (void *)&instance);
  2136. if (res != 0) {
  2137. goto error_exit;
  2138. }
  2139. callback_handle = (struct token_callback_instance *)malloc (sizeof (struct token_callback_instance));
  2140. if (callback_handle == 0) {
  2141. return (-1);
  2142. }
  2143. *handle_out = (void *)callback_handle;
  2144. list_init (&callback_handle->list);
  2145. callback_handle->callback_fn = callback_fn;
  2146. callback_handle->data = data;
  2147. callback_handle->callback_type = type;
  2148. callback_handle->delete = delete;
  2149. switch (type) {
  2150. case TOTEM_CALLBACK_TOKEN_RECEIVED:
  2151. list_add (&callback_handle->list, &instance->token_callback_received_listhead);
  2152. break;
  2153. case TOTEM_CALLBACK_TOKEN_SENT:
  2154. list_add (&callback_handle->list, &instance->token_callback_sent_listhead);
  2155. break;
  2156. }
  2157. hdb_handle_put (&totemsrp_instance_database, handle);
  2158. error_exit:
  2159. return (0);
  2160. }
  2161. void totemsrp_callback_token_destroy (totemsrp_handle handle, void **handle_out)
  2162. {
  2163. struct token_callback_instance *h;
  2164. if (*handle_out) {
  2165. h = (struct token_callback_instance *)*handle_out;
  2166. list_del (&h->list);
  2167. free (h);
  2168. h = NULL;
  2169. *handle_out = 0;
  2170. }
  2171. }
  2172. void totem_callback_token_type (struct totemsrp_instance *instance, void *handle)
  2173. {
  2174. struct token_callback_instance *token_callback_instance = (struct token_callback_instance *)handle;
  2175. list_del (&token_callback_instance->list);
  2176. free (token_callback_instance);
  2177. }
  2178. static void token_callbacks_execute (
  2179. struct totemsrp_instance *instance,
  2180. enum totem_callback_token_type type)
  2181. {
  2182. struct list_head *list;
  2183. struct list_head *list_next;
  2184. struct list_head *callback_listhead = 0;
  2185. struct token_callback_instance *token_callback_instance;
  2186. int res;
  2187. int del;
  2188. switch (type) {
  2189. case TOTEM_CALLBACK_TOKEN_RECEIVED:
  2190. callback_listhead = &instance->token_callback_received_listhead;
  2191. break;
  2192. case TOTEM_CALLBACK_TOKEN_SENT:
  2193. callback_listhead = &instance->token_callback_sent_listhead;
  2194. break;
  2195. default:
  2196. assert (0);
  2197. }
  2198. for (list = callback_listhead->next; list != callback_listhead;
  2199. list = list_next) {
  2200. token_callback_instance = list_entry (list, struct token_callback_instance, list);
  2201. list_next = list->next;
  2202. del = token_callback_instance->delete;
  2203. if (del == 1) {
  2204. list_del (list);
  2205. }
  2206. res = token_callback_instance->callback_fn (
  2207. token_callback_instance->callback_type,
  2208. token_callback_instance->data);
  2209. /*
  2210. * This callback failed to execute, try it again on the next token
  2211. */
  2212. if (res == -1 && del == 1) {
  2213. list_add (list, callback_listhead);
  2214. } else if (del) {
  2215. free (token_callback_instance);
  2216. }
  2217. }
  2218. }
  2219. /*
  2220. * Flow control functions
  2221. */
  2222. static unsigned int backlog_get (struct totemsrp_instance *instance)
  2223. {
  2224. unsigned int backlog = 0;
  2225. if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
  2226. backlog = queue_used (&instance->new_message_queue);
  2227. } else
  2228. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2229. backlog = queue_used (&instance->retrans_message_queue);
  2230. }
  2231. return (backlog);
  2232. }
  2233. static int fcc_calculate (
  2234. struct totemsrp_instance *instance,
  2235. struct orf_token *token)
  2236. {
  2237. unsigned int transmits_allowed;
  2238. unsigned int backlog_calc;
  2239. transmits_allowed = instance->totem_config->max_messages;
  2240. if (transmits_allowed > instance->totem_config->window_size - token->fcc) {
  2241. transmits_allowed = instance->totem_config->window_size - token->fcc;
  2242. }
  2243. /*
  2244. * Only do backlog calculation if there is a backlog otherwise
  2245. * we would result in div by zero
  2246. */
  2247. if (token->backlog + backlog_get (instance) - instance->my_pbl) {
  2248. backlog_calc = (instance->totem_config->window_size * instance->my_pbl) /
  2249. (token->backlog + backlog_get (instance) - instance->my_pbl);
  2250. if (backlog_calc > 0 && transmits_allowed > backlog_calc) {
  2251. transmits_allowed = backlog_calc;
  2252. }
  2253. }
  2254. return (transmits_allowed);
  2255. }
  2256. static void fcc_token_update (
  2257. struct totemsrp_instance *instance,
  2258. struct orf_token *token,
  2259. unsigned int msgs_transmitted)
  2260. {
  2261. token->fcc += msgs_transmitted - instance->my_trc;
  2262. token->backlog += backlog_get (instance) - instance->my_pbl;
  2263. instance->my_trc = msgs_transmitted;
  2264. instance->my_pbl = backlog_get (instance);
  2265. }
  2266. /*
  2267. * Message Handlers
  2268. */
  2269. /*
  2270. * message handler called when TOKEN message type received
  2271. */
  2272. static int message_handler_orf_token (
  2273. struct totemsrp_instance *instance,
  2274. struct totem_ip_address *system_from,
  2275. void *msg,
  2276. int msg_len,
  2277. int endian_conversion_needed)
  2278. {
  2279. char token_storage[1500];
  2280. char token_convert[1500];
  2281. struct orf_token *token = NULL;
  2282. int forward_token;
  2283. unsigned int transmits_allowed;
  2284. unsigned int mcasted_retransmit;
  2285. unsigned int mcasted_regular;
  2286. unsigned int last_aru;
  2287. unsigned int low_water;
  2288. #ifdef GIVEINFO
  2289. struct timeval tv_current;
  2290. struct timeval tv_diff;
  2291. gettimeofday (&tv_current, NULL);
  2292. timersub (&tv_current, &tv_old, &tv_diff);
  2293. memcpy (&tv_old, &tv_current, sizeof (struct timeval));
  2294. if ((((float)tv_diff.tv_usec) / 100.0) > 5.0) {
  2295. printf ("OTHERS %0.4f ms\n", ((float)tv_diff.tv_usec) / 100.0);
  2296. }
  2297. #endif
  2298. #ifdef TEST_DROP_ORF_TOKEN_PERCENTAGE
  2299. if (random()%100 < TEST_DROP_ORF_TOKEN_PERCENTAGE) {
  2300. return (0);
  2301. }
  2302. #endif
  2303. if (endian_conversion_needed) {
  2304. orf_token_endian_convert ((struct orf_token *)msg,
  2305. (struct orf_token *)token_convert);
  2306. msg = (struct orf_token *)token_convert;
  2307. }
  2308. /*
  2309. * Make copy of token and retransmit list in case we have
  2310. * to flush incoming messages from the kernel queue
  2311. */
  2312. token = (struct orf_token *)token_storage;
  2313. memcpy (token, msg, sizeof (struct orf_token));
  2314. memcpy (&token->rtr_list[0], msg + sizeof (struct orf_token),
  2315. sizeof (struct rtr_item) * RETRANSMIT_ENTRIES_MAX);
  2316. /*
  2317. * Handle merge detection timeout
  2318. */
  2319. if (token->seq == instance->my_last_seq) {
  2320. start_merge_detect_timeout (instance);
  2321. instance->my_seq_unchanged += 1;
  2322. } else {
  2323. cancel_merge_detect_timeout (instance);
  2324. cancel_token_hold_retransmit_timeout (instance);
  2325. instance->my_seq_unchanged = 0;
  2326. }
  2327. instance->my_last_seq = token->seq;
  2328. #ifdef TEST_RECOVERY_MSG_COUNT
  2329. if (instance->memb_state == MEMB_STATE_OPERATIONAL && token->seq > TEST_RECOVERY_MSG_COUNT) {
  2330. return (0);
  2331. }
  2332. #endif
  2333. totemrrp_recv_flush (instance->totemrrp_handle);
  2334. /*
  2335. * Determine if we should hold (in reality drop) the token
  2336. */
  2337. instance->my_token_held = 0;
  2338. if (totemip_equal(&instance->my_ring_id.rep, &instance->my_id) &&
  2339. instance->my_seq_unchanged > instance->totem_config->seqno_unchanged_const) {
  2340. instance->my_token_held = 1;
  2341. } else
  2342. if (!totemip_equal(&instance->my_ring_id.rep, &instance->my_id) &&
  2343. instance->my_seq_unchanged >= instance->totem_config->seqno_unchanged_const) {
  2344. instance->my_token_held = 1;
  2345. }
  2346. /*
  2347. * Hold onto token when there is no activity on ring and
  2348. * this processor is the ring rep
  2349. */
  2350. forward_token = 1;
  2351. if (totemip_equal(&instance->my_ring_id.rep, &instance->my_id)) {
  2352. if (instance->my_token_held) {
  2353. forward_token = 0;
  2354. }
  2355. }
  2356. token_callbacks_execute (instance, TOTEM_CALLBACK_TOKEN_RECEIVED);
  2357. switch (instance->memb_state) {
  2358. case MEMB_STATE_COMMIT:
  2359. /* Discard token */
  2360. break;
  2361. case MEMB_STATE_OPERATIONAL:
  2362. messages_free (instance, token->aru);
  2363. case MEMB_STATE_GATHER:
  2364. /*
  2365. * DO NOT add break, we use different free mechanism in recovery state
  2366. */
  2367. case MEMB_STATE_RECOVERY:
  2368. last_aru = instance->my_last_aru;
  2369. instance->my_last_aru = token->aru;
  2370. /*
  2371. * Discard tokens from another configuration
  2372. */
  2373. if (memcmp (&token->ring_id, &instance->my_ring_id,
  2374. sizeof (struct memb_ring_id)) != 0) {
  2375. if ((forward_token)
  2376. && instance->use_heartbeat) {
  2377. reset_heartbeat_timeout(instance);
  2378. }
  2379. else {
  2380. cancel_heartbeat_timeout(instance);
  2381. }
  2382. return (0); /* discard token */
  2383. }
  2384. /*
  2385. * Discard retransmitted tokens
  2386. */
  2387. if (sq_lte_compare (token->token_seq, instance->my_token_seq)) {
  2388. /*
  2389. * If this processor receives a retransmitted token, it is sure
  2390. * the previous processor is still alive. As a result, it can
  2391. * reset its token timeout. If some processor previous to that
  2392. * has failed, it will eventually not execute a reset of the
  2393. * token timeout, and will cause a reconfiguration to occur.
  2394. */
  2395. reset_token_timeout (instance);
  2396. if ((forward_token)
  2397. && instance->use_heartbeat) {
  2398. reset_heartbeat_timeout(instance);
  2399. }
  2400. else {
  2401. cancel_heartbeat_timeout(instance);
  2402. }
  2403. return (0); /* discard token */
  2404. }
  2405. transmits_allowed = fcc_calculate (instance, token);
  2406. mcasted_retransmit = orf_token_rtr (instance, token, &transmits_allowed);
  2407. if (sq_lt_compare (instance->last_released + MISSING_MCAST_WINDOW, token->seq + TRANSMITS_ALLOWED)) {
  2408. transmits_allowed = 0;
  2409. }
  2410. mcasted_regular = orf_token_mcast (instance, token, transmits_allowed, system_from);
  2411. fcc_token_update (instance, token, mcasted_retransmit +
  2412. mcasted_regular);
  2413. if (sq_lt_compare (instance->my_aru, token->aru) ||
  2414. totemip_equal(&instance->my_id, &token->aru_addr) ||
  2415. totemip_zero_check(&token->aru_addr)) {
  2416. token->aru = instance->my_aru;
  2417. if (token->aru == token->seq) {
  2418. totemip_zero_set(&token->aru_addr);
  2419. } else {
  2420. totemip_copy(&token->aru_addr, &instance->my_id);
  2421. }
  2422. }
  2423. if (token->aru == last_aru && !totemip_zero_check(&token->aru_addr)) {
  2424. instance->my_aru_count += 1;
  2425. } else {
  2426. instance->my_aru_count = 0;
  2427. }
  2428. if (instance->my_aru_count > instance->totem_config->fail_to_recv_const &&
  2429. !totemip_equal(&token->aru_addr, &instance->my_id)) {
  2430. instance->totemsrp_log_printf (instance->totemsrp_log_level_error,
  2431. "FAILED TO RECEIVE\n");
  2432. // TODO if we fail to receive, it may be possible to end with a gather
  2433. // state of proc == failed = 0 entries
  2434. memb_set_merge (&token->aru_addr, 1,
  2435. instance->my_failed_list,
  2436. &instance->my_failed_list_entries);
  2437. ring_state_restore (instance);
  2438. memb_state_gather_enter (instance);
  2439. } else {
  2440. instance->my_token_seq = token->token_seq;
  2441. token->token_seq += 1;
  2442. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2443. /*
  2444. * instance->my_aru == instance->my_high_seq_received means this processor
  2445. * has recovered all messages it can recover
  2446. * (ie: its retrans queue is empty)
  2447. */
  2448. low_water = instance->my_aru;
  2449. if (sq_lt_compare (last_aru, low_water)) {
  2450. low_water = last_aru;
  2451. }
  2452. // TODO is this code right
  2453. if (queue_is_empty (&instance->retrans_message_queue) == 0 ||
  2454. low_water != instance->my_high_seq_received) {
  2455. if (token->retrans_flg == 0) {
  2456. token->retrans_flg = 1;
  2457. instance->my_set_retrans_flg = 1;
  2458. }
  2459. } else
  2460. if (token->retrans_flg == 1 && instance->my_set_retrans_flg) {
  2461. token->retrans_flg = 0;
  2462. }
  2463. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2464. "token retrans flag is %d my set retrans flag%d retrans queue empty %d count %d, low_water %x aru %x\n",
  2465. token->retrans_flg, instance->my_set_retrans_flg,
  2466. queue_is_empty (&instance->retrans_message_queue),
  2467. instance->my_retrans_flg_count, low_water, token->aru);
  2468. if (token->retrans_flg == 0) {
  2469. instance->my_retrans_flg_count += 1;
  2470. } else {
  2471. instance->my_retrans_flg_count = 0;
  2472. }
  2473. if (instance->my_retrans_flg_count == 2) {
  2474. instance->my_install_seq = token->seq;
  2475. }
  2476. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2477. "install seq %x aru %x high seq received %x\n",
  2478. instance->my_install_seq, instance->my_aru, instance->my_high_seq_received);
  2479. if (instance->my_retrans_flg_count >= 2 && instance->my_aru >= instance->my_install_seq && instance->my_received_flg == 0) {
  2480. instance->my_received_flg = 1;
  2481. instance->my_deliver_memb_entries = instance->my_trans_memb_entries;
  2482. memcpy (instance->my_deliver_memb_list, instance->my_trans_memb_list,
  2483. sizeof (struct totem_ip_address) * instance->my_trans_memb_entries);
  2484. }
  2485. if (instance->my_retrans_flg_count >= 3 && token->aru >= instance->my_install_seq) {
  2486. instance->my_rotation_counter += 1;
  2487. } else {
  2488. instance->my_rotation_counter = 0;
  2489. }
  2490. if (instance->my_rotation_counter == 2) {
  2491. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2492. "retrans flag count %x token aru %x install seq %x aru %x %x\n",
  2493. instance->my_retrans_flg_count, token->aru, instance->my_install_seq,
  2494. instance->my_aru, token->seq);
  2495. memb_state_operational_enter (instance);
  2496. instance->my_rotation_counter = 0;
  2497. instance->my_retrans_flg_count = 0;
  2498. }
  2499. }
  2500. totemrrp_send_flush (instance->totemrrp_handle);
  2501. token_send (instance, token, forward_token);
  2502. #ifdef GIVEINFO
  2503. gettimeofday (&tv_current, NULL);
  2504. timersub (&tv_current, &tv_old, &tv_diff);
  2505. memcpy (&tv_old, &tv_current, sizeof (struct timeval));
  2506. if ((((float)tv_diff.tv_usec) / 100.0) > 5.0) {
  2507. printf ("I held %0.4f ms\n", ((float)tv_diff.tv_usec) / 100.0);
  2508. }
  2509. #endif
  2510. if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
  2511. messages_deliver_to_app (instance, 0,
  2512. instance->my_high_seq_received);
  2513. }
  2514. /*
  2515. * Deliver messages after token has been transmitted
  2516. * to improve performance
  2517. */
  2518. reset_token_timeout (instance); // REVIEWED
  2519. reset_token_retransmit_timeout (instance); // REVIEWED
  2520. if (totemip_equal(&instance->my_id, &instance->my_ring_id.rep) &&
  2521. instance->my_token_held == 1) {
  2522. start_token_hold_retransmit_timeout (instance);
  2523. }
  2524. token_callbacks_execute (instance, TOTEM_CALLBACK_TOKEN_SENT);
  2525. }
  2526. break;
  2527. }
  2528. if ((forward_token)
  2529. && instance->use_heartbeat) {
  2530. reset_heartbeat_timeout(instance);
  2531. }
  2532. else {
  2533. cancel_heartbeat_timeout(instance);
  2534. }
  2535. return (0);
  2536. }
  2537. static void messages_deliver_to_app (
  2538. struct totemsrp_instance *instance,
  2539. int skip,
  2540. unsigned int end_point)
  2541. {
  2542. struct sort_queue_item *sort_queue_item_p;
  2543. unsigned int i;
  2544. int res;
  2545. struct mcast *mcast;
  2546. unsigned int range = 0;
  2547. unsigned int my_high_delivered_stored = 0;
  2548. struct totem_ip_address msg_source;
  2549. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2550. "Delivering %x to %x\n", instance->my_high_delivered,
  2551. end_point);
  2552. range = end_point - instance->my_high_delivered;
  2553. assert (range < 10240);
  2554. my_high_delivered_stored = instance->my_high_delivered;
  2555. /*
  2556. * Deliver messages in order from rtr queue to pending delivery queue
  2557. */
  2558. for (i = 1; i <= range; i++) {
  2559. void *ptr = 0;
  2560. /*
  2561. * If out of range of sort queue, stop assembly
  2562. */
  2563. res = sq_in_range (&instance->regular_sort_queue,
  2564. my_high_delivered_stored + i);
  2565. if (res == 0) {
  2566. break;
  2567. }
  2568. res = sq_item_get (&instance->regular_sort_queue,
  2569. my_high_delivered_stored + i, &ptr);
  2570. /*
  2571. * If hole, stop assembly
  2572. */
  2573. if (res != 0 && skip == 0) {
  2574. break;
  2575. }
  2576. instance->my_high_delivered = my_high_delivered_stored + i;
  2577. if (res != 0) {
  2578. continue;
  2579. }
  2580. sort_queue_item_p = ptr;
  2581. mcast = sort_queue_item_p->iovec[0].iov_base;
  2582. assert (mcast != (struct mcast *)0xdeadbeef);
  2583. /*
  2584. * Skip messages not originated in instance->my_deliver_memb
  2585. */
  2586. if (skip &&
  2587. memb_set_subset (&mcast->source,
  2588. 1,
  2589. instance->my_deliver_memb_list,
  2590. instance->my_deliver_memb_entries) == 0) {
  2591. instance->my_high_delivered = my_high_delivered_stored + i;
  2592. continue;
  2593. }
  2594. /*
  2595. * Message found
  2596. */
  2597. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2598. "Delivering MCAST message with seq %x to pending delivery queue\n",
  2599. mcast->seq);
  2600. if (mcast->header.endian_detector == ENDIAN_LOCAL) {
  2601. totemip_copy (&msg_source, &mcast->source);
  2602. } else {
  2603. totemip_copy_endian_convert (&msg_source, &mcast->source);
  2604. }
  2605. /*
  2606. * Message is locally originated multicast
  2607. */
  2608. if (sort_queue_item_p->iov_len > 1 &&
  2609. sort_queue_item_p->iovec[0].iov_len == sizeof (struct mcast)) {
  2610. instance->totemsrp_deliver_fn (
  2611. &msg_source,
  2612. &sort_queue_item_p->iovec[1],
  2613. sort_queue_item_p->iov_len - 1,
  2614. mcast->header.endian_detector != ENDIAN_LOCAL);
  2615. } else {
  2616. sort_queue_item_p->iovec[0].iov_len -= sizeof (struct mcast);
  2617. sort_queue_item_p->iovec[0].iov_base += sizeof (struct mcast);
  2618. instance->totemsrp_deliver_fn (
  2619. &msg_source,
  2620. sort_queue_item_p->iovec,
  2621. sort_queue_item_p->iov_len,
  2622. mcast->header.endian_detector != ENDIAN_LOCAL);
  2623. sort_queue_item_p->iovec[0].iov_len += sizeof (struct mcast);
  2624. sort_queue_item_p->iovec[0].iov_base -= sizeof (struct mcast);
  2625. }
  2626. //TODO instance->stats_delv += 1;
  2627. }
  2628. }
  2629. /*
  2630. * recv message handler called when MCAST message type received
  2631. */
  2632. static int message_handler_mcast (
  2633. struct totemsrp_instance *instance,
  2634. struct totem_ip_address *system_from,
  2635. void *msg,
  2636. int msg_len,
  2637. int endian_conversion_needed)
  2638. {
  2639. struct sort_queue_item sort_queue_item;
  2640. struct sq *sort_queue;
  2641. struct mcast mcast_header;
  2642. if (endian_conversion_needed) {
  2643. mcast_endian_convert (msg, &mcast_header);
  2644. } else {
  2645. memcpy (&mcast_header, msg, sizeof (struct mcast));
  2646. }
  2647. /*
  2648. if (mcast_header.header.encapsulated == 1) {
  2649. sort_queue = &instance->recovery_sort_queue;
  2650. } else {
  2651. sort_queue = &instance->regular_sort_queue;
  2652. }
  2653. */
  2654. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2655. sort_queue = &instance->recovery_sort_queue;
  2656. } else {
  2657. sort_queue = &instance->regular_sort_queue;
  2658. }
  2659. assert (msg_len < FRAME_SIZE_MAX);
  2660. #ifdef TEST_DROP_MCAST_PERCENTAGE
  2661. if (random()%100 < TEST_DROP_MCAST_PERCENTAGE) {
  2662. printf ("dropping message %d\n", mcast_header.seq);
  2663. return (0);
  2664. } else {
  2665. printf ("accepting message %d\n", mcast_header.seq);
  2666. }
  2667. #endif
  2668. if (!totemip_equal(system_from, &instance->my_id)) {
  2669. cancel_token_retransmit_timeout (instance);
  2670. }
  2671. assert (system_from->nodeid != 0);
  2672. /*
  2673. * If the message is foreign execute the switch below
  2674. */
  2675. if (memcmp (&instance->my_ring_id, &mcast_header.ring_id,
  2676. sizeof (struct memb_ring_id)) != 0) {
  2677. switch (instance->memb_state) {
  2678. case MEMB_STATE_OPERATIONAL:
  2679. memb_set_merge (system_from, 1,
  2680. instance->my_proc_list, &instance->my_proc_list_entries);
  2681. memb_state_gather_enter (instance);
  2682. break;
  2683. case MEMB_STATE_GATHER:
  2684. if (!memb_set_subset (system_from,
  2685. 1,
  2686. instance->my_proc_list,
  2687. instance->my_proc_list_entries)) {
  2688. memb_set_merge (system_from, 1,
  2689. instance->my_proc_list, &instance->my_proc_list_entries);
  2690. memb_state_gather_enter (instance);
  2691. return (0);
  2692. }
  2693. break;
  2694. case MEMB_STATE_COMMIT:
  2695. /* discard message */
  2696. break;
  2697. case MEMB_STATE_RECOVERY:
  2698. /* discard message */
  2699. break;
  2700. }
  2701. return (0);
  2702. }
  2703. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2704. "Received ringid(%s:%lld) seq %x\n",
  2705. totemip_print (&mcast_header.ring_id.rep),
  2706. mcast_header.ring_id.seq,
  2707. mcast_header.seq);
  2708. /*
  2709. * Add mcast message to rtr queue if not already in rtr queue
  2710. * otherwise free io vectors
  2711. */
  2712. if (msg_len > 0 && msg_len < FRAME_SIZE_MAX &&
  2713. sq_in_range (sort_queue, mcast_header.seq) &&
  2714. sq_item_inuse (sort_queue, mcast_header.seq) == 0) {
  2715. /*
  2716. * Allocate new multicast memory block
  2717. */
  2718. // TODO LEAK
  2719. sort_queue_item.iovec[0].iov_base = malloc (msg_len);
  2720. if (sort_queue_item.iovec[0].iov_base == 0) {
  2721. return (-1); /* error here is corrected by the algorithm */
  2722. }
  2723. memcpy (sort_queue_item.iovec[0].iov_base, msg, msg_len);
  2724. sort_queue_item.iovec[0].iov_len = msg_len;
  2725. assert (sort_queue_item.iovec[0].iov_len > 0);
  2726. assert (sort_queue_item.iovec[0].iov_len < FRAME_SIZE_MAX);
  2727. sort_queue_item.iov_len = 1;
  2728. if (sq_lt_compare (instance->my_high_seq_received,
  2729. mcast_header.seq)) {
  2730. instance->my_high_seq_received = mcast_header.seq;
  2731. }
  2732. sq_item_add (sort_queue, &sort_queue_item, mcast_header.seq);
  2733. }
  2734. update_aru (instance);
  2735. if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
  2736. messages_deliver_to_app (instance, 0, instance->my_high_seq_received);
  2737. }
  2738. /* TODO remove from retrans message queue for old ring in recovery state */
  2739. return (0);
  2740. }
  2741. static int message_handler_memb_merge_detect (
  2742. struct totemsrp_instance *instance,
  2743. struct totem_ip_address *system_from,
  2744. void *msg,
  2745. int msg_len,
  2746. int endian_conversion_needed)
  2747. {
  2748. struct memb_merge_detect *memb_merge_detect = (struct memb_merge_detect *)msg;
  2749. if (endian_conversion_needed) {
  2750. memb_merge_detect_endian_convert (msg, msg);
  2751. }
  2752. /*
  2753. * do nothing if this is a merge detect from this configuration
  2754. */
  2755. if (memcmp (&instance->my_ring_id, &memb_merge_detect->ring_id,
  2756. sizeof (struct memb_ring_id)) == 0) {
  2757. return (0);
  2758. }
  2759. assert (system_from->nodeid != 0);
  2760. /*
  2761. * Execute merge operation
  2762. */
  2763. switch (instance->memb_state) {
  2764. case MEMB_STATE_OPERATIONAL:
  2765. memb_set_merge (system_from, 1,
  2766. instance->my_proc_list, &instance->my_proc_list_entries);
  2767. memb_state_gather_enter (instance);
  2768. break;
  2769. case MEMB_STATE_GATHER:
  2770. if (!memb_set_subset (system_from,
  2771. 1,
  2772. instance->my_proc_list,
  2773. instance->my_proc_list_entries)) {
  2774. memb_set_merge (system_from, 1,
  2775. instance->my_proc_list, &instance->my_proc_list_entries);
  2776. memb_state_gather_enter (instance);
  2777. return (0);
  2778. }
  2779. break;
  2780. case MEMB_STATE_COMMIT:
  2781. /* do nothing in commit */
  2782. break;
  2783. case MEMB_STATE_RECOVERY:
  2784. /* do nothing in recovery */
  2785. break;
  2786. }
  2787. return (0);
  2788. }
  2789. static int memb_join_process (
  2790. struct totemsrp_instance *instance,
  2791. struct memb_join *memb_join,
  2792. struct totem_ip_address *system_from)
  2793. {
  2794. struct memb_commit_token my_commit_token;
  2795. if (memb_set_equal (memb_join->proc_list,
  2796. memb_join->proc_list_entries,
  2797. instance->my_proc_list,
  2798. instance->my_proc_list_entries) &&
  2799. memb_set_equal (memb_join->failed_list,
  2800. memb_join->failed_list_entries,
  2801. instance->my_failed_list,
  2802. instance->my_failed_list_entries)) {
  2803. memb_consensus_set (instance, system_from);
  2804. if (memb_consensus_agreed (instance) &&
  2805. memb_lowest_in_config (instance)) {
  2806. memb_state_commit_token_create (instance, &my_commit_token);
  2807. memb_state_commit_enter (instance, &my_commit_token);
  2808. } else {
  2809. return (0);
  2810. }
  2811. } else
  2812. if (memb_set_subset (memb_join->proc_list,
  2813. memb_join->proc_list_entries,
  2814. instance->my_proc_list,
  2815. instance->my_proc_list_entries) &&
  2816. memb_set_subset (memb_join->failed_list,
  2817. memb_join->failed_list_entries,
  2818. instance->my_failed_list,
  2819. instance->my_failed_list_entries)) {
  2820. return (0);
  2821. } else
  2822. if (memb_set_subset (system_from, 1,
  2823. instance->my_failed_list, instance->my_failed_list_entries)) {
  2824. return (0);
  2825. } else {
  2826. memb_set_merge (memb_join->proc_list,
  2827. memb_join->proc_list_entries,
  2828. instance->my_proc_list, &instance->my_proc_list_entries);
  2829. if (memb_set_subset (&instance->my_id, 1,
  2830. memb_join->failed_list, memb_join->failed_list_entries)) {
  2831. memb_set_merge (system_from, 1,
  2832. instance->my_failed_list, &instance->my_failed_list_entries);
  2833. } else {
  2834. memb_set_merge (memb_join->failed_list,
  2835. memb_join->failed_list_entries,
  2836. instance->my_failed_list, &instance->my_failed_list_entries);
  2837. }
  2838. memb_state_gather_enter (instance);
  2839. return (1); /* gather entered */
  2840. }
  2841. return (0); /* gather not entered */
  2842. }
  2843. static void memb_join_endian_convert (struct memb_join *in, struct memb_join *out)
  2844. {
  2845. int i;
  2846. out->header.type = in->header.type;
  2847. out->header.endian_detector = ENDIAN_LOCAL;
  2848. out->header.nodeid = swab32 (in->header.nodeid);
  2849. out->proc_list_entries = swab32 (in->proc_list_entries);
  2850. out->failed_list_entries = swab32 (in->failed_list_entries);
  2851. out->ring_seq = swab64 (in->ring_seq);
  2852. for (i = 0; i < out->proc_list_entries; i++) {
  2853. totemip_copy_endian_convert(&out->proc_list[i], &in->proc_list[i]);
  2854. }
  2855. for (i = 0; i < out->failed_list_entries; i++) {
  2856. totemip_copy_endian_convert(&out->failed_list[i], &in->failed_list[i]);
  2857. }
  2858. }
  2859. static void memb_commit_token_endian_convert (struct memb_commit_token *in, struct memb_commit_token *out)
  2860. {
  2861. int i;
  2862. out->header.type = in->header.type;
  2863. out->header.endian_detector = ENDIAN_LOCAL;
  2864. out->header.nodeid = swab32 (in->header.nodeid);
  2865. out->token_seq = swab32 (in->token_seq);
  2866. totemip_copy_endian_convert(&out->ring_id.rep, &in->ring_id.rep);
  2867. out->ring_id.seq = swab64 (in->ring_id.seq);
  2868. out->retrans_flg = swab32 (in->retrans_flg);
  2869. out->memb_index = swab32 (in->memb_index);
  2870. out->addr_entries = swab32 (in->addr_entries);
  2871. for (i = 0; i < out->addr_entries; i++) {
  2872. totemip_copy_endian_convert(&out->addr[i], &in->addr[i]);
  2873. /*
  2874. * Only convert the memb entry if it has been set
  2875. */
  2876. if (in->memb_list[i].ring_id.rep.family != 0) {
  2877. totemip_copy_endian_convert(&out->memb_list[i].ring_id.rep,
  2878. &in->memb_list[i].ring_id.rep);
  2879. out->memb_list[i].ring_id.seq =
  2880. swab64 (in->memb_list[i].ring_id.seq);
  2881. out->memb_list[i].aru = swab32 (in->memb_list[i].aru);
  2882. out->memb_list[i].high_delivered = swab32 (in->memb_list[i].high_delivered);
  2883. out->memb_list[i].received_flg = swab32 (in->memb_list[i].received_flg);
  2884. }
  2885. }
  2886. }
  2887. static void orf_token_endian_convert (struct orf_token *in, struct orf_token *out)
  2888. {
  2889. int i;
  2890. out->header.type = in->header.type;
  2891. out->header.endian_detector = ENDIAN_LOCAL;
  2892. out->header.nodeid = swab32 (in->header.nodeid);
  2893. out->seq = swab32 (in->seq);
  2894. out->token_seq = swab32 (in->token_seq);
  2895. out->aru = swab32 (in->aru);
  2896. totemip_copy_endian_convert(&out->ring_id.rep, &in->ring_id.rep);
  2897. totemip_copy_endian_convert(&out->aru_addr, &in->aru_addr);
  2898. out->ring_id.seq = swab64 (in->ring_id.seq);
  2899. out->fcc = swab32 (in->fcc);
  2900. out->backlog = swab32 (in->backlog);
  2901. out->retrans_flg = swab32 (in->retrans_flg);
  2902. out->rtr_list_entries = swab32 (in->rtr_list_entries);
  2903. for (i = 0; i < out->rtr_list_entries; i++) {
  2904. totemip_copy_endian_convert(&out->rtr_list[i].ring_id.rep, &in->rtr_list[i].ring_id.rep);
  2905. out->rtr_list[i].ring_id.seq = swab64 (in->rtr_list[i].ring_id.seq);
  2906. out->rtr_list[i].seq = swab32 (in->rtr_list[i].seq);
  2907. }
  2908. }
  2909. static void mcast_endian_convert (struct mcast *in, struct mcast *out)
  2910. {
  2911. out->header.type = in->header.type;
  2912. out->header.endian_detector = ENDIAN_LOCAL;
  2913. out->header.nodeid = swab32 (in->header.nodeid);
  2914. out->seq = swab32 (in->seq);
  2915. totemip_copy_endian_convert(&out->ring_id.rep, &in->ring_id.rep);
  2916. totemip_copy_endian_convert(&out->source, &in->source);
  2917. out->ring_id.seq = swab64 (in->ring_id.seq);
  2918. out->guarantee = in->guarantee;
  2919. }
  2920. static void memb_merge_detect_endian_convert (
  2921. struct memb_merge_detect *in,
  2922. struct memb_merge_detect *out)
  2923. {
  2924. out->header.type = in->header.type;
  2925. out->header.endian_detector = ENDIAN_LOCAL;
  2926. out->header.nodeid = swab32 (in->header.nodeid);
  2927. totemip_copy_endian_convert(&out->ring_id.rep, &in->ring_id.rep);
  2928. out->ring_id.seq = swab64 (in->ring_id.seq);
  2929. }
  2930. static int message_handler_memb_join (
  2931. struct totemsrp_instance *instance,
  2932. struct totem_ip_address *system_from,
  2933. void *msg,
  2934. int msg_len,
  2935. int endian_conversion_needed)
  2936. {
  2937. struct memb_join *memb_join;
  2938. struct memb_join memb_join_convert;
  2939. int gather_entered;
  2940. if (endian_conversion_needed) {
  2941. memb_join = &memb_join_convert;
  2942. memb_join_endian_convert (msg, &memb_join_convert);
  2943. } else {
  2944. memb_join = (struct memb_join *)msg;
  2945. }
  2946. memb_set_merge(memb_join->proc_list, memb_join->proc_list_entries,
  2947. instance->my_nodeid_lookup_list, &instance->my_nodeid_lookup_entries);
  2948. assert (system_from->nodeid != 0);
  2949. if (instance->token_ring_id_seq < memb_join->ring_seq) {
  2950. instance->token_ring_id_seq = memb_join->ring_seq;
  2951. }
  2952. switch (instance->memb_state) {
  2953. case MEMB_STATE_OPERATIONAL:
  2954. gather_entered = memb_join_process (instance,
  2955. memb_join, system_from);
  2956. if (gather_entered == 0) {
  2957. memb_state_gather_enter (instance);
  2958. }
  2959. break;
  2960. case MEMB_STATE_GATHER:
  2961. memb_join_process (instance, memb_join, system_from);
  2962. break;
  2963. case MEMB_STATE_COMMIT:
  2964. if (memb_set_subset (system_from,
  2965. 1,
  2966. instance->my_new_memb_list,
  2967. instance->my_new_memb_entries) &&
  2968. memb_join->ring_seq >= instance->my_ring_id.seq) {
  2969. memb_join_process (instance, memb_join, system_from);
  2970. memb_state_gather_enter (instance);
  2971. }
  2972. break;
  2973. case MEMB_STATE_RECOVERY:
  2974. if (memb_set_subset (system_from,
  2975. 1,
  2976. instance->my_new_memb_list,
  2977. instance->my_new_memb_entries) &&
  2978. memb_join->ring_seq >= instance->my_ring_id.seq) {
  2979. ring_state_restore (instance);
  2980. memb_join_process (instance,memb_join,
  2981. system_from);
  2982. memb_state_gather_enter (instance);
  2983. }
  2984. break;
  2985. }
  2986. return (0);
  2987. }
  2988. static int message_handler_memb_commit_token (
  2989. struct totemsrp_instance *instance,
  2990. struct totem_ip_address *system_from,
  2991. void *msg,
  2992. int msg_len,
  2993. int endian_conversion_needed)
  2994. {
  2995. struct memb_commit_token memb_commit_token_convert;
  2996. struct memb_commit_token *memb_commit_token;
  2997. struct totem_ip_address sub[PROCESSOR_COUNT_MAX];
  2998. int sub_entries;
  2999. if (endian_conversion_needed) {
  3000. memb_commit_token = &memb_commit_token_convert;
  3001. memb_commit_token_endian_convert (msg, memb_commit_token);
  3002. } else {
  3003. memb_commit_token = (struct memb_commit_token *)msg;
  3004. }
  3005. /* TODO do we need to check for a duplicate token?
  3006. if (memb_commit_token->token_seq > 0 &&
  3007. instance->my_token_seq >= memb_commit_token->token_seq) {
  3008. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  3009. "already received commit token %d %d\n",
  3010. memb_commit_token->token_seq, instance->my_token_seq);
  3011. return (0);
  3012. }
  3013. */
  3014. #ifdef TEST_DROP_COMMIT_TOKEN_PERCENTAGE
  3015. if (random()%100 < TEST_DROP_COMMIT_TOKEN_PERCENTAGE) {
  3016. return (0);
  3017. }
  3018. #endif
  3019. switch (instance->memb_state) {
  3020. case MEMB_STATE_OPERATIONAL:
  3021. /* discard token */
  3022. break;
  3023. case MEMB_STATE_GATHER:
  3024. memb_set_subtract (sub, &sub_entries,
  3025. instance->my_proc_list, instance->my_proc_list_entries,
  3026. instance->my_failed_list, instance->my_failed_list_entries);
  3027. if (memb_set_equal (memb_commit_token->addr,
  3028. memb_commit_token->addr_entries,
  3029. sub,
  3030. sub_entries) &&
  3031. memb_commit_token->ring_id.seq > instance->my_ring_id.seq) {
  3032. memb_state_commit_enter (instance, memb_commit_token);
  3033. }
  3034. break;
  3035. case MEMB_STATE_COMMIT:
  3036. if (memcmp (&memb_commit_token->ring_id, &instance->my_ring_id,
  3037. sizeof (struct memb_ring_id)) == 0) {
  3038. // if (memb_commit_token->ring_id.seq == instance->my_ring_id.seq) {
  3039. memb_state_recovery_enter (instance, memb_commit_token);
  3040. }
  3041. break;
  3042. case MEMB_STATE_RECOVERY:
  3043. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  3044. "Sending initial ORF token\n");
  3045. if (totemip_equal(&instance->my_id, &instance->my_ring_id.rep)) {
  3046. // TODO convert instead of initiate
  3047. orf_token_send_initial (instance);
  3048. reset_token_timeout (instance); // REVIEWED
  3049. reset_token_retransmit_timeout (instance); // REVIEWED
  3050. }
  3051. break;
  3052. }
  3053. return (0);
  3054. }
  3055. static int message_handler_token_hold_cancel (
  3056. struct totemsrp_instance *instance,
  3057. struct totem_ip_address *system_from,
  3058. void *msg,
  3059. int msg_len,
  3060. int endian_conversion_needed)
  3061. {
  3062. struct token_hold_cancel *token_hold_cancel = (struct token_hold_cancel *)msg;
  3063. if (memcmp (&token_hold_cancel->ring_id, &instance->my_ring_id,
  3064. sizeof (struct memb_ring_id)) == 0) {
  3065. instance->my_seq_unchanged = 0;
  3066. if (totemip_equal(&instance->my_ring_id.rep, &instance->my_id)) {
  3067. timer_function_token_retransmit_timeout (instance);
  3068. }
  3069. }
  3070. return (0);
  3071. }
  3072. void main_deliver_fn (
  3073. void *context,
  3074. struct totem_ip_address *system_from,
  3075. void *msg,
  3076. int msg_len)
  3077. {
  3078. struct totemsrp_instance *instance = (struct totemsrp_instance *)context;
  3079. struct message_header *message_header = (struct message_header *)msg;
  3080. if (msg_len < sizeof (struct message_header)) {
  3081. instance->totemsrp_log_printf (instance->totemsrp_log_level_security, "Received message is too short... ignoring %d.\n", msg_len);
  3082. return;
  3083. }
  3084. system_from->nodeid = message_header->nodeid;
  3085. assert (system_from->nodeid != 0);
  3086. /*
  3087. * Handle incoming message
  3088. */
  3089. totemsrp_message_handlers.handler_functions[(int)message_header->type] (
  3090. instance,
  3091. system_from,
  3092. msg,
  3093. msg_len,
  3094. message_header->endian_detector != ENDIAN_LOCAL);
  3095. }
  3096. void main_iface_change_fn (
  3097. void *context,
  3098. struct totem_ip_address *iface_addr)
  3099. {
  3100. struct totemsrp_instance *instance = (struct totemsrp_instance *)context;
  3101. totemip_copy (&instance->my_id, iface_addr);
  3102. assert (instance->my_id.nodeid);
  3103. totemip_copy (&instance->my_memb_list[0], iface_addr);
  3104. if (instance->first_run++ == 0) {
  3105. memb_ring_id_create_or_load (instance, &instance->my_ring_id);
  3106. instance->totemsrp_log_printf (
  3107. instance->totemsrp_log_level_notice,
  3108. "Created or loaded sequence id %lld.%s for this ring.\n",
  3109. instance->my_ring_id.seq,
  3110. totemip_print (&instance->my_ring_id.rep));
  3111. }
  3112. memb_state_gather_enter (instance);
  3113. }
  3114. void totemsrp_net_mtu_adjust (struct totem_config *totem_config) {
  3115. totem_config->net_mtu -= sizeof (struct mcast);
  3116. }