totemsrp.c 109 KB

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