totempg.c 38 KB

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