totemrrp.c 52 KB

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