evt.c 110 KB

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