evt.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207
  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. req = message;
  1832. log_printf(LOG_LEVEL_DEBUG,
  1833. "saEvtChannelClose (Close channel request)\n");
  1834. log_printf(LOG_LEVEL_DEBUG, "handle 0x%x\n", req->icc_channel_handle);
  1835. /*
  1836. * look up the channel handle
  1837. */
  1838. error = saHandleInstanceGet(&esip->esi_hdb,
  1839. req->icc_channel_handle, (void**)&eco);
  1840. if (error != SA_OK) {
  1841. goto chan_close_done;
  1842. }
  1843. common_chan_close(eco, esip);
  1844. saHandleDestroy(&esip->esi_hdb, req->icc_channel_handle);
  1845. saHandleInstancePut(&esip->esi_hdb, req->icc_channel_handle);
  1846. chan_close_done:
  1847. res.icc_head.size = sizeof(res);
  1848. res.icc_head.id = MESSAGE_RES_EVT_CLOSE_CHANNEL;
  1849. res.icc_head.error = error;
  1850. libais_send_response (conn_info, &res, sizeof(res));
  1851. return 0;
  1852. }
  1853. /*
  1854. * Subscribe to an event channel.
  1855. *
  1856. * - First look up the channel to subscribe.
  1857. * - Make sure that the subscription ID is not already in use.
  1858. * - Fill in the subscription data structures and add them to the channels
  1859. * subscription list.
  1860. * - See if there are any events with retetion times that need to be delivered
  1861. * because of the new subscription.
  1862. */
  1863. static char *filter_types[] = {
  1864. "INVALID FILTER TYPE",
  1865. "SA_EVT_PREFIX_FILTER",
  1866. "SA_EVT_SUFFIX_FILTER",
  1867. "SA_EVT_EXACT_FILTER",
  1868. "SA_EVT_PASS_ALL_FILTER",
  1869. };
  1870. /*
  1871. * saEvtEventSubscribe Handler
  1872. */
  1873. static int lib_evt_event_subscribe(struct conn_info *conn_info, void *message)
  1874. {
  1875. struct req_evt_event_subscribe *req;
  1876. struct res_evt_event_subscribe res;
  1877. SaEvtEventFilterArrayT *filters;
  1878. SaErrorT error = SA_OK;
  1879. struct event_svr_channel_open *eco;
  1880. struct event_svr_channel_instance *eci;
  1881. struct event_svr_channel_subscr *ecs;
  1882. struct event_data *evt;
  1883. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1884. struct list_head *l;
  1885. int i;
  1886. req = message;
  1887. log_printf(LOG_LEVEL_DEBUG,
  1888. "saEvtEventSubscribe (Subscribe request)\n");
  1889. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%x\n",
  1890. req->ics_sub_id);
  1891. error = evtfilt_to_aisfilt(req, &filters);
  1892. if (error == SA_OK) {
  1893. log_printf(LOG_LEVEL_DEBUG, "Subscribe filters count %d\n",
  1894. filters->filtersNumber);
  1895. for (i = 0; i < filters->filtersNumber; i++) {
  1896. log_printf(LOG_LEVEL_DEBUG, "type %s(%d) sz %d, <%s>\n",
  1897. filter_types[filters->filters[i].filterType],
  1898. filters->filters[i].filterType,
  1899. filters->filters[i].filter.patternSize,
  1900. (filters->filters[i].filter.patternSize)
  1901. ? (char *)filters->filters[i].filter.pattern
  1902. : "");
  1903. }
  1904. }
  1905. if (error != SA_OK) {
  1906. goto subr_done;
  1907. }
  1908. /*
  1909. * look up the channel handle
  1910. */
  1911. error = saHandleInstanceGet(&esip->esi_hdb,
  1912. req->ics_channel_handle, (void**)&eco);
  1913. if (error != SA_OK) {
  1914. goto subr_done;
  1915. }
  1916. eci = eco->eco_channel;
  1917. /*
  1918. * See if the id is already being used
  1919. */
  1920. ecs = find_subscr(eco, req->ics_sub_id);
  1921. if (ecs) {
  1922. error = SA_ERR_EXIST;
  1923. goto subr_put;
  1924. }
  1925. ecs = (struct event_svr_channel_subscr *)malloc(sizeof(*ecs));
  1926. if (!ecs) {
  1927. error = SA_ERR_NO_MEMORY;
  1928. goto subr_put;
  1929. }
  1930. ecs->ecs_filters = filters;
  1931. ecs->ecs_sub_id = req->ics_sub_id;
  1932. list_init(&ecs->ecs_entry);
  1933. list_add(&ecs->ecs_entry, &eco->eco_subscr);
  1934. res.ics_head.size = sizeof(res);
  1935. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  1936. res.ics_head.error = error;
  1937. libais_send_response (conn_info, &res, sizeof(res));
  1938. /*
  1939. * See if an existing event with a retention time
  1940. * needs to be delivered based on this subscription
  1941. */
  1942. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1943. evt = list_entry(l, struct event_data, ed_retained);
  1944. log_printf(LOG_LEVEL_DEBUG,
  1945. "Checking event ID %llx chanp %p -- sub chanp %p\n",
  1946. evt->ed_event.led_event_id, evt->ed_my_chan, eci);
  1947. if (evt->ed_my_chan == eci) {
  1948. if (evt_already_delivered(evt, eco)) {
  1949. continue;
  1950. }
  1951. if (event_match(evt, ecs) == SA_OK) {
  1952. log_printf(LOG_LEVEL_DEBUG,
  1953. "deliver event ID: 0x%llx\n",
  1954. evt->ed_event.led_event_id);
  1955. deliver_event(evt, eco, ecs);
  1956. }
  1957. }
  1958. }
  1959. saHandleInstancePut(&esip->esi_hdb, req->ics_channel_handle);
  1960. return 0;
  1961. subr_put:
  1962. saHandleInstancePut(&esip->esi_hdb, req->ics_channel_handle);
  1963. subr_done:
  1964. res.ics_head.size = sizeof(res);
  1965. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  1966. res.ics_head.error = error;
  1967. libais_send_response (conn_info, &res, sizeof(res));
  1968. return 0;
  1969. }
  1970. /*
  1971. * saEvtEventUnsubscribe Handler
  1972. */
  1973. static int lib_evt_event_unsubscribe(struct conn_info *conn_info,
  1974. void *message)
  1975. {
  1976. struct req_evt_event_unsubscribe *req;
  1977. struct res_evt_event_unsubscribe res;
  1978. struct event_svr_channel_open *eco;
  1979. struct event_svr_channel_instance *eci;
  1980. struct event_svr_channel_subscr *ecs;
  1981. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1982. SaErrorT error = SA_OK;
  1983. req = message;
  1984. log_printf(LOG_LEVEL_DEBUG,
  1985. "saEvtEventUnsubscribe (Unsubscribe request)\n");
  1986. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%x\n",
  1987. req->icu_sub_id);
  1988. /*
  1989. * look up the channel handle, get the open channel
  1990. * data.
  1991. */
  1992. error = saHandleInstanceGet(&esip->esi_hdb,
  1993. req->icu_channel_handle, (void**)&eco);
  1994. if (error != SA_OK) {
  1995. goto unsubr_done;
  1996. }
  1997. eci = eco->eco_channel;
  1998. /*
  1999. * Make sure that the id exists.
  2000. */
  2001. ecs = find_subscr(eco, req->icu_sub_id);
  2002. if (!ecs) {
  2003. error = SA_ERR_INVALID_PARAM;
  2004. goto unsubr_put;
  2005. }
  2006. list_del(&ecs->ecs_entry);
  2007. log_printf(LOG_LEVEL_DEBUG,
  2008. "unsubscribe from channel %s subscription ID 0x%x "
  2009. "with %d filters\n",
  2010. eci->esc_channel_name.value,
  2011. ecs->ecs_sub_id, ecs->ecs_filters->filtersNumber);
  2012. free_filters(ecs->ecs_filters);
  2013. free(ecs);
  2014. unsubr_put:
  2015. saHandleInstancePut(&esip->esi_hdb, req->icu_channel_handle);
  2016. unsubr_done:
  2017. res.icu_head.size = sizeof(res);
  2018. res.icu_head.id = MESSAGE_RES_EVT_UNSUBSCRIBE;
  2019. res.icu_head.error = error;
  2020. libais_send_response (conn_info, &res, sizeof(res));
  2021. return 0;
  2022. }
  2023. /*
  2024. * saEvtEventPublish Handler
  2025. */
  2026. static int lib_evt_event_publish(struct conn_info *conn_info, void *message)
  2027. {
  2028. struct lib_event_data *req;
  2029. struct res_evt_event_publish res;
  2030. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2031. struct event_svr_channel_open *eco;
  2032. struct event_svr_channel_instance *eci;
  2033. SaEvtEventIdT event_id = 0;
  2034. SaErrorT error = SA_OK;
  2035. struct iovec pub_iovec;
  2036. int result;
  2037. req = message;
  2038. log_printf(LOG_LEVEL_DEBUG,
  2039. "saEvtEventPublish (Publish event request)\n");
  2040. /*
  2041. * look up and validate open channel info
  2042. */
  2043. error = saHandleInstanceGet(&esip->esi_hdb,
  2044. req->led_svr_channel_handle, (void**)&eco);
  2045. if (error != SA_OK) {
  2046. goto pub_done;
  2047. }
  2048. eci = eco->eco_channel;
  2049. /*
  2050. * modify the request structure for sending event data to subscribed
  2051. * processes.
  2052. */
  2053. get_event_id(&event_id);
  2054. req->led_head.id = MESSAGE_REQ_EXEC_EVT_EVENTDATA;
  2055. req->led_chan_name = eci->esc_channel_name;
  2056. req->led_event_id = event_id;
  2057. /*
  2058. * Distribute the event.
  2059. * The multicasted event will be picked up and delivered
  2060. * locally by the local network event receiver.
  2061. */
  2062. pub_iovec.iov_base = req;
  2063. pub_iovec.iov_len = req->led_head.size;
  2064. result = gmi_mcast (&aisexec_groupname, &pub_iovec, 1, GMI_PRIO_LOW);
  2065. if (result != 0) {
  2066. error = SA_ERR_SYSTEM;
  2067. }
  2068. saHandleInstancePut(&esip->esi_hdb, req->led_svr_channel_handle);
  2069. pub_done:
  2070. res.iep_head.size = sizeof(res);
  2071. res.iep_head.id = MESSAGE_RES_EVT_PUBLISH;
  2072. res.iep_head.error = error;
  2073. res.iep_event_id = event_id;
  2074. libais_send_response (conn_info, &res, sizeof(res));
  2075. return 0;
  2076. }
  2077. /*
  2078. * saEvtEventRetentionTimeClear handler
  2079. */
  2080. static int lib_evt_event_clear_retentiontime(struct conn_info *conn_info,
  2081. void *message)
  2082. {
  2083. struct req_evt_event_clear_retentiontime *req;
  2084. struct res_evt_event_clear_retentiontime res;
  2085. struct req_evt_chan_command cpkt;
  2086. struct iovec rtn_iovec;
  2087. SaErrorT error = SA_OK;
  2088. int ret;
  2089. req = message;
  2090. log_printf(LOG_LEVEL_DEBUG,
  2091. "saEvtEventRetentionTimeClear (Clear event retentiontime request)\n");
  2092. log_printf(LOG_LEVEL_DEBUG,
  2093. "event ID 0x%llx, chan handle 0x%x\n",
  2094. req->iec_event_id,
  2095. req->iec_channel_handle);
  2096. memset(&cpkt, 0, sizeof(cpkt));
  2097. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  2098. cpkt.chc_head.size = sizeof(cpkt);
  2099. cpkt.chc_op = EVT_CLEAR_RET_OP;
  2100. cpkt.u.chc_event_id = req->iec_event_id;
  2101. rtn_iovec.iov_base = &cpkt;
  2102. rtn_iovec.iov_len = cpkt.chc_head.size;
  2103. ret = gmi_mcast (&aisexec_groupname, &rtn_iovec, 1, GMI_PRIO_MED);
  2104. if (ret != 0) {
  2105. error = SA_ERR_SYSTEM;
  2106. }
  2107. res.iec_head.size = sizeof(res);
  2108. res.iec_head.id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME;
  2109. res.iec_head.error = error;
  2110. libais_send_response (conn_info, &res, sizeof(res));
  2111. return 0;
  2112. }
  2113. /*
  2114. * Send requested event data to the application
  2115. */
  2116. static int lib_evt_event_data_get(struct conn_info *conn_info, void *message)
  2117. {
  2118. struct lib_event_data res;
  2119. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2120. struct chan_event_list *cel;
  2121. struct event_data *edp;
  2122. int i;
  2123. /*
  2124. * Deliver events in publish order within priority
  2125. */
  2126. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  2127. if (!list_empty(&esip->esi_events[i])) {
  2128. cel = list_entry(esip->esi_events[i].next, struct chan_event_list,
  2129. cel_entry);
  2130. list_del(&cel->cel_entry);
  2131. list_init(&cel->cel_entry);
  2132. esip->esi_nevents--;
  2133. if (esip->esi_queue_blocked &&
  2134. (esip->esi_nevents < MIN_EVT_QUEUE_RESUME)) {
  2135. esip->esi_queue_blocked = 0;
  2136. log_printf(LOG_LEVEL_DEBUG, "unblock\n");
  2137. }
  2138. edp = cel->cel_event;
  2139. edp->ed_event.led_lib_channel_handle = cel->cel_chan_handle;
  2140. edp->ed_event.led_sub_id = cel->cel_sub_id;
  2141. edp->ed_event.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2142. edp->ed_event.led_head.error = SA_OK;
  2143. free(cel);
  2144. libais_send_response(conn_info, &edp->ed_event,
  2145. edp->ed_event.led_head.size);
  2146. free_event_data(edp);
  2147. goto data_get_done;
  2148. }
  2149. }
  2150. res.led_head.size = sizeof(res.led_head);
  2151. res.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2152. res.led_head.error = SA_ERR_NOT_EXIST;
  2153. libais_send_response(conn_info, &res, res.led_head.size);
  2154. /*
  2155. * See if there are any events that the app doesn't know about
  2156. * because the notify pipe was full.
  2157. */
  2158. data_get_done:
  2159. if (esip->esi_nevents) {
  2160. __notify_event(conn_info);
  2161. }
  2162. return 0;
  2163. }
  2164. /*
  2165. * Scan the list of channels and remove the specified node.
  2166. */
  2167. static void remove_chan_open_info(SaClmNodeIdT node_id)
  2168. {
  2169. struct list_head *l, *nxt;
  2170. struct event_svr_channel_instance *eci;
  2171. for (l = esc_head.next; l != &esc_head; l = nxt) {
  2172. nxt = l->next;
  2173. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  2174. remove_open_count(eci, node_id);
  2175. }
  2176. }
  2177. /*
  2178. * Called when there is a configuration change in the cluster.
  2179. * This function looks at any joiners and leavers and updates the evt
  2180. * node list. The node list is used to keep track of event IDs
  2181. * received for each node for the detection of duplicate events.
  2182. */
  2183. static int evt_conf_change(
  2184. enum gmi_configuration_type configuration_type,
  2185. struct sockaddr_in *member_list, int member_list_entries,
  2186. struct sockaddr_in *left_list, int left_list_entries,
  2187. struct sockaddr_in *joined_list, int joined_list_entries)
  2188. {
  2189. struct in_addr my_node = {SA_CLM_LOCAL_NODE_ID};
  2190. SaClmClusterNodeT *cn;
  2191. static int first = 1;
  2192. struct sockaddr_in *add_list;
  2193. struct member_node_data *md;
  2194. int add_count;
  2195. struct req_evt_chan_command cpkt;
  2196. struct iovec chn_iovec;
  2197. int res;
  2198. log_printf(LOG_LEVEL_DEBUG, "Evt conf change %d\n",
  2199. configuration_type);
  2200. log_printf(LOG_LEVEL_DEBUG, "m %d, j %d, l %d\n",
  2201. member_list_entries,
  2202. joined_list_entries,
  2203. left_list_entries);
  2204. /*
  2205. * Stop any recovery callbacks in progress.
  2206. */
  2207. if (tok_call_handle) {
  2208. gmi_token_callback_destroy(tok_call_handle);
  2209. tok_call_handle = 0;
  2210. }
  2211. /*
  2212. * Don't seem to be able to tell who joined if we're just coming up. Not all
  2213. * nodes show up in the join list. If this is the first time through,
  2214. * choose the members list to use to add nodes, after that use the join
  2215. * list. Always use the left list for removing nodes.
  2216. */
  2217. if (first) {
  2218. add_list = member_list;
  2219. add_count = member_list_entries;
  2220. first = 0;
  2221. } else {
  2222. add_list = joined_list;
  2223. add_count = joined_list_entries;
  2224. }
  2225. while (add_count--) {
  2226. /*
  2227. * If we've seen this node before, send out the last event ID
  2228. * that we've seen from him. He will set his base event ID to
  2229. * the highest one seen.
  2230. */
  2231. md = evt_find_node(add_list->sin_addr);
  2232. if (md != NULL) {
  2233. if (!md->mn_started) {
  2234. log_printf(RECOVERY_DEBUG,
  2235. "end set evt ID %llx to %s\n",
  2236. md->mn_last_evt_id, inet_ntoa(add_list->sin_addr));
  2237. md->mn_started = 1;
  2238. memset(&cpkt, 0, sizeof(cpkt));
  2239. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  2240. cpkt.chc_head.size = sizeof(cpkt);
  2241. cpkt.chc_op = EVT_SET_ID_OP;
  2242. cpkt.u.chc_set_id.chc_addr = add_list->sin_addr;
  2243. cpkt.u.chc_set_id.chc_last_id =
  2244. md->mn_last_evt_id & BASE_ID_MASK;
  2245. chn_iovec.iov_base = &cpkt;
  2246. chn_iovec.iov_len = cpkt.chc_head.size;
  2247. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  2248. GMI_PRIO_RECOVERY);
  2249. if (res != 0) {
  2250. log_printf(LOG_LEVEL_WARNING,
  2251. "Unable to send event id to %s\n",
  2252. inet_ntoa(add_list->sin_addr));
  2253. }
  2254. }
  2255. }
  2256. add_list++;
  2257. }
  2258. while (left_list_entries--) {
  2259. md = evt_find_node(left_list->sin_addr);
  2260. if (md == 0) {
  2261. log_printf(LOG_LEVEL_WARNING,
  2262. "Can't find cluster node at %s\n",
  2263. inet_ntoa(left_list->sin_addr));
  2264. /*
  2265. * Mark this one as down.
  2266. */
  2267. } else {
  2268. log_printf(RECOVERY_DEBUG, "cluster node at %s down\n",
  2269. inet_ntoa(left_list->sin_addr));
  2270. md->mn_started = 0;
  2271. remove_chan_open_info(md->mn_node_info.nodeId);
  2272. }
  2273. left_list++;
  2274. }
  2275. /*
  2276. * Set the base event id
  2277. */
  2278. cn = clm_get_by_nodeid(my_node);
  2279. if (!base_id_top) {
  2280. log_printf(RECOVERY_DEBUG, "My node ID 0x%x\n", cn->nodeId);
  2281. my_node_id = cn->nodeId;
  2282. set_event_id(my_node_id);
  2283. }
  2284. /*
  2285. * Notify that a config change happened. The exec handler will
  2286. * then determine what to do.
  2287. */
  2288. if (configuration_type == GMI_CONFIGURATION_REGULAR) {
  2289. if (in_cfg_change) {
  2290. log_printf(LOG_LEVEL_NOTICE,
  2291. "Already in config change, Starting over, m %d, c %d\n",
  2292. total_members, checked_in);
  2293. }
  2294. in_cfg_change = 1;
  2295. total_members = member_list_entries;
  2296. checked_in = 0;
  2297. any_joined = joined_list_entries;
  2298. /*
  2299. * Start by updating all the nodes on our
  2300. * open channel count. Once that is done, proceed to determining who
  2301. * sends ratained events. Then we can start normal operation again.
  2302. */
  2303. send_open_count();
  2304. }
  2305. return 0;
  2306. }
  2307. /*
  2308. * saEvtFinalize Handler
  2309. */
  2310. static int evt_finalize(struct conn_info *conn_info)
  2311. {
  2312. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2313. struct event_svr_channel_open *eco;
  2314. struct list_head *l, *nxt;
  2315. log_printf(LOG_LEVEL_DEBUG, "saEvtFinalize (Event exit request)\n");
  2316. log_printf(LOG_LEVEL_DEBUG, "saEvtFinalize %d evts on list\n",
  2317. esip->esi_nevents);
  2318. /*
  2319. * Clean up any open channels and associated subscriptions.
  2320. */
  2321. for (l = esip->esi_open_chans.next; l != &esip->esi_open_chans; l = nxt) {
  2322. nxt = l->next;
  2323. eco = list_entry(l, struct event_svr_channel_open, eco_instance_entry);
  2324. common_chan_close(eco, esip);
  2325. saHandleDestroy(&esip->esi_hdb, eco->eco_my_handle);
  2326. }
  2327. /*
  2328. * Delete track entry if there is one
  2329. */
  2330. list_del (&conn_info->conn_list);
  2331. return 0;
  2332. }
  2333. /*
  2334. * Called at service start time.
  2335. */
  2336. static int evt_exec_init(void)
  2337. {
  2338. int res;
  2339. log_printf(LOG_LEVEL_DEBUG, "Evt exec init request\n");
  2340. res = gmi_recovery_plug_create (&evt_recovery_plug_handle);
  2341. if (res != 0) {
  2342. log_printf(LOG_LEVEL_ERROR,
  2343. "Could not create recovery plug for event service.\n");
  2344. return (-1);
  2345. }
  2346. /*
  2347. * Create an event to be sent when we have to drop messages
  2348. * for an application.
  2349. */
  2350. dropped_event_size = sizeof(*dropped_event) + sizeof(dropped_pattern);
  2351. dropped_event = malloc(dropped_event_size);
  2352. if (dropped_event == 0) {
  2353. log_printf(LOG_LEVEL_ERROR,
  2354. "Memory Allocation Failure, event service not started\n");
  2355. res = gmi_recovery_plug_destroy (evt_recovery_plug_handle);
  2356. errno = ENOMEM;
  2357. return -1;
  2358. }
  2359. memset(dropped_event, 0, sizeof(*dropped_event) + sizeof(dropped_pattern));
  2360. dropped_event->ed_ref_count = 1;
  2361. list_init(&dropped_event->ed_retained);
  2362. dropped_event->ed_event.led_head.size =
  2363. sizeof(*dropped_event) + sizeof(dropped_pattern);
  2364. dropped_event->ed_event.led_head.error = SA_OK;
  2365. dropped_event->ed_event.led_priority = SA_EVT_HIGHEST_PRIORITY;
  2366. dropped_event->ed_event.led_chan_name = lost_chan;
  2367. dropped_event->ed_event.led_publisher_name = dropped_publisher;
  2368. dropped_event->ed_event.led_patterns_number = 1;
  2369. memcpy(&dropped_event->ed_event.led_body[0],
  2370. &dropped_pattern, sizeof(dropped_pattern));
  2371. return 0;
  2372. }
  2373. /*
  2374. * Receive the network event message and distribute it to local subscribers
  2375. */
  2376. static int evt_remote_evt(void *msg, struct in_addr source_addr)
  2377. {
  2378. /*
  2379. * - retain events that have a retention time
  2380. * - Find assocated channel
  2381. * - Scan list of subscribers
  2382. * - Apply filters
  2383. * - Deliver events that pass the filter test
  2384. */
  2385. struct lib_event_data *evtpkt = msg;
  2386. struct event_svr_channel_instance *eci;
  2387. struct event_svr_channel_open *eco;
  2388. struct event_svr_channel_subscr *ecs;
  2389. struct event_data *evt;
  2390. struct list_head *l, *l1;
  2391. SaClmClusterNodeT *cn;
  2392. log_printf(LOG_LEVEL_DEBUG, "Remote event data received from %s\n",
  2393. inet_ntoa(source_addr));
  2394. /*
  2395. * See where the message came from so that we can set the
  2396. * publishing node id in the message before delivery.
  2397. */
  2398. cn = clm_get_by_nodeid (source_addr);
  2399. if (!cn) {
  2400. /*
  2401. * Not sure how this can happen...
  2402. */
  2403. log_printf(LOG_LEVEL_NOTICE, "No cluster node data for %s\n",
  2404. inet_ntoa(source_addr));
  2405. errno = ENXIO;
  2406. return -1;
  2407. }
  2408. log_printf(LOG_LEVEL_DEBUG, "Cluster node ID 0x%x name %s\n",
  2409. cn->nodeId, cn->nodeName.value);
  2410. evtpkt->led_publisher_node_id = cn->nodeId;
  2411. evtpkt->led_in_addr = source_addr;
  2412. evtpkt->led_receive_time = clust_time_now();
  2413. eci = find_channel(&evtpkt->led_chan_name);
  2414. /*
  2415. * We shouldn't see an event for a channel that we don't know about.
  2416. */
  2417. if (!eci) {
  2418. log_printf(LOG_LEVEL_WARNING, "Channel %s doesn't exist\n",
  2419. evtpkt->led_chan_name.value);
  2420. return 0;
  2421. }
  2422. if (check_last_event(evtpkt, source_addr)) {
  2423. return 0;
  2424. }
  2425. evt = make_local_event(evtpkt, eci);
  2426. if (!evt) {
  2427. log_printf(LOG_LEVEL_WARNING,
  2428. "Memory allocation error, can't deliver event\n");
  2429. errno = ENOMEM;
  2430. return -1;
  2431. }
  2432. if (evt->ed_event.led_retention_time) {
  2433. retain_event(evt);
  2434. }
  2435. /*
  2436. * Check open channels
  2437. */
  2438. for (l = eci->esc_open_chans.next; l != &eci->esc_open_chans; l = l->next) {
  2439. eco = list_entry(l, struct event_svr_channel_open, eco_entry);
  2440. /*
  2441. * See if enabled to receive
  2442. */
  2443. if (!(eco->eco_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  2444. continue;
  2445. }
  2446. /*
  2447. * Check subscriptions
  2448. */
  2449. for (l1 = eco->eco_subscr.next; l1 != &eco->eco_subscr; l1 = l1->next) {
  2450. ecs = list_entry(l1, struct event_svr_channel_subscr, ecs_entry);
  2451. /*
  2452. * Apply filter rules and deliver if patterns
  2453. * match filters.
  2454. * Only deliver one event per open channel
  2455. */
  2456. if (event_match(evt, ecs) == SA_OK) {
  2457. deliver_event(evt, eco, ecs);
  2458. break;
  2459. }
  2460. }
  2461. }
  2462. free_event_data(evt);
  2463. return 0;
  2464. }
  2465. /*
  2466. * Calculate the remaining retention time of a received event during recovery
  2467. */
  2468. inline SaTimeT calc_retention_time(SaTimeT retention,
  2469. SaTimeT received, SaTimeT now)
  2470. {
  2471. if ((received < now) && ((now - received) < retention)) {
  2472. return retention - (now - received);
  2473. } else {
  2474. return 0;
  2475. }
  2476. }
  2477. /*
  2478. * Receive a recovery network event message and save it in the retained list
  2479. */
  2480. static int evt_remote_recovery_evt(void *msg, struct in_addr source_addr)
  2481. {
  2482. /*
  2483. * - retain events that have a retention time
  2484. * - Find assocated channel
  2485. */
  2486. struct lib_event_data *evtpkt = msg;
  2487. struct event_svr_channel_instance *eci;
  2488. struct event_data *evt;
  2489. struct member_node_data *md;
  2490. SaTimeT now;
  2491. now = clust_time_now();
  2492. log_printf(LOG_LEVEL_DEBUG,
  2493. "Remote recovery event data received from %s\n",
  2494. inet_ntoa(source_addr));
  2495. if (!in_cfg_change) {
  2496. log_printf(LOG_LEVEL_NOTICE,
  2497. "Received recovery data, not in recovery mode\n");
  2498. return 0;
  2499. }
  2500. log_printf(LOG_LEVEL_DEBUG,
  2501. "Processing recovery of retained events\n");
  2502. if (recovery_node) {
  2503. log_printf(LOG_LEVEL_DEBUG, "This node is the recovery node\n");
  2504. }
  2505. log_printf(LOG_LEVEL_DEBUG, "(1)EVT ID: %llx, Time: %llx\n",
  2506. evtpkt->led_event_id, evtpkt->led_retention_time);
  2507. /*
  2508. * Calculate remaining retention time
  2509. */
  2510. evtpkt->led_retention_time = calc_retention_time(
  2511. evtpkt->led_retention_time,
  2512. evtpkt->led_receive_time,
  2513. now);
  2514. log_printf(LOG_LEVEL_DEBUG,
  2515. "(2)EVT ID: %llx, ret: %llx, rec: %llx, now: %llx\n",
  2516. evtpkt->led_event_id,
  2517. evtpkt->led_retention_time, evtpkt->led_receive_time, now);
  2518. /*
  2519. * If we haven't seen this event yet and it has remaining time, process
  2520. * the event.
  2521. */
  2522. if (!check_last_event(evtpkt, evtpkt->led_in_addr) &&
  2523. evtpkt->led_retention_time) {
  2524. /*
  2525. * See where the message came from so that we can set the
  2526. * publishing node id in the message before delivery.
  2527. */
  2528. md = evt_find_node(evtpkt->led_in_addr);
  2529. if (!md) {
  2530. /*
  2531. * Not sure how this can happen
  2532. */
  2533. log_printf(LOG_LEVEL_NOTICE, "No node for %s\n",
  2534. inet_ntoa(evtpkt->led_in_addr));
  2535. errno = ENXIO;
  2536. return -1;
  2537. }
  2538. log_printf(LOG_LEVEL_DEBUG, "Cluster node ID 0x%x name %s\n",
  2539. md->mn_node_info.nodeId,
  2540. md->mn_node_info.nodeName.value);
  2541. eci = find_channel(&evtpkt->led_chan_name);
  2542. /*
  2543. * We shouldn't see an event for a channel that we don't know about.
  2544. */
  2545. if (!eci) {
  2546. log_printf(LOG_LEVEL_WARNING, "Channel %s doesn't exist\n",
  2547. evtpkt->led_chan_name.value);
  2548. return 0;
  2549. }
  2550. evt = make_local_event(evtpkt, eci);
  2551. if (!evt) {
  2552. log_printf(LOG_LEVEL_WARNING,
  2553. "Memory allocation error, can't deliver event\n");
  2554. errno = ENOMEM;
  2555. return -1;
  2556. }
  2557. retain_event(evt);
  2558. free_event_data(evt);
  2559. }
  2560. return 0;
  2561. }
  2562. /*
  2563. * Timeout handler for event channel open.
  2564. */
  2565. static void chan_open_timeout(void *data)
  2566. {
  2567. struct open_chan_pending *ocp = (struct open_chan_pending *)data;
  2568. struct res_evt_channel_open res;
  2569. res.ico_head.size = sizeof(res);
  2570. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2571. res.ico_head.error = SA_ERR_TIMEOUT;
  2572. libais_send_response (ocp->ocp_conn_info, &res, sizeof(res));
  2573. list_del(&ocp->ocp_entry);
  2574. free(ocp);
  2575. }
  2576. /*
  2577. * Called by the channel open exec handler to finish the open and
  2578. * respond to the application
  2579. */
  2580. static void evt_chan_open_finish(struct open_chan_pending *ocp,
  2581. struct event_svr_channel_instance *eci)
  2582. {
  2583. uint32_t handle;
  2584. struct res_evt_channel_open res;
  2585. struct event_svr_channel_open *eco;
  2586. SaErrorT error;
  2587. struct libevt_ci *esip = &ocp->ocp_conn_info->ais_ci.u.libevt_ci;
  2588. int ret = 0;
  2589. if (!ocp->ocp_async && ocp->ocp_timer_handle) {
  2590. ret = poll_timer_delete(aisexec_poll_handle, ocp->ocp_timer_handle);
  2591. if (ret != 0 ) {
  2592. log_printf(LOG_LEVEL_WARNING,
  2593. "Error clearing timeout for open channel of %s\n",
  2594. ocp->ocp_chan_name);
  2595. }
  2596. }
  2597. /*
  2598. * Create a handle to give back to the caller to associate
  2599. * with this channel open instance.
  2600. */
  2601. error = saHandleCreate(&esip->esi_hdb, sizeof(*eco), &handle);
  2602. if (error != SA_OK) {
  2603. goto open_return;
  2604. }
  2605. error = saHandleInstanceGet(&esip->esi_hdb, handle, (void**)&eco);
  2606. if (error != SA_OK) {
  2607. goto open_return;
  2608. }
  2609. /*
  2610. * Initailize and link into the global channel structure.
  2611. */
  2612. list_init(&eco->eco_subscr);
  2613. list_init(&eco->eco_entry);
  2614. list_init(&eco->eco_instance_entry);
  2615. eco->eco_flags = ocp->ocp_open_flag;
  2616. eco->eco_channel = eci;
  2617. eco->eco_lib_handle = ocp->ocp_c_handle;
  2618. eco->eco_my_handle = handle;
  2619. eco->eco_conn_info = ocp->ocp_conn_info;
  2620. list_add_tail(&eco->eco_entry, &eci->esc_open_chans);
  2621. list_add_tail(&eco->eco_instance_entry, &esip->esi_open_chans);
  2622. /*
  2623. * respond back with a handle to access this channel
  2624. * open instance for later subscriptions, etc.
  2625. */
  2626. saHandleInstancePut(&esip->esi_hdb, handle);
  2627. open_return:
  2628. res.ico_head.size = sizeof(res);
  2629. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2630. res.ico_head.error = error;
  2631. res.ico_channel_handle = handle;
  2632. libais_send_response (ocp->ocp_conn_info, &res, sizeof(res));
  2633. if (ret == 0) {
  2634. list_del(&ocp->ocp_entry);
  2635. free(ocp);
  2636. }
  2637. }
  2638. /*
  2639. * Receive and process remote event operations.
  2640. * Used to communicate channel opens/closes, clear retention time,
  2641. * config change updates...
  2642. */
  2643. static int evt_remote_chan_op(void *msg, struct in_addr source_addr)
  2644. {
  2645. struct req_evt_chan_command *cpkt = msg;
  2646. struct in_addr local_node = {SA_CLM_LOCAL_NODE_ID};
  2647. SaClmClusterNodeT *cn, *my_node;
  2648. struct member_node_data *mn;
  2649. struct event_svr_channel_instance *eci;
  2650. log_printf(LOG_LEVEL_DEBUG, "Remote channel operation request\n");
  2651. my_node = clm_get_by_nodeid(local_node);
  2652. mn = evt_find_node(source_addr);
  2653. if (mn == NULL) {
  2654. cn = clm_get_by_nodeid(source_addr);
  2655. if (cn == NULL) {
  2656. log_printf(LOG_LEVEL_WARNING,
  2657. "Evt remote channel op: Node data for addr %s is NULL\n",
  2658. inet_ntoa(source_addr));
  2659. } else {
  2660. evt_add_node(source_addr, cn);
  2661. mn = evt_find_node(source_addr);
  2662. }
  2663. }
  2664. switch (cpkt->chc_op) {
  2665. /*
  2666. * Open channel remote command. The open channel request is done
  2667. * in two steps. When an pplication tries to open, we send an open
  2668. * channel message to the other nodes. When we receive an open channel
  2669. * message, we may create the channel structure if it doesn't exist
  2670. * and also complete applicaiton open requests for the specified
  2671. * channel. We keep a counter of total opens for the given channel and
  2672. * later when it has been completely closed (everywhere in the cluster)
  2673. * we will free up the assocated channel data.
  2674. */
  2675. case EVT_OPEN_CHAN_OP: {
  2676. struct open_chan_pending *ocp;
  2677. struct list_head *l, *nxt;
  2678. log_printf(LOG_LEVEL_DEBUG, "Opening channel %s for node 0x%x\n",
  2679. cpkt->u.chc_chan.value, mn->mn_node_info.nodeId);
  2680. eci = find_channel(&cpkt->u.chc_chan);
  2681. if (!eci) {
  2682. eci = create_channel(&cpkt->u.chc_chan);
  2683. }
  2684. if (!eci) {
  2685. log_printf(LOG_LEVEL_WARNING, "Could not create channel %s\n",
  2686. &cpkt->u.chc_chan.value);
  2687. break;
  2688. }
  2689. inc_open_count(eci, mn->mn_node_info.nodeId);
  2690. if (mn->mn_node_info.nodeId == my_node->nodeId) {
  2691. /*
  2692. * Complete one of our pending open requests
  2693. */
  2694. for (l = open_pending.next; l != &open_pending; l = nxt) {
  2695. nxt = l->next;
  2696. ocp = list_entry(l, struct open_chan_pending, ocp_entry);
  2697. if (name_match(&ocp->ocp_chan_name, &eci->esc_channel_name)) {
  2698. evt_chan_open_finish(ocp, eci);
  2699. break;
  2700. }
  2701. }
  2702. }
  2703. log_printf(LOG_LEVEL_DEBUG,
  2704. "Open channel %s t %d, l %d, r %d\n",
  2705. eci->esc_channel_name.value,
  2706. eci->esc_total_opens, eci->esc_local_opens,
  2707. eci->esc_retained_count);
  2708. break;
  2709. }
  2710. /*
  2711. * Handle a channel close. We'll decrement the global open counter and
  2712. * free up channel associated data when all instances are closed.
  2713. */
  2714. case EVT_CLOSE_CHAN_OP:
  2715. log_printf(LOG_LEVEL_DEBUG, "Closing channel %s for node 0x%x\n",
  2716. cpkt->u.chc_chan.value, mn->mn_node_info.nodeId);
  2717. eci = find_channel(&cpkt->u.chc_chan);
  2718. if (!eci) {
  2719. log_printf(LOG_LEVEL_NOTICE,
  2720. "Channel close request for %s not found\n",
  2721. cpkt->u.chc_chan.value);
  2722. break;
  2723. }
  2724. /*
  2725. * if last instance, we can free up assocated data.
  2726. */
  2727. dec_open_count(eci, mn->mn_node_info.nodeId);
  2728. log_printf(LOG_LEVEL_DEBUG,
  2729. "Close channel %s t %d, l %d, r %d\n",
  2730. eci->esc_channel_name.value,
  2731. eci->esc_total_opens, eci->esc_local_opens,
  2732. eci->esc_retained_count);
  2733. delete_channel(eci);
  2734. break;
  2735. /*
  2736. * saEvtClearRetentiotime handler.
  2737. */
  2738. case EVT_CLEAR_RET_OP:
  2739. log_printf(LOG_LEVEL_DEBUG, "Clear retention time request %llx\n",
  2740. cpkt->u.chc_event_id);
  2741. clear_retention_time(cpkt->u.chc_event_id);
  2742. break;
  2743. /*
  2744. * Set our next event ID based on the largest event ID seen
  2745. * by others in the cluster. This way, if we've left and re-joined, we'll
  2746. * start using an event ID that is unique.
  2747. */
  2748. case EVT_SET_ID_OP: {
  2749. struct in_addr my_addr;
  2750. my_addr.s_addr = my_node->nodeId;
  2751. log_printf(RECOVERY_DEBUG,
  2752. "Received Set event ID OP from %x to %llx for %x my addr %x base %llx\n",
  2753. inet_ntoa(source_addr),
  2754. cpkt->u.chc_set_id.chc_last_id,
  2755. cpkt->u.chc_set_id.chc_addr.s_addr,
  2756. my_addr.s_addr,
  2757. base_id);
  2758. if (cpkt->u.chc_set_id.chc_addr.s_addr == my_addr.s_addr) {
  2759. if (cpkt->u.chc_set_id.chc_last_id > base_id) {
  2760. log_printf(RECOVERY_DEBUG,
  2761. "Set event ID from %s to %llx\n",
  2762. inet_ntoa(source_addr), cpkt->u.chc_set_id.chc_last_id);
  2763. base_id = cpkt->u.chc_set_id.chc_last_id + 1;
  2764. }
  2765. }
  2766. break;
  2767. }
  2768. /*
  2769. * Receive the open count for a particular channel during recovery.
  2770. * This insures that everyone has the same notion of who has a channel
  2771. * open so that it can be removed when no one else has it open anymore.
  2772. */
  2773. case EVT_OPEN_COUNT:
  2774. if (!in_cfg_change) {
  2775. log_printf(LOG_LEVEL_ERROR,
  2776. "Evt open count msg from %s, but not in membership change\n",
  2777. inet_ntoa(source_addr));
  2778. }
  2779. log_printf(RECOVERY_DEBUG,
  2780. "Open channel count %s is %d for node 0x%x\n",
  2781. cpkt->u.chc_set_opens.chc_chan_name.value,
  2782. cpkt->u.chc_set_opens.chc_open_count,
  2783. mn->mn_node_info.nodeId);
  2784. eci = find_channel(&cpkt->u.chc_set_opens.chc_chan_name);
  2785. if (!eci) {
  2786. eci = create_channel(&cpkt->u.chc_set_opens.chc_chan_name);
  2787. }
  2788. if (!eci) {
  2789. log_printf(LOG_LEVEL_WARNING, "Could not create channel %s\n",
  2790. &cpkt->u.chc_set_opens.chc_chan_name.value);
  2791. break;
  2792. }
  2793. if (set_open_count(eci, mn->mn_node_info.nodeId,
  2794. cpkt->u.chc_set_opens.chc_open_count)) {
  2795. log_printf(LOG_LEVEL_ERROR,
  2796. "Error setting Open channel count %s for node 0x%x\n",
  2797. cpkt->u.chc_set_opens.chc_chan_name.value,
  2798. mn->mn_node_info.nodeId);
  2799. }
  2800. break;
  2801. /*
  2802. * Once we get all the messages from
  2803. * the current membership, determine who delivers any retained events.
  2804. */
  2805. case EVT_OPEN_COUNT_DONE: {
  2806. if (!in_cfg_change) {
  2807. log_printf(LOG_LEVEL_ERROR,
  2808. "Evt config msg from %s, but not in membership change\n",
  2809. inet_ntoa(source_addr));
  2810. }
  2811. log_printf(RECOVERY_DEBUG,
  2812. "Receive EVT_CONF_CHANGE_DONE from %s members %d checked in %d\n",
  2813. inet_ntoa(source_addr), total_members, checked_in+1);
  2814. if (!mn) {
  2815. log_printf(RECOVERY_DEBUG,
  2816. "NO NODE DATA AVAILABLE FOR %s\n",
  2817. inet_ntoa(source_addr));
  2818. }
  2819. if (++checked_in == total_members) {
  2820. /*
  2821. * We're all here, now figure out who should send the
  2822. * retained events, if any.
  2823. */
  2824. mn = oldest_node();
  2825. if (mn->mn_node_info.nodeId == my_node_id) {
  2826. log_printf(RECOVERY_DEBUG, "I am oldest\n");
  2827. send_retained();
  2828. }
  2829. }
  2830. break;
  2831. }
  2832. /*
  2833. * OK, We're done with recovery, continue operations.
  2834. */
  2835. case EVT_CONF_DONE: {
  2836. log_printf(RECOVERY_DEBUG,
  2837. "Receive EVT_CONF_DONE from %s\n",
  2838. inet_ntoa(source_addr));
  2839. in_cfg_change = 0;
  2840. gmi_recovery_plug_unplug (evt_recovery_plug_handle);
  2841. #ifdef DUMP_CHAN_INFO
  2842. dump_all_chans();
  2843. #endif
  2844. break;
  2845. }
  2846. default:
  2847. log_printf(LOG_LEVEL_NOTICE, "Invalid channel operation %d\n",
  2848. cpkt->chc_op);
  2849. break;
  2850. }
  2851. return 0;
  2852. }