totemrrp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. };
  244. #define log_printf(level, format, args...) \
  245. instance->totemrrp_log_printf (__FILE__, __LINE__, level, format, ##args)
  246. struct passive_instance *passive_instance_initialize (
  247. int interface_count)
  248. {
  249. struct passive_instance *instance;
  250. int i;
  251. instance = malloc (sizeof (struct passive_instance));
  252. if (instance == 0) {
  253. goto error_exit;
  254. }
  255. instance->faulty = malloc (sizeof (int) * interface_count);
  256. if (instance->faulty == 0) {
  257. free (instance);
  258. instance = 0;
  259. goto error_exit;
  260. }
  261. instance->last_seq = malloc (sizeof (int) * interface_count);
  262. if (instance->last_seq == 0) {
  263. free (instance->faulty);
  264. free (instance);
  265. instance = 0;
  266. goto error_exit;
  267. }
  268. instance->counter_problems = malloc (sizeof (int) * interface_count);
  269. if (instance->counter_problems == 0) {
  270. free (instance->last_seq);
  271. free (instance->faulty);
  272. free (instance);
  273. instance = 0;
  274. goto error_exit;
  275. }
  276. for (i = 0; i < interface_count; i++) {
  277. instance->faulty[i] = 0;
  278. instance->last_seq[i] = 0;
  279. instance->counter_problems[i] = 0;
  280. }
  281. error_exit:
  282. return (instance);
  283. }
  284. struct active_instance *active_instance_initialize (
  285. struct totemrrp_instance *rrp_instance,
  286. int interface_count)
  287. {
  288. struct active_instance *instance;
  289. int i;
  290. instance = malloc (sizeof (struct active_instance));
  291. if (instance == 0) {
  292. goto error_exit;
  293. }
  294. instance->faulty = malloc (sizeof (int) * interface_count);
  295. if (instance->faulty == 0) {
  296. free (instance);
  297. instance = 0;
  298. goto error_exit;
  299. }
  300. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  301. if (instance->last_token_recv == 0) {
  302. free (instance->faulty);
  303. free (instance);
  304. instance = 0;
  305. goto error_exit;
  306. }
  307. instance->counter_problems = malloc (sizeof (int) * interface_count);
  308. if (instance->counter_problems == 0) {
  309. free (instance->last_token_recv);
  310. free (instance->faulty);
  311. free (instance);
  312. instance = 0;
  313. goto error_exit;
  314. }
  315. instance->faulty = malloc (sizeof (int) * interface_count);
  316. instance->last_token_recv = malloc (sizeof (int) * interface_count);
  317. instance->counter_problems = malloc (sizeof (int) * interface_count);
  318. for (i = 0; i < interface_count; i++) {
  319. instance->faulty[i] = 0;
  320. instance->last_token_recv[i] = 0;
  321. instance->counter_problems[i] = 0;
  322. }
  323. instance->timer_active_token = 0;
  324. instance->rrp_instance = rrp_instance;
  325. error_exit:
  326. return (instance);
  327. }
  328. static void timer_function_active_token (void *context)
  329. {
  330. // struct active_instance *instance = (struct active_instance *)context;
  331. }
  332. static void active_token_timer_start (struct active_instance *active_instance)
  333. {
  334. poll_timer_add (
  335. active_instance->rrp_instance->poll_handle,
  336. 10, /* 10 msec */
  337. (void *)active_instance,
  338. timer_function_active_token,
  339. &active_instance->timer_active_token);
  340. }
  341. static void active_token_timer_cancel (struct active_instance *active_instance)
  342. {
  343. poll_timer_delete (
  344. active_instance->rrp_instance->poll_handle,
  345. active_instance->timer_active_token);
  346. }
  347. static void active_mcast_recv (
  348. struct totemrrp_instance *instance,
  349. void *context,
  350. struct totem_ip_address *system_from,
  351. void *msg,
  352. unsigned int msg_len)
  353. {
  354. instance->totemrrp_deliver_fn (
  355. context,
  356. system_from,
  357. msg,
  358. msg_len);
  359. }
  360. static void active_mcast_flush_send (
  361. struct totemrrp_instance *instance,
  362. struct iovec *iovec,
  363. unsigned int iov_len)
  364. {
  365. int i;
  366. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  367. for (i = 0; i < instance->interface_count; i++) {
  368. if (rrp_algo_instance->faulty[i] == 0) {
  369. totemnet_mcast_flush_send (instance->net_handles[i], iovec, iov_len);
  370. }
  371. }
  372. }
  373. static void active_mcast_noflush_send (
  374. struct totemrrp_instance *instance,
  375. struct iovec *iovec,
  376. unsigned int iov_len)
  377. {
  378. int i;
  379. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  380. for (i = 0; i < instance->interface_count; i++) {
  381. if (rrp_algo_instance->faulty[i] == 0) {
  382. totemnet_mcast_noflush_send (instance->net_handles[i], iovec, iov_len);
  383. }
  384. }
  385. }
  386. static void active_token_recv (
  387. struct totemrrp_instance *instance,
  388. unsigned int interface_no,
  389. void *context,
  390. struct totem_ip_address *system_from,
  391. void *msg,
  392. unsigned int msg_len,
  393. unsigned int token_seqid)
  394. {
  395. unsigned int cur_token_seq;
  396. unsigned int last_token_seq;
  397. unsigned int token_is;
  398. int i;
  399. struct active_instance *active_instance = (struct active_instance *)instance->rrp_algo_instance;
  400. instance->totemrrp_token_seqid_get (
  401. msg,
  402. &cur_token_seq,
  403. &token_is);
  404. assert (token_is);
  405. if (token_seqid > cur_token_seq) {
  406. memcpy (active_instance->token, msg, msg_len);
  407. active_instance->token_len = msg_len;
  408. for (i = 0; i < instance->interface_count; i++) {
  409. active_instance->last_token_recv[i] = 0;
  410. }
  411. active_instance->last_token_recv[interface_no] = 1;
  412. active_token_timer_start (active_instance);
  413. }
  414. instance->totemrrp_token_seqid_get (
  415. msg,
  416. &last_token_seq,
  417. &token_is);
  418. assert (token_is);
  419. if (cur_token_seq == last_token_seq) {
  420. active_instance->last_token_recv[interface_no] = 1;
  421. for (i = 0; i < instance->interface_count; i++) {
  422. if ((active_instance->last_token_recv[i] == 0) &&
  423. active_instance->faulty[i] == 0) {
  424. return; /* don't deliver token */
  425. }
  426. }
  427. active_token_timer_cancel (active_instance);
  428. instance->totemrrp_deliver_fn (
  429. context,
  430. system_from,
  431. msg,
  432. msg_len);
  433. }
  434. }
  435. static void active_token_send (
  436. struct totemrrp_instance *instance,
  437. struct totem_ip_address *system_to,
  438. struct iovec *iovec,
  439. unsigned int iov_len)
  440. {
  441. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  442. int i;
  443. for (i = 0; i < instance->interface_count; i++) {
  444. if (rrp_algo_instance->faulty[i] == 0) {
  445. totemnet_token_send (
  446. instance->net_handles[i], system_to,
  447. iovec, iov_len);
  448. }
  449. }
  450. }
  451. static void active_recv_flush (struct totemrrp_instance *instance)
  452. {
  453. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  454. unsigned int i;
  455. for (i = 0; i < instance->interface_count; i++) {
  456. if (rrp_algo_instance->faulty[i] == 0) {
  457. totemnet_recv_flush (instance->net_handles[i]);
  458. }
  459. }
  460. }
  461. static void active_send_flush (struct totemrrp_instance *instance)
  462. {
  463. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  464. unsigned int i;
  465. for (i = 0; i < instance->interface_count; i++) {
  466. if (rrp_algo_instance->faulty[i] == 0) {
  467. totemnet_send_flush (instance->net_handles[i]);
  468. }
  469. }
  470. }
  471. static void active_iface_check (struct totemrrp_instance *instance)
  472. {
  473. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  474. unsigned int i;
  475. for (i = 0; i < instance->interface_count; i++) {
  476. if (rrp_algo_instance->faulty[i] == 0) {
  477. totemnet_iface_check (instance->net_handles[i]);
  478. }
  479. }
  480. }
  481. static void active_processor_count_set (
  482. struct totemrrp_instance *instance,
  483. unsigned int processor_count)
  484. {
  485. struct active_instance *rrp_algo_instance = (struct active_instance *)instance->rrp_algo_instance;
  486. unsigned int i;
  487. for (i = 0; i < instance->interface_count; i++) {
  488. if (rrp_algo_instance->faulty[i] == 0) {
  489. totemnet_processor_count_set (instance->net_handles[i],
  490. processor_count);
  491. }
  492. }
  493. }
  494. struct deliver_fn_context {
  495. struct totemrrp_instance *instance;
  496. void *context;
  497. int interface_no;
  498. };
  499. static void totemrrp_instance_initialize (struct totemrrp_instance *instance)
  500. {
  501. memset (instance, 0, sizeof (struct totemrrp_instance));
  502. instance->rrp_algo = &active_algo;
  503. }
  504. void rrp_deliver_fn (
  505. void *context,
  506. struct totem_ip_address *system_from,
  507. void *msg,
  508. int msg_len)
  509. {
  510. unsigned int token_seqid;
  511. unsigned int token_is;
  512. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  513. deliver_fn_context->instance->totemrrp_token_seqid_get (
  514. msg,
  515. &token_seqid,
  516. &token_is);
  517. if (token_is) {
  518. /*
  519. * Deliver to the token receiver for this rrp algorithm
  520. */
  521. deliver_fn_context->instance->rrp_algo->token_recv (
  522. deliver_fn_context->instance,
  523. deliver_fn_context->interface_no,
  524. deliver_fn_context->context,
  525. system_from,
  526. msg,
  527. msg_len,
  528. token_seqid);
  529. } else {
  530. /*
  531. * Deliver to the mcast receiver for this rrp algorithm
  532. */
  533. deliver_fn_context->instance->rrp_algo->mcast_recv (
  534. deliver_fn_context->instance,
  535. deliver_fn_context->context,
  536. system_from,
  537. msg,
  538. msg_len);
  539. }
  540. }
  541. void rrp_iface_change_fn (
  542. void *context,
  543. struct totem_ip_address *iface_addr)
  544. {
  545. struct deliver_fn_context *deliver_fn_context = (struct deliver_fn_context *)context;
  546. deliver_fn_context->instance->totemrrp_iface_change_fn (
  547. deliver_fn_context->context,
  548. iface_addr);
  549. }
  550. int totemrrp_finalize (
  551. totemrrp_handle handle)
  552. {
  553. struct totemrrp_instance *instance;
  554. int res = 0;
  555. int i;
  556. res = hdb_handle_get (&totemrrp_instance_database, handle,
  557. (void *)&instance);
  558. if (res != 0) {
  559. res = ENOENT;
  560. goto error_exit;
  561. }
  562. for (i = 0; i < instance->interface_count; i++) {
  563. totemnet_finalize (instance->net_handles[i]);
  564. }
  565. hdb_handle_put (&totemrrp_instance_database, handle);
  566. error_exit:
  567. return (res);
  568. }
  569. /*
  570. * Totem Redundant Ring interface
  571. * depends on poll abstraction, POSIX, IPV4
  572. */
  573. /*
  574. * Create an instance
  575. */
  576. int totemrrp_initialize (
  577. poll_handle poll_handle,
  578. totemrrp_handle *handle,
  579. struct totem_config *totem_config,
  580. void *context,
  581. void (*deliver_fn) (
  582. void *context,
  583. struct totem_ip_address *system_from,
  584. void *msg,
  585. int msg_len),
  586. void (*iface_change_fn) (
  587. void *context,
  588. struct totem_ip_address *iface_addr),
  589. void (*token_seqid_get) (
  590. void *msg,
  591. unsigned int *seqid,
  592. unsigned int *token_is))
  593. {
  594. struct totemrrp_instance *instance;
  595. unsigned int res;
  596. int i;
  597. res = hdb_handle_create (&totemrrp_instance_database,
  598. sizeof (struct totemrrp_instance), handle);
  599. if (res != 0) {
  600. goto error_exit;
  601. }
  602. res = hdb_handle_get (&totemrrp_instance_database, *handle,
  603. (void *)&instance);
  604. if (res != 0) {
  605. goto error_destroy;
  606. }
  607. totemrrp_instance_initialize (instance);
  608. /*
  609. * Configure logging
  610. */
  611. instance->totemrrp_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  612. instance->totemrrp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  613. instance->totemrrp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  614. instance->totemrrp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  615. instance->totemrrp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  616. instance->totemrrp_log_printf = totem_config->totem_logging_configuration.log_printf;
  617. instance->totemrrp_interfaces = totem_config->interfaces;
  618. instance->totemrrp_poll_handle = poll_handle;
  619. instance->totemrrp_deliver_fn = deliver_fn;
  620. instance->totemrrp_iface_change_fn = iface_change_fn;
  621. instance->totemrrp_token_seqid_get = token_seqid_get;
  622. instance->interface_count = totem_config->interface_count;
  623. instance->net_handles = malloc (sizeof (totemnet_handle) * totem_config->interface_count);
  624. instance->context = context;
  625. instance->poll_handle = poll_handle;
  626. instance->rrp_algo_instance = malloc (sizeof (struct active_instance));
  627. assert (instance->rrp_algo_instance);
  628. instance->rrp_algo_instance = active_instance_initialize (
  629. instance,
  630. totem_config->interface_count);
  631. for (i = 0; i < totem_config->interface_count; i++) {
  632. struct deliver_fn_context *deliver_fn_context;
  633. deliver_fn_context = malloc (sizeof (struct deliver_fn_context));
  634. assert (deliver_fn_context);
  635. deliver_fn_context->instance = instance;
  636. deliver_fn_context->context = context;
  637. deliver_fn_context->interface_no = i;
  638. totemnet_initialize (
  639. poll_handle,
  640. &instance->net_handles[i],
  641. totem_config,
  642. i,
  643. (void *)deliver_fn_context,
  644. rrp_deliver_fn,
  645. rrp_iface_change_fn);
  646. }
  647. totemnet_net_mtu_adjust (totem_config);
  648. error_exit:
  649. hdb_handle_put (&totemrrp_instance_database, *handle);
  650. return (0);
  651. error_destroy:
  652. hdb_handle_destroy (&totemrrp_instance_database, *handle);
  653. return (-1);
  654. }
  655. int totemrrp_processor_count_set (
  656. totemrrp_handle handle,
  657. unsigned int processor_count)
  658. {
  659. struct totemrrp_instance *instance;
  660. int res = 0;
  661. res = hdb_handle_get (&totemrrp_instance_database, handle,
  662. (void *)&instance);
  663. if (res != 0) {
  664. res = ENOENT;
  665. goto error_exit;
  666. }
  667. instance->rrp_algo->processor_count_set (instance, processor_count);
  668. instance->processor_count = processor_count;
  669. hdb_handle_put (&totemrrp_instance_database, handle);
  670. error_exit:
  671. return (res);
  672. }
  673. int totemrrp_recv_flush (totemrrp_handle handle)
  674. {
  675. struct totemrrp_instance *instance;
  676. int res = 0;
  677. res = hdb_handle_get (&totemrrp_instance_database, handle,
  678. (void *)&instance);
  679. if (res != 0) {
  680. res = ENOENT;
  681. goto error_exit;
  682. }
  683. instance->rrp_algo->recv_flush (instance);
  684. hdb_handle_put (&totemrrp_instance_database, handle);
  685. error_exit:
  686. return (res);
  687. }
  688. int totemrrp_send_flush (totemrrp_handle handle)
  689. {
  690. struct totemrrp_instance *instance;
  691. int res = 0;
  692. res = hdb_handle_get (&totemrrp_instance_database, handle,
  693. (void *)&instance);
  694. if (res != 0) {
  695. res = ENOENT;
  696. goto error_exit;
  697. }
  698. instance->rrp_algo->send_flush (instance);
  699. hdb_handle_put (&totemrrp_instance_database, handle);
  700. error_exit:
  701. return (res);
  702. }
  703. int totemrrp_token_send (
  704. totemrrp_handle handle,
  705. struct totem_ip_address *system_to,
  706. struct iovec *iovec,
  707. unsigned int iov_len)
  708. {
  709. struct totemrrp_instance *instance;
  710. int res = 0;
  711. res = hdb_handle_get (&totemrrp_instance_database, handle,
  712. (void *)&instance);
  713. if (res != 0) {
  714. res = ENOENT;
  715. goto error_exit;
  716. }
  717. instance->rrp_algo->token_send (instance, system_to, iovec, iov_len);
  718. hdb_handle_put (&totemrrp_instance_database, handle);
  719. error_exit:
  720. return (res);
  721. }
  722. int totemrrp_mcast_flush_send (
  723. totemrrp_handle handle,
  724. struct iovec *iovec,
  725. unsigned int iov_len)
  726. {
  727. struct totemrrp_instance *instance;
  728. int res = 0;
  729. res = hdb_handle_get (&totemrrp_instance_database, handle,
  730. (void *)&instance);
  731. if (res != 0) {
  732. res = ENOENT;
  733. goto error_exit;
  734. }
  735. // TODO this needs to return the result
  736. instance->rrp_algo->mcast_flush_send (instance, iovec, iov_len);
  737. hdb_handle_put (&totemrrp_instance_database, handle);
  738. error_exit:
  739. return (res);
  740. }
  741. int totemrrp_mcast_noflush_send (
  742. totemrrp_handle handle,
  743. struct iovec *iovec,
  744. unsigned int iov_len)
  745. {
  746. struct totemrrp_instance *instance;
  747. int res = 0;
  748. res = hdb_handle_get (&totemrrp_instance_database, handle,
  749. (void *)&instance);
  750. if (res != 0) {
  751. res = ENOENT;
  752. goto error_exit;
  753. }
  754. /*
  755. * merge detects go out through mcast_flush_send so it is safe to
  756. * flush these messages if we are only one processor. This avoids
  757. * an encryption/hmac and decryption/hmac
  758. */
  759. if (instance->processor_count > 1) {
  760. // TODO this needs to return the result
  761. instance->rrp_algo->mcast_noflush_send (instance, iovec, iov_len);
  762. }
  763. hdb_handle_put (&totemrrp_instance_database, handle);
  764. error_exit:
  765. return (res);
  766. }
  767. int totemrrp_iface_check (totemrrp_handle handle)
  768. {
  769. struct totemrrp_instance *instance;
  770. int res = 0;
  771. res = hdb_handle_get (&totemrrp_instance_database, handle,
  772. (void *)&instance);
  773. if (res != 0) {
  774. res = ENOENT;
  775. goto error_exit;
  776. }
  777. instance->rrp_algo->iface_check (instance);
  778. hdb_handle_put (&totemrrp_instance_database, handle);
  779. error_exit:
  780. return (res);
  781. }