totemsrp.c 114 KB

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