totempg.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  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 "util.h"
  105. #include "totemsrp.h"
  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. size_t expected_msg_len;
  522. assembly = assembly_ref (nodeid);
  523. assert (assembly);
  524. if (msg_len < sizeof(struct totempg_mcast)) {
  525. log_printf(LOG_WARNING,
  526. "Message (totempg_mcast) received from node " CS_PRI_NODE_ID " is too short... Ignoring.", nodeid);
  527. return ;
  528. }
  529. /*
  530. * Assemble the header into one block of data and
  531. * assemble the packet contents into one block of data to simplify delivery
  532. */
  533. mcast = (struct totempg_mcast *)msg;
  534. if (endian_conversion_required) {
  535. mcast->msg_count = swab16 (mcast->msg_count);
  536. }
  537. msg_count = mcast->msg_count;
  538. datasize = sizeof (struct totempg_mcast) +
  539. msg_count * sizeof (unsigned short);
  540. if (msg_len < datasize) {
  541. log_printf(LOG_WARNING,
  542. "Message (totempg_mcast datasize) received from node " CS_PRI_NODE_ID
  543. " is too short... Ignoring.", nodeid);
  544. return ;
  545. }
  546. memcpy (header, msg, datasize);
  547. data = msg;
  548. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  549. expected_msg_len = datasize;
  550. for (i = 0; i < mcast->msg_count; i++) {
  551. if (endian_conversion_required) {
  552. msg_lens[i] = swab16 (msg_lens[i]);
  553. }
  554. expected_msg_len += msg_lens[i];
  555. }
  556. if (msg_len != expected_msg_len) {
  557. log_printf(LOG_WARNING,
  558. "Message (totempg_mcast) received from node " CS_PRI_NODE_ID
  559. " doesn't have expected length of %zu (has %u) bytes... Ignoring.",
  560. nodeid, expected_msg_len, msg_len);
  561. return ;
  562. }
  563. assert((assembly->index+msg_len) < sizeof(assembly->data));
  564. memcpy (&assembly->data[assembly->index], &data[datasize],
  565. msg_len - datasize);
  566. /*
  567. * If the last message in the buffer is a fragment, then we
  568. * can't deliver it. We'll first deliver the full messages
  569. * then adjust the assembly buffer so we can add the rest of the
  570. * fragment when it arrives.
  571. */
  572. msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
  573. continuation = mcast->continuation;
  574. iov_delv.iov_base = (void *)&assembly->data[0];
  575. iov_delv.iov_len = assembly->index + msg_lens[0];
  576. /*
  577. * Make sure that if this message is a continuation, that it
  578. * matches the sequence number of the previous fragment.
  579. * Also, if the first packed message is a continuation
  580. * of a previous message, but the assembly buffer
  581. * is empty, then we need to discard it since we can't
  582. * assemble a complete message. Likewise, if this message isn't a
  583. * continuation and the assembly buffer is empty, we have to discard
  584. * the continued message.
  585. */
  586. start = 0;
  587. if (assembly->throw_away_mode == THROW_AWAY_ACTIVE) {
  588. /* Throw away the first msg block */
  589. if (mcast->fragmented == 0 || mcast->fragmented == 1) {
  590. assembly->throw_away_mode = THROW_AWAY_INACTIVE;
  591. assembly->index += msg_lens[0];
  592. iov_delv.iov_base = (void *)&assembly->data[assembly->index];
  593. iov_delv.iov_len = msg_lens[1];
  594. start = 1;
  595. }
  596. } else
  597. if (assembly->throw_away_mode == THROW_AWAY_INACTIVE) {
  598. if (continuation == assembly->last_frag_num) {
  599. assembly->last_frag_num = mcast->fragmented;
  600. for (i = start; i < msg_count; i++) {
  601. app_deliver_fn(nodeid, iov_delv.iov_base, iov_delv.iov_len,
  602. endian_conversion_required);
  603. assembly->index += msg_lens[i];
  604. iov_delv.iov_base = (void *)&assembly->data[assembly->index];
  605. if (i < (msg_count - 1)) {
  606. iov_delv.iov_len = msg_lens[i + 1];
  607. }
  608. }
  609. } else {
  610. log_printf (LOG_DEBUG, "fragmented continuation %u is not equal to assembly last_frag_num %u",
  611. continuation, assembly->last_frag_num);
  612. assembly->throw_away_mode = THROW_AWAY_ACTIVE;
  613. }
  614. }
  615. if (mcast->fragmented == 0) {
  616. /*
  617. * End of messages, dereference assembly struct
  618. */
  619. assembly->last_frag_num = 0;
  620. assembly->index = 0;
  621. assembly_deref (assembly);
  622. } else {
  623. /*
  624. * Message is fragmented, keep around assembly list
  625. */
  626. if (mcast->msg_count > 1) {
  627. memmove (&assembly->data[0],
  628. &assembly->data[assembly->index],
  629. msg_lens[msg_count]);
  630. assembly->index = 0;
  631. }
  632. assembly->index += msg_lens[msg_count];
  633. }
  634. }
  635. /*
  636. * Totem Process Group Abstraction
  637. * depends on poll abstraction, POSIX, IPV4
  638. */
  639. void *callback_token_received_handle;
  640. int callback_token_received_fn (enum totem_callback_token_type type,
  641. const void *data)
  642. {
  643. struct totempg_mcast mcast;
  644. struct iovec iovecs[3];
  645. if (totempg_threaded_mode == 1) {
  646. pthread_mutex_lock (&mcast_msg_mutex);
  647. }
  648. if (mcast_packed_msg_count == 0) {
  649. if (totempg_threaded_mode == 1) {
  650. pthread_mutex_unlock (&mcast_msg_mutex);
  651. }
  652. return (0);
  653. }
  654. if (totemsrp_avail(totemsrp_context) == 0) {
  655. if (totempg_threaded_mode == 1) {
  656. pthread_mutex_unlock (&mcast_msg_mutex);
  657. }
  658. return (0);
  659. }
  660. mcast.header.version = 0;
  661. mcast.header.type = 0;
  662. mcast.fragmented = 0;
  663. /*
  664. * Was the first message in this buffer a continuation of a
  665. * fragmented message?
  666. */
  667. mcast.continuation = fragment_continuation;
  668. fragment_continuation = 0;
  669. mcast.msg_count = mcast_packed_msg_count;
  670. iovecs[0].iov_base = (void *)&mcast;
  671. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  672. iovecs[1].iov_base = (void *)mcast_packed_msg_lens;
  673. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  674. iovecs[2].iov_base = (void *)&fragmentation_data[0];
  675. iovecs[2].iov_len = fragment_size;
  676. (void)totemsrp_mcast (totemsrp_context, iovecs, 3, 0);
  677. mcast_packed_msg_count = 0;
  678. fragment_size = 0;
  679. if (totempg_threaded_mode == 1) {
  680. pthread_mutex_unlock (&mcast_msg_mutex);
  681. }
  682. return (0);
  683. }
  684. /*
  685. * Initialize the totem process group abstraction
  686. */
  687. int totempg_initialize (
  688. qb_loop_t *poll_handle,
  689. struct totem_config *totem_config)
  690. {
  691. int res;
  692. totempg_totem_config = totem_config;
  693. totempg_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  694. totempg_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  695. totempg_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  696. totempg_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  697. totempg_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  698. totempg_log_printf = totem_config->totem_logging_configuration.log_printf;
  699. totempg_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  700. fragmentation_data = malloc (TOTEMPG_PACKET_SIZE);
  701. if (fragmentation_data == 0) {
  702. return (-1);
  703. }
  704. totemsrp_net_mtu_adjust (totem_config);
  705. res = totemsrp_initialize (
  706. poll_handle,
  707. &totemsrp_context,
  708. totem_config,
  709. &totempg_stats,
  710. totempg_deliver_fn,
  711. totempg_confchg_fn,
  712. totempg_waiting_trans_ack_cb);
  713. if (res == -1) {
  714. goto error_exit;
  715. }
  716. totemsrp_callback_token_create (
  717. totemsrp_context,
  718. &callback_token_received_handle,
  719. TOTEM_CALLBACK_TOKEN_RECEIVED,
  720. 0,
  721. callback_token_received_fn,
  722. 0);
  723. totempg_size_limit = (totemsrp_avail(totemsrp_context) - 1) *
  724. (totempg_totem_config->net_mtu -
  725. sizeof (struct totempg_mcast) - 16);
  726. qb_list_init (&totempg_groups_list);
  727. error_exit:
  728. return (res);
  729. }
  730. void totempg_finalize (void)
  731. {
  732. if (totempg_threaded_mode == 1) {
  733. pthread_mutex_lock (&totempg_mutex);
  734. }
  735. totemsrp_finalize (totemsrp_context);
  736. if (totempg_threaded_mode == 1) {
  737. pthread_mutex_unlock (&totempg_mutex);
  738. }
  739. }
  740. /*
  741. * Multicast a message
  742. */
  743. static int mcast_msg (
  744. struct iovec *iovec_in,
  745. unsigned int iov_len,
  746. int guarantee)
  747. {
  748. int res = 0;
  749. struct totempg_mcast mcast;
  750. struct iovec iovecs[3];
  751. struct iovec iovec[64];
  752. int i;
  753. int dest, src;
  754. int max_packet_size = 0;
  755. int copy_len = 0;
  756. int copy_base = 0;
  757. int total_size = 0;
  758. if (totempg_threaded_mode == 1) {
  759. pthread_mutex_lock (&mcast_msg_mutex);
  760. }
  761. totemsrp_event_signal (totemsrp_context, TOTEM_EVENT_NEW_MSG, 1);
  762. /*
  763. * Remove zero length iovectors from the list
  764. */
  765. assert (iov_len < 64);
  766. for (dest = 0, src = 0; src < iov_len; src++) {
  767. if (iovec_in[src].iov_len) {
  768. memcpy (&iovec[dest++], &iovec_in[src],
  769. sizeof (struct iovec));
  770. }
  771. }
  772. iov_len = dest;
  773. max_packet_size = TOTEMPG_PACKET_SIZE -
  774. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  775. mcast_packed_msg_lens[mcast_packed_msg_count] = 0;
  776. /*
  777. * Check if we would overwrite new message queue
  778. */
  779. for (i = 0; i < iov_len; i++) {
  780. total_size += iovec[i].iov_len;
  781. }
  782. if (byte_count_send_ok (total_size + sizeof(unsigned short) *
  783. (mcast_packed_msg_count)) == 0) {
  784. if (totempg_threaded_mode == 1) {
  785. pthread_mutex_unlock (&mcast_msg_mutex);
  786. }
  787. return(-1);
  788. }
  789. memset(&mcast, 0, sizeof(mcast));
  790. mcast.header.version = 0;
  791. for (i = 0; i < iov_len; ) {
  792. mcast.fragmented = 0;
  793. mcast.continuation = fragment_continuation;
  794. copy_len = iovec[i].iov_len - copy_base;
  795. /*
  796. * If it all fits with room left over, copy it in.
  797. * We need to leave at least sizeof(short) + 1 bytes in the
  798. * fragment_buffer on exit so that max_packet_size + fragment_size
  799. * doesn't exceed the size of the fragment_buffer on the next call.
  800. */
  801. if ((iovec[i].iov_len + fragment_size) <
  802. (max_packet_size - sizeof (unsigned short))) {
  803. memcpy (&fragmentation_data[fragment_size],
  804. (char *)iovec[i].iov_base + copy_base, copy_len);
  805. fragment_size += copy_len;
  806. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  807. next_fragment = 1;
  808. copy_len = 0;
  809. copy_base = 0;
  810. i++;
  811. continue;
  812. /*
  813. * If it just fits or is too big, then send out what fits.
  814. */
  815. } else {
  816. unsigned char *data_ptr;
  817. copy_len = min(copy_len, max_packet_size - fragment_size);
  818. if( copy_len == max_packet_size )
  819. data_ptr = (unsigned char *)iovec[i].iov_base + copy_base;
  820. else {
  821. data_ptr = fragmentation_data;
  822. }
  823. memcpy (&fragmentation_data[fragment_size],
  824. (unsigned char *)iovec[i].iov_base + copy_base, copy_len);
  825. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  826. /*
  827. * if we're not on the last iovec or the iovec is too large to
  828. * fit, then indicate a fragment. This also means that the next
  829. * message will have the continuation of this one.
  830. */
  831. if ((i < (iov_len - 1)) ||
  832. ((copy_base + copy_len) < iovec[i].iov_len)) {
  833. if (!next_fragment) {
  834. next_fragment++;
  835. }
  836. fragment_continuation = next_fragment;
  837. mcast.fragmented = next_fragment++;
  838. assert(fragment_continuation != 0);
  839. assert(mcast.fragmented != 0);
  840. } else {
  841. fragment_continuation = 0;
  842. }
  843. /*
  844. * assemble the message and send it
  845. */
  846. mcast.msg_count = ++mcast_packed_msg_count;
  847. iovecs[0].iov_base = (void *)&mcast;
  848. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  849. iovecs[1].iov_base = (void *)mcast_packed_msg_lens;
  850. iovecs[1].iov_len = mcast_packed_msg_count *
  851. sizeof(unsigned short);
  852. iovecs[2].iov_base = (void *)data_ptr;
  853. iovecs[2].iov_len = fragment_size + copy_len;
  854. assert (totemsrp_avail(totemsrp_context) > 0);
  855. res = totemsrp_mcast (totemsrp_context, iovecs, 3, guarantee);
  856. if (res == -1) {
  857. goto error_exit;
  858. }
  859. /*
  860. * Recalculate counts and indexes for the next.
  861. */
  862. mcast_packed_msg_lens[0] = 0;
  863. mcast_packed_msg_count = 0;
  864. fragment_size = 0;
  865. max_packet_size = TOTEMPG_PACKET_SIZE - (sizeof(unsigned short));
  866. /*
  867. * If the iovec all fit, go to the next iovec
  868. */
  869. if ((copy_base + copy_len) == iovec[i].iov_len) {
  870. copy_len = 0;
  871. copy_base = 0;
  872. i++;
  873. /*
  874. * Continue with the rest of the current iovec.
  875. */
  876. } else {
  877. copy_base += copy_len;
  878. }
  879. }
  880. }
  881. /*
  882. * Bump only if we added message data. This may be zero if
  883. * the last buffer just fit into the fragmentation_data buffer
  884. * and we were at the last iovec.
  885. */
  886. if (mcast_packed_msg_lens[mcast_packed_msg_count]) {
  887. mcast_packed_msg_count++;
  888. }
  889. error_exit:
  890. if (totempg_threaded_mode == 1) {
  891. pthread_mutex_unlock (&mcast_msg_mutex);
  892. }
  893. return (res);
  894. }
  895. /*
  896. * Determine if a message of msg_size could be queued
  897. */
  898. static int msg_count_send_ok (
  899. int msg_count)
  900. {
  901. int avail = 0;
  902. avail = totemsrp_avail (totemsrp_context);
  903. totempg_stats.msg_queue_avail = avail;
  904. return ((avail - totempg_reserved) > msg_count);
  905. }
  906. static int byte_count_send_ok (
  907. int byte_count)
  908. {
  909. unsigned int msg_count = 0;
  910. int avail = 0;
  911. avail = totemsrp_avail (totemsrp_context);
  912. msg_count = (byte_count / (totempg_totem_config->net_mtu - sizeof (struct totempg_mcast) - 16)) + 1;
  913. return (avail >= msg_count);
  914. }
  915. static int send_reserve (
  916. int msg_size)
  917. {
  918. unsigned int msg_count = 0;
  919. msg_count = (msg_size / (totempg_totem_config->net_mtu - sizeof (struct totempg_mcast) - 16)) + 1;
  920. totempg_reserved += msg_count;
  921. totempg_stats.msg_reserved = totempg_reserved;
  922. return (msg_count);
  923. }
  924. static void send_release (
  925. int msg_count)
  926. {
  927. totempg_reserved -= msg_count;
  928. totempg_stats.msg_reserved = totempg_reserved;
  929. }
  930. #ifndef HAVE_SMALL_MEMORY_FOOTPRINT
  931. #undef MESSAGE_QUEUE_MAX
  932. #define MESSAGE_QUEUE_MAX ((4 * MESSAGE_SIZE_MAX) / totempg_totem_config->net_mtu)
  933. #endif /* HAVE_SMALL_MEMORY_FOOTPRINT */
  934. static uint32_t q_level_precent_used(void)
  935. {
  936. return (100 - (((totemsrp_avail(totemsrp_context) - totempg_reserved) * 100) / MESSAGE_QUEUE_MAX));
  937. }
  938. int totempg_callback_token_create (
  939. void **handle_out,
  940. enum totem_callback_token_type type,
  941. int delete,
  942. int (*callback_fn) (enum totem_callback_token_type type, const void *),
  943. const void *data)
  944. {
  945. unsigned int res;
  946. if (totempg_threaded_mode == 1) {
  947. pthread_mutex_lock (&callback_token_mutex);
  948. }
  949. res = totemsrp_callback_token_create (totemsrp_context, handle_out, type, delete,
  950. callback_fn, data);
  951. if (totempg_threaded_mode == 1) {
  952. pthread_mutex_unlock (&callback_token_mutex);
  953. }
  954. return (res);
  955. }
  956. void totempg_callback_token_destroy (
  957. void *handle_out)
  958. {
  959. if (totempg_threaded_mode == 1) {
  960. pthread_mutex_lock (&callback_token_mutex);
  961. }
  962. totemsrp_callback_token_destroy (totemsrp_context, handle_out);
  963. if (totempg_threaded_mode == 1) {
  964. pthread_mutex_unlock (&callback_token_mutex);
  965. }
  966. }
  967. /*
  968. * vi: set autoindent tabstop=4 shiftwidth=4 :
  969. */
  970. int totempg_groups_initialize (
  971. void **totempg_groups_instance,
  972. void (*deliver_fn) (
  973. unsigned int nodeid,
  974. const void *msg,
  975. unsigned int msg_len,
  976. int endian_conversion_required),
  977. void (*confchg_fn) (
  978. enum totem_configuration_type configuration_type,
  979. const unsigned int *member_list, size_t member_list_entries,
  980. const unsigned int *left_list, size_t left_list_entries,
  981. const unsigned int *joined_list, size_t joined_list_entries,
  982. const struct memb_ring_id *ring_id))
  983. {
  984. struct totempg_group_instance *instance;
  985. if (totempg_threaded_mode == 1) {
  986. pthread_mutex_lock (&totempg_mutex);
  987. }
  988. instance = malloc (sizeof (struct totempg_group_instance));
  989. if (instance == NULL) {
  990. goto error_exit;
  991. }
  992. instance->deliver_fn = deliver_fn;
  993. instance->confchg_fn = confchg_fn;
  994. instance->groups = 0;
  995. instance->groups_cnt = 0;
  996. instance->q_level = QB_LOOP_MED;
  997. qb_list_init (&instance->list);
  998. qb_list_add (&instance->list, &totempg_groups_list);
  999. if (totempg_threaded_mode == 1) {
  1000. pthread_mutex_unlock (&totempg_mutex);
  1001. }
  1002. *totempg_groups_instance = instance;
  1003. return (0);
  1004. error_exit:
  1005. if (totempg_threaded_mode == 1) {
  1006. pthread_mutex_unlock (&totempg_mutex);
  1007. }
  1008. return (-1);
  1009. }
  1010. int totempg_groups_join (
  1011. void *totempg_groups_instance,
  1012. const struct totempg_group *groups,
  1013. size_t group_cnt)
  1014. {
  1015. struct totempg_group_instance *instance = (struct totempg_group_instance *)totempg_groups_instance;
  1016. struct totempg_group *new_groups;
  1017. int res = 0;
  1018. if (totempg_threaded_mode == 1) {
  1019. pthread_mutex_lock (&totempg_mutex);
  1020. }
  1021. new_groups = realloc (instance->groups,
  1022. sizeof (struct totempg_group) *
  1023. (instance->groups_cnt + group_cnt));
  1024. if (new_groups == 0) {
  1025. res = -1;
  1026. goto error_exit;
  1027. }
  1028. memcpy (&new_groups[instance->groups_cnt],
  1029. groups, group_cnt * sizeof (struct totempg_group));
  1030. instance->groups = new_groups;
  1031. instance->groups_cnt += group_cnt;
  1032. error_exit:
  1033. if (totempg_threaded_mode == 1) {
  1034. pthread_mutex_unlock (&totempg_mutex);
  1035. }
  1036. return (res);
  1037. }
  1038. int totempg_groups_leave (
  1039. void *totempg_groups_instance,
  1040. const struct totempg_group *groups,
  1041. size_t group_cnt)
  1042. {
  1043. if (totempg_threaded_mode == 1) {
  1044. pthread_mutex_lock (&totempg_mutex);
  1045. }
  1046. if (totempg_threaded_mode == 1) {
  1047. pthread_mutex_unlock (&totempg_mutex);
  1048. }
  1049. return (0);
  1050. }
  1051. #define MAX_IOVECS_FROM_APP 32
  1052. #define MAX_GROUPS_PER_MSG 32
  1053. int totempg_groups_mcast_joined (
  1054. void *totempg_groups_instance,
  1055. const struct iovec *iovec,
  1056. unsigned int iov_len,
  1057. int guarantee)
  1058. {
  1059. struct totempg_group_instance *instance = (struct totempg_group_instance *)totempg_groups_instance;
  1060. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  1061. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  1062. int i;
  1063. unsigned int res;
  1064. if (totempg_threaded_mode == 1) {
  1065. pthread_mutex_lock (&totempg_mutex);
  1066. }
  1067. /*
  1068. * Build group_len structure and the iovec_mcast structure
  1069. */
  1070. group_len[0] = instance->groups_cnt;
  1071. for (i = 0; i < instance->groups_cnt; i++) {
  1072. group_len[i + 1] = instance->groups[i].group_len;
  1073. iovec_mcast[i + 1].iov_len = instance->groups[i].group_len;
  1074. iovec_mcast[i + 1].iov_base = (void *) instance->groups[i].group;
  1075. }
  1076. iovec_mcast[0].iov_len = (instance->groups_cnt + 1) * sizeof (unsigned short);
  1077. iovec_mcast[0].iov_base = group_len;
  1078. for (i = 0; i < iov_len; i++) {
  1079. iovec_mcast[i + instance->groups_cnt + 1].iov_len = iovec[i].iov_len;
  1080. iovec_mcast[i + instance->groups_cnt + 1].iov_base = iovec[i].iov_base;
  1081. }
  1082. res = mcast_msg (iovec_mcast, iov_len + instance->groups_cnt + 1, guarantee);
  1083. if (totempg_threaded_mode == 1) {
  1084. pthread_mutex_unlock (&totempg_mutex);
  1085. }
  1086. return (res);
  1087. }
  1088. static void check_q_level(
  1089. void *totempg_groups_instance)
  1090. {
  1091. struct totempg_group_instance *instance = (struct totempg_group_instance *)totempg_groups_instance;
  1092. int32_t old_level = instance->q_level;
  1093. int32_t percent_used = q_level_precent_used();
  1094. if (percent_used >= 75 && instance->q_level != TOTEM_Q_LEVEL_CRITICAL) {
  1095. instance->q_level = TOTEM_Q_LEVEL_CRITICAL;
  1096. } else if (percent_used < 30 && instance->q_level != TOTEM_Q_LEVEL_LOW) {
  1097. instance->q_level = TOTEM_Q_LEVEL_LOW;
  1098. } else if (percent_used > 40 && percent_used < 50 && instance->q_level != TOTEM_Q_LEVEL_GOOD) {
  1099. instance->q_level = TOTEM_Q_LEVEL_GOOD;
  1100. } else if (percent_used > 60 && percent_used < 70 && instance->q_level != TOTEM_Q_LEVEL_HIGH) {
  1101. instance->q_level = TOTEM_Q_LEVEL_HIGH;
  1102. }
  1103. if (totem_queue_level_changed && old_level != instance->q_level) {
  1104. totem_queue_level_changed(instance->q_level);
  1105. }
  1106. }
  1107. void totempg_check_q_level(
  1108. void *totempg_groups_instance)
  1109. {
  1110. struct totempg_group_instance *instance = (struct totempg_group_instance *)totempg_groups_instance;
  1111. check_q_level(instance);
  1112. }
  1113. int totempg_groups_joined_reserve (
  1114. void *totempg_groups_instance,
  1115. const struct iovec *iovec,
  1116. unsigned int iov_len)
  1117. {
  1118. struct totempg_group_instance *instance = (struct totempg_group_instance *)totempg_groups_instance;
  1119. unsigned int size = 0;
  1120. unsigned int i;
  1121. unsigned int reserved = 0;
  1122. if (totempg_threaded_mode == 1) {
  1123. pthread_mutex_lock (&totempg_mutex);
  1124. pthread_mutex_lock (&mcast_msg_mutex);
  1125. }
  1126. for (i = 0; i < instance->groups_cnt; i++) {
  1127. size += instance->groups[i].group_len;
  1128. }
  1129. for (i = 0; i < iov_len; i++) {
  1130. size += iovec[i].iov_len;
  1131. }
  1132. if (size >= totempg_size_limit) {
  1133. reserved = -1;
  1134. goto error_exit;
  1135. }
  1136. if (byte_count_send_ok (size)) {
  1137. reserved = send_reserve (size);
  1138. } else {
  1139. reserved = 0;
  1140. }
  1141. error_exit:
  1142. check_q_level(instance);
  1143. if (totempg_threaded_mode == 1) {
  1144. pthread_mutex_unlock (&mcast_msg_mutex);
  1145. pthread_mutex_unlock (&totempg_mutex);
  1146. }
  1147. return (reserved);
  1148. }
  1149. int totempg_groups_joined_release (int msg_count)
  1150. {
  1151. if (totempg_threaded_mode == 1) {
  1152. pthread_mutex_lock (&totempg_mutex);
  1153. pthread_mutex_lock (&mcast_msg_mutex);
  1154. }
  1155. send_release (msg_count);
  1156. if (totempg_threaded_mode == 1) {
  1157. pthread_mutex_unlock (&mcast_msg_mutex);
  1158. pthread_mutex_unlock (&totempg_mutex);
  1159. }
  1160. return 0;
  1161. }
  1162. int totempg_groups_mcast_groups (
  1163. void *totempg_groups_instance,
  1164. int guarantee,
  1165. const struct totempg_group *groups,
  1166. size_t groups_cnt,
  1167. const struct iovec *iovec,
  1168. unsigned int iov_len)
  1169. {
  1170. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  1171. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  1172. int i;
  1173. unsigned int res;
  1174. if (totempg_threaded_mode == 1) {
  1175. pthread_mutex_lock (&totempg_mutex);
  1176. }
  1177. /*
  1178. * Build group_len structure and the iovec_mcast structure
  1179. */
  1180. group_len[0] = groups_cnt;
  1181. for (i = 0; i < groups_cnt; i++) {
  1182. group_len[i + 1] = groups[i].group_len;
  1183. iovec_mcast[i + 1].iov_len = groups[i].group_len;
  1184. iovec_mcast[i + 1].iov_base = (void *) groups[i].group;
  1185. }
  1186. iovec_mcast[0].iov_len = (groups_cnt + 1) * sizeof (unsigned short);
  1187. iovec_mcast[0].iov_base = group_len;
  1188. for (i = 0; i < iov_len; i++) {
  1189. iovec_mcast[i + groups_cnt + 1].iov_len = iovec[i].iov_len;
  1190. iovec_mcast[i + groups_cnt + 1].iov_base = iovec[i].iov_base;
  1191. }
  1192. res = mcast_msg (iovec_mcast, iov_len + groups_cnt + 1, guarantee);
  1193. if (totempg_threaded_mode == 1) {
  1194. pthread_mutex_unlock (&totempg_mutex);
  1195. }
  1196. return (res);
  1197. }
  1198. /*
  1199. * Returns -1 if error, 0 if can't send, 1 if can send the message
  1200. */
  1201. int totempg_groups_send_ok_groups (
  1202. void *totempg_groups_instance,
  1203. const struct totempg_group *groups,
  1204. size_t groups_cnt,
  1205. const struct iovec *iovec,
  1206. unsigned int iov_len)
  1207. {
  1208. unsigned int size = 0;
  1209. unsigned int i;
  1210. unsigned int res;
  1211. if (totempg_threaded_mode == 1) {
  1212. pthread_mutex_lock (&totempg_mutex);
  1213. }
  1214. for (i = 0; i < groups_cnt; i++) {
  1215. size += groups[i].group_len;
  1216. }
  1217. for (i = 0; i < iov_len; i++) {
  1218. size += iovec[i].iov_len;
  1219. }
  1220. res = msg_count_send_ok (size);
  1221. if (totempg_threaded_mode == 1) {
  1222. pthread_mutex_unlock (&totempg_mutex);
  1223. }
  1224. return (res);
  1225. }
  1226. int totempg_iface_set (
  1227. struct totem_ip_address *interface_addr,
  1228. unsigned short ip_port,
  1229. unsigned int iface_no)
  1230. {
  1231. int res;
  1232. res = totemsrp_iface_set (
  1233. totemsrp_context,
  1234. interface_addr,
  1235. ip_port,
  1236. iface_no);
  1237. return (res);
  1238. }
  1239. int totempg_ifaces_get (
  1240. unsigned int nodeid,
  1241. unsigned int *interface_id,
  1242. struct totem_ip_address *interfaces,
  1243. unsigned int interfaces_size,
  1244. char ***status,
  1245. unsigned int *iface_count)
  1246. {
  1247. int res;
  1248. res = totemsrp_ifaces_get (
  1249. totemsrp_context,
  1250. nodeid,
  1251. interface_id,
  1252. interfaces,
  1253. interfaces_size,
  1254. status,
  1255. iface_count);
  1256. return (res);
  1257. }
  1258. void totempg_event_signal (enum totem_event_type type, int value)
  1259. {
  1260. totemsrp_event_signal (totemsrp_context, type, value);
  1261. }
  1262. void* totempg_get_stats (void)
  1263. {
  1264. return &totempg_stats;
  1265. }
  1266. int totempg_crypto_set (
  1267. const char *cipher_type,
  1268. const char *hash_type)
  1269. {
  1270. int res;
  1271. res = totemsrp_crypto_set (totemsrp_context, cipher_type, hash_type);
  1272. return (res);
  1273. }
  1274. #define ONE_IFACE_LEN 63
  1275. const char *totempg_ifaces_print (unsigned int nodeid)
  1276. {
  1277. static char iface_string[256 * INTERFACE_MAX];
  1278. char one_iface[ONE_IFACE_LEN+1];
  1279. struct totem_ip_address interfaces[INTERFACE_MAX];
  1280. unsigned int iface_count;
  1281. unsigned int iface_ids[INTERFACE_MAX];
  1282. unsigned int i;
  1283. int res;
  1284. iface_string[0] = '\0';
  1285. res = totempg_ifaces_get (nodeid, iface_ids, interfaces, INTERFACE_MAX, NULL, &iface_count);
  1286. if (res == -1) {
  1287. return ("no interface found for nodeid");
  1288. }
  1289. res = totempg_ifaces_get (nodeid, iface_ids, interfaces, INTERFACE_MAX, NULL, &iface_count);
  1290. for (i = 0; i < iface_count; i++) {
  1291. if (!interfaces[i].family) {
  1292. continue;
  1293. }
  1294. snprintf (one_iface, ONE_IFACE_LEN,
  1295. "r(%d) ip(%s) ",
  1296. i, totemip_print (&interfaces[i]));
  1297. strcat (iface_string, one_iface);
  1298. }
  1299. return (iface_string);
  1300. }
  1301. unsigned int totempg_my_nodeid_get (void)
  1302. {
  1303. return (totemsrp_my_nodeid_get(totemsrp_context));
  1304. }
  1305. int totempg_my_family_get (void)
  1306. {
  1307. return (totemsrp_my_family_get(totemsrp_context));
  1308. }
  1309. extern void totempg_service_ready_register (
  1310. void (*totem_service_ready) (void))
  1311. {
  1312. totemsrp_service_ready_register (totemsrp_context, totem_service_ready);
  1313. }
  1314. void totempg_queue_level_register_callback (totem_queue_level_changed_fn fn)
  1315. {
  1316. totem_queue_level_changed = fn;
  1317. }
  1318. extern int totempg_member_add (
  1319. const struct totem_ip_address *member,
  1320. int ring_no)
  1321. {
  1322. return totemsrp_member_add (totemsrp_context, member, ring_no);
  1323. }
  1324. extern int totempg_member_remove (
  1325. const struct totem_ip_address *member,
  1326. int ring_no)
  1327. {
  1328. return totemsrp_member_remove (totemsrp_context, member, ring_no);
  1329. }
  1330. extern int totempg_reconfigure (void)
  1331. {
  1332. return totemsrp_reconfigure (totemsrp_context, totempg_totem_config);
  1333. }
  1334. extern void totempg_stats_clear (int flags)
  1335. {
  1336. if (flags & TOTEMPG_STATS_CLEAR_TOTEM) {
  1337. totempg_stats.msg_reserved = 0;
  1338. totempg_stats.msg_queue_avail = 0;
  1339. }
  1340. return totemsrp_stats_clear (totemsrp_context, flags);
  1341. }
  1342. void totempg_threaded_mode_enable (void)
  1343. {
  1344. totempg_threaded_mode = 1;
  1345. totemsrp_threaded_mode_enable (totemsrp_context);
  1346. }
  1347. void totempg_trans_ack (void)
  1348. {
  1349. totemsrp_trans_ack (totemsrp_context);
  1350. }
  1351. void totempg_force_gather (void)
  1352. {
  1353. totemsrp_force_gather(totemsrp_context);
  1354. }
  1355. /* Assumes ->orig_interfaces is already allocated */
  1356. void totempg_get_config(struct totem_config *config)
  1357. {
  1358. struct totem_interface *temp_if = config->orig_interfaces;
  1359. memcpy(config, totempg_totem_config, sizeof(struct totem_config));
  1360. config->orig_interfaces = temp_if;
  1361. memcpy(config->orig_interfaces, totempg_totem_config->interfaces, sizeof(struct totem_interface) * INTERFACE_MAX);
  1362. config->interfaces = NULL;
  1363. }
  1364. void totempg_put_config(struct totem_config *config)
  1365. {
  1366. /* Free this before we overwite the pointer */
  1367. free(totempg_totem_config->interfaces);
  1368. memcpy(totempg_totem_config, config, sizeof(struct totem_config));
  1369. }