evt.c 103 KB

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