evt.c 85 KB

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