totempg.c 27 KB

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