totempg.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. /*
  2. * Copyright (c) 2003-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2005 OSDL.
  4. * Copyright (c) 2006-2009 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@redhat.com)
  9. * Author: Mark Haverkamp (markh@osdl.org)
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * FRAGMENTATION AND PACKING ALGORITHM:
  39. *
  40. * Assemble the entire message into one buffer
  41. * if full fragment
  42. * store fragment into lengths list
  43. * for each full fragment
  44. * multicast fragment
  45. * set length and fragment fields of pg mesage
  46. * store remaining multicast into head of fragmentation data and set lens field
  47. *
  48. * If a message exceeds the maximum packet size allowed by the totem
  49. * single ring protocol, the protocol could lose forward progress.
  50. * Statically calculating the allowed data amount doesn't work because
  51. * the amount of data allowed depends on the number of fragments in
  52. * each message. In this implementation, the maximum fragment size
  53. * is dynamically calculated for each fragment added to the message.
  54. * It is possible for a message to be two bytes short of the maximum
  55. * packet size. This occurs when a message or collection of
  56. * messages + the mcast header + the lens are two bytes short of the
  57. * end of the packet. Since another len field consumes two bytes, the
  58. * len field would consume the rest of the packet without room for data.
  59. *
  60. * One optimization would be to forgo the final len field and determine
  61. * it from the size of the udp datagram. Then this condition would no
  62. * longer occur.
  63. */
  64. /*
  65. * ASSEMBLY AND UNPACKING ALGORITHM:
  66. *
  67. * copy incoming packet into assembly data buffer indexed by current
  68. * location of end of fragment
  69. *
  70. * if not fragmented
  71. * deliver all messages in assembly data buffer
  72. * else
  73. * if msg_count > 1 and fragmented
  74. * deliver all messages except last message in assembly data buffer
  75. * copy last fragmented section to start of assembly data buffer
  76. * else
  77. * if msg_count = 1 and fragmented
  78. * do nothing
  79. *
  80. */
  81. #include <config.h>
  82. #ifdef HAVE_ALLOCA_H
  83. #include <alloca.h>
  84. #endif
  85. #include <netinet/in.h>
  86. #include <sys/uio.h>
  87. #include <stdio.h>
  88. #include <stdlib.h>
  89. #include <string.h>
  90. #include <assert.h>
  91. #include <pthread.h>
  92. #include <errno.h>
  93. #include <limits.h>
  94. #include <corosync/swab.h>
  95. #include <corosync/hdb.h>
  96. #include <corosync/list.h>
  97. #include <corosync/totem/coropoll.h>
  98. #include <corosync/totem/totempg.h>
  99. #define LOGSYS_UTILS_ONLY 1
  100. #include <corosync/engine/logsys.h>
  101. #include "totemmrp.h"
  102. #include "totemsrp.h"
  103. #define min(a,b) ((a) < (b)) ? a : b
  104. struct totempg_mcast_header {
  105. short version;
  106. short type;
  107. };
  108. #if !(defined(__i386__) || defined(__x86_64__))
  109. /*
  110. * Need align on architectures different then i386 or x86_64
  111. */
  112. #define TOTEMPG_NEED_ALIGN 1
  113. #endif
  114. /*
  115. * totempg_mcast structure
  116. *
  117. * header: Identify the mcast.
  118. * fragmented: Set if this message continues into next message
  119. * continuation: Set if this message is a continuation from last message
  120. * msg_count Indicates how many packed messages are contained
  121. * in the mcast.
  122. * Also, the size of each packed message and the messages themselves are
  123. * appended to the end of this structure when sent.
  124. */
  125. struct totempg_mcast {
  126. struct totempg_mcast_header header;
  127. unsigned char fragmented;
  128. unsigned char continuation;
  129. unsigned short msg_count;
  130. /*
  131. * short msg_len[msg_count];
  132. */
  133. /*
  134. * data for messages
  135. */
  136. };
  137. /*
  138. * Maximum packet size for totem pg messages
  139. */
  140. #define TOTEMPG_PACKET_SIZE (totempg_totem_config->net_mtu - \
  141. sizeof (struct totempg_mcast))
  142. /*
  143. * Local variables used for packing small messages
  144. */
  145. static unsigned short mcast_packed_msg_lens[FRAME_SIZE_MAX];
  146. static int mcast_packed_msg_count = 0;
  147. static int totempg_reserved = 1;
  148. static unsigned int totempg_size_limit;
  149. /*
  150. * Function and data used to log messages
  151. */
  152. static int totempg_log_level_security;
  153. static int totempg_log_level_error;
  154. static int totempg_log_level_warning;
  155. static int totempg_log_level_notice;
  156. static int totempg_log_level_debug;
  157. static int totempg_subsys_id;
  158. static void (*totempg_log_printf) (
  159. unsigned int rec_ident,
  160. const char *function,
  161. const char *file,
  162. int line,
  163. const char *format, ...) __attribute__((format(printf, 5, 6)));
  164. struct totem_config *totempg_totem_config;
  165. static totempg_stats_t totempg_stats;
  166. enum throw_away_mode {
  167. THROW_AWAY_INACTIVE,
  168. THROW_AWAY_ACTIVE
  169. };
  170. struct assembly {
  171. unsigned int nodeid;
  172. unsigned char data[MESSAGE_SIZE_MAX];
  173. int index;
  174. unsigned char last_frag_num;
  175. enum throw_away_mode throw_away_mode;
  176. struct list_head list;
  177. };
  178. static void assembly_deref (struct assembly *assembly);
  179. static int callback_token_received_fn (enum totem_callback_token_type type,
  180. const void *data);
  181. DECLARE_LIST_INIT(assembly_list_inuse);
  182. DECLARE_LIST_INIT(assembly_list_inuse_trans);
  183. DECLARE_LIST_INIT(assembly_list_free_trans);
  184. DECLARE_LIST_INIT(assembly_list_free);
  185. /*
  186. * Staging buffer for packed messages. Messages are staged in this buffer
  187. * before sending. Multiple messages may fit which cuts down on the
  188. * number of mcasts sent. If a message doesn't completely fit, then
  189. * the mcast header has a fragment bit set that says that there are more
  190. * data to follow. fragment_size is an index into the buffer. It indicates
  191. * the size of message data and where to place new message data.
  192. * fragment_contuation indicates whether the first packed message in
  193. * the buffer is a continuation of a previously packed fragment.
  194. */
  195. static unsigned char *fragmentation_data;
  196. static int fragment_size = 0;
  197. static int fragment_continuation = 0;
  198. static struct iovec iov_delv;
  199. static int totempg_waiting_transack = 0;
  200. static unsigned int totempg_max_handle = 0;
  201. struct totempg_group_instance {
  202. void (*deliver_fn) (
  203. unsigned int nodeid,
  204. const void *msg,
  205. unsigned int msg_len,
  206. int endian_conversion_required);
  207. void (*confchg_fn) (
  208. enum totem_configuration_type configuration_type,
  209. const unsigned int *member_list, size_t member_list_entries,
  210. const unsigned int *left_list, size_t left_list_entries,
  211. const unsigned int *joined_list, size_t joined_list_entries,
  212. const struct memb_ring_id *ring_id);
  213. struct totempg_group *groups;
  214. int groups_cnt;
  215. };
  216. DECLARE_HDB_DATABASE (totempg_groups_instance_database,NULL);
  217. static unsigned char next_fragment = 1;
  218. static pthread_mutex_t totempg_mutex = PTHREAD_MUTEX_INITIALIZER;
  219. static pthread_mutex_t callback_token_mutex = PTHREAD_MUTEX_INITIALIZER;
  220. static pthread_mutex_t mcast_msg_mutex = PTHREAD_MUTEX_INITIALIZER;
  221. #define log_printf(level, format, args...) \
  222. do { \
  223. totempg_log_printf ( \
  224. LOGSYS_ENCODE_RECID(level, \
  225. totempg_subsys_id, \
  226. LOGSYS_RECID_LOG), \
  227. __FUNCTION__, __FILE__, __LINE__, \
  228. format, ##args); \
  229. } while (0);
  230. static int msg_count_send_ok (int msg_count);
  231. static int byte_count_send_ok (int byte_count);
  232. static void totempg_waiting_trans_ack_cb (int waiting_trans_ack)
  233. {
  234. log_printf(LOG_DEBUG, "waiting_trans_ack changed to %u", waiting_trans_ack);
  235. totempg_waiting_transack = waiting_trans_ack;
  236. }
  237. static struct assembly *assembly_ref (unsigned int nodeid)
  238. {
  239. struct assembly *assembly;
  240. struct list_head *list;
  241. struct list_head *active_assembly_list_inuse;
  242. struct list_head *active_assembly_list_free;
  243. if (totempg_waiting_transack) {
  244. active_assembly_list_inuse = &assembly_list_inuse_trans;
  245. active_assembly_list_free = &assembly_list_free_trans;
  246. } else {
  247. active_assembly_list_inuse = &assembly_list_inuse;
  248. active_assembly_list_free = &assembly_list_free;
  249. }
  250. /*
  251. * Search inuse list for node id and return assembly buffer if found
  252. */
  253. for (list = active_assembly_list_inuse->next;
  254. list != active_assembly_list_inuse;
  255. list = list->next) {
  256. assembly = list_entry (list, struct assembly, list);
  257. if (nodeid == assembly->nodeid) {
  258. return (assembly);
  259. }
  260. }
  261. /*
  262. * Nothing found in inuse list get one from free list if available
  263. */
  264. if (list_empty (active_assembly_list_free) == 0) {
  265. assembly = list_entry (active_assembly_list_free->next, struct assembly, list);
  266. list_del (&assembly->list);
  267. list_add (&assembly->list, active_assembly_list_inuse);
  268. assembly->nodeid = nodeid;
  269. assembly->index = 0;
  270. assembly->last_frag_num = 0;
  271. assembly->throw_away_mode = THROW_AWAY_INACTIVE;
  272. return (assembly);
  273. }
  274. /*
  275. * Nothing available in inuse or free list, so allocate a new one
  276. */
  277. assembly = malloc (sizeof (struct assembly));
  278. /*
  279. * TODO handle memory allocation failure here
  280. */
  281. assert (assembly);
  282. assembly->nodeid = nodeid;
  283. assembly->data[0] = 0;
  284. assembly->index = 0;
  285. assembly->last_frag_num = 0;
  286. assembly->throw_away_mode = THROW_AWAY_INACTIVE;
  287. list_init (&assembly->list);
  288. list_add (&assembly->list, active_assembly_list_inuse);
  289. return (assembly);
  290. }
  291. static void assembly_deref (struct assembly *assembly)
  292. {
  293. struct list_head *active_assembly_list_free;
  294. if (totempg_waiting_transack) {
  295. active_assembly_list_free = &assembly_list_free_trans;
  296. } else {
  297. active_assembly_list_free = &assembly_list_free;
  298. }
  299. list_del (&assembly->list);
  300. list_add (&assembly->list, active_assembly_list_free);
  301. }
  302. static void assembly_deref_from_normal_and_trans (int nodeid)
  303. {
  304. int j;
  305. struct list_head *list, *list_next;
  306. struct list_head *active_assembly_list_inuse;
  307. struct list_head *active_assembly_list_free;
  308. struct assembly *assembly;
  309. for (j = 0; j < 2; j++) {
  310. if (j == 0) {
  311. active_assembly_list_inuse = &assembly_list_inuse;
  312. active_assembly_list_free = &assembly_list_free;
  313. } else {
  314. active_assembly_list_inuse = &assembly_list_inuse_trans;
  315. active_assembly_list_free = &assembly_list_free_trans;
  316. }
  317. for (list = active_assembly_list_inuse->next;
  318. list != active_assembly_list_inuse;
  319. list = list_next) {
  320. list_next = list->next;
  321. assembly = list_entry (list, struct assembly, list);
  322. if (nodeid == assembly->nodeid) {
  323. list_del (&assembly->list);
  324. list_add (&assembly->list, active_assembly_list_free);
  325. }
  326. }
  327. }
  328. }
  329. static inline void app_confchg_fn (
  330. enum totem_configuration_type configuration_type,
  331. const unsigned int *member_list, size_t member_list_entries,
  332. const unsigned int *left_list, size_t left_list_entries,
  333. const unsigned int *joined_list, size_t joined_list_entries,
  334. const struct memb_ring_id *ring_id)
  335. {
  336. int i;
  337. struct totempg_group_instance *instance;
  338. unsigned int res;
  339. /*
  340. * For every leaving processor, add to free list
  341. * This also has the side effect of clearing out the dataset
  342. * In the leaving processor's assembly buffer.
  343. */
  344. for (i = 0; i < left_list_entries; i++) {
  345. assembly_deref_from_normal_and_trans (left_list[i]);
  346. }
  347. for (i = 0; i <= totempg_max_handle; i++) {
  348. res = hdb_handle_get (&totempg_groups_instance_database,
  349. hdb_nocheck_convert (i), (void *)&instance);
  350. if (res == 0) {
  351. if (instance->confchg_fn) {
  352. instance->confchg_fn (
  353. configuration_type,
  354. member_list,
  355. member_list_entries,
  356. left_list,
  357. left_list_entries,
  358. joined_list,
  359. joined_list_entries,
  360. ring_id);
  361. }
  362. hdb_handle_put (&totempg_groups_instance_database,
  363. hdb_nocheck_convert (i));
  364. }
  365. }
  366. }
  367. static inline void group_endian_convert (
  368. void *msg,
  369. int msg_len)
  370. {
  371. unsigned short *group_len;
  372. int i;
  373. char *aligned_msg;
  374. #ifdef TOTEMPG_NEED_ALIGN
  375. /*
  376. * Align data structure for not i386 or x86_64
  377. */
  378. if ((size_t)msg % 4 != 0) {
  379. aligned_msg = alloca(msg_len);
  380. memcpy(aligned_msg, msg, msg_len);
  381. } else {
  382. aligned_msg = msg;
  383. }
  384. #else
  385. aligned_msg = msg;
  386. #endif
  387. group_len = (unsigned short *)aligned_msg;
  388. group_len[0] = swab16(group_len[0]);
  389. for (i = 1; i < group_len[0] + 1; i++) {
  390. group_len[i] = swab16(group_len[i]);
  391. }
  392. if (aligned_msg != msg) {
  393. memcpy(msg, aligned_msg, msg_len);
  394. }
  395. }
  396. static inline int group_matches (
  397. struct iovec *iovec,
  398. unsigned int iov_len,
  399. struct totempg_group *groups_b,
  400. unsigned int group_b_cnt,
  401. unsigned int *adjust_iovec)
  402. {
  403. unsigned short *group_len;
  404. char *group_name;
  405. int i;
  406. int j;
  407. #ifdef TOTEMPG_NEED_ALIGN
  408. struct iovec iovec_aligned = { NULL, 0 };
  409. #endif
  410. assert (iov_len == 1);
  411. #ifdef TOTEMPG_NEED_ALIGN
  412. /*
  413. * Align data structure for not i386 or x86_64
  414. */
  415. if ((size_t)iovec->iov_base % 4 != 0) {
  416. iovec_aligned.iov_base = alloca(iovec->iov_len);
  417. memcpy(iovec_aligned.iov_base, iovec->iov_base, iovec->iov_len);
  418. iovec_aligned.iov_len = iovec->iov_len;
  419. iovec = &iovec_aligned;
  420. }
  421. #endif
  422. group_len = (unsigned short *)iovec->iov_base;
  423. group_name = ((char *)iovec->iov_base) +
  424. sizeof (unsigned short) * (group_len[0] + 1);
  425. /*
  426. * Calculate amount to adjust the iovec by before delivering to app
  427. */
  428. *adjust_iovec = sizeof (unsigned short) * (group_len[0] + 1);
  429. for (i = 1; i < group_len[0] + 1; i++) {
  430. *adjust_iovec += group_len[i];
  431. }
  432. /*
  433. * Determine if this message should be delivered to this instance
  434. */
  435. for (i = 1; i < group_len[0] + 1; i++) {
  436. for (j = 0; j < group_b_cnt; j++) {
  437. if ((group_len[i] == groups_b[j].group_len) &&
  438. (memcmp (groups_b[j].group, group_name, group_len[i]) == 0)) {
  439. return (1);
  440. }
  441. }
  442. group_name += group_len[i];
  443. }
  444. return (0);
  445. }
  446. static inline void app_deliver_fn (
  447. unsigned int nodeid,
  448. void *msg,
  449. unsigned int msg_len,
  450. int endian_conversion_required)
  451. {
  452. int i;
  453. struct totempg_group_instance *instance;
  454. struct iovec stripped_iovec;
  455. unsigned int adjust_iovec;
  456. unsigned int res;
  457. struct iovec *iovec;
  458. struct iovec aligned_iovec = { NULL, 0 };
  459. if (endian_conversion_required) {
  460. group_endian_convert (msg, msg_len);
  461. }
  462. /*
  463. * TODO: segmentation/assembly need to be redesigned to provide aligned access
  464. * in all cases to avoid memory copies on non386 archs. Probably broke backwars
  465. * compatibility
  466. */
  467. #ifdef TOTEMPG_NEED_ALIGN
  468. /*
  469. * Align data structure for not i386 or x86_64
  470. */
  471. aligned_iovec.iov_base = alloca(msg_len);
  472. aligned_iovec.iov_len = msg_len;
  473. memcpy(aligned_iovec.iov_base, msg, msg_len);
  474. #else
  475. aligned_iovec.iov_base = msg;
  476. aligned_iovec.iov_len = msg_len;
  477. #endif
  478. iovec = &aligned_iovec;
  479. for (i = 0; i <= totempg_max_handle; i++) {
  480. res = hdb_handle_get (&totempg_groups_instance_database,
  481. hdb_nocheck_convert (i), (void *)&instance);
  482. if (res == 0) {
  483. if (group_matches (iovec, 1, instance->groups, instance->groups_cnt, &adjust_iovec)) {
  484. stripped_iovec.iov_len = iovec->iov_len - adjust_iovec;
  485. stripped_iovec.iov_base = (char *)iovec->iov_base + adjust_iovec;
  486. #ifdef TOTEMPG_NEED_ALIGN
  487. /*
  488. * Align data structure for not i386 or x86_64
  489. */
  490. if ((char *)iovec->iov_base + adjust_iovec % 4 != 0) {
  491. /*
  492. * Deal with misalignment
  493. */
  494. stripped_iovec.iov_base =
  495. alloca (stripped_iovec.iov_len);
  496. memcpy (stripped_iovec.iov_base,
  497. (char *)iovec->iov_base + adjust_iovec,
  498. stripped_iovec.iov_len);
  499. }
  500. #endif
  501. instance->deliver_fn (
  502. nodeid,
  503. stripped_iovec.iov_base,
  504. stripped_iovec.iov_len,
  505. endian_conversion_required);
  506. }
  507. hdb_handle_put (&totempg_groups_instance_database, hdb_nocheck_convert(i));
  508. }
  509. }
  510. }
  511. static void totempg_confchg_fn (
  512. enum totem_configuration_type configuration_type,
  513. const unsigned int *member_list, size_t member_list_entries,
  514. const unsigned int *left_list, size_t left_list_entries,
  515. const unsigned int *joined_list, size_t joined_list_entries,
  516. const struct memb_ring_id *ring_id)
  517. {
  518. // TODO optimize this
  519. app_confchg_fn (configuration_type,
  520. member_list, member_list_entries,
  521. left_list, left_list_entries,
  522. joined_list, joined_list_entries,
  523. ring_id);
  524. }
  525. static void totempg_deliver_fn (
  526. unsigned int nodeid,
  527. const void *msg,
  528. unsigned int msg_len,
  529. int endian_conversion_required)
  530. {
  531. struct totempg_mcast *mcast;
  532. unsigned short *msg_lens;
  533. int i;
  534. struct assembly *assembly;
  535. char header[FRAME_SIZE_MAX];
  536. int msg_count;
  537. int continuation;
  538. int start;
  539. const char *data;
  540. int datasize;
  541. assembly = assembly_ref (nodeid);
  542. assert (assembly);
  543. /*
  544. * Assemble the header into one block of data and
  545. * assemble the packet contents into one block of data to simplify delivery
  546. */
  547. mcast = (struct totempg_mcast *)msg;
  548. if (endian_conversion_required) {
  549. mcast->msg_count = swab16 (mcast->msg_count);
  550. }
  551. msg_count = mcast->msg_count;
  552. datasize = sizeof (struct totempg_mcast) +
  553. msg_count * sizeof (unsigned short);
  554. memcpy (header, msg, datasize);
  555. data = msg;
  556. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  557. if (endian_conversion_required) {
  558. for (i = 0; i < mcast->msg_count; i++) {
  559. msg_lens[i] = swab16 (msg_lens[i]);
  560. }
  561. }
  562. memcpy (&assembly->data[assembly->index], &data[datasize],
  563. msg_len - datasize);
  564. /*
  565. * If the last message in the buffer is a fragment, then we
  566. * can't deliver it. We'll first deliver the full messages
  567. * then adjust the assembly buffer so we can add the rest of the
  568. * fragment when it arrives.
  569. */
  570. msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
  571. continuation = mcast->continuation;
  572. iov_delv.iov_base = (void *)&assembly->data[0];
  573. iov_delv.iov_len = assembly->index + msg_lens[0];
  574. /*
  575. * Make sure that if this message is a continuation, that it
  576. * matches the sequence number of the previous fragment.
  577. * Also, if the first packed message is a continuation
  578. * of a previous message, but the assembly buffer
  579. * is empty, then we need to discard it since we can't
  580. * assemble a complete message. Likewise, if this message isn't a
  581. * continuation and the assembly buffer is empty, we have to discard
  582. * the continued message.
  583. */
  584. start = 0;
  585. if (assembly->throw_away_mode == THROW_AWAY_ACTIVE) {
  586. /* Throw away the first msg block */
  587. if (mcast->fragmented == 0 || mcast->fragmented == 1) {
  588. assembly->throw_away_mode = THROW_AWAY_INACTIVE;
  589. assembly->index += msg_lens[0];
  590. iov_delv.iov_base = (void *)&assembly->data[assembly->index];
  591. iov_delv.iov_len = msg_lens[1];
  592. start = 1;
  593. }
  594. } else
  595. if (assembly->throw_away_mode == THROW_AWAY_INACTIVE) {
  596. if (continuation == assembly->last_frag_num) {
  597. assembly->last_frag_num = mcast->fragmented;
  598. for (i = start; i < msg_count; i++) {
  599. app_deliver_fn(nodeid, iov_delv.iov_base, iov_delv.iov_len,
  600. endian_conversion_required);
  601. assembly->index += msg_lens[i];
  602. iov_delv.iov_base = (void *)&assembly->data[assembly->index];
  603. if (i < (msg_count - 1)) {
  604. iov_delv.iov_len = msg_lens[i + 1];
  605. }
  606. }
  607. } else {
  608. log_printf (LOG_DEBUG, "fragmented continuation %u is not equal to assembly last_frag_num %u",
  609. continuation, assembly->last_frag_num);
  610. assembly->throw_away_mode = THROW_AWAY_ACTIVE;
  611. }
  612. }
  613. if (mcast->fragmented == 0) {
  614. /*
  615. * End of messages, dereference assembly struct
  616. */
  617. assembly->last_frag_num = 0;
  618. assembly->index = 0;
  619. assembly_deref (assembly);
  620. } else {
  621. /*
  622. * Message is fragmented, keep around assembly list
  623. */
  624. if (mcast->msg_count > 1) {
  625. memmove (&assembly->data[0],
  626. &assembly->data[assembly->index],
  627. msg_lens[msg_count]);
  628. assembly->index = 0;
  629. }
  630. assembly->index += msg_lens[msg_count];
  631. }
  632. }
  633. /*
  634. * Totem Process Group Abstraction
  635. * depends on poll abstraction, POSIX, IPV4
  636. */
  637. void *callback_token_received_handle;
  638. int callback_token_received_fn (enum totem_callback_token_type type,
  639. const void *data)
  640. {
  641. struct totempg_mcast mcast;
  642. struct iovec iovecs[3];
  643. int res;
  644. pthread_mutex_lock (&mcast_msg_mutex);
  645. if (mcast_packed_msg_count == 0) {
  646. pthread_mutex_unlock (&mcast_msg_mutex);
  647. return (0);
  648. }
  649. if (totemmrp_avail() == 0) {
  650. pthread_mutex_unlock (&mcast_msg_mutex);
  651. return (0);
  652. }
  653. mcast.header.version = 0;
  654. mcast.fragmented = 0;
  655. /*
  656. * Was the first message in this buffer a continuation of a
  657. * fragmented message?
  658. */
  659. mcast.continuation = fragment_continuation;
  660. fragment_continuation = 0;
  661. mcast.msg_count = mcast_packed_msg_count;
  662. iovecs[0].iov_base = (void *)&mcast;
  663. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  664. iovecs[1].iov_base = (void *)mcast_packed_msg_lens;
  665. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  666. iovecs[2].iov_base = (void *)&fragmentation_data[0];
  667. iovecs[2].iov_len = fragment_size;
  668. res = totemmrp_mcast (iovecs, 3, 0);
  669. mcast_packed_msg_count = 0;
  670. fragment_size = 0;
  671. pthread_mutex_unlock (&mcast_msg_mutex);
  672. return (0);
  673. }
  674. /*
  675. * Initialize the totem process group abstraction
  676. */
  677. int totempg_initialize (
  678. hdb_handle_t poll_handle,
  679. struct totem_config *totem_config)
  680. {
  681. int res;
  682. totempg_totem_config = totem_config;
  683. totempg_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  684. totempg_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  685. totempg_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  686. totempg_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  687. totempg_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  688. totempg_log_printf = totem_config->totem_logging_configuration.log_printf;
  689. totempg_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  690. fragmentation_data = malloc (TOTEMPG_PACKET_SIZE);
  691. if (fragmentation_data == 0) {
  692. return (-1);
  693. }
  694. totemsrp_net_mtu_adjust (totem_config);
  695. res = totemmrp_initialize (
  696. poll_handle,
  697. totem_config,
  698. &totempg_stats,
  699. totempg_deliver_fn,
  700. totempg_confchg_fn,
  701. totempg_waiting_trans_ack_cb);
  702. totemmrp_callback_token_create (
  703. &callback_token_received_handle,
  704. TOTEM_CALLBACK_TOKEN_RECEIVED,
  705. 0,
  706. callback_token_received_fn,
  707. 0);
  708. totempg_size_limit = (totemmrp_avail() - 1) *
  709. (totempg_totem_config->net_mtu -
  710. sizeof (struct totempg_mcast) - 16);
  711. return (res);
  712. }
  713. void totempg_finalize (void)
  714. {
  715. pthread_mutex_lock (&totempg_mutex);
  716. totemmrp_finalize ();
  717. pthread_mutex_unlock (&totempg_mutex);
  718. }
  719. /*
  720. * Multicast a message
  721. */
  722. static int mcast_msg (
  723. struct iovec *iovec_in,
  724. unsigned int iov_len,
  725. int guarantee)
  726. {
  727. int res = 0;
  728. struct totempg_mcast mcast;
  729. struct iovec iovecs[3];
  730. struct iovec iovec[64];
  731. int i;
  732. int dest, src;
  733. int max_packet_size = 0;
  734. int copy_len = 0;
  735. int copy_base = 0;
  736. int total_size = 0;
  737. pthread_mutex_lock (&mcast_msg_mutex);
  738. totemmrp_event_signal (TOTEM_EVENT_NEW_MSG, 1);
  739. /*
  740. * Remove zero length iovectors from the list
  741. */
  742. assert (iov_len < 64);
  743. for (dest = 0, src = 0; src < iov_len; src++) {
  744. if (iovec_in[src].iov_len) {
  745. memcpy (&iovec[dest++], &iovec_in[src],
  746. sizeof (struct iovec));
  747. }
  748. }
  749. iov_len = dest;
  750. max_packet_size = TOTEMPG_PACKET_SIZE -
  751. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  752. mcast_packed_msg_lens[mcast_packed_msg_count] = 0;
  753. /*
  754. * Check if we would overwrite new message queue
  755. */
  756. for (i = 0; i < iov_len; i++) {
  757. total_size += iovec[i].iov_len;
  758. }
  759. if (byte_count_send_ok (total_size + sizeof(unsigned short) *
  760. (mcast_packed_msg_count)) == 0) {
  761. pthread_mutex_unlock (&mcast_msg_mutex);
  762. return(-1);
  763. }
  764. mcast.header.version = 0;
  765. for (i = 0; i < iov_len; ) {
  766. mcast.fragmented = 0;
  767. mcast.continuation = fragment_continuation;
  768. copy_len = iovec[i].iov_len - copy_base;
  769. /*
  770. * If it all fits with room left over, copy it in.
  771. * We need to leave at least sizeof(short) + 1 bytes in the
  772. * fragment_buffer on exit so that max_packet_size + fragment_size
  773. * doesn't exceed the size of the fragment_buffer on the next call.
  774. */
  775. if ((copy_len + fragment_size) <
  776. (max_packet_size - sizeof (unsigned short))) {
  777. memcpy (&fragmentation_data[fragment_size],
  778. (char *)iovec[i].iov_base + copy_base, copy_len);
  779. fragment_size += copy_len;
  780. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  781. next_fragment = 1;
  782. copy_len = 0;
  783. copy_base = 0;
  784. i++;
  785. continue;
  786. /*
  787. * If it just fits or is too big, then send out what fits.
  788. */
  789. } else {
  790. unsigned char *data_ptr;
  791. copy_len = min(copy_len, max_packet_size - fragment_size);
  792. if( copy_len == max_packet_size )
  793. data_ptr = (unsigned char *)iovec[i].iov_base + copy_base;
  794. else {
  795. data_ptr = fragmentation_data;
  796. memcpy (&fragmentation_data[fragment_size],
  797. (unsigned char *)iovec[i].iov_base + copy_base, copy_len);
  798. }
  799. memcpy (&fragmentation_data[fragment_size],
  800. (unsigned char *)iovec[i].iov_base + copy_base, copy_len);
  801. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  802. /*
  803. * if we're not on the last iovec or the iovec is too large to
  804. * fit, then indicate a fragment. This also means that the next
  805. * message will have the continuation of this one.
  806. */
  807. if ((i < (iov_len - 1)) ||
  808. ((copy_base + copy_len) < iovec[i].iov_len)) {
  809. if (!next_fragment) {
  810. next_fragment++;
  811. }
  812. fragment_continuation = next_fragment;
  813. mcast.fragmented = next_fragment++;
  814. assert(fragment_continuation != 0);
  815. assert(mcast.fragmented != 0);
  816. } else {
  817. fragment_continuation = 0;
  818. }
  819. /*
  820. * assemble the message and send it
  821. */
  822. mcast.msg_count = ++mcast_packed_msg_count;
  823. iovecs[0].iov_base = (void *)&mcast;
  824. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  825. iovecs[1].iov_base = (void *)mcast_packed_msg_lens;
  826. iovecs[1].iov_len = mcast_packed_msg_count *
  827. sizeof(unsigned short);
  828. iovecs[2].iov_base = (void *)data_ptr;
  829. iovecs[2].iov_len = max_packet_size;
  830. assert (totemmrp_avail() > 0);
  831. res = totemmrp_mcast (iovecs, 3, guarantee);
  832. if (res == -1) {
  833. goto error_exit;
  834. }
  835. /*
  836. * Recalculate counts and indexes for the next.
  837. */
  838. mcast_packed_msg_lens[0] = 0;
  839. mcast_packed_msg_count = 0;
  840. fragment_size = 0;
  841. max_packet_size = TOTEMPG_PACKET_SIZE - (sizeof(unsigned short));
  842. /*
  843. * If the iovec all fit, go to the next iovec
  844. */
  845. if ((copy_base + copy_len) == iovec[i].iov_len) {
  846. copy_len = 0;
  847. copy_base = 0;
  848. i++;
  849. /*
  850. * Continue with the rest of the current iovec.
  851. */
  852. } else {
  853. copy_base += copy_len;
  854. }
  855. }
  856. }
  857. /*
  858. * Bump only if we added message data. This may be zero if
  859. * the last buffer just fit into the fragmentation_data buffer
  860. * and we were at the last iovec.
  861. */
  862. if (mcast_packed_msg_lens[mcast_packed_msg_count]) {
  863. mcast_packed_msg_count++;
  864. }
  865. error_exit:
  866. pthread_mutex_unlock (&mcast_msg_mutex);
  867. return (res);
  868. }
  869. /*
  870. * Determine if a message of msg_size could be queued
  871. */
  872. static int msg_count_send_ok (
  873. int msg_count)
  874. {
  875. int avail = 0;
  876. avail = totemmrp_avail ();
  877. totempg_stats.msg_queue_avail = avail;
  878. return ((avail - totempg_reserved) > msg_count);
  879. }
  880. static int byte_count_send_ok (
  881. int byte_count)
  882. {
  883. unsigned int msg_count = 0;
  884. int avail = 0;
  885. avail = totemmrp_avail ();
  886. msg_count = (byte_count / (totempg_totem_config->net_mtu - sizeof (struct totempg_mcast) - 16)) + 1;
  887. return (avail >= msg_count);
  888. }
  889. static int send_reserve (
  890. int msg_size)
  891. {
  892. unsigned int msg_count = 0;
  893. msg_count = (msg_size / (totempg_totem_config->net_mtu - sizeof (struct totempg_mcast) - 16)) + 1;
  894. totempg_reserved += msg_count;
  895. totempg_stats.msg_reserved = totempg_reserved;
  896. return (msg_count);
  897. }
  898. static void send_release (
  899. int msg_count)
  900. {
  901. totempg_reserved -= msg_count;
  902. totempg_stats.msg_reserved = totempg_reserved;
  903. }
  904. int totempg_callback_token_create (
  905. void **handle_out,
  906. enum totem_callback_token_type type,
  907. int delete,
  908. int (*callback_fn) (enum totem_callback_token_type type, const void *),
  909. const void *data)
  910. {
  911. unsigned int res;
  912. pthread_mutex_lock (&callback_token_mutex);
  913. res = totemmrp_callback_token_create (handle_out, type, delete,
  914. callback_fn, data);
  915. pthread_mutex_unlock (&callback_token_mutex);
  916. return (res);
  917. }
  918. void totempg_callback_token_destroy (
  919. void *handle_out)
  920. {
  921. pthread_mutex_lock (&callback_token_mutex);
  922. totemmrp_callback_token_destroy (handle_out);
  923. pthread_mutex_unlock (&callback_token_mutex);
  924. }
  925. /*
  926. * vi: set autoindent tabstop=4 shiftwidth=4 :
  927. */
  928. int totempg_groups_initialize (
  929. hdb_handle_t *handle,
  930. void (*deliver_fn) (
  931. unsigned int nodeid,
  932. const void *msg,
  933. unsigned int msg_len,
  934. int endian_conversion_required),
  935. void (*confchg_fn) (
  936. enum totem_configuration_type configuration_type,
  937. const unsigned int *member_list, size_t member_list_entries,
  938. const unsigned int *left_list, size_t left_list_entries,
  939. const unsigned int *joined_list, size_t joined_list_entries,
  940. const struct memb_ring_id *ring_id))
  941. {
  942. struct totempg_group_instance *instance;
  943. unsigned int res;
  944. pthread_mutex_lock (&totempg_mutex);
  945. res = hdb_handle_create (&totempg_groups_instance_database,
  946. sizeof (struct totempg_group_instance), handle);
  947. if (res != 0) {
  948. goto error_exit;
  949. }
  950. if (*handle > totempg_max_handle) {
  951. totempg_max_handle = *handle;
  952. }
  953. res = hdb_handle_get (&totempg_groups_instance_database, *handle,
  954. (void *)&instance);
  955. if (res != 0) {
  956. goto error_destroy;
  957. }
  958. instance->deliver_fn = deliver_fn;
  959. instance->confchg_fn = confchg_fn;
  960. instance->groups = 0;
  961. instance->groups_cnt = 0;
  962. hdb_handle_put (&totempg_groups_instance_database, *handle);
  963. pthread_mutex_unlock (&totempg_mutex);
  964. return (0);
  965. error_destroy:
  966. hdb_handle_destroy (&totempg_groups_instance_database, *handle);
  967. error_exit:
  968. pthread_mutex_unlock (&totempg_mutex);
  969. return (-1);
  970. }
  971. int totempg_groups_join (
  972. hdb_handle_t handle,
  973. const struct totempg_group *groups,
  974. size_t group_cnt)
  975. {
  976. struct totempg_group_instance *instance;
  977. struct totempg_group *new_groups;
  978. unsigned int res;
  979. pthread_mutex_lock (&totempg_mutex);
  980. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  981. (void *)&instance);
  982. if (res != 0) {
  983. goto error_exit;
  984. }
  985. new_groups = realloc (instance->groups,
  986. sizeof (struct totempg_group) *
  987. (instance->groups_cnt + group_cnt));
  988. if (new_groups == 0) {
  989. res = ENOMEM;
  990. goto error_exit;
  991. }
  992. memcpy (&new_groups[instance->groups_cnt],
  993. groups, group_cnt * sizeof (struct totempg_group));
  994. instance->groups = new_groups;
  995. instance->groups_cnt += group_cnt;
  996. hdb_handle_put (&totempg_groups_instance_database, handle);
  997. error_exit:
  998. pthread_mutex_unlock (&totempg_mutex);
  999. return (res);
  1000. }
  1001. int totempg_groups_leave (
  1002. hdb_handle_t handle,
  1003. const struct totempg_group *groups,
  1004. size_t group_cnt)
  1005. {
  1006. struct totempg_group_instance *instance;
  1007. unsigned int res;
  1008. pthread_mutex_lock (&totempg_mutex);
  1009. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1010. (void *)&instance);
  1011. if (res != 0) {
  1012. goto error_exit;
  1013. }
  1014. hdb_handle_put (&totempg_groups_instance_database, handle);
  1015. error_exit:
  1016. pthread_mutex_unlock (&totempg_mutex);
  1017. return (res);
  1018. }
  1019. #define MAX_IOVECS_FROM_APP 32
  1020. #define MAX_GROUPS_PER_MSG 32
  1021. int totempg_groups_mcast_joined (
  1022. hdb_handle_t handle,
  1023. const struct iovec *iovec,
  1024. unsigned int iov_len,
  1025. int guarantee)
  1026. {
  1027. struct totempg_group_instance *instance;
  1028. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  1029. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  1030. int i;
  1031. unsigned int res;
  1032. pthread_mutex_lock (&totempg_mutex);
  1033. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1034. (void *)&instance);
  1035. if (res != 0) {
  1036. goto error_exit;
  1037. }
  1038. /*
  1039. * Build group_len structure and the iovec_mcast structure
  1040. */
  1041. group_len[0] = instance->groups_cnt;
  1042. for (i = 0; i < instance->groups_cnt; i++) {
  1043. group_len[i + 1] = instance->groups[i].group_len;
  1044. iovec_mcast[i + 1].iov_len = instance->groups[i].group_len;
  1045. iovec_mcast[i + 1].iov_base = (void *) instance->groups[i].group;
  1046. }
  1047. iovec_mcast[0].iov_len = (instance->groups_cnt + 1) * sizeof (unsigned short);
  1048. iovec_mcast[0].iov_base = group_len;
  1049. for (i = 0; i < iov_len; i++) {
  1050. iovec_mcast[i + instance->groups_cnt + 1].iov_len = iovec[i].iov_len;
  1051. iovec_mcast[i + instance->groups_cnt + 1].iov_base = iovec[i].iov_base;
  1052. }
  1053. res = mcast_msg (iovec_mcast, iov_len + instance->groups_cnt + 1, guarantee);
  1054. hdb_handle_put (&totempg_groups_instance_database, handle);
  1055. error_exit:
  1056. pthread_mutex_unlock (&totempg_mutex);
  1057. return (res);
  1058. }
  1059. int totempg_groups_joined_reserve (
  1060. hdb_handle_t handle,
  1061. const struct iovec *iovec,
  1062. unsigned int iov_len)
  1063. {
  1064. struct totempg_group_instance *instance;
  1065. unsigned int size = 0;
  1066. unsigned int i;
  1067. unsigned int res;
  1068. unsigned int reserved = 0;
  1069. pthread_mutex_lock (&totempg_mutex);
  1070. pthread_mutex_lock (&mcast_msg_mutex);
  1071. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1072. (void *)&instance);
  1073. if (res != 0) {
  1074. goto error_exit;
  1075. }
  1076. for (i = 0; i < instance->groups_cnt; i++) {
  1077. size += instance->groups[i].group_len;
  1078. }
  1079. for (i = 0; i < iov_len; i++) {
  1080. size += iovec[i].iov_len;
  1081. }
  1082. if (size >= totempg_size_limit) {
  1083. reserved = -1;
  1084. goto error_put;
  1085. }
  1086. reserved = send_reserve (size);
  1087. if (msg_count_send_ok (reserved) == 0) {
  1088. send_release (reserved);
  1089. reserved = 0;
  1090. }
  1091. error_put:
  1092. hdb_handle_put (&totempg_groups_instance_database, handle);
  1093. error_exit:
  1094. pthread_mutex_unlock (&mcast_msg_mutex);
  1095. pthread_mutex_unlock (&totempg_mutex);
  1096. return (reserved);
  1097. }
  1098. int totempg_groups_joined_release (int msg_count)
  1099. {
  1100. pthread_mutex_lock (&totempg_mutex);
  1101. pthread_mutex_lock (&mcast_msg_mutex);
  1102. send_release (msg_count);
  1103. pthread_mutex_unlock (&mcast_msg_mutex);
  1104. pthread_mutex_unlock (&totempg_mutex);
  1105. return 0;
  1106. }
  1107. int totempg_groups_mcast_groups (
  1108. hdb_handle_t handle,
  1109. int guarantee,
  1110. const struct totempg_group *groups,
  1111. size_t groups_cnt,
  1112. const struct iovec *iovec,
  1113. unsigned int iov_len)
  1114. {
  1115. struct totempg_group_instance *instance;
  1116. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  1117. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  1118. int i;
  1119. unsigned int res;
  1120. pthread_mutex_lock (&totempg_mutex);
  1121. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1122. (void *)&instance);
  1123. if (res != 0) {
  1124. goto error_exit;
  1125. }
  1126. /*
  1127. * Build group_len structure and the iovec_mcast structure
  1128. */
  1129. group_len[0] = groups_cnt;
  1130. for (i = 0; i < groups_cnt; i++) {
  1131. group_len[i + 1] = groups[i].group_len;
  1132. iovec_mcast[i + 1].iov_len = groups[i].group_len;
  1133. iovec_mcast[i + 1].iov_base = (void *) groups[i].group;
  1134. }
  1135. iovec_mcast[0].iov_len = (groups_cnt + 1) * sizeof (unsigned short);
  1136. iovec_mcast[0].iov_base = group_len;
  1137. for (i = 0; i < iov_len; i++) {
  1138. iovec_mcast[i + groups_cnt + 1].iov_len = iovec[i].iov_len;
  1139. iovec_mcast[i + groups_cnt + 1].iov_base = iovec[i].iov_base;
  1140. }
  1141. res = mcast_msg (iovec_mcast, iov_len + groups_cnt + 1, guarantee);
  1142. hdb_handle_put (&totempg_groups_instance_database, handle);
  1143. error_exit:
  1144. pthread_mutex_unlock (&totempg_mutex);
  1145. return (res);
  1146. }
  1147. /*
  1148. * Returns -1 if error, 0 if can't send, 1 if can send the message
  1149. */
  1150. int totempg_groups_send_ok_groups (
  1151. hdb_handle_t handle,
  1152. const struct totempg_group *groups,
  1153. size_t groups_cnt,
  1154. const struct iovec *iovec,
  1155. unsigned int iov_len)
  1156. {
  1157. struct totempg_group_instance *instance;
  1158. unsigned int size = 0;
  1159. unsigned int i;
  1160. unsigned int res;
  1161. pthread_mutex_lock (&totempg_mutex);
  1162. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1163. (void *)&instance);
  1164. if (res != 0) {
  1165. goto error_exit;
  1166. }
  1167. for (i = 0; i < groups_cnt; i++) {
  1168. size += groups[i].group_len;
  1169. }
  1170. for (i = 0; i < iov_len; i++) {
  1171. size += iovec[i].iov_len;
  1172. }
  1173. res = msg_count_send_ok (size);
  1174. hdb_handle_put (&totempg_groups_instance_database, handle);
  1175. error_exit:
  1176. pthread_mutex_unlock (&totempg_mutex);
  1177. return (res);
  1178. }
  1179. int totempg_ifaces_get (
  1180. unsigned int nodeid,
  1181. struct totem_ip_address *interfaces,
  1182. char ***status,
  1183. unsigned int *iface_count)
  1184. {
  1185. int res;
  1186. res = totemmrp_ifaces_get (
  1187. nodeid,
  1188. interfaces,
  1189. status,
  1190. iface_count);
  1191. return (res);
  1192. }
  1193. void totempg_event_signal (enum totem_event_type type, int value)
  1194. {
  1195. totemmrp_event_signal (type, value);
  1196. }
  1197. void* totempg_get_stats (void)
  1198. {
  1199. return &totempg_stats;
  1200. }
  1201. int totempg_crypto_set (
  1202. unsigned int type)
  1203. {
  1204. int res;
  1205. res = totemmrp_crypto_set (
  1206. type);
  1207. return (res);
  1208. }
  1209. int totempg_ring_reenable (void)
  1210. {
  1211. int res;
  1212. res = totemmrp_ring_reenable ();
  1213. return (res);
  1214. }
  1215. const char *totempg_ifaces_print (unsigned int nodeid)
  1216. {
  1217. static char iface_string[256 * INTERFACE_MAX];
  1218. char one_iface[64];
  1219. struct totem_ip_address interfaces[INTERFACE_MAX];
  1220. char **status;
  1221. unsigned int iface_count;
  1222. unsigned int i;
  1223. int res;
  1224. iface_string[0] = '\0';
  1225. res = totempg_ifaces_get (nodeid, interfaces, &status, &iface_count);
  1226. if (res == -1) {
  1227. return ("no interface found for nodeid");
  1228. }
  1229. for (i = 0; i < iface_count; i++) {
  1230. sprintf (one_iface, "r(%d) ip(%s) ",
  1231. i, totemip_print (&interfaces[i]));
  1232. strcat (iface_string, one_iface);
  1233. }
  1234. return (iface_string);
  1235. }
  1236. unsigned int totempg_my_nodeid_get (void)
  1237. {
  1238. return (totemmrp_my_nodeid_get());
  1239. }
  1240. int totempg_my_family_get (void)
  1241. {
  1242. return (totemmrp_my_family_get());
  1243. }
  1244. extern void totempg_service_ready_register (
  1245. void (*totem_service_ready) (void))
  1246. {
  1247. totemmrp_service_ready_register (totem_service_ready);
  1248. }
  1249. extern int totempg_member_add (
  1250. const struct totem_ip_address *member,
  1251. int ring_no);
  1252. extern int totempg_member_remove (
  1253. const struct totem_ip_address *member,
  1254. int ring_no);
  1255. void totempg_trans_ack (void)
  1256. {
  1257. totemmrp_trans_ack ();
  1258. }