totemsrp.c 109 KB

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