totemrrp.c 44 KB

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