totemsrp.c 109 KB

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