totemsrp.c 114 KB

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