totemrrp.c 43 KB

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