totempg.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /*
  2. * Copyright (c) 2003-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2005 OSDL.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.com)
  8. * Mark Haverkamp (markh@osdl.org)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * FRAGMENTATION AND PACKING ALGORITHM:
  38. *
  39. * Assemble the entire message into one buffer
  40. * if full fragment
  41. * store fragment into lengths list
  42. * for each full fragment
  43. * multicast fragment
  44. * set length and fragment fields of pg mesage
  45. * store remaining multicast into head of fragmentation data and set lens field
  46. *
  47. * If a message exceeds the maximum packet size allowed by the totem
  48. * single ring protocol, the protocol could lose forward progress.
  49. * Statically calculating the allowed data amount doesn't work because
  50. * the amount of data allowed depends on the number of fragments in
  51. * each message. In this implementation, the maximum fragment size
  52. * is dynamically calculated for each fragment added to the message.
  53. * It is possible for a message to be two bytes short of the maximum
  54. * packet size. This occurs when a message or collection of
  55. * messages + the mcast header + the lens are two bytes short of the
  56. * end of the packet. Since another len field consumes two bytes, the
  57. * len field would consume the rest of the packet without room for data.
  58. *
  59. * One optimization would be to forgo the final len field and determine
  60. * it from the size of the udp datagram. Then this condition would no
  61. * longer occur.
  62. */
  63. /*
  64. * ASSEMBLY AND UNPACKING ALGORITHM:
  65. *
  66. * copy incoming packet into assembly data buffer indexed by current
  67. * location of end of fragment
  68. *
  69. * if not fragmented
  70. * deliver all messages in assembly data buffer
  71. * else
  72. * if msg_count > 1 and fragmented
  73. * deliver all messages except last message in assembly data buffer
  74. * copy last fragmented section to start of assembly data buffer
  75. * else
  76. * if msg_count = 1 and fragmented
  77. * do nothing
  78. *
  79. */
  80. #include <netinet/in.h>
  81. #include <sys/uio.h>
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <string.h>
  85. #include <assert.h>
  86. #include <pthread.h>
  87. #include "../include/hdb.h"
  88. #include "totempg.h"
  89. #include "totemmrp.h"
  90. #include "totemsrp.h"
  91. #include "swab.h"
  92. #define min(a,b) ((a) < (b)) ? a : b
  93. struct totempg_mcast_header {
  94. short version;
  95. short type;
  96. };
  97. /*
  98. * totempg_mcast structure
  99. *
  100. * header: Identify the mcast.
  101. * fragmented: Set if this message continues into next message
  102. * continuation: Set if this message is a continuation from last message
  103. * msg_count Indicates how many packed messages are contained
  104. * in the mcast.
  105. * Also, the size of each packed message and the messages themselves are
  106. * appended to the end of this structure when sent.
  107. */
  108. struct totempg_mcast {
  109. struct totempg_mcast_header header;
  110. unsigned char fragmented;
  111. unsigned char continuation;
  112. unsigned short msg_count;
  113. /*
  114. * short msg_len[msg_count];
  115. */
  116. /*
  117. * data for messages
  118. */
  119. };
  120. /*
  121. * Maximum packet size for totem pg messages
  122. */
  123. #define TOTEMPG_PACKET_SIZE (totempg_totem_config->net_mtu - \
  124. sizeof (struct totempg_mcast))
  125. /*
  126. * Local variables used for packing small messages
  127. */
  128. static unsigned short mcast_packed_msg_lens[FRAME_SIZE_MAX];
  129. static int mcast_packed_msg_count = 0;
  130. struct totem_config *totempg_totem_config;
  131. struct assembly {
  132. struct totem_ip_address addr;
  133. unsigned char data[MESSAGE_SIZE_MAX];
  134. int index;
  135. unsigned char last_frag_num;
  136. };
  137. struct assembly *assembly_list[PROCESSOR_COUNT_MAX];
  138. int assembly_list_entries = 0;
  139. /*
  140. * Staging buffer for packed messages. Messages are staged in this buffer
  141. * before sending. Multiple messages may fit which cuts down on the
  142. * number of mcasts sent. If a message doesn't completely fit, then
  143. * the mcast header has a fragment bit set that says that there are more
  144. * data to follow. fragment_size is an index into the buffer. It indicates
  145. * the size of message data and where to place new message data.
  146. * fragment_contuation indicates whether the first packed message in
  147. * the buffer is a continuation of a previously packed fragment.
  148. */
  149. static unsigned char *fragmentation_data;
  150. static int fragment_size = 0;
  151. static int fragment_continuation = 0;
  152. static struct iovec iov_delv;
  153. static unsigned int totempg_max_handle = 0;
  154. struct totempg_group_instance {
  155. void (*deliver_fn) (
  156. struct totem_ip_address *source_addr,
  157. struct iovec *iovec,
  158. int iov_len,
  159. int endian_conversion_required);
  160. void (*confchg_fn) (
  161. enum totem_configuration_type configuration_type,
  162. struct totem_ip_address *member_list, int member_list_entries,
  163. struct totem_ip_address *left_list, int left_list_entries,
  164. struct totem_ip_address *joined_list, int joined_list_entries,
  165. struct memb_ring_id *ring_id);
  166. struct totempg_group *groups;
  167. int groups_cnt;
  168. };
  169. static struct hdb_handle_database totempg_groups_instance_database = {
  170. .handle_count = 0,
  171. .handles = 0,
  172. .iterator = 0
  173. };
  174. static int send_ok (int msg_size);
  175. static unsigned char next_fragment = 1;
  176. static pthread_mutex_t totempg_mutex = PTHREAD_MUTEX_INITIALIZER;
  177. static pthread_mutex_t callback_token_mutex = PTHREAD_MUTEX_INITIALIZER;
  178. static struct assembly *find_assembly (struct totem_ip_address *addr)
  179. {
  180. int i;
  181. for (i = 0; i < assembly_list_entries; i++) {
  182. if (totemip_equal(addr, &assembly_list[i]->addr)) {
  183. return (assembly_list[i]);
  184. }
  185. }
  186. return (0);
  187. }
  188. static inline void app_confchg_fn (
  189. enum totem_configuration_type configuration_type,
  190. struct totem_ip_address *member_list, int member_list_entries,
  191. struct totem_ip_address *left_list, int left_list_entries,
  192. struct totem_ip_address *joined_list, int joined_list_entries,
  193. struct memb_ring_id *ring_id)
  194. {
  195. int i;
  196. struct totempg_group_instance *instance;
  197. unsigned int res;
  198. for (i = 0; i <= totempg_max_handle; i++) {
  199. res = hdb_handle_get (&totempg_groups_instance_database,
  200. i, (void *)&instance);
  201. if (res == 0) {
  202. if (instance->confchg_fn) {
  203. instance->confchg_fn (
  204. configuration_type,
  205. member_list,
  206. member_list_entries,
  207. left_list,
  208. left_list_entries,
  209. joined_list,
  210. joined_list_entries,
  211. ring_id);
  212. }
  213. hdb_handle_put (&totempg_groups_instance_database, i);
  214. }
  215. }
  216. }
  217. static inline int group_matches (
  218. struct iovec *iovec,
  219. unsigned int iov_len,
  220. struct totempg_group *groups_b,
  221. unsigned int group_b_cnt,
  222. unsigned int *adjust_iovec)
  223. {
  224. unsigned short *group_len;
  225. char *group_name;
  226. int i;
  227. int j;
  228. assert (iov_len == 1);
  229. group_len = (unsigned short *)iovec->iov_base;
  230. group_name = ((char *)iovec->iov_base) +
  231. sizeof (unsigned short) * (group_len[0] + 1);
  232. /*
  233. * Calculate amount to adjust the iovec by before delivering to app
  234. */
  235. *adjust_iovec = sizeof (unsigned short) * (group_len[0] + 1);
  236. for (i = 1; i < group_len[0] + 1; i++) {
  237. *adjust_iovec += group_len[i];
  238. }
  239. /*
  240. * Determine if this message should be delivered to this instance
  241. */
  242. for (i = 1; i < group_len[0] + 1; i++) {
  243. for (j = 0; j < group_b_cnt; j++) {
  244. if ((group_len[i] == groups_b[j].group_len) &&
  245. (memcmp (groups_b[j].group, group_name, group_len[i]) == 0)) {
  246. return (1);
  247. }
  248. }
  249. group_name += group_len[i];
  250. }
  251. return (0);
  252. }
  253. static inline void app_deliver_fn (
  254. struct totem_ip_address *source_addr,
  255. struct iovec *iovec,
  256. unsigned int iov_len,
  257. int endian_conversion_required)
  258. {
  259. int i;
  260. struct totempg_group_instance *instance;
  261. struct iovec stripped_iovec;
  262. unsigned int adjust_iovec;
  263. unsigned int res;
  264. for (i = 0; i <= totempg_max_handle; i++) {
  265. res = hdb_handle_get (&totempg_groups_instance_database,
  266. i, (void *)&instance);
  267. if (res == 0) {
  268. assert (iov_len == 1);
  269. if (group_matches (iovec, iov_len, instance->groups, instance->groups_cnt, &adjust_iovec)) {
  270. stripped_iovec.iov_len = iovec->iov_len - adjust_iovec;
  271. stripped_iovec.iov_base = (char *)iovec->iov_base + adjust_iovec;
  272. instance->deliver_fn (
  273. source_addr,
  274. &stripped_iovec,
  275. iov_len,
  276. endian_conversion_required);
  277. }
  278. hdb_handle_put (&totempg_groups_instance_database, i);
  279. }
  280. }
  281. }
  282. static void totempg_confchg_fn (
  283. enum totem_configuration_type configuration_type,
  284. struct totem_ip_address *member_list, int member_list_entries,
  285. struct totem_ip_address *left_list, int left_list_entries,
  286. struct totem_ip_address *joined_list, int joined_list_entries,
  287. struct memb_ring_id *ring_id)
  288. {
  289. int i;
  290. int j;
  291. int found;
  292. /*
  293. * Clean out the assembly area for nodes that have left the
  294. * membership. If they return, we don't want any stale message
  295. * data that may be there.
  296. */
  297. for (i = 0; i < left_list_entries; i++) {
  298. for (j = 0; j < assembly_list_entries; j++) {
  299. if (totemip_equal(&left_list[i], &assembly_list[j]->addr)) {
  300. assembly_list[j]->index = 0;
  301. }
  302. }
  303. }
  304. /*
  305. * Create a message assembly area for any new members.
  306. */
  307. for (i = 0; i < member_list_entries; i++) {
  308. found = 0;
  309. for (j = 0; j < assembly_list_entries; j++) {
  310. if (totemip_equal(&member_list[i], &assembly_list[j]->addr)) {
  311. found = 1;
  312. break;
  313. }
  314. }
  315. if (found == 0) {
  316. assembly_list[assembly_list_entries] =
  317. malloc (sizeof (struct assembly));
  318. assert (assembly_list[assembly_list_entries]); // TODO
  319. totemip_copy(&assembly_list[assembly_list_entries]->addr,
  320. &member_list[i]);
  321. assembly_list[assembly_list_entries]->index = 0;
  322. assembly_list_entries += 1;
  323. }
  324. }
  325. app_confchg_fn (configuration_type,
  326. member_list, member_list_entries,
  327. left_list, left_list_entries,
  328. joined_list, joined_list_entries,
  329. ring_id);
  330. }
  331. static void totempg_deliver_fn (
  332. struct totem_ip_address *source_addr,
  333. struct iovec *iovec,
  334. int iov_len,
  335. int endian_conversion_required)
  336. {
  337. struct totempg_mcast *mcast;
  338. unsigned short *msg_lens;
  339. int i;
  340. struct assembly *assembly;
  341. char header[FRAME_SIZE_MAX];
  342. int h_index;
  343. int a_i = 0;
  344. int msg_count;
  345. int continuation;
  346. int start;
  347. assembly = find_assembly (source_addr);
  348. assert (assembly);
  349. /*
  350. * Assemble the header into one block of data and
  351. * assemble the packet contents into one block of data to simplify delivery
  352. */
  353. if (iov_len == 1) {
  354. /*
  355. * This message originated from external processor
  356. * because there is only one iovec for the full msg.
  357. */
  358. char *data;
  359. int datasize;
  360. mcast = (struct totempg_mcast *)iovec[0].iov_base;
  361. msg_count = mcast->msg_count;
  362. if (endian_conversion_required) {
  363. msg_count = swab16 (mcast->msg_count);
  364. }
  365. datasize = sizeof (struct totempg_mcast) +
  366. msg_count * sizeof (unsigned short);
  367. memcpy (header, iovec[0].iov_base, datasize);
  368. assert(iovec);
  369. data = iovec[0].iov_base;
  370. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  371. memcpy (&assembly->data[assembly->index], &data[datasize],
  372. iovec[0].iov_len - datasize);
  373. } else {
  374. /*
  375. * The message originated from local processor
  376. * becasue there is greater than one iovec for then full msg.
  377. */
  378. h_index = 0;
  379. for (i = 0; i < 2; i++) {
  380. memcpy (&header[h_index], iovec[i].iov_base, iovec[i].iov_len);
  381. h_index += iovec[i].iov_len;
  382. }
  383. mcast = (struct totempg_mcast *)header;
  384. // TODO make sure we are using a copy of mcast not the actual data itself
  385. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  386. for (i = 2; i < iov_len; i++) {
  387. a_i = assembly->index;
  388. assert (iovec[i].iov_len + a_i <= MESSAGE_SIZE_MAX);
  389. memcpy (&assembly->data[a_i], iovec[i].iov_base, iovec[i].iov_len);
  390. a_i += msg_lens[i - 2];
  391. }
  392. iov_len -= 2;
  393. }
  394. if (endian_conversion_required) {
  395. mcast->msg_count = swab16 (mcast->msg_count);
  396. for (i = 0; i < mcast->msg_count; i++) {
  397. msg_lens[i] = swab16 (msg_lens[i]);
  398. }
  399. }
  400. /*
  401. printf ("Message fragmented %d count %d\n", mcast->fragmented, mcast->msg_count);
  402. for (i = 0; i < mcast->msg_count; i++) {
  403. printf ("len[%d] = %d\n", i, msg_lens[i]);
  404. }
  405. */
  406. /*
  407. * If the last message in the buffer is a fragment, then we
  408. * can't deliver it. We'll first deliver the full messages
  409. * then adjust the assembly buffer so we can add the rest of the
  410. * fragment when it arrives.
  411. */
  412. msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
  413. continuation = mcast->continuation;
  414. iov_delv.iov_base = &assembly->data[0];
  415. iov_delv.iov_len = assembly->index + msg_lens[0];
  416. // printf ("%d %d %d\n", msg_count, continuation, assembly->last_frag_num);
  417. /*
  418. * Make sure that if this message is a continuation, that it
  419. * matches the sequence number of the previous fragment.
  420. * Also, if the first packed message is a continuation
  421. * of a previous message, but the assembly buffer
  422. * is empty, then we need to discard it since we can't
  423. * assemble a complete message. Likewise, if this message isn't a
  424. * continuation and the assembly buffer is empty, we have to discard
  425. * the continued message.
  426. */
  427. start = 0;
  428. if (continuation) {
  429. if (continuation != assembly->last_frag_num) {
  430. printf("Message continuation doesn't match previous frag e: %u - a: %u\n",
  431. assembly->last_frag_num, continuation);
  432. continuation = 0;
  433. }
  434. if ((assembly->index == 0) ||
  435. (!continuation && assembly->index)) {
  436. printf("Throwing away broken message: continuation %u, index %u\n",
  437. continuation, assembly->index);
  438. continuation = 0;
  439. }
  440. /*
  441. * we decided to throw away the first continued message
  442. * in this buffer, if continuation was set to zero.
  443. */
  444. if (!continuation) {
  445. assembly->index += msg_lens[0];
  446. iov_delv.iov_base = &assembly->data[assembly->index];
  447. iov_delv.iov_len = msg_lens[1];
  448. start = 1;
  449. }
  450. }
  451. for (i = start; i < msg_count; i++) {
  452. app_deliver_fn(source_addr, &iov_delv, 1,
  453. endian_conversion_required);
  454. assembly->index += msg_lens[i];
  455. iov_delv.iov_base = &assembly->data[assembly->index];
  456. if (i < (msg_count - 1)) {
  457. iov_delv.iov_len = msg_lens[i + 1];
  458. }
  459. }
  460. if (mcast->fragmented) {
  461. assembly->last_frag_num = mcast->fragmented;
  462. if (mcast->msg_count > 1) {
  463. memmove (&assembly->data[0],
  464. &assembly->data[assembly->index],
  465. msg_lens[msg_count]);
  466. assembly->index = 0;
  467. }
  468. assembly->index += msg_lens[msg_count];
  469. } else {
  470. assembly->last_frag_num = 0;
  471. assembly->index = 0;
  472. }
  473. }
  474. /*
  475. * Totem Process Group Abstraction
  476. * depends on poll abstraction, POSIX, IPV4
  477. */
  478. void *callback_token_received_handle;
  479. int callback_token_received_fn (enum totem_callback_token_type type,
  480. void *data)
  481. {
  482. struct totempg_mcast mcast;
  483. struct iovec iovecs[3];
  484. int res;
  485. if (mcast_packed_msg_count == 0) {
  486. return (0);
  487. }
  488. if (totemmrp_avail() == 0) {
  489. return (0);
  490. }
  491. mcast.fragmented = 0;
  492. /*
  493. * Was the first message in this buffer a continuation of a
  494. * fragmented message?
  495. */
  496. mcast.continuation = fragment_continuation;
  497. fragment_continuation = 0;
  498. mcast.msg_count = mcast_packed_msg_count;
  499. iovecs[0].iov_base = &mcast;
  500. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  501. iovecs[1].iov_base = mcast_packed_msg_lens;
  502. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  503. iovecs[2].iov_base = &fragmentation_data[0];
  504. iovecs[2].iov_len = fragment_size;
  505. res = totemmrp_mcast (iovecs, 3, 0);
  506. mcast_packed_msg_count = 0;
  507. fragment_size = 0;
  508. return (0);
  509. }
  510. /*
  511. * Initialize the totem process group abstraction
  512. */
  513. int totempg_initialize (
  514. poll_handle poll_handle,
  515. struct totem_config *totem_config)
  516. {
  517. int res;
  518. totempg_totem_config = totem_config;
  519. fragmentation_data = malloc (TOTEMPG_PACKET_SIZE);
  520. if (fragmentation_data == 0) {
  521. return (-1);
  522. }
  523. res = totemmrp_initialize (
  524. poll_handle,
  525. totem_config,
  526. totempg_deliver_fn,
  527. totempg_confchg_fn);
  528. totemmrp_callback_token_create (
  529. &callback_token_received_handle,
  530. TOTEM_CALLBACK_TOKEN_RECEIVED,
  531. 0,
  532. callback_token_received_fn,
  533. 0);
  534. totemsrp_net_mtu_adjust (totem_config);
  535. return (res);
  536. }
  537. void totempg_finalize (void)
  538. {
  539. pthread_mutex_lock (&totempg_mutex);
  540. totemmrp_finalize ();
  541. pthread_mutex_unlock (&totempg_mutex);
  542. }
  543. /*
  544. * Multicast a message
  545. */
  546. static int mcast_msg (
  547. struct iovec *iovec,
  548. int iov_len,
  549. int guarantee)
  550. {
  551. int res = 0;
  552. struct totempg_mcast mcast;
  553. struct iovec iovecs[3];
  554. int i;
  555. int max_packet_size = 0;
  556. int copy_len = 0;
  557. int copy_base = 0;
  558. int total_size = 0;
  559. totemmrp_new_msg_signal ();
  560. max_packet_size = TOTEMPG_PACKET_SIZE -
  561. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  562. mcast_packed_msg_lens[mcast_packed_msg_count] = 0;
  563. /*
  564. * Check if we would overwrite new message queue
  565. */
  566. for (i = 0; i < iov_len; i++) {
  567. total_size += iovec[i].iov_len;
  568. }
  569. if (send_ok (total_size + sizeof(unsigned short) *
  570. (mcast_packed_msg_count+1)) == 0) {
  571. return(-1);
  572. }
  573. for (i = 0; i < iov_len; ) {
  574. mcast.fragmented = 0;
  575. mcast.continuation = fragment_continuation;
  576. copy_len = iovec[i].iov_len - copy_base;
  577. /*
  578. * If it all fits with room left over, copy it in.
  579. * We need to leave at least sizeof(short) + 1 bytes in the
  580. * fragment_buffer on exit so that max_packet_size + fragment_size
  581. * doesn't exceed the size of the fragment_buffer on the next call.
  582. */
  583. if ((copy_len + fragment_size) <
  584. (max_packet_size - sizeof (unsigned short))) {
  585. memcpy (&fragmentation_data[fragment_size],
  586. iovec[i].iov_base + copy_base, copy_len);
  587. fragment_size += copy_len;
  588. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  589. copy_len = 0;
  590. copy_base = 0;
  591. i++;
  592. continue;
  593. /*
  594. * If it just fits or is too big, then send out what fits.
  595. */
  596. } else {
  597. unsigned char *data_ptr;
  598. copy_len = min(copy_len, max_packet_size - fragment_size);
  599. if( copy_len == max_packet_size )
  600. data_ptr = iovec[i].iov_base + copy_base;
  601. else {
  602. data_ptr = fragmentation_data;
  603. memcpy (&fragmentation_data[fragment_size],
  604. iovec[i].iov_base + copy_base, copy_len);
  605. }
  606. memcpy (&fragmentation_data[fragment_size],
  607. iovec[i].iov_base + copy_base, copy_len);
  608. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  609. /*
  610. * if we're not on the last iovec or the iovec is too large to
  611. * fit, then indicate a fragment. This also means that the next
  612. * message will have the continuation of this one.
  613. */
  614. if ((i < (iov_len - 1)) ||
  615. ((copy_base + copy_len) < iovec[i].iov_len)) {
  616. if (!next_fragment) {
  617. next_fragment++;
  618. }
  619. fragment_continuation = next_fragment;
  620. mcast.fragmented = next_fragment++;
  621. assert(fragment_continuation != 0);
  622. assert(mcast.fragmented != 0);
  623. } else {
  624. fragment_continuation = 0;
  625. }
  626. /*
  627. * assemble the message and send it
  628. */
  629. mcast.msg_count = ++mcast_packed_msg_count;
  630. iovecs[0].iov_base = &mcast;
  631. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  632. iovecs[1].iov_base = mcast_packed_msg_lens;
  633. iovecs[1].iov_len = mcast_packed_msg_count *
  634. sizeof(unsigned short);
  635. iovecs[2].iov_base = data_ptr;
  636. iovecs[2].iov_len = max_packet_size;
  637. assert (totemmrp_avail() > 0);
  638. res = totemmrp_mcast (iovecs, 3, guarantee);
  639. /*
  640. * Recalculate counts and indexes for the next.
  641. */
  642. mcast_packed_msg_lens[0] = 0;
  643. mcast_packed_msg_count = 0;
  644. fragment_size = 0;
  645. max_packet_size = TOTEMPG_PACKET_SIZE - (sizeof(unsigned short));
  646. /*
  647. * If the iovec all fit, go to the next iovec
  648. */
  649. if ((copy_base + copy_len) == iovec[i].iov_len) {
  650. copy_len = 0;
  651. copy_base = 0;
  652. i++;
  653. /*
  654. * Continue with the rest of the current iovec.
  655. */
  656. } else {
  657. copy_base += copy_len;
  658. }
  659. }
  660. }
  661. /*
  662. * Bump only if we added message data. This may be zero if
  663. * the last buffer just fit into the fragmentation_data buffer
  664. * and we were at the last iovec.
  665. */
  666. if (mcast_packed_msg_lens[mcast_packed_msg_count]) {
  667. mcast_packed_msg_count++;
  668. }
  669. return (res);
  670. }
  671. /*
  672. * Determine if a message of msg_size could be queued
  673. */
  674. static int send_ok (
  675. int msg_size)
  676. {
  677. int avail = 0;
  678. int total;
  679. avail = totemmrp_avail ();
  680. /*
  681. * msg size less then totempg_totem_config->net_mtu - 25 will take up
  682. * a full message, so add +1
  683. * totempg_totem_config->net_mtu - 25 is for the totempg_mcast header
  684. */
  685. total = (msg_size / (totempg_totem_config->net_mtu - 25)) + 1;
  686. return (avail >= total);
  687. }
  688. int totempg_callback_token_create (
  689. void **handle_out,
  690. enum totem_callback_token_type type,
  691. int delete,
  692. int (*callback_fn) (enum totem_callback_token_type type, void *),
  693. void *data)
  694. {
  695. unsigned int res;
  696. pthread_mutex_lock (&callback_token_mutex);
  697. res = totemmrp_callback_token_create (handle_out, type, delete,
  698. callback_fn, data);
  699. pthread_mutex_unlock (&callback_token_mutex);
  700. return (res);
  701. }
  702. void totempg_callback_token_destroy (
  703. void *handle_out)
  704. {
  705. pthread_mutex_lock (&callback_token_mutex);
  706. totemmrp_callback_token_destroy (handle_out);
  707. pthread_mutex_unlock (&callback_token_mutex);
  708. }
  709. /*
  710. * vi: set autoindent tabstop=4 shiftwidth=4 :
  711. */
  712. int totempg_groups_initialize (
  713. totempg_groups_handle *handle,
  714. void (*deliver_fn) (
  715. struct totem_ip_address *source_addr,
  716. struct iovec *iovec,
  717. int iov_len,
  718. int endian_conversion_required),
  719. void (*confchg_fn) (
  720. enum totem_configuration_type configuration_type,
  721. struct totem_ip_address *member_list, int member_list_entries,
  722. struct totem_ip_address *left_list, int left_list_entries,
  723. struct totem_ip_address *joined_list, int joined_list_entries,
  724. struct memb_ring_id *ring_id))
  725. {
  726. struct totempg_group_instance *instance;
  727. unsigned int res;
  728. pthread_mutex_lock (&totempg_mutex);
  729. res = hdb_handle_create (&totempg_groups_instance_database,
  730. sizeof (struct totempg_group_instance), handle);
  731. if (res != 0) {
  732. goto error_exit;
  733. }
  734. if (*handle > totempg_max_handle) {
  735. totempg_max_handle = *handle;
  736. }
  737. res = hdb_handle_get (&totempg_groups_instance_database, *handle,
  738. (void *)&instance);
  739. if (res != 0) {
  740. goto error_destroy;
  741. }
  742. instance->deliver_fn = deliver_fn;
  743. instance->confchg_fn = confchg_fn;
  744. instance->groups = 0;
  745. instance->groups_cnt = 0;
  746. hdb_handle_put (&totempg_groups_instance_database, *handle);
  747. pthread_mutex_unlock (&totempg_mutex);
  748. return (0);
  749. error_destroy:
  750. hdb_handle_destroy (&totempg_groups_instance_database, *handle);
  751. error_exit:
  752. pthread_mutex_unlock (&totempg_mutex);
  753. return (-1);
  754. }
  755. int totempg_groups_join (
  756. totempg_groups_handle handle,
  757. struct totempg_group *groups,
  758. int group_cnt)
  759. {
  760. struct totempg_group_instance *instance;
  761. struct totempg_group *new_groups;
  762. unsigned int res;
  763. pthread_mutex_lock (&totempg_mutex);
  764. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  765. (void *)&instance);
  766. if (res != 0) {
  767. goto error_exit;
  768. }
  769. new_groups = realloc (instance->groups,
  770. sizeof (struct totempg_group) *
  771. (instance->groups_cnt + group_cnt));
  772. if (new_groups == 0) {
  773. res = ENOMEM;
  774. goto error_exit;
  775. }
  776. memcpy (&new_groups[instance->groups_cnt],
  777. groups, group_cnt * sizeof (struct totempg_group));
  778. instance->groups = new_groups;
  779. instance->groups_cnt = instance->groups_cnt = group_cnt;
  780. hdb_handle_put (&totempg_groups_instance_database, handle);
  781. error_exit:
  782. pthread_mutex_unlock (&totempg_mutex);
  783. return (res);
  784. }
  785. int totempg_groups_leave (
  786. totempg_groups_handle handle,
  787. struct totempg_group *groups,
  788. int group_cnt)
  789. {
  790. struct totempg_group_instance *instance;
  791. unsigned int res;
  792. pthread_mutex_lock (&totempg_mutex);
  793. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  794. (void *)&instance);
  795. if (res != 0) {
  796. goto error_exit;
  797. }
  798. hdb_handle_put (&totempg_groups_instance_database, handle);
  799. error_exit:
  800. pthread_mutex_unlock (&totempg_mutex);
  801. return (res);
  802. }
  803. #define MAX_IOVECS_FROM_APP 32
  804. #define MAX_GROUPS_PER_MSG 32
  805. int totempg_groups_mcast_joined (
  806. totempg_groups_handle handle,
  807. struct iovec *iovec,
  808. int iov_len,
  809. int guarantee)
  810. {
  811. struct totempg_group_instance *instance;
  812. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  813. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  814. int i;
  815. unsigned int res;
  816. pthread_mutex_lock (&totempg_mutex);
  817. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  818. (void *)&instance);
  819. if (res != 0) {
  820. goto error_exit;
  821. }
  822. /*
  823. * Build group_len structure and the iovec_mcast structure
  824. */
  825. group_len[0] = instance->groups_cnt;
  826. for (i = 0; i < instance->groups_cnt; i++) {
  827. group_len[i + 1] = instance->groups[i].group_len;
  828. iovec_mcast[i + 1].iov_len = instance->groups[i].group_len;
  829. iovec_mcast[i + 1].iov_base = instance->groups[i].group;
  830. }
  831. iovec_mcast[0].iov_len = (instance->groups_cnt + 1) * sizeof (unsigned short);
  832. iovec_mcast[0].iov_base = group_len;
  833. for (i = 0; i < iov_len; i++) {
  834. iovec_mcast[i + instance->groups_cnt + 1].iov_len = iovec[i].iov_len;
  835. iovec_mcast[i + instance->groups_cnt + 1].iov_base = iovec[i].iov_base;
  836. }
  837. res = mcast_msg (iovec_mcast, iov_len + instance->groups_cnt + 1, guarantee);
  838. hdb_handle_put (&totempg_groups_instance_database, handle);
  839. error_exit:
  840. pthread_mutex_unlock (&totempg_mutex);
  841. return (res);
  842. }
  843. int totempg_groups_send_ok_joined (
  844. totempg_groups_handle handle,
  845. struct iovec *iovec,
  846. int iov_len)
  847. {
  848. struct totempg_group_instance *instance;
  849. unsigned int size = 0;
  850. unsigned int i;
  851. unsigned int res;
  852. pthread_mutex_lock (&totempg_mutex);
  853. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  854. (void *)&instance);
  855. if (res != 0) {
  856. goto error_exit;
  857. }
  858. for (i = 0; i < instance->groups_cnt; i++) {
  859. size += instance->groups[i].group_len;
  860. }
  861. for (i = 0; i < iov_len; i++) {
  862. size += iovec[i].iov_len;
  863. }
  864. res = send_ok (size);
  865. hdb_handle_put (&totempg_groups_instance_database, handle);
  866. error_exit:
  867. pthread_mutex_unlock (&totempg_mutex);
  868. return (res);
  869. }
  870. int totempg_groups_mcast_groups (
  871. totempg_groups_handle handle,
  872. int guarantee,
  873. struct totempg_group *groups,
  874. int groups_cnt,
  875. struct iovec *iovec,
  876. int iov_len)
  877. {
  878. struct totempg_group_instance *instance;
  879. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  880. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  881. int i;
  882. unsigned int res;
  883. pthread_mutex_lock (&totempg_mutex);
  884. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  885. (void *)&instance);
  886. if (res != 0) {
  887. goto error_exit;
  888. }
  889. /*
  890. * Build group_len structure and the iovec_mcast structure
  891. */
  892. group_len[0] = groups_cnt;
  893. for (i = 0; i < groups_cnt; i++) {
  894. group_len[i + 1] = groups[i].group_len;
  895. iovec_mcast[i + 1].iov_len = groups[i].group_len;
  896. iovec_mcast[i + 1].iov_base = groups[i].group;
  897. }
  898. iovec_mcast[0].iov_len = (groups_cnt + 1) * sizeof (unsigned short);
  899. iovec_mcast[0].iov_base = group_len;
  900. for (i = 0; i < iov_len; i++) {
  901. iovec_mcast[i + groups_cnt + 1].iov_len = iovec[i].iov_len;
  902. iovec_mcast[i + groups_cnt + 1].iov_base = iovec[i].iov_base;
  903. }
  904. res = mcast_msg (iovec_mcast, iov_len + groups_cnt + 1, guarantee);
  905. hdb_handle_put (&totempg_groups_instance_database, handle);
  906. error_exit:
  907. pthread_mutex_unlock (&totempg_mutex);
  908. return (res);
  909. }
  910. /*
  911. * Returns -1 if error, 0 if can't send, 1 if can send the message
  912. */
  913. int totempg_groups_send_ok_groups (
  914. totempg_groups_handle handle,
  915. struct totempg_group *groups,
  916. int groups_cnt,
  917. struct iovec *iovec,
  918. int iov_len)
  919. {
  920. struct totempg_group_instance *instance;
  921. unsigned int size = 0;
  922. unsigned int i;
  923. unsigned int res;
  924. pthread_mutex_lock (&totempg_mutex);
  925. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  926. (void *)&instance);
  927. if (res != 0) {
  928. goto error_exit;
  929. }
  930. for (i = 0; i < groups_cnt; i++) {
  931. size += groups[i].group_len;
  932. }
  933. for (i = 0; i < iov_len; i++) {
  934. size += iovec[i].iov_len;
  935. }
  936. res = send_ok (size);
  937. hdb_handle_put (&totempg_groups_instance_database, handle);
  938. error_exit:
  939. pthread_mutex_unlock (&totempg_mutex);
  940. return (res);
  941. }