totemsrp.c 114 KB

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