totemsrp.c 112 KB

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