totemrrp.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327
  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 then 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. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  941. }
  942. }
  943. static void passive_mcast_noflush_send (
  944. struct totemrrp_instance *instance,
  945. const void *msg,
  946. unsigned int msg_len)
  947. {
  948. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  949. int i = 0;
  950. do {
  951. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  952. i++;
  953. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1));
  954. if (i <= instance->interface_count) {
  955. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  956. }
  957. }
  958. static void passive_token_recv (
  959. struct totemrrp_instance *rrp_instance,
  960. unsigned int iface_no,
  961. void *context,
  962. const void *msg,
  963. unsigned int msg_len,
  964. unsigned int token_seq)
  965. {
  966. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  967. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  968. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  969. rrp_instance->totemrrp_deliver_fn (
  970. context,
  971. msg,
  972. msg_len);
  973. } else {
  974. memcpy (passive_instance->token, msg, msg_len);
  975. passive_timer_expired_token_start (passive_instance);
  976. }
  977. passive_monitor (rrp_instance, iface_no, 1);
  978. }
  979. static void passive_token_send (
  980. struct totemrrp_instance *instance,
  981. const void *msg,
  982. unsigned int msg_len)
  983. {
  984. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  985. int i = 0;
  986. do {
  987. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  988. i++;
  989. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->token_xmit_iface] == 1));
  990. if (i <= instance->interface_count) {
  991. totemnet_token_send (
  992. instance->net_handles[passive_instance->token_xmit_iface],
  993. msg, msg_len);
  994. }
  995. }
  996. static void passive_recv_flush (struct totemrrp_instance *instance)
  997. {
  998. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  999. unsigned int i;
  1000. for (i = 0; i < instance->interface_count; i++) {
  1001. if (rrp_algo_instance->faulty[i] == 0) {
  1002. totemnet_recv_flush (instance->net_handles[i]);
  1003. }
  1004. }
  1005. }
  1006. static void passive_send_flush (struct totemrrp_instance *instance)
  1007. {
  1008. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1009. unsigned int i;
  1010. for (i = 0; i < instance->interface_count; i++) {
  1011. if (rrp_algo_instance->faulty[i] == 0) {
  1012. totemnet_send_flush (instance->net_handles[i]);
  1013. }
  1014. }
  1015. }
  1016. static void passive_iface_check (struct totemrrp_instance *instance)
  1017. {
  1018. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1019. unsigned int i;
  1020. for (i = 0; i < instance->interface_count; i++) {
  1021. if (rrp_algo_instance->faulty[i] == 0) {
  1022. totemnet_iface_check (instance->net_handles[i]);
  1023. }
  1024. }
  1025. }
  1026. static void passive_processor_count_set (
  1027. struct totemrrp_instance *instance,
  1028. unsigned int processor_count)
  1029. {
  1030. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1031. unsigned int i;
  1032. for (i = 0; i < instance->interface_count; i++) {
  1033. if (rrp_algo_instance->faulty[i] == 0) {
  1034. totemnet_processor_count_set (instance->net_handles[i],
  1035. processor_count);
  1036. }
  1037. }
  1038. }
  1039. static void passive_token_target_set (
  1040. struct totemrrp_instance *instance,
  1041. struct totem_ip_address *token_target,
  1042. unsigned int iface_no)
  1043. {
  1044. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1045. }
  1046. static int passive_mcast_recv_empty (
  1047. struct totemrrp_instance *instance)
  1048. {
  1049. int res;
  1050. int msgs_emptied = 0;
  1051. int i;
  1052. for (i = 0; i < instance->interface_count; i++) {
  1053. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1054. if (res == -1) {
  1055. return (-1);
  1056. }
  1057. if (res == 1) {
  1058. msgs_emptied = 1;
  1059. }
  1060. }
  1061. return (msgs_emptied);
  1062. }
  1063. static int passive_member_add (
  1064. struct totemrrp_instance *instance,
  1065. const struct totem_ip_address *member,
  1066. unsigned int iface_no)
  1067. {
  1068. int res;
  1069. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1070. return (res);
  1071. }
  1072. static int passive_member_remove (
  1073. struct totemrrp_instance *instance,
  1074. const struct totem_ip_address *member,
  1075. unsigned int iface_no)
  1076. {
  1077. int res;
  1078. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1079. return (res);
  1080. }
  1081. static void passive_membership_changed (
  1082. struct totemrrp_instance *rrp_instance,
  1083. enum totem_configuration_type configuration_type,
  1084. const struct srp_addr *member_list, size_t member_list_entries,
  1085. const struct srp_addr *left_list, size_t left_list_entries,
  1086. const struct srp_addr *joined_list, size_t joined_list_entries,
  1087. const struct memb_ring_id *ring_id)
  1088. {
  1089. int i;
  1090. int interface;
  1091. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1092. for (i = 0; i < left_list_entries; i++) {
  1093. if (left_list->no_addrs < interface + 1 ||
  1094. (left_list[i].addr[interface].family != AF_INET &&
  1095. left_list[i].addr[interface].family != AF_INET6)) {
  1096. log_printf(rrp_instance->totemrrp_log_level_error,
  1097. "Membership left list contains incorrect address. "
  1098. "This is sign of misconfiguration between nodes!");
  1099. } else {
  1100. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1101. &left_list[i].addr[interface], 0);
  1102. }
  1103. }
  1104. for (i = 0; i < joined_list_entries; i++) {
  1105. if (joined_list->no_addrs < interface + 1 ||
  1106. (joined_list[i].addr[interface].family != AF_INET &&
  1107. joined_list[i].addr[interface].family != AF_INET6)) {
  1108. log_printf(rrp_instance->totemrrp_log_level_error,
  1109. "Membership join list contains incorrect address. "
  1110. "This is sign of misconfiguration between nodes!");
  1111. } else {
  1112. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1113. &joined_list[i].addr[interface], 1);
  1114. }
  1115. }
  1116. }
  1117. }
  1118. static void passive_ring_reenable (
  1119. struct totemrrp_instance *instance,
  1120. unsigned int iface_no)
  1121. {
  1122. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1123. int i;
  1124. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  1125. instance->interface_count);
  1126. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  1127. instance->interface_count);
  1128. if (iface_no == instance->interface_count) {
  1129. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1130. instance->interface_count);
  1131. for (i = 0; i < instance->interface_count; i++) {
  1132. stats_set_interface_faulty (instance, i, 0);
  1133. }
  1134. } else {
  1135. rrp_algo_instance->faulty[iface_no] = 0;
  1136. stats_set_interface_faulty (instance, iface_no, 0);
  1137. }
  1138. }
  1139. /*
  1140. * Active Replication Implementation
  1141. */
  1142. void *active_instance_initialize (
  1143. struct totemrrp_instance *rrp_instance,
  1144. int interface_count)
  1145. {
  1146. struct active_instance *instance;
  1147. int i;
  1148. instance = malloc (sizeof (struct active_instance));
  1149. if (instance == 0) {
  1150. goto error_exit;
  1151. }
  1152. memset (instance, 0, sizeof (struct active_instance));
  1153. instance->faulty = malloc (sizeof (int) * interface_count);
  1154. if (instance->faulty == 0) {
  1155. free (instance);
  1156. instance = 0;
  1157. goto error_exit;
  1158. }
  1159. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  1160. for (i = 0; i < interface_count; i++) {
  1161. stats_set_interface_faulty (rrp_instance, i, 0);
  1162. }
  1163. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  1164. if (instance->last_token_recv == 0) {
  1165. free (instance->faulty);
  1166. free (instance);
  1167. instance = 0;
  1168. goto error_exit;
  1169. }
  1170. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  1171. instance->counter_problems = malloc (sizeof (int) * interface_count);
  1172. if (instance->counter_problems == 0) {
  1173. free (instance->last_token_recv);
  1174. free (instance->faulty);
  1175. free (instance);
  1176. instance = 0;
  1177. goto error_exit;
  1178. }
  1179. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  1180. instance->timer_expired_token = 0;
  1181. instance->timer_problem_decrementer = 0;
  1182. instance->rrp_instance = rrp_instance;
  1183. instance->last_token_seq = ARR_SEQNO_START_TOKEN - 1;
  1184. error_exit:
  1185. return ((void *)instance);
  1186. }
  1187. static void timer_function_active_problem_decrementer (void *context)
  1188. {
  1189. struct active_instance *active_instance = (struct active_instance *)context;
  1190. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1191. unsigned int problem_found = 0;
  1192. unsigned int i;
  1193. for (i = 0; i < rrp_instance->interface_count; i++) {
  1194. if (active_instance->counter_problems[i] > 0) {
  1195. problem_found = 1;
  1196. active_instance->counter_problems[i] -= 1;
  1197. if (active_instance->counter_problems[i] == 0) {
  1198. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1199. "ring %d active with no faults", i);
  1200. } else {
  1201. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1202. "Decrementing problem counter for iface %s to [%d of %d]",
  1203. totemnet_iface_print (rrp_instance->net_handles[i]),
  1204. active_instance->counter_problems[i],
  1205. rrp_instance->totem_config->rrp_problem_count_threshold);
  1206. }
  1207. log_printf (
  1208. rrp_instance->totemrrp_log_level_warning,
  1209. "%s",
  1210. rrp_instance->status[i]);
  1211. }
  1212. }
  1213. if (problem_found) {
  1214. active_timer_problem_decrementer_start (active_instance);
  1215. } else {
  1216. active_instance->timer_problem_decrementer = 0;
  1217. }
  1218. }
  1219. static void timer_function_active_token_expired (void *context)
  1220. {
  1221. struct active_instance *active_instance = (struct active_instance *)context;
  1222. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1223. unsigned int i;
  1224. for (i = 0; i < rrp_instance->interface_count; i++) {
  1225. if (active_instance->last_token_recv[i] == 0) {
  1226. active_instance->counter_problems[i] += 1;
  1227. if (active_instance->timer_problem_decrementer == 0) {
  1228. active_timer_problem_decrementer_start (active_instance);
  1229. }
  1230. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1231. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  1232. active_instance->last_token_seq,
  1233. totemnet_iface_print (rrp_instance->net_handles[i]),
  1234. active_instance->counter_problems[i],
  1235. rrp_instance->totem_config->rrp_problem_count_threshold);
  1236. log_printf (
  1237. rrp_instance->totemrrp_log_level_warning,
  1238. "%s",
  1239. rrp_instance->status[i]);
  1240. }
  1241. }
  1242. for (i = 0; i < rrp_instance->interface_count; i++) {
  1243. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold &&
  1244. active_instance->faulty[i] == 0) {
  1245. active_instance->faulty[i] = 1;
  1246. qb_loop_timer_add (rrp_instance->poll_handle,
  1247. QB_LOOP_MED,
  1248. rrp_instance->totem_config->rrp_autorecovery_check_timeout*QB_TIME_NS_IN_MSEC,
  1249. rrp_instance->deliver_fn_context[i],
  1250. timer_function_test_ring_timeout,
  1251. &rrp_instance->timer_active_test_ring_timeout[i]);
  1252. stats_set_interface_faulty (rrp_instance, i, active_instance->faulty[i]);
  1253. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1254. "Marking seqid %d ringid %u interface %s FAULTY",
  1255. active_instance->last_token_seq,
  1256. i,
  1257. totemnet_iface_print (rrp_instance->net_handles[i]));
  1258. log_printf (
  1259. rrp_instance->totemrrp_log_level_error,
  1260. "%s",
  1261. rrp_instance->status[i]);
  1262. active_timer_problem_decrementer_cancel (active_instance);
  1263. }
  1264. }
  1265. rrp_instance->totemrrp_deliver_fn (
  1266. active_instance->totemrrp_context,
  1267. active_instance->token,
  1268. active_instance->token_len);
  1269. }
  1270. static void active_timer_expired_token_start (
  1271. struct active_instance *active_instance)
  1272. {
  1273. qb_loop_timer_add (
  1274. active_instance->rrp_instance->poll_handle,
  1275. QB_LOOP_MED,
  1276. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout*QB_TIME_NS_IN_MSEC,
  1277. (void *)active_instance,
  1278. timer_function_active_token_expired,
  1279. &active_instance->timer_expired_token);
  1280. }
  1281. static void active_timer_expired_token_cancel (
  1282. struct active_instance *active_instance)
  1283. {
  1284. qb_loop_timer_del (
  1285. active_instance->rrp_instance->poll_handle,
  1286. active_instance->timer_expired_token);
  1287. }
  1288. static void active_timer_problem_decrementer_start (
  1289. struct active_instance *active_instance)
  1290. {
  1291. qb_loop_timer_add (
  1292. active_instance->rrp_instance->poll_handle,
  1293. QB_LOOP_MED,
  1294. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout*QB_TIME_NS_IN_MSEC,
  1295. (void *)active_instance,
  1296. timer_function_active_problem_decrementer,
  1297. &active_instance->timer_problem_decrementer);
  1298. }
  1299. static void active_timer_problem_decrementer_cancel (
  1300. struct active_instance *active_instance)
  1301. {
  1302. qb_loop_timer_del (
  1303. active_instance->rrp_instance->poll_handle,
  1304. active_instance->timer_problem_decrementer);
  1305. active_instance->timer_problem_decrementer = 0;
  1306. }
  1307. /*
  1308. * active replication
  1309. */
  1310. static void active_mcast_recv (
  1311. struct totemrrp_instance *instance,
  1312. unsigned int iface_no,
  1313. void *context,
  1314. const void *msg,
  1315. unsigned int msg_len)
  1316. {
  1317. instance->totemrrp_deliver_fn (
  1318. context,
  1319. msg,
  1320. msg_len);
  1321. }
  1322. static void active_mcast_flush_send (
  1323. struct totemrrp_instance *instance,
  1324. const void *msg,
  1325. unsigned int msg_len)
  1326. {
  1327. int i;
  1328. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1329. for (i = 0; i < instance->interface_count; i++) {
  1330. if (rrp_algo_instance->faulty[i] == 0) {
  1331. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  1332. }
  1333. }
  1334. }
  1335. static void active_mcast_noflush_send (
  1336. struct totemrrp_instance *instance,
  1337. const void *msg,
  1338. unsigned int msg_len)
  1339. {
  1340. int i;
  1341. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1342. for (i = 0; i < instance->interface_count; i++) {
  1343. if (rrp_algo_instance->faulty[i] == 0) {
  1344. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  1345. }
  1346. }
  1347. }
  1348. static void active_token_recv (
  1349. struct totemrrp_instance *rrp_instance,
  1350. unsigned int iface_no,
  1351. void *context,
  1352. const void *msg,
  1353. unsigned int msg_len,
  1354. unsigned int token_seq)
  1355. {
  1356. int i;
  1357. struct active_instance *active_instance = (struct active_instance *)rrp_instance->rrp_algo_instance;
  1358. active_instance->totemrrp_context = context;
  1359. if (sq_lt_compare (active_instance->last_token_seq, token_seq)) {
  1360. memcpy (active_instance->token, msg, msg_len);
  1361. active_instance->token_len = msg_len;
  1362. for (i = 0; i < rrp_instance->interface_count; i++) {
  1363. active_instance->last_token_recv[i] = 0;
  1364. }
  1365. active_instance->last_token_recv[iface_no] = 1;
  1366. active_timer_expired_token_start (active_instance);
  1367. }
  1368. /*
  1369. * This doesn't follow spec because the spec assumes we will know
  1370. * when token resets occur.
  1371. */
  1372. active_instance->last_token_seq = token_seq;
  1373. if (token_seq == active_instance->last_token_seq) {
  1374. active_instance->last_token_recv[iface_no] = 1;
  1375. for (i = 0; i < rrp_instance->interface_count; i++) {
  1376. if ((active_instance->last_token_recv[i] == 0) &&
  1377. active_instance->faulty[i] == 0) {
  1378. return; /* don't deliver token */
  1379. }
  1380. }
  1381. active_timer_expired_token_cancel (active_instance);
  1382. rrp_instance->totemrrp_deliver_fn (
  1383. context,
  1384. msg,
  1385. msg_len);
  1386. }
  1387. }
  1388. static void active_token_send (
  1389. struct totemrrp_instance *instance,
  1390. const void *msg,
  1391. unsigned int msg_len)
  1392. {
  1393. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1394. int i;
  1395. for (i = 0; i < instance->interface_count; i++) {
  1396. if (rrp_algo_instance->faulty[i] == 0) {
  1397. totemnet_token_send (
  1398. instance->net_handles[i],
  1399. msg, msg_len);
  1400. }
  1401. }
  1402. }
  1403. static void active_recv_flush (struct totemrrp_instance *instance)
  1404. {
  1405. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1406. unsigned int i;
  1407. for (i = 0; i < instance->interface_count; i++) {
  1408. if (rrp_algo_instance->faulty[i] == 0) {
  1409. totemnet_recv_flush (instance->net_handles[i]);
  1410. }
  1411. }
  1412. }
  1413. static void active_send_flush (struct totemrrp_instance *instance)
  1414. {
  1415. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1416. unsigned int i;
  1417. for (i = 0; i < instance->interface_count; i++) {
  1418. if (rrp_algo_instance->faulty[i] == 0) {
  1419. totemnet_send_flush (instance->net_handles[i]);
  1420. }
  1421. }
  1422. }
  1423. static int active_member_add (
  1424. struct totemrrp_instance *instance,
  1425. const struct totem_ip_address *member,
  1426. unsigned int iface_no)
  1427. {
  1428. int res;
  1429. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1430. return (res);
  1431. }
  1432. static int active_member_remove (
  1433. struct totemrrp_instance *instance,
  1434. const struct totem_ip_address *member,
  1435. unsigned int iface_no)
  1436. {
  1437. int res;
  1438. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1439. return (res);
  1440. }
  1441. static void active_membership_changed (
  1442. struct totemrrp_instance *rrp_instance,
  1443. enum totem_configuration_type configuration_type,
  1444. const struct srp_addr *member_list, size_t member_list_entries,
  1445. const struct srp_addr *left_list, size_t left_list_entries,
  1446. const struct srp_addr *joined_list, size_t joined_list_entries,
  1447. const struct memb_ring_id *ring_id)
  1448. {
  1449. int i;
  1450. int interface;
  1451. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1452. for (i = 0; i < left_list_entries; i++) {
  1453. if (left_list->no_addrs < interface + 1 ||
  1454. (left_list[i].addr[interface].family != AF_INET &&
  1455. left_list[i].addr[interface].family != AF_INET6)) {
  1456. log_printf(rrp_instance->totemrrp_log_level_error,
  1457. "Membership left list contains incorrect address. "
  1458. "This is sign of misconfiguration between nodes!");
  1459. } else {
  1460. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1461. &left_list[i].addr[interface], 0);
  1462. }
  1463. }
  1464. for (i = 0; i < joined_list_entries; i++) {
  1465. if (joined_list->no_addrs < interface + 1 ||
  1466. (joined_list[i].addr[interface].family != AF_INET &&
  1467. joined_list[i].addr[interface].family != AF_INET6)) {
  1468. log_printf(rrp_instance->totemrrp_log_level_error,
  1469. "Membership join 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. &joined_list[i].addr[interface], 1);
  1474. }
  1475. }
  1476. }
  1477. }
  1478. static void active_iface_check (struct totemrrp_instance *instance)
  1479. {
  1480. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1481. unsigned int i;
  1482. for (i = 0; i < instance->interface_count; i++) {
  1483. if (rrp_algo_instance->faulty[i] == 0) {
  1484. totemnet_iface_check (instance->net_handles[i]);
  1485. }
  1486. }
  1487. }
  1488. static void active_processor_count_set (
  1489. struct totemrrp_instance *instance,
  1490. unsigned int processor_count)
  1491. {
  1492. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1493. unsigned int i;
  1494. for (i = 0; i < instance->interface_count; i++) {
  1495. if (rrp_algo_instance->faulty[i] == 0) {
  1496. totemnet_processor_count_set (instance->net_handles[i],
  1497. processor_count);
  1498. }
  1499. }
  1500. }
  1501. static void active_token_target_set (
  1502. struct totemrrp_instance *instance,
  1503. struct totem_ip_address *token_target,
  1504. unsigned int iface_no)
  1505. {
  1506. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1507. }
  1508. static int active_mcast_recv_empty (
  1509. struct totemrrp_instance *instance)
  1510. {
  1511. int res;
  1512. int msgs_emptied = 0;
  1513. int i;
  1514. for (i = 0; i < instance->interface_count; i++) {
  1515. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1516. if (res == -1) {
  1517. return (-1);
  1518. }
  1519. if (res == 1) {
  1520. msgs_emptied = 1;
  1521. }
  1522. }
  1523. return (msgs_emptied);
  1524. }
  1525. static void active_ring_reenable (
  1526. struct totemrrp_instance *instance,
  1527. unsigned int iface_no)
  1528. {
  1529. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1530. int i;
  1531. if (iface_no == instance->interface_count) {
  1532. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1533. instance->interface_count);
  1534. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1535. instance->interface_count);
  1536. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1537. instance->interface_count);
  1538. for (i = 0; i < instance->interface_count; i++) {
  1539. stats_set_interface_faulty (instance, i, 0);
  1540. }
  1541. } else {
  1542. rrp_algo_instance->last_token_recv[iface_no] = 0;
  1543. rrp_algo_instance->faulty[iface_no] = 0;
  1544. rrp_algo_instance->counter_problems[iface_no] = 0;
  1545. stats_set_interface_faulty (instance, iface_no, 0);
  1546. }
  1547. }
  1548. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1549. {
  1550. memset (instance, 0, sizeof (struct totemrrp_instance));
  1551. }
  1552. static int totemrrp_algorithm_set (
  1553. struct totem_config *totem_config,
  1554. struct totemrrp_instance *instance)
  1555. {
  1556. unsigned int res = -1;
  1557. unsigned int i;
  1558. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1559. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1560. instance->rrp_algo = rrp_algos[i];
  1561. if (rrp_algos[i]->initialize) {
  1562. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1563. instance,
  1564. totem_config->interface_count);
  1565. }
  1566. res = 0;
  1567. break;
  1568. }
  1569. }
  1570. for (i = 0; i < totem_config->interface_count; i++) {
  1571. instance->status[i] = malloc (STATUS_STR_LEN+1);
  1572. snprintf (instance->status[i], STATUS_STR_LEN,
  1573. "ring %d active with no faults", i);
  1574. }
  1575. return (res);
  1576. }
  1577. void rrp_deliver_fn (
  1578. void *context,
  1579. const void *msg,
  1580. unsigned int msg_len)
  1581. {
  1582. unsigned int token_seqid;
  1583. unsigned int token_is;
  1584. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1585. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  1586. const struct message_header *hdr = msg;
  1587. struct message_header tmp_msg, activate_msg;
  1588. memset(&tmp_msg, 0, sizeof(struct message_header));
  1589. memset(&activate_msg, 0, sizeof(struct message_header));
  1590. rrp_instance->totemrrp_token_seqid_get (
  1591. msg,
  1592. &token_seqid,
  1593. &token_is);
  1594. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVE) {
  1595. log_printf (
  1596. rrp_instance->totemrrp_log_level_debug,
  1597. "received message requesting test of ring now active");
  1598. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1599. test_active_msg_endian_convert(hdr, &tmp_msg);
  1600. hdr = &tmp_msg;
  1601. }
  1602. if (hdr->nodeid_activator == rrp_instance->my_nodeid) {
  1603. /*
  1604. * Send an activate message
  1605. */
  1606. activate_msg.type = MESSAGE_TYPE_RING_TEST_ACTIVATE;
  1607. activate_msg.endian_detector = ENDIAN_LOCAL;
  1608. activate_msg.ring_number = hdr->ring_number;
  1609. activate_msg.nodeid_activator = rrp_instance->my_nodeid;
  1610. totemnet_token_send (
  1611. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1612. &activate_msg, sizeof (struct message_header));
  1613. } else {
  1614. /*
  1615. * Send a ring test message
  1616. */
  1617. totemnet_token_send (
  1618. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1619. msg, msg_len);
  1620. }
  1621. } else
  1622. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVATE) {
  1623. log_printf (
  1624. rrp_instance->totemrrp_log_level_notice,
  1625. "Automatically recovered ring %d", hdr->ring_number);
  1626. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1627. test_active_msg_endian_convert(hdr, &tmp_msg);
  1628. hdr = &tmp_msg;
  1629. }
  1630. totemrrp_ring_reenable (rrp_instance, deliver_fn_context->iface_no);
  1631. if (hdr->nodeid_activator != rrp_instance->my_nodeid) {
  1632. totemnet_token_send (
  1633. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1634. msg, msg_len);
  1635. }
  1636. } else
  1637. if (token_is) {
  1638. /*
  1639. * Deliver to the token receiver for this rrp algorithm
  1640. */
  1641. rrp_instance->rrp_algo->token_recv (
  1642. rrp_instance,
  1643. deliver_fn_context->iface_no,
  1644. deliver_fn_context->context,
  1645. msg,
  1646. msg_len,
  1647. token_seqid);
  1648. } else {
  1649. /*
  1650. * Deliver to the mcast receiver for this rrp algorithm
  1651. */
  1652. rrp_instance->rrp_algo->mcast_recv (
  1653. rrp_instance,
  1654. deliver_fn_context->iface_no,
  1655. deliver_fn_context->context,
  1656. msg,
  1657. msg_len);
  1658. }
  1659. }
  1660. void rrp_iface_change_fn (
  1661. void *context,
  1662. const struct totem_ip_address *iface_addr)
  1663. {
  1664. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1665. deliver_fn_context->instance->my_nodeid = iface_addr->nodeid;
  1666. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1667. deliver_fn_context->context,
  1668. iface_addr,
  1669. deliver_fn_context->iface_no);
  1670. }
  1671. int totemrrp_finalize (
  1672. void *rrp_context)
  1673. {
  1674. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1675. int i;
  1676. for (i = 0; i < instance->interface_count; i++) {
  1677. totemnet_finalize (instance->net_handles[i]);
  1678. }
  1679. free (instance->net_handles);
  1680. free (instance);
  1681. return (0);
  1682. }
  1683. static void rrp_target_set_completed (void *context)
  1684. {
  1685. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1686. deliver_fn_context->instance->totemrrp_target_set_completed (deliver_fn_context->context);
  1687. }
  1688. /*
  1689. * Totem Redundant Ring interface
  1690. * depends on poll abstraction, POSIX, IPV4
  1691. */
  1692. /*
  1693. * Create an instance
  1694. */
  1695. int totemrrp_initialize (
  1696. qb_loop_t *poll_handle,
  1697. void **rrp_context,
  1698. struct totem_config *totem_config,
  1699. totemsrp_stats_t *stats,
  1700. void *context,
  1701. void (*deliver_fn) (
  1702. void *context,
  1703. const void *msg,
  1704. unsigned int msg_len),
  1705. void (*iface_change_fn) (
  1706. void *context,
  1707. const struct totem_ip_address *iface_addr,
  1708. unsigned int iface_no),
  1709. void (*token_seqid_get) (
  1710. const void *msg,
  1711. unsigned int *seqid,
  1712. unsigned int *token_is),
  1713. unsigned int (*msgs_missing) (void),
  1714. void (*target_set_completed) (void *context))
  1715. {
  1716. struct totemrrp_instance *instance;
  1717. unsigned int res;
  1718. int i;
  1719. instance = malloc (sizeof (struct totemrrp_instance));
  1720. if (instance == 0) {
  1721. return (-1);
  1722. }
  1723. totemrrp_instance_initialize (instance);
  1724. instance->totem_config = totem_config;
  1725. stats->rrp = &instance->stats;
  1726. instance->stats.interface_count = totem_config->interface_count;
  1727. instance->stats.faulty = calloc(instance->stats.interface_count, sizeof(uint8_t));
  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. void *totemrrp_buffer_alloc (void *rrp_context)
  1782. {
  1783. struct totemrrp_instance *instance = rrp_context;
  1784. assert (instance != NULL);
  1785. return totemnet_buffer_alloc (instance->net_handles[0]);
  1786. }
  1787. void totemrrp_buffer_release (void *rrp_context, void *ptr)
  1788. {
  1789. struct totemrrp_instance *instance = rrp_context;
  1790. assert (instance != NULL);
  1791. totemnet_buffer_release (instance->net_handles[0], ptr);
  1792. }
  1793. int totemrrp_processor_count_set (
  1794. void *rrp_context,
  1795. unsigned int processor_count)
  1796. {
  1797. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1798. instance->rrp_algo->processor_count_set (instance, processor_count);
  1799. instance->processor_count = processor_count;
  1800. return (0);
  1801. }
  1802. int totemrrp_token_target_set (
  1803. void *rrp_context,
  1804. struct totem_ip_address *addr,
  1805. unsigned int iface_no)
  1806. {
  1807. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1808. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1809. return (0);
  1810. }
  1811. int totemrrp_recv_flush (void *rrp_context)
  1812. {
  1813. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1814. instance->rrp_algo->recv_flush (instance);
  1815. return (0);
  1816. }
  1817. int totemrrp_send_flush (void *rrp_context)
  1818. {
  1819. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1820. instance->rrp_algo->send_flush (instance);
  1821. return (0);
  1822. }
  1823. int totemrrp_token_send (
  1824. void *rrp_context,
  1825. const void *msg,
  1826. unsigned int msg_len)
  1827. {
  1828. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1829. instance->rrp_algo->token_send (instance, msg, msg_len);
  1830. return (0);
  1831. }
  1832. int totemrrp_mcast_flush_send (
  1833. void *rrp_context,
  1834. const void *msg,
  1835. unsigned int msg_len)
  1836. {
  1837. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1838. int res = 0;
  1839. // TODO this needs to return the result
  1840. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1841. return (res);
  1842. }
  1843. int totemrrp_mcast_noflush_send (
  1844. void *rrp_context,
  1845. const void *msg,
  1846. unsigned int msg_len)
  1847. {
  1848. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1849. /*
  1850. * merge detects go out through mcast_flush_send so it is safe to
  1851. * flush these messages if we are only one processor. This avoids
  1852. * an encryption/hmac and decryption/hmac
  1853. */
  1854. if (instance->processor_count > 1) {
  1855. // TODO this needs to return the result
  1856. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1857. }
  1858. return (0);
  1859. }
  1860. int totemrrp_iface_check (void *rrp_context)
  1861. {
  1862. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1863. instance->rrp_algo->iface_check (instance);
  1864. return (0);
  1865. }
  1866. int totemrrp_ifaces_get (
  1867. void *rrp_context,
  1868. char ***status,
  1869. unsigned int *iface_count)
  1870. {
  1871. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1872. *status = instance->status;
  1873. if (iface_count) {
  1874. *iface_count = instance->interface_count;
  1875. }
  1876. return (0);
  1877. }
  1878. int totemrrp_crypto_set (
  1879. void *rrp_context,
  1880. const char *cipher_type,
  1881. const char *hash_type)
  1882. {
  1883. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1884. int res;
  1885. res = totemnet_crypto_set(instance->net_handles[0], cipher_type, hash_type);
  1886. return (res);
  1887. }
  1888. /*
  1889. * iface_no indicates the interface number [0, ..., interface_count-1] of the
  1890. * specific ring which will be reenabled. We specify iface_no == interface_count
  1891. * means reenabling all the rings.
  1892. */
  1893. int totemrrp_ring_reenable (
  1894. void *rrp_context,
  1895. unsigned int iface_no)
  1896. {
  1897. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1898. int res = 0;
  1899. unsigned int i;
  1900. instance->rrp_algo->ring_reenable (instance, iface_no);
  1901. if (iface_no == instance->interface_count) {
  1902. for (i = 0; i < instance->interface_count; i++) {
  1903. snprintf (instance->status[i], STATUS_STR_LEN,
  1904. "ring %d active with no faults", i);
  1905. }
  1906. } else {
  1907. snprintf (instance->status[iface_no], STATUS_STR_LEN,
  1908. "ring %d active with no faults", iface_no);
  1909. }
  1910. return (res);
  1911. }
  1912. extern int totemrrp_mcast_recv_empty (
  1913. void *rrp_context)
  1914. {
  1915. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1916. int res;
  1917. res = instance->rrp_algo->mcast_recv_empty (instance);
  1918. return (res);
  1919. }
  1920. int totemrrp_member_add (
  1921. void *rrp_context,
  1922. const struct totem_ip_address *member,
  1923. int iface_no)
  1924. {
  1925. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1926. int res;
  1927. res = instance->rrp_algo->member_add (instance, member, iface_no);
  1928. return (res);
  1929. }
  1930. int totemrrp_member_remove (
  1931. void *rrp_context,
  1932. const struct totem_ip_address *member,
  1933. int iface_no)
  1934. {
  1935. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1936. int res;
  1937. res = instance->rrp_algo->member_remove (instance, member, iface_no);
  1938. return (res);
  1939. }
  1940. void totemrrp_membership_changed (
  1941. void *rrp_context,
  1942. enum totem_configuration_type configuration_type,
  1943. const struct srp_addr *member_list, size_t member_list_entries,
  1944. const struct srp_addr *left_list, size_t left_list_entries,
  1945. const struct srp_addr *joined_list, size_t joined_list_entries,
  1946. const struct memb_ring_id *ring_id)
  1947. {
  1948. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1949. instance->rrp_algo->membership_changed (instance,
  1950. configuration_type,
  1951. member_list, member_list_entries,
  1952. left_list, left_list_entries,
  1953. joined_list, joined_list_entries,
  1954. ring_id);
  1955. }