evt.c 116 KB

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