evt.c 117 KB

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