totemsrp.c 109 KB

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