totemsrp.c 108 KB

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