evt.c 117 KB

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