totemrrp.c 47 KB

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