totempg.c 37 KB

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