evt.c 107 KB

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