4
0

totempg.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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 than 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. * Function and data used to log messages
  144. */
  145. static int totempg_log_level_security;
  146. static int totempg_log_level_error;
  147. static int totempg_log_level_warning;
  148. static int totempg_log_level_notice;
  149. static int totempg_log_level_debug;
  150. static int totempg_subsys_id;
  151. static void (*totempg_log_printf) (
  152. unsigned int rec_ident,
  153. const char *function,
  154. const char *file,
  155. int line,
  156. const char *format, ...) __attribute__((format(printf, 5, 6)));
  157. struct totem_config *totempg_totem_config;
  158. static totempg_stats_t totempg_stats;
  159. enum throw_away_mode {
  160. THROW_AWAY_INACTIVE,
  161. THROW_AWAY_ACTIVE
  162. };
  163. struct assembly {
  164. unsigned int nodeid;
  165. unsigned char data[MESSAGE_SIZE_MAX];
  166. int index;
  167. unsigned char last_frag_num;
  168. enum throw_away_mode throw_away_mode;
  169. struct list_head list;
  170. };
  171. static void assembly_deref (struct assembly *assembly);
  172. static int callback_token_received_fn (enum totem_callback_token_type type,
  173. const void *data);
  174. DECLARE_LIST_INIT(assembly_list_inuse);
  175. DECLARE_LIST_INIT(assembly_list_inuse_trans);
  176. /*
  177. * Free list is used both for transitional and operational assemblies
  178. */
  179. DECLARE_LIST_INIT(assembly_list_free);
  180. /*
  181. * Structure for storing totem_pg contexts so they can be switched
  182. */
  183. struct totempg_context {
  184. /*
  185. * Staging buffer for packed messages. Messages are staged in this buffer
  186. * before sending. Multiple messages may fit which cuts down on the
  187. * number of mcasts sent. If a message doesn't completely fit, then
  188. * the mcast header has a fragment bit set that says that there are more
  189. * data to follow. fragment_size is an index into the buffer. It indicates
  190. * the size of message data and where to place new message data.
  191. * fragment_contuation indicates whether the first packed message in
  192. * the buffer is a continuation of a previously packed fragment.
  193. */
  194. unsigned char *fragmentation_data;
  195. int fragment_size;
  196. int fragment_continuation;
  197. unsigned char next_fragment;
  198. /*
  199. * Local variables used for packing small messages
  200. */
  201. unsigned short mcast_packed_msg_lens[FRAME_SIZE_MAX];
  202. int mcast_packed_msg_count;
  203. int totempg_reserved;
  204. };
  205. #define TOTEMPG_NO_CONTEXTS 2
  206. static struct totempg_context totempg_contexts_array[TOTEMPG_NO_CONTEXTS];
  207. static struct totempg_context *totempg_active_context;
  208. struct totempg_group_instance {
  209. void (*deliver_fn) (
  210. unsigned int nodeid,
  211. const void *msg,
  212. unsigned int msg_len,
  213. int endian_conversion_required);
  214. void (*confchg_fn) (
  215. enum totem_configuration_type configuration_type,
  216. const unsigned int *member_list, size_t member_list_entries,
  217. const unsigned int *left_list, size_t left_list_entries,
  218. const unsigned int *joined_list, size_t joined_list_entries,
  219. const struct memb_ring_id *ring_id);
  220. struct totempg_group *groups;
  221. int groups_cnt;
  222. };
  223. DECLARE_HDB_DATABASE (totempg_groups_instance_database,NULL);
  224. static unsigned int totempg_max_handle = 0;
  225. static int totempg_waiting_transack = 0;
  226. static unsigned int totempg_size_limit = 0;
  227. static pthread_mutex_t totempg_mutex = PTHREAD_MUTEX_INITIALIZER;
  228. static pthread_mutex_t callback_token_mutex = PTHREAD_MUTEX_INITIALIZER;
  229. static pthread_mutex_t mcast_msg_mutex = PTHREAD_MUTEX_INITIALIZER;
  230. #define log_printf(level, format, args...) \
  231. do { \
  232. totempg_log_printf ( \
  233. LOGSYS_ENCODE_RECID(level, \
  234. totempg_subsys_id, \
  235. LOGSYS_RECID_LOG), \
  236. __FUNCTION__, __FILE__, __LINE__, \
  237. format, ##args); \
  238. } while (0);
  239. static int msg_count_send_ok (int msg_count);
  240. static int byte_count_send_ok (int byte_count);
  241. static void totempg_set_context(int context_no)
  242. {
  243. totempg_active_context = &totempg_contexts_array[context_no];
  244. }
  245. static void totempg_waiting_trans_ack_cb (int waiting_trans_ack)
  246. {
  247. log_printf(LOG_DEBUG, "waiting_trans_ack changed to %u", waiting_trans_ack);
  248. totempg_waiting_transack = waiting_trans_ack;
  249. if (!waiting_trans_ack) {
  250. totempg_set_context(0);
  251. } else {
  252. totempg_set_context(1);
  253. }
  254. }
  255. static struct assembly *assembly_ref (unsigned int nodeid)
  256. {
  257. struct assembly *assembly;
  258. struct list_head *list;
  259. struct list_head *active_assembly_list_inuse;
  260. if (totempg_waiting_transack) {
  261. active_assembly_list_inuse = &assembly_list_inuse_trans;
  262. } else {
  263. active_assembly_list_inuse = &assembly_list_inuse;
  264. }
  265. /*
  266. * Search inuse list for node id and return assembly buffer if found
  267. */
  268. for (list = active_assembly_list_inuse->next;
  269. list != active_assembly_list_inuse;
  270. list = list->next) {
  271. assembly = list_entry (list, struct assembly, list);
  272. if (nodeid == assembly->nodeid) {
  273. return (assembly);
  274. }
  275. }
  276. /*
  277. * Nothing found in inuse list get one from free list if available
  278. */
  279. if (list_empty (&assembly_list_free) == 0) {
  280. assembly = list_entry (assembly_list_free.next, struct assembly, list);
  281. list_del (&assembly->list);
  282. list_add (&assembly->list, active_assembly_list_inuse);
  283. assembly->nodeid = nodeid;
  284. assembly->index = 0;
  285. assembly->last_frag_num = 0;
  286. assembly->throw_away_mode = THROW_AWAY_INACTIVE;
  287. return (assembly);
  288. }
  289. /*
  290. * Nothing available in inuse or free list, so allocate a new one
  291. */
  292. assembly = malloc (sizeof (struct assembly));
  293. /*
  294. * TODO handle memory allocation failure here
  295. */
  296. assert (assembly);
  297. assembly->nodeid = nodeid;
  298. assembly->data[0] = 0;
  299. assembly->index = 0;
  300. assembly->last_frag_num = 0;
  301. assembly->throw_away_mode = THROW_AWAY_INACTIVE;
  302. list_init (&assembly->list);
  303. list_add (&assembly->list, active_assembly_list_inuse);
  304. return (assembly);
  305. }
  306. static void assembly_deref (struct assembly *assembly)
  307. {
  308. list_del (&assembly->list);
  309. list_add (&assembly->list, &assembly_list_free);
  310. }
  311. static void assembly_deref_from_normal_and_trans (int nodeid)
  312. {
  313. int j;
  314. struct list_head *list, *list_next;
  315. struct list_head *active_assembly_list_inuse;
  316. struct assembly *assembly;
  317. for (j = 0; j < 2; j++) {
  318. if (j == 0) {
  319. active_assembly_list_inuse = &assembly_list_inuse;
  320. } else {
  321. active_assembly_list_inuse = &assembly_list_inuse_trans;
  322. }
  323. for (list = active_assembly_list_inuse->next;
  324. list != active_assembly_list_inuse;
  325. list = list_next) {
  326. list_next = list->next;
  327. assembly = list_entry (list, struct assembly, list);
  328. if (nodeid == assembly->nodeid) {
  329. list_del (&assembly->list);
  330. list_add (&assembly->list, &assembly_list_free);
  331. }
  332. }
  333. }
  334. }
  335. static inline void app_confchg_fn (
  336. enum totem_configuration_type configuration_type,
  337. const unsigned int *member_list, size_t member_list_entries,
  338. const unsigned int *left_list, size_t left_list_entries,
  339. const unsigned int *joined_list, size_t joined_list_entries,
  340. const struct memb_ring_id *ring_id)
  341. {
  342. int i;
  343. struct totempg_group_instance *instance;
  344. unsigned int res;
  345. /*
  346. * For every leaving processor, add to free list
  347. * This also has the side effect of clearing out the dataset
  348. * In the leaving processor's assembly buffer.
  349. */
  350. for (i = 0; i < left_list_entries; i++) {
  351. assembly_deref_from_normal_and_trans (left_list[i]);
  352. }
  353. for (i = 0; i <= totempg_max_handle; i++) {
  354. res = hdb_handle_get (&totempg_groups_instance_database,
  355. hdb_nocheck_convert (i), (void *)&instance);
  356. if (res == 0) {
  357. if (instance->confchg_fn) {
  358. instance->confchg_fn (
  359. configuration_type,
  360. member_list,
  361. member_list_entries,
  362. left_list,
  363. left_list_entries,
  364. joined_list,
  365. joined_list_entries,
  366. ring_id);
  367. }
  368. hdb_handle_put (&totempg_groups_instance_database,
  369. hdb_nocheck_convert (i));
  370. }
  371. }
  372. }
  373. static inline void group_endian_convert (
  374. void *msg,
  375. int msg_len)
  376. {
  377. unsigned short *group_len;
  378. int i;
  379. char *aligned_msg;
  380. #ifdef TOTEMPG_NEED_ALIGN
  381. /*
  382. * Align data structure for not i386 or x86_64
  383. */
  384. if ((size_t)msg % 4 != 0) {
  385. aligned_msg = alloca(msg_len);
  386. memcpy(aligned_msg, msg, msg_len);
  387. } else {
  388. aligned_msg = msg;
  389. }
  390. #else
  391. aligned_msg = msg;
  392. #endif
  393. group_len = (unsigned short *)aligned_msg;
  394. group_len[0] = swab16(group_len[0]);
  395. for (i = 1; i < group_len[0] + 1; i++) {
  396. group_len[i] = swab16(group_len[i]);
  397. }
  398. if (aligned_msg != msg) {
  399. memcpy(msg, aligned_msg, msg_len);
  400. }
  401. }
  402. static inline int group_matches (
  403. struct iovec *iovec,
  404. unsigned int iov_len,
  405. struct totempg_group *groups_b,
  406. unsigned int group_b_cnt,
  407. unsigned int *adjust_iovec)
  408. {
  409. unsigned short *group_len;
  410. char *group_name;
  411. int i;
  412. int j;
  413. #ifdef TOTEMPG_NEED_ALIGN
  414. struct iovec iovec_aligned = { NULL, 0 };
  415. #endif
  416. assert (iov_len == 1);
  417. #ifdef TOTEMPG_NEED_ALIGN
  418. /*
  419. * Align data structure for not i386 or x86_64
  420. */
  421. if ((size_t)iovec->iov_base % 4 != 0) {
  422. iovec_aligned.iov_base = alloca(iovec->iov_len);
  423. memcpy(iovec_aligned.iov_base, iovec->iov_base, iovec->iov_len);
  424. iovec_aligned.iov_len = iovec->iov_len;
  425. iovec = &iovec_aligned;
  426. }
  427. #endif
  428. group_len = (unsigned short *)iovec->iov_base;
  429. group_name = ((char *)iovec->iov_base) +
  430. sizeof (unsigned short) * (group_len[0] + 1);
  431. /*
  432. * Calculate amount to adjust the iovec by before delivering to app
  433. */
  434. *adjust_iovec = sizeof (unsigned short) * (group_len[0] + 1);
  435. for (i = 1; i < group_len[0] + 1; i++) {
  436. *adjust_iovec += group_len[i];
  437. }
  438. /*
  439. * Determine if this message should be delivered to this instance
  440. */
  441. for (i = 1; i < group_len[0] + 1; i++) {
  442. for (j = 0; j < group_b_cnt; j++) {
  443. if ((group_len[i] == groups_b[j].group_len) &&
  444. (memcmp (groups_b[j].group, group_name, group_len[i]) == 0)) {
  445. return (1);
  446. }
  447. }
  448. group_name += group_len[i];
  449. }
  450. return (0);
  451. }
  452. static inline void app_deliver_fn (
  453. unsigned int nodeid,
  454. void *msg,
  455. unsigned int msg_len,
  456. int endian_conversion_required)
  457. {
  458. int i;
  459. struct totempg_group_instance *instance;
  460. struct iovec stripped_iovec;
  461. unsigned int adjust_iovec;
  462. unsigned int res;
  463. struct iovec *iovec;
  464. struct iovec aligned_iovec = { NULL, 0 };
  465. if (endian_conversion_required) {
  466. group_endian_convert (msg, msg_len);
  467. }
  468. /*
  469. * TODO: segmentation/assembly need to be redesigned to provide aligned access
  470. * in all cases to avoid memory copies on non386 archs. Probably broke backwars
  471. * compatibility
  472. */
  473. #ifdef TOTEMPG_NEED_ALIGN
  474. /*
  475. * Align data structure for not i386 or x86_64
  476. */
  477. aligned_iovec.iov_base = alloca(msg_len);
  478. aligned_iovec.iov_len = msg_len;
  479. memcpy(aligned_iovec.iov_base, msg, msg_len);
  480. #else
  481. aligned_iovec.iov_base = msg;
  482. aligned_iovec.iov_len = msg_len;
  483. #endif
  484. iovec = &aligned_iovec;
  485. for (i = 0; i <= totempg_max_handle; i++) {
  486. res = hdb_handle_get (&totempg_groups_instance_database,
  487. hdb_nocheck_convert (i), (void *)&instance);
  488. if (res == 0) {
  489. if (group_matches (iovec, 1, instance->groups, instance->groups_cnt, &adjust_iovec)) {
  490. stripped_iovec.iov_len = iovec->iov_len - adjust_iovec;
  491. stripped_iovec.iov_base = (char *)iovec->iov_base + adjust_iovec;
  492. #ifdef TOTEMPG_NEED_ALIGN
  493. /*
  494. * Align data structure for not i386 or x86_64
  495. */
  496. if ((char *)iovec->iov_base + adjust_iovec % 4 != 0) {
  497. /*
  498. * Deal with misalignment
  499. */
  500. stripped_iovec.iov_base =
  501. alloca (stripped_iovec.iov_len);
  502. memcpy (stripped_iovec.iov_base,
  503. (char *)iovec->iov_base + adjust_iovec,
  504. stripped_iovec.iov_len);
  505. }
  506. #endif
  507. instance->deliver_fn (
  508. nodeid,
  509. stripped_iovec.iov_base,
  510. stripped_iovec.iov_len,
  511. endian_conversion_required);
  512. }
  513. hdb_handle_put (&totempg_groups_instance_database, hdb_nocheck_convert(i));
  514. }
  515. }
  516. }
  517. static void totempg_confchg_fn (
  518. enum totem_configuration_type configuration_type,
  519. const unsigned int *member_list, size_t member_list_entries,
  520. const unsigned int *left_list, size_t left_list_entries,
  521. const unsigned int *joined_list, size_t joined_list_entries,
  522. const struct memb_ring_id *ring_id)
  523. {
  524. // TODO optimize this
  525. app_confchg_fn (configuration_type,
  526. member_list, member_list_entries,
  527. left_list, left_list_entries,
  528. joined_list, joined_list_entries,
  529. ring_id);
  530. }
  531. static void totempg_deliver_fn (
  532. unsigned int nodeid,
  533. const void *msg,
  534. unsigned int msg_len,
  535. int endian_conversion_required)
  536. {
  537. struct totempg_mcast *mcast;
  538. unsigned short *msg_lens;
  539. int i;
  540. struct assembly *assembly;
  541. char header[FRAME_SIZE_MAX];
  542. int msg_count;
  543. int continuation;
  544. int start;
  545. const char *data;
  546. int datasize;
  547. struct iovec iov_delv;
  548. assembly = assembly_ref (nodeid);
  549. assert (assembly);
  550. /*
  551. * Assemble the header into one block of data and
  552. * assemble the packet contents into one block of data to simplify delivery
  553. */
  554. mcast = (struct totempg_mcast *)msg;
  555. if (endian_conversion_required) {
  556. mcast->msg_count = swab16 (mcast->msg_count);
  557. }
  558. msg_count = mcast->msg_count;
  559. datasize = sizeof (struct totempg_mcast) +
  560. msg_count * sizeof (unsigned short);
  561. memcpy (header, msg, datasize);
  562. data = msg;
  563. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  564. if (endian_conversion_required) {
  565. for (i = 0; i < mcast->msg_count; i++) {
  566. msg_lens[i] = swab16 (msg_lens[i]);
  567. }
  568. }
  569. memcpy (&assembly->data[assembly->index], &data[datasize],
  570. msg_len - datasize);
  571. /*
  572. * If the last message in the buffer is a fragment, then we
  573. * can't deliver it. We'll first deliver the full messages
  574. * then adjust the assembly buffer so we can add the rest of the
  575. * fragment when it arrives.
  576. */
  577. msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
  578. continuation = mcast->continuation;
  579. iov_delv.iov_base = (void *)&assembly->data[0];
  580. iov_delv.iov_len = assembly->index + msg_lens[0];
  581. /*
  582. * Make sure that if this message is a continuation, that it
  583. * matches the sequence number of the previous fragment.
  584. * Also, if the first packed message is a continuation
  585. * of a previous message, but the assembly buffer
  586. * is empty, then we need to discard it since we can't
  587. * assemble a complete message. Likewise, if this message isn't a
  588. * continuation and the assembly buffer is empty, we have to discard
  589. * the continued message.
  590. */
  591. start = 0;
  592. if (assembly->throw_away_mode == THROW_AWAY_ACTIVE) {
  593. /* Throw away the first msg block */
  594. if (mcast->fragmented == 0 || mcast->fragmented == 1) {
  595. assembly->throw_away_mode = THROW_AWAY_INACTIVE;
  596. assembly->index += msg_lens[0];
  597. iov_delv.iov_base = (void *)&assembly->data[assembly->index];
  598. iov_delv.iov_len = msg_lens[1];
  599. start = 1;
  600. }
  601. } else
  602. if (assembly->throw_away_mode == THROW_AWAY_INACTIVE) {
  603. if (continuation == assembly->last_frag_num) {
  604. assembly->last_frag_num = mcast->fragmented;
  605. for (i = start; i < msg_count; i++) {
  606. app_deliver_fn(nodeid, iov_delv.iov_base, iov_delv.iov_len,
  607. endian_conversion_required);
  608. assembly->index += msg_lens[i];
  609. iov_delv.iov_base = (void *)&assembly->data[assembly->index];
  610. if (i < (msg_count - 1)) {
  611. iov_delv.iov_len = msg_lens[i + 1];
  612. }
  613. }
  614. } else {
  615. log_printf (LOG_DEBUG, "fragmented continuation %u is not equal to assembly last_frag_num %u",
  616. continuation, assembly->last_frag_num);
  617. assembly->throw_away_mode = THROW_AWAY_ACTIVE;
  618. }
  619. }
  620. if (mcast->fragmented == 0) {
  621. /*
  622. * End of messages, dereference assembly struct
  623. */
  624. assembly->last_frag_num = 0;
  625. assembly->index = 0;
  626. assembly_deref (assembly);
  627. } else {
  628. /*
  629. * Message is fragmented, keep around assembly list
  630. */
  631. if (mcast->msg_count > 1) {
  632. memmove (&assembly->data[0],
  633. &assembly->data[assembly->index],
  634. msg_lens[msg_count]);
  635. assembly->index = 0;
  636. }
  637. assembly->index += msg_lens[msg_count];
  638. }
  639. }
  640. /*
  641. * Totem Process Group Abstraction
  642. * depends on poll abstraction, POSIX, IPV4
  643. */
  644. void *callback_token_received_handle;
  645. int callback_token_received_fn (enum totem_callback_token_type type,
  646. const void *data)
  647. {
  648. struct totempg_mcast mcast;
  649. struct iovec iovecs[3];
  650. int res;
  651. struct totempg_context *con = totempg_active_context;
  652. pthread_mutex_lock (&mcast_msg_mutex);
  653. if (con->mcast_packed_msg_count == 0) {
  654. pthread_mutex_unlock (&mcast_msg_mutex);
  655. return (0);
  656. }
  657. if (totemmrp_avail() == 0) {
  658. pthread_mutex_unlock (&mcast_msg_mutex);
  659. return (0);
  660. }
  661. mcast.header.version = 0;
  662. mcast.fragmented = 0;
  663. /*
  664. * Was the first message in this buffer a continuation of a
  665. * fragmented message?
  666. */
  667. mcast.continuation = con->fragment_continuation;
  668. con->fragment_continuation = 0;
  669. mcast.msg_count = con->mcast_packed_msg_count;
  670. iovecs[0].iov_base = (void *)&mcast;
  671. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  672. iovecs[1].iov_base = (void *)con->mcast_packed_msg_lens;
  673. iovecs[1].iov_len = con->mcast_packed_msg_count * sizeof (unsigned short);
  674. iovecs[2].iov_base = (void *)&con->fragmentation_data[0];
  675. iovecs[2].iov_len = con->fragment_size;
  676. res = totemmrp_mcast (iovecs, 3, 0);
  677. con->mcast_packed_msg_count = 0;
  678. con->fragment_size = 0;
  679. pthread_mutex_unlock (&mcast_msg_mutex);
  680. return (0);
  681. }
  682. /*
  683. * Initialize the totem process group abstraction
  684. */
  685. int totempg_initialize (
  686. hdb_handle_t poll_handle,
  687. struct totem_config *totem_config)
  688. {
  689. int res;
  690. int i;
  691. struct totempg_context *con;
  692. totempg_totem_config = totem_config;
  693. totempg_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  694. totempg_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  695. totempg_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  696. totempg_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  697. totempg_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  698. totempg_log_printf = totem_config->totem_logging_configuration.log_printf;
  699. totempg_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  700. for (i = 0; i < TOTEMPG_NO_CONTEXTS; i++) {
  701. con = &totempg_contexts_array[i];
  702. memset(con, 0, sizeof(*con));
  703. con->fragmentation_data = malloc (TOTEMPG_PACKET_SIZE);
  704. if (con->fragmentation_data == 0) {
  705. return (-1);
  706. }
  707. con->totempg_reserved = 1;
  708. con->next_fragment = 1;
  709. }
  710. totemsrp_net_mtu_adjust (totem_config);
  711. res = totemmrp_initialize (
  712. poll_handle,
  713. totem_config,
  714. &totempg_stats,
  715. totempg_deliver_fn,
  716. totempg_confchg_fn,
  717. totempg_waiting_trans_ack_cb);
  718. totemmrp_callback_token_create (
  719. &callback_token_received_handle,
  720. TOTEM_CALLBACK_TOKEN_RECEIVED,
  721. 0,
  722. callback_token_received_fn,
  723. 0);
  724. totempg_size_limit = (totemmrp_avail() - 1) *
  725. (totempg_totem_config->net_mtu -
  726. sizeof (struct totempg_mcast) - 16);
  727. return (res);
  728. }
  729. void totempg_finalize (void)
  730. {
  731. pthread_mutex_lock (&totempg_mutex);
  732. totemmrp_finalize ();
  733. pthread_mutex_unlock (&totempg_mutex);
  734. }
  735. /*
  736. * Multicast a message
  737. */
  738. static int mcast_msg (
  739. struct iovec *iovec_in,
  740. unsigned int iov_len,
  741. int guarantee)
  742. {
  743. int res = 0;
  744. struct totempg_mcast mcast;
  745. struct iovec iovecs[3];
  746. struct iovec iovec[64];
  747. int i;
  748. int dest, src;
  749. int max_packet_size = 0;
  750. int copy_len = 0;
  751. int copy_base = 0;
  752. int total_size = 0;
  753. struct totempg_context *con = totempg_active_context;
  754. pthread_mutex_lock (&mcast_msg_mutex);
  755. totemmrp_event_signal (TOTEM_EVENT_NEW_MSG, 1);
  756. /*
  757. * Remove zero length iovectors from the list
  758. */
  759. assert (iov_len < 64);
  760. for (dest = 0, src = 0; src < iov_len; src++) {
  761. if (iovec_in[src].iov_len) {
  762. memcpy (&iovec[dest++], &iovec_in[src],
  763. sizeof (struct iovec));
  764. }
  765. }
  766. iov_len = dest;
  767. max_packet_size = TOTEMPG_PACKET_SIZE -
  768. (sizeof (unsigned short) * (con->mcast_packed_msg_count + 1));
  769. con->mcast_packed_msg_lens[con->mcast_packed_msg_count] = 0;
  770. /*
  771. * Check if we would overwrite new message queue
  772. */
  773. for (i = 0; i < iov_len; i++) {
  774. total_size += iovec[i].iov_len;
  775. }
  776. if (byte_count_send_ok (total_size + sizeof(unsigned short) *
  777. (con->mcast_packed_msg_count)) == 0) {
  778. pthread_mutex_unlock (&mcast_msg_mutex);
  779. return(-1);
  780. }
  781. mcast.header.version = 0;
  782. for (i = 0; i < iov_len; ) {
  783. mcast.fragmented = 0;
  784. mcast.continuation = con->fragment_continuation;
  785. copy_len = iovec[i].iov_len - copy_base;
  786. /*
  787. * If it all fits with room left over, copy it in.
  788. * We need to leave at least sizeof(short) + 1 bytes in the
  789. * fragment_buffer on exit so that max_packet_size + fragment_size
  790. * doesn't exceed the size of the fragment_buffer on the next call.
  791. */
  792. if ((copy_len + con->fragment_size) <
  793. (max_packet_size - sizeof (unsigned short))) {
  794. memcpy (&con->fragmentation_data[con->fragment_size],
  795. (char *)iovec[i].iov_base + copy_base, copy_len);
  796. con->fragment_size += copy_len;
  797. con->mcast_packed_msg_lens[con->mcast_packed_msg_count] += copy_len;
  798. con->next_fragment = 1;
  799. copy_len = 0;
  800. copy_base = 0;
  801. i++;
  802. continue;
  803. /*
  804. * If it just fits or is too big, then send out what fits.
  805. */
  806. } else {
  807. unsigned char *data_ptr;
  808. copy_len = min(copy_len, max_packet_size - con->fragment_size);
  809. if( copy_len == max_packet_size )
  810. data_ptr = (unsigned char *)iovec[i].iov_base + copy_base;
  811. else {
  812. data_ptr = con->fragmentation_data;
  813. memcpy (&con->fragmentation_data[con->fragment_size],
  814. (unsigned char *)iovec[i].iov_base + copy_base, copy_len);
  815. }
  816. memcpy (&con->fragmentation_data[con->fragment_size],
  817. (unsigned char *)iovec[i].iov_base + copy_base, copy_len);
  818. con->mcast_packed_msg_lens[con->mcast_packed_msg_count] += copy_len;
  819. /*
  820. * if we're not on the last iovec or the iovec is too large to
  821. * fit, then indicate a fragment. This also means that the next
  822. * message will have the continuation of this one.
  823. */
  824. if ((i < (iov_len - 1)) ||
  825. ((copy_base + copy_len) < iovec[i].iov_len)) {
  826. if (!con->next_fragment) {
  827. con->next_fragment++;
  828. }
  829. con->fragment_continuation = con->next_fragment;
  830. mcast.fragmented = con->next_fragment++;
  831. assert(con->fragment_continuation != 0);
  832. assert(mcast.fragmented != 0);
  833. } else {
  834. con->fragment_continuation = 0;
  835. }
  836. /*
  837. * assemble the message and send it
  838. */
  839. mcast.msg_count = ++con->mcast_packed_msg_count;
  840. iovecs[0].iov_base = (void *)&mcast;
  841. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  842. iovecs[1].iov_base = (void *)con->mcast_packed_msg_lens;
  843. iovecs[1].iov_len = con->mcast_packed_msg_count *
  844. sizeof(unsigned short);
  845. iovecs[2].iov_base = (void *)data_ptr;
  846. iovecs[2].iov_len = max_packet_size;
  847. assert (totemmrp_avail() > 0);
  848. res = totemmrp_mcast (iovecs, 3, guarantee);
  849. if (res == -1) {
  850. goto error_exit;
  851. }
  852. /*
  853. * Recalculate counts and indexes for the next.
  854. */
  855. con->mcast_packed_msg_lens[0] = 0;
  856. con->mcast_packed_msg_count = 0;
  857. con->fragment_size = 0;
  858. max_packet_size = TOTEMPG_PACKET_SIZE - (sizeof(unsigned short));
  859. /*
  860. * If the iovec all fit, go to the next iovec
  861. */
  862. if ((copy_base + copy_len) == iovec[i].iov_len) {
  863. copy_len = 0;
  864. copy_base = 0;
  865. i++;
  866. /*
  867. * Continue with the rest of the current iovec.
  868. */
  869. } else {
  870. copy_base += copy_len;
  871. }
  872. }
  873. }
  874. /*
  875. * Bump only if we added message data. This may be zero if
  876. * the last buffer just fit into the fragmentation_data buffer
  877. * and we were at the last iovec.
  878. */
  879. if (con->mcast_packed_msg_lens[con->mcast_packed_msg_count]) {
  880. con->mcast_packed_msg_count++;
  881. }
  882. error_exit:
  883. pthread_mutex_unlock (&mcast_msg_mutex);
  884. return (res);
  885. }
  886. /*
  887. * Determine if a message of msg_size could be queued
  888. */
  889. static int msg_count_send_ok (
  890. int msg_count)
  891. {
  892. int avail = 0;
  893. struct totempg_context *con = totempg_active_context;
  894. avail = totemmrp_avail ();
  895. totempg_stats.msg_queue_avail = avail;
  896. return ((avail - con->totempg_reserved) > msg_count);
  897. }
  898. static int byte_count_send_ok (
  899. int byte_count)
  900. {
  901. unsigned int msg_count = 0;
  902. int avail = 0;
  903. avail = totemmrp_avail ();
  904. msg_count = (byte_count / (totempg_totem_config->net_mtu - sizeof (struct totempg_mcast) - 16)) + 1;
  905. return (avail >= msg_count);
  906. }
  907. static int send_reserve (
  908. int msg_size)
  909. {
  910. unsigned int msg_count = 0;
  911. struct totempg_context *con = totempg_active_context;
  912. msg_count = (msg_size / (totempg_totem_config->net_mtu - sizeof (struct totempg_mcast) - 16)) + 1;
  913. con->totempg_reserved += msg_count;
  914. totempg_stats.msg_reserved = con->totempg_reserved;
  915. return (msg_count);
  916. }
  917. static void send_release (
  918. int msg_count)
  919. {
  920. struct totempg_context *con = totempg_active_context;
  921. con->totempg_reserved -= msg_count;
  922. totempg_stats.msg_reserved = con->totempg_reserved;
  923. }
  924. int totempg_callback_token_create (
  925. void **handle_out,
  926. enum totem_callback_token_type type,
  927. int delete,
  928. int (*callback_fn) (enum totem_callback_token_type type, const void *),
  929. const void *data)
  930. {
  931. unsigned int res;
  932. pthread_mutex_lock (&callback_token_mutex);
  933. res = totemmrp_callback_token_create (handle_out, type, delete,
  934. callback_fn, data);
  935. pthread_mutex_unlock (&callback_token_mutex);
  936. return (res);
  937. }
  938. void totempg_callback_token_destroy (
  939. void *handle_out)
  940. {
  941. pthread_mutex_lock (&callback_token_mutex);
  942. totemmrp_callback_token_destroy (handle_out);
  943. pthread_mutex_unlock (&callback_token_mutex);
  944. }
  945. /*
  946. * vi: set autoindent tabstop=4 shiftwidth=4 :
  947. */
  948. int totempg_groups_initialize (
  949. hdb_handle_t *handle,
  950. void (*deliver_fn) (
  951. unsigned int nodeid,
  952. const void *msg,
  953. unsigned int msg_len,
  954. int endian_conversion_required),
  955. void (*confchg_fn) (
  956. enum totem_configuration_type configuration_type,
  957. const unsigned int *member_list, size_t member_list_entries,
  958. const unsigned int *left_list, size_t left_list_entries,
  959. const unsigned int *joined_list, size_t joined_list_entries,
  960. const struct memb_ring_id *ring_id))
  961. {
  962. struct totempg_group_instance *instance;
  963. unsigned int res;
  964. pthread_mutex_lock (&totempg_mutex);
  965. res = hdb_handle_create (&totempg_groups_instance_database,
  966. sizeof (struct totempg_group_instance), handle);
  967. if (res != 0) {
  968. goto error_exit;
  969. }
  970. if (*handle > totempg_max_handle) {
  971. totempg_max_handle = *handle;
  972. }
  973. res = hdb_handle_get (&totempg_groups_instance_database, *handle,
  974. (void *)&instance);
  975. if (res != 0) {
  976. goto error_destroy;
  977. }
  978. instance->deliver_fn = deliver_fn;
  979. instance->confchg_fn = confchg_fn;
  980. instance->groups = 0;
  981. instance->groups_cnt = 0;
  982. hdb_handle_put (&totempg_groups_instance_database, *handle);
  983. pthread_mutex_unlock (&totempg_mutex);
  984. return (0);
  985. error_destroy:
  986. hdb_handle_destroy (&totempg_groups_instance_database, *handle);
  987. error_exit:
  988. pthread_mutex_unlock (&totempg_mutex);
  989. return (-1);
  990. }
  991. int totempg_groups_join (
  992. hdb_handle_t handle,
  993. const struct totempg_group *groups,
  994. size_t group_cnt)
  995. {
  996. struct totempg_group_instance *instance;
  997. struct totempg_group *new_groups;
  998. unsigned int res;
  999. pthread_mutex_lock (&totempg_mutex);
  1000. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1001. (void *)&instance);
  1002. if (res != 0) {
  1003. goto error_exit;
  1004. }
  1005. new_groups = realloc (instance->groups,
  1006. sizeof (struct totempg_group) *
  1007. (instance->groups_cnt + group_cnt));
  1008. if (new_groups == 0) {
  1009. res = ENOMEM;
  1010. goto error_exit;
  1011. }
  1012. memcpy (&new_groups[instance->groups_cnt],
  1013. groups, group_cnt * sizeof (struct totempg_group));
  1014. instance->groups = new_groups;
  1015. instance->groups_cnt += group_cnt;
  1016. hdb_handle_put (&totempg_groups_instance_database, handle);
  1017. error_exit:
  1018. pthread_mutex_unlock (&totempg_mutex);
  1019. return (res);
  1020. }
  1021. int totempg_groups_leave (
  1022. hdb_handle_t handle,
  1023. const struct totempg_group *groups,
  1024. size_t group_cnt)
  1025. {
  1026. struct totempg_group_instance *instance;
  1027. unsigned int res;
  1028. pthread_mutex_lock (&totempg_mutex);
  1029. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1030. (void *)&instance);
  1031. if (res != 0) {
  1032. goto error_exit;
  1033. }
  1034. hdb_handle_put (&totempg_groups_instance_database, handle);
  1035. error_exit:
  1036. pthread_mutex_unlock (&totempg_mutex);
  1037. return (res);
  1038. }
  1039. #define MAX_IOVECS_FROM_APP 32
  1040. #define MAX_GROUPS_PER_MSG 32
  1041. int totempg_groups_mcast_joined (
  1042. hdb_handle_t handle,
  1043. const struct iovec *iovec,
  1044. unsigned int iov_len,
  1045. int guarantee)
  1046. {
  1047. struct totempg_group_instance *instance;
  1048. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  1049. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  1050. int i;
  1051. unsigned int res;
  1052. pthread_mutex_lock (&totempg_mutex);
  1053. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1054. (void *)&instance);
  1055. if (res != 0) {
  1056. goto error_exit;
  1057. }
  1058. /*
  1059. * Build group_len structure and the iovec_mcast structure
  1060. */
  1061. group_len[0] = instance->groups_cnt;
  1062. for (i = 0; i < instance->groups_cnt; i++) {
  1063. group_len[i + 1] = instance->groups[i].group_len;
  1064. iovec_mcast[i + 1].iov_len = instance->groups[i].group_len;
  1065. iovec_mcast[i + 1].iov_base = (void *) instance->groups[i].group;
  1066. }
  1067. iovec_mcast[0].iov_len = (instance->groups_cnt + 1) * sizeof (unsigned short);
  1068. iovec_mcast[0].iov_base = group_len;
  1069. for (i = 0; i < iov_len; i++) {
  1070. iovec_mcast[i + instance->groups_cnt + 1].iov_len = iovec[i].iov_len;
  1071. iovec_mcast[i + instance->groups_cnt + 1].iov_base = iovec[i].iov_base;
  1072. }
  1073. res = mcast_msg (iovec_mcast, iov_len + instance->groups_cnt + 1, guarantee);
  1074. hdb_handle_put (&totempg_groups_instance_database, handle);
  1075. error_exit:
  1076. pthread_mutex_unlock (&totempg_mutex);
  1077. return (res);
  1078. }
  1079. int totempg_groups_joined_reserve (
  1080. hdb_handle_t handle,
  1081. const struct iovec *iovec,
  1082. unsigned int iov_len)
  1083. {
  1084. struct totempg_group_instance *instance;
  1085. unsigned int size = 0;
  1086. unsigned int i;
  1087. unsigned int res;
  1088. unsigned int reserved = 0;
  1089. pthread_mutex_lock (&totempg_mutex);
  1090. pthread_mutex_lock (&mcast_msg_mutex);
  1091. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1092. (void *)&instance);
  1093. if (res != 0) {
  1094. goto error_exit;
  1095. }
  1096. for (i = 0; i < instance->groups_cnt; i++) {
  1097. size += instance->groups[i].group_len;
  1098. }
  1099. for (i = 0; i < iov_len; i++) {
  1100. size += iovec[i].iov_len;
  1101. }
  1102. if (size >= totempg_size_limit) {
  1103. reserved = -1;
  1104. goto error_put;
  1105. }
  1106. reserved = send_reserve (size);
  1107. if (msg_count_send_ok (reserved) == 0) {
  1108. send_release (reserved);
  1109. reserved = 0;
  1110. }
  1111. error_put:
  1112. hdb_handle_put (&totempg_groups_instance_database, handle);
  1113. error_exit:
  1114. pthread_mutex_unlock (&mcast_msg_mutex);
  1115. pthread_mutex_unlock (&totempg_mutex);
  1116. return (reserved);
  1117. }
  1118. int totempg_groups_joined_release (int msg_count)
  1119. {
  1120. pthread_mutex_lock (&totempg_mutex);
  1121. pthread_mutex_lock (&mcast_msg_mutex);
  1122. send_release (msg_count);
  1123. pthread_mutex_unlock (&mcast_msg_mutex);
  1124. pthread_mutex_unlock (&totempg_mutex);
  1125. return 0;
  1126. }
  1127. int totempg_groups_mcast_groups (
  1128. hdb_handle_t handle,
  1129. int guarantee,
  1130. const struct totempg_group *groups,
  1131. size_t groups_cnt,
  1132. const struct iovec *iovec,
  1133. unsigned int iov_len)
  1134. {
  1135. struct totempg_group_instance *instance;
  1136. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  1137. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  1138. int i;
  1139. unsigned int res;
  1140. pthread_mutex_lock (&totempg_mutex);
  1141. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1142. (void *)&instance);
  1143. if (res != 0) {
  1144. goto error_exit;
  1145. }
  1146. /*
  1147. * Build group_len structure and the iovec_mcast structure
  1148. */
  1149. group_len[0] = groups_cnt;
  1150. for (i = 0; i < groups_cnt; i++) {
  1151. group_len[i + 1] = groups[i].group_len;
  1152. iovec_mcast[i + 1].iov_len = groups[i].group_len;
  1153. iovec_mcast[i + 1].iov_base = (void *) groups[i].group;
  1154. }
  1155. iovec_mcast[0].iov_len = (groups_cnt + 1) * sizeof (unsigned short);
  1156. iovec_mcast[0].iov_base = group_len;
  1157. for (i = 0; i < iov_len; i++) {
  1158. iovec_mcast[i + groups_cnt + 1].iov_len = iovec[i].iov_len;
  1159. iovec_mcast[i + groups_cnt + 1].iov_base = iovec[i].iov_base;
  1160. }
  1161. res = mcast_msg (iovec_mcast, iov_len + groups_cnt + 1, guarantee);
  1162. hdb_handle_put (&totempg_groups_instance_database, handle);
  1163. error_exit:
  1164. pthread_mutex_unlock (&totempg_mutex);
  1165. return (res);
  1166. }
  1167. /*
  1168. * Returns -1 if error, 0 if can't send, 1 if can send the message
  1169. */
  1170. int totempg_groups_send_ok_groups (
  1171. hdb_handle_t handle,
  1172. const struct totempg_group *groups,
  1173. size_t groups_cnt,
  1174. const struct iovec *iovec,
  1175. unsigned int iov_len)
  1176. {
  1177. struct totempg_group_instance *instance;
  1178. unsigned int size = 0;
  1179. unsigned int i;
  1180. unsigned int res;
  1181. pthread_mutex_lock (&totempg_mutex);
  1182. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1183. (void *)&instance);
  1184. if (res != 0) {
  1185. goto error_exit;
  1186. }
  1187. for (i = 0; i < groups_cnt; i++) {
  1188. size += groups[i].group_len;
  1189. }
  1190. for (i = 0; i < iov_len; i++) {
  1191. size += iovec[i].iov_len;
  1192. }
  1193. res = msg_count_send_ok (size);
  1194. hdb_handle_put (&totempg_groups_instance_database, handle);
  1195. error_exit:
  1196. pthread_mutex_unlock (&totempg_mutex);
  1197. return (res);
  1198. }
  1199. int totempg_ifaces_get (
  1200. unsigned int nodeid,
  1201. struct totem_ip_address *interfaces,
  1202. char ***status,
  1203. unsigned int *iface_count)
  1204. {
  1205. int res;
  1206. res = totemmrp_ifaces_get (
  1207. nodeid,
  1208. interfaces,
  1209. status,
  1210. iface_count);
  1211. return (res);
  1212. }
  1213. void totempg_event_signal (enum totem_event_type type, int value)
  1214. {
  1215. totemmrp_event_signal (type, value);
  1216. }
  1217. void* totempg_get_stats (void)
  1218. {
  1219. return &totempg_stats;
  1220. }
  1221. int totempg_crypto_set (
  1222. unsigned int type)
  1223. {
  1224. int res;
  1225. res = totemmrp_crypto_set (
  1226. type);
  1227. return (res);
  1228. }
  1229. int totempg_ring_reenable (void)
  1230. {
  1231. int res;
  1232. res = totemmrp_ring_reenable ();
  1233. return (res);
  1234. }
  1235. const char *totempg_ifaces_print (unsigned int nodeid)
  1236. {
  1237. static char iface_string[256 * INTERFACE_MAX];
  1238. char one_iface[64];
  1239. struct totem_ip_address interfaces[INTERFACE_MAX];
  1240. char **status;
  1241. unsigned int iface_count;
  1242. unsigned int i;
  1243. int res;
  1244. iface_string[0] = '\0';
  1245. res = totempg_ifaces_get (nodeid, interfaces, &status, &iface_count);
  1246. if (res == -1) {
  1247. return ("no interface found for nodeid");
  1248. }
  1249. for (i = 0; i < iface_count; i++) {
  1250. sprintf (one_iface, "r(%d) ip(%s) ",
  1251. i, totemip_print (&interfaces[i]));
  1252. strcat (iface_string, one_iface);
  1253. }
  1254. return (iface_string);
  1255. }
  1256. unsigned int totempg_my_nodeid_get (void)
  1257. {
  1258. return (totemmrp_my_nodeid_get());
  1259. }
  1260. int totempg_my_family_get (void)
  1261. {
  1262. return (totemmrp_my_family_get());
  1263. }
  1264. extern void totempg_service_ready_register (
  1265. void (*totem_service_ready) (void))
  1266. {
  1267. totemmrp_service_ready_register (totem_service_ready);
  1268. }
  1269. extern int totempg_member_add (
  1270. const struct totem_ip_address *member,
  1271. int ring_no);
  1272. extern int totempg_member_remove (
  1273. const struct totem_ip_address *member,
  1274. int ring_no);
  1275. void totempg_trans_ack (void)
  1276. {
  1277. totemmrp_trans_ack ();
  1278. }