totemrrp.c 43 KB

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