totemsrp.c 108 KB

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