totemsrp.c 105 KB

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