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 "print.h"
  63. #include "tlist.h"
  64. #include "timer.h"
  65. /*
  66. * event instance structure. Contains information about the
  67. * active connection to the API library.
  68. *
  69. * esi_version: Version that the library is running.
  70. * esi_open_chans: list of open channels associated with this
  71. * instance. Used to clean up any data left
  72. * allocated when the finalize is done.
  73. * (event_svr_channel_open.eco_instance_entry)
  74. * esi_events: list of pending events to be delivered on this
  75. * instance (struct chan_event_list.cel_entry)
  76. * esi_queue_blocked: non-zero if the delivery queue got too full
  77. * and we're blocking new messages until we
  78. * drain some of the queued messages.
  79. * esi_nevents: Number of events in events lists to be sent.
  80. * esi_hdb: Handle data base for open channels on this
  81. * instance. Used for a quick lookup of
  82. * open channel data from a lib api message.
  83. */
  84. struct libevt_pd {
  85. SaVersionT esi_version;
  86. struct list_head esi_open_chans;
  87. struct list_head esi_events[SA_EVT_LOWEST_PRIORITY+1];
  88. int esi_nevents;
  89. int esi_queue_blocked;
  90. struct hdb_handle_database esi_hdb;
  91. };
  92. enum evt_message_req_types {
  93. MESSAGE_REQ_EXEC_EVT_EVENTDATA = 0,
  94. MESSAGE_REQ_EXEC_EVT_CHANCMD = 1,
  95. MESSAGE_REQ_EXEC_EVT_RECOVERY_EVENTDATA = 2
  96. };
  97. static void lib_evt_open_channel(void *conn, void *message);
  98. static void lib_evt_open_channel_async(void *conn, void *message);
  99. static void lib_evt_close_channel(void *conn, void *message);
  100. static void lib_evt_unlink_channel(void *conn, void *message);
  101. static void lib_evt_event_subscribe(void *conn, void *message);
  102. static void lib_evt_event_unsubscribe(void *conn, void *message);
  103. static void lib_evt_event_publish(void *conn, void *message);
  104. static void lib_evt_event_clear_retentiontime(void *conn, void *message);
  105. static void lib_evt_event_data_get(void *conn, void *message);
  106. static void evt_conf_change(
  107. enum totem_configuration_type configuration_type,
  108. unsigned int *member_list, int member_list_entries,
  109. unsigned int *left_list, int left_list_entries,
  110. unsigned int *joined_list, int joined_list_entries,
  111. struct memb_ring_id *ring_id);
  112. static int evt_lib_init(void *conn);
  113. static int evt_lib_exit(void *conn);
  114. static int evt_exec_init(struct objdb_iface_ver0 *objdb);
  115. /*
  116. * Recovery sync functions
  117. */
  118. static void evt_sync_init(void);
  119. static int evt_sync_process(void);
  120. static void evt_sync_activate(void);
  121. static void evt_sync_abort(void);
  122. static void convert_event(void *msg);
  123. static void convert_chan_packet(void *msg);
  124. static struct openais_lib_handler evt_lib_service[] = {
  125. {
  126. .lib_handler_fn = lib_evt_open_channel,
  127. .response_size = sizeof(struct res_evt_channel_open),
  128. .response_id = MESSAGE_RES_EVT_OPEN_CHANNEL,
  129. .flow_control = OPENAIS_FLOW_CONTROL_REQUIRED
  130. },
  131. {
  132. .lib_handler_fn = lib_evt_open_channel_async,
  133. .response_size = sizeof(struct res_evt_channel_open),
  134. .response_id = MESSAGE_RES_EVT_OPEN_CHANNEL,
  135. .flow_control = OPENAIS_FLOW_CONTROL_REQUIRED
  136. },
  137. {
  138. .lib_handler_fn = lib_evt_close_channel,
  139. .response_size = sizeof(struct res_evt_channel_close),
  140. .response_id = MESSAGE_RES_EVT_CLOSE_CHANNEL,
  141. .flow_control = OPENAIS_FLOW_CONTROL_REQUIRED
  142. },
  143. {
  144. .lib_handler_fn = lib_evt_unlink_channel,
  145. .response_size = sizeof(struct res_evt_channel_unlink),
  146. .response_id = MESSAGE_RES_EVT_UNLINK_CHANNEL,
  147. .flow_control = OPENAIS_FLOW_CONTROL_REQUIRED
  148. },
  149. {
  150. .lib_handler_fn = lib_evt_event_subscribe,
  151. .response_size = sizeof(struct res_evt_event_subscribe),
  152. .response_id = MESSAGE_RES_EVT_SUBSCRIBE,
  153. .flow_control = OPENAIS_FLOW_CONTROL_NOT_REQUIRED
  154. },
  155. {
  156. .lib_handler_fn = lib_evt_event_unsubscribe,
  157. .response_size = sizeof(struct res_evt_event_unsubscribe),
  158. .response_id = MESSAGE_RES_EVT_UNSUBSCRIBE,
  159. .flow_control = OPENAIS_FLOW_CONTROL_NOT_REQUIRED
  160. },
  161. {
  162. .lib_handler_fn = lib_evt_event_publish,
  163. .response_size = sizeof(struct res_evt_event_publish),
  164. .response_id = MESSAGE_RES_EVT_PUBLISH,
  165. .flow_control = OPENAIS_FLOW_CONTROL_REQUIRED
  166. },
  167. {
  168. .lib_handler_fn = lib_evt_event_clear_retentiontime,
  169. .response_size = sizeof(struct res_evt_event_clear_retentiontime),
  170. .response_id = MESSAGE_RES_EVT_CLEAR_RETENTIONTIME,
  171. .flow_control = OPENAIS_FLOW_CONTROL_REQUIRED
  172. },
  173. {
  174. .lib_handler_fn = lib_evt_event_data_get,
  175. .response_size = sizeof(struct lib_event_data),
  176. .response_id = MESSAGE_RES_EVT_EVENT_DATA,
  177. .flow_control = OPENAIS_FLOW_CONTROL_NOT_REQUIRED
  178. },
  179. };
  180. static void evt_remote_evt(void *msg, unsigned int nodeid);
  181. static void evt_remote_recovery_evt(void *msg, unsigned int nodeid);
  182. static void evt_remote_chan_op(void *msg, unsigned int nodeid);
  183. static struct openais_exec_handler evt_exec_service[] = {
  184. {
  185. .exec_handler_fn = evt_remote_evt,
  186. .exec_endian_convert_fn = convert_event
  187. },
  188. {
  189. .exec_handler_fn = evt_remote_chan_op,
  190. .exec_endian_convert_fn = convert_chan_packet
  191. },
  192. {
  193. .exec_handler_fn = evt_remote_recovery_evt,
  194. .exec_endian_convert_fn = convert_event
  195. }
  196. };
  197. struct openais_service_handler evt_service_handler = {
  198. .name = "openais event service B.01.01",
  199. .id = EVT_SERVICE,
  200. .private_data_size = sizeof (struct libevt_pd),
  201. .flow_control = OPENAIS_FLOW_CONTROL_NOT_REQUIRED,
  202. .lib_init_fn = evt_lib_init,
  203. .lib_exit_fn = evt_lib_exit,
  204. .lib_service = evt_lib_service,
  205. .lib_service_count = sizeof(evt_lib_service) / sizeof(struct openais_lib_handler),
  206. .exec_init_fn = evt_exec_init,
  207. .exec_service = evt_exec_service,
  208. .exec_service_count = sizeof(evt_exec_service) / sizeof(struct openais_exec_handler),
  209. .exec_dump_fn = NULL,
  210. .confchg_fn = evt_conf_change,
  211. .sync_init = evt_sync_init,
  212. .sync_process = evt_sync_process,
  213. .sync_activate = evt_sync_activate,
  214. .sync_abort = evt_sync_abort
  215. };
  216. static struct openais_service_handler *evt_get_handler_ver0(void);
  217. static struct openais_service_handler_iface_ver0 evt_service_handler_iface = {
  218. .openais_get_service_handler_ver0 = evt_get_handler_ver0
  219. };
  220. static struct lcr_iface openais_evt_ver0[1] = {
  221. {
  222. .name = "openais_evt",
  223. .version = 0,
  224. .versions_replace = 0,
  225. .versions_replace_count = 0,
  226. .dependencies = 0,
  227. .dependency_count = 0,
  228. .constructor = NULL,
  229. .destructor = NULL,
  230. .interfaces = NULL,
  231. }
  232. };
  233. static struct lcr_comp evt_comp_ver0 = {
  234. .iface_count = 1,
  235. .ifaces = openais_evt_ver0
  236. };
  237. static struct openais_service_handler *evt_get_handler_ver0(void)
  238. {
  239. return (&evt_service_handler);
  240. }
  241. __attribute__ ((constructor)) static void evt_comp_register (void) {
  242. lcr_interfaces_set (&openais_evt_ver0[0], &evt_service_handler_iface);
  243. lcr_component_register (&evt_comp_ver0);
  244. }
  245. /*
  246. * MESSAGE_REQ_EXEC_EVT_CHANCMD
  247. *
  248. * Used for various event related operations.
  249. *
  250. */
  251. enum evt_chan_ops {
  252. EVT_OPEN_CHAN_OP, /* chc_chan */
  253. EVT_CLOSE_CHAN_OP, /* chc_close_unlink_chan */
  254. EVT_UNLINK_CHAN_OP, /* chc_close_unlink_chan */
  255. EVT_CLEAR_RET_OP, /* chc_event_id */
  256. EVT_SET_ID_OP, /* chc_set_id */
  257. EVT_CONF_DONE, /* no data used */
  258. EVT_OPEN_COUNT, /* chc_set_opens */
  259. EVT_OPEN_COUNT_DONE /* no data used */
  260. };
  261. /*
  262. * Used during recovery to set the next issued event ID
  263. * based on the highest ID seen by any of the members
  264. */
  265. struct evt_set_id {
  266. mar_uint32_t chc_nodeid __attribute__((aligned(8)));
  267. mar_uint64_t chc_last_id __attribute__((aligned(8)));
  268. };
  269. /*
  270. * For set open count used during recovery to syncronize all nodes
  271. *
  272. * chc_chan_name: Channel name.
  273. * chc_open_count: number of local opens of this channel.
  274. */
  275. struct evt_set_opens {
  276. mar_name_t chc_chan_name __attribute__((aligned(8)));
  277. mar_uint32_t chc_open_count __attribute__((aligned(8)));
  278. };
  279. /*
  280. * Used to communicate channel to close or unlink.
  281. */
  282. #define EVT_CHAN_ACTIVE 0
  283. struct evt_close_unlink_chan {
  284. mar_name_t chcu_name __attribute__((aligned(8)));
  285. mar_uint64_t chcu_unlink_id __attribute__((aligned(8)));
  286. };
  287. struct open_chan_req {
  288. mar_name_t ocr_name __attribute__((aligned(8)));
  289. mar_uint64_t ocr_serial_no __attribute__((aligned(8)));
  290. };
  291. /*
  292. * Sent via MESSAGE_REQ_EXEC_EVT_CHANCMD
  293. *
  294. * chc_head: Request head
  295. * chc_op: Channel operation (open, close, clear retentiontime)
  296. * u: union of operation options.
  297. */
  298. struct req_evt_chan_command {
  299. mar_req_header_t chc_head __attribute__((aligned(8)));
  300. mar_uint32_t chc_op __attribute__((aligned(8)));
  301. union {
  302. struct open_chan_req chc_chan __attribute__((aligned(8)));
  303. mar_evteventid_t chc_event_id __attribute__((aligned(8)));
  304. struct evt_set_id chc_set_id __attribute__((aligned(8)));
  305. struct evt_set_opens chc_set_opens __attribute__((aligned(8)));
  306. struct evt_close_unlink_chan chcu __attribute__((aligned(8)));
  307. } u;
  308. };
  309. /*
  310. * list of all retained events
  311. * struct event_data
  312. */
  313. static DECLARE_LIST_INIT(retained_list);
  314. /*
  315. * list of all event channel information
  316. * struct event_svr_channel_instance
  317. */
  318. static DECLARE_LIST_INIT(esc_head);
  319. /*
  320. * list of all unlinked event channel information
  321. * struct event_svr_channel_instance
  322. */
  323. static DECLARE_LIST_INIT(esc_unlinked_head);
  324. /*
  325. * Track the state of event service recovery.
  326. *
  327. * evt_recovery_complete: Normal operational mode
  328. *
  329. * evt_send_event_id: Node is sending known last
  330. * event IDs.
  331. *
  332. * evt_send_open_count: Node is sending its open
  333. * Channel information.
  334. *
  335. * evt_wait_open_count_done: Node is done sending open channel data and
  336. * is waiting for the other nodes to finish.
  337. *
  338. * evt_send_retained_events: Node is sending retained event data.
  339. *
  340. * evt_send_retained_events_done: Node is sending done message.
  341. *
  342. * evt_wait_send_retained_events: Node is waiting for other nodes to
  343. * finish sending retained event data.
  344. */
  345. enum recovery_phases {
  346. evt_recovery_complete,
  347. evt_send_event_id,
  348. evt_send_open_count,
  349. evt_wait_open_count_done,
  350. evt_send_retained_events,
  351. evt_send_retained_events_done,
  352. evt_wait_send_retained_events
  353. };
  354. /*
  355. * Global varaibles used by the event service
  356. *
  357. * base_id_top: upper bits of next event ID to assign
  358. * base_id: Lower bits of Next event ID to assign
  359. * my_node_id: My cluster node id
  360. * checked_in: keep track during config change.
  361. * recovery_node: True if we're the recovery node. i.e. the
  362. * node that sends the retained events.
  363. * next_retained: pointer to next retained message to send
  364. * during recovery.
  365. * next_chan: pointer to next channel to send during recovery.
  366. * recovery_phase: Indicates what recovery is taking place.
  367. * left_member_count: How many left this configuration.
  368. * left_member_list: Members that left this config
  369. * joined_member_count: How many joined this configuration.
  370. * joined_member_list: Members that joined this config
  371. * total_member_count: how many members in this cluster
  372. * current_member_list: Total membership this config
  373. * trans_member_count: Node count in transitional membership
  374. * trans_member_list: Total membership from the transitional membership
  375. * add_count: count of joined members used for sending event id
  376. * recovery data.
  377. * add_list: pointer to joined list used for sending event id
  378. * recovery data.
  379. * processed_open_counts: Flag used to coordinate clearing open
  380. * channel counts for config change recovery.
  381. *
  382. */
  383. #define BASE_ID_MASK 0xffffffffLL
  384. static mar_evteventid_t base_id = 1;
  385. static mar_evteventid_t base_id_top = 0;
  386. static mar_uint32_t my_node_id = 0;
  387. static int checked_in = 0;
  388. static int recovery_node = 0;
  389. static struct list_head *next_retained = 0;
  390. static struct list_head *next_chan = 0;
  391. static enum recovery_phases recovery_phase = evt_recovery_complete;
  392. static int left_member_count = 0;
  393. static unsigned int *left_member_list = 0;
  394. static int joined_member_count = 0;
  395. static unsigned int *joined_member_list = 0;
  396. static int total_member_count = 0;
  397. static unsigned int *current_member_list = 0;
  398. static int trans_member_count = 0;
  399. static unsigned int *trans_member_list = 0;
  400. static int add_count = 0;
  401. static unsigned int *add_list = 0;
  402. static int processed_open_counts = 0;
  403. /*
  404. * Structure to track pending channel open requests.
  405. * ocp_async: 1 for async open
  406. * ocp_invocation: invocation for async open
  407. * ocp_chan_name: requested channel
  408. * ocp_conn: conn for returning to the library.
  409. * ocp_open_flags: channel open flags
  410. * ocp_timer_handle: timer handle for sync open
  411. * ocp_serial_no: Identifier for the request
  412. * ocp_entry: list entry for pending open list.
  413. */
  414. struct open_chan_pending {
  415. int ocp_async;
  416. mar_invocation_t ocp_invocation;
  417. mar_name_t ocp_chan_name;
  418. void *ocp_conn;
  419. mar_evtchannelopenflags_t ocp_open_flag;
  420. timer_handle ocp_timer_handle;
  421. uint64_t ocp_c_handle;
  422. uint64_t ocp_serial_no;
  423. struct list_head ocp_entry;
  424. };
  425. static uint64_t open_serial_no = 0;
  426. /*
  427. * code to indicate that the open request has timed out. The
  428. * invocation data element is used for this code since it is
  429. * only used by the open async call which cannot have a timeout.
  430. */
  431. #define OPEN_TIMED_OUT 0x5a6b5a6b5a6b5a6bLLU
  432. /*
  433. * list of pending channel opens
  434. */
  435. static DECLARE_LIST_INIT(open_pending);
  436. static void chan_open_timeout(void *data);
  437. /*
  438. * Structure to track pending channel unlink requests.
  439. * ucp_unlink_id: unlink ID of unlinked channel.
  440. * ucp_conn: conn for returning to the library.
  441. * ucp_entry: list entry for pending unlink list.
  442. */
  443. struct unlink_chan_pending {
  444. uint64_t ucp_unlink_id;
  445. void *ucp_conn;
  446. struct list_head ucp_entry;
  447. };
  448. /*
  449. * list of pending unlink requests
  450. */
  451. static DECLARE_LIST_INIT(unlink_pending);
  452. /*
  453. * Structure to track pending retention time clear requests.
  454. * rtc_event_id: event ID to clear.
  455. * rtc_conn: conn for returning to the library.
  456. * rtc_entry: list entry for pending clear list.
  457. */
  458. struct retention_time_clear_pending {
  459. mar_evteventid_t rtc_event_id;
  460. void *rtc_conn;
  461. struct list_head rtc_entry;
  462. };
  463. /*
  464. * list of pending clear requests.
  465. */
  466. static DECLARE_LIST_INIT(clear_pending);
  467. /*
  468. * Next unlink ID
  469. */
  470. static uint64_t base_unlink_id = 0;
  471. inline uint64_t
  472. next_chan_unlink_id()
  473. {
  474. uint64_t uid = my_node_id;
  475. uid = (uid << 32ULL) | base_unlink_id;
  476. base_unlink_id = (base_unlink_id + 1ULL) & BASE_ID_MASK;
  477. return uid;
  478. }
  479. #define min(a,b) ((a) < (b) ? (a) : (b))
  480. /*
  481. * Throttle event delivery to applications to keep
  482. * the exec from using too much memory if the app is
  483. * slow to process its events.
  484. */
  485. #define MAX_EVT_DELIVERY_QUEUE 1000
  486. #define MIN_EVT_QUEUE_RESUME (MAX_EVT_DELIVERY_QUEUE / 2)
  487. static unsigned int evt_delivery_queue_size = MAX_EVT_DELIVERY_QUEUE;
  488. static unsigned int evt_delivery_queue_resume = MIN_EVT_QUEUE_RESUME;
  489. #define LOST_PUB "EVENT_SERIVCE"
  490. #define LOST_CHAN "LOST EVENT"
  491. /*
  492. * Event to send when the delivery queue gets too full
  493. */
  494. char lost_evt[] = SA_EVT_LOST_EVENT;
  495. static int dropped_event_size;
  496. static struct event_data *dropped_event;
  497. struct evt_pattern {
  498. mar_evt_event_pattern_t pat;
  499. char str[sizeof(lost_evt)];
  500. };
  501. static struct evt_pattern dropped_pattern = {
  502. .pat = {
  503. sizeof(lost_evt),
  504. sizeof(lost_evt),
  505. (SaUint8T *) &dropped_pattern.str[0]},
  506. .str = {SA_EVT_LOST_EVENT}
  507. };
  508. mar_name_t lost_chan = {
  509. .value = LOST_CHAN,
  510. .length = sizeof(LOST_CHAN)
  511. };
  512. mar_name_t dropped_publisher = {
  513. .value = LOST_PUB,
  514. .length = sizeof(LOST_PUB)
  515. };
  516. struct event_svr_channel_open;
  517. struct event_svr_channel_subscr;
  518. struct open_count {
  519. mar_uint32_t oc_node_id;
  520. int32_t oc_open_count;
  521. };
  522. /*
  523. * Structure to contain global channel releated information
  524. *
  525. * esc_channel_name: The name of this channel.
  526. * esc_total_opens: The total number of opens on this channel including
  527. * other nodes.
  528. * esc_local_opens: The number of opens on this channel for this node.
  529. * esc_oc_size: The total number of entries in esc_node_opens;
  530. * esc_node_opens: list of node IDs and how many opens are associated.
  531. * esc_retained_count: How many retained events for this channel
  532. * esc_open_chans: list of opens of this channel.
  533. * (event_svr_channel_open.eco_entry)
  534. * esc_entry: links to other channels. (used by esc_head)
  535. * esc_unlink_id: If non-zero, then the channel has been marked
  536. * for unlink. This unlink ID is used to
  537. * mark events still associated with current openings
  538. * so they get delivered to the proper recipients.
  539. */
  540. struct event_svr_channel_instance {
  541. mar_name_t esc_channel_name;
  542. int32_t esc_total_opens;
  543. int32_t esc_local_opens;
  544. uint32_t esc_oc_size;
  545. struct open_count *esc_node_opens;
  546. uint32_t esc_retained_count;
  547. struct list_head esc_open_chans;
  548. struct list_head esc_entry;
  549. uint64_t esc_unlink_id;
  550. };
  551. /*
  552. * Has the event data in the correct format to send to the library API
  553. * with aditional field for accounting.
  554. *
  555. * ed_ref_count: how many other strutures are referencing.
  556. * ed_retained: retained event list.
  557. * ed_timer_handle: Timer handle for retained event expiration.
  558. * ed_delivered: arrays of open channel pointers that this event
  559. * has been delivered to. (only used for events
  560. * with a retention time).
  561. * ed_delivered_count: Number of entries available in ed_delivered.
  562. * ed_delivered_next: Next free spot in ed_delivered
  563. * ed_my_chan: pointer to the global channel instance associated
  564. * with this event.
  565. * ed_event: The event data formatted to be ready to send.
  566. */
  567. struct event_data {
  568. uint32_t ed_ref_count;
  569. struct list_head ed_retained;
  570. timer_handle ed_timer_handle;
  571. struct event_svr_channel_open **ed_delivered;
  572. uint32_t ed_delivered_count;
  573. uint32_t ed_delivered_next;
  574. struct event_svr_channel_instance *ed_my_chan;
  575. struct lib_event_data ed_event;
  576. };
  577. /*
  578. * Contains a list of pending events to be delivered to a subscribed
  579. * application.
  580. *
  581. * cel_chan_handle: associated library channel handle
  582. * cel_sub_id: associated library subscription ID
  583. * cel_event: event structure to deliver.
  584. * cel_entry: list of pending events
  585. * (struct event_server_instance.esi_events)
  586. */
  587. struct chan_event_list {
  588. uint64_t cel_chan_handle;
  589. uint32_t cel_sub_id;
  590. struct event_data* cel_event;
  591. struct list_head cel_entry;
  592. };
  593. /*
  594. * Contains information about each open for a given channel
  595. *
  596. * eco_flags: How the channel was opened.
  597. * eco_lib_handle: channel handle in the app. Used for event delivery.
  598. * eco_my_handle: the handle used to access this data structure.
  599. * eco_channel: Pointer to global channel info.
  600. * eco_entry: links to other opeinings of this channel.
  601. * eco_instance_entry: links to other channel opeinings for the
  602. * associated server instance.
  603. * eco_subscr: head of list of sbuscriptions for this channel open.
  604. * (event_svr_channel_subscr.ecs_entry)
  605. * eco_conn: refrence to EvtInitialize who owns this open.
  606. */
  607. struct event_svr_channel_open {
  608. uint8_t eco_flags;
  609. uint64_t eco_lib_handle;
  610. uint32_t eco_my_handle;
  611. struct event_svr_channel_instance *eco_channel;
  612. struct list_head eco_entry;
  613. struct list_head eco_instance_entry;
  614. struct list_head eco_subscr;
  615. void *eco_conn;
  616. };
  617. /*
  618. * Contains information about each channel subscription
  619. *
  620. * ecs_open_chan: Link to our open channel.
  621. * ecs_sub_id: Subscription ID.
  622. * ecs_filter_count: number of filters in ecs_filters
  623. * ecs_filters: filters for determining event delivery.
  624. * ecs_entry: Links to other subscriptions to this channel opening.
  625. */
  626. struct event_svr_channel_subscr {
  627. struct event_svr_channel_open *ecs_open_chan;
  628. uint32_t ecs_sub_id;
  629. mar_evt_event_filter_array_t *ecs_filters;
  630. struct list_head ecs_entry;
  631. };
  632. /*
  633. * Member node data
  634. * mn_node_info: cluster node info from membership
  635. * mn_last_msg_id: last seen message ID for this node
  636. * mn_started: Indicates that event service has started
  637. * on this node.
  638. * mn_next: pointer to the next node in the hash chain.
  639. * mn_entry: List of all nodes.
  640. */
  641. struct member_node_data {
  642. unsigned int mn_nodeid;
  643. SaClmClusterNodeT mn_node_info;
  644. mar_evteventid_t mn_last_msg_id;
  645. mar_uint32_t mn_started;
  646. struct member_node_data *mn_next;
  647. struct list_head mn_entry;
  648. };
  649. DECLARE_LIST_INIT(mnd);
  650. /*
  651. * Take the filters we received from the application via the library and
  652. * make them into a real mar_evt_event_filter_array_t
  653. */
  654. static SaAisErrorT evtfilt_to_aisfilt(struct req_evt_event_subscribe *req,
  655. mar_evt_event_filter_array_t **evtfilters)
  656. {
  657. mar_evt_event_filter_array_t *filta =
  658. (mar_evt_event_filter_array_t *)req->ics_filter_data;
  659. mar_evt_event_filter_array_t *filters;
  660. mar_evt_event_filter_t *filt = (void *)filta + sizeof(mar_evt_event_filter_array_t);
  661. SaUint8T *str = (void *)filta + sizeof(mar_evt_event_filter_array_t) +
  662. (sizeof(mar_evt_event_filter_t) * filta->filters_number);
  663. int i;
  664. int j;
  665. filters = malloc(sizeof(mar_evt_event_filter_array_t));
  666. if (!filters) {
  667. return SA_AIS_ERR_NO_MEMORY;
  668. }
  669. filters->filters_number = filta->filters_number;
  670. filters->filters = malloc(sizeof(mar_evt_event_filter_t) *
  671. filta->filters_number);
  672. if (!filters->filters) {
  673. free(filters);
  674. return SA_AIS_ERR_NO_MEMORY;
  675. }
  676. for (i = 0; i < filters->filters_number; i++) {
  677. filters->filters[i].filter.pattern =
  678. malloc(filt[i].filter.pattern_size);
  679. if (!filters->filters[i].filter.pattern) {
  680. for (j = 0; j < i; j++) {
  681. free(filters->filters[j].filter.pattern);
  682. }
  683. free(filters->filters);
  684. free(filters);
  685. return SA_AIS_ERR_NO_MEMORY;
  686. }
  687. filters->filters[i].filter.pattern_size =
  688. filt[i].filter.pattern_size;
  689. filters->filters[i].filter.allocated_size =
  690. filt[i].filter.pattern_size;
  691. memcpy(filters->filters[i].filter.pattern,
  692. str, filters->filters[i].filter.pattern_size);
  693. filters->filters[i].filter_type = filt[i].filter_type;
  694. str += filters->filters[i].filter.pattern_size;
  695. }
  696. *evtfilters = filters;
  697. return SA_AIS_OK;
  698. }
  699. /*
  700. * Free up filter data
  701. */
  702. static void free_filters(mar_evt_event_filter_array_t *fp)
  703. {
  704. int i;
  705. for (i = 0; i < fp->filters_number; i++) {
  706. free(fp->filters[i].filter.pattern);
  707. }
  708. free(fp->filters);
  709. free(fp);
  710. }
  711. /*
  712. * Look up a channel in the global channel list
  713. */
  714. static struct event_svr_channel_instance *
  715. find_channel(mar_name_t *chan_name, uint64_t unlink_id)
  716. {
  717. struct list_head *l, *head;
  718. struct event_svr_channel_instance *eci;
  719. /*
  720. * choose which list to look through
  721. */
  722. if (unlink_id == EVT_CHAN_ACTIVE) {
  723. head = &esc_head;
  724. } else {
  725. head = &esc_unlinked_head;
  726. }
  727. for (l = head->next; l != head; l = l->next) {
  728. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  729. if (!mar_name_match(chan_name, &eci->esc_channel_name)) {
  730. continue;
  731. } else if (unlink_id != eci->esc_unlink_id) {
  732. continue;
  733. }
  734. return eci;
  735. }
  736. return 0;
  737. }
  738. /*
  739. * Find the last unlinked version of a channel.
  740. */
  741. static struct event_svr_channel_instance *
  742. find_last_unlinked_channel(mar_name_t *chan_name)
  743. {
  744. struct list_head *l;
  745. struct event_svr_channel_instance *eci;
  746. /*
  747. * unlinked channels are added to the head of the list
  748. * so the first one we see is the last one added.
  749. */
  750. for (l = esc_unlinked_head.next; l != &esc_unlinked_head; l = l->next) {
  751. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  752. if (!mar_name_match(chan_name, &eci->esc_channel_name)) {
  753. continue;
  754. }
  755. }
  756. return 0;
  757. }
  758. /*
  759. * Create and initialize a channel instance structure
  760. */
  761. static struct event_svr_channel_instance *create_channel(mar_name_t *cn)
  762. {
  763. struct event_svr_channel_instance *eci;
  764. eci = (struct event_svr_channel_instance *) malloc(sizeof(*eci));
  765. if (!eci) {
  766. return (eci);
  767. }
  768. memset(eci, 0, sizeof(*eci));
  769. list_init(&eci->esc_entry);
  770. list_init(&eci->esc_open_chans);
  771. eci->esc_oc_size = total_member_count;
  772. eci->esc_node_opens =
  773. malloc(sizeof(struct open_count) * total_member_count);
  774. if (!eci->esc_node_opens) {
  775. free(eci);
  776. return 0;
  777. }
  778. memset(eci->esc_node_opens, 0,
  779. sizeof(struct open_count) * total_member_count);
  780. eci->esc_channel_name = *cn;
  781. eci->esc_channel_name.value[eci->esc_channel_name.length] = '\0';
  782. list_add(&eci->esc_entry, &esc_head);
  783. return eci;
  784. }
  785. /*
  786. * Make sure that the list of nodes is large enough to hold the whole
  787. * membership
  788. */
  789. static int check_open_size(struct event_svr_channel_instance *eci)
  790. {
  791. struct open_count *esc_node_opens_tmp;
  792. if (total_member_count > eci->esc_oc_size) {
  793. esc_node_opens_tmp = realloc (eci->esc_node_opens,
  794. sizeof(struct open_count) * total_member_count);
  795. if (esc_node_opens_tmp == NULL) {
  796. log_printf(LOG_LEVEL_WARNING,
  797. "Memory error realloc of node list\n");
  798. return -1;
  799. }
  800. eci->esc_node_opens = esc_node_opens_tmp;
  801. memset(&eci->esc_node_opens[eci->esc_oc_size], 0,
  802. sizeof(struct open_count) *
  803. (total_member_count - eci->esc_oc_size));
  804. eci->esc_oc_size = total_member_count;
  805. }
  806. return 0;
  807. }
  808. /*
  809. * Find the specified node ID in the node list of the channel.
  810. * If it's not in the list, add it.
  811. */
  812. static struct open_count* find_open_count(
  813. struct event_svr_channel_instance *eci,
  814. mar_uint32_t node_id)
  815. {
  816. int i;
  817. for (i = 0; i < eci->esc_oc_size; i++) {
  818. if (eci->esc_node_opens[i].oc_node_id == 0) {
  819. eci->esc_node_opens[i].oc_node_id = node_id;
  820. eci->esc_node_opens[i].oc_open_count = 0;
  821. }
  822. if (eci->esc_node_opens[i].oc_node_id == node_id) {
  823. return &eci->esc_node_opens[i];
  824. }
  825. }
  826. log_printf(LOG_LEVEL_DEBUG,
  827. "find_open_count: node id %s not found\n",
  828. totempg_ifaces_print (node_id));
  829. return 0;
  830. }
  831. static void dump_chan_opens(struct event_svr_channel_instance *eci)
  832. {
  833. int i;
  834. log_printf(LOG_LEVEL_NOTICE,
  835. "Channel %s, total %d, local %d\n",
  836. eci->esc_channel_name.value,
  837. eci->esc_total_opens,
  838. eci->esc_local_opens);
  839. for (i = 0; i < eci->esc_oc_size; i++) {
  840. if (eci->esc_node_opens[i].oc_node_id == 0) {
  841. break;
  842. }
  843. log_printf(LOG_LEVEL_NOTICE, "Node %s, count %d\n",
  844. totempg_ifaces_print (eci->esc_node_opens[i].oc_node_id),
  845. eci->esc_node_opens[i].oc_open_count);
  846. }
  847. }
  848. #ifdef DUMP_CHAN_INFO
  849. /*
  850. * Scan the list of channels and dump the open count info.
  851. */
  852. static void dump_all_chans()
  853. {
  854. struct list_head *l;
  855. struct event_svr_channel_instance *eci;
  856. for (l = esc_head.next; l != &esc_head; l = l->next) {
  857. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  858. dump_chan_opens(eci);
  859. }
  860. }
  861. #endif
  862. /*
  863. * Scan the list of channels and zero out the open counts
  864. */
  865. static void zero_chan_open_counts()
  866. {
  867. struct list_head *l;
  868. struct event_svr_channel_instance *eci;
  869. int i;
  870. for (l = esc_head.next; l != &esc_head; l = l->next) {
  871. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  872. for (i = 0; i < eci->esc_oc_size; i++) {
  873. if (eci->esc_node_opens[i].oc_node_id == 0) {
  874. break;
  875. }
  876. eci->esc_node_opens[i].oc_open_count = 0;
  877. }
  878. eci->esc_total_opens = 0;
  879. }
  880. }
  881. /*
  882. * Replace the current open count for a node with the specified value.
  883. */
  884. static int set_open_count(struct event_svr_channel_instance *eci,
  885. mar_uint32_t node_id, uint32_t open_count)
  886. {
  887. struct open_count *oc;
  888. int i;
  889. if ((i = check_open_size(eci)) != 0) {
  890. return i;
  891. }
  892. oc = find_open_count(eci, node_id);
  893. if (oc) {
  894. log_printf(RECOVERY_DEBUG,
  895. "Set count: Chan %s for node %s, was %d, now %d\n",
  896. eci->esc_channel_name.value, totempg_ifaces_print (node_id),
  897. oc->oc_open_count, open_count);
  898. eci->esc_total_opens -= oc->oc_open_count;
  899. eci->esc_total_opens += open_count;
  900. oc->oc_open_count = open_count;
  901. return 0;
  902. }
  903. return -1;
  904. }
  905. /*
  906. * Increment the open count for the specified node.
  907. */
  908. static int inc_open_count(struct event_svr_channel_instance *eci,
  909. mar_uint32_t node_id)
  910. {
  911. struct open_count *oc;
  912. int i;
  913. if ((i = check_open_size(eci)) != 0) {
  914. return i;
  915. }
  916. if (node_id == my_node_id) {
  917. eci->esc_local_opens++;
  918. }
  919. oc = find_open_count(eci, node_id);
  920. if (oc) {
  921. eci->esc_total_opens++;
  922. oc->oc_open_count++;
  923. return 0;
  924. }
  925. return -1;
  926. }
  927. /*
  928. * Decrement the open count for the specified node in the
  929. * specified channel.
  930. */
  931. static int dec_open_count(struct event_svr_channel_instance *eci,
  932. mar_uint32_t node_id)
  933. {
  934. struct open_count *oc;
  935. int i;
  936. if ((i = check_open_size(eci)) != 0) {
  937. return i;
  938. }
  939. if (node_id == my_node_id) {
  940. eci->esc_local_opens--;
  941. }
  942. oc = find_open_count(eci, node_id);
  943. if (oc) {
  944. eci->esc_total_opens--;
  945. oc->oc_open_count--;
  946. if ((eci->esc_total_opens < 0) || (oc->oc_open_count < 0)) {
  947. log_printf(LOG_LEVEL_ERROR, "Channel open decrement error\n");
  948. dump_chan_opens(eci);
  949. }
  950. return 0;
  951. }
  952. return -1;
  953. }
  954. /*
  955. * Remove a channel and free its memory if it's not in use anymore.
  956. */
  957. static void delete_channel(struct event_svr_channel_instance *eci)
  958. {
  959. log_printf(CHAN_DEL_DEBUG,
  960. "Called Delete channel %s t %d, l %d, r %d\n",
  961. eci->esc_channel_name.value,
  962. eci->esc_total_opens, eci->esc_local_opens,
  963. eci->esc_retained_count);
  964. /*
  965. * If no one has the channel open anywhere and there are no unexpired
  966. * retained events for this channel, and it has been marked for deletion
  967. * by an unlink, then it is OK to delete the data structure.
  968. */
  969. if ((eci->esc_retained_count == 0) && (eci->esc_total_opens == 0) &&
  970. (eci->esc_unlink_id != EVT_CHAN_ACTIVE)) {
  971. log_printf(CHAN_DEL_DEBUG, "Delete channel %s\n",
  972. eci->esc_channel_name.value);
  973. log_printf(CHAN_UNLINK_DEBUG, "Delete channel %s, unlink_id %0llx\n",
  974. eci->esc_channel_name.value, (unsigned long long)eci->esc_unlink_id);
  975. if (!list_empty(&eci->esc_open_chans)) {
  976. log_printf(LOG_LEVEL_NOTICE,
  977. "Last channel close request for %s (still open)\n",
  978. eci->esc_channel_name.value);
  979. dump_chan_opens(eci);
  980. return;
  981. }
  982. /*
  983. * adjust if we're sending open counts on a config change.
  984. */
  985. if ((recovery_phase != evt_recovery_complete) &&
  986. (&eci->esc_entry == next_chan)) {
  987. next_chan = eci->esc_entry.next;
  988. }
  989. list_del(&eci->esc_entry);
  990. if (eci->esc_node_opens) {
  991. free(eci->esc_node_opens);
  992. }
  993. free(eci);
  994. }
  995. }
  996. /*
  997. * Free up an event structure if it isn't being used anymore.
  998. */
  999. static void
  1000. free_event_data(struct event_data *edp)
  1001. {
  1002. if (--edp->ed_ref_count) {
  1003. return;
  1004. }
  1005. log_printf(LOG_LEVEL_DEBUG, "Freeing event ID: 0x%llx\n",
  1006. (unsigned long long)edp->ed_event.led_event_id);
  1007. if (edp->ed_delivered) {
  1008. free(edp->ed_delivered);
  1009. }
  1010. free(edp);
  1011. }
  1012. /*
  1013. * Mark a channel for deletion.
  1014. */
  1015. static void unlink_channel(struct event_svr_channel_instance *eci,
  1016. uint64_t unlink_id)
  1017. {
  1018. struct event_data *edp;
  1019. struct list_head *l, *nxt;
  1020. log_printf(CHAN_UNLINK_DEBUG, "Unlink request: %s, id 0x%llx\n",
  1021. eci->esc_channel_name.value, (unsigned long long)unlink_id);
  1022. /*
  1023. * The unlink ID is used to note that the channel has been marked
  1024. * for deletion and is a way to distinguish between multiple
  1025. * channels of the same name each marked for deletion.
  1026. */
  1027. eci->esc_unlink_id = unlink_id;
  1028. /*
  1029. * Move the unlinked channel to the unlinked list. This way
  1030. * we don't have to worry about filtering it out when we need to
  1031. * distribute retained events at recovery time.
  1032. */
  1033. list_del(&eci->esc_entry);
  1034. list_add(&eci->esc_entry, &esc_unlinked_head);
  1035. /*
  1036. * Scan the retained event list and remove any retained events.
  1037. * Since no new opens can occur there won't be any need of sending
  1038. * retained events on the channel.
  1039. */
  1040. for (l = retained_list.next; l != &retained_list; l = nxt) {
  1041. nxt = l->next;
  1042. edp = list_entry(l, struct event_data, ed_retained);
  1043. if ((edp->ed_my_chan == eci) &&
  1044. (edp->ed_event.led_chan_unlink_id == EVT_CHAN_ACTIVE)) {
  1045. openais_timer_delete(edp->ed_timer_handle);
  1046. edp->ed_event.led_retention_time = 0;
  1047. list_del(&edp->ed_retained);
  1048. list_init(&edp->ed_retained);
  1049. edp->ed_my_chan->esc_retained_count--;
  1050. log_printf(CHAN_UNLINK_DEBUG,
  1051. "Unlink: Delete retained event id 0x%llx\n",
  1052. (unsigned long long)edp->ed_event.led_event_id);
  1053. free_event_data(edp);
  1054. }
  1055. }
  1056. delete_channel(eci);
  1057. }
  1058. /*
  1059. * Remove the specified node from the node list in this channel.
  1060. */
  1061. static int remove_open_count(
  1062. struct event_svr_channel_instance *eci,
  1063. mar_uint32_t node_id)
  1064. {
  1065. int i;
  1066. int j;
  1067. /*
  1068. * Find the node, remove it and re-pack the array.
  1069. */
  1070. for (i = 0; i < eci->esc_oc_size; i++) {
  1071. if (eci->esc_node_opens[i].oc_node_id == 0) {
  1072. break;
  1073. }
  1074. log_printf(RECOVERY_DEBUG, "roc: %s/%s, t %d, oc %d\n",
  1075. totempg_ifaces_print (node_id),
  1076. totempg_ifaces_print (eci->esc_node_opens[i].oc_node_id),
  1077. eci->esc_total_opens, eci->esc_node_opens[i].oc_open_count);
  1078. if (eci->esc_node_opens[i].oc_node_id == node_id) {
  1079. eci->esc_total_opens -= eci->esc_node_opens[i].oc_open_count;
  1080. for (j = i+1; j < eci->esc_oc_size; j++, i++) {
  1081. eci->esc_node_opens[i].oc_node_id =
  1082. eci->esc_node_opens[j].oc_node_id;
  1083. eci->esc_node_opens[i].oc_open_count =
  1084. eci->esc_node_opens[j].oc_open_count;
  1085. }
  1086. eci->esc_node_opens[eci->esc_oc_size-1].oc_node_id = 0;
  1087. eci->esc_node_opens[eci->esc_oc_size-1].oc_open_count = 0;
  1088. /*
  1089. * Remove the channel if it's not being used anymore
  1090. */
  1091. delete_channel(eci);
  1092. return 0;
  1093. }
  1094. }
  1095. return -1;
  1096. }
  1097. /*
  1098. * Send a request to open a channel to the rest of the cluster.
  1099. */
  1100. static SaAisErrorT evt_open_channel(mar_name_t *cn, SaUint8T flgs)
  1101. {
  1102. struct req_evt_chan_command cpkt;
  1103. struct event_svr_channel_instance *eci;
  1104. struct iovec chn_iovec;
  1105. int res;
  1106. SaAisErrorT ret;
  1107. ret = SA_AIS_OK;
  1108. eci = find_channel(cn, EVT_CHAN_ACTIVE);
  1109. /*
  1110. * If the create flag set, and it doesn't exist, we can make the channel.
  1111. * Otherwise, it's an error since we're notified of channels being created
  1112. * and opened.
  1113. */
  1114. if (!eci && !(flgs & SA_EVT_CHANNEL_CREATE)) {
  1115. ret = SA_AIS_ERR_NOT_EXIST;
  1116. goto chan_open_end;
  1117. }
  1118. /*
  1119. * create the channel packet to send. Tell the the cluster
  1120. * to create the channel.
  1121. */
  1122. memset(&cpkt, 0, sizeof(cpkt));
  1123. cpkt.chc_head.id =
  1124. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  1125. cpkt.chc_head.size = sizeof(cpkt);
  1126. cpkt.chc_op = EVT_OPEN_CHAN_OP;
  1127. cpkt.u.chc_chan.ocr_name = *cn;
  1128. cpkt.u.chc_chan.ocr_serial_no = ++open_serial_no;
  1129. chn_iovec.iov_base = (char *)&cpkt;
  1130. chn_iovec.iov_len = cpkt.chc_head.size;
  1131. log_printf(CHAN_OPEN_DEBUG, "evt_open_channel: Send open mcast\n");
  1132. res = totempg_groups_mcast_joined(openais_group_handle,
  1133. &chn_iovec, 1, TOTEMPG_AGREED);
  1134. log_printf(CHAN_OPEN_DEBUG, "evt_open_channel: Open mcast result: %d\n",
  1135. res);
  1136. if (res != 0) {
  1137. ret = SA_AIS_ERR_LIBRARY;
  1138. }
  1139. chan_open_end:
  1140. return ret;
  1141. }
  1142. /*
  1143. * Send a request to close a channel with the rest of the cluster.
  1144. */
  1145. static SaAisErrorT evt_close_channel(mar_name_t *cn, uint64_t unlink_id)
  1146. {
  1147. struct req_evt_chan_command cpkt;
  1148. struct iovec chn_iovec;
  1149. int res;
  1150. SaAisErrorT ret;
  1151. ret = SA_AIS_OK;
  1152. /*
  1153. * create the channel packet to send. Tell the the cluster
  1154. * to close the channel.
  1155. */
  1156. memset(&cpkt, 0, sizeof(cpkt));
  1157. cpkt.chc_head.id =
  1158. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  1159. cpkt.chc_head.size = sizeof(cpkt);
  1160. cpkt.chc_op = EVT_CLOSE_CHAN_OP;
  1161. cpkt.u.chcu.chcu_name = *cn;
  1162. cpkt.u.chcu.chcu_unlink_id = unlink_id;
  1163. chn_iovec.iov_base = (char *)&cpkt;
  1164. chn_iovec.iov_len = cpkt.chc_head.size;
  1165. res = totempg_groups_mcast_joined(openais_group_handle,
  1166. &chn_iovec, 1, TOTEMPG_AGREED);
  1167. if (res != 0) {
  1168. ret = SA_AIS_ERR_LIBRARY;
  1169. }
  1170. return ret;
  1171. }
  1172. /*
  1173. * Node data access functions. Used to keep track of event IDs
  1174. * delivery of messages.
  1175. *
  1176. * add_node: Add a new member node to our list.
  1177. * remove_node: Remove a node that left membership.
  1178. * find_node: Given the node ID return a pointer to node information.
  1179. *
  1180. */
  1181. #define NODE_HASH_SIZE 256
  1182. static struct member_node_data *nl[NODE_HASH_SIZE] = {0};
  1183. inline int
  1184. hash_sock_addr(unsigned int nodeid)
  1185. {
  1186. return nodeid & (NODE_HASH_SIZE - 1);
  1187. }
  1188. static struct member_node_data **lookup_node(unsigned int nodeid)
  1189. {
  1190. int index = hash_sock_addr(nodeid);
  1191. struct member_node_data **nlp;
  1192. nlp = &nl[index];
  1193. for (nlp = &nl[index]; *nlp; nlp = &((*nlp)->mn_next)) {
  1194. if ((*(nlp))->mn_nodeid == nodeid) {
  1195. break;
  1196. }
  1197. }
  1198. return nlp;
  1199. }
  1200. static struct member_node_data *
  1201. evt_find_node(unsigned int nodeid)
  1202. {
  1203. struct member_node_data **nlp;
  1204. nlp = lookup_node(nodeid);
  1205. if (!nlp) {
  1206. log_printf(LOG_LEVEL_DEBUG, "find_node: Got NULL nlp?\n");
  1207. return 0;
  1208. }
  1209. return *nlp;
  1210. }
  1211. static SaAisErrorT
  1212. evt_add_node(
  1213. unsigned int nodeid,
  1214. SaClmClusterNodeT *cn)
  1215. {
  1216. struct member_node_data **nlp;
  1217. struct member_node_data *nl;
  1218. SaAisErrorT err = SA_AIS_ERR_EXIST;
  1219. nlp = lookup_node(nodeid);
  1220. if (!nlp) {
  1221. log_printf(LOG_LEVEL_DEBUG, "add_node: Got NULL nlp?\n");
  1222. goto an_out;
  1223. }
  1224. if (*nlp) {
  1225. goto an_out;
  1226. }
  1227. *nlp = malloc(sizeof(struct member_node_data));
  1228. if (!(*nlp)) {
  1229. return SA_AIS_ERR_NO_MEMORY;
  1230. }
  1231. nl = *nlp;
  1232. if (nl) {
  1233. memset(nl, 0, sizeof(*nl));
  1234. err = SA_AIS_OK;
  1235. nl->mn_nodeid = nodeid;
  1236. nl->mn_started = 1;
  1237. }
  1238. list_init(&nl->mn_entry);
  1239. list_add(&nl->mn_entry, &mnd);
  1240. nl->mn_node_info = *cn;
  1241. an_out:
  1242. return err;
  1243. }
  1244. /*
  1245. * Find the oldest node in the membership. This is the one we choose to
  1246. * perform some cluster-wide functions like distributing retained events.
  1247. * We only check nodes that were in our transitional configuration. In this
  1248. * way there is a recovery node chosen for each original partition in case
  1249. * of a merge.
  1250. */
  1251. static struct member_node_data* oldest_node()
  1252. {
  1253. struct member_node_data *mn = 0;
  1254. struct member_node_data *oldest = 0;
  1255. int i;
  1256. for (i = 0; i < trans_member_count; i++) {
  1257. mn = evt_find_node(trans_member_list[i]);
  1258. if (!mn || (mn->mn_started == 0)) {
  1259. log_printf(LOG_LEVEL_ERROR,
  1260. "Transitional config Node %s not active\n",
  1261. totempg_ifaces_print (trans_member_list[i]));
  1262. continue;
  1263. }
  1264. if ((oldest == NULL) ||
  1265. (mn->mn_node_info.bootTimestamp <
  1266. oldest->mn_node_info.bootTimestamp )) {
  1267. oldest = mn;
  1268. } else if (mn->mn_node_info.bootTimestamp ==
  1269. oldest->mn_node_info.bootTimestamp) {
  1270. if (mn->mn_node_info.nodeId < oldest->mn_node_info.nodeId) {
  1271. oldest = mn;
  1272. }
  1273. }
  1274. }
  1275. return oldest;
  1276. }
  1277. /*
  1278. * keep track of the last event ID from a node.
  1279. * If we get an event ID less than our last, we've already
  1280. * seen it. It's probably a retained event being sent to
  1281. * a new node.
  1282. */
  1283. static int check_last_event(
  1284. struct lib_event_data *evtpkt,
  1285. unsigned int nodeid)
  1286. {
  1287. struct member_node_data *nd;
  1288. SaClmClusterNodeT *cn;
  1289. nd = evt_find_node(nodeid);
  1290. if (!nd) {
  1291. log_printf(LOG_LEVEL_DEBUG,
  1292. "Node ID %s not found for event %llx\n",
  1293. totempg_ifaces_print (evtpkt->led_publisher_node_id),
  1294. (unsigned long long)evtpkt->led_event_id);
  1295. cn = main_clm_get_by_nodeid(nodeid);
  1296. if (!cn) {
  1297. log_printf(LOG_LEVEL_DEBUG,
  1298. "Cluster Node 0x%s not found for event %llx\n",
  1299. totempg_ifaces_print (evtpkt->led_publisher_node_id),
  1300. (unsigned long long)evtpkt->led_event_id);
  1301. } else {
  1302. evt_add_node(nodeid, cn);
  1303. nd = evt_find_node(nodeid);
  1304. }
  1305. }
  1306. if (!nd) {
  1307. return 0;
  1308. }
  1309. if ((nd->mn_last_msg_id < evtpkt->led_msg_id)) {
  1310. nd->mn_last_msg_id = evtpkt->led_msg_id;
  1311. return 0;
  1312. }
  1313. return 1;
  1314. }
  1315. /*
  1316. * event id generating code. We use the node ID for this node for the
  1317. * upper 32 bits of the event ID to make sure that we can generate a cluster
  1318. * wide unique event ID for a given event.
  1319. */
  1320. SaAisErrorT set_event_id(mar_uint32_t node_id)
  1321. {
  1322. SaAisErrorT err = SA_AIS_OK;
  1323. if (base_id_top) {
  1324. err = SA_AIS_ERR_EXIST;
  1325. }
  1326. base_id_top = (mar_evteventid_t)node_id << 32;
  1327. return err;
  1328. }
  1329. /*
  1330. * See if an event Id is still in use in the retained event
  1331. * list.
  1332. */
  1333. static int id_in_use(uint64_t id, uint64_t base)
  1334. {
  1335. struct list_head *l;
  1336. struct event_data *edp;
  1337. mar_evteventid_t evtid = (id << 32) | (base & BASE_ID_MASK);
  1338. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1339. edp = list_entry(l, struct event_data, ed_retained);
  1340. if (edp->ed_event.led_event_id == evtid) {
  1341. return 1;
  1342. }
  1343. }
  1344. return 0;
  1345. }
  1346. static SaAisErrorT get_event_id(uint64_t *event_id, uint64_t *msg_id)
  1347. {
  1348. /*
  1349. * Don't reuse an event ID if it is still valid because of
  1350. * a retained event.
  1351. */
  1352. while (id_in_use(base_id_top, base_id)) {
  1353. base_id++;
  1354. }
  1355. *event_id = base_id_top | (base_id & BASE_ID_MASK) ;
  1356. *msg_id = base_id++;
  1357. return SA_AIS_OK;
  1358. }
  1359. /*
  1360. * Timer handler to delete expired events.
  1361. *
  1362. */
  1363. static void
  1364. event_retention_timeout(void *data)
  1365. {
  1366. struct event_data *edp = data;
  1367. log_printf(RETENTION_TIME_DEBUG, "Event ID %llx expired\n",
  1368. (unsigned long long)edp->ed_event.led_event_id);
  1369. /*
  1370. * adjust next_retained if we're in recovery and
  1371. * were in charge of sending retained events.
  1372. */
  1373. if (recovery_phase != evt_recovery_complete && recovery_node) {
  1374. if (next_retained == &edp->ed_retained) {
  1375. next_retained = edp->ed_retained.next;
  1376. }
  1377. }
  1378. list_del(&edp->ed_retained);
  1379. list_init(&edp->ed_retained);
  1380. /*
  1381. * Check to see if the channel isn't in use anymore.
  1382. */
  1383. edp->ed_my_chan->esc_retained_count--;
  1384. if (edp->ed_my_chan->esc_retained_count == 0) {
  1385. delete_channel(edp->ed_my_chan);
  1386. }
  1387. free_event_data(edp);
  1388. }
  1389. /*
  1390. * clear a particular event's retention time.
  1391. * This will free the event as long as it isn't being
  1392. * currently used.
  1393. *
  1394. */
  1395. static SaAisErrorT
  1396. clear_retention_time(mar_evteventid_t event_id)
  1397. {
  1398. struct event_data *edp;
  1399. struct list_head *l, *nxt;
  1400. log_printf(RETENTION_TIME_DEBUG, "Search for Event ID %llx\n",
  1401. (unsigned long long)event_id);
  1402. for (l = retained_list.next; l != &retained_list; l = nxt) {
  1403. nxt = l->next;
  1404. edp = list_entry(l, struct event_data, ed_retained);
  1405. if (edp->ed_event.led_event_id != event_id) {
  1406. continue;
  1407. }
  1408. log_printf(RETENTION_TIME_DEBUG,
  1409. "Clear retention time for Event ID %llx\n",
  1410. (unsigned long long)edp->ed_event.led_event_id);
  1411. openais_timer_delete(edp->ed_timer_handle);
  1412. edp->ed_event.led_retention_time = 0;
  1413. list_del(&edp->ed_retained);
  1414. list_init(&edp->ed_retained);
  1415. /*
  1416. * Check to see if the channel isn't in use anymore.
  1417. */
  1418. edp->ed_my_chan->esc_retained_count--;
  1419. if (edp->ed_my_chan->esc_retained_count == 0) {
  1420. delete_channel(edp->ed_my_chan);
  1421. }
  1422. free_event_data(edp);
  1423. return SA_AIS_OK;
  1424. }
  1425. return SA_AIS_ERR_NOT_EXIST;
  1426. }
  1427. /*
  1428. * Remove specified channel from event delivery list
  1429. */
  1430. static void
  1431. remove_delivered_channel(struct event_svr_channel_open *eco)
  1432. {
  1433. int i;
  1434. struct list_head *l;
  1435. struct event_data *edp;
  1436. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1437. edp = list_entry(l, struct event_data, ed_retained);
  1438. for (i = 0; i < edp->ed_delivered_next; i++) {
  1439. if (edp->ed_delivered[i] == eco) {
  1440. edp->ed_delivered_next--;
  1441. if (edp->ed_delivered_next == i) {
  1442. break;
  1443. }
  1444. memmove(&edp->ed_delivered[i],
  1445. &edp->ed_delivered[i+1],
  1446. &edp->ed_delivered[edp->ed_delivered_next] -
  1447. &edp->ed_delivered[i]);
  1448. break;
  1449. }
  1450. }
  1451. }
  1452. }
  1453. /*
  1454. * If there is a retention time, add this open channel to the event so
  1455. * we can check if we've already delivered this message later if a new
  1456. * subscription matches.
  1457. */
  1458. #define DELIVER_SIZE 8
  1459. static void
  1460. evt_delivered(struct event_data *evt, struct event_svr_channel_open *eco)
  1461. {
  1462. if (!evt->ed_event.led_retention_time) {
  1463. return;
  1464. }
  1465. log_printf(LOG_LEVEL_DEBUG, "delivered ID %llx to eco %p\n",
  1466. (unsigned long long)evt->ed_event.led_event_id, eco);
  1467. if (evt->ed_delivered_count == evt->ed_delivered_next) {
  1468. struct event_svr_channel_open **ed_delivered_tmp;
  1469. ed_delivered_tmp = realloc (evt->ed_delivered,
  1470. DELIVER_SIZE * sizeof(struct event_svr_channel_open *));
  1471. if (ed_delivered_tmp == NULL) {
  1472. log_printf(LOG_LEVEL_WARNING, "Memory error realloc\n");
  1473. return;
  1474. }
  1475. evt->ed_delivered = ed_delivered_tmp;
  1476. memset(evt->ed_delivered + evt->ed_delivered_next, 0,
  1477. DELIVER_SIZE * sizeof(struct event_svr_channel_open *));
  1478. evt->ed_delivered_next = evt->ed_delivered_count;
  1479. evt->ed_delivered_count += DELIVER_SIZE;
  1480. }
  1481. evt->ed_delivered[evt->ed_delivered_next++] = eco;
  1482. }
  1483. /*
  1484. * Check to see if an event has already been delivered to this open channel
  1485. */
  1486. static int
  1487. evt_already_delivered(struct event_data *evt,
  1488. struct event_svr_channel_open *eco)
  1489. {
  1490. int i;
  1491. if (!evt->ed_event.led_retention_time) {
  1492. return 0;
  1493. }
  1494. log_printf(LOG_LEVEL_DEBUG, "Deliver count: %d deliver_next %d\n",
  1495. evt->ed_delivered_count, evt->ed_delivered_next);
  1496. for (i = 0; i < evt->ed_delivered_next; i++) {
  1497. log_printf(LOG_LEVEL_DEBUG, "Checking ID %llx delivered %p eco %p\n",
  1498. (unsigned long long)evt->ed_event.led_event_id,
  1499. evt->ed_delivered[i], eco);
  1500. if (evt->ed_delivered[i] == eco) {
  1501. return 1;
  1502. }
  1503. }
  1504. return 0;
  1505. }
  1506. /*
  1507. * Compare a filter to a given pattern.
  1508. * return SA_AIS_OK if the pattern matches a filter
  1509. */
  1510. static SaAisErrorT
  1511. filter_match(mar_evt_event_pattern_t *ep, mar_evt_event_filter_t *ef)
  1512. {
  1513. int ret;
  1514. ret = SA_AIS_ERR_FAILED_OPERATION;
  1515. switch (ef->filter_type) {
  1516. case SA_EVT_PREFIX_FILTER:
  1517. if (ef->filter.pattern_size > ep->pattern_size) {
  1518. break;
  1519. }
  1520. if (strncmp((char *)ef->filter.pattern, (char *)ep->pattern,
  1521. ef->filter.pattern_size) == 0) {
  1522. ret = SA_AIS_OK;
  1523. }
  1524. break;
  1525. case SA_EVT_SUFFIX_FILTER:
  1526. if (ef->filter.pattern_size > ep->pattern_size) {
  1527. break;
  1528. }
  1529. if (strncmp((char *)ef->filter.pattern,
  1530. (char *)&ep->pattern[ep->pattern_size - ef->filter.pattern_size],
  1531. ef->filter.pattern_size) == 0) {
  1532. ret = SA_AIS_OK;
  1533. }
  1534. break;
  1535. case SA_EVT_EXACT_FILTER:
  1536. if (ef->filter.pattern_size != ep->pattern_size) {
  1537. break;
  1538. }
  1539. if (strncmp((char *)ef->filter.pattern, (char *)ep->pattern,
  1540. ef->filter.pattern_size) == 0) {
  1541. ret = SA_AIS_OK;
  1542. }
  1543. break;
  1544. case SA_EVT_PASS_ALL_FILTER:
  1545. ret = SA_AIS_OK;
  1546. break;
  1547. default:
  1548. break;
  1549. }
  1550. return ret;
  1551. }
  1552. /*
  1553. * compare the event's patterns with the subscription's filter rules.
  1554. * SA_AIS_OK is returned if the event matches the filter rules.
  1555. */
  1556. static SaAisErrorT
  1557. event_match(struct event_data *evt,
  1558. struct event_svr_channel_subscr *ecs)
  1559. {
  1560. mar_evt_event_filter_t *ef;
  1561. mar_evt_event_pattern_t *ep;
  1562. uint32_t filt_count;
  1563. SaAisErrorT ret = SA_AIS_OK;
  1564. int i;
  1565. ep = (mar_evt_event_pattern_t *)(&evt->ed_event.led_body[0]);
  1566. ef = ecs->ecs_filters->filters;
  1567. filt_count = min(ecs->ecs_filters->filters_number,
  1568. evt->ed_event.led_patterns_number);
  1569. for (i = 0; i < filt_count; i++) {
  1570. ret = filter_match(ep, ef);
  1571. if (ret != SA_AIS_OK) {
  1572. break;
  1573. }
  1574. ep++;
  1575. ef++;
  1576. }
  1577. return ret;
  1578. }
  1579. /*
  1580. * Scan undelivered pending events and either remove them if no subscription
  1581. * filters match anymore or re-assign them to another matching subscription
  1582. */
  1583. static void
  1584. filter_undelivered_events(struct event_svr_channel_open *op_chan)
  1585. {
  1586. struct event_svr_channel_open *eco;
  1587. struct event_svr_channel_instance *eci;
  1588. struct event_svr_channel_subscr *ecs;
  1589. struct chan_event_list *cel;
  1590. struct libevt_pd *esip;
  1591. struct list_head *l, *nxt;
  1592. struct list_head *l1, *l2;
  1593. int i;
  1594. esip = (struct libevt_pd *)openais_conn_private_data_get(op_chan->eco_conn);
  1595. eci = op_chan->eco_channel;
  1596. /*
  1597. * Scan each of the priority queues for messages
  1598. */
  1599. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  1600. /*
  1601. * examine each message queued for delivery
  1602. */
  1603. for (l = esip->esi_events[i].next; l != &esip->esi_events[i]; l = nxt) {
  1604. nxt = l->next;
  1605. cel = list_entry(l, struct chan_event_list, cel_entry);
  1606. /*
  1607. * Check open channels
  1608. */
  1609. for (l1 = eci->esc_open_chans.next;
  1610. l1 != &eci->esc_open_chans; l1 = l1->next) {
  1611. eco = list_entry(l1, struct event_svr_channel_open, eco_entry);
  1612. /*
  1613. * See if this channel open instance belongs
  1614. * to this evtinitialize instance
  1615. */
  1616. if (eco->eco_conn != op_chan->eco_conn) {
  1617. continue;
  1618. }
  1619. /*
  1620. * See if enabled to receive
  1621. */
  1622. if (!(eco->eco_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  1623. continue;
  1624. }
  1625. /*
  1626. * Check subscriptions
  1627. */
  1628. for (l2 = eco->eco_subscr.next;
  1629. l2 != &eco->eco_subscr; l2 = l2->next) {
  1630. ecs = list_entry(l2,
  1631. struct event_svr_channel_subscr, ecs_entry);
  1632. if (event_match(cel->cel_event, ecs) == SA_AIS_OK) {
  1633. /*
  1634. * Something still matches.
  1635. * We'll assign it to
  1636. * the new subscription.
  1637. */
  1638. cel->cel_sub_id = ecs->ecs_sub_id;
  1639. cel->cel_chan_handle = eco->eco_lib_handle;
  1640. goto next_event;
  1641. }
  1642. }
  1643. }
  1644. /*
  1645. * No subscription filter matches anymore. We
  1646. * can delete this event.
  1647. */
  1648. list_del(&cel->cel_entry);
  1649. list_init(&cel->cel_entry);
  1650. esip->esi_nevents--;
  1651. free_event_data(cel->cel_event);
  1652. free(cel);
  1653. next_event:
  1654. continue;
  1655. }
  1656. }
  1657. }
  1658. /*
  1659. * Notify the library of a pending event
  1660. */
  1661. static void __notify_event(void *conn)
  1662. {
  1663. struct res_evt_event_data res;
  1664. struct libevt_pd *esip;
  1665. esip = (struct libevt_pd *)openais_conn_private_data_get(conn);
  1666. log_printf(LOG_LEVEL_DEBUG, "DELIVER: notify\n");
  1667. if (esip->esi_nevents != 0) {
  1668. res.evd_head.size = sizeof(res);
  1669. res.evd_head.id = MESSAGE_RES_EVT_AVAILABLE;
  1670. res.evd_head.error = SA_AIS_OK;
  1671. openais_conn_send_response(openais_conn_partner_get(conn),
  1672. &res, sizeof(res));
  1673. }
  1674. }
  1675. inline void notify_event(void *conn)
  1676. {
  1677. struct libevt_pd *esip;
  1678. esip = (struct libevt_pd *)openais_conn_private_data_get(conn);
  1679. /*
  1680. * Give the library a kick if there aren't already
  1681. * events queued for delivery.
  1682. */
  1683. if (esip->esi_nevents++ == 0) {
  1684. __notify_event(conn);
  1685. }
  1686. }
  1687. /*
  1688. * sends/queues up an event for a subscribed channel.
  1689. */
  1690. static void
  1691. deliver_event(struct event_data *evt,
  1692. struct event_svr_channel_open *eco,
  1693. struct event_svr_channel_subscr *ecs)
  1694. {
  1695. struct chan_event_list *ep;
  1696. SaEvtEventPriorityT evt_prio = evt->ed_event.led_priority;
  1697. struct chan_event_list *cel;
  1698. int do_deliver_event = 0;
  1699. int do_deliver_warning = 0;
  1700. int i;
  1701. struct libevt_pd *esip;
  1702. esip = (struct libevt_pd *)openais_conn_private_data_get(eco->eco_conn);
  1703. if (evt_prio > SA_EVT_LOWEST_PRIORITY) {
  1704. evt_prio = SA_EVT_LOWEST_PRIORITY;
  1705. }
  1706. /*
  1707. * Delivery queue check.
  1708. * - If the queue is blocked, see if we've sent enough messages to
  1709. * unblock it.
  1710. * - If it isn't blocked, see if this message will put us over the top.
  1711. * - If we can't deliver this message, see if we can toss some lower
  1712. * priority message to make room for this one.
  1713. * - If we toss any messages, queue up an event of SA_EVT_LOST_EVENT_PATTERN
  1714. * to let the application know that we dropped some messages.
  1715. */
  1716. if (esip->esi_queue_blocked) {
  1717. if (esip->esi_nevents < evt_delivery_queue_resume) {
  1718. esip->esi_queue_blocked = 0;
  1719. log_printf(LOG_LEVEL_DEBUG, "unblock\n");
  1720. }
  1721. }
  1722. assert (esip->esi_nevents >= 0);
  1723. if (!esip->esi_queue_blocked &&
  1724. (esip->esi_nevents >= evt_delivery_queue_size)) {
  1725. log_printf(LOG_LEVEL_DEBUG, "block\n");
  1726. esip->esi_queue_blocked = 1;
  1727. do_deliver_warning = 1;
  1728. }
  1729. if (esip->esi_queue_blocked) {
  1730. do_deliver_event = 0;
  1731. for (i = SA_EVT_LOWEST_PRIORITY; i > evt_prio; i--) {
  1732. if (!list_empty(&esip->esi_events[i])) {
  1733. /*
  1734. * Get the last item on the list, so we drop the most
  1735. * recent lowest priority event.
  1736. */
  1737. cel = list_entry(esip->esi_events[i].prev,
  1738. struct chan_event_list, cel_entry);
  1739. log_printf(LOG_LEVEL_DEBUG, "Drop 0x%0llx\n",
  1740. (unsigned long long)cel->cel_event->ed_event.led_event_id);
  1741. list_del(&cel->cel_entry);
  1742. free_event_data(cel->cel_event);
  1743. free(cel);
  1744. esip->esi_nevents--;
  1745. do_deliver_event = 1;
  1746. break;
  1747. }
  1748. }
  1749. } else {
  1750. do_deliver_event = 1;
  1751. }
  1752. /*
  1753. * Queue the event for delivery
  1754. */
  1755. if (do_deliver_event) {
  1756. ep = malloc(sizeof(*ep));
  1757. if (!ep) {
  1758. log_printf(LOG_LEVEL_WARNING,
  1759. "3Memory allocation error, can't deliver event\n");
  1760. return;
  1761. }
  1762. evt->ed_ref_count++;
  1763. ep->cel_chan_handle = eco->eco_lib_handle;
  1764. ep->cel_sub_id = ecs->ecs_sub_id;
  1765. list_init(&ep->cel_entry);
  1766. ep->cel_event = evt;
  1767. list_add_tail(&ep->cel_entry, &esip->esi_events[evt_prio]);
  1768. evt_delivered(evt, eco);
  1769. notify_event(eco->eco_conn);
  1770. }
  1771. /*
  1772. * If we dropped an event, queue this so that the application knows
  1773. * what has happened.
  1774. */
  1775. if (do_deliver_warning) {
  1776. struct event_data *ed;
  1777. ed = malloc(dropped_event_size);
  1778. if (!ed) {
  1779. log_printf(LOG_LEVEL_WARNING,
  1780. "4Memory allocation error, can't deliver event\n");
  1781. return;
  1782. }
  1783. log_printf(LOG_LEVEL_DEBUG, "Warn 0x%0llx\n",
  1784. (unsigned long long)evt->ed_event.led_event_id);
  1785. memcpy(ed, dropped_event, dropped_event_size);
  1786. ed->ed_event.led_publish_time = clust_time_now();
  1787. ed->ed_event.led_event_id = SA_EVT_EVENTID_LOST;
  1788. list_init(&ed->ed_retained);
  1789. ep = malloc(sizeof(*ep));
  1790. if (!ep) {
  1791. log_printf(LOG_LEVEL_WARNING,
  1792. "5Memory allocation error, can't deliver event\n");
  1793. return;
  1794. }
  1795. ep->cel_chan_handle = eco->eco_lib_handle;
  1796. ep->cel_sub_id = ecs->ecs_sub_id;
  1797. list_init(&ep->cel_entry);
  1798. ep->cel_event = ed;
  1799. list_add_tail(&ep->cel_entry, &esip->esi_events[SA_EVT_HIGHEST_PRIORITY]);
  1800. notify_event(eco->eco_conn);
  1801. }
  1802. }
  1803. /*
  1804. * Take the event data and swap the elements so they match our architectures
  1805. * word order.
  1806. */
  1807. static void
  1808. convert_event(void *msg)
  1809. {
  1810. struct lib_event_data *evt = (struct lib_event_data *)msg;
  1811. mar_evt_event_pattern_t *eps;
  1812. int i;
  1813. /*
  1814. * The following elements don't require processing:
  1815. *
  1816. * converted in the main deliver_fn:
  1817. * led_head.id, led_head.size.
  1818. *
  1819. * Supplied localy:
  1820. * source_addr, publisher_node_id, receive_time.
  1821. *
  1822. * Used internaly only:
  1823. * led_svr_channel_handle and led_lib_channel_handle.
  1824. */
  1825. swab_mar_name_t (&evt->led_chan_name);
  1826. evt->led_chan_unlink_id = swab64(evt->led_chan_unlink_id);
  1827. evt->led_event_id = swab64(evt->led_event_id);
  1828. evt->led_sub_id = swab32(evt->led_sub_id);
  1829. swab_mar_name_t (&evt->led_publisher_name);
  1830. evt->led_retention_time = swab64(evt->led_retention_time);
  1831. evt->led_publish_time = swab64(evt->led_publish_time);
  1832. evt->led_user_data_offset = swab32(evt->led_user_data_offset);
  1833. evt->led_user_data_size = swab32(evt->led_user_data_size);
  1834. evt->led_patterns_number = swab32(evt->led_patterns_number);
  1835. /*
  1836. * Now we need to go through the led_body and swizzle pattern counts.
  1837. * We can't do anything about user data since it doesn't have a specified
  1838. * format. The application is on its own here.
  1839. */
  1840. eps = (mar_evt_event_pattern_t *)evt->led_body;
  1841. for (i = 0; i < evt->led_patterns_number; i++) {
  1842. eps->pattern_size = swab32(eps->pattern_size);
  1843. eps->allocated_size = swab32(eps->allocated_size);
  1844. eps++;
  1845. }
  1846. }
  1847. /*
  1848. * Take an event received from the network and fix it up to be usable.
  1849. * - fix up pointers for pattern list.
  1850. * - fill in some channel info
  1851. */
  1852. static struct event_data *
  1853. make_local_event(struct lib_event_data *p,
  1854. struct event_svr_channel_instance *eci)
  1855. {
  1856. struct event_data *ed;
  1857. mar_evt_event_pattern_t *eps;
  1858. SaUint8T *str;
  1859. uint32_t ed_size;
  1860. int i;
  1861. ed_size = sizeof(*ed) + p->led_user_data_offset + p->led_user_data_size;
  1862. ed = malloc(ed_size);
  1863. if (!ed) {
  1864. log_printf(LOG_LEVEL_WARNING,
  1865. "Failed to allocate %u bytes for event, offset %u, data size %u\n",
  1866. ed_size, p->led_user_data_offset, p->led_user_data_size);
  1867. return 0;
  1868. }
  1869. memset(ed, 0, ed_size);
  1870. list_init(&ed->ed_retained);
  1871. ed->ed_my_chan = eci;
  1872. /*
  1873. * Fill in lib_event_data and make the pattern pointers valid
  1874. */
  1875. memcpy(&ed->ed_event, p, sizeof(*p) +
  1876. p->led_user_data_offset + p->led_user_data_size);
  1877. eps = (mar_evt_event_pattern_t *)ed->ed_event.led_body;
  1878. str = ed->ed_event.led_body +
  1879. (ed->ed_event.led_patterns_number * sizeof(mar_evt_event_pattern_t));
  1880. for (i = 0; i < ed->ed_event.led_patterns_number; i++) {
  1881. eps->pattern = str;
  1882. str += eps->pattern_size;
  1883. eps++;
  1884. }
  1885. ed->ed_ref_count++;
  1886. return ed;
  1887. }
  1888. /*
  1889. * Set an event to be retained.
  1890. */
  1891. static void retain_event(struct event_data *evt)
  1892. {
  1893. uint32_t ret;
  1894. evt->ed_ref_count++;
  1895. evt->ed_my_chan->esc_retained_count++;
  1896. list_add_tail(&evt->ed_retained, &retained_list);
  1897. ret = openais_timer_add_duration (
  1898. evt->ed_event.led_retention_time,
  1899. evt,
  1900. event_retention_timeout,
  1901. &evt->ed_timer_handle);
  1902. if (ret != 0) {
  1903. log_printf(LOG_LEVEL_ERROR,
  1904. "retention of event id 0x%llx failed\n",
  1905. (unsigned long long)evt->ed_event.led_event_id);
  1906. } else {
  1907. log_printf(RETENTION_TIME_DEBUG, "Retain event ID 0x%llx for %llu ms\n",
  1908. (unsigned long long)evt->ed_event.led_event_id, evt->ed_event.led_retention_time/100000LL);
  1909. }
  1910. }
  1911. /*
  1912. * Scan the subscription list and look for the specified subsctiption ID.
  1913. * Only look for the ID in subscriptions that are associated with the
  1914. * saEvtInitialize associated with the specified open channel.
  1915. */
  1916. static struct event_svr_channel_subscr *find_subscr(
  1917. struct event_svr_channel_open *open_chan, SaEvtSubscriptionIdT sub_id)
  1918. {
  1919. struct event_svr_channel_instance *eci;
  1920. struct event_svr_channel_subscr *ecs;
  1921. struct event_svr_channel_open *eco;
  1922. struct list_head *l, *l1;
  1923. void *conn = open_chan->eco_conn;
  1924. eci = open_chan->eco_channel;
  1925. /*
  1926. * Check for subscription id already in use.
  1927. * Subscriptions are unique within saEvtInitialize (Callback scope).
  1928. */
  1929. for (l = eci->esc_open_chans.next; l != &eci->esc_open_chans; l = l->next) {
  1930. eco = list_entry(l, struct event_svr_channel_open, eco_entry);
  1931. /*
  1932. * Don't bother with open channels associated with another
  1933. * EvtInitialize
  1934. */
  1935. if (eco->eco_conn != conn) {
  1936. continue;
  1937. }
  1938. for (l1 = eco->eco_subscr.next; l1 != &eco->eco_subscr; l1 = l1->next) {
  1939. ecs = list_entry(l1, struct event_svr_channel_subscr, ecs_entry);
  1940. if (ecs->ecs_sub_id == sub_id) {
  1941. return ecs;
  1942. }
  1943. }
  1944. }
  1945. return 0;
  1946. }
  1947. /*
  1948. * Handler for saEvtInitialize
  1949. */
  1950. static int evt_lib_init(void *conn)
  1951. {
  1952. struct libevt_pd *libevt_pd;
  1953. int i;
  1954. libevt_pd = (struct libevt_pd *)openais_conn_private_data_get(conn);
  1955. log_printf(LOG_LEVEL_DEBUG, "saEvtInitialize request.\n");
  1956. /*
  1957. * Initailze event instance data
  1958. */
  1959. memset(libevt_pd, 0, sizeof(*libevt_pd));
  1960. /*
  1961. * Initialize the open channel handle database.
  1962. */
  1963. hdb_create(&libevt_pd->esi_hdb);
  1964. /*
  1965. * list of channels open on this instance
  1966. */
  1967. list_init(&libevt_pd->esi_open_chans);
  1968. /*
  1969. * pending event lists for each piriority
  1970. */
  1971. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  1972. list_init(&libevt_pd->esi_events[i]);
  1973. }
  1974. return 0;
  1975. }
  1976. /*
  1977. * Handler for saEvtChannelOpen
  1978. */
  1979. static void lib_evt_open_channel(void *conn, void *message)
  1980. {
  1981. SaAisErrorT error;
  1982. struct req_evt_channel_open *req;
  1983. struct res_evt_channel_open res;
  1984. struct open_chan_pending *ocp;
  1985. int ret;
  1986. req = message;
  1987. log_printf(CHAN_OPEN_DEBUG,
  1988. "saEvtChannelOpen (Open channel request)\n");
  1989. log_printf(CHAN_OPEN_DEBUG,
  1990. "handle 0x%llx, to 0x%llx\n",
  1991. (unsigned long long)req->ico_c_handle,
  1992. (unsigned long long)req->ico_timeout);
  1993. log_printf(CHAN_OPEN_DEBUG, "flags %x, channel name(%d) %s\n",
  1994. req->ico_open_flag,
  1995. req->ico_channel_name.length,
  1996. req->ico_channel_name.value);
  1997. /*
  1998. * Open the channel.
  1999. *
  2000. */
  2001. error = evt_open_channel(&req->ico_channel_name, req->ico_open_flag);
  2002. if (error != SA_AIS_OK) {
  2003. goto open_return;
  2004. }
  2005. ocp = malloc(sizeof(struct open_chan_pending));
  2006. if (!ocp) {
  2007. error = SA_AIS_ERR_NO_MEMORY;
  2008. goto open_return;
  2009. }
  2010. ocp->ocp_async = 0;
  2011. ocp->ocp_invocation = 0;
  2012. ocp->ocp_chan_name = req->ico_channel_name;
  2013. ocp->ocp_open_flag = req->ico_open_flag;
  2014. ocp->ocp_conn = conn;
  2015. ocp->ocp_c_handle = req->ico_c_handle;
  2016. ocp->ocp_timer_handle = 0;
  2017. ocp->ocp_serial_no = open_serial_no;
  2018. list_init(&ocp->ocp_entry);
  2019. list_add_tail(&ocp->ocp_entry, &open_pending);
  2020. ret = openais_timer_add_duration (
  2021. req->ico_timeout,
  2022. ocp,
  2023. chan_open_timeout,
  2024. &ocp->ocp_timer_handle);
  2025. if (ret != 0) {
  2026. log_printf(LOG_LEVEL_WARNING,
  2027. "Error setting timeout for open channel %s\n",
  2028. req->ico_channel_name.value);
  2029. }
  2030. return;
  2031. open_return:
  2032. res.ico_head.size = sizeof(res);
  2033. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2034. res.ico_head.error = error;
  2035. openais_conn_send_response(conn, &res, sizeof(res));
  2036. }
  2037. /*
  2038. * Handler for saEvtChannelOpen
  2039. */
  2040. static void lib_evt_open_channel_async(void *conn, void *message)
  2041. {
  2042. SaAisErrorT error;
  2043. struct req_evt_channel_open *req;
  2044. struct res_evt_channel_open res;
  2045. struct open_chan_pending *ocp;
  2046. req = message;
  2047. log_printf(CHAN_OPEN_DEBUG,
  2048. "saEvtChannelOpenAsync (Async Open channel request)\n");
  2049. log_printf(CHAN_OPEN_DEBUG,
  2050. "handle 0x%llx, to 0x%llx\n",
  2051. (unsigned long long)req->ico_c_handle,
  2052. (unsigned long long)req->ico_invocation);
  2053. log_printf(CHAN_OPEN_DEBUG, "flags %x, channel name(%d) %s\n",
  2054. req->ico_open_flag,
  2055. req->ico_channel_name.length,
  2056. req->ico_channel_name.value);
  2057. /*
  2058. * Open the channel.
  2059. *
  2060. */
  2061. error = evt_open_channel(&req->ico_channel_name, req->ico_open_flag);
  2062. if (error != SA_AIS_OK) {
  2063. goto open_return;
  2064. }
  2065. ocp = malloc(sizeof(struct open_chan_pending));
  2066. if (!ocp) {
  2067. error = SA_AIS_ERR_NO_MEMORY;
  2068. goto open_return;
  2069. }
  2070. ocp->ocp_async = 1;
  2071. ocp->ocp_invocation = req->ico_invocation;
  2072. ocp->ocp_c_handle = req->ico_c_handle;
  2073. ocp->ocp_chan_name = req->ico_channel_name;
  2074. ocp->ocp_open_flag = req->ico_open_flag;
  2075. ocp->ocp_conn = conn;
  2076. ocp->ocp_timer_handle = 0;
  2077. ocp->ocp_serial_no = open_serial_no;
  2078. list_init(&ocp->ocp_entry);
  2079. list_add_tail(&ocp->ocp_entry, &open_pending);
  2080. open_return:
  2081. res.ico_head.size = sizeof(res);
  2082. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2083. res.ico_head.error = error;
  2084. openais_conn_send_response(conn, &res, sizeof(res));
  2085. }
  2086. /*
  2087. * Used by the channel close code and by the implicit close
  2088. * when saEvtFinalize is called with channels open.
  2089. */
  2090. static SaAisErrorT
  2091. common_chan_close(struct event_svr_channel_open *eco, struct libevt_pd *esip)
  2092. {
  2093. struct event_svr_channel_subscr *ecs;
  2094. struct list_head *l, *nxt;
  2095. log_printf(LOG_LEVEL_DEBUG, "Close channel %s flags 0x%02x\n",
  2096. eco->eco_channel->esc_channel_name.value,
  2097. eco->eco_flags);
  2098. /*
  2099. * Disconnect the channel open structure.
  2100. *
  2101. * Check for subscriptions and deal with them. In this case
  2102. * if there are any, we just implicitly unsubscribe.
  2103. *
  2104. * When We're done with the channel open data then we can
  2105. * remove it's handle (this frees the memory too).
  2106. *
  2107. */
  2108. list_del(&eco->eco_entry);
  2109. list_del(&eco->eco_instance_entry);
  2110. for (l = eco->eco_subscr.next; l != &eco->eco_subscr; l = nxt) {
  2111. nxt = l->next;
  2112. ecs = list_entry(l, struct event_svr_channel_subscr, ecs_entry);
  2113. log_printf(LOG_LEVEL_DEBUG, "Unsubscribe ID: %x\n",
  2114. ecs->ecs_sub_id);
  2115. list_del(&ecs->ecs_entry);
  2116. free(ecs);
  2117. /*
  2118. * Purge any pending events associated with this subscription
  2119. * that don't match another subscription.
  2120. */
  2121. filter_undelivered_events(eco);
  2122. }
  2123. /*
  2124. * Remove this channel from the retained event's notion
  2125. * of who they have been delivered to.
  2126. */
  2127. remove_delivered_channel(eco);
  2128. return evt_close_channel(&eco->eco_channel->esc_channel_name,
  2129. eco->eco_channel->esc_unlink_id);
  2130. }
  2131. /*
  2132. * Handler for saEvtChannelClose
  2133. */
  2134. static void lib_evt_close_channel(void *conn, void *message)
  2135. {
  2136. struct req_evt_channel_close *req;
  2137. struct res_evt_channel_close res;
  2138. struct event_svr_channel_open *eco;
  2139. unsigned int ret;
  2140. void *ptr;
  2141. struct libevt_pd *esip;
  2142. esip = (struct libevt_pd *)openais_conn_private_data_get(conn);
  2143. req = message;
  2144. log_printf(LOG_LEVEL_DEBUG,
  2145. "saEvtChannelClose (Close channel request)\n");
  2146. log_printf(LOG_LEVEL_DEBUG, "handle 0x%x\n", req->icc_channel_handle);
  2147. /*
  2148. * look up the channel handle
  2149. */
  2150. ret = hdb_handle_get(&esip->esi_hdb,
  2151. req->icc_channel_handle, &ptr);
  2152. if (ret != 0) {
  2153. goto chan_close_done;
  2154. }
  2155. eco = ptr;
  2156. common_chan_close(eco, esip);
  2157. hdb_handle_destroy(&esip->esi_hdb, req->icc_channel_handle);
  2158. hdb_handle_put(&esip->esi_hdb, req->icc_channel_handle);
  2159. chan_close_done:
  2160. res.icc_head.size = sizeof(res);
  2161. res.icc_head.id = MESSAGE_RES_EVT_CLOSE_CHANNEL;
  2162. res.icc_head.error = ((ret == 0) ? SA_AIS_OK : SA_AIS_ERR_BAD_HANDLE);
  2163. openais_conn_send_response(conn, &res, sizeof(res));
  2164. }
  2165. /*
  2166. * Handler for saEvtChannelUnlink
  2167. */
  2168. static void lib_evt_unlink_channel(void *conn, void *message)
  2169. {
  2170. struct req_evt_channel_unlink *req;
  2171. struct res_evt_channel_unlink res;
  2172. struct iovec chn_iovec;
  2173. struct unlink_chan_pending *ucp = 0;
  2174. struct req_evt_chan_command cpkt;
  2175. SaAisErrorT error = SA_AIS_ERR_LIBRARY;
  2176. req = message;
  2177. log_printf(CHAN_UNLINK_DEBUG,
  2178. "saEvtChannelUnlink (Unlink channel request)\n");
  2179. log_printf(CHAN_UNLINK_DEBUG, "Channel Name %s\n",
  2180. req->iuc_channel_name.value);
  2181. if (!find_channel(&req->iuc_channel_name, EVT_CHAN_ACTIVE)) {
  2182. log_printf(CHAN_UNLINK_DEBUG, "Channel Name doesn't exist\n");
  2183. error = SA_AIS_ERR_NOT_EXIST;
  2184. goto evt_unlink_err;
  2185. }
  2186. /*
  2187. * Set up the data structure so that the channel op
  2188. * mcast handler can complete the unlink comamnd back to the
  2189. * requestor.
  2190. */
  2191. ucp = malloc(sizeof(*ucp));
  2192. if (!ucp) {
  2193. log_printf(LOG_LEVEL_ERROR,
  2194. "saEvtChannelUnlink: Memory allocation failure\n");
  2195. error = SA_AIS_ERR_TRY_AGAIN;
  2196. goto evt_unlink_err;
  2197. }
  2198. ucp->ucp_unlink_id = next_chan_unlink_id();
  2199. ucp->ucp_conn = conn;
  2200. list_init(&ucp->ucp_entry);
  2201. list_add_tail(&ucp->ucp_entry, &unlink_pending);
  2202. /*
  2203. * Put together a mcast packet to notify everyone
  2204. * of the channel unlink.
  2205. */
  2206. memset(&cpkt, 0, sizeof(cpkt));
  2207. cpkt.chc_head.id =
  2208. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  2209. cpkt.chc_head.size = sizeof(cpkt);
  2210. cpkt.chc_op = EVT_UNLINK_CHAN_OP;
  2211. cpkt.u.chcu.chcu_name = req->iuc_channel_name;
  2212. cpkt.u.chcu.chcu_unlink_id = ucp->ucp_unlink_id;
  2213. chn_iovec.iov_base = (char *)&cpkt;
  2214. chn_iovec.iov_len = cpkt.chc_head.size;
  2215. if (totempg_groups_mcast_joined(openais_group_handle,
  2216. &chn_iovec, 1, TOTEMPG_AGREED) == 0) {
  2217. return;
  2218. }
  2219. evt_unlink_err:
  2220. if (ucp) {
  2221. list_del(&ucp->ucp_entry);
  2222. free(ucp);
  2223. }
  2224. res.iuc_head.size = sizeof(res);
  2225. res.iuc_head.id = MESSAGE_RES_EVT_UNLINK_CHANNEL;
  2226. res.iuc_head.error = error;
  2227. openais_conn_send_response(conn, &res, sizeof(res));
  2228. }
  2229. /*
  2230. * Subscribe to an event channel.
  2231. *
  2232. * - First look up the channel to subscribe.
  2233. * - Make sure that the subscription ID is not already in use.
  2234. * - Fill in the subscription data structures and add them to the channels
  2235. * subscription list.
  2236. * - See if there are any events with retetion times that need to be delivered
  2237. * because of the new subscription.
  2238. */
  2239. static char *filter_types[] = {
  2240. "INVALID FILTER TYPE",
  2241. "SA_EVT_PREFIX_FILTER",
  2242. "SA_EVT_SUFFIX_FILTER",
  2243. "SA_EVT_EXACT_FILTER",
  2244. "SA_EVT_PASS_ALL_FILTER",
  2245. };
  2246. /*
  2247. * saEvtEventSubscribe Handler
  2248. */
  2249. static void lib_evt_event_subscribe(void *conn, void *message)
  2250. {
  2251. struct req_evt_event_subscribe *req;
  2252. struct res_evt_event_subscribe res;
  2253. mar_evt_event_filter_array_t *filters;
  2254. SaAisErrorT error;
  2255. struct event_svr_channel_open *eco;
  2256. struct event_svr_channel_instance *eci;
  2257. struct event_svr_channel_subscr *ecs;
  2258. struct event_data *evt;
  2259. struct list_head *l;
  2260. void *ptr;
  2261. unsigned int ret;
  2262. int i;
  2263. struct libevt_pd *esip;
  2264. esip = (struct libevt_pd *)openais_conn_private_data_get(conn);
  2265. req = message;
  2266. log_printf(LOG_LEVEL_DEBUG,
  2267. "saEvtEventSubscribe (Subscribe request)\n");
  2268. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%llx\n",
  2269. (unsigned long long)req->ics_sub_id);
  2270. /*
  2271. * look up the channel handle
  2272. */
  2273. ret = hdb_handle_get(&esip->esi_hdb, req->ics_channel_handle, &ptr);
  2274. if (ret != 0) {
  2275. error = SA_AIS_ERR_BAD_HANDLE;
  2276. goto subr_done;
  2277. }
  2278. eco = ptr;
  2279. eci = eco->eco_channel;
  2280. /*
  2281. * See if the id is already being used
  2282. */
  2283. ecs = find_subscr(eco, req->ics_sub_id);
  2284. if (ecs) {
  2285. error = SA_AIS_ERR_EXIST;
  2286. goto subr_put;
  2287. }
  2288. error = evtfilt_to_aisfilt(req, &filters);
  2289. if (error == SA_AIS_OK) {
  2290. log_printf(LOG_LEVEL_DEBUG, "Subscribe filters count %d\n",
  2291. (int)filters->filters_number);
  2292. for (i = 0; i < filters->filters_number; i++) {
  2293. log_printf(LOG_LEVEL_DEBUG, "type %s(%d) sz %d, <%s>\n",
  2294. filter_types[filters->filters[i].filter_type],
  2295. filters->filters[i].filter_type,
  2296. (int)filters->filters[i].filter.pattern_size,
  2297. (filters->filters[i].filter.pattern_size)
  2298. ? (char *)filters->filters[i].filter.pattern
  2299. : "");
  2300. }
  2301. }
  2302. if (error != SA_AIS_OK) {
  2303. goto subr_put;
  2304. }
  2305. ecs = (struct event_svr_channel_subscr *)malloc(sizeof(*ecs));
  2306. if (!ecs) {
  2307. error = SA_AIS_ERR_NO_MEMORY;
  2308. goto subr_put;
  2309. }
  2310. ecs->ecs_filters = filters;
  2311. ecs->ecs_sub_id = req->ics_sub_id;
  2312. list_init(&ecs->ecs_entry);
  2313. list_add(&ecs->ecs_entry, &eco->eco_subscr);
  2314. res.ics_head.size = sizeof(res);
  2315. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  2316. res.ics_head.error = error;
  2317. openais_conn_send_response(conn, &res, sizeof(res));
  2318. /*
  2319. * See if an existing event with a retention time
  2320. * needs to be delivered based on this subscription
  2321. */
  2322. for (l = retained_list.next; l != &retained_list; l = l->next) {
  2323. evt = list_entry(l, struct event_data, ed_retained);
  2324. log_printf(LOG_LEVEL_DEBUG,
  2325. "Checking event ID %llx chanp %p -- sub chanp %p\n",
  2326. (unsigned long long)evt->ed_event.led_event_id,
  2327. evt->ed_my_chan, eci);
  2328. if (evt->ed_my_chan == eci) {
  2329. if (evt_already_delivered(evt, eco)) {
  2330. continue;
  2331. }
  2332. if (event_match(evt, ecs) == SA_AIS_OK) {
  2333. log_printf(LOG_LEVEL_DEBUG,
  2334. "deliver event ID: 0x%llx\n",
  2335. (unsigned long long)evt->ed_event.led_event_id);
  2336. deliver_event(evt, eco, ecs);
  2337. }
  2338. }
  2339. }
  2340. hdb_handle_put(&esip->esi_hdb, req->ics_channel_handle);
  2341. return;
  2342. subr_put:
  2343. hdb_handle_put(&esip->esi_hdb, req->ics_channel_handle);
  2344. subr_done:
  2345. res.ics_head.size = sizeof(res);
  2346. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  2347. res.ics_head.error = error;
  2348. openais_conn_send_response(conn, &res, sizeof(res));
  2349. }
  2350. /*
  2351. * saEvtEventUnsubscribe Handler
  2352. */
  2353. static void lib_evt_event_unsubscribe(void *conn, void *message)
  2354. {
  2355. struct req_evt_event_unsubscribe *req;
  2356. struct res_evt_event_unsubscribe res;
  2357. struct event_svr_channel_open *eco;
  2358. struct event_svr_channel_instance *eci;
  2359. struct event_svr_channel_subscr *ecs;
  2360. SaAisErrorT error = SA_AIS_OK;
  2361. unsigned int ret;
  2362. void *ptr;
  2363. struct libevt_pd *esip;
  2364. esip = (struct libevt_pd *)openais_conn_private_data_get(conn);
  2365. req = message;
  2366. log_printf(LOG_LEVEL_DEBUG,
  2367. "saEvtEventUnsubscribe (Unsubscribe request)\n");
  2368. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%llx\n",
  2369. (unsigned long long)req->icu_sub_id);
  2370. /*
  2371. * look up the channel handle, get the open channel
  2372. * data.
  2373. */
  2374. ret = hdb_handle_get(&esip->esi_hdb,
  2375. req->icu_channel_handle, &ptr);
  2376. if (ret != 0) {
  2377. error = SA_AIS_ERR_BAD_HANDLE;
  2378. goto unsubr_done;
  2379. }
  2380. eco = ptr;
  2381. eci = eco->eco_channel;
  2382. /*
  2383. * Make sure that the id exists.
  2384. */
  2385. ecs = find_subscr(eco, req->icu_sub_id);
  2386. if (!ecs) {
  2387. error = SA_AIS_ERR_NOT_EXIST;
  2388. goto unsubr_put;
  2389. }
  2390. list_del(&ecs->ecs_entry);
  2391. log_printf(LOG_LEVEL_DEBUG,
  2392. "unsubscribe from channel %s subscription ID 0x%x "
  2393. "with %d filters\n",
  2394. eci->esc_channel_name.value,
  2395. ecs->ecs_sub_id, (int)ecs->ecs_filters->filters_number);
  2396. free_filters(ecs->ecs_filters);
  2397. free(ecs);
  2398. unsubr_put:
  2399. hdb_handle_put(&esip->esi_hdb, req->icu_channel_handle);
  2400. unsubr_done:
  2401. res.icu_head.size = sizeof(res);
  2402. res.icu_head.id = MESSAGE_RES_EVT_UNSUBSCRIBE;
  2403. res.icu_head.error = error;
  2404. openais_conn_send_response(conn, &res, sizeof(res));
  2405. }
  2406. /*
  2407. * saEvtEventPublish Handler
  2408. */
  2409. static void lib_evt_event_publish(void *conn, void *message)
  2410. {
  2411. struct lib_event_data *req;
  2412. struct res_evt_event_publish res;
  2413. struct event_svr_channel_open *eco;
  2414. struct event_svr_channel_instance *eci;
  2415. mar_evteventid_t event_id = 0;
  2416. uint64_t msg_id = 0;
  2417. SaAisErrorT error = SA_AIS_OK;
  2418. struct iovec pub_iovec;
  2419. void *ptr;
  2420. int result;
  2421. unsigned int ret;
  2422. struct libevt_pd *esip;
  2423. esip = (struct libevt_pd *)openais_conn_private_data_get(conn);
  2424. req = message;
  2425. log_printf(LOG_LEVEL_DEBUG,
  2426. "saEvtEventPublish (Publish event request)\n");
  2427. /*
  2428. * look up and validate open channel info
  2429. */
  2430. ret = hdb_handle_get(&esip->esi_hdb,
  2431. req->led_svr_channel_handle, &ptr);
  2432. if (ret != 0) {
  2433. error = SA_AIS_ERR_BAD_HANDLE;
  2434. goto pub_done;
  2435. }
  2436. eco = ptr;
  2437. eci = eco->eco_channel;
  2438. /*
  2439. * modify the request structure for sending event data to subscribed
  2440. * processes.
  2441. */
  2442. get_event_id(&event_id, &msg_id);
  2443. req->led_head.id = SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_EVENTDATA);
  2444. req->led_chan_name = eci->esc_channel_name;
  2445. req->led_event_id = event_id;
  2446. req->led_msg_id = msg_id;
  2447. req->led_chan_unlink_id = eci->esc_unlink_id;
  2448. /*
  2449. * Distribute the event.
  2450. * The multicasted event will be picked up and delivered
  2451. * locally by the local network event receiver.
  2452. */
  2453. pub_iovec.iov_base = (char *)req;
  2454. pub_iovec.iov_len = req->led_head.size;
  2455. result = totempg_groups_mcast_joined(openais_group_handle, &pub_iovec, 1, TOTEMPG_AGREED);
  2456. if (result != 0) {
  2457. error = SA_AIS_ERR_LIBRARY;
  2458. }
  2459. hdb_handle_put(&esip->esi_hdb, req->led_svr_channel_handle);
  2460. pub_done:
  2461. res.iep_head.size = sizeof(res);
  2462. res.iep_head.id = MESSAGE_RES_EVT_PUBLISH;
  2463. res.iep_head.error = error;
  2464. res.iep_event_id = event_id;
  2465. openais_conn_send_response(conn, &res, sizeof(res));
  2466. }
  2467. /*
  2468. * saEvtEventRetentionTimeClear handler
  2469. */
  2470. static void lib_evt_event_clear_retentiontime(void *conn, void *message)
  2471. {
  2472. struct req_evt_event_clear_retentiontime *req;
  2473. struct res_evt_event_clear_retentiontime res;
  2474. struct req_evt_chan_command cpkt;
  2475. struct retention_time_clear_pending *rtc = 0;
  2476. struct iovec rtn_iovec;
  2477. SaAisErrorT error;
  2478. int ret;
  2479. req = message;
  2480. log_printf(RETENTION_TIME_DEBUG,
  2481. "saEvtEventRetentionTimeClear (Clear event retentiontime request)\n");
  2482. log_printf(RETENTION_TIME_DEBUG,
  2483. "event ID 0x%llx, chan handle 0x%x\n",
  2484. (unsigned long long)req->iec_event_id,
  2485. req->iec_channel_handle);
  2486. rtc = malloc(sizeof(*rtc));
  2487. if (!rtc) {
  2488. log_printf(LOG_LEVEL_ERROR,
  2489. "saEvtEventRetentionTimeClear: Memory allocation failure\n");
  2490. error = SA_AIS_ERR_TRY_AGAIN;
  2491. goto evt_ret_clr_err;
  2492. }
  2493. rtc->rtc_event_id = req->iec_event_id;
  2494. rtc->rtc_conn = conn;
  2495. list_init(&rtc->rtc_entry);
  2496. list_add_tail(&rtc->rtc_entry, &clear_pending);
  2497. /*
  2498. * Send the clear request
  2499. */
  2500. memset(&cpkt, 0, sizeof(cpkt));
  2501. cpkt.chc_head.id =
  2502. SERVICE_ID_MAKE(EVT_SERVICE, MESSAGE_REQ_EXEC_EVT_CHANCMD);
  2503. cpkt.chc_head.size = sizeof(cpkt);
  2504. cpkt.chc_op = EVT_CLEAR_RET_OP;
  2505. cpkt.u.chc_event_id = req->iec_event_id;
  2506. rtn_iovec.iov_base = (char *)&cpkt;
  2507. rtn_iovec.iov_len = cpkt.chc_head.size;
  2508. ret = totempg_groups_mcast_joined(openais_group_handle,
  2509. &rtn_iovec, 1, TOTEMPG_AGREED);
  2510. if (ret == 0) {
  2511. // TODO this should really assert
  2512. return;
  2513. }
  2514. error = SA_AIS_ERR_LIBRARY;
  2515. evt_ret_clr_err:
  2516. if (rtc) {
  2517. list_del(&rtc->rtc_entry);
  2518. free(rtc);
  2519. }
  2520. res.iec_head.size = sizeof(res);
  2521. res.iec_head.id = MESSAGE_RES_EVT_CLEAR_RETENTIONTIME;
  2522. res.iec_head.error = error;
  2523. openais_conn_send_response(conn, &res, sizeof(res));
  2524. }
  2525. /*
  2526. * Send requested event data to the application
  2527. */
  2528. static void lib_evt_event_data_get(void *conn, void *message)
  2529. {
  2530. struct lib_event_data res;
  2531. struct chan_event_list *cel;
  2532. struct event_data *edp;
  2533. int i;
  2534. struct libevt_pd *esip;
  2535. esip = (struct libevt_pd *)openais_conn_private_data_get(conn);
  2536. /*
  2537. * Deliver events in publish order within priority
  2538. */
  2539. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  2540. if (!list_empty(&esip->esi_events[i])) {
  2541. cel = list_entry(esip->esi_events[i].next, struct chan_event_list,
  2542. cel_entry);
  2543. list_del(&cel->cel_entry);
  2544. list_init(&cel->cel_entry);
  2545. esip->esi_nevents--;
  2546. if (esip->esi_queue_blocked &&
  2547. (esip->esi_nevents < evt_delivery_queue_resume)) {
  2548. esip->esi_queue_blocked = 0;
  2549. log_printf(LOG_LEVEL_DEBUG, "unblock\n");
  2550. }
  2551. edp = cel->cel_event;
  2552. edp->ed_event.led_lib_channel_handle = cel->cel_chan_handle;
  2553. edp->ed_event.led_sub_id = cel->cel_sub_id;
  2554. edp->ed_event.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2555. edp->ed_event.led_head.error = SA_AIS_OK;
  2556. free(cel);
  2557. openais_conn_send_response(conn, &edp->ed_event,
  2558. edp->ed_event.led_head.size);
  2559. free_event_data(edp);
  2560. goto data_get_done;
  2561. }
  2562. }
  2563. res.led_head.size = sizeof(res.led_head);
  2564. res.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2565. res.led_head.error = SA_AIS_ERR_NOT_EXIST;
  2566. openais_conn_send_response(conn, &res, res.led_head.size);
  2567. /*
  2568. * See if there are any events that the app doesn't know about
  2569. * because the notify pipe was full.
  2570. */
  2571. data_get_done:
  2572. if (esip->esi_nevents) {
  2573. __notify_event(conn);
  2574. }
  2575. }
  2576. /*
  2577. * Scan the list of channels and remove the specified node.
  2578. */
  2579. static void remove_chan_open_info(mar_uint32_t node_id)
  2580. {
  2581. struct list_head *l, *nxt;
  2582. struct event_svr_channel_instance *eci;
  2583. for (l = esc_head.next; l != &esc_head; l = nxt) {
  2584. nxt = l->next;
  2585. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  2586. remove_open_count(eci, node_id);
  2587. }
  2588. }
  2589. /*
  2590. * Called when there is a configuration change in the cluster.
  2591. * This function looks at any joiners and leavers and updates the evt
  2592. * node list. The node list is used to keep track of event IDs
  2593. * received for each node for the detection of duplicate events.
  2594. */
  2595. static void evt_conf_change(
  2596. enum totem_configuration_type configuration_type,
  2597. unsigned int *member_list, int member_list_entries,
  2598. unsigned int *left_list, int left_list_entries,
  2599. unsigned int *joined_list, int joined_list_entries,
  2600. struct memb_ring_id *ring_id)
  2601. {
  2602. log_printf(RECOVERY_DEBUG, "Evt conf change %d\n",
  2603. configuration_type);
  2604. log_printf(RECOVERY_DEBUG, "m %d, j %d, l %d\n",
  2605. member_list_entries,
  2606. joined_list_entries,
  2607. left_list_entries);
  2608. /*
  2609. * Save the various membership lists for later processing by
  2610. * the synchronization functions. The left list is only
  2611. * valid in the transitional configuration, the joined list is
  2612. * only valid in the regular configuration. Other than for the
  2613. * purposes of delivering retained events from merging partitions,
  2614. * we only care about the final membership from the regular
  2615. * configuration.
  2616. */
  2617. if (configuration_type == TOTEM_CONFIGURATION_TRANSITIONAL) {
  2618. left_member_count = left_list_entries;
  2619. trans_member_count = member_list_entries;
  2620. if (left_member_list) {
  2621. free(left_member_list);
  2622. left_member_list = 0;
  2623. }
  2624. if (left_list_entries) {
  2625. left_member_list =
  2626. malloc(sizeof(unsigned int) * left_list_entries);
  2627. if (!left_member_list) {
  2628. /*
  2629. * ERROR: No recovery.
  2630. */
  2631. log_printf(LOG_LEVEL_ERROR,
  2632. "Config change left list allocation error\n");
  2633. assert(0);
  2634. }
  2635. memcpy(left_member_list, left_list,
  2636. sizeof(unsigned int) * left_list_entries);
  2637. }
  2638. if (trans_member_list) {
  2639. free(trans_member_list);
  2640. trans_member_list = 0;
  2641. }
  2642. if (member_list_entries) {
  2643. trans_member_list =
  2644. malloc(sizeof(unsigned int) * member_list_entries);
  2645. if (!trans_member_list) {
  2646. /*
  2647. * ERROR: No recovery.
  2648. */
  2649. log_printf(LOG_LEVEL_ERROR,
  2650. "Config change transitional member list allocation error\n");
  2651. assert(0);
  2652. }
  2653. memcpy(trans_member_list, member_list,
  2654. sizeof(unsigned int) * member_list_entries);
  2655. }
  2656. }
  2657. if (configuration_type == TOTEM_CONFIGURATION_REGULAR) {
  2658. joined_member_count = joined_list_entries;
  2659. total_member_count = member_list_entries;
  2660. if (joined_member_list) {
  2661. free(joined_member_list);
  2662. joined_member_list = 0;
  2663. }
  2664. if (joined_list_entries) {
  2665. joined_member_list =
  2666. malloc(sizeof(unsigned int) * joined_list_entries);
  2667. if (!joined_member_list) {
  2668. /*
  2669. * ERROR: No recovery.
  2670. */
  2671. log_printf(LOG_LEVEL_ERROR,
  2672. "Config change joined list allocation error\n");
  2673. assert(0);
  2674. }
  2675. memcpy(joined_member_list, joined_list,
  2676. sizeof(unsigned int) * joined_list_entries);
  2677. }
  2678. if (current_member_list) {
  2679. free(current_member_list);
  2680. current_member_list = 0;
  2681. }
  2682. if (member_list_entries) {
  2683. current_member_list =
  2684. malloc(sizeof(unsigned int) * member_list_entries);
  2685. if (!current_member_list) {
  2686. /*
  2687. * ERROR: No recovery.
  2688. */
  2689. log_printf(LOG_LEVEL_ERROR,
  2690. "Config change member list allocation error\n");
  2691. assert(0);
  2692. }
  2693. memcpy(current_member_list, member_list,
  2694. sizeof(unsigned int) * member_list_entries);
  2695. }
  2696. }
  2697. }
  2698. /*
  2699. * saEvtFinalize Handler
  2700. */
  2701. static int evt_lib_exit(void *conn)
  2702. {
  2703. struct event_svr_channel_open *eco;
  2704. struct list_head *l, *nxt;
  2705. struct open_chan_pending *ocp;
  2706. struct unlink_chan_pending *ucp;
  2707. struct retention_time_clear_pending *rtc;
  2708. struct libevt_pd *esip =
  2709. openais_conn_private_data_get(openais_conn_partner_get(conn));
  2710. log_printf(LOG_LEVEL_DEBUG, "saEvtFinalize (Event exit request)\n");
  2711. log_printf(LOG_LEVEL_DEBUG, "saEvtFinalize %d evts on list\n",
  2712. esip->esi_nevents);
  2713. /*
  2714. * Clean up any open channels and associated subscriptions.
  2715. */
  2716. for (l = esip->esi_open_chans.next; l != &esip->esi_open_chans; l = nxt) {
  2717. nxt = l->next;
  2718. eco = list_entry(l, struct event_svr_channel_open, eco_instance_entry);
  2719. common_chan_close(eco, esip);
  2720. hdb_handle_destroy(&esip->esi_hdb, eco->eco_my_handle);
  2721. }
  2722. /*
  2723. * Clean up any pending async operations
  2724. */
  2725. for (l = open_pending.next; l != &open_pending; l = nxt) {
  2726. nxt = l->next;
  2727. ocp = list_entry(l, struct open_chan_pending, ocp_entry);
  2728. if (esip == openais_conn_private_data_get(ocp->ocp_conn)) {
  2729. list_del(&ocp->ocp_entry);
  2730. free(ocp);
  2731. }
  2732. }
  2733. for (l = unlink_pending.next; l != &unlink_pending; l = nxt) {
  2734. nxt = l->next;
  2735. ucp = list_entry(l, struct unlink_chan_pending, ucp_entry);
  2736. if (esip == openais_conn_private_data_get(ucp->ucp_conn)) {
  2737. list_del(&ucp->ucp_entry);
  2738. free(ucp);
  2739. }
  2740. }
  2741. for (l = clear_pending.next;
  2742. l != &clear_pending; l = nxt) {
  2743. nxt = l->next;
  2744. rtc = list_entry(l, struct retention_time_clear_pending, rtc_entry);
  2745. if (esip == openais_conn_private_data_get(rtc->rtc_conn)) {
  2746. list_del(&rtc->rtc_entry);
  2747. free(rtc);
  2748. }
  2749. }
  2750. /*
  2751. * Destroy the open channel handle database
  2752. */
  2753. hdb_destroy(&esip->esi_hdb);
  2754. return 0;
  2755. }
  2756. /*
  2757. * Called at service start time.
  2758. */
  2759. static int evt_exec_init(struct objdb_iface_ver0 *objdb)
  2760. {
  2761. unsigned int object_service_handle;
  2762. char *value;
  2763. log_init ("EVT");
  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. */