4
0

evt.c 116 KB

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