totemsrp.c 108 KB

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