evt.c 105 KB

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