totemrrp.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 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/swab.h>
  61. #include <qb/qbdefs.h>
  62. #include <qb/qbloop.h>
  63. #define LOGSYS_UTILS_ONLY 1
  64. #include <corosync/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. qb_loop_timer_handle timer_expired_token;
  83. qb_loop_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. qb_loop_timer_handle timer_expired_token;
  97. qb_loop_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. unsigned int iface_no);
  146. int (*mcast_recv_empty) (
  147. struct totemrrp_instance *instance);
  148. int (*member_add) (
  149. struct totemrrp_instance *instance,
  150. const struct totem_ip_address *member,
  151. unsigned int iface_no);
  152. int (*member_remove) (
  153. struct totemrrp_instance *instance,
  154. const struct totem_ip_address *member,
  155. unsigned int iface_no);
  156. void (*membership_changed) (
  157. struct totemrrp_instance *instance,
  158. enum totem_configuration_type configuration_type,
  159. const struct srp_addr *member_list, size_t member_list_entries,
  160. const struct srp_addr *left_list, size_t left_list_entries,
  161. const struct srp_addr *joined_list, size_t joined_list_entries,
  162. const struct memb_ring_id *ring_id);
  163. };
  164. #define STATUS_STR_LEN 512
  165. struct totemrrp_instance {
  166. qb_loop_t *poll_handle;
  167. struct totem_interface *interfaces;
  168. struct rrp_algo *rrp_algo;
  169. void *context;
  170. char *status[INTERFACE_MAX];
  171. void (*totemrrp_deliver_fn) (
  172. void *context,
  173. const void *msg,
  174. unsigned int msg_len);
  175. void (*totemrrp_iface_change_fn) (
  176. void *context,
  177. const struct totem_ip_address *iface_addr,
  178. unsigned int iface_no);
  179. void (*totemrrp_token_seqid_get) (
  180. const void *msg,
  181. unsigned int *seqid,
  182. unsigned int *token_is);
  183. void (*totemrrp_target_set_completed) (
  184. void *context);
  185. unsigned int (*totemrrp_msgs_missing) (void);
  186. /*
  187. * Function and data used to log messages
  188. */
  189. int totemrrp_log_level_security;
  190. int totemrrp_log_level_error;
  191. int totemrrp_log_level_warning;
  192. int totemrrp_log_level_notice;
  193. int totemrrp_log_level_debug;
  194. int totemrrp_subsys_id;
  195. void (*totemrrp_log_printf) (
  196. int level,
  197. int subsys,
  198. const char *function,
  199. const char *file,
  200. int line,
  201. const char *format, ...)__attribute__((format(printf, 6, 7)));
  202. void **net_handles;
  203. void *rrp_algo_instance;
  204. int interface_count;
  205. int processor_count;
  206. int my_nodeid;
  207. struct totem_config *totem_config;
  208. void *deliver_fn_context[INTERFACE_MAX];
  209. qb_loop_timer_handle timer_active_test_ring_timeout[INTERFACE_MAX];
  210. totemrrp_stats_t stats;
  211. };
  212. static void stats_set_interface_faulty(struct totemrrp_instance *rrp_instance,
  213. unsigned int iface_no, int is_faulty);
  214. /*
  215. * None Replication Forward Declerations
  216. */
  217. static void none_mcast_recv (
  218. struct totemrrp_instance *instance,
  219. unsigned int iface_no,
  220. void *context,
  221. const void *msg,
  222. unsigned int msg_len);
  223. static void none_mcast_noflush_send (
  224. struct totemrrp_instance *instance,
  225. const void *msg,
  226. unsigned int msg_len);
  227. static void none_mcast_flush_send (
  228. struct totemrrp_instance *instance,
  229. const void *msg,
  230. unsigned int msg_len);
  231. static void none_token_recv (
  232. struct totemrrp_instance *instance,
  233. unsigned int iface_no,
  234. void *context,
  235. const void *msg,
  236. unsigned int msg_len,
  237. unsigned int token_seqid);
  238. static void none_token_send (
  239. struct totemrrp_instance *instance,
  240. const void *msg,
  241. unsigned int msg_len);
  242. static void none_recv_flush (
  243. struct totemrrp_instance *instance);
  244. static void none_send_flush (
  245. struct totemrrp_instance *instance);
  246. static void none_iface_check (
  247. struct totemrrp_instance *instance);
  248. static void none_processor_count_set (
  249. struct totemrrp_instance *instance,
  250. unsigned int processor_count_set);
  251. static void none_token_target_set (
  252. struct totemrrp_instance *instance,
  253. struct totem_ip_address *token_target,
  254. unsigned int iface_no);
  255. static void none_ring_reenable (
  256. struct totemrrp_instance *instance,
  257. unsigned int iface_no);
  258. static int none_mcast_recv_empty (
  259. struct totemrrp_instance *instance);
  260. static int none_member_add (
  261. struct totemrrp_instance *instance,
  262. const struct totem_ip_address *member,
  263. unsigned int iface_no);
  264. static int none_member_remove (
  265. struct totemrrp_instance *instance,
  266. const struct totem_ip_address *member,
  267. unsigned int iface_no);
  268. static void none_membership_changed (
  269. struct totemrrp_instance *instance,
  270. enum totem_configuration_type configuration_type,
  271. const struct srp_addr *member_list, size_t member_list_entries,
  272. const struct srp_addr *left_list, size_t left_list_entries,
  273. const struct srp_addr *joined_list, size_t joined_list_entries,
  274. const struct memb_ring_id *ring_id);
  275. /*
  276. * Passive Replication Forward Declerations
  277. */
  278. static void *passive_instance_initialize (
  279. struct totemrrp_instance *rrp_instance,
  280. int interface_count);
  281. static void passive_mcast_recv (
  282. struct totemrrp_instance *instance,
  283. unsigned int iface_no,
  284. void *context,
  285. const void *msg,
  286. unsigned int msg_len);
  287. static void passive_mcast_noflush_send (
  288. struct totemrrp_instance *instance,
  289. const void *msg,
  290. unsigned int msg_len);
  291. static void passive_mcast_flush_send (
  292. struct totemrrp_instance *instance,
  293. const void *msg,
  294. unsigned int msg_len);
  295. static void passive_monitor (
  296. struct totemrrp_instance *rrp_instance,
  297. unsigned int iface_no,
  298. int is_token_recv_count);
  299. static void passive_token_recv (
  300. struct totemrrp_instance *instance,
  301. unsigned int iface_no,
  302. void *context,
  303. const void *msg,
  304. unsigned int msg_len,
  305. unsigned int token_seqid);
  306. static void passive_token_send (
  307. struct totemrrp_instance *instance,
  308. const void *msg,
  309. unsigned int msg_len);
  310. static void passive_recv_flush (
  311. struct totemrrp_instance *instance);
  312. static void passive_send_flush (
  313. struct totemrrp_instance *instance);
  314. static void passive_iface_check (
  315. struct totemrrp_instance *instance);
  316. static void passive_processor_count_set (
  317. struct totemrrp_instance *instance,
  318. unsigned int processor_count_set);
  319. static void passive_token_target_set (
  320. struct totemrrp_instance *instance,
  321. struct totem_ip_address *token_target,
  322. unsigned int iface_no);
  323. static void passive_ring_reenable (
  324. struct totemrrp_instance *instance,
  325. unsigned int iface_no);
  326. static int passive_mcast_recv_empty (
  327. struct totemrrp_instance *instance);
  328. static int passive_member_add (
  329. struct totemrrp_instance *instance,
  330. const struct totem_ip_address *member,
  331. unsigned int iface_no);
  332. static int passive_member_remove (
  333. struct totemrrp_instance *instance,
  334. const struct totem_ip_address *member,
  335. unsigned int iface_no);
  336. static void passive_membership_changed (
  337. struct totemrrp_instance *instance,
  338. enum totem_configuration_type configuration_type,
  339. const struct srp_addr *member_list, size_t member_list_entries,
  340. const struct srp_addr *left_list, size_t left_list_entries,
  341. const struct srp_addr *joined_list, size_t joined_list_entries,
  342. const struct memb_ring_id *ring_id);
  343. /*
  344. * Active Replication Forward Definitions
  345. */
  346. static void *active_instance_initialize (
  347. struct totemrrp_instance *rrp_instance,
  348. int interface_count);
  349. static void active_mcast_recv (
  350. struct totemrrp_instance *instance,
  351. unsigned int iface_no,
  352. void *context,
  353. const void *msg,
  354. unsigned int msg_len);
  355. static void active_mcast_noflush_send (
  356. struct totemrrp_instance *instance,
  357. const void *msg,
  358. unsigned int msg_len);
  359. static void active_mcast_flush_send (
  360. struct totemrrp_instance *instance,
  361. const void *msg,
  362. unsigned int msg_len);
  363. static void active_token_recv (
  364. struct totemrrp_instance *instance,
  365. unsigned int iface_no,
  366. void *context,
  367. const void *msg,
  368. unsigned int msg_len,
  369. unsigned int token_seqid);
  370. static void active_token_send (
  371. struct totemrrp_instance *instance,
  372. const void *msg,
  373. unsigned int msg_len);
  374. static void active_recv_flush (
  375. struct totemrrp_instance *instance);
  376. static void active_send_flush (
  377. struct totemrrp_instance *instance);
  378. static void active_iface_check (
  379. struct totemrrp_instance *instance);
  380. static void active_processor_count_set (
  381. struct totemrrp_instance *instance,
  382. unsigned int processor_count_set);
  383. static void active_token_target_set (
  384. struct totemrrp_instance *instance,
  385. struct totem_ip_address *token_target,
  386. unsigned int iface_no);
  387. static void active_ring_reenable (
  388. struct totemrrp_instance *instance,
  389. unsigned int iface_no);
  390. static int active_mcast_recv_empty (
  391. struct totemrrp_instance *instance);
  392. static int active_member_add (
  393. struct totemrrp_instance *instance,
  394. const struct totem_ip_address *member,
  395. unsigned int iface_no);
  396. static int active_member_remove (
  397. struct totemrrp_instance *instance,
  398. const struct totem_ip_address *member,
  399. unsigned int iface_no);
  400. static void active_membership_changed (
  401. struct totemrrp_instance *instance,
  402. enum totem_configuration_type configuration_type,
  403. const struct srp_addr *member_list, size_t member_list_entries,
  404. const struct srp_addr *left_list, size_t left_list_entries,
  405. const struct srp_addr *joined_list, size_t joined_list_entries,
  406. const struct memb_ring_id *ring_id);
  407. static void active_timer_expired_token_start (
  408. struct active_instance *active_instance);
  409. static void active_timer_expired_token_cancel (
  410. struct active_instance *active_instance);
  411. static void active_timer_problem_decrementer_start (
  412. struct active_instance *active_instance);
  413. static void active_timer_problem_decrementer_cancel (
  414. struct active_instance *active_instance);
  415. /*
  416. * 0-5 reserved for totemsrp.c
  417. */
  418. #define MESSAGE_TYPE_RING_TEST_ACTIVE 6
  419. #define MESSAGE_TYPE_RING_TEST_ACTIVATE 7
  420. #define ENDIAN_LOCAL 0xff22
  421. /*
  422. * Rollover handling:
  423. *
  424. * ARR_SEQNO_START_TOKEN is the starting sequence number of last seen sequence
  425. * for a token for active redundand ring. This should remain zero, unless testing
  426. * overflow in which case 07fffff00 or 0xffffff00 are good starting values.
  427. * It should be same as on defined in totemsrp.c
  428. */
  429. #define ARR_SEQNO_START_TOKEN 0x0
  430. /*
  431. * These can be used ot test different rollover points
  432. * #define ARR_SEQNO_START_MSG 0xfffffe00
  433. */
  434. /*
  435. * Threshold value when recv_count for passive rrp should be adjusted.
  436. * Set this value to some smaller for testing of adjusting proper
  437. * functionality. Also keep in mind that this value must be smaller
  438. * then rrp_problem_count_threshold
  439. */
  440. #define PASSIVE_RECV_COUNT_THRESHOLD (INT_MAX / 2)
  441. struct message_header {
  442. char type;
  443. char encapsulated;
  444. unsigned short endian_detector;
  445. int ring_number;
  446. int nodeid_activator;
  447. } __attribute__((packed));
  448. struct deliver_fn_context {
  449. struct totemrrp_instance *instance;
  450. void *context;
  451. int iface_no;
  452. };
  453. struct rrp_algo none_algo = {
  454. .name = "none",
  455. .initialize = NULL,
  456. .mcast_recv = none_mcast_recv,
  457. .mcast_noflush_send = none_mcast_noflush_send,
  458. .mcast_flush_send = none_mcast_flush_send,
  459. .token_recv = none_token_recv,
  460. .token_send = none_token_send,
  461. .recv_flush = none_recv_flush,
  462. .send_flush = none_send_flush,
  463. .iface_check = none_iface_check,
  464. .processor_count_set = none_processor_count_set,
  465. .token_target_set = none_token_target_set,
  466. .ring_reenable = none_ring_reenable,
  467. .mcast_recv_empty = none_mcast_recv_empty,
  468. .member_add = none_member_add,
  469. .member_remove = none_member_remove,
  470. .membership_changed = none_membership_changed
  471. };
  472. struct rrp_algo passive_algo = {
  473. .name = "passive",
  474. .initialize = passive_instance_initialize,
  475. .mcast_recv = passive_mcast_recv,
  476. .mcast_noflush_send = passive_mcast_noflush_send,
  477. .mcast_flush_send = passive_mcast_flush_send,
  478. .token_recv = passive_token_recv,
  479. .token_send = passive_token_send,
  480. .recv_flush = passive_recv_flush,
  481. .send_flush = passive_send_flush,
  482. .iface_check = passive_iface_check,
  483. .processor_count_set = passive_processor_count_set,
  484. .token_target_set = passive_token_target_set,
  485. .ring_reenable = passive_ring_reenable,
  486. .mcast_recv_empty = passive_mcast_recv_empty,
  487. .member_add = passive_member_add,
  488. .member_remove = passive_member_remove,
  489. .membership_changed = passive_membership_changed
  490. };
  491. struct rrp_algo active_algo = {
  492. .name = "active",
  493. .initialize = active_instance_initialize,
  494. .mcast_recv = active_mcast_recv,
  495. .mcast_noflush_send = active_mcast_noflush_send,
  496. .mcast_flush_send = active_mcast_flush_send,
  497. .token_recv = active_token_recv,
  498. .token_send = active_token_send,
  499. .recv_flush = active_recv_flush,
  500. .send_flush = active_send_flush,
  501. .iface_check = active_iface_check,
  502. .processor_count_set = active_processor_count_set,
  503. .token_target_set = active_token_target_set,
  504. .ring_reenable = active_ring_reenable,
  505. .mcast_recv_empty = active_mcast_recv_empty,
  506. .member_add = active_member_add,
  507. .member_remove = active_member_remove,
  508. .membership_changed = active_membership_changed
  509. };
  510. struct rrp_algo *rrp_algos[] = {
  511. &none_algo,
  512. &passive_algo,
  513. &active_algo
  514. };
  515. #define RRP_ALGOS_COUNT 3
  516. #define log_printf(level, format, args...) \
  517. do { \
  518. rrp_instance->totemrrp_log_printf ( \
  519. level, rrp_instance->totemrrp_subsys_id, \
  520. __FUNCTION__, __FILE__, __LINE__, \
  521. format, ##args); \
  522. } while (0);
  523. static void stats_set_interface_faulty(struct totemrrp_instance *rrp_instance,
  524. unsigned int iface_no, int is_faulty)
  525. {
  526. rrp_instance->stats.faulty[iface_no] = (is_faulty ? 1 : 0);
  527. }
  528. static void test_active_msg_endian_convert(const struct message_header *in, struct message_header *out)
  529. {
  530. out->type = in->type;
  531. out->encapsulated = in->encapsulated;
  532. out->endian_detector = ENDIAN_LOCAL;
  533. out->ring_number = swab32 (in->ring_number);
  534. out->nodeid_activator = swab32(in->nodeid_activator);
  535. }
  536. static void timer_function_test_ring_timeout (void *context)
  537. {
  538. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  539. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  540. unsigned int *faulty = NULL;
  541. int iface_no = deliver_fn_context->iface_no;
  542. struct message_header msg = {
  543. .type = MESSAGE_TYPE_RING_TEST_ACTIVE,
  544. .endian_detector = ENDIAN_LOCAL,
  545. };
  546. if (strcmp(rrp_instance->totem_config->rrp_mode, "active") == 0)
  547. faulty = ((struct active_instance *)(rrp_instance->rrp_algo_instance))->faulty;
  548. if (strcmp(rrp_instance->totem_config->rrp_mode, "passive") == 0)
  549. faulty = ((struct passive_instance *)(rrp_instance->rrp_algo_instance))->faulty;
  550. assert (faulty != NULL);
  551. if (faulty[iface_no] == 1) {
  552. msg.ring_number = iface_no;
  553. msg.nodeid_activator = rrp_instance->my_nodeid;
  554. totemnet_token_send (
  555. rrp_instance->net_handles[iface_no],
  556. &msg, sizeof (struct message_header));
  557. qb_loop_timer_add (rrp_instance->poll_handle,
  558. QB_LOOP_MED,
  559. rrp_instance->totem_config->rrp_autorecovery_check_timeout*QB_TIME_NS_IN_MSEC,
  560. (void *)deliver_fn_context,
  561. timer_function_test_ring_timeout,
  562. &rrp_instance->timer_active_test_ring_timeout[iface_no]);
  563. }
  564. }
  565. /*
  566. * None Replication Implementation
  567. */
  568. static void none_mcast_recv (
  569. struct totemrrp_instance *rrp_instance,
  570. unsigned int iface_no,
  571. void *context,
  572. const void *msg,
  573. unsigned int msg_len)
  574. {
  575. rrp_instance->totemrrp_deliver_fn (
  576. context,
  577. msg,
  578. msg_len);
  579. }
  580. static void none_mcast_flush_send (
  581. struct totemrrp_instance *instance,
  582. const void *msg,
  583. unsigned int msg_len)
  584. {
  585. totemnet_mcast_flush_send (instance->net_handles[0], msg, msg_len);
  586. }
  587. static void none_mcast_noflush_send (
  588. struct totemrrp_instance *instance,
  589. const void *msg,
  590. unsigned int msg_len)
  591. {
  592. totemnet_mcast_noflush_send (instance->net_handles[0], msg, msg_len);
  593. }
  594. static void none_token_recv (
  595. struct totemrrp_instance *rrp_instance,
  596. unsigned int iface_no,
  597. void *context,
  598. const void *msg,
  599. unsigned int msg_len,
  600. unsigned int token_seq)
  601. {
  602. rrp_instance->totemrrp_deliver_fn (
  603. context,
  604. msg,
  605. msg_len);
  606. }
  607. static void none_token_send (
  608. struct totemrrp_instance *instance,
  609. const void *msg,
  610. unsigned int msg_len)
  611. {
  612. totemnet_token_send (
  613. instance->net_handles[0],
  614. msg, msg_len);
  615. }
  616. static void none_recv_flush (struct totemrrp_instance *instance)
  617. {
  618. totemnet_recv_flush (instance->net_handles[0]);
  619. }
  620. static void none_send_flush (struct totemrrp_instance *instance)
  621. {
  622. totemnet_send_flush (instance->net_handles[0]);
  623. }
  624. static void none_iface_check (struct totemrrp_instance *instance)
  625. {
  626. totemnet_iface_check (instance->net_handles[0]);
  627. }
  628. static void none_processor_count_set (
  629. struct totemrrp_instance *instance,
  630. unsigned int processor_count)
  631. {
  632. totemnet_processor_count_set (instance->net_handles[0],
  633. processor_count);
  634. }
  635. static void none_token_target_set (
  636. struct totemrrp_instance *instance,
  637. struct totem_ip_address *token_target,
  638. unsigned int iface_no)
  639. {
  640. totemnet_token_target_set (instance->net_handles[0], token_target);
  641. }
  642. static void none_ring_reenable (
  643. struct totemrrp_instance *instance,
  644. unsigned int iface_no)
  645. {
  646. /*
  647. * No operation
  648. */
  649. }
  650. static int none_mcast_recv_empty (
  651. struct totemrrp_instance *instance)
  652. {
  653. int res;
  654. res = totemnet_recv_mcast_empty (instance->net_handles[0]);
  655. return (res);
  656. }
  657. static int none_member_add (
  658. struct totemrrp_instance *instance,
  659. const struct totem_ip_address *member,
  660. unsigned int iface_no)
  661. {
  662. int res;
  663. res = totemnet_member_add (instance->net_handles[0], member);
  664. return (res);
  665. }
  666. static int none_member_remove (
  667. struct totemrrp_instance *instance,
  668. const struct totem_ip_address *member,
  669. unsigned int iface_no)
  670. {
  671. int res;
  672. res = totemnet_member_remove (instance->net_handles[0], member);
  673. return (res);
  674. }
  675. static void none_membership_changed (
  676. struct totemrrp_instance *instance,
  677. enum totem_configuration_type configuration_type,
  678. const struct srp_addr *member_list, size_t member_list_entries,
  679. const struct srp_addr *left_list, size_t left_list_entries,
  680. const struct srp_addr *joined_list, size_t joined_list_entries,
  681. const struct memb_ring_id *ring_id)
  682. {
  683. }
  684. /*
  685. * Passive Replication Implementation
  686. */
  687. void *passive_instance_initialize (
  688. struct totemrrp_instance *rrp_instance,
  689. int interface_count)
  690. {
  691. struct passive_instance *instance;
  692. int i;
  693. instance = malloc (sizeof (struct passive_instance));
  694. if (instance == 0) {
  695. goto error_exit;
  696. }
  697. memset (instance, 0, sizeof (struct passive_instance));
  698. instance->faulty = malloc (sizeof (int) * interface_count);
  699. if (instance->faulty == 0) {
  700. free (instance);
  701. instance = 0;
  702. goto error_exit;
  703. }
  704. memset (instance->faulty, 0, sizeof (int) * interface_count);
  705. for (i = 0; i < interface_count; i++) {
  706. stats_set_interface_faulty (rrp_instance, i, 0);
  707. }
  708. instance->token_recv_count = malloc (sizeof (int) * interface_count);
  709. if (instance->token_recv_count == 0) {
  710. free (instance->faulty);
  711. free (instance);
  712. instance = 0;
  713. goto error_exit;
  714. }
  715. memset (instance->token_recv_count, 0, sizeof (int) * interface_count);
  716. instance->mcast_recv_count = malloc (sizeof (int) * interface_count);
  717. if (instance->mcast_recv_count == 0) {
  718. free (instance->token_recv_count);
  719. free (instance->faulty);
  720. free (instance);
  721. instance = 0;
  722. goto error_exit;
  723. }
  724. memset (instance->mcast_recv_count, 0, sizeof (int) * interface_count);
  725. error_exit:
  726. return ((void *)instance);
  727. }
  728. static void timer_function_passive_token_expired (void *context)
  729. {
  730. struct passive_instance *passive_instance = (struct passive_instance *)context;
  731. struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  732. rrp_instance->totemrrp_deliver_fn (
  733. passive_instance->totemrrp_context,
  734. passive_instance->token,
  735. passive_instance->token_len);
  736. }
  737. /* TODO
  738. static void timer_function_passive_problem_decrementer (void *context)
  739. {
  740. // struct passive_instance *passive_instance = (struct passive_instance *)context;
  741. // struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  742. }
  743. */
  744. static void passive_timer_expired_token_start (
  745. struct passive_instance *passive_instance)
  746. {
  747. qb_loop_timer_add (
  748. passive_instance->rrp_instance->poll_handle,
  749. QB_LOOP_MED,
  750. passive_instance->rrp_instance->totem_config->rrp_token_expired_timeout*QB_TIME_NS_IN_MSEC,
  751. (void *)passive_instance,
  752. timer_function_passive_token_expired,
  753. &passive_instance->timer_expired_token);
  754. }
  755. static void passive_timer_expired_token_cancel (
  756. struct passive_instance *passive_instance)
  757. {
  758. qb_loop_timer_del (
  759. passive_instance->rrp_instance->poll_handle,
  760. passive_instance->timer_expired_token);
  761. }
  762. /*
  763. static void passive_timer_problem_decrementer_start (
  764. struct passive_instance *passive_instance)
  765. {
  766. qb_loop_timer_add (
  767. QB_LOOP_MED,
  768. passive_instance->rrp_instance->poll_handle,
  769. passive_instance->rrp_instance->totem_config->rrp_problem_count_timeout*QB_TIME_NS_IN_MSEC,
  770. (void *)passive_instance,
  771. timer_function_passive_problem_decrementer,
  772. &passive_instance->timer_problem_decrementer);
  773. }
  774. static void passive_timer_problem_decrementer_cancel (
  775. struct passive_instance *passive_instance)
  776. {
  777. qb_loop_timer_del (
  778. passive_instance->rrp_instance->poll_handle,
  779. passive_instance->timer_problem_decrementer);
  780. }
  781. */
  782. /*
  783. * Monitor function implementation from rrp paper.
  784. * rrp_instance is passive rrp instance, iface_no is interface with received messgae/token and
  785. * is_token_recv_count is boolean variable which donates if message is token (>1) or regular
  786. * message (= 0)
  787. */
  788. static void passive_monitor (
  789. struct totemrrp_instance *rrp_instance,
  790. unsigned int iface_no,
  791. int is_token_recv_count)
  792. {
  793. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  794. unsigned int *recv_count;
  795. unsigned int max;
  796. unsigned int i;
  797. unsigned int min_all, min_active;
  798. unsigned int threshold;
  799. /*
  800. * Monitor for failures
  801. */
  802. if (is_token_recv_count) {
  803. recv_count = passive_instance->token_recv_count;
  804. threshold = rrp_instance->totem_config->rrp_problem_count_threshold;
  805. } else {
  806. recv_count = passive_instance->mcast_recv_count;
  807. threshold = rrp_instance->totem_config->rrp_problem_count_mcast_threshold;
  808. }
  809. recv_count[iface_no] += 1;
  810. max = 0;
  811. for (i = 0; i < rrp_instance->interface_count; i++) {
  812. if (max < recv_count[i]) {
  813. max = recv_count[i];
  814. }
  815. }
  816. /*
  817. * Max is larger then threshold -> start adjusting process
  818. */
  819. if (max > PASSIVE_RECV_COUNT_THRESHOLD) {
  820. min_all = min_active = recv_count[iface_no];
  821. for (i = 0; i < rrp_instance->interface_count; i++) {
  822. if (recv_count[i] < min_all) {
  823. min_all = recv_count[i];
  824. }
  825. if (passive_instance->faulty[i] == 0 &&
  826. recv_count[i] < min_active) {
  827. min_active = recv_count[i];
  828. }
  829. }
  830. if (min_all > 0) {
  831. /*
  832. * There is one or more faulty device with recv_count > 0
  833. */
  834. for (i = 0; i < rrp_instance->interface_count; i++) {
  835. recv_count[i] -= min_all;
  836. }
  837. } else {
  838. /*
  839. * No faulty device with recv_count > 0, adjust only active
  840. * devices
  841. */
  842. for (i = 0; i < rrp_instance->interface_count; i++) {
  843. if (passive_instance->faulty[i] == 0) {
  844. recv_count[i] -= min_active;
  845. }
  846. }
  847. }
  848. /*
  849. * Find again max
  850. */
  851. max = 0;
  852. for (i = 0; i < rrp_instance->interface_count; i++) {
  853. if (max < recv_count[i]) {
  854. max = recv_count[i];
  855. }
  856. }
  857. }
  858. for (i = 0; i < rrp_instance->interface_count; i++) {
  859. if ((passive_instance->faulty[i] == 0) &&
  860. (max - recv_count[i] > threshold)) {
  861. passive_instance->faulty[i] = 1;
  862. qb_loop_timer_add (rrp_instance->poll_handle,
  863. QB_LOOP_MED,
  864. rrp_instance->totem_config->rrp_autorecovery_check_timeout*QB_TIME_NS_IN_MSEC,
  865. rrp_instance->deliver_fn_context[i],
  866. timer_function_test_ring_timeout,
  867. &rrp_instance->timer_active_test_ring_timeout[i]);
  868. stats_set_interface_faulty (rrp_instance, i, passive_instance->faulty[i]);
  869. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  870. "Marking ringid %u interface %s FAULTY",
  871. i,
  872. totemnet_iface_print (rrp_instance->net_handles[i]));
  873. log_printf (
  874. rrp_instance->totemrrp_log_level_error,
  875. "%s",
  876. rrp_instance->status[i]);
  877. }
  878. }
  879. }
  880. static void passive_mcast_recv (
  881. struct totemrrp_instance *rrp_instance,
  882. unsigned int iface_no,
  883. void *context,
  884. const void *msg,
  885. unsigned int msg_len)
  886. {
  887. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  888. rrp_instance->totemrrp_deliver_fn (
  889. context,
  890. msg,
  891. msg_len);
  892. if (rrp_instance->totemrrp_msgs_missing() == 0 &&
  893. passive_instance->timer_expired_token) {
  894. /*
  895. * Delivers the last token
  896. */
  897. rrp_instance->totemrrp_deliver_fn (
  898. passive_instance->totemrrp_context,
  899. passive_instance->token,
  900. passive_instance->token_len);
  901. passive_timer_expired_token_cancel (passive_instance);
  902. }
  903. passive_monitor (rrp_instance, iface_no, 0);
  904. }
  905. static void passive_mcast_flush_send (
  906. struct totemrrp_instance *instance,
  907. const void *msg,
  908. unsigned int msg_len)
  909. {
  910. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  911. int i = 0;
  912. do {
  913. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  914. i++;
  915. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1));
  916. if (i <= instance->interface_count) {
  917. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  918. }
  919. }
  920. static void passive_mcast_noflush_send (
  921. struct totemrrp_instance *instance,
  922. const void *msg,
  923. unsigned int msg_len)
  924. {
  925. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  926. int i = 0;
  927. do {
  928. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  929. i++;
  930. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1));
  931. if (i <= instance->interface_count) {
  932. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  933. }
  934. }
  935. static void passive_token_recv (
  936. struct totemrrp_instance *rrp_instance,
  937. unsigned int iface_no,
  938. void *context,
  939. const void *msg,
  940. unsigned int msg_len,
  941. unsigned int token_seq)
  942. {
  943. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  944. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  945. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  946. rrp_instance->totemrrp_deliver_fn (
  947. context,
  948. msg,
  949. msg_len);
  950. } else {
  951. memcpy (passive_instance->token, msg, msg_len);
  952. passive_timer_expired_token_start (passive_instance);
  953. }
  954. passive_monitor (rrp_instance, iface_no, 1);
  955. }
  956. static void passive_token_send (
  957. struct totemrrp_instance *instance,
  958. const void *msg,
  959. unsigned int msg_len)
  960. {
  961. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  962. int i = 0;
  963. do {
  964. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  965. i++;
  966. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->token_xmit_iface] == 1));
  967. if (i <= instance->interface_count) {
  968. totemnet_token_send (
  969. instance->net_handles[passive_instance->token_xmit_iface],
  970. msg, msg_len);
  971. }
  972. }
  973. static void passive_recv_flush (struct totemrrp_instance *instance)
  974. {
  975. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  976. unsigned int i;
  977. for (i = 0; i < instance->interface_count; i++) {
  978. if (rrp_algo_instance->faulty[i] == 0) {
  979. totemnet_recv_flush (instance->net_handles[i]);
  980. }
  981. }
  982. }
  983. static void passive_send_flush (struct totemrrp_instance *instance)
  984. {
  985. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  986. unsigned int i;
  987. for (i = 0; i < instance->interface_count; i++) {
  988. if (rrp_algo_instance->faulty[i] == 0) {
  989. totemnet_send_flush (instance->net_handles[i]);
  990. }
  991. }
  992. }
  993. static void passive_iface_check (struct totemrrp_instance *instance)
  994. {
  995. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  996. unsigned int i;
  997. for (i = 0; i < instance->interface_count; i++) {
  998. if (rrp_algo_instance->faulty[i] == 0) {
  999. totemnet_iface_check (instance->net_handles[i]);
  1000. }
  1001. }
  1002. }
  1003. static void passive_processor_count_set (
  1004. struct totemrrp_instance *instance,
  1005. unsigned int processor_count)
  1006. {
  1007. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1008. unsigned int i;
  1009. for (i = 0; i < instance->interface_count; i++) {
  1010. if (rrp_algo_instance->faulty[i] == 0) {
  1011. totemnet_processor_count_set (instance->net_handles[i],
  1012. processor_count);
  1013. }
  1014. }
  1015. }
  1016. static void passive_token_target_set (
  1017. struct totemrrp_instance *instance,
  1018. struct totem_ip_address *token_target,
  1019. unsigned int iface_no)
  1020. {
  1021. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1022. }
  1023. static int passive_mcast_recv_empty (
  1024. struct totemrrp_instance *instance)
  1025. {
  1026. int res;
  1027. int msgs_emptied = 0;
  1028. int i;
  1029. for (i = 0; i < instance->interface_count; i++) {
  1030. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1031. if (res == -1) {
  1032. return (-1);
  1033. }
  1034. if (res == 1) {
  1035. msgs_emptied = 1;
  1036. }
  1037. }
  1038. return (msgs_emptied);
  1039. }
  1040. static int passive_member_add (
  1041. struct totemrrp_instance *instance,
  1042. const struct totem_ip_address *member,
  1043. unsigned int iface_no)
  1044. {
  1045. int res;
  1046. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1047. return (res);
  1048. }
  1049. static int passive_member_remove (
  1050. struct totemrrp_instance *instance,
  1051. const struct totem_ip_address *member,
  1052. unsigned int iface_no)
  1053. {
  1054. int res;
  1055. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1056. return (res);
  1057. }
  1058. static void passive_membership_changed (
  1059. struct totemrrp_instance *instance,
  1060. enum totem_configuration_type configuration_type,
  1061. const struct srp_addr *member_list, size_t member_list_entries,
  1062. const struct srp_addr *left_list, size_t left_list_entries,
  1063. const struct srp_addr *joined_list, size_t joined_list_entries,
  1064. const struct memb_ring_id *ring_id)
  1065. {
  1066. }
  1067. static void passive_ring_reenable (
  1068. struct totemrrp_instance *instance,
  1069. unsigned int iface_no)
  1070. {
  1071. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1072. int i;
  1073. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  1074. instance->interface_count);
  1075. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  1076. instance->interface_count);
  1077. if (iface_no == instance->interface_count) {
  1078. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1079. instance->interface_count);
  1080. for (i = 0; i < instance->interface_count; i++) {
  1081. stats_set_interface_faulty (instance, i, 0);
  1082. }
  1083. } else {
  1084. rrp_algo_instance->faulty[iface_no] = 0;
  1085. stats_set_interface_faulty (instance, iface_no, 0);
  1086. }
  1087. }
  1088. /*
  1089. * Active Replication Implementation
  1090. */
  1091. void *active_instance_initialize (
  1092. struct totemrrp_instance *rrp_instance,
  1093. int interface_count)
  1094. {
  1095. struct active_instance *instance;
  1096. int i;
  1097. instance = malloc (sizeof (struct active_instance));
  1098. if (instance == 0) {
  1099. goto error_exit;
  1100. }
  1101. memset (instance, 0, sizeof (struct active_instance));
  1102. instance->faulty = malloc (sizeof (int) * interface_count);
  1103. if (instance->faulty == 0) {
  1104. free (instance);
  1105. instance = 0;
  1106. goto error_exit;
  1107. }
  1108. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  1109. for (i = 0; i < interface_count; i++) {
  1110. stats_set_interface_faulty (rrp_instance, i, 0);
  1111. }
  1112. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  1113. if (instance->last_token_recv == 0) {
  1114. free (instance->faulty);
  1115. free (instance);
  1116. instance = 0;
  1117. goto error_exit;
  1118. }
  1119. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  1120. instance->counter_problems = malloc (sizeof (int) * interface_count);
  1121. if (instance->counter_problems == 0) {
  1122. free (instance->last_token_recv);
  1123. free (instance->faulty);
  1124. free (instance);
  1125. instance = 0;
  1126. goto error_exit;
  1127. }
  1128. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  1129. instance->timer_expired_token = 0;
  1130. instance->timer_problem_decrementer = 0;
  1131. instance->rrp_instance = rrp_instance;
  1132. instance->last_token_seq = ARR_SEQNO_START_TOKEN - 1;
  1133. error_exit:
  1134. return ((void *)instance);
  1135. }
  1136. static void timer_function_active_problem_decrementer (void *context)
  1137. {
  1138. struct active_instance *active_instance = (struct active_instance *)context;
  1139. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1140. unsigned int problem_found = 0;
  1141. unsigned int i;
  1142. for (i = 0; i < rrp_instance->interface_count; i++) {
  1143. if (active_instance->counter_problems[i] > 0) {
  1144. problem_found = 1;
  1145. active_instance->counter_problems[i] -= 1;
  1146. if (active_instance->counter_problems[i] == 0) {
  1147. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1148. "ring %d active with no faults", i);
  1149. } else {
  1150. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1151. "Decrementing problem counter for iface %s to [%d of %d]",
  1152. totemnet_iface_print (rrp_instance->net_handles[i]),
  1153. active_instance->counter_problems[i],
  1154. rrp_instance->totem_config->rrp_problem_count_threshold);
  1155. }
  1156. log_printf (
  1157. rrp_instance->totemrrp_log_level_warning,
  1158. "%s",
  1159. rrp_instance->status[i]);
  1160. }
  1161. }
  1162. if (problem_found) {
  1163. active_timer_problem_decrementer_start (active_instance);
  1164. } else {
  1165. active_instance->timer_problem_decrementer = 0;
  1166. }
  1167. }
  1168. static void timer_function_active_token_expired (void *context)
  1169. {
  1170. struct active_instance *active_instance = (struct active_instance *)context;
  1171. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1172. unsigned int i;
  1173. for (i = 0; i < rrp_instance->interface_count; i++) {
  1174. if (active_instance->last_token_recv[i] == 0) {
  1175. active_instance->counter_problems[i] += 1;
  1176. if (active_instance->timer_problem_decrementer == 0) {
  1177. active_timer_problem_decrementer_start (active_instance);
  1178. }
  1179. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1180. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  1181. active_instance->last_token_seq,
  1182. totemnet_iface_print (rrp_instance->net_handles[i]),
  1183. active_instance->counter_problems[i],
  1184. rrp_instance->totem_config->rrp_problem_count_threshold);
  1185. log_printf (
  1186. rrp_instance->totemrrp_log_level_warning,
  1187. "%s",
  1188. rrp_instance->status[i]);
  1189. }
  1190. }
  1191. for (i = 0; i < rrp_instance->interface_count; i++) {
  1192. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold &&
  1193. active_instance->faulty[i] == 0) {
  1194. active_instance->faulty[i] = 1;
  1195. qb_loop_timer_add (rrp_instance->poll_handle,
  1196. QB_LOOP_MED,
  1197. rrp_instance->totem_config->rrp_autorecovery_check_timeout*QB_TIME_NS_IN_MSEC,
  1198. rrp_instance->deliver_fn_context[i],
  1199. timer_function_test_ring_timeout,
  1200. &rrp_instance->timer_active_test_ring_timeout[i]);
  1201. stats_set_interface_faulty (rrp_instance, i, active_instance->faulty[i]);
  1202. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1203. "Marking seqid %d ringid %u interface %s FAULTY",
  1204. active_instance->last_token_seq,
  1205. i,
  1206. totemnet_iface_print (rrp_instance->net_handles[i]));
  1207. log_printf (
  1208. rrp_instance->totemrrp_log_level_error,
  1209. "%s",
  1210. rrp_instance->status[i]);
  1211. active_timer_problem_decrementer_cancel (active_instance);
  1212. }
  1213. }
  1214. rrp_instance->totemrrp_deliver_fn (
  1215. active_instance->totemrrp_context,
  1216. active_instance->token,
  1217. active_instance->token_len);
  1218. }
  1219. static void active_timer_expired_token_start (
  1220. struct active_instance *active_instance)
  1221. {
  1222. qb_loop_timer_add (
  1223. active_instance->rrp_instance->poll_handle,
  1224. QB_LOOP_MED,
  1225. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout*QB_TIME_NS_IN_MSEC,
  1226. (void *)active_instance,
  1227. timer_function_active_token_expired,
  1228. &active_instance->timer_expired_token);
  1229. }
  1230. static void active_timer_expired_token_cancel (
  1231. struct active_instance *active_instance)
  1232. {
  1233. qb_loop_timer_del (
  1234. active_instance->rrp_instance->poll_handle,
  1235. active_instance->timer_expired_token);
  1236. }
  1237. static void active_timer_problem_decrementer_start (
  1238. struct active_instance *active_instance)
  1239. {
  1240. qb_loop_timer_add (
  1241. active_instance->rrp_instance->poll_handle,
  1242. QB_LOOP_MED,
  1243. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout*QB_TIME_NS_IN_MSEC,
  1244. (void *)active_instance,
  1245. timer_function_active_problem_decrementer,
  1246. &active_instance->timer_problem_decrementer);
  1247. }
  1248. static void active_timer_problem_decrementer_cancel (
  1249. struct active_instance *active_instance)
  1250. {
  1251. qb_loop_timer_del (
  1252. active_instance->rrp_instance->poll_handle,
  1253. active_instance->timer_problem_decrementer);
  1254. }
  1255. /*
  1256. * active replication
  1257. */
  1258. static void active_mcast_recv (
  1259. struct totemrrp_instance *instance,
  1260. unsigned int iface_no,
  1261. void *context,
  1262. const void *msg,
  1263. unsigned int msg_len)
  1264. {
  1265. instance->totemrrp_deliver_fn (
  1266. context,
  1267. msg,
  1268. msg_len);
  1269. }
  1270. static void active_mcast_flush_send (
  1271. struct totemrrp_instance *instance,
  1272. const void *msg,
  1273. unsigned int msg_len)
  1274. {
  1275. int i;
  1276. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1277. for (i = 0; i < instance->interface_count; i++) {
  1278. if (rrp_algo_instance->faulty[i] == 0) {
  1279. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  1280. }
  1281. }
  1282. }
  1283. static void active_mcast_noflush_send (
  1284. struct totemrrp_instance *instance,
  1285. const void *msg,
  1286. unsigned int msg_len)
  1287. {
  1288. int i;
  1289. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1290. for (i = 0; i < instance->interface_count; i++) {
  1291. if (rrp_algo_instance->faulty[i] == 0) {
  1292. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  1293. }
  1294. }
  1295. }
  1296. static void active_token_recv (
  1297. struct totemrrp_instance *rrp_instance,
  1298. unsigned int iface_no,
  1299. void *context,
  1300. const void *msg,
  1301. unsigned int msg_len,
  1302. unsigned int token_seq)
  1303. {
  1304. int i;
  1305. struct active_instance *active_instance = (struct active_instance *)rrp_instance->rrp_algo_instance;
  1306. active_instance->totemrrp_context = context;
  1307. if (sq_lt_compare (active_instance->last_token_seq, token_seq)) {
  1308. memcpy (active_instance->token, msg, msg_len);
  1309. active_instance->token_len = msg_len;
  1310. for (i = 0; i < rrp_instance->interface_count; i++) {
  1311. active_instance->last_token_recv[i] = 0;
  1312. }
  1313. active_instance->last_token_recv[iface_no] = 1;
  1314. active_timer_expired_token_start (active_instance);
  1315. }
  1316. /*
  1317. * This doesn't follow spec because the spec assumes we will know
  1318. * when token resets occur.
  1319. */
  1320. active_instance->last_token_seq = token_seq;
  1321. if (token_seq == active_instance->last_token_seq) {
  1322. active_instance->last_token_recv[iface_no] = 1;
  1323. for (i = 0; i < rrp_instance->interface_count; i++) {
  1324. if ((active_instance->last_token_recv[i] == 0) &&
  1325. active_instance->faulty[i] == 0) {
  1326. return; /* don't deliver token */
  1327. }
  1328. }
  1329. active_timer_expired_token_cancel (active_instance);
  1330. rrp_instance->totemrrp_deliver_fn (
  1331. context,
  1332. msg,
  1333. msg_len);
  1334. }
  1335. }
  1336. static void active_token_send (
  1337. struct totemrrp_instance *instance,
  1338. const void *msg,
  1339. unsigned int msg_len)
  1340. {
  1341. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1342. int i;
  1343. for (i = 0; i < instance->interface_count; i++) {
  1344. if (rrp_algo_instance->faulty[i] == 0) {
  1345. totemnet_token_send (
  1346. instance->net_handles[i],
  1347. msg, msg_len);
  1348. }
  1349. }
  1350. }
  1351. static void active_recv_flush (struct totemrrp_instance *instance)
  1352. {
  1353. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1354. unsigned int i;
  1355. for (i = 0; i < instance->interface_count; i++) {
  1356. if (rrp_algo_instance->faulty[i] == 0) {
  1357. totemnet_recv_flush (instance->net_handles[i]);
  1358. }
  1359. }
  1360. }
  1361. static void active_send_flush (struct totemrrp_instance *instance)
  1362. {
  1363. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1364. unsigned int i;
  1365. for (i = 0; i < instance->interface_count; i++) {
  1366. if (rrp_algo_instance->faulty[i] == 0) {
  1367. totemnet_send_flush (instance->net_handles[i]);
  1368. }
  1369. }
  1370. }
  1371. static int active_member_add (
  1372. struct totemrrp_instance *instance,
  1373. const struct totem_ip_address *member,
  1374. unsigned int iface_no)
  1375. {
  1376. int res;
  1377. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1378. return (res);
  1379. }
  1380. static int active_member_remove (
  1381. struct totemrrp_instance *instance,
  1382. const struct totem_ip_address *member,
  1383. unsigned int iface_no)
  1384. {
  1385. int res;
  1386. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1387. return (res);
  1388. }
  1389. static void active_membership_changed (
  1390. struct totemrrp_instance *instance,
  1391. enum totem_configuration_type configuration_type,
  1392. const struct srp_addr *member_list, size_t member_list_entries,
  1393. const struct srp_addr *left_list, size_t left_list_entries,
  1394. const struct srp_addr *joined_list, size_t joined_list_entries,
  1395. const struct memb_ring_id *ring_id)
  1396. {
  1397. }
  1398. static void active_iface_check (struct totemrrp_instance *instance)
  1399. {
  1400. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1401. unsigned int i;
  1402. for (i = 0; i < instance->interface_count; i++) {
  1403. if (rrp_algo_instance->faulty[i] == 0) {
  1404. totemnet_iface_check (instance->net_handles[i]);
  1405. }
  1406. }
  1407. }
  1408. static void active_processor_count_set (
  1409. struct totemrrp_instance *instance,
  1410. unsigned int processor_count)
  1411. {
  1412. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1413. unsigned int i;
  1414. for (i = 0; i < instance->interface_count; i++) {
  1415. if (rrp_algo_instance->faulty[i] == 0) {
  1416. totemnet_processor_count_set (instance->net_handles[i],
  1417. processor_count);
  1418. }
  1419. }
  1420. }
  1421. static void active_token_target_set (
  1422. struct totemrrp_instance *instance,
  1423. struct totem_ip_address *token_target,
  1424. unsigned int iface_no)
  1425. {
  1426. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1427. }
  1428. static int active_mcast_recv_empty (
  1429. struct totemrrp_instance *instance)
  1430. {
  1431. int res;
  1432. int msgs_emptied = 0;
  1433. int i;
  1434. for (i = 0; i < instance->interface_count; i++) {
  1435. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1436. if (res == -1) {
  1437. return (-1);
  1438. }
  1439. if (res == 1) {
  1440. msgs_emptied = 1;
  1441. }
  1442. }
  1443. return (msgs_emptied);
  1444. }
  1445. static void active_ring_reenable (
  1446. struct totemrrp_instance *instance,
  1447. unsigned int iface_no)
  1448. {
  1449. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1450. int i;
  1451. if (iface_no == instance->interface_count) {
  1452. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1453. instance->interface_count);
  1454. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1455. instance->interface_count);
  1456. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1457. instance->interface_count);
  1458. for (i = 0; i < instance->interface_count; i++) {
  1459. stats_set_interface_faulty (instance, i, 0);
  1460. }
  1461. } else {
  1462. rrp_algo_instance->last_token_recv[iface_no] = 0;
  1463. rrp_algo_instance->faulty[iface_no] = 0;
  1464. rrp_algo_instance->counter_problems[iface_no] = 0;
  1465. stats_set_interface_faulty (instance, iface_no, 0);
  1466. }
  1467. }
  1468. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1469. {
  1470. memset (instance, 0, sizeof (struct totemrrp_instance));
  1471. }
  1472. static int totemrrp_algorithm_set (
  1473. struct totem_config *totem_config,
  1474. struct totemrrp_instance *instance)
  1475. {
  1476. unsigned int res = -1;
  1477. unsigned int i;
  1478. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1479. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1480. instance->rrp_algo = rrp_algos[i];
  1481. if (rrp_algos[i]->initialize) {
  1482. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1483. instance,
  1484. totem_config->interface_count);
  1485. }
  1486. res = 0;
  1487. break;
  1488. }
  1489. }
  1490. for (i = 0; i < totem_config->interface_count; i++) {
  1491. instance->status[i] = malloc (STATUS_STR_LEN+1);
  1492. snprintf (instance->status[i], STATUS_STR_LEN,
  1493. "ring %d active with no faults", i);
  1494. }
  1495. return (res);
  1496. }
  1497. void rrp_deliver_fn (
  1498. void *context,
  1499. const void *msg,
  1500. unsigned int msg_len)
  1501. {
  1502. unsigned int token_seqid;
  1503. unsigned int token_is;
  1504. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1505. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  1506. const struct message_header *hdr = msg;
  1507. struct message_header tmp_msg, activate_msg;
  1508. memset(&tmp_msg, 0, sizeof(struct message_header));
  1509. memset(&activate_msg, 0, sizeof(struct message_header));
  1510. rrp_instance->totemrrp_token_seqid_get (
  1511. msg,
  1512. &token_seqid,
  1513. &token_is);
  1514. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVE) {
  1515. log_printf (
  1516. rrp_instance->totemrrp_log_level_debug,
  1517. "received message requesting test of ring now active");
  1518. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1519. test_active_msg_endian_convert(hdr, &tmp_msg);
  1520. hdr = &tmp_msg;
  1521. }
  1522. if (hdr->nodeid_activator == rrp_instance->my_nodeid) {
  1523. /*
  1524. * Send an activate message
  1525. */
  1526. activate_msg.type = MESSAGE_TYPE_RING_TEST_ACTIVATE;
  1527. activate_msg.endian_detector = ENDIAN_LOCAL;
  1528. activate_msg.ring_number = hdr->ring_number;
  1529. activate_msg.nodeid_activator = rrp_instance->my_nodeid;
  1530. totemnet_token_send (
  1531. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1532. &activate_msg, sizeof (struct message_header));
  1533. } else {
  1534. /*
  1535. * Send a ring test message
  1536. */
  1537. totemnet_token_send (
  1538. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1539. msg, msg_len);
  1540. }
  1541. } else
  1542. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVATE) {
  1543. log_printf (
  1544. rrp_instance->totemrrp_log_level_notice,
  1545. "Automatically recovered ring %d", hdr->ring_number);
  1546. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1547. test_active_msg_endian_convert(hdr, &tmp_msg);
  1548. hdr = &tmp_msg;
  1549. }
  1550. totemrrp_ring_reenable (rrp_instance, deliver_fn_context->iface_no);
  1551. if (hdr->nodeid_activator != rrp_instance->my_nodeid) {
  1552. totemnet_token_send (
  1553. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1554. msg, msg_len);
  1555. }
  1556. } else
  1557. if (token_is) {
  1558. /*
  1559. * Deliver to the token receiver for this rrp algorithm
  1560. */
  1561. rrp_instance->rrp_algo->token_recv (
  1562. rrp_instance,
  1563. deliver_fn_context->iface_no,
  1564. deliver_fn_context->context,
  1565. msg,
  1566. msg_len,
  1567. token_seqid);
  1568. } else {
  1569. /*
  1570. * Deliver to the mcast receiver for this rrp algorithm
  1571. */
  1572. rrp_instance->rrp_algo->mcast_recv (
  1573. rrp_instance,
  1574. deliver_fn_context->iface_no,
  1575. deliver_fn_context->context,
  1576. msg,
  1577. msg_len);
  1578. }
  1579. }
  1580. void rrp_iface_change_fn (
  1581. void *context,
  1582. const struct totem_ip_address *iface_addr)
  1583. {
  1584. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1585. deliver_fn_context->instance->my_nodeid = iface_addr->nodeid;
  1586. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1587. deliver_fn_context->context,
  1588. iface_addr,
  1589. deliver_fn_context->iface_no);
  1590. }
  1591. int totemrrp_finalize (
  1592. void *rrp_context)
  1593. {
  1594. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1595. int i;
  1596. for (i = 0; i < instance->interface_count; i++) {
  1597. totemnet_finalize (instance->net_handles[i]);
  1598. }
  1599. free (instance->net_handles);
  1600. free (instance);
  1601. return (0);
  1602. }
  1603. static void rrp_target_set_completed (void *context)
  1604. {
  1605. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1606. deliver_fn_context->instance->totemrrp_target_set_completed (deliver_fn_context->context);
  1607. }
  1608. /*
  1609. * Totem Redundant Ring interface
  1610. * depends on poll abstraction, POSIX, IPV4
  1611. */
  1612. /*
  1613. * Create an instance
  1614. */
  1615. int totemrrp_initialize (
  1616. qb_loop_t *poll_handle,
  1617. void **rrp_context,
  1618. struct totem_config *totem_config,
  1619. totemsrp_stats_t *stats,
  1620. void *context,
  1621. void (*deliver_fn) (
  1622. void *context,
  1623. const void *msg,
  1624. unsigned int msg_len),
  1625. void (*iface_change_fn) (
  1626. void *context,
  1627. const struct totem_ip_address *iface_addr,
  1628. unsigned int iface_no),
  1629. void (*token_seqid_get) (
  1630. const void *msg,
  1631. unsigned int *seqid,
  1632. unsigned int *token_is),
  1633. unsigned int (*msgs_missing) (void),
  1634. void (*target_set_completed) (void *context))
  1635. {
  1636. struct totemrrp_instance *instance;
  1637. unsigned int res;
  1638. int i;
  1639. instance = malloc (sizeof (struct totemrrp_instance));
  1640. if (instance == 0) {
  1641. return (-1);
  1642. }
  1643. totemrrp_instance_initialize (instance);
  1644. instance->totem_config = totem_config;
  1645. stats->rrp = &instance->stats;
  1646. instance->stats.interface_count = totem_config->interface_count;
  1647. instance->stats.faulty = calloc(instance->stats.interface_count, sizeof(uint8_t));
  1648. res = totemrrp_algorithm_set (
  1649. instance->totem_config,
  1650. instance);
  1651. if (res == -1) {
  1652. goto error_destroy;
  1653. }
  1654. /*
  1655. * Configure logging
  1656. */
  1657. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1658. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1659. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1660. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1661. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1662. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1663. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1664. instance->interfaces = totem_config->interfaces;
  1665. instance->poll_handle = poll_handle;
  1666. instance->totemrrp_deliver_fn = deliver_fn;
  1667. instance->totemrrp_iface_change_fn = iface_change_fn;
  1668. instance->totemrrp_token_seqid_get = token_seqid_get;
  1669. instance->totemrrp_target_set_completed = target_set_completed;
  1670. instance->totemrrp_msgs_missing = msgs_missing;
  1671. instance->interface_count = totem_config->interface_count;
  1672. instance->net_handles = malloc (sizeof (void *) * totem_config->interface_count);
  1673. instance->context = context;
  1674. instance->poll_handle = poll_handle;
  1675. for (i = 0; i < totem_config->interface_count; i++) {
  1676. struct deliver_fn_context *deliver_fn_context;
  1677. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1678. assert (deliver_fn_context);
  1679. deliver_fn_context->instance = instance;
  1680. deliver_fn_context->context = context;
  1681. deliver_fn_context->iface_no = i;
  1682. instance->deliver_fn_context[i] = (void *)deliver_fn_context;
  1683. totemnet_initialize (
  1684. poll_handle,
  1685. &instance->net_handles[i],
  1686. totem_config,
  1687. stats,
  1688. i,
  1689. (void *)deliver_fn_context,
  1690. rrp_deliver_fn,
  1691. rrp_iface_change_fn,
  1692. rrp_target_set_completed);
  1693. totemnet_net_mtu_adjust (instance->net_handles[i], totem_config);
  1694. }
  1695. *rrp_context = instance;
  1696. return (0);
  1697. error_destroy:
  1698. free (instance);
  1699. return (res);
  1700. }
  1701. void *totemrrp_buffer_alloc (void *rrp_context)
  1702. {
  1703. struct totemrrp_instance *instance = rrp_context;
  1704. assert (instance != NULL);
  1705. return totemnet_buffer_alloc (instance->net_handles[0]);
  1706. }
  1707. void totemrrp_buffer_release (void *rrp_context, void *ptr)
  1708. {
  1709. struct totemrrp_instance *instance = rrp_context;
  1710. assert (instance != NULL);
  1711. totemnet_buffer_release (instance->net_handles[0], ptr);
  1712. }
  1713. int totemrrp_processor_count_set (
  1714. void *rrp_context,
  1715. unsigned int processor_count)
  1716. {
  1717. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1718. instance->rrp_algo->processor_count_set (instance, processor_count);
  1719. instance->processor_count = processor_count;
  1720. return (0);
  1721. }
  1722. int totemrrp_token_target_set (
  1723. void *rrp_context,
  1724. struct totem_ip_address *addr,
  1725. unsigned int iface_no)
  1726. {
  1727. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1728. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1729. return (0);
  1730. }
  1731. int totemrrp_recv_flush (void *rrp_context)
  1732. {
  1733. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1734. instance->rrp_algo->recv_flush (instance);
  1735. return (0);
  1736. }
  1737. int totemrrp_send_flush (void *rrp_context)
  1738. {
  1739. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1740. instance->rrp_algo->send_flush (instance);
  1741. return (0);
  1742. }
  1743. int totemrrp_token_send (
  1744. void *rrp_context,
  1745. const void *msg,
  1746. unsigned int msg_len)
  1747. {
  1748. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1749. instance->rrp_algo->token_send (instance, msg, msg_len);
  1750. return (0);
  1751. }
  1752. int totemrrp_mcast_flush_send (
  1753. void *rrp_context,
  1754. const void *msg,
  1755. unsigned int msg_len)
  1756. {
  1757. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1758. int res = 0;
  1759. // TODO this needs to return the result
  1760. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1761. return (res);
  1762. }
  1763. int totemrrp_mcast_noflush_send (
  1764. void *rrp_context,
  1765. const void *msg,
  1766. unsigned int msg_len)
  1767. {
  1768. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1769. /*
  1770. * merge detects go out through mcast_flush_send so it is safe to
  1771. * flush these messages if we are only one processor. This avoids
  1772. * an encryption/hmac and decryption/hmac
  1773. */
  1774. if (instance->processor_count > 1) {
  1775. // TODO this needs to return the result
  1776. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1777. }
  1778. return (0);
  1779. }
  1780. int totemrrp_iface_check (void *rrp_context)
  1781. {
  1782. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1783. instance->rrp_algo->iface_check (instance);
  1784. return (0);
  1785. }
  1786. int totemrrp_ifaces_get (
  1787. void *rrp_context,
  1788. char ***status,
  1789. unsigned int *iface_count)
  1790. {
  1791. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1792. *status = instance->status;
  1793. if (iface_count) {
  1794. *iface_count = instance->interface_count;
  1795. }
  1796. return (0);
  1797. }
  1798. int totemrrp_crypto_set (
  1799. void *rrp_context,
  1800. const char *cipher_type,
  1801. const char *hash_type)
  1802. {
  1803. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1804. int res;
  1805. res = totemnet_crypto_set(instance->net_handles[0], cipher_type, hash_type);
  1806. return (res);
  1807. }
  1808. /*
  1809. * iface_no indicates the interface number [0, ..., interface_count-1] of the
  1810. * specific ring which will be reenabled. We specify iface_no == interface_count
  1811. * means reenabling all the rings.
  1812. */
  1813. int totemrrp_ring_reenable (
  1814. void *rrp_context,
  1815. unsigned int iface_no)
  1816. {
  1817. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1818. int res = 0;
  1819. unsigned int i;
  1820. instance->rrp_algo->ring_reenable (instance, iface_no);
  1821. if (iface_no == instance->interface_count) {
  1822. for (i = 0; i < instance->interface_count; i++) {
  1823. snprintf (instance->status[i], STATUS_STR_LEN,
  1824. "ring %d active with no faults", i);
  1825. }
  1826. } else {
  1827. snprintf (instance->status[iface_no], STATUS_STR_LEN,
  1828. "ring %d active with no faults", iface_no);
  1829. }
  1830. return (res);
  1831. }
  1832. extern int totemrrp_mcast_recv_empty (
  1833. void *rrp_context)
  1834. {
  1835. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1836. int res;
  1837. res = instance->rrp_algo->mcast_recv_empty (instance);
  1838. return (res);
  1839. }
  1840. int totemrrp_member_add (
  1841. void *rrp_context,
  1842. const struct totem_ip_address *member,
  1843. int iface_no)
  1844. {
  1845. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1846. int res;
  1847. res = instance->rrp_algo->member_add (instance, member, iface_no);
  1848. return (res);
  1849. }
  1850. int totemrrp_member_remove (
  1851. void *rrp_context,
  1852. const struct totem_ip_address *member,
  1853. int iface_no)
  1854. {
  1855. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1856. int res;
  1857. res = instance->rrp_algo->member_remove (instance, member, iface_no);
  1858. return (res);
  1859. }
  1860. void totemrrp_membership_changed (
  1861. void *rrp_context,
  1862. enum totem_configuration_type configuration_type,
  1863. const struct srp_addr *member_list, size_t member_list_entries,
  1864. const struct srp_addr *left_list, size_t left_list_entries,
  1865. const struct srp_addr *joined_list, size_t joined_list_entries,
  1866. const struct memb_ring_id *ring_id)
  1867. {
  1868. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1869. instance->rrp_algo->membership_changed (instance,
  1870. configuration_type,
  1871. member_list, member_list_entries,
  1872. left_list, left_list_entries,
  1873. joined_list, joined_list_entries,
  1874. ring_id);
  1875. }