totempg.c 39 KB

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