totemrrp.c 45 KB

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