evt.c 105 KB

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