4
0

evt.c 106 KB

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