totemsrp.c 116 KB

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