totemrrp.c 50 KB

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