totempg.c 38 KB

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