totemsrp.c 110 KB

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