totemrrp.c 43 KB

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