totemsrp.c 116 KB

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