totempg.c 39 KB

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