totemsrp.c 114 KB

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