evt.c 107 KB

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