totemrrp.c 45 KB

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