totemrrp.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <assert.h>
  37. #include <pthread.h>
  38. #include <sys/mman.h>
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <sys/socket.h>
  42. #include <netdb.h>
  43. #include <sys/un.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/param.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <unistd.h>
  49. #include <fcntl.h>
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <errno.h>
  53. #include <sched.h>
  54. #include <time.h>
  55. #include <sys/time.h>
  56. #include <sys/poll.h>
  57. #include <limits.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/list.h>
  60. #include <corosync/hdb.h>
  61. #include <corosync/swab.h>
  62. #include <corosync/totem/coropoll.h>
  63. #define LOGSYS_UTILS_ONLY 1
  64. #include <corosync/engine/logsys.h>
  65. #include "totemnet.h"
  66. #include "totemrrp.h"
  67. void rrp_deliver_fn (
  68. void *context,
  69. const void *msg,
  70. unsigned int msg_len);
  71. void rrp_iface_change_fn (
  72. void *context,
  73. const struct totem_ip_address *iface_addr);
  74. struct totemrrp_instance;
  75. struct passive_instance {
  76. struct totemrrp_instance *rrp_instance;
  77. unsigned int *faulty;
  78. unsigned int *token_recv_count;
  79. unsigned int *mcast_recv_count;
  80. unsigned char token[15000];
  81. unsigned int token_len;
  82. poll_timer_handle timer_expired_token;
  83. poll_timer_handle timer_problem_decrementer;
  84. void *totemrrp_context;
  85. unsigned int token_xmit_iface;
  86. unsigned int msg_xmit_iface;
  87. };
  88. struct active_instance {
  89. struct totemrrp_instance *rrp_instance;
  90. unsigned int *faulty;
  91. unsigned int *last_token_recv;
  92. unsigned int *counter_problems;
  93. unsigned char token[15000];
  94. unsigned int token_len;
  95. unsigned int last_token_seq;
  96. poll_timer_handle timer_expired_token;
  97. poll_timer_handle timer_problem_decrementer;
  98. void *totemrrp_context;
  99. };
  100. struct rrp_algo {
  101. const char *name;
  102. void * (*initialize) (
  103. struct totemrrp_instance *rrp_instance,
  104. int interface_count);
  105. void (*mcast_recv) (
  106. struct totemrrp_instance *instance,
  107. unsigned int iface_no,
  108. void *context,
  109. const void *msg,
  110. unsigned int msg_len);
  111. void (*mcast_noflush_send) (
  112. struct totemrrp_instance *instance,
  113. const void *msg,
  114. unsigned int msg_len);
  115. void (*mcast_flush_send) (
  116. struct totemrrp_instance *instance,
  117. const void *msg,
  118. unsigned int msg_len);
  119. void (*token_recv) (
  120. struct totemrrp_instance *instance,
  121. unsigned int iface_no,
  122. void *context,
  123. const void *msg,
  124. unsigned int msg_len,
  125. unsigned int token_seqid);
  126. void (*token_send) (
  127. struct totemrrp_instance *instance,
  128. const void *msg,
  129. unsigned int msg_len);
  130. void (*send_flush) (
  131. struct totemrrp_instance *instance);
  132. void (*iface_check) (
  133. struct totemrrp_instance *instance);
  134. void (*processor_count_set) (
  135. struct totemrrp_instance *instance,
  136. unsigned int processor_count);
  137. void (*token_target_set) (
  138. struct totemrrp_instance *instance,
  139. struct totem_ip_address *token_target,
  140. unsigned int iface_no);
  141. void (*ring_reenable) (
  142. struct totemrrp_instance *instance,
  143. unsigned int iface_no);
  144. int (*mcast_recv_empty) (
  145. struct totemrrp_instance *instance);
  146. int (*member_add) (
  147. struct totemrrp_instance *instance,
  148. const struct totem_ip_address *member,
  149. unsigned int iface_no);
  150. int (*member_remove) (
  151. struct totemrrp_instance *instance,
  152. const struct totem_ip_address *member,
  153. unsigned int iface_no);
  154. };
  155. struct totemrrp_instance {
  156. hdb_handle_t poll_handle;
  157. struct totem_interface *interfaces;
  158. struct rrp_algo *rrp_algo;
  159. void *context;
  160. char *status[INTERFACE_MAX];
  161. void (*totemrrp_deliver_fn) (
  162. void *context,
  163. const void *msg,
  164. unsigned int msg_len);
  165. void (*totemrrp_iface_change_fn) (
  166. void *context,
  167. const struct totem_ip_address *iface_addr,
  168. unsigned int iface_no);
  169. void (*totemrrp_token_seqid_get) (
  170. const void *msg,
  171. unsigned int *seqid,
  172. unsigned int *token_is);
  173. void (*totemrrp_target_set_completed) (
  174. void *context);
  175. unsigned int (*totemrrp_msgs_missing) (void);
  176. /*
  177. * Function and data used to log messages
  178. */
  179. int totemrrp_log_level_security;
  180. int totemrrp_log_level_error;
  181. int totemrrp_log_level_warning;
  182. int totemrrp_log_level_notice;
  183. int totemrrp_log_level_debug;
  184. int totemrrp_subsys_id;
  185. void (*totemrrp_log_printf) (
  186. unsigned int rec_ident,
  187. const char *function,
  188. const char *file,
  189. int line,
  190. const char *format, ...)__attribute__((format(printf, 5, 6)));
  191. void **net_handles;
  192. void *rrp_algo_instance;
  193. int interface_count;
  194. int processor_count;
  195. int my_nodeid;
  196. struct totem_config *totem_config;
  197. void *deliver_fn_context[INTERFACE_MAX];
  198. poll_timer_handle timer_active_test_ring_timeout[INTERFACE_MAX];
  199. };
  200. /*
  201. * None Replication Forward Declerations
  202. */
  203. static void none_mcast_recv (
  204. struct totemrrp_instance *instance,
  205. unsigned int iface_no,
  206. void *context,
  207. const void *msg,
  208. unsigned int msg_len);
  209. static void none_mcast_noflush_send (
  210. struct totemrrp_instance *instance,
  211. const void *msg,
  212. unsigned int msg_len);
  213. static void none_mcast_flush_send (
  214. struct totemrrp_instance *instance,
  215. const void *msg,
  216. unsigned int msg_len);
  217. static void none_token_recv (
  218. struct totemrrp_instance *instance,
  219. unsigned int iface_no,
  220. void *context,
  221. const void *msg,
  222. unsigned int msg_len,
  223. unsigned int token_seqid);
  224. static void none_token_send (
  225. struct totemrrp_instance *instance,
  226. const void *msg,
  227. unsigned int msg_len);
  228. static void none_send_flush (
  229. struct totemrrp_instance *instance);
  230. static void none_iface_check (
  231. struct totemrrp_instance *instance);
  232. static void none_processor_count_set (
  233. struct totemrrp_instance *instance,
  234. unsigned int processor_count_set);
  235. static void none_token_target_set (
  236. struct totemrrp_instance *instance,
  237. struct totem_ip_address *token_target,
  238. unsigned int iface_no);
  239. static void none_ring_reenable (
  240. struct totemrrp_instance *instance,
  241. unsigned int iface_no);
  242. static int none_mcast_recv_empty (
  243. struct totemrrp_instance *instance);
  244. static int none_member_add (
  245. struct totemrrp_instance *instance,
  246. const struct totem_ip_address *member,
  247. unsigned int iface_no);
  248. static int none_member_remove (
  249. struct totemrrp_instance *instance,
  250. const struct totem_ip_address *member,
  251. unsigned int iface_no);
  252. /*
  253. * Passive Replication Forward Declerations
  254. */
  255. static void *passive_instance_initialize (
  256. struct totemrrp_instance *rrp_instance,
  257. int interface_count);
  258. static void passive_mcast_recv (
  259. struct totemrrp_instance *instance,
  260. unsigned int iface_no,
  261. void *context,
  262. const void *msg,
  263. unsigned int msg_len);
  264. static void passive_mcast_noflush_send (
  265. struct totemrrp_instance *instance,
  266. const void *msg,
  267. unsigned int msg_len);
  268. static void passive_mcast_flush_send (
  269. struct totemrrp_instance *instance,
  270. const void *msg,
  271. unsigned int msg_len);
  272. static void passive_monitor (
  273. struct totemrrp_instance *rrp_instance,
  274. unsigned int iface_no,
  275. int is_token_recv_count);
  276. static void passive_token_recv (
  277. struct totemrrp_instance *instance,
  278. unsigned int iface_no,
  279. void *context,
  280. const void *msg,
  281. unsigned int msg_len,
  282. unsigned int token_seqid);
  283. static void passive_token_send (
  284. struct totemrrp_instance *instance,
  285. const void *msg,
  286. unsigned int msg_len);
  287. static void passive_send_flush (
  288. struct totemrrp_instance *instance);
  289. static void passive_iface_check (
  290. struct totemrrp_instance *instance);
  291. static void passive_processor_count_set (
  292. struct totemrrp_instance *instance,
  293. unsigned int processor_count_set);
  294. static void passive_token_target_set (
  295. struct totemrrp_instance *instance,
  296. struct totem_ip_address *token_target,
  297. unsigned int iface_no);
  298. static void passive_ring_reenable (
  299. struct totemrrp_instance *instance,
  300. unsigned int iface_no);
  301. static int passive_mcast_recv_empty (
  302. struct totemrrp_instance *instance);
  303. static int passive_member_add (
  304. struct totemrrp_instance *instance,
  305. const struct totem_ip_address *member,
  306. unsigned int iface_no);
  307. static int passive_member_remove (
  308. struct totemrrp_instance *instance,
  309. const struct totem_ip_address *member,
  310. unsigned int iface_no);
  311. /*
  312. * Active Replication Forward Definitions
  313. */
  314. static void *active_instance_initialize (
  315. struct totemrrp_instance *rrp_instance,
  316. int interface_count);
  317. static void active_mcast_recv (
  318. struct totemrrp_instance *instance,
  319. unsigned int iface_no,
  320. void *context,
  321. const void *msg,
  322. unsigned int msg_len);
  323. static void active_mcast_noflush_send (
  324. struct totemrrp_instance *instance,
  325. const void *msg,
  326. unsigned int msg_len);
  327. static void active_mcast_flush_send (
  328. struct totemrrp_instance *instance,
  329. const void *msg,
  330. unsigned int msg_len);
  331. static void active_token_recv (
  332. struct totemrrp_instance *instance,
  333. unsigned int iface_no,
  334. void *context,
  335. const void *msg,
  336. unsigned int msg_len,
  337. unsigned int token_seqid);
  338. static void active_token_send (
  339. struct totemrrp_instance *instance,
  340. const void *msg,
  341. unsigned int msg_len);
  342. static void active_send_flush (
  343. struct totemrrp_instance *instance);
  344. static void active_iface_check (
  345. struct totemrrp_instance *instance);
  346. static void active_processor_count_set (
  347. struct totemrrp_instance *instance,
  348. unsigned int processor_count_set);
  349. static void active_token_target_set (
  350. struct totemrrp_instance *instance,
  351. struct totem_ip_address *token_target,
  352. unsigned int iface_no);
  353. static void active_ring_reenable (
  354. struct totemrrp_instance *instance,
  355. unsigned int iface_no);
  356. static int active_mcast_recv_empty (
  357. struct totemrrp_instance *instance);
  358. static int active_member_add (
  359. struct totemrrp_instance *instance,
  360. const struct totem_ip_address *member,
  361. unsigned int iface_no);
  362. static int active_member_remove (
  363. struct totemrrp_instance *instance,
  364. const struct totem_ip_address *member,
  365. unsigned int iface_no);
  366. static void active_timer_expired_token_start (
  367. struct active_instance *active_instance);
  368. static void active_timer_expired_token_cancel (
  369. struct active_instance *active_instance);
  370. static void active_timer_problem_decrementer_start (
  371. struct active_instance *active_instance);
  372. static void active_timer_problem_decrementer_cancel (
  373. struct active_instance *active_instance);
  374. /*
  375. * 0-5 reserved for totemsrp.c
  376. */
  377. #define MESSAGE_TYPE_RING_TEST_ACTIVE 6
  378. #define MESSAGE_TYPE_RING_TEST_ACTIVATE 7
  379. #define ENDIAN_LOCAL 0xff22
  380. /*
  381. * Rollover handling:
  382. *
  383. * ARR_SEQNO_START_TOKEN is the starting sequence number of last seen sequence
  384. * for a token for active redundand ring. This should remain zero, unless testing
  385. * overflow in which case 07fffff00 or 0xffffff00 are good starting values.
  386. * It should be same as on defined in totemsrp.c
  387. */
  388. #define ARR_SEQNO_START_TOKEN 0x0
  389. /*
  390. * These can be used ot test different rollover points
  391. * #define ARR_SEQNO_START_MSG 0xfffffe00
  392. */
  393. /*
  394. * Threshold value when recv_count for passive rrp should be adjusted.
  395. * Set this value to some smaller for testing of adjusting proper
  396. * functionality. Also keep in mind that this value must be smaller
  397. * then rrp_problem_count_threshold
  398. */
  399. #define PASSIVE_RECV_COUNT_THRESHOLD (INT_MAX / 2)
  400. struct message_header {
  401. char type;
  402. char encapsulated;
  403. unsigned short endian_detector;
  404. int ring_number;
  405. int nodeid_activator;
  406. } __attribute__((packed));
  407. struct deliver_fn_context {
  408. struct totemrrp_instance *instance;
  409. void *context;
  410. int iface_no;
  411. };
  412. struct rrp_algo none_algo = {
  413. .name = "none",
  414. .initialize = NULL,
  415. .mcast_recv = none_mcast_recv,
  416. .mcast_noflush_send = none_mcast_noflush_send,
  417. .mcast_flush_send = none_mcast_flush_send,
  418. .token_recv = none_token_recv,
  419. .token_send = none_token_send,
  420. .send_flush = none_send_flush,
  421. .iface_check = none_iface_check,
  422. .processor_count_set = none_processor_count_set,
  423. .token_target_set = none_token_target_set,
  424. .ring_reenable = none_ring_reenable,
  425. .mcast_recv_empty = none_mcast_recv_empty,
  426. .member_add = none_member_add,
  427. .member_remove = none_member_remove
  428. };
  429. struct rrp_algo passive_algo = {
  430. .name = "passive",
  431. .initialize = passive_instance_initialize,
  432. .mcast_recv = passive_mcast_recv,
  433. .mcast_noflush_send = passive_mcast_noflush_send,
  434. .mcast_flush_send = passive_mcast_flush_send,
  435. .token_recv = passive_token_recv,
  436. .token_send = passive_token_send,
  437. .send_flush = passive_send_flush,
  438. .iface_check = passive_iface_check,
  439. .processor_count_set = passive_processor_count_set,
  440. .token_target_set = passive_token_target_set,
  441. .ring_reenable = passive_ring_reenable,
  442. .mcast_recv_empty = passive_mcast_recv_empty,
  443. .member_add = passive_member_add,
  444. .member_remove = passive_member_remove
  445. };
  446. struct rrp_algo active_algo = {
  447. .name = "active",
  448. .initialize = active_instance_initialize,
  449. .mcast_recv = active_mcast_recv,
  450. .mcast_noflush_send = active_mcast_noflush_send,
  451. .mcast_flush_send = active_mcast_flush_send,
  452. .token_recv = active_token_recv,
  453. .token_send = active_token_send,
  454. .send_flush = active_send_flush,
  455. .iface_check = active_iface_check,
  456. .processor_count_set = active_processor_count_set,
  457. .token_target_set = active_token_target_set,
  458. .ring_reenable = active_ring_reenable,
  459. .mcast_recv_empty = active_mcast_recv_empty,
  460. .member_add = active_member_add,
  461. .member_remove = active_member_remove
  462. };
  463. struct rrp_algo *rrp_algos[] = {
  464. &none_algo,
  465. &passive_algo,
  466. &active_algo
  467. };
  468. #define RRP_ALGOS_COUNT 3
  469. #define log_printf(level, format, args...) \
  470. do { \
  471. rrp_instance->totemrrp_log_printf ( \
  472. LOGSYS_ENCODE_RECID(level, \
  473. rrp_instance->totemrrp_subsys_id, \
  474. LOGSYS_RECID_LOG), \
  475. __FUNCTION__, __FILE__, __LINE__, \
  476. format, ##args); \
  477. } while (0);
  478. static void test_active_msg_endian_convert(const struct message_header *in, struct message_header *out)
  479. {
  480. out->type = in->type;
  481. out->encapsulated = in->encapsulated;
  482. out->endian_detector = ENDIAN_LOCAL;
  483. out->ring_number = swab32 (in->ring_number);
  484. out->nodeid_activator = swab32(in->nodeid_activator);
  485. }
  486. static void timer_function_test_ring_timeout (void *context)
  487. {
  488. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  489. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  490. unsigned int *faulty = NULL;
  491. int iface_no = deliver_fn_context->iface_no;
  492. struct message_header msg = {
  493. .type = MESSAGE_TYPE_RING_TEST_ACTIVE,
  494. .endian_detector = ENDIAN_LOCAL,
  495. };
  496. if (strcmp(rrp_instance->totem_config->rrp_mode, "active") == 0)
  497. faulty = ((struct active_instance *)(rrp_instance->rrp_algo_instance))->faulty;
  498. if (strcmp(rrp_instance->totem_config->rrp_mode, "passive") == 0)
  499. faulty = ((struct passive_instance *)(rrp_instance->rrp_algo_instance))->faulty;
  500. assert (faulty != NULL);
  501. if (faulty[iface_no] == 1) {
  502. msg.ring_number = iface_no;
  503. msg.nodeid_activator = rrp_instance->my_nodeid;
  504. totemnet_token_send (
  505. rrp_instance->net_handles[iface_no],
  506. &msg, sizeof (struct message_header));
  507. poll_timer_add (rrp_instance->poll_handle,
  508. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  509. (void *)deliver_fn_context,
  510. timer_function_test_ring_timeout,
  511. &rrp_instance->timer_active_test_ring_timeout[iface_no]);
  512. }
  513. }
  514. /*
  515. * None Replication Implementation
  516. */
  517. static void none_mcast_recv (
  518. struct totemrrp_instance *rrp_instance,
  519. unsigned int iface_no,
  520. void *context,
  521. const void *msg,
  522. unsigned int msg_len)
  523. {
  524. rrp_instance->totemrrp_deliver_fn (
  525. context,
  526. msg,
  527. msg_len);
  528. }
  529. static void none_mcast_flush_send (
  530. struct totemrrp_instance *instance,
  531. const void *msg,
  532. unsigned int msg_len)
  533. {
  534. totemnet_mcast_flush_send (instance->net_handles[0], msg, msg_len);
  535. }
  536. static void none_mcast_noflush_send (
  537. struct totemrrp_instance *instance,
  538. const void *msg,
  539. unsigned int msg_len)
  540. {
  541. totemnet_mcast_noflush_send (instance->net_handles[0], msg, msg_len);
  542. }
  543. static void none_token_recv (
  544. struct totemrrp_instance *rrp_instance,
  545. unsigned int iface_no,
  546. void *context,
  547. const void *msg,
  548. unsigned int msg_len,
  549. unsigned int token_seq)
  550. {
  551. rrp_instance->totemrrp_deliver_fn (
  552. context,
  553. msg,
  554. msg_len);
  555. }
  556. static void none_token_send (
  557. struct totemrrp_instance *instance,
  558. const void *msg,
  559. unsigned int msg_len)
  560. {
  561. totemnet_token_send (
  562. instance->net_handles[0],
  563. msg, msg_len);
  564. }
  565. static void none_send_flush (struct totemrrp_instance *instance)
  566. {
  567. totemnet_send_flush (instance->net_handles[0]);
  568. }
  569. static void none_iface_check (struct totemrrp_instance *instance)
  570. {
  571. totemnet_iface_check (instance->net_handles[0]);
  572. }
  573. static void none_processor_count_set (
  574. struct totemrrp_instance *instance,
  575. unsigned int processor_count)
  576. {
  577. totemnet_processor_count_set (instance->net_handles[0],
  578. processor_count);
  579. }
  580. static void none_token_target_set (
  581. struct totemrrp_instance *instance,
  582. struct totem_ip_address *token_target,
  583. unsigned int iface_no)
  584. {
  585. totemnet_token_target_set (instance->net_handles[0], token_target);
  586. }
  587. static void none_ring_reenable (
  588. struct totemrrp_instance *instance,
  589. unsigned int iface_no)
  590. {
  591. /*
  592. * No operation
  593. */
  594. }
  595. static int none_mcast_recv_empty (
  596. struct totemrrp_instance *instance)
  597. {
  598. int res;
  599. res = totemnet_recv_mcast_empty (instance->net_handles[0]);
  600. return (res);
  601. }
  602. static int none_member_add (
  603. struct totemrrp_instance *instance,
  604. const struct totem_ip_address *member,
  605. unsigned int iface_no)
  606. {
  607. int res;
  608. res = totemnet_member_add (instance->net_handles[0], member);
  609. return (res);
  610. }
  611. static int none_member_remove (
  612. struct totemrrp_instance *instance,
  613. const struct totem_ip_address *member,
  614. unsigned int iface_no)
  615. {
  616. int res;
  617. res = totemnet_member_remove (instance->net_handles[0], member);
  618. return (res);
  619. }
  620. /*
  621. * Passive Replication Implementation
  622. */
  623. void *passive_instance_initialize (
  624. struct totemrrp_instance *rrp_instance,
  625. int interface_count)
  626. {
  627. struct passive_instance *instance;
  628. instance = malloc (sizeof (struct passive_instance));
  629. if (instance == 0) {
  630. goto error_exit;
  631. }
  632. memset (instance, 0, sizeof (struct passive_instance));
  633. instance->faulty = malloc (sizeof (int) * interface_count);
  634. if (instance->faulty == 0) {
  635. free (instance);
  636. instance = 0;
  637. goto error_exit;
  638. }
  639. memset (instance->faulty, 0, sizeof (int) * interface_count);
  640. instance->token_recv_count = malloc (sizeof (int) * interface_count);
  641. if (instance->token_recv_count == 0) {
  642. free (instance->faulty);
  643. free (instance);
  644. instance = 0;
  645. goto error_exit;
  646. }
  647. memset (instance->token_recv_count, 0, sizeof (int) * interface_count);
  648. instance->mcast_recv_count = malloc (sizeof (int) * interface_count);
  649. if (instance->mcast_recv_count == 0) {
  650. free (instance->token_recv_count);
  651. free (instance->faulty);
  652. free (instance);
  653. instance = 0;
  654. goto error_exit;
  655. }
  656. memset (instance->mcast_recv_count, 0, sizeof (int) * interface_count);
  657. error_exit:
  658. return ((void *)instance);
  659. }
  660. static void timer_function_passive_token_expired (void *context)
  661. {
  662. struct passive_instance *passive_instance = (struct passive_instance *)context;
  663. struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  664. rrp_instance->totemrrp_deliver_fn (
  665. passive_instance->totemrrp_context,
  666. passive_instance->token,
  667. passive_instance->token_len);
  668. }
  669. /* TODO
  670. static void timer_function_passive_problem_decrementer (void *context)
  671. {
  672. // struct passive_instance *passive_instance = (struct passive_instance *)context;
  673. // struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  674. }
  675. */
  676. static void passive_timer_expired_token_start (
  677. struct passive_instance *passive_instance)
  678. {
  679. poll_timer_add (
  680. passive_instance->rrp_instance->poll_handle,
  681. passive_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  682. (void *)passive_instance,
  683. timer_function_passive_token_expired,
  684. &passive_instance->timer_expired_token);
  685. }
  686. static void passive_timer_expired_token_cancel (
  687. struct passive_instance *passive_instance)
  688. {
  689. poll_timer_delete (
  690. passive_instance->rrp_instance->poll_handle,
  691. passive_instance->timer_expired_token);
  692. }
  693. /*
  694. static void passive_timer_problem_decrementer_start (
  695. struct passive_instance *passive_instance)
  696. {
  697. poll_timer_add (
  698. passive_instance->rrp_instance->poll_handle,
  699. passive_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  700. (void *)passive_instance,
  701. timer_function_passive_problem_decrementer,
  702. &passive_instance->timer_problem_decrementer);
  703. }
  704. static void passive_timer_problem_decrementer_cancel (
  705. struct passive_instance *passive_instance)
  706. {
  707. poll_timer_delete (
  708. passive_instance->rrp_instance->poll_handle,
  709. passive_instance->timer_problem_decrementer);
  710. }
  711. */
  712. /*
  713. * Monitor function implementation from rrp paper.
  714. * rrp_instance is passive rrp instance, iface_no is interface with received messgae/token and
  715. * is_token_recv_count is boolean variable which donates if message is token (>1) or regular
  716. * message (= 0)
  717. */
  718. static void passive_monitor (
  719. struct totemrrp_instance *rrp_instance,
  720. unsigned int iface_no,
  721. int is_token_recv_count)
  722. {
  723. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  724. unsigned int *recv_count;
  725. unsigned int max;
  726. unsigned int i;
  727. unsigned int min_all, min_active;
  728. /*
  729. * Monitor for failures
  730. */
  731. if (is_token_recv_count) {
  732. recv_count = passive_instance->token_recv_count;
  733. } else {
  734. recv_count = passive_instance->mcast_recv_count;
  735. }
  736. recv_count[iface_no] += 1;
  737. max = 0;
  738. for (i = 0; i < rrp_instance->interface_count; i++) {
  739. if (max < recv_count[i]) {
  740. max = recv_count[i];
  741. }
  742. }
  743. /*
  744. * Max is larger then threshold -> start adjusting process
  745. */
  746. if (max > PASSIVE_RECV_COUNT_THRESHOLD) {
  747. min_all = min_active = recv_count[iface_no];
  748. for (i = 0; i < rrp_instance->interface_count; i++) {
  749. if (recv_count[i] < min_all) {
  750. min_all = recv_count[i];
  751. }
  752. if (passive_instance->faulty[i] == 0 &&
  753. recv_count[i] < min_active) {
  754. min_active = recv_count[i];
  755. }
  756. }
  757. if (min_all > 0) {
  758. /*
  759. * There is one or more faulty device with recv_count > 0
  760. */
  761. for (i = 0; i < rrp_instance->interface_count; i++) {
  762. recv_count[i] -= min_all;
  763. }
  764. } else {
  765. /*
  766. * No faulty device with recv_count > 0, adjust only active
  767. * devices
  768. */
  769. for (i = 0; i < rrp_instance->interface_count; i++) {
  770. if (passive_instance->faulty[i] == 0) {
  771. recv_count[i] -= min_active;
  772. }
  773. }
  774. }
  775. /*
  776. * Find again max
  777. */
  778. max = 0;
  779. for (i = 0; i < rrp_instance->interface_count; i++) {
  780. if (max < recv_count[i]) {
  781. max = recv_count[i];
  782. }
  783. }
  784. }
  785. for (i = 0; i < rrp_instance->interface_count; i++) {
  786. if ((passive_instance->faulty[i] == 0) &&
  787. (max - recv_count[i] >
  788. rrp_instance->totem_config->rrp_problem_count_threshold)) {
  789. passive_instance->faulty[i] = 1;
  790. poll_timer_add (rrp_instance->poll_handle,
  791. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  792. rrp_instance->deliver_fn_context[i],
  793. timer_function_test_ring_timeout,
  794. &rrp_instance->timer_active_test_ring_timeout[i]);
  795. sprintf (rrp_instance->status[i],
  796. "Marking ringid %u interface %s FAULTY",
  797. i,
  798. totemnet_iface_print (rrp_instance->net_handles[i]));
  799. log_printf (
  800. rrp_instance->totemrrp_log_level_error,
  801. "%s",
  802. rrp_instance->status[i]);
  803. }
  804. }
  805. }
  806. static void passive_mcast_recv (
  807. struct totemrrp_instance *rrp_instance,
  808. unsigned int iface_no,
  809. void *context,
  810. const void *msg,
  811. unsigned int msg_len)
  812. {
  813. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  814. rrp_instance->totemrrp_deliver_fn (
  815. context,
  816. msg,
  817. msg_len);
  818. if (rrp_instance->totemrrp_msgs_missing() == 0 &&
  819. passive_instance->timer_expired_token) {
  820. /*
  821. * Delivers the last token
  822. */
  823. rrp_instance->totemrrp_deliver_fn (
  824. passive_instance->totemrrp_context,
  825. passive_instance->token,
  826. passive_instance->token_len);
  827. passive_timer_expired_token_cancel (passive_instance);
  828. }
  829. passive_monitor (rrp_instance, iface_no, 0);
  830. }
  831. static void passive_mcast_flush_send (
  832. struct totemrrp_instance *instance,
  833. const void *msg,
  834. unsigned int msg_len)
  835. {
  836. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  837. do {
  838. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  839. } while (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1);
  840. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  841. }
  842. static void passive_mcast_noflush_send (
  843. struct totemrrp_instance *instance,
  844. const void *msg,
  845. unsigned int msg_len)
  846. {
  847. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  848. do {
  849. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  850. } while (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1);
  851. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  852. }
  853. static void passive_token_recv (
  854. struct totemrrp_instance *rrp_instance,
  855. unsigned int iface_no,
  856. void *context,
  857. const void *msg,
  858. unsigned int msg_len,
  859. unsigned int token_seq)
  860. {
  861. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  862. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  863. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  864. rrp_instance->totemrrp_deliver_fn (
  865. context,
  866. msg,
  867. msg_len);
  868. } else {
  869. memcpy (passive_instance->token, msg, msg_len);
  870. passive_timer_expired_token_start (passive_instance);
  871. }
  872. passive_monitor (rrp_instance, iface_no, 1);
  873. }
  874. static void passive_token_send (
  875. struct totemrrp_instance *instance,
  876. const void *msg,
  877. unsigned int msg_len)
  878. {
  879. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  880. do {
  881. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  882. } while (passive_instance->faulty[passive_instance->token_xmit_iface] == 1);
  883. totemnet_token_send (
  884. instance->net_handles[passive_instance->token_xmit_iface],
  885. msg, msg_len);
  886. }
  887. static void passive_send_flush (struct totemrrp_instance *instance)
  888. {
  889. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  890. unsigned int i;
  891. for (i = 0; i < instance->interface_count; i++) {
  892. if (rrp_algo_instance->faulty[i] == 0) {
  893. totemnet_send_flush (instance->net_handles[i]);
  894. }
  895. }
  896. }
  897. static void passive_iface_check (struct totemrrp_instance *instance)
  898. {
  899. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  900. unsigned int i;
  901. for (i = 0; i < instance->interface_count; i++) {
  902. if (rrp_algo_instance->faulty[i] == 0) {
  903. totemnet_iface_check (instance->net_handles[i]);
  904. }
  905. }
  906. }
  907. static void passive_processor_count_set (
  908. struct totemrrp_instance *instance,
  909. unsigned int processor_count)
  910. {
  911. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  912. unsigned int i;
  913. for (i = 0; i < instance->interface_count; i++) {
  914. if (rrp_algo_instance->faulty[i] == 0) {
  915. totemnet_processor_count_set (instance->net_handles[i],
  916. processor_count);
  917. }
  918. }
  919. }
  920. static void passive_token_target_set (
  921. struct totemrrp_instance *instance,
  922. struct totem_ip_address *token_target,
  923. unsigned int iface_no)
  924. {
  925. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  926. }
  927. static int passive_mcast_recv_empty (
  928. struct totemrrp_instance *instance)
  929. {
  930. int res;
  931. int msgs_emptied = 0;
  932. int i;
  933. for (i = 0; i < instance->interface_count; i++) {
  934. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  935. if (res == -1) {
  936. return (-1);
  937. }
  938. if (res == 1) {
  939. msgs_emptied = 1;
  940. }
  941. }
  942. return (msgs_emptied);
  943. }
  944. static int passive_member_add (
  945. struct totemrrp_instance *instance,
  946. const struct totem_ip_address *member,
  947. unsigned int iface_no)
  948. {
  949. int res;
  950. res = totemnet_member_add (instance->net_handles[iface_no], member);
  951. return (res);
  952. }
  953. static int passive_member_remove (
  954. struct totemrrp_instance *instance,
  955. const struct totem_ip_address *member,
  956. unsigned int iface_no)
  957. {
  958. int res;
  959. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  960. return (res);
  961. }
  962. static void passive_ring_reenable (
  963. struct totemrrp_instance *instance,
  964. unsigned int iface_no)
  965. {
  966. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  967. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  968. instance->interface_count);
  969. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  970. instance->interface_count);
  971. if (iface_no == instance->interface_count) {
  972. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  973. instance->interface_count);
  974. } else {
  975. rrp_algo_instance->faulty[iface_no] = 0;
  976. }
  977. }
  978. /*
  979. * Active Replication Implementation
  980. */
  981. void *active_instance_initialize (
  982. struct totemrrp_instance *rrp_instance,
  983. int interface_count)
  984. {
  985. struct active_instance *instance;
  986. instance = malloc (sizeof (struct active_instance));
  987. if (instance == 0) {
  988. goto error_exit;
  989. }
  990. memset (instance, 0, sizeof (struct active_instance));
  991. instance->faulty = malloc (sizeof (int) * interface_count);
  992. if (instance->faulty == 0) {
  993. free (instance);
  994. instance = 0;
  995. goto error_exit;
  996. }
  997. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  998. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  999. if (instance->last_token_recv == 0) {
  1000. free (instance->faulty);
  1001. free (instance);
  1002. instance = 0;
  1003. goto error_exit;
  1004. }
  1005. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  1006. instance->counter_problems = malloc (sizeof (int) * interface_count);
  1007. if (instance->counter_problems == 0) {
  1008. free (instance->last_token_recv);
  1009. free (instance->faulty);
  1010. free (instance);
  1011. instance = 0;
  1012. goto error_exit;
  1013. }
  1014. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  1015. instance->timer_expired_token = 0;
  1016. instance->timer_problem_decrementer = 0;
  1017. instance->rrp_instance = rrp_instance;
  1018. instance->last_token_seq = ARR_SEQNO_START_TOKEN - 1;
  1019. error_exit:
  1020. return ((void *)instance);
  1021. }
  1022. static void timer_function_active_problem_decrementer (void *context)
  1023. {
  1024. struct active_instance *active_instance = (struct active_instance *)context;
  1025. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1026. unsigned int problem_found = 0;
  1027. unsigned int i;
  1028. for (i = 0; i < rrp_instance->interface_count; i++) {
  1029. if (active_instance->counter_problems[i] > 0) {
  1030. problem_found = 1;
  1031. active_instance->counter_problems[i] -= 1;
  1032. if (active_instance->counter_problems[i] == 0) {
  1033. sprintf (rrp_instance->status[i],
  1034. "ring %d active with no faults", i);
  1035. } else {
  1036. sprintf (rrp_instance->status[i],
  1037. "Decrementing problem counter for iface %s to [%d of %d]",
  1038. totemnet_iface_print (rrp_instance->net_handles[i]),
  1039. active_instance->counter_problems[i],
  1040. rrp_instance->totem_config->rrp_problem_count_threshold);
  1041. }
  1042. log_printf (
  1043. rrp_instance->totemrrp_log_level_warning,
  1044. "%s",
  1045. rrp_instance->status[i]);
  1046. }
  1047. }
  1048. if (problem_found) {
  1049. active_timer_problem_decrementer_start (active_instance);
  1050. } else {
  1051. active_instance->timer_problem_decrementer = 0;
  1052. }
  1053. }
  1054. static void timer_function_active_token_expired (void *context)
  1055. {
  1056. struct active_instance *active_instance = (struct active_instance *)context;
  1057. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  1058. unsigned int i;
  1059. for (i = 0; i < rrp_instance->interface_count; i++) {
  1060. if (active_instance->last_token_recv[i] == 0) {
  1061. active_instance->counter_problems[i] += 1;
  1062. if (active_instance->timer_problem_decrementer == 0) {
  1063. active_timer_problem_decrementer_start (active_instance);
  1064. }
  1065. sprintf (rrp_instance->status[i],
  1066. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  1067. active_instance->last_token_seq,
  1068. totemnet_iface_print (rrp_instance->net_handles[i]),
  1069. active_instance->counter_problems[i],
  1070. rrp_instance->totem_config->rrp_problem_count_threshold);
  1071. log_printf (
  1072. rrp_instance->totemrrp_log_level_warning,
  1073. "%s",
  1074. rrp_instance->status[i]);
  1075. }
  1076. }
  1077. for (i = 0; i < rrp_instance->interface_count; i++) {
  1078. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold)
  1079. {
  1080. active_instance->faulty[i] = 1;
  1081. poll_timer_add (rrp_instance->poll_handle,
  1082. rrp_instance->totem_config->rrp_autorecovery_check_timeout,
  1083. rrp_instance->deliver_fn_context[i],
  1084. timer_function_test_ring_timeout,
  1085. &rrp_instance->timer_active_test_ring_timeout[i]);
  1086. sprintf (rrp_instance->status[i],
  1087. "Marking seqid %d ringid %u interface %s FAULTY",
  1088. active_instance->last_token_seq,
  1089. i,
  1090. totemnet_iface_print (rrp_instance->net_handles[i]));
  1091. log_printf (
  1092. rrp_instance->totemrrp_log_level_error,
  1093. "%s",
  1094. rrp_instance->status[i]);
  1095. active_timer_problem_decrementer_cancel (active_instance);
  1096. }
  1097. }
  1098. rrp_instance->totemrrp_deliver_fn (
  1099. active_instance->totemrrp_context,
  1100. active_instance->token,
  1101. active_instance->token_len);
  1102. }
  1103. static void active_timer_expired_token_start (
  1104. struct active_instance *active_instance)
  1105. {
  1106. poll_timer_add (
  1107. active_instance->rrp_instance->poll_handle,
  1108. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  1109. (void *)active_instance,
  1110. timer_function_active_token_expired,
  1111. &active_instance->timer_expired_token);
  1112. }
  1113. static void active_timer_expired_token_cancel (
  1114. struct active_instance *active_instance)
  1115. {
  1116. poll_timer_delete (
  1117. active_instance->rrp_instance->poll_handle,
  1118. active_instance->timer_expired_token);
  1119. }
  1120. static void active_timer_problem_decrementer_start (
  1121. struct active_instance *active_instance)
  1122. {
  1123. poll_timer_add (
  1124. active_instance->rrp_instance->poll_handle,
  1125. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  1126. (void *)active_instance,
  1127. timer_function_active_problem_decrementer,
  1128. &active_instance->timer_problem_decrementer);
  1129. }
  1130. static void active_timer_problem_decrementer_cancel (
  1131. struct active_instance *active_instance)
  1132. {
  1133. poll_timer_delete (
  1134. active_instance->rrp_instance->poll_handle,
  1135. active_instance->timer_problem_decrementer);
  1136. }
  1137. /*
  1138. * active replication
  1139. */
  1140. static void active_mcast_recv (
  1141. struct totemrrp_instance *instance,
  1142. unsigned int iface_no,
  1143. void *context,
  1144. const void *msg,
  1145. unsigned int msg_len)
  1146. {
  1147. instance->totemrrp_deliver_fn (
  1148. context,
  1149. msg,
  1150. msg_len);
  1151. }
  1152. static void active_mcast_flush_send (
  1153. struct totemrrp_instance *instance,
  1154. const void *msg,
  1155. unsigned int msg_len)
  1156. {
  1157. int i;
  1158. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1159. for (i = 0; i < instance->interface_count; i++) {
  1160. if (rrp_algo_instance->faulty[i] == 0) {
  1161. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  1162. }
  1163. }
  1164. }
  1165. static void active_mcast_noflush_send (
  1166. struct totemrrp_instance *instance,
  1167. const void *msg,
  1168. unsigned int msg_len)
  1169. {
  1170. int i;
  1171. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1172. for (i = 0; i < instance->interface_count; i++) {
  1173. if (rrp_algo_instance->faulty[i] == 0) {
  1174. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  1175. }
  1176. }
  1177. }
  1178. static void active_token_recv (
  1179. struct totemrrp_instance *rrp_instance,
  1180. unsigned int iface_no,
  1181. void *context,
  1182. const void *msg,
  1183. unsigned int msg_len,
  1184. unsigned int token_seq)
  1185. {
  1186. int i;
  1187. struct active_instance *active_instance = (struct active_instance *)rrp_instance->rrp_algo_instance;
  1188. active_instance->totemrrp_context = context;
  1189. if (sq_lt_compare (active_instance->last_token_seq, token_seq)) {
  1190. memcpy (active_instance->token, msg, msg_len);
  1191. active_instance->token_len = msg_len;
  1192. for (i = 0; i < rrp_instance->interface_count; i++) {
  1193. active_instance->last_token_recv[i] = 0;
  1194. }
  1195. active_instance->last_token_recv[iface_no] = 1;
  1196. active_timer_expired_token_start (active_instance);
  1197. }
  1198. /*
  1199. * This doesn't follow spec because the spec assumes we will know
  1200. * when token resets occur.
  1201. */
  1202. active_instance->last_token_seq = token_seq;
  1203. if (token_seq == active_instance->last_token_seq) {
  1204. active_instance->last_token_recv[iface_no] = 1;
  1205. for (i = 0; i < rrp_instance->interface_count; i++) {
  1206. if ((active_instance->last_token_recv[i] == 0) &&
  1207. active_instance->faulty[i] == 0) {
  1208. return; /* don't deliver token */
  1209. }
  1210. }
  1211. active_timer_expired_token_cancel (active_instance);
  1212. rrp_instance->totemrrp_deliver_fn (
  1213. context,
  1214. msg,
  1215. msg_len);
  1216. }
  1217. }
  1218. static void active_token_send (
  1219. struct totemrrp_instance *instance,
  1220. const void *msg,
  1221. unsigned int msg_len)
  1222. {
  1223. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1224. int i;
  1225. for (i = 0; i < instance->interface_count; i++) {
  1226. if (rrp_algo_instance->faulty[i] == 0) {
  1227. totemnet_token_send (
  1228. instance->net_handles[i],
  1229. msg, msg_len);
  1230. }
  1231. }
  1232. }
  1233. static void active_send_flush (struct totemrrp_instance *instance)
  1234. {
  1235. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1236. unsigned int i;
  1237. for (i = 0; i < instance->interface_count; i++) {
  1238. if (rrp_algo_instance->faulty[i] == 0) {
  1239. totemnet_send_flush (instance->net_handles[i]);
  1240. }
  1241. }
  1242. }
  1243. static int active_member_add (
  1244. struct totemrrp_instance *instance,
  1245. const struct totem_ip_address *member,
  1246. unsigned int iface_no)
  1247. {
  1248. int res;
  1249. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1250. return (res);
  1251. }
  1252. static int active_member_remove (
  1253. struct totemrrp_instance *instance,
  1254. const struct totem_ip_address *member,
  1255. unsigned int iface_no)
  1256. {
  1257. int res;
  1258. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1259. return (res);
  1260. }
  1261. static void active_iface_check (struct totemrrp_instance *instance)
  1262. {
  1263. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1264. unsigned int i;
  1265. for (i = 0; i < instance->interface_count; i++) {
  1266. if (rrp_algo_instance->faulty[i] == 0) {
  1267. totemnet_iface_check (instance->net_handles[i]);
  1268. }
  1269. }
  1270. }
  1271. static void active_processor_count_set (
  1272. struct totemrrp_instance *instance,
  1273. unsigned int processor_count)
  1274. {
  1275. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1276. unsigned int i;
  1277. for (i = 0; i < instance->interface_count; i++) {
  1278. if (rrp_algo_instance->faulty[i] == 0) {
  1279. totemnet_processor_count_set (instance->net_handles[i],
  1280. processor_count);
  1281. }
  1282. }
  1283. }
  1284. static void active_token_target_set (
  1285. struct totemrrp_instance *instance,
  1286. struct totem_ip_address *token_target,
  1287. unsigned int iface_no)
  1288. {
  1289. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1290. }
  1291. static int active_mcast_recv_empty (
  1292. struct totemrrp_instance *instance)
  1293. {
  1294. int res;
  1295. int msgs_emptied = 0;
  1296. int i;
  1297. for (i = 0; i < instance->interface_count; i++) {
  1298. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1299. if (res == -1) {
  1300. return (-1);
  1301. }
  1302. if (res == 1) {
  1303. msgs_emptied = 1;
  1304. }
  1305. }
  1306. return (msgs_emptied);
  1307. }
  1308. static void active_ring_reenable (
  1309. struct totemrrp_instance *instance,
  1310. unsigned int iface_no)
  1311. {
  1312. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1313. if (iface_no == instance->interface_count) {
  1314. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1315. instance->interface_count);
  1316. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1317. instance->interface_count);
  1318. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1319. instance->interface_count);
  1320. } else {
  1321. rrp_algo_instance->last_token_recv[iface_no] = 0;
  1322. rrp_algo_instance->faulty[iface_no] = 0;
  1323. rrp_algo_instance->counter_problems[iface_no] = 0;
  1324. }
  1325. }
  1326. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1327. {
  1328. memset (instance, 0, sizeof (struct totemrrp_instance));
  1329. }
  1330. static int totemrrp_algorithm_set (
  1331. struct totem_config *totem_config,
  1332. struct totemrrp_instance *instance)
  1333. {
  1334. unsigned int res = -1;
  1335. unsigned int i;
  1336. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1337. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1338. instance->rrp_algo = rrp_algos[i];
  1339. if (rrp_algos[i]->initialize) {
  1340. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1341. instance,
  1342. totem_config->interface_count);
  1343. }
  1344. res = 0;
  1345. break;
  1346. }
  1347. }
  1348. for (i = 0; i < totem_config->interface_count; i++) {
  1349. instance->status[i] = malloc (1024);
  1350. sprintf (instance->status[i], "ring %d active with no faults", i);
  1351. }
  1352. return (res);
  1353. }
  1354. void rrp_deliver_fn (
  1355. void *context,
  1356. const void *msg,
  1357. unsigned int msg_len)
  1358. {
  1359. unsigned int token_seqid;
  1360. unsigned int token_is;
  1361. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1362. struct totemrrp_instance *rrp_instance = deliver_fn_context->instance;
  1363. const struct message_header *hdr = msg;
  1364. struct message_header tmp_msg, activate_msg;
  1365. memset(&tmp_msg, 0, sizeof(struct message_header));
  1366. memset(&activate_msg, 0, sizeof(struct message_header));
  1367. rrp_instance->totemrrp_token_seqid_get (
  1368. msg,
  1369. &token_seqid,
  1370. &token_is);
  1371. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVE) {
  1372. log_printf (
  1373. rrp_instance->totemrrp_log_level_debug,
  1374. "received message requesting test of ring now active\n");
  1375. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1376. test_active_msg_endian_convert(hdr, &tmp_msg);
  1377. hdr = &tmp_msg;
  1378. }
  1379. if (hdr->nodeid_activator == rrp_instance->my_nodeid) {
  1380. /*
  1381. * Send an activate message
  1382. */
  1383. activate_msg.type = MESSAGE_TYPE_RING_TEST_ACTIVATE;
  1384. activate_msg.endian_detector = ENDIAN_LOCAL;
  1385. activate_msg.ring_number = hdr->ring_number;
  1386. activate_msg.nodeid_activator = rrp_instance->my_nodeid;
  1387. totemnet_token_send (
  1388. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1389. &activate_msg, sizeof (struct message_header));
  1390. } else {
  1391. /*
  1392. * Send a ring test message
  1393. */
  1394. totemnet_token_send (
  1395. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1396. msg, msg_len);
  1397. }
  1398. } else
  1399. if (hdr->type == MESSAGE_TYPE_RING_TEST_ACTIVATE) {
  1400. log_printf (
  1401. rrp_instance->totemrrp_log_level_notice,
  1402. "Automatically recovered ring %d\n", hdr->ring_number);
  1403. if (hdr->endian_detector != ENDIAN_LOCAL) {
  1404. test_active_msg_endian_convert(hdr, &tmp_msg);
  1405. hdr = &tmp_msg;
  1406. }
  1407. totemrrp_ring_reenable (rrp_instance, deliver_fn_context->iface_no);
  1408. if (hdr->nodeid_activator != rrp_instance->my_nodeid) {
  1409. totemnet_token_send (
  1410. rrp_instance->net_handles[deliver_fn_context->iface_no],
  1411. msg, msg_len);
  1412. }
  1413. } else
  1414. if (token_is) {
  1415. /*
  1416. * Deliver to the token receiver for this rrp algorithm
  1417. */
  1418. rrp_instance->rrp_algo->token_recv (
  1419. rrp_instance,
  1420. deliver_fn_context->iface_no,
  1421. deliver_fn_context->context,
  1422. msg,
  1423. msg_len,
  1424. token_seqid);
  1425. } else {
  1426. /*
  1427. * Deliver to the mcast receiver for this rrp algorithm
  1428. */
  1429. rrp_instance->rrp_algo->mcast_recv (
  1430. rrp_instance,
  1431. deliver_fn_context->iface_no,
  1432. deliver_fn_context->context,
  1433. msg,
  1434. msg_len);
  1435. }
  1436. }
  1437. void rrp_iface_change_fn (
  1438. void *context,
  1439. const struct totem_ip_address *iface_addr)
  1440. {
  1441. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1442. deliver_fn_context->instance->my_nodeid = iface_addr->nodeid;
  1443. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1444. deliver_fn_context->context,
  1445. iface_addr,
  1446. deliver_fn_context->iface_no);
  1447. }
  1448. int totemrrp_finalize (
  1449. void *rrp_context)
  1450. {
  1451. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1452. int i;
  1453. for (i = 0; i < instance->interface_count; i++) {
  1454. totemnet_finalize (instance->net_handles[i]);
  1455. }
  1456. return (0);
  1457. }
  1458. static void rrp_target_set_completed (void *context)
  1459. {
  1460. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1461. deliver_fn_context->instance->totemrrp_target_set_completed (deliver_fn_context->context);
  1462. }
  1463. /*
  1464. * Totem Redundant Ring interface
  1465. * depends on poll abstraction, POSIX, IPV4
  1466. */
  1467. /*
  1468. * Create an instance
  1469. */
  1470. int totemrrp_initialize (
  1471. hdb_handle_t poll_handle,
  1472. void **rrp_context,
  1473. struct totem_config *totem_config,
  1474. void *context,
  1475. void (*deliver_fn) (
  1476. void *context,
  1477. const void *msg,
  1478. unsigned int msg_len),
  1479. void (*iface_change_fn) (
  1480. void *context,
  1481. const struct totem_ip_address *iface_addr,
  1482. unsigned int iface_no),
  1483. void (*token_seqid_get) (
  1484. const void *msg,
  1485. unsigned int *seqid,
  1486. unsigned int *token_is),
  1487. unsigned int (*msgs_missing) (void),
  1488. void (*target_set_completed) (void *context))
  1489. {
  1490. struct totemrrp_instance *instance;
  1491. unsigned int res;
  1492. int i;
  1493. instance = malloc (sizeof (struct totemrrp_instance));
  1494. if (instance == 0) {
  1495. return (-1);
  1496. }
  1497. totemrrp_instance_initialize (instance);
  1498. instance->totem_config = totem_config;
  1499. res = totemrrp_algorithm_set (
  1500. instance->totem_config,
  1501. instance);
  1502. if (res == -1) {
  1503. goto error_destroy;
  1504. }
  1505. /*
  1506. * Configure logging
  1507. */
  1508. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1509. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1510. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1511. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1512. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1513. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1514. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1515. instance->interfaces = totem_config->interfaces;
  1516. instance->poll_handle = poll_handle;
  1517. instance->totemrrp_deliver_fn = deliver_fn;
  1518. instance->totemrrp_iface_change_fn = iface_change_fn;
  1519. instance->totemrrp_token_seqid_get = token_seqid_get;
  1520. instance->totemrrp_target_set_completed = target_set_completed;
  1521. instance->totemrrp_msgs_missing = msgs_missing;
  1522. instance->interface_count = totem_config->interface_count;
  1523. instance->net_handles = malloc (sizeof (void *) * totem_config->interface_count);
  1524. instance->context = context;
  1525. instance->poll_handle = poll_handle;
  1526. for (i = 0; i < totem_config->interface_count; i++) {
  1527. struct deliver_fn_context *deliver_fn_context;
  1528. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1529. assert (deliver_fn_context);
  1530. deliver_fn_context->instance = instance;
  1531. deliver_fn_context->context = context;
  1532. deliver_fn_context->iface_no = i;
  1533. instance->deliver_fn_context[i] = (void *)deliver_fn_context;
  1534. totemnet_initialize (
  1535. poll_handle,
  1536. &instance->net_handles[i],
  1537. totem_config,
  1538. i,
  1539. (void *)deliver_fn_context,
  1540. rrp_deliver_fn,
  1541. rrp_iface_change_fn,
  1542. rrp_target_set_completed);
  1543. totemnet_net_mtu_adjust (instance->net_handles[i], totem_config);
  1544. }
  1545. *rrp_context = instance;
  1546. return (0);
  1547. error_destroy:
  1548. free (instance);
  1549. return (res);
  1550. }
  1551. void *totemrrp_buffer_alloc (void *rrp_context)
  1552. {
  1553. struct totemrrp_instance *instance = rrp_context;
  1554. assert (instance != NULL);
  1555. return totemnet_buffer_alloc (instance->net_handles[0]);
  1556. }
  1557. void totemrrp_buffer_release (void *rrp_context, void *ptr)
  1558. {
  1559. struct totemrrp_instance *instance = rrp_context;
  1560. assert (instance != NULL);
  1561. totemnet_buffer_release (instance->net_handles[0], ptr);
  1562. }
  1563. int totemrrp_processor_count_set (
  1564. void *rrp_context,
  1565. unsigned int processor_count)
  1566. {
  1567. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1568. instance->rrp_algo->processor_count_set (instance, processor_count);
  1569. instance->processor_count = processor_count;
  1570. return (0);
  1571. }
  1572. int totemrrp_token_target_set (
  1573. void *rrp_context,
  1574. struct totem_ip_address *addr,
  1575. unsigned int iface_no)
  1576. {
  1577. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1578. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1579. return (0);
  1580. }
  1581. int totemrrp_send_flush (void *rrp_context)
  1582. {
  1583. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1584. instance->rrp_algo->send_flush (instance);
  1585. return (0);
  1586. }
  1587. int totemrrp_token_send (
  1588. void *rrp_context,
  1589. const void *msg,
  1590. unsigned int msg_len)
  1591. {
  1592. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1593. instance->rrp_algo->token_send (instance, msg, msg_len);
  1594. return (0);
  1595. }
  1596. int totemrrp_mcast_flush_send (
  1597. void *rrp_context,
  1598. const void *msg,
  1599. unsigned int msg_len)
  1600. {
  1601. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1602. int res = 0;
  1603. // TODO this needs to return the result
  1604. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1605. return (res);
  1606. }
  1607. int totemrrp_mcast_noflush_send (
  1608. void *rrp_context,
  1609. const void *msg,
  1610. unsigned int msg_len)
  1611. {
  1612. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1613. /*
  1614. * merge detects go out through mcast_flush_send so it is safe to
  1615. * flush these messages if we are only one processor. This avoids
  1616. * an encryption/hmac and decryption/hmac
  1617. */
  1618. if (instance->processor_count > 1) {
  1619. // TODO this needs to return the result
  1620. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1621. }
  1622. return (0);
  1623. }
  1624. int totemrrp_iface_check (void *rrp_context)
  1625. {
  1626. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1627. instance->rrp_algo->iface_check (instance);
  1628. return (0);
  1629. }
  1630. int totemrrp_ifaces_get (
  1631. void *rrp_context,
  1632. char ***status,
  1633. unsigned int *iface_count)
  1634. {
  1635. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1636. *status = instance->status;
  1637. if (iface_count) {
  1638. *iface_count = instance->interface_count;
  1639. }
  1640. return (0);
  1641. }
  1642. int totemrrp_crypto_set (
  1643. void *rrp_context,
  1644. unsigned int type)
  1645. {
  1646. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1647. int res;
  1648. res = totemnet_crypto_set(instance->net_handles[0], type);
  1649. return (res);
  1650. }
  1651. /*
  1652. * iface_no indicates the interface number [0, ..., interface_count-1] of the
  1653. * specific ring which will be reenabled. We specify iface_no == interface_count
  1654. * means reenabling all the rings.
  1655. */
  1656. int totemrrp_ring_reenable (
  1657. void *rrp_context,
  1658. unsigned int iface_no)
  1659. {
  1660. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1661. int res = 0;
  1662. unsigned int i;
  1663. instance->rrp_algo->ring_reenable (instance, iface_no);
  1664. if (iface_no == instance->interface_count) {
  1665. for (i = 0; i < instance->interface_count; i++) {
  1666. sprintf (instance->status[i], "ring %d active with no faults", i);
  1667. }
  1668. } else {
  1669. sprintf (instance->status[iface_no], "ring %d active with no faults", iface_no);
  1670. }
  1671. return (res);
  1672. }
  1673. extern int totemrrp_mcast_recv_empty (
  1674. void *rrp_context)
  1675. {
  1676. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1677. int res;
  1678. res = instance->rrp_algo->mcast_recv_empty (instance);
  1679. return (res);
  1680. }
  1681. int totemrrp_member_add (
  1682. void *rrp_context,
  1683. const struct totem_ip_address *member,
  1684. int iface_no)
  1685. {
  1686. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1687. int res;
  1688. res = instance->rrp_algo->member_add (instance, member, iface_no);
  1689. return (res);
  1690. }
  1691. int totemrrp_member_remove (
  1692. void *rrp_context,
  1693. const struct totem_ip_address *member,
  1694. int iface_no)
  1695. {
  1696. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1697. int res;
  1698. res = instance->rrp_algo->member_remove (instance, member, iface_no);
  1699. return (res);
  1700. }