totemsrp.c 109 KB

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