evt.c 105 KB

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