totemsrp.c 114 KB

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