4
0

totemrrp.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335
  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 then 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. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  941. }
  942. }
  943. static void passive_mcast_noflush_send (
  944. struct totemrrp_instance *instance,
  945. const void *msg,
  946. unsigned int msg_len)
  947. {
  948. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  949. int i = 0;
  950. do {
  951. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  952. i++;
  953. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1));
  954. if (i <= instance->interface_count) {
  955. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  956. }
  957. }
  958. static void passive_token_recv (
  959. struct totemrrp_instance *rrp_instance,
  960. unsigned int iface_no,
  961. void *context,
  962. const void *msg,
  963. unsigned int msg_len,
  964. unsigned int token_seq)
  965. {
  966. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  967. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  968. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  969. rrp_instance->totemrrp_deliver_fn (
  970. context,
  971. msg,
  972. msg_len);
  973. } else {
  974. memcpy (passive_instance->token, msg, msg_len);
  975. passive_timer_expired_token_start (passive_instance);
  976. }
  977. passive_monitor (rrp_instance, iface_no, 1);
  978. }
  979. static void passive_token_send (
  980. struct totemrrp_instance *instance,
  981. const void *msg,
  982. unsigned int msg_len)
  983. {
  984. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  985. int i = 0;
  986. do {
  987. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  988. i++;
  989. } while ((i <= instance->interface_count) && (passive_instance->faulty[passive_instance->token_xmit_iface] == 1));
  990. if (i <= instance->interface_count) {
  991. totemnet_token_send (
  992. instance->net_handles[passive_instance->token_xmit_iface],
  993. msg, msg_len);
  994. }
  995. }
  996. static void passive_recv_flush (struct totemrrp_instance *instance)
  997. {
  998. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  999. unsigned int i;
  1000. for (i = 0; i < instance->interface_count; i++) {
  1001. if (rrp_algo_instance->faulty[i] == 0) {
  1002. totemnet_recv_flush (instance->net_handles[i]);
  1003. }
  1004. }
  1005. }
  1006. static void passive_send_flush (struct totemrrp_instance *instance)
  1007. {
  1008. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1009. unsigned int i;
  1010. for (i = 0; i < instance->interface_count; i++) {
  1011. if (rrp_algo_instance->faulty[i] == 0) {
  1012. totemnet_send_flush (instance->net_handles[i]);
  1013. }
  1014. }
  1015. }
  1016. static void passive_iface_check (struct totemrrp_instance *instance)
  1017. {
  1018. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1019. unsigned int i;
  1020. for (i = 0; i < instance->interface_count; i++) {
  1021. if (rrp_algo_instance->faulty[i] == 0) {
  1022. totemnet_iface_check (instance->net_handles[i]);
  1023. }
  1024. }
  1025. }
  1026. static void passive_processor_count_set (
  1027. struct totemrrp_instance *instance,
  1028. unsigned int processor_count)
  1029. {
  1030. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1031. unsigned int i;
  1032. for (i = 0; i < instance->interface_count; i++) {
  1033. if (rrp_algo_instance->faulty[i] == 0) {
  1034. totemnet_processor_count_set (instance->net_handles[i],
  1035. processor_count);
  1036. }
  1037. }
  1038. }
  1039. static void passive_token_target_set (
  1040. struct totemrrp_instance *instance,
  1041. struct totem_ip_address *token_target,
  1042. unsigned int iface_no)
  1043. {
  1044. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1045. }
  1046. static int passive_mcast_recv_empty (
  1047. struct totemrrp_instance *instance)
  1048. {
  1049. int res;
  1050. int msgs_emptied = 0;
  1051. int i;
  1052. for (i = 0; i < instance->interface_count; i++) {
  1053. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1054. if (res == -1) {
  1055. return (-1);
  1056. }
  1057. if (res == 1) {
  1058. msgs_emptied = 1;
  1059. }
  1060. }
  1061. return (msgs_emptied);
  1062. }
  1063. static int passive_member_add (
  1064. struct totemrrp_instance *instance,
  1065. const struct totem_ip_address *member,
  1066. unsigned int iface_no)
  1067. {
  1068. int res;
  1069. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1070. return (res);
  1071. }
  1072. static int passive_member_remove (
  1073. struct totemrrp_instance *instance,
  1074. const struct totem_ip_address *member,
  1075. unsigned int iface_no)
  1076. {
  1077. int res;
  1078. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1079. return (res);
  1080. }
  1081. static void passive_membership_changed (
  1082. struct totemrrp_instance *rrp_instance,
  1083. enum totem_configuration_type configuration_type,
  1084. const struct srp_addr *member_list, size_t member_list_entries,
  1085. const struct srp_addr *left_list, size_t left_list_entries,
  1086. const struct srp_addr *joined_list, size_t joined_list_entries,
  1087. const struct memb_ring_id *ring_id)
  1088. {
  1089. int i;
  1090. int interface;
  1091. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1092. for (i = 0; i < left_list_entries; i++) {
  1093. if (left_list->no_addrs < interface + 1 ||
  1094. (left_list[i].addr[interface].family != AF_INET &&
  1095. left_list[i].addr[interface].family != AF_INET6)) {
  1096. log_printf(rrp_instance->totemrrp_log_level_error,
  1097. "Membership left list contains incorrect address. "
  1098. "This is sign of misconfiguration between nodes!");
  1099. } else {
  1100. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1101. &left_list[i].addr[interface], 0);
  1102. }
  1103. }
  1104. for (i = 0; i < joined_list_entries; i++) {
  1105. if (joined_list->no_addrs < interface + 1 ||
  1106. (joined_list[i].addr[interface].family != AF_INET &&
  1107. joined_list[i].addr[interface].family != AF_INET6)) {
  1108. log_printf(rrp_instance->totemrrp_log_level_error,
  1109. "Membership join list contains incorrect address. "
  1110. "This is sign of misconfiguration between nodes!");
  1111. } else {
  1112. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1113. &joined_list[i].addr[interface], 1);
  1114. }
  1115. }
  1116. }
  1117. }
  1118. static void passive_ring_reenable (
  1119. struct totemrrp_instance *instance,
  1120. unsigned int iface_no)
  1121. {
  1122. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  1123. int i;
  1124. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  1125. instance->interface_count);
  1126. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  1127. instance->interface_count);
  1128. if (iface_no == instance->interface_count) {
  1129. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1130. instance->interface_count);
  1131. for (i = 0; i < instance->interface_count; i++) {
  1132. stats_set_interface_faulty (instance, i, 0);
  1133. }
  1134. } else {
  1135. rrp_algo_instance->faulty[iface_no] = 0;
  1136. stats_set_interface_faulty (instance, iface_no, 0);
  1137. }
  1138. }
  1139. /*
  1140. * Active Replication Implementation
  1141. */
  1142. void *active_instance_initialize (
  1143. struct totemrrp_instance *rrp_instance,
  1144. int interface_count)
  1145. {
  1146. struct active_instance *instance;
  1147. int i;
  1148. instance = malloc (sizeof (struct active_instance));
  1149. if (instance == 0) {
  1150. goto error_exit;
  1151. }
  1152. memset (instance, 0, sizeof (struct active_instance));
  1153. instance->faulty = malloc (sizeof (int) * interface_count);
  1154. if (instance->faulty == 0) {
  1155. free (instance);
  1156. instance = 0;
  1157. goto error_exit;
  1158. }
  1159. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  1160. for (i = 0; i < interface_count; i++) {
  1161. stats_set_interface_faulty (rrp_instance, i, 0);
  1162. }
  1163. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  1164. if (instance->last_token_recv == 0) {
  1165. free (instance->faulty);
  1166. free (instance);
  1167. instance = 0;
  1168. goto error_exit;
  1169. }
  1170. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  1171. instance->counter_problems = malloc (sizeof (int) * interface_count);
  1172. if (instance->counter_problems == 0) {
  1173. free (instance->last_token_recv);
  1174. free (instance->faulty);
  1175. free (instance);
  1176. instance = 0;
  1177. goto error_exit;
  1178. }
  1179. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  1180. instance->timer_expired_token = 0;
  1181. instance->timer_problem_decrementer = 0;
  1182. instance->rrp_instance = rrp_instance;
  1183. instance->last_token_seq = ARR_SEQNO_START_TOKEN - 1;
  1184. error_exit:
  1185. return ((void *)instance);
  1186. }
  1187. static void timer_function_active_problem_decrementer (void *context)
  1188. {
  1189. struct active_instance *active_instance = (struct active_instance *)context;
  1190. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1191. unsigned int problem_found = 0;
  1192. unsigned int i;
  1193. for (i = 0; i < rrp_instance->interface_count; i++) {
  1194. if (active_instance->counter_problems[i] > 0) {
  1195. problem_found = 1;
  1196. active_instance->counter_problems[i] -= 1;
  1197. if (active_instance->counter_problems[i] == 0) {
  1198. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1199. "ring %d active with no faults", i);
  1200. } else {
  1201. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1202. "Decrementing problem counter for iface %s to [%d of %d]",
  1203. totemnet_iface_print (rrp_instance->net_handles[i]),
  1204. active_instance->counter_problems[i],
  1205. rrp_instance->totem_config->rrp_problem_count_threshold);
  1206. }
  1207. log_printf (
  1208. rrp_instance->totemrrp_log_level_warning,
  1209. "%s",
  1210. rrp_instance->status[i]);
  1211. }
  1212. }
  1213. if (problem_found) {
  1214. active_timer_problem_decrementer_start (active_instance);
  1215. } else {
  1216. active_instance->timer_problem_decrementer = 0;
  1217. }
  1218. }
  1219. static void timer_function_active_token_expired (void *context)
  1220. {
  1221. struct active_instance *active_instance = (struct active_instance *)context;
  1222. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1223. unsigned int i;
  1224. for (i = 0; i < rrp_instance->interface_count; i++) {
  1225. if (active_instance->last_token_recv[i] == 0) {
  1226. active_instance->counter_problems[i] += 1;
  1227. if (active_instance->timer_problem_decrementer == 0) {
  1228. active_timer_problem_decrementer_start (active_instance);
  1229. }
  1230. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1231. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  1232. active_instance->last_token_seq,
  1233. totemnet_iface_print (rrp_instance->net_handles[i]),
  1234. active_instance->counter_problems[i],
  1235. rrp_instance->totem_config->rrp_problem_count_threshold);
  1236. log_printf (
  1237. rrp_instance->totemrrp_log_level_warning,
  1238. "%s",
  1239. rrp_instance->status[i]);
  1240. }
  1241. }
  1242. for (i = 0; i < rrp_instance->interface_count; i++) {
  1243. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold &&
  1244. active_instance->faulty[i] == 0) {
  1245. active_instance->faulty[i] = 1;
  1246. qb_loop_timer_add (rrp_instance->poll_handle,
  1247. QB_LOOP_MED,
  1248. rrp_instance->totem_config->rrp_autorecovery_check_timeout*QB_TIME_NS_IN_MSEC,
  1249. rrp_instance->deliver_fn_context[i],
  1250. timer_function_test_ring_timeout,
  1251. &rrp_instance->timer_active_test_ring_timeout[i]);
  1252. stats_set_interface_faulty (rrp_instance, i, active_instance->faulty[i]);
  1253. snprintf (rrp_instance->status[i], STATUS_STR_LEN,
  1254. "Marking seqid %d ringid %u interface %s FAULTY",
  1255. active_instance->last_token_seq,
  1256. i,
  1257. totemnet_iface_print (rrp_instance->net_handles[i]));
  1258. log_printf (
  1259. rrp_instance->totemrrp_log_level_error,
  1260. "%s",
  1261. rrp_instance->status[i]);
  1262. active_timer_problem_decrementer_cancel (active_instance);
  1263. }
  1264. }
  1265. rrp_instance->totemrrp_deliver_fn (
  1266. active_instance->totemrrp_context,
  1267. active_instance->token,
  1268. active_instance->token_len);
  1269. }
  1270. static void active_timer_expired_token_start (
  1271. struct active_instance *active_instance)
  1272. {
  1273. qb_loop_timer_add (
  1274. active_instance->rrp_instance->poll_handle,
  1275. QB_LOOP_MED,
  1276. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout*QB_TIME_NS_IN_MSEC,
  1277. (void *)active_instance,
  1278. timer_function_active_token_expired,
  1279. &active_instance->timer_expired_token);
  1280. }
  1281. static void active_timer_expired_token_cancel (
  1282. struct active_instance *active_instance)
  1283. {
  1284. qb_loop_timer_del (
  1285. active_instance->rrp_instance->poll_handle,
  1286. active_instance->timer_expired_token);
  1287. }
  1288. static void active_timer_problem_decrementer_start (
  1289. struct active_instance *active_instance)
  1290. {
  1291. qb_loop_timer_add (
  1292. active_instance->rrp_instance->poll_handle,
  1293. QB_LOOP_MED,
  1294. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout*QB_TIME_NS_IN_MSEC,
  1295. (void *)active_instance,
  1296. timer_function_active_problem_decrementer,
  1297. &active_instance->timer_problem_decrementer);
  1298. }
  1299. static void active_timer_problem_decrementer_cancel (
  1300. struct active_instance *active_instance)
  1301. {
  1302. qb_loop_timer_del (
  1303. active_instance->rrp_instance->poll_handle,
  1304. active_instance->timer_problem_decrementer);
  1305. active_instance->timer_problem_decrementer = 0;
  1306. }
  1307. /*
  1308. * active replication
  1309. */
  1310. static void active_mcast_recv (
  1311. struct totemrrp_instance *instance,
  1312. unsigned int iface_no,
  1313. void *context,
  1314. const void *msg,
  1315. unsigned int msg_len)
  1316. {
  1317. instance->totemrrp_deliver_fn (
  1318. context,
  1319. msg,
  1320. msg_len);
  1321. }
  1322. static void active_mcast_flush_send (
  1323. struct totemrrp_instance *instance,
  1324. const void *msg,
  1325. unsigned int msg_len)
  1326. {
  1327. int i;
  1328. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1329. for (i = 0; i < instance->interface_count; i++) {
  1330. if (rrp_algo_instance->faulty[i] == 0) {
  1331. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  1332. }
  1333. }
  1334. }
  1335. static void active_mcast_noflush_send (
  1336. struct totemrrp_instance *instance,
  1337. const void *msg,
  1338. unsigned int msg_len)
  1339. {
  1340. int i;
  1341. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1342. for (i = 0; i < instance->interface_count; i++) {
  1343. if (rrp_algo_instance->faulty[i] == 0) {
  1344. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  1345. }
  1346. }
  1347. }
  1348. static void active_token_recv (
  1349. struct totemrrp_instance *rrp_instance,
  1350. unsigned int iface_no,
  1351. void *context,
  1352. const void *msg,
  1353. unsigned int msg_len,
  1354. unsigned int token_seq)
  1355. {
  1356. int i;
  1357. struct active_instance *active_instance = (struct active_instance *)rrp_instance->rrp_algo_instance;
  1358. active_instance->totemrrp_context = context;
  1359. if (sq_lt_compare (active_instance->last_token_seq, token_seq)) {
  1360. memcpy (active_instance->token, msg, msg_len);
  1361. active_instance->token_len = msg_len;
  1362. for (i = 0; i < rrp_instance->interface_count; i++) {
  1363. active_instance->last_token_recv[i] = 0;
  1364. }
  1365. active_instance->last_token_recv[iface_no] = 1;
  1366. active_timer_expired_token_start (active_instance);
  1367. }
  1368. /*
  1369. * This doesn't follow spec because the spec assumes we will know
  1370. * when token resets occur.
  1371. */
  1372. active_instance->last_token_seq = token_seq;
  1373. if (token_seq == active_instance->last_token_seq) {
  1374. active_instance->last_token_recv[iface_no] = 1;
  1375. for (i = 0; i < rrp_instance->interface_count; i++) {
  1376. if ((active_instance->last_token_recv[i] == 0) &&
  1377. active_instance->faulty[i] == 0) {
  1378. return; /* don't deliver token */
  1379. }
  1380. }
  1381. active_timer_expired_token_cancel (active_instance);
  1382. rrp_instance->totemrrp_deliver_fn (
  1383. context,
  1384. msg,
  1385. msg_len);
  1386. }
  1387. }
  1388. static void active_token_send (
  1389. struct totemrrp_instance *instance,
  1390. const void *msg,
  1391. unsigned int msg_len)
  1392. {
  1393. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1394. int i;
  1395. for (i = 0; i < instance->interface_count; i++) {
  1396. if (rrp_algo_instance->faulty[i] == 0) {
  1397. totemnet_token_send (
  1398. instance->net_handles[i],
  1399. msg, msg_len);
  1400. }
  1401. }
  1402. }
  1403. static void active_recv_flush (struct totemrrp_instance *instance)
  1404. {
  1405. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1406. unsigned int i;
  1407. for (i = 0; i < instance->interface_count; i++) {
  1408. if (rrp_algo_instance->faulty[i] == 0) {
  1409. totemnet_recv_flush (instance->net_handles[i]);
  1410. }
  1411. }
  1412. }
  1413. static void active_send_flush (struct totemrrp_instance *instance)
  1414. {
  1415. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1416. unsigned int i;
  1417. for (i = 0; i < instance->interface_count; i++) {
  1418. if (rrp_algo_instance->faulty[i] == 0) {
  1419. totemnet_send_flush (instance->net_handles[i]);
  1420. }
  1421. }
  1422. }
  1423. static int active_member_add (
  1424. struct totemrrp_instance *instance,
  1425. const struct totem_ip_address *member,
  1426. unsigned int iface_no)
  1427. {
  1428. int res;
  1429. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1430. return (res);
  1431. }
  1432. static int active_member_remove (
  1433. struct totemrrp_instance *instance,
  1434. const struct totem_ip_address *member,
  1435. unsigned int iface_no)
  1436. {
  1437. int res;
  1438. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1439. return (res);
  1440. }
  1441. static void active_membership_changed (
  1442. struct totemrrp_instance *rrp_instance,
  1443. enum totem_configuration_type configuration_type,
  1444. const struct srp_addr *member_list, size_t member_list_entries,
  1445. const struct srp_addr *left_list, size_t left_list_entries,
  1446. const struct srp_addr *joined_list, size_t joined_list_entries,
  1447. const struct memb_ring_id *ring_id)
  1448. {
  1449. int i;
  1450. int interface;
  1451. for (interface = 0; interface < rrp_instance->interface_count; interface++) {
  1452. for (i = 0; i < left_list_entries; i++) {
  1453. if (left_list->no_addrs < interface + 1 ||
  1454. (left_list[i].addr[interface].family != AF_INET &&
  1455. left_list[i].addr[interface].family != AF_INET6)) {
  1456. log_printf(rrp_instance->totemrrp_log_level_error,
  1457. "Membership left list contains incorrect address. "
  1458. "This is sign of misconfiguration between nodes!");
  1459. } else {
  1460. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1461. &left_list[i].addr[interface], 0);
  1462. }
  1463. }
  1464. for (i = 0; i < joined_list_entries; i++) {
  1465. if (joined_list->no_addrs < interface + 1 ||
  1466. (joined_list[i].addr[interface].family != AF_INET &&
  1467. joined_list[i].addr[interface].family != AF_INET6)) {
  1468. log_printf(rrp_instance->totemrrp_log_level_error,
  1469. "Membership join list contains incorrect address. "
  1470. "This is sign of misconfiguration between nodes!");
  1471. } else {
  1472. totemnet_member_set_active(rrp_instance->net_handles[interface],
  1473. &joined_list[i].addr[interface], 1);
  1474. }
  1475. }
  1476. }
  1477. }
  1478. static void active_iface_check (struct totemrrp_instance *instance)
  1479. {
  1480. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1481. unsigned int i;
  1482. for (i = 0; i < instance->interface_count; i++) {
  1483. if (rrp_algo_instance->faulty[i] == 0) {
  1484. totemnet_iface_check (instance->net_handles[i]);
  1485. }
  1486. }
  1487. }
  1488. static void active_processor_count_set (
  1489. struct totemrrp_instance *instance,
  1490. unsigned int processor_count)
  1491. {
  1492. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1493. unsigned int i;
  1494. for (i = 0; i < instance->interface_count; i++) {
  1495. if (rrp_algo_instance->faulty[i] == 0) {
  1496. totemnet_processor_count_set (instance->net_handles[i],
  1497. processor_count);
  1498. }
  1499. }
  1500. }
  1501. static void active_token_target_set (
  1502. struct totemrrp_instance *instance,
  1503. struct totem_ip_address *token_target,
  1504. unsigned int iface_no)
  1505. {
  1506. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1507. }
  1508. static int active_mcast_recv_empty (
  1509. struct totemrrp_instance *instance)
  1510. {
  1511. int res;
  1512. int msgs_emptied = 0;
  1513. int i;
  1514. for (i = 0; i < instance->interface_count; i++) {
  1515. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1516. if (res == -1) {
  1517. return (-1);
  1518. }
  1519. if (res == 1) {
  1520. msgs_emptied = 1;
  1521. }
  1522. }
  1523. return (msgs_emptied);
  1524. }
  1525. static void active_ring_reenable (
  1526. struct totemrrp_instance *instance,
  1527. unsigned int iface_no)
  1528. {
  1529. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1530. int i;
  1531. if (iface_no == instance->interface_count) {
  1532. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1533. instance->interface_count);
  1534. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1535. instance->interface_count);
  1536. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1537. instance->interface_count);
  1538. for (i = 0; i < instance->interface_count; i++) {
  1539. stats_set_interface_faulty (instance, i, 0);
  1540. }
  1541. } else {
  1542. rrp_algo_instance->last_token_recv[iface_no] = 0;
  1543. rrp_algo_instance->faulty[iface_no] = 0;
  1544. rrp_algo_instance->counter_problems[iface_no] = 0;
  1545. stats_set_interface_faulty (instance, iface_no, 0);
  1546. }
  1547. }
  1548. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1549. {
  1550. memset (instance, 0, sizeof (struct totemrrp_instance));
  1551. }
  1552. static int totemrrp_algorithm_set (
  1553. struct totem_config *totem_config,
  1554. struct totemrrp_instance *instance)
  1555. {
  1556. unsigned int res = -1;
  1557. unsigned int i;
  1558. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1559. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1560. instance->rrp_algo = rrp_algos[i];
  1561. if (rrp_algos[i]->initialize) {
  1562. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1563. instance,
  1564. totem_config->interface_count);
  1565. }
  1566. res = 0;
  1567. break;
  1568. }
  1569. }
  1570. for (i = 0; i < totem_config->interface_count; i++) {
  1571. instance->status[i] = malloc (STATUS_STR_LEN+1);
  1572. snprintf (instance->status[i], STATUS_STR_LEN,
  1573. "ring %d active with no faults", i);
  1574. }
  1575. return (res);
  1576. }
  1577. void rrp_deliver_fn (
  1578. void *context,
  1579. const void *msg,
  1580. unsigned int msg_len)
  1581. {
  1582. unsigned int token_seqid;
  1583. unsigned int token_is;
  1584. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1585. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  1586. const struct message_header *hdr = msg;
  1587. struct message_header tmp_msg, activate_msg;
  1588. memset(&tmp_msg, 0, sizeof(struct message_header));
  1589. memset(&activate_msg, 0, sizeof(struct message_header));
  1590. rrp_instance->totemrrp_token_seqid_get (
  1591. msg,
  1592. &token_seqid,
  1593. &token_is);
  1594. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVE) {
  1595. log_printf (
  1596. rrp_instance->totemrrp_log_level_debug,
  1597. "received message requesting test of ring now active");
  1598. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1599. test_active_msg_endian_convert(hdr, &tmp_msg);
  1600. hdr = &tmp_msg;
  1601. }
  1602. if (hdr->nodeid_activator == rrp_instance->my_nodeid) {
  1603. /*
  1604. * Send an activate message
  1605. */
  1606. activate_msg.type = MESSAGE_TYPE_RING_TEST_ACTIVATE;
  1607. activate_msg.endian_detector = ENDIAN_LOCAL;
  1608. activate_msg.ring_number = hdr->ring_number;
  1609. activate_msg.nodeid_activator = rrp_instance->my_nodeid;
  1610. totemnet_token_send (
  1611. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1612. &activate_msg, sizeof (struct message_header));
  1613. } else {
  1614. /*
  1615. * Send a ring test message
  1616. */
  1617. totemnet_token_send (
  1618. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1619. msg, msg_len);
  1620. }
  1621. } else
  1622. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVATE) {
  1623. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1624. test_active_msg_endian_convert(hdr, &tmp_msg);
  1625. hdr = &tmp_msg;
  1626. }
  1627. log_printf (
  1628. rrp_instance->totemrrp_log_level_debug,
  1629. "Received ring test activate message for ring %d sent by node %u",
  1630. hdr->ring_number,
  1631. hdr->nodeid_activator);
  1632. if (rrp_instance->stats.faulty[deliver_fn_context->iface_no]) {
  1633. log_printf (rrp_instance->totemrrp_log_level_notice,
  1634. "Automatically recovered ring %d", hdr->ring_number);
  1635. }
  1636. totemrrp_ring_reenable (rrp_instance, deliver_fn_context->iface_no);
  1637. if (hdr->nodeid_activator != rrp_instance->my_nodeid) {
  1638. totemnet_token_send (
  1639. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1640. msg, msg_len);
  1641. }
  1642. } else
  1643. if (token_is) {
  1644. /*
  1645. * Deliver to the token receiver for this rrp algorithm
  1646. */
  1647. rrp_instance->rrp_algo->token_recv (
  1648. rrp_instance,
  1649. deliver_fn_context->iface_no,
  1650. deliver_fn_context->context,
  1651. msg,
  1652. msg_len,
  1653. token_seqid);
  1654. } else {
  1655. /*
  1656. * Deliver to the mcast receiver for this rrp algorithm
  1657. */
  1658. rrp_instance->rrp_algo->mcast_recv (
  1659. rrp_instance,
  1660. deliver_fn_context->iface_no,
  1661. deliver_fn_context->context,
  1662. msg,
  1663. msg_len);
  1664. }
  1665. }
  1666. void rrp_iface_change_fn (
  1667. void *context,
  1668. const struct totem_ip_address *iface_addr)
  1669. {
  1670. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1671. deliver_fn_context->instance->my_nodeid = iface_addr->nodeid;
  1672. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1673. deliver_fn_context->context,
  1674. iface_addr,
  1675. deliver_fn_context->iface_no);
  1676. }
  1677. int totemrrp_finalize (
  1678. void *rrp_context)
  1679. {
  1680. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1681. int i;
  1682. for (i = 0; i < instance->interface_count; i++) {
  1683. totemnet_finalize (instance->net_handles[i]);
  1684. }
  1685. free (instance->net_handles);
  1686. free (instance);
  1687. return (0);
  1688. }
  1689. static void rrp_target_set_completed (void *context)
  1690. {
  1691. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1692. deliver_fn_context->instance->totemrrp_target_set_completed (deliver_fn_context->context);
  1693. }
  1694. /*
  1695. * Totem Redundant Ring interface
  1696. * depends on poll abstraction, POSIX, IPV4
  1697. */
  1698. /*
  1699. * Create an instance
  1700. */
  1701. int totemrrp_initialize (
  1702. qb_loop_t *poll_handle,
  1703. void **rrp_context,
  1704. struct totem_config *totem_config,
  1705. totemsrp_stats_t *stats,
  1706. void *context,
  1707. void (*deliver_fn) (
  1708. void *context,
  1709. const void *msg,
  1710. unsigned int msg_len),
  1711. void (*iface_change_fn) (
  1712. void *context,
  1713. const struct totem_ip_address *iface_addr,
  1714. unsigned int iface_no),
  1715. void (*token_seqid_get) (
  1716. const void *msg,
  1717. unsigned int *seqid,
  1718. unsigned int *token_is),
  1719. unsigned int (*msgs_missing) (void),
  1720. void (*target_set_completed) (void *context))
  1721. {
  1722. struct totemrrp_instance *instance;
  1723. unsigned int res;
  1724. int i;
  1725. instance = malloc (sizeof (struct totemrrp_instance));
  1726. if (instance == 0) {
  1727. return (-1);
  1728. }
  1729. totemrrp_instance_initialize (instance);
  1730. instance->totem_config = totem_config;
  1731. stats->rrp = &instance->stats;
  1732. instance->stats.interface_count = totem_config->interface_count;
  1733. instance->stats.faulty = calloc(instance->stats.interface_count, sizeof(uint8_t));
  1734. res = totemrrp_algorithm_set (
  1735. instance->totem_config,
  1736. instance);
  1737. if (res == -1) {
  1738. goto error_destroy;
  1739. }
  1740. /*
  1741. * Configure logging
  1742. */
  1743. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1744. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1745. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1746. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1747. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1748. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1749. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1750. instance->interfaces = totem_config->interfaces;
  1751. instance->poll_handle = poll_handle;
  1752. instance->totemrrp_deliver_fn = deliver_fn;
  1753. instance->totemrrp_iface_change_fn = iface_change_fn;
  1754. instance->totemrrp_token_seqid_get = token_seqid_get;
  1755. instance->totemrrp_target_set_completed = target_set_completed;
  1756. instance->totemrrp_msgs_missing = msgs_missing;
  1757. instance->interface_count = totem_config->interface_count;
  1758. instance->net_handles = malloc (sizeof (void *) * totem_config->interface_count);
  1759. instance->context = context;
  1760. instance->poll_handle = poll_handle;
  1761. for (i = 0; i < totem_config->interface_count; i++) {
  1762. struct deliver_fn_context *deliver_fn_context;
  1763. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1764. assert (deliver_fn_context);
  1765. deliver_fn_context->instance = instance;
  1766. deliver_fn_context->context = context;
  1767. deliver_fn_context->iface_no = i;
  1768. instance->deliver_fn_context[i] = (void *)deliver_fn_context;
  1769. totemnet_initialize (
  1770. poll_handle,
  1771. &instance->net_handles[i],
  1772. totem_config,
  1773. stats,
  1774. i,
  1775. (void *)deliver_fn_context,
  1776. rrp_deliver_fn,
  1777. rrp_iface_change_fn,
  1778. rrp_target_set_completed);
  1779. totemnet_net_mtu_adjust (instance->net_handles[i], totem_config);
  1780. }
  1781. *rrp_context = instance;
  1782. return (0);
  1783. error_destroy:
  1784. free (instance);
  1785. return (res);
  1786. }
  1787. void *totemrrp_buffer_alloc (void *rrp_context)
  1788. {
  1789. struct totemrrp_instance *instance = rrp_context;
  1790. assert (instance != NULL);
  1791. return totemnet_buffer_alloc (instance->net_handles[0]);
  1792. }
  1793. void totemrrp_buffer_release (void *rrp_context, void *ptr)
  1794. {
  1795. struct totemrrp_instance *instance = rrp_context;
  1796. assert (instance != NULL);
  1797. totemnet_buffer_release (instance->net_handles[0], ptr);
  1798. }
  1799. int totemrrp_processor_count_set (
  1800. void *rrp_context,
  1801. unsigned int processor_count)
  1802. {
  1803. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1804. instance->rrp_algo->processor_count_set (instance, processor_count);
  1805. instance->processor_count = processor_count;
  1806. return (0);
  1807. }
  1808. int totemrrp_token_target_set (
  1809. void *rrp_context,
  1810. struct totem_ip_address *addr,
  1811. unsigned int iface_no)
  1812. {
  1813. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1814. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1815. return (0);
  1816. }
  1817. int totemrrp_recv_flush (void *rrp_context)
  1818. {
  1819. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1820. instance->rrp_algo->recv_flush (instance);
  1821. return (0);
  1822. }
  1823. int totemrrp_send_flush (void *rrp_context)
  1824. {
  1825. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1826. instance->rrp_algo->send_flush (instance);
  1827. return (0);
  1828. }
  1829. int totemrrp_token_send (
  1830. void *rrp_context,
  1831. const void *msg,
  1832. unsigned int msg_len)
  1833. {
  1834. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1835. instance->rrp_algo->token_send (instance, msg, msg_len);
  1836. return (0);
  1837. }
  1838. int totemrrp_mcast_flush_send (
  1839. void *rrp_context,
  1840. const void *msg,
  1841. unsigned int msg_len)
  1842. {
  1843. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1844. int res = 0;
  1845. // TODO this needs to return the result
  1846. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1847. return (res);
  1848. }
  1849. int totemrrp_mcast_noflush_send (
  1850. void *rrp_context,
  1851. const void *msg,
  1852. unsigned int msg_len)
  1853. {
  1854. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1855. /*
  1856. * merge detects go out through mcast_flush_send so it is safe to
  1857. * flush these messages if we are only one processor. This avoids
  1858. * an encryption/hmac and decryption/hmac
  1859. */
  1860. if (instance->processor_count > 1) {
  1861. // TODO this needs to return the result
  1862. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1863. }
  1864. return (0);
  1865. }
  1866. int totemrrp_iface_check (void *rrp_context)
  1867. {
  1868. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1869. instance->rrp_algo->iface_check (instance);
  1870. return (0);
  1871. }
  1872. int totemrrp_ifaces_get (
  1873. void *rrp_context,
  1874. char ***status,
  1875. unsigned int *iface_count)
  1876. {
  1877. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1878. *status = instance->status;
  1879. if (iface_count) {
  1880. *iface_count = instance->interface_count;
  1881. }
  1882. return (0);
  1883. }
  1884. int totemrrp_crypto_set (
  1885. void *rrp_context,
  1886. const char *cipher_type,
  1887. const char *hash_type)
  1888. {
  1889. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1890. int res;
  1891. res = totemnet_crypto_set(instance->net_handles[0], cipher_type, hash_type);
  1892. return (res);
  1893. }
  1894. /*
  1895. * iface_no indicates the interface number [0, ..., interface_count-1] of the
  1896. * specific ring which will be reenabled. We specify iface_no == interface_count
  1897. * means reenabling all the rings.
  1898. */
  1899. int totemrrp_ring_reenable (
  1900. void *rrp_context,
  1901. unsigned int iface_no)
  1902. {
  1903. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1904. int res = 0;
  1905. unsigned int i;
  1906. instance->rrp_algo->ring_reenable (instance, iface_no);
  1907. if (iface_no == instance->interface_count) {
  1908. for (i = 0; i < instance->interface_count; i++) {
  1909. snprintf (instance->status[i], STATUS_STR_LEN,
  1910. "ring %d active with no faults", i);
  1911. }
  1912. } else {
  1913. snprintf (instance->status[iface_no], STATUS_STR_LEN,
  1914. "ring %d active with no faults", iface_no);
  1915. }
  1916. return (res);
  1917. }
  1918. extern int totemrrp_mcast_recv_empty (
  1919. void *rrp_context)
  1920. {
  1921. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1922. int res;
  1923. res = instance->rrp_algo->mcast_recv_empty (instance);
  1924. return (res);
  1925. }
  1926. int totemrrp_member_add (
  1927. void *rrp_context,
  1928. const struct totem_ip_address *member,
  1929. int iface_no)
  1930. {
  1931. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1932. int res;
  1933. res = instance->rrp_algo->member_add (instance, member, iface_no);
  1934. return (res);
  1935. }
  1936. int totemrrp_member_remove (
  1937. void *rrp_context,
  1938. const struct totem_ip_address *member,
  1939. int iface_no)
  1940. {
  1941. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1942. int res;
  1943. res = instance->rrp_algo->member_remove (instance, member, iface_no);
  1944. return (res);
  1945. }
  1946. void totemrrp_membership_changed (
  1947. void *rrp_context,
  1948. enum totem_configuration_type configuration_type,
  1949. const struct srp_addr *member_list, size_t member_list_entries,
  1950. const struct srp_addr *left_list, size_t left_list_entries,
  1951. const struct srp_addr *joined_list, size_t joined_list_entries,
  1952. const struct memb_ring_id *ring_id)
  1953. {
  1954. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1955. instance->rrp_algo->membership_changed (instance,
  1956. configuration_type,
  1957. member_list, member_list_entries,
  1958. left_list, left_list_entries,
  1959. joined_list, joined_list_entries,
  1960. ring_id);
  1961. }