evt.c 109 KB

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