evt.c 90 KB

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