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