totemsrp.c 116 KB

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