totempg.c 40 KB

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