totemsrp.c 109 KB

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