evt.c 99 KB

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