evt.c 116 KB

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