totemrrp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <assert.h>
  35. #include <pthread.h>
  36. #include <sys/mman.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/param.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <signal.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <sys/time.h>
  55. #include <sys/poll.h>
  56. #include "../include/queue.h"
  57. #include "../include/sq.h"
  58. #include "../include/list.h"
  59. #include "../include/hdb.h"
  60. #include "swab.h"
  61. #include "aispoll.h"
  62. #include "totemnet.h"
  63. #include "totemrrp.h"
  64. struct totemrrp_instance;
  65. struct passive_instance {
  66. unsigned int *faulty;
  67. unsigned int *last_seq;
  68. unsigned int *counter_problems;
  69. unsigned char token[15000];
  70. unsigned int token_len;
  71. };
  72. struct active_instance {
  73. struct totemrrp_instance *rrp_instance;
  74. unsigned int *faulty;
  75. unsigned int *last_token_recv;
  76. unsigned int *counter_problems;
  77. unsigned char token[15000];
  78. unsigned int token_len;
  79. poll_timer_handle timer_active_token;
  80. };
  81. struct rrp_algo {
  82. void (*mcast_recv) (
  83. struct totemrrp_instance *instance,
  84. void *context,
  85. struct totem_ip_address *system_from,
  86. void *msg,
  87. unsigned int msg_len);
  88. void (*mcast_noflush_send) (
  89. struct totemrrp_instance *instance,
  90. struct iovec *iovec,
  91. unsigned int iov_len);
  92. void (*mcast_flush_send) (
  93. struct totemrrp_instance *instance,
  94. struct iovec *iovec,
  95. unsigned int iov_len);
  96. void (*token_recv) (
  97. struct totemrrp_instance *instance,
  98. unsigned int interface_no,
  99. void *context,
  100. struct totem_ip_address *system_from,
  101. void *msg,
  102. unsigned int msg_len,
  103. unsigned int token_seqid);
  104. void (*token_send) (
  105. struct totemrrp_instance *instance,
  106. struct totem_ip_address *system_to,
  107. struct iovec *iovec,
  108. unsigned int iov_len);
  109. void (*recv_flush) (
  110. struct totemrrp_instance *instance);
  111. void (*send_flush) (
  112. struct totemrrp_instance *instance);
  113. void (*iface_check) (
  114. struct totemrrp_instance *instance);
  115. void (*processor_count_set) (
  116. struct totemrrp_instance *instance,
  117. unsigned int processor_count);
  118. };
  119. struct totemrrp_instance {
  120. poll_handle totemrrp_poll_handle;
  121. struct totem_interface *totemrrp_interfaces;
  122. struct rrp_algo *rrp_algo;
  123. void *context;
  124. void (*totemrrp_deliver_fn) (
  125. void *context,
  126. struct totem_ip_address *system_from,
  127. void *msg,
  128. int msg_len);
  129. void (*totemrrp_iface_change_fn) (
  130. void *context,
  131. struct totem_ip_address *iface_addr);
  132. void (*totemrrp_token_seqid_get) (
  133. void *msg,
  134. unsigned int *seqid,
  135. unsigned int *token_is);
  136. /*
  137. * Function and data used to log messages
  138. */
  139. int totemrrp_log_level_security;
  140. int totemrrp_log_level_error;
  141. int totemrrp_log_level_warning;
  142. int totemrrp_log_level_notice;
  143. int totemrrp_log_level_debug;
  144. void (*totemrrp_log_printf) (char *file, int line, int level, char *format, ...);
  145. totemrrp_handle handle;
  146. totemnet_handle *net_handles;
  147. void *rrp_algo_instance;
  148. int interface_count;
  149. int poll_handle;
  150. int processor_count;
  151. };
  152. #ifdef TODO
  153. static void passive_mcast_recv (
  154. struct totemrrp_instance *instance,
  155. void *context,
  156. struct totem_ip_address *system_from,
  157. void *msg,
  158. unsigned int msg_len);
  159. static void passive_mcast_noflush_send (
  160. struct totemrrp_instance *instance,
  161. struct iovec *iovec,
  162. unsigned int iov_len);
  163. static void passive_mcast_flush_send (
  164. struct totemrrp_instance *instance,
  165. struct iovec *iovec,
  166. unsigned int iov_len);
  167. static void passive_token_recv (
  168. struct totemrrp_instance *instance,
  169. unsigned int interface_no,
  170. struct totem_ip_address *system_from,
  171. void *msg,
  172. unsigned int msg_len,
  173. unsigned int token_seqid);
  174. static void passive_token_send (
  175. struct totemrrp_instance *instance,
  176. struct totem_ip_address *system_to,
  177. struct iovec *iovec,
  178. unsigned int iov_len);
  179. #endif
  180. static void active_mcast_recv (
  181. struct totemrrp_instance *instance,
  182. void *context,
  183. struct totem_ip_address *system_from,
  184. void *msg,
  185. unsigned int msg_len);
  186. static void active_mcast_noflush_send (
  187. struct totemrrp_instance *instance,
  188. struct iovec *iovec,
  189. unsigned int iov_len);
  190. static void active_mcast_flush_send (
  191. struct totemrrp_instance *instance,
  192. struct iovec *iovec,
  193. unsigned int iov_len);
  194. static void active_token_recv (
  195. struct totemrrp_instance *instance,
  196. unsigned int interface_no,
  197. void *context,
  198. struct totem_ip_address *system_from,
  199. void *msg,
  200. unsigned int msg_len,
  201. unsigned int token_seqid);
  202. static void active_token_send (
  203. struct totemrrp_instance *instance,
  204. struct totem_ip_address *system_to,
  205. struct iovec *iovec,
  206. unsigned int iov_len);
  207. static void active_recv_flush (
  208. struct totemrrp_instance *instance);
  209. static void active_send_flush (
  210. struct totemrrp_instance *instance);
  211. static void active_iface_check (
  212. struct totemrrp_instance *instance);
  213. static void active_processor_count_set (
  214. struct totemrrp_instance *instance,
  215. unsigned int processor_count_set);
  216. /*
  217. struct rrp_algo passive_algo = {
  218. .mcast_recv = passive_mcast_recv,
  219. .mcast_noflush_send = passive_mcast_noflush_send,
  220. .mcast_flush_send = passive_mcast_flush_send,
  221. .token_recv = passive_token_recv,
  222. .token_send = passive_token_send
  223. };
  224. */
  225. struct rrp_algo active_algo = {
  226. .mcast_recv = active_mcast_recv,
  227. .mcast_noflush_send = active_mcast_noflush_send,
  228. .mcast_flush_send = active_mcast_flush_send,
  229. .token_recv = active_token_recv,
  230. .token_send = active_token_send,
  231. .recv_flush = active_recv_flush,
  232. .send_flush = active_send_flush,
  233. .iface_check = active_iface_check,
  234. .processor_count_set = active_processor_count_set,
  235. };
  236. /*
  237. * All instances in one database
  238. */
  239. static struct hdb_handle_database totemrrp_instance_database = {
  240. .handle_count = 0,
  241. .handles = 0,
  242. .iterator = 0,
  243. .mutex = PTHREAD_MUTEX_INITIALIZER
  244. };
  245. #define log_printf(level, format, args...) \
  246. instance->totemrrp_log_printf (__FILE__, __LINE__, level, format, ##args)
  247. struct passive_instance *passive_instance_initialize (
  248. int interface_count)
  249. {
  250. struct passive_instance *instance;
  251. int i;
  252. instance = malloc (sizeof (struct passive_instance));
  253. if (instance == 0) {
  254. goto error_exit;
  255. }
  256. instance->faulty = malloc (sizeof (int) * interface_count);
  257. if (instance->faulty == 0) {
  258. free (instance);
  259. instance = 0;
  260. goto error_exit;
  261. }
  262. instance->last_seq = malloc (sizeof (int) * interface_count);
  263. if (instance->last_seq == 0) {
  264. free (instance->faulty);
  265. free (instance);
  266. instance = 0;
  267. goto error_exit;
  268. }
  269. instance->counter_problems = malloc (sizeof (int) * interface_count);
  270. if (instance->counter_problems == 0) {
  271. free (instance->last_seq);
  272. free (instance->faulty);
  273. free (instance);
  274. instance = 0;
  275. goto error_exit;
  276. }
  277. for (i = 0; i < interface_count; i++) {
  278. instance->faulty[i] = 0;
  279. instance->last_seq[i] = 0;
  280. instance->counter_problems[i] = 0;
  281. }
  282. error_exit:
  283. return (instance);
  284. }
  285. struct active_instance *active_instance_initialize (
  286. struct totemrrp_instance *rrp_instance,
  287. int interface_count)
  288. {
  289. struct active_instance *instance;
  290. int i;
  291. instance = malloc (sizeof (struct active_instance));
  292. if (instance == 0) {
  293. goto error_exit;
  294. }
  295. instance->faulty = malloc (sizeof (int) * interface_count);
  296. if (instance->faulty == 0) {
  297. free (instance);
  298. instance = 0;
  299. goto error_exit;
  300. }
  301. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  302. if (instance->last_token_recv == 0) {
  303. free (instance->faulty);
  304. free (instance);
  305. instance = 0;
  306. goto error_exit;
  307. }
  308. instance->counter_problems = malloc (sizeof (int) * interface_count);
  309. if (instance->counter_problems == 0) {
  310. free (instance->last_token_recv);
  311. free (instance->faulty);
  312. free (instance);
  313. instance = 0;
  314. goto error_exit;
  315. }
  316. instance->faulty = malloc (sizeof (int) * interface_count);
  317. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  318. instance->counter_problems = malloc (sizeof (int) * interface_count);
  319. for (i = 0; i < interface_count; i++) {
  320. instance->faulty[i] = 0;
  321. instance->last_token_recv[i] = 0;
  322. instance->counter_problems[i] = 0;
  323. }
  324. instance->timer_active_token = 0;
  325. instance->rrp_instance = rrp_instance;
  326. error_exit:
  327. return (instance);
  328. }
  329. static void timer_function_active_token (void *context)
  330. {
  331. // struct active_instance *instance = (struct active_instance *)context;
  332. }
  333. static void active_token_timer_start (struct active_instance *active_instance)
  334. {
  335. poll_timer_add (
  336. active_instance->rrp_instance->poll_handle,
  337. 10, /* 10 msec */
  338. (void *)active_instance,
  339. timer_function_active_token,
  340. &active_instance->timer_active_token);
  341. }
  342. static void active_token_timer_cancel (struct active_instance *active_instance)
  343. {
  344. poll_timer_delete (
  345. active_instance->rrp_instance->poll_handle,
  346. active_instance->timer_active_token);
  347. }
  348. static void active_mcast_recv (
  349. struct totemrrp_instance *instance,
  350. void *context,
  351. struct totem_ip_address *system_from,
  352. void *msg,
  353. unsigned int msg_len)
  354. {
  355. instance->totemrrp_deliver_fn (
  356. context,
  357. system_from,
  358. msg,
  359. msg_len);
  360. }
  361. static void active_mcast_flush_send (
  362. struct totemrrp_instance *instance,
  363. struct iovec *iovec,
  364. unsigned int iov_len)
  365. {
  366. int i;
  367. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  368. for (i = 0; i < instance->interface_count; i++) {
  369. if (rrp_algo_instance->faulty[i] == 0) {
  370. totemnet_mcast_flush_send (instance->net_handles[i], iovec, iov_len);
  371. }
  372. }
  373. }
  374. static void active_mcast_noflush_send (
  375. struct totemrrp_instance *instance,
  376. struct iovec *iovec,
  377. unsigned int iov_len)
  378. {
  379. int i;
  380. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  381. for (i = 0; i < instance->interface_count; i++) {
  382. if (rrp_algo_instance->faulty[i] == 0) {
  383. totemnet_mcast_noflush_send (instance->net_handles[i], iovec, iov_len);
  384. }
  385. }
  386. }
  387. static void active_token_recv (
  388. struct totemrrp_instance *instance,
  389. unsigned int interface_no,
  390. void *context,
  391. struct totem_ip_address *system_from,
  392. void *msg,
  393. unsigned int msg_len,
  394. unsigned int token_seqid)
  395. {
  396. unsigned int cur_token_seq;
  397. unsigned int last_token_seq;
  398. unsigned int token_is;
  399. int i;
  400. struct active_instance *active_instance = (struct active_instance *)instance->rrp_algo_instance;
  401. instance->totemrrp_token_seqid_get (
  402. msg,
  403. &cur_token_seq,
  404. &token_is);
  405. assert (token_is);
  406. if (token_seqid > cur_token_seq) {
  407. memcpy (active_instance->token, msg, msg_len);
  408. active_instance->token_len = msg_len;
  409. for (i = 0; i < instance->interface_count; i++) {
  410. active_instance->last_token_recv[i] = 0;
  411. }
  412. active_instance->last_token_recv[interface_no] = 1;
  413. active_token_timer_start (active_instance);
  414. }
  415. instance->totemrrp_token_seqid_get (
  416. msg,
  417. &last_token_seq,
  418. &token_is);
  419. assert (token_is);
  420. if (cur_token_seq == last_token_seq) {
  421. active_instance->last_token_recv[interface_no] = 1;
  422. for (i = 0; i < instance->interface_count; i++) {
  423. if ((active_instance->last_token_recv[i] == 0) &&
  424. active_instance->faulty[i] == 0) {
  425. return; /* don't deliver token */
  426. }
  427. }
  428. active_token_timer_cancel (active_instance);
  429. instance->totemrrp_deliver_fn (
  430. context,
  431. system_from,
  432. msg,
  433. msg_len);
  434. }
  435. }
  436. static void active_token_send (
  437. struct totemrrp_instance *instance,
  438. struct totem_ip_address *system_to,
  439. struct iovec *iovec,
  440. unsigned int iov_len)
  441. {
  442. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  443. int i;
  444. for (i = 0; i < instance->interface_count; i++) {
  445. if (rrp_algo_instance->faulty[i] == 0) {
  446. totemnet_token_send (
  447. instance->net_handles[i], system_to,
  448. iovec, iov_len);
  449. }
  450. }
  451. }
  452. static void active_recv_flush (struct totemrrp_instance *instance)
  453. {
  454. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  455. unsigned int i;
  456. for (i = 0; i < instance->interface_count; i++) {
  457. if (rrp_algo_instance->faulty[i] == 0) {
  458. totemnet_recv_flush (instance->net_handles[i]);
  459. }
  460. }
  461. }
  462. static void active_send_flush (struct totemrrp_instance *instance)
  463. {
  464. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  465. unsigned int i;
  466. for (i = 0; i < instance->interface_count; i++) {
  467. if (rrp_algo_instance->faulty[i] == 0) {
  468. totemnet_send_flush (instance->net_handles[i]);
  469. }
  470. }
  471. }
  472. static void active_iface_check (struct totemrrp_instance *instance)
  473. {
  474. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  475. unsigned int i;
  476. for (i = 0; i < instance->interface_count; i++) {
  477. if (rrp_algo_instance->faulty[i] == 0) {
  478. totemnet_iface_check (instance->net_handles[i]);
  479. }
  480. }
  481. }
  482. static void active_processor_count_set (
  483. struct totemrrp_instance *instance,
  484. unsigned int processor_count)
  485. {
  486. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  487. unsigned int i;
  488. for (i = 0; i < instance->interface_count; i++) {
  489. if (rrp_algo_instance->faulty[i] == 0) {
  490. totemnet_processor_count_set (instance->net_handles[i],
  491. processor_count);
  492. }
  493. }
  494. }
  495. struct deliver_fn_context {
  496. struct totemrrp_instance *instance;
  497. void *context;
  498. int interface_no;
  499. };
  500. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  501. {
  502. memset (instance, 0, sizeof (struct totemrrp_instance));
  503. instance->rrp_algo = &active_algo;
  504. }
  505. void rrp_deliver_fn (
  506. void *context,
  507. struct totem_ip_address *system_from,
  508. void *msg,
  509. int msg_len)
  510. {
  511. unsigned int token_seqid;
  512. unsigned int token_is;
  513. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  514. deliver_fn_context->instance->totemrrp_token_seqid_get (
  515. msg,
  516. &token_seqid,
  517. &token_is);
  518. if (token_is) {
  519. /*
  520. * Deliver to the token receiver for this rrp algorithm
  521. */
  522. deliver_fn_context->instance->rrp_algo->token_recv (
  523. deliver_fn_context->instance,
  524. deliver_fn_context->interface_no,
  525. deliver_fn_context->context,
  526. system_from,
  527. msg,
  528. msg_len,
  529. token_seqid);
  530. } else {
  531. /*
  532. * Deliver to the mcast receiver for this rrp algorithm
  533. */
  534. deliver_fn_context->instance->rrp_algo->mcast_recv (
  535. deliver_fn_context->instance,
  536. deliver_fn_context->context,
  537. system_from,
  538. msg,
  539. msg_len);
  540. }
  541. }
  542. void rrp_iface_change_fn (
  543. void *context,
  544. struct totem_ip_address *iface_addr)
  545. {
  546. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  547. deliver_fn_context->instance->totemrrp_iface_change_fn (
  548. deliver_fn_context->context,
  549. iface_addr);
  550. }
  551. int totemrrp_finalize (
  552. totemrrp_handle handle)
  553. {
  554. struct totemrrp_instance *instance;
  555. int res = 0;
  556. int i;
  557. res = hdb_handle_get (&totemrrp_instance_database, handle,
  558. (void *)&instance);
  559. if (res != 0) {
  560. res = ENOENT;
  561. goto error_exit;
  562. }
  563. for (i = 0; i < instance->interface_count; i++) {
  564. totemnet_finalize (instance->net_handles[i]);
  565. }
  566. hdb_handle_put (&totemrrp_instance_database, handle);
  567. error_exit:
  568. return (res);
  569. }
  570. /*
  571. * Totem Redundant Ring interface
  572. * depends on poll abstraction, POSIX, IPV4
  573. */
  574. /*
  575. * Create an instance
  576. */
  577. int totemrrp_initialize (
  578. poll_handle poll_handle,
  579. totemrrp_handle *handle,
  580. struct totem_config *totem_config,
  581. void *context,
  582. void (*deliver_fn) (
  583. void *context,
  584. struct totem_ip_address *system_from,
  585. void *msg,
  586. int msg_len),
  587. void (*iface_change_fn) (
  588. void *context,
  589. struct totem_ip_address *iface_addr),
  590. void (*token_seqid_get) (
  591. void *msg,
  592. unsigned int *seqid,
  593. unsigned int *token_is))
  594. {
  595. struct totemrrp_instance *instance;
  596. unsigned int res;
  597. int i;
  598. res = hdb_handle_create (&totemrrp_instance_database,
  599. sizeof (struct totemrrp_instance), handle);
  600. if (res != 0) {
  601. goto error_exit;
  602. }
  603. res = hdb_handle_get (&totemrrp_instance_database, *handle,
  604. (void *)&instance);
  605. if (res != 0) {
  606. goto error_destroy;
  607. }
  608. totemrrp_instance_initialize (instance);
  609. /*
  610. * Configure logging
  611. */
  612. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  613. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  614. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  615. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  616. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  617. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  618. instance->totemrrp_interfaces = totem_config->interfaces;
  619. instance->totemrrp_poll_handle = poll_handle;
  620. instance->totemrrp_deliver_fn = deliver_fn;
  621. instance->totemrrp_iface_change_fn = iface_change_fn;
  622. instance->totemrrp_token_seqid_get = token_seqid_get;
  623. instance->interface_count = totem_config->interface_count;
  624. instance->net_handles = malloc (sizeof (totemnet_handle) * totem_config->interface_count);
  625. instance->context = context;
  626. instance->poll_handle = poll_handle;
  627. instance->rrp_algo_instance = malloc (sizeof (struct active_instance));
  628. assert (instance->rrp_algo_instance);
  629. instance->rrp_algo_instance = active_instance_initialize (
  630. instance,
  631. totem_config->interface_count);
  632. for (i = 0; i < totem_config->interface_count; i++) {
  633. struct deliver_fn_context *deliver_fn_context;
  634. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  635. assert (deliver_fn_context);
  636. deliver_fn_context->instance = instance;
  637. deliver_fn_context->context = context;
  638. deliver_fn_context->interface_no = i;
  639. totemnet_initialize (
  640. poll_handle,
  641. &instance->net_handles[i],
  642. totem_config,
  643. i,
  644. (void *)deliver_fn_context,
  645. rrp_deliver_fn,
  646. rrp_iface_change_fn);
  647. }
  648. totemnet_net_mtu_adjust (totem_config);
  649. error_exit:
  650. hdb_handle_put (&totemrrp_instance_database, *handle);
  651. return (0);
  652. error_destroy:
  653. hdb_handle_destroy (&totemrrp_instance_database, *handle);
  654. return (-1);
  655. }
  656. int totemrrp_processor_count_set (
  657. totemrrp_handle handle,
  658. unsigned int processor_count)
  659. {
  660. struct totemrrp_instance *instance;
  661. int res = 0;
  662. res = hdb_handle_get (&totemrrp_instance_database, handle,
  663. (void *)&instance);
  664. if (res != 0) {
  665. res = ENOENT;
  666. goto error_exit;
  667. }
  668. instance->rrp_algo->processor_count_set (instance, processor_count);
  669. instance->processor_count = processor_count;
  670. hdb_handle_put (&totemrrp_instance_database, handle);
  671. error_exit:
  672. return (res);
  673. }
  674. int totemrrp_recv_flush (totemrrp_handle handle)
  675. {
  676. struct totemrrp_instance *instance;
  677. int res = 0;
  678. res = hdb_handle_get (&totemrrp_instance_database, handle,
  679. (void *)&instance);
  680. if (res != 0) {
  681. res = ENOENT;
  682. goto error_exit;
  683. }
  684. instance->rrp_algo->recv_flush (instance);
  685. hdb_handle_put (&totemrrp_instance_database, handle);
  686. error_exit:
  687. return (res);
  688. }
  689. int totemrrp_send_flush (totemrrp_handle handle)
  690. {
  691. struct totemrrp_instance *instance;
  692. int res = 0;
  693. res = hdb_handle_get (&totemrrp_instance_database, handle,
  694. (void *)&instance);
  695. if (res != 0) {
  696. res = ENOENT;
  697. goto error_exit;
  698. }
  699. instance->rrp_algo->send_flush (instance);
  700. hdb_handle_put (&totemrrp_instance_database, handle);
  701. error_exit:
  702. return (res);
  703. }
  704. int totemrrp_token_send (
  705. totemrrp_handle handle,
  706. struct totem_ip_address *system_to,
  707. struct iovec *iovec,
  708. unsigned int iov_len)
  709. {
  710. struct totemrrp_instance *instance;
  711. int res = 0;
  712. res = hdb_handle_get (&totemrrp_instance_database, handle,
  713. (void *)&instance);
  714. if (res != 0) {
  715. res = ENOENT;
  716. goto error_exit;
  717. }
  718. instance->rrp_algo->token_send (instance, system_to, iovec, iov_len);
  719. hdb_handle_put (&totemrrp_instance_database, handle);
  720. error_exit:
  721. return (res);
  722. }
  723. int totemrrp_mcast_flush_send (
  724. totemrrp_handle handle,
  725. struct iovec *iovec,
  726. unsigned int iov_len)
  727. {
  728. struct totemrrp_instance *instance;
  729. int res = 0;
  730. res = hdb_handle_get (&totemrrp_instance_database, handle,
  731. (void *)&instance);
  732. if (res != 0) {
  733. res = ENOENT;
  734. goto error_exit;
  735. }
  736. // TODO this needs to return the result
  737. instance->rrp_algo->mcast_flush_send (instance, iovec, iov_len);
  738. hdb_handle_put (&totemrrp_instance_database, handle);
  739. error_exit:
  740. return (res);
  741. }
  742. int totemrrp_mcast_noflush_send (
  743. totemrrp_handle handle,
  744. struct iovec *iovec,
  745. unsigned int iov_len)
  746. {
  747. struct totemrrp_instance *instance;
  748. int res = 0;
  749. res = hdb_handle_get (&totemrrp_instance_database, handle,
  750. (void *)&instance);
  751. if (res != 0) {
  752. res = ENOENT;
  753. goto error_exit;
  754. }
  755. /*
  756. * merge detects go out through mcast_flush_send so it is safe to
  757. * flush these messages if we are only one processor. This avoids
  758. * an encryption/hmac and decryption/hmac
  759. */
  760. if (instance->processor_count > 1) {
  761. // TODO this needs to return the result
  762. instance->rrp_algo->mcast_noflush_send (instance, iovec, iov_len);
  763. }
  764. hdb_handle_put (&totemrrp_instance_database, handle);
  765. error_exit:
  766. return (res);
  767. }
  768. int totemrrp_iface_check (totemrrp_handle handle)
  769. {
  770. struct totemrrp_instance *instance;
  771. int res = 0;
  772. res = hdb_handle_get (&totemrrp_instance_database, handle,
  773. (void *)&instance);
  774. if (res != 0) {
  775. res = ENOENT;
  776. goto error_exit;
  777. }
  778. instance->rrp_algo->iface_check (instance);
  779. hdb_handle_put (&totemrrp_instance_database, handle);
  780. error_exit:
  781. return (res);
  782. }