totemrrp.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <assert.h>
  37. #include <pthread.h>
  38. #include <sys/mman.h>
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <sys/socket.h>
  42. #include <netdb.h>
  43. #include <sys/un.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/param.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <unistd.h>
  49. #include <fcntl.h>
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <errno.h>
  53. #include <sched.h>
  54. #include <time.h>
  55. #include <sys/time.h>
  56. #include <sys/poll.h>
  57. #include <limits.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/list.h>
  60. #include <corosync/hdb.h>
  61. #include <corosync/swab.h>
  62. #include <corosync/totem/coropoll.h>
  63. #define LOGSYS_UTILS_ONLY 1
  64. #include <corosync/engine/logsys.h>
  65. #include "totemnet.h"
  66. #include "totemrrp.h"
  67. void rrp_deliver_fn (
  68. void *context,
  69. const void *msg,
  70. unsigned int msg_len);
  71. void rrp_iface_change_fn (
  72. void *context,
  73. const struct totem_ip_address *iface_addr);
  74. struct totemrrp_instance;
  75. struct passive_instance {
  76. struct totemrrp_instance *rrp_instance;
  77. unsigned int *faulty;
  78. unsigned int *token_recv_count;
  79. unsigned int *mcast_recv_count;
  80. unsigned char token[15000];
  81. unsigned int token_len;
  82. poll_timer_handle timer_expired_token;
  83. poll_timer_handle timer_problem_decrementer;
  84. void *totemrrp_context;
  85. unsigned int token_xmit_iface;
  86. unsigned int msg_xmit_iface;
  87. };
  88. struct active_instance {
  89. struct totemrrp_instance *rrp_instance;
  90. unsigned int *faulty;
  91. unsigned int *last_token_recv;
  92. unsigned int *counter_problems;
  93. unsigned char token[15000];
  94. unsigned int token_len;
  95. unsigned int last_token_seq;
  96. poll_timer_handle timer_expired_token;
  97. poll_timer_handle timer_problem_decrementer;
  98. void *totemrrp_context;
  99. };
  100. struct rrp_algo {
  101. const char *name;
  102. void * (*initialize) (
  103. struct totemrrp_instance *rrp_instance,
  104. int interface_count);
  105. void (*mcast_recv) (
  106. struct totemrrp_instance *instance,
  107. unsigned int iface_no,
  108. void *context,
  109. const void *msg,
  110. unsigned int msg_len);
  111. void (*mcast_noflush_send) (
  112. struct totemrrp_instance *instance,
  113. const void *msg,
  114. unsigned int msg_len);
  115. void (*mcast_flush_send) (
  116. struct totemrrp_instance *instance,
  117. const void *msg,
  118. unsigned int msg_len);
  119. void (*token_recv) (
  120. struct totemrrp_instance *instance,
  121. unsigned int iface_no,
  122. void *context,
  123. const void *msg,
  124. unsigned int msg_len,
  125. unsigned int token_seqid);
  126. void (*token_send) (
  127. struct totemrrp_instance *instance,
  128. const void *msg,
  129. unsigned int msg_len);
  130. void (*recv_flush) (
  131. struct totemrrp_instance *instance);
  132. void (*send_flush) (
  133. struct totemrrp_instance *instance);
  134. void (*iface_check) (
  135. struct totemrrp_instance *instance);
  136. void (*processor_count_set) (
  137. struct totemrrp_instance *instance,
  138. unsigned int processor_count);
  139. void (*token_target_set) (
  140. struct totemrrp_instance *instance,
  141. struct totem_ip_address *token_target,
  142. unsigned int iface_no);
  143. void (*ring_reenable) (
  144. struct totemrrp_instance *instance,
  145. unsigned int iface_no);
  146. int (*mcast_recv_empty) (
  147. struct totemrrp_instance *instance);
  148. int (*member_add) (
  149. struct totemrrp_instance *instance,
  150. const struct totem_ip_address *member,
  151. unsigned int iface_no);
  152. int (*member_remove) (
  153. struct totemrrp_instance *instance,
  154. const struct totem_ip_address *member,
  155. unsigned int iface_no);
  156. void (*membership_changed) (
  157. struct totemrrp_instance *instance,
  158. enum totem_configuration_type configuration_type,
  159. const struct srp_addr *member_list, size_t member_list_entries,
  160. const struct srp_addr *left_list, size_t left_list_entries,
  161. const struct srp_addr *joined_list, size_t joined_list_entries,
  162. const struct memb_ring_id *ring_id);
  163. };
  164. struct totemrrp_instance {
  165. hdb_handle_t poll_handle;
  166. struct totem_interface *interfaces;
  167. struct rrp_algo *rrp_algo;
  168. void *context;
  169. char *status[INTERFACE_MAX];
  170. void (*totemrrp_deliver_fn) (
  171. void *context,
  172. const void *msg,
  173. unsigned int msg_len);
  174. void (*totemrrp_iface_change_fn) (
  175. void *context,
  176. const struct totem_ip_address *iface_addr,
  177. unsigned int iface_no);
  178. void (*totemrrp_token_seqid_get) (
  179. const void *msg,
  180. unsigned int *seqid,
  181. unsigned int *token_is);
  182. void (*totemrrp_target_set_completed) (
  183. void *context);
  184. unsigned int (*totemrrp_msgs_missing) (void);
  185. /*
  186. * Function and data used to log messages
  187. */
  188. int totemrrp_log_level_security;
  189. int totemrrp_log_level_error;
  190. int totemrrp_log_level_warning;
  191. int totemrrp_log_level_notice;
  192. int totemrrp_log_level_debug;
  193. int totemrrp_subsys_id;
  194. void (*totemrrp_log_printf) (
  195. unsigned int rec_ident,
  196. const char *function,
  197. const char *file,
  198. int line,
  199. const char *format, ...)__attribute__((format(printf, 5, 6)));
  200. void **net_handles;
  201. void *rrp_algo_instance;
  202. int interface_count;
  203. int processor_count;
  204. int my_nodeid;
  205. struct totem_config *totem_config;
  206. void *deliver_fn_context[INTERFACE_MAX];
  207. poll_timer_handle timer_active_test_ring_timeout[INTERFACE_MAX];
  208. };
  209. /*
  210. * None Replication Forward Declerations
  211. */
  212. static void none_mcast_recv (
  213. struct totemrrp_instance *instance,
  214. unsigned int iface_no,
  215. void *context,
  216. const void *msg,
  217. unsigned int msg_len);
  218. static void none_mcast_noflush_send (
  219. struct totemrrp_instance *instance,
  220. const void *msg,
  221. unsigned int msg_len);
  222. static void none_mcast_flush_send (
  223. struct totemrrp_instance *instance,
  224. const void *msg,
  225. unsigned int msg_len);
  226. static void none_token_recv (
  227. struct totemrrp_instance *instance,
  228. unsigned int iface_no,
  229. void *context,
  230. const void *msg,
  231. unsigned int msg_len,
  232. unsigned int token_seqid);
  233. static void none_token_send (
  234. struct totemrrp_instance *instance,
  235. const void *msg,
  236. unsigned int msg_len);
  237. static void none_recv_flush (
  238. struct totemrrp_instance *instance);
  239. static void none_send_flush (
  240. struct totemrrp_instance *instance);
  241. static void none_iface_check (
  242. struct totemrrp_instance *instance);
  243. static void none_processor_count_set (
  244. struct totemrrp_instance *instance,
  245. unsigned int processor_count_set);
  246. static void none_token_target_set (
  247. struct totemrrp_instance *instance,
  248. struct totem_ip_address *token_target,
  249. unsigned int iface_no);
  250. static void none_ring_reenable (
  251. struct totemrrp_instance *instance,
  252. unsigned int iface_no);
  253. static int none_mcast_recv_empty (
  254. struct totemrrp_instance *instance);
  255. static int none_member_add (
  256. struct totemrrp_instance *instance,
  257. const struct totem_ip_address *member,
  258. unsigned int iface_no);
  259. static int none_member_remove (
  260. struct totemrrp_instance *instance,
  261. const struct totem_ip_address *member,
  262. unsigned int iface_no);
  263. static void none_membership_changed (
  264. struct totemrrp_instance *instance,
  265. enum totem_configuration_type configuration_type,
  266. const struct srp_addr *member_list, size_t member_list_entries,
  267. const struct srp_addr *left_list, size_t left_list_entries,
  268. const struct srp_addr *joined_list, size_t joined_list_entries,
  269. const struct memb_ring_id *ring_id);
  270. /*
  271. * Passive Replication Forward Declerations
  272. */
  273. static void *passive_instance_initialize (
  274. struct totemrrp_instance *rrp_instance,
  275. int interface_count);
  276. static void passive_mcast_recv (
  277. struct totemrrp_instance *instance,
  278. unsigned int iface_no,
  279. void *context,
  280. const void *msg,
  281. unsigned int msg_len);
  282. static void passive_mcast_noflush_send (
  283. struct totemrrp_instance *instance,
  284. const void *msg,
  285. unsigned int msg_len);
  286. static void passive_mcast_flush_send (
  287. struct totemrrp_instance *instance,
  288. const void *msg,
  289. unsigned int msg_len);
  290. static void passive_monitor (
  291. struct totemrrp_instance *rrp_instance,
  292. unsigned int iface_no,
  293. int is_token_recv_count);
  294. static void passive_token_recv (
  295. struct totemrrp_instance *instance,
  296. unsigned int iface_no,
  297. void *context,
  298. const void *msg,
  299. unsigned int msg_len,
  300. unsigned int token_seqid);
  301. static void passive_token_send (
  302. struct totemrrp_instance *instance,
  303. const void *msg,
  304. unsigned int msg_len);
  305. static void passive_recv_flush (
  306. struct totemrrp_instance *instance);
  307. static void passive_send_flush (
  308. struct totemrrp_instance *instance);
  309. static void passive_iface_check (
  310. struct totemrrp_instance *instance);
  311. static void passive_processor_count_set (
  312. struct totemrrp_instance *instance,
  313. unsigned int processor_count_set);
  314. static void passive_token_target_set (
  315. struct totemrrp_instance *instance,
  316. struct totem_ip_address *token_target,
  317. unsigned int iface_no);
  318. static void passive_ring_reenable (
  319. struct totemrrp_instance *instance,
  320. unsigned int iface_no);
  321. static int passive_mcast_recv_empty (
  322. struct totemrrp_instance *instance);
  323. static int passive_member_add (
  324. struct totemrrp_instance *instance,
  325. const struct totem_ip_address *member,
  326. unsigned int iface_no);
  327. static int passive_member_remove (
  328. struct totemrrp_instance *instance,
  329. const struct totem_ip_address *member,
  330. unsigned int iface_no);
  331. static void passive_membership_changed (
  332. struct totemrrp_instance *instance,
  333. enum totem_configuration_type configuration_type,
  334. const struct srp_addr *member_list, size_t member_list_entries,
  335. const struct srp_addr *left_list, size_t left_list_entries,
  336. const struct srp_addr *joined_list, size_t joined_list_entries,
  337. const struct memb_ring_id *ring_id);
  338. /*
  339. * Active Replication Forward Definitions
  340. */
  341. static void *active_instance_initialize (
  342. struct totemrrp_instance *rrp_instance,
  343. int interface_count);
  344. static void active_mcast_recv (
  345. struct totemrrp_instance *instance,
  346. unsigned int iface_no,
  347. void *context,
  348. const void *msg,
  349. unsigned int msg_len);
  350. static void active_mcast_noflush_send (
  351. struct totemrrp_instance *instance,
  352. const void *msg,
  353. unsigned int msg_len);
  354. static void active_mcast_flush_send (
  355. struct totemrrp_instance *instance,
  356. const void *msg,
  357. unsigned int msg_len);
  358. static void active_token_recv (
  359. struct totemrrp_instance *instance,
  360. unsigned int iface_no,
  361. void *context,
  362. const void *msg,
  363. unsigned int msg_len,
  364. unsigned int token_seqid);
  365. static void active_token_send (
  366. struct totemrrp_instance *instance,
  367. const void *msg,
  368. unsigned int msg_len);
  369. static void active_recv_flush (
  370. struct totemrrp_instance *instance);
  371. static void active_send_flush (
  372. struct totemrrp_instance *instance);
  373. static void active_iface_check (
  374. struct totemrrp_instance *instance);
  375. static void active_processor_count_set (
  376. struct totemrrp_instance *instance,
  377. unsigned int processor_count_set);
  378. static void active_token_target_set (
  379. struct totemrrp_instance *instance,
  380. struct totem_ip_address *token_target,
  381. unsigned int iface_no);
  382. static void active_ring_reenable (
  383. struct totemrrp_instance *instance,
  384. unsigned int iface_no);
  385. static int active_mcast_recv_empty (
  386. struct totemrrp_instance *instance);
  387. static int active_member_add (
  388. struct totemrrp_instance *instance,
  389. const struct totem_ip_address *member,
  390. unsigned int iface_no);
  391. static int active_member_remove (
  392. struct totemrrp_instance *instance,
  393. const struct totem_ip_address *member,
  394. unsigned int iface_no);
  395. static void active_membership_changed (
  396. struct totemrrp_instance *instance,
  397. enum totem_configuration_type configuration_type,
  398. const struct srp_addr *member_list, size_t member_list_entries,
  399. const struct srp_addr *left_list, size_t left_list_entries,
  400. const struct srp_addr *joined_list, size_t joined_list_entries,
  401. const struct memb_ring_id *ring_id);
  402. static void active_timer_expired_token_start (
  403. struct active_instance *active_instance);
  404. static void active_timer_expired_token_cancel (
  405. struct active_instance *active_instance);
  406. static void active_timer_problem_decrementer_start (
  407. struct active_instance *active_instance);
  408. static void active_timer_problem_decrementer_cancel (
  409. struct active_instance *active_instance);
  410. /*
  411. * 0-5 reserved for totemsrp.c
  412. */
  413. #define MESSAGE_TYPE_RING_TEST_ACTIVE 6
  414. #define MESSAGE_TYPE_RING_TEST_ACTIVATE 7
  415. #define ENDIAN_LOCAL 0xff22
  416. /*
  417. * Rollover handling:
  418. *
  419. * ARR_SEQNO_START_TOKEN is the starting sequence number of last seen sequence
  420. * for a token for active redundand ring. This should remain zero, unless testing
  421. * overflow in which case 07fffff00 or 0xffffff00 are good starting values.
  422. * It should be same as on defined in totemsrp.c
  423. */
  424. #define ARR_SEQNO_START_TOKEN 0x0
  425. /*
  426. * These can be used ot test different rollover points
  427. * #define ARR_SEQNO_START_MSG 0xfffffe00
  428. */
  429. /*
  430. * Threshold value when recv_count for passive rrp should be adjusted.
  431. * Set this value to some smaller for testing of adjusting proper
  432. * functionality. Also keep in mind that this value must be smaller
  433. * than rrp_problem_count_threshold
  434. */
  435. #define PASSIVE_RECV_COUNT_THRESHOLD (INT_MAX / 2)
  436. struct message_header {
  437. char type;
  438. char encapsulated;
  439. unsigned short endian_detector;
  440. int ring_number;
  441. int nodeid_activator;
  442. } __attribute__((packed));
  443. struct deliver_fn_context {
  444. struct totemrrp_instance *instance;
  445. void *context;
  446. int iface_no;
  447. };
  448. struct rrp_algo none_algo = {
  449. .name = "none",
  450. .initialize = NULL,
  451. .mcast_recv = none_mcast_recv,
  452. .mcast_noflush_send = none_mcast_noflush_send,
  453. .mcast_flush_send = none_mcast_flush_send,
  454. .token_recv = none_token_recv,
  455. .token_send = none_token_send,
  456. .recv_flush = none_recv_flush,
  457. .send_flush = none_send_flush,
  458. .iface_check = none_iface_check,
  459. .processor_count_set = none_processor_count_set,
  460. .token_target_set = none_token_target_set,
  461. .ring_reenable = none_ring_reenable,
  462. .mcast_recv_empty = none_mcast_recv_empty,
  463. .member_add = none_member_add,
  464. .member_remove = none_member_remove,
  465. .membership_changed = none_membership_changed
  466. };
  467. struct rrp_algo passive_algo = {
  468. .name = "passive",
  469. .initialize = passive_instance_initialize,
  470. .mcast_recv = passive_mcast_recv,
  471. .mcast_noflush_send = passive_mcast_noflush_send,
  472. .mcast_flush_send = passive_mcast_flush_send,
  473. .token_recv = passive_token_recv,
  474. .token_send = passive_token_send,
  475. .recv_flush = passive_recv_flush,
  476. .send_flush = passive_send_flush,
  477. .iface_check = passive_iface_check,
  478. .processor_count_set = passive_processor_count_set,
  479. .token_target_set = passive_token_target_set,
  480. .ring_reenable = passive_ring_reenable,
  481. .mcast_recv_empty = passive_mcast_recv_empty,
  482. .member_add = passive_member_add,
  483. .member_remove = passive_member_remove,
  484. .membership_changed = passive_membership_changed
  485. };
  486. struct rrp_algo active_algo = {
  487. .name = "active",
  488. .initialize = active_instance_initialize,
  489. .mcast_recv = active_mcast_recv,
  490. .mcast_noflush_send = active_mcast_noflush_send,
  491. .mcast_flush_send = active_mcast_flush_send,
  492. .token_recv = active_token_recv,
  493. .token_send = active_token_send,
  494. .recv_flush = active_recv_flush,
  495. .send_flush = active_send_flush,
  496. .iface_check = active_iface_check,
  497. .processor_count_set = active_processor_count_set,
  498. .token_target_set = active_token_target_set,
  499. .ring_reenable = active_ring_reenable,
  500. .mcast_recv_empty = active_mcast_recv_empty,
  501. .member_add = active_member_add,
  502. .member_remove = active_member_remove,
  503. .membership_changed = active_membership_changed
  504. };
  505. struct rrp_algo *rrp_algos[] = {
  506. &none_algo,
  507. &passive_algo,
  508. &active_algo
  509. };
  510. #define RRP_ALGOS_COUNT 3
  511. #define log_printf(level, format, args...) \
  512. do { \
  513. rrp_instance->totemrrp_log_printf ( \
  514. LOGSYS_ENCODE_RECID(level, \
  515. rrp_instance->totemrrp_subsys_id, \
  516. LOGSYS_RECID_LOG), \
  517. __FUNCTION__, __FILE__, __LINE__, \
  518. format, ##args); \
  519. } while (0);
  520. static void test_active_msg_endian_convert(const struct message_header *in, struct message_header *out)
  521. {
  522. out->type = in->type;
  523. out->encapsulated = in->encapsulated;
  524. out->endian_detector = ENDIAN_LOCAL;
  525. out->ring_number = swab32 (in->ring_number);
  526. out->nodeid_activator = swab32(in->nodeid_activator);
  527. }
  528. static void timer_function_test_ring_timeout (void *context)
  529. {
  530. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  531. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  532. unsigned int *faulty = NULL;
  533. int iface_no = deliver_fn_context->iface_no;
  534. struct message_header msg = {
  535. .type = MESSAGE_TYPE_RING_TEST_ACTIVE,
  536. .endian_detector = ENDIAN_LOCAL,
  537. };
  538. if (strcmp(rrp_instance->totem_config->rrp_mode, "active") == 0)
  539. faulty = ((struct active_instance *)(rrp_instance->rrp_algo_instance))->faulty;
  540. if (strcmp(rrp_instance->totem_config->rrp_mode, "passive") == 0)
  541. faulty = ((struct passive_instance *)(rrp_instance->rrp_algo_instance))->faulty;
  542. assert (faulty != NULL);
  543. if (faulty[iface_no] == 1) {
  544. msg.ring_number = iface_no;
  545. msg.nodeid_activator = rrp_instance->my_nodeid;
  546. totemnet_token_send (
  547. rrp_instance->net_handles[iface_no],
  548. &msg, sizeof (struct message_header));
  549. poll_timer_add (rrp_instance->poll_handle,
  550. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  551. (void *)deliver_fn_context,
  552. timer_function_test_ring_timeout,
  553. &rrp_instance->timer_active_test_ring_timeout[iface_no]);
  554. }
  555. }
  556. /*
  557. * None Replication Implementation
  558. */
  559. static void none_mcast_recv (
  560. struct totemrrp_instance *rrp_instance,
  561. unsigned int iface_no,
  562. void *context,
  563. const void *msg,
  564. unsigned int msg_len)
  565. {
  566. rrp_instance->totemrrp_deliver_fn (
  567. context,
  568. msg,
  569. msg_len);
  570. }
  571. static void none_mcast_flush_send (
  572. struct totemrrp_instance *instance,
  573. const void *msg,
  574. unsigned int msg_len)
  575. {
  576. totemnet_mcast_flush_send (instance->net_handles[0], msg, msg_len);
  577. }
  578. static void none_mcast_noflush_send (
  579. struct totemrrp_instance *instance,
  580. const void *msg,
  581. unsigned int msg_len)
  582. {
  583. totemnet_mcast_noflush_send (instance->net_handles[0], msg, msg_len);
  584. }
  585. static void none_token_recv (
  586. struct totemrrp_instance *rrp_instance,
  587. unsigned int iface_no,
  588. void *context,
  589. const void *msg,
  590. unsigned int msg_len,
  591. unsigned int token_seq)
  592. {
  593. rrp_instance->totemrrp_deliver_fn (
  594. context,
  595. msg,
  596. msg_len);
  597. }
  598. static void none_token_send (
  599. struct totemrrp_instance *instance,
  600. const void *msg,
  601. unsigned int msg_len)
  602. {
  603. totemnet_token_send (
  604. instance->net_handles[0],
  605. msg, msg_len);
  606. }
  607. static void none_recv_flush (struct totemrrp_instance *instance)
  608. {
  609. totemnet_recv_flush (instance->net_handles[0]);
  610. }
  611. static void none_send_flush (struct totemrrp_instance *instance)
  612. {
  613. totemnet_send_flush (instance->net_handles[0]);
  614. }
  615. static void none_iface_check (struct totemrrp_instance *instance)
  616. {
  617. totemnet_iface_check (instance->net_handles[0]);
  618. }
  619. static void none_processor_count_set (
  620. struct totemrrp_instance *instance,
  621. unsigned int processor_count)
  622. {
  623. totemnet_processor_count_set (instance->net_handles[0],
  624. processor_count);
  625. }
  626. static void none_token_target_set (
  627. struct totemrrp_instance *instance,
  628. struct totem_ip_address *token_target,
  629. unsigned int iface_no)
  630. {
  631. totemnet_token_target_set (instance->net_handles[0], token_target);
  632. }
  633. static void none_ring_reenable (
  634. struct totemrrp_instance *instance,
  635. unsigned int iface_no)
  636. {
  637. /*
  638. * No operation
  639. */
  640. }
  641. static int none_mcast_recv_empty (
  642. struct totemrrp_instance *instance)
  643. {
  644. int res;
  645. res = totemnet_recv_mcast_empty (instance->net_handles[0]);
  646. return (res);
  647. }
  648. static int none_member_add (
  649. struct totemrrp_instance *instance,
  650. const struct totem_ip_address *member,
  651. unsigned int iface_no)
  652. {
  653. int res;
  654. res = totemnet_member_add (instance->net_handles[0], member);
  655. return (res);
  656. }
  657. static int none_member_remove (
  658. struct totemrrp_instance *instance,
  659. const struct totem_ip_address *member,
  660. unsigned int iface_no)
  661. {
  662. int res;
  663. res = totemnet_member_remove (instance->net_handles[0], member);
  664. return (res);
  665. }
  666. static void none_membership_changed (
  667. struct totemrrp_instance *rrp_instance,
  668. enum totem_configuration_type configuration_type,
  669. const struct srp_addr *member_list, size_t member_list_entries,
  670. const struct srp_addr *left_list, size_t left_list_entries,
  671. const struct srp_addr *joined_list, size_t joined_list_entries,
  672. const struct memb_ring_id *ring_id)
  673. {
  674. int i;
  675. for (i = 0; i < left_list_entries; i++) {
  676. if (left_list[i].addr[0].family != AF_INET && left_list[i].addr[0].family != AF_INET6) {
  677. log_printf(rrp_instance->totemrrp_log_level_error,
  678. "Membership left list contains incorrect address. "
  679. "This is sign of misconfiguration between nodes!");
  680. } else {
  681. totemnet_member_set_active(rrp_instance->net_handles[0],
  682. &left_list[i].addr[0], 0);
  683. }
  684. }
  685. for (i = 0; i < joined_list_entries; i++) {
  686. if (joined_list[i].addr[0].family != AF_INET && joined_list[i].addr[0].family != AF_INET6) {
  687. log_printf(rrp_instance->totemrrp_log_level_error,
  688. "Membership join list contains incorrect address. "
  689. "This is sign of misconfiguration between nodes!");
  690. } else {
  691. totemnet_member_set_active(rrp_instance->net_handles[0],
  692. &joined_list[i].addr[0], 1);
  693. }
  694. }
  695. }
  696. /*
  697. * Passive Replication Implementation
  698. */
  699. void *passive_instance_initialize (
  700. struct totemrrp_instance *rrp_instance,
  701. int interface_count)
  702. {
  703. struct passive_instance *instance;
  704. instance = malloc (sizeof (struct passive_instance));
  705. if (instance == 0) {
  706. goto error_exit;
  707. }
  708. memset (instance, 0, sizeof (struct passive_instance));
  709. instance->faulty = malloc (sizeof (int) * interface_count);
  710. if (instance->faulty == 0) {
  711. free (instance);
  712. instance = 0;
  713. goto error_exit;
  714. }
  715. memset (instance->faulty, 0, sizeof (int) * interface_count);
  716. instance->token_recv_count = malloc (sizeof (int) * interface_count);
  717. if (instance->token_recv_count == 0) {
  718. free (instance->faulty);
  719. free (instance);
  720. instance = 0;
  721. goto error_exit;
  722. }
  723. memset (instance->token_recv_count, 0, sizeof (int) * interface_count);
  724. instance->mcast_recv_count = malloc (sizeof (int) * interface_count);
  725. if (instance->mcast_recv_count == 0) {
  726. free (instance->token_recv_count);
  727. free (instance->faulty);
  728. free (instance);
  729. instance = 0;
  730. goto error_exit;
  731. }
  732. memset (instance->mcast_recv_count, 0, sizeof (int) * interface_count);
  733. error_exit:
  734. return ((void *)instance);
  735. }
  736. static void timer_function_passive_token_expired (void *context)
  737. {
  738. struct passive_instance *passive_instance = (struct passive_instance *)context;
  739. struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  740. rrp_instance->totemrrp_deliver_fn (
  741. passive_instance->totemrrp_context,
  742. passive_instance->token,
  743. passive_instance->token_len);
  744. }
  745. /* TODO
  746. static void timer_function_passive_problem_decrementer (void *context)
  747. {
  748. // struct passive_instance *passive_instance = (struct passive_instance *)context;
  749. // struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  750. }
  751. */
  752. static void passive_timer_expired_token_start (
  753. struct passive_instance *passive_instance)
  754. {
  755. poll_timer_add (
  756. passive_instance->rrp_instance->poll_handle,
  757. passive_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  758. (void *)passive_instance,
  759. timer_function_passive_token_expired,
  760. &passive_instance->timer_expired_token);
  761. }
  762. static void passive_timer_expired_token_cancel (
  763. struct passive_instance *passive_instance)
  764. {
  765. poll_timer_delete (
  766. passive_instance->rrp_instance->poll_handle,
  767. passive_instance->timer_expired_token);
  768. }
  769. /*
  770. static void passive_timer_problem_decrementer_start (
  771. struct passive_instance *passive_instance)
  772. {
  773. poll_timer_add (
  774. passive_instance->rrp_instance->poll_handle,
  775. passive_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  776. (void *)passive_instance,
  777. timer_function_passive_problem_decrementer,
  778. &passive_instance->timer_problem_decrementer);
  779. }
  780. static void passive_timer_problem_decrementer_cancel (
  781. struct passive_instance *passive_instance)
  782. {
  783. poll_timer_delete (
  784. passive_instance->rrp_instance->poll_handle,
  785. passive_instance->timer_problem_decrementer);
  786. }
  787. */
  788. /*
  789. * Monitor function implementation from rrp paper.
  790. * rrp_instance is passive rrp instance, iface_no is interface with received messgae/token and
  791. * is_token_recv_count is boolean variable which donates if message is token (>1) or regular
  792. * message (= 0)
  793. */
  794. static void passive_monitor (
  795. struct totemrrp_instance *rrp_instance,
  796. unsigned int iface_no,
  797. int is_token_recv_count)
  798. {
  799. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  800. unsigned int *recv_count;
  801. unsigned int max;
  802. unsigned int i;
  803. unsigned int min_all, min_active;
  804. unsigned int threshold;
  805. /*
  806. * Monitor for failures
  807. */
  808. if (is_token_recv_count) {
  809. recv_count = passive_instance->token_recv_count;
  810. threshold = rrp_instance->totem_config->rrp_problem_count_threshold;
  811. } else {
  812. recv_count = passive_instance->mcast_recv_count;
  813. threshold = rrp_instance->totem_config->rrp_problem_count_mcast_threshold;
  814. }
  815. recv_count[iface_no] += 1;
  816. max = 0;
  817. for (i = 0; i < rrp_instance->interface_count; i++) {
  818. if (max < recv_count[i]) {
  819. max = recv_count[i];
  820. }
  821. }
  822. /*
  823. * Max is larger than threshold -> start adjusting process
  824. */
  825. if (max > PASSIVE_RECV_COUNT_THRESHOLD) {
  826. min_all = min_active = recv_count[iface_no];
  827. for (i = 0; i < rrp_instance->interface_count; i++) {
  828. if (recv_count[i] < min_all) {
  829. min_all = recv_count[i];
  830. }
  831. if (passive_instance->faulty[i] == 0 &&
  832. recv_count[i] < min_active) {
  833. min_active = recv_count[i];
  834. }
  835. }
  836. if (min_all > 0) {
  837. /*
  838. * There is one or more faulty device with recv_count > 0
  839. */
  840. for (i = 0; i < rrp_instance->interface_count; i++) {
  841. recv_count[i] -= min_all;
  842. }
  843. } else {
  844. /*
  845. * No faulty device with recv_count > 0, adjust only active
  846. * devices
  847. */
  848. for (i = 0; i < rrp_instance->interface_count; i++) {
  849. if (passive_instance->faulty[i] == 0) {
  850. recv_count[i] -= min_active;
  851. }
  852. }
  853. }
  854. /*
  855. * Find again max
  856. */
  857. max = 0;
  858. for (i = 0; i < rrp_instance->interface_count; i++) {
  859. if (max < recv_count[i]) {
  860. max = recv_count[i];
  861. }
  862. }
  863. }
  864. for (i = 0; i < rrp_instance->interface_count; i++) {
  865. if ((passive_instance->faulty[i] == 0) &&
  866. (max - recv_count[i] > threshold)) {
  867. passive_instance->faulty[i] = 1;
  868. poll_timer_add (rrp_instance->poll_handle,
  869. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  870. rrp_instance->deliver_fn_context[i],
  871. timer_function_test_ring_timeout,
  872. &rrp_instance->timer_active_test_ring_timeout[i]);
  873. sprintf (rrp_instance->status[i],
  874. "Marking ringid %u interface %s FAULTY",
  875. i,
  876. totemnet_iface_print (rrp_instance->net_handles[i]));
  877. log_printf (
  878. rrp_instance->totemrrp_log_level_error,
  879. "%s",
  880. rrp_instance->status[i]);
  881. }
  882. }
  883. }
  884. static void passive_mcast_recv (
  885. struct totemrrp_instance *rrp_instance,
  886. unsigned int iface_no,
  887. void *context,
  888. const void *msg,
  889. unsigned int msg_len)
  890. {
  891. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  892. rrp_instance->totemrrp_deliver_fn (
  893. context,
  894. msg,
  895. msg_len);
  896. if (rrp_instance->totemrrp_msgs_missing() == 0 &&
  897. passive_instance->timer_expired_token) {
  898. /*
  899. * Delivers the last token
  900. */
  901. rrp_instance->totemrrp_deliver_fn (
  902. passive_instance->totemrrp_context,
  903. passive_instance->token,
  904. passive_instance->token_len);
  905. passive_timer_expired_token_cancel (passive_instance);
  906. }
  907. passive_monitor (rrp_instance, iface_no, 0);
  908. }
  909. static void passive_mcast_flush_send (
  910. struct totemrrp_instance *instance,
  911. const void *msg,
  912. unsigned int msg_len)
  913. {
  914. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  915. int i = 0;
  916. do {
  917. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  918. i++;
  919. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1));
  920. if (i > instance->interface_count) {
  921. /*
  922. * All interfaces are faulty. It's still needed to send mcast
  923. * message to local host so use first interface.
  924. */
  925. passive_instance->msg_xmit_iface = 0;
  926. }
  927. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  928. }
  929. static void passive_mcast_noflush_send (
  930. struct totemrrp_instance *instance,
  931. const void *msg,
  932. unsigned int msg_len)
  933. {
  934. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  935. int i = 0;
  936. do {
  937. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  938. i++;
  939. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1));
  940. if (i > instance->interface_count) {
  941. /*
  942. * All interfaces are faulty. It's still needed to send mcast
  943. * message to local host so use first interface.
  944. */
  945. passive_instance->msg_xmit_iface = 0;
  946. }
  947. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  948. }
  949. static void passive_token_recv (
  950. struct totemrrp_instance *rrp_instance,
  951. unsigned int iface_no,
  952. void *context,
  953. const void *msg,
  954. unsigned int msg_len,
  955. unsigned int token_seq)
  956. {
  957. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  958. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  959. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  960. rrp_instance->totemrrp_deliver_fn (
  961. context,
  962. msg,
  963. msg_len);
  964. } else {
  965. memcpy (passive_instance->token, msg, msg_len);
  966. passive_timer_expired_token_start (passive_instance);
  967. }
  968. passive_monitor (rrp_instance, iface_no, 1);
  969. }
  970. static void passive_token_send (
  971. struct totemrrp_instance *instance,
  972. const void *msg,
  973. unsigned int msg_len)
  974. {
  975. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  976. int i = 0;
  977. do {
  978. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  979. i++;
  980. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->token_xmit_iface] == 1));
  981. if (i > instance->interface_count) {
  982. /*
  983. * All interfaces are faulty. It's still needed to send token
  984. * message to (potentionally) local host so use first interface.
  985. */
  986. passive_instance->msg_xmit_iface = 0;
  987. }
  988. totemnet_token_send (
  989. instance->net_handles[passive_instance->token_xmit_iface],
  990. msg, msg_len);
  991. }
  992. static void passive_recv_flush (struct totemrrp_instance *instance)
  993. {
  994. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  995. unsigned int i;
  996. for (i = 0; i < instance->interface_count; i++) {
  997. if (rrp_algo_instance->faulty[i] == 0) {
  998. totemnet_recv_flush (instance->net_handles[i]);
  999. }
  1000. }
  1001. }
  1002. static void passive_send_flush (struct totemrrp_instance *instance)
  1003. {
  1004. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1005. unsigned int i;
  1006. for (i = 0; i < instance->interface_count; i++) {
  1007. if (rrp_algo_instance->faulty[i] == 0) {
  1008. totemnet_send_flush (instance->net_handles[i]);
  1009. }
  1010. }
  1011. }
  1012. static void passive_iface_check (struct totemrrp_instance *instance)
  1013. {
  1014. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1015. unsigned int i;
  1016. for (i = 0; i < instance->interface_count; i++) {
  1017. if (rrp_algo_instance->faulty[i] == 0) {
  1018. totemnet_iface_check (instance->net_handles[i]);
  1019. }
  1020. }
  1021. }
  1022. static void passive_processor_count_set (
  1023. struct totemrrp_instance *instance,
  1024. unsigned int processor_count)
  1025. {
  1026. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1027. unsigned int i;
  1028. for (i = 0; i < instance->interface_count; i++) {
  1029. if (rrp_algo_instance->faulty[i] == 0) {
  1030. totemnet_processor_count_set (instance->net_handles[i],
  1031. processor_count);
  1032. }
  1033. }
  1034. }
  1035. static void passive_token_target_set (
  1036. struct totemrrp_instance *instance,
  1037. struct totem_ip_address *token_target,
  1038. unsigned int iface_no)
  1039. {
  1040. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1041. }
  1042. static int passive_mcast_recv_empty (
  1043. struct totemrrp_instance *instance)
  1044. {
  1045. int res;
  1046. int msgs_emptied = 0;
  1047. int i;
  1048. for (i = 0; i < instance->interface_count; i++) {
  1049. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1050. if (res == -1) {
  1051. return (-1);
  1052. }
  1053. if (res == 1) {
  1054. msgs_emptied = 1;
  1055. }
  1056. }
  1057. return (msgs_emptied);
  1058. }
  1059. static int passive_member_add (
  1060. struct totemrrp_instance *instance,
  1061. const struct totem_ip_address *member,
  1062. unsigned int iface_no)
  1063. {
  1064. int res;
  1065. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1066. return (res);
  1067. }
  1068. static int passive_member_remove (
  1069. struct totemrrp_instance *instance,
  1070. const struct totem_ip_address *member,
  1071. unsigned int iface_no)
  1072. {
  1073. int res;
  1074. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1075. return (res);
  1076. }
  1077. static void passive_membership_changed (
  1078. struct totemrrp_instance *rrp_instance,
  1079. enum totem_configuration_type configuration_type,
  1080. const struct srp_addr *member_list, size_t member_list_entries,
  1081. const struct srp_addr *left_list, size_t left_list_entries,
  1082. const struct srp_addr *joined_list, size_t joined_list_entries,
  1083. const struct memb_ring_id *ring_id)
  1084. {
  1085. int i;
  1086. int interface;
  1087. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1088. for (i = 0; i < left_list_entries; i++) {
  1089. if (left_list[i].addr[interface].family != AF_INET &&
  1090. left_list[i].addr[interface].family != AF_INET6) {
  1091. log_printf(rrp_instance->totemrrp_log_level_error,
  1092. "Membership left list contains incorrect address. "
  1093. "This is sign of misconfiguration between nodes!");
  1094. } else {
  1095. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1096. &left_list[i].addr[interface], 0);
  1097. }
  1098. }
  1099. for (i = 0; i < joined_list_entries; i++) {
  1100. if (joined_list[i].addr[interface].family != AF_INET &&
  1101. joined_list[i].addr[interface].family != AF_INET6) {
  1102. log_printf(rrp_instance->totemrrp_log_level_error,
  1103. "Membership join list contains incorrect address. "
  1104. "This is sign of misconfiguration between nodes!");
  1105. } else {
  1106. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1107. &joined_list[i].addr[interface], 1);
  1108. }
  1109. }
  1110. }
  1111. }
  1112. static void passive_ring_reenable (
  1113. struct totemrrp_instance *instance,
  1114. unsigned int iface_no)
  1115. {
  1116. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1117. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  1118. instance->interface_count);
  1119. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  1120. instance->interface_count);
  1121. if (iface_no == instance->interface_count) {
  1122. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1123. instance->interface_count);
  1124. } else {
  1125. rrp_algo_instance->faulty[iface_no] = 0;
  1126. }
  1127. }
  1128. /*
  1129. * Active Replication Implementation
  1130. */
  1131. void *active_instance_initialize (
  1132. struct totemrrp_instance *rrp_instance,
  1133. int interface_count)
  1134. {
  1135. struct active_instance *instance;
  1136. instance = malloc (sizeof (struct active_instance));
  1137. if (instance == 0) {
  1138. goto error_exit;
  1139. }
  1140. memset (instance, 0, sizeof (struct active_instance));
  1141. instance->faulty = malloc (sizeof (int) * interface_count);
  1142. if (instance->faulty == 0) {
  1143. free (instance);
  1144. instance = 0;
  1145. goto error_exit;
  1146. }
  1147. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  1148. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  1149. if (instance->last_token_recv == 0) {
  1150. free (instance->faulty);
  1151. free (instance);
  1152. instance = 0;
  1153. goto error_exit;
  1154. }
  1155. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  1156. instance->counter_problems = malloc (sizeof (int) * interface_count);
  1157. if (instance->counter_problems == 0) {
  1158. free (instance->last_token_recv);
  1159. free (instance->faulty);
  1160. free (instance);
  1161. instance = 0;
  1162. goto error_exit;
  1163. }
  1164. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  1165. instance->timer_expired_token = 0;
  1166. instance->timer_problem_decrementer = 0;
  1167. instance->rrp_instance = rrp_instance;
  1168. instance->last_token_seq = ARR_SEQNO_START_TOKEN - 1;
  1169. error_exit:
  1170. return ((void *)instance);
  1171. }
  1172. static void timer_function_active_problem_decrementer (void *context)
  1173. {
  1174. struct active_instance *active_instance = (struct active_instance *)context;
  1175. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1176. unsigned int problem_found = 0;
  1177. unsigned int i;
  1178. for (i = 0; i < rrp_instance->interface_count; i++) {
  1179. if (active_instance->counter_problems[i] > 0) {
  1180. problem_found = 1;
  1181. active_instance->counter_problems[i] -= 1;
  1182. if (active_instance->counter_problems[i] == 0) {
  1183. sprintf (rrp_instance->status[i],
  1184. "ring %d active with no faults", i);
  1185. } else {
  1186. sprintf (rrp_instance->status[i],
  1187. "Decrementing problem counter for iface %s to [%d of %d]",
  1188. totemnet_iface_print (rrp_instance->net_handles[i]),
  1189. active_instance->counter_problems[i],
  1190. rrp_instance->totem_config->rrp_problem_count_threshold);
  1191. }
  1192. log_printf (
  1193. rrp_instance->totemrrp_log_level_warning,
  1194. "%s",
  1195. rrp_instance->status[i]);
  1196. }
  1197. }
  1198. if (problem_found) {
  1199. active_timer_problem_decrementer_start (active_instance);
  1200. } else {
  1201. active_instance->timer_problem_decrementer = 0;
  1202. }
  1203. }
  1204. static void timer_function_active_token_expired (void *context)
  1205. {
  1206. struct active_instance *active_instance = (struct active_instance *)context;
  1207. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1208. unsigned int i;
  1209. for (i = 0; i < rrp_instance->interface_count; i++) {
  1210. if (active_instance->last_token_recv[i] == 0) {
  1211. active_instance->counter_problems[i] += 1;
  1212. if (active_instance->timer_problem_decrementer == 0) {
  1213. active_timer_problem_decrementer_start (active_instance);
  1214. }
  1215. sprintf (rrp_instance->status[i],
  1216. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  1217. active_instance->last_token_seq,
  1218. totemnet_iface_print (rrp_instance->net_handles[i]),
  1219. active_instance->counter_problems[i],
  1220. rrp_instance->totem_config->rrp_problem_count_threshold);
  1221. log_printf (
  1222. rrp_instance->totemrrp_log_level_warning,
  1223. "%s",
  1224. rrp_instance->status[i]);
  1225. }
  1226. }
  1227. for (i = 0; i < rrp_instance->interface_count; i++) {
  1228. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold)
  1229. {
  1230. active_instance->faulty[i] = 1;
  1231. poll_timer_add (rrp_instance->poll_handle,
  1232. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  1233. rrp_instance->deliver_fn_context[i],
  1234. timer_function_test_ring_timeout,
  1235. &rrp_instance->timer_active_test_ring_timeout[i]);
  1236. sprintf (rrp_instance->status[i],
  1237. "Marking seqid %d ringid %u interface %s FAULTY",
  1238. active_instance->last_token_seq,
  1239. i,
  1240. totemnet_iface_print (rrp_instance->net_handles[i]));
  1241. log_printf (
  1242. rrp_instance->totemrrp_log_level_error,
  1243. "%s",
  1244. rrp_instance->status[i]);
  1245. active_timer_problem_decrementer_cancel (active_instance);
  1246. }
  1247. }
  1248. rrp_instance->totemrrp_deliver_fn (
  1249. active_instance->totemrrp_context,
  1250. active_instance->token,
  1251. active_instance->token_len);
  1252. }
  1253. static void active_timer_expired_token_start (
  1254. struct active_instance *active_instance)
  1255. {
  1256. poll_timer_add (
  1257. active_instance->rrp_instance->poll_handle,
  1258. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  1259. (void *)active_instance,
  1260. timer_function_active_token_expired,
  1261. &active_instance->timer_expired_token);
  1262. }
  1263. static void active_timer_expired_token_cancel (
  1264. struct active_instance *active_instance)
  1265. {
  1266. poll_timer_delete (
  1267. active_instance->rrp_instance->poll_handle,
  1268. active_instance->timer_expired_token);
  1269. }
  1270. static void active_timer_problem_decrementer_start (
  1271. struct active_instance *active_instance)
  1272. {
  1273. poll_timer_add (
  1274. active_instance->rrp_instance->poll_handle,
  1275. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  1276. (void *)active_instance,
  1277. timer_function_active_problem_decrementer,
  1278. &active_instance->timer_problem_decrementer);
  1279. }
  1280. static void active_timer_problem_decrementer_cancel (
  1281. struct active_instance *active_instance)
  1282. {
  1283. poll_timer_delete (
  1284. active_instance->rrp_instance->poll_handle,
  1285. active_instance->timer_problem_decrementer);
  1286. active_instance->timer_problem_decrementer = 0;
  1287. }
  1288. /*
  1289. * active replication
  1290. */
  1291. static void active_mcast_recv (
  1292. struct totemrrp_instance *instance,
  1293. unsigned int iface_no,
  1294. void *context,
  1295. const void *msg,
  1296. unsigned int msg_len)
  1297. {
  1298. instance->totemrrp_deliver_fn (
  1299. context,
  1300. msg,
  1301. msg_len);
  1302. }
  1303. static void active_mcast_flush_send (
  1304. struct totemrrp_instance *instance,
  1305. const void *msg,
  1306. unsigned int msg_len)
  1307. {
  1308. int i;
  1309. int msg_sent;
  1310. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1311. msg_sent = 0;
  1312. for (i = 0; i < instance->interface_count; i++) {
  1313. if (rrp_algo_instance->faulty[i] == 0) {
  1314. msg_sent = 1;
  1315. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  1316. }
  1317. }
  1318. if (!msg_sent) {
  1319. /*
  1320. * All interfaces are faulty. It's still needed to send mcast
  1321. * message to local host so use first interface.
  1322. */
  1323. totemnet_mcast_flush_send (instance->net_handles[0], msg, msg_len);
  1324. }
  1325. }
  1326. static void active_mcast_noflush_send (
  1327. struct totemrrp_instance *instance,
  1328. const void *msg,
  1329. unsigned int msg_len)
  1330. {
  1331. int i;
  1332. int msg_sent;
  1333. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1334. msg_sent = 0;
  1335. for (i = 0; i < instance->interface_count; i++) {
  1336. if (rrp_algo_instance->faulty[i] == 0) {
  1337. msg_sent = 1;
  1338. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  1339. }
  1340. }
  1341. if (!msg_sent) {
  1342. /*
  1343. * All interfaces are faulty. It's still needed to send mcast
  1344. * message to local host so use first interface.
  1345. */
  1346. totemnet_mcast_noflush_send (instance->net_handles[0], msg, msg_len);
  1347. }
  1348. }
  1349. static void active_token_recv (
  1350. struct totemrrp_instance *rrp_instance,
  1351. unsigned int iface_no,
  1352. void *context,
  1353. const void *msg,
  1354. unsigned int msg_len,
  1355. unsigned int token_seq)
  1356. {
  1357. int i;
  1358. struct active_instance *active_instance = (struct active_instance *)rrp_instance->rrp_algo_instance;
  1359. active_instance->totemrrp_context = context;
  1360. if (sq_lt_compare (active_instance->last_token_seq, token_seq)) {
  1361. memcpy (active_instance->token, msg, msg_len);
  1362. active_instance->token_len = msg_len;
  1363. for (i = 0; i < rrp_instance->interface_count; i++) {
  1364. active_instance->last_token_recv[i] = 0;
  1365. }
  1366. active_instance->last_token_recv[iface_no] = 1;
  1367. active_timer_expired_token_start (active_instance);
  1368. }
  1369. /*
  1370. * This doesn't follow spec because the spec assumes we will know
  1371. * when token resets occur.
  1372. */
  1373. active_instance->last_token_seq = token_seq;
  1374. if (token_seq == active_instance->last_token_seq) {
  1375. active_instance->last_token_recv[iface_no] = 1;
  1376. for (i = 0; i < rrp_instance->interface_count; i++) {
  1377. if ((active_instance->last_token_recv[i] == 0) &&
  1378. active_instance->faulty[i] == 0) {
  1379. return; /* don't deliver token */
  1380. }
  1381. }
  1382. active_timer_expired_token_cancel (active_instance);
  1383. rrp_instance->totemrrp_deliver_fn (
  1384. context,
  1385. msg,
  1386. msg_len);
  1387. }
  1388. }
  1389. static void active_token_send (
  1390. struct totemrrp_instance *instance,
  1391. const void *msg,
  1392. unsigned int msg_len)
  1393. {
  1394. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1395. int i;
  1396. int msg_sent;
  1397. msg_sent = 0;
  1398. for (i = 0; i < instance->interface_count; i++) {
  1399. if (rrp_algo_instance->faulty[i] == 0) {
  1400. msg_sent = 1;
  1401. totemnet_token_send (
  1402. instance->net_handles[i],
  1403. msg, msg_len);
  1404. }
  1405. }
  1406. if (!msg_sent) {
  1407. /*
  1408. * All interfaces are faulty. It's still needed to send token
  1409. * message to (potentionally) local host so use first interface.
  1410. */
  1411. totemnet_token_send (
  1412. instance->net_handles[0],
  1413. msg, msg_len);
  1414. }
  1415. }
  1416. static void active_recv_flush (struct totemrrp_instance *instance)
  1417. {
  1418. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1419. unsigned int i;
  1420. for (i = 0; i < instance->interface_count; i++) {
  1421. if (rrp_algo_instance->faulty[i] == 0) {
  1422. totemnet_recv_flush (instance->net_handles[i]);
  1423. }
  1424. }
  1425. }
  1426. static void active_send_flush (struct totemrrp_instance *instance)
  1427. {
  1428. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1429. unsigned int i;
  1430. for (i = 0; i < instance->interface_count; i++) {
  1431. if (rrp_algo_instance->faulty[i] == 0) {
  1432. totemnet_send_flush (instance->net_handles[i]);
  1433. }
  1434. }
  1435. }
  1436. static int active_member_add (
  1437. struct totemrrp_instance *instance,
  1438. const struct totem_ip_address *member,
  1439. unsigned int iface_no)
  1440. {
  1441. int res;
  1442. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1443. return (res);
  1444. }
  1445. static int active_member_remove (
  1446. struct totemrrp_instance *instance,
  1447. const struct totem_ip_address *member,
  1448. unsigned int iface_no)
  1449. {
  1450. int res;
  1451. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1452. return (res);
  1453. }
  1454. static void active_membership_changed (
  1455. struct totemrrp_instance *rrp_instance,
  1456. enum totem_configuration_type configuration_type,
  1457. const struct srp_addr *member_list, size_t member_list_entries,
  1458. const struct srp_addr *left_list, size_t left_list_entries,
  1459. const struct srp_addr *joined_list, size_t joined_list_entries,
  1460. const struct memb_ring_id *ring_id)
  1461. {
  1462. int i;
  1463. int interface;
  1464. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1465. for (i = 0; i < left_list_entries; i++) {
  1466. if (left_list[i].addr[interface].family != AF_INET &&
  1467. left_list[i].addr[interface].family != AF_INET6) {
  1468. log_printf(rrp_instance->totemrrp_log_level_error,
  1469. "Membership left list contains incorrect address. "
  1470. "This is sign of misconfiguration between nodes!");
  1471. } else {
  1472. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1473. &left_list[i].addr[interface], 0);
  1474. }
  1475. }
  1476. for (i = 0; i < joined_list_entries; i++) {
  1477. if (joined_list[i].addr[interface].family != AF_INET &&
  1478. joined_list[i].addr[interface].family != AF_INET6) {
  1479. log_printf(rrp_instance->totemrrp_log_level_error,
  1480. "Membership join list contains incorrect address. "
  1481. "This is sign of misconfiguration between nodes!");
  1482. } else {
  1483. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1484. &joined_list[i].addr[interface], 1);
  1485. }
  1486. }
  1487. }
  1488. }
  1489. static void active_iface_check (struct totemrrp_instance *instance)
  1490. {
  1491. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1492. unsigned int i;
  1493. for (i = 0; i < instance->interface_count; i++) {
  1494. if (rrp_algo_instance->faulty[i] == 0) {
  1495. totemnet_iface_check (instance->net_handles[i]);
  1496. }
  1497. }
  1498. }
  1499. static void active_processor_count_set (
  1500. struct totemrrp_instance *instance,
  1501. unsigned int processor_count)
  1502. {
  1503. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1504. unsigned int i;
  1505. for (i = 0; i < instance->interface_count; i++) {
  1506. if (rrp_algo_instance->faulty[i] == 0) {
  1507. totemnet_processor_count_set (instance->net_handles[i],
  1508. processor_count);
  1509. }
  1510. }
  1511. }
  1512. static void active_token_target_set (
  1513. struct totemrrp_instance *instance,
  1514. struct totem_ip_address *token_target,
  1515. unsigned int iface_no)
  1516. {
  1517. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1518. }
  1519. static int active_mcast_recv_empty (
  1520. struct totemrrp_instance *instance)
  1521. {
  1522. int res;
  1523. int msgs_emptied = 0;
  1524. int i;
  1525. for (i = 0; i < instance->interface_count; i++) {
  1526. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1527. if (res == -1) {
  1528. return (-1);
  1529. }
  1530. if (res == 1) {
  1531. msgs_emptied = 1;
  1532. }
  1533. }
  1534. return (msgs_emptied);
  1535. }
  1536. static void active_ring_reenable (
  1537. struct totemrrp_instance *instance,
  1538. unsigned int iface_no)
  1539. {
  1540. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1541. if (iface_no == instance->interface_count) {
  1542. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1543. instance->interface_count);
  1544. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1545. instance->interface_count);
  1546. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1547. instance->interface_count);
  1548. } else {
  1549. rrp_algo_instance->last_token_recv[iface_no] = 0;
  1550. rrp_algo_instance->faulty[iface_no] = 0;
  1551. rrp_algo_instance->counter_problems[iface_no] = 0;
  1552. }
  1553. }
  1554. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1555. {
  1556. memset (instance, 0, sizeof (struct totemrrp_instance));
  1557. }
  1558. static int totemrrp_algorithm_set (
  1559. struct totem_config *totem_config,
  1560. struct totemrrp_instance *instance)
  1561. {
  1562. unsigned int res = -1;
  1563. unsigned int i;
  1564. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1565. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1566. instance->rrp_algo = rrp_algos[i];
  1567. if (rrp_algos[i]->initialize) {
  1568. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1569. instance,
  1570. totem_config->interface_count);
  1571. }
  1572. res = 0;
  1573. break;
  1574. }
  1575. }
  1576. for (i = 0; i < totem_config->interface_count; i++) {
  1577. instance->status[i] = malloc (1024);
  1578. sprintf (instance->status[i], "ring %d active with no faults", i);
  1579. }
  1580. return (res);
  1581. }
  1582. void rrp_deliver_fn (
  1583. void *context,
  1584. const void *msg,
  1585. unsigned int msg_len)
  1586. {
  1587. unsigned int token_seqid;
  1588. unsigned int token_is;
  1589. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1590. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  1591. const struct message_header *hdr = msg;
  1592. struct message_header tmp_msg, activate_msg;
  1593. memset(&tmp_msg, 0, sizeof(struct message_header));
  1594. memset(&activate_msg, 0, sizeof(struct message_header));
  1595. rrp_instance->totemrrp_token_seqid_get (
  1596. msg,
  1597. &token_seqid,
  1598. &token_is);
  1599. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVE) {
  1600. log_printf (
  1601. rrp_instance->totemrrp_log_level_debug,
  1602. "received message requesting test of ring now active\n");
  1603. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1604. test_active_msg_endian_convert(hdr, &tmp_msg);
  1605. hdr = &tmp_msg;
  1606. }
  1607. if (hdr->nodeid_activator == rrp_instance->my_nodeid) {
  1608. /*
  1609. * Send an activate message
  1610. */
  1611. activate_msg.type = MESSAGE_TYPE_RING_TEST_ACTIVATE;
  1612. activate_msg.endian_detector = ENDIAN_LOCAL;
  1613. activate_msg.ring_number = hdr->ring_number;
  1614. activate_msg.nodeid_activator = rrp_instance->my_nodeid;
  1615. totemnet_token_send (
  1616. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1617. &activate_msg, sizeof (struct message_header));
  1618. } else {
  1619. /*
  1620. * Send a ring test message
  1621. */
  1622. totemnet_token_send (
  1623. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1624. msg, msg_len);
  1625. }
  1626. } else
  1627. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVATE) {
  1628. log_printf (
  1629. rrp_instance->totemrrp_log_level_notice,
  1630. "Automatically recovered ring %d\n", hdr->ring_number);
  1631. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1632. test_active_msg_endian_convert(hdr, &tmp_msg);
  1633. hdr = &tmp_msg;
  1634. }
  1635. totemrrp_ring_reenable (rrp_instance, deliver_fn_context->iface_no);
  1636. if (hdr->nodeid_activator != rrp_instance->my_nodeid) {
  1637. totemnet_token_send (
  1638. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1639. msg, msg_len);
  1640. }
  1641. } else
  1642. if (token_is) {
  1643. /*
  1644. * Deliver to the token receiver for this rrp algorithm
  1645. */
  1646. rrp_instance->rrp_algo->token_recv (
  1647. rrp_instance,
  1648. deliver_fn_context->iface_no,
  1649. deliver_fn_context->context,
  1650. msg,
  1651. msg_len,
  1652. token_seqid);
  1653. } else {
  1654. /*
  1655. * Deliver to the mcast receiver for this rrp algorithm
  1656. */
  1657. rrp_instance->rrp_algo->mcast_recv (
  1658. rrp_instance,
  1659. deliver_fn_context->iface_no,
  1660. deliver_fn_context->context,
  1661. msg,
  1662. msg_len);
  1663. }
  1664. }
  1665. void rrp_iface_change_fn (
  1666. void *context,
  1667. const struct totem_ip_address *iface_addr)
  1668. {
  1669. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1670. deliver_fn_context->instance->my_nodeid = iface_addr->nodeid;
  1671. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1672. deliver_fn_context->context,
  1673. iface_addr,
  1674. deliver_fn_context->iface_no);
  1675. }
  1676. int totemrrp_finalize (
  1677. void *rrp_context)
  1678. {
  1679. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1680. int i;
  1681. for (i = 0; i < instance->interface_count; i++) {
  1682. totemnet_finalize (instance->net_handles[i]);
  1683. }
  1684. return (0);
  1685. }
  1686. static void rrp_target_set_completed (void *context)
  1687. {
  1688. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1689. deliver_fn_context->instance->totemrrp_target_set_completed (deliver_fn_context->context);
  1690. }
  1691. /*
  1692. * Totem Redundant Ring interface
  1693. * depends on poll abstraction, POSIX, IPV4
  1694. */
  1695. /*
  1696. * Create an instance
  1697. */
  1698. int totemrrp_initialize (
  1699. hdb_handle_t poll_handle,
  1700. void **rrp_context,
  1701. struct totem_config *totem_config,
  1702. totemsrp_stats_t *stats,
  1703. void *context,
  1704. void (*deliver_fn) (
  1705. void *context,
  1706. const void *msg,
  1707. unsigned int msg_len),
  1708. void (*iface_change_fn) (
  1709. void *context,
  1710. const struct totem_ip_address *iface_addr,
  1711. unsigned int iface_no),
  1712. void (*token_seqid_get) (
  1713. const void *msg,
  1714. unsigned int *seqid,
  1715. unsigned int *token_is),
  1716. unsigned int (*msgs_missing) (void),
  1717. void (*target_set_completed) (void *context))
  1718. {
  1719. struct totemrrp_instance *instance;
  1720. unsigned int res;
  1721. int i;
  1722. instance = malloc (sizeof (struct totemrrp_instance));
  1723. if (instance == 0) {
  1724. return (-1);
  1725. }
  1726. totemrrp_instance_initialize (instance);
  1727. instance->totem_config = totem_config;
  1728. res = totemrrp_algorithm_set (
  1729. instance->totem_config,
  1730. instance);
  1731. if (res == -1) {
  1732. goto error_destroy;
  1733. }
  1734. /*
  1735. * Configure logging
  1736. */
  1737. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1738. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1739. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1740. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1741. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1742. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1743. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1744. instance->interfaces = totem_config->interfaces;
  1745. instance->poll_handle = poll_handle;
  1746. instance->totemrrp_deliver_fn = deliver_fn;
  1747. instance->totemrrp_iface_change_fn = iface_change_fn;
  1748. instance->totemrrp_token_seqid_get = token_seqid_get;
  1749. instance->totemrrp_target_set_completed = target_set_completed;
  1750. instance->totemrrp_msgs_missing = msgs_missing;
  1751. instance->interface_count = totem_config->interface_count;
  1752. instance->net_handles = malloc (sizeof (void *) * totem_config->interface_count);
  1753. instance->context = context;
  1754. instance->poll_handle = poll_handle;
  1755. for (i = 0; i < totem_config->interface_count; i++) {
  1756. struct deliver_fn_context *deliver_fn_context;
  1757. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1758. assert (deliver_fn_context);
  1759. deliver_fn_context->instance = instance;
  1760. deliver_fn_context->context = context;
  1761. deliver_fn_context->iface_no = i;
  1762. instance->deliver_fn_context[i] = (void *)deliver_fn_context;
  1763. totemnet_initialize (
  1764. poll_handle,
  1765. &instance->net_handles[i],
  1766. totem_config,
  1767. stats,
  1768. i,
  1769. (void *)deliver_fn_context,
  1770. rrp_deliver_fn,
  1771. rrp_iface_change_fn,
  1772. rrp_target_set_completed);
  1773. totemnet_net_mtu_adjust (instance->net_handles[i], totem_config);
  1774. }
  1775. *rrp_context = instance;
  1776. return (0);
  1777. error_destroy:
  1778. free (instance);
  1779. return (res);
  1780. }
  1781. int totemrrp_processor_count_set (
  1782. void *rrp_context,
  1783. unsigned int processor_count)
  1784. {
  1785. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1786. instance->rrp_algo->processor_count_set (instance, processor_count);
  1787. instance->processor_count = processor_count;
  1788. return (0);
  1789. }
  1790. int totemrrp_token_target_set (
  1791. void *rrp_context,
  1792. struct totem_ip_address *addr,
  1793. unsigned int iface_no)
  1794. {
  1795. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1796. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1797. return (0);
  1798. }
  1799. int totemrrp_recv_flush (void *rrp_context)
  1800. {
  1801. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1802. instance->rrp_algo->recv_flush (instance);
  1803. return (0);
  1804. }
  1805. int totemrrp_send_flush (void *rrp_context)
  1806. {
  1807. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1808. instance->rrp_algo->send_flush (instance);
  1809. return (0);
  1810. }
  1811. int totemrrp_token_send (
  1812. void *rrp_context,
  1813. const void *msg,
  1814. unsigned int msg_len)
  1815. {
  1816. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1817. instance->rrp_algo->token_send (instance, msg, msg_len);
  1818. return (0);
  1819. }
  1820. int totemrrp_mcast_flush_send (
  1821. void *rrp_context,
  1822. const void *msg,
  1823. unsigned int msg_len)
  1824. {
  1825. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1826. int res = 0;
  1827. // TODO this needs to return the result
  1828. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1829. return (res);
  1830. }
  1831. int totemrrp_mcast_noflush_send (
  1832. void *rrp_context,
  1833. const void *msg,
  1834. unsigned int msg_len)
  1835. {
  1836. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1837. /*
  1838. * merge detects go out through mcast_flush_send so it is safe to
  1839. * flush these messages if we are only one processor. This avoids
  1840. * an encryption/hmac and decryption/hmac
  1841. */
  1842. if (instance->processor_count > 1) {
  1843. // TODO this needs to return the result
  1844. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1845. }
  1846. return (0);
  1847. }
  1848. int totemrrp_iface_check (void *rrp_context)
  1849. {
  1850. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1851. instance->rrp_algo->iface_check (instance);
  1852. return (0);
  1853. }
  1854. int totemrrp_ifaces_get (
  1855. void *rrp_context,
  1856. char ***status,
  1857. unsigned int *iface_count)
  1858. {
  1859. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1860. *status = instance->status;
  1861. if (iface_count) {
  1862. *iface_count = instance->interface_count;
  1863. }
  1864. return (0);
  1865. }
  1866. int totemrrp_crypto_set (
  1867. void *rrp_context,
  1868. unsigned int type)
  1869. {
  1870. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1871. int res;
  1872. res = totemnet_crypto_set(instance->net_handles[0], type);
  1873. return (res);
  1874. }
  1875. /*
  1876. * iface_no indicates the interface number [0, ..., interface_count-1] of the
  1877. * specific ring which will be reenabled. We specify iface_no == interface_count
  1878. * means reenabling all the rings.
  1879. */
  1880. int totemrrp_ring_reenable (
  1881. void *rrp_context,
  1882. unsigned int iface_no)
  1883. {
  1884. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1885. int res = 0;
  1886. unsigned int i;
  1887. instance->rrp_algo->ring_reenable (instance, iface_no);
  1888. if (iface_no == instance->interface_count) {
  1889. for (i = 0; i < instance->interface_count; i++) {
  1890. sprintf (instance->status[i], "ring %d active with no faults", i);
  1891. }
  1892. } else {
  1893. sprintf (instance->status[iface_no], "ring %d active with no faults", iface_no);
  1894. }
  1895. return (res);
  1896. }
  1897. extern int totemrrp_mcast_recv_empty (
  1898. void *rrp_context)
  1899. {
  1900. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1901. int res;
  1902. res = instance->rrp_algo->mcast_recv_empty (instance);
  1903. return (res);
  1904. }
  1905. int totemrrp_member_add (
  1906. void *rrp_context,
  1907. const struct totem_ip_address *member,
  1908. int iface_no)
  1909. {
  1910. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1911. int res;
  1912. res = instance->rrp_algo->member_add (instance, member, iface_no);
  1913. return (res);
  1914. }
  1915. int totemrrp_member_remove (
  1916. void *rrp_context,
  1917. const struct totem_ip_address *member,
  1918. int iface_no)
  1919. {
  1920. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1921. int res;
  1922. res = instance->rrp_algo->member_remove (instance, member, iface_no);
  1923. return (res);
  1924. }
  1925. void totemrrp_membership_changed (
  1926. void *rrp_context,
  1927. enum totem_configuration_type configuration_type,
  1928. const struct srp_addr *member_list, size_t member_list_entries,
  1929. const struct srp_addr *left_list, size_t left_list_entries,
  1930. const struct srp_addr *joined_list, size_t joined_list_entries,
  1931. const struct memb_ring_id *ring_id)
  1932. {
  1933. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1934. instance->rrp_algo->membership_changed (instance,
  1935. configuration_type,
  1936. member_list, member_list_entries,
  1937. left_list, left_list_entries,
  1938. joined_list, joined_list_entries,
  1939. ring_id);
  1940. }