evt.c 114 KB

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