totemrrp.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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 (*recv_flush) (
  131. struct totemrrp_instance *instance);
  132. void (*send_flush) (
  133. struct totemrrp_instance *instance);
  134. void (*iface_check) (
  135. struct totemrrp_instance *instance);
  136. void (*processor_count_set) (
  137. struct totemrrp_instance *instance,
  138. unsigned int processor_count);
  139. void (*token_target_set) (
  140. struct totemrrp_instance *instance,
  141. struct totem_ip_address *token_target,
  142. unsigned int iface_no);
  143. void (*ring_reenable) (
  144. struct totemrrp_instance *instance);
  145. int (*mcast_recv_empty) (
  146. struct totemrrp_instance *instance);
  147. int (*member_add) (
  148. struct totemrrp_instance *instance,
  149. const struct totem_ip_address *member,
  150. unsigned int iface_no);
  151. int (*member_remove) (
  152. struct totemrrp_instance *instance,
  153. const struct totem_ip_address *member,
  154. unsigned int iface_no);
  155. };
  156. struct totemrrp_instance {
  157. hdb_handle_t poll_handle;
  158. struct totem_interface *interfaces;
  159. struct rrp_algo *rrp_algo;
  160. void *context;
  161. char *status[INTERFACE_MAX];
  162. void (*totemrrp_deliver_fn) (
  163. void *context,
  164. const void *msg,
  165. unsigned int msg_len);
  166. void (*totemrrp_iface_change_fn) (
  167. void *context,
  168. const struct totem_ip_address *iface_addr,
  169. unsigned int iface_no);
  170. void (*totemrrp_token_seqid_get) (
  171. const void *msg,
  172. unsigned int *seqid,
  173. unsigned int *token_is);
  174. void (*totemrrp_target_set_completed) (
  175. void *context);
  176. unsigned int (*totemrrp_msgs_missing) (void);
  177. /*
  178. * Function and data used to log messages
  179. */
  180. int totemrrp_log_level_security;
  181. int totemrrp_log_level_error;
  182. int totemrrp_log_level_warning;
  183. int totemrrp_log_level_notice;
  184. int totemrrp_log_level_debug;
  185. int totemrrp_subsys_id;
  186. void (*totemrrp_log_printf) (
  187. unsigned int rec_ident,
  188. const char *function,
  189. const char *file,
  190. int line,
  191. const char *format, ...)__attribute__((format(printf, 5, 6)));
  192. void **net_handles;
  193. void *rrp_algo_instance;
  194. int interface_count;
  195. int processor_count;
  196. struct totem_config *totem_config;
  197. };
  198. /*
  199. * None Replication Forward Declerations
  200. */
  201. static void none_mcast_recv (
  202. struct totemrrp_instance *instance,
  203. unsigned int iface_no,
  204. void *context,
  205. const void *msg,
  206. unsigned int msg_len);
  207. static void none_mcast_noflush_send (
  208. struct totemrrp_instance *instance,
  209. const void *msg,
  210. unsigned int msg_len);
  211. static void none_mcast_flush_send (
  212. struct totemrrp_instance *instance,
  213. const void *msg,
  214. unsigned int msg_len);
  215. static void none_token_recv (
  216. struct totemrrp_instance *instance,
  217. unsigned int iface_no,
  218. void *context,
  219. const void *msg,
  220. unsigned int msg_len,
  221. unsigned int token_seqid);
  222. static void none_token_send (
  223. struct totemrrp_instance *instance,
  224. const void *msg,
  225. unsigned int msg_len);
  226. static void none_recv_flush (
  227. struct totemrrp_instance *instance);
  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. static int none_mcast_recv_empty (
  242. struct totemrrp_instance *instance);
  243. static int none_member_add (
  244. struct totemrrp_instance *instance,
  245. const struct totem_ip_address *member,
  246. unsigned int iface_no);
  247. static int none_member_remove (
  248. struct totemrrp_instance *instance,
  249. const struct totem_ip_address *member,
  250. unsigned int iface_no);
  251. /*
  252. * Passive Replication Forward Declerations
  253. */
  254. static void *passive_instance_initialize (
  255. struct totemrrp_instance *rrp_instance,
  256. int interface_count);
  257. static void passive_mcast_recv (
  258. struct totemrrp_instance *instance,
  259. unsigned int iface_no,
  260. void *context,
  261. const void *msg,
  262. unsigned int msg_len);
  263. static void passive_mcast_noflush_send (
  264. struct totemrrp_instance *instance,
  265. const void *msg,
  266. unsigned int msg_len);
  267. static void passive_mcast_flush_send (
  268. struct totemrrp_instance *instance,
  269. const void *msg,
  270. unsigned int msg_len);
  271. static void passive_token_recv (
  272. struct totemrrp_instance *instance,
  273. unsigned int iface_no,
  274. void *context,
  275. const void *msg,
  276. unsigned int msg_len,
  277. unsigned int token_seqid);
  278. static void passive_token_send (
  279. struct totemrrp_instance *instance,
  280. const void *msg,
  281. unsigned int msg_len);
  282. static void passive_recv_flush (
  283. struct totemrrp_instance *instance);
  284. static void passive_send_flush (
  285. struct totemrrp_instance *instance);
  286. static void passive_iface_check (
  287. struct totemrrp_instance *instance);
  288. static void passive_processor_count_set (
  289. struct totemrrp_instance *instance,
  290. unsigned int processor_count_set);
  291. static void passive_token_target_set (
  292. struct totemrrp_instance *instance,
  293. struct totem_ip_address *token_target,
  294. unsigned int iface_no);
  295. static void passive_ring_reenable (
  296. struct totemrrp_instance *instance);
  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_recv_flush (
  339. struct totemrrp_instance *instance);
  340. static void active_send_flush (
  341. struct totemrrp_instance *instance);
  342. static void active_iface_check (
  343. struct totemrrp_instance *instance);
  344. static void active_processor_count_set (
  345. struct totemrrp_instance *instance,
  346. unsigned int processor_count_set);
  347. static void active_token_target_set (
  348. struct totemrrp_instance *instance,
  349. struct totem_ip_address *token_target,
  350. unsigned int iface_no);
  351. static void active_ring_reenable (
  352. struct totemrrp_instance *instance);
  353. static int active_mcast_recv_empty (
  354. struct totemrrp_instance *instance);
  355. static int active_member_add (
  356. struct totemrrp_instance *instance,
  357. const struct totem_ip_address *member,
  358. unsigned int iface_no);
  359. static int active_member_remove (
  360. struct totemrrp_instance *instance,
  361. const struct totem_ip_address *member,
  362. unsigned int iface_no);
  363. static void active_timer_expired_token_start (
  364. struct active_instance *active_instance);
  365. static void active_timer_expired_token_cancel (
  366. struct active_instance *active_instance);
  367. static void active_timer_problem_decrementer_start (
  368. struct active_instance *active_instance);
  369. static void active_timer_problem_decrementer_cancel (
  370. struct active_instance *active_instance);
  371. struct rrp_algo none_algo = {
  372. .name = "none",
  373. .initialize = NULL,
  374. .mcast_recv = none_mcast_recv,
  375. .mcast_noflush_send = none_mcast_noflush_send,
  376. .mcast_flush_send = none_mcast_flush_send,
  377. .token_recv = none_token_recv,
  378. .token_send = none_token_send,
  379. .recv_flush = none_recv_flush,
  380. .send_flush = none_send_flush,
  381. .iface_check = none_iface_check,
  382. .processor_count_set = none_processor_count_set,
  383. .token_target_set = none_token_target_set,
  384. .ring_reenable = none_ring_reenable,
  385. .mcast_recv_empty = none_mcast_recv_empty,
  386. .member_add = none_member_add,
  387. .member_remove = none_member_remove
  388. };
  389. struct rrp_algo passive_algo = {
  390. .name = "passive",
  391. .initialize = passive_instance_initialize,
  392. .mcast_recv = passive_mcast_recv,
  393. .mcast_noflush_send = passive_mcast_noflush_send,
  394. .mcast_flush_send = passive_mcast_flush_send,
  395. .token_recv = passive_token_recv,
  396. .token_send = passive_token_send,
  397. .recv_flush = passive_recv_flush,
  398. .send_flush = passive_send_flush,
  399. .iface_check = passive_iface_check,
  400. .processor_count_set = passive_processor_count_set,
  401. .token_target_set = passive_token_target_set,
  402. .ring_reenable = passive_ring_reenable,
  403. .mcast_recv_empty = passive_mcast_recv_empty,
  404. .member_add = passive_member_add,
  405. .member_remove = passive_member_remove
  406. };
  407. struct rrp_algo active_algo = {
  408. .name = "active",
  409. .initialize = active_instance_initialize,
  410. .mcast_recv = active_mcast_recv,
  411. .mcast_noflush_send = active_mcast_noflush_send,
  412. .mcast_flush_send = active_mcast_flush_send,
  413. .token_recv = active_token_recv,
  414. .token_send = active_token_send,
  415. .recv_flush = active_recv_flush,
  416. .send_flush = active_send_flush,
  417. .iface_check = active_iface_check,
  418. .processor_count_set = active_processor_count_set,
  419. .token_target_set = active_token_target_set,
  420. .ring_reenable = active_ring_reenable,
  421. .mcast_recv_empty = active_mcast_recv_empty,
  422. .member_add = active_member_add,
  423. .member_remove = active_member_remove
  424. };
  425. struct rrp_algo *rrp_algos[] = {
  426. &none_algo,
  427. &passive_algo,
  428. &active_algo
  429. };
  430. #define RRP_ALGOS_COUNT 3
  431. #define log_printf(level, format, args...) \
  432. do { \
  433. rrp_instance->totemrrp_log_printf ( \
  434. LOGSYS_ENCODE_RECID(level, \
  435. rrp_instance->totemrrp_subsys_id, \
  436. LOGSYS_RECID_LOG), \
  437. __FUNCTION__, __FILE__, __LINE__, \
  438. format, ##args); \
  439. } while (0);
  440. /*
  441. * None Replication Implementation
  442. */
  443. static void none_mcast_recv (
  444. struct totemrrp_instance *rrp_instance,
  445. unsigned int iface_no,
  446. void *context,
  447. const void *msg,
  448. unsigned int msg_len)
  449. {
  450. rrp_instance->totemrrp_deliver_fn (
  451. context,
  452. msg,
  453. msg_len);
  454. }
  455. static void none_mcast_flush_send (
  456. struct totemrrp_instance *instance,
  457. const void *msg,
  458. unsigned int msg_len)
  459. {
  460. totemnet_mcast_flush_send (instance->net_handles[0], msg, msg_len);
  461. }
  462. static void none_mcast_noflush_send (
  463. struct totemrrp_instance *instance,
  464. const void *msg,
  465. unsigned int msg_len)
  466. {
  467. totemnet_mcast_noflush_send (instance->net_handles[0], msg, msg_len);
  468. }
  469. static void none_token_recv (
  470. struct totemrrp_instance *rrp_instance,
  471. unsigned int iface_no,
  472. void *context,
  473. const void *msg,
  474. unsigned int msg_len,
  475. unsigned int token_seq)
  476. {
  477. rrp_instance->totemrrp_deliver_fn (
  478. context,
  479. msg,
  480. msg_len);
  481. }
  482. static void none_token_send (
  483. struct totemrrp_instance *instance,
  484. const void *msg,
  485. unsigned int msg_len)
  486. {
  487. totemnet_token_send (
  488. instance->net_handles[0],
  489. msg, msg_len);
  490. }
  491. static void none_recv_flush (struct totemrrp_instance *instance)
  492. {
  493. totemnet_recv_flush (instance->net_handles[0]);
  494. }
  495. static void none_send_flush (struct totemrrp_instance *instance)
  496. {
  497. totemnet_send_flush (instance->net_handles[0]);
  498. }
  499. static void none_iface_check (struct totemrrp_instance *instance)
  500. {
  501. totemnet_iface_check (instance->net_handles[0]);
  502. }
  503. static void none_processor_count_set (
  504. struct totemrrp_instance *instance,
  505. unsigned int processor_count)
  506. {
  507. totemnet_processor_count_set (instance->net_handles[0],
  508. processor_count);
  509. }
  510. static void none_token_target_set (
  511. struct totemrrp_instance *instance,
  512. struct totem_ip_address *token_target,
  513. unsigned int iface_no)
  514. {
  515. totemnet_token_target_set (instance->net_handles[0], token_target);
  516. }
  517. static void none_ring_reenable (
  518. struct totemrrp_instance *instance)
  519. {
  520. /*
  521. * No operation
  522. */
  523. }
  524. static int none_mcast_recv_empty (
  525. struct totemrrp_instance *instance)
  526. {
  527. int res;
  528. res = totemnet_recv_mcast_empty (instance->net_handles[0]);
  529. return (res);
  530. }
  531. static int none_member_add (
  532. struct totemrrp_instance *instance,
  533. const struct totem_ip_address *member,
  534. unsigned int iface_no)
  535. {
  536. int res;
  537. res = totemnet_member_add (instance->net_handles[0], member);
  538. return (res);
  539. }
  540. static int none_member_remove (
  541. struct totemrrp_instance *instance,
  542. const struct totem_ip_address *member,
  543. unsigned int iface_no)
  544. {
  545. int res;
  546. res = totemnet_member_remove (instance->net_handles[0], member);
  547. return (res);
  548. }
  549. /*
  550. * Passive Replication Implementation
  551. */
  552. void *passive_instance_initialize (
  553. struct totemrrp_instance *rrp_instance,
  554. int interface_count)
  555. {
  556. struct passive_instance *instance;
  557. instance = malloc (sizeof (struct passive_instance));
  558. if (instance == 0) {
  559. goto error_exit;
  560. }
  561. memset (instance, 0, sizeof (struct passive_instance));
  562. instance->faulty = malloc (sizeof (int) * interface_count);
  563. if (instance->faulty == 0) {
  564. free (instance);
  565. instance = 0;
  566. goto error_exit;
  567. }
  568. memset (instance->faulty, 0, sizeof (int) * interface_count);
  569. instance->token_recv_count = malloc (sizeof (int) * interface_count);
  570. if (instance->token_recv_count == 0) {
  571. free (instance->faulty);
  572. free (instance);
  573. instance = 0;
  574. goto error_exit;
  575. }
  576. memset (instance->token_recv_count, 0, sizeof (int) * interface_count);
  577. instance->mcast_recv_count = malloc (sizeof (int) * interface_count);
  578. if (instance->mcast_recv_count == 0) {
  579. free (instance->token_recv_count);
  580. free (instance->faulty);
  581. free (instance);
  582. instance = 0;
  583. goto error_exit;
  584. }
  585. memset (instance->mcast_recv_count, 0, sizeof (int) * interface_count);
  586. error_exit:
  587. return ((void *)instance);
  588. }
  589. static void timer_function_passive_token_expired (void *context)
  590. {
  591. struct passive_instance *passive_instance = (struct passive_instance *)context;
  592. struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  593. rrp_instance->totemrrp_deliver_fn (
  594. passive_instance->totemrrp_context,
  595. passive_instance->token,
  596. passive_instance->token_len);
  597. }
  598. /* TODO
  599. static void timer_function_passive_problem_decrementer (void *context)
  600. {
  601. // struct passive_instance *passive_instance = (struct passive_instance *)context;
  602. // struct totemrrp_instance *rrp_instance = passive_instance->rrp_instance;
  603. }
  604. */
  605. static void passive_timer_expired_token_start (
  606. struct passive_instance *passive_instance)
  607. {
  608. poll_timer_add (
  609. passive_instance->rrp_instance->poll_handle,
  610. passive_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  611. (void *)passive_instance,
  612. timer_function_passive_token_expired,
  613. &passive_instance->timer_expired_token);
  614. }
  615. static void passive_timer_expired_token_cancel (
  616. struct passive_instance *passive_instance)
  617. {
  618. poll_timer_delete (
  619. passive_instance->rrp_instance->poll_handle,
  620. passive_instance->timer_expired_token);
  621. }
  622. /*
  623. static void passive_timer_problem_decrementer_start (
  624. struct passive_instance *passive_instance)
  625. {
  626. poll_timer_add (
  627. passive_instance->rrp_instance->poll_handle,
  628. passive_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  629. (void *)passive_instance,
  630. timer_function_passive_problem_decrementer,
  631. &passive_instance->timer_problem_decrementer);
  632. }
  633. static void passive_timer_problem_decrementer_cancel (
  634. struct passive_instance *passive_instance)
  635. {
  636. poll_timer_delete (
  637. passive_instance->rrp_instance->poll_handle,
  638. passive_instance->timer_problem_decrementer);
  639. }
  640. */
  641. static void passive_mcast_recv (
  642. struct totemrrp_instance *rrp_instance,
  643. unsigned int iface_no,
  644. void *context,
  645. const void *msg,
  646. unsigned int msg_len)
  647. {
  648. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  649. unsigned int max;
  650. unsigned int i;
  651. rrp_instance->totemrrp_deliver_fn (
  652. context,
  653. msg,
  654. msg_len);
  655. if (rrp_instance->totemrrp_msgs_missing() == 0 &&
  656. passive_instance->timer_expired_token) {
  657. /*
  658. * Delivers the last token
  659. */
  660. rrp_instance->totemrrp_deliver_fn (
  661. passive_instance->totemrrp_context,
  662. passive_instance->token,
  663. passive_instance->token_len);
  664. passive_timer_expired_token_cancel (passive_instance);
  665. }
  666. /*
  667. * Monitor for failures
  668. * TODO doesn't handle wrap-around of the mcast recv count
  669. */
  670. passive_instance->mcast_recv_count[iface_no] += 1;
  671. max = 0;
  672. for (i = 0; i < rrp_instance->interface_count; i++) {
  673. if (max < passive_instance->mcast_recv_count[i]) {
  674. max = passive_instance->mcast_recv_count[i];
  675. }
  676. }
  677. for (i = 0; i < rrp_instance->interface_count; i++) {
  678. if ((passive_instance->faulty[i] == 0) &&
  679. (max - passive_instance->mcast_recv_count[i] >
  680. rrp_instance->totem_config->rrp_problem_count_threshold)) {
  681. passive_instance->faulty[i] = 1;
  682. sprintf (rrp_instance->status[i],
  683. "Marking ringid %u interface %s FAULTY - adminisrtative intervention required.",
  684. i,
  685. totemnet_iface_print (rrp_instance->net_handles[i]));
  686. log_printf (
  687. rrp_instance->totemrrp_log_level_error,
  688. "%s",
  689. rrp_instance->status[i]);
  690. }
  691. }
  692. }
  693. static void passive_mcast_flush_send (
  694. struct totemrrp_instance *instance,
  695. const void *msg,
  696. unsigned int msg_len)
  697. {
  698. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  699. do {
  700. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  701. } while (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1);
  702. totemnet_mcast_flush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  703. }
  704. static void passive_mcast_noflush_send (
  705. struct totemrrp_instance *instance,
  706. const void *msg,
  707. unsigned int msg_len)
  708. {
  709. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  710. do {
  711. passive_instance->msg_xmit_iface = (passive_instance->msg_xmit_iface + 1) % instance->interface_count;
  712. } while (passive_instance->faulty[passive_instance->msg_xmit_iface] == 1);
  713. totemnet_mcast_noflush_send (instance->net_handles[passive_instance->msg_xmit_iface], msg, msg_len);
  714. }
  715. static void passive_token_recv (
  716. struct totemrrp_instance *rrp_instance,
  717. unsigned int iface_no,
  718. void *context,
  719. const void *msg,
  720. unsigned int msg_len,
  721. unsigned int token_seq)
  722. {
  723. struct passive_instance *passive_instance = (struct passive_instance *)rrp_instance->rrp_algo_instance;
  724. unsigned int max;
  725. unsigned int i;
  726. passive_instance->totemrrp_context = context; // this should be in totemrrp_instance ? TODO
  727. if (rrp_instance->totemrrp_msgs_missing() == 0) {
  728. rrp_instance->totemrrp_deliver_fn (
  729. context,
  730. msg,
  731. msg_len);
  732. } else {
  733. memcpy (passive_instance->token, msg, msg_len);
  734. passive_timer_expired_token_start (passive_instance);
  735. }
  736. /*
  737. * Monitor for failures
  738. * TODO doesn't handle wrap-around of the token
  739. */
  740. passive_instance->token_recv_count[iface_no] += 1;
  741. max = 0;
  742. for (i = 0; i < rrp_instance->interface_count; i++) {
  743. if (max < passive_instance->token_recv_count[i]) {
  744. max = passive_instance->token_recv_count[i];
  745. }
  746. }
  747. for (i = 0; i < rrp_instance->interface_count; i++) {
  748. if ((passive_instance->faulty[i] == 0) &&
  749. (max - passive_instance->token_recv_count[i] >
  750. rrp_instance->totem_config->rrp_problem_count_threshold)) {
  751. passive_instance->faulty[i] = 1;
  752. sprintf (rrp_instance->status[i],
  753. "Marking seqid %d ringid %u interface %s FAULTY - adminisrtative intervention required.",
  754. token_seq,
  755. i,
  756. totemnet_iface_print (rrp_instance->net_handles[i]));
  757. log_printf (
  758. rrp_instance->totemrrp_log_level_error,
  759. "%s",
  760. rrp_instance->status[i]);
  761. }
  762. }
  763. }
  764. static void passive_token_send (
  765. struct totemrrp_instance *instance,
  766. const void *msg,
  767. unsigned int msg_len)
  768. {
  769. struct passive_instance *passive_instance = (struct passive_instance *)instance->rrp_algo_instance;
  770. do {
  771. passive_instance->token_xmit_iface = (passive_instance->token_xmit_iface + 1) % instance->interface_count;
  772. } while (passive_instance->faulty[passive_instance->token_xmit_iface] == 1);
  773. totemnet_token_send (
  774. instance->net_handles[passive_instance->token_xmit_iface],
  775. msg, msg_len);
  776. }
  777. static void passive_recv_flush (struct totemrrp_instance *instance)
  778. {
  779. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  780. unsigned int i;
  781. for (i = 0; i < instance->interface_count; i++) {
  782. if (rrp_algo_instance->faulty[i] == 0) {
  783. totemnet_recv_flush (instance->net_handles[i]);
  784. }
  785. }
  786. }
  787. static void passive_send_flush (struct totemrrp_instance *instance)
  788. {
  789. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  790. unsigned int i;
  791. for (i = 0; i < instance->interface_count; i++) {
  792. if (rrp_algo_instance->faulty[i] == 0) {
  793. totemnet_send_flush (instance->net_handles[i]);
  794. }
  795. }
  796. }
  797. static void passive_iface_check (struct totemrrp_instance *instance)
  798. {
  799. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  800. unsigned int i;
  801. for (i = 0; i < instance->interface_count; i++) {
  802. if (rrp_algo_instance->faulty[i] == 0) {
  803. totemnet_iface_check (instance->net_handles[i]);
  804. }
  805. }
  806. }
  807. static void passive_processor_count_set (
  808. struct totemrrp_instance *instance,
  809. unsigned int processor_count)
  810. {
  811. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  812. unsigned int i;
  813. for (i = 0; i < instance->interface_count; i++) {
  814. if (rrp_algo_instance->faulty[i] == 0) {
  815. totemnet_processor_count_set (instance->net_handles[i],
  816. processor_count);
  817. }
  818. }
  819. }
  820. static void passive_token_target_set (
  821. struct totemrrp_instance *instance,
  822. struct totem_ip_address *token_target,
  823. unsigned int iface_no)
  824. {
  825. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  826. }
  827. static int passive_mcast_recv_empty (
  828. struct totemrrp_instance *instance)
  829. {
  830. int res;
  831. int msgs_emptied = 0;
  832. int i;
  833. for (i = 0; i < instance->interface_count; i++) {
  834. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  835. if (res == -1) {
  836. return (-1);
  837. }
  838. if (res == 1) {
  839. msgs_emptied = 1;
  840. }
  841. }
  842. return (msgs_emptied);
  843. }
  844. static int passive_member_add (
  845. struct totemrrp_instance *instance,
  846. const struct totem_ip_address *member,
  847. unsigned int iface_no)
  848. {
  849. int res;
  850. res = totemnet_member_add (instance->net_handles[iface_no], member);
  851. return (res);
  852. }
  853. static int passive_member_remove (
  854. struct totemrrp_instance *instance,
  855. const struct totem_ip_address *member,
  856. unsigned int iface_no)
  857. {
  858. int res;
  859. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  860. return (res);
  861. }
  862. static void passive_ring_reenable (
  863. struct totemrrp_instance *instance)
  864. {
  865. struct passive_instance *rrp_algo_instance = (struct passive_instance *)instance->rrp_algo_instance;
  866. memset (rrp_algo_instance->mcast_recv_count, 0, sizeof (unsigned int) *
  867. instance->interface_count);
  868. memset (rrp_algo_instance->token_recv_count, 0, sizeof (unsigned int) *
  869. instance->interface_count);
  870. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  871. instance->interface_count);
  872. }
  873. /*
  874. * Active Replication Implementation
  875. */
  876. void *active_instance_initialize (
  877. struct totemrrp_instance *rrp_instance,
  878. int interface_count)
  879. {
  880. struct active_instance *instance;
  881. instance = malloc (sizeof (struct active_instance));
  882. if (instance == 0) {
  883. goto error_exit;
  884. }
  885. memset (instance, 0, sizeof (struct active_instance));
  886. instance->faulty = malloc (sizeof (int) * interface_count);
  887. if (instance->faulty == 0) {
  888. free (instance);
  889. instance = 0;
  890. goto error_exit;
  891. }
  892. memset (instance->faulty, 0, sizeof (unsigned int) * interface_count);
  893. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  894. if (instance->last_token_recv == 0) {
  895. free (instance->faulty);
  896. free (instance);
  897. instance = 0;
  898. goto error_exit;
  899. }
  900. memset (instance->last_token_recv, 0, sizeof (unsigned int) * interface_count);
  901. instance->counter_problems = malloc (sizeof (int) * interface_count);
  902. if (instance->counter_problems == 0) {
  903. free (instance->last_token_recv);
  904. free (instance->faulty);
  905. free (instance);
  906. instance = 0;
  907. goto error_exit;
  908. }
  909. memset (instance->counter_problems, 0, sizeof (unsigned int) * interface_count);
  910. instance->timer_expired_token = 0;
  911. instance->timer_problem_decrementer = 0;
  912. instance->rrp_instance = rrp_instance;
  913. error_exit:
  914. return ((void *)instance);
  915. }
  916. static void timer_function_active_problem_decrementer (void *context)
  917. {
  918. struct active_instance *active_instance = (struct active_instance *)context;
  919. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  920. unsigned int problem_found = 0;
  921. unsigned int i;
  922. for (i = 0; i < rrp_instance->interface_count; i++) {
  923. if (active_instance->counter_problems[i] > 0) {
  924. problem_found = 1;
  925. active_instance->counter_problems[i] -= 1;
  926. if (active_instance->counter_problems[i] == 0) {
  927. sprintf (rrp_instance->status[i],
  928. "ring %d active with no faults", i);
  929. } else {
  930. sprintf (rrp_instance->status[i],
  931. "Decrementing problem counter for iface %s to [%d of %d]",
  932. totemnet_iface_print (rrp_instance->net_handles[i]),
  933. active_instance->counter_problems[i],
  934. rrp_instance->totem_config->rrp_problem_count_threshold);
  935. }
  936. log_printf (
  937. rrp_instance->totemrrp_log_level_warning,
  938. "%s",
  939. rrp_instance->status[i]);
  940. }
  941. }
  942. if (problem_found) {
  943. active_timer_problem_decrementer_start (active_instance);
  944. } else {
  945. active_instance->timer_problem_decrementer = 0;
  946. }
  947. }
  948. static void timer_function_active_token_expired (void *context)
  949. {
  950. struct active_instance *active_instance = (struct active_instance *)context;
  951. struct totemrrp_instance *rrp_instance = active_instance->rrp_instance;
  952. unsigned int i;
  953. for (i = 0; i < rrp_instance->interface_count; i++) {
  954. if (active_instance->last_token_recv[i] == 0) {
  955. active_instance->counter_problems[i] += 1;
  956. if (active_instance->timer_problem_decrementer == 0) {
  957. active_timer_problem_decrementer_start (active_instance);
  958. }
  959. sprintf (rrp_instance->status[i],
  960. "Incrementing problem counter for seqid %d iface %s to [%d of %d]",
  961. active_instance->last_token_seq,
  962. totemnet_iface_print (rrp_instance->net_handles[i]),
  963. active_instance->counter_problems[i],
  964. rrp_instance->totem_config->rrp_problem_count_threshold);
  965. log_printf (
  966. rrp_instance->totemrrp_log_level_warning,
  967. "%s",
  968. rrp_instance->status[i]);
  969. }
  970. }
  971. for (i = 0; i < rrp_instance->interface_count; i++) {
  972. if (active_instance->counter_problems[i] >= rrp_instance->totem_config->rrp_problem_count_threshold)
  973. {
  974. active_instance->faulty[i] = 1;
  975. sprintf (rrp_instance->status[i],
  976. "Marking seqid %d ringid %u interface %s FAULTY - adminisrtative intervention required.",
  977. active_instance->last_token_seq,
  978. i,
  979. totemnet_iface_print (rrp_instance->net_handles[i]));
  980. log_printf (
  981. rrp_instance->totemrrp_log_level_error,
  982. "%s",
  983. rrp_instance->status[i]);
  984. active_timer_problem_decrementer_cancel (active_instance);
  985. }
  986. }
  987. rrp_instance->totemrrp_deliver_fn (
  988. active_instance->totemrrp_context,
  989. active_instance->token,
  990. active_instance->token_len);
  991. }
  992. static void active_timer_expired_token_start (
  993. struct active_instance *active_instance)
  994. {
  995. poll_timer_add (
  996. active_instance->rrp_instance->poll_handle,
  997. active_instance->rrp_instance->totem_config->rrp_token_expired_timeout,
  998. (void *)active_instance,
  999. timer_function_active_token_expired,
  1000. &active_instance->timer_expired_token);
  1001. }
  1002. static void active_timer_expired_token_cancel (
  1003. struct active_instance *active_instance)
  1004. {
  1005. poll_timer_delete (
  1006. active_instance->rrp_instance->poll_handle,
  1007. active_instance->timer_expired_token);
  1008. }
  1009. static void active_timer_problem_decrementer_start (
  1010. struct active_instance *active_instance)
  1011. {
  1012. poll_timer_add (
  1013. active_instance->rrp_instance->poll_handle,
  1014. active_instance->rrp_instance->totem_config->rrp_problem_count_timeout,
  1015. (void *)active_instance,
  1016. timer_function_active_problem_decrementer,
  1017. &active_instance->timer_problem_decrementer);
  1018. }
  1019. static void active_timer_problem_decrementer_cancel (
  1020. struct active_instance *active_instance)
  1021. {
  1022. poll_timer_delete (
  1023. active_instance->rrp_instance->poll_handle,
  1024. active_instance->timer_problem_decrementer);
  1025. }
  1026. /*
  1027. * active replication
  1028. */
  1029. static void active_mcast_recv (
  1030. struct totemrrp_instance *instance,
  1031. unsigned int iface_no,
  1032. void *context,
  1033. const void *msg,
  1034. unsigned int msg_len)
  1035. {
  1036. instance->totemrrp_deliver_fn (
  1037. context,
  1038. msg,
  1039. msg_len);
  1040. }
  1041. static void active_mcast_flush_send (
  1042. struct totemrrp_instance *instance,
  1043. const void *msg,
  1044. unsigned int msg_len)
  1045. {
  1046. int i;
  1047. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1048. for (i = 0; i < instance->interface_count; i++) {
  1049. if (rrp_algo_instance->faulty[i] == 0) {
  1050. totemnet_mcast_flush_send (instance->net_handles[i], msg, msg_len);
  1051. }
  1052. }
  1053. }
  1054. static void active_mcast_noflush_send (
  1055. struct totemrrp_instance *instance,
  1056. const void *msg,
  1057. unsigned int msg_len)
  1058. {
  1059. int i;
  1060. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1061. for (i = 0; i < instance->interface_count; i++) {
  1062. if (rrp_algo_instance->faulty[i] == 0) {
  1063. totemnet_mcast_noflush_send (instance->net_handles[i], msg, msg_len);
  1064. }
  1065. }
  1066. }
  1067. static void active_token_recv (
  1068. struct totemrrp_instance *instance,
  1069. unsigned int iface_no,
  1070. void *context,
  1071. const void *msg,
  1072. unsigned int msg_len,
  1073. unsigned int token_seq)
  1074. {
  1075. int i;
  1076. struct active_instance *active_instance = (struct active_instance *)instance->rrp_algo_instance;
  1077. active_instance->totemrrp_context = context; // this should be in totemrrp_instance ?
  1078. if (token_seq > active_instance->last_token_seq) {
  1079. memcpy (active_instance->token, msg, msg_len);
  1080. active_instance->token_len = msg_len;
  1081. for (i = 0; i < instance->interface_count; i++) {
  1082. active_instance->last_token_recv[i] = 0;
  1083. }
  1084. active_instance->last_token_recv[iface_no] = 1;
  1085. active_timer_expired_token_start (active_instance);
  1086. }
  1087. active_instance->last_token_seq = token_seq;
  1088. if (token_seq == active_instance->last_token_seq) {
  1089. active_instance->last_token_recv[iface_no] = 1;
  1090. for (i = 0; i < instance->interface_count; i++) {
  1091. if ((active_instance->last_token_recv[i] == 0) &&
  1092. active_instance->faulty[i] == 0) {
  1093. return; /* don't deliver token */
  1094. }
  1095. }
  1096. active_timer_expired_token_cancel (active_instance);
  1097. instance->totemrrp_deliver_fn (
  1098. context,
  1099. msg,
  1100. msg_len);
  1101. }
  1102. }
  1103. static void active_token_send (
  1104. struct totemrrp_instance *instance,
  1105. const void *msg,
  1106. unsigned int msg_len)
  1107. {
  1108. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1109. int i;
  1110. for (i = 0; i < instance->interface_count; i++) {
  1111. if (rrp_algo_instance->faulty[i] == 0) {
  1112. totemnet_token_send (
  1113. instance->net_handles[i],
  1114. msg, msg_len);
  1115. }
  1116. }
  1117. }
  1118. static void active_recv_flush (struct totemrrp_instance *instance)
  1119. {
  1120. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1121. unsigned int i;
  1122. for (i = 0; i < instance->interface_count; i++) {
  1123. if (rrp_algo_instance->faulty[i] == 0) {
  1124. totemnet_recv_flush (instance->net_handles[i]);
  1125. }
  1126. }
  1127. }
  1128. static void active_send_flush (struct totemrrp_instance *instance)
  1129. {
  1130. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1131. unsigned int i;
  1132. for (i = 0; i < instance->interface_count; i++) {
  1133. if (rrp_algo_instance->faulty[i] == 0) {
  1134. totemnet_send_flush (instance->net_handles[i]);
  1135. }
  1136. }
  1137. }
  1138. static int active_member_add (
  1139. struct totemrrp_instance *instance,
  1140. const struct totem_ip_address *member,
  1141. unsigned int iface_no)
  1142. {
  1143. int res;
  1144. res = totemnet_member_add (instance->net_handles[iface_no], member);
  1145. return (res);
  1146. }
  1147. static int active_member_remove (
  1148. struct totemrrp_instance *instance,
  1149. const struct totem_ip_address *member,
  1150. unsigned int iface_no)
  1151. {
  1152. int res;
  1153. res = totemnet_member_remove (instance->net_handles[iface_no], member);
  1154. return (res);
  1155. }
  1156. static void active_iface_check (struct totemrrp_instance *instance)
  1157. {
  1158. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1159. unsigned int i;
  1160. for (i = 0; i < instance->interface_count; i++) {
  1161. if (rrp_algo_instance->faulty[i] == 0) {
  1162. totemnet_iface_check (instance->net_handles[i]);
  1163. }
  1164. }
  1165. }
  1166. static void active_processor_count_set (
  1167. struct totemrrp_instance *instance,
  1168. unsigned int processor_count)
  1169. {
  1170. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1171. unsigned int i;
  1172. for (i = 0; i < instance->interface_count; i++) {
  1173. if (rrp_algo_instance->faulty[i] == 0) {
  1174. totemnet_processor_count_set (instance->net_handles[i],
  1175. processor_count);
  1176. }
  1177. }
  1178. }
  1179. static void active_token_target_set (
  1180. struct totemrrp_instance *instance,
  1181. struct totem_ip_address *token_target,
  1182. unsigned int iface_no)
  1183. {
  1184. totemnet_token_target_set (instance->net_handles[iface_no], token_target);
  1185. }
  1186. static int active_mcast_recv_empty (
  1187. struct totemrrp_instance *instance)
  1188. {
  1189. int res;
  1190. int msgs_emptied = 0;
  1191. int i;
  1192. for (i = 0; i < instance->interface_count; i++) {
  1193. res = totemnet_recv_mcast_empty (instance->net_handles[i]);
  1194. if (res == -1) {
  1195. return (-1);
  1196. }
  1197. if (res == 1) {
  1198. msgs_emptied = 1;
  1199. }
  1200. }
  1201. return (msgs_emptied);
  1202. }
  1203. static void active_ring_reenable (
  1204. struct totemrrp_instance *instance)
  1205. {
  1206. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  1207. memset (rrp_algo_instance->last_token_recv, 0, sizeof (unsigned int) *
  1208. instance->interface_count);
  1209. memset (rrp_algo_instance->faulty, 0, sizeof (unsigned int) *
  1210. instance->interface_count);
  1211. memset (rrp_algo_instance->counter_problems, 0, sizeof (unsigned int) *
  1212. instance->interface_count);
  1213. }
  1214. struct deliver_fn_context {
  1215. struct totemrrp_instance *instance;
  1216. void *context;
  1217. int iface_no;
  1218. };
  1219. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  1220. {
  1221. memset (instance, 0, sizeof (struct totemrrp_instance));
  1222. }
  1223. static int totemrrp_algorithm_set (
  1224. struct totem_config *totem_config,
  1225. struct totemrrp_instance *instance)
  1226. {
  1227. unsigned int res = -1;
  1228. unsigned int i;
  1229. for (i = 0; i < RRP_ALGOS_COUNT; i++) {
  1230. if (strcmp (totem_config->rrp_mode, rrp_algos[i]->name) == 0) {
  1231. instance->rrp_algo = rrp_algos[i];
  1232. if (rrp_algos[i]->initialize) {
  1233. instance->rrp_algo_instance = rrp_algos[i]->initialize (
  1234. instance,
  1235. totem_config->interface_count);
  1236. }
  1237. res = 0;
  1238. break;
  1239. }
  1240. }
  1241. for (i = 0; i < totem_config->interface_count; i++) {
  1242. instance->status[i] = malloc (1024);
  1243. sprintf (instance->status[i], "ring %d active with no faults", i);
  1244. }
  1245. return (res);
  1246. }
  1247. void rrp_deliver_fn (
  1248. void *context,
  1249. const void *msg,
  1250. unsigned int msg_len)
  1251. {
  1252. unsigned int token_seqid;
  1253. unsigned int token_is;
  1254. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1255. deliver_fn_context->instance->totemrrp_token_seqid_get (
  1256. msg,
  1257. &token_seqid,
  1258. &token_is);
  1259. if (token_is) {
  1260. /*
  1261. * Deliver to the token receiver for this rrp algorithm
  1262. */
  1263. deliver_fn_context->instance->rrp_algo->token_recv (
  1264. deliver_fn_context->instance,
  1265. deliver_fn_context->iface_no,
  1266. deliver_fn_context->context,
  1267. msg,
  1268. msg_len,
  1269. token_seqid);
  1270. } else {
  1271. /*
  1272. * Deliver to the mcast receiver for this rrp algorithm
  1273. */
  1274. deliver_fn_context->instance->rrp_algo->mcast_recv (
  1275. deliver_fn_context->instance,
  1276. deliver_fn_context->iface_no,
  1277. deliver_fn_context->context,
  1278. msg,
  1279. msg_len);
  1280. }
  1281. }
  1282. void rrp_iface_change_fn (
  1283. void *context,
  1284. const struct totem_ip_address *iface_addr)
  1285. {
  1286. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1287. deliver_fn_context->instance->totemrrp_iface_change_fn (
  1288. deliver_fn_context->context,
  1289. iface_addr,
  1290. deliver_fn_context->iface_no);
  1291. }
  1292. int totemrrp_finalize (
  1293. void *rrp_context)
  1294. {
  1295. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1296. int i;
  1297. for (i = 0; i < instance->interface_count; i++) {
  1298. totemnet_finalize (instance->net_handles[i]);
  1299. }
  1300. return (0);
  1301. }
  1302. static void rrp_target_set_completed (void *context)
  1303. {
  1304. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  1305. deliver_fn_context->instance->totemrrp_target_set_completed (deliver_fn_context->context);
  1306. }
  1307. /*
  1308. * Totem Redundant Ring interface
  1309. * depends on poll abstraction, POSIX, IPV4
  1310. */
  1311. /*
  1312. * Create an instance
  1313. */
  1314. int totemrrp_initialize (
  1315. hdb_handle_t poll_handle,
  1316. void **rrp_context,
  1317. struct totem_config *totem_config,
  1318. void *context,
  1319. void (*deliver_fn) (
  1320. void *context,
  1321. const void *msg,
  1322. unsigned int msg_len),
  1323. void (*iface_change_fn) (
  1324. void *context,
  1325. const struct totem_ip_address *iface_addr,
  1326. unsigned int iface_no),
  1327. void (*token_seqid_get) (
  1328. const void *msg,
  1329. unsigned int *seqid,
  1330. unsigned int *token_is),
  1331. unsigned int (*msgs_missing) (void),
  1332. void (*target_set_completed) (void *context))
  1333. {
  1334. struct totemrrp_instance *instance;
  1335. unsigned int res;
  1336. int i;
  1337. instance = malloc (sizeof (struct totemrrp_instance));
  1338. if (instance == 0) {
  1339. return (-1);
  1340. }
  1341. totemrrp_instance_initialize (instance);
  1342. instance->totem_config = totem_config;
  1343. res = totemrrp_algorithm_set (
  1344. instance->totem_config,
  1345. instance);
  1346. if (res == -1) {
  1347. goto error_destroy;
  1348. }
  1349. /*
  1350. * Configure logging
  1351. */
  1352. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  1353. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1354. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1355. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1356. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1357. instance->totemrrp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1358. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1359. instance->interfaces = totem_config->interfaces;
  1360. instance->poll_handle = poll_handle;
  1361. instance->totemrrp_deliver_fn = deliver_fn;
  1362. instance->totemrrp_iface_change_fn = iface_change_fn;
  1363. instance->totemrrp_token_seqid_get = token_seqid_get;
  1364. instance->totemrrp_target_set_completed = target_set_completed;
  1365. instance->totemrrp_msgs_missing = msgs_missing;
  1366. instance->interface_count = totem_config->interface_count;
  1367. instance->net_handles = malloc (sizeof (void *) * totem_config->interface_count);
  1368. instance->context = context;
  1369. instance->poll_handle = poll_handle;
  1370. for (i = 0; i < totem_config->interface_count; i++) {
  1371. struct deliver_fn_context *deliver_fn_context;
  1372. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  1373. assert (deliver_fn_context);
  1374. deliver_fn_context->instance = instance;
  1375. deliver_fn_context->context = context;
  1376. deliver_fn_context->iface_no = i;
  1377. totemnet_initialize (
  1378. poll_handle,
  1379. &instance->net_handles[i],
  1380. totem_config,
  1381. i,
  1382. (void *)deliver_fn_context,
  1383. rrp_deliver_fn,
  1384. rrp_iface_change_fn,
  1385. rrp_target_set_completed);
  1386. totemnet_net_mtu_adjust (instance->net_handles[i], totem_config);
  1387. }
  1388. *rrp_context = instance;
  1389. return (0);
  1390. error_destroy:
  1391. free (instance);
  1392. return (res);
  1393. }
  1394. int totemrrp_processor_count_set (
  1395. void *rrp_context,
  1396. unsigned int processor_count)
  1397. {
  1398. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1399. instance->rrp_algo->processor_count_set (instance, processor_count);
  1400. instance->processor_count = processor_count;
  1401. return (0);
  1402. }
  1403. int totemrrp_token_target_set (
  1404. void *rrp_context,
  1405. struct totem_ip_address *addr,
  1406. unsigned int iface_no)
  1407. {
  1408. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1409. instance->rrp_algo->token_target_set (instance, addr, iface_no);
  1410. return (0);
  1411. }
  1412. int totemrrp_recv_flush (void *rrp_context)
  1413. {
  1414. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1415. instance->rrp_algo->recv_flush (instance);
  1416. return (0);
  1417. }
  1418. int totemrrp_send_flush (void *rrp_context)
  1419. {
  1420. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1421. instance->rrp_algo->send_flush (instance);
  1422. return (0);
  1423. }
  1424. int totemrrp_token_send (
  1425. void *rrp_context,
  1426. const void *msg,
  1427. unsigned int msg_len)
  1428. {
  1429. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1430. instance->rrp_algo->token_send (instance, msg, msg_len);
  1431. return (0);
  1432. }
  1433. int totemrrp_mcast_flush_send (
  1434. void *rrp_context,
  1435. const void *msg,
  1436. unsigned int msg_len)
  1437. {
  1438. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1439. int res = 0;
  1440. // TODO this needs to return the result
  1441. instance->rrp_algo->mcast_flush_send (instance, msg, msg_len);
  1442. return (res);
  1443. }
  1444. int totemrrp_mcast_noflush_send (
  1445. void *rrp_context,
  1446. const void *msg,
  1447. unsigned int msg_len)
  1448. {
  1449. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1450. /*
  1451. * merge detects go out through mcast_flush_send so it is safe to
  1452. * flush these messages if we are only one processor. This avoids
  1453. * an encryption/hmac and decryption/hmac
  1454. */
  1455. if (instance->processor_count > 1) {
  1456. // TODO this needs to return the result
  1457. instance->rrp_algo->mcast_noflush_send (instance, msg, msg_len);
  1458. }
  1459. return (0);
  1460. }
  1461. int totemrrp_iface_check (void *rrp_context)
  1462. {
  1463. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1464. instance->rrp_algo->iface_check (instance);
  1465. return (0);
  1466. }
  1467. int totemrrp_ifaces_get (
  1468. void *rrp_context,
  1469. char ***status,
  1470. unsigned int *iface_count)
  1471. {
  1472. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1473. *status = instance->status;
  1474. if (iface_count) {
  1475. *iface_count = instance->interface_count;
  1476. }
  1477. return (0);
  1478. }
  1479. int totemrrp_crypto_set (
  1480. void *rrp_context,
  1481. unsigned int type)
  1482. {
  1483. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1484. int res;
  1485. res = totemnet_crypto_set(instance->net_handles[0], type);
  1486. return (res);
  1487. }
  1488. int totemrrp_ring_reenable (
  1489. void *rrp_context)
  1490. {
  1491. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1492. int res = 0;
  1493. unsigned int i;
  1494. instance->rrp_algo->ring_reenable (instance);
  1495. for (i = 0; i < instance->interface_count; i++) {
  1496. sprintf (instance->status[i], "ring %d active with no faults", i);
  1497. }
  1498. return (res);
  1499. }
  1500. extern int totemrrp_mcast_recv_empty (
  1501. void *rrp_context)
  1502. {
  1503. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1504. int res;
  1505. res = instance->rrp_algo->mcast_recv_empty (instance);
  1506. return (res);
  1507. }
  1508. int totemrrp_member_add (
  1509. void *rrp_context,
  1510. const struct totem_ip_address *member,
  1511. int iface_no)
  1512. {
  1513. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1514. int res;
  1515. res = instance->rrp_algo->member_add (instance, member, iface_no);
  1516. return (res);
  1517. }
  1518. int totemrrp_member_remove (
  1519. void *rrp_context,
  1520. const struct totem_ip_address *member,
  1521. int iface_no)
  1522. {
  1523. struct totemrrp_instance *instance = (struct totemrrp_instance *)rrp_context;
  1524. int res;
  1525. res = instance->rrp_algo->member_remove (instance, member, iface_no);
  1526. return (res);
  1527. }