totemsrp.c 109 KB

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