evt.c 117 KB

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