totemsrp.c 109 KB

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