totemrrp.c 43 KB

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