totemrrp.c 51 KB

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