totemsrp.c 112 KB

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