evt.c 116 KB

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