totemsrp.c 116 KB

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