totemsrp.c 113 KB

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