totemsrp.c 108 KB

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