totemsrp.c 101 KB

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