evt.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355
  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_DEBUG LOG_LEVEL_DEBUG
  35. #define CHAN_DEL_DEBUG LOG_LEVEL_DEBUG
  36. #define CHAN_OPEN_DEBUG LOG_LEVEL_DEBUG
  37. #define REMOTE_OP_DEBUG LOG_LEVEL_DEBUG
  38. #include <sys/types.h>
  39. #include <malloc.h>
  40. #include <errno.h>
  41. #include <sys/time.h>
  42. #include <sys/uio.h>
  43. #include <sys/socket.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include "../include/ipc_evt.h"
  47. #include "../include/list.h"
  48. #include "../include/queue.h"
  49. #include "util.h"
  50. #include "aispoll.h"
  51. #include "mempool.h"
  52. #include "parse.h"
  53. #include "main.h"
  54. #include "totempg.h"
  55. #include "hdb.h"
  56. #include "clm.h"
  57. #include "evt.h"
  58. #include "swab.h"
  59. #define LOG_SERVICE LOG_SERVICE_EVT
  60. #include "print.h"
  61. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info,
  62. void *message);
  63. static int lib_evt_open_channel(struct conn_info *conn_info, void *message);
  64. static int lib_evt_close_channel(struct conn_info *conn_info, void *message);
  65. static int lib_evt_event_subscribe(struct conn_info *conn_info,
  66. void *message);
  67. static int lib_evt_event_unsubscribe(struct conn_info *conn_info,
  68. void *message);
  69. static int lib_evt_event_publish(struct conn_info *conn_info, void *message);
  70. static int lib_evt_event_clear_retentiontime(struct conn_info *conn_info,
  71. void *message);
  72. static int lib_evt_event_data_get(struct conn_info *conn_info,
  73. void *message);
  74. static int evt_conf_change(
  75. enum totempg_configuration_type configuration_type,
  76. struct in_addr *member_list, void *member_list_private,
  77. int member_list_entries,
  78. struct in_addr *left_list, void *left_list_private,
  79. int left_list_entries,
  80. struct in_addr *joined_list, void *joined_list_private,
  81. int joined_list_entries);
  82. static int evt_initialize(struct conn_info *conn_info, void *msg);
  83. static int evt_finalize(struct conn_info *conn_info);
  84. static int evt_exec_init(void);
  85. static struct libais_handler evt_libais_handlers[] = {
  86. {
  87. .libais_handler_fn = message_handler_req_lib_activatepoll,
  88. .response_size = sizeof(struct res_lib_activatepoll),
  89. .response_id = MESSAGE_RES_LIB_ACTIVATEPOLL,
  90. },
  91. {
  92. .libais_handler_fn = lib_evt_open_channel,
  93. .response_size = sizeof(struct res_evt_channel_open),
  94. .response_id = MESSAGE_RES_EVT_OPEN_CHANNEL,
  95. },
  96. {
  97. .libais_handler_fn = lib_evt_close_channel,
  98. .response_size = sizeof(struct res_evt_channel_close),
  99. .response_id = MESSAGE_RES_EVT_CLOSE_CHANNEL,
  100. },
  101. {
  102. .libais_handler_fn = lib_evt_event_subscribe,
  103. .response_size = sizeof(struct res_evt_event_subscribe),
  104. .response_id = MESSAGE_RES_EVT_SUBSCRIBE,
  105. },
  106. {
  107. .libais_handler_fn = lib_evt_event_unsubscribe,
  108. .response_size = sizeof(struct res_evt_event_unsubscribe),
  109. .response_id = MESSAGE_RES_EVT_UNSUBSCRIBE,
  110. },
  111. {
  112. .libais_handler_fn = lib_evt_event_publish,
  113. .response_size = sizeof(struct res_evt_event_publish),
  114. .response_id = MESSAGE_RES_EVT_PUBLISH,
  115. },
  116. {
  117. .libais_handler_fn = lib_evt_event_clear_retentiontime,
  118. .response_size = sizeof(struct res_evt_event_clear_retentiontime),
  119. .response_id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME,
  120. },
  121. {
  122. .libais_handler_fn = lib_evt_event_data_get,
  123. .response_size = sizeof(struct lib_event_data),
  124. .response_id = MESSAGE_RES_EVT_EVENT_DATA,
  125. },
  126. };
  127. static int evt_remote_evt(void *msg, struct in_addr source_addr,
  128. int endian_conversion_required);
  129. static int evt_remote_recovery_evt(void *msg, struct in_addr source_addr,
  130. int endian_conversion_required);
  131. static int evt_remote_chan_op(void *msg, struct in_addr source_addr,
  132. int endian_conversion_required);
  133. static int (*evt_exec_handler_fns[]) (void *m, struct in_addr s,
  134. int endian_conversion_required) = {
  135. evt_remote_evt,
  136. evt_remote_chan_op,
  137. evt_remote_recovery_evt
  138. };
  139. struct service_handler evt_service_handler = {
  140. .libais_handlers = evt_libais_handlers,
  141. .libais_handlers_count = sizeof(evt_libais_handlers) /
  142. sizeof(struct libais_handler),
  143. .aisexec_handler_fns = evt_exec_handler_fns,
  144. .aisexec_handler_fns_count = sizeof(evt_exec_handler_fns) /
  145. sizeof(int (*)),
  146. .confchg_fn = evt_conf_change,
  147. .libais_init_fn = evt_initialize,
  148. .libais_exit_fn = evt_finalize,
  149. .exec_init_fn = evt_exec_init,
  150. .exec_dump_fn = 0
  151. };
  152. // TODOstatic totempg_recovery_plug_handle evt_recovery_plug_handle;
  153. /*
  154. * list of all retained events
  155. * struct event_data
  156. */
  157. static DECLARE_LIST_INIT(retained_list);
  158. /*
  159. * list of all event channel information
  160. * struct event_svr_channel_instance
  161. */
  162. static DECLARE_LIST_INIT(esc_head);
  163. /*
  164. * list of all active event conn_info structs.
  165. * struct conn_info
  166. */
  167. static DECLARE_LIST_INIT(ci_head);
  168. /*
  169. * Structure to track pending channel open requests.
  170. * ocp_async: 1 for async open
  171. * ocp_invocation: invocation for async open
  172. * ocp_chan_name: requested channel
  173. * ocp_conn_info: conn_info for returning to the library.
  174. * ocp_open_flags: channel open flags
  175. * ocp_timer_handle: timer handle for sync open
  176. * ocp_entry: list entry for pending open list.
  177. */
  178. struct open_chan_pending {
  179. int ocp_async;
  180. SaInvocationT ocp_invocation;
  181. SaNameT ocp_chan_name;
  182. struct conn_info *ocp_conn_info;
  183. SaEvtChannelOpenFlagsT ocp_open_flag;
  184. poll_timer_handle ocp_timer_handle;
  185. uint32_t ocp_c_handle;
  186. struct list_head ocp_entry;
  187. };
  188. /*
  189. * list of pending channel opens
  190. */
  191. static DECLARE_LIST_INIT(open_pending);
  192. static void chan_open_timeout(void *data);
  193. #define min(a,b) ((a) < (b) ? (a) : (b))
  194. /*
  195. * Throttle event delivery to applications to keep
  196. * the exec from using too much memory if the app is
  197. * slow to process its events.
  198. */
  199. #define MAX_EVT_DELIVERY_QUEUE 1000
  200. #define MIN_EVT_QUEUE_RESUME (MAX_EVT_DELIVERY_QUEUE / 2)
  201. #define LOST_PUB "EVENT_SERIVCE"
  202. #define LOST_CHAN "LOST EVENT"
  203. /*
  204. * Event to send when the delivery queue gets too full
  205. */
  206. char lost_evt[] = SA_EVT_LOST_EVENT;
  207. static int dropped_event_size;
  208. static struct event_data *dropped_event;
  209. struct evt_pattern {
  210. SaEvtEventPatternT pat;
  211. char str[sizeof(lost_evt)];
  212. };
  213. static struct evt_pattern dropped_pattern = {
  214. .pat = {&dropped_pattern.str[0],
  215. sizeof(lost_evt)},
  216. .str = {SA_EVT_LOST_EVENT}
  217. };
  218. SaNameT lost_chan = {
  219. .value = LOST_CHAN,
  220. .length = sizeof(LOST_CHAN)
  221. };
  222. SaNameT dropped_publisher = {
  223. .value = LOST_PUB,
  224. .length = sizeof(LOST_PUB)
  225. };
  226. struct event_svr_channel_open;
  227. struct event_svr_channel_subscr;
  228. struct open_count {
  229. SaClmNodeIdT oc_node_id;
  230. int32_t oc_open_count;
  231. };
  232. /*
  233. * Structure to contain global channel releated information
  234. *
  235. * esc_channel_name: The name of this channel.
  236. * esc_total_opens: The total number of opens on this channel including
  237. * other nodes.
  238. * esc_local_opens: The number of opens on this channel for this node.
  239. * esc_oc_size: The total number of entries in esc_node_opens;
  240. * esc_node_opens: list of node IDs and how many opens are associated.
  241. * esc_retained_count: How many retained events for this channel
  242. * esc_open_chans: list of opens of this channel.
  243. * (event_svr_channel_open.eco_entry)
  244. * esc_entry: links to other channels. (used by esc_head)
  245. */
  246. struct event_svr_channel_instance {
  247. SaNameT esc_channel_name;
  248. int32_t esc_total_opens;
  249. int32_t esc_local_opens;
  250. uint32_t esc_oc_size;
  251. struct open_count *esc_node_opens;
  252. uint32_t esc_retained_count;
  253. struct list_head esc_open_chans;
  254. struct list_head esc_entry;
  255. };
  256. /*
  257. * Has the event data in the correct format to send to the library API
  258. * with aditional field for accounting.
  259. *
  260. * ed_ref_count: how many other strutures are referencing.
  261. * ed_retained: retained event list.
  262. * ed_timer_handle: Timer handle for retained event expiration.
  263. * ed_delivered: arrays of open channel pointers that this event
  264. * has been delivered to. (only used for events
  265. * with a retention time).
  266. * ed_delivered_count: Number of entries available in ed_delivered.
  267. * ed_delivered_next: Next free spot in ed_delivered
  268. * ed_my_chan: pointer to the global channel instance associated
  269. * with this event.
  270. * ed_event: The event data formatted to be ready to send.
  271. */
  272. struct event_data {
  273. uint32_t ed_ref_count;
  274. struct list_head ed_retained;
  275. poll_timer_handle ed_timer_handle;
  276. struct event_svr_channel_open **ed_delivered;
  277. uint32_t ed_delivered_count;
  278. uint32_t ed_delivered_next;
  279. struct event_svr_channel_instance *ed_my_chan;
  280. struct lib_event_data ed_event;
  281. };
  282. /*
  283. * Contains a list of pending events to be delivered to a subscribed
  284. * application.
  285. *
  286. * cel_chan_handle: associated library channel handle
  287. * cel_sub_id: associated library subscription ID
  288. * cel_event: event structure to deliver.
  289. * cel_entry: list of pending events
  290. * (struct event_server_instance.esi_events)
  291. */
  292. struct chan_event_list {
  293. uint32_t cel_chan_handle;
  294. uint32_t cel_sub_id;
  295. struct event_data* cel_event;
  296. struct list_head cel_entry;
  297. };
  298. /*
  299. * Contains information about each open for a given channel
  300. *
  301. * eco_flags: How the channel was opened.
  302. * eco_lib_handle: channel handle in the app. Used for event delivery.
  303. * eco_my_handle: the handle used to access this data structure.
  304. * eco_channel: Pointer to global channel info.
  305. * eco_entry: links to other opeinings of this channel.
  306. * eco_instance_entry: links to other channel opeinings for the
  307. * associated server instance.
  308. * eco_subscr: head of list of sbuscriptions for this channel open.
  309. * (event_svr_channel_subscr.ecs_entry)
  310. * eco_conn_info: refrence to EvtInitialize who owns this open.
  311. */
  312. struct event_svr_channel_open {
  313. uint8_t eco_flags;
  314. uint32_t eco_lib_handle;
  315. uint32_t eco_my_handle;
  316. struct event_svr_channel_instance *eco_channel;
  317. struct list_head eco_entry;
  318. struct list_head eco_instance_entry;
  319. struct list_head eco_subscr;
  320. struct conn_info *eco_conn_info;
  321. };
  322. /*
  323. * Contains information about each channel subscription
  324. *
  325. * ecs_open_chan: Link to our open channel.
  326. * ecs_sub_id: Subscription ID.
  327. * ecs_filter_count: number of filters in ecs_filters
  328. * ecs_filters: filters for determining event delivery.
  329. * ecs_entry: Links to other subscriptions to this channel opening.
  330. */
  331. struct event_svr_channel_subscr {
  332. struct event_svr_channel_open *ecs_open_chan;
  333. uint32_t ecs_sub_id;
  334. SaEvtEventFilterArrayT *ecs_filters;
  335. struct list_head ecs_entry;
  336. };
  337. /*
  338. * Member node data
  339. * mn_node_info: cluster node info from membership
  340. * mn_last_evt_id: last seen event ID for this node
  341. * mn_started: Indicates that event service has started
  342. * on this node.
  343. * mn_next: pointer to the next node in the hash chain.
  344. * mn_entry: List of all nodes.
  345. */
  346. struct member_node_data {
  347. struct in_addr mn_node_addr;
  348. SaClmClusterNodeT mn_node_info;
  349. SaEvtEventIdT mn_last_evt_id;
  350. SaClmNodeIdT mn_started;
  351. struct member_node_data *mn_next;
  352. struct list_head mn_entry;
  353. };
  354. DECLARE_LIST_INIT(mnd);
  355. /*
  356. * Global varaibles used by the event service
  357. *
  358. * base_id_top: upper bits of next event ID to assign
  359. * base_id: Lower bits of Next event ID to assign
  360. * my_node_id: My cluster node id
  361. * in_cfg_change: Config change occurred. Figure out who sends retained evts.
  362. * cleared when retained events have been delivered.
  363. * total_members: how many members in this cluster
  364. * checked_in: keep track during config change.
  365. * any_joined: did any nodes join on this change?
  366. * recovery_node: True if we're the recovery node.
  367. * tok_call_handle: totempg token callback handle for recovery.
  368. * next_retained: pointer to next retained message to send during recovery.
  369. * next_chan: pointer to next channel to send during recovery.
  370. *
  371. */
  372. #define BASE_ID_MASK 0xffffffffLL
  373. static SaEvtEventIdT base_id = 0;
  374. static SaEvtEventIdT base_id_top = 0;
  375. static SaClmNodeIdT my_node_id = 0;
  376. static int in_cfg_change = 0;
  377. static int total_members = 0;
  378. static int checked_in = 0;
  379. static int any_joined = 0;
  380. static int recovery_node = 0;
  381. static void *tok_call_handle = 0;
  382. static struct list_head *next_retained = 0;
  383. static struct list_head *next_chan = 0;
  384. /*
  385. * Take the filters we received from the application via the library and
  386. * make them into a real SaEvtEventFilterArrayT
  387. */
  388. static SaErrorT evtfilt_to_aisfilt(struct req_evt_event_subscribe *req,
  389. SaEvtEventFilterArrayT **evtfilters)
  390. {
  391. SaEvtEventFilterArrayT *filta =
  392. (SaEvtEventFilterArrayT *)req->ics_filter_data;
  393. SaEvtEventFilterArrayT *filters;
  394. SaEvtEventFilterT *filt = (void *)filta + sizeof(SaEvtEventFilterArrayT);
  395. SaUint8T *str = (void *)filta + sizeof(SaEvtEventFilterArrayT) +
  396. (sizeof(SaEvtEventFilterT) * filta->filtersNumber);
  397. int i;
  398. int j;
  399. filters = malloc(sizeof(SaEvtEventFilterArrayT));
  400. if (!filters) {
  401. return SA_AIS_ERR_NO_MEMORY;
  402. }
  403. filters->filtersNumber = filta->filtersNumber;
  404. filters->filters = malloc(sizeof(SaEvtEventFilterT) *
  405. filta->filtersNumber);
  406. if (!filters->filters) {
  407. free(filters);
  408. return SA_AIS_ERR_NO_MEMORY;
  409. }
  410. for (i = 0; i < filters->filtersNumber; i++) {
  411. filters->filters[i].filter.pattern =
  412. malloc(filt[i].filter.patternSize);
  413. if (!filters->filters[i].filter.pattern) {
  414. for (j = 0; j < i; j++) {
  415. free(filters->filters[j].filter.pattern);
  416. }
  417. free(filters->filters);
  418. free(filters);
  419. return SA_AIS_ERR_NO_MEMORY;
  420. }
  421. filters->filters[i].filter.patternSize =
  422. filt[i].filter.patternSize;
  423. memcpy(filters->filters[i].filter.pattern,
  424. str, filters->filters[i].filter.patternSize);
  425. filters->filters[i].filterType = filt[i].filterType;
  426. str += filters->filters[i].filter.patternSize;
  427. }
  428. *evtfilters = filters;
  429. return SA_AIS_OK;
  430. }
  431. /*
  432. * Free up filter data
  433. */
  434. static void free_filters(SaEvtEventFilterArrayT *fp)
  435. {
  436. int i;
  437. for (i = 0; i < fp->filtersNumber; i++) {
  438. free(fp->filters[i].filter.pattern);
  439. }
  440. free(fp->filters);
  441. free(fp);
  442. }
  443. /*
  444. * Look up a channel in the global channel list
  445. */
  446. static struct event_svr_channel_instance *
  447. find_channel(SaNameT *chan_name)
  448. {
  449. struct list_head *l;
  450. struct event_svr_channel_instance *eci;
  451. for (l = esc_head.next; l != &esc_head; l = l->next) {
  452. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  453. if (!name_match(chan_name, &eci->esc_channel_name)) {
  454. continue;
  455. }
  456. return eci;
  457. }
  458. return 0;
  459. }
  460. /*
  461. * Create and initialize a channel instance structure
  462. */
  463. static struct event_svr_channel_instance *create_channel(SaNameT *cn)
  464. {
  465. struct event_svr_channel_instance *eci;
  466. eci = (struct event_svr_channel_instance *) malloc(sizeof(*eci));
  467. if (!eci) {
  468. return (eci);
  469. }
  470. memset(eci, 0, sizeof(*eci));
  471. list_init(&eci->esc_entry);
  472. list_init(&eci->esc_open_chans);
  473. eci->esc_oc_size = total_members;
  474. eci->esc_node_opens = malloc(sizeof(struct open_count) * total_members);
  475. if (!eci->esc_node_opens) {
  476. free(eci);
  477. return 0;
  478. }
  479. memset(eci->esc_node_opens, 0, sizeof(struct open_count) * total_members);
  480. eci->esc_channel_name = *cn;
  481. eci->esc_channel_name.value[eci->esc_channel_name.length] = '\0';
  482. list_add(&eci->esc_entry, &esc_head);
  483. return eci;
  484. }
  485. /*
  486. * Make sure that the list of nodes is large enough to hold the whole
  487. * membership
  488. */
  489. static int check_open_size(struct event_svr_channel_instance *eci)
  490. {
  491. if (total_members > eci->esc_oc_size) {
  492. eci->esc_node_opens = realloc(eci->esc_node_opens,
  493. sizeof(struct open_count) * total_members);
  494. if (!eci->esc_node_opens) {
  495. log_printf(LOG_LEVEL_WARNING,
  496. "Memory error realloc of node list\n");
  497. return -1;
  498. }
  499. memset(&eci->esc_node_opens[eci->esc_oc_size], 0,
  500. sizeof(struct open_count) * (total_members - eci->esc_oc_size));
  501. eci->esc_oc_size = total_members;
  502. }
  503. return 0;
  504. }
  505. /*
  506. * Find the specified node ID in the node list of the channel.
  507. * If it's not in the list, add it.
  508. */
  509. static struct open_count* find_open_count(
  510. struct event_svr_channel_instance *eci,
  511. SaClmNodeIdT node_id)
  512. {
  513. int i;
  514. for (i = 0; i < eci->esc_oc_size; i++) {
  515. if (eci->esc_node_opens[i].oc_node_id == 0) {
  516. eci->esc_node_opens[i].oc_node_id = node_id;
  517. eci->esc_node_opens[i].oc_open_count = 0;
  518. }
  519. if (eci->esc_node_opens[i].oc_node_id == node_id) {
  520. return &eci->esc_node_opens[i];
  521. }
  522. }
  523. log_printf(LOG_LEVEL_DEBUG,
  524. "find_open_count: node id 0x%x not found\n",
  525. node_id);
  526. return 0;
  527. }
  528. static void dump_chan_opens(struct event_svr_channel_instance *eci)
  529. {
  530. int i;
  531. log_printf(LOG_LEVEL_NOTICE,
  532. "Channel %s, total %d, local %d\n",
  533. eci->esc_channel_name.value,
  534. eci->esc_total_opens,
  535. eci->esc_local_opens);
  536. for (i = 0; i < eci->esc_oc_size; i++) {
  537. if (eci->esc_node_opens[i].oc_node_id == 0) {
  538. break;
  539. }
  540. log_printf(LOG_LEVEL_NOTICE, "Node 0x%x, count %d\n",
  541. eci->esc_node_opens[i].oc_node_id,
  542. eci->esc_node_opens[i].oc_open_count);
  543. }
  544. }
  545. #ifdef DUMP_CHAN_INFO
  546. /*
  547. * Scan the list of channels and dump the open count info.
  548. */
  549. static void dump_all_chans()
  550. {
  551. struct list_head *l;
  552. struct event_svr_channel_instance *eci;
  553. for (l = esc_head.next; l != &esc_head; l = l->next) {
  554. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  555. dump_chan_opens(eci);
  556. }
  557. }
  558. #endif
  559. /*
  560. * Replace the current open count for a node with the specified value.
  561. */
  562. static int set_open_count(struct event_svr_channel_instance *eci,
  563. SaClmNodeIdT node_id, uint32_t open_count)
  564. {
  565. struct open_count *oc;
  566. int i;
  567. if ((i = check_open_size(eci)) != 0) {
  568. return i;
  569. }
  570. oc = find_open_count(eci, node_id);
  571. if (oc) {
  572. if (oc->oc_open_count) {
  573. /*
  574. * If the open count wasn't zero, then we already
  575. * knew about this node. It should never be different than
  576. * what we already had for an open count.
  577. */
  578. if (oc->oc_open_count != open_count) {
  579. log_printf(LOG_LEVEL_ERROR,
  580. "Channel open count error\n");
  581. dump_chan_opens(eci);
  582. }
  583. return 0;
  584. }
  585. log_printf(LOG_LEVEL_DEBUG,
  586. "Set count: Chan %s for node 0x%x, was %d, now %d\n",
  587. eci->esc_channel_name.value,
  588. node_id, eci->esc_node_opens[i].oc_open_count, open_count);
  589. eci->esc_total_opens += open_count;
  590. oc->oc_open_count = open_count;
  591. return 0;
  592. }
  593. return -1;
  594. }
  595. /*
  596. * Increment the open count for the specified node.
  597. */
  598. static int inc_open_count(struct event_svr_channel_instance *eci,
  599. SaClmNodeIdT node_id)
  600. {
  601. struct open_count *oc;
  602. int i;
  603. if ((i = check_open_size(eci)) != 0) {
  604. return i;
  605. }
  606. if (node_id == my_node_id) {
  607. eci->esc_local_opens++;
  608. }
  609. oc = find_open_count(eci, node_id);
  610. if (oc) {
  611. eci->esc_total_opens++;
  612. oc->oc_open_count++;
  613. return 0;
  614. }
  615. return -1;
  616. }
  617. /*
  618. * Decrement the open count for the specified node in the
  619. * specified channel.
  620. */
  621. static int dec_open_count(struct event_svr_channel_instance *eci,
  622. SaClmNodeIdT node_id)
  623. {
  624. struct open_count *oc;
  625. int i;
  626. if ((i = check_open_size(eci)) != 0) {
  627. return i;
  628. }
  629. if (node_id == my_node_id) {
  630. eci->esc_local_opens--;
  631. }
  632. oc = find_open_count(eci, node_id);
  633. if (oc) {
  634. eci->esc_total_opens--;
  635. oc->oc_open_count--;
  636. if ((eci->esc_total_opens < 0) || (oc->oc_open_count < 0)) {
  637. log_printf(LOG_LEVEL_ERROR, "Channel open decrement error\n");
  638. dump_chan_opens(eci);
  639. }
  640. return 0;
  641. }
  642. return -1;
  643. }
  644. /*
  645. * Remove a channel and free its memory if it's not in use anymore.
  646. */
  647. static void delete_channel(struct event_svr_channel_instance *eci)
  648. {
  649. log_printf(CHAN_DEL_DEBUG,
  650. "Called Delete channel %s t %d, l %d, r %d\n",
  651. eci->esc_channel_name.value,
  652. eci->esc_total_opens, eci->esc_local_opens,
  653. eci->esc_retained_count);
  654. /*
  655. * If no one has the channel open anywhere and there are no unexpired
  656. * retained events for this channel, then it is OK to delete the
  657. * data structure.
  658. */
  659. if ((eci->esc_retained_count == 0) && (eci->esc_total_opens == 0)) {
  660. log_printf(CHAN_DEL_DEBUG, "Delete channel %s\n",
  661. eci->esc_channel_name.value);
  662. if (!list_empty(&eci->esc_open_chans)) {
  663. log_printf(LOG_LEVEL_NOTICE,
  664. "Last channel close request for %s (still open)\n",
  665. eci->esc_channel_name.value);
  666. dump_chan_opens(eci);
  667. return;
  668. }
  669. /*
  670. * adjust if we're sending open counts on a config change.
  671. */
  672. if (in_cfg_change && (&eci->esc_entry == next_chan)) {
  673. next_chan = eci->esc_entry.next;
  674. }
  675. list_del(&eci->esc_entry);
  676. if (eci->esc_node_opens) {
  677. free(eci->esc_node_opens);
  678. }
  679. free(eci);
  680. }
  681. }
  682. /*
  683. * Remove the specified node from the node list in this channel.
  684. */
  685. static int remove_open_count(
  686. struct event_svr_channel_instance *eci,
  687. SaClmNodeIdT node_id)
  688. {
  689. int i;
  690. int j;
  691. /*
  692. * Find the node, remove it and re-pack the array.
  693. */
  694. for (i = 0; i < eci->esc_oc_size; i++) {
  695. if (eci->esc_node_opens[i].oc_node_id == 0) {
  696. break;
  697. }
  698. log_printf(RECOVERY_DEBUG, "roc: %x/%x, t %d, oc %d\n",
  699. node_id, eci->esc_node_opens[i].oc_node_id,
  700. eci->esc_total_opens, eci->esc_node_opens[i].oc_open_count);
  701. if (eci->esc_node_opens[i].oc_node_id == node_id) {
  702. eci->esc_total_opens -= eci->esc_node_opens[i].oc_open_count;
  703. for (j = i+1; j < eci->esc_oc_size; j++, i++) {
  704. eci->esc_node_opens[i].oc_node_id =
  705. eci->esc_node_opens[j].oc_node_id;
  706. eci->esc_node_opens[i].oc_open_count =
  707. eci->esc_node_opens[j].oc_open_count;
  708. }
  709. eci->esc_node_opens[eci->esc_oc_size-1].oc_node_id = 0;
  710. eci->esc_node_opens[eci->esc_oc_size-1].oc_open_count = 0;
  711. /*
  712. * Remove the channel if it's not being used anymore
  713. */
  714. delete_channel(eci);
  715. return 0;
  716. }
  717. }
  718. return -1;
  719. }
  720. /*
  721. * Send a request to open a channel to the rest of the cluster.
  722. */
  723. static SaErrorT evt_open_channel(SaNameT *cn, SaUint8T flgs)
  724. {
  725. struct req_evt_chan_command cpkt;
  726. struct event_svr_channel_instance *eci;
  727. struct iovec chn_iovec;
  728. int res;
  729. SaErrorT ret;
  730. ret = SA_AIS_OK;
  731. eci = find_channel(cn);
  732. /*
  733. * If the create flag set, and it doesn't exist, we can make the channel.
  734. * Otherwise, it's an error since we're notified of channels being created
  735. * and opened.
  736. */
  737. if (!eci && !(flgs & SA_EVT_CHANNEL_CREATE)) {
  738. ret = SA_AIS_ERR_NOT_EXIST;
  739. goto chan_open_end;
  740. }
  741. /*
  742. * create the channel packet to send. Tell the the cluster
  743. * to create the channel.
  744. */
  745. memset(&cpkt, 0, sizeof(cpkt));
  746. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  747. cpkt.chc_head.size = sizeof(cpkt);
  748. cpkt.chc_op = EVT_OPEN_CHAN_OP;
  749. cpkt.u.chc_chan = *cn;
  750. chn_iovec.iov_base = &cpkt;
  751. chn_iovec.iov_len = cpkt.chc_head.size;
  752. log_printf(CHAN_OPEN_DEBUG, "evt_open_channel: Send open mcast\n");
  753. res = totempg_mcast (&chn_iovec, 1, TOTEMPG_AGREED);
  754. log_printf(CHAN_OPEN_DEBUG, "evt_open_channel: Open mcast result: %d\n",
  755. res);
  756. if (res != 0) {
  757. ret = SA_AIS_ERR_LIBRARY;
  758. }
  759. chan_open_end:
  760. return ret;
  761. }
  762. /*
  763. * Send a request to close a channel with the rest of the cluster.
  764. */
  765. static SaErrorT evt_close_channel(SaNameT *cn)
  766. {
  767. struct req_evt_chan_command cpkt;
  768. struct iovec chn_iovec;
  769. int res;
  770. SaErrorT ret;
  771. ret = SA_AIS_OK;
  772. /*
  773. * create the channel packet to send. Tell the the cluster
  774. * to close the channel.
  775. */
  776. memset(&cpkt, 0, sizeof(cpkt));
  777. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  778. cpkt.chc_head.size = sizeof(cpkt);
  779. cpkt.chc_op = EVT_CLOSE_CHAN_OP;
  780. cpkt.u.chc_chan = *cn;
  781. chn_iovec.iov_base = &cpkt;
  782. chn_iovec.iov_len = cpkt.chc_head.size;
  783. res = totempg_mcast (&chn_iovec, 1, TOTEMPG_AGREED);
  784. if (res != 0) {
  785. ret = SA_AIS_ERR_LIBRARY;
  786. }
  787. return ret;
  788. }
  789. /*
  790. * Node data access functions. Used to keep track of event IDs
  791. * delivery of messages.
  792. *
  793. * add_node: Add a new member node to our list.
  794. * remove_node: Remove a node that left membership.
  795. * find_node: Given the node ID return a pointer to node information.
  796. *
  797. */
  798. #define NODE_HASH_SIZE 256
  799. static struct member_node_data *nl[NODE_HASH_SIZE] = {0};
  800. inline int
  801. hash_sock_addr(struct in_addr addr)
  802. {
  803. return addr.s_addr & (NODE_HASH_SIZE - 1);
  804. }
  805. static struct member_node_data **lookup_node(struct in_addr addr)
  806. {
  807. int index = hash_sock_addr(addr);
  808. struct member_node_data **nlp;
  809. nlp = &nl[index];
  810. for (nlp = &nl[index]; *nlp; nlp = &((*nlp)->mn_next)) {
  811. if ((*nlp)->mn_node_addr.s_addr == addr.s_addr) {
  812. break;
  813. }
  814. }
  815. return nlp;
  816. }
  817. static struct member_node_data *
  818. evt_find_node(struct in_addr addr)
  819. {
  820. struct member_node_data **nlp;
  821. nlp = lookup_node(addr);
  822. if (!nlp) {
  823. log_printf(LOG_LEVEL_DEBUG, "find_node: Got NULL nlp?\n");
  824. return 0;
  825. }
  826. return *nlp;
  827. }
  828. static SaErrorT
  829. evt_add_node(struct in_addr addr, SaClmClusterNodeT *cn)
  830. {
  831. struct member_node_data **nlp;
  832. struct member_node_data *nl;
  833. SaErrorT err = SA_AIS_ERR_EXIST;
  834. nlp = lookup_node(addr);
  835. if (!nlp) {
  836. log_printf(LOG_LEVEL_DEBUG, "add_node: Got NULL nlp?\n");
  837. goto an_out;
  838. }
  839. if (*nlp) {
  840. goto an_out;
  841. }
  842. *nlp = malloc(sizeof(struct member_node_data));
  843. if (!nlp) {
  844. return SA_AIS_ERR_NO_MEMORY;
  845. }
  846. nl = *nlp;
  847. if (nl) {
  848. memset(nl, 0, sizeof(*nl));
  849. err = SA_AIS_OK;
  850. nl->mn_node_addr = addr;
  851. nl->mn_started = 1;
  852. }
  853. list_init(&nl->mn_entry);
  854. list_add(&nl->mn_entry, &mnd);
  855. nl->mn_node_info = *cn;
  856. an_out:
  857. return err;
  858. }
  859. #ifdef REMOVE_NODE
  860. static SaErrorT
  861. evt_remove_node(struct in_addr addr)
  862. {
  863. struct member_node_data **nlp;
  864. struct member_node_data *nl;
  865. SaErrorT err = SA_AIS_ERR_NOT_EXIST;
  866. nlp = lookup_node(addr);
  867. if (!nlp) {
  868. log_printf(LOG_LEVEL_DEBUG, "remove_node: Got NULL nlp?\n");
  869. goto an_out;
  870. }
  871. if (!(*nlp)) {
  872. goto an_out;
  873. }
  874. nl = *nlp;
  875. list_del(&nl->mn_entry);
  876. *nlp = nl->mn_next;
  877. free(*nlp);
  878. err = SA_AIS_OK;
  879. an_out:
  880. return err;
  881. }
  882. #endif /* REMOVE_NODE */
  883. /*
  884. * Find the oldest node in the membership. This is the one we choose to
  885. * perform some cluster-wide functions like distributing retained events.
  886. */
  887. static struct member_node_data* oldest_node()
  888. {
  889. struct list_head *l;
  890. struct member_node_data *mn = 0;
  891. struct member_node_data *oldest = 0;
  892. for (l = mnd.next; l != &mnd; l = l->next) {
  893. mn = list_entry(l, struct member_node_data, mn_entry);
  894. if (mn->mn_started == 0) {
  895. continue;
  896. }
  897. if ((oldest == NULL) ||
  898. (mn->mn_node_info.bootTimestamp <
  899. oldest->mn_node_info.bootTimestamp )) {
  900. oldest = mn;
  901. } else if (mn->mn_node_info.bootTimestamp ==
  902. oldest->mn_node_info.bootTimestamp) {
  903. if (mn->mn_node_info.nodeId < oldest->mn_node_info.nodeId) {
  904. oldest = mn;
  905. }
  906. }
  907. }
  908. return oldest;
  909. }
  910. /*
  911. * Token callback routine. Send as many mcasts as possible to distribute
  912. * retained events on a config change.
  913. */
  914. static int send_next_retained(void *data)
  915. {
  916. struct req_evt_chan_command cpkt;
  917. struct iovec chn_iovec;
  918. struct event_data *evt;
  919. int res;
  920. if (in_cfg_change && recovery_node) {
  921. /*
  922. * Process messages. When we're done, send the done message
  923. * to the nodes.
  924. */
  925. for (;next_retained != &retained_list;
  926. next_retained = next_retained->next) {
  927. log_printf(LOG_LEVEL_DEBUG, "Sending next retained event\n");
  928. evt = list_entry(next_retained, struct event_data, ed_retained);
  929. evt->ed_event.led_head.id = MESSAGE_REQ_EXEC_EVT_RECOVERY_EVENTDATA;
  930. chn_iovec.iov_base = &evt->ed_event;
  931. chn_iovec.iov_len = evt->ed_event.led_head.size;
  932. res = totempg_mcast(&chn_iovec, 1, TOTEMPG_AGREED);
  933. if (res != 0) {
  934. /*
  935. * Try again later.
  936. */
  937. return -1;
  938. }
  939. }
  940. log_printf(RECOVERY_DEBUG, "DONE Sending retained events\n");
  941. memset(&cpkt, 0, sizeof(cpkt));
  942. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  943. cpkt.chc_head.size = sizeof(cpkt);
  944. cpkt.chc_op = EVT_CONF_DONE;
  945. chn_iovec.iov_base = &cpkt;
  946. chn_iovec.iov_len = cpkt.chc_head.size;
  947. res = totempg_mcast (&chn_iovec, 1, TOTEMPG_AGREED);
  948. }
  949. tok_call_handle = 0;
  950. return 0;
  951. }
  952. /*
  953. * Send our retained events. If we've been chosen as the recovery node, kick
  954. * kick off the process of sending retained events.
  955. */
  956. static void send_retained()
  957. {
  958. struct req_evt_chan_command cpkt;
  959. struct iovec chn_iovec;
  960. int res = 0;
  961. if (list_empty(&retained_list) || !any_joined) {
  962. memset(&cpkt, 0, sizeof(cpkt));
  963. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  964. cpkt.chc_head.size = sizeof(cpkt);
  965. cpkt.chc_op = EVT_CONF_DONE;
  966. chn_iovec.iov_base = &cpkt;
  967. chn_iovec.iov_len = cpkt.chc_head.size;
  968. log_printf(RECOVERY_DEBUG, "No messages to send\n");
  969. res = totempg_mcast (&chn_iovec, 1, TOTEMPG_AGREED);
  970. } else {
  971. log_printf(RECOVERY_DEBUG,
  972. "Start sending retained messages\n");
  973. recovery_node = 1;
  974. next_retained = retained_list.next;
  975. // TODO res = totempg_token_callback_create(&tok_call_handle, send_next_retained,
  976. // NULL);
  977. }
  978. if (res != 0) {
  979. log_printf(LOG_LEVEL_ERROR, "ERROR sending evt recovery data\n");
  980. }
  981. }
  982. /*
  983. * Token callback routine. Send as many mcasts as possible to distribute
  984. * open counts on a config change.
  985. */
  986. static int send_next_open_count(void *data)
  987. {
  988. struct req_evt_chan_command cpkt;
  989. struct iovec chn_iovec;
  990. struct event_svr_channel_instance *eci;
  991. int res;
  992. if (in_cfg_change) {
  993. /*
  994. * Process messages. When we're done, send the done message
  995. * to the nodes.
  996. */
  997. memset(&cpkt, 0, sizeof(cpkt));
  998. for (;next_chan != &esc_head;
  999. next_chan = next_chan->next) {
  1000. log_printf(RECOVERY_DEBUG, "Sending next open count\n");
  1001. eci = list_entry(next_chan, struct event_svr_channel_instance,
  1002. esc_entry);
  1003. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  1004. cpkt.chc_head.size = sizeof(cpkt);
  1005. cpkt.chc_op = EVT_OPEN_COUNT;
  1006. cpkt.u.chc_set_opens.chc_chan_name = eci->esc_channel_name;
  1007. cpkt.u.chc_set_opens.chc_open_count = eci->esc_local_opens;
  1008. chn_iovec.iov_base = &cpkt;
  1009. chn_iovec.iov_len = cpkt.chc_head.size;
  1010. res = totempg_mcast(&chn_iovec, 1,TOTEMPG_AGREED);
  1011. if (res != 0) {
  1012. /*
  1013. * Try again later.
  1014. */
  1015. return -1;
  1016. }
  1017. }
  1018. log_printf(RECOVERY_DEBUG, "DONE Sending open counts\n");
  1019. memset(&cpkt, 0, sizeof(cpkt));
  1020. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  1021. cpkt.chc_head.size = sizeof(cpkt);
  1022. cpkt.chc_op = EVT_OPEN_COUNT_DONE;
  1023. chn_iovec.iov_base = &cpkt;
  1024. chn_iovec.iov_len = cpkt.chc_head.size;
  1025. res = totempg_mcast (&chn_iovec, 1,TOTEMPG_AGREED);
  1026. }
  1027. tok_call_handle = 0;
  1028. return 0;
  1029. }
  1030. /*
  1031. * kick off the process of sending open channel counts during recovery.
  1032. * Every node does this.
  1033. */
  1034. static void send_open_count()
  1035. {
  1036. struct req_evt_chan_command cpkt;
  1037. struct iovec chn_iovec;
  1038. int res;
  1039. if (list_empty(&esc_head)) {
  1040. memset(&cpkt, 0, sizeof(cpkt));
  1041. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  1042. cpkt.chc_head.size = sizeof(cpkt);
  1043. cpkt.chc_op = EVT_OPEN_COUNT_DONE;
  1044. chn_iovec.iov_base = &cpkt;
  1045. chn_iovec.iov_len = cpkt.chc_head.size;
  1046. log_printf(RECOVERY_DEBUG, "No channels to send\n");
  1047. res = totempg_mcast (&chn_iovec, 1,TOTEMPG_AGREED);
  1048. } else {
  1049. log_printf(RECOVERY_DEBUG,
  1050. "Start sending open channel count\n");
  1051. next_chan = esc_head.next;
  1052. // TODO res = totempg_token_callback_create(&tok_call_handle, send_next_open_count,
  1053. // NULL);
  1054. }
  1055. if (res != 0) {
  1056. log_printf(LOG_LEVEL_ERROR, "ERROR sending evt recovery data\n");
  1057. }
  1058. }
  1059. /*
  1060. * keep track of the last event ID from a node.
  1061. * If we get an event ID less than our last, we've already
  1062. * seen it. It's probably a retained event being sent to
  1063. * a new node.
  1064. */
  1065. static int check_last_event(struct lib_event_data *evtpkt,
  1066. struct in_addr addr)
  1067. {
  1068. struct member_node_data *nd;
  1069. SaClmClusterNodeT *cn;
  1070. nd = evt_find_node(addr);
  1071. if (!nd) {
  1072. log_printf(LOG_LEVEL_DEBUG,
  1073. "Node ID 0x%x not found for event %llx\n",
  1074. evtpkt->led_publisher_node_id, evtpkt->led_event_id);
  1075. cn = clm_get_by_nodeid(addr);
  1076. if (!cn) {
  1077. log_printf(LOG_LEVEL_DEBUG,
  1078. "Cluster Node 0x%x not found for event %llx\n",
  1079. evtpkt->led_publisher_node_id, evtpkt->led_event_id);
  1080. } else {
  1081. evt_add_node(addr, cn);
  1082. nd = evt_find_node(addr);
  1083. }
  1084. }
  1085. if (!nd) {
  1086. return 0;
  1087. }
  1088. if ((nd->mn_last_evt_id < evtpkt->led_event_id)) {
  1089. nd->mn_last_evt_id = evtpkt->led_event_id;
  1090. return 0;
  1091. }
  1092. return 1;
  1093. }
  1094. /*
  1095. * Send a message to the app to wake it up if it is polling
  1096. */
  1097. static int message_handler_req_lib_activatepoll(struct conn_info *conn_info,
  1098. void *message)
  1099. {
  1100. struct res_lib_activatepoll res;
  1101. res.header.error = SA_AIS_OK;
  1102. res.header.size = sizeof (struct res_lib_activatepoll);
  1103. res.header.id = MESSAGE_RES_LIB_ACTIVATEPOLL;
  1104. libais_send_response(conn_info, &res, sizeof(res));
  1105. return (0);
  1106. }
  1107. /*
  1108. * event id generating code. We use the node ID for this node for the
  1109. * upper 32 bits of the event ID to make sure that we can generate a cluster
  1110. * wide unique event ID for a given event.
  1111. */
  1112. SaErrorT set_event_id(SaClmNodeIdT node_id)
  1113. {
  1114. SaErrorT err = SA_AIS_OK;
  1115. if (base_id_top) {
  1116. err = SA_AIS_ERR_EXIST;
  1117. }
  1118. base_id_top = (SaEvtEventIdT)node_id << 32;
  1119. return err;
  1120. }
  1121. static SaErrorT get_event_id(uint64_t *event_id)
  1122. {
  1123. *event_id = base_id_top | base_id ;
  1124. base_id = (base_id + 1) & BASE_ID_MASK;
  1125. return SA_AIS_OK;
  1126. }
  1127. /*
  1128. * Free up an event structure if it isn't being used anymore.
  1129. */
  1130. static void
  1131. free_event_data(struct event_data *edp)
  1132. {
  1133. if (--edp->ed_ref_count) {
  1134. return;
  1135. }
  1136. log_printf(LOG_LEVEL_DEBUG, "Freeing event ID: 0x%llx\n",
  1137. edp->ed_event.led_event_id);
  1138. if (edp->ed_delivered) {
  1139. free(edp->ed_delivered);
  1140. }
  1141. free(edp);
  1142. }
  1143. /*
  1144. * Timer handler to delete expired events.
  1145. *
  1146. */
  1147. static void
  1148. event_retention_timeout(void *data)
  1149. {
  1150. struct event_data *edp = data;
  1151. log_printf(LOG_LEVEL_DEBUG, "Event ID %llx expired\n",
  1152. edp->ed_event.led_event_id);
  1153. /*
  1154. * adjust next_retained if we're in recovery and
  1155. * were in charge of sending retained events.
  1156. */
  1157. if (in_cfg_change && recovery_node) {
  1158. if (next_retained == &edp->ed_retained) {
  1159. next_retained = edp->ed_retained.next;
  1160. }
  1161. }
  1162. list_del(&edp->ed_retained);
  1163. list_init(&edp->ed_retained);
  1164. /*
  1165. * Check to see it the channel isn't in use anymore.
  1166. */
  1167. edp->ed_my_chan->esc_retained_count--;
  1168. if (edp->ed_my_chan->esc_retained_count == 0) {
  1169. delete_channel(edp->ed_my_chan);
  1170. }
  1171. free_event_data(edp);
  1172. }
  1173. /*
  1174. * clear a particular event's retention time.
  1175. * This will free the event as long as it isn't being
  1176. * currently used.
  1177. *
  1178. */
  1179. static void
  1180. clear_retention_time(SaEvtEventIdT event_id)
  1181. {
  1182. struct event_data *edp;
  1183. struct list_head *l, *nxt;
  1184. int ret;
  1185. log_printf(LOG_LEVEL_DEBUG, "Search for Event ID %llx\n", event_id);
  1186. for(l = retained_list.next; l != &retained_list; l = nxt) {
  1187. nxt = l->next;
  1188. edp = list_entry(l, struct event_data, ed_retained);
  1189. if (edp->ed_event.led_event_id != event_id) {
  1190. continue;
  1191. }
  1192. log_printf(LOG_LEVEL_DEBUG,
  1193. "Clear retention time for Event ID %llx\n",
  1194. edp->ed_event.led_event_id);
  1195. ret = poll_timer_delete(aisexec_poll_handle, edp->ed_timer_handle);
  1196. if (ret != 0 ) {
  1197. log_printf(LOG_LEVEL_ERROR, "Error expiring event ID %llx\n",
  1198. edp->ed_event.led_event_id);
  1199. return;
  1200. }
  1201. edp->ed_event.led_retention_time = 0;
  1202. list_del(&edp->ed_retained);
  1203. list_init(&edp->ed_retained);
  1204. /*
  1205. * Check to see if the channel isn't in use anymore.
  1206. */
  1207. edp->ed_my_chan->esc_retained_count--;
  1208. if (edp->ed_my_chan->esc_retained_count == 0) {
  1209. delete_channel(edp->ed_my_chan);
  1210. }
  1211. free_event_data(edp);
  1212. break;
  1213. }
  1214. }
  1215. /*
  1216. * Remove specified channel from event delivery list
  1217. */
  1218. static void
  1219. remove_delivered_channel(struct event_svr_channel_open *eco)
  1220. {
  1221. int i;
  1222. struct list_head *l;
  1223. struct event_data *edp;
  1224. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1225. edp = list_entry(l, struct event_data, ed_retained);
  1226. for (i = 0; i < edp->ed_delivered_next; i++) {
  1227. if (edp->ed_delivered[i] == eco) {
  1228. edp->ed_delivered_next--;
  1229. if (edp->ed_delivered_next == i) {
  1230. break;
  1231. }
  1232. memmove(&edp->ed_delivered[i],
  1233. &edp->ed_delivered[i+1],
  1234. &edp->ed_delivered[edp->ed_delivered_next] -
  1235. &edp->ed_delivered[i]);
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. return;
  1241. }
  1242. /*
  1243. * If there is a retention time, add this open channel to the event so
  1244. * we can check if we've already delivered this message later if a new
  1245. * subscription matches.
  1246. */
  1247. #define DELIVER_SIZE 8
  1248. static void
  1249. evt_delivered(struct event_data *evt, struct event_svr_channel_open *eco)
  1250. {
  1251. if (!evt->ed_event.led_retention_time) {
  1252. return;
  1253. }
  1254. log_printf(LOG_LEVEL_DEBUG, "delivered ID %llx to eco %p\n",
  1255. evt->ed_event.led_event_id, eco);
  1256. if (evt->ed_delivered_count == evt->ed_delivered_next) {
  1257. evt->ed_delivered = realloc(evt->ed_delivered,
  1258. DELIVER_SIZE * sizeof(struct event_svr_channel_open *));
  1259. memset(evt->ed_delivered + evt->ed_delivered_next, 0,
  1260. DELIVER_SIZE * sizeof(struct event_svr_channel_open *));
  1261. evt->ed_delivered_next = evt->ed_delivered_count;
  1262. evt->ed_delivered_count += DELIVER_SIZE;
  1263. }
  1264. evt->ed_delivered[evt->ed_delivered_next++] = eco;
  1265. }
  1266. /*
  1267. * Check to see if an event has already been delivered to this open channel
  1268. */
  1269. static int
  1270. evt_already_delivered(struct event_data *evt,
  1271. struct event_svr_channel_open *eco)
  1272. {
  1273. int i;
  1274. if (!evt->ed_event.led_retention_time) {
  1275. return 0;
  1276. }
  1277. log_printf(LOG_LEVEL_DEBUG, "Deliver count: %d deliver_next %d\n",
  1278. evt->ed_delivered_count, evt->ed_delivered_next);
  1279. for (i = 0; i < evt->ed_delivered_next; i++) {
  1280. log_printf(LOG_LEVEL_DEBUG, "Checking ID %llx delivered %p eco %p\n",
  1281. evt->ed_event.led_event_id, evt->ed_delivered[i], eco);
  1282. if (evt->ed_delivered[i] == eco) {
  1283. return 1;
  1284. }
  1285. }
  1286. return 0;
  1287. }
  1288. /*
  1289. * Compare a filter to a given pattern.
  1290. * return SA_AIS_OK if the pattern matches a filter
  1291. */
  1292. static SaErrorT
  1293. filter_match(SaEvtEventPatternT *ep, SaEvtEventFilterT *ef)
  1294. {
  1295. int ret;
  1296. ret = SA_AIS_ERR_FAILED_OPERATION;
  1297. switch (ef->filterType) {
  1298. case SA_EVT_PREFIX_FILTER:
  1299. if (ef->filter.patternSize > ep->patternSize) {
  1300. break;
  1301. }
  1302. if (strncmp(ef->filter.pattern, ep->pattern,
  1303. ef->filter.patternSize) == 0) {
  1304. ret = SA_AIS_OK;
  1305. }
  1306. break;
  1307. case SA_EVT_SUFFIX_FILTER:
  1308. if (ef->filter.patternSize > ep->patternSize) {
  1309. break;
  1310. }
  1311. if (strncmp(ef->filter.pattern,
  1312. &ep->pattern[ep->patternSize - ef->filter.patternSize],
  1313. ef->filter.patternSize) == 0) {
  1314. ret = SA_AIS_OK;
  1315. }
  1316. break;
  1317. case SA_EVT_EXACT_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_PASS_ALL_FILTER:
  1327. ret = SA_AIS_OK;
  1328. break;
  1329. default:
  1330. break;
  1331. }
  1332. return ret;
  1333. }
  1334. /*
  1335. * compare the event's patterns with the subscription's filter rules.
  1336. * SA_AIS_OK is returned if the event matches the filter rules.
  1337. */
  1338. static SaErrorT
  1339. event_match(struct event_data *evt,
  1340. struct event_svr_channel_subscr *ecs)
  1341. {
  1342. SaEvtEventFilterT *ef;
  1343. SaEvtEventPatternT *ep;
  1344. uint32_t filt_count;
  1345. SaErrorT ret = SA_AIS_OK;
  1346. int i;
  1347. ep = (SaEvtEventPatternT *)(&evt->ed_event.led_body[0]);
  1348. ef = ecs->ecs_filters->filters;
  1349. filt_count = min(ecs->ecs_filters->filtersNumber,
  1350. evt->ed_event.led_patterns_number);
  1351. for (i = 0; i < filt_count; i++) {
  1352. ret = filter_match(ep, ef);
  1353. if (ret != SA_AIS_OK) {
  1354. break;
  1355. }
  1356. ep++;
  1357. ef++;
  1358. }
  1359. return ret;
  1360. }
  1361. /*
  1362. * Scan undelivered pending events and either remove them if no subscription
  1363. * filters match anymore or re-assign them to another matching subscription
  1364. */
  1365. static void
  1366. filter_undelivered_events(struct event_svr_channel_open *op_chan)
  1367. {
  1368. struct event_svr_channel_open *eco;
  1369. struct event_svr_channel_instance *eci;
  1370. struct event_svr_channel_subscr *ecs;
  1371. struct chan_event_list *cel;
  1372. struct libevt_ci *esip = &op_chan->eco_conn_info->ais_ci.u.libevt_ci;
  1373. struct list_head *l, *nxt;
  1374. struct list_head *l1, *l2;
  1375. int i;
  1376. eci = op_chan->eco_channel;
  1377. /*
  1378. * Scan each of the priority queues for messages
  1379. */
  1380. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  1381. /*
  1382. * examine each message queued for delivery
  1383. */
  1384. for (l = esip->esi_events[i].next; l != &esip->esi_events[i]; l = nxt) {
  1385. nxt = l->next;
  1386. cel = list_entry(l, struct chan_event_list, cel_entry);
  1387. /*
  1388. * Check open channels
  1389. */
  1390. for (l1 = eci->esc_open_chans.next;
  1391. l1 != &eci->esc_open_chans; l1 = l1->next) {
  1392. eco = list_entry(l1, struct event_svr_channel_open, eco_entry);
  1393. /*
  1394. * See if this channel open instance belongs
  1395. * to this evtinitialize instance
  1396. */
  1397. if (eco->eco_conn_info != op_chan->eco_conn_info) {
  1398. continue;
  1399. }
  1400. /*
  1401. * See if enabled to receive
  1402. */
  1403. if (!(eco->eco_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  1404. continue;
  1405. }
  1406. /*
  1407. * Check subscriptions
  1408. */
  1409. for (l2 = eco->eco_subscr.next;
  1410. l2 != &eco->eco_subscr; l2 = l2->next) {
  1411. ecs = list_entry(l2,
  1412. struct event_svr_channel_subscr, ecs_entry);
  1413. if (event_match(cel->cel_event, ecs) == SA_AIS_OK) {
  1414. /*
  1415. * Something still matches.
  1416. * We'll assign it to
  1417. * the new subscription.
  1418. */
  1419. cel->cel_sub_id = ecs->ecs_sub_id;
  1420. cel->cel_chan_handle = eco->eco_lib_handle;
  1421. goto next_event;
  1422. }
  1423. }
  1424. }
  1425. /*
  1426. * No subscription filter matches anymore. We
  1427. * can delete this event.
  1428. */
  1429. list_del(&cel->cel_entry);
  1430. list_init(&cel->cel_entry);
  1431. esip->esi_nevents--;
  1432. free_event_data(cel->cel_event);
  1433. free(cel);
  1434. next_event:
  1435. continue;
  1436. }
  1437. }
  1438. }
  1439. /*
  1440. * Notify the library of a pending event
  1441. */
  1442. static void __notify_event(struct conn_info *conn_info)
  1443. {
  1444. struct res_evt_event_data res;
  1445. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1446. log_printf(LOG_LEVEL_DEBUG, "DELIVER: notify\n");
  1447. if (esip->esi_nevents != 0) {
  1448. res.evd_head.size = sizeof(res);
  1449. res.evd_head.id = MESSAGE_RES_EVT_AVAILABLE;
  1450. res.evd_head.error = SA_AIS_OK;
  1451. libais_send_response(conn_info, &res, sizeof(res));
  1452. }
  1453. }
  1454. inline void notify_event(struct conn_info *conn_info)
  1455. {
  1456. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1457. /*
  1458. * Give the library a kick if there aren't already
  1459. * events queued for delivery.
  1460. */
  1461. if (esip->esi_nevents++ == 0) {
  1462. __notify_event(conn_info);
  1463. }
  1464. }
  1465. /*
  1466. * sends/queues up an event for a subscribed channel.
  1467. */
  1468. static void
  1469. deliver_event(struct event_data *evt,
  1470. struct event_svr_channel_open *eco,
  1471. struct event_svr_channel_subscr *ecs)
  1472. {
  1473. struct chan_event_list *ep;
  1474. struct libevt_ci *esip = &eco->eco_conn_info->ais_ci.u.libevt_ci;
  1475. SaEvtEventPriorityT evt_prio = evt->ed_event.led_priority;
  1476. struct chan_event_list *cel;
  1477. int do_deliver_event = 0;
  1478. int do_deliver_warning = 0;
  1479. int i;
  1480. if (evt_prio > SA_EVT_LOWEST_PRIORITY) {
  1481. evt_prio = SA_EVT_LOWEST_PRIORITY;
  1482. }
  1483. /*
  1484. * Delivery queue check.
  1485. * - If the queue is blocked, see if we've sent enough messages to
  1486. * unblock it.
  1487. * - If it isn't blocked, see if this message will put us over the top.
  1488. * - If we can't deliver this message, see if we can toss some lower
  1489. * priority message to make room for this one.
  1490. * - If we toss any messages, queue up an event of SA_EVT_LOST_EVENT_PATTERN
  1491. * to let the application know that we dropped some messages.
  1492. */
  1493. if (esip->esi_queue_blocked) {
  1494. if (esip->esi_nevents < MIN_EVT_QUEUE_RESUME) {
  1495. esip->esi_queue_blocked = 0;
  1496. log_printf(LOG_LEVEL_DEBUG, "unblock\n");
  1497. }
  1498. }
  1499. if (!esip->esi_queue_blocked &&
  1500. (esip->esi_nevents >= MAX_EVT_DELIVERY_QUEUE)) {
  1501. log_printf(LOG_LEVEL_DEBUG, "block\n");
  1502. esip->esi_queue_blocked = 1;
  1503. do_deliver_warning = 1;
  1504. }
  1505. if (esip->esi_queue_blocked) {
  1506. do_deliver_event = 0;
  1507. for (i = SA_EVT_LOWEST_PRIORITY; i > evt_prio; i--) {
  1508. if (!list_empty(&esip->esi_events[i])) {
  1509. /*
  1510. * Get the last item on the list, so we drop the most
  1511. * recent lowest priority event.
  1512. */
  1513. cel = list_entry(esip->esi_events[i].prev,
  1514. struct chan_event_list, cel_entry);
  1515. log_printf(LOG_LEVEL_DEBUG, "Drop 0x%0llx\n",
  1516. cel->cel_event->ed_event.led_event_id);
  1517. list_del(&cel->cel_entry);
  1518. free_event_data(cel->cel_event);
  1519. free(cel);
  1520. esip->esi_nevents--;
  1521. do_deliver_event = 1;
  1522. break;
  1523. }
  1524. }
  1525. } else {
  1526. do_deliver_event = 1;
  1527. }
  1528. /*
  1529. * Queue the event for delivery
  1530. */
  1531. if (do_deliver_event) {
  1532. ep = malloc(sizeof(*ep));
  1533. if (!ep) {
  1534. log_printf(LOG_LEVEL_WARNING,
  1535. "3Memory allocation error, can't deliver event\n");
  1536. return;
  1537. }
  1538. evt->ed_ref_count++;
  1539. ep->cel_chan_handle = eco->eco_lib_handle;
  1540. ep->cel_sub_id = ecs->ecs_sub_id;
  1541. list_init(&ep->cel_entry);
  1542. ep->cel_event = evt;
  1543. list_add_tail(&ep->cel_entry, &esip->esi_events[evt_prio]);
  1544. evt_delivered(evt, eco);
  1545. notify_event(eco->eco_conn_info);
  1546. }
  1547. /*
  1548. * If we dropped an event, queue this so that the application knows
  1549. * what has happened.
  1550. */
  1551. if (do_deliver_warning) {
  1552. struct event_data *ed;
  1553. ed = malloc(dropped_event_size);
  1554. if (!ed) {
  1555. log_printf(LOG_LEVEL_WARNING,
  1556. "4Memory allocation error, can't deliver event\n");
  1557. return;
  1558. }
  1559. log_printf(LOG_LEVEL_DEBUG, "Warn 0x%0llx\n",
  1560. evt->ed_event.led_event_id);
  1561. memcpy(ed, dropped_event, dropped_event_size);
  1562. ed->ed_event.led_publish_time = clust_time_now();
  1563. list_init(&ed->ed_retained);
  1564. ep = malloc(sizeof(*ep));
  1565. if (!ep) {
  1566. log_printf(LOG_LEVEL_WARNING,
  1567. "5Memory allocation error, can't deliver event\n");
  1568. return;
  1569. }
  1570. ep->cel_chan_handle = eco->eco_lib_handle;
  1571. ep->cel_sub_id = ecs->ecs_sub_id;
  1572. list_init(&ep->cel_entry);
  1573. ep->cel_event = ed;
  1574. list_add_tail(&ep->cel_entry, &esip->esi_events[SA_EVT_HIGHEST_PRIORITY]);
  1575. notify_event(eco->eco_conn_info);
  1576. }
  1577. }
  1578. /*
  1579. * Take the event data and swap the elements so they match our architectures
  1580. * word order.
  1581. */
  1582. static void
  1583. convert_event(struct lib_event_data *evt)
  1584. {
  1585. SaEvtEventPatternT *eps;
  1586. int i;
  1587. /*
  1588. * The following elements don't require processing:
  1589. *
  1590. * converted in the main deliver_fn:
  1591. * led_head.id, led_head.size.
  1592. *
  1593. * Supplied localy:
  1594. * source_addr, publisher_node_id, receive_time.
  1595. *
  1596. * Used internaly only:
  1597. * led_svr_channel_handle and led_lib_channel_handle.
  1598. */
  1599. evt->led_chan_name.length = swab16(evt->led_chan_name.length);
  1600. evt->led_event_id = swab64(evt->led_event_id);
  1601. evt->led_sub_id = swab32(evt->led_sub_id);
  1602. evt->led_publisher_name.length = swab32(evt->led_publisher_name.length);
  1603. evt->led_retention_time = swab64(evt->led_retention_time);
  1604. evt->led_publish_time = swab64(evt->led_publish_time);
  1605. evt->led_user_data_offset = swab32(evt->led_user_data_offset);
  1606. evt->led_user_data_size = swab32(evt->led_user_data_size);
  1607. evt->led_patterns_number = swab32(evt->led_patterns_number);
  1608. /*
  1609. * Now we need to go through the led_body and swizzle pattern counts.
  1610. * We can't do anything about user data since it doesn't have a specified
  1611. * format. The application is on its own here.
  1612. */
  1613. eps = (SaEvtEventPatternT *)evt->led_body;
  1614. for (i = 0; i < evt->led_patterns_number; i++) {
  1615. eps->patternSize = swab32(eps->patternSize);
  1616. eps++;
  1617. }
  1618. }
  1619. /*
  1620. * Take an event received from the network and fix it up to be usable.
  1621. * - fix up pointers for pattern list.
  1622. * - fill in some channel info
  1623. */
  1624. static struct event_data *
  1625. make_local_event(struct lib_event_data *p,
  1626. struct event_svr_channel_instance *eci)
  1627. {
  1628. struct event_data *ed;
  1629. SaEvtEventPatternT *eps;
  1630. SaUint8T *str;
  1631. uint32_t ed_size;
  1632. int i;
  1633. ed_size = sizeof(*ed) + p->led_user_data_offset + p->led_user_data_size;
  1634. ed = malloc(ed_size);
  1635. if (!ed) {
  1636. return 0;
  1637. }
  1638. memset(ed, 0, ed_size);
  1639. list_init(&ed->ed_retained);
  1640. ed->ed_my_chan = eci;
  1641. /*
  1642. * Fill in lib_event_data and make the pattern pointers valid
  1643. */
  1644. memcpy(&ed->ed_event, p, sizeof(*p) +
  1645. p->led_user_data_offset + p->led_user_data_size);
  1646. eps = (SaEvtEventPatternT *)ed->ed_event.led_body;
  1647. str = ed->ed_event.led_body +
  1648. (ed->ed_event.led_patterns_number * sizeof(SaEvtEventPatternT));
  1649. for (i = 0; i < ed->ed_event.led_patterns_number; i++) {
  1650. eps->pattern = str;
  1651. str += eps->patternSize;
  1652. eps++;
  1653. }
  1654. ed->ed_ref_count++;
  1655. return ed;
  1656. }
  1657. /*
  1658. * Set an event to be retained.
  1659. */
  1660. static void retain_event(struct event_data *evt)
  1661. {
  1662. uint32_t ret;
  1663. int msec_in_future;
  1664. evt->ed_ref_count++;
  1665. evt->ed_my_chan->esc_retained_count++;
  1666. list_add_tail(&evt->ed_retained, &retained_list);
  1667. /*
  1668. * Time in nanoseconds - convert to miliseconds
  1669. */
  1670. msec_in_future = (uint32_t)((evt->ed_event.led_retention_time) / 1000000ULL);
  1671. ret = poll_timer_add(aisexec_poll_handle,
  1672. msec_in_future,
  1673. evt,
  1674. event_retention_timeout,
  1675. &evt->ed_timer_handle);
  1676. if (ret != 0) {
  1677. log_printf(LOG_LEVEL_ERROR,
  1678. "retention of event id 0x%llx failed\n",
  1679. evt->ed_event.led_event_id);
  1680. } else {
  1681. log_printf(LOG_LEVEL_DEBUG, "Retain event ID 0x%llx\n",
  1682. evt->ed_event.led_event_id);
  1683. }
  1684. }
  1685. /*
  1686. * Scan the subscription list and look for the specified subsctiption ID.
  1687. * Only look for the ID in subscriptions that are associated with the
  1688. * saEvtInitialize associated with the specified open channel.
  1689. */
  1690. static struct event_svr_channel_subscr *find_subscr(
  1691. struct event_svr_channel_open *open_chan, SaEvtSubscriptionIdT sub_id)
  1692. {
  1693. struct event_svr_channel_instance *eci;
  1694. struct event_svr_channel_subscr *ecs;
  1695. struct event_svr_channel_open *eco;
  1696. struct list_head *l, *l1;
  1697. struct conn_info* conn_info = open_chan->eco_conn_info;
  1698. eci = open_chan->eco_channel;
  1699. /*
  1700. * Check for subscription id already in use.
  1701. * Subscriptions are unique within saEvtInitialize (Callback scope).
  1702. */
  1703. for (l = eci->esc_open_chans.next; l != &eci->esc_open_chans; l = l->next) {
  1704. eco = list_entry(l, struct event_svr_channel_open, eco_entry);
  1705. /*
  1706. * Don't bother with open channels associated with another
  1707. * EvtInitialize
  1708. */
  1709. if (eco->eco_conn_info != conn_info) {
  1710. continue;
  1711. }
  1712. for (l1 = eco->eco_subscr.next; l1 != &eco->eco_subscr; l1 = l1->next) {
  1713. ecs = list_entry(l1, struct event_svr_channel_subscr, ecs_entry);
  1714. if (ecs->ecs_sub_id == sub_id) {
  1715. return ecs;
  1716. }
  1717. }
  1718. }
  1719. return 0;
  1720. }
  1721. /*
  1722. * Handler for saEvtInitialize
  1723. */
  1724. static int evt_initialize(struct conn_info *conn_info, void *msg)
  1725. {
  1726. struct res_lib_init res;
  1727. struct libevt_ci *libevt_ci = &conn_info->ais_ci.u.libevt_ci;
  1728. int i;
  1729. res.header.size = sizeof (struct res_lib_init);
  1730. res.header.id = MESSAGE_RES_INIT;
  1731. res.header.error = SA_AIS_OK;
  1732. log_printf(LOG_LEVEL_DEBUG, "saEvtInitialize request.\n");
  1733. if (!conn_info->authenticated) {
  1734. log_printf(LOG_LEVEL_ERROR, "event service: Not authenticated\n");
  1735. res.header.error = SA_AIS_ERR_LIBRARY;
  1736. libais_send_response(conn_info, &res, sizeof(res));
  1737. return -1;
  1738. }
  1739. memset(libevt_ci, 0, sizeof(*libevt_ci));
  1740. list_init(&libevt_ci->esi_open_chans);
  1741. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  1742. list_init(&libevt_ci->esi_events[i]);
  1743. }
  1744. conn_info->service = SOCKET_SERVICE_EVT;
  1745. list_init (&conn_info->conn_list);
  1746. list_add_tail(&conn_info->conn_list, &ci_head);
  1747. libais_send_response (conn_info, &res, sizeof(res));
  1748. return 0;
  1749. }
  1750. /*
  1751. * Handler for saEvtChannelOpen
  1752. */
  1753. static int lib_evt_open_channel(struct conn_info *conn_info, void *message)
  1754. {
  1755. SaErrorT error;
  1756. struct req_evt_channel_open *req;
  1757. struct res_evt_channel_open res;
  1758. struct open_chan_pending *ocp;
  1759. int msec_in_future;
  1760. int ret;
  1761. req = message;
  1762. log_printf(CHAN_OPEN_DEBUG,
  1763. "saEvtChannelOpen (Open channel request)\n");
  1764. log_printf(CHAN_OPEN_DEBUG,
  1765. "handle 0x%x, to 0x%llx\n",
  1766. req->ico_c_handle,
  1767. req->ico_timeout);
  1768. log_printf(CHAN_OPEN_DEBUG, "flags %x, channel name(%d) %s\n",
  1769. req->ico_open_flag,
  1770. req->ico_channel_name.length,
  1771. req->ico_channel_name.value);
  1772. /*
  1773. * Open the channel.
  1774. *
  1775. */
  1776. error = evt_open_channel(&req->ico_channel_name, req->ico_open_flag);
  1777. if (error != SA_AIS_OK) {
  1778. goto open_return;
  1779. }
  1780. ocp = malloc(sizeof(struct open_chan_pending));
  1781. if (!ocp) {
  1782. error = SA_AIS_ERR_NO_MEMORY;
  1783. goto open_return;
  1784. }
  1785. ocp->ocp_async = 0;
  1786. ocp->ocp_invocation = 0;
  1787. ocp->ocp_chan_name = req->ico_channel_name;
  1788. ocp->ocp_open_flag = req->ico_open_flag;
  1789. ocp->ocp_conn_info = conn_info;
  1790. ocp->ocp_timer_handle = 0;
  1791. list_init(&ocp->ocp_entry);
  1792. list_add_tail(&ocp->ocp_entry, &open_pending);
  1793. if (req->ico_timeout != 0) {
  1794. msec_in_future = (uint32_t)(req->ico_timeout / 1000000ULL);
  1795. ret = poll_timer_add(aisexec_poll_handle,
  1796. msec_in_future,
  1797. ocp,
  1798. chan_open_timeout,
  1799. &ocp->ocp_timer_handle);
  1800. if (ret != 0) {
  1801. log_printf(LOG_LEVEL_WARNING,
  1802. "Error setting timeout for open channel %s\n",
  1803. req->ico_channel_name.value);
  1804. }
  1805. }
  1806. return 0;
  1807. open_return:
  1808. res.ico_head.size = sizeof(res);
  1809. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  1810. res.ico_head.error = error;
  1811. libais_send_response (conn_info, &res, sizeof(res));
  1812. return 0;
  1813. }
  1814. /*
  1815. * Used by the channel close code and by the implicit close
  1816. * when saEvtFinalize is called with channels open.
  1817. */
  1818. static SaErrorT
  1819. common_chan_close(struct event_svr_channel_open *eco, struct libevt_ci *esip)
  1820. {
  1821. struct event_svr_channel_subscr *ecs;
  1822. struct list_head *l, *nxt;
  1823. log_printf(LOG_LEVEL_DEBUG, "Close channel %s flags 0x%02x\n",
  1824. eco->eco_channel->esc_channel_name.value,
  1825. eco->eco_flags);
  1826. /*
  1827. * Unlink the channel open structure.
  1828. *
  1829. * Check for subscriptions and deal with them. In this case
  1830. * if there are any, we just implicitly unsubscribe.
  1831. *
  1832. * When We're done with the channel open data then we can
  1833. * remove it's handle (this frees the memory too).
  1834. *
  1835. */
  1836. list_del(&eco->eco_entry);
  1837. list_del(&eco->eco_instance_entry);
  1838. for (l = eco->eco_subscr.next; l != &eco->eco_subscr; l = nxt) {
  1839. nxt = l->next;
  1840. ecs = list_entry(l, struct event_svr_channel_subscr, ecs_entry);
  1841. log_printf(LOG_LEVEL_DEBUG, "Unsubscribe ID: %x\n",
  1842. ecs->ecs_sub_id);
  1843. list_del(&ecs->ecs_entry);
  1844. free(ecs);
  1845. /*
  1846. * Purge any pending events associated with this subscription
  1847. * that don't match another subscription.
  1848. */
  1849. filter_undelivered_events(eco);
  1850. }
  1851. /*
  1852. * Remove this channel from the retained event's notion
  1853. * of who they have been delivered to.
  1854. */
  1855. remove_delivered_channel(eco);
  1856. return evt_close_channel(&eco->eco_channel->esc_channel_name);
  1857. }
  1858. /*
  1859. * Handler for saEvtChannelClose
  1860. */
  1861. static int lib_evt_close_channel(struct conn_info *conn_info, void *message)
  1862. {
  1863. struct req_evt_channel_close *req;
  1864. struct res_evt_channel_close res;
  1865. struct event_svr_channel_open *eco;
  1866. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1867. SaErrorT error;
  1868. void *ptr;
  1869. req = message;
  1870. log_printf(LOG_LEVEL_DEBUG,
  1871. "saEvtChannelClose (Close channel request)\n");
  1872. log_printf(LOG_LEVEL_DEBUG, "handle 0x%x\n", req->icc_channel_handle);
  1873. /*
  1874. * look up the channel handle
  1875. */
  1876. error = saHandleInstanceGet(&esip->esi_hdb,
  1877. req->icc_channel_handle, &ptr);
  1878. if (error != SA_AIS_OK) {
  1879. goto chan_close_done;
  1880. }
  1881. eco = ptr;
  1882. common_chan_close(eco, esip);
  1883. saHandleDestroy(&esip->esi_hdb, req->icc_channel_handle);
  1884. saHandleInstancePut(&esip->esi_hdb, req->icc_channel_handle);
  1885. chan_close_done:
  1886. res.icc_head.size = sizeof(res);
  1887. res.icc_head.id = MESSAGE_RES_EVT_CLOSE_CHANNEL;
  1888. res.icc_head.error = error;
  1889. libais_send_response (conn_info, &res, sizeof(res));
  1890. return 0;
  1891. }
  1892. /*
  1893. * Subscribe to an event channel.
  1894. *
  1895. * - First look up the channel to subscribe.
  1896. * - Make sure that the subscription ID is not already in use.
  1897. * - Fill in the subscription data structures and add them to the channels
  1898. * subscription list.
  1899. * - See if there are any events with retetion times that need to be delivered
  1900. * because of the new subscription.
  1901. */
  1902. static char *filter_types[] = {
  1903. "INVALID FILTER TYPE",
  1904. "SA_EVT_PREFIX_FILTER",
  1905. "SA_EVT_SUFFIX_FILTER",
  1906. "SA_EVT_EXACT_FILTER",
  1907. "SA_EVT_PASS_ALL_FILTER",
  1908. };
  1909. /*
  1910. * saEvtEventSubscribe Handler
  1911. */
  1912. static int lib_evt_event_subscribe(struct conn_info *conn_info, void *message)
  1913. {
  1914. struct req_evt_event_subscribe *req;
  1915. struct res_evt_event_subscribe res;
  1916. SaEvtEventFilterArrayT *filters;
  1917. SaErrorT error = SA_AIS_OK;
  1918. struct event_svr_channel_open *eco;
  1919. struct event_svr_channel_instance *eci;
  1920. struct event_svr_channel_subscr *ecs;
  1921. struct event_data *evt;
  1922. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1923. struct list_head *l;
  1924. void *ptr;
  1925. int i;
  1926. req = message;
  1927. log_printf(LOG_LEVEL_DEBUG,
  1928. "saEvtEventSubscribe (Subscribe request)\n");
  1929. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%x\n",
  1930. req->ics_sub_id);
  1931. error = evtfilt_to_aisfilt(req, &filters);
  1932. if (error == SA_AIS_OK) {
  1933. log_printf(LOG_LEVEL_DEBUG, "Subscribe filters count %d\n",
  1934. filters->filtersNumber);
  1935. for (i = 0; i < filters->filtersNumber; i++) {
  1936. log_printf(LOG_LEVEL_DEBUG, "type %s(%d) sz %d, <%s>\n",
  1937. filter_types[filters->filters[i].filterType],
  1938. filters->filters[i].filterType,
  1939. filters->filters[i].filter.patternSize,
  1940. (filters->filters[i].filter.patternSize)
  1941. ? (char *)filters->filters[i].filter.pattern
  1942. : "");
  1943. }
  1944. }
  1945. if (error != SA_AIS_OK) {
  1946. goto subr_done;
  1947. }
  1948. /*
  1949. * look up the channel handle
  1950. */
  1951. error = saHandleInstanceGet(&esip->esi_hdb,
  1952. req->ics_channel_handle, &ptr);
  1953. if (error != SA_AIS_OK) {
  1954. goto subr_done;
  1955. }
  1956. eco = ptr;
  1957. eci = eco->eco_channel;
  1958. /*
  1959. * See if the id is already being used
  1960. */
  1961. ecs = find_subscr(eco, req->ics_sub_id);
  1962. if (ecs) {
  1963. error = SA_AIS_ERR_EXIST;
  1964. goto subr_put;
  1965. }
  1966. ecs = (struct event_svr_channel_subscr *)malloc(sizeof(*ecs));
  1967. if (!ecs) {
  1968. error = SA_AIS_ERR_NO_MEMORY;
  1969. goto subr_put;
  1970. }
  1971. ecs->ecs_filters = filters;
  1972. ecs->ecs_sub_id = req->ics_sub_id;
  1973. list_init(&ecs->ecs_entry);
  1974. list_add(&ecs->ecs_entry, &eco->eco_subscr);
  1975. res.ics_head.size = sizeof(res);
  1976. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  1977. res.ics_head.error = error;
  1978. libais_send_response (conn_info, &res, sizeof(res));
  1979. /*
  1980. * See if an existing event with a retention time
  1981. * needs to be delivered based on this subscription
  1982. */
  1983. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1984. evt = list_entry(l, struct event_data, ed_retained);
  1985. log_printf(LOG_LEVEL_DEBUG,
  1986. "Checking event ID %llx chanp %p -- sub chanp %p\n",
  1987. evt->ed_event.led_event_id, evt->ed_my_chan, eci);
  1988. if (evt->ed_my_chan == eci) {
  1989. if (evt_already_delivered(evt, eco)) {
  1990. continue;
  1991. }
  1992. if (event_match(evt, ecs) == SA_AIS_OK) {
  1993. log_printf(LOG_LEVEL_DEBUG,
  1994. "deliver event ID: 0x%llx\n",
  1995. evt->ed_event.led_event_id);
  1996. deliver_event(evt, eco, ecs);
  1997. }
  1998. }
  1999. }
  2000. saHandleInstancePut(&esip->esi_hdb, req->ics_channel_handle);
  2001. return 0;
  2002. subr_put:
  2003. saHandleInstancePut(&esip->esi_hdb, req->ics_channel_handle);
  2004. subr_done:
  2005. res.ics_head.size = sizeof(res);
  2006. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  2007. res.ics_head.error = error;
  2008. libais_send_response (conn_info, &res, sizeof(res));
  2009. return 0;
  2010. }
  2011. /*
  2012. * saEvtEventUnsubscribe Handler
  2013. */
  2014. static int lib_evt_event_unsubscribe(struct conn_info *conn_info,
  2015. void *message)
  2016. {
  2017. struct req_evt_event_unsubscribe *req;
  2018. struct res_evt_event_unsubscribe res;
  2019. struct event_svr_channel_open *eco;
  2020. struct event_svr_channel_instance *eci;
  2021. struct event_svr_channel_subscr *ecs;
  2022. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2023. SaErrorT error = SA_AIS_OK;
  2024. void *ptr;
  2025. req = message;
  2026. log_printf(LOG_LEVEL_DEBUG,
  2027. "saEvtEventUnsubscribe (Unsubscribe request)\n");
  2028. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%x\n",
  2029. req->icu_sub_id);
  2030. /*
  2031. * look up the channel handle, get the open channel
  2032. * data.
  2033. */
  2034. error = saHandleInstanceGet(&esip->esi_hdb,
  2035. req->icu_channel_handle, &ptr);
  2036. if (error != SA_AIS_OK) {
  2037. goto unsubr_done;
  2038. }
  2039. eco = ptr;
  2040. eci = eco->eco_channel;
  2041. /*
  2042. * Make sure that the id exists.
  2043. */
  2044. ecs = find_subscr(eco, req->icu_sub_id);
  2045. if (!ecs) {
  2046. error = SA_AIS_ERR_INVALID_PARAM;
  2047. goto unsubr_put;
  2048. }
  2049. list_del(&ecs->ecs_entry);
  2050. log_printf(LOG_LEVEL_DEBUG,
  2051. "unsubscribe from channel %s subscription ID 0x%x "
  2052. "with %d filters\n",
  2053. eci->esc_channel_name.value,
  2054. ecs->ecs_sub_id, ecs->ecs_filters->filtersNumber);
  2055. free_filters(ecs->ecs_filters);
  2056. free(ecs);
  2057. unsubr_put:
  2058. saHandleInstancePut(&esip->esi_hdb, req->icu_channel_handle);
  2059. unsubr_done:
  2060. res.icu_head.size = sizeof(res);
  2061. res.icu_head.id = MESSAGE_RES_EVT_UNSUBSCRIBE;
  2062. res.icu_head.error = error;
  2063. libais_send_response (conn_info, &res, sizeof(res));
  2064. return 0;
  2065. }
  2066. /*
  2067. * saEvtEventPublish Handler
  2068. */
  2069. static int lib_evt_event_publish(struct conn_info *conn_info, void *message)
  2070. {
  2071. struct lib_event_data *req;
  2072. struct res_evt_event_publish res;
  2073. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2074. struct event_svr_channel_open *eco;
  2075. struct event_svr_channel_instance *eci;
  2076. SaEvtEventIdT event_id = 0;
  2077. SaErrorT error = SA_AIS_OK;
  2078. struct iovec pub_iovec;
  2079. void *ptr;
  2080. int result;
  2081. req = message;
  2082. log_printf(LOG_LEVEL_DEBUG,
  2083. "saEvtEventPublish (Publish event request)\n");
  2084. /*
  2085. * look up and validate open channel info
  2086. */
  2087. error = saHandleInstanceGet(&esip->esi_hdb,
  2088. req->led_svr_channel_handle, &ptr);
  2089. if (error != SA_AIS_OK) {
  2090. goto pub_done;
  2091. }
  2092. eco = ptr;
  2093. eci = eco->eco_channel;
  2094. /*
  2095. * modify the request structure for sending event data to subscribed
  2096. * processes.
  2097. */
  2098. get_event_id(&event_id);
  2099. req->led_head.id = MESSAGE_REQ_EXEC_EVT_EVENTDATA;
  2100. req->led_chan_name = eci->esc_channel_name;
  2101. req->led_event_id = event_id;
  2102. /*
  2103. * Distribute the event.
  2104. * The multicasted event will be picked up and delivered
  2105. * locally by the local network event receiver.
  2106. */
  2107. pub_iovec.iov_base = req;
  2108. pub_iovec.iov_len = req->led_head.size;
  2109. result = totempg_mcast (&pub_iovec, 1, TOTEMPG_AGREED);
  2110. if (result != 0) {
  2111. error = SA_AIS_ERR_LIBRARY;
  2112. }
  2113. saHandleInstancePut(&esip->esi_hdb, req->led_svr_channel_handle);
  2114. pub_done:
  2115. res.iep_head.size = sizeof(res);
  2116. res.iep_head.id = MESSAGE_RES_EVT_PUBLISH;
  2117. res.iep_head.error = error;
  2118. res.iep_event_id = event_id;
  2119. libais_send_response (conn_info, &res, sizeof(res));
  2120. return 0;
  2121. }
  2122. /*
  2123. * saEvtEventRetentionTimeClear handler
  2124. */
  2125. static int lib_evt_event_clear_retentiontime(struct conn_info *conn_info,
  2126. void *message)
  2127. {
  2128. struct req_evt_event_clear_retentiontime *req;
  2129. struct res_evt_event_clear_retentiontime res;
  2130. struct req_evt_chan_command cpkt;
  2131. struct iovec rtn_iovec;
  2132. SaErrorT error = SA_AIS_OK;
  2133. int ret;
  2134. req = message;
  2135. log_printf(LOG_LEVEL_DEBUG,
  2136. "saEvtEventRetentionTimeClear (Clear event retentiontime request)\n");
  2137. log_printf(LOG_LEVEL_DEBUG,
  2138. "event ID 0x%llx, chan handle 0x%x\n",
  2139. req->iec_event_id,
  2140. req->iec_channel_handle);
  2141. memset(&cpkt, 0, sizeof(cpkt));
  2142. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  2143. cpkt.chc_head.size = sizeof(cpkt);
  2144. cpkt.chc_op = EVT_CLEAR_RET_OP;
  2145. cpkt.u.chc_event_id = req->iec_event_id;
  2146. rtn_iovec.iov_base = &cpkt;
  2147. rtn_iovec.iov_len = cpkt.chc_head.size;
  2148. ret = totempg_mcast (&rtn_iovec, 1, TOTEMPG_AGREED);
  2149. if (ret != 0) {
  2150. error = SA_AIS_ERR_LIBRARY;
  2151. }
  2152. res.iec_head.size = sizeof(res);
  2153. res.iec_head.id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME;
  2154. res.iec_head.error = error;
  2155. libais_send_response (conn_info, &res, sizeof(res));
  2156. return 0;
  2157. }
  2158. /*
  2159. * Send requested event data to the application
  2160. */
  2161. static int lib_evt_event_data_get(struct conn_info *conn_info, void *message)
  2162. {
  2163. struct lib_event_data res;
  2164. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2165. struct chan_event_list *cel;
  2166. struct event_data *edp;
  2167. int i;
  2168. /*
  2169. * Deliver events in publish order within priority
  2170. */
  2171. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  2172. if (!list_empty(&esip->esi_events[i])) {
  2173. cel = list_entry(esip->esi_events[i].next, struct chan_event_list,
  2174. cel_entry);
  2175. list_del(&cel->cel_entry);
  2176. list_init(&cel->cel_entry);
  2177. esip->esi_nevents--;
  2178. if (esip->esi_queue_blocked &&
  2179. (esip->esi_nevents < MIN_EVT_QUEUE_RESUME)) {
  2180. esip->esi_queue_blocked = 0;
  2181. log_printf(LOG_LEVEL_DEBUG, "unblock\n");
  2182. }
  2183. edp = cel->cel_event;
  2184. edp->ed_event.led_lib_channel_handle = cel->cel_chan_handle;
  2185. edp->ed_event.led_sub_id = cel->cel_sub_id;
  2186. edp->ed_event.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2187. edp->ed_event.led_head.error = SA_AIS_OK;
  2188. free(cel);
  2189. libais_send_response(conn_info, &edp->ed_event,
  2190. edp->ed_event.led_head.size);
  2191. free_event_data(edp);
  2192. goto data_get_done;
  2193. }
  2194. }
  2195. res.led_head.size = sizeof(res.led_head);
  2196. res.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2197. res.led_head.error = SA_AIS_ERR_NOT_EXIST;
  2198. libais_send_response(conn_info, &res, res.led_head.size);
  2199. /*
  2200. * See if there are any events that the app doesn't know about
  2201. * because the notify pipe was full.
  2202. */
  2203. data_get_done:
  2204. if (esip->esi_nevents) {
  2205. __notify_event(conn_info);
  2206. }
  2207. return 0;
  2208. }
  2209. /*
  2210. * Scan the list of channels and remove the specified node.
  2211. */
  2212. static void remove_chan_open_info(SaClmNodeIdT node_id)
  2213. {
  2214. struct list_head *l, *nxt;
  2215. struct event_svr_channel_instance *eci;
  2216. for (l = esc_head.next; l != &esc_head; l = nxt) {
  2217. nxt = l->next;
  2218. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  2219. remove_open_count(eci, node_id);
  2220. }
  2221. }
  2222. /*
  2223. * Called when there is a configuration change in the cluster.
  2224. * This function looks at any joiners and leavers and updates the evt
  2225. * node list. The node list is used to keep track of event IDs
  2226. * received for each node for the detection of duplicate events.
  2227. */
  2228. static int evt_conf_change(
  2229. enum totempg_configuration_type configuration_type,
  2230. struct in_addr *member_list, void *member_list_private,
  2231. int member_list_entries,
  2232. struct in_addr *left_list, void *left_list_private,
  2233. int left_list_entries,
  2234. struct in_addr *joined_list, void *joined_list_private,
  2235. int joined_list_entries)
  2236. {
  2237. struct in_addr my_node = {SA_CLM_LOCAL_NODE_ID};
  2238. SaClmClusterNodeT *cn;
  2239. static int first = 1;
  2240. struct sockaddr_in *add_list;
  2241. struct member_node_data *md;
  2242. int add_count;
  2243. struct req_evt_chan_command cpkt;
  2244. struct iovec chn_iovec;
  2245. int res;
  2246. /*
  2247. * TODO required for open count accounting
  2248. * until the recovery code is re-enabled.
  2249. */
  2250. total_members = member_list_entries;
  2251. /*
  2252. * Set the base event id
  2253. */
  2254. cn = clm_get_by_nodeid(my_node);
  2255. if (!base_id_top) {
  2256. log_printf(RECOVERY_DEBUG, "My node ID 0x%x\n", cn->nodeId);
  2257. my_node_id = cn->nodeId;
  2258. set_event_id(my_node_id);
  2259. }
  2260. return (0); // TODO
  2261. log_printf(LOG_LEVEL_DEBUG, "Evt conf change %d\n",
  2262. configuration_type);
  2263. log_printf(LOG_LEVEL_DEBUG, "m %d, j %d, l %d\n",
  2264. member_list_entries,
  2265. joined_list_entries,
  2266. left_list_entries);
  2267. /*
  2268. * Stop any recovery callbacks in progress.
  2269. */
  2270. if (tok_call_handle) {
  2271. // TODO totempg_token_callback_destroy(tok_call_handle);
  2272. tok_call_handle = 0;
  2273. }
  2274. /*
  2275. * Don't seem to be able to tell who joined if we're just coming up. Not all
  2276. * nodes show up in the join list. If this is the first time through,
  2277. * choose the members list to use to add nodes, after that use the join
  2278. * list. Always use the left list for removing nodes.
  2279. */
  2280. if (first) {
  2281. //j add_list = member_list;
  2282. // add_count = member_list_entries;
  2283. first = 0;
  2284. } else {
  2285. // add_list = joined_list;
  2286. // add_count = joined_list_entries;
  2287. }
  2288. while (add_count--) {
  2289. /*
  2290. * If we've seen this node before, send out the last event ID
  2291. * that we've seen from him. He will set his base event ID to
  2292. * the highest one seen.
  2293. */
  2294. md = evt_find_node(add_list->sin_addr);
  2295. if (md != NULL) {
  2296. if (!md->mn_started) {
  2297. log_printf(RECOVERY_DEBUG,
  2298. "end set evt ID %llx to %s\n",
  2299. md->mn_last_evt_id, inet_ntoa(add_list->sin_addr));
  2300. md->mn_started = 1;
  2301. memset(&cpkt, 0, sizeof(cpkt));
  2302. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  2303. cpkt.chc_head.size = sizeof(cpkt);
  2304. cpkt.chc_op = EVT_SET_ID_OP;
  2305. cpkt.u.chc_set_id.chc_addr = add_list->sin_addr;
  2306. cpkt.u.chc_set_id.chc_last_id =
  2307. md->mn_last_evt_id & BASE_ID_MASK;
  2308. chn_iovec.iov_base = &cpkt;
  2309. chn_iovec.iov_len = cpkt.chc_head.size;
  2310. res = totempg_mcast (&chn_iovec, 1,TOTEMPG_AGREED);
  2311. if (res != 0) {
  2312. log_printf(LOG_LEVEL_WARNING,
  2313. "Unable to send event id to %s\n",
  2314. inet_ntoa(add_list->sin_addr));
  2315. }
  2316. }
  2317. }
  2318. add_list++;
  2319. }
  2320. while (left_list_entries--) {
  2321. // TODO md = evt_find_node(left_list);
  2322. if (md == 0) {
  2323. log_printf(LOG_LEVEL_WARNING,
  2324. "Can't find cluster node at %s\n",
  2325. inet_ntoa(left_list[0]));
  2326. /*
  2327. * Mark this one as down.
  2328. */
  2329. } else {
  2330. log_printf(RECOVERY_DEBUG, "cluster node at %s down\n",
  2331. inet_ntoa(left_list[0]));
  2332. md->mn_started = 0;
  2333. remove_chan_open_info(md->mn_node_info.nodeId);
  2334. }
  2335. left_list++;
  2336. }
  2337. /*
  2338. * Notify that a config change happened. The exec handler will
  2339. * then determine what to do.
  2340. */
  2341. if (configuration_type == TOTEMPG_CONFIGURATION_REGULAR) {
  2342. if (in_cfg_change) {
  2343. log_printf(LOG_LEVEL_NOTICE,
  2344. "Already in config change, Starting over, m %d, c %d\n",
  2345. total_members, checked_in);
  2346. }
  2347. in_cfg_change = 1;
  2348. total_members = member_list_entries;
  2349. checked_in = 0;
  2350. any_joined = joined_list_entries;
  2351. /*
  2352. * Start by updating all the nodes on our
  2353. * open channel count. Once that is done, proceed to determining who
  2354. * sends ratained events. Then we can start normal operation again.
  2355. */
  2356. send_open_count();
  2357. }
  2358. return 0;
  2359. }
  2360. /*
  2361. * saEvtFinalize Handler
  2362. */
  2363. static int evt_finalize(struct conn_info *conn_info)
  2364. {
  2365. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2366. struct event_svr_channel_open *eco;
  2367. struct list_head *l, *nxt;
  2368. log_printf(LOG_LEVEL_DEBUG, "saEvtFinalize (Event exit request)\n");
  2369. log_printf(LOG_LEVEL_DEBUG, "saEvtFinalize %d evts on list\n",
  2370. esip->esi_nevents);
  2371. /*
  2372. * Clean up any open channels and associated subscriptions.
  2373. */
  2374. for (l = esip->esi_open_chans.next; l != &esip->esi_open_chans; l = nxt) {
  2375. nxt = l->next;
  2376. eco = list_entry(l, struct event_svr_channel_open, eco_instance_entry);
  2377. common_chan_close(eco, esip);
  2378. saHandleDestroy(&esip->esi_hdb, eco->eco_my_handle);
  2379. }
  2380. /*
  2381. * Delete track entry if there is one
  2382. */
  2383. list_del (&conn_info->conn_list);
  2384. return 0;
  2385. }
  2386. /*
  2387. * Called at service start time.
  2388. */
  2389. static int evt_exec_init(void)
  2390. {
  2391. log_printf(LOG_LEVEL_DEBUG, "Evt exec init request\n");
  2392. #ifdef TODO
  2393. int res;
  2394. res = totempg_recovery_plug_create (&evt_recovery_plug_handle);
  2395. if (res != 0) {
  2396. log_printf(LOG_LEVEL_ERROR,
  2397. "Could not create recovery plug for event service.\n");
  2398. return (-1);
  2399. }
  2400. #endif
  2401. /*
  2402. * Create an event to be sent when we have to drop messages
  2403. * for an application.
  2404. */
  2405. dropped_event_size = sizeof(*dropped_event) + sizeof(dropped_pattern);
  2406. dropped_event = malloc(dropped_event_size);
  2407. if (dropped_event == 0) {
  2408. log_printf(LOG_LEVEL_ERROR,
  2409. "Memory Allocation Failure, event service not started\n");
  2410. // TODO res = totempg_recovery_plug_destroy (evt_recovery_plug_handle);
  2411. errno = ENOMEM;
  2412. return -1;
  2413. }
  2414. memset(dropped_event, 0, sizeof(*dropped_event) + sizeof(dropped_pattern));
  2415. dropped_event->ed_ref_count = 1;
  2416. list_init(&dropped_event->ed_retained);
  2417. dropped_event->ed_event.led_head.size =
  2418. sizeof(*dropped_event) + sizeof(dropped_pattern);
  2419. dropped_event->ed_event.led_head.error = SA_AIS_OK;
  2420. dropped_event->ed_event.led_priority = SA_EVT_HIGHEST_PRIORITY;
  2421. dropped_event->ed_event.led_chan_name = lost_chan;
  2422. dropped_event->ed_event.led_publisher_name = dropped_publisher;
  2423. dropped_event->ed_event.led_patterns_number = 1;
  2424. memcpy(&dropped_event->ed_event.led_body[0],
  2425. &dropped_pattern, sizeof(dropped_pattern));
  2426. return 0;
  2427. }
  2428. /*
  2429. * Receive the network event message and distribute it to local subscribers
  2430. */
  2431. static int evt_remote_evt(void *msg, struct in_addr source_addr,
  2432. int endian_conversion_required)
  2433. {
  2434. /*
  2435. * - retain events that have a retention time
  2436. * - Find assocated channel
  2437. * - Scan list of subscribers
  2438. * - Apply filters
  2439. * - Deliver events that pass the filter test
  2440. */
  2441. struct lib_event_data *evtpkt = msg;
  2442. struct event_svr_channel_instance *eci;
  2443. struct event_svr_channel_open *eco;
  2444. struct event_svr_channel_subscr *ecs;
  2445. struct event_data *evt;
  2446. struct list_head *l, *l1;
  2447. SaClmClusterNodeT *cn;
  2448. log_printf(LOG_LEVEL_DEBUG, "Remote event data received from %s\n",
  2449. inet_ntoa(source_addr));
  2450. /*
  2451. * See where the message came from so that we can set the
  2452. * publishing node id in the message before delivery.
  2453. */
  2454. cn = clm_get_by_nodeid (source_addr);
  2455. if (!cn) {
  2456. /*
  2457. * Not sure how this can happen...
  2458. */
  2459. log_printf(LOG_LEVEL_NOTICE, "No cluster node data for %s\n",
  2460. inet_ntoa(source_addr));
  2461. errno = ENXIO;
  2462. return -1;
  2463. }
  2464. log_printf(LOG_LEVEL_DEBUG, "Cluster node ID 0x%x name %s\n",
  2465. cn->nodeId, cn->nodeName.value);
  2466. if (endian_conversion_required) {
  2467. convert_event(evtpkt);
  2468. }
  2469. evtpkt->led_publisher_node_id = cn->nodeId;
  2470. evtpkt->led_in_addr = source_addr;
  2471. evtpkt->led_receive_time = clust_time_now();
  2472. eci = find_channel(&evtpkt->led_chan_name);
  2473. /*
  2474. * We shouldn't see an event for a channel that we don't know about.
  2475. */
  2476. if (!eci) {
  2477. log_printf(LOG_LEVEL_WARNING, "Channel %s doesn't exist\n",
  2478. evtpkt->led_chan_name.value);
  2479. return 0;
  2480. }
  2481. if (check_last_event(evtpkt, source_addr)) {
  2482. return 0;
  2483. }
  2484. evt = make_local_event(evtpkt, eci);
  2485. if (!evt) {
  2486. log_printf(LOG_LEVEL_WARNING,
  2487. "1Memory allocation error, can't deliver event\n");
  2488. errno = ENOMEM;
  2489. return -1;
  2490. }
  2491. if (evt->ed_event.led_retention_time) {
  2492. retain_event(evt);
  2493. }
  2494. /*
  2495. * Check open channels
  2496. */
  2497. for (l = eci->esc_open_chans.next; l != &eci->esc_open_chans; l = l->next) {
  2498. eco = list_entry(l, struct event_svr_channel_open, eco_entry);
  2499. /*
  2500. * See if enabled to receive
  2501. */
  2502. if (!(eco->eco_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  2503. continue;
  2504. }
  2505. /*
  2506. * Check subscriptions
  2507. */
  2508. for (l1 = eco->eco_subscr.next; l1 != &eco->eco_subscr; l1 = l1->next) {
  2509. ecs = list_entry(l1, struct event_svr_channel_subscr, ecs_entry);
  2510. /*
  2511. * Apply filter rules and deliver if patterns
  2512. * match filters.
  2513. * Only deliver one event per open channel
  2514. */
  2515. if (event_match(evt, ecs) == SA_AIS_OK) {
  2516. deliver_event(evt, eco, ecs);
  2517. break;
  2518. }
  2519. }
  2520. }
  2521. free_event_data(evt);
  2522. return 0;
  2523. }
  2524. /*
  2525. * Calculate the remaining retention time of a received event during recovery
  2526. */
  2527. inline SaTimeT calc_retention_time(SaTimeT retention,
  2528. SaTimeT received, SaTimeT now)
  2529. {
  2530. if ((received < now) && ((now - received) < retention)) {
  2531. return retention - (now - received);
  2532. } else {
  2533. return 0;
  2534. }
  2535. }
  2536. /*
  2537. * Receive a recovery network event message and save it in the retained list
  2538. */
  2539. static int evt_remote_recovery_evt(void *msg, struct in_addr source_addr,
  2540. int endian_conversion_required)
  2541. {
  2542. /*
  2543. * - retain events that have a retention time
  2544. * - Find assocated channel
  2545. */
  2546. struct lib_event_data *evtpkt = msg;
  2547. struct event_svr_channel_instance *eci;
  2548. struct event_data *evt;
  2549. struct member_node_data *md;
  2550. SaTimeT now;
  2551. now = clust_time_now();
  2552. log_printf(LOG_LEVEL_DEBUG,
  2553. "Remote recovery event data received from %s\n",
  2554. inet_ntoa(source_addr));
  2555. if (!in_cfg_change) {
  2556. log_printf(LOG_LEVEL_NOTICE,
  2557. "Received recovery data, not in recovery mode\n");
  2558. return 0;
  2559. }
  2560. if (endian_conversion_required) {
  2561. convert_event(evtpkt);
  2562. }
  2563. log_printf(LOG_LEVEL_DEBUG,
  2564. "Processing recovery of retained events\n");
  2565. if (recovery_node) {
  2566. log_printf(LOG_LEVEL_DEBUG, "This node is the recovery node\n");
  2567. }
  2568. log_printf(LOG_LEVEL_DEBUG, "(1)EVT ID: %llx, Time: %llx\n",
  2569. evtpkt->led_event_id, evtpkt->led_retention_time);
  2570. /*
  2571. * Calculate remaining retention time
  2572. */
  2573. evtpkt->led_retention_time = calc_retention_time(
  2574. evtpkt->led_retention_time,
  2575. evtpkt->led_receive_time,
  2576. now);
  2577. log_printf(LOG_LEVEL_DEBUG,
  2578. "(2)EVT ID: %llx, ret: %llx, rec: %llx, now: %llx\n",
  2579. evtpkt->led_event_id,
  2580. evtpkt->led_retention_time, evtpkt->led_receive_time, now);
  2581. /*
  2582. * If we haven't seen this event yet and it has remaining time, process
  2583. * the event.
  2584. */
  2585. if (!check_last_event(evtpkt, evtpkt->led_in_addr) &&
  2586. evtpkt->led_retention_time) {
  2587. /*
  2588. * See where the message came from so that we can set the
  2589. * publishing node id in the message before delivery.
  2590. */
  2591. md = evt_find_node(evtpkt->led_in_addr);
  2592. if (!md) {
  2593. /*
  2594. * Not sure how this can happen
  2595. */
  2596. log_printf(LOG_LEVEL_NOTICE, "No node for %s\n",
  2597. inet_ntoa(evtpkt->led_in_addr));
  2598. errno = ENXIO;
  2599. return -1;
  2600. }
  2601. log_printf(LOG_LEVEL_DEBUG, "Cluster node ID 0x%x name %s\n",
  2602. md->mn_node_info.nodeId,
  2603. md->mn_node_info.nodeName.value);
  2604. eci = find_channel(&evtpkt->led_chan_name);
  2605. /*
  2606. * We shouldn't see an event for a channel that we don't know about.
  2607. */
  2608. if (!eci) {
  2609. log_printf(LOG_LEVEL_WARNING, "Channel %s doesn't exist\n",
  2610. evtpkt->led_chan_name.value);
  2611. return 0;
  2612. }
  2613. evt = make_local_event(evtpkt, eci);
  2614. if (!evt) {
  2615. log_printf(LOG_LEVEL_WARNING,
  2616. "2Memory allocation error, can't deliver event\n");
  2617. errno = ENOMEM;
  2618. return -1;
  2619. }
  2620. retain_event(evt);
  2621. free_event_data(evt);
  2622. }
  2623. return 0;
  2624. }
  2625. /*
  2626. * Timeout handler for event channel open.
  2627. */
  2628. static void chan_open_timeout(void *data)
  2629. {
  2630. struct open_chan_pending *ocp = (struct open_chan_pending *)data;
  2631. struct res_evt_channel_open res;
  2632. res.ico_head.size = sizeof(res);
  2633. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2634. res.ico_head.error = SA_AIS_ERR_TIMEOUT;
  2635. libais_send_response (ocp->ocp_conn_info, &res, sizeof(res));
  2636. list_del(&ocp->ocp_entry);
  2637. free(ocp);
  2638. }
  2639. /*
  2640. * Called by the channel open exec handler to finish the open and
  2641. * respond to the application
  2642. */
  2643. static void evt_chan_open_finish(struct open_chan_pending *ocp,
  2644. struct event_svr_channel_instance *eci)
  2645. {
  2646. uint32_t handle;
  2647. struct res_evt_channel_open res;
  2648. struct event_svr_channel_open *eco;
  2649. SaErrorT error;
  2650. struct libevt_ci *esip = &ocp->ocp_conn_info->ais_ci.u.libevt_ci;
  2651. int ret = 0;
  2652. void *ptr;
  2653. log_printf(CHAN_OPEN_DEBUG, "Open channel finish %s\n",
  2654. getSaNameT(&ocp->ocp_chan_name));
  2655. if (!ocp->ocp_async && ocp->ocp_timer_handle) {
  2656. ret = poll_timer_delete(aisexec_poll_handle, ocp->ocp_timer_handle);
  2657. if (ret != 0 ) {
  2658. log_printf(LOG_LEVEL_WARNING,
  2659. "Error clearing timeout for open channel of %s\n",
  2660. getSaNameT(&ocp->ocp_chan_name));
  2661. }
  2662. }
  2663. /*
  2664. * Create a handle to give back to the caller to associate
  2665. * with this channel open instance.
  2666. */
  2667. error = saHandleCreate(&esip->esi_hdb, sizeof(*eco), &handle);
  2668. if (error != SA_AIS_OK) {
  2669. goto open_return;
  2670. }
  2671. error = saHandleInstanceGet(&esip->esi_hdb, handle, &ptr);
  2672. if (error != SA_AIS_OK) {
  2673. goto open_return;
  2674. }
  2675. eco = ptr;
  2676. /*
  2677. * Initailize and link into the global channel structure.
  2678. */
  2679. list_init(&eco->eco_subscr);
  2680. list_init(&eco->eco_entry);
  2681. list_init(&eco->eco_instance_entry);
  2682. eco->eco_flags = ocp->ocp_open_flag;
  2683. eco->eco_channel = eci;
  2684. eco->eco_lib_handle = ocp->ocp_c_handle;
  2685. eco->eco_my_handle = handle;
  2686. eco->eco_conn_info = ocp->ocp_conn_info;
  2687. list_add_tail(&eco->eco_entry, &eci->esc_open_chans);
  2688. list_add_tail(&eco->eco_instance_entry, &esip->esi_open_chans);
  2689. /*
  2690. * respond back with a handle to access this channel
  2691. * open instance for later subscriptions, etc.
  2692. */
  2693. saHandleInstancePut(&esip->esi_hdb, handle);
  2694. open_return:
  2695. log_printf(CHAN_OPEN_DEBUG, "Open channel finish %s send response %d\n",
  2696. getSaNameT(&ocp->ocp_chan_name),
  2697. error);
  2698. res.ico_head.size = sizeof(res);
  2699. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2700. res.ico_head.error = error;
  2701. res.ico_channel_handle = handle;
  2702. libais_send_response (ocp->ocp_conn_info, &res, sizeof(res));
  2703. if (ret == 0) {
  2704. list_del(&ocp->ocp_entry);
  2705. free(ocp);
  2706. }
  2707. }
  2708. /*
  2709. * Take the channel command data and swap the elements so they match
  2710. * our architectures word order.
  2711. */
  2712. static void
  2713. convert_chan_packet(struct req_evt_chan_command *cpkt)
  2714. {
  2715. /*
  2716. * converted in the main deliver_fn:
  2717. * led_head.id, led_head.size.
  2718. *
  2719. */
  2720. cpkt->chc_op = swab32(cpkt->chc_op);
  2721. /*
  2722. * Which elements of the packet that are converted depend
  2723. * on the operation.
  2724. */
  2725. switch (cpkt->chc_op) {
  2726. case EVT_OPEN_CHAN_OP:
  2727. case EVT_CLOSE_CHAN_OP:
  2728. cpkt->u.chc_chan.length = swab16(cpkt->u.chc_chan.length);
  2729. break;
  2730. case EVT_CLEAR_RET_OP:
  2731. cpkt->u.chc_event_id = swab64(cpkt->u.chc_event_id);
  2732. break;
  2733. case EVT_SET_ID_OP:
  2734. cpkt->u.chc_set_id.chc_addr.s_addr =
  2735. swab32(cpkt->u.chc_set_id.chc_addr.s_addr);
  2736. cpkt->u.chc_set_id.chc_last_id = swab64(cpkt->u.chc_set_id.chc_last_id);
  2737. break;
  2738. case EVT_OPEN_COUNT:
  2739. cpkt->u.chc_set_opens.chc_chan_name.length =
  2740. swab16(cpkt->u.chc_set_opens.chc_chan_name.length);
  2741. cpkt->u.chc_set_opens.chc_open_count =
  2742. swab32(cpkt->u.chc_set_opens.chc_open_count);
  2743. break;
  2744. /*
  2745. * No data assocaited with these ops.
  2746. */
  2747. case EVT_CONF_DONE:
  2748. case EVT_OPEN_COUNT_DONE:
  2749. break;
  2750. /*
  2751. * Make sure that this function is updated when new ops are added.
  2752. */
  2753. default:
  2754. assert(0);
  2755. }
  2756. }
  2757. /*
  2758. * Receive and process remote event operations.
  2759. * Used to communicate channel opens/closes, clear retention time,
  2760. * config change updates...
  2761. */
  2762. static int evt_remote_chan_op(void *msg, struct in_addr source_addr,
  2763. int endian_conversion_required)
  2764. {
  2765. struct req_evt_chan_command *cpkt = msg;
  2766. struct in_addr local_node = {SA_CLM_LOCAL_NODE_ID};
  2767. SaClmClusterNodeT *cn, *my_node;
  2768. struct member_node_data *mn;
  2769. struct event_svr_channel_instance *eci;
  2770. if (endian_conversion_required) {
  2771. convert_chan_packet(cpkt);
  2772. }
  2773. log_printf(REMOTE_OP_DEBUG, "Remote channel operation request\n");
  2774. my_node = clm_get_by_nodeid(local_node);
  2775. log_printf(REMOTE_OP_DEBUG, "my node ID: 0x%x\n", my_node->nodeId);
  2776. mn = evt_find_node(source_addr);
  2777. if (mn == NULL) {
  2778. cn = clm_get_by_nodeid(source_addr);
  2779. if (cn == NULL) {
  2780. log_printf(LOG_LEVEL_WARNING,
  2781. "Evt remote channel op: Node data for addr %s is NULL\n",
  2782. inet_ntoa(source_addr));
  2783. } else {
  2784. evt_add_node(source_addr, cn);
  2785. mn = evt_find_node(source_addr);
  2786. }
  2787. }
  2788. switch (cpkt->chc_op) {
  2789. /*
  2790. * Open channel remote command. The open channel request is done
  2791. * in two steps. When an pplication tries to open, we send an open
  2792. * channel message to the other nodes. When we receive an open channel
  2793. * message, we may create the channel structure if it doesn't exist
  2794. * and also complete applicaiton open requests for the specified
  2795. * channel. We keep a counter of total opens for the given channel and
  2796. * later when it has been completely closed (everywhere in the cluster)
  2797. * we will free up the assocated channel data.
  2798. */
  2799. case EVT_OPEN_CHAN_OP: {
  2800. struct open_chan_pending *ocp;
  2801. struct list_head *l, *nxt;
  2802. log_printf(CHAN_OPEN_DEBUG, "Opening channel %s for node 0x%x\n",
  2803. cpkt->u.chc_chan.value, mn->mn_node_info.nodeId);
  2804. eci = find_channel(&cpkt->u.chc_chan);
  2805. if (!eci) {
  2806. eci = create_channel(&cpkt->u.chc_chan);
  2807. }
  2808. if (!eci) {
  2809. log_printf(LOG_LEVEL_WARNING, "Could not create channel %s\n",
  2810. getSaNameT(&cpkt->u.chc_chan));
  2811. break;
  2812. }
  2813. inc_open_count(eci, mn->mn_node_info.nodeId);
  2814. if (mn->mn_node_info.nodeId == my_node->nodeId) {
  2815. /*
  2816. * Complete one of our pending open requests
  2817. */
  2818. for (l = open_pending.next; l != &open_pending; l = nxt) {
  2819. nxt = l->next;
  2820. ocp = list_entry(l, struct open_chan_pending, ocp_entry);
  2821. log_printf(CHAN_OPEN_DEBUG,
  2822. "Compare channel %s %s\n", ocp->ocp_chan_name.value,
  2823. eci->esc_channel_name.value);
  2824. if (name_match(&ocp->ocp_chan_name, &eci->esc_channel_name)) {
  2825. evt_chan_open_finish(ocp, eci);
  2826. break;
  2827. }
  2828. }
  2829. }
  2830. log_printf(CHAN_OPEN_DEBUG,
  2831. "Open channel %s t %d, l %d, r %d\n",
  2832. getSaNameT(&eci->esc_channel_name),
  2833. eci->esc_total_opens, eci->esc_local_opens,
  2834. eci->esc_retained_count);
  2835. break;
  2836. }
  2837. /*
  2838. * Handle a channel close. We'll decrement the global open counter and
  2839. * free up channel associated data when all instances are closed.
  2840. */
  2841. case EVT_CLOSE_CHAN_OP:
  2842. log_printf(LOG_LEVEL_DEBUG, "Closing channel %s for node 0x%x\n",
  2843. cpkt->u.chc_chan.value, mn->mn_node_info.nodeId);
  2844. eci = find_channel(&cpkt->u.chc_chan);
  2845. if (!eci) {
  2846. log_printf(LOG_LEVEL_NOTICE,
  2847. "Channel close request for %s not found\n",
  2848. cpkt->u.chc_chan.value);
  2849. break;
  2850. }
  2851. /*
  2852. * if last instance, we can free up assocated data.
  2853. */
  2854. dec_open_count(eci, mn->mn_node_info.nodeId);
  2855. log_printf(LOG_LEVEL_DEBUG,
  2856. "Close channel %s t %d, l %d, r %d\n",
  2857. eci->esc_channel_name.value,
  2858. eci->esc_total_opens, eci->esc_local_opens,
  2859. eci->esc_retained_count);
  2860. delete_channel(eci);
  2861. break;
  2862. /*
  2863. * saEvtClearRetentiotime handler.
  2864. */
  2865. case EVT_CLEAR_RET_OP:
  2866. log_printf(LOG_LEVEL_DEBUG, "Clear retention time request %llx\n",
  2867. cpkt->u.chc_event_id);
  2868. clear_retention_time(cpkt->u.chc_event_id);
  2869. break;
  2870. /*
  2871. * Set our next event ID based on the largest event ID seen
  2872. * by others in the cluster. This way, if we've left and re-joined, we'll
  2873. * start using an event ID that is unique.
  2874. */
  2875. case EVT_SET_ID_OP: {
  2876. struct in_addr my_addr;
  2877. my_addr.s_addr = my_node->nodeId;
  2878. log_printf(RECOVERY_DEBUG,
  2879. "Received Set event ID OP from %x to %llx for %x my addr %x base %llx\n",
  2880. inet_ntoa(source_addr),
  2881. cpkt->u.chc_set_id.chc_last_id,
  2882. cpkt->u.chc_set_id.chc_addr.s_addr,
  2883. my_addr.s_addr,
  2884. base_id);
  2885. if (cpkt->u.chc_set_id.chc_addr.s_addr == my_addr.s_addr) {
  2886. if (cpkt->u.chc_set_id.chc_last_id > base_id) {
  2887. log_printf(RECOVERY_DEBUG,
  2888. "Set event ID from %s to %llx\n",
  2889. inet_ntoa(source_addr), cpkt->u.chc_set_id.chc_last_id);
  2890. base_id = cpkt->u.chc_set_id.chc_last_id + 1;
  2891. }
  2892. }
  2893. break;
  2894. }
  2895. /*
  2896. * Receive the open count for a particular channel during recovery.
  2897. * This insures that everyone has the same notion of who has a channel
  2898. * open so that it can be removed when no one else has it open anymore.
  2899. */
  2900. case EVT_OPEN_COUNT:
  2901. if (!in_cfg_change) {
  2902. log_printf(LOG_LEVEL_ERROR,
  2903. "Evt open count msg from %s, but not in membership change\n",
  2904. inet_ntoa(source_addr));
  2905. }
  2906. log_printf(RECOVERY_DEBUG,
  2907. "Open channel count %s is %d for node 0x%x\n",
  2908. cpkt->u.chc_set_opens.chc_chan_name.value,
  2909. cpkt->u.chc_set_opens.chc_open_count,
  2910. mn->mn_node_info.nodeId);
  2911. eci = find_channel(&cpkt->u.chc_set_opens.chc_chan_name);
  2912. if (!eci) {
  2913. eci = create_channel(&cpkt->u.chc_set_opens.chc_chan_name);
  2914. }
  2915. if (!eci) {
  2916. log_printf(LOG_LEVEL_WARNING, "Could not create channel %s\n",
  2917. getSaNameT(&cpkt->u.chc_set_opens.chc_chan_name));
  2918. break;
  2919. }
  2920. if (set_open_count(eci, mn->mn_node_info.nodeId,
  2921. cpkt->u.chc_set_opens.chc_open_count)) {
  2922. log_printf(LOG_LEVEL_ERROR,
  2923. "Error setting Open channel count %s for node 0x%x\n",
  2924. cpkt->u.chc_set_opens.chc_chan_name.value,
  2925. mn->mn_node_info.nodeId);
  2926. }
  2927. break;
  2928. /*
  2929. * Once we get all the messages from
  2930. * the current membership, determine who delivers any retained events.
  2931. */
  2932. case EVT_OPEN_COUNT_DONE: {
  2933. if (!in_cfg_change) {
  2934. log_printf(LOG_LEVEL_ERROR,
  2935. "Evt config msg from %s, but not in membership change\n",
  2936. inet_ntoa(source_addr));
  2937. }
  2938. log_printf(RECOVERY_DEBUG,
  2939. "Receive EVT_CONF_CHANGE_DONE from %s members %d checked in %d\n",
  2940. inet_ntoa(source_addr), total_members, checked_in+1);
  2941. if (!mn) {
  2942. log_printf(RECOVERY_DEBUG,
  2943. "NO NODE DATA AVAILABLE FOR %s\n",
  2944. inet_ntoa(source_addr));
  2945. }
  2946. if (++checked_in == total_members) {
  2947. /*
  2948. * We're all here, now figure out who should send the
  2949. * retained events, if any.
  2950. */
  2951. mn = oldest_node();
  2952. if (mn->mn_node_info.nodeId == my_node_id) {
  2953. log_printf(RECOVERY_DEBUG, "I am oldest\n");
  2954. send_retained();
  2955. }
  2956. }
  2957. break;
  2958. }
  2959. /*
  2960. * OK, We're done with recovery, continue operations.
  2961. */
  2962. case EVT_CONF_DONE: {
  2963. log_printf(RECOVERY_DEBUG,
  2964. "Receive EVT_CONF_DONE from %s\n",
  2965. inet_ntoa(source_addr));
  2966. in_cfg_change = 0;
  2967. // TODO totempg_recovery_plug_unplug (evt_recovery_plug_handle);
  2968. #ifdef DUMP_CHAN_INFO
  2969. dump_all_chans();
  2970. #endif
  2971. break;
  2972. }
  2973. default:
  2974. log_printf(LOG_LEVEL_NOTICE, "Invalid channel operation %d\n",
  2975. cpkt->chc_op);
  2976. break;
  2977. }
  2978. return 0;
  2979. }
  2980. /*
  2981. * vi: set autoindent tabstop=4 shiftwidth=4 :
  2982. */