totemsrp.c 99 KB

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