totemsrp.c 114 KB

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