totemsrp.c 111 KB

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