evt.c 99 KB

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