4
0

totemrrp.c 63 KB

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