evt.c 103 KB

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