totemsrp.c 110 KB

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