evt.c 99 KB

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