totemrrp.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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. char *name;
  93. void * (*initialize) (
  94. struct totemrrp_instance *rrp_instance,
  95. int interface_count);
  96. void (*mcast_recv) (
  97. struct totemrrp_instance *instance,
  98. unsigned int iface_no,
  99. void *context,
  100. void *msg,
  101. unsigned int msg_len);
  102. void (*mcast_noflush_send) (
  103. struct totemrrp_instance *instance,
  104. struct iovec *iovec,
  105. unsigned int iov_len);
  106. void (*mcast_flush_send) (
  107. struct totemrrp_instance *instance,
  108. struct iovec *iovec,
  109. unsigned int iov_len);
  110. void (*token_recv) (
  111. struct totemrrp_instance *instance,
  112. unsigned int iface_no,
  113. void *context,
  114. void *msg,
  115. unsigned int msg_len,
  116. unsigned int token_seqid);
  117. void (*token_send) (
  118. struct totemrrp_instance *instance,
  119. struct iovec *iovec,
  120. unsigned int iov_len);
  121. void (*recv_flush) (
  122. struct totemrrp_instance *instance);
  123. void (*send_flush) (
  124. struct totemrrp_instance *instance);
  125. void (*iface_check) (
  126. struct totemrrp_instance *instance);
  127. void (*processor_count_set) (
  128. struct totemrrp_instance *instance,
  129. unsigned int processor_count);
  130. void (*token_target_set) (
  131. struct totemrrp_instance *instance,
  132. struct totem_ip_address *token_target,
  133. unsigned int iface_no);
  134. void (*ring_reenable) (
  135. struct totemrrp_instance *instance);
  136. };
  137. struct totemrrp_instance {
  138. poll_handle totemrrp_poll_handle;
  139. struct totem_interface *interfaces;
  140. struct rrp_algo *rrp_algo;
  141. void *context;
  142. char *status[INTERFACE_MAX];
  143. void (*totemrrp_deliver_fn) (
  144. void *context,
  145. void *msg,
  146. int msg_len);
  147. void (*totemrrp_iface_change_fn) (
  148. void *context,
  149. struct totem_ip_address *iface_addr,
  150. unsigned int iface_no);
  151. void (*totemrrp_token_seqid_get) (
  152. void *msg,
  153. unsigned int *seqid,
  154. unsigned int *token_is);
  155. unsigned int (*totemrrp_msgs_missing) (void);
  156. /*
  157. * Function and data used to log messages
  158. */
  159. int totemrrp_log_level_security;
  160. int totemrrp_log_level_error;
  161. int totemrrp_log_level_warning;
  162. int totemrrp_log_level_notice;
  163. int totemrrp_log_level_debug;
  164. int totemrrp_subsys_id;
  165. void (*totemrrp_log_printf) (int subsys, char *function, char *file,
  166. int line, unsigned int level, char *format,
  167. ...)__attribute__((format(printf, 6, 7)));
  168. totemrrp_handle handle;
  169. totemnet_handle *net_handles;
  170. void *rrp_algo_instance;
  171. int interface_count;
  172. int 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. (char *)__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. rrp_instance->status[i]);
  610. }
  611. }
  612. }
  613. static void passive_mcast_flush_send (
  614. struct totemrrp_instance *instance,
  615. struct iovec *iovec,
  616. unsigned int iov_len)
  617. {
  618. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  619. do {
  620. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  621. } while (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1);
  622. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], iovec, iov_len);
  623. }
  624. static void passive_mcast_noflush_send (
  625. struct totemrrp_instance *instance,
  626. struct iovec *iovec,
  627. unsigned int iov_len)
  628. {
  629. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  630. do {
  631. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  632. } while (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1);
  633. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], iovec, iov_len);
  634. }
  635. static void passive_token_recv (
  636. struct totemrrp_instance *rrp_instance,
  637. unsigned int iface_no,
  638. void *context,
  639. void *msg,
  640. unsigned int msg_len,
  641. unsigned int token_seq)
  642. {
  643. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  644. unsigned int max;
  645. unsigned int i;
  646. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  647. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  648. rrp_instance->totemrrp_deliver_fn (
  649. context,
  650. msg,
  651. msg_len);
  652. } else {
  653. memcpy (passive_instance->token, msg, msg_len);
  654. passive_timer_expired_token_start (passive_instance);
  655. }
  656. /*
  657. * Monitor for failures
  658. * TODO doesn't handle wrap-around of the token
  659. */
  660. passive_instance->token_recv_count[iface_no] += 1;
  661. max = 0;
  662. for (i = 0; i < rrp_instance->interface_count; i++) {
  663. if (max < passive_instance->token_recv_count[i]) {
  664. max = passive_instance->token_recv_count[i];
  665. }
  666. }
  667. for (i = 0; i < rrp_instance->interface_count; i++) {
  668. if ((passive_instance->faulty[i] == 0) &&
  669. (max - passive_instance->token_recv_count[i] >
  670. rrp_instance->totem_config->rrp_problem_count_threshold)) {
  671. passive_instance->faulty[i] = 1;
  672. sprintf (rrp_instance->status[i],
  673. "Marking seqid %d ringid %u interface %s FAULTY - adminisrtative intervention required.",
  674. token_seq,
  675. i,
  676. totemnet_iface_print (rrp_instance->net_handles[i]));
  677. log_printf (
  678. rrp_instance->totemrrp_log_level_error,
  679. rrp_instance->status[i]);
  680. }
  681. }
  682. }
  683. static void passive_token_send (
  684. struct totemrrp_instance *instance,
  685. struct iovec *iovec,
  686. unsigned int iov_len)
  687. {
  688. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  689. do {
  690. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  691. } while (passive_instance->faulty[passive_instance->token_xmit_iface] == 1);
  692. totemnet_token_send (
  693. instance->net_handles[passive_instance->token_xmit_iface],
  694. iovec, iov_len);
  695. }
  696. static void passive_recv_flush (struct totemrrp_instance *instance)
  697. {
  698. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  699. unsigned int i;
  700. for (i = 0; i < instance->interface_count; i++) {
  701. if (rrp_algo_instance->faulty[i] == 0) {
  702. totemnet_recv_flush (instance->net_handles[i]);
  703. }
  704. }
  705. }
  706. static void passive_send_flush (struct totemrrp_instance *instance)
  707. {
  708. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  709. unsigned int i;
  710. for (i = 0; i < instance->interface_count; i++) {
  711. if (rrp_algo_instance->faulty[i] == 0) {
  712. totemnet_send_flush (instance->net_handles[i]);
  713. }
  714. }
  715. }
  716. static void passive_iface_check (struct totemrrp_instance *instance)
  717. {
  718. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  719. unsigned int i;
  720. for (i = 0; i < instance->interface_count; i++) {
  721. if (rrp_algo_instance->faulty[i] == 0) {
  722. totemnet_iface_check (instance->net_handles[i]);
  723. }
  724. }
  725. }
  726. static void passive_processor_count_set (
  727. struct totemrrp_instance *instance,
  728. unsigned int processor_count)
  729. {
  730. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  731. unsigned int i;
  732. for (i = 0; i < instance->interface_count; i++) {
  733. if (rrp_algo_instance->faulty[i] == 0) {
  734. totemnet_processor_count_set (instance->net_handles[i],
  735. processor_count);
  736. }
  737. }
  738. }
  739. static void passive_token_target_set (
  740. struct totemrrp_instance *instance,
  741. struct totem_ip_address *token_target,
  742. unsigned int iface_no)
  743. {
  744. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  745. }
  746. static void passive_ring_reenable (
  747. struct totemrrp_instance *instance)
  748. {
  749. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  750. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  751. instance->interface_count);
  752. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  753. instance->interface_count);
  754. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  755. instance->interface_count);
  756. }
  757. /*
  758. * Active Replication Implementation
  759. */
  760. void *active_instance_initialize (
  761. struct totemrrp_instance *rrp_instance,
  762. int interface_count)
  763. {
  764. struct active_instance *instance;
  765. instance = malloc (sizeof (struct active_instance));
  766. if (instance == 0) {
  767. goto error_exit;
  768. }
  769. memset (instance, 0, sizeof (struct active_instance));
  770. instance->faulty = malloc (sizeof (int) * interface_count);
  771. if (instance->faulty == 0) {
  772. free (instance);
  773. instance = 0;
  774. goto error_exit;
  775. }
  776. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  777. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  778. if (instance->last_token_recv == 0) {
  779. free (instance->faulty);
  780. free (instance);
  781. instance = 0;
  782. goto error_exit;
  783. }
  784. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  785. instance->counter_problems = malloc (sizeof (int) * interface_count);
  786. if (instance->counter_problems == 0) {
  787. free (instance->last_token_recv);
  788. free (instance->faulty);
  789. free (instance);
  790. instance = 0;
  791. goto error_exit;
  792. }
  793. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  794. instance->timer_expired_token = 0;
  795. instance->timer_problem_decrementer = 0;
  796. instance->rrp_instance = rrp_instance;
  797. error_exit:
  798. return ((void *)instance);
  799. }
  800. static void timer_function_active_problem_decrementer (void *context)
  801. {
  802. struct active_instance *active_instance = (struct active_instance *)context;
  803. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  804. unsigned int problem_found = 0;
  805. unsigned int i;
  806. for (i = 0; i < rrp_instance->interface_count; i++) {
  807. if (active_instance->counter_problems[i] > 0) {
  808. problem_found = 1;
  809. active_instance->counter_problems[i] -= 1;
  810. if (active_instance->counter_problems[i] == 0) {
  811. sprintf (rrp_instance->status[i],
  812. "ring %d active with no faults", i);
  813. } else {
  814. sprintf (rrp_instance->status[i],
  815. "Decrementing problem counter for iface %s to [%d of %d]",
  816. totemnet_iface_print (rrp_instance->net_handles[i]),
  817. active_instance->counter_problems[i],
  818. rrp_instance->totem_config->rrp_problem_count_threshold);
  819. }
  820. log_printf (
  821. rrp_instance->totemrrp_log_level_warning,
  822. rrp_instance->status[i]);
  823. }
  824. }
  825. if (problem_found) {
  826. active_timer_problem_decrementer_start (active_instance);
  827. } else {
  828. active_instance->timer_problem_decrementer = 0;
  829. }
  830. }
  831. static void timer_function_active_token_expired (void *context)
  832. {
  833. struct active_instance *active_instance = (struct active_instance *)context;
  834. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  835. unsigned int i;
  836. for (i = 0; i < rrp_instance->interface_count; i++) {
  837. if (active_instance->last_token_recv[i] == 0) {
  838. active_instance->counter_problems[i] += 1;
  839. if (active_instance->timer_problem_decrementer == 0) {
  840. active_timer_problem_decrementer_start (active_instance);
  841. }
  842. sprintf (rrp_instance->status[i],
  843. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  844. active_instance->last_token_seq,
  845. totemnet_iface_print (rrp_instance->net_handles[i]),
  846. active_instance->counter_problems[i],
  847. rrp_instance->totem_config->rrp_problem_count_threshold);
  848. log_printf (
  849. rrp_instance->totemrrp_log_level_warning,
  850. rrp_instance->status[i]);
  851. }
  852. }
  853. for (i = 0; i < rrp_instance->interface_count; i++) {
  854. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold)
  855. {
  856. active_instance->faulty[i] = 1;
  857. sprintf (rrp_instance->status[i],
  858. "Marking seqid %d ringid %u interface %s FAULTY - adminisrtative intervention required.",
  859. active_instance->last_token_seq,
  860. i,
  861. totemnet_iface_print (rrp_instance->net_handles[i]));
  862. log_printf (
  863. rrp_instance->totemrrp_log_level_error,
  864. rrp_instance->status[i]);
  865. active_timer_problem_decrementer_cancel (active_instance);
  866. }
  867. }
  868. rrp_instance->totemrrp_deliver_fn (
  869. active_instance->totemrrp_context,
  870. active_instance->token,
  871. active_instance->token_len);
  872. }
  873. static void active_timer_expired_token_start (
  874. struct active_instance *active_instance)
  875. {
  876. poll_timer_add (
  877. active_instance->rrp_instance->poll_handle,
  878. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  879. (void *)active_instance,
  880. timer_function_active_token_expired,
  881. &active_instance->timer_expired_token);
  882. }
  883. static void active_timer_expired_token_cancel (
  884. struct active_instance *active_instance)
  885. {
  886. poll_timer_delete (
  887. active_instance->rrp_instance->poll_handle,
  888. active_instance->timer_expired_token);
  889. }
  890. static void active_timer_problem_decrementer_start (
  891. struct active_instance *active_instance)
  892. {
  893. poll_timer_add (
  894. active_instance->rrp_instance->poll_handle,
  895. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  896. (void *)active_instance,
  897. timer_function_active_problem_decrementer,
  898. &active_instance->timer_problem_decrementer);
  899. }
  900. static void active_timer_problem_decrementer_cancel (
  901. struct active_instance *active_instance)
  902. {
  903. poll_timer_delete (
  904. active_instance->rrp_instance->poll_handle,
  905. active_instance->timer_problem_decrementer);
  906. }
  907. /*
  908. * active replication
  909. */
  910. static void active_mcast_recv (
  911. struct totemrrp_instance *instance,
  912. unsigned int iface_no,
  913. void *context,
  914. void *msg,
  915. unsigned int msg_len)
  916. {
  917. instance->totemrrp_deliver_fn (
  918. context,
  919. msg,
  920. msg_len);
  921. }
  922. static void active_mcast_flush_send (
  923. struct totemrrp_instance *instance,
  924. struct iovec *iovec,
  925. unsigned int iov_len)
  926. {
  927. int i;
  928. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  929. for (i = 0; i < instance->interface_count; i++) {
  930. if (rrp_algo_instance->faulty[i] == 0) {
  931. totemnet_mcast_flush_send (instance->net_handles[i], iovec, iov_len);
  932. }
  933. }
  934. }
  935. static void active_mcast_noflush_send (
  936. struct totemrrp_instance *instance,
  937. struct iovec *iovec,
  938. unsigned int iov_len)
  939. {
  940. int i;
  941. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  942. for (i = 0; i < instance->interface_count; i++) {
  943. if (rrp_algo_instance->faulty[i] == 0) {
  944. totemnet_mcast_noflush_send (instance->net_handles[i], iovec, iov_len);
  945. }
  946. }
  947. }
  948. static void active_token_recv (
  949. struct totemrrp_instance *instance,
  950. unsigned int iface_no,
  951. void *context,
  952. void *msg,
  953. unsigned int msg_len,
  954. unsigned int token_seq)
  955. {
  956. int i;
  957. struct active_instance *active_instance = (struct active_instance *)instance->rrp_algo_instance;
  958. active_instance->totemrrp_context = context; // this should be in totemrrp_instance ?
  959. if (token_seq > active_instance->last_token_seq) {
  960. memcpy (active_instance->token, msg, msg_len);
  961. active_instance->token_len = msg_len;
  962. for (i = 0; i < instance->interface_count; i++) {
  963. active_instance->last_token_recv[i] = 0;
  964. }
  965. active_instance->last_token_recv[iface_no] = 1;
  966. active_timer_expired_token_start (active_instance);
  967. }
  968. active_instance->last_token_seq = token_seq;
  969. if (token_seq == active_instance->last_token_seq) {
  970. active_instance->last_token_recv[iface_no] = 1;
  971. for (i = 0; i < instance->interface_count; i++) {
  972. if ((active_instance->last_token_recv[i] == 0) &&
  973. active_instance->faulty[i] == 0) {
  974. return; /* don't deliver token */
  975. }
  976. }
  977. active_timer_expired_token_cancel (active_instance);
  978. instance->totemrrp_deliver_fn (
  979. context,
  980. msg,
  981. msg_len);
  982. }
  983. }
  984. static void active_token_send (
  985. struct totemrrp_instance *instance,
  986. struct iovec *iovec,
  987. unsigned int iov_len)
  988. {
  989. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  990. int i;
  991. for (i = 0; i < instance->interface_count; i++) {
  992. if (rrp_algo_instance->faulty[i] == 0) {
  993. totemnet_token_send (
  994. instance->net_handles[i],
  995. iovec, iov_len);
  996. }
  997. }
  998. }
  999. static void active_recv_flush (struct totemrrp_instance *instance)
  1000. {
  1001. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1002. unsigned int i;
  1003. for (i = 0; i < instance->interface_count; i++) {
  1004. if (rrp_algo_instance->faulty[i] == 0) {
  1005. totemnet_recv_flush (instance->net_handles[i]);
  1006. }
  1007. }
  1008. }
  1009. static void active_send_flush (struct totemrrp_instance *instance)
  1010. {
  1011. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1012. unsigned int i;
  1013. for (i = 0; i < instance->interface_count; i++) {
  1014. if (rrp_algo_instance->faulty[i] == 0) {
  1015. totemnet_send_flush (instance->net_handles[i]);
  1016. }
  1017. }
  1018. }
  1019. static void active_iface_check (struct totemrrp_instance *instance)
  1020. {
  1021. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1022. unsigned int i;
  1023. for (i = 0; i < instance->interface_count; i++) {
  1024. if (rrp_algo_instance->faulty[i] == 0) {
  1025. totemnet_iface_check (instance->net_handles[i]);
  1026. }
  1027. }
  1028. }
  1029. static void active_processor_count_set (
  1030. struct totemrrp_instance *instance,
  1031. unsigned int processor_count)
  1032. {
  1033. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1034. unsigned int i;
  1035. for (i = 0; i < instance->interface_count; i++) {
  1036. if (rrp_algo_instance->faulty[i] == 0) {
  1037. totemnet_processor_count_set (instance->net_handles[i],
  1038. processor_count);
  1039. }
  1040. }
  1041. }
  1042. static void active_token_target_set (
  1043. struct totemrrp_instance *instance,
  1044. struct totem_ip_address *token_target,
  1045. unsigned int iface_no)
  1046. {
  1047. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1048. }
  1049. static void active_ring_reenable (
  1050. struct totemrrp_instance *instance)
  1051. {
  1052. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1053. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1054. instance->interface_count);
  1055. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1056. instance->interface_count);
  1057. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1058. instance->interface_count);
  1059. }
  1060. struct deliver_fn_context {
  1061. struct totemrrp_instance *instance;
  1062. void *context;
  1063. int iface_no;
  1064. };
  1065. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1066. {
  1067. memset (instance, 0, sizeof (struct totemrrp_instance));
  1068. }
  1069. static int totemrrp_algorithm_set (
  1070. struct totem_config *totem_config,
  1071. struct totemrrp_instance *instance)
  1072. {
  1073. unsigned int res = -1;
  1074. unsigned int i;
  1075. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1076. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1077. instance->rrp_algo = rrp_algos[i];
  1078. if (rrp_algos[i]->initialize) {
  1079. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1080. instance,
  1081. totem_config->interface_count);
  1082. }
  1083. res = 0;
  1084. break;
  1085. }
  1086. }
  1087. for (i = 0; i < totem_config->interface_count; i++) {
  1088. instance->status[i] = malloc (1024);
  1089. sprintf (instance->status[i], "ring %d active with no faults", i);
  1090. }
  1091. return (res);
  1092. }
  1093. void rrp_deliver_fn (
  1094. void *context,
  1095. void *msg,
  1096. int msg_len)
  1097. {
  1098. unsigned int token_seqid;
  1099. unsigned int token_is;
  1100. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1101. deliver_fn_context->instance->totemrrp_token_seqid_get (
  1102. msg,
  1103. &token_seqid,
  1104. &token_is);
  1105. if (token_is) {
  1106. /*
  1107. * Deliver to the token receiver for this rrp algorithm
  1108. */
  1109. deliver_fn_context->instance->rrp_algo->token_recv (
  1110. deliver_fn_context->instance,
  1111. deliver_fn_context->iface_no,
  1112. deliver_fn_context->context,
  1113. msg,
  1114. msg_len,
  1115. token_seqid);
  1116. } else {
  1117. /*
  1118. * Deliver to the mcast receiver for this rrp algorithm
  1119. */
  1120. deliver_fn_context->instance->rrp_algo->mcast_recv (
  1121. deliver_fn_context->instance,
  1122. deliver_fn_context->iface_no,
  1123. deliver_fn_context->context,
  1124. msg,
  1125. msg_len);
  1126. }
  1127. }
  1128. void rrp_iface_change_fn (
  1129. void *context,
  1130. struct totem_ip_address *iface_addr)
  1131. {
  1132. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1133. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1134. deliver_fn_context->context,
  1135. iface_addr,
  1136. deliver_fn_context->iface_no);
  1137. }
  1138. int totemrrp_finalize (
  1139. totemrrp_handle handle)
  1140. {
  1141. struct totemrrp_instance *instance;
  1142. int res = 0;
  1143. int i;
  1144. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1145. (void *)&instance);
  1146. if (res != 0) {
  1147. res = ENOENT;
  1148. goto error_exit;
  1149. }
  1150. for (i = 0; i < instance->interface_count; i++) {
  1151. totemnet_finalize (instance->net_handles[i]);
  1152. }
  1153. hdb_handle_put (&totemrrp_instance_database, handle);
  1154. error_exit:
  1155. return (res);
  1156. }
  1157. /*
  1158. * Totem Redundant Ring interface
  1159. * depends on poll abstraction, POSIX, IPV4
  1160. */
  1161. /*
  1162. * Create an instance
  1163. */
  1164. int totemrrp_initialize (
  1165. poll_handle poll_handle,
  1166. totemrrp_handle *handle,
  1167. struct totem_config *totem_config,
  1168. void *context,
  1169. void (*deliver_fn) (
  1170. void *context,
  1171. void *msg,
  1172. int msg_len),
  1173. void (*iface_change_fn) (
  1174. void *context,
  1175. struct totem_ip_address *iface_addr,
  1176. unsigned int iface_no),
  1177. void (*token_seqid_get) (
  1178. void *msg,
  1179. unsigned int *seqid,
  1180. unsigned int *token_is),
  1181. unsigned int (*msgs_missing) (void))
  1182. {
  1183. struct totemrrp_instance *instance;
  1184. unsigned int res;
  1185. int i;
  1186. res = hdb_handle_create (&totemrrp_instance_database,
  1187. sizeof (struct totemrrp_instance), handle);
  1188. if (res != 0) {
  1189. goto error_exit;
  1190. }
  1191. res = hdb_handle_get (&totemrrp_instance_database, *handle,
  1192. (void *)&instance);
  1193. if (res != 0) {
  1194. goto error_destroy;
  1195. }
  1196. totemrrp_instance_initialize (instance);
  1197. instance->totem_config = totem_config;
  1198. res = totemrrp_algorithm_set (
  1199. instance->totem_config,
  1200. instance);
  1201. if (res == -1) {
  1202. goto error_put;
  1203. }
  1204. /*
  1205. * Configure logging
  1206. */
  1207. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1208. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1209. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1210. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1211. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1212. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1213. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1214. instance->interfaces = totem_config->interfaces;
  1215. instance->totemrrp_poll_handle = poll_handle;
  1216. instance->totemrrp_deliver_fn = deliver_fn;
  1217. instance->totemrrp_iface_change_fn = iface_change_fn;
  1218. instance->totemrrp_token_seqid_get = token_seqid_get;
  1219. instance->totemrrp_msgs_missing = msgs_missing;
  1220. instance->interface_count = totem_config->interface_count;
  1221. instance->net_handles = malloc (sizeof (totemnet_handle) * totem_config->interface_count);
  1222. instance->context = context;
  1223. instance->poll_handle = poll_handle;
  1224. for (i = 0; i < totem_config->interface_count; i++) {
  1225. struct deliver_fn_context *deliver_fn_context;
  1226. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1227. assert (deliver_fn_context);
  1228. deliver_fn_context->instance = instance;
  1229. deliver_fn_context->context = context;
  1230. deliver_fn_context->iface_no = i;
  1231. totemnet_initialize (
  1232. poll_handle,
  1233. &instance->net_handles[i],
  1234. totem_config,
  1235. i,
  1236. (void *)deliver_fn_context,
  1237. rrp_deliver_fn,
  1238. rrp_iface_change_fn);
  1239. }
  1240. totemnet_net_mtu_adjust (totem_config);
  1241. error_exit:
  1242. hdb_handle_put (&totemrrp_instance_database, *handle);
  1243. return (0);
  1244. error_put:
  1245. hdb_handle_put (&totemrrp_instance_database, *handle);
  1246. error_destroy:
  1247. hdb_handle_destroy (&totemrrp_instance_database, *handle);
  1248. return (res);
  1249. }
  1250. int totemrrp_processor_count_set (
  1251. totemrrp_handle handle,
  1252. unsigned int processor_count)
  1253. {
  1254. struct totemrrp_instance *instance;
  1255. int res = 0;
  1256. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1257. (void *)&instance);
  1258. if (res != 0) {
  1259. res = ENOENT;
  1260. goto error_exit;
  1261. }
  1262. instance->rrp_algo->processor_count_set (instance, processor_count);
  1263. instance->processor_count = processor_count;
  1264. hdb_handle_put (&totemrrp_instance_database, handle);
  1265. error_exit:
  1266. return (res);
  1267. }
  1268. int totemrrp_token_target_set (
  1269. totemrrp_handle handle,
  1270. struct totem_ip_address *addr,
  1271. unsigned int iface_no)
  1272. {
  1273. struct totemrrp_instance *instance;
  1274. int res = 0;
  1275. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1276. (void *)&instance);
  1277. if (res != 0) {
  1278. res = ENOENT;
  1279. goto error_exit;
  1280. }
  1281. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1282. hdb_handle_put (&totemrrp_instance_database, handle);
  1283. error_exit:
  1284. return (res);
  1285. }
  1286. int totemrrp_recv_flush (totemrrp_handle handle)
  1287. {
  1288. struct totemrrp_instance *instance;
  1289. int res = 0;
  1290. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1291. (void *)&instance);
  1292. if (res != 0) {
  1293. res = ENOENT;
  1294. goto error_exit;
  1295. }
  1296. instance->rrp_algo->recv_flush (instance);
  1297. hdb_handle_put (&totemrrp_instance_database, handle);
  1298. error_exit:
  1299. return (res);
  1300. }
  1301. int totemrrp_send_flush (totemrrp_handle handle)
  1302. {
  1303. struct totemrrp_instance *instance;
  1304. int res = 0;
  1305. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1306. (void *)&instance);
  1307. if (res != 0) {
  1308. res = ENOENT;
  1309. goto error_exit;
  1310. }
  1311. instance->rrp_algo->send_flush (instance);
  1312. hdb_handle_put (&totemrrp_instance_database, handle);
  1313. error_exit:
  1314. return (res);
  1315. }
  1316. int totemrrp_token_send (
  1317. totemrrp_handle handle,
  1318. struct iovec *iovec,
  1319. unsigned int iov_len)
  1320. {
  1321. struct totemrrp_instance *instance;
  1322. int res = 0;
  1323. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1324. (void *)&instance);
  1325. if (res != 0) {
  1326. res = ENOENT;
  1327. goto error_exit;
  1328. }
  1329. instance->rrp_algo->token_send (instance, iovec, iov_len);
  1330. hdb_handle_put (&totemrrp_instance_database, handle);
  1331. error_exit:
  1332. return (res);
  1333. }
  1334. int totemrrp_mcast_flush_send (
  1335. totemrrp_handle handle,
  1336. struct iovec *iovec,
  1337. unsigned int iov_len)
  1338. {
  1339. struct totemrrp_instance *instance;
  1340. int res = 0;
  1341. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1342. (void *)&instance);
  1343. if (res != 0) {
  1344. res = ENOENT;
  1345. goto error_exit;
  1346. }
  1347. // TODO this needs to return the result
  1348. instance->rrp_algo->mcast_flush_send (instance, iovec, iov_len);
  1349. hdb_handle_put (&totemrrp_instance_database, handle);
  1350. error_exit:
  1351. return (res);
  1352. }
  1353. int totemrrp_mcast_noflush_send (
  1354. totemrrp_handle handle,
  1355. struct iovec *iovec,
  1356. unsigned int iov_len)
  1357. {
  1358. struct totemrrp_instance *instance;
  1359. int res = 0;
  1360. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1361. (void *)&instance);
  1362. if (res != 0) {
  1363. res = ENOENT;
  1364. goto error_exit;
  1365. }
  1366. /*
  1367. * merge detects go out through mcast_flush_send so it is safe to
  1368. * flush these messages if we are only one processor. This avoids
  1369. * an encryption/hmac and decryption/hmac
  1370. */
  1371. if (instance->processor_count > 1) {
  1372. // TODO this needs to return the result
  1373. instance->rrp_algo->mcast_noflush_send (instance, iovec, iov_len);
  1374. }
  1375. hdb_handle_put (&totemrrp_instance_database, handle);
  1376. error_exit:
  1377. return (res);
  1378. }
  1379. int totemrrp_iface_check (totemrrp_handle handle)
  1380. {
  1381. struct totemrrp_instance *instance;
  1382. int res = 0;
  1383. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1384. (void *)&instance);
  1385. if (res != 0) {
  1386. res = ENOENT;
  1387. goto error_exit;
  1388. }
  1389. instance->rrp_algo->iface_check (instance);
  1390. hdb_handle_put (&totemrrp_instance_database, handle);
  1391. error_exit:
  1392. return (res);
  1393. }
  1394. int totemrrp_ifaces_get (
  1395. totemrrp_handle handle,
  1396. char ***status,
  1397. unsigned int *iface_count)
  1398. {
  1399. struct totemrrp_instance *instance;
  1400. int res = 0;
  1401. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1402. (void *)&instance);
  1403. if (res != 0) {
  1404. res = ENOENT;
  1405. goto error_exit;
  1406. }
  1407. *status = instance->status;
  1408. if (iface_count) {
  1409. *iface_count = instance->interface_count;
  1410. }
  1411. hdb_handle_put (&totemrrp_instance_database, handle);
  1412. error_exit:
  1413. return (res);
  1414. }
  1415. int totemrrp_ring_reenable (
  1416. totemrrp_handle handle)
  1417. {
  1418. struct totemrrp_instance *instance;
  1419. int res = 0;
  1420. unsigned int i;
  1421. res = hdb_handle_get (&totemrrp_instance_database, handle,
  1422. (void *)&instance);
  1423. if (res != 0) {
  1424. res = ENOENT;
  1425. goto error_exit;
  1426. }
  1427. instance->rrp_algo->ring_reenable (instance);
  1428. for (i = 0; i < instance->interface_count; i++) {
  1429. sprintf (instance->status[i], "ring %d active with no faults", i);
  1430. }
  1431. hdb_handle_put (&totemrrp_instance_database, handle);
  1432. error_exit:
  1433. return (res);
  1434. }