totemrrp.c 43 KB

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