totemsrp.c 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668
  1. #define WORKER_THREAD_COUNT 2
  2. /*
  3. * Copyright (c) 2003-2005 MontaVista Software, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * The first version of this code was based upon Yair Amir's PhD thesis:
  37. * http://www.cs.jhu.edu/~yairamir/phd.ps) (ch4,5).
  38. *
  39. * The current version of totemsrp implements the Totem protocol specified in:
  40. * http://citeseer.ist.psu.edu/amir95totem.html
  41. *
  42. * The deviations from the above published protocols are:
  43. * - encryption of message contents with SOBER128
  44. * - authentication of meessage contents with SHA1/HMAC
  45. * - token hold mode where token doesn't rotate on unused ring - reduces cpu
  46. * usage on 1.6ghz xeon from 35% to less then .1 % as measured by top
  47. */
  48. #include <assert.h>
  49. #include <pthread.h>
  50. #include <sys/mman.h>
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #include <sys/socket.h>
  54. #include <netdb.h>
  55. #include <sys/un.h>
  56. #include <sys/sysinfo.h>
  57. #include <sys/ioctl.h>
  58. #include <sys/param.h>
  59. #include <netinet/in.h>
  60. #include <arpa/inet.h>
  61. #include <linux/if.h>
  62. #include <linux/sockios.h>
  63. #include <unistd.h>
  64. #include <fcntl.h>
  65. #include <stdlib.h>
  66. #include <stdio.h>
  67. #include <errno.h>
  68. #include <signal.h>
  69. #include <sched.h>
  70. #include <time.h>
  71. #include <sys/time.h>
  72. #include <sys/poll.h>
  73. #include "aispoll.h"
  74. #include "totemsrp.h"
  75. #include "../include/queue.h"
  76. #include "../include/sq.h"
  77. #include "../include/list.h"
  78. #include "hdb.h"
  79. #include "swab.h"
  80. #include "crypto.h"
  81. #define AUTHENTICATION 1 /* use authentication */
  82. #define ENCRYPTION 1 /* use encryption */
  83. #define LOCALHOST_IP inet_addr("127.0.0.1")
  84. #define QUEUE_RTR_ITEMS_SIZE_MAX 2000 /* allow 512 retransmit items */
  85. #define NEW_MESSAGE_QUEUE_SIZE_MAX 2000 /* allow 500 messages to be queued */
  86. #define RETRANS_MESSAGE_QUEUE_SIZE_MAX 2000 /* allow 500 messages to be queued */
  87. #define RECEIVED_MESSAGE_QUEUE_SIZE_MAX 2000 /* allow 500 messages to be queued */
  88. #define MAXIOVS 5
  89. #define RETRANSMIT_ENTRIES_MAX 30
  90. #define MISSING_MCAST_WINDOW 128
  91. #define TIMEOUT_STATE_GATHER_JOIN 100
  92. #define TIMEOUT_STATE_GATHER_CONSENSUS 200
  93. #define TOKEN_RETRANSMITS_BEFORE_LOSS 4
  94. #define TIMEOUT_TOKEN 200
  95. #define TIMEOUT_TOKEN_RETRANSMIT (int)(TIMEOUT_TOKEN / (TOKEN_RETRANSMITS_BEFORE_LOSS + 0.2))
  96. #define TIMEOUT_TOKEN_HOLD (int)(TIMEOUT_TOKEN_RETRANSMIT * 0.8 - (1000/HZ))
  97. #define TIMEOUT_MERGE_DETECT 200
  98. #define PACKET_SIZE_MAX 2000
  99. #define FAIL_TO_RECV_CONST 250
  100. #define SEQNO_UNCHANGED_CONST 20
  101. #define TIMEOUT_DOWNCHECK 1000
  102. /*
  103. * we compare incoming messages to determine if their endian is
  104. * different - if so convert them
  105. *
  106. * do not change
  107. */
  108. #define ENDIAN_LOCAL 0xff22
  109. enum message_type {
  110. MESSAGE_TYPE_ORF_TOKEN = 0, /* Ordering, Reliability, Flow (ORF) control Token */
  111. MESSAGE_TYPE_MCAST = 1, /* ring ordered multicast message */
  112. MESSAGE_TYPE_MEMB_MERGE_DETECT = 2, /* merge rings if there are available rings */
  113. MESSAGE_TYPE_MEMB_JOIN = 3, /* membership join message */
  114. MESSAGE_TYPE_MEMB_COMMIT_TOKEN = 4, /* membership commit token */
  115. MESSAGE_TYPE_TOKEN_HOLD_CANCEL = 5, /* cancel the holding of the token */
  116. };
  117. /*
  118. * New membership algorithm local variables
  119. */
  120. struct consensus_list_item {
  121. struct in_addr addr;
  122. int set;
  123. };
  124. struct token_callback_instance {
  125. struct list_head list;
  126. int (*callback_fn) (enum totem_callback_token_type type, void *);
  127. enum totem_callback_token_type callback_type;
  128. int delete;
  129. void *data;
  130. };
  131. struct totemsrp_socket {
  132. int mcast;
  133. int token;
  134. };
  135. #define HMAC_HASH_SIZE 20
  136. struct security_header {
  137. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  138. unsigned char salt[16]; /* random number */
  139. } __attribute__((packed));
  140. struct message_header {
  141. struct security_header security_header;
  142. char type;
  143. char encapsulated;
  144. // unsigned short filler;
  145. unsigned short endian_detector;
  146. } __attribute__((packed));
  147. struct mcast {
  148. struct message_header header;
  149. int seq;
  150. int this_seqno;
  151. struct memb_ring_id ring_id;
  152. struct in_addr source;
  153. int guarantee;
  154. } __attribute__((packed));
  155. /*
  156. * MTU - multicast message header - IP header - UDP header
  157. *
  158. * On lossy switches, making use of the DF UDP flag can lead to loss of
  159. * forward progress. So the packets must be fragmented by a higher layer
  160. *
  161. * This layer can only handle packets of MTU size.
  162. */
  163. #define FRAGMENT_SIZE (PACKET_SIZE_MAX - sizeof (struct mcast) - 20 - 8)
  164. struct rtr_item {
  165. struct memb_ring_id ring_id;
  166. int seq;
  167. }__attribute__((packed));
  168. struct orf_token {
  169. struct message_header header;
  170. int seq;
  171. int token_seq;
  172. int aru;
  173. struct in_addr aru_addr;
  174. struct memb_ring_id ring_id;
  175. short int fcc;
  176. int retrans_flg;
  177. int rtr_list_entries;
  178. struct rtr_item rtr_list[0];
  179. }__attribute__((packed));
  180. struct memb_join {
  181. struct message_header header;
  182. struct in_addr proc_list[PROCESSOR_COUNT_MAX];
  183. int proc_list_entries;
  184. struct in_addr failed_list[PROCESSOR_COUNT_MAX];
  185. int failed_list_entries;
  186. unsigned long long ring_seq;
  187. } __attribute__((packed));
  188. struct memb_merge_detect {
  189. struct message_header header;
  190. struct memb_ring_id ring_id;
  191. } __attribute__((packed));
  192. struct token_hold_cancel {
  193. struct message_header header;
  194. struct memb_ring_id ring_id;
  195. } __attribute__((packed));
  196. struct memb_commit_token_memb_entry {
  197. struct memb_ring_id ring_id;
  198. int aru;
  199. int high_delivered;
  200. int received_flg;
  201. }__attribute__((packed));
  202. struct memb_commit_token {
  203. struct message_header header;
  204. int token_seq;
  205. struct memb_ring_id ring_id;
  206. unsigned int retrans_flg;
  207. int memb_index;
  208. int addr_entries;
  209. struct in_addr addr[PROCESSOR_COUNT_MAX];
  210. struct memb_commit_token_memb_entry memb_list[PROCESSOR_COUNT_MAX];
  211. }__attribute__((packed));
  212. struct worker_thread_group {
  213. int threadcount;
  214. int last_scheduled;
  215. struct worker_thread *threads;
  216. void (*worker_fn) (void *thread_state, void *work_item);
  217. };
  218. struct thread_data {
  219. void *thread_state;
  220. void *data;
  221. };
  222. struct worker_thread {
  223. struct worker_thread_group *worker_thread_group;
  224. pthread_mutex_t new_work_mutex;
  225. pthread_cond_t new_work_cond;
  226. pthread_cond_t cond;
  227. pthread_mutex_t done_work_mutex;
  228. pthread_cond_t done_work_cond;
  229. pthread_t thread_id;
  230. struct queue queue;
  231. void *thread_state;
  232. struct thread_data thread_data;
  233. };
  234. struct mcast_worker_fn_work_item {
  235. struct sort_queue_item *sort_queue_item;
  236. struct totemsrp_instance *instance;
  237. };
  238. struct message_item {
  239. struct mcast *mcast;
  240. struct iovec iovec[MAXIOVS];
  241. int iov_len;
  242. };
  243. struct sort_queue_item {
  244. struct iovec iovec[MAXIOVS];
  245. int iov_len;
  246. };
  247. struct orf_token_mcast_thread_state {
  248. char iobuf[9000];
  249. prng_state prng_state;
  250. };
  251. enum memb_state {
  252. MEMB_STATE_OPERATIONAL = 1,
  253. MEMB_STATE_GATHER = 2,
  254. MEMB_STATE_COMMIT = 3,
  255. MEMB_STATE_RECOVERY = 4
  256. };
  257. struct totemsrp_instance {
  258. /*
  259. * Authentication of messages
  260. */
  261. hmac_state totemsrp_hmac_state;
  262. prng_state totemsrp_prng_state;
  263. unsigned char totemsrp_private_key[1024];
  264. unsigned int totemsrp_private_key_len;
  265. int stats_sent;
  266. int stats_recv;
  267. int stats_delv;
  268. int stats_remcasts;
  269. int stats_orf_token;
  270. struct timeval stats_tv_start;
  271. /*
  272. * Flow control mcasts and remcasts on last and current orf_token
  273. */
  274. int fcc_remcast_last;
  275. int fcc_mcast_last;
  276. int fcc_mcast_current;
  277. int fcc_remcast_current;
  278. struct consensus_list_item consensus_list[PROCESSOR_COUNT_MAX];
  279. int consensus_list_entries;
  280. struct in_addr my_proc_list[PROCESSOR_COUNT_MAX];
  281. struct in_addr my_failed_list[PROCESSOR_COUNT_MAX];
  282. struct in_addr my_new_memb_list[PROCESSOR_COUNT_MAX];
  283. struct in_addr my_trans_memb_list[PROCESSOR_COUNT_MAX];
  284. struct in_addr my_memb_list[PROCESSOR_COUNT_MAX];
  285. struct in_addr my_deliver_memb_list[PROCESSOR_COUNT_MAX];
  286. int my_proc_list_entries;
  287. int my_failed_list_entries;
  288. int my_new_memb_entries;
  289. int my_trans_memb_entries;
  290. int my_memb_entries;
  291. int my_deliver_memb_entries;
  292. struct memb_ring_id my_ring_id;
  293. struct memb_ring_id my_old_ring_id;
  294. int my_aru_count;
  295. int my_merge_detect_timeout_outstanding;
  296. int my_last_aru;
  297. int my_seq_unchanged;
  298. int my_received_flg;
  299. int my_high_seq_received;
  300. int my_install_seq;
  301. int my_rotation_counter;
  302. int my_set_retrans_flg;
  303. int my_retrans_flg_count;
  304. unsigned int my_high_ring_delivered;
  305. unsigned int timeout_token;
  306. unsigned int timeout_token_retransmit;
  307. unsigned int timeout_token_hold;
  308. unsigned int token_retransmits_before_loss;
  309. unsigned int timeout_state_gather_join;
  310. unsigned int timeout_state_gather_consensus;
  311. unsigned int timeout_merge_detect;
  312. unsigned int timeout_downcheck;
  313. unsigned int fail_to_recv_const;
  314. /*
  315. * Queues used to order, deliver, and recover messages
  316. */
  317. struct queue new_message_queue;
  318. struct queue retrans_message_queue;
  319. struct sq regular_sort_queue;
  320. struct sq recovery_sort_queue;
  321. /*
  322. * File descriptors in use by TOTEMSRP
  323. */
  324. struct totemsrp_socket totemsrp_sockets[2];
  325. /*
  326. * Received up to and including
  327. */
  328. int my_aru;
  329. int my_high_delivered;
  330. struct list_head token_callback_received_listhead;
  331. struct list_head token_callback_sent_listhead;
  332. char orf_token_retransmit[15000]; // sizeof (struct orf_token) + sizeof (struct rtr_item) * RETRANSMIT_ENTRIES_MAX];
  333. int orf_token_retransmit_size;
  334. int my_token_seq;
  335. /*
  336. * Timers
  337. */
  338. poll_timer_handle timer_orf_token_timeout;
  339. poll_timer_handle timer_orf_token_retransmit_timeout;
  340. poll_timer_handle timer_orf_token_hold_retransmit_timeout;
  341. poll_timer_handle timer_merge_detect_timeout;
  342. poll_timer_handle memb_timer_state_gather_join_timeout;
  343. poll_timer_handle memb_timer_state_gather_consensus_timeout;
  344. poll_timer_handle memb_timer_state_commit_timeout;
  345. poll_timer_handle timer_netif_check_timeout;
  346. /*
  347. * Function and data used to log messages
  348. */
  349. int totemsrp_log_level_security;
  350. int totemsrp_log_level_error;
  351. int totemsrp_log_level_warning;
  352. int totemsrp_log_level_notice;
  353. int totemsrp_log_level_debug;
  354. void (*totemsrp_log_printf) (int level, char *format, ...);
  355. enum memb_state memb_state;
  356. struct sockaddr_in my_id;
  357. struct sockaddr_in next_memb;
  358. struct sockaddr_in memb_local_sockaddr_in;
  359. char iov_buffer[15000]; //PACKET_SIZE_MAX];
  360. struct iovec totemsrp_iov_recv;
  361. poll_handle *totemsrp_poll_handle;
  362. struct totem_interface *totemsrp_interfaces;
  363. int totemsrp_interface_count;
  364. int netif_state_report;
  365. int netif_bind_state;
  366. struct worker_thread_group worker_thread_group;
  367. struct worker_thread_group worker_thread_group_orf_token_mcast;
  368. /*
  369. * Function called when new message received
  370. */
  371. int (*totemsrp_recv) (char *group, struct iovec *iovec, int iov_len);
  372. /*
  373. * Multicast address
  374. */
  375. struct sockaddr_in sockaddr_in_mcast;
  376. void (*totemsrp_deliver_fn) (
  377. struct in_addr source_addr,
  378. struct iovec *iovec,
  379. int iov_len,
  380. int endian_conversion_required);
  381. void (*totemsrp_confchg_fn) (
  382. enum totem_configuration_type configuration_type,
  383. struct in_addr *member_list, int member_list_entries,
  384. struct in_addr *left_list, int left_list_entries,
  385. struct in_addr *joined_list, int joined_list_entries,
  386. struct memb_ring_id *ring_id);
  387. char iov_encrypted_buffer[15000];
  388. struct iovec iov_encrypted;
  389. int global_seqno;
  390. int my_token_held;
  391. unsigned long long token_ring_id_seq;
  392. int log_digest;
  393. int last_released;
  394. int set_aru;
  395. int totemsrp_brake;
  396. int old_ring_state_saved;
  397. int old_ring_state_aru;
  398. int old_ring_state_high_seq_received;
  399. int ring_saved;
  400. int my_last_seq;
  401. struct timeval tv_old;
  402. int firstrun;
  403. };
  404. struct message_handlers {
  405. int count;
  406. int (*handler_functions[6]) (struct totemsrp_instance *,
  407. struct sockaddr_in *, struct iovec *, int, int, int);
  408. };
  409. /*
  410. * forward decls
  411. */
  412. static int message_handler_orf_token (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  413. static int message_handler_mcast (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  414. static int message_handler_memb_merge_detect (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  415. static int message_handler_memb_join (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  416. static int message_handler_memb_commit_token (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  417. static int message_handler_token_hold_cancel (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  418. static void memb_ring_id_create_or_load (struct totemsrp_instance *, struct memb_ring_id *);
  419. static int recv_handler (poll_handle handle, int fd, int revents, void *data, unsigned int *prio);
  420. static int netif_determine (struct totemsrp_instance *instance, struct sockaddr_in *bindnet, struct sockaddr_in *bound_to,int *interface_up);
  421. static int loopback_determine (struct sockaddr_in *bound_to);
  422. static void netif_down_check (struct totemsrp_instance *instance);
  423. static void token_callbacks_execute (struct totemsrp_instance *instance, enum totem_callback_token_type type);
  424. #define NETIF_STATE_REPORT_UP 1
  425. #define NETIF_STATE_REPORT_DOWN 2
  426. #define BIND_STATE_UNBOUND 0
  427. #define BIND_STATE_REGULAR 1
  428. #define BIND_STATE_LOOPBACK 2
  429. static int totemsrp_build_sockets (
  430. struct totemsrp_instance *instance,
  431. struct sockaddr_in *sockaddr_mcast,
  432. struct sockaddr_in *sockaddr_bindnet,
  433. struct totemsrp_socket *sockets,
  434. struct sockaddr_in *bound_to,
  435. int *interface_up);
  436. static int totemsrp_build_sockets_loopback (
  437. struct totemsrp_instance *instance,
  438. struct sockaddr_in *sockaddr_mcast,
  439. struct sockaddr_in *sockaddr_bindnet,
  440. struct totemsrp_socket *sockets,
  441. struct sockaddr_in *bound_to);
  442. static void memb_state_gather_enter (struct totemsrp_instance *instance);
  443. static void messages_deliver_to_app (struct totemsrp_instance *instance, int skip, int end_point);
  444. static int orf_token_mcast (struct totemsrp_instance *instance, struct orf_token *oken,
  445. int fcc_mcasts_allowed, struct sockaddr_in *system_from);
  446. static int messages_free (struct totemsrp_instance *instance, int token_aru);
  447. static void encrypt_and_sign (struct totemsrp_instance *instance, struct iovec *iovec, int iov_len);
  448. static int authenticate_and_decrypt (struct totemsrp_instance *instance, struct iovec *iov);
  449. static int recv_handler (poll_handle handle, int fd, int revents, void *data, unsigned int *prio);
  450. static void memb_ring_id_store (struct totemsrp_instance *instance, struct memb_commit_token *commit_token);
  451. static void memb_state_commit_token_update (struct totemsrp_instance *instance, struct memb_commit_token *memb_commit_token);
  452. static int memb_state_commit_token_send (struct totemsrp_instance *instance, struct memb_commit_token *memb_commit_token);
  453. static void memb_state_commit_token_create (struct totemsrp_instance *instance, struct memb_commit_token *commit_token);
  454. static int token_hold_cancel_send (struct totemsrp_instance *instance);
  455. static void orf_token_endian_convert (struct orf_token *in, struct orf_token *out);
  456. static void memb_commit_token_endian_convert (struct memb_commit_token *in, struct memb_commit_token *out);
  457. static void memb_join_endian_convert (struct memb_join *in, struct memb_join *out);
  458. static void mcast_endian_convert (struct mcast *in, struct mcast *out);
  459. static void timer_function_orf_token_timeout (void *data);
  460. static void timer_function_token_retransmit_timeout (void *data);
  461. static void timer_function_token_hold_retransmit_timeout (void *data);
  462. static void timer_function_merge_detect_timeout (void *data);
  463. /*
  464. * All instances in one database
  465. */
  466. static struct saHandleDatabase totemsrp_instance_database = {
  467. .handleCount = 0,
  468. .handles = 0,
  469. .handleInstanceDestructor = 0
  470. };
  471. struct message_handlers totemsrp_message_handlers = {
  472. 6,
  473. {
  474. message_handler_orf_token,
  475. message_handler_mcast,
  476. message_handler_memb_merge_detect,
  477. message_handler_memb_join,
  478. message_handler_memb_commit_token,
  479. message_handler_token_hold_cancel
  480. }
  481. };
  482. void instance_initialize (struct totemsrp_instance *instance)
  483. {
  484. memset (instance, 0, sizeof (struct totemsrp_instance));
  485. list_init (&instance->token_callback_received_listhead);
  486. list_init (&instance->token_callback_sent_listhead);
  487. instance->my_received_flg = 1;
  488. instance->timeout_token = TIMEOUT_TOKEN;
  489. instance->timeout_token_retransmit = TIMEOUT_TOKEN_RETRANSMIT;
  490. instance->token_retransmits_before_loss = TOKEN_RETRANSMITS_BEFORE_LOSS;
  491. instance->timeout_state_gather_join = TIMEOUT_STATE_GATHER_JOIN;
  492. instance->timeout_state_gather_consensus = TIMEOUT_STATE_GATHER_CONSENSUS;
  493. instance->timeout_merge_detect = TIMEOUT_MERGE_DETECT;
  494. instance->timeout_downcheck = TIMEOUT_DOWNCHECK;
  495. instance->fail_to_recv_const = FAIL_TO_RECV_CONST;
  496. instance->my_token_seq = -1;
  497. instance->memb_state = MEMB_STATE_OPERATIONAL;
  498. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  499. instance->totemsrp_iov_recv.iov_base = instance->iov_buffer;
  500. instance->totemsrp_iov_recv.iov_len = sizeof (instance->iov_buffer);
  501. instance->iov_encrypted.iov_base = instance->iov_encrypted_buffer;
  502. instance->iov_encrypted.iov_len = sizeof (instance->iov_encrypted_buffer);
  503. instance->set_aru = -1;
  504. }
  505. #ifdef CODE_COVERAGE_COMPILE_OUT
  506. void print_digest (char *where, unsigned char *digest)
  507. {
  508. int i;
  509. printf ("DIGEST %s:\n", where);
  510. for (i = 0; i < 16; i++) {
  511. printf ("%x ", digest[i]);
  512. }
  513. printf ("\n");
  514. }
  515. void print_msg (unsigned char *msg, int size)
  516. {
  517. int i;
  518. printf ("MSG CONTENTS START\n");
  519. for (i = 0; i < size; i++) {
  520. printf ("%x ", msg[i]);
  521. if ((i % 16) == 15) {
  522. printf ("\n");
  523. }
  524. }
  525. printf ("MSG CONTENTS DONE\n");
  526. }
  527. #endif
  528. static void orf_token_mcast_worker_fn (void *thread_state, void *work_item);
  529. static void *worker_thread (void *thread_data_in) {
  530. struct thread_data *thread_data = (struct thread_data *)thread_data_in;
  531. struct orf_token_mcast_thread_state *orf_token_mcast_thread_state =
  532. (struct orf_token_mcast_thread_state *)thread_data->thread_state;
  533. struct worker_thread *worker_thread =
  534. (struct worker_thread *)thread_data->data;
  535. void *data_for_worker_fn;
  536. for (;;) {
  537. pthread_mutex_lock (&worker_thread->new_work_mutex);
  538. if (queue_is_empty (&worker_thread->queue) == 1) {
  539. pthread_cond_wait (&worker_thread->new_work_cond,
  540. &worker_thread->new_work_mutex);
  541. }
  542. data_for_worker_fn = queue_item_get (&worker_thread->queue);
  543. worker_thread->worker_thread_group->worker_fn (orf_token_mcast_thread_state, data_for_worker_fn);
  544. queue_item_remove (&worker_thread->queue);
  545. pthread_mutex_unlock (&worker_thread->new_work_mutex);
  546. pthread_mutex_lock (&worker_thread->done_work_mutex);
  547. if (queue_is_empty (&worker_thread->queue) == 1) {
  548. pthread_cond_signal (&worker_thread->done_work_cond);
  549. }
  550. pthread_mutex_unlock (&worker_thread->done_work_mutex);
  551. }
  552. return (0);
  553. }
  554. static int worker_thread_group_init (
  555. struct worker_thread_group *worker_thread_group,
  556. int threads,
  557. int items_max,
  558. int item_size,
  559. int thread_state_size,
  560. void (*thread_state_constructor)(void *),
  561. void (*worker_fn)(void *thread_state, void *work_item))
  562. {
  563. int i;
  564. worker_thread_group->threadcount = threads;
  565. worker_thread_group->last_scheduled = 0;
  566. worker_thread_group->worker_fn = worker_fn;
  567. worker_thread_group->threads = malloc (sizeof (struct worker_thread) *
  568. threads);
  569. if (worker_thread_group->threads == 0) {
  570. return (-1);
  571. }
  572. for (i = 0; i < threads; i++) {
  573. worker_thread_group->threads[i].thread_state = malloc (thread_state_size);
  574. thread_state_constructor (worker_thread_group->threads[i].thread_state);
  575. worker_thread_group->threads[i].worker_thread_group = worker_thread_group;
  576. pthread_mutex_init (&worker_thread_group->threads[i].new_work_mutex, NULL);
  577. pthread_cond_init (&worker_thread_group->threads[i].new_work_cond, NULL);
  578. pthread_mutex_init (&worker_thread_group->threads[i].done_work_mutex, NULL);
  579. pthread_cond_init (&worker_thread_group->threads[i].done_work_cond, NULL);
  580. queue_init (&worker_thread_group->threads[i].queue, items_max,
  581. item_size);
  582. worker_thread_group->threads[i].thread_data.thread_state =
  583. worker_thread_group->threads[i].thread_state;
  584. worker_thread_group->threads[i].thread_data.data = &worker_thread_group->threads[i];
  585. pthread_create (&worker_thread_group->threads[i].thread_id,
  586. NULL, worker_thread, &worker_thread_group->threads[i].thread_data);
  587. }
  588. return (0);
  589. }
  590. static void worker_thread_group_exit (
  591. struct worker_thread_group *worker_thread_group)
  592. {
  593. int i;
  594. for (i = 0; i < worker_thread_group->threadcount; i++) {
  595. pthread_cancel (worker_thread_group->threads[i].thread_id);
  596. }
  597. }
  598. static void worker_thread_group_work_add (
  599. struct worker_thread_group *worker_thread_group,
  600. void *item)
  601. {
  602. int schedule;
  603. schedule = (worker_thread_group->last_scheduled + 1) % (worker_thread_group->threadcount);
  604. worker_thread_group->last_scheduled = schedule;
  605. pthread_mutex_lock (&worker_thread_group->threads[schedule].new_work_mutex);
  606. queue_item_add (&worker_thread_group->threads[schedule].queue, item);
  607. pthread_cond_signal (&worker_thread_group->threads[schedule].new_work_cond);
  608. pthread_mutex_unlock (&worker_thread_group->threads[schedule].new_work_mutex);
  609. }
  610. static void worker_thread_group_wait (
  611. struct worker_thread_group *worker_thread_group)
  612. {
  613. int i;
  614. for (i = 0; i < worker_thread_group->threadcount; i++) {
  615. pthread_mutex_lock (&worker_thread_group->threads[i].done_work_mutex);
  616. if (queue_is_empty (&worker_thread_group->threads[i].queue) == 0) {
  617. pthread_cond_wait (&worker_thread_group->threads[i].done_work_cond,
  618. &worker_thread_group->threads[i].done_work_mutex);
  619. }
  620. pthread_mutex_unlock (&worker_thread_group->threads[i].done_work_mutex);
  621. }
  622. }
  623. static void orf_token_mcast_thread_state_constructor (
  624. void *orf_token_mcast_thread_state_in)
  625. {
  626. struct orf_token_mcast_thread_state *orf_token_mcast_thread_state =
  627. (struct orf_token_mcast_thread_state *)orf_token_mcast_thread_state_in;
  628. memset (orf_token_mcast_thread_state, 0,
  629. sizeof (orf_token_mcast_thread_state));
  630. rng_make_prng (128, PRNG_SOBER,
  631. &orf_token_mcast_thread_state->prng_state, NULL);
  632. }
  633. /*
  634. * Exported interfaces
  635. */
  636. int totemsrp_initialize (
  637. poll_handle *poll_handle,
  638. totemsrp_handle *handle,
  639. struct totem_config *totem_config,
  640. void (*deliver_fn) (
  641. struct in_addr source_addr,
  642. struct iovec *iovec,
  643. int iov_len,
  644. int endian_conversion_required),
  645. void (*confchg_fn) (
  646. enum totem_configuration_type configuration_type,
  647. struct in_addr *member_list, int member_list_entries,
  648. struct in_addr *left_list, int left_list_entries,
  649. struct in_addr *joined_list, int joined_list_entries,
  650. struct memb_ring_id *ring_id))
  651. {
  652. unsigned int *timeouts = totem_config->timeouts;
  653. struct totemsrp_instance *instance;
  654. SaErrorT error;
  655. int i;
  656. error = saHandleCreate (&totemsrp_instance_database,
  657. sizeof (struct totemsrp_instance), handle);
  658. if (error != SA_OK) {
  659. goto error_exit;
  660. }
  661. error = saHandleInstanceGet (&totemsrp_instance_database, *handle,
  662. (void *)&instance);
  663. if (error != SA_OK) {
  664. goto error_destroy;
  665. }
  666. instance_initialize (instance);
  667. /*
  668. * Configure logging
  669. */
  670. instance->totemsrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  671. instance->totemsrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  672. instance->totemsrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  673. instance->totemsrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  674. instance->totemsrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  675. instance->totemsrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  676. instance->timeout_token_hold = (int)(instance->timeout_token_retransmit * 0.8 - (1000/HZ));
  677. /*
  678. * Initialize random number generator for later use to generate salt
  679. */
  680. memcpy (instance->totemsrp_private_key, totem_config->private_key,
  681. totem_config->private_key_len);
  682. instance->totemsrp_private_key_len = totem_config->private_key_len;
  683. rng_make_prng (128, PRNG_SOBER, &instance->totemsrp_prng_state, NULL);
  684. /*
  685. * Initialize local variables for totemsrp
  686. */
  687. memcpy (&instance->sockaddr_in_mcast, &totem_config->mcast_addr,
  688. sizeof (struct sockaddr_in));
  689. memset (&instance->next_memb, 0, sizeof (struct sockaddr_in));
  690. memset (instance->iov_buffer, 0, PACKET_SIZE_MAX);
  691. /*
  692. * Initialize thread group data structure
  693. */
  694. worker_thread_group_init (&instance->worker_thread_group_orf_token_mcast,
  695. WORKER_THREAD_COUNT, 128, sizeof (struct mcast_worker_fn_work_item),
  696. sizeof (struct orf_token_mcast_thread_state),
  697. orf_token_mcast_thread_state_constructor,
  698. orf_token_mcast_worker_fn);
  699. /*
  700. * Update our timeout values if they were specified in the openais.conf
  701. * file.
  702. */
  703. for (i = 0; i < MAX_TOTEM_TIMEOUTS; i++) {
  704. if (!timeouts[i]) {
  705. continue;
  706. }
  707. switch (i) {
  708. case TOTEM_TOKEN:
  709. instance->timeout_token = timeouts[i];
  710. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  711. "Overriding token timeout to (%u ms)\n", timeouts[i]);
  712. instance->timeout_token_retransmit = (int)(instance->timeout_token / (instance->token_retransmits_before_loss + 0.2));
  713. instance->timeout_token_hold = (int)(instance->timeout_token_retransmit * 0.8 - (1000/HZ));
  714. break;
  715. case TOTEM_RETRANSMIT_TOKEN:
  716. instance->timeout_token_retransmit = timeouts[i];
  717. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  718. "Overriding token retransmit timeout to (%u ms)\n", timeouts[i]);
  719. break;
  720. case TOTEM_RETRANSMITS_BEFORE_LOSS:
  721. instance->token_retransmits_before_loss = timeouts[i];
  722. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  723. "Overriding retransmits before loss (%u retrans)\n", timeouts[i]);
  724. break;
  725. case TOTEM_HOLD_TOKEN:
  726. instance->timeout_token_hold = timeouts[i];
  727. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  728. "Overriding token hold timeout to (%u ms)\n", timeouts[i]);
  729. break;
  730. case TOTEM_JOIN:
  731. instance->timeout_state_gather_join = timeouts[i];
  732. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  733. "Join Timeout set to %u ms\n", timeouts[i]);
  734. break;
  735. case TOTEM_CONSENSUS:
  736. instance->timeout_state_gather_consensus = timeouts[i];
  737. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  738. "Consensus Timeout set to %u ms\n", timeouts[i]);
  739. break;
  740. case TOTEM_MERGE:
  741. instance->timeout_merge_detect = timeouts[i];
  742. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  743. "Merge Detect Timeout set to %u ms\n", timeouts[i]);
  744. break;
  745. case TOTEM_DOWNCHECK:
  746. instance->timeout_downcheck = timeouts[i];
  747. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  748. "Downcheck Timeout set to %u ms\n", timeouts[i]);
  749. break;
  750. case TOTEM_FAIL_RECV_CONST:
  751. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  752. "Failed To Receive Const set to %u\n", timeouts[i]);
  753. instance->fail_to_recv_const = timeouts[i];
  754. break;
  755. default:
  756. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  757. "Received unknown timeout type: %d\n", timeouts[i]);
  758. break;
  759. }
  760. }
  761. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  762. "Token Timeout (%d ms) retransmit timeout (%d ms)\n",
  763. instance->timeout_token, instance->timeout_token_retransmit);
  764. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  765. "token hold (%d ms) retransmits before loss (%d retrans)\n",
  766. instance->timeout_token_hold, instance->token_retransmits_before_loss);
  767. queue_init (&instance->new_message_queue, NEW_MESSAGE_QUEUE_SIZE_MAX,
  768. sizeof (struct message_item));
  769. queue_init (&instance->retrans_message_queue, RETRANS_MESSAGE_QUEUE_SIZE_MAX,
  770. sizeof (struct message_item));
  771. sq_init (&instance->regular_sort_queue,
  772. QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct sort_queue_item), 0);
  773. sq_init (&instance->recovery_sort_queue,
  774. QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct sort_queue_item), 0);
  775. instance->totemsrp_interfaces = totem_config->interfaces;
  776. instance->totemsrp_interface_count = 1;
  777. instance->totemsrp_poll_handle = poll_handle;
  778. netif_down_check (instance);
  779. memb_state_gather_enter (instance);
  780. instance->totemsrp_deliver_fn = deliver_fn;
  781. instance->totemsrp_confchg_fn = confchg_fn;
  782. return (0);
  783. error_destroy:
  784. saHandleDestroy (&totemsrp_instance_database, *handle);
  785. error_exit:
  786. return (-1);
  787. }
  788. void totemsrp_finalize (
  789. totemsrp_handle handle)
  790. {
  791. struct totemsrp_instance *instance;
  792. SaErrorT error;
  793. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  794. (void *)&instance);
  795. if (error != SA_OK) {
  796. return;
  797. }
  798. worker_thread_group_exit (&instance->worker_thread_group_orf_token_mcast);
  799. saHandleInstancePut (&totemsrp_instance_database, handle);
  800. }
  801. /*
  802. * Set operations for use by the membership algorithm
  803. */
  804. static void memb_consensus_reset (struct totemsrp_instance *instance)
  805. {
  806. instance->consensus_list_entries = 0;
  807. }
  808. static void memb_set_subtract (
  809. struct in_addr *out_list, int *out_list_entries,
  810. struct in_addr *one_list, int one_list_entries,
  811. struct in_addr *two_list, int two_list_entries)
  812. {
  813. int found = 0;
  814. int i;
  815. int j;
  816. *out_list_entries = 0;
  817. for (i = 0; i < one_list_entries; i++) {
  818. for (j = 0; j < two_list_entries; j++) {
  819. if (one_list[i].s_addr == two_list[j].s_addr) {
  820. found = 1;
  821. break;
  822. }
  823. }
  824. if (found == 0) {
  825. out_list[*out_list_entries].s_addr = one_list[i].s_addr;
  826. *out_list_entries = *out_list_entries + 1;
  827. }
  828. found = 0;
  829. }
  830. }
  831. /*
  832. * Set consensus for a specific processor
  833. */
  834. static void memb_consensus_set (
  835. struct totemsrp_instance *instance,
  836. struct in_addr *addr)
  837. {
  838. int found = 0;
  839. int i;
  840. for (i = 0; i < instance->consensus_list_entries; i++) {
  841. if (addr->s_addr == instance->consensus_list[i].addr.s_addr) {
  842. found = 1;
  843. break; /* found entry */
  844. }
  845. }
  846. instance->consensus_list[i].addr.s_addr = addr->s_addr;
  847. instance->consensus_list[i].set = 1;
  848. if (found == 0) {
  849. instance->consensus_list_entries++;
  850. }
  851. return;
  852. }
  853. /*
  854. * Is consensus set for a specific processor
  855. */
  856. static int memb_consensus_isset (
  857. struct totemsrp_instance *instance,
  858. struct in_addr *addr)
  859. {
  860. int i;
  861. for (i = 0; i < instance->consensus_list_entries; i++) {
  862. if (addr->s_addr == instance->consensus_list[i].addr.s_addr) {
  863. return (instance->consensus_list[i].set);
  864. }
  865. }
  866. return (0);
  867. }
  868. /*
  869. * Is consensus agreed upon based upon consensus database
  870. */
  871. static int memb_consensus_agreed (
  872. struct totemsrp_instance *instance)
  873. {
  874. struct in_addr token_memb[PROCESSOR_COUNT_MAX];
  875. int token_memb_entries = 0;
  876. int agreed = 1;
  877. int i;
  878. memb_set_subtract (token_memb, &token_memb_entries,
  879. instance->my_proc_list, instance->my_proc_list_entries,
  880. instance->my_failed_list, instance->my_failed_list_entries);
  881. for (i = 0; i < token_memb_entries; i++) {
  882. if (memb_consensus_isset (instance, &token_memb[i]) == 0) {
  883. agreed = 0;
  884. break;
  885. }
  886. }
  887. assert (token_memb_entries >= 1);
  888. return (agreed);
  889. }
  890. static void memb_consensus_notset (
  891. struct totemsrp_instance *instance,
  892. struct in_addr *no_consensus_list,
  893. int *no_consensus_list_entries,
  894. struct in_addr *comparison_list,
  895. int comparison_list_entries)
  896. {
  897. int i;
  898. *no_consensus_list_entries = 0;
  899. for (i = 0; i < instance->my_proc_list_entries; i++) {
  900. if (memb_consensus_isset (instance, &instance->my_proc_list[i]) == 0) {
  901. no_consensus_list[*no_consensus_list_entries].s_addr = instance->my_proc_list[i].s_addr;
  902. *no_consensus_list_entries = *no_consensus_list_entries + 1;
  903. }
  904. }
  905. }
  906. /*
  907. * Is set1 equal to set2 Entries can be in different orders
  908. */
  909. static int memb_set_equal (struct in_addr *set1, int set1_entries,
  910. struct in_addr *set2, int set2_entries)
  911. {
  912. int i;
  913. int j;
  914. int found = 0;
  915. if (set1_entries != set2_entries) {
  916. return (0);
  917. }
  918. for (i = 0; i < set2_entries; i++) {
  919. for (j = 0; j < set1_entries; j++) {
  920. if (set1[j].s_addr == set2[i].s_addr) {
  921. found = 1;
  922. break;
  923. }
  924. }
  925. if (found == 0) {
  926. return (0);
  927. }
  928. found = 0;
  929. }
  930. return (1);
  931. }
  932. /*
  933. * Is subset fully contained in fullset
  934. */
  935. static int memb_set_subset (
  936. struct in_addr *subset, int subset_entries,
  937. struct in_addr *fullset, int fullset_entries)
  938. {
  939. int i;
  940. int j;
  941. int found = 0;
  942. if (subset_entries > fullset_entries) {
  943. return (0);
  944. }
  945. for (i = 0; i < subset_entries; i++) {
  946. for (j = 0; j < fullset_entries; j++) {
  947. if (subset[i].s_addr == fullset[j].s_addr) {
  948. found = 1;
  949. }
  950. }
  951. if (found == 0) {
  952. return (0);
  953. }
  954. found = 1;
  955. }
  956. return (1);
  957. }
  958. /*
  959. * merge subset into fullset taking care not to add duplicates
  960. */
  961. static void memb_set_merge (
  962. struct in_addr *subset, int subset_entries,
  963. struct in_addr *fullset, int *fullset_entries)
  964. {
  965. int found = 0;
  966. int i;
  967. int j;
  968. for (i = 0; i < subset_entries; i++) {
  969. for (j = 0; j < *fullset_entries; j++) {
  970. if (fullset[j].s_addr == subset[i].s_addr) {
  971. found = 1;
  972. break;
  973. }
  974. }
  975. if (found == 0) {
  976. fullset[j].s_addr = subset[i].s_addr;
  977. *fullset_entries = *fullset_entries + 1;
  978. }
  979. found = 0;
  980. }
  981. return;
  982. }
  983. static void memb_set_and (
  984. struct in_addr *set1, int set1_entries,
  985. struct in_addr *set2, int set2_entries,
  986. struct in_addr *and, int *and_entries)
  987. {
  988. int i;
  989. int j;
  990. int found = 0;
  991. *and_entries = 0;
  992. for (i = 0; i < set2_entries; i++) {
  993. for (j = 0; j < set1_entries; j++) {
  994. if (set1[j].s_addr == set2[i].s_addr) {
  995. found = 1;
  996. break;
  997. }
  998. }
  999. if (found) {
  1000. and[*and_entries].s_addr = set1[j].s_addr;
  1001. *and_entries = *and_entries + 1;
  1002. }
  1003. found = 0;
  1004. }
  1005. return;
  1006. }
  1007. #ifdef CODE_COVERGE
  1008. static void memb_set_print (
  1009. char *string,
  1010. struct in_addr *list,
  1011. int list_entries)
  1012. {
  1013. int i;
  1014. printf ("List '%s' contains %d entries:\n", string, list_entries);
  1015. for (i = 0; i < list_entries; i++) {
  1016. printf ("addr %s\n", inet_ntoa (list[i]));
  1017. }
  1018. }
  1019. #endif
  1020. static void reset_token_retransmit_timeout (struct totemsrp_instance *instance)
  1021. {
  1022. poll_timer_delete (*instance->totemsrp_poll_handle,
  1023. instance->timer_orf_token_retransmit_timeout);
  1024. poll_timer_add (*instance->totemsrp_poll_handle,
  1025. instance->timeout_token_retransmit,
  1026. (void *)instance,
  1027. timer_function_token_retransmit_timeout,
  1028. &instance->timer_orf_token_retransmit_timeout);
  1029. }
  1030. static void start_merge_detect_timeout (struct totemsrp_instance *instance)
  1031. {
  1032. if (instance->my_merge_detect_timeout_outstanding == 0) {
  1033. poll_timer_add (*instance->totemsrp_poll_handle,
  1034. instance->timeout_merge_detect,
  1035. (void *)instance,
  1036. timer_function_merge_detect_timeout,
  1037. &instance->timer_merge_detect_timeout);
  1038. instance->my_merge_detect_timeout_outstanding = 1;
  1039. }
  1040. }
  1041. static void cancel_merge_detect_timeout (struct totemsrp_instance *instance)
  1042. {
  1043. poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_merge_detect_timeout);
  1044. instance->my_merge_detect_timeout_outstanding = 0;
  1045. }
  1046. /*
  1047. * ring_state_* is used to save and restore the sort queue
  1048. * state when a recovery operation fails (and enters gather)
  1049. */
  1050. static void old_ring_state_save (struct totemsrp_instance *instance)
  1051. {
  1052. if (instance->old_ring_state_saved == 0) {
  1053. instance->old_ring_state_saved = 1;
  1054. instance->old_ring_state_aru = instance->my_aru;
  1055. instance->old_ring_state_high_seq_received = instance->my_high_seq_received;
  1056. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1057. "Saving state aru %d high seq recieved %d\n",
  1058. instance->my_aru, instance->my_high_seq_received);
  1059. }
  1060. }
  1061. static void ring_save (struct totemsrp_instance *instance)
  1062. {
  1063. if (instance->ring_saved == 0) {
  1064. instance->ring_saved = 1;
  1065. memcpy (&instance->my_old_ring_id, &instance->my_ring_id,
  1066. sizeof (struct memb_ring_id));
  1067. }
  1068. }
  1069. static void ring_reset (struct totemsrp_instance *instance)
  1070. {
  1071. instance->ring_saved = 0;
  1072. }
  1073. static void ring_state_restore (struct totemsrp_instance *instance)
  1074. {
  1075. if (instance->old_ring_state_saved) {
  1076. instance->my_ring_id.rep.s_addr = 0;
  1077. instance->my_aru = instance->old_ring_state_aru;
  1078. instance->my_high_seq_received = instance->old_ring_state_high_seq_received;
  1079. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1080. "Restoring instance->my_aru %d my high seq received %d\n",
  1081. instance->my_aru, instance->my_high_seq_received);
  1082. }
  1083. }
  1084. static void old_ring_state_reset (struct totemsrp_instance *instance)
  1085. {
  1086. instance->old_ring_state_saved = 0;
  1087. }
  1088. static void reset_token_timeout (struct totemsrp_instance *instance) {
  1089. poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_orf_token_timeout);
  1090. poll_timer_add (*instance->totemsrp_poll_handle,
  1091. instance->timeout_token,
  1092. (void *)instance,
  1093. timer_function_orf_token_timeout,
  1094. &instance->timer_orf_token_timeout);
  1095. }
  1096. static void cancel_token_timeout (struct totemsrp_instance *instance) {
  1097. poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_orf_token_timeout);
  1098. }
  1099. static void cancel_token_retransmit_timeout (struct totemsrp_instance *instance)
  1100. {
  1101. poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_orf_token_retransmit_timeout);
  1102. }
  1103. static void start_token_hold_retransmit_timeout (struct totemsrp_instance *instance)
  1104. {
  1105. poll_timer_add (*instance->totemsrp_poll_handle,
  1106. instance->timeout_token_hold,
  1107. (void *)instance,
  1108. timer_function_token_hold_retransmit_timeout,
  1109. &instance->timer_orf_token_hold_retransmit_timeout);
  1110. }
  1111. static void cancel_token_hold_retransmit_timeout (struct totemsrp_instance *instance)
  1112. {
  1113. poll_timer_delete (*instance->totemsrp_poll_handle,
  1114. instance->timer_orf_token_hold_retransmit_timeout);
  1115. }
  1116. static void memb_state_consensus_timeout_expired (
  1117. struct totemsrp_instance *instance)
  1118. {
  1119. struct in_addr no_consensus_list[PROCESSOR_COUNT_MAX];
  1120. int no_consensus_list_entries;
  1121. if (memb_consensus_agreed (instance)) {
  1122. memb_consensus_reset (instance);
  1123. memb_consensus_set (instance, &instance->my_id.sin_addr);
  1124. reset_token_timeout (instance); // REVIEWED
  1125. } else {
  1126. memb_consensus_notset (instance, no_consensus_list,
  1127. &no_consensus_list_entries,
  1128. instance->my_proc_list, instance->my_proc_list_entries);
  1129. memb_set_merge (no_consensus_list, no_consensus_list_entries,
  1130. instance->my_failed_list, &instance->my_failed_list_entries);
  1131. memb_state_gather_enter (instance);
  1132. }
  1133. }
  1134. static int memb_join_message_send (struct totemsrp_instance *instance);
  1135. static int memb_merge_detect_transmit (struct totemsrp_instance *instance);
  1136. /*
  1137. * Timers used for various states of the membership algorithm
  1138. */
  1139. static void timer_function_orf_token_timeout (void *data)
  1140. {
  1141. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1142. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1143. "The token was lost in state %d from timer %x\n", instance->memb_state, data);
  1144. switch (instance->memb_state) {
  1145. case MEMB_STATE_OPERATIONAL:
  1146. netif_down_check (instance);
  1147. memb_state_gather_enter (instance);
  1148. break;
  1149. case MEMB_STATE_GATHER:
  1150. memb_state_consensus_timeout_expired (instance);
  1151. memb_state_gather_enter (instance);
  1152. break;
  1153. case MEMB_STATE_COMMIT:
  1154. memb_state_gather_enter (instance);
  1155. break;
  1156. case MEMB_STATE_RECOVERY:
  1157. ring_state_restore (instance);
  1158. memb_state_gather_enter (instance);
  1159. break;
  1160. }
  1161. }
  1162. static void memb_timer_function_state_gather (void *data)
  1163. {
  1164. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1165. switch (instance->memb_state) {
  1166. case MEMB_STATE_OPERATIONAL:
  1167. case MEMB_STATE_RECOVERY:
  1168. assert (0); /* this should never happen */
  1169. break;
  1170. case MEMB_STATE_GATHER:
  1171. case MEMB_STATE_COMMIT:
  1172. memb_join_message_send (instance);
  1173. /*
  1174. * Restart the join timeout
  1175. `*/
  1176. poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1177. poll_timer_add (*instance->totemsrp_poll_handle,
  1178. instance->timeout_state_gather_join,
  1179. (void *)instance,
  1180. memb_timer_function_state_gather,
  1181. &instance->memb_timer_state_gather_join_timeout);
  1182. break;
  1183. }
  1184. }
  1185. static void memb_timer_function_gather_consensus_timeout (void *data)
  1186. {
  1187. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1188. memb_state_consensus_timeout_expired (instance);
  1189. }
  1190. static void deliver_messages_from_recovery_to_regular (struct totemsrp_instance *instance)
  1191. {
  1192. int i;
  1193. struct sort_queue_item *recovery_message_item;
  1194. struct sort_queue_item regular_message_item;
  1195. int res;
  1196. void *ptr;
  1197. struct mcast *mcast;
  1198. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1199. "recovery to regular %d-%d\n", 1, instance->my_aru);
  1200. /*
  1201. * Move messages from recovery to regular sort queue
  1202. */
  1203. // todo should i be initialized to 0 or 1 ?
  1204. for (i = 1; i <= instance->my_aru; i++) {
  1205. res = sq_item_get (&instance->recovery_sort_queue, i, &ptr);
  1206. if (res != 0) {
  1207. continue;
  1208. }
  1209. printf ("Transferring message with seq id %d\n", i);
  1210. recovery_message_item = (struct sort_queue_item *)ptr;
  1211. /*
  1212. * Convert recovery message into regular message
  1213. */
  1214. if (recovery_message_item->iov_len > 1) {
  1215. mcast = recovery_message_item->iovec[1].iov_base;
  1216. memcpy (&regular_message_item.iovec[0],
  1217. &recovery_message_item->iovec[1],
  1218. sizeof (struct iovec) * recovery_message_item->iov_len);
  1219. } else {
  1220. mcast = recovery_message_item->iovec[0].iov_base;
  1221. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1222. "encapsulated is %d\n",
  1223. mcast->header.encapsulated);
  1224. if (mcast->header.encapsulated == 1) {
  1225. /*
  1226. * Message is a recovery message encapsulated
  1227. * in a new ring message
  1228. */
  1229. regular_message_item.iovec[0].iov_base =
  1230. recovery_message_item->iovec[0].iov_base + sizeof (struct mcast);
  1231. regular_message_item.iovec[0].iov_len =
  1232. recovery_message_item->iovec[0].iov_len - sizeof (struct mcast);
  1233. regular_message_item.iov_len = 1;
  1234. mcast = regular_message_item.iovec[0].iov_base;
  1235. } else {
  1236. printf ("not encapsulated\n");
  1237. continue; /* TODO this case shouldn't happen */
  1238. /*
  1239. * Message is originated on new ring and not
  1240. * encapsulated
  1241. */
  1242. regular_message_item.iovec[0].iov_base =
  1243. recovery_message_item->iovec[0].iov_base;
  1244. regular_message_item.iovec[0].iov_len =
  1245. recovery_message_item->iovec[0].iov_len;
  1246. }
  1247. }
  1248. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1249. "comparing if ring id is for this processors old ring seqno %d\n",
  1250. mcast->seq);
  1251. /*
  1252. * Only add this message to the regular sort
  1253. * queue if it was originated with the same ring
  1254. * id as the previous ring
  1255. */
  1256. if (memcmp (&instance->my_old_ring_id, &mcast->ring_id,
  1257. sizeof (struct memb_ring_id)) == 0) {
  1258. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1259. "adding msg with seq no %d\n", mcast->seq, mcast->this_seqno);
  1260. regular_message_item.iov_len = recovery_message_item->iov_len;
  1261. res = sq_item_inuse (&instance->regular_sort_queue, mcast->seq);
  1262. if (res == 0) {
  1263. sq_item_add (&instance->regular_sort_queue,
  1264. &regular_message_item, mcast->seq);
  1265. if (mcast->seq > instance->old_ring_state_high_seq_received) {
  1266. instance->old_ring_state_high_seq_received = mcast->seq;
  1267. }
  1268. }
  1269. } else {
  1270. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1271. "-not adding msg with seq no %d\n", mcast->seq);
  1272. }
  1273. }
  1274. }
  1275. /*
  1276. * Change states in the state machine of the membership algorithm
  1277. */
  1278. static void memb_state_operational_enter (struct totemsrp_instance *instance)
  1279. {
  1280. struct in_addr joined_list[PROCESSOR_COUNT_MAX];
  1281. int joined_list_entries = 0;
  1282. struct in_addr left_list[PROCESSOR_COUNT_MAX];
  1283. int left_list_entries = 0;
  1284. int aru_save;
  1285. old_ring_state_reset (instance);
  1286. ring_reset (instance);
  1287. deliver_messages_from_recovery_to_regular (instance);
  1288. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1289. "Delivering to app %d to %d\n",
  1290. instance->my_high_delivered + 1, instance->old_ring_state_high_seq_received);
  1291. aru_save = instance->my_aru;
  1292. instance->my_aru = instance->old_ring_state_aru;
  1293. messages_deliver_to_app (instance, 0, instance->old_ring_state_high_seq_received);
  1294. /*
  1295. * Calculate joined and left list
  1296. */
  1297. memb_set_subtract (left_list, &left_list_entries,
  1298. instance->my_memb_list, instance->my_memb_entries,
  1299. instance->my_trans_memb_list, instance->my_trans_memb_entries);
  1300. memb_set_subtract (joined_list, &joined_list_entries,
  1301. instance->my_new_memb_list, instance->my_new_memb_entries,
  1302. instance->my_trans_memb_list, instance->my_trans_memb_entries);
  1303. /*
  1304. * Deliver transitional configuration to application
  1305. */
  1306. instance->totemsrp_confchg_fn (TOTEM_CONFIGURATION_TRANSITIONAL,
  1307. instance->my_trans_memb_list, instance->my_trans_memb_entries,
  1308. left_list, left_list_entries,
  1309. 0, 0, &instance->my_ring_id);
  1310. // TODO we need to filter to ensure we only deliver those
  1311. // messages which are part of instance->my_deliver_memb
  1312. messages_deliver_to_app (instance, 1, instance->old_ring_state_high_seq_received);
  1313. instance->my_aru = aru_save;
  1314. /*
  1315. * Deliver regular configuration to application
  1316. */
  1317. instance->totemsrp_confchg_fn (TOTEM_CONFIGURATION_REGULAR,
  1318. instance->my_new_memb_list, instance->my_new_memb_entries,
  1319. 0, 0,
  1320. joined_list, joined_list_entries, &instance->my_ring_id);
  1321. /*
  1322. * Install new membership
  1323. */
  1324. instance->my_memb_entries = instance->my_new_memb_entries;
  1325. memcpy (instance->my_memb_list, instance->my_new_memb_list,
  1326. sizeof (struct in_addr) * instance->my_memb_entries);
  1327. instance->last_released = 0;
  1328. instance->my_set_retrans_flg = 0;
  1329. /*
  1330. * The recovery sort queue now becomes the regular
  1331. * sort queue. It is necessary to copy the state
  1332. * into the regular sort queue.
  1333. */
  1334. sq_copy (&instance->regular_sort_queue, &instance->recovery_sort_queue);
  1335. instance->my_last_aru = 0;
  1336. instance->my_proc_list_entries = instance->my_new_memb_entries;
  1337. memcpy (instance->my_proc_list, instance->my_new_memb_list,
  1338. sizeof (struct in_addr) * instance->my_memb_entries);
  1339. instance->my_failed_list_entries = 0;
  1340. instance->my_high_delivered = instance->my_aru;
  1341. // TODO the recovery messages are leaked
  1342. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1343. "entering OPERATIONAL state.\n");
  1344. instance->memb_state = MEMB_STATE_OPERATIONAL;
  1345. return;
  1346. }
  1347. static void memb_state_gather_enter (struct totemsrp_instance *instance)
  1348. {
  1349. memb_set_merge (&instance->my_id.sin_addr, 1,
  1350. instance->my_proc_list, &instance->my_proc_list_entries);
  1351. memb_join_message_send (instance);
  1352. /*
  1353. * Restart the join timeout
  1354. */
  1355. poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1356. poll_timer_add (*instance->totemsrp_poll_handle,
  1357. instance->timeout_state_gather_join,
  1358. (void *)instance,
  1359. memb_timer_function_state_gather,
  1360. &instance->memb_timer_state_gather_join_timeout);
  1361. /*
  1362. * Restart the consensus timeout
  1363. */
  1364. poll_timer_delete (*instance->totemsrp_poll_handle,
  1365. instance->memb_timer_state_gather_consensus_timeout);
  1366. poll_timer_add (*instance->totemsrp_poll_handle,
  1367. instance->timeout_state_gather_consensus,
  1368. (void *)instance,
  1369. memb_timer_function_gather_consensus_timeout,
  1370. &instance->memb_timer_state_gather_consensus_timeout);
  1371. /*
  1372. * Cancel the token loss and token retransmission timeouts
  1373. */
  1374. cancel_token_retransmit_timeout (instance); // REVIEWED
  1375. cancel_token_timeout (instance); // REVIEWED
  1376. cancel_merge_detect_timeout (instance);
  1377. memb_consensus_reset (instance);
  1378. memb_consensus_set (instance, &instance->my_id.sin_addr);
  1379. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1380. "entering GATHER state.\n");
  1381. instance->memb_state = MEMB_STATE_GATHER;
  1382. return;
  1383. }
  1384. static void timer_function_token_retransmit_timeout (void *data);
  1385. static void memb_state_commit_enter (
  1386. struct totemsrp_instance *instance,
  1387. struct memb_commit_token *commit_token)
  1388. {
  1389. ring_save (instance);
  1390. old_ring_state_save (instance);
  1391. memb_state_commit_token_update (instance, commit_token);
  1392. memb_state_commit_token_send (instance, commit_token);
  1393. memb_ring_id_store (instance, commit_token);
  1394. poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1395. instance->memb_timer_state_gather_join_timeout = 0;
  1396. poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_consensus_timeout);
  1397. instance->memb_timer_state_gather_consensus_timeout = 0;
  1398. reset_token_timeout (instance); // REVIEWED
  1399. reset_token_retransmit_timeout (instance); // REVIEWED
  1400. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1401. "entering COMMIT state.\n");
  1402. instance->memb_state = MEMB_STATE_COMMIT;
  1403. return;
  1404. }
  1405. static void memb_state_recovery_enter (
  1406. struct totemsrp_instance *instance,
  1407. struct memb_commit_token *commit_token)
  1408. {
  1409. int i;
  1410. #ifdef COMPILE_OUT
  1411. int local_received_flg = 1;
  1412. #endif
  1413. unsigned int low_ring_aru;
  1414. unsigned int messages_originated = 0;
  1415. instance->my_high_ring_delivered = 0;
  1416. sq_reinit (&instance->recovery_sort_queue, 0);
  1417. queue_reinit (&instance->retrans_message_queue);
  1418. low_ring_aru = instance->old_ring_state_high_seq_received;
  1419. memb_state_commit_token_send (instance, commit_token);
  1420. instance->my_token_seq = -1;
  1421. /*
  1422. * Build regular configuration
  1423. */
  1424. instance->my_new_memb_entries = commit_token->addr_entries;
  1425. memcpy (instance->my_new_memb_list, commit_token->addr,
  1426. sizeof (struct in_addr) * instance->my_new_memb_entries);
  1427. /*
  1428. * Build transitional configuration
  1429. */
  1430. memb_set_and (instance->my_new_memb_list, instance->my_new_memb_entries,
  1431. instance->my_memb_list, instance->my_memb_entries,
  1432. instance->my_trans_memb_list, &instance->my_trans_memb_entries);
  1433. for (i = 0; i < instance->my_new_memb_entries; i++) {
  1434. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1435. "position [%d] member %s:\n", i, inet_ntoa (commit_token->addr[i]));
  1436. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1437. "previous ring seq %lld rep %s\n",
  1438. commit_token->memb_list[i].ring_id.seq,
  1439. inet_ntoa (commit_token->memb_list[i].ring_id.rep));
  1440. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1441. "aru %d high delivered %d received flag %d\n",
  1442. commit_token->memb_list[i].aru,
  1443. commit_token->memb_list[i].high_delivered,
  1444. commit_token->memb_list[i].received_flg);
  1445. assert (commit_token->memb_list[i].ring_id.rep.s_addr);
  1446. }
  1447. /*
  1448. * Determine if any received flag is false
  1449. */
  1450. #ifdef COMPILE_OUT
  1451. for (i = 0; i < commit_token->addr_entries; i++) {
  1452. if (memb_set_subset (&instance->my_new_memb_list[i], 1,
  1453. instance->my_trans_memb_list, instance->my_trans_memb_entries) &&
  1454. commit_token->memb_list[i].received_flg == 0) {
  1455. #endif
  1456. instance->my_deliver_memb_entries = instance->my_trans_memb_entries;
  1457. memcpy (instance->my_deliver_memb_list, instance->my_trans_memb_list,
  1458. sizeof (struct in_addr) * instance->my_trans_memb_entries);
  1459. #ifdef COMPILE_OUT
  1460. local_received_flg = 0;
  1461. break;
  1462. }
  1463. }
  1464. #endif
  1465. // if (local_received_flg == 0) {
  1466. /*
  1467. * Calculate my_low_ring_aru, instance->my_high_ring_delivered for the transitional membership
  1468. */
  1469. for (i = 0; i < commit_token->addr_entries; i++) {
  1470. printf ("comparing %d old ring %s.%lld with commit ring %s.%lld.\n", i,
  1471. inet_ntoa (instance->my_old_ring_id.rep), instance->my_old_ring_id.seq,
  1472. inet_ntoa (commit_token->memb_list[i].ring_id.rep),
  1473. commit_token->memb_list[i].ring_id.seq);
  1474. printf ("memb set subset %d\n",
  1475. memb_set_subset (&instance->my_new_memb_list[i], 1, instance->my_deliver_memb_list, instance->my_deliver_memb_entries));
  1476. if (memb_set_subset (&instance->my_new_memb_list[i], 1,
  1477. instance->my_deliver_memb_list,
  1478. instance->my_deliver_memb_entries) &&
  1479. memcmp (&instance->my_old_ring_id,
  1480. &commit_token->memb_list[i].ring_id,
  1481. sizeof (struct memb_ring_id)) == 0) {
  1482. if (low_ring_aru == 0 ||
  1483. low_ring_aru > commit_token->memb_list[i].aru) {
  1484. low_ring_aru = commit_token->memb_list[i].aru;
  1485. }
  1486. if (instance->my_high_ring_delivered < commit_token->memb_list[i].high_delivered) {
  1487. instance->my_high_ring_delivered = commit_token->memb_list[i].high_delivered;
  1488. }
  1489. }
  1490. }
  1491. assert (low_ring_aru != 0xffffffff);
  1492. /*
  1493. * Cpy all old ring messages to instance->retrans_message_queue
  1494. */
  1495. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1496. "copying all old ring messages from %d-%d.\n",
  1497. low_ring_aru + 1, instance->old_ring_state_high_seq_received);
  1498. for (i = low_ring_aru + 1; i <= instance->old_ring_state_high_seq_received; i++) {
  1499. struct sort_queue_item *sort_queue_item;
  1500. struct message_item message_item;
  1501. void *ptr;
  1502. int res;
  1503. res = sq_item_get (&instance->regular_sort_queue, i, &ptr);
  1504. if (res != 0) {
  1505. printf ("-not copying %d-\n", i);
  1506. continue;
  1507. }
  1508. printf ("copying %d\n", i);
  1509. sort_queue_item = ptr;
  1510. assert (sort_queue_item->iov_len > 0);
  1511. assert (sort_queue_item->iov_len <= MAXIOVS);
  1512. messages_originated++;
  1513. memset (&message_item, 0, sizeof (struct message_item));
  1514. // TODO LEAK
  1515. message_item.mcast = malloc (sizeof (struct mcast));
  1516. assert (message_item.mcast);
  1517. memcpy (message_item.mcast, sort_queue_item->iovec[0].iov_base,
  1518. sizeof (struct mcast));
  1519. memcpy (&message_item.mcast->ring_id, &instance->my_ring_id,
  1520. sizeof (struct memb_ring_id));
  1521. message_item.mcast->header.encapsulated = 1;
  1522. message_item.iov_len = sort_queue_item->iov_len;
  1523. memcpy (&message_item.iovec, &sort_queue_item->iovec, sizeof (struct iovec) *
  1524. sort_queue_item->iov_len);
  1525. queue_item_add (&instance->retrans_message_queue, &message_item);
  1526. }
  1527. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1528. "Originated %d messages in RECOVERY.\n", messages_originated);
  1529. // }
  1530. instance->my_aru = 0;
  1531. instance->my_aru_count = 0;
  1532. instance->my_seq_unchanged = 0;
  1533. instance->my_high_seq_received = 0;
  1534. instance->my_install_seq = 0;
  1535. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "entering RECOVERY state.\n");
  1536. reset_token_timeout (instance); // REVIEWED
  1537. reset_token_retransmit_timeout (instance); // REVIEWED
  1538. instance->memb_state = MEMB_STATE_RECOVERY;
  1539. return;
  1540. }
  1541. static void encrypt_and_sign_worker (
  1542. struct totemsrp_instance *instance,
  1543. char *buf,
  1544. int *buf_len,
  1545. struct iovec *iovec,
  1546. int iov_len,
  1547. prng_state *prng_state_in)
  1548. {
  1549. int i;
  1550. char *addr;
  1551. char keys[48];
  1552. struct security_header *header;
  1553. char *hmac_key = &keys[32];
  1554. char *cipher_key = &keys[16];
  1555. char *initial_vector = &keys[0];
  1556. unsigned long len;
  1557. int outlen = 0;
  1558. hmac_state hmac_state;
  1559. prng_state keygen_prng_state;
  1560. prng_state stream_prng_state;
  1561. header = (struct security_header *)buf;
  1562. addr = buf + sizeof (struct security_header);
  1563. memset (keys, 0, sizeof (keys));
  1564. memset (header->salt, 0, sizeof (header->salt));
  1565. #if (defined(ENCRYPTION) || defined(AUTHENITCATION))
  1566. /*
  1567. * Generate MAC, CIPHER, IV keys from private key
  1568. */
  1569. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  1570. sober128_start (&keygen_prng_state);
  1571. sober128_add_entropy (instance->totemsrp_private_key,
  1572. instance->totemsrp_private_key_len,
  1573. &keygen_prng_state);
  1574. sober128_add_entropy (header->salt, sizeof (header->salt),
  1575. &keygen_prng_state);
  1576. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  1577. #endif
  1578. #ifdef ENCRYPTION
  1579. /*
  1580. * Setup stream cipher
  1581. */
  1582. sober128_start (&stream_prng_state);
  1583. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  1584. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  1585. #endif
  1586. /*
  1587. * Copy header of message, then remainder of message, then encrypt it
  1588. */
  1589. memcpy (addr, iovec[0].iov_base + sizeof (struct security_header),
  1590. iovec[0].iov_len - sizeof (struct security_header));
  1591. addr += iovec[0].iov_len - sizeof (struct security_header);
  1592. outlen += iovec[0].iov_len;
  1593. for (i = 1; i < iov_len; i++) {
  1594. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  1595. addr += iovec[i].iov_len;
  1596. outlen += iovec[i].iov_len;
  1597. }
  1598. /*
  1599. * Encrypt message by XORing stream cipher data
  1600. */
  1601. #ifdef ENCRYPTION
  1602. sober128_read (buf + sizeof (struct security_header),
  1603. outlen - sizeof (struct security_header),
  1604. &stream_prng_state);
  1605. #endif
  1606. #ifdef AUTHENTICATION
  1607. memset (&hmac_state, 0, sizeof (hmac_state));
  1608. /*
  1609. * Sign the contents of the message with the hmac key and store signature in message
  1610. */
  1611. hmac_init (&hmac_state, DIGEST_SHA1, hmac_key, 16);
  1612. hmac_process (&hmac_state,
  1613. buf + HMAC_HASH_SIZE,
  1614. outlen - HMAC_HASH_SIZE);
  1615. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  1616. hmac_done (&hmac_state, header->hash_digest, &len);
  1617. #endif
  1618. *buf_len = outlen;
  1619. }
  1620. static void encrypt_and_sign (
  1621. struct totemsrp_instance *instance,
  1622. struct iovec *iovec,
  1623. int iov_len)
  1624. {
  1625. char *addr = instance->iov_encrypted.iov_base +
  1626. sizeof (struct security_header);
  1627. int i;
  1628. char keys[48];
  1629. struct security_header *header = instance->iov_encrypted.iov_base;
  1630. prng_state keygen_prng_state;
  1631. prng_state stream_prng_state;
  1632. char *hmac_key = &keys[32];
  1633. char *cipher_key = &keys[16];
  1634. char *initial_vector = &keys[0];
  1635. unsigned long len;
  1636. instance->iov_encrypted.iov_len = 0;
  1637. memset (keys, 0, sizeof (keys));
  1638. memset (header->salt, 0, sizeof (header->salt));
  1639. #if (defined(ENCRYPTION) || defined(AUTHENITCATION))
  1640. /*
  1641. * Generate MAC, CIPHER, IV keys from private key
  1642. */
  1643. sober128_read (header->salt, sizeof (header->salt),
  1644. &instance->totemsrp_prng_state);
  1645. sober128_start (&keygen_prng_state);
  1646. sober128_add_entropy (instance->totemsrp_private_key,
  1647. instance->totemsrp_private_key_len, &keygen_prng_state);
  1648. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  1649. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  1650. #endif
  1651. #ifdef ENCRYPTION
  1652. /*
  1653. * Setup stream cipher
  1654. */
  1655. sober128_start (&stream_prng_state);
  1656. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  1657. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  1658. #endif
  1659. #ifdef CODE_COVERAGE_COMPILE_OUT
  1660. if (log_digest) {
  1661. printf ("new encryption\n");
  1662. print_digest ("salt", header->salt);
  1663. print_digest ("initial_vector", initial_vector);
  1664. print_digest ("cipher_key", cipher_key);
  1665. print_digest ("hmac_key", hmac_key);
  1666. }
  1667. #endif
  1668. /*
  1669. * Copy header of message, then remainder of message, then encrypt it
  1670. */
  1671. memcpy (addr, iovec[0].iov_base + sizeof (struct security_header),
  1672. iovec[0].iov_len - sizeof (struct security_header));
  1673. addr += iovec[0].iov_len - sizeof (struct security_header);
  1674. instance->iov_encrypted.iov_len += iovec[0].iov_len;
  1675. for (i = 1; i < iov_len; i++) {
  1676. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  1677. addr += iovec[i].iov_len;
  1678. instance->iov_encrypted.iov_len += iovec[i].iov_len;
  1679. }
  1680. /*
  1681. * Encrypt message by XORing stream cipher data
  1682. */
  1683. #ifdef ENCRYPTION
  1684. sober128_read (instance->iov_encrypted.iov_base + sizeof (struct security_header),
  1685. instance->iov_encrypted.iov_len - sizeof (struct security_header),
  1686. &stream_prng_state);
  1687. #endif
  1688. #ifdef AUTHENTICATION
  1689. memset (&instance->totemsrp_hmac_state, 0, sizeof (hmac_state));
  1690. /*
  1691. * Sign the contents of the message with the hmac key and store signature in message
  1692. */
  1693. hmac_init (&instance->totemsrp_hmac_state, DIGEST_SHA1, hmac_key, 16);
  1694. hmac_process (&instance->totemsrp_hmac_state,
  1695. instance->iov_encrypted.iov_base + HMAC_HASH_SIZE,
  1696. instance->iov_encrypted.iov_len - HMAC_HASH_SIZE);
  1697. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  1698. hmac_done (&instance->totemsrp_hmac_state, header->hash_digest, &len);
  1699. #endif
  1700. #ifdef COMPILE_OUT
  1701. print_digest ("initial_vector", initial_vector);
  1702. print_digest ("cipher_key", cipher_key);
  1703. print_digest ("hmac_key", hmac_key);
  1704. print_digest ("salt", header->salt);
  1705. print_digest ("sent digest", header->hash_digest);
  1706. #endif
  1707. }
  1708. /*
  1709. * Only designed to work with a message with one iov
  1710. */
  1711. static int authenticate_and_decrypt (
  1712. struct totemsrp_instance *instance,
  1713. struct iovec *iov)
  1714. {
  1715. char keys[48];
  1716. struct security_header *header = iov[0].iov_base;
  1717. prng_state keygen_prng_state;
  1718. prng_state stream_prng_state;
  1719. char *hmac_key = &keys[32];
  1720. char *cipher_key = &keys[16];
  1721. char *initial_vector = &keys[0];
  1722. char digest_comparison[HMAC_HASH_SIZE];
  1723. unsigned long len;
  1724. int res = 0;
  1725. instance->iov_encrypted.iov_len = 0;
  1726. #ifdef COMPILE_OUT
  1727. printf ("Decryption message\n");
  1728. print_msg (header, iov[0].iov_len);
  1729. #endif
  1730. #if (defined(ENCRYPTION) || defined(AUTHENITCATION))
  1731. /*
  1732. * Generate MAC, CIPHER, IV keys from private key
  1733. */
  1734. memset (keys, 0, sizeof (keys));
  1735. sober128_start (&keygen_prng_state);
  1736. sober128_add_entropy (instance->totemsrp_private_key,
  1737. instance->totemsrp_private_key_len, &keygen_prng_state);
  1738. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  1739. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  1740. #endif
  1741. #ifdef ENCRYPTION
  1742. /*
  1743. * Setup stream cipher
  1744. */
  1745. sober128_start (&stream_prng_state);
  1746. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  1747. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  1748. #endif
  1749. #ifdef CODE_COVERAGE_COMPILE_OUT
  1750. if (log_digest) {
  1751. printf ("New decryption\n");
  1752. print_digest ("salt", header->salt);
  1753. print_digest ("initial_vector", initial_vector);
  1754. print_digest ("cipher_key", cipher_key);
  1755. print_digest ("hmac_key", hmac_key);
  1756. }
  1757. #endif
  1758. #ifdef AUTHENTICATION
  1759. /*
  1760. * Authenticate contents of message
  1761. */
  1762. hmac_init (&instance->totemsrp_hmac_state, DIGEST_SHA1, hmac_key, 16);
  1763. hmac_process (&instance->totemsrp_hmac_state,
  1764. iov->iov_base + HMAC_HASH_SIZE,
  1765. iov->iov_len - HMAC_HASH_SIZE);
  1766. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  1767. assert (HMAC_HASH_SIZE >= len);
  1768. hmac_done (&instance->totemsrp_hmac_state, digest_comparison, &len);
  1769. #ifdef PRINTDIGESTS
  1770. print_digest ("received digest", header->hash_digest);
  1771. print_digest ("calculated digest", digest_comparison);
  1772. #endif
  1773. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  1774. #ifdef CODE_COVERAGE_COMPILE_OUT
  1775. print_digest ("initial_vector", initial_vector);
  1776. print_digest ("cipher_key", cipher_key);
  1777. print_digest ("hmac_key", hmac_key);
  1778. print_digest ("salt", header->salt);
  1779. print_digest ("sent digest", header->hash_digest);
  1780. print_digest ("calculated digest", digest_comparison);
  1781. printf ("received message size %d\n", iov->iov_len);
  1782. #endif
  1783. instance->totemsrp_log_printf (instance->totemsrp_log_level_security, "Received message has invalid digest... ignoring.\n");
  1784. res = -1;
  1785. return (-1);
  1786. }
  1787. #endif /* AUTHENTICATION */
  1788. /*
  1789. * Decrypt the contents of the message with the cipher key
  1790. */
  1791. #ifdef ENCRYPTION
  1792. sober128_read (iov->iov_base + sizeof (struct security_header),
  1793. iov->iov_len - sizeof (struct security_header),
  1794. &stream_prng_state);
  1795. #endif
  1796. return (res);
  1797. return (0);
  1798. }
  1799. int totemsrp_new_msg_signal (totemsrp_handle handle)
  1800. {
  1801. struct totemsrp_instance *instance;
  1802. SaErrorT error;
  1803. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  1804. (void *)&instance);
  1805. if (error != SA_OK) {
  1806. goto error_exit;
  1807. }
  1808. token_hold_cancel_send (instance);
  1809. saHandleInstancePut (&totemsrp_instance_database, handle);
  1810. return (0);
  1811. error_exit:
  1812. return (-1);
  1813. }
  1814. int totemsrp_mcast (
  1815. totemsrp_handle handle,
  1816. struct iovec *iovec,
  1817. int iov_len,
  1818. int guarantee)
  1819. {
  1820. int i;
  1821. int j;
  1822. struct message_item message_item;
  1823. struct totemsrp_instance *instance;
  1824. SaErrorT error;
  1825. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  1826. (void *)&instance);
  1827. if (error != SA_OK) {
  1828. goto error_exit;
  1829. }
  1830. if (queue_is_full (&instance->new_message_queue)) {
  1831. return (-1);
  1832. }
  1833. for (j = 0, i = 0; i < iov_len; i++) {
  1834. j+= iovec[i].iov_len;
  1835. }
  1836. memset (&message_item, 0, sizeof (struct message_item));
  1837. /*
  1838. * Allocate pending item
  1839. */
  1840. // TODO LEAK
  1841. message_item.mcast = malloc (sizeof (struct mcast));
  1842. if (message_item.mcast == 0) {
  1843. goto error_mcast;
  1844. }
  1845. /*
  1846. * Set mcast header
  1847. */
  1848. message_item.mcast->header.type = MESSAGE_TYPE_MCAST;
  1849. message_item.mcast->header.endian_detector = ENDIAN_LOCAL;
  1850. message_item.mcast->header.encapsulated = 2;
  1851. message_item.mcast->guarantee = guarantee;
  1852. message_item.mcast->source.s_addr = instance->my_id.sin_addr.s_addr;
  1853. for (i = 0; i < iov_len; i++) {
  1854. // TODO LEAK
  1855. message_item.iovec[i].iov_base = malloc (iovec[i].iov_len);
  1856. if (message_item.iovec[i].iov_base == 0) {
  1857. goto error_iovec;
  1858. }
  1859. memcpy (message_item.iovec[i].iov_base, iovec[i].iov_base,
  1860. iovec[i].iov_len);
  1861. message_item.iovec[i].iov_len = iovec[i].iov_len;
  1862. }
  1863. message_item.iov_len = iov_len;
  1864. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug, "mcasted message added to pending queue\n");
  1865. queue_item_add (&instance->new_message_queue, &message_item);
  1866. saHandleInstancePut (&totemsrp_instance_database, handle);
  1867. return (0);
  1868. error_iovec:
  1869. saHandleInstancePut (&totemsrp_instance_database, handle);
  1870. for (j = 0; j < i; j++) {
  1871. free (message_item.iovec[j].iov_base);
  1872. }
  1873. return (-1);
  1874. error_mcast:
  1875. saHandleInstancePut (&totemsrp_instance_database, handle);
  1876. error_exit:
  1877. return (0);
  1878. }
  1879. /*
  1880. * Determine if there is room to queue a new message
  1881. */
  1882. int totemsrp_avail (totemsrp_handle handle)
  1883. {
  1884. int avail;
  1885. struct totemsrp_instance *instance;
  1886. SaErrorT error;
  1887. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  1888. (void *)&instance);
  1889. if (error != SA_OK) {
  1890. goto error_exit;
  1891. }
  1892. queue_avail (&instance->new_message_queue, &avail);
  1893. saHandleInstancePut (&totemsrp_instance_database, handle);
  1894. return (avail);
  1895. error_exit:
  1896. return (0);
  1897. }
  1898. static int netif_determine (
  1899. struct totemsrp_instance *instance,
  1900. struct sockaddr_in *bindnet,
  1901. struct sockaddr_in *bound_to,
  1902. int *interface_up)
  1903. {
  1904. struct sockaddr_in *sockaddr_in;
  1905. int id_fd;
  1906. struct ifconf ifc;
  1907. int numreqs = 0;
  1908. int res;
  1909. int i;
  1910. in_addr_t mask_addr;
  1911. *interface_up = 0;
  1912. /*
  1913. * Generate list of local interfaces in ifc.ifc_req structure
  1914. */
  1915. id_fd = socket (AF_INET, SOCK_STREAM, 0);
  1916. ifc.ifc_buf = 0;
  1917. do {
  1918. numreqs += 32;
  1919. ifc.ifc_len = sizeof (struct ifreq) * numreqs;
  1920. ifc.ifc_buf = (void *)realloc(ifc.ifc_buf, ifc.ifc_len);
  1921. res = ioctl (id_fd, SIOCGIFCONF, &ifc);
  1922. if (res < 0) {
  1923. close (id_fd);
  1924. return -1;
  1925. }
  1926. } while (ifc.ifc_len == sizeof (struct ifreq) * numreqs);
  1927. res = -1;
  1928. /*
  1929. * Find interface address to bind to
  1930. */
  1931. for (i = 0; i < ifc.ifc_len / sizeof (struct ifreq); i++) {
  1932. sockaddr_in = (struct sockaddr_in *)&ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_addr;
  1933. mask_addr = inet_addr ("255.255.255.0");
  1934. if ((sockaddr_in->sin_family == AF_INET) &&
  1935. (sockaddr_in->sin_addr.s_addr & mask_addr) ==
  1936. (bindnet->sin_addr.s_addr & mask_addr)) {
  1937. bound_to->sin_addr.s_addr = sockaddr_in->sin_addr.s_addr;
  1938. res = i;
  1939. if (ioctl(id_fd, SIOCGIFFLAGS, &ifc.ifc_ifcu.ifcu_req[i]) < 0) {
  1940. printf ("couldn't do ioctl\n");
  1941. }
  1942. *interface_up = ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_flags & IFF_UP;
  1943. break; /* for */
  1944. }
  1945. }
  1946. free (ifc.ifc_buf);
  1947. close (id_fd);
  1948. return (res);
  1949. }
  1950. static int loopback_determine (struct sockaddr_in *bound_to)
  1951. {
  1952. bound_to->sin_addr.s_addr = LOCALHOST_IP;
  1953. if (&bound_to->sin_addr.s_addr == 0) {
  1954. return -1;
  1955. }
  1956. return 1;
  1957. }
  1958. /*
  1959. * If the interface is up, the sockets for totem are built. If the interface is down
  1960. * this function is requeued in the timer list to retry building the sockets later.
  1961. */
  1962. static void timer_function_netif_check_timeout (
  1963. void *data)
  1964. {
  1965. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1966. int res;
  1967. int interface_no;
  1968. int interface_up;
  1969. /*
  1970. * Build sockets for every interface
  1971. */
  1972. for (interface_no = 0; interface_no < instance->totemsrp_interface_count; interface_no++) {
  1973. netif_determine (instance,
  1974. &instance->totemsrp_interfaces[interface_no].bindnet,
  1975. &instance->totemsrp_interfaces[interface_no].boundto,
  1976. &interface_up);
  1977. if (((instance->netif_bind_state & BIND_STATE_LOOPBACK) && (!interface_up))
  1978. || ((instance->netif_bind_state & BIND_STATE_REGULAR) && (interface_up))) {
  1979. break;
  1980. }
  1981. instance->totemsrp_log_printf(instance->totemsrp_log_level_debug,"network interface UP %s\n",
  1982. inet_ntoa (instance->totemsrp_interfaces[interface_no].boundto.sin_addr));
  1983. if (instance->totemsrp_sockets[interface_no].mcast > 0) {
  1984. close (instance->totemsrp_sockets[interface_no].mcast);
  1985. poll_dispatch_delete (*instance->totemsrp_poll_handle,
  1986. instance->totemsrp_sockets[interface_no].mcast);
  1987. }
  1988. if (instance->totemsrp_sockets[interface_no].token > 0) {
  1989. close (instance->totemsrp_sockets[interface_no].token);
  1990. poll_dispatch_delete (*instance->totemsrp_poll_handle,
  1991. instance->totemsrp_sockets[interface_no].token);
  1992. }
  1993. if (!interface_up) {
  1994. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,"Interface is down binding to LOOPBACK addr.\n");
  1995. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1996. res = totemsrp_build_sockets_loopback(instance,
  1997. &instance->sockaddr_in_mcast,
  1998. &instance->totemsrp_interfaces[interface_no].bindnet,
  1999. &instance->totemsrp_sockets[interface_no],
  2000. &instance->totemsrp_interfaces[interface_no].boundto);
  2001. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,"network interface LOCAL %s\n",
  2002. inet_ntoa (instance->totemsrp_interfaces[interface_no].boundto.sin_addr));
  2003. poll_dispatch_add (*instance->totemsrp_poll_handle, instance->totemsrp_sockets[interface_no].token,
  2004. POLLIN, instance, recv_handler, UINT_MAX);
  2005. continue;
  2006. }
  2007. instance->netif_bind_state = BIND_STATE_REGULAR;
  2008. /*
  2009. * Create and bind the multicast and unicast sockets
  2010. */
  2011. res = totemsrp_build_sockets (instance,
  2012. &instance->sockaddr_in_mcast,
  2013. &instance->totemsrp_interfaces[interface_no].bindnet,
  2014. &instance->totemsrp_sockets[interface_no],
  2015. &instance->totemsrp_interfaces[interface_no].boundto,
  2016. &interface_up);
  2017. poll_dispatch_add (*instance->totemsrp_poll_handle, instance->totemsrp_sockets[interface_no].mcast,
  2018. POLLIN, instance, recv_handler, UINT_MAX);
  2019. poll_dispatch_add (*instance->totemsrp_poll_handle, instance->totemsrp_sockets[interface_no].token,
  2020. POLLIN, instance, recv_handler, UINT_MAX);
  2021. }
  2022. memcpy (&instance->my_id, &instance->totemsrp_interfaces->boundto, sizeof (struct sockaddr_in));
  2023. /*
  2024. * This stuff depends on totemsrp_build_sockets
  2025. */
  2026. if (instance->firstrun == 0) {
  2027. instance->firstrun += 1;
  2028. memcpy (&instance->my_memb_list[0], &instance->totemsrp_interfaces->boundto,
  2029. sizeof (struct sockaddr_in));
  2030. memb_ring_id_create_or_load (instance, &instance->my_ring_id);
  2031. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Created or loaded sequence id %lld.%s for this ring.\n",
  2032. instance->my_ring_id.seq, inet_ntoa (instance->my_ring_id.rep));
  2033. }
  2034. if (interface_up) {
  2035. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  2036. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2037. " The network interface is now up.\n");
  2038. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  2039. memb_state_gather_enter (instance);
  2040. }
  2041. /*
  2042. * If this is a single processor, detect downs which may not
  2043. * be detected by token loss when the interface is downed
  2044. */
  2045. if (instance->my_memb_entries <= 1) {
  2046. poll_timer_add (*instance->totemsrp_poll_handle,
  2047. instance->timeout_downcheck,
  2048. (void *)instance,
  2049. timer_function_netif_check_timeout,
  2050. &instance->timer_netif_check_timeout);
  2051. }
  2052. } else {
  2053. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  2054. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2055. "The network interface is down.\n");
  2056. memb_state_gather_enter (instance);
  2057. }
  2058. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  2059. /*
  2060. * Add a timer to retry building interfaces and request memb_gather_enter
  2061. */
  2062. cancel_token_timeout (instance);
  2063. poll_timer_add (*instance->totemsrp_poll_handle,
  2064. instance->timeout_downcheck,
  2065. (void *)instance,
  2066. timer_function_netif_check_timeout,
  2067. &instance->timer_netif_check_timeout);
  2068. }
  2069. }
  2070. /*
  2071. * Check if an interface is down and reconfigure
  2072. * totemsrp waiting for it to come back up
  2073. */
  2074. static void netif_down_check (struct totemsrp_instance *instance)
  2075. {
  2076. timer_function_netif_check_timeout (instance);
  2077. }
  2078. static int totemsrp_build_sockets_loopback (
  2079. struct totemsrp_instance *instance,
  2080. struct sockaddr_in *sockaddr_mcast,
  2081. struct sockaddr_in *sockaddr_bindnet,
  2082. struct totemsrp_socket *sockets,
  2083. struct sockaddr_in *bound_to)
  2084. {
  2085. struct ip_mreq mreq;
  2086. struct sockaddr_in sockaddr_in;
  2087. int res;
  2088. memset (&mreq, 0, sizeof (struct ip_mreq));
  2089. /*
  2090. * Determine the ip address bound to and the interface name
  2091. */
  2092. res = loopback_determine (bound_to);
  2093. if (res == -1) {
  2094. return (-1);
  2095. }
  2096. /* TODO this should be somewhere else */
  2097. instance->memb_local_sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  2098. instance->memb_local_sockaddr_in.sin_family = AF_INET;
  2099. instance->memb_local_sockaddr_in.sin_port = sockaddr_mcast->sin_port;
  2100. sockaddr_in.sin_family = AF_INET;
  2101. sockaddr_in.sin_port = sockaddr_mcast->sin_port;
  2102. /*
  2103. * Setup unicast socket
  2104. */
  2105. sockets->token = socket (AF_INET, SOCK_DGRAM, 0);
  2106. if (sockets->token == -1) {
  2107. perror ("socket2");
  2108. return (-1);
  2109. }
  2110. /*
  2111. * Bind to unicast socket used for token send/receives
  2112. * This has the side effect of binding to the correct interface
  2113. */
  2114. sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  2115. res = bind (sockets->token, (struct sockaddr *)&sockaddr_in,
  2116. sizeof (struct sockaddr_in));
  2117. if (res == -1) {
  2118. perror ("bind2 failed");
  2119. return (-1);
  2120. }
  2121. memcpy(&instance->sockaddr_in_mcast, &sockaddr_in,
  2122. sizeof(struct sockaddr_in));
  2123. sockets->mcast = sockets->token;
  2124. return (0);
  2125. }
  2126. static int totemsrp_build_sockets (
  2127. struct totemsrp_instance *instance,
  2128. struct sockaddr_in *sockaddr_mcast,
  2129. struct sockaddr_in *sockaddr_bindnet,
  2130. struct totemsrp_socket *sockets,
  2131. struct sockaddr_in *bound_to,
  2132. int *interface_up)
  2133. {
  2134. struct ip_mreq mreq;
  2135. struct sockaddr_in sockaddr_in;
  2136. char flag;
  2137. int res;
  2138. memset (&mreq, 0, sizeof (struct ip_mreq));
  2139. /*
  2140. * Determine the ip address bound to and the interface name
  2141. */
  2142. res = netif_determine (instance,
  2143. sockaddr_bindnet,
  2144. bound_to,
  2145. interface_up);
  2146. if (res == -1) {
  2147. return (-1);
  2148. }
  2149. /* TODO this should be somewhere else */
  2150. instance->memb_local_sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  2151. instance->memb_local_sockaddr_in.sin_family = AF_INET;
  2152. instance->memb_local_sockaddr_in.sin_port = sockaddr_mcast->sin_port;
  2153. /*
  2154. * Create multicast socket
  2155. */
  2156. sockets->mcast = socket (AF_INET, SOCK_DGRAM, 0);
  2157. if (sockets->mcast == -1) {
  2158. perror ("socket");
  2159. return (-1);
  2160. }
  2161. if (setsockopt (sockets->mcast, SOL_IP, IP_MULTICAST_IF,
  2162. &bound_to->sin_addr, sizeof (struct in_addr)) < 0) {
  2163. instance->totemsrp_log_printf (instance->totemsrp_log_level_warning, "Could not bind to device for multicast, group messaging may not work properly. (%s)\n", strerror (errno));
  2164. }
  2165. /*
  2166. * Bind to multicast socket used for multicast send/receives
  2167. */
  2168. sockaddr_in.sin_family = AF_INET;
  2169. sockaddr_in.sin_addr.s_addr = sockaddr_mcast->sin_addr.s_addr;
  2170. sockaddr_in.sin_port = sockaddr_mcast->sin_port;
  2171. res = bind (sockets->mcast, (struct sockaddr *)&sockaddr_in,
  2172. sizeof (struct sockaddr_in));
  2173. if (res == -1) {
  2174. perror ("bind failed");
  2175. return (-1);
  2176. }
  2177. /*
  2178. * Setup unicast socket
  2179. */
  2180. sockets->token = socket (AF_INET, SOCK_DGRAM, 0);
  2181. if (sockets->token == -1) {
  2182. perror ("socket2");
  2183. return (-1);
  2184. }
  2185. /*
  2186. * Bind to unicast socket used for token send/receives
  2187. * This has the side effect of binding to the correct interface
  2188. */
  2189. sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  2190. res = bind (sockets->token, (struct sockaddr *)&sockaddr_in,
  2191. sizeof (struct sockaddr_in));
  2192. if (res == -1) {
  2193. perror ("bind2 failed");
  2194. return (-1);
  2195. }
  2196. #ifdef CONFIG_USE_BROADCAST
  2197. /* This config option doesn't work */
  2198. {
  2199. int on = 1;
  2200. setsockopt (sockets->mcast, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof (on));
  2201. }
  2202. #else
  2203. /*
  2204. * Join group membership on socket
  2205. */
  2206. mreq.imr_multiaddr.s_addr = sockaddr_mcast->sin_addr.s_addr;
  2207. mreq.imr_interface.s_addr = bound_to->sin_addr.s_addr;
  2208. res = setsockopt (sockets->mcast, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  2209. &mreq, sizeof (mreq));
  2210. if (res == -1) {
  2211. perror ("join multicast group failed");
  2212. return (-1);
  2213. }
  2214. #endif
  2215. /*
  2216. * Turn on multicast loopback
  2217. */
  2218. flag = 1;
  2219. res = setsockopt (sockets->mcast, IPPROTO_IP, IP_MULTICAST_LOOP,
  2220. &flag, sizeof (flag));
  2221. if (res == -1) {
  2222. perror ("turn off loopback");
  2223. return (-1);
  2224. }
  2225. return (0);
  2226. }
  2227. /*
  2228. * Misc Management
  2229. */
  2230. static int in_addr_compare (const void *a, const void *b) {
  2231. struct in_addr *in_addr_a = (struct in_addr *)a;
  2232. struct in_addr *in_addr_b = (struct in_addr *)b;
  2233. return (in_addr_a->s_addr > in_addr_b->s_addr);
  2234. }
  2235. /*
  2236. * ORF Token Management
  2237. */
  2238. /*
  2239. * Recast message to mcast group if it is available
  2240. */
  2241. static int orf_token_remcast (
  2242. struct totemsrp_instance *instance,
  2243. int seq)
  2244. {
  2245. struct msghdr msg_mcast;
  2246. struct sort_queue_item *sort_queue_item;
  2247. int res;
  2248. struct mcast *mcast;
  2249. void *ptr;
  2250. struct sq *sort_queue;
  2251. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2252. sort_queue = &instance->recovery_sort_queue;
  2253. } else {
  2254. sort_queue = &instance->regular_sort_queue;
  2255. }
  2256. /*
  2257. * Get RTR item at seq, if not available, return
  2258. */
  2259. res = sq_item_get (sort_queue, seq, &ptr);
  2260. if (res != 0) {
  2261. return -1;
  2262. }
  2263. sort_queue_item = ptr;
  2264. mcast = (struct mcast *)sort_queue_item->iovec[0].iov_base;
  2265. encrypt_and_sign (instance, sort_queue_item->iovec, sort_queue_item->iov_len);
  2266. /*
  2267. * Build multicast message
  2268. */
  2269. msg_mcast.msg_name = (caddr_t)&instance->sockaddr_in_mcast;
  2270. msg_mcast.msg_namelen = sizeof (struct sockaddr_in);
  2271. msg_mcast.msg_iov = &instance->iov_encrypted;
  2272. msg_mcast.msg_iovlen = 1;
  2273. msg_mcast.msg_control = 0;
  2274. msg_mcast.msg_controllen = 0;
  2275. msg_mcast.msg_flags = 0;
  2276. /*
  2277. * Multicast message
  2278. */
  2279. res = sendmsg (instance->totemsrp_sockets[0].mcast, &msg_mcast, MSG_NOSIGNAL | MSG_DONTWAIT);
  2280. if (res == -1) {
  2281. return (-1);
  2282. }
  2283. instance->stats_sent += res;
  2284. return (0);
  2285. }
  2286. /*
  2287. * Free all freeable messages from ring
  2288. */
  2289. static int messages_free (
  2290. struct totemsrp_instance *instance,
  2291. int token_aru)
  2292. {
  2293. struct sort_queue_item *regular_message;
  2294. int i, j;
  2295. int res;
  2296. int log_release = 0;
  2297. int release_to;
  2298. release_to = token_aru;
  2299. if (release_to > instance->my_last_aru) {
  2300. release_to = instance->my_last_aru;
  2301. }
  2302. if (release_to > instance->my_high_delivered) {
  2303. release_to = instance->my_high_delivered;
  2304. }
  2305. /*
  2306. * Release retransmit list items if group aru indicates they are transmitted
  2307. */
  2308. for (i = instance->last_released; i <= release_to; i++) {
  2309. void *ptr;
  2310. res = sq_item_get (&instance->regular_sort_queue, i, &ptr);
  2311. if (res == 0) {
  2312. regular_message = ptr;
  2313. for (j = 0; j < regular_message->iov_len; j++) {
  2314. free (regular_message->iovec[j].iov_base);
  2315. }
  2316. }
  2317. sq_items_release (&instance->regular_sort_queue, i);
  2318. instance->last_released = i + 1;
  2319. log_release = 1;
  2320. }
  2321. if (log_release) {
  2322. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2323. "releasing messages up to and including %d\n", release_to);
  2324. }
  2325. return (0);
  2326. }
  2327. static void update_aru (
  2328. struct totemsrp_instance *instance)
  2329. {
  2330. int i;
  2331. int res;
  2332. struct sq *sort_queue;
  2333. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2334. sort_queue = &instance->recovery_sort_queue;
  2335. } else {
  2336. sort_queue = &instance->regular_sort_queue;
  2337. }
  2338. for (i = instance->my_aru + 1; i <= instance->my_high_seq_received; i++) {
  2339. void *ptr;
  2340. res = sq_item_get (sort_queue, i, &ptr);
  2341. /*
  2342. * If hole, stop assembly
  2343. */
  2344. if (res != 0) {
  2345. break;
  2346. }
  2347. instance->my_aru = i;
  2348. }
  2349. // instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2350. // "setting received flag to FALSE %d %d\n",
  2351. // instance->my_aru, instance->my_high_seq_received);
  2352. instance->my_received_flg = 0;
  2353. if (instance->my_aru == instance->my_high_seq_received) {
  2354. // instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2355. // "setting received flag to TRUE %d %d\n",
  2356. // instance->my_aru, instance->my_high_seq_received);
  2357. instance->my_received_flg = 1;
  2358. }
  2359. }
  2360. static void orf_token_mcast_worker_fn (void *thread_state, void *work_item)
  2361. {
  2362. struct mcast_worker_fn_work_item *mcast_worker_fn_work_item =
  2363. (struct mcast_worker_fn_work_item *)work_item;
  2364. struct sort_queue_item *sort_queue_item = mcast_worker_fn_work_item->sort_queue_item;
  2365. struct orf_token_mcast_thread_state *orf_token_mcast_thread_state =
  2366. (struct orf_token_mcast_thread_state *)thread_state;
  2367. struct totemsrp_instance *instance = mcast_worker_fn_work_item->instance;
  2368. struct msghdr msg_mcast;
  2369. int res = 0;
  2370. int buf_len;
  2371. struct iovec iov;
  2372. /*
  2373. * Encrypt and digest the message
  2374. */
  2375. encrypt_and_sign_worker (
  2376. instance,
  2377. orf_token_mcast_thread_state->iobuf, &buf_len,
  2378. sort_queue_item->iovec, sort_queue_item->iov_len,
  2379. &orf_token_mcast_thread_state->prng_state);
  2380. iov.iov_base = orf_token_mcast_thread_state->iobuf;
  2381. iov.iov_len = buf_len;
  2382. /*
  2383. * Build multicast message
  2384. */
  2385. msg_mcast.msg_name = &instance->sockaddr_in_mcast;
  2386. msg_mcast.msg_namelen = sizeof (struct sockaddr_in);
  2387. msg_mcast.msg_iov = &iov;
  2388. msg_mcast.msg_iovlen = 1;
  2389. msg_mcast.msg_control = 0;
  2390. msg_mcast.msg_controllen = 0;
  2391. msg_mcast.msg_flags = 0;
  2392. /*
  2393. * Multicast message
  2394. * An error here is recovered by the multicast algorithm
  2395. */
  2396. res = sendmsg (instance->totemsrp_sockets[0].mcast, &msg_mcast, MSG_NOSIGNAL | MSG_DONTWAIT);
  2397. instance->iov_encrypted.iov_len = PACKET_SIZE_MAX;
  2398. if (res > 0) {
  2399. instance->stats_sent += res;
  2400. }
  2401. }
  2402. /*
  2403. * Multicasts pending messages onto the ring (requires orf_token possession)
  2404. */
  2405. static int orf_token_mcast (
  2406. struct totemsrp_instance *instance,
  2407. struct orf_token *token,
  2408. int fcc_mcasts_allowed,
  2409. struct sockaddr_in *system_from)
  2410. {
  2411. struct mcast_worker_fn_work_item work_item;
  2412. struct message_item *message_item = 0;
  2413. struct queue *mcast_queue;
  2414. struct sq *sort_queue;
  2415. struct sort_queue_item sort_queue_item;
  2416. struct sort_queue_item *sort_queue_item_ptr;
  2417. struct mcast *mcast;
  2418. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2419. mcast_queue = &instance->retrans_message_queue;
  2420. sort_queue = &instance->recovery_sort_queue;
  2421. reset_token_retransmit_timeout (instance); // REVIEWED
  2422. } else {
  2423. mcast_queue = &instance->new_message_queue;
  2424. sort_queue = &instance->regular_sort_queue;
  2425. }
  2426. for (instance->fcc_mcast_current = 0; instance->fcc_mcast_current < fcc_mcasts_allowed; instance->fcc_mcast_current++) {
  2427. if (queue_is_empty (mcast_queue)) {
  2428. break;
  2429. }
  2430. message_item = (struct message_item *)queue_item_get (mcast_queue);
  2431. /* preincrement required by algo */
  2432. if (instance->old_ring_state_saved &&
  2433. (instance->memb_state == MEMB_STATE_GATHER ||
  2434. instance->memb_state == MEMB_STATE_COMMIT)) {
  2435. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2436. "not multicasting at seqno is %d\n",
  2437. token->seq);
  2438. return (0);
  2439. }
  2440. message_item->mcast->seq = ++token->seq;
  2441. message_item->mcast->this_seqno = instance->global_seqno++;
  2442. /*
  2443. * Build IO vector
  2444. */
  2445. memset (&sort_queue_item, 0, sizeof (struct sort_queue_item));
  2446. sort_queue_item.iovec[0].iov_base = message_item->mcast;
  2447. sort_queue_item.iovec[0].iov_len = sizeof (struct mcast);
  2448. mcast = sort_queue_item.iovec[0].iov_base;
  2449. memcpy (&sort_queue_item.iovec[1], message_item->iovec,
  2450. message_item->iov_len * sizeof (struct iovec));
  2451. memcpy (&mcast->ring_id, &instance->my_ring_id, sizeof (struct memb_ring_id));
  2452. sort_queue_item.iov_len = message_item->iov_len + 1;
  2453. assert (sort_queue_item.iov_len < 16);
  2454. /*
  2455. * Add message to retransmit queue
  2456. */
  2457. sort_queue_item_ptr = sq_item_add (sort_queue,
  2458. &sort_queue_item, message_item->mcast->seq);
  2459. // XXX printf ("ORIG [%s.%d-%d]\n", inet_ntoa (message_item->mcast->source),
  2460. // XXX message_item->mcast->seq, message_item->mcast->this_seqno);
  2461. /*
  2462. * Only encrypt and send on wire if we are single processor
  2463. */
  2464. #define CONFIG_THREADED_SEND
  2465. #ifdef CONFIG_THREADED_SEND
  2466. work_item.sort_queue_item = sort_queue_item_ptr;
  2467. work_item.instance = instance;
  2468. /*
  2469. * If threads, add message to worker thread queue
  2470. */
  2471. worker_thread_group_work_add (&instance->worker_thread_group_orf_token_mcast,
  2472. &work_item);
  2473. #else
  2474. /*
  2475. * If no threads, execute worker thread directly
  2476. */
  2477. // orf_token_mcast_worker_fn (&worker_data);
  2478. #endif
  2479. /*
  2480. * Delete item from pending queue
  2481. */
  2482. queue_item_remove (mcast_queue);
  2483. }
  2484. assert (instance->fcc_mcast_current < 100);
  2485. /*
  2486. * If messages mcasted, deliver any new messages to totemg
  2487. */
  2488. instance->my_high_seq_received = token->seq;
  2489. update_aru (instance);
  2490. /*
  2491. * Return 1 if more messages are available for single node clusters
  2492. */
  2493. return (instance->fcc_mcast_current);
  2494. }
  2495. /*
  2496. * Remulticasts messages in orf_token's retransmit list (requires orf_token)
  2497. * Modify's orf_token's rtr to include retransmits required by this process
  2498. */
  2499. static int orf_token_rtr (
  2500. struct totemsrp_instance *instance,
  2501. struct orf_token *orf_token,
  2502. int *fcc_allowed)
  2503. {
  2504. int res;
  2505. int i, j;
  2506. int found;
  2507. int total_entries;
  2508. struct sq *sort_queue;
  2509. struct rtr_item *rtr_list;
  2510. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2511. sort_queue = &instance->recovery_sort_queue;
  2512. } else {
  2513. sort_queue = &instance->regular_sort_queue;
  2514. }
  2515. rtr_list = &orf_token->rtr_list[0];
  2516. if (orf_token->rtr_list_entries) {
  2517. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2518. "Retransmit List %d\n", orf_token->rtr_list_entries);
  2519. for (i = 0; i < orf_token->rtr_list_entries; i++) {
  2520. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2521. "%d ", rtr_list[i].seq);
  2522. }
  2523. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug, "\n");
  2524. }
  2525. total_entries = orf_token->rtr_list_entries;
  2526. /*
  2527. * Retransmit messages on orf_token's RTR list from RTR queue
  2528. */
  2529. for (instance->fcc_remcast_current = 0, i = 0;
  2530. instance->fcc_remcast_current <= *fcc_allowed && i < orf_token->rtr_list_entries;) {
  2531. /*
  2532. * If this retransmit request isn't from this configuration,
  2533. * try next rtr entry
  2534. */
  2535. if (memcmp (&rtr_list[i].ring_id, &instance->my_ring_id,
  2536. sizeof (struct memb_ring_id)) != 0) {
  2537. i += 1;
  2538. continue;
  2539. }
  2540. assert (rtr_list[i].seq > 0);
  2541. res = orf_token_remcast (instance, rtr_list[i].seq);
  2542. if (res == 0) {
  2543. /*
  2544. * Multicasted message, so no need to copy to new retransmit list
  2545. */
  2546. orf_token->rtr_list_entries -= 1;
  2547. assert (orf_token->rtr_list_entries >= 0);
  2548. memmove (&rtr_list[i], &rtr_list[i + 1],
  2549. sizeof (struct rtr_item) * (orf_token->rtr_list_entries));
  2550. instance->fcc_remcast_current++;
  2551. instance->stats_remcasts++;
  2552. } else {
  2553. i += 1;
  2554. }
  2555. }
  2556. *fcc_allowed = *fcc_allowed - instance->fcc_remcast_current - 1;
  2557. #ifdef COMPILE_OUT
  2558. for (i = 0; i < orf_token->rtr_list_entries; i++) {
  2559. assert (rtr_list_old[index_old].seq != -1);
  2560. }
  2561. #endif
  2562. /*
  2563. * Add messages to retransmit to RTR list
  2564. * but only retry if there is room in the retransmit list
  2565. */
  2566. for (i = instance->my_aru + 1;
  2567. orf_token->rtr_list_entries < RETRANSMIT_ENTRIES_MAX &&
  2568. i <= instance->my_high_seq_received;
  2569. i++) {
  2570. /*
  2571. * Find if a message is missing from this processor
  2572. */
  2573. res = sq_item_inuse (sort_queue, i);
  2574. if (res == 0) {
  2575. /*
  2576. * Determine if missing message is already in retransmit list
  2577. */
  2578. found = 0;
  2579. for (j = 0; j < orf_token->rtr_list_entries; j++) {
  2580. if (i == rtr_list[j].seq) {
  2581. found = 1;
  2582. }
  2583. }
  2584. if (found == 0) {
  2585. /*
  2586. * Missing message not found in current retransmit list so add it
  2587. */
  2588. memcpy (&rtr_list[orf_token->rtr_list_entries].ring_id,
  2589. &instance->my_ring_id, sizeof (struct memb_ring_id));
  2590. rtr_list[orf_token->rtr_list_entries].seq = i;
  2591. orf_token->rtr_list_entries++;
  2592. }
  2593. }
  2594. }
  2595. return (instance->fcc_remcast_current);
  2596. }
  2597. static void token_retransmit (struct totemsrp_instance *instance) {
  2598. struct iovec iovec;
  2599. struct msghdr msg_orf_token;
  2600. int res;
  2601. iovec.iov_base = instance->orf_token_retransmit;
  2602. iovec.iov_len = instance->orf_token_retransmit_size;
  2603. msg_orf_token.msg_name = &instance->next_memb;
  2604. msg_orf_token.msg_namelen = sizeof (struct sockaddr_in);
  2605. msg_orf_token.msg_iov = &iovec;
  2606. msg_orf_token.msg_iovlen = 1;
  2607. msg_orf_token.msg_control = 0;
  2608. msg_orf_token.msg_controllen = 0;
  2609. msg_orf_token.msg_flags = 0;
  2610. res = sendmsg (instance->totemsrp_sockets[0].token, &msg_orf_token, MSG_NOSIGNAL);
  2611. }
  2612. /*
  2613. * Retransmit the regular token if no mcast or token has
  2614. * been received in retransmit token period retransmit
  2615. * the token to the next processor
  2616. */
  2617. static void timer_function_token_retransmit_timeout (void *data)
  2618. {
  2619. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  2620. switch (instance->memb_state) {
  2621. case MEMB_STATE_GATHER:
  2622. break;
  2623. case MEMB_STATE_COMMIT:
  2624. break;
  2625. case MEMB_STATE_OPERATIONAL:
  2626. case MEMB_STATE_RECOVERY:
  2627. token_retransmit (instance);
  2628. reset_token_retransmit_timeout (instance); // REVIEWED
  2629. break;
  2630. }
  2631. }
  2632. static void timer_function_token_hold_retransmit_timeout (void *data)
  2633. {
  2634. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  2635. switch (instance->memb_state) {
  2636. case MEMB_STATE_GATHER:
  2637. break;
  2638. case MEMB_STATE_COMMIT:
  2639. break;
  2640. case MEMB_STATE_OPERATIONAL:
  2641. case MEMB_STATE_RECOVERY:
  2642. token_retransmit (instance);
  2643. break;
  2644. }
  2645. }
  2646. static void timer_function_merge_detect_timeout(void *data)
  2647. {
  2648. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  2649. instance->my_merge_detect_timeout_outstanding = 0;
  2650. switch (instance->memb_state) {
  2651. case MEMB_STATE_OPERATIONAL:
  2652. if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr) {
  2653. memb_merge_detect_transmit (instance);
  2654. }
  2655. break;
  2656. case MEMB_STATE_GATHER:
  2657. case MEMB_STATE_COMMIT:
  2658. case MEMB_STATE_RECOVERY:
  2659. break;
  2660. }
  2661. }
  2662. /*
  2663. * Send orf_token to next member (requires orf_token)
  2664. */
  2665. static int token_send (
  2666. struct totemsrp_instance *instance,
  2667. struct orf_token *orf_token,
  2668. int forward_token)
  2669. {
  2670. struct msghdr msg_orf_token;
  2671. struct iovec iovec;
  2672. int res;
  2673. iovec.iov_base = (char *)orf_token;
  2674. iovec.iov_len = sizeof (struct orf_token) +
  2675. (orf_token->rtr_list_entries * sizeof (struct rtr_item));
  2676. encrypt_and_sign (instance, &iovec, 1);
  2677. /*
  2678. * Keep an encrypted copy in case the token retransmit timer expires
  2679. */
  2680. memcpy (instance->orf_token_retransmit, instance->iov_encrypted.iov_base, instance->iov_encrypted.iov_len);
  2681. instance->orf_token_retransmit_size = instance->iov_encrypted.iov_len;
  2682. /*
  2683. * IF the user doesn't want the token forwarded, then dont send
  2684. * it but keep an encrypted copy for the retransmit timeout
  2685. */
  2686. if (forward_token == 0) {
  2687. return (0);
  2688. }
  2689. /*
  2690. * Send the message
  2691. */
  2692. msg_orf_token.msg_name = &instance->next_memb;
  2693. msg_orf_token.msg_namelen = sizeof (struct sockaddr_in);
  2694. msg_orf_token.msg_iov = &instance->iov_encrypted;
  2695. msg_orf_token.msg_iovlen = 1;
  2696. msg_orf_token.msg_control = 0;
  2697. msg_orf_token.msg_controllen = 0;
  2698. msg_orf_token.msg_flags = 0;
  2699. res = sendmsg (instance->totemsrp_sockets[0].token, &msg_orf_token, MSG_NOSIGNAL);
  2700. if (res == -1) {
  2701. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2702. "Couldn't send token to addr %s %s %d\n",
  2703. inet_ntoa (instance->next_memb.sin_addr),
  2704. strerror (errno), instance->totemsrp_sockets[0].token);
  2705. }
  2706. /*
  2707. * res not used here errors are handled by algorithm
  2708. */
  2709. if (res > 0) {
  2710. instance->stats_sent += res;
  2711. }
  2712. return (res);
  2713. }
  2714. static int token_hold_cancel_send (struct totemsrp_instance *instance)
  2715. {
  2716. struct token_hold_cancel token_hold_cancel;
  2717. struct iovec iov;
  2718. struct msghdr msghdr;
  2719. /*
  2720. * Only cancel if the token is currently held
  2721. */
  2722. if (instance->my_token_held == 0) {
  2723. return (0);
  2724. }
  2725. instance->my_token_held = 0;
  2726. /*
  2727. * Build message
  2728. */
  2729. token_hold_cancel.header.type = MESSAGE_TYPE_TOKEN_HOLD_CANCEL;
  2730. token_hold_cancel.header.endian_detector = ENDIAN_LOCAL;
  2731. memcpy (&token_hold_cancel.ring_id, &instance->my_ring_id,
  2732. sizeof (struct memb_ring_id));
  2733. iov.iov_base = &token_hold_cancel;
  2734. iov.iov_len = sizeof (struct token_hold_cancel);
  2735. encrypt_and_sign (instance, &iov, 1);
  2736. /*
  2737. * Build multicast message
  2738. */
  2739. msghdr.msg_name = (caddr_t)&instance->sockaddr_in_mcast;
  2740. msghdr.msg_namelen = sizeof (struct sockaddr_in);
  2741. msghdr.msg_iov = &instance->iov_encrypted;
  2742. msghdr.msg_iovlen = 1;
  2743. msghdr.msg_control = 0;
  2744. msghdr.msg_controllen = 0;
  2745. msghdr.msg_flags = 0;
  2746. /*
  2747. * Multicast message
  2748. */
  2749. sendmsg (instance->totemsrp_sockets[0].mcast, &msghdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  2750. return (0);
  2751. }
  2752. static int orf_token_send_initial (struct totemsrp_instance *instance)
  2753. {
  2754. struct orf_token orf_token;
  2755. int res;
  2756. orf_token.header.type = MESSAGE_TYPE_ORF_TOKEN;
  2757. orf_token.header.endian_detector = ENDIAN_LOCAL;
  2758. orf_token.header.encapsulated = 0;
  2759. orf_token.seq = 0;
  2760. orf_token.token_seq = 0;
  2761. orf_token.retrans_flg = 1;
  2762. instance->my_set_retrans_flg = 1;
  2763. /*
  2764. if (queue_is_empty (&instance->retrans_message_queue) == 1) {
  2765. orf_token.retrans_flg = 0;
  2766. } else {
  2767. orf_token.retrans_flg = 1;
  2768. instance->my_set_retrans_flg = 1;
  2769. }
  2770. */
  2771. orf_token.aru = 0;
  2772. // orf_token.aru_addr.s_addr = 0;//instance->my_id.sin_addr.s_addr;
  2773. orf_token.aru_addr.s_addr = instance->my_id.sin_addr.s_addr;
  2774. memcpy (&orf_token.ring_id, &instance->my_ring_id, sizeof (struct memb_ring_id));
  2775. orf_token.fcc = 0;
  2776. orf_token.rtr_list_entries = 0;
  2777. res = token_send (instance, &orf_token, 1);
  2778. return (res);
  2779. }
  2780. static void memb_state_commit_token_update (
  2781. struct totemsrp_instance *instance,
  2782. struct memb_commit_token *memb_commit_token)
  2783. {
  2784. int memb_index_this;
  2785. memb_index_this = (memb_commit_token->memb_index + 1) % memb_commit_token->addr_entries;
  2786. memcpy (&memb_commit_token->memb_list[memb_index_this].ring_id,
  2787. &instance->my_old_ring_id, sizeof (struct memb_ring_id));
  2788. assert (instance->my_old_ring_id.rep.s_addr != 0);
  2789. memb_commit_token->memb_list[memb_index_this].aru = instance->old_ring_state_aru;
  2790. /*
  2791. * TODO high delivered is really instance->my_aru, but with safe this
  2792. * could change?
  2793. */
  2794. memb_commit_token->memb_list[memb_index_this].high_delivered = instance->my_high_delivered;
  2795. memb_commit_token->memb_list[memb_index_this].received_flg = instance->my_received_flg;
  2796. }
  2797. static int memb_state_commit_token_send (struct totemsrp_instance *instance,
  2798. struct memb_commit_token *memb_commit_token)
  2799. {
  2800. struct msghdr msghdr;
  2801. struct iovec iovec;
  2802. int res;
  2803. int memb_index_this;
  2804. int memb_index_next;
  2805. memb_commit_token->token_seq++;
  2806. memb_index_this = (memb_commit_token->memb_index + 1) % memb_commit_token->addr_entries;
  2807. memb_index_next = (memb_index_this + 1) % memb_commit_token->addr_entries;
  2808. memb_commit_token->memb_index = memb_index_this;
  2809. iovec.iov_base = memb_commit_token;
  2810. iovec.iov_len = sizeof (struct memb_commit_token);
  2811. encrypt_and_sign (instance, &iovec, 1);
  2812. instance->next_memb.sin_addr.s_addr = memb_commit_token->addr[memb_index_next].s_addr;
  2813. instance->next_memb.sin_family = AF_INET;
  2814. instance->next_memb.sin_port = instance->sockaddr_in_mcast.sin_port;
  2815. msghdr.msg_name = &instance->next_memb;
  2816. msghdr.msg_namelen = sizeof (struct sockaddr_in);
  2817. msghdr.msg_iov = &instance->iov_encrypted;
  2818. msghdr.msg_iovlen = 1;
  2819. msghdr.msg_control = 0;
  2820. msghdr.msg_controllen = 0;
  2821. msghdr.msg_flags = 0;
  2822. res = sendmsg (instance->totemsrp_sockets[0].token, &msghdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  2823. return (res);
  2824. }
  2825. static int memb_lowest_in_config (struct totemsrp_instance *instance)
  2826. {
  2827. struct in_addr token_memb[PROCESSOR_COUNT_MAX];
  2828. int token_memb_entries = 0;
  2829. struct in_addr lowest_addr;
  2830. int i;
  2831. lowest_addr.s_addr = 0xFFFFFFFF;
  2832. memb_set_subtract (token_memb, &token_memb_entries,
  2833. instance->my_proc_list, instance->my_proc_list_entries,
  2834. instance->my_failed_list, instance->my_failed_list_entries);
  2835. /*
  2836. * find representative by searching for smallest identifier
  2837. */
  2838. for (i = 0; i < token_memb_entries; i++) {
  2839. if (lowest_addr.s_addr > token_memb[i].s_addr) {
  2840. lowest_addr.s_addr = token_memb[i].s_addr;
  2841. }
  2842. }
  2843. return (instance->my_id.sin_addr.s_addr == lowest_addr.s_addr);
  2844. }
  2845. static void memb_state_commit_token_create (
  2846. struct totemsrp_instance *instance,
  2847. struct memb_commit_token *commit_token)
  2848. {
  2849. struct in_addr token_memb[PROCESSOR_COUNT_MAX];
  2850. int token_memb_entries = 0;
  2851. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2852. "Creating commit token because I am the rep.\n");
  2853. memb_set_subtract (token_memb, &token_memb_entries,
  2854. instance->my_proc_list, instance->my_proc_list_entries,
  2855. instance->my_failed_list, instance->my_failed_list_entries);
  2856. memset (commit_token, 0, sizeof (struct memb_commit_token));
  2857. commit_token->header.type = MESSAGE_TYPE_MEMB_COMMIT_TOKEN;
  2858. commit_token->header.endian_detector = ENDIAN_LOCAL;
  2859. commit_token->header.encapsulated = 0;
  2860. commit_token->ring_id.rep.s_addr = instance->my_id.sin_addr.s_addr;
  2861. commit_token->ring_id.seq = instance->token_ring_id_seq + 4;
  2862. qsort (token_memb, token_memb_entries,
  2863. sizeof (struct in_addr), in_addr_compare);
  2864. memcpy (commit_token->addr, token_memb,
  2865. token_memb_entries * sizeof (struct in_addr));
  2866. memset (commit_token->memb_list, 0,
  2867. sizeof (struct memb_commit_token_memb_entry) * PROCESSOR_COUNT_MAX);
  2868. commit_token->memb_index = token_memb_entries - 1;
  2869. commit_token->addr_entries = token_memb_entries;
  2870. }
  2871. static int memb_join_message_send (struct totemsrp_instance *instance)
  2872. {
  2873. struct msghdr msghdr;
  2874. struct iovec iovec;
  2875. struct memb_join memb_join;
  2876. int res;
  2877. memb_join.header.type = MESSAGE_TYPE_MEMB_JOIN;
  2878. memb_join.header.endian_detector = ENDIAN_LOCAL;
  2879. memb_join.header.encapsulated = 0;
  2880. memb_join.ring_seq = instance->my_ring_id.seq;
  2881. memcpy (memb_join.proc_list, instance->my_proc_list,
  2882. instance->my_proc_list_entries * sizeof (struct in_addr));
  2883. memb_join.proc_list_entries = instance->my_proc_list_entries;
  2884. memcpy (memb_join.failed_list, instance->my_failed_list,
  2885. instance->my_failed_list_entries * sizeof (struct in_addr));
  2886. memb_join.failed_list_entries = instance->my_failed_list_entries;
  2887. iovec.iov_base = &memb_join;
  2888. iovec.iov_len = sizeof (struct memb_join);
  2889. encrypt_and_sign (instance, &iovec, 1);
  2890. msghdr.msg_name = &instance->sockaddr_in_mcast;
  2891. msghdr.msg_namelen = sizeof (struct sockaddr_in);
  2892. msghdr.msg_iov = &instance->iov_encrypted;
  2893. msghdr.msg_iovlen = 1;
  2894. msghdr.msg_control = 0;
  2895. msghdr.msg_controllen = 0;
  2896. msghdr.msg_flags = 0;
  2897. res = sendmsg (instance->totemsrp_sockets[0].mcast, &msghdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  2898. return (res);
  2899. }
  2900. static int memb_merge_detect_transmit (struct totemsrp_instance *instance)
  2901. {
  2902. struct msghdr msghdr;
  2903. struct iovec iovec;
  2904. struct memb_merge_detect memb_merge_detect;
  2905. int res;
  2906. memb_merge_detect.header.type = MESSAGE_TYPE_MEMB_MERGE_DETECT;
  2907. memb_merge_detect.header.endian_detector = ENDIAN_LOCAL;
  2908. memb_merge_detect.header.encapsulated = 0;
  2909. memcpy (&memb_merge_detect.ring_id, &instance->my_ring_id,
  2910. sizeof (struct memb_ring_id));
  2911. iovec.iov_base = &memb_merge_detect;
  2912. iovec.iov_len = sizeof (struct memb_merge_detect);
  2913. encrypt_and_sign (instance, &iovec, 1);
  2914. msghdr.msg_name = &instance->sockaddr_in_mcast;
  2915. msghdr.msg_namelen = sizeof (struct sockaddr_in);
  2916. msghdr.msg_iov = &instance->iov_encrypted;
  2917. msghdr.msg_iovlen = 1;
  2918. msghdr.msg_control = 0;
  2919. msghdr.msg_controllen = 0;
  2920. msghdr.msg_flags = 0;
  2921. res = sendmsg (instance->totemsrp_sockets[0].mcast, &msghdr,
  2922. MSG_NOSIGNAL | MSG_DONTWAIT);
  2923. return (res);
  2924. }
  2925. static void memb_ring_id_create_or_load (
  2926. struct totemsrp_instance *instance,
  2927. struct memb_ring_id *memb_ring_id)
  2928. {
  2929. int fd;
  2930. int res;
  2931. char filename[256];
  2932. sprintf (filename, "/tmp/ringid_%s",
  2933. inet_ntoa (instance->my_id.sin_addr));
  2934. fd = open (filename, O_RDONLY, 0777);
  2935. if (fd > 0) {
  2936. res = read (fd, &memb_ring_id->seq, sizeof (unsigned long long));
  2937. assert (res == sizeof (unsigned long long));
  2938. close (fd);
  2939. } else
  2940. if (fd == -1 && errno == ENOENT) {
  2941. memb_ring_id->seq = 0;
  2942. umask(0);
  2943. fd = open (filename, O_CREAT|O_RDWR, 0777);
  2944. if (fd == -1) {
  2945. printf ("couldn't create file %d %s\n", fd, strerror(errno));
  2946. }
  2947. res = write (fd, &memb_ring_id->seq, sizeof (unsigned long long));
  2948. assert (res == sizeof (unsigned long long));
  2949. close (fd);
  2950. } else {
  2951. instance->totemsrp_log_printf (instance->totemsrp_log_level_warning,
  2952. "Couldn't open %s %s\n", filename, strerror (errno));
  2953. }
  2954. memb_ring_id->rep.s_addr = instance->my_id.sin_addr.s_addr;
  2955. assert (memb_ring_id->rep.s_addr);
  2956. instance->token_ring_id_seq = memb_ring_id->seq;
  2957. }
  2958. static void memb_ring_id_store (
  2959. struct totemsrp_instance *instance,
  2960. struct memb_commit_token *commit_token)
  2961. {
  2962. char filename[256];
  2963. int fd;
  2964. int res;
  2965. sprintf (filename, "/tmp/ringid_%s",
  2966. inet_ntoa (instance->my_id.sin_addr));
  2967. fd = open (filename, O_WRONLY, 0777);
  2968. if (fd == -1) {
  2969. fd = open (filename, O_CREAT|O_RDWR, 0777);
  2970. }
  2971. if (fd == -1) {
  2972. instance->totemsrp_log_printf (instance->totemsrp_log_level_warning,
  2973. "Couldn't store new ring id %llx to stable storage (%s)\n",
  2974. commit_token->ring_id.seq, strerror (errno));
  2975. assert (0);
  2976. return;
  2977. }
  2978. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2979. "Storing new sequence id for ring %d\n", commit_token->ring_id.seq);
  2980. assert (fd > 0);
  2981. res = write (fd, &commit_token->ring_id.seq, sizeof (unsigned long long));
  2982. assert (res == sizeof (unsigned long long));
  2983. close (fd);
  2984. memcpy (&instance->my_ring_id, &commit_token->ring_id, sizeof (struct memb_ring_id));
  2985. instance->token_ring_id_seq = instance->my_ring_id.seq;
  2986. }
  2987. void print_stats (totemsrp_handle handle)
  2988. {
  2989. struct timeval tv_end;
  2990. struct totemsrp_instance *instance;
  2991. SaAisErrorT error;
  2992. gettimeofday (&tv_end, NULL);
  2993. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  2994. (void *)&instance);
  2995. if (error != SA_OK) {
  2996. return;
  2997. }
  2998. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Bytes recv %d\n", instance->stats_recv);
  2999. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Bytes sent %d\n", instance->stats_sent);
  3000. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Messages delivered %d\n", instance->stats_delv);
  3001. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Re-Mcasts %d\n", instance->stats_remcasts);
  3002. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Tokens process %d\n", instance->stats_orf_token);
  3003. saHandleInstancePut (&totemsrp_instance_database, handle);
  3004. }
  3005. int totemsrp_callback_token_create (
  3006. totemsrp_handle handle,
  3007. void **handle_out,
  3008. enum totem_callback_token_type type,
  3009. int delete,
  3010. int (*callback_fn) (enum totem_callback_token_type type, void *),
  3011. void *data)
  3012. {
  3013. struct token_callback_instance *callback_handle;
  3014. struct totemsrp_instance *instance;
  3015. SaErrorT error;
  3016. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  3017. (void *)&instance);
  3018. if (error != SA_OK) {
  3019. goto error_exit;
  3020. }
  3021. callback_handle = (struct token_callback_instance *)malloc (sizeof (struct token_callback_instance));
  3022. if (callback_handle == 0) {
  3023. return (-1);
  3024. }
  3025. *handle_out = (void *)callback_handle;
  3026. list_init (&callback_handle->list);
  3027. callback_handle->callback_fn = callback_fn;
  3028. callback_handle->data = data;
  3029. callback_handle->callback_type = type;
  3030. callback_handle->delete = delete;
  3031. switch (type) {
  3032. case TOTEM_CALLBACK_TOKEN_RECEIVED:
  3033. list_add (&callback_handle->list, &instance->token_callback_received_listhead);
  3034. break;
  3035. case TOTEM_CALLBACK_TOKEN_SENT:
  3036. list_add (&callback_handle->list, &instance->token_callback_sent_listhead);
  3037. break;
  3038. }
  3039. saHandleInstancePut (&totemsrp_instance_database, handle);
  3040. error_exit:
  3041. return (0);
  3042. }
  3043. void totemsrp_callback_token_destroy (totemsrp_handle handle, void **handle_out)
  3044. {
  3045. struct token_callback_instance *h;
  3046. if (*handle_out) {
  3047. h = (struct token_callback_instance *)*handle_out;
  3048. list_del (&h->list);
  3049. free (h);
  3050. h = NULL;
  3051. *handle_out = 0;
  3052. }
  3053. }
  3054. void totem_callback_token_type (struct totemsrp_instance *instance, void *handle)
  3055. {
  3056. struct token_callback_instance *token_callback_instance = (struct token_callback_instance *)handle;
  3057. list_del (&token_callback_instance->list);
  3058. free (token_callback_instance);
  3059. }
  3060. static void token_callbacks_execute (
  3061. struct totemsrp_instance *instance,
  3062. enum totem_callback_token_type type)
  3063. {
  3064. struct list_head *list;
  3065. struct list_head *list_next;
  3066. struct list_head *callback_listhead = 0;
  3067. struct token_callback_instance *token_callback_instance;
  3068. int res;
  3069. int del;
  3070. switch (type) {
  3071. case TOTEM_CALLBACK_TOKEN_RECEIVED:
  3072. callback_listhead = &instance->token_callback_received_listhead;
  3073. break;
  3074. case TOTEM_CALLBACK_TOKEN_SENT:
  3075. callback_listhead = &instance->token_callback_sent_listhead;
  3076. break;
  3077. default:
  3078. assert (0);
  3079. }
  3080. for (list = callback_listhead->next; list != callback_listhead;
  3081. list = list_next) {
  3082. token_callback_instance = list_entry (list, struct token_callback_instance, list);
  3083. list_next = list->next;
  3084. del = token_callback_instance->delete;
  3085. if (del == 1) {
  3086. list_del (list);
  3087. }
  3088. res = token_callback_instance->callback_fn (
  3089. token_callback_instance->callback_type,
  3090. token_callback_instance->data);
  3091. /*
  3092. * This callback failed to execute, try it again on the next token
  3093. */
  3094. if (res == -1 && del == 1) {
  3095. list_add (list, callback_listhead);
  3096. } else if (del) {
  3097. free (token_callback_instance);
  3098. }
  3099. }
  3100. }
  3101. /*
  3102. * Message Handlers
  3103. */
  3104. /*
  3105. * message handler called when TOKEN message type received
  3106. */
  3107. static int message_handler_orf_token (
  3108. struct totemsrp_instance *instance,
  3109. struct sockaddr_in *system_from,
  3110. struct iovec *iovec,
  3111. int iov_len,
  3112. int bytes_received,
  3113. int endian_conversion_needed)
  3114. {
  3115. char token_storage[1500];
  3116. char token_convert[1500];
  3117. struct orf_token *token;
  3118. int prio = UINT_MAX;
  3119. struct pollfd ufd;
  3120. int nfds;
  3121. struct orf_token *token_ref = (struct orf_token *)iovec->iov_base;
  3122. int transmits_allowed;
  3123. int forward_token;
  3124. int mcasted;
  3125. int last_aru;
  3126. int low_water;
  3127. #ifdef GIVEINFO
  3128. struct timeval tv_current;
  3129. struct timeval tv_diff;
  3130. gettimeofday (&tv_current, NULL);
  3131. timersub (&tv_current, &tv_old, &tv_diff);
  3132. memcpy (&tv_old, &tv_current, sizeof (struct timeval));
  3133. if ((((float)tv_diff.tv_usec) / 100.0) > 5.0) {
  3134. printf ("OTHERS %0.4f ms\n", ((float)tv_diff.tv_usec) / 100.0);
  3135. }
  3136. #endif
  3137. #ifdef RANDOM_DROP
  3138. if (random () % 100 < 10) {
  3139. return (0);
  3140. }
  3141. #endif
  3142. /*
  3143. * Handle merge detection timeout
  3144. */
  3145. if (token_ref->seq == instance->my_last_seq) {
  3146. start_merge_detect_timeout (instance);
  3147. instance->my_seq_unchanged += 1;
  3148. } else {
  3149. cancel_merge_detect_timeout (instance);
  3150. cancel_token_hold_retransmit_timeout (instance);
  3151. instance->my_seq_unchanged = 0;
  3152. }
  3153. instance->my_last_seq = token_ref->seq;
  3154. assert (bytes_received >= sizeof (struct orf_token));
  3155. // assert (bytes_received == sizeof (struct orf_token) +
  3156. // (sizeof (struct rtr_item) * token_ref->rtr_list_entries);
  3157. /*
  3158. * Make copy of token and retransmit list in case we have
  3159. * to flush incoming messages from the kernel queue
  3160. */
  3161. token = (struct orf_token *)token_storage;
  3162. memcpy (token, iovec->iov_base, sizeof (struct orf_token));
  3163. memcpy (&token->rtr_list[0], iovec->iov_base + sizeof (struct orf_token),
  3164. sizeof (struct rtr_item) * RETRANSMIT_ENTRIES_MAX);
  3165. if (endian_conversion_needed) {
  3166. orf_token_endian_convert (token, (struct orf_token *)token_convert);
  3167. token = (struct orf_token *)token_convert;
  3168. }
  3169. /*
  3170. * flush incoming queue from kernel
  3171. */
  3172. do {
  3173. ufd.fd = instance->totemsrp_sockets[0].mcast;
  3174. ufd.events = POLLIN;
  3175. nfds = poll (&ufd, 1, 0);
  3176. if (nfds == 1 && ufd.revents & POLLIN) {
  3177. instance->totemsrp_iov_recv.iov_len = PACKET_SIZE_MAX;
  3178. recv_handler (0, instance->totemsrp_sockets[0].mcast,
  3179. ufd.revents, instance, &prio);
  3180. }
  3181. } while (nfds == 1);
  3182. /*
  3183. * Determine if we should hold (in reality drop) the token
  3184. */
  3185. instance->my_token_held = 0;
  3186. if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr &&
  3187. instance->my_seq_unchanged > SEQNO_UNCHANGED_CONST) {
  3188. instance->my_token_held = 1;
  3189. } else
  3190. if (instance->my_ring_id.rep.s_addr != instance->my_id.sin_addr.s_addr &&
  3191. instance->my_seq_unchanged >= SEQNO_UNCHANGED_CONST) {
  3192. instance->my_token_held = 1;
  3193. }
  3194. /*
  3195. * Hold onto token when there is no activity on ring and
  3196. * this processor is the ring rep
  3197. */
  3198. forward_token = 1;
  3199. if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr) {
  3200. if (instance->my_token_held) {
  3201. forward_token = 0;
  3202. }
  3203. }
  3204. token_callbacks_execute (instance, TOTEM_CALLBACK_TOKEN_RECEIVED);
  3205. switch (instance->memb_state) {
  3206. case MEMB_STATE_COMMIT:
  3207. /* Discard token */
  3208. break;
  3209. case MEMB_STATE_OPERATIONAL:
  3210. messages_free (instance, token->aru);
  3211. case MEMB_STATE_GATHER:
  3212. /*
  3213. * DO NOT add break, we use different free mechanism in recovery state
  3214. */
  3215. case MEMB_STATE_RECOVERY:
  3216. last_aru = instance->my_last_aru;
  3217. instance->my_last_aru = token->aru;
  3218. /*
  3219. * Discard tokens from another configuration
  3220. */
  3221. if (memcmp (&token->ring_id, &instance->my_ring_id,
  3222. sizeof (struct memb_ring_id)) != 0) {
  3223. return (0); /* discard token */
  3224. }
  3225. /*
  3226. * Discard retransmitted tokens
  3227. */
  3228. if (instance->my_token_seq >= token->token_seq) {
  3229. reset_token_retransmit_timeout (instance);
  3230. reset_token_timeout (instance);
  3231. return (0); /* discard token */
  3232. }
  3233. transmits_allowed = 30;
  3234. mcasted = orf_token_rtr (instance, token, &transmits_allowed);
  3235. if ((last_aru + MISSING_MCAST_WINDOW) < token->seq) {
  3236. transmits_allowed = 0;
  3237. }
  3238. mcasted = orf_token_mcast (instance, token, transmits_allowed, system_from);
  3239. if (instance->my_aru < token->aru ||
  3240. instance->my_id.sin_addr.s_addr == token->aru_addr.s_addr ||
  3241. token->aru_addr.s_addr == 0) {
  3242. token->aru = instance->my_aru;
  3243. if (token->aru == token->seq) {
  3244. token->aru_addr.s_addr = 0;
  3245. } else {
  3246. token->aru_addr.s_addr = instance->my_id.sin_addr.s_addr;
  3247. }
  3248. }
  3249. if (token->aru == last_aru && token->aru_addr.s_addr != 0) {
  3250. instance->my_aru_count += 1;
  3251. } else {
  3252. instance->my_aru_count = 0;
  3253. }
  3254. if (instance->my_aru_count > instance->fail_to_recv_const &&
  3255. token->aru_addr.s_addr != instance->my_id.sin_addr.s_addr) {
  3256. printf ("FAILED TO RECEIVE\n");
  3257. // TODO if we fail to receive, it may be possible to end with a gather
  3258. // state of proc == failed = 0 entries
  3259. memb_set_merge (&token->aru_addr, 1,
  3260. instance->my_failed_list,
  3261. &instance->my_failed_list_entries);
  3262. ring_state_restore (instance);
  3263. memb_state_gather_enter (instance);
  3264. } else {
  3265. instance->my_token_seq = token->token_seq;
  3266. token->token_seq += 1;
  3267. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  3268. /*
  3269. * instance->my_aru == instance->my_high_seq_received means this processor
  3270. * has recovered all messages it can recover
  3271. * (ie: its retrans queue is empty)
  3272. */
  3273. low_water = instance->my_aru;
  3274. if (low_water > last_aru) {
  3275. low_water = last_aru;
  3276. }
  3277. // TODO is this code right
  3278. if (queue_is_empty (&instance->retrans_message_queue) == 0 ||
  3279. low_water != instance->my_high_seq_received) {
  3280. if (token->retrans_flg == 0) {
  3281. token->retrans_flg = 1;
  3282. instance->my_set_retrans_flg = 1;
  3283. }
  3284. } else
  3285. if (token->retrans_flg == 1 && instance->my_set_retrans_flg) {
  3286. token->retrans_flg = 0;
  3287. }
  3288. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3289. "token retrans flag is %d my set retrans flag%d retrans queue empty %d count %d, low_water %d aru %d\n",
  3290. token->retrans_flg, instance->my_set_retrans_flg,
  3291. queue_is_empty (&instance->retrans_message_queue),
  3292. instance->my_retrans_flg_count, low_water, token->aru);
  3293. if (token->retrans_flg == 0) {
  3294. instance->my_retrans_flg_count += 1;
  3295. } else {
  3296. instance->my_retrans_flg_count = 0;
  3297. }
  3298. if (instance->my_retrans_flg_count == 2) {
  3299. instance->my_install_seq = token->seq;
  3300. }
  3301. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3302. "install seq %d aru %d high seq received %d\n",
  3303. instance->my_install_seq, instance->my_aru, instance->my_high_seq_received);
  3304. if (instance->my_retrans_flg_count >= 2 && instance->my_aru >= instance->my_install_seq && instance->my_received_flg == 0) {
  3305. instance->my_received_flg = 1;
  3306. instance->my_deliver_memb_entries = instance->my_trans_memb_entries;
  3307. memcpy (instance->my_deliver_memb_list, instance->my_trans_memb_list,
  3308. sizeof (struct in_addr) * instance->my_trans_memb_entries);
  3309. }
  3310. if (instance->my_retrans_flg_count >= 3 && token->aru >= instance->my_install_seq) {
  3311. instance->my_rotation_counter += 1;
  3312. } else {
  3313. instance->my_rotation_counter = 0;
  3314. }
  3315. if (instance->my_rotation_counter == 2) {
  3316. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3317. "retrans flag count %d token aru %d install seq %d aru %d %d\n",
  3318. instance->my_retrans_flg_count, token->aru, instance->my_install_seq,
  3319. instance->my_aru, token->seq);
  3320. memb_state_operational_enter (instance);
  3321. instance->my_rotation_counter = 0;
  3322. instance->my_retrans_flg_count = 0;
  3323. }
  3324. }
  3325. #ifdef CONFIG_THREADED_SEND
  3326. worker_thread_group_wait (&instance->worker_thread_group_orf_token_mcast);
  3327. #endif
  3328. token_send (instance, token, forward_token);
  3329. #ifdef GIVEINFO
  3330. gettimeofday (&tv_current, NULL);
  3331. timersub (&tv_current, &tv_old, &tv_diff);
  3332. memcpy (&tv_old, &tv_current, sizeof (struct timeval));
  3333. if ((((float)tv_diff.tv_usec) / 100.0) > 5.0) {
  3334. printf ("I held %0.4f ms\n", ((float)tv_diff.tv_usec) / 100.0);
  3335. }
  3336. #endif
  3337. if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
  3338. messages_deliver_to_app (instance, 0, instance->my_high_seq_received);
  3339. }
  3340. /*
  3341. * Deliver messages after token has been transmitted
  3342. * to improve performance
  3343. */
  3344. reset_token_timeout (instance); // REVIEWED
  3345. reset_token_retransmit_timeout (instance); // REVIEWED
  3346. if (instance->my_id.sin_addr.s_addr == instance->my_ring_id.rep.s_addr &&
  3347. instance->my_token_held == 1) {
  3348. start_token_hold_retransmit_timeout (instance);
  3349. }
  3350. token_callbacks_execute (instance, TOTEM_CALLBACK_TOKEN_SENT);
  3351. }
  3352. break;
  3353. }
  3354. return (0);
  3355. }
  3356. static void messages_deliver_to_app (
  3357. struct totemsrp_instance *instance,
  3358. int skip,
  3359. int end_point)
  3360. {
  3361. struct sort_queue_item *sort_queue_item_p;
  3362. int i;
  3363. int res;
  3364. struct mcast *mcast;
  3365. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3366. "Delivering %d to %d\n", instance->my_high_delivered + 1,
  3367. end_point);
  3368. /*
  3369. * Deliver messages in order from rtr queue to pending delivery queue
  3370. */
  3371. for (i = instance->my_high_delivered + 1; i <= end_point; i++) {
  3372. void *ptr;
  3373. res = sq_item_get (&instance->regular_sort_queue, i, &ptr);
  3374. if (res != 0 && skip) {
  3375. printf ("-skipping %d-\n", i);
  3376. instance->my_high_delivered = i;
  3377. continue;
  3378. }
  3379. /*
  3380. * If hole, stop assembly
  3381. */
  3382. if (res != 0) {
  3383. break;
  3384. }
  3385. sort_queue_item_p = ptr;
  3386. mcast = sort_queue_item_p->iovec[0].iov_base;
  3387. assert (mcast != (struct mcast *)0xdeadbeef);
  3388. // XXX printf ("[%s.%d-%d]\n", inet_ntoa (mcast->source),
  3389. // XXX mcast->seq, mcast->this_seqno);
  3390. /*
  3391. * Skip messages not originated in instance->my_deliver_memb
  3392. */
  3393. if (skip &&
  3394. memb_set_subset (&mcast->source,
  3395. 1,
  3396. instance->my_deliver_memb_list,
  3397. instance->my_deliver_memb_entries) == 0) {
  3398. printf ("-skipping %d - wrong ip", i);
  3399. instance->my_high_delivered = i;
  3400. continue;
  3401. }
  3402. instance->my_high_delivered = i;
  3403. /*
  3404. * Message found
  3405. */
  3406. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3407. "Delivering MCAST message with seq %d to pending delivery queue\n",
  3408. mcast->seq);
  3409. /*
  3410. * Message is locally originated multicast
  3411. */
  3412. if (sort_queue_item_p->iov_len > 1 &&
  3413. sort_queue_item_p->iovec[0].iov_len == sizeof (struct mcast)) {
  3414. instance->totemsrp_deliver_fn (
  3415. mcast->source,
  3416. &sort_queue_item_p->iovec[1],
  3417. sort_queue_item_p->iov_len - 1,
  3418. mcast->header.endian_detector != ENDIAN_LOCAL);
  3419. } else {
  3420. sort_queue_item_p->iovec[0].iov_len -= sizeof (struct mcast);
  3421. sort_queue_item_p->iovec[0].iov_base += sizeof (struct mcast);
  3422. instance->totemsrp_deliver_fn (
  3423. mcast->source,
  3424. sort_queue_item_p->iovec,
  3425. sort_queue_item_p->iov_len,
  3426. mcast->header.endian_detector != ENDIAN_LOCAL);
  3427. sort_queue_item_p->iovec[0].iov_len += sizeof (struct mcast);
  3428. sort_queue_item_p->iovec[0].iov_base -= sizeof (struct mcast);
  3429. }
  3430. instance->stats_delv += 1;
  3431. }
  3432. instance->my_received_flg = 0;
  3433. if (instance->my_aru == instance->my_high_seq_received) {
  3434. // instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3435. // "setting received flag to TRUE %d %d\n",
  3436. // instance->my_aru, instance->my_high_seq_received);
  3437. instance->my_received_flg = 1;
  3438. }
  3439. }
  3440. /*
  3441. * recv message handler called when MCAST message type received
  3442. */
  3443. static int message_handler_mcast (
  3444. struct totemsrp_instance *instance,
  3445. struct sockaddr_in *system_from,
  3446. struct iovec *iovec,
  3447. int iov_len,
  3448. int bytes_received,
  3449. int endian_conversion_needed)
  3450. {
  3451. struct sort_queue_item sort_queue_item;
  3452. struct sq *sort_queue;
  3453. struct mcast mcast_header;
  3454. if (endian_conversion_needed) {
  3455. mcast_endian_convert (iovec[0].iov_base, &mcast_header);
  3456. } else {
  3457. memcpy (&mcast_header, iovec[0].iov_base, sizeof (struct mcast));
  3458. }
  3459. if (mcast_header.header.encapsulated == 1) {
  3460. sort_queue = &instance->recovery_sort_queue;
  3461. } else {
  3462. sort_queue = &instance->regular_sort_queue;
  3463. }
  3464. assert (bytes_received < PACKET_SIZE_MAX);
  3465. #ifdef RANDOM_DROP
  3466. if (random()%100 < 50) {
  3467. return (0);
  3468. }
  3469. #endif
  3470. if (system_from->sin_addr.s_addr != instance->my_id.sin_addr.s_addr) {
  3471. cancel_token_retransmit_timeout (instance);
  3472. }
  3473. /*
  3474. * If the message is foreign execute the switch below
  3475. */
  3476. if (memcmp (&instance->my_ring_id, &mcast_header.ring_id,
  3477. sizeof (struct memb_ring_id)) != 0) {
  3478. switch (instance->memb_state) {
  3479. case MEMB_STATE_OPERATIONAL:
  3480. memb_set_merge (&system_from->sin_addr, 1,
  3481. instance->my_proc_list, &instance->my_proc_list_entries);
  3482. memb_state_gather_enter (instance);
  3483. break;
  3484. case MEMB_STATE_GATHER:
  3485. if (!memb_set_subset (&system_from->sin_addr,
  3486. 1,
  3487. instance->my_proc_list,
  3488. instance->my_proc_list_entries)) {
  3489. memb_set_merge (&system_from->sin_addr, 1,
  3490. instance->my_proc_list, &instance->my_proc_list_entries);
  3491. memb_state_gather_enter (instance);
  3492. return (0);
  3493. }
  3494. break;
  3495. case MEMB_STATE_COMMIT:
  3496. /* discard message */
  3497. break;
  3498. case MEMB_STATE_RECOVERY:
  3499. /* discard message */
  3500. break;
  3501. }
  3502. return (0);
  3503. }
  3504. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3505. "Received ringid(%s:%lld) seq %d\n",
  3506. inet_ntoa (mcast_header.ring_id.rep),
  3507. mcast_header.ring_id.seq,
  3508. mcast_header.seq);
  3509. /*
  3510. * Add mcast message to rtr queue if not already in rtr queue
  3511. * otherwise free io vectors
  3512. */
  3513. if (bytes_received > 0 && bytes_received < PACKET_SIZE_MAX &&
  3514. instance->my_aru < mcast_header.seq &&
  3515. sq_item_inuse (sort_queue, mcast_header.seq) == 0) {
  3516. /*
  3517. * Allocate new multicast memory block
  3518. */
  3519. // TODO LEAK
  3520. sort_queue_item.iovec[0].iov_base = malloc (bytes_received);
  3521. if (sort_queue_item.iovec[0].iov_base == 0) {
  3522. return (-1); /* error here is corrected by the algorithm */
  3523. }
  3524. memcpy (sort_queue_item.iovec[0].iov_base, iovec[0].iov_base,
  3525. bytes_received);
  3526. sort_queue_item.iovec[0].iov_len = bytes_received;
  3527. assert (sort_queue_item.iovec[0].iov_len > 0);
  3528. assert (sort_queue_item.iovec[0].iov_len < PACKET_SIZE_MAX);
  3529. sort_queue_item.iov_len = 1;
  3530. if (mcast_header.seq > instance->my_high_seq_received) {
  3531. instance->my_high_seq_received = mcast_header.seq;
  3532. }
  3533. sq_item_add (sort_queue, &sort_queue_item, mcast_header.seq);
  3534. }
  3535. if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
  3536. update_aru (instance);
  3537. messages_deliver_to_app (instance, 0, instance->my_high_seq_received);
  3538. }
  3539. /* TODO remove from retrans message queue for old ring in recovery state */
  3540. return (0);
  3541. }
  3542. static int message_handler_memb_merge_detect (
  3543. struct totemsrp_instance *instance,
  3544. struct sockaddr_in *system_from,
  3545. struct iovec *iovec,
  3546. int iov_len,
  3547. int bytes_received,
  3548. int endian_conversion_needed)
  3549. {
  3550. struct memb_merge_detect *memb_merge_detect = iovec[0].iov_base;
  3551. /*
  3552. * do nothing if this is a merge detect from this configuration
  3553. */
  3554. if (memcmp (&instance->my_ring_id, &memb_merge_detect->ring_id,
  3555. sizeof (struct memb_ring_id)) == 0) {
  3556. return (0);
  3557. }
  3558. /*
  3559. * Execute merge operation
  3560. */
  3561. switch (instance->memb_state) {
  3562. case MEMB_STATE_OPERATIONAL:
  3563. memb_set_merge (&system_from->sin_addr, 1,
  3564. instance->my_proc_list, &instance->my_proc_list_entries);
  3565. memb_state_gather_enter (instance);
  3566. break;
  3567. case MEMB_STATE_GATHER:
  3568. if (!memb_set_subset (&system_from->sin_addr,
  3569. 1,
  3570. instance->my_proc_list,
  3571. instance->my_proc_list_entries)) {
  3572. memb_set_merge (&system_from->sin_addr, 1,
  3573. instance->my_proc_list, &instance->my_proc_list_entries);
  3574. memb_state_gather_enter (instance);
  3575. return (0);
  3576. }
  3577. break;
  3578. case MEMB_STATE_COMMIT:
  3579. /* do nothing in commit */
  3580. break;
  3581. case MEMB_STATE_RECOVERY:
  3582. /* do nothing in recovery */
  3583. break;
  3584. }
  3585. return (0);
  3586. }
  3587. static int memb_join_process (
  3588. struct totemsrp_instance *instance,
  3589. struct memb_join *memb_join,
  3590. struct sockaddr_in *system_from)
  3591. {
  3592. struct memb_commit_token my_commit_token;
  3593. if (memb_set_equal (memb_join->proc_list,
  3594. memb_join->proc_list_entries,
  3595. instance->my_proc_list,
  3596. instance->my_proc_list_entries) &&
  3597. memb_set_equal (memb_join->failed_list,
  3598. memb_join->failed_list_entries,
  3599. instance->my_failed_list,
  3600. instance->my_failed_list_entries)) {
  3601. memb_consensus_set (instance, &system_from->sin_addr);
  3602. if (memb_consensus_agreed (instance) &&
  3603. memb_lowest_in_config (instance)) {
  3604. memb_state_commit_token_create (instance, &my_commit_token);
  3605. memb_state_commit_enter (instance, &my_commit_token);
  3606. } else {
  3607. return (0);
  3608. }
  3609. } else
  3610. if (memb_set_subset (memb_join->proc_list,
  3611. memb_join->proc_list_entries,
  3612. instance->my_proc_list,
  3613. instance->my_proc_list_entries) &&
  3614. memb_set_subset (memb_join->failed_list,
  3615. memb_join->failed_list_entries,
  3616. instance->my_failed_list,
  3617. instance->my_failed_list_entries)) {
  3618. return (0);
  3619. } else
  3620. if (memb_set_subset (&system_from->sin_addr, 1,
  3621. instance->my_failed_list, instance->my_failed_list_entries)) {
  3622. return (0);
  3623. } else {
  3624. memb_set_merge (memb_join->proc_list,
  3625. memb_join->proc_list_entries,
  3626. instance->my_proc_list, &instance->my_proc_list_entries);
  3627. if (memb_set_subset (&instance->my_id.sin_addr, 1,
  3628. memb_join->failed_list, memb_join->failed_list_entries)) {
  3629. memb_set_merge (&system_from->sin_addr, 1,
  3630. instance->my_failed_list, &instance->my_failed_list_entries);
  3631. } else {
  3632. memb_set_merge (memb_join->failed_list,
  3633. memb_join->failed_list_entries,
  3634. instance->my_failed_list, &instance->my_failed_list_entries);
  3635. }
  3636. memb_state_gather_enter (instance);
  3637. return (1); /* gather entered */
  3638. }
  3639. return (0); /* gather not entered */
  3640. }
  3641. static void memb_join_endian_convert (struct memb_join *in, struct memb_join *out)
  3642. {
  3643. int i;
  3644. out->header.type = in->header.type;
  3645. out->header.endian_detector = ENDIAN_LOCAL;
  3646. out->proc_list_entries = swab32 (in->proc_list_entries);
  3647. out->failed_list_entries = swab32 (in->failed_list_entries);
  3648. out->ring_seq = swab64 (in->ring_seq);
  3649. for (i = 0; i < out->proc_list_entries; i++) {
  3650. out->proc_list[i].s_addr = in->proc_list[i].s_addr;
  3651. }
  3652. for (i = 0; i < out->failed_list_entries; i++) {
  3653. out->failed_list[i].s_addr = in->failed_list[i].s_addr;
  3654. }
  3655. }
  3656. static void memb_commit_token_endian_convert (struct memb_commit_token *in, struct memb_commit_token *out)
  3657. {
  3658. int i;
  3659. out->header.type = in->header.type;
  3660. out->header.endian_detector = ENDIAN_LOCAL;
  3661. out->token_seq = swab32 (in->token_seq);
  3662. out->ring_id.rep.s_addr = in->ring_id.rep.s_addr;
  3663. out->ring_id.seq = swab64 (in->ring_id.seq);
  3664. out->retrans_flg = swab32 (in->retrans_flg);
  3665. out->memb_index = swab32 (in->memb_index);
  3666. out->addr_entries = swab32 (in->addr_entries);
  3667. for (i = 0; i < out->addr_entries; i++) {
  3668. out->addr[i].s_addr = in->addr[i].s_addr;
  3669. out->memb_list[i].ring_id.rep.s_addr =
  3670. in->memb_list[i].ring_id.rep.s_addr;
  3671. out->memb_list[i].ring_id.seq =
  3672. swab64 (in->memb_list[i].ring_id.seq);
  3673. out->memb_list[i].aru = swab32 (in->memb_list[i].aru);
  3674. out->memb_list[i].high_delivered = swab32 (in->memb_list[i].high_delivered);
  3675. out->memb_list[i].received_flg = swab32 (in->memb_list[i].received_flg);
  3676. }
  3677. }
  3678. static void orf_token_endian_convert (struct orf_token *in, struct orf_token *out)
  3679. {
  3680. int i;
  3681. out->header.type = in->header.type;
  3682. out->header.endian_detector = ENDIAN_LOCAL;
  3683. out->seq = swab32 (in->seq);
  3684. out->token_seq = swab32 (in->token_seq);
  3685. out->aru = swab32 (in->aru);
  3686. out->ring_id.rep.s_addr = in->ring_id.rep.s_addr;
  3687. out->ring_id.seq = swab64 (in->ring_id.seq);
  3688. out->fcc = swab32 (in->fcc);
  3689. out->retrans_flg = swab32 (in->retrans_flg);
  3690. out->rtr_list_entries = swab32 (in->rtr_list_entries);
  3691. for (i = 0; i < out->rtr_list_entries; i++) {
  3692. out->rtr_list[i].ring_id.rep.s_addr = in->rtr_list[i].ring_id.rep.s_addr;
  3693. out->rtr_list[i].ring_id.seq = swab64 (in->rtr_list[i].ring_id.seq);
  3694. out->rtr_list[i].seq = swab32 (in->rtr_list[i].seq);
  3695. }
  3696. }
  3697. static void mcast_endian_convert (struct mcast *in, struct mcast *out)
  3698. {
  3699. out->header.type = in->header.type;
  3700. out->header.endian_detector = ENDIAN_LOCAL;
  3701. out->seq = swab32 (in->seq);
  3702. out->ring_id.rep.s_addr = in->ring_id.rep.s_addr;
  3703. out->ring_id.seq = swab64 (in->ring_id.seq);
  3704. out->source = in->source;
  3705. out->guarantee = in->guarantee;
  3706. }
  3707. static int message_handler_memb_join (
  3708. struct totemsrp_instance *instance,
  3709. struct sockaddr_in *system_from,
  3710. struct iovec *iovec,
  3711. int iov_len,
  3712. int bytes_received,
  3713. int endian_conversion_needed)
  3714. {
  3715. struct memb_join *memb_join;
  3716. struct memb_join memb_join_convert;
  3717. int gather_entered;
  3718. if (endian_conversion_needed) {
  3719. memb_join = &memb_join_convert;
  3720. memb_join_endian_convert (iovec->iov_base, &memb_join_convert);
  3721. } else {
  3722. memb_join = (struct memb_join *)iovec->iov_base;
  3723. }
  3724. if (instance->token_ring_id_seq < memb_join->ring_seq) {
  3725. instance->token_ring_id_seq = memb_join->ring_seq;
  3726. }
  3727. switch (instance->memb_state) {
  3728. case MEMB_STATE_OPERATIONAL:
  3729. gather_entered = memb_join_process (instance,
  3730. memb_join, system_from);
  3731. if (gather_entered == 0) {
  3732. memb_state_gather_enter (instance);
  3733. }
  3734. break;
  3735. case MEMB_STATE_GATHER:
  3736. memb_join_process (instance, memb_join, system_from);
  3737. break;
  3738. case MEMB_STATE_COMMIT:
  3739. if (memb_set_subset (&system_from->sin_addr,
  3740. 1,
  3741. instance->my_new_memb_list,
  3742. instance->my_new_memb_entries) &&
  3743. memb_join->ring_seq >= instance->my_ring_id.seq) {
  3744. memb_join_process (instance, memb_join, system_from);
  3745. memb_state_gather_enter (instance);
  3746. }
  3747. break;
  3748. case MEMB_STATE_RECOVERY:
  3749. if (memb_set_subset (&system_from->sin_addr,
  3750. 1,
  3751. instance->my_new_memb_list,
  3752. instance->my_new_memb_entries) &&
  3753. memb_join->ring_seq >= instance->my_ring_id.seq) {
  3754. ring_state_restore (instance);
  3755. memb_join_process (instance,memb_join,
  3756. system_from);
  3757. memb_state_gather_enter (instance);
  3758. }
  3759. break;
  3760. }
  3761. return (0);
  3762. }
  3763. static int message_handler_memb_commit_token (
  3764. struct totemsrp_instance *instance,
  3765. struct sockaddr_in *system_from,
  3766. struct iovec *iovec,
  3767. int iov_len,
  3768. int bytes_received,
  3769. int endian_conversion_needed)
  3770. {
  3771. struct memb_commit_token memb_commit_token_convert;
  3772. struct memb_commit_token *memb_commit_token;
  3773. struct in_addr sub[PROCESSOR_COUNT_MAX];
  3774. int sub_entries;
  3775. if (endian_conversion_needed) {
  3776. memb_commit_token = &memb_commit_token_convert;
  3777. memb_commit_token_endian_convert (iovec->iov_base, memb_commit_token);
  3778. } else {
  3779. memb_commit_token = (struct memb_commit_token *)iovec->iov_base;
  3780. }
  3781. /* TODO do we need to check for a duplicate token?
  3782. if (memb_commit_token->token_seq > 0 &&
  3783. instance->my_token_seq >= memb_commit_token->token_seq) {
  3784. printf ("already received commit token %d %d\n",
  3785. memb_commit_token->token_seq, instance->my_token_seq);
  3786. return (0);
  3787. }
  3788. */
  3789. #ifdef RANDOM_DROP
  3790. if (random()%100 < 10) {
  3791. return (0);
  3792. }
  3793. #endif
  3794. switch (instance->memb_state) {
  3795. case MEMB_STATE_OPERATIONAL:
  3796. /* discard token */
  3797. break;
  3798. case MEMB_STATE_GATHER:
  3799. memb_set_subtract (sub, &sub_entries,
  3800. instance->my_proc_list, instance->my_proc_list_entries,
  3801. instance->my_failed_list, instance->my_failed_list_entries);
  3802. if (memb_set_equal (memb_commit_token->addr,
  3803. memb_commit_token->addr_entries,
  3804. sub,
  3805. sub_entries) &&
  3806. memb_commit_token->ring_id.seq > instance->my_ring_id.seq) {
  3807. memb_state_commit_enter (instance, memb_commit_token);
  3808. }
  3809. break;
  3810. case MEMB_STATE_COMMIT:
  3811. if (memcmp (&memb_commit_token->ring_id, &instance->my_ring_id,
  3812. sizeof (struct memb_ring_id)) == 0) {
  3813. // if (memb_commit_token->ring_id.seq == instance->my_ring_id.seq) {
  3814. memb_state_recovery_enter (instance, memb_commit_token);
  3815. }
  3816. break;
  3817. case MEMB_STATE_RECOVERY:
  3818. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  3819. "Sending initial ORF token\n");
  3820. if (instance->my_id.sin_addr.s_addr == instance->my_ring_id.rep.s_addr) {
  3821. // TODO convert instead of initiate
  3822. orf_token_send_initial (instance);
  3823. reset_token_timeout (instance); // REVIEWED
  3824. reset_token_retransmit_timeout (instance); // REVIEWED
  3825. }
  3826. break;
  3827. }
  3828. return (0);
  3829. }
  3830. static int message_handler_token_hold_cancel (
  3831. struct totemsrp_instance *instance,
  3832. struct sockaddr_in *system_from,
  3833. struct iovec *iovec,
  3834. int iov_len,
  3835. int bytes_received,
  3836. int endian_conversion_needed)
  3837. {
  3838. struct token_hold_cancel *token_hold_cancel = (struct token_hold_cancel *)iovec->iov_base;
  3839. if (memcmp (&token_hold_cancel->ring_id, &instance->my_ring_id,
  3840. sizeof (struct memb_ring_id)) == 0) {
  3841. instance->my_seq_unchanged = 0;
  3842. if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr) {
  3843. timer_function_token_retransmit_timeout (instance);
  3844. }
  3845. }
  3846. return (0);
  3847. }
  3848. static int recv_handler (
  3849. poll_handle handle,
  3850. int fd,
  3851. int revents,
  3852. void *data,
  3853. unsigned int *prio)
  3854. {
  3855. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  3856. struct msghdr msg_recv;
  3857. struct message_header *message_header;
  3858. struct sockaddr_in system_from;
  3859. int bytes_received;
  3860. int res = 0;
  3861. *prio = UINT_MAX;
  3862. /*
  3863. * Receive datagram
  3864. */
  3865. msg_recv.msg_name = &system_from;
  3866. msg_recv.msg_namelen = sizeof (struct sockaddr_in);
  3867. msg_recv.msg_iov = &instance->totemsrp_iov_recv;
  3868. msg_recv.msg_iovlen = 1;
  3869. msg_recv.msg_control = 0;
  3870. msg_recv.msg_controllen = 0;
  3871. msg_recv.msg_flags = 0;
  3872. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  3873. if (bytes_received == -1) {
  3874. return (0);
  3875. } else {
  3876. instance->stats_recv += bytes_received;
  3877. }
  3878. if (bytes_received < sizeof (struct message_header)) {
  3879. instance->totemsrp_log_printf (instance->totemsrp_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  3880. return (0);
  3881. }
  3882. message_header = (struct message_header *)msg_recv.msg_iov->iov_base;
  3883. /*
  3884. * Authenticate and if authenticated, decrypt datagram
  3885. */
  3886. instance->totemsrp_iov_recv.iov_len = bytes_received;
  3887. res = authenticate_and_decrypt (instance, &instance->totemsrp_iov_recv);
  3888. instance->log_digest = 0;
  3889. if (res == -1) {
  3890. instance->totemsrp_iov_recv.iov_len = PACKET_SIZE_MAX;
  3891. return 0;
  3892. }
  3893. if (instance->stats_tv_start.tv_usec == 0) {
  3894. gettimeofday (&instance->stats_tv_start, NULL);
  3895. }
  3896. /*
  3897. * Handle incoming message
  3898. */
  3899. message_header = (struct message_header *)msg_recv.msg_iov[0].iov_base;
  3900. totemsrp_message_handlers.handler_functions[(int)message_header->type] (
  3901. instance,
  3902. &system_from,
  3903. msg_recv.msg_iov,
  3904. msg_recv.msg_iovlen,
  3905. bytes_received,
  3906. message_header->endian_detector != ENDIAN_LOCAL);
  3907. instance->totemsrp_iov_recv.iov_len = PACKET_SIZE_MAX;
  3908. return (0);
  3909. }