totemsrp.c 109 KB

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