totempg.c 37 KB

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