evt.c 84 KB

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