totempg.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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 "totempg.h"
  87. #include "totemmrp.h"
  88. #include "totemsrp.h"
  89. #include "hdb.h"
  90. #include "swab.h"
  91. #define min(a,b) ((a) < (b)) ? a : b
  92. struct totempg_mcast_header {
  93. short version;
  94. short type;
  95. };
  96. /*
  97. * totempg_mcast structure
  98. *
  99. * header: Identify the mcast.
  100. * fragmented: Set if this message continues into next message
  101. * continuation: Set if this message is a continuation from last message
  102. * msg_count Indicates how many packed messages are contained
  103. * in the mcast.
  104. * Also, the size of each packed message and the messages themselves are
  105. * appended to the end of this structure when sent.
  106. */
  107. struct totempg_mcast {
  108. struct totempg_mcast_header header;
  109. unsigned char fragmented;
  110. unsigned char continuation;
  111. unsigned short msg_count;
  112. /*
  113. * short msg_len[msg_count];
  114. */
  115. /*
  116. * data for messages
  117. */
  118. };
  119. /*
  120. * Maximum packet size for totem pg messages
  121. */
  122. #define TOTEMPG_PACKET_SIZE (totempg_totem_config->net_mtu - \
  123. sizeof (struct totempg_mcast))
  124. /*
  125. * Local variables used for packing small messages
  126. */
  127. static unsigned short mcast_packed_msg_lens[FRAME_SIZE_MAX];
  128. static int mcast_packed_msg_count = 0;
  129. struct totem_config *totempg_totem_config;
  130. struct assembly {
  131. struct totem_ip_address addr;
  132. unsigned char data[MESSAGE_SIZE_MAX];
  133. int index;
  134. unsigned char last_frag_num;
  135. };
  136. struct assembly *assembly_list[PROCESSOR_COUNT_MAX];
  137. int assembly_list_entries = 0;
  138. /*
  139. * Staging buffer for packed messages. Messages are staged in this buffer
  140. * before sending. Multiple messages may fit which cuts down on the
  141. * number of mcasts sent. If a message doesn't completely fit, then
  142. * the mcast header has a fragment bit set that says that there are more
  143. * data to follow. fragment_size is an index into the buffer. It indicates
  144. * the size of message data and where to place new message data.
  145. * fragment_contuation indicates whether the first packed message in
  146. * the buffer is a continuation of a previously packed fragment.
  147. */
  148. static unsigned char *fragmentation_data;
  149. static int fragment_size = 0;
  150. static int fragment_continuation = 0;
  151. static struct iovec iov_delv;
  152. static unsigned int totempg_max_handle = 0;
  153. struct totempg_group_instance {
  154. void (*deliver_fn) (
  155. struct totem_ip_address *source_addr,
  156. struct iovec *iovec,
  157. int iov_len,
  158. int endian_conversion_required);
  159. void (*confchg_fn) (
  160. enum totem_configuration_type configuration_type,
  161. struct totem_ip_address *member_list, int member_list_entries,
  162. struct totem_ip_address *left_list, int left_list_entries,
  163. struct totem_ip_address *joined_list, int joined_list_entries,
  164. struct memb_ring_id *ring_id);
  165. struct totempg_group *groups;
  166. int groups_cnt;
  167. };
  168. static struct saHandleDatabase totempg_groups_instance_database = {
  169. .handleCount = 0,
  170. .handles = 0,
  171. .handleInstanceDestructor = 0
  172. };
  173. static int send_ok (int msg_size);
  174. static struct assembly *find_assembly (struct totem_ip_address *addr)
  175. {
  176. int i;
  177. for (i = 0; i < assembly_list_entries; i++) {
  178. if (totemip_equal(addr, &assembly_list[i]->addr)) {
  179. return (assembly_list[i]);
  180. }
  181. }
  182. return (0);
  183. }
  184. static inline void app_confchg_fn (
  185. enum totem_configuration_type configuration_type,
  186. struct totem_ip_address *member_list, int member_list_entries,
  187. struct totem_ip_address *left_list, int left_list_entries,
  188. struct totem_ip_address *joined_list, int joined_list_entries,
  189. struct memb_ring_id *ring_id)
  190. {
  191. int i;
  192. SaAisErrorT error;
  193. struct totempg_group_instance *instance;
  194. for (i = 0; i <= totempg_max_handle; i++) {
  195. error = saHandleInstanceGet (&totempg_groups_instance_database,
  196. i, (void *)&instance);
  197. if (error == SA_OK) {
  198. instance->confchg_fn (
  199. configuration_type,
  200. member_list,
  201. member_list_entries,
  202. left_list,
  203. left_list_entries,
  204. joined_list,
  205. joined_list_entries,
  206. ring_id);
  207. saHandleInstancePut (&totempg_groups_instance_database, i);
  208. }
  209. }
  210. }
  211. static inline int group_matches (
  212. struct iovec *iovec,
  213. unsigned int iov_len,
  214. struct totempg_group *groups_b,
  215. unsigned int group_b_cnt,
  216. unsigned int *adjust_iovec)
  217. {
  218. unsigned short *group_len;
  219. char *group_name;
  220. int i;
  221. int j;
  222. assert (iov_len == 1);
  223. group_len = (unsigned short *)iovec->iov_base;
  224. group_name = ((char *)iovec->iov_base) +
  225. sizeof (unsigned short) * (group_len[0] + 1);
  226. /*
  227. * Calculate amount to adjust the iovec by before delivering to app
  228. */
  229. *adjust_iovec = sizeof (unsigned short) * (group_len[0] + 1);
  230. for (i = 1; i < group_len[0] + 1; i++) {
  231. *adjust_iovec += group_len[i];
  232. }
  233. /*
  234. * Determine if this message should be delivered to this instance
  235. */
  236. for (i = 1; i < group_len[0] + 1; i++) {
  237. for (j = 0; j < group_b_cnt; j++) {
  238. if ((group_len[i] == groups_b[j].group_len) &&
  239. (memcmp (groups_b[j].group, group_name, group_len[i]) == 0)) {
  240. return (1);
  241. }
  242. }
  243. group_name += group_len[i];
  244. }
  245. return (0);
  246. }
  247. static inline void app_deliver_fn (
  248. struct totem_ip_address *source_addr,
  249. struct iovec *iovec,
  250. unsigned int iov_len,
  251. int endian_conversion_required)
  252. {
  253. int i;
  254. SaAisErrorT error;
  255. struct totempg_group_instance *instance;
  256. struct iovec stripped_iovec;
  257. unsigned int adjust_iovec;
  258. for (i = 0; i <= totempg_max_handle; i++) {
  259. error = saHandleInstanceGet (&totempg_groups_instance_database,
  260. i, (void *)&instance);
  261. if (error == SA_OK) {
  262. assert (iov_len == 1);
  263. if (group_matches (iovec, iov_len, instance->groups, instance->groups_cnt, &adjust_iovec)) {
  264. stripped_iovec.iov_len = iovec->iov_len - adjust_iovec;
  265. stripped_iovec.iov_base = (char *)iovec->iov_base + adjust_iovec;
  266. instance->deliver_fn (
  267. source_addr,
  268. &stripped_iovec,
  269. iov_len,
  270. endian_conversion_required);
  271. }
  272. saHandleInstancePut (&totempg_groups_instance_database, i);
  273. }
  274. }
  275. }
  276. static void totempg_confchg_fn (
  277. enum totem_configuration_type configuration_type,
  278. struct totem_ip_address *member_list, int member_list_entries,
  279. struct totem_ip_address *left_list, int left_list_entries,
  280. struct totem_ip_address *joined_list, int joined_list_entries,
  281. struct memb_ring_id *ring_id)
  282. {
  283. int i;
  284. int j;
  285. int found;
  286. /*
  287. * Clean out the assembly area for nodes that have left the
  288. * membership. If they return, we don't want any stale message
  289. * data that may be there.
  290. */
  291. for (i = 0; i < left_list_entries; i++) {
  292. for (j = 0; j < assembly_list_entries; j++) {
  293. if (totemip_equal(&left_list[i], &assembly_list[j]->addr)) {
  294. assembly_list[j]->index = 0;
  295. }
  296. }
  297. }
  298. /*
  299. * Create a message assembly area for any new members.
  300. */
  301. for (i = 0; i < member_list_entries; i++) {
  302. found = 0;
  303. for (j = 0; j < assembly_list_entries; j++) {
  304. if (totemip_equal(&member_list[i], &assembly_list[j]->addr)) {
  305. found = 1;
  306. break;
  307. }
  308. }
  309. if (found == 0) {
  310. assembly_list[assembly_list_entries] =
  311. malloc (sizeof (struct assembly));
  312. assert (assembly_list[assembly_list_entries]); // TODO
  313. totemip_copy(&assembly_list[assembly_list_entries]->addr,
  314. &member_list[i]);
  315. assembly_list[assembly_list_entries]->index = 0;
  316. assembly_list_entries += 1;
  317. }
  318. }
  319. app_confchg_fn (configuration_type,
  320. member_list, member_list_entries,
  321. left_list, left_list_entries,
  322. joined_list, joined_list_entries,
  323. ring_id);
  324. }
  325. static void totempg_deliver_fn (
  326. struct totem_ip_address *source_addr,
  327. struct iovec *iovec,
  328. int iov_len,
  329. int endian_conversion_required)
  330. {
  331. struct totempg_mcast *mcast;
  332. unsigned short *msg_lens;
  333. int i;
  334. struct assembly *assembly;
  335. char header[FRAME_SIZE_MAX];
  336. int h_index;
  337. int a_i = 0;
  338. int msg_count;
  339. int continuation;
  340. int start;
  341. assembly = find_assembly (source_addr);
  342. assert (assembly);
  343. /*
  344. * Assemble the header into one block of data and
  345. * assemble the packet contents into one block of data to simplify delivery
  346. */
  347. if (iov_len == 1) {
  348. /*
  349. * This message originated from external processor
  350. * because there is only one iovec for the full msg.
  351. */
  352. char *data;
  353. int datasize;
  354. mcast = (struct totempg_mcast *)iovec[0].iov_base;
  355. msg_count = mcast->msg_count;
  356. if (endian_conversion_required) {
  357. msg_count = swab16 (mcast->msg_count);
  358. }
  359. datasize = sizeof (struct totempg_mcast) +
  360. msg_count * sizeof (unsigned short);
  361. memcpy (header, iovec[0].iov_base, datasize);
  362. assert(iovec);
  363. data = iovec[0].iov_base;
  364. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  365. memcpy (&assembly->data[assembly->index], &data[datasize],
  366. iovec[0].iov_len - datasize);
  367. } else {
  368. /*
  369. * The message originated from local processor
  370. * becasue there is greater than one iovec for then full msg.
  371. */
  372. h_index = 0;
  373. for (i = 0; i < 2; i++) {
  374. memcpy (&header[h_index], iovec[i].iov_base, iovec[i].iov_len);
  375. h_index += iovec[i].iov_len;
  376. }
  377. mcast = (struct totempg_mcast *)header;
  378. // TODO make sure we are using a copy of mcast not the actual data itself
  379. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  380. for (i = 2; i < iov_len; i++) {
  381. a_i = assembly->index;
  382. assert (iovec[i].iov_len + a_i <= MESSAGE_SIZE_MAX);
  383. memcpy (&assembly->data[a_i], iovec[i].iov_base, iovec[i].iov_len);
  384. a_i += msg_lens[i - 2];
  385. }
  386. iov_len -= 2;
  387. }
  388. if (endian_conversion_required) {
  389. mcast->msg_count = swab16 (mcast->msg_count);
  390. for (i = 0; i < mcast->msg_count; i++) {
  391. msg_lens[i] = swab16 (msg_lens[i]);
  392. }
  393. }
  394. /*
  395. printf ("Message fragmented %d count %d\n", mcast->fragmented, mcast->msg_count);
  396. for (i = 0; i < mcast->msg_count; i++) {
  397. printf ("len[%d] = %d\n", i, msg_lens[i]);
  398. }
  399. */
  400. /*
  401. * If the last message in the buffer is a fragment, then we
  402. * can't deliver it. We'll first deliver the full messages
  403. * then adjust the assembly buffer so we can add the rest of the
  404. * fragment when it arrives.
  405. */
  406. msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
  407. continuation = mcast->continuation;
  408. iov_delv.iov_base = &assembly->data[0];
  409. iov_delv.iov_len = assembly->index + msg_lens[0];
  410. // printf ("%d %d %d\n", msg_count, continuation, assembly->last_frag_num);
  411. /*
  412. * Make sure that if this message is a continuation, that it
  413. * matches the sequence number of the previous fragment.
  414. * Also, if the first packed message is a continuation
  415. * of a previous message, but the assembly buffer
  416. * is empty, then we need to discard it since we can't
  417. * assemble a complete message. Likewise, if this message isn't a
  418. * continuation and the assembly buffer is empty, we have to discard
  419. * the continued message.
  420. */
  421. start = 0;
  422. if (continuation) {
  423. if (continuation != assembly->last_frag_num) {
  424. printf("Message continuation doesn't match previous frag e: %u - a: %u\n",
  425. assembly->last_frag_num, continuation);
  426. continuation = 0;
  427. }
  428. if ((assembly->index == 0) ||
  429. (!continuation && assembly->index)) {
  430. printf("Throwing away broken message: continuation %u, index %u\n",
  431. continuation, assembly->index);
  432. continuation = 0;
  433. }
  434. /*
  435. * we decided to throw away the first continued message
  436. * in this buffer, if continuation was set to zero.
  437. */
  438. if (!continuation) {
  439. assembly->index += msg_lens[0];
  440. iov_delv.iov_base = &assembly->data[assembly->index];
  441. iov_delv.iov_len = msg_lens[1];
  442. start = 1;
  443. }
  444. }
  445. for (i = start; i < msg_count; i++) {
  446. app_deliver_fn(source_addr, &iov_delv, 1,
  447. endian_conversion_required);
  448. assembly->index += msg_lens[i];
  449. iov_delv.iov_base = &assembly->data[assembly->index];
  450. if (i < (msg_count - 1)) {
  451. iov_delv.iov_len = msg_lens[i + 1];
  452. }
  453. }
  454. if (mcast->fragmented) {
  455. assembly->last_frag_num = mcast->fragmented;
  456. if (mcast->msg_count > 1) {
  457. memmove (&assembly->data[0],
  458. &assembly->data[assembly->index],
  459. msg_lens[msg_count]);
  460. assembly->index = 0;
  461. }
  462. assembly->index += msg_lens[msg_count];
  463. } else {
  464. assembly->last_frag_num = 0;
  465. assembly->index = 0;
  466. }
  467. }
  468. /*
  469. * Totem Process Group Abstraction
  470. * depends on poll abstraction, POSIX, IPV4
  471. */
  472. void *callback_token_received_handle;
  473. int callback_token_received_fn (enum totem_callback_token_type type,
  474. void *data)
  475. {
  476. struct totempg_mcast mcast;
  477. struct iovec iovecs[3];
  478. int res;
  479. if (mcast_packed_msg_count == 0) {
  480. return (0);
  481. }
  482. if (totemmrp_avail() == 0) {
  483. return (0);
  484. }
  485. mcast.fragmented = 0;
  486. /*
  487. * Was the first message in this buffer a continuation of a
  488. * fragmented message?
  489. */
  490. mcast.continuation = fragment_continuation;
  491. fragment_continuation = 0;
  492. mcast.msg_count = mcast_packed_msg_count;
  493. iovecs[0].iov_base = &mcast;
  494. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  495. iovecs[1].iov_base = mcast_packed_msg_lens;
  496. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  497. iovecs[2].iov_base = &fragmentation_data[0];
  498. iovecs[2].iov_len = fragment_size;
  499. res = totemmrp_mcast (iovecs, 3, 0);
  500. mcast_packed_msg_count = 0;
  501. fragment_size = 0;
  502. return (0);
  503. }
  504. /*
  505. * Initialize the totem process group abstraction
  506. */
  507. int totempg_initialize (
  508. poll_handle poll_handle,
  509. struct totem_config *totem_config)
  510. {
  511. int res;
  512. totempg_totem_config = totem_config;
  513. fragmentation_data = malloc (TOTEMPG_PACKET_SIZE);
  514. if (fragmentation_data == 0) {
  515. return (-1);
  516. }
  517. res = totemmrp_initialize (
  518. poll_handle,
  519. totem_config,
  520. totempg_deliver_fn,
  521. totempg_confchg_fn);
  522. totemmrp_callback_token_create (
  523. &callback_token_received_handle,
  524. TOTEM_CALLBACK_TOKEN_RECEIVED,
  525. 0,
  526. callback_token_received_fn,
  527. 0);
  528. totemsrp_net_mtu_adjust (totem_config);
  529. return (res);
  530. }
  531. void totempg_finalize (void)
  532. {
  533. totemmrp_finalize ();
  534. }
  535. static unsigned char next_fragment = 1;
  536. /*
  537. * Multicast a message
  538. */
  539. static int mcast_msg (
  540. struct iovec *iovec,
  541. int iov_len,
  542. int guarantee)
  543. {
  544. int res = 0;
  545. struct totempg_mcast mcast;
  546. struct iovec iovecs[3];
  547. int i;
  548. int max_packet_size = 0;
  549. int copy_len = 0;
  550. int copy_base = 0;
  551. int total_size = 0;
  552. totemmrp_new_msg_signal ();
  553. max_packet_size = TOTEMPG_PACKET_SIZE -
  554. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  555. mcast_packed_msg_lens[mcast_packed_msg_count] = 0;
  556. /*
  557. * Check if we would overwrite new message queue
  558. */
  559. for (i = 0; i < iov_len; i++) {
  560. total_size += iovec[i].iov_len;
  561. }
  562. if (send_ok (total_size + sizeof(unsigned short) *
  563. (mcast_packed_msg_count+1)) == 0) {
  564. return(-1);
  565. }
  566. for (i = 0; i < iov_len; ) {
  567. mcast.fragmented = 0;
  568. mcast.continuation = fragment_continuation;
  569. copy_len = iovec[i].iov_len - copy_base;
  570. /*
  571. * If it all fits with room left over, copy it in.
  572. * We need to leave at least sizeof(short) + 1 bytes in the
  573. * fragment_buffer on exit so that max_packet_size + fragment_size
  574. * doesn't exceed the size of the fragment_buffer on the next call.
  575. */
  576. if ((copy_len + fragment_size) <
  577. (max_packet_size - sizeof (unsigned short))) {
  578. memcpy (&fragmentation_data[fragment_size],
  579. iovec[i].iov_base + copy_base, copy_len);
  580. fragment_size += copy_len;
  581. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  582. copy_len = 0;
  583. copy_base = 0;
  584. i++;
  585. continue;
  586. /*
  587. * If it just fits or is too big, then send out what fits.
  588. */
  589. } else {
  590. unsigned char *data_ptr;
  591. copy_len = min(copy_len, max_packet_size - fragment_size);
  592. if( copy_len == max_packet_size )
  593. data_ptr = iovec[i].iov_base + copy_base;
  594. else {
  595. data_ptr = fragmentation_data;
  596. memcpy (&fragmentation_data[fragment_size],
  597. iovec[i].iov_base + copy_base, copy_len);
  598. }
  599. memcpy (&fragmentation_data[fragment_size],
  600. iovec[i].iov_base + copy_base, copy_len);
  601. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  602. /*
  603. * if we're not on the last iovec or the iovec is too large to
  604. * fit, then indicate a fragment. This also means that the next
  605. * message will have the continuation of this one.
  606. */
  607. if ((i < (iov_len - 1)) ||
  608. ((copy_base + copy_len) < iovec[i].iov_len)) {
  609. if (!next_fragment) {
  610. next_fragment++;
  611. }
  612. fragment_continuation = next_fragment;
  613. mcast.fragmented = next_fragment++;
  614. assert(fragment_continuation != 0);
  615. assert(mcast.fragmented != 0);
  616. } else {
  617. fragment_continuation = 0;
  618. }
  619. /*
  620. * assemble the message and send it
  621. */
  622. mcast.msg_count = ++mcast_packed_msg_count;
  623. iovecs[0].iov_base = &mcast;
  624. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  625. iovecs[1].iov_base = mcast_packed_msg_lens;
  626. iovecs[1].iov_len = mcast_packed_msg_count *
  627. sizeof(unsigned short);
  628. iovecs[2].iov_base = data_ptr;
  629. iovecs[2].iov_len = max_packet_size;
  630. assert (totemmrp_avail() > 0);
  631. res = totemmrp_mcast (iovecs, 3, guarantee);
  632. /*
  633. * Recalculate counts and indexes for the next.
  634. */
  635. mcast_packed_msg_lens[0] = 0;
  636. mcast_packed_msg_count = 0;
  637. fragment_size = 0;
  638. max_packet_size = TOTEMPG_PACKET_SIZE - (sizeof(unsigned short));
  639. /*
  640. * If the iovec all fit, go to the next iovec
  641. */
  642. if ((copy_base + copy_len) == iovec[i].iov_len) {
  643. copy_len = 0;
  644. copy_base = 0;
  645. i++;
  646. /*
  647. * Continue with the rest of the current iovec.
  648. */
  649. } else {
  650. copy_base += copy_len;
  651. }
  652. }
  653. }
  654. /*
  655. * Bump only if we added message data. This may be zero if
  656. * the last buffer just fit into the fragmentation_data buffer
  657. * and we were at the last iovec.
  658. */
  659. if (mcast_packed_msg_lens[mcast_packed_msg_count]) {
  660. mcast_packed_msg_count++;
  661. }
  662. return (res);
  663. }
  664. /*
  665. * Determine if a message of msg_size could be queued
  666. */
  667. static int send_ok (
  668. int msg_size)
  669. {
  670. int avail = 0;
  671. int total;
  672. avail = totemmrp_avail ();
  673. /*
  674. * msg size less then totempg_totem_config->net_mtu - 25 will take up
  675. * a full message, so add +1
  676. * totempg_totem_config->net_mtu - 25 is for the totempg_mcast header
  677. */
  678. total = (msg_size / (totempg_totem_config->net_mtu - 25)) + 1;
  679. return (avail >= total);
  680. }
  681. int totempg_callback_token_create (
  682. void **handle_out,
  683. enum totem_callback_token_type type,
  684. int delete,
  685. int (*callback_fn) (enum totem_callback_token_type type, void *),
  686. void *data)
  687. {
  688. return totemmrp_callback_token_create (handle_out, type, delete, callback_fn, data);
  689. }
  690. void totempg_callback_token_destroy (
  691. void *handle_out)
  692. {
  693. totemmrp_callback_token_destroy (handle_out);
  694. }
  695. /*
  696. * vi: set autoindent tabstop=4 shiftwidth=4 :
  697. */
  698. int totempg_groups_initialize (
  699. totempg_groups_handle *handle,
  700. void (*deliver_fn) (
  701. struct totem_ip_address *source_addr,
  702. struct iovec *iovec,
  703. int iov_len,
  704. int endian_conversion_required),
  705. void (*confchg_fn) (
  706. enum totem_configuration_type configuration_type,
  707. struct totem_ip_address *member_list, int member_list_entries,
  708. struct totem_ip_address *left_list, int left_list_entries,
  709. struct totem_ip_address *joined_list, int joined_list_entries,
  710. struct memb_ring_id *ring_id))
  711. {
  712. SaAisErrorT error;
  713. struct totempg_group_instance *instance;
  714. error = saHandleCreate (&totempg_groups_instance_database,
  715. sizeof (struct totempg_group_instance), handle);
  716. if (error != SA_OK) {
  717. goto error_exit;
  718. }
  719. if (*handle > totempg_max_handle) {
  720. totempg_max_handle = *handle;
  721. }
  722. error = saHandleInstanceGet (&totempg_groups_instance_database, *handle,
  723. (void *)&instance);
  724. if (error != SA_OK) {
  725. goto error_destroy;
  726. }
  727. instance->deliver_fn = deliver_fn;
  728. instance->confchg_fn = confchg_fn;
  729. instance->groups = 0;
  730. instance->groups_cnt = 0;
  731. saHandleInstancePut (&totempg_groups_instance_database, *handle);
  732. return (0);
  733. error_destroy:
  734. saHandleDestroy (&totempg_groups_instance_database, *handle);
  735. error_exit:
  736. return (-1);
  737. }
  738. int totempg_groups_join (
  739. totempg_groups_handle handle,
  740. struct totempg_group *groups,
  741. int group_cnt)
  742. {
  743. SaAisErrorT error;
  744. struct totempg_group_instance *instance;
  745. struct totempg_group *new_groups;
  746. error = saHandleInstanceGet (&totempg_groups_instance_database, handle,
  747. (void *)&instance);
  748. if (error != SA_OK) {
  749. goto error_exit;
  750. }
  751. new_groups = realloc (instance->groups,
  752. sizeof (struct totempg_group) *
  753. (instance->groups_cnt + group_cnt));
  754. if (new_groups == 0) {
  755. error = ENOMEM;
  756. goto error_exit;
  757. }
  758. memcpy (&new_groups[instance->groups_cnt],
  759. groups, group_cnt * sizeof (struct totempg_group));
  760. instance->groups = new_groups;
  761. instance->groups_cnt = instance->groups_cnt = group_cnt;
  762. saHandleInstancePut (&totempg_groups_instance_database, handle);
  763. return (0);
  764. error_exit:
  765. return (error);
  766. }
  767. int totempg_groups_leave (
  768. totempg_groups_handle handle,
  769. struct totempg_group *groups,
  770. int group_cnt)
  771. {
  772. SaAisErrorT error;
  773. struct totempg_group_instance *instance;
  774. error = saHandleInstanceGet (&totempg_groups_instance_database, handle,
  775. (void *)&instance);
  776. if (error != SA_OK) {
  777. goto error_exit;
  778. }
  779. saHandleInstancePut (&totempg_groups_instance_database, handle);
  780. return (0);
  781. error_exit:
  782. return (error);
  783. }
  784. #define MAX_IOVECS_FROM_APP 32
  785. #define MAX_GROUPS_PER_MSG 32
  786. int totempg_groups_mcast_joined (
  787. totempg_groups_handle handle,
  788. struct iovec *iovec,
  789. int iov_len,
  790. int guarantee)
  791. {
  792. SaAisErrorT error;
  793. struct totempg_group_instance *instance;
  794. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  795. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  796. int i;
  797. error = saHandleInstanceGet (&totempg_groups_instance_database, handle,
  798. (void *)&instance);
  799. if (error != SA_OK) {
  800. goto error_exit;
  801. }
  802. /*
  803. * Build group_len structure and the iovec_mcast structure
  804. */
  805. group_len[0] = instance->groups_cnt;
  806. for (i = 0; i < instance->groups_cnt; i++) {
  807. group_len[i + 1] = instance->groups[i].group_len;
  808. iovec_mcast[i + 1].iov_len = instance->groups[i].group_len;
  809. iovec_mcast[i + 1].iov_base = instance->groups[i].group;
  810. }
  811. iovec_mcast[0].iov_len = (instance->groups_cnt + 1) * sizeof (unsigned short);
  812. iovec_mcast[0].iov_base = group_len;
  813. for (i = 0; i < iov_len; i++) {
  814. iovec_mcast[i + instance->groups_cnt + 1].iov_len = iovec[i].iov_len;
  815. iovec_mcast[i + instance->groups_cnt + 1].iov_base = iovec[i].iov_base;
  816. }
  817. mcast_msg (iovec_mcast, iov_len + instance->groups_cnt + 1, guarantee);
  818. saHandleInstancePut (&totempg_groups_instance_database, handle);
  819. return (0);
  820. error_exit:
  821. return (error);
  822. }
  823. int totempg_groups_send_ok_joined (
  824. totempg_groups_handle handle,
  825. struct iovec *iovec,
  826. int iov_len)
  827. {
  828. SaAisErrorT error;
  829. struct totempg_group_instance *instance;
  830. unsigned int size = 0;
  831. unsigned int i;
  832. unsigned int res;
  833. error = saHandleInstanceGet (&totempg_groups_instance_database, handle,
  834. (void *)&instance);
  835. if (error != SA_OK) {
  836. goto error_exit;
  837. }
  838. for (i = 0; i < instance->groups_cnt; i++) {
  839. size += instance->groups[i].group_len;
  840. }
  841. for (i = 0; i < iov_len; i++) {
  842. size += iovec[i].iov_len;
  843. }
  844. res = send_ok (size);
  845. saHandleInstancePut (&totempg_groups_instance_database, handle);
  846. return (res);
  847. error_exit:
  848. return (error);
  849. }
  850. int totempg_groups_mcast_groups (
  851. totempg_groups_handle handle,
  852. int guarantee,
  853. struct totempg_group *groups,
  854. int groups_cnt,
  855. struct iovec *iovec,
  856. int iov_len)
  857. {
  858. SaAisErrorT error;
  859. struct totempg_group_instance *instance;
  860. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  861. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  862. int i;
  863. error = saHandleInstanceGet (&totempg_groups_instance_database, handle,
  864. (void *)&instance);
  865. if (error != SA_OK) {
  866. goto error_exit;
  867. }
  868. /*
  869. * Build group_len structure and the iovec_mcast structure
  870. */
  871. group_len[0] = groups_cnt;
  872. for (i = 0; i < groups_cnt; i++) {
  873. group_len[i + 1] = groups[i].group_len;
  874. iovec_mcast[i + 1].iov_len = groups[i].group_len;
  875. iovec_mcast[i + 1].iov_base = groups[i].group;
  876. }
  877. iovec_mcast[0].iov_len = (groups_cnt + 1) * sizeof (unsigned short);
  878. iovec_mcast[0].iov_base = group_len;
  879. for (i = 0; i < iov_len; i++) {
  880. iovec_mcast[i + groups_cnt + 1].iov_len = iovec[i].iov_len;
  881. iovec_mcast[i + groups_cnt + 1].iov_base = iovec[i].iov_base;
  882. }
  883. mcast_msg (iovec_mcast, iov_len + groups_cnt + 1, guarantee);
  884. saHandleInstancePut (&totempg_groups_instance_database, handle);
  885. return (0);
  886. error_exit:
  887. return (error);
  888. }
  889. int totempg_groups_send_ok_groups (
  890. totempg_groups_handle handle,
  891. struct totempg_group *groups,
  892. int groups_cnt,
  893. struct iovec *iovec,
  894. int iov_len)
  895. {
  896. SaAisErrorT error;
  897. struct totempg_group_instance *instance;
  898. unsigned int size = 0;
  899. unsigned int i;
  900. unsigned int res;
  901. error = saHandleInstanceGet (&totempg_groups_instance_database, handle,
  902. (void *)&instance);
  903. if (error != SA_OK) {
  904. goto error_exit;
  905. }
  906. for (i = 0; i < groups_cnt; i++) {
  907. size += groups[i].group_len;
  908. }
  909. for (i = 0; i < iov_len; i++) {
  910. size += iovec[i].iov_len;
  911. }
  912. res = send_ok (size);
  913. saHandleInstancePut (&totempg_groups_instance_database, handle);
  914. return (0);
  915. error_exit:
  916. return (error);
  917. }