evt.c 106 KB

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