evt.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216
  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_DEBUG LOG_LEVEL_DEBUG
  35. #define CHAN_DEL_DEBUG LOG_LEVEL_DEBUG
  36. #include <sys/types.h>
  37. #include <malloc.h>
  38. #include <errno.h>
  39. #include <sys/time.h>
  40. #include <sys/uio.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include "../include/ais_types.h"
  45. #include "../include/ais_msg.h"
  46. #include "../include/list.h"
  47. #include "../include/queue.h"
  48. #include "util.h"
  49. #include "aispoll.h"
  50. #include "mempool.h"
  51. #include "parse.h"
  52. #include "main.h"
  53. #include "gmi.h"
  54. #include "hdb.h"
  55. #include "clm.h"
  56. #include "evt.h"
  57. #define LOG_SERVICE LOG_SERVICE_EVT
  58. #include "print.h"
  59. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info,
  60. void *message);
  61. static int lib_evt_open_channel(struct conn_info *conn_info, void *message);
  62. static int lib_evt_close_channel(struct conn_info *conn_info, void *message);
  63. static int lib_evt_event_subscribe(struct conn_info *conn_info,
  64. void *message);
  65. static int lib_evt_event_unsubscribe(struct conn_info *conn_info,
  66. void *message);
  67. static int lib_evt_event_publish(struct conn_info *conn_info, void *message);
  68. static int lib_evt_event_clear_retentiontime(struct conn_info *conn_info,
  69. void *message);
  70. static int lib_evt_event_data_get(struct conn_info *conn_info,
  71. void *message);
  72. static int evt_conf_change(
  73. enum gmi_configuration_type configuration_type,
  74. struct sockaddr_in *member_list, int member_list_entries,
  75. struct sockaddr_in *left_list, int left_list_entries,
  76. struct sockaddr_in *joined_list, int joined_list_entries);
  77. static int evt_initialize(struct conn_info *conn_info, void *msg);
  78. static int evt_finalize(struct conn_info *conn_info);
  79. static int evt_exec_init(void);
  80. static struct libais_handler evt_libais_handlers[] = {
  81. {
  82. .libais_handler_fn = message_handler_req_lib_activatepoll,
  83. .response_size = sizeof(struct res_lib_activatepoll),
  84. .response_id = MESSAGE_RES_LIB_ACTIVATEPOLL,
  85. .gmi_prio = GMI_PRIO_RECOVERY
  86. },
  87. {
  88. .libais_handler_fn = lib_evt_open_channel,
  89. .response_size = sizeof(struct res_evt_channel_open),
  90. .response_id = MESSAGE_RES_EVT_OPEN_CHANNEL,
  91. .gmi_prio = GMI_PRIO_MED
  92. },
  93. {
  94. .libais_handler_fn = lib_evt_close_channel,
  95. .response_size = sizeof(struct res_evt_channel_close),
  96. .response_id = MESSAGE_RES_EVT_CLOSE_CHANNEL,
  97. .gmi_prio = GMI_PRIO_RECOVERY
  98. },
  99. {
  100. .libais_handler_fn = lib_evt_event_subscribe,
  101. .response_size = sizeof(struct res_evt_event_subscribe),
  102. .response_id = MESSAGE_RES_EVT_SUBSCRIBE,
  103. .gmi_prio = GMI_PRIO_RECOVERY
  104. },
  105. {
  106. .libais_handler_fn = lib_evt_event_unsubscribe,
  107. .response_size = sizeof(struct res_evt_event_unsubscribe),
  108. .response_id = MESSAGE_RES_EVT_UNSUBSCRIBE,
  109. .gmi_prio = GMI_PRIO_RECOVERY
  110. },
  111. {
  112. .libais_handler_fn = lib_evt_event_publish,
  113. .response_size = sizeof(struct res_evt_event_publish),
  114. .response_id = MESSAGE_RES_EVT_PUBLISH,
  115. .gmi_prio = GMI_PRIO_LOW
  116. },
  117. {
  118. .libais_handler_fn = lib_evt_event_clear_retentiontime,
  119. .response_size = sizeof(struct res_evt_event_clear_retentiontime),
  120. .response_id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME,
  121. .gmi_prio = GMI_PRIO_MED
  122. },
  123. {
  124. .libais_handler_fn = lib_evt_event_data_get,
  125. .response_size = sizeof(struct lib_event_data),
  126. .response_id = MESSAGE_RES_EVT_EVENT_DATA,
  127. .gmi_prio = GMI_PRIO_RECOVERY
  128. },
  129. };
  130. static int evt_remote_evt(void *msg, struct in_addr source_addr);
  131. static int evt_remote_recovery_evt(void *msg, struct in_addr source_addr);
  132. static int evt_remote_chan_op(void *msg, struct in_addr source_addr);
  133. static int (*evt_exec_handler_fns[]) (void *m, struct in_addr s) = {
  134. evt_remote_evt,
  135. evt_remote_chan_op,
  136. evt_remote_recovery_evt
  137. };
  138. struct service_handler evt_service_handler = {
  139. .libais_handlers = evt_libais_handlers,
  140. .libais_handlers_count = sizeof(evt_libais_handlers) /
  141. sizeof(struct libais_handler),
  142. .aisexec_handler_fns = evt_exec_handler_fns,
  143. .aisexec_handler_fns_count = sizeof(evt_exec_handler_fns) /
  144. sizeof(int (*)),
  145. .confchg_fn = evt_conf_change,
  146. .libais_init_fn = evt_initialize,
  147. .libais_exit_fn = evt_finalize,
  148. .exec_init_fn = evt_exec_init,
  149. .exec_dump_fn = 0
  150. };
  151. static gmi_recovery_plug_handle evt_recovery_plug_handle;
  152. /*
  153. * list of all retained events
  154. * struct event_data
  155. */
  156. static DECLARE_LIST_INIT(retained_list);
  157. /*
  158. * list of all event channel information
  159. * struct event_svr_channel_instance
  160. */
  161. static DECLARE_LIST_INIT(esc_head);
  162. /*
  163. * list of all active event conn_info structs.
  164. * struct conn_info
  165. */
  166. static DECLARE_LIST_INIT(ci_head);
  167. /*
  168. * Structure to track pending channel open requests.
  169. * ocp_async: 1 for async open
  170. * ocp_invocation: invocation for async open
  171. * ocp_chan_name: requested channel
  172. * ocp_conn_info: conn_info for returning to the library.
  173. * ocp_open_flags: channel open flags
  174. * ocp_timer_handle: timer handle for sync open
  175. * ocp_entry: list entry for pending open list.
  176. */
  177. struct open_chan_pending {
  178. int ocp_async;
  179. SaInvocationT ocp_invocation;
  180. SaNameT ocp_chan_name;
  181. struct conn_info *ocp_conn_info;
  182. SaEvtChannelOpenFlagsT ocp_open_flag;
  183. poll_timer_handle ocp_timer_handle;
  184. uint32_t ocp_c_handle;
  185. struct list_head ocp_entry;
  186. };
  187. /*
  188. * list of pending channel opens
  189. */
  190. static DECLARE_LIST_INIT(open_pending);
  191. static void chan_open_timeout(void *data);
  192. #define min(a,b) ((a) < (b) ? (a) : (b))
  193. /*
  194. * Throttle event delivery to applications to keep
  195. * the exec from using too much memory if the app is
  196. * slow to process its events.
  197. */
  198. #define MAX_EVT_DELIVERY_QUEUE 1000
  199. #define MIN_EVT_QUEUE_RESUME (MAX_EVT_DELIVERY_QUEUE / 2)
  200. #define LOST_PUB "EVENT_SERIVCE"
  201. #define LOST_CHAN "LOST EVENT"
  202. /*
  203. * Event to send when the delivery queue gets too full
  204. */
  205. char lost_evt[] = SA_EVT_LOST_EVENT;
  206. static int dropped_event_size;
  207. static struct event_data *dropped_event;
  208. struct evt_pattern {
  209. SaEvtEventPatternT pat;
  210. char str[sizeof(lost_evt)];
  211. };
  212. static struct evt_pattern dropped_pattern = {
  213. .pat = {&dropped_pattern.str[0],
  214. sizeof(lost_evt)},
  215. .str = {SA_EVT_LOST_EVENT}
  216. };
  217. SaNameT lost_chan = {
  218. .value = LOST_CHAN,
  219. .length = sizeof(LOST_CHAN)
  220. };
  221. SaNameT dropped_publisher = {
  222. .value = LOST_PUB,
  223. .length = sizeof(LOST_PUB)
  224. };
  225. struct event_svr_channel_open;
  226. struct event_svr_channel_subscr;
  227. struct open_count {
  228. SaClmNodeIdT oc_node_id;
  229. int32_t oc_open_count;
  230. };
  231. /*
  232. * Structure to contain global channel releated information
  233. *
  234. * esc_channel_name: The name of this channel.
  235. * esc_total_opens: The total number of opens on this channel including
  236. * other nodes.
  237. * esc_local_opens: The number of opens on this channel for this node.
  238. * esc_oc_size: The total number of entries in esc_node_opens;
  239. * esc_node_opens: list of node IDs and how many opens are associated.
  240. * esc_retained_count: How many retained events for this channel
  241. * esc_open_chans: list of opens of this channel.
  242. * (event_svr_channel_open.eco_entry)
  243. * esc_entry: links to other channels. (used by esc_head)
  244. */
  245. struct event_svr_channel_instance {
  246. SaNameT esc_channel_name;
  247. int32_t esc_total_opens;
  248. int32_t esc_local_opens;
  249. uint32_t esc_oc_size;
  250. struct open_count *esc_node_opens;
  251. uint32_t esc_retained_count;
  252. struct list_head esc_open_chans;
  253. struct list_head esc_entry;
  254. };
  255. /*
  256. * Has the event data in the correct format to send to the library API
  257. * with aditional field for accounting.
  258. *
  259. * ed_ref_count: how many other strutures are referencing.
  260. * ed_retained: retained event list.
  261. * ed_timer_handle: Timer handle for retained event expiration.
  262. * ed_delivered: arrays of open channel pointers that this event
  263. * has been delivered to. (only used for events
  264. * with a retention time).
  265. * ed_delivered_count: Number of entries available in ed_delivered.
  266. * ed_delivered_next: Next free spot in ed_delivered
  267. * ed_my_chan: pointer to the global channel instance associated
  268. * with this event.
  269. * ed_event: The event data formatted to be ready to send.
  270. */
  271. struct event_data {
  272. uint32_t ed_ref_count;
  273. struct list_head ed_retained;
  274. poll_timer_handle ed_timer_handle;
  275. struct event_svr_channel_open **ed_delivered;
  276. uint32_t ed_delivered_count;
  277. uint32_t ed_delivered_next;
  278. struct event_svr_channel_instance *ed_my_chan;
  279. struct lib_event_data ed_event;
  280. };
  281. /*
  282. * Contains a list of pending events to be delivered to a subscribed
  283. * application.
  284. *
  285. * cel_chan_handle: associated library channel handle
  286. * cel_sub_id: associated library subscription ID
  287. * cel_event: event structure to deliver.
  288. * cel_entry: list of pending events
  289. * (struct event_server_instance.esi_events)
  290. */
  291. struct chan_event_list {
  292. uint32_t cel_chan_handle;
  293. uint32_t cel_sub_id;
  294. struct event_data* cel_event;
  295. struct list_head cel_entry;
  296. };
  297. /*
  298. * Contains information about each open for a given channel
  299. *
  300. * eco_flags: How the channel was opened.
  301. * eco_lib_handle: channel handle in the app. Used for event delivery.
  302. * eco_my_handle: the handle used to access this data structure.
  303. * eco_channel: Pointer to global channel info.
  304. * eco_entry: links to other opeinings of this channel.
  305. * eco_instance_entry: links to other channel opeinings for the
  306. * associated server instance.
  307. * eco_subscr: head of list of sbuscriptions for this channel open.
  308. * (event_svr_channel_subscr.ecs_entry)
  309. * eco_conn_info: refrence to EvtInitialize who owns this open.
  310. */
  311. struct event_svr_channel_open {
  312. uint8_t eco_flags;
  313. uint32_t eco_lib_handle;
  314. uint32_t eco_my_handle;
  315. struct event_svr_channel_instance *eco_channel;
  316. struct list_head eco_entry;
  317. struct list_head eco_instance_entry;
  318. struct list_head eco_subscr;
  319. struct conn_info *eco_conn_info;
  320. };
  321. /*
  322. * Contains information about each channel subscription
  323. *
  324. * ecs_open_chan: Link to our open channel.
  325. * ecs_sub_id: Subscription ID.
  326. * ecs_filter_count: number of filters in ecs_filters
  327. * ecs_filters: filters for determining event delivery.
  328. * ecs_entry: Links to other subscriptions to this channel opening.
  329. */
  330. struct event_svr_channel_subscr {
  331. struct event_svr_channel_open *ecs_open_chan;
  332. uint32_t ecs_sub_id;
  333. SaEvtEventFilterArrayT *ecs_filters;
  334. struct list_head ecs_entry;
  335. };
  336. /*
  337. * Member node data
  338. * mn_node_info: cluster node info from membership
  339. * mn_last_evt_id: last seen event ID for this node
  340. * mn_started: Indicates that event service has started
  341. * on this node.
  342. * mn_next: pointer to the next node in the hash chain.
  343. * mn_entry: List of all nodes.
  344. */
  345. struct member_node_data {
  346. struct in_addr mn_node_addr;
  347. SaClmClusterNodeT mn_node_info;
  348. SaEvtEventIdT mn_last_evt_id;
  349. SaClmNodeIdT mn_started;
  350. struct member_node_data *mn_next;
  351. struct list_head mn_entry;
  352. };
  353. DECLARE_LIST_INIT(mnd);
  354. /*
  355. * Global varaibles used by the event service
  356. *
  357. * base_id_top: upper bits of next event ID to assign
  358. * base_id: Lower bits of Next event ID to assign
  359. * my_node_id: My cluster node id
  360. * in_cfg_change: Config change occurred. Figure out who sends retained evts.
  361. * cleared when retained events have been delivered.
  362. * total_members: how many members in this cluster
  363. * checked_in: keep track during config change.
  364. * any_joined: did any nodes join on this change?
  365. * recovery_node: True if we're the recovery node.
  366. * tok_call_handle: gmi token callback handle for recovery.
  367. * next_retained: pointer to next retained message to send during recovery.
  368. * next_chan: pointer to next channel to send during recovery.
  369. *
  370. */
  371. #define BASE_ID_MASK 0xffffffffLL
  372. static SaEvtEventIdT base_id = 0;
  373. static SaEvtEventIdT base_id_top = 0;
  374. static SaClmNodeIdT my_node_id = 0;
  375. static int in_cfg_change = 0;
  376. static int total_members = 0;
  377. static int checked_in = 0;
  378. static int any_joined = 0;
  379. static int recovery_node = 0;
  380. static void *tok_call_handle = 0;
  381. static struct list_head *next_retained = 0;
  382. static struct list_head *next_chan = 0;
  383. /*
  384. * Take the filters we received from the application via the library and
  385. * make them into a real SaEvtEventFilterArrayT
  386. */
  387. static SaErrorT evtfilt_to_aisfilt(struct req_evt_event_subscribe *req,
  388. SaEvtEventFilterArrayT **evtfilters)
  389. {
  390. SaEvtEventFilterArrayT *filta =
  391. (SaEvtEventFilterArrayT *)req->ics_filter_data;
  392. SaEvtEventFilterArrayT *filters;
  393. SaEvtEventFilterT *filt = (void *)filta + sizeof(SaEvtEventFilterArrayT);
  394. SaUint8T *str = (void *)filta + sizeof(SaEvtEventFilterArrayT) +
  395. (sizeof(SaEvtEventFilterT) * filta->filtersNumber);
  396. int i;
  397. int j;
  398. filters = malloc(sizeof(SaEvtEventFilterArrayT));
  399. if (!filters) {
  400. return SA_ERR_NO_MEMORY;
  401. }
  402. filters->filtersNumber = filta->filtersNumber;
  403. filters->filters = malloc(sizeof(SaEvtEventFilterT) *
  404. filta->filtersNumber);
  405. if (!filters->filters) {
  406. free(filters);
  407. return SA_ERR_NO_MEMORY;
  408. }
  409. for (i = 0; i < filters->filtersNumber; i++) {
  410. filters->filters[i].filter.pattern =
  411. malloc(filt[i].filter.patternSize);
  412. if (!filters->filters[i].filter.pattern) {
  413. for (j = 0; j < i; j++) {
  414. free(filters->filters[j].filter.pattern);
  415. }
  416. free(filters->filters);
  417. free(filters);
  418. return SA_ERR_NO_MEMORY;
  419. }
  420. filters->filters[i].filter.patternSize =
  421. filt[i].filter.patternSize;
  422. memcpy(filters->filters[i].filter.pattern,
  423. str, filters->filters[i].filter.patternSize);
  424. filters->filters[i].filterType = filt[i].filterType;
  425. str += filters->filters[i].filter.patternSize;
  426. }
  427. *evtfilters = filters;
  428. return SA_OK;
  429. }
  430. /*
  431. * Free up filter data
  432. */
  433. static void free_filters(SaEvtEventFilterArrayT *fp)
  434. {
  435. int i;
  436. for (i = 0; i < fp->filtersNumber; i++) {
  437. free(fp->filters[i].filter.pattern);
  438. }
  439. free(fp->filters);
  440. free(fp);
  441. }
  442. /*
  443. * Look up a channel in the global channel list
  444. */
  445. static struct event_svr_channel_instance *
  446. find_channel(SaNameT *chan_name)
  447. {
  448. struct list_head *l;
  449. struct event_svr_channel_instance *eci;
  450. for (l = esc_head.next; l != &esc_head; l = l->next) {
  451. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  452. if (!name_match(chan_name, &eci->esc_channel_name)) {
  453. continue;
  454. }
  455. return eci;
  456. }
  457. return 0;
  458. }
  459. /*
  460. * Create and initialize a channel instance structure
  461. */
  462. static struct event_svr_channel_instance *create_channel(SaNameT *cn)
  463. {
  464. struct event_svr_channel_instance *eci;
  465. eci = (struct event_svr_channel_instance *) malloc(sizeof(*eci));
  466. if (!eci) {
  467. return (eci);
  468. }
  469. memset(eci, 0, sizeof(*eci));
  470. list_init(&eci->esc_entry);
  471. list_init(&eci->esc_open_chans);
  472. eci->esc_oc_size = total_members;
  473. eci->esc_node_opens = malloc(sizeof(struct open_count) * total_members);
  474. if (!eci->esc_node_opens) {
  475. free(eci);
  476. return 0;
  477. }
  478. memset(eci->esc_node_opens, 0, sizeof(struct open_count) * total_members);
  479. eci->esc_channel_name = *cn;
  480. eci->esc_channel_name.value[eci->esc_channel_name.length] = '\0';
  481. list_add(&eci->esc_entry, &esc_head);
  482. return eci;
  483. }
  484. /*
  485. * Make sure that the list of nodes is large enough to hold the whole
  486. * membership
  487. */
  488. static int check_open_size(struct event_svr_channel_instance *eci)
  489. {
  490. if (total_members > eci->esc_oc_size) {
  491. eci->esc_node_opens = realloc(eci->esc_node_opens,
  492. sizeof(struct open_count) * total_members);
  493. if (!eci->esc_node_opens) {
  494. log_printf(LOG_LEVEL_WARNING,
  495. "Memory error realloc of node list\n");
  496. return -1;
  497. }
  498. memset(&eci->esc_node_opens[eci->esc_oc_size], 0,
  499. sizeof(struct open_count) * (total_members - eci->esc_oc_size));
  500. eci->esc_oc_size = total_members;
  501. }
  502. return 0;
  503. }
  504. /*
  505. * Find the specified node ID in the node list of the channel.
  506. * If it's not in the list, add it.
  507. */
  508. static struct open_count* find_open_count(
  509. struct event_svr_channel_instance *eci,
  510. SaClmNodeIdT node_id)
  511. {
  512. int i;
  513. for (i = 0; i < eci->esc_oc_size; i++) {
  514. if (eci->esc_node_opens[i].oc_node_id == 0) {
  515. eci->esc_node_opens[i].oc_node_id = node_id;
  516. eci->esc_node_opens[i].oc_open_count = 0;
  517. }
  518. if (eci->esc_node_opens[i].oc_node_id == node_id) {
  519. return &eci->esc_node_opens[i];
  520. }
  521. }
  522. log_printf(LOG_LEVEL_DEBUG,
  523. "find_open_count: node id 0x%x not found\n",
  524. node_id);
  525. return 0;
  526. }
  527. static void dump_chan_opens(struct event_svr_channel_instance *eci)
  528. {
  529. int i;
  530. log_printf(LOG_LEVEL_NOTICE,
  531. "Channel %s, total %d, local %d\n",
  532. eci->esc_channel_name.value,
  533. eci->esc_total_opens,
  534. eci->esc_local_opens);
  535. for (i = 0; i < eci->esc_oc_size; i++) {
  536. if (eci->esc_node_opens[i].oc_node_id == 0) {
  537. break;
  538. }
  539. log_printf(LOG_LEVEL_NOTICE, "Node 0x%x, count %d\n",
  540. eci->esc_node_opens[i].oc_node_id,
  541. eci->esc_node_opens[i].oc_open_count);
  542. }
  543. }
  544. #ifdef DUMP_CHAN_INFO
  545. /*
  546. * Scan the list of channels and dump the open count info.
  547. */
  548. static void dump_all_chans()
  549. {
  550. struct list_head *l;
  551. struct event_svr_channel_instance *eci;
  552. for (l = esc_head.next; l != &esc_head; l = l->next) {
  553. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  554. dump_chan_opens(eci);
  555. }
  556. }
  557. #endif
  558. /*
  559. * Replace the current open count for a node with the specified value.
  560. */
  561. static int set_open_count(struct event_svr_channel_instance *eci,
  562. SaClmNodeIdT node_id, uint32_t open_count)
  563. {
  564. struct open_count *oc;
  565. int i;
  566. if ((i = check_open_size(eci)) != 0) {
  567. return i;
  568. }
  569. oc = find_open_count(eci, node_id);
  570. if (oc) {
  571. if (oc->oc_open_count) {
  572. /*
  573. * If the open count wasn't zero, then we already
  574. * knew about this node. It should never be different than
  575. * what we already had for an open count.
  576. */
  577. if (oc->oc_open_count != open_count) {
  578. log_printf(LOG_LEVEL_ERROR,
  579. "Channel open count error\n");
  580. dump_chan_opens(eci);
  581. }
  582. return 0;
  583. }
  584. log_printf(LOG_LEVEL_DEBUG,
  585. "Set count: Chan %s for node 0x%x, was %d, now %d\n",
  586. eci->esc_channel_name.value,
  587. node_id, eci->esc_node_opens[i].oc_open_count, open_count);
  588. eci->esc_total_opens += open_count;
  589. oc->oc_open_count = open_count;
  590. return 0;
  591. }
  592. return -1;
  593. }
  594. /*
  595. * Increment the open count for the specified node.
  596. */
  597. static int inc_open_count(struct event_svr_channel_instance *eci,
  598. SaClmNodeIdT node_id)
  599. {
  600. struct open_count *oc;
  601. int i;
  602. if ((i = check_open_size(eci)) != 0) {
  603. return i;
  604. }
  605. if (node_id == my_node_id) {
  606. eci->esc_local_opens++;
  607. }
  608. oc = find_open_count(eci, node_id);
  609. if (oc) {
  610. eci->esc_total_opens++;
  611. oc->oc_open_count++;
  612. return 0;
  613. }
  614. return -1;
  615. }
  616. /*
  617. * Decrement the open count for the specified node in the
  618. * specified channel.
  619. */
  620. static int dec_open_count(struct event_svr_channel_instance *eci,
  621. SaClmNodeIdT node_id)
  622. {
  623. struct open_count *oc;
  624. int i;
  625. if ((i = check_open_size(eci)) != 0) {
  626. return i;
  627. }
  628. if (node_id == my_node_id) {
  629. eci->esc_local_opens--;
  630. }
  631. oc = find_open_count(eci, node_id);
  632. if (oc) {
  633. eci->esc_total_opens--;
  634. oc->oc_open_count--;
  635. if ((eci->esc_total_opens < 0) || (oc->oc_open_count < 0)) {
  636. log_printf(LOG_LEVEL_ERROR, "Channel open decrement error\n");
  637. dump_chan_opens(eci);
  638. }
  639. return 0;
  640. }
  641. return -1;
  642. }
  643. /*
  644. * Remove a channel and free its memory if it's not in use anymore.
  645. */
  646. static void delete_channel(struct event_svr_channel_instance *eci)
  647. {
  648. log_printf(CHAN_DEL_DEBUG,
  649. "Called Delete channel %s t %d, l %d, r %d\n",
  650. eci->esc_channel_name.value,
  651. eci->esc_total_opens, eci->esc_local_opens,
  652. eci->esc_retained_count);
  653. /*
  654. * If no one has the channel open anywhere and there are no unexpired
  655. * retained events for this channel, then it is OK to delete the
  656. * data structure.
  657. */
  658. if ((eci->esc_retained_count == 0) && (eci->esc_total_opens == 0)) {
  659. log_printf(CHAN_DEL_DEBUG, "Delete channel %s\n",
  660. eci->esc_channel_name.value);
  661. if (!list_empty(&eci->esc_open_chans)) {
  662. log_printf(LOG_LEVEL_NOTICE,
  663. "Last channel close request for %s (still open)\n",
  664. eci->esc_channel_name.value);
  665. dump_chan_opens(eci);
  666. return;
  667. }
  668. /*
  669. * adjust if we're sending open counts on a config change.
  670. */
  671. if (in_cfg_change && (&eci->esc_entry == next_chan)) {
  672. next_chan = eci->esc_entry.next;
  673. }
  674. list_del(&eci->esc_entry);
  675. if (eci->esc_node_opens) {
  676. free(eci->esc_node_opens);
  677. }
  678. free(eci);
  679. }
  680. }
  681. /*
  682. * Remove the specified node from the node list in this channel.
  683. */
  684. static int remove_open_count(
  685. struct event_svr_channel_instance *eci,
  686. SaClmNodeIdT node_id)
  687. {
  688. int i;
  689. int j;
  690. /*
  691. * Find the node, remove it and re-pack the array.
  692. */
  693. for (i = 0; i < eci->esc_oc_size; i++) {
  694. if (eci->esc_node_opens[i].oc_node_id == 0) {
  695. break;
  696. }
  697. log_printf(RECOVERY_DEBUG, "roc: %x/%x, t %d, oc %d\n",
  698. node_id, eci->esc_node_opens[i].oc_node_id,
  699. eci->esc_total_opens, eci->esc_node_opens[i].oc_open_count);
  700. if (eci->esc_node_opens[i].oc_node_id == node_id) {
  701. eci->esc_total_opens -= eci->esc_node_opens[i].oc_open_count;
  702. for (j = i+1; j < eci->esc_oc_size; j++, i++) {
  703. eci->esc_node_opens[i].oc_node_id =
  704. eci->esc_node_opens[j].oc_node_id;
  705. eci->esc_node_opens[i].oc_open_count =
  706. eci->esc_node_opens[j].oc_open_count;
  707. }
  708. eci->esc_node_opens[eci->esc_oc_size-1].oc_node_id = 0;
  709. eci->esc_node_opens[eci->esc_oc_size-1].oc_open_count = 0;
  710. /*
  711. * Remove the channel if it's not being used anymore
  712. */
  713. delete_channel(eci);
  714. return 0;
  715. }
  716. }
  717. return -1;
  718. }
  719. /*
  720. * Send a request to open a channel to the rest of the cluster.
  721. */
  722. static SaErrorT evt_open_channel(SaNameT *cn, SaUint8T flgs)
  723. {
  724. struct req_evt_chan_command cpkt;
  725. struct event_svr_channel_instance *eci;
  726. struct iovec chn_iovec;
  727. int res;
  728. SaErrorT ret;
  729. ret = SA_OK;
  730. eci = find_channel(cn);
  731. /*
  732. * If the create flag set, and it doesn't exist, we can make the channel.
  733. * Otherwise, it's an error since we're notified of channels being created
  734. * and opened.
  735. */
  736. if (!eci && !(flgs & SA_EVT_CHANNEL_CREATE)) {
  737. ret = SA_ERR_NOT_EXIST;
  738. goto chan_open_end;
  739. }
  740. /*
  741. * create the channel packet to send. Tell the the cluster
  742. * to create the channel.
  743. */
  744. memset(&cpkt, 0, sizeof(cpkt));
  745. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  746. cpkt.chc_head.size = sizeof(cpkt);
  747. cpkt.chc_op = EVT_OPEN_CHAN_OP;
  748. cpkt.u.chc_chan = *cn;
  749. chn_iovec.iov_base = &cpkt;
  750. chn_iovec.iov_len = cpkt.chc_head.size;
  751. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1, GMI_PRIO_MED);
  752. if (res != 0) {
  753. ret = SA_ERR_SYSTEM;
  754. }
  755. chan_open_end:
  756. return ret;
  757. }
  758. /*
  759. * Send a request to close a channel with the rest of the cluster.
  760. */
  761. static SaErrorT evt_close_channel(SaNameT *cn)
  762. {
  763. struct req_evt_chan_command cpkt;
  764. struct iovec chn_iovec;
  765. int res;
  766. SaErrorT ret;
  767. ret = SA_OK;
  768. /*
  769. * create the channel packet to send. Tell the the cluster
  770. * to close the channel.
  771. */
  772. memset(&cpkt, 0, sizeof(cpkt));
  773. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  774. cpkt.chc_head.size = sizeof(cpkt);
  775. cpkt.chc_op = EVT_CLOSE_CHAN_OP;
  776. cpkt.u.chc_chan = *cn;
  777. chn_iovec.iov_base = &cpkt;
  778. chn_iovec.iov_len = cpkt.chc_head.size;
  779. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1, GMI_PRIO_MED);
  780. if (res != 0) {
  781. ret = SA_ERR_SYSTEM;
  782. }
  783. return ret;
  784. }
  785. /*
  786. * Node data access functions. Used to keep track of event IDs
  787. * delivery of messages.
  788. *
  789. * add_node: Add a new member node to our list.
  790. * remove_node: Remove a node that left membership.
  791. * find_node: Given the node ID return a pointer to node information.
  792. *
  793. */
  794. #define NODE_HASH_SIZE 256
  795. static struct member_node_data *nl[NODE_HASH_SIZE] = {0};
  796. inline int
  797. hash_sock_addr(struct in_addr addr)
  798. {
  799. return addr.s_addr & (NODE_HASH_SIZE - 1);
  800. }
  801. static struct member_node_data **lookup_node(struct in_addr addr)
  802. {
  803. int index = hash_sock_addr(addr);
  804. struct member_node_data **nlp;
  805. nlp = &nl[index];
  806. for (nlp = &nl[index]; *nlp; nlp = &((*nlp)->mn_next)) {
  807. if ((*nlp)->mn_node_addr.s_addr == addr.s_addr) {
  808. break;
  809. }
  810. }
  811. return nlp;
  812. }
  813. static struct member_node_data *
  814. evt_find_node(struct in_addr addr)
  815. {
  816. struct member_node_data **nlp;
  817. nlp = lookup_node(addr);
  818. if (!nlp) {
  819. log_printf(LOG_LEVEL_DEBUG, "find_node: Got NULL nlp?\n");
  820. return 0;
  821. }
  822. return *nlp;
  823. }
  824. static SaErrorT
  825. evt_add_node(struct in_addr addr, SaClmClusterNodeT *cn)
  826. {
  827. struct member_node_data **nlp;
  828. struct member_node_data *nl;
  829. SaErrorT err = SA_ERR_EXIST;
  830. nlp = lookup_node(addr);
  831. if (!nlp) {
  832. log_printf(LOG_LEVEL_DEBUG, "add_node: Got NULL nlp?\n");
  833. goto an_out;
  834. }
  835. if (*nlp) {
  836. goto an_out;
  837. }
  838. *nlp = malloc(sizeof(struct member_node_data));
  839. if (!nlp) {
  840. return SA_ERR_NO_MEMORY;
  841. }
  842. nl = *nlp;
  843. if (nl) {
  844. memset(nl, 0, sizeof(*nl));
  845. err = SA_OK;
  846. nl->mn_node_addr = addr;
  847. nl->mn_started = 1;
  848. }
  849. list_init(&nl->mn_entry);
  850. list_add(&nl->mn_entry, &mnd);
  851. nl->mn_node_info = *cn;
  852. an_out:
  853. return err;
  854. }
  855. #ifdef REMOVE_NODE
  856. static SaErrorT
  857. evt_remove_node(struct in_addr addr)
  858. {
  859. struct member_node_data **nlp;
  860. struct member_node_data *nl;
  861. SaErrorT err = SA_ERR_NOT_EXIST;
  862. nlp = lookup_node(addr);
  863. if (!nlp) {
  864. log_printf(LOG_LEVEL_DEBUG, "remove_node: Got NULL nlp?\n");
  865. goto an_out;
  866. }
  867. if (!(*nlp)) {
  868. goto an_out;
  869. }
  870. nl = *nlp;
  871. list_del(&nl->mn_entry);
  872. *nlp = nl->mn_next;
  873. free(*nlp);
  874. err = SA_OK;
  875. an_out:
  876. return err;
  877. }
  878. #endif /* REMOVE_NODE */
  879. /*
  880. * Find the oldest node in the membership. This is the one we choose to
  881. * perform some cluster-wide functions like distributing retained events.
  882. */
  883. static struct member_node_data* oldest_node()
  884. {
  885. struct list_head *l;
  886. struct member_node_data *mn = 0;
  887. struct member_node_data *oldest = 0;
  888. for (l = mnd.next; l != &mnd; l = l->next) {
  889. mn = list_entry(l, struct member_node_data, mn_entry);
  890. if (mn->mn_started == 0) {
  891. continue;
  892. }
  893. if ((oldest == NULL) ||
  894. (mn->mn_node_info.bootTimestamp <
  895. oldest->mn_node_info.bootTimestamp )) {
  896. oldest = mn;
  897. } else if (mn->mn_node_info.bootTimestamp ==
  898. oldest->mn_node_info.bootTimestamp) {
  899. if (mn->mn_node_info.nodeId < oldest->mn_node_info.nodeId) {
  900. oldest = mn;
  901. }
  902. }
  903. }
  904. return oldest;
  905. }
  906. /*
  907. * Token callback routine. Send as many mcasts as possible to distribute
  908. * retained events on a config change.
  909. */
  910. static int send_next_retained(void *data)
  911. {
  912. struct req_evt_chan_command cpkt;
  913. struct iovec chn_iovec;
  914. struct event_data *evt;
  915. int res;
  916. if (in_cfg_change && recovery_node) {
  917. /*
  918. * Process messages. When we're done, send the done message
  919. * to the nodes.
  920. */
  921. for (;next_retained != &retained_list;
  922. next_retained = next_retained->next) {
  923. log_printf(LOG_LEVEL_DEBUG, "Sending next retained event\n");
  924. evt = list_entry(next_retained, struct event_data, ed_retained);
  925. evt->ed_event.led_head.id = MESSAGE_REQ_EXEC_EVT_RECOVERY_EVENTDATA;
  926. chn_iovec.iov_base = &evt->ed_event;
  927. chn_iovec.iov_len = evt->ed_event.led_head.size;
  928. res = gmi_mcast(&aisexec_groupname, &chn_iovec, 1,
  929. GMI_PRIO_RECOVERY);
  930. if (res != 0) {
  931. /*
  932. * Try again later.
  933. */
  934. return -1;
  935. }
  936. }
  937. log_printf(RECOVERY_DEBUG, "DONE Sending retained events\n");
  938. memset(&cpkt, 0, sizeof(cpkt));
  939. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  940. cpkt.chc_head.size = sizeof(cpkt);
  941. cpkt.chc_op = EVT_CONF_DONE;
  942. chn_iovec.iov_base = &cpkt;
  943. chn_iovec.iov_len = cpkt.chc_head.size;
  944. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  945. GMI_PRIO_RECOVERY);
  946. }
  947. tok_call_handle = 0;
  948. return 0;
  949. }
  950. /*
  951. * Send our retained events. If we've been chosen as the recovery node, kick
  952. * kick off the process of sending retained events.
  953. */
  954. static void send_retained()
  955. {
  956. struct req_evt_chan_command cpkt;
  957. struct iovec chn_iovec;
  958. int res;
  959. if (list_empty(&retained_list) || !any_joined) {
  960. memset(&cpkt, 0, sizeof(cpkt));
  961. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  962. cpkt.chc_head.size = sizeof(cpkt);
  963. cpkt.chc_op = EVT_CONF_DONE;
  964. chn_iovec.iov_base = &cpkt;
  965. chn_iovec.iov_len = cpkt.chc_head.size;
  966. log_printf(RECOVERY_DEBUG, "No messages to send\n");
  967. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  968. GMI_PRIO_RECOVERY);
  969. } else {
  970. log_printf(RECOVERY_DEBUG,
  971. "Start sending retained messages\n");
  972. recovery_node = 1;
  973. next_retained = retained_list.next;
  974. res = gmi_token_callback_create(&tok_call_handle, send_next_retained,
  975. NULL);
  976. }
  977. if (res != 0) {
  978. log_printf(LOG_LEVEL_ERROR, "ERROR sending evt recovery data\n");
  979. }
  980. }
  981. /*
  982. * Token callback routine. Send as many mcasts as possible to distribute
  983. * open counts on a config change.
  984. */
  985. static int send_next_open_count(void *data)
  986. {
  987. struct req_evt_chan_command cpkt;
  988. struct iovec chn_iovec;
  989. struct event_svr_channel_instance *eci;
  990. int res;
  991. if (in_cfg_change) {
  992. /*
  993. * Process messages. When we're done, send the done message
  994. * to the nodes.
  995. */
  996. memset(&cpkt, 0, sizeof(cpkt));
  997. for (;next_chan != &esc_head;
  998. next_chan = next_chan->next) {
  999. log_printf(RECOVERY_DEBUG, "Sending next open count\n");
  1000. eci = list_entry(next_chan, struct event_svr_channel_instance,
  1001. esc_entry);
  1002. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  1003. cpkt.chc_head.size = sizeof(cpkt);
  1004. cpkt.chc_op = EVT_OPEN_COUNT;
  1005. cpkt.u.chc_set_opens.chc_chan_name = eci->esc_channel_name;
  1006. cpkt.u.chc_set_opens.chc_open_count = eci->esc_local_opens;
  1007. chn_iovec.iov_base = &cpkt;
  1008. chn_iovec.iov_len = cpkt.chc_head.size;
  1009. res = gmi_mcast(&aisexec_groupname, &chn_iovec, 1,
  1010. GMI_PRIO_RECOVERY);
  1011. if (res != 0) {
  1012. /*
  1013. * Try again later.
  1014. */
  1015. return -1;
  1016. }
  1017. }
  1018. log_printf(RECOVERY_DEBUG, "DONE Sending open counts\n");
  1019. memset(&cpkt, 0, sizeof(cpkt));
  1020. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  1021. cpkt.chc_head.size = sizeof(cpkt);
  1022. cpkt.chc_op = EVT_OPEN_COUNT_DONE;
  1023. chn_iovec.iov_base = &cpkt;
  1024. chn_iovec.iov_len = cpkt.chc_head.size;
  1025. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  1026. GMI_PRIO_RECOVERY);
  1027. }
  1028. tok_call_handle = 0;
  1029. return 0;
  1030. }
  1031. /*
  1032. * kick off the process of sending open channel counts during recovery.
  1033. * Every node does this.
  1034. */
  1035. static void send_open_count()
  1036. {
  1037. struct req_evt_chan_command cpkt;
  1038. struct iovec chn_iovec;
  1039. int res;
  1040. if (list_empty(&esc_head)) {
  1041. memset(&cpkt, 0, sizeof(cpkt));
  1042. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  1043. cpkt.chc_head.size = sizeof(cpkt);
  1044. cpkt.chc_op = EVT_OPEN_COUNT_DONE;
  1045. chn_iovec.iov_base = &cpkt;
  1046. chn_iovec.iov_len = cpkt.chc_head.size;
  1047. log_printf(RECOVERY_DEBUG, "No channels to send\n");
  1048. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  1049. GMI_PRIO_RECOVERY);
  1050. } else {
  1051. log_printf(RECOVERY_DEBUG,
  1052. "Start sending open channel count\n");
  1053. next_chan = esc_head.next;
  1054. res = gmi_token_callback_create(&tok_call_handle, send_next_open_count,
  1055. NULL);
  1056. }
  1057. if (res != 0) {
  1058. log_printf(LOG_LEVEL_ERROR, "ERROR sending evt recovery data\n");
  1059. }
  1060. }
  1061. /*
  1062. * keep track of the last event ID from a node.
  1063. * If we get an event ID less than our last, we've already
  1064. * seen it. It's probably a retained event being sent to
  1065. * a new node.
  1066. */
  1067. static int check_last_event(struct lib_event_data *evtpkt,
  1068. struct in_addr addr)
  1069. {
  1070. struct member_node_data *nd;
  1071. SaClmClusterNodeT *cn;
  1072. nd = evt_find_node(addr);
  1073. if (!nd) {
  1074. log_printf(LOG_LEVEL_DEBUG,
  1075. "Node ID 0x%x not found for event %llx\n",
  1076. evtpkt->led_publisher_node_id, evtpkt->led_event_id);
  1077. cn = clm_get_by_nodeid(addr);
  1078. if (!cn) {
  1079. log_printf(LOG_LEVEL_DEBUG,
  1080. "Cluster Node 0x%x not found for event %llx\n",
  1081. evtpkt->led_publisher_node_id, evtpkt->led_event_id);
  1082. } else {
  1083. evt_add_node(addr, cn);
  1084. nd = evt_find_node(addr);
  1085. }
  1086. }
  1087. if (!nd) {
  1088. return 0;
  1089. }
  1090. if ((nd->mn_last_evt_id < evtpkt->led_event_id)) {
  1091. nd->mn_last_evt_id = evtpkt->led_event_id;
  1092. return 0;
  1093. }
  1094. return 1;
  1095. }
  1096. /*
  1097. * Send a message to the app to wake it up if it is polling
  1098. */
  1099. static int message_handler_req_lib_activatepoll(struct conn_info *conn_info,
  1100. void *message)
  1101. {
  1102. struct res_lib_activatepoll res;
  1103. res.header.error = SA_OK;
  1104. res.header.size = sizeof (struct res_lib_activatepoll);
  1105. res.header.id = MESSAGE_RES_LIB_ACTIVATEPOLL;
  1106. libais_send_response(conn_info, &res, sizeof(res));
  1107. return (0);
  1108. }
  1109. /*
  1110. * event id generating code. We use the node ID for this node for the
  1111. * upper 32 bits of the event ID to make sure that we can generate a cluster
  1112. * wide unique event ID for a given event.
  1113. */
  1114. SaErrorT set_event_id(SaClmNodeIdT node_id)
  1115. {
  1116. SaErrorT err = SA_OK;
  1117. if (base_id_top) {
  1118. err = SA_ERR_EXIST;
  1119. }
  1120. base_id_top = (SaEvtEventIdT)node_id << 32;
  1121. return err;
  1122. }
  1123. static SaErrorT get_event_id(uint64_t *event_id)
  1124. {
  1125. *event_id = base_id_top | base_id ;
  1126. base_id = (base_id + 1) & BASE_ID_MASK;
  1127. return SA_OK;
  1128. }
  1129. /*
  1130. * Free up an event structure if it isn't being used anymore.
  1131. */
  1132. static void
  1133. free_event_data(struct event_data *edp)
  1134. {
  1135. if (--edp->ed_ref_count) {
  1136. return;
  1137. }
  1138. log_printf(LOG_LEVEL_DEBUG, "Freeing event ID: 0x%llx\n",
  1139. edp->ed_event.led_event_id);
  1140. if (edp->ed_delivered) {
  1141. free(edp->ed_delivered);
  1142. }
  1143. free(edp);
  1144. }
  1145. /*
  1146. * Timer handler to delete expired events.
  1147. *
  1148. */
  1149. static void
  1150. event_retention_timeout(void *data)
  1151. {
  1152. struct event_data *edp = data;
  1153. log_printf(LOG_LEVEL_DEBUG, "Event ID %llx expired\n",
  1154. edp->ed_event.led_event_id);
  1155. /*
  1156. * adjust next_retained if we're in recovery and
  1157. * were in charge of sending retained events.
  1158. */
  1159. if (in_cfg_change && recovery_node) {
  1160. if (next_retained == &edp->ed_retained) {
  1161. next_retained = edp->ed_retained.next;
  1162. }
  1163. }
  1164. list_del(&edp->ed_retained);
  1165. list_init(&edp->ed_retained);
  1166. /*
  1167. * Check to see it the channel isn't in use anymore.
  1168. */
  1169. edp->ed_my_chan->esc_retained_count--;
  1170. if (edp->ed_my_chan->esc_retained_count == 0) {
  1171. delete_channel(edp->ed_my_chan);
  1172. }
  1173. free_event_data(edp);
  1174. }
  1175. /*
  1176. * clear a particular event's retention time.
  1177. * This will free the event as long as it isn't being
  1178. * currently used.
  1179. *
  1180. */
  1181. static void
  1182. clear_retention_time(SaEvtEventIdT event_id)
  1183. {
  1184. struct event_data *edp;
  1185. struct list_head *l, *nxt;
  1186. int ret;
  1187. log_printf(LOG_LEVEL_DEBUG, "Search for Event ID %llx\n", event_id);
  1188. for(l = retained_list.next; l != &retained_list; l = nxt) {
  1189. nxt = l->next;
  1190. edp = list_entry(l, struct event_data, ed_retained);
  1191. if (edp->ed_event.led_event_id != event_id) {
  1192. continue;
  1193. }
  1194. log_printf(LOG_LEVEL_DEBUG,
  1195. "Clear retention time for Event ID %llx\n",
  1196. edp->ed_event.led_event_id);
  1197. ret = poll_timer_delete(aisexec_poll_handle, edp->ed_timer_handle);
  1198. if (ret != 0 ) {
  1199. log_printf(LOG_LEVEL_ERROR, "Error expiring event ID %llx\n",
  1200. edp->ed_event.led_event_id);
  1201. return;
  1202. }
  1203. edp->ed_event.led_retention_time = 0;
  1204. list_del(&edp->ed_retained);
  1205. list_init(&edp->ed_retained);
  1206. /*
  1207. * Check to see if the channel isn't in use anymore.
  1208. */
  1209. edp->ed_my_chan->esc_retained_count--;
  1210. if (edp->ed_my_chan->esc_retained_count == 0) {
  1211. delete_channel(edp->ed_my_chan);
  1212. }
  1213. free_event_data(edp);
  1214. break;
  1215. }
  1216. }
  1217. /*
  1218. * Remove specified channel from event delivery list
  1219. */
  1220. static void
  1221. remove_delivered_channel(struct event_svr_channel_open *eco)
  1222. {
  1223. int i;
  1224. struct list_head *l;
  1225. struct event_data *edp;
  1226. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1227. edp = list_entry(l, struct event_data, ed_retained);
  1228. for (i = 0; i < edp->ed_delivered_next; i++) {
  1229. if (edp->ed_delivered[i] == eco) {
  1230. edp->ed_delivered_next--;
  1231. if (edp->ed_delivered_next == i) {
  1232. break;
  1233. }
  1234. memmove(&edp->ed_delivered[i],
  1235. &edp->ed_delivered[i+1],
  1236. &edp->ed_delivered[edp->ed_delivered_next] -
  1237. &edp->ed_delivered[i]);
  1238. break;
  1239. }
  1240. }
  1241. }
  1242. return;
  1243. }
  1244. /*
  1245. * If there is a retention time, add this open channel to the event so
  1246. * we can check if we've already delivered this message later if a new
  1247. * subscription matches.
  1248. */
  1249. #define DELIVER_SIZE 8
  1250. static void
  1251. evt_delivered(struct event_data *evt, struct event_svr_channel_open *eco)
  1252. {
  1253. if (!evt->ed_event.led_retention_time) {
  1254. return;
  1255. }
  1256. log_printf(LOG_LEVEL_DEBUG, "delivered ID %llx to eco %p\n",
  1257. evt->ed_event.led_event_id, eco);
  1258. if (evt->ed_delivered_count == evt->ed_delivered_next) {
  1259. evt->ed_delivered = realloc(evt->ed_delivered,
  1260. DELIVER_SIZE * sizeof(struct event_svr_channel_open *));
  1261. memset(evt->ed_delivered + evt->ed_delivered_next, 0,
  1262. DELIVER_SIZE * sizeof(struct event_svr_channel_open *));
  1263. evt->ed_delivered_next = evt->ed_delivered_count;
  1264. evt->ed_delivered_count += DELIVER_SIZE;
  1265. }
  1266. evt->ed_delivered[evt->ed_delivered_next++] = eco;
  1267. }
  1268. /*
  1269. * Check to see if an event has already been delivered to this open channel
  1270. */
  1271. static int
  1272. evt_already_delivered(struct event_data *evt,
  1273. struct event_svr_channel_open *eco)
  1274. {
  1275. int i;
  1276. if (!evt->ed_event.led_retention_time) {
  1277. return 0;
  1278. }
  1279. log_printf(LOG_LEVEL_DEBUG, "Deliver count: %d deliver_next %d\n",
  1280. evt->ed_delivered_count, evt->ed_delivered_next);
  1281. for (i = 0; i < evt->ed_delivered_next; i++) {
  1282. log_printf(LOG_LEVEL_DEBUG, "Checking ID %llx delivered %p eco %p\n",
  1283. evt->ed_event.led_event_id, evt->ed_delivered[i], eco);
  1284. if (evt->ed_delivered[i] == eco) {
  1285. return 1;
  1286. }
  1287. }
  1288. return 0;
  1289. }
  1290. /*
  1291. * Compare a filter to a given pattern.
  1292. * return SA_OK if the pattern matches a filter
  1293. */
  1294. static SaErrorT
  1295. filter_match(SaEvtEventPatternT *ep, SaEvtEventFilterT *ef)
  1296. {
  1297. int ret;
  1298. ret = SA_ERR_FAILED_OPERATION;
  1299. switch (ef->filterType) {
  1300. case SA_EVT_PREFIX_FILTER:
  1301. if (ef->filter.patternSize > ep->patternSize) {
  1302. break;
  1303. }
  1304. if (strncmp(ef->filter.pattern, ep->pattern,
  1305. ef->filter.patternSize) == 0) {
  1306. ret = SA_OK;
  1307. }
  1308. break;
  1309. case SA_EVT_SUFFIX_FILTER:
  1310. if (ef->filter.patternSize > ep->patternSize) {
  1311. break;
  1312. }
  1313. if (strncmp(ef->filter.pattern,
  1314. &ep->pattern[ep->patternSize - ef->filter.patternSize],
  1315. ef->filter.patternSize) == 0) {
  1316. ret = SA_OK;
  1317. }
  1318. break;
  1319. case SA_EVT_EXACT_FILTER:
  1320. if (ef->filter.patternSize != ep->patternSize) {
  1321. break;
  1322. }
  1323. if (strncmp(ef->filter.pattern, ep->pattern,
  1324. ef->filter.patternSize) == 0) {
  1325. ret = SA_OK;
  1326. }
  1327. break;
  1328. case SA_EVT_PASS_ALL_FILTER:
  1329. ret = SA_OK;
  1330. break;
  1331. default:
  1332. break;
  1333. }
  1334. return ret;
  1335. }
  1336. /*
  1337. * compare the event's patterns with the subscription's filter rules.
  1338. * SA_OK is returned if the event matches the filter rules.
  1339. */
  1340. static SaErrorT
  1341. event_match(struct event_data *evt,
  1342. struct event_svr_channel_subscr *ecs)
  1343. {
  1344. SaEvtEventFilterT *ef;
  1345. SaEvtEventPatternT *ep;
  1346. uint32_t filt_count;
  1347. SaErrorT ret = SA_OK;
  1348. int i;
  1349. ep = (SaEvtEventPatternT *)(&evt->ed_event.led_body[0]);
  1350. ef = ecs->ecs_filters->filters;
  1351. filt_count = min(ecs->ecs_filters->filtersNumber,
  1352. evt->ed_event.led_patterns_number);
  1353. for (i = 0; i < filt_count; i++) {
  1354. ret = filter_match(ep, ef);
  1355. if (ret != SA_OK) {
  1356. break;
  1357. }
  1358. ep++;
  1359. ef++;
  1360. }
  1361. return ret;
  1362. }
  1363. /*
  1364. * Scan undelivered pending events and either remove them if no subscription
  1365. * filters match anymore or re-assign them to another matching subscription
  1366. */
  1367. static void
  1368. filter_undelivered_events(struct event_svr_channel_open *op_chan)
  1369. {
  1370. struct event_svr_channel_open *eco;
  1371. struct event_svr_channel_instance *eci;
  1372. struct event_svr_channel_subscr *ecs;
  1373. struct chan_event_list *cel;
  1374. struct libevt_ci *esip = &op_chan->eco_conn_info->ais_ci.u.libevt_ci;
  1375. struct list_head *l, *nxt;
  1376. struct list_head *l1, *l2;
  1377. int i;
  1378. eci = op_chan->eco_channel;
  1379. /*
  1380. * Scan each of the priority queues for messages
  1381. */
  1382. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  1383. /*
  1384. * examine each message queued for delivery
  1385. */
  1386. for (l = esip->esi_events[i].next; l != &esip->esi_events[i]; l = nxt) {
  1387. nxt = l->next;
  1388. cel = list_entry(l, struct chan_event_list, cel_entry);
  1389. /*
  1390. * Check open channels
  1391. */
  1392. for (l1 = eci->esc_open_chans.next;
  1393. l1 != &eci->esc_open_chans; l1 = l1->next) {
  1394. eco = list_entry(l1, struct event_svr_channel_open, eco_entry);
  1395. /*
  1396. * See if this channel open instance belongs
  1397. * to this evtinitialize instance
  1398. */
  1399. if (eco->eco_conn_info != op_chan->eco_conn_info) {
  1400. continue;
  1401. }
  1402. /*
  1403. * See if enabled to receive
  1404. */
  1405. if (!(eco->eco_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  1406. continue;
  1407. }
  1408. /*
  1409. * Check subscriptions
  1410. */
  1411. for (l2 = eco->eco_subscr.next;
  1412. l2 != &eco->eco_subscr; l2 = l2->next) {
  1413. ecs = list_entry(l2,
  1414. struct event_svr_channel_subscr, ecs_entry);
  1415. if (event_match(cel->cel_event, ecs) == SA_OK) {
  1416. /*
  1417. * Something still matches.
  1418. * We'll assign it to
  1419. * the new subscription.
  1420. */
  1421. cel->cel_sub_id = ecs->ecs_sub_id;
  1422. cel->cel_chan_handle = eco->eco_lib_handle;
  1423. goto next_event;
  1424. }
  1425. }
  1426. }
  1427. /*
  1428. * No subscription filter matches anymore. We
  1429. * can delete this event.
  1430. */
  1431. list_del(&cel->cel_entry);
  1432. list_init(&cel->cel_entry);
  1433. esip->esi_nevents--;
  1434. free_event_data(cel->cel_event);
  1435. free(cel);
  1436. next_event:
  1437. continue;
  1438. }
  1439. }
  1440. }
  1441. /*
  1442. * Notify the library of a pending event
  1443. */
  1444. static void __notify_event(struct conn_info *conn_info)
  1445. {
  1446. struct res_evt_event_data res;
  1447. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1448. log_printf(LOG_LEVEL_DEBUG, "DELIVER: notify\n");
  1449. if (esip->esi_nevents != 0) {
  1450. res.evd_head.size = sizeof(res);
  1451. res.evd_head.id = MESSAGE_RES_EVT_AVAILABLE;
  1452. res.evd_head.error = SA_OK;
  1453. libais_send_response(conn_info, &res, sizeof(res));
  1454. }
  1455. }
  1456. inline void notify_event(struct conn_info *conn_info)
  1457. {
  1458. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1459. /*
  1460. * Give the library a kick if there aren't already
  1461. * events queued for delivery.
  1462. */
  1463. if (esip->esi_nevents++ == 0) {
  1464. __notify_event(conn_info);
  1465. }
  1466. }
  1467. /*
  1468. * sends/queues up an event for a subscribed channel.
  1469. */
  1470. static void
  1471. deliver_event(struct event_data *evt,
  1472. struct event_svr_channel_open *eco,
  1473. struct event_svr_channel_subscr *ecs)
  1474. {
  1475. struct chan_event_list *ep;
  1476. struct libevt_ci *esip = &eco->eco_conn_info->ais_ci.u.libevt_ci;
  1477. SaEvtEventPriorityT evt_prio = evt->ed_event.led_priority;
  1478. struct chan_event_list *cel;
  1479. int do_deliver_event = 0;
  1480. int do_deliver_warning = 0;
  1481. int i;
  1482. if (evt_prio > SA_EVT_LOWEST_PRIORITY) {
  1483. evt_prio = SA_EVT_LOWEST_PRIORITY;
  1484. }
  1485. /*
  1486. * Delivery queue check.
  1487. * - If the queue is blocked, see if we've sent enough messages to
  1488. * unblock it.
  1489. * - If it isn't blocked, see if this message will put us over the top.
  1490. * - If we can't deliver this message, see if we can toss some lower
  1491. * priority message to make room for this one.
  1492. * - If we toss any messages, queue up an event of SA_EVT_LOST_EVENT_PATTERN
  1493. * to let the application know that we dropped some messages.
  1494. */
  1495. if (esip->esi_queue_blocked) {
  1496. if (esip->esi_nevents < MIN_EVT_QUEUE_RESUME) {
  1497. esip->esi_queue_blocked = 0;
  1498. log_printf(LOG_LEVEL_DEBUG, "unblock\n");
  1499. }
  1500. }
  1501. if (!esip->esi_queue_blocked &&
  1502. (esip->esi_nevents >= MAX_EVT_DELIVERY_QUEUE)) {
  1503. log_printf(LOG_LEVEL_DEBUG, "block\n");
  1504. esip->esi_queue_blocked = 1;
  1505. do_deliver_warning = 1;
  1506. }
  1507. if (esip->esi_queue_blocked) {
  1508. do_deliver_event = 0;
  1509. for (i = SA_EVT_LOWEST_PRIORITY; i > evt_prio; i--) {
  1510. if (!list_empty(&esip->esi_events[i])) {
  1511. /*
  1512. * Get the last item on the list, so we drop the most
  1513. * recent lowest priority event.
  1514. */
  1515. cel = list_entry(esip->esi_events[i].prev,
  1516. struct chan_event_list, cel_entry);
  1517. log_printf(LOG_LEVEL_DEBUG, "Drop 0x%0llx\n",
  1518. cel->cel_event->ed_event.led_event_id);
  1519. list_del(&cel->cel_entry);
  1520. free_event_data(cel->cel_event);
  1521. free(cel);
  1522. esip->esi_nevents--;
  1523. do_deliver_event = 1;
  1524. break;
  1525. }
  1526. }
  1527. } else {
  1528. do_deliver_event = 1;
  1529. }
  1530. /*
  1531. * Queue the event for delivery
  1532. */
  1533. if (do_deliver_event) {
  1534. ep = malloc(sizeof(*ep));
  1535. if (!ep) {
  1536. log_printf(LOG_LEVEL_WARNING,
  1537. "Memory allocation error, can't deliver event\n");
  1538. return;
  1539. }
  1540. evt->ed_ref_count++;
  1541. ep->cel_chan_handle = eco->eco_lib_handle;
  1542. ep->cel_sub_id = ecs->ecs_sub_id;
  1543. list_init(&ep->cel_entry);
  1544. ep->cel_event = evt;
  1545. list_add_tail(&ep->cel_entry, &esip->esi_events[evt_prio]);
  1546. evt_delivered(evt, eco);
  1547. notify_event(eco->eco_conn_info);
  1548. }
  1549. /*
  1550. * If we dropped an event, queue this so that the application knows
  1551. * what has happened.
  1552. */
  1553. if (do_deliver_warning) {
  1554. struct event_data *ed;
  1555. ed = malloc(dropped_event_size);
  1556. if (!ed) {
  1557. log_printf(LOG_LEVEL_WARNING,
  1558. "Memory allocation error, can't deliver event\n");
  1559. return;
  1560. }
  1561. log_printf(LOG_LEVEL_DEBUG, "Warn 0x%0llx\n",
  1562. evt->ed_event.led_event_id);
  1563. memcpy(ed, dropped_event, dropped_event_size);
  1564. ed->ed_event.led_publish_time = clust_time_now();
  1565. list_init(&ed->ed_retained);
  1566. ep = malloc(sizeof(*ep));
  1567. if (!ep) {
  1568. log_printf(LOG_LEVEL_WARNING,
  1569. "Memory allocation error, can't deliver event\n");
  1570. return;
  1571. }
  1572. ep->cel_chan_handle = eco->eco_lib_handle;
  1573. ep->cel_sub_id = ecs->ecs_sub_id;
  1574. list_init(&ep->cel_entry);
  1575. ep->cel_event = ed;
  1576. list_add_tail(&ep->cel_entry, &esip->esi_events[SA_EVT_HIGHEST_PRIORITY]);
  1577. notify_event(eco->eco_conn_info);
  1578. }
  1579. }
  1580. /*
  1581. * Take an event received from the network and fix it up to be usable.
  1582. * - fix up pointers for pattern list.
  1583. * - fill in some channel info
  1584. */
  1585. static struct event_data *
  1586. make_local_event(struct lib_event_data *p,
  1587. struct event_svr_channel_instance *eci)
  1588. {
  1589. struct event_data *ed;
  1590. SaEvtEventPatternT *eps;
  1591. SaUint8T *str;
  1592. uint32_t ed_size;
  1593. int i;
  1594. ed_size = sizeof(*ed) + p->led_user_data_offset + p->led_user_data_size;
  1595. ed = malloc(ed_size);
  1596. if (!ed) {
  1597. return 0;
  1598. }
  1599. memset(ed, 0, ed_size);
  1600. list_init(&ed->ed_retained);
  1601. ed->ed_my_chan = eci;
  1602. /*
  1603. * Fill in lib_event_data and make the pattern pointers valid
  1604. */
  1605. memcpy(&ed->ed_event, p, sizeof(*p) +
  1606. p->led_user_data_offset + p->led_user_data_size);
  1607. eps = (SaEvtEventPatternT *)ed->ed_event.led_body;
  1608. str = ed->ed_event.led_body +
  1609. (ed->ed_event.led_patterns_number * sizeof(SaEvtEventPatternT));
  1610. for (i = 0; i < ed->ed_event.led_patterns_number; i++) {
  1611. eps->pattern = str;
  1612. str += eps->patternSize;
  1613. eps++;
  1614. }
  1615. ed->ed_ref_count++;
  1616. return ed;
  1617. }
  1618. /*
  1619. * Set an event to be retained.
  1620. */
  1621. static void retain_event(struct event_data *evt)
  1622. {
  1623. uint32_t ret;
  1624. int msec_in_future;
  1625. evt->ed_ref_count++;
  1626. evt->ed_my_chan->esc_retained_count++;
  1627. list_add_tail(&evt->ed_retained, &retained_list);
  1628. /*
  1629. * Time in nanoseconds - convert to miliseconds
  1630. */
  1631. msec_in_future = (uint32_t)((evt->ed_event.led_retention_time) / 1000000ULL);
  1632. ret = poll_timer_add(aisexec_poll_handle,
  1633. msec_in_future,
  1634. evt,
  1635. event_retention_timeout,
  1636. &evt->ed_timer_handle);
  1637. if (ret != 0) {
  1638. log_printf(LOG_LEVEL_ERROR,
  1639. "retention of event id 0x%llx failed\n",
  1640. evt->ed_event.led_event_id);
  1641. } else {
  1642. log_printf(LOG_LEVEL_DEBUG, "Retain event ID 0x%llx\n",
  1643. evt->ed_event.led_event_id);
  1644. }
  1645. }
  1646. /*
  1647. * Scan the subscription list and look for the specified subsctiption ID.
  1648. * Only look for the ID in subscriptions that are associated with the
  1649. * saEvtInitialize associated with the specified open channel.
  1650. */
  1651. static struct event_svr_channel_subscr *find_subscr(
  1652. struct event_svr_channel_open *open_chan, SaEvtSubscriptionIdT sub_id)
  1653. {
  1654. struct event_svr_channel_instance *eci;
  1655. struct event_svr_channel_subscr *ecs;
  1656. struct event_svr_channel_open *eco;
  1657. struct list_head *l, *l1;
  1658. struct conn_info* conn_info = open_chan->eco_conn_info;
  1659. eci = open_chan->eco_channel;
  1660. /*
  1661. * Check for subscription id already in use.
  1662. * Subscriptions are unique within saEvtInitialize (Callback scope).
  1663. */
  1664. for (l = eci->esc_open_chans.next; l != &eci->esc_open_chans; l = l->next) {
  1665. eco = list_entry(l, struct event_svr_channel_open, eco_entry);
  1666. /*
  1667. * Don't bother with open channels associated with another
  1668. * EvtInitialize
  1669. */
  1670. if (eco->eco_conn_info != conn_info) {
  1671. continue;
  1672. }
  1673. for (l1 = eco->eco_subscr.next; l1 != &eco->eco_subscr; l1 = l1->next) {
  1674. ecs = list_entry(l1, struct event_svr_channel_subscr, ecs_entry);
  1675. if (ecs->ecs_sub_id == sub_id) {
  1676. return ecs;
  1677. }
  1678. }
  1679. }
  1680. return 0;
  1681. }
  1682. /*
  1683. * Handler for saEvtInitialize
  1684. */
  1685. static int evt_initialize(struct conn_info *conn_info, void *msg)
  1686. {
  1687. struct res_lib_init res;
  1688. struct libevt_ci *libevt_ci = &conn_info->ais_ci.u.libevt_ci;
  1689. int i;
  1690. res.header.size = sizeof (struct res_lib_init);
  1691. res.header.id = MESSAGE_RES_INIT;
  1692. res.header.error = SA_OK;
  1693. log_printf(LOG_LEVEL_DEBUG, "saEvtInitialize request.\n");
  1694. if (!conn_info->authenticated) {
  1695. log_printf(LOG_LEVEL_ERROR, "event service: Not authenticated\n");
  1696. res.header.error = SA_ERR_SECURITY;
  1697. libais_send_response(conn_info, &res, sizeof(res));
  1698. return -1;
  1699. }
  1700. memset(libevt_ci, 0, sizeof(*libevt_ci));
  1701. list_init(&libevt_ci->esi_open_chans);
  1702. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  1703. list_init(&libevt_ci->esi_events[i]);
  1704. }
  1705. conn_info->service = SOCKET_SERVICE_EVT;
  1706. list_init (&conn_info->conn_list);
  1707. list_add_tail(&conn_info->conn_list, &ci_head);
  1708. libais_send_response (conn_info, &res, sizeof(res));
  1709. return 0;
  1710. }
  1711. /*
  1712. * Handler for saEvtChannelOpen
  1713. */
  1714. static int lib_evt_open_channel(struct conn_info *conn_info, void *message)
  1715. {
  1716. uint32_t handle;
  1717. SaErrorT error;
  1718. struct req_evt_channel_open *req;
  1719. struct res_evt_channel_open res;
  1720. struct open_chan_pending *ocp;
  1721. int msec_in_future;
  1722. int ret;
  1723. req = message;
  1724. log_printf(LOG_LEVEL_DEBUG,
  1725. "saEvtChannelOpen (Open channel request)\n");
  1726. log_printf(LOG_LEVEL_DEBUG,
  1727. "handle 0x%x, to 0x%llx\n",
  1728. req->ico_c_handle,
  1729. req->ico_timeout);
  1730. log_printf(LOG_LEVEL_DEBUG, "flags %x, channel name(%d) %s\n",
  1731. req->ico_open_flag,
  1732. req->ico_channel_name.length,
  1733. req->ico_channel_name.value);
  1734. /*
  1735. * Open the channel.
  1736. *
  1737. */
  1738. error = evt_open_channel(&req->ico_channel_name, req->ico_open_flag);
  1739. if (error != SA_OK) {
  1740. goto open_return;
  1741. }
  1742. ocp = malloc(sizeof(struct open_chan_pending));
  1743. if (!ocp) {
  1744. error = SA_ERR_NO_MEMORY;
  1745. goto open_return;
  1746. }
  1747. ocp->ocp_async = 0;
  1748. ocp->ocp_invocation = 0;
  1749. ocp->ocp_chan_name = req->ico_channel_name;
  1750. ocp->ocp_open_flag = req->ico_open_flag;
  1751. ocp->ocp_conn_info = conn_info;
  1752. ocp->ocp_timer_handle = 0;
  1753. list_init(&ocp->ocp_entry);
  1754. list_add_tail(&ocp->ocp_entry, &open_pending);
  1755. if (req->ico_timeout != 0) {
  1756. msec_in_future = (uint32_t)(req->ico_timeout / 1000000ULL);
  1757. ret = poll_timer_add(aisexec_poll_handle,
  1758. msec_in_future,
  1759. ocp,
  1760. chan_open_timeout,
  1761. &ocp->ocp_timer_handle);
  1762. if (ret != 0) {
  1763. log_printf(LOG_LEVEL_WARNING,
  1764. "Error setting timeout for open channel %s\n",
  1765. req->ico_channel_name.value);
  1766. }
  1767. }
  1768. return 0;
  1769. open_return:
  1770. res.ico_head.size = sizeof(res);
  1771. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  1772. res.ico_head.error = error;
  1773. res.ico_channel_handle = handle;
  1774. libais_send_response (conn_info, &res, sizeof(res));
  1775. return 0;
  1776. }
  1777. /*
  1778. * Used by the channel close code and by the implicit close
  1779. * when saEvtFinalize is called with channels open.
  1780. */
  1781. static SaErrorT
  1782. common_chan_close(struct event_svr_channel_open *eco, struct libevt_ci *esip)
  1783. {
  1784. struct event_svr_channel_subscr *ecs;
  1785. struct list_head *l, *nxt;
  1786. log_printf(LOG_LEVEL_DEBUG, "Close channel %s flags 0x%02x\n",
  1787. eco->eco_channel->esc_channel_name.value,
  1788. eco->eco_flags);
  1789. /*
  1790. * Unlink the channel open structure.
  1791. *
  1792. * Check for subscriptions and deal with them. In this case
  1793. * if there are any, we just implicitly unsubscribe.
  1794. *
  1795. * When We're done with the channel open data then we can
  1796. * remove it's handle (this frees the memory too).
  1797. *
  1798. */
  1799. list_del(&eco->eco_entry);
  1800. list_del(&eco->eco_instance_entry);
  1801. for (l = eco->eco_subscr.next; l != &eco->eco_subscr; l = nxt) {
  1802. nxt = l->next;
  1803. ecs = list_entry(l, struct event_svr_channel_subscr, ecs_entry);
  1804. log_printf(LOG_LEVEL_DEBUG, "Unsubscribe ID: %x\n",
  1805. ecs->ecs_sub_id);
  1806. list_del(&ecs->ecs_entry);
  1807. free(ecs);
  1808. /*
  1809. * Purge any pending events associated with this subscription
  1810. * that don't match another subscription.
  1811. */
  1812. filter_undelivered_events(eco);
  1813. }
  1814. /*
  1815. * Remove this channel from the retained event's notion
  1816. * of who they have been delivered to.
  1817. */
  1818. remove_delivered_channel(eco);
  1819. return evt_close_channel(&eco->eco_channel->esc_channel_name);
  1820. }
  1821. /*
  1822. * Handler for saEvtChannelClose
  1823. */
  1824. static int lib_evt_close_channel(struct conn_info *conn_info, void *message)
  1825. {
  1826. struct req_evt_channel_close *req;
  1827. struct res_evt_channel_close res;
  1828. struct event_svr_channel_open *eco;
  1829. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1830. SaErrorT error;
  1831. void *ptr;
  1832. req = message;
  1833. log_printf(LOG_LEVEL_DEBUG,
  1834. "saEvtChannelClose (Close channel request)\n");
  1835. log_printf(LOG_LEVEL_DEBUG, "handle 0x%x\n", req->icc_channel_handle);
  1836. /*
  1837. * look up the channel handle
  1838. */
  1839. error = saHandleInstanceGet(&esip->esi_hdb,
  1840. req->icc_channel_handle, &ptr);
  1841. if (error != SA_OK) {
  1842. goto chan_close_done;
  1843. }
  1844. eco = ptr;
  1845. common_chan_close(eco, esip);
  1846. saHandleDestroy(&esip->esi_hdb, req->icc_channel_handle);
  1847. saHandleInstancePut(&esip->esi_hdb, req->icc_channel_handle);
  1848. chan_close_done:
  1849. res.icc_head.size = sizeof(res);
  1850. res.icc_head.id = MESSAGE_RES_EVT_CLOSE_CHANNEL;
  1851. res.icc_head.error = error;
  1852. libais_send_response (conn_info, &res, sizeof(res));
  1853. return 0;
  1854. }
  1855. /*
  1856. * Subscribe to an event channel.
  1857. *
  1858. * - First look up the channel to subscribe.
  1859. * - Make sure that the subscription ID is not already in use.
  1860. * - Fill in the subscription data structures and add them to the channels
  1861. * subscription list.
  1862. * - See if there are any events with retetion times that need to be delivered
  1863. * because of the new subscription.
  1864. */
  1865. static char *filter_types[] = {
  1866. "INVALID FILTER TYPE",
  1867. "SA_EVT_PREFIX_FILTER",
  1868. "SA_EVT_SUFFIX_FILTER",
  1869. "SA_EVT_EXACT_FILTER",
  1870. "SA_EVT_PASS_ALL_FILTER",
  1871. };
  1872. /*
  1873. * saEvtEventSubscribe Handler
  1874. */
  1875. static int lib_evt_event_subscribe(struct conn_info *conn_info, void *message)
  1876. {
  1877. struct req_evt_event_subscribe *req;
  1878. struct res_evt_event_subscribe res;
  1879. SaEvtEventFilterArrayT *filters;
  1880. SaErrorT error = SA_OK;
  1881. struct event_svr_channel_open *eco;
  1882. struct event_svr_channel_instance *eci;
  1883. struct event_svr_channel_subscr *ecs;
  1884. struct event_data *evt;
  1885. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1886. struct list_head *l;
  1887. void *ptr;
  1888. int i;
  1889. req = message;
  1890. log_printf(LOG_LEVEL_DEBUG,
  1891. "saEvtEventSubscribe (Subscribe request)\n");
  1892. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%x\n",
  1893. req->ics_sub_id);
  1894. error = evtfilt_to_aisfilt(req, &filters);
  1895. if (error == SA_OK) {
  1896. log_printf(LOG_LEVEL_DEBUG, "Subscribe filters count %d\n",
  1897. filters->filtersNumber);
  1898. for (i = 0; i < filters->filtersNumber; i++) {
  1899. log_printf(LOG_LEVEL_DEBUG, "type %s(%d) sz %d, <%s>\n",
  1900. filter_types[filters->filters[i].filterType],
  1901. filters->filters[i].filterType,
  1902. filters->filters[i].filter.patternSize,
  1903. (filters->filters[i].filter.patternSize)
  1904. ? (char *)filters->filters[i].filter.pattern
  1905. : "");
  1906. }
  1907. }
  1908. if (error != SA_OK) {
  1909. goto subr_done;
  1910. }
  1911. /*
  1912. * look up the channel handle
  1913. */
  1914. error = saHandleInstanceGet(&esip->esi_hdb,
  1915. req->ics_channel_handle, &ptr);
  1916. if (error != SA_OK) {
  1917. goto subr_done;
  1918. }
  1919. eco = ptr;
  1920. eci = eco->eco_channel;
  1921. /*
  1922. * See if the id is already being used
  1923. */
  1924. ecs = find_subscr(eco, req->ics_sub_id);
  1925. if (ecs) {
  1926. error = SA_ERR_EXIST;
  1927. goto subr_put;
  1928. }
  1929. ecs = (struct event_svr_channel_subscr *)malloc(sizeof(*ecs));
  1930. if (!ecs) {
  1931. error = SA_ERR_NO_MEMORY;
  1932. goto subr_put;
  1933. }
  1934. ecs->ecs_filters = filters;
  1935. ecs->ecs_sub_id = req->ics_sub_id;
  1936. list_init(&ecs->ecs_entry);
  1937. list_add(&ecs->ecs_entry, &eco->eco_subscr);
  1938. res.ics_head.size = sizeof(res);
  1939. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  1940. res.ics_head.error = error;
  1941. libais_send_response (conn_info, &res, sizeof(res));
  1942. /*
  1943. * See if an existing event with a retention time
  1944. * needs to be delivered based on this subscription
  1945. */
  1946. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1947. evt = list_entry(l, struct event_data, ed_retained);
  1948. log_printf(LOG_LEVEL_DEBUG,
  1949. "Checking event ID %llx chanp %p -- sub chanp %p\n",
  1950. evt->ed_event.led_event_id, evt->ed_my_chan, eci);
  1951. if (evt->ed_my_chan == eci) {
  1952. if (evt_already_delivered(evt, eco)) {
  1953. continue;
  1954. }
  1955. if (event_match(evt, ecs) == SA_OK) {
  1956. log_printf(LOG_LEVEL_DEBUG,
  1957. "deliver event ID: 0x%llx\n",
  1958. evt->ed_event.led_event_id);
  1959. deliver_event(evt, eco, ecs);
  1960. }
  1961. }
  1962. }
  1963. saHandleInstancePut(&esip->esi_hdb, req->ics_channel_handle);
  1964. return 0;
  1965. subr_put:
  1966. saHandleInstancePut(&esip->esi_hdb, req->ics_channel_handle);
  1967. subr_done:
  1968. res.ics_head.size = sizeof(res);
  1969. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  1970. res.ics_head.error = error;
  1971. libais_send_response (conn_info, &res, sizeof(res));
  1972. return 0;
  1973. }
  1974. /*
  1975. * saEvtEventUnsubscribe Handler
  1976. */
  1977. static int lib_evt_event_unsubscribe(struct conn_info *conn_info,
  1978. void *message)
  1979. {
  1980. struct req_evt_event_unsubscribe *req;
  1981. struct res_evt_event_unsubscribe res;
  1982. struct event_svr_channel_open *eco;
  1983. struct event_svr_channel_instance *eci;
  1984. struct event_svr_channel_subscr *ecs;
  1985. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1986. SaErrorT error = SA_OK;
  1987. void *ptr;
  1988. req = message;
  1989. log_printf(LOG_LEVEL_DEBUG,
  1990. "saEvtEventUnsubscribe (Unsubscribe request)\n");
  1991. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%x\n",
  1992. req->icu_sub_id);
  1993. /*
  1994. * look up the channel handle, get the open channel
  1995. * data.
  1996. */
  1997. error = saHandleInstanceGet(&esip->esi_hdb,
  1998. req->icu_channel_handle, &ptr);
  1999. if (error != SA_OK) {
  2000. goto unsubr_done;
  2001. }
  2002. eco = ptr;
  2003. eci = eco->eco_channel;
  2004. /*
  2005. * Make sure that the id exists.
  2006. */
  2007. ecs = find_subscr(eco, req->icu_sub_id);
  2008. if (!ecs) {
  2009. error = SA_ERR_INVALID_PARAM;
  2010. goto unsubr_put;
  2011. }
  2012. list_del(&ecs->ecs_entry);
  2013. log_printf(LOG_LEVEL_DEBUG,
  2014. "unsubscribe from channel %s subscription ID 0x%x "
  2015. "with %d filters\n",
  2016. eci->esc_channel_name.value,
  2017. ecs->ecs_sub_id, ecs->ecs_filters->filtersNumber);
  2018. free_filters(ecs->ecs_filters);
  2019. free(ecs);
  2020. unsubr_put:
  2021. saHandleInstancePut(&esip->esi_hdb, req->icu_channel_handle);
  2022. unsubr_done:
  2023. res.icu_head.size = sizeof(res);
  2024. res.icu_head.id = MESSAGE_RES_EVT_UNSUBSCRIBE;
  2025. res.icu_head.error = error;
  2026. libais_send_response (conn_info, &res, sizeof(res));
  2027. return 0;
  2028. }
  2029. /*
  2030. * saEvtEventPublish Handler
  2031. */
  2032. static int lib_evt_event_publish(struct conn_info *conn_info, void *message)
  2033. {
  2034. struct lib_event_data *req;
  2035. struct res_evt_event_publish res;
  2036. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2037. struct event_svr_channel_open *eco;
  2038. struct event_svr_channel_instance *eci;
  2039. SaEvtEventIdT event_id = 0;
  2040. SaErrorT error = SA_OK;
  2041. struct iovec pub_iovec;
  2042. void *ptr;
  2043. int result;
  2044. req = message;
  2045. log_printf(LOG_LEVEL_DEBUG,
  2046. "saEvtEventPublish (Publish event request)\n");
  2047. /*
  2048. * look up and validate open channel info
  2049. */
  2050. error = saHandleInstanceGet(&esip->esi_hdb,
  2051. req->led_svr_channel_handle, &ptr);
  2052. if (error != SA_OK) {
  2053. goto pub_done;
  2054. }
  2055. eco = ptr;
  2056. eci = eco->eco_channel;
  2057. /*
  2058. * modify the request structure for sending event data to subscribed
  2059. * processes.
  2060. */
  2061. get_event_id(&event_id);
  2062. req->led_head.id = MESSAGE_REQ_EXEC_EVT_EVENTDATA;
  2063. req->led_chan_name = eci->esc_channel_name;
  2064. req->led_event_id = event_id;
  2065. /*
  2066. * Distribute the event.
  2067. * The multicasted event will be picked up and delivered
  2068. * locally by the local network event receiver.
  2069. */
  2070. pub_iovec.iov_base = req;
  2071. pub_iovec.iov_len = req->led_head.size;
  2072. result = gmi_mcast (&aisexec_groupname, &pub_iovec, 1, GMI_PRIO_LOW);
  2073. if (result != 0) {
  2074. error = SA_ERR_SYSTEM;
  2075. }
  2076. saHandleInstancePut(&esip->esi_hdb, req->led_svr_channel_handle);
  2077. pub_done:
  2078. res.iep_head.size = sizeof(res);
  2079. res.iep_head.id = MESSAGE_RES_EVT_PUBLISH;
  2080. res.iep_head.error = error;
  2081. res.iep_event_id = event_id;
  2082. libais_send_response (conn_info, &res, sizeof(res));
  2083. return 0;
  2084. }
  2085. /*
  2086. * saEvtEventRetentionTimeClear handler
  2087. */
  2088. static int lib_evt_event_clear_retentiontime(struct conn_info *conn_info,
  2089. void *message)
  2090. {
  2091. struct req_evt_event_clear_retentiontime *req;
  2092. struct res_evt_event_clear_retentiontime res;
  2093. struct req_evt_chan_command cpkt;
  2094. struct iovec rtn_iovec;
  2095. SaErrorT error = SA_OK;
  2096. int ret;
  2097. req = message;
  2098. log_printf(LOG_LEVEL_DEBUG,
  2099. "saEvtEventRetentionTimeClear (Clear event retentiontime request)\n");
  2100. log_printf(LOG_LEVEL_DEBUG,
  2101. "event ID 0x%llx, chan handle 0x%x\n",
  2102. req->iec_event_id,
  2103. req->iec_channel_handle);
  2104. memset(&cpkt, 0, sizeof(cpkt));
  2105. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  2106. cpkt.chc_head.size = sizeof(cpkt);
  2107. cpkt.chc_op = EVT_CLEAR_RET_OP;
  2108. cpkt.u.chc_event_id = req->iec_event_id;
  2109. rtn_iovec.iov_base = &cpkt;
  2110. rtn_iovec.iov_len = cpkt.chc_head.size;
  2111. ret = gmi_mcast (&aisexec_groupname, &rtn_iovec, 1, GMI_PRIO_MED);
  2112. if (ret != 0) {
  2113. error = SA_ERR_SYSTEM;
  2114. }
  2115. res.iec_head.size = sizeof(res);
  2116. res.iec_head.id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME;
  2117. res.iec_head.error = error;
  2118. libais_send_response (conn_info, &res, sizeof(res));
  2119. return 0;
  2120. }
  2121. /*
  2122. * Send requested event data to the application
  2123. */
  2124. static int lib_evt_event_data_get(struct conn_info *conn_info, void *message)
  2125. {
  2126. struct lib_event_data res;
  2127. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2128. struct chan_event_list *cel;
  2129. struct event_data *edp;
  2130. int i;
  2131. /*
  2132. * Deliver events in publish order within priority
  2133. */
  2134. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  2135. if (!list_empty(&esip->esi_events[i])) {
  2136. cel = list_entry(esip->esi_events[i].next, struct chan_event_list,
  2137. cel_entry);
  2138. list_del(&cel->cel_entry);
  2139. list_init(&cel->cel_entry);
  2140. esip->esi_nevents--;
  2141. if (esip->esi_queue_blocked &&
  2142. (esip->esi_nevents < MIN_EVT_QUEUE_RESUME)) {
  2143. esip->esi_queue_blocked = 0;
  2144. log_printf(LOG_LEVEL_DEBUG, "unblock\n");
  2145. }
  2146. edp = cel->cel_event;
  2147. edp->ed_event.led_lib_channel_handle = cel->cel_chan_handle;
  2148. edp->ed_event.led_sub_id = cel->cel_sub_id;
  2149. edp->ed_event.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2150. edp->ed_event.led_head.error = SA_OK;
  2151. free(cel);
  2152. libais_send_response(conn_info, &edp->ed_event,
  2153. edp->ed_event.led_head.size);
  2154. free_event_data(edp);
  2155. goto data_get_done;
  2156. }
  2157. }
  2158. res.led_head.size = sizeof(res.led_head);
  2159. res.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2160. res.led_head.error = SA_ERR_NOT_EXIST;
  2161. libais_send_response(conn_info, &res, res.led_head.size);
  2162. /*
  2163. * See if there are any events that the app doesn't know about
  2164. * because the notify pipe was full.
  2165. */
  2166. data_get_done:
  2167. if (esip->esi_nevents) {
  2168. __notify_event(conn_info);
  2169. }
  2170. return 0;
  2171. }
  2172. /*
  2173. * Scan the list of channels and remove the specified node.
  2174. */
  2175. static void remove_chan_open_info(SaClmNodeIdT node_id)
  2176. {
  2177. struct list_head *l, *nxt;
  2178. struct event_svr_channel_instance *eci;
  2179. for (l = esc_head.next; l != &esc_head; l = nxt) {
  2180. nxt = l->next;
  2181. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  2182. remove_open_count(eci, node_id);
  2183. }
  2184. }
  2185. /*
  2186. * Called when there is a configuration change in the cluster.
  2187. * This function looks at any joiners and leavers and updates the evt
  2188. * node list. The node list is used to keep track of event IDs
  2189. * received for each node for the detection of duplicate events.
  2190. */
  2191. static int evt_conf_change(
  2192. enum gmi_configuration_type configuration_type,
  2193. struct sockaddr_in *member_list, int member_list_entries,
  2194. struct sockaddr_in *left_list, int left_list_entries,
  2195. struct sockaddr_in *joined_list, int joined_list_entries)
  2196. {
  2197. struct in_addr my_node = {SA_CLM_LOCAL_NODE_ID};
  2198. SaClmClusterNodeT *cn;
  2199. static int first = 1;
  2200. struct sockaddr_in *add_list;
  2201. struct member_node_data *md;
  2202. int add_count;
  2203. struct req_evt_chan_command cpkt;
  2204. struct iovec chn_iovec;
  2205. int res;
  2206. log_printf(LOG_LEVEL_DEBUG, "Evt conf change %d\n",
  2207. configuration_type);
  2208. log_printf(LOG_LEVEL_DEBUG, "m %d, j %d, l %d\n",
  2209. member_list_entries,
  2210. joined_list_entries,
  2211. left_list_entries);
  2212. /*
  2213. * Stop any recovery callbacks in progress.
  2214. */
  2215. if (tok_call_handle) {
  2216. gmi_token_callback_destroy(tok_call_handle);
  2217. tok_call_handle = 0;
  2218. }
  2219. /*
  2220. * Don't seem to be able to tell who joined if we're just coming up. Not all
  2221. * nodes show up in the join list. If this is the first time through,
  2222. * choose the members list to use to add nodes, after that use the join
  2223. * list. Always use the left list for removing nodes.
  2224. */
  2225. if (first) {
  2226. add_list = member_list;
  2227. add_count = member_list_entries;
  2228. first = 0;
  2229. } else {
  2230. add_list = joined_list;
  2231. add_count = joined_list_entries;
  2232. }
  2233. while (add_count--) {
  2234. /*
  2235. * If we've seen this node before, send out the last event ID
  2236. * that we've seen from him. He will set his base event ID to
  2237. * the highest one seen.
  2238. */
  2239. md = evt_find_node(add_list->sin_addr);
  2240. if (md != NULL) {
  2241. if (!md->mn_started) {
  2242. log_printf(RECOVERY_DEBUG,
  2243. "end set evt ID %llx to %s\n",
  2244. md->mn_last_evt_id, inet_ntoa(add_list->sin_addr));
  2245. md->mn_started = 1;
  2246. memset(&cpkt, 0, sizeof(cpkt));
  2247. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  2248. cpkt.chc_head.size = sizeof(cpkt);
  2249. cpkt.chc_op = EVT_SET_ID_OP;
  2250. cpkt.u.chc_set_id.chc_addr = add_list->sin_addr;
  2251. cpkt.u.chc_set_id.chc_last_id =
  2252. md->mn_last_evt_id & BASE_ID_MASK;
  2253. chn_iovec.iov_base = &cpkt;
  2254. chn_iovec.iov_len = cpkt.chc_head.size;
  2255. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  2256. GMI_PRIO_RECOVERY);
  2257. if (res != 0) {
  2258. log_printf(LOG_LEVEL_WARNING,
  2259. "Unable to send event id to %s\n",
  2260. inet_ntoa(add_list->sin_addr));
  2261. }
  2262. }
  2263. }
  2264. add_list++;
  2265. }
  2266. while (left_list_entries--) {
  2267. md = evt_find_node(left_list->sin_addr);
  2268. if (md == 0) {
  2269. log_printf(LOG_LEVEL_WARNING,
  2270. "Can't find cluster node at %s\n",
  2271. inet_ntoa(left_list->sin_addr));
  2272. /*
  2273. * Mark this one as down.
  2274. */
  2275. } else {
  2276. log_printf(RECOVERY_DEBUG, "cluster node at %s down\n",
  2277. inet_ntoa(left_list->sin_addr));
  2278. md->mn_started = 0;
  2279. remove_chan_open_info(md->mn_node_info.nodeId);
  2280. }
  2281. left_list++;
  2282. }
  2283. /*
  2284. * Set the base event id
  2285. */
  2286. cn = clm_get_by_nodeid(my_node);
  2287. if (!base_id_top) {
  2288. log_printf(RECOVERY_DEBUG, "My node ID 0x%x\n", cn->nodeId);
  2289. my_node_id = cn->nodeId;
  2290. set_event_id(my_node_id);
  2291. }
  2292. /*
  2293. * Notify that a config change happened. The exec handler will
  2294. * then determine what to do.
  2295. */
  2296. if (configuration_type == GMI_CONFIGURATION_REGULAR) {
  2297. if (in_cfg_change) {
  2298. log_printf(LOG_LEVEL_NOTICE,
  2299. "Already in config change, Starting over, m %d, c %d\n",
  2300. total_members, checked_in);
  2301. }
  2302. in_cfg_change = 1;
  2303. total_members = member_list_entries;
  2304. checked_in = 0;
  2305. any_joined = joined_list_entries;
  2306. /*
  2307. * Start by updating all the nodes on our
  2308. * open channel count. Once that is done, proceed to determining who
  2309. * sends ratained events. Then we can start normal operation again.
  2310. */
  2311. send_open_count();
  2312. }
  2313. return 0;
  2314. }
  2315. /*
  2316. * saEvtFinalize Handler
  2317. */
  2318. static int evt_finalize(struct conn_info *conn_info)
  2319. {
  2320. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2321. struct event_svr_channel_open *eco;
  2322. struct list_head *l, *nxt;
  2323. log_printf(LOG_LEVEL_DEBUG, "saEvtFinalize (Event exit request)\n");
  2324. log_printf(LOG_LEVEL_DEBUG, "saEvtFinalize %d evts on list\n",
  2325. esip->esi_nevents);
  2326. /*
  2327. * Clean up any open channels and associated subscriptions.
  2328. */
  2329. for (l = esip->esi_open_chans.next; l != &esip->esi_open_chans; l = nxt) {
  2330. nxt = l->next;
  2331. eco = list_entry(l, struct event_svr_channel_open, eco_instance_entry);
  2332. common_chan_close(eco, esip);
  2333. saHandleDestroy(&esip->esi_hdb, eco->eco_my_handle);
  2334. }
  2335. /*
  2336. * Delete track entry if there is one
  2337. */
  2338. list_del (&conn_info->conn_list);
  2339. return 0;
  2340. }
  2341. /*
  2342. * Called at service start time.
  2343. */
  2344. static int evt_exec_init(void)
  2345. {
  2346. int res;
  2347. log_printf(LOG_LEVEL_DEBUG, "Evt exec init request\n");
  2348. res = gmi_recovery_plug_create (&evt_recovery_plug_handle);
  2349. if (res != 0) {
  2350. log_printf(LOG_LEVEL_ERROR,
  2351. "Could not create recovery plug for event service.\n");
  2352. return (-1);
  2353. }
  2354. /*
  2355. * Create an event to be sent when we have to drop messages
  2356. * for an application.
  2357. */
  2358. dropped_event_size = sizeof(*dropped_event) + sizeof(dropped_pattern);
  2359. dropped_event = malloc(dropped_event_size);
  2360. if (dropped_event == 0) {
  2361. log_printf(LOG_LEVEL_ERROR,
  2362. "Memory Allocation Failure, event service not started\n");
  2363. res = gmi_recovery_plug_destroy (evt_recovery_plug_handle);
  2364. errno = ENOMEM;
  2365. return -1;
  2366. }
  2367. memset(dropped_event, 0, sizeof(*dropped_event) + sizeof(dropped_pattern));
  2368. dropped_event->ed_ref_count = 1;
  2369. list_init(&dropped_event->ed_retained);
  2370. dropped_event->ed_event.led_head.size =
  2371. sizeof(*dropped_event) + sizeof(dropped_pattern);
  2372. dropped_event->ed_event.led_head.error = SA_OK;
  2373. dropped_event->ed_event.led_priority = SA_EVT_HIGHEST_PRIORITY;
  2374. dropped_event->ed_event.led_chan_name = lost_chan;
  2375. dropped_event->ed_event.led_publisher_name = dropped_publisher;
  2376. dropped_event->ed_event.led_patterns_number = 1;
  2377. memcpy(&dropped_event->ed_event.led_body[0],
  2378. &dropped_pattern, sizeof(dropped_pattern));
  2379. return 0;
  2380. }
  2381. /*
  2382. * Receive the network event message and distribute it to local subscribers
  2383. */
  2384. static int evt_remote_evt(void *msg, struct in_addr source_addr)
  2385. {
  2386. /*
  2387. * - retain events that have a retention time
  2388. * - Find assocated channel
  2389. * - Scan list of subscribers
  2390. * - Apply filters
  2391. * - Deliver events that pass the filter test
  2392. */
  2393. struct lib_event_data *evtpkt = msg;
  2394. struct event_svr_channel_instance *eci;
  2395. struct event_svr_channel_open *eco;
  2396. struct event_svr_channel_subscr *ecs;
  2397. struct event_data *evt;
  2398. struct list_head *l, *l1;
  2399. SaClmClusterNodeT *cn;
  2400. log_printf(LOG_LEVEL_DEBUG, "Remote event data received from %s\n",
  2401. inet_ntoa(source_addr));
  2402. /*
  2403. * See where the message came from so that we can set the
  2404. * publishing node id in the message before delivery.
  2405. */
  2406. cn = clm_get_by_nodeid (source_addr);
  2407. if (!cn) {
  2408. /*
  2409. * Not sure how this can happen...
  2410. */
  2411. log_printf(LOG_LEVEL_NOTICE, "No cluster node data for %s\n",
  2412. inet_ntoa(source_addr));
  2413. errno = ENXIO;
  2414. return -1;
  2415. }
  2416. log_printf(LOG_LEVEL_DEBUG, "Cluster node ID 0x%x name %s\n",
  2417. cn->nodeId, cn->nodeName.value);
  2418. evtpkt->led_publisher_node_id = cn->nodeId;
  2419. evtpkt->led_in_addr = source_addr;
  2420. evtpkt->led_receive_time = clust_time_now();
  2421. eci = find_channel(&evtpkt->led_chan_name);
  2422. /*
  2423. * We shouldn't see an event for a channel that we don't know about.
  2424. */
  2425. if (!eci) {
  2426. log_printf(LOG_LEVEL_WARNING, "Channel %s doesn't exist\n",
  2427. evtpkt->led_chan_name.value);
  2428. return 0;
  2429. }
  2430. if (check_last_event(evtpkt, source_addr)) {
  2431. return 0;
  2432. }
  2433. evt = make_local_event(evtpkt, eci);
  2434. if (!evt) {
  2435. log_printf(LOG_LEVEL_WARNING,
  2436. "Memory allocation error, can't deliver event\n");
  2437. errno = ENOMEM;
  2438. return -1;
  2439. }
  2440. if (evt->ed_event.led_retention_time) {
  2441. retain_event(evt);
  2442. }
  2443. /*
  2444. * Check open channels
  2445. */
  2446. for (l = eci->esc_open_chans.next; l != &eci->esc_open_chans; l = l->next) {
  2447. eco = list_entry(l, struct event_svr_channel_open, eco_entry);
  2448. /*
  2449. * See if enabled to receive
  2450. */
  2451. if (!(eco->eco_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  2452. continue;
  2453. }
  2454. /*
  2455. * Check subscriptions
  2456. */
  2457. for (l1 = eco->eco_subscr.next; l1 != &eco->eco_subscr; l1 = l1->next) {
  2458. ecs = list_entry(l1, struct event_svr_channel_subscr, ecs_entry);
  2459. /*
  2460. * Apply filter rules and deliver if patterns
  2461. * match filters.
  2462. * Only deliver one event per open channel
  2463. */
  2464. if (event_match(evt, ecs) == SA_OK) {
  2465. deliver_event(evt, eco, ecs);
  2466. break;
  2467. }
  2468. }
  2469. }
  2470. free_event_data(evt);
  2471. return 0;
  2472. }
  2473. /*
  2474. * Calculate the remaining retention time of a received event during recovery
  2475. */
  2476. inline SaTimeT calc_retention_time(SaTimeT retention,
  2477. SaTimeT received, SaTimeT now)
  2478. {
  2479. if ((received < now) && ((now - received) < retention)) {
  2480. return retention - (now - received);
  2481. } else {
  2482. return 0;
  2483. }
  2484. }
  2485. /*
  2486. * Receive a recovery network event message and save it in the retained list
  2487. */
  2488. static int evt_remote_recovery_evt(void *msg, struct in_addr source_addr)
  2489. {
  2490. /*
  2491. * - retain events that have a retention time
  2492. * - Find assocated channel
  2493. */
  2494. struct lib_event_data *evtpkt = msg;
  2495. struct event_svr_channel_instance *eci;
  2496. struct event_data *evt;
  2497. struct member_node_data *md;
  2498. SaTimeT now;
  2499. now = clust_time_now();
  2500. log_printf(LOG_LEVEL_DEBUG,
  2501. "Remote recovery event data received from %s\n",
  2502. inet_ntoa(source_addr));
  2503. if (!in_cfg_change) {
  2504. log_printf(LOG_LEVEL_NOTICE,
  2505. "Received recovery data, not in recovery mode\n");
  2506. return 0;
  2507. }
  2508. log_printf(LOG_LEVEL_DEBUG,
  2509. "Processing recovery of retained events\n");
  2510. if (recovery_node) {
  2511. log_printf(LOG_LEVEL_DEBUG, "This node is the recovery node\n");
  2512. }
  2513. log_printf(LOG_LEVEL_DEBUG, "(1)EVT ID: %llx, Time: %llx\n",
  2514. evtpkt->led_event_id, evtpkt->led_retention_time);
  2515. /*
  2516. * Calculate remaining retention time
  2517. */
  2518. evtpkt->led_retention_time = calc_retention_time(
  2519. evtpkt->led_retention_time,
  2520. evtpkt->led_receive_time,
  2521. now);
  2522. log_printf(LOG_LEVEL_DEBUG,
  2523. "(2)EVT ID: %llx, ret: %llx, rec: %llx, now: %llx\n",
  2524. evtpkt->led_event_id,
  2525. evtpkt->led_retention_time, evtpkt->led_receive_time, now);
  2526. /*
  2527. * If we haven't seen this event yet and it has remaining time, process
  2528. * the event.
  2529. */
  2530. if (!check_last_event(evtpkt, evtpkt->led_in_addr) &&
  2531. evtpkt->led_retention_time) {
  2532. /*
  2533. * See where the message came from so that we can set the
  2534. * publishing node id in the message before delivery.
  2535. */
  2536. md = evt_find_node(evtpkt->led_in_addr);
  2537. if (!md) {
  2538. /*
  2539. * Not sure how this can happen
  2540. */
  2541. log_printf(LOG_LEVEL_NOTICE, "No node for %s\n",
  2542. inet_ntoa(evtpkt->led_in_addr));
  2543. errno = ENXIO;
  2544. return -1;
  2545. }
  2546. log_printf(LOG_LEVEL_DEBUG, "Cluster node ID 0x%x name %s\n",
  2547. md->mn_node_info.nodeId,
  2548. md->mn_node_info.nodeName.value);
  2549. eci = find_channel(&evtpkt->led_chan_name);
  2550. /*
  2551. * We shouldn't see an event for a channel that we don't know about.
  2552. */
  2553. if (!eci) {
  2554. log_printf(LOG_LEVEL_WARNING, "Channel %s doesn't exist\n",
  2555. evtpkt->led_chan_name.value);
  2556. return 0;
  2557. }
  2558. evt = make_local_event(evtpkt, eci);
  2559. if (!evt) {
  2560. log_printf(LOG_LEVEL_WARNING,
  2561. "Memory allocation error, can't deliver event\n");
  2562. errno = ENOMEM;
  2563. return -1;
  2564. }
  2565. retain_event(evt);
  2566. free_event_data(evt);
  2567. }
  2568. return 0;
  2569. }
  2570. /*
  2571. * Timeout handler for event channel open.
  2572. */
  2573. static void chan_open_timeout(void *data)
  2574. {
  2575. struct open_chan_pending *ocp = (struct open_chan_pending *)data;
  2576. struct res_evt_channel_open res;
  2577. res.ico_head.size = sizeof(res);
  2578. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2579. res.ico_head.error = SA_ERR_TIMEOUT;
  2580. libais_send_response (ocp->ocp_conn_info, &res, sizeof(res));
  2581. list_del(&ocp->ocp_entry);
  2582. free(ocp);
  2583. }
  2584. /*
  2585. * Called by the channel open exec handler to finish the open and
  2586. * respond to the application
  2587. */
  2588. static void evt_chan_open_finish(struct open_chan_pending *ocp,
  2589. struct event_svr_channel_instance *eci)
  2590. {
  2591. uint32_t handle;
  2592. struct res_evt_channel_open res;
  2593. struct event_svr_channel_open *eco;
  2594. SaErrorT error;
  2595. struct libevt_ci *esip = &ocp->ocp_conn_info->ais_ci.u.libevt_ci;
  2596. int ret = 0;
  2597. void *ptr;
  2598. if (!ocp->ocp_async && ocp->ocp_timer_handle) {
  2599. ret = poll_timer_delete(aisexec_poll_handle, ocp->ocp_timer_handle);
  2600. if (ret != 0 ) {
  2601. log_printf(LOG_LEVEL_WARNING,
  2602. "Error clearing timeout for open channel of %s\n",
  2603. getSaNameT(&ocp->ocp_chan_name));
  2604. }
  2605. }
  2606. /*
  2607. * Create a handle to give back to the caller to associate
  2608. * with this channel open instance.
  2609. */
  2610. error = saHandleCreate(&esip->esi_hdb, sizeof(*eco), &handle);
  2611. if (error != SA_OK) {
  2612. goto open_return;
  2613. }
  2614. error = saHandleInstanceGet(&esip->esi_hdb, handle, &ptr);
  2615. if (error != SA_OK) {
  2616. goto open_return;
  2617. }
  2618. eco = ptr;
  2619. /*
  2620. * Initailize and link into the global channel structure.
  2621. */
  2622. list_init(&eco->eco_subscr);
  2623. list_init(&eco->eco_entry);
  2624. list_init(&eco->eco_instance_entry);
  2625. eco->eco_flags = ocp->ocp_open_flag;
  2626. eco->eco_channel = eci;
  2627. eco->eco_lib_handle = ocp->ocp_c_handle;
  2628. eco->eco_my_handle = handle;
  2629. eco->eco_conn_info = ocp->ocp_conn_info;
  2630. list_add_tail(&eco->eco_entry, &eci->esc_open_chans);
  2631. list_add_tail(&eco->eco_instance_entry, &esip->esi_open_chans);
  2632. /*
  2633. * respond back with a handle to access this channel
  2634. * open instance for later subscriptions, etc.
  2635. */
  2636. saHandleInstancePut(&esip->esi_hdb, handle);
  2637. open_return:
  2638. res.ico_head.size = sizeof(res);
  2639. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2640. res.ico_head.error = error;
  2641. res.ico_channel_handle = handle;
  2642. libais_send_response (ocp->ocp_conn_info, &res, sizeof(res));
  2643. if (ret == 0) {
  2644. list_del(&ocp->ocp_entry);
  2645. free(ocp);
  2646. }
  2647. }
  2648. /*
  2649. * Receive and process remote event operations.
  2650. * Used to communicate channel opens/closes, clear retention time,
  2651. * config change updates...
  2652. */
  2653. static int evt_remote_chan_op(void *msg, struct in_addr source_addr)
  2654. {
  2655. struct req_evt_chan_command *cpkt = msg;
  2656. struct in_addr local_node = {SA_CLM_LOCAL_NODE_ID};
  2657. SaClmClusterNodeT *cn, *my_node;
  2658. struct member_node_data *mn;
  2659. struct event_svr_channel_instance *eci;
  2660. log_printf(LOG_LEVEL_DEBUG, "Remote channel operation request\n");
  2661. my_node = clm_get_by_nodeid(local_node);
  2662. mn = evt_find_node(source_addr);
  2663. if (mn == NULL) {
  2664. cn = clm_get_by_nodeid(source_addr);
  2665. if (cn == NULL) {
  2666. log_printf(LOG_LEVEL_WARNING,
  2667. "Evt remote channel op: Node data for addr %s is NULL\n",
  2668. inet_ntoa(source_addr));
  2669. } else {
  2670. evt_add_node(source_addr, cn);
  2671. mn = evt_find_node(source_addr);
  2672. }
  2673. }
  2674. switch (cpkt->chc_op) {
  2675. /*
  2676. * Open channel remote command. The open channel request is done
  2677. * in two steps. When an pplication tries to open, we send an open
  2678. * channel message to the other nodes. When we receive an open channel
  2679. * message, we may create the channel structure if it doesn't exist
  2680. * and also complete applicaiton open requests for the specified
  2681. * channel. We keep a counter of total opens for the given channel and
  2682. * later when it has been completely closed (everywhere in the cluster)
  2683. * we will free up the assocated channel data.
  2684. */
  2685. case EVT_OPEN_CHAN_OP: {
  2686. struct open_chan_pending *ocp;
  2687. struct list_head *l, *nxt;
  2688. log_printf(LOG_LEVEL_DEBUG, "Opening channel %s for node 0x%x\n",
  2689. cpkt->u.chc_chan.value, mn->mn_node_info.nodeId);
  2690. eci = find_channel(&cpkt->u.chc_chan);
  2691. if (!eci) {
  2692. eci = create_channel(&cpkt->u.chc_chan);
  2693. }
  2694. if (!eci) {
  2695. log_printf(LOG_LEVEL_WARNING, "Could not create channel %s\n",
  2696. getSaNameT(&cpkt->u.chc_chan));
  2697. break;
  2698. }
  2699. inc_open_count(eci, mn->mn_node_info.nodeId);
  2700. if (mn->mn_node_info.nodeId == my_node->nodeId) {
  2701. /*
  2702. * Complete one of our pending open requests
  2703. */
  2704. for (l = open_pending.next; l != &open_pending; l = nxt) {
  2705. nxt = l->next;
  2706. ocp = list_entry(l, struct open_chan_pending, ocp_entry);
  2707. if (name_match(&ocp->ocp_chan_name, &eci->esc_channel_name)) {
  2708. evt_chan_open_finish(ocp, eci);
  2709. break;
  2710. }
  2711. }
  2712. }
  2713. log_printf(LOG_LEVEL_DEBUG,
  2714. "Open channel %s t %d, l %d, r %d\n",
  2715. eci->esc_channel_name.value,
  2716. eci->esc_total_opens, eci->esc_local_opens,
  2717. eci->esc_retained_count);
  2718. break;
  2719. }
  2720. /*
  2721. * Handle a channel close. We'll decrement the global open counter and
  2722. * free up channel associated data when all instances are closed.
  2723. */
  2724. case EVT_CLOSE_CHAN_OP:
  2725. log_printf(LOG_LEVEL_DEBUG, "Closing channel %s for node 0x%x\n",
  2726. cpkt->u.chc_chan.value, mn->mn_node_info.nodeId);
  2727. eci = find_channel(&cpkt->u.chc_chan);
  2728. if (!eci) {
  2729. log_printf(LOG_LEVEL_NOTICE,
  2730. "Channel close request for %s not found\n",
  2731. cpkt->u.chc_chan.value);
  2732. break;
  2733. }
  2734. /*
  2735. * if last instance, we can free up assocated data.
  2736. */
  2737. dec_open_count(eci, mn->mn_node_info.nodeId);
  2738. log_printf(LOG_LEVEL_DEBUG,
  2739. "Close channel %s t %d, l %d, r %d\n",
  2740. eci->esc_channel_name.value,
  2741. eci->esc_total_opens, eci->esc_local_opens,
  2742. eci->esc_retained_count);
  2743. delete_channel(eci);
  2744. break;
  2745. /*
  2746. * saEvtClearRetentiotime handler.
  2747. */
  2748. case EVT_CLEAR_RET_OP:
  2749. log_printf(LOG_LEVEL_DEBUG, "Clear retention time request %llx\n",
  2750. cpkt->u.chc_event_id);
  2751. clear_retention_time(cpkt->u.chc_event_id);
  2752. break;
  2753. /*
  2754. * Set our next event ID based on the largest event ID seen
  2755. * by others in the cluster. This way, if we've left and re-joined, we'll
  2756. * start using an event ID that is unique.
  2757. */
  2758. case EVT_SET_ID_OP: {
  2759. struct in_addr my_addr;
  2760. my_addr.s_addr = my_node->nodeId;
  2761. log_printf(RECOVERY_DEBUG,
  2762. "Received Set event ID OP from %x to %llx for %x my addr %x base %llx\n",
  2763. inet_ntoa(source_addr),
  2764. cpkt->u.chc_set_id.chc_last_id,
  2765. cpkt->u.chc_set_id.chc_addr.s_addr,
  2766. my_addr.s_addr,
  2767. base_id);
  2768. if (cpkt->u.chc_set_id.chc_addr.s_addr == my_addr.s_addr) {
  2769. if (cpkt->u.chc_set_id.chc_last_id > base_id) {
  2770. log_printf(RECOVERY_DEBUG,
  2771. "Set event ID from %s to %llx\n",
  2772. inet_ntoa(source_addr), cpkt->u.chc_set_id.chc_last_id);
  2773. base_id = cpkt->u.chc_set_id.chc_last_id + 1;
  2774. }
  2775. }
  2776. break;
  2777. }
  2778. /*
  2779. * Receive the open count for a particular channel during recovery.
  2780. * This insures that everyone has the same notion of who has a channel
  2781. * open so that it can be removed when no one else has it open anymore.
  2782. */
  2783. case EVT_OPEN_COUNT:
  2784. if (!in_cfg_change) {
  2785. log_printf(LOG_LEVEL_ERROR,
  2786. "Evt open count msg from %s, but not in membership change\n",
  2787. inet_ntoa(source_addr));
  2788. }
  2789. log_printf(RECOVERY_DEBUG,
  2790. "Open channel count %s is %d for node 0x%x\n",
  2791. cpkt->u.chc_set_opens.chc_chan_name.value,
  2792. cpkt->u.chc_set_opens.chc_open_count,
  2793. mn->mn_node_info.nodeId);
  2794. eci = find_channel(&cpkt->u.chc_set_opens.chc_chan_name);
  2795. if (!eci) {
  2796. eci = create_channel(&cpkt->u.chc_set_opens.chc_chan_name);
  2797. }
  2798. if (!eci) {
  2799. log_printf(LOG_LEVEL_WARNING, "Could not create channel %s\n",
  2800. getSaNameT(&cpkt->u.chc_set_opens.chc_chan_name));
  2801. break;
  2802. }
  2803. if (set_open_count(eci, mn->mn_node_info.nodeId,
  2804. cpkt->u.chc_set_opens.chc_open_count)) {
  2805. log_printf(LOG_LEVEL_ERROR,
  2806. "Error setting Open channel count %s for node 0x%x\n",
  2807. cpkt->u.chc_set_opens.chc_chan_name.value,
  2808. mn->mn_node_info.nodeId);
  2809. }
  2810. break;
  2811. /*
  2812. * Once we get all the messages from
  2813. * the current membership, determine who delivers any retained events.
  2814. */
  2815. case EVT_OPEN_COUNT_DONE: {
  2816. if (!in_cfg_change) {
  2817. log_printf(LOG_LEVEL_ERROR,
  2818. "Evt config msg from %s, but not in membership change\n",
  2819. inet_ntoa(source_addr));
  2820. }
  2821. log_printf(RECOVERY_DEBUG,
  2822. "Receive EVT_CONF_CHANGE_DONE from %s members %d checked in %d\n",
  2823. inet_ntoa(source_addr), total_members, checked_in+1);
  2824. if (!mn) {
  2825. log_printf(RECOVERY_DEBUG,
  2826. "NO NODE DATA AVAILABLE FOR %s\n",
  2827. inet_ntoa(source_addr));
  2828. }
  2829. if (++checked_in == total_members) {
  2830. /*
  2831. * We're all here, now figure out who should send the
  2832. * retained events, if any.
  2833. */
  2834. mn = oldest_node();
  2835. if (mn->mn_node_info.nodeId == my_node_id) {
  2836. log_printf(RECOVERY_DEBUG, "I am oldest\n");
  2837. send_retained();
  2838. }
  2839. }
  2840. break;
  2841. }
  2842. /*
  2843. * OK, We're done with recovery, continue operations.
  2844. */
  2845. case EVT_CONF_DONE: {
  2846. log_printf(RECOVERY_DEBUG,
  2847. "Receive EVT_CONF_DONE from %s\n",
  2848. inet_ntoa(source_addr));
  2849. in_cfg_change = 0;
  2850. gmi_recovery_plug_unplug (evt_recovery_plug_handle);
  2851. #ifdef DUMP_CHAN_INFO
  2852. dump_all_chans();
  2853. #endif
  2854. break;
  2855. }
  2856. default:
  2857. log_printf(LOG_LEVEL_NOTICE, "Invalid channel operation %d\n",
  2858. cpkt->chc_op);
  2859. break;
  2860. }
  2861. return 0;
  2862. }