totemsrp.c 113 KB

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