evt.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365
  1. /*
  2. * Copyright (c) 2004-2006 Mark Haverkamp
  3. * Copyright (c) 2004-2006 Open Source Development Lab
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Open Source Development Lab nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #define DUMP_CHAN_INFO
  35. #define RECOVERY_EVENT_DEBUG LOG_LEVEL_DEBUG
  36. #define RECOVERY_DEBUG LOG_LEVEL_DEBUG
  37. #define CHAN_DEL_DEBUG LOG_LEVEL_DEBUG
  38. #define CHAN_OPEN_DEBUG LOG_LEVEL_DEBUG
  39. #define CHAN_UNLINK_DEBUG LOG_LEVEL_DEBUG
  40. #define REMOTE_OP_DEBUG LOG_LEVEL_DEBUG
  41. #define RETENTION_TIME_DEBUG LOG_LEVEL_DEBUG
  42. #include <sys/types.h>
  43. #include <stdlib.h>
  44. #include <errno.h>
  45. #include <sys/time.h>
  46. #include <sys/uio.h>
  47. #include <sys/socket.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <corosync/ipc_gen.h>
  51. #include <corosync/mar_gen.h>
  52. #include <corosync/hdb.h>
  53. #include <corosync/engine/swab.h>
  54. #include <corosync/engine/list.h>
  55. #include <corosync/engine/coroapi.h>
  56. #include <corosync/engine/logsys.h>
  57. #include <corosync/saAis.h>
  58. #include <corosync/lcr/lcr_comp.h>
  59. #include "../include/saEvt.h"
  60. #include "../include/ipc_evt.h"
  61. LOGSYS_DECLARE_SUBSYS ("EVT", LOG_INFO);
  62. /*
  63. * event instance structure. Contains information about the
  64. * active connection to the API library.
  65. *
  66. * esi_version: Version that the library is running.
  67. * esi_open_chans: list of open channels associated with this
  68. * instance. Used to clean up any data left
  69. * allocated when the finalize is done.
  70. * (event_svr_channel_open.eco_instance_entry)
  71. * esi_events: list of pending events to be delivered on this
  72. * instance (struct chan_event_list.cel_entry)
  73. * esi_queue_blocked: non-zero if the delivery queue got too full
  74. * and we're blocking new messages until we
  75. * drain some of the queued messages.
  76. * esi_nevents: Number of events in events lists to be sent.
  77. * esi_hdb: Handle data base for open channels on this
  78. * instance. Used for a quick lookup of
  79. * open channel data from a lib api message.
  80. */
  81. struct libevt_pd {
  82. SaVersionT esi_version;
  83. struct list_head esi_open_chans;
  84. struct list_head esi_events[SA_EVT_LOWEST_PRIORITY+1];
  85. int esi_nevents;
  86. int esi_queue_blocked;
  87. struct hdb_handle_database esi_hdb;
  88. };
  89. enum evt_message_req_types {
  90. MESSAGE_REQ_EXEC_EVT_EVENTDATA = 0,
  91. MESSAGE_REQ_EXEC_EVT_CHANCMD = 1,
  92. MESSAGE_REQ_EXEC_EVT_RECOVERY_EVENTDATA = 2
  93. };
  94. static void lib_evt_open_channel(void *conn, void *message);
  95. static void lib_evt_open_channel_async(void *conn, void *message);
  96. static void lib_evt_close_channel(void *conn, void *message);
  97. static void lib_evt_unlink_channel(void *conn, void *message);
  98. static void lib_evt_event_subscribe(void *conn, void *message);
  99. static void lib_evt_event_unsubscribe(void *conn, void *message);
  100. static void lib_evt_event_publish(void *conn, void *message);
  101. static void lib_evt_event_clear_retentiontime(void *conn, void *message);
  102. static void lib_evt_event_data_get(void *conn, void *message);
  103. static void evt_conf_change(
  104. enum totem_configuration_type configuration_type,
  105. unsigned int *member_list, int member_list_entries,
  106. unsigned int *left_list, int left_list_entries,
  107. unsigned int *joined_list, int joined_list_entries,
  108. struct memb_ring_id *ring_id);
  109. static int evt_lib_init(void *conn);
  110. static int evt_lib_exit(void *conn);
  111. static int evt_exec_init(struct corosync_api_v1 *);
  112. /*
  113. * Recovery sync functions
  114. */
  115. static void evt_sync_init(void);
  116. static int evt_sync_process(void);
  117. static void evt_sync_activate(void);
  118. static void evt_sync_abort(void);
  119. static void convert_event(void *msg);
  120. static void convert_chan_packet(void *msg);
  121. struct corosync_api_v1 *api;
  122. static struct corosync_lib_handler evt_lib_engine[] = {
  123. {
  124. .lib_handler_fn = lib_evt_open_channel,
  125. .response_size = sizeof(struct res_evt_channel_open),
  126. .response_id = MESSAGE_RES_EVT_OPEN_CHANNEL,
  127. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED
  128. },
  129. {
  130. .lib_handler_fn = lib_evt_open_channel_async,
  131. .response_size = sizeof(struct res_evt_channel_open),
  132. .response_id = MESSAGE_RES_EVT_OPEN_CHANNEL,
  133. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED
  134. },
  135. {
  136. .lib_handler_fn = lib_evt_close_channel,
  137. .response_size = sizeof(struct res_evt_channel_close),
  138. .response_id = MESSAGE_RES_EVT_CLOSE_CHANNEL,
  139. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED
  140. },
  141. {
  142. .lib_handler_fn = lib_evt_unlink_channel,
  143. .response_size = sizeof(struct res_evt_channel_unlink),
  144. .response_id = MESSAGE_RES_EVT_UNLINK_CHANNEL,
  145. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED
  146. },
  147. {
  148. .lib_handler_fn = lib_evt_event_subscribe,
  149. .response_size = sizeof(struct res_evt_event_subscribe),
  150. .response_id = MESSAGE_RES_EVT_SUBSCRIBE,
  151. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  152. },
  153. {
  154. .lib_handler_fn = lib_evt_event_unsubscribe,
  155. .response_size = sizeof(struct res_evt_event_unsubscribe),
  156. .response_id = MESSAGE_RES_EVT_UNSUBSCRIBE,
  157. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  158. },
  159. {
  160. .lib_handler_fn = lib_evt_event_publish,
  161. .response_size = sizeof(struct res_evt_event_publish),
  162. .response_id = MESSAGE_RES_EVT_PUBLISH,
  163. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED
  164. },
  165. {
  166. .lib_handler_fn = lib_evt_event_clear_retentiontime,
  167. .response_size = sizeof(struct res_evt_event_clear_retentiontime),
  168. .response_id = MESSAGE_RES_EVT_CLEAR_RETENTIONTIME,
  169. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED
  170. },
  171. {
  172. .lib_handler_fn = lib_evt_event_data_get,
  173. .response_size = sizeof(struct lib_event_data),
  174. .response_id = MESSAGE_RES_EVT_EVENT_DATA,
  175. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  176. },
  177. };
  178. static void evt_remote_evt(void *msg, unsigned int nodeid);
  179. static void evt_remote_recovery_evt(void *msg, unsigned int nodeid);
  180. static void evt_remote_chan_op(void *msg, unsigned int nodeid);
  181. static struct corosync_exec_handler evt_exec_engine[] = {
  182. {
  183. .exec_handler_fn = evt_remote_evt,
  184. .exec_endian_convert_fn = convert_event
  185. },
  186. {
  187. .exec_handler_fn = evt_remote_chan_op,
  188. .exec_endian_convert_fn = convert_chan_packet
  189. },
  190. {
  191. .exec_handler_fn = evt_remote_recovery_evt,
  192. .exec_endian_convert_fn = convert_event
  193. }
  194. };
  195. struct corosync_service_engine evt_service_engine = {
  196. .name = "openais event service B.01.01",
  197. .id = EVT_SERVICE,
  198. .private_data_size = sizeof (struct libevt_pd),
  199. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED,
  200. .lib_init_fn = evt_lib_init,
  201. .lib_exit_fn = evt_lib_exit,
  202. .lib_engine = evt_lib_engine,
  203. .lib_engine_count = sizeof(evt_lib_engine) / sizeof(struct corosync_lib_handler),
  204. .exec_init_fn = evt_exec_init,
  205. .exec_engine = evt_exec_engine,
  206. .exec_engine_count = sizeof(evt_exec_engine) / sizeof(struct corosync_exec_handler),
  207. .exec_dump_fn = NULL,
  208. .confchg_fn = evt_conf_change,
  209. .sync_init = evt_sync_init,
  210. .sync_process = evt_sync_process,
  211. .sync_activate = evt_sync_activate,
  212. .sync_abort = evt_sync_abort
  213. };
  214. static struct corosync_service_engine *evt_get_engine_ver0(void);
  215. static struct corosync_service_engine_iface_ver0 evt_service_engine_iface = {
  216. .corosync_get_service_engine_ver0 = evt_get_engine_ver0
  217. };
  218. static struct lcr_iface openais_evt_ver0[1] = {
  219. {
  220. .name = "openais_evt",
  221. .version = 0,
  222. .versions_replace = 0,
  223. .versions_replace_count = 0,
  224. .dependencies = 0,
  225. .dependency_count = 0,
  226. .constructor = NULL,
  227. .destructor = NULL,
  228. .interfaces = NULL,
  229. }
  230. };
  231. static struct lcr_comp evt_comp_ver0 = {
  232. .iface_count = 1,
  233. .ifaces = openais_evt_ver0
  234. };
  235. static struct corosync_service_engine *evt_get_engine_ver0(void)
  236. {
  237. return (&evt_service_engine);
  238. }
  239. __attribute__ ((constructor)) static void evt_comp_register (void) {
  240. lcr_interfaces_set (&openais_evt_ver0[0], &evt_service_engine_iface);
  241. lcr_component_register (&evt_comp_ver0);
  242. }
  243. /*
  244. * MESSAGE_REQ_EXEC_EVT_CHANCMD
  245. *
  246. * Used for various event related operations.
  247. *
  248. */
  249. enum evt_chan_ops {
  250. EVT_OPEN_CHAN_OP, /* chc_chan */
  251. EVT_CLOSE_CHAN_OP, /* chc_close_unlink_chan */
  252. EVT_UNLINK_CHAN_OP, /* chc_close_unlink_chan */
  253. EVT_CLEAR_RET_OP, /* chc_event_id */
  254. EVT_SET_ID_OP, /* chc_set_id */
  255. EVT_CONF_DONE, /* no data used */
  256. EVT_OPEN_COUNT, /* chc_set_opens */
  257. EVT_OPEN_COUNT_DONE /* no data used */
  258. };
  259. /*
  260. * Used during recovery to set the next issued event ID
  261. * based on the highest ID seen by any of the members
  262. */
  263. struct evt_set_id {
  264. mar_uint32_t chc_nodeid __attribute__((aligned(8)));
  265. mar_uint64_t chc_last_id __attribute__((aligned(8)));
  266. };
  267. /*
  268. * For set open count used during recovery to syncronize all nodes
  269. *
  270. * chc_chan_name: Channel name.
  271. * chc_open_count: number of local opens of this channel.
  272. */
  273. struct evt_set_opens {
  274. mar_name_t chc_chan_name __attribute__((aligned(8)));
  275. mar_uint32_t chc_open_count __attribute__((aligned(8)));
  276. };
  277. /*
  278. * Used to communicate channel to close or unlink.
  279. */
  280. #define EVT_CHAN_ACTIVE 0
  281. struct evt_close_unlink_chan {
  282. mar_name_t chcu_name __attribute__((aligned(8)));
  283. mar_uint64_t chcu_unlink_id __attribute__((aligned(8)));
  284. };
  285. struct open_chan_req {
  286. mar_name_t ocr_name __attribute__((aligned(8)));
  287. mar_uint64_t ocr_serial_no __attribute__((aligned(8)));
  288. };
  289. /*
  290. * Sent via MESSAGE_REQ_EXEC_EVT_CHANCMD
  291. *
  292. * chc_head: Request head
  293. * chc_op: Channel operation (open, close, clear retentiontime)
  294. * u: union of operation options.
  295. */
  296. struct req_evt_chan_command {
  297. mar_req_header_t chc_head __attribute__((aligned(8)));
  298. mar_uint32_t chc_op __attribute__((aligned(8)));
  299. union {
  300. struct open_chan_req chc_chan __attribute__((aligned(8)));
  301. mar_evteventid_t chc_event_id __attribute__((aligned(8)));
  302. struct evt_set_id chc_set_id __attribute__((aligned(8)));
  303. struct evt_set_opens chc_set_opens __attribute__((aligned(8)));
  304. struct evt_close_unlink_chan chcu __attribute__((aligned(8)));
  305. } u;
  306. };
  307. /*
  308. * list of all retained events
  309. * struct event_data
  310. */
  311. static DECLARE_LIST_INIT(retained_list);
  312. /*
  313. * list of all event channel information
  314. * struct event_svr_channel_instance
  315. */
  316. static DECLARE_LIST_INIT(esc_head);
  317. /*
  318. * list of all unlinked event channel information
  319. * struct event_svr_channel_instance
  320. */
  321. static DECLARE_LIST_INIT(esc_unlinked_head);
  322. /*
  323. * Track the state of event service recovery.
  324. *
  325. * evt_recovery_complete: Normal operational mode
  326. *
  327. * evt_send_event_id: Node is sending known last
  328. * event IDs.
  329. *
  330. * evt_send_open_count: Node is sending its open
  331. * Channel information.
  332. *
  333. * evt_wait_open_count_done: Node is done sending open channel data and
  334. * is waiting for the other nodes to finish.
  335. *
  336. * evt_send_retained_events: Node is sending retained event data.
  337. *
  338. * evt_send_retained_events_done: Node is sending done message.
  339. *
  340. * evt_wait_send_retained_events: Node is waiting for other nodes to
  341. * finish sending retained event data.
  342. */
  343. enum recovery_phases {
  344. evt_recovery_complete,
  345. evt_send_event_id,
  346. evt_send_open_count,
  347. evt_wait_open_count_done,
  348. evt_send_retained_events,
  349. evt_send_retained_events_done,
  350. evt_wait_send_retained_events
  351. };
  352. /*
  353. * Global varaibles used by the event service
  354. *
  355. * base_id_top: upper bits of next event ID to assign
  356. * base_id: Lower bits of Next event ID to assign
  357. * my_node_id: My cluster node id
  358. * checked_in: keep track during config change.
  359. * recovery_node: True if we're the recovery node. i.e. the
  360. * node that sends the retained events.
  361. * next_retained: pointer to next retained message to send
  362. * during recovery.
  363. * next_chan: pointer to next channel to send during recovery.
  364. * recovery_phase: Indicates what recovery is taking place.
  365. * left_member_count: How many left this configuration.
  366. * left_member_list: Members that left this config
  367. * joined_member_count: How many joined this configuration.
  368. * joined_member_list: Members that joined this config
  369. * total_member_count: how many members in this cluster
  370. * current_member_list: Total membership this config
  371. * trans_member_count: Node count in transitional membership
  372. * trans_member_list: Total membership from the transitional membership
  373. * add_count: count of joined members used for sending event id
  374. * recovery data.
  375. * add_list: pointer to joined list used for sending event id
  376. * recovery data.
  377. * processed_open_counts: Flag used to coordinate clearing open
  378. * channel counts for config change recovery.
  379. *
  380. */
  381. #define BASE_ID_MASK 0xffffffffLL
  382. static mar_evteventid_t base_id = 1;
  383. static mar_evteventid_t base_id_top = 0;
  384. static mar_uint32_t my_node_id = 0;
  385. static int checked_in = 0;
  386. static int recovery_node = 0;
  387. static struct list_head *next_retained = 0;
  388. static struct list_head *next_chan = 0;
  389. static enum recovery_phases recovery_phase = evt_recovery_complete;
  390. static int left_member_count = 0;
  391. static unsigned int *left_member_list = 0;
  392. static int joined_member_count = 0;
  393. static unsigned int *joined_member_list = 0;
  394. static int total_member_count = 0;
  395. static unsigned int *current_member_list = 0;
  396. static int trans_member_count = 0;
  397. static unsigned int *trans_member_list = 0;
  398. static int add_count = 0;
  399. static unsigned int *add_list = 0;
  400. static int processed_open_counts = 0;
  401. /*
  402. * Structure to track pending channel open requests.
  403. * ocp_async: 1 for async open
  404. * ocp_invocation: invocation for async open
  405. * ocp_chan_name: requested channel
  406. * ocp_conn: conn for returning to the library.
  407. * ocp_open_flags: channel open flags
  408. * ocp_timer_handle: timer handle for sync open
  409. * ocp_serial_no: Identifier for the request
  410. * ocp_entry: list entry for pending open list.
  411. */
  412. struct open_chan_pending {
  413. int ocp_async;
  414. mar_invocation_t ocp_invocation;
  415. mar_name_t ocp_chan_name;
  416. void *ocp_conn;
  417. mar_evtchannelopenflags_t ocp_open_flag;
  418. corosync_timer_handle_t ocp_timer_handle;
  419. uint64_t ocp_c_handle;
  420. uint64_t ocp_serial_no;
  421. struct list_head ocp_entry;
  422. };
  423. static uint64_t open_serial_no = 0;
  424. /*
  425. * code to indicate that the open request has timed out. The
  426. * invocation data element is used for this code since it is
  427. * only used by the open async call which cannot have a timeout.
  428. */
  429. #define OPEN_TIMED_OUT 0x5a6b5a6b5a6b5a6bLLU
  430. /*
  431. * list of pending channel opens
  432. */
  433. static DECLARE_LIST_INIT(open_pending);
  434. static void chan_open_timeout(void *data);
  435. /*
  436. * Structure to track pending channel unlink requests.
  437. * ucp_unlink_id: unlink ID of unlinked channel.
  438. * ucp_conn: conn for returning to the library.
  439. * ucp_entry: list entry for pending unlink list.
  440. */
  441. struct unlink_chan_pending {
  442. uint64_t ucp_unlink_id;
  443. void *ucp_conn;
  444. struct list_head ucp_entry;
  445. };
  446. /*
  447. * list of pending unlink requests
  448. */
  449. static DECLARE_LIST_INIT(unlink_pending);
  450. /*
  451. * Structure to track pending retention time clear requests.
  452. * rtc_event_id: event ID to clear.
  453. * rtc_conn: conn for returning to the library.
  454. * rtc_entry: list entry for pending clear list.
  455. */
  456. struct retention_time_clear_pending {
  457. mar_evteventid_t rtc_event_id;
  458. void *rtc_conn;
  459. struct list_head rtc_entry;
  460. };
  461. /*
  462. * list of pending clear requests.
  463. */
  464. static DECLARE_LIST_INIT(clear_pending);
  465. /*
  466. * Next unlink ID
  467. */
  468. static uint64_t base_unlink_id = 0;
  469. inline uint64_t
  470. next_chan_unlink_id()
  471. {
  472. uint64_t uid = my_node_id;
  473. uid = (uid << 32ULL) | base_unlink_id;
  474. base_unlink_id = (base_unlink_id + 1ULL) & BASE_ID_MASK;
  475. return uid;
  476. }
  477. #define min(a,b) ((a) < (b) ? (a) : (b))
  478. /*
  479. * Throttle event delivery to applications to keep
  480. * the exec from using too much memory if the app is
  481. * slow to process its events.
  482. */
  483. #define MAX_EVT_DELIVERY_QUEUE 1000
  484. #define MIN_EVT_QUEUE_RESUME (MAX_EVT_DELIVERY_QUEUE / 2)
  485. static unsigned int evt_delivery_queue_size = MAX_EVT_DELIVERY_QUEUE;
  486. static unsigned int evt_delivery_queue_resume = MIN_EVT_QUEUE_RESUME;
  487. #define LOST_PUB "EVENT_SERIVCE"
  488. #define LOST_CHAN "LOST EVENT"
  489. /*
  490. * Event to send when the delivery queue gets too full
  491. */
  492. char lost_evt[] = SA_EVT_LOST_EVENT;
  493. static int dropped_event_size;
  494. static struct event_data *dropped_event;
  495. struct evt_pattern {
  496. mar_evt_event_pattern_t pat;
  497. char str[sizeof(lost_evt)];
  498. };
  499. static struct evt_pattern dropped_pattern = {
  500. .pat = {
  501. sizeof(lost_evt),
  502. sizeof(lost_evt),
  503. (SaUint8T *) &dropped_pattern.str[0]},
  504. .str = {SA_EVT_LOST_EVENT}
  505. };
  506. mar_name_t lost_chan = {
  507. .value = LOST_CHAN,
  508. .length = sizeof(LOST_CHAN)
  509. };
  510. mar_name_t dropped_publisher = {
  511. .value = LOST_PUB,
  512. .length = sizeof(LOST_PUB)
  513. };
  514. struct event_svr_channel_open;
  515. struct event_svr_channel_subscr;
  516. struct open_count {
  517. mar_uint32_t oc_node_id;
  518. int32_t oc_open_count;
  519. };
  520. /*
  521. * Structure to contain global channel releated information
  522. *
  523. * esc_channel_name: The name of this channel.
  524. * esc_total_opens: The total number of opens on this channel including
  525. * other nodes.
  526. * esc_local_opens: The number of opens on this channel for this node.
  527. * esc_oc_size: The total number of entries in esc_node_opens;
  528. * esc_node_opens: list of node IDs and how many opens are associated.
  529. * esc_retained_count: How many retained events for this channel
  530. * esc_open_chans: list of opens of this channel.
  531. * (event_svr_channel_open.eco_entry)
  532. * esc_entry: links to other channels. (used by esc_head)
  533. * esc_unlink_id: If non-zero, then the channel has been marked
  534. * for unlink. This unlink ID is used to
  535. * mark events still associated with current openings
  536. * so they get delivered to the proper recipients.
  537. */
  538. struct event_svr_channel_instance {
  539. mar_name_t esc_channel_name;
  540. int32_t esc_total_opens;
  541. int32_t esc_local_opens;
  542. uint32_t esc_oc_size;
  543. struct open_count *esc_node_opens;
  544. uint32_t esc_retained_count;
  545. struct list_head esc_open_chans;
  546. struct list_head esc_entry;
  547. uint64_t esc_unlink_id;
  548. };
  549. /*
  550. * Has the event data in the correct format to send to the library API
  551. * with aditional field for accounting.
  552. *
  553. * ed_ref_count: how many other strutures are referencing.
  554. * ed_retained: retained event list.
  555. * ed_timer_handle: Timer handle for retained event expiration.
  556. * ed_delivered: arrays of open channel pointers that this event
  557. * has been delivered to. (only used for events
  558. * with a retention time).
  559. * ed_delivered_count: Number of entries available in ed_delivered.
  560. * ed_delivered_next: Next free spot in ed_delivered
  561. * ed_my_chan: pointer to the global channel instance associated
  562. * with this event.
  563. * ed_event: The event data formatted to be ready to send.
  564. */
  565. struct event_data {
  566. uint32_t ed_ref_count;
  567. struct list_head ed_retained;
  568. corosync_timer_handle_t ed_timer_handle;
  569. struct event_svr_channel_open **ed_delivered;
  570. uint32_t ed_delivered_count;
  571. uint32_t ed_delivered_next;
  572. struct event_svr_channel_instance *ed_my_chan;
  573. struct lib_event_data ed_event;
  574. };
  575. /*
  576. * Contains a list of pending events to be delivered to a subscribed
  577. * application.
  578. *
  579. * cel_chan_handle: associated library channel handle
  580. * cel_sub_id: associated library subscription ID
  581. * cel_event: event structure to deliver.
  582. * cel_entry: list of pending events
  583. * (struct event_server_instance.esi_events)
  584. */
  585. struct chan_event_list {
  586. uint64_t cel_chan_handle;
  587. uint32_t cel_sub_id;
  588. struct event_data* cel_event;
  589. struct list_head cel_entry;
  590. };
  591. /*
  592. * Contains information about each open for a given channel
  593. *
  594. * eco_flags: How the channel was opened.
  595. * eco_lib_handle: channel handle in the app. Used for event delivery.
  596. * eco_my_handle: the handle used to access this data structure.
  597. * eco_channel: Pointer to global channel info.
  598. * eco_entry: links to other opeinings of this channel.
  599. * eco_instance_entry: links to other channel opeinings for the
  600. * associated server instance.
  601. * eco_subscr: head of list of sbuscriptions for this channel open.
  602. * (event_svr_channel_subscr.ecs_entry)
  603. * eco_conn: refrence to EvtInitialize who owns this open.
  604. */
  605. struct event_svr_channel_open {
  606. uint8_t eco_flags;
  607. uint64_t eco_lib_handle;
  608. uint32_t eco_my_handle;
  609. struct event_svr_channel_instance *eco_channel;
  610. struct list_head eco_entry;
  611. struct list_head eco_instance_entry;
  612. struct list_head eco_subscr;
  613. void *eco_conn;
  614. };
  615. /*
  616. * Contains information about each channel subscription
  617. *
  618. * ecs_open_chan: Link to our open channel.
  619. * ecs_sub_id: Subscription ID.
  620. * ecs_filter_count: number of filters in ecs_filters
  621. * ecs_filters: filters for determining event delivery.
  622. * ecs_entry: Links to other subscriptions to this channel opening.
  623. */
  624. struct event_svr_channel_subscr {
  625. struct event_svr_channel_open *ecs_open_chan;
  626. uint32_t ecs_sub_id;
  627. mar_evt_event_filter_array_t *ecs_filters;
  628. struct list_head ecs_entry;
  629. };
  630. /*
  631. * Member node data
  632. * mn_node_info: cluster node info from membership
  633. * mn_last_msg_id: last seen message ID for this node
  634. * mn_started: Indicates that event service has started
  635. * on this node.
  636. * mn_next: pointer to the next node in the hash chain.
  637. * mn_entry: List of all nodes.
  638. */
  639. struct member_node_data {
  640. unsigned int mn_nodeid;
  641. SaClmClusterNodeT mn_node_info;
  642. mar_evteventid_t mn_last_msg_id;
  643. mar_uint32_t mn_started;
  644. struct member_node_data *mn_next;
  645. struct list_head mn_entry;
  646. };
  647. DECLARE_LIST_INIT(mnd);
  648. /*
  649. * Take the filters we received from the application via the library and
  650. * make them into a real mar_evt_event_filter_array_t
  651. */
  652. static SaAisErrorT evtfilt_to_aisfilt(struct req_evt_event_subscribe *req,
  653. mar_evt_event_filter_array_t **evtfilters)
  654. {
  655. mar_evt_event_filter_array_t *filta =
  656. (mar_evt_event_filter_array_t *)req->ics_filter_data;
  657. mar_evt_event_filter_array_t *filters;
  658. mar_evt_event_filter_t *filt = (void *)filta + sizeof(mar_evt_event_filter_array_t);
  659. SaUint8T *str = (void *)filta + sizeof(mar_evt_event_filter_array_t) +
  660. (sizeof(mar_evt_event_filter_t) * filta->filters_number);
  661. int i;
  662. int j;
  663. filters = malloc(sizeof(mar_evt_event_filter_array_t));
  664. if (!filters) {
  665. return SA_AIS_ERR_NO_MEMORY;
  666. }
  667. filters->filters_number = filta->filters_number;
  668. filters->filters = malloc(sizeof(mar_evt_event_filter_t) *
  669. filta->filters_number);
  670. if (!filters->filters) {
  671. free(filters);
  672. return SA_AIS_ERR_NO_MEMORY;
  673. }
  674. for (i = 0; i < filters->filters_number; i++) {
  675. filters->filters[i].filter.pattern =
  676. malloc(filt[i].filter.pattern_size);
  677. if (!filters->filters[i].filter.pattern) {
  678. for (j = 0; j < i; j++) {
  679. free(filters->filters[j].filter.pattern);
  680. }
  681. free(filters->filters);
  682. free(filters);
  683. return SA_AIS_ERR_NO_MEMORY;
  684. }
  685. filters->filters[i].filter.pattern_size =
  686. filt[i].filter.pattern_size;
  687. filters->filters[i].filter.allocated_size =
  688. filt[i].filter.pattern_size;
  689. memcpy(filters->filters[i].filter.pattern,
  690. str, filters->filters[i].filter.pattern_size);
  691. filters->filters[i].filter_type = filt[i].filter_type;
  692. str += filters->filters[i].filter.pattern_size;
  693. }
  694. *evtfilters = filters;
  695. return SA_AIS_OK;
  696. }
  697. SaTimeT clust_time_now(void)
  698. {
  699. struct timeval tv;
  700. SaTimeT time_now;
  701. if (gettimeofday(&tv, 0)) {
  702. return 0ULL;
  703. }
  704. time_now = (SaTimeT)(tv.tv_sec) * 1000000000ULL;
  705. time_now += (SaTimeT)(tv.tv_usec) * 1000ULL;
  706. return time_now;
  707. }
  708. static void free_filters(mar_evt_event_filter_array_t *fp)
  709. {
  710. int i;
  711. for (i = 0; i < fp->filters_number; i++) {
  712. free(fp->filters[i].filter.pattern);
  713. }
  714. free(fp->filters);
  715. free(fp);
  716. }
  717. /*
  718. * Look up a channel in the global channel list
  719. */
  720. static struct event_svr_channel_instance *
  721. find_channel(mar_name_t *chan_name, uint64_t unlink_id)
  722. {
  723. struct list_head *l, *head;
  724. struct event_svr_channel_instance *eci;
  725. /*
  726. * choose which list to look through
  727. */
  728. if (unlink_id == EVT_CHAN_ACTIVE) {
  729. head = &esc_head;
  730. } else {
  731. head = &esc_unlinked_head;
  732. }
  733. for (l = head->next; l != head; l = l->next) {
  734. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  735. if (!mar_name_match(chan_name, &eci->esc_channel_name)) {
  736. continue;
  737. } else if (unlink_id != eci->esc_unlink_id) {
  738. continue;
  739. }
  740. return eci;
  741. }
  742. return 0;
  743. }
  744. /*
  745. * Find the last unlinked version of a channel.
  746. */
  747. static struct event_svr_channel_instance *
  748. find_last_unlinked_channel(mar_name_t *chan_name)
  749. {
  750. struct list_head *l;
  751. struct event_svr_channel_instance *eci;
  752. /*
  753. * unlinked channels are added to the head of the list
  754. * so the first one we see is the last one added.
  755. */
  756. for (l = esc_unlinked_head.next; l != &esc_unlinked_head; l = l->next) {
  757. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  758. if (!mar_name_match(chan_name, &eci->esc_channel_name)) {
  759. continue;
  760. }
  761. }
  762. return 0;
  763. }
  764. /*
  765. * Create and initialize a channel instance structure
  766. */
  767. static struct event_svr_channel_instance *create_channel(mar_name_t *cn)
  768. {
  769. struct event_svr_channel_instance *eci;
  770. eci = (struct event_svr_channel_instance *) malloc(sizeof(*eci));
  771. if (!eci) {
  772. return (eci);
  773. }
  774. memset(eci, 0, sizeof(*eci));
  775. list_init(&eci->esc_entry);
  776. list_init(&eci->esc_open_chans);
  777. eci->esc_oc_size = total_member_count;
  778. eci->esc_node_opens =
  779. malloc(sizeof(struct open_count) * total_member_count);
  780. if (!eci->esc_node_opens) {
  781. free(eci);
  782. return 0;
  783. }
  784. memset(eci->esc_node_opens, 0,
  785. sizeof(struct open_count) * total_member_count);
  786. eci->esc_channel_name = *cn;
  787. eci->esc_channel_name.value[eci->esc_channel_name.length] = '\0';
  788. list_add(&eci->esc_entry, &esc_head);
  789. return eci;
  790. }
  791. /*
  792. * Make sure that the list of nodes is large enough to hold the whole
  793. * membership
  794. */
  795. static int check_open_size(struct event_svr_channel_instance *eci)
  796. {
  797. struct open_count *esc_node_opens_tmp;
  798. if (total_member_count > eci->esc_oc_size) {
  799. esc_node_opens_tmp = realloc (eci->esc_node_opens,
  800. sizeof(struct open_count) * total_member_count);
  801. if (esc_node_opens_tmp == NULL) {
  802. log_printf(LOG_LEVEL_WARNING,
  803. "Memory error realloc of node list\n");
  804. return -1;
  805. }
  806. eci->esc_node_opens = esc_node_opens_tmp;
  807. memset(&eci->esc_node_opens[eci->esc_oc_size], 0,
  808. sizeof(struct open_count) *
  809. (total_member_count - eci->esc_oc_size));
  810. eci->esc_oc_size = total_member_count;
  811. }
  812. return 0;
  813. }
  814. /*
  815. * Find the specified node ID in the node list of the channel.
  816. * If it's not in the list, add it.
  817. */
  818. static struct open_count* find_open_count(
  819. struct event_svr_channel_instance *eci,
  820. mar_uint32_t node_id)
  821. {
  822. int i;
  823. for (i = 0; i < eci->esc_oc_size; i++) {
  824. if (eci->esc_node_opens[i].oc_node_id == 0) {
  825. eci->esc_node_opens[i].oc_node_id = node_id;
  826. eci->esc_node_opens[i].oc_open_count = 0;
  827. }
  828. if (eci->esc_node_opens[i].oc_node_id == node_id) {
  829. return &eci->esc_node_opens[i];
  830. }
  831. }
  832. log_printf(LOG_LEVEL_DEBUG,
  833. "find_open_count: node id %s not found\n",
  834. api->totem_ifaces_print (node_id));
  835. return 0;
  836. }
  837. static void dump_chan_opens(struct event_svr_channel_instance *eci)
  838. {
  839. int i;
  840. log_printf(LOG_LEVEL_NOTICE,
  841. "Channel %s, total %d, local %d\n",
  842. eci->esc_channel_name.value,
  843. eci->esc_total_opens,
  844. eci->esc_local_opens);
  845. for (i = 0; i < eci->esc_oc_size; i++) {
  846. if (eci->esc_node_opens[i].oc_node_id == 0) {
  847. break;
  848. }
  849. log_printf(LOG_LEVEL_NOTICE, "Node %s, count %d\n",
  850. api->totem_ifaces_print (eci->esc_node_opens[i].oc_node_id),
  851. eci->esc_node_opens[i].oc_open_count);
  852. }
  853. }
  854. #ifdef DUMP_CHAN_INFO
  855. /*
  856. * Scan the list of channels and dump the open count info.
  857. */
  858. static void dump_all_chans()
  859. {
  860. struct list_head *l;
  861. struct event_svr_channel_instance *eci;
  862. for (l = esc_head.next; l != &esc_head; l = l->next) {
  863. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  864. dump_chan_opens(eci);
  865. }
  866. }
  867. #endif
  868. /*
  869. * Scan the list of channels and zero out the open counts
  870. */
  871. static void zero_chan_open_counts()
  872. {
  873. struct list_head *l;
  874. struct event_svr_channel_instance *eci;
  875. int i;
  876. for (l = esc_head.next; l != &esc_head; l = l->next) {
  877. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  878. for (i = 0; i < eci->esc_oc_size; i++) {
  879. if (eci->esc_node_opens[i].oc_node_id == 0) {
  880. break;
  881. }
  882. eci->esc_node_opens[i].oc_open_count = 0;
  883. }
  884. eci->esc_total_opens = 0;
  885. }
  886. }
  887. /*
  888. * Replace the current open count for a node with the specified value.
  889. */
  890. static int set_open_count(struct event_svr_channel_instance *eci,
  891. mar_uint32_t node_id, uint32_t open_count)
  892. {
  893. struct open_count *oc;
  894. int i;
  895. if ((i = check_open_size(eci)) != 0) {
  896. return i;
  897. }
  898. oc = find_open_count(eci, node_id);
  899. if (oc) {
  900. log_printf(RECOVERY_DEBUG,
  901. "Set count: Chan %s for node %s, was %d, now %d\n",
  902. eci->esc_channel_name.value, api->totem_ifaces_print (node_id),
  903. oc->oc_open_count, open_count);
  904. eci->esc_total_opens -= oc->oc_open_count;
  905. eci->esc_total_opens += open_count;
  906. oc->oc_open_count = open_count;
  907. return 0;
  908. }
  909. return -1;
  910. }
  911. /*
  912. * Increment the open count for the specified node.
  913. */
  914. static int inc_open_count(struct event_svr_channel_instance *eci,
  915. mar_uint32_t node_id)
  916. {
  917. struct open_count *oc;
  918. int i;
  919. if ((i = check_open_size(eci)) != 0) {
  920. return i;
  921. }
  922. if (node_id == my_node_id) {
  923. eci->esc_local_opens++;
  924. }
  925. oc = find_open_count(eci, node_id);
  926. if (oc) {
  927. eci->esc_total_opens++;
  928. oc->oc_open_count++;
  929. return 0;
  930. }
  931. return -1;
  932. }
  933. /*
  934. * Decrement the open count for the specified node in the
  935. * specified channel.
  936. */
  937. static int dec_open_count(struct event_svr_channel_instance *eci,
  938. mar_uint32_t node_id)
  939. {
  940. struct open_count *oc;
  941. int i;
  942. if ((i = check_open_size(eci)) != 0) {
  943. return i;
  944. }
  945. if (node_id == my_node_id) {
  946. eci->esc_local_opens--;
  947. }
  948. oc = find_open_count(eci, node_id);
  949. if (oc) {
  950. eci->esc_total_opens--;
  951. oc->oc_open_count--;
  952. if ((eci->esc_total_opens < 0) || (oc->oc_open_count < 0)) {
  953. log_printf(LOG_LEVEL_ERROR, "Channel open decrement error\n");
  954. dump_chan_opens(eci);
  955. }
  956. return 0;
  957. }
  958. return -1;
  959. }
  960. /*
  961. * Remove a channel and free its memory if it's not in use anymore.
  962. */
  963. static void delete_channel(struct event_svr_channel_instance *eci)
  964. {
  965. log_printf(CHAN_DEL_DEBUG,
  966. "Called Delete channel %s t %d, l %d, r %d\n",
  967. eci->esc_channel_name.value,
  968. eci->esc_total_opens, eci->esc_local_opens,
  969. eci->esc_retained_count);
  970. /*
  971. * If no one has the channel open anywhere and there are no unexpired
  972. * retained events for this channel, and it has been marked for deletion
  973. * by an unlink, then it is OK to delete the data structure.
  974. */
  975. if ((eci->esc_retained_count == 0) && (eci->esc_total_opens == 0) &&
  976. (eci->esc_unlink_id != EVT_CHAN_ACTIVE)) {
  977. log_printf(CHAN_DEL_DEBUG, "Delete channel %s\n",
  978. eci->esc_channel_name.value);
  979. log_printf(CHAN_UNLINK_DEBUG, "Delete channel %s, unlink_id %0llx\n",
  980. eci->esc_channel_name.value, (unsigned long long)eci->esc_unlink_id);
  981. if (!list_empty(&eci->esc_open_chans)) {
  982. log_printf(LOG_LEVEL_NOTICE,
  983. "Last channel close request for %s (still open)\n",
  984. eci->esc_channel_name.value);
  985. dump_chan_opens(eci);
  986. return;
  987. }
  988. /*
  989. * adjust if we're sending open counts on a config change.
  990. */
  991. if ((recovery_phase != evt_recovery_complete) &&
  992. (&eci->esc_entry == next_chan)) {
  993. next_chan = eci->esc_entry.next;
  994. }
  995. list_del(&eci->esc_entry);
  996. if (eci->esc_node_opens) {
  997. free(eci->esc_node_opens);
  998. }
  999. free(eci);
  1000. }
  1001. }
  1002. /*
  1003. * Free up an event structure if it isn't being used anymore.
  1004. */
  1005. static void
  1006. free_event_data(struct event_data *edp)
  1007. {
  1008. if (--edp->ed_ref_count) {
  1009. return;
  1010. }
  1011. log_printf(LOG_LEVEL_DEBUG, "Freeing event ID: 0x%llx\n",
  1012. (unsigned long long)edp->ed_event.led_event_id);
  1013. if (edp->ed_delivered) {
  1014. free(edp->ed_delivered);
  1015. }
  1016. free(edp);
  1017. }
  1018. /*
  1019. * Mark a channel for deletion.
  1020. */
  1021. static void unlink_channel(struct event_svr_channel_instance *eci,
  1022. uint64_t unlink_id)
  1023. {
  1024. struct event_data *edp;
  1025. struct list_head *l, *nxt;
  1026. log_printf(CHAN_UNLINK_DEBUG, "Unlink request: %s, id 0x%llx\n",
  1027. eci->esc_channel_name.value, (unsigned long long)unlink_id);
  1028. /*
  1029. * The unlink ID is used to note that the channel has been marked
  1030. * for deletion and is a way to distinguish between multiple
  1031. * channels of the same name each marked for deletion.
  1032. */
  1033. eci->esc_unlink_id = unlink_id;
  1034. /*
  1035. * Move the unlinked channel to the unlinked list. This way
  1036. * we don't have to worry about filtering it out when we need to
  1037. * distribute retained events at recovery time.
  1038. */
  1039. list_del(&eci->esc_entry);
  1040. list_add(&eci->esc_entry, &esc_unlinked_head);
  1041. /*
  1042. * Scan the retained event list and remove any retained events.
  1043. * Since no new opens can occur there won't be any need of sending
  1044. * retained events on the channel.
  1045. */
  1046. for (l = retained_list.next; l != &retained_list; l = nxt) {
  1047. nxt = l->next;
  1048. edp = list_entry(l, struct event_data, ed_retained);
  1049. if ((edp->ed_my_chan == eci) &&
  1050. (edp->ed_event.led_chan_unlink_id == EVT_CHAN_ACTIVE)) {
  1051. api->timer_delete(edp->ed_timer_handle);
  1052. edp->ed_event.led_retention_time = 0;
  1053. list_del(&edp->ed_retained);
  1054. list_init(&edp->ed_retained);
  1055. edp->ed_my_chan->esc_retained_count--;
  1056. log_printf(CHAN_UNLINK_DEBUG,
  1057. "Unlink: Delete retained event id 0x%llx\n",
  1058. (unsigned long long)edp->ed_event.led_event_id);
  1059. free_event_data(edp);
  1060. }
  1061. }
  1062. delete_channel(eci);
  1063. }
  1064. /*
  1065. * Remove the specified node from the node list in this channel.
  1066. */
  1067. static int remove_open_count(
  1068. struct event_svr_channel_instance *eci,
  1069. mar_uint32_t node_id)
  1070. {
  1071. int i;
  1072. int j;
  1073. /*
  1074. * Find the node, remove it and re-pack the array.
  1075. */
  1076. for (i = 0; i < eci->esc_oc_size; i++) {
  1077. if (eci->esc_node_opens[i].oc_node_id == 0) {
  1078. break;
  1079. }
  1080. log_printf(RECOVERY_DEBUG, "roc: %s/%s, t %d, oc %d\n",
  1081. api->totem_ifaces_print (node_id),
  1082. api->totem_ifaces_print (eci->esc_node_opens[i].oc_node_id),
  1083. eci->esc_total_opens, eci->esc_node_opens[i].oc_open_count);
  1084. if (eci->esc_node_opens[i].oc_node_id == node_id) {
  1085. eci->esc_total_opens -= eci->esc_node_opens[i].oc_open_count;
  1086. for (j = i+1; j < eci->esc_oc_size; j++, i++) {
  1087. eci->esc_node_opens[i].oc_node_id =
  1088. eci->esc_node_opens[j].oc_node_id;
  1089. eci->esc_node_opens[i].oc_open_count =
  1090. eci->esc_node_opens[j].oc_open_count;
  1091. }
  1092. eci->esc_node_opens[eci->esc_oc_size-1].oc_node_id = 0;
  1093. eci->esc_node_opens[eci->esc_oc_size-1].oc_open_count = 0;
  1094. /*
  1095. * Remove the channel if it's not being used anymore
  1096. */
  1097. delete_channel(eci);
  1098. return 0;
  1099. }
  1100. }
  1101. return -1;
  1102. }
  1103. /*
  1104. * Send a request to open a channel to the rest of the cluster.
  1105. */
  1106. static SaAisErrorT evt_open_channel(mar_name_t *cn, SaUint8T flgs)
  1107. {
  1108. struct req_evt_chan_command cpkt;
  1109. struct event_svr_channel_instance *eci;
  1110. struct iovec chn_iovec;
  1111. int res;
  1112. SaAisErrorT ret;
  1113. ret = SA_AIS_OK;
  1114. eci = find_channel(cn, EVT_CHAN_ACTIVE);
  1115. /*
  1116. * If the create flag set, and it doesn't exist, we can make the channel.
  1117. * Otherwise, it's an error since we're notified of channels being created
  1118. * and opened.
  1119. */
  1120. if (!eci && !(flgs & SA_EVT_CHANNEL_CREATE)) {
  1121. ret = SA_AIS_ERR_NOT_EXIST;
  1122. goto chan_open_end;
  1123. }
  1124. /*
  1125. * create the channel packet to send. Tell the the cluster
  1126. * to create the channel.
  1127. */
  1128. memset(&cpkt, 0, sizeof(cpkt));
  1129. cpkt.chc_head.id =
  1130. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  1131. cpkt.chc_head.size = sizeof(cpkt);
  1132. cpkt.chc_op = EVT_OPEN_CHAN_OP;
  1133. cpkt.u.chc_chan.ocr_name = *cn;
  1134. cpkt.u.chc_chan.ocr_serial_no = ++open_serial_no;
  1135. chn_iovec.iov_base = (char *)&cpkt;
  1136. chn_iovec.iov_len = cpkt.chc_head.size;
  1137. log_printf(CHAN_OPEN_DEBUG, "evt_open_channel: Send open mcast\n");
  1138. res = api->totem_mcast (&chn_iovec, 1, TOTEM_AGREED);
  1139. log_printf(CHAN_OPEN_DEBUG, "evt_open_channel: Open mcast result: %d\n",
  1140. res);
  1141. if (res != 0) {
  1142. ret = SA_AIS_ERR_LIBRARY;
  1143. }
  1144. chan_open_end:
  1145. return ret;
  1146. }
  1147. /*
  1148. * Send a request to close a channel with the rest of the cluster.
  1149. */
  1150. static SaAisErrorT evt_close_channel(mar_name_t *cn, uint64_t unlink_id)
  1151. {
  1152. struct req_evt_chan_command cpkt;
  1153. struct iovec chn_iovec;
  1154. int res;
  1155. SaAisErrorT ret;
  1156. ret = SA_AIS_OK;
  1157. /*
  1158. * create the channel packet to send. Tell the the cluster
  1159. * to close the channel.
  1160. */
  1161. memset(&cpkt, 0, sizeof(cpkt));
  1162. cpkt.chc_head.id =
  1163. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  1164. cpkt.chc_head.size = sizeof(cpkt);
  1165. cpkt.chc_op = EVT_CLOSE_CHAN_OP;
  1166. cpkt.u.chcu.chcu_name = *cn;
  1167. cpkt.u.chcu.chcu_unlink_id = unlink_id;
  1168. chn_iovec.iov_base = (char *)&cpkt;
  1169. chn_iovec.iov_len = cpkt.chc_head.size;
  1170. res = api->totem_mcast (&chn_iovec, 1, TOTEM_AGREED);
  1171. if (res != 0) {
  1172. ret = SA_AIS_ERR_LIBRARY;
  1173. }
  1174. return ret;
  1175. }
  1176. /*
  1177. * Node data access functions. Used to keep track of event IDs
  1178. * delivery of messages.
  1179. *
  1180. * add_node: Add a new member node to our list.
  1181. * remove_node: Remove a node that left membership.
  1182. * find_node: Given the node ID return a pointer to node information.
  1183. *
  1184. */
  1185. #define NODE_HASH_SIZE 256
  1186. static struct member_node_data *nl[NODE_HASH_SIZE] = {0};
  1187. inline int
  1188. hash_sock_addr(unsigned int nodeid)
  1189. {
  1190. return nodeid & (NODE_HASH_SIZE - 1);
  1191. }
  1192. static struct member_node_data **lookup_node(unsigned int nodeid)
  1193. {
  1194. int index = hash_sock_addr(nodeid);
  1195. struct member_node_data **nlp;
  1196. nlp = &nl[index];
  1197. for (nlp = &nl[index]; *nlp; nlp = &((*nlp)->mn_next)) {
  1198. if ((*(nlp))->mn_nodeid == nodeid) {
  1199. break;
  1200. }
  1201. }
  1202. return nlp;
  1203. }
  1204. static struct member_node_data *
  1205. evt_find_node(unsigned int nodeid)
  1206. {
  1207. struct member_node_data **nlp;
  1208. nlp = lookup_node(nodeid);
  1209. if (!nlp) {
  1210. log_printf(LOG_LEVEL_DEBUG, "find_node: Got NULL nlp?\n");
  1211. return 0;
  1212. }
  1213. return *nlp;
  1214. }
  1215. static SaAisErrorT
  1216. evt_add_node(
  1217. unsigned int nodeid,
  1218. SaClmClusterNodeT *cn)
  1219. {
  1220. struct member_node_data **nlp;
  1221. struct member_node_data *nl;
  1222. SaAisErrorT err = SA_AIS_ERR_EXIST;
  1223. nlp = lookup_node(nodeid);
  1224. if (!nlp) {
  1225. log_printf(LOG_LEVEL_DEBUG, "add_node: Got NULL nlp?\n");
  1226. goto an_out;
  1227. }
  1228. if (*nlp) {
  1229. goto an_out;
  1230. }
  1231. *nlp = malloc(sizeof(struct member_node_data));
  1232. if (!(*nlp)) {
  1233. return SA_AIS_ERR_NO_MEMORY;
  1234. }
  1235. nl = *nlp;
  1236. if (nl) {
  1237. memset(nl, 0, sizeof(*nl));
  1238. err = SA_AIS_OK;
  1239. nl->mn_nodeid = nodeid;
  1240. nl->mn_started = 1;
  1241. }
  1242. list_init(&nl->mn_entry);
  1243. list_add(&nl->mn_entry, &mnd);
  1244. nl->mn_node_info = *cn;
  1245. an_out:
  1246. return err;
  1247. }
  1248. /*
  1249. * Find the oldest node in the membership. This is the one we choose to
  1250. * perform some cluster-wide functions like distributing retained events.
  1251. * We only check nodes that were in our transitional configuration. In this
  1252. * way there is a recovery node chosen for each original partition in case
  1253. * of a merge.
  1254. */
  1255. static struct member_node_data* oldest_node()
  1256. {
  1257. struct member_node_data *mn = 0;
  1258. struct member_node_data *oldest = 0;
  1259. int i;
  1260. for (i = 0; i < trans_member_count; i++) {
  1261. mn = evt_find_node(trans_member_list[i]);
  1262. if (!mn || (mn->mn_started == 0)) {
  1263. log_printf(LOG_LEVEL_ERROR,
  1264. "Transitional config Node %s not active\n",
  1265. api->totem_ifaces_print (trans_member_list[i]));
  1266. continue;
  1267. }
  1268. if ((oldest == NULL) ||
  1269. (mn->mn_node_info.bootTimestamp <
  1270. oldest->mn_node_info.bootTimestamp )) {
  1271. oldest = mn;
  1272. } else if (mn->mn_node_info.bootTimestamp ==
  1273. oldest->mn_node_info.bootTimestamp) {
  1274. if (mn->mn_node_info.nodeId < oldest->mn_node_info.nodeId) {
  1275. oldest = mn;
  1276. }
  1277. }
  1278. }
  1279. return oldest;
  1280. }
  1281. /*
  1282. * keep track of the last event ID from a node.
  1283. * If we get an event ID less than our last, we've already
  1284. * seen it. It's probably a retained event being sent to
  1285. * a new node.
  1286. */
  1287. static int check_last_event(
  1288. struct lib_event_data *evtpkt,
  1289. unsigned int nodeid)
  1290. {
  1291. struct member_node_data *nd;
  1292. SaClmClusterNodeT *cn;
  1293. #ifdef TODO
  1294. nd = evt_find_node(nodeid);
  1295. if (!nd) {
  1296. log_printf(LOG_LEVEL_DEBUG,
  1297. "Node ID %s not found for event %llx\n",
  1298. api->totem_ifaces_print (evtpkt->led_publisher_node_id),
  1299. (unsigned long long)evtpkt->led_event_id);
  1300. cn = main_clm_get_by_nodeid(nodeid);
  1301. if (!cn) {
  1302. log_printf(LOG_LEVEL_DEBUG,
  1303. "Cluster Node 0x%s not found for event %llx\n",
  1304. api->totem_ifaces_print (evtpkt->led_publisher_node_id),
  1305. (unsigned long long)evtpkt->led_event_id);
  1306. } else {
  1307. evt_add_node(nodeid, cn);
  1308. nd = evt_find_node(nodeid);
  1309. }
  1310. }
  1311. if (!nd) {
  1312. return 0;
  1313. }
  1314. if ((nd->mn_last_msg_id < evtpkt->led_msg_id)) {
  1315. nd->mn_last_msg_id = evtpkt->led_msg_id;
  1316. return 0;
  1317. }
  1318. #endif
  1319. return 1;
  1320. }
  1321. /*
  1322. * event id generating code. We use the node ID for this node for the
  1323. * upper 32 bits of the event ID to make sure that we can generate a cluster
  1324. * wide unique event ID for a given event.
  1325. */
  1326. SaAisErrorT set_event_id(mar_uint32_t node_id)
  1327. {
  1328. SaAisErrorT err = SA_AIS_OK;
  1329. if (base_id_top) {
  1330. err = SA_AIS_ERR_EXIST;
  1331. }
  1332. base_id_top = (mar_evteventid_t)node_id << 32;
  1333. return err;
  1334. }
  1335. /*
  1336. * See if an event Id is still in use in the retained event
  1337. * list.
  1338. */
  1339. static int id_in_use(uint64_t id, uint64_t base)
  1340. {
  1341. struct list_head *l;
  1342. struct event_data *edp;
  1343. mar_evteventid_t evtid = (id << 32) | (base & BASE_ID_MASK);
  1344. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1345. edp = list_entry(l, struct event_data, ed_retained);
  1346. if (edp->ed_event.led_event_id == evtid) {
  1347. return 1;
  1348. }
  1349. }
  1350. return 0;
  1351. }
  1352. static SaAisErrorT get_event_id(uint64_t *event_id, uint64_t *msg_id)
  1353. {
  1354. /*
  1355. * Don't reuse an event ID if it is still valid because of
  1356. * a retained event.
  1357. */
  1358. while (id_in_use(base_id_top, base_id)) {
  1359. base_id++;
  1360. }
  1361. *event_id = base_id_top | (base_id & BASE_ID_MASK) ;
  1362. *msg_id = base_id++;
  1363. return SA_AIS_OK;
  1364. }
  1365. /*
  1366. * Timer handler to delete expired events.
  1367. *
  1368. */
  1369. static void
  1370. event_retention_timeout(void *data)
  1371. {
  1372. struct event_data *edp = data;
  1373. log_printf(RETENTION_TIME_DEBUG, "Event ID %llx expired\n",
  1374. (unsigned long long)edp->ed_event.led_event_id);
  1375. /*
  1376. * adjust next_retained if we're in recovery and
  1377. * were in charge of sending retained events.
  1378. */
  1379. if (recovery_phase != evt_recovery_complete && recovery_node) {
  1380. if (next_retained == &edp->ed_retained) {
  1381. next_retained = edp->ed_retained.next;
  1382. }
  1383. }
  1384. list_del(&edp->ed_retained);
  1385. list_init(&edp->ed_retained);
  1386. /*
  1387. * Check to see if the channel isn't in use anymore.
  1388. */
  1389. edp->ed_my_chan->esc_retained_count--;
  1390. if (edp->ed_my_chan->esc_retained_count == 0) {
  1391. delete_channel(edp->ed_my_chan);
  1392. }
  1393. free_event_data(edp);
  1394. }
  1395. /*
  1396. * clear a particular event's retention time.
  1397. * This will free the event as long as it isn't being
  1398. * currently used.
  1399. *
  1400. */
  1401. static SaAisErrorT
  1402. clear_retention_time(mar_evteventid_t event_id)
  1403. {
  1404. struct event_data *edp;
  1405. struct list_head *l, *nxt;
  1406. log_printf(RETENTION_TIME_DEBUG, "Search for Event ID %llx\n",
  1407. (unsigned long long)event_id);
  1408. for (l = retained_list.next; l != &retained_list; l = nxt) {
  1409. nxt = l->next;
  1410. edp = list_entry(l, struct event_data, ed_retained);
  1411. if (edp->ed_event.led_event_id != event_id) {
  1412. continue;
  1413. }
  1414. log_printf(RETENTION_TIME_DEBUG,
  1415. "Clear retention time for Event ID %llx\n",
  1416. (unsigned long long)edp->ed_event.led_event_id);
  1417. api->timer_delete(edp->ed_timer_handle);
  1418. edp->ed_event.led_retention_time = 0;
  1419. list_del(&edp->ed_retained);
  1420. list_init(&edp->ed_retained);
  1421. /*
  1422. * Check to see if the channel isn't in use anymore.
  1423. */
  1424. edp->ed_my_chan->esc_retained_count--;
  1425. if (edp->ed_my_chan->esc_retained_count == 0) {
  1426. delete_channel(edp->ed_my_chan);
  1427. }
  1428. free_event_data(edp);
  1429. return SA_AIS_OK;
  1430. }
  1431. return SA_AIS_ERR_NOT_EXIST;
  1432. }
  1433. /*
  1434. * Remove specified channel from event delivery list
  1435. */
  1436. static void
  1437. remove_delivered_channel(struct event_svr_channel_open *eco)
  1438. {
  1439. int i;
  1440. struct list_head *l;
  1441. struct event_data *edp;
  1442. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1443. edp = list_entry(l, struct event_data, ed_retained);
  1444. for (i = 0; i < edp->ed_delivered_next; i++) {
  1445. if (edp->ed_delivered[i] == eco) {
  1446. edp->ed_delivered_next--;
  1447. if (edp->ed_delivered_next == i) {
  1448. break;
  1449. }
  1450. memmove(&edp->ed_delivered[i],
  1451. &edp->ed_delivered[i+1],
  1452. &edp->ed_delivered[edp->ed_delivered_next] -
  1453. &edp->ed_delivered[i]);
  1454. break;
  1455. }
  1456. }
  1457. }
  1458. }
  1459. /*
  1460. * If there is a retention time, add this open channel to the event so
  1461. * we can check if we've already delivered this message later if a new
  1462. * subscription matches.
  1463. */
  1464. #define DELIVER_SIZE 8
  1465. static void
  1466. evt_delivered(struct event_data *evt, struct event_svr_channel_open *eco)
  1467. {
  1468. if (!evt->ed_event.led_retention_time) {
  1469. return;
  1470. }
  1471. log_printf(LOG_LEVEL_DEBUG, "delivered ID %llx to eco %p\n",
  1472. (unsigned long long)evt->ed_event.led_event_id, eco);
  1473. if (evt->ed_delivered_count == evt->ed_delivered_next) {
  1474. struct event_svr_channel_open **ed_delivered_tmp;
  1475. ed_delivered_tmp = realloc (evt->ed_delivered,
  1476. DELIVER_SIZE * sizeof(struct event_svr_channel_open *));
  1477. if (ed_delivered_tmp == NULL) {
  1478. log_printf(LOG_LEVEL_WARNING, "Memory error realloc\n");
  1479. return;
  1480. }
  1481. evt->ed_delivered = ed_delivered_tmp;
  1482. memset(evt->ed_delivered + evt->ed_delivered_next, 0,
  1483. DELIVER_SIZE * sizeof(struct event_svr_channel_open *));
  1484. evt->ed_delivered_next = evt->ed_delivered_count;
  1485. evt->ed_delivered_count += DELIVER_SIZE;
  1486. }
  1487. evt->ed_delivered[evt->ed_delivered_next++] = eco;
  1488. }
  1489. /*
  1490. * Check to see if an event has already been delivered to this open channel
  1491. */
  1492. static int
  1493. evt_already_delivered(struct event_data *evt,
  1494. struct event_svr_channel_open *eco)
  1495. {
  1496. int i;
  1497. if (!evt->ed_event.led_retention_time) {
  1498. return 0;
  1499. }
  1500. log_printf(LOG_LEVEL_DEBUG, "Deliver count: %d deliver_next %d\n",
  1501. evt->ed_delivered_count, evt->ed_delivered_next);
  1502. for (i = 0; i < evt->ed_delivered_next; i++) {
  1503. log_printf(LOG_LEVEL_DEBUG, "Checking ID %llx delivered %p eco %p\n",
  1504. (unsigned long long)evt->ed_event.led_event_id,
  1505. evt->ed_delivered[i], eco);
  1506. if (evt->ed_delivered[i] == eco) {
  1507. return 1;
  1508. }
  1509. }
  1510. return 0;
  1511. }
  1512. /*
  1513. * Compare a filter to a given pattern.
  1514. * return SA_AIS_OK if the pattern matches a filter
  1515. */
  1516. static SaAisErrorT
  1517. filter_match(mar_evt_event_pattern_t *ep, mar_evt_event_filter_t *ef)
  1518. {
  1519. int ret;
  1520. ret = SA_AIS_ERR_FAILED_OPERATION;
  1521. switch (ef->filter_type) {
  1522. case SA_EVT_PREFIX_FILTER:
  1523. if (ef->filter.pattern_size > ep->pattern_size) {
  1524. break;
  1525. }
  1526. if (strncmp((char *)ef->filter.pattern, (char *)ep->pattern,
  1527. ef->filter.pattern_size) == 0) {
  1528. ret = SA_AIS_OK;
  1529. }
  1530. break;
  1531. case SA_EVT_SUFFIX_FILTER:
  1532. if (ef->filter.pattern_size > ep->pattern_size) {
  1533. break;
  1534. }
  1535. if (strncmp((char *)ef->filter.pattern,
  1536. (char *)&ep->pattern[ep->pattern_size - ef->filter.pattern_size],
  1537. ef->filter.pattern_size) == 0) {
  1538. ret = SA_AIS_OK;
  1539. }
  1540. break;
  1541. case SA_EVT_EXACT_FILTER:
  1542. if (ef->filter.pattern_size != ep->pattern_size) {
  1543. break;
  1544. }
  1545. if (strncmp((char *)ef->filter.pattern, (char *)ep->pattern,
  1546. ef->filter.pattern_size) == 0) {
  1547. ret = SA_AIS_OK;
  1548. }
  1549. break;
  1550. case SA_EVT_PASS_ALL_FILTER:
  1551. ret = SA_AIS_OK;
  1552. break;
  1553. default:
  1554. break;
  1555. }
  1556. return ret;
  1557. }
  1558. /*
  1559. * compare the event's patterns with the subscription's filter rules.
  1560. * SA_AIS_OK is returned if the event matches the filter rules.
  1561. */
  1562. static SaAisErrorT
  1563. event_match(struct event_data *evt,
  1564. struct event_svr_channel_subscr *ecs)
  1565. {
  1566. mar_evt_event_filter_t *ef;
  1567. mar_evt_event_pattern_t *ep;
  1568. uint32_t filt_count;
  1569. SaAisErrorT ret = SA_AIS_OK;
  1570. int i;
  1571. ep = (mar_evt_event_pattern_t *)(&evt->ed_event.led_body[0]);
  1572. ef = ecs->ecs_filters->filters;
  1573. filt_count = min(ecs->ecs_filters->filters_number,
  1574. evt->ed_event.led_patterns_number);
  1575. for (i = 0; i < filt_count; i++) {
  1576. ret = filter_match(ep, ef);
  1577. if (ret != SA_AIS_OK) {
  1578. break;
  1579. }
  1580. ep++;
  1581. ef++;
  1582. }
  1583. return ret;
  1584. }
  1585. /*
  1586. * Scan undelivered pending events and either remove them if no subscription
  1587. * filters match anymore or re-assign them to another matching subscription
  1588. */
  1589. static void
  1590. filter_undelivered_events(struct event_svr_channel_open *op_chan)
  1591. {
  1592. struct event_svr_channel_open *eco;
  1593. struct event_svr_channel_instance *eci;
  1594. struct event_svr_channel_subscr *ecs;
  1595. struct chan_event_list *cel;
  1596. struct libevt_pd *esip;
  1597. struct list_head *l, *nxt;
  1598. struct list_head *l1, *l2;
  1599. int i;
  1600. esip = (struct libevt_pd *)api->ipc_private_data_get(op_chan->eco_conn);
  1601. eci = op_chan->eco_channel;
  1602. /*
  1603. * Scan each of the priority queues for messages
  1604. */
  1605. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  1606. /*
  1607. * examine each message queued for delivery
  1608. */
  1609. for (l = esip->esi_events[i].next; l != &esip->esi_events[i]; l = nxt) {
  1610. nxt = l->next;
  1611. cel = list_entry(l, struct chan_event_list, cel_entry);
  1612. /*
  1613. * Check open channels
  1614. */
  1615. for (l1 = eci->esc_open_chans.next;
  1616. l1 != &eci->esc_open_chans; l1 = l1->next) {
  1617. eco = list_entry(l1, struct event_svr_channel_open, eco_entry);
  1618. /*
  1619. * See if this channel open instance belongs
  1620. * to this evtinitialize instance
  1621. */
  1622. if (eco->eco_conn != op_chan->eco_conn) {
  1623. continue;
  1624. }
  1625. /*
  1626. * See if enabled to receive
  1627. */
  1628. if (!(eco->eco_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  1629. continue;
  1630. }
  1631. /*
  1632. * Check subscriptions
  1633. */
  1634. for (l2 = eco->eco_subscr.next;
  1635. l2 != &eco->eco_subscr; l2 = l2->next) {
  1636. ecs = list_entry(l2,
  1637. struct event_svr_channel_subscr, ecs_entry);
  1638. if (event_match(cel->cel_event, ecs) == SA_AIS_OK) {
  1639. /*
  1640. * Something still matches.
  1641. * We'll assign it to
  1642. * the new subscription.
  1643. */
  1644. cel->cel_sub_id = ecs->ecs_sub_id;
  1645. cel->cel_chan_handle = eco->eco_lib_handle;
  1646. goto next_event;
  1647. }
  1648. }
  1649. }
  1650. /*
  1651. * No subscription filter matches anymore. We
  1652. * can delete this event.
  1653. */
  1654. list_del(&cel->cel_entry);
  1655. list_init(&cel->cel_entry);
  1656. esip->esi_nevents--;
  1657. free_event_data(cel->cel_event);
  1658. free(cel);
  1659. next_event:
  1660. continue;
  1661. }
  1662. }
  1663. }
  1664. /*
  1665. * Notify the library of a pending event
  1666. */
  1667. static void __notify_event(void *conn)
  1668. {
  1669. struct res_evt_event_data res;
  1670. struct libevt_pd *esip;
  1671. esip = (struct libevt_pd *)api->ipc_private_data_get(conn);
  1672. log_printf(LOG_LEVEL_DEBUG, "DELIVER: notify\n");
  1673. if (esip->esi_nevents != 0) {
  1674. res.evd_head.size = sizeof(res);
  1675. res.evd_head.id = MESSAGE_RES_EVT_AVAILABLE;
  1676. res.evd_head.error = SA_AIS_OK;
  1677. api->ipc_conn_send_response(api->ipc_conn_partner_get(conn),
  1678. &res, sizeof(res));
  1679. }
  1680. }
  1681. inline void notify_event(void *conn)
  1682. {
  1683. struct libevt_pd *esip;
  1684. esip = (struct libevt_pd *)api->ipc_private_data_get(conn);
  1685. /*
  1686. * Give the library a kick if there aren't already
  1687. * events queued for delivery.
  1688. */
  1689. if (esip->esi_nevents++ == 0) {
  1690. __notify_event(conn);
  1691. }
  1692. }
  1693. /*
  1694. * sends/queues up an event for a subscribed channel.
  1695. */
  1696. static void
  1697. deliver_event(struct event_data *evt,
  1698. struct event_svr_channel_open *eco,
  1699. struct event_svr_channel_subscr *ecs)
  1700. {
  1701. struct chan_event_list *ep;
  1702. SaEvtEventPriorityT evt_prio = evt->ed_event.led_priority;
  1703. struct chan_event_list *cel;
  1704. int do_deliver_event = 0;
  1705. int do_deliver_warning = 0;
  1706. int i;
  1707. struct libevt_pd *esip;
  1708. esip = (struct libevt_pd *)api->ipc_private_data_get(eco->eco_conn);
  1709. if (evt_prio > SA_EVT_LOWEST_PRIORITY) {
  1710. evt_prio = SA_EVT_LOWEST_PRIORITY;
  1711. }
  1712. /*
  1713. * Delivery queue check.
  1714. * - If the queue is blocked, see if we've sent enough messages to
  1715. * unblock it.
  1716. * - If it isn't blocked, see if this message will put us over the top.
  1717. * - If we can't deliver this message, see if we can toss some lower
  1718. * priority message to make room for this one.
  1719. * - If we toss any messages, queue up an event of SA_EVT_LOST_EVENT_PATTERN
  1720. * to let the application know that we dropped some messages.
  1721. */
  1722. if (esip->esi_queue_blocked) {
  1723. if (esip->esi_nevents < evt_delivery_queue_resume) {
  1724. esip->esi_queue_blocked = 0;
  1725. log_printf(LOG_LEVEL_DEBUG, "unblock\n");
  1726. }
  1727. }
  1728. assert (esip->esi_nevents >= 0);
  1729. if (!esip->esi_queue_blocked &&
  1730. (esip->esi_nevents >= evt_delivery_queue_size)) {
  1731. log_printf(LOG_LEVEL_DEBUG, "block\n");
  1732. esip->esi_queue_blocked = 1;
  1733. do_deliver_warning = 1;
  1734. }
  1735. if (esip->esi_queue_blocked) {
  1736. do_deliver_event = 0;
  1737. for (i = SA_EVT_LOWEST_PRIORITY; i > evt_prio; i--) {
  1738. if (!list_empty(&esip->esi_events[i])) {
  1739. /*
  1740. * Get the last item on the list, so we drop the most
  1741. * recent lowest priority event.
  1742. */
  1743. cel = list_entry(esip->esi_events[i].prev,
  1744. struct chan_event_list, cel_entry);
  1745. log_printf(LOG_LEVEL_DEBUG, "Drop 0x%0llx\n",
  1746. (unsigned long long)cel->cel_event->ed_event.led_event_id);
  1747. list_del(&cel->cel_entry);
  1748. free_event_data(cel->cel_event);
  1749. free(cel);
  1750. esip->esi_nevents--;
  1751. do_deliver_event = 1;
  1752. break;
  1753. }
  1754. }
  1755. } else {
  1756. do_deliver_event = 1;
  1757. }
  1758. /*
  1759. * Queue the event for delivery
  1760. */
  1761. if (do_deliver_event) {
  1762. ep = malloc(sizeof(*ep));
  1763. if (!ep) {
  1764. log_printf(LOG_LEVEL_WARNING,
  1765. "3Memory allocation error, can't deliver event\n");
  1766. return;
  1767. }
  1768. evt->ed_ref_count++;
  1769. ep->cel_chan_handle = eco->eco_lib_handle;
  1770. ep->cel_sub_id = ecs->ecs_sub_id;
  1771. list_init(&ep->cel_entry);
  1772. ep->cel_event = evt;
  1773. list_add_tail(&ep->cel_entry, &esip->esi_events[evt_prio]);
  1774. evt_delivered(evt, eco);
  1775. notify_event(eco->eco_conn);
  1776. }
  1777. /*
  1778. * If we dropped an event, queue this so that the application knows
  1779. * what has happened.
  1780. */
  1781. if (do_deliver_warning) {
  1782. struct event_data *ed;
  1783. ed = malloc(dropped_event_size);
  1784. if (!ed) {
  1785. log_printf(LOG_LEVEL_WARNING,
  1786. "4Memory allocation error, can't deliver event\n");
  1787. return;
  1788. }
  1789. log_printf(LOG_LEVEL_DEBUG, "Warn 0x%0llx\n",
  1790. (unsigned long long)evt->ed_event.led_event_id);
  1791. memcpy(ed, dropped_event, dropped_event_size);
  1792. ed->ed_event.led_publish_time = clust_time_now();
  1793. ed->ed_event.led_event_id = SA_EVT_EVENTID_LOST;
  1794. list_init(&ed->ed_retained);
  1795. ep = malloc(sizeof(*ep));
  1796. if (!ep) {
  1797. log_printf(LOG_LEVEL_WARNING,
  1798. "5Memory allocation error, can't deliver event\n");
  1799. return;
  1800. }
  1801. ep->cel_chan_handle = eco->eco_lib_handle;
  1802. ep->cel_sub_id = ecs->ecs_sub_id;
  1803. list_init(&ep->cel_entry);
  1804. ep->cel_event = ed;
  1805. list_add_tail(&ep->cel_entry, &esip->esi_events[SA_EVT_HIGHEST_PRIORITY]);
  1806. notify_event(eco->eco_conn);
  1807. }
  1808. }
  1809. /*
  1810. * Take the event data and swap the elements so they match our architectures
  1811. * word order.
  1812. */
  1813. static void
  1814. convert_event(void *msg)
  1815. {
  1816. struct lib_event_data *evt = (struct lib_event_data *)msg;
  1817. mar_evt_event_pattern_t *eps;
  1818. int i;
  1819. /*
  1820. * The following elements don't require processing:
  1821. *
  1822. * converted in the main deliver_fn:
  1823. * led_head.id, led_head.size.
  1824. *
  1825. * Supplied localy:
  1826. * source_addr, publisher_node_id, receive_time.
  1827. *
  1828. * Used internaly only:
  1829. * led_svr_channel_handle and led_lib_channel_handle.
  1830. */
  1831. swab_mar_name_t (&evt->led_chan_name);
  1832. evt->led_chan_unlink_id = swab64(evt->led_chan_unlink_id);
  1833. evt->led_event_id = swab64(evt->led_event_id);
  1834. evt->led_sub_id = swab32(evt->led_sub_id);
  1835. swab_mar_name_t (&evt->led_publisher_name);
  1836. evt->led_retention_time = swab64(evt->led_retention_time);
  1837. evt->led_publish_time = swab64(evt->led_publish_time);
  1838. evt->led_user_data_offset = swab32(evt->led_user_data_offset);
  1839. evt->led_user_data_size = swab32(evt->led_user_data_size);
  1840. evt->led_patterns_number = swab32(evt->led_patterns_number);
  1841. /*
  1842. * Now we need to go through the led_body and swizzle pattern counts.
  1843. * We can't do anything about user data since it doesn't have a specified
  1844. * format. The application is on its own here.
  1845. */
  1846. eps = (mar_evt_event_pattern_t *)evt->led_body;
  1847. for (i = 0; i < evt->led_patterns_number; i++) {
  1848. eps->pattern_size = swab32(eps->pattern_size);
  1849. eps->allocated_size = swab32(eps->allocated_size);
  1850. eps++;
  1851. }
  1852. }
  1853. /*
  1854. * Take an event received from the network and fix it up to be usable.
  1855. * - fix up pointers for pattern list.
  1856. * - fill in some channel info
  1857. */
  1858. static struct event_data *
  1859. make_local_event(struct lib_event_data *p,
  1860. struct event_svr_channel_instance *eci)
  1861. {
  1862. struct event_data *ed;
  1863. mar_evt_event_pattern_t *eps;
  1864. SaUint8T *str;
  1865. uint32_t ed_size;
  1866. int i;
  1867. ed_size = sizeof(*ed) + p->led_user_data_offset + p->led_user_data_size;
  1868. ed = malloc(ed_size);
  1869. if (!ed) {
  1870. log_printf(LOG_LEVEL_WARNING,
  1871. "Failed to allocate %u bytes for event, offset %u, data size %u\n",
  1872. ed_size, p->led_user_data_offset, p->led_user_data_size);
  1873. return 0;
  1874. }
  1875. memset(ed, 0, ed_size);
  1876. list_init(&ed->ed_retained);
  1877. ed->ed_my_chan = eci;
  1878. /*
  1879. * Fill in lib_event_data and make the pattern pointers valid
  1880. */
  1881. memcpy(&ed->ed_event, p, sizeof(*p) +
  1882. p->led_user_data_offset + p->led_user_data_size);
  1883. eps = (mar_evt_event_pattern_t *)ed->ed_event.led_body;
  1884. str = ed->ed_event.led_body +
  1885. (ed->ed_event.led_patterns_number * sizeof(mar_evt_event_pattern_t));
  1886. for (i = 0; i < ed->ed_event.led_patterns_number; i++) {
  1887. eps->pattern = str;
  1888. str += eps->pattern_size;
  1889. eps++;
  1890. }
  1891. ed->ed_ref_count++;
  1892. return ed;
  1893. }
  1894. /*
  1895. * Set an event to be retained.
  1896. */
  1897. static void retain_event(struct event_data *evt)
  1898. {
  1899. uint32_t ret;
  1900. evt->ed_ref_count++;
  1901. evt->ed_my_chan->esc_retained_count++;
  1902. list_add_tail(&evt->ed_retained, &retained_list);
  1903. ret = api->timer_add_duration (
  1904. evt->ed_event.led_retention_time,
  1905. evt,
  1906. event_retention_timeout,
  1907. &evt->ed_timer_handle);
  1908. if (ret != 0) {
  1909. log_printf(LOG_LEVEL_ERROR,
  1910. "retention of event id 0x%llx failed\n",
  1911. (unsigned long long)evt->ed_event.led_event_id);
  1912. } else {
  1913. log_printf(RETENTION_TIME_DEBUG, "Retain event ID 0x%llx for %llu ms\n",
  1914. (unsigned long long)evt->ed_event.led_event_id, evt->ed_event.led_retention_time/100000LL);
  1915. }
  1916. }
  1917. /*
  1918. * Scan the subscription list and look for the specified subsctiption ID.
  1919. * Only look for the ID in subscriptions that are associated with the
  1920. * saEvtInitialize associated with the specified open channel.
  1921. */
  1922. static struct event_svr_channel_subscr *find_subscr(
  1923. struct event_svr_channel_open *open_chan, SaEvtSubscriptionIdT sub_id)
  1924. {
  1925. struct event_svr_channel_instance *eci;
  1926. struct event_svr_channel_subscr *ecs;
  1927. struct event_svr_channel_open *eco;
  1928. struct list_head *l, *l1;
  1929. void *conn = open_chan->eco_conn;
  1930. eci = open_chan->eco_channel;
  1931. /*
  1932. * Check for subscription id already in use.
  1933. * Subscriptions are unique within saEvtInitialize (Callback scope).
  1934. */
  1935. for (l = eci->esc_open_chans.next; l != &eci->esc_open_chans; l = l->next) {
  1936. eco = list_entry(l, struct event_svr_channel_open, eco_entry);
  1937. /*
  1938. * Don't bother with open channels associated with another
  1939. * EvtInitialize
  1940. */
  1941. if (eco->eco_conn != conn) {
  1942. continue;
  1943. }
  1944. for (l1 = eco->eco_subscr.next; l1 != &eco->eco_subscr; l1 = l1->next) {
  1945. ecs = list_entry(l1, struct event_svr_channel_subscr, ecs_entry);
  1946. if (ecs->ecs_sub_id == sub_id) {
  1947. return ecs;
  1948. }
  1949. }
  1950. }
  1951. return 0;
  1952. }
  1953. /*
  1954. * Handler for saEvtInitialize
  1955. */
  1956. static int evt_lib_init(void *conn)
  1957. {
  1958. struct libevt_pd *libevt_pd;
  1959. int i;
  1960. libevt_pd = (struct libevt_pd *)api->ipc_private_data_get(conn);
  1961. log_printf(LOG_LEVEL_DEBUG, "saEvtInitialize request.\n");
  1962. /*
  1963. * Initailze event instance data
  1964. */
  1965. memset(libevt_pd, 0, sizeof(*libevt_pd));
  1966. /*
  1967. * Initialize the open channel handle database.
  1968. */
  1969. hdb_create(&libevt_pd->esi_hdb);
  1970. /*
  1971. * list of channels open on this instance
  1972. */
  1973. list_init(&libevt_pd->esi_open_chans);
  1974. /*
  1975. * pending event lists for each piriority
  1976. */
  1977. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  1978. list_init(&libevt_pd->esi_events[i]);
  1979. }
  1980. return 0;
  1981. }
  1982. /*
  1983. * Handler for saEvtChannelOpen
  1984. */
  1985. static void lib_evt_open_channel(void *conn, void *message)
  1986. {
  1987. SaAisErrorT error;
  1988. struct req_evt_channel_open *req;
  1989. struct res_evt_channel_open res;
  1990. struct open_chan_pending *ocp;
  1991. int ret;
  1992. req = message;
  1993. log_printf(CHAN_OPEN_DEBUG,
  1994. "saEvtChannelOpen (Open channel request)\n");
  1995. log_printf(CHAN_OPEN_DEBUG,
  1996. "handle 0x%llx, to 0x%llx\n",
  1997. (unsigned long long)req->ico_c_handle,
  1998. (unsigned long long)req->ico_timeout);
  1999. log_printf(CHAN_OPEN_DEBUG, "flags %x, channel name(%d) %s\n",
  2000. req->ico_open_flag,
  2001. req->ico_channel_name.length,
  2002. req->ico_channel_name.value);
  2003. /*
  2004. * Open the channel.
  2005. *
  2006. */
  2007. error = evt_open_channel(&req->ico_channel_name, req->ico_open_flag);
  2008. if (error != SA_AIS_OK) {
  2009. goto open_return;
  2010. }
  2011. ocp = malloc(sizeof(struct open_chan_pending));
  2012. if (!ocp) {
  2013. error = SA_AIS_ERR_NO_MEMORY;
  2014. goto open_return;
  2015. }
  2016. ocp->ocp_async = 0;
  2017. ocp->ocp_invocation = 0;
  2018. ocp->ocp_chan_name = req->ico_channel_name;
  2019. ocp->ocp_open_flag = req->ico_open_flag;
  2020. ocp->ocp_conn = conn;
  2021. ocp->ocp_c_handle = req->ico_c_handle;
  2022. ocp->ocp_timer_handle = 0;
  2023. ocp->ocp_serial_no = open_serial_no;
  2024. list_init(&ocp->ocp_entry);
  2025. list_add_tail(&ocp->ocp_entry, &open_pending);
  2026. ret = api->timer_add_duration (
  2027. req->ico_timeout,
  2028. ocp,
  2029. chan_open_timeout,
  2030. &ocp->ocp_timer_handle);
  2031. if (ret != 0) {
  2032. log_printf(LOG_LEVEL_WARNING,
  2033. "Error setting timeout for open channel %s\n",
  2034. req->ico_channel_name.value);
  2035. }
  2036. return;
  2037. open_return:
  2038. res.ico_head.size = sizeof(res);
  2039. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2040. res.ico_head.error = error;
  2041. api->ipc_conn_send_response(conn, &res, sizeof(res));
  2042. }
  2043. /*
  2044. * Handler for saEvtChannelOpen
  2045. */
  2046. static void lib_evt_open_channel_async(void *conn, void *message)
  2047. {
  2048. SaAisErrorT error;
  2049. struct req_evt_channel_open *req;
  2050. struct res_evt_channel_open res;
  2051. struct open_chan_pending *ocp;
  2052. req = message;
  2053. log_printf(CHAN_OPEN_DEBUG,
  2054. "saEvtChannelOpenAsync (Async Open channel request)\n");
  2055. log_printf(CHAN_OPEN_DEBUG,
  2056. "handle 0x%llx, to 0x%llx\n",
  2057. (unsigned long long)req->ico_c_handle,
  2058. (unsigned long long)req->ico_invocation);
  2059. log_printf(CHAN_OPEN_DEBUG, "flags %x, channel name(%d) %s\n",
  2060. req->ico_open_flag,
  2061. req->ico_channel_name.length,
  2062. req->ico_channel_name.value);
  2063. /*
  2064. * Open the channel.
  2065. *
  2066. */
  2067. error = evt_open_channel(&req->ico_channel_name, req->ico_open_flag);
  2068. if (error != SA_AIS_OK) {
  2069. goto open_return;
  2070. }
  2071. ocp = malloc(sizeof(struct open_chan_pending));
  2072. if (!ocp) {
  2073. error = SA_AIS_ERR_NO_MEMORY;
  2074. goto open_return;
  2075. }
  2076. ocp->ocp_async = 1;
  2077. ocp->ocp_invocation = req->ico_invocation;
  2078. ocp->ocp_c_handle = req->ico_c_handle;
  2079. ocp->ocp_chan_name = req->ico_channel_name;
  2080. ocp->ocp_open_flag = req->ico_open_flag;
  2081. ocp->ocp_conn = conn;
  2082. ocp->ocp_timer_handle = 0;
  2083. ocp->ocp_serial_no = open_serial_no;
  2084. list_init(&ocp->ocp_entry);
  2085. list_add_tail(&ocp->ocp_entry, &open_pending);
  2086. open_return:
  2087. res.ico_head.size = sizeof(res);
  2088. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2089. res.ico_head.error = error;
  2090. api->ipc_conn_send_response(conn, &res, sizeof(res));
  2091. }
  2092. /*
  2093. * Used by the channel close code and by the implicit close
  2094. * when saEvtFinalize is called with channels open.
  2095. */
  2096. static SaAisErrorT
  2097. common_chan_close(struct event_svr_channel_open *eco, struct libevt_pd *esip)
  2098. {
  2099. struct event_svr_channel_subscr *ecs;
  2100. struct list_head *l, *nxt;
  2101. log_printf(LOG_LEVEL_DEBUG, "Close channel %s flags 0x%02x\n",
  2102. eco->eco_channel->esc_channel_name.value,
  2103. eco->eco_flags);
  2104. /*
  2105. * Disconnect the channel open structure.
  2106. *
  2107. * Check for subscriptions and deal with them. In this case
  2108. * if there are any, we just implicitly unsubscribe.
  2109. *
  2110. * When We're done with the channel open data then we can
  2111. * remove it's handle (this frees the memory too).
  2112. *
  2113. */
  2114. list_del(&eco->eco_entry);
  2115. list_del(&eco->eco_instance_entry);
  2116. for (l = eco->eco_subscr.next; l != &eco->eco_subscr; l = nxt) {
  2117. nxt = l->next;
  2118. ecs = list_entry(l, struct event_svr_channel_subscr, ecs_entry);
  2119. log_printf(LOG_LEVEL_DEBUG, "Unsubscribe ID: %x\n",
  2120. ecs->ecs_sub_id);
  2121. list_del(&ecs->ecs_entry);
  2122. free(ecs);
  2123. /*
  2124. * Purge any pending events associated with this subscription
  2125. * that don't match another subscription.
  2126. */
  2127. filter_undelivered_events(eco);
  2128. }
  2129. /*
  2130. * Remove this channel from the retained event's notion
  2131. * of who they have been delivered to.
  2132. */
  2133. remove_delivered_channel(eco);
  2134. return evt_close_channel(&eco->eco_channel->esc_channel_name,
  2135. eco->eco_channel->esc_unlink_id);
  2136. }
  2137. /*
  2138. * Handler for saEvtChannelClose
  2139. */
  2140. static void lib_evt_close_channel(void *conn, void *message)
  2141. {
  2142. struct req_evt_channel_close *req;
  2143. struct res_evt_channel_close res;
  2144. struct event_svr_channel_open *eco;
  2145. unsigned int ret;
  2146. void *ptr;
  2147. struct libevt_pd *esip;
  2148. esip = (struct libevt_pd *)api->ipc_private_data_get(conn);
  2149. req = message;
  2150. log_printf(LOG_LEVEL_DEBUG,
  2151. "saEvtChannelClose (Close channel request)\n");
  2152. log_printf(LOG_LEVEL_DEBUG, "handle 0x%x\n", req->icc_channel_handle);
  2153. /*
  2154. * look up the channel handle
  2155. */
  2156. ret = hdb_handle_get(&esip->esi_hdb,
  2157. req->icc_channel_handle, &ptr);
  2158. if (ret != 0) {
  2159. goto chan_close_done;
  2160. }
  2161. eco = ptr;
  2162. common_chan_close(eco, esip);
  2163. hdb_handle_destroy(&esip->esi_hdb, req->icc_channel_handle);
  2164. hdb_handle_put(&esip->esi_hdb, req->icc_channel_handle);
  2165. chan_close_done:
  2166. res.icc_head.size = sizeof(res);
  2167. res.icc_head.id = MESSAGE_RES_EVT_CLOSE_CHANNEL;
  2168. res.icc_head.error = ((ret == 0) ? SA_AIS_OK : SA_AIS_ERR_BAD_HANDLE);
  2169. api->ipc_conn_send_response(conn, &res, sizeof(res));
  2170. }
  2171. /*
  2172. * Handler for saEvtChannelUnlink
  2173. */
  2174. static void lib_evt_unlink_channel(void *conn, void *message)
  2175. {
  2176. struct req_evt_channel_unlink *req;
  2177. struct res_evt_channel_unlink res;
  2178. struct iovec chn_iovec;
  2179. struct unlink_chan_pending *ucp = 0;
  2180. struct req_evt_chan_command cpkt;
  2181. SaAisErrorT error = SA_AIS_ERR_LIBRARY;
  2182. req = message;
  2183. log_printf(CHAN_UNLINK_DEBUG,
  2184. "saEvtChannelUnlink (Unlink channel request)\n");
  2185. log_printf(CHAN_UNLINK_DEBUG, "Channel Name %s\n",
  2186. req->iuc_channel_name.value);
  2187. if (!find_channel(&req->iuc_channel_name, EVT_CHAN_ACTIVE)) {
  2188. log_printf(CHAN_UNLINK_DEBUG, "Channel Name doesn't exist\n");
  2189. error = SA_AIS_ERR_NOT_EXIST;
  2190. goto evt_unlink_err;
  2191. }
  2192. /*
  2193. * Set up the data structure so that the channel op
  2194. * mcast handler can complete the unlink comamnd back to the
  2195. * requestor.
  2196. */
  2197. ucp = malloc(sizeof(*ucp));
  2198. if (!ucp) {
  2199. log_printf(LOG_LEVEL_ERROR,
  2200. "saEvtChannelUnlink: Memory allocation failure\n");
  2201. error = SA_AIS_ERR_TRY_AGAIN;
  2202. goto evt_unlink_err;
  2203. }
  2204. ucp->ucp_unlink_id = next_chan_unlink_id();
  2205. ucp->ucp_conn = conn;
  2206. list_init(&ucp->ucp_entry);
  2207. list_add_tail(&ucp->ucp_entry, &unlink_pending);
  2208. /*
  2209. * Put together a mcast packet to notify everyone
  2210. * of the channel unlink.
  2211. */
  2212. memset(&cpkt, 0, sizeof(cpkt));
  2213. cpkt.chc_head.id =
  2214. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  2215. cpkt.chc_head.size = sizeof(cpkt);
  2216. cpkt.chc_op = EVT_UNLINK_CHAN_OP;
  2217. cpkt.u.chcu.chcu_name = req->iuc_channel_name;
  2218. cpkt.u.chcu.chcu_unlink_id = ucp->ucp_unlink_id;
  2219. chn_iovec.iov_base = (char *)&cpkt;
  2220. chn_iovec.iov_len = cpkt.chc_head.size;
  2221. if (api->totem_mcast (&chn_iovec, 1, TOTEM_AGREED) == 0) {
  2222. return;
  2223. }
  2224. evt_unlink_err:
  2225. if (ucp) {
  2226. list_del(&ucp->ucp_entry);
  2227. free(ucp);
  2228. }
  2229. res.iuc_head.size = sizeof(res);
  2230. res.iuc_head.id = MESSAGE_RES_EVT_UNLINK_CHANNEL;
  2231. res.iuc_head.error = error;
  2232. api->ipc_conn_send_response(conn, &res, sizeof(res));
  2233. }
  2234. /*
  2235. * Subscribe to an event channel.
  2236. *
  2237. * - First look up the channel to subscribe.
  2238. * - Make sure that the subscription ID is not already in use.
  2239. * - Fill in the subscription data structures and add them to the channels
  2240. * subscription list.
  2241. * - See if there are any events with retetion times that need to be delivered
  2242. * because of the new subscription.
  2243. */
  2244. static char *filter_types[] = {
  2245. "INVALID FILTER TYPE",
  2246. "SA_EVT_PREFIX_FILTER",
  2247. "SA_EVT_SUFFIX_FILTER",
  2248. "SA_EVT_EXACT_FILTER",
  2249. "SA_EVT_PASS_ALL_FILTER",
  2250. };
  2251. /*
  2252. * saEvtEventSubscribe Handler
  2253. */
  2254. static void lib_evt_event_subscribe(void *conn, void *message)
  2255. {
  2256. struct req_evt_event_subscribe *req;
  2257. struct res_evt_event_subscribe res;
  2258. mar_evt_event_filter_array_t *filters;
  2259. SaAisErrorT error;
  2260. struct event_svr_channel_open *eco;
  2261. struct event_svr_channel_instance *eci;
  2262. struct event_svr_channel_subscr *ecs;
  2263. struct event_data *evt;
  2264. struct list_head *l;
  2265. void *ptr;
  2266. unsigned int ret;
  2267. int i;
  2268. struct libevt_pd *esip;
  2269. esip = (struct libevt_pd *)api->ipc_private_data_get(conn);
  2270. req = message;
  2271. log_printf(LOG_LEVEL_DEBUG,
  2272. "saEvtEventSubscribe (Subscribe request)\n");
  2273. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%llx\n",
  2274. (unsigned long long)req->ics_sub_id);
  2275. /*
  2276. * look up the channel handle
  2277. */
  2278. ret = hdb_handle_get(&esip->esi_hdb, req->ics_channel_handle, &ptr);
  2279. if (ret != 0) {
  2280. error = SA_AIS_ERR_BAD_HANDLE;
  2281. goto subr_done;
  2282. }
  2283. eco = ptr;
  2284. eci = eco->eco_channel;
  2285. /*
  2286. * See if the id is already being used
  2287. */
  2288. ecs = find_subscr(eco, req->ics_sub_id);
  2289. if (ecs) {
  2290. error = SA_AIS_ERR_EXIST;
  2291. goto subr_put;
  2292. }
  2293. error = evtfilt_to_aisfilt(req, &filters);
  2294. if (error == SA_AIS_OK) {
  2295. log_printf(LOG_LEVEL_DEBUG, "Subscribe filters count %d\n",
  2296. (int)filters->filters_number);
  2297. for (i = 0; i < filters->filters_number; i++) {
  2298. log_printf(LOG_LEVEL_DEBUG, "type %s(%d) sz %d, <%s>\n",
  2299. filter_types[filters->filters[i].filter_type],
  2300. filters->filters[i].filter_type,
  2301. (int)filters->filters[i].filter.pattern_size,
  2302. (filters->filters[i].filter.pattern_size)
  2303. ? (char *)filters->filters[i].filter.pattern
  2304. : "");
  2305. }
  2306. }
  2307. if (error != SA_AIS_OK) {
  2308. goto subr_put;
  2309. }
  2310. ecs = (struct event_svr_channel_subscr *)malloc(sizeof(*ecs));
  2311. if (!ecs) {
  2312. error = SA_AIS_ERR_NO_MEMORY;
  2313. goto subr_put;
  2314. }
  2315. ecs->ecs_filters = filters;
  2316. ecs->ecs_sub_id = req->ics_sub_id;
  2317. list_init(&ecs->ecs_entry);
  2318. list_add(&ecs->ecs_entry, &eco->eco_subscr);
  2319. res.ics_head.size = sizeof(res);
  2320. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  2321. res.ics_head.error = error;
  2322. api->ipc_conn_send_response(conn, &res, sizeof(res));
  2323. /*
  2324. * See if an existing event with a retention time
  2325. * needs to be delivered based on this subscription
  2326. */
  2327. for (l = retained_list.next; l != &retained_list; l = l->next) {
  2328. evt = list_entry(l, struct event_data, ed_retained);
  2329. log_printf(LOG_LEVEL_DEBUG,
  2330. "Checking event ID %llx chanp %p -- sub chanp %p\n",
  2331. (unsigned long long)evt->ed_event.led_event_id,
  2332. evt->ed_my_chan, eci);
  2333. if (evt->ed_my_chan == eci) {
  2334. if (evt_already_delivered(evt, eco)) {
  2335. continue;
  2336. }
  2337. if (event_match(evt, ecs) == SA_AIS_OK) {
  2338. log_printf(LOG_LEVEL_DEBUG,
  2339. "deliver event ID: 0x%llx\n",
  2340. (unsigned long long)evt->ed_event.led_event_id);
  2341. deliver_event(evt, eco, ecs);
  2342. }
  2343. }
  2344. }
  2345. hdb_handle_put(&esip->esi_hdb, req->ics_channel_handle);
  2346. return;
  2347. subr_put:
  2348. hdb_handle_put(&esip->esi_hdb, req->ics_channel_handle);
  2349. subr_done:
  2350. res.ics_head.size = sizeof(res);
  2351. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  2352. res.ics_head.error = error;
  2353. api->ipc_conn_send_response(conn, &res, sizeof(res));
  2354. }
  2355. /*
  2356. * saEvtEventUnsubscribe Handler
  2357. */
  2358. static void lib_evt_event_unsubscribe(void *conn, void *message)
  2359. {
  2360. struct req_evt_event_unsubscribe *req;
  2361. struct res_evt_event_unsubscribe res;
  2362. struct event_svr_channel_open *eco;
  2363. struct event_svr_channel_instance *eci;
  2364. struct event_svr_channel_subscr *ecs;
  2365. SaAisErrorT error = SA_AIS_OK;
  2366. unsigned int ret;
  2367. void *ptr;
  2368. struct libevt_pd *esip;
  2369. esip = (struct libevt_pd *)api->ipc_private_data_get(conn);
  2370. req = message;
  2371. log_printf(LOG_LEVEL_DEBUG,
  2372. "saEvtEventUnsubscribe (Unsubscribe request)\n");
  2373. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%llx\n",
  2374. (unsigned long long)req->icu_sub_id);
  2375. /*
  2376. * look up the channel handle, get the open channel
  2377. * data.
  2378. */
  2379. ret = hdb_handle_get(&esip->esi_hdb,
  2380. req->icu_channel_handle, &ptr);
  2381. if (ret != 0) {
  2382. error = SA_AIS_ERR_BAD_HANDLE;
  2383. goto unsubr_done;
  2384. }
  2385. eco = ptr;
  2386. eci = eco->eco_channel;
  2387. /*
  2388. * Make sure that the id exists.
  2389. */
  2390. ecs = find_subscr(eco, req->icu_sub_id);
  2391. if (!ecs) {
  2392. error = SA_AIS_ERR_NOT_EXIST;
  2393. goto unsubr_put;
  2394. }
  2395. list_del(&ecs->ecs_entry);
  2396. log_printf(LOG_LEVEL_DEBUG,
  2397. "unsubscribe from channel %s subscription ID 0x%x "
  2398. "with %d filters\n",
  2399. eci->esc_channel_name.value,
  2400. ecs->ecs_sub_id, (int)ecs->ecs_filters->filters_number);
  2401. free_filters(ecs->ecs_filters);
  2402. free(ecs);
  2403. unsubr_put:
  2404. hdb_handle_put(&esip->esi_hdb, req->icu_channel_handle);
  2405. unsubr_done:
  2406. res.icu_head.size = sizeof(res);
  2407. res.icu_head.id = MESSAGE_RES_EVT_UNSUBSCRIBE;
  2408. res.icu_head.error = error;
  2409. api->ipc_conn_send_response(conn, &res, sizeof(res));
  2410. }
  2411. /*
  2412. * saEvtEventPublish Handler
  2413. */
  2414. static void lib_evt_event_publish(void *conn, void *message)
  2415. {
  2416. struct lib_event_data *req;
  2417. struct res_evt_event_publish res;
  2418. struct event_svr_channel_open *eco;
  2419. struct event_svr_channel_instance *eci;
  2420. mar_evteventid_t event_id = 0;
  2421. uint64_t msg_id = 0;
  2422. SaAisErrorT error = SA_AIS_OK;
  2423. struct iovec pub_iovec;
  2424. void *ptr;
  2425. int result;
  2426. unsigned int ret;
  2427. struct libevt_pd *esip;
  2428. esip = (struct libevt_pd *)api->ipc_private_data_get(conn);
  2429. req = message;
  2430. log_printf(LOG_LEVEL_DEBUG,
  2431. "saEvtEventPublish (Publish event request)\n");
  2432. /*
  2433. * look up and validate open channel info
  2434. */
  2435. ret = hdb_handle_get(&esip->esi_hdb,
  2436. req->led_svr_channel_handle, &ptr);
  2437. if (ret != 0) {
  2438. error = SA_AIS_ERR_BAD_HANDLE;
  2439. goto pub_done;
  2440. }
  2441. eco = ptr;
  2442. eci = eco->eco_channel;
  2443. /*
  2444. * modify the request structure for sending event data to subscribed
  2445. * processes.
  2446. */
  2447. get_event_id(&event_id, &msg_id);
  2448. req->led_head.id = SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_EVENTDATA);
  2449. req->led_chan_name = eci->esc_channel_name;
  2450. req->led_event_id = event_id;
  2451. req->led_msg_id = msg_id;
  2452. req->led_chan_unlink_id = eci->esc_unlink_id;
  2453. /*
  2454. * Distribute the event.
  2455. * The multicasted event will be picked up and delivered
  2456. * locally by the local network event receiver.
  2457. */
  2458. pub_iovec.iov_base = (char *)req;
  2459. pub_iovec.iov_len = req->led_head.size;
  2460. result = api->totem_mcast (&pub_iovec, 1, TOTEM_AGREED);
  2461. if (result != 0) {
  2462. error = SA_AIS_ERR_LIBRARY;
  2463. }
  2464. hdb_handle_put(&esip->esi_hdb, req->led_svr_channel_handle);
  2465. pub_done:
  2466. res.iep_head.size = sizeof(res);
  2467. res.iep_head.id = MESSAGE_RES_EVT_PUBLISH;
  2468. res.iep_head.error = error;
  2469. res.iep_event_id = event_id;
  2470. api->ipc_conn_send_response(conn, &res, sizeof(res));
  2471. }
  2472. /*
  2473. * saEvtEventRetentionTimeClear handler
  2474. */
  2475. static void lib_evt_event_clear_retentiontime(void *conn, void *message)
  2476. {
  2477. struct req_evt_event_clear_retentiontime *req;
  2478. struct res_evt_event_clear_retentiontime res;
  2479. struct req_evt_chan_command cpkt;
  2480. struct retention_time_clear_pending *rtc = 0;
  2481. struct iovec rtn_iovec;
  2482. SaAisErrorT error;
  2483. int ret;
  2484. req = message;
  2485. log_printf(RETENTION_TIME_DEBUG,
  2486. "saEvtEventRetentionTimeClear (Clear event retentiontime request)\n");
  2487. log_printf(RETENTION_TIME_DEBUG,
  2488. "event ID 0x%llx, chan handle 0x%x\n",
  2489. (unsigned long long)req->iec_event_id,
  2490. req->iec_channel_handle);
  2491. rtc = malloc(sizeof(*rtc));
  2492. if (!rtc) {
  2493. log_printf(LOG_LEVEL_ERROR,
  2494. "saEvtEventRetentionTimeClear: Memory allocation failure\n");
  2495. error = SA_AIS_ERR_TRY_AGAIN;
  2496. goto evt_ret_clr_err;
  2497. }
  2498. rtc->rtc_event_id = req->iec_event_id;
  2499. rtc->rtc_conn = conn;
  2500. list_init(&rtc->rtc_entry);
  2501. list_add_tail(&rtc->rtc_entry, &clear_pending);
  2502. /*
  2503. * Send the clear request
  2504. */
  2505. memset(&cpkt, 0, sizeof(cpkt));
  2506. cpkt.chc_head.id =
  2507. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  2508. cpkt.chc_head.size = sizeof(cpkt);
  2509. cpkt.chc_op = EVT_CLEAR_RET_OP;
  2510. cpkt.u.chc_event_id = req->iec_event_id;
  2511. rtn_iovec.iov_base = (char *)&cpkt;
  2512. rtn_iovec.iov_len = cpkt.chc_head.size;
  2513. ret = api->totem_mcast (&rtn_iovec, 1, TOTEM_AGREED);
  2514. if (ret == 0) {
  2515. // TODO this should really assert
  2516. return;
  2517. }
  2518. error = SA_AIS_ERR_LIBRARY;
  2519. evt_ret_clr_err:
  2520. if (rtc) {
  2521. list_del(&rtc->rtc_entry);
  2522. free(rtc);
  2523. }
  2524. res.iec_head.size = sizeof(res);
  2525. res.iec_head.id = MESSAGE_RES_EVT_CLEAR_RETENTIONTIME;
  2526. res.iec_head.error = error;
  2527. api->ipc_conn_send_response(conn, &res, sizeof(res));
  2528. }
  2529. /*
  2530. * Send requested event data to the application
  2531. */
  2532. static void lib_evt_event_data_get(void *conn, void *message)
  2533. {
  2534. struct lib_event_data res;
  2535. struct chan_event_list *cel;
  2536. struct event_data *edp;
  2537. int i;
  2538. struct libevt_pd *esip;
  2539. esip = (struct libevt_pd *)api->ipc_private_data_get(conn);
  2540. /*
  2541. * Deliver events in publish order within priority
  2542. */
  2543. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  2544. if (!list_empty(&esip->esi_events[i])) {
  2545. cel = list_entry(esip->esi_events[i].next, struct chan_event_list,
  2546. cel_entry);
  2547. list_del(&cel->cel_entry);
  2548. list_init(&cel->cel_entry);
  2549. esip->esi_nevents--;
  2550. if (esip->esi_queue_blocked &&
  2551. (esip->esi_nevents < evt_delivery_queue_resume)) {
  2552. esip->esi_queue_blocked = 0;
  2553. log_printf(LOG_LEVEL_DEBUG, "unblock\n");
  2554. }
  2555. edp = cel->cel_event;
  2556. edp->ed_event.led_lib_channel_handle = cel->cel_chan_handle;
  2557. edp->ed_event.led_sub_id = cel->cel_sub_id;
  2558. edp->ed_event.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2559. edp->ed_event.led_head.error = SA_AIS_OK;
  2560. free(cel);
  2561. api->ipc_conn_send_response(conn, &edp->ed_event,
  2562. edp->ed_event.led_head.size);
  2563. free_event_data(edp);
  2564. goto data_get_done;
  2565. }
  2566. }
  2567. res.led_head.size = sizeof(res.led_head);
  2568. res.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2569. res.led_head.error = SA_AIS_ERR_NOT_EXIST;
  2570. api->ipc_conn_send_response(conn, &res, res.led_head.size);
  2571. /*
  2572. * See if there are any events that the app doesn't know about
  2573. * because the notify pipe was full.
  2574. */
  2575. data_get_done:
  2576. if (esip->esi_nevents) {
  2577. __notify_event(conn);
  2578. }
  2579. }
  2580. /*
  2581. * Scan the list of channels and remove the specified node.
  2582. */
  2583. static void remove_chan_open_info(mar_uint32_t node_id)
  2584. {
  2585. struct list_head *l, *nxt;
  2586. struct event_svr_channel_instance *eci;
  2587. for (l = esc_head.next; l != &esc_head; l = nxt) {
  2588. nxt = l->next;
  2589. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  2590. remove_open_count(eci, node_id);
  2591. }
  2592. }
  2593. /*
  2594. * Called when there is a configuration change in the cluster.
  2595. * This function looks at any joiners and leavers and updates the evt
  2596. * node list. The node list is used to keep track of event IDs
  2597. * received for each node for the detection of duplicate events.
  2598. */
  2599. static void evt_conf_change(
  2600. enum totem_configuration_type configuration_type,
  2601. unsigned int *member_list, int member_list_entries,
  2602. unsigned int *left_list, int left_list_entries,
  2603. unsigned int *joined_list, int joined_list_entries,
  2604. struct memb_ring_id *ring_id)
  2605. {
  2606. log_printf(RECOVERY_DEBUG, "Evt conf change %d\n",
  2607. configuration_type);
  2608. log_printf(RECOVERY_DEBUG, "m %d, j %d, l %d\n",
  2609. member_list_entries,
  2610. joined_list_entries,
  2611. left_list_entries);
  2612. /*
  2613. * Save the various membership lists for later processing by
  2614. * the synchronization functions. The left list is only
  2615. * valid in the transitional configuration, the joined list is
  2616. * only valid in the regular configuration. Other than for the
  2617. * purposes of delivering retained events from merging partitions,
  2618. * we only care about the final membership from the regular
  2619. * configuration.
  2620. */
  2621. if (configuration_type == TOTEM_CONFIGURATION_TRANSITIONAL) {
  2622. left_member_count = left_list_entries;
  2623. trans_member_count = member_list_entries;
  2624. if (left_member_list) {
  2625. free(left_member_list);
  2626. left_member_list = 0;
  2627. }
  2628. if (left_list_entries) {
  2629. left_member_list =
  2630. malloc(sizeof(unsigned int) * left_list_entries);
  2631. if (!left_member_list) {
  2632. /*
  2633. * ERROR: No recovery.
  2634. */
  2635. log_printf(LOG_LEVEL_ERROR,
  2636. "Config change left list allocation error\n");
  2637. assert(0);
  2638. }
  2639. memcpy(left_member_list, left_list,
  2640. sizeof(unsigned int) * left_list_entries);
  2641. }
  2642. if (trans_member_list) {
  2643. free(trans_member_list);
  2644. trans_member_list = 0;
  2645. }
  2646. if (member_list_entries) {
  2647. trans_member_list =
  2648. malloc(sizeof(unsigned int) * member_list_entries);
  2649. if (!trans_member_list) {
  2650. /*
  2651. * ERROR: No recovery.
  2652. */
  2653. log_printf(LOG_LEVEL_ERROR,
  2654. "Config change transitional member list allocation error\n");
  2655. assert(0);
  2656. }
  2657. memcpy(trans_member_list, member_list,
  2658. sizeof(unsigned int) * member_list_entries);
  2659. }
  2660. }
  2661. if (configuration_type == TOTEM_CONFIGURATION_REGULAR) {
  2662. joined_member_count = joined_list_entries;
  2663. total_member_count = member_list_entries;
  2664. if (joined_member_list) {
  2665. free(joined_member_list);
  2666. joined_member_list = 0;
  2667. }
  2668. if (joined_list_entries) {
  2669. joined_member_list =
  2670. malloc(sizeof(unsigned int) * joined_list_entries);
  2671. if (!joined_member_list) {
  2672. /*
  2673. * ERROR: No recovery.
  2674. */
  2675. log_printf(LOG_LEVEL_ERROR,
  2676. "Config change joined list allocation error\n");
  2677. assert(0);
  2678. }
  2679. memcpy(joined_member_list, joined_list,
  2680. sizeof(unsigned int) * joined_list_entries);
  2681. }
  2682. if (current_member_list) {
  2683. free(current_member_list);
  2684. current_member_list = 0;
  2685. }
  2686. if (member_list_entries) {
  2687. current_member_list =
  2688. malloc(sizeof(unsigned int) * member_list_entries);
  2689. if (!current_member_list) {
  2690. /*
  2691. * ERROR: No recovery.
  2692. */
  2693. log_printf(LOG_LEVEL_ERROR,
  2694. "Config change member list allocation error\n");
  2695. assert(0);
  2696. }
  2697. memcpy(current_member_list, member_list,
  2698. sizeof(unsigned int) * member_list_entries);
  2699. }
  2700. }
  2701. }
  2702. /*
  2703. * saEvtFinalize Handler
  2704. */
  2705. static int evt_lib_exit(void *conn)
  2706. {
  2707. struct event_svr_channel_open *eco;
  2708. struct list_head *l, *nxt;
  2709. struct open_chan_pending *ocp;
  2710. struct unlink_chan_pending *ucp;
  2711. struct retention_time_clear_pending *rtc;
  2712. struct libevt_pd *esip =
  2713. api->ipc_private_data_get(api->ipc_conn_partner_get(conn));
  2714. log_printf(LOG_LEVEL_DEBUG, "saEvtFinalize (Event exit request)\n");
  2715. log_printf(LOG_LEVEL_DEBUG, "saEvtFinalize %d evts on list\n",
  2716. esip->esi_nevents);
  2717. /*
  2718. * Clean up any open channels and associated subscriptions.
  2719. */
  2720. for (l = esip->esi_open_chans.next; l != &esip->esi_open_chans; l = nxt) {
  2721. nxt = l->next;
  2722. eco = list_entry(l, struct event_svr_channel_open, eco_instance_entry);
  2723. common_chan_close(eco, esip);
  2724. hdb_handle_destroy(&esip->esi_hdb, eco->eco_my_handle);
  2725. }
  2726. /*
  2727. * Clean up any pending async operations
  2728. */
  2729. for (l = open_pending.next; l != &open_pending; l = nxt) {
  2730. nxt = l->next;
  2731. ocp = list_entry(l, struct open_chan_pending, ocp_entry);
  2732. if (esip == api->ipc_private_data_get(ocp->ocp_conn)) {
  2733. list_del(&ocp->ocp_entry);
  2734. free(ocp);
  2735. }
  2736. }
  2737. for (l = unlink_pending.next; l != &unlink_pending; l = nxt) {
  2738. nxt = l->next;
  2739. ucp = list_entry(l, struct unlink_chan_pending, ucp_entry);
  2740. if (esip == api->ipc_private_data_get(ucp->ucp_conn)) {
  2741. list_del(&ucp->ucp_entry);
  2742. free(ucp);
  2743. }
  2744. }
  2745. for (l = clear_pending.next;
  2746. l != &clear_pending; l = nxt) {
  2747. nxt = l->next;
  2748. rtc = list_entry(l, struct retention_time_clear_pending, rtc_entry);
  2749. if (esip == api->ipc_private_data_get(rtc->rtc_conn)) {
  2750. list_del(&rtc->rtc_entry);
  2751. free(rtc);
  2752. }
  2753. }
  2754. /*
  2755. * Destroy the open channel handle database
  2756. */
  2757. hdb_destroy(&esip->esi_hdb);
  2758. return 0;
  2759. }
  2760. /*
  2761. * Called at service start time.
  2762. */
  2763. static int evt_exec_init(struct corosync_api_v1 *corosync_api)
  2764. {
  2765. unsigned int object_service_handle;
  2766. char *value;
  2767. api = corosync_api;
  2768. log_printf(LOG_LEVEL_DEBUG, "Evt exec init request\n");
  2769. api->object_find_reset (OBJECT_PARENT_HANDLE);
  2770. if (api->object_find (
  2771. OBJECT_PARENT_HANDLE,
  2772. "event",
  2773. strlen ("event"),
  2774. &object_service_handle) == 0) {
  2775. value = NULL;
  2776. if ( !api->object_key_get (object_service_handle,
  2777. "delivery_queue_size",
  2778. strlen ("delivery_queue_size"),
  2779. (void *)&value,
  2780. NULL) && value) {
  2781. evt_delivery_queue_size = atoi(value);
  2782. log_printf(LOG_LEVEL_NOTICE,
  2783. "event delivery_queue_size set to %u\n",
  2784. evt_delivery_queue_size);
  2785. }
  2786. value = NULL;
  2787. if ( !api->object_key_get (object_service_handle,
  2788. "delivery_queue_resume",
  2789. strlen ("delivery_queue_resume"),
  2790. (void *)&value,
  2791. NULL) && value) {
  2792. evt_delivery_queue_resume = atoi(value);
  2793. log_printf(LOG_LEVEL_NOTICE,
  2794. "event delivery_queue_resume set to %u\n",
  2795. evt_delivery_queue_size);
  2796. }
  2797. }
  2798. /*
  2799. * Create an event to be sent when we have to drop messages
  2800. * for an application.
  2801. */
  2802. dropped_event_size = sizeof(*dropped_event) + sizeof(dropped_pattern);
  2803. dropped_event = malloc(dropped_event_size);
  2804. if (dropped_event == 0) {
  2805. log_printf(LOG_LEVEL_ERROR,
  2806. "Memory Allocation Failure, event service not started\n");
  2807. errno = ENOMEM;
  2808. return -1;
  2809. }
  2810. memset(dropped_event, 0, sizeof(*dropped_event) + sizeof(dropped_pattern));
  2811. dropped_event->ed_ref_count = 1;
  2812. list_init(&dropped_event->ed_retained);
  2813. dropped_event->ed_event.led_head.size =
  2814. sizeof(*dropped_event) + sizeof(dropped_pattern);
  2815. dropped_event->ed_event.led_head.error = SA_AIS_OK;
  2816. dropped_event->ed_event.led_priority = SA_EVT_HIGHEST_PRIORITY;
  2817. dropped_event->ed_event.led_chan_name = lost_chan;
  2818. dropped_event->ed_event.led_publisher_name = dropped_publisher;
  2819. dropped_event->ed_event.led_patterns_number = 1;
  2820. memcpy(&dropped_event->ed_event.led_body[0],
  2821. &dropped_pattern, sizeof(dropped_pattern));
  2822. return 0;
  2823. }
  2824. static int
  2825. try_deliver_event(struct event_data *evt,
  2826. struct event_svr_channel_instance *eci)
  2827. {
  2828. struct list_head *l, *l1;
  2829. struct event_svr_channel_open *eco;
  2830. struct event_svr_channel_subscr *ecs;
  2831. int delivered_event = 0;
  2832. /*
  2833. * Check open channels
  2834. */
  2835. for (l = eci->esc_open_chans.next; l != &eci->esc_open_chans; l = l->next) {
  2836. eco = list_entry(l, struct event_svr_channel_open, eco_entry);
  2837. /*
  2838. * See if enabled to receive
  2839. */
  2840. if (!(eco->eco_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  2841. continue;
  2842. }
  2843. /*
  2844. * Check subscriptions
  2845. */
  2846. for (l1 = eco->eco_subscr.next; l1 != &eco->eco_subscr; l1 = l1->next) {
  2847. ecs = list_entry(l1, struct event_svr_channel_subscr, ecs_entry);
  2848. /*
  2849. * Apply filter rules and deliver if patterns
  2850. * match filters.
  2851. * Only deliver one event per open channel
  2852. */
  2853. if (event_match(evt, ecs) == SA_AIS_OK) {
  2854. deliver_event(evt, eco, ecs);
  2855. delivered_event++;
  2856. break;
  2857. }
  2858. }
  2859. }
  2860. return delivered_event;
  2861. }
  2862. /*
  2863. * Receive the network event message and distribute it to local subscribers
  2864. */
  2865. static void evt_remote_evt(void *msg, unsigned int nodeid)
  2866. {
  2867. /*
  2868. * - retain events that have a retention time
  2869. * - Find assocated channel
  2870. * - Scan list of subscribers
  2871. * - Apply filters
  2872. * - Deliver events that pass the filter test
  2873. */
  2874. struct lib_event_data *evtpkt = msg;
  2875. struct event_svr_channel_instance *eci;
  2876. struct event_data *evt;
  2877. SaClmClusterNodeT *cn;
  2878. log_printf(LOG_LEVEL_DEBUG, "Remote event data received from nodeid %s\n",
  2879. api->totem_ifaces_print (nodeid));
  2880. /*
  2881. * See where the message came from so that we can set the
  2882. * publishing node id in the message before delivery.
  2883. */
  2884. #ifdef TODO
  2885. cn = main_clm_get_by_nodeid(nodeid);
  2886. if (!cn) {
  2887. /*
  2888. * Not sure how this can happen...
  2889. */
  2890. log_printf(LOG_LEVEL_DEBUG, "No cluster node data for nodeid %s\n",
  2891. api->totem_ifaces_print (nodeid));
  2892. errno = ENXIO;
  2893. return;
  2894. }
  2895. log_printf(LOG_LEVEL_DEBUG, "Cluster node ID %s name %s\n",
  2896. api->totem_ifaces_print (cn->nodeId), cn->nodeName.value);
  2897. evtpkt->led_publisher_node_id = nodeid;
  2898. evtpkt->led_nodeid = nodeid;
  2899. evtpkt->led_receive_time = clust_time_now();
  2900. #endif
  2901. if (evtpkt->led_chan_unlink_id != EVT_CHAN_ACTIVE) {
  2902. log_printf(CHAN_UNLINK_DEBUG,
  2903. "evt_remote_evt(0): chan %s, id 0x%llx\n",
  2904. evtpkt->led_chan_name.value,
  2905. (unsigned long long)evtpkt->led_chan_unlink_id);
  2906. }
  2907. eci = find_channel(&evtpkt->led_chan_name, evtpkt->led_chan_unlink_id);
  2908. /*
  2909. * We may have had some events that were already queued when an
  2910. * unlink happened, if we don't find the channel in the active list
  2911. * look for the last unlinked channel of the same name. If this channel
  2912. * is re-opened the messages will still be routed correctly because new
  2913. * active channel messages will be ordered behind the open.
  2914. */
  2915. if (!eci && (evtpkt->led_chan_unlink_id == EVT_CHAN_ACTIVE)) {
  2916. log_printf(CHAN_UNLINK_DEBUG,
  2917. "evt_remote_evt(1): chan %s, id 0x%llx\n",
  2918. evtpkt->led_chan_name.value,
  2919. (unsigned long long)evtpkt->led_chan_unlink_id);
  2920. eci = find_last_unlinked_channel(&evtpkt->led_chan_name);
  2921. }
  2922. /*
  2923. * We shouldn't normally see an event for a channel that we
  2924. * don't know about.
  2925. */
  2926. if (!eci) {
  2927. log_printf(LOG_LEVEL_DEBUG, "Channel %s doesn't exist\n",
  2928. evtpkt->led_chan_name.value);
  2929. return;
  2930. }
  2931. if (check_last_event(evtpkt, nodeid)) {
  2932. return;
  2933. }
  2934. evt = make_local_event(evtpkt, eci);
  2935. if (!evt) {
  2936. log_printf(LOG_LEVEL_WARNING,
  2937. "1Memory allocation error, can't deliver event\n");
  2938. return;
  2939. }
  2940. if (evt->ed_event.led_retention_time) {
  2941. retain_event(evt);
  2942. }
  2943. try_deliver_event(evt, eci);
  2944. free_event_data(evt);
  2945. }
  2946. /*
  2947. * Calculate the remaining retention time of a received event during recovery
  2948. */
  2949. inline mar_time_t calc_retention_time(mar_time_t retention,
  2950. mar_time_t received, mar_time_t now)
  2951. {
  2952. if ((received < now) && ((now - received) < retention)) {
  2953. return retention - (now - received);
  2954. } else {
  2955. return 0;
  2956. }
  2957. }
  2958. /*
  2959. * Receive a recovery network event message and save it in the retained list
  2960. */
  2961. static void evt_remote_recovery_evt(void *msg, unsigned int nodeid)
  2962. {
  2963. /*
  2964. * - calculate remaining retention time
  2965. * - Find assocated channel
  2966. * - Scan list of subscribers
  2967. * - Apply filters
  2968. * - Deliver events that pass the filter test
  2969. */
  2970. struct lib_event_data *evtpkt = msg;
  2971. struct event_svr_channel_instance *eci;
  2972. struct event_data *evt;
  2973. struct member_node_data *md;
  2974. int num_delivered;
  2975. mar_time_t now;
  2976. now = clust_time_now();
  2977. log_printf(RECOVERY_EVENT_DEBUG,
  2978. "Remote recovery event data received from nodeid %d\n", nodeid);
  2979. if (recovery_phase == evt_recovery_complete) {
  2980. log_printf(RECOVERY_EVENT_DEBUG,
  2981. "Received recovery data, not in recovery mode\n");
  2982. return;
  2983. }
  2984. log_printf(RECOVERY_EVENT_DEBUG,
  2985. "Processing recovery of retained events\n");
  2986. if (recovery_node) {
  2987. log_printf(RECOVERY_EVENT_DEBUG, "This node is the recovery node\n");
  2988. }
  2989. log_printf(RECOVERY_EVENT_DEBUG, "(1)EVT ID: %llx, Time: %llx\n",
  2990. (unsigned long long)evtpkt->led_event_id,
  2991. (unsigned long long)evtpkt->led_retention_time);
  2992. /*
  2993. * Calculate remaining retention time
  2994. */
  2995. evtpkt->led_retention_time = calc_retention_time(
  2996. evtpkt->led_retention_time,
  2997. evtpkt->led_receive_time,
  2998. now);
  2999. log_printf(RECOVERY_EVENT_DEBUG,
  3000. "(2)EVT ID: %llx, ret: %llx, rec: %llx, now: %llx\n",
  3001. (unsigned long long)evtpkt->led_event_id,
  3002. (unsigned long long)evtpkt->led_retention_time,
  3003. (unsigned long long)evtpkt->led_receive_time,
  3004. (unsigned long long)now);
  3005. /*
  3006. * If we haven't seen this event yet and it has remaining time, process
  3007. * the event.
  3008. */
  3009. if (!check_last_event(evtpkt, evtpkt->led_nodeid) &&
  3010. evtpkt->led_retention_time) {
  3011. /*
  3012. * See where the message came from so that we can set the
  3013. * publishing node id in the message before delivery.
  3014. */
  3015. md = evt_find_node(evtpkt->led_nodeid);
  3016. if (!md) {
  3017. /*
  3018. * Not sure how this can happen
  3019. */
  3020. log_printf(LOG_LEVEL_NOTICE, "No node for nodeid %s\n",
  3021. api->totem_ifaces_print (evtpkt->led_nodeid));
  3022. return;
  3023. }
  3024. log_printf(LOG_LEVEL_DEBUG, "Cluster node ID %s name %s\n",
  3025. api->totem_ifaces_print (md->mn_node_info.nodeId),
  3026. md->mn_node_info.nodeName.value);
  3027. log_printf(CHAN_UNLINK_DEBUG,
  3028. "evt_recovery_event: chan %s, id 0x%llx\n",
  3029. evtpkt->led_chan_name.value,
  3030. (unsigned long long)evtpkt->led_chan_unlink_id);
  3031. eci = find_channel(&evtpkt->led_chan_name, evtpkt->led_chan_unlink_id);
  3032. /*
  3033. * We shouldn't normally see an event for a channel that we don't
  3034. * know about.
  3035. */
  3036. if (!eci) {
  3037. log_printf(RECOVERY_EVENT_DEBUG, "Channel %s doesn't exist\n",
  3038. evtpkt->led_chan_name.value);
  3039. return;
  3040. }
  3041. evt = make_local_event(evtpkt, eci);
  3042. if (!evt) {
  3043. log_printf(LOG_LEVEL_WARNING,
  3044. "2Memory allocation error, can't deliver event\n");
  3045. errno = ENOMEM;
  3046. return;
  3047. }
  3048. retain_event(evt);
  3049. num_delivered = try_deliver_event(evt, eci);
  3050. log_printf(RECOVERY_EVENT_DEBUG, "Delivered to %d subscribers\n",
  3051. num_delivered);
  3052. free_event_data(evt);
  3053. }
  3054. }
  3055. /*
  3056. * Timeout handler for event channel open. We flag the structure
  3057. * as timed out. Then if the open request is ever returned, we can
  3058. * issue a close channel and keep the reference counting correct.
  3059. */
  3060. static void chan_open_timeout(void *data)
  3061. {
  3062. struct open_chan_pending *ocp = (struct open_chan_pending *)data;
  3063. struct res_evt_channel_open res;
  3064. res.ico_head.size = sizeof(res);
  3065. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  3066. res.ico_head.error = SA_AIS_ERR_TIMEOUT;
  3067. ocp->ocp_invocation = OPEN_TIMED_OUT;
  3068. api->ipc_conn_send_response(ocp->ocp_conn, &res, sizeof(res));
  3069. }
  3070. /*
  3071. * Called by the channel open exec handler to finish the open and
  3072. * respond to the application
  3073. */
  3074. static void evt_chan_open_finish(struct open_chan_pending *ocp,
  3075. struct event_svr_channel_instance *eci)
  3076. {
  3077. struct event_svr_channel_open *eco;
  3078. SaAisErrorT error = SA_AIS_OK;
  3079. unsigned int ret = 0;
  3080. unsigned int timer_del_status = 0;
  3081. void *ptr = 0;
  3082. uint32_t handle = 0;
  3083. struct libevt_pd *esip;
  3084. esip = (struct libevt_pd *)api->ipc_private_data_get(ocp->ocp_conn);
  3085. log_printf(CHAN_OPEN_DEBUG, "Open channel finish %s\n",
  3086. get_mar_name_t(&ocp->ocp_chan_name));
  3087. if (ocp->ocp_timer_handle) {
  3088. api->timer_delete (ocp->ocp_timer_handle);
  3089. }
  3090. /*
  3091. * If this is a finished open for a timed out request, then
  3092. * send out a close on this channel to clean things up.
  3093. */
  3094. if (ocp->ocp_invocation == OPEN_TIMED_OUT) {
  3095. log_printf(CHAN_OPEN_DEBUG, "Closing timed out open of %s\n",
  3096. get_mar_name_t(&ocp->ocp_chan_name));
  3097. error = evt_close_channel(&ocp->ocp_chan_name, EVT_CHAN_ACTIVE);
  3098. if (error != SA_AIS_OK) {
  3099. log_printf(CHAN_OPEN_DEBUG,
  3100. "Close of timed out open failed for %s\n",
  3101. get_mar_name_t(&ocp->ocp_chan_name));
  3102. }
  3103. list_del(&ocp->ocp_entry);
  3104. free(ocp);
  3105. return;
  3106. }
  3107. /*
  3108. * Create a handle to give back to the caller to associate
  3109. * with this channel open instance.
  3110. */
  3111. ret = hdb_handle_create(&esip->esi_hdb, sizeof(*eco), &handle);
  3112. if (ret != 0) {
  3113. goto open_return;
  3114. }
  3115. ret = hdb_handle_get(&esip->esi_hdb, handle, &ptr);
  3116. if (ret != 0) {
  3117. goto open_return;
  3118. }
  3119. eco = ptr;
  3120. /*
  3121. * Initailize and link into the global channel structure.
  3122. */
  3123. list_init(&eco->eco_subscr);
  3124. list_init(&eco->eco_entry);
  3125. list_init(&eco->eco_instance_entry);
  3126. eco->eco_flags = ocp->ocp_open_flag;
  3127. eco->eco_channel = eci;
  3128. eco->eco_lib_handle = ocp->ocp_c_handle;
  3129. eco->eco_my_handle = handle;
  3130. eco->eco_conn = ocp->ocp_conn;
  3131. list_add_tail(&eco->eco_entry, &eci->esc_open_chans);
  3132. list_add_tail(&eco->eco_instance_entry, &esip->esi_open_chans);
  3133. /*
  3134. * respond back with a handle to access this channel
  3135. * open instance for later subscriptions, etc.
  3136. */
  3137. hdb_handle_put(&esip->esi_hdb, handle);
  3138. open_return:
  3139. log_printf(CHAN_OPEN_DEBUG, "Open channel finish %s send response %d\n",
  3140. get_mar_name_t(&ocp->ocp_chan_name),
  3141. error);
  3142. if (ocp->ocp_async) {
  3143. struct res_evt_open_chan_async resa;
  3144. resa.ica_head.size = sizeof(resa);
  3145. resa.ica_head.id = MESSAGE_RES_EVT_CHAN_OPEN_CALLBACK;
  3146. resa.ica_head.error = (ret == 0 ? SA_AIS_OK: SA_AIS_ERR_BAD_HANDLE);
  3147. resa.ica_channel_handle = handle;
  3148. resa.ica_c_handle = ocp->ocp_c_handle;
  3149. resa.ica_invocation = ocp->ocp_invocation;
  3150. api->ipc_conn_send_response(api->ipc_conn_partner_get(ocp->ocp_conn),
  3151. &resa, sizeof(resa));
  3152. } else {
  3153. struct res_evt_channel_open res;
  3154. res.ico_head.size = sizeof(res);
  3155. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  3156. res.ico_head.error = (ret == 0 ? SA_AIS_OK : SA_AIS_ERR_BAD_HANDLE);
  3157. res.ico_channel_handle = handle;
  3158. api->ipc_conn_send_response(ocp->ocp_conn, &res, sizeof(res));
  3159. }
  3160. if (timer_del_status == 0) {
  3161. list_del(&ocp->ocp_entry);
  3162. free(ocp);
  3163. }
  3164. }
  3165. /*
  3166. * Called by the channel unlink exec handler to
  3167. * respond to the application.
  3168. */
  3169. static void evt_chan_unlink_finish(struct unlink_chan_pending *ucp)
  3170. {
  3171. struct res_evt_channel_unlink res;
  3172. log_printf(CHAN_UNLINK_DEBUG, "Unlink channel finish ID 0x%llx\n",
  3173. (unsigned long long)ucp->ucp_unlink_id);
  3174. list_del(&ucp->ucp_entry);
  3175. res.iuc_head.size = sizeof(res);
  3176. res.iuc_head.id = MESSAGE_RES_EVT_UNLINK_CHANNEL;
  3177. res.iuc_head.error = SA_AIS_OK;
  3178. api->ipc_conn_send_response(ucp->ucp_conn, &res, sizeof(res));
  3179. free(ucp);
  3180. }
  3181. /*
  3182. * Called by the retention time clear exec handler to
  3183. * respond to the application.
  3184. */
  3185. static void evt_ret_time_clr_finish(struct retention_time_clear_pending *rtc,
  3186. SaAisErrorT ret)
  3187. {
  3188. struct res_evt_event_clear_retentiontime res;
  3189. log_printf(RETENTION_TIME_DEBUG, "Retention Time Clear finish ID 0x%llx\n",
  3190. (unsigned long long)rtc->rtc_event_id);
  3191. res.iec_head.size = sizeof(res);
  3192. res.iec_head.id = MESSAGE_RES_EVT_CLEAR_RETENTIONTIME;
  3193. res.iec_head.error = ret;
  3194. api->ipc_conn_send_response(rtc->rtc_conn, &res, sizeof(res));
  3195. list_del(&rtc->rtc_entry);
  3196. free(rtc);
  3197. }
  3198. /*
  3199. * Take the channel command data and swap the elements so they match
  3200. * our architectures word order.
  3201. */
  3202. static void
  3203. convert_chan_packet(void *msg)
  3204. {
  3205. struct req_evt_chan_command *cpkt = (struct req_evt_chan_command *)msg;
  3206. /*
  3207. * converted in the main deliver_fn:
  3208. * led_head.id, led_head.size.
  3209. *
  3210. */
  3211. cpkt->chc_op = swab32(cpkt->chc_op);
  3212. /*
  3213. * Which elements of the packet that are converted depend
  3214. * on the operation.
  3215. */
  3216. switch (cpkt->chc_op) {
  3217. case EVT_OPEN_CHAN_OP:
  3218. swab_mar_name_t (&cpkt->u.chc_chan.ocr_name);
  3219. cpkt->u.chc_chan.ocr_serial_no = swab64(cpkt->u.chc_chan.ocr_serial_no);
  3220. break;
  3221. case EVT_UNLINK_CHAN_OP:
  3222. case EVT_CLOSE_CHAN_OP:
  3223. swab_mar_name_t (&cpkt->u.chcu.chcu_name);
  3224. cpkt->u.chcu.chcu_unlink_id = swab64(cpkt->u.chcu.chcu_unlink_id);
  3225. break;
  3226. case EVT_CLEAR_RET_OP:
  3227. cpkt->u.chc_event_id = swab64(cpkt->u.chc_event_id);
  3228. break;
  3229. case EVT_SET_ID_OP:
  3230. cpkt->u.chc_set_id.chc_nodeid =
  3231. swab32(cpkt->u.chc_set_id.chc_nodeid);
  3232. cpkt->u.chc_set_id.chc_last_id = swab64(cpkt->u.chc_set_id.chc_last_id);
  3233. break;
  3234. case EVT_OPEN_COUNT:
  3235. swab_mar_name_t (&cpkt->u.chc_set_opens.chc_chan_name);
  3236. cpkt->u.chc_set_opens.chc_open_count =
  3237. swab32(cpkt->u.chc_set_opens.chc_open_count);
  3238. break;
  3239. /*
  3240. * No data assocaited with these ops.
  3241. */
  3242. case EVT_CONF_DONE:
  3243. case EVT_OPEN_COUNT_DONE:
  3244. break;
  3245. /*
  3246. * Make sure that this function is updated when new ops are added.
  3247. */
  3248. default:
  3249. assert(0);
  3250. }
  3251. }
  3252. /*
  3253. * Receive and process remote event operations.
  3254. * Used to communicate channel opens/closes, clear retention time,
  3255. * config change updates...
  3256. */
  3257. static void evt_remote_chan_op(void *msg, unsigned int nodeid)
  3258. {
  3259. struct req_evt_chan_command *cpkt = msg;
  3260. unsigned int local_node = {SA_CLM_LOCAL_NODE_ID};
  3261. SaClmClusterNodeT *cn, *my_node;
  3262. struct member_node_data *mn;
  3263. struct event_svr_channel_instance *eci;
  3264. #ifdef TODO
  3265. log_printf(REMOTE_OP_DEBUG, "Remote channel operation request\n");
  3266. my_node = main_clm_get_by_nodeid(local_node);
  3267. log_printf(REMOTE_OP_DEBUG, "my node ID: 0x%x\n", my_node->nodeId);
  3268. mn = evt_find_node(nodeid);
  3269. if (mn == NULL) {
  3270. cn = main_clm_get_by_nodeid(nodeid);
  3271. if (cn == NULL) {
  3272. log_printf(LOG_LEVEL_WARNING,
  3273. "Evt remote channel op: Node data for nodeid %d is NULL\n",
  3274. nodeid);
  3275. return;
  3276. } else {
  3277. evt_add_node(nodeid, cn);
  3278. mn = evt_find_node(nodeid);
  3279. }
  3280. }
  3281. #endif
  3282. switch (cpkt->chc_op) {
  3283. /*
  3284. * Open channel remote command. The open channel request is done
  3285. * in two steps. When an pplication tries to open, we send an open
  3286. * channel message to the other nodes. When we receive an open channel
  3287. * message, we may create the channel structure if it doesn't exist
  3288. * and also complete applicaiton open requests for the specified
  3289. * channel. We keep a counter of total opens for the given channel and
  3290. * later when it has been completely closed (everywhere in the cluster)
  3291. * we will free up the assocated channel data.
  3292. */
  3293. case EVT_OPEN_CHAN_OP: {
  3294. struct open_chan_pending *ocp;
  3295. struct list_head *l, *nxt;
  3296. log_printf(CHAN_OPEN_DEBUG, "Opening channel %s for node %s\n",
  3297. cpkt->u.chc_chan.ocr_name.value,
  3298. api->totem_ifaces_print (mn->mn_node_info.nodeId));
  3299. eci = find_channel(&cpkt->u.chc_chan.ocr_name, EVT_CHAN_ACTIVE);
  3300. if (!eci) {
  3301. eci = create_channel(&cpkt->u.chc_chan.ocr_name);
  3302. }
  3303. if (!eci) {
  3304. log_printf(LOG_LEVEL_WARNING, "Could not create channel %s\n",
  3305. get_mar_name_t(&cpkt->u.chc_chan.ocr_name));
  3306. break;
  3307. }
  3308. inc_open_count(eci, mn->mn_node_info.nodeId);
  3309. if (mn->mn_node_info.nodeId == my_node->nodeId) {
  3310. /*
  3311. * Complete one of our pending open requests
  3312. */
  3313. for (l = open_pending.next; l != &open_pending; l = nxt) {
  3314. nxt = l->next;
  3315. ocp = list_entry(l, struct open_chan_pending, ocp_entry);
  3316. log_printf(CHAN_OPEN_DEBUG,
  3317. "Compare channel req no %llu %llu\n",
  3318. (unsigned long long)ocp->ocp_serial_no,
  3319. (unsigned long long)cpkt->u.chc_chan.ocr_serial_no);
  3320. if (ocp->ocp_serial_no == cpkt->u.chc_chan.ocr_serial_no) {
  3321. evt_chan_open_finish(ocp, eci);
  3322. break;
  3323. }
  3324. }
  3325. }
  3326. log_printf(CHAN_OPEN_DEBUG,
  3327. "Open channel %s t %d, l %d, r %d\n",
  3328. get_mar_name_t(&eci->esc_channel_name),
  3329. eci->esc_total_opens, eci->esc_local_opens,
  3330. eci->esc_retained_count);
  3331. break;
  3332. }
  3333. /*
  3334. * Handle a channel close. We'll decrement the global open counter and
  3335. * free up channel associated data when all instances are closed.
  3336. */
  3337. case EVT_CLOSE_CHAN_OP:
  3338. log_printf(LOG_LEVEL_DEBUG, "Closing channel %s for node 0x%x\n",
  3339. cpkt->u.chcu.chcu_name.value, mn->mn_node_info.nodeId);
  3340. eci = find_channel(&cpkt->u.chcu.chcu_name, cpkt->u.chcu.chcu_unlink_id);
  3341. if (!eci) {
  3342. log_printf(LOG_LEVEL_NOTICE,
  3343. "Channel close request for %s not found\n",
  3344. cpkt->u.chcu.chcu_name.value);
  3345. break;
  3346. }
  3347. /*
  3348. * if last instance, we can free up assocated data.
  3349. */
  3350. dec_open_count(eci, mn->mn_node_info.nodeId);
  3351. log_printf(LOG_LEVEL_DEBUG,
  3352. "Close channel %s t %d, l %d, r %d\n",
  3353. eci->esc_channel_name.value,
  3354. eci->esc_total_opens, eci->esc_local_opens,
  3355. eci->esc_retained_count);
  3356. delete_channel(eci);
  3357. break;
  3358. /*
  3359. * Handle a request for channel unlink saEvtChannelUnlink().
  3360. * We'll look up the channel and mark it as unlinked. Respond to
  3361. * local library unlink commands.
  3362. */
  3363. case EVT_UNLINK_CHAN_OP: {
  3364. struct unlink_chan_pending *ucp;
  3365. struct list_head *l, *nxt;
  3366. log_printf(CHAN_UNLINK_DEBUG,
  3367. "Unlink request channel %s unlink ID 0x%llx from node 0x%x\n",
  3368. cpkt->u.chcu.chcu_name.value,
  3369. (unsigned long long)cpkt->u.chcu.chcu_unlink_id,
  3370. mn->mn_node_info.nodeId);
  3371. /*
  3372. * look up the channel name and get its assoicated data.
  3373. */
  3374. eci = find_channel(&cpkt->u.chcu.chcu_name,
  3375. EVT_CHAN_ACTIVE);
  3376. if (!eci) {
  3377. log_printf(LOG_LEVEL_NOTICE,
  3378. "Channel unlink request for %s not found\n",
  3379. cpkt->u.chcu.chcu_name.value);
  3380. } else {
  3381. /*
  3382. * Mark channel as unlinked.
  3383. */
  3384. unlink_channel(eci, cpkt->u.chcu.chcu_unlink_id);
  3385. }
  3386. /*
  3387. * respond only to local library requests.
  3388. */
  3389. if (mn->mn_node_info.nodeId == my_node->nodeId) {
  3390. /*
  3391. * Complete one of our pending unlink requests
  3392. */
  3393. for (l = unlink_pending.next; l != &unlink_pending; l = nxt) {
  3394. nxt = l->next;
  3395. ucp = list_entry(l, struct unlink_chan_pending, ucp_entry);
  3396. log_printf(CHAN_UNLINK_DEBUG,
  3397. "Compare channel id 0x%llx 0x%llx\n",
  3398. (unsigned long long)ucp->ucp_unlink_id,
  3399. (unsigned long long)cpkt->u.chcu.chcu_unlink_id);
  3400. if (ucp->ucp_unlink_id == cpkt->u.chcu.chcu_unlink_id) {
  3401. evt_chan_unlink_finish(ucp);
  3402. break;
  3403. }
  3404. }
  3405. }
  3406. break;
  3407. }
  3408. /*
  3409. * saEvtClearRetentionTime handler.
  3410. */
  3411. case EVT_CLEAR_RET_OP:
  3412. {
  3413. SaAisErrorT did_clear;
  3414. struct retention_time_clear_pending *rtc;
  3415. struct list_head *l, *nxt;
  3416. log_printf(RETENTION_TIME_DEBUG, "Clear retention time request %llx\n",
  3417. (unsigned long long)cpkt->u.chc_event_id);
  3418. did_clear = clear_retention_time(cpkt->u.chc_event_id);
  3419. /*
  3420. * Respond to local library requests
  3421. */
  3422. if (mn->mn_node_info.nodeId == my_node->nodeId) {
  3423. /*
  3424. * Complete pending request
  3425. */
  3426. for (l = clear_pending.next; l != &clear_pending; l = nxt) {
  3427. nxt = l->next;
  3428. rtc = list_entry(l, struct retention_time_clear_pending,
  3429. rtc_entry);
  3430. if (rtc->rtc_event_id == cpkt->u.chc_event_id) {
  3431. evt_ret_time_clr_finish(rtc, did_clear);
  3432. break;
  3433. }
  3434. }
  3435. }
  3436. break;
  3437. }
  3438. /*
  3439. * Set our next event ID based on the largest event ID seen
  3440. * by others in the cluster. This way, if we've left and re-joined, we'll
  3441. * start using an event ID that is unique.
  3442. */
  3443. case EVT_SET_ID_OP: {
  3444. int log_level = LOG_LEVEL_DEBUG;
  3445. if (cpkt->u.chc_set_id.chc_nodeid == my_node->nodeId) {
  3446. log_level = RECOVERY_DEBUG;
  3447. }
  3448. log_printf(log_level,
  3449. "Received Set event ID OP from nodeid %x to %llx for %x my addr %s base %llx\n",
  3450. nodeid,
  3451. (unsigned long long)cpkt->u.chc_set_id.chc_last_id,
  3452. cpkt->u.chc_set_id.chc_nodeid,
  3453. api->totem_ifaces_print (my_node->nodeId),
  3454. (unsigned long long)base_id);
  3455. if (cpkt->u.chc_set_id.chc_nodeid == my_node->nodeId) {
  3456. if (cpkt->u.chc_set_id.chc_last_id >= base_id) {
  3457. log_printf(RECOVERY_DEBUG,
  3458. "Set event ID from nodeid %s to %llx\n",
  3459. api->totem_ifaces_print (nodeid),
  3460. (unsigned long long)cpkt->u.chc_set_id.chc_last_id);
  3461. base_id = cpkt->u.chc_set_id.chc_last_id + 1;
  3462. }
  3463. }
  3464. break;
  3465. }
  3466. /*
  3467. * Receive the open count for a particular channel during recovery.
  3468. * This insures that everyone has the same notion of who has a channel
  3469. * open so that it can be removed when no one else has it open anymore.
  3470. */
  3471. case EVT_OPEN_COUNT:
  3472. if (recovery_phase == evt_recovery_complete) {
  3473. log_printf(LOG_LEVEL_ERROR,
  3474. "Evt open count msg from nodeid %s, but not in membership change\n",
  3475. api->totem_ifaces_print (nodeid));
  3476. }
  3477. /*
  3478. * Zero out all open counts because we're setting then based
  3479. * on each nodes local counts.
  3480. */
  3481. if (!processed_open_counts) {
  3482. zero_chan_open_counts();
  3483. processed_open_counts = 1;
  3484. }
  3485. log_printf(RECOVERY_DEBUG,
  3486. "Open channel count %s is %d for node %s\n",
  3487. cpkt->u.chc_set_opens.chc_chan_name.value,
  3488. cpkt->u.chc_set_opens.chc_open_count,
  3489. api->totem_ifaces_print (mn->mn_node_info.nodeId));
  3490. eci = find_channel(&cpkt->u.chc_set_opens.chc_chan_name,
  3491. EVT_CHAN_ACTIVE);
  3492. if (!eci) {
  3493. eci = create_channel(&cpkt->u.chc_set_opens.chc_chan_name);
  3494. }
  3495. if (!eci) {
  3496. log_printf(LOG_LEVEL_WARNING, "Could not create channel %s\n",
  3497. get_mar_name_t(&cpkt->u.chc_set_opens.chc_chan_name));
  3498. break;
  3499. }
  3500. if (set_open_count(eci, mn->mn_node_info.nodeId,
  3501. cpkt->u.chc_set_opens.chc_open_count)) {
  3502. log_printf(LOG_LEVEL_ERROR,
  3503. "Error setting Open channel count %s for node %s\n",
  3504. cpkt->u.chc_set_opens.chc_chan_name.value,
  3505. api->totem_ifaces_print (mn->mn_node_info.nodeId));
  3506. }
  3507. break;
  3508. /*
  3509. * Once we get all the messages from
  3510. * the current membership, determine who delivers any retained events.
  3511. */
  3512. case EVT_OPEN_COUNT_DONE: {
  3513. if (recovery_phase == evt_recovery_complete) {
  3514. log_printf(LOG_LEVEL_ERROR,
  3515. "Evt config msg from nodeid %s, but not in membership change\n",
  3516. api->totem_ifaces_print (nodeid));
  3517. }
  3518. log_printf(RECOVERY_DEBUG,
  3519. "Receive EVT_CONF_CHANGE_DONE from nodeid %s members %d checked in %d\n",
  3520. api->totem_ifaces_print (nodeid), total_member_count, checked_in+1);
  3521. if (!mn) {
  3522. log_printf(RECOVERY_DEBUG,
  3523. "NO NODE DATA AVAILABLE FOR nodeid %s\n", api->totem_ifaces_print (nodeid));
  3524. }
  3525. if (++checked_in == total_member_count) {
  3526. /*
  3527. * We're all here, now figure out who should send the
  3528. * retained events.
  3529. */
  3530. mn = oldest_node();
  3531. if (mn && mn->mn_node_info.nodeId == my_node_id) {
  3532. log_printf(RECOVERY_DEBUG,
  3533. "I am oldest in my transitional config\n");
  3534. recovery_node = 1;
  3535. recovery_phase = evt_send_retained_events;
  3536. } else {
  3537. recovery_phase = evt_send_retained_events_done;
  3538. recovery_node = 0;
  3539. }
  3540. checked_in = 0;
  3541. }
  3542. break;
  3543. }
  3544. /*
  3545. * Count up the nodes again, when all the nodes have responded, we've
  3546. * distributed the retained events and we're done with recovery and can
  3547. * continue operations.
  3548. */
  3549. case EVT_CONF_DONE: {
  3550. log_printf(RECOVERY_DEBUG,
  3551. "Receive EVT_CONF_DONE from nodeid %s, members %d checked in %d\n",
  3552. api->totem_ifaces_print (nodeid),
  3553. total_member_count, checked_in+1);
  3554. if (++checked_in == total_member_count) {
  3555. /*
  3556. * All recovery complete, carry on.
  3557. */
  3558. recovery_phase = evt_recovery_complete;
  3559. #ifdef DUMP_CHAN_INFO
  3560. dump_all_chans();
  3561. #endif
  3562. }
  3563. break;
  3564. }
  3565. default:
  3566. log_printf(LOG_LEVEL_NOTICE, "Invalid channel operation %d\n",
  3567. cpkt->chc_op);
  3568. break;
  3569. }
  3570. }
  3571. /*
  3572. * Set up initial conditions for processing event service
  3573. * recovery.
  3574. */
  3575. static void evt_sync_init(void)
  3576. {
  3577. SaClmClusterNodeT *cn;
  3578. struct member_node_data *md;
  3579. unsigned int my_node = {SA_CLM_LOCAL_NODE_ID};
  3580. int left_list_entries = left_member_count;
  3581. unsigned int *left_list = left_member_list;
  3582. log_printf(RECOVERY_DEBUG, "Evt synchronize initialization\n");
  3583. /*
  3584. * Set the base event id
  3585. */
  3586. #ifdef TODO
  3587. if (!my_node_id) {
  3588. cn = main_clm_get_by_nodeid(my_node);
  3589. log_printf(RECOVERY_DEBUG, "My node ID %s\n",
  3590. api->totem_ifaces_print (cn->nodeId));
  3591. my_node_id = cn->nodeId;
  3592. set_event_id(my_node_id);
  3593. }
  3594. #endif
  3595. /*
  3596. * account for nodes that left the membership
  3597. */
  3598. while (left_list_entries--) {
  3599. md = evt_find_node(*left_list);
  3600. if (md == 0) {
  3601. log_printf(LOG_LEVEL_WARNING,
  3602. "Can't find cluster node at %s\n",
  3603. api->totem_ifaces_print (*left_list));
  3604. /*
  3605. * Mark this one as down.
  3606. */
  3607. } else {
  3608. log_printf(RECOVERY_DEBUG, "cluster node at %s down\n",
  3609. api->totem_ifaces_print(*left_list));
  3610. md->mn_started = 0;
  3611. remove_chan_open_info(md->mn_node_info.nodeId);
  3612. }
  3613. left_list++;
  3614. }
  3615. /*
  3616. * set up for recovery processing, first phase:
  3617. */
  3618. recovery_phase = evt_send_event_id;
  3619. /*
  3620. * List used to distribute last know event IDs.
  3621. */
  3622. add_list = current_member_list;
  3623. add_count = total_member_count;
  3624. processed_open_counts = 0;
  3625. /*
  3626. * List used for distributing open channel counts
  3627. */
  3628. next_chan = esc_head.next;
  3629. /*
  3630. * List used for distributing retained events
  3631. */
  3632. next_retained = retained_list.next;
  3633. /*
  3634. * Member check in counts for open channel counts and
  3635. * retained events.
  3636. */
  3637. checked_in = 0;
  3638. }
  3639. /*
  3640. * Handle event service recovery. It passes through a number of states to
  3641. * finish the recovery.
  3642. *
  3643. * First, the node broadcasts the highest event ID that it has seen for any
  3644. * joinig node. This helps to make sure that rejoining nodes don't re-use
  3645. * event IDs that have already been seen.
  3646. *
  3647. * Next, The node broadcasts its open channel information to the other nodes.
  3648. * This makes sure that any joining nodes have complete data on any channels
  3649. * already open.
  3650. *
  3651. * Once done sending open channel information the node waits in a state for
  3652. * the rest of the nodes to finish sending their data. When the last node
  3653. * has checked in, then the remote channel operation handler selects the next
  3654. * state which is evt_send_retained_events if this is the oldest node in the
  3655. * cluster, or otherwise to evt_wait_send_retained_events to wait for the
  3656. * retained events to be sent. When the retained events have been sent, the
  3657. * state is changed to evt_recovery_complete and this function exits with
  3658. * zero to inidicate that recovery is done.
  3659. */
  3660. static int evt_sync_process(void)
  3661. {
  3662. log_printf(RECOVERY_DEBUG, "Process Evt synchronization \n");
  3663. switch (recovery_phase) {
  3664. /*
  3665. * Send last know event ID to joining nodes to prevent duplicate
  3666. * event IDs.
  3667. */
  3668. case evt_send_event_id:
  3669. {
  3670. struct member_node_data *md;
  3671. SaClmClusterNodeT *cn;
  3672. struct req_evt_chan_command cpkt;
  3673. struct iovec chn_iovec;
  3674. int res;
  3675. log_printf(RECOVERY_DEBUG, "Send max event ID updates\n");
  3676. while (add_count) {
  3677. /*
  3678. * If we've seen this node before, send out the last msg ID
  3679. * that we've seen from him. He will set his base ID for
  3680. * generating event and message IDs to the highest one seen.
  3681. */
  3682. md = evt_find_node(*add_list);
  3683. if (md != NULL) {
  3684. log_printf(RECOVERY_DEBUG,
  3685. "Send set evt ID %llx to %s\n",
  3686. (unsigned long long)md->mn_last_msg_id,
  3687. api->totem_ifaces_print (*add_list));
  3688. md->mn_started = 1;
  3689. memset(&cpkt, 0, sizeof(cpkt));
  3690. cpkt.chc_head.id =
  3691. SERVICE_ID_MAKE (EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  3692. cpkt.chc_head.size = sizeof(cpkt);
  3693. cpkt.chc_op = EVT_SET_ID_OP;
  3694. cpkt.u.chc_set_id.chc_nodeid = *add_list;
  3695. cpkt.u.chc_set_id.chc_last_id = md->mn_last_msg_id;
  3696. chn_iovec.iov_base = (char *)&cpkt;
  3697. chn_iovec.iov_len = cpkt.chc_head.size;
  3698. res = api->totem_mcast(&chn_iovec, 1, TOTEM_AGREED);
  3699. if (res != 0) {
  3700. log_printf(RECOVERY_DEBUG,
  3701. "Unable to send event id to %s\n",
  3702. api->totem_ifaces_print (*add_list));
  3703. /*
  3704. * We'll try again later.
  3705. */
  3706. return 1;
  3707. }
  3708. } else {
  3709. /*
  3710. * Not seen before, add it to our list of nodes.
  3711. */
  3712. #ifdef TODO
  3713. cn = main_clm_get_by_nodeid(*add_list);
  3714. if (!cn) {
  3715. /*
  3716. * Error: shouldn't happen
  3717. */
  3718. log_printf(LOG_LEVEL_ERROR,
  3719. "recovery error node: %s not found\n",
  3720. api->totem_ifaces_print (*add_list));
  3721. assert(0);
  3722. } else {
  3723. evt_add_node(*add_list, cn);
  3724. }
  3725. #endif
  3726. }
  3727. add_list++;
  3728. add_count--;
  3729. }
  3730. recovery_phase = evt_send_open_count;
  3731. return 1;
  3732. }
  3733. /*
  3734. * Send channel open counts so all members have the same channel open
  3735. * counts.
  3736. */
  3737. case evt_send_open_count:
  3738. {
  3739. struct req_evt_chan_command cpkt;
  3740. struct iovec chn_iovec;
  3741. struct event_svr_channel_instance *eci;
  3742. int res;
  3743. log_printf(RECOVERY_DEBUG, "Send open count updates\n");
  3744. /*
  3745. * Process messages. When we're done, send the done message
  3746. * to the nodes.
  3747. */
  3748. memset(&cpkt, 0, sizeof(cpkt));
  3749. for (;next_chan != &esc_head;
  3750. next_chan = next_chan->next) {
  3751. log_printf(RECOVERY_DEBUG, "Sending next open count\n");
  3752. eci = list_entry(next_chan, struct event_svr_channel_instance,
  3753. esc_entry);
  3754. cpkt.chc_head.id =
  3755. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  3756. cpkt.chc_head.size = sizeof(cpkt);
  3757. cpkt.chc_op = EVT_OPEN_COUNT;
  3758. cpkt.u.chc_set_opens.chc_chan_name = eci->esc_channel_name;
  3759. cpkt.u.chc_set_opens.chc_open_count = eci->esc_local_opens;
  3760. chn_iovec.iov_base = (char *)&cpkt;
  3761. chn_iovec.iov_len = cpkt.chc_head.size;
  3762. res = api->totem_mcast (&chn_iovec, 1, TOTEM_AGREED);
  3763. if (res != 0) {
  3764. /*
  3765. * Try again later.
  3766. */
  3767. return 1;
  3768. }
  3769. }
  3770. memset(&cpkt, 0, sizeof(cpkt));
  3771. cpkt.chc_head.id =
  3772. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  3773. cpkt.chc_head.size = sizeof(cpkt);
  3774. cpkt.chc_op = EVT_OPEN_COUNT_DONE;
  3775. chn_iovec.iov_base = (char *)&cpkt;
  3776. chn_iovec.iov_len = cpkt.chc_head.size;
  3777. res = api->totem_mcast (&chn_iovec, 1, TOTEM_AGREED);
  3778. if (res != 0) {
  3779. /*
  3780. * Try again later.
  3781. */
  3782. return 1;
  3783. }
  3784. log_printf(RECOVERY_DEBUG, "DONE Sending open counts\n");
  3785. recovery_phase = evt_wait_open_count_done;
  3786. return 1;
  3787. }
  3788. /*
  3789. * Wait for all nodes to finish sending open updates before proceding.
  3790. * the EVT_OPEN_COUNT_DONE handler will set the state to
  3791. * evt_send_retained_events to get us out of this.
  3792. */
  3793. case evt_wait_open_count_done:
  3794. {
  3795. log_printf(RECOVERY_DEBUG, "Wait for open count done\n");
  3796. return 1;
  3797. }
  3798. /*
  3799. * If I'm the oldest node, send out retained events so that new nodes
  3800. * have all the information.
  3801. */
  3802. case evt_send_retained_events:
  3803. {
  3804. struct iovec chn_iovec;
  3805. struct event_data *evt;
  3806. int res;
  3807. log_printf(RECOVERY_DEBUG, "Send retained event updates\n");
  3808. /*
  3809. * Process messages. When we're done, send the done message
  3810. * to the nodes.
  3811. */
  3812. for (;next_retained != &retained_list;
  3813. next_retained = next_retained->next) {
  3814. log_printf(LOG_LEVEL_DEBUG, "Sending next retained event\n");
  3815. evt = list_entry(next_retained, struct event_data, ed_retained);
  3816. evt->ed_event.led_head.id =
  3817. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_RECOVERY_EVENTDATA);
  3818. chn_iovec.iov_base = (char *)&evt->ed_event;
  3819. chn_iovec.iov_len = evt->ed_event.led_head.size;
  3820. res = api->totem_mcast (&chn_iovec, 1, TOTEM_AGREED);
  3821. if (res != 0) {
  3822. /*
  3823. * Try again later.
  3824. */
  3825. return -1;
  3826. }
  3827. }
  3828. recovery_phase = evt_send_retained_events_done;
  3829. return 1;
  3830. }
  3831. case evt_send_retained_events_done:
  3832. {
  3833. struct req_evt_chan_command cpkt;
  3834. struct iovec chn_iovec;
  3835. int res;
  3836. log_printf(RECOVERY_DEBUG, "DONE Sending retained events\n");
  3837. memset(&cpkt, 0, sizeof(cpkt));
  3838. cpkt.chc_head.id =
  3839. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  3840. cpkt.chc_head.size = sizeof(cpkt);
  3841. cpkt.chc_op = EVT_CONF_DONE;
  3842. chn_iovec.iov_base = (char *)&cpkt;
  3843. chn_iovec.iov_len = cpkt.chc_head.size;
  3844. res = api->totem_mcast (&chn_iovec, 1, TOTEM_AGREED);
  3845. recovery_phase = evt_wait_send_retained_events;
  3846. return 1;
  3847. }
  3848. /*
  3849. * Wait for send of retained events to finish
  3850. * the EVT_CONF_DONE handler will set the state to
  3851. * evt_recovery_complete to get us out of this.
  3852. */
  3853. case evt_wait_send_retained_events:
  3854. {
  3855. log_printf(RECOVERY_DEBUG, "Wait for retained events\n");
  3856. return 1;
  3857. }
  3858. case evt_recovery_complete:
  3859. {
  3860. log_printf(RECOVERY_DEBUG, "Recovery complete\n");
  3861. return 0;
  3862. }
  3863. default:
  3864. log_printf(LOG_LEVEL_WARNING, "Bad recovery phase state: %u\n",
  3865. recovery_phase);
  3866. recovery_phase = evt_recovery_complete;
  3867. return 0;
  3868. }
  3869. return 0;
  3870. }
  3871. /*
  3872. * Not used at this time
  3873. */
  3874. static void evt_sync_activate(void)
  3875. {
  3876. log_printf(RECOVERY_DEBUG, "Evt synchronize activation\n");
  3877. }
  3878. /*
  3879. * Not used at this time
  3880. */
  3881. static void evt_sync_abort(void)
  3882. {
  3883. log_printf(RECOVERY_DEBUG, "Abort Evt synchronization\n");
  3884. }
  3885. /*
  3886. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3887. */