evt.c 109 KB

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