totemrrp.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  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_token_recv (
  273. struct totemrrp_instance *instance,
  274. unsigned int iface_no,
  275. void *context,
  276. const void *msg,
  277. unsigned int msg_len,
  278. unsigned int token_seqid);
  279. static void passive_token_send (
  280. struct totemrrp_instance *instance,
  281. const void *msg,
  282. unsigned int msg_len);
  283. static void passive_send_flush (
  284. struct totemrrp_instance *instance);
  285. static void passive_iface_check (
  286. struct totemrrp_instance *instance);
  287. static void passive_processor_count_set (
  288. struct totemrrp_instance *instance,
  289. unsigned int processor_count_set);
  290. static void passive_token_target_set (
  291. struct totemrrp_instance *instance,
  292. struct totem_ip_address *token_target,
  293. unsigned int iface_no);
  294. static void passive_ring_reenable (
  295. struct totemrrp_instance *instance,
  296. unsigned int iface_no);
  297. static int passive_mcast_recv_empty (
  298. struct totemrrp_instance *instance);
  299. static int passive_member_add (
  300. struct totemrrp_instance *instance,
  301. const struct totem_ip_address *member,
  302. unsigned int iface_no);
  303. static int passive_member_remove (
  304. struct totemrrp_instance *instance,
  305. const struct totem_ip_address *member,
  306. unsigned int iface_no);
  307. /*
  308. * Active Replication Forward Definitions
  309. */
  310. static void *active_instance_initialize (
  311. struct totemrrp_instance *rrp_instance,
  312. int interface_count);
  313. static void active_mcast_recv (
  314. struct totemrrp_instance *instance,
  315. unsigned int iface_no,
  316. void *context,
  317. const void *msg,
  318. unsigned int msg_len);
  319. static void active_mcast_noflush_send (
  320. struct totemrrp_instance *instance,
  321. const void *msg,
  322. unsigned int msg_len);
  323. static void active_mcast_flush_send (
  324. struct totemrrp_instance *instance,
  325. const void *msg,
  326. unsigned int msg_len);
  327. static void active_token_recv (
  328. struct totemrrp_instance *instance,
  329. unsigned int iface_no,
  330. void *context,
  331. const void *msg,
  332. unsigned int msg_len,
  333. unsigned int token_seqid);
  334. static void active_token_send (
  335. struct totemrrp_instance *instance,
  336. const void *msg,
  337. unsigned int msg_len);
  338. static void active_send_flush (
  339. struct totemrrp_instance *instance);
  340. static void active_iface_check (
  341. struct totemrrp_instance *instance);
  342. static void active_processor_count_set (
  343. struct totemrrp_instance *instance,
  344. unsigned int processor_count_set);
  345. static void active_token_target_set (
  346. struct totemrrp_instance *instance,
  347. struct totem_ip_address *token_target,
  348. unsigned int iface_no);
  349. static void active_ring_reenable (
  350. struct totemrrp_instance *instance,
  351. unsigned int iface_no);
  352. static int active_mcast_recv_empty (
  353. struct totemrrp_instance *instance);
  354. static int active_member_add (
  355. struct totemrrp_instance *instance,
  356. const struct totem_ip_address *member,
  357. unsigned int iface_no);
  358. static int active_member_remove (
  359. struct totemrrp_instance *instance,
  360. const struct totem_ip_address *member,
  361. unsigned int iface_no);
  362. static void active_timer_expired_token_start (
  363. struct active_instance *active_instance);
  364. static void active_timer_expired_token_cancel (
  365. struct active_instance *active_instance);
  366. static void active_timer_problem_decrementer_start (
  367. struct active_instance *active_instance);
  368. static void active_timer_problem_decrementer_cancel (
  369. struct active_instance *active_instance);
  370. /*
  371. * 0-5 reserved for totemsrp.c
  372. */
  373. #define MESSAGE_TYPE_RING_TEST_ACTIVE 6
  374. #define MESSAGE_TYPE_RING_TEST_ACTIVATE 7
  375. #define ENDIAN_LOCAL 0xff22
  376. struct message_header {
  377. char type;
  378. char encapsulated;
  379. unsigned short endian_detector;
  380. int ring_number;
  381. int nodeid_activator;
  382. } __attribute__((packed));
  383. struct deliver_fn_context {
  384. struct totemrrp_instance *instance;
  385. void *context;
  386. int iface_no;
  387. };
  388. struct rrp_algo none_algo = {
  389. .name = "none",
  390. .initialize = NULL,
  391. .mcast_recv = none_mcast_recv,
  392. .mcast_noflush_send = none_mcast_noflush_send,
  393. .mcast_flush_send = none_mcast_flush_send,
  394. .token_recv = none_token_recv,
  395. .token_send = none_token_send,
  396. .send_flush = none_send_flush,
  397. .iface_check = none_iface_check,
  398. .processor_count_set = none_processor_count_set,
  399. .token_target_set = none_token_target_set,
  400. .ring_reenable = none_ring_reenable,
  401. .mcast_recv_empty = none_mcast_recv_empty,
  402. .member_add = none_member_add,
  403. .member_remove = none_member_remove
  404. };
  405. struct rrp_algo passive_algo = {
  406. .name = "passive",
  407. .initialize = passive_instance_initialize,
  408. .mcast_recv = passive_mcast_recv,
  409. .mcast_noflush_send = passive_mcast_noflush_send,
  410. .mcast_flush_send = passive_mcast_flush_send,
  411. .token_recv = passive_token_recv,
  412. .token_send = passive_token_send,
  413. .send_flush = passive_send_flush,
  414. .iface_check = passive_iface_check,
  415. .processor_count_set = passive_processor_count_set,
  416. .token_target_set = passive_token_target_set,
  417. .ring_reenable = passive_ring_reenable,
  418. .mcast_recv_empty = passive_mcast_recv_empty,
  419. .member_add = passive_member_add,
  420. .member_remove = passive_member_remove
  421. };
  422. struct rrp_algo active_algo = {
  423. .name = "active",
  424. .initialize = active_instance_initialize,
  425. .mcast_recv = active_mcast_recv,
  426. .mcast_noflush_send = active_mcast_noflush_send,
  427. .mcast_flush_send = active_mcast_flush_send,
  428. .token_recv = active_token_recv,
  429. .token_send = active_token_send,
  430. .send_flush = active_send_flush,
  431. .iface_check = active_iface_check,
  432. .processor_count_set = active_processor_count_set,
  433. .token_target_set = active_token_target_set,
  434. .ring_reenable = active_ring_reenable,
  435. .mcast_recv_empty = active_mcast_recv_empty,
  436. .member_add = active_member_add,
  437. .member_remove = active_member_remove
  438. };
  439. struct rrp_algo *rrp_algos[] = {
  440. &none_algo,
  441. &passive_algo,
  442. &active_algo
  443. };
  444. #define RRP_ALGOS_COUNT 3
  445. #define log_printf(level, format, args...) \
  446. do { \
  447. rrp_instance->totemrrp_log_printf ( \
  448. LOGSYS_ENCODE_RECID(level, \
  449. rrp_instance->totemrrp_subsys_id, \
  450. LOGSYS_RECID_LOG), \
  451. __FUNCTION__, __FILE__, __LINE__, \
  452. format, ##args); \
  453. } while (0);
  454. static void test_active_msg_endian_convert(const struct message_header *in, struct message_header *out)
  455. {
  456. out->type = in->type;
  457. out->encapsulated = in->encapsulated;
  458. out->endian_detector = ENDIAN_LOCAL;
  459. out->ring_number = swab32 (in->ring_number);
  460. out->nodeid_activator = swab32(in->nodeid_activator);
  461. }
  462. static void timer_function_test_ring_timeout (void *context)
  463. {
  464. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  465. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  466. unsigned int *faulty = NULL;
  467. int iface_no = deliver_fn_context->iface_no;
  468. struct message_header msg = {
  469. .type = MESSAGE_TYPE_RING_TEST_ACTIVE,
  470. .endian_detector = ENDIAN_LOCAL,
  471. };
  472. if (strcmp(rrp_instance->totem_config->rrp_mode, "active") == 0)
  473. faulty = ((struct active_instance *)(rrp_instance->rrp_algo_instance))->faulty;
  474. if (strcmp(rrp_instance->totem_config->rrp_mode, "passive") == 0)
  475. faulty = ((struct passive_instance *)(rrp_instance->rrp_algo_instance))->faulty;
  476. assert (faulty != NULL);
  477. if (faulty[iface_no] == 1) {
  478. msg.ring_number = iface_no;
  479. msg.nodeid_activator = rrp_instance->my_nodeid;
  480. totemnet_token_send (
  481. rrp_instance->net_handles[iface_no],
  482. &msg, sizeof (struct message_header));
  483. poll_timer_add (rrp_instance->poll_handle,
  484. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  485. (void *)deliver_fn_context,
  486. timer_function_test_ring_timeout,
  487. &rrp_instance->timer_active_test_ring_timeout[iface_no]);
  488. }
  489. }
  490. /*
  491. * None Replication Implementation
  492. */
  493. static void none_mcast_recv (
  494. struct totemrrp_instance *rrp_instance,
  495. unsigned int iface_no,
  496. void *context,
  497. const void *msg,
  498. unsigned int msg_len)
  499. {
  500. rrp_instance->totemrrp_deliver_fn (
  501. context,
  502. msg,
  503. msg_len);
  504. }
  505. static void none_mcast_flush_send (
  506. struct totemrrp_instance *instance,
  507. const void *msg,
  508. unsigned int msg_len)
  509. {
  510. totemnet_mcast_flush_send (instance->net_handles[0], msg, msg_len);
  511. }
  512. static void none_mcast_noflush_send (
  513. struct totemrrp_instance *instance,
  514. const void *msg,
  515. unsigned int msg_len)
  516. {
  517. totemnet_mcast_noflush_send (instance->net_handles[0], msg, msg_len);
  518. }
  519. static void none_token_recv (
  520. struct totemrrp_instance *rrp_instance,
  521. unsigned int iface_no,
  522. void *context,
  523. const void *msg,
  524. unsigned int msg_len,
  525. unsigned int token_seq)
  526. {
  527. rrp_instance->totemrrp_deliver_fn (
  528. context,
  529. msg,
  530. msg_len);
  531. }
  532. static void none_token_send (
  533. struct totemrrp_instance *instance,
  534. const void *msg,
  535. unsigned int msg_len)
  536. {
  537. totemnet_token_send (
  538. instance->net_handles[0],
  539. msg, msg_len);
  540. }
  541. static void none_send_flush (struct totemrrp_instance *instance)
  542. {
  543. totemnet_send_flush (instance->net_handles[0]);
  544. }
  545. static void none_iface_check (struct totemrrp_instance *instance)
  546. {
  547. totemnet_iface_check (instance->net_handles[0]);
  548. }
  549. static void none_processor_count_set (
  550. struct totemrrp_instance *instance,
  551. unsigned int processor_count)
  552. {
  553. totemnet_processor_count_set (instance->net_handles[0],
  554. processor_count);
  555. }
  556. static void none_token_target_set (
  557. struct totemrrp_instance *instance,
  558. struct totem_ip_address *token_target,
  559. unsigned int iface_no)
  560. {
  561. totemnet_token_target_set (instance->net_handles[0], token_target);
  562. }
  563. static void none_ring_reenable (
  564. struct totemrrp_instance *instance,
  565. unsigned int iface_no)
  566. {
  567. /*
  568. * No operation
  569. */
  570. }
  571. static int none_mcast_recv_empty (
  572. struct totemrrp_instance *instance)
  573. {
  574. int res;
  575. res = totemnet_recv_mcast_empty (instance->net_handles[0]);
  576. return (res);
  577. }
  578. static int none_member_add (
  579. struct totemrrp_instance *instance,
  580. const struct totem_ip_address *member,
  581. unsigned int iface_no)
  582. {
  583. int res;
  584. res = totemnet_member_add (instance->net_handles[0], member);
  585. return (res);
  586. }
  587. static int none_member_remove (
  588. struct totemrrp_instance *instance,
  589. const struct totem_ip_address *member,
  590. unsigned int iface_no)
  591. {
  592. int res;
  593. res = totemnet_member_remove (instance->net_handles[0], member);
  594. return (res);
  595. }
  596. /*
  597. * Passive Replication Implementation
  598. */
  599. void *passive_instance_initialize (
  600. struct totemrrp_instance *rrp_instance,
  601. int interface_count)
  602. {
  603. struct passive_instance *instance;
  604. instance = malloc (sizeof (struct passive_instance));
  605. if (instance == 0) {
  606. goto error_exit;
  607. }
  608. memset (instance, 0, sizeof (struct passive_instance));
  609. instance->faulty = malloc (sizeof (int) * interface_count);
  610. if (instance->faulty == 0) {
  611. free (instance);
  612. instance = 0;
  613. goto error_exit;
  614. }
  615. memset (instance->faulty, 0, sizeof (int) * interface_count);
  616. instance->token_recv_count = malloc (sizeof (int) * interface_count);
  617. if (instance->token_recv_count == 0) {
  618. free (instance->faulty);
  619. free (instance);
  620. instance = 0;
  621. goto error_exit;
  622. }
  623. memset (instance->token_recv_count, 0, sizeof (int) * interface_count);
  624. instance->mcast_recv_count = malloc (sizeof (int) * interface_count);
  625. if (instance->mcast_recv_count == 0) {
  626. free (instance->token_recv_count);
  627. free (instance->faulty);
  628. free (instance);
  629. instance = 0;
  630. goto error_exit;
  631. }
  632. memset (instance->mcast_recv_count, 0, sizeof (int) * interface_count);
  633. error_exit:
  634. return ((void *)instance);
  635. }
  636. static void timer_function_passive_token_expired (void *context)
  637. {
  638. struct passive_instance *passive_instance = (struct passive_instance *)context;
  639. struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  640. rrp_instance->totemrrp_deliver_fn (
  641. passive_instance->totemrrp_context,
  642. passive_instance->token,
  643. passive_instance->token_len);
  644. }
  645. /* TODO
  646. static void timer_function_passive_problem_decrementer (void *context)
  647. {
  648. // struct passive_instance *passive_instance = (struct passive_instance *)context;
  649. // struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  650. }
  651. */
  652. static void passive_timer_expired_token_start (
  653. struct passive_instance *passive_instance)
  654. {
  655. poll_timer_add (
  656. passive_instance->rrp_instance->poll_handle,
  657. passive_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  658. (void *)passive_instance,
  659. timer_function_passive_token_expired,
  660. &passive_instance->timer_expired_token);
  661. }
  662. static void passive_timer_expired_token_cancel (
  663. struct passive_instance *passive_instance)
  664. {
  665. poll_timer_delete (
  666. passive_instance->rrp_instance->poll_handle,
  667. passive_instance->timer_expired_token);
  668. }
  669. /*
  670. static void passive_timer_problem_decrementer_start (
  671. struct passive_instance *passive_instance)
  672. {
  673. poll_timer_add (
  674. passive_instance->rrp_instance->poll_handle,
  675. passive_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  676. (void *)passive_instance,
  677. timer_function_passive_problem_decrementer,
  678. &passive_instance->timer_problem_decrementer);
  679. }
  680. static void passive_timer_problem_decrementer_cancel (
  681. struct passive_instance *passive_instance)
  682. {
  683. poll_timer_delete (
  684. passive_instance->rrp_instance->poll_handle,
  685. passive_instance->timer_problem_decrementer);
  686. }
  687. */
  688. static void passive_mcast_recv (
  689. struct totemrrp_instance *rrp_instance,
  690. unsigned int iface_no,
  691. void *context,
  692. const void *msg,
  693. unsigned int msg_len)
  694. {
  695. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  696. unsigned int max;
  697. unsigned int i;
  698. rrp_instance->totemrrp_deliver_fn (
  699. context,
  700. msg,
  701. msg_len);
  702. if (rrp_instance->totemrrp_msgs_missing() == 0 &&
  703. passive_instance->timer_expired_token) {
  704. /*
  705. * Delivers the last token
  706. */
  707. rrp_instance->totemrrp_deliver_fn (
  708. passive_instance->totemrrp_context,
  709. passive_instance->token,
  710. passive_instance->token_len);
  711. passive_timer_expired_token_cancel (passive_instance);
  712. }
  713. /*
  714. * Monitor for failures
  715. * TODO doesn't handle wrap-around of the mcast recv count
  716. */
  717. passive_instance->mcast_recv_count[iface_no] += 1;
  718. max = 0;
  719. for (i = 0; i < rrp_instance->interface_count; i++) {
  720. if (max < passive_instance->mcast_recv_count[i]) {
  721. max = passive_instance->mcast_recv_count[i];
  722. }
  723. }
  724. for (i = 0; i < rrp_instance->interface_count; i++) {
  725. if ((passive_instance->faulty[i] == 0) &&
  726. (max - passive_instance->mcast_recv_count[i] >
  727. rrp_instance->totem_config->rrp_problem_count_threshold)) {
  728. passive_instance->faulty[i] = 1;
  729. poll_timer_add (rrp_instance->poll_handle,
  730. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  731. rrp_instance->deliver_fn_context[i],
  732. timer_function_test_ring_timeout,
  733. &rrp_instance->timer_active_test_ring_timeout[i]);
  734. sprintf (rrp_instance->status[i],
  735. "Marking ringid %u interface %s FAULTY",
  736. i,
  737. totemnet_iface_print (rrp_instance->net_handles[i]));
  738. log_printf (
  739. rrp_instance->totemrrp_log_level_error,
  740. "%s",
  741. rrp_instance->status[i]);
  742. }
  743. }
  744. }
  745. static void passive_mcast_flush_send (
  746. struct totemrrp_instance *instance,
  747. const void *msg,
  748. unsigned int msg_len)
  749. {
  750. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  751. do {
  752. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  753. } while (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1);
  754. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  755. }
  756. static void passive_mcast_noflush_send (
  757. struct totemrrp_instance *instance,
  758. const void *msg,
  759. unsigned int msg_len)
  760. {
  761. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  762. do {
  763. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  764. } while (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1);
  765. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  766. }
  767. static void passive_token_recv (
  768. struct totemrrp_instance *rrp_instance,
  769. unsigned int iface_no,
  770. void *context,
  771. const void *msg,
  772. unsigned int msg_len,
  773. unsigned int token_seq)
  774. {
  775. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  776. unsigned int max;
  777. unsigned int i;
  778. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  779. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  780. rrp_instance->totemrrp_deliver_fn (
  781. context,
  782. msg,
  783. msg_len);
  784. } else {
  785. memcpy (passive_instance->token, msg, msg_len);
  786. passive_timer_expired_token_start (passive_instance);
  787. }
  788. /*
  789. * Monitor for failures
  790. * TODO doesn't handle wrap-around of the token
  791. */
  792. passive_instance->token_recv_count[iface_no] += 1;
  793. max = 0;
  794. for (i = 0; i < rrp_instance->interface_count; i++) {
  795. if (max < passive_instance->token_recv_count[i]) {
  796. max = passive_instance->token_recv_count[i];
  797. }
  798. }
  799. for (i = 0; i < rrp_instance->interface_count; i++) {
  800. if ((passive_instance->faulty[i] == 0) &&
  801. (max - passive_instance->token_recv_count[i] >
  802. rrp_instance->totem_config->rrp_problem_count_threshold)) {
  803. passive_instance->faulty[i] = 1;
  804. poll_timer_add (rrp_instance->poll_handle,
  805. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  806. rrp_instance->deliver_fn_context[i],
  807. timer_function_test_ring_timeout,
  808. &rrp_instance->timer_active_test_ring_timeout[i]);
  809. sprintf (rrp_instance->status[i],
  810. "Marking seqid %d ringid %u interface %s FAULTY",
  811. token_seq,
  812. i,
  813. totemnet_iface_print (rrp_instance->net_handles[i]));
  814. log_printf (
  815. rrp_instance->totemrrp_log_level_error,
  816. "%s",
  817. rrp_instance->status[i]);
  818. }
  819. }
  820. }
  821. static void passive_token_send (
  822. struct totemrrp_instance *instance,
  823. const void *msg,
  824. unsigned int msg_len)
  825. {
  826. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  827. do {
  828. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  829. } while (passive_instance->faulty[passive_instance->token_xmit_iface] == 1);
  830. totemnet_token_send (
  831. instance->net_handles[passive_instance->token_xmit_iface],
  832. msg, msg_len);
  833. }
  834. static void passive_send_flush (struct totemrrp_instance *instance)
  835. {
  836. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  837. unsigned int i;
  838. for (i = 0; i < instance->interface_count; i++) {
  839. if (rrp_algo_instance->faulty[i] == 0) {
  840. totemnet_send_flush (instance->net_handles[i]);
  841. }
  842. }
  843. }
  844. static void passive_iface_check (struct totemrrp_instance *instance)
  845. {
  846. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  847. unsigned int i;
  848. for (i = 0; i < instance->interface_count; i++) {
  849. if (rrp_algo_instance->faulty[i] == 0) {
  850. totemnet_iface_check (instance->net_handles[i]);
  851. }
  852. }
  853. }
  854. static void passive_processor_count_set (
  855. struct totemrrp_instance *instance,
  856. unsigned int processor_count)
  857. {
  858. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  859. unsigned int i;
  860. for (i = 0; i < instance->interface_count; i++) {
  861. if (rrp_algo_instance->faulty[i] == 0) {
  862. totemnet_processor_count_set (instance->net_handles[i],
  863. processor_count);
  864. }
  865. }
  866. }
  867. static void passive_token_target_set (
  868. struct totemrrp_instance *instance,
  869. struct totem_ip_address *token_target,
  870. unsigned int iface_no)
  871. {
  872. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  873. }
  874. static int passive_mcast_recv_empty (
  875. struct totemrrp_instance *instance)
  876. {
  877. int res;
  878. int msgs_emptied = 0;
  879. int i;
  880. for (i = 0; i < instance->interface_count; i++) {
  881. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  882. if (res == -1) {
  883. return (-1);
  884. }
  885. if (res == 1) {
  886. msgs_emptied = 1;
  887. }
  888. }
  889. return (msgs_emptied);
  890. }
  891. static int passive_member_add (
  892. struct totemrrp_instance *instance,
  893. const struct totem_ip_address *member,
  894. unsigned int iface_no)
  895. {
  896. int res;
  897. res = totemnet_member_add (instance->net_handles[iface_no], member);
  898. return (res);
  899. }
  900. static int passive_member_remove (
  901. struct totemrrp_instance *instance,
  902. const struct totem_ip_address *member,
  903. unsigned int iface_no)
  904. {
  905. int res;
  906. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  907. return (res);
  908. }
  909. static void passive_ring_reenable (
  910. struct totemrrp_instance *instance,
  911. unsigned int iface_no)
  912. {
  913. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  914. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  915. instance->interface_count);
  916. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  917. instance->interface_count);
  918. if (iface_no == instance->interface_count) {
  919. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  920. instance->interface_count);
  921. } else {
  922. rrp_algo_instance->faulty[iface_no] = 0;
  923. }
  924. }
  925. /*
  926. * Active Replication Implementation
  927. */
  928. void *active_instance_initialize (
  929. struct totemrrp_instance *rrp_instance,
  930. int interface_count)
  931. {
  932. struct active_instance *instance;
  933. instance = malloc (sizeof (struct active_instance));
  934. if (instance == 0) {
  935. goto error_exit;
  936. }
  937. memset (instance, 0, sizeof (struct active_instance));
  938. instance->faulty = malloc (sizeof (int) * interface_count);
  939. if (instance->faulty == 0) {
  940. free (instance);
  941. instance = 0;
  942. goto error_exit;
  943. }
  944. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  945. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  946. if (instance->last_token_recv == 0) {
  947. free (instance->faulty);
  948. free (instance);
  949. instance = 0;
  950. goto error_exit;
  951. }
  952. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  953. instance->counter_problems = malloc (sizeof (int) * interface_count);
  954. if (instance->counter_problems == 0) {
  955. free (instance->last_token_recv);
  956. free (instance->faulty);
  957. free (instance);
  958. instance = 0;
  959. goto error_exit;
  960. }
  961. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  962. instance->timer_expired_token = 0;
  963. instance->timer_problem_decrementer = 0;
  964. instance->rrp_instance = rrp_instance;
  965. error_exit:
  966. return ((void *)instance);
  967. }
  968. static void timer_function_active_problem_decrementer (void *context)
  969. {
  970. struct active_instance *active_instance = (struct active_instance *)context;
  971. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  972. unsigned int problem_found = 0;
  973. unsigned int i;
  974. for (i = 0; i < rrp_instance->interface_count; i++) {
  975. if (active_instance->counter_problems[i] > 0) {
  976. problem_found = 1;
  977. active_instance->counter_problems[i] -= 1;
  978. if (active_instance->counter_problems[i] == 0) {
  979. sprintf (rrp_instance->status[i],
  980. "ring %d active with no faults", i);
  981. } else {
  982. sprintf (rrp_instance->status[i],
  983. "Decrementing problem counter for iface %s to [%d of %d]",
  984. totemnet_iface_print (rrp_instance->net_handles[i]),
  985. active_instance->counter_problems[i],
  986. rrp_instance->totem_config->rrp_problem_count_threshold);
  987. }
  988. log_printf (
  989. rrp_instance->totemrrp_log_level_warning,
  990. "%s",
  991. rrp_instance->status[i]);
  992. }
  993. }
  994. if (problem_found) {
  995. active_timer_problem_decrementer_start (active_instance);
  996. } else {
  997. active_instance->timer_problem_decrementer = 0;
  998. }
  999. }
  1000. static void timer_function_active_token_expired (void *context)
  1001. {
  1002. struct active_instance *active_instance = (struct active_instance *)context;
  1003. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1004. unsigned int i;
  1005. for (i = 0; i < rrp_instance->interface_count; i++) {
  1006. if (active_instance->last_token_recv[i] == 0) {
  1007. active_instance->counter_problems[i] += 1;
  1008. if (active_instance->timer_problem_decrementer == 0) {
  1009. active_timer_problem_decrementer_start (active_instance);
  1010. }
  1011. sprintf (rrp_instance->status[i],
  1012. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  1013. active_instance->last_token_seq,
  1014. totemnet_iface_print (rrp_instance->net_handles[i]),
  1015. active_instance->counter_problems[i],
  1016. rrp_instance->totem_config->rrp_problem_count_threshold);
  1017. log_printf (
  1018. rrp_instance->totemrrp_log_level_warning,
  1019. "%s",
  1020. rrp_instance->status[i]);
  1021. }
  1022. }
  1023. for (i = 0; i < rrp_instance->interface_count; i++) {
  1024. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold)
  1025. {
  1026. active_instance->faulty[i] = 1;
  1027. poll_timer_add (rrp_instance->poll_handle,
  1028. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  1029. rrp_instance->deliver_fn_context[i],
  1030. timer_function_test_ring_timeout,
  1031. &rrp_instance->timer_active_test_ring_timeout[i]);
  1032. sprintf (rrp_instance->status[i],
  1033. "Marking seqid %d ringid %u interface %s FAULTY",
  1034. active_instance->last_token_seq,
  1035. i,
  1036. totemnet_iface_print (rrp_instance->net_handles[i]));
  1037. log_printf (
  1038. rrp_instance->totemrrp_log_level_error,
  1039. "%s",
  1040. rrp_instance->status[i]);
  1041. active_timer_problem_decrementer_cancel (active_instance);
  1042. }
  1043. }
  1044. rrp_instance->totemrrp_deliver_fn (
  1045. active_instance->totemrrp_context,
  1046. active_instance->token,
  1047. active_instance->token_len);
  1048. }
  1049. static void active_timer_expired_token_start (
  1050. struct active_instance *active_instance)
  1051. {
  1052. poll_timer_add (
  1053. active_instance->rrp_instance->poll_handle,
  1054. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  1055. (void *)active_instance,
  1056. timer_function_active_token_expired,
  1057. &active_instance->timer_expired_token);
  1058. }
  1059. static void active_timer_expired_token_cancel (
  1060. struct active_instance *active_instance)
  1061. {
  1062. poll_timer_delete (
  1063. active_instance->rrp_instance->poll_handle,
  1064. active_instance->timer_expired_token);
  1065. }
  1066. static void active_timer_problem_decrementer_start (
  1067. struct active_instance *active_instance)
  1068. {
  1069. poll_timer_add (
  1070. active_instance->rrp_instance->poll_handle,
  1071. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  1072. (void *)active_instance,
  1073. timer_function_active_problem_decrementer,
  1074. &active_instance->timer_problem_decrementer);
  1075. }
  1076. static void active_timer_problem_decrementer_cancel (
  1077. struct active_instance *active_instance)
  1078. {
  1079. poll_timer_delete (
  1080. active_instance->rrp_instance->poll_handle,
  1081. active_instance->timer_problem_decrementer);
  1082. }
  1083. /*
  1084. * active replication
  1085. */
  1086. static void active_mcast_recv (
  1087. struct totemrrp_instance *instance,
  1088. unsigned int iface_no,
  1089. void *context,
  1090. const void *msg,
  1091. unsigned int msg_len)
  1092. {
  1093. instance->totemrrp_deliver_fn (
  1094. context,
  1095. msg,
  1096. msg_len);
  1097. }
  1098. static void active_mcast_flush_send (
  1099. struct totemrrp_instance *instance,
  1100. const void *msg,
  1101. unsigned int msg_len)
  1102. {
  1103. int i;
  1104. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1105. for (i = 0; i < instance->interface_count; i++) {
  1106. if (rrp_algo_instance->faulty[i] == 0) {
  1107. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  1108. }
  1109. }
  1110. }
  1111. static void active_mcast_noflush_send (
  1112. struct totemrrp_instance *instance,
  1113. const void *msg,
  1114. unsigned int msg_len)
  1115. {
  1116. int i;
  1117. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1118. for (i = 0; i < instance->interface_count; i++) {
  1119. if (rrp_algo_instance->faulty[i] == 0) {
  1120. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  1121. }
  1122. }
  1123. }
  1124. static void active_token_recv (
  1125. struct totemrrp_instance *rrp_instance,
  1126. unsigned int iface_no,
  1127. void *context,
  1128. const void *msg,
  1129. unsigned int msg_len,
  1130. unsigned int token_seq)
  1131. {
  1132. int i;
  1133. struct active_instance *active_instance = (struct active_instance *)rrp_instance->rrp_algo_instance;
  1134. active_instance->totemrrp_context = context;
  1135. if (token_seq > active_instance->last_token_seq) {
  1136. memcpy (active_instance->token, msg, msg_len);
  1137. active_instance->token_len = msg_len;
  1138. for (i = 0; i < rrp_instance->interface_count; i++) {
  1139. active_instance->last_token_recv[i] = 0;
  1140. }
  1141. active_instance->last_token_recv[iface_no] = 1;
  1142. active_timer_expired_token_start (active_instance);
  1143. }
  1144. active_instance->last_token_seq = token_seq;
  1145. if (token_seq == active_instance->last_token_seq) {
  1146. active_instance->last_token_recv[iface_no] = 1;
  1147. for (i = 0; i < rrp_instance->interface_count; i++) {
  1148. if ((active_instance->last_token_recv[i] == 0) &&
  1149. active_instance->faulty[i] == 0) {
  1150. return; /* don't deliver token */
  1151. }
  1152. }
  1153. active_timer_expired_token_cancel (active_instance);
  1154. rrp_instance->totemrrp_deliver_fn (
  1155. context,
  1156. msg,
  1157. msg_len);
  1158. }
  1159. }
  1160. static void active_token_send (
  1161. struct totemrrp_instance *instance,
  1162. const void *msg,
  1163. unsigned int msg_len)
  1164. {
  1165. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1166. int i;
  1167. for (i = 0; i < instance->interface_count; i++) {
  1168. if (rrp_algo_instance->faulty[i] == 0) {
  1169. totemnet_token_send (
  1170. instance->net_handles[i],
  1171. msg, msg_len);
  1172. }
  1173. }
  1174. }
  1175. static void active_send_flush (struct totemrrp_instance *instance)
  1176. {
  1177. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1178. unsigned int i;
  1179. for (i = 0; i < instance->interface_count; i++) {
  1180. if (rrp_algo_instance->faulty[i] == 0) {
  1181. totemnet_send_flush (instance->net_handles[i]);
  1182. }
  1183. }
  1184. }
  1185. static int active_member_add (
  1186. struct totemrrp_instance *instance,
  1187. const struct totem_ip_address *member,
  1188. unsigned int iface_no)
  1189. {
  1190. int res;
  1191. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1192. return (res);
  1193. }
  1194. static int active_member_remove (
  1195. struct totemrrp_instance *instance,
  1196. const struct totem_ip_address *member,
  1197. unsigned int iface_no)
  1198. {
  1199. int res;
  1200. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1201. return (res);
  1202. }
  1203. static void active_iface_check (struct totemrrp_instance *instance)
  1204. {
  1205. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1206. unsigned int i;
  1207. for (i = 0; i < instance->interface_count; i++) {
  1208. if (rrp_algo_instance->faulty[i] == 0) {
  1209. totemnet_iface_check (instance->net_handles[i]);
  1210. }
  1211. }
  1212. }
  1213. static void active_processor_count_set (
  1214. struct totemrrp_instance *instance,
  1215. unsigned int processor_count)
  1216. {
  1217. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1218. unsigned int i;
  1219. for (i = 0; i < instance->interface_count; i++) {
  1220. if (rrp_algo_instance->faulty[i] == 0) {
  1221. totemnet_processor_count_set (instance->net_handles[i],
  1222. processor_count);
  1223. }
  1224. }
  1225. }
  1226. static void active_token_target_set (
  1227. struct totemrrp_instance *instance,
  1228. struct totem_ip_address *token_target,
  1229. unsigned int iface_no)
  1230. {
  1231. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1232. }
  1233. static int active_mcast_recv_empty (
  1234. struct totemrrp_instance *instance)
  1235. {
  1236. int res;
  1237. int msgs_emptied = 0;
  1238. int i;
  1239. for (i = 0; i < instance->interface_count; i++) {
  1240. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1241. if (res == -1) {
  1242. return (-1);
  1243. }
  1244. if (res == 1) {
  1245. msgs_emptied = 1;
  1246. }
  1247. }
  1248. return (msgs_emptied);
  1249. }
  1250. static void active_ring_reenable (
  1251. struct totemrrp_instance *instance,
  1252. unsigned int iface_no)
  1253. {
  1254. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1255. if (iface_no == instance->interface_count) {
  1256. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1257. instance->interface_count);
  1258. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1259. instance->interface_count);
  1260. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1261. instance->interface_count);
  1262. } else {
  1263. rrp_algo_instance->last_token_recv[iface_no] = 0;
  1264. rrp_algo_instance->faulty[iface_no] = 0;
  1265. rrp_algo_instance->counter_problems[iface_no] = 0;
  1266. }
  1267. }
  1268. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1269. {
  1270. memset (instance, 0, sizeof (struct totemrrp_instance));
  1271. }
  1272. static int totemrrp_algorithm_set (
  1273. struct totem_config *totem_config,
  1274. struct totemrrp_instance *instance)
  1275. {
  1276. unsigned int res = -1;
  1277. unsigned int i;
  1278. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1279. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1280. instance->rrp_algo = rrp_algos[i];
  1281. if (rrp_algos[i]->initialize) {
  1282. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1283. instance,
  1284. totem_config->interface_count);
  1285. }
  1286. res = 0;
  1287. break;
  1288. }
  1289. }
  1290. for (i = 0; i < totem_config->interface_count; i++) {
  1291. instance->status[i] = malloc (1024);
  1292. sprintf (instance->status[i], "ring %d active with no faults", i);
  1293. }
  1294. return (res);
  1295. }
  1296. void rrp_deliver_fn (
  1297. void *context,
  1298. const void *msg,
  1299. unsigned int msg_len)
  1300. {
  1301. unsigned int token_seqid;
  1302. unsigned int token_is;
  1303. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1304. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  1305. const struct message_header *hdr = msg;
  1306. struct message_header tmp_msg, activate_msg;
  1307. memset(&tmp_msg, 0, sizeof(struct message_header));
  1308. memset(&activate_msg, 0, sizeof(struct message_header));
  1309. rrp_instance->totemrrp_token_seqid_get (
  1310. msg,
  1311. &token_seqid,
  1312. &token_is);
  1313. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVE) {
  1314. log_printf (
  1315. rrp_instance->totemrrp_log_level_debug,
  1316. "received message requesting test of ring now active\n");
  1317. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1318. test_active_msg_endian_convert(hdr, &tmp_msg);
  1319. hdr = &tmp_msg;
  1320. }
  1321. if (hdr->nodeid_activator == rrp_instance->my_nodeid) {
  1322. /*
  1323. * Send an activate message
  1324. */
  1325. activate_msg.type = MESSAGE_TYPE_RING_TEST_ACTIVATE;
  1326. activate_msg.endian_detector = ENDIAN_LOCAL;
  1327. activate_msg.ring_number = hdr->ring_number;
  1328. activate_msg.nodeid_activator = rrp_instance->my_nodeid;
  1329. totemnet_token_send (
  1330. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1331. &activate_msg, sizeof (struct message_header));
  1332. } else {
  1333. /*
  1334. * Send a ring test message
  1335. */
  1336. totemnet_token_send (
  1337. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1338. msg, msg_len);
  1339. }
  1340. } else
  1341. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVATE) {
  1342. log_printf (
  1343. rrp_instance->totemrrp_log_level_notice,
  1344. "Automatically recovered ring %d\n", hdr->ring_number);
  1345. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1346. test_active_msg_endian_convert(hdr, &tmp_msg);
  1347. hdr = &tmp_msg;
  1348. }
  1349. totemrrp_ring_reenable (rrp_instance, deliver_fn_context->iface_no);
  1350. if (hdr->nodeid_activator != rrp_instance->my_nodeid) {
  1351. totemnet_token_send (
  1352. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1353. msg, msg_len);
  1354. }
  1355. } else
  1356. if (token_is) {
  1357. /*
  1358. * Deliver to the token receiver for this rrp algorithm
  1359. */
  1360. rrp_instance->rrp_algo->token_recv (
  1361. rrp_instance,
  1362. deliver_fn_context->iface_no,
  1363. deliver_fn_context->context,
  1364. msg,
  1365. msg_len,
  1366. token_seqid);
  1367. } else {
  1368. /*
  1369. * Deliver to the mcast receiver for this rrp algorithm
  1370. */
  1371. rrp_instance->rrp_algo->mcast_recv (
  1372. rrp_instance,
  1373. deliver_fn_context->iface_no,
  1374. deliver_fn_context->context,
  1375. msg,
  1376. msg_len);
  1377. }
  1378. }
  1379. void rrp_iface_change_fn (
  1380. void *context,
  1381. const struct totem_ip_address *iface_addr)
  1382. {
  1383. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1384. deliver_fn_context->instance->my_nodeid = iface_addr->nodeid;
  1385. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1386. deliver_fn_context->context,
  1387. iface_addr,
  1388. deliver_fn_context->iface_no);
  1389. }
  1390. int totemrrp_finalize (
  1391. void *rrp_context)
  1392. {
  1393. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1394. int i;
  1395. for (i = 0; i < instance->interface_count; i++) {
  1396. totemnet_finalize (instance->net_handles[i]);
  1397. }
  1398. return (0);
  1399. }
  1400. static void rrp_target_set_completed (void *context)
  1401. {
  1402. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1403. deliver_fn_context->instance->totemrrp_target_set_completed (deliver_fn_context->context);
  1404. }
  1405. /*
  1406. * Totem Redundant Ring interface
  1407. * depends on poll abstraction, POSIX, IPV4
  1408. */
  1409. /*
  1410. * Create an instance
  1411. */
  1412. int totemrrp_initialize (
  1413. hdb_handle_t poll_handle,
  1414. void **rrp_context,
  1415. struct totem_config *totem_config,
  1416. void *context,
  1417. void (*deliver_fn) (
  1418. void *context,
  1419. const void *msg,
  1420. unsigned int msg_len),
  1421. void (*iface_change_fn) (
  1422. void *context,
  1423. const struct totem_ip_address *iface_addr,
  1424. unsigned int iface_no),
  1425. void (*token_seqid_get) (
  1426. const void *msg,
  1427. unsigned int *seqid,
  1428. unsigned int *token_is),
  1429. unsigned int (*msgs_missing) (void),
  1430. void (*target_set_completed) (void *context))
  1431. {
  1432. struct totemrrp_instance *instance;
  1433. unsigned int res;
  1434. int i;
  1435. instance = malloc (sizeof (struct totemrrp_instance));
  1436. if (instance == 0) {
  1437. return (-1);
  1438. }
  1439. totemrrp_instance_initialize (instance);
  1440. instance->totem_config = totem_config;
  1441. res = totemrrp_algorithm_set (
  1442. instance->totem_config,
  1443. instance);
  1444. if (res == -1) {
  1445. goto error_destroy;
  1446. }
  1447. /*
  1448. * Configure logging
  1449. */
  1450. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1451. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1452. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1453. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1454. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1455. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1456. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1457. instance->interfaces = totem_config->interfaces;
  1458. instance->poll_handle = poll_handle;
  1459. instance->totemrrp_deliver_fn = deliver_fn;
  1460. instance->totemrrp_iface_change_fn = iface_change_fn;
  1461. instance->totemrrp_token_seqid_get = token_seqid_get;
  1462. instance->totemrrp_target_set_completed = target_set_completed;
  1463. instance->totemrrp_msgs_missing = msgs_missing;
  1464. instance->interface_count = totem_config->interface_count;
  1465. instance->net_handles = malloc (sizeof (void *) * totem_config->interface_count);
  1466. instance->context = context;
  1467. instance->poll_handle = poll_handle;
  1468. for (i = 0; i < totem_config->interface_count; i++) {
  1469. struct deliver_fn_context *deliver_fn_context;
  1470. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1471. assert (deliver_fn_context);
  1472. deliver_fn_context->instance = instance;
  1473. deliver_fn_context->context = context;
  1474. deliver_fn_context->iface_no = i;
  1475. instance->deliver_fn_context[i] = (void *)deliver_fn_context;
  1476. totemnet_initialize (
  1477. poll_handle,
  1478. &instance->net_handles[i],
  1479. totem_config,
  1480. i,
  1481. (void *)deliver_fn_context,
  1482. rrp_deliver_fn,
  1483. rrp_iface_change_fn,
  1484. rrp_target_set_completed);
  1485. totemnet_net_mtu_adjust (instance->net_handles[i], totem_config);
  1486. }
  1487. *rrp_context = instance;
  1488. return (0);
  1489. error_destroy:
  1490. free (instance);
  1491. return (res);
  1492. }
  1493. void *totemrrp_buffer_alloc (void *rrp_context)
  1494. {
  1495. struct totemrrp_instance *instance = rrp_context;
  1496. assert (instance != NULL);
  1497. return totemnet_buffer_alloc (instance->net_handles[0]);
  1498. }
  1499. void totemrrp_buffer_release (void *rrp_context, void *ptr)
  1500. {
  1501. struct totemrrp_instance *instance = rrp_context;
  1502. assert (instance != NULL);
  1503. totemnet_buffer_release (instance->net_handles[0], ptr);
  1504. }
  1505. int totemrrp_processor_count_set (
  1506. void *rrp_context,
  1507. unsigned int processor_count)
  1508. {
  1509. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1510. instance->rrp_algo->processor_count_set (instance, processor_count);
  1511. instance->processor_count = processor_count;
  1512. return (0);
  1513. }
  1514. int totemrrp_token_target_set (
  1515. void *rrp_context,
  1516. struct totem_ip_address *addr,
  1517. unsigned int iface_no)
  1518. {
  1519. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1520. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1521. return (0);
  1522. }
  1523. int totemrrp_send_flush (void *rrp_context)
  1524. {
  1525. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1526. instance->rrp_algo->send_flush (instance);
  1527. return (0);
  1528. }
  1529. int totemrrp_token_send (
  1530. void *rrp_context,
  1531. const void *msg,
  1532. unsigned int msg_len)
  1533. {
  1534. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1535. instance->rrp_algo->token_send (instance, msg, msg_len);
  1536. return (0);
  1537. }
  1538. int totemrrp_mcast_flush_send (
  1539. void *rrp_context,
  1540. const void *msg,
  1541. unsigned int msg_len)
  1542. {
  1543. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1544. int res = 0;
  1545. // TODO this needs to return the result
  1546. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1547. return (res);
  1548. }
  1549. int totemrrp_mcast_noflush_send (
  1550. void *rrp_context,
  1551. const void *msg,
  1552. unsigned int msg_len)
  1553. {
  1554. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1555. /*
  1556. * merge detects go out through mcast_flush_send so it is safe to
  1557. * flush these messages if we are only one processor. This avoids
  1558. * an encryption/hmac and decryption/hmac
  1559. */
  1560. if (instance->processor_count > 1) {
  1561. // TODO this needs to return the result
  1562. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1563. }
  1564. return (0);
  1565. }
  1566. int totemrrp_iface_check (void *rrp_context)
  1567. {
  1568. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1569. instance->rrp_algo->iface_check (instance);
  1570. return (0);
  1571. }
  1572. int totemrrp_ifaces_get (
  1573. void *rrp_context,
  1574. char ***status,
  1575. unsigned int *iface_count)
  1576. {
  1577. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1578. *status = instance->status;
  1579. if (iface_count) {
  1580. *iface_count = instance->interface_count;
  1581. }
  1582. return (0);
  1583. }
  1584. int totemrrp_crypto_set (
  1585. void *rrp_context,
  1586. unsigned int type)
  1587. {
  1588. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1589. int res;
  1590. res = totemnet_crypto_set(instance->net_handles[0], type);
  1591. return (res);
  1592. }
  1593. /*
  1594. * iface_no indicates the interface number [0, ..., interface_count-1] of the
  1595. * specific ring which will be reenabled. We specify iface_no == interface_count
  1596. * means reenabling all the rings.
  1597. */
  1598. int totemrrp_ring_reenable (
  1599. void *rrp_context,
  1600. unsigned int iface_no)
  1601. {
  1602. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1603. int res = 0;
  1604. unsigned int i;
  1605. instance->rrp_algo->ring_reenable (instance, iface_no);
  1606. if (iface_no == instance->interface_count) {
  1607. for (i = 0; i < instance->interface_count; i++) {
  1608. sprintf (instance->status[i], "ring %d active with no faults", i);
  1609. }
  1610. } else {
  1611. sprintf (instance->status[iface_no], "ring %d active with no faults", iface_no);
  1612. }
  1613. return (res);
  1614. }
  1615. extern int totemrrp_mcast_recv_empty (
  1616. void *rrp_context)
  1617. {
  1618. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1619. int res;
  1620. res = instance->rrp_algo->mcast_recv_empty (instance);
  1621. return (res);
  1622. }
  1623. int totemrrp_member_add (
  1624. void *rrp_context,
  1625. const struct totem_ip_address *member,
  1626. int iface_no)
  1627. {
  1628. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1629. int res;
  1630. res = instance->rrp_algo->member_add (instance, member, iface_no);
  1631. return (res);
  1632. }
  1633. int totemrrp_member_remove (
  1634. void *rrp_context,
  1635. const struct totem_ip_address *member,
  1636. int iface_no)
  1637. {
  1638. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1639. int res;
  1640. res = instance->rrp_algo->member_remove (instance, member, iface_no);
  1641. return (res);
  1642. }