evt.c 116 KB

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