totemrrp.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392
  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 *rrp_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. int i;
  684. for (i = 0; i < left_list_entries; i++) {
  685. if (left_list->no_addrs < 1 ||
  686. (left_list[i].addr[0].family != AF_INET && left_list[i].addr[0].family != AF_INET6)) {
  687. log_printf(rrp_instance->totemrrp_log_level_error,
  688. "Membership left list contains incorrect address. "
  689. "This is sign of misconfiguration between nodes!");
  690. } else {
  691. totemnet_member_set_active(rrp_instance->net_handles[0],
  692. &left_list[i].addr[0], 0);
  693. }
  694. }
  695. for (i = 0; i < joined_list_entries; i++) {
  696. if (joined_list->no_addrs < 1 ||
  697. (joined_list[i].addr[0].family != AF_INET && joined_list[i].addr[0].family != AF_INET6)) {
  698. log_printf(rrp_instance->totemrrp_log_level_error,
  699. "Membership join list contains incorrect address. "
  700. "This is sign of misconfiguration between nodes!");
  701. } else {
  702. totemnet_member_set_active(rrp_instance->net_handles[0],
  703. &joined_list[i].addr[0], 1);
  704. }
  705. }
  706. }
  707. /*
  708. * Passive Replication Implementation
  709. */
  710. void *passive_instance_initialize (
  711. struct totemrrp_instance *rrp_instance,
  712. int interface_count)
  713. {
  714. struct passive_instance *instance;
  715. int i;
  716. instance = malloc (sizeof (struct passive_instance));
  717. if (instance == 0) {
  718. goto error_exit;
  719. }
  720. memset (instance, 0, sizeof (struct passive_instance));
  721. instance->faulty = malloc (sizeof (int) * interface_count);
  722. if (instance->faulty == 0) {
  723. free (instance);
  724. instance = 0;
  725. goto error_exit;
  726. }
  727. memset (instance->faulty, 0, sizeof (int) * interface_count);
  728. for (i = 0; i < interface_count; i++) {
  729. stats_set_interface_faulty (rrp_instance, i, 0);
  730. }
  731. instance->token_recv_count = malloc (sizeof (int) * interface_count);
  732. if (instance->token_recv_count == 0) {
  733. free (instance->faulty);
  734. free (instance);
  735. instance = 0;
  736. goto error_exit;
  737. }
  738. memset (instance->token_recv_count, 0, sizeof (int) * interface_count);
  739. instance->mcast_recv_count = malloc (sizeof (int) * interface_count);
  740. if (instance->mcast_recv_count == 0) {
  741. free (instance->token_recv_count);
  742. free (instance->faulty);
  743. free (instance);
  744. instance = 0;
  745. goto error_exit;
  746. }
  747. memset (instance->mcast_recv_count, 0, sizeof (int) * interface_count);
  748. error_exit:
  749. return ((void *)instance);
  750. }
  751. static void timer_function_passive_token_expired (void *context)
  752. {
  753. struct passive_instance *passive_instance = (struct passive_instance *)context;
  754. struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  755. rrp_instance->totemrrp_deliver_fn (
  756. passive_instance->totemrrp_context,
  757. passive_instance->token,
  758. passive_instance->token_len);
  759. }
  760. /* TODO
  761. static void timer_function_passive_problem_decrementer (void *context)
  762. {
  763. // struct passive_instance *passive_instance = (struct passive_instance *)context;
  764. // struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  765. }
  766. */
  767. static void passive_timer_expired_token_start (
  768. struct passive_instance *passive_instance)
  769. {
  770. qb_loop_timer_add (
  771. passive_instance->rrp_instance->poll_handle,
  772. QB_LOOP_MED,
  773. passive_instance->rrp_instance->totem_config->rrp_token_expired_timeout*QB_TIME_NS_IN_MSEC,
  774. (void *)passive_instance,
  775. timer_function_passive_token_expired,
  776. &passive_instance->timer_expired_token);
  777. }
  778. static void passive_timer_expired_token_cancel (
  779. struct passive_instance *passive_instance)
  780. {
  781. qb_loop_timer_del (
  782. passive_instance->rrp_instance->poll_handle,
  783. passive_instance->timer_expired_token);
  784. }
  785. /*
  786. static void passive_timer_problem_decrementer_start (
  787. struct passive_instance *passive_instance)
  788. {
  789. qb_loop_timer_add (
  790. QB_LOOP_MED,
  791. passive_instance->rrp_instance->poll_handle,
  792. passive_instance->rrp_instance->totem_config->rrp_problem_count_timeout*QB_TIME_NS_IN_MSEC,
  793. (void *)passive_instance,
  794. timer_function_passive_problem_decrementer,
  795. &passive_instance->timer_problem_decrementer);
  796. }
  797. static void passive_timer_problem_decrementer_cancel (
  798. struct passive_instance *passive_instance)
  799. {
  800. qb_loop_timer_del (
  801. passive_instance->rrp_instance->poll_handle,
  802. passive_instance->timer_problem_decrementer);
  803. }
  804. */
  805. /*
  806. * Monitor function implementation from rrp paper.
  807. * rrp_instance is passive rrp instance, iface_no is interface with received messgae/token and
  808. * is_token_recv_count is boolean variable which donates if message is token (>1) or regular
  809. * message (= 0)
  810. */
  811. static void passive_monitor (
  812. struct totemrrp_instance *rrp_instance,
  813. unsigned int iface_no,
  814. int is_token_recv_count)
  815. {
  816. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  817. unsigned int *recv_count;
  818. unsigned int max;
  819. unsigned int i;
  820. unsigned int min_all, min_active;
  821. unsigned int threshold;
  822. /*
  823. * Monitor for failures
  824. */
  825. if (is_token_recv_count) {
  826. recv_count = passive_instance->token_recv_count;
  827. threshold = rrp_instance->totem_config->rrp_problem_count_threshold;
  828. } else {
  829. recv_count = passive_instance->mcast_recv_count;
  830. threshold = rrp_instance->totem_config->rrp_problem_count_mcast_threshold;
  831. }
  832. recv_count[iface_no] += 1;
  833. max = 0;
  834. for (i = 0; i < rrp_instance->interface_count; i++) {
  835. if (max < recv_count[i]) {
  836. max = recv_count[i];
  837. }
  838. }
  839. /*
  840. * Max is larger than threshold -> start adjusting process
  841. */
  842. if (max > PASSIVE_RECV_COUNT_THRESHOLD) {
  843. min_all = min_active = recv_count[iface_no];
  844. for (i = 0; i < rrp_instance->interface_count; i++) {
  845. if (recv_count[i] < min_all) {
  846. min_all = recv_count[i];
  847. }
  848. if (passive_instance->faulty[i] == 0 &&
  849. recv_count[i] < min_active) {
  850. min_active = recv_count[i];
  851. }
  852. }
  853. if (min_all > 0) {
  854. /*
  855. * There is one or more faulty device with recv_count > 0
  856. */
  857. for (i = 0; i < rrp_instance->interface_count; i++) {
  858. recv_count[i] -= min_all;
  859. }
  860. } else {
  861. /*
  862. * No faulty device with recv_count > 0, adjust only active
  863. * devices
  864. */
  865. for (i = 0; i < rrp_instance->interface_count; i++) {
  866. if (passive_instance->faulty[i] == 0) {
  867. recv_count[i] -= min_active;
  868. }
  869. }
  870. }
  871. /*
  872. * Find again max
  873. */
  874. max = 0;
  875. for (i = 0; i < rrp_instance->interface_count; i++) {
  876. if (max < recv_count[i]) {
  877. max = recv_count[i];
  878. }
  879. }
  880. }
  881. for (i = 0; i < rrp_instance->interface_count; i++) {
  882. if ((passive_instance->faulty[i] == 0) &&
  883. (max - recv_count[i] > threshold)) {
  884. passive_instance->faulty[i] = 1;
  885. qb_loop_timer_add (rrp_instance->poll_handle,
  886. QB_LOOP_MED,
  887. rrp_instance->totem_config->rrp_autorecovery_check_timeout*QB_TIME_NS_IN_MSEC,
  888. rrp_instance->deliver_fn_context[i],
  889. timer_function_test_ring_timeout,
  890. &rrp_instance->timer_active_test_ring_timeout[i]);
  891. stats_set_interface_faulty (rrp_instance, i, passive_instance->faulty[i]);
  892. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  893. "Marking ringid %u interface %s FAULTY",
  894. i,
  895. totemnet_iface_print (rrp_instance->net_handles[i]));
  896. log_printf (
  897. rrp_instance->totemrrp_log_level_error,
  898. "%s",
  899. rrp_instance->status[i]);
  900. }
  901. }
  902. }
  903. static void passive_mcast_recv (
  904. struct totemrrp_instance *rrp_instance,
  905. unsigned int iface_no,
  906. void *context,
  907. const void *msg,
  908. unsigned int msg_len)
  909. {
  910. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  911. rrp_instance->totemrrp_deliver_fn (
  912. context,
  913. msg,
  914. msg_len);
  915. if (rrp_instance->totemrrp_msgs_missing() == 0 &&
  916. passive_instance->timer_expired_token) {
  917. /*
  918. * Delivers the last token
  919. */
  920. rrp_instance->totemrrp_deliver_fn (
  921. passive_instance->totemrrp_context,
  922. passive_instance->token,
  923. passive_instance->token_len);
  924. passive_timer_expired_token_cancel (passive_instance);
  925. }
  926. passive_monitor (rrp_instance, iface_no, 0);
  927. }
  928. static void passive_mcast_flush_send (
  929. struct totemrrp_instance *instance,
  930. const void *msg,
  931. unsigned int msg_len)
  932. {
  933. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  934. int i = 0;
  935. do {
  936. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  937. i++;
  938. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1));
  939. if (i > instance->interface_count) {
  940. /*
  941. * All interfaces are faulty. It's still needed to send mcast
  942. * message to local host so use first interface.
  943. */
  944. passive_instance->msg_xmit_iface = 0;
  945. }
  946. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  947. }
  948. static void passive_mcast_noflush_send (
  949. struct totemrrp_instance *instance,
  950. const void *msg,
  951. unsigned int msg_len)
  952. {
  953. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  954. int i = 0;
  955. do {
  956. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  957. i++;
  958. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1));
  959. if (i > instance->interface_count) {
  960. /*
  961. * All interfaces are faulty. It's still needed to send mcast
  962. * message to local host so use first interface.
  963. */
  964. passive_instance->msg_xmit_iface = 0;
  965. }
  966. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  967. }
  968. static void passive_token_recv (
  969. struct totemrrp_instance *rrp_instance,
  970. unsigned int iface_no,
  971. void *context,
  972. const void *msg,
  973. unsigned int msg_len,
  974. unsigned int token_seq)
  975. {
  976. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  977. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  978. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  979. rrp_instance->totemrrp_deliver_fn (
  980. context,
  981. msg,
  982. msg_len);
  983. } else {
  984. memcpy (passive_instance->token, msg, msg_len);
  985. passive_timer_expired_token_start (passive_instance);
  986. }
  987. passive_monitor (rrp_instance, iface_no, 1);
  988. }
  989. static void passive_token_send (
  990. struct totemrrp_instance *instance,
  991. const void *msg,
  992. unsigned int msg_len)
  993. {
  994. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  995. int i = 0;
  996. do {
  997. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  998. i++;
  999. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->token_xmit_iface] == 1));
  1000. if (i > instance->interface_count) {
  1001. /*
  1002. * All interfaces are faulty. It's still needed to send token
  1003. * message to (potentionally) local host so use first interface.
  1004. */
  1005. passive_instance->msg_xmit_iface = 0;
  1006. }
  1007. totemnet_token_send (
  1008. instance->net_handles[passive_instance->token_xmit_iface],
  1009. msg, msg_len);
  1010. }
  1011. static void passive_recv_flush (struct totemrrp_instance *instance)
  1012. {
  1013. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1014. unsigned int i;
  1015. for (i = 0; i < instance->interface_count; i++) {
  1016. if (rrp_algo_instance->faulty[i] == 0) {
  1017. totemnet_recv_flush (instance->net_handles[i]);
  1018. }
  1019. }
  1020. }
  1021. static void passive_send_flush (struct totemrrp_instance *instance)
  1022. {
  1023. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1024. unsigned int i;
  1025. for (i = 0; i < instance->interface_count; i++) {
  1026. if (rrp_algo_instance->faulty[i] == 0) {
  1027. totemnet_send_flush (instance->net_handles[i]);
  1028. }
  1029. }
  1030. }
  1031. static void passive_iface_check (struct totemrrp_instance *instance)
  1032. {
  1033. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1034. unsigned int i;
  1035. for (i = 0; i < instance->interface_count; i++) {
  1036. if (rrp_algo_instance->faulty[i] == 0) {
  1037. totemnet_iface_check (instance->net_handles[i]);
  1038. }
  1039. }
  1040. }
  1041. static void passive_processor_count_set (
  1042. struct totemrrp_instance *instance,
  1043. unsigned int processor_count)
  1044. {
  1045. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1046. unsigned int i;
  1047. for (i = 0; i < instance->interface_count; i++) {
  1048. if (rrp_algo_instance->faulty[i] == 0) {
  1049. totemnet_processor_count_set (instance->net_handles[i],
  1050. processor_count);
  1051. }
  1052. }
  1053. }
  1054. static void passive_token_target_set (
  1055. struct totemrrp_instance *instance,
  1056. struct totem_ip_address *token_target,
  1057. unsigned int iface_no)
  1058. {
  1059. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1060. }
  1061. static int passive_mcast_recv_empty (
  1062. struct totemrrp_instance *instance)
  1063. {
  1064. int res;
  1065. int msgs_emptied = 0;
  1066. int i;
  1067. for (i = 0; i < instance->interface_count; i++) {
  1068. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1069. if (res == -1) {
  1070. return (-1);
  1071. }
  1072. if (res == 1) {
  1073. msgs_emptied = 1;
  1074. }
  1075. }
  1076. return (msgs_emptied);
  1077. }
  1078. static int passive_member_add (
  1079. struct totemrrp_instance *instance,
  1080. const struct totem_ip_address *member,
  1081. unsigned int iface_no)
  1082. {
  1083. int res;
  1084. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1085. return (res);
  1086. }
  1087. static int passive_member_remove (
  1088. struct totemrrp_instance *instance,
  1089. const struct totem_ip_address *member,
  1090. unsigned int iface_no)
  1091. {
  1092. int res;
  1093. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1094. return (res);
  1095. }
  1096. static void passive_membership_changed (
  1097. struct totemrrp_instance *rrp_instance,
  1098. enum totem_configuration_type configuration_type,
  1099. const struct srp_addr *member_list, size_t member_list_entries,
  1100. const struct srp_addr *left_list, size_t left_list_entries,
  1101. const struct srp_addr *joined_list, size_t joined_list_entries,
  1102. const struct memb_ring_id *ring_id)
  1103. {
  1104. int i;
  1105. int interface;
  1106. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1107. for (i = 0; i < left_list_entries; i++) {
  1108. if (left_list->no_addrs < interface + 1 ||
  1109. (left_list[i].addr[interface].family != AF_INET &&
  1110. left_list[i].addr[interface].family != AF_INET6)) {
  1111. log_printf(rrp_instance->totemrrp_log_level_error,
  1112. "Membership left list contains incorrect address. "
  1113. "This is sign of misconfiguration between nodes!");
  1114. } else {
  1115. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1116. &left_list[i].addr[interface], 0);
  1117. }
  1118. }
  1119. for (i = 0; i < joined_list_entries; i++) {
  1120. if (joined_list->no_addrs < interface + 1 ||
  1121. (joined_list[i].addr[interface].family != AF_INET &&
  1122. joined_list[i].addr[interface].family != AF_INET6)) {
  1123. log_printf(rrp_instance->totemrrp_log_level_error,
  1124. "Membership join list contains incorrect address. "
  1125. "This is sign of misconfiguration between nodes!");
  1126. } else {
  1127. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1128. &joined_list[i].addr[interface], 1);
  1129. }
  1130. }
  1131. }
  1132. }
  1133. static void passive_ring_reenable (
  1134. struct totemrrp_instance *instance,
  1135. unsigned int iface_no)
  1136. {
  1137. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1138. int i;
  1139. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  1140. instance->interface_count);
  1141. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  1142. instance->interface_count);
  1143. if (iface_no == instance->interface_count) {
  1144. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1145. instance->interface_count);
  1146. for (i = 0; i < instance->interface_count; i++) {
  1147. stats_set_interface_faulty (instance, i, 0);
  1148. }
  1149. } else {
  1150. rrp_algo_instance->faulty[iface_no] = 0;
  1151. stats_set_interface_faulty (instance, iface_no, 0);
  1152. }
  1153. }
  1154. /*
  1155. * Active Replication Implementation
  1156. */
  1157. void *active_instance_initialize (
  1158. struct totemrrp_instance *rrp_instance,
  1159. int interface_count)
  1160. {
  1161. struct active_instance *instance;
  1162. int i;
  1163. instance = malloc (sizeof (struct active_instance));
  1164. if (instance == 0) {
  1165. goto error_exit;
  1166. }
  1167. memset (instance, 0, sizeof (struct active_instance));
  1168. instance->faulty = malloc (sizeof (int) * interface_count);
  1169. if (instance->faulty == 0) {
  1170. free (instance);
  1171. instance = 0;
  1172. goto error_exit;
  1173. }
  1174. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  1175. for (i = 0; i < interface_count; i++) {
  1176. stats_set_interface_faulty (rrp_instance, i, 0);
  1177. }
  1178. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  1179. if (instance->last_token_recv == 0) {
  1180. free (instance->faulty);
  1181. free (instance);
  1182. instance = 0;
  1183. goto error_exit;
  1184. }
  1185. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  1186. instance->counter_problems = malloc (sizeof (int) * interface_count);
  1187. if (instance->counter_problems == 0) {
  1188. free (instance->last_token_recv);
  1189. free (instance->faulty);
  1190. free (instance);
  1191. instance = 0;
  1192. goto error_exit;
  1193. }
  1194. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  1195. instance->timer_expired_token = 0;
  1196. instance->timer_problem_decrementer = 0;
  1197. instance->rrp_instance = rrp_instance;
  1198. instance->last_token_seq = ARR_SEQNO_START_TOKEN - 1;
  1199. error_exit:
  1200. return ((void *)instance);
  1201. }
  1202. static void timer_function_active_problem_decrementer (void *context)
  1203. {
  1204. struct active_instance *active_instance = (struct active_instance *)context;
  1205. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1206. unsigned int problem_found = 0;
  1207. unsigned int i;
  1208. for (i = 0; i < rrp_instance->interface_count; i++) {
  1209. if (active_instance->counter_problems[i] > 0) {
  1210. problem_found = 1;
  1211. active_instance->counter_problems[i] -= 1;
  1212. if (active_instance->counter_problems[i] == 0) {
  1213. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1214. "ring %d active with no faults", i);
  1215. } else {
  1216. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1217. "Decrementing problem counter for iface %s to [%d of %d]",
  1218. totemnet_iface_print (rrp_instance->net_handles[i]),
  1219. active_instance->counter_problems[i],
  1220. rrp_instance->totem_config->rrp_problem_count_threshold);
  1221. }
  1222. log_printf (
  1223. rrp_instance->totemrrp_log_level_warning,
  1224. "%s",
  1225. rrp_instance->status[i]);
  1226. }
  1227. }
  1228. if (problem_found) {
  1229. active_timer_problem_decrementer_start (active_instance);
  1230. } else {
  1231. active_instance->timer_problem_decrementer = 0;
  1232. }
  1233. }
  1234. static void timer_function_active_token_expired (void *context)
  1235. {
  1236. struct active_instance *active_instance = (struct active_instance *)context;
  1237. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1238. unsigned int i;
  1239. for (i = 0; i < rrp_instance->interface_count; i++) {
  1240. if (active_instance->last_token_recv[i] == 0) {
  1241. active_instance->counter_problems[i] += 1;
  1242. if (active_instance->timer_problem_decrementer == 0) {
  1243. active_timer_problem_decrementer_start (active_instance);
  1244. }
  1245. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1246. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  1247. active_instance->last_token_seq,
  1248. totemnet_iface_print (rrp_instance->net_handles[i]),
  1249. active_instance->counter_problems[i],
  1250. rrp_instance->totem_config->rrp_problem_count_threshold);
  1251. log_printf (
  1252. rrp_instance->totemrrp_log_level_warning,
  1253. "%s",
  1254. rrp_instance->status[i]);
  1255. }
  1256. }
  1257. for (i = 0; i < rrp_instance->interface_count; i++) {
  1258. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold &&
  1259. active_instance->faulty[i] == 0) {
  1260. active_instance->faulty[i] = 1;
  1261. qb_loop_timer_add (rrp_instance->poll_handle,
  1262. QB_LOOP_MED,
  1263. rrp_instance->totem_config->rrp_autorecovery_check_timeout*QB_TIME_NS_IN_MSEC,
  1264. rrp_instance->deliver_fn_context[i],
  1265. timer_function_test_ring_timeout,
  1266. &rrp_instance->timer_active_test_ring_timeout[i]);
  1267. stats_set_interface_faulty (rrp_instance, i, active_instance->faulty[i]);
  1268. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1269. "Marking seqid %d ringid %u interface %s FAULTY",
  1270. active_instance->last_token_seq,
  1271. i,
  1272. totemnet_iface_print (rrp_instance->net_handles[i]));
  1273. log_printf (
  1274. rrp_instance->totemrrp_log_level_error,
  1275. "%s",
  1276. rrp_instance->status[i]);
  1277. active_timer_problem_decrementer_cancel (active_instance);
  1278. }
  1279. }
  1280. rrp_instance->totemrrp_deliver_fn (
  1281. active_instance->totemrrp_context,
  1282. active_instance->token,
  1283. active_instance->token_len);
  1284. }
  1285. static void active_timer_expired_token_start (
  1286. struct active_instance *active_instance)
  1287. {
  1288. qb_loop_timer_add (
  1289. active_instance->rrp_instance->poll_handle,
  1290. QB_LOOP_MED,
  1291. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout*QB_TIME_NS_IN_MSEC,
  1292. (void *)active_instance,
  1293. timer_function_active_token_expired,
  1294. &active_instance->timer_expired_token);
  1295. }
  1296. static void active_timer_expired_token_cancel (
  1297. struct active_instance *active_instance)
  1298. {
  1299. qb_loop_timer_del (
  1300. active_instance->rrp_instance->poll_handle,
  1301. active_instance->timer_expired_token);
  1302. }
  1303. static void active_timer_problem_decrementer_start (
  1304. struct active_instance *active_instance)
  1305. {
  1306. qb_loop_timer_add (
  1307. active_instance->rrp_instance->poll_handle,
  1308. QB_LOOP_MED,
  1309. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout*QB_TIME_NS_IN_MSEC,
  1310. (void *)active_instance,
  1311. timer_function_active_problem_decrementer,
  1312. &active_instance->timer_problem_decrementer);
  1313. }
  1314. static void active_timer_problem_decrementer_cancel (
  1315. struct active_instance *active_instance)
  1316. {
  1317. qb_loop_timer_del (
  1318. active_instance->rrp_instance->poll_handle,
  1319. active_instance->timer_problem_decrementer);
  1320. active_instance->timer_problem_decrementer = 0;
  1321. }
  1322. /*
  1323. * active replication
  1324. */
  1325. static void active_mcast_recv (
  1326. struct totemrrp_instance *instance,
  1327. unsigned int iface_no,
  1328. void *context,
  1329. const void *msg,
  1330. unsigned int msg_len)
  1331. {
  1332. instance->totemrrp_deliver_fn (
  1333. context,
  1334. msg,
  1335. msg_len);
  1336. }
  1337. static void active_mcast_flush_send (
  1338. struct totemrrp_instance *instance,
  1339. const void *msg,
  1340. unsigned int msg_len)
  1341. {
  1342. int i;
  1343. int msg_sent;
  1344. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1345. msg_sent = 0;
  1346. for (i = 0; i < instance->interface_count; i++) {
  1347. if (rrp_algo_instance->faulty[i] == 0) {
  1348. msg_sent = 1;
  1349. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  1350. }
  1351. }
  1352. if (!msg_sent) {
  1353. /*
  1354. * All interfaces are faulty. It's still needed to send mcast
  1355. * message to local host so use first interface.
  1356. */
  1357. totemnet_mcast_flush_send (instance->net_handles[0], msg, msg_len);
  1358. }
  1359. }
  1360. static void active_mcast_noflush_send (
  1361. struct totemrrp_instance *instance,
  1362. const void *msg,
  1363. unsigned int msg_len)
  1364. {
  1365. int i;
  1366. int msg_sent;
  1367. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1368. msg_sent = 0;
  1369. for (i = 0; i < instance->interface_count; i++) {
  1370. if (rrp_algo_instance->faulty[i] == 0) {
  1371. msg_sent = 1;
  1372. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  1373. }
  1374. }
  1375. if (!msg_sent) {
  1376. /*
  1377. * All interfaces are faulty. It's still needed to send mcast
  1378. * message to local host so use first interface.
  1379. */
  1380. totemnet_mcast_noflush_send (instance->net_handles[0], msg, msg_len);
  1381. }
  1382. }
  1383. static void active_token_recv (
  1384. struct totemrrp_instance *rrp_instance,
  1385. unsigned int iface_no,
  1386. void *context,
  1387. const void *msg,
  1388. unsigned int msg_len,
  1389. unsigned int token_seq)
  1390. {
  1391. int i;
  1392. struct active_instance *active_instance = (struct active_instance *)rrp_instance->rrp_algo_instance;
  1393. active_instance->totemrrp_context = context;
  1394. if (sq_lt_compare (active_instance->last_token_seq, token_seq)) {
  1395. memcpy (active_instance->token, msg, msg_len);
  1396. active_instance->token_len = msg_len;
  1397. for (i = 0; i < rrp_instance->interface_count; i++) {
  1398. active_instance->last_token_recv[i] = 0;
  1399. }
  1400. active_instance->last_token_recv[iface_no] = 1;
  1401. active_timer_expired_token_start (active_instance);
  1402. }
  1403. /*
  1404. * This doesn't follow spec because the spec assumes we will know
  1405. * when token resets occur.
  1406. */
  1407. active_instance->last_token_seq = token_seq;
  1408. if (token_seq == active_instance->last_token_seq) {
  1409. active_instance->last_token_recv[iface_no] = 1;
  1410. for (i = 0; i < rrp_instance->interface_count; i++) {
  1411. if ((active_instance->last_token_recv[i] == 0) &&
  1412. active_instance->faulty[i] == 0) {
  1413. return; /* don't deliver token */
  1414. }
  1415. }
  1416. active_timer_expired_token_cancel (active_instance);
  1417. rrp_instance->totemrrp_deliver_fn (
  1418. context,
  1419. msg,
  1420. msg_len);
  1421. }
  1422. }
  1423. static void active_token_send (
  1424. struct totemrrp_instance *instance,
  1425. const void *msg,
  1426. unsigned int msg_len)
  1427. {
  1428. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1429. int i;
  1430. int msg_sent;
  1431. msg_sent = 0;
  1432. for (i = 0; i < instance->interface_count; i++) {
  1433. if (rrp_algo_instance->faulty[i] == 0) {
  1434. msg_sent = 1;
  1435. totemnet_token_send (
  1436. instance->net_handles[i],
  1437. msg, msg_len);
  1438. }
  1439. }
  1440. if (!msg_sent) {
  1441. /*
  1442. * All interfaces are faulty. It's still needed to send token
  1443. * message to (potentionally) local host so use first interface.
  1444. */
  1445. totemnet_token_send (
  1446. instance->net_handles[0],
  1447. msg, msg_len);
  1448. }
  1449. }
  1450. static void active_recv_flush (struct totemrrp_instance *instance)
  1451. {
  1452. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1453. unsigned int i;
  1454. for (i = 0; i < instance->interface_count; i++) {
  1455. if (rrp_algo_instance->faulty[i] == 0) {
  1456. totemnet_recv_flush (instance->net_handles[i]);
  1457. }
  1458. }
  1459. }
  1460. static void active_send_flush (struct totemrrp_instance *instance)
  1461. {
  1462. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1463. unsigned int i;
  1464. for (i = 0; i < instance->interface_count; i++) {
  1465. if (rrp_algo_instance->faulty[i] == 0) {
  1466. totemnet_send_flush (instance->net_handles[i]);
  1467. }
  1468. }
  1469. }
  1470. static int active_member_add (
  1471. struct totemrrp_instance *instance,
  1472. const struct totem_ip_address *member,
  1473. unsigned int iface_no)
  1474. {
  1475. int res;
  1476. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1477. return (res);
  1478. }
  1479. static int active_member_remove (
  1480. struct totemrrp_instance *instance,
  1481. const struct totem_ip_address *member,
  1482. unsigned int iface_no)
  1483. {
  1484. int res;
  1485. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1486. return (res);
  1487. }
  1488. static void active_membership_changed (
  1489. struct totemrrp_instance *rrp_instance,
  1490. enum totem_configuration_type configuration_type,
  1491. const struct srp_addr *member_list, size_t member_list_entries,
  1492. const struct srp_addr *left_list, size_t left_list_entries,
  1493. const struct srp_addr *joined_list, size_t joined_list_entries,
  1494. const struct memb_ring_id *ring_id)
  1495. {
  1496. int i;
  1497. int interface;
  1498. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1499. for (i = 0; i < left_list_entries; i++) {
  1500. if (left_list->no_addrs < interface + 1 ||
  1501. (left_list[i].addr[interface].family != AF_INET &&
  1502. left_list[i].addr[interface].family != AF_INET6)) {
  1503. log_printf(rrp_instance->totemrrp_log_level_error,
  1504. "Membership left list contains incorrect address. "
  1505. "This is sign of misconfiguration between nodes!");
  1506. } else {
  1507. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1508. &left_list[i].addr[interface], 0);
  1509. }
  1510. }
  1511. for (i = 0; i < joined_list_entries; i++) {
  1512. if (joined_list->no_addrs < interface + 1 ||
  1513. (joined_list[i].addr[interface].family != AF_INET &&
  1514. joined_list[i].addr[interface].family != AF_INET6)) {
  1515. log_printf(rrp_instance->totemrrp_log_level_error,
  1516. "Membership join list contains incorrect address. "
  1517. "This is sign of misconfiguration between nodes!");
  1518. } else {
  1519. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1520. &joined_list[i].addr[interface], 1);
  1521. }
  1522. }
  1523. }
  1524. }
  1525. static void active_iface_check (struct totemrrp_instance *instance)
  1526. {
  1527. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1528. unsigned int i;
  1529. for (i = 0; i < instance->interface_count; i++) {
  1530. if (rrp_algo_instance->faulty[i] == 0) {
  1531. totemnet_iface_check (instance->net_handles[i]);
  1532. }
  1533. }
  1534. }
  1535. static void active_processor_count_set (
  1536. struct totemrrp_instance *instance,
  1537. unsigned int processor_count)
  1538. {
  1539. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1540. unsigned int i;
  1541. for (i = 0; i < instance->interface_count; i++) {
  1542. if (rrp_algo_instance->faulty[i] == 0) {
  1543. totemnet_processor_count_set (instance->net_handles[i],
  1544. processor_count);
  1545. }
  1546. }
  1547. }
  1548. static void active_token_target_set (
  1549. struct totemrrp_instance *instance,
  1550. struct totem_ip_address *token_target,
  1551. unsigned int iface_no)
  1552. {
  1553. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1554. }
  1555. static int active_mcast_recv_empty (
  1556. struct totemrrp_instance *instance)
  1557. {
  1558. int res;
  1559. int msgs_emptied = 0;
  1560. int i;
  1561. for (i = 0; i < instance->interface_count; i++) {
  1562. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1563. if (res == -1) {
  1564. return (-1);
  1565. }
  1566. if (res == 1) {
  1567. msgs_emptied = 1;
  1568. }
  1569. }
  1570. return (msgs_emptied);
  1571. }
  1572. static void active_ring_reenable (
  1573. struct totemrrp_instance *instance,
  1574. unsigned int iface_no)
  1575. {
  1576. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1577. int i;
  1578. if (iface_no == instance->interface_count) {
  1579. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1580. instance->interface_count);
  1581. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1582. instance->interface_count);
  1583. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1584. instance->interface_count);
  1585. for (i = 0; i < instance->interface_count; i++) {
  1586. stats_set_interface_faulty (instance, i, 0);
  1587. }
  1588. } else {
  1589. rrp_algo_instance->last_token_recv[iface_no] = 0;
  1590. rrp_algo_instance->faulty[iface_no] = 0;
  1591. rrp_algo_instance->counter_problems[iface_no] = 0;
  1592. stats_set_interface_faulty (instance, iface_no, 0);
  1593. }
  1594. }
  1595. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1596. {
  1597. memset (instance, 0, sizeof (struct totemrrp_instance));
  1598. }
  1599. static int totemrrp_algorithm_set (
  1600. struct totem_config *totem_config,
  1601. struct totemrrp_instance *instance)
  1602. {
  1603. unsigned int res = -1;
  1604. unsigned int i;
  1605. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1606. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1607. instance->rrp_algo = rrp_algos[i];
  1608. if (rrp_algos[i]->initialize) {
  1609. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1610. instance,
  1611. totem_config->interface_count);
  1612. }
  1613. res = 0;
  1614. break;
  1615. }
  1616. }
  1617. for (i = 0; i < totem_config->interface_count; i++) {
  1618. instance->status[i] = malloc (STATUS_STR_LEN+1);
  1619. snprintf (instance->status[i], STATUS_STR_LEN,
  1620. "ring %d active with no faults", i);
  1621. }
  1622. return (res);
  1623. }
  1624. void rrp_deliver_fn (
  1625. void *context,
  1626. const void *msg,
  1627. unsigned int msg_len)
  1628. {
  1629. unsigned int token_seqid;
  1630. unsigned int token_is;
  1631. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1632. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  1633. const struct message_header *hdr = msg;
  1634. struct message_header tmp_msg, activate_msg;
  1635. memset(&tmp_msg, 0, sizeof(struct message_header));
  1636. memset(&activate_msg, 0, sizeof(struct message_header));
  1637. rrp_instance->totemrrp_token_seqid_get (
  1638. msg,
  1639. &token_seqid,
  1640. &token_is);
  1641. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVE) {
  1642. log_printf (
  1643. rrp_instance->totemrrp_log_level_debug,
  1644. "received message requesting test of ring now active");
  1645. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1646. test_active_msg_endian_convert(hdr, &tmp_msg);
  1647. hdr = &tmp_msg;
  1648. }
  1649. if (hdr->nodeid_activator == rrp_instance->my_nodeid) {
  1650. /*
  1651. * Send an activate message
  1652. */
  1653. activate_msg.type = MESSAGE_TYPE_RING_TEST_ACTIVATE;
  1654. activate_msg.endian_detector = ENDIAN_LOCAL;
  1655. activate_msg.ring_number = hdr->ring_number;
  1656. activate_msg.nodeid_activator = rrp_instance->my_nodeid;
  1657. totemnet_token_send (
  1658. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1659. &activate_msg, sizeof (struct message_header));
  1660. } else {
  1661. /*
  1662. * Send a ring test message
  1663. */
  1664. totemnet_token_send (
  1665. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1666. msg, msg_len);
  1667. }
  1668. } else
  1669. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVATE) {
  1670. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1671. test_active_msg_endian_convert(hdr, &tmp_msg);
  1672. hdr = &tmp_msg;
  1673. }
  1674. log_printf (
  1675. rrp_instance->totemrrp_log_level_debug,
  1676. "Received ring test activate message for ring %d sent by node %u",
  1677. hdr->ring_number,
  1678. hdr->nodeid_activator);
  1679. if (rrp_instance->stats.faulty[deliver_fn_context->iface_no]) {
  1680. log_printf (rrp_instance->totemrrp_log_level_notice,
  1681. "Automatically recovered ring %d", hdr->ring_number);
  1682. }
  1683. totemrrp_ring_reenable (rrp_instance, deliver_fn_context->iface_no);
  1684. if (hdr->nodeid_activator != rrp_instance->my_nodeid) {
  1685. totemnet_token_send (
  1686. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1687. msg, msg_len);
  1688. }
  1689. } else
  1690. if (token_is) {
  1691. /*
  1692. * Deliver to the token receiver for this rrp algorithm
  1693. */
  1694. rrp_instance->rrp_algo->token_recv (
  1695. rrp_instance,
  1696. deliver_fn_context->iface_no,
  1697. deliver_fn_context->context,
  1698. msg,
  1699. msg_len,
  1700. token_seqid);
  1701. } else {
  1702. /*
  1703. * Deliver to the mcast receiver for this rrp algorithm
  1704. */
  1705. rrp_instance->rrp_algo->mcast_recv (
  1706. rrp_instance,
  1707. deliver_fn_context->iface_no,
  1708. deliver_fn_context->context,
  1709. msg,
  1710. msg_len);
  1711. }
  1712. }
  1713. void rrp_iface_change_fn (
  1714. void *context,
  1715. const struct totem_ip_address *iface_addr)
  1716. {
  1717. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1718. deliver_fn_context->instance->my_nodeid = iface_addr->nodeid;
  1719. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1720. deliver_fn_context->context,
  1721. iface_addr,
  1722. deliver_fn_context->iface_no);
  1723. }
  1724. int totemrrp_finalize (
  1725. void *rrp_context)
  1726. {
  1727. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1728. int i;
  1729. for (i = 0; i < instance->interface_count; i++) {
  1730. totemnet_finalize (instance->net_handles[i]);
  1731. }
  1732. free (instance->net_handles);
  1733. free (instance);
  1734. return (0);
  1735. }
  1736. static void rrp_target_set_completed (void *context)
  1737. {
  1738. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1739. deliver_fn_context->instance->totemrrp_target_set_completed (deliver_fn_context->context);
  1740. }
  1741. /*
  1742. * Totem Redundant Ring interface
  1743. * depends on poll abstraction, POSIX, IPV4
  1744. */
  1745. /*
  1746. * Create an instance
  1747. */
  1748. int totemrrp_initialize (
  1749. qb_loop_t *poll_handle,
  1750. void **rrp_context,
  1751. struct totem_config *totem_config,
  1752. totemsrp_stats_t *stats,
  1753. void *context,
  1754. void (*deliver_fn) (
  1755. void *context,
  1756. const void *msg,
  1757. unsigned int msg_len),
  1758. void (*iface_change_fn) (
  1759. void *context,
  1760. const struct totem_ip_address *iface_addr,
  1761. unsigned int iface_no),
  1762. void (*token_seqid_get) (
  1763. const void *msg,
  1764. unsigned int *seqid,
  1765. unsigned int *token_is),
  1766. unsigned int (*msgs_missing) (void),
  1767. void (*target_set_completed) (void *context))
  1768. {
  1769. struct totemrrp_instance *instance;
  1770. unsigned int res;
  1771. int i;
  1772. instance = malloc (sizeof (struct totemrrp_instance));
  1773. if (instance == 0) {
  1774. return (-1);
  1775. }
  1776. totemrrp_instance_initialize (instance);
  1777. instance->totem_config = totem_config;
  1778. stats->rrp = &instance->stats;
  1779. instance->stats.interface_count = totem_config->interface_count;
  1780. instance->stats.faulty = calloc(instance->stats.interface_count, sizeof(uint8_t));
  1781. res = totemrrp_algorithm_set (
  1782. instance->totem_config,
  1783. instance);
  1784. if (res == -1) {
  1785. goto error_destroy;
  1786. }
  1787. /*
  1788. * Configure logging
  1789. */
  1790. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1791. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1792. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1793. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1794. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1795. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1796. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1797. instance->interfaces = totem_config->interfaces;
  1798. instance->poll_handle = poll_handle;
  1799. instance->totemrrp_deliver_fn = deliver_fn;
  1800. instance->totemrrp_iface_change_fn = iface_change_fn;
  1801. instance->totemrrp_token_seqid_get = token_seqid_get;
  1802. instance->totemrrp_target_set_completed = target_set_completed;
  1803. instance->totemrrp_msgs_missing = msgs_missing;
  1804. instance->interface_count = totem_config->interface_count;
  1805. instance->net_handles = malloc (sizeof (void *) * totem_config->interface_count);
  1806. instance->context = context;
  1807. instance->poll_handle = poll_handle;
  1808. for (i = 0; i < totem_config->interface_count; i++) {
  1809. struct deliver_fn_context *deliver_fn_context;
  1810. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1811. assert (deliver_fn_context);
  1812. deliver_fn_context->instance = instance;
  1813. deliver_fn_context->context = context;
  1814. deliver_fn_context->iface_no = i;
  1815. instance->deliver_fn_context[i] = (void *)deliver_fn_context;
  1816. totemnet_initialize (
  1817. poll_handle,
  1818. &instance->net_handles[i],
  1819. totem_config,
  1820. stats,
  1821. i,
  1822. (void *)deliver_fn_context,
  1823. rrp_deliver_fn,
  1824. rrp_iface_change_fn,
  1825. rrp_target_set_completed);
  1826. totemnet_net_mtu_adjust (instance->net_handles[i], totem_config);
  1827. }
  1828. *rrp_context = instance;
  1829. return (0);
  1830. error_destroy:
  1831. free (instance);
  1832. return (res);
  1833. }
  1834. void *totemrrp_buffer_alloc (void *rrp_context)
  1835. {
  1836. struct totemrrp_instance *instance = rrp_context;
  1837. assert (instance != NULL);
  1838. return totemnet_buffer_alloc (instance->net_handles[0]);
  1839. }
  1840. void totemrrp_buffer_release (void *rrp_context, void *ptr)
  1841. {
  1842. struct totemrrp_instance *instance = rrp_context;
  1843. assert (instance != NULL);
  1844. totemnet_buffer_release (instance->net_handles[0], ptr);
  1845. }
  1846. int totemrrp_processor_count_set (
  1847. void *rrp_context,
  1848. unsigned int processor_count)
  1849. {
  1850. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1851. instance->rrp_algo->processor_count_set (instance, processor_count);
  1852. instance->processor_count = processor_count;
  1853. return (0);
  1854. }
  1855. int totemrrp_token_target_set (
  1856. void *rrp_context,
  1857. struct totem_ip_address *addr,
  1858. unsigned int iface_no)
  1859. {
  1860. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1861. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1862. return (0);
  1863. }
  1864. int totemrrp_recv_flush (void *rrp_context)
  1865. {
  1866. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1867. instance->rrp_algo->recv_flush (instance);
  1868. return (0);
  1869. }
  1870. int totemrrp_send_flush (void *rrp_context)
  1871. {
  1872. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1873. instance->rrp_algo->send_flush (instance);
  1874. return (0);
  1875. }
  1876. int totemrrp_token_send (
  1877. void *rrp_context,
  1878. const void *msg,
  1879. unsigned int msg_len)
  1880. {
  1881. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1882. instance->rrp_algo->token_send (instance, msg, msg_len);
  1883. return (0);
  1884. }
  1885. int totemrrp_mcast_flush_send (
  1886. void *rrp_context,
  1887. const void *msg,
  1888. unsigned int msg_len)
  1889. {
  1890. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1891. int res = 0;
  1892. // TODO this needs to return the result
  1893. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1894. return (res);
  1895. }
  1896. int totemrrp_mcast_noflush_send (
  1897. void *rrp_context,
  1898. const void *msg,
  1899. unsigned int msg_len)
  1900. {
  1901. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1902. /*
  1903. * merge detects go out through mcast_flush_send so it is safe to
  1904. * flush these messages if we are only one processor. This avoids
  1905. * an encryption/hmac and decryption/hmac
  1906. */
  1907. if (instance->processor_count > 1) {
  1908. // TODO this needs to return the result
  1909. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1910. }
  1911. return (0);
  1912. }
  1913. int totemrrp_iface_check (void *rrp_context)
  1914. {
  1915. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1916. instance->rrp_algo->iface_check (instance);
  1917. return (0);
  1918. }
  1919. int totemrrp_ifaces_get (
  1920. void *rrp_context,
  1921. char ***status,
  1922. unsigned int *iface_count)
  1923. {
  1924. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1925. *status = instance->status;
  1926. if (iface_count) {
  1927. *iface_count = instance->interface_count;
  1928. }
  1929. return (0);
  1930. }
  1931. int totemrrp_crypto_set (
  1932. void *rrp_context,
  1933. const char *cipher_type,
  1934. const char *hash_type)
  1935. {
  1936. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1937. int res;
  1938. res = totemnet_crypto_set(instance->net_handles[0], cipher_type, hash_type);
  1939. return (res);
  1940. }
  1941. /*
  1942. * iface_no indicates the interface number [0, ..., interface_count-1] of the
  1943. * specific ring which will be reenabled. We specify iface_no == interface_count
  1944. * means reenabling all the rings.
  1945. */
  1946. int totemrrp_ring_reenable (
  1947. void *rrp_context,
  1948. unsigned int iface_no)
  1949. {
  1950. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1951. int res = 0;
  1952. unsigned int i;
  1953. instance->rrp_algo->ring_reenable (instance, iface_no);
  1954. if (iface_no == instance->interface_count) {
  1955. for (i = 0; i < instance->interface_count; i++) {
  1956. snprintf (instance->status[i], STATUS_STR_LEN,
  1957. "ring %d active with no faults", i);
  1958. }
  1959. } else {
  1960. snprintf (instance->status[iface_no], STATUS_STR_LEN,
  1961. "ring %d active with no faults", iface_no);
  1962. }
  1963. return (res);
  1964. }
  1965. extern int totemrrp_mcast_recv_empty (
  1966. void *rrp_context)
  1967. {
  1968. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1969. int res;
  1970. res = instance->rrp_algo->mcast_recv_empty (instance);
  1971. return (res);
  1972. }
  1973. int totemrrp_member_add (
  1974. void *rrp_context,
  1975. const struct totem_ip_address *member,
  1976. int iface_no)
  1977. {
  1978. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1979. int res;
  1980. res = instance->rrp_algo->member_add (instance, member, iface_no);
  1981. return (res);
  1982. }
  1983. int totemrrp_member_remove (
  1984. void *rrp_context,
  1985. const struct totem_ip_address *member,
  1986. int iface_no)
  1987. {
  1988. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1989. int res;
  1990. res = instance->rrp_algo->member_remove (instance, member, iface_no);
  1991. return (res);
  1992. }
  1993. void totemrrp_membership_changed (
  1994. void *rrp_context,
  1995. enum totem_configuration_type configuration_type,
  1996. const struct srp_addr *member_list, size_t member_list_entries,
  1997. const struct srp_addr *left_list, size_t left_list_entries,
  1998. const struct srp_addr *joined_list, size_t joined_list_entries,
  1999. const struct memb_ring_id *ring_id)
  2000. {
  2001. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  2002. instance->rrp_algo->membership_changed (instance,
  2003. configuration_type,
  2004. member_list, member_list_entries,
  2005. left_list, left_list_entries,
  2006. joined_list, joined_list_entries,
  2007. ring_id);
  2008. }