| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668 |
- #define WORKER_THREAD_COUNT 2
- /*
- * Copyright (c) 2003-2005 MontaVista Software, Inc.
- *
- * All rights reserved.
- *
- * Author: Steven Dake (sdake@mvista.com)
- *
- * This software licensed under BSD license, the text of which follows:
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * - Neither the name of the MontaVista Software, Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
- /*
- * The first version of this code was based upon Yair Amir's PhD thesis:
- * http://www.cs.jhu.edu/~yairamir/phd.ps) (ch4,5).
- *
- * The current version of totemsrp implements the Totem protocol specified in:
- * http://citeseer.ist.psu.edu/amir95totem.html
- *
- * The deviations from the above published protocols are:
- * - encryption of message contents with SOBER128
- * - authentication of meessage contents with SHA1/HMAC
- * - token hold mode where token doesn't rotate on unused ring - reduces cpu
- * usage on 1.6ghz xeon from 35% to less then .1 % as measured by top
- */
- #include <assert.h>
- #include <pthread.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <sys/un.h>
- #include <sys/sysinfo.h>
- #include <sys/ioctl.h>
- #include <sys/param.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <linux/if.h>
- #include <linux/sockios.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <signal.h>
- #include <sched.h>
- #include <time.h>
- #include <sys/time.h>
- #include <sys/poll.h>
- #include "aispoll.h"
- #include "totemsrp.h"
- #include "../include/queue.h"
- #include "../include/sq.h"
- #include "../include/list.h"
- #include "hdb.h"
- #include "swab.h"
- #include "crypto.h"
- #define AUTHENTICATION 1 /* use authentication */
- #define ENCRYPTION 1 /* use encryption */
- #define LOCALHOST_IP inet_addr("127.0.0.1")
- #define QUEUE_RTR_ITEMS_SIZE_MAX 2000 /* allow 512 retransmit items */
- #define NEW_MESSAGE_QUEUE_SIZE_MAX 2000 /* allow 500 messages to be queued */
- #define RETRANS_MESSAGE_QUEUE_SIZE_MAX 2000 /* allow 500 messages to be queued */
- #define RECEIVED_MESSAGE_QUEUE_SIZE_MAX 2000 /* allow 500 messages to be queued */
- #define MAXIOVS 5
- #define RETRANSMIT_ENTRIES_MAX 30
- #define MISSING_MCAST_WINDOW 128
- #define TIMEOUT_STATE_GATHER_JOIN 100
- #define TIMEOUT_STATE_GATHER_CONSENSUS 200
- #define TOKEN_RETRANSMITS_BEFORE_LOSS 4
- #define TIMEOUT_TOKEN 200
- #define TIMEOUT_TOKEN_RETRANSMIT (int)(TIMEOUT_TOKEN / (TOKEN_RETRANSMITS_BEFORE_LOSS + 0.2))
- #define TIMEOUT_TOKEN_HOLD (int)(TIMEOUT_TOKEN_RETRANSMIT * 0.8 - (1000/HZ))
- #define TIMEOUT_MERGE_DETECT 200
- #define PACKET_SIZE_MAX 2000
- #define FAIL_TO_RECV_CONST 250
- #define SEQNO_UNCHANGED_CONST 20
- #define TIMEOUT_DOWNCHECK 1000
- /*
- * we compare incoming messages to determine if their endian is
- * different - if so convert them
- *
- * do not change
- */
- #define ENDIAN_LOCAL 0xff22
- enum message_type {
- MESSAGE_TYPE_ORF_TOKEN = 0, /* Ordering, Reliability, Flow (ORF) control Token */
- MESSAGE_TYPE_MCAST = 1, /* ring ordered multicast message */
- MESSAGE_TYPE_MEMB_MERGE_DETECT = 2, /* merge rings if there are available rings */
- MESSAGE_TYPE_MEMB_JOIN = 3, /* membership join message */
- MESSAGE_TYPE_MEMB_COMMIT_TOKEN = 4, /* membership commit token */
- MESSAGE_TYPE_TOKEN_HOLD_CANCEL = 5, /* cancel the holding of the token */
- };
- /*
- * New membership algorithm local variables
- */
- struct consensus_list_item {
- struct in_addr addr;
- int set;
- };
- struct token_callback_instance {
- struct list_head list;
- int (*callback_fn) (enum totem_callback_token_type type, void *);
- enum totem_callback_token_type callback_type;
- int delete;
- void *data;
- };
- struct totemsrp_socket {
- int mcast;
- int token;
- };
- #define HMAC_HASH_SIZE 20
- struct security_header {
- unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
- unsigned char salt[16]; /* random number */
- } __attribute__((packed));
- struct message_header {
- struct security_header security_header;
- char type;
- char encapsulated;
- // unsigned short filler;
- unsigned short endian_detector;
- } __attribute__((packed));
- struct mcast {
- struct message_header header;
- int seq;
- int this_seqno;
- struct memb_ring_id ring_id;
- struct in_addr source;
- int guarantee;
- } __attribute__((packed));
- /*
- * MTU - multicast message header - IP header - UDP header
- *
- * On lossy switches, making use of the DF UDP flag can lead to loss of
- * forward progress. So the packets must be fragmented by a higher layer
- *
- * This layer can only handle packets of MTU size.
- */
- #define FRAGMENT_SIZE (PACKET_SIZE_MAX - sizeof (struct mcast) - 20 - 8)
- struct rtr_item {
- struct memb_ring_id ring_id;
- int seq;
- }__attribute__((packed));
- struct orf_token {
- struct message_header header;
- int seq;
- int token_seq;
- int aru;
- struct in_addr aru_addr;
- struct memb_ring_id ring_id;
- short int fcc;
- int retrans_flg;
- int rtr_list_entries;
- struct rtr_item rtr_list[0];
- }__attribute__((packed));
- struct memb_join {
- struct message_header header;
- struct in_addr proc_list[PROCESSOR_COUNT_MAX];
- int proc_list_entries;
- struct in_addr failed_list[PROCESSOR_COUNT_MAX];
- int failed_list_entries;
- unsigned long long ring_seq;
- } __attribute__((packed));
- struct memb_merge_detect {
- struct message_header header;
- struct memb_ring_id ring_id;
- } __attribute__((packed));
- struct token_hold_cancel {
- struct message_header header;
- struct memb_ring_id ring_id;
- } __attribute__((packed));
- struct memb_commit_token_memb_entry {
- struct memb_ring_id ring_id;
- int aru;
- int high_delivered;
- int received_flg;
- }__attribute__((packed));
- struct memb_commit_token {
- struct message_header header;
- int token_seq;
- struct memb_ring_id ring_id;
- unsigned int retrans_flg;
- int memb_index;
- int addr_entries;
- struct in_addr addr[PROCESSOR_COUNT_MAX];
- struct memb_commit_token_memb_entry memb_list[PROCESSOR_COUNT_MAX];
- }__attribute__((packed));
- struct worker_thread_group {
- int threadcount;
- int last_scheduled;
- struct worker_thread *threads;
- void (*worker_fn) (void *thread_state, void *work_item);
- };
- struct thread_data {
- void *thread_state;
- void *data;
- };
- struct worker_thread {
- struct worker_thread_group *worker_thread_group;
- pthread_mutex_t new_work_mutex;
- pthread_cond_t new_work_cond;
- pthread_cond_t cond;
- pthread_mutex_t done_work_mutex;
- pthread_cond_t done_work_cond;
- pthread_t thread_id;
- struct queue queue;
- void *thread_state;
- struct thread_data thread_data;
- };
- struct mcast_worker_fn_work_item {
- struct sort_queue_item *sort_queue_item;
- struct totemsrp_instance *instance;
- };
- struct message_item {
- struct mcast *mcast;
- struct iovec iovec[MAXIOVS];
- int iov_len;
- };
- struct sort_queue_item {
- struct iovec iovec[MAXIOVS];
- int iov_len;
- };
- struct orf_token_mcast_thread_state {
- char iobuf[9000];
- prng_state prng_state;
- };
- enum memb_state {
- MEMB_STATE_OPERATIONAL = 1,
- MEMB_STATE_GATHER = 2,
- MEMB_STATE_COMMIT = 3,
- MEMB_STATE_RECOVERY = 4
- };
- struct totemsrp_instance {
- /*
- * Authentication of messages
- */
- hmac_state totemsrp_hmac_state;
- prng_state totemsrp_prng_state;
- unsigned char totemsrp_private_key[1024];
- unsigned int totemsrp_private_key_len;
- int stats_sent;
- int stats_recv;
- int stats_delv;
- int stats_remcasts;
- int stats_orf_token;
- struct timeval stats_tv_start;
- /*
- * Flow control mcasts and remcasts on last and current orf_token
- */
- int fcc_remcast_last;
- int fcc_mcast_last;
- int fcc_mcast_current;
- int fcc_remcast_current;
- struct consensus_list_item consensus_list[PROCESSOR_COUNT_MAX];
- int consensus_list_entries;
- struct in_addr my_proc_list[PROCESSOR_COUNT_MAX];
- struct in_addr my_failed_list[PROCESSOR_COUNT_MAX];
- struct in_addr my_new_memb_list[PROCESSOR_COUNT_MAX];
- struct in_addr my_trans_memb_list[PROCESSOR_COUNT_MAX];
- struct in_addr my_memb_list[PROCESSOR_COUNT_MAX];
- struct in_addr my_deliver_memb_list[PROCESSOR_COUNT_MAX];
- int my_proc_list_entries;
- int my_failed_list_entries;
- int my_new_memb_entries;
- int my_trans_memb_entries;
- int my_memb_entries;
- int my_deliver_memb_entries;
- struct memb_ring_id my_ring_id;
- struct memb_ring_id my_old_ring_id;
- int my_aru_count;
- int my_merge_detect_timeout_outstanding;
- int my_last_aru;
- int my_seq_unchanged;
- int my_received_flg;
- int my_high_seq_received;
- int my_install_seq;
- int my_rotation_counter;
- int my_set_retrans_flg;
- int my_retrans_flg_count;
- unsigned int my_high_ring_delivered;
- unsigned int timeout_token;
- unsigned int timeout_token_retransmit;
- unsigned int timeout_token_hold;
- unsigned int token_retransmits_before_loss;
- unsigned int timeout_state_gather_join;
- unsigned int timeout_state_gather_consensus;
- unsigned int timeout_merge_detect;
- unsigned int timeout_downcheck;
- unsigned int fail_to_recv_const;
- /*
- * Queues used to order, deliver, and recover messages
- */
- struct queue new_message_queue;
- struct queue retrans_message_queue;
- struct sq regular_sort_queue;
- struct sq recovery_sort_queue;
- /*
- * File descriptors in use by TOTEMSRP
- */
- struct totemsrp_socket totemsrp_sockets[2];
- /*
- * Received up to and including
- */
- int my_aru;
- int my_high_delivered;
- struct list_head token_callback_received_listhead;
- struct list_head token_callback_sent_listhead;
- char orf_token_retransmit[15000]; // sizeof (struct orf_token) + sizeof (struct rtr_item) * RETRANSMIT_ENTRIES_MAX];
- int orf_token_retransmit_size;
- int my_token_seq;
- /*
- * Timers
- */
- poll_timer_handle timer_orf_token_timeout;
- poll_timer_handle timer_orf_token_retransmit_timeout;
- poll_timer_handle timer_orf_token_hold_retransmit_timeout;
- poll_timer_handle timer_merge_detect_timeout;
- poll_timer_handle memb_timer_state_gather_join_timeout;
- poll_timer_handle memb_timer_state_gather_consensus_timeout;
- poll_timer_handle memb_timer_state_commit_timeout;
- poll_timer_handle timer_netif_check_timeout;
- /*
- * Function and data used to log messages
- */
- int totemsrp_log_level_security;
- int totemsrp_log_level_error;
- int totemsrp_log_level_warning;
- int totemsrp_log_level_notice;
- int totemsrp_log_level_debug;
- void (*totemsrp_log_printf) (int level, char *format, ...);
- enum memb_state memb_state;
- struct sockaddr_in my_id;
- struct sockaddr_in next_memb;
- struct sockaddr_in memb_local_sockaddr_in;
- char iov_buffer[15000]; //PACKET_SIZE_MAX];
- struct iovec totemsrp_iov_recv;
- poll_handle *totemsrp_poll_handle;
- struct totem_interface *totemsrp_interfaces;
- int totemsrp_interface_count;
- int netif_state_report;
- int netif_bind_state;
- struct worker_thread_group worker_thread_group;
- struct worker_thread_group worker_thread_group_orf_token_mcast;
- /*
- * Function called when new message received
- */
- int (*totemsrp_recv) (char *group, struct iovec *iovec, int iov_len);
- /*
- * Multicast address
- */
- struct sockaddr_in sockaddr_in_mcast;
- void (*totemsrp_deliver_fn) (
- struct in_addr source_addr,
- struct iovec *iovec,
- int iov_len,
- int endian_conversion_required);
- void (*totemsrp_confchg_fn) (
- enum totem_configuration_type configuration_type,
- struct in_addr *member_list, int member_list_entries,
- struct in_addr *left_list, int left_list_entries,
- struct in_addr *joined_list, int joined_list_entries,
- struct memb_ring_id *ring_id);
- char iov_encrypted_buffer[15000];
- struct iovec iov_encrypted;
- int global_seqno;
- int my_token_held;
- unsigned long long token_ring_id_seq;
- int log_digest;
- int last_released;
- int set_aru;
- int totemsrp_brake;
- int old_ring_state_saved;
- int old_ring_state_aru;
- int old_ring_state_high_seq_received;
- int ring_saved;
- int my_last_seq;
- struct timeval tv_old;
- int firstrun;
- };
- struct message_handlers {
- int count;
- int (*handler_functions[6]) (struct totemsrp_instance *,
- struct sockaddr_in *, struct iovec *, int, int, int);
- };
- /*
- * forward decls
- */
- static int message_handler_orf_token (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
- static int message_handler_mcast (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
- static int message_handler_memb_merge_detect (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
- static int message_handler_memb_join (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
- static int message_handler_memb_commit_token (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
- static int message_handler_token_hold_cancel (struct totemsrp_instance *, struct sockaddr_in *, struct iovec *, int, int, int);
- static void memb_ring_id_create_or_load (struct totemsrp_instance *, struct memb_ring_id *);
- static int recv_handler (poll_handle handle, int fd, int revents, void *data, unsigned int *prio);
- static int netif_determine (struct totemsrp_instance *instance, struct sockaddr_in *bindnet, struct sockaddr_in *bound_to,int *interface_up);
- static int loopback_determine (struct sockaddr_in *bound_to);
- static void netif_down_check (struct totemsrp_instance *instance);
- static void token_callbacks_execute (struct totemsrp_instance *instance, enum totem_callback_token_type type);
- #define NETIF_STATE_REPORT_UP 1
- #define NETIF_STATE_REPORT_DOWN 2
- #define BIND_STATE_UNBOUND 0
- #define BIND_STATE_REGULAR 1
- #define BIND_STATE_LOOPBACK 2
- static int totemsrp_build_sockets (
- struct totemsrp_instance *instance,
- struct sockaddr_in *sockaddr_mcast,
- struct sockaddr_in *sockaddr_bindnet,
- struct totemsrp_socket *sockets,
- struct sockaddr_in *bound_to,
- int *interface_up);
- static int totemsrp_build_sockets_loopback (
- struct totemsrp_instance *instance,
- struct sockaddr_in *sockaddr_mcast,
- struct sockaddr_in *sockaddr_bindnet,
- struct totemsrp_socket *sockets,
- struct sockaddr_in *bound_to);
- static void memb_state_gather_enter (struct totemsrp_instance *instance);
- static void messages_deliver_to_app (struct totemsrp_instance *instance, int skip, int end_point);
- static int orf_token_mcast (struct totemsrp_instance *instance, struct orf_token *oken,
- int fcc_mcasts_allowed, struct sockaddr_in *system_from);
- static int messages_free (struct totemsrp_instance *instance, int token_aru);
- static void encrypt_and_sign (struct totemsrp_instance *instance, struct iovec *iovec, int iov_len);
- static int authenticate_and_decrypt (struct totemsrp_instance *instance, struct iovec *iov);
- static int recv_handler (poll_handle handle, int fd, int revents, void *data, unsigned int *prio);
- static void memb_ring_id_store (struct totemsrp_instance *instance, struct memb_commit_token *commit_token);
- static void memb_state_commit_token_update (struct totemsrp_instance *instance, struct memb_commit_token *memb_commit_token);
- static int memb_state_commit_token_send (struct totemsrp_instance *instance, struct memb_commit_token *memb_commit_token);
- static void memb_state_commit_token_create (struct totemsrp_instance *instance, struct memb_commit_token *commit_token);
- static int token_hold_cancel_send (struct totemsrp_instance *instance);
- static void orf_token_endian_convert (struct orf_token *in, struct orf_token *out);
- static void memb_commit_token_endian_convert (struct memb_commit_token *in, struct memb_commit_token *out);
- static void memb_join_endian_convert (struct memb_join *in, struct memb_join *out);
- static void mcast_endian_convert (struct mcast *in, struct mcast *out);
- static void timer_function_orf_token_timeout (void *data);
- static void timer_function_token_retransmit_timeout (void *data);
- static void timer_function_token_hold_retransmit_timeout (void *data);
- static void timer_function_merge_detect_timeout (void *data);
- /*
- * All instances in one database
- */
- static struct saHandleDatabase totemsrp_instance_database = {
- .handleCount = 0,
- .handles = 0,
- .handleInstanceDestructor = 0
- };
- struct message_handlers totemsrp_message_handlers = {
- 6,
- {
- message_handler_orf_token,
- message_handler_mcast,
- message_handler_memb_merge_detect,
- message_handler_memb_join,
- message_handler_memb_commit_token,
- message_handler_token_hold_cancel
- }
- };
- void instance_initialize (struct totemsrp_instance *instance)
- {
- memset (instance, 0, sizeof (struct totemsrp_instance));
- list_init (&instance->token_callback_received_listhead);
- list_init (&instance->token_callback_sent_listhead);
- instance->my_received_flg = 1;
- instance->timeout_token = TIMEOUT_TOKEN;
- instance->timeout_token_retransmit = TIMEOUT_TOKEN_RETRANSMIT;
- instance->token_retransmits_before_loss = TOKEN_RETRANSMITS_BEFORE_LOSS;
- instance->timeout_state_gather_join = TIMEOUT_STATE_GATHER_JOIN;
- instance->timeout_state_gather_consensus = TIMEOUT_STATE_GATHER_CONSENSUS;
- instance->timeout_merge_detect = TIMEOUT_MERGE_DETECT;
- instance->timeout_downcheck = TIMEOUT_DOWNCHECK;
- instance->fail_to_recv_const = FAIL_TO_RECV_CONST;
- instance->my_token_seq = -1;
- instance->memb_state = MEMB_STATE_OPERATIONAL;
- instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
- instance->totemsrp_iov_recv.iov_base = instance->iov_buffer;
- instance->totemsrp_iov_recv.iov_len = sizeof (instance->iov_buffer);
- instance->iov_encrypted.iov_base = instance->iov_encrypted_buffer;
- instance->iov_encrypted.iov_len = sizeof (instance->iov_encrypted_buffer);
- instance->set_aru = -1;
- }
- #ifdef CODE_COVERAGE_COMPILE_OUT
- void print_digest (char *where, unsigned char *digest)
- {
- int i;
- printf ("DIGEST %s:\n", where);
- for (i = 0; i < 16; i++) {
- printf ("%x ", digest[i]);
- }
- printf ("\n");
- }
- void print_msg (unsigned char *msg, int size)
- {
- int i;
- printf ("MSG CONTENTS START\n");
- for (i = 0; i < size; i++) {
- printf ("%x ", msg[i]);
- if ((i % 16) == 15) {
- printf ("\n");
- }
- }
- printf ("MSG CONTENTS DONE\n");
- }
- #endif
- static void orf_token_mcast_worker_fn (void *thread_state, void *work_item);
- static void *worker_thread (void *thread_data_in) {
- struct thread_data *thread_data = (struct thread_data *)thread_data_in;
- struct orf_token_mcast_thread_state *orf_token_mcast_thread_state =
- (struct orf_token_mcast_thread_state *)thread_data->thread_state;
- struct worker_thread *worker_thread =
- (struct worker_thread *)thread_data->data;
- void *data_for_worker_fn;
- for (;;) {
- pthread_mutex_lock (&worker_thread->new_work_mutex);
- if (queue_is_empty (&worker_thread->queue) == 1) {
- pthread_cond_wait (&worker_thread->new_work_cond,
- &worker_thread->new_work_mutex);
- }
- data_for_worker_fn = queue_item_get (&worker_thread->queue);
- worker_thread->worker_thread_group->worker_fn (orf_token_mcast_thread_state, data_for_worker_fn);
- queue_item_remove (&worker_thread->queue);
- pthread_mutex_unlock (&worker_thread->new_work_mutex);
- pthread_mutex_lock (&worker_thread->done_work_mutex);
- if (queue_is_empty (&worker_thread->queue) == 1) {
- pthread_cond_signal (&worker_thread->done_work_cond);
- }
- pthread_mutex_unlock (&worker_thread->done_work_mutex);
- }
- return (0);
- }
- static int worker_thread_group_init (
- struct worker_thread_group *worker_thread_group,
- int threads,
- int items_max,
- int item_size,
- int thread_state_size,
- void (*thread_state_constructor)(void *),
- void (*worker_fn)(void *thread_state, void *work_item))
- {
- int i;
- worker_thread_group->threadcount = threads;
- worker_thread_group->last_scheduled = 0;
- worker_thread_group->worker_fn = worker_fn;
- worker_thread_group->threads = malloc (sizeof (struct worker_thread) *
- threads);
- if (worker_thread_group->threads == 0) {
- return (-1);
- }
- for (i = 0; i < threads; i++) {
- worker_thread_group->threads[i].thread_state = malloc (thread_state_size);
- thread_state_constructor (worker_thread_group->threads[i].thread_state);
- worker_thread_group->threads[i].worker_thread_group = worker_thread_group;
- pthread_mutex_init (&worker_thread_group->threads[i].new_work_mutex, NULL);
- pthread_cond_init (&worker_thread_group->threads[i].new_work_cond, NULL);
- pthread_mutex_init (&worker_thread_group->threads[i].done_work_mutex, NULL);
- pthread_cond_init (&worker_thread_group->threads[i].done_work_cond, NULL);
- queue_init (&worker_thread_group->threads[i].queue, items_max,
- item_size);
- worker_thread_group->threads[i].thread_data.thread_state =
- worker_thread_group->threads[i].thread_state;
- worker_thread_group->threads[i].thread_data.data = &worker_thread_group->threads[i];
- pthread_create (&worker_thread_group->threads[i].thread_id,
- NULL, worker_thread, &worker_thread_group->threads[i].thread_data);
- }
- return (0);
- }
- static void worker_thread_group_exit (
- struct worker_thread_group *worker_thread_group)
- {
- int i;
- for (i = 0; i < worker_thread_group->threadcount; i++) {
- pthread_cancel (worker_thread_group->threads[i].thread_id);
- }
- }
- static void worker_thread_group_work_add (
- struct worker_thread_group *worker_thread_group,
- void *item)
- {
- int schedule;
- schedule = (worker_thread_group->last_scheduled + 1) % (worker_thread_group->threadcount);
- worker_thread_group->last_scheduled = schedule;
- pthread_mutex_lock (&worker_thread_group->threads[schedule].new_work_mutex);
- queue_item_add (&worker_thread_group->threads[schedule].queue, item);
- pthread_cond_signal (&worker_thread_group->threads[schedule].new_work_cond);
- pthread_mutex_unlock (&worker_thread_group->threads[schedule].new_work_mutex);
- }
- static void worker_thread_group_wait (
- struct worker_thread_group *worker_thread_group)
- {
- int i;
- for (i = 0; i < worker_thread_group->threadcount; i++) {
- pthread_mutex_lock (&worker_thread_group->threads[i].done_work_mutex);
- if (queue_is_empty (&worker_thread_group->threads[i].queue) == 0) {
- pthread_cond_wait (&worker_thread_group->threads[i].done_work_cond,
- &worker_thread_group->threads[i].done_work_mutex);
- }
- pthread_mutex_unlock (&worker_thread_group->threads[i].done_work_mutex);
- }
- }
- static void orf_token_mcast_thread_state_constructor (
- void *orf_token_mcast_thread_state_in)
- {
- struct orf_token_mcast_thread_state *orf_token_mcast_thread_state =
- (struct orf_token_mcast_thread_state *)orf_token_mcast_thread_state_in;
- memset (orf_token_mcast_thread_state, 0,
- sizeof (orf_token_mcast_thread_state));
- rng_make_prng (128, PRNG_SOBER,
- &orf_token_mcast_thread_state->prng_state, NULL);
- }
- /*
- * Exported interfaces
- */
- int totemsrp_initialize (
- poll_handle *poll_handle,
- totemsrp_handle *handle,
- struct totem_config *totem_config,
- void (*deliver_fn) (
- struct in_addr source_addr,
- struct iovec *iovec,
- int iov_len,
- int endian_conversion_required),
- void (*confchg_fn) (
- enum totem_configuration_type configuration_type,
- struct in_addr *member_list, int member_list_entries,
- struct in_addr *left_list, int left_list_entries,
- struct in_addr *joined_list, int joined_list_entries,
- struct memb_ring_id *ring_id))
- {
- unsigned int *timeouts = totem_config->timeouts;
- struct totemsrp_instance *instance;
- SaErrorT error;
- int i;
- error = saHandleCreate (&totemsrp_instance_database,
- sizeof (struct totemsrp_instance), handle);
- if (error != SA_OK) {
- goto error_exit;
- }
- error = saHandleInstanceGet (&totemsrp_instance_database, *handle,
- (void *)&instance);
- if (error != SA_OK) {
- goto error_destroy;
- }
- instance_initialize (instance);
- /*
- * Configure logging
- */
- instance->totemsrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
- instance->totemsrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
- instance->totemsrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
- instance->totemsrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
- instance->totemsrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
- instance->totemsrp_log_printf = totem_config->totem_logging_configuration.log_printf;
- instance->timeout_token_hold = (int)(instance->timeout_token_retransmit * 0.8 - (1000/HZ));
- /*
- * Initialize random number generator for later use to generate salt
- */
- memcpy (instance->totemsrp_private_key, totem_config->private_key,
- totem_config->private_key_len);
- instance->totemsrp_private_key_len = totem_config->private_key_len;
- rng_make_prng (128, PRNG_SOBER, &instance->totemsrp_prng_state, NULL);
- /*
- * Initialize local variables for totemsrp
- */
- memcpy (&instance->sockaddr_in_mcast, &totem_config->mcast_addr,
- sizeof (struct sockaddr_in));
- memset (&instance->next_memb, 0, sizeof (struct sockaddr_in));
- memset (instance->iov_buffer, 0, PACKET_SIZE_MAX);
- /*
- * Initialize thread group data structure
- */
- worker_thread_group_init (&instance->worker_thread_group_orf_token_mcast,
- WORKER_THREAD_COUNT, 128, sizeof (struct mcast_worker_fn_work_item),
- sizeof (struct orf_token_mcast_thread_state),
- orf_token_mcast_thread_state_constructor,
- orf_token_mcast_worker_fn);
- /*
- * Update our timeout values if they were specified in the openais.conf
- * file.
- */
- for (i = 0; i < MAX_TOTEM_TIMEOUTS; i++) {
- if (!timeouts[i]) {
- continue;
- }
- switch (i) {
- case TOTEM_TOKEN:
- instance->timeout_token = timeouts[i];
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Overriding token timeout to (%u ms)\n", timeouts[i]);
- instance->timeout_token_retransmit = (int)(instance->timeout_token / (instance->token_retransmits_before_loss + 0.2));
- instance->timeout_token_hold = (int)(instance->timeout_token_retransmit * 0.8 - (1000/HZ));
- break;
- case TOTEM_RETRANSMIT_TOKEN:
- instance->timeout_token_retransmit = timeouts[i];
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Overriding token retransmit timeout to (%u ms)\n", timeouts[i]);
- break;
- case TOTEM_RETRANSMITS_BEFORE_LOSS:
- instance->token_retransmits_before_loss = timeouts[i];
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Overriding retransmits before loss (%u retrans)\n", timeouts[i]);
- break;
- case TOTEM_HOLD_TOKEN:
- instance->timeout_token_hold = timeouts[i];
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Overriding token hold timeout to (%u ms)\n", timeouts[i]);
- break;
- case TOTEM_JOIN:
- instance->timeout_state_gather_join = timeouts[i];
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Join Timeout set to %u ms\n", timeouts[i]);
- break;
- case TOTEM_CONSENSUS:
- instance->timeout_state_gather_consensus = timeouts[i];
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Consensus Timeout set to %u ms\n", timeouts[i]);
- break;
- case TOTEM_MERGE:
- instance->timeout_merge_detect = timeouts[i];
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Merge Detect Timeout set to %u ms\n", timeouts[i]);
- break;
- case TOTEM_DOWNCHECK:
- instance->timeout_downcheck = timeouts[i];
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Downcheck Timeout set to %u ms\n", timeouts[i]);
- break;
- case TOTEM_FAIL_RECV_CONST:
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Failed To Receive Const set to %u\n", timeouts[i]);
- instance->fail_to_recv_const = timeouts[i];
- break;
- default:
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Received unknown timeout type: %d\n", timeouts[i]);
- break;
- }
- }
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Token Timeout (%d ms) retransmit timeout (%d ms)\n",
- instance->timeout_token, instance->timeout_token_retransmit);
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "token hold (%d ms) retransmits before loss (%d retrans)\n",
- instance->timeout_token_hold, instance->token_retransmits_before_loss);
- queue_init (&instance->new_message_queue, NEW_MESSAGE_QUEUE_SIZE_MAX,
- sizeof (struct message_item));
- queue_init (&instance->retrans_message_queue, RETRANS_MESSAGE_QUEUE_SIZE_MAX,
- sizeof (struct message_item));
- sq_init (&instance->regular_sort_queue,
- QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct sort_queue_item), 0);
- sq_init (&instance->recovery_sort_queue,
- QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct sort_queue_item), 0);
- instance->totemsrp_interfaces = totem_config->interfaces;
- instance->totemsrp_interface_count = 1;
- instance->totemsrp_poll_handle = poll_handle;
- netif_down_check (instance);
- memb_state_gather_enter (instance);
- instance->totemsrp_deliver_fn = deliver_fn;
- instance->totemsrp_confchg_fn = confchg_fn;
- return (0);
- error_destroy:
- saHandleDestroy (&totemsrp_instance_database, *handle);
- error_exit:
- return (-1);
- }
- void totemsrp_finalize (
- totemsrp_handle handle)
- {
- struct totemsrp_instance *instance;
- SaErrorT error;
- error = saHandleInstanceGet (&totemsrp_instance_database, handle,
- (void *)&instance);
- if (error != SA_OK) {
- return;
- }
- worker_thread_group_exit (&instance->worker_thread_group_orf_token_mcast);
- saHandleInstancePut (&totemsrp_instance_database, handle);
- }
- /*
- * Set operations for use by the membership algorithm
- */
- static void memb_consensus_reset (struct totemsrp_instance *instance)
- {
- instance->consensus_list_entries = 0;
- }
- static void memb_set_subtract (
- struct in_addr *out_list, int *out_list_entries,
- struct in_addr *one_list, int one_list_entries,
- struct in_addr *two_list, int two_list_entries)
- {
- int found = 0;
- int i;
- int j;
- *out_list_entries = 0;
- for (i = 0; i < one_list_entries; i++) {
- for (j = 0; j < two_list_entries; j++) {
- if (one_list[i].s_addr == two_list[j].s_addr) {
- found = 1;
- break;
- }
- }
- if (found == 0) {
- out_list[*out_list_entries].s_addr = one_list[i].s_addr;
- *out_list_entries = *out_list_entries + 1;
- }
- found = 0;
- }
- }
- /*
- * Set consensus for a specific processor
- */
- static void memb_consensus_set (
- struct totemsrp_instance *instance,
- struct in_addr *addr)
- {
- int found = 0;
- int i;
- for (i = 0; i < instance->consensus_list_entries; i++) {
- if (addr->s_addr == instance->consensus_list[i].addr.s_addr) {
- found = 1;
- break; /* found entry */
- }
- }
- instance->consensus_list[i].addr.s_addr = addr->s_addr;
- instance->consensus_list[i].set = 1;
- if (found == 0) {
- instance->consensus_list_entries++;
- }
- return;
- }
- /*
- * Is consensus set for a specific processor
- */
- static int memb_consensus_isset (
- struct totemsrp_instance *instance,
- struct in_addr *addr)
- {
- int i;
- for (i = 0; i < instance->consensus_list_entries; i++) {
- if (addr->s_addr == instance->consensus_list[i].addr.s_addr) {
- return (instance->consensus_list[i].set);
- }
- }
- return (0);
- }
- /*
- * Is consensus agreed upon based upon consensus database
- */
- static int memb_consensus_agreed (
- struct totemsrp_instance *instance)
- {
- struct in_addr token_memb[PROCESSOR_COUNT_MAX];
- int token_memb_entries = 0;
- int agreed = 1;
- int i;
- memb_set_subtract (token_memb, &token_memb_entries,
- instance->my_proc_list, instance->my_proc_list_entries,
- instance->my_failed_list, instance->my_failed_list_entries);
- for (i = 0; i < token_memb_entries; i++) {
- if (memb_consensus_isset (instance, &token_memb[i]) == 0) {
- agreed = 0;
- break;
- }
- }
- assert (token_memb_entries >= 1);
- return (agreed);
- }
- static void memb_consensus_notset (
- struct totemsrp_instance *instance,
- struct in_addr *no_consensus_list,
- int *no_consensus_list_entries,
- struct in_addr *comparison_list,
- int comparison_list_entries)
- {
- int i;
- *no_consensus_list_entries = 0;
- for (i = 0; i < instance->my_proc_list_entries; i++) {
- if (memb_consensus_isset (instance, &instance->my_proc_list[i]) == 0) {
- no_consensus_list[*no_consensus_list_entries].s_addr = instance->my_proc_list[i].s_addr;
- *no_consensus_list_entries = *no_consensus_list_entries + 1;
- }
- }
- }
- /*
- * Is set1 equal to set2 Entries can be in different orders
- */
- static int memb_set_equal (struct in_addr *set1, int set1_entries,
- struct in_addr *set2, int set2_entries)
- {
- int i;
- int j;
- int found = 0;
- if (set1_entries != set2_entries) {
- return (0);
- }
- for (i = 0; i < set2_entries; i++) {
- for (j = 0; j < set1_entries; j++) {
- if (set1[j].s_addr == set2[i].s_addr) {
- found = 1;
- break;
- }
- }
- if (found == 0) {
- return (0);
- }
- found = 0;
- }
- return (1);
- }
- /*
- * Is subset fully contained in fullset
- */
- static int memb_set_subset (
- struct in_addr *subset, int subset_entries,
- struct in_addr *fullset, int fullset_entries)
- {
- int i;
- int j;
- int found = 0;
- if (subset_entries > fullset_entries) {
- return (0);
- }
- for (i = 0; i < subset_entries; i++) {
- for (j = 0; j < fullset_entries; j++) {
- if (subset[i].s_addr == fullset[j].s_addr) {
- found = 1;
- }
- }
- if (found == 0) {
- return (0);
- }
- found = 1;
- }
- return (1);
- }
- /*
- * merge subset into fullset taking care not to add duplicates
- */
- static void memb_set_merge (
- struct in_addr *subset, int subset_entries,
- struct in_addr *fullset, int *fullset_entries)
- {
- int found = 0;
- int i;
- int j;
- for (i = 0; i < subset_entries; i++) {
- for (j = 0; j < *fullset_entries; j++) {
- if (fullset[j].s_addr == subset[i].s_addr) {
- found = 1;
- break;
- }
- }
- if (found == 0) {
- fullset[j].s_addr = subset[i].s_addr;
- *fullset_entries = *fullset_entries + 1;
- }
- found = 0;
- }
- return;
- }
- static void memb_set_and (
- struct in_addr *set1, int set1_entries,
- struct in_addr *set2, int set2_entries,
- struct in_addr *and, int *and_entries)
- {
- int i;
- int j;
- int found = 0;
- *and_entries = 0;
- for (i = 0; i < set2_entries; i++) {
- for (j = 0; j < set1_entries; j++) {
- if (set1[j].s_addr == set2[i].s_addr) {
- found = 1;
- break;
- }
- }
- if (found) {
- and[*and_entries].s_addr = set1[j].s_addr;
- *and_entries = *and_entries + 1;
- }
- found = 0;
- }
- return;
- }
- #ifdef CODE_COVERGE
- static void memb_set_print (
- char *string,
- struct in_addr *list,
- int list_entries)
- {
- int i;
- printf ("List '%s' contains %d entries:\n", string, list_entries);
- for (i = 0; i < list_entries; i++) {
- printf ("addr %s\n", inet_ntoa (list[i]));
- }
- }
- #endif
- static void reset_token_retransmit_timeout (struct totemsrp_instance *instance)
- {
- poll_timer_delete (*instance->totemsrp_poll_handle,
- instance->timer_orf_token_retransmit_timeout);
- poll_timer_add (*instance->totemsrp_poll_handle,
- instance->timeout_token_retransmit,
- (void *)instance,
- timer_function_token_retransmit_timeout,
- &instance->timer_orf_token_retransmit_timeout);
- }
- static void start_merge_detect_timeout (struct totemsrp_instance *instance)
- {
- if (instance->my_merge_detect_timeout_outstanding == 0) {
- poll_timer_add (*instance->totemsrp_poll_handle,
- instance->timeout_merge_detect,
- (void *)instance,
- timer_function_merge_detect_timeout,
- &instance->timer_merge_detect_timeout);
- instance->my_merge_detect_timeout_outstanding = 1;
- }
- }
- static void cancel_merge_detect_timeout (struct totemsrp_instance *instance)
- {
- poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_merge_detect_timeout);
- instance->my_merge_detect_timeout_outstanding = 0;
- }
- /*
- * ring_state_* is used to save and restore the sort queue
- * state when a recovery operation fails (and enters gather)
- */
- static void old_ring_state_save (struct totemsrp_instance *instance)
- {
- if (instance->old_ring_state_saved == 0) {
- instance->old_ring_state_saved = 1;
- instance->old_ring_state_aru = instance->my_aru;
- instance->old_ring_state_high_seq_received = instance->my_high_seq_received;
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Saving state aru %d high seq recieved %d\n",
- instance->my_aru, instance->my_high_seq_received);
- }
- }
- static void ring_save (struct totemsrp_instance *instance)
- {
- if (instance->ring_saved == 0) {
- instance->ring_saved = 1;
- memcpy (&instance->my_old_ring_id, &instance->my_ring_id,
- sizeof (struct memb_ring_id));
- }
- }
- static void ring_reset (struct totemsrp_instance *instance)
- {
- instance->ring_saved = 0;
- }
- static void ring_state_restore (struct totemsrp_instance *instance)
- {
- if (instance->old_ring_state_saved) {
- instance->my_ring_id.rep.s_addr = 0;
- instance->my_aru = instance->old_ring_state_aru;
- instance->my_high_seq_received = instance->old_ring_state_high_seq_received;
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "Restoring instance->my_aru %d my high seq received %d\n",
- instance->my_aru, instance->my_high_seq_received);
- }
- }
- static void old_ring_state_reset (struct totemsrp_instance *instance)
- {
- instance->old_ring_state_saved = 0;
- }
- static void reset_token_timeout (struct totemsrp_instance *instance) {
- poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_orf_token_timeout);
- poll_timer_add (*instance->totemsrp_poll_handle,
- instance->timeout_token,
- (void *)instance,
- timer_function_orf_token_timeout,
- &instance->timer_orf_token_timeout);
- }
- static void cancel_token_timeout (struct totemsrp_instance *instance) {
- poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_orf_token_timeout);
- }
- static void cancel_token_retransmit_timeout (struct totemsrp_instance *instance)
- {
- poll_timer_delete (*instance->totemsrp_poll_handle, instance->timer_orf_token_retransmit_timeout);
- }
- static void start_token_hold_retransmit_timeout (struct totemsrp_instance *instance)
- {
- poll_timer_add (*instance->totemsrp_poll_handle,
- instance->timeout_token_hold,
- (void *)instance,
- timer_function_token_hold_retransmit_timeout,
- &instance->timer_orf_token_hold_retransmit_timeout);
- }
- static void cancel_token_hold_retransmit_timeout (struct totemsrp_instance *instance)
- {
- poll_timer_delete (*instance->totemsrp_poll_handle,
- instance->timer_orf_token_hold_retransmit_timeout);
- }
- static void memb_state_consensus_timeout_expired (
- struct totemsrp_instance *instance)
- {
- struct in_addr no_consensus_list[PROCESSOR_COUNT_MAX];
- int no_consensus_list_entries;
- if (memb_consensus_agreed (instance)) {
- memb_consensus_reset (instance);
- memb_consensus_set (instance, &instance->my_id.sin_addr);
- reset_token_timeout (instance); // REVIEWED
- } else {
- memb_consensus_notset (instance, no_consensus_list,
- &no_consensus_list_entries,
- instance->my_proc_list, instance->my_proc_list_entries);
- memb_set_merge (no_consensus_list, no_consensus_list_entries,
- instance->my_failed_list, &instance->my_failed_list_entries);
- memb_state_gather_enter (instance);
- }
- }
- static int memb_join_message_send (struct totemsrp_instance *instance);
- static int memb_merge_detect_transmit (struct totemsrp_instance *instance);
- /*
- * Timers used for various states of the membership algorithm
- */
- static void timer_function_orf_token_timeout (void *data)
- {
- struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "The token was lost in state %d from timer %x\n", instance->memb_state, data);
- switch (instance->memb_state) {
- case MEMB_STATE_OPERATIONAL:
- netif_down_check (instance);
- memb_state_gather_enter (instance);
- break;
- case MEMB_STATE_GATHER:
- memb_state_consensus_timeout_expired (instance);
- memb_state_gather_enter (instance);
- break;
- case MEMB_STATE_COMMIT:
- memb_state_gather_enter (instance);
- break;
-
- case MEMB_STATE_RECOVERY:
- ring_state_restore (instance);
- memb_state_gather_enter (instance);
- break;
- }
- }
- static void memb_timer_function_state_gather (void *data)
- {
- struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
- switch (instance->memb_state) {
- case MEMB_STATE_OPERATIONAL:
- case MEMB_STATE_RECOVERY:
- assert (0); /* this should never happen */
- break;
- case MEMB_STATE_GATHER:
- case MEMB_STATE_COMMIT:
- memb_join_message_send (instance);
- /*
- * Restart the join timeout
- `*/
- poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
-
- poll_timer_add (*instance->totemsrp_poll_handle,
- instance->timeout_state_gather_join,
- (void *)instance,
- memb_timer_function_state_gather,
- &instance->memb_timer_state_gather_join_timeout);
- break;
- }
- }
- static void memb_timer_function_gather_consensus_timeout (void *data)
- {
- struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
- memb_state_consensus_timeout_expired (instance);
- }
- static void deliver_messages_from_recovery_to_regular (struct totemsrp_instance *instance)
- {
- int i;
- struct sort_queue_item *recovery_message_item;
- struct sort_queue_item regular_message_item;
- int res;
- void *ptr;
- struct mcast *mcast;
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "recovery to regular %d-%d\n", 1, instance->my_aru);
- /*
- * Move messages from recovery to regular sort queue
- */
- // todo should i be initialized to 0 or 1 ?
- for (i = 1; i <= instance->my_aru; i++) {
- res = sq_item_get (&instance->recovery_sort_queue, i, &ptr);
- if (res != 0) {
- continue;
- }
- printf ("Transferring message with seq id %d\n", i);
- recovery_message_item = (struct sort_queue_item *)ptr;
- /*
- * Convert recovery message into regular message
- */
- if (recovery_message_item->iov_len > 1) {
- mcast = recovery_message_item->iovec[1].iov_base;
- memcpy (®ular_message_item.iovec[0],
- &recovery_message_item->iovec[1],
- sizeof (struct iovec) * recovery_message_item->iov_len);
- } else {
- mcast = recovery_message_item->iovec[0].iov_base;
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "encapsulated is %d\n",
- mcast->header.encapsulated);
- if (mcast->header.encapsulated == 1) {
- /*
- * Message is a recovery message encapsulated
- * in a new ring message
- */
- regular_message_item.iovec[0].iov_base =
- recovery_message_item->iovec[0].iov_base + sizeof (struct mcast);
- regular_message_item.iovec[0].iov_len =
- recovery_message_item->iovec[0].iov_len - sizeof (struct mcast);
- regular_message_item.iov_len = 1;
- mcast = regular_message_item.iovec[0].iov_base;
- } else {
- printf ("not encapsulated\n");
- continue; /* TODO this case shouldn't happen */
- /*
- * Message is originated on new ring and not
- * encapsulated
- */
- regular_message_item.iovec[0].iov_base =
- recovery_message_item->iovec[0].iov_base;
- regular_message_item.iovec[0].iov_len =
- recovery_message_item->iovec[0].iov_len;
- }
- }
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "comparing if ring id is for this processors old ring seqno %d\n",
- mcast->seq);
- /*
- * Only add this message to the regular sort
- * queue if it was originated with the same ring
- * id as the previous ring
- */
- if (memcmp (&instance->my_old_ring_id, &mcast->ring_id,
- sizeof (struct memb_ring_id)) == 0) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "adding msg with seq no %d\n", mcast->seq, mcast->this_seqno);
- regular_message_item.iov_len = recovery_message_item->iov_len;
- res = sq_item_inuse (&instance->regular_sort_queue, mcast->seq);
- if (res == 0) {
- sq_item_add (&instance->regular_sort_queue,
- ®ular_message_item, mcast->seq);
- if (mcast->seq > instance->old_ring_state_high_seq_received) {
- instance->old_ring_state_high_seq_received = mcast->seq;
- }
- }
- } else {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "-not adding msg with seq no %d\n", mcast->seq);
- }
- }
- }
- /*
- * Change states in the state machine of the membership algorithm
- */
- static void memb_state_operational_enter (struct totemsrp_instance *instance)
- {
- struct in_addr joined_list[PROCESSOR_COUNT_MAX];
- int joined_list_entries = 0;
- struct in_addr left_list[PROCESSOR_COUNT_MAX];
- int left_list_entries = 0;
- int aru_save;
- old_ring_state_reset (instance);
- ring_reset (instance);
- deliver_messages_from_recovery_to_regular (instance);
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "Delivering to app %d to %d\n",
- instance->my_high_delivered + 1, instance->old_ring_state_high_seq_received);
- aru_save = instance->my_aru;
- instance->my_aru = instance->old_ring_state_aru;
- messages_deliver_to_app (instance, 0, instance->old_ring_state_high_seq_received);
- /*
- * Calculate joined and left list
- */
- memb_set_subtract (left_list, &left_list_entries,
- instance->my_memb_list, instance->my_memb_entries,
- instance->my_trans_memb_list, instance->my_trans_memb_entries);
- memb_set_subtract (joined_list, &joined_list_entries,
- instance->my_new_memb_list, instance->my_new_memb_entries,
- instance->my_trans_memb_list, instance->my_trans_memb_entries);
- /*
- * Deliver transitional configuration to application
- */
- instance->totemsrp_confchg_fn (TOTEM_CONFIGURATION_TRANSITIONAL,
- instance->my_trans_memb_list, instance->my_trans_memb_entries,
- left_list, left_list_entries,
- 0, 0, &instance->my_ring_id);
-
- // TODO we need to filter to ensure we only deliver those
- // messages which are part of instance->my_deliver_memb
- messages_deliver_to_app (instance, 1, instance->old_ring_state_high_seq_received);
- instance->my_aru = aru_save;
- /*
- * Deliver regular configuration to application
- */
- instance->totemsrp_confchg_fn (TOTEM_CONFIGURATION_REGULAR,
- instance->my_new_memb_list, instance->my_new_memb_entries,
- 0, 0,
- joined_list, joined_list_entries, &instance->my_ring_id);
- /*
- * Install new membership
- */
- instance->my_memb_entries = instance->my_new_memb_entries;
- memcpy (instance->my_memb_list, instance->my_new_memb_list,
- sizeof (struct in_addr) * instance->my_memb_entries);
- instance->last_released = 0;
- instance->my_set_retrans_flg = 0;
- /*
- * The recovery sort queue now becomes the regular
- * sort queue. It is necessary to copy the state
- * into the regular sort queue.
- */
- sq_copy (&instance->regular_sort_queue, &instance->recovery_sort_queue);
- instance->my_last_aru = 0;
- instance->my_proc_list_entries = instance->my_new_memb_entries;
- memcpy (instance->my_proc_list, instance->my_new_memb_list,
- sizeof (struct in_addr) * instance->my_memb_entries);
- instance->my_failed_list_entries = 0;
- instance->my_high_delivered = instance->my_aru;
- // TODO the recovery messages are leaked
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "entering OPERATIONAL state.\n");
- instance->memb_state = MEMB_STATE_OPERATIONAL;
- return;
- }
- static void memb_state_gather_enter (struct totemsrp_instance *instance)
- {
- memb_set_merge (&instance->my_id.sin_addr, 1,
- instance->my_proc_list, &instance->my_proc_list_entries);
- memb_join_message_send (instance);
- /*
- * Restart the join timeout
- */
- poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
- poll_timer_add (*instance->totemsrp_poll_handle,
- instance->timeout_state_gather_join,
- (void *)instance,
- memb_timer_function_state_gather,
- &instance->memb_timer_state_gather_join_timeout);
- /*
- * Restart the consensus timeout
- */
- poll_timer_delete (*instance->totemsrp_poll_handle,
- instance->memb_timer_state_gather_consensus_timeout);
- poll_timer_add (*instance->totemsrp_poll_handle,
- instance->timeout_state_gather_consensus,
- (void *)instance,
- memb_timer_function_gather_consensus_timeout,
- &instance->memb_timer_state_gather_consensus_timeout);
- /*
- * Cancel the token loss and token retransmission timeouts
- */
- cancel_token_retransmit_timeout (instance); // REVIEWED
- cancel_token_timeout (instance); // REVIEWED
- cancel_merge_detect_timeout (instance);
- memb_consensus_reset (instance);
- memb_consensus_set (instance, &instance->my_id.sin_addr);
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "entering GATHER state.\n");
- instance->memb_state = MEMB_STATE_GATHER;
- return;
- }
- static void timer_function_token_retransmit_timeout (void *data);
- static void memb_state_commit_enter (
- struct totemsrp_instance *instance,
- struct memb_commit_token *commit_token)
- {
- ring_save (instance);
- old_ring_state_save (instance);
- memb_state_commit_token_update (instance, commit_token);
- memb_state_commit_token_send (instance, commit_token);
- memb_ring_id_store (instance, commit_token);
- poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_join_timeout);
- instance->memb_timer_state_gather_join_timeout = 0;
- poll_timer_delete (*instance->totemsrp_poll_handle, instance->memb_timer_state_gather_consensus_timeout);
- instance->memb_timer_state_gather_consensus_timeout = 0;
- reset_token_timeout (instance); // REVIEWED
- reset_token_retransmit_timeout (instance); // REVIEWED
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "entering COMMIT state.\n");
- instance->memb_state = MEMB_STATE_COMMIT;
- return;
- }
- static void memb_state_recovery_enter (
- struct totemsrp_instance *instance,
- struct memb_commit_token *commit_token)
- {
- int i;
- #ifdef COMPILE_OUT
- int local_received_flg = 1;
- #endif
- unsigned int low_ring_aru;
- unsigned int messages_originated = 0;
- instance->my_high_ring_delivered = 0;
- sq_reinit (&instance->recovery_sort_queue, 0);
- queue_reinit (&instance->retrans_message_queue);
- low_ring_aru = instance->old_ring_state_high_seq_received;
- memb_state_commit_token_send (instance, commit_token);
- instance->my_token_seq = -1;
- /*
- * Build regular configuration
- */
- instance->my_new_memb_entries = commit_token->addr_entries;
- memcpy (instance->my_new_memb_list, commit_token->addr,
- sizeof (struct in_addr) * instance->my_new_memb_entries);
- /*
- * Build transitional configuration
- */
- memb_set_and (instance->my_new_memb_list, instance->my_new_memb_entries,
- instance->my_memb_list, instance->my_memb_entries,
- instance->my_trans_memb_list, &instance->my_trans_memb_entries);
- for (i = 0; i < instance->my_new_memb_entries; i++) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "position [%d] member %s:\n", i, inet_ntoa (commit_token->addr[i]));
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "previous ring seq %lld rep %s\n",
- commit_token->memb_list[i].ring_id.seq,
- inet_ntoa (commit_token->memb_list[i].ring_id.rep));
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "aru %d high delivered %d received flag %d\n",
- commit_token->memb_list[i].aru,
- commit_token->memb_list[i].high_delivered,
- commit_token->memb_list[i].received_flg);
- assert (commit_token->memb_list[i].ring_id.rep.s_addr);
- }
- /*
- * Determine if any received flag is false
- */
- #ifdef COMPILE_OUT
- for (i = 0; i < commit_token->addr_entries; i++) {
- if (memb_set_subset (&instance->my_new_memb_list[i], 1,
- instance->my_trans_memb_list, instance->my_trans_memb_entries) &&
- commit_token->memb_list[i].received_flg == 0) {
- #endif
- instance->my_deliver_memb_entries = instance->my_trans_memb_entries;
- memcpy (instance->my_deliver_memb_list, instance->my_trans_memb_list,
- sizeof (struct in_addr) * instance->my_trans_memb_entries);
- #ifdef COMPILE_OUT
- local_received_flg = 0;
- break;
- }
- }
- #endif
- // if (local_received_flg == 0) {
- /*
- * Calculate my_low_ring_aru, instance->my_high_ring_delivered for the transitional membership
- */
- for (i = 0; i < commit_token->addr_entries; i++) {
- printf ("comparing %d old ring %s.%lld with commit ring %s.%lld.\n", i,
- inet_ntoa (instance->my_old_ring_id.rep), instance->my_old_ring_id.seq,
- inet_ntoa (commit_token->memb_list[i].ring_id.rep),
- commit_token->memb_list[i].ring_id.seq);
- printf ("memb set subset %d\n",
- memb_set_subset (&instance->my_new_memb_list[i], 1, instance->my_deliver_memb_list, instance->my_deliver_memb_entries));
- if (memb_set_subset (&instance->my_new_memb_list[i], 1,
- instance->my_deliver_memb_list,
- instance->my_deliver_memb_entries) &&
- memcmp (&instance->my_old_ring_id,
- &commit_token->memb_list[i].ring_id,
- sizeof (struct memb_ring_id)) == 0) {
-
- if (low_ring_aru == 0 ||
- low_ring_aru > commit_token->memb_list[i].aru) {
- low_ring_aru = commit_token->memb_list[i].aru;
- }
- if (instance->my_high_ring_delivered < commit_token->memb_list[i].high_delivered) {
- instance->my_high_ring_delivered = commit_token->memb_list[i].high_delivered;
- }
- }
- }
- assert (low_ring_aru != 0xffffffff);
- /*
- * Cpy all old ring messages to instance->retrans_message_queue
- */
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "copying all old ring messages from %d-%d.\n",
- low_ring_aru + 1, instance->old_ring_state_high_seq_received);
-
- for (i = low_ring_aru + 1; i <= instance->old_ring_state_high_seq_received; i++) {
- struct sort_queue_item *sort_queue_item;
- struct message_item message_item;
- void *ptr;
- int res;
- res = sq_item_get (&instance->regular_sort_queue, i, &ptr);
- if (res != 0) {
- printf ("-not copying %d-\n", i);
- continue;
- }
- printf ("copying %d\n", i);
- sort_queue_item = ptr;
- assert (sort_queue_item->iov_len > 0);
- assert (sort_queue_item->iov_len <= MAXIOVS);
- messages_originated++;
- memset (&message_item, 0, sizeof (struct message_item));
- // TODO LEAK
- message_item.mcast = malloc (sizeof (struct mcast));
- assert (message_item.mcast);
- memcpy (message_item.mcast, sort_queue_item->iovec[0].iov_base,
- sizeof (struct mcast));
- memcpy (&message_item.mcast->ring_id, &instance->my_ring_id,
- sizeof (struct memb_ring_id));
- message_item.mcast->header.encapsulated = 1;
- message_item.iov_len = sort_queue_item->iov_len;
- memcpy (&message_item.iovec, &sort_queue_item->iovec, sizeof (struct iovec) *
- sort_queue_item->iov_len);
- queue_item_add (&instance->retrans_message_queue, &message_item);
- }
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Originated %d messages in RECOVERY.\n", messages_originated);
- // }
- instance->my_aru = 0;
- instance->my_aru_count = 0;
- instance->my_seq_unchanged = 0;
- instance->my_high_seq_received = 0;
- instance->my_install_seq = 0;
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "entering RECOVERY state.\n");
- reset_token_timeout (instance); // REVIEWED
- reset_token_retransmit_timeout (instance); // REVIEWED
- instance->memb_state = MEMB_STATE_RECOVERY;
- return;
- }
- static void encrypt_and_sign_worker (
- struct totemsrp_instance *instance,
- char *buf,
- int *buf_len,
- struct iovec *iovec,
- int iov_len,
- prng_state *prng_state_in)
- {
- int i;
- char *addr;
- char keys[48];
- struct security_header *header;
- char *hmac_key = &keys[32];
- char *cipher_key = &keys[16];
- char *initial_vector = &keys[0];
- unsigned long len;
- int outlen = 0;
- hmac_state hmac_state;
- prng_state keygen_prng_state;
- prng_state stream_prng_state;
- header = (struct security_header *)buf;
- addr = buf + sizeof (struct security_header);
- memset (keys, 0, sizeof (keys));
- memset (header->salt, 0, sizeof (header->salt));
- #if (defined(ENCRYPTION) || defined(AUTHENITCATION))
- /*
- * Generate MAC, CIPHER, IV keys from private key
- */
- sober128_read (header->salt, sizeof (header->salt), prng_state_in);
- sober128_start (&keygen_prng_state);
- sober128_add_entropy (instance->totemsrp_private_key,
- instance->totemsrp_private_key_len,
- &keygen_prng_state);
- sober128_add_entropy (header->salt, sizeof (header->salt),
- &keygen_prng_state);
- sober128_read (keys, sizeof (keys), &keygen_prng_state);
- #endif
- #ifdef ENCRYPTION
- /*
- * Setup stream cipher
- */
- sober128_start (&stream_prng_state);
- sober128_add_entropy (cipher_key, 16, &stream_prng_state);
- sober128_add_entropy (initial_vector, 16, &stream_prng_state);
- #endif
- /*
- * Copy header of message, then remainder of message, then encrypt it
- */
- memcpy (addr, iovec[0].iov_base + sizeof (struct security_header),
- iovec[0].iov_len - sizeof (struct security_header));
- addr += iovec[0].iov_len - sizeof (struct security_header);
- outlen += iovec[0].iov_len;
- for (i = 1; i < iov_len; i++) {
- memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
- addr += iovec[i].iov_len;
- outlen += iovec[i].iov_len;
- }
- /*
- * Encrypt message by XORing stream cipher data
- */
- #ifdef ENCRYPTION
- sober128_read (buf + sizeof (struct security_header),
- outlen - sizeof (struct security_header),
- &stream_prng_state);
- #endif
- #ifdef AUTHENTICATION
- memset (&hmac_state, 0, sizeof (hmac_state));
- /*
- * Sign the contents of the message with the hmac key and store signature in message
- */
- hmac_init (&hmac_state, DIGEST_SHA1, hmac_key, 16);
- hmac_process (&hmac_state,
- buf + HMAC_HASH_SIZE,
- outlen - HMAC_HASH_SIZE);
- len = hash_descriptor[DIGEST_SHA1]->hashsize;
- hmac_done (&hmac_state, header->hash_digest, &len);
- #endif
- *buf_len = outlen;
- }
- static void encrypt_and_sign (
- struct totemsrp_instance *instance,
- struct iovec *iovec,
- int iov_len)
- {
- char *addr = instance->iov_encrypted.iov_base +
- sizeof (struct security_header);
- int i;
- char keys[48];
- struct security_header *header = instance->iov_encrypted.iov_base;
- prng_state keygen_prng_state;
- prng_state stream_prng_state;
- char *hmac_key = &keys[32];
- char *cipher_key = &keys[16];
- char *initial_vector = &keys[0];
- unsigned long len;
- instance->iov_encrypted.iov_len = 0;
- memset (keys, 0, sizeof (keys));
- memset (header->salt, 0, sizeof (header->salt));
- #if (defined(ENCRYPTION) || defined(AUTHENITCATION))
- /*
- * Generate MAC, CIPHER, IV keys from private key
- */
- sober128_read (header->salt, sizeof (header->salt),
- &instance->totemsrp_prng_state);
- sober128_start (&keygen_prng_state);
- sober128_add_entropy (instance->totemsrp_private_key,
- instance->totemsrp_private_key_len, &keygen_prng_state);
- sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
- sober128_read (keys, sizeof (keys), &keygen_prng_state);
- #endif
- #ifdef ENCRYPTION
- /*
- * Setup stream cipher
- */
- sober128_start (&stream_prng_state);
- sober128_add_entropy (cipher_key, 16, &stream_prng_state);
- sober128_add_entropy (initial_vector, 16, &stream_prng_state);
- #endif
- #ifdef CODE_COVERAGE_COMPILE_OUT
- if (log_digest) {
- printf ("new encryption\n");
- print_digest ("salt", header->salt);
- print_digest ("initial_vector", initial_vector);
- print_digest ("cipher_key", cipher_key);
- print_digest ("hmac_key", hmac_key);
- }
- #endif
- /*
- * Copy header of message, then remainder of message, then encrypt it
- */
- memcpy (addr, iovec[0].iov_base + sizeof (struct security_header),
- iovec[0].iov_len - sizeof (struct security_header));
- addr += iovec[0].iov_len - sizeof (struct security_header);
- instance->iov_encrypted.iov_len += iovec[0].iov_len;
- for (i = 1; i < iov_len; i++) {
- memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
- addr += iovec[i].iov_len;
- instance->iov_encrypted.iov_len += iovec[i].iov_len;
- }
- /*
- * Encrypt message by XORing stream cipher data
- */
- #ifdef ENCRYPTION
- sober128_read (instance->iov_encrypted.iov_base + sizeof (struct security_header),
- instance->iov_encrypted.iov_len - sizeof (struct security_header),
- &stream_prng_state);
- #endif
- #ifdef AUTHENTICATION
- memset (&instance->totemsrp_hmac_state, 0, sizeof (hmac_state));
- /*
- * Sign the contents of the message with the hmac key and store signature in message
- */
- hmac_init (&instance->totemsrp_hmac_state, DIGEST_SHA1, hmac_key, 16);
- hmac_process (&instance->totemsrp_hmac_state,
- instance->iov_encrypted.iov_base + HMAC_HASH_SIZE,
- instance->iov_encrypted.iov_len - HMAC_HASH_SIZE);
- len = hash_descriptor[DIGEST_SHA1]->hashsize;
- hmac_done (&instance->totemsrp_hmac_state, header->hash_digest, &len);
- #endif
- #ifdef COMPILE_OUT
- print_digest ("initial_vector", initial_vector);
- print_digest ("cipher_key", cipher_key);
- print_digest ("hmac_key", hmac_key);
- print_digest ("salt", header->salt);
- print_digest ("sent digest", header->hash_digest);
- #endif
- }
- /*
- * Only designed to work with a message with one iov
- */
- static int authenticate_and_decrypt (
- struct totemsrp_instance *instance,
- struct iovec *iov)
- {
- char keys[48];
- struct security_header *header = iov[0].iov_base;
- prng_state keygen_prng_state;
- prng_state stream_prng_state;
- char *hmac_key = &keys[32];
- char *cipher_key = &keys[16];
- char *initial_vector = &keys[0];
- char digest_comparison[HMAC_HASH_SIZE];
- unsigned long len;
- int res = 0;
- instance->iov_encrypted.iov_len = 0;
- #ifdef COMPILE_OUT
- printf ("Decryption message\n");
- print_msg (header, iov[0].iov_len);
- #endif
- #if (defined(ENCRYPTION) || defined(AUTHENITCATION))
- /*
- * Generate MAC, CIPHER, IV keys from private key
- */
- memset (keys, 0, sizeof (keys));
- sober128_start (&keygen_prng_state);
- sober128_add_entropy (instance->totemsrp_private_key,
- instance->totemsrp_private_key_len, &keygen_prng_state);
- sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
- sober128_read (keys, sizeof (keys), &keygen_prng_state);
- #endif
- #ifdef ENCRYPTION
- /*
- * Setup stream cipher
- */
- sober128_start (&stream_prng_state);
- sober128_add_entropy (cipher_key, 16, &stream_prng_state);
- sober128_add_entropy (initial_vector, 16, &stream_prng_state);
- #endif
- #ifdef CODE_COVERAGE_COMPILE_OUT
- if (log_digest) {
- printf ("New decryption\n");
- print_digest ("salt", header->salt);
- print_digest ("initial_vector", initial_vector);
- print_digest ("cipher_key", cipher_key);
- print_digest ("hmac_key", hmac_key);
- }
- #endif
- #ifdef AUTHENTICATION
- /*
- * Authenticate contents of message
- */
- hmac_init (&instance->totemsrp_hmac_state, DIGEST_SHA1, hmac_key, 16);
- hmac_process (&instance->totemsrp_hmac_state,
- iov->iov_base + HMAC_HASH_SIZE,
- iov->iov_len - HMAC_HASH_SIZE);
- len = hash_descriptor[DIGEST_SHA1]->hashsize;
- assert (HMAC_HASH_SIZE >= len);
- hmac_done (&instance->totemsrp_hmac_state, digest_comparison, &len);
- #ifdef PRINTDIGESTS
- print_digest ("received digest", header->hash_digest);
- print_digest ("calculated digest", digest_comparison);
- #endif
- if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
- #ifdef CODE_COVERAGE_COMPILE_OUT
- print_digest ("initial_vector", initial_vector);
- print_digest ("cipher_key", cipher_key);
- print_digest ("hmac_key", hmac_key);
- print_digest ("salt", header->salt);
- print_digest ("sent digest", header->hash_digest);
- print_digest ("calculated digest", digest_comparison);
- printf ("received message size %d\n", iov->iov_len);
- #endif
- instance->totemsrp_log_printf (instance->totemsrp_log_level_security, "Received message has invalid digest... ignoring.\n");
- res = -1;
- return (-1);
- }
- #endif /* AUTHENTICATION */
-
- /*
- * Decrypt the contents of the message with the cipher key
- */
- #ifdef ENCRYPTION
- sober128_read (iov->iov_base + sizeof (struct security_header),
- iov->iov_len - sizeof (struct security_header),
- &stream_prng_state);
- #endif
- return (res);
- return (0);
- }
- int totemsrp_new_msg_signal (totemsrp_handle handle)
- {
- struct totemsrp_instance *instance;
- SaErrorT error;
- error = saHandleInstanceGet (&totemsrp_instance_database, handle,
- (void *)&instance);
- if (error != SA_OK) {
- goto error_exit;
- }
- token_hold_cancel_send (instance);
- saHandleInstancePut (&totemsrp_instance_database, handle);
- return (0);
- error_exit:
- return (-1);
- }
- int totemsrp_mcast (
- totemsrp_handle handle,
- struct iovec *iovec,
- int iov_len,
- int guarantee)
- {
- int i;
- int j;
- struct message_item message_item;
- struct totemsrp_instance *instance;
- SaErrorT error;
- error = saHandleInstanceGet (&totemsrp_instance_database, handle,
- (void *)&instance);
- if (error != SA_OK) {
- goto error_exit;
- }
-
- if (queue_is_full (&instance->new_message_queue)) {
- return (-1);
- }
- for (j = 0, i = 0; i < iov_len; i++) {
- j+= iovec[i].iov_len;
- }
- memset (&message_item, 0, sizeof (struct message_item));
- /*
- * Allocate pending item
- */
- // TODO LEAK
- message_item.mcast = malloc (sizeof (struct mcast));
- if (message_item.mcast == 0) {
- goto error_mcast;
- }
- /*
- * Set mcast header
- */
- message_item.mcast->header.type = MESSAGE_TYPE_MCAST;
- message_item.mcast->header.endian_detector = ENDIAN_LOCAL;
- message_item.mcast->header.encapsulated = 2;
- message_item.mcast->guarantee = guarantee;
- message_item.mcast->source.s_addr = instance->my_id.sin_addr.s_addr;
- for (i = 0; i < iov_len; i++) {
- // TODO LEAK
- message_item.iovec[i].iov_base = malloc (iovec[i].iov_len);
- if (message_item.iovec[i].iov_base == 0) {
- goto error_iovec;
- }
- memcpy (message_item.iovec[i].iov_base, iovec[i].iov_base,
- iovec[i].iov_len);
- message_item.iovec[i].iov_len = iovec[i].iov_len;
- }
- message_item.iov_len = iov_len;
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug, "mcasted message added to pending queue\n");
- queue_item_add (&instance->new_message_queue, &message_item);
- saHandleInstancePut (&totemsrp_instance_database, handle);
- return (0);
- error_iovec:
- saHandleInstancePut (&totemsrp_instance_database, handle);
- for (j = 0; j < i; j++) {
- free (message_item.iovec[j].iov_base);
- }
- return (-1);
- error_mcast:
- saHandleInstancePut (&totemsrp_instance_database, handle);
- error_exit:
- return (0);
- }
- /*
- * Determine if there is room to queue a new message
- */
- int totemsrp_avail (totemsrp_handle handle)
- {
- int avail;
- struct totemsrp_instance *instance;
- SaErrorT error;
- error = saHandleInstanceGet (&totemsrp_instance_database, handle,
- (void *)&instance);
- if (error != SA_OK) {
- goto error_exit;
- }
- queue_avail (&instance->new_message_queue, &avail);
- saHandleInstancePut (&totemsrp_instance_database, handle);
- return (avail);
- error_exit:
- return (0);
- }
- static int netif_determine (
- struct totemsrp_instance *instance,
- struct sockaddr_in *bindnet,
- struct sockaddr_in *bound_to,
- int *interface_up)
- {
- struct sockaddr_in *sockaddr_in;
- int id_fd;
- struct ifconf ifc;
- int numreqs = 0;
- int res;
- int i;
- in_addr_t mask_addr;
- *interface_up = 0;
- /*
- * Generate list of local interfaces in ifc.ifc_req structure
- */
- id_fd = socket (AF_INET, SOCK_STREAM, 0);
- ifc.ifc_buf = 0;
- do {
- numreqs += 32;
- ifc.ifc_len = sizeof (struct ifreq) * numreqs;
- ifc.ifc_buf = (void *)realloc(ifc.ifc_buf, ifc.ifc_len);
- res = ioctl (id_fd, SIOCGIFCONF, &ifc);
- if (res < 0) {
- close (id_fd);
- return -1;
- }
- } while (ifc.ifc_len == sizeof (struct ifreq) * numreqs);
- res = -1;
- /*
- * Find interface address to bind to
- */
- for (i = 0; i < ifc.ifc_len / sizeof (struct ifreq); i++) {
- sockaddr_in = (struct sockaddr_in *)&ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_addr;
- mask_addr = inet_addr ("255.255.255.0");
- if ((sockaddr_in->sin_family == AF_INET) &&
- (sockaddr_in->sin_addr.s_addr & mask_addr) ==
- (bindnet->sin_addr.s_addr & mask_addr)) {
- bound_to->sin_addr.s_addr = sockaddr_in->sin_addr.s_addr;
- res = i;
- if (ioctl(id_fd, SIOCGIFFLAGS, &ifc.ifc_ifcu.ifcu_req[i]) < 0) {
- printf ("couldn't do ioctl\n");
- }
- *interface_up = ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_flags & IFF_UP;
- break; /* for */
- }
- }
- free (ifc.ifc_buf);
- close (id_fd);
-
- return (res);
- }
- static int loopback_determine (struct sockaddr_in *bound_to)
- {
- bound_to->sin_addr.s_addr = LOCALHOST_IP;
- if (&bound_to->sin_addr.s_addr == 0) {
- return -1;
- }
- return 1;
- }
- /*
- * If the interface is up, the sockets for totem are built. If the interface is down
- * this function is requeued in the timer list to retry building the sockets later.
- */
- static void timer_function_netif_check_timeout (
- void *data)
- {
- struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
- int res;
- int interface_no;
- int interface_up;
- /*
- * Build sockets for every interface
- */
- for (interface_no = 0; interface_no < instance->totemsrp_interface_count; interface_no++) {
- netif_determine (instance,
- &instance->totemsrp_interfaces[interface_no].bindnet,
- &instance->totemsrp_interfaces[interface_no].boundto,
- &interface_up);
- if (((instance->netif_bind_state & BIND_STATE_LOOPBACK) && (!interface_up))
- || ((instance->netif_bind_state & BIND_STATE_REGULAR) && (interface_up))) {
- break;
- }
- instance->totemsrp_log_printf(instance->totemsrp_log_level_debug,"network interface UP %s\n",
- inet_ntoa (instance->totemsrp_interfaces[interface_no].boundto.sin_addr));
-
- if (instance->totemsrp_sockets[interface_no].mcast > 0) {
- close (instance->totemsrp_sockets[interface_no].mcast);
- poll_dispatch_delete (*instance->totemsrp_poll_handle,
- instance->totemsrp_sockets[interface_no].mcast);
- }
- if (instance->totemsrp_sockets[interface_no].token > 0) {
- close (instance->totemsrp_sockets[interface_no].token);
- poll_dispatch_delete (*instance->totemsrp_poll_handle,
- instance->totemsrp_sockets[interface_no].token);
- }
- if (!interface_up) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,"Interface is down binding to LOOPBACK addr.\n");
- instance->netif_bind_state = BIND_STATE_LOOPBACK;
- res = totemsrp_build_sockets_loopback(instance,
- &instance->sockaddr_in_mcast,
- &instance->totemsrp_interfaces[interface_no].bindnet,
- &instance->totemsrp_sockets[interface_no],
- &instance->totemsrp_interfaces[interface_no].boundto);
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,"network interface LOCAL %s\n",
- inet_ntoa (instance->totemsrp_interfaces[interface_no].boundto.sin_addr));
- poll_dispatch_add (*instance->totemsrp_poll_handle, instance->totemsrp_sockets[interface_no].token,
- POLLIN, instance, recv_handler, UINT_MAX);
- continue;
- }
- instance->netif_bind_state = BIND_STATE_REGULAR;
- /*
- * Create and bind the multicast and unicast sockets
- */
- res = totemsrp_build_sockets (instance,
- &instance->sockaddr_in_mcast,
- &instance->totemsrp_interfaces[interface_no].bindnet,
- &instance->totemsrp_sockets[interface_no],
- &instance->totemsrp_interfaces[interface_no].boundto,
- &interface_up);
- poll_dispatch_add (*instance->totemsrp_poll_handle, instance->totemsrp_sockets[interface_no].mcast,
- POLLIN, instance, recv_handler, UINT_MAX);
- poll_dispatch_add (*instance->totemsrp_poll_handle, instance->totemsrp_sockets[interface_no].token,
- POLLIN, instance, recv_handler, UINT_MAX);
- }
- memcpy (&instance->my_id, &instance->totemsrp_interfaces->boundto, sizeof (struct sockaddr_in));
- /*
- * This stuff depends on totemsrp_build_sockets
- */
- if (instance->firstrun == 0) {
- instance->firstrun += 1;
- memcpy (&instance->my_memb_list[0], &instance->totemsrp_interfaces->boundto,
- sizeof (struct sockaddr_in));
- memb_ring_id_create_or_load (instance, &instance->my_ring_id);
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Created or loaded sequence id %lld.%s for this ring.\n",
- instance->my_ring_id.seq, inet_ntoa (instance->my_ring_id.rep));
- }
- if (interface_up) {
- if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- " The network interface is now up.\n");
- instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
- memb_state_gather_enter (instance);
- }
- /*
- * If this is a single processor, detect downs which may not
- * be detected by token loss when the interface is downed
- */
- if (instance->my_memb_entries <= 1) {
- poll_timer_add (*instance->totemsrp_poll_handle,
- instance->timeout_downcheck,
- (void *)instance,
- timer_function_netif_check_timeout,
- &instance->timer_netif_check_timeout);
- }
- } else {
- if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "The network interface is down.\n");
- memb_state_gather_enter (instance);
- }
- instance->netif_state_report = NETIF_STATE_REPORT_UP;
- /*
- * Add a timer to retry building interfaces and request memb_gather_enter
- */
- cancel_token_timeout (instance);
- poll_timer_add (*instance->totemsrp_poll_handle,
- instance->timeout_downcheck,
- (void *)instance,
- timer_function_netif_check_timeout,
- &instance->timer_netif_check_timeout);
- }
- }
- /*
- * Check if an interface is down and reconfigure
- * totemsrp waiting for it to come back up
- */
- static void netif_down_check (struct totemsrp_instance *instance)
- {
- timer_function_netif_check_timeout (instance);
- }
- static int totemsrp_build_sockets_loopback (
- struct totemsrp_instance *instance,
- struct sockaddr_in *sockaddr_mcast,
- struct sockaddr_in *sockaddr_bindnet,
- struct totemsrp_socket *sockets,
- struct sockaddr_in *bound_to)
- {
- struct ip_mreq mreq;
- struct sockaddr_in sockaddr_in;
- int res;
- memset (&mreq, 0, sizeof (struct ip_mreq));
- /*
- * Determine the ip address bound to and the interface name
- */
- res = loopback_determine (bound_to);
- if (res == -1) {
- return (-1);
- }
- /* TODO this should be somewhere else */
- instance->memb_local_sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
- instance->memb_local_sockaddr_in.sin_family = AF_INET;
- instance->memb_local_sockaddr_in.sin_port = sockaddr_mcast->sin_port;
- sockaddr_in.sin_family = AF_INET;
- sockaddr_in.sin_port = sockaddr_mcast->sin_port;
- /*
- * Setup unicast socket
- */
- sockets->token = socket (AF_INET, SOCK_DGRAM, 0);
- if (sockets->token == -1) {
- perror ("socket2");
- return (-1);
- }
- /*
- * Bind to unicast socket used for token send/receives
- * This has the side effect of binding to the correct interface
- */
- sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
- res = bind (sockets->token, (struct sockaddr *)&sockaddr_in,
- sizeof (struct sockaddr_in));
- if (res == -1) {
- perror ("bind2 failed");
- return (-1);
- }
- memcpy(&instance->sockaddr_in_mcast, &sockaddr_in,
- sizeof(struct sockaddr_in));
- sockets->mcast = sockets->token;
- return (0);
- }
- static int totemsrp_build_sockets (
- struct totemsrp_instance *instance,
- struct sockaddr_in *sockaddr_mcast,
- struct sockaddr_in *sockaddr_bindnet,
- struct totemsrp_socket *sockets,
- struct sockaddr_in *bound_to,
- int *interface_up)
- {
- struct ip_mreq mreq;
- struct sockaddr_in sockaddr_in;
- char flag;
- int res;
-
- memset (&mreq, 0, sizeof (struct ip_mreq));
- /*
- * Determine the ip address bound to and the interface name
- */
- res = netif_determine (instance,
- sockaddr_bindnet,
- bound_to,
- interface_up);
- if (res == -1) {
- return (-1);
- }
- /* TODO this should be somewhere else */
- instance->memb_local_sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
- instance->memb_local_sockaddr_in.sin_family = AF_INET;
- instance->memb_local_sockaddr_in.sin_port = sockaddr_mcast->sin_port;
- /*
- * Create multicast socket
- */
- sockets->mcast = socket (AF_INET, SOCK_DGRAM, 0);
- if (sockets->mcast == -1) {
- perror ("socket");
- return (-1);
- }
- if (setsockopt (sockets->mcast, SOL_IP, IP_MULTICAST_IF,
- &bound_to->sin_addr, sizeof (struct in_addr)) < 0) {
- 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));
- }
- /*
- * Bind to multicast socket used for multicast send/receives
- */
- sockaddr_in.sin_family = AF_INET;
- sockaddr_in.sin_addr.s_addr = sockaddr_mcast->sin_addr.s_addr;
- sockaddr_in.sin_port = sockaddr_mcast->sin_port;
- res = bind (sockets->mcast, (struct sockaddr *)&sockaddr_in,
- sizeof (struct sockaddr_in));
- if (res == -1) {
- perror ("bind failed");
- return (-1);
- }
- /*
- * Setup unicast socket
- */
- sockets->token = socket (AF_INET, SOCK_DGRAM, 0);
- if (sockets->token == -1) {
- perror ("socket2");
- return (-1);
- }
- /*
- * Bind to unicast socket used for token send/receives
- * This has the side effect of binding to the correct interface
- */
- sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
- res = bind (sockets->token, (struct sockaddr *)&sockaddr_in,
- sizeof (struct sockaddr_in));
- if (res == -1) {
- perror ("bind2 failed");
- return (-1);
- }
- #ifdef CONFIG_USE_BROADCAST
- /* This config option doesn't work */
- {
- int on = 1;
- setsockopt (sockets->mcast, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof (on));
- }
- #else
- /*
- * Join group membership on socket
- */
- mreq.imr_multiaddr.s_addr = sockaddr_mcast->sin_addr.s_addr;
- mreq.imr_interface.s_addr = bound_to->sin_addr.s_addr;
- res = setsockopt (sockets->mcast, IPPROTO_IP, IP_ADD_MEMBERSHIP,
- &mreq, sizeof (mreq));
- if (res == -1) {
- perror ("join multicast group failed");
- return (-1);
- }
- #endif
- /*
- * Turn on multicast loopback
- */
- flag = 1;
- res = setsockopt (sockets->mcast, IPPROTO_IP, IP_MULTICAST_LOOP,
- &flag, sizeof (flag));
- if (res == -1) {
- perror ("turn off loopback");
- return (-1);
- }
- return (0);
- }
-
- /*
- * Misc Management
- */
- static int in_addr_compare (const void *a, const void *b) {
- struct in_addr *in_addr_a = (struct in_addr *)a;
- struct in_addr *in_addr_b = (struct in_addr *)b;
- return (in_addr_a->s_addr > in_addr_b->s_addr);
- }
- /*
- * ORF Token Management
- */
- /*
- * Recast message to mcast group if it is available
- */
- static int orf_token_remcast (
- struct totemsrp_instance *instance,
- int seq)
- {
- struct msghdr msg_mcast;
- struct sort_queue_item *sort_queue_item;
- int res;
- struct mcast *mcast;
- void *ptr;
- struct sq *sort_queue;
- if (instance->memb_state == MEMB_STATE_RECOVERY) {
- sort_queue = &instance->recovery_sort_queue;
- } else {
- sort_queue = &instance->regular_sort_queue;
- }
- /*
- * Get RTR item at seq, if not available, return
- */
- res = sq_item_get (sort_queue, seq, &ptr);
- if (res != 0) {
- return -1;
- }
- sort_queue_item = ptr;
- mcast = (struct mcast *)sort_queue_item->iovec[0].iov_base;
- encrypt_and_sign (instance, sort_queue_item->iovec, sort_queue_item->iov_len);
- /*
- * Build multicast message
- */
- msg_mcast.msg_name = (caddr_t)&instance->sockaddr_in_mcast;
- msg_mcast.msg_namelen = sizeof (struct sockaddr_in);
- msg_mcast.msg_iov = &instance->iov_encrypted;
- msg_mcast.msg_iovlen = 1;
- msg_mcast.msg_control = 0;
- msg_mcast.msg_controllen = 0;
- msg_mcast.msg_flags = 0;
- /*
- * Multicast message
- */
- res = sendmsg (instance->totemsrp_sockets[0].mcast, &msg_mcast, MSG_NOSIGNAL | MSG_DONTWAIT);
-
- if (res == -1) {
- return (-1);
- }
- instance->stats_sent += res;
- return (0);
- }
- /*
- * Free all freeable messages from ring
- */
- static int messages_free (
- struct totemsrp_instance *instance,
- int token_aru)
- {
- struct sort_queue_item *regular_message;
- int i, j;
- int res;
- int log_release = 0;
- int release_to;
- release_to = token_aru;
- if (release_to > instance->my_last_aru) {
- release_to = instance->my_last_aru;
- }
- if (release_to > instance->my_high_delivered) {
- release_to = instance->my_high_delivered;
- }
- /*
- * Release retransmit list items if group aru indicates they are transmitted
- */
- for (i = instance->last_released; i <= release_to; i++) {
- void *ptr;
- res = sq_item_get (&instance->regular_sort_queue, i, &ptr);
- if (res == 0) {
- regular_message = ptr;
- for (j = 0; j < regular_message->iov_len; j++) {
- free (regular_message->iovec[j].iov_base);
- }
- }
- sq_items_release (&instance->regular_sort_queue, i);
- instance->last_released = i + 1;
- log_release = 1;
- }
- if (log_release) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "releasing messages up to and including %d\n", release_to);
- }
- return (0);
- }
- static void update_aru (
- struct totemsrp_instance *instance)
- {
- int i;
- int res;
- struct sq *sort_queue;
- if (instance->memb_state == MEMB_STATE_RECOVERY) {
- sort_queue = &instance->recovery_sort_queue;
- } else {
- sort_queue = &instance->regular_sort_queue;
- }
- for (i = instance->my_aru + 1; i <= instance->my_high_seq_received; i++) {
- void *ptr;
- res = sq_item_get (sort_queue, i, &ptr);
- /*
- * If hole, stop assembly
- */
- if (res != 0) {
- break;
- }
- instance->my_aru = i;
- }
- // instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- // "setting received flag to FALSE %d %d\n",
- // instance->my_aru, instance->my_high_seq_received);
- instance->my_received_flg = 0;
- if (instance->my_aru == instance->my_high_seq_received) {
- // instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- // "setting received flag to TRUE %d %d\n",
- // instance->my_aru, instance->my_high_seq_received);
- instance->my_received_flg = 1;
- }
- }
- static void orf_token_mcast_worker_fn (void *thread_state, void *work_item)
- {
- struct mcast_worker_fn_work_item *mcast_worker_fn_work_item =
- (struct mcast_worker_fn_work_item *)work_item;
- struct sort_queue_item *sort_queue_item = mcast_worker_fn_work_item->sort_queue_item;
- struct orf_token_mcast_thread_state *orf_token_mcast_thread_state =
- (struct orf_token_mcast_thread_state *)thread_state;
- struct totemsrp_instance *instance = mcast_worker_fn_work_item->instance;
- struct msghdr msg_mcast;
- int res = 0;
- int buf_len;
- struct iovec iov;
- /*
- * Encrypt and digest the message
- */
- encrypt_and_sign_worker (
- instance,
- orf_token_mcast_thread_state->iobuf, &buf_len,
- sort_queue_item->iovec, sort_queue_item->iov_len,
- &orf_token_mcast_thread_state->prng_state);
- iov.iov_base = orf_token_mcast_thread_state->iobuf;
- iov.iov_len = buf_len;
- /*
- * Build multicast message
- */
- msg_mcast.msg_name = &instance->sockaddr_in_mcast;
- msg_mcast.msg_namelen = sizeof (struct sockaddr_in);
- msg_mcast.msg_iov = &iov;
- msg_mcast.msg_iovlen = 1;
- msg_mcast.msg_control = 0;
- msg_mcast.msg_controllen = 0;
- msg_mcast.msg_flags = 0;
- /*
- * Multicast message
- * An error here is recovered by the multicast algorithm
- */
- res = sendmsg (instance->totemsrp_sockets[0].mcast, &msg_mcast, MSG_NOSIGNAL | MSG_DONTWAIT);
- instance->iov_encrypted.iov_len = PACKET_SIZE_MAX;
- if (res > 0) {
- instance->stats_sent += res;
- }
- }
- /*
- * Multicasts pending messages onto the ring (requires orf_token possession)
- */
- static int orf_token_mcast (
- struct totemsrp_instance *instance,
- struct orf_token *token,
- int fcc_mcasts_allowed,
- struct sockaddr_in *system_from)
- {
- struct mcast_worker_fn_work_item work_item;
- struct message_item *message_item = 0;
- struct queue *mcast_queue;
- struct sq *sort_queue;
- struct sort_queue_item sort_queue_item;
- struct sort_queue_item *sort_queue_item_ptr;
- struct mcast *mcast;
- if (instance->memb_state == MEMB_STATE_RECOVERY) {
- mcast_queue = &instance->retrans_message_queue;
- sort_queue = &instance->recovery_sort_queue;
- reset_token_retransmit_timeout (instance); // REVIEWED
- } else {
- mcast_queue = &instance->new_message_queue;
- sort_queue = &instance->regular_sort_queue;
- }
- for (instance->fcc_mcast_current = 0; instance->fcc_mcast_current < fcc_mcasts_allowed; instance->fcc_mcast_current++) {
- if (queue_is_empty (mcast_queue)) {
- break;
- }
- message_item = (struct message_item *)queue_item_get (mcast_queue);
- /* preincrement required by algo */
- if (instance->old_ring_state_saved &&
- (instance->memb_state == MEMB_STATE_GATHER ||
- instance->memb_state == MEMB_STATE_COMMIT)) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "not multicasting at seqno is %d\n",
- token->seq);
- return (0);
- }
- message_item->mcast->seq = ++token->seq;
- message_item->mcast->this_seqno = instance->global_seqno++;
- /*
- * Build IO vector
- */
- memset (&sort_queue_item, 0, sizeof (struct sort_queue_item));
- sort_queue_item.iovec[0].iov_base = message_item->mcast;
- sort_queue_item.iovec[0].iov_len = sizeof (struct mcast);
-
- mcast = sort_queue_item.iovec[0].iov_base;
-
- memcpy (&sort_queue_item.iovec[1], message_item->iovec,
- message_item->iov_len * sizeof (struct iovec));
- memcpy (&mcast->ring_id, &instance->my_ring_id, sizeof (struct memb_ring_id));
- sort_queue_item.iov_len = message_item->iov_len + 1;
- assert (sort_queue_item.iov_len < 16);
- /*
- * Add message to retransmit queue
- */
- sort_queue_item_ptr = sq_item_add (sort_queue,
- &sort_queue_item, message_item->mcast->seq);
- // XXX printf ("ORIG [%s.%d-%d]\n", inet_ntoa (message_item->mcast->source),
- // XXX message_item->mcast->seq, message_item->mcast->this_seqno);
- /*
- * Only encrypt and send on wire if we are single processor
- */
- #define CONFIG_THREADED_SEND
- #ifdef CONFIG_THREADED_SEND
- work_item.sort_queue_item = sort_queue_item_ptr;
- work_item.instance = instance;
- /*
- * If threads, add message to worker thread queue
- */
- worker_thread_group_work_add (&instance->worker_thread_group_orf_token_mcast,
- &work_item);
- #else
- /*
- * If no threads, execute worker thread directly
- */
- // orf_token_mcast_worker_fn (&worker_data);
- #endif
- /*
- * Delete item from pending queue
- */
- queue_item_remove (mcast_queue);
- }
- assert (instance->fcc_mcast_current < 100);
- /*
- * If messages mcasted, deliver any new messages to totemg
- */
- instance->my_high_seq_received = token->seq;
-
- update_aru (instance);
- /*
- * Return 1 if more messages are available for single node clusters
- */
- return (instance->fcc_mcast_current);
- }
- /*
- * Remulticasts messages in orf_token's retransmit list (requires orf_token)
- * Modify's orf_token's rtr to include retransmits required by this process
- */
- static int orf_token_rtr (
- struct totemsrp_instance *instance,
- struct orf_token *orf_token,
- int *fcc_allowed)
- {
- int res;
- int i, j;
- int found;
- int total_entries;
- struct sq *sort_queue;
- struct rtr_item *rtr_list;
- if (instance->memb_state == MEMB_STATE_RECOVERY) {
- sort_queue = &instance->recovery_sort_queue;
- } else {
- sort_queue = &instance->regular_sort_queue;
- }
- rtr_list = &orf_token->rtr_list[0];
- if (orf_token->rtr_list_entries) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "Retransmit List %d\n", orf_token->rtr_list_entries);
- for (i = 0; i < orf_token->rtr_list_entries; i++) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "%d ", rtr_list[i].seq);
- }
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug, "\n");
- }
- total_entries = orf_token->rtr_list_entries;
- /*
- * Retransmit messages on orf_token's RTR list from RTR queue
- */
- for (instance->fcc_remcast_current = 0, i = 0;
- instance->fcc_remcast_current <= *fcc_allowed && i < orf_token->rtr_list_entries;) {
- /*
- * If this retransmit request isn't from this configuration,
- * try next rtr entry
- */
- if (memcmp (&rtr_list[i].ring_id, &instance->my_ring_id,
- sizeof (struct memb_ring_id)) != 0) {
- i += 1;
- continue;
- }
- assert (rtr_list[i].seq > 0);
- res = orf_token_remcast (instance, rtr_list[i].seq);
- if (res == 0) {
- /*
- * Multicasted message, so no need to copy to new retransmit list
- */
- orf_token->rtr_list_entries -= 1;
- assert (orf_token->rtr_list_entries >= 0);
- memmove (&rtr_list[i], &rtr_list[i + 1],
- sizeof (struct rtr_item) * (orf_token->rtr_list_entries));
- instance->fcc_remcast_current++;
- instance->stats_remcasts++;
- } else {
- i += 1;
- }
- }
- *fcc_allowed = *fcc_allowed - instance->fcc_remcast_current - 1;
- #ifdef COMPILE_OUT
- for (i = 0; i < orf_token->rtr_list_entries; i++) {
- assert (rtr_list_old[index_old].seq != -1);
- }
- #endif
- /*
- * Add messages to retransmit to RTR list
- * but only retry if there is room in the retransmit list
- */
- for (i = instance->my_aru + 1;
- orf_token->rtr_list_entries < RETRANSMIT_ENTRIES_MAX &&
- i <= instance->my_high_seq_received;
- i++) {
- /*
- * Find if a message is missing from this processor
- */
- res = sq_item_inuse (sort_queue, i);
- if (res == 0) {
- /*
- * Determine if missing message is already in retransmit list
- */
- found = 0;
- for (j = 0; j < orf_token->rtr_list_entries; j++) {
- if (i == rtr_list[j].seq) {
- found = 1;
- }
- }
- if (found == 0) {
- /*
- * Missing message not found in current retransmit list so add it
- */
- memcpy (&rtr_list[orf_token->rtr_list_entries].ring_id,
- &instance->my_ring_id, sizeof (struct memb_ring_id));
- rtr_list[orf_token->rtr_list_entries].seq = i;
- orf_token->rtr_list_entries++;
- }
- }
- }
- return (instance->fcc_remcast_current);
- }
- static void token_retransmit (struct totemsrp_instance *instance) {
- struct iovec iovec;
- struct msghdr msg_orf_token;
- int res;
- iovec.iov_base = instance->orf_token_retransmit;
- iovec.iov_len = instance->orf_token_retransmit_size;
- msg_orf_token.msg_name = &instance->next_memb;
- msg_orf_token.msg_namelen = sizeof (struct sockaddr_in);
- msg_orf_token.msg_iov = &iovec;
- msg_orf_token.msg_iovlen = 1;
- msg_orf_token.msg_control = 0;
- msg_orf_token.msg_controllen = 0;
- msg_orf_token.msg_flags = 0;
-
- res = sendmsg (instance->totemsrp_sockets[0].token, &msg_orf_token, MSG_NOSIGNAL);
- }
- /*
- * Retransmit the regular token if no mcast or token has
- * been received in retransmit token period retransmit
- * the token to the next processor
- */
- static void timer_function_token_retransmit_timeout (void *data)
- {
- struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
- switch (instance->memb_state) {
- case MEMB_STATE_GATHER:
- break;
- case MEMB_STATE_COMMIT:
- break;
- case MEMB_STATE_OPERATIONAL:
- case MEMB_STATE_RECOVERY:
- token_retransmit (instance);
- reset_token_retransmit_timeout (instance); // REVIEWED
- break;
- }
- }
- static void timer_function_token_hold_retransmit_timeout (void *data)
- {
- struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
- switch (instance->memb_state) {
- case MEMB_STATE_GATHER:
- break;
- case MEMB_STATE_COMMIT:
- break;
- case MEMB_STATE_OPERATIONAL:
- case MEMB_STATE_RECOVERY:
- token_retransmit (instance);
- break;
- }
- }
- static void timer_function_merge_detect_timeout(void *data)
- {
- struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
- instance->my_merge_detect_timeout_outstanding = 0;
- switch (instance->memb_state) {
- case MEMB_STATE_OPERATIONAL:
- if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr) {
- memb_merge_detect_transmit (instance);
- }
- break;
- case MEMB_STATE_GATHER:
- case MEMB_STATE_COMMIT:
- case MEMB_STATE_RECOVERY:
- break;
- }
- }
- /*
- * Send orf_token to next member (requires orf_token)
- */
- static int token_send (
- struct totemsrp_instance *instance,
- struct orf_token *orf_token,
- int forward_token)
- {
- struct msghdr msg_orf_token;
- struct iovec iovec;
- int res;
- iovec.iov_base = (char *)orf_token;
- iovec.iov_len = sizeof (struct orf_token) +
- (orf_token->rtr_list_entries * sizeof (struct rtr_item));
- encrypt_and_sign (instance, &iovec, 1);
- /*
- * Keep an encrypted copy in case the token retransmit timer expires
- */
- memcpy (instance->orf_token_retransmit, instance->iov_encrypted.iov_base, instance->iov_encrypted.iov_len);
- instance->orf_token_retransmit_size = instance->iov_encrypted.iov_len;
- /*
- * IF the user doesn't want the token forwarded, then dont send
- * it but keep an encrypted copy for the retransmit timeout
- */
- if (forward_token == 0) {
- return (0);
- }
-
- /*
- * Send the message
- */
- msg_orf_token.msg_name = &instance->next_memb;
- msg_orf_token.msg_namelen = sizeof (struct sockaddr_in);
- msg_orf_token.msg_iov = &instance->iov_encrypted;
- msg_orf_token.msg_iovlen = 1;
- msg_orf_token.msg_control = 0;
- msg_orf_token.msg_controllen = 0;
- msg_orf_token.msg_flags = 0;
- res = sendmsg (instance->totemsrp_sockets[0].token, &msg_orf_token, MSG_NOSIGNAL);
- if (res == -1) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Couldn't send token to addr %s %s %d\n",
- inet_ntoa (instance->next_memb.sin_addr),
- strerror (errno), instance->totemsrp_sockets[0].token);
- }
-
- /*
- * res not used here errors are handled by algorithm
- */
- if (res > 0) {
- instance->stats_sent += res;
- }
- return (res);
- }
- static int token_hold_cancel_send (struct totemsrp_instance *instance)
- {
- struct token_hold_cancel token_hold_cancel;
- struct iovec iov;
- struct msghdr msghdr;
- /*
- * Only cancel if the token is currently held
- */
- if (instance->my_token_held == 0) {
- return (0);
- }
- instance->my_token_held = 0;
- /*
- * Build message
- */
- token_hold_cancel.header.type = MESSAGE_TYPE_TOKEN_HOLD_CANCEL;
- token_hold_cancel.header.endian_detector = ENDIAN_LOCAL;
- memcpy (&token_hold_cancel.ring_id, &instance->my_ring_id,
- sizeof (struct memb_ring_id));
- iov.iov_base = &token_hold_cancel;
- iov.iov_len = sizeof (struct token_hold_cancel);
- encrypt_and_sign (instance, &iov, 1);
- /*
- * Build multicast message
- */
- msghdr.msg_name = (caddr_t)&instance->sockaddr_in_mcast;
- msghdr.msg_namelen = sizeof (struct sockaddr_in);
- msghdr.msg_iov = &instance->iov_encrypted;
- msghdr.msg_iovlen = 1;
- msghdr.msg_control = 0;
- msghdr.msg_controllen = 0;
- msghdr.msg_flags = 0;
- /*
- * Multicast message
- */
- sendmsg (instance->totemsrp_sockets[0].mcast, &msghdr, MSG_NOSIGNAL | MSG_DONTWAIT);
- return (0);
- }
- static int orf_token_send_initial (struct totemsrp_instance *instance)
- {
- struct orf_token orf_token;
- int res;
- orf_token.header.type = MESSAGE_TYPE_ORF_TOKEN;
- orf_token.header.endian_detector = ENDIAN_LOCAL;
- orf_token.header.encapsulated = 0;
- orf_token.seq = 0;
- orf_token.token_seq = 0;
- orf_token.retrans_flg = 1;
- instance->my_set_retrans_flg = 1;
- /*
- if (queue_is_empty (&instance->retrans_message_queue) == 1) {
- orf_token.retrans_flg = 0;
- } else {
- orf_token.retrans_flg = 1;
- instance->my_set_retrans_flg = 1;
- }
- */
-
- orf_token.aru = 0;
- // orf_token.aru_addr.s_addr = 0;//instance->my_id.sin_addr.s_addr;
- orf_token.aru_addr.s_addr = instance->my_id.sin_addr.s_addr;
- memcpy (&orf_token.ring_id, &instance->my_ring_id, sizeof (struct memb_ring_id));
- orf_token.fcc = 0;
- orf_token.rtr_list_entries = 0;
- res = token_send (instance, &orf_token, 1);
- return (res);
- }
- static void memb_state_commit_token_update (
- struct totemsrp_instance *instance,
- struct memb_commit_token *memb_commit_token)
- {
- int memb_index_this;
- memb_index_this = (memb_commit_token->memb_index + 1) % memb_commit_token->addr_entries;
- memcpy (&memb_commit_token->memb_list[memb_index_this].ring_id,
- &instance->my_old_ring_id, sizeof (struct memb_ring_id));
- assert (instance->my_old_ring_id.rep.s_addr != 0);
- memb_commit_token->memb_list[memb_index_this].aru = instance->old_ring_state_aru;
- /*
- * TODO high delivered is really instance->my_aru, but with safe this
- * could change?
- */
- memb_commit_token->memb_list[memb_index_this].high_delivered = instance->my_high_delivered;
- memb_commit_token->memb_list[memb_index_this].received_flg = instance->my_received_flg;
- }
- static int memb_state_commit_token_send (struct totemsrp_instance *instance,
- struct memb_commit_token *memb_commit_token)
- {
- struct msghdr msghdr;
- struct iovec iovec;
- int res;
- int memb_index_this;
- int memb_index_next;
- memb_commit_token->token_seq++;
- memb_index_this = (memb_commit_token->memb_index + 1) % memb_commit_token->addr_entries;
- memb_index_next = (memb_index_this + 1) % memb_commit_token->addr_entries;
- memb_commit_token->memb_index = memb_index_this;
- iovec.iov_base = memb_commit_token;
- iovec.iov_len = sizeof (struct memb_commit_token);
- encrypt_and_sign (instance, &iovec, 1);
- instance->next_memb.sin_addr.s_addr = memb_commit_token->addr[memb_index_next].s_addr;
- instance->next_memb.sin_family = AF_INET;
- instance->next_memb.sin_port = instance->sockaddr_in_mcast.sin_port;
- msghdr.msg_name = &instance->next_memb;
- msghdr.msg_namelen = sizeof (struct sockaddr_in);
- msghdr.msg_iov = &instance->iov_encrypted;
- msghdr.msg_iovlen = 1;
- msghdr.msg_control = 0;
- msghdr.msg_controllen = 0;
- msghdr.msg_flags = 0;
- res = sendmsg (instance->totemsrp_sockets[0].token, &msghdr, MSG_NOSIGNAL | MSG_DONTWAIT);
- return (res);
- }
- static int memb_lowest_in_config (struct totemsrp_instance *instance)
- {
- struct in_addr token_memb[PROCESSOR_COUNT_MAX];
- int token_memb_entries = 0;
- struct in_addr lowest_addr;
- int i;
- lowest_addr.s_addr = 0xFFFFFFFF;
- memb_set_subtract (token_memb, &token_memb_entries,
- instance->my_proc_list, instance->my_proc_list_entries,
- instance->my_failed_list, instance->my_failed_list_entries);
- /*
- * find representative by searching for smallest identifier
- */
- for (i = 0; i < token_memb_entries; i++) {
- if (lowest_addr.s_addr > token_memb[i].s_addr) {
- lowest_addr.s_addr = token_memb[i].s_addr;
- }
- }
- return (instance->my_id.sin_addr.s_addr == lowest_addr.s_addr);
- }
- static void memb_state_commit_token_create (
- struct totemsrp_instance *instance,
- struct memb_commit_token *commit_token)
- {
- struct in_addr token_memb[PROCESSOR_COUNT_MAX];
- int token_memb_entries = 0;
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Creating commit token because I am the rep.\n");
- memb_set_subtract (token_memb, &token_memb_entries,
- instance->my_proc_list, instance->my_proc_list_entries,
- instance->my_failed_list, instance->my_failed_list_entries);
- memset (commit_token, 0, sizeof (struct memb_commit_token));
- commit_token->header.type = MESSAGE_TYPE_MEMB_COMMIT_TOKEN;
- commit_token->header.endian_detector = ENDIAN_LOCAL;
- commit_token->header.encapsulated = 0;
- commit_token->ring_id.rep.s_addr = instance->my_id.sin_addr.s_addr;
- commit_token->ring_id.seq = instance->token_ring_id_seq + 4;
- qsort (token_memb, token_memb_entries,
- sizeof (struct in_addr), in_addr_compare);
- memcpy (commit_token->addr, token_memb,
- token_memb_entries * sizeof (struct in_addr));
- memset (commit_token->memb_list, 0,
- sizeof (struct memb_commit_token_memb_entry) * PROCESSOR_COUNT_MAX);
- commit_token->memb_index = token_memb_entries - 1;
- commit_token->addr_entries = token_memb_entries;
- }
- static int memb_join_message_send (struct totemsrp_instance *instance)
- {
- struct msghdr msghdr;
- struct iovec iovec;
- struct memb_join memb_join;
- int res;
- memb_join.header.type = MESSAGE_TYPE_MEMB_JOIN;
- memb_join.header.endian_detector = ENDIAN_LOCAL;
- memb_join.header.encapsulated = 0;
- memb_join.ring_seq = instance->my_ring_id.seq;
- memcpy (memb_join.proc_list, instance->my_proc_list,
- instance->my_proc_list_entries * sizeof (struct in_addr));
- memb_join.proc_list_entries = instance->my_proc_list_entries;
- memcpy (memb_join.failed_list, instance->my_failed_list,
- instance->my_failed_list_entries * sizeof (struct in_addr));
- memb_join.failed_list_entries = instance->my_failed_list_entries;
-
- iovec.iov_base = &memb_join;
- iovec.iov_len = sizeof (struct memb_join);
- encrypt_and_sign (instance, &iovec, 1);
- msghdr.msg_name = &instance->sockaddr_in_mcast;
- msghdr.msg_namelen = sizeof (struct sockaddr_in);
- msghdr.msg_iov = &instance->iov_encrypted;
- msghdr.msg_iovlen = 1;
- msghdr.msg_control = 0;
- msghdr.msg_controllen = 0;
- msghdr.msg_flags = 0;
- res = sendmsg (instance->totemsrp_sockets[0].mcast, &msghdr, MSG_NOSIGNAL | MSG_DONTWAIT);
- return (res);
- }
- static int memb_merge_detect_transmit (struct totemsrp_instance *instance)
- {
- struct msghdr msghdr;
- struct iovec iovec;
- struct memb_merge_detect memb_merge_detect;
- int res;
- memb_merge_detect.header.type = MESSAGE_TYPE_MEMB_MERGE_DETECT;
- memb_merge_detect.header.endian_detector = ENDIAN_LOCAL;
- memb_merge_detect.header.encapsulated = 0;
- memcpy (&memb_merge_detect.ring_id, &instance->my_ring_id,
- sizeof (struct memb_ring_id));
- iovec.iov_base = &memb_merge_detect;
- iovec.iov_len = sizeof (struct memb_merge_detect);
- encrypt_and_sign (instance, &iovec, 1);
- msghdr.msg_name = &instance->sockaddr_in_mcast;
- msghdr.msg_namelen = sizeof (struct sockaddr_in);
- msghdr.msg_iov = &instance->iov_encrypted;
- msghdr.msg_iovlen = 1;
- msghdr.msg_control = 0;
- msghdr.msg_controllen = 0;
- msghdr.msg_flags = 0;
- res = sendmsg (instance->totemsrp_sockets[0].mcast, &msghdr,
- MSG_NOSIGNAL | MSG_DONTWAIT);
- return (res);
- }
- static void memb_ring_id_create_or_load (
- struct totemsrp_instance *instance,
- struct memb_ring_id *memb_ring_id)
- {
- int fd;
- int res;
- char filename[256];
- sprintf (filename, "/tmp/ringid_%s",
- inet_ntoa (instance->my_id.sin_addr));
- fd = open (filename, O_RDONLY, 0777);
- if (fd > 0) {
- res = read (fd, &memb_ring_id->seq, sizeof (unsigned long long));
- assert (res == sizeof (unsigned long long));
- close (fd);
- } else
- if (fd == -1 && errno == ENOENT) {
- memb_ring_id->seq = 0;
- umask(0);
- fd = open (filename, O_CREAT|O_RDWR, 0777);
- if (fd == -1) {
- printf ("couldn't create file %d %s\n", fd, strerror(errno));
- }
- res = write (fd, &memb_ring_id->seq, sizeof (unsigned long long));
- assert (res == sizeof (unsigned long long));
- close (fd);
- } else {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_warning,
- "Couldn't open %s %s\n", filename, strerror (errno));
- }
-
- memb_ring_id->rep.s_addr = instance->my_id.sin_addr.s_addr;
- assert (memb_ring_id->rep.s_addr);
- instance->token_ring_id_seq = memb_ring_id->seq;
- }
- static void memb_ring_id_store (
- struct totemsrp_instance *instance,
- struct memb_commit_token *commit_token)
- {
- char filename[256];
- int fd;
- int res;
- sprintf (filename, "/tmp/ringid_%s",
- inet_ntoa (instance->my_id.sin_addr));
- fd = open (filename, O_WRONLY, 0777);
- if (fd == -1) {
- fd = open (filename, O_CREAT|O_RDWR, 0777);
- }
- if (fd == -1) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_warning,
- "Couldn't store new ring id %llx to stable storage (%s)\n",
- commit_token->ring_id.seq, strerror (errno));
- assert (0);
- return;
- }
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Storing new sequence id for ring %d\n", commit_token->ring_id.seq);
- assert (fd > 0);
- res = write (fd, &commit_token->ring_id.seq, sizeof (unsigned long long));
- assert (res == sizeof (unsigned long long));
- close (fd);
- memcpy (&instance->my_ring_id, &commit_token->ring_id, sizeof (struct memb_ring_id));
- instance->token_ring_id_seq = instance->my_ring_id.seq;
- }
- void print_stats (totemsrp_handle handle)
- {
- struct timeval tv_end;
- struct totemsrp_instance *instance;
- SaAisErrorT error;
- gettimeofday (&tv_end, NULL);
-
- error = saHandleInstanceGet (&totemsrp_instance_database, handle,
- (void *)&instance);
- if (error != SA_OK) {
- return;
- }
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Bytes recv %d\n", instance->stats_recv);
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Bytes sent %d\n", instance->stats_sent);
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Messages delivered %d\n", instance->stats_delv);
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Re-Mcasts %d\n", instance->stats_remcasts);
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice, "Tokens process %d\n", instance->stats_orf_token);
- saHandleInstancePut (&totemsrp_instance_database, handle);
- }
- int totemsrp_callback_token_create (
- totemsrp_handle handle,
- void **handle_out,
- enum totem_callback_token_type type,
- int delete,
- int (*callback_fn) (enum totem_callback_token_type type, void *),
- void *data)
- {
- struct token_callback_instance *callback_handle;
- struct totemsrp_instance *instance;
- SaErrorT error;
- error = saHandleInstanceGet (&totemsrp_instance_database, handle,
- (void *)&instance);
- if (error != SA_OK) {
- goto error_exit;
- }
- callback_handle = (struct token_callback_instance *)malloc (sizeof (struct token_callback_instance));
- if (callback_handle == 0) {
- return (-1);
- }
- *handle_out = (void *)callback_handle;
- list_init (&callback_handle->list);
- callback_handle->callback_fn = callback_fn;
- callback_handle->data = data;
- callback_handle->callback_type = type;
- callback_handle->delete = delete;
- switch (type) {
- case TOTEM_CALLBACK_TOKEN_RECEIVED:
- list_add (&callback_handle->list, &instance->token_callback_received_listhead);
- break;
- case TOTEM_CALLBACK_TOKEN_SENT:
- list_add (&callback_handle->list, &instance->token_callback_sent_listhead);
- break;
- }
- saHandleInstancePut (&totemsrp_instance_database, handle);
- error_exit:
- return (0);
- }
- void totemsrp_callback_token_destroy (totemsrp_handle handle, void **handle_out)
- {
- struct token_callback_instance *h;
- if (*handle_out) {
- h = (struct token_callback_instance *)*handle_out;
- list_del (&h->list);
- free (h);
- h = NULL;
- *handle_out = 0;
- }
- }
- void totem_callback_token_type (struct totemsrp_instance *instance, void *handle)
- {
- struct token_callback_instance *token_callback_instance = (struct token_callback_instance *)handle;
- list_del (&token_callback_instance->list);
- free (token_callback_instance);
- }
- static void token_callbacks_execute (
- struct totemsrp_instance *instance,
- enum totem_callback_token_type type)
- {
- struct list_head *list;
- struct list_head *list_next;
- struct list_head *callback_listhead = 0;
- struct token_callback_instance *token_callback_instance;
- int res;
- int del;
- switch (type) {
- case TOTEM_CALLBACK_TOKEN_RECEIVED:
- callback_listhead = &instance->token_callback_received_listhead;
- break;
- case TOTEM_CALLBACK_TOKEN_SENT:
- callback_listhead = &instance->token_callback_sent_listhead;
- break;
- default:
- assert (0);
- }
-
- for (list = callback_listhead->next; list != callback_listhead;
- list = list_next) {
- token_callback_instance = list_entry (list, struct token_callback_instance, list);
- list_next = list->next;
- del = token_callback_instance->delete;
- if (del == 1) {
- list_del (list);
- }
- res = token_callback_instance->callback_fn (
- token_callback_instance->callback_type,
- token_callback_instance->data);
- /*
- * This callback failed to execute, try it again on the next token
- */
- if (res == -1 && del == 1) {
- list_add (list, callback_listhead);
- } else if (del) {
- free (token_callback_instance);
- }
- }
- }
- /*
- * Message Handlers
- */
- /*
- * message handler called when TOKEN message type received
- */
- static int message_handler_orf_token (
- struct totemsrp_instance *instance,
- struct sockaddr_in *system_from,
- struct iovec *iovec,
- int iov_len,
- int bytes_received,
- int endian_conversion_needed)
- {
- char token_storage[1500];
- char token_convert[1500];
- struct orf_token *token;
- int prio = UINT_MAX;
- struct pollfd ufd;
- int nfds;
- struct orf_token *token_ref = (struct orf_token *)iovec->iov_base;
- int transmits_allowed;
- int forward_token;
- int mcasted;
- int last_aru;
- int low_water;
- #ifdef GIVEINFO
- struct timeval tv_current;
- struct timeval tv_diff;
- gettimeofday (&tv_current, NULL);
- timersub (&tv_current, &tv_old, &tv_diff);
- memcpy (&tv_old, &tv_current, sizeof (struct timeval));
- if ((((float)tv_diff.tv_usec) / 100.0) > 5.0) {
- printf ("OTHERS %0.4f ms\n", ((float)tv_diff.tv_usec) / 100.0);
- }
- #endif
- #ifdef RANDOM_DROP
- if (random () % 100 < 10) {
- return (0);
- }
- #endif
- /*
- * Handle merge detection timeout
- */
- if (token_ref->seq == instance->my_last_seq) {
- start_merge_detect_timeout (instance);
- instance->my_seq_unchanged += 1;
- } else {
- cancel_merge_detect_timeout (instance);
- cancel_token_hold_retransmit_timeout (instance);
- instance->my_seq_unchanged = 0;
- }
- instance->my_last_seq = token_ref->seq;
- assert (bytes_received >= sizeof (struct orf_token));
- // assert (bytes_received == sizeof (struct orf_token) +
- // (sizeof (struct rtr_item) * token_ref->rtr_list_entries);
- /*
- * Make copy of token and retransmit list in case we have
- * to flush incoming messages from the kernel queue
- */
- token = (struct orf_token *)token_storage;
- memcpy (token, iovec->iov_base, sizeof (struct orf_token));
- memcpy (&token->rtr_list[0], iovec->iov_base + sizeof (struct orf_token),
- sizeof (struct rtr_item) * RETRANSMIT_ENTRIES_MAX);
- if (endian_conversion_needed) {
- orf_token_endian_convert (token, (struct orf_token *)token_convert);
- token = (struct orf_token *)token_convert;
- }
- /*
- * flush incoming queue from kernel
- */
- do {
- ufd.fd = instance->totemsrp_sockets[0].mcast;
- ufd.events = POLLIN;
- nfds = poll (&ufd, 1, 0);
- if (nfds == 1 && ufd.revents & POLLIN) {
- instance->totemsrp_iov_recv.iov_len = PACKET_SIZE_MAX;
- recv_handler (0, instance->totemsrp_sockets[0].mcast,
- ufd.revents, instance, &prio);
- }
- } while (nfds == 1);
- /*
- * Determine if we should hold (in reality drop) the token
- */
- instance->my_token_held = 0;
- if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr &&
- instance->my_seq_unchanged > SEQNO_UNCHANGED_CONST) {
- instance->my_token_held = 1;
- } else
- if (instance->my_ring_id.rep.s_addr != instance->my_id.sin_addr.s_addr &&
- instance->my_seq_unchanged >= SEQNO_UNCHANGED_CONST) {
- instance->my_token_held = 1;
- }
- /*
- * Hold onto token when there is no activity on ring and
- * this processor is the ring rep
- */
- forward_token = 1;
- if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr) {
- if (instance->my_token_held) {
- forward_token = 0;
- }
- }
- token_callbacks_execute (instance, TOTEM_CALLBACK_TOKEN_RECEIVED);
- switch (instance->memb_state) {
- case MEMB_STATE_COMMIT:
- /* Discard token */
- break;
- case MEMB_STATE_OPERATIONAL:
- messages_free (instance, token->aru);
- case MEMB_STATE_GATHER:
- /*
- * DO NOT add break, we use different free mechanism in recovery state
- */
- case MEMB_STATE_RECOVERY:
- last_aru = instance->my_last_aru;
- instance->my_last_aru = token->aru;
- /*
- * Discard tokens from another configuration
- */
- if (memcmp (&token->ring_id, &instance->my_ring_id,
- sizeof (struct memb_ring_id)) != 0) {
- return (0); /* discard token */
- }
- /*
- * Discard retransmitted tokens
- */
- if (instance->my_token_seq >= token->token_seq) {
- reset_token_retransmit_timeout (instance);
- reset_token_timeout (instance);
- return (0); /* discard token */
- }
- transmits_allowed = 30;
- mcasted = orf_token_rtr (instance, token, &transmits_allowed);
- if ((last_aru + MISSING_MCAST_WINDOW) < token->seq) {
- transmits_allowed = 0;
- }
- mcasted = orf_token_mcast (instance, token, transmits_allowed, system_from);
- if (instance->my_aru < token->aru ||
- instance->my_id.sin_addr.s_addr == token->aru_addr.s_addr ||
- token->aru_addr.s_addr == 0) {
-
- token->aru = instance->my_aru;
- if (token->aru == token->seq) {
- token->aru_addr.s_addr = 0;
- } else {
- token->aru_addr.s_addr = instance->my_id.sin_addr.s_addr;
- }
- }
- if (token->aru == last_aru && token->aru_addr.s_addr != 0) {
- instance->my_aru_count += 1;
- } else {
- instance->my_aru_count = 0;
- }
- if (instance->my_aru_count > instance->fail_to_recv_const &&
- token->aru_addr.s_addr != instance->my_id.sin_addr.s_addr) {
-
- printf ("FAILED TO RECEIVE\n");
- // TODO if we fail to receive, it may be possible to end with a gather
- // state of proc == failed = 0 entries
- memb_set_merge (&token->aru_addr, 1,
- instance->my_failed_list,
- &instance->my_failed_list_entries);
- ring_state_restore (instance);
- memb_state_gather_enter (instance);
- } else {
- instance->my_token_seq = token->token_seq;
- token->token_seq += 1;
- if (instance->memb_state == MEMB_STATE_RECOVERY) {
- /*
- * instance->my_aru == instance->my_high_seq_received means this processor
- * has recovered all messages it can recover
- * (ie: its retrans queue is empty)
- */
- low_water = instance->my_aru;
- if (low_water > last_aru) {
- low_water = last_aru;
- }
- // TODO is this code right
- if (queue_is_empty (&instance->retrans_message_queue) == 0 ||
- low_water != instance->my_high_seq_received) {
- if (token->retrans_flg == 0) {
- token->retrans_flg = 1;
- instance->my_set_retrans_flg = 1;
- }
- } else
- if (token->retrans_flg == 1 && instance->my_set_retrans_flg) {
- token->retrans_flg = 0;
- }
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "token retrans flag is %d my set retrans flag%d retrans queue empty %d count %d, low_water %d aru %d\n",
- token->retrans_flg, instance->my_set_retrans_flg,
- queue_is_empty (&instance->retrans_message_queue),
- instance->my_retrans_flg_count, low_water, token->aru);
- if (token->retrans_flg == 0) {
- instance->my_retrans_flg_count += 1;
- } else {
- instance->my_retrans_flg_count = 0;
- }
- if (instance->my_retrans_flg_count == 2) {
- instance->my_install_seq = token->seq;
- }
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "install seq %d aru %d high seq received %d\n",
- instance->my_install_seq, instance->my_aru, instance->my_high_seq_received);
- if (instance->my_retrans_flg_count >= 2 && instance->my_aru >= instance->my_install_seq && instance->my_received_flg == 0) {
- instance->my_received_flg = 1;
- instance->my_deliver_memb_entries = instance->my_trans_memb_entries;
- memcpy (instance->my_deliver_memb_list, instance->my_trans_memb_list,
- sizeof (struct in_addr) * instance->my_trans_memb_entries);
- }
- if (instance->my_retrans_flg_count >= 3 && token->aru >= instance->my_install_seq) {
- instance->my_rotation_counter += 1;
- } else {
- instance->my_rotation_counter = 0;
- }
- if (instance->my_rotation_counter == 2) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "retrans flag count %d token aru %d install seq %d aru %d %d\n",
- instance->my_retrans_flg_count, token->aru, instance->my_install_seq,
- instance->my_aru, token->seq);
- memb_state_operational_enter (instance);
- instance->my_rotation_counter = 0;
- instance->my_retrans_flg_count = 0;
- }
- }
-
- #ifdef CONFIG_THREADED_SEND
- worker_thread_group_wait (&instance->worker_thread_group_orf_token_mcast);
- #endif
- token_send (instance, token, forward_token);
- #ifdef GIVEINFO
- gettimeofday (&tv_current, NULL);
- timersub (&tv_current, &tv_old, &tv_diff);
- memcpy (&tv_old, &tv_current, sizeof (struct timeval));
- if ((((float)tv_diff.tv_usec) / 100.0) > 5.0) {
- printf ("I held %0.4f ms\n", ((float)tv_diff.tv_usec) / 100.0);
- }
- #endif
- if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
- messages_deliver_to_app (instance, 0, instance->my_high_seq_received);
- }
- /*
- * Deliver messages after token has been transmitted
- * to improve performance
- */
- reset_token_timeout (instance); // REVIEWED
- reset_token_retransmit_timeout (instance); // REVIEWED
- if (instance->my_id.sin_addr.s_addr == instance->my_ring_id.rep.s_addr &&
- instance->my_token_held == 1) {
- start_token_hold_retransmit_timeout (instance);
- }
- token_callbacks_execute (instance, TOTEM_CALLBACK_TOKEN_SENT);
- }
- break;
- }
- return (0);
- }
- static void messages_deliver_to_app (
- struct totemsrp_instance *instance,
- int skip,
- int end_point)
- {
- struct sort_queue_item *sort_queue_item_p;
- int i;
- int res;
- struct mcast *mcast;
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "Delivering %d to %d\n", instance->my_high_delivered + 1,
- end_point);
- /*
- * Deliver messages in order from rtr queue to pending delivery queue
- */
- for (i = instance->my_high_delivered + 1; i <= end_point; i++) {
- void *ptr;
- res = sq_item_get (&instance->regular_sort_queue, i, &ptr);
- if (res != 0 && skip) {
- printf ("-skipping %d-\n", i);
- instance->my_high_delivered = i;
- continue;
- }
- /*
- * If hole, stop assembly
- */
- if (res != 0) {
- break;
- }
- sort_queue_item_p = ptr;
- mcast = sort_queue_item_p->iovec[0].iov_base;
- assert (mcast != (struct mcast *)0xdeadbeef);
- // XXX printf ("[%s.%d-%d]\n", inet_ntoa (mcast->source),
- // XXX mcast->seq, mcast->this_seqno);
- /*
- * Skip messages not originated in instance->my_deliver_memb
- */
- if (skip &&
- memb_set_subset (&mcast->source,
- 1,
- instance->my_deliver_memb_list,
- instance->my_deliver_memb_entries) == 0) {
- printf ("-skipping %d - wrong ip", i);
- instance->my_high_delivered = i;
- continue;
- }
- instance->my_high_delivered = i;
- /*
- * Message found
- */
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "Delivering MCAST message with seq %d to pending delivery queue\n",
- mcast->seq);
- /*
- * Message is locally originated multicast
- */
- if (sort_queue_item_p->iov_len > 1 &&
- sort_queue_item_p->iovec[0].iov_len == sizeof (struct mcast)) {
- instance->totemsrp_deliver_fn (
- mcast->source,
- &sort_queue_item_p->iovec[1],
- sort_queue_item_p->iov_len - 1,
- mcast->header.endian_detector != ENDIAN_LOCAL);
- } else {
- sort_queue_item_p->iovec[0].iov_len -= sizeof (struct mcast);
- sort_queue_item_p->iovec[0].iov_base += sizeof (struct mcast);
- instance->totemsrp_deliver_fn (
- mcast->source,
- sort_queue_item_p->iovec,
- sort_queue_item_p->iov_len,
- mcast->header.endian_detector != ENDIAN_LOCAL);
- sort_queue_item_p->iovec[0].iov_len += sizeof (struct mcast);
- sort_queue_item_p->iovec[0].iov_base -= sizeof (struct mcast);
- }
- instance->stats_delv += 1;
- }
- instance->my_received_flg = 0;
- if (instance->my_aru == instance->my_high_seq_received) {
- // instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- // "setting received flag to TRUE %d %d\n",
- // instance->my_aru, instance->my_high_seq_received);
- instance->my_received_flg = 1;
- }
- }
- /*
- * recv message handler called when MCAST message type received
- */
- static int message_handler_mcast (
- struct totemsrp_instance *instance,
- struct sockaddr_in *system_from,
- struct iovec *iovec,
- int iov_len,
- int bytes_received,
- int endian_conversion_needed)
- {
- struct sort_queue_item sort_queue_item;
- struct sq *sort_queue;
- struct mcast mcast_header;
-
- if (endian_conversion_needed) {
- mcast_endian_convert (iovec[0].iov_base, &mcast_header);
- } else {
- memcpy (&mcast_header, iovec[0].iov_base, sizeof (struct mcast));
- }
- if (mcast_header.header.encapsulated == 1) {
- sort_queue = &instance->recovery_sort_queue;
- } else {
- sort_queue = &instance->regular_sort_queue;
- }
- assert (bytes_received < PACKET_SIZE_MAX);
- #ifdef RANDOM_DROP
- if (random()%100 < 50) {
- return (0);
- }
- #endif
- if (system_from->sin_addr.s_addr != instance->my_id.sin_addr.s_addr) {
- cancel_token_retransmit_timeout (instance);
- }
- /*
- * If the message is foreign execute the switch below
- */
- if (memcmp (&instance->my_ring_id, &mcast_header.ring_id,
- sizeof (struct memb_ring_id)) != 0) {
- switch (instance->memb_state) {
- case MEMB_STATE_OPERATIONAL:
- memb_set_merge (&system_from->sin_addr, 1,
- instance->my_proc_list, &instance->my_proc_list_entries);
- memb_state_gather_enter (instance);
- break;
- case MEMB_STATE_GATHER:
- if (!memb_set_subset (&system_from->sin_addr,
- 1,
- instance->my_proc_list,
- instance->my_proc_list_entries)) {
- memb_set_merge (&system_from->sin_addr, 1,
- instance->my_proc_list, &instance->my_proc_list_entries);
- memb_state_gather_enter (instance);
- return (0);
- }
- break;
- case MEMB_STATE_COMMIT:
- /* discard message */
- break;
- case MEMB_STATE_RECOVERY:
- /* discard message */
- break;
- }
- return (0);
- }
- instance->totemsrp_log_printf (instance->totemsrp_log_level_debug,
- "Received ringid(%s:%lld) seq %d\n",
- inet_ntoa (mcast_header.ring_id.rep),
- mcast_header.ring_id.seq,
- mcast_header.seq);
- /*
- * Add mcast message to rtr queue if not already in rtr queue
- * otherwise free io vectors
- */
- if (bytes_received > 0 && bytes_received < PACKET_SIZE_MAX &&
- instance->my_aru < mcast_header.seq &&
- sq_item_inuse (sort_queue, mcast_header.seq) == 0) {
- /*
- * Allocate new multicast memory block
- */
- // TODO LEAK
- sort_queue_item.iovec[0].iov_base = malloc (bytes_received);
- if (sort_queue_item.iovec[0].iov_base == 0) {
- return (-1); /* error here is corrected by the algorithm */
- }
- memcpy (sort_queue_item.iovec[0].iov_base, iovec[0].iov_base,
- bytes_received);
- sort_queue_item.iovec[0].iov_len = bytes_received;
- assert (sort_queue_item.iovec[0].iov_len > 0);
- assert (sort_queue_item.iovec[0].iov_len < PACKET_SIZE_MAX);
- sort_queue_item.iov_len = 1;
-
- if (mcast_header.seq > instance->my_high_seq_received) {
- instance->my_high_seq_received = mcast_header.seq;
- }
- sq_item_add (sort_queue, &sort_queue_item, mcast_header.seq);
- }
- if (instance->memb_state == MEMB_STATE_OPERATIONAL) {
- update_aru (instance);
- messages_deliver_to_app (instance, 0, instance->my_high_seq_received);
- }
- /* TODO remove from retrans message queue for old ring in recovery state */
- return (0);
- }
- static int message_handler_memb_merge_detect (
- struct totemsrp_instance *instance,
- struct sockaddr_in *system_from,
- struct iovec *iovec,
- int iov_len,
- int bytes_received,
- int endian_conversion_needed)
- {
- struct memb_merge_detect *memb_merge_detect = iovec[0].iov_base;
- /*
- * do nothing if this is a merge detect from this configuration
- */
- if (memcmp (&instance->my_ring_id, &memb_merge_detect->ring_id,
- sizeof (struct memb_ring_id)) == 0) {
- return (0);
- }
- /*
- * Execute merge operation
- */
- switch (instance->memb_state) {
- case MEMB_STATE_OPERATIONAL:
- memb_set_merge (&system_from->sin_addr, 1,
- instance->my_proc_list, &instance->my_proc_list_entries);
- memb_state_gather_enter (instance);
- break;
- case MEMB_STATE_GATHER:
- if (!memb_set_subset (&system_from->sin_addr,
- 1,
- instance->my_proc_list,
- instance->my_proc_list_entries)) {
- memb_set_merge (&system_from->sin_addr, 1,
- instance->my_proc_list, &instance->my_proc_list_entries);
- memb_state_gather_enter (instance);
- return (0);
- }
- break;
- case MEMB_STATE_COMMIT:
- /* do nothing in commit */
- break;
- case MEMB_STATE_RECOVERY:
- /* do nothing in recovery */
- break;
- }
- return (0);
- }
- static int memb_join_process (
- struct totemsrp_instance *instance,
- struct memb_join *memb_join,
- struct sockaddr_in *system_from)
- {
- struct memb_commit_token my_commit_token;
- if (memb_set_equal (memb_join->proc_list,
- memb_join->proc_list_entries,
- instance->my_proc_list,
- instance->my_proc_list_entries) &&
- memb_set_equal (memb_join->failed_list,
- memb_join->failed_list_entries,
- instance->my_failed_list,
- instance->my_failed_list_entries)) {
- memb_consensus_set (instance, &system_from->sin_addr);
-
- if (memb_consensus_agreed (instance) &&
- memb_lowest_in_config (instance)) {
- memb_state_commit_token_create (instance, &my_commit_token);
-
- memb_state_commit_enter (instance, &my_commit_token);
- } else {
- return (0);
- }
- } else
- if (memb_set_subset (memb_join->proc_list,
- memb_join->proc_list_entries,
- instance->my_proc_list,
- instance->my_proc_list_entries) &&
- memb_set_subset (memb_join->failed_list,
- memb_join->failed_list_entries,
- instance->my_failed_list,
- instance->my_failed_list_entries)) {
- return (0);
- } else
- if (memb_set_subset (&system_from->sin_addr, 1,
- instance->my_failed_list, instance->my_failed_list_entries)) {
- return (0);
- } else {
- memb_set_merge (memb_join->proc_list,
- memb_join->proc_list_entries,
- instance->my_proc_list, &instance->my_proc_list_entries);
- if (memb_set_subset (&instance->my_id.sin_addr, 1,
- memb_join->failed_list, memb_join->failed_list_entries)) {
- memb_set_merge (&system_from->sin_addr, 1,
- instance->my_failed_list, &instance->my_failed_list_entries);
- } else {
- memb_set_merge (memb_join->failed_list,
- memb_join->failed_list_entries,
- instance->my_failed_list, &instance->my_failed_list_entries);
- }
- memb_state_gather_enter (instance);
- return (1); /* gather entered */
- }
- return (0); /* gather not entered */
- }
- static void memb_join_endian_convert (struct memb_join *in, struct memb_join *out)
- {
- int i;
- out->header.type = in->header.type;
- out->header.endian_detector = ENDIAN_LOCAL;
- out->proc_list_entries = swab32 (in->proc_list_entries);
- out->failed_list_entries = swab32 (in->failed_list_entries);
- out->ring_seq = swab64 (in->ring_seq);
- for (i = 0; i < out->proc_list_entries; i++) {
- out->proc_list[i].s_addr = in->proc_list[i].s_addr;
- }
- for (i = 0; i < out->failed_list_entries; i++) {
- out->failed_list[i].s_addr = in->failed_list[i].s_addr;
- }
- }
- static void memb_commit_token_endian_convert (struct memb_commit_token *in, struct memb_commit_token *out)
- {
- int i;
- out->header.type = in->header.type;
- out->header.endian_detector = ENDIAN_LOCAL;
- out->token_seq = swab32 (in->token_seq);
- out->ring_id.rep.s_addr = in->ring_id.rep.s_addr;
- out->ring_id.seq = swab64 (in->ring_id.seq);
- out->retrans_flg = swab32 (in->retrans_flg);
- out->memb_index = swab32 (in->memb_index);
- out->addr_entries = swab32 (in->addr_entries);
- for (i = 0; i < out->addr_entries; i++) {
- out->addr[i].s_addr = in->addr[i].s_addr;
- out->memb_list[i].ring_id.rep.s_addr =
- in->memb_list[i].ring_id.rep.s_addr;
- out->memb_list[i].ring_id.seq =
- swab64 (in->memb_list[i].ring_id.seq);
- out->memb_list[i].aru = swab32 (in->memb_list[i].aru);
- out->memb_list[i].high_delivered = swab32 (in->memb_list[i].high_delivered);
- out->memb_list[i].received_flg = swab32 (in->memb_list[i].received_flg);
- }
- }
- static void orf_token_endian_convert (struct orf_token *in, struct orf_token *out)
- {
- int i;
- out->header.type = in->header.type;
- out->header.endian_detector = ENDIAN_LOCAL;
- out->seq = swab32 (in->seq);
- out->token_seq = swab32 (in->token_seq);
- out->aru = swab32 (in->aru);
- out->ring_id.rep.s_addr = in->ring_id.rep.s_addr;
- out->ring_id.seq = swab64 (in->ring_id.seq);
- out->fcc = swab32 (in->fcc);
- out->retrans_flg = swab32 (in->retrans_flg);
- out->rtr_list_entries = swab32 (in->rtr_list_entries);
- for (i = 0; i < out->rtr_list_entries; i++) {
- out->rtr_list[i].ring_id.rep.s_addr = in->rtr_list[i].ring_id.rep.s_addr;
- out->rtr_list[i].ring_id.seq = swab64 (in->rtr_list[i].ring_id.seq);
- out->rtr_list[i].seq = swab32 (in->rtr_list[i].seq);
- }
- }
- static void mcast_endian_convert (struct mcast *in, struct mcast *out)
- {
- out->header.type = in->header.type;
- out->header.endian_detector = ENDIAN_LOCAL;
- out->seq = swab32 (in->seq);
- out->ring_id.rep.s_addr = in->ring_id.rep.s_addr;
- out->ring_id.seq = swab64 (in->ring_id.seq);
- out->source = in->source;
- out->guarantee = in->guarantee;
- }
- static int message_handler_memb_join (
- struct totemsrp_instance *instance,
- struct sockaddr_in *system_from,
- struct iovec *iovec,
- int iov_len,
- int bytes_received,
- int endian_conversion_needed)
- {
- struct memb_join *memb_join;
- struct memb_join memb_join_convert;
- int gather_entered;
- if (endian_conversion_needed) {
- memb_join = &memb_join_convert;
- memb_join_endian_convert (iovec->iov_base, &memb_join_convert);
- } else {
- memb_join = (struct memb_join *)iovec->iov_base;
- }
- if (instance->token_ring_id_seq < memb_join->ring_seq) {
- instance->token_ring_id_seq = memb_join->ring_seq;
- }
- switch (instance->memb_state) {
- case MEMB_STATE_OPERATIONAL:
- gather_entered = memb_join_process (instance,
- memb_join, system_from);
- if (gather_entered == 0) {
- memb_state_gather_enter (instance);
- }
- break;
- case MEMB_STATE_GATHER:
- memb_join_process (instance, memb_join, system_from);
- break;
-
- case MEMB_STATE_COMMIT:
- if (memb_set_subset (&system_from->sin_addr,
- 1,
- instance->my_new_memb_list,
- instance->my_new_memb_entries) &&
- memb_join->ring_seq >= instance->my_ring_id.seq) {
- memb_join_process (instance, memb_join, system_from);
- memb_state_gather_enter (instance);
- }
- break;
- case MEMB_STATE_RECOVERY:
- if (memb_set_subset (&system_from->sin_addr,
- 1,
- instance->my_new_memb_list,
- instance->my_new_memb_entries) &&
- memb_join->ring_seq >= instance->my_ring_id.seq) {
- ring_state_restore (instance);
- memb_join_process (instance,memb_join,
- system_from);
- memb_state_gather_enter (instance);
- }
- break;
- }
- return (0);
- }
- static int message_handler_memb_commit_token (
- struct totemsrp_instance *instance,
- struct sockaddr_in *system_from,
- struct iovec *iovec,
- int iov_len,
- int bytes_received,
- int endian_conversion_needed)
- {
- struct memb_commit_token memb_commit_token_convert;
- struct memb_commit_token *memb_commit_token;
- struct in_addr sub[PROCESSOR_COUNT_MAX];
- int sub_entries;
-
- if (endian_conversion_needed) {
- memb_commit_token = &memb_commit_token_convert;
- memb_commit_token_endian_convert (iovec->iov_base, memb_commit_token);
- } else {
- memb_commit_token = (struct memb_commit_token *)iovec->iov_base;
- }
-
- /* TODO do we need to check for a duplicate token?
- if (memb_commit_token->token_seq > 0 &&
- instance->my_token_seq >= memb_commit_token->token_seq) {
- printf ("already received commit token %d %d\n",
- memb_commit_token->token_seq, instance->my_token_seq);
- return (0);
- }
- */
- #ifdef RANDOM_DROP
- if (random()%100 < 10) {
- return (0);
- }
- #endif
- switch (instance->memb_state) {
- case MEMB_STATE_OPERATIONAL:
- /* discard token */
- break;
- case MEMB_STATE_GATHER:
- memb_set_subtract (sub, &sub_entries,
- instance->my_proc_list, instance->my_proc_list_entries,
- instance->my_failed_list, instance->my_failed_list_entries);
-
- if (memb_set_equal (memb_commit_token->addr,
- memb_commit_token->addr_entries,
- sub,
- sub_entries) &&
- memb_commit_token->ring_id.seq > instance->my_ring_id.seq) {
- memb_state_commit_enter (instance, memb_commit_token);
- }
- break;
- case MEMB_STATE_COMMIT:
- if (memcmp (&memb_commit_token->ring_id, &instance->my_ring_id,
- sizeof (struct memb_ring_id)) == 0) {
- // if (memb_commit_token->ring_id.seq == instance->my_ring_id.seq) {
- memb_state_recovery_enter (instance, memb_commit_token);
- }
- break;
- case MEMB_STATE_RECOVERY:
- instance->totemsrp_log_printf (instance->totemsrp_log_level_notice,
- "Sending initial ORF token\n");
- if (instance->my_id.sin_addr.s_addr == instance->my_ring_id.rep.s_addr) {
- // TODO convert instead of initiate
- orf_token_send_initial (instance);
- reset_token_timeout (instance); // REVIEWED
- reset_token_retransmit_timeout (instance); // REVIEWED
- }
- break;
- }
- return (0);
- }
- static int message_handler_token_hold_cancel (
- struct totemsrp_instance *instance,
- struct sockaddr_in *system_from,
- struct iovec *iovec,
- int iov_len,
- int bytes_received,
- int endian_conversion_needed)
- {
- struct token_hold_cancel *token_hold_cancel = (struct token_hold_cancel *)iovec->iov_base;
- if (memcmp (&token_hold_cancel->ring_id, &instance->my_ring_id,
- sizeof (struct memb_ring_id)) == 0) {
- instance->my_seq_unchanged = 0;
- if (instance->my_ring_id.rep.s_addr == instance->my_id.sin_addr.s_addr) {
- timer_function_token_retransmit_timeout (instance);
- }
- }
- return (0);
- }
- static int recv_handler (
- poll_handle handle,
- int fd,
- int revents,
- void *data,
- unsigned int *prio)
- {
- struct totemsrp_instance *instance = (struct totemsrp_instance *)data;
- struct msghdr msg_recv;
- struct message_header *message_header;
- struct sockaddr_in system_from;
- int bytes_received;
- int res = 0;
- *prio = UINT_MAX;
- /*
- * Receive datagram
- */
- msg_recv.msg_name = &system_from;
- msg_recv.msg_namelen = sizeof (struct sockaddr_in);
- msg_recv.msg_iov = &instance->totemsrp_iov_recv;
- msg_recv.msg_iovlen = 1;
- msg_recv.msg_control = 0;
- msg_recv.msg_controllen = 0;
- msg_recv.msg_flags = 0;
- bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
- if (bytes_received == -1) {
- return (0);
- } else {
- instance->stats_recv += bytes_received;
- }
- if (bytes_received < sizeof (struct message_header)) {
- instance->totemsrp_log_printf (instance->totemsrp_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
- return (0);
- }
- message_header = (struct message_header *)msg_recv.msg_iov->iov_base;
- /*
- * Authenticate and if authenticated, decrypt datagram
- */
- instance->totemsrp_iov_recv.iov_len = bytes_received;
- res = authenticate_and_decrypt (instance, &instance->totemsrp_iov_recv);
- instance->log_digest = 0;
- if (res == -1) {
- instance->totemsrp_iov_recv.iov_len = PACKET_SIZE_MAX;
- return 0;
- }
- if (instance->stats_tv_start.tv_usec == 0) {
- gettimeofday (&instance->stats_tv_start, NULL);
- }
- /*
- * Handle incoming message
- */
- message_header = (struct message_header *)msg_recv.msg_iov[0].iov_base;
- totemsrp_message_handlers.handler_functions[(int)message_header->type] (
- instance,
- &system_from,
- msg_recv.msg_iov,
- msg_recv.msg_iovlen,
- bytes_received,
- message_header->endian_detector != ENDIAN_LOCAL);
- instance->totemsrp_iov_recv.iov_len = PACKET_SIZE_MAX;
- return (0);
- }
|