totemsrp.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497
  1. /*
  2. * Copyright (c) 2003-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  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 <config.h>
  49. #include <assert.h>
  50. #ifdef HAVE_ALLOCA_H
  51. #include <alloca.h>
  52. #endif
  53. #include <sys/mman.h>
  54. #include <sys/types.h>
  55. #include <sys/stat.h>
  56. #include <sys/socket.h>
  57. #include <netdb.h>
  58. #include <sys/un.h>
  59. #include <sys/ioctl.h>
  60. #include <sys/param.h>
  61. #include <netinet/in.h>
  62. #include <arpa/inet.h>
  63. #include <unistd.h>
  64. #include <fcntl.h>
  65. #include <stdlib.h>
  66. #include <stdio.h>
  67. #include <errno.h>
  68. #include <sched.h>
  69. #include <time.h>
  70. #include <sys/time.h>
  71. #include <sys/poll.h>
  72. #include <limits.h>
  73. #include <qb/qbdefs.h>
  74. #include <qb/qbutil.h>
  75. #include <qb/qbloop.h>
  76. #include <corosync/swab.h>
  77. #include <corosync/cs_queue.h>
  78. #include <corosync/sq.h>
  79. #include <corosync/list.h>
  80. #include <corosync/hdb.h>
  81. #define LOGSYS_UTILS_ONLY 1
  82. #include <corosync/engine/logsys.h>
  83. #include "totemsrp.h"
  84. #include "totemrrp.h"
  85. #include "totemnet.h"
  86. #include "crypto.h"
  87. #define LOCALHOST_IP inet_addr("127.0.0.1")
  88. #define QUEUE_RTR_ITEMS_SIZE_MAX 16384 /* allow 16384 retransmit items */
  89. #define RETRANS_MESSAGE_QUEUE_SIZE_MAX 16384 /* allow 500 messages to be queued */
  90. #define RECEIVED_MESSAGE_QUEUE_SIZE_MAX 500 /* allow 500 messages to be queued */
  91. #define MAXIOVS 5
  92. #define RETRANSMIT_ENTRIES_MAX 30
  93. #define TOKEN_SIZE_MAX 64000 /* bytes */
  94. #define LEAVE_DUMMY_NODEID 0
  95. /*
  96. * Rollover handling:
  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 starting values.
  100. *
  101. * SEQNO_START_TOKEN is the starting sequence number after a new configuration
  102. * for a token. This should remain zero, unless testing overflow in which
  103. * case 07fffff00 or 0xffffff00 are good starting values.
  104. *
  105. * SEQNO_START_MSG is the starting sequence number after a new configuration
  106. * This should remain zero, unless testing overflow in which case
  107. * 0x7ffff000 and 0xfffff000 are good values to start with
  108. */
  109. #define SEQNO_START_MSG 0x0
  110. #define SEQNO_START_TOKEN 0x0
  111. /*
  112. * These can be used ot test different rollover points
  113. * #define SEQNO_START_MSG 0xfffffe00
  114. * #define SEQNO_START_TOKEN 0xfffffe00
  115. */
  116. /*
  117. * These can be used to test the error recovery algorithms
  118. * #define TEST_DROP_ORF_TOKEN_PERCENTAGE 30
  119. * #define TEST_DROP_COMMIT_TOKEN_PERCENTAGE 30
  120. * #define TEST_DROP_MCAST_PERCENTAGE 50
  121. * #define TEST_RECOVERY_MSG_COUNT 300
  122. */
  123. /*
  124. * we compare incoming messages to determine if their endian is
  125. * different - if so convert them
  126. *
  127. * do not change
  128. */
  129. #define ENDIAN_LOCAL 0xff22
  130. enum message_type {
  131. MESSAGE_TYPE_ORF_TOKEN = 0, /* Ordering, Reliability, Flow (ORF) control Token */
  132. MESSAGE_TYPE_MCAST = 1, /* ring ordered multicast message */
  133. MESSAGE_TYPE_MEMB_MERGE_DETECT = 2, /* merge rings if there are available rings */
  134. MESSAGE_TYPE_MEMB_JOIN = 3, /* membership join message */
  135. MESSAGE_TYPE_MEMB_COMMIT_TOKEN = 4, /* membership commit token */
  136. MESSAGE_TYPE_TOKEN_HOLD_CANCEL = 5, /* cancel the holding of the token */
  137. };
  138. enum encapsulation_type {
  139. MESSAGE_ENCAPSULATED = 1,
  140. MESSAGE_NOT_ENCAPSULATED = 2
  141. };
  142. /*
  143. * New membership algorithm local variables
  144. */
  145. struct srp_addr {
  146. struct totem_ip_address addr[INTERFACE_MAX];
  147. };
  148. struct consensus_list_item {
  149. struct srp_addr addr;
  150. int set;
  151. };
  152. struct token_callback_instance {
  153. struct list_head list;
  154. int (*callback_fn) (enum totem_callback_token_type type, const void *);
  155. enum totem_callback_token_type callback_type;
  156. int delete;
  157. void *data;
  158. };
  159. struct totemsrp_socket {
  160. int mcast;
  161. int token;
  162. };
  163. struct message_header {
  164. char type;
  165. char encapsulated;
  166. unsigned short endian_detector;
  167. unsigned int nodeid;
  168. } __attribute__((packed));
  169. struct mcast {
  170. struct message_header header;
  171. struct srp_addr system_from;
  172. unsigned int seq;
  173. int this_seqno;
  174. struct memb_ring_id ring_id;
  175. unsigned int node_id;
  176. int guarantee;
  177. } __attribute__((packed));
  178. struct rtr_item {
  179. struct memb_ring_id ring_id;
  180. unsigned int seq;
  181. }__attribute__((packed));
  182. struct orf_token {
  183. struct message_header header;
  184. unsigned int seq;
  185. unsigned int token_seq;
  186. unsigned int aru;
  187. unsigned int aru_addr;
  188. struct memb_ring_id ring_id;
  189. unsigned int backlog;
  190. unsigned int fcc;
  191. int retrans_flg;
  192. int rtr_list_entries;
  193. struct rtr_item rtr_list[0];
  194. }__attribute__((packed));
  195. struct memb_join {
  196. struct message_header header;
  197. struct srp_addr system_from;
  198. unsigned int proc_list_entries;
  199. unsigned int failed_list_entries;
  200. unsigned long long ring_seq;
  201. unsigned char end_of_memb_join[0];
  202. /*
  203. * These parts of the data structure are dynamic:
  204. * struct srp_addr proc_list[];
  205. * struct srp_addr failed_list[];
  206. */
  207. } __attribute__((packed));
  208. struct memb_merge_detect {
  209. struct message_header header;
  210. struct srp_addr system_from;
  211. struct memb_ring_id ring_id;
  212. } __attribute__((packed));
  213. struct token_hold_cancel {
  214. struct message_header header;
  215. struct memb_ring_id ring_id;
  216. } __attribute__((packed));
  217. struct memb_commit_token_memb_entry {
  218. struct memb_ring_id ring_id;
  219. unsigned int aru;
  220. unsigned int high_delivered;
  221. unsigned int received_flg;
  222. }__attribute__((packed));
  223. struct memb_commit_token {
  224. struct message_header header;
  225. unsigned int token_seq;
  226. struct memb_ring_id ring_id;
  227. unsigned int retrans_flg;
  228. int memb_index;
  229. int addr_entries;
  230. unsigned char end_of_commit_token[0];
  231. /*
  232. * These parts of the data structure are dynamic:
  233. *
  234. * struct srp_addr addr[PROCESSOR_COUNT_MAX];
  235. * struct memb_commit_token_memb_entry memb_list[PROCESSOR_COUNT_MAX];
  236. */
  237. }__attribute__((packed));
  238. struct message_item {
  239. struct mcast *mcast;
  240. unsigned int msg_len;
  241. };
  242. struct sort_queue_item {
  243. struct mcast *mcast;
  244. unsigned int msg_len;
  245. };
  246. struct orf_token_mcast_thread_state {
  247. char iobuf[9000];
  248. prng_state prng_state;
  249. };
  250. enum memb_state {
  251. MEMB_STATE_OPERATIONAL = 1,
  252. MEMB_STATE_GATHER = 2,
  253. MEMB_STATE_COMMIT = 3,
  254. MEMB_STATE_RECOVERY = 4
  255. };
  256. struct totemsrp_instance {
  257. int iface_changes;
  258. int failed_to_recv;
  259. /*
  260. * Flow control mcasts and remcasts on last and current orf_token
  261. */
  262. int fcc_remcast_last;
  263. int fcc_mcast_last;
  264. int fcc_remcast_current;
  265. struct consensus_list_item consensus_list[PROCESSOR_COUNT_MAX];
  266. int consensus_list_entries;
  267. struct srp_addr my_id;
  268. struct srp_addr my_proc_list[PROCESSOR_COUNT_MAX];
  269. struct srp_addr my_failed_list[PROCESSOR_COUNT_MAX];
  270. struct srp_addr my_new_memb_list[PROCESSOR_COUNT_MAX];
  271. struct srp_addr my_trans_memb_list[PROCESSOR_COUNT_MAX];
  272. struct srp_addr my_memb_list[PROCESSOR_COUNT_MAX];
  273. struct srp_addr my_deliver_memb_list[PROCESSOR_COUNT_MAX];
  274. struct srp_addr my_left_memb_list[PROCESSOR_COUNT_MAX];
  275. int my_proc_list_entries;
  276. int my_failed_list_entries;
  277. int my_new_memb_entries;
  278. int my_trans_memb_entries;
  279. int my_memb_entries;
  280. int my_deliver_memb_entries;
  281. int my_left_memb_entries;
  282. struct memb_ring_id my_ring_id;
  283. struct memb_ring_id my_old_ring_id;
  284. int my_aru_count;
  285. int my_merge_detect_timeout_outstanding;
  286. unsigned int my_last_aru;
  287. int my_seq_unchanged;
  288. int my_received_flg;
  289. unsigned int my_high_seq_received;
  290. unsigned int my_install_seq;
  291. int my_rotation_counter;
  292. int my_set_retrans_flg;
  293. int my_retrans_flg_count;
  294. unsigned int my_high_ring_delivered;
  295. int heartbeat_timeout;
  296. /*
  297. * Queues used to order, deliver, and recover messages
  298. */
  299. struct cs_queue new_message_queue;
  300. struct cs_queue retrans_message_queue;
  301. struct sq regular_sort_queue;
  302. struct sq recovery_sort_queue;
  303. /*
  304. * Received up to and including
  305. */
  306. unsigned int my_aru;
  307. unsigned int my_high_delivered;
  308. struct list_head token_callback_received_listhead;
  309. struct list_head token_callback_sent_listhead;
  310. char orf_token_retransmit[TOKEN_SIZE_MAX];
  311. int orf_token_retransmit_size;
  312. unsigned int my_token_seq;
  313. /*
  314. * Timers
  315. */
  316. qb_loop_timer_handle timer_pause_timeout;
  317. qb_loop_timer_handle timer_orf_token_timeout;
  318. qb_loop_timer_handle timer_orf_token_retransmit_timeout;
  319. qb_loop_timer_handle timer_orf_token_hold_retransmit_timeout;
  320. qb_loop_timer_handle timer_merge_detect_timeout;
  321. qb_loop_timer_handle memb_timer_state_gather_join_timeout;
  322. qb_loop_timer_handle memb_timer_state_gather_consensus_timeout;
  323. qb_loop_timer_handle memb_timer_state_commit_timeout;
  324. qb_loop_timer_handle timer_heartbeat_timeout;
  325. /*
  326. * Function and data used to log messages
  327. */
  328. int totemsrp_log_level_security;
  329. int totemsrp_log_level_error;
  330. int totemsrp_log_level_warning;
  331. int totemsrp_log_level_notice;
  332. int totemsrp_log_level_debug;
  333. int totemsrp_subsys_id;
  334. void (*totemsrp_log_printf) (
  335. int level,
  336. int sybsys,
  337. const char *function,
  338. const char *file,
  339. int line,
  340. const char *format, ...)__attribute__((format(printf, 6, 7)));;
  341. enum memb_state memb_state;
  342. //TODO struct srp_addr next_memb;
  343. qb_loop_t *totemsrp_poll_handle;
  344. struct totem_ip_address mcast_address;
  345. void (*totemsrp_deliver_fn) (
  346. unsigned int nodeid,
  347. const void *msg,
  348. unsigned int msg_len,
  349. int endian_conversion_required);
  350. void (*totemsrp_confchg_fn) (
  351. enum totem_configuration_type configuration_type,
  352. const unsigned int *member_list, size_t member_list_entries,
  353. const unsigned int *left_list, size_t left_list_entries,
  354. const unsigned int *joined_list, size_t joined_list_entries,
  355. const struct memb_ring_id *ring_id);
  356. void (*totemsrp_service_ready_fn) (void);
  357. int global_seqno;
  358. int my_token_held;
  359. unsigned long long token_ring_id_seq;
  360. unsigned int last_released;
  361. unsigned int set_aru;
  362. int old_ring_state_saved;
  363. int old_ring_state_aru;
  364. unsigned int old_ring_state_high_seq_received;
  365. unsigned int my_last_seq;
  366. struct timeval tv_old;
  367. void *totemrrp_context;
  368. struct totem_config *totem_config;
  369. unsigned int use_heartbeat;
  370. unsigned int my_trc;
  371. unsigned int my_pbl;
  372. unsigned int my_cbl;
  373. uint64_t pause_timestamp;
  374. struct memb_commit_token *commit_token;
  375. totemsrp_stats_t stats;
  376. uint32_t orf_token_discard;
  377. void * token_recv_event_handle;
  378. void * token_sent_event_handle;
  379. char commit_token_storage[40000];
  380. };
  381. struct message_handlers {
  382. int count;
  383. int (*handler_functions[6]) (
  384. struct totemsrp_instance *instance,
  385. const void *msg,
  386. size_t msg_len,
  387. int endian_conversion_needed);
  388. };
  389. /*
  390. * forward decls
  391. */
  392. static int message_handler_orf_token (
  393. struct totemsrp_instance *instance,
  394. const void *msg,
  395. size_t msg_len,
  396. int endian_conversion_needed);
  397. static int message_handler_mcast (
  398. struct totemsrp_instance *instance,
  399. const void *msg,
  400. size_t msg_len,
  401. int endian_conversion_needed);
  402. static int message_handler_memb_merge_detect (
  403. struct totemsrp_instance *instance,
  404. const void *msg,
  405. size_t msg_len,
  406. int endian_conversion_needed);
  407. static int message_handler_memb_join (
  408. struct totemsrp_instance *instance,
  409. const void *msg,
  410. size_t msg_len,
  411. int endian_conversion_needed);
  412. static int message_handler_memb_commit_token (
  413. struct totemsrp_instance *instance,
  414. const void *msg,
  415. size_t msg_len,
  416. int endian_conversion_needed);
  417. static int message_handler_token_hold_cancel (
  418. struct totemsrp_instance *instance,
  419. const void *msg,
  420. size_t msg_len,
  421. int endian_conversion_needed);
  422. static void totemsrp_instance_initialize (struct totemsrp_instance *instance);
  423. static unsigned int main_msgs_missing (void);
  424. static void main_token_seqid_get (
  425. const void *msg,
  426. unsigned int *seqid,
  427. unsigned int *token_is);
  428. static void srp_addr_copy (struct srp_addr *dest, const struct srp_addr *src);
  429. static void srp_addr_to_nodeid (
  430. unsigned int *nodeid_out,
  431. struct srp_addr *srp_addr_in,
  432. unsigned int entries);
  433. static int srp_addr_equal (const struct srp_addr *a, const struct srp_addr *b);
  434. static void memb_leave_message_send (struct totemsrp_instance *instance);
  435. static void memb_ring_id_create_or_load (struct totemsrp_instance *, struct memb_ring_id *);
  436. static void token_callbacks_execute (struct totemsrp_instance *instance, enum totem_callback_token_type type);
  437. static void memb_state_gather_enter (struct totemsrp_instance *instance, int gather_from);
  438. static void messages_deliver_to_app (struct totemsrp_instance *instance, int skip, unsigned int end_point);
  439. static int orf_token_mcast (struct totemsrp_instance *instance, struct orf_token *oken,
  440. int fcc_mcasts_allowed);
  441. static void messages_free (struct totemsrp_instance *instance, unsigned int token_aru);
  442. static void memb_ring_id_set_and_store (struct totemsrp_instance *instance,
  443. const struct memb_ring_id *ring_id);
  444. static void target_set_completed (void *context);
  445. static void memb_state_commit_token_update (struct totemsrp_instance *instance);
  446. static void memb_state_commit_token_target_set (struct totemsrp_instance *instance);
  447. static int memb_state_commit_token_send (struct totemsrp_instance *instance);
  448. static int memb_state_commit_token_send_recovery (struct totemsrp_instance *instance, struct memb_commit_token *memb_commit_token);
  449. static void memb_state_commit_token_create (struct totemsrp_instance *instance);
  450. static int token_hold_cancel_send (struct totemsrp_instance *instance);
  451. static void orf_token_endian_convert (const struct orf_token *in, struct orf_token *out);
  452. static void memb_commit_token_endian_convert (const struct memb_commit_token *in, struct memb_commit_token *out);
  453. static void memb_join_endian_convert (const struct memb_join *in, struct memb_join *out);
  454. static void mcast_endian_convert (const struct mcast *in, struct mcast *out);
  455. static void memb_merge_detect_endian_convert (
  456. const struct memb_merge_detect *in,
  457. struct memb_merge_detect *out);
  458. static void srp_addr_copy_endian_convert (struct srp_addr *out, const struct srp_addr *in);
  459. static void timer_function_orf_token_timeout (void *data);
  460. static void timer_function_pause_timeout (void *data);
  461. static void timer_function_heartbeat_timeout (void *data);
  462. static void timer_function_token_retransmit_timeout (void *data);
  463. static void timer_function_token_hold_retransmit_timeout (void *data);
  464. static void timer_function_merge_detect_timeout (void *data);
  465. static void *totemsrp_buffer_alloc (struct totemsrp_instance *instance);
  466. static void totemsrp_buffer_release (struct totemsrp_instance *instance, void *ptr);
  467. void main_deliver_fn (
  468. void *context,
  469. const void *msg,
  470. unsigned int msg_len);
  471. void main_iface_change_fn (
  472. void *context,
  473. const struct totem_ip_address *iface_address,
  474. unsigned int iface_no);
  475. struct message_handlers totemsrp_message_handlers = {
  476. 6,
  477. {
  478. message_handler_orf_token,
  479. message_handler_mcast,
  480. message_handler_memb_merge_detect,
  481. message_handler_memb_join,
  482. message_handler_memb_commit_token,
  483. message_handler_token_hold_cancel
  484. }
  485. };
  486. static const char *rundir = NULL;
  487. #define log_printf(level, format, args...) \
  488. do { \
  489. instance->totemsrp_log_printf ( \
  490. level, instance->totemsrp_subsys_id, \
  491. __FUNCTION__, __FILE__, __LINE__, \
  492. format, ##args); \
  493. } while (0);
  494. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  495. do { \
  496. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  497. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  498. instance->totemsrp_log_printf ( \
  499. level, instance->totemsrp_subsys_id, \
  500. __FUNCTION__, __FILE__, __LINE__, \
  501. fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \
  502. } while(0)
  503. static void totemsrp_instance_initialize (struct totemsrp_instance *instance)
  504. {
  505. memset (instance, 0, sizeof (struct totemsrp_instance));
  506. list_init (&instance->token_callback_received_listhead);
  507. list_init (&instance->token_callback_sent_listhead);
  508. instance->my_received_flg = 1;
  509. instance->my_token_seq = SEQNO_START_TOKEN - 1;
  510. instance->memb_state = MEMB_STATE_OPERATIONAL;
  511. instance->set_aru = -1;
  512. instance->my_aru = SEQNO_START_MSG;
  513. instance->my_high_seq_received = SEQNO_START_MSG;
  514. instance->my_high_delivered = SEQNO_START_MSG;
  515. instance->orf_token_discard = 0;
  516. instance->commit_token = (struct memb_commit_token *)instance->commit_token_storage;
  517. }
  518. static void main_token_seqid_get (
  519. const void *msg,
  520. unsigned int *seqid,
  521. unsigned int *token_is)
  522. {
  523. const struct orf_token *token = msg;
  524. *seqid = 0;
  525. *token_is = 0;
  526. if (token->header.type == MESSAGE_TYPE_ORF_TOKEN) {
  527. *seqid = token->token_seq;
  528. *token_is = 1;
  529. }
  530. }
  531. static unsigned int main_msgs_missing (void)
  532. {
  533. // TODO
  534. return (0);
  535. }
  536. static int pause_flush (struct totemsrp_instance *instance)
  537. {
  538. uint64_t now_msec;
  539. uint64_t timestamp_msec;
  540. int res = 0;
  541. now_msec = (qb_util_nano_current_get () / QB_TIME_NS_IN_MSEC);
  542. timestamp_msec = instance->pause_timestamp / QB_TIME_NS_IN_MSEC;
  543. if ((now_msec - timestamp_msec) > (instance->totem_config->token_timeout / 2)) {
  544. log_printf (instance->totemsrp_log_level_notice,
  545. "Process pause detected for %d ms, flushing membership messages.\n", (unsigned int)(now_msec - timestamp_msec));
  546. /*
  547. * -1 indicates an error from recvmsg
  548. */
  549. do {
  550. res = totemrrp_mcast_recv_empty (instance->totemrrp_context);
  551. } while (res == -1);
  552. }
  553. return (res);
  554. }
  555. static int token_event_stats_collector (enum totem_callback_token_type type, const void *void_instance)
  556. {
  557. struct totemsrp_instance *instance = (struct totemsrp_instance *)void_instance;
  558. uint32_t time_now;
  559. unsigned long long nano_secs = qb_util_nano_current_get ();
  560. time_now = (nano_secs / QB_TIME_NS_IN_MSEC);
  561. if (type == TOTEM_CALLBACK_TOKEN_RECEIVED) {
  562. /* incr latest token the index */
  563. if (instance->stats.latest_token == (TOTEM_TOKEN_STATS_MAX - 1))
  564. instance->stats.latest_token = 0;
  565. else
  566. instance->stats.latest_token++;
  567. if (instance->stats.earliest_token == instance->stats.latest_token) {
  568. /* we have filled up the array, start overwriting */
  569. if (instance->stats.earliest_token == (TOTEM_TOKEN_STATS_MAX - 1))
  570. instance->stats.earliest_token = 0;
  571. else
  572. instance->stats.earliest_token++;
  573. instance->stats.token[instance->stats.earliest_token].rx = 0;
  574. instance->stats.token[instance->stats.earliest_token].tx = 0;
  575. instance->stats.token[instance->stats.earliest_token].backlog_calc = 0;
  576. }
  577. instance->stats.token[instance->stats.latest_token].rx = time_now;
  578. instance->stats.token[instance->stats.latest_token].tx = 0; /* in case we drop the token */
  579. } else {
  580. instance->stats.token[instance->stats.latest_token].tx = time_now;
  581. }
  582. return 0;
  583. }
  584. /*
  585. * Exported interfaces
  586. */
  587. int totemsrp_initialize (
  588. qb_loop_t *poll_handle,
  589. void **srp_context,
  590. struct totem_config *totem_config,
  591. totemmrp_stats_t *stats,
  592. void (*deliver_fn) (
  593. unsigned int nodeid,
  594. const void *msg,
  595. unsigned int msg_len,
  596. int endian_conversion_required),
  597. void (*confchg_fn) (
  598. enum totem_configuration_type configuration_type,
  599. const unsigned int *member_list, size_t member_list_entries,
  600. const unsigned int *left_list, size_t left_list_entries,
  601. const unsigned int *joined_list, size_t joined_list_entries,
  602. const struct memb_ring_id *ring_id))
  603. {
  604. struct totemsrp_instance *instance;
  605. unsigned int res;
  606. instance = malloc (sizeof (struct totemsrp_instance));
  607. if (instance == NULL) {
  608. goto error_exit;
  609. }
  610. rundir = getenv ("COROSYNC_RUN_DIR");
  611. if (rundir == NULL) {
  612. rundir = LOCALSTATEDIR "/lib/corosync";
  613. }
  614. res = mkdir (rundir, 0700);
  615. if (res == -1 && errno != EEXIST) {
  616. goto error_destroy;
  617. }
  618. res = chdir (rundir);
  619. if (res == -1) {
  620. goto error_destroy;
  621. }
  622. totemsrp_instance_initialize (instance);
  623. stats->srp = &instance->stats;
  624. instance->stats.latest_token = 0;
  625. instance->stats.earliest_token = 0;
  626. instance->totem_config = totem_config;
  627. /*
  628. * Configure logging
  629. */
  630. instance->totemsrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  631. instance->totemsrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  632. instance->totemsrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  633. instance->totemsrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  634. instance->totemsrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  635. instance->totemsrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  636. instance->totemsrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  637. /*
  638. * Initialize local variables for totemsrp
  639. */
  640. totemip_copy (&instance->mcast_address, &totem_config->interfaces[0].mcast_addr);
  641. /*
  642. * Display totem configuration
  643. */
  644. log_printf (instance->totemsrp_log_level_debug,
  645. "Token Timeout (%d ms) retransmit timeout (%d ms)\n",
  646. totem_config->token_timeout, totem_config->token_retransmit_timeout);
  647. log_printf (instance->totemsrp_log_level_debug,
  648. "token hold (%d ms) retransmits before loss (%d retrans)\n",
  649. totem_config->token_hold_timeout, totem_config->token_retransmits_before_loss_const);
  650. log_printf (instance->totemsrp_log_level_debug,
  651. "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)\n",
  652. totem_config->join_timeout,
  653. totem_config->send_join_timeout,
  654. totem_config->consensus_timeout,
  655. totem_config->merge_timeout);
  656. log_printf (instance->totemsrp_log_level_debug,
  657. "downcheck (%d ms) fail to recv const (%d msgs)\n",
  658. totem_config->downcheck_timeout, totem_config->fail_to_recv_const);
  659. log_printf (instance->totemsrp_log_level_debug,
  660. "seqno unchanged const (%d rotations) Maximum network MTU %d\n", totem_config->seqno_unchanged_const, totem_config->net_mtu);
  661. log_printf (instance->totemsrp_log_level_debug,
  662. "window size per rotation (%d messages) maximum messages per rotation (%d messages)\n",
  663. totem_config->window_size, totem_config->max_messages);
  664. log_printf (instance->totemsrp_log_level_debug,
  665. "missed count const (%d messages)\n",
  666. totem_config->miss_count_const);
  667. log_printf (instance->totemsrp_log_level_debug,
  668. "send threads (%d threads)\n", totem_config->threads);
  669. log_printf (instance->totemsrp_log_level_debug,
  670. "RRP token expired timeout (%d ms)\n",
  671. totem_config->rrp_token_expired_timeout);
  672. log_printf (instance->totemsrp_log_level_debug,
  673. "RRP token problem counter (%d ms)\n",
  674. totem_config->rrp_problem_count_timeout);
  675. log_printf (instance->totemsrp_log_level_debug,
  676. "RRP threshold (%d problem count)\n",
  677. totem_config->rrp_problem_count_threshold);
  678. log_printf (instance->totemsrp_log_level_debug,
  679. "RRP automatic recovery check timeout (%d ms)\n",
  680. totem_config->rrp_autorecovery_check_timeout);
  681. log_printf (instance->totemsrp_log_level_debug,
  682. "RRP mode set to %s.\n", instance->totem_config->rrp_mode);
  683. log_printf (instance->totemsrp_log_level_debug,
  684. "heartbeat_failures_allowed (%d)\n", totem_config->heartbeat_failures_allowed);
  685. log_printf (instance->totemsrp_log_level_debug,
  686. "max_network_delay (%d ms)\n", totem_config->max_network_delay);
  687. cs_queue_init (&instance->retrans_message_queue, RETRANS_MESSAGE_QUEUE_SIZE_MAX,
  688. sizeof (struct message_item));
  689. sq_init (&instance->regular_sort_queue,
  690. QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct sort_queue_item), 0);
  691. sq_init (&instance->recovery_sort_queue,
  692. QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct sort_queue_item), 0);
  693. instance->totemsrp_poll_handle = poll_handle;
  694. instance->totemsrp_deliver_fn = deliver_fn;
  695. instance->totemsrp_confchg_fn = confchg_fn;
  696. instance->use_heartbeat = 1;
  697. timer_function_pause_timeout (instance);
  698. if ( totem_config->heartbeat_failures_allowed == 0 ) {
  699. log_printf (instance->totemsrp_log_level_debug,
  700. "HeartBeat is Disabled. To enable set heartbeat_failures_allowed > 0\n");
  701. instance->use_heartbeat = 0;
  702. }
  703. if (instance->use_heartbeat) {
  704. instance->heartbeat_timeout
  705. = (totem_config->heartbeat_failures_allowed) * totem_config->token_retransmit_timeout
  706. + totem_config->max_network_delay;
  707. if (instance->heartbeat_timeout >= totem_config->token_timeout) {
  708. log_printf (instance->totemsrp_log_level_debug,
  709. "total heartbeat_timeout (%d ms) is not less than token timeout (%d ms)\n",
  710. instance->heartbeat_timeout,
  711. totem_config->token_timeout);
  712. log_printf (instance->totemsrp_log_level_debug,
  713. "heartbeat_timeout = heartbeat_failures_allowed * token_retransmit_timeout + max_network_delay\n");
  714. log_printf (instance->totemsrp_log_level_debug,
  715. "heartbeat timeout should be less than the token timeout. HeartBeat is Diabled !!\n");
  716. instance->use_heartbeat = 0;
  717. }
  718. else {
  719. log_printf (instance->totemsrp_log_level_debug,
  720. "total heartbeat_timeout (%d ms)\n", instance->heartbeat_timeout);
  721. }
  722. }
  723. totemrrp_initialize (
  724. poll_handle,
  725. &instance->totemrrp_context,
  726. totem_config,
  727. instance,
  728. main_deliver_fn,
  729. main_iface_change_fn,
  730. main_token_seqid_get,
  731. main_msgs_missing,
  732. target_set_completed);
  733. /*
  734. * Must have net_mtu adjusted by totemrrp_initialize first
  735. */
  736. cs_queue_init (&instance->new_message_queue,
  737. MESSAGE_QUEUE_MAX,
  738. sizeof (struct message_item));
  739. totemsrp_callback_token_create (instance,
  740. &instance->token_recv_event_handle,
  741. TOTEM_CALLBACK_TOKEN_RECEIVED,
  742. 0,
  743. token_event_stats_collector,
  744. instance);
  745. totemsrp_callback_token_create (instance,
  746. &instance->token_sent_event_handle,
  747. TOTEM_CALLBACK_TOKEN_SENT,
  748. 0,
  749. token_event_stats_collector,
  750. instance);
  751. *srp_context = instance;
  752. return (0);
  753. error_destroy:
  754. free (instance);
  755. error_exit:
  756. return (-1);
  757. }
  758. void totemsrp_finalize (
  759. void *srp_context)
  760. {
  761. struct totemsrp_instance *instance = (struct totemsrp_instance *)srp_context;
  762. memb_leave_message_send (instance);
  763. free (srp_context);
  764. }
  765. int totemsrp_ifaces_get (
  766. void *srp_context,
  767. unsigned int nodeid,
  768. struct totem_ip_address *interfaces,
  769. char ***status,
  770. unsigned int *iface_count)
  771. {
  772. struct totemsrp_instance *instance = (struct totemsrp_instance *)srp_context;
  773. int res = 0;
  774. unsigned int found = 0;
  775. unsigned int i;
  776. for (i = 0; i < instance->my_memb_entries; i++) {
  777. if (instance->my_memb_list[i].addr[0].nodeid == nodeid) {
  778. found = 1;
  779. break;
  780. }
  781. }
  782. if (found) {
  783. memcpy (interfaces, &instance->my_memb_list[i],
  784. sizeof (struct srp_addr));
  785. *iface_count = instance->totem_config->interface_count;
  786. goto finish;
  787. }
  788. for (i = 0; i < instance->my_left_memb_entries; i++) {
  789. if (instance->my_left_memb_list[i].addr[0].nodeid == nodeid) {
  790. found = 1;
  791. break;
  792. }
  793. }
  794. if (found) {
  795. memcpy (interfaces, &instance->my_left_memb_list[i],
  796. sizeof (struct srp_addr));
  797. *iface_count = instance->totem_config->interface_count;
  798. } else {
  799. res = -1;
  800. }
  801. finish:
  802. totemrrp_ifaces_get (instance->totemrrp_context, status, NULL);
  803. return (res);
  804. }
  805. int totemsrp_crypto_set (
  806. void *srp_context,
  807. unsigned int type)
  808. {
  809. struct totemsrp_instance *instance = (struct totemsrp_instance *)srp_context;
  810. int res;
  811. res = totemrrp_crypto_set(instance->totemrrp_context, type);
  812. return (res);
  813. }
  814. unsigned int totemsrp_my_nodeid_get (
  815. void *srp_context)
  816. {
  817. struct totemsrp_instance *instance = (struct totemsrp_instance *)srp_context;
  818. unsigned int res;
  819. res = instance->totem_config->interfaces[0].boundto.nodeid;
  820. return (res);
  821. }
  822. int totemsrp_my_family_get (
  823. void *srp_context)
  824. {
  825. struct totemsrp_instance *instance = (struct totemsrp_instance *)srp_context;
  826. int res;
  827. res = instance->totem_config->interfaces[0].boundto.family;
  828. return (res);
  829. }
  830. int totemsrp_ring_reenable (
  831. void *srp_context)
  832. {
  833. struct totemsrp_instance *instance = (struct totemsrp_instance *)srp_context;
  834. totemrrp_ring_reenable (instance->totemrrp_context,
  835. instance->totem_config->interface_count);
  836. return (0);
  837. }
  838. /*
  839. * Set operations for use by the membership algorithm
  840. */
  841. static int srp_addr_equal (const struct srp_addr *a, const struct srp_addr *b)
  842. {
  843. unsigned int i;
  844. unsigned int res;
  845. for (i = 0; i < 1; i++) {
  846. res = totemip_equal (&a->addr[i], &b->addr[i]);
  847. if (res == 0) {
  848. return (0);
  849. }
  850. }
  851. return (1);
  852. }
  853. static void srp_addr_copy (struct srp_addr *dest, const struct srp_addr *src)
  854. {
  855. unsigned int i;
  856. for (i = 0; i < INTERFACE_MAX; i++) {
  857. totemip_copy (&dest->addr[i], &src->addr[i]);
  858. }
  859. }
  860. static void srp_addr_to_nodeid (
  861. unsigned int *nodeid_out,
  862. struct srp_addr *srp_addr_in,
  863. unsigned int entries)
  864. {
  865. unsigned int i;
  866. for (i = 0; i < entries; i++) {
  867. nodeid_out[i] = srp_addr_in[i].addr[0].nodeid;
  868. }
  869. }
  870. static void srp_addr_copy_endian_convert (struct srp_addr *out, const struct srp_addr *in)
  871. {
  872. int i;
  873. for (i = 0; i < INTERFACE_MAX; i++) {
  874. totemip_copy_endian_convert (&out->addr[i], &in->addr[i]);
  875. }
  876. }
  877. static void memb_consensus_reset (struct totemsrp_instance *instance)
  878. {
  879. instance->consensus_list_entries = 0;
  880. }
  881. static void memb_set_subtract (
  882. struct srp_addr *out_list, int *out_list_entries,
  883. struct srp_addr *one_list, int one_list_entries,
  884. struct srp_addr *two_list, int two_list_entries)
  885. {
  886. int found = 0;
  887. int i;
  888. int j;
  889. *out_list_entries = 0;
  890. for (i = 0; i < one_list_entries; i++) {
  891. for (j = 0; j < two_list_entries; j++) {
  892. if (srp_addr_equal (&one_list[i], &two_list[j])) {
  893. found = 1;
  894. break;
  895. }
  896. }
  897. if (found == 0) {
  898. srp_addr_copy (&out_list[*out_list_entries], &one_list[i]);
  899. *out_list_entries = *out_list_entries + 1;
  900. }
  901. found = 0;
  902. }
  903. }
  904. /*
  905. * Set consensus for a specific processor
  906. */
  907. static void memb_consensus_set (
  908. struct totemsrp_instance *instance,
  909. const struct srp_addr *addr)
  910. {
  911. int found = 0;
  912. int i;
  913. if (addr->addr[0].nodeid == LEAVE_DUMMY_NODEID)
  914. return;
  915. for (i = 0; i < instance->consensus_list_entries; i++) {
  916. if (srp_addr_equal(addr, &instance->consensus_list[i].addr)) {
  917. found = 1;
  918. break; /* found entry */
  919. }
  920. }
  921. srp_addr_copy (&instance->consensus_list[i].addr, addr);
  922. instance->consensus_list[i].set = 1;
  923. if (found == 0) {
  924. instance->consensus_list_entries++;
  925. }
  926. return;
  927. }
  928. /*
  929. * Is consensus set for a specific processor
  930. */
  931. static int memb_consensus_isset (
  932. struct totemsrp_instance *instance,
  933. const struct srp_addr *addr)
  934. {
  935. int i;
  936. for (i = 0; i < instance->consensus_list_entries; i++) {
  937. if (srp_addr_equal (addr, &instance->consensus_list[i].addr)) {
  938. return (instance->consensus_list[i].set);
  939. }
  940. }
  941. return (0);
  942. }
  943. /*
  944. * Is consensus agreed upon based upon consensus database
  945. */
  946. static int memb_consensus_agreed (
  947. struct totemsrp_instance *instance)
  948. {
  949. struct srp_addr token_memb[PROCESSOR_COUNT_MAX];
  950. int token_memb_entries = 0;
  951. int agreed = 1;
  952. int i;
  953. memb_set_subtract (token_memb, &token_memb_entries,
  954. instance->my_proc_list, instance->my_proc_list_entries,
  955. instance->my_failed_list, instance->my_failed_list_entries);
  956. for (i = 0; i < token_memb_entries; i++) {
  957. if (memb_consensus_isset (instance, &token_memb[i]) == 0) {
  958. agreed = 0;
  959. break;
  960. }
  961. }
  962. assert (token_memb_entries >= 1);
  963. return (agreed);
  964. }
  965. static void memb_consensus_notset (
  966. struct totemsrp_instance *instance,
  967. struct srp_addr *no_consensus_list,
  968. int *no_consensus_list_entries,
  969. struct srp_addr *comparison_list,
  970. int comparison_list_entries)
  971. {
  972. int i;
  973. *no_consensus_list_entries = 0;
  974. for (i = 0; i < instance->my_proc_list_entries; i++) {
  975. if (memb_consensus_isset (instance, &instance->my_proc_list[i]) == 0) {
  976. srp_addr_copy (&no_consensus_list[*no_consensus_list_entries], &instance->my_proc_list[i]);
  977. *no_consensus_list_entries = *no_consensus_list_entries + 1;
  978. }
  979. }
  980. }
  981. /*
  982. * Is set1 equal to set2 Entries can be in different orders
  983. */
  984. static int memb_set_equal (
  985. struct srp_addr *set1, int set1_entries,
  986. struct srp_addr *set2, int set2_entries)
  987. {
  988. int i;
  989. int j;
  990. int found = 0;
  991. if (set1_entries != set2_entries) {
  992. return (0);
  993. }
  994. for (i = 0; i < set2_entries; i++) {
  995. for (j = 0; j < set1_entries; j++) {
  996. if (srp_addr_equal (&set1[j], &set2[i])) {
  997. found = 1;
  998. break;
  999. }
  1000. }
  1001. if (found == 0) {
  1002. return (0);
  1003. }
  1004. found = 0;
  1005. }
  1006. return (1);
  1007. }
  1008. /*
  1009. * Is subset fully contained in fullset
  1010. */
  1011. static int memb_set_subset (
  1012. const struct srp_addr *subset, int subset_entries,
  1013. const struct srp_addr *fullset, int fullset_entries)
  1014. {
  1015. int i;
  1016. int j;
  1017. int found = 0;
  1018. if (subset_entries > fullset_entries) {
  1019. return (0);
  1020. }
  1021. for (i = 0; i < subset_entries; i++) {
  1022. for (j = 0; j < fullset_entries; j++) {
  1023. if (srp_addr_equal (&subset[i], &fullset[j])) {
  1024. found = 1;
  1025. }
  1026. }
  1027. if (found == 0) {
  1028. return (0);
  1029. }
  1030. found = 0;
  1031. }
  1032. return (1);
  1033. }
  1034. /*
  1035. * merge subset into fullset taking care not to add duplicates
  1036. */
  1037. static void memb_set_merge (
  1038. const struct srp_addr *subset, int subset_entries,
  1039. struct srp_addr *fullset, int *fullset_entries)
  1040. {
  1041. int found = 0;
  1042. int i;
  1043. int j;
  1044. for (i = 0; i < subset_entries; i++) {
  1045. for (j = 0; j < *fullset_entries; j++) {
  1046. if (srp_addr_equal (&fullset[j], &subset[i])) {
  1047. found = 1;
  1048. break;
  1049. }
  1050. }
  1051. if (found == 0) {
  1052. srp_addr_copy (&fullset[*fullset_entries], &subset[i]);
  1053. *fullset_entries = *fullset_entries + 1;
  1054. }
  1055. found = 0;
  1056. }
  1057. return;
  1058. }
  1059. static void memb_set_and_with_ring_id (
  1060. struct srp_addr *set1,
  1061. struct memb_ring_id *set1_ring_ids,
  1062. int set1_entries,
  1063. struct srp_addr *set2,
  1064. int set2_entries,
  1065. struct memb_ring_id *old_ring_id,
  1066. struct srp_addr *and,
  1067. int *and_entries)
  1068. {
  1069. int i;
  1070. int j;
  1071. int found = 0;
  1072. *and_entries = 0;
  1073. for (i = 0; i < set2_entries; i++) {
  1074. for (j = 0; j < set1_entries; j++) {
  1075. if (srp_addr_equal (&set1[j], &set2[i])) {
  1076. if (memcmp (&set1_ring_ids[j], old_ring_id, sizeof (struct memb_ring_id)) == 0) {
  1077. found = 1;
  1078. }
  1079. break;
  1080. }
  1081. }
  1082. if (found) {
  1083. srp_addr_copy (&and[*and_entries], &set1[j]);
  1084. *and_entries = *and_entries + 1;
  1085. }
  1086. found = 0;
  1087. }
  1088. return;
  1089. }
  1090. #ifdef CODE_COVERAGE
  1091. static void memb_set_print (
  1092. char *string,
  1093. struct srp_addr *list,
  1094. int list_entries)
  1095. {
  1096. int i;
  1097. int j;
  1098. printf ("List '%s' contains %d entries:\n", string, list_entries);
  1099. for (i = 0; i < list_entries; i++) {
  1100. for (j = 0; j < INTERFACE_MAX; j++) {
  1101. printf ("Address %d\n", i);
  1102. printf ("\tiface %d %s\n", j, totemip_print (&list[i].addr[j]));
  1103. printf ("family %d\n", list[i].addr[j].family);
  1104. }
  1105. }
  1106. }
  1107. #endif
  1108. static void *totemsrp_buffer_alloc (struct totemsrp_instance *instance)
  1109. {
  1110. assert (instance != NULL);
  1111. return totemrrp_buffer_alloc (instance->totemrrp_context);
  1112. }
  1113. static void totemsrp_buffer_release (struct totemsrp_instance *instance, void *ptr)
  1114. {
  1115. assert (instance != NULL);
  1116. totemrrp_buffer_release (instance->totemrrp_context, ptr);
  1117. }
  1118. static void reset_token_retransmit_timeout (struct totemsrp_instance *instance)
  1119. {
  1120. qb_loop_timer_del (instance->totemsrp_poll_handle,
  1121. instance->timer_orf_token_retransmit_timeout);
  1122. qb_loop_timer_add (instance->totemsrp_poll_handle,
  1123. QB_LOOP_MED,
  1124. instance->totem_config->token_retransmit_timeout*QB_TIME_NS_IN_MSEC,
  1125. (void *)instance,
  1126. timer_function_token_retransmit_timeout,
  1127. &instance->timer_orf_token_retransmit_timeout);
  1128. }
  1129. static void start_merge_detect_timeout (struct totemsrp_instance *instance)
  1130. {
  1131. if (instance->my_merge_detect_timeout_outstanding == 0) {
  1132. qb_loop_timer_add (instance->totemsrp_poll_handle,
  1133. QB_LOOP_MED,
  1134. instance->totem_config->merge_timeout*QB_TIME_NS_IN_MSEC,
  1135. (void *)instance,
  1136. timer_function_merge_detect_timeout,
  1137. &instance->timer_merge_detect_timeout);
  1138. instance->my_merge_detect_timeout_outstanding = 1;
  1139. }
  1140. }
  1141. static void cancel_merge_detect_timeout (struct totemsrp_instance *instance)
  1142. {
  1143. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->timer_merge_detect_timeout);
  1144. instance->my_merge_detect_timeout_outstanding = 0;
  1145. }
  1146. /*
  1147. * ring_state_* is used to save and restore the sort queue
  1148. * state when a recovery operation fails (and enters gather)
  1149. */
  1150. static void old_ring_state_save (struct totemsrp_instance *instance)
  1151. {
  1152. if (instance->old_ring_state_saved == 0) {
  1153. instance->old_ring_state_saved = 1;
  1154. memcpy (&instance->my_old_ring_id, &instance->my_ring_id,
  1155. sizeof (struct memb_ring_id));
  1156. instance->old_ring_state_aru = instance->my_aru;
  1157. instance->old_ring_state_high_seq_received = instance->my_high_seq_received;
  1158. log_printf (instance->totemsrp_log_level_debug,
  1159. "Saving state aru %x high seq received %x\n",
  1160. instance->my_aru, instance->my_high_seq_received);
  1161. }
  1162. }
  1163. static void old_ring_state_restore (struct totemsrp_instance *instance)
  1164. {
  1165. instance->my_aru = instance->old_ring_state_aru;
  1166. instance->my_high_seq_received = instance->old_ring_state_high_seq_received;
  1167. log_printf (instance->totemsrp_log_level_debug,
  1168. "Restoring instance->my_aru %x my high seq received %x\n",
  1169. instance->my_aru, instance->my_high_seq_received);
  1170. }
  1171. static void old_ring_state_reset (struct totemsrp_instance *instance)
  1172. {
  1173. log_printf (instance->totemsrp_log_level_debug,
  1174. "Resetting old ring state\n");
  1175. instance->old_ring_state_saved = 0;
  1176. }
  1177. static void reset_pause_timeout (struct totemsrp_instance *instance)
  1178. {
  1179. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->timer_pause_timeout);
  1180. qb_loop_timer_add (instance->totemsrp_poll_handle,
  1181. QB_LOOP_MED,
  1182. instance->totem_config->token_timeout * QB_TIME_NS_IN_MSEC / 5,
  1183. (void *)instance,
  1184. timer_function_pause_timeout,
  1185. &instance->timer_pause_timeout);
  1186. }
  1187. static void reset_token_timeout (struct totemsrp_instance *instance) {
  1188. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->timer_orf_token_timeout);
  1189. qb_loop_timer_add (instance->totemsrp_poll_handle,
  1190. QB_LOOP_MED,
  1191. instance->totem_config->token_timeout*QB_TIME_NS_IN_MSEC,
  1192. (void *)instance,
  1193. timer_function_orf_token_timeout,
  1194. &instance->timer_orf_token_timeout);
  1195. }
  1196. static void reset_heartbeat_timeout (struct totemsrp_instance *instance) {
  1197. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->timer_heartbeat_timeout);
  1198. qb_loop_timer_add (instance->totemsrp_poll_handle,
  1199. QB_LOOP_MED,
  1200. instance->heartbeat_timeout*QB_TIME_NS_IN_MSEC,
  1201. (void *)instance,
  1202. timer_function_heartbeat_timeout,
  1203. &instance->timer_heartbeat_timeout);
  1204. }
  1205. static void cancel_token_timeout (struct totemsrp_instance *instance) {
  1206. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->timer_orf_token_timeout);
  1207. }
  1208. static void cancel_heartbeat_timeout (struct totemsrp_instance *instance) {
  1209. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->timer_heartbeat_timeout);
  1210. }
  1211. static void cancel_token_retransmit_timeout (struct totemsrp_instance *instance)
  1212. {
  1213. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->timer_orf_token_retransmit_timeout);
  1214. }
  1215. static void start_token_hold_retransmit_timeout (struct totemsrp_instance *instance)
  1216. {
  1217. qb_loop_timer_add (instance->totemsrp_poll_handle,
  1218. QB_LOOP_MED,
  1219. instance->totem_config->token_hold_timeout*QB_TIME_NS_IN_MSEC,
  1220. (void *)instance,
  1221. timer_function_token_hold_retransmit_timeout,
  1222. &instance->timer_orf_token_hold_retransmit_timeout);
  1223. }
  1224. static void cancel_token_hold_retransmit_timeout (struct totemsrp_instance *instance)
  1225. {
  1226. qb_loop_timer_del (instance->totemsrp_poll_handle,
  1227. instance->timer_orf_token_hold_retransmit_timeout);
  1228. }
  1229. static void memb_state_consensus_timeout_expired (
  1230. struct totemsrp_instance *instance)
  1231. {
  1232. struct srp_addr no_consensus_list[PROCESSOR_COUNT_MAX];
  1233. int no_consensus_list_entries;
  1234. instance->stats.consensus_timeouts++;
  1235. if (memb_consensus_agreed (instance)) {
  1236. memb_consensus_reset (instance);
  1237. memb_consensus_set (instance, &instance->my_id);
  1238. reset_token_timeout (instance); // REVIEWED
  1239. } else {
  1240. memb_consensus_notset (
  1241. instance,
  1242. no_consensus_list,
  1243. &no_consensus_list_entries,
  1244. instance->my_proc_list,
  1245. instance->my_proc_list_entries);
  1246. memb_set_merge (no_consensus_list, no_consensus_list_entries,
  1247. instance->my_failed_list, &instance->my_failed_list_entries);
  1248. memb_state_gather_enter (instance, 0);
  1249. }
  1250. }
  1251. static void memb_join_message_send (struct totemsrp_instance *instance);
  1252. static void memb_merge_detect_transmit (struct totemsrp_instance *instance);
  1253. /*
  1254. * Timers used for various states of the membership algorithm
  1255. */
  1256. static void timer_function_pause_timeout (void *data)
  1257. {
  1258. struct totemsrp_instance *instance = data;
  1259. instance->pause_timestamp = qb_util_nano_current_get ();
  1260. reset_pause_timeout (instance);
  1261. }
  1262. static void memb_recovery_state_token_loss (struct totemsrp_instance *instance)
  1263. {
  1264. old_ring_state_restore (instance);
  1265. memb_state_gather_enter (instance, 5);
  1266. instance->stats.recovery_token_lost++;
  1267. }
  1268. static void timer_function_orf_token_timeout (void *data)
  1269. {
  1270. struct totemsrp_instance *instance = data;
  1271. switch (instance->memb_state) {
  1272. case MEMB_STATE_OPERATIONAL:
  1273. log_printf (instance->totemsrp_log_level_debug,
  1274. "The token was lost in the OPERATIONAL state.\n");
  1275. log_printf (instance->totemsrp_log_level_notice,
  1276. "A processor failed, forming new configuration.\n");
  1277. totemrrp_iface_check (instance->totemrrp_context);
  1278. memb_state_gather_enter (instance, 2);
  1279. instance->stats.operational_token_lost++;
  1280. break;
  1281. case MEMB_STATE_GATHER:
  1282. log_printf (instance->totemsrp_log_level_debug,
  1283. "The consensus timeout expired.\n");
  1284. memb_state_consensus_timeout_expired (instance);
  1285. memb_state_gather_enter (instance, 3);
  1286. instance->stats.gather_token_lost++;
  1287. break;
  1288. case MEMB_STATE_COMMIT:
  1289. log_printf (instance->totemsrp_log_level_debug,
  1290. "The token was lost in the COMMIT state.\n");
  1291. memb_state_gather_enter (instance, 4);
  1292. instance->stats.commit_token_lost++;
  1293. break;
  1294. case MEMB_STATE_RECOVERY:
  1295. log_printf (instance->totemsrp_log_level_debug,
  1296. "The token was lost in the RECOVERY state.\n");
  1297. memb_recovery_state_token_loss (instance);
  1298. instance->orf_token_discard = 1;
  1299. break;
  1300. }
  1301. }
  1302. static void timer_function_heartbeat_timeout (void *data)
  1303. {
  1304. struct totemsrp_instance *instance = data;
  1305. log_printf (instance->totemsrp_log_level_debug,
  1306. "HeartBeat Timer expired Invoking token loss mechanism in state %d \n", instance->memb_state);
  1307. timer_function_orf_token_timeout(data);
  1308. }
  1309. static void memb_timer_function_state_gather (void *data)
  1310. {
  1311. struct totemsrp_instance *instance = data;
  1312. switch (instance->memb_state) {
  1313. case MEMB_STATE_OPERATIONAL:
  1314. case MEMB_STATE_RECOVERY:
  1315. assert (0); /* this should never happen */
  1316. break;
  1317. case MEMB_STATE_GATHER:
  1318. case MEMB_STATE_COMMIT:
  1319. memb_join_message_send (instance);
  1320. /*
  1321. * Restart the join timeout
  1322. `*/
  1323. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1324. qb_loop_timer_add (instance->totemsrp_poll_handle,
  1325. QB_LOOP_MED,
  1326. instance->totem_config->join_timeout*QB_TIME_NS_IN_MSEC,
  1327. (void *)instance,
  1328. memb_timer_function_state_gather,
  1329. &instance->memb_timer_state_gather_join_timeout);
  1330. break;
  1331. }
  1332. }
  1333. static void memb_timer_function_gather_consensus_timeout (void *data)
  1334. {
  1335. struct totemsrp_instance *instance = data;
  1336. memb_state_consensus_timeout_expired (instance);
  1337. }
  1338. static void deliver_messages_from_recovery_to_regular (struct totemsrp_instance *instance)
  1339. {
  1340. unsigned int i;
  1341. struct sort_queue_item *recovery_message_item;
  1342. struct sort_queue_item regular_message_item;
  1343. unsigned int range = 0;
  1344. int res;
  1345. void *ptr;
  1346. struct mcast *mcast;
  1347. log_printf (instance->totemsrp_log_level_debug,
  1348. "recovery to regular %x-%x\n", SEQNO_START_MSG + 1, instance->my_aru);
  1349. range = instance->my_aru - SEQNO_START_MSG;
  1350. /*
  1351. * Move messages from recovery to regular sort queue
  1352. */
  1353. // todo should i be initialized to 0 or 1 ?
  1354. for (i = 1; i <= range; i++) {
  1355. res = sq_item_get (&instance->recovery_sort_queue,
  1356. i + SEQNO_START_MSG, &ptr);
  1357. if (res != 0) {
  1358. continue;
  1359. }
  1360. recovery_message_item = ptr;
  1361. /*
  1362. * Convert recovery message into regular message
  1363. */
  1364. mcast = recovery_message_item->mcast;
  1365. if (mcast->header.encapsulated == MESSAGE_ENCAPSULATED) {
  1366. /*
  1367. * Message is a recovery message encapsulated
  1368. * in a new ring message
  1369. */
  1370. regular_message_item.mcast =
  1371. (struct mcast *)(((char *)recovery_message_item->mcast) + sizeof (struct mcast));
  1372. regular_message_item.msg_len =
  1373. recovery_message_item->msg_len - sizeof (struct mcast);
  1374. mcast = regular_message_item.mcast;
  1375. } else {
  1376. /*
  1377. * TODO this case shouldn't happen
  1378. */
  1379. continue;
  1380. }
  1381. log_printf (instance->totemsrp_log_level_debug,
  1382. "comparing if ring id is for this processors old ring seqno %d\n",
  1383. mcast->seq);
  1384. /*
  1385. * Only add this message to the regular sort
  1386. * queue if it was originated with the same ring
  1387. * id as the previous ring
  1388. */
  1389. if (memcmp (&instance->my_old_ring_id, &mcast->ring_id,
  1390. sizeof (struct memb_ring_id)) == 0) {
  1391. res = sq_item_inuse (&instance->regular_sort_queue, mcast->seq);
  1392. if (res == 0) {
  1393. sq_item_add (&instance->regular_sort_queue,
  1394. &regular_message_item, mcast->seq);
  1395. if (sq_lt_compare (instance->old_ring_state_high_seq_received, mcast->seq)) {
  1396. instance->old_ring_state_high_seq_received = mcast->seq;
  1397. }
  1398. }
  1399. } else {
  1400. log_printf (instance->totemsrp_log_level_debug,
  1401. "-not adding msg with seq no %x\n", mcast->seq);
  1402. }
  1403. }
  1404. }
  1405. /*
  1406. * Change states in the state machine of the membership algorithm
  1407. */
  1408. static void memb_state_operational_enter (struct totemsrp_instance *instance)
  1409. {
  1410. struct srp_addr joined_list[PROCESSOR_COUNT_MAX];
  1411. int joined_list_entries = 0;
  1412. unsigned int aru_save;
  1413. unsigned int joined_list_totemip[PROCESSOR_COUNT_MAX];
  1414. unsigned int trans_memb_list_totemip[PROCESSOR_COUNT_MAX];
  1415. unsigned int new_memb_list_totemip[PROCESSOR_COUNT_MAX];
  1416. unsigned int left_list[PROCESSOR_COUNT_MAX];
  1417. unsigned int i;
  1418. unsigned int res;
  1419. memb_consensus_reset (instance);
  1420. old_ring_state_reset (instance);
  1421. deliver_messages_from_recovery_to_regular (instance);
  1422. log_printf (instance->totemsrp_log_level_debug,
  1423. "Delivering to app %x to %x\n",
  1424. instance->my_high_delivered + 1, instance->old_ring_state_high_seq_received);
  1425. aru_save = instance->my_aru;
  1426. instance->my_aru = instance->old_ring_state_aru;
  1427. messages_deliver_to_app (instance, 0, instance->old_ring_state_high_seq_received);
  1428. /*
  1429. * Calculate joined and left list
  1430. */
  1431. memb_set_subtract (instance->my_left_memb_list,
  1432. &instance->my_left_memb_entries,
  1433. instance->my_memb_list, instance->my_memb_entries,
  1434. instance->my_trans_memb_list, instance->my_trans_memb_entries);
  1435. memb_set_subtract (joined_list, &joined_list_entries,
  1436. instance->my_new_memb_list, instance->my_new_memb_entries,
  1437. instance->my_trans_memb_list, instance->my_trans_memb_entries);
  1438. /*
  1439. * Install new membership
  1440. */
  1441. instance->my_memb_entries = instance->my_new_memb_entries;
  1442. memcpy (&instance->my_memb_list, instance->my_new_memb_list,
  1443. sizeof (struct srp_addr) * instance->my_memb_entries);
  1444. instance->last_released = 0;
  1445. instance->my_set_retrans_flg = 0;
  1446. /*
  1447. * Deliver transitional configuration to application
  1448. */
  1449. srp_addr_to_nodeid (left_list, instance->my_left_memb_list,
  1450. instance->my_left_memb_entries);
  1451. srp_addr_to_nodeid (trans_memb_list_totemip,
  1452. instance->my_trans_memb_list, instance->my_trans_memb_entries);
  1453. instance->totemsrp_confchg_fn (TOTEM_CONFIGURATION_TRANSITIONAL,
  1454. trans_memb_list_totemip, instance->my_trans_memb_entries,
  1455. left_list, instance->my_left_memb_entries,
  1456. 0, 0, &instance->my_ring_id);
  1457. // TODO we need to filter to ensure we only deliver those
  1458. // messages which are part of instance->my_deliver_memb
  1459. messages_deliver_to_app (instance, 1, instance->old_ring_state_high_seq_received);
  1460. instance->my_aru = aru_save;
  1461. /*
  1462. * Deliver regular configuration to application
  1463. */
  1464. srp_addr_to_nodeid (new_memb_list_totemip,
  1465. instance->my_new_memb_list, instance->my_new_memb_entries);
  1466. srp_addr_to_nodeid (joined_list_totemip, joined_list,
  1467. joined_list_entries);
  1468. instance->totemsrp_confchg_fn (TOTEM_CONFIGURATION_REGULAR,
  1469. new_memb_list_totemip, instance->my_new_memb_entries,
  1470. 0, 0,
  1471. joined_list_totemip, joined_list_entries, &instance->my_ring_id);
  1472. /*
  1473. * The recovery sort queue now becomes the regular
  1474. * sort queue. It is necessary to copy the state
  1475. * into the regular sort queue.
  1476. */
  1477. sq_copy (&instance->regular_sort_queue, &instance->recovery_sort_queue);
  1478. instance->my_last_aru = SEQNO_START_MSG;
  1479. /* When making my_proc_list smaller, ensure that the
  1480. * now non-used entries are zero-ed out. There are some suspect
  1481. * assert's that assume that there is always 2 entries in the list.
  1482. * These fail when my_proc_list is reduced to 1 entry (and the
  1483. * valid [0] entry is the same as the 'unused' [1] entry).
  1484. */
  1485. memset(instance->my_proc_list, 0,
  1486. sizeof (struct srp_addr) * instance->my_proc_list_entries);
  1487. instance->my_proc_list_entries = instance->my_new_memb_entries;
  1488. memcpy (instance->my_proc_list, instance->my_new_memb_list,
  1489. sizeof (struct srp_addr) * instance->my_memb_entries);
  1490. instance->my_failed_list_entries = 0;
  1491. instance->my_high_delivered = instance->my_high_seq_received;
  1492. for (i = 0; i <= instance->my_high_delivered; i++) {
  1493. void *ptr;
  1494. res = sq_item_get (&instance->regular_sort_queue, i, &ptr);
  1495. if (res == 0) {
  1496. struct sort_queue_item *regular_message;
  1497. regular_message = ptr;
  1498. free (regular_message->mcast);
  1499. }
  1500. }
  1501. sq_items_release (&instance->regular_sort_queue, instance->my_high_delivered);
  1502. instance->last_released = instance->my_high_delivered;
  1503. log_printf (instance->totemsrp_log_level_debug,
  1504. "entering OPERATIONAL state.\n");
  1505. log_printf (instance->totemsrp_log_level_notice,
  1506. "A processor joined or left the membership and a new membership was formed.\n");
  1507. instance->memb_state = MEMB_STATE_OPERATIONAL;
  1508. instance->stats.operational_entered++;
  1509. instance->stats.continuous_gather = 0;
  1510. instance->my_received_flg = 1;
  1511. reset_pause_timeout (instance);
  1512. /*
  1513. * Save ring id information from this configuration to determine
  1514. * which processors are transitioning from old regular configuration
  1515. * in to new regular configuration on the next configuration change
  1516. */
  1517. memcpy (&instance->my_old_ring_id, &instance->my_ring_id,
  1518. sizeof (struct memb_ring_id));
  1519. return;
  1520. }
  1521. static void memb_state_gather_enter (
  1522. struct totemsrp_instance *instance,
  1523. int gather_from)
  1524. {
  1525. instance->orf_token_discard = 1;
  1526. memb_set_merge (
  1527. &instance->my_id, 1,
  1528. instance->my_proc_list, &instance->my_proc_list_entries);
  1529. memb_join_message_send (instance);
  1530. /*
  1531. * Restart the join timeout
  1532. */
  1533. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1534. qb_loop_timer_add (instance->totemsrp_poll_handle,
  1535. QB_LOOP_MED,
  1536. instance->totem_config->join_timeout*QB_TIME_NS_IN_MSEC,
  1537. (void *)instance,
  1538. memb_timer_function_state_gather,
  1539. &instance->memb_timer_state_gather_join_timeout);
  1540. /*
  1541. * Restart the consensus timeout
  1542. */
  1543. qb_loop_timer_del (instance->totemsrp_poll_handle,
  1544. instance->memb_timer_state_gather_consensus_timeout);
  1545. qb_loop_timer_add (instance->totemsrp_poll_handle,
  1546. QB_LOOP_MED,
  1547. instance->totem_config->consensus_timeout*QB_TIME_NS_IN_MSEC,
  1548. (void *)instance,
  1549. memb_timer_function_gather_consensus_timeout,
  1550. &instance->memb_timer_state_gather_consensus_timeout);
  1551. /*
  1552. * Cancel the token loss and token retransmission timeouts
  1553. */
  1554. cancel_token_retransmit_timeout (instance); // REVIEWED
  1555. cancel_token_timeout (instance); // REVIEWED
  1556. cancel_merge_detect_timeout (instance);
  1557. memb_consensus_reset (instance);
  1558. memb_consensus_set (instance, &instance->my_id);
  1559. log_printf (instance->totemsrp_log_level_debug,
  1560. "entering GATHER state from %d.\n", gather_from);
  1561. instance->memb_state = MEMB_STATE_GATHER;
  1562. instance->stats.gather_entered++;
  1563. if (gather_from == 3) {
  1564. /*
  1565. * State 3 means gather, so we are continuously gathering.
  1566. */
  1567. instance->stats.continuous_gather++;
  1568. }
  1569. if (instance->stats.continuous_gather > MAX_NO_CONT_GATHER) {
  1570. log_printf (instance->totemsrp_log_level_warning,
  1571. "Totem is unable to form a cluster because of an "
  1572. "operating system or network fault. The most common "
  1573. "cause of this message is that the local firewall is "
  1574. "configured improperly.\n");
  1575. }
  1576. return;
  1577. }
  1578. static void timer_function_token_retransmit_timeout (void *data);
  1579. static void target_set_completed (
  1580. void *context)
  1581. {
  1582. struct totemsrp_instance *instance = (struct totemsrp_instance *)context;
  1583. memb_state_commit_token_send (instance);
  1584. }
  1585. static void memb_state_commit_enter (
  1586. struct totemsrp_instance *instance)
  1587. {
  1588. old_ring_state_save (instance);
  1589. memb_state_commit_token_update (instance);
  1590. memb_state_commit_token_target_set (instance);
  1591. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1592. instance->memb_timer_state_gather_join_timeout = 0;
  1593. qb_loop_timer_del (instance->totemsrp_poll_handle, instance->memb_timer_state_gather_consensus_timeout);
  1594. instance->memb_timer_state_gather_consensus_timeout = 0;
  1595. memb_ring_id_set_and_store (instance, &instance->commit_token->ring_id);
  1596. instance->token_ring_id_seq = instance->my_ring_id.seq;
  1597. log_printf (instance->totemsrp_log_level_debug,
  1598. "entering COMMIT state.\n");
  1599. instance->memb_state = MEMB_STATE_COMMIT;
  1600. reset_token_retransmit_timeout (instance); // REVIEWED
  1601. reset_token_timeout (instance); // REVIEWED
  1602. instance->stats.commit_entered++;
  1603. instance->stats.continuous_gather = 0;
  1604. /*
  1605. * reset all flow control variables since we are starting a new ring
  1606. */
  1607. instance->my_trc = 0;
  1608. instance->my_pbl = 0;
  1609. instance->my_cbl = 0;
  1610. /*
  1611. * commit token sent after callback that token target has been set
  1612. */
  1613. }
  1614. static void memb_state_recovery_enter (
  1615. struct totemsrp_instance *instance,
  1616. struct memb_commit_token *commit_token)
  1617. {
  1618. int i;
  1619. int local_received_flg = 1;
  1620. unsigned int low_ring_aru;
  1621. unsigned int range = 0;
  1622. unsigned int messages_originated = 0;
  1623. const struct srp_addr *addr;
  1624. struct memb_commit_token_memb_entry *memb_list;
  1625. struct memb_ring_id my_new_memb_ring_id_list[PROCESSOR_COUNT_MAX];
  1626. addr = (const struct srp_addr *)commit_token->end_of_commit_token;
  1627. memb_list = (struct memb_commit_token_memb_entry *)(addr + commit_token->addr_entries);
  1628. log_printf (instance->totemsrp_log_level_debug,
  1629. "entering RECOVERY state.\n");
  1630. instance->orf_token_discard = 0;
  1631. instance->my_high_ring_delivered = 0;
  1632. sq_reinit (&instance->recovery_sort_queue, SEQNO_START_MSG);
  1633. cs_queue_reinit (&instance->retrans_message_queue);
  1634. low_ring_aru = instance->old_ring_state_high_seq_received;
  1635. memb_state_commit_token_send_recovery (instance, commit_token);
  1636. instance->my_token_seq = SEQNO_START_TOKEN - 1;
  1637. /*
  1638. * Build regular configuration
  1639. */
  1640. totemrrp_processor_count_set (
  1641. instance->totemrrp_context,
  1642. commit_token->addr_entries);
  1643. /*
  1644. * Build transitional configuration
  1645. */
  1646. for (i = 0; i < instance->my_new_memb_entries; i++) {
  1647. memcpy (&my_new_memb_ring_id_list[i],
  1648. &memb_list[i].ring_id,
  1649. sizeof (struct memb_ring_id));
  1650. }
  1651. memb_set_and_with_ring_id (
  1652. instance->my_new_memb_list,
  1653. my_new_memb_ring_id_list,
  1654. instance->my_new_memb_entries,
  1655. instance->my_memb_list,
  1656. instance->my_memb_entries,
  1657. &instance->my_old_ring_id,
  1658. instance->my_trans_memb_list,
  1659. &instance->my_trans_memb_entries);
  1660. for (i = 0; i < instance->my_trans_memb_entries; i++) {
  1661. log_printf (instance->totemsrp_log_level_debug,
  1662. "TRANS [%d] member %s:\n", i, totemip_print (&instance->my_trans_memb_list[i].addr[0]));
  1663. }
  1664. for (i = 0; i < instance->my_new_memb_entries; i++) {
  1665. log_printf (instance->totemsrp_log_level_debug,
  1666. "position [%d] member %s:\n", i, totemip_print (&addr[i].addr[0]));
  1667. log_printf (instance->totemsrp_log_level_debug,
  1668. "previous ring seq %lld rep %s\n",
  1669. memb_list[i].ring_id.seq,
  1670. totemip_print (&memb_list[i].ring_id.rep));
  1671. log_printf (instance->totemsrp_log_level_debug,
  1672. "aru %x high delivered %x received flag %d\n",
  1673. memb_list[i].aru,
  1674. memb_list[i].high_delivered,
  1675. memb_list[i].received_flg);
  1676. // assert (totemip_print (&memb_list[i].ring_id.rep) != 0);
  1677. }
  1678. /*
  1679. * Determine if any received flag is false
  1680. */
  1681. for (i = 0; i < commit_token->addr_entries; i++) {
  1682. if (memb_set_subset (&instance->my_new_memb_list[i], 1,
  1683. instance->my_trans_memb_list, instance->my_trans_memb_entries) &&
  1684. memb_list[i].received_flg == 0) {
  1685. instance->my_deliver_memb_entries = instance->my_trans_memb_entries;
  1686. memcpy (instance->my_deliver_memb_list, instance->my_trans_memb_list,
  1687. sizeof (struct srp_addr) * instance->my_trans_memb_entries);
  1688. local_received_flg = 0;
  1689. break;
  1690. }
  1691. }
  1692. if (local_received_flg == 1) {
  1693. goto no_originate;
  1694. } /* Else originate messages if we should */
  1695. /*
  1696. * Calculate my_low_ring_aru, instance->my_high_ring_delivered for the transitional membership
  1697. */
  1698. for (i = 0; i < commit_token->addr_entries; i++) {
  1699. if (memb_set_subset (&instance->my_new_memb_list[i], 1,
  1700. instance->my_deliver_memb_list,
  1701. instance->my_deliver_memb_entries) &&
  1702. memcmp (&instance->my_old_ring_id,
  1703. &memb_list[i].ring_id,
  1704. sizeof (struct memb_ring_id)) == 0) {
  1705. if (sq_lt_compare (memb_list[i].aru, low_ring_aru)) {
  1706. low_ring_aru = memb_list[i].aru;
  1707. }
  1708. if (sq_lt_compare (instance->my_high_ring_delivered, memb_list[i].high_delivered)) {
  1709. instance->my_high_ring_delivered = memb_list[i].high_delivered;
  1710. }
  1711. }
  1712. }
  1713. /*
  1714. * Copy all old ring messages to instance->retrans_message_queue
  1715. */
  1716. range = instance->old_ring_state_high_seq_received - low_ring_aru;
  1717. if (range == 0) {
  1718. /*
  1719. * No messages to copy
  1720. */
  1721. goto no_originate;
  1722. }
  1723. assert (range < QUEUE_RTR_ITEMS_SIZE_MAX);
  1724. log_printf (instance->totemsrp_log_level_debug,
  1725. "copying all old ring messages from %x-%x.\n",
  1726. low_ring_aru + 1, instance->old_ring_state_high_seq_received);
  1727. for (i = 1; i <= range; i++) {
  1728. struct sort_queue_item *sort_queue_item;
  1729. struct message_item message_item;
  1730. void *ptr;
  1731. int res;
  1732. res = sq_item_get (&instance->regular_sort_queue,
  1733. low_ring_aru + i, &ptr);
  1734. if (res != 0) {
  1735. continue;
  1736. }
  1737. sort_queue_item = ptr;
  1738. messages_originated++;
  1739. memset (&message_item, 0, sizeof (struct message_item));
  1740. // TODO LEAK
  1741. message_item.mcast = totemsrp_buffer_alloc (instance);
  1742. assert (message_item.mcast);
  1743. message_item.mcast->header.type = MESSAGE_TYPE_MCAST;
  1744. srp_addr_copy (&message_item.mcast->system_from, &instance->my_id);
  1745. message_item.mcast->header.encapsulated = MESSAGE_ENCAPSULATED;
  1746. message_item.mcast->header.nodeid = instance->my_id.addr[0].nodeid;
  1747. assert (message_item.mcast->header.nodeid);
  1748. message_item.mcast->header.endian_detector = ENDIAN_LOCAL;
  1749. memcpy (&message_item.mcast->ring_id, &instance->my_ring_id,
  1750. sizeof (struct memb_ring_id));
  1751. message_item.msg_len = sort_queue_item->msg_len + sizeof (struct mcast);
  1752. memcpy (((char *)message_item.mcast) + sizeof (struct mcast),
  1753. sort_queue_item->mcast,
  1754. sort_queue_item->msg_len);
  1755. cs_queue_item_add (&instance->retrans_message_queue, &message_item);
  1756. }
  1757. log_printf (instance->totemsrp_log_level_debug,
  1758. "Originated %d messages in RECOVERY.\n", messages_originated);
  1759. goto originated;
  1760. no_originate:
  1761. log_printf (instance->totemsrp_log_level_debug,
  1762. "Did not need to originate any messages in recovery.\n");
  1763. originated:
  1764. instance->my_aru = SEQNO_START_MSG;
  1765. instance->my_aru_count = 0;
  1766. instance->my_seq_unchanged = 0;
  1767. instance->my_high_seq_received = SEQNO_START_MSG;
  1768. instance->my_install_seq = SEQNO_START_MSG;
  1769. instance->last_released = SEQNO_START_MSG;
  1770. reset_token_timeout (instance); // REVIEWED
  1771. reset_token_retransmit_timeout (instance); // REVIEWED
  1772. instance->memb_state = MEMB_STATE_RECOVERY;
  1773. instance->stats.recovery_entered++;
  1774. instance->stats.continuous_gather = 0;
  1775. return;
  1776. }
  1777. void totemsrp_event_signal (void *srp_context, enum totem_event_type type, int value)
  1778. {
  1779. struct totemsrp_instance *instance = (struct totemsrp_instance *)srp_context;
  1780. token_hold_cancel_send (instance);
  1781. return;
  1782. }
  1783. int totemsrp_mcast (
  1784. void *srp_context,
  1785. struct iovec *iovec,
  1786. unsigned int iov_len,
  1787. int guarantee)
  1788. {
  1789. struct totemsrp_instance *instance = (struct totemsrp_instance *)srp_context;
  1790. int i;
  1791. struct message_item message_item;
  1792. char *addr;
  1793. unsigned int addr_idx;
  1794. if (cs_queue_is_full (&instance->new_message_queue)) {
  1795. log_printf (instance->totemsrp_log_level_debug, "queue full\n");
  1796. return (-1);
  1797. }
  1798. memset (&message_item, 0, sizeof (struct message_item));
  1799. /*
  1800. * Allocate pending item
  1801. */
  1802. message_item.mcast = totemsrp_buffer_alloc (instance);
  1803. if (message_item.mcast == 0) {
  1804. goto error_mcast;
  1805. }
  1806. /*
  1807. * Set mcast header
  1808. */
  1809. memset(message_item.mcast, 0, sizeof (struct mcast));
  1810. message_item.mcast->header.type = MESSAGE_TYPE_MCAST;
  1811. message_item.mcast->header.endian_detector = ENDIAN_LOCAL;
  1812. message_item.mcast->header.encapsulated = MESSAGE_NOT_ENCAPSULATED;
  1813. message_item.mcast->header.nodeid = instance->my_id.addr[0].nodeid;
  1814. assert (message_item.mcast->header.nodeid);
  1815. message_item.mcast->guarantee = guarantee;
  1816. srp_addr_copy (&message_item.mcast->system_from, &instance->my_id);
  1817. addr = (char *)message_item.mcast;
  1818. addr_idx = sizeof (struct mcast);
  1819. for (i = 0; i < iov_len; i++) {
  1820. memcpy (&addr[addr_idx], iovec[i].iov_base, iovec[i].iov_len);
  1821. addr_idx += iovec[i].iov_len;
  1822. }
  1823. message_item.msg_len = addr_idx;
  1824. log_printf (instance->totemsrp_log_level_debug, "mcasted message added to pending queue\n");
  1825. instance->stats.mcast_tx++;
  1826. cs_queue_item_add (&instance->new_message_queue, &message_item);
  1827. return (0);
  1828. error_mcast:
  1829. return (-1);
  1830. }
  1831. /*
  1832. * Determine if there is room to queue a new message
  1833. */
  1834. int totemsrp_avail (void *srp_context)
  1835. {
  1836. struct totemsrp_instance *instance = (struct totemsrp_instance *)srp_context;
  1837. int avail;
  1838. cs_queue_avail (&instance->new_message_queue, &avail);
  1839. return (avail);
  1840. }
  1841. /*
  1842. * ORF Token Management
  1843. */
  1844. /*
  1845. * Recast message to mcast group if it is available
  1846. */
  1847. static int orf_token_remcast (
  1848. struct totemsrp_instance *instance,
  1849. int seq)
  1850. {
  1851. struct sort_queue_item *sort_queue_item;
  1852. int res;
  1853. void *ptr;
  1854. struct sq *sort_queue;
  1855. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  1856. sort_queue = &instance->recovery_sort_queue;
  1857. } else {
  1858. sort_queue = &instance->regular_sort_queue;
  1859. }
  1860. res = sq_in_range (sort_queue, seq);
  1861. if (res == 0) {
  1862. log_printf (instance->totemsrp_log_level_debug, "sq not in range\n");
  1863. return (-1);
  1864. }
  1865. /*
  1866. * Get RTR item at seq, if not available, return
  1867. */
  1868. res = sq_item_get (sort_queue, seq, &ptr);
  1869. if (res != 0) {
  1870. return -1;
  1871. }
  1872. sort_queue_item = ptr;
  1873. totemrrp_mcast_noflush_send (
  1874. instance->totemrrp_context,
  1875. sort_queue_item->mcast,
  1876. sort_queue_item->msg_len);
  1877. return (0);
  1878. }
  1879. /*
  1880. * Free all freeable messages from ring
  1881. */
  1882. static void messages_free (
  1883. struct totemsrp_instance *instance,
  1884. unsigned int token_aru)
  1885. {
  1886. struct sort_queue_item *regular_message;
  1887. unsigned int i;
  1888. int res;
  1889. int log_release = 0;
  1890. unsigned int release_to;
  1891. unsigned int range = 0;
  1892. release_to = token_aru;
  1893. if (sq_lt_compare (instance->my_last_aru, release_to)) {
  1894. release_to = instance->my_last_aru;
  1895. }
  1896. if (sq_lt_compare (instance->my_high_delivered, release_to)) {
  1897. release_to = instance->my_high_delivered;
  1898. }
  1899. /*
  1900. * Ensure we dont try release before an already released point
  1901. */
  1902. if (sq_lt_compare (release_to, instance->last_released)) {
  1903. return;
  1904. }
  1905. range = release_to - instance->last_released;
  1906. assert (range < QUEUE_RTR_ITEMS_SIZE_MAX);
  1907. /*
  1908. * Release retransmit list items if group aru indicates they are transmitted
  1909. */
  1910. for (i = 1; i <= range; i++) {
  1911. void *ptr;
  1912. res = sq_item_get (&instance->regular_sort_queue,
  1913. instance->last_released + i, &ptr);
  1914. if (res == 0) {
  1915. regular_message = ptr;
  1916. totemsrp_buffer_release (instance, regular_message->mcast);
  1917. }
  1918. sq_items_release (&instance->regular_sort_queue,
  1919. instance->last_released + i);
  1920. log_release = 1;
  1921. }
  1922. instance->last_released += range;
  1923. if (log_release) {
  1924. log_printf (instance->totemsrp_log_level_debug,
  1925. "releasing messages up to and including %x\n", release_to);
  1926. }
  1927. }
  1928. static void update_aru (
  1929. struct totemsrp_instance *instance)
  1930. {
  1931. unsigned int i;
  1932. int res;
  1933. struct sq *sort_queue;
  1934. unsigned int range;
  1935. unsigned int my_aru_saved = 0;
  1936. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  1937. sort_queue = &instance->recovery_sort_queue;
  1938. } else {
  1939. sort_queue = &instance->regular_sort_queue;
  1940. }
  1941. range = instance->my_high_seq_received - instance->my_aru;
  1942. if (range > 1024) {
  1943. return;
  1944. }
  1945. my_aru_saved = instance->my_aru;
  1946. for (i = 1; i <= range; i++) {
  1947. void *ptr;
  1948. res = sq_item_get (sort_queue, my_aru_saved + i, &ptr);
  1949. /*
  1950. * If hole, stop updating aru
  1951. */
  1952. if (res != 0) {
  1953. break;
  1954. }
  1955. }
  1956. instance->my_aru += i - 1;
  1957. }
  1958. /*
  1959. * Multicasts pending messages onto the ring (requires orf_token possession)
  1960. */
  1961. static int orf_token_mcast (
  1962. struct totemsrp_instance *instance,
  1963. struct orf_token *token,
  1964. int fcc_mcasts_allowed)
  1965. {
  1966. struct message_item *message_item = 0;
  1967. struct cs_queue *mcast_queue;
  1968. struct sq *sort_queue;
  1969. struct sort_queue_item sort_queue_item;
  1970. struct mcast *mcast;
  1971. unsigned int fcc_mcast_current;
  1972. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  1973. mcast_queue = &instance->retrans_message_queue;
  1974. sort_queue = &instance->recovery_sort_queue;
  1975. reset_token_retransmit_timeout (instance); // REVIEWED
  1976. } else {
  1977. mcast_queue = &instance->new_message_queue;
  1978. sort_queue = &instance->regular_sort_queue;
  1979. }
  1980. for (fcc_mcast_current = 0; fcc_mcast_current < fcc_mcasts_allowed; fcc_mcast_current++) {
  1981. if (cs_queue_is_empty (mcast_queue)) {
  1982. break;
  1983. }
  1984. message_item = (struct message_item *)cs_queue_item_get (mcast_queue);
  1985. message_item->mcast->seq = ++token->seq;
  1986. message_item->mcast->this_seqno = instance->global_seqno++;
  1987. /*
  1988. * Build IO vector
  1989. */
  1990. memset (&sort_queue_item, 0, sizeof (struct sort_queue_item));
  1991. sort_queue_item.mcast = message_item->mcast;
  1992. sort_queue_item.msg_len = message_item->msg_len;
  1993. mcast = sort_queue_item.mcast;
  1994. memcpy (&mcast->ring_id, &instance->my_ring_id, sizeof (struct memb_ring_id));
  1995. /*
  1996. * Add message to retransmit queue
  1997. */
  1998. sq_item_add (sort_queue, &sort_queue_item, message_item->mcast->seq);
  1999. totemrrp_mcast_noflush_send (
  2000. instance->totemrrp_context,
  2001. message_item->mcast,
  2002. message_item->msg_len);
  2003. /*
  2004. * Delete item from pending queue
  2005. */
  2006. cs_queue_item_remove (mcast_queue);
  2007. /*
  2008. * If messages mcasted, deliver any new messages to totempg
  2009. */
  2010. instance->my_high_seq_received = token->seq;
  2011. }
  2012. update_aru (instance);
  2013. /*
  2014. * Return 1 if more messages are available for single node clusters
  2015. */
  2016. return (fcc_mcast_current);
  2017. }
  2018. /*
  2019. * Remulticasts messages in orf_token's retransmit list (requires orf_token)
  2020. * Modify's orf_token's rtr to include retransmits required by this process
  2021. */
  2022. static int orf_token_rtr (
  2023. struct totemsrp_instance *instance,
  2024. struct orf_token *orf_token,
  2025. unsigned int *fcc_allowed)
  2026. {
  2027. unsigned int res;
  2028. unsigned int i, j;
  2029. unsigned int found;
  2030. struct sq *sort_queue;
  2031. struct rtr_item *rtr_list;
  2032. unsigned int range = 0;
  2033. char retransmit_msg[1024];
  2034. char value[64];
  2035. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2036. sort_queue = &instance->recovery_sort_queue;
  2037. } else {
  2038. sort_queue = &instance->regular_sort_queue;
  2039. }
  2040. rtr_list = &orf_token->rtr_list[0];
  2041. strcpy (retransmit_msg, "Retransmit List: ");
  2042. if (orf_token->rtr_list_entries) {
  2043. log_printf (instance->totemsrp_log_level_debug,
  2044. "Retransmit List %d\n", orf_token->rtr_list_entries);
  2045. for (i = 0; i < orf_token->rtr_list_entries; i++) {
  2046. sprintf (value, "%x ", rtr_list[i].seq);
  2047. strcat (retransmit_msg, value);
  2048. }
  2049. strcat (retransmit_msg, "\n");
  2050. log_printf (instance->totemsrp_log_level_notice,
  2051. "%s", retransmit_msg);
  2052. }
  2053. /*
  2054. * Retransmit messages on orf_token's RTR list from RTR queue
  2055. */
  2056. for (instance->fcc_remcast_current = 0, i = 0;
  2057. instance->fcc_remcast_current < *fcc_allowed && i < orf_token->rtr_list_entries;) {
  2058. /*
  2059. * If this retransmit request isn't from this configuration,
  2060. * try next rtr entry
  2061. */
  2062. if (memcmp (&rtr_list[i].ring_id, &instance->my_ring_id,
  2063. sizeof (struct memb_ring_id)) != 0) {
  2064. i += 1;
  2065. continue;
  2066. }
  2067. res = orf_token_remcast (instance, rtr_list[i].seq);
  2068. if (res == 0) {
  2069. /*
  2070. * Multicasted message, so no need to copy to new retransmit list
  2071. */
  2072. orf_token->rtr_list_entries -= 1;
  2073. assert (orf_token->rtr_list_entries >= 0);
  2074. memmove (&rtr_list[i], &rtr_list[i + 1],
  2075. sizeof (struct rtr_item) * (orf_token->rtr_list_entries - i));
  2076. instance->stats.mcast_retx++;
  2077. instance->fcc_remcast_current++;
  2078. } else {
  2079. i += 1;
  2080. }
  2081. }
  2082. *fcc_allowed = *fcc_allowed - instance->fcc_remcast_current;
  2083. /*
  2084. * Add messages to retransmit to RTR list
  2085. * but only retry if there is room in the retransmit list
  2086. */
  2087. range = orf_token->seq - instance->my_aru;
  2088. assert (range < QUEUE_RTR_ITEMS_SIZE_MAX);
  2089. for (i = 1; (orf_token->rtr_list_entries < RETRANSMIT_ENTRIES_MAX) &&
  2090. (i <= range); i++) {
  2091. /*
  2092. * Ensure message is within the sort queue range
  2093. */
  2094. res = sq_in_range (sort_queue, instance->my_aru + i);
  2095. if (res == 0) {
  2096. break;
  2097. }
  2098. /*
  2099. * Find if a message is missing from this processor
  2100. */
  2101. res = sq_item_inuse (sort_queue, instance->my_aru + i);
  2102. if (res == 0) {
  2103. /*
  2104. * Determine how many times we have missed receiving
  2105. * this sequence number. sq_item_miss_count increments
  2106. * a counter for the sequence number. The miss count
  2107. * will be returned and compared. This allows time for
  2108. * delayed multicast messages to be received before
  2109. * declaring the message is missing and requesting a
  2110. * retransmit.
  2111. */
  2112. res = sq_item_miss_count (sort_queue, instance->my_aru + i);
  2113. if (res < instance->totem_config->miss_count_const) {
  2114. continue;
  2115. }
  2116. /*
  2117. * Determine if missing message is already in retransmit list
  2118. */
  2119. found = 0;
  2120. for (j = 0; j < orf_token->rtr_list_entries; j++) {
  2121. if (instance->my_aru + i == rtr_list[j].seq) {
  2122. found = 1;
  2123. }
  2124. }
  2125. if (found == 0) {
  2126. /*
  2127. * Missing message not found in current retransmit list so add it
  2128. */
  2129. memcpy (&rtr_list[orf_token->rtr_list_entries].ring_id,
  2130. &instance->my_ring_id, sizeof (struct memb_ring_id));
  2131. rtr_list[orf_token->rtr_list_entries].seq = instance->my_aru + i;
  2132. orf_token->rtr_list_entries++;
  2133. }
  2134. }
  2135. }
  2136. return (instance->fcc_remcast_current);
  2137. }
  2138. static void token_retransmit (struct totemsrp_instance *instance)
  2139. {
  2140. totemrrp_token_send (instance->totemrrp_context,
  2141. instance->orf_token_retransmit,
  2142. instance->orf_token_retransmit_size);
  2143. }
  2144. /*
  2145. * Retransmit the regular token if no mcast or token has
  2146. * been received in retransmit token period retransmit
  2147. * the token to the next processor
  2148. */
  2149. static void timer_function_token_retransmit_timeout (void *data)
  2150. {
  2151. struct totemsrp_instance *instance = data;
  2152. switch (instance->memb_state) {
  2153. case MEMB_STATE_GATHER:
  2154. break;
  2155. case MEMB_STATE_COMMIT:
  2156. case MEMB_STATE_OPERATIONAL:
  2157. case MEMB_STATE_RECOVERY:
  2158. token_retransmit (instance);
  2159. reset_token_retransmit_timeout (instance); // REVIEWED
  2160. break;
  2161. }
  2162. }
  2163. static void timer_function_token_hold_retransmit_timeout (void *data)
  2164. {
  2165. struct totemsrp_instance *instance = data;
  2166. switch (instance->memb_state) {
  2167. case MEMB_STATE_GATHER:
  2168. break;
  2169. case MEMB_STATE_COMMIT:
  2170. break;
  2171. case MEMB_STATE_OPERATIONAL:
  2172. case MEMB_STATE_RECOVERY:
  2173. token_retransmit (instance);
  2174. break;
  2175. }
  2176. }
  2177. static void timer_function_merge_detect_timeout(void *data)
  2178. {
  2179. struct totemsrp_instance *instance = data;
  2180. instance->my_merge_detect_timeout_outstanding = 0;
  2181. switch (instance->memb_state) {
  2182. case MEMB_STATE_OPERATIONAL:
  2183. if (totemip_equal(&instance->my_ring_id.rep, &instance->my_id.addr[0])) {
  2184. memb_merge_detect_transmit (instance);
  2185. }
  2186. break;
  2187. case MEMB_STATE_GATHER:
  2188. case MEMB_STATE_COMMIT:
  2189. case MEMB_STATE_RECOVERY:
  2190. break;
  2191. }
  2192. }
  2193. /*
  2194. * Send orf_token to next member (requires orf_token)
  2195. */
  2196. static int token_send (
  2197. struct totemsrp_instance *instance,
  2198. struct orf_token *orf_token,
  2199. int forward_token)
  2200. {
  2201. int res = 0;
  2202. unsigned int orf_token_size;
  2203. orf_token_size = sizeof (struct orf_token) +
  2204. (orf_token->rtr_list_entries * sizeof (struct rtr_item));
  2205. memcpy (instance->orf_token_retransmit, orf_token, orf_token_size);
  2206. instance->orf_token_retransmit_size = orf_token_size;
  2207. orf_token->header.nodeid = instance->my_id.addr[0].nodeid;
  2208. assert (orf_token->header.nodeid);
  2209. if (forward_token == 0) {
  2210. return (0);
  2211. }
  2212. totemrrp_token_send (instance->totemrrp_context,
  2213. orf_token,
  2214. orf_token_size);
  2215. return (res);
  2216. }
  2217. static int token_hold_cancel_send (struct totemsrp_instance *instance)
  2218. {
  2219. struct token_hold_cancel token_hold_cancel;
  2220. /*
  2221. * Only cancel if the token is currently held
  2222. */
  2223. if (instance->my_token_held == 0) {
  2224. return (0);
  2225. }
  2226. instance->my_token_held = 0;
  2227. /*
  2228. * Build message
  2229. */
  2230. token_hold_cancel.header.type = MESSAGE_TYPE_TOKEN_HOLD_CANCEL;
  2231. token_hold_cancel.header.endian_detector = ENDIAN_LOCAL;
  2232. token_hold_cancel.header.encapsulated = 0;
  2233. token_hold_cancel.header.nodeid = instance->my_id.addr[0].nodeid;
  2234. memcpy (&token_hold_cancel.ring_id, &instance->my_ring_id,
  2235. sizeof (struct memb_ring_id));
  2236. assert (token_hold_cancel.header.nodeid);
  2237. instance->stats.token_hold_cancel_tx++;
  2238. totemrrp_mcast_flush_send (instance->totemrrp_context, &token_hold_cancel,
  2239. sizeof (struct token_hold_cancel));
  2240. return (0);
  2241. }
  2242. static int orf_token_send_initial (struct totemsrp_instance *instance)
  2243. {
  2244. struct orf_token orf_token;
  2245. int res;
  2246. orf_token.header.type = MESSAGE_TYPE_ORF_TOKEN;
  2247. orf_token.header.endian_detector = ENDIAN_LOCAL;
  2248. orf_token.header.encapsulated = 0;
  2249. orf_token.header.nodeid = instance->my_id.addr[0].nodeid;
  2250. assert (orf_token.header.nodeid);
  2251. orf_token.seq = SEQNO_START_MSG;
  2252. orf_token.token_seq = SEQNO_START_TOKEN;
  2253. orf_token.retrans_flg = 1;
  2254. instance->my_set_retrans_flg = 1;
  2255. instance->stats.orf_token_tx++;
  2256. if (cs_queue_is_empty (&instance->retrans_message_queue) == 1) {
  2257. orf_token.retrans_flg = 0;
  2258. instance->my_set_retrans_flg = 0;
  2259. } else {
  2260. orf_token.retrans_flg = 1;
  2261. instance->my_set_retrans_flg = 1;
  2262. }
  2263. orf_token.aru = 0;
  2264. orf_token.aru = SEQNO_START_MSG - 1;
  2265. orf_token.aru_addr = instance->my_id.addr[0].nodeid;
  2266. memcpy (&orf_token.ring_id, &instance->my_ring_id, sizeof (struct memb_ring_id));
  2267. orf_token.fcc = 0;
  2268. orf_token.backlog = 0;
  2269. orf_token.rtr_list_entries = 0;
  2270. res = token_send (instance, &orf_token, 1);
  2271. return (res);
  2272. }
  2273. static void memb_state_commit_token_update (
  2274. struct totemsrp_instance *instance)
  2275. {
  2276. struct srp_addr *addr;
  2277. struct memb_commit_token_memb_entry *memb_list;
  2278. unsigned int high_aru;
  2279. unsigned int i;
  2280. addr = (struct srp_addr *)instance->commit_token->end_of_commit_token;
  2281. memb_list = (struct memb_commit_token_memb_entry *)(addr + instance->commit_token->addr_entries);
  2282. memcpy (instance->my_new_memb_list, addr,
  2283. sizeof (struct srp_addr) * instance->commit_token->addr_entries);
  2284. instance->my_new_memb_entries = instance->commit_token->addr_entries;
  2285. memcpy (&memb_list[instance->commit_token->memb_index].ring_id,
  2286. &instance->my_old_ring_id, sizeof (struct memb_ring_id));
  2287. memb_list[instance->commit_token->memb_index].aru = instance->old_ring_state_aru;
  2288. /*
  2289. * TODO high delivered is really instance->my_aru, but with safe this
  2290. * could change?
  2291. */
  2292. instance->my_received_flg =
  2293. (instance->my_aru == instance->my_high_seq_received);
  2294. memb_list[instance->commit_token->memb_index].received_flg = instance->my_received_flg;
  2295. memb_list[instance->commit_token->memb_index].high_delivered = instance->my_high_delivered;
  2296. /*
  2297. * find high aru up to current memb_index for all matching ring ids
  2298. * if any ring id matching memb_index has aru less then high aru set
  2299. * received flag for that entry to false
  2300. */
  2301. high_aru = memb_list[instance->commit_token->memb_index].aru;
  2302. for (i = 0; i <= instance->commit_token->memb_index; i++) {
  2303. if (memcmp (&memb_list[instance->commit_token->memb_index].ring_id,
  2304. &memb_list[i].ring_id,
  2305. sizeof (struct memb_ring_id)) == 0) {
  2306. if (sq_lt_compare (high_aru, memb_list[i].aru)) {
  2307. high_aru = memb_list[i].aru;
  2308. }
  2309. }
  2310. }
  2311. for (i = 0; i <= instance->commit_token->memb_index; i++) {
  2312. if (memcmp (&memb_list[instance->commit_token->memb_index].ring_id,
  2313. &memb_list[i].ring_id,
  2314. sizeof (struct memb_ring_id)) == 0) {
  2315. if (sq_lt_compare (memb_list[i].aru, high_aru)) {
  2316. memb_list[i].received_flg = 0;
  2317. if (i == instance->commit_token->memb_index) {
  2318. instance->my_received_flg = 0;
  2319. }
  2320. }
  2321. }
  2322. }
  2323. instance->commit_token->header.nodeid = instance->my_id.addr[0].nodeid;
  2324. instance->commit_token->memb_index += 1;
  2325. assert (instance->commit_token->memb_index <= instance->commit_token->addr_entries);
  2326. assert (instance->commit_token->header.nodeid);
  2327. }
  2328. static void memb_state_commit_token_target_set (
  2329. struct totemsrp_instance *instance)
  2330. {
  2331. struct srp_addr *addr;
  2332. unsigned int i;
  2333. addr = (struct srp_addr *)instance->commit_token->end_of_commit_token;
  2334. for (i = 0; i < instance->totem_config->interface_count; i++) {
  2335. totemrrp_token_target_set (
  2336. instance->totemrrp_context,
  2337. &addr[instance->commit_token->memb_index %
  2338. instance->commit_token->addr_entries].addr[i],
  2339. i);
  2340. }
  2341. }
  2342. static int memb_state_commit_token_send_recovery (
  2343. struct totemsrp_instance *instance,
  2344. struct memb_commit_token *commit_token)
  2345. {
  2346. unsigned int commit_token_size;
  2347. commit_token->token_seq++;
  2348. commit_token_size = sizeof (struct memb_commit_token) +
  2349. ((sizeof (struct srp_addr) +
  2350. sizeof (struct memb_commit_token_memb_entry)) * commit_token->addr_entries);
  2351. /*
  2352. * Make a copy for retransmission if necessary
  2353. */
  2354. memcpy (instance->orf_token_retransmit, commit_token, commit_token_size);
  2355. instance->orf_token_retransmit_size = commit_token_size;
  2356. instance->stats.memb_commit_token_tx++;
  2357. totemrrp_token_send (instance->totemrrp_context,
  2358. commit_token,
  2359. commit_token_size);
  2360. /*
  2361. * Request retransmission of the commit token in case it is lost
  2362. */
  2363. reset_token_retransmit_timeout (instance);
  2364. return (0);
  2365. }
  2366. static int memb_state_commit_token_send (
  2367. struct totemsrp_instance *instance)
  2368. {
  2369. unsigned int commit_token_size;
  2370. instance->commit_token->token_seq++;
  2371. commit_token_size = sizeof (struct memb_commit_token) +
  2372. ((sizeof (struct srp_addr) +
  2373. sizeof (struct memb_commit_token_memb_entry)) * instance->commit_token->addr_entries);
  2374. /*
  2375. * Make a copy for retransmission if necessary
  2376. */
  2377. memcpy (instance->orf_token_retransmit, instance->commit_token, commit_token_size);
  2378. instance->orf_token_retransmit_size = commit_token_size;
  2379. instance->stats.memb_commit_token_tx++;
  2380. totemrrp_token_send (instance->totemrrp_context,
  2381. instance->commit_token,
  2382. commit_token_size);
  2383. /*
  2384. * Request retransmission of the commit token in case it is lost
  2385. */
  2386. reset_token_retransmit_timeout (instance);
  2387. return (0);
  2388. }
  2389. static int memb_lowest_in_config (struct totemsrp_instance *instance)
  2390. {
  2391. struct srp_addr token_memb[PROCESSOR_COUNT_MAX];
  2392. int token_memb_entries = 0;
  2393. int i;
  2394. struct totem_ip_address *lowest_addr;
  2395. memb_set_subtract (token_memb, &token_memb_entries,
  2396. instance->my_proc_list, instance->my_proc_list_entries,
  2397. instance->my_failed_list, instance->my_failed_list_entries);
  2398. /*
  2399. * find representative by searching for smallest identifier
  2400. */
  2401. lowest_addr = &token_memb[0].addr[0];
  2402. for (i = 1; i < token_memb_entries; i++) {
  2403. if (totemip_compare(lowest_addr, &token_memb[i].addr[0]) > 0) {
  2404. totemip_copy (lowest_addr, &token_memb[i].addr[0]);
  2405. }
  2406. }
  2407. return (totemip_compare (lowest_addr, &instance->my_id.addr[0]) == 0);
  2408. }
  2409. static int srp_addr_compare (const void *a, const void *b)
  2410. {
  2411. const struct srp_addr *srp_a = (const struct srp_addr *)a;
  2412. const struct srp_addr *srp_b = (const struct srp_addr *)b;
  2413. return (totemip_compare (&srp_a->addr[0], &srp_b->addr[0]));
  2414. }
  2415. static void memb_state_commit_token_create (
  2416. struct totemsrp_instance *instance)
  2417. {
  2418. struct srp_addr token_memb[PROCESSOR_COUNT_MAX];
  2419. struct srp_addr *addr;
  2420. struct memb_commit_token_memb_entry *memb_list;
  2421. int token_memb_entries = 0;
  2422. log_printf (instance->totemsrp_log_level_debug,
  2423. "Creating commit token because I am the rep.\n");
  2424. memb_set_subtract (token_memb, &token_memb_entries,
  2425. instance->my_proc_list, instance->my_proc_list_entries,
  2426. instance->my_failed_list, instance->my_failed_list_entries);
  2427. memset (instance->commit_token, 0, sizeof (struct memb_commit_token));
  2428. instance->commit_token->header.type = MESSAGE_TYPE_MEMB_COMMIT_TOKEN;
  2429. instance->commit_token->header.endian_detector = ENDIAN_LOCAL;
  2430. instance->commit_token->header.encapsulated = 0;
  2431. instance->commit_token->header.nodeid = instance->my_id.addr[0].nodeid;
  2432. assert (instance->commit_token->header.nodeid);
  2433. totemip_copy(&instance->commit_token->ring_id.rep, &instance->my_id.addr[0]);
  2434. instance->commit_token->ring_id.seq = instance->token_ring_id_seq + 4;
  2435. /*
  2436. * This qsort is necessary to ensure the commit token traverses
  2437. * the ring in the proper order
  2438. */
  2439. qsort (token_memb, token_memb_entries, sizeof (struct srp_addr),
  2440. srp_addr_compare);
  2441. instance->commit_token->memb_index = 0;
  2442. instance->commit_token->addr_entries = token_memb_entries;
  2443. addr = (struct srp_addr *)instance->commit_token->end_of_commit_token;
  2444. memb_list = (struct memb_commit_token_memb_entry *)(addr + instance->commit_token->addr_entries);
  2445. memcpy (addr, token_memb,
  2446. token_memb_entries * sizeof (struct srp_addr));
  2447. memset (memb_list, 0,
  2448. sizeof (struct memb_commit_token_memb_entry) * token_memb_entries);
  2449. }
  2450. static void memb_join_message_send (struct totemsrp_instance *instance)
  2451. {
  2452. char memb_join_data[40000];
  2453. struct memb_join *memb_join = (struct memb_join *)memb_join_data;
  2454. char *addr;
  2455. unsigned int addr_idx;
  2456. memb_join->header.type = MESSAGE_TYPE_MEMB_JOIN;
  2457. memb_join->header.endian_detector = ENDIAN_LOCAL;
  2458. memb_join->header.encapsulated = 0;
  2459. memb_join->header.nodeid = instance->my_id.addr[0].nodeid;
  2460. assert (memb_join->header.nodeid);
  2461. memb_join->ring_seq = instance->my_ring_id.seq;
  2462. memb_join->proc_list_entries = instance->my_proc_list_entries;
  2463. memb_join->failed_list_entries = instance->my_failed_list_entries;
  2464. srp_addr_copy (&memb_join->system_from, &instance->my_id);
  2465. /*
  2466. * This mess adds the joined and failed processor lists into the join
  2467. * message
  2468. */
  2469. addr = (char *)memb_join;
  2470. addr_idx = sizeof (struct memb_join);
  2471. memcpy (&addr[addr_idx],
  2472. instance->my_proc_list,
  2473. instance->my_proc_list_entries *
  2474. sizeof (struct srp_addr));
  2475. addr_idx +=
  2476. instance->my_proc_list_entries *
  2477. sizeof (struct srp_addr);
  2478. memcpy (&addr[addr_idx],
  2479. instance->my_failed_list,
  2480. instance->my_failed_list_entries *
  2481. sizeof (struct srp_addr));
  2482. addr_idx +=
  2483. instance->my_failed_list_entries *
  2484. sizeof (struct srp_addr);
  2485. if (instance->totem_config->send_join_timeout) {
  2486. usleep (random() % (instance->totem_config->send_join_timeout * 1000));
  2487. }
  2488. instance->stats.memb_join_tx++;
  2489. totemrrp_mcast_flush_send (
  2490. instance->totemrrp_context,
  2491. memb_join,
  2492. addr_idx);
  2493. }
  2494. static void memb_leave_message_send (struct totemsrp_instance *instance)
  2495. {
  2496. char memb_join_data[40000];
  2497. struct memb_join *memb_join = (struct memb_join *)memb_join_data;
  2498. char *addr;
  2499. unsigned int addr_idx;
  2500. int active_memb_entries;
  2501. struct srp_addr active_memb[PROCESSOR_COUNT_MAX];
  2502. log_printf (instance->totemsrp_log_level_debug,
  2503. "sending join/leave message\n");
  2504. /*
  2505. * add us to the failed list, and remove us from
  2506. * the members list
  2507. */
  2508. memb_set_merge(
  2509. &instance->my_id, 1,
  2510. instance->my_failed_list, &instance->my_failed_list_entries);
  2511. memb_set_subtract (active_memb, &active_memb_entries,
  2512. instance->my_proc_list, instance->my_proc_list_entries,
  2513. &instance->my_id, 1);
  2514. memb_join->header.type = MESSAGE_TYPE_MEMB_JOIN;
  2515. memb_join->header.endian_detector = ENDIAN_LOCAL;
  2516. memb_join->header.encapsulated = 0;
  2517. memb_join->header.nodeid = LEAVE_DUMMY_NODEID;
  2518. memb_join->ring_seq = instance->my_ring_id.seq;
  2519. memb_join->proc_list_entries = active_memb_entries;
  2520. memb_join->failed_list_entries = instance->my_failed_list_entries;
  2521. srp_addr_copy (&memb_join->system_from, &instance->my_id);
  2522. memb_join->system_from.addr[0].nodeid = LEAVE_DUMMY_NODEID;
  2523. // TODO: CC Maybe use the actual join send routine.
  2524. /*
  2525. * This mess adds the joined and failed processor lists into the join
  2526. * message
  2527. */
  2528. addr = (char *)memb_join;
  2529. addr_idx = sizeof (struct memb_join);
  2530. memcpy (&addr[addr_idx],
  2531. active_memb,
  2532. active_memb_entries *
  2533. sizeof (struct srp_addr));
  2534. addr_idx +=
  2535. active_memb_entries *
  2536. sizeof (struct srp_addr);
  2537. memcpy (&addr[addr_idx],
  2538. instance->my_failed_list,
  2539. instance->my_failed_list_entries *
  2540. sizeof (struct srp_addr));
  2541. addr_idx +=
  2542. instance->my_failed_list_entries *
  2543. sizeof (struct srp_addr);
  2544. if (instance->totem_config->send_join_timeout) {
  2545. usleep (random() % (instance->totem_config->send_join_timeout * 1000));
  2546. }
  2547. instance->stats.memb_join_tx++;
  2548. totemrrp_mcast_flush_send (
  2549. instance->totemrrp_context,
  2550. memb_join,
  2551. addr_idx);
  2552. }
  2553. static void memb_merge_detect_transmit (struct totemsrp_instance *instance)
  2554. {
  2555. struct memb_merge_detect memb_merge_detect;
  2556. memb_merge_detect.header.type = MESSAGE_TYPE_MEMB_MERGE_DETECT;
  2557. memb_merge_detect.header.endian_detector = ENDIAN_LOCAL;
  2558. memb_merge_detect.header.encapsulated = 0;
  2559. memb_merge_detect.header.nodeid = instance->my_id.addr[0].nodeid;
  2560. srp_addr_copy (&memb_merge_detect.system_from, &instance->my_id);
  2561. memcpy (&memb_merge_detect.ring_id, &instance->my_ring_id,
  2562. sizeof (struct memb_ring_id));
  2563. assert (memb_merge_detect.header.nodeid);
  2564. instance->stats.memb_merge_detect_tx++;
  2565. totemrrp_mcast_flush_send (instance->totemrrp_context,
  2566. &memb_merge_detect,
  2567. sizeof (struct memb_merge_detect));
  2568. }
  2569. static void memb_ring_id_create_or_load (
  2570. struct totemsrp_instance *instance,
  2571. struct memb_ring_id *memb_ring_id)
  2572. {
  2573. int fd;
  2574. int res = 0;
  2575. char filename[PATH_MAX];
  2576. snprintf (filename, sizeof(filename), "%s/ringid_%s",
  2577. rundir, totemip_print (&instance->my_id.addr[0]));
  2578. fd = open (filename, O_RDONLY, 0700);
  2579. /*
  2580. * If file can be opened and read, read the ring id
  2581. */
  2582. if (fd != -1) {
  2583. res = read (fd, &memb_ring_id->seq, sizeof (uint64_t));
  2584. close (fd);
  2585. }
  2586. /*
  2587. * If file could not be opened or read, create a new ring id
  2588. */
  2589. if ((fd == -1) || (res != sizeof (uint64_t))) {
  2590. memb_ring_id->seq = 0;
  2591. umask(0);
  2592. fd = open (filename, O_CREAT|O_RDWR, 0700);
  2593. if (fd != -1) {
  2594. res = write (fd, &memb_ring_id->seq, sizeof (uint64_t));
  2595. close (fd);
  2596. if (res == -1) {
  2597. LOGSYS_PERROR (errno, instance->totemsrp_log_level_warning,
  2598. "Couldn't write ringid file '%s'", filename);
  2599. }
  2600. } else {
  2601. LOGSYS_PERROR (errno, instance->totemsrp_log_level_warning,
  2602. "Couldn't create ringid file '%s'", filename);
  2603. }
  2604. }
  2605. totemip_copy(&memb_ring_id->rep, &instance->my_id.addr[0]);
  2606. assert (!totemip_zero_check(&memb_ring_id->rep));
  2607. instance->token_ring_id_seq = memb_ring_id->seq;
  2608. }
  2609. static void memb_ring_id_set_and_store (
  2610. struct totemsrp_instance *instance,
  2611. const struct memb_ring_id *ring_id)
  2612. {
  2613. char filename[256];
  2614. int fd;
  2615. int res;
  2616. memcpy (&instance->my_ring_id, ring_id, sizeof (struct memb_ring_id));
  2617. snprintf (filename, sizeof(filename), "%s/ringid_%s",
  2618. rundir, totemip_print (&instance->my_id.addr[0]));
  2619. fd = open (filename, O_WRONLY, 0777);
  2620. if (fd == -1) {
  2621. fd = open (filename, O_CREAT|O_RDWR, 0777);
  2622. }
  2623. if (fd == -1) {
  2624. LOGSYS_PERROR(errno, instance->totemsrp_log_level_warning,
  2625. "Couldn't store new ring id %llx to stable storage",
  2626. instance->my_ring_id.seq);
  2627. assert (0);
  2628. return;
  2629. }
  2630. log_printf (instance->totemsrp_log_level_debug,
  2631. "Storing new sequence id for ring %llx\n", instance->my_ring_id.seq);
  2632. //assert (fd > 0);
  2633. res = write (fd, &instance->my_ring_id.seq, sizeof (unsigned long long));
  2634. assert (res == sizeof (unsigned long long));
  2635. close (fd);
  2636. }
  2637. int totemsrp_callback_token_create (
  2638. void *srp_context,
  2639. void **handle_out,
  2640. enum totem_callback_token_type type,
  2641. int delete,
  2642. int (*callback_fn) (enum totem_callback_token_type type, const void *),
  2643. const void *data)
  2644. {
  2645. struct totemsrp_instance *instance = (struct totemsrp_instance *)srp_context;
  2646. struct token_callback_instance *callback_handle;
  2647. token_hold_cancel_send (instance);
  2648. callback_handle = malloc (sizeof (struct token_callback_instance));
  2649. if (callback_handle == 0) {
  2650. return (-1);
  2651. }
  2652. *handle_out = (void *)callback_handle;
  2653. list_init (&callback_handle->list);
  2654. callback_handle->callback_fn = callback_fn;
  2655. callback_handle->data = (void *) data;
  2656. callback_handle->callback_type = type;
  2657. callback_handle->delete = delete;
  2658. switch (type) {
  2659. case TOTEM_CALLBACK_TOKEN_RECEIVED:
  2660. list_add (&callback_handle->list, &instance->token_callback_received_listhead);
  2661. break;
  2662. case TOTEM_CALLBACK_TOKEN_SENT:
  2663. list_add (&callback_handle->list, &instance->token_callback_sent_listhead);
  2664. break;
  2665. }
  2666. return (0);
  2667. }
  2668. void totemsrp_callback_token_destroy (void *srp_context, void **handle_out)
  2669. {
  2670. struct token_callback_instance *h;
  2671. if (*handle_out) {
  2672. h = (struct token_callback_instance *)*handle_out;
  2673. list_del (&h->list);
  2674. free (h);
  2675. h = NULL;
  2676. *handle_out = 0;
  2677. }
  2678. }
  2679. static void token_callbacks_execute (
  2680. struct totemsrp_instance *instance,
  2681. enum totem_callback_token_type type)
  2682. {
  2683. struct list_head *list;
  2684. struct list_head *list_next;
  2685. struct list_head *callback_listhead = 0;
  2686. struct token_callback_instance *token_callback_instance;
  2687. int res;
  2688. int del;
  2689. switch (type) {
  2690. case TOTEM_CALLBACK_TOKEN_RECEIVED:
  2691. callback_listhead = &instance->token_callback_received_listhead;
  2692. break;
  2693. case TOTEM_CALLBACK_TOKEN_SENT:
  2694. callback_listhead = &instance->token_callback_sent_listhead;
  2695. break;
  2696. default:
  2697. assert (0);
  2698. }
  2699. for (list = callback_listhead->next; list != callback_listhead;
  2700. list = list_next) {
  2701. token_callback_instance = list_entry (list, struct token_callback_instance, list);
  2702. list_next = list->next;
  2703. del = token_callback_instance->delete;
  2704. if (del == 1) {
  2705. list_del (list);
  2706. }
  2707. res = token_callback_instance->callback_fn (
  2708. token_callback_instance->callback_type,
  2709. token_callback_instance->data);
  2710. /*
  2711. * This callback failed to execute, try it again on the next token
  2712. */
  2713. if (res == -1 && del == 1) {
  2714. list_add (list, callback_listhead);
  2715. } else if (del) {
  2716. free (token_callback_instance);
  2717. }
  2718. }
  2719. }
  2720. /*
  2721. * Flow control functions
  2722. */
  2723. static unsigned int backlog_get (struct totemsrp_instance *instance)
  2724. {
  2725. unsigned int backlog = 0;
  2726. if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
  2727. backlog = cs_queue_used (&instance->new_message_queue);
  2728. } else
  2729. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2730. backlog = cs_queue_used (&instance->retrans_message_queue);
  2731. }
  2732. instance->stats.token[instance->stats.latest_token].backlog_calc = backlog;
  2733. return (backlog);
  2734. }
  2735. static int fcc_calculate (
  2736. struct totemsrp_instance *instance,
  2737. struct orf_token *token)
  2738. {
  2739. unsigned int transmits_allowed;
  2740. unsigned int backlog_calc;
  2741. transmits_allowed = instance->totem_config->max_messages;
  2742. if (transmits_allowed > instance->totem_config->window_size - token->fcc) {
  2743. transmits_allowed = instance->totem_config->window_size - token->fcc;
  2744. }
  2745. instance->my_cbl = backlog_get (instance);
  2746. /*
  2747. * Only do backlog calculation if there is a backlog otherwise
  2748. * we would result in div by zero
  2749. */
  2750. if (token->backlog + instance->my_cbl - instance->my_pbl) {
  2751. backlog_calc = (instance->totem_config->window_size * instance->my_pbl) /
  2752. (token->backlog + instance->my_cbl - instance->my_pbl);
  2753. if (backlog_calc > 0 && transmits_allowed > backlog_calc) {
  2754. transmits_allowed = backlog_calc;
  2755. }
  2756. }
  2757. return (transmits_allowed);
  2758. }
  2759. /*
  2760. * don't overflow the RTR sort queue
  2761. */
  2762. static void fcc_rtr_limit (
  2763. struct totemsrp_instance *instance,
  2764. struct orf_token *token,
  2765. unsigned int *transmits_allowed)
  2766. {
  2767. int check = QUEUE_RTR_ITEMS_SIZE_MAX;
  2768. check -= (*transmits_allowed + instance->totem_config->window_size);
  2769. assert (check >= 0);
  2770. if (sq_lt_compare (instance->last_released +
  2771. QUEUE_RTR_ITEMS_SIZE_MAX - *transmits_allowed -
  2772. instance->totem_config->window_size,
  2773. token->seq)) {
  2774. *transmits_allowed = 0;
  2775. }
  2776. }
  2777. static void fcc_token_update (
  2778. struct totemsrp_instance *instance,
  2779. struct orf_token *token,
  2780. unsigned int msgs_transmitted)
  2781. {
  2782. token->fcc += msgs_transmitted - instance->my_trc;
  2783. token->backlog += instance->my_cbl - instance->my_pbl;
  2784. instance->my_trc = msgs_transmitted;
  2785. instance->my_pbl = instance->my_cbl;
  2786. }
  2787. /*
  2788. * Message Handlers
  2789. */
  2790. unsigned long long int tv_old;
  2791. /*
  2792. * message handler called when TOKEN message type received
  2793. */
  2794. static int message_handler_orf_token (
  2795. struct totemsrp_instance *instance,
  2796. const void *msg,
  2797. size_t msg_len,
  2798. int endian_conversion_needed)
  2799. {
  2800. char token_storage[1500];
  2801. char token_convert[1500];
  2802. struct orf_token *token = NULL;
  2803. int forward_token;
  2804. unsigned int transmits_allowed;
  2805. unsigned int mcasted_retransmit;
  2806. unsigned int mcasted_regular;
  2807. unsigned int last_aru;
  2808. #ifdef GIVEINFO
  2809. unsigned long long tv_current;
  2810. unsigned long long tv_diff;
  2811. tv_current = qb_util_nano_current_get ();
  2812. tv_diff = tv_current - tv_old;
  2813. tv_old = tv_current;
  2814. log_printf (instance->totemsrp_log_level_debug,
  2815. "Time since last token %0.4f ms\n", ((float)tv_diff) / 1000000.0);
  2816. #endif
  2817. if (instance->orf_token_discard) {
  2818. return (0);
  2819. }
  2820. #ifdef TEST_DROP_ORF_TOKEN_PERCENTAGE
  2821. if (random()%100 < TEST_DROP_ORF_TOKEN_PERCENTAGE) {
  2822. return (0);
  2823. }
  2824. #endif
  2825. if (endian_conversion_needed) {
  2826. orf_token_endian_convert ((struct orf_token *)msg,
  2827. (struct orf_token *)token_convert);
  2828. msg = (struct orf_token *)token_convert;
  2829. }
  2830. /*
  2831. * Make copy of token and retransmit list in case we have
  2832. * to flush incoming messages from the kernel queue
  2833. */
  2834. token = (struct orf_token *)token_storage;
  2835. memcpy (token, msg, sizeof (struct orf_token));
  2836. memcpy (&token->rtr_list[0], (char *)msg + sizeof (struct orf_token),
  2837. sizeof (struct rtr_item) * RETRANSMIT_ENTRIES_MAX);
  2838. /*
  2839. * Handle merge detection timeout
  2840. */
  2841. if (token->seq == instance->my_last_seq) {
  2842. start_merge_detect_timeout (instance);
  2843. instance->my_seq_unchanged += 1;
  2844. } else {
  2845. cancel_merge_detect_timeout (instance);
  2846. cancel_token_hold_retransmit_timeout (instance);
  2847. instance->my_seq_unchanged = 0;
  2848. }
  2849. instance->my_last_seq = token->seq;
  2850. #ifdef TEST_RECOVERY_MSG_COUNT
  2851. if (instance->memb_state == MEMB_STATE_OPERATIONAL && token->seq > TEST_RECOVERY_MSG_COUNT) {
  2852. return (0);
  2853. }
  2854. #endif
  2855. totemrrp_recv_flush (instance->totemrrp_context);
  2856. /*
  2857. * Determine if we should hold (in reality drop) the token
  2858. */
  2859. instance->my_token_held = 0;
  2860. if (totemip_equal(&instance->my_ring_id.rep, &instance->my_id.addr[0]) &&
  2861. instance->my_seq_unchanged > instance->totem_config->seqno_unchanged_const) {
  2862. instance->my_token_held = 1;
  2863. } else
  2864. if (!totemip_equal(&instance->my_ring_id.rep, &instance->my_id.addr[0]) &&
  2865. instance->my_seq_unchanged >= instance->totem_config->seqno_unchanged_const) {
  2866. instance->my_token_held = 1;
  2867. }
  2868. /*
  2869. * Hold onto token when there is no activity on ring and
  2870. * this processor is the ring rep
  2871. */
  2872. forward_token = 1;
  2873. if (totemip_equal(&instance->my_ring_id.rep, &instance->my_id.addr[0])) {
  2874. if (instance->my_token_held) {
  2875. forward_token = 0;
  2876. }
  2877. }
  2878. token_callbacks_execute (instance, TOTEM_CALLBACK_TOKEN_RECEIVED);
  2879. switch (instance->memb_state) {
  2880. case MEMB_STATE_COMMIT:
  2881. /* Discard token */
  2882. break;
  2883. case MEMB_STATE_OPERATIONAL:
  2884. messages_free (instance, token->aru);
  2885. /*
  2886. * Do NOT add break, this case should also execute code in gather case.
  2887. */
  2888. case MEMB_STATE_GATHER:
  2889. /*
  2890. * DO NOT add break, we use different free mechanism in recovery state
  2891. */
  2892. case MEMB_STATE_RECOVERY:
  2893. /*
  2894. * Discard tokens from another configuration
  2895. */
  2896. if (memcmp (&token->ring_id, &instance->my_ring_id,
  2897. sizeof (struct memb_ring_id)) != 0) {
  2898. if ((forward_token)
  2899. && instance->use_heartbeat) {
  2900. reset_heartbeat_timeout(instance);
  2901. }
  2902. else {
  2903. cancel_heartbeat_timeout(instance);
  2904. }
  2905. return (0); /* discard token */
  2906. }
  2907. /*
  2908. * Discard retransmitted tokens
  2909. */
  2910. if (sq_lte_compare (token->token_seq, instance->my_token_seq)) {
  2911. return (0); /* discard token */
  2912. }
  2913. last_aru = instance->my_last_aru;
  2914. instance->my_last_aru = token->aru;
  2915. transmits_allowed = fcc_calculate (instance, token);
  2916. mcasted_retransmit = orf_token_rtr (instance, token, &transmits_allowed);
  2917. fcc_rtr_limit (instance, token, &transmits_allowed);
  2918. mcasted_regular = orf_token_mcast (instance, token, transmits_allowed);
  2919. /*
  2920. if (mcasted_regular) {
  2921. printf ("mcasted regular %d\n", mcasted_regular);
  2922. printf ("token seq %d\n", token->seq);
  2923. }
  2924. */
  2925. fcc_token_update (instance, token, mcasted_retransmit +
  2926. mcasted_regular);
  2927. if (sq_lt_compare (instance->my_aru, token->aru) ||
  2928. instance->my_id.addr[0].nodeid == token->aru_addr ||
  2929. token->aru_addr == 0) {
  2930. token->aru = instance->my_aru;
  2931. if (token->aru == token->seq) {
  2932. token->aru_addr = 0;
  2933. } else {
  2934. token->aru_addr = instance->my_id.addr[0].nodeid;
  2935. }
  2936. }
  2937. if (token->aru == last_aru && token->aru_addr != 0) {
  2938. instance->my_aru_count += 1;
  2939. } else {
  2940. instance->my_aru_count = 0;
  2941. }
  2942. if (instance->my_aru_count > instance->totem_config->fail_to_recv_const &&
  2943. token->aru_addr == instance->my_id.addr[0].nodeid) {
  2944. log_printf (instance->totemsrp_log_level_error,
  2945. "FAILED TO RECEIVE\n");
  2946. instance->failed_to_recv = 1;
  2947. memb_set_merge (&instance->my_id, 1,
  2948. instance->my_failed_list,
  2949. &instance->my_failed_list_entries);
  2950. memb_state_gather_enter (instance, 6);
  2951. } else {
  2952. instance->my_token_seq = token->token_seq;
  2953. token->token_seq += 1;
  2954. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2955. /*
  2956. * instance->my_aru == instance->my_high_seq_received means this processor
  2957. * has recovered all messages it can recover
  2958. * (ie: its retrans queue is empty)
  2959. */
  2960. if (cs_queue_is_empty (&instance->retrans_message_queue) == 0) {
  2961. if (token->retrans_flg == 0) {
  2962. token->retrans_flg = 1;
  2963. instance->my_set_retrans_flg = 1;
  2964. }
  2965. } else
  2966. if (token->retrans_flg == 1 && instance->my_set_retrans_flg) {
  2967. token->retrans_flg = 0;
  2968. instance->my_set_retrans_flg = 0;
  2969. }
  2970. log_printf (instance->totemsrp_log_level_debug,
  2971. "token retrans flag is %d my set retrans flag%d retrans queue empty %d count %d, aru %x\n",
  2972. token->retrans_flg, instance->my_set_retrans_flg,
  2973. cs_queue_is_empty (&instance->retrans_message_queue),
  2974. instance->my_retrans_flg_count, token->aru);
  2975. if (token->retrans_flg == 0) {
  2976. instance->my_retrans_flg_count += 1;
  2977. } else {
  2978. instance->my_retrans_flg_count = 0;
  2979. }
  2980. if (instance->my_retrans_flg_count == 2) {
  2981. instance->my_install_seq = token->seq;
  2982. }
  2983. log_printf (instance->totemsrp_log_level_debug,
  2984. "install seq %x aru %x high seq received %x\n",
  2985. instance->my_install_seq, instance->my_aru, instance->my_high_seq_received);
  2986. if (instance->my_retrans_flg_count >= 2 &&
  2987. instance->my_received_flg == 0 &&
  2988. sq_lte_compare (instance->my_install_seq, instance->my_aru)) {
  2989. instance->my_received_flg = 1;
  2990. instance->my_deliver_memb_entries = instance->my_trans_memb_entries;
  2991. memcpy (instance->my_deliver_memb_list, instance->my_trans_memb_list,
  2992. sizeof (struct totem_ip_address) * instance->my_trans_memb_entries);
  2993. }
  2994. if (instance->my_retrans_flg_count >= 3 &&
  2995. sq_lte_compare (instance->my_install_seq, token->aru)) {
  2996. instance->my_rotation_counter += 1;
  2997. } else {
  2998. instance->my_rotation_counter = 0;
  2999. }
  3000. if (instance->my_rotation_counter == 2) {
  3001. log_printf (instance->totemsrp_log_level_debug,
  3002. "retrans flag count %x token aru %x install seq %x aru %x %x\n",
  3003. instance->my_retrans_flg_count, token->aru, instance->my_install_seq,
  3004. instance->my_aru, token->seq);
  3005. memb_state_operational_enter (instance);
  3006. instance->my_rotation_counter = 0;
  3007. instance->my_retrans_flg_count = 0;
  3008. }
  3009. }
  3010. totemrrp_send_flush (instance->totemrrp_context);
  3011. token_send (instance, token, forward_token);
  3012. #ifdef GIVEINFO
  3013. tv_current = qb_util_nano_current_get ();
  3014. tv_diff = tv_current - tv_old;
  3015. tv_old = tv_current;
  3016. log_printf (instance->totemsrp_log_level_debug,
  3017. "I held %0.4f ms\n",
  3018. ((float)tv_diff) / 1000000.0);
  3019. #endif
  3020. if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
  3021. messages_deliver_to_app (instance, 0,
  3022. instance->my_high_seq_received);
  3023. }
  3024. /*
  3025. * Deliver messages after token has been transmitted
  3026. * to improve performance
  3027. */
  3028. reset_token_timeout (instance); // REVIEWED
  3029. reset_token_retransmit_timeout (instance); // REVIEWED
  3030. if (totemip_equal(&instance->my_id.addr[0], &instance->my_ring_id.rep) &&
  3031. instance->my_token_held == 1) {
  3032. start_token_hold_retransmit_timeout (instance);
  3033. }
  3034. token_callbacks_execute (instance, TOTEM_CALLBACK_TOKEN_SENT);
  3035. }
  3036. break;
  3037. }
  3038. if ((forward_token)
  3039. && instance->use_heartbeat) {
  3040. reset_heartbeat_timeout(instance);
  3041. }
  3042. else {
  3043. cancel_heartbeat_timeout(instance);
  3044. }
  3045. return (0);
  3046. }
  3047. static void messages_deliver_to_app (
  3048. struct totemsrp_instance *instance,
  3049. int skip,
  3050. unsigned int end_point)
  3051. {
  3052. struct sort_queue_item *sort_queue_item_p;
  3053. unsigned int i;
  3054. int res;
  3055. struct mcast *mcast_in;
  3056. struct mcast mcast_header;
  3057. unsigned int range = 0;
  3058. int endian_conversion_required;
  3059. unsigned int my_high_delivered_stored = 0;
  3060. range = end_point - instance->my_high_delivered;
  3061. if (range) {
  3062. log_printf (instance->totemsrp_log_level_debug,
  3063. "Delivering %x to %x\n", instance->my_high_delivered,
  3064. end_point);
  3065. }
  3066. assert (range < QUEUE_RTR_ITEMS_SIZE_MAX);
  3067. my_high_delivered_stored = instance->my_high_delivered;
  3068. /*
  3069. * Deliver messages in order from rtr queue to pending delivery queue
  3070. */
  3071. for (i = 1; i <= range; i++) {
  3072. void *ptr = 0;
  3073. /*
  3074. * If out of range of sort queue, stop assembly
  3075. */
  3076. res = sq_in_range (&instance->regular_sort_queue,
  3077. my_high_delivered_stored + i);
  3078. if (res == 0) {
  3079. break;
  3080. }
  3081. res = sq_item_get (&instance->regular_sort_queue,
  3082. my_high_delivered_stored + i, &ptr);
  3083. /*
  3084. * If hole, stop assembly
  3085. */
  3086. if (res != 0 && skip == 0) {
  3087. break;
  3088. }
  3089. instance->my_high_delivered = my_high_delivered_stored + i;
  3090. if (res != 0) {
  3091. continue;
  3092. }
  3093. sort_queue_item_p = ptr;
  3094. mcast_in = sort_queue_item_p->mcast;
  3095. assert (mcast_in != (struct mcast *)0xdeadbeef);
  3096. endian_conversion_required = 0;
  3097. if (mcast_in->header.endian_detector != ENDIAN_LOCAL) {
  3098. endian_conversion_required = 1;
  3099. mcast_endian_convert (mcast_in, &mcast_header);
  3100. } else {
  3101. memcpy (&mcast_header, mcast_in, sizeof (struct mcast));
  3102. }
  3103. /*
  3104. * Skip messages not originated in instance->my_deliver_memb
  3105. */
  3106. if (skip &&
  3107. memb_set_subset (&mcast_header.system_from,
  3108. 1,
  3109. instance->my_deliver_memb_list,
  3110. instance->my_deliver_memb_entries) == 0) {
  3111. instance->my_high_delivered = my_high_delivered_stored + i;
  3112. continue;
  3113. }
  3114. /*
  3115. * Message found
  3116. */
  3117. log_printf (instance->totemsrp_log_level_debug,
  3118. "Delivering MCAST message with seq %x to pending delivery queue\n",
  3119. mcast_header.seq);
  3120. /*
  3121. * Message is locally originated multicast
  3122. */
  3123. instance->totemsrp_deliver_fn (
  3124. mcast_header.header.nodeid,
  3125. ((char *)sort_queue_item_p->mcast) + sizeof (struct mcast),
  3126. sort_queue_item_p->msg_len - sizeof (struct mcast),
  3127. endian_conversion_required);
  3128. }
  3129. }
  3130. /*
  3131. * recv message handler called when MCAST message type received
  3132. */
  3133. static int message_handler_mcast (
  3134. struct totemsrp_instance *instance,
  3135. const void *msg,
  3136. size_t msg_len,
  3137. int endian_conversion_needed)
  3138. {
  3139. struct sort_queue_item sort_queue_item;
  3140. struct sq *sort_queue;
  3141. struct mcast mcast_header;
  3142. if (endian_conversion_needed) {
  3143. mcast_endian_convert (msg, &mcast_header);
  3144. } else {
  3145. memcpy (&mcast_header, msg, sizeof (struct mcast));
  3146. }
  3147. if (mcast_header.header.encapsulated == MESSAGE_ENCAPSULATED) {
  3148. sort_queue = &instance->recovery_sort_queue;
  3149. } else {
  3150. sort_queue = &instance->regular_sort_queue;
  3151. }
  3152. assert (msg_len <= FRAME_SIZE_MAX);
  3153. #ifdef TEST_DROP_MCAST_PERCENTAGE
  3154. if (random()%100 < TEST_DROP_MCAST_PERCENTAGE) {
  3155. return (0);
  3156. }
  3157. #endif
  3158. /*
  3159. * If the message is foreign execute the switch below
  3160. */
  3161. if (memcmp (&instance->my_ring_id, &mcast_header.ring_id,
  3162. sizeof (struct memb_ring_id)) != 0) {
  3163. switch (instance->memb_state) {
  3164. case MEMB_STATE_OPERATIONAL:
  3165. memb_set_merge (
  3166. &mcast_header.system_from, 1,
  3167. instance->my_proc_list, &instance->my_proc_list_entries);
  3168. memb_state_gather_enter (instance, 7);
  3169. break;
  3170. case MEMB_STATE_GATHER:
  3171. if (!memb_set_subset (
  3172. &mcast_header.system_from,
  3173. 1,
  3174. instance->my_proc_list,
  3175. instance->my_proc_list_entries)) {
  3176. memb_set_merge (&mcast_header.system_from, 1,
  3177. instance->my_proc_list, &instance->my_proc_list_entries);
  3178. memb_state_gather_enter (instance, 8);
  3179. return (0);
  3180. }
  3181. break;
  3182. case MEMB_STATE_COMMIT:
  3183. /* discard message */
  3184. instance->stats.rx_msg_dropped++;
  3185. break;
  3186. case MEMB_STATE_RECOVERY:
  3187. /* discard message */
  3188. instance->stats.rx_msg_dropped++;
  3189. break;
  3190. }
  3191. return (0);
  3192. }
  3193. log_printf (instance->totemsrp_log_level_debug,
  3194. "Received ringid(%s:%lld) seq %x\n",
  3195. totemip_print (&mcast_header.ring_id.rep),
  3196. mcast_header.ring_id.seq,
  3197. mcast_header.seq);
  3198. /*
  3199. * Add mcast message to rtr queue if not already in rtr queue
  3200. * otherwise free io vectors
  3201. */
  3202. if (msg_len > 0 && msg_len <= FRAME_SIZE_MAX &&
  3203. sq_in_range (sort_queue, mcast_header.seq) &&
  3204. sq_item_inuse (sort_queue, mcast_header.seq) == 0) {
  3205. /*
  3206. * Allocate new multicast memory block
  3207. */
  3208. // TODO LEAK
  3209. sort_queue_item.mcast = totemsrp_buffer_alloc (instance);
  3210. if (sort_queue_item.mcast == NULL) {
  3211. return (-1); /* error here is corrected by the algorithm */
  3212. }
  3213. memcpy (sort_queue_item.mcast, msg, msg_len);
  3214. sort_queue_item.msg_len = msg_len;
  3215. if (sq_lt_compare (instance->my_high_seq_received,
  3216. mcast_header.seq)) {
  3217. instance->my_high_seq_received = mcast_header.seq;
  3218. }
  3219. sq_item_add (sort_queue, &sort_queue_item, mcast_header.seq);
  3220. }
  3221. update_aru (instance);
  3222. if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
  3223. messages_deliver_to_app (instance, 0, instance->my_high_seq_received);
  3224. }
  3225. /* TODO remove from retrans message queue for old ring in recovery state */
  3226. return (0);
  3227. }
  3228. static int message_handler_memb_merge_detect (
  3229. struct totemsrp_instance *instance,
  3230. const void *msg,
  3231. size_t msg_len,
  3232. int endian_conversion_needed)
  3233. {
  3234. struct memb_merge_detect memb_merge_detect;
  3235. if (endian_conversion_needed) {
  3236. memb_merge_detect_endian_convert (msg, &memb_merge_detect);
  3237. } else {
  3238. memcpy (&memb_merge_detect, msg,
  3239. sizeof (struct memb_merge_detect));
  3240. }
  3241. /*
  3242. * do nothing if this is a merge detect from this configuration
  3243. */
  3244. if (memcmp (&instance->my_ring_id, &memb_merge_detect.ring_id,
  3245. sizeof (struct memb_ring_id)) == 0) {
  3246. return (0);
  3247. }
  3248. /*
  3249. * Execute merge operation
  3250. */
  3251. switch (instance->memb_state) {
  3252. case MEMB_STATE_OPERATIONAL:
  3253. memb_set_merge (&memb_merge_detect.system_from, 1,
  3254. instance->my_proc_list, &instance->my_proc_list_entries);
  3255. memb_state_gather_enter (instance, 9);
  3256. break;
  3257. case MEMB_STATE_GATHER:
  3258. if (!memb_set_subset (
  3259. &memb_merge_detect.system_from,
  3260. 1,
  3261. instance->my_proc_list,
  3262. instance->my_proc_list_entries)) {
  3263. memb_set_merge (&memb_merge_detect.system_from, 1,
  3264. instance->my_proc_list, &instance->my_proc_list_entries);
  3265. memb_state_gather_enter (instance, 10);
  3266. return (0);
  3267. }
  3268. break;
  3269. case MEMB_STATE_COMMIT:
  3270. /* do nothing in commit */
  3271. break;
  3272. case MEMB_STATE_RECOVERY:
  3273. /* do nothing in recovery */
  3274. break;
  3275. }
  3276. return (0);
  3277. }
  3278. static void memb_join_process (
  3279. struct totemsrp_instance *instance,
  3280. const struct memb_join *memb_join)
  3281. {
  3282. struct srp_addr *proc_list;
  3283. struct srp_addr *failed_list;
  3284. int gather_entered = 0;
  3285. int fail_minus_memb_entries = 0;
  3286. struct srp_addr fail_minus_memb[PROCESSOR_COUNT_MAX];
  3287. proc_list = (struct srp_addr *)memb_join->end_of_memb_join;
  3288. failed_list = proc_list + memb_join->proc_list_entries;
  3289. /*
  3290. memb_set_print ("proclist", proc_list, memb_join->proc_list_entries);
  3291. memb_set_print ("faillist", failed_list, memb_join->failed_list_entries);
  3292. memb_set_print ("my_proclist", instance->my_proc_list, instance->my_proc_list_entries);
  3293. memb_set_print ("my_faillist", instance->my_failed_list, instance->my_failed_list_entries);
  3294. -*/
  3295. if (memb_set_equal (proc_list,
  3296. memb_join->proc_list_entries,
  3297. instance->my_proc_list,
  3298. instance->my_proc_list_entries) &&
  3299. memb_set_equal (failed_list,
  3300. memb_join->failed_list_entries,
  3301. instance->my_failed_list,
  3302. instance->my_failed_list_entries)) {
  3303. memb_consensus_set (instance, &memb_join->system_from);
  3304. if (memb_consensus_agreed (instance) && instance->failed_to_recv == 1) {
  3305. instance->failed_to_recv = 0;
  3306. srp_addr_copy (&instance->my_proc_list[0],
  3307. &instance->my_id);
  3308. instance->my_proc_list_entries = 1;
  3309. instance->my_failed_list_entries = 0;
  3310. memb_state_commit_token_create (instance);
  3311. memb_state_commit_enter (instance);
  3312. return;
  3313. }
  3314. if (memb_consensus_agreed (instance) &&
  3315. memb_lowest_in_config (instance)) {
  3316. memb_state_commit_token_create (instance);
  3317. memb_state_commit_enter (instance);
  3318. } else {
  3319. return;
  3320. }
  3321. } else
  3322. if (memb_set_subset (proc_list,
  3323. memb_join->proc_list_entries,
  3324. instance->my_proc_list,
  3325. instance->my_proc_list_entries) &&
  3326. memb_set_subset (failed_list,
  3327. memb_join->failed_list_entries,
  3328. instance->my_failed_list,
  3329. instance->my_failed_list_entries)) {
  3330. return;
  3331. } else
  3332. if (memb_set_subset (&memb_join->system_from, 1,
  3333. instance->my_failed_list, instance->my_failed_list_entries)) {
  3334. return;
  3335. } else {
  3336. memb_set_merge (proc_list,
  3337. memb_join->proc_list_entries,
  3338. instance->my_proc_list, &instance->my_proc_list_entries);
  3339. if (memb_set_subset (
  3340. &instance->my_id, 1,
  3341. failed_list, memb_join->failed_list_entries)) {
  3342. memb_set_merge (
  3343. &memb_join->system_from, 1,
  3344. instance->my_failed_list, &instance->my_failed_list_entries);
  3345. } else {
  3346. if (memb_set_subset (
  3347. &memb_join->system_from, 1,
  3348. instance->my_memb_list,
  3349. instance->my_memb_entries)) {
  3350. if (memb_set_subset (
  3351. &memb_join->system_from, 1,
  3352. instance->my_failed_list,
  3353. instance->my_failed_list_entries) == 0) {
  3354. memb_set_merge (failed_list,
  3355. memb_join->failed_list_entries,
  3356. instance->my_failed_list, &instance->my_failed_list_entries);
  3357. } else {
  3358. memb_set_subtract (fail_minus_memb,
  3359. &fail_minus_memb_entries,
  3360. failed_list,
  3361. memb_join->failed_list_entries,
  3362. instance->my_memb_list,
  3363. instance->my_memb_entries);
  3364. memb_set_merge (fail_minus_memb,
  3365. fail_minus_memb_entries,
  3366. instance->my_failed_list,
  3367. &instance->my_failed_list_entries);
  3368. }
  3369. }
  3370. }
  3371. memb_state_gather_enter (instance, 11);
  3372. gather_entered = 1;
  3373. }
  3374. if (gather_entered == 0 &&
  3375. instance->memb_state == MEMB_STATE_OPERATIONAL) {
  3376. memb_state_gather_enter (instance, 12);
  3377. }
  3378. }
  3379. static void memb_join_endian_convert (const struct memb_join *in, struct memb_join *out)
  3380. {
  3381. int i;
  3382. struct srp_addr *in_proc_list;
  3383. struct srp_addr *in_failed_list;
  3384. struct srp_addr *out_proc_list;
  3385. struct srp_addr *out_failed_list;
  3386. out->header.type = in->header.type;
  3387. out->header.endian_detector = ENDIAN_LOCAL;
  3388. out->header.nodeid = swab32 (in->header.nodeid);
  3389. srp_addr_copy_endian_convert (&out->system_from, &in->system_from);
  3390. out->proc_list_entries = swab32 (in->proc_list_entries);
  3391. out->failed_list_entries = swab32 (in->failed_list_entries);
  3392. out->ring_seq = swab64 (in->ring_seq);
  3393. in_proc_list = (struct srp_addr *)in->end_of_memb_join;
  3394. in_failed_list = in_proc_list + out->proc_list_entries;
  3395. out_proc_list = (struct srp_addr *)out->end_of_memb_join;
  3396. out_failed_list = out_proc_list + out->proc_list_entries;
  3397. for (i = 0; i < out->proc_list_entries; i++) {
  3398. srp_addr_copy_endian_convert (&out_proc_list[i], &in_proc_list[i]);
  3399. }
  3400. for (i = 0; i < out->failed_list_entries; i++) {
  3401. srp_addr_copy_endian_convert (&out_failed_list[i], &in_failed_list[i]);
  3402. }
  3403. }
  3404. static void memb_commit_token_endian_convert (const struct memb_commit_token *in, struct memb_commit_token *out)
  3405. {
  3406. int i;
  3407. struct srp_addr *in_addr = (struct srp_addr *)in->end_of_commit_token;
  3408. struct srp_addr *out_addr = (struct srp_addr *)out->end_of_commit_token;
  3409. struct memb_commit_token_memb_entry *in_memb_list;
  3410. struct memb_commit_token_memb_entry *out_memb_list;
  3411. out->header.type = in->header.type;
  3412. out->header.endian_detector = ENDIAN_LOCAL;
  3413. out->header.nodeid = swab32 (in->header.nodeid);
  3414. out->token_seq = swab32 (in->token_seq);
  3415. totemip_copy_endian_convert(&out->ring_id.rep, &in->ring_id.rep);
  3416. out->ring_id.seq = swab64 (in->ring_id.seq);
  3417. out->retrans_flg = swab32 (in->retrans_flg);
  3418. out->memb_index = swab32 (in->memb_index);
  3419. out->addr_entries = swab32 (in->addr_entries);
  3420. in_memb_list = (struct memb_commit_token_memb_entry *)(in_addr + out->addr_entries);
  3421. out_memb_list = (struct memb_commit_token_memb_entry *)(out_addr + out->addr_entries);
  3422. for (i = 0; i < out->addr_entries; i++) {
  3423. srp_addr_copy_endian_convert (&out_addr[i], &in_addr[i]);
  3424. /*
  3425. * Only convert the memb entry if it has been set
  3426. */
  3427. if (in_memb_list[i].ring_id.rep.family != 0) {
  3428. totemip_copy_endian_convert (&out_memb_list[i].ring_id.rep,
  3429. &in_memb_list[i].ring_id.rep);
  3430. out_memb_list[i].ring_id.seq =
  3431. swab64 (in_memb_list[i].ring_id.seq);
  3432. out_memb_list[i].aru = swab32 (in_memb_list[i].aru);
  3433. out_memb_list[i].high_delivered = swab32 (in_memb_list[i].high_delivered);
  3434. out_memb_list[i].received_flg = swab32 (in_memb_list[i].received_flg);
  3435. }
  3436. }
  3437. }
  3438. static void orf_token_endian_convert (const struct orf_token *in, struct orf_token *out)
  3439. {
  3440. int i;
  3441. out->header.type = in->header.type;
  3442. out->header.endian_detector = ENDIAN_LOCAL;
  3443. out->header.nodeid = swab32 (in->header.nodeid);
  3444. out->seq = swab32 (in->seq);
  3445. out->token_seq = swab32 (in->token_seq);
  3446. out->aru = swab32 (in->aru);
  3447. totemip_copy_endian_convert(&out->ring_id.rep, &in->ring_id.rep);
  3448. out->aru_addr = swab32(in->aru_addr);
  3449. out->ring_id.seq = swab64 (in->ring_id.seq);
  3450. out->fcc = swab32 (in->fcc);
  3451. out->backlog = swab32 (in->backlog);
  3452. out->retrans_flg = swab32 (in->retrans_flg);
  3453. out->rtr_list_entries = swab32 (in->rtr_list_entries);
  3454. for (i = 0; i < out->rtr_list_entries; i++) {
  3455. totemip_copy_endian_convert(&out->rtr_list[i].ring_id.rep, &in->rtr_list[i].ring_id.rep);
  3456. out->rtr_list[i].ring_id.seq = swab64 (in->rtr_list[i].ring_id.seq);
  3457. out->rtr_list[i].seq = swab32 (in->rtr_list[i].seq);
  3458. }
  3459. }
  3460. static void mcast_endian_convert (const struct mcast *in, struct mcast *out)
  3461. {
  3462. out->header.type = in->header.type;
  3463. out->header.endian_detector = ENDIAN_LOCAL;
  3464. out->header.nodeid = swab32 (in->header.nodeid);
  3465. out->header.encapsulated = in->header.encapsulated;
  3466. out->seq = swab32 (in->seq);
  3467. out->this_seqno = swab32 (in->this_seqno);
  3468. totemip_copy_endian_convert(&out->ring_id.rep, &in->ring_id.rep);
  3469. out->ring_id.seq = swab64 (in->ring_id.seq);
  3470. out->node_id = swab32 (in->node_id);
  3471. out->guarantee = swab32 (in->guarantee);
  3472. srp_addr_copy_endian_convert (&out->system_from, &in->system_from);
  3473. }
  3474. static void memb_merge_detect_endian_convert (
  3475. const struct memb_merge_detect *in,
  3476. struct memb_merge_detect *out)
  3477. {
  3478. out->header.type = in->header.type;
  3479. out->header.endian_detector = ENDIAN_LOCAL;
  3480. out->header.nodeid = swab32 (in->header.nodeid);
  3481. totemip_copy_endian_convert(&out->ring_id.rep, &in->ring_id.rep);
  3482. out->ring_id.seq = swab64 (in->ring_id.seq);
  3483. srp_addr_copy_endian_convert (&out->system_from, &in->system_from);
  3484. }
  3485. static int message_handler_memb_join (
  3486. struct totemsrp_instance *instance,
  3487. const void *msg,
  3488. size_t msg_len,
  3489. int endian_conversion_needed)
  3490. {
  3491. const struct memb_join *memb_join;
  3492. struct memb_join *memb_join_convert = alloca (msg_len);
  3493. if (endian_conversion_needed) {
  3494. memb_join = memb_join_convert;
  3495. memb_join_endian_convert (msg, memb_join_convert);
  3496. } else {
  3497. memb_join = msg;
  3498. }
  3499. /*
  3500. * If the process paused because it wasn't scheduled in a timely
  3501. * fashion, flush the join messages because they may be queued
  3502. * entries
  3503. */
  3504. if (pause_flush (instance)) {
  3505. return (0);
  3506. }
  3507. if (instance->token_ring_id_seq < memb_join->ring_seq) {
  3508. instance->token_ring_id_seq = memb_join->ring_seq;
  3509. }
  3510. switch (instance->memb_state) {
  3511. case MEMB_STATE_OPERATIONAL:
  3512. memb_join_process (instance, memb_join);
  3513. break;
  3514. case MEMB_STATE_GATHER:
  3515. memb_join_process (instance, memb_join);
  3516. break;
  3517. case MEMB_STATE_COMMIT:
  3518. if (memb_set_subset (&memb_join->system_from,
  3519. 1,
  3520. instance->my_new_memb_list,
  3521. instance->my_new_memb_entries) &&
  3522. memb_join->ring_seq >= instance->my_ring_id.seq) {
  3523. memb_join_process (instance, memb_join);
  3524. memb_state_gather_enter (instance, 13);
  3525. }
  3526. break;
  3527. case MEMB_STATE_RECOVERY:
  3528. if (memb_set_subset (&memb_join->system_from,
  3529. 1,
  3530. instance->my_new_memb_list,
  3531. instance->my_new_memb_entries) &&
  3532. memb_join->ring_seq >= instance->my_ring_id.seq) {
  3533. memb_join_process (instance, memb_join);
  3534. memb_recovery_state_token_loss (instance);
  3535. memb_state_gather_enter (instance, 14);
  3536. }
  3537. break;
  3538. }
  3539. return (0);
  3540. }
  3541. static int message_handler_memb_commit_token (
  3542. struct totemsrp_instance *instance,
  3543. const void *msg,
  3544. size_t msg_len,
  3545. int endian_conversion_needed)
  3546. {
  3547. struct memb_commit_token *memb_commit_token_convert = alloca (msg_len);
  3548. struct memb_commit_token *memb_commit_token;
  3549. struct srp_addr sub[PROCESSOR_COUNT_MAX];
  3550. int sub_entries;
  3551. struct srp_addr *addr;
  3552. log_printf (instance->totemsrp_log_level_debug,
  3553. "got commit token\n");
  3554. if (endian_conversion_needed) {
  3555. memb_commit_token_endian_convert (msg, memb_commit_token_convert);
  3556. } else {
  3557. memcpy (memb_commit_token_convert, msg, msg_len);
  3558. }
  3559. memb_commit_token = memb_commit_token_convert;
  3560. addr = (struct srp_addr *)memb_commit_token->end_of_commit_token;
  3561. #ifdef TEST_DROP_COMMIT_TOKEN_PERCENTAGE
  3562. if (random()%100 < TEST_DROP_COMMIT_TOKEN_PERCENTAGE) {
  3563. return (0);
  3564. }
  3565. #endif
  3566. switch (instance->memb_state) {
  3567. case MEMB_STATE_OPERATIONAL:
  3568. /* discard token */
  3569. break;
  3570. case MEMB_STATE_GATHER:
  3571. memb_set_subtract (sub, &sub_entries,
  3572. instance->my_proc_list, instance->my_proc_list_entries,
  3573. instance->my_failed_list, instance->my_failed_list_entries);
  3574. if (memb_set_equal (addr,
  3575. memb_commit_token->addr_entries,
  3576. sub,
  3577. sub_entries) &&
  3578. memb_commit_token->ring_id.seq > instance->my_ring_id.seq) {
  3579. memcpy (instance->commit_token, memb_commit_token, msg_len);
  3580. memb_state_commit_enter (instance);
  3581. }
  3582. break;
  3583. case MEMB_STATE_COMMIT:
  3584. /*
  3585. * If retransmitted commit tokens are sent on this ring
  3586. * filter them out and only enter recovery once the
  3587. * commit token has traversed the array. This is
  3588. * determined by :
  3589. * memb_commit_token->memb_index == memb_commit_token->addr_entries) {
  3590. */
  3591. if (memb_commit_token->ring_id.seq == instance->my_ring_id.seq &&
  3592. memb_commit_token->memb_index == memb_commit_token->addr_entries) {
  3593. memb_state_recovery_enter (instance, memb_commit_token);
  3594. }
  3595. break;
  3596. case MEMB_STATE_RECOVERY:
  3597. if (totemip_equal (&instance->my_id.addr[0], &instance->my_ring_id.rep)) {
  3598. log_printf (instance->totemsrp_log_level_debug,
  3599. "Sending initial ORF token\n");
  3600. // TODO convert instead of initiate
  3601. orf_token_send_initial (instance);
  3602. reset_token_timeout (instance); // REVIEWED
  3603. reset_token_retransmit_timeout (instance); // REVIEWED
  3604. }
  3605. break;
  3606. }
  3607. return (0);
  3608. }
  3609. static int message_handler_token_hold_cancel (
  3610. struct totemsrp_instance *instance,
  3611. const void *msg,
  3612. size_t msg_len,
  3613. int endian_conversion_needed)
  3614. {
  3615. const struct token_hold_cancel *token_hold_cancel = msg;
  3616. if (memcmp (&token_hold_cancel->ring_id, &instance->my_ring_id,
  3617. sizeof (struct memb_ring_id)) == 0) {
  3618. instance->my_seq_unchanged = 0;
  3619. if (totemip_equal(&instance->my_ring_id.rep, &instance->my_id.addr[0])) {
  3620. timer_function_token_retransmit_timeout (instance);
  3621. }
  3622. }
  3623. return (0);
  3624. }
  3625. void main_deliver_fn (
  3626. void *context,
  3627. const void *msg,
  3628. unsigned int msg_len)
  3629. {
  3630. struct totemsrp_instance *instance = context;
  3631. const struct message_header *message_header = msg;
  3632. if (msg_len < sizeof (struct message_header)) {
  3633. log_printf (instance->totemsrp_log_level_security,
  3634. "Received message is too short... ignoring %u.\n",
  3635. (unsigned int)msg_len);
  3636. return;
  3637. }
  3638. switch (message_header->type) {
  3639. case MESSAGE_TYPE_ORF_TOKEN:
  3640. instance->stats.orf_token_rx++;
  3641. break;
  3642. case MESSAGE_TYPE_MCAST:
  3643. instance->stats.mcast_rx++;
  3644. break;
  3645. case MESSAGE_TYPE_MEMB_MERGE_DETECT:
  3646. instance->stats.memb_merge_detect_rx++;
  3647. break;
  3648. case MESSAGE_TYPE_MEMB_JOIN:
  3649. instance->stats.memb_join_rx++;
  3650. break;
  3651. case MESSAGE_TYPE_MEMB_COMMIT_TOKEN:
  3652. instance->stats.memb_commit_token_rx++;
  3653. break;
  3654. case MESSAGE_TYPE_TOKEN_HOLD_CANCEL:
  3655. instance->stats.token_hold_cancel_rx++;
  3656. break;
  3657. default:
  3658. log_printf (instance->totemsrp_log_level_security, "Type of received message is wrong... ignoring %d.\n", (int)message_header->type);
  3659. printf ("wrong message type\n");
  3660. instance->stats.rx_msg_dropped++;
  3661. return;
  3662. }
  3663. /*
  3664. * Handle incoming message
  3665. */
  3666. totemsrp_message_handlers.handler_functions[(int)message_header->type] (
  3667. instance,
  3668. msg,
  3669. msg_len,
  3670. message_header->endian_detector != ENDIAN_LOCAL);
  3671. }
  3672. void main_iface_change_fn (
  3673. void *context,
  3674. const struct totem_ip_address *iface_addr,
  3675. unsigned int iface_no)
  3676. {
  3677. struct totemsrp_instance *instance = context;
  3678. int i;
  3679. totemip_copy (&instance->my_id.addr[iface_no], iface_addr);
  3680. assert (instance->my_id.addr[iface_no].nodeid);
  3681. totemip_copy (&instance->my_memb_list[0].addr[iface_no], iface_addr);
  3682. if (instance->iface_changes++ == 0) {
  3683. memb_ring_id_create_or_load (instance, &instance->my_ring_id);
  3684. log_printf (
  3685. instance->totemsrp_log_level_debug,
  3686. "Created or loaded sequence id %lld.%s for this ring.\n",
  3687. instance->my_ring_id.seq,
  3688. totemip_print (&instance->my_ring_id.rep));
  3689. if (instance->totemsrp_service_ready_fn) {
  3690. instance->totemsrp_service_ready_fn ();
  3691. }
  3692. }
  3693. for (i = 0; i < instance->totem_config->interfaces[iface_no].member_count; i++) {
  3694. totemsrp_member_add (instance,
  3695. &instance->totem_config->interfaces[iface_no].member_list[i],
  3696. iface_no);
  3697. }
  3698. if (instance->iface_changes >= instance->totem_config->interface_count) {
  3699. memb_state_gather_enter (instance, 15);
  3700. }
  3701. }
  3702. void totemsrp_net_mtu_adjust (struct totem_config *totem_config) {
  3703. totem_config->net_mtu -= sizeof (struct mcast);
  3704. }
  3705. void totemsrp_service_ready_register (
  3706. void *context,
  3707. void (*totem_service_ready) (void))
  3708. {
  3709. struct totemsrp_instance *instance = (struct totemsrp_instance *)context;
  3710. instance->totemsrp_service_ready_fn = totem_service_ready;
  3711. }
  3712. int totemsrp_member_add (
  3713. void *context,
  3714. const struct totem_ip_address *member,
  3715. int ring_no)
  3716. {
  3717. struct totemsrp_instance *instance = (struct totemsrp_instance *)context;
  3718. int res;
  3719. res = totemrrp_member_add (instance->totemrrp_context, member, ring_no);
  3720. return (res);
  3721. }
  3722. int totemsrp_member_remove (
  3723. void *context,
  3724. const struct totem_ip_address *member,
  3725. int ring_no)
  3726. {
  3727. struct totemsrp_instance *instance = (struct totemsrp_instance *)context;
  3728. int res;
  3729. res = totemrrp_member_remove (instance->totemrrp_context, member, ring_no);
  3730. return (res);
  3731. }