totemrrp.c 43 KB

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