evt.c 109 KB

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