evt.c 106 KB

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