totemsrp.c 105 KB

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