totemsrp.c 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657
  1. #define WORKER_THREAD_COUNT 2
  2. /*
  3. * Copyright (c) 2003-2005 MontaVista Software, 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 <pthread.h>
  50. #include <sys/mman.h>
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #include <sys/socket.h>
  54. #include <netdb.h>
  55. #include <sys/un.h>
  56. #include <sys/sysinfo.h>
  57. #include <sys/ioctl.h>
  58. #include <sys/param.h>
  59. #include <netinet/in.h>
  60. #include <arpa/inet.h>
  61. #include <linux/if.h>
  62. #include <linux/sockios.h>
  63. #include <unistd.h>
  64. #include <fcntl.h>
  65. #include <stdlib.h>
  66. #include <stdio.h>
  67. #include <errno.h>
  68. #include <signal.h>
  69. #include <sched.h>
  70. #include <time.h>
  71. #include <sys/time.h>
  72. #include <sys/poll.h>
  73. #include "aispoll.h"
  74. #include "totemsrp.h"
  75. #include "../include/queue.h"
  76. #include "../include/sq.h"
  77. #include "../include/list.h"
  78. #include "hdb.h"
  79. #include "swab.h"
  80. #include "crypto.h"
  81. #define AUTHENTICATION 1 /* use authentication */
  82. #define ENCRYPTION 1 /* use encryption */
  83. #define LOCALHOST_IP inet_addr("127.0.0.1")
  84. #define QUEUE_RTR_ITEMS_SIZE_MAX 2000 /* allow 512 retransmit items */
  85. #define NEW_MESSAGE_QUEUE_SIZE_MAX 2000 /* allow 500 messages to be queued */
  86. #define RETRANS_MESSAGE_QUEUE_SIZE_MAX 2000 /* allow 500 messages to be queued */
  87. #define RECEIVED_MESSAGE_QUEUE_SIZE_MAX 2000 /* allow 500 messages to be queued */
  88. #define MAXIOVS 5
  89. #define RETRANSMIT_ENTRIES_MAX 30
  90. #define MISSING_MCAST_WINDOW 128
  91. #define TIMEOUT_STATE_GATHER_JOIN 100
  92. #define TIMEOUT_STATE_GATHER_CONSENSUS 200
  93. #define TOKEN_RETRANSMITS_BEFORE_LOSS 4
  94. #define TIMEOUT_TOKEN 200
  95. #define TIMEOUT_TOKEN_RETRANSMIT (int)(TIMEOUT_TOKEN / (TOKEN_RETRANSMITS_BEFORE_LOSS + 0.2))
  96. #define TIMEOUT_TOKEN_HOLD (int)(TIMEOUT_TOKEN_RETRANSMIT * 0.8 - (1000/HZ))
  97. #define TIMEOUT_MERGE_DETECT 200
  98. #define PACKET_SIZE_MAX 2000
  99. #define FAIL_TO_RECV_CONST 250
  100. #define SEQNO_UNCHANGED_CONST 20
  101. #define TIMEOUT_DOWNCHECK 1000
  102. /*
  103. * we compare incoming messages to determine if their endian is
  104. * different - if so convert them
  105. *
  106. * do not change
  107. */
  108. #define ENDIAN_LOCAL 0xff22
  109. enum message_type {
  110. MESSAGE_TYPE_ORF_TOKEN = 0, /* Ordering, Reliability, Flow (ORF) control Token */
  111. MESSAGE_TYPE_MCAST = 1, /* ring ordered multicast message */
  112. MESSAGE_TYPE_MEMB_MERGE_DETECT = 2, /* merge rings if there are available rings */
  113. MESSAGE_TYPE_MEMB_JOIN = 3, /* membership join message */
  114. MESSAGE_TYPE_MEMB_COMMIT_TOKEN = 4, /* membership commit token */
  115. MESSAGE_TYPE_TOKEN_HOLD_CANCEL = 5, /* cancel the holding of the token */
  116. };
  117. /*
  118. * New membership algorithm local variables
  119. */
  120. struct consensus_list_item {
  121. struct in_addr addr;
  122. int set;
  123. };
  124. struct token_callback_instance {
  125. struct list_head list;
  126. int (*callback_fn) (enum totem_callback_token_type type, void *);
  127. enum totem_callback_token_type callback_type;
  128. int delete;
  129. void *data;
  130. };
  131. struct totemsrp_socket {
  132. int mcast;
  133. int token;
  134. };
  135. #define HMAC_HASH_SIZE 20
  136. struct security_header {
  137. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  138. unsigned char salt[16]; /* random number */
  139. } __attribute__((packed));
  140. struct message_header {
  141. struct security_header security_header;
  142. char type;
  143. char encapsulated;
  144. // unsigned short filler;
  145. unsigned short endian_detector;
  146. } __attribute__((packed));
  147. struct mcast {
  148. struct message_header header;
  149. int seq;
  150. int this_seqno;
  151. struct memb_ring_id ring_id;
  152. struct in_addr source;
  153. int guarantee;
  154. } __attribute__((packed));
  155. /*
  156. * MTU - multicast message header - IP header - UDP header
  157. *
  158. * On lossy switches, making use of the DF UDP flag can lead to loss of
  159. * forward progress. So the packets must be fragmented by a higher layer
  160. *
  161. * This layer can only handle packets of MTU size.
  162. */
  163. #define FRAGMENT_SIZE (PACKET_SIZE_MAX - sizeof (struct mcast) - 20 - 8)
  164. struct rtr_item {
  165. struct memb_ring_id ring_id;
  166. int seq;
  167. }__attribute__((packed));
  168. struct orf_token {
  169. struct message_header header;
  170. int seq;
  171. int token_seq;
  172. int aru;
  173. struct in_addr aru_addr;
  174. struct memb_ring_id ring_id;
  175. short int fcc;
  176. int retrans_flg;
  177. int rtr_list_entries;
  178. struct rtr_item rtr_list[0];
  179. }__attribute__((packed));
  180. struct memb_join {
  181. struct message_header header;
  182. struct in_addr proc_list[PROCESSOR_COUNT_MAX];
  183. int proc_list_entries;
  184. struct in_addr failed_list[PROCESSOR_COUNT_MAX];
  185. int failed_list_entries;
  186. unsigned long long ring_seq;
  187. } __attribute__((packed));
  188. struct memb_merge_detect {
  189. struct message_header header;
  190. struct memb_ring_id ring_id;
  191. } __attribute__((packed));
  192. struct token_hold_cancel {
  193. struct message_header header;
  194. struct memb_ring_id ring_id;
  195. } __attribute__((packed));
  196. struct memb_commit_token_memb_entry {
  197. struct memb_ring_id ring_id;
  198. int aru;
  199. int high_delivered;
  200. int received_flg;
  201. }__attribute__((packed));
  202. struct memb_commit_token {
  203. struct message_header header;
  204. int token_seq;
  205. struct memb_ring_id ring_id;
  206. unsigned int retrans_flg;
  207. int memb_index;
  208. int addr_entries;
  209. struct in_addr addr[PROCESSOR_COUNT_MAX];
  210. struct memb_commit_token_memb_entry memb_list[PROCESSOR_COUNT_MAX];
  211. }__attribute__((packed));
  212. struct worker_thread_group {
  213. int threadcount;
  214. int last_scheduled;
  215. struct worker_thread *threads;
  216. void (*worker_fn) (void *thread_state, void *work_item);
  217. };
  218. struct thread_data {
  219. void *thread_state;
  220. void *data;
  221. };
  222. struct worker_thread {
  223. struct worker_thread_group *worker_thread_group;
  224. pthread_mutex_t new_work_mutex;
  225. pthread_cond_t new_work_cond;
  226. pthread_cond_t cond;
  227. pthread_mutex_t done_work_mutex;
  228. pthread_cond_t done_work_cond;
  229. pthread_t thread_id;
  230. struct queue queue;
  231. void *thread_state;
  232. struct thread_data thread_data;
  233. };
  234. struct mcast_worker_fn_work_item {
  235. struct sort_queue_item *sort_queue_item;
  236. struct totemsrp_instance *instance;
  237. };
  238. struct message_item {
  239. struct mcast *mcast;
  240. struct iovec iovec[MAXIOVS];
  241. int iov_len;
  242. };
  243. struct sort_queue_item {
  244. struct iovec iovec[MAXIOVS];
  245. int iov_len;
  246. };
  247. struct orf_token_mcast_thread_state {
  248. char iobuf[9000];
  249. prng_state prng_state;
  250. };
  251. enum memb_state {
  252. MEMB_STATE_OPERATIONAL = 1,
  253. MEMB_STATE_GATHER = 2,
  254. MEMB_STATE_COMMIT = 3,
  255. MEMB_STATE_RECOVERY = 4
  256. };
  257. struct totemsrp_instance {
  258. /*
  259. * Authentication of messages
  260. */
  261. hmac_state totemsrp_hmac_state;
  262. prng_state totemsrp_prng_state;
  263. unsigned char totemsrp_private_key[1024];
  264. unsigned int totemsrp_private_key_len;
  265. int stats_sent;
  266. int stats_recv;
  267. int stats_delv;
  268. int stats_remcasts;
  269. int stats_orf_token;
  270. struct timeval stats_tv_start;
  271. /*
  272. * Flow control mcasts and remcasts on last and current orf_token
  273. */
  274. int fcc_remcast_last;
  275. int fcc_mcast_last;
  276. int fcc_mcast_current;
  277. int fcc_remcast_current;
  278. struct consensus_list_item consensus_list[PROCESSOR_COUNT_MAX];
  279. int consensus_list_entries;
  280. struct in_addr my_proc_list[PROCESSOR_COUNT_MAX];
  281. struct in_addr my_failed_list[PROCESSOR_COUNT_MAX];
  282. struct in_addr my_new_memb_list[PROCESSOR_COUNT_MAX];
  283. struct in_addr my_trans_memb_list[PROCESSOR_COUNT_MAX];
  284. struct in_addr my_memb_list[PROCESSOR_COUNT_MAX];
  285. struct in_addr my_deliver_memb_list[PROCESSOR_COUNT_MAX];
  286. int my_proc_list_entries;
  287. int my_failed_list_entries;
  288. int my_new_memb_entries;
  289. int my_trans_memb_entries;
  290. int my_memb_entries;
  291. int my_deliver_memb_entries;
  292. struct memb_ring_id my_ring_id;
  293. struct memb_ring_id my_old_ring_id;
  294. int my_aru_count;
  295. int my_merge_detect_timeout_outstanding;
  296. int my_last_aru;
  297. int my_seq_unchanged;
  298. int my_received_flg;
  299. int my_high_seq_received;
  300. int my_install_seq;
  301. int my_rotation_counter;
  302. int my_set_retrans_flg;
  303. int my_retrans_flg_count;
  304. unsigned int my_high_ring_delivered;
  305. unsigned int timeout_token;
  306. unsigned int timeout_token_retransmit;
  307. unsigned int timeout_token_hold;
  308. unsigned int token_retransmits_before_loss;
  309. unsigned int timeout_state_gather_join;
  310. unsigned int timeout_state_gather_consensus;
  311. unsigned int timeout_merge_detect;
  312. unsigned int timeout_downcheck;
  313. unsigned int fail_to_recv_const;
  314. /*
  315. * Queues used to order, deliver, and recover messages
  316. */
  317. struct queue new_message_queue;
  318. struct queue retrans_message_queue;
  319. struct sq regular_sort_queue;
  320. struct sq recovery_sort_queue;
  321. /*
  322. * File descriptors in use by TOTEMSRP
  323. */
  324. struct totemsrp_socket totemsrp_sockets[2];
  325. /*
  326. * Received up to and including
  327. */
  328. int my_aru;
  329. int my_high_delivered;
  330. struct list_head token_callback_received_listhead;
  331. struct list_head token_callback_sent_listhead;
  332. char orf_token_retransmit[15000]; // sizeof (struct orf_token) + sizeof (struct rtr_item) * RETRANSMIT_ENTRIES_MAX];
  333. int orf_token_retransmit_size;
  334. int my_token_seq;
  335. /*
  336. * Timers
  337. */
  338. poll_timer_handle timer_orf_token_timeout;
  339. poll_timer_handle timer_orf_token_retransmit_timeout;
  340. poll_timer_handle timer_orf_token_hold_retransmit_timeout;
  341. poll_timer_handle timer_merge_detect_timeout;
  342. poll_timer_handle memb_timer_state_gather_join_timeout;
  343. poll_timer_handle memb_timer_state_gather_consensus_timeout;
  344. poll_timer_handle memb_timer_state_commit_timeout;
  345. poll_timer_handle timer_netif_check_timeout;
  346. /*
  347. * Function and data used to log messages
  348. */
  349. int totemsrp_log_level_security;
  350. int totemsrp_log_level_error;
  351. int totemsrp_log_level_warning;
  352. int totemsrp_log_level_notice;
  353. int totemsrp_log_level_debug;
  354. void (*totemsrp_log_printf) (int level, char *format, ...);
  355. enum memb_state memb_state;
  356. struct sockaddr_in my_id;
  357. struct sockaddr_in next_memb;
  358. struct sockaddr_in memb_local_sockaddr_in;
  359. char iov_buffer[15000]; //PACKET_SIZE_MAX];
  360. struct iovec totemsrp_iov_recv;
  361. poll_handle *totemsrp_poll_handle;
  362. struct totem_interface *totemsrp_interfaces;
  363. int totemsrp_interface_count;
  364. int netif_state_report;
  365. int netif_bind_state;
  366. struct worker_thread_group worker_thread_group;
  367. struct worker_thread_group worker_thread_group_orf_token_mcast;
  368. /*
  369. * Function called when new message received
  370. */
  371. int (*totemsrp_recv) (char *group, struct iovec *iovec, int iov_len);
  372. /*
  373. * Multicast address
  374. */
  375. struct sockaddr_in sockaddr_in_mcast;
  376. void (*totemsrp_deliver_fn) (
  377. struct in_addr source_addr,
  378. struct iovec *iovec,
  379. int iov_len,
  380. int endian_conversion_required);
  381. void (*totemsrp_confchg_fn) (
  382. enum totem_configuration_type configuration_type,
  383. struct in_addr *member_list, int member_list_entries,
  384. struct in_addr *left_list, int left_list_entries,
  385. struct in_addr *joined_list, int joined_list_entries,
  386. struct memb_ring_id *ring_id);
  387. char iov_encrypted_buffer[15000];
  388. struct iovec iov_encrypted;
  389. int global_seqno;
  390. int my_token_held;
  391. unsigned long long token_ring_id_seq;
  392. int log_digest;
  393. int last_released;
  394. int set_aru;
  395. int totemsrp_brake;
  396. int old_ring_state_saved;
  397. int old_ring_state_aru;
  398. int old_ring_state_high_seq_received;
  399. int ring_saved;
  400. int my_last_seq;
  401. struct timeval tv_old;
  402. int firstrun;
  403. };
  404. struct message_handlers {
  405. int count;
  406. int (*handler_functions[6]) (struct totemsrp_instance *,
  407. struct sockaddr_in *, struct iovec *, int, int, int);
  408. };
  409. /*
  410. * forward decls
  411. */
  412. static int message_handler_orf_token (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  413. static int message_handler_mcast (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  414. static int message_handler_memb_merge_detect (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  415. static int message_handler_memb_join (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  416. static int message_handler_memb_commit_token (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  417. static int message_handler_token_hold_cancel (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
  418. static void memb_ring_id_create_or_load (struct totemsrp_instance *, struct memb_ring_id *);
  419. static int recv_handler (poll_handle handle, int fd, int revents, void *data, unsigned int *prio);
  420. static int netif_determine (struct totemsrp_instance *instance, struct sockaddr_in *bindnet, struct sockaddr_in *bound_to,int *interface_up);
  421. static int loopback_determine (struct sockaddr_in *bound_to);
  422. static void netif_down_check (struct totemsrp_instance *instance);
  423. static void token_callbacks_execute (struct totemsrp_instance *instance, enum totem_callback_token_type type);
  424. #define NETIF_STATE_REPORT_UP 1
  425. #define NETIF_STATE_REPORT_DOWN 2
  426. #define BIND_STATE_UNBOUND 0
  427. #define BIND_STATE_REGULAR 1
  428. #define BIND_STATE_LOOPBACK 2
  429. static int totemsrp_build_sockets (
  430. struct totemsrp_instance *instance,
  431. struct sockaddr_in *sockaddr_mcast,
  432. struct sockaddr_in *sockaddr_bindnet,
  433. struct totemsrp_socket *sockets,
  434. struct sockaddr_in *bound_to,
  435. int *interface_up);
  436. static int totemsrp_build_sockets_loopback (
  437. struct totemsrp_instance *instance,
  438. struct sockaddr_in *sockaddr_mcast,
  439. struct sockaddr_in *sockaddr_bindnet,
  440. struct totemsrp_socket *sockets,
  441. struct sockaddr_in *bound_to);
  442. static void memb_state_gather_enter (struct totemsrp_instance *instance);
  443. static void messages_deliver_to_app (struct totemsrp_instance *instance, int skip, int end_point);
  444. static int orf_token_mcast (struct totemsrp_instance *instance, struct orf_token *oken,
  445. int fcc_mcasts_allowed, struct sockaddr_in *system_from);
  446. static int messages_free (struct totemsrp_instance *instance, int token_aru);
  447. static void encrypt_and_sign (struct totemsrp_instance *instance, struct iovec *iovec, int iov_len);
  448. static int authenticate_and_decrypt (struct totemsrp_instance *instance, struct iovec *iov);
  449. static int recv_handler (poll_handle handle, int fd, int revents, void *data, unsigned int *prio);
  450. static void memb_ring_id_store (struct totemsrp_instance *instance, struct memb_commit_token *commit_token);
  451. static void memb_state_commit_token_update (struct totemsrp_instance *instance, struct memb_commit_token *memb_commit_token);
  452. static int memb_state_commit_token_send (struct totemsrp_instance *instance, struct memb_commit_token *memb_commit_token);
  453. static void memb_state_commit_token_create (struct totemsrp_instance *instance, struct memb_commit_token *commit_token);
  454. static int token_hold_cancel_send (struct totemsrp_instance *instance);
  455. static void orf_token_endian_convert (struct orf_token *in, struct orf_token *out);
  456. static void memb_commit_token_endian_convert (struct memb_commit_token *in, struct memb_commit_token *out);
  457. static void memb_join_endian_convert (struct memb_join *in, struct memb_join *out);
  458. static void mcast_endian_convert (struct mcast *in, struct mcast *out);
  459. static void timer_function_orf_token_timeout (void *data);
  460. static void timer_function_token_retransmit_timeout (void *data);
  461. static void timer_function_token_hold_retransmit_timeout (void *data);
  462. static void timer_function_merge_detect_timeout (void *data);
  463. /*
  464. * All instances in one database
  465. */
  466. static struct saHandleDatabase totemsrp_instance_database = {
  467. .handleCount = 0,
  468. .handles = 0,
  469. .handleInstanceDestructor = 0
  470. };
  471. struct message_handlers totemsrp_message_handlers = {
  472. 6,
  473. {
  474. message_handler_orf_token,
  475. message_handler_mcast,
  476. message_handler_memb_merge_detect,
  477. message_handler_memb_join,
  478. message_handler_memb_commit_token,
  479. message_handler_token_hold_cancel
  480. }
  481. };
  482. void instance_initialize (struct totemsrp_instance *instance)
  483. {
  484. memset (instance, 0, sizeof (struct totemsrp_instance));
  485. list_init (&instance->token_callback_received_listhead);
  486. list_init (&instance->token_callback_sent_listhead);
  487. instance->my_received_flg = 1;
  488. instance->timeout_token = TIMEOUT_TOKEN;
  489. instance->timeout_token_retransmit = TIMEOUT_TOKEN_RETRANSMIT;
  490. instance->token_retransmits_before_loss = TOKEN_RETRANSMITS_BEFORE_LOSS;
  491. instance->timeout_state_gather_join = TIMEOUT_STATE_GATHER_JOIN;
  492. instance->timeout_state_gather_consensus = TIMEOUT_STATE_GATHER_CONSENSUS;
  493. instance->timeout_merge_detect = TIMEOUT_MERGE_DETECT;
  494. instance->timeout_downcheck = TIMEOUT_DOWNCHECK;
  495. instance->fail_to_recv_const = FAIL_TO_RECV_CONST;
  496. instance->my_token_seq = -1;
  497. instance->memb_state = MEMB_STATE_OPERATIONAL;
  498. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  499. instance->totemsrp_iov_recv.iov_base = instance->iov_buffer;
  500. instance->totemsrp_iov_recv.iov_len = sizeof (instance->iov_buffer);
  501. instance->iov_encrypted.iov_base = instance->iov_encrypted_buffer;
  502. instance->iov_encrypted.iov_len = sizeof (instance->iov_encrypted_buffer);
  503. instance->set_aru = -1;
  504. }
  505. #ifdef CODE_COVERAGE_COMPILE_OUT
  506. void print_digest (char *where, unsigned char *digest)
  507. {
  508. int i;
  509. printf ("DIGEST %s:\n", where);
  510. for (i = 0; i < 16; i++) {
  511. printf ("%x ", digest[i]);
  512. }
  513. printf ("\n");
  514. }
  515. void print_msg (unsigned char *msg, int size)
  516. {
  517. int i;
  518. printf ("MSG CONTENTS START\n");
  519. for (i = 0; i < size; i++) {
  520. printf ("%x ", msg[i]);
  521. if ((i % 16) == 15) {
  522. printf ("\n");
  523. }
  524. }
  525. printf ("MSG CONTENTS DONE\n");
  526. }
  527. #endif
  528. static void orf_token_mcast_worker_fn (void *thread_state, void *work_item);
  529. static void *worker_thread (void *thread_data_in) {
  530. struct thread_data *thread_data = (struct thread_data *)thread_data_in;
  531. struct orf_token_mcast_thread_state *orf_token_mcast_thread_state =
  532. (struct orf_token_mcast_thread_state *)thread_data->thread_state;
  533. struct worker_thread *worker_thread =
  534. (struct worker_thread *)thread_data->data;
  535. void *data_for_worker_fn;
  536. for (;;) {
  537. pthread_mutex_lock (&worker_thread->new_work_mutex);
  538. if (queue_is_empty (&worker_thread->queue) == 1) {
  539. pthread_cond_wait (&worker_thread->new_work_cond,
  540. &worker_thread->new_work_mutex);
  541. }
  542. data_for_worker_fn = queue_item_get (&worker_thread->queue);
  543. worker_thread->worker_thread_group->worker_fn (orf_token_mcast_thread_state, data_for_worker_fn);
  544. queue_item_remove (&worker_thread->queue);
  545. pthread_mutex_unlock (&worker_thread->new_work_mutex);
  546. pthread_mutex_lock (&worker_thread->done_work_mutex);
  547. if (queue_is_empty (&worker_thread->queue) == 1) {
  548. pthread_cond_signal (&worker_thread->done_work_cond);
  549. }
  550. pthread_mutex_unlock (&worker_thread->done_work_mutex);
  551. }
  552. return (0);
  553. }
  554. static int worker_thread_group_init (
  555. struct worker_thread_group *worker_thread_group,
  556. int threads,
  557. int items_max,
  558. int item_size,
  559. int thread_state_size,
  560. void (*thread_state_constructor)(void *),
  561. void (*worker_fn)(void *thread_state, void *work_item))
  562. {
  563. int i;
  564. worker_thread_group->threadcount = threads;
  565. worker_thread_group->last_scheduled = 0;
  566. worker_thread_group->worker_fn = worker_fn;
  567. worker_thread_group->threads = malloc (sizeof (struct worker_thread) *
  568. threads);
  569. if (worker_thread_group->threads == 0) {
  570. return (-1);
  571. }
  572. for (i = 0; i < threads; i++) {
  573. worker_thread_group->threads[i].thread_state = malloc (thread_state_size);
  574. thread_state_constructor (worker_thread_group->threads[i].thread_state);
  575. worker_thread_group->threads[i].worker_thread_group = worker_thread_group;
  576. pthread_mutex_init (&worker_thread_group->threads[i].new_work_mutex, NULL);
  577. pthread_cond_init (&worker_thread_group->threads[i].new_work_cond, NULL);
  578. pthread_mutex_init (&worker_thread_group->threads[i].done_work_mutex, NULL);
  579. pthread_cond_init (&worker_thread_group->threads[i].done_work_cond, NULL);
  580. queue_init (&worker_thread_group->threads[i].queue, items_max,
  581. item_size);
  582. worker_thread_group->threads[i].thread_data.thread_state =
  583. worker_thread_group->threads[i].thread_state;
  584. worker_thread_group->threads[i].thread_data.data = &worker_thread_group->threads[i];
  585. pthread_create (&worker_thread_group->threads[i].thread_id,
  586. NULL, worker_thread, &worker_thread_group->threads[i].thread_data);
  587. }
  588. return (0);
  589. }
  590. static void worker_thread_group_exit (
  591. struct worker_thread_group *worker_thread_group)
  592. {
  593. int i;
  594. for (i = 0; i < worker_thread_group->threadcount; i++) {
  595. pthread_cancel (worker_thread_group->threads[i].thread_id);
  596. }
  597. }
  598. static void worker_thread_group_work_add (
  599. struct worker_thread_group *worker_thread_group,
  600. void *item)
  601. {
  602. int schedule;
  603. schedule = (worker_thread_group->last_scheduled + 1) % (worker_thread_group->threadcount);
  604. worker_thread_group->last_scheduled = schedule;
  605. pthread_mutex_lock (&worker_thread_group->threads[schedule].new_work_mutex);
  606. queue_item_add (&worker_thread_group->threads[schedule].queue, item);
  607. pthread_cond_signal (&worker_thread_group->threads[schedule].new_work_cond);
  608. pthread_mutex_unlock (&worker_thread_group->threads[schedule].new_work_mutex);
  609. }
  610. static void worker_thread_group_wait (
  611. struct worker_thread_group *worker_thread_group)
  612. {
  613. int i;
  614. for (i = 0; i < worker_thread_group->threadcount; i++) {
  615. pthread_mutex_lock (&worker_thread_group->threads[i].done_work_mutex);
  616. if (queue_is_empty (&worker_thread_group->threads[i].queue) == 0) {
  617. pthread_cond_wait (&worker_thread_group->threads[i].done_work_cond,
  618. &worker_thread_group->threads[i].done_work_mutex);
  619. }
  620. pthread_mutex_unlock (&worker_thread_group->threads[i].done_work_mutex);
  621. }
  622. }
  623. static void orf_token_mcast_thread_state_constructor (
  624. void *orf_token_mcast_thread_state_in)
  625. {
  626. struct orf_token_mcast_thread_state *orf_token_mcast_thread_state =
  627. (struct orf_token_mcast_thread_state *)orf_token_mcast_thread_state_in;
  628. memset (orf_token_mcast_thread_state, 0,
  629. sizeof (orf_token_mcast_thread_state));
  630. rng_make_prng (128, PRNG_SOBER,
  631. &orf_token_mcast_thread_state->prng_state, NULL);
  632. }
  633. /*
  634. * Exported interfaces
  635. */
  636. int totemsrp_initialize (
  637. poll_handle *poll_handle,
  638. totemsrp_handle *handle,
  639. struct totem_config *totem_config,
  640. void (*deliver_fn) (
  641. struct in_addr source_addr,
  642. struct iovec *iovec,
  643. int iov_len,
  644. int endian_conversion_required),
  645. void (*confchg_fn) (
  646. enum totem_configuration_type configuration_type,
  647. struct in_addr *member_list, int member_list_entries,
  648. struct in_addr *left_list, int left_list_entries,
  649. struct in_addr *joined_list, int joined_list_entries,
  650. struct memb_ring_id *ring_id))
  651. {
  652. unsigned int *timeouts = totem_config->timeouts;
  653. struct totemsrp_instance *instance;
  654. SaErrorT error;
  655. int i;
  656. error = saHandleCreate (&totemsrp_instance_database,
  657. sizeof (struct totemsrp_instance), handle);
  658. if (error != SA_OK) {
  659. goto error_exit;
  660. }
  661. error = saHandleInstanceGet (&totemsrp_instance_database, *handle,
  662. (void *)&instance);
  663. if (error != SA_OK) {
  664. goto error_destroy;
  665. }
  666. instance_initialize (instance);
  667. /*
  668. * Configure logging
  669. */
  670. instance->totemsrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  671. instance->totemsrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  672. instance->totemsrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  673. instance->totemsrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  674. instance->totemsrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  675. instance->totemsrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  676. instance->timeout_token_hold = (int)(instance->timeout_token_retransmit * 0.8 - (1000/HZ));
  677. /*
  678. * Initialize random number generator for later use to generate salt
  679. */
  680. memcpy (instance->totemsrp_private_key, totem_config->private_key,
  681. totem_config->private_key_len);
  682. instance->totemsrp_private_key_len = totem_config->private_key_len;
  683. rng_make_prng (128, PRNG_SOBER, &instance->totemsrp_prng_state, NULL);
  684. /*
  685. * Initialize local variables for totemsrp
  686. */
  687. memcpy (&instance->sockaddr_in_mcast, &totem_config->mcast_addr,
  688. sizeof (struct sockaddr_in));
  689. memset (&instance->next_memb, 0, sizeof (struct sockaddr_in));
  690. memset (instance->iov_buffer, 0, PACKET_SIZE_MAX);
  691. /*
  692. * Initialize thread group data structure
  693. */
  694. worker_thread_group_init (&instance->worker_thread_group_orf_token_mcast,
  695. WORKER_THREAD_COUNT, 128, sizeof (struct mcast_worker_fn_work_item),
  696. sizeof (struct orf_token_mcast_thread_state),
  697. orf_token_mcast_thread_state_constructor,
  698. orf_token_mcast_worker_fn);
  699. /*
  700. * Update our timeout values if they were specified in the openais.conf
  701. * file.
  702. */
  703. for (i = 0; i < MAX_TOTEM_TIMEOUTS; i++) {
  704. if (!timeouts[i]) {
  705. continue;
  706. }
  707. switch (i) {
  708. case TOTEM_TOKEN:
  709. instance->timeout_token = timeouts[i];
  710. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  711. "Overriding token timeout to (%u ms)\n", timeouts[i]);
  712. instance->timeout_token_retransmit = (int)(instance->timeout_token / (instance->token_retransmits_before_loss + 0.2));
  713. instance->timeout_token_hold = (int)(instance->timeout_token_retransmit * 0.8 - (1000/HZ));
  714. break;
  715. case TOTEM_RETRANSMIT_TOKEN:
  716. instance->timeout_token_retransmit = timeouts[i];
  717. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  718. "Overriding token retransmit timeout to (%u ms)\n", timeouts[i]);
  719. break;
  720. case TOTEM_RETRANSMITS_BEFORE_LOSS:
  721. instance->token_retransmits_before_loss = timeouts[i];
  722. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  723. "Overriding retransmits before loss (%u retrans)\n", timeouts[i]);
  724. break;
  725. case TOTEM_HOLD_TOKEN:
  726. instance->timeout_token_hold = timeouts[i];
  727. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  728. "Overriding token hold timeout to (%u ms)\n", timeouts[i]);
  729. break;
  730. case TOTEM_JOIN:
  731. instance->timeout_state_gather_join = timeouts[i];
  732. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  733. "Join Timeout set to %u ms\n", timeouts[i]);
  734. break;
  735. case TOTEM_CONSENSUS:
  736. instance->timeout_state_gather_consensus = timeouts[i];
  737. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  738. "Consensus Timeout set to %u ms\n", timeouts[i]);
  739. break;
  740. case TOTEM_MERGE:
  741. instance->timeout_merge_detect = timeouts[i];
  742. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  743. "Merge Detect Timeout set to %u ms\n", timeouts[i]);
  744. break;
  745. case TOTEM_DOWNCHECK:
  746. instance->timeout_downcheck = timeouts[i];
  747. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  748. "Downcheck Timeout set to %u ms\n", timeouts[i]);
  749. break;
  750. case TOTEM_FAIL_RECV_CONST:
  751. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  752. "Failed To Receive Const set to %u\n", timeouts[i]);
  753. instance->fail_to_recv_const = timeouts[i];
  754. break;
  755. default:
  756. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  757. "Received unknown timeout type: %d\n", timeouts[i]);
  758. break;
  759. }
  760. }
  761. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  762. "Token Timeout (%d ms) retransmit timeout (%d ms)\n",
  763. instance->timeout_token, instance->timeout_token_retransmit);
  764. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  765. "token hold (%d ms) retransmits before loss (%d retrans)\n",
  766. instance->timeout_token_hold, instance->token_retransmits_before_loss);
  767. queue_init (&instance->new_message_queue, NEW_MESSAGE_QUEUE_SIZE_MAX,
  768. sizeof (struct message_item));
  769. queue_init (&instance->retrans_message_queue, RETRANS_MESSAGE_QUEUE_SIZE_MAX,
  770. sizeof (struct message_item));
  771. sq_init (&instance->regular_sort_queue,
  772. QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct sort_queue_item), 0);
  773. sq_init (&instance->recovery_sort_queue,
  774. QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct sort_queue_item), 0);
  775. instance->totemsrp_interfaces = totem_config->interfaces;
  776. instance->totemsrp_interface_count = 1;
  777. instance->totemsrp_poll_handle = poll_handle;
  778. netif_down_check (instance);
  779. memb_state_gather_enter (instance);
  780. instance->totemsrp_deliver_fn = deliver_fn;
  781. instance->totemsrp_confchg_fn = confchg_fn;
  782. return (0);
  783. error_destroy:
  784. saHandleDestroy (&totemsrp_instance_database, *handle);
  785. error_exit:
  786. return (-1);
  787. }
  788. void totemsrp_finalize (
  789. totemsrp_handle handle)
  790. {
  791. struct totemsrp_instance *instance;
  792. SaErrorT error;
  793. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  794. (void *)&instance);
  795. if (error != SA_OK) {
  796. return;
  797. }
  798. worker_thread_group_exit (&instance->worker_thread_group_orf_token_mcast);
  799. saHandleInstancePut (&totemsrp_instance_database, handle);
  800. }
  801. /*
  802. * Set operations for use by the membership algorithm
  803. */
  804. static void memb_consensus_reset (struct totemsrp_instance *instance)
  805. {
  806. instance->consensus_list_entries = 0;
  807. }
  808. static void memb_set_subtract (
  809. struct in_addr *out_list, int *out_list_entries,
  810. struct in_addr *one_list, int one_list_entries,
  811. struct in_addr *two_list, int two_list_entries)
  812. {
  813. int found = 0;
  814. int i;
  815. int j;
  816. *out_list_entries = 0;
  817. for (i = 0; i < one_list_entries; i++) {
  818. for (j = 0; j < two_list_entries; j++) {
  819. if (one_list[i].s_addr == two_list[j].s_addr) {
  820. found = 1;
  821. break;
  822. }
  823. }
  824. if (found == 0) {
  825. out_list[*out_list_entries].s_addr = one_list[i].s_addr;
  826. *out_list_entries = *out_list_entries + 1;
  827. }
  828. found = 0;
  829. }
  830. }
  831. /*
  832. * Set consensus for a specific processor
  833. */
  834. static void memb_consensus_set (
  835. struct totemsrp_instance *instance,
  836. struct in_addr *addr)
  837. {
  838. int found = 0;
  839. int i;
  840. for (i = 0; i < instance->consensus_list_entries; i++) {
  841. if (addr->s_addr == instance->consensus_list[i].addr.s_addr) {
  842. found = 1;
  843. break; /* found entry */
  844. }
  845. }
  846. instance->consensus_list[i].addr.s_addr = addr->s_addr;
  847. instance->consensus_list[i].set = 1;
  848. if (found == 0) {
  849. instance->consensus_list_entries++;
  850. }
  851. return;
  852. }
  853. /*
  854. * Is consensus set for a specific processor
  855. */
  856. static int memb_consensus_isset (
  857. struct totemsrp_instance *instance,
  858. struct in_addr *addr)
  859. {
  860. int i;
  861. for (i = 0; i < instance->consensus_list_entries; i++) {
  862. if (addr->s_addr == instance->consensus_list[i].addr.s_addr) {
  863. return (instance->consensus_list[i].set);
  864. }
  865. }
  866. return (0);
  867. }
  868. /*
  869. * Is consensus agreed upon based upon consensus database
  870. */
  871. static int memb_consensus_agreed (
  872. struct totemsrp_instance *instance)
  873. {
  874. struct in_addr token_memb[PROCESSOR_COUNT_MAX];
  875. int token_memb_entries = 0;
  876. int agreed = 1;
  877. int i;
  878. memb_set_subtract (token_memb, &token_memb_entries,
  879. instance->my_proc_list, instance->my_proc_list_entries,
  880. instance->my_failed_list, instance->my_failed_list_entries);
  881. for (i = 0; i < token_memb_entries; i++) {
  882. if (memb_consensus_isset (instance, &token_memb[i]) == 0) {
  883. agreed = 0;
  884. break;
  885. }
  886. }
  887. assert (token_memb_entries >= 1);
  888. return (agreed);
  889. }
  890. static void memb_consensus_notset (
  891. struct totemsrp_instance *instance,
  892. struct in_addr *no_consensus_list,
  893. int *no_consensus_list_entries,
  894. struct in_addr *comparison_list,
  895. int comparison_list_entries)
  896. {
  897. int i;
  898. *no_consensus_list_entries = 0;
  899. for (i = 0; i < instance->my_proc_list_entries; i++) {
  900. if (memb_consensus_isset (instance, &instance->my_proc_list[i]) == 0) {
  901. no_consensus_list[*no_consensus_list_entries].s_addr = instance->my_proc_list[i].s_addr;
  902. *no_consensus_list_entries = *no_consensus_list_entries + 1;
  903. }
  904. }
  905. }
  906. /*
  907. * Is set1 equal to set2 Entries can be in different orders
  908. */
  909. static int memb_set_equal (struct in_addr *set1, int set1_entries,
  910. struct in_addr *set2, int set2_entries)
  911. {
  912. int i;
  913. int j;
  914. int found = 0;
  915. if (set1_entries != set2_entries) {
  916. return (0);
  917. }
  918. for (i = 0; i < set2_entries; i++) {
  919. for (j = 0; j < set1_entries; j++) {
  920. if (set1[j].s_addr == set2[i].s_addr) {
  921. found = 1;
  922. break;
  923. }
  924. }
  925. if (found == 0) {
  926. return (0);
  927. }
  928. found = 0;
  929. }
  930. return (1);
  931. }
  932. /*
  933. * Is subset fully contained in fullset
  934. */
  935. static int memb_set_subset (
  936. struct in_addr *subset, int subset_entries,
  937. struct in_addr *fullset, int fullset_entries)
  938. {
  939. int i;
  940. int j;
  941. int found = 0;
  942. if (subset_entries > fullset_entries) {
  943. return (0);
  944. }
  945. for (i = 0; i < subset_entries; i++) {
  946. for (j = 0; j < fullset_entries; j++) {
  947. if (subset[i].s_addr == fullset[j].s_addr) {
  948. found = 1;
  949. }
  950. }
  951. if (found == 0) {
  952. return (0);
  953. }
  954. found = 1;
  955. }
  956. return (1);
  957. }
  958. /*
  959. * merge subset into fullset taking care not to add duplicates
  960. */
  961. static void memb_set_merge (
  962. struct in_addr *subset, int subset_entries,
  963. struct in_addr *fullset, int *fullset_entries)
  964. {
  965. int found = 0;
  966. int i;
  967. int j;
  968. for (i = 0; i < subset_entries; i++) {
  969. for (j = 0; j < *fullset_entries; j++) {
  970. if (fullset[j].s_addr == subset[i].s_addr) {
  971. found = 1;
  972. break;
  973. }
  974. }
  975. if (found == 0) {
  976. fullset[j].s_addr = subset[i].s_addr;
  977. *fullset_entries = *fullset_entries + 1;
  978. }
  979. found = 0;
  980. }
  981. return;
  982. }
  983. static void memb_set_and (
  984. struct in_addr *set1, int set1_entries,
  985. struct in_addr *set2, int set2_entries,
  986. struct in_addr *and, int *and_entries)
  987. {
  988. int i;
  989. int j;
  990. int found = 0;
  991. *and_entries = 0;
  992. for (i = 0; i < set2_entries; i++) {
  993. for (j = 0; j < set1_entries; j++) {
  994. if (set1[j].s_addr == set2[i].s_addr) {
  995. found = 1;
  996. break;
  997. }
  998. }
  999. if (found) {
  1000. and[*and_entries].s_addr = set1[j].s_addr;
  1001. *and_entries = *and_entries + 1;
  1002. }
  1003. found = 0;
  1004. }
  1005. return;
  1006. }
  1007. #ifdef CODE_COVERGE
  1008. static void memb_set_print (
  1009. char *string,
  1010. struct in_addr *list,
  1011. int list_entries)
  1012. {
  1013. int i;
  1014. printf ("List '%s' contains %d entries:\n", string, list_entries);
  1015. for (i = 0; i < list_entries; i++) {
  1016. printf ("addr %s\n", inet_ntoa (list[i]));
  1017. }
  1018. }
  1019. #endif
  1020. static void reset_token_retransmit_timeout (struct totemsrp_instance *instance)
  1021. {
  1022. poll_timer_delete (*instance->totemsrp_poll_handle,
  1023. instance->timer_orf_token_retransmit_timeout);
  1024. poll_timer_add (*instance->totemsrp_poll_handle,
  1025. instance->timeout_token_retransmit,
  1026. (void *)instance,
  1027. timer_function_token_retransmit_timeout,
  1028. &instance->timer_orf_token_retransmit_timeout);
  1029. }
  1030. static void start_merge_detect_timeout (struct totemsrp_instance *instance)
  1031. {
  1032. if (instance->my_merge_detect_timeout_outstanding == 0) {
  1033. poll_timer_add (*instance->totemsrp_poll_handle,
  1034. instance->timeout_merge_detect,
  1035. (void *)instance,
  1036. timer_function_merge_detect_timeout,
  1037. &instance->timer_merge_detect_timeout);
  1038. instance->my_merge_detect_timeout_outstanding = 1;
  1039. }
  1040. }
  1041. static void cancel_merge_detect_timeout (struct totemsrp_instance *instance)
  1042. {
  1043. poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_merge_detect_timeout);
  1044. instance->my_merge_detect_timeout_outstanding = 0;
  1045. }
  1046. /*
  1047. * ring_state_* is used to save and restore the sort queue
  1048. * state when a recovery operation fails (and enters gather)
  1049. */
  1050. static void old_ring_state_save (struct totemsrp_instance *instance)
  1051. {
  1052. if (instance->old_ring_state_saved == 0) {
  1053. instance->old_ring_state_saved = 1;
  1054. instance->old_ring_state_aru = instance->my_aru;
  1055. instance->old_ring_state_high_seq_received = instance->my_high_seq_received;
  1056. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1057. "Saving state aru %d high seq recieved %d\n",
  1058. instance->my_aru, instance->my_high_seq_received);
  1059. }
  1060. }
  1061. static void ring_save (struct totemsrp_instance *instance)
  1062. {
  1063. if (instance->ring_saved == 0) {
  1064. instance->ring_saved = 1;
  1065. memcpy (&instance->my_old_ring_id, &instance->my_ring_id,
  1066. sizeof (struct memb_ring_id));
  1067. }
  1068. }
  1069. static void ring_reset (struct totemsrp_instance *instance)
  1070. {
  1071. instance->ring_saved = 0;
  1072. }
  1073. static void ring_state_restore (struct totemsrp_instance *instance)
  1074. {
  1075. if (instance->old_ring_state_saved) {
  1076. instance->my_ring_id.rep.s_addr = 0;
  1077. instance->my_aru = instance->old_ring_state_aru;
  1078. instance->my_high_seq_received = instance->old_ring_state_high_seq_received;
  1079. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1080. "Restoring instance->my_aru %d my high seq received %d\n",
  1081. instance->my_aru, instance->my_high_seq_received);
  1082. }
  1083. }
  1084. static void old_ring_state_reset (struct totemsrp_instance *instance)
  1085. {
  1086. instance->old_ring_state_saved = 0;
  1087. }
  1088. static void reset_token_timeout (struct totemsrp_instance *instance) {
  1089. poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_orf_token_timeout);
  1090. poll_timer_add (*instance->totemsrp_poll_handle,
  1091. instance->timeout_token,
  1092. (void *)instance,
  1093. timer_function_orf_token_timeout,
  1094. &instance->timer_orf_token_timeout);
  1095. }
  1096. static void cancel_token_timeout (struct totemsrp_instance *instance) {
  1097. poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_orf_token_timeout);
  1098. }
  1099. static void cancel_token_retransmit_timeout (struct totemsrp_instance *instance)
  1100. {
  1101. poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_orf_token_retransmit_timeout);
  1102. }
  1103. static void start_token_hold_retransmit_timeout (struct totemsrp_instance *instance)
  1104. {
  1105. poll_timer_add (*instance->totemsrp_poll_handle,
  1106. instance->timeout_token_hold,
  1107. (void *)instance,
  1108. timer_function_token_hold_retransmit_timeout,
  1109. &instance->timer_orf_token_hold_retransmit_timeout);
  1110. }
  1111. static void cancel_token_hold_retransmit_timeout (struct totemsrp_instance *instance)
  1112. {
  1113. poll_timer_delete (*instance->totemsrp_poll_handle,
  1114. instance->timer_orf_token_hold_retransmit_timeout);
  1115. }
  1116. static void memb_state_consensus_timeout_expired (
  1117. struct totemsrp_instance *instance)
  1118. {
  1119. struct in_addr no_consensus_list[PROCESSOR_COUNT_MAX];
  1120. int no_consensus_list_entries;
  1121. if (memb_consensus_agreed (instance)) {
  1122. memb_consensus_reset (instance);
  1123. memb_consensus_set (instance, &instance->my_id.sin_addr);
  1124. reset_token_timeout (instance); // REVIEWED
  1125. } else {
  1126. memb_consensus_notset (instance, no_consensus_list,
  1127. &no_consensus_list_entries,
  1128. instance->my_proc_list, instance->my_proc_list_entries);
  1129. memb_set_merge (no_consensus_list, no_consensus_list_entries,
  1130. instance->my_failed_list, &instance->my_failed_list_entries);
  1131. memb_state_gather_enter (instance);
  1132. }
  1133. }
  1134. static int memb_join_message_send (struct totemsrp_instance *instance);
  1135. static int memb_merge_detect_transmit (struct totemsrp_instance *instance);
  1136. /*
  1137. * Timers used for various states of the membership algorithm
  1138. */
  1139. static void timer_function_orf_token_timeout (void *data)
  1140. {
  1141. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1142. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1143. "The token was lost in state %d from timer %x\n", instance->memb_state, data);
  1144. switch (instance->memb_state) {
  1145. case MEMB_STATE_OPERATIONAL:
  1146. netif_down_check (instance);
  1147. memb_state_gather_enter (instance);
  1148. break;
  1149. case MEMB_STATE_GATHER:
  1150. memb_state_consensus_timeout_expired (instance);
  1151. memb_state_gather_enter (instance);
  1152. break;
  1153. case MEMB_STATE_COMMIT:
  1154. memb_state_gather_enter (instance);
  1155. break;
  1156. case MEMB_STATE_RECOVERY:
  1157. ring_state_restore (instance);
  1158. memb_state_gather_enter (instance);
  1159. break;
  1160. }
  1161. }
  1162. static void memb_timer_function_state_gather (void *data)
  1163. {
  1164. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1165. switch (instance->memb_state) {
  1166. case MEMB_STATE_OPERATIONAL:
  1167. case MEMB_STATE_RECOVERY:
  1168. assert (0); /* this should never happen */
  1169. break;
  1170. case MEMB_STATE_GATHER:
  1171. case MEMB_STATE_COMMIT:
  1172. memb_join_message_send (instance);
  1173. /*
  1174. * Restart the join timeout
  1175. `*/
  1176. poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1177. poll_timer_add (*instance->totemsrp_poll_handle,
  1178. instance->timeout_state_gather_join,
  1179. (void *)instance,
  1180. memb_timer_function_state_gather,
  1181. &instance->memb_timer_state_gather_join_timeout);
  1182. break;
  1183. }
  1184. }
  1185. static void memb_timer_function_gather_consensus_timeout (void *data)
  1186. {
  1187. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1188. memb_state_consensus_timeout_expired (instance);
  1189. }
  1190. static void deliver_messages_from_recovery_to_regular (struct totemsrp_instance *instance)
  1191. {
  1192. int i;
  1193. struct sort_queue_item *recovery_message_item;
  1194. struct sort_queue_item regular_message_item;
  1195. int res;
  1196. void *ptr;
  1197. struct mcast *mcast;
  1198. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1199. "recovery to regular %d-%d\n", 1, instance->my_aru);
  1200. /*
  1201. * Move messages from recovery to regular sort queue
  1202. */
  1203. // todo should i be initialized to 0 or 1 ?
  1204. for (i = 1; i <= instance->my_aru; i++) {
  1205. res = sq_item_get (&instance->recovery_sort_queue, i, &ptr);
  1206. if (res != 0) {
  1207. continue;
  1208. }
  1209. printf ("Transferring message with seq id %d\n", i);
  1210. recovery_message_item = (struct sort_queue_item *)ptr;
  1211. /*
  1212. * Convert recovery message into regular message
  1213. */
  1214. if (recovery_message_item->iov_len > 1) {
  1215. mcast = recovery_message_item->iovec[1].iov_base;
  1216. memcpy (&regular_message_item.iovec[0],
  1217. &recovery_message_item->iovec[1],
  1218. sizeof (struct iovec) * recovery_message_item->iov_len);
  1219. } else {
  1220. mcast = recovery_message_item->iovec[0].iov_base;
  1221. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1222. "encapsulated is %d\n",
  1223. mcast->header.encapsulated);
  1224. if (mcast->header.encapsulated == 1) {
  1225. /*
  1226. * Message is a recovery message encapsulated
  1227. * in a new ring message
  1228. */
  1229. regular_message_item.iovec[0].iov_base =
  1230. recovery_message_item->iovec[0].iov_base + sizeof (struct mcast);
  1231. regular_message_item.iovec[0].iov_len =
  1232. recovery_message_item->iovec[0].iov_len - sizeof (struct mcast);
  1233. regular_message_item.iov_len = 1;
  1234. mcast = regular_message_item.iovec[0].iov_base;
  1235. } else {
  1236. printf ("not encapsulated\n");
  1237. continue; /* TODO this case shouldn't happen */
  1238. /*
  1239. * Message is originated on new ring and not
  1240. * encapsulated
  1241. */
  1242. regular_message_item.iovec[0].iov_base =
  1243. recovery_message_item->iovec[0].iov_base;
  1244. regular_message_item.iovec[0].iov_len =
  1245. recovery_message_item->iovec[0].iov_len;
  1246. }
  1247. }
  1248. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1249. "comparing if ring id is for this processors old ring seqno %d\n",
  1250. mcast->seq);
  1251. /*
  1252. * Only add this message to the regular sort
  1253. * queue if it was originated with the same ring
  1254. * id as the previous ring
  1255. */
  1256. if (memcmp (&instance->my_old_ring_id, &mcast->ring_id,
  1257. sizeof (struct memb_ring_id)) == 0) {
  1258. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1259. "adding msg with seq no %d\n", mcast->seq, mcast->this_seqno);
  1260. regular_message_item.iov_len = recovery_message_item->iov_len;
  1261. res = sq_item_inuse (&instance->regular_sort_queue, mcast->seq);
  1262. if (res == 0) {
  1263. sq_item_add (&instance->regular_sort_queue,
  1264. &regular_message_item, mcast->seq);
  1265. if (mcast->seq > instance->old_ring_state_high_seq_received) {
  1266. instance->old_ring_state_high_seq_received = mcast->seq;
  1267. }
  1268. }
  1269. } else {
  1270. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1271. "-not adding msg with seq no %d\n", mcast->seq);
  1272. }
  1273. }
  1274. }
  1275. /*
  1276. * Change states in the state machine of the membership algorithm
  1277. */
  1278. static void memb_state_operational_enter (struct totemsrp_instance *instance)
  1279. {
  1280. struct in_addr joined_list[PROCESSOR_COUNT_MAX];
  1281. int joined_list_entries = 0;
  1282. struct in_addr left_list[PROCESSOR_COUNT_MAX];
  1283. int left_list_entries = 0;
  1284. int aru_save;
  1285. old_ring_state_reset (instance);
  1286. ring_reset (instance);
  1287. deliver_messages_from_recovery_to_regular (instance);
  1288. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  1289. "Delivering to app %d to %d\n",
  1290. instance->my_high_delivered + 1, instance->old_ring_state_high_seq_received);
  1291. aru_save = instance->my_aru;
  1292. instance->my_aru = instance->old_ring_state_aru;
  1293. messages_deliver_to_app (instance, 0, instance->old_ring_state_high_seq_received);
  1294. /*
  1295. * Calculate joined and left list
  1296. */
  1297. memb_set_subtract (left_list, &left_list_entries,
  1298. instance->my_memb_list, instance->my_memb_entries,
  1299. instance->my_trans_memb_list, instance->my_trans_memb_entries);
  1300. memb_set_subtract (joined_list, &joined_list_entries,
  1301. instance->my_new_memb_list, instance->my_new_memb_entries,
  1302. instance->my_trans_memb_list, instance->my_trans_memb_entries);
  1303. /*
  1304. * Deliver transitional configuration to application
  1305. */
  1306. instance->totemsrp_confchg_fn (TOTEM_CONFIGURATION_TRANSITIONAL,
  1307. instance->my_trans_memb_list, instance->my_trans_memb_entries,
  1308. left_list, left_list_entries,
  1309. 0, 0, &instance->my_ring_id);
  1310. // TODO we need to filter to ensure we only deliver those
  1311. // messages which are part of instance->my_deliver_memb
  1312. messages_deliver_to_app (instance, 1, instance->old_ring_state_high_seq_received);
  1313. instance->my_aru = aru_save;
  1314. /*
  1315. * Deliver regular configuration to application
  1316. */
  1317. instance->totemsrp_confchg_fn (TOTEM_CONFIGURATION_REGULAR,
  1318. instance->my_new_memb_list, instance->my_new_memb_entries,
  1319. 0, 0,
  1320. joined_list, joined_list_entries, &instance->my_ring_id);
  1321. /*
  1322. * Install new membership
  1323. */
  1324. instance->my_memb_entries = instance->my_new_memb_entries;
  1325. memcpy (instance->my_memb_list, instance->my_new_memb_list,
  1326. sizeof (struct in_addr) * instance->my_memb_entries);
  1327. instance->last_released = 0;
  1328. instance->my_set_retrans_flg = 0;
  1329. /*
  1330. * The recovery sort queue now becomes the regular
  1331. * sort queue. It is necessary to copy the state
  1332. * into the regular sort queue.
  1333. */
  1334. sq_copy (&instance->regular_sort_queue, &instance->recovery_sort_queue);
  1335. instance->my_last_aru = 0;
  1336. instance->my_proc_list_entries = instance->my_new_memb_entries;
  1337. memcpy (instance->my_proc_list, instance->my_new_memb_list,
  1338. sizeof (struct in_addr) * instance->my_memb_entries);
  1339. instance->my_failed_list_entries = 0;
  1340. instance->my_high_delivered = instance->my_aru;
  1341. // TODO the recovery messages are leaked
  1342. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1343. "entering OPERATIONAL state.\n");
  1344. instance->memb_state = MEMB_STATE_OPERATIONAL;
  1345. return;
  1346. }
  1347. static void memb_state_gather_enter (struct totemsrp_instance *instance)
  1348. {
  1349. memb_set_merge (&instance->my_id.sin_addr, 1,
  1350. instance->my_proc_list, &instance->my_proc_list_entries);
  1351. memb_join_message_send (instance);
  1352. /*
  1353. * Restart the join timeout
  1354. */
  1355. poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1356. poll_timer_add (*instance->totemsrp_poll_handle,
  1357. instance->timeout_state_gather_join,
  1358. (void *)instance,
  1359. memb_timer_function_state_gather,
  1360. &instance->memb_timer_state_gather_join_timeout);
  1361. /*
  1362. * Restart the consensus timeout
  1363. */
  1364. poll_timer_delete (*instance->totemsrp_poll_handle,
  1365. instance->memb_timer_state_gather_consensus_timeout);
  1366. poll_timer_add (*instance->totemsrp_poll_handle,
  1367. instance->timeout_state_gather_consensus,
  1368. (void *)instance,
  1369. memb_timer_function_gather_consensus_timeout,
  1370. &instance->memb_timer_state_gather_consensus_timeout);
  1371. /*
  1372. * Cancel the token loss and token retransmission timeouts
  1373. */
  1374. cancel_token_retransmit_timeout (instance); // REVIEWED
  1375. cancel_token_timeout (instance); // REVIEWED
  1376. cancel_merge_detect_timeout (instance);
  1377. memb_consensus_reset (instance);
  1378. memb_consensus_set (instance, &instance->my_id.sin_addr);
  1379. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1380. "entering GATHER state.\n");
  1381. instance->memb_state = MEMB_STATE_GATHER;
  1382. return;
  1383. }
  1384. static void timer_function_token_retransmit_timeout (void *data);
  1385. static void memb_state_commit_enter (
  1386. struct totemsrp_instance *instance,
  1387. struct memb_commit_token *commit_token)
  1388. {
  1389. ring_save (instance);
  1390. old_ring_state_save (instance);
  1391. memb_state_commit_token_update (instance, commit_token);
  1392. memb_state_commit_token_send (instance, commit_token);
  1393. memb_ring_id_store (instance, commit_token);
  1394. poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
  1395. instance->memb_timer_state_gather_join_timeout = 0;
  1396. poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_consensus_timeout);
  1397. instance->memb_timer_state_gather_consensus_timeout = 0;
  1398. reset_token_timeout (instance); // REVIEWED
  1399. reset_token_retransmit_timeout (instance); // REVIEWED
  1400. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1401. "entering COMMIT state.\n");
  1402. instance->memb_state = MEMB_STATE_COMMIT;
  1403. return;
  1404. }
  1405. static void memb_state_recovery_enter (
  1406. struct totemsrp_instance *instance,
  1407. struct memb_commit_token *commit_token)
  1408. {
  1409. int i;
  1410. #ifdef COMPILE_OUT
  1411. int local_received_flg = 1;
  1412. #endif
  1413. unsigned int low_ring_aru;
  1414. unsigned int messages_originated = 0;
  1415. instance->my_high_ring_delivered = 0;
  1416. sq_reinit (&instance->recovery_sort_queue, 0);
  1417. queue_reinit (&instance->retrans_message_queue);
  1418. low_ring_aru = instance->old_ring_state_high_seq_received;
  1419. memb_state_commit_token_send (instance, commit_token);
  1420. instance->my_token_seq = -1;
  1421. /*
  1422. * Build regular configuration
  1423. */
  1424. instance->my_new_memb_entries = commit_token->addr_entries;
  1425. memcpy (instance->my_new_memb_list, commit_token->addr,
  1426. sizeof (struct in_addr) * instance->my_new_memb_entries);
  1427. /*
  1428. * Build transitional configuration
  1429. */
  1430. memb_set_and (instance->my_new_memb_list, instance->my_new_memb_entries,
  1431. instance->my_memb_list, instance->my_memb_entries,
  1432. instance->my_trans_memb_list, &instance->my_trans_memb_entries);
  1433. for (i = 0; i < instance->my_new_memb_entries; i++) {
  1434. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1435. "position [%d] member %s:\n", i, inet_ntoa (commit_token->addr[i]));
  1436. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1437. "previous ring seq %lld rep %s\n",
  1438. commit_token->memb_list[i].ring_id.seq,
  1439. inet_ntoa (commit_token->memb_list[i].ring_id.rep));
  1440. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1441. "aru %d high delivered %d received flag %d\n",
  1442. commit_token->memb_list[i].aru,
  1443. commit_token->memb_list[i].high_delivered,
  1444. commit_token->memb_list[i].received_flg);
  1445. assert (commit_token->memb_list[i].ring_id.rep.s_addr);
  1446. }
  1447. /*
  1448. * Determine if any received flag is false
  1449. */
  1450. #ifdef COMPILE_OUT
  1451. for (i = 0; i < commit_token->addr_entries; i++) {
  1452. if (memb_set_subset (&instance->my_new_memb_list[i], 1,
  1453. instance->my_trans_memb_list, instance->my_trans_memb_entries) &&
  1454. commit_token->memb_list[i].received_flg == 0) {
  1455. #endif
  1456. instance->my_deliver_memb_entries = instance->my_trans_memb_entries;
  1457. memcpy (instance->my_deliver_memb_list, instance->my_trans_memb_list,
  1458. sizeof (struct in_addr) * instance->my_trans_memb_entries);
  1459. #ifdef COMPILE_OUT
  1460. local_received_flg = 0;
  1461. break;
  1462. }
  1463. }
  1464. #endif
  1465. // if (local_received_flg == 0) {
  1466. /*
  1467. * Calculate my_low_ring_aru, instance->my_high_ring_delivered for the transitional membership
  1468. */
  1469. for (i = 0; i < commit_token->addr_entries; i++) {
  1470. printf ("comparing %d old ring %s.%lld with commit ring %s.%lld.\n", i,
  1471. inet_ntoa (instance->my_old_ring_id.rep), instance->my_old_ring_id.seq,
  1472. inet_ntoa (commit_token->memb_list[i].ring_id.rep),
  1473. commit_token->memb_list[i].ring_id.seq);
  1474. printf ("memb set subset %d\n",
  1475. memb_set_subset (&instance->my_new_memb_list[i], 1, instance->my_deliver_memb_list, instance->my_deliver_memb_entries));
  1476. if (memb_set_subset (&instance->my_new_memb_list[i], 1,
  1477. instance->my_deliver_memb_list,
  1478. instance->my_deliver_memb_entries) &&
  1479. memcmp (&instance->my_old_ring_id,
  1480. &commit_token->memb_list[i].ring_id,
  1481. sizeof (struct memb_ring_id)) == 0) {
  1482. if (low_ring_aru == 0 ||
  1483. low_ring_aru > commit_token->memb_list[i].aru) {
  1484. low_ring_aru = commit_token->memb_list[i].aru;
  1485. }
  1486. if (instance->my_high_ring_delivered < commit_token->memb_list[i].high_delivered) {
  1487. instance->my_high_ring_delivered = commit_token->memb_list[i].high_delivered;
  1488. }
  1489. }
  1490. }
  1491. assert (low_ring_aru != 0xffffffff);
  1492. /*
  1493. * Cpy all old ring messages to instance->retrans_message_queue
  1494. */
  1495. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1496. "copying all old ring messages from %d-%d.\n",
  1497. low_ring_aru + 1, instance->old_ring_state_high_seq_received);
  1498. for (i = low_ring_aru + 1; i <= instance->old_ring_state_high_seq_received; i++) {
  1499. struct sort_queue_item *sort_queue_item;
  1500. struct message_item message_item;
  1501. void *ptr;
  1502. int res;
  1503. res = sq_item_get (&instance->regular_sort_queue, i, &ptr);
  1504. if (res != 0) {
  1505. printf ("-not copying %d-\n", i);
  1506. continue;
  1507. }
  1508. printf ("copying %d\n", i);
  1509. sort_queue_item = ptr;
  1510. assert (sort_queue_item->iov_len > 0);
  1511. assert (sort_queue_item->iov_len <= MAXIOVS);
  1512. messages_originated++;
  1513. memset (&message_item, 0, sizeof (struct message_item));
  1514. // TODO LEAK
  1515. message_item.mcast = malloc (sizeof (struct mcast));
  1516. assert (message_item.mcast);
  1517. memcpy (message_item.mcast, sort_queue_item->iovec[0].iov_base,
  1518. sizeof (struct mcast));
  1519. memcpy (&message_item.mcast->ring_id, &instance->my_ring_id,
  1520. sizeof (struct memb_ring_id));
  1521. message_item.mcast->header.encapsulated = 1;
  1522. message_item.iov_len = sort_queue_item->iov_len;
  1523. memcpy (&message_item.iovec, &sort_queue_item->iovec, sizeof (struct iovec) *
  1524. sort_queue_item->iov_len);
  1525. queue_item_add (&instance->retrans_message_queue, &message_item);
  1526. }
  1527. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  1528. "Originated %d messages in RECOVERY.\n", messages_originated);
  1529. // }
  1530. instance->my_aru = 0;
  1531. instance->my_aru_count = 0;
  1532. instance->my_seq_unchanged = 0;
  1533. instance->my_high_seq_received = 0;
  1534. instance->my_install_seq = 0;
  1535. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "entering RECOVERY state.\n");
  1536. reset_token_timeout (instance); // REVIEWED
  1537. reset_token_retransmit_timeout (instance); // REVIEWED
  1538. instance->memb_state = MEMB_STATE_RECOVERY;
  1539. return;
  1540. }
  1541. static void encrypt_and_sign_worker (
  1542. struct totemsrp_instance *instance,
  1543. char *buf,
  1544. int *buf_len,
  1545. struct iovec *iovec,
  1546. int iov_len,
  1547. prng_state *prng_state_in)
  1548. {
  1549. int i;
  1550. char *addr;
  1551. char keys[48];
  1552. struct security_header *header;
  1553. char *hmac_key = &keys[32];
  1554. char *cipher_key = &keys[16];
  1555. char *initial_vector = &keys[0];
  1556. unsigned long len;
  1557. int outlen = 0;
  1558. hmac_state hmac_state;
  1559. prng_state keygen_prng_state;
  1560. prng_state stream_prng_state;
  1561. header = (struct security_header *)buf;
  1562. addr = buf + sizeof (struct security_header);
  1563. memset (keys, 0, sizeof (keys));
  1564. memset (header->salt, 0, sizeof (header->salt));
  1565. #if (defined(ENCRYPTION) || defined(AUTHENITCATION))
  1566. /*
  1567. * Generate MAC, CIPHER, IV keys from private key
  1568. */
  1569. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  1570. sober128_start (&keygen_prng_state);
  1571. sober128_add_entropy (instance->totemsrp_private_key,
  1572. instance->totemsrp_private_key_len,
  1573. &keygen_prng_state);
  1574. sober128_add_entropy (header->salt, sizeof (header->salt),
  1575. &keygen_prng_state);
  1576. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  1577. #endif
  1578. #ifdef ENCRYPTION
  1579. /*
  1580. * Setup stream cipher
  1581. */
  1582. sober128_start (&stream_prng_state);
  1583. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  1584. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  1585. #endif
  1586. /*
  1587. * Copy header of message, then remainder of message, then encrypt it
  1588. */
  1589. memcpy (addr, iovec[0].iov_base + sizeof (struct security_header),
  1590. iovec[0].iov_len - sizeof (struct security_header));
  1591. addr += iovec[0].iov_len - sizeof (struct security_header);
  1592. outlen += iovec[0].iov_len;
  1593. for (i = 1; i < iov_len; i++) {
  1594. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  1595. addr += iovec[i].iov_len;
  1596. outlen += iovec[i].iov_len;
  1597. }
  1598. /*
  1599. * Encrypt message by XORing stream cipher data
  1600. */
  1601. #ifdef ENCRYPTION
  1602. sober128_read (buf + sizeof (struct security_header),
  1603. outlen - sizeof (struct security_header),
  1604. &stream_prng_state);
  1605. #endif
  1606. #ifdef AUTHENTICATION
  1607. memset (&hmac_state, 0, sizeof (hmac_state));
  1608. /*
  1609. * Sign the contents of the message with the hmac key and store signature in message
  1610. */
  1611. hmac_init (&hmac_state, DIGEST_SHA1, hmac_key, 16);
  1612. hmac_process (&hmac_state,
  1613. buf + HMAC_HASH_SIZE,
  1614. outlen - HMAC_HASH_SIZE);
  1615. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  1616. hmac_done (&hmac_state, header->hash_digest, &len);
  1617. #endif
  1618. *buf_len = outlen;
  1619. }
  1620. static void encrypt_and_sign (
  1621. struct totemsrp_instance *instance,
  1622. struct iovec *iovec,
  1623. int iov_len)
  1624. {
  1625. char *addr = instance->iov_encrypted.iov_base +
  1626. sizeof (struct security_header);
  1627. int i;
  1628. char keys[48];
  1629. struct security_header *header = instance->iov_encrypted.iov_base;
  1630. prng_state keygen_prng_state;
  1631. prng_state stream_prng_state;
  1632. char *hmac_key = &keys[32];
  1633. char *cipher_key = &keys[16];
  1634. char *initial_vector = &keys[0];
  1635. unsigned long len;
  1636. instance->iov_encrypted.iov_len = 0;
  1637. memset (keys, 0, sizeof (keys));
  1638. memset (header->salt, 0, sizeof (header->salt));
  1639. #if (defined(ENCRYPTION) || defined(AUTHENITCATION))
  1640. /*
  1641. * Generate MAC, CIPHER, IV keys from private key
  1642. */
  1643. sober128_read (header->salt, sizeof (header->salt),
  1644. &instance->totemsrp_prng_state);
  1645. sober128_start (&keygen_prng_state);
  1646. sober128_add_entropy (instance->totemsrp_private_key,
  1647. instance->totemsrp_private_key_len, &keygen_prng_state);
  1648. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  1649. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  1650. #endif
  1651. #ifdef ENCRYPTION
  1652. /*
  1653. * Setup stream cipher
  1654. */
  1655. sober128_start (&stream_prng_state);
  1656. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  1657. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  1658. #endif
  1659. #ifdef CODE_COVERAGE_COMPILE_OUT
  1660. if (log_digest) {
  1661. printf ("new encryption\n");
  1662. print_digest ("salt", header->salt);
  1663. print_digest ("initial_vector", initial_vector);
  1664. print_digest ("cipher_key", cipher_key);
  1665. print_digest ("hmac_key", hmac_key);
  1666. }
  1667. #endif
  1668. /*
  1669. * Copy header of message, then remainder of message, then encrypt it
  1670. */
  1671. memcpy (addr, iovec[0].iov_base + sizeof (struct security_header),
  1672. iovec[0].iov_len - sizeof (struct security_header));
  1673. addr += iovec[0].iov_len - sizeof (struct security_header);
  1674. instance->iov_encrypted.iov_len += iovec[0].iov_len;
  1675. for (i = 1; i < iov_len; i++) {
  1676. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  1677. addr += iovec[i].iov_len;
  1678. instance->iov_encrypted.iov_len += iovec[i].iov_len;
  1679. }
  1680. /*
  1681. * Encrypt message by XORing stream cipher data
  1682. */
  1683. #ifdef ENCRYPTION
  1684. sober128_read (instance->iov_encrypted.iov_base + sizeof (struct security_header),
  1685. instance->iov_encrypted.iov_len - sizeof (struct security_header),
  1686. &stream_prng_state);
  1687. #endif
  1688. #ifdef AUTHENTICATION
  1689. memset (&instance->totemsrp_hmac_state, 0, sizeof (hmac_state));
  1690. /*
  1691. * Sign the contents of the message with the hmac key and store signature in message
  1692. */
  1693. hmac_init (&instance->totemsrp_hmac_state, DIGEST_SHA1, hmac_key, 16);
  1694. hmac_process (&instance->totemsrp_hmac_state,
  1695. instance->iov_encrypted.iov_base + HMAC_HASH_SIZE,
  1696. instance->iov_encrypted.iov_len - HMAC_HASH_SIZE);
  1697. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  1698. hmac_done (&instance->totemsrp_hmac_state, header->hash_digest, &len);
  1699. #endif
  1700. #ifdef COMPILE_OUT
  1701. print_digest ("initial_vector", initial_vector);
  1702. print_digest ("cipher_key", cipher_key);
  1703. print_digest ("hmac_key", hmac_key);
  1704. print_digest ("salt", header->salt);
  1705. print_digest ("sent digest", header->hash_digest);
  1706. #endif
  1707. }
  1708. /*
  1709. * Only designed to work with a message with one iov
  1710. */
  1711. static int authenticate_and_decrypt (
  1712. struct totemsrp_instance *instance,
  1713. struct iovec *iov)
  1714. {
  1715. char keys[48];
  1716. struct security_header *header = iov[0].iov_base;
  1717. prng_state keygen_prng_state;
  1718. prng_state stream_prng_state;
  1719. char *hmac_key = &keys[32];
  1720. char *cipher_key = &keys[16];
  1721. char *initial_vector = &keys[0];
  1722. char digest_comparison[HMAC_HASH_SIZE];
  1723. unsigned long len;
  1724. int res = 0;
  1725. instance->iov_encrypted.iov_len = 0;
  1726. #ifdef COMPILE_OUT
  1727. printf ("Decryption message\n");
  1728. print_msg (header, iov[0].iov_len);
  1729. #endif
  1730. #if (defined(ENCRYPTION) || defined(AUTHENITCATION))
  1731. /*
  1732. * Generate MAC, CIPHER, IV keys from private key
  1733. */
  1734. memset (keys, 0, sizeof (keys));
  1735. sober128_start (&keygen_prng_state);
  1736. sober128_add_entropy (instance->totemsrp_private_key,
  1737. instance->totemsrp_private_key_len, &keygen_prng_state);
  1738. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  1739. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  1740. #endif
  1741. #ifdef ENCRYPTION
  1742. /*
  1743. * Setup stream cipher
  1744. */
  1745. sober128_start (&stream_prng_state);
  1746. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  1747. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  1748. #endif
  1749. #ifdef CODE_COVERAGE_COMPILE_OUT
  1750. if (log_digest) {
  1751. printf ("New decryption\n");
  1752. print_digest ("salt", header->salt);
  1753. print_digest ("initial_vector", initial_vector);
  1754. print_digest ("cipher_key", cipher_key);
  1755. print_digest ("hmac_key", hmac_key);
  1756. }
  1757. #endif
  1758. #ifdef AUTHENTICATION
  1759. /*
  1760. * Authenticate contents of message
  1761. */
  1762. hmac_init (&instance->totemsrp_hmac_state, DIGEST_SHA1, hmac_key, 16);
  1763. hmac_process (&instance->totemsrp_hmac_state,
  1764. iov->iov_base + HMAC_HASH_SIZE,
  1765. iov->iov_len - HMAC_HASH_SIZE);
  1766. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  1767. assert (HMAC_HASH_SIZE >= len);
  1768. hmac_done (&instance->totemsrp_hmac_state, digest_comparison, &len);
  1769. #ifdef PRINTDIGESTS
  1770. print_digest ("received digest", header->hash_digest);
  1771. print_digest ("calculated digest", digest_comparison);
  1772. #endif
  1773. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  1774. #ifdef CODE_COVERAGE_COMPILE_OUT
  1775. print_digest ("initial_vector", initial_vector);
  1776. print_digest ("cipher_key", cipher_key);
  1777. print_digest ("hmac_key", hmac_key);
  1778. print_digest ("salt", header->salt);
  1779. print_digest ("sent digest", header->hash_digest);
  1780. print_digest ("calculated digest", digest_comparison);
  1781. printf ("received message size %d\n", iov->iov_len);
  1782. #endif
  1783. instance->totemsrp_log_printf (instance->totemsrp_log_level_security, "Received message has invalid digest... ignoring.\n");
  1784. res = -1;
  1785. return (-1);
  1786. }
  1787. #endif /* AUTHENTICATION */
  1788. /*
  1789. * Decrypt the contents of the message with the cipher key
  1790. */
  1791. #ifdef ENCRYPTION
  1792. sober128_read (iov->iov_base + sizeof (struct security_header),
  1793. iov->iov_len - sizeof (struct security_header),
  1794. &stream_prng_state);
  1795. #endif
  1796. return (res);
  1797. return (0);
  1798. }
  1799. int totemsrp_new_msg_signal (totemsrp_handle handle)
  1800. {
  1801. struct totemsrp_instance *instance;
  1802. SaErrorT error;
  1803. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  1804. (void *)&instance);
  1805. if (error != SA_OK) {
  1806. goto error_exit;
  1807. }
  1808. token_hold_cancel_send (instance);
  1809. saHandleInstancePut (&totemsrp_instance_database, handle);
  1810. return (0);
  1811. error_exit:
  1812. return (-1);
  1813. }
  1814. int totemsrp_mcast (
  1815. totemsrp_handle handle,
  1816. struct iovec *iovec,
  1817. int iov_len,
  1818. int guarantee)
  1819. {
  1820. int i;
  1821. int j;
  1822. struct message_item message_item;
  1823. struct totemsrp_instance *instance;
  1824. SaErrorT error;
  1825. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  1826. (void *)&instance);
  1827. if (error != SA_OK) {
  1828. goto error_exit;
  1829. }
  1830. if (queue_is_full (&instance->new_message_queue)) {
  1831. return (-1);
  1832. }
  1833. for (j = 0, i = 0; i < iov_len; i++) {
  1834. j+= iovec[i].iov_len;
  1835. }
  1836. memset (&message_item, 0, sizeof (struct message_item));
  1837. /*
  1838. * Allocate pending item
  1839. */
  1840. // TODO LEAK
  1841. message_item.mcast = malloc (sizeof (struct mcast));
  1842. if (message_item.mcast == 0) {
  1843. goto error_mcast;
  1844. }
  1845. /*
  1846. * Set mcast header
  1847. */
  1848. message_item.mcast->header.type = MESSAGE_TYPE_MCAST;
  1849. message_item.mcast->header.endian_detector = ENDIAN_LOCAL;
  1850. message_item.mcast->header.encapsulated = 2;
  1851. message_item.mcast->guarantee = guarantee;
  1852. message_item.mcast->source.s_addr = instance->my_id.sin_addr.s_addr;
  1853. for (i = 0; i < iov_len; i++) {
  1854. // TODO LEAK
  1855. message_item.iovec[i].iov_base = malloc (iovec[i].iov_len);
  1856. if (message_item.iovec[i].iov_base == 0) {
  1857. goto error_iovec;
  1858. }
  1859. memcpy (message_item.iovec[i].iov_base, iovec[i].iov_base,
  1860. iovec[i].iov_len);
  1861. message_item.iovec[i].iov_len = iovec[i].iov_len;
  1862. }
  1863. message_item.iov_len = iov_len;
  1864. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug, "mcasted message added to pending queue\n");
  1865. queue_item_add (&instance->new_message_queue, &message_item);
  1866. saHandleInstancePut (&totemsrp_instance_database, handle);
  1867. return (0);
  1868. error_iovec:
  1869. saHandleInstancePut (&totemsrp_instance_database, handle);
  1870. for (j = 0; j < i; j++) {
  1871. free (message_item.iovec[j].iov_base);
  1872. }
  1873. return (-1);
  1874. error_mcast:
  1875. saHandleInstancePut (&totemsrp_instance_database, handle);
  1876. error_exit:
  1877. return (0);
  1878. }
  1879. /*
  1880. * Determine if there is room to queue a new message
  1881. */
  1882. int totemsrp_avail (totemsrp_handle handle)
  1883. {
  1884. int avail;
  1885. struct totemsrp_instance *instance;
  1886. SaErrorT error;
  1887. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  1888. (void *)&instance);
  1889. if (error != SA_OK) {
  1890. goto error_exit;
  1891. }
  1892. queue_avail (&instance->new_message_queue, &avail);
  1893. saHandleInstancePut (&totemsrp_instance_database, handle);
  1894. return (avail);
  1895. error_exit:
  1896. return (0);
  1897. }
  1898. static int netif_determine (
  1899. struct totemsrp_instance *instance,
  1900. struct sockaddr_in *bindnet,
  1901. struct sockaddr_in *bound_to,
  1902. int *interface_up)
  1903. {
  1904. struct sockaddr_in *sockaddr_in;
  1905. int id_fd;
  1906. struct ifconf ifc;
  1907. int numreqs = 0;
  1908. int res;
  1909. int i;
  1910. in_addr_t mask_addr;
  1911. *interface_up = 0;
  1912. /*
  1913. * Generate list of local interfaces in ifc.ifc_req structure
  1914. */
  1915. id_fd = socket (AF_INET, SOCK_STREAM, 0);
  1916. ifc.ifc_buf = 0;
  1917. do {
  1918. numreqs += 32;
  1919. ifc.ifc_len = sizeof (struct ifreq) * numreqs;
  1920. ifc.ifc_buf = (void *)realloc(ifc.ifc_buf, ifc.ifc_len);
  1921. res = ioctl (id_fd, SIOCGIFCONF, &ifc);
  1922. if (res < 0) {
  1923. close (id_fd);
  1924. return -1;
  1925. }
  1926. } while (ifc.ifc_len == sizeof (struct ifreq) * numreqs);
  1927. res = -1;
  1928. /*
  1929. * Find interface address to bind to
  1930. */
  1931. for (i = 0; i < ifc.ifc_len / sizeof (struct ifreq); i++) {
  1932. sockaddr_in = (struct sockaddr_in *)&ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_addr;
  1933. mask_addr = inet_addr ("255.255.255.0");
  1934. if ((sockaddr_in->sin_family == AF_INET) &&
  1935. (sockaddr_in->sin_addr.s_addr & mask_addr) ==
  1936. (bindnet->sin_addr.s_addr & mask_addr)) {
  1937. bound_to->sin_addr.s_addr = sockaddr_in->sin_addr.s_addr;
  1938. res = i;
  1939. if (ioctl(id_fd, SIOCGIFFLAGS, &ifc.ifc_ifcu.ifcu_req[i]) < 0) {
  1940. printf ("couldn't do ioctl\n");
  1941. }
  1942. *interface_up = ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_flags & IFF_UP;
  1943. break; /* for */
  1944. }
  1945. }
  1946. free (ifc.ifc_buf);
  1947. close (id_fd);
  1948. return (res);
  1949. }
  1950. static int loopback_determine (struct sockaddr_in *bound_to)
  1951. {
  1952. bound_to->sin_addr.s_addr = LOCALHOST_IP;
  1953. if (&bound_to->sin_addr.s_addr == 0) {
  1954. return -1;
  1955. }
  1956. return 1;
  1957. }
  1958. /*
  1959. * If the interface is up, the sockets for totem are built. If the interface is down
  1960. * this function is requeued in the timer list to retry building the sockets later.
  1961. */
  1962. static void timer_function_netif_check_timeout (
  1963. void *data)
  1964. {
  1965. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  1966. int res;
  1967. int interface_no;
  1968. int interface_up;
  1969. /*
  1970. * Build sockets for every interface
  1971. */
  1972. for (interface_no = 0; interface_no < instance->totemsrp_interface_count; interface_no++) {
  1973. netif_determine (instance,
  1974. &instance->totemsrp_interfaces[interface_no].bindnet,
  1975. &instance->totemsrp_interfaces[interface_no].boundto,
  1976. &interface_up);
  1977. if (((instance->netif_bind_state & BIND_STATE_LOOPBACK) && (!interface_up))
  1978. || ((instance->netif_bind_state & BIND_STATE_REGULAR) && (interface_up))) {
  1979. break;
  1980. }
  1981. instance->totemsrp_log_printf(instance->totemsrp_log_level_debug,"network interface UP %s\n",
  1982. inet_ntoa (instance->totemsrp_interfaces[interface_no].boundto.sin_addr));
  1983. if (instance->totemsrp_sockets[interface_no].mcast > 0) {
  1984. close (instance->totemsrp_sockets[interface_no].mcast);
  1985. poll_dispatch_delete (*instance->totemsrp_poll_handle,
  1986. instance->totemsrp_sockets[interface_no].mcast);
  1987. }
  1988. if (instance->totemsrp_sockets[interface_no].token > 0) {
  1989. close (instance->totemsrp_sockets[interface_no].token);
  1990. poll_dispatch_delete (*instance->totemsrp_poll_handle,
  1991. instance->totemsrp_sockets[interface_no].token);
  1992. }
  1993. if (!interface_up) {
  1994. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,"Interface is down binding to LOOPBACK addr.\n");
  1995. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1996. res = totemsrp_build_sockets_loopback(instance,
  1997. &instance->sockaddr_in_mcast,
  1998. &instance->totemsrp_interfaces[interface_no].bindnet,
  1999. &instance->totemsrp_sockets[interface_no],
  2000. &instance->totemsrp_interfaces[interface_no].boundto);
  2001. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,"network interface LOCAL %s\n",
  2002. inet_ntoa (instance->totemsrp_interfaces[interface_no].boundto.sin_addr));
  2003. poll_dispatch_add (*instance->totemsrp_poll_handle, instance->totemsrp_sockets[interface_no].token,
  2004. POLLIN, instance, recv_handler, UINT_MAX);
  2005. continue;
  2006. }
  2007. instance->netif_bind_state = BIND_STATE_REGULAR;
  2008. /*
  2009. * Create and bind the multicast and unicast sockets
  2010. */
  2011. res = totemsrp_build_sockets (instance,
  2012. &instance->sockaddr_in_mcast,
  2013. &instance->totemsrp_interfaces[interface_no].bindnet,
  2014. &instance->totemsrp_sockets[interface_no],
  2015. &instance->totemsrp_interfaces[interface_no].boundto,
  2016. &interface_up);
  2017. poll_dispatch_add (*instance->totemsrp_poll_handle, instance->totemsrp_sockets[interface_no].mcast,
  2018. POLLIN, instance, recv_handler, UINT_MAX);
  2019. poll_dispatch_add (*instance->totemsrp_poll_handle, instance->totemsrp_sockets[interface_no].token,
  2020. POLLIN, instance, recv_handler, UINT_MAX);
  2021. }
  2022. memcpy (&instance->my_id, &instance->totemsrp_interfaces->boundto, sizeof (struct sockaddr_in));
  2023. /*
  2024. * This stuff depends on totemsrp_build_sockets
  2025. */
  2026. if (instance->firstrun == 0) {
  2027. instance->firstrun += 1;
  2028. memcpy (&instance->my_memb_list[0], &instance->totemsrp_interfaces->boundto,
  2029. sizeof (struct sockaddr_in));
  2030. memb_ring_id_create_or_load (instance, &instance->my_ring_id);
  2031. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Created or loaded sequence id %lld.%s for this ring.\n",
  2032. instance->my_ring_id.seq, inet_ntoa (instance->my_ring_id.rep));
  2033. }
  2034. if (interface_up) {
  2035. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  2036. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2037. " The network interface is now up.\n");
  2038. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  2039. memb_state_gather_enter (instance);
  2040. }
  2041. /*
  2042. * If this is a single processor, detect downs which may not
  2043. * be detected by token loss when the interface is downed
  2044. */
  2045. if (instance->my_memb_entries <= 1) {
  2046. poll_timer_add (*instance->totemsrp_poll_handle,
  2047. instance->timeout_downcheck,
  2048. (void *)instance,
  2049. timer_function_netif_check_timeout,
  2050. &instance->timer_netif_check_timeout);
  2051. }
  2052. } else {
  2053. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  2054. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2055. "The network interface is down.\n");
  2056. memb_state_gather_enter (instance);
  2057. }
  2058. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  2059. /*
  2060. * Add a timer to retry building interfaces and request memb_gather_enter
  2061. */
  2062. cancel_token_timeout (instance);
  2063. poll_timer_add (*instance->totemsrp_poll_handle,
  2064. instance->timeout_downcheck,
  2065. (void *)instance,
  2066. timer_function_netif_check_timeout,
  2067. &instance->timer_netif_check_timeout);
  2068. }
  2069. }
  2070. /*
  2071. * Check if an interface is down and reconfigure
  2072. * totemsrp waiting for it to come back up
  2073. */
  2074. static void netif_down_check (struct totemsrp_instance *instance)
  2075. {
  2076. timer_function_netif_check_timeout (instance);
  2077. }
  2078. static int totemsrp_build_sockets_loopback (
  2079. struct totemsrp_instance *instance,
  2080. struct sockaddr_in *sockaddr_mcast,
  2081. struct sockaddr_in *sockaddr_bindnet,
  2082. struct totemsrp_socket *sockets,
  2083. struct sockaddr_in *bound_to)
  2084. {
  2085. struct ip_mreq mreq;
  2086. struct sockaddr_in sockaddr_in;
  2087. int res;
  2088. memset (&mreq, 0, sizeof (struct ip_mreq));
  2089. /*
  2090. * Determine the ip address bound to and the interface name
  2091. */
  2092. res = loopback_determine (bound_to);
  2093. if (res == -1) {
  2094. return (-1);
  2095. }
  2096. /* TODO this should be somewhere else */
  2097. instance->memb_local_sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  2098. instance->memb_local_sockaddr_in.sin_family = AF_INET;
  2099. instance->memb_local_sockaddr_in.sin_port = sockaddr_mcast->sin_port;
  2100. sockaddr_in.sin_family = AF_INET;
  2101. sockaddr_in.sin_port = sockaddr_mcast->sin_port;
  2102. /*
  2103. * Setup unicast socket
  2104. */
  2105. sockets->token = socket (AF_INET, SOCK_DGRAM, 0);
  2106. if (sockets->token == -1) {
  2107. perror ("socket2");
  2108. return (-1);
  2109. }
  2110. /*
  2111. * Bind to unicast socket used for token send/receives
  2112. * This has the side effect of binding to the correct interface
  2113. */
  2114. sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  2115. res = bind (sockets->token, (struct sockaddr *)&sockaddr_in,
  2116. sizeof (struct sockaddr_in));
  2117. if (res == -1) {
  2118. perror ("bind2 failed");
  2119. return (-1);
  2120. }
  2121. memcpy(&instance->sockaddr_in_mcast, &sockaddr_in,
  2122. sizeof(struct sockaddr_in));
  2123. sockets->mcast = sockets->token;
  2124. return (0);
  2125. }
  2126. static int totemsrp_build_sockets (
  2127. struct totemsrp_instance *instance,
  2128. struct sockaddr_in *sockaddr_mcast,
  2129. struct sockaddr_in *sockaddr_bindnet,
  2130. struct totemsrp_socket *sockets,
  2131. struct sockaddr_in *bound_to,
  2132. int *interface_up)
  2133. {
  2134. struct ip_mreq mreq;
  2135. struct sockaddr_in sockaddr_in;
  2136. char flag;
  2137. int res;
  2138. memset (&mreq, 0, sizeof (struct ip_mreq));
  2139. /*
  2140. * Determine the ip address bound to and the interface name
  2141. */
  2142. res = netif_determine (instance,
  2143. sockaddr_bindnet,
  2144. bound_to,
  2145. interface_up);
  2146. if (res == -1) {
  2147. return (-1);
  2148. }
  2149. /* TODO this should be somewhere else */
  2150. instance->memb_local_sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  2151. instance->memb_local_sockaddr_in.sin_family = AF_INET;
  2152. instance->memb_local_sockaddr_in.sin_port = sockaddr_mcast->sin_port;
  2153. /*
  2154. * Create multicast socket
  2155. */
  2156. sockets->mcast = socket (AF_INET, SOCK_DGRAM, 0);
  2157. if (sockets->mcast == -1) {
  2158. perror ("socket");
  2159. return (-1);
  2160. }
  2161. if (setsockopt (sockets->mcast, SOL_IP, IP_MULTICAST_IF,
  2162. &bound_to->sin_addr, sizeof (struct in_addr)) < 0) {
  2163. instance->totemsrp_log_printf (instance->totemsrp_log_level_warning, "Could not bind to device for multicast, group messaging may not work properly. (%s)\n", strerror (errno));
  2164. }
  2165. /*
  2166. * Bind to multicast socket used for multicast send/receives
  2167. */
  2168. sockaddr_in.sin_family = AF_INET;
  2169. sockaddr_in.sin_addr.s_addr = sockaddr_mcast->sin_addr.s_addr;
  2170. sockaddr_in.sin_port = sockaddr_mcast->sin_port;
  2171. res = bind (sockets->mcast, (struct sockaddr *)&sockaddr_in,
  2172. sizeof (struct sockaddr_in));
  2173. if (res == -1) {
  2174. perror ("bind failed");
  2175. return (-1);
  2176. }
  2177. /*
  2178. * Setup unicast socket
  2179. */
  2180. sockets->token = socket (AF_INET, SOCK_DGRAM, 0);
  2181. if (sockets->token == -1) {
  2182. perror ("socket2");
  2183. return (-1);
  2184. }
  2185. /*
  2186. * Bind to unicast socket used for token send/receives
  2187. * This has the side effect of binding to the correct interface
  2188. */
  2189. sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  2190. res = bind (sockets->token, (struct sockaddr *)&sockaddr_in,
  2191. sizeof (struct sockaddr_in));
  2192. if (res == -1) {
  2193. perror ("bind2 failed");
  2194. return (-1);
  2195. }
  2196. #ifdef CONFIG_USE_BROADCAST
  2197. /* This config option doesn't work */
  2198. {
  2199. int on = 1;
  2200. setsockopt (sockets->mcast, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof (on));
  2201. }
  2202. #else
  2203. /*
  2204. * Join group membership on socket
  2205. */
  2206. mreq.imr_multiaddr.s_addr = sockaddr_mcast->sin_addr.s_addr;
  2207. mreq.imr_interface.s_addr = bound_to->sin_addr.s_addr;
  2208. res = setsockopt (sockets->mcast, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  2209. &mreq, sizeof (mreq));
  2210. if (res == -1) {
  2211. perror ("join multicast group failed");
  2212. return (-1);
  2213. }
  2214. #endif
  2215. /*
  2216. * Turn on multicast loopback
  2217. */
  2218. flag = 1;
  2219. res = setsockopt (sockets->mcast, IPPROTO_IP, IP_MULTICAST_LOOP,
  2220. &flag, sizeof (flag));
  2221. if (res == -1) {
  2222. perror ("turn off loopback");
  2223. return (-1);
  2224. }
  2225. return (0);
  2226. }
  2227. /*
  2228. * Misc Management
  2229. */
  2230. static int in_addr_compare (const void *a, const void *b) {
  2231. struct in_addr *in_addr_a = (struct in_addr *)a;
  2232. struct in_addr *in_addr_b = (struct in_addr *)b;
  2233. return (in_addr_a->s_addr > in_addr_b->s_addr);
  2234. }
  2235. /*
  2236. * ORF Token Management
  2237. */
  2238. /*
  2239. * Recast message to mcast group if it is available
  2240. */
  2241. static int orf_token_remcast (
  2242. struct totemsrp_instance *instance,
  2243. int seq)
  2244. {
  2245. struct msghdr msg_mcast;
  2246. struct sort_queue_item *sort_queue_item;
  2247. int res;
  2248. struct mcast *mcast;
  2249. void *ptr;
  2250. struct sq *sort_queue;
  2251. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2252. sort_queue = &instance->recovery_sort_queue;
  2253. } else {
  2254. sort_queue = &instance->regular_sort_queue;
  2255. }
  2256. /*
  2257. * Get RTR item at seq, if not available, return
  2258. */
  2259. res = sq_item_get (sort_queue, seq, &ptr);
  2260. if (res != 0) {
  2261. return -1;
  2262. }
  2263. sort_queue_item = ptr;
  2264. mcast = (struct mcast *)sort_queue_item->iovec[0].iov_base;
  2265. encrypt_and_sign (instance, sort_queue_item->iovec, sort_queue_item->iov_len);
  2266. /*
  2267. * Build multicast message
  2268. */
  2269. msg_mcast.msg_name = (caddr_t)&instance->sockaddr_in_mcast;
  2270. msg_mcast.msg_namelen = sizeof (struct sockaddr_in);
  2271. msg_mcast.msg_iov = &instance->iov_encrypted;
  2272. msg_mcast.msg_iovlen = 1;
  2273. msg_mcast.msg_control = 0;
  2274. msg_mcast.msg_controllen = 0;
  2275. msg_mcast.msg_flags = 0;
  2276. /*
  2277. * Multicast message
  2278. */
  2279. res = sendmsg (instance->totemsrp_sockets[0].mcast, &msg_mcast, MSG_NOSIGNAL | MSG_DONTWAIT);
  2280. if (res == -1) {
  2281. return (-1);
  2282. }
  2283. instance->stats_sent += res;
  2284. return (0);
  2285. }
  2286. /*
  2287. * Free all freeable messages from ring
  2288. */
  2289. static int messages_free (
  2290. struct totemsrp_instance *instance,
  2291. int token_aru)
  2292. {
  2293. struct sort_queue_item *regular_message;
  2294. int i, j;
  2295. int res;
  2296. int log_release = 0;
  2297. int release_to;
  2298. release_to = token_aru;
  2299. if (release_to > instance->my_last_aru) {
  2300. release_to = instance->my_last_aru;
  2301. }
  2302. if (release_to > instance->my_high_delivered) {
  2303. release_to = instance->my_high_delivered;
  2304. }
  2305. /*
  2306. * Release retransmit list items if group aru indicates they are transmitted
  2307. */
  2308. for (i = instance->last_released; i <= release_to; i++) {
  2309. void *ptr;
  2310. res = sq_item_get (&instance->regular_sort_queue, i, &ptr);
  2311. if (res == 0) {
  2312. regular_message = ptr;
  2313. for (j = 0; j < regular_message->iov_len; j++) {
  2314. free (regular_message->iovec[j].iov_base);
  2315. }
  2316. }
  2317. sq_items_release (&instance->regular_sort_queue, i);
  2318. instance->last_released = i + 1;
  2319. log_release = 1;
  2320. }
  2321. if (log_release) {
  2322. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2323. "releasing messages up to and including %d\n", release_to);
  2324. }
  2325. return (0);
  2326. }
  2327. static void update_aru (
  2328. struct totemsrp_instance *instance)
  2329. {
  2330. int i;
  2331. int res;
  2332. struct sq *sort_queue;
  2333. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2334. sort_queue = &instance->recovery_sort_queue;
  2335. } else {
  2336. sort_queue = &instance->regular_sort_queue;
  2337. }
  2338. for (i = instance->my_aru + 1; i <= instance->my_high_seq_received; i++) {
  2339. void *ptr;
  2340. res = sq_item_get (sort_queue, i, &ptr);
  2341. /*
  2342. * If hole, stop assembly
  2343. */
  2344. if (res != 0) {
  2345. break;
  2346. }
  2347. instance->my_aru = i;
  2348. }
  2349. // instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2350. // "setting received flag to FALSE %d %d\n",
  2351. // instance->my_aru, instance->my_high_seq_received);
  2352. instance->my_received_flg = 0;
  2353. if (instance->my_aru == instance->my_high_seq_received) {
  2354. // instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2355. // "setting received flag to TRUE %d %d\n",
  2356. // instance->my_aru, instance->my_high_seq_received);
  2357. instance->my_received_flg = 1;
  2358. }
  2359. }
  2360. static void orf_token_mcast_worker_fn (void *thread_state, void *work_item)
  2361. {
  2362. struct mcast_worker_fn_work_item *mcast_worker_fn_work_item =
  2363. (struct mcast_worker_fn_work_item *)work_item;
  2364. struct sort_queue_item *sort_queue_item = mcast_worker_fn_work_item->sort_queue_item;
  2365. struct orf_token_mcast_thread_state *orf_token_mcast_thread_state =
  2366. (struct orf_token_mcast_thread_state *)thread_state;
  2367. struct totemsrp_instance *instance = mcast_worker_fn_work_item->instance;
  2368. struct msghdr msg_mcast;
  2369. int res = 0;
  2370. int buf_len;
  2371. struct iovec iov;
  2372. /*
  2373. * Encrypt and digest the message
  2374. */
  2375. encrypt_and_sign_worker (
  2376. instance,
  2377. orf_token_mcast_thread_state->iobuf, &buf_len,
  2378. sort_queue_item->iovec, sort_queue_item->iov_len,
  2379. &orf_token_mcast_thread_state->prng_state);
  2380. iov.iov_base = orf_token_mcast_thread_state->iobuf;
  2381. iov.iov_len = buf_len;
  2382. /*
  2383. * Build multicast message
  2384. */
  2385. msg_mcast.msg_name = &instance->sockaddr_in_mcast;
  2386. msg_mcast.msg_namelen = sizeof (struct sockaddr_in);
  2387. msg_mcast.msg_iov = &iov;
  2388. msg_mcast.msg_iovlen = 1;
  2389. msg_mcast.msg_control = 0;
  2390. msg_mcast.msg_controllen = 0;
  2391. msg_mcast.msg_flags = 0;
  2392. /*
  2393. * Multicast message
  2394. * An error here is recovered by the multicast algorithm
  2395. */
  2396. res = sendmsg (instance->totemsrp_sockets[0].mcast, &msg_mcast, MSG_NOSIGNAL | MSG_DONTWAIT);
  2397. instance->iov_encrypted.iov_len = PACKET_SIZE_MAX;
  2398. if (res > 0) {
  2399. instance->stats_sent += res;
  2400. }
  2401. }
  2402. /*
  2403. * Multicasts pending messages onto the ring (requires orf_token possession)
  2404. */
  2405. static int orf_token_mcast (
  2406. struct totemsrp_instance *instance,
  2407. struct orf_token *token,
  2408. int fcc_mcasts_allowed,
  2409. struct sockaddr_in *system_from)
  2410. {
  2411. struct mcast_worker_fn_work_item work_item;
  2412. struct message_item *message_item = 0;
  2413. struct queue *mcast_queue;
  2414. struct sq *sort_queue;
  2415. struct sort_queue_item sort_queue_item;
  2416. struct sort_queue_item *sort_queue_item_ptr;
  2417. struct mcast *mcast;
  2418. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2419. mcast_queue = &instance->retrans_message_queue;
  2420. sort_queue = &instance->recovery_sort_queue;
  2421. reset_token_retransmit_timeout (instance); // REVIEWED
  2422. } else {
  2423. mcast_queue = &instance->new_message_queue;
  2424. sort_queue = &instance->regular_sort_queue;
  2425. }
  2426. for (instance->fcc_mcast_current = 0; instance->fcc_mcast_current < fcc_mcasts_allowed; instance->fcc_mcast_current++) {
  2427. if (queue_is_empty (mcast_queue)) {
  2428. break;
  2429. }
  2430. message_item = (struct message_item *)queue_item_get (mcast_queue);
  2431. /* preincrement required by algo */
  2432. if (instance->old_ring_state_saved &&
  2433. (instance->memb_state == MEMB_STATE_GATHER ||
  2434. instance->memb_state == MEMB_STATE_COMMIT)) {
  2435. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2436. "not multicasting at seqno is %d\n",
  2437. token->seq);
  2438. return (0);
  2439. }
  2440. message_item->mcast->seq = ++token->seq;
  2441. message_item->mcast->this_seqno = instance->global_seqno++;
  2442. /*
  2443. * Build IO vector
  2444. */
  2445. memset (&sort_queue_item, 0, sizeof (struct sort_queue_item));
  2446. sort_queue_item.iovec[0].iov_base = message_item->mcast;
  2447. sort_queue_item.iovec[0].iov_len = sizeof (struct mcast);
  2448. mcast = sort_queue_item.iovec[0].iov_base;
  2449. memcpy (&sort_queue_item.iovec[1], message_item->iovec,
  2450. message_item->iov_len * sizeof (struct iovec));
  2451. memcpy (&mcast->ring_id, &instance->my_ring_id, sizeof (struct memb_ring_id));
  2452. sort_queue_item.iov_len = message_item->iov_len + 1;
  2453. assert (sort_queue_item.iov_len < 16);
  2454. /*
  2455. * Add message to retransmit queue
  2456. */
  2457. sort_queue_item_ptr = sq_item_add (sort_queue,
  2458. &sort_queue_item, message_item->mcast->seq);
  2459. // XXX printf ("ORIG [%s.%d-%d]\n", inet_ntoa (message_item->mcast->source),
  2460. // XXX message_item->mcast->seq, message_item->mcast->this_seqno);
  2461. /*
  2462. * Only encrypt and send on wire if we are single processor
  2463. */
  2464. #define CONFIG_THREADED_SEND
  2465. #ifdef CONFIG_THREADED_SEND
  2466. work_item.sort_queue_item = sort_queue_item_ptr;
  2467. work_item.instance = instance;
  2468. /*
  2469. * If threads, add message to worker thread queue
  2470. */
  2471. worker_thread_group_work_add (&instance->worker_thread_group_orf_token_mcast,
  2472. &work_item);
  2473. #else
  2474. /*
  2475. * If no threads, execute worker thread directly
  2476. */
  2477. // orf_token_mcast_worker_fn (&worker_data);
  2478. #endif
  2479. /*
  2480. * Delete item from pending queue
  2481. */
  2482. queue_item_remove (mcast_queue);
  2483. }
  2484. assert (instance->fcc_mcast_current < 100);
  2485. /*
  2486. * If messages mcasted, deliver any new messages to totemg
  2487. */
  2488. instance->my_high_seq_received = token->seq;
  2489. update_aru (instance);
  2490. /*
  2491. * Return 1 if more messages are available for single node clusters
  2492. */
  2493. return (instance->fcc_mcast_current);
  2494. }
  2495. /*
  2496. * Remulticasts messages in orf_token's retransmit list (requires orf_token)
  2497. * Modify's orf_token's rtr to include retransmits required by this process
  2498. */
  2499. static int orf_token_rtr (
  2500. struct totemsrp_instance *instance,
  2501. struct orf_token *orf_token,
  2502. int *fcc_allowed)
  2503. {
  2504. int res;
  2505. int i, j;
  2506. int found;
  2507. int total_entries;
  2508. struct sq *sort_queue;
  2509. struct rtr_item *rtr_list;
  2510. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  2511. sort_queue = &instance->recovery_sort_queue;
  2512. } else {
  2513. sort_queue = &instance->regular_sort_queue;
  2514. }
  2515. rtr_list = &orf_token->rtr_list[0];
  2516. if (orf_token->rtr_list_entries) {
  2517. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2518. "Retransmit List %d\n", orf_token->rtr_list_entries);
  2519. for (i = 0; i < orf_token->rtr_list_entries; i++) {
  2520. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  2521. "%d ", rtr_list[i].seq);
  2522. }
  2523. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug, "\n");
  2524. }
  2525. total_entries = orf_token->rtr_list_entries;
  2526. /*
  2527. * Retransmit messages on orf_token's RTR list from RTR queue
  2528. */
  2529. for (instance->fcc_remcast_current = 0, i = 0;
  2530. instance->fcc_remcast_current <= *fcc_allowed && i < orf_token->rtr_list_entries;) {
  2531. /*
  2532. * If this retransmit request isn't from this configuration,
  2533. * try next rtr entry
  2534. */
  2535. if (memcmp (&rtr_list[i].ring_id, &instance->my_ring_id,
  2536. sizeof (struct memb_ring_id)) != 0) {
  2537. i += 1;
  2538. continue;
  2539. }
  2540. assert (rtr_list[i].seq > 0);
  2541. res = orf_token_remcast (instance, rtr_list[i].seq);
  2542. if (res == 0) {
  2543. /*
  2544. * Multicasted message, so no need to copy to new retransmit list
  2545. */
  2546. orf_token->rtr_list_entries -= 1;
  2547. assert (orf_token->rtr_list_entries >= 0);
  2548. memmove (&rtr_list[i], &rtr_list[i + 1],
  2549. sizeof (struct rtr_item) * (orf_token->rtr_list_entries));
  2550. instance->fcc_remcast_current++;
  2551. instance->stats_remcasts++;
  2552. } else {
  2553. i += 1;
  2554. }
  2555. }
  2556. *fcc_allowed = *fcc_allowed - instance->fcc_remcast_current - 1;
  2557. #ifdef COMPILE_OUT
  2558. for (i = 0; i < orf_token->rtr_list_entries; i++) {
  2559. assert (rtr_list_old[index_old].seq != -1);
  2560. }
  2561. #endif
  2562. /*
  2563. * Add messages to retransmit to RTR list
  2564. * but only retry if there is room in the retransmit list
  2565. */
  2566. for (i = instance->my_aru + 1;
  2567. orf_token->rtr_list_entries < RETRANSMIT_ENTRIES_MAX &&
  2568. i <= instance->my_high_seq_received;
  2569. i++) {
  2570. /*
  2571. * Find if a message is missing from this processor
  2572. */
  2573. res = sq_item_inuse (sort_queue, i);
  2574. if (res == 0) {
  2575. /*
  2576. * Determine if missing message is already in retransmit list
  2577. */
  2578. found = 0;
  2579. for (j = 0; j < orf_token->rtr_list_entries; j++) {
  2580. if (i == rtr_list[j].seq) {
  2581. found = 1;
  2582. }
  2583. }
  2584. if (found == 0) {
  2585. /*
  2586. * Missing message not found in current retransmit list so add it
  2587. */
  2588. memcpy (&rtr_list[orf_token->rtr_list_entries].ring_id,
  2589. &instance->my_ring_id, sizeof (struct memb_ring_id));
  2590. rtr_list[orf_token->rtr_list_entries].seq = i;
  2591. orf_token->rtr_list_entries++;
  2592. }
  2593. }
  2594. }
  2595. return (instance->fcc_remcast_current);
  2596. }
  2597. static void token_retransmit (struct totemsrp_instance *instance) {
  2598. struct iovec iovec;
  2599. struct msghdr msg_orf_token;
  2600. int res;
  2601. iovec.iov_base = instance->orf_token_retransmit;
  2602. iovec.iov_len = instance->orf_token_retransmit_size;
  2603. msg_orf_token.msg_name = &instance->next_memb;
  2604. msg_orf_token.msg_namelen = sizeof (struct sockaddr_in);
  2605. msg_orf_token.msg_iov = &iovec;
  2606. msg_orf_token.msg_iovlen = 1;
  2607. msg_orf_token.msg_control = 0;
  2608. msg_orf_token.msg_controllen = 0;
  2609. msg_orf_token.msg_flags = 0;
  2610. res = sendmsg (instance->totemsrp_sockets[0].token, &msg_orf_token, MSG_NOSIGNAL);
  2611. }
  2612. /*
  2613. * Retransmit the regular token if no mcast or token has
  2614. * been received in retransmit token period retransmit
  2615. * the token to the next processor
  2616. */
  2617. static void timer_function_token_retransmit_timeout (void *data)
  2618. {
  2619. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  2620. switch (instance->memb_state) {
  2621. case MEMB_STATE_GATHER:
  2622. break;
  2623. case MEMB_STATE_COMMIT:
  2624. break;
  2625. case MEMB_STATE_OPERATIONAL:
  2626. case MEMB_STATE_RECOVERY:
  2627. token_retransmit (instance);
  2628. reset_token_retransmit_timeout (instance); // REVIEWED
  2629. break;
  2630. }
  2631. }
  2632. static void timer_function_token_hold_retransmit_timeout (void *data)
  2633. {
  2634. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  2635. switch (instance->memb_state) {
  2636. case MEMB_STATE_GATHER:
  2637. break;
  2638. case MEMB_STATE_COMMIT:
  2639. break;
  2640. case MEMB_STATE_OPERATIONAL:
  2641. case MEMB_STATE_RECOVERY:
  2642. token_retransmit (instance);
  2643. break;
  2644. }
  2645. }
  2646. static void timer_function_merge_detect_timeout(void *data)
  2647. {
  2648. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  2649. instance->my_merge_detect_timeout_outstanding = 0;
  2650. switch (instance->memb_state) {
  2651. case MEMB_STATE_OPERATIONAL:
  2652. if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr) {
  2653. memb_merge_detect_transmit (instance);
  2654. }
  2655. break;
  2656. case MEMB_STATE_GATHER:
  2657. case MEMB_STATE_COMMIT:
  2658. case MEMB_STATE_RECOVERY:
  2659. break;
  2660. }
  2661. }
  2662. /*
  2663. * Send orf_token to next member (requires orf_token)
  2664. */
  2665. static int token_send (
  2666. struct totemsrp_instance *instance,
  2667. struct orf_token *orf_token,
  2668. int forward_token)
  2669. {
  2670. struct msghdr msg_orf_token;
  2671. struct iovec iovec;
  2672. int res;
  2673. iovec.iov_base = (char *)orf_token;
  2674. iovec.iov_len = sizeof (struct orf_token) +
  2675. (orf_token->rtr_list_entries * sizeof (struct rtr_item));
  2676. encrypt_and_sign (instance, &iovec, 1);
  2677. /*
  2678. * Keep an encrypted copy in case the token retransmit timer expires
  2679. */
  2680. memcpy (instance->orf_token_retransmit, instance->iov_encrypted.iov_base, instance->iov_encrypted.iov_len);
  2681. instance->orf_token_retransmit_size = instance->iov_encrypted.iov_len;
  2682. /*
  2683. * IF the user doesn't want the token forwarded, then dont send
  2684. * it but keep an encrypted copy for the retransmit timeout
  2685. */
  2686. if (forward_token == 0) {
  2687. return (0);
  2688. }
  2689. /*
  2690. * Send the message
  2691. */
  2692. msg_orf_token.msg_name = &instance->next_memb;
  2693. msg_orf_token.msg_namelen = sizeof (struct sockaddr_in);
  2694. msg_orf_token.msg_iov = &instance->iov_encrypted;
  2695. msg_orf_token.msg_iovlen = 1;
  2696. msg_orf_token.msg_control = 0;
  2697. msg_orf_token.msg_controllen = 0;
  2698. msg_orf_token.msg_flags = 0;
  2699. res = sendmsg (instance->totemsrp_sockets[0].token, &msg_orf_token, MSG_NOSIGNAL);
  2700. if (res == -1) {
  2701. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2702. "Couldn't send token to addr %s %s %d\n",
  2703. inet_ntoa (instance->next_memb.sin_addr),
  2704. strerror (errno), instance->totemsrp_sockets[0].token);
  2705. }
  2706. /*
  2707. * res not used here errors are handled by algorithm
  2708. */
  2709. if (res > 0) {
  2710. instance->stats_sent += res;
  2711. }
  2712. return (res);
  2713. }
  2714. static int token_hold_cancel_send (struct totemsrp_instance *instance)
  2715. {
  2716. struct token_hold_cancel token_hold_cancel;
  2717. struct iovec iov;
  2718. struct msghdr msghdr;
  2719. /*
  2720. * Only cancel if the token is currently held
  2721. */
  2722. if (instance->my_token_held == 0) {
  2723. return (0);
  2724. }
  2725. instance->my_token_held = 0;
  2726. /*
  2727. * Build message
  2728. */
  2729. token_hold_cancel.header.type = MESSAGE_TYPE_TOKEN_HOLD_CANCEL;
  2730. token_hold_cancel.header.endian_detector = ENDIAN_LOCAL;
  2731. memcpy (&token_hold_cancel.ring_id, &instance->my_ring_id,
  2732. sizeof (struct memb_ring_id));
  2733. iov.iov_base = &token_hold_cancel;
  2734. iov.iov_len = sizeof (struct token_hold_cancel);
  2735. encrypt_and_sign (instance, &iov, 1);
  2736. /*
  2737. * Build multicast message
  2738. */
  2739. msghdr.msg_name = (caddr_t)&instance->sockaddr_in_mcast;
  2740. msghdr.msg_namelen = sizeof (struct sockaddr_in);
  2741. msghdr.msg_iov = &instance->iov_encrypted;
  2742. msghdr.msg_iovlen = 1;
  2743. msghdr.msg_control = 0;
  2744. msghdr.msg_controllen = 0;
  2745. msghdr.msg_flags = 0;
  2746. /*
  2747. * Multicast message
  2748. */
  2749. sendmsg (instance->totemsrp_sockets[0].mcast, &msghdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  2750. return (0);
  2751. }
  2752. static int orf_token_send_initial (struct totemsrp_instance *instance)
  2753. {
  2754. struct orf_token orf_token;
  2755. int res;
  2756. orf_token.header.type = MESSAGE_TYPE_ORF_TOKEN;
  2757. orf_token.header.endian_detector = ENDIAN_LOCAL;
  2758. orf_token.header.encapsulated = 0;
  2759. orf_token.seq = 0;
  2760. orf_token.token_seq = 0;
  2761. orf_token.retrans_flg = 1;
  2762. instance->my_set_retrans_flg = 1;
  2763. /*
  2764. if (queue_is_empty (&instance->retrans_message_queue) == 1) {
  2765. orf_token.retrans_flg = 0;
  2766. } else {
  2767. orf_token.retrans_flg = 1;
  2768. instance->my_set_retrans_flg = 1;
  2769. }
  2770. */
  2771. orf_token.aru = 0;
  2772. // orf_token.aru_addr.s_addr = 0;//instance->my_id.sin_addr.s_addr;
  2773. orf_token.aru_addr.s_addr = instance->my_id.sin_addr.s_addr;
  2774. memcpy (&orf_token.ring_id, &instance->my_ring_id, sizeof (struct memb_ring_id));
  2775. orf_token.fcc = 0;
  2776. orf_token.rtr_list_entries = 0;
  2777. res = token_send (instance, &orf_token, 1);
  2778. return (res);
  2779. }
  2780. static void memb_state_commit_token_update (
  2781. struct totemsrp_instance *instance,
  2782. struct memb_commit_token *memb_commit_token)
  2783. {
  2784. int memb_index_this;
  2785. memb_index_this = (memb_commit_token->memb_index + 1) % memb_commit_token->addr_entries;
  2786. memcpy (&memb_commit_token->memb_list[memb_index_this].ring_id,
  2787. &instance->my_old_ring_id, sizeof (struct memb_ring_id));
  2788. assert (instance->my_old_ring_id.rep.s_addr != 0);
  2789. memb_commit_token->memb_list[memb_index_this].aru = instance->old_ring_state_aru;
  2790. /*
  2791. * TODO high delivered is really instance->my_aru, but with safe this
  2792. * could change?
  2793. */
  2794. memb_commit_token->memb_list[memb_index_this].high_delivered = instance->my_high_delivered;
  2795. memb_commit_token->memb_list[memb_index_this].received_flg = instance->my_received_flg;
  2796. }
  2797. static int memb_state_commit_token_send (struct totemsrp_instance *instance,
  2798. struct memb_commit_token *memb_commit_token)
  2799. {
  2800. struct msghdr msghdr;
  2801. struct iovec iovec;
  2802. int res;
  2803. int memb_index_this;
  2804. int memb_index_next;
  2805. memb_commit_token->token_seq++;
  2806. memb_index_this = (memb_commit_token->memb_index + 1) % memb_commit_token->addr_entries;
  2807. memb_index_next = (memb_index_this + 1) % memb_commit_token->addr_entries;
  2808. memb_commit_token->memb_index = memb_index_this;
  2809. iovec.iov_base = memb_commit_token;
  2810. iovec.iov_len = sizeof (struct memb_commit_token);
  2811. encrypt_and_sign (instance, &iovec, 1);
  2812. instance->next_memb.sin_addr.s_addr = memb_commit_token->addr[memb_index_next].s_addr;
  2813. instance->next_memb.sin_family = AF_INET;
  2814. instance->next_memb.sin_port = instance->sockaddr_in_mcast.sin_port;
  2815. msghdr.msg_name = &instance->next_memb;
  2816. msghdr.msg_namelen = sizeof (struct sockaddr_in);
  2817. msghdr.msg_iov = &instance->iov_encrypted;
  2818. msghdr.msg_iovlen = 1;
  2819. msghdr.msg_control = 0;
  2820. msghdr.msg_controllen = 0;
  2821. msghdr.msg_flags = 0;
  2822. res = sendmsg (instance->totemsrp_sockets[0].token, &msghdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  2823. return (res);
  2824. }
  2825. static int memb_lowest_in_config (struct totemsrp_instance *instance)
  2826. {
  2827. struct in_addr token_memb[PROCESSOR_COUNT_MAX];
  2828. int token_memb_entries = 0;
  2829. struct in_addr lowest_addr;
  2830. int i;
  2831. lowest_addr.s_addr = 0xFFFFFFFF;
  2832. memb_set_subtract (token_memb, &token_memb_entries,
  2833. instance->my_proc_list, instance->my_proc_list_entries,
  2834. instance->my_failed_list, instance->my_failed_list_entries);
  2835. /*
  2836. * find representative by searching for smallest identifier
  2837. */
  2838. for (i = 0; i < token_memb_entries; i++) {
  2839. if (lowest_addr.s_addr > token_memb[i].s_addr) {
  2840. lowest_addr.s_addr = token_memb[i].s_addr;
  2841. }
  2842. }
  2843. return (instance->my_id.sin_addr.s_addr == lowest_addr.s_addr);
  2844. }
  2845. static void memb_state_commit_token_create (
  2846. struct totemsrp_instance *instance,
  2847. struct memb_commit_token *commit_token)
  2848. {
  2849. struct in_addr token_memb[PROCESSOR_COUNT_MAX];
  2850. int token_memb_entries = 0;
  2851. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2852. "Creating commit token because I am the rep.\n");
  2853. memb_set_subtract (token_memb, &token_memb_entries,
  2854. instance->my_proc_list, instance->my_proc_list_entries,
  2855. instance->my_failed_list, instance->my_failed_list_entries);
  2856. memset (commit_token, 0, sizeof (struct memb_commit_token));
  2857. commit_token->header.type = MESSAGE_TYPE_MEMB_COMMIT_TOKEN;
  2858. commit_token->header.endian_detector = ENDIAN_LOCAL;
  2859. commit_token->header.encapsulated = 0;
  2860. commit_token->ring_id.rep.s_addr = instance->my_id.sin_addr.s_addr;
  2861. commit_token->ring_id.seq = instance->token_ring_id_seq + 4;
  2862. qsort (token_memb, token_memb_entries,
  2863. sizeof (struct in_addr), in_addr_compare);
  2864. memcpy (commit_token->addr, token_memb,
  2865. token_memb_entries * sizeof (struct in_addr));
  2866. memset (commit_token->memb_list, 0,
  2867. sizeof (struct memb_commit_token_memb_entry) * PROCESSOR_COUNT_MAX);
  2868. commit_token->memb_index = token_memb_entries - 1;
  2869. commit_token->addr_entries = token_memb_entries;
  2870. }
  2871. static int memb_join_message_send (struct totemsrp_instance *instance)
  2872. {
  2873. struct msghdr msghdr;
  2874. struct iovec iovec;
  2875. struct memb_join memb_join;
  2876. int res;
  2877. memb_join.header.type = MESSAGE_TYPE_MEMB_JOIN;
  2878. memb_join.header.endian_detector = ENDIAN_LOCAL;
  2879. memb_join.header.encapsulated = 0;
  2880. memb_join.ring_seq = instance->my_ring_id.seq;
  2881. memcpy (memb_join.proc_list, instance->my_proc_list,
  2882. instance->my_proc_list_entries * sizeof (struct in_addr));
  2883. memb_join.proc_list_entries = instance->my_proc_list_entries;
  2884. memcpy (memb_join.failed_list, instance->my_failed_list,
  2885. instance->my_failed_list_entries * sizeof (struct in_addr));
  2886. memb_join.failed_list_entries = instance->my_failed_list_entries;
  2887. iovec.iov_base = &memb_join;
  2888. iovec.iov_len = sizeof (struct memb_join);
  2889. encrypt_and_sign (instance, &iovec, 1);
  2890. msghdr.msg_name = &instance->sockaddr_in_mcast;
  2891. msghdr.msg_namelen = sizeof (struct sockaddr_in);
  2892. msghdr.msg_iov = &instance->iov_encrypted;
  2893. msghdr.msg_iovlen = 1;
  2894. msghdr.msg_control = 0;
  2895. msghdr.msg_controllen = 0;
  2896. msghdr.msg_flags = 0;
  2897. res = sendmsg (instance->totemsrp_sockets[0].mcast, &msghdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  2898. return (res);
  2899. }
  2900. static int memb_merge_detect_transmit (struct totemsrp_instance *instance)
  2901. {
  2902. struct msghdr msghdr;
  2903. struct iovec iovec;
  2904. struct memb_merge_detect memb_merge_detect;
  2905. int res;
  2906. memb_merge_detect.header.type = MESSAGE_TYPE_MEMB_MERGE_DETECT;
  2907. memb_merge_detect.header.endian_detector = ENDIAN_LOCAL;
  2908. memb_merge_detect.header.encapsulated = 0;
  2909. memcpy (&memb_merge_detect.ring_id, &instance->my_ring_id,
  2910. sizeof (struct memb_ring_id));
  2911. iovec.iov_base = &memb_merge_detect;
  2912. iovec.iov_len = sizeof (struct memb_merge_detect);
  2913. encrypt_and_sign (instance, &iovec, 1);
  2914. msghdr.msg_name = &instance->sockaddr_in_mcast;
  2915. msghdr.msg_namelen = sizeof (struct sockaddr_in);
  2916. msghdr.msg_iov = &instance->iov_encrypted;
  2917. msghdr.msg_iovlen = 1;
  2918. msghdr.msg_control = 0;
  2919. msghdr.msg_controllen = 0;
  2920. msghdr.msg_flags = 0;
  2921. res = sendmsg (instance->totemsrp_sockets[0].mcast, &msghdr,
  2922. MSG_NOSIGNAL | MSG_DONTWAIT);
  2923. return (res);
  2924. }
  2925. static void memb_ring_id_create_or_load (
  2926. struct totemsrp_instance *instance,
  2927. struct memb_ring_id *memb_ring_id)
  2928. {
  2929. int fd;
  2930. int res;
  2931. char filename[256];
  2932. sprintf (filename, "/tmp/ringid_%s",
  2933. inet_ntoa (instance->my_id.sin_addr));
  2934. fd = open (filename, O_RDONLY, 0777);
  2935. if (fd > 0) {
  2936. res = read (fd, &memb_ring_id->seq, sizeof (unsigned long long));
  2937. assert (res == sizeof (unsigned long long));
  2938. close (fd);
  2939. } else
  2940. if (fd == -1 && errno == ENOENT) {
  2941. memb_ring_id->seq = 0;
  2942. umask(0);
  2943. fd = open (filename, O_CREAT|O_RDWR, 0777);
  2944. if (fd == -1) {
  2945. printf ("couldn't create file %d %s\n", fd, strerror(errno));
  2946. }
  2947. res = write (fd, &memb_ring_id->seq, sizeof (unsigned long long));
  2948. assert (res == sizeof (unsigned long long));
  2949. close (fd);
  2950. } else {
  2951. instance->totemsrp_log_printf (instance->totemsrp_log_level_warning,
  2952. "Couldn't open %s %s\n", filename, strerror (errno));
  2953. }
  2954. memb_ring_id->rep.s_addr = instance->my_id.sin_addr.s_addr;
  2955. assert (memb_ring_id->rep.s_addr);
  2956. instance->token_ring_id_seq = memb_ring_id->seq;
  2957. }
  2958. static void memb_ring_id_store (
  2959. struct totemsrp_instance *instance,
  2960. struct memb_commit_token *commit_token)
  2961. {
  2962. char filename[256];
  2963. int fd;
  2964. int res;
  2965. sprintf (filename, "/tmp/ringid_%s",
  2966. inet_ntoa (instance->my_id.sin_addr));
  2967. fd = open (filename, O_WRONLY, 0777);
  2968. if (fd == -1) {
  2969. fd = open (filename, O_CREAT|O_RDWR, 0777);
  2970. }
  2971. if (fd == -1) {
  2972. instance->totemsrp_log_printf (instance->totemsrp_log_level_warning,
  2973. "Couldn't store new ring id %llx to stable storage (%s)\n",
  2974. commit_token->ring_id.seq, strerror (errno));
  2975. assert (0);
  2976. return;
  2977. }
  2978. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  2979. "Storing new sequence id for ring %d\n", commit_token->ring_id.seq);
  2980. assert (fd > 0);
  2981. res = write (fd, &commit_token->ring_id.seq, sizeof (unsigned long long));
  2982. assert (res == sizeof (unsigned long long));
  2983. close (fd);
  2984. memcpy (&instance->my_ring_id, &commit_token->ring_id, sizeof (struct memb_ring_id));
  2985. instance->token_ring_id_seq = instance->my_ring_id.seq;
  2986. }
  2987. void print_stats (struct totemsrp_instance *instance)
  2988. {
  2989. struct timeval tv_end;
  2990. gettimeofday (&tv_end, NULL);
  2991. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Bytes recv %d\n", instance->stats_recv);
  2992. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Bytes sent %d\n", instance->stats_sent);
  2993. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Messages delivered %d\n", instance->stats_delv);
  2994. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Re-Mcasts %d\n", instance->stats_remcasts);
  2995. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Tokens process %d\n", instance->stats_orf_token);
  2996. }
  2997. int totemsrp_callback_token_create (
  2998. totemsrp_handle handle,
  2999. void **handle_out,
  3000. enum totem_callback_token_type type,
  3001. int delete,
  3002. int (*callback_fn) (enum totem_callback_token_type type, void *),
  3003. void *data)
  3004. {
  3005. struct token_callback_instance *callback_handle;
  3006. struct totemsrp_instance *instance;
  3007. SaErrorT error;
  3008. error = saHandleInstanceGet (&totemsrp_instance_database, handle,
  3009. (void *)&instance);
  3010. if (error != SA_OK) {
  3011. goto error_exit;
  3012. }
  3013. callback_handle = (struct token_callback_instance *)malloc (sizeof (struct token_callback_instance));
  3014. if (callback_handle == 0) {
  3015. return (-1);
  3016. }
  3017. *handle_out = (void *)callback_handle;
  3018. list_init (&callback_handle->list);
  3019. callback_handle->callback_fn = callback_fn;
  3020. callback_handle->data = data;
  3021. callback_handle->callback_type = type;
  3022. callback_handle->delete = delete;
  3023. switch (type) {
  3024. case TOTEM_CALLBACK_TOKEN_RECEIVED:
  3025. list_add (&callback_handle->list, &instance->token_callback_received_listhead);
  3026. break;
  3027. case TOTEM_CALLBACK_TOKEN_SENT:
  3028. list_add (&callback_handle->list, &instance->token_callback_sent_listhead);
  3029. break;
  3030. }
  3031. saHandleInstancePut (&totemsrp_instance_database, handle);
  3032. error_exit:
  3033. return (0);
  3034. }
  3035. void totemsrp_callback_token_destroy (totemsrp_handle handle, void **handle_out)
  3036. {
  3037. struct token_callback_instance *h;
  3038. if (*handle_out) {
  3039. h = (struct token_callback_instance *)*handle_out;
  3040. list_del (&h->list);
  3041. free (h);
  3042. h = NULL;
  3043. *handle_out = 0;
  3044. }
  3045. }
  3046. void totem_callback_token_type (struct totemsrp_instance *instance, void *handle)
  3047. {
  3048. struct token_callback_instance *token_callback_instance = (struct token_callback_instance *)handle;
  3049. list_del (&token_callback_instance->list);
  3050. free (token_callback_instance);
  3051. }
  3052. static void token_callbacks_execute (
  3053. struct totemsrp_instance *instance,
  3054. enum totem_callback_token_type type)
  3055. {
  3056. struct list_head *list;
  3057. struct list_head *list_next;
  3058. struct list_head *callback_listhead = 0;
  3059. struct token_callback_instance *token_callback_instance;
  3060. int res;
  3061. int del;
  3062. switch (type) {
  3063. case TOTEM_CALLBACK_TOKEN_RECEIVED:
  3064. callback_listhead = &instance->token_callback_received_listhead;
  3065. break;
  3066. case TOTEM_CALLBACK_TOKEN_SENT:
  3067. callback_listhead = &instance->token_callback_sent_listhead;
  3068. break;
  3069. default:
  3070. assert (0);
  3071. }
  3072. for (list = callback_listhead->next; list != callback_listhead;
  3073. list = list_next) {
  3074. token_callback_instance = list_entry (list, struct token_callback_instance, list);
  3075. list_next = list->next;
  3076. del = token_callback_instance->delete;
  3077. if (del == 1) {
  3078. list_del (list);
  3079. }
  3080. res = token_callback_instance->callback_fn (
  3081. token_callback_instance->callback_type,
  3082. token_callback_instance->data);
  3083. /*
  3084. * This callback failed to execute, try it again on the next token
  3085. */
  3086. if (res == -1 && del == 1) {
  3087. list_add (list, callback_listhead);
  3088. } else if (del) {
  3089. free (token_callback_instance);
  3090. }
  3091. }
  3092. }
  3093. /*
  3094. * Message Handlers
  3095. */
  3096. /*
  3097. * message handler called when TOKEN message type received
  3098. */
  3099. static int message_handler_orf_token (
  3100. struct totemsrp_instance *instance,
  3101. struct sockaddr_in *system_from,
  3102. struct iovec *iovec,
  3103. int iov_len,
  3104. int bytes_received,
  3105. int endian_conversion_needed)
  3106. {
  3107. char token_storage[1500];
  3108. char token_convert[1500];
  3109. struct orf_token *token;
  3110. int prio = UINT_MAX;
  3111. struct pollfd ufd;
  3112. int nfds;
  3113. struct orf_token *token_ref = (struct orf_token *)iovec->iov_base;
  3114. int transmits_allowed;
  3115. int forward_token;
  3116. int mcasted;
  3117. int last_aru;
  3118. int low_water;
  3119. #ifdef GIVEINFO
  3120. struct timeval tv_current;
  3121. struct timeval tv_diff;
  3122. gettimeofday (&tv_current, NULL);
  3123. timersub (&tv_current, &tv_old, &tv_diff);
  3124. memcpy (&tv_old, &tv_current, sizeof (struct timeval));
  3125. if ((((float)tv_diff.tv_usec) / 100.0) > 5.0) {
  3126. printf ("OTHERS %0.4f ms\n", ((float)tv_diff.tv_usec) / 100.0);
  3127. }
  3128. #endif
  3129. #ifdef RANDOM_DROP
  3130. if (random () % 100 < 10) {
  3131. return (0);
  3132. }
  3133. #endif
  3134. /*
  3135. * Handle merge detection timeout
  3136. */
  3137. if (token_ref->seq == instance->my_last_seq) {
  3138. start_merge_detect_timeout (instance);
  3139. instance->my_seq_unchanged += 1;
  3140. } else {
  3141. cancel_merge_detect_timeout (instance);
  3142. cancel_token_hold_retransmit_timeout (instance);
  3143. instance->my_seq_unchanged = 0;
  3144. }
  3145. instance->my_last_seq = token_ref->seq;
  3146. assert (bytes_received >= sizeof (struct orf_token));
  3147. // assert (bytes_received == sizeof (struct orf_token) +
  3148. // (sizeof (struct rtr_item) * token_ref->rtr_list_entries);
  3149. /*
  3150. * Make copy of token and retransmit list in case we have
  3151. * to flush incoming messages from the kernel queue
  3152. */
  3153. token = (struct orf_token *)token_storage;
  3154. memcpy (token, iovec->iov_base, sizeof (struct orf_token));
  3155. memcpy (&token->rtr_list[0], iovec->iov_base + sizeof (struct orf_token),
  3156. sizeof (struct rtr_item) * RETRANSMIT_ENTRIES_MAX);
  3157. if (endian_conversion_needed) {
  3158. orf_token_endian_convert (token, (struct orf_token *)token_convert);
  3159. token = (struct orf_token *)token_convert;
  3160. }
  3161. /*
  3162. * flush incoming queue from kernel
  3163. */
  3164. do {
  3165. ufd.fd = instance->totemsrp_sockets[0].mcast;
  3166. ufd.events = POLLIN;
  3167. nfds = poll (&ufd, 1, 0);
  3168. if (nfds == 1 && ufd.revents & POLLIN) {
  3169. instance->totemsrp_iov_recv.iov_len = PACKET_SIZE_MAX;
  3170. recv_handler (0, instance->totemsrp_sockets[0].mcast,
  3171. ufd.revents, instance, &prio);
  3172. }
  3173. } while (nfds == 1);
  3174. /*
  3175. * Determine if we should hold (in reality drop) the token
  3176. */
  3177. instance->my_token_held = 0;
  3178. if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr &&
  3179. instance->my_seq_unchanged > SEQNO_UNCHANGED_CONST) {
  3180. instance->my_token_held = 1;
  3181. } else
  3182. if (instance->my_ring_id.rep.s_addr != instance->my_id.sin_addr.s_addr &&
  3183. instance->my_seq_unchanged >= SEQNO_UNCHANGED_CONST) {
  3184. instance->my_token_held = 1;
  3185. }
  3186. /*
  3187. * Hold onto token when there is no activity on ring and
  3188. * this processor is the ring rep
  3189. */
  3190. forward_token = 1;
  3191. if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr) {
  3192. if (instance->my_token_held) {
  3193. forward_token = 0;
  3194. }
  3195. }
  3196. token_callbacks_execute (instance, TOTEM_CALLBACK_TOKEN_RECEIVED);
  3197. switch (instance->memb_state) {
  3198. case MEMB_STATE_COMMIT:
  3199. /* Discard token */
  3200. break;
  3201. case MEMB_STATE_OPERATIONAL:
  3202. messages_free (instance, token->aru);
  3203. case MEMB_STATE_GATHER:
  3204. /*
  3205. * DO NOT add break, we use different free mechanism in recovery state
  3206. */
  3207. case MEMB_STATE_RECOVERY:
  3208. last_aru = instance->my_last_aru;
  3209. instance->my_last_aru = token->aru;
  3210. /*
  3211. * Discard tokens from another configuration
  3212. */
  3213. if (memcmp (&token->ring_id, &instance->my_ring_id,
  3214. sizeof (struct memb_ring_id)) != 0) {
  3215. return (0); /* discard token */
  3216. }
  3217. /*
  3218. * Discard retransmitted tokens
  3219. */
  3220. if (instance->my_token_seq >= token->token_seq) {
  3221. reset_token_retransmit_timeout (instance);
  3222. reset_token_timeout (instance);
  3223. return (0); /* discard token */
  3224. }
  3225. transmits_allowed = 30;
  3226. mcasted = orf_token_rtr (instance, token, &transmits_allowed);
  3227. if ((last_aru + MISSING_MCAST_WINDOW) < token->seq) {
  3228. transmits_allowed = 0;
  3229. }
  3230. mcasted = orf_token_mcast (instance, token, transmits_allowed, system_from);
  3231. if (instance->my_aru < token->aru ||
  3232. instance->my_id.sin_addr.s_addr == token->aru_addr.s_addr ||
  3233. token->aru_addr.s_addr == 0) {
  3234. token->aru = instance->my_aru;
  3235. if (token->aru == token->seq) {
  3236. token->aru_addr.s_addr = 0;
  3237. } else {
  3238. token->aru_addr.s_addr = instance->my_id.sin_addr.s_addr;
  3239. }
  3240. }
  3241. if (token->aru == last_aru && token->aru_addr.s_addr != 0) {
  3242. instance->my_aru_count += 1;
  3243. } else {
  3244. instance->my_aru_count = 0;
  3245. }
  3246. if (instance->my_aru_count > instance->fail_to_recv_const &&
  3247. token->aru_addr.s_addr != instance->my_id.sin_addr.s_addr) {
  3248. printf ("FAILED TO RECEIVE\n");
  3249. // TODO if we fail to receive, it may be possible to end with a gather
  3250. // state of proc == failed = 0 entries
  3251. memb_set_merge (&token->aru_addr, 1,
  3252. instance->my_failed_list,
  3253. &instance->my_failed_list_entries);
  3254. ring_state_restore (instance);
  3255. memb_state_gather_enter (instance);
  3256. } else {
  3257. instance->my_token_seq = token->token_seq;
  3258. token->token_seq += 1;
  3259. if (instance->memb_state == MEMB_STATE_RECOVERY) {
  3260. /*
  3261. * instance->my_aru == instance->my_high_seq_received means this processor
  3262. * has recovered all messages it can recover
  3263. * (ie: its retrans queue is empty)
  3264. */
  3265. low_water = instance->my_aru;
  3266. if (low_water > last_aru) {
  3267. low_water = last_aru;
  3268. }
  3269. // TODO is this code right
  3270. if (queue_is_empty (&instance->retrans_message_queue) == 0 ||
  3271. low_water != instance->my_high_seq_received) {
  3272. if (token->retrans_flg == 0) {
  3273. token->retrans_flg = 1;
  3274. instance->my_set_retrans_flg = 1;
  3275. }
  3276. } else
  3277. if (token->retrans_flg == 1 && instance->my_set_retrans_flg) {
  3278. token->retrans_flg = 0;
  3279. }
  3280. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3281. "token retrans flag is %d my set retrans flag%d retrans queue empty %d count %d, low_water %d aru %d\n",
  3282. token->retrans_flg, instance->my_set_retrans_flg,
  3283. queue_is_empty (&instance->retrans_message_queue),
  3284. instance->my_retrans_flg_count, low_water, token->aru);
  3285. if (token->retrans_flg == 0) {
  3286. instance->my_retrans_flg_count += 1;
  3287. } else {
  3288. instance->my_retrans_flg_count = 0;
  3289. }
  3290. if (instance->my_retrans_flg_count == 2) {
  3291. instance->my_install_seq = token->seq;
  3292. }
  3293. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3294. "install seq %d aru %d high seq received %d\n",
  3295. instance->my_install_seq, instance->my_aru, instance->my_high_seq_received);
  3296. if (instance->my_retrans_flg_count >= 2 && instance->my_aru >= instance->my_install_seq && instance->my_received_flg == 0) {
  3297. instance->my_received_flg = 1;
  3298. instance->my_deliver_memb_entries = instance->my_trans_memb_entries;
  3299. memcpy (instance->my_deliver_memb_list, instance->my_trans_memb_list,
  3300. sizeof (struct in_addr) * instance->my_trans_memb_entries);
  3301. }
  3302. if (instance->my_retrans_flg_count >= 3 && token->aru >= instance->my_install_seq) {
  3303. instance->my_rotation_counter += 1;
  3304. } else {
  3305. instance->my_rotation_counter = 0;
  3306. }
  3307. if (instance->my_rotation_counter == 2) {
  3308. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3309. "retrans flag count %d token aru %d install seq %d aru %d %d\n",
  3310. instance->my_retrans_flg_count, token->aru, instance->my_install_seq,
  3311. instance->my_aru, token->seq);
  3312. memb_state_operational_enter (instance);
  3313. instance->my_rotation_counter = 0;
  3314. instance->my_retrans_flg_count = 0;
  3315. }
  3316. }
  3317. #ifdef CONFIG_THREADED_SEND
  3318. worker_thread_group_wait (&instance->worker_thread_group_orf_token_mcast);
  3319. #endif
  3320. token_send (instance, token, forward_token);
  3321. #ifdef GIVEINFO
  3322. gettimeofday (&tv_current, NULL);
  3323. timersub (&tv_current, &tv_old, &tv_diff);
  3324. memcpy (&tv_old, &tv_current, sizeof (struct timeval));
  3325. if ((((float)tv_diff.tv_usec) / 100.0) > 5.0) {
  3326. printf ("I held %0.4f ms\n", ((float)tv_diff.tv_usec) / 100.0);
  3327. }
  3328. #endif
  3329. if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
  3330. messages_deliver_to_app (instance, 0, instance->my_high_seq_received);
  3331. }
  3332. /*
  3333. * Deliver messages after token has been transmitted
  3334. * to improve performance
  3335. */
  3336. reset_token_timeout (instance); // REVIEWED
  3337. reset_token_retransmit_timeout (instance); // REVIEWED
  3338. if (instance->my_id.sin_addr.s_addr == instance->my_ring_id.rep.s_addr &&
  3339. instance->my_token_held == 1) {
  3340. start_token_hold_retransmit_timeout (instance);
  3341. }
  3342. token_callbacks_execute (instance, TOTEM_CALLBACK_TOKEN_SENT);
  3343. }
  3344. break;
  3345. }
  3346. return (0);
  3347. }
  3348. static void messages_deliver_to_app (
  3349. struct totemsrp_instance *instance,
  3350. int skip,
  3351. int end_point)
  3352. {
  3353. struct sort_queue_item *sort_queue_item_p;
  3354. int i;
  3355. int res;
  3356. struct mcast *mcast;
  3357. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3358. "Delivering %d to %d\n", instance->my_high_delivered + 1,
  3359. end_point);
  3360. /*
  3361. * Deliver messages in order from rtr queue to pending delivery queue
  3362. */
  3363. for (i = instance->my_high_delivered + 1; i <= end_point; i++) {
  3364. void *ptr;
  3365. res = sq_item_get (&instance->regular_sort_queue, i, &ptr);
  3366. if (res != 0 && skip) {
  3367. printf ("-skipping %d-\n", i);
  3368. instance->my_high_delivered = i;
  3369. continue;
  3370. }
  3371. /*
  3372. * If hole, stop assembly
  3373. */
  3374. if (res != 0) {
  3375. break;
  3376. }
  3377. sort_queue_item_p = ptr;
  3378. mcast = sort_queue_item_p->iovec[0].iov_base;
  3379. assert (mcast != (struct mcast *)0xdeadbeef);
  3380. // XXX printf ("[%s.%d-%d]\n", inet_ntoa (mcast->source),
  3381. // XXX mcast->seq, mcast->this_seqno);
  3382. /*
  3383. * Skip messages not originated in instance->my_deliver_memb
  3384. */
  3385. if (skip &&
  3386. memb_set_subset (&mcast->source,
  3387. 1,
  3388. instance->my_deliver_memb_list,
  3389. instance->my_deliver_memb_entries) == 0) {
  3390. printf ("-skipping %d - wrong ip", i);
  3391. instance->my_high_delivered = i;
  3392. continue;
  3393. }
  3394. instance->my_high_delivered = i;
  3395. /*
  3396. * Message found
  3397. */
  3398. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3399. "Delivering MCAST message with seq %d to pending delivery queue\n",
  3400. mcast->seq);
  3401. /*
  3402. * Message is locally originated multicast
  3403. */
  3404. if (sort_queue_item_p->iov_len > 1 &&
  3405. sort_queue_item_p->iovec[0].iov_len == sizeof (struct mcast)) {
  3406. instance->totemsrp_deliver_fn (
  3407. mcast->source,
  3408. &sort_queue_item_p->iovec[1],
  3409. sort_queue_item_p->iov_len - 1,
  3410. mcast->header.endian_detector != ENDIAN_LOCAL);
  3411. } else {
  3412. sort_queue_item_p->iovec[0].iov_len -= sizeof (struct mcast);
  3413. sort_queue_item_p->iovec[0].iov_base += sizeof (struct mcast);
  3414. instance->totemsrp_deliver_fn (
  3415. mcast->source,
  3416. sort_queue_item_p->iovec,
  3417. sort_queue_item_p->iov_len,
  3418. mcast->header.endian_detector != ENDIAN_LOCAL);
  3419. sort_queue_item_p->iovec[0].iov_len += sizeof (struct mcast);
  3420. sort_queue_item_p->iovec[0].iov_base -= sizeof (struct mcast);
  3421. }
  3422. instance->stats_delv += 1;
  3423. }
  3424. instance->my_received_flg = 0;
  3425. if (instance->my_aru == instance->my_high_seq_received) {
  3426. // instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3427. // "setting received flag to TRUE %d %d\n",
  3428. // instance->my_aru, instance->my_high_seq_received);
  3429. instance->my_received_flg = 1;
  3430. }
  3431. }
  3432. /*
  3433. * recv message handler called when MCAST message type received
  3434. */
  3435. static int message_handler_mcast (
  3436. struct totemsrp_instance *instance,
  3437. struct sockaddr_in *system_from,
  3438. struct iovec *iovec,
  3439. int iov_len,
  3440. int bytes_received,
  3441. int endian_conversion_needed)
  3442. {
  3443. struct sort_queue_item sort_queue_item;
  3444. struct sq *sort_queue;
  3445. struct mcast mcast_header;
  3446. if (endian_conversion_needed) {
  3447. mcast_endian_convert (iovec[0].iov_base, &mcast_header);
  3448. } else {
  3449. memcpy (&mcast_header, iovec[0].iov_base, sizeof (struct mcast));
  3450. }
  3451. if (mcast_header.header.encapsulated == 1) {
  3452. sort_queue = &instance->recovery_sort_queue;
  3453. } else {
  3454. sort_queue = &instance->regular_sort_queue;
  3455. }
  3456. assert (bytes_received < PACKET_SIZE_MAX);
  3457. #ifdef RANDOM_DROP
  3458. if (random()%100 < 50) {
  3459. return (0);
  3460. }
  3461. #endif
  3462. if (system_from->sin_addr.s_addr != instance->my_id.sin_addr.s_addr) {
  3463. cancel_token_retransmit_timeout (instance);
  3464. }
  3465. /*
  3466. * If the message is foreign execute the switch below
  3467. */
  3468. if (memcmp (&instance->my_ring_id, &mcast_header.ring_id,
  3469. sizeof (struct memb_ring_id)) != 0) {
  3470. switch (instance->memb_state) {
  3471. case MEMB_STATE_OPERATIONAL:
  3472. memb_set_merge (&system_from->sin_addr, 1,
  3473. instance->my_proc_list, &instance->my_proc_list_entries);
  3474. memb_state_gather_enter (instance);
  3475. break;
  3476. case MEMB_STATE_GATHER:
  3477. if (!memb_set_subset (&system_from->sin_addr,
  3478. 1,
  3479. instance->my_proc_list,
  3480. instance->my_proc_list_entries)) {
  3481. memb_set_merge (&system_from->sin_addr, 1,
  3482. instance->my_proc_list, &instance->my_proc_list_entries);
  3483. memb_state_gather_enter (instance);
  3484. return (0);
  3485. }
  3486. break;
  3487. case MEMB_STATE_COMMIT:
  3488. /* discard message */
  3489. break;
  3490. case MEMB_STATE_RECOVERY:
  3491. /* discard message */
  3492. break;
  3493. }
  3494. return (0);
  3495. }
  3496. instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
  3497. "Received ringid(%s:%lld) seq %d\n",
  3498. inet_ntoa (mcast_header.ring_id.rep),
  3499. mcast_header.ring_id.seq,
  3500. mcast_header.seq);
  3501. /*
  3502. * Add mcast message to rtr queue if not already in rtr queue
  3503. * otherwise free io vectors
  3504. */
  3505. if (bytes_received > 0 && bytes_received < PACKET_SIZE_MAX &&
  3506. instance->my_aru < mcast_header.seq &&
  3507. sq_item_inuse (sort_queue, mcast_header.seq) == 0) {
  3508. /*
  3509. * Allocate new multicast memory block
  3510. */
  3511. // TODO LEAK
  3512. sort_queue_item.iovec[0].iov_base = malloc (bytes_received);
  3513. if (sort_queue_item.iovec[0].iov_base == 0) {
  3514. return (-1); /* error here is corrected by the algorithm */
  3515. }
  3516. memcpy (sort_queue_item.iovec[0].iov_base, iovec[0].iov_base,
  3517. bytes_received);
  3518. sort_queue_item.iovec[0].iov_len = bytes_received;
  3519. assert (sort_queue_item.iovec[0].iov_len > 0);
  3520. assert (sort_queue_item.iovec[0].iov_len < PACKET_SIZE_MAX);
  3521. sort_queue_item.iov_len = 1;
  3522. if (mcast_header.seq > instance->my_high_seq_received) {
  3523. instance->my_high_seq_received = mcast_header.seq;
  3524. }
  3525. sq_item_add (sort_queue, &sort_queue_item, mcast_header.seq);
  3526. }
  3527. if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
  3528. update_aru (instance);
  3529. messages_deliver_to_app (instance, 0, instance->my_high_seq_received);
  3530. }
  3531. /* TODO remove from retrans message queue for old ring in recovery state */
  3532. return (0);
  3533. }
  3534. static int message_handler_memb_merge_detect (
  3535. struct totemsrp_instance *instance,
  3536. struct sockaddr_in *system_from,
  3537. struct iovec *iovec,
  3538. int iov_len,
  3539. int bytes_received,
  3540. int endian_conversion_needed)
  3541. {
  3542. struct memb_merge_detect *memb_merge_detect = iovec[0].iov_base;
  3543. /*
  3544. * do nothing if this is a merge detect from this configuration
  3545. */
  3546. if (memcmp (&instance->my_ring_id, &memb_merge_detect->ring_id,
  3547. sizeof (struct memb_ring_id)) == 0) {
  3548. return (0);
  3549. }
  3550. /*
  3551. * Execute merge operation
  3552. */
  3553. switch (instance->memb_state) {
  3554. case MEMB_STATE_OPERATIONAL:
  3555. memb_set_merge (&system_from->sin_addr, 1,
  3556. instance->my_proc_list, &instance->my_proc_list_entries);
  3557. memb_state_gather_enter (instance);
  3558. break;
  3559. case MEMB_STATE_GATHER:
  3560. if (!memb_set_subset (&system_from->sin_addr,
  3561. 1,
  3562. instance->my_proc_list,
  3563. instance->my_proc_list_entries)) {
  3564. memb_set_merge (&system_from->sin_addr, 1,
  3565. instance->my_proc_list, &instance->my_proc_list_entries);
  3566. memb_state_gather_enter (instance);
  3567. return (0);
  3568. }
  3569. break;
  3570. case MEMB_STATE_COMMIT:
  3571. /* do nothing in commit */
  3572. break;
  3573. case MEMB_STATE_RECOVERY:
  3574. /* do nothing in recovery */
  3575. break;
  3576. }
  3577. return (0);
  3578. }
  3579. static int memb_join_process (
  3580. struct totemsrp_instance *instance,
  3581. struct memb_join *memb_join,
  3582. struct sockaddr_in *system_from)
  3583. {
  3584. struct memb_commit_token my_commit_token;
  3585. if (memb_set_equal (memb_join->proc_list,
  3586. memb_join->proc_list_entries,
  3587. instance->my_proc_list,
  3588. instance->my_proc_list_entries) &&
  3589. memb_set_equal (memb_join->failed_list,
  3590. memb_join->failed_list_entries,
  3591. instance->my_failed_list,
  3592. instance->my_failed_list_entries)) {
  3593. memb_consensus_set (instance, &system_from->sin_addr);
  3594. if (memb_consensus_agreed (instance) &&
  3595. memb_lowest_in_config (instance)) {
  3596. memb_state_commit_token_create (instance, &my_commit_token);
  3597. memb_state_commit_enter (instance, &my_commit_token);
  3598. } else {
  3599. return (0);
  3600. }
  3601. } else
  3602. if (memb_set_subset (memb_join->proc_list,
  3603. memb_join->proc_list_entries,
  3604. instance->my_proc_list,
  3605. instance->my_proc_list_entries) &&
  3606. memb_set_subset (memb_join->failed_list,
  3607. memb_join->failed_list_entries,
  3608. instance->my_failed_list,
  3609. instance->my_failed_list_entries)) {
  3610. return (0);
  3611. } else
  3612. if (memb_set_subset (&system_from->sin_addr, 1,
  3613. instance->my_failed_list, instance->my_failed_list_entries)) {
  3614. return (0);
  3615. } else {
  3616. memb_set_merge (memb_join->proc_list,
  3617. memb_join->proc_list_entries,
  3618. instance->my_proc_list, &instance->my_proc_list_entries);
  3619. if (memb_set_subset (&instance->my_id.sin_addr, 1,
  3620. memb_join->failed_list, memb_join->failed_list_entries)) {
  3621. memb_set_merge (&system_from->sin_addr, 1,
  3622. instance->my_failed_list, &instance->my_failed_list_entries);
  3623. } else {
  3624. memb_set_merge (memb_join->failed_list,
  3625. memb_join->failed_list_entries,
  3626. instance->my_failed_list, &instance->my_failed_list_entries);
  3627. }
  3628. memb_state_gather_enter (instance);
  3629. return (1); /* gather entered */
  3630. }
  3631. return (0); /* gather not entered */
  3632. }
  3633. static void memb_join_endian_convert (struct memb_join *in, struct memb_join *out)
  3634. {
  3635. int i;
  3636. out->header.type = in->header.type;
  3637. out->header.endian_detector = ENDIAN_LOCAL;
  3638. out->proc_list_entries = swab32 (in->proc_list_entries);
  3639. out->failed_list_entries = swab32 (in->failed_list_entries);
  3640. out->ring_seq = swab64 (in->ring_seq);
  3641. for (i = 0; i < out->proc_list_entries; i++) {
  3642. out->proc_list[i].s_addr = in->proc_list[i].s_addr;
  3643. }
  3644. for (i = 0; i < out->failed_list_entries; i++) {
  3645. out->failed_list[i].s_addr = in->failed_list[i].s_addr;
  3646. }
  3647. }
  3648. static void memb_commit_token_endian_convert (struct memb_commit_token *in, struct memb_commit_token *out)
  3649. {
  3650. int i;
  3651. out->header.type = in->header.type;
  3652. out->header.endian_detector = ENDIAN_LOCAL;
  3653. out->token_seq = swab32 (in->token_seq);
  3654. out->ring_id.rep.s_addr = in->ring_id.rep.s_addr;
  3655. out->ring_id.seq = swab64 (in->ring_id.seq);
  3656. out->retrans_flg = swab32 (in->retrans_flg);
  3657. out->memb_index = swab32 (in->memb_index);
  3658. out->addr_entries = swab32 (in->addr_entries);
  3659. for (i = 0; i < out->addr_entries; i++) {
  3660. out->addr[i].s_addr = in->addr[i].s_addr;
  3661. out->memb_list[i].ring_id.rep.s_addr =
  3662. in->memb_list[i].ring_id.rep.s_addr;
  3663. out->memb_list[i].ring_id.seq =
  3664. swab64 (in->memb_list[i].ring_id.seq);
  3665. out->memb_list[i].aru = swab32 (in->memb_list[i].aru);
  3666. out->memb_list[i].high_delivered = swab32 (in->memb_list[i].high_delivered);
  3667. out->memb_list[i].received_flg = swab32 (in->memb_list[i].received_flg);
  3668. }
  3669. }
  3670. static void orf_token_endian_convert (struct orf_token *in, struct orf_token *out)
  3671. {
  3672. int i;
  3673. out->header.type = in->header.type;
  3674. out->header.endian_detector = ENDIAN_LOCAL;
  3675. out->seq = swab32 (in->seq);
  3676. out->token_seq = swab32 (in->token_seq);
  3677. out->aru = swab32 (in->aru);
  3678. out->ring_id.rep.s_addr = in->ring_id.rep.s_addr;
  3679. out->ring_id.seq = swab64 (in->ring_id.seq);
  3680. out->fcc = swab32 (in->fcc);
  3681. out->retrans_flg = swab32 (in->retrans_flg);
  3682. out->rtr_list_entries = swab32 (in->rtr_list_entries);
  3683. for (i = 0; i < out->rtr_list_entries; i++) {
  3684. out->rtr_list[i].ring_id.rep.s_addr = in->rtr_list[i].ring_id.rep.s_addr;
  3685. out->rtr_list[i].ring_id.seq = swab64 (in->rtr_list[i].ring_id.seq);
  3686. out->rtr_list[i].seq = swab32 (in->rtr_list[i].seq);
  3687. }
  3688. }
  3689. static void mcast_endian_convert (struct mcast *in, struct mcast *out)
  3690. {
  3691. out->header.type = in->header.type;
  3692. out->header.endian_detector = ENDIAN_LOCAL;
  3693. out->seq = swab32 (in->seq);
  3694. out->ring_id.rep.s_addr = in->ring_id.rep.s_addr;
  3695. out->ring_id.seq = swab64 (in->ring_id.seq);
  3696. out->source = in->source;
  3697. out->guarantee = in->guarantee;
  3698. }
  3699. static int message_handler_memb_join (
  3700. struct totemsrp_instance *instance,
  3701. struct sockaddr_in *system_from,
  3702. struct iovec *iovec,
  3703. int iov_len,
  3704. int bytes_received,
  3705. int endian_conversion_needed)
  3706. {
  3707. struct memb_join *memb_join;
  3708. struct memb_join memb_join_convert;
  3709. int gather_entered;
  3710. if (endian_conversion_needed) {
  3711. memb_join = &memb_join_convert;
  3712. memb_join_endian_convert (iovec->iov_base, &memb_join_convert);
  3713. } else {
  3714. memb_join = (struct memb_join *)iovec->iov_base;
  3715. }
  3716. if (instance->token_ring_id_seq < memb_join->ring_seq) {
  3717. instance->token_ring_id_seq = memb_join->ring_seq;
  3718. }
  3719. switch (instance->memb_state) {
  3720. case MEMB_STATE_OPERATIONAL:
  3721. gather_entered = memb_join_process (instance,
  3722. memb_join, system_from);
  3723. if (gather_entered == 0) {
  3724. memb_state_gather_enter (instance);
  3725. }
  3726. break;
  3727. case MEMB_STATE_GATHER:
  3728. memb_join_process (instance, memb_join, system_from);
  3729. break;
  3730. case MEMB_STATE_COMMIT:
  3731. if (memb_set_subset (&system_from->sin_addr,
  3732. 1,
  3733. instance->my_new_memb_list,
  3734. instance->my_new_memb_entries) &&
  3735. memb_join->ring_seq >= instance->my_ring_id.seq) {
  3736. memb_join_process (instance, memb_join, system_from);
  3737. memb_state_gather_enter (instance);
  3738. }
  3739. break;
  3740. case MEMB_STATE_RECOVERY:
  3741. if (memb_set_subset (&system_from->sin_addr,
  3742. 1,
  3743. instance->my_new_memb_list,
  3744. instance->my_new_memb_entries) &&
  3745. memb_join->ring_seq >= instance->my_ring_id.seq) {
  3746. ring_state_restore (instance);
  3747. memb_join_process (instance,memb_join,
  3748. system_from);
  3749. memb_state_gather_enter (instance);
  3750. }
  3751. break;
  3752. }
  3753. return (0);
  3754. }
  3755. static int message_handler_memb_commit_token (
  3756. struct totemsrp_instance *instance,
  3757. struct sockaddr_in *system_from,
  3758. struct iovec *iovec,
  3759. int iov_len,
  3760. int bytes_received,
  3761. int endian_conversion_needed)
  3762. {
  3763. struct memb_commit_token memb_commit_token_convert;
  3764. struct memb_commit_token *memb_commit_token;
  3765. struct in_addr sub[PROCESSOR_COUNT_MAX];
  3766. int sub_entries;
  3767. if (endian_conversion_needed) {
  3768. memb_commit_token = &memb_commit_token_convert;
  3769. memb_commit_token_endian_convert (iovec->iov_base, memb_commit_token);
  3770. } else {
  3771. memb_commit_token = (struct memb_commit_token *)iovec->iov_base;
  3772. }
  3773. /* TODO do we need to check for a duplicate token?
  3774. if (memb_commit_token->token_seq > 0 &&
  3775. instance->my_token_seq >= memb_commit_token->token_seq) {
  3776. printf ("already received commit token %d %d\n",
  3777. memb_commit_token->token_seq, instance->my_token_seq);
  3778. return (0);
  3779. }
  3780. */
  3781. #ifdef RANDOM_DROP
  3782. if (random()%100 < 10) {
  3783. return (0);
  3784. }
  3785. #endif
  3786. switch (instance->memb_state) {
  3787. case MEMB_STATE_OPERATIONAL:
  3788. /* discard token */
  3789. break;
  3790. case MEMB_STATE_GATHER:
  3791. memb_set_subtract (sub, &sub_entries,
  3792. instance->my_proc_list, instance->my_proc_list_entries,
  3793. instance->my_failed_list, instance->my_failed_list_entries);
  3794. if (memb_set_equal (memb_commit_token->addr,
  3795. memb_commit_token->addr_entries,
  3796. sub,
  3797. sub_entries) &&
  3798. memb_commit_token->ring_id.seq > instance->my_ring_id.seq) {
  3799. memb_state_commit_enter (instance, memb_commit_token);
  3800. }
  3801. break;
  3802. case MEMB_STATE_COMMIT:
  3803. if (memcmp (&memb_commit_token->ring_id, &instance->my_ring_id,
  3804. sizeof (struct memb_ring_id)) == 0) {
  3805. // if (memb_commit_token->ring_id.seq == instance->my_ring_id.seq) {
  3806. memb_state_recovery_enter (instance, memb_commit_token);
  3807. }
  3808. break;
  3809. case MEMB_STATE_RECOVERY:
  3810. instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
  3811. "Sending initial ORF token\n");
  3812. if (instance->my_id.sin_addr.s_addr == instance->my_ring_id.rep.s_addr) {
  3813. // TODO convert instead of initiate
  3814. orf_token_send_initial (instance);
  3815. reset_token_timeout (instance); // REVIEWED
  3816. reset_token_retransmit_timeout (instance); // REVIEWED
  3817. }
  3818. break;
  3819. }
  3820. return (0);
  3821. }
  3822. static int message_handler_token_hold_cancel (
  3823. struct totemsrp_instance *instance,
  3824. struct sockaddr_in *system_from,
  3825. struct iovec *iovec,
  3826. int iov_len,
  3827. int bytes_received,
  3828. int endian_conversion_needed)
  3829. {
  3830. struct token_hold_cancel *token_hold_cancel = (struct token_hold_cancel *)iovec->iov_base;
  3831. if (memcmp (&token_hold_cancel->ring_id, &instance->my_ring_id,
  3832. sizeof (struct memb_ring_id)) == 0) {
  3833. instance->my_seq_unchanged = 0;
  3834. if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr) {
  3835. timer_function_token_retransmit_timeout (instance);
  3836. }
  3837. }
  3838. return (0);
  3839. }
  3840. static int recv_handler (
  3841. poll_handle handle,
  3842. int fd,
  3843. int revents,
  3844. void *data,
  3845. unsigned int *prio)
  3846. {
  3847. struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
  3848. struct msghdr msg_recv;
  3849. struct message_header *message_header;
  3850. struct sockaddr_in system_from;
  3851. int bytes_received;
  3852. int res = 0;
  3853. *prio = UINT_MAX;
  3854. /*
  3855. * Receive datagram
  3856. */
  3857. msg_recv.msg_name = &system_from;
  3858. msg_recv.msg_namelen = sizeof (struct sockaddr_in);
  3859. msg_recv.msg_iov = &instance->totemsrp_iov_recv;
  3860. msg_recv.msg_iovlen = 1;
  3861. msg_recv.msg_control = 0;
  3862. msg_recv.msg_controllen = 0;
  3863. msg_recv.msg_flags = 0;
  3864. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  3865. if (bytes_received == -1) {
  3866. return (0);
  3867. } else {
  3868. instance->stats_recv += bytes_received;
  3869. }
  3870. if (bytes_received < sizeof (struct message_header)) {
  3871. instance->totemsrp_log_printf (instance->totemsrp_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  3872. return (0);
  3873. }
  3874. message_header = (struct message_header *)msg_recv.msg_iov->iov_base;
  3875. /*
  3876. * Authenticate and if authenticated, decrypt datagram
  3877. */
  3878. instance->totemsrp_iov_recv.iov_len = bytes_received;
  3879. res = authenticate_and_decrypt (instance, &instance->totemsrp_iov_recv);
  3880. instance->log_digest = 0;
  3881. if (res == -1) {
  3882. instance->totemsrp_iov_recv.iov_len = PACKET_SIZE_MAX;
  3883. return 0;
  3884. }
  3885. if (instance->stats_tv_start.tv_usec == 0) {
  3886. gettimeofday (&instance->stats_tv_start, NULL);
  3887. }
  3888. /*
  3889. * Handle incoming message
  3890. */
  3891. message_header = (struct message_header *)msg_recv.msg_iov[0].iov_base;
  3892. totemsrp_message_handlers.handler_functions[(int)message_header->type] (
  3893. instance,
  3894. &system_from,
  3895. msg_recv.msg_iov,
  3896. msg_recv.msg_iovlen,
  3897. bytes_received,
  3898. message_header->endian_detector != ENDIAN_LOCAL);
  3899. instance->totemsrp_iov_recv.iov_len = PACKET_SIZE_MAX;
  3900. return (0);
  3901. }