totempg.c 39 KB

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