totemsrp.c 110 KB

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