evt.c 106 KB

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